blob: c2c99237936c5241d0b35d3436b01b220264da18 [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
Ian Rogers7b3ddd22013-02-21 15:19:52 -080056void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070057 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070058 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070059 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070060 register_lines_.reset(new RegisterLine*[insns_size]());
61 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070062 for (uint32_t i = 0; i < insns_size; i++) {
63 bool interesting = false;
64 switch (mode) {
65 case kTrackRegsAll:
66 interesting = flags[i].IsOpcode();
67 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070068 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070069 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070070 break;
71 case kTrackRegsBranches:
72 interesting = flags[i].IsBranchTarget();
73 break;
74 default:
75 break;
76 }
77 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070078 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
79 }
80 }
81}
82
83PcToRegisterLineTable::~PcToRegisterLineTable() {
84 for (size_t i = 0; i < size_; i++) {
85 delete register_lines_[i];
86 if (kIsDebugBuild) {
87 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
89 }
90}
91
Andreas Gampe7c038102014-10-27 20:08:46 -070092// Note: returns true on failure.
93ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
94 const char* error_msg, uint32_t work_insn_idx) {
95 if (kIsDebugBuild) {
96 // In a debug build, abort if the error condition is wrong.
97 DCHECK(condition) << error_msg << work_insn_idx;
98 } else {
99 // In a non-debug build, just fail the class.
100 if (!condition) {
101 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
102 return true;
103 }
104 }
105
106 return false;
107}
108
Stephen Kyle7e541c92014-12-17 17:10:02 +0000109static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
110 if (verifier->IsConstructor()) {
111 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
112 reg_line->CheckConstructorReturn(verifier);
113 }
114 reg_line->MarkAllRegistersAsConflicts(verifier);
115}
116
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
119 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 mirror::Class* klass = method->GetDeclaringClass();
121 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
122 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 return VerifyClass(
164 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700165}
166
Ian Rogers7b078e82014-09-10 14:44:24 -0700167MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
168 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700169 Handle<mirror::DexCache> dex_cache,
170 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700171 const DexFile::ClassDef* class_def,
172 bool allow_soft_failures,
173 std::string* error) {
174 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700175 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700176 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700177 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700178 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700179 }
jeffhaof56197c2012-03-05 18:01:54 -0800180 ClassDataItemIterator it(*dex_file, class_data);
181 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
182 it.Next();
183 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700184 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700185 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700187 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800188 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700189 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800190 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700191 if (method_idx == previous_direct_method_idx) {
192 // smali can create dex files with two encoded_methods sharing the same method_idx
193 // http://code.google.com/p/smali/issues/detail?id=119
194 it.Next();
195 continue;
196 }
197 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700198 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199 ArtMethod* method = linker->ResolveMethod(
200 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700202 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700203 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700204 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 } else {
206 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700208 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700209 MethodVerifier::FailureKind result = VerifyMethod(self,
210 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700211 dex_file,
212 dex_cache,
213 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700214 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700215 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 if (result != kNoFailure) {
218 if (result == kHardFailure) {
219 hard_fail = true;
220 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700221 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700222 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 *error = "Verifier rejected class ";
224 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
225 *error += " due to bad method ";
226 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700227 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800229 }
230 it.Next();
231 }
jeffhao9b0b1882012-10-01 16:51:22 -0700232 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800233 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700234 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800235 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700236 if (method_idx == previous_virtual_method_idx) {
237 // smali can create dex files with two encoded_methods sharing the same method_idx
238 // http://code.google.com/p/smali/issues/detail?id=119
239 it.Next();
240 continue;
241 }
242 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700243 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 ArtMethod* method = linker->ResolveMethod(
245 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700247 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700248 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700249 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700250 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700251 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700252 MethodVerifier::FailureKind result = VerifyMethod(self,
253 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700254 dex_file,
255 dex_cache,
256 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700257 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700258 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700260 if (result != kNoFailure) {
261 if (result == kHardFailure) {
262 hard_fail = true;
263 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700264 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700265 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 *error = "Verifier rejected class ";
267 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
268 *error += " due to bad method ";
269 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700270 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700271 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800272 }
273 it.Next();
274 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700275 if (error_count == 0) {
276 return kNoFailure;
277 } else {
278 return hard_fail ? kHardFailure : kSoftFailure;
279 }
jeffhaof56197c2012-03-05 18:01:54 -0800280}
281
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700282static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700283 if (code_item == nullptr) {
284 return false;
285 }
286
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700287 uint16_t registers_size = code_item->registers_size_;
288 uint32_t insns_size = code_item->insns_size_in_code_units_;
289
290 return registers_size * insns_size > 4*1024*1024;
291}
292
Ian Rogers7b078e82014-09-10 14:44:24 -0700293MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700295 Handle<mirror::DexCache> dex_cache,
296 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700297 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700300 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700301 bool allow_soft_failures,
302 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700303 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700304 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700305
Ian Rogers7b078e82014-09-10 14:44:24 -0700306 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700307 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800308 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700309 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700310 // Verification completed, however failures may be pending that didn't cause the verification
311 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700312 CHECK(!verifier.have_pending_hard_failure_);
313 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700314 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700315 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700316 << PrettyMethod(method_idx, *dex_file) << "\n");
317 }
Ian Rogersc8982582012-09-07 16:53:25 -0700318 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800319 }
320 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700321 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700322 CHECK_NE(verifier.failures_.size(), 0U);
323 CHECK(verifier.have_pending_hard_failure_);
324 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700325 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800326 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 std::cout << "\n" << verifier.info_messages_.str();
328 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800329 }
Ian Rogersc8982582012-09-07 16:53:25 -0700330 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800331 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700332 if (kTimeVerifyMethod) {
333 uint64_t duration_ns = NanoTime() - start_ns;
334 if (duration_ns > MsToNs(100)) {
335 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700336 << " took " << PrettyDuration(duration_ns)
337 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700338 }
Ian Rogersc8982582012-09-07 16:53:25 -0700339 }
340 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800341}
342
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700343MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700344 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700345 Handle<mirror::DexCache> dex_cache,
346 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700347 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800350 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700351 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
352 class_def, code_item, dex_method_idx, method,
353 method_access_flags, true, true, true, true);
354 verifier->Verify();
355 verifier->DumpFailures(os);
356 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700357 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
358 // and querying any info is dangerous/can abort.
359 if (verifier->have_pending_hard_failure_) {
360 delete verifier;
361 return nullptr;
362 } else {
363 verifier->Dump(os);
364 return verifier;
365 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800366}
367
Ian Rogers7b078e82014-09-10 14:44:24 -0700368MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700369 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
370 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700371 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700372 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700374 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800375 bool need_precise_constants, bool verify_to_dump,
376 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700377 : self_(self),
378 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800379 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800380 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700381 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700382 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700383 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800384 dex_file_(dex_file),
385 dex_cache_(dex_cache),
386 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700387 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800388 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700389 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700390 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700391 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700392 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700393 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800394 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800395 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700396 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200397 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700398 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200399 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700400 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800401 verify_to_dump_(verify_to_dump),
402 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700403 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700404 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800405}
406
Mathieu Chartier590fee92013-09-13 13:46:47 -0700407MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700408 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700409 STLDeleteElements(&failure_messages_);
410}
411
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700413 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700415 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
416 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
418 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800419 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700420 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700421 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700422 verifier.FindLocksAtDexPc();
423}
424
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700425static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
426 const Instruction* inst = Instruction::At(code_item->insns_);
427
428 uint32_t insns_size = code_item->insns_size_in_code_units_;
429 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
430 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
431 return true;
432 }
433
434 dex_pc += inst->SizeInCodeUnits();
435 inst = inst->Next();
436 }
437
438 return false;
439}
440
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700442 CHECK(monitor_enter_dex_pcs_ != nullptr);
443 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700444
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700445 // Quick check whether there are any monitor_enter instructions at all.
446 if (!HasMonitorEnterInstructions(code_item_)) {
447 return;
448 }
449
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700450 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
451 // verification. In practice, the phase we want relies on data structures set up by all the
452 // earlier passes, so we just run the full method verification and bail out early when we've
453 // got what we wanted.
454 Verify();
455}
456
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
458 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700459 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
460 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700461 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
462 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
463 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200464 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200465}
466
Mathieu Chartierc7853442015-03-27 14:35:38 -0700467ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200469
470 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
471 // verification. In practice, the phase we want relies on data structures set up by all the
472 // earlier passes, so we just run the full method verification and bail out early when we've
473 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200474 bool success = Verify();
475 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700476 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200477 }
478 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700480 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200481 }
482 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
483 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200484}
485
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
487 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700488 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
489 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
491 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
492 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200493 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200494}
495
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700497 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200498
499 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
500 // verification. In practice, the phase we want relies on data structures set up by all the
501 // earlier passes, so we just run the full method verification and bail out early when we've
502 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200503 bool success = Verify();
504 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700505 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200506 }
507 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 if (register_line == nullptr) {
509 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200510 }
511 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
512 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800513 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200514}
515
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800517 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800519 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
520 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800521 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800523 true, true, false, true);
524 return verifier.FindStringInitMap();
525}
526
527SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
528 Verify();
529 return GetStringInitPcRegMap();
530}
531
Ian Rogersad0b3a32012-04-16 14:50:24 -0700532bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700534 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700535 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700537 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 } else {
539 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700540 }
jeffhaobdb76512011-09-07 11:43:16 -0700541 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
543 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
545 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700547 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800549 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 // Run through the instructions and see if the width checks out.
551 bool result = ComputeWidthsAndCountOps();
552 // Flag instructions guarded by a "try" block and check exception handlers.
553 result = result && ScanTryCatchBlocks();
554 // Perform static instruction verification.
555 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000557 result = result && VerifyCodeFlow();
558 // Compute information for compiler.
559 if (result && Runtime::Current()->IsCompiler()) {
560 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
561 }
562 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700563}
564
Ian Rogers776ac1f2012-04-13 23:36:36 -0700565std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566 switch (error) {
567 case VERIFY_ERROR_NO_CLASS:
568 case VERIFY_ERROR_NO_FIELD:
569 case VERIFY_ERROR_NO_METHOD:
570 case VERIFY_ERROR_ACCESS_CLASS:
571 case VERIFY_ERROR_ACCESS_FIELD:
572 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700573 case VERIFY_ERROR_INSTANTIATION:
574 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700575 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800576 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700577 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
578 // class change and instantiation errors into soft verification errors so that we re-verify
579 // at runtime. We may fail to find or to agree on access because of not yet available class
580 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
581 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
582 // paths" that dynamically perform the verification and cause the behavior to be that akin
583 // to an interpreter.
584 error = VERIFY_ERROR_BAD_CLASS_SOFT;
585 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700586 // If we fail again at runtime, mark that this instruction would throw and force this
587 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700588 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700589
590 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
591 // try to merge garbage.
592 // Note: this assumes that Fail is called before we do any work_line modifications.
593 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
594 const Instruction* inst = Instruction::At(insns);
595 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
596
597 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
598 saved_line_->CopyFromLine(work_line_.get());
599 }
jeffhaofaf459e2012-08-31 15:32:47 -0700600 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700601 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700602 // Indication that verification should be retried at runtime.
603 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700604 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700605 have_pending_hard_failure_ = true;
606 }
607 break;
jeffhaod5347e02012-03-22 17:25:05 -0700608 // Hard verification failures at compile time will still fail at runtime, so the class is
609 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700610 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800611 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700612 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000613 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800614 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700615 have_pending_hard_failure_ = true;
616 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800617 }
618 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700619 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700620 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700621 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700622 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700623 failure_messages_.push_back(failure_message);
624 return *failure_message;
625}
626
Ian Rogers576ca0c2014-06-06 15:58:22 -0700627std::ostream& MethodVerifier::LogVerifyInfo() {
628 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
629 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
630}
631
Ian Rogersad0b3a32012-04-16 14:50:24 -0700632void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
633 size_t failure_num = failure_messages_.size();
634 DCHECK_NE(failure_num, 0U);
635 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
636 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700637 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 delete last_fail_message;
639}
640
641void MethodVerifier::AppendToLastFailMessage(std::string append) {
642 size_t failure_num = failure_messages_.size();
643 DCHECK_NE(failure_num, 0U);
644 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
645 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800646}
647
Ian Rogers776ac1f2012-04-13 23:36:36 -0700648bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 const uint16_t* insns = code_item_->insns_;
650 size_t insns_size = code_item_->insns_size_in_code_units_;
651 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700652 size_t new_instance_count = 0;
653 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700655
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700657 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700658 switch (opcode) {
659 case Instruction::APUT_OBJECT:
660 case Instruction::CHECK_CAST:
661 has_check_casts_ = true;
662 break;
663 case Instruction::INVOKE_VIRTUAL:
664 case Instruction::INVOKE_VIRTUAL_RANGE:
665 case Instruction::INVOKE_INTERFACE:
666 case Instruction::INVOKE_INTERFACE_RANGE:
667 has_virtual_or_interface_invokes_ = true;
668 break;
669 case Instruction::MONITOR_ENTER:
670 monitor_enter_count++;
671 break;
672 case Instruction::NEW_INSTANCE:
673 new_instance_count++;
674 break;
675 default:
676 break;
jeffhaobdb76512011-09-07 11:43:16 -0700677 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700679 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700681 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700682 }
683
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
686 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700687 return false;
688 }
689
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 new_instance_count_ = new_instance_count;
691 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700692 return true;
693}
694
Ian Rogers776ac1f2012-04-13 23:36:36 -0700695bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700697 if (tries_size == 0) {
698 return true;
699 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700701 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700702
703 for (uint32_t idx = 0; idx < tries_size; idx++) {
704 const DexFile::TryItem* try_item = &tries[idx];
705 uint32_t start = try_item->start_addr_;
706 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700707 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
709 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700710 return false;
711 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700713 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
714 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700715 return false;
716 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700717 uint32_t dex_pc = start;
718 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
719 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700721 size_t insn_size = inst->SizeInCodeUnits();
722 dex_pc += insn_size;
723 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700724 }
725 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800726 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700727 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700728 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700729 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700730 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700731 CatchHandlerIterator iterator(handlers_ptr);
732 for (; iterator.HasNext(); iterator.Next()) {
733 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700735 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
736 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700737 return false;
738 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100739 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
740 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
741 << "exception handler begins with move-result* (" << dex_pc << ")";
742 return false;
743 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700745 // Ensure exception types are resolved so that they don't need resolution to be delivered,
746 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700747 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800748 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
749 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700750 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700751 if (exception_type == nullptr) {
752 DCHECK(self_->IsExceptionPending());
753 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700754 }
755 }
jeffhaobdb76512011-09-07 11:43:16 -0700756 }
Ian Rogers0571d352011-11-03 19:51:38 -0700757 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700758 }
jeffhaobdb76512011-09-07 11:43:16 -0700759 return true;
760}
761
Ian Rogers776ac1f2012-04-13 23:36:36 -0700762bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700764
Ian Rogers0c7abda2012-09-19 13:33:42 -0700765 /* 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 -0700766 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700767 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700768
769 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700770 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700772 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 return false;
774 }
775 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700776 // All invoke points are marked as "Throw" points already.
777 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000778 if (inst->IsBranch()) {
779 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
780 // The compiler also needs safepoints for fall-through to loop heads.
781 // Such a loop head must be a target of a branch.
782 int32_t offset = 0;
783 bool cond, self_ok;
784 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
785 DCHECK(target_ok);
786 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
787 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700788 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000789 } else if (inst->IsReturn()) {
790 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 }
792 dex_pc += inst->SizeInCodeUnits();
793 inst = inst->Next();
794 }
795 return true;
796}
797
Ian Rogers776ac1f2012-04-13 23:36:36 -0700798bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 bool result = true;
800 switch (inst->GetVerifyTypeArgumentA()) {
801 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700802 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 break;
804 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700805 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 break;
807 }
808 switch (inst->GetVerifyTypeArgumentB()) {
809 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700810 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 break;
812 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700813 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 break;
815 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700816 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700817 break;
818 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700819 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 break;
821 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700822 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 break;
824 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700825 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 break;
827 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700828 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 break;
830 }
831 switch (inst->GetVerifyTypeArgumentC()) {
832 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700833 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 break;
835 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700836 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 break;
838 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700839 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 break;
841 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700842 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 break;
844 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700845 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 break;
847 }
848 switch (inst->GetVerifyExtraFlags()) {
849 case Instruction::kVerifyArrayData:
850 result = result && CheckArrayData(code_offset);
851 break;
852 case Instruction::kVerifyBranchTarget:
853 result = result && CheckBranchTarget(code_offset);
854 break;
855 case Instruction::kVerifySwitchTargets:
856 result = result && CheckSwitchTargets(code_offset);
857 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700858 case Instruction::kVerifyVarArgNonZero:
859 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700860 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700861 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
863 "non-range invoke";
864 return false;
865 }
Ian Rogers29a26482014-05-02 15:27:29 -0700866 uint32_t args[Instruction::kMaxVarArgRegs];
867 inst->GetVarArgs(args);
868 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700870 }
Andreas Gampec3314312014-06-19 18:13:29 -0700871 case Instruction::kVerifyVarArgRangeNonZero:
872 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700874 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
875 inst->VRegA() <= 0) {
876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
877 "range invoke";
878 return false;
879 }
Ian Rogers29a26482014-05-02 15:27:29 -0700880 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 break;
882 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 result = false;
885 break;
886 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800887 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
889 result = false;
890 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 return result;
892}
893
Ian Rogers7b078e82014-09-10 14:44:24 -0700894inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
897 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 return false;
899 }
900 return true;
901}
902
Ian Rogers7b078e82014-09-10 14:44:24 -0700903inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
906 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 return false;
908 }
909 return true;
910}
911
Ian Rogers7b078e82014-09-10 14:44:24 -0700912inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
915 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 return false;
917 }
918 return true;
919}
920
Ian Rogers7b078e82014-09-10 14:44:24 -0700921inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
924 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 return false;
926 }
927 return true;
928}
929
Ian Rogers7b078e82014-09-10 14:44:24 -0700930inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
933 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 return false;
935 }
936 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700937 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 return false;
941 }
942 return true;
943}
944
Ian Rogers7b078e82014-09-10 14:44:24 -0700945inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
948 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700949 return false;
950 }
951 return true;
952}
953
Ian Rogers7b078e82014-09-10 14:44:24 -0700954inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
957 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 return false;
959 }
960 return true;
961}
962
Ian Rogers776ac1f2012-04-13 23:36:36 -0700963bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
966 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 return false;
968 }
969 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700970 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 const char* cp = descriptor;
972 while (*cp++ == '[') {
973 bracket_count++;
974 }
975 if (bracket_count == 0) {
976 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700977 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
978 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 return false;
980 } else if (bracket_count > 255) {
981 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700982 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
983 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 return false;
985 }
986 return true;
987}
988
Ian Rogers776ac1f2012-04-13 23:36:36 -0700989bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
991 const uint16_t* insns = code_item_->insns_ + cur_offset;
992 const uint16_t* array_data;
993 int32_t array_data_offset;
994
995 DCHECK_LT(cur_offset, insn_count);
996 /* make sure the start of the array data table is in range */
997 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
998 if ((int32_t) cur_offset + array_data_offset < 0 ||
999 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001001 << ", data offset " << array_data_offset
1002 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 return false;
1004 }
1005 /* offset to array data table is a relative branch-style offset */
1006 array_data = insns + array_data_offset;
1007 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001008 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1010 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 return false;
1012 }
1013 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001014 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1016 /* make sure the end of the switch is in range */
1017 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1019 << ", data offset " << array_data_offset << ", end "
1020 << cur_offset + array_data_offset + table_size
1021 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
1024 return true;
1025}
1026
Ian Rogers776ac1f2012-04-13 23:36:36 -07001027bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 int32_t offset;
1029 bool isConditional, selfOkay;
1030 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1031 return false;
1032 }
1033 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1035 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 return false;
1037 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001038 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1039 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1042 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 return false;
1044 }
1045 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1046 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001047 if (abs_offset < 0 ||
1048 (uint32_t) abs_offset >= insn_count ||
1049 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001051 << reinterpret_cast<void*>(abs_offset) << ") at "
1052 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 return false;
1054 }
1055 insn_flags_[abs_offset].SetBranchTarget();
1056 return true;
1057}
1058
Ian Rogers776ac1f2012-04-13 23:36:36 -07001059bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 bool* selfOkay) {
1061 const uint16_t* insns = code_item_->insns_ + cur_offset;
1062 *pConditional = false;
1063 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001064 switch (*insns & 0xff) {
1065 case Instruction::GOTO:
1066 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001067 break;
1068 case Instruction::GOTO_32:
1069 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001070 *selfOkay = true;
1071 break;
1072 case Instruction::GOTO_16:
1073 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001074 break;
1075 case Instruction::IF_EQ:
1076 case Instruction::IF_NE:
1077 case Instruction::IF_LT:
1078 case Instruction::IF_GE:
1079 case Instruction::IF_GT:
1080 case Instruction::IF_LE:
1081 case Instruction::IF_EQZ:
1082 case Instruction::IF_NEZ:
1083 case Instruction::IF_LTZ:
1084 case Instruction::IF_GEZ:
1085 case Instruction::IF_GTZ:
1086 case Instruction::IF_LEZ:
1087 *pOffset = (int16_t) insns[1];
1088 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001089 break;
1090 default:
1091 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001092 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001093 return true;
1094}
1095
Ian Rogers776ac1f2012-04-13 23:36:36 -07001096bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001098 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001100 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001102 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001104 << ", switch offset " << switch_offset
1105 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001106 return false;
1107 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001108 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001110 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001111 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1113 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001114 return false;
1115 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 uint32_t switch_count = switch_insns[1];
1117 int32_t keys_offset, targets_offset;
1118 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1120 /* 0=sig, 1=count, 2/3=firstKey */
1121 targets_offset = 4;
1122 keys_offset = -1;
1123 expected_signature = Instruction::kPackedSwitchSignature;
1124 } else {
1125 /* 0=sig, 1=count, 2..count*2 = keys */
1126 keys_offset = 2;
1127 targets_offset = 2 + 2 * switch_count;
1128 expected_signature = Instruction::kSparseSwitchSignature;
1129 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001131 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001132 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1133 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1134 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001135 return false;
1136 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001137 /* make sure the end of the switch is in range */
1138 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1140 << ", switch offset " << switch_offset
1141 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001142 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001143 return false;
1144 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001145 /* for a sparse switch, verify the keys are in ascending order */
1146 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1148 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001149 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1150 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1151 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1153 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001154 return false;
1155 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001156 last_key = key;
1157 }
1158 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001159 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 for (uint32_t targ = 0; targ < switch_count; targ++) {
1161 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1162 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1163 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001164 if (abs_offset < 0 ||
1165 abs_offset >= (int32_t) insn_count ||
1166 !insn_flags_[abs_offset].IsOpcode()) {
1167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1168 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1169 << reinterpret_cast<void*>(cur_offset)
1170 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001171 return false;
1172 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 insn_flags_[abs_offset].SetBranchTarget();
1174 }
1175 return true;
1176}
1177
Ian Rogers776ac1f2012-04-13 23:36:36 -07001178bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001179 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 return false;
1182 }
1183 uint16_t registers_size = code_item_->registers_size_;
1184 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001185 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1187 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 return false;
1189 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001190 }
1191
1192 return true;
1193}
1194
Ian Rogers776ac1f2012-04-13 23:36:36 -07001195bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 uint16_t registers_size = code_item_->registers_size_;
1197 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1198 // integer overflow when adding them here.
1199 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1201 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001202 return false;
1203 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001204 return true;
1205}
1206
Ian Rogers776ac1f2012-04-13 23:36:36 -07001207bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 uint16_t registers_size = code_item_->registers_size_;
1209 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001210
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001212 reg_table_.Init(kTrackCompilerInterestPoints,
1213 insn_flags_.get(),
1214 insns_size,
1215 registers_size,
1216 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001217
jeffhaobdb76512011-09-07 11:43:16 -07001218
Ian Rogersd0fbd852013-09-24 18:17:04 -07001219 work_line_.reset(RegisterLine::Create(registers_size, this));
1220 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001221
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 /* Initialize register types of method arguments. */
1223 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001224 DCHECK_NE(failures_.size(), 0U);
1225 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001226 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001227 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 return false;
1229 }
1230 /* Perform code flow verification. */
1231 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001232 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001234 }
jeffhaobdb76512011-09-07 11:43:16 -07001235 return true;
1236}
1237
Ian Rogersad0b3a32012-04-16 14:50:24 -07001238std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1239 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001240 for (size_t i = 0; i < failures_.size(); ++i) {
1241 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001242 }
1243 return os;
1244}
1245
Ian Rogers776ac1f2012-04-13 23:36:36 -07001246void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001247 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001248 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 return;
jeffhaobdb76512011-09-07 11:43:16 -07001250 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001251 {
1252 os << "Register Types:\n";
1253 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1254 std::ostream indent_os(&indent_filter);
1255 reg_types_.Dump(indent_os);
1256 }
Ian Rogersb4903572012-10-11 11:52:56 -07001257 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001258 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1259 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 const Instruction* inst = Instruction::At(code_item_->insns_);
1261 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001262 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001264 if (reg_line != nullptr) {
1265 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001266 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001267 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001268 const bool kDumpHexOfInstruction = false;
1269 if (kDumpHexOfInstruction) {
1270 indent_os << inst->DumpHex(5) << " ";
1271 }
1272 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001273 inst = inst->Next();
1274 }
jeffhaobdb76512011-09-07 11:43:16 -07001275}
1276
Ian Rogersd81871c2011-10-03 13:57:23 -07001277static bool IsPrimitiveDescriptor(char descriptor) {
1278 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001279 case 'I':
1280 case 'C':
1281 case 'S':
1282 case 'B':
1283 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001284 case 'F':
1285 case 'D':
1286 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001288 default:
1289 return false;
1290 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001291}
1292
Ian Rogers776ac1f2012-04-13 23:36:36 -07001293bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 RegisterLine* reg_line = reg_table_.GetLine(0);
1295 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1296 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001297
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001299 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001301 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1303 // argument as uninitialized. This restricts field access until the superclass constructor is
1304 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001305 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001306 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001307 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 reg_types_.UninitializedThisArgument(declaring_class));
1309 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001310 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001311 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001313 }
1314
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001315 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001316 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001317 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001318
1319 for (; iterator.HasNext(); iterator.Next()) {
1320 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001321 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 LOG(FATAL) << "Null descriptor";
1323 }
1324 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1326 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 return false;
1328 }
1329 switch (descriptor[0]) {
1330 case 'L':
1331 case '[':
1332 // We assume that reference arguments are initialized. The only way it could be otherwise
1333 // (assuming the caller was verified) is if the current method is <init>, but in that case
1334 // it's effectively considered initialized the instant we reach here (in the sense that we
1335 // can return without doing anything or call virtual methods).
1336 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001337 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001338 if (!reg_type.IsNonZeroReferenceTypes()) {
1339 DCHECK(HasFailures());
1340 return false;
1341 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001342 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 }
1344 break;
1345 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001346 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 break;
1348 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001349 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 break;
1351 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001352 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 break;
1354 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001355 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 break;
1357 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001358 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 break;
1360 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001361 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 break;
1363 case 'J':
1364 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001365 if (cur_arg + 1 >= expected_args) {
1366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1367 << " args, found more (" << descriptor << ")";
1368 return false;
1369 }
1370
Ian Rogers7b078e82014-09-10 14:44:24 -07001371 const RegType* lo_half;
1372 const RegType* hi_half;
1373 if (descriptor[0] == 'J') {
1374 lo_half = &reg_types_.LongLo();
1375 hi_half = &reg_types_.LongHi();
1376 } else {
1377 lo_half = &reg_types_.DoubleLo();
1378 hi_half = &reg_types_.DoubleHi();
1379 }
1380 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 cur_arg++;
1382 break;
1383 }
1384 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1386 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 return false;
1388 }
1389 cur_arg++;
1390 }
1391 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1393 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 return false;
1395 }
1396 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1397 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1398 // format. Only major difference from the method argument format is that 'V' is supported.
1399 bool result;
1400 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1401 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001402 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 size_t i = 0;
1404 do {
1405 i++;
1406 } while (descriptor[i] == '['); // process leading [
1407 if (descriptor[i] == 'L') { // object array
1408 do {
1409 i++; // find closing ;
1410 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1411 result = descriptor[i] == ';';
1412 } else { // primitive array
1413 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1414 }
1415 } else if (descriptor[0] == 'L') {
1416 // could be more thorough here, but shouldn't be required
1417 size_t i = 0;
1418 do {
1419 i++;
1420 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1421 result = descriptor[i] == ';';
1422 } else {
1423 result = false;
1424 }
1425 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1427 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 }
1429 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001430}
1431
Ian Rogers776ac1f2012-04-13 23:36:36 -07001432bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 const uint16_t* insns = code_item_->insns_;
1434 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001435
jeffhaobdb76512011-09-07 11:43:16 -07001436 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 insn_flags_[0].SetChanged();
1438 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001439
jeffhaobdb76512011-09-07 11:43:16 -07001440 /* Continue until no instructions are marked "changed". */
1441 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001442 if (allow_thread_suspension_) {
1443 self_->AllowThreadSuspension();
1444 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1446 uint32_t insn_idx = start_guess;
1447 for (; insn_idx < insns_size; insn_idx++) {
1448 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001449 break;
1450 }
jeffhaobdb76512011-09-07 11:43:16 -07001451 if (insn_idx == insns_size) {
1452 if (start_guess != 0) {
1453 /* try again, starting from the top */
1454 start_guess = 0;
1455 continue;
1456 } else {
1457 /* all flags are clear */
1458 break;
1459 }
1460 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 // We carry the working set of registers from instruction to instruction. If this address can
1462 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1463 // "changed" flags, we need to load the set of registers from the table.
1464 // Because we always prefer to continue on to the next instruction, we should never have a
1465 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1466 // target.
1467 work_insn_idx_ = insn_idx;
1468 if (insn_flags_[insn_idx].IsBranchTarget()) {
1469 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001470 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001471 /*
1472 * Sanity check: retrieve the stored register line (assuming
1473 * a full table) and make sure it actually matches.
1474 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001476 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 if (work_line_->CompareLine(register_line) != 0) {
1478 Dump(std::cout);
1479 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001480 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001481 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001482 << " work_line=" << work_line_->Dump(this) << "\n"
1483 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 }
jeffhaobdb76512011-09-07 11:43:16 -07001485 }
jeffhaobdb76512011-09-07 11:43:16 -07001486 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001488 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001489 prepend += " failed to verify: ";
1490 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001491 return false;
1492 }
jeffhaobdb76512011-09-07 11:43:16 -07001493 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001494 insn_flags_[insn_idx].SetVisited();
1495 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001496 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001497
Ian Rogers1c849e52012-06-28 14:00:33 -07001498 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001499 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001500 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001501 * (besides the wasted space), but it indicates a flaw somewhere
1502 * down the line, possibly in the verifier.
1503 *
1504 * If we've substituted "always throw" instructions into the stream,
1505 * we are almost certainly going to have some dead code.
1506 */
1507 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001509 for (; insn_idx < insns_size;
1510 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001511 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001512 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001513 * may or may not be preceded by a padding NOP (for alignment).
1514 */
1515 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1516 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1517 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001518 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001519 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1520 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1521 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001523 }
1524
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001526 if (dead_start < 0)
1527 dead_start = insn_idx;
1528 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001529 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1530 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001531 dead_start = -1;
1532 }
1533 }
1534 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001535 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1536 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001537 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001538 // To dump the state of the verify after a method, do something like:
1539 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1540 // "boolean java.lang.String.equals(java.lang.Object)") {
1541 // LOG(INFO) << info_messages_.str();
1542 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001543 }
jeffhaobdb76512011-09-07 11:43:16 -07001544 return true;
1545}
1546
Ian Rogers776ac1f2012-04-13 23:36:36 -07001547bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001548 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1549 // We want the state _before_ the instruction, for the case where the dex pc we're
1550 // interested in is itself a monitor-enter instruction (which is a likely place
1551 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001552 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001553 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001554 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1555 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1556 }
1557 }
1558
jeffhaobdb76512011-09-07 11:43:16 -07001559 /*
1560 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001561 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001562 * control to another statement:
1563 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001564 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001565 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001566 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001567 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001568 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001569 * throw an exception that is handled by an encompassing "try"
1570 * block.
1571 *
1572 * We can also return, in which case there is no successor instruction
1573 * from this point.
1574 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001575 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001576 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1578 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001579 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001580
jeffhaobdb76512011-09-07 11:43:16 -07001581 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001582 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001583 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001585 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001586 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 }
jeffhaobdb76512011-09-07 11:43:16 -07001588
1589 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001590 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001591 * can throw an exception, we will copy/merge this into the "catch"
1592 * address rather than work_line, because we don't want the result
1593 * from the "successful" code path (e.g. a check-cast that "improves"
1594 * a type) to be visible to the exception handler.
1595 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001596 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001598 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001600 }
1601
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001602
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001603 // We need to ensure the work line is consistent while performing validation. When we spot a
1604 // peephole pattern we compute a new line for either the fallthrough instruction or the
1605 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001606 std::unique_ptr<RegisterLine> branch_line;
1607 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001608
Stephen Kyle7e541c92014-12-17 17:10:02 +00001609 /*
1610 * If we are in a constructor, and we currently have an UninitializedThis type
1611 * in a register somewhere, we need to make sure it isn't overwritten.
1612 */
1613 bool track_uninitialized_this = false;
1614 size_t uninitialized_this_loc = 0;
1615 if (IsConstructor()) {
1616 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1617 }
1618
Sebastien Hertz5243e912013-05-21 10:55:07 +02001619 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001620 case Instruction::NOP:
1621 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001622 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001623 * a signature that looks like a NOP; if we see one of these in
1624 * the course of executing code then we have a problem.
1625 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001628 }
1629 break;
1630
1631 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001632 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 break;
jeffhaobdb76512011-09-07 11:43:16 -07001634 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001635 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 break;
jeffhaobdb76512011-09-07 11:43:16 -07001637 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001638 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001639 break;
1640 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001641 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 break;
jeffhaobdb76512011-09-07 11:43:16 -07001643 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001644 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 break;
jeffhaobdb76512011-09-07 11:43:16 -07001646 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001647 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001648 break;
1649 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001650 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 break;
jeffhaobdb76512011-09-07 11:43:16 -07001652 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001653 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 break;
jeffhaobdb76512011-09-07 11:43:16 -07001655 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001656 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001657 break;
1658
1659 /*
1660 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001661 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001662 * might want to hold the result in an actual CPU register, so the
1663 * Dalvik spec requires that these only appear immediately after an
1664 * invoke or filled-new-array.
1665 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001667 * redundant with the reset done below, but it can make the debug info
1668 * easier to read in some cases.)
1669 */
1670 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001671 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001672 break;
1673 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001674 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001675 break;
1676 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001677 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001678 break;
1679
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001681 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1682 // where one entrypoint to the catch block is not actually an exception path.
1683 if (work_insn_idx_ == 0) {
1684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1685 break;
1686 }
jeffhaobdb76512011-09-07 11:43:16 -07001687 /*
jeffhao60f83e32012-02-13 17:16:30 -08001688 * This statement can only appear as the first instruction in an exception handler. We verify
1689 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001690 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001691 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001692 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001693 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001696 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001697 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 }
jeffhaobdb76512011-09-07 11:43:16 -07001700 }
1701 break;
1702 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001703 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001704 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001705 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1708 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 } else {
1710 // Compilers may generate synthetic functions that write byte values into boolean fields.
1711 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001713 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1715 ((return_type.IsBoolean() || return_type.IsByte() ||
1716 return_type.IsShort() || return_type.IsChar()) &&
1717 src_type.IsInteger()));
1718 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001719 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001721 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001722 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 }
jeffhaobdb76512011-09-07 11:43:16 -07001724 }
1725 }
1726 break;
1727 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001728 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001729 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001730 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 } else {
1734 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001735 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001737 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 }
jeffhaobdb76512011-09-07 11:43:16 -07001740 }
1741 }
1742 break;
1743 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001745 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 } else {
1749 /* return_type is the *expected* return type, not register value */
1750 DCHECK(!return_type.IsZero());
1751 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001753 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001754 // Disallow returning uninitialized values and verify that the reference in vAA is an
1755 // instance of the "return_type"
1756 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001757 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1758 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001759 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001760 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1761 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1762 << "' or '" << reg_type << "'";
1763 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001764 bool soft_error = false;
1765 // Check whether arrays are involved. They will show a valid class status, even
1766 // if their components are erroneous.
1767 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1768 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1769 if (soft_error) {
1770 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1771 << reg_type << " vs " << return_type;
1772 }
1773 }
1774
1775 if (!soft_error) {
1776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1777 << "', but expected from declaration '" << return_type << "'";
1778 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001779 }
jeffhaobdb76512011-09-07 11:43:16 -07001780 }
1781 }
1782 }
1783 break;
1784
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001785 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 case Instruction::CONST_4: {
1787 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001788 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001789 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001790 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 }
1792 case Instruction::CONST_16: {
1793 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001794 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001795 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001796 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001797 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001798 case Instruction::CONST: {
1799 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001800 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001801 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001802 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001803 }
1804 case Instruction::CONST_HIGH16: {
1805 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001806 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001807 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001808 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001809 }
jeffhaobdb76512011-09-07 11:43:16 -07001810 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001811 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001812 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001813 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1814 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001815 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001817 }
1818 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001819 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001820 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1821 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001822 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001823 break;
1824 }
1825 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001827 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1828 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001829 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001830 break;
1831 }
1832 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001833 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001834 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1835 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001836 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001837 break;
1838 }
jeffhaobdb76512011-09-07 11:43:16 -07001839 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001840 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 break;
jeffhaobdb76512011-09-07 11:43:16 -07001842 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001843 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001846 // Get type from instruction if unresolved then we need an access check
1847 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001848 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001849 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001850 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001851 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001852 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001853 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 }
jeffhaobdb76512011-09-07 11:43:16 -07001855 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001856 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001857 break;
1858 case Instruction::MONITOR_EXIT:
1859 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001860 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001861 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001862 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001863 * to the need to handle asynchronous exceptions, a now-deprecated
1864 * feature that Dalvik doesn't support.)
1865 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001866 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001867 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001868 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001869 * structured locking checks are working, the former would have
1870 * failed on the -enter instruction, and the latter is impossible.
1871 *
1872 * This is fortunate, because issue 3221411 prevents us from
1873 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001874 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001875 * some catch blocks (which will show up as "dead" code when
1876 * we skip them here); if we can't, then the code path could be
1877 * "live" so we still need to check it.
1878 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001879 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
1882
Ian Rogers28ad40d2011-10-27 15:19:26 -07001883 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001885 /*
1886 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1887 * could be a "upcast" -- not expected, so we don't try to address it.)
1888 *
1889 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001890 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001891 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001892 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1893 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001894 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001895 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001896 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001897 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001898 if (klass != nullptr && klass->IsPrimitive()) {
1899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1900 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1901 << GetDeclaringClass();
1902 break;
1903 }
1904
Ian Rogersad0b3a32012-04-16 14:50:24 -07001905 DCHECK_NE(failures_.size(), 0U);
1906 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001908 }
1909 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001910 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001911 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001913 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001914 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 if (is_checkcast) {
1916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1917 } else {
1918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1919 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001920 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 if (is_checkcast) {
1922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1923 } else {
1924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1925 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001926 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001927 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001928 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001930 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001931 }
jeffhaobdb76512011-09-07 11:43:16 -07001932 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001933 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 }
1935 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001936 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001937 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001938 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001941 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001943 } else {
1944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 }
1946 break;
1947 }
1948 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001949 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001950 if (res_type.IsConflict()) {
1951 DCHECK_NE(failures_.size(), 0U);
1952 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001953 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001954 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1955 // can't create an instance of an interface or abstract class */
1956 if (!res_type.IsInstantiableTypes()) {
1957 Fail(VERIFY_ERROR_INSTANTIATION)
1958 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001959 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001961 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001962 // Any registers holding previous allocations from this address that have not yet been
1963 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001964 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001965 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 break;
1968 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001969 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001970 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001971 break;
1972 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001973 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001974 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001976 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001977 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001978 just_set_result = true; // Filled new array range sets result register
1979 break;
jeffhaobdb76512011-09-07 11:43:16 -07001980 case Instruction::CMPL_FLOAT:
1981 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001982 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001983 break;
1984 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001985 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001986 break;
1987 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001988 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001989 break;
1990 case Instruction::CMPL_DOUBLE:
1991 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001992 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001993 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001994 break;
1995 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001996 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001997 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001998 break;
1999 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002000 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002001 break;
2002 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002003 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002004 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002005 break;
2006 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002008 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002009 break;
2010 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002011 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002014 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002015 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002016 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2017 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002018 }
2019 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 }
jeffhaobdb76512011-09-07 11:43:16 -07002021 case Instruction::GOTO:
2022 case Instruction::GOTO_16:
2023 case Instruction::GOTO_32:
2024 /* no effect on or use of registers */
2025 break;
2026
2027 case Instruction::PACKED_SWITCH:
2028 case Instruction::SPARSE_SWITCH:
2029 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002030 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002031 break;
2032
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 case Instruction::FILL_ARRAY_DATA: {
2034 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002035 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002036 /* array_type can be null if the reg type is Zero */
2037 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002038 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2040 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002041 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002042 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002043 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002044 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2046 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 } else {
jeffhao457cc512012-02-02 16:55:13 -08002048 // Now verify if the element width in the table matches the element width declared in
2049 // the array
2050 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2051 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002053 } else {
2054 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2055 // Since we don't compress the data in Dex, expect to see equal width of data stored
2056 // in the table and expected from the array class.
2057 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2059 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002060 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 }
2062 }
jeffhaobdb76512011-09-07 11:43:16 -07002063 }
2064 }
2065 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 }
jeffhaobdb76512011-09-07 11:43:16 -07002067 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2070 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 bool mismatch = false;
2072 if (reg_type1.IsZero()) { // zero then integral or reference expected
2073 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2074 } else if (reg_type1.IsReferenceTypes()) { // both references?
2075 mismatch = !reg_type2.IsReferenceTypes();
2076 } else { // both integral?
2077 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2078 }
2079 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2081 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002082 }
2083 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 }
jeffhaobdb76512011-09-07 11:43:16 -07002085 case Instruction::IF_LT:
2086 case Instruction::IF_GE:
2087 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002089 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2090 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2093 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002094 }
2095 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 }
jeffhaobdb76512011-09-07 11:43:16 -07002097 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2102 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002104
2105 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002106 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002107 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002108 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002109 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002110 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002111 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002112 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2113 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2114 work_insn_idx_)) {
2115 break;
2116 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002117 } else {
2118 break;
2119 }
2120
Ian Rogers9b360392013-06-06 14:45:07 -07002121 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002122
2123 /* Check for peep-hole pattern of:
2124 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002125 * instance-of vX, vY, T;
2126 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002127 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002128 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002129 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002130 * and sharpen the type of vY to be type T.
2131 * Note, this pattern can't be if:
2132 * - if there are other branches to this branch,
2133 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002134 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002135 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002136 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2137 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2138 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002139 // Check the type of the instance-of is different than that of registers type, as if they
2140 // are the same there is no work to be done here. Check that the conversion is not to or
2141 // from an unresolved type as type information is imprecise. If the instance-of is to an
2142 // interface then ignore the type information as interfaces can only be treated as Objects
2143 // and we don't want to disallow field and other operations on the object. If the value
2144 // being instance-of checked against is known null (zero) then allow the optimization as
2145 // we didn't have type information. If the merge of the instance-of type with the original
2146 // type is assignable to the original then allow optimization. This check is performed to
2147 // ensure that subsequent merges don't lose type information - such as becoming an
2148 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002149 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002150 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002151
Ian Rogersebbdd872014-07-07 23:53:08 -07002152 if (!orig_type.Equals(cast_type) &&
2153 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002154 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002155 !cast_type.GetClass()->IsInterface() &&
2156 (orig_type.IsZero() ||
2157 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002158 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002159 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002160 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002161 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002162 branch_line.reset(update_line);
2163 }
2164 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002165 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002166 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2167 // See if instance-of was preceded by a move-object operation, common due to the small
2168 // register encoding space of instance-of, and propagate type information to the source
2169 // of the move-object.
2170 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002171 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002172 move_idx--;
2173 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002174 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2175 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2176 work_insn_idx_)) {
2177 break;
2178 }
Ian Rogers9b360392013-06-06 14:45:07 -07002179 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2180 switch (move_inst->Opcode()) {
2181 case Instruction::MOVE_OBJECT:
2182 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002183 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002184 }
2185 break;
2186 case Instruction::MOVE_OBJECT_FROM16:
2187 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002188 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002189 }
2190 break;
2191 case Instruction::MOVE_OBJECT_16:
2192 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002193 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002194 }
2195 break;
2196 default:
2197 break;
2198 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002199 }
2200 }
2201 }
2202
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 }
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::IF_LTZ:
2206 case Instruction::IF_GEZ:
2207 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002209 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2212 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 }
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 }
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 break;
jeffhaobdb76512011-09-07 11:43:16 -07002219 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 break;
jeffhaobdb76512011-09-07 11:43:16 -07002222 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 break;
jeffhaobdb76512011-09-07 11:43:16 -07002225 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002226 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002229 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 break;
jeffhaobdb76512011-09-07 11:43:16 -07002231 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 break;
2234 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
2237
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 break;
2241 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 break;
2244 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002245 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 break;
2247 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002254 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002258 break;
2259
jeffhaobdb76512011-09-07 11:43:16 -07002260 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002261 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 break;
jeffhaobdb76512011-09-07 11:43:16 -07002263 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002264 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 break;
jeffhaobdb76512011-09-07 11:43:16 -07002266 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002267 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 break;
jeffhaobdb76512011-09-07 11:43:16 -07002269 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002270 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 break;
2272 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002273 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002274 break;
2275 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002276 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002277 break;
2278 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002279 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2280 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 break;
jeffhaobdb76512011-09-07 11:43:16 -07002282
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002284 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 break;
2286 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002287 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 break;
2289 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002290 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 break;
2292 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002293 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002296 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
2298 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002299 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 break;
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002302 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2303 false);
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305
jeffhaobdb76512011-09-07 11:43:16 -07002306 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002307 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 break;
jeffhaobdb76512011-09-07 11:43:16 -07002309 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002310 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 break;
jeffhaobdb76512011-09-07 11:43:16 -07002312 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002313 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
jeffhaobdb76512011-09-07 11:43:16 -07002315 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002316 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
2318 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002319 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002322 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002325 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2326 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 break;
2328
2329 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002330 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 break;
2332 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002333 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 break;
2335 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002336 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 break;
2338 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002339 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002342 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002345 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002348 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2349 true);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351
2352 case Instruction::INVOKE_VIRTUAL:
2353 case Instruction::INVOKE_VIRTUAL_RANGE:
2354 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2357 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002358 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2359 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002360 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002361 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002362 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002363 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002364 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002365 if (return_type_class != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002366 return_type = &reg_types_.FromClass(called_method->GetReturnTypeDescriptor(),
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002367 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002368 return_type_class->CannotBeAssignedFromOtherTypes());
2369 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002370 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2371 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002372 }
2373 }
2374 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002375 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002376 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2377 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002378 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002379 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002380 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002381 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002382 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002384 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002385 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002386 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002391 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002392 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002393 const char* return_type_descriptor;
2394 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002395 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002396 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002398 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002399 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002400 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2401 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2402 } else {
2403 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002404 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002405 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002406 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002407 if (return_type_class != nullptr) {
2408 return_type = &reg_types_.FromClass(return_type_descriptor,
2409 return_type_class,
2410 return_type_class->CannotBeAssignedFromOtherTypes());
2411 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002412 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2413 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002414 }
Ian Rogers46685432012-06-03 22:26:43 -07002415 }
2416 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002417 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 * Some additional checks when calling a constructor. We know from the invocation arg check
2419 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2420 * that to require that called_method->klass is the same as this->klass or this->super,
2421 * allowing the latter only if the "this" argument is the same as the "this" argument to
2422 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002423 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002424 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002425 if (this_type.IsConflict()) // failure.
2426 break;
jeffhaobdb76512011-09-07 11:43:16 -07002427
jeffhaob57e9522012-04-26 18:08:21 -07002428 /* no null refs allowed (?) */
2429 if (this_type.IsZero()) {
2430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2431 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002432 }
jeffhaob57e9522012-04-26 18:08:21 -07002433
2434 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002435 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002436 // TODO: re-enable constructor type verification
2437 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002438 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002439 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2440 // break;
2441 // }
jeffhaob57e9522012-04-26 18:08:21 -07002442
2443 /* arg must be an uninitialized reference */
2444 if (!this_type.IsUninitializedTypes()) {
2445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2446 << this_type;
2447 break;
2448 }
2449
2450 /*
2451 * Replace the uninitialized reference with an initialized one. We need to do this for all
2452 * registers that have the same object instance in them, not just the "this" register.
2453 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002454 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2455 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002456 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002457 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002458 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2459 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002460 }
2461 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002462 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002463 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002464 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002465 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002466 just_set_result = true;
2467 break;
2468 }
2469 case Instruction::INVOKE_STATIC:
2470 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002471 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002472 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002473 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002474 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002475 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2477 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002478 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002479 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002480 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002481 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002482 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002483 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002484 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002485 } else {
2486 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2487 }
jeffhaobdb76512011-09-07 11:43:16 -07002488 just_set_result = true;
2489 }
2490 break;
jeffhaobdb76512011-09-07 11:43:16 -07002491 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002493 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002494 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002495 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002496 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002497 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2498 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2499 << PrettyMethod(abs_method) << "'";
2500 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002501 }
Ian Rogers0d604842012-04-16 14:50:24 -07002502 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002503 /* Get the type of the "this" arg, which should either be a sub-interface of called
2504 * interface or Object (see comments in RegType::JoinClass).
2505 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002506 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002507 if (this_type.IsZero()) {
2508 /* null pointer always passes (and always fails at runtime) */
2509 } else {
2510 if (this_type.IsUninitializedTypes()) {
2511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2512 << this_type;
2513 break;
2514 }
2515 // In the past we have tried to assert that "called_interface" is assignable
2516 // from "this_type.GetClass()", however, as we do an imprecise Join
2517 // (RegType::JoinClass) we don't have full information on what interfaces are
2518 // implemented by "this_type". For example, two classes may implement the same
2519 // interfaces and have a common parent that doesn't implement the interface. The
2520 // join will set "this_type" to the parent class and a test that this implements
2521 // the interface will incorrectly fail.
2522 }
2523 /*
2524 * We don't have an object instance, so we can't find the concrete method. However, all of
2525 * the type information is in the abstract method, so we're good.
2526 */
2527 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002528 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002529 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002530 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2531 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2532 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2533 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002534 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002535 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002536 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002537 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 } else {
2540 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2541 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002542 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 }
jeffhaobdb76512011-09-07 11:43:16 -07002545 case Instruction::NEG_INT:
2546 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002547 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002548 break;
2549 case Instruction::NEG_LONG:
2550 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002552 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002558 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002559 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002560 break;
2561 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002562 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002563 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002564 break;
2565 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002570 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
2572 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002573 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002574 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002578 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002579 break;
2580 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002582 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002586 break;
2587 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002588 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002589 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002592 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002593 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002596 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002597 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002600 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002601 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002611 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002614 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616
2617 case Instruction::ADD_INT:
2618 case Instruction::SUB_INT:
2619 case Instruction::MUL_INT:
2620 case Instruction::REM_INT:
2621 case Instruction::DIV_INT:
2622 case Instruction::SHL_INT:
2623 case Instruction::SHR_INT:
2624 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002626 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::AND_INT:
2629 case Instruction::OR_INT:
2630 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002631 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::ADD_LONG:
2635 case Instruction::SUB_LONG:
2636 case Instruction::MUL_LONG:
2637 case Instruction::DIV_LONG:
2638 case Instruction::REM_LONG:
2639 case Instruction::AND_LONG:
2640 case Instruction::OR_LONG:
2641 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002642 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002643 reg_types_.LongLo(), reg_types_.LongHi(),
2644 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002645 break;
2646 case Instruction::SHL_LONG:
2647 case Instruction::SHR_LONG:
2648 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002650 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002651 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::ADD_FLOAT:
2654 case Instruction::SUB_FLOAT:
2655 case Instruction::MUL_FLOAT:
2656 case Instruction::DIV_FLOAT:
2657 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2659 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::ADD_DOUBLE:
2662 case Instruction::SUB_DOUBLE:
2663 case Instruction::MUL_DOUBLE:
2664 case Instruction::DIV_DOUBLE:
2665 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002666 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002667 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2668 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::ADD_INT_2ADDR:
2671 case Instruction::SUB_INT_2ADDR:
2672 case Instruction::MUL_INT_2ADDR:
2673 case Instruction::REM_INT_2ADDR:
2674 case Instruction::SHL_INT_2ADDR:
2675 case Instruction::SHR_INT_2ADDR:
2676 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2678 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::AND_INT_2ADDR:
2681 case Instruction::OR_INT_2ADDR:
2682 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002683 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2684 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2688 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::ADD_LONG_2ADDR:
2691 case Instruction::SUB_LONG_2ADDR:
2692 case Instruction::MUL_LONG_2ADDR:
2693 case Instruction::DIV_LONG_2ADDR:
2694 case Instruction::REM_LONG_2ADDR:
2695 case Instruction::AND_LONG_2ADDR:
2696 case Instruction::OR_LONG_2ADDR:
2697 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002698 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002699 reg_types_.LongLo(), reg_types_.LongHi(),
2700 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::SHL_LONG_2ADDR:
2703 case Instruction::SHR_LONG_2ADDR:
2704 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002705 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002706 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
2708 case Instruction::ADD_FLOAT_2ADDR:
2709 case Instruction::SUB_FLOAT_2ADDR:
2710 case Instruction::MUL_FLOAT_2ADDR:
2711 case Instruction::DIV_FLOAT_2ADDR:
2712 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2714 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::ADD_DOUBLE_2ADDR:
2717 case Instruction::SUB_DOUBLE_2ADDR:
2718 case Instruction::MUL_DOUBLE_2ADDR:
2719 case Instruction::DIV_DOUBLE_2ADDR:
2720 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2723 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002724 break;
2725 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002726 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002727 case Instruction::MUL_INT_LIT16:
2728 case Instruction::DIV_INT_LIT16:
2729 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002730 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2731 true);
jeffhaobdb76512011-09-07 11:43:16 -07002732 break;
2733 case Instruction::AND_INT_LIT16:
2734 case Instruction::OR_INT_LIT16:
2735 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002736 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2737 true);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::ADD_INT_LIT8:
2740 case Instruction::RSUB_INT_LIT8:
2741 case Instruction::MUL_INT_LIT8:
2742 case Instruction::DIV_INT_LIT8:
2743 case Instruction::REM_INT_LIT8:
2744 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002745 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002746 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002747 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2748 false);
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::AND_INT_LIT8:
2751 case Instruction::OR_INT_LIT8:
2752 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002753 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2754 false);
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002757 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002758 case Instruction::RETURN_VOID_NO_BARRIER:
2759 if (IsConstructor() && !IsStatic()) {
2760 auto& declaring_class = GetDeclaringClass();
2761 auto* klass = declaring_class.GetClass();
2762 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2763 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2765 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002766 break;
2767 }
2768 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002769 }
2770 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002771 // Note: the following instructions encode offsets derived from class linking.
2772 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2773 // meaning if the class linking and resolution were successful.
2774 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002775 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002776 break;
2777 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002778 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002779 break;
2780 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002781 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002782 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002783 case Instruction::IGET_BOOLEAN_QUICK:
2784 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2785 break;
2786 case Instruction::IGET_BYTE_QUICK:
2787 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2788 break;
2789 case Instruction::IGET_CHAR_QUICK:
2790 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2791 break;
2792 case Instruction::IGET_SHORT_QUICK:
2793 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2794 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002795 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002797 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002798 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002800 break;
2801 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002803 break;
2804 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002806 break;
2807 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002808 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002809 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002810 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002811 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002812 break;
2813 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002814 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002815 break;
2816 case Instruction::INVOKE_VIRTUAL_QUICK:
2817 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2818 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002819 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002820 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002821 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002822 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002823 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002824 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002825 } else {
2826 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2827 }
2828 just_set_result = true;
2829 }
2830 break;
2831 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002832 case Instruction::INVOKE_LAMBDA: {
2833 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2834 // If the code would've normally hard-failed, then the interpreter will throw the
2835 // appropriate verification errors at runtime.
2836 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2837 break;
2838 }
2839 case Instruction::CREATE_LAMBDA: {
2840 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2841 // If the code would've normally hard-failed, then the interpreter will throw the
2842 // appropriate verification errors at runtime.
2843 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2844 break;
2845 }
2846
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002847 case Instruction::UNUSED_F4:
2848 case Instruction::UNUSED_F5:
2849 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002850 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002851 // Conservatively fail verification on release builds.
2852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2853 break;
2854 }
2855
2856 case Instruction::BOX_LAMBDA: {
2857 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2858 // If the code would've normally hard-failed, then the interpreter will throw the
2859 // appropriate verification errors at runtime.
2860 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
2861 break;
2862 }
2863
2864 case Instruction::UNBOX_LAMBDA: {
2865 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2866 // If the code would've normally hard-failed, then the interpreter will throw the
2867 // appropriate verification errors at runtime.
2868 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2869 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002870 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002871
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002873 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002874 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002875 case Instruction::UNUSED_79:
2876 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879
2880 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002881 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002882 * complain if an instruction is missing (which is desirable).
2883 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002884 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002885
Stephen Kyle7e541c92014-12-17 17:10:02 +00002886 /*
2887 * If we are in a constructor, and we had an UninitializedThis type
2888 * in a register somewhere, we need to make sure it wasn't overwritten.
2889 */
2890 if (track_uninitialized_this) {
2891 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2892 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2893 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2894 was_invoke_direct)) {
2895 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2896 << "Constructor failed to initialize this object";
2897 }
2898 }
2899
Ian Rogersad0b3a32012-04-16 14:50:24 -07002900 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002901 if (Runtime::Current()->IsAotCompiler()) {
2902 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002903 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2904 LOG(ERROR) << "Pending failures:";
2905 for (auto& error : failures_) {
2906 LOG(ERROR) << error;
2907 }
2908 for (auto& error_msg : failure_messages_) {
2909 LOG(ERROR) << error_msg->str();
2910 }
2911 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
2912 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002913 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 /* immediate failure, reject class */
2915 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2916 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002917 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002918 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002919 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002920 }
jeffhaobdb76512011-09-07 11:43:16 -07002921 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 * If we didn't just set the result register, clear it out. This ensures that you can only use
2923 * "move-result" immediately after the result is set. (We could check this statically, but it's
2924 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002925 */
2926 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002927 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002928 }
2929
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002930
jeffhaobdb76512011-09-07 11:43:16 -07002931
2932 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002933 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002934 *
2935 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002936 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002937 * somebody could get a reference field, check it for zero, and if the
2938 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002939 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002940 * that, and will reject the code.
2941 *
2942 * TODO: avoid re-fetching the branch target
2943 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002944 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002945 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002947 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002949 return false;
2950 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002951 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002952 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002953 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 }
jeffhaobdb76512011-09-07 11:43:16 -07002955 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002956 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002957 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002958 return false;
2959 }
2960 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002961 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002962 return false;
2963 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002964 }
jeffhaobdb76512011-09-07 11:43:16 -07002965 }
2966
2967 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002968 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002969 *
2970 * We've already verified that the table is structurally sound, so we
2971 * just need to walk through and tag the targets.
2972 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002973 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002974 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2975 const uint16_t* switch_insns = insns + offset_to_switch;
2976 int switch_count = switch_insns[1];
2977 int offset_to_targets, targ;
2978
2979 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2980 /* 0 = sig, 1 = count, 2/3 = first key */
2981 offset_to_targets = 4;
2982 } else {
2983 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002984 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002985 offset_to_targets = 2 + 2 * switch_count;
2986 }
2987
2988 /* verify each switch target */
2989 for (targ = 0; targ < switch_count; targ++) {
2990 int offset;
2991 uint32_t abs_offset;
2992
2993 /* offsets are 32-bit, and only partly endian-swapped */
2994 offset = switch_insns[offset_to_targets + targ * 2] |
2995 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 abs_offset = work_insn_idx_ + offset;
2997 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002998 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002999 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003001 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003002 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003003 }
jeffhaobdb76512011-09-07 11:43:16 -07003004 }
3005 }
3006
3007 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3009 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003010 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003011 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003012 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003013 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003014
Andreas Gampef91baf12014-07-18 15:41:00 -07003015 // Need the linker to try and resolve the handled class to check if it's Throwable.
3016 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3017
Ian Rogers0571d352011-11-03 19:51:38 -07003018 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003019 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3020 if (handler_type_idx == DexFile::kDexNoIndex16) {
3021 has_catch_all_handler = true;
3022 } else {
3023 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003024 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3025 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003026 if (klass != nullptr) {
3027 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3028 has_catch_all_handler = true;
3029 }
3030 } else {
3031 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 DCHECK(self_->IsExceptionPending());
3033 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003034 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 }
jeffhaobdb76512011-09-07 11:43:16 -07003036 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3038 * "work_regs", because at runtime the exception will be thrown before the instruction
3039 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003040 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003041 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003042 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 }
jeffhaobdb76512011-09-07 11:43:16 -07003044 }
3045
3046 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3048 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003049 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003050 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003051 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 * The state in work_line reflects the post-execution state. If the current instruction is a
3053 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003054 * it will do so before grabbing the lock).
3055 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003056 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003057 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003058 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003059 return false;
3060 }
3061 }
3062 }
3063
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003064 /* Handle "continue". Tag the next consecutive instruction.
3065 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3066 * because it changes work_line_ when performing peephole optimization
3067 * and this change should not be used in those cases.
3068 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003069 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003070 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3071 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003072 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3074 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003075 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003076 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3077 // next instruction isn't one.
3078 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3079 return false;
3080 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003081 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003082 // Make workline consistent with fallthrough computed from peephole optimization.
3083 work_line_->CopyFromLine(fallthrough_line.get());
3084 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003085 if (insn_flags_[next_insn_idx].IsReturn()) {
3086 // For returns we only care about the operand to the return, all other registers are dead.
3087 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3088 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003089 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003090 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003091 } else {
3092 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003093 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003094 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003095 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003096 }
3097 }
3098 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003099 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003101 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3102 // needed. If the merge changes the state of the registers then the work line will be
3103 // updated.
3104 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003105 return false;
3106 }
3107 } else {
3108 /*
3109 * We're not recording register data for the next instruction, so we don't know what the
3110 * prior state was. We have to assume that something has changed and re-evaluate it.
3111 */
3112 insn_flags_[next_insn_idx].SetChanged();
3113 }
3114 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003115
jeffhaod1f0fde2011-09-08 17:25:33 -07003116 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003117 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003118 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 return false;
3120 }
jeffhaobdb76512011-09-07 11:43:16 -07003121 }
3122
3123 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003124 * Update start_guess. Advance to the next instruction of that's
3125 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003126 * neither of those exists we're in a return or throw; leave start_guess
3127 * alone and let the caller sort it out.
3128 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3131 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003132 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003133 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003135 }
3136
Ian Rogersd81871c2011-10-03 13:57:23 -07003137 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3138 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003139
3140 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003141} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003142
Ian Rogersd8f69b02014-09-10 21:43:52 +00003143const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003144 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003145 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003146 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003148 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3149 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003150 if (result.IsConflict()) {
3151 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3152 << "' in " << referrer;
3153 return result;
3154 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003155 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003156 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003157 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003158 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003159 // check at runtime if access is allowed and so pass here. If result is
3160 // primitive, skip the access check.
3161 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3162 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003163 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003164 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003165 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003166 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003167}
3168
Ian Rogersd8f69b02014-09-10 21:43:52 +00003169const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003170 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003172 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003173 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3174 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003175 CatchHandlerIterator iterator(handlers_ptr);
3176 for (; iterator.HasNext(); iterator.Next()) {
3177 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3178 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003179 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003181 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003182 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003183 if (exception.IsUnresolvedTypes()) {
3184 // We don't know enough about the type. Fail here and let runtime handle it.
3185 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3186 return exception;
3187 } else {
3188 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3189 return reg_types_.Conflict();
3190 }
Jeff Haob878f212014-04-24 16:25:36 -07003191 } else if (common_super == nullptr) {
3192 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003193 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003194 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003196 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003197 if (FailOrAbort(this,
3198 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3199 "java.lang.Throwable is not assignable-from common_super at ",
3200 work_insn_idx_)) {
3201 break;
3202 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 }
3204 }
3205 }
3206 }
Ian Rogers0571d352011-11-03 19:51:38 -07003207 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003208 }
3209 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003212 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003213 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003215 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003216}
3217
Mathieu Chartiere401d142015-04-22 13:56:20 -07003218ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3219 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003220 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003221 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003222 if (klass_type.IsConflict()) {
3223 std::string append(" in attempt to access method ");
3224 append += dex_file_->GetMethodName(method_id);
3225 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003227 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003228 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003230 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003231 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003232 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003233 auto* cl = Runtime::Current()->GetClassLinker();
3234 auto pointer_size = cl->GetImagePointerSize();
3235 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003237 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003238 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003239
3240 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003241 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003242 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003243 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003244 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003245 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003246 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003248 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003250 // If a virtual or interface method wasn't found with the expected type, look in
3251 // the direct methods. This can happen when the wrong invoke type is used or when
3252 // a class has changed, and will be flagged as an error in later checks.
3253 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003254 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003255 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003257 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3258 << PrettyDescriptor(klass) << "." << name
3259 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003260 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003261 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 }
3263 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003264 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3265 // enforce them here.
3266 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3268 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003269 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003271 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003272 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003273 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3274 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003275 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003276 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003277 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003278 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003279 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003280 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003281 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003282 }
jeffhaode0d9c92012-02-27 13:58:13 -08003283 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3284 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3286 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003287 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003288 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003289 // Check that interface methods match interface classes.
3290 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3291 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3292 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003293 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003294 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3295 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3296 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003297 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003298 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003299 // See if the method type implied by the invoke instruction matches the access flags for the
3300 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003301 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3303 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3304 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003305 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3306 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003307 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003309 return res_method;
3310}
3311
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003312template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003313ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3314 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003315 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3316 // match the call to the signature. Also, we might be calling through an abstract method
3317 // definition (which doesn't have register count values).
3318 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3319 /* caught by static verifier */
3320 DCHECK(is_range || expected_args <= 5);
3321 if (expected_args > code_item_->outs_size_) {
3322 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3323 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3324 return nullptr;
3325 }
3326
3327 uint32_t arg[5];
3328 if (!is_range) {
3329 inst->GetVarArgs(arg);
3330 }
3331 uint32_t sig_registers = 0;
3332
3333 /*
3334 * Check the "this" argument, which must be an instance of the class that declared the method.
3335 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3336 * rigorous check here (which is okay since we have to do it at runtime).
3337 */
3338 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003339 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003340 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3341 CHECK(have_pending_hard_failure_);
3342 return nullptr;
3343 }
3344 if (actual_arg_type.IsUninitializedReference()) {
3345 if (res_method) {
3346 if (!res_method->IsConstructor()) {
3347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3348 return nullptr;
3349 }
3350 } else {
3351 // Check whether the name of the called method is "<init>"
3352 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003353 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3355 return nullptr;
3356 }
3357 }
3358 }
3359 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003360 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003361 // Miranda methods have the declaring interface as their declaring class, not the abstract
3362 // class. It would be wrong to use this for the type check (interface type checks are
3363 // postponed to runtime).
3364 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003365 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003366 std::string temp;
3367 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003368 klass->CannotBeAssignedFromOtherTypes());
3369 } else {
3370 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3371 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003372 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003373 dex_file_->StringByTypeIdx(class_idx),
3374 false);
3375 }
3376 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3377 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3378 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3379 << "' not instance of '" << *res_method_class << "'";
3380 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3381 // the compiler.
3382 if (have_pending_hard_failure_) {
3383 return nullptr;
3384 }
3385 }
3386 }
3387 sig_registers = 1;
3388 }
3389
3390 for ( ; it->HasNext(); it->Next()) {
3391 if (sig_registers >= expected_args) {
3392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3393 " arguments, found " << sig_registers << " or more.";
3394 return nullptr;
3395 }
3396
3397 const char* param_descriptor = it->GetDescriptor();
3398
3399 if (param_descriptor == nullptr) {
3400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3401 "component";
3402 return nullptr;
3403 }
3404
Ian Rogersd8f69b02014-09-10 21:43:52 +00003405 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003406 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3407 arg[sig_registers];
3408 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003409 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003410 if (!src_type.IsIntegralTypes()) {
3411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3412 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003413 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003414 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003415 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003416 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3417 // compiler.
3418 if (have_pending_hard_failure_) {
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003419 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003420 }
3421 }
3422 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3423 }
3424 if (expected_args != sig_registers) {
3425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3426 " arguments, found " << sig_registers;
3427 return nullptr;
3428 }
3429 return res_method;
3430}
3431
3432void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3433 MethodType method_type,
3434 bool is_range) {
3435 // As the method may not have been resolved, make this static check against what we expect.
3436 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3437 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3438 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3439 DexFileParameterIterator it(*dex_file_,
3440 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3441 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3442 nullptr);
3443}
3444
3445class MethodParamListDescriptorIterator {
3446 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003447 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003448 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3449 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3450 }
3451
3452 bool HasNext() {
3453 return pos_ < params_size_;
3454 }
3455
3456 void Next() {
3457 ++pos_;
3458 }
3459
3460 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3461 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3462 }
3463
3464 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003465 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003466 size_t pos_;
3467 const DexFile::TypeList* params_;
3468 const size_t params_size_;
3469};
3470
Mathieu Chartiere401d142015-04-22 13:56:20 -07003471ArtMethod* MethodVerifier::VerifyInvocationArgs(
3472 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003473 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3474 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003475 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003476
Mathieu Chartiere401d142015-04-22 13:56:20 -07003477 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003478 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003479 // Check what we can statically.
3480 if (!have_pending_hard_failure_) {
3481 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3482 }
3483 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003484 }
3485
Ian Rogersd81871c2011-10-03 13:57:23 -07003486 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3487 // has a vtable entry for the target method.
3488 if (is_super) {
3489 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003490 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003491 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003492 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003493 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003494 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003495 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003496 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003497 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003498 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003499 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003500 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003501 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003502 << "." << res_method->GetName()
3503 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003504 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 }
3506 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003507
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003508 // Process the target method's signature. This signature may or may not
3509 MethodParamListDescriptorIterator it(res_method);
3510 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3511 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003512}
3513
Mathieu Chartiere401d142015-04-22 13:56:20 -07003514ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3515 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003516 if (is_range) {
3517 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3518 } else {
3519 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3520 }
3521 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003522 if (!actual_arg_type.HasClass()) {
3523 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003524 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003525 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003526 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003527 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003528 if (klass->IsInterface()) {
3529 // Derive Object.class from Class.class.getSuperclass().
3530 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003531 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003532 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003533 return nullptr;
3534 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003535 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003536 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003537 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003538 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003539 if (!dispatch_class->HasVTable()) {
3540 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3541 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003542 return nullptr;
3543 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003544 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003545 auto* cl = Runtime::Current()->GetClassLinker();
3546 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003547 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3548 FailOrAbort(this, allow_failure,
3549 "Receiver class has not enough vtable slots for quickened invoke at ",
3550 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003551 return nullptr;
3552 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003553 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003554 if (self_->IsExceptionPending()) {
3555 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3556 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003557 return nullptr;
3558 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003559 return res_method;
3560}
3561
Mathieu Chartiere401d142015-04-22 13:56:20 -07003562ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003563 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3564 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3565
Mathieu Chartiere401d142015-04-22 13:56:20 -07003566 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003567 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003569 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003570 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003571 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3572 work_insn_idx_)) {
3573 return nullptr;
3574 }
3575 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3576 work_insn_idx_)) {
3577 return nullptr;
3578 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003579
3580 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3581 // match the call to the signature. Also, we might be calling through an abstract method
3582 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003583 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003584 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003585 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003586 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003587 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3588 /* caught by static verifier */
3589 DCHECK(is_range || expected_args <= 5);
3590 if (expected_args > code_item_->outs_size_) {
3591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3592 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003593 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003594 }
3595
3596 /*
3597 * Check the "this" argument, which must be an instance of the class that declared the method.
3598 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3599 * rigorous check here (which is okay since we have to do it at runtime).
3600 */
3601 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003603 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003604 }
3605 if (!actual_arg_type.IsZero()) {
3606 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003607 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003608 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003609 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003610 klass->CannotBeAssignedFromOtherTypes());
3611 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003612 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3613 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003614 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003615 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003616 }
3617 }
3618 /*
3619 * Process the target method's signature. This signature may or may not
3620 * have been verified, so we can't assume it's properly formed.
3621 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003622 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003623 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003624 uint32_t arg[5];
3625 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003626 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003627 }
3628 size_t actual_args = 1;
3629 for (size_t param_index = 0; param_index < params_size; param_index++) {
3630 if (actual_args >= expected_args) {
3631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003632 << "'. Expected " << expected_args
3633 << " arguments, processing argument " << actual_args
3634 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003635 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003636 }
3637 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003638 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003639 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003641 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003643 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003644 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003645 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003646 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003647 return res_method;
3648 }
3649 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3650 }
3651 if (actual_args != expected_args) {
3652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3653 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003654 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003655 } else {
3656 return res_method;
3657 }
3658}
3659
Ian Rogers62342ec2013-06-11 10:26:37 -07003660void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003661 uint32_t type_idx;
3662 if (!is_filled) {
3663 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3664 type_idx = inst->VRegC_22c();
3665 } else if (!is_range) {
3666 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3667 type_idx = inst->VRegB_35c();
3668 } else {
3669 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3670 type_idx = inst->VRegB_3rc();
3671 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003672 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003673 if (res_type.IsConflict()) { // bad class
3674 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003675 } else {
3676 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3677 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003679 } else if (!is_filled) {
3680 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003681 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003682 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003683 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003684 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003685 } else {
3686 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3687 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003688 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003689 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3690 uint32_t arg[5];
3691 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003692 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003693 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003694 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003695 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003696 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3697 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003698 return;
3699 }
3700 }
3701 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003702 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003703 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003704 }
3705 }
3706}
3707
Sebastien Hertz5243e912013-05-21 10:55:07 +02003708void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003709 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003711 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003713 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003715 if (array_type.IsZero()) {
3716 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3717 // instruction type. TODO: have a proper notion of bottom here.
3718 if (!is_primitive || insn_type.IsCategory1Types()) {
3719 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003720 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003721 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003722 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003723 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3724 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003725 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003726 }
jeffhaofc3144e2012-02-01 17:21:15 -08003727 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003729 } else {
3730 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003731 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003732 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003734 << " source for aget-object";
3735 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003737 << " source for category 1 aget";
3738 } else if (is_primitive && !insn_type.Equals(component_type) &&
3739 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003740 (insn_type.IsLong() && component_type.IsDouble()))) {
3741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3742 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003743 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003744 // Use knowledge of the field type which is stronger than the type inferred from the
3745 // instruction, which can't differentiate object types and ints from floats, longs from
3746 // doubles.
3747 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003748 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003749 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003750 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003751 component_type.HighHalf(&reg_types_));
3752 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003753 }
3754 }
3755 }
3756}
3757
Ian Rogersd8f69b02014-09-10 21:43:52 +00003758void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003759 const uint32_t vregA) {
3760 // Primitive assignability rules are weaker than regular assignability rules.
3761 bool instruction_compatible;
3762 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003763 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003764 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003765 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003766 value_compatible = value_type.IsIntegralTypes();
3767 } else if (target_type.IsFloat()) {
3768 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3769 value_compatible = value_type.IsFloatTypes();
3770 } else if (target_type.IsLong()) {
3771 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003772 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3773 // as target_type depends on the resolved type of the field.
3774 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003776 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3777 } else {
3778 value_compatible = false;
3779 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003780 } else if (target_type.IsDouble()) {
3781 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003782 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3783 // as target_type depends on the resolved type of the field.
3784 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003785 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003786 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3787 } else {
3788 value_compatible = false;
3789 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003790 } else {
3791 instruction_compatible = false; // reference with primitive store
3792 value_compatible = false; // unused
3793 }
3794 if (!instruction_compatible) {
3795 // This is a global failure rather than a class change failure as the instructions and
3796 // the descriptors for the type should have been consistent within the same file at
3797 // compile time.
3798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3799 << "' but expected type '" << target_type << "'";
3800 return;
3801 }
3802 if (!value_compatible) {
3803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3804 << " of type " << value_type << " but expected " << target_type << " for put";
3805 return;
3806 }
3807}
3808
Sebastien Hertz5243e912013-05-21 10:55:07 +02003809void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003810 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003812 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003816 if (array_type.IsZero()) {
3817 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3818 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003819 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003821 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003822 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003823 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003824 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003825 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003827 if (!component_type.IsReferenceTypes()) {
3828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3829 << " source for aput-object";
3830 } else {
3831 // The instruction agrees with the type of array, confirm the value to be stored does too
3832 // Note: we use the instruction type (rather than the component type) for aput-object as
3833 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003834 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003835 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 }
3837 }
3838 }
3839}
3840
Mathieu Chartierc7853442015-03-27 14:35:38 -07003841ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003842 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3843 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003844 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003845 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003846 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3847 field_idx, dex_file_->GetFieldName(field_id),
3848 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003849 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003850 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003851 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003853 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003854 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003855 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3856 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003857 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003858 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003859 << dex_file_->GetFieldName(field_id) << ") in "
3860 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003861 DCHECK(self_->IsExceptionPending());
3862 self_->ClearException();
3863 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003864 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3865 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003866 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003867 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003868 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 } else if (!field->IsStatic()) {
3870 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003871 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003873 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003874}
3875
Mathieu Chartierc7853442015-03-27 14:35:38 -07003876ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003877 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3878 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003879 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003880 if (klass_type.IsConflict()) {
3881 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3882 field_idx, dex_file_->GetFieldName(field_id),
3883 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003884 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003885 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003886 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003888 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003889 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003890 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3891 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003892 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003893 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003894 << dex_file_->GetFieldName(field_id) << ") in "
3895 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003896 DCHECK(self_->IsExceptionPending());
3897 self_->ClearException();
3898 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003899 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3900 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003901 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003902 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003903 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003904 } else if (field->IsStatic()) {
3905 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3906 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003907 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003908 } else if (obj_type.IsZero()) {
3909 // Cannot infer and check type, however, access will cause null pointer exception
3910 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003911 } else if (!obj_type.IsReferenceTypes()) {
3912 // Trying to read a field from something that isn't a reference
3913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3914 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003915 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003916 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003917 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003918 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003919 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003920 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003921 if (obj_type.IsUninitializedTypes() &&
3922 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3923 !field_klass.Equals(GetDeclaringClass()))) {
3924 // Field accesses through uninitialized references are only allowable for constructors where
3925 // the field is declared in this class
3926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003927 << " of a not fully initialized object within the context"
3928 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003929 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003930 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3931 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3932 // of C1. For resolution to occur the declared class of the field must be compatible with
3933 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3934 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3935 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003936 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003937 } else {
3938 return field;
3939 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003940 }
3941}
3942
Andreas Gampe896df402014-10-20 22:25:29 -07003943template <MethodVerifier::FieldAccessType kAccType>
3944void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3945 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003946 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003947 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003948 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003949 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003950 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003951 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003952 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003953 if (UNLIKELY(have_pending_hard_failure_)) {
3954 return;
3955 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003956 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003957 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003958 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003959 if (kAccType == FieldAccessType::kAccPut) {
3960 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3961 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3962 << " from other class " << GetDeclaringClass();
3963 return;
3964 }
3965 }
3966
Mathieu Chartierc7853442015-03-27 14:35:38 -07003967 mirror::Class* field_type_class =
3968 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003969 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003970 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003971 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003972 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003973 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3974 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003975 }
Ian Rogers0d604842012-04-16 14:50:24 -07003976 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003977 if (field_type == nullptr) {
3978 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3979 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003980 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003981 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003982 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003983 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003984 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3985 "Unexpected third access type");
3986 if (kAccType == FieldAccessType::kAccPut) {
3987 // sput or iput.
3988 if (is_primitive) {
3989 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003990 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003991 if (!insn_type.IsAssignableFrom(*field_type)) {
3992 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3993 << " to be compatible with type '" << insn_type
3994 << "' but found type '" << *field_type
3995 << "' in put-object";
3996 return;
3997 }
3998 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003999 }
Andreas Gampe896df402014-10-20 22:25:29 -07004000 } else if (kAccType == FieldAccessType::kAccGet) {
4001 // sget or iget.
4002 if (is_primitive) {
4003 if (field_type->Equals(insn_type) ||
4004 (field_type->IsFloat() && insn_type.IsInteger()) ||
4005 (field_type->IsDouble() && insn_type.IsLong())) {
4006 // expected that read is of the correct primitive type or that int reads are reading
4007 // floats or long reads are reading doubles
4008 } else {
4009 // This is a global failure rather than a class change failure as the instructions and
4010 // the descriptors for the type should have been consistent within the same file at
4011 // compile time
4012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4013 << " to be of type '" << insn_type
4014 << "' but found type '" << *field_type << "' in get";
4015 return;
4016 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004017 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004018 if (!insn_type.IsAssignableFrom(*field_type)) {
4019 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4020 << " to be compatible with type '" << insn_type
4021 << "' but found type '" << *field_type
4022 << "' in get-object";
4023 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4024 return;
4025 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004026 }
Andreas Gampe896df402014-10-20 22:25:29 -07004027 if (!field_type->IsLowHalf()) {
4028 work_line_->SetRegisterType(this, vregA, *field_type);
4029 } else {
4030 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4031 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004032 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004033 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004034 }
4035}
4036
Mathieu Chartierc7853442015-03-27 14:35:38 -07004037ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004038 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004039 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004040 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004041 if (!object_type.HasClass()) {
4042 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4043 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004044 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004045 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004046 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004047 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004048 if (f == nullptr) {
4049 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4050 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4051 }
4052 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004053}
4054
Andreas Gampe896df402014-10-20 22:25:29 -07004055template <MethodVerifier::FieldAccessType kAccType>
4056void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4057 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004058 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004059
Mathieu Chartierc7853442015-03-27 14:35:38 -07004060 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004061 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4063 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004064 }
Andreas Gampe896df402014-10-20 22:25:29 -07004065
4066 // For an IPUT_QUICK, we now test for final flag of the field.
4067 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004068 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4069 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004070 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004071 return;
4072 }
4073 }
Andreas Gampe896df402014-10-20 22:25:29 -07004074
4075 // Get the field type.
4076 const RegType* field_type;
4077 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004078 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4079 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004080
4081 if (field_type_class != nullptr) {
4082 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4083 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004084 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004085 Thread* self = Thread::Current();
4086 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4087 self->ClearException();
4088 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4089 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004090 }
Andreas Gampe896df402014-10-20 22:25:29 -07004091 if (field_type == nullptr) {
4092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004093 return;
4094 }
Andreas Gampe896df402014-10-20 22:25:29 -07004095 }
4096
4097 const uint32_t vregA = inst->VRegA_22c();
4098 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4099 "Unexpected third access type");
4100 if (kAccType == FieldAccessType::kAccPut) {
4101 if (is_primitive) {
4102 // Primitive field assignability rules are weaker than regular assignability rules
4103 bool instruction_compatible;
4104 bool value_compatible;
4105 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4106 if (field_type->IsIntegralTypes()) {
4107 instruction_compatible = insn_type.IsIntegralTypes();
4108 value_compatible = value_type.IsIntegralTypes();
4109 } else if (field_type->IsFloat()) {
4110 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4111 value_compatible = value_type.IsFloatTypes();
4112 } else if (field_type->IsLong()) {
4113 instruction_compatible = insn_type.IsLong();
4114 value_compatible = value_type.IsLongTypes();
4115 } else if (field_type->IsDouble()) {
4116 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4117 value_compatible = value_type.IsDoubleTypes();
4118 } else {
4119 instruction_compatible = false; // reference field with primitive store
4120 value_compatible = false; // unused
4121 }
4122 if (!instruction_compatible) {
4123 // This is a global failure rather than a class change failure as the instructions and
4124 // the descriptors for the type should have been consistent within the same file at
4125 // compile time
4126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4127 << " to be of type '" << insn_type
4128 << "' but found type '" << *field_type
4129 << "' in put";
4130 return;
4131 }
4132 if (!value_compatible) {
4133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4134 << " of type " << value_type
4135 << " but expected " << *field_type
4136 << " for store to " << PrettyField(field) << " in put";
4137 return;
4138 }
4139 } else {
4140 if (!insn_type.IsAssignableFrom(*field_type)) {
4141 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4142 << " to be compatible with type '" << insn_type
4143 << "' but found type '" << *field_type
4144 << "' in put-object";
4145 return;
4146 }
4147 work_line_->VerifyRegisterType(this, vregA, *field_type);
4148 }
4149 } else if (kAccType == FieldAccessType::kAccGet) {
4150 if (is_primitive) {
4151 if (field_type->Equals(insn_type) ||
4152 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4153 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4154 // expected that read is of the correct primitive type or that int reads are reading
4155 // floats or long reads are reading doubles
4156 } else {
4157 // This is a global failure rather than a class change failure as the instructions and
4158 // the descriptors for the type should have been consistent within the same file at
4159 // compile time
4160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4161 << " to be of type '" << insn_type
4162 << "' but found type '" << *field_type << "' in Get";
4163 return;
4164 }
4165 } else {
4166 if (!insn_type.IsAssignableFrom(*field_type)) {
4167 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4168 << " to be compatible with type '" << insn_type
4169 << "' but found type '" << *field_type
4170 << "' in get-object";
4171 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4172 return;
4173 }
4174 }
4175 if (!field_type->IsLowHalf()) {
4176 work_line_->SetRegisterType(this, vregA, *field_type);
4177 } else {
4178 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004179 }
4180 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004181 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004182 }
4183}
4184
Ian Rogers776ac1f2012-04-13 23:36:36 -07004185bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004186 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004188 return false;
4189 }
4190 return true;
4191}
4192
Stephen Kyle9bc61992014-09-22 13:53:15 +01004193bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4194 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4195 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4197 return false;
4198 }
4199 return true;
4200}
4201
4202bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4203 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4204}
4205
Ian Rogersebbdd872014-07-07 23:53:08 -07004206bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4207 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004208 bool changed = true;
4209 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4210 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004211 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004212 * We haven't processed this instruction before, and we haven't touched the registers here, so
4213 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4214 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004215 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004216 if (!insn_flags_[next_insn].IsReturn()) {
4217 target_line->CopyFromLine(merge_line);
4218 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004219 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004220 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004221 return false;
4222 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004223 // For returns we only care about the operand to the return, all other registers are dead.
4224 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4225 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4226 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004227 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004228 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004229 } else {
4230 target_line->CopyFromLine(merge_line);
4231 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004232 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004233 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004235 }
4236 }
4237 }
jeffhaobdb76512011-09-07 11:43:16 -07004238 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004239 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004240 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004241 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004242 if (gDebugVerify) {
4243 copy->CopyFromLine(target_line);
4244 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004246 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004247 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004248 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004249 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004250 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004251 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004252 << copy->Dump(this) << " MERGE\n"
4253 << merge_line->Dump(this) << " ==\n"
4254 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004255 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004256 if (update_merge_line && changed) {
4257 merge_line->CopyFromLine(target_line);
4258 }
jeffhaobdb76512011-09-07 11:43:16 -07004259 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004260 if (changed) {
4261 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004262 }
4263 return true;
4264}
4265
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004266InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004267 return &insn_flags_[work_insn_idx_];
4268}
4269
Ian Rogersd8f69b02014-09-10 21:43:52 +00004270const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004271 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004272 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004273 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004274 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004275 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4276 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004277 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004278 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004279 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4280 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004281 }
4282 }
4283 if (return_type_ == nullptr) {
4284 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4285 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4286 uint16_t return_type_idx = proto_id.return_type_idx_;
4287 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004288 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004289 }
4290 }
4291 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004292}
4293
Ian Rogersd8f69b02014-09-10 21:43:52 +00004294const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004295 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004296 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004297 const char* descriptor
4298 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004299 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004300 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004301 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4302 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004303 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004304 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004305 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004306 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004307 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004308}
4309
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004310std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4311 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004312 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004313 std::vector<int32_t> result;
4314 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004315 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004316 if (type.IsConstant()) {
4317 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004318 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4319 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004320 } else if (type.IsConstantLo()) {
4321 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004322 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4323 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004324 } else if (type.IsConstantHi()) {
4325 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004326 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4327 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004328 } else if (type.IsIntegralTypes()) {
4329 result.push_back(kIntVReg);
4330 result.push_back(0);
4331 } else if (type.IsFloat()) {
4332 result.push_back(kFloatVReg);
4333 result.push_back(0);
4334 } else if (type.IsLong()) {
4335 result.push_back(kLongLoVReg);
4336 result.push_back(0);
4337 result.push_back(kLongHiVReg);
4338 result.push_back(0);
4339 ++i;
4340 } else if (type.IsDouble()) {
4341 result.push_back(kDoubleLoVReg);
4342 result.push_back(0);
4343 result.push_back(kDoubleHiVReg);
4344 result.push_back(0);
4345 ++i;
4346 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4347 result.push_back(kUndefined);
4348 result.push_back(0);
4349 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004350 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004351 result.push_back(kReferenceVReg);
4352 result.push_back(0);
4353 }
4354 }
4355 return result;
4356}
4357
Ian Rogersd8f69b02014-09-10 21:43:52 +00004358const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004359 if (precise) {
4360 // Precise constant type.
4361 return reg_types_.FromCat1Const(value, true);
4362 } else {
4363 // Imprecise constant type.
4364 if (value < -32768) {
4365 return reg_types_.IntConstant();
4366 } else if (value < -128) {
4367 return reg_types_.ShortConstant();
4368 } else if (value < 0) {
4369 return reg_types_.ByteConstant();
4370 } else if (value == 0) {
4371 return reg_types_.Zero();
4372 } else if (value == 1) {
4373 return reg_types_.One();
4374 } else if (value < 128) {
4375 return reg_types_.PosByteConstant();
4376 } else if (value < 32768) {
4377 return reg_types_.PosShortConstant();
4378 } else if (value < 65536) {
4379 return reg_types_.CharConstant();
4380 } else {
4381 return reg_types_.IntConstant();
4382 }
4383 }
4384}
4385
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004386void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004387 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004388}
4389
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004390void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004391 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004392}
jeffhaod1224c72012-02-29 13:43:08 -08004393
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004394void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4395 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004396}
4397
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004398void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4399 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004400}
4401
Ian Rogersd81871c2011-10-03 13:57:23 -07004402} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004403} // namespace art