blob: a48735b081180009818568d0f3233995b2d20318 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "mirror/art_field-inl.h"
34#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class.h"
36#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070037#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070040#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080045#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070050static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070051static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070052// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070053
Ian Rogers7b3ddd22013-02-21 15:19:52 -080054void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070055 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070056 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070057 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070058 register_lines_.reset(new RegisterLine*[insns_size]());
59 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070060 for (uint32_t i = 0; i < insns_size; i++) {
61 bool interesting = false;
62 switch (mode) {
63 case kTrackRegsAll:
64 interesting = flags[i].IsOpcode();
65 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070066 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070067 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070068 break;
69 case kTrackRegsBranches:
70 interesting = flags[i].IsBranchTarget();
71 break;
72 default:
73 break;
74 }
75 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070076 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
77 }
78 }
79}
80
81PcToRegisterLineTable::~PcToRegisterLineTable() {
82 for (size_t i = 0; i < size_; i++) {
83 delete register_lines_[i];
84 if (kIsDebugBuild) {
85 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070086 }
87 }
88}
89
Andreas Gampe7c038102014-10-27 20:08:46 -070090// Note: returns true on failure.
91ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
92 const char* error_msg, uint32_t work_insn_idx) {
93 if (kIsDebugBuild) {
94 // In a debug build, abort if the error condition is wrong.
95 DCHECK(condition) << error_msg << work_insn_idx;
96 } else {
97 // In a non-debug build, just fail the class.
98 if (!condition) {
99 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
100 return true;
101 }
102 }
103
104 return false;
105}
106
Stephen Kyle7e541c92014-12-17 17:10:02 +0000107static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
108 if (verifier->IsConstructor()) {
109 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
110 reg_line->CheckConstructorReturn(verifier);
111 }
112 reg_line->MarkAllRegistersAsConflicts(verifier);
113}
114
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
116 mirror::ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
117 Thread* self = Thread::Current();
118 StackHandleScope<3> hs(self);
119 mirror::Class* klass = method->GetDeclaringClass();
120 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
121 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
122 auto h_method = hs.NewHandle(method);
123 return VerifyMethod(self, method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), h_method,
125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700164}
165
Ian Rogers7b078e82014-09-10 14:44:24 -0700166MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
167 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700168 Handle<mirror::DexCache> dex_cache,
169 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700170 const DexFile::ClassDef* class_def,
171 bool allow_soft_failures,
172 std::string* error) {
173 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700174 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700175 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700178 }
jeffhaof56197c2012-03-05 18:01:54 -0800179 ClassDataItemIterator it(*dex_file, class_data);
180 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
181 it.Next();
182 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700184 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700185 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700186 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800187 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800189 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700190 if (method_idx == previous_direct_method_idx) {
191 // smali can create dex files with two encoded_methods sharing the same method_idx
192 // http://code.google.com/p/smali/issues/detail?id=119
193 it.Next();
194 continue;
195 }
196 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700197 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700198 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700199 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
200 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700202 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700203 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700204 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700206 StackHandleScope<1> hs(self);
207 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700208 MethodVerifier::FailureKind result = VerifyMethod(self,
209 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700210 dex_file,
211 dex_cache,
212 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700213 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700214 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700215 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700216 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700217 allow_soft_failures,
218 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (result != kNoFailure) {
220 if (result == kHardFailure) {
221 hard_fail = true;
222 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700224 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700225 *error = "Verifier rejected class ";
226 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
227 *error += " due to bad method ";
228 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700230 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800231 }
232 it.Next();
233 }
jeffhao9b0b1882012-10-01 16:51:22 -0700234 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800235 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700236 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800237 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700238 if (method_idx == previous_virtual_method_idx) {
239 // smali can create dex files with two encoded_methods sharing the same method_idx
240 // http://code.google.com/p/smali/issues/detail?id=119
241 it.Next();
242 continue;
243 }
244 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700245 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700246 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700247 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
248 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700249 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700250 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700251 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700252 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700253 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700254 StackHandleScope<1> hs(self);
255 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700256 MethodVerifier::FailureKind result = VerifyMethod(self,
257 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700258 dex_file,
259 dex_cache,
260 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700261 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700262 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700264 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700265 allow_soft_failures,
266 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 if (result != kNoFailure) {
268 if (result == kHardFailure) {
269 hard_fail = true;
270 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error = "Verifier rejected class ";
274 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
275 *error += " due to bad method ";
276 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700277 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700278 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800279 }
280 it.Next();
281 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700282 if (error_count == 0) {
283 return kNoFailure;
284 } else {
285 return hard_fail ? kHardFailure : kSoftFailure;
286 }
jeffhaof56197c2012-03-05 18:01:54 -0800287}
288
Ian Rogers7b078e82014-09-10 14:44:24 -0700289MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700291 Handle<mirror::DexCache> dex_cache,
292 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700293 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700295 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700296 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700297 bool allow_soft_failures,
298 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700299 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700300 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700303 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800304 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700305 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700306 // Verification completed, however failures may be pending that didn't cause the verification
307 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700308 CHECK(!verifier.have_pending_hard_failure_);
309 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700310 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700311 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700312 << PrettyMethod(method_idx, *dex_file) << "\n");
313 }
Ian Rogersc8982582012-09-07 16:53:25 -0700314 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800315 }
316 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700317 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 CHECK_NE(verifier.failures_.size(), 0U);
319 CHECK(verifier.have_pending_hard_failure_);
320 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700321 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800322 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700323 std::cout << "\n" << verifier.info_messages_.str();
324 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800325 }
Ian Rogersc8982582012-09-07 16:53:25 -0700326 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800327 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700328 if (kTimeVerifyMethod) {
329 uint64_t duration_ns = NanoTime() - start_ns;
330 if (duration_ns > MsToNs(100)) {
331 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
332 << " took " << PrettyDuration(duration_ns);
333 }
Ian Rogersc8982582012-09-07 16:53:25 -0700334 }
335 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800336}
337
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700338MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700339 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700340 Handle<mirror::DexCache> dex_cache,
341 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700342 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700344 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800345 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700346 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
347 class_def, code_item, dex_method_idx, method,
348 method_access_flags, true, true, true, true);
349 verifier->Verify();
350 verifier->DumpFailures(os);
351 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700352 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
353 // and querying any info is dangerous/can abort.
354 if (verifier->have_pending_hard_failure_) {
355 delete verifier;
356 return nullptr;
357 } else {
358 verifier->Dump(os);
359 return verifier;
360 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800361}
362
Ian Rogers7b078e82014-09-10 14:44:24 -0700363MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700364 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
365 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700366 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700367 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700368 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700369 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800370 bool need_precise_constants, bool verify_to_dump,
371 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700372 : self_(self),
373 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800374 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800375 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700376 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700377 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700378 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800379 dex_file_(dex_file),
380 dex_cache_(dex_cache),
381 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700382 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800383 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700384 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700385 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700386 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700387 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700388 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800389 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800390 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700391 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200392 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200394 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700395 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800396 verify_to_dump_(verify_to_dump),
397 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800398 Runtime::Current()->AddMethodVerifier(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 Chartierc528dba2013-11-26 12:00:11 -0800403 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700404 STLDeleteElements(&failure_messages_);
405}
406
Brian Carlstromea46f952013-07-30 01:26:50 -0700407void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700408 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700409 Thread* self = Thread::Current();
410 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700411 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
412 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700413 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700414 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700415 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800416 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700417 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700418 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700419 verifier.FindLocksAtDexPc();
420}
421
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700422static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
423 const Instruction* inst = Instruction::At(code_item->insns_);
424
425 uint32_t insns_size = code_item->insns_size_in_code_units_;
426 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
427 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
428 return true;
429 }
430
431 dex_pc += inst->SizeInCodeUnits();
432 inst = inst->Next();
433 }
434
435 return false;
436}
437
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700438void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700439 CHECK(monitor_enter_dex_pcs_ != nullptr);
440 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700442 // Quick check whether there are any monitor_enter instructions at all.
443 if (!HasMonitorEnterInstructions(code_item_)) {
444 return;
445 }
446
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700447 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
448 // verification. In practice, the phase we want relies on data structures set up by all the
449 // earlier passes, so we just run the full method verification and bail out early when we've
450 // got what we wanted.
451 Verify();
452}
453
Brian Carlstromea46f952013-07-30 01:26:50 -0700454mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700455 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700456 Thread* self = Thread::Current();
457 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700458 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
459 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700460 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700461 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700462 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800463 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200464 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200465}
466
Brian Carlstromea46f952013-07-30 01:26:50 -0700467mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200469
470 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
471 // verification. In practice, the phase we want relies on data structures set up by all the
472 // earlier passes, so we just run the full method verification and bail out early when we've
473 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200474 bool success = Verify();
475 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700476 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200477 }
478 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700480 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200481 }
482 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
483 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200484}
485
Brian Carlstromea46f952013-07-30 01:26:50 -0700486mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700487 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700488 Thread* self = Thread::Current();
489 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700490 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
491 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700492 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700493 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700494 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800495 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200496 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200497}
498
Brian Carlstromea46f952013-07-30 01:26:50 -0700499mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700500 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200501
502 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
503 // verification. In practice, the phase we want relies on data structures set up by all the
504 // earlier passes, so we just run the full method verification and bail out early when we've
505 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200506 bool success = Verify();
507 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200509 }
510 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700511 if (register_line == nullptr) {
512 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200513 }
514 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
515 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800516 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200517}
518
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700522 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700524 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 } else {
526 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700527 }
jeffhaobdb76512011-09-07 11:43:16 -0700528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
530 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
532 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700534 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800536 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 // Run through the instructions and see if the width checks out.
538 bool result = ComputeWidthsAndCountOps();
539 // Flag instructions guarded by a "try" block and check exception handlers.
540 result = result && ScanTryCatchBlocks();
541 // Perform static instruction verification.
542 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700543 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000544 result = result && VerifyCodeFlow();
545 // Compute information for compiler.
546 if (result && Runtime::Current()->IsCompiler()) {
547 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
548 }
549 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700550}
551
Ian Rogers776ac1f2012-04-13 23:36:36 -0700552std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553 switch (error) {
554 case VERIFY_ERROR_NO_CLASS:
555 case VERIFY_ERROR_NO_FIELD:
556 case VERIFY_ERROR_NO_METHOD:
557 case VERIFY_ERROR_ACCESS_CLASS:
558 case VERIFY_ERROR_ACCESS_FIELD:
559 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700560 case VERIFY_ERROR_INSTANTIATION:
561 case VERIFY_ERROR_CLASS_CHANGE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800562 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700563 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
564 // class change and instantiation errors into soft verification errors so that we re-verify
565 // at runtime. We may fail to find or to agree on access because of not yet available class
566 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
567 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
568 // paths" that dynamically perform the verification and cause the behavior to be that akin
569 // to an interpreter.
570 error = VERIFY_ERROR_BAD_CLASS_SOFT;
571 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700572 // If we fail again at runtime, mark that this instruction would throw and force this
573 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700574 have_pending_runtime_throw_failure_ = true;
575 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700576 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577 // Indication that verification should be retried at runtime.
578 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700579 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700580 have_pending_hard_failure_ = true;
581 }
582 break;
jeffhaod5347e02012-03-22 17:25:05 -0700583 // Hard verification failures at compile time will still fail at runtime, so the class is
584 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700585 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800586 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700587 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000588 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800589 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590 have_pending_hard_failure_ = true;
591 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800592 }
593 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700594 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700595 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700596 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700597 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700598 failure_messages_.push_back(failure_message);
599 return *failure_message;
600}
601
Ian Rogers576ca0c2014-06-06 15:58:22 -0700602std::ostream& MethodVerifier::LogVerifyInfo() {
603 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
604 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
605}
606
Ian Rogersad0b3a32012-04-16 14:50:24 -0700607void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
608 size_t failure_num = failure_messages_.size();
609 DCHECK_NE(failure_num, 0U);
610 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
611 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700612 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700613 delete last_fail_message;
614}
615
616void MethodVerifier::AppendToLastFailMessage(std::string append) {
617 size_t failure_num = failure_messages_.size();
618 DCHECK_NE(failure_num, 0U);
619 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
620 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800621}
622
Ian Rogers776ac1f2012-04-13 23:36:36 -0700623bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 const uint16_t* insns = code_item_->insns_;
625 size_t insns_size = code_item_->insns_size_in_code_units_;
626 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700627 size_t new_instance_count = 0;
628 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700630
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700632 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700633 switch (opcode) {
634 case Instruction::APUT_OBJECT:
635 case Instruction::CHECK_CAST:
636 has_check_casts_ = true;
637 break;
638 case Instruction::INVOKE_VIRTUAL:
639 case Instruction::INVOKE_VIRTUAL_RANGE:
640 case Instruction::INVOKE_INTERFACE:
641 case Instruction::INVOKE_INTERFACE_RANGE:
642 has_virtual_or_interface_invokes_ = true;
643 break;
644 case Instruction::MONITOR_ENTER:
645 monitor_enter_count++;
646 break;
647 case Instruction::NEW_INSTANCE:
648 new_instance_count++;
649 break;
650 default:
651 break;
jeffhaobdb76512011-09-07 11:43:16 -0700652 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700654 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700656 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700657 }
658
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
661 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700662 return false;
663 }
664
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 new_instance_count_ = new_instance_count;
666 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700667 return true;
668}
669
Ian Rogers776ac1f2012-04-13 23:36:36 -0700670bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700672 if (tries_size == 0) {
673 return true;
674 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700676 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700677
678 for (uint32_t idx = 0; idx < tries_size; idx++) {
679 const DexFile::TryItem* try_item = &tries[idx];
680 uint32_t start = try_item->start_addr_;
681 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700682 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
684 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700685 return false;
686 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700688 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
689 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700690 return false;
691 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700692 uint32_t dex_pc = start;
693 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
694 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700696 size_t insn_size = inst->SizeInCodeUnits();
697 dex_pc += insn_size;
698 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700699 }
700 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800701 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700702 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700703 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700704 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700705 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700706 CatchHandlerIterator iterator(handlers_ptr);
707 for (; iterator.HasNext(); iterator.Next()) {
708 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700710 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
711 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700712 return false;
713 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100714 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
715 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
716 << "exception handler begins with move-result* (" << dex_pc << ")";
717 return false;
718 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700720 // Ensure exception types are resolved so that they don't need resolution to be delivered,
721 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700722 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800723 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
724 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700725 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700726 if (exception_type == nullptr) {
727 DCHECK(self_->IsExceptionPending());
728 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700729 }
730 }
jeffhaobdb76512011-09-07 11:43:16 -0700731 }
Ian Rogers0571d352011-11-03 19:51:38 -0700732 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700733 }
jeffhaobdb76512011-09-07 11:43:16 -0700734 return true;
735}
736
Ian Rogers776ac1f2012-04-13 23:36:36 -0700737bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700739
Ian Rogers0c7abda2012-09-19 13:33:42 -0700740 /* 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 -0700741 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700742 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700743
744 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700745 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700747 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 return false;
749 }
750 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700751 // All invoke points are marked as "Throw" points already.
752 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000753 if (inst->IsBranch()) {
754 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
755 // The compiler also needs safepoints for fall-through to loop heads.
756 // Such a loop head must be a target of a branch.
757 int32_t offset = 0;
758 bool cond, self_ok;
759 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
760 DCHECK(target_ok);
761 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
762 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700763 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000764 } else if (inst->IsReturn()) {
765 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 }
767 dex_pc += inst->SizeInCodeUnits();
768 inst = inst->Next();
769 }
770 return true;
771}
772
Ian Rogers776ac1f2012-04-13 23:36:36 -0700773bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 bool result = true;
775 switch (inst->GetVerifyTypeArgumentA()) {
776 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700777 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 break;
779 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700780 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 break;
782 }
783 switch (inst->GetVerifyTypeArgumentB()) {
784 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700785 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 break;
787 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700788 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 break;
790 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700791 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 break;
793 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700794 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 break;
796 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700797 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 break;
799 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700800 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 break;
802 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700803 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 break;
805 }
806 switch (inst->GetVerifyTypeArgumentC()) {
807 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700808 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 break;
810 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700811 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 break;
813 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700814 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 break;
816 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700817 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 break;
819 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700820 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 break;
822 }
823 switch (inst->GetVerifyExtraFlags()) {
824 case Instruction::kVerifyArrayData:
825 result = result && CheckArrayData(code_offset);
826 break;
827 case Instruction::kVerifyBranchTarget:
828 result = result && CheckBranchTarget(code_offset);
829 break;
830 case Instruction::kVerifySwitchTargets:
831 result = result && CheckSwitchTargets(code_offset);
832 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700833 case Instruction::kVerifyVarArgNonZero:
834 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700835 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700836 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
838 "non-range invoke";
839 return false;
840 }
Ian Rogers29a26482014-05-02 15:27:29 -0700841 uint32_t args[Instruction::kMaxVarArgRegs];
842 inst->GetVarArgs(args);
843 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700845 }
Andreas Gampec3314312014-06-19 18:13:29 -0700846 case Instruction::kVerifyVarArgRangeNonZero:
847 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700849 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
850 inst->VRegA() <= 0) {
851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
852 "range invoke";
853 return false;
854 }
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 result = false;
860 break;
861 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800862 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
864 result = false;
865 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 return result;
867}
868
Ian Rogers7b078e82014-09-10 14:44:24 -0700869inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
872 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 return false;
874 }
875 return true;
876}
877
Ian Rogers7b078e82014-09-10 14:44:24 -0700878inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
881 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 return false;
883 }
884 return true;
885}
886
Ian Rogers7b078e82014-09-10 14:44:24 -0700887inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
890 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 return false;
892 }
893 return true;
894}
895
Ian Rogers7b078e82014-09-10 14:44:24 -0700896inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
899 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 return false;
901 }
902 return true;
903}
904
Ian Rogers7b078e82014-09-10 14:44:24 -0700905inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
908 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 return false;
910 }
911 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700912 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 return false;
916 }
917 return true;
918}
919
Ian Rogers7b078e82014-09-10 14:44:24 -0700920inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
923 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 return false;
925 }
926 return true;
927}
928
Ian Rogers7b078e82014-09-10 14:44:24 -0700929inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
932 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 return false;
934 }
935 return true;
936}
937
Ian Rogers776ac1f2012-04-13 23:36:36 -0700938bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
941 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 return false;
943 }
944 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700945 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 const char* cp = descriptor;
947 while (*cp++ == '[') {
948 bracket_count++;
949 }
950 if (bracket_count == 0) {
951 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700952 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
953 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 return false;
955 } else if (bracket_count > 255) {
956 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700957 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
958 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 return false;
960 }
961 return true;
962}
963
Ian Rogers776ac1f2012-04-13 23:36:36 -0700964bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
966 const uint16_t* insns = code_item_->insns_ + cur_offset;
967 const uint16_t* array_data;
968 int32_t array_data_offset;
969
970 DCHECK_LT(cur_offset, insn_count);
971 /* make sure the start of the array data table is in range */
972 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
973 if ((int32_t) cur_offset + array_data_offset < 0 ||
974 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700976 << ", data offset " << array_data_offset
977 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 return false;
979 }
980 /* offset to array data table is a relative branch-style offset */
981 array_data = insns + array_data_offset;
982 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800983 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
985 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 return false;
987 }
988 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700989 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
991 /* make sure the end of the switch is in range */
992 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
994 << ", data offset " << array_data_offset << ", end "
995 << cur_offset + array_data_offset + table_size
996 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 return false;
998 }
999 return true;
1000}
1001
Ian Rogers776ac1f2012-04-13 23:36:36 -07001002bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 int32_t offset;
1004 bool isConditional, selfOkay;
1005 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1006 return false;
1007 }
1008 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1010 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 return false;
1012 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001013 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1014 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1017 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 return false;
1019 }
1020 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1021 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001022 if (abs_offset < 0 ||
1023 (uint32_t) abs_offset >= insn_count ||
1024 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001026 << reinterpret_cast<void*>(abs_offset) << ") at "
1027 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return false;
1029 }
1030 insn_flags_[abs_offset].SetBranchTarget();
1031 return true;
1032}
1033
Ian Rogers776ac1f2012-04-13 23:36:36 -07001034bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 bool* selfOkay) {
1036 const uint16_t* insns = code_item_->insns_ + cur_offset;
1037 *pConditional = false;
1038 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001039 switch (*insns & 0xff) {
1040 case Instruction::GOTO:
1041 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001042 break;
1043 case Instruction::GOTO_32:
1044 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001045 *selfOkay = true;
1046 break;
1047 case Instruction::GOTO_16:
1048 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001049 break;
1050 case Instruction::IF_EQ:
1051 case Instruction::IF_NE:
1052 case Instruction::IF_LT:
1053 case Instruction::IF_GE:
1054 case Instruction::IF_GT:
1055 case Instruction::IF_LE:
1056 case Instruction::IF_EQZ:
1057 case Instruction::IF_NEZ:
1058 case Instruction::IF_LTZ:
1059 case Instruction::IF_GEZ:
1060 case Instruction::IF_GTZ:
1061 case Instruction::IF_LEZ:
1062 *pOffset = (int16_t) insns[1];
1063 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001064 break;
1065 default:
1066 return false;
1067 break;
1068 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001069 return true;
1070}
1071
Ian Rogers776ac1f2012-04-13 23:36:36 -07001072bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001074 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001076 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1078 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001080 << ", switch offset " << switch_offset
1081 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001082 return false;
1083 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001084 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001086 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001087 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1089 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001090 return false;
1091 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 uint32_t switch_count = switch_insns[1];
1093 int32_t keys_offset, targets_offset;
1094 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001095 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1096 /* 0=sig, 1=count, 2/3=firstKey */
1097 targets_offset = 4;
1098 keys_offset = -1;
1099 expected_signature = Instruction::kPackedSwitchSignature;
1100 } else {
1101 /* 0=sig, 1=count, 2..count*2 = keys */
1102 keys_offset = 2;
1103 targets_offset = 2 + 2 * switch_count;
1104 expected_signature = Instruction::kSparseSwitchSignature;
1105 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001107 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001108 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1109 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1110 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001111 return false;
1112 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 /* make sure the end of the switch is in range */
1114 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1116 << ", switch offset " << switch_offset
1117 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001118 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 return false;
1120 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001121 /* for a sparse switch, verify the keys are in ascending order */
1122 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1124 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1126 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1127 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1129 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 return false;
1131 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 last_key = key;
1133 }
1134 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001135 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 for (uint32_t targ = 0; targ < switch_count; targ++) {
1137 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1138 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1139 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001140 if (abs_offset < 0 ||
1141 abs_offset >= (int32_t) insn_count ||
1142 !insn_flags_[abs_offset].IsOpcode()) {
1143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1144 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1145 << reinterpret_cast<void*>(cur_offset)
1146 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001147 return false;
1148 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 insn_flags_[abs_offset].SetBranchTarget();
1150 }
1151 return true;
1152}
1153
Ian Rogers776ac1f2012-04-13 23:36:36 -07001154bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001155 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 return false;
1158 }
1159 uint16_t registers_size = code_item_->registers_size_;
1160 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001161 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1163 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 return false;
1165 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001166 }
1167
1168 return true;
1169}
1170
Ian Rogers776ac1f2012-04-13 23:36:36 -07001171bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 uint16_t registers_size = code_item_->registers_size_;
1173 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1174 // integer overflow when adding them here.
1175 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1177 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001178 return false;
1179 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001180 return true;
1181}
1182
Ian Rogers776ac1f2012-04-13 23:36:36 -07001183bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 uint16_t registers_size = code_item_->registers_size_;
1185 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001186
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001188 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1189 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 }
1191 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001192 reg_table_.Init(kTrackCompilerInterestPoints,
1193 insn_flags_.get(),
1194 insns_size,
1195 registers_size,
1196 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001197
jeffhaobdb76512011-09-07 11:43:16 -07001198
Ian Rogersd0fbd852013-09-24 18:17:04 -07001199 work_line_.reset(RegisterLine::Create(registers_size, this));
1200 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001201
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 /* Initialize register types of method arguments. */
1203 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001204 DCHECK_NE(failures_.size(), 0U);
1205 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001206 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001207 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 return false;
1209 }
1210 /* Perform code flow verification. */
1211 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001212 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001214 }
jeffhaobdb76512011-09-07 11:43:16 -07001215 return true;
1216}
1217
Ian Rogersad0b3a32012-04-16 14:50:24 -07001218std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1219 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001220 for (size_t i = 0; i < failures_.size(); ++i) {
1221 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001222 }
1223 return os;
1224}
1225
Ian Rogers776ac1f2012-04-13 23:36:36 -07001226void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001227 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001228 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 return;
jeffhaobdb76512011-09-07 11:43:16 -07001230 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001231 {
1232 os << "Register Types:\n";
1233 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1234 std::ostream indent_os(&indent_filter);
1235 reg_types_.Dump(indent_os);
1236 }
Ian Rogersb4903572012-10-11 11:52:56 -07001237 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001238 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1239 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 const Instruction* inst = Instruction::At(code_item_->insns_);
1241 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001242 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001244 if (reg_line != nullptr) {
1245 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001246 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001247 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001248 const bool kDumpHexOfInstruction = false;
1249 if (kDumpHexOfInstruction) {
1250 indent_os << inst->DumpHex(5) << " ";
1251 }
1252 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001253 inst = inst->Next();
1254 }
jeffhaobdb76512011-09-07 11:43:16 -07001255}
1256
Ian Rogersd81871c2011-10-03 13:57:23 -07001257static bool IsPrimitiveDescriptor(char descriptor) {
1258 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001259 case 'I':
1260 case 'C':
1261 case 'S':
1262 case 'B':
1263 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001264 case 'F':
1265 case 'D':
1266 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001268 default:
1269 return false;
1270 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001271}
1272
Ian Rogers776ac1f2012-04-13 23:36:36 -07001273bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 RegisterLine* reg_line = reg_table_.GetLine(0);
1275 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1276 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001277
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001279 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001281 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1283 // argument as uninitialized. This restricts field access until the superclass constructor is
1284 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001285 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001286 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001287 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 reg_types_.UninitializedThisArgument(declaring_class));
1289 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001290 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001291 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001293 }
1294
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001295 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001296 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001297 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001298
1299 for (; iterator.HasNext(); iterator.Next()) {
1300 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001301 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 LOG(FATAL) << "Null descriptor";
1303 }
1304 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1306 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 return false;
1308 }
1309 switch (descriptor[0]) {
1310 case 'L':
1311 case '[':
1312 // We assume that reference arguments are initialized. The only way it could be otherwise
1313 // (assuming the caller was verified) is if the current method is <init>, but in that case
1314 // it's effectively considered initialized the instant we reach here (in the sense that we
1315 // can return without doing anything or call virtual methods).
1316 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001317 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001318 if (!reg_type.IsNonZeroReferenceTypes()) {
1319 DCHECK(HasFailures());
1320 return false;
1321 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001322 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 }
1324 break;
1325 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001326 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 break;
1328 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001329 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 break;
1331 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001332 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 break;
1334 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001335 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 break;
1337 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001338 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 break;
1340 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001341 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 break;
1343 case 'J':
1344 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001345 if (cur_arg + 1 >= expected_args) {
1346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1347 << " args, found more (" << descriptor << ")";
1348 return false;
1349 }
1350
Ian Rogers7b078e82014-09-10 14:44:24 -07001351 const RegType* lo_half;
1352 const RegType* hi_half;
1353 if (descriptor[0] == 'J') {
1354 lo_half = &reg_types_.LongLo();
1355 hi_half = &reg_types_.LongHi();
1356 } else {
1357 lo_half = &reg_types_.DoubleLo();
1358 hi_half = &reg_types_.DoubleHi();
1359 }
1360 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 cur_arg++;
1362 break;
1363 }
1364 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1366 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 return false;
1368 }
1369 cur_arg++;
1370 }
1371 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1373 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 return false;
1375 }
1376 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1377 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1378 // format. Only major difference from the method argument format is that 'V' is supported.
1379 bool result;
1380 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1381 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001382 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 size_t i = 0;
1384 do {
1385 i++;
1386 } while (descriptor[i] == '['); // process leading [
1387 if (descriptor[i] == 'L') { // object array
1388 do {
1389 i++; // find closing ;
1390 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1391 result = descriptor[i] == ';';
1392 } else { // primitive array
1393 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1394 }
1395 } else if (descriptor[0] == 'L') {
1396 // could be more thorough here, but shouldn't be required
1397 size_t i = 0;
1398 do {
1399 i++;
1400 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1401 result = descriptor[i] == ';';
1402 } else {
1403 result = false;
1404 }
1405 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1407 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 }
1409 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001410}
1411
Ian Rogers776ac1f2012-04-13 23:36:36 -07001412bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 const uint16_t* insns = code_item_->insns_;
1414 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001415
jeffhaobdb76512011-09-07 11:43:16 -07001416 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 insn_flags_[0].SetChanged();
1418 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001419
jeffhaobdb76512011-09-07 11:43:16 -07001420 /* Continue until no instructions are marked "changed". */
1421 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001422 if (allow_thread_suspension_) {
1423 self_->AllowThreadSuspension();
1424 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1426 uint32_t insn_idx = start_guess;
1427 for (; insn_idx < insns_size; insn_idx++) {
1428 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001429 break;
1430 }
jeffhaobdb76512011-09-07 11:43:16 -07001431 if (insn_idx == insns_size) {
1432 if (start_guess != 0) {
1433 /* try again, starting from the top */
1434 start_guess = 0;
1435 continue;
1436 } else {
1437 /* all flags are clear */
1438 break;
1439 }
1440 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 // We carry the working set of registers from instruction to instruction. If this address can
1442 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1443 // "changed" flags, we need to load the set of registers from the table.
1444 // Because we always prefer to continue on to the next instruction, we should never have a
1445 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1446 // target.
1447 work_insn_idx_ = insn_idx;
1448 if (insn_flags_[insn_idx].IsBranchTarget()) {
1449 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001450 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001451 /*
1452 * Sanity check: retrieve the stored register line (assuming
1453 * a full table) and make sure it actually matches.
1454 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001456 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 if (work_line_->CompareLine(register_line) != 0) {
1458 Dump(std::cout);
1459 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001460 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001461 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001462 << " work_line=" << work_line_->Dump(this) << "\n"
1463 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 }
jeffhaobdb76512011-09-07 11:43:16 -07001465 }
jeffhaobdb76512011-09-07 11:43:16 -07001466 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001468 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001469 prepend += " failed to verify: ";
1470 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 return false;
1472 }
jeffhaobdb76512011-09-07 11:43:16 -07001473 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 insn_flags_[insn_idx].SetVisited();
1475 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001476 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001477
Ian Rogers1c849e52012-06-28 14:00:33 -07001478 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001479 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001480 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001481 * (besides the wasted space), but it indicates a flaw somewhere
1482 * down the line, possibly in the verifier.
1483 *
1484 * If we've substituted "always throw" instructions into the stream,
1485 * we are almost certainly going to have some dead code.
1486 */
1487 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001489 for (; insn_idx < insns_size;
1490 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001491 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001492 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001493 * may or may not be preceded by a padding NOP (for alignment).
1494 */
1495 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1496 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1497 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001498 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001499 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1500 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1501 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001503 }
1504
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001506 if (dead_start < 0)
1507 dead_start = insn_idx;
1508 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001509 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1510 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001511 dead_start = -1;
1512 }
1513 }
1514 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001515 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1516 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001517 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001518 // To dump the state of the verify after a method, do something like:
1519 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1520 // "boolean java.lang.String.equals(java.lang.Object)") {
1521 // LOG(INFO) << info_messages_.str();
1522 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001523 }
jeffhaobdb76512011-09-07 11:43:16 -07001524 return true;
1525}
1526
Ian Rogers776ac1f2012-04-13 23:36:36 -07001527bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001528 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1529 // We want the state _before_ the instruction, for the case where the dex pc we're
1530 // interested in is itself a monitor-enter instruction (which is a likely place
1531 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001532 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001533 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001534 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1535 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1536 }
1537 }
1538
jeffhaobdb76512011-09-07 11:43:16 -07001539 /*
1540 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001541 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001542 * control to another statement:
1543 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001544 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001545 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001546 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001547 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001548 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001549 * throw an exception that is handled by an encompassing "try"
1550 * block.
1551 *
1552 * We can also return, in which case there is no successor instruction
1553 * from this point.
1554 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001555 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001556 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1558 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001559 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001560
jeffhaobdb76512011-09-07 11:43:16 -07001561 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001562 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001563 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001565 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001566 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 }
jeffhaobdb76512011-09-07 11:43:16 -07001568
1569 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001570 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001571 * can throw an exception, we will copy/merge this into the "catch"
1572 * address rather than work_line, because we don't want the result
1573 * from the "successful" code path (e.g. a check-cast that "improves"
1574 * a type) to be visible to the exception handler.
1575 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001576 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001578 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001580 }
1581
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001582
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001583 // We need to ensure the work line is consistent while performing validation. When we spot a
1584 // peephole pattern we compute a new line for either the fallthrough instruction or the
1585 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001586 std::unique_ptr<RegisterLine> branch_line;
1587 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001588
Stephen Kyle7e541c92014-12-17 17:10:02 +00001589 /*
1590 * If we are in a constructor, and we currently have an UninitializedThis type
1591 * in a register somewhere, we need to make sure it isn't overwritten.
1592 */
1593 bool track_uninitialized_this = false;
1594 size_t uninitialized_this_loc = 0;
1595 if (IsConstructor()) {
1596 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1597 }
1598
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001600 case Instruction::NOP:
1601 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001602 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001603 * a signature that looks like a NOP; if we see one of these in
1604 * the course of executing code then we have a problem.
1605 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001608 }
1609 break;
1610
1611 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001612 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001613 break;
jeffhaobdb76512011-09-07 11:43:16 -07001614 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001615 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001616 break;
jeffhaobdb76512011-09-07 11:43:16 -07001617 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001618 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001619 break;
1620 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001621 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 break;
jeffhaobdb76512011-09-07 11:43:16 -07001623 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001624 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 break;
jeffhaobdb76512011-09-07 11:43:16 -07001626 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001627 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001628 break;
1629 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001630 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 break;
jeffhaobdb76512011-09-07 11:43:16 -07001632 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001633 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 break;
jeffhaobdb76512011-09-07 11:43:16 -07001635 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001636 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001637 break;
1638
1639 /*
1640 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001641 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001642 * might want to hold the result in an actual CPU register, so the
1643 * Dalvik spec requires that these only appear immediately after an
1644 * invoke or filled-new-array.
1645 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001646 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001647 * redundant with the reset done below, but it can make the debug info
1648 * easier to read in some cases.)
1649 */
1650 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001651 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001652 break;
1653 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001654 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001655 break;
1656 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001657 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001658 break;
1659
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001661 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1662 // where one entrypoint to the catch block is not actually an exception path.
1663 if (work_insn_idx_ == 0) {
1664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1665 break;
1666 }
jeffhaobdb76512011-09-07 11:43:16 -07001667 /*
jeffhao60f83e32012-02-13 17:16:30 -08001668 * This statement can only appear as the first instruction in an exception handler. We verify
1669 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001670 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001671 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001672 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001673 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 }
jeffhaobdb76512011-09-07 11:43:16 -07001675 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001676 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001677 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 }
jeffhaobdb76512011-09-07 11:43:16 -07001680 }
1681 break;
1682 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001684 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001685 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1688 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 } else {
1690 // Compilers may generate synthetic functions that write byte values into boolean fields.
1691 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001692 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001693 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1695 ((return_type.IsBoolean() || return_type.IsByte() ||
1696 return_type.IsShort() || return_type.IsChar()) &&
1697 src_type.IsInteger()));
1698 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001699 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001700 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001701 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 }
jeffhaobdb76512011-09-07 11:43:16 -07001704 }
1705 }
1706 break;
1707 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001708 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001709 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001710 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 } else {
1714 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001716 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001717 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 }
jeffhaobdb76512011-09-07 11:43:16 -07001720 }
1721 }
1722 break;
1723 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001724 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001725 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 } else {
1729 /* return_type is the *expected* return type, not register value */
1730 DCHECK(!return_type.IsZero());
1731 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001732 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001733 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001734 // Disallow returning uninitialized values and verify that the reference in vAA is an
1735 // instance of the "return_type"
1736 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001737 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1738 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001739 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001740 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1741 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1742 << "' or '" << reg_type << "'";
1743 } else {
1744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1745 << "', but expected from declaration '" << return_type << "'";
1746 }
jeffhaobdb76512011-09-07 11:43:16 -07001747 }
1748 }
1749 }
1750 break;
1751
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001752 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 case Instruction::CONST_4: {
1754 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001755 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001756 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001757 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001758 }
1759 case Instruction::CONST_16: {
1760 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001761 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001762 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001763 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001765 case Instruction::CONST: {
1766 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001767 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001768 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001770 }
1771 case Instruction::CONST_HIGH16: {
1772 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001773 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001774 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001776 }
jeffhaobdb76512011-09-07 11:43:16 -07001777 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001778 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001780 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1781 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001782 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001783 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001784 }
1785 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001787 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1788 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001789 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001790 break;
1791 }
1792 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001793 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001794 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1795 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001796 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 break;
1798 }
1799 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001801 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1802 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001803 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001804 break;
1805 }
jeffhaobdb76512011-09-07 11:43:16 -07001806 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001807 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 break;
jeffhaobdb76512011-09-07 11:43:16 -07001809 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001810 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001811 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001813 // Get type from instruction if unresolved then we need an access check
1814 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001815 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001816 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001817 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001818 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001819 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001820 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 }
jeffhaobdb76512011-09-07 11:43:16 -07001822 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001823 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001824 break;
1825 case Instruction::MONITOR_EXIT:
1826 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001827 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001828 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001829 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001830 * to the need to handle asynchronous exceptions, a now-deprecated
1831 * feature that Dalvik doesn't support.)
1832 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001833 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001834 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001835 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001836 * structured locking checks are working, the former would have
1837 * failed on the -enter instruction, and the latter is impossible.
1838 *
1839 * This is fortunate, because issue 3221411 prevents us from
1840 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001841 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001842 * some catch blocks (which will show up as "dead" code when
1843 * we skip them here); if we can't, then the code path could be
1844 * "live" so we still need to check it.
1845 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001846 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001847 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
1849
Ian Rogers28ad40d2011-10-27 15:19:26 -07001850 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001852 /*
1853 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1854 * could be a "upcast" -- not expected, so we don't try to address it.)
1855 *
1856 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001857 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001858 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001859 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1860 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001861 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001862 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001863 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001864 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001865 if (klass != nullptr && klass->IsPrimitive()) {
1866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1867 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1868 << GetDeclaringClass();
1869 break;
1870 }
1871
Ian Rogersad0b3a32012-04-16 14:50:24 -07001872 DCHECK_NE(failures_.size(), 0U);
1873 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001874 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001875 }
1876 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001877 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001878 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001879 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001881 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001882 if (is_checkcast) {
1883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1884 } else {
1885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1886 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001887 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001888 if (is_checkcast) {
1889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1890 } else {
1891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1892 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001893 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001894 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001895 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001898 }
jeffhaobdb76512011-09-07 11:43:16 -07001899 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001900 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 }
1902 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001903 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001904 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001905 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001910 } else {
1911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 }
1913 break;
1914 }
1915 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001916 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001917 if (res_type.IsConflict()) {
1918 DCHECK_NE(failures_.size(), 0U);
1919 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001920 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001921 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1922 // can't create an instance of an interface or abstract class */
1923 if (!res_type.IsInstantiableTypes()) {
1924 Fail(VERIFY_ERROR_INSTANTIATION)
1925 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001926 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001928 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001929 // Any registers holding previous allocations from this address that have not yet been
1930 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001931 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001932 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 break;
1935 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001936 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001938 break;
1939 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001940 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001941 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001942 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001943 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001944 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001945 just_set_result = true; // Filled new array range sets result register
1946 break;
jeffhaobdb76512011-09-07 11:43:16 -07001947 case Instruction::CMPL_FLOAT:
1948 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001949 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001950 break;
1951 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001953 break;
1954 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001955 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001956 break;
1957 case Instruction::CMPL_DOUBLE:
1958 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001960 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001961 break;
1962 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001963 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001964 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001965 break;
1966 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001967 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001968 break;
1969 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001970 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001971 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001972 break;
1973 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001974 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001975 reg_types_.LongHi())) {
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;
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001981 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001982 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001983 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1984 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001985 }
1986 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
jeffhaobdb76512011-09-07 11:43:16 -07001988 case Instruction::GOTO:
1989 case Instruction::GOTO_16:
1990 case Instruction::GOTO_32:
1991 /* no effect on or use of registers */
1992 break;
1993
1994 case Instruction::PACKED_SWITCH:
1995 case Instruction::SPARSE_SWITCH:
1996 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001997 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001998 break;
1999
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 case Instruction::FILL_ARRAY_DATA: {
2001 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002002 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002003 /* array_type can be null if the reg type is Zero */
2004 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002005 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2007 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002008 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002009 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002010 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002011 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2013 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 } else {
jeffhao457cc512012-02-02 16:55:13 -08002015 // Now verify if the element width in the table matches the element width declared in
2016 // the array
2017 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2018 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002020 } else {
2021 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2022 // Since we don't compress the data in Dex, expect to see equal width of data stored
2023 // in the table and expected from the array class.
2024 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2026 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002027 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 }
2029 }
jeffhaobdb76512011-09-07 11:43:16 -07002030 }
2031 }
2032 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 }
jeffhaobdb76512011-09-07 11:43:16 -07002034 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002036 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2037 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 bool mismatch = false;
2039 if (reg_type1.IsZero()) { // zero then integral or reference expected
2040 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2041 } else if (reg_type1.IsReferenceTypes()) { // both references?
2042 mismatch = !reg_type2.IsReferenceTypes();
2043 } else { // both integral?
2044 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2045 }
2046 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2048 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002049 }
2050 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 }
jeffhaobdb76512011-09-07 11:43:16 -07002052 case Instruction::IF_LT:
2053 case Instruction::IF_GE:
2054 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2057 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2060 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002061 }
2062 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 }
jeffhaobdb76512011-09-07 11:43:16 -07002064 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2069 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002071
2072 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002073 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002074 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002075 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002076 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002077 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002078 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002079 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2080 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2081 work_insn_idx_)) {
2082 break;
2083 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002084 } else {
2085 break;
2086 }
2087
Ian Rogers9b360392013-06-06 14:45:07 -07002088 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002089
2090 /* Check for peep-hole pattern of:
2091 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002092 * instance-of vX, vY, T;
2093 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002094 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002095 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002096 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002097 * and sharpen the type of vY to be type T.
2098 * Note, this pattern can't be if:
2099 * - if there are other branches to this branch,
2100 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002101 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002102 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002103 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2104 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2105 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002106 // Check the type of the instance-of is different than that of registers type, as if they
2107 // are the same there is no work to be done here. Check that the conversion is not to or
2108 // from an unresolved type as type information is imprecise. If the instance-of is to an
2109 // interface then ignore the type information as interfaces can only be treated as Objects
2110 // and we don't want to disallow field and other operations on the object. If the value
2111 // being instance-of checked against is known null (zero) then allow the optimization as
2112 // we didn't have type information. If the merge of the instance-of type with the original
2113 // type is assignable to the original then allow optimization. This check is performed to
2114 // ensure that subsequent merges don't lose type information - such as becoming an
2115 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002116 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002117 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002118
Ian Rogersebbdd872014-07-07 23:53:08 -07002119 if (!orig_type.Equals(cast_type) &&
2120 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002121 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002122 !cast_type.GetClass()->IsInterface() &&
2123 (orig_type.IsZero() ||
2124 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002125 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002126 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002127 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002128 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002129 branch_line.reset(update_line);
2130 }
2131 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002132 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002133 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2134 // See if instance-of was preceded by a move-object operation, common due to the small
2135 // register encoding space of instance-of, and propagate type information to the source
2136 // of the move-object.
2137 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002138 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002139 move_idx--;
2140 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002141 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2142 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2143 work_insn_idx_)) {
2144 break;
2145 }
Ian Rogers9b360392013-06-06 14:45:07 -07002146 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2147 switch (move_inst->Opcode()) {
2148 case Instruction::MOVE_OBJECT:
2149 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002150 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002151 }
2152 break;
2153 case Instruction::MOVE_OBJECT_FROM16:
2154 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002155 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002156 }
2157 break;
2158 case Instruction::MOVE_OBJECT_16:
2159 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002160 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002161 }
2162 break;
2163 default:
2164 break;
2165 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002166 }
2167 }
2168 }
2169
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 }
jeffhaobdb76512011-09-07 11:43:16 -07002172 case Instruction::IF_LTZ:
2173 case Instruction::IF_GEZ:
2174 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2179 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 }
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 }
jeffhaobdb76512011-09-07 11:43:16 -07002183 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002184 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 break;
jeffhaobdb76512011-09-07 11:43:16 -07002186 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 break;
jeffhaobdb76512011-09-07 11:43:16 -07002189 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002190 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 break;
jeffhaobdb76512011-09-07 11:43:16 -07002192 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002193 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002196 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 break;
jeffhaobdb76512011-09-07 11:43:16 -07002198 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 break;
2201 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002202 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
2204
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 break;
2208 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 break;
2211 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002212 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 break;
2214 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002215 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002218 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
2226
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002228 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 break;
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002231 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 break;
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002234 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 break;
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002237 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 break;
2239 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002240 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002243 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
2245 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002246 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2247 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 break;
jeffhaobdb76512011-09-07 11:43:16 -07002249
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002251 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 break;
2253 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002254 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 break;
2256 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002257 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 break;
2259 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002260 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002261 break;
2262 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002263 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002266 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 break;
jeffhaobdb76512011-09-07 11:43:16 -07002268 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002269 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2270 false);
jeffhaobdb76512011-09-07 11:43:16 -07002271 break;
2272
jeffhaobdb76512011-09-07 11:43:16 -07002273 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002274 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 break;
jeffhaobdb76512011-09-07 11:43:16 -07002276 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002277 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 break;
jeffhaobdb76512011-09-07 11:43:16 -07002279 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002280 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 break;
jeffhaobdb76512011-09-07 11:43:16 -07002282 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002283 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 break;
2285 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002286 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002287 break;
2288 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002289 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002290 break;
2291 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002292 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2293 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 break;
2295
2296 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002297 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
2299 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002300 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
2302 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002303 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 break;
2305 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002306 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002309 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002312 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002315 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2316 true);
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318
2319 case Instruction::INVOKE_VIRTUAL:
2320 case Instruction::INVOKE_VIRTUAL_RANGE:
2321 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2324 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002325 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2326 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002327 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2328 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002329 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002330 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002331 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002332 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002333 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002334 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002335 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2336 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002337 return_type_class->CannotBeAssignedFromOtherTypes());
2338 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002339 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2340 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002341 }
2342 }
2343 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002345 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2346 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002347 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002348 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002349 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002350 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002351 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002352 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002353 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002354 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002355 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 }
jeffhaobdb76512011-09-07 11:43:16 -07002358 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002361 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002362 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002363 const char* return_type_descriptor;
2364 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002365 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002366 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002368 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002369 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002370 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2371 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2372 } else {
2373 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002374 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002375 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002376 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002377 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002378 if (return_type_class != nullptr) {
2379 return_type = &reg_types_.FromClass(return_type_descriptor,
2380 return_type_class,
2381 return_type_class->CannotBeAssignedFromOtherTypes());
2382 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002383 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2384 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002385 }
Ian Rogers46685432012-06-03 22:26:43 -07002386 }
2387 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002388 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 * Some additional checks when calling a constructor. We know from the invocation arg check
2390 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2391 * that to require that called_method->klass is the same as this->klass or this->super,
2392 * allowing the latter only if the "this" argument is the same as the "this" argument to
2393 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002394 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002395 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002396 if (this_type.IsConflict()) // failure.
2397 break;
jeffhaobdb76512011-09-07 11:43:16 -07002398
jeffhaob57e9522012-04-26 18:08:21 -07002399 /* no null refs allowed (?) */
2400 if (this_type.IsZero()) {
2401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2402 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002403 }
jeffhaob57e9522012-04-26 18:08:21 -07002404
2405 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002406 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002407 // TODO: re-enable constructor type verification
2408 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002409 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002410 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2411 // break;
2412 // }
jeffhaob57e9522012-04-26 18:08:21 -07002413
2414 /* arg must be an uninitialized reference */
2415 if (!this_type.IsUninitializedTypes()) {
2416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2417 << this_type;
2418 break;
2419 }
2420
2421 /*
2422 * Replace the uninitialized reference with an initialized one. We need to do this for all
2423 * registers that have the same object instance in them, not just the "this" register.
2424 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002425 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002426 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002427 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002428 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2429 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002430 }
2431 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002433 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002434 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002435 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002436 just_set_result = true;
2437 break;
2438 }
2439 case Instruction::INVOKE_STATIC:
2440 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002441 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002442 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002443 METHOD_STATIC,
2444 is_range,
2445 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002446 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002447 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002448 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002449 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2450 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002451 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002452 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002453 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002454 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002455 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002456 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002457 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002458 } else {
2459 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2460 }
jeffhaobdb76512011-09-07 11:43:16 -07002461 just_set_result = true;
2462 }
2463 break;
jeffhaobdb76512011-09-07 11:43:16 -07002464 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002466 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002467 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002468 METHOD_INTERFACE,
2469 is_range,
2470 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002471 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002472 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002473 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2474 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2475 << PrettyMethod(abs_method) << "'";
2476 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002477 }
Ian Rogers0d604842012-04-16 14:50:24 -07002478 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002479 /* Get the type of the "this" arg, which should either be a sub-interface of called
2480 * interface or Object (see comments in RegType::JoinClass).
2481 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002483 if (this_type.IsZero()) {
2484 /* null pointer always passes (and always fails at runtime) */
2485 } else {
2486 if (this_type.IsUninitializedTypes()) {
2487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2488 << this_type;
2489 break;
2490 }
2491 // In the past we have tried to assert that "called_interface" is assignable
2492 // from "this_type.GetClass()", however, as we do an imprecise Join
2493 // (RegType::JoinClass) we don't have full information on what interfaces are
2494 // implemented by "this_type". For example, two classes may implement the same
2495 // interfaces and have a common parent that doesn't implement the interface. The
2496 // join will set "this_type" to the parent class and a test that this implements
2497 // the interface will incorrectly fail.
2498 }
2499 /*
2500 * We don't have an object instance, so we can't find the concrete method. However, all of
2501 * the type information is in the abstract method, so we're good.
2502 */
2503 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002504 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002505 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002506 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2507 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2508 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2509 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002510 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002511 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002512 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002513 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002514 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002515 } else {
2516 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2517 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002518 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 }
jeffhaobdb76512011-09-07 11:43:16 -07002521 case Instruction::NEG_INT:
2522 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
2525 case Instruction::NEG_LONG:
2526 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002528 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002531 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002534 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002535 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
2537 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002542 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002545 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002546 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002550 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002554 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556 case Instruction::LONG_TO_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_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002564 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002565 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::FLOAT_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_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
2571 case Instruction::DOUBLE_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_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002577 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002580 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002581 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002582 break;
2583 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002584 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002588 break;
2589 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002590 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002591 break;
2592
2593 case Instruction::ADD_INT:
2594 case Instruction::SUB_INT:
2595 case Instruction::MUL_INT:
2596 case Instruction::REM_INT:
2597 case Instruction::DIV_INT:
2598 case Instruction::SHL_INT:
2599 case Instruction::SHR_INT:
2600 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002601 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002602 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
2604 case Instruction::AND_INT:
2605 case Instruction::OR_INT:
2606 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002607 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002608 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::ADD_LONG:
2611 case Instruction::SUB_LONG:
2612 case Instruction::MUL_LONG:
2613 case Instruction::DIV_LONG:
2614 case Instruction::REM_LONG:
2615 case Instruction::AND_LONG:
2616 case Instruction::OR_LONG:
2617 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002618 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002619 reg_types_.LongLo(), reg_types_.LongHi(),
2620 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::SHL_LONG:
2623 case Instruction::SHR_LONG:
2624 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002626 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002627 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::ADD_FLOAT:
2630 case Instruction::SUB_FLOAT:
2631 case Instruction::MUL_FLOAT:
2632 case Instruction::DIV_FLOAT:
2633 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002634 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2635 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::ADD_DOUBLE:
2638 case Instruction::SUB_DOUBLE:
2639 case Instruction::MUL_DOUBLE:
2640 case Instruction::DIV_DOUBLE:
2641 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002642 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002643 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2644 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002645 break;
2646 case Instruction::ADD_INT_2ADDR:
2647 case Instruction::SUB_INT_2ADDR:
2648 case Instruction::MUL_INT_2ADDR:
2649 case Instruction::REM_INT_2ADDR:
2650 case Instruction::SHL_INT_2ADDR:
2651 case Instruction::SHR_INT_2ADDR:
2652 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002653 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2654 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::AND_INT_2ADDR:
2657 case Instruction::OR_INT_2ADDR:
2658 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002659 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2660 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002661 break;
2662 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002663 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2664 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002665 break;
2666 case Instruction::ADD_LONG_2ADDR:
2667 case Instruction::SUB_LONG_2ADDR:
2668 case Instruction::MUL_LONG_2ADDR:
2669 case Instruction::DIV_LONG_2ADDR:
2670 case Instruction::REM_LONG_2ADDR:
2671 case Instruction::AND_LONG_2ADDR:
2672 case Instruction::OR_LONG_2ADDR:
2673 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002674 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002675 reg_types_.LongLo(), reg_types_.LongHi(),
2676 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::SHL_LONG_2ADDR:
2679 case Instruction::SHR_LONG_2ADDR:
2680 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002681 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002682 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::ADD_FLOAT_2ADDR:
2685 case Instruction::SUB_FLOAT_2ADDR:
2686 case Instruction::MUL_FLOAT_2ADDR:
2687 case Instruction::DIV_FLOAT_2ADDR:
2688 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002689 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2690 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002691 break;
2692 case Instruction::ADD_DOUBLE_2ADDR:
2693 case Instruction::SUB_DOUBLE_2ADDR:
2694 case Instruction::MUL_DOUBLE_2ADDR:
2695 case Instruction::DIV_DOUBLE_2ADDR:
2696 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002697 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2699 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002702 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002703 case Instruction::MUL_INT_LIT16:
2704 case Instruction::DIV_INT_LIT16:
2705 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002706 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2707 true);
jeffhaobdb76512011-09-07 11:43:16 -07002708 break;
2709 case Instruction::AND_INT_LIT16:
2710 case Instruction::OR_INT_LIT16:
2711 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002712 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2713 true);
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715 case Instruction::ADD_INT_LIT8:
2716 case Instruction::RSUB_INT_LIT8:
2717 case Instruction::MUL_INT_LIT8:
2718 case Instruction::DIV_INT_LIT8:
2719 case Instruction::REM_INT_LIT8:
2720 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002721 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002722 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002723 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2724 false);
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
2726 case Instruction::AND_INT_LIT8:
2727 case Instruction::OR_INT_LIT8:
2728 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2730 false);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002733 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002734 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002735 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2737 }
2738 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002739 // Note: the following instructions encode offsets derived from class linking.
2740 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2741 // meaning if the class linking and resolution were successful.
2742 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002743 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002744 break;
2745 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002746 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002747 break;
2748 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002749 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002750 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002751 case Instruction::IGET_BOOLEAN_QUICK:
2752 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2753 break;
2754 case Instruction::IGET_BYTE_QUICK:
2755 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2756 break;
2757 case Instruction::IGET_CHAR_QUICK:
2758 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2759 break;
2760 case Instruction::IGET_SHORT_QUICK:
2761 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2762 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002763 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002764 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002765 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002766 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002767 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002768 break;
2769 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002770 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002771 break;
2772 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002773 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002774 break;
2775 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002776 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002777 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002778 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002779 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002780 break;
2781 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002782 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002783 break;
2784 case Instruction::INVOKE_VIRTUAL_QUICK:
2785 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2786 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002787 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002788 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002789 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002790 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002791 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002792 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002793 } else {
2794 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2795 }
2796 just_set_result = true;
2797 }
2798 break;
2799 }
2800
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002802 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2803 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002804 case Instruction::UNUSED_79:
2805 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002807 break;
2808
2809 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002810 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002811 * complain if an instruction is missing (which is desirable).
2812 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002813 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002814
Stephen Kyle7e541c92014-12-17 17:10:02 +00002815 /*
2816 * If we are in a constructor, and we had an UninitializedThis type
2817 * in a register somewhere, we need to make sure it wasn't overwritten.
2818 */
2819 if (track_uninitialized_this) {
2820 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2821 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2822 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2823 was_invoke_direct)) {
2824 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2825 << "Constructor failed to initialize this object";
2826 }
2827 }
2828
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002830 if (Runtime::Current()->IsAotCompiler()) {
2831 /* When AOT compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002832 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002833 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002834 /* immediate failure, reject class */
2835 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2836 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002837 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002838 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002839 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002840 }
jeffhaobdb76512011-09-07 11:43:16 -07002841 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 * If we didn't just set the result register, clear it out. This ensures that you can only use
2843 * "move-result" immediately after the result is set. (We could check this statically, but it's
2844 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002845 */
2846 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002847 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002848 }
2849
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002850
jeffhaobdb76512011-09-07 11:43:16 -07002851
2852 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002853 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002854 *
2855 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002856 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002857 * somebody could get a reference field, check it for zero, and if the
2858 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002859 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002860 * that, and will reject the code.
2861 *
2862 * TODO: avoid re-fetching the branch target
2863 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002864 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002865 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002867 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002869 return false;
2870 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002871 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002872 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002873 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 }
jeffhaobdb76512011-09-07 11:43:16 -07002875 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002876 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002877 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002878 return false;
2879 }
2880 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002881 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002882 return false;
2883 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 }
jeffhaobdb76512011-09-07 11:43:16 -07002885 }
2886
2887 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002888 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002889 *
2890 * We've already verified that the table is structurally sound, so we
2891 * just need to walk through and tag the targets.
2892 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002893 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002894 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2895 const uint16_t* switch_insns = insns + offset_to_switch;
2896 int switch_count = switch_insns[1];
2897 int offset_to_targets, targ;
2898
2899 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2900 /* 0 = sig, 1 = count, 2/3 = first key */
2901 offset_to_targets = 4;
2902 } else {
2903 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002904 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002905 offset_to_targets = 2 + 2 * switch_count;
2906 }
2907
2908 /* verify each switch target */
2909 for (targ = 0; targ < switch_count; targ++) {
2910 int offset;
2911 uint32_t abs_offset;
2912
2913 /* offsets are 32-bit, and only partly endian-swapped */
2914 offset = switch_insns[offset_to_targets + targ * 2] |
2915 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 abs_offset = work_insn_idx_ + offset;
2917 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002918 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002919 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002921 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002922 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002923 }
jeffhaobdb76512011-09-07 11:43:16 -07002924 }
2925 }
2926
2927 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2929 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002930 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002931 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002932 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002933 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002934
Andreas Gampef91baf12014-07-18 15:41:00 -07002935 // Need the linker to try and resolve the handled class to check if it's Throwable.
2936 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2937
Ian Rogers0571d352011-11-03 19:51:38 -07002938 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002939 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2940 if (handler_type_idx == DexFile::kDexNoIndex16) {
2941 has_catch_all_handler = true;
2942 } else {
2943 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002944 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2945 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002946 if (klass != nullptr) {
2947 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2948 has_catch_all_handler = true;
2949 }
2950 } else {
2951 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002952 DCHECK(self_->IsExceptionPending());
2953 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002954 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 }
jeffhaobdb76512011-09-07 11:43:16 -07002956 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2958 * "work_regs", because at runtime the exception will be thrown before the instruction
2959 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002960 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002961 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002962 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 }
jeffhaobdb76512011-09-07 11:43:16 -07002964 }
2965
2966 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002967 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2968 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002969 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002970 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002971 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 * The state in work_line reflects the post-execution state. If the current instruction is a
2973 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002974 * it will do so before grabbing the lock).
2975 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002976 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002977 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002978 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002979 return false;
2980 }
2981 }
2982 }
2983
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002984 /* Handle "continue". Tag the next consecutive instruction.
2985 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2986 * because it changes work_line_ when performing peephole optimization
2987 * and this change should not be used in those cases.
2988 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002989 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002990 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2991 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002992 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2994 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002995 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002996 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2997 // next instruction isn't one.
2998 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2999 return false;
3000 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003002 // Make workline consistent with fallthrough computed from peephole optimization.
3003 work_line_->CopyFromLine(fallthrough_line.get());
3004 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003005 if (insn_flags_[next_insn_idx].IsReturn()) {
3006 // For returns we only care about the operand to the return, all other registers are dead.
3007 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3008 Instruction::Code opcode = ret_inst->Opcode();
3009 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003010 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003011 } else {
3012 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003014 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003015 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003016 }
3017 }
3018 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003019 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003020 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003021 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3022 // needed. If the merge changes the state of the registers then the work line will be
3023 // updated.
3024 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003025 return false;
3026 }
3027 } else {
3028 /*
3029 * We're not recording register data for the next instruction, so we don't know what the
3030 * prior state was. We have to assume that something has changed and re-evaluate it.
3031 */
3032 insn_flags_[next_insn_idx].SetChanged();
3033 }
3034 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003035
jeffhaod1f0fde2011-09-08 17:25:33 -07003036 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003038 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 return false;
3040 }
jeffhaobdb76512011-09-07 11:43:16 -07003041 }
3042
3043 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003044 * Update start_guess. Advance to the next instruction of that's
3045 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003046 * neither of those exists we're in a return or throw; leave start_guess
3047 * alone and let the caller sort it out.
3048 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003049 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003050 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3051 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003052 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003053 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003055 }
3056
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3058 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003059
3060 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003061} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003062
Ian Rogersd8f69b02014-09-10 21:43:52 +00003063const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003064 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003065 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003066 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003068 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3069 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003070 if (result.IsConflict()) {
3071 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3072 << "' in " << referrer;
3073 return result;
3074 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003076 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003077 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003078 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003079 // check at runtime if access is allowed and so pass here. If result is
3080 // primitive, skip the access check.
3081 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3082 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003083 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003084 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003085 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003086 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003087}
3088
Ian Rogersd8f69b02014-09-10 21:43:52 +00003089const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003092 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003093 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3094 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003095 CatchHandlerIterator iterator(handlers_ptr);
3096 for (; iterator.HasNext(); iterator.Next()) {
3097 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3098 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003099 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003101 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003102 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003103 if (exception.IsUnresolvedTypes()) {
3104 // We don't know enough about the type. Fail here and let runtime handle it.
3105 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3106 return exception;
3107 } else {
3108 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3109 return reg_types_.Conflict();
3110 }
Jeff Haob878f212014-04-24 16:25:36 -07003111 } else if (common_super == nullptr) {
3112 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003113 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003114 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003116 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003117 if (FailOrAbort(this,
3118 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3119 "java.lang.Throwable is not assignable-from common_super at ",
3120 work_insn_idx_)) {
3121 break;
3122 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 }
3124 }
3125 }
3126 }
Ian Rogers0571d352011-11-03 19:51:38 -07003127 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 }
3129 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003132 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003133 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003135 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003136}
3137
Brian Carlstromea46f952013-07-30 01:26:50 -07003138mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003139 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003140 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003141 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003142 if (klass_type.IsConflict()) {
3143 std::string append(" in attempt to access method ");
3144 append += dex_file_->GetMethodName(method_id);
3145 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003146 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003147 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003148 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003150 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003151 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003152 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003153 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003155 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003156 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003157
3158 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003160 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 res_method = klass->FindInterfaceMethod(name, signature);
3162 } else {
3163 res_method = klass->FindVirtualMethod(name, signature);
3164 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003166 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003167 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003168 // If a virtual or interface method wasn't found with the expected type, look in
3169 // the direct methods. This can happen when the wrong invoke type is used or when
3170 // a class has changed, and will be flagged as an error in later checks.
3171 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3172 res_method = klass->FindDirectMethod(name, signature);
3173 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003174 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003175 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3176 << PrettyDescriptor(klass) << "." << name
3177 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003179 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 }
3181 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3183 // enforce them here.
3184 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3186 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003189 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003190 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3192 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003193 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003194 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003195 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003196 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003197 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003198 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003199 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003200 }
jeffhaode0d9c92012-02-27 13:58:13 -08003201 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3202 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3204 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003206 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003207 // Check that interface methods match interface classes.
3208 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3209 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3210 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003211 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003212 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3213 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3214 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003215 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003216 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 // See if the method type implied by the invoke instruction matches the access flags for the
3218 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003219 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3221 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3222 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003223 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3224 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003225 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003227 return res_method;
3228}
3229
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003230template <class T>
3231mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3232 MethodType method_type,
3233 bool is_range,
3234 mirror::ArtMethod* res_method) {
3235 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3236 // match the call to the signature. Also, we might be calling through an abstract method
3237 // definition (which doesn't have register count values).
3238 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3239 /* caught by static verifier */
3240 DCHECK(is_range || expected_args <= 5);
3241 if (expected_args > code_item_->outs_size_) {
3242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3243 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3244 return nullptr;
3245 }
3246
3247 uint32_t arg[5];
3248 if (!is_range) {
3249 inst->GetVarArgs(arg);
3250 }
3251 uint32_t sig_registers = 0;
3252
3253 /*
3254 * Check the "this" argument, which must be an instance of the class that declared the method.
3255 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3256 * rigorous check here (which is okay since we have to do it at runtime).
3257 */
3258 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003259 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003260 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3261 CHECK(have_pending_hard_failure_);
3262 return nullptr;
3263 }
3264 if (actual_arg_type.IsUninitializedReference()) {
3265 if (res_method) {
3266 if (!res_method->IsConstructor()) {
3267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3268 return nullptr;
3269 }
3270 } else {
3271 // Check whether the name of the called method is "<init>"
3272 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003273 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3275 return nullptr;
3276 }
3277 }
3278 }
3279 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003280 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003281 if (res_method != nullptr) {
3282 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003283 std::string temp;
3284 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003285 klass->CannotBeAssignedFromOtherTypes());
3286 } else {
3287 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3288 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003289 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003290 dex_file_->StringByTypeIdx(class_idx),
3291 false);
3292 }
3293 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3294 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3295 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3296 << "' not instance of '" << *res_method_class << "'";
3297 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3298 // the compiler.
3299 if (have_pending_hard_failure_) {
3300 return nullptr;
3301 }
3302 }
3303 }
3304 sig_registers = 1;
3305 }
3306
3307 for ( ; it->HasNext(); it->Next()) {
3308 if (sig_registers >= expected_args) {
3309 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3310 " arguments, found " << sig_registers << " or more.";
3311 return nullptr;
3312 }
3313
3314 const char* param_descriptor = it->GetDescriptor();
3315
3316 if (param_descriptor == nullptr) {
3317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3318 "component";
3319 return nullptr;
3320 }
3321
Ian Rogersd8f69b02014-09-10 21:43:52 +00003322 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003323 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3324 arg[sig_registers];
3325 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003326 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003327 if (!src_type.IsIntegralTypes()) {
3328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3329 << " but expected " << reg_type;
3330 return res_method;
3331 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003332 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003333 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3334 // compiler.
3335 if (have_pending_hard_failure_) {
3336 return res_method;
3337 }
3338 }
3339 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3340 }
3341 if (expected_args != sig_registers) {
3342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3343 " arguments, found " << sig_registers;
3344 return nullptr;
3345 }
3346 return res_method;
3347}
3348
3349void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3350 MethodType method_type,
3351 bool is_range) {
3352 // As the method may not have been resolved, make this static check against what we expect.
3353 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3354 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3355 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3356 DexFileParameterIterator it(*dex_file_,
3357 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3358 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3359 nullptr);
3360}
3361
3362class MethodParamListDescriptorIterator {
3363 public:
3364 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3365 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3366 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3367 }
3368
3369 bool HasNext() {
3370 return pos_ < params_size_;
3371 }
3372
3373 void Next() {
3374 ++pos_;
3375 }
3376
3377 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3378 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3379 }
3380
3381 private:
3382 mirror::ArtMethod* res_method_;
3383 size_t pos_;
3384 const DexFile::TypeList* params_;
3385 const size_t params_size_;
3386};
3387
Brian Carlstromea46f952013-07-30 01:26:50 -07003388mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003389 MethodType method_type,
3390 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003391 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003392 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3393 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003394 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003395
Brian Carlstromea46f952013-07-30 01:26:50 -07003396 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003397 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003398 // Check what we can statically.
3399 if (!have_pending_hard_failure_) {
3400 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3401 }
3402 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003403 }
3404
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3406 // has a vtable entry for the target method.
3407 if (is_super) {
3408 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003409 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003410 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003411 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003412 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003413 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003414 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003415 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003416 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003417 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003418 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003419 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003420 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003421 << "." << res_method->GetName()
3422 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003423 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003424 }
3425 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003426
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003427 // Process the target method's signature. This signature may or may not
3428 MethodParamListDescriptorIterator it(res_method);
3429 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3430 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003431}
3432
Brian Carlstromea46f952013-07-30 01:26:50 -07003433mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -08003434 RegisterLine* reg_line, bool is_range,
3435 bool allow_failure) {
3436 if (is_range) {
3437 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3438 } else {
3439 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3440 }
3441 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003442 if (!actual_arg_type.HasClass()) {
3443 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003444 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003445 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003446 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003447 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003448 if (klass->IsInterface()) {
3449 // Derive Object.class from Class.class.getSuperclass().
3450 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003451 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003452 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003453 return nullptr;
3454 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003455 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003456 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003457 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003458 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003459 if (!dispatch_class->HasVTable()) {
3460 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3461 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003462 return nullptr;
3463 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003464 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003465 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3466 FailOrAbort(this, allow_failure,
3467 "Receiver class has not enough vtable slots for quickened invoke at ",
3468 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003469 return nullptr;
3470 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003471 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003472 if (self_->IsExceptionPending()) {
3473 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3474 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003475 return nullptr;
3476 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003477 return res_method;
3478}
3479
Brian Carlstromea46f952013-07-30 01:26:50 -07003480mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003481 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003482 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3483 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3484
Mathieu Chartier091d2382015-03-06 10:59:06 -08003485 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003486 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003488 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003489 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003490 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3491 work_insn_idx_)) {
3492 return nullptr;
3493 }
3494 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3495 work_insn_idx_)) {
3496 return nullptr;
3497 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003498
3499 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3500 // match the call to the signature. Also, we might be calling through an abstract method
3501 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003502 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003503 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003504 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003505 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003506 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3507 /* caught by static verifier */
3508 DCHECK(is_range || expected_args <= 5);
3509 if (expected_args > code_item_->outs_size_) {
3510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3511 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003512 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003513 }
3514
3515 /*
3516 * Check the "this" argument, which must be an instance of the class that declared the method.
3517 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3518 * rigorous check here (which is okay since we have to do it at runtime).
3519 */
3520 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003522 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003523 }
3524 if (!actual_arg_type.IsZero()) {
3525 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003526 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003527 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003528 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003529 klass->CannotBeAssignedFromOtherTypes());
3530 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003531 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3532 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003533 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003534 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003535 }
3536 }
3537 /*
3538 * Process the target method's signature. This signature may or may not
3539 * have been verified, so we can't assume it's properly formed.
3540 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003541 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003542 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003543 uint32_t arg[5];
3544 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003545 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003546 }
3547 size_t actual_args = 1;
3548 for (size_t param_index = 0; param_index < params_size; param_index++) {
3549 if (actual_args >= expected_args) {
3550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003551 << "'. Expected " << expected_args
3552 << " arguments, processing argument " << actual_args
3553 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003554 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003555 }
3556 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003557 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003558 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003560 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003562 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003563 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003564 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003565 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003566 return res_method;
3567 }
3568 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3569 }
3570 if (actual_args != expected_args) {
3571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3572 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003573 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003574 } else {
3575 return res_method;
3576 }
3577}
3578
Ian Rogers62342ec2013-06-11 10:26:37 -07003579void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003580 uint32_t type_idx;
3581 if (!is_filled) {
3582 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3583 type_idx = inst->VRegC_22c();
3584 } else if (!is_range) {
3585 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3586 type_idx = inst->VRegB_35c();
3587 } else {
3588 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3589 type_idx = inst->VRegB_3rc();
3590 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003591 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003592 if (res_type.IsConflict()) { // bad class
3593 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003594 } else {
3595 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3596 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003598 } else if (!is_filled) {
3599 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003600 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003601 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003602 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003603 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003604 } else {
3605 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3606 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003607 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003608 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3609 uint32_t arg[5];
3610 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003611 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003612 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003613 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003614 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003615 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3616 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003617 return;
3618 }
3619 }
3620 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003621 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003622 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003623 }
3624 }
3625}
3626
Sebastien Hertz5243e912013-05-21 10:55:07 +02003627void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003628 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003629 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003630 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003632 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003633 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003634 if (array_type.IsZero()) {
3635 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3636 // instruction type. TODO: have a proper notion of bottom here.
3637 if (!is_primitive || insn_type.IsCategory1Types()) {
3638 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003639 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003641 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3643 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003644 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003645 }
jeffhaofc3144e2012-02-01 17:21:15 -08003646 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003648 } else {
3649 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003650 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003651 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003653 << " source for aget-object";
3654 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003656 << " source for category 1 aget";
3657 } else if (is_primitive && !insn_type.Equals(component_type) &&
3658 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003659 (insn_type.IsLong() && component_type.IsDouble()))) {
3660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3661 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003662 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003663 // Use knowledge of the field type which is stronger than the type inferred from the
3664 // instruction, which can't differentiate object types and ints from floats, longs from
3665 // doubles.
3666 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003667 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003668 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003670 component_type.HighHalf(&reg_types_));
3671 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 }
3673 }
3674 }
3675}
3676
Ian Rogersd8f69b02014-09-10 21:43:52 +00003677void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003678 const uint32_t vregA) {
3679 // Primitive assignability rules are weaker than regular assignability rules.
3680 bool instruction_compatible;
3681 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003682 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003683 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003684 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003685 value_compatible = value_type.IsIntegralTypes();
3686 } else if (target_type.IsFloat()) {
3687 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3688 value_compatible = value_type.IsFloatTypes();
3689 } else if (target_type.IsLong()) {
3690 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003691 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3692 // as target_type depends on the resolved type of the field.
3693 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003694 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003695 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3696 } else {
3697 value_compatible = false;
3698 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003699 } else if (target_type.IsDouble()) {
3700 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003701 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3702 // as target_type depends on the resolved type of the field.
3703 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003704 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003705 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3706 } else {
3707 value_compatible = false;
3708 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003709 } else {
3710 instruction_compatible = false; // reference with primitive store
3711 value_compatible = false; // unused
3712 }
3713 if (!instruction_compatible) {
3714 // This is a global failure rather than a class change failure as the instructions and
3715 // the descriptors for the type should have been consistent within the same file at
3716 // compile time.
3717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3718 << "' but expected type '" << target_type << "'";
3719 return;
3720 }
3721 if (!value_compatible) {
3722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3723 << " of type " << value_type << " but expected " << target_type << " for put";
3724 return;
3725 }
3726}
3727
Sebastien Hertz5243e912013-05-21 10:55:07 +02003728void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003729 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003730 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003731 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003733 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003734 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003735 if (array_type.IsZero()) {
3736 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3737 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003738 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003740 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003741 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003742 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003743 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003744 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003745 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003746 if (!component_type.IsReferenceTypes()) {
3747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3748 << " source for aput-object";
3749 } else {
3750 // The instruction agrees with the type of array, confirm the value to be stored does too
3751 // Note: we use the instruction type (rather than the component type) for aput-object as
3752 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003753 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003754 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003755 }
3756 }
3757 }
3758}
3759
Brian Carlstromea46f952013-07-30 01:26:50 -07003760mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003761 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3762 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003763 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003764 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003765 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3766 field_idx, dex_file_->GetFieldName(field_id),
3767 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003768 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003769 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003770 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003772 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003773 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003774 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3775 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003776 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003777 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003778 << dex_file_->GetFieldName(field_id) << ") in "
3779 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003780 DCHECK(self_->IsExceptionPending());
3781 self_->ClearException();
3782 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003783 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3784 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003786 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003787 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 } else if (!field->IsStatic()) {
3789 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003790 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003791 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003792 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003793}
3794
Ian Rogersd8f69b02014-09-10 21:43:52 +00003795mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003796 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3797 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003798 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003799 if (klass_type.IsConflict()) {
3800 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3801 field_idx, dex_file_->GetFieldName(field_id),
3802 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003803 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003804 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003805 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003806 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003807 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003808 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003809 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3810 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003812 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003813 << dex_file_->GetFieldName(field_id) << ") in "
3814 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 DCHECK(self_->IsExceptionPending());
3816 self_->ClearException();
3817 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003818 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3819 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003821 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003822 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 } else if (field->IsStatic()) {
3824 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3825 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003827 } else if (obj_type.IsZero()) {
3828 // Cannot infer and check type, however, access will cause null pointer exception
3829 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003830 } else if (!obj_type.IsReferenceTypes()) {
3831 // Trying to read a field from something that isn't a reference
3832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3833 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003834 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003835 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003836 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003837 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003838 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003839 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003840 if (obj_type.IsUninitializedTypes() &&
3841 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3842 !field_klass.Equals(GetDeclaringClass()))) {
3843 // Field accesses through uninitialized references are only allowable for constructors where
3844 // the field is declared in this class
3845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003846 << " of a not fully initialized object within the context"
3847 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003848 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003849 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3850 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3851 // of C1. For resolution to occur the declared class of the field must be compatible with
3852 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3853 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3854 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003855 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003856 } else {
3857 return field;
3858 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003859 }
3860}
3861
Andreas Gampe896df402014-10-20 22:25:29 -07003862template <MethodVerifier::FieldAccessType kAccType>
3863void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3864 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003865 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003866 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003867 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003868 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003869 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003870 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003871 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003872 if (UNLIKELY(have_pending_hard_failure_)) {
3873 return;
3874 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003875 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003876 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003877 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003878 if (kAccType == FieldAccessType::kAccPut) {
3879 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3880 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3881 << " from other class " << GetDeclaringClass();
3882 return;
3883 }
3884 }
3885
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003886 mirror::Class* field_type_class;
3887 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003888 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003889 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08003890 field_type_class = h_field->GetType(can_load_classes_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003891 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003892 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003893 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003894 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003895 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003896 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3897 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003898 }
Ian Rogers0d604842012-04-16 14:50:24 -07003899 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003900 if (field_type == nullptr) {
3901 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3902 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003903 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003904 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003905 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003906 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003907 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3908 "Unexpected third access type");
3909 if (kAccType == FieldAccessType::kAccPut) {
3910 // sput or iput.
3911 if (is_primitive) {
3912 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003913 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003914 if (!insn_type.IsAssignableFrom(*field_type)) {
3915 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3916 << " to be compatible with type '" << insn_type
3917 << "' but found type '" << *field_type
3918 << "' in put-object";
3919 return;
3920 }
3921 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003922 }
Andreas Gampe896df402014-10-20 22:25:29 -07003923 } else if (kAccType == FieldAccessType::kAccGet) {
3924 // sget or iget.
3925 if (is_primitive) {
3926 if (field_type->Equals(insn_type) ||
3927 (field_type->IsFloat() && insn_type.IsInteger()) ||
3928 (field_type->IsDouble() && insn_type.IsLong())) {
3929 // expected that read is of the correct primitive type or that int reads are reading
3930 // floats or long reads are reading doubles
3931 } else {
3932 // This is a global failure rather than a class change failure as the instructions and
3933 // the descriptors for the type should have been consistent within the same file at
3934 // compile time
3935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3936 << " to be of type '" << insn_type
3937 << "' but found type '" << *field_type << "' in get";
3938 return;
3939 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003940 } 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 get-object";
3946 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3947 return;
3948 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003949 }
Andreas Gampe896df402014-10-20 22:25:29 -07003950 if (!field_type->IsLowHalf()) {
3951 work_line_->SetRegisterType(this, vregA, *field_type);
3952 } else {
3953 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3954 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003955 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003956 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003957 }
3958}
3959
Brian Carlstromea46f952013-07-30 01:26:50 -07003960mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003961 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003962 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07003963 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003964 if (!object_type.HasClass()) {
3965 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3966 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003967 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003968 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003969 mirror::ArtField* const f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3970 field_offset);
3971 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003972 if (f == nullptr) {
3973 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3974 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3975 }
3976 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003977}
3978
Andreas Gampe896df402014-10-20 22:25:29 -07003979template <MethodVerifier::FieldAccessType kAccType>
3980void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
3981 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003982 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003983
Brian Carlstromea46f952013-07-30 01:26:50 -07003984 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003985 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3987 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003988 }
Andreas Gampe896df402014-10-20 22:25:29 -07003989
3990 // For an IPUT_QUICK, we now test for final flag of the field.
3991 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003992 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3993 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003994 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003995 return;
3996 }
3997 }
Andreas Gampe896df402014-10-20 22:25:29 -07003998
3999 // Get the field type.
4000 const RegType* field_type;
4001 {
4002 mirror::Class* field_type_class;
4003 {
4004 StackHandleScope<1> hs(Thread::Current());
4005 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08004006 field_type_class = h_field->GetType(can_load_classes_);
Andreas Gampe896df402014-10-20 22:25:29 -07004007 }
4008
4009 if (field_type_class != nullptr) {
4010 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4011 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004012 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004013 Thread* self = Thread::Current();
4014 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4015 self->ClearException();
4016 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4017 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004018 }
Andreas Gampe896df402014-10-20 22:25:29 -07004019 if (field_type == nullptr) {
4020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004021 return;
4022 }
Andreas Gampe896df402014-10-20 22:25:29 -07004023 }
4024
4025 const uint32_t vregA = inst->VRegA_22c();
4026 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4027 "Unexpected third access type");
4028 if (kAccType == FieldAccessType::kAccPut) {
4029 if (is_primitive) {
4030 // Primitive field assignability rules are weaker than regular assignability rules
4031 bool instruction_compatible;
4032 bool value_compatible;
4033 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4034 if (field_type->IsIntegralTypes()) {
4035 instruction_compatible = insn_type.IsIntegralTypes();
4036 value_compatible = value_type.IsIntegralTypes();
4037 } else if (field_type->IsFloat()) {
4038 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4039 value_compatible = value_type.IsFloatTypes();
4040 } else if (field_type->IsLong()) {
4041 instruction_compatible = insn_type.IsLong();
4042 value_compatible = value_type.IsLongTypes();
4043 } else if (field_type->IsDouble()) {
4044 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4045 value_compatible = value_type.IsDoubleTypes();
4046 } else {
4047 instruction_compatible = false; // reference field with primitive store
4048 value_compatible = false; // unused
4049 }
4050 if (!instruction_compatible) {
4051 // This is a global failure rather than a class change failure as the instructions and
4052 // the descriptors for the type should have been consistent within the same file at
4053 // compile time
4054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4055 << " to be of type '" << insn_type
4056 << "' but found type '" << *field_type
4057 << "' in put";
4058 return;
4059 }
4060 if (!value_compatible) {
4061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4062 << " of type " << value_type
4063 << " but expected " << *field_type
4064 << " for store to " << PrettyField(field) << " in put";
4065 return;
4066 }
4067 } else {
4068 if (!insn_type.IsAssignableFrom(*field_type)) {
4069 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4070 << " to be compatible with type '" << insn_type
4071 << "' but found type '" << *field_type
4072 << "' in put-object";
4073 return;
4074 }
4075 work_line_->VerifyRegisterType(this, vregA, *field_type);
4076 }
4077 } else if (kAccType == FieldAccessType::kAccGet) {
4078 if (is_primitive) {
4079 if (field_type->Equals(insn_type) ||
4080 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4081 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4082 // expected that read is of the correct primitive type or that int reads are reading
4083 // floats or long reads are reading doubles
4084 } else {
4085 // This is a global failure rather than a class change failure as the instructions and
4086 // the descriptors for the type should have been consistent within the same file at
4087 // compile time
4088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4089 << " to be of type '" << insn_type
4090 << "' but found type '" << *field_type << "' in Get";
4091 return;
4092 }
4093 } else {
4094 if (!insn_type.IsAssignableFrom(*field_type)) {
4095 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4096 << " to be compatible with type '" << insn_type
4097 << "' but found type '" << *field_type
4098 << "' in get-object";
4099 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4100 return;
4101 }
4102 }
4103 if (!field_type->IsLowHalf()) {
4104 work_line_->SetRegisterType(this, vregA, *field_type);
4105 } else {
4106 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004107 }
4108 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004109 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004110 }
4111}
4112
Ian Rogers776ac1f2012-04-13 23:36:36 -07004113bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004114 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004116 return false;
4117 }
4118 return true;
4119}
4120
Stephen Kyle9bc61992014-09-22 13:53:15 +01004121bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4122 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4123 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4125 return false;
4126 }
4127 return true;
4128}
4129
4130bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4131 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4132}
4133
Ian Rogersebbdd872014-07-07 23:53:08 -07004134bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4135 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004136 bool changed = true;
4137 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4138 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004139 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004140 * We haven't processed this instruction before, and we haven't touched the registers here, so
4141 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4142 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004143 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004144 if (!insn_flags_[next_insn].IsReturn()) {
4145 target_line->CopyFromLine(merge_line);
4146 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004147 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004148 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004149 return false;
4150 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004151 // For returns we only care about the operand to the return, all other registers are dead.
4152 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4153 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4154 Instruction::Code opcode = ret_inst->Opcode();
4155 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004156 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004157 } else {
4158 target_line->CopyFromLine(merge_line);
4159 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004160 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004161 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004162 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004163 }
4164 }
4165 }
jeffhaobdb76512011-09-07 11:43:16 -07004166 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004167 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004168 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004169 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004170 if (gDebugVerify) {
4171 copy->CopyFromLine(target_line);
4172 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004173 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004174 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004175 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004176 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004177 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004178 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004179 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004180 << copy->Dump(this) << " MERGE\n"
4181 << merge_line->Dump(this) << " ==\n"
4182 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004183 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004184 if (update_merge_line && changed) {
4185 merge_line->CopyFromLine(target_line);
4186 }
jeffhaobdb76512011-09-07 11:43:16 -07004187 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004188 if (changed) {
4189 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004190 }
4191 return true;
4192}
4193
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004194InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004195 return &insn_flags_[work_insn_idx_];
4196}
4197
Ian Rogersd8f69b02014-09-10 21:43:52 +00004198const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004199 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004200 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004201 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004202 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004203 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4204 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004205 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004206 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004207 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4208 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004209 }
4210 }
4211 if (return_type_ == nullptr) {
4212 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4213 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4214 uint16_t return_type_idx = proto_id.return_type_idx_;
4215 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004216 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004217 }
4218 }
4219 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004220}
4221
Ian Rogersd8f69b02014-09-10 21:43:52 +00004222const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004223 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004224 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004225 const char* descriptor
4226 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004227 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004228 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004229 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4230 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004231 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004232 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004233 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004234 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004235 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004236}
4237
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004238std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4239 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004240 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004241 std::vector<int32_t> result;
4242 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004243 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004244 if (type.IsConstant()) {
4245 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004246 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4247 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004248 } else if (type.IsConstantLo()) {
4249 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004250 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4251 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004252 } else if (type.IsConstantHi()) {
4253 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004254 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4255 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004256 } else if (type.IsIntegralTypes()) {
4257 result.push_back(kIntVReg);
4258 result.push_back(0);
4259 } else if (type.IsFloat()) {
4260 result.push_back(kFloatVReg);
4261 result.push_back(0);
4262 } else if (type.IsLong()) {
4263 result.push_back(kLongLoVReg);
4264 result.push_back(0);
4265 result.push_back(kLongHiVReg);
4266 result.push_back(0);
4267 ++i;
4268 } else if (type.IsDouble()) {
4269 result.push_back(kDoubleLoVReg);
4270 result.push_back(0);
4271 result.push_back(kDoubleHiVReg);
4272 result.push_back(0);
4273 ++i;
4274 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4275 result.push_back(kUndefined);
4276 result.push_back(0);
4277 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004278 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004279 result.push_back(kReferenceVReg);
4280 result.push_back(0);
4281 }
4282 }
4283 return result;
4284}
4285
Ian Rogersd8f69b02014-09-10 21:43:52 +00004286const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004287 if (precise) {
4288 // Precise constant type.
4289 return reg_types_.FromCat1Const(value, true);
4290 } else {
4291 // Imprecise constant type.
4292 if (value < -32768) {
4293 return reg_types_.IntConstant();
4294 } else if (value < -128) {
4295 return reg_types_.ShortConstant();
4296 } else if (value < 0) {
4297 return reg_types_.ByteConstant();
4298 } else if (value == 0) {
4299 return reg_types_.Zero();
4300 } else if (value == 1) {
4301 return reg_types_.One();
4302 } else if (value < 128) {
4303 return reg_types_.PosByteConstant();
4304 } else if (value < 32768) {
4305 return reg_types_.PosShortConstant();
4306 } else if (value < 65536) {
4307 return reg_types_.CharConstant();
4308 } else {
4309 return reg_types_.IntConstant();
4310 }
4311 }
4312}
4313
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004314void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004315 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004316}
4317
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004318void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004319 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004320}
jeffhaod1224c72012-02-29 13:43:08 -08004321
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004322void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4323 RegTypeCache::VisitStaticRoots(callback, arg);
4324}
4325
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004326void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4327 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004328}
4329
Ian Rogersd81871c2011-10-03 13:57:23 -07004330} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004331} // namespace art