blob: 9faaa4a57effa32f393dbf54b75593ac959e25e2 [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:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800575 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700576 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
577 // class change and instantiation errors into soft verification errors so that we re-verify
578 // at runtime. We may fail to find or to agree on access because of not yet available class
579 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
580 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
581 // paths" that dynamically perform the verification and cause the behavior to be that akin
582 // to an interpreter.
583 error = VERIFY_ERROR_BAD_CLASS_SOFT;
584 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700585 // If we fail again at runtime, mark that this instruction would throw and force this
586 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700587 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700588
589 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
590 // try to merge garbage.
591 // Note: this assumes that Fail is called before we do any work_line modifications.
592 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
593 const Instruction* inst = Instruction::At(insns);
594 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
595
596 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
597 saved_line_->CopyFromLine(work_line_.get());
598 }
jeffhaofaf459e2012-08-31 15:32:47 -0700599 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700600 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700601 // Indication that verification should be retried at runtime.
602 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700603 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700604 have_pending_hard_failure_ = true;
605 }
606 break;
jeffhaod5347e02012-03-22 17:25:05 -0700607 // Hard verification failures at compile time will still fail at runtime, so the class is
608 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700609 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800610 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700611 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000612 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800613 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700614 have_pending_hard_failure_ = true;
615 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800616 }
617 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700618 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700619 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700620 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700621 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700622 failure_messages_.push_back(failure_message);
623 return *failure_message;
624}
625
Ian Rogers576ca0c2014-06-06 15:58:22 -0700626std::ostream& MethodVerifier::LogVerifyInfo() {
627 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
628 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
629}
630
Ian Rogersad0b3a32012-04-16 14:50:24 -0700631void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
632 size_t failure_num = failure_messages_.size();
633 DCHECK_NE(failure_num, 0U);
634 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
635 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700636 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700637 delete last_fail_message;
638}
639
640void MethodVerifier::AppendToLastFailMessage(std::string append) {
641 size_t failure_num = failure_messages_.size();
642 DCHECK_NE(failure_num, 0U);
643 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
644 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800645}
646
Ian Rogers776ac1f2012-04-13 23:36:36 -0700647bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 const uint16_t* insns = code_item_->insns_;
649 size_t insns_size = code_item_->insns_size_in_code_units_;
650 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700651 size_t new_instance_count = 0;
652 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700654
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700656 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700657 switch (opcode) {
658 case Instruction::APUT_OBJECT:
659 case Instruction::CHECK_CAST:
660 has_check_casts_ = true;
661 break;
662 case Instruction::INVOKE_VIRTUAL:
663 case Instruction::INVOKE_VIRTUAL_RANGE:
664 case Instruction::INVOKE_INTERFACE:
665 case Instruction::INVOKE_INTERFACE_RANGE:
666 has_virtual_or_interface_invokes_ = true;
667 break;
668 case Instruction::MONITOR_ENTER:
669 monitor_enter_count++;
670 break;
671 case Instruction::NEW_INSTANCE:
672 new_instance_count++;
673 break;
674 default:
675 break;
jeffhaobdb76512011-09-07 11:43:16 -0700676 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700678 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700680 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700681 }
682
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
685 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700686 return false;
687 }
688
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 new_instance_count_ = new_instance_count;
690 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700691 return true;
692}
693
Ian Rogers776ac1f2012-04-13 23:36:36 -0700694bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700696 if (tries_size == 0) {
697 return true;
698 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700700 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700701
702 for (uint32_t idx = 0; idx < tries_size; idx++) {
703 const DexFile::TryItem* try_item = &tries[idx];
704 uint32_t start = try_item->start_addr_;
705 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700706 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
708 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700709 return false;
710 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700712 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
713 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700714 return false;
715 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700716 uint32_t dex_pc = start;
717 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
718 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700720 size_t insn_size = inst->SizeInCodeUnits();
721 dex_pc += insn_size;
722 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700723 }
724 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800725 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700726 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700727 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700728 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700729 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700730 CatchHandlerIterator iterator(handlers_ptr);
731 for (; iterator.HasNext(); iterator.Next()) {
732 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
735 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700736 return false;
737 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100738 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
739 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
740 << "exception handler begins with move-result* (" << dex_pc << ")";
741 return false;
742 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700744 // Ensure exception types are resolved so that they don't need resolution to be delivered,
745 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700746 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800747 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
748 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700749 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700750 if (exception_type == nullptr) {
751 DCHECK(self_->IsExceptionPending());
752 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700753 }
754 }
jeffhaobdb76512011-09-07 11:43:16 -0700755 }
Ian Rogers0571d352011-11-03 19:51:38 -0700756 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700757 }
jeffhaobdb76512011-09-07 11:43:16 -0700758 return true;
759}
760
Ian Rogers776ac1f2012-04-13 23:36:36 -0700761bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700763
Ian Rogers0c7abda2012-09-19 13:33:42 -0700764 /* 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 -0700765 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700766 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700767
768 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700769 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700771 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700775 // All invoke points are marked as "Throw" points already.
776 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000777 if (inst->IsBranch()) {
778 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
779 // The compiler also needs safepoints for fall-through to loop heads.
780 // Such a loop head must be a target of a branch.
781 int32_t offset = 0;
782 bool cond, self_ok;
783 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
784 DCHECK(target_ok);
785 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
786 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700787 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000788 } else if (inst->IsReturn()) {
789 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 }
791 dex_pc += inst->SizeInCodeUnits();
792 inst = inst->Next();
793 }
794 return true;
795}
796
Ian Rogers776ac1f2012-04-13 23:36:36 -0700797bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 bool result = true;
799 switch (inst->GetVerifyTypeArgumentA()) {
800 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700801 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 break;
803 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700804 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 break;
806 }
807 switch (inst->GetVerifyTypeArgumentB()) {
808 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700809 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 break;
811 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700812 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 break;
814 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700815 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 break;
817 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700818 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 break;
820 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700821 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 break;
823 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700824 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 break;
826 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700827 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 break;
829 }
830 switch (inst->GetVerifyTypeArgumentC()) {
831 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700832 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 break;
834 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700835 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 break;
837 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700838 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 break;
840 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700841 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 break;
843 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700844 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 break;
846 }
847 switch (inst->GetVerifyExtraFlags()) {
848 case Instruction::kVerifyArrayData:
849 result = result && CheckArrayData(code_offset);
850 break;
851 case Instruction::kVerifyBranchTarget:
852 result = result && CheckBranchTarget(code_offset);
853 break;
854 case Instruction::kVerifySwitchTargets:
855 result = result && CheckSwitchTargets(code_offset);
856 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700857 case Instruction::kVerifyVarArgNonZero:
858 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700859 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700860 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
862 "non-range invoke";
863 return false;
864 }
Ian Rogers29a26482014-05-02 15:27:29 -0700865 uint32_t args[Instruction::kMaxVarArgRegs];
866 inst->GetVarArgs(args);
867 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700869 }
Andreas Gampec3314312014-06-19 18:13:29 -0700870 case Instruction::kVerifyVarArgRangeNonZero:
871 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700873 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
874 inst->VRegA() <= 0) {
875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
876 "range invoke";
877 return false;
878 }
Ian Rogers29a26482014-05-02 15:27:29 -0700879 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 break;
881 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 result = false;
884 break;
885 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800886 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
888 result = false;
889 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 return result;
891}
892
Ian Rogers7b078e82014-09-10 14:44:24 -0700893inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
896 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 return false;
898 }
899 return true;
900}
901
Ian Rogers7b078e82014-09-10 14:44:24 -0700902inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
905 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 return false;
907 }
908 return true;
909}
910
Ian Rogers7b078e82014-09-10 14:44:24 -0700911inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
914 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 return false;
916 }
917 return true;
918}
919
Ian Rogers7b078e82014-09-10 14:44:24 -0700920inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
923 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 return false;
925 }
926 return true;
927}
928
Ian Rogers7b078e82014-09-10 14:44:24 -0700929inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
932 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 return false;
934 }
935 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700936 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700938 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 return true;
942}
943
Ian Rogers7b078e82014-09-10 14:44:24 -0700944inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
947 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 return false;
949 }
950 return true;
951}
952
Ian Rogers7b078e82014-09-10 14:44:24 -0700953inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
956 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 return false;
958 }
959 return true;
960}
961
Ian Rogers776ac1f2012-04-13 23:36:36 -0700962bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
965 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 return false;
967 }
968 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700969 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 const char* cp = descriptor;
971 while (*cp++ == '[') {
972 bracket_count++;
973 }
974 if (bracket_count == 0) {
975 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
977 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 return false;
979 } else if (bracket_count > 255) {
980 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
982 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 return false;
984 }
985 return true;
986}
987
Ian Rogers776ac1f2012-04-13 23:36:36 -0700988bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
990 const uint16_t* insns = code_item_->insns_ + cur_offset;
991 const uint16_t* array_data;
992 int32_t array_data_offset;
993
994 DCHECK_LT(cur_offset, insn_count);
995 /* make sure the start of the array data table is in range */
996 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
997 if ((int32_t) cur_offset + array_data_offset < 0 ||
998 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001000 << ", data offset " << array_data_offset
1001 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 return false;
1003 }
1004 /* offset to array data table is a relative branch-style offset */
1005 array_data = insns + array_data_offset;
1006 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001007 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1009 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 return false;
1011 }
1012 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001013 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1015 /* make sure the end of the switch is in range */
1016 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1018 << ", data offset " << array_data_offset << ", end "
1019 << cur_offset + array_data_offset + table_size
1020 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 return false;
1022 }
1023 return true;
1024}
1025
Ian Rogers776ac1f2012-04-13 23:36:36 -07001026bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 int32_t offset;
1028 bool isConditional, selfOkay;
1029 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1030 return false;
1031 }
1032 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1034 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 return false;
1036 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001037 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1038 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1041 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 return false;
1043 }
1044 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1045 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001046 if (abs_offset < 0 ||
1047 (uint32_t) abs_offset >= insn_count ||
1048 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001050 << reinterpret_cast<void*>(abs_offset) << ") at "
1051 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
1054 insn_flags_[abs_offset].SetBranchTarget();
1055 return true;
1056}
1057
Ian Rogers776ac1f2012-04-13 23:36:36 -07001058bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 bool* selfOkay) {
1060 const uint16_t* insns = code_item_->insns_ + cur_offset;
1061 *pConditional = false;
1062 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001063 switch (*insns & 0xff) {
1064 case Instruction::GOTO:
1065 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001066 break;
1067 case Instruction::GOTO_32:
1068 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001069 *selfOkay = true;
1070 break;
1071 case Instruction::GOTO_16:
1072 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001073 break;
1074 case Instruction::IF_EQ:
1075 case Instruction::IF_NE:
1076 case Instruction::IF_LT:
1077 case Instruction::IF_GE:
1078 case Instruction::IF_GT:
1079 case Instruction::IF_LE:
1080 case Instruction::IF_EQZ:
1081 case Instruction::IF_NEZ:
1082 case Instruction::IF_LTZ:
1083 case Instruction::IF_GEZ:
1084 case Instruction::IF_GTZ:
1085 case Instruction::IF_LEZ:
1086 *pOffset = (int16_t) insns[1];
1087 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001088 break;
1089 default:
1090 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001091 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001092 return true;
1093}
1094
Ian Rogers776ac1f2012-04-13 23:36:36 -07001095bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001097 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001099 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001101 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001103 << ", switch offset " << switch_offset
1104 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001105 return false;
1106 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001107 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001109 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001110 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1112 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 return false;
1114 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 uint32_t switch_count = switch_insns[1];
1116 int32_t keys_offset, targets_offset;
1117 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001118 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1119 /* 0=sig, 1=count, 2/3=firstKey */
1120 targets_offset = 4;
1121 keys_offset = -1;
1122 expected_signature = Instruction::kPackedSwitchSignature;
1123 } else {
1124 /* 0=sig, 1=count, 2..count*2 = keys */
1125 keys_offset = 2;
1126 targets_offset = 2 + 2 * switch_count;
1127 expected_signature = Instruction::kSparseSwitchSignature;
1128 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001131 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1132 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1133 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001134 return false;
1135 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 /* make sure the end of the switch is in range */
1137 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1139 << ", switch offset " << switch_offset
1140 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001141 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 return false;
1143 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001144 /* for a sparse switch, verify the keys are in ascending order */
1145 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1147 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001148 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1149 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1150 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1152 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001153 return false;
1154 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 last_key = key;
1156 }
1157 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 for (uint32_t targ = 0; targ < switch_count; targ++) {
1160 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1161 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1162 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001163 if (abs_offset < 0 ||
1164 abs_offset >= (int32_t) insn_count ||
1165 !insn_flags_[abs_offset].IsOpcode()) {
1166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1167 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1168 << reinterpret_cast<void*>(cur_offset)
1169 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001170 return false;
1171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 insn_flags_[abs_offset].SetBranchTarget();
1173 }
1174 return true;
1175}
1176
Ian Rogers776ac1f2012-04-13 23:36:36 -07001177bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001178 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 return false;
1181 }
1182 uint16_t registers_size = code_item_->registers_size_;
1183 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001184 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1186 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 return false;
1188 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001189 }
1190
1191 return true;
1192}
1193
Ian Rogers776ac1f2012-04-13 23:36:36 -07001194bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 uint16_t registers_size = code_item_->registers_size_;
1196 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1197 // integer overflow when adding them here.
1198 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1200 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001201 return false;
1202 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 return true;
1204}
1205
Ian Rogers776ac1f2012-04-13 23:36:36 -07001206bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 uint16_t registers_size = code_item_->registers_size_;
1208 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001209
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 reg_table_.Init(kTrackCompilerInterestPoints,
1212 insn_flags_.get(),
1213 insns_size,
1214 registers_size,
1215 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001216
jeffhaobdb76512011-09-07 11:43:16 -07001217
Ian Rogersd0fbd852013-09-24 18:17:04 -07001218 work_line_.reset(RegisterLine::Create(registers_size, this));
1219 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001220
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 /* Initialize register types of method arguments. */
1222 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001223 DCHECK_NE(failures_.size(), 0U);
1224 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001225 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001226 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 return false;
1228 }
1229 /* Perform code flow verification. */
1230 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001231 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001233 }
jeffhaobdb76512011-09-07 11:43:16 -07001234 return true;
1235}
1236
Ian Rogersad0b3a32012-04-16 14:50:24 -07001237std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1238 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001239 for (size_t i = 0; i < failures_.size(); ++i) {
1240 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001241 }
1242 return os;
1243}
1244
Ian Rogers776ac1f2012-04-13 23:36:36 -07001245void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001246 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001247 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 return;
jeffhaobdb76512011-09-07 11:43:16 -07001249 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001250 {
1251 os << "Register Types:\n";
1252 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1253 std::ostream indent_os(&indent_filter);
1254 reg_types_.Dump(indent_os);
1255 }
Ian Rogersb4903572012-10-11 11:52:56 -07001256 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001257 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1258 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 const Instruction* inst = Instruction::At(code_item_->insns_);
1260 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001261 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001263 if (reg_line != nullptr) {
1264 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001265 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001266 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001267 const bool kDumpHexOfInstruction = false;
1268 if (kDumpHexOfInstruction) {
1269 indent_os << inst->DumpHex(5) << " ";
1270 }
1271 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001272 inst = inst->Next();
1273 }
jeffhaobdb76512011-09-07 11:43:16 -07001274}
1275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276static bool IsPrimitiveDescriptor(char descriptor) {
1277 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001278 case 'I':
1279 case 'C':
1280 case 'S':
1281 case 'B':
1282 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001283 case 'F':
1284 case 'D':
1285 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001287 default:
1288 return false;
1289 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001290}
1291
Ian Rogers776ac1f2012-04-13 23:36:36 -07001292bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 RegisterLine* reg_line = reg_table_.GetLine(0);
1294 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1295 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001296
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001298 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001300 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1302 // argument as uninitialized. This restricts field access until the superclass constructor is
1303 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001304 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001305 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001306 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 reg_types_.UninitializedThisArgument(declaring_class));
1308 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001309 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001310 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001312 }
1313
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001314 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001315 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001316 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001317
1318 for (; iterator.HasNext(); iterator.Next()) {
1319 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001320 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 LOG(FATAL) << "Null descriptor";
1322 }
1323 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1325 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 return false;
1327 }
1328 switch (descriptor[0]) {
1329 case 'L':
1330 case '[':
1331 // We assume that reference arguments are initialized. The only way it could be otherwise
1332 // (assuming the caller was verified) is if the current method is <init>, but in that case
1333 // it's effectively considered initialized the instant we reach here (in the sense that we
1334 // can return without doing anything or call virtual methods).
1335 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001336 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001337 if (!reg_type.IsNonZeroReferenceTypes()) {
1338 DCHECK(HasFailures());
1339 return false;
1340 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001341 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 }
1343 break;
1344 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001345 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 break;
1347 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001348 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 break;
1350 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001351 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 break;
1353 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001354 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 break;
1356 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001357 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 break;
1359 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001360 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 break;
1362 case 'J':
1363 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001364 if (cur_arg + 1 >= expected_args) {
1365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1366 << " args, found more (" << descriptor << ")";
1367 return false;
1368 }
1369
Ian Rogers7b078e82014-09-10 14:44:24 -07001370 const RegType* lo_half;
1371 const RegType* hi_half;
1372 if (descriptor[0] == 'J') {
1373 lo_half = &reg_types_.LongLo();
1374 hi_half = &reg_types_.LongHi();
1375 } else {
1376 lo_half = &reg_types_.DoubleLo();
1377 hi_half = &reg_types_.DoubleHi();
1378 }
1379 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 cur_arg++;
1381 break;
1382 }
1383 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1385 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 return false;
1387 }
1388 cur_arg++;
1389 }
1390 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1392 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 return false;
1394 }
1395 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1396 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1397 // format. Only major difference from the method argument format is that 'V' is supported.
1398 bool result;
1399 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1400 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001401 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 size_t i = 0;
1403 do {
1404 i++;
1405 } while (descriptor[i] == '['); // process leading [
1406 if (descriptor[i] == 'L') { // object array
1407 do {
1408 i++; // find closing ;
1409 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1410 result = descriptor[i] == ';';
1411 } else { // primitive array
1412 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1413 }
1414 } else if (descriptor[0] == 'L') {
1415 // could be more thorough here, but shouldn't be required
1416 size_t i = 0;
1417 do {
1418 i++;
1419 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1420 result = descriptor[i] == ';';
1421 } else {
1422 result = false;
1423 }
1424 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1426 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 }
1428 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001429}
1430
Ian Rogers776ac1f2012-04-13 23:36:36 -07001431bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 const uint16_t* insns = code_item_->insns_;
1433 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001434
jeffhaobdb76512011-09-07 11:43:16 -07001435 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 insn_flags_[0].SetChanged();
1437 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001438
jeffhaobdb76512011-09-07 11:43:16 -07001439 /* Continue until no instructions are marked "changed". */
1440 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001441 if (allow_thread_suspension_) {
1442 self_->AllowThreadSuspension();
1443 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1445 uint32_t insn_idx = start_guess;
1446 for (; insn_idx < insns_size; insn_idx++) {
1447 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001448 break;
1449 }
jeffhaobdb76512011-09-07 11:43:16 -07001450 if (insn_idx == insns_size) {
1451 if (start_guess != 0) {
1452 /* try again, starting from the top */
1453 start_guess = 0;
1454 continue;
1455 } else {
1456 /* all flags are clear */
1457 break;
1458 }
1459 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 // We carry the working set of registers from instruction to instruction. If this address can
1461 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1462 // "changed" flags, we need to load the set of registers from the table.
1463 // Because we always prefer to continue on to the next instruction, we should never have a
1464 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1465 // target.
1466 work_insn_idx_ = insn_idx;
1467 if (insn_flags_[insn_idx].IsBranchTarget()) {
1468 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001469 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001470 /*
1471 * Sanity check: retrieve the stored register line (assuming
1472 * a full table) and make sure it actually matches.
1473 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001475 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 if (work_line_->CompareLine(register_line) != 0) {
1477 Dump(std::cout);
1478 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001480 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001481 << " work_line=" << work_line_->Dump(this) << "\n"
1482 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 }
jeffhaobdb76512011-09-07 11:43:16 -07001484 }
jeffhaobdb76512011-09-07 11:43:16 -07001485 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001487 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001488 prepend += " failed to verify: ";
1489 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 return false;
1491 }
jeffhaobdb76512011-09-07 11:43:16 -07001492 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 insn_flags_[insn_idx].SetVisited();
1494 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001495 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001496
Ian Rogers1c849e52012-06-28 14:00:33 -07001497 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001498 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001499 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001500 * (besides the wasted space), but it indicates a flaw somewhere
1501 * down the line, possibly in the verifier.
1502 *
1503 * If we've substituted "always throw" instructions into the stream,
1504 * we are almost certainly going to have some dead code.
1505 */
1506 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001508 for (; insn_idx < insns_size;
1509 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001510 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001511 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001512 * may or may not be preceded by a padding NOP (for alignment).
1513 */
1514 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1515 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1516 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001517 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001518 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1519 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1520 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001522 }
1523
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001525 if (dead_start < 0)
1526 dead_start = insn_idx;
1527 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001528 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1529 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001530 dead_start = -1;
1531 }
1532 }
1533 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001534 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1535 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001536 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001537 // To dump the state of the verify after a method, do something like:
1538 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1539 // "boolean java.lang.String.equals(java.lang.Object)") {
1540 // LOG(INFO) << info_messages_.str();
1541 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543 return true;
1544}
1545
Ian Rogers776ac1f2012-04-13 23:36:36 -07001546bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001547 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1548 // We want the state _before_ the instruction, for the case where the dex pc we're
1549 // interested in is itself a monitor-enter instruction (which is a likely place
1550 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001551 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001552 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001553 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1554 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1555 }
1556 }
1557
jeffhaobdb76512011-09-07 11:43:16 -07001558 /*
1559 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001560 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001561 * control to another statement:
1562 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001563 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001564 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001565 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001566 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001567 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001568 * throw an exception that is handled by an encompassing "try"
1569 * block.
1570 *
1571 * We can also return, in which case there is no successor instruction
1572 * from this point.
1573 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001574 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001575 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1577 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001578 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001579
jeffhaobdb76512011-09-07 11:43:16 -07001580 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001581 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001582 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001584 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001585 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 }
jeffhaobdb76512011-09-07 11:43:16 -07001587
1588 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001589 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001590 * can throw an exception, we will copy/merge this into the "catch"
1591 * address rather than work_line, because we don't want the result
1592 * from the "successful" code path (e.g. a check-cast that "improves"
1593 * a type) to be visible to the exception handler.
1594 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001595 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001597 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001599 }
1600
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001601
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001602 // We need to ensure the work line is consistent while performing validation. When we spot a
1603 // peephole pattern we compute a new line for either the fallthrough instruction or the
1604 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001605 std::unique_ptr<RegisterLine> branch_line;
1606 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001607
Stephen Kyle7e541c92014-12-17 17:10:02 +00001608 /*
1609 * If we are in a constructor, and we currently have an UninitializedThis type
1610 * in a register somewhere, we need to make sure it isn't overwritten.
1611 */
1612 bool track_uninitialized_this = false;
1613 size_t uninitialized_this_loc = 0;
1614 if (IsConstructor()) {
1615 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1616 }
1617
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001619 case Instruction::NOP:
1620 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001621 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001622 * a signature that looks like a NOP; if we see one of these in
1623 * the course of executing code then we have a problem.
1624 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001626 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001627 }
1628 break;
1629
1630 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001631 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 break;
jeffhaobdb76512011-09-07 11:43:16 -07001633 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001634 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 break;
jeffhaobdb76512011-09-07 11:43:16 -07001636 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001637 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001638 break;
1639 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001640 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 break;
jeffhaobdb76512011-09-07 11:43:16 -07001642 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001643 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 break;
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001647 break;
1648 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001649 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 break;
jeffhaobdb76512011-09-07 11:43:16 -07001651 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001652 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 break;
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001655 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657
1658 /*
1659 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001660 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001661 * might want to hold the result in an actual CPU register, so the
1662 * Dalvik spec requires that these only appear immediately after an
1663 * invoke or filled-new-array.
1664 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001666 * redundant with the reset done below, but it can make the debug info
1667 * easier to read in some cases.)
1668 */
1669 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001670 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001671 break;
1672 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001673 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001674 break;
1675 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001676 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001677 break;
1678
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001680 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1681 // where one entrypoint to the catch block is not actually an exception path.
1682 if (work_insn_idx_ == 0) {
1683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1684 break;
1685 }
jeffhaobdb76512011-09-07 11:43:16 -07001686 /*
jeffhao60f83e32012-02-13 17:16:30 -08001687 * This statement can only appear as the first instruction in an exception handler. We verify
1688 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001689 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001690 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001691 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 }
jeffhaobdb76512011-09-07 11:43:16 -07001694 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001695 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001696 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 }
jeffhaobdb76512011-09-07 11:43:16 -07001699 }
1700 break;
1701 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001702 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001703 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001704 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1707 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 } else {
1709 // Compilers may generate synthetic functions that write byte values into boolean fields.
1710 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001711 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001712 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1714 ((return_type.IsBoolean() || return_type.IsByte() ||
1715 return_type.IsShort() || return_type.IsChar()) &&
1716 src_type.IsInteger()));
1717 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001718 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001719 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001720 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 }
1724 }
1725 break;
1726 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001727 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001728 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001729 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 } else {
1733 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001736 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 }
jeffhaobdb76512011-09-07 11:43:16 -07001739 }
1740 }
1741 break;
1742 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001743 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001744 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 } else {
1748 /* return_type is the *expected* return type, not register value */
1749 DCHECK(!return_type.IsZero());
1750 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001751 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001753 // Disallow returning uninitialized values and verify that the reference in vAA is an
1754 // instance of the "return_type"
1755 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001756 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1757 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001758 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001759 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1760 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1761 << "' or '" << reg_type << "'";
1762 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001763 bool soft_error = false;
1764 // Check whether arrays are involved. They will show a valid class status, even
1765 // if their components are erroneous.
1766 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1767 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1768 if (soft_error) {
1769 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1770 << reg_type << " vs " << return_type;
1771 }
1772 }
1773
1774 if (!soft_error) {
1775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1776 << "', but expected from declaration '" << return_type << "'";
1777 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001778 }
jeffhaobdb76512011-09-07 11:43:16 -07001779 }
1780 }
1781 }
1782 break;
1783
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001784 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 case Instruction::CONST_4: {
1786 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001787 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001788 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001789 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 }
1791 case Instruction::CONST_16: {
1792 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001793 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001794 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001795 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001797 case Instruction::CONST: {
1798 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001799 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001800 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001801 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001802 }
1803 case Instruction::CONST_HIGH16: {
1804 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001806 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001808 }
jeffhaobdb76512011-09-07 11:43:16 -07001809 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001810 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001811 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001812 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1813 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001814 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001815 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001816 }
1817 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001819 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1820 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001821 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 break;
1823 }
1824 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001826 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1827 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001828 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001829 break;
1830 }
1831 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001832 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001833 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1834 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001835 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001836 break;
1837 }
jeffhaobdb76512011-09-07 11:43:16 -07001838 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001839 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001840 break;
jeffhaobdb76512011-09-07 11:43:16 -07001841 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001842 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001843 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001845 // Get type from instruction if unresolved then we need an access check
1846 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001847 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001848 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001849 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001850 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001852 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 }
jeffhaobdb76512011-09-07 11:43:16 -07001854 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
1857 case Instruction::MONITOR_EXIT:
1858 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001859 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001860 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001861 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001862 * to the need to handle asynchronous exceptions, a now-deprecated
1863 * feature that Dalvik doesn't support.)
1864 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001865 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001866 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001867 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001868 * structured locking checks are working, the former would have
1869 * failed on the -enter instruction, and the latter is impossible.
1870 *
1871 * This is fortunate, because issue 3221411 prevents us from
1872 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001873 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001874 * some catch blocks (which will show up as "dead" code when
1875 * we skip them here); if we can't, then the code path could be
1876 * "live" so we still need to check it.
1877 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001878 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001880 break;
1881
Ian Rogers28ad40d2011-10-27 15:19:26 -07001882 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001884 /*
1885 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1886 * could be a "upcast" -- not expected, so we don't try to address it.)
1887 *
1888 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001889 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001890 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001891 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1892 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001893 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001894 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001895 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001896 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001897 if (klass != nullptr && klass->IsPrimitive()) {
1898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1899 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1900 << GetDeclaringClass();
1901 break;
1902 }
1903
Ian Rogersad0b3a32012-04-16 14:50:24 -07001904 DCHECK_NE(failures_.size(), 0U);
1905 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001906 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001907 }
1908 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001909 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001910 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001911 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001912 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001913 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001914 if (is_checkcast) {
1915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1916 } else {
1917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1918 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001919 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001920 if (is_checkcast) {
1921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1922 } else {
1923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1924 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001925 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001926 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001930 }
jeffhaobdb76512011-09-07 11:43:16 -07001931 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001932 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 }
1934 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001935 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001936 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001937 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001938 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001942 } else {
1943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 }
1945 break;
1946 }
1947 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001948 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001949 if (res_type.IsConflict()) {
1950 DCHECK_NE(failures_.size(), 0U);
1951 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001952 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001953 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1954 // can't create an instance of an interface or abstract class */
1955 if (!res_type.IsInstantiableTypes()) {
1956 Fail(VERIFY_ERROR_INSTANTIATION)
1957 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001958 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001960 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001961 // Any registers holding previous allocations from this address that have not yet been
1962 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001963 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001964 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001965 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 break;
1967 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001968 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001969 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001970 break;
1971 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001973 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001974 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001975 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001976 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001977 just_set_result = true; // Filled new array range sets result register
1978 break;
jeffhaobdb76512011-09-07 11:43:16 -07001979 case Instruction::CMPL_FLOAT:
1980 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001981 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001982 break;
1983 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001984 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001985 break;
1986 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001987 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
1989 case Instruction::CMPL_DOUBLE:
1990 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001991 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001992 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001993 break;
1994 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001995 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001996 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001997 break;
1998 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
2001 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002002 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002003 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002004 break;
2005 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002006 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002007 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002008 break;
2009 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002010 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002011 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002013 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002014 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002015 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2016 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002017 }
2018 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 }
jeffhaobdb76512011-09-07 11:43:16 -07002020 case Instruction::GOTO:
2021 case Instruction::GOTO_16:
2022 case Instruction::GOTO_32:
2023 /* no effect on or use of registers */
2024 break;
2025
2026 case Instruction::PACKED_SWITCH:
2027 case Instruction::SPARSE_SWITCH:
2028 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002029 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 case Instruction::FILL_ARRAY_DATA: {
2033 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002034 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002035 /* array_type can be null if the reg type is Zero */
2036 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002037 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002038 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2039 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002040 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002041 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002042 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002043 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2045 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 } else {
jeffhao457cc512012-02-02 16:55:13 -08002047 // Now verify if the element width in the table matches the element width declared in
2048 // the array
2049 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2050 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002052 } else {
2053 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2054 // Since we don't compress the data in Dex, expect to see equal width of data stored
2055 // in the table and expected from the array class.
2056 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002057 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2058 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002059 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 }
2061 }
jeffhaobdb76512011-09-07 11:43:16 -07002062 }
2063 }
2064 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 }
jeffhaobdb76512011-09-07 11:43:16 -07002066 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002068 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2069 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 bool mismatch = false;
2071 if (reg_type1.IsZero()) { // zero then integral or reference expected
2072 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2073 } else if (reg_type1.IsReferenceTypes()) { // both references?
2074 mismatch = !reg_type2.IsReferenceTypes();
2075 } else { // both integral?
2076 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2077 }
2078 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2080 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002081 }
2082 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 }
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::IF_LT:
2085 case Instruction::IF_GE:
2086 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2089 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2092 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002093 }
2094 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 }
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002098 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2101 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002103
2104 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002105 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002106 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002107 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002108 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002109 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002110 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002111 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2112 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2113 work_insn_idx_)) {
2114 break;
2115 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002116 } else {
2117 break;
2118 }
2119
Ian Rogers9b360392013-06-06 14:45:07 -07002120 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002121
2122 /* Check for peep-hole pattern of:
2123 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002124 * instance-of vX, vY, T;
2125 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002126 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002127 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002128 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002129 * and sharpen the type of vY to be type T.
2130 * Note, this pattern can't be if:
2131 * - if there are other branches to this branch,
2132 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002133 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002134 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002135 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2136 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2137 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002138 // Check the type of the instance-of is different than that of registers type, as if they
2139 // are the same there is no work to be done here. Check that the conversion is not to or
2140 // from an unresolved type as type information is imprecise. If the instance-of is to an
2141 // interface then ignore the type information as interfaces can only be treated as Objects
2142 // and we don't want to disallow field and other operations on the object. If the value
2143 // being instance-of checked against is known null (zero) then allow the optimization as
2144 // we didn't have type information. If the merge of the instance-of type with the original
2145 // type is assignable to the original then allow optimization. This check is performed to
2146 // ensure that subsequent merges don't lose type information - such as becoming an
2147 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002148 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002149 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002150
Ian Rogersebbdd872014-07-07 23:53:08 -07002151 if (!orig_type.Equals(cast_type) &&
2152 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002153 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002154 !cast_type.GetClass()->IsInterface() &&
2155 (orig_type.IsZero() ||
2156 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002157 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002158 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002159 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002160 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002161 branch_line.reset(update_line);
2162 }
2163 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002164 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002165 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2166 // See if instance-of was preceded by a move-object operation, common due to the small
2167 // register encoding space of instance-of, and propagate type information to the source
2168 // of the move-object.
2169 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002170 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002171 move_idx--;
2172 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002173 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2174 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2175 work_insn_idx_)) {
2176 break;
2177 }
Ian Rogers9b360392013-06-06 14:45:07 -07002178 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2179 switch (move_inst->Opcode()) {
2180 case Instruction::MOVE_OBJECT:
2181 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002182 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002183 }
2184 break;
2185 case Instruction::MOVE_OBJECT_FROM16:
2186 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002187 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002188 }
2189 break;
2190 case Instruction::MOVE_OBJECT_16:
2191 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002192 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002193 }
2194 break;
2195 default:
2196 break;
2197 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002198 }
2199 }
2200 }
2201
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 }
jeffhaobdb76512011-09-07 11:43:16 -07002204 case Instruction::IF_LTZ:
2205 case Instruction::IF_GEZ:
2206 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002208 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2211 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 }
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 }
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 break;
jeffhaobdb76512011-09-07 11:43:16 -07002218 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002219 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 break;
jeffhaobdb76512011-09-07 11:43:16 -07002221 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002222 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 break;
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 break;
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 break;
2233 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
2236
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 break;
2240 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002241 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 break;
2243 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002244 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 break;
2246 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
2249 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002253 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002254 break;
2255 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002256 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002257 break;
2258
jeffhaobdb76512011-09-07 11:43:16 -07002259 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002260 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 break;
jeffhaobdb76512011-09-07 11:43:16 -07002262 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002263 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 break;
jeffhaobdb76512011-09-07 11:43:16 -07002265 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002266 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 break;
jeffhaobdb76512011-09-07 11:43:16 -07002268 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002269 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 break;
2271 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002272 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
2274 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002275 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002278 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2279 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 break;
jeffhaobdb76512011-09-07 11:43:16 -07002281
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002283 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 break;
2285 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002286 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 break;
2288 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002289 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 break;
2291 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002292 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002295 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002296 break;
2297 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002298 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 break;
jeffhaobdb76512011-09-07 11:43:16 -07002300 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002301 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2302 false);
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
2304
jeffhaobdb76512011-09-07 11:43:16 -07002305 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002306 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 break;
jeffhaobdb76512011-09-07 11:43:16 -07002308 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002309 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
jeffhaobdb76512011-09-07 11:43:16 -07002311 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002312 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 break;
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002315 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 break;
2317 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002318 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002321 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002324 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2325 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 break;
2327
2328 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002329 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 break;
2331 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002332 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 break;
2334 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002335 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 break;
2337 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002338 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
2340 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002341 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002344 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002347 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2348 true);
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350
2351 case Instruction::INVOKE_VIRTUAL:
2352 case Instruction::INVOKE_VIRTUAL_RANGE:
2353 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2356 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002357 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2358 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002359 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002360 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002361 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002362 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002363 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002364 if (return_type_class != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002365 return_type = &reg_types_.FromClass(called_method->GetReturnTypeDescriptor(),
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002366 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002367 return_type_class->CannotBeAssignedFromOtherTypes());
2368 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002369 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2370 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002371 }
2372 }
2373 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2376 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002377 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002378 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002379 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002380 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002381 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002382 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002383 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002385 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 }
jeffhaobdb76512011-09-07 11:43:16 -07002388 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002391 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002392 const char* return_type_descriptor;
2393 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002394 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002395 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002397 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002398 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002399 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2400 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2401 } else {
2402 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002403 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002404 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002405 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002406 if (return_type_class != nullptr) {
2407 return_type = &reg_types_.FromClass(return_type_descriptor,
2408 return_type_class,
2409 return_type_class->CannotBeAssignedFromOtherTypes());
2410 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002411 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2412 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002413 }
Ian Rogers46685432012-06-03 22:26:43 -07002414 }
2415 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002416 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 * Some additional checks when calling a constructor. We know from the invocation arg check
2418 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2419 * that to require that called_method->klass is the same as this->klass or this->super,
2420 * allowing the latter only if the "this" argument is the same as the "this" argument to
2421 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002422 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002423 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002424 if (this_type.IsConflict()) // failure.
2425 break;
jeffhaobdb76512011-09-07 11:43:16 -07002426
jeffhaob57e9522012-04-26 18:08:21 -07002427 /* no null refs allowed (?) */
2428 if (this_type.IsZero()) {
2429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2430 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002431 }
jeffhaob57e9522012-04-26 18:08:21 -07002432
2433 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002434 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002435 // TODO: re-enable constructor type verification
2436 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002437 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002438 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2439 // break;
2440 // }
jeffhaob57e9522012-04-26 18:08:21 -07002441
2442 /* arg must be an uninitialized reference */
2443 if (!this_type.IsUninitializedTypes()) {
2444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2445 << this_type;
2446 break;
2447 }
2448
2449 /*
2450 * Replace the uninitialized reference with an initialized one. We need to do this for all
2451 * registers that have the same object instance in them, not just the "this" register.
2452 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002453 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2454 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002455 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002456 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002457 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2458 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002459 }
2460 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002461 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002462 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002463 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002465 just_set_result = true;
2466 break;
2467 }
2468 case Instruction::INVOKE_STATIC:
2469 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002470 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002471 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002472 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002474 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002475 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2476 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002477 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002478 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002479 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002481 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002482 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002484 } else {
2485 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2486 }
jeffhaobdb76512011-09-07 11:43:16 -07002487 just_set_result = true;
2488 }
2489 break;
jeffhaobdb76512011-09-07 11:43:16 -07002490 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002492 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002493 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002495 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002496 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2497 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2498 << PrettyMethod(abs_method) << "'";
2499 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002500 }
Ian Rogers0d604842012-04-16 14:50:24 -07002501 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002502 /* Get the type of the "this" arg, which should either be a sub-interface of called
2503 * interface or Object (see comments in RegType::JoinClass).
2504 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002506 if (this_type.IsZero()) {
2507 /* null pointer always passes (and always fails at runtime) */
2508 } else {
2509 if (this_type.IsUninitializedTypes()) {
2510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2511 << this_type;
2512 break;
2513 }
2514 // In the past we have tried to assert that "called_interface" is assignable
2515 // from "this_type.GetClass()", however, as we do an imprecise Join
2516 // (RegType::JoinClass) we don't have full information on what interfaces are
2517 // implemented by "this_type". For example, two classes may implement the same
2518 // interfaces and have a common parent that doesn't implement the interface. The
2519 // join will set "this_type" to the parent class and a test that this implements
2520 // the interface will incorrectly fail.
2521 }
2522 /*
2523 * We don't have an object instance, so we can't find the concrete method. However, all of
2524 * the type information is in the abstract method, so we're good.
2525 */
2526 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002529 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2530 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2531 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2532 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002533 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002534 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002535 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002536 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002537 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002538 } else {
2539 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2540 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002541 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002542 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 }
jeffhaobdb76512011-09-07 11:43:16 -07002544 case Instruction::NEG_INT:
2545 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::NEG_LONG:
2549 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002550 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002551 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002557 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002558 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002562 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002568 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002569 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
2571 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002572 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002573 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002577 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002580 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002581 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002582 break;
2583 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002584 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002588 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002592 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002595 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002596 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002597 break;
2598 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002599 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002600 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002601 break;
2602 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002603 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002604 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002607 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
2609 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002610 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002613 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615
2616 case Instruction::ADD_INT:
2617 case Instruction::SUB_INT:
2618 case Instruction::MUL_INT:
2619 case Instruction::REM_INT:
2620 case Instruction::DIV_INT:
2621 case Instruction::SHL_INT:
2622 case Instruction::SHR_INT:
2623 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002624 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002625 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::AND_INT:
2628 case Instruction::OR_INT:
2629 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002630 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002631 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::ADD_LONG:
2634 case Instruction::SUB_LONG:
2635 case Instruction::MUL_LONG:
2636 case Instruction::DIV_LONG:
2637 case Instruction::REM_LONG:
2638 case Instruction::AND_LONG:
2639 case Instruction::OR_LONG:
2640 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002641 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002642 reg_types_.LongLo(), reg_types_.LongHi(),
2643 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::SHL_LONG:
2646 case Instruction::SHR_LONG:
2647 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002649 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002650 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652 case Instruction::ADD_FLOAT:
2653 case Instruction::SUB_FLOAT:
2654 case Instruction::MUL_FLOAT:
2655 case Instruction::DIV_FLOAT:
2656 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2658 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::ADD_DOUBLE:
2661 case Instruction::SUB_DOUBLE:
2662 case Instruction::MUL_DOUBLE:
2663 case Instruction::DIV_DOUBLE:
2664 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2667 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002668 break;
2669 case Instruction::ADD_INT_2ADDR:
2670 case Instruction::SUB_INT_2ADDR:
2671 case Instruction::MUL_INT_2ADDR:
2672 case Instruction::REM_INT_2ADDR:
2673 case Instruction::SHL_INT_2ADDR:
2674 case Instruction::SHR_INT_2ADDR:
2675 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002676 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2677 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::AND_INT_2ADDR:
2680 case Instruction::OR_INT_2ADDR:
2681 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002682 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2683 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002684 break;
2685 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002686 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2687 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::ADD_LONG_2ADDR:
2690 case Instruction::SUB_LONG_2ADDR:
2691 case Instruction::MUL_LONG_2ADDR:
2692 case Instruction::DIV_LONG_2ADDR:
2693 case Instruction::REM_LONG_2ADDR:
2694 case Instruction::AND_LONG_2ADDR:
2695 case Instruction::OR_LONG_2ADDR:
2696 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002697 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 reg_types_.LongLo(), reg_types_.LongHi(),
2699 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::SHL_LONG_2ADDR:
2702 case Instruction::SHR_LONG_2ADDR:
2703 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002705 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::ADD_FLOAT_2ADDR:
2708 case Instruction::SUB_FLOAT_2ADDR:
2709 case Instruction::MUL_FLOAT_2ADDR:
2710 case Instruction::DIV_FLOAT_2ADDR:
2711 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002712 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2713 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715 case Instruction::ADD_DOUBLE_2ADDR:
2716 case Instruction::SUB_DOUBLE_2ADDR:
2717 case Instruction::MUL_DOUBLE_2ADDR:
2718 case Instruction::DIV_DOUBLE_2ADDR:
2719 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002720 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002721 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2722 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002725 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::MUL_INT_LIT16:
2727 case Instruction::DIV_INT_LIT16:
2728 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2730 true);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::AND_INT_LIT16:
2733 case Instruction::OR_INT_LIT16:
2734 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002735 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2736 true);
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::ADD_INT_LIT8:
2739 case Instruction::RSUB_INT_LIT8:
2740 case Instruction::MUL_INT_LIT8:
2741 case Instruction::DIV_INT_LIT8:
2742 case Instruction::REM_INT_LIT8:
2743 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002744 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002745 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002746 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2747 false);
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::AND_INT_LIT8:
2750 case Instruction::OR_INT_LIT8:
2751 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002752 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2753 false);
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002756 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002757 case Instruction::RETURN_VOID_NO_BARRIER:
2758 if (IsConstructor() && !IsStatic()) {
2759 auto& declaring_class = GetDeclaringClass();
2760 auto* klass = declaring_class.GetClass();
2761 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2762 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2764 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002765 break;
2766 }
2767 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002768 }
2769 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002770 // Note: the following instructions encode offsets derived from class linking.
2771 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2772 // meaning if the class linking and resolution were successful.
2773 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002774 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002775 break;
2776 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002777 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002778 break;
2779 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002780 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002781 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002782 case Instruction::IGET_BOOLEAN_QUICK:
2783 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2784 break;
2785 case Instruction::IGET_BYTE_QUICK:
2786 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2787 break;
2788 case Instruction::IGET_CHAR_QUICK:
2789 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2790 break;
2791 case Instruction::IGET_SHORT_QUICK:
2792 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2793 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002794 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002795 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002796 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002797 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002798 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002799 break;
2800 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002801 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002802 break;
2803 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002804 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002805 break;
2806 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002807 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002808 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002809 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002811 break;
2812 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002813 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002814 break;
2815 case Instruction::INVOKE_VIRTUAL_QUICK:
2816 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2817 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002818 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002819 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002820 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002821 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002822 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002823 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002824 } else {
2825 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2826 }
2827 just_set_result = true;
2828 }
2829 break;
2830 }
2831
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002833 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2834 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002835 case Instruction::UNUSED_79:
2836 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002838 break;
2839
2840 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002841 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002842 * complain if an instruction is missing (which is desirable).
2843 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002844 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002845
Stephen Kyle7e541c92014-12-17 17:10:02 +00002846 /*
2847 * If we are in a constructor, and we had an UninitializedThis type
2848 * in a register somewhere, we need to make sure it wasn't overwritten.
2849 */
2850 if (track_uninitialized_this) {
2851 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2852 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2853 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2854 was_invoke_direct)) {
2855 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2856 << "Constructor failed to initialize this object";
2857 }
2858 }
2859
Ian Rogersad0b3a32012-04-16 14:50:24 -07002860 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002861 if (Runtime::Current()->IsAotCompiler()) {
2862 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002863 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2864 LOG(ERROR) << "Pending failures:";
2865 for (auto& error : failures_) {
2866 LOG(ERROR) << error;
2867 }
2868 for (auto& error_msg : failure_messages_) {
2869 LOG(ERROR) << error_msg->str();
2870 }
2871 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
2872 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002873 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002874 /* immediate failure, reject class */
2875 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2876 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002877 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002878 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002879 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002880 }
jeffhaobdb76512011-09-07 11:43:16 -07002881 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 * If we didn't just set the result register, clear it out. This ensures that you can only use
2883 * "move-result" immediately after the result is set. (We could check this statically, but it's
2884 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002885 */
2886 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002887 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002888 }
2889
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002890
jeffhaobdb76512011-09-07 11:43:16 -07002891
2892 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002893 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002894 *
2895 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002896 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002897 * somebody could get a reference field, check it for zero, and if the
2898 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002899 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002900 * that, and will reject the code.
2901 *
2902 * TODO: avoid re-fetching the branch target
2903 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002904 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002905 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002907 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002909 return false;
2910 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002911 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002912 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002913 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 }
jeffhaobdb76512011-09-07 11:43:16 -07002915 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002916 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002917 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002918 return false;
2919 }
2920 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002921 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002922 return false;
2923 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 }
jeffhaobdb76512011-09-07 11:43:16 -07002925 }
2926
2927 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002928 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002929 *
2930 * We've already verified that the table is structurally sound, so we
2931 * just need to walk through and tag the targets.
2932 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002933 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002934 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2935 const uint16_t* switch_insns = insns + offset_to_switch;
2936 int switch_count = switch_insns[1];
2937 int offset_to_targets, targ;
2938
2939 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2940 /* 0 = sig, 1 = count, 2/3 = first key */
2941 offset_to_targets = 4;
2942 } else {
2943 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002944 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002945 offset_to_targets = 2 + 2 * switch_count;
2946 }
2947
2948 /* verify each switch target */
2949 for (targ = 0; targ < switch_count; targ++) {
2950 int offset;
2951 uint32_t abs_offset;
2952
2953 /* offsets are 32-bit, and only partly endian-swapped */
2954 offset = switch_insns[offset_to_targets + targ * 2] |
2955 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 abs_offset = work_insn_idx_ + offset;
2957 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002958 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002959 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002960 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002961 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002962 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002963 }
jeffhaobdb76512011-09-07 11:43:16 -07002964 }
2965 }
2966
2967 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2969 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002970 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002971 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002972 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002973 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002974
Andreas Gampef91baf12014-07-18 15:41:00 -07002975 // Need the linker to try and resolve the handled class to check if it's Throwable.
2976 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2977
Ian Rogers0571d352011-11-03 19:51:38 -07002978 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002979 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2980 if (handler_type_idx == DexFile::kDexNoIndex16) {
2981 has_catch_all_handler = true;
2982 } else {
2983 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002984 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2985 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002986 if (klass != nullptr) {
2987 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2988 has_catch_all_handler = true;
2989 }
2990 } else {
2991 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 DCHECK(self_->IsExceptionPending());
2993 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002994 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 }
jeffhaobdb76512011-09-07 11:43:16 -07002996 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2998 * "work_regs", because at runtime the exception will be thrown before the instruction
2999 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003000 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003001 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003002 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 }
jeffhaobdb76512011-09-07 11:43:16 -07003004 }
3005
3006 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3008 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003009 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003010 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003011 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 * The state in work_line reflects the post-execution state. If the current instruction is a
3013 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003014 * it will do so before grabbing the lock).
3015 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003016 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003017 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003018 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003019 return false;
3020 }
3021 }
3022 }
3023
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003024 /* Handle "continue". Tag the next consecutive instruction.
3025 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3026 * because it changes work_line_ when performing peephole optimization
3027 * and this change should not be used in those cases.
3028 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003029 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003030 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3031 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003032 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3034 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003035 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003036 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3037 // next instruction isn't one.
3038 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3039 return false;
3040 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003041 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003042 // Make workline consistent with fallthrough computed from peephole optimization.
3043 work_line_->CopyFromLine(fallthrough_line.get());
3044 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003045 if (insn_flags_[next_insn_idx].IsReturn()) {
3046 // For returns we only care about the operand to the return, all other registers are dead.
3047 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3048 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003049 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003050 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003051 } else {
3052 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003053 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003054 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003055 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003056 }
3057 }
3058 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003059 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003061 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3062 // needed. If the merge changes the state of the registers then the work line will be
3063 // updated.
3064 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003065 return false;
3066 }
3067 } else {
3068 /*
3069 * We're not recording register data for the next instruction, so we don't know what the
3070 * prior state was. We have to assume that something has changed and re-evaluate it.
3071 */
3072 insn_flags_[next_insn_idx].SetChanged();
3073 }
3074 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003075
jeffhaod1f0fde2011-09-08 17:25:33 -07003076 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003077 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 return false;
3080 }
jeffhaobdb76512011-09-07 11:43:16 -07003081 }
3082
3083 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003084 * Update start_guess. Advance to the next instruction of that's
3085 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003086 * neither of those exists we're in a return or throw; leave start_guess
3087 * alone and let the caller sort it out.
3088 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003089 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3091 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003092 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003093 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003095 }
3096
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3098 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003099
3100 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003101} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003102
Ian Rogersd8f69b02014-09-10 21:43:52 +00003103const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003104 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003105 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003106 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003107 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003108 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3109 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003110 if (result.IsConflict()) {
3111 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3112 << "' in " << referrer;
3113 return result;
3114 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003115 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003116 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003117 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003118 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003119 // check at runtime if access is allowed and so pass here. If result is
3120 // primitive, skip the access check.
3121 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3122 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003123 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003124 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003125 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003126 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003127}
3128
Ian Rogersd8f69b02014-09-10 21:43:52 +00003129const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003132 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3134 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003135 CatchHandlerIterator iterator(handlers_ptr);
3136 for (; iterator.HasNext(); iterator.Next()) {
3137 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3138 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003139 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003140 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003141 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003142 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003143 if (exception.IsUnresolvedTypes()) {
3144 // We don't know enough about the type. Fail here and let runtime handle it.
3145 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3146 return exception;
3147 } else {
3148 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3149 return reg_types_.Conflict();
3150 }
Jeff Haob878f212014-04-24 16:25:36 -07003151 } else if (common_super == nullptr) {
3152 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003153 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003154 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003156 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003157 if (FailOrAbort(this,
3158 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3159 "java.lang.Throwable is not assignable-from common_super at ",
3160 work_insn_idx_)) {
3161 break;
3162 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 }
3164 }
3165 }
3166 }
Ian Rogers0571d352011-11-03 19:51:38 -07003167 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 }
3169 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003170 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003172 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003173 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003175 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003176}
3177
Mathieu Chartiere401d142015-04-22 13:56:20 -07003178ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3179 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003180 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003181 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003182 if (klass_type.IsConflict()) {
3183 std::string append(" in attempt to access method ");
3184 append += dex_file_->GetMethodName(method_id);
3185 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003187 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003188 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003189 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003190 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003191 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003192 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003193 auto* cl = Runtime::Current()->GetClassLinker();
3194 auto pointer_size = cl->GetImagePointerSize();
3195 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003196 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003197 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003198 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003199
3200 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003201 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003202 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003203 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003204 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003205 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003208 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003210 // If a virtual or interface method wasn't found with the expected type, look in
3211 // the direct methods. This can happen when the wrong invoke type is used or when
3212 // a class has changed, and will be flagged as an error in later checks.
3213 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003214 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003215 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003216 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003217 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3218 << PrettyDescriptor(klass) << "." << name
3219 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003221 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 }
3223 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003224 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3225 // enforce them here.
3226 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3228 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003230 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003231 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003232 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3234 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003235 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003236 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003237 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003238 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003239 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003240 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003241 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003242 }
jeffhaode0d9c92012-02-27 13:58:13 -08003243 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3244 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3246 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003248 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003249 // Check that interface methods match interface classes.
3250 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3251 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3252 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003253 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003254 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3255 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3256 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003257 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003258 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003259 // See if the method type implied by the invoke instruction matches the access flags for the
3260 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003261 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3263 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3264 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003265 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3266 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003267 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003269 return res_method;
3270}
3271
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003272template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003273ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3274 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003275 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3276 // match the call to the signature. Also, we might be calling through an abstract method
3277 // definition (which doesn't have register count values).
3278 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3279 /* caught by static verifier */
3280 DCHECK(is_range || expected_args <= 5);
3281 if (expected_args > code_item_->outs_size_) {
3282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3283 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3284 return nullptr;
3285 }
3286
3287 uint32_t arg[5];
3288 if (!is_range) {
3289 inst->GetVarArgs(arg);
3290 }
3291 uint32_t sig_registers = 0;
3292
3293 /*
3294 * Check the "this" argument, which must be an instance of the class that declared the method.
3295 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3296 * rigorous check here (which is okay since we have to do it at runtime).
3297 */
3298 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003299 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003300 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3301 CHECK(have_pending_hard_failure_);
3302 return nullptr;
3303 }
3304 if (actual_arg_type.IsUninitializedReference()) {
3305 if (res_method) {
3306 if (!res_method->IsConstructor()) {
3307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3308 return nullptr;
3309 }
3310 } else {
3311 // Check whether the name of the called method is "<init>"
3312 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003313 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3315 return nullptr;
3316 }
3317 }
3318 }
3319 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003320 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003321 if (res_method != nullptr) {
3322 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003323 std::string temp;
3324 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003325 klass->CannotBeAssignedFromOtherTypes());
3326 } else {
3327 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3328 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003329 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003330 dex_file_->StringByTypeIdx(class_idx),
3331 false);
3332 }
3333 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3334 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3335 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3336 << "' not instance of '" << *res_method_class << "'";
3337 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3338 // the compiler.
3339 if (have_pending_hard_failure_) {
3340 return nullptr;
3341 }
3342 }
3343 }
3344 sig_registers = 1;
3345 }
3346
3347 for ( ; it->HasNext(); it->Next()) {
3348 if (sig_registers >= expected_args) {
3349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3350 " arguments, found " << sig_registers << " or more.";
3351 return nullptr;
3352 }
3353
3354 const char* param_descriptor = it->GetDescriptor();
3355
3356 if (param_descriptor == nullptr) {
3357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3358 "component";
3359 return nullptr;
3360 }
3361
Ian Rogersd8f69b02014-09-10 21:43:52 +00003362 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003363 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3364 arg[sig_registers];
3365 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003366 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003367 if (!src_type.IsIntegralTypes()) {
3368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3369 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003370 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003371 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003372 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003373 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3374 // compiler.
3375 if (have_pending_hard_failure_) {
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003376 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003377 }
3378 }
3379 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3380 }
3381 if (expected_args != sig_registers) {
3382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3383 " arguments, found " << sig_registers;
3384 return nullptr;
3385 }
3386 return res_method;
3387}
3388
3389void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3390 MethodType method_type,
3391 bool is_range) {
3392 // As the method may not have been resolved, make this static check against what we expect.
3393 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3394 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3395 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3396 DexFileParameterIterator it(*dex_file_,
3397 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3398 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3399 nullptr);
3400}
3401
3402class MethodParamListDescriptorIterator {
3403 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003404 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003405 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3406 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3407 }
3408
3409 bool HasNext() {
3410 return pos_ < params_size_;
3411 }
3412
3413 void Next() {
3414 ++pos_;
3415 }
3416
3417 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3418 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3419 }
3420
3421 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003422 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003423 size_t pos_;
3424 const DexFile::TypeList* params_;
3425 const size_t params_size_;
3426};
3427
Mathieu Chartiere401d142015-04-22 13:56:20 -07003428ArtMethod* MethodVerifier::VerifyInvocationArgs(
3429 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003430 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3431 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003432 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003433
Mathieu Chartiere401d142015-04-22 13:56:20 -07003434 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003435 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003436 // Check what we can statically.
3437 if (!have_pending_hard_failure_) {
3438 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3439 }
3440 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003441 }
3442
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3444 // has a vtable entry for the target method.
3445 if (is_super) {
3446 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003447 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003448 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003449 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003450 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003451 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003452 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003453 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003454 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003455 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003456 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003457 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003458 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003459 << "." << res_method->GetName()
3460 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003461 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 }
3463 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003464
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003465 // Process the target method's signature. This signature may or may not
3466 MethodParamListDescriptorIterator it(res_method);
3467 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3468 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003469}
3470
Mathieu Chartiere401d142015-04-22 13:56:20 -07003471ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3472 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003473 if (is_range) {
3474 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3475 } else {
3476 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3477 }
3478 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003479 if (!actual_arg_type.HasClass()) {
3480 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003481 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003482 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003483 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003484 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003485 if (klass->IsInterface()) {
3486 // Derive Object.class from Class.class.getSuperclass().
3487 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003488 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003489 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003490 return nullptr;
3491 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003492 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003493 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003494 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003495 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003496 if (!dispatch_class->HasVTable()) {
3497 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3498 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003499 return nullptr;
3500 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003501 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003502 auto* cl = Runtime::Current()->GetClassLinker();
3503 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003504 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3505 FailOrAbort(this, allow_failure,
3506 "Receiver class has not enough vtable slots for quickened invoke at ",
3507 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003508 return nullptr;
3509 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003510 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003511 if (self_->IsExceptionPending()) {
3512 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3513 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003514 return nullptr;
3515 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003516 return res_method;
3517}
3518
Mathieu Chartiere401d142015-04-22 13:56:20 -07003519ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003520 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3521 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3522
Mathieu Chartiere401d142015-04-22 13:56:20 -07003523 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003524 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003526 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003527 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003528 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3529 work_insn_idx_)) {
3530 return nullptr;
3531 }
3532 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3533 work_insn_idx_)) {
3534 return nullptr;
3535 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003536
3537 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3538 // match the call to the signature. Also, we might be calling through an abstract method
3539 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003540 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003541 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003542 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003543 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003544 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3545 /* caught by static verifier */
3546 DCHECK(is_range || expected_args <= 5);
3547 if (expected_args > code_item_->outs_size_) {
3548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3549 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003550 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003551 }
3552
3553 /*
3554 * Check the "this" argument, which must be an instance of the class that declared the method.
3555 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3556 * rigorous check here (which is okay since we have to do it at runtime).
3557 */
3558 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003560 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003561 }
3562 if (!actual_arg_type.IsZero()) {
3563 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003564 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003565 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003566 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003567 klass->CannotBeAssignedFromOtherTypes());
3568 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003569 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3570 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003571 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003572 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003573 }
3574 }
3575 /*
3576 * Process the target method's signature. This signature may or may not
3577 * have been verified, so we can't assume it's properly formed.
3578 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003579 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003580 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003581 uint32_t arg[5];
3582 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003583 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003584 }
3585 size_t actual_args = 1;
3586 for (size_t param_index = 0; param_index < params_size; param_index++) {
3587 if (actual_args >= expected_args) {
3588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003589 << "'. Expected " << expected_args
3590 << " arguments, processing argument " << actual_args
3591 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003592 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003593 }
3594 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003595 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003596 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003598 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003599 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003600 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003601 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003602 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003603 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003604 return res_method;
3605 }
3606 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3607 }
3608 if (actual_args != expected_args) {
3609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3610 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003612 } else {
3613 return res_method;
3614 }
3615}
3616
Ian Rogers62342ec2013-06-11 10:26:37 -07003617void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003618 uint32_t type_idx;
3619 if (!is_filled) {
3620 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3621 type_idx = inst->VRegC_22c();
3622 } else if (!is_range) {
3623 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3624 type_idx = inst->VRegB_35c();
3625 } else {
3626 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3627 type_idx = inst->VRegB_3rc();
3628 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003629 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003630 if (res_type.IsConflict()) { // bad class
3631 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003632 } else {
3633 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3634 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003636 } else if (!is_filled) {
3637 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003638 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003639 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003640 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003641 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003642 } else {
3643 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3644 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003645 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003646 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3647 uint32_t arg[5];
3648 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003649 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003650 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003651 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003652 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3654 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003655 return;
3656 }
3657 }
3658 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003659 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003660 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003661 }
3662 }
3663}
3664
Sebastien Hertz5243e912013-05-21 10:55:07 +02003665void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003666 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003667 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003671 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003672 if (array_type.IsZero()) {
3673 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3674 // instruction type. TODO: have a proper notion of bottom here.
3675 if (!is_primitive || insn_type.IsCategory1Types()) {
3676 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003677 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003678 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003679 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003680 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3681 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003682 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003683 }
jeffhaofc3144e2012-02-01 17:21:15 -08003684 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003686 } else {
3687 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003688 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003689 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003691 << " source for aget-object";
3692 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003694 << " source for category 1 aget";
3695 } else if (is_primitive && !insn_type.Equals(component_type) &&
3696 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003697 (insn_type.IsLong() && component_type.IsDouble()))) {
3698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3699 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003700 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003701 // Use knowledge of the field type which is stronger than the type inferred from the
3702 // instruction, which can't differentiate object types and ints from floats, longs from
3703 // doubles.
3704 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003705 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003706 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003707 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003708 component_type.HighHalf(&reg_types_));
3709 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003710 }
3711 }
3712 }
3713}
3714
Ian Rogersd8f69b02014-09-10 21:43:52 +00003715void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003716 const uint32_t vregA) {
3717 // Primitive assignability rules are weaker than regular assignability rules.
3718 bool instruction_compatible;
3719 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003720 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003721 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003722 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003723 value_compatible = value_type.IsIntegralTypes();
3724 } else if (target_type.IsFloat()) {
3725 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3726 value_compatible = value_type.IsFloatTypes();
3727 } else if (target_type.IsLong()) {
3728 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003729 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3730 // as target_type depends on the resolved type of the field.
3731 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003732 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003733 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3734 } else {
3735 value_compatible = false;
3736 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003737 } else if (target_type.IsDouble()) {
3738 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003739 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3740 // as target_type depends on the resolved type of the field.
3741 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003742 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003743 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3744 } else {
3745 value_compatible = false;
3746 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003747 } else {
3748 instruction_compatible = false; // reference with primitive store
3749 value_compatible = false; // unused
3750 }
3751 if (!instruction_compatible) {
3752 // This is a global failure rather than a class change failure as the instructions and
3753 // the descriptors for the type should have been consistent within the same file at
3754 // compile time.
3755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3756 << "' but expected type '" << target_type << "'";
3757 return;
3758 }
3759 if (!value_compatible) {
3760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3761 << " of type " << value_type << " but expected " << target_type << " for put";
3762 return;
3763 }
3764}
3765
Sebastien Hertz5243e912013-05-21 10:55:07 +02003766void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003767 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003768 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003771 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003772 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003773 if (array_type.IsZero()) {
3774 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3775 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003776 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003778 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003779 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003780 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003781 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003782 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003783 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003784 if (!component_type.IsReferenceTypes()) {
3785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3786 << " source for aput-object";
3787 } else {
3788 // The instruction agrees with the type of array, confirm the value to be stored does too
3789 // Note: we use the instruction type (rather than the component type) for aput-object as
3790 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003791 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003792 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 }
3794 }
3795 }
3796}
3797
Mathieu Chartierc7853442015-03-27 14:35:38 -07003798ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003799 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3800 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003801 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003802 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003803 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3804 field_idx, dex_file_->GetFieldName(field_id),
3805 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003806 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003807 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003808 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003809 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003810 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003811 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003812 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3813 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003814 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003815 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003816 << dex_file_->GetFieldName(field_id) << ") in "
3817 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003818 DCHECK(self_->IsExceptionPending());
3819 self_->ClearException();
3820 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003821 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3822 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003824 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003825 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 } else if (!field->IsStatic()) {
3827 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003828 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003829 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003830 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003831}
3832
Mathieu Chartierc7853442015-03-27 14:35:38 -07003833ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003834 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3835 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003836 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003837 if (klass_type.IsConflict()) {
3838 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3839 field_idx, dex_file_->GetFieldName(field_id),
3840 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003841 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003842 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003843 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003844 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003845 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003846 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003847 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3848 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003849 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003850 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003851 << dex_file_->GetFieldName(field_id) << ") in "
3852 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003853 DCHECK(self_->IsExceptionPending());
3854 self_->ClearException();
3855 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003856 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3857 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003859 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003860 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 } else if (field->IsStatic()) {
3862 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3863 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003864 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 } else if (obj_type.IsZero()) {
3866 // Cannot infer and check type, however, access will cause null pointer exception
3867 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003868 } else if (!obj_type.IsReferenceTypes()) {
3869 // Trying to read a field from something that isn't a reference
3870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3871 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003872 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003873 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003874 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003875 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003876 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003877 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003878 if (obj_type.IsUninitializedTypes() &&
3879 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3880 !field_klass.Equals(GetDeclaringClass()))) {
3881 // Field accesses through uninitialized references are only allowable for constructors where
3882 // the field is declared in this class
3883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003884 << " of a not fully initialized object within the context"
3885 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003886 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003887 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3888 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3889 // of C1. For resolution to occur the declared class of the field must be compatible with
3890 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3891 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3892 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003893 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003894 } else {
3895 return field;
3896 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003897 }
3898}
3899
Andreas Gampe896df402014-10-20 22:25:29 -07003900template <MethodVerifier::FieldAccessType kAccType>
3901void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3902 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003903 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003904 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003905 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003906 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003907 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003909 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003910 if (UNLIKELY(have_pending_hard_failure_)) {
3911 return;
3912 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003913 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003914 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003915 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003916 if (kAccType == FieldAccessType::kAccPut) {
3917 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3918 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3919 << " from other class " << GetDeclaringClass();
3920 return;
3921 }
3922 }
3923
Mathieu Chartierc7853442015-03-27 14:35:38 -07003924 mirror::Class* field_type_class =
3925 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003926 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003927 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003928 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003929 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003930 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3931 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003932 }
Ian Rogers0d604842012-04-16 14:50:24 -07003933 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003934 if (field_type == nullptr) {
3935 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3936 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003937 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003938 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003939 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003940 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003941 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3942 "Unexpected third access type");
3943 if (kAccType == FieldAccessType::kAccPut) {
3944 // sput or iput.
3945 if (is_primitive) {
3946 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003947 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003948 if (!insn_type.IsAssignableFrom(*field_type)) {
3949 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3950 << " to be compatible with type '" << insn_type
3951 << "' but found type '" << *field_type
3952 << "' in put-object";
3953 return;
3954 }
3955 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003956 }
Andreas Gampe896df402014-10-20 22:25:29 -07003957 } else if (kAccType == FieldAccessType::kAccGet) {
3958 // sget or iget.
3959 if (is_primitive) {
3960 if (field_type->Equals(insn_type) ||
3961 (field_type->IsFloat() && insn_type.IsInteger()) ||
3962 (field_type->IsDouble() && insn_type.IsLong())) {
3963 // expected that read is of the correct primitive type or that int reads are reading
3964 // floats or long reads are reading doubles
3965 } else {
3966 // This is a global failure rather than a class change failure as the instructions and
3967 // the descriptors for the type should have been consistent within the same file at
3968 // compile time
3969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3970 << " to be of type '" << insn_type
3971 << "' but found type '" << *field_type << "' in get";
3972 return;
3973 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003974 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003975 if (!insn_type.IsAssignableFrom(*field_type)) {
3976 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3977 << " to be compatible with type '" << insn_type
3978 << "' but found type '" << *field_type
3979 << "' in get-object";
3980 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3981 return;
3982 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003983 }
Andreas Gampe896df402014-10-20 22:25:29 -07003984 if (!field_type->IsLowHalf()) {
3985 work_line_->SetRegisterType(this, vregA, *field_type);
3986 } else {
3987 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3988 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003989 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003990 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003991 }
3992}
3993
Mathieu Chartierc7853442015-03-27 14:35:38 -07003994ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003995 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003996 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07003997 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003998 if (!object_type.HasClass()) {
3999 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4000 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004001 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004002 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004003 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004004 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004005 if (f == nullptr) {
4006 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4007 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4008 }
4009 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004010}
4011
Andreas Gampe896df402014-10-20 22:25:29 -07004012template <MethodVerifier::FieldAccessType kAccType>
4013void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4014 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004015 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004016
Mathieu Chartierc7853442015-03-27 14:35:38 -07004017 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004018 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4020 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004021 }
Andreas Gampe896df402014-10-20 22:25:29 -07004022
4023 // For an IPUT_QUICK, we now test for final flag of the field.
4024 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004025 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4026 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004027 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004028 return;
4029 }
4030 }
Andreas Gampe896df402014-10-20 22:25:29 -07004031
4032 // Get the field type.
4033 const RegType* field_type;
4034 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004035 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4036 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004037
4038 if (field_type_class != nullptr) {
4039 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4040 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004041 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004042 Thread* self = Thread::Current();
4043 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4044 self->ClearException();
4045 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4046 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004047 }
Andreas Gampe896df402014-10-20 22:25:29 -07004048 if (field_type == nullptr) {
4049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004050 return;
4051 }
Andreas Gampe896df402014-10-20 22:25:29 -07004052 }
4053
4054 const uint32_t vregA = inst->VRegA_22c();
4055 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4056 "Unexpected third access type");
4057 if (kAccType == FieldAccessType::kAccPut) {
4058 if (is_primitive) {
4059 // Primitive field assignability rules are weaker than regular assignability rules
4060 bool instruction_compatible;
4061 bool value_compatible;
4062 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4063 if (field_type->IsIntegralTypes()) {
4064 instruction_compatible = insn_type.IsIntegralTypes();
4065 value_compatible = value_type.IsIntegralTypes();
4066 } else if (field_type->IsFloat()) {
4067 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4068 value_compatible = value_type.IsFloatTypes();
4069 } else if (field_type->IsLong()) {
4070 instruction_compatible = insn_type.IsLong();
4071 value_compatible = value_type.IsLongTypes();
4072 } else if (field_type->IsDouble()) {
4073 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4074 value_compatible = value_type.IsDoubleTypes();
4075 } else {
4076 instruction_compatible = false; // reference field with primitive store
4077 value_compatible = false; // unused
4078 }
4079 if (!instruction_compatible) {
4080 // This is a global failure rather than a class change failure as the instructions and
4081 // the descriptors for the type should have been consistent within the same file at
4082 // compile time
4083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4084 << " to be of type '" << insn_type
4085 << "' but found type '" << *field_type
4086 << "' in put";
4087 return;
4088 }
4089 if (!value_compatible) {
4090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4091 << " of type " << value_type
4092 << " but expected " << *field_type
4093 << " for store to " << PrettyField(field) << " in put";
4094 return;
4095 }
4096 } else {
4097 if (!insn_type.IsAssignableFrom(*field_type)) {
4098 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4099 << " to be compatible with type '" << insn_type
4100 << "' but found type '" << *field_type
4101 << "' in put-object";
4102 return;
4103 }
4104 work_line_->VerifyRegisterType(this, vregA, *field_type);
4105 }
4106 } else if (kAccType == FieldAccessType::kAccGet) {
4107 if (is_primitive) {
4108 if (field_type->Equals(insn_type) ||
4109 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4110 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4111 // expected that read is of the correct primitive type or that int reads are reading
4112 // floats or long reads are reading doubles
4113 } else {
4114 // This is a global failure rather than a class change failure as the instructions and
4115 // the descriptors for the type should have been consistent within the same file at
4116 // compile time
4117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4118 << " to be of type '" << insn_type
4119 << "' but found type '" << *field_type << "' in Get";
4120 return;
4121 }
4122 } else {
4123 if (!insn_type.IsAssignableFrom(*field_type)) {
4124 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4125 << " to be compatible with type '" << insn_type
4126 << "' but found type '" << *field_type
4127 << "' in get-object";
4128 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4129 return;
4130 }
4131 }
4132 if (!field_type->IsLowHalf()) {
4133 work_line_->SetRegisterType(this, vregA, *field_type);
4134 } else {
4135 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004136 }
4137 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004138 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004139 }
4140}
4141
Ian Rogers776ac1f2012-04-13 23:36:36 -07004142bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004143 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004145 return false;
4146 }
4147 return true;
4148}
4149
Stephen Kyle9bc61992014-09-22 13:53:15 +01004150bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4151 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4152 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4154 return false;
4155 }
4156 return true;
4157}
4158
4159bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4160 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4161}
4162
Ian Rogersebbdd872014-07-07 23:53:08 -07004163bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4164 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004165 bool changed = true;
4166 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4167 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004168 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004169 * We haven't processed this instruction before, and we haven't touched the registers here, so
4170 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4171 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004172 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004173 if (!insn_flags_[next_insn].IsReturn()) {
4174 target_line->CopyFromLine(merge_line);
4175 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004176 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004177 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004178 return false;
4179 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004180 // For returns we only care about the operand to the return, all other registers are dead.
4181 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4182 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4183 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004184 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004185 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004186 } else {
4187 target_line->CopyFromLine(merge_line);
4188 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004189 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004190 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004191 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004192 }
4193 }
4194 }
jeffhaobdb76512011-09-07 11:43:16 -07004195 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004196 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004197 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004198 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004199 if (gDebugVerify) {
4200 copy->CopyFromLine(target_line);
4201 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004202 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004203 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004204 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004205 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004206 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004207 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004208 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004209 << copy->Dump(this) << " MERGE\n"
4210 << merge_line->Dump(this) << " ==\n"
4211 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004212 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004213 if (update_merge_line && changed) {
4214 merge_line->CopyFromLine(target_line);
4215 }
jeffhaobdb76512011-09-07 11:43:16 -07004216 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004217 if (changed) {
4218 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004219 }
4220 return true;
4221}
4222
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004223InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004224 return &insn_flags_[work_insn_idx_];
4225}
4226
Ian Rogersd8f69b02014-09-10 21:43:52 +00004227const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004228 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004229 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004230 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004231 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004232 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4233 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004234 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004235 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004236 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4237 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004238 }
4239 }
4240 if (return_type_ == nullptr) {
4241 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4242 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4243 uint16_t return_type_idx = proto_id.return_type_idx_;
4244 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004245 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004246 }
4247 }
4248 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004249}
4250
Ian Rogersd8f69b02014-09-10 21:43:52 +00004251const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004252 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004253 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004254 const char* descriptor
4255 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004256 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004257 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004258 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4259 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004260 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004261 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004262 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004263 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004264 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004265}
4266
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004267std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4268 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004269 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004270 std::vector<int32_t> result;
4271 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004272 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004273 if (type.IsConstant()) {
4274 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004275 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4276 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004277 } else if (type.IsConstantLo()) {
4278 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004279 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4280 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004281 } else if (type.IsConstantHi()) {
4282 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004283 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4284 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004285 } else if (type.IsIntegralTypes()) {
4286 result.push_back(kIntVReg);
4287 result.push_back(0);
4288 } else if (type.IsFloat()) {
4289 result.push_back(kFloatVReg);
4290 result.push_back(0);
4291 } else if (type.IsLong()) {
4292 result.push_back(kLongLoVReg);
4293 result.push_back(0);
4294 result.push_back(kLongHiVReg);
4295 result.push_back(0);
4296 ++i;
4297 } else if (type.IsDouble()) {
4298 result.push_back(kDoubleLoVReg);
4299 result.push_back(0);
4300 result.push_back(kDoubleHiVReg);
4301 result.push_back(0);
4302 ++i;
4303 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4304 result.push_back(kUndefined);
4305 result.push_back(0);
4306 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004307 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004308 result.push_back(kReferenceVReg);
4309 result.push_back(0);
4310 }
4311 }
4312 return result;
4313}
4314
Ian Rogersd8f69b02014-09-10 21:43:52 +00004315const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004316 if (precise) {
4317 // Precise constant type.
4318 return reg_types_.FromCat1Const(value, true);
4319 } else {
4320 // Imprecise constant type.
4321 if (value < -32768) {
4322 return reg_types_.IntConstant();
4323 } else if (value < -128) {
4324 return reg_types_.ShortConstant();
4325 } else if (value < 0) {
4326 return reg_types_.ByteConstant();
4327 } else if (value == 0) {
4328 return reg_types_.Zero();
4329 } else if (value == 1) {
4330 return reg_types_.One();
4331 } else if (value < 128) {
4332 return reg_types_.PosByteConstant();
4333 } else if (value < 32768) {
4334 return reg_types_.PosShortConstant();
4335 } else if (value < 65536) {
4336 return reg_types_.CharConstant();
4337 } else {
4338 return reg_types_.IntConstant();
4339 }
4340 }
4341}
4342
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004343void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004344 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004345}
4346
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004347void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004348 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004349}
jeffhaod1224c72012-02-29 13:43:08 -08004350
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004351void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4352 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004353}
4354
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004355void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4356 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004357}
4358
Ian Rogersd81871c2011-10-03 13:57:23 -07004359} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004360} // namespace art