blob: 1d0419252814030022e47ad06e4b3b3670b0120b [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "mirror/art_field-inl.h"
34#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class.h"
36#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070037#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070040#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080045#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070050static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070051static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070052// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070053
Ian Rogers7b3ddd22013-02-21 15:19:52 -080054void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070055 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070056 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070057 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070058 register_lines_.reset(new RegisterLine*[insns_size]());
59 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070060 for (uint32_t i = 0; i < insns_size; i++) {
61 bool interesting = false;
62 switch (mode) {
63 case kTrackRegsAll:
64 interesting = flags[i].IsOpcode();
65 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070066 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070067 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070068 break;
69 case kTrackRegsBranches:
70 interesting = flags[i].IsBranchTarget();
71 break;
72 default:
73 break;
74 }
75 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070076 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
77 }
78 }
79}
80
81PcToRegisterLineTable::~PcToRegisterLineTable() {
82 for (size_t i = 0; i < size_; i++) {
83 delete register_lines_[i];
84 if (kIsDebugBuild) {
85 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070086 }
87 }
88}
89
Andreas Gampe7c038102014-10-27 20:08:46 -070090// Note: returns true on failure.
91ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
92 const char* error_msg, uint32_t work_insn_idx) {
93 if (kIsDebugBuild) {
94 // In a debug build, abort if the error condition is wrong.
95 DCHECK(condition) << error_msg << work_insn_idx;
96 } else {
97 // In a non-debug build, just fail the class.
98 if (!condition) {
99 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
100 return true;
101 }
102 }
103
104 return false;
105}
106
Stephen Kyle7e541c92014-12-17 17:10:02 +0000107static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
108 if (verifier->IsConstructor()) {
109 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
110 reg_line->CheckConstructorReturn(verifier);
111 }
112 reg_line->MarkAllRegistersAsConflicts(verifier);
113}
114
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
116 mirror::ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
117 Thread* self = Thread::Current();
118 StackHandleScope<3> hs(self);
119 mirror::Class* klass = method->GetDeclaringClass();
120 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
121 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
122 auto h_method = hs.NewHandle(method);
123 return VerifyMethod(self, method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), h_method,
125 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()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700164}
165
Ian Rogers7b078e82014-09-10 14:44:24 -0700166MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
167 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700168 Handle<mirror::DexCache> dex_cache,
169 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700170 const DexFile::ClassDef* class_def,
171 bool allow_soft_failures,
172 std::string* error) {
173 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700174 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700175 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700178 }
jeffhaof56197c2012-03-05 18:01:54 -0800179 ClassDataItemIterator it(*dex_file, class_data);
180 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
181 it.Next();
182 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700184 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700185 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700186 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800187 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800189 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700190 if (method_idx == previous_direct_method_idx) {
191 // smali can create dex files with two encoded_methods sharing the same method_idx
192 // http://code.google.com/p/smali/issues/detail?id=119
193 it.Next();
194 continue;
195 }
196 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700197 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700198 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700199 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
200 NullHandle<mirror::ArtMethod>(), 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();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700206 StackHandleScope<1> hs(self);
207 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700208 MethodVerifier::FailureKind result = VerifyMethod(self,
209 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700210 dex_file,
211 dex_cache,
212 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700213 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700214 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700215 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700216 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700217 allow_soft_failures,
218 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (result != kNoFailure) {
220 if (result == kHardFailure) {
221 hard_fail = true;
222 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700224 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700225 *error = "Verifier rejected class ";
226 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
227 *error += " due to bad method ";
228 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700230 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800231 }
232 it.Next();
233 }
jeffhao9b0b1882012-10-01 16:51:22 -0700234 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800235 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700236 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800237 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700238 if (method_idx == previous_virtual_method_idx) {
239 // smali can create dex files with two encoded_methods sharing the same method_idx
240 // http://code.google.com/p/smali/issues/detail?id=119
241 it.Next();
242 continue;
243 }
244 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700245 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700246 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700247 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
248 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700249 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700250 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700251 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700252 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700253 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700254 StackHandleScope<1> hs(self);
255 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700256 MethodVerifier::FailureKind result = VerifyMethod(self,
257 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700258 dex_file,
259 dex_cache,
260 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700261 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700262 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700264 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700265 allow_soft_failures,
266 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 if (result != kNoFailure) {
268 if (result == kHardFailure) {
269 hard_fail = true;
270 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error = "Verifier rejected class ";
274 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
275 *error += " due to bad method ";
276 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700277 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700278 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800279 }
280 it.Next();
281 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700282 if (error_count == 0) {
283 return kNoFailure;
284 } else {
285 return hard_fail ? kHardFailure : kSoftFailure;
286 }
jeffhaof56197c2012-03-05 18:01:54 -0800287}
288
Ian Rogers7b078e82014-09-10 14:44:24 -0700289MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700291 Handle<mirror::DexCache> dex_cache,
292 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700293 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700295 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700296 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700297 bool allow_soft_failures,
298 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700299 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700300 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700303 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800304 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700305 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700306 // Verification completed, however failures may be pending that didn't cause the verification
307 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700308 CHECK(!verifier.have_pending_hard_failure_);
309 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700310 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700311 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700312 << PrettyMethod(method_idx, *dex_file) << "\n");
313 }
Ian Rogersc8982582012-09-07 16:53:25 -0700314 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800315 }
316 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700317 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 CHECK_NE(verifier.failures_.size(), 0U);
319 CHECK(verifier.have_pending_hard_failure_);
320 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700321 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800322 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700323 std::cout << "\n" << verifier.info_messages_.str();
324 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800325 }
Ian Rogersc8982582012-09-07 16:53:25 -0700326 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800327 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700328 if (kTimeVerifyMethod) {
329 uint64_t duration_ns = NanoTime() - start_ns;
330 if (duration_ns > MsToNs(100)) {
331 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
332 << " took " << PrettyDuration(duration_ns);
333 }
Ian Rogersc8982582012-09-07 16:53:25 -0700334 }
335 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800336}
337
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700338MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700339 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700340 Handle<mirror::DexCache> dex_cache,
341 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700342 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700344 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800345 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700346 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
347 class_def, code_item, dex_method_idx, method,
348 method_access_flags, true, true, true, true);
349 verifier->Verify();
350 verifier->DumpFailures(os);
351 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700352 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
353 // and querying any info is dangerous/can abort.
354 if (verifier->have_pending_hard_failure_) {
355 delete verifier;
356 return nullptr;
357 } else {
358 verifier->Dump(os);
359 return verifier;
360 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800361}
362
Ian Rogers7b078e82014-09-10 14:44:24 -0700363MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700364 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
365 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700366 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700367 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700368 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700369 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800370 bool need_precise_constants, bool verify_to_dump,
371 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700372 : self_(self),
373 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800374 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800375 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700376 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700377 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700378 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800379 dex_file_(dex_file),
380 dex_cache_(dex_cache),
381 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700382 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800383 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700384 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700385 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700386 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700387 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700388 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800389 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800390 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700391 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200392 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200394 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700395 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800396 verify_to_dump_(verify_to_dump),
397 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartier12d625f2015-03-13 11:33:37 -0700398 self->SetVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700399 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800400}
401
Mathieu Chartier590fee92013-09-13 13:46:47 -0700402MethodVerifier::~MethodVerifier() {
Mathieu Chartier12d625f2015-03-13 11:33:37 -0700403 Thread::Current()->ClearVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700404 STLDeleteElements(&failure_messages_);
405}
406
Brian Carlstromea46f952013-07-30 01:26:50 -0700407void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700408 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700409 Thread* self = Thread::Current();
410 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700411 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
412 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700413 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700414 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700415 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800416 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700417 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700418 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700419 verifier.FindLocksAtDexPc();
420}
421
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700422static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
423 const Instruction* inst = Instruction::At(code_item->insns_);
424
425 uint32_t insns_size = code_item->insns_size_in_code_units_;
426 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
427 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
428 return true;
429 }
430
431 dex_pc += inst->SizeInCodeUnits();
432 inst = inst->Next();
433 }
434
435 return false;
436}
437
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700438void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700439 CHECK(monitor_enter_dex_pcs_ != nullptr);
440 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700442 // Quick check whether there are any monitor_enter instructions at all.
443 if (!HasMonitorEnterInstructions(code_item_)) {
444 return;
445 }
446
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700447 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
448 // verification. In practice, the phase we want relies on data structures set up by all the
449 // earlier passes, so we just run the full method verification and bail out early when we've
450 // got what we wanted.
451 Verify();
452}
453
Brian Carlstromea46f952013-07-30 01:26:50 -0700454mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700455 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700456 Thread* self = Thread::Current();
457 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700458 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
459 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700460 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700461 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700462 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800463 true, 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700467mirror::ArtField* 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700486mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700487 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700488 Thread* self = Thread::Current();
489 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700490 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
491 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700492 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700493 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700494 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800495 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200496 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200497}
498
Brian Carlstromea46f952013-07-30 01:26:50 -0700499mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700500 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200501
502 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
503 // verification. In practice, the phase we want relies on data structures set up by all the
504 // earlier passes, so we just run the full method verification and bail out early when we've
505 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200506 bool success = Verify();
507 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200509 }
510 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700511 if (register_line == nullptr) {
512 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200513 }
514 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
515 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800516 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200517}
518
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700522 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700524 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 } else {
526 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700527 }
jeffhaobdb76512011-09-07 11:43:16 -0700528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
530 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
532 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700534 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800536 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 // Run through the instructions and see if the width checks out.
538 bool result = ComputeWidthsAndCountOps();
539 // Flag instructions guarded by a "try" block and check exception handlers.
540 result = result && ScanTryCatchBlocks();
541 // Perform static instruction verification.
542 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700543 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000544 result = result && VerifyCodeFlow();
545 // Compute information for compiler.
546 if (result && Runtime::Current()->IsCompiler()) {
547 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
548 }
549 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700550}
551
Ian Rogers776ac1f2012-04-13 23:36:36 -0700552std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553 switch (error) {
554 case VERIFY_ERROR_NO_CLASS:
555 case VERIFY_ERROR_NO_FIELD:
556 case VERIFY_ERROR_NO_METHOD:
557 case VERIFY_ERROR_ACCESS_CLASS:
558 case VERIFY_ERROR_ACCESS_FIELD:
559 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700560 case VERIFY_ERROR_INSTANTIATION:
561 case VERIFY_ERROR_CLASS_CHANGE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800562 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700563 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
564 // class change and instantiation errors into soft verification errors so that we re-verify
565 // at runtime. We may fail to find or to agree on access because of not yet available class
566 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
567 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
568 // paths" that dynamically perform the verification and cause the behavior to be that akin
569 // to an interpreter.
570 error = VERIFY_ERROR_BAD_CLASS_SOFT;
571 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700572 // If we fail again at runtime, mark that this instruction would throw and force this
573 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700574 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700575
576 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
577 // try to merge garbage.
578 // Note: this assumes that Fail is called before we do any work_line modifications.
579 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
580 const Instruction* inst = Instruction::At(insns);
581 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
582
583 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
584 saved_line_->CopyFromLine(work_line_.get());
585 }
jeffhaofaf459e2012-08-31 15:32:47 -0700586 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700587 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700588 // Indication that verification should be retried at runtime.
589 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700590 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700591 have_pending_hard_failure_ = true;
592 }
593 break;
jeffhaod5347e02012-03-22 17:25:05 -0700594 // Hard verification failures at compile time will still fail at runtime, so the class is
595 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700596 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800597 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700598 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000599 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800600 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700601 have_pending_hard_failure_ = true;
602 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800603 }
604 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700605 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700606 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700607 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700608 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700609 failure_messages_.push_back(failure_message);
610 return *failure_message;
611}
612
Ian Rogers576ca0c2014-06-06 15:58:22 -0700613std::ostream& MethodVerifier::LogVerifyInfo() {
614 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
615 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
616}
617
Ian Rogersad0b3a32012-04-16 14:50:24 -0700618void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
619 size_t failure_num = failure_messages_.size();
620 DCHECK_NE(failure_num, 0U);
621 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
622 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700623 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700624 delete last_fail_message;
625}
626
627void MethodVerifier::AppendToLastFailMessage(std::string append) {
628 size_t failure_num = failure_messages_.size();
629 DCHECK_NE(failure_num, 0U);
630 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
631 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800632}
633
Ian Rogers776ac1f2012-04-13 23:36:36 -0700634bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 const uint16_t* insns = code_item_->insns_;
636 size_t insns_size = code_item_->insns_size_in_code_units_;
637 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700638 size_t new_instance_count = 0;
639 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700641
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700643 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700644 switch (opcode) {
645 case Instruction::APUT_OBJECT:
646 case Instruction::CHECK_CAST:
647 has_check_casts_ = true;
648 break;
649 case Instruction::INVOKE_VIRTUAL:
650 case Instruction::INVOKE_VIRTUAL_RANGE:
651 case Instruction::INVOKE_INTERFACE:
652 case Instruction::INVOKE_INTERFACE_RANGE:
653 has_virtual_or_interface_invokes_ = true;
654 break;
655 case Instruction::MONITOR_ENTER:
656 monitor_enter_count++;
657 break;
658 case Instruction::NEW_INSTANCE:
659 new_instance_count++;
660 break;
661 default:
662 break;
jeffhaobdb76512011-09-07 11:43:16 -0700663 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700665 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700667 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700668 }
669
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
672 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700673 return false;
674 }
675
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 new_instance_count_ = new_instance_count;
677 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700678 return true;
679}
680
Ian Rogers776ac1f2012-04-13 23:36:36 -0700681bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700683 if (tries_size == 0) {
684 return true;
685 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700687 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700688
689 for (uint32_t idx = 0; idx < tries_size; idx++) {
690 const DexFile::TryItem* try_item = &tries[idx];
691 uint32_t start = try_item->start_addr_;
692 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700693 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
695 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700696 return false;
697 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
700 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700701 return false;
702 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700703 uint32_t dex_pc = start;
704 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
705 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700707 size_t insn_size = inst->SizeInCodeUnits();
708 dex_pc += insn_size;
709 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700710 }
711 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800712 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700713 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700714 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700715 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700716 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700717 CatchHandlerIterator iterator(handlers_ptr);
718 for (; iterator.HasNext(); iterator.Next()) {
719 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700721 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
722 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700723 return false;
724 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100725 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
726 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
727 << "exception handler begins with move-result* (" << dex_pc << ")";
728 return false;
729 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700731 // Ensure exception types are resolved so that they don't need resolution to be delivered,
732 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700733 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800734 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
735 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700736 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700737 if (exception_type == nullptr) {
738 DCHECK(self_->IsExceptionPending());
739 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700740 }
741 }
jeffhaobdb76512011-09-07 11:43:16 -0700742 }
Ian Rogers0571d352011-11-03 19:51:38 -0700743 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700744 }
jeffhaobdb76512011-09-07 11:43:16 -0700745 return true;
746}
747
Ian Rogers776ac1f2012-04-13 23:36:36 -0700748bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700750
Ian Rogers0c7abda2012-09-19 13:33:42 -0700751 /* 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 -0700752 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700753 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700754
755 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700756 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700758 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 return false;
760 }
761 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700762 // All invoke points are marked as "Throw" points already.
763 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000764 if (inst->IsBranch()) {
765 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
766 // The compiler also needs safepoints for fall-through to loop heads.
767 // Such a loop head must be a target of a branch.
768 int32_t offset = 0;
769 bool cond, self_ok;
770 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
771 DCHECK(target_ok);
772 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
773 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700774 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000775 } else if (inst->IsReturn()) {
776 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 }
778 dex_pc += inst->SizeInCodeUnits();
779 inst = inst->Next();
780 }
781 return true;
782}
783
Ian Rogers776ac1f2012-04-13 23:36:36 -0700784bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 bool result = true;
786 switch (inst->GetVerifyTypeArgumentA()) {
787 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700788 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 break;
790 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700791 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 break;
793 }
794 switch (inst->GetVerifyTypeArgumentB()) {
795 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700796 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 break;
798 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700799 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 break;
801 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700802 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 break;
804 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700805 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 break;
807 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700808 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 break;
810 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700811 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 break;
813 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700814 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 break;
816 }
817 switch (inst->GetVerifyTypeArgumentC()) {
818 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700819 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 break;
821 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700822 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 break;
824 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700825 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 break;
827 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700828 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 break;
830 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700831 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 break;
833 }
834 switch (inst->GetVerifyExtraFlags()) {
835 case Instruction::kVerifyArrayData:
836 result = result && CheckArrayData(code_offset);
837 break;
838 case Instruction::kVerifyBranchTarget:
839 result = result && CheckBranchTarget(code_offset);
840 break;
841 case Instruction::kVerifySwitchTargets:
842 result = result && CheckSwitchTargets(code_offset);
843 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700844 case Instruction::kVerifyVarArgNonZero:
845 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700846 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700847 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
849 "non-range invoke";
850 return false;
851 }
Ian Rogers29a26482014-05-02 15:27:29 -0700852 uint32_t args[Instruction::kMaxVarArgRegs];
853 inst->GetVarArgs(args);
854 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700856 }
Andreas Gampec3314312014-06-19 18:13:29 -0700857 case Instruction::kVerifyVarArgRangeNonZero:
858 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700860 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
861 inst->VRegA() <= 0) {
862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
863 "range invoke";
864 return false;
865 }
Ian Rogers29a26482014-05-02 15:27:29 -0700866 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 break;
868 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 result = false;
871 break;
872 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800873 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
875 result = false;
876 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return result;
878}
879
Ian Rogers7b078e82014-09-10 14:44:24 -0700880inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
883 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 return false;
885 }
886 return true;
887}
888
Ian Rogers7b078e82014-09-10 14:44:24 -0700889inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
892 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 return false;
894 }
895 return true;
896}
897
Ian Rogers7b078e82014-09-10 14:44:24 -0700898inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
901 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 return false;
903 }
904 return true;
905}
906
Ian Rogers7b078e82014-09-10 14:44:24 -0700907inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
910 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 return false;
912 }
913 return true;
914}
915
Ian Rogers7b078e82014-09-10 14:44:24 -0700916inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
919 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 return false;
921 }
922 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700923 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 return true;
929}
930
Ian Rogers7b078e82014-09-10 14:44:24 -0700931inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
934 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 return false;
936 }
937 return true;
938}
939
Ian Rogers7b078e82014-09-10 14:44:24 -0700940inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
943 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
945 }
946 return true;
947}
948
Ian Rogers776ac1f2012-04-13 23:36:36 -0700949bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
952 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 return false;
954 }
955 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700956 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 const char* cp = descriptor;
958 while (*cp++ == '[') {
959 bracket_count++;
960 }
961 if (bracket_count == 0) {
962 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700963 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
964 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 } else if (bracket_count > 255) {
967 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700968 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
969 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 return false;
971 }
972 return true;
973}
974
Ian Rogers776ac1f2012-04-13 23:36:36 -0700975bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
977 const uint16_t* insns = code_item_->insns_ + cur_offset;
978 const uint16_t* array_data;
979 int32_t array_data_offset;
980
981 DCHECK_LT(cur_offset, insn_count);
982 /* make sure the start of the array data table is in range */
983 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
984 if ((int32_t) cur_offset + array_data_offset < 0 ||
985 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700987 << ", data offset " << array_data_offset
988 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 /* offset to array data table is a relative branch-style offset */
992 array_data = insns + array_data_offset;
993 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800994 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
996 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 return false;
998 }
999 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001000 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1002 /* make sure the end of the switch is in range */
1003 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1005 << ", data offset " << array_data_offset << ", end "
1006 << cur_offset + array_data_offset + table_size
1007 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 return false;
1009 }
1010 return true;
1011}
1012
Ian Rogers776ac1f2012-04-13 23:36:36 -07001013bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 int32_t offset;
1015 bool isConditional, selfOkay;
1016 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1017 return false;
1018 }
1019 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1021 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001024 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1025 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1028 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 return false;
1030 }
1031 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1032 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001033 if (abs_offset < 0 ||
1034 (uint32_t) abs_offset >= insn_count ||
1035 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001037 << reinterpret_cast<void*>(abs_offset) << ") at "
1038 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 return false;
1040 }
1041 insn_flags_[abs_offset].SetBranchTarget();
1042 return true;
1043}
1044
Ian Rogers776ac1f2012-04-13 23:36:36 -07001045bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 bool* selfOkay) {
1047 const uint16_t* insns = code_item_->insns_ + cur_offset;
1048 *pConditional = false;
1049 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001050 switch (*insns & 0xff) {
1051 case Instruction::GOTO:
1052 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001053 break;
1054 case Instruction::GOTO_32:
1055 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001056 *selfOkay = true;
1057 break;
1058 case Instruction::GOTO_16:
1059 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001060 break;
1061 case Instruction::IF_EQ:
1062 case Instruction::IF_NE:
1063 case Instruction::IF_LT:
1064 case Instruction::IF_GE:
1065 case Instruction::IF_GT:
1066 case Instruction::IF_LE:
1067 case Instruction::IF_EQZ:
1068 case Instruction::IF_NEZ:
1069 case Instruction::IF_LTZ:
1070 case Instruction::IF_GEZ:
1071 case Instruction::IF_GTZ:
1072 case Instruction::IF_LEZ:
1073 *pOffset = (int16_t) insns[1];
1074 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001075 break;
1076 default:
1077 return false;
1078 break;
1079 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001080 return true;
1081}
1082
Ian Rogers776ac1f2012-04-13 23:36:36 -07001083bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001085 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001087 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001089 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001091 << ", switch offset " << switch_offset
1092 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001093 return false;
1094 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001095 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001097 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001098 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1100 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001101 return false;
1102 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 uint32_t switch_count = switch_insns[1];
1104 int32_t keys_offset, targets_offset;
1105 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001106 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1107 /* 0=sig, 1=count, 2/3=firstKey */
1108 targets_offset = 4;
1109 keys_offset = -1;
1110 expected_signature = Instruction::kPackedSwitchSignature;
1111 } else {
1112 /* 0=sig, 1=count, 2..count*2 = keys */
1113 keys_offset = 2;
1114 targets_offset = 2 + 2 * switch_count;
1115 expected_signature = Instruction::kSparseSwitchSignature;
1116 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001118 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001119 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1120 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1121 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001122 return false;
1123 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001124 /* make sure the end of the switch is in range */
1125 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1127 << ", switch offset " << switch_offset
1128 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001129 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 return false;
1131 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 /* for a sparse switch, verify the keys are in ascending order */
1133 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1135 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1137 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1138 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1140 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001141 return false;
1142 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001143 last_key = key;
1144 }
1145 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001146 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 for (uint32_t targ = 0; targ < switch_count; targ++) {
1148 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1149 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1150 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001151 if (abs_offset < 0 ||
1152 abs_offset >= (int32_t) insn_count ||
1153 !insn_flags_[abs_offset].IsOpcode()) {
1154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1155 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1156 << reinterpret_cast<void*>(cur_offset)
1157 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 return false;
1159 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 insn_flags_[abs_offset].SetBranchTarget();
1161 }
1162 return true;
1163}
1164
Ian Rogers776ac1f2012-04-13 23:36:36 -07001165bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001166 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 return false;
1169 }
1170 uint16_t registers_size = code_item_->registers_size_;
1171 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001172 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1174 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 return false;
1176 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001177 }
1178
1179 return true;
1180}
1181
Ian Rogers776ac1f2012-04-13 23:36:36 -07001182bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 uint16_t registers_size = code_item_->registers_size_;
1184 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1185 // integer overflow when adding them here.
1186 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1188 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001189 return false;
1190 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001191 return true;
1192}
1193
Ian Rogers776ac1f2012-04-13 23:36:36 -07001194bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 uint16_t registers_size = code_item_->registers_size_;
1196 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001197
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001199 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1200 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 }
1202 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001203 reg_table_.Init(kTrackCompilerInterestPoints,
1204 insn_flags_.get(),
1205 insns_size,
1206 registers_size,
1207 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001208
jeffhaobdb76512011-09-07 11:43:16 -07001209
Ian Rogersd0fbd852013-09-24 18:17:04 -07001210 work_line_.reset(RegisterLine::Create(registers_size, this));
1211 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001212
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 /* Initialize register types of method arguments. */
1214 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001215 DCHECK_NE(failures_.size(), 0U);
1216 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001217 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001218 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 return false;
1220 }
1221 /* Perform code flow verification. */
1222 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001223 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001225 }
jeffhaobdb76512011-09-07 11:43:16 -07001226 return true;
1227}
1228
Ian Rogersad0b3a32012-04-16 14:50:24 -07001229std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1230 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001231 for (size_t i = 0; i < failures_.size(); ++i) {
1232 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001233 }
1234 return os;
1235}
1236
Ian Rogers776ac1f2012-04-13 23:36:36 -07001237void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001238 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001239 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 return;
jeffhaobdb76512011-09-07 11:43:16 -07001241 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001242 {
1243 os << "Register Types:\n";
1244 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1245 std::ostream indent_os(&indent_filter);
1246 reg_types_.Dump(indent_os);
1247 }
Ian Rogersb4903572012-10-11 11:52:56 -07001248 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001249 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1250 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 const Instruction* inst = Instruction::At(code_item_->insns_);
1252 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001253 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001255 if (reg_line != nullptr) {
1256 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001257 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001258 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 const bool kDumpHexOfInstruction = false;
1260 if (kDumpHexOfInstruction) {
1261 indent_os << inst->DumpHex(5) << " ";
1262 }
1263 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001264 inst = inst->Next();
1265 }
jeffhaobdb76512011-09-07 11:43:16 -07001266}
1267
Ian Rogersd81871c2011-10-03 13:57:23 -07001268static bool IsPrimitiveDescriptor(char descriptor) {
1269 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001270 case 'I':
1271 case 'C':
1272 case 'S':
1273 case 'B':
1274 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001275 case 'F':
1276 case 'D':
1277 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001279 default:
1280 return false;
1281 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001282}
1283
Ian Rogers776ac1f2012-04-13 23:36:36 -07001284bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 RegisterLine* reg_line = reg_table_.GetLine(0);
1286 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1287 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001288
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001290 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001292 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1294 // argument as uninitialized. This restricts field access until the superclass constructor is
1295 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001296 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001297 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001298 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 reg_types_.UninitializedThisArgument(declaring_class));
1300 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001301 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001302 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001304 }
1305
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001306 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001307 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001308 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001309
1310 for (; iterator.HasNext(); iterator.Next()) {
1311 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001312 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 LOG(FATAL) << "Null descriptor";
1314 }
1315 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1317 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 return false;
1319 }
1320 switch (descriptor[0]) {
1321 case 'L':
1322 case '[':
1323 // We assume that reference arguments are initialized. The only way it could be otherwise
1324 // (assuming the caller was verified) is if the current method is <init>, but in that case
1325 // it's effectively considered initialized the instant we reach here (in the sense that we
1326 // can return without doing anything or call virtual methods).
1327 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001328 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001329 if (!reg_type.IsNonZeroReferenceTypes()) {
1330 DCHECK(HasFailures());
1331 return false;
1332 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001333 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 }
1335 break;
1336 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001337 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 break;
1339 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001340 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 break;
1342 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001343 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 break;
1345 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001346 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 break;
1348 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001349 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 break;
1351 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001352 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 break;
1354 case 'J':
1355 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001356 if (cur_arg + 1 >= expected_args) {
1357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1358 << " args, found more (" << descriptor << ")";
1359 return false;
1360 }
1361
Ian Rogers7b078e82014-09-10 14:44:24 -07001362 const RegType* lo_half;
1363 const RegType* hi_half;
1364 if (descriptor[0] == 'J') {
1365 lo_half = &reg_types_.LongLo();
1366 hi_half = &reg_types_.LongHi();
1367 } else {
1368 lo_half = &reg_types_.DoubleLo();
1369 hi_half = &reg_types_.DoubleHi();
1370 }
1371 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 cur_arg++;
1373 break;
1374 }
1375 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1377 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 return false;
1379 }
1380 cur_arg++;
1381 }
1382 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1384 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 return false;
1386 }
1387 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1388 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1389 // format. Only major difference from the method argument format is that 'V' is supported.
1390 bool result;
1391 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1392 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001393 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 size_t i = 0;
1395 do {
1396 i++;
1397 } while (descriptor[i] == '['); // process leading [
1398 if (descriptor[i] == 'L') { // object array
1399 do {
1400 i++; // find closing ;
1401 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1402 result = descriptor[i] == ';';
1403 } else { // primitive array
1404 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1405 }
1406 } else if (descriptor[0] == 'L') {
1407 // could be more thorough here, but shouldn't be required
1408 size_t i = 0;
1409 do {
1410 i++;
1411 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1412 result = descriptor[i] == ';';
1413 } else {
1414 result = false;
1415 }
1416 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1418 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 }
1420 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001421}
1422
Ian Rogers776ac1f2012-04-13 23:36:36 -07001423bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 const uint16_t* insns = code_item_->insns_;
1425 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001426
jeffhaobdb76512011-09-07 11:43:16 -07001427 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 insn_flags_[0].SetChanged();
1429 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001430
jeffhaobdb76512011-09-07 11:43:16 -07001431 /* Continue until no instructions are marked "changed". */
1432 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001433 if (allow_thread_suspension_) {
1434 self_->AllowThreadSuspension();
1435 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1437 uint32_t insn_idx = start_guess;
1438 for (; insn_idx < insns_size; insn_idx++) {
1439 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001440 break;
1441 }
jeffhaobdb76512011-09-07 11:43:16 -07001442 if (insn_idx == insns_size) {
1443 if (start_guess != 0) {
1444 /* try again, starting from the top */
1445 start_guess = 0;
1446 continue;
1447 } else {
1448 /* all flags are clear */
1449 break;
1450 }
1451 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 // We carry the working set of registers from instruction to instruction. If this address can
1453 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1454 // "changed" flags, we need to load the set of registers from the table.
1455 // Because we always prefer to continue on to the next instruction, we should never have a
1456 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1457 // target.
1458 work_insn_idx_ = insn_idx;
1459 if (insn_flags_[insn_idx].IsBranchTarget()) {
1460 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001461 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001462 /*
1463 * Sanity check: retrieve the stored register line (assuming
1464 * a full table) and make sure it actually matches.
1465 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001467 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 if (work_line_->CompareLine(register_line) != 0) {
1469 Dump(std::cout);
1470 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001472 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001473 << " work_line=" << work_line_->Dump(this) << "\n"
1474 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 }
jeffhaobdb76512011-09-07 11:43:16 -07001476 }
jeffhaobdb76512011-09-07 11:43:16 -07001477 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001480 prepend += " failed to verify: ";
1481 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 return false;
1483 }
jeffhaobdb76512011-09-07 11:43:16 -07001484 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 insn_flags_[insn_idx].SetVisited();
1486 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001487 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001488
Ian Rogers1c849e52012-06-28 14:00:33 -07001489 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001490 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001491 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001492 * (besides the wasted space), but it indicates a flaw somewhere
1493 * down the line, possibly in the verifier.
1494 *
1495 * If we've substituted "always throw" instructions into the stream,
1496 * we are almost certainly going to have some dead code.
1497 */
1498 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001500 for (; insn_idx < insns_size;
1501 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001502 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001503 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001504 * may or may not be preceded by a padding NOP (for alignment).
1505 */
1506 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1507 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1508 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001509 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001510 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1511 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1512 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001514 }
1515
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001517 if (dead_start < 0)
1518 dead_start = insn_idx;
1519 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001520 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1521 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001522 dead_start = -1;
1523 }
1524 }
1525 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001526 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1527 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001528 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001529 // To dump the state of the verify after a method, do something like:
1530 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1531 // "boolean java.lang.String.equals(java.lang.Object)") {
1532 // LOG(INFO) << info_messages_.str();
1533 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001534 }
jeffhaobdb76512011-09-07 11:43:16 -07001535 return true;
1536}
1537
Ian Rogers776ac1f2012-04-13 23:36:36 -07001538bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001539 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1540 // We want the state _before_ the instruction, for the case where the dex pc we're
1541 // interested in is itself a monitor-enter instruction (which is a likely place
1542 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001543 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001544 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001545 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1546 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1547 }
1548 }
1549
jeffhaobdb76512011-09-07 11:43:16 -07001550 /*
1551 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001552 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001553 * control to another statement:
1554 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001555 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001556 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001557 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001558 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001559 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001560 * throw an exception that is handled by an encompassing "try"
1561 * block.
1562 *
1563 * We can also return, in which case there is no successor instruction
1564 * from this point.
1565 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001566 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001567 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1569 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001570 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001571
jeffhaobdb76512011-09-07 11:43:16 -07001572 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001573 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001574 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001576 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001577 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 }
jeffhaobdb76512011-09-07 11:43:16 -07001579
1580 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001581 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001582 * can throw an exception, we will copy/merge this into the "catch"
1583 * address rather than work_line, because we don't want the result
1584 * from the "successful" code path (e.g. a check-cast that "improves"
1585 * a type) to be visible to the exception handler.
1586 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001587 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001589 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001591 }
1592
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001593
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001594 // We need to ensure the work line is consistent while performing validation. When we spot a
1595 // peephole pattern we compute a new line for either the fallthrough instruction or the
1596 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001597 std::unique_ptr<RegisterLine> branch_line;
1598 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001599
Stephen Kyle7e541c92014-12-17 17:10:02 +00001600 /*
1601 * If we are in a constructor, and we currently have an UninitializedThis type
1602 * in a register somewhere, we need to make sure it isn't overwritten.
1603 */
1604 bool track_uninitialized_this = false;
1605 size_t uninitialized_this_loc = 0;
1606 if (IsConstructor()) {
1607 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1608 }
1609
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001611 case Instruction::NOP:
1612 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001613 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001614 * a signature that looks like a NOP; if we see one of these in
1615 * the course of executing code then we have a problem.
1616 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001619 }
1620 break;
1621
1622 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001623 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 break;
jeffhaobdb76512011-09-07 11:43:16 -07001625 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001626 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 break;
jeffhaobdb76512011-09-07 11:43:16 -07001628 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001629 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001630 break;
1631 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001632 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 break;
jeffhaobdb76512011-09-07 11:43:16 -07001634 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001635 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 break;
jeffhaobdb76512011-09-07 11:43:16 -07001637 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001638 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001639 break;
1640 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001641 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 break;
jeffhaobdb76512011-09-07 11:43:16 -07001643 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001644 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 break;
jeffhaobdb76512011-09-07 11:43:16 -07001646 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001647 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001648 break;
1649
1650 /*
1651 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001652 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001653 * might want to hold the result in an actual CPU register, so the
1654 * Dalvik spec requires that these only appear immediately after an
1655 * invoke or filled-new-array.
1656 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001657 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001658 * redundant with the reset done below, but it can make the debug info
1659 * easier to read in some cases.)
1660 */
1661 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001662 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001663 break;
1664 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001665 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001666 break;
1667 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001668 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001669 break;
1670
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001672 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1673 // where one entrypoint to the catch block is not actually an exception path.
1674 if (work_insn_idx_ == 0) {
1675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1676 break;
1677 }
jeffhaobdb76512011-09-07 11:43:16 -07001678 /*
jeffhao60f83e32012-02-13 17:16:30 -08001679 * This statement can only appear as the first instruction in an exception handler. We verify
1680 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001681 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001682 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001684 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 }
jeffhaobdb76512011-09-07 11:43:16 -07001686 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001687 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001688 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 }
jeffhaobdb76512011-09-07 11:43:16 -07001691 }
1692 break;
1693 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001694 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001695 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001696 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1699 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 } else {
1701 // Compilers may generate synthetic functions that write byte values into boolean fields.
1702 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001704 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1706 ((return_type.IsBoolean() || return_type.IsByte() ||
1707 return_type.IsShort() || return_type.IsChar()) &&
1708 src_type.IsInteger()));
1709 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001710 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001711 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001712 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 }
jeffhaobdb76512011-09-07 11:43:16 -07001715 }
1716 }
1717 break;
1718 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001719 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001720 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001721 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 } else {
1725 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001727 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001728 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 }
jeffhaobdb76512011-09-07 11:43:16 -07001731 }
1732 }
1733 break;
1734 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001736 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 } else {
1740 /* return_type is the *expected* return type, not register value */
1741 DCHECK(!return_type.IsZero());
1742 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001745 // Disallow returning uninitialized values and verify that the reference in vAA is an
1746 // instance of the "return_type"
1747 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001748 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1749 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001750 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001751 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1752 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1753 << "' or '" << reg_type << "'";
1754 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001755 bool soft_error = false;
1756 // Check whether arrays are involved. They will show a valid class status, even
1757 // if their components are erroneous.
1758 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1759 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1760 if (soft_error) {
1761 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1762 << reg_type << " vs " << return_type;
1763 }
1764 }
1765
1766 if (!soft_error) {
1767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1768 << "', but expected from declaration '" << return_type << "'";
1769 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001770 }
jeffhaobdb76512011-09-07 11:43:16 -07001771 }
1772 }
1773 }
1774 break;
1775
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001776 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 case Instruction::CONST_4: {
1778 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001779 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001780 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001781 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001782 }
1783 case Instruction::CONST_16: {
1784 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001785 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001786 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001787 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001789 case Instruction::CONST: {
1790 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001791 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001792 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001794 }
1795 case Instruction::CONST_HIGH16: {
1796 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001797 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001798 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001799 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001800 }
jeffhaobdb76512011-09-07 11:43:16 -07001801 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001803 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001804 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1805 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001806 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001808 }
1809 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001811 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1812 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001813 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001814 break;
1815 }
1816 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001818 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1819 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001820 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001821 break;
1822 }
1823 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001824 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001825 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1826 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001827 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 break;
1829 }
jeffhaobdb76512011-09-07 11:43:16 -07001830 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001831 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001832 break;
jeffhaobdb76512011-09-07 11:43:16 -07001833 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001834 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001835 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001837 // Get type from instruction if unresolved then we need an access check
1838 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001839 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001840 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001841 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001842 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001843 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 }
jeffhaobdb76512011-09-07 11:43:16 -07001846 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001847 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
1849 case Instruction::MONITOR_EXIT:
1850 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001851 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001852 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001853 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001854 * to the need to handle asynchronous exceptions, a now-deprecated
1855 * feature that Dalvik doesn't support.)
1856 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001857 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001858 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001859 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001860 * structured locking checks are working, the former would have
1861 * failed on the -enter instruction, and the latter is impossible.
1862 *
1863 * This is fortunate, because issue 3221411 prevents us from
1864 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001865 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001866 * some catch blocks (which will show up as "dead" code when
1867 * we skip them here); if we can't, then the code path could be
1868 * "live" so we still need to check it.
1869 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001870 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001871 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001872 break;
1873
Ian Rogers28ad40d2011-10-27 15:19:26 -07001874 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001876 /*
1877 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1878 * could be a "upcast" -- not expected, so we don't try to address it.)
1879 *
1880 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001881 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001882 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001883 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1884 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001885 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001886 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001887 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001888 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001889 if (klass != nullptr && klass->IsPrimitive()) {
1890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1891 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1892 << GetDeclaringClass();
1893 break;
1894 }
1895
Ian Rogersad0b3a32012-04-16 14:50:24 -07001896 DCHECK_NE(failures_.size(), 0U);
1897 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001899 }
1900 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001901 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001902 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001904 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001905 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001906 if (is_checkcast) {
1907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1908 } else {
1909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1910 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001911 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 if (is_checkcast) {
1913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1914 } else {
1915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1916 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001917 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001918 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001921 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001922 }
jeffhaobdb76512011-09-07 11:43:16 -07001923 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001924 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 }
1926 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001928 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001929 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001932 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001934 } else {
1935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 }
1937 break;
1938 }
1939 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001940 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001941 if (res_type.IsConflict()) {
1942 DCHECK_NE(failures_.size(), 0U);
1943 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001944 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001945 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1946 // can't create an instance of an interface or abstract class */
1947 if (!res_type.IsInstantiableTypes()) {
1948 Fail(VERIFY_ERROR_INSTANTIATION)
1949 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001950 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001952 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001953 // Any registers holding previous allocations from this address that have not yet been
1954 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001955 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001956 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001957 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 break;
1959 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001960 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001961 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001962 break;
1963 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001964 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001965 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001966 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001967 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001969 just_set_result = true; // Filled new array range sets result register
1970 break;
jeffhaobdb76512011-09-07 11:43:16 -07001971 case Instruction::CMPL_FLOAT:
1972 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001973 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001974 break;
1975 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001976 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001977 break;
1978 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001979 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001980 break;
1981 case Instruction::CMPL_DOUBLE:
1982 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001983 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001984 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001985 break;
1986 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001987 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001988 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001989 break;
1990 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001991 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001992 break;
1993 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001994 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001995 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001996 break;
1997 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001998 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001999 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002000 break;
2001 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002002 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002003 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002006 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002007 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2008 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002009 }
2010 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 }
jeffhaobdb76512011-09-07 11:43:16 -07002012 case Instruction::GOTO:
2013 case Instruction::GOTO_16:
2014 case Instruction::GOTO_32:
2015 /* no effect on or use of registers */
2016 break;
2017
2018 case Instruction::PACKED_SWITCH:
2019 case Instruction::SPARSE_SWITCH:
2020 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002021 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002022 break;
2023
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 case Instruction::FILL_ARRAY_DATA: {
2025 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002026 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002027 /* array_type can be null if the reg type is Zero */
2028 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002029 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2031 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002032 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002033 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002034 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002035 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2037 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 } else {
jeffhao457cc512012-02-02 16:55:13 -08002039 // Now verify if the element width in the table matches the element width declared in
2040 // the array
2041 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2042 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002044 } else {
2045 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2046 // Since we don't compress the data in Dex, expect to see equal width of data stored
2047 // in the table and expected from the array class.
2048 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2050 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002051 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 }
2053 }
jeffhaobdb76512011-09-07 11:43:16 -07002054 }
2055 }
2056 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 }
jeffhaobdb76512011-09-07 11:43:16 -07002058 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2061 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 bool mismatch = false;
2063 if (reg_type1.IsZero()) { // zero then integral or reference expected
2064 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2065 } else if (reg_type1.IsReferenceTypes()) { // both references?
2066 mismatch = !reg_type2.IsReferenceTypes();
2067 } else { // both integral?
2068 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2069 }
2070 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2072 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002073 }
2074 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 }
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::IF_LT:
2077 case Instruction::IF_GE:
2078 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2081 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2084 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002085 }
2086 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 }
jeffhaobdb76512011-09-07 11:43:16 -07002088 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002090 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2093 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002095
2096 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002097 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002098 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002099 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002100 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002101 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002102 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002103 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2104 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2105 work_insn_idx_)) {
2106 break;
2107 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002108 } else {
2109 break;
2110 }
2111
Ian Rogers9b360392013-06-06 14:45:07 -07002112 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002113
2114 /* Check for peep-hole pattern of:
2115 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002116 * instance-of vX, vY, T;
2117 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002118 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002119 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002120 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002121 * and sharpen the type of vY to be type T.
2122 * Note, this pattern can't be if:
2123 * - if there are other branches to this branch,
2124 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002125 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002126 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002127 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2128 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2129 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002130 // Check the type of the instance-of is different than that of registers type, as if they
2131 // are the same there is no work to be done here. Check that the conversion is not to or
2132 // from an unresolved type as type information is imprecise. If the instance-of is to an
2133 // interface then ignore the type information as interfaces can only be treated as Objects
2134 // and we don't want to disallow field and other operations on the object. If the value
2135 // being instance-of checked against is known null (zero) then allow the optimization as
2136 // we didn't have type information. If the merge of the instance-of type with the original
2137 // type is assignable to the original then allow optimization. This check is performed to
2138 // ensure that subsequent merges don't lose type information - such as becoming an
2139 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002140 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002141 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002142
Ian Rogersebbdd872014-07-07 23:53:08 -07002143 if (!orig_type.Equals(cast_type) &&
2144 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002145 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002146 !cast_type.GetClass()->IsInterface() &&
2147 (orig_type.IsZero() ||
2148 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002149 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002150 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002151 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002152 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002153 branch_line.reset(update_line);
2154 }
2155 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002156 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002157 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2158 // See if instance-of was preceded by a move-object operation, common due to the small
2159 // register encoding space of instance-of, and propagate type information to the source
2160 // of the move-object.
2161 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002162 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002163 move_idx--;
2164 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002165 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2166 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2167 work_insn_idx_)) {
2168 break;
2169 }
Ian Rogers9b360392013-06-06 14:45:07 -07002170 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2171 switch (move_inst->Opcode()) {
2172 case Instruction::MOVE_OBJECT:
2173 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002174 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002175 }
2176 break;
2177 case Instruction::MOVE_OBJECT_FROM16:
2178 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002179 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002180 }
2181 break;
2182 case Instruction::MOVE_OBJECT_16:
2183 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002184 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002185 }
2186 break;
2187 default:
2188 break;
2189 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002190 }
2191 }
2192 }
2193
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 }
jeffhaobdb76512011-09-07 11:43:16 -07002196 case Instruction::IF_LTZ:
2197 case Instruction::IF_GEZ:
2198 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002200 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2203 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 }
jeffhaobdb76512011-09-07 11:43:16 -07002205 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 }
jeffhaobdb76512011-09-07 11:43:16 -07002207 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002208 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 break;
jeffhaobdb76512011-09-07 11:43:16 -07002210 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002211 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 break;
jeffhaobdb76512011-09-07 11:43:16 -07002213 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002214 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 break;
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 break;
jeffhaobdb76512011-09-07 11:43:16 -07002222 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 break;
2225 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002226 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
2228
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 break;
2232 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002233 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 break;
2235 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002236 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 break;
2238 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002240 break;
2241 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002243 break;
2244 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002245 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
2247 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250
jeffhaobdb76512011-09-07 11:43:16 -07002251 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002252 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 break;
jeffhaobdb76512011-09-07 11:43:16 -07002254 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002255 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 break;
jeffhaobdb76512011-09-07 11:43:16 -07002257 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002258 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 break;
jeffhaobdb76512011-09-07 11:43:16 -07002260 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002261 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 break;
2263 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002264 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002265 break;
2266 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002267 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
2269 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002270 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2271 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002272 break;
jeffhaobdb76512011-09-07 11:43:16 -07002273
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002275 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
2277 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002278 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 break;
2280 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002281 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 break;
2283 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002284 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002287 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002290 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 break;
jeffhaobdb76512011-09-07 11:43:16 -07002292 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002293 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2294 false);
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
2296
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002298 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 break;
jeffhaobdb76512011-09-07 11:43:16 -07002300 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002301 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 break;
jeffhaobdb76512011-09-07 11:43:16 -07002303 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002304 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 break;
jeffhaobdb76512011-09-07 11:43:16 -07002306 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002307 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 break;
2309 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002310 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002313 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002316 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2317 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 break;
2319
2320 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002321 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 break;
2323 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002324 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 break;
2326 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002327 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 break;
2329 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002330 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002333 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002336 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002339 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2340 true);
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342
2343 case Instruction::INVOKE_VIRTUAL:
2344 case Instruction::INVOKE_VIRTUAL_RANGE:
2345 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2348 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002349 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2350 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002351 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2352 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002353 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002354 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002355 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002356 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002357 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002358 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002359 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2360 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002361 return_type_class->CannotBeAssignedFromOtherTypes());
2362 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002363 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2364 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002365 }
2366 }
2367 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002369 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2370 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002371 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002372 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002373 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002374 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002375 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002376 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002377 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002379 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 }
jeffhaobdb76512011-09-07 11:43:16 -07002382 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002384 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002385 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002386 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002387 const char* return_type_descriptor;
2388 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002389 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002390 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002391 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002392 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002393 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002394 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2395 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2396 } else {
2397 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002398 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002399 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002400 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002401 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002402 if (return_type_class != nullptr) {
2403 return_type = &reg_types_.FromClass(return_type_descriptor,
2404 return_type_class,
2405 return_type_class->CannotBeAssignedFromOtherTypes());
2406 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002407 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2408 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002409 }
Ian Rogers46685432012-06-03 22:26:43 -07002410 }
2411 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002412 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 * Some additional checks when calling a constructor. We know from the invocation arg check
2414 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2415 * that to require that called_method->klass is the same as this->klass or this->super,
2416 * allowing the latter only if the "this" argument is the same as the "this" argument to
2417 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002418 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002419 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002420 if (this_type.IsConflict()) // failure.
2421 break;
jeffhaobdb76512011-09-07 11:43:16 -07002422
jeffhaob57e9522012-04-26 18:08:21 -07002423 /* no null refs allowed (?) */
2424 if (this_type.IsZero()) {
2425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2426 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002427 }
jeffhaob57e9522012-04-26 18:08:21 -07002428
2429 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002430 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002431 // TODO: re-enable constructor type verification
2432 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002433 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002434 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2435 // break;
2436 // }
jeffhaob57e9522012-04-26 18:08:21 -07002437
2438 /* arg must be an uninitialized reference */
2439 if (!this_type.IsUninitializedTypes()) {
2440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2441 << this_type;
2442 break;
2443 }
2444
2445 /*
2446 * Replace the uninitialized reference with an initialized one. We need to do this for all
2447 * registers that have the same object instance in them, not just the "this" register.
2448 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002449 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002450 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002451 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002452 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2453 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002454 }
2455 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002456 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002457 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002458 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002459 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002460 just_set_result = true;
2461 break;
2462 }
2463 case Instruction::INVOKE_STATIC:
2464 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002465 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002466 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002467 METHOD_STATIC,
2468 is_range,
2469 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002470 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002471 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002472 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002473 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2474 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002475 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002477 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002478 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002479 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002480 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002481 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002482 } else {
2483 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2484 }
jeffhaobdb76512011-09-07 11:43:16 -07002485 just_set_result = true;
2486 }
2487 break;
jeffhaobdb76512011-09-07 11:43:16 -07002488 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002491 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002492 METHOD_INTERFACE,
2493 is_range,
2494 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002495 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002496 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002497 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2498 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2499 << PrettyMethod(abs_method) << "'";
2500 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002501 }
Ian Rogers0d604842012-04-16 14:50:24 -07002502 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002503 /* Get the type of the "this" arg, which should either be a sub-interface of called
2504 * interface or Object (see comments in RegType::JoinClass).
2505 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002506 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002507 if (this_type.IsZero()) {
2508 /* null pointer always passes (and always fails at runtime) */
2509 } else {
2510 if (this_type.IsUninitializedTypes()) {
2511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2512 << this_type;
2513 break;
2514 }
2515 // In the past we have tried to assert that "called_interface" is assignable
2516 // from "this_type.GetClass()", however, as we do an imprecise Join
2517 // (RegType::JoinClass) we don't have full information on what interfaces are
2518 // implemented by "this_type". For example, two classes may implement the same
2519 // interfaces and have a common parent that doesn't implement the interface. The
2520 // join will set "this_type" to the parent class and a test that this implements
2521 // the interface will incorrectly fail.
2522 }
2523 /*
2524 * We don't have an object instance, so we can't find the concrete method. However, all of
2525 * the type information is in the abstract method, so we're good.
2526 */
2527 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002528 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002529 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002530 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2531 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2532 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2533 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002534 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002535 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002536 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002537 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 } else {
2540 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2541 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002542 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 }
jeffhaobdb76512011-09-07 11:43:16 -07002545 case Instruction::NEG_INT:
2546 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002547 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002548 break;
2549 case Instruction::NEG_LONG:
2550 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002552 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002558 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002559 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002560 break;
2561 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002562 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002563 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002564 break;
2565 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002570 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
2572 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002573 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002574 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002578 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002579 break;
2580 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002582 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002586 break;
2587 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002588 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002589 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002592 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002593 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002596 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002597 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002600 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002601 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002611 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002614 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616
2617 case Instruction::ADD_INT:
2618 case Instruction::SUB_INT:
2619 case Instruction::MUL_INT:
2620 case Instruction::REM_INT:
2621 case Instruction::DIV_INT:
2622 case Instruction::SHL_INT:
2623 case Instruction::SHR_INT:
2624 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002626 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::AND_INT:
2629 case Instruction::OR_INT:
2630 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002631 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::ADD_LONG:
2635 case Instruction::SUB_LONG:
2636 case Instruction::MUL_LONG:
2637 case Instruction::DIV_LONG:
2638 case Instruction::REM_LONG:
2639 case Instruction::AND_LONG:
2640 case Instruction::OR_LONG:
2641 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002642 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002643 reg_types_.LongLo(), reg_types_.LongHi(),
2644 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002645 break;
2646 case Instruction::SHL_LONG:
2647 case Instruction::SHR_LONG:
2648 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002650 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002651 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::ADD_FLOAT:
2654 case Instruction::SUB_FLOAT:
2655 case Instruction::MUL_FLOAT:
2656 case Instruction::DIV_FLOAT:
2657 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2659 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::ADD_DOUBLE:
2662 case Instruction::SUB_DOUBLE:
2663 case Instruction::MUL_DOUBLE:
2664 case Instruction::DIV_DOUBLE:
2665 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002666 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002667 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2668 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::ADD_INT_2ADDR:
2671 case Instruction::SUB_INT_2ADDR:
2672 case Instruction::MUL_INT_2ADDR:
2673 case Instruction::REM_INT_2ADDR:
2674 case Instruction::SHL_INT_2ADDR:
2675 case Instruction::SHR_INT_2ADDR:
2676 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2678 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::AND_INT_2ADDR:
2681 case Instruction::OR_INT_2ADDR:
2682 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002683 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2684 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2688 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::ADD_LONG_2ADDR:
2691 case Instruction::SUB_LONG_2ADDR:
2692 case Instruction::MUL_LONG_2ADDR:
2693 case Instruction::DIV_LONG_2ADDR:
2694 case Instruction::REM_LONG_2ADDR:
2695 case Instruction::AND_LONG_2ADDR:
2696 case Instruction::OR_LONG_2ADDR:
2697 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002698 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002699 reg_types_.LongLo(), reg_types_.LongHi(),
2700 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::SHL_LONG_2ADDR:
2703 case Instruction::SHR_LONG_2ADDR:
2704 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002705 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002706 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
2708 case Instruction::ADD_FLOAT_2ADDR:
2709 case Instruction::SUB_FLOAT_2ADDR:
2710 case Instruction::MUL_FLOAT_2ADDR:
2711 case Instruction::DIV_FLOAT_2ADDR:
2712 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2714 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::ADD_DOUBLE_2ADDR:
2717 case Instruction::SUB_DOUBLE_2ADDR:
2718 case Instruction::MUL_DOUBLE_2ADDR:
2719 case Instruction::DIV_DOUBLE_2ADDR:
2720 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2723 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002724 break;
2725 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002726 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002727 case Instruction::MUL_INT_LIT16:
2728 case Instruction::DIV_INT_LIT16:
2729 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002730 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2731 true);
jeffhaobdb76512011-09-07 11:43:16 -07002732 break;
2733 case Instruction::AND_INT_LIT16:
2734 case Instruction::OR_INT_LIT16:
2735 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002736 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2737 true);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::ADD_INT_LIT8:
2740 case Instruction::RSUB_INT_LIT8:
2741 case Instruction::MUL_INT_LIT8:
2742 case Instruction::DIV_INT_LIT8:
2743 case Instruction::REM_INT_LIT8:
2744 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002745 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002746 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002747 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2748 false);
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::AND_INT_LIT8:
2751 case Instruction::OR_INT_LIT8:
2752 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002753 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2754 false);
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002757 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002758 case Instruction::RETURN_VOID_NO_BARRIER:
2759 if (IsConstructor() && !IsStatic()) {
2760 auto& declaring_class = GetDeclaringClass();
2761 auto* klass = declaring_class.GetClass();
2762 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2763 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2765 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002766 break;
2767 }
2768 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002769 }
2770 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002771 // Note: the following instructions encode offsets derived from class linking.
2772 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2773 // meaning if the class linking and resolution were successful.
2774 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002775 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002776 break;
2777 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002778 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002779 break;
2780 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002781 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002782 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002783 case Instruction::IGET_BOOLEAN_QUICK:
2784 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2785 break;
2786 case Instruction::IGET_BYTE_QUICK:
2787 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2788 break;
2789 case Instruction::IGET_CHAR_QUICK:
2790 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2791 break;
2792 case Instruction::IGET_SHORT_QUICK:
2793 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2794 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002795 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002797 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002798 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002800 break;
2801 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002803 break;
2804 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002806 break;
2807 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002808 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002809 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002810 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002811 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002812 break;
2813 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002814 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002815 break;
2816 case Instruction::INVOKE_VIRTUAL_QUICK:
2817 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2818 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002819 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002820 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002821 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002822 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002823 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002824 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002825 } else {
2826 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2827 }
2828 just_set_result = true;
2829 }
2830 break;
2831 }
2832
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002834 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2835 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002836 case Instruction::UNUSED_79:
2837 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002839 break;
2840
2841 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002842 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002843 * complain if an instruction is missing (which is desirable).
2844 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002845 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002846
Stephen Kyle7e541c92014-12-17 17:10:02 +00002847 /*
2848 * If we are in a constructor, and we had an UninitializedThis type
2849 * in a register somewhere, we need to make sure it wasn't overwritten.
2850 */
2851 if (track_uninitialized_this) {
2852 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2853 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2854 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2855 was_invoke_direct)) {
2856 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2857 << "Constructor failed to initialize this object";
2858 }
2859 }
2860
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002862 if (Runtime::Current()->IsAotCompiler()) {
2863 /* When AOT compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002864 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002865 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 /* immediate failure, reject class */
2867 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2868 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002869 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002870 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002871 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002872 }
jeffhaobdb76512011-09-07 11:43:16 -07002873 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 * If we didn't just set the result register, clear it out. This ensures that you can only use
2875 * "move-result" immediately after the result is set. (We could check this statically, but it's
2876 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002877 */
2878 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002879 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002880 }
2881
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002882
jeffhaobdb76512011-09-07 11:43:16 -07002883
2884 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002885 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002886 *
2887 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002888 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002889 * somebody could get a reference field, check it for zero, and if the
2890 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002891 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002892 * that, and will reject the code.
2893 *
2894 * TODO: avoid re-fetching the branch target
2895 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002896 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002897 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002899 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002901 return false;
2902 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002903 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002904 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002905 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 }
jeffhaobdb76512011-09-07 11:43:16 -07002907 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002909 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002910 return false;
2911 }
2912 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002913 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002914 return false;
2915 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 }
jeffhaobdb76512011-09-07 11:43:16 -07002917 }
2918
2919 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002920 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002921 *
2922 * We've already verified that the table is structurally sound, so we
2923 * just need to walk through and tag the targets.
2924 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002925 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002926 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2927 const uint16_t* switch_insns = insns + offset_to_switch;
2928 int switch_count = switch_insns[1];
2929 int offset_to_targets, targ;
2930
2931 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2932 /* 0 = sig, 1 = count, 2/3 = first key */
2933 offset_to_targets = 4;
2934 } else {
2935 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002936 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002937 offset_to_targets = 2 + 2 * switch_count;
2938 }
2939
2940 /* verify each switch target */
2941 for (targ = 0; targ < switch_count; targ++) {
2942 int offset;
2943 uint32_t abs_offset;
2944
2945 /* offsets are 32-bit, and only partly endian-swapped */
2946 offset = switch_insns[offset_to_targets + targ * 2] |
2947 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 abs_offset = work_insn_idx_ + offset;
2949 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002950 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002951 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002953 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002954 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002955 }
jeffhaobdb76512011-09-07 11:43:16 -07002956 }
2957 }
2958
2959 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002960 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2961 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002962 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002963 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002964 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002965 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002966
Andreas Gampef91baf12014-07-18 15:41:00 -07002967 // Need the linker to try and resolve the handled class to check if it's Throwable.
2968 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2969
Ian Rogers0571d352011-11-03 19:51:38 -07002970 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002971 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2972 if (handler_type_idx == DexFile::kDexNoIndex16) {
2973 has_catch_all_handler = true;
2974 } else {
2975 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002976 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2977 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002978 if (klass != nullptr) {
2979 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2980 has_catch_all_handler = true;
2981 }
2982 } else {
2983 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002984 DCHECK(self_->IsExceptionPending());
2985 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002986 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 }
jeffhaobdb76512011-09-07 11:43:16 -07002988 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2990 * "work_regs", because at runtime the exception will be thrown before the instruction
2991 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002992 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002993 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002994 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 }
jeffhaobdb76512011-09-07 11:43:16 -07002996 }
2997
2998 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3000 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003001 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003002 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003003 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 * The state in work_line reflects the post-execution state. If the current instruction is a
3005 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003006 * it will do so before grabbing the lock).
3007 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003008 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003009 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003011 return false;
3012 }
3013 }
3014 }
3015
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003016 /* Handle "continue". Tag the next consecutive instruction.
3017 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3018 * because it changes work_line_ when performing peephole optimization
3019 * and this change should not be used in those cases.
3020 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003021 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003022 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3023 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003024 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3026 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003027 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003028 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3029 // next instruction isn't one.
3030 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3031 return false;
3032 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003033 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003034 // Make workline consistent with fallthrough computed from peephole optimization.
3035 work_line_->CopyFromLine(fallthrough_line.get());
3036 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003037 if (insn_flags_[next_insn_idx].IsReturn()) {
3038 // For returns we only care about the operand to the return, all other registers are dead.
3039 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3040 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003041 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003042 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003043 } else {
3044 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003045 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003046 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003047 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003048 }
3049 }
3050 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003051 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003052 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003053 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3054 // needed. If the merge changes the state of the registers then the work line will be
3055 // updated.
3056 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003057 return false;
3058 }
3059 } else {
3060 /*
3061 * We're not recording register data for the next instruction, so we don't know what the
3062 * prior state was. We have to assume that something has changed and re-evaluate it.
3063 */
3064 insn_flags_[next_insn_idx].SetChanged();
3065 }
3066 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003067
jeffhaod1f0fde2011-09-08 17:25:33 -07003068 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003069 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003070 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 return false;
3072 }
jeffhaobdb76512011-09-07 11:43:16 -07003073 }
3074
3075 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003076 * Update start_guess. Advance to the next instruction of that's
3077 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003078 * neither of those exists we're in a return or throw; leave start_guess
3079 * alone and let the caller sort it out.
3080 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003081 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003082 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3083 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003084 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003085 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003087 }
3088
Ian Rogersd81871c2011-10-03 13:57:23 -07003089 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3090 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003091
3092 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003093} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003094
Ian Rogersd8f69b02014-09-10 21:43:52 +00003095const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003096 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003097 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003098 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003099 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003100 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3101 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003102 if (result.IsConflict()) {
3103 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3104 << "' in " << referrer;
3105 return result;
3106 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003107 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003108 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003109 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003110 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003111 // check at runtime if access is allowed and so pass here. If result is
3112 // primitive, skip the access check.
3113 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3114 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003115 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003116 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003117 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003118 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003119}
3120
Ian Rogersd8f69b02014-09-10 21:43:52 +00003121const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003122 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003124 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3126 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003127 CatchHandlerIterator iterator(handlers_ptr);
3128 for (; iterator.HasNext(); iterator.Next()) {
3129 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3130 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003131 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003132 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003133 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003134 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003135 if (exception.IsUnresolvedTypes()) {
3136 // We don't know enough about the type. Fail here and let runtime handle it.
3137 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3138 return exception;
3139 } else {
3140 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3141 return reg_types_.Conflict();
3142 }
Jeff Haob878f212014-04-24 16:25:36 -07003143 } else if (common_super == nullptr) {
3144 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003145 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003146 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003148 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003149 if (FailOrAbort(this,
3150 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3151 "java.lang.Throwable is not assignable-from common_super at ",
3152 work_insn_idx_)) {
3153 break;
3154 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 }
3156 }
3157 }
3158 }
Ian Rogers0571d352011-11-03 19:51:38 -07003159 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 }
3161 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003164 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003165 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003166 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003167 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003168}
3169
Brian Carlstromea46f952013-07-30 01:26:50 -07003170mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003171 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003172 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003173 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003174 if (klass_type.IsConflict()) {
3175 std::string append(" in attempt to access method ");
3176 append += dex_file_->GetMethodName(method_id);
3177 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003179 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003180 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003181 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003182 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003183 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003184 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003185 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003187 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003188 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003189
3190 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003192 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 res_method = klass->FindInterfaceMethod(name, signature);
3194 } else {
3195 res_method = klass->FindVirtualMethod(name, signature);
3196 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003197 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003198 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003200 // If a virtual or interface method wasn't found with the expected type, look in
3201 // the direct methods. This can happen when the wrong invoke type is used or when
3202 // a class has changed, and will be flagged as an error in later checks.
3203 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3204 res_method = klass->FindDirectMethod(name, signature);
3205 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003206 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003207 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3208 << PrettyDescriptor(klass) << "." << name
3209 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003211 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 }
3213 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3215 // enforce them here.
3216 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3218 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003219 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003221 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003222 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3224 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003225 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003226 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003227 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003228 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003229 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003230 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003231 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003232 }
jeffhaode0d9c92012-02-27 13:58:13 -08003233 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3234 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3236 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003237 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003238 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003239 // Check that interface methods match interface classes.
3240 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3241 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3242 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003243 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003244 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3245 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3246 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003248 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 // See if the method type implied by the invoke instruction matches the access flags for the
3250 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003251 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3253 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3254 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003255 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3256 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003257 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003259 return res_method;
3260}
3261
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003262template <class T>
3263mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3264 MethodType method_type,
3265 bool is_range,
3266 mirror::ArtMethod* res_method) {
3267 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3268 // match the call to the signature. Also, we might be calling through an abstract method
3269 // definition (which doesn't have register count values).
3270 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3271 /* caught by static verifier */
3272 DCHECK(is_range || expected_args <= 5);
3273 if (expected_args > code_item_->outs_size_) {
3274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3275 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3276 return nullptr;
3277 }
3278
3279 uint32_t arg[5];
3280 if (!is_range) {
3281 inst->GetVarArgs(arg);
3282 }
3283 uint32_t sig_registers = 0;
3284
3285 /*
3286 * Check the "this" argument, which must be an instance of the class that declared the method.
3287 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3288 * rigorous check here (which is okay since we have to do it at runtime).
3289 */
3290 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003291 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003292 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3293 CHECK(have_pending_hard_failure_);
3294 return nullptr;
3295 }
3296 if (actual_arg_type.IsUninitializedReference()) {
3297 if (res_method) {
3298 if (!res_method->IsConstructor()) {
3299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3300 return nullptr;
3301 }
3302 } else {
3303 // Check whether the name of the called method is "<init>"
3304 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003305 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3307 return nullptr;
3308 }
3309 }
3310 }
3311 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003312 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003313 if (res_method != nullptr) {
3314 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003315 std::string temp;
3316 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003317 klass->CannotBeAssignedFromOtherTypes());
3318 } else {
3319 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3320 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003321 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003322 dex_file_->StringByTypeIdx(class_idx),
3323 false);
3324 }
3325 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3326 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3327 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3328 << "' not instance of '" << *res_method_class << "'";
3329 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3330 // the compiler.
3331 if (have_pending_hard_failure_) {
3332 return nullptr;
3333 }
3334 }
3335 }
3336 sig_registers = 1;
3337 }
3338
3339 for ( ; it->HasNext(); it->Next()) {
3340 if (sig_registers >= expected_args) {
3341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3342 " arguments, found " << sig_registers << " or more.";
3343 return nullptr;
3344 }
3345
3346 const char* param_descriptor = it->GetDescriptor();
3347
3348 if (param_descriptor == nullptr) {
3349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3350 "component";
3351 return nullptr;
3352 }
3353
Ian Rogersd8f69b02014-09-10 21:43:52 +00003354 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003355 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3356 arg[sig_registers];
3357 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003358 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003359 if (!src_type.IsIntegralTypes()) {
3360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3361 << " but expected " << reg_type;
3362 return res_method;
3363 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003364 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003365 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3366 // compiler.
3367 if (have_pending_hard_failure_) {
3368 return res_method;
3369 }
3370 }
3371 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3372 }
3373 if (expected_args != sig_registers) {
3374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3375 " arguments, found " << sig_registers;
3376 return nullptr;
3377 }
3378 return res_method;
3379}
3380
3381void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3382 MethodType method_type,
3383 bool is_range) {
3384 // As the method may not have been resolved, make this static check against what we expect.
3385 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3386 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3387 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3388 DexFileParameterIterator it(*dex_file_,
3389 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3390 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3391 nullptr);
3392}
3393
3394class MethodParamListDescriptorIterator {
3395 public:
3396 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3397 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3398 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3399 }
3400
3401 bool HasNext() {
3402 return pos_ < params_size_;
3403 }
3404
3405 void Next() {
3406 ++pos_;
3407 }
3408
3409 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3410 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3411 }
3412
3413 private:
3414 mirror::ArtMethod* res_method_;
3415 size_t pos_;
3416 const DexFile::TypeList* params_;
3417 const size_t params_size_;
3418};
3419
Brian Carlstromea46f952013-07-30 01:26:50 -07003420mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003421 MethodType method_type,
3422 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003423 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003424 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3425 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003426 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003427
Brian Carlstromea46f952013-07-30 01:26:50 -07003428 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003429 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003430 // Check what we can statically.
3431 if (!have_pending_hard_failure_) {
3432 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3433 }
3434 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003435 }
3436
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3438 // has a vtable entry for the target method.
3439 if (is_super) {
3440 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003441 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003442 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003443 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003444 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003445 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003446 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003447 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003448 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003449 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003450 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003451 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003452 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003453 << "." << res_method->GetName()
3454 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003455 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003456 }
3457 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003458
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003459 // Process the target method's signature. This signature may or may not
3460 MethodParamListDescriptorIterator it(res_method);
3461 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3462 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003463}
3464
Brian Carlstromea46f952013-07-30 01:26:50 -07003465mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -08003466 RegisterLine* reg_line, bool is_range,
3467 bool allow_failure) {
3468 if (is_range) {
3469 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3470 } else {
3471 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3472 }
3473 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003474 if (!actual_arg_type.HasClass()) {
3475 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003476 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003477 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003478 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003479 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003480 if (klass->IsInterface()) {
3481 // Derive Object.class from Class.class.getSuperclass().
3482 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003483 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003484 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003485 return nullptr;
3486 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003487 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003488 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003489 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003490 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003491 if (!dispatch_class->HasVTable()) {
3492 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3493 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003494 return nullptr;
3495 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003496 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003497 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3498 FailOrAbort(this, allow_failure,
3499 "Receiver class has not enough vtable slots for quickened invoke at ",
3500 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003501 return nullptr;
3502 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003503 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003504 if (self_->IsExceptionPending()) {
3505 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3506 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003507 return nullptr;
3508 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003509 return res_method;
3510}
3511
Brian Carlstromea46f952013-07-30 01:26:50 -07003512mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003513 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003514 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3515 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3516
Mathieu Chartier091d2382015-03-06 10:59:06 -08003517 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003518 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003520 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003521 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003522 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3523 work_insn_idx_)) {
3524 return nullptr;
3525 }
3526 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3527 work_insn_idx_)) {
3528 return nullptr;
3529 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003530
3531 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3532 // match the call to the signature. Also, we might be calling through an abstract method
3533 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003534 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003535 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003536 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003537 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003538 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3539 /* caught by static verifier */
3540 DCHECK(is_range || expected_args <= 5);
3541 if (expected_args > code_item_->outs_size_) {
3542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3543 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003544 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003545 }
3546
3547 /*
3548 * Check the "this" argument, which must be an instance of the class that declared the method.
3549 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3550 * rigorous check here (which is okay since we have to do it at runtime).
3551 */
3552 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003554 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003555 }
3556 if (!actual_arg_type.IsZero()) {
3557 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003558 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003559 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003560 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003561 klass->CannotBeAssignedFromOtherTypes());
3562 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003563 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3564 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003565 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003566 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003567 }
3568 }
3569 /*
3570 * Process the target method's signature. This signature may or may not
3571 * have been verified, so we can't assume it's properly formed.
3572 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003573 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003574 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003575 uint32_t arg[5];
3576 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003577 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003578 }
3579 size_t actual_args = 1;
3580 for (size_t param_index = 0; param_index < params_size; param_index++) {
3581 if (actual_args >= expected_args) {
3582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003583 << "'. Expected " << expected_args
3584 << " arguments, processing argument " << actual_args
3585 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003586 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003587 }
3588 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003589 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003590 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003592 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003593 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003594 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003595 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003596 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003597 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003598 return res_method;
3599 }
3600 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3601 }
3602 if (actual_args != expected_args) {
3603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3604 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003605 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003606 } else {
3607 return res_method;
3608 }
3609}
3610
Ian Rogers62342ec2013-06-11 10:26:37 -07003611void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003612 uint32_t type_idx;
3613 if (!is_filled) {
3614 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3615 type_idx = inst->VRegC_22c();
3616 } else if (!is_range) {
3617 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3618 type_idx = inst->VRegB_35c();
3619 } else {
3620 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3621 type_idx = inst->VRegB_3rc();
3622 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003623 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003624 if (res_type.IsConflict()) { // bad class
3625 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003626 } else {
3627 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3628 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003630 } else if (!is_filled) {
3631 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003632 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003633 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003634 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003635 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003636 } else {
3637 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3638 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003639 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003640 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3641 uint32_t arg[5];
3642 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003643 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003644 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003645 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003646 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003647 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3648 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003649 return;
3650 }
3651 }
3652 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003653 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003654 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003655 }
3656 }
3657}
3658
Sebastien Hertz5243e912013-05-21 10:55:07 +02003659void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003660 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003661 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003662 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003664 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003665 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003666 if (array_type.IsZero()) {
3667 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3668 // instruction type. TODO: have a proper notion of bottom here.
3669 if (!is_primitive || insn_type.IsCategory1Types()) {
3670 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003671 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003673 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003674 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3675 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003676 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003677 }
jeffhaofc3144e2012-02-01 17:21:15 -08003678 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003680 } else {
3681 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003682 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003683 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003685 << " source for aget-object";
3686 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003688 << " source for category 1 aget";
3689 } else if (is_primitive && !insn_type.Equals(component_type) &&
3690 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003691 (insn_type.IsLong() && component_type.IsDouble()))) {
3692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3693 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003694 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003695 // Use knowledge of the field type which is stronger than the type inferred from the
3696 // instruction, which can't differentiate object types and ints from floats, longs from
3697 // doubles.
3698 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003699 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003700 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003701 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003702 component_type.HighHalf(&reg_types_));
3703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003704 }
3705 }
3706 }
3707}
3708
Ian Rogersd8f69b02014-09-10 21:43:52 +00003709void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003710 const uint32_t vregA) {
3711 // Primitive assignability rules are weaker than regular assignability rules.
3712 bool instruction_compatible;
3713 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003715 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003716 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003717 value_compatible = value_type.IsIntegralTypes();
3718 } else if (target_type.IsFloat()) {
3719 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3720 value_compatible = value_type.IsFloatTypes();
3721 } else if (target_type.IsLong()) {
3722 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003723 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3724 // as target_type depends on the resolved type of the field.
3725 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003727 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3728 } else {
3729 value_compatible = false;
3730 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003731 } else if (target_type.IsDouble()) {
3732 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003733 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3734 // as target_type depends on the resolved type of the field.
3735 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003736 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003737 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3738 } else {
3739 value_compatible = false;
3740 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003741 } else {
3742 instruction_compatible = false; // reference with primitive store
3743 value_compatible = false; // unused
3744 }
3745 if (!instruction_compatible) {
3746 // This is a global failure rather than a class change failure as the instructions and
3747 // the descriptors for the type should have been consistent within the same file at
3748 // compile time.
3749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3750 << "' but expected type '" << target_type << "'";
3751 return;
3752 }
3753 if (!value_compatible) {
3754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3755 << " of type " << value_type << " but expected " << target_type << " for put";
3756 return;
3757 }
3758}
3759
Sebastien Hertz5243e912013-05-21 10:55:07 +02003760void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003761 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003762 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003763 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003765 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003766 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003767 if (array_type.IsZero()) {
3768 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3769 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003770 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003772 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003773 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003774 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003775 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003776 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003778 if (!component_type.IsReferenceTypes()) {
3779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3780 << " source for aput-object";
3781 } else {
3782 // The instruction agrees with the type of array, confirm the value to be stored does too
3783 // Note: we use the instruction type (rather than the component type) for aput-object as
3784 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003785 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003786 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003787 }
3788 }
3789 }
3790}
3791
Brian Carlstromea46f952013-07-30 01:26:50 -07003792mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003793 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3794 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003795 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003796 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003797 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3798 field_idx, dex_file_->GetFieldName(field_id),
3799 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003800 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003801 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003802 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003803 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003804 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003805 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003806 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3807 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003808 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003809 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003810 << dex_file_->GetFieldName(field_id) << ") in "
3811 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003812 DCHECK(self_->IsExceptionPending());
3813 self_->ClearException();
3814 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003815 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3816 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003817 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003818 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003819 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 } else if (!field->IsStatic()) {
3821 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003822 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003824 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003825}
3826
Ian Rogersd8f69b02014-09-10 21:43:52 +00003827mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003828 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3829 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003830 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831 if (klass_type.IsConflict()) {
3832 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3833 field_idx, dex_file_->GetFieldName(field_id),
3834 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003836 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003837 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003838 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003839 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003840 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003841 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3842 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003843 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003844 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003845 << dex_file_->GetFieldName(field_id) << ") in "
3846 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003847 DCHECK(self_->IsExceptionPending());
3848 self_->ClearException();
3849 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003850 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3851 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003853 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003854 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003855 } else if (field->IsStatic()) {
3856 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3857 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003858 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003859 } else if (obj_type.IsZero()) {
3860 // Cannot infer and check type, however, access will cause null pointer exception
3861 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003862 } else if (!obj_type.IsReferenceTypes()) {
3863 // Trying to read a field from something that isn't a reference
3864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3865 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003866 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003867 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003868 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003869 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003870 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003871 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003872 if (obj_type.IsUninitializedTypes() &&
3873 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3874 !field_klass.Equals(GetDeclaringClass()))) {
3875 // Field accesses through uninitialized references are only allowable for constructors where
3876 // the field is declared in this class
3877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003878 << " of a not fully initialized object within the context"
3879 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003881 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3882 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3883 // of C1. For resolution to occur the declared class of the field must be compatible with
3884 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3885 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3886 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003888 } else {
3889 return field;
3890 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003891 }
3892}
3893
Andreas Gampe896df402014-10-20 22:25:29 -07003894template <MethodVerifier::FieldAccessType kAccType>
3895void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3896 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003897 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003898 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003899 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003900 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003901 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003902 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003903 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003904 if (UNLIKELY(have_pending_hard_failure_)) {
3905 return;
3906 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003907 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003908 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003909 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003910 if (kAccType == FieldAccessType::kAccPut) {
3911 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3912 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3913 << " from other class " << GetDeclaringClass();
3914 return;
3915 }
3916 }
3917
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003918 mirror::Class* field_type_class;
3919 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003920 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003921 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07003922 field_type_class = can_load_classes_ ? h_field->GetType<true>() : h_field->GetType<false>();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003923 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003924 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003925 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003926 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003927 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003928 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3929 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003930 }
Ian Rogers0d604842012-04-16 14:50:24 -07003931 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003932 if (field_type == nullptr) {
3933 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3934 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003935 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003936 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003937 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003938 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003939 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3940 "Unexpected third access type");
3941 if (kAccType == FieldAccessType::kAccPut) {
3942 // sput or iput.
3943 if (is_primitive) {
3944 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003945 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003946 if (!insn_type.IsAssignableFrom(*field_type)) {
3947 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3948 << " to be compatible with type '" << insn_type
3949 << "' but found type '" << *field_type
3950 << "' in put-object";
3951 return;
3952 }
3953 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003954 }
Andreas Gampe896df402014-10-20 22:25:29 -07003955 } else if (kAccType == FieldAccessType::kAccGet) {
3956 // sget or iget.
3957 if (is_primitive) {
3958 if (field_type->Equals(insn_type) ||
3959 (field_type->IsFloat() && insn_type.IsInteger()) ||
3960 (field_type->IsDouble() && insn_type.IsLong())) {
3961 // expected that read is of the correct primitive type or that int reads are reading
3962 // floats or long reads are reading doubles
3963 } else {
3964 // This is a global failure rather than a class change failure as the instructions and
3965 // the descriptors for the type should have been consistent within the same file at
3966 // compile time
3967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3968 << " to be of type '" << insn_type
3969 << "' but found type '" << *field_type << "' in get";
3970 return;
3971 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003972 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003973 if (!insn_type.IsAssignableFrom(*field_type)) {
3974 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3975 << " to be compatible with type '" << insn_type
3976 << "' but found type '" << *field_type
3977 << "' in get-object";
3978 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3979 return;
3980 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003981 }
Andreas Gampe896df402014-10-20 22:25:29 -07003982 if (!field_type->IsLowHalf()) {
3983 work_line_->SetRegisterType(this, vregA, *field_type);
3984 } else {
3985 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3986 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003987 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003988 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003989 }
3990}
3991
Brian Carlstromea46f952013-07-30 01:26:50 -07003992mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003993 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003994 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07003995 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003996 if (!object_type.HasClass()) {
3997 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3998 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003999 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004000 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004001 mirror::ArtField* const f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
4002 field_offset);
4003 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004004 if (f == nullptr) {
4005 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4006 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4007 }
4008 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004009}
4010
Andreas Gampe896df402014-10-20 22:25:29 -07004011template <MethodVerifier::FieldAccessType kAccType>
4012void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4013 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004014 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004015
Brian Carlstromea46f952013-07-30 01:26:50 -07004016 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004017 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4019 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004020 }
Andreas Gampe896df402014-10-20 22:25:29 -07004021
4022 // For an IPUT_QUICK, we now test for final flag of the field.
4023 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004024 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4025 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004026 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004027 return;
4028 }
4029 }
Andreas Gampe896df402014-10-20 22:25:29 -07004030
4031 // Get the field type.
4032 const RegType* field_type;
4033 {
4034 mirror::Class* field_type_class;
4035 {
4036 StackHandleScope<1> hs(Thread::Current());
4037 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07004038 field_type_class = can_load_classes_ ? h_field->GetType<true>() : h_field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004039 }
4040
4041 if (field_type_class != nullptr) {
4042 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4043 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004044 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004045 Thread* self = Thread::Current();
4046 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4047 self->ClearException();
4048 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4049 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004050 }
Andreas Gampe896df402014-10-20 22:25:29 -07004051 if (field_type == nullptr) {
4052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004053 return;
4054 }
Andreas Gampe896df402014-10-20 22:25:29 -07004055 }
4056
4057 const uint32_t vregA = inst->VRegA_22c();
4058 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4059 "Unexpected third access type");
4060 if (kAccType == FieldAccessType::kAccPut) {
4061 if (is_primitive) {
4062 // Primitive field assignability rules are weaker than regular assignability rules
4063 bool instruction_compatible;
4064 bool value_compatible;
4065 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4066 if (field_type->IsIntegralTypes()) {
4067 instruction_compatible = insn_type.IsIntegralTypes();
4068 value_compatible = value_type.IsIntegralTypes();
4069 } else if (field_type->IsFloat()) {
4070 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4071 value_compatible = value_type.IsFloatTypes();
4072 } else if (field_type->IsLong()) {
4073 instruction_compatible = insn_type.IsLong();
4074 value_compatible = value_type.IsLongTypes();
4075 } else if (field_type->IsDouble()) {
4076 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4077 value_compatible = value_type.IsDoubleTypes();
4078 } else {
4079 instruction_compatible = false; // reference field with primitive store
4080 value_compatible = false; // unused
4081 }
4082 if (!instruction_compatible) {
4083 // This is a global failure rather than a class change failure as the instructions and
4084 // the descriptors for the type should have been consistent within the same file at
4085 // compile time
4086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4087 << " to be of type '" << insn_type
4088 << "' but found type '" << *field_type
4089 << "' in put";
4090 return;
4091 }
4092 if (!value_compatible) {
4093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4094 << " of type " << value_type
4095 << " but expected " << *field_type
4096 << " for store to " << PrettyField(field) << " in put";
4097 return;
4098 }
4099 } else {
4100 if (!insn_type.IsAssignableFrom(*field_type)) {
4101 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4102 << " to be compatible with type '" << insn_type
4103 << "' but found type '" << *field_type
4104 << "' in put-object";
4105 return;
4106 }
4107 work_line_->VerifyRegisterType(this, vregA, *field_type);
4108 }
4109 } else if (kAccType == FieldAccessType::kAccGet) {
4110 if (is_primitive) {
4111 if (field_type->Equals(insn_type) ||
4112 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4113 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4114 // expected that read is of the correct primitive type or that int reads are reading
4115 // floats or long reads are reading doubles
4116 } else {
4117 // This is a global failure rather than a class change failure as the instructions and
4118 // the descriptors for the type should have been consistent within the same file at
4119 // compile time
4120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4121 << " to be of type '" << insn_type
4122 << "' but found type '" << *field_type << "' in Get";
4123 return;
4124 }
4125 } else {
4126 if (!insn_type.IsAssignableFrom(*field_type)) {
4127 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4128 << " to be compatible with type '" << insn_type
4129 << "' but found type '" << *field_type
4130 << "' in get-object";
4131 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4132 return;
4133 }
4134 }
4135 if (!field_type->IsLowHalf()) {
4136 work_line_->SetRegisterType(this, vregA, *field_type);
4137 } else {
4138 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004139 }
4140 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004141 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004142 }
4143}
4144
Ian Rogers776ac1f2012-04-13 23:36:36 -07004145bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004146 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004148 return false;
4149 }
4150 return true;
4151}
4152
Stephen Kyle9bc61992014-09-22 13:53:15 +01004153bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4154 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4155 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4157 return false;
4158 }
4159 return true;
4160}
4161
4162bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4163 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4164}
4165
Ian Rogersebbdd872014-07-07 23:53:08 -07004166bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4167 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004168 bool changed = true;
4169 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4170 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004171 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004172 * We haven't processed this instruction before, and we haven't touched the registers here, so
4173 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4174 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004175 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004176 if (!insn_flags_[next_insn].IsReturn()) {
4177 target_line->CopyFromLine(merge_line);
4178 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004179 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004180 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004181 return false;
4182 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004183 // For returns we only care about the operand to the return, all other registers are dead.
4184 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4185 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4186 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004187 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004188 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004189 } else {
4190 target_line->CopyFromLine(merge_line);
4191 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004192 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004193 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004194 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004195 }
4196 }
4197 }
jeffhaobdb76512011-09-07 11:43:16 -07004198 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004199 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004200 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004201 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004202 if (gDebugVerify) {
4203 copy->CopyFromLine(target_line);
4204 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004205 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004206 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004207 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004208 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004209 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004210 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004211 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004212 << copy->Dump(this) << " MERGE\n"
4213 << merge_line->Dump(this) << " ==\n"
4214 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004215 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004216 if (update_merge_line && changed) {
4217 merge_line->CopyFromLine(target_line);
4218 }
jeffhaobdb76512011-09-07 11:43:16 -07004219 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004220 if (changed) {
4221 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004222 }
4223 return true;
4224}
4225
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004226InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004227 return &insn_flags_[work_insn_idx_];
4228}
4229
Ian Rogersd8f69b02014-09-10 21:43:52 +00004230const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004231 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004232 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004233 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004234 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004235 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4236 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004237 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004238 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004239 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4240 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004241 }
4242 }
4243 if (return_type_ == nullptr) {
4244 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4245 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4246 uint16_t return_type_idx = proto_id.return_type_idx_;
4247 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004248 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004249 }
4250 }
4251 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004252}
4253
Ian Rogersd8f69b02014-09-10 21:43:52 +00004254const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004255 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004256 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004257 const char* descriptor
4258 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004259 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004260 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004261 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4262 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004263 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004264 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004265 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004266 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004267 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004268}
4269
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004270std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4271 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004272 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004273 std::vector<int32_t> result;
4274 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004275 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004276 if (type.IsConstant()) {
4277 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004278 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4279 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004280 } else if (type.IsConstantLo()) {
4281 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004282 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4283 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004284 } else if (type.IsConstantHi()) {
4285 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004286 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4287 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004288 } else if (type.IsIntegralTypes()) {
4289 result.push_back(kIntVReg);
4290 result.push_back(0);
4291 } else if (type.IsFloat()) {
4292 result.push_back(kFloatVReg);
4293 result.push_back(0);
4294 } else if (type.IsLong()) {
4295 result.push_back(kLongLoVReg);
4296 result.push_back(0);
4297 result.push_back(kLongHiVReg);
4298 result.push_back(0);
4299 ++i;
4300 } else if (type.IsDouble()) {
4301 result.push_back(kDoubleLoVReg);
4302 result.push_back(0);
4303 result.push_back(kDoubleHiVReg);
4304 result.push_back(0);
4305 ++i;
4306 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4307 result.push_back(kUndefined);
4308 result.push_back(0);
4309 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004310 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004311 result.push_back(kReferenceVReg);
4312 result.push_back(0);
4313 }
4314 }
4315 return result;
4316}
4317
Ian Rogersd8f69b02014-09-10 21:43:52 +00004318const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004319 if (precise) {
4320 // Precise constant type.
4321 return reg_types_.FromCat1Const(value, true);
4322 } else {
4323 // Imprecise constant type.
4324 if (value < -32768) {
4325 return reg_types_.IntConstant();
4326 } else if (value < -128) {
4327 return reg_types_.ShortConstant();
4328 } else if (value < 0) {
4329 return reg_types_.ByteConstant();
4330 } else if (value == 0) {
4331 return reg_types_.Zero();
4332 } else if (value == 1) {
4333 return reg_types_.One();
4334 } else if (value < 128) {
4335 return reg_types_.PosByteConstant();
4336 } else if (value < 32768) {
4337 return reg_types_.PosShortConstant();
4338 } else if (value < 65536) {
4339 return reg_types_.CharConstant();
4340 } else {
4341 return reg_types_.IntConstant();
4342 }
4343 }
4344}
4345
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004346void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004347 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004348}
4349
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004350void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004351 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004352}
jeffhaod1224c72012-02-29 13:43:08 -08004353
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004354void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4355 RegTypeCache::VisitStaticRoots(callback, arg);
4356}
4357
Mathieu Chartier12d625f2015-03-13 11:33:37 -07004358void MethodVerifier::VisitRoots(RootCallback* callback, void* arg, const RootInfo& root_info) {
4359 reg_types_.VisitRoots(callback, arg, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004360}
4361
Ian Rogersd81871c2011-10-03 13:57:23 -07004362} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004363} // namespace art