blob: f9098c769b32d8da770b3d5c26d45c7e82a2bf96 [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"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "field_helper.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
Ian Rogers7b078e82014-09-10 14:44:24 -0700107MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
108 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700109 bool allow_soft_failures,
110 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700111 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700112 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700113 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800114 bool early_failure = false;
115 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700116 const DexFile& dex_file = klass->GetDexFile();
117 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800118 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700119 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700120 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800121 early_failure = true;
122 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700123 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800124 early_failure = true;
125 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700126 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800127 early_failure = true;
128 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
129 }
130 if (early_failure) {
131 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
132 if (Runtime::Current()->IsCompiler()) {
133 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000134 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800135 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700136 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700137 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700138 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700139 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700141 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700142}
143
Ian Rogers7b078e82014-09-10 14:44:24 -0700144MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
145 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700146 Handle<mirror::DexCache> dex_cache,
147 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700148 const DexFile::ClassDef* class_def,
149 bool allow_soft_failures,
150 std::string* error) {
151 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700152 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700153 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700154 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700155 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700156 }
jeffhaof56197c2012-03-05 18:01:54 -0800157 ClassDataItemIterator it(*dex_file, class_data);
158 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
159 it.Next();
160 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700161 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700162 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700163 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700164 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800165 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700166 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800167 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700168 if (method_idx == previous_direct_method_idx) {
169 // smali can create dex files with two encoded_methods sharing the same method_idx
170 // http://code.google.com/p/smali/issues/detail?id=119
171 it.Next();
172 continue;
173 }
174 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700176 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700177 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
178 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700179 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700180 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700181 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700182 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700184 StackHandleScope<1> hs(self);
185 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700186 MethodVerifier::FailureKind result = VerifyMethod(self,
187 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700188 dex_file,
189 dex_cache,
190 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700191 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700192 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700193 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700194 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700195 allow_soft_failures,
196 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700197 if (result != kNoFailure) {
198 if (result == kHardFailure) {
199 hard_fail = true;
200 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700201 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700202 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700203 *error = "Verifier rejected class ";
204 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
205 *error += " due to bad method ";
206 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700208 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800209 }
210 it.Next();
211 }
jeffhao9b0b1882012-10-01 16:51:22 -0700212 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800213 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700214 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800215 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700216 if (method_idx == previous_virtual_method_idx) {
217 // smali can create dex files with two encoded_methods sharing the same method_idx
218 // http://code.google.com/p/smali/issues/detail?id=119
219 it.Next();
220 continue;
221 }
222 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700224 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700225 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
226 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700227 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700228 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700230 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700231 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700232 StackHandleScope<1> hs(self);
233 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700234 MethodVerifier::FailureKind result = VerifyMethod(self,
235 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700236 dex_file,
237 dex_cache,
238 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700239 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700240 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700241 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700242 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700243 allow_soft_failures,
244 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700245 if (result != kNoFailure) {
246 if (result == kHardFailure) {
247 hard_fail = true;
248 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700249 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700250 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700251 *error = "Verifier rejected class ";
252 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
253 *error += " due to bad method ";
254 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700255 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800257 }
258 it.Next();
259 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700260 if (error_count == 0) {
261 return kNoFailure;
262 } else {
263 return hard_fail ? kHardFailure : kSoftFailure;
264 }
jeffhaof56197c2012-03-05 18:01:54 -0800265}
266
Ian Rogers7b078e82014-09-10 14:44:24 -0700267MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700269 Handle<mirror::DexCache> dex_cache,
270 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700273 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700274 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700275 bool allow_soft_failures,
276 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700277 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700278 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700279
Ian Rogers7b078e82014-09-10 14:44:24 -0700280 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700281 method_idx, method, method_access_flags, true, allow_soft_failures,
282 need_precise_constants);
Ian Rogers46960fe2014-05-23 10:43:43 -0700283 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 // Verification completed, however failures may be pending that didn't cause the verification
285 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700286 CHECK(!verifier.have_pending_hard_failure_);
287 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700288 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700289 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700290 << PrettyMethod(method_idx, *dex_file) << "\n");
291 }
Ian Rogersc8982582012-09-07 16:53:25 -0700292 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800293 }
294 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700295 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700296 CHECK_NE(verifier.failures_.size(), 0U);
297 CHECK(verifier.have_pending_hard_failure_);
298 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700299 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800300 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700301 std::cout << "\n" << verifier.info_messages_.str();
302 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800303 }
Ian Rogersc8982582012-09-07 16:53:25 -0700304 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800305 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700306 if (kTimeVerifyMethod) {
307 uint64_t duration_ns = NanoTime() - start_ns;
308 if (duration_ns > MsToNs(100)) {
309 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
310 << " took " << PrettyDuration(duration_ns);
311 }
Ian Rogersc8982582012-09-07 16:53:25 -0700312 }
313 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800314}
315
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700316MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700317 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700318 Handle<mirror::DexCache> dex_cache,
319 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700320 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800321 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700322 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800323 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700324 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
325 class_def, code_item, dex_method_idx, method,
326 method_access_flags, true, true, true, true);
327 verifier->Verify();
328 verifier->DumpFailures(os);
329 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700330 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
331 // and querying any info is dangerous/can abort.
332 if (verifier->have_pending_hard_failure_) {
333 delete verifier;
334 return nullptr;
335 } else {
336 verifier->Dump(os);
337 return verifier;
338 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800339}
340
Ian Rogers7b078e82014-09-10 14:44:24 -0700341MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700342 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
343 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700344 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700345 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700346 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 bool can_load_classes, bool allow_soft_failures,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700348 bool need_precise_constants, bool verify_to_dump)
Ian Rogers7b078e82014-09-10 14:44:24 -0700349 : self_(self),
350 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800351 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800352 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700353 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700354 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700355 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800356 dex_file_(dex_file),
357 dex_cache_(dex_cache),
358 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700359 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800360 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700361 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700362 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700363 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700364 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700365 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800366 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800367 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700368 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200369 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700370 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200371 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700372 has_virtual_or_interface_invokes_(false),
373 verify_to_dump_(verify_to_dump) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800374 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700375 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800376}
377
Mathieu Chartier590fee92013-09-13 13:46:47 -0700378MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800379 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700380 STLDeleteElements(&failure_messages_);
381}
382
Brian Carlstromea46f952013-07-30 01:26:50 -0700383void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700384 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700385 Thread* self = Thread::Current();
386 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700387 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
388 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700389 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700390 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700391 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
392 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700393 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700394 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700395 verifier.FindLocksAtDexPc();
396}
397
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700398static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
399 const Instruction* inst = Instruction::At(code_item->insns_);
400
401 uint32_t insns_size = code_item->insns_size_in_code_units_;
402 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
403 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
404 return true;
405 }
406
407 dex_pc += inst->SizeInCodeUnits();
408 inst = inst->Next();
409 }
410
411 return false;
412}
413
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700414void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700415 CHECK(monitor_enter_dex_pcs_ != nullptr);
416 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700417
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700418 // Quick check whether there are any monitor_enter instructions at all.
419 if (!HasMonitorEnterInstructions(code_item_)) {
420 return;
421 }
422
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700423 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
424 // verification. In practice, the phase we want relies on data structures set up by all the
425 // earlier passes, so we just run the full method verification and bail out early when we've
426 // got what we wanted.
427 Verify();
428}
429
Brian Carlstromea46f952013-07-30 01:26:50 -0700430mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700431 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700432 Thread* self = Thread::Current();
433 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700434 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
435 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700436 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700437 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700438 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
439 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200440 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200441}
442
Brian Carlstromea46f952013-07-30 01:26:50 -0700443mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700444 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200445
446 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
447 // verification. In practice, the phase we want relies on data structures set up by all the
448 // earlier passes, so we just run the full method verification and bail out early when we've
449 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200450 bool success = Verify();
451 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700452 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200453 }
454 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700455 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700456 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200457 }
458 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
459 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200460}
461
Brian Carlstromea46f952013-07-30 01:26:50 -0700462mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700463 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700464 Thread* self = Thread::Current();
465 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700466 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
467 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700468 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700469 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700470 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
471 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200472 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200473}
474
Brian Carlstromea46f952013-07-30 01:26:50 -0700475mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700476 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200477
478 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
479 // verification. In practice, the phase we want relies on data structures set up by all the
480 // earlier passes, so we just run the full method verification and bail out early when we've
481 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200482 bool success = Verify();
483 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700484 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200485 }
486 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700487 if (register_line == nullptr) {
488 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200489 }
490 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
491 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
492 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200493}
494
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700497 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700498 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700500 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700501 } else {
502 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700503 }
jeffhaobdb76512011-09-07 11:43:16 -0700504 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
506 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
508 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700509 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700510 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700511 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800512 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 // Run through the instructions and see if the width checks out.
514 bool result = ComputeWidthsAndCountOps();
515 // Flag instructions guarded by a "try" block and check exception handlers.
516 result = result && ScanTryCatchBlocks();
517 // Perform static instruction verification.
518 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000520 result = result && VerifyCodeFlow();
521 // Compute information for compiler.
522 if (result && Runtime::Current()->IsCompiler()) {
523 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
524 }
525 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700526}
527
Ian Rogers776ac1f2012-04-13 23:36:36 -0700528std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700529 switch (error) {
530 case VERIFY_ERROR_NO_CLASS:
531 case VERIFY_ERROR_NO_FIELD:
532 case VERIFY_ERROR_NO_METHOD:
533 case VERIFY_ERROR_ACCESS_CLASS:
534 case VERIFY_ERROR_ACCESS_FIELD:
535 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700536 case VERIFY_ERROR_INSTANTIATION:
537 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800538 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700539 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
540 // class change and instantiation errors into soft verification errors so that we re-verify
541 // at runtime. We may fail to find or to agree on access because of not yet available class
542 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
543 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
544 // paths" that dynamically perform the verification and cause the behavior to be that akin
545 // to an interpreter.
546 error = VERIFY_ERROR_BAD_CLASS_SOFT;
547 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700548 // If we fail again at runtime, mark that this instruction would throw and force this
549 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700550 have_pending_runtime_throw_failure_ = true;
551 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700552 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553 // Indication that verification should be retried at runtime.
554 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700555 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556 have_pending_hard_failure_ = true;
557 }
558 break;
jeffhaod5347e02012-03-22 17:25:05 -0700559 // Hard verification failures at compile time will still fail at runtime, so the class is
560 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561 case VERIFY_ERROR_BAD_CLASS_HARD: {
562 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700563 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000564 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800565 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566 have_pending_hard_failure_ = true;
567 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800568 }
569 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700570 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700571 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700572 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700573 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700574 failure_messages_.push_back(failure_message);
575 return *failure_message;
576}
577
Ian Rogers576ca0c2014-06-06 15:58:22 -0700578std::ostream& MethodVerifier::LogVerifyInfo() {
579 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
580 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
581}
582
Ian Rogersad0b3a32012-04-16 14:50:24 -0700583void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
584 size_t failure_num = failure_messages_.size();
585 DCHECK_NE(failure_num, 0U);
586 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
587 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700588 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700589 delete last_fail_message;
590}
591
592void MethodVerifier::AppendToLastFailMessage(std::string append) {
593 size_t failure_num = failure_messages_.size();
594 DCHECK_NE(failure_num, 0U);
595 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
596 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800597}
598
Ian Rogers776ac1f2012-04-13 23:36:36 -0700599bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700600 const uint16_t* insns = code_item_->insns_;
601 size_t insns_size = code_item_->insns_size_in_code_units_;
602 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700603 size_t new_instance_count = 0;
604 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700606
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700608 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700609 switch (opcode) {
610 case Instruction::APUT_OBJECT:
611 case Instruction::CHECK_CAST:
612 has_check_casts_ = true;
613 break;
614 case Instruction::INVOKE_VIRTUAL:
615 case Instruction::INVOKE_VIRTUAL_RANGE:
616 case Instruction::INVOKE_INTERFACE:
617 case Instruction::INVOKE_INTERFACE_RANGE:
618 has_virtual_or_interface_invokes_ = true;
619 break;
620 case Instruction::MONITOR_ENTER:
621 monitor_enter_count++;
622 break;
623 case Instruction::NEW_INSTANCE:
624 new_instance_count++;
625 break;
626 default:
627 break;
jeffhaobdb76512011-09-07 11:43:16 -0700628 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700630 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700632 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700633 }
634
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
637 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700638 return false;
639 }
640
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 new_instance_count_ = new_instance_count;
642 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700643 return true;
644}
645
Ian Rogers776ac1f2012-04-13 23:36:36 -0700646bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700648 if (tries_size == 0) {
649 return true;
650 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700652 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700653
654 for (uint32_t idx = 0; idx < tries_size; idx++) {
655 const DexFile::TryItem* try_item = &tries[idx];
656 uint32_t start = try_item->start_addr_;
657 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700658 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
660 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700661 return false;
662 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700664 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
665 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700666 return false;
667 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700668 uint32_t dex_pc = start;
669 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
670 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700672 size_t insn_size = inst->SizeInCodeUnits();
673 dex_pc += insn_size;
674 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700675 }
676 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800677 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700678 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700679 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700680 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700681 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700682 CatchHandlerIterator iterator(handlers_ptr);
683 for (; iterator.HasNext(); iterator.Next()) {
684 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700686 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
687 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700688 return false;
689 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100690 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
691 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
692 << "exception handler begins with move-result* (" << dex_pc << ")";
693 return false;
694 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700696 // Ensure exception types are resolved so that they don't need resolution to be delivered,
697 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700698 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800699 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
700 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700701 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700702 if (exception_type == nullptr) {
703 DCHECK(self_->IsExceptionPending());
704 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700705 }
706 }
jeffhaobdb76512011-09-07 11:43:16 -0700707 }
Ian Rogers0571d352011-11-03 19:51:38 -0700708 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700709 }
jeffhaobdb76512011-09-07 11:43:16 -0700710 return true;
711}
712
Ian Rogers776ac1f2012-04-13 23:36:36 -0700713bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700715
Ian Rogers0c7abda2012-09-19 13:33:42 -0700716 /* 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 -0700717 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700718 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700719
720 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700721 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700723 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 return false;
725 }
726 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700727 // All invoke points are marked as "Throw" points already.
728 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000729 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700730 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000731 } else if (inst->IsReturn()) {
732 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 }
734 dex_pc += inst->SizeInCodeUnits();
735 inst = inst->Next();
736 }
737 return true;
738}
739
Ian Rogers776ac1f2012-04-13 23:36:36 -0700740bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 bool result = true;
742 switch (inst->GetVerifyTypeArgumentA()) {
743 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700744 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 break;
746 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700747 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 break;
749 }
750 switch (inst->GetVerifyTypeArgumentB()) {
751 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700752 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 break;
754 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700755 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 break;
757 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700758 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 break;
760 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700761 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 break;
763 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700764 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 break;
766 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700767 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 break;
769 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700770 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 break;
772 }
773 switch (inst->GetVerifyTypeArgumentC()) {
774 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700775 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700776 break;
777 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700778 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 break;
780 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700781 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 break;
783 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700784 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 break;
786 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700787 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 break;
789 }
790 switch (inst->GetVerifyExtraFlags()) {
791 case Instruction::kVerifyArrayData:
792 result = result && CheckArrayData(code_offset);
793 break;
794 case Instruction::kVerifyBranchTarget:
795 result = result && CheckBranchTarget(code_offset);
796 break;
797 case Instruction::kVerifySwitchTargets:
798 result = result && CheckSwitchTargets(code_offset);
799 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700800 case Instruction::kVerifyVarArgNonZero:
801 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700802 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700803 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
805 "non-range invoke";
806 return false;
807 }
Ian Rogers29a26482014-05-02 15:27:29 -0700808 uint32_t args[Instruction::kMaxVarArgRegs];
809 inst->GetVarArgs(args);
810 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700812 }
Andreas Gampec3314312014-06-19 18:13:29 -0700813 case Instruction::kVerifyVarArgRangeNonZero:
814 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700816 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
817 inst->VRegA() <= 0) {
818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
819 "range invoke";
820 return false;
821 }
Ian Rogers29a26482014-05-02 15:27:29 -0700822 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 break;
824 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 result = false;
827 break;
828 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700829 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
831 result = false;
832 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 return result;
834}
835
Ian Rogers7b078e82014-09-10 14:44:24 -0700836inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
839 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 return false;
841 }
842 return true;
843}
844
Ian Rogers7b078e82014-09-10 14:44:24 -0700845inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
848 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 return false;
850 }
851 return true;
852}
853
Ian Rogers7b078e82014-09-10 14:44:24 -0700854inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
857 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 return false;
859 }
860 return true;
861}
862
Ian Rogers7b078e82014-09-10 14:44:24 -0700863inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
866 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 return false;
868 }
869 return true;
870}
871
Ian Rogers7b078e82014-09-10 14:44:24 -0700872inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
875 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 return false;
877 }
878 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700879 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 return false;
883 }
884 return true;
885}
886
Ian Rogers7b078e82014-09-10 14:44:24 -0700887inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
890 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 return false;
892 }
893 return true;
894}
895
Ian Rogers7b078e82014-09-10 14:44:24 -0700896inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
899 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 return false;
901 }
902 return true;
903}
904
Ian Rogers776ac1f2012-04-13 23:36:36 -0700905bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
908 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 return false;
910 }
911 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700912 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 const char* cp = descriptor;
914 while (*cp++ == '[') {
915 bracket_count++;
916 }
917 if (bracket_count == 0) {
918 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700919 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
920 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 return false;
922 } else if (bracket_count > 255) {
923 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700924 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
925 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 return true;
929}
930
Ian Rogers776ac1f2012-04-13 23:36:36 -0700931bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
933 const uint16_t* insns = code_item_->insns_ + cur_offset;
934 const uint16_t* array_data;
935 int32_t array_data_offset;
936
937 DCHECK_LT(cur_offset, insn_count);
938 /* make sure the start of the array data table is in range */
939 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
940 if ((int32_t) cur_offset + array_data_offset < 0 ||
941 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700943 << ", data offset " << array_data_offset
944 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 return false;
946 }
947 /* offset to array data table is a relative branch-style offset */
948 array_data = insns + array_data_offset;
949 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800950 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
952 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 return false;
954 }
955 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700956 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
958 /* make sure the end of the switch is in range */
959 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
961 << ", data offset " << array_data_offset << ", end "
962 << cur_offset + array_data_offset + table_size
963 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 return false;
965 }
966 return true;
967}
968
Ian Rogers776ac1f2012-04-13 23:36:36 -0700969bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 int32_t offset;
971 bool isConditional, selfOkay;
972 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
973 return false;
974 }
975 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
977 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 return false;
979 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700980 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
981 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
984 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 return false;
986 }
987 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
988 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700989 if (abs_offset < 0 ||
990 (uint32_t) abs_offset >= insn_count ||
991 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700993 << reinterpret_cast<void*>(abs_offset) << ") at "
994 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 return false;
996 }
997 insn_flags_[abs_offset].SetBranchTarget();
998 return true;
999}
1000
Ian Rogers776ac1f2012-04-13 23:36:36 -07001001bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 bool* selfOkay) {
1003 const uint16_t* insns = code_item_->insns_ + cur_offset;
1004 *pConditional = false;
1005 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001006 switch (*insns & 0xff) {
1007 case Instruction::GOTO:
1008 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001009 break;
1010 case Instruction::GOTO_32:
1011 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001012 *selfOkay = true;
1013 break;
1014 case Instruction::GOTO_16:
1015 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001016 break;
1017 case Instruction::IF_EQ:
1018 case Instruction::IF_NE:
1019 case Instruction::IF_LT:
1020 case Instruction::IF_GE:
1021 case Instruction::IF_GT:
1022 case Instruction::IF_LE:
1023 case Instruction::IF_EQZ:
1024 case Instruction::IF_NEZ:
1025 case Instruction::IF_LTZ:
1026 case Instruction::IF_GEZ:
1027 case Instruction::IF_GTZ:
1028 case Instruction::IF_LEZ:
1029 *pOffset = (int16_t) insns[1];
1030 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001031 break;
1032 default:
1033 return false;
1034 break;
1035 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001036 return true;
1037}
1038
Ian Rogers776ac1f2012-04-13 23:36:36 -07001039bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001041 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001043 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1045 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001047 << ", switch offset " << switch_offset
1048 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001049 return false;
1050 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001051 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001053 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001054 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1056 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001057 return false;
1058 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 uint32_t switch_count = switch_insns[1];
1060 int32_t keys_offset, targets_offset;
1061 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001062 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1063 /* 0=sig, 1=count, 2/3=firstKey */
1064 targets_offset = 4;
1065 keys_offset = -1;
1066 expected_signature = Instruction::kPackedSwitchSignature;
1067 } else {
1068 /* 0=sig, 1=count, 2..count*2 = keys */
1069 keys_offset = 2;
1070 targets_offset = 2 + 2 * switch_count;
1071 expected_signature = Instruction::kSparseSwitchSignature;
1072 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001074 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001075 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1076 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1077 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001078 return false;
1079 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001080 /* make sure the end of the switch is in range */
1081 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1083 << ", switch offset " << switch_offset
1084 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001085 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001086 return false;
1087 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001088 /* for a sparse switch, verify the keys are in ascending order */
1089 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1091 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001092 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1093 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1094 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1096 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001097 return false;
1098 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001099 last_key = key;
1100 }
1101 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001102 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 for (uint32_t targ = 0; targ < switch_count; targ++) {
1104 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1105 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1106 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001107 if (abs_offset < 0 ||
1108 abs_offset >= (int32_t) insn_count ||
1109 !insn_flags_[abs_offset].IsOpcode()) {
1110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1111 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1112 << reinterpret_cast<void*>(cur_offset)
1113 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001114 return false;
1115 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 insn_flags_[abs_offset].SetBranchTarget();
1117 }
1118 return true;
1119}
1120
Ian Rogers776ac1f2012-04-13 23:36:36 -07001121bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001122 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001124 return false;
1125 }
1126 uint16_t registers_size = code_item_->registers_size_;
1127 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001128 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1130 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 return false;
1132 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001133 }
1134
1135 return true;
1136}
1137
Ian Rogers776ac1f2012-04-13 23:36:36 -07001138bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 uint16_t registers_size = code_item_->registers_size_;
1140 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1141 // integer overflow when adding them here.
1142 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1144 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001145 return false;
1146 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001147 return true;
1148}
1149
Ian Rogers776ac1f2012-04-13 23:36:36 -07001150bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 uint16_t registers_size = code_item_->registers_size_;
1152 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001153
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001155 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1156 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 }
1158 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001159 reg_table_.Init(kTrackCompilerInterestPoints,
1160 insn_flags_.get(),
1161 insns_size,
1162 registers_size,
1163 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001164
jeffhaobdb76512011-09-07 11:43:16 -07001165
Ian Rogersd0fbd852013-09-24 18:17:04 -07001166 work_line_.reset(RegisterLine::Create(registers_size, this));
1167 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001168
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 /* Initialize register types of method arguments. */
1170 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001171 DCHECK_NE(failures_.size(), 0U);
1172 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001173 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001174 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 return false;
1176 }
1177 /* Perform code flow verification. */
1178 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001179 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001181 }
jeffhaobdb76512011-09-07 11:43:16 -07001182 return true;
1183}
1184
Ian Rogersad0b3a32012-04-16 14:50:24 -07001185std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1186 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001187 for (size_t i = 0; i < failures_.size(); ++i) {
1188 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001189 }
1190 return os;
1191}
1192
Ian Rogers776ac1f2012-04-13 23:36:36 -07001193void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001194 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001195 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 return;
jeffhaobdb76512011-09-07 11:43:16 -07001197 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001198 {
1199 os << "Register Types:\n";
1200 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1201 std::ostream indent_os(&indent_filter);
1202 reg_types_.Dump(indent_os);
1203 }
Ian Rogersb4903572012-10-11 11:52:56 -07001204 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001205 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1206 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 const Instruction* inst = Instruction::At(code_item_->insns_);
1208 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001209 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001211 if (reg_line != nullptr) {
1212 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001213 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001214 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001215 const bool kDumpHexOfInstruction = false;
1216 if (kDumpHexOfInstruction) {
1217 indent_os << inst->DumpHex(5) << " ";
1218 }
1219 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001220 inst = inst->Next();
1221 }
jeffhaobdb76512011-09-07 11:43:16 -07001222}
1223
Ian Rogersd81871c2011-10-03 13:57:23 -07001224static bool IsPrimitiveDescriptor(char descriptor) {
1225 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001226 case 'I':
1227 case 'C':
1228 case 'S':
1229 case 'B':
1230 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001231 case 'F':
1232 case 'D':
1233 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001235 default:
1236 return false;
1237 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001238}
1239
Ian Rogers776ac1f2012-04-13 23:36:36 -07001240bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 RegisterLine* reg_line = reg_table_.GetLine(0);
1242 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1243 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001244
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001246 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001248 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1250 // argument as uninitialized. This restricts field access until the superclass constructor is
1251 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001252 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001253 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001254 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 reg_types_.UninitializedThisArgument(declaring_class));
1256 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001257 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001258 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001260 }
1261
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001262 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001263 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001264 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001265
1266 for (; iterator.HasNext(); iterator.Next()) {
1267 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001268 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 LOG(FATAL) << "Null descriptor";
1270 }
1271 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1273 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 return false;
1275 }
1276 switch (descriptor[0]) {
1277 case 'L':
1278 case '[':
1279 // We assume that reference arguments are initialized. The only way it could be otherwise
1280 // (assuming the caller was verified) is if the current method is <init>, but in that case
1281 // it's effectively considered initialized the instant we reach here (in the sense that we
1282 // can return without doing anything or call virtual methods).
1283 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001284 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001285 if (!reg_type.IsNonZeroReferenceTypes()) {
1286 DCHECK(HasFailures());
1287 return false;
1288 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001289 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 }
1291 break;
1292 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001293 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 break;
1295 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001296 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 break;
1298 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001299 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 break;
1301 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001302 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 break;
1304 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001305 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 break;
1307 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001308 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 break;
1310 case 'J':
1311 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001312 if (cur_arg + 1 >= expected_args) {
1313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1314 << " args, found more (" << descriptor << ")";
1315 return false;
1316 }
1317
Ian Rogers7b078e82014-09-10 14:44:24 -07001318 const RegType* lo_half;
1319 const RegType* hi_half;
1320 if (descriptor[0] == 'J') {
1321 lo_half = &reg_types_.LongLo();
1322 hi_half = &reg_types_.LongHi();
1323 } else {
1324 lo_half = &reg_types_.DoubleLo();
1325 hi_half = &reg_types_.DoubleHi();
1326 }
1327 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 cur_arg++;
1329 break;
1330 }
1331 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1333 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 return false;
1335 }
1336 cur_arg++;
1337 }
1338 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1340 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 return false;
1342 }
1343 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1344 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1345 // format. Only major difference from the method argument format is that 'V' is supported.
1346 bool result;
1347 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1348 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001349 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 size_t i = 0;
1351 do {
1352 i++;
1353 } while (descriptor[i] == '['); // process leading [
1354 if (descriptor[i] == 'L') { // object array
1355 do {
1356 i++; // find closing ;
1357 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1358 result = descriptor[i] == ';';
1359 } else { // primitive array
1360 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1361 }
1362 } else if (descriptor[0] == 'L') {
1363 // could be more thorough here, but shouldn't be required
1364 size_t i = 0;
1365 do {
1366 i++;
1367 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1368 result = descriptor[i] == ';';
1369 } else {
1370 result = false;
1371 }
1372 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1374 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 }
1376 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001377}
1378
Ian Rogers776ac1f2012-04-13 23:36:36 -07001379bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 const uint16_t* insns = code_item_->insns_;
1381 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001382
jeffhaobdb76512011-09-07 11:43:16 -07001383 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 insn_flags_[0].SetChanged();
1385 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001386
jeffhaobdb76512011-09-07 11:43:16 -07001387 /* Continue until no instructions are marked "changed". */
1388 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001389 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1391 uint32_t insn_idx = start_guess;
1392 for (; insn_idx < insns_size; insn_idx++) {
1393 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001394 break;
1395 }
jeffhaobdb76512011-09-07 11:43:16 -07001396 if (insn_idx == insns_size) {
1397 if (start_guess != 0) {
1398 /* try again, starting from the top */
1399 start_guess = 0;
1400 continue;
1401 } else {
1402 /* all flags are clear */
1403 break;
1404 }
1405 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 // We carry the working set of registers from instruction to instruction. If this address can
1407 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1408 // "changed" flags, we need to load the set of registers from the table.
1409 // Because we always prefer to continue on to the next instruction, we should never have a
1410 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1411 // target.
1412 work_insn_idx_ = insn_idx;
1413 if (insn_flags_[insn_idx].IsBranchTarget()) {
1414 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001415 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001416 /*
1417 * Sanity check: retrieve the stored register line (assuming
1418 * a full table) and make sure it actually matches.
1419 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001421 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001422 if (work_line_->CompareLine(register_line) != 0) {
1423 Dump(std::cout);
1424 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001425 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001426 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001427 << " work_line=" << work_line_->Dump(this) << "\n"
1428 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 }
jeffhaobdb76512011-09-07 11:43:16 -07001430 }
jeffhaobdb76512011-09-07 11:43:16 -07001431 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001433 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001434 prepend += " failed to verify: ";
1435 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001436 return false;
1437 }
jeffhaobdb76512011-09-07 11:43:16 -07001438 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 insn_flags_[insn_idx].SetVisited();
1440 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001441 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001442
Ian Rogers1c849e52012-06-28 14:00:33 -07001443 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001444 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001445 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001446 * (besides the wasted space), but it indicates a flaw somewhere
1447 * down the line, possibly in the verifier.
1448 *
1449 * If we've substituted "always throw" instructions into the stream,
1450 * we are almost certainly going to have some dead code.
1451 */
1452 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001454 for (; insn_idx < insns_size;
1455 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001456 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001457 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001458 * may or may not be preceded by a padding NOP (for alignment).
1459 */
1460 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1461 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1462 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001463 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001464 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1465 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1466 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001468 }
1469
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001471 if (dead_start < 0)
1472 dead_start = insn_idx;
1473 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001474 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1475 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001476 dead_start = -1;
1477 }
1478 }
1479 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001480 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1481 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001483 // To dump the state of the verify after a method, do something like:
1484 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1485 // "boolean java.lang.String.equals(java.lang.Object)") {
1486 // LOG(INFO) << info_messages_.str();
1487 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 }
jeffhaobdb76512011-09-07 11:43:16 -07001489 return true;
1490}
1491
Ian Rogers776ac1f2012-04-13 23:36:36 -07001492bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001493 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1494 // We want the state _before_ the instruction, for the case where the dex pc we're
1495 // interested in is itself a monitor-enter instruction (which is a likely place
1496 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001497 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001498 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001499 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1500 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1501 }
1502 }
1503
jeffhaobdb76512011-09-07 11:43:16 -07001504 /*
1505 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001506 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001507 * control to another statement:
1508 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001509 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001510 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001511 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001512 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001513 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001514 * throw an exception that is handled by an encompassing "try"
1515 * block.
1516 *
1517 * We can also return, in which case there is no successor instruction
1518 * from this point.
1519 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001520 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001521 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1523 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001524 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001525
jeffhaobdb76512011-09-07 11:43:16 -07001526 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001527 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001528 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001530 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001531 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 }
jeffhaobdb76512011-09-07 11:43:16 -07001533
1534 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001535 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001536 * can throw an exception, we will copy/merge this into the "catch"
1537 * address rather than work_line, because we don't want the result
1538 * from the "successful" code path (e.g. a check-cast that "improves"
1539 * a type) to be visible to the exception handler.
1540 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001541 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001543 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001544 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001545 }
1546
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001547
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001548 // We need to ensure the work line is consistent while performing validation. When we spot a
1549 // peephole pattern we compute a new line for either the fallthrough instruction or the
1550 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001551 std::unique_ptr<RegisterLine> branch_line;
1552 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001553
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001555 case Instruction::NOP:
1556 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001557 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001558 * a signature that looks like a NOP; if we see one of these in
1559 * the course of executing code then we have a problem.
1560 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001563 }
1564 break;
1565
1566 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001567 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 break;
jeffhaobdb76512011-09-07 11:43:16 -07001569 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001570 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 break;
jeffhaobdb76512011-09-07 11:43:16 -07001572 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001573 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001574 break;
1575 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001576 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001577 break;
jeffhaobdb76512011-09-07 11:43:16 -07001578 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001579 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001580 break;
jeffhaobdb76512011-09-07 11:43:16 -07001581 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001582 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001583 break;
1584 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001585 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001586 break;
jeffhaobdb76512011-09-07 11:43:16 -07001587 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001588 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001589 break;
jeffhaobdb76512011-09-07 11:43:16 -07001590 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001591 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001592 break;
1593
1594 /*
1595 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001597 * might want to hold the result in an actual CPU register, so the
1598 * Dalvik spec requires that these only appear immediately after an
1599 * invoke or filled-new-array.
1600 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001601 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001602 * redundant with the reset done below, but it can make the debug info
1603 * easier to read in some cases.)
1604 */
1605 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001606 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
1608 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001609 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001610 break;
1611 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001612 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001613 break;
1614
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001616 /*
jeffhao60f83e32012-02-13 17:16:30 -08001617 * This statement can only appear as the first instruction in an exception handler. We verify
1618 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001619 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001620 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001621 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001622 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 }
jeffhaobdb76512011-09-07 11:43:16 -07001624 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001625 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001626 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001628 }
jeffhaobdb76512011-09-07 11:43:16 -07001629 }
1630 break;
1631 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001632 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001633 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001634 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1637 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 } else {
1639 // Compilers may generate synthetic functions that write byte values into boolean fields.
1640 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001642 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1644 ((return_type.IsBoolean() || return_type.IsByte() ||
1645 return_type.IsShort() || return_type.IsChar()) &&
1646 src_type.IsInteger()));
1647 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001648 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001649 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001650 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 }
jeffhaobdb76512011-09-07 11:43:16 -07001653 }
1654 }
1655 break;
1656 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001657 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001658 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001659 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001662 } else {
1663 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001664 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001665 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001666 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 }
jeffhaobdb76512011-09-07 11:43:16 -07001669 }
1670 }
1671 break;
1672 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001673 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001674 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 } else {
1678 /* return_type is the *expected* return type, not register value */
1679 DCHECK(!return_type.IsZero());
1680 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001681 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001682 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001683 // Disallow returning uninitialized values and verify that the reference in vAA is an
1684 // instance of the "return_type"
1685 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001686 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1687 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001688 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001689 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1690 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1691 << "' or '" << reg_type << "'";
1692 } else {
1693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1694 << "', but expected from declaration '" << return_type << "'";
1695 }
jeffhaobdb76512011-09-07 11:43:16 -07001696 }
1697 }
1698 }
1699 break;
1700
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001701 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 case Instruction::CONST_4: {
1703 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001704 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001705 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001706 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001707 }
1708 case Instruction::CONST_16: {
1709 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001710 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001711 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001712 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001714 case Instruction::CONST: {
1715 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001716 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001717 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001718 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001719 }
1720 case Instruction::CONST_HIGH16: {
1721 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001722 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001723 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001724 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001725 }
jeffhaobdb76512011-09-07 11:43:16 -07001726 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001727 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001729 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1730 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001731 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001732 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001733 }
1734 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001735 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001736 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1737 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001738 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001739 break;
1740 }
1741 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001743 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1744 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001746 break;
1747 }
1748 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001750 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1751 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001753 break;
1754 }
jeffhaobdb76512011-09-07 11:43:16 -07001755 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001756 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 break;
jeffhaobdb76512011-09-07 11:43:16 -07001758 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001759 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001760 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001762 // Get type from instruction if unresolved then we need an access check
1763 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001764 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001765 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001766 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001767 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001768 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 }
jeffhaobdb76512011-09-07 11:43:16 -07001771 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001772 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
1774 case Instruction::MONITOR_EXIT:
1775 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001776 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001777 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001778 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001779 * to the need to handle asynchronous exceptions, a now-deprecated
1780 * feature that Dalvik doesn't support.)
1781 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001782 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001783 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001784 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001785 * structured locking checks are working, the former would have
1786 * failed on the -enter instruction, and the latter is impossible.
1787 *
1788 * This is fortunate, because issue 3221411 prevents us from
1789 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001790 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001791 * some catch blocks (which will show up as "dead" code when
1792 * we skip them here); if we can't, then the code path could be
1793 * "live" so we still need to check it.
1794 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001795 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001796 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
1798
Ian Rogers28ad40d2011-10-27 15:19:26 -07001799 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001801 /*
1802 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1803 * could be a "upcast" -- not expected, so we don't try to address it.)
1804 *
1805 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001806 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001807 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1809 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001810 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001811 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001812 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001813 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001814 if (klass != nullptr && klass->IsPrimitive()) {
1815 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1816 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1817 << GetDeclaringClass();
1818 break;
1819 }
1820
Ian Rogersad0b3a32012-04-16 14:50:24 -07001821 DCHECK_NE(failures_.size(), 0U);
1822 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001823 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001824 }
1825 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001826 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001827 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001828 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001829 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001830 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001831 if (is_checkcast) {
1832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1833 } else {
1834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1835 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001836 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001837 if (is_checkcast) {
1838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1839 } else {
1840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1841 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001842 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001843 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001844 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001846 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001847 }
jeffhaobdb76512011-09-07 11:43:16 -07001848 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001849 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 }
1851 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001852 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001853 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001854 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001857 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001859 } else {
1860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 }
1862 break;
1863 }
1864 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001865 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001866 if (res_type.IsConflict()) {
1867 DCHECK_NE(failures_.size(), 0U);
1868 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001869 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001870 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1871 // can't create an instance of an interface or abstract class */
1872 if (!res_type.IsInstantiableTypes()) {
1873 Fail(VERIFY_ERROR_INSTANTIATION)
1874 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001875 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001877 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001878 // Any registers holding previous allocations from this address that have not yet been
1879 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001881 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001882 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 break;
1884 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001885 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001887 break;
1888 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001889 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001890 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001892 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001894 just_set_result = true; // Filled new array range sets result register
1895 break;
jeffhaobdb76512011-09-07 11:43:16 -07001896 case Instruction::CMPL_FLOAT:
1897 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001899 break;
1900 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001901 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001902 break;
1903 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001904 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001905 break;
1906 case Instruction::CMPL_DOUBLE:
1907 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001909 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001910 break;
1911 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001912 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001914 break;
1915 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001916 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001920 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001921 break;
1922 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001924 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001925 break;
1926 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001928 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001930 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001931 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001932 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1933 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001934 }
1935 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 }
jeffhaobdb76512011-09-07 11:43:16 -07001937 case Instruction::GOTO:
1938 case Instruction::GOTO_16:
1939 case Instruction::GOTO_32:
1940 /* no effect on or use of registers */
1941 break;
1942
1943 case Instruction::PACKED_SWITCH:
1944 case Instruction::SPARSE_SWITCH:
1945 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001946 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
1948
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 case Instruction::FILL_ARRAY_DATA: {
1950 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001951 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001952 /* array_type can be null if the reg type is Zero */
1953 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001954 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1956 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001957 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001958 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001959 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001960 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1962 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 } else {
jeffhao457cc512012-02-02 16:55:13 -08001964 // Now verify if the element width in the table matches the element width declared in
1965 // the array
1966 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1967 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001969 } else {
1970 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1971 // Since we don't compress the data in Dex, expect to see equal width of data stored
1972 // in the table and expected from the array class.
1973 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1975 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001976 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 }
1978 }
jeffhaobdb76512011-09-07 11:43:16 -07001979 }
1980 }
1981 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 }
jeffhaobdb76512011-09-07 11:43:16 -07001983 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001985 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1986 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 bool mismatch = false;
1988 if (reg_type1.IsZero()) { // zero then integral or reference expected
1989 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1990 } else if (reg_type1.IsReferenceTypes()) { // both references?
1991 mismatch = !reg_type2.IsReferenceTypes();
1992 } else { // both integral?
1993 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1994 }
1995 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1997 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001998 }
1999 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 }
jeffhaobdb76512011-09-07 11:43:16 -07002001 case Instruction::IF_LT:
2002 case Instruction::IF_GE:
2003 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2006 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2009 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002010 }
2011 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 }
jeffhaobdb76512011-09-07 11:43:16 -07002013 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002015 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2018 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002020
2021 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002022 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002023 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002024 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002025 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002026 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002027 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002028 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2029 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2030 work_insn_idx_)) {
2031 break;
2032 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002033 } else {
2034 break;
2035 }
2036
Ian Rogers9b360392013-06-06 14:45:07 -07002037 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002038
2039 /* Check for peep-hole pattern of:
2040 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002041 * instance-of vX, vY, T;
2042 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002043 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002044 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002045 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002046 * and sharpen the type of vY to be type T.
2047 * Note, this pattern can't be if:
2048 * - if there are other branches to this branch,
2049 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002050 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002051 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002052 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2053 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2054 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002055 // Check the type of the instance-of is different than that of registers type, as if they
2056 // are the same there is no work to be done here. Check that the conversion is not to or
2057 // from an unresolved type as type information is imprecise. If the instance-of is to an
2058 // interface then ignore the type information as interfaces can only be treated as Objects
2059 // and we don't want to disallow field and other operations on the object. If the value
2060 // being instance-of checked against is known null (zero) then allow the optimization as
2061 // we didn't have type information. If the merge of the instance-of type with the original
2062 // type is assignable to the original then allow optimization. This check is performed to
2063 // ensure that subsequent merges don't lose type information - such as becoming an
2064 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002065 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002066 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002067
Ian Rogersebbdd872014-07-07 23:53:08 -07002068 if (!orig_type.Equals(cast_type) &&
2069 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002070 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002071 !cast_type.GetClass()->IsInterface() &&
2072 (orig_type.IsZero() ||
2073 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002074 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002075 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002076 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002077 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002078 branch_line.reset(update_line);
2079 }
2080 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002081 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002082 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2083 // See if instance-of was preceded by a move-object operation, common due to the small
2084 // register encoding space of instance-of, and propagate type information to the source
2085 // of the move-object.
2086 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002087 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002088 move_idx--;
2089 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002090 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2091 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2092 work_insn_idx_)) {
2093 break;
2094 }
Ian Rogers9b360392013-06-06 14:45:07 -07002095 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2096 switch (move_inst->Opcode()) {
2097 case Instruction::MOVE_OBJECT:
2098 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002100 }
2101 break;
2102 case Instruction::MOVE_OBJECT_FROM16:
2103 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002104 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002105 }
2106 break;
2107 case Instruction::MOVE_OBJECT_16:
2108 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002110 }
2111 break;
2112 default:
2113 break;
2114 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002115 }
2116 }
2117 }
2118
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 }
jeffhaobdb76512011-09-07 11:43:16 -07002121 case Instruction::IF_LTZ:
2122 case Instruction::IF_GEZ:
2123 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2128 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 }
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 }
jeffhaobdb76512011-09-07 11:43:16 -07002132 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002133 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 break;
jeffhaobdb76512011-09-07 11:43:16 -07002135 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 break;
jeffhaobdb76512011-09-07 11:43:16 -07002138 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002139 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 break;
jeffhaobdb76512011-09-07 11:43:16 -07002141 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 break;
jeffhaobdb76512011-09-07 11:43:16 -07002147 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 break;
2150 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002155 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 break;
2157 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002158 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 break;
2160 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 break;
2163 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002165 break;
2166 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175
jeffhaobdb76512011-09-07 11:43:16 -07002176 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002177 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002178 break;
jeffhaobdb76512011-09-07 11:43:16 -07002179 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002180 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 break;
jeffhaobdb76512011-09-07 11:43:16 -07002182 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002183 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 break;
jeffhaobdb76512011-09-07 11:43:16 -07002185 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002186 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 break;
2188 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002189 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
2191 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002192 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002195 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2196 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 break;
jeffhaobdb76512011-09-07 11:43:16 -07002198
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002200 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 break;
2202 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002203 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 break;
2205 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002206 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 break;
2208 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002209 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002212 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
2214 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002215 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 break;
jeffhaobdb76512011-09-07 11:43:16 -07002217 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002218 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2219 false);
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
2221
jeffhaobdb76512011-09-07 11:43:16 -07002222 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002223 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 break;
jeffhaobdb76512011-09-07 11:43:16 -07002225 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002226 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 break;
jeffhaobdb76512011-09-07 11:43:16 -07002228 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002229 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 break;
jeffhaobdb76512011-09-07 11:43:16 -07002231 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002232 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 break;
2234 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002235 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
2237 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002238 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002239 break;
2240 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002241 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2242 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 break;
2244
2245 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002246 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 break;
2248 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002249 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 break;
2251 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002252 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 break;
2254 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002255 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002258 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
2260 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002261 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002262 break;
2263 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002264 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2265 true);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267
2268 case Instruction::INVOKE_VIRTUAL:
2269 case Instruction::INVOKE_VIRTUAL_RANGE:
2270 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2273 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002274 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2275 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002276 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2277 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002278 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002279 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002280 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002281 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002282 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002283 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002284 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2285 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002286 return_type_class->CannotBeAssignedFromOtherTypes());
2287 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002288 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2289 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002290 }
2291 }
2292 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002294 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2295 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002296 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002297 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002298 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002299 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002300 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002301 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002302 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002304 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 }
jeffhaobdb76512011-09-07 11:43:16 -07002307 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002310 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002311 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002312 const char* return_type_descriptor;
2313 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002314 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002315 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002317 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002318 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002319 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2320 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2321 } else {
2322 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002323 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002324 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002325 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002326 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002327 if (return_type_class != nullptr) {
2328 return_type = &reg_types_.FromClass(return_type_descriptor,
2329 return_type_class,
2330 return_type_class->CannotBeAssignedFromOtherTypes());
2331 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002332 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2333 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002334 }
Ian Rogers46685432012-06-03 22:26:43 -07002335 }
2336 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002337 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 * Some additional checks when calling a constructor. We know from the invocation arg check
2339 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2340 * that to require that called_method->klass is the same as this->klass or this->super,
2341 * allowing the latter only if the "this" argument is the same as the "this" argument to
2342 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002343 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002344 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002345 if (this_type.IsConflict()) // failure.
2346 break;
jeffhaobdb76512011-09-07 11:43:16 -07002347
jeffhaob57e9522012-04-26 18:08:21 -07002348 /* no null refs allowed (?) */
2349 if (this_type.IsZero()) {
2350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2351 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002352 }
jeffhaob57e9522012-04-26 18:08:21 -07002353
2354 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002355 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002356 // TODO: re-enable constructor type verification
2357 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002358 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002359 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2360 // break;
2361 // }
jeffhaob57e9522012-04-26 18:08:21 -07002362
2363 /* arg must be an uninitialized reference */
2364 if (!this_type.IsUninitializedTypes()) {
2365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2366 << this_type;
2367 break;
2368 }
2369
2370 /*
2371 * Replace the uninitialized reference with an initialized one. We need to do this for all
2372 * registers that have the same object instance in them, not just the "this" register.
2373 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002374 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002376 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002377 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2378 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002379 }
2380 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002381 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002382 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002383 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002385 just_set_result = true;
2386 break;
2387 }
2388 case Instruction::INVOKE_STATIC:
2389 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002391 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002392 METHOD_STATIC,
2393 is_range,
2394 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002395 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002396 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002398 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2399 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002400 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002401 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002402 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002403 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002404 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002405 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002407 } else {
2408 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2409 }
jeffhaobdb76512011-09-07 11:43:16 -07002410 just_set_result = true;
2411 }
2412 break;
jeffhaobdb76512011-09-07 11:43:16 -07002413 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002415 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002416 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002417 METHOD_INTERFACE,
2418 is_range,
2419 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002420 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002421 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002422 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2423 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2424 << PrettyMethod(abs_method) << "'";
2425 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002426 }
Ian Rogers0d604842012-04-16 14:50:24 -07002427 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002428 /* Get the type of the "this" arg, which should either be a sub-interface of called
2429 * interface or Object (see comments in RegType::JoinClass).
2430 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002431 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002432 if (this_type.IsZero()) {
2433 /* null pointer always passes (and always fails at runtime) */
2434 } else {
2435 if (this_type.IsUninitializedTypes()) {
2436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2437 << this_type;
2438 break;
2439 }
2440 // In the past we have tried to assert that "called_interface" is assignable
2441 // from "this_type.GetClass()", however, as we do an imprecise Join
2442 // (RegType::JoinClass) we don't have full information on what interfaces are
2443 // implemented by "this_type". For example, two classes may implement the same
2444 // interfaces and have a common parent that doesn't implement the interface. The
2445 // join will set "this_type" to the parent class and a test that this implements
2446 // the interface will incorrectly fail.
2447 }
2448 /*
2449 * We don't have an object instance, so we can't find the concrete method. However, all of
2450 * the type information is in the abstract method, so we're good.
2451 */
2452 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002453 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002454 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002455 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2456 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2457 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2458 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002459 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002460 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002461 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002462 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002463 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 } else {
2465 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2466 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002467 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002468 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 }
jeffhaobdb76512011-09-07 11:43:16 -07002470 case Instruction::NEG_INT:
2471 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002472 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::NEG_LONG:
2475 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002476 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002477 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002480 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002484 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002487 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002488 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002489 break;
2490 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002491 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002492 break;
2493 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002495 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002498 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002499 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002500 break;
2501 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002502 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002503 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002504 break;
2505 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002506 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002507 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002510 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002513 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002514 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002517 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002518 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
2520 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002521 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002522 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002525 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002526 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002529 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002530 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002533 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002534 break;
2535 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002536 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541
2542 case Instruction::ADD_INT:
2543 case Instruction::SUB_INT:
2544 case Instruction::MUL_INT:
2545 case Instruction::REM_INT:
2546 case Instruction::DIV_INT:
2547 case Instruction::SHL_INT:
2548 case Instruction::SHR_INT:
2549 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002550 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002551 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::AND_INT:
2554 case Instruction::OR_INT:
2555 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002556 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002557 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002558 break;
2559 case Instruction::ADD_LONG:
2560 case Instruction::SUB_LONG:
2561 case Instruction::MUL_LONG:
2562 case Instruction::DIV_LONG:
2563 case Instruction::REM_LONG:
2564 case Instruction::AND_LONG:
2565 case Instruction::OR_LONG:
2566 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002567 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002568 reg_types_.LongLo(), reg_types_.LongHi(),
2569 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
2571 case Instruction::SHL_LONG:
2572 case Instruction::SHR_LONG:
2573 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002575 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002576 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578 case Instruction::ADD_FLOAT:
2579 case Instruction::SUB_FLOAT:
2580 case Instruction::MUL_FLOAT:
2581 case Instruction::DIV_FLOAT:
2582 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002583 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2584 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::ADD_DOUBLE:
2587 case Instruction::SUB_DOUBLE:
2588 case Instruction::MUL_DOUBLE:
2589 case Instruction::DIV_DOUBLE:
2590 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002592 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2593 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595 case Instruction::ADD_INT_2ADDR:
2596 case Instruction::SUB_INT_2ADDR:
2597 case Instruction::MUL_INT_2ADDR:
2598 case Instruction::REM_INT_2ADDR:
2599 case Instruction::SHL_INT_2ADDR:
2600 case Instruction::SHR_INT_2ADDR:
2601 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002602 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2603 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::AND_INT_2ADDR:
2606 case Instruction::OR_INT_2ADDR:
2607 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2609 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002612 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2613 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::ADD_LONG_2ADDR:
2616 case Instruction::SUB_LONG_2ADDR:
2617 case Instruction::MUL_LONG_2ADDR:
2618 case Instruction::DIV_LONG_2ADDR:
2619 case Instruction::REM_LONG_2ADDR:
2620 case Instruction::AND_LONG_2ADDR:
2621 case Instruction::OR_LONG_2ADDR:
2622 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002623 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002624 reg_types_.LongLo(), reg_types_.LongHi(),
2625 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::SHL_LONG_2ADDR:
2628 case Instruction::SHR_LONG_2ADDR:
2629 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002630 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002631 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::ADD_FLOAT_2ADDR:
2634 case Instruction::SUB_FLOAT_2ADDR:
2635 case Instruction::MUL_FLOAT_2ADDR:
2636 case Instruction::DIV_FLOAT_2ADDR:
2637 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002638 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2639 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002640 break;
2641 case Instruction::ADD_DOUBLE_2ADDR:
2642 case Instruction::SUB_DOUBLE_2ADDR:
2643 case Instruction::MUL_DOUBLE_2ADDR:
2644 case Instruction::DIV_DOUBLE_2ADDR:
2645 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002646 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002647 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2648 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002651 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002652 case Instruction::MUL_INT_LIT16:
2653 case Instruction::DIV_INT_LIT16:
2654 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002655 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2656 true);
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::AND_INT_LIT16:
2659 case Instruction::OR_INT_LIT16:
2660 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002661 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2662 true);
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::ADD_INT_LIT8:
2665 case Instruction::RSUB_INT_LIT8:
2666 case Instruction::MUL_INT_LIT8:
2667 case Instruction::DIV_INT_LIT8:
2668 case Instruction::REM_INT_LIT8:
2669 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002670 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002671 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002672 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2673 false);
jeffhaobdb76512011-09-07 11:43:16 -07002674 break;
2675 case Instruction::AND_INT_LIT8:
2676 case Instruction::OR_INT_LIT8:
2677 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002678 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2679 false);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002682 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002683 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002684 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2686 }
2687 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002688 // Note: the following instructions encode offsets derived from class linking.
2689 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2690 // meaning if the class linking and resolution were successful.
2691 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002692 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002693 break;
2694 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002695 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002696 break;
2697 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002698 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002699 break;
2700 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002701 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002702 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002703 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002704 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002705 break;
2706 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002707 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002708 break;
2709 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002710 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002711 break;
2712 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002713 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002714 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002715 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002716 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002717 break;
2718 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002719 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002720 break;
2721 case Instruction::INVOKE_VIRTUAL_QUICK:
2722 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2723 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002724 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002725 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002726 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002727 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002728 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002730 } else {
2731 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2732 }
2733 just_set_result = true;
2734 }
2735 break;
2736 }
2737
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002739 case Instruction::UNUSED_3E:
2740 case Instruction::UNUSED_3F:
2741 case Instruction::UNUSED_40:
2742 case Instruction::UNUSED_41:
2743 case Instruction::UNUSED_42:
2744 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002745 case Instruction::UNUSED_79:
2746 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002747 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002748 case Instruction::UNUSED_F0:
2749 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002750 case Instruction::UNUSED_F2:
2751 case Instruction::UNUSED_F3:
2752 case Instruction::UNUSED_F4:
2753 case Instruction::UNUSED_F5:
2754 case Instruction::UNUSED_F6:
2755 case Instruction::UNUSED_F7:
2756 case Instruction::UNUSED_F8:
2757 case Instruction::UNUSED_F9:
2758 case Instruction::UNUSED_FA:
2759 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002760 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002761 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002762 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002763 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002765 break;
2766
2767 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002768 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002769 * complain if an instruction is missing (which is desirable).
2770 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002771 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002772
Ian Rogersad0b3a32012-04-16 14:50:24 -07002773 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002774 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002775 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002776 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002777 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002778 /* immediate failure, reject class */
2779 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2780 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002781 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002782 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002783 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002784 }
jeffhaobdb76512011-09-07 11:43:16 -07002785 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 * If we didn't just set the result register, clear it out. This ensures that you can only use
2787 * "move-result" immediately after the result is set. (We could check this statically, but it's
2788 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002789 */
2790 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002791 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002792 }
2793
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002794
jeffhaobdb76512011-09-07 11:43:16 -07002795
2796 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002797 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002798 *
2799 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002800 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002801 * somebody could get a reference field, check it for zero, and if the
2802 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002803 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002804 * that, and will reject the code.
2805 *
2806 * TODO: avoid re-fetching the branch target
2807 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002808 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002809 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002811 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002813 return false;
2814 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002815 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002816 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002817 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 }
jeffhaobdb76512011-09-07 11:43:16 -07002819 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002820 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002821 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002822 return false;
2823 }
2824 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002825 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002826 return false;
2827 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 }
jeffhaobdb76512011-09-07 11:43:16 -07002829 }
2830
2831 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002832 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002833 *
2834 * We've already verified that the table is structurally sound, so we
2835 * just need to walk through and tag the targets.
2836 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002837 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002838 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2839 const uint16_t* switch_insns = insns + offset_to_switch;
2840 int switch_count = switch_insns[1];
2841 int offset_to_targets, targ;
2842
2843 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2844 /* 0 = sig, 1 = count, 2/3 = first key */
2845 offset_to_targets = 4;
2846 } else {
2847 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002848 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002849 offset_to_targets = 2 + 2 * switch_count;
2850 }
2851
2852 /* verify each switch target */
2853 for (targ = 0; targ < switch_count; targ++) {
2854 int offset;
2855 uint32_t abs_offset;
2856
2857 /* offsets are 32-bit, and only partly endian-swapped */
2858 offset = switch_insns[offset_to_targets + targ * 2] |
2859 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 abs_offset = work_insn_idx_ + offset;
2861 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002862 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002863 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002865 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002866 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002867 }
jeffhaobdb76512011-09-07 11:43:16 -07002868 }
2869 }
2870
2871 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2873 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002874 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002875 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002876 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002877 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002878
Andreas Gampef91baf12014-07-18 15:41:00 -07002879 // Need the linker to try and resolve the handled class to check if it's Throwable.
2880 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2881
Ian Rogers0571d352011-11-03 19:51:38 -07002882 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002883 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2884 if (handler_type_idx == DexFile::kDexNoIndex16) {
2885 has_catch_all_handler = true;
2886 } else {
2887 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002888 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2889 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002890 if (klass != nullptr) {
2891 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2892 has_catch_all_handler = true;
2893 }
2894 } else {
2895 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002896 DCHECK(self_->IsExceptionPending());
2897 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002898 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 }
jeffhaobdb76512011-09-07 11:43:16 -07002900 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2902 * "work_regs", because at runtime the exception will be thrown before the instruction
2903 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002904 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002905 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002906 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 }
jeffhaobdb76512011-09-07 11:43:16 -07002908 }
2909
2910 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2912 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002913 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002914 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002915 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 * The state in work_line reflects the post-execution state. If the current instruction is a
2917 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002918 * it will do so before grabbing the lock).
2919 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002920 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002921 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002923 return false;
2924 }
2925 }
2926 }
2927
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002928 /* Handle "continue". Tag the next consecutive instruction.
2929 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2930 * because it changes work_line_ when performing peephole optimization
2931 * and this change should not be used in those cases.
2932 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002933 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002934 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2935 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002936 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2938 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002939 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002940 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2941 // next instruction isn't one.
2942 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2943 return false;
2944 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002945 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002946 // Make workline consistent with fallthrough computed from peephole optimization.
2947 work_line_->CopyFromLine(fallthrough_line.get());
2948 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002949 if (insn_flags_[next_insn_idx].IsReturn()) {
2950 // For returns we only care about the operand to the return, all other registers are dead.
2951 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2952 Instruction::Code opcode = ret_inst->Opcode();
2953 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002954 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002955 } else {
2956 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002957 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002958 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002959 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002960 }
2961 }
2962 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002963 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002965 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2966 // needed. If the merge changes the state of the registers then the work line will be
2967 // updated.
2968 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002969 return false;
2970 }
2971 } else {
2972 /*
2973 * We're not recording register data for the next instruction, so we don't know what the
2974 * prior state was. We have to assume that something has changed and re-evaluate it.
2975 */
2976 insn_flags_[next_insn_idx].SetChanged();
2977 }
2978 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002979
jeffhaod1f0fde2011-09-08 17:25:33 -07002980 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002981 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 return false;
2984 }
jeffhaobdb76512011-09-07 11:43:16 -07002985 }
2986
2987 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002988 * Update start_guess. Advance to the next instruction of that's
2989 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002990 * neither of those exists we're in a return or throw; leave start_guess
2991 * alone and let the caller sort it out.
2992 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002993 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002994 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2995 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002996 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002997 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002999 }
3000
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3002 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003003
3004 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003005} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003006
Ian Rogersd8f69b02014-09-10 21:43:52 +00003007const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003008 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003009 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003010 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003011 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003012 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3013 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003014 if (result.IsConflict()) {
3015 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3016 << "' in " << referrer;
3017 return result;
3018 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003019 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003020 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003021 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003022 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003023 // check at runtime if access is allowed and so pass here. If result is
3024 // primitive, skip the access check.
3025 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3026 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003027 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003029 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003031}
3032
Ian Rogersd8f69b02014-09-10 21:43:52 +00003033const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003034 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003036 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3038 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003039 CatchHandlerIterator iterator(handlers_ptr);
3040 for (; iterator.HasNext(); iterator.Next()) {
3041 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3042 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003043 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003045 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003046 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003047 if (exception.IsUnresolvedTypes()) {
3048 // We don't know enough about the type. Fail here and let runtime handle it.
3049 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3050 return exception;
3051 } else {
3052 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3053 return reg_types_.Conflict();
3054 }
Jeff Haob878f212014-04-24 16:25:36 -07003055 } else if (common_super == nullptr) {
3056 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003057 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003058 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003060 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003061 if (FailOrAbort(this,
3062 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3063 "java.lang.Throwable is not assignable-from common_super at ",
3064 work_insn_idx_)) {
3065 break;
3066 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003067 }
3068 }
3069 }
3070 }
Ian Rogers0571d352011-11-03 19:51:38 -07003071 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 }
3073 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003074 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003076 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003077 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003079 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003080}
3081
Brian Carlstromea46f952013-07-30 01:26:50 -07003082mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003083 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003084 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003085 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003086 if (klass_type.IsConflict()) {
3087 std::string append(" in attempt to access method ");
3088 append += dex_file_->GetMethodName(method_id);
3089 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003091 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003092 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003093 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003094 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003095 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003096 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003097 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003098 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003099 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003100 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003101
3102 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003104 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003105 res_method = klass->FindInterfaceMethod(name, signature);
3106 } else {
3107 res_method = klass->FindVirtualMethod(name, signature);
3108 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003110 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003112 // If a virtual or interface method wasn't found with the expected type, look in
3113 // the direct methods. This can happen when the wrong invoke type is used or when
3114 // a class has changed, and will be flagged as an error in later checks.
3115 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3116 res_method = klass->FindDirectMethod(name, signature);
3117 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003118 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003119 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3120 << PrettyDescriptor(klass) << "." << name
3121 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003122 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003123 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003124 }
3125 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3127 // enforce them here.
3128 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3130 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003132 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003133 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003134 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3136 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003137 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003138 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003139 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003140 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003141 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003142 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003143 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003144 }
jeffhaode0d9c92012-02-27 13:58:13 -08003145 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3146 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3148 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003150 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003151 // Check that interface methods match interface classes.
3152 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3153 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3154 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003155 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003156 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3157 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3158 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003159 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003160 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 // See if the method type implied by the invoke instruction matches the access flags for the
3162 // target method.
3163 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3164 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3165 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3166 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003167 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3168 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003169 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003171 return res_method;
3172}
3173
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003174template <class T>
3175mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3176 MethodType method_type,
3177 bool is_range,
3178 mirror::ArtMethod* res_method) {
3179 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3180 // match the call to the signature. Also, we might be calling through an abstract method
3181 // definition (which doesn't have register count values).
3182 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3183 /* caught by static verifier */
3184 DCHECK(is_range || expected_args <= 5);
3185 if (expected_args > code_item_->outs_size_) {
3186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3187 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3188 return nullptr;
3189 }
3190
3191 uint32_t arg[5];
3192 if (!is_range) {
3193 inst->GetVarArgs(arg);
3194 }
3195 uint32_t sig_registers = 0;
3196
3197 /*
3198 * Check the "this" argument, which must be an instance of the class that declared the method.
3199 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3200 * rigorous check here (which is okay since we have to do it at runtime).
3201 */
3202 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003204 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3205 CHECK(have_pending_hard_failure_);
3206 return nullptr;
3207 }
3208 if (actual_arg_type.IsUninitializedReference()) {
3209 if (res_method) {
3210 if (!res_method->IsConstructor()) {
3211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3212 return nullptr;
3213 }
3214 } else {
3215 // Check whether the name of the called method is "<init>"
3216 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003217 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3219 return nullptr;
3220 }
3221 }
3222 }
3223 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003224 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003225 if (res_method != nullptr) {
3226 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003227 std::string temp;
3228 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003229 klass->CannotBeAssignedFromOtherTypes());
3230 } else {
3231 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3232 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003233 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003234 dex_file_->StringByTypeIdx(class_idx),
3235 false);
3236 }
3237 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3238 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3239 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3240 << "' not instance of '" << *res_method_class << "'";
3241 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3242 // the compiler.
3243 if (have_pending_hard_failure_) {
3244 return nullptr;
3245 }
3246 }
3247 }
3248 sig_registers = 1;
3249 }
3250
3251 for ( ; it->HasNext(); it->Next()) {
3252 if (sig_registers >= expected_args) {
3253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3254 " arguments, found " << sig_registers << " or more.";
3255 return nullptr;
3256 }
3257
3258 const char* param_descriptor = it->GetDescriptor();
3259
3260 if (param_descriptor == nullptr) {
3261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3262 "component";
3263 return nullptr;
3264 }
3265
Ian Rogersd8f69b02014-09-10 21:43:52 +00003266 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003267 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3268 arg[sig_registers];
3269 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003270 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003271 if (!src_type.IsIntegralTypes()) {
3272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3273 << " but expected " << reg_type;
3274 return res_method;
3275 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003276 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003277 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3278 // compiler.
3279 if (have_pending_hard_failure_) {
3280 return res_method;
3281 }
3282 }
3283 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3284 }
3285 if (expected_args != sig_registers) {
3286 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3287 " arguments, found " << sig_registers;
3288 return nullptr;
3289 }
3290 return res_method;
3291}
3292
3293void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3294 MethodType method_type,
3295 bool is_range) {
3296 // As the method may not have been resolved, make this static check against what we expect.
3297 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3298 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3299 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3300 DexFileParameterIterator it(*dex_file_,
3301 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3302 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3303 nullptr);
3304}
3305
3306class MethodParamListDescriptorIterator {
3307 public:
3308 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3309 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3310 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3311 }
3312
3313 bool HasNext() {
3314 return pos_ < params_size_;
3315 }
3316
3317 void Next() {
3318 ++pos_;
3319 }
3320
3321 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3322 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3323 }
3324
3325 private:
3326 mirror::ArtMethod* res_method_;
3327 size_t pos_;
3328 const DexFile::TypeList* params_;
3329 const size_t params_size_;
3330};
3331
Brian Carlstromea46f952013-07-30 01:26:50 -07003332mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003333 MethodType method_type,
3334 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003335 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003336 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3337 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003338 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003339
Brian Carlstromea46f952013-07-30 01:26:50 -07003340 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003341 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003342 // Check what we can statically.
3343 if (!have_pending_hard_failure_) {
3344 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3345 }
3346 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003347 }
3348
Ian Rogersd81871c2011-10-03 13:57:23 -07003349 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3350 // has a vtable entry for the target method.
3351 if (is_super) {
3352 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003353 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003354 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003355 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003356 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003357 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003358 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003359 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003360 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003361 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003362 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003363 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003364 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003365 << "." << res_method->GetName()
3366 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003367 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 }
3369 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003370
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003371 // Process the target method's signature. This signature may or may not
3372 MethodParamListDescriptorIterator it(res_method);
3373 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3374 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003375}
3376
Brian Carlstromea46f952013-07-30 01:26:50 -07003377mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003378 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003379 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3380 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003381 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003382 if (!actual_arg_type.HasClass()) {
3383 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003384 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003385 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003386 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003387 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003388 if (klass->IsInterface()) {
3389 // Derive Object.class from Class.class.getSuperclass().
3390 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003391 if (FailOrAbort(this, object_klass->IsObjectClass(),
3392 "Failed to find Object class in quickened invoke receiver",
3393 work_insn_idx_)) {
3394 return nullptr;
3395 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003396 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003397 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003398 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003399 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003400 if (FailOrAbort(this, dispatch_class->HasVTable(),
3401 "Receiver class has no vtable for quickened invoke at ",
3402 work_insn_idx_)) {
3403 return nullptr;
3404 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003405 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe7c038102014-10-27 20:08:46 -07003406 if (FailOrAbort(this, static_cast<int32_t>(vtable_index) < dispatch_class->GetVTableLength(),
3407 "Receiver class has not enough vtable slots for quickened invoke at ",
3408 work_insn_idx_)) {
3409 return nullptr;
3410 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003411 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Andreas Gampe7c038102014-10-27 20:08:46 -07003412 if (FailOrAbort(this, !Thread::Current()->IsExceptionPending(),
3413 "Unexpected exception pending for quickened invoke at ",
3414 work_insn_idx_)) {
3415 return nullptr;
3416 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003417 return res_method;
3418}
3419
Brian Carlstromea46f952013-07-30 01:26:50 -07003420mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003421 bool is_range) {
3422 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003423 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003424 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003425 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003427 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003428 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003429 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3430 work_insn_idx_)) {
3431 return nullptr;
3432 }
3433 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3434 work_insn_idx_)) {
3435 return nullptr;
3436 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003437
3438 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3439 // match the call to the signature. Also, we might be calling through an abstract method
3440 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003441 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003442 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003443 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003444 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003445 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3446 /* caught by static verifier */
3447 DCHECK(is_range || expected_args <= 5);
3448 if (expected_args > code_item_->outs_size_) {
3449 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3450 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003451 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003452 }
3453
3454 /*
3455 * Check the "this" argument, which must be an instance of the class that declared the method.
3456 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3457 * rigorous check here (which is okay since we have to do it at runtime).
3458 */
3459 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003461 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003462 }
3463 if (!actual_arg_type.IsZero()) {
3464 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003465 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003466 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003467 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003468 klass->CannotBeAssignedFromOtherTypes());
3469 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003470 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3471 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003472 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003473 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003474 }
3475 }
3476 /*
3477 * Process the target method's signature. This signature may or may not
3478 * have been verified, so we can't assume it's properly formed.
3479 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003480 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003481 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003482 uint32_t arg[5];
3483 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003484 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003485 }
3486 size_t actual_args = 1;
3487 for (size_t param_index = 0; param_index < params_size; param_index++) {
3488 if (actual_args >= expected_args) {
3489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003490 << "'. Expected " << expected_args
3491 << " arguments, processing argument " << actual_args
3492 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003493 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003494 }
3495 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003496 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003497 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003499 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003500 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003501 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003502 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003503 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003504 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003505 return res_method;
3506 }
3507 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3508 }
3509 if (actual_args != expected_args) {
3510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3511 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003512 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003513 } else {
3514 return res_method;
3515 }
3516}
3517
Ian Rogers62342ec2013-06-11 10:26:37 -07003518void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003519 uint32_t type_idx;
3520 if (!is_filled) {
3521 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3522 type_idx = inst->VRegC_22c();
3523 } else if (!is_range) {
3524 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3525 type_idx = inst->VRegB_35c();
3526 } else {
3527 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3528 type_idx = inst->VRegB_3rc();
3529 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003530 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003531 if (res_type.IsConflict()) { // bad class
3532 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003533 } else {
3534 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3535 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003537 } else if (!is_filled) {
3538 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003539 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003540 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003541 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003542 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003543 } else {
3544 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3545 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003546 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003547 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3548 uint32_t arg[5];
3549 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003550 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003551 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003552 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003553 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003554 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3555 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003556 return;
3557 }
3558 }
3559 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003560 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003562 }
3563 }
3564}
3565
Sebastien Hertz5243e912013-05-21 10:55:07 +02003566void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003567 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003568 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003569 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003572 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003573 if (array_type.IsZero()) {
3574 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3575 // instruction type. TODO: have a proper notion of bottom here.
3576 if (!is_primitive || insn_type.IsCategory1Types()) {
3577 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003578 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003579 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003580 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003581 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3582 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003583 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003584 }
jeffhaofc3144e2012-02-01 17:21:15 -08003585 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003587 } else {
3588 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003589 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003590 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003592 << " source for aget-object";
3593 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003595 << " source for category 1 aget";
3596 } else if (is_primitive && !insn_type.Equals(component_type) &&
3597 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003598 (insn_type.IsLong() && component_type.IsDouble()))) {
3599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3600 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003601 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003602 // Use knowledge of the field type which is stronger than the type inferred from the
3603 // instruction, which can't differentiate object types and ints from floats, longs from
3604 // doubles.
3605 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003606 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003607 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003609 component_type.HighHalf(&reg_types_));
3610 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 }
3612 }
3613 }
3614}
3615
Ian Rogersd8f69b02014-09-10 21:43:52 +00003616void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003617 const uint32_t vregA) {
3618 // Primitive assignability rules are weaker than regular assignability rules.
3619 bool instruction_compatible;
3620 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003621 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003622 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003623 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003624 value_compatible = value_type.IsIntegralTypes();
3625 } else if (target_type.IsFloat()) {
3626 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3627 value_compatible = value_type.IsFloatTypes();
3628 } else if (target_type.IsLong()) {
3629 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003630 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3631 // as target_type depends on the resolved type of the field.
3632 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003633 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003634 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3635 } else {
3636 value_compatible = false;
3637 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003638 } else if (target_type.IsDouble()) {
3639 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003640 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3641 // as target_type depends on the resolved type of the field.
3642 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003643 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003644 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3645 } else {
3646 value_compatible = false;
3647 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003648 } else {
3649 instruction_compatible = false; // reference with primitive store
3650 value_compatible = false; // unused
3651 }
3652 if (!instruction_compatible) {
3653 // This is a global failure rather than a class change failure as the instructions and
3654 // the descriptors for the type should have been consistent within the same file at
3655 // compile time.
3656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3657 << "' but expected type '" << target_type << "'";
3658 return;
3659 }
3660 if (!value_compatible) {
3661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3662 << " of type " << value_type << " but expected " << target_type << " for put";
3663 return;
3664 }
3665}
3666
Sebastien Hertz5243e912013-05-21 10:55:07 +02003667void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003668 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003673 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003674 if (array_type.IsZero()) {
3675 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3676 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003677 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003679 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003680 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003681 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003682 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003683 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003684 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003685 if (!component_type.IsReferenceTypes()) {
3686 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3687 << " source for aput-object";
3688 } else {
3689 // The instruction agrees with the type of array, confirm the value to be stored does too
3690 // Note: we use the instruction type (rather than the component type) for aput-object as
3691 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003692 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003693 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003694 }
3695 }
3696 }
3697}
3698
Brian Carlstromea46f952013-07-30 01:26:50 -07003699mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003700 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3701 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003702 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003703 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003704 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3705 field_idx, dex_file_->GetFieldName(field_id),
3706 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003707 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003708 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003709 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003711 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003712 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003713 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3714 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003716 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003717 << dex_file_->GetFieldName(field_id) << ") in "
3718 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003719 DCHECK(self_->IsExceptionPending());
3720 self_->ClearException();
3721 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003722 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3723 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003725 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 } else if (!field->IsStatic()) {
3728 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003729 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003730 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003731 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003732}
3733
Ian Rogersd8f69b02014-09-10 21:43:52 +00003734mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003735 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3736 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003737 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003738 if (klass_type.IsConflict()) {
3739 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3740 field_idx, dex_file_->GetFieldName(field_id),
3741 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003742 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003743 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003744 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003745 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003746 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003747 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003748 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3749 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003750 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003751 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003752 << dex_file_->GetFieldName(field_id) << ") in "
3753 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003754 DCHECK(self_->IsExceptionPending());
3755 self_->ClearException();
3756 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003757 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3758 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003760 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003761 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 } else if (field->IsStatic()) {
3763 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3764 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003765 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 } else if (obj_type.IsZero()) {
3767 // Cannot infer and check type, however, access will cause null pointer exception
3768 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003769 } else if (!obj_type.IsReferenceTypes()) {
3770 // Trying to read a field from something that isn't a reference
3771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3772 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003773 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003774 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003775 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003776 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003777 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003778 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003779 if (obj_type.IsUninitializedTypes() &&
3780 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3781 !field_klass.Equals(GetDeclaringClass()))) {
3782 // Field accesses through uninitialized references are only allowable for constructors where
3783 // the field is declared in this class
3784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003785 << " of a not fully initialized object within the context"
3786 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003787 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003788 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3789 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3790 // of C1. For resolution to occur the declared class of the field must be compatible with
3791 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3792 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3793 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003794 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003795 } else {
3796 return field;
3797 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003798 }
3799}
3800
Andreas Gampe896df402014-10-20 22:25:29 -07003801template <MethodVerifier::FieldAccessType kAccType>
3802void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3803 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003804 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003805 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003806 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003807 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003808 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003809 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003810 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003811 if (UNLIKELY(have_pending_hard_failure_)) {
3812 return;
3813 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003814 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003815 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003817 if (kAccType == FieldAccessType::kAccPut) {
3818 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3819 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3820 << " from other class " << GetDeclaringClass();
3821 return;
3822 }
3823 }
3824
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003825 mirror::Class* field_type_class;
3826 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003827 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003828 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3829 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3830 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003831 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003832 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003833 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003834 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3836 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003837 }
Ian Rogers0d604842012-04-16 14:50:24 -07003838 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003839 if (field_type == nullptr) {
3840 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3841 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003842 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003843 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003844 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003845 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003846 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3847 "Unexpected third access type");
3848 if (kAccType == FieldAccessType::kAccPut) {
3849 // sput or iput.
3850 if (is_primitive) {
3851 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003852 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003853 if (!insn_type.IsAssignableFrom(*field_type)) {
3854 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3855 << " to be compatible with type '" << insn_type
3856 << "' but found type '" << *field_type
3857 << "' in put-object";
3858 return;
3859 }
3860 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003861 }
Andreas Gampe896df402014-10-20 22:25:29 -07003862 } else if (kAccType == FieldAccessType::kAccGet) {
3863 // sget or iget.
3864 if (is_primitive) {
3865 if (field_type->Equals(insn_type) ||
3866 (field_type->IsFloat() && insn_type.IsInteger()) ||
3867 (field_type->IsDouble() && insn_type.IsLong())) {
3868 // expected that read is of the correct primitive type or that int reads are reading
3869 // floats or long reads are reading doubles
3870 } else {
3871 // This is a global failure rather than a class change failure as the instructions and
3872 // the descriptors for the type should have been consistent within the same file at
3873 // compile time
3874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3875 << " to be of type '" << insn_type
3876 << "' but found type '" << *field_type << "' in get";
3877 return;
3878 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003879 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003880 if (!insn_type.IsAssignableFrom(*field_type)) {
3881 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3882 << " to be compatible with type '" << insn_type
3883 << "' but found type '" << *field_type
3884 << "' in get-object";
3885 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3886 return;
3887 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003888 }
Andreas Gampe896df402014-10-20 22:25:29 -07003889 if (!field_type->IsLowHalf()) {
3890 work_line_->SetRegisterType(this, vregA, *field_type);
3891 } else {
3892 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3893 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003894 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003895 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003896 }
3897}
3898
Brian Carlstromea46f952013-07-30 01:26:50 -07003899mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003900 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003901 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3902 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3903 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3904 inst->Opcode() == Instruction::IPUT_QUICK ||
3905 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
Hiroshi Yamauchi5f09be92014-09-26 10:43:59 -07003906 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3907 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3908 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3909 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3910 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003911 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003912 if (!object_type.HasClass()) {
3913 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3914 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003915 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003916 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003917 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3918 field_offset);
3919 if (f == nullptr) {
3920 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3921 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3922 }
3923 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003924}
3925
Andreas Gampe896df402014-10-20 22:25:29 -07003926template <MethodVerifier::FieldAccessType kAccType>
3927void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
3928 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003929 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003930
Brian Carlstromea46f952013-07-30 01:26:50 -07003931 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003932 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3934 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003935 }
Andreas Gampe896df402014-10-20 22:25:29 -07003936
3937 // For an IPUT_QUICK, we now test for final flag of the field.
3938 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003939 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3940 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003941 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003942 return;
3943 }
3944 }
Andreas Gampe896df402014-10-20 22:25:29 -07003945
3946 // Get the field type.
3947 const RegType* field_type;
3948 {
3949 mirror::Class* field_type_class;
3950 {
3951 StackHandleScope<1> hs(Thread::Current());
3952 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3953 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3954 }
3955
3956 if (field_type_class != nullptr) {
3957 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
3958 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003959 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003960 Thread* self = Thread::Current();
3961 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3962 self->ClearException();
3963 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3964 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003965 }
Andreas Gampe896df402014-10-20 22:25:29 -07003966 if (field_type == nullptr) {
3967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003968 return;
3969 }
Andreas Gampe896df402014-10-20 22:25:29 -07003970 }
3971
3972 const uint32_t vregA = inst->VRegA_22c();
3973 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3974 "Unexpected third access type");
3975 if (kAccType == FieldAccessType::kAccPut) {
3976 if (is_primitive) {
3977 // Primitive field assignability rules are weaker than regular assignability rules
3978 bool instruction_compatible;
3979 bool value_compatible;
3980 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
3981 if (field_type->IsIntegralTypes()) {
3982 instruction_compatible = insn_type.IsIntegralTypes();
3983 value_compatible = value_type.IsIntegralTypes();
3984 } else if (field_type->IsFloat()) {
3985 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3986 value_compatible = value_type.IsFloatTypes();
3987 } else if (field_type->IsLong()) {
3988 instruction_compatible = insn_type.IsLong();
3989 value_compatible = value_type.IsLongTypes();
3990 } else if (field_type->IsDouble()) {
3991 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3992 value_compatible = value_type.IsDoubleTypes();
3993 } else {
3994 instruction_compatible = false; // reference field with primitive store
3995 value_compatible = false; // unused
3996 }
3997 if (!instruction_compatible) {
3998 // This is a global failure rather than a class change failure as the instructions and
3999 // the descriptors for the type should have been consistent within the same file at
4000 // compile time
4001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4002 << " to be of type '" << insn_type
4003 << "' but found type '" << *field_type
4004 << "' in put";
4005 return;
4006 }
4007 if (!value_compatible) {
4008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4009 << " of type " << value_type
4010 << " but expected " << *field_type
4011 << " for store to " << PrettyField(field) << " in put";
4012 return;
4013 }
4014 } else {
4015 if (!insn_type.IsAssignableFrom(*field_type)) {
4016 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4017 << " to be compatible with type '" << insn_type
4018 << "' but found type '" << *field_type
4019 << "' in put-object";
4020 return;
4021 }
4022 work_line_->VerifyRegisterType(this, vregA, *field_type);
4023 }
4024 } else if (kAccType == FieldAccessType::kAccGet) {
4025 if (is_primitive) {
4026 if (field_type->Equals(insn_type) ||
4027 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4028 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4029 // expected that read is of the correct primitive type or that int reads are reading
4030 // floats or long reads are reading doubles
4031 } else {
4032 // This is a global failure rather than a class change failure as the instructions and
4033 // the descriptors for the type should have been consistent within the same file at
4034 // compile time
4035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4036 << " to be of type '" << insn_type
4037 << "' but found type '" << *field_type << "' in Get";
4038 return;
4039 }
4040 } else {
4041 if (!insn_type.IsAssignableFrom(*field_type)) {
4042 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4043 << " to be compatible with type '" << insn_type
4044 << "' but found type '" << *field_type
4045 << "' in get-object";
4046 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4047 return;
4048 }
4049 }
4050 if (!field_type->IsLowHalf()) {
4051 work_line_->SetRegisterType(this, vregA, *field_type);
4052 } else {
4053 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004054 }
4055 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004056 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004057 }
4058}
4059
Ian Rogers776ac1f2012-04-13 23:36:36 -07004060bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004061 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004063 return false;
4064 }
4065 return true;
4066}
4067
Stephen Kyle9bc61992014-09-22 13:53:15 +01004068bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4069 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4070 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4072 return false;
4073 }
4074 return true;
4075}
4076
4077bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4078 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4079}
4080
Ian Rogersebbdd872014-07-07 23:53:08 -07004081bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4082 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004083 bool changed = true;
4084 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4085 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004086 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004087 * We haven't processed this instruction before, and we haven't touched the registers here, so
4088 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4089 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004090 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004091 if (!insn_flags_[next_insn].IsReturn()) {
4092 target_line->CopyFromLine(merge_line);
4093 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004094 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004095 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004096 return false;
4097 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004098 // For returns we only care about the operand to the return, all other registers are dead.
4099 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4100 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4101 Instruction::Code opcode = ret_inst->Opcode();
4102 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004103 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004104 } else {
4105 target_line->CopyFromLine(merge_line);
4106 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004107 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004108 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004109 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004110 }
4111 }
4112 }
jeffhaobdb76512011-09-07 11:43:16 -07004113 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004114 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004115 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004116 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004117 if (gDebugVerify) {
4118 copy->CopyFromLine(target_line);
4119 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004120 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004121 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004122 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004123 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004124 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004125 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004126 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004127 << copy->Dump(this) << " MERGE\n"
4128 << merge_line->Dump(this) << " ==\n"
4129 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004130 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004131 if (update_merge_line && changed) {
4132 merge_line->CopyFromLine(target_line);
4133 }
jeffhaobdb76512011-09-07 11:43:16 -07004134 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004135 if (changed) {
4136 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004137 }
4138 return true;
4139}
4140
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004141InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004142 return &insn_flags_[work_insn_idx_];
4143}
4144
Ian Rogersd8f69b02014-09-10 21:43:52 +00004145const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004146 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004147 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004148 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004149 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004150 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4151 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004152 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004153 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004154 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4155 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004156 }
4157 }
4158 if (return_type_ == nullptr) {
4159 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4160 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4161 uint16_t return_type_idx = proto_id.return_type_idx_;
4162 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004163 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004164 }
4165 }
4166 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004167}
4168
Ian Rogersd8f69b02014-09-10 21:43:52 +00004169const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004170 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004171 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004172 const char* descriptor
4173 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004174 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004175 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004176 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4177 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004178 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004179 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004180 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004181 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004182 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004183}
4184
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004185std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4186 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004187 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004188 std::vector<int32_t> result;
4189 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004190 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004191 if (type.IsConstant()) {
4192 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004193 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4194 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004195 } else if (type.IsConstantLo()) {
4196 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004197 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4198 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004199 } else if (type.IsConstantHi()) {
4200 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004201 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4202 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004203 } else if (type.IsIntegralTypes()) {
4204 result.push_back(kIntVReg);
4205 result.push_back(0);
4206 } else if (type.IsFloat()) {
4207 result.push_back(kFloatVReg);
4208 result.push_back(0);
4209 } else if (type.IsLong()) {
4210 result.push_back(kLongLoVReg);
4211 result.push_back(0);
4212 result.push_back(kLongHiVReg);
4213 result.push_back(0);
4214 ++i;
4215 } else if (type.IsDouble()) {
4216 result.push_back(kDoubleLoVReg);
4217 result.push_back(0);
4218 result.push_back(kDoubleHiVReg);
4219 result.push_back(0);
4220 ++i;
4221 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4222 result.push_back(kUndefined);
4223 result.push_back(0);
4224 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004225 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004226 result.push_back(kReferenceVReg);
4227 result.push_back(0);
4228 }
4229 }
4230 return result;
4231}
4232
Ian Rogersd8f69b02014-09-10 21:43:52 +00004233const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004234 if (precise) {
4235 // Precise constant type.
4236 return reg_types_.FromCat1Const(value, true);
4237 } else {
4238 // Imprecise constant type.
4239 if (value < -32768) {
4240 return reg_types_.IntConstant();
4241 } else if (value < -128) {
4242 return reg_types_.ShortConstant();
4243 } else if (value < 0) {
4244 return reg_types_.ByteConstant();
4245 } else if (value == 0) {
4246 return reg_types_.Zero();
4247 } else if (value == 1) {
4248 return reg_types_.One();
4249 } else if (value < 128) {
4250 return reg_types_.PosByteConstant();
4251 } else if (value < 32768) {
4252 return reg_types_.PosShortConstant();
4253 } else if (value < 65536) {
4254 return reg_types_.CharConstant();
4255 } else {
4256 return reg_types_.IntConstant();
4257 }
4258 }
4259}
4260
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004261void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004262 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004263}
4264
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004265void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004266 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004267}
jeffhaod1224c72012-02-29 13:43:08 -08004268
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004269void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4270 RegTypeCache::VisitStaticRoots(callback, arg);
4271}
4272
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004273void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4274 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004275}
4276
Ian Rogersd81871c2011-10-03 13:57:23 -07004277} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004278} // namespace art