blob: 065df051f4d235ff210123dc52cddd4593952c88 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070023#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000025#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070027#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080028#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070029#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080031#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070032#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070033#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#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 Chartierd0ad2ee2015-03-31 14:59:59 -0700398 self->PushVerifier(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 Chartierd0ad2ee2015-03-31 14:59:59 -0700403 Thread::Current()->PopVerifier(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
Mathieu Chartierc7853442015-03-27 14:35:38 -0700454ArtField* 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
Mathieu Chartierc7853442015-03-27 14:35:38 -0700467ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200469
470 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
471 // verification. In practice, the phase we want relies on data structures set up by all the
472 // earlier passes, so we just run the full method verification and bail out early when we've
473 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200474 bool success = Verify();
475 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700476 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200477 }
478 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700480 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200481 }
482 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
483 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200484}
485
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;
jeffhaoba5ebb92011-08-25 17:24:37 -07001078 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001079 return true;
1080}
1081
Ian Rogers776ac1f2012-04-13 23:36:36 -07001082bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001084 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001086 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001088 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001090 << ", switch offset " << switch_offset
1091 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001092 return false;
1093 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001094 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001095 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001096 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001097 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1099 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001100 return false;
1101 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 uint32_t switch_count = switch_insns[1];
1103 int32_t keys_offset, targets_offset;
1104 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001105 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1106 /* 0=sig, 1=count, 2/3=firstKey */
1107 targets_offset = 4;
1108 keys_offset = -1;
1109 expected_signature = Instruction::kPackedSwitchSignature;
1110 } else {
1111 /* 0=sig, 1=count, 2..count*2 = keys */
1112 keys_offset = 2;
1113 targets_offset = 2 + 2 * switch_count;
1114 expected_signature = Instruction::kSparseSwitchSignature;
1115 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001117 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001118 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1119 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1120 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001121 return false;
1122 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001123 /* make sure the end of the switch is in range */
1124 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1126 << ", switch offset " << switch_offset
1127 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001128 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001129 return false;
1130 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001131 /* for a sparse switch, verify the keys are in ascending order */
1132 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1134 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001135 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1136 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1137 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1139 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001140 return false;
1141 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 last_key = key;
1143 }
1144 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001145 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 for (uint32_t targ = 0; targ < switch_count; targ++) {
1147 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1148 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1149 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001150 if (abs_offset < 0 ||
1151 abs_offset >= (int32_t) insn_count ||
1152 !insn_flags_[abs_offset].IsOpcode()) {
1153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1154 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1155 << reinterpret_cast<void*>(cur_offset)
1156 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001157 return false;
1158 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 insn_flags_[abs_offset].SetBranchTarget();
1160 }
1161 return true;
1162}
1163
Ian Rogers776ac1f2012-04-13 23:36:36 -07001164bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001165 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 return false;
1168 }
1169 uint16_t registers_size = code_item_->registers_size_;
1170 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001171 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1173 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 return false;
1175 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001176 }
1177
1178 return true;
1179}
1180
Ian Rogers776ac1f2012-04-13 23:36:36 -07001181bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 uint16_t registers_size = code_item_->registers_size_;
1183 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1184 // integer overflow when adding them here.
1185 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1187 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001188 return false;
1189 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001190 return true;
1191}
1192
Ian Rogers776ac1f2012-04-13 23:36:36 -07001193bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 uint16_t registers_size = code_item_->registers_size_;
1195 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001196
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001198 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1199 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 }
1201 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001202 reg_table_.Init(kTrackCompilerInterestPoints,
1203 insn_flags_.get(),
1204 insns_size,
1205 registers_size,
1206 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001207
jeffhaobdb76512011-09-07 11:43:16 -07001208
Ian Rogersd0fbd852013-09-24 18:17:04 -07001209 work_line_.reset(RegisterLine::Create(registers_size, this));
1210 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001211
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 /* Initialize register types of method arguments. */
1213 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001214 DCHECK_NE(failures_.size(), 0U);
1215 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001216 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001217 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 return false;
1219 }
1220 /* Perform code flow verification. */
1221 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001222 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001224 }
jeffhaobdb76512011-09-07 11:43:16 -07001225 return true;
1226}
1227
Ian Rogersad0b3a32012-04-16 14:50:24 -07001228std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1229 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001230 for (size_t i = 0; i < failures_.size(); ++i) {
1231 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001232 }
1233 return os;
1234}
1235
Ian Rogers776ac1f2012-04-13 23:36:36 -07001236void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001237 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001238 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 return;
jeffhaobdb76512011-09-07 11:43:16 -07001240 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001241 {
1242 os << "Register Types:\n";
1243 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1244 std::ostream indent_os(&indent_filter);
1245 reg_types_.Dump(indent_os);
1246 }
Ian Rogersb4903572012-10-11 11:52:56 -07001247 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001248 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1249 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 const Instruction* inst = Instruction::At(code_item_->insns_);
1251 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001252 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001254 if (reg_line != nullptr) {
1255 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001256 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001257 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001258 const bool kDumpHexOfInstruction = false;
1259 if (kDumpHexOfInstruction) {
1260 indent_os << inst->DumpHex(5) << " ";
1261 }
1262 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001263 inst = inst->Next();
1264 }
jeffhaobdb76512011-09-07 11:43:16 -07001265}
1266
Ian Rogersd81871c2011-10-03 13:57:23 -07001267static bool IsPrimitiveDescriptor(char descriptor) {
1268 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001269 case 'I':
1270 case 'C':
1271 case 'S':
1272 case 'B':
1273 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001274 case 'F':
1275 case 'D':
1276 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001278 default:
1279 return false;
1280 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001281}
1282
Ian Rogers776ac1f2012-04-13 23:36:36 -07001283bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 RegisterLine* reg_line = reg_table_.GetLine(0);
1285 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1286 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001287
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001289 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001291 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1293 // argument as uninitialized. This restricts field access until the superclass constructor is
1294 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001295 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001296 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001297 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 reg_types_.UninitializedThisArgument(declaring_class));
1299 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001300 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001301 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001303 }
1304
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001305 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001306 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001307 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001308
1309 for (; iterator.HasNext(); iterator.Next()) {
1310 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001311 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 LOG(FATAL) << "Null descriptor";
1313 }
1314 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1316 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 return false;
1318 }
1319 switch (descriptor[0]) {
1320 case 'L':
1321 case '[':
1322 // We assume that reference arguments are initialized. The only way it could be otherwise
1323 // (assuming the caller was verified) is if the current method is <init>, but in that case
1324 // it's effectively considered initialized the instant we reach here (in the sense that we
1325 // can return without doing anything or call virtual methods).
1326 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001327 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001328 if (!reg_type.IsNonZeroReferenceTypes()) {
1329 DCHECK(HasFailures());
1330 return false;
1331 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001332 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 }
1334 break;
1335 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001336 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 break;
1338 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001339 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 break;
1341 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001342 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 break;
1344 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001345 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 break;
1347 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001348 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 break;
1350 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001351 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 break;
1353 case 'J':
1354 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001355 if (cur_arg + 1 >= expected_args) {
1356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1357 << " args, found more (" << descriptor << ")";
1358 return false;
1359 }
1360
Ian Rogers7b078e82014-09-10 14:44:24 -07001361 const RegType* lo_half;
1362 const RegType* hi_half;
1363 if (descriptor[0] == 'J') {
1364 lo_half = &reg_types_.LongLo();
1365 hi_half = &reg_types_.LongHi();
1366 } else {
1367 lo_half = &reg_types_.DoubleLo();
1368 hi_half = &reg_types_.DoubleHi();
1369 }
1370 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 cur_arg++;
1372 break;
1373 }
1374 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1376 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 return false;
1378 }
1379 cur_arg++;
1380 }
1381 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1383 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 return false;
1385 }
1386 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1387 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1388 // format. Only major difference from the method argument format is that 'V' is supported.
1389 bool result;
1390 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1391 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001392 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 size_t i = 0;
1394 do {
1395 i++;
1396 } while (descriptor[i] == '['); // process leading [
1397 if (descriptor[i] == 'L') { // object array
1398 do {
1399 i++; // find closing ;
1400 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1401 result = descriptor[i] == ';';
1402 } else { // primitive array
1403 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1404 }
1405 } else if (descriptor[0] == 'L') {
1406 // could be more thorough here, but shouldn't be required
1407 size_t i = 0;
1408 do {
1409 i++;
1410 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1411 result = descriptor[i] == ';';
1412 } else {
1413 result = false;
1414 }
1415 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1417 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 }
1419 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001420}
1421
Ian Rogers776ac1f2012-04-13 23:36:36 -07001422bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 const uint16_t* insns = code_item_->insns_;
1424 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001425
jeffhaobdb76512011-09-07 11:43:16 -07001426 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 insn_flags_[0].SetChanged();
1428 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001429
jeffhaobdb76512011-09-07 11:43:16 -07001430 /* Continue until no instructions are marked "changed". */
1431 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001432 if (allow_thread_suspension_) {
1433 self_->AllowThreadSuspension();
1434 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1436 uint32_t insn_idx = start_guess;
1437 for (; insn_idx < insns_size; insn_idx++) {
1438 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001439 break;
1440 }
jeffhaobdb76512011-09-07 11:43:16 -07001441 if (insn_idx == insns_size) {
1442 if (start_guess != 0) {
1443 /* try again, starting from the top */
1444 start_guess = 0;
1445 continue;
1446 } else {
1447 /* all flags are clear */
1448 break;
1449 }
1450 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 // We carry the working set of registers from instruction to instruction. If this address can
1452 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1453 // "changed" flags, we need to load the set of registers from the table.
1454 // Because we always prefer to continue on to the next instruction, we should never have a
1455 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1456 // target.
1457 work_insn_idx_ = insn_idx;
1458 if (insn_flags_[insn_idx].IsBranchTarget()) {
1459 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001460 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001461 /*
1462 * Sanity check: retrieve the stored register line (assuming
1463 * a full table) and make sure it actually matches.
1464 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001466 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 if (work_line_->CompareLine(register_line) != 0) {
1468 Dump(std::cout);
1469 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001470 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001471 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001472 << " work_line=" << work_line_->Dump(this) << "\n"
1473 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 }
jeffhaobdb76512011-09-07 11:43:16 -07001475 }
jeffhaobdb76512011-09-07 11:43:16 -07001476 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001478 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001479 prepend += " failed to verify: ";
1480 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 return false;
1482 }
jeffhaobdb76512011-09-07 11:43:16 -07001483 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 insn_flags_[insn_idx].SetVisited();
1485 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001486 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001487
Ian Rogers1c849e52012-06-28 14:00:33 -07001488 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001489 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001490 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001491 * (besides the wasted space), but it indicates a flaw somewhere
1492 * down the line, possibly in the verifier.
1493 *
1494 * If we've substituted "always throw" instructions into the stream,
1495 * we are almost certainly going to have some dead code.
1496 */
1497 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001499 for (; insn_idx < insns_size;
1500 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001501 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001502 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001503 * may or may not be preceded by a padding NOP (for alignment).
1504 */
1505 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1506 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1507 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001508 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001509 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1510 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1511 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001513 }
1514
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001516 if (dead_start < 0)
1517 dead_start = insn_idx;
1518 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001519 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1520 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001521 dead_start = -1;
1522 }
1523 }
1524 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001525 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1526 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001527 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001528 // To dump the state of the verify after a method, do something like:
1529 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1530 // "boolean java.lang.String.equals(java.lang.Object)") {
1531 // LOG(INFO) << info_messages_.str();
1532 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001533 }
jeffhaobdb76512011-09-07 11:43:16 -07001534 return true;
1535}
1536
Ian Rogers776ac1f2012-04-13 23:36:36 -07001537bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001538 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1539 // We want the state _before_ the instruction, for the case where the dex pc we're
1540 // interested in is itself a monitor-enter instruction (which is a likely place
1541 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001542 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001543 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001544 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1545 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1546 }
1547 }
1548
jeffhaobdb76512011-09-07 11:43:16 -07001549 /*
1550 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001551 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001552 * control to another statement:
1553 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001554 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001555 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001556 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001557 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001558 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001559 * throw an exception that is handled by an encompassing "try"
1560 * block.
1561 *
1562 * We can also return, in which case there is no successor instruction
1563 * from this point.
1564 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001565 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001566 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1568 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001569 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001570
jeffhaobdb76512011-09-07 11:43:16 -07001571 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001572 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001573 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001575 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001576 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 }
jeffhaobdb76512011-09-07 11:43:16 -07001578
1579 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001580 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001581 * can throw an exception, we will copy/merge this into the "catch"
1582 * address rather than work_line, because we don't want the result
1583 * from the "successful" code path (e.g. a check-cast that "improves"
1584 * a type) to be visible to the exception handler.
1585 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001586 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001588 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001589 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001590 }
1591
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001592
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001593 // We need to ensure the work line is consistent while performing validation. When we spot a
1594 // peephole pattern we compute a new line for either the fallthrough instruction or the
1595 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001596 std::unique_ptr<RegisterLine> branch_line;
1597 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001598
Stephen Kyle7e541c92014-12-17 17:10:02 +00001599 /*
1600 * If we are in a constructor, and we currently have an UninitializedThis type
1601 * in a register somewhere, we need to make sure it isn't overwritten.
1602 */
1603 bool track_uninitialized_this = false;
1604 size_t uninitialized_this_loc = 0;
1605 if (IsConstructor()) {
1606 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1607 }
1608
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001610 case Instruction::NOP:
1611 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001612 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001613 * a signature that looks like a NOP; if we see one of these in
1614 * the course of executing code then we have a problem.
1615 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001616 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001618 }
1619 break;
1620
1621 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001622 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001623 break;
jeffhaobdb76512011-09-07 11:43:16 -07001624 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001625 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 break;
jeffhaobdb76512011-09-07 11:43:16 -07001627 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001628 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001629 break;
1630 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001631 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 break;
jeffhaobdb76512011-09-07 11:43:16 -07001633 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001634 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 break;
jeffhaobdb76512011-09-07 11:43:16 -07001636 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001637 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001638 break;
1639 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001640 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 break;
jeffhaobdb76512011-09-07 11:43:16 -07001642 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001643 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 break;
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001647 break;
1648
1649 /*
1650 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001651 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001652 * might want to hold the result in an actual CPU register, so the
1653 * Dalvik spec requires that these only appear immediately after an
1654 * invoke or filled-new-array.
1655 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001656 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001657 * redundant with the reset done below, but it can make the debug info
1658 * easier to read in some cases.)
1659 */
1660 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001662 break;
1663 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001664 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001665 break;
1666 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001667 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001668 break;
1669
Ian Rogersd81871c2011-10-03 13:57:23 -07001670 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001671 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1672 // where one entrypoint to the catch block is not actually an exception path.
1673 if (work_insn_idx_ == 0) {
1674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1675 break;
1676 }
jeffhaobdb76512011-09-07 11:43:16 -07001677 /*
jeffhao60f83e32012-02-13 17:16:30 -08001678 * This statement can only appear as the first instruction in an exception handler. We verify
1679 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001680 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001681 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001682 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001683 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 }
jeffhaobdb76512011-09-07 11:43:16 -07001685 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001686 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001687 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 }
jeffhaobdb76512011-09-07 11:43:16 -07001690 }
1691 break;
1692 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001693 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001694 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001695 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1698 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 } else {
1700 // Compilers may generate synthetic functions that write byte values into boolean fields.
1701 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001703 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1705 ((return_type.IsBoolean() || return_type.IsByte() ||
1706 return_type.IsShort() || return_type.IsChar()) &&
1707 src_type.IsInteger()));
1708 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001709 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001710 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001711 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 }
jeffhaobdb76512011-09-07 11:43:16 -07001714 }
1715 }
1716 break;
1717 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001718 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001719 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001720 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 } else {
1724 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001726 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001727 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 }
jeffhaobdb76512011-09-07 11:43:16 -07001730 }
1731 }
1732 break;
1733 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001734 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001735 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 } else {
1739 /* return_type is the *expected* return type, not register value */
1740 DCHECK(!return_type.IsZero());
1741 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001743 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001744 // Disallow returning uninitialized values and verify that the reference in vAA is an
1745 // instance of the "return_type"
1746 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001747 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1748 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001749 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001750 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1751 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1752 << "' or '" << reg_type << "'";
1753 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001754 bool soft_error = false;
1755 // Check whether arrays are involved. They will show a valid class status, even
1756 // if their components are erroneous.
1757 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1758 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1759 if (soft_error) {
1760 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1761 << reg_type << " vs " << return_type;
1762 }
1763 }
1764
1765 if (!soft_error) {
1766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1767 << "', but expected from declaration '" << return_type << "'";
1768 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001769 }
jeffhaobdb76512011-09-07 11:43:16 -07001770 }
1771 }
1772 }
1773 break;
1774
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001775 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 case Instruction::CONST_4: {
1777 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001779 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001780 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001781 }
1782 case Instruction::CONST_16: {
1783 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001784 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001785 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001786 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001788 case Instruction::CONST: {
1789 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001790 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001791 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001792 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001793 }
1794 case Instruction::CONST_HIGH16: {
1795 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001796 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001797 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001798 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001799 }
jeffhaobdb76512011-09-07 11:43:16 -07001800 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001803 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1804 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001806 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001807 }
1808 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001810 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1811 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001812 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001813 break;
1814 }
1815 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001816 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001817 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1818 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001819 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001820 break;
1821 }
1822 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001824 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1825 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001826 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001827 break;
1828 }
jeffhaobdb76512011-09-07 11:43:16 -07001829 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001830 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001831 break;
jeffhaobdb76512011-09-07 11:43:16 -07001832 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001833 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001834 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001836 // Get type from instruction if unresolved then we need an access check
1837 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001838 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001839 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001840 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001841 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001842 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001843 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 }
jeffhaobdb76512011-09-07 11:43:16 -07001845 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001846 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
1848 case Instruction::MONITOR_EXIT:
1849 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001850 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001851 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001852 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001853 * to the need to handle asynchronous exceptions, a now-deprecated
1854 * feature that Dalvik doesn't support.)
1855 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001856 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001857 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001858 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001859 * structured locking checks are working, the former would have
1860 * failed on the -enter instruction, and the latter is impossible.
1861 *
1862 * This is fortunate, because issue 3221411 prevents us from
1863 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001864 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001865 * some catch blocks (which will show up as "dead" code when
1866 * we skip them here); if we can't, then the code path could be
1867 * "live" so we still need to check it.
1868 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001869 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001870 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001871 break;
1872
Ian Rogers28ad40d2011-10-27 15:19:26 -07001873 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001875 /*
1876 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1877 * could be a "upcast" -- not expected, so we don't try to address it.)
1878 *
1879 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001880 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001881 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001882 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1883 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001884 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001885 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001886 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001887 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001888 if (klass != nullptr && klass->IsPrimitive()) {
1889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1890 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1891 << GetDeclaringClass();
1892 break;
1893 }
1894
Ian Rogersad0b3a32012-04-16 14:50:24 -07001895 DCHECK_NE(failures_.size(), 0U);
1896 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001898 }
1899 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001900 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001901 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001902 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001903 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001904 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001905 if (is_checkcast) {
1906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1907 } else {
1908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1909 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001910 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001911 if (is_checkcast) {
1912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1913 } else {
1914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1915 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001916 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001917 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001920 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001921 }
jeffhaobdb76512011-09-07 11:43:16 -07001922 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001923 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 }
1925 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001927 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001928 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001931 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001933 } else {
1934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 }
1936 break;
1937 }
1938 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001939 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001940 if (res_type.IsConflict()) {
1941 DCHECK_NE(failures_.size(), 0U);
1942 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001943 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001944 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1945 // can't create an instance of an interface or abstract class */
1946 if (!res_type.IsInstantiableTypes()) {
1947 Fail(VERIFY_ERROR_INSTANTIATION)
1948 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001949 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001951 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001952 // Any registers holding previous allocations from this address that have not yet been
1953 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001954 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001955 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 break;
1958 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001959 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001961 break;
1962 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001963 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001964 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001965 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001966 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001967 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001968 just_set_result = true; // Filled new array range sets result register
1969 break;
jeffhaobdb76512011-09-07 11:43:16 -07001970 case Instruction::CMPL_FLOAT:
1971 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001972 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001973 break;
1974 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001975 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001976 break;
1977 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001978 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001979 break;
1980 case Instruction::CMPL_DOUBLE:
1981 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001982 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001983 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001984 break;
1985 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001986 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001987 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001988 break;
1989 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001990 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
1992 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001993 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001994 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001995 break;
1996 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001997 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001998 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001999 break;
2000 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002001 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002002 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002004 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002005 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002006 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2007 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002008 }
2009 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 }
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::GOTO:
2012 case Instruction::GOTO_16:
2013 case Instruction::GOTO_32:
2014 /* no effect on or use of registers */
2015 break;
2016
2017 case Instruction::PACKED_SWITCH:
2018 case Instruction::SPARSE_SWITCH:
2019 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002020 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 case Instruction::FILL_ARRAY_DATA: {
2024 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002025 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002026 /* array_type can be null if the reg type is Zero */
2027 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002028 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2030 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002031 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002032 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002033 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002034 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2036 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 } else {
jeffhao457cc512012-02-02 16:55:13 -08002038 // Now verify if the element width in the table matches the element width declared in
2039 // the array
2040 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2041 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002043 } else {
2044 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2045 // Since we don't compress the data in Dex, expect to see equal width of data stored
2046 // in the table and expected from the array class.
2047 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2049 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002050 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 }
2052 }
jeffhaobdb76512011-09-07 11:43:16 -07002053 }
2054 }
2055 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 }
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002059 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2060 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 bool mismatch = false;
2062 if (reg_type1.IsZero()) { // zero then integral or reference expected
2063 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2064 } else if (reg_type1.IsReferenceTypes()) { // both references?
2065 mismatch = !reg_type2.IsReferenceTypes();
2066 } else { // both integral?
2067 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2068 }
2069 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2071 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002072 }
2073 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 }
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::IF_LT:
2076 case Instruction::IF_GE:
2077 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002079 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2080 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2083 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002084 }
2085 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 }
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002089 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2092 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002094
2095 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002096 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002097 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002098 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002099 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002100 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002101 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002102 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2103 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2104 work_insn_idx_)) {
2105 break;
2106 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002107 } else {
2108 break;
2109 }
2110
Ian Rogers9b360392013-06-06 14:45:07 -07002111 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002112
2113 /* Check for peep-hole pattern of:
2114 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002115 * instance-of vX, vY, T;
2116 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002117 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002118 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002119 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002120 * and sharpen the type of vY to be type T.
2121 * Note, this pattern can't be if:
2122 * - if there are other branches to this branch,
2123 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002124 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002125 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002126 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2127 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2128 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002129 // Check the type of the instance-of is different than that of registers type, as if they
2130 // are the same there is no work to be done here. Check that the conversion is not to or
2131 // from an unresolved type as type information is imprecise. If the instance-of is to an
2132 // interface then ignore the type information as interfaces can only be treated as Objects
2133 // and we don't want to disallow field and other operations on the object. If the value
2134 // being instance-of checked against is known null (zero) then allow the optimization as
2135 // we didn't have type information. If the merge of the instance-of type with the original
2136 // type is assignable to the original then allow optimization. This check is performed to
2137 // ensure that subsequent merges don't lose type information - such as becoming an
2138 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002139 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002140 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002141
Ian Rogersebbdd872014-07-07 23:53:08 -07002142 if (!orig_type.Equals(cast_type) &&
2143 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002144 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002145 !cast_type.GetClass()->IsInterface() &&
2146 (orig_type.IsZero() ||
2147 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002148 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002149 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002150 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002151 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002152 branch_line.reset(update_line);
2153 }
2154 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002155 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002156 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2157 // See if instance-of was preceded by a move-object operation, common due to the small
2158 // register encoding space of instance-of, and propagate type information to the source
2159 // of the move-object.
2160 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002161 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002162 move_idx--;
2163 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002164 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2165 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2166 work_insn_idx_)) {
2167 break;
2168 }
Ian Rogers9b360392013-06-06 14:45:07 -07002169 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2170 switch (move_inst->Opcode()) {
2171 case Instruction::MOVE_OBJECT:
2172 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002173 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002174 }
2175 break;
2176 case Instruction::MOVE_OBJECT_FROM16:
2177 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002178 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002179 }
2180 break;
2181 case Instruction::MOVE_OBJECT_16:
2182 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002183 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002184 }
2185 break;
2186 default:
2187 break;
2188 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002189 }
2190 }
2191 }
2192
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 }
jeffhaobdb76512011-09-07 11:43:16 -07002195 case Instruction::IF_LTZ:
2196 case Instruction::IF_GEZ:
2197 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002199 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2202 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 }
jeffhaobdb76512011-09-07 11:43:16 -07002204 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 }
jeffhaobdb76512011-09-07 11:43:16 -07002206 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002207 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 break;
jeffhaobdb76512011-09-07 11:43:16 -07002209 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 break;
jeffhaobdb76512011-09-07 11:43:16 -07002212 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 break;
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002217 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002219 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 break;
jeffhaobdb76512011-09-07 11:43:16 -07002221 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002222 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 break;
2224 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002229 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 break;
2231 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 break;
2234 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 break;
2237 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002239 break;
2240 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002241 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002242 break;
2243 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002244 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002245 break;
2246 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
2249
jeffhaobdb76512011-09-07 11:43:16 -07002250 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002251 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 break;
jeffhaobdb76512011-09-07 11:43:16 -07002253 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002254 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 break;
jeffhaobdb76512011-09-07 11:43:16 -07002256 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002257 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 break;
jeffhaobdb76512011-09-07 11:43:16 -07002259 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002260 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 break;
2262 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002263 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002266 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
2268 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002269 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2270 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 break;
jeffhaobdb76512011-09-07 11:43:16 -07002272
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002274 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 break;
2276 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002277 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 break;
2279 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002280 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 break;
2282 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002283 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002284 break;
2285 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002286 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002287 break;
2288 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002289 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 break;
jeffhaobdb76512011-09-07 11:43:16 -07002291 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002292 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2293 false);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002297 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
jeffhaobdb76512011-09-07 11:43:16 -07002299 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002300 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
jeffhaobdb76512011-09-07 11:43:16 -07002302 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002303 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 break;
jeffhaobdb76512011-09-07 11:43:16 -07002305 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002306 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 break;
2308 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002309 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002312 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002315 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2316 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
2318
2319 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002320 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 break;
2322 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002323 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 break;
2325 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002326 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 break;
2328 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002329 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002332 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002335 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002338 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2339 true);
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341
2342 case Instruction::INVOKE_VIRTUAL:
2343 case Instruction::INVOKE_VIRTUAL_RANGE:
2344 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2347 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002348 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2349 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002350 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2351 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002352 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002353 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002354 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002355 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002356 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002357 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002358 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2359 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002360 return_type_class->CannotBeAssignedFromOtherTypes());
2361 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002362 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2363 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002364 }
2365 }
2366 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002368 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2369 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002370 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002371 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002372 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002373 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002374 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002375 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002376 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002378 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 }
jeffhaobdb76512011-09-07 11:43:16 -07002381 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002384 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002385 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002386 const char* return_type_descriptor;
2387 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002388 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002389 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002391 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002392 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002393 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2394 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2395 } else {
2396 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002397 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002398 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002399 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002400 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002401 if (return_type_class != nullptr) {
2402 return_type = &reg_types_.FromClass(return_type_descriptor,
2403 return_type_class,
2404 return_type_class->CannotBeAssignedFromOtherTypes());
2405 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2407 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002408 }
Ian Rogers46685432012-06-03 22:26:43 -07002409 }
2410 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002411 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 * Some additional checks when calling a constructor. We know from the invocation arg check
2413 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2414 * that to require that called_method->klass is the same as this->klass or this->super,
2415 * allowing the latter only if the "this" argument is the same as the "this" argument to
2416 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002417 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002418 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002419 if (this_type.IsConflict()) // failure.
2420 break;
jeffhaobdb76512011-09-07 11:43:16 -07002421
jeffhaob57e9522012-04-26 18:08:21 -07002422 /* no null refs allowed (?) */
2423 if (this_type.IsZero()) {
2424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2425 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002426 }
jeffhaob57e9522012-04-26 18:08:21 -07002427
2428 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002429 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002430 // TODO: re-enable constructor type verification
2431 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002432 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002433 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2434 // break;
2435 // }
jeffhaob57e9522012-04-26 18:08:21 -07002436
2437 /* arg must be an uninitialized reference */
2438 if (!this_type.IsUninitializedTypes()) {
2439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2440 << this_type;
2441 break;
2442 }
2443
2444 /*
2445 * Replace the uninitialized reference with an initialized one. We need to do this for all
2446 * registers that have the same object instance in them, not just the "this" register.
2447 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002448 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002449 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002450 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002451 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2452 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002453 }
2454 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002456 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002457 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002458 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002459 just_set_result = true;
2460 break;
2461 }
2462 case Instruction::INVOKE_STATIC:
2463 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002464 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002465 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002466 METHOD_STATIC,
2467 is_range,
2468 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002469 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002470 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002471 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002472 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2473 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002474 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002475 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002476 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002477 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002478 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002479 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002480 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002481 } else {
2482 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2483 }
jeffhaobdb76512011-09-07 11:43:16 -07002484 just_set_result = true;
2485 }
2486 break;
jeffhaobdb76512011-09-07 11:43:16 -07002487 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002489 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002490 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002491 METHOD_INTERFACE,
2492 is_range,
2493 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002495 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002496 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2497 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2498 << PrettyMethod(abs_method) << "'";
2499 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002500 }
Ian Rogers0d604842012-04-16 14:50:24 -07002501 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002502 /* Get the type of the "this" arg, which should either be a sub-interface of called
2503 * interface or Object (see comments in RegType::JoinClass).
2504 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002506 if (this_type.IsZero()) {
2507 /* null pointer always passes (and always fails at runtime) */
2508 } else {
2509 if (this_type.IsUninitializedTypes()) {
2510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2511 << this_type;
2512 break;
2513 }
2514 // In the past we have tried to assert that "called_interface" is assignable
2515 // from "this_type.GetClass()", however, as we do an imprecise Join
2516 // (RegType::JoinClass) we don't have full information on what interfaces are
2517 // implemented by "this_type". For example, two classes may implement the same
2518 // interfaces and have a common parent that doesn't implement the interface. The
2519 // join will set "this_type" to the parent class and a test that this implements
2520 // the interface will incorrectly fail.
2521 }
2522 /*
2523 * We don't have an object instance, so we can't find the concrete method. However, all of
2524 * the type information is in the abstract method, so we're good.
2525 */
2526 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002529 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2530 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2531 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2532 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002533 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002534 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002535 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002536 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002537 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002538 } else {
2539 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2540 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002541 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002542 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 }
jeffhaobdb76512011-09-07 11:43:16 -07002544 case Instruction::NEG_INT:
2545 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::NEG_LONG:
2549 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002550 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002551 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002557 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002558 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002562 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002568 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002569 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
2571 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002572 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002573 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002577 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002580 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002581 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002582 break;
2583 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002584 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002588 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002592 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002595 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002596 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002597 break;
2598 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002599 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002600 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002601 break;
2602 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002603 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002604 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002607 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
2609 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002610 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002613 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615
2616 case Instruction::ADD_INT:
2617 case Instruction::SUB_INT:
2618 case Instruction::MUL_INT:
2619 case Instruction::REM_INT:
2620 case Instruction::DIV_INT:
2621 case Instruction::SHL_INT:
2622 case Instruction::SHR_INT:
2623 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002624 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002625 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::AND_INT:
2628 case Instruction::OR_INT:
2629 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002630 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002631 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::ADD_LONG:
2634 case Instruction::SUB_LONG:
2635 case Instruction::MUL_LONG:
2636 case Instruction::DIV_LONG:
2637 case Instruction::REM_LONG:
2638 case Instruction::AND_LONG:
2639 case Instruction::OR_LONG:
2640 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002641 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002642 reg_types_.LongLo(), reg_types_.LongHi(),
2643 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::SHL_LONG:
2646 case Instruction::SHR_LONG:
2647 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002649 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002650 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652 case Instruction::ADD_FLOAT:
2653 case Instruction::SUB_FLOAT:
2654 case Instruction::MUL_FLOAT:
2655 case Instruction::DIV_FLOAT:
2656 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2658 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::ADD_DOUBLE:
2661 case Instruction::SUB_DOUBLE:
2662 case Instruction::MUL_DOUBLE:
2663 case Instruction::DIV_DOUBLE:
2664 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2667 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002668 break;
2669 case Instruction::ADD_INT_2ADDR:
2670 case Instruction::SUB_INT_2ADDR:
2671 case Instruction::MUL_INT_2ADDR:
2672 case Instruction::REM_INT_2ADDR:
2673 case Instruction::SHL_INT_2ADDR:
2674 case Instruction::SHR_INT_2ADDR:
2675 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002676 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2677 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::AND_INT_2ADDR:
2680 case Instruction::OR_INT_2ADDR:
2681 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002682 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2683 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002684 break;
2685 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002686 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2687 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::ADD_LONG_2ADDR:
2690 case Instruction::SUB_LONG_2ADDR:
2691 case Instruction::MUL_LONG_2ADDR:
2692 case Instruction::DIV_LONG_2ADDR:
2693 case Instruction::REM_LONG_2ADDR:
2694 case Instruction::AND_LONG_2ADDR:
2695 case Instruction::OR_LONG_2ADDR:
2696 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002697 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 reg_types_.LongLo(), reg_types_.LongHi(),
2699 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::SHL_LONG_2ADDR:
2702 case Instruction::SHR_LONG_2ADDR:
2703 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002705 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::ADD_FLOAT_2ADDR:
2708 case Instruction::SUB_FLOAT_2ADDR:
2709 case Instruction::MUL_FLOAT_2ADDR:
2710 case Instruction::DIV_FLOAT_2ADDR:
2711 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002712 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2713 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715 case Instruction::ADD_DOUBLE_2ADDR:
2716 case Instruction::SUB_DOUBLE_2ADDR:
2717 case Instruction::MUL_DOUBLE_2ADDR:
2718 case Instruction::DIV_DOUBLE_2ADDR:
2719 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002720 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002721 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2722 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002725 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::MUL_INT_LIT16:
2727 case Instruction::DIV_INT_LIT16:
2728 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2730 true);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::AND_INT_LIT16:
2733 case Instruction::OR_INT_LIT16:
2734 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002735 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2736 true);
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::ADD_INT_LIT8:
2739 case Instruction::RSUB_INT_LIT8:
2740 case Instruction::MUL_INT_LIT8:
2741 case Instruction::DIV_INT_LIT8:
2742 case Instruction::REM_INT_LIT8:
2743 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002744 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002745 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002746 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2747 false);
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::AND_INT_LIT8:
2750 case Instruction::OR_INT_LIT8:
2751 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002752 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2753 false);
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002756 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002757 case Instruction::RETURN_VOID_NO_BARRIER:
2758 if (IsConstructor() && !IsStatic()) {
2759 auto& declaring_class = GetDeclaringClass();
2760 auto* klass = declaring_class.GetClass();
2761 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2762 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2764 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002765 break;
2766 }
2767 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002768 }
2769 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002770 // Note: the following instructions encode offsets derived from class linking.
2771 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2772 // meaning if the class linking and resolution were successful.
2773 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002774 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002775 break;
2776 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002777 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002778 break;
2779 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002780 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002781 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002782 case Instruction::IGET_BOOLEAN_QUICK:
2783 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2784 break;
2785 case Instruction::IGET_BYTE_QUICK:
2786 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2787 break;
2788 case Instruction::IGET_CHAR_QUICK:
2789 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2790 break;
2791 case Instruction::IGET_SHORT_QUICK:
2792 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2793 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002794 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002795 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002796 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002797 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002798 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002799 break;
2800 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002801 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002802 break;
2803 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002804 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002805 break;
2806 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002807 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002808 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002809 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002811 break;
2812 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002813 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002814 break;
2815 case Instruction::INVOKE_VIRTUAL_QUICK:
2816 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2817 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002818 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002819 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002820 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002821 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002822 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002823 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002824 } else {
2825 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2826 }
2827 just_set_result = true;
2828 }
2829 break;
2830 }
2831
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002833 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2834 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002835 case Instruction::UNUSED_79:
2836 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002838 break;
2839
2840 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002841 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002842 * complain if an instruction is missing (which is desirable).
2843 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002844 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002845
Stephen Kyle7e541c92014-12-17 17:10:02 +00002846 /*
2847 * If we are in a constructor, and we had an UninitializedThis type
2848 * in a register somewhere, we need to make sure it wasn't overwritten.
2849 */
2850 if (track_uninitialized_this) {
2851 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2852 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2853 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2854 was_invoke_direct)) {
2855 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2856 << "Constructor failed to initialize this object";
2857 }
2858 }
2859
Ian Rogersad0b3a32012-04-16 14:50:24 -07002860 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002861 if (Runtime::Current()->IsAotCompiler()) {
2862 /* When AOT compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002863 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002864 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002865 /* immediate failure, reject class */
2866 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2867 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002868 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002869 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002870 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002871 }
jeffhaobdb76512011-09-07 11:43:16 -07002872 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 * If we didn't just set the result register, clear it out. This ensures that you can only use
2874 * "move-result" immediately after the result is set. (We could check this statically, but it's
2875 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002876 */
2877 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002879 }
2880
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002881
jeffhaobdb76512011-09-07 11:43:16 -07002882
2883 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002884 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002885 *
2886 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002887 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002888 * somebody could get a reference field, check it for zero, and if the
2889 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002890 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002891 * that, and will reject the code.
2892 *
2893 * TODO: avoid re-fetching the branch target
2894 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002895 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002896 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002898 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002900 return false;
2901 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002902 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002903 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002904 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 }
jeffhaobdb76512011-09-07 11:43:16 -07002906 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002907 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002908 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002909 return false;
2910 }
2911 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002912 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002913 return false;
2914 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 }
jeffhaobdb76512011-09-07 11:43:16 -07002916 }
2917
2918 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002919 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002920 *
2921 * We've already verified that the table is structurally sound, so we
2922 * just need to walk through and tag the targets.
2923 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002924 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002925 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2926 const uint16_t* switch_insns = insns + offset_to_switch;
2927 int switch_count = switch_insns[1];
2928 int offset_to_targets, targ;
2929
2930 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2931 /* 0 = sig, 1 = count, 2/3 = first key */
2932 offset_to_targets = 4;
2933 } else {
2934 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002935 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002936 offset_to_targets = 2 + 2 * switch_count;
2937 }
2938
2939 /* verify each switch target */
2940 for (targ = 0; targ < switch_count; targ++) {
2941 int offset;
2942 uint32_t abs_offset;
2943
2944 /* offsets are 32-bit, and only partly endian-swapped */
2945 offset = switch_insns[offset_to_targets + targ * 2] |
2946 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 abs_offset = work_insn_idx_ + offset;
2948 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002949 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002950 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002951 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002952 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002953 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002954 }
jeffhaobdb76512011-09-07 11:43:16 -07002955 }
2956 }
2957
2958 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2960 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002961 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002962 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002963 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002964 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002965
Andreas Gampef91baf12014-07-18 15:41:00 -07002966 // Need the linker to try and resolve the handled class to check if it's Throwable.
2967 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2968
Ian Rogers0571d352011-11-03 19:51:38 -07002969 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002970 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2971 if (handler_type_idx == DexFile::kDexNoIndex16) {
2972 has_catch_all_handler = true;
2973 } else {
2974 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002975 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2976 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002977 if (klass != nullptr) {
2978 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2979 has_catch_all_handler = true;
2980 }
2981 } else {
2982 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002983 DCHECK(self_->IsExceptionPending());
2984 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002985 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002986 }
jeffhaobdb76512011-09-07 11:43:16 -07002987 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2989 * "work_regs", because at runtime the exception will be thrown before the instruction
2990 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002991 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002992 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002993 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002994 }
jeffhaobdb76512011-09-07 11:43:16 -07002995 }
2996
2997 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2999 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003000 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003001 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003002 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 * The state in work_line reflects the post-execution state. If the current instruction is a
3004 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003005 * it will do so before grabbing the lock).
3006 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003007 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003008 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003009 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003010 return false;
3011 }
3012 }
3013 }
3014
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003015 /* Handle "continue". Tag the next consecutive instruction.
3016 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3017 * because it changes work_line_ when performing peephole optimization
3018 * and this change should not be used in those cases.
3019 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003020 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003021 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3022 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003023 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3025 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003026 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003027 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3028 // next instruction isn't one.
3029 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3030 return false;
3031 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003033 // Make workline consistent with fallthrough computed from peephole optimization.
3034 work_line_->CopyFromLine(fallthrough_line.get());
3035 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003036 if (insn_flags_[next_insn_idx].IsReturn()) {
3037 // For returns we only care about the operand to the return, all other registers are dead.
3038 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3039 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003040 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003041 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003042 } else {
3043 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003045 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003046 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003047 }
3048 }
3049 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003050 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003052 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3053 // needed. If the merge changes the state of the registers then the work line will be
3054 // updated.
3055 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003056 return false;
3057 }
3058 } else {
3059 /*
3060 * We're not recording register data for the next instruction, so we don't know what the
3061 * prior state was. We have to assume that something has changed and re-evaluate it.
3062 */
3063 insn_flags_[next_insn_idx].SetChanged();
3064 }
3065 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003066
jeffhaod1f0fde2011-09-08 17:25:33 -07003067 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003068 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003069 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 return false;
3071 }
jeffhaobdb76512011-09-07 11:43:16 -07003072 }
3073
3074 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003075 * Update start_guess. Advance to the next instruction of that's
3076 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003077 * neither of those exists we're in a return or throw; leave start_guess
3078 * alone and let the caller sort it out.
3079 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003080 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003081 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3082 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003083 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003084 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003086 }
3087
Ian Rogersd81871c2011-10-03 13:57:23 -07003088 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3089 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003090
3091 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003092} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003093
Ian Rogersd8f69b02014-09-10 21:43:52 +00003094const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003095 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003096 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003097 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003098 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003099 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3100 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003101 if (result.IsConflict()) {
3102 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3103 << "' in " << referrer;
3104 return result;
3105 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003106 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003107 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003108 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003109 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003110 // check at runtime if access is allowed and so pass here. If result is
3111 // primitive, skip the access check.
3112 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3113 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003114 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003115 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003116 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003118}
3119
Ian Rogersd8f69b02014-09-10 21:43:52 +00003120const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003121 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003123 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003124 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3125 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003126 CatchHandlerIterator iterator(handlers_ptr);
3127 for (; iterator.HasNext(); iterator.Next()) {
3128 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3129 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003130 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003132 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003133 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003134 if (exception.IsUnresolvedTypes()) {
3135 // We don't know enough about the type. Fail here and let runtime handle it.
3136 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3137 return exception;
3138 } else {
3139 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3140 return reg_types_.Conflict();
3141 }
Jeff Haob878f212014-04-24 16:25:36 -07003142 } else if (common_super == nullptr) {
3143 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003144 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003145 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003146 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003147 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003148 if (FailOrAbort(this,
3149 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3150 "java.lang.Throwable is not assignable-from common_super at ",
3151 work_insn_idx_)) {
3152 break;
3153 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 }
3155 }
3156 }
3157 }
Ian Rogers0571d352011-11-03 19:51:38 -07003158 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 }
3160 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003161 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003162 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003163 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003164 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003166 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003167}
3168
Brian Carlstromea46f952013-07-30 01:26:50 -07003169mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003170 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003171 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003172 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003173 if (klass_type.IsConflict()) {
3174 std::string append(" in attempt to access method ");
3175 append += dex_file_->GetMethodName(method_id);
3176 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003177 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003178 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003179 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003180 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003181 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003182 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003183 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003184 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003185 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003186 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003187 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003188
3189 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003191 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 res_method = klass->FindInterfaceMethod(name, signature);
3193 } else {
3194 res_method = klass->FindVirtualMethod(name, signature);
3195 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003196 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003197 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003199 // If a virtual or interface method wasn't found with the expected type, look in
3200 // the direct methods. This can happen when the wrong invoke type is used or when
3201 // a class has changed, and will be flagged as an error in later checks.
3202 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3203 res_method = klass->FindDirectMethod(name, signature);
3204 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003206 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3207 << PrettyDescriptor(klass) << "." << name
3208 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003209 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003210 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 }
3212 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3214 // enforce them here.
3215 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3217 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003220 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003221 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3223 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003225 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003226 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003227 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003228 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003229 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003230 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003231 }
jeffhaode0d9c92012-02-27 13:58:13 -08003232 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3233 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3235 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003237 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003238 // Check that interface methods match interface classes.
3239 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3240 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3241 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003242 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003243 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3244 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3245 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003246 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003247 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 // See if the method type implied by the invoke instruction matches the access flags for the
3249 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003250 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003251 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3252 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3253 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003254 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3255 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003258 return res_method;
3259}
3260
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003261template <class T>
3262mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3263 MethodType method_type,
3264 bool is_range,
3265 mirror::ArtMethod* res_method) {
3266 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3267 // match the call to the signature. Also, we might be calling through an abstract method
3268 // definition (which doesn't have register count values).
3269 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3270 /* caught by static verifier */
3271 DCHECK(is_range || expected_args <= 5);
3272 if (expected_args > code_item_->outs_size_) {
3273 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3274 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3275 return nullptr;
3276 }
3277
3278 uint32_t arg[5];
3279 if (!is_range) {
3280 inst->GetVarArgs(arg);
3281 }
3282 uint32_t sig_registers = 0;
3283
3284 /*
3285 * Check the "this" argument, which must be an instance of the class that declared the method.
3286 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3287 * rigorous check here (which is okay since we have to do it at runtime).
3288 */
3289 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003290 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003291 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3292 CHECK(have_pending_hard_failure_);
3293 return nullptr;
3294 }
3295 if (actual_arg_type.IsUninitializedReference()) {
3296 if (res_method) {
3297 if (!res_method->IsConstructor()) {
3298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3299 return nullptr;
3300 }
3301 } else {
3302 // Check whether the name of the called method is "<init>"
3303 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003304 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3306 return nullptr;
3307 }
3308 }
3309 }
3310 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003311 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003312 if (res_method != nullptr) {
3313 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003314 std::string temp;
3315 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003316 klass->CannotBeAssignedFromOtherTypes());
3317 } else {
3318 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3319 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003320 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003321 dex_file_->StringByTypeIdx(class_idx),
3322 false);
3323 }
3324 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3325 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3326 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3327 << "' not instance of '" << *res_method_class << "'";
3328 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3329 // the compiler.
3330 if (have_pending_hard_failure_) {
3331 return nullptr;
3332 }
3333 }
3334 }
3335 sig_registers = 1;
3336 }
3337
3338 for ( ; it->HasNext(); it->Next()) {
3339 if (sig_registers >= expected_args) {
3340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3341 " arguments, found " << sig_registers << " or more.";
3342 return nullptr;
3343 }
3344
3345 const char* param_descriptor = it->GetDescriptor();
3346
3347 if (param_descriptor == nullptr) {
3348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3349 "component";
3350 return nullptr;
3351 }
3352
Ian Rogersd8f69b02014-09-10 21:43:52 +00003353 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003354 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3355 arg[sig_registers];
3356 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003357 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003358 if (!src_type.IsIntegralTypes()) {
3359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3360 << " but expected " << reg_type;
3361 return res_method;
3362 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003363 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003364 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3365 // compiler.
3366 if (have_pending_hard_failure_) {
3367 return res_method;
3368 }
3369 }
3370 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3371 }
3372 if (expected_args != sig_registers) {
3373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3374 " arguments, found " << sig_registers;
3375 return nullptr;
3376 }
3377 return res_method;
3378}
3379
3380void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3381 MethodType method_type,
3382 bool is_range) {
3383 // As the method may not have been resolved, make this static check against what we expect.
3384 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3385 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3386 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3387 DexFileParameterIterator it(*dex_file_,
3388 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3389 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3390 nullptr);
3391}
3392
3393class MethodParamListDescriptorIterator {
3394 public:
3395 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3396 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3397 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3398 }
3399
3400 bool HasNext() {
3401 return pos_ < params_size_;
3402 }
3403
3404 void Next() {
3405 ++pos_;
3406 }
3407
3408 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3409 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3410 }
3411
3412 private:
3413 mirror::ArtMethod* res_method_;
3414 size_t pos_;
3415 const DexFile::TypeList* params_;
3416 const size_t params_size_;
3417};
3418
Brian Carlstromea46f952013-07-30 01:26:50 -07003419mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003420 MethodType method_type,
3421 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003422 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003423 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3424 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003425 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003426
Brian Carlstromea46f952013-07-30 01:26:50 -07003427 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003428 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003429 // Check what we can statically.
3430 if (!have_pending_hard_failure_) {
3431 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3432 }
3433 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003434 }
3435
Ian Rogersd81871c2011-10-03 13:57:23 -07003436 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3437 // has a vtable entry for the target method.
3438 if (is_super) {
3439 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003440 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003441 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003442 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003443 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003444 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003445 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003446 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003447 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003448 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003449 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003450 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003451 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003452 << "." << res_method->GetName()
3453 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003454 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 }
3456 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003457
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003458 // Process the target method's signature. This signature may or may not
3459 MethodParamListDescriptorIterator it(res_method);
3460 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3461 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003462}
3463
Brian Carlstromea46f952013-07-30 01:26:50 -07003464mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -08003465 RegisterLine* reg_line, bool is_range,
3466 bool allow_failure) {
3467 if (is_range) {
3468 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3469 } else {
3470 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3471 }
3472 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003473 if (!actual_arg_type.HasClass()) {
3474 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003475 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003476 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003477 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003478 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003479 if (klass->IsInterface()) {
3480 // Derive Object.class from Class.class.getSuperclass().
3481 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003482 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003483 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003484 return nullptr;
3485 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003486 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003487 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003488 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003489 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003490 if (!dispatch_class->HasVTable()) {
3491 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3492 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003493 return nullptr;
3494 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003495 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003496 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3497 FailOrAbort(this, allow_failure,
3498 "Receiver class has not enough vtable slots for quickened invoke at ",
3499 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003500 return nullptr;
3501 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003502 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003503 if (self_->IsExceptionPending()) {
3504 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3505 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003506 return nullptr;
3507 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003508 return res_method;
3509}
3510
Brian Carlstromea46f952013-07-30 01:26:50 -07003511mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003512 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003513 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3514 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3515
Mathieu Chartier091d2382015-03-06 10:59:06 -08003516 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003517 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003519 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003520 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003521 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3522 work_insn_idx_)) {
3523 return nullptr;
3524 }
3525 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3526 work_insn_idx_)) {
3527 return nullptr;
3528 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003529
3530 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3531 // match the call to the signature. Also, we might be calling through an abstract method
3532 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003533 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003534 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003535 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003536 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003537 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3538 /* caught by static verifier */
3539 DCHECK(is_range || expected_args <= 5);
3540 if (expected_args > code_item_->outs_size_) {
3541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3542 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003543 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003544 }
3545
3546 /*
3547 * Check the "this" argument, which must be an instance of the class that declared the method.
3548 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3549 * rigorous check here (which is okay since we have to do it at runtime).
3550 */
3551 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003553 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003554 }
3555 if (!actual_arg_type.IsZero()) {
3556 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003557 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003558 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003559 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003560 klass->CannotBeAssignedFromOtherTypes());
3561 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003562 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3563 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003564 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003565 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003566 }
3567 }
3568 /*
3569 * Process the target method's signature. This signature may or may not
3570 * have been verified, so we can't assume it's properly formed.
3571 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003572 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003573 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003574 uint32_t arg[5];
3575 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003576 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003577 }
3578 size_t actual_args = 1;
3579 for (size_t param_index = 0; param_index < params_size; param_index++) {
3580 if (actual_args >= expected_args) {
3581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003582 << "'. Expected " << expected_args
3583 << " arguments, processing argument " << actual_args
3584 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003585 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003586 }
3587 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003588 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003589 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003591 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003592 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003593 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003594 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003595 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003596 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003597 return res_method;
3598 }
3599 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3600 }
3601 if (actual_args != expected_args) {
3602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3603 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003604 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003605 } else {
3606 return res_method;
3607 }
3608}
3609
Ian Rogers62342ec2013-06-11 10:26:37 -07003610void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003611 uint32_t type_idx;
3612 if (!is_filled) {
3613 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3614 type_idx = inst->VRegC_22c();
3615 } else if (!is_range) {
3616 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3617 type_idx = inst->VRegB_35c();
3618 } else {
3619 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3620 type_idx = inst->VRegB_3rc();
3621 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003622 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003623 if (res_type.IsConflict()) { // bad class
3624 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003625 } else {
3626 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3627 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003629 } else if (!is_filled) {
3630 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003631 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003632 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003633 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003634 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003635 } else {
3636 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3637 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003638 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003639 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3640 uint32_t arg[5];
3641 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003642 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003643 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003644 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003645 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003646 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3647 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003648 return;
3649 }
3650 }
3651 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003652 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003654 }
3655 }
3656}
3657
Sebastien Hertz5243e912013-05-21 10:55:07 +02003658void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003659 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003660 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003661 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003663 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003664 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003665 if (array_type.IsZero()) {
3666 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3667 // instruction type. TODO: have a proper notion of bottom here.
3668 if (!is_primitive || insn_type.IsCategory1Types()) {
3669 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003670 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003671 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003672 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003673 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3674 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003675 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003676 }
jeffhaofc3144e2012-02-01 17:21:15 -08003677 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003679 } else {
3680 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003681 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003682 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003684 << " source for aget-object";
3685 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003686 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003687 << " source for category 1 aget";
3688 } else if (is_primitive && !insn_type.Equals(component_type) &&
3689 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003690 (insn_type.IsLong() && component_type.IsDouble()))) {
3691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3692 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003693 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003694 // Use knowledge of the field type which is stronger than the type inferred from the
3695 // instruction, which can't differentiate object types and ints from floats, longs from
3696 // doubles.
3697 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003698 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003699 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003700 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003701 component_type.HighHalf(&reg_types_));
3702 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 }
3704 }
3705 }
3706}
3707
Ian Rogersd8f69b02014-09-10 21:43:52 +00003708void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003709 const uint32_t vregA) {
3710 // Primitive assignability rules are weaker than regular assignability rules.
3711 bool instruction_compatible;
3712 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003713 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003714 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003715 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003716 value_compatible = value_type.IsIntegralTypes();
3717 } else if (target_type.IsFloat()) {
3718 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3719 value_compatible = value_type.IsFloatTypes();
3720 } else if (target_type.IsLong()) {
3721 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003722 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3723 // as target_type depends on the resolved type of the field.
3724 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003726 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3727 } else {
3728 value_compatible = false;
3729 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003730 } else if (target_type.IsDouble()) {
3731 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003732 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3733 // as target_type depends on the resolved type of the field.
3734 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003735 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003736 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3737 } else {
3738 value_compatible = false;
3739 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003740 } else {
3741 instruction_compatible = false; // reference with primitive store
3742 value_compatible = false; // unused
3743 }
3744 if (!instruction_compatible) {
3745 // This is a global failure rather than a class change failure as the instructions and
3746 // the descriptors for the type should have been consistent within the same file at
3747 // compile time.
3748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3749 << "' but expected type '" << target_type << "'";
3750 return;
3751 }
3752 if (!value_compatible) {
3753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3754 << " of type " << value_type << " but expected " << target_type << " for put";
3755 return;
3756 }
3757}
3758
Sebastien Hertz5243e912013-05-21 10:55:07 +02003759void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003760 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003761 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003765 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003766 if (array_type.IsZero()) {
3767 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3768 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003769 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003771 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003772 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003773 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003774 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003775 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003777 if (!component_type.IsReferenceTypes()) {
3778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3779 << " source for aput-object";
3780 } else {
3781 // The instruction agrees with the type of array, confirm the value to be stored does too
3782 // Note: we use the instruction type (rather than the component type) for aput-object as
3783 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003784 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003785 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003786 }
3787 }
3788 }
3789}
3790
Mathieu Chartierc7853442015-03-27 14:35:38 -07003791ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003792 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3793 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003794 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003795 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003796 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3797 field_idx, dex_file_->GetFieldName(field_id),
3798 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003799 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003800 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003801 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003802 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003803 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003804 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003805 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3806 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003807 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003808 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003809 << dex_file_->GetFieldName(field_id) << ") in "
3810 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 DCHECK(self_->IsExceptionPending());
3812 self_->ClearException();
3813 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003814 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3815 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003817 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003818 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003819 } else if (!field->IsStatic()) {
3820 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003821 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003823 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003824}
3825
Mathieu Chartierc7853442015-03-27 14:35:38 -07003826ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003827 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3828 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003829 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003830 if (klass_type.IsConflict()) {
3831 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3832 field_idx, dex_file_->GetFieldName(field_id),
3833 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003834 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003835 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003836 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003837 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003838 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003839 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003840 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3841 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003842 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003843 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003844 << dex_file_->GetFieldName(field_id) << ") in "
3845 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003846 DCHECK(self_->IsExceptionPending());
3847 self_->ClearException();
3848 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003849 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3850 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003851 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003852 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003853 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 } else if (field->IsStatic()) {
3855 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3856 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003857 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 } else if (obj_type.IsZero()) {
3859 // Cannot infer and check type, however, access will cause null pointer exception
3860 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003861 } else if (!obj_type.IsReferenceTypes()) {
3862 // Trying to read a field from something that isn't a reference
3863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3864 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003865 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003866 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003867 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003868 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003869 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003870 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003871 if (obj_type.IsUninitializedTypes() &&
3872 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3873 !field_klass.Equals(GetDeclaringClass()))) {
3874 // Field accesses through uninitialized references are only allowable for constructors where
3875 // the field is declared in this class
3876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003877 << " of a not fully initialized object within the context"
3878 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003879 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003880 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3881 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3882 // of C1. For resolution to occur the declared class of the field must be compatible with
3883 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3884 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3885 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003886 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003887 } else {
3888 return field;
3889 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 }
3891}
3892
Andreas Gampe896df402014-10-20 22:25:29 -07003893template <MethodVerifier::FieldAccessType kAccType>
3894void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3895 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003896 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003897 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003898 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003899 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003900 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003901 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003902 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003903 if (UNLIKELY(have_pending_hard_failure_)) {
3904 return;
3905 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003906 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003907 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003909 if (kAccType == FieldAccessType::kAccPut) {
3910 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3911 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3912 << " from other class " << GetDeclaringClass();
3913 return;
3914 }
3915 }
3916
Mathieu Chartierc7853442015-03-27 14:35:38 -07003917 mirror::Class* field_type_class =
3918 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003919 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003920 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003921 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003922 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003923 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3924 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003925 }
Ian Rogers0d604842012-04-16 14:50:24 -07003926 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003927 if (field_type == nullptr) {
3928 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3929 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003930 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003931 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003932 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003933 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003934 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3935 "Unexpected third access type");
3936 if (kAccType == FieldAccessType::kAccPut) {
3937 // sput or iput.
3938 if (is_primitive) {
3939 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003940 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003941 if (!insn_type.IsAssignableFrom(*field_type)) {
3942 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3943 << " to be compatible with type '" << insn_type
3944 << "' but found type '" << *field_type
3945 << "' in put-object";
3946 return;
3947 }
3948 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003949 }
Andreas Gampe896df402014-10-20 22:25:29 -07003950 } else if (kAccType == FieldAccessType::kAccGet) {
3951 // sget or iget.
3952 if (is_primitive) {
3953 if (field_type->Equals(insn_type) ||
3954 (field_type->IsFloat() && insn_type.IsInteger()) ||
3955 (field_type->IsDouble() && insn_type.IsLong())) {
3956 // expected that read is of the correct primitive type or that int reads are reading
3957 // floats or long reads are reading doubles
3958 } else {
3959 // This is a global failure rather than a class change failure as the instructions and
3960 // the descriptors for the type should have been consistent within the same file at
3961 // compile time
3962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3963 << " to be of type '" << insn_type
3964 << "' but found type '" << *field_type << "' in get";
3965 return;
3966 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003967 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003968 if (!insn_type.IsAssignableFrom(*field_type)) {
3969 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3970 << " to be compatible with type '" << insn_type
3971 << "' but found type '" << *field_type
3972 << "' in get-object";
3973 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3974 return;
3975 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003976 }
Andreas Gampe896df402014-10-20 22:25:29 -07003977 if (!field_type->IsLowHalf()) {
3978 work_line_->SetRegisterType(this, vregA, *field_type);
3979 } else {
3980 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3981 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003982 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003983 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003984 }
3985}
3986
Mathieu Chartierc7853442015-03-27 14:35:38 -07003987ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003988 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003989 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07003990 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003991 if (!object_type.HasClass()) {
3992 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3993 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003994 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003995 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07003996 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003997 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003998 if (f == nullptr) {
3999 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4000 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4001 }
4002 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004003}
4004
Andreas Gampe896df402014-10-20 22:25:29 -07004005template <MethodVerifier::FieldAccessType kAccType>
4006void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4007 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004008 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004009
Mathieu Chartierc7853442015-03-27 14:35:38 -07004010 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004011 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4013 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004014 }
Andreas Gampe896df402014-10-20 22:25:29 -07004015
4016 // For an IPUT_QUICK, we now test for final flag of the field.
4017 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004018 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4019 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004020 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004021 return;
4022 }
4023 }
Andreas Gampe896df402014-10-20 22:25:29 -07004024
4025 // Get the field type.
4026 const RegType* field_type;
4027 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004028 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4029 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004030
4031 if (field_type_class != nullptr) {
4032 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4033 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004034 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004035 Thread* self = Thread::Current();
4036 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4037 self->ClearException();
4038 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4039 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004040 }
Andreas Gampe896df402014-10-20 22:25:29 -07004041 if (field_type == nullptr) {
4042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004043 return;
4044 }
Andreas Gampe896df402014-10-20 22:25:29 -07004045 }
4046
4047 const uint32_t vregA = inst->VRegA_22c();
4048 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4049 "Unexpected third access type");
4050 if (kAccType == FieldAccessType::kAccPut) {
4051 if (is_primitive) {
4052 // Primitive field assignability rules are weaker than regular assignability rules
4053 bool instruction_compatible;
4054 bool value_compatible;
4055 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4056 if (field_type->IsIntegralTypes()) {
4057 instruction_compatible = insn_type.IsIntegralTypes();
4058 value_compatible = value_type.IsIntegralTypes();
4059 } else if (field_type->IsFloat()) {
4060 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4061 value_compatible = value_type.IsFloatTypes();
4062 } else if (field_type->IsLong()) {
4063 instruction_compatible = insn_type.IsLong();
4064 value_compatible = value_type.IsLongTypes();
4065 } else if (field_type->IsDouble()) {
4066 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4067 value_compatible = value_type.IsDoubleTypes();
4068 } else {
4069 instruction_compatible = false; // reference field with primitive store
4070 value_compatible = false; // unused
4071 }
4072 if (!instruction_compatible) {
4073 // This is a global failure rather than a class change failure as the instructions and
4074 // the descriptors for the type should have been consistent within the same file at
4075 // compile time
4076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4077 << " to be of type '" << insn_type
4078 << "' but found type '" << *field_type
4079 << "' in put";
4080 return;
4081 }
4082 if (!value_compatible) {
4083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4084 << " of type " << value_type
4085 << " but expected " << *field_type
4086 << " for store to " << PrettyField(field) << " in put";
4087 return;
4088 }
4089 } else {
4090 if (!insn_type.IsAssignableFrom(*field_type)) {
4091 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4092 << " to be compatible with type '" << insn_type
4093 << "' but found type '" << *field_type
4094 << "' in put-object";
4095 return;
4096 }
4097 work_line_->VerifyRegisterType(this, vregA, *field_type);
4098 }
4099 } else if (kAccType == FieldAccessType::kAccGet) {
4100 if (is_primitive) {
4101 if (field_type->Equals(insn_type) ||
4102 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4103 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4104 // expected that read is of the correct primitive type or that int reads are reading
4105 // floats or long reads are reading doubles
4106 } else {
4107 // This is a global failure rather than a class change failure as the instructions and
4108 // the descriptors for the type should have been consistent within the same file at
4109 // compile time
4110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4111 << " to be of type '" << insn_type
4112 << "' but found type '" << *field_type << "' in Get";
4113 return;
4114 }
4115 } else {
4116 if (!insn_type.IsAssignableFrom(*field_type)) {
4117 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4118 << " to be compatible with type '" << insn_type
4119 << "' but found type '" << *field_type
4120 << "' in get-object";
4121 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4122 return;
4123 }
4124 }
4125 if (!field_type->IsLowHalf()) {
4126 work_line_->SetRegisterType(this, vregA, *field_type);
4127 } else {
4128 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004129 }
4130 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004131 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004132 }
4133}
4134
Ian Rogers776ac1f2012-04-13 23:36:36 -07004135bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004136 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004138 return false;
4139 }
4140 return true;
4141}
4142
Stephen Kyle9bc61992014-09-22 13:53:15 +01004143bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4144 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4145 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4147 return false;
4148 }
4149 return true;
4150}
4151
4152bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4153 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4154}
4155
Ian Rogersebbdd872014-07-07 23:53:08 -07004156bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4157 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004158 bool changed = true;
4159 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4160 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004161 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004162 * We haven't processed this instruction before, and we haven't touched the registers here, so
4163 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4164 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004165 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004166 if (!insn_flags_[next_insn].IsReturn()) {
4167 target_line->CopyFromLine(merge_line);
4168 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004169 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004170 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004171 return false;
4172 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004173 // For returns we only care about the operand to the return, all other registers are dead.
4174 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4175 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4176 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004177 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004178 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004179 } else {
4180 target_line->CopyFromLine(merge_line);
4181 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004182 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004183 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004184 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004185 }
4186 }
4187 }
jeffhaobdb76512011-09-07 11:43:16 -07004188 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004189 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004190 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004191 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004192 if (gDebugVerify) {
4193 copy->CopyFromLine(target_line);
4194 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004195 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004196 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004197 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004198 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004199 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004200 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004201 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004202 << copy->Dump(this) << " MERGE\n"
4203 << merge_line->Dump(this) << " ==\n"
4204 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004205 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004206 if (update_merge_line && changed) {
4207 merge_line->CopyFromLine(target_line);
4208 }
jeffhaobdb76512011-09-07 11:43:16 -07004209 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004210 if (changed) {
4211 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004212 }
4213 return true;
4214}
4215
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004216InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004217 return &insn_flags_[work_insn_idx_];
4218}
4219
Ian Rogersd8f69b02014-09-10 21:43:52 +00004220const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004221 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004222 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004223 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004224 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004225 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4226 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004227 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004228 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004229 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4230 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004231 }
4232 }
4233 if (return_type_ == nullptr) {
4234 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4235 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4236 uint16_t return_type_idx = proto_id.return_type_idx_;
4237 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004238 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004239 }
4240 }
4241 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004242}
4243
Ian Rogersd8f69b02014-09-10 21:43:52 +00004244const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004246 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004247 const char* descriptor
4248 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004249 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004250 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004251 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4252 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004253 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004254 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004255 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004256 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004257 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004258}
4259
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004260std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4261 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004262 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004263 std::vector<int32_t> result;
4264 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004265 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004266 if (type.IsConstant()) {
4267 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004268 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4269 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004270 } else if (type.IsConstantLo()) {
4271 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004272 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4273 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004274 } else if (type.IsConstantHi()) {
4275 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004276 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4277 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004278 } else if (type.IsIntegralTypes()) {
4279 result.push_back(kIntVReg);
4280 result.push_back(0);
4281 } else if (type.IsFloat()) {
4282 result.push_back(kFloatVReg);
4283 result.push_back(0);
4284 } else if (type.IsLong()) {
4285 result.push_back(kLongLoVReg);
4286 result.push_back(0);
4287 result.push_back(kLongHiVReg);
4288 result.push_back(0);
4289 ++i;
4290 } else if (type.IsDouble()) {
4291 result.push_back(kDoubleLoVReg);
4292 result.push_back(0);
4293 result.push_back(kDoubleHiVReg);
4294 result.push_back(0);
4295 ++i;
4296 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4297 result.push_back(kUndefined);
4298 result.push_back(0);
4299 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004300 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004301 result.push_back(kReferenceVReg);
4302 result.push_back(0);
4303 }
4304 }
4305 return result;
4306}
4307
Ian Rogersd8f69b02014-09-10 21:43:52 +00004308const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004309 if (precise) {
4310 // Precise constant type.
4311 return reg_types_.FromCat1Const(value, true);
4312 } else {
4313 // Imprecise constant type.
4314 if (value < -32768) {
4315 return reg_types_.IntConstant();
4316 } else if (value < -128) {
4317 return reg_types_.ShortConstant();
4318 } else if (value < 0) {
4319 return reg_types_.ByteConstant();
4320 } else if (value == 0) {
4321 return reg_types_.Zero();
4322 } else if (value == 1) {
4323 return reg_types_.One();
4324 } else if (value < 128) {
4325 return reg_types_.PosByteConstant();
4326 } else if (value < 32768) {
4327 return reg_types_.PosShortConstant();
4328 } else if (value < 65536) {
4329 return reg_types_.CharConstant();
4330 } else {
4331 return reg_types_.IntConstant();
4332 }
4333 }
4334}
4335
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004336void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004337 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004338}
4339
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004340void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004341 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004342}
jeffhaod1224c72012-02-29 13:43:08 -08004343
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004344void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4345 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004346}
4347
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004348void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4349 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004350}
4351
Ian Rogersd81871c2011-10-03 13:57:23 -07004352} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004353} // namespace art