blob: 9ceb6f4748155a33015c614ff82b171476341530 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "mirror/art_field-inl.h"
34#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class.h"
36#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070037#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070040#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080045#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070050static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070051static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070052// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070053
Ian Rogers7b3ddd22013-02-21 15:19:52 -080054void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070055 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070056 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070057 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070058 register_lines_.reset(new RegisterLine*[insns_size]());
59 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070060 for (uint32_t i = 0; i < insns_size; i++) {
61 bool interesting = false;
62 switch (mode) {
63 case kTrackRegsAll:
64 interesting = flags[i].IsOpcode();
65 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070066 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070067 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070068 break;
69 case kTrackRegsBranches:
70 interesting = flags[i].IsBranchTarget();
71 break;
72 default:
73 break;
74 }
75 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070076 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
77 }
78 }
79}
80
81PcToRegisterLineTable::~PcToRegisterLineTable() {
82 for (size_t i = 0; i < size_; i++) {
83 delete register_lines_[i];
84 if (kIsDebugBuild) {
85 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070086 }
87 }
88}
89
Andreas Gampe7c038102014-10-27 20:08:46 -070090// Note: returns true on failure.
91ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
92 const char* error_msg, uint32_t work_insn_idx) {
93 if (kIsDebugBuild) {
94 // In a debug build, abort if the error condition is wrong.
95 DCHECK(condition) << error_msg << work_insn_idx;
96 } else {
97 // In a non-debug build, just fail the class.
98 if (!condition) {
99 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
100 return true;
101 }
102 }
103
104 return false;
105}
106
Stephen Kyle7e541c92014-12-17 17:10:02 +0000107static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
108 if (verifier->IsConstructor()) {
109 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
110 reg_line->CheckConstructorReturn(verifier);
111 }
112 reg_line->MarkAllRegistersAsConflicts(verifier);
113}
114
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
116 mirror::ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
117 Thread* self = Thread::Current();
118 StackHandleScope<3> hs(self);
119 mirror::Class* klass = method->GetDeclaringClass();
120 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
121 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
122 auto h_method = hs.NewHandle(method);
123 return VerifyMethod(self, method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), h_method,
125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700164}
165
Ian Rogers7b078e82014-09-10 14:44:24 -0700166MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
167 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700168 Handle<mirror::DexCache> dex_cache,
169 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700170 const DexFile::ClassDef* class_def,
171 bool allow_soft_failures,
172 std::string* error) {
173 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700174 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700175 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700178 }
jeffhaof56197c2012-03-05 18:01:54 -0800179 ClassDataItemIterator it(*dex_file, class_data);
180 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
181 it.Next();
182 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700184 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700185 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700186 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800187 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800189 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700190 if (method_idx == previous_direct_method_idx) {
191 // smali can create dex files with two encoded_methods sharing the same method_idx
192 // http://code.google.com/p/smali/issues/detail?id=119
193 it.Next();
194 continue;
195 }
196 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700197 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700198 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700199 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
200 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700202 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700203 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700204 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700206 StackHandleScope<1> hs(self);
207 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700208 MethodVerifier::FailureKind result = VerifyMethod(self,
209 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700210 dex_file,
211 dex_cache,
212 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700213 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700214 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700215 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700216 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700217 allow_soft_failures,
218 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (result != kNoFailure) {
220 if (result == kHardFailure) {
221 hard_fail = true;
222 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700224 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700225 *error = "Verifier rejected class ";
226 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
227 *error += " due to bad method ";
228 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700230 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800231 }
232 it.Next();
233 }
jeffhao9b0b1882012-10-01 16:51:22 -0700234 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800235 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700236 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800237 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700238 if (method_idx == previous_virtual_method_idx) {
239 // smali can create dex files with two encoded_methods sharing the same method_idx
240 // http://code.google.com/p/smali/issues/detail?id=119
241 it.Next();
242 continue;
243 }
244 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700245 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700246 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700247 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
248 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700249 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700250 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700251 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700252 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700253 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700254 StackHandleScope<1> hs(self);
255 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700256 MethodVerifier::FailureKind result = VerifyMethod(self,
257 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700258 dex_file,
259 dex_cache,
260 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700261 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700262 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700264 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700265 allow_soft_failures,
266 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 if (result != kNoFailure) {
268 if (result == kHardFailure) {
269 hard_fail = true;
270 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error = "Verifier rejected class ";
274 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
275 *error += " due to bad method ";
276 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700277 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700278 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800279 }
280 it.Next();
281 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700282 if (error_count == 0) {
283 return kNoFailure;
284 } else {
285 return hard_fail ? kHardFailure : kSoftFailure;
286 }
jeffhaof56197c2012-03-05 18:01:54 -0800287}
288
Ian Rogers7b078e82014-09-10 14:44:24 -0700289MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700291 Handle<mirror::DexCache> dex_cache,
292 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700293 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700295 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700296 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700297 bool allow_soft_failures,
298 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700299 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700300 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700303 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800304 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700305 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700306 // Verification completed, however failures may be pending that didn't cause the verification
307 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700308 CHECK(!verifier.have_pending_hard_failure_);
309 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700310 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700311 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700312 << PrettyMethod(method_idx, *dex_file) << "\n");
313 }
Ian Rogersc8982582012-09-07 16:53:25 -0700314 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800315 }
316 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700317 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 CHECK_NE(verifier.failures_.size(), 0U);
319 CHECK(verifier.have_pending_hard_failure_);
320 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700321 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800322 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700323 std::cout << "\n" << verifier.info_messages_.str();
324 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800325 }
Ian Rogersc8982582012-09-07 16:53:25 -0700326 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800327 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700328 if (kTimeVerifyMethod) {
329 uint64_t duration_ns = NanoTime() - start_ns;
330 if (duration_ns > MsToNs(100)) {
331 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
332 << " took " << PrettyDuration(duration_ns);
333 }
Ian Rogersc8982582012-09-07 16:53:25 -0700334 }
335 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800336}
337
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700338MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700339 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700340 Handle<mirror::DexCache> dex_cache,
341 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700342 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700344 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800345 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700346 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
347 class_def, code_item, dex_method_idx, method,
348 method_access_flags, true, true, true, true);
349 verifier->Verify();
350 verifier->DumpFailures(os);
351 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700352 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
353 // and querying any info is dangerous/can abort.
354 if (verifier->have_pending_hard_failure_) {
355 delete verifier;
356 return nullptr;
357 } else {
358 verifier->Dump(os);
359 return verifier;
360 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800361}
362
Ian Rogers7b078e82014-09-10 14:44:24 -0700363MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700364 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
365 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700366 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700367 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700368 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700369 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800370 bool need_precise_constants, bool verify_to_dump,
371 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700372 : self_(self),
373 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800374 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800375 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700376 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700377 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700378 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800379 dex_file_(dex_file),
380 dex_cache_(dex_cache),
381 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700382 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800383 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700384 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700385 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700386 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700387 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700388 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800389 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800390 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700391 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200392 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200394 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700395 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800396 verify_to_dump_(verify_to_dump),
397 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartier12d625f2015-03-13 11:33:37 -0700398 self->SetVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700399 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800400}
401
Mathieu Chartier590fee92013-09-13 13:46:47 -0700402MethodVerifier::~MethodVerifier() {
Mathieu Chartier12d625f2015-03-13 11:33:37 -0700403 Thread::Current()->ClearVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700404 STLDeleteElements(&failure_messages_);
405}
406
Brian Carlstromea46f952013-07-30 01:26:50 -0700407void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700408 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700409 Thread* self = Thread::Current();
410 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700411 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
412 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700413 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700414 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700415 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800416 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700417 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700418 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700419 verifier.FindLocksAtDexPc();
420}
421
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700422static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
423 const Instruction* inst = Instruction::At(code_item->insns_);
424
425 uint32_t insns_size = code_item->insns_size_in_code_units_;
426 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
427 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
428 return true;
429 }
430
431 dex_pc += inst->SizeInCodeUnits();
432 inst = inst->Next();
433 }
434
435 return false;
436}
437
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700438void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700439 CHECK(monitor_enter_dex_pcs_ != nullptr);
440 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700442 // Quick check whether there are any monitor_enter instructions at all.
443 if (!HasMonitorEnterInstructions(code_item_)) {
444 return;
445 }
446
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700447 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
448 // verification. In practice, the phase we want relies on data structures set up by all the
449 // earlier passes, so we just run the full method verification and bail out early when we've
450 // got what we wanted.
451 Verify();
452}
453
Brian Carlstromea46f952013-07-30 01:26:50 -0700454mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700455 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700456 Thread* self = Thread::Current();
457 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700458 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
459 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700460 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700461 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700462 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800463 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200464 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200465}
466
Brian Carlstromea46f952013-07-30 01:26:50 -0700467mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200469
470 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
471 // verification. In practice, the phase we want relies on data structures set up by all the
472 // earlier passes, so we just run the full method verification and bail out early when we've
473 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200474 bool success = Verify();
475 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700476 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200477 }
478 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700480 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200481 }
482 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
483 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200484}
485
Brian Carlstromea46f952013-07-30 01:26:50 -0700486mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700487 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700488 Thread* self = Thread::Current();
489 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700490 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
491 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700492 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700493 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700494 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800495 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200496 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200497}
498
Brian Carlstromea46f952013-07-30 01:26:50 -0700499mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700500 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200501
502 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
503 // verification. In practice, the phase we want relies on data structures set up by all the
504 // earlier passes, so we just run the full method verification and bail out early when we've
505 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200506 bool success = Verify();
507 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200509 }
510 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700511 if (register_line == nullptr) {
512 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200513 }
514 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
515 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800516 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200517}
518
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700522 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700524 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 } else {
526 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700527 }
jeffhaobdb76512011-09-07 11:43:16 -0700528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
530 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
532 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700534 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800536 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 // Run through the instructions and see if the width checks out.
538 bool result = ComputeWidthsAndCountOps();
539 // Flag instructions guarded by a "try" block and check exception handlers.
540 result = result && ScanTryCatchBlocks();
541 // Perform static instruction verification.
542 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700543 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000544 result = result && VerifyCodeFlow();
545 // Compute information for compiler.
546 if (result && Runtime::Current()->IsCompiler()) {
547 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
548 }
549 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700550}
551
Ian Rogers776ac1f2012-04-13 23:36:36 -0700552std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553 switch (error) {
554 case VERIFY_ERROR_NO_CLASS:
555 case VERIFY_ERROR_NO_FIELD:
556 case VERIFY_ERROR_NO_METHOD:
557 case VERIFY_ERROR_ACCESS_CLASS:
558 case VERIFY_ERROR_ACCESS_FIELD:
559 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700560 case VERIFY_ERROR_INSTANTIATION:
561 case VERIFY_ERROR_CLASS_CHANGE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800562 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700563 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
564 // class change and instantiation errors into soft verification errors so that we re-verify
565 // at runtime. We may fail to find or to agree on access because of not yet available class
566 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
567 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
568 // paths" that dynamically perform the verification and cause the behavior to be that akin
569 // to an interpreter.
570 error = VERIFY_ERROR_BAD_CLASS_SOFT;
571 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700572 // If we fail again at runtime, mark that this instruction would throw and force this
573 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700574 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700575
576 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
577 // try to merge garbage.
578 // Note: this assumes that Fail is called before we do any work_line modifications.
579 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
580 const Instruction* inst = Instruction::At(insns);
581 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
582
583 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
584 saved_line_->CopyFromLine(work_line_.get());
585 }
jeffhaofaf459e2012-08-31 15:32:47 -0700586 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700587 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700588 // Indication that verification should be retried at runtime.
589 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700590 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700591 have_pending_hard_failure_ = true;
592 }
593 break;
jeffhaod5347e02012-03-22 17:25:05 -0700594 // Hard verification failures at compile time will still fail at runtime, so the class is
595 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700596 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800597 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700598 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000599 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800600 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700601 have_pending_hard_failure_ = true;
602 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800603 }
604 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700605 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700606 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700607 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700608 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700609 failure_messages_.push_back(failure_message);
610 return *failure_message;
611}
612
Ian Rogers576ca0c2014-06-06 15:58:22 -0700613std::ostream& MethodVerifier::LogVerifyInfo() {
614 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
615 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
616}
617
Ian Rogersad0b3a32012-04-16 14:50:24 -0700618void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
619 size_t failure_num = failure_messages_.size();
620 DCHECK_NE(failure_num, 0U);
621 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
622 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700623 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700624 delete last_fail_message;
625}
626
627void MethodVerifier::AppendToLastFailMessage(std::string append) {
628 size_t failure_num = failure_messages_.size();
629 DCHECK_NE(failure_num, 0U);
630 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
631 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800632}
633
Ian Rogers776ac1f2012-04-13 23:36:36 -0700634bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 const uint16_t* insns = code_item_->insns_;
636 size_t insns_size = code_item_->insns_size_in_code_units_;
637 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700638 size_t new_instance_count = 0;
639 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700641
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700643 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700644 switch (opcode) {
645 case Instruction::APUT_OBJECT:
646 case Instruction::CHECK_CAST:
647 has_check_casts_ = true;
648 break;
649 case Instruction::INVOKE_VIRTUAL:
650 case Instruction::INVOKE_VIRTUAL_RANGE:
651 case Instruction::INVOKE_INTERFACE:
652 case Instruction::INVOKE_INTERFACE_RANGE:
653 has_virtual_or_interface_invokes_ = true;
654 break;
655 case Instruction::MONITOR_ENTER:
656 monitor_enter_count++;
657 break;
658 case Instruction::NEW_INSTANCE:
659 new_instance_count++;
660 break;
661 default:
662 break;
jeffhaobdb76512011-09-07 11:43:16 -0700663 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700665 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700667 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700668 }
669
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
672 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700673 return false;
674 }
675
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 new_instance_count_ = new_instance_count;
677 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700678 return true;
679}
680
Ian Rogers776ac1f2012-04-13 23:36:36 -0700681bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700683 if (tries_size == 0) {
684 return true;
685 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700687 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700688
689 for (uint32_t idx = 0; idx < tries_size; idx++) {
690 const DexFile::TryItem* try_item = &tries[idx];
691 uint32_t start = try_item->start_addr_;
692 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700693 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
695 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700696 return false;
697 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
700 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700701 return false;
702 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700703 uint32_t dex_pc = start;
704 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
705 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700707 size_t insn_size = inst->SizeInCodeUnits();
708 dex_pc += insn_size;
709 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700710 }
711 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800712 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700713 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700714 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700715 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700716 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700717 CatchHandlerIterator iterator(handlers_ptr);
718 for (; iterator.HasNext(); iterator.Next()) {
719 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700721 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
722 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700723 return false;
724 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100725 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
726 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
727 << "exception handler begins with move-result* (" << dex_pc << ")";
728 return false;
729 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700731 // Ensure exception types are resolved so that they don't need resolution to be delivered,
732 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700733 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800734 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
735 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700736 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700737 if (exception_type == nullptr) {
738 DCHECK(self_->IsExceptionPending());
739 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700740 }
741 }
jeffhaobdb76512011-09-07 11:43:16 -0700742 }
Ian Rogers0571d352011-11-03 19:51:38 -0700743 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700744 }
jeffhaobdb76512011-09-07 11:43:16 -0700745 return true;
746}
747
Ian Rogers776ac1f2012-04-13 23:36:36 -0700748bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700750
Ian Rogers0c7abda2012-09-19 13:33:42 -0700751 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700753 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700754
755 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700756 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700758 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 return false;
760 }
761 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700762 // All invoke points are marked as "Throw" points already.
763 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000764 if (inst->IsBranch()) {
765 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
766 // The compiler also needs safepoints for fall-through to loop heads.
767 // Such a loop head must be a target of a branch.
768 int32_t offset = 0;
769 bool cond, self_ok;
770 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
771 DCHECK(target_ok);
772 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
773 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700774 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000775 } else if (inst->IsReturn()) {
776 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 }
778 dex_pc += inst->SizeInCodeUnits();
779 inst = inst->Next();
780 }
781 return true;
782}
783
Ian Rogers776ac1f2012-04-13 23:36:36 -0700784bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 bool result = true;
786 switch (inst->GetVerifyTypeArgumentA()) {
787 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700788 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 break;
790 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700791 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 break;
793 }
794 switch (inst->GetVerifyTypeArgumentB()) {
795 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700796 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 break;
798 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700799 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 break;
801 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700802 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 break;
804 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700805 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 break;
807 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700808 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 break;
810 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700811 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 break;
813 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700814 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 break;
816 }
817 switch (inst->GetVerifyTypeArgumentC()) {
818 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700819 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 break;
821 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700822 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 break;
824 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700825 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 break;
827 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700828 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 break;
830 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700831 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 break;
833 }
834 switch (inst->GetVerifyExtraFlags()) {
835 case Instruction::kVerifyArrayData:
836 result = result && CheckArrayData(code_offset);
837 break;
838 case Instruction::kVerifyBranchTarget:
839 result = result && CheckBranchTarget(code_offset);
840 break;
841 case Instruction::kVerifySwitchTargets:
842 result = result && CheckSwitchTargets(code_offset);
843 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700844 case Instruction::kVerifyVarArgNonZero:
845 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700846 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700847 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
849 "non-range invoke";
850 return false;
851 }
Ian Rogers29a26482014-05-02 15:27:29 -0700852 uint32_t args[Instruction::kMaxVarArgRegs];
853 inst->GetVarArgs(args);
854 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700856 }
Andreas Gampec3314312014-06-19 18:13:29 -0700857 case Instruction::kVerifyVarArgRangeNonZero:
858 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700860 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
861 inst->VRegA() <= 0) {
862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
863 "range invoke";
864 return false;
865 }
Ian Rogers29a26482014-05-02 15:27:29 -0700866 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 break;
868 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 result = false;
871 break;
872 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800873 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
875 result = false;
876 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return result;
878}
879
Ian Rogers7b078e82014-09-10 14:44:24 -0700880inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
883 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 return false;
885 }
886 return true;
887}
888
Ian Rogers7b078e82014-09-10 14:44:24 -0700889inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
892 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 return false;
894 }
895 return true;
896}
897
Ian Rogers7b078e82014-09-10 14:44:24 -0700898inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
901 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 return false;
903 }
904 return true;
905}
906
Ian Rogers7b078e82014-09-10 14:44:24 -0700907inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
910 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 return false;
912 }
913 return true;
914}
915
Ian Rogers7b078e82014-09-10 14:44:24 -0700916inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
919 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 return false;
921 }
922 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700923 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 return true;
929}
930
Ian Rogers7b078e82014-09-10 14:44:24 -0700931inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
934 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 return false;
936 }
937 return true;
938}
939
Ian Rogers7b078e82014-09-10 14:44:24 -0700940inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
943 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
945 }
946 return true;
947}
948
Ian Rogers776ac1f2012-04-13 23:36:36 -0700949bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
952 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 return false;
954 }
955 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700956 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 const char* cp = descriptor;
958 while (*cp++ == '[') {
959 bracket_count++;
960 }
961 if (bracket_count == 0) {
962 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700963 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
964 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 } else if (bracket_count > 255) {
967 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700968 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
969 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 return false;
971 }
972 return true;
973}
974
Ian Rogers776ac1f2012-04-13 23:36:36 -0700975bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
977 const uint16_t* insns = code_item_->insns_ + cur_offset;
978 const uint16_t* array_data;
979 int32_t array_data_offset;
980
981 DCHECK_LT(cur_offset, insn_count);
982 /* make sure the start of the array data table is in range */
983 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
984 if ((int32_t) cur_offset + array_data_offset < 0 ||
985 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700987 << ", data offset " << array_data_offset
988 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 /* offset to array data table is a relative branch-style offset */
992 array_data = insns + array_data_offset;
993 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800994 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
996 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 return false;
998 }
999 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001000 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1002 /* make sure the end of the switch is in range */
1003 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1005 << ", data offset " << array_data_offset << ", end "
1006 << cur_offset + array_data_offset + table_size
1007 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 return false;
1009 }
1010 return true;
1011}
1012
Ian Rogers776ac1f2012-04-13 23:36:36 -07001013bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 int32_t offset;
1015 bool isConditional, selfOkay;
1016 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1017 return false;
1018 }
1019 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1021 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001024 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1025 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1028 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 return false;
1030 }
1031 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1032 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001033 if (abs_offset < 0 ||
1034 (uint32_t) abs_offset >= insn_count ||
1035 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001037 << reinterpret_cast<void*>(abs_offset) << ") at "
1038 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 return false;
1040 }
1041 insn_flags_[abs_offset].SetBranchTarget();
1042 return true;
1043}
1044
Ian Rogers776ac1f2012-04-13 23:36:36 -07001045bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 bool* selfOkay) {
1047 const uint16_t* insns = code_item_->insns_ + cur_offset;
1048 *pConditional = false;
1049 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001050 switch (*insns & 0xff) {
1051 case Instruction::GOTO:
1052 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001053 break;
1054 case Instruction::GOTO_32:
1055 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001056 *selfOkay = true;
1057 break;
1058 case Instruction::GOTO_16:
1059 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001060 break;
1061 case Instruction::IF_EQ:
1062 case Instruction::IF_NE:
1063 case Instruction::IF_LT:
1064 case Instruction::IF_GE:
1065 case Instruction::IF_GT:
1066 case Instruction::IF_LE:
1067 case Instruction::IF_EQZ:
1068 case Instruction::IF_NEZ:
1069 case Instruction::IF_LTZ:
1070 case Instruction::IF_GEZ:
1071 case Instruction::IF_GTZ:
1072 case Instruction::IF_LEZ:
1073 *pOffset = (int16_t) insns[1];
1074 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001075 break;
1076 default:
1077 return false;
1078 break;
1079 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001080 return true;
1081}
1082
Ian Rogers776ac1f2012-04-13 23:36:36 -07001083bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001085 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001087 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1089 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001091 << ", switch offset " << switch_offset
1092 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001093 return false;
1094 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001095 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001097 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001098 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1100 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001101 return false;
1102 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 uint32_t switch_count = switch_insns[1];
1104 int32_t keys_offset, targets_offset;
1105 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001106 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1107 /* 0=sig, 1=count, 2/3=firstKey */
1108 targets_offset = 4;
1109 keys_offset = -1;
1110 expected_signature = Instruction::kPackedSwitchSignature;
1111 } else {
1112 /* 0=sig, 1=count, 2..count*2 = keys */
1113 keys_offset = 2;
1114 targets_offset = 2 + 2 * switch_count;
1115 expected_signature = Instruction::kSparseSwitchSignature;
1116 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001118 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001119 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1120 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1121 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001122 return false;
1123 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001124 /* make sure the end of the switch is in range */
1125 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1127 << ", switch offset " << switch_offset
1128 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001129 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 return false;
1131 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 /* for a sparse switch, verify the keys are in ascending order */
1133 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1135 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1137 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1138 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1140 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001141 return false;
1142 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001143 last_key = key;
1144 }
1145 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001146 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 for (uint32_t targ = 0; targ < switch_count; targ++) {
1148 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1149 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1150 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001151 if (abs_offset < 0 ||
1152 abs_offset >= (int32_t) insn_count ||
1153 !insn_flags_[abs_offset].IsOpcode()) {
1154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1155 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1156 << reinterpret_cast<void*>(cur_offset)
1157 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 return false;
1159 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 insn_flags_[abs_offset].SetBranchTarget();
1161 }
1162 return true;
1163}
1164
Ian Rogers776ac1f2012-04-13 23:36:36 -07001165bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001166 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 return false;
1169 }
1170 uint16_t registers_size = code_item_->registers_size_;
1171 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001172 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1174 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 return false;
1176 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001177 }
1178
1179 return true;
1180}
1181
Ian Rogers776ac1f2012-04-13 23:36:36 -07001182bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 uint16_t registers_size = code_item_->registers_size_;
1184 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1185 // integer overflow when adding them here.
1186 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1188 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001189 return false;
1190 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001191 return true;
1192}
1193
Ian Rogers776ac1f2012-04-13 23:36:36 -07001194bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 uint16_t registers_size = code_item_->registers_size_;
1196 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001197
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001199 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1200 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 }
1202 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001203 reg_table_.Init(kTrackCompilerInterestPoints,
1204 insn_flags_.get(),
1205 insns_size,
1206 registers_size,
1207 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001208
jeffhaobdb76512011-09-07 11:43:16 -07001209
Ian Rogersd0fbd852013-09-24 18:17:04 -07001210 work_line_.reset(RegisterLine::Create(registers_size, this));
1211 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001212
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 /* Initialize register types of method arguments. */
1214 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001215 DCHECK_NE(failures_.size(), 0U);
1216 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001217 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001218 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 return false;
1220 }
1221 /* Perform code flow verification. */
1222 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001223 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001225 }
jeffhaobdb76512011-09-07 11:43:16 -07001226 return true;
1227}
1228
Ian Rogersad0b3a32012-04-16 14:50:24 -07001229std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1230 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001231 for (size_t i = 0; i < failures_.size(); ++i) {
1232 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001233 }
1234 return os;
1235}
1236
Ian Rogers776ac1f2012-04-13 23:36:36 -07001237void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001238 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001239 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 return;
jeffhaobdb76512011-09-07 11:43:16 -07001241 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001242 {
1243 os << "Register Types:\n";
1244 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1245 std::ostream indent_os(&indent_filter);
1246 reg_types_.Dump(indent_os);
1247 }
Ian Rogersb4903572012-10-11 11:52:56 -07001248 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001249 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1250 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 const Instruction* inst = Instruction::At(code_item_->insns_);
1252 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001253 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001255 if (reg_line != nullptr) {
1256 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001257 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001258 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 const bool kDumpHexOfInstruction = false;
1260 if (kDumpHexOfInstruction) {
1261 indent_os << inst->DumpHex(5) << " ";
1262 }
1263 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001264 inst = inst->Next();
1265 }
jeffhaobdb76512011-09-07 11:43:16 -07001266}
1267
Ian Rogersd81871c2011-10-03 13:57:23 -07001268static bool IsPrimitiveDescriptor(char descriptor) {
1269 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001270 case 'I':
1271 case 'C':
1272 case 'S':
1273 case 'B':
1274 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001275 case 'F':
1276 case 'D':
1277 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001279 default:
1280 return false;
1281 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001282}
1283
Ian Rogers776ac1f2012-04-13 23:36:36 -07001284bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 RegisterLine* reg_line = reg_table_.GetLine(0);
1286 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1287 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001288
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001290 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001292 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1294 // argument as uninitialized. This restricts field access until the superclass constructor is
1295 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001296 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001297 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001298 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 reg_types_.UninitializedThisArgument(declaring_class));
1300 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001301 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001302 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001304 }
1305
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001306 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001307 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001308 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001309
1310 for (; iterator.HasNext(); iterator.Next()) {
1311 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001312 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 LOG(FATAL) << "Null descriptor";
1314 }
1315 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1317 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 return false;
1319 }
1320 switch (descriptor[0]) {
1321 case 'L':
1322 case '[':
1323 // We assume that reference arguments are initialized. The only way it could be otherwise
1324 // (assuming the caller was verified) is if the current method is <init>, but in that case
1325 // it's effectively considered initialized the instant we reach here (in the sense that we
1326 // can return without doing anything or call virtual methods).
1327 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001328 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001329 if (!reg_type.IsNonZeroReferenceTypes()) {
1330 DCHECK(HasFailures());
1331 return false;
1332 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001333 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 }
1335 break;
1336 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001337 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 break;
1339 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001340 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 break;
1342 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001343 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 break;
1345 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001346 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 break;
1348 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001349 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 break;
1351 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001352 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 break;
1354 case 'J':
1355 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001356 if (cur_arg + 1 >= expected_args) {
1357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1358 << " args, found more (" << descriptor << ")";
1359 return false;
1360 }
1361
Ian Rogers7b078e82014-09-10 14:44:24 -07001362 const RegType* lo_half;
1363 const RegType* hi_half;
1364 if (descriptor[0] == 'J') {
1365 lo_half = &reg_types_.LongLo();
1366 hi_half = &reg_types_.LongHi();
1367 } else {
1368 lo_half = &reg_types_.DoubleLo();
1369 hi_half = &reg_types_.DoubleHi();
1370 }
1371 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 cur_arg++;
1373 break;
1374 }
1375 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1377 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 return false;
1379 }
1380 cur_arg++;
1381 }
1382 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1384 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 return false;
1386 }
1387 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1388 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1389 // format. Only major difference from the method argument format is that 'V' is supported.
1390 bool result;
1391 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1392 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001393 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 size_t i = 0;
1395 do {
1396 i++;
1397 } while (descriptor[i] == '['); // process leading [
1398 if (descriptor[i] == 'L') { // object array
1399 do {
1400 i++; // find closing ;
1401 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1402 result = descriptor[i] == ';';
1403 } else { // primitive array
1404 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1405 }
1406 } else if (descriptor[0] == 'L') {
1407 // could be more thorough here, but shouldn't be required
1408 size_t i = 0;
1409 do {
1410 i++;
1411 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1412 result = descriptor[i] == ';';
1413 } else {
1414 result = false;
1415 }
1416 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1418 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 }
1420 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001421}
1422
Ian Rogers776ac1f2012-04-13 23:36:36 -07001423bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 const uint16_t* insns = code_item_->insns_;
1425 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001426
jeffhaobdb76512011-09-07 11:43:16 -07001427 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 insn_flags_[0].SetChanged();
1429 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001430
jeffhaobdb76512011-09-07 11:43:16 -07001431 /* Continue until no instructions are marked "changed". */
1432 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001433 if (allow_thread_suspension_) {
1434 self_->AllowThreadSuspension();
1435 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1437 uint32_t insn_idx = start_guess;
1438 for (; insn_idx < insns_size; insn_idx++) {
1439 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001440 break;
1441 }
jeffhaobdb76512011-09-07 11:43:16 -07001442 if (insn_idx == insns_size) {
1443 if (start_guess != 0) {
1444 /* try again, starting from the top */
1445 start_guess = 0;
1446 continue;
1447 } else {
1448 /* all flags are clear */
1449 break;
1450 }
1451 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 // We carry the working set of registers from instruction to instruction. If this address can
1453 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1454 // "changed" flags, we need to load the set of registers from the table.
1455 // Because we always prefer to continue on to the next instruction, we should never have a
1456 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1457 // target.
1458 work_insn_idx_ = insn_idx;
1459 if (insn_flags_[insn_idx].IsBranchTarget()) {
1460 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001461 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001462 /*
1463 * Sanity check: retrieve the stored register line (assuming
1464 * a full table) and make sure it actually matches.
1465 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001467 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 if (work_line_->CompareLine(register_line) != 0) {
1469 Dump(std::cout);
1470 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001472 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001473 << " work_line=" << work_line_->Dump(this) << "\n"
1474 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 }
jeffhaobdb76512011-09-07 11:43:16 -07001476 }
jeffhaobdb76512011-09-07 11:43:16 -07001477 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001480 prepend += " failed to verify: ";
1481 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 return false;
1483 }
jeffhaobdb76512011-09-07 11:43:16 -07001484 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 insn_flags_[insn_idx].SetVisited();
1486 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001487 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001488
Ian Rogers1c849e52012-06-28 14:00:33 -07001489 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001490 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001491 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001492 * (besides the wasted space), but it indicates a flaw somewhere
1493 * down the line, possibly in the verifier.
1494 *
1495 * If we've substituted "always throw" instructions into the stream,
1496 * we are almost certainly going to have some dead code.
1497 */
1498 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001500 for (; insn_idx < insns_size;
1501 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001502 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001503 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001504 * may or may not be preceded by a padding NOP (for alignment).
1505 */
1506 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1507 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1508 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001509 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001510 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1511 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1512 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001514 }
1515
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001517 if (dead_start < 0)
1518 dead_start = insn_idx;
1519 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001520 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1521 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001522 dead_start = -1;
1523 }
1524 }
1525 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001526 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1527 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001528 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001529 // To dump the state of the verify after a method, do something like:
1530 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1531 // "boolean java.lang.String.equals(java.lang.Object)") {
1532 // LOG(INFO) << info_messages_.str();
1533 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001534 }
jeffhaobdb76512011-09-07 11:43:16 -07001535 return true;
1536}
1537
Ian Rogers776ac1f2012-04-13 23:36:36 -07001538bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001539 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1540 // We want the state _before_ the instruction, for the case where the dex pc we're
1541 // interested in is itself a monitor-enter instruction (which is a likely place
1542 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001543 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001544 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001545 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1546 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1547 }
1548 }
1549
jeffhaobdb76512011-09-07 11:43:16 -07001550 /*
1551 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001552 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001553 * control to another statement:
1554 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001555 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001556 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001557 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001558 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001559 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001560 * throw an exception that is handled by an encompassing "try"
1561 * block.
1562 *
1563 * We can also return, in which case there is no successor instruction
1564 * from this point.
1565 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001566 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001567 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1569 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001570 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001571
jeffhaobdb76512011-09-07 11:43:16 -07001572 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001573 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001574 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001576 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001577 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 }
jeffhaobdb76512011-09-07 11:43:16 -07001579
1580 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001581 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001582 * can throw an exception, we will copy/merge this into the "catch"
1583 * address rather than work_line, because we don't want the result
1584 * from the "successful" code path (e.g. a check-cast that "improves"
1585 * a type) to be visible to the exception handler.
1586 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001587 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001589 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001591 }
1592
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001593
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001594 // We need to ensure the work line is consistent while performing validation. When we spot a
1595 // peephole pattern we compute a new line for either the fallthrough instruction or the
1596 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001597 std::unique_ptr<RegisterLine> branch_line;
1598 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001599
Stephen Kyle7e541c92014-12-17 17:10:02 +00001600 /*
1601 * If we are in a constructor, and we currently have an UninitializedThis type
1602 * in a register somewhere, we need to make sure it isn't overwritten.
1603 */
1604 bool track_uninitialized_this = false;
1605 size_t uninitialized_this_loc = 0;
1606 if (IsConstructor()) {
1607 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1608 }
1609
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001611 case Instruction::NOP:
1612 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001613 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001614 * a signature that looks like a NOP; if we see one of these in
1615 * the course of executing code then we have a problem.
1616 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001619 }
1620 break;
1621
1622 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001623 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 break;
jeffhaobdb76512011-09-07 11:43:16 -07001625 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001626 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 break;
jeffhaobdb76512011-09-07 11:43:16 -07001628 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001629 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001630 break;
1631 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001632 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 break;
jeffhaobdb76512011-09-07 11:43:16 -07001634 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001635 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 break;
jeffhaobdb76512011-09-07 11:43:16 -07001637 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001638 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001639 break;
1640 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001641 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 break;
jeffhaobdb76512011-09-07 11:43:16 -07001643 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001644 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 break;
jeffhaobdb76512011-09-07 11:43:16 -07001646 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001647 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001648 break;
1649
1650 /*
1651 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001652 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001653 * might want to hold the result in an actual CPU register, so the
1654 * Dalvik spec requires that these only appear immediately after an
1655 * invoke or filled-new-array.
1656 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001657 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001658 * redundant with the reset done below, but it can make the debug info
1659 * easier to read in some cases.)
1660 */
1661 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001662 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001663 break;
1664 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001665 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001666 break;
1667 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001668 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001669 break;
1670
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001672 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1673 // where one entrypoint to the catch block is not actually an exception path.
1674 if (work_insn_idx_ == 0) {
1675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1676 break;
1677 }
jeffhaobdb76512011-09-07 11:43:16 -07001678 /*
jeffhao60f83e32012-02-13 17:16:30 -08001679 * This statement can only appear as the first instruction in an exception handler. We verify
1680 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001681 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001682 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001684 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 }
jeffhaobdb76512011-09-07 11:43:16 -07001686 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001687 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001688 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 }
jeffhaobdb76512011-09-07 11:43:16 -07001691 }
1692 break;
1693 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001694 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001695 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001696 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1699 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 } else {
1701 // Compilers may generate synthetic functions that write byte values into boolean fields.
1702 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001704 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1706 ((return_type.IsBoolean() || return_type.IsByte() ||
1707 return_type.IsShort() || return_type.IsChar()) &&
1708 src_type.IsInteger()));
1709 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001710 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001711 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001712 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 }
jeffhaobdb76512011-09-07 11:43:16 -07001715 }
1716 }
1717 break;
1718 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001719 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001720 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001721 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 } else {
1725 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001727 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001728 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 }
jeffhaobdb76512011-09-07 11:43:16 -07001731 }
1732 }
1733 break;
1734 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001736 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 } else {
1740 /* return_type is the *expected* return type, not register value */
1741 DCHECK(!return_type.IsZero());
1742 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001745 // Disallow returning uninitialized values and verify that the reference in vAA is an
1746 // instance of the "return_type"
1747 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001748 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1749 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001750 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001751 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1752 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1753 << "' or '" << reg_type << "'";
1754 } else {
1755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1756 << "', but expected from declaration '" << return_type << "'";
1757 }
jeffhaobdb76512011-09-07 11:43:16 -07001758 }
1759 }
1760 }
1761 break;
1762
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001763 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 case Instruction::CONST_4: {
1765 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001766 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001767 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001768 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001769 }
1770 case Instruction::CONST_16: {
1771 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001772 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001773 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001774 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001775 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001776 case Instruction::CONST: {
1777 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001779 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001780 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001781 }
1782 case Instruction::CONST_HIGH16: {
1783 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001784 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001785 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001786 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001787 }
jeffhaobdb76512011-09-07 11:43:16 -07001788 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001789 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001791 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1792 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001793 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 }
1796 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001797 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001798 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1799 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001800 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 break;
1802 }
1803 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001805 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1806 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001807 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001808 break;
1809 }
1810 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001811 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001812 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1813 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001814 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001815 break;
1816 }
jeffhaobdb76512011-09-07 11:43:16 -07001817 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001818 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001819 break;
jeffhaobdb76512011-09-07 11:43:16 -07001820 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001821 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001824 // Get type from instruction if unresolved then we need an access check
1825 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001826 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001827 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001828 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001829 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001830 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001831 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 }
jeffhaobdb76512011-09-07 11:43:16 -07001833 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001834 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001835 break;
1836 case Instruction::MONITOR_EXIT:
1837 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001838 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001839 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001840 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001841 * to the need to handle asynchronous exceptions, a now-deprecated
1842 * feature that Dalvik doesn't support.)
1843 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001844 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001845 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001846 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001847 * structured locking checks are working, the former would have
1848 * failed on the -enter instruction, and the latter is impossible.
1849 *
1850 * This is fortunate, because issue 3221411 prevents us from
1851 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001852 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001853 * some catch blocks (which will show up as "dead" code when
1854 * we skip them here); if we can't, then the code path could be
1855 * "live" so we still need to check it.
1856 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001857 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001859 break;
1860
Ian Rogers28ad40d2011-10-27 15:19:26 -07001861 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001863 /*
1864 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1865 * could be a "upcast" -- not expected, so we don't try to address it.)
1866 *
1867 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001868 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001869 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001870 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1871 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001872 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001873 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001874 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001875 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001876 if (klass != nullptr && klass->IsPrimitive()) {
1877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1878 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1879 << GetDeclaringClass();
1880 break;
1881 }
1882
Ian Rogersad0b3a32012-04-16 14:50:24 -07001883 DCHECK_NE(failures_.size(), 0U);
1884 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001885 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001886 }
1887 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001888 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001889 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001890 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001891 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001892 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 if (is_checkcast) {
1894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1895 } else {
1896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1897 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001898 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 if (is_checkcast) {
1900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1901 } else {
1902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1903 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001904 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001905 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001906 work_line_->SetRegisterType(this, inst->VRegA_21c(), 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_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001909 }
jeffhaobdb76512011-09-07 11:43:16 -07001910 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001911 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 }
1913 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001915 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001916 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001921 } else {
1922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 }
1924 break;
1925 }
1926 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001927 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001928 if (res_type.IsConflict()) {
1929 DCHECK_NE(failures_.size(), 0U);
1930 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001931 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001932 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1933 // can't create an instance of an interface or abstract class */
1934 if (!res_type.IsInstantiableTypes()) {
1935 Fail(VERIFY_ERROR_INSTANTIATION)
1936 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001937 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001939 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001940 // Any registers holding previous allocations from this address that have not yet been
1941 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001942 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001943 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001944 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 break;
1946 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001947 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001948 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001949 break;
1950 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001951 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001952 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001954 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001955 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001956 just_set_result = true; // Filled new array range sets result register
1957 break;
jeffhaobdb76512011-09-07 11:43:16 -07001958 case Instruction::CMPL_FLOAT:
1959 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001960 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001961 break;
1962 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001963 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001964 break;
1965 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001967 break;
1968 case Instruction::CMPL_DOUBLE:
1969 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001970 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001971 reg_types_.DoubleHi())) {
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_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001975 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001976 break;
1977 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001978 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001979 break;
1980 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001981 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001982 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001983 break;
1984 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001985 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001986 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001987 break;
1988 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001989 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001990 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001992 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001993 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001994 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1995 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001996 }
1997 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 }
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::GOTO:
2000 case Instruction::GOTO_16:
2001 case Instruction::GOTO_32:
2002 /* no effect on or use of registers */
2003 break;
2004
2005 case Instruction::PACKED_SWITCH:
2006 case Instruction::SPARSE_SWITCH:
2007 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002008 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 case Instruction::FILL_ARRAY_DATA: {
2012 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002013 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002014 /* array_type can be null if the reg type is Zero */
2015 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002016 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2018 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002019 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002020 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002021 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002022 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2024 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 } else {
jeffhao457cc512012-02-02 16:55:13 -08002026 // Now verify if the element width in the table matches the element width declared in
2027 // the array
2028 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2029 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002031 } else {
2032 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2033 // Since we don't compress the data in Dex, expect to see equal width of data stored
2034 // in the table and expected from the array class.
2035 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2037 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002038 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 }
2040 }
jeffhaobdb76512011-09-07 11:43:16 -07002041 }
2042 }
2043 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 }
jeffhaobdb76512011-09-07 11:43:16 -07002045 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002047 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2048 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 bool mismatch = false;
2050 if (reg_type1.IsZero()) { // zero then integral or reference expected
2051 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2052 } else if (reg_type1.IsReferenceTypes()) { // both references?
2053 mismatch = !reg_type2.IsReferenceTypes();
2054 } else { // both integral?
2055 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2056 }
2057 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2059 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002060 }
2061 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 }
jeffhaobdb76512011-09-07 11:43:16 -07002063 case Instruction::IF_LT:
2064 case Instruction::IF_GE:
2065 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002067 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2068 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2071 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002072 }
2073 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 }
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002077 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2080 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002082
2083 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002084 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002085 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002086 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002087 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002088 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002089 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002090 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2091 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2092 work_insn_idx_)) {
2093 break;
2094 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002095 } else {
2096 break;
2097 }
2098
Ian Rogers9b360392013-06-06 14:45:07 -07002099 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002100
2101 /* Check for peep-hole pattern of:
2102 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002103 * instance-of vX, vY, T;
2104 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002105 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002106 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002107 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002108 * and sharpen the type of vY to be type T.
2109 * Note, this pattern can't be if:
2110 * - if there are other branches to this branch,
2111 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002112 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002113 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002114 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2115 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2116 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002117 // Check the type of the instance-of is different than that of registers type, as if they
2118 // are the same there is no work to be done here. Check that the conversion is not to or
2119 // from an unresolved type as type information is imprecise. If the instance-of is to an
2120 // interface then ignore the type information as interfaces can only be treated as Objects
2121 // and we don't want to disallow field and other operations on the object. If the value
2122 // being instance-of checked against is known null (zero) then allow the optimization as
2123 // we didn't have type information. If the merge of the instance-of type with the original
2124 // type is assignable to the original then allow optimization. This check is performed to
2125 // ensure that subsequent merges don't lose type information - such as becoming an
2126 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002127 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002128 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002129
Ian Rogersebbdd872014-07-07 23:53:08 -07002130 if (!orig_type.Equals(cast_type) &&
2131 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002132 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002133 !cast_type.GetClass()->IsInterface() &&
2134 (orig_type.IsZero() ||
2135 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002136 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002137 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002138 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002139 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002140 branch_line.reset(update_line);
2141 }
2142 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002143 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002144 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2145 // See if instance-of was preceded by a move-object operation, common due to the small
2146 // register encoding space of instance-of, and propagate type information to the source
2147 // of the move-object.
2148 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002149 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002150 move_idx--;
2151 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002152 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2153 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2154 work_insn_idx_)) {
2155 break;
2156 }
Ian Rogers9b360392013-06-06 14:45:07 -07002157 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2158 switch (move_inst->Opcode()) {
2159 case Instruction::MOVE_OBJECT:
2160 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002161 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002162 }
2163 break;
2164 case Instruction::MOVE_OBJECT_FROM16:
2165 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002166 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002167 }
2168 break;
2169 case Instruction::MOVE_OBJECT_16:
2170 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002171 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002172 }
2173 break;
2174 default:
2175 break;
2176 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002177 }
2178 }
2179 }
2180
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::IF_LTZ:
2184 case Instruction::IF_GEZ:
2185 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002187 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2190 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 }
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 break;
jeffhaobdb76512011-09-07 11:43:16 -07002197 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 break;
jeffhaobdb76512011-09-07 11:43:16 -07002200 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 break;
jeffhaobdb76512011-09-07 11:43:16 -07002203 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002205 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002207 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 break;
jeffhaobdb76512011-09-07 11:43:16 -07002209 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 break;
2212 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
2215
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 break;
2219 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 break;
2222 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 break;
2225 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002226 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
2228 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002229 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
2237
jeffhaobdb76512011-09-07 11:43:16 -07002238 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002239 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 break;
jeffhaobdb76512011-09-07 11:43:16 -07002241 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002242 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 break;
jeffhaobdb76512011-09-07 11:43:16 -07002244 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002245 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 break;
jeffhaobdb76512011-09-07 11:43:16 -07002247 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002248 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 break;
2250 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002251 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002254 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002257 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2258 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 break;
jeffhaobdb76512011-09-07 11:43:16 -07002260
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002262 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 break;
2264 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002265 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 break;
2267 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002268 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 break;
2270 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002271 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002274 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002277 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 break;
jeffhaobdb76512011-09-07 11:43:16 -07002279 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002280 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2281 false);
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002285 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 break;
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002288 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002291 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 break;
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002294 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
2296 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002297 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002300 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002303 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2304 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 break;
2306
2307 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002308 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 break;
2310 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002311 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 break;
2313 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002314 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 break;
2316 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002317 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002320 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002323 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002326 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2327 true);
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329
2330 case Instruction::INVOKE_VIRTUAL:
2331 case Instruction::INVOKE_VIRTUAL_RANGE:
2332 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002334 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2335 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002336 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2337 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002338 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2339 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002340 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002341 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002342 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002343 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002344 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002345 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002346 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2347 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002348 return_type_class->CannotBeAssignedFromOtherTypes());
2349 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002350 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2351 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002352 }
2353 }
2354 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002356 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2357 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002358 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002359 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002360 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002361 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002362 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002364 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002366 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 }
jeffhaobdb76512011-09-07 11:43:16 -07002369 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002372 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002373 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002374 const char* return_type_descriptor;
2375 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002376 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002377 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002378 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002379 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002380 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002381 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2382 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2383 } else {
2384 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002385 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002386 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002387 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002388 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002389 if (return_type_class != nullptr) {
2390 return_type = &reg_types_.FromClass(return_type_descriptor,
2391 return_type_class,
2392 return_type_class->CannotBeAssignedFromOtherTypes());
2393 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002394 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2395 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002396 }
Ian Rogers46685432012-06-03 22:26:43 -07002397 }
2398 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002399 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 * Some additional checks when calling a constructor. We know from the invocation arg check
2401 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2402 * that to require that called_method->klass is the same as this->klass or this->super,
2403 * allowing the latter only if the "this" argument is the same as the "this" argument to
2404 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002405 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002407 if (this_type.IsConflict()) // failure.
2408 break;
jeffhaobdb76512011-09-07 11:43:16 -07002409
jeffhaob57e9522012-04-26 18:08:21 -07002410 /* no null refs allowed (?) */
2411 if (this_type.IsZero()) {
2412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2413 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002414 }
jeffhaob57e9522012-04-26 18:08:21 -07002415
2416 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002417 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002418 // TODO: re-enable constructor type verification
2419 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002420 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002421 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2422 // break;
2423 // }
jeffhaob57e9522012-04-26 18:08:21 -07002424
2425 /* arg must be an uninitialized reference */
2426 if (!this_type.IsUninitializedTypes()) {
2427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2428 << this_type;
2429 break;
2430 }
2431
2432 /*
2433 * Replace the uninitialized reference with an initialized one. We need to do this for all
2434 * registers that have the same object instance in them, not just the "this" register.
2435 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002436 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002437 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002438 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002439 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2440 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002441 }
2442 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002443 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002444 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002445 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002446 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002447 just_set_result = true;
2448 break;
2449 }
2450 case Instruction::INVOKE_STATIC:
2451 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002453 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002454 METHOD_STATIC,
2455 is_range,
2456 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002457 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002458 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002459 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002460 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2461 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002462 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002463 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002464 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002465 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002466 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002467 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002468 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002469 } else {
2470 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2471 }
jeffhaobdb76512011-09-07 11:43:16 -07002472 just_set_result = true;
2473 }
2474 break;
jeffhaobdb76512011-09-07 11:43:16 -07002475 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002477 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002478 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002479 METHOD_INTERFACE,
2480 is_range,
2481 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002483 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002484 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2485 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2486 << PrettyMethod(abs_method) << "'";
2487 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002488 }
Ian Rogers0d604842012-04-16 14:50:24 -07002489 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002490 /* Get the type of the "this" arg, which should either be a sub-interface of called
2491 * interface or Object (see comments in RegType::JoinClass).
2492 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002493 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002494 if (this_type.IsZero()) {
2495 /* null pointer always passes (and always fails at runtime) */
2496 } else {
2497 if (this_type.IsUninitializedTypes()) {
2498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2499 << this_type;
2500 break;
2501 }
2502 // In the past we have tried to assert that "called_interface" is assignable
2503 // from "this_type.GetClass()", however, as we do an imprecise Join
2504 // (RegType::JoinClass) we don't have full information on what interfaces are
2505 // implemented by "this_type". For example, two classes may implement the same
2506 // interfaces and have a common parent that doesn't implement the interface. The
2507 // join will set "this_type" to the parent class and a test that this implements
2508 // the interface will incorrectly fail.
2509 }
2510 /*
2511 * We don't have an object instance, so we can't find the concrete method. However, all of
2512 * the type information is in the abstract method, so we're good.
2513 */
2514 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002515 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002516 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002517 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2518 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2519 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2520 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002521 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002522 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002523 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002524 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002525 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002526 } else {
2527 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2528 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002529 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 }
jeffhaobdb76512011-09-07 11:43:16 -07002532 case Instruction::NEG_INT:
2533 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002534 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002535 break;
2536 case Instruction::NEG_LONG:
2537 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002542 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002545 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002546 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002550 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002554 break;
2555 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002556 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002557 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002558 break;
2559 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002560 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002561 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002564 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002565 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002568 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002569 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
2571 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002572 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002573 break;
2574 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002575 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002576 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002579 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002580 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002583 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002584 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002588 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002592 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002595 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002601 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603
2604 case Instruction::ADD_INT:
2605 case Instruction::SUB_INT:
2606 case Instruction::MUL_INT:
2607 case Instruction::REM_INT:
2608 case Instruction::DIV_INT:
2609 case Instruction::SHL_INT:
2610 case Instruction::SHR_INT:
2611 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002612 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002613 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::AND_INT:
2616 case Instruction::OR_INT:
2617 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002618 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002619 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::ADD_LONG:
2622 case Instruction::SUB_LONG:
2623 case Instruction::MUL_LONG:
2624 case Instruction::DIV_LONG:
2625 case Instruction::REM_LONG:
2626 case Instruction::AND_LONG:
2627 case Instruction::OR_LONG:
2628 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002629 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002630 reg_types_.LongLo(), reg_types_.LongHi(),
2631 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::SHL_LONG:
2634 case Instruction::SHR_LONG:
2635 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002637 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002638 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::ADD_FLOAT:
2641 case Instruction::SUB_FLOAT:
2642 case Instruction::MUL_FLOAT:
2643 case Instruction::DIV_FLOAT:
2644 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002645 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2646 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648 case Instruction::ADD_DOUBLE:
2649 case Instruction::SUB_DOUBLE:
2650 case Instruction::MUL_DOUBLE:
2651 case Instruction::DIV_DOUBLE:
2652 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002653 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002654 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2655 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::ADD_INT_2ADDR:
2658 case Instruction::SUB_INT_2ADDR:
2659 case Instruction::MUL_INT_2ADDR:
2660 case Instruction::REM_INT_2ADDR:
2661 case Instruction::SHL_INT_2ADDR:
2662 case Instruction::SHR_INT_2ADDR:
2663 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002664 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2665 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002666 break;
2667 case Instruction::AND_INT_2ADDR:
2668 case Instruction::OR_INT_2ADDR:
2669 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002670 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2671 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
2673 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002674 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2675 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::ADD_LONG_2ADDR:
2678 case Instruction::SUB_LONG_2ADDR:
2679 case Instruction::MUL_LONG_2ADDR:
2680 case Instruction::DIV_LONG_2ADDR:
2681 case Instruction::REM_LONG_2ADDR:
2682 case Instruction::AND_LONG_2ADDR:
2683 case Instruction::OR_LONG_2ADDR:
2684 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002686 reg_types_.LongLo(), reg_types_.LongHi(),
2687 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::SHL_LONG_2ADDR:
2690 case Instruction::SHR_LONG_2ADDR:
2691 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002692 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002693 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::ADD_FLOAT_2ADDR:
2696 case Instruction::SUB_FLOAT_2ADDR:
2697 case Instruction::MUL_FLOAT_2ADDR:
2698 case Instruction::DIV_FLOAT_2ADDR:
2699 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002700 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2701 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002702 break;
2703 case Instruction::ADD_DOUBLE_2ADDR:
2704 case Instruction::SUB_DOUBLE_2ADDR:
2705 case Instruction::MUL_DOUBLE_2ADDR:
2706 case Instruction::DIV_DOUBLE_2ADDR:
2707 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002708 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002709 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2710 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
2712 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002713 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002714 case Instruction::MUL_INT_LIT16:
2715 case Instruction::DIV_INT_LIT16:
2716 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002717 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2718 true);
jeffhaobdb76512011-09-07 11:43:16 -07002719 break;
2720 case Instruction::AND_INT_LIT16:
2721 case Instruction::OR_INT_LIT16:
2722 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002723 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2724 true);
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
2726 case Instruction::ADD_INT_LIT8:
2727 case Instruction::RSUB_INT_LIT8:
2728 case Instruction::MUL_INT_LIT8:
2729 case Instruction::DIV_INT_LIT8:
2730 case Instruction::REM_INT_LIT8:
2731 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002732 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002733 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002734 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2735 false);
jeffhaobdb76512011-09-07 11:43:16 -07002736 break;
2737 case Instruction::AND_INT_LIT8:
2738 case Instruction::OR_INT_LIT8:
2739 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002740 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2741 false);
jeffhaobdb76512011-09-07 11:43:16 -07002742 break;
2743
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002744 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002745 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002746 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2748 }
2749 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002750 // Note: the following instructions encode offsets derived from class linking.
2751 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2752 // meaning if the class linking and resolution were successful.
2753 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002754 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002755 break;
2756 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002757 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002758 break;
2759 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002760 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002761 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002762 case Instruction::IGET_BOOLEAN_QUICK:
2763 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2764 break;
2765 case Instruction::IGET_BYTE_QUICK:
2766 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2767 break;
2768 case Instruction::IGET_CHAR_QUICK:
2769 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2770 break;
2771 case Instruction::IGET_SHORT_QUICK:
2772 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2773 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002774 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002775 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002776 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002777 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002778 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002779 break;
2780 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002781 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002782 break;
2783 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002784 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002785 break;
2786 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002787 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002788 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002789 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002790 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002791 break;
2792 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002793 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002794 break;
2795 case Instruction::INVOKE_VIRTUAL_QUICK:
2796 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2797 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002798 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002799 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002800 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002801 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002802 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002803 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002804 } else {
2805 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2806 }
2807 just_set_result = true;
2808 }
2809 break;
2810 }
2811
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002813 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2814 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002815 case Instruction::UNUSED_79:
2816 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002818 break;
2819
2820 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002821 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002822 * complain if an instruction is missing (which is desirable).
2823 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002824 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002825
Stephen Kyle7e541c92014-12-17 17:10:02 +00002826 /*
2827 * If we are in a constructor, and we had an UninitializedThis type
2828 * in a register somewhere, we need to make sure it wasn't overwritten.
2829 */
2830 if (track_uninitialized_this) {
2831 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2832 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2833 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2834 was_invoke_direct)) {
2835 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2836 << "Constructor failed to initialize this object";
2837 }
2838 }
2839
Ian Rogersad0b3a32012-04-16 14:50:24 -07002840 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002841 if (Runtime::Current()->IsAotCompiler()) {
2842 /* When AOT compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002843 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002844 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002845 /* immediate failure, reject class */
2846 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2847 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002848 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002849 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002850 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002851 }
jeffhaobdb76512011-09-07 11:43:16 -07002852 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 * If we didn't just set the result register, clear it out. This ensures that you can only use
2854 * "move-result" immediately after the result is set. (We could check this statically, but it's
2855 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002856 */
2857 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002858 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002859 }
2860
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002861
jeffhaobdb76512011-09-07 11:43:16 -07002862
2863 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002864 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002865 *
2866 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002867 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002868 * somebody could get a reference field, check it for zero, and if the
2869 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002870 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002871 * that, and will reject the code.
2872 *
2873 * TODO: avoid re-fetching the branch target
2874 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002875 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002876 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002878 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002880 return false;
2881 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002882 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002883 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002884 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 }
jeffhaobdb76512011-09-07 11:43:16 -07002886 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002887 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002888 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002889 return false;
2890 }
2891 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002892 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002893 return false;
2894 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 }
jeffhaobdb76512011-09-07 11:43:16 -07002896 }
2897
2898 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002899 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002900 *
2901 * We've already verified that the table is structurally sound, so we
2902 * just need to walk through and tag the targets.
2903 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002904 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002905 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2906 const uint16_t* switch_insns = insns + offset_to_switch;
2907 int switch_count = switch_insns[1];
2908 int offset_to_targets, targ;
2909
2910 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2911 /* 0 = sig, 1 = count, 2/3 = first key */
2912 offset_to_targets = 4;
2913 } else {
2914 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002915 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002916 offset_to_targets = 2 + 2 * switch_count;
2917 }
2918
2919 /* verify each switch target */
2920 for (targ = 0; targ < switch_count; targ++) {
2921 int offset;
2922 uint32_t abs_offset;
2923
2924 /* offsets are 32-bit, and only partly endian-swapped */
2925 offset = switch_insns[offset_to_targets + targ * 2] |
2926 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 abs_offset = work_insn_idx_ + offset;
2928 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002929 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002930 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002932 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002933 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002934 }
jeffhaobdb76512011-09-07 11:43:16 -07002935 }
2936 }
2937
2938 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002939 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2940 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002941 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002942 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002943 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002944 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002945
Andreas Gampef91baf12014-07-18 15:41:00 -07002946 // Need the linker to try and resolve the handled class to check if it's Throwable.
2947 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2948
Ian Rogers0571d352011-11-03 19:51:38 -07002949 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002950 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2951 if (handler_type_idx == DexFile::kDexNoIndex16) {
2952 has_catch_all_handler = true;
2953 } else {
2954 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002955 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2956 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002957 if (klass != nullptr) {
2958 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2959 has_catch_all_handler = true;
2960 }
2961 } else {
2962 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002963 DCHECK(self_->IsExceptionPending());
2964 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002965 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 }
jeffhaobdb76512011-09-07 11:43:16 -07002967 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2969 * "work_regs", because at runtime the exception will be thrown before the instruction
2970 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002971 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002972 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002973 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002974 }
jeffhaobdb76512011-09-07 11:43:16 -07002975 }
2976
2977 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002978 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2979 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002980 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002981 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002982 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 * The state in work_line reflects the post-execution state. If the current instruction is a
2984 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002985 * it will do so before grabbing the lock).
2986 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002987 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002988 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002990 return false;
2991 }
2992 }
2993 }
2994
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002995 /* Handle "continue". Tag the next consecutive instruction.
2996 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2997 * because it changes work_line_ when performing peephole optimization
2998 * and this change should not be used in those cases.
2999 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003000 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3002 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003003 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3005 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003006 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003007 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3008 // next instruction isn't one.
3009 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3010 return false;
3011 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003012 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003013 // Make workline consistent with fallthrough computed from peephole optimization.
3014 work_line_->CopyFromLine(fallthrough_line.get());
3015 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003016 if (insn_flags_[next_insn_idx].IsReturn()) {
3017 // For returns we only care about the operand to the return, all other registers are dead.
3018 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3019 Instruction::Code opcode = ret_inst->Opcode();
3020 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003021 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003022 } else {
3023 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003024 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003025 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003026 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003027 }
3028 }
3029 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003030 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003031 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003032 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3033 // needed. If the merge changes the state of the registers then the work line will be
3034 // updated.
3035 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003036 return false;
3037 }
3038 } else {
3039 /*
3040 * We're not recording register data for the next instruction, so we don't know what the
3041 * prior state was. We have to assume that something has changed and re-evaluate it.
3042 */
3043 insn_flags_[next_insn_idx].SetChanged();
3044 }
3045 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003046
jeffhaod1f0fde2011-09-08 17:25:33 -07003047 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003048 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003049 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 return false;
3051 }
jeffhaobdb76512011-09-07 11:43:16 -07003052 }
3053
3054 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003055 * Update start_guess. Advance to the next instruction of that's
3056 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003057 * neither of those exists we're in a return or throw; leave start_guess
3058 * alone and let the caller sort it out.
3059 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003060 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003061 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3062 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003063 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003064 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003066 }
3067
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3069 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003070
3071 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003072} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003073
Ian Rogersd8f69b02014-09-10 21:43:52 +00003074const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003075 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003076 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003077 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003079 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3080 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003081 if (result.IsConflict()) {
3082 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3083 << "' in " << referrer;
3084 return result;
3085 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003086 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003087 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003088 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003089 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003090 // check at runtime if access is allowed and so pass here. If result is
3091 // primitive, skip the access check.
3092 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3093 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003094 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003095 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003096 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003097 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003098}
3099
Ian Rogersd8f69b02014-09-10 21:43:52 +00003100const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003101 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003103 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003104 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3105 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003106 CatchHandlerIterator iterator(handlers_ptr);
3107 for (; iterator.HasNext(); iterator.Next()) {
3108 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3109 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003110 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003112 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003113 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003114 if (exception.IsUnresolvedTypes()) {
3115 // We don't know enough about the type. Fail here and let runtime handle it.
3116 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3117 return exception;
3118 } else {
3119 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3120 return reg_types_.Conflict();
3121 }
Jeff Haob878f212014-04-24 16:25:36 -07003122 } else if (common_super == nullptr) {
3123 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003124 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003125 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003127 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003128 if (FailOrAbort(this,
3129 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3130 "java.lang.Throwable is not assignable-from common_super at ",
3131 work_insn_idx_)) {
3132 break;
3133 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 }
3135 }
3136 }
3137 }
Ian Rogers0571d352011-11-03 19:51:38 -07003138 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 }
3140 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003141 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003143 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003144 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003145 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003146 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003147}
3148
Brian Carlstromea46f952013-07-30 01:26:50 -07003149mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003150 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003151 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003152 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003153 if (klass_type.IsConflict()) {
3154 std::string append(" in attempt to access method ");
3155 append += dex_file_->GetMethodName(method_id);
3156 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003157 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003158 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003159 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003160 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003161 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003162 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003163 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003164 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003166 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003167 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003168
3169 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003171 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 res_method = klass->FindInterfaceMethod(name, signature);
3173 } else {
3174 res_method = klass->FindVirtualMethod(name, signature);
3175 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003176 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003177 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003179 // If a virtual or interface method wasn't found with the expected type, look in
3180 // the direct methods. This can happen when the wrong invoke type is used or when
3181 // a class has changed, and will be flagged as an error in later checks.
3182 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3183 res_method = klass->FindDirectMethod(name, signature);
3184 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003185 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003186 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3187 << PrettyDescriptor(klass) << "." << name
3188 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003189 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003190 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 }
3192 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3194 // enforce them here.
3195 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3197 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003198 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003200 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003201 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3203 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003204 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003205 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003206 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003207 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003208 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003209 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003210 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003211 }
jeffhaode0d9c92012-02-27 13:58:13 -08003212 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3213 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3215 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003216 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003217 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003218 // Check that interface methods match interface classes.
3219 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3220 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3221 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003223 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3224 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3225 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003227 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003228 // See if the method type implied by the invoke instruction matches the access flags for the
3229 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003230 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3232 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3233 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003234 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3235 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003237 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003238 return res_method;
3239}
3240
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003241template <class T>
3242mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3243 MethodType method_type,
3244 bool is_range,
3245 mirror::ArtMethod* res_method) {
3246 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3247 // match the call to the signature. Also, we might be calling through an abstract method
3248 // definition (which doesn't have register count values).
3249 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3250 /* caught by static verifier */
3251 DCHECK(is_range || expected_args <= 5);
3252 if (expected_args > code_item_->outs_size_) {
3253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3254 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3255 return nullptr;
3256 }
3257
3258 uint32_t arg[5];
3259 if (!is_range) {
3260 inst->GetVarArgs(arg);
3261 }
3262 uint32_t sig_registers = 0;
3263
3264 /*
3265 * Check the "this" argument, which must be an instance of the class that declared the method.
3266 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3267 * rigorous check here (which is okay since we have to do it at runtime).
3268 */
3269 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003270 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003271 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3272 CHECK(have_pending_hard_failure_);
3273 return nullptr;
3274 }
3275 if (actual_arg_type.IsUninitializedReference()) {
3276 if (res_method) {
3277 if (!res_method->IsConstructor()) {
3278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3279 return nullptr;
3280 }
3281 } else {
3282 // Check whether the name of the called method is "<init>"
3283 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003284 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3286 return nullptr;
3287 }
3288 }
3289 }
3290 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003291 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003292 if (res_method != nullptr) {
3293 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003294 std::string temp;
3295 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003296 klass->CannotBeAssignedFromOtherTypes());
3297 } else {
3298 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3299 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003300 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003301 dex_file_->StringByTypeIdx(class_idx),
3302 false);
3303 }
3304 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3305 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3306 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3307 << "' not instance of '" << *res_method_class << "'";
3308 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3309 // the compiler.
3310 if (have_pending_hard_failure_) {
3311 return nullptr;
3312 }
3313 }
3314 }
3315 sig_registers = 1;
3316 }
3317
3318 for ( ; it->HasNext(); it->Next()) {
3319 if (sig_registers >= expected_args) {
3320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3321 " arguments, found " << sig_registers << " or more.";
3322 return nullptr;
3323 }
3324
3325 const char* param_descriptor = it->GetDescriptor();
3326
3327 if (param_descriptor == nullptr) {
3328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3329 "component";
3330 return nullptr;
3331 }
3332
Ian Rogersd8f69b02014-09-10 21:43:52 +00003333 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003334 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3335 arg[sig_registers];
3336 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003337 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003338 if (!src_type.IsIntegralTypes()) {
3339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3340 << " but expected " << reg_type;
3341 return res_method;
3342 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003344 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3345 // compiler.
3346 if (have_pending_hard_failure_) {
3347 return res_method;
3348 }
3349 }
3350 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3351 }
3352 if (expected_args != sig_registers) {
3353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3354 " arguments, found " << sig_registers;
3355 return nullptr;
3356 }
3357 return res_method;
3358}
3359
3360void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3361 MethodType method_type,
3362 bool is_range) {
3363 // As the method may not have been resolved, make this static check against what we expect.
3364 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3365 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3366 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3367 DexFileParameterIterator it(*dex_file_,
3368 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3369 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3370 nullptr);
3371}
3372
3373class MethodParamListDescriptorIterator {
3374 public:
3375 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3376 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3377 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3378 }
3379
3380 bool HasNext() {
3381 return pos_ < params_size_;
3382 }
3383
3384 void Next() {
3385 ++pos_;
3386 }
3387
3388 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3389 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3390 }
3391
3392 private:
3393 mirror::ArtMethod* res_method_;
3394 size_t pos_;
3395 const DexFile::TypeList* params_;
3396 const size_t params_size_;
3397};
3398
Brian Carlstromea46f952013-07-30 01:26:50 -07003399mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003400 MethodType method_type,
3401 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003402 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003403 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3404 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003405 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003406
Brian Carlstromea46f952013-07-30 01:26:50 -07003407 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003408 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003409 // Check what we can statically.
3410 if (!have_pending_hard_failure_) {
3411 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3412 }
3413 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003414 }
3415
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3417 // has a vtable entry for the target method.
3418 if (is_super) {
3419 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003420 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003421 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003422 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003423 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003424 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003425 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003426 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003427 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003428 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003429 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003430 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003431 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003432 << "." << res_method->GetName()
3433 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003434 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 }
3436 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003437
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003438 // Process the target method's signature. This signature may or may not
3439 MethodParamListDescriptorIterator it(res_method);
3440 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3441 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003442}
3443
Brian Carlstromea46f952013-07-30 01:26:50 -07003444mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -08003445 RegisterLine* reg_line, bool is_range,
3446 bool allow_failure) {
3447 if (is_range) {
3448 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3449 } else {
3450 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3451 }
3452 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003453 if (!actual_arg_type.HasClass()) {
3454 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003455 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003456 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003457 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003458 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003459 if (klass->IsInterface()) {
3460 // Derive Object.class from Class.class.getSuperclass().
3461 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003462 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003463 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003464 return nullptr;
3465 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003466 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003467 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003468 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003469 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003470 if (!dispatch_class->HasVTable()) {
3471 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3472 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003473 return nullptr;
3474 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003475 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003476 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3477 FailOrAbort(this, allow_failure,
3478 "Receiver class has not enough vtable slots for quickened invoke at ",
3479 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003480 return nullptr;
3481 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003482 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003483 if (self_->IsExceptionPending()) {
3484 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3485 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003486 return nullptr;
3487 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003488 return res_method;
3489}
3490
Brian Carlstromea46f952013-07-30 01:26:50 -07003491mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003492 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003493 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3494 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3495
Mathieu Chartier091d2382015-03-06 10:59:06 -08003496 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003497 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003499 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003500 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003501 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3502 work_insn_idx_)) {
3503 return nullptr;
3504 }
3505 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3506 work_insn_idx_)) {
3507 return nullptr;
3508 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003509
3510 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3511 // match the call to the signature. Also, we might be calling through an abstract method
3512 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003513 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003514 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003515 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003516 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003517 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3518 /* caught by static verifier */
3519 DCHECK(is_range || expected_args <= 5);
3520 if (expected_args > code_item_->outs_size_) {
3521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3522 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003523 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003524 }
3525
3526 /*
3527 * Check the "this" argument, which must be an instance of the class that declared the method.
3528 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3529 * rigorous check here (which is okay since we have to do it at runtime).
3530 */
3531 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003533 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003534 }
3535 if (!actual_arg_type.IsZero()) {
3536 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003537 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003538 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003539 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003540 klass->CannotBeAssignedFromOtherTypes());
3541 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003542 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3543 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003544 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003545 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003546 }
3547 }
3548 /*
3549 * Process the target method's signature. This signature may or may not
3550 * have been verified, so we can't assume it's properly formed.
3551 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003552 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003553 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003554 uint32_t arg[5];
3555 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003556 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003557 }
3558 size_t actual_args = 1;
3559 for (size_t param_index = 0; param_index < params_size; param_index++) {
3560 if (actual_args >= expected_args) {
3561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003562 << "'. Expected " << expected_args
3563 << " arguments, processing argument " << actual_args
3564 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003565 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003566 }
3567 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003568 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003569 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003571 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003572 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003573 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003574 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003575 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003576 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003577 return res_method;
3578 }
3579 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3580 }
3581 if (actual_args != expected_args) {
3582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3583 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003584 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003585 } else {
3586 return res_method;
3587 }
3588}
3589
Ian Rogers62342ec2013-06-11 10:26:37 -07003590void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003591 uint32_t type_idx;
3592 if (!is_filled) {
3593 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3594 type_idx = inst->VRegC_22c();
3595 } else if (!is_range) {
3596 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3597 type_idx = inst->VRegB_35c();
3598 } else {
3599 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3600 type_idx = inst->VRegB_3rc();
3601 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003602 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003603 if (res_type.IsConflict()) { // bad class
3604 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003605 } else {
3606 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3607 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003609 } else if (!is_filled) {
3610 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003612 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003613 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003614 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003615 } else {
3616 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3617 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003618 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003619 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3620 uint32_t arg[5];
3621 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003622 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003623 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003624 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003625 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003626 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3627 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003628 return;
3629 }
3630 }
3631 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003632 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003633 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003634 }
3635 }
3636}
3637
Sebastien Hertz5243e912013-05-21 10:55:07 +02003638void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003639 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003640 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003641 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003643 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003644 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003645 if (array_type.IsZero()) {
3646 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3647 // instruction type. TODO: have a proper notion of bottom here.
3648 if (!is_primitive || insn_type.IsCategory1Types()) {
3649 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003650 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003651 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003652 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3654 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003655 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003656 }
jeffhaofc3144e2012-02-01 17:21:15 -08003657 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003659 } else {
3660 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003661 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003662 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003664 << " source for aget-object";
3665 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003667 << " source for category 1 aget";
3668 } else if (is_primitive && !insn_type.Equals(component_type) &&
3669 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003670 (insn_type.IsLong() && component_type.IsDouble()))) {
3671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3672 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003673 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003674 // Use knowledge of the field type which is stronger than the type inferred from the
3675 // instruction, which can't differentiate object types and ints from floats, longs from
3676 // doubles.
3677 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003678 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003679 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003680 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003681 component_type.HighHalf(&reg_types_));
3682 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003683 }
3684 }
3685 }
3686}
3687
Ian Rogersd8f69b02014-09-10 21:43:52 +00003688void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003689 const uint32_t vregA) {
3690 // Primitive assignability rules are weaker than regular assignability rules.
3691 bool instruction_compatible;
3692 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003693 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003694 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003695 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003696 value_compatible = value_type.IsIntegralTypes();
3697 } else if (target_type.IsFloat()) {
3698 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3699 value_compatible = value_type.IsFloatTypes();
3700 } else if (target_type.IsLong()) {
3701 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003702 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3703 // as target_type depends on the resolved type of the field.
3704 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003705 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003706 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3707 } else {
3708 value_compatible = false;
3709 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003710 } else if (target_type.IsDouble()) {
3711 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003712 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3713 // as target_type depends on the resolved type of the field.
3714 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003716 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3717 } else {
3718 value_compatible = false;
3719 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003720 } else {
3721 instruction_compatible = false; // reference with primitive store
3722 value_compatible = false; // unused
3723 }
3724 if (!instruction_compatible) {
3725 // This is a global failure rather than a class change failure as the instructions and
3726 // the descriptors for the type should have been consistent within the same file at
3727 // compile time.
3728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3729 << "' but expected type '" << target_type << "'";
3730 return;
3731 }
3732 if (!value_compatible) {
3733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3734 << " of type " << value_type << " but expected " << target_type << " for put";
3735 return;
3736 }
3737}
3738
Sebastien Hertz5243e912013-05-21 10:55:07 +02003739void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003740 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003741 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003744 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003745 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003746 if (array_type.IsZero()) {
3747 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3748 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003749 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003751 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003752 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003753 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003754 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003755 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003756 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003757 if (!component_type.IsReferenceTypes()) {
3758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3759 << " source for aput-object";
3760 } else {
3761 // The instruction agrees with the type of array, confirm the value to be stored does too
3762 // Note: we use the instruction type (rather than the component type) for aput-object as
3763 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003764 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003765 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 }
3767 }
3768 }
3769}
3770
Brian Carlstromea46f952013-07-30 01:26:50 -07003771mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003772 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3773 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003774 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003775 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003776 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3777 field_idx, dex_file_->GetFieldName(field_id),
3778 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003779 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003780 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003781 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003782 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003783 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003784 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003785 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3786 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003787 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003788 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003789 << dex_file_->GetFieldName(field_id) << ") in "
3790 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003791 DCHECK(self_->IsExceptionPending());
3792 self_->ClearException();
3793 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003794 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3795 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003796 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003797 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003798 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 } else if (!field->IsStatic()) {
3800 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003801 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003803 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003804}
3805
Ian Rogersd8f69b02014-09-10 21:43:52 +00003806mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003807 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3808 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003809 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003810 if (klass_type.IsConflict()) {
3811 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3812 field_idx, dex_file_->GetFieldName(field_id),
3813 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003814 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003815 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003816 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003817 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003818 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003819 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003820 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3821 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003822 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003823 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003824 << dex_file_->GetFieldName(field_id) << ") in "
3825 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 DCHECK(self_->IsExceptionPending());
3827 self_->ClearException();
3828 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003829 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3830 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003831 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003832 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003833 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 } else if (field->IsStatic()) {
3835 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3836 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003837 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 } else if (obj_type.IsZero()) {
3839 // Cannot infer and check type, however, access will cause null pointer exception
3840 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003841 } else if (!obj_type.IsReferenceTypes()) {
3842 // Trying to read a field from something that isn't a reference
3843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3844 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003845 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003846 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003847 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003848 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003849 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003850 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003851 if (obj_type.IsUninitializedTypes() &&
3852 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3853 !field_klass.Equals(GetDeclaringClass()))) {
3854 // Field accesses through uninitialized references are only allowable for constructors where
3855 // the field is declared in this class
3856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003857 << " of a not fully initialized object within the context"
3858 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003859 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003860 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3861 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3862 // of C1. For resolution to occur the declared class of the field must be compatible with
3863 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3864 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3865 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003866 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003867 } else {
3868 return field;
3869 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003870 }
3871}
3872
Andreas Gampe896df402014-10-20 22:25:29 -07003873template <MethodVerifier::FieldAccessType kAccType>
3874void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3875 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003876 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003877 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003878 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003879 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003880 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003881 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003882 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003883 if (UNLIKELY(have_pending_hard_failure_)) {
3884 return;
3885 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003886 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003887 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003888 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003889 if (kAccType == FieldAccessType::kAccPut) {
3890 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3891 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3892 << " from other class " << GetDeclaringClass();
3893 return;
3894 }
3895 }
3896
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003897 mirror::Class* field_type_class;
3898 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003899 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003900 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08003901 field_type_class = h_field->GetType(can_load_classes_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003902 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003903 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003904 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003905 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003906 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003907 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3908 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003909 }
Ian Rogers0d604842012-04-16 14:50:24 -07003910 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003911 if (field_type == nullptr) {
3912 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3913 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003914 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003915 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003916 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003917 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003918 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3919 "Unexpected third access type");
3920 if (kAccType == FieldAccessType::kAccPut) {
3921 // sput or iput.
3922 if (is_primitive) {
3923 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003924 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003925 if (!insn_type.IsAssignableFrom(*field_type)) {
3926 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3927 << " to be compatible with type '" << insn_type
3928 << "' but found type '" << *field_type
3929 << "' in put-object";
3930 return;
3931 }
3932 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003933 }
Andreas Gampe896df402014-10-20 22:25:29 -07003934 } else if (kAccType == FieldAccessType::kAccGet) {
3935 // sget or iget.
3936 if (is_primitive) {
3937 if (field_type->Equals(insn_type) ||
3938 (field_type->IsFloat() && insn_type.IsInteger()) ||
3939 (field_type->IsDouble() && insn_type.IsLong())) {
3940 // expected that read is of the correct primitive type or that int reads are reading
3941 // floats or long reads are reading doubles
3942 } else {
3943 // This is a global failure rather than a class change failure as the instructions and
3944 // the descriptors for the type should have been consistent within the same file at
3945 // compile time
3946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3947 << " to be of type '" << insn_type
3948 << "' but found type '" << *field_type << "' in get";
3949 return;
3950 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003951 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003952 if (!insn_type.IsAssignableFrom(*field_type)) {
3953 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3954 << " to be compatible with type '" << insn_type
3955 << "' but found type '" << *field_type
3956 << "' in get-object";
3957 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3958 return;
3959 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003960 }
Andreas Gampe896df402014-10-20 22:25:29 -07003961 if (!field_type->IsLowHalf()) {
3962 work_line_->SetRegisterType(this, vregA, *field_type);
3963 } else {
3964 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3965 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003966 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003967 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003968 }
3969}
3970
Brian Carlstromea46f952013-07-30 01:26:50 -07003971mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003972 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003973 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07003974 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003975 if (!object_type.HasClass()) {
3976 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3977 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003978 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003979 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003980 mirror::ArtField* const f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3981 field_offset);
3982 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003983 if (f == nullptr) {
3984 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3985 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3986 }
3987 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003988}
3989
Andreas Gampe896df402014-10-20 22:25:29 -07003990template <MethodVerifier::FieldAccessType kAccType>
3991void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
3992 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003993 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003994
Brian Carlstromea46f952013-07-30 01:26:50 -07003995 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003996 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3998 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003999 }
Andreas Gampe896df402014-10-20 22:25:29 -07004000
4001 // For an IPUT_QUICK, we now test for final flag of the field.
4002 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004003 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4004 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004005 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004006 return;
4007 }
4008 }
Andreas Gampe896df402014-10-20 22:25:29 -07004009
4010 // Get the field type.
4011 const RegType* field_type;
4012 {
4013 mirror::Class* field_type_class;
4014 {
4015 StackHandleScope<1> hs(Thread::Current());
4016 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08004017 field_type_class = h_field->GetType(can_load_classes_);
Andreas Gampe896df402014-10-20 22:25:29 -07004018 }
4019
4020 if (field_type_class != nullptr) {
4021 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4022 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004023 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004024 Thread* self = Thread::Current();
4025 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4026 self->ClearException();
4027 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4028 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004029 }
Andreas Gampe896df402014-10-20 22:25:29 -07004030 if (field_type == nullptr) {
4031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004032 return;
4033 }
Andreas Gampe896df402014-10-20 22:25:29 -07004034 }
4035
4036 const uint32_t vregA = inst->VRegA_22c();
4037 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4038 "Unexpected third access type");
4039 if (kAccType == FieldAccessType::kAccPut) {
4040 if (is_primitive) {
4041 // Primitive field assignability rules are weaker than regular assignability rules
4042 bool instruction_compatible;
4043 bool value_compatible;
4044 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4045 if (field_type->IsIntegralTypes()) {
4046 instruction_compatible = insn_type.IsIntegralTypes();
4047 value_compatible = value_type.IsIntegralTypes();
4048 } else if (field_type->IsFloat()) {
4049 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4050 value_compatible = value_type.IsFloatTypes();
4051 } else if (field_type->IsLong()) {
4052 instruction_compatible = insn_type.IsLong();
4053 value_compatible = value_type.IsLongTypes();
4054 } else if (field_type->IsDouble()) {
4055 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4056 value_compatible = value_type.IsDoubleTypes();
4057 } else {
4058 instruction_compatible = false; // reference field with primitive store
4059 value_compatible = false; // unused
4060 }
4061 if (!instruction_compatible) {
4062 // This is a global failure rather than a class change failure as the instructions and
4063 // the descriptors for the type should have been consistent within the same file at
4064 // compile time
4065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4066 << " to be of type '" << insn_type
4067 << "' but found type '" << *field_type
4068 << "' in put";
4069 return;
4070 }
4071 if (!value_compatible) {
4072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4073 << " of type " << value_type
4074 << " but expected " << *field_type
4075 << " for store to " << PrettyField(field) << " in put";
4076 return;
4077 }
4078 } else {
4079 if (!insn_type.IsAssignableFrom(*field_type)) {
4080 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4081 << " to be compatible with type '" << insn_type
4082 << "' but found type '" << *field_type
4083 << "' in put-object";
4084 return;
4085 }
4086 work_line_->VerifyRegisterType(this, vregA, *field_type);
4087 }
4088 } else if (kAccType == FieldAccessType::kAccGet) {
4089 if (is_primitive) {
4090 if (field_type->Equals(insn_type) ||
4091 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4092 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4093 // expected that read is of the correct primitive type or that int reads are reading
4094 // floats or long reads are reading doubles
4095 } else {
4096 // This is a global failure rather than a class change failure as the instructions and
4097 // the descriptors for the type should have been consistent within the same file at
4098 // compile time
4099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4100 << " to be of type '" << insn_type
4101 << "' but found type '" << *field_type << "' in Get";
4102 return;
4103 }
4104 } else {
4105 if (!insn_type.IsAssignableFrom(*field_type)) {
4106 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4107 << " to be compatible with type '" << insn_type
4108 << "' but found type '" << *field_type
4109 << "' in get-object";
4110 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4111 return;
4112 }
4113 }
4114 if (!field_type->IsLowHalf()) {
4115 work_line_->SetRegisterType(this, vregA, *field_type);
4116 } else {
4117 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004118 }
4119 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004120 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004121 }
4122}
4123
Ian Rogers776ac1f2012-04-13 23:36:36 -07004124bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004125 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004127 return false;
4128 }
4129 return true;
4130}
4131
Stephen Kyle9bc61992014-09-22 13:53:15 +01004132bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4133 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4134 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4136 return false;
4137 }
4138 return true;
4139}
4140
4141bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4142 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4143}
4144
Ian Rogersebbdd872014-07-07 23:53:08 -07004145bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4146 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004147 bool changed = true;
4148 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4149 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004150 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004151 * We haven't processed this instruction before, and we haven't touched the registers here, so
4152 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4153 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004154 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004155 if (!insn_flags_[next_insn].IsReturn()) {
4156 target_line->CopyFromLine(merge_line);
4157 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004158 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004159 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004160 return false;
4161 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004162 // For returns we only care about the operand to the return, all other registers are dead.
4163 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4164 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4165 Instruction::Code opcode = ret_inst->Opcode();
4166 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004167 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004168 } else {
4169 target_line->CopyFromLine(merge_line);
4170 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004171 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004172 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004173 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004174 }
4175 }
4176 }
jeffhaobdb76512011-09-07 11:43:16 -07004177 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004178 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004179 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004180 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004181 if (gDebugVerify) {
4182 copy->CopyFromLine(target_line);
4183 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004184 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004185 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004186 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004187 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004188 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004189 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004190 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004191 << copy->Dump(this) << " MERGE\n"
4192 << merge_line->Dump(this) << " ==\n"
4193 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004194 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004195 if (update_merge_line && changed) {
4196 merge_line->CopyFromLine(target_line);
4197 }
jeffhaobdb76512011-09-07 11:43:16 -07004198 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004199 if (changed) {
4200 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004201 }
4202 return true;
4203}
4204
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004205InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004206 return &insn_flags_[work_insn_idx_];
4207}
4208
Ian Rogersd8f69b02014-09-10 21:43:52 +00004209const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004210 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004211 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004212 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004213 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004214 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4215 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004216 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004217 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004218 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4219 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004220 }
4221 }
4222 if (return_type_ == nullptr) {
4223 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4224 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4225 uint16_t return_type_idx = proto_id.return_type_idx_;
4226 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004227 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004228 }
4229 }
4230 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004231}
4232
Ian Rogersd8f69b02014-09-10 21:43:52 +00004233const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004235 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004236 const char* descriptor
4237 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004238 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004239 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004240 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4241 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004242 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004243 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004244 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004245 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004246 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004247}
4248
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004249std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4250 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004251 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004252 std::vector<int32_t> result;
4253 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004254 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004255 if (type.IsConstant()) {
4256 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004257 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4258 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004259 } else if (type.IsConstantLo()) {
4260 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004261 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4262 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004263 } else if (type.IsConstantHi()) {
4264 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004265 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4266 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004267 } else if (type.IsIntegralTypes()) {
4268 result.push_back(kIntVReg);
4269 result.push_back(0);
4270 } else if (type.IsFloat()) {
4271 result.push_back(kFloatVReg);
4272 result.push_back(0);
4273 } else if (type.IsLong()) {
4274 result.push_back(kLongLoVReg);
4275 result.push_back(0);
4276 result.push_back(kLongHiVReg);
4277 result.push_back(0);
4278 ++i;
4279 } else if (type.IsDouble()) {
4280 result.push_back(kDoubleLoVReg);
4281 result.push_back(0);
4282 result.push_back(kDoubleHiVReg);
4283 result.push_back(0);
4284 ++i;
4285 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4286 result.push_back(kUndefined);
4287 result.push_back(0);
4288 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004289 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004290 result.push_back(kReferenceVReg);
4291 result.push_back(0);
4292 }
4293 }
4294 return result;
4295}
4296
Ian Rogersd8f69b02014-09-10 21:43:52 +00004297const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004298 if (precise) {
4299 // Precise constant type.
4300 return reg_types_.FromCat1Const(value, true);
4301 } else {
4302 // Imprecise constant type.
4303 if (value < -32768) {
4304 return reg_types_.IntConstant();
4305 } else if (value < -128) {
4306 return reg_types_.ShortConstant();
4307 } else if (value < 0) {
4308 return reg_types_.ByteConstant();
4309 } else if (value == 0) {
4310 return reg_types_.Zero();
4311 } else if (value == 1) {
4312 return reg_types_.One();
4313 } else if (value < 128) {
4314 return reg_types_.PosByteConstant();
4315 } else if (value < 32768) {
4316 return reg_types_.PosShortConstant();
4317 } else if (value < 65536) {
4318 return reg_types_.CharConstant();
4319 } else {
4320 return reg_types_.IntConstant();
4321 }
4322 }
4323}
4324
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004325void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004326 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004327}
4328
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004329void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004330 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004331}
jeffhaod1224c72012-02-29 13:43:08 -08004332
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004333void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4334 RegTypeCache::VisitStaticRoots(callback, arg);
4335}
4336
Mathieu Chartier12d625f2015-03-13 11:33:37 -07004337void MethodVerifier::VisitRoots(RootCallback* callback, void* arg, const RootInfo& root_info) {
4338 reg_types_.VisitRoots(callback, arg, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004339}
4340
Ian Rogersd81871c2011-10-03 13:57:23 -07004341} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004342} // namespace art