blob: 9485172e10ca34ef6d228d556eda9fdb96c80639 [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"
Ian Rogerse5877a12014-07-16 12:06:35 -070033#include "method_helper-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080046#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
48namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070049namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070051static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070052static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070053// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070054
Ian Rogers7b3ddd22013-02-21 15:19:52 -080055void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070056 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070057 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070058 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070059 register_lines_.reset(new RegisterLine*[insns_size]());
60 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070061 for (uint32_t i = 0; i < insns_size; i++) {
62 bool interesting = false;
63 switch (mode) {
64 case kTrackRegsAll:
65 interesting = flags[i].IsOpcode();
66 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070067 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070068 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070069 break;
70 case kTrackRegsBranches:
71 interesting = flags[i].IsBranchTarget();
72 break;
73 default:
74 break;
75 }
76 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070077 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
78 }
79 }
80}
81
82PcToRegisterLineTable::~PcToRegisterLineTable() {
83 for (size_t i = 0; i < size_; i++) {
84 delete register_lines_[i];
85 if (kIsDebugBuild) {
86 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070087 }
88 }
89}
90
Andreas Gampe7c038102014-10-27 20:08:46 -070091// Note: returns true on failure.
92ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
93 const char* error_msg, uint32_t work_insn_idx) {
94 if (kIsDebugBuild) {
95 // In a debug build, abort if the error condition is wrong.
96 DCHECK(condition) << error_msg << work_insn_idx;
97 } else {
98 // In a non-debug build, just fail the class.
99 if (!condition) {
100 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
101 return true;
102 }
103 }
104
105 return false;
106}
107
Ian Rogers7b078e82014-09-10 14:44:24 -0700108MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
109 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700110 bool allow_soft_failures,
111 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700112 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700113 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700114 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800115 bool early_failure = false;
116 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700117 const DexFile& dex_file = klass->GetDexFile();
118 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800119 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700120 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700121 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800122 early_failure = true;
123 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700124 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800125 early_failure = true;
126 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700127 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800128 early_failure = true;
129 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
130 }
131 if (early_failure) {
132 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
133 if (Runtime::Current()->IsCompiler()) {
134 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000135 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700137 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700138 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700139 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700140 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700141 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700143}
144
Ian Rogers7b078e82014-09-10 14:44:24 -0700145MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
146 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700147 Handle<mirror::DexCache> dex_cache,
148 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700149 const DexFile::ClassDef* class_def,
150 bool allow_soft_failures,
151 std::string* error) {
152 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700153 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700155 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700156 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700157 }
jeffhaof56197c2012-03-05 18:01:54 -0800158 ClassDataItemIterator it(*dex_file, class_data);
159 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
160 it.Next();
161 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700162 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700163 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700164 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700165 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800166 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700167 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800168 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700169 if (method_idx == previous_direct_method_idx) {
170 // smali can create dex files with two encoded_methods sharing the same method_idx
171 // http://code.google.com/p/smali/issues/detail?id=119
172 it.Next();
173 continue;
174 }
175 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700176 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700177 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700178 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
179 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700180 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700181 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700182 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700183 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700184 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700185 StackHandleScope<1> hs(self);
186 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700187 MethodVerifier::FailureKind result = VerifyMethod(self,
188 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700189 dex_file,
190 dex_cache,
191 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700192 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700193 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700194 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700195 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700196 allow_soft_failures,
197 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 if (result != kNoFailure) {
199 if (result == kHardFailure) {
200 hard_fail = true;
201 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700202 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700203 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700204 *error = "Verifier rejected class ";
205 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
206 *error += " due to bad method ";
207 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700208 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700209 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800210 }
211 it.Next();
212 }
jeffhao9b0b1882012-10-01 16:51:22 -0700213 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800214 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700215 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800216 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700217 if (method_idx == previous_virtual_method_idx) {
218 // smali can create dex files with two encoded_methods sharing the same method_idx
219 // http://code.google.com/p/smali/issues/detail?id=119
220 it.Next();
221 continue;
222 }
223 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700224 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700225 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700226 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
227 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700228 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700229 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700230 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700231 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700232 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700233 StackHandleScope<1> hs(self);
234 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700235 MethodVerifier::FailureKind result = VerifyMethod(self,
236 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700237 dex_file,
238 dex_cache,
239 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700240 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700241 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700242 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700243 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700244 allow_soft_failures,
245 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700246 if (result != kNoFailure) {
247 if (result == kHardFailure) {
248 hard_fail = true;
249 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700250 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700251 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700252 *error = "Verifier rejected class ";
253 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
254 *error += " due to bad method ";
255 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700257 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800258 }
259 it.Next();
260 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700261 if (error_count == 0) {
262 return kNoFailure;
263 } else {
264 return hard_fail ? kHardFailure : kSoftFailure;
265 }
jeffhaof56197c2012-03-05 18:01:54 -0800266}
267
Ian Rogers7b078e82014-09-10 14:44:24 -0700268MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700270 Handle<mirror::DexCache> dex_cache,
271 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700272 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700274 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700275 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700276 bool allow_soft_failures,
277 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700278 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700279 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700280
Ian Rogers7b078e82014-09-10 14:44:24 -0700281 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700282 method_idx, method, method_access_flags, true, allow_soft_failures,
283 need_precise_constants);
Ian Rogers46960fe2014-05-23 10:43:43 -0700284 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700285 // Verification completed, however failures may be pending that didn't cause the verification
286 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700287 CHECK(!verifier.have_pending_hard_failure_);
288 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700289 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700290 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700291 << PrettyMethod(method_idx, *dex_file) << "\n");
292 }
Ian Rogersc8982582012-09-07 16:53:25 -0700293 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800294 }
295 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700296 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700297 CHECK_NE(verifier.failures_.size(), 0U);
298 CHECK(verifier.have_pending_hard_failure_);
299 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700300 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800301 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700302 std::cout << "\n" << verifier.info_messages_.str();
303 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800304 }
Ian Rogersc8982582012-09-07 16:53:25 -0700305 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800306 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700307 if (kTimeVerifyMethod) {
308 uint64_t duration_ns = NanoTime() - start_ns;
309 if (duration_ns > MsToNs(100)) {
310 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
311 << " took " << PrettyDuration(duration_ns);
312 }
Ian Rogersc8982582012-09-07 16:53:25 -0700313 }
314 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800315}
316
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700317MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700318 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700319 Handle<mirror::DexCache> dex_cache,
320 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700321 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700323 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800324 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700325 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
326 class_def, code_item, dex_method_idx, method,
327 method_access_flags, true, true, true, true);
328 verifier->Verify();
329 verifier->DumpFailures(os);
330 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700331 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
332 // and querying any info is dangerous/can abort.
333 if (verifier->have_pending_hard_failure_) {
334 delete verifier;
335 return nullptr;
336 } else {
337 verifier->Dump(os);
338 return verifier;
339 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800340}
341
Ian Rogers7b078e82014-09-10 14:44:24 -0700342MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700343 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
344 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700345 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700346 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700347 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 bool can_load_classes, bool allow_soft_failures,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700349 bool need_precise_constants, bool verify_to_dump)
Ian Rogers7b078e82014-09-10 14:44:24 -0700350 : self_(self),
351 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800352 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800353 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700354 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700355 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700356 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800357 dex_file_(dex_file),
358 dex_cache_(dex_cache),
359 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700360 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800361 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700362 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700363 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700364 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700365 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700366 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800367 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800368 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700369 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200370 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700371 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200372 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700373 has_virtual_or_interface_invokes_(false),
374 verify_to_dump_(verify_to_dump) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800375 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700376 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800377}
378
Mathieu Chartier590fee92013-09-13 13:46:47 -0700379MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800380 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700381 STLDeleteElements(&failure_messages_);
382}
383
Brian Carlstromea46f952013-07-30 01:26:50 -0700384void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700385 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700386 Thread* self = Thread::Current();
387 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700388 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
389 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700390 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700391 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700392 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
393 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700394 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700396 verifier.FindLocksAtDexPc();
397}
398
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700399static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
400 const Instruction* inst = Instruction::At(code_item->insns_);
401
402 uint32_t insns_size = code_item->insns_size_in_code_units_;
403 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
404 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
405 return true;
406 }
407
408 dex_pc += inst->SizeInCodeUnits();
409 inst = inst->Next();
410 }
411
412 return false;
413}
414
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700415void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700416 CHECK(monitor_enter_dex_pcs_ != nullptr);
417 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700418
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700419 // Quick check whether there are any monitor_enter instructions at all.
420 if (!HasMonitorEnterInstructions(code_item_)) {
421 return;
422 }
423
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700424 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
425 // verification. In practice, the phase we want relies on data structures set up by all the
426 // earlier passes, so we just run the full method verification and bail out early when we've
427 // got what we wanted.
428 Verify();
429}
430
Brian Carlstromea46f952013-07-30 01:26:50 -0700431mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700432 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700433 Thread* self = Thread::Current();
434 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700435 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
436 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700437 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700438 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700439 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
440 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200441 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200442}
443
Brian Carlstromea46f952013-07-30 01:26:50 -0700444mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700445 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200446
447 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
448 // verification. In practice, the phase we want relies on data structures set up by all the
449 // earlier passes, so we just run the full method verification and bail out early when we've
450 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200451 bool success = Verify();
452 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700453 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200454 }
455 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700456 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700457 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200458 }
459 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
460 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200461}
462
Brian Carlstromea46f952013-07-30 01:26:50 -0700463mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700464 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700465 Thread* self = Thread::Current();
466 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700467 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
468 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700469 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700470 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700471 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
472 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200473 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200474}
475
Brian Carlstromea46f952013-07-30 01:26:50 -0700476mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700477 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200478
479 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
480 // verification. In practice, the phase we want relies on data structures set up by all the
481 // earlier passes, so we just run the full method verification and bail out early when we've
482 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200483 bool success = Verify();
484 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700485 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200486 }
487 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700488 if (register_line == nullptr) {
489 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200490 }
491 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
492 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
493 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200494}
495
Ian Rogersad0b3a32012-04-16 14:50:24 -0700496bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700497 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700498 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700499 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700501 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700502 } else {
503 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700504 }
jeffhaobdb76512011-09-07 11:43:16 -0700505 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
507 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
509 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700511 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800513 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700514 // Run through the instructions and see if the width checks out.
515 bool result = ComputeWidthsAndCountOps();
516 // Flag instructions guarded by a "try" block and check exception handlers.
517 result = result && ScanTryCatchBlocks();
518 // Perform static instruction verification.
519 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700520 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000521 result = result && VerifyCodeFlow();
522 // Compute information for compiler.
523 if (result && Runtime::Current()->IsCompiler()) {
524 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
525 }
526 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700527}
528
Ian Rogers776ac1f2012-04-13 23:36:36 -0700529std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700530 switch (error) {
531 case VERIFY_ERROR_NO_CLASS:
532 case VERIFY_ERROR_NO_FIELD:
533 case VERIFY_ERROR_NO_METHOD:
534 case VERIFY_ERROR_ACCESS_CLASS:
535 case VERIFY_ERROR_ACCESS_FIELD:
536 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700537 case VERIFY_ERROR_INSTANTIATION:
538 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800539 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700540 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
541 // class change and instantiation errors into soft verification errors so that we re-verify
542 // at runtime. We may fail to find or to agree on access because of not yet available class
543 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
544 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
545 // paths" that dynamically perform the verification and cause the behavior to be that akin
546 // to an interpreter.
547 error = VERIFY_ERROR_BAD_CLASS_SOFT;
548 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700549 // If we fail again at runtime, mark that this instruction would throw and force this
550 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700551 have_pending_runtime_throw_failure_ = true;
552 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700554 // Indication that verification should be retried at runtime.
555 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700556 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557 have_pending_hard_failure_ = true;
558 }
559 break;
jeffhaod5347e02012-03-22 17:25:05 -0700560 // Hard verification failures at compile time will still fail at runtime, so the class is
561 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700562 case VERIFY_ERROR_BAD_CLASS_HARD: {
563 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700564 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000565 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800566 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700567 have_pending_hard_failure_ = true;
568 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800569 }
570 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700571 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700572 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700573 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700574 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700575 failure_messages_.push_back(failure_message);
576 return *failure_message;
577}
578
Ian Rogers576ca0c2014-06-06 15:58:22 -0700579std::ostream& MethodVerifier::LogVerifyInfo() {
580 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
581 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
582}
583
Ian Rogersad0b3a32012-04-16 14:50:24 -0700584void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
585 size_t failure_num = failure_messages_.size();
586 DCHECK_NE(failure_num, 0U);
587 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
588 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700589 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590 delete last_fail_message;
591}
592
593void MethodVerifier::AppendToLastFailMessage(std::string append) {
594 size_t failure_num = failure_messages_.size();
595 DCHECK_NE(failure_num, 0U);
596 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
597 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800598}
599
Ian Rogers776ac1f2012-04-13 23:36:36 -0700600bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 const uint16_t* insns = code_item_->insns_;
602 size_t insns_size = code_item_->insns_size_in_code_units_;
603 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700604 size_t new_instance_count = 0;
605 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700607
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700609 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700610 switch (opcode) {
611 case Instruction::APUT_OBJECT:
612 case Instruction::CHECK_CAST:
613 has_check_casts_ = true;
614 break;
615 case Instruction::INVOKE_VIRTUAL:
616 case Instruction::INVOKE_VIRTUAL_RANGE:
617 case Instruction::INVOKE_INTERFACE:
618 case Instruction::INVOKE_INTERFACE_RANGE:
619 has_virtual_or_interface_invokes_ = true;
620 break;
621 case Instruction::MONITOR_ENTER:
622 monitor_enter_count++;
623 break;
624 case Instruction::NEW_INSTANCE:
625 new_instance_count++;
626 break;
627 default:
628 break;
jeffhaobdb76512011-09-07 11:43:16 -0700629 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700630 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700631 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700633 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700634 }
635
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
638 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700639 return false;
640 }
641
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 new_instance_count_ = new_instance_count;
643 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700644 return true;
645}
646
Ian Rogers776ac1f2012-04-13 23:36:36 -0700647bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700649 if (tries_size == 0) {
650 return true;
651 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700653 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700654
655 for (uint32_t idx = 0; idx < tries_size; idx++) {
656 const DexFile::TryItem* try_item = &tries[idx];
657 uint32_t start = try_item->start_addr_;
658 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700659 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
661 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700662 return false;
663 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700665 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
666 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700667 return false;
668 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700669 uint32_t dex_pc = start;
670 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
671 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700673 size_t insn_size = inst->SizeInCodeUnits();
674 dex_pc += insn_size;
675 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700676 }
677 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800678 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700679 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700680 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700681 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700682 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700683 CatchHandlerIterator iterator(handlers_ptr);
684 for (; iterator.HasNext(); iterator.Next()) {
685 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700687 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
688 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700689 return false;
690 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100691 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
692 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
693 << "exception handler begins with move-result* (" << dex_pc << ")";
694 return false;
695 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700697 // Ensure exception types are resolved so that they don't need resolution to be delivered,
698 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700699 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800700 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
701 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700702 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700703 if (exception_type == nullptr) {
704 DCHECK(self_->IsExceptionPending());
705 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700706 }
707 }
jeffhaobdb76512011-09-07 11:43:16 -0700708 }
Ian Rogers0571d352011-11-03 19:51:38 -0700709 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700710 }
jeffhaobdb76512011-09-07 11:43:16 -0700711 return true;
712}
713
Ian Rogers776ac1f2012-04-13 23:36:36 -0700714bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700716
Ian Rogers0c7abda2012-09-19 13:33:42 -0700717 /* 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 -0700718 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700719 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700720
721 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700722 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700724 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 return false;
726 }
727 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700728 // All invoke points are marked as "Throw" points already.
729 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000730 if (inst->IsBranch()) {
731 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
732 // The compiler also needs safepoints for fall-through to loop heads.
733 // Such a loop head must be a target of a branch.
734 int32_t offset = 0;
735 bool cond, self_ok;
736 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
737 DCHECK(target_ok);
738 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
739 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700740 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000741 } else if (inst->IsReturn()) {
742 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 }
744 dex_pc += inst->SizeInCodeUnits();
745 inst = inst->Next();
746 }
747 return true;
748}
749
Ian Rogers776ac1f2012-04-13 23:36:36 -0700750bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 bool result = true;
752 switch (inst->GetVerifyTypeArgumentA()) {
753 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700754 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 break;
756 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700757 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 break;
759 }
760 switch (inst->GetVerifyTypeArgumentB()) {
761 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700762 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 break;
764 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700765 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 break;
767 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700768 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 break;
770 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700771 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 break;
773 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700774 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 break;
776 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700777 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 break;
779 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700780 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 break;
782 }
783 switch (inst->GetVerifyTypeArgumentC()) {
784 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700785 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 break;
787 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700788 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 break;
790 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700791 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 break;
793 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700794 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 break;
796 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700797 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 break;
799 }
800 switch (inst->GetVerifyExtraFlags()) {
801 case Instruction::kVerifyArrayData:
802 result = result && CheckArrayData(code_offset);
803 break;
804 case Instruction::kVerifyBranchTarget:
805 result = result && CheckBranchTarget(code_offset);
806 break;
807 case Instruction::kVerifySwitchTargets:
808 result = result && CheckSwitchTargets(code_offset);
809 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700810 case Instruction::kVerifyVarArgNonZero:
811 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700812 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700813 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
815 "non-range invoke";
816 return false;
817 }
Ian Rogers29a26482014-05-02 15:27:29 -0700818 uint32_t args[Instruction::kMaxVarArgRegs];
819 inst->GetVarArgs(args);
820 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700822 }
Andreas Gampec3314312014-06-19 18:13:29 -0700823 case Instruction::kVerifyVarArgRangeNonZero:
824 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700826 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
827 inst->VRegA() <= 0) {
828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
829 "range invoke";
830 return false;
831 }
Ian Rogers29a26482014-05-02 15:27:29 -0700832 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 break;
834 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 result = false;
837 break;
838 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700839 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
841 result = false;
842 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 return result;
844}
845
Ian Rogers7b078e82014-09-10 14:44:24 -0700846inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
849 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 return false;
851 }
852 return true;
853}
854
Ian Rogers7b078e82014-09-10 14:44:24 -0700855inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
858 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 return false;
860 }
861 return true;
862}
863
Ian Rogers7b078e82014-09-10 14:44:24 -0700864inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
867 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 return false;
869 }
870 return true;
871}
872
Ian Rogers7b078e82014-09-10 14:44:24 -0700873inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
876 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return false;
878 }
879 return true;
880}
881
Ian Rogers7b078e82014-09-10 14:44:24 -0700882inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
885 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 return false;
887 }
888 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700889 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 return false;
893 }
894 return true;
895}
896
Ian Rogers7b078e82014-09-10 14:44:24 -0700897inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
900 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 return false;
902 }
903 return true;
904}
905
Ian Rogers7b078e82014-09-10 14:44:24 -0700906inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
909 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 return false;
911 }
912 return true;
913}
914
Ian Rogers776ac1f2012-04-13 23:36:36 -0700915bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
918 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 return false;
920 }
921 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700922 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 const char* cp = descriptor;
924 while (*cp++ == '[') {
925 bracket_count++;
926 }
927 if (bracket_count == 0) {
928 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700929 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
930 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 return false;
932 } else if (bracket_count > 255) {
933 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700934 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
935 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 return false;
937 }
938 return true;
939}
940
Ian Rogers776ac1f2012-04-13 23:36:36 -0700941bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
943 const uint16_t* insns = code_item_->insns_ + cur_offset;
944 const uint16_t* array_data;
945 int32_t array_data_offset;
946
947 DCHECK_LT(cur_offset, insn_count);
948 /* make sure the start of the array data table is in range */
949 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
950 if ((int32_t) cur_offset + array_data_offset < 0 ||
951 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700953 << ", data offset " << array_data_offset
954 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 return false;
956 }
957 /* offset to array data table is a relative branch-style offset */
958 array_data = insns + array_data_offset;
959 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800960 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
962 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 return false;
964 }
965 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700966 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
968 /* make sure the end of the switch is in range */
969 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
971 << ", data offset " << array_data_offset << ", end "
972 << cur_offset + array_data_offset + table_size
973 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 return false;
975 }
976 return true;
977}
978
Ian Rogers776ac1f2012-04-13 23:36:36 -0700979bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 int32_t offset;
981 bool isConditional, selfOkay;
982 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
983 return false;
984 }
985 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
987 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 return false;
989 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700990 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
991 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
994 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 return false;
996 }
997 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
998 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700999 if (abs_offset < 0 ||
1000 (uint32_t) abs_offset >= insn_count ||
1001 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001003 << reinterpret_cast<void*>(abs_offset) << ") at "
1004 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 return false;
1006 }
1007 insn_flags_[abs_offset].SetBranchTarget();
1008 return true;
1009}
1010
Ian Rogers776ac1f2012-04-13 23:36:36 -07001011bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 bool* selfOkay) {
1013 const uint16_t* insns = code_item_->insns_ + cur_offset;
1014 *pConditional = false;
1015 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001016 switch (*insns & 0xff) {
1017 case Instruction::GOTO:
1018 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001019 break;
1020 case Instruction::GOTO_32:
1021 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001022 *selfOkay = true;
1023 break;
1024 case Instruction::GOTO_16:
1025 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001026 break;
1027 case Instruction::IF_EQ:
1028 case Instruction::IF_NE:
1029 case Instruction::IF_LT:
1030 case Instruction::IF_GE:
1031 case Instruction::IF_GT:
1032 case Instruction::IF_LE:
1033 case Instruction::IF_EQZ:
1034 case Instruction::IF_NEZ:
1035 case Instruction::IF_LTZ:
1036 case Instruction::IF_GEZ:
1037 case Instruction::IF_GTZ:
1038 case Instruction::IF_LEZ:
1039 *pOffset = (int16_t) insns[1];
1040 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001041 break;
1042 default:
1043 return false;
1044 break;
1045 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001046 return true;
1047}
1048
Ian Rogers776ac1f2012-04-13 23:36:36 -07001049bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001051 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001053 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1055 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001057 << ", switch offset " << switch_offset
1058 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001059 return false;
1060 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001061 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001063 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001064 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1066 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001067 return false;
1068 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 uint32_t switch_count = switch_insns[1];
1070 int32_t keys_offset, targets_offset;
1071 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001072 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1073 /* 0=sig, 1=count, 2/3=firstKey */
1074 targets_offset = 4;
1075 keys_offset = -1;
1076 expected_signature = Instruction::kPackedSwitchSignature;
1077 } else {
1078 /* 0=sig, 1=count, 2..count*2 = keys */
1079 keys_offset = 2;
1080 targets_offset = 2 + 2 * switch_count;
1081 expected_signature = Instruction::kSparseSwitchSignature;
1082 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001084 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001085 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1086 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1087 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001088 return false;
1089 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001090 /* make sure the end of the switch is in range */
1091 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1093 << ", switch offset " << switch_offset
1094 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001095 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001096 return false;
1097 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001098 /* for a sparse switch, verify the keys are in ascending order */
1099 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1101 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001102 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1103 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1104 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1106 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001107 return false;
1108 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001109 last_key = key;
1110 }
1111 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001112 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 for (uint32_t targ = 0; targ < switch_count; targ++) {
1114 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1115 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1116 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001117 if (abs_offset < 0 ||
1118 abs_offset >= (int32_t) insn_count ||
1119 !insn_flags_[abs_offset].IsOpcode()) {
1120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1121 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1122 << reinterpret_cast<void*>(cur_offset)
1123 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001124 return false;
1125 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 insn_flags_[abs_offset].SetBranchTarget();
1127 }
1128 return true;
1129}
1130
Ian Rogers776ac1f2012-04-13 23:36:36 -07001131bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001132 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 return false;
1135 }
1136 uint16_t registers_size = code_item_->registers_size_;
1137 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001138 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1140 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 return false;
1142 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001143 }
1144
1145 return true;
1146}
1147
Ian Rogers776ac1f2012-04-13 23:36:36 -07001148bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 uint16_t registers_size = code_item_->registers_size_;
1150 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1151 // integer overflow when adding them here.
1152 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1154 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 return false;
1156 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001157 return true;
1158}
1159
Ian Rogers776ac1f2012-04-13 23:36:36 -07001160bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 uint16_t registers_size = code_item_->registers_size_;
1162 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001163
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001165 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1166 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 }
1168 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001169 reg_table_.Init(kTrackCompilerInterestPoints,
1170 insn_flags_.get(),
1171 insns_size,
1172 registers_size,
1173 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001174
jeffhaobdb76512011-09-07 11:43:16 -07001175
Ian Rogersd0fbd852013-09-24 18:17:04 -07001176 work_line_.reset(RegisterLine::Create(registers_size, this));
1177 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001178
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 /* Initialize register types of method arguments. */
1180 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001181 DCHECK_NE(failures_.size(), 0U);
1182 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001183 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001184 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 return false;
1186 }
1187 /* Perform code flow verification. */
1188 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001189 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001191 }
jeffhaobdb76512011-09-07 11:43:16 -07001192 return true;
1193}
1194
Ian Rogersad0b3a32012-04-16 14:50:24 -07001195std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1196 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001197 for (size_t i = 0; i < failures_.size(); ++i) {
1198 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001199 }
1200 return os;
1201}
1202
Ian Rogers776ac1f2012-04-13 23:36:36 -07001203void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001204 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001205 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 return;
jeffhaobdb76512011-09-07 11:43:16 -07001207 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001208 {
1209 os << "Register Types:\n";
1210 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1211 std::ostream indent_os(&indent_filter);
1212 reg_types_.Dump(indent_os);
1213 }
Ian Rogersb4903572012-10-11 11:52:56 -07001214 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001215 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1216 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 const Instruction* inst = Instruction::At(code_item_->insns_);
1218 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001219 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001221 if (reg_line != nullptr) {
1222 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001223 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001224 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001225 const bool kDumpHexOfInstruction = false;
1226 if (kDumpHexOfInstruction) {
1227 indent_os << inst->DumpHex(5) << " ";
1228 }
1229 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001230 inst = inst->Next();
1231 }
jeffhaobdb76512011-09-07 11:43:16 -07001232}
1233
Ian Rogersd81871c2011-10-03 13:57:23 -07001234static bool IsPrimitiveDescriptor(char descriptor) {
1235 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001236 case 'I':
1237 case 'C':
1238 case 'S':
1239 case 'B':
1240 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001241 case 'F':
1242 case 'D':
1243 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001245 default:
1246 return false;
1247 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001248}
1249
Ian Rogers776ac1f2012-04-13 23:36:36 -07001250bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 RegisterLine* reg_line = reg_table_.GetLine(0);
1252 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1253 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001254
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001256 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001258 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1260 // argument as uninitialized. This restricts field access until the superclass constructor is
1261 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001262 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001263 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001264 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 reg_types_.UninitializedThisArgument(declaring_class));
1266 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001267 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001268 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001270 }
1271
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001272 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001273 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001274 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001275
1276 for (; iterator.HasNext(); iterator.Next()) {
1277 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001278 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 LOG(FATAL) << "Null descriptor";
1280 }
1281 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1283 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 return false;
1285 }
1286 switch (descriptor[0]) {
1287 case 'L':
1288 case '[':
1289 // We assume that reference arguments are initialized. The only way it could be otherwise
1290 // (assuming the caller was verified) is if the current method is <init>, but in that case
1291 // it's effectively considered initialized the instant we reach here (in the sense that we
1292 // can return without doing anything or call virtual methods).
1293 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001294 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001295 if (!reg_type.IsNonZeroReferenceTypes()) {
1296 DCHECK(HasFailures());
1297 return false;
1298 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001299 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 }
1301 break;
1302 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001303 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 break;
1305 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001306 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 break;
1308 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001309 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 break;
1311 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001312 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 break;
1314 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001315 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 break;
1317 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001318 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 break;
1320 case 'J':
1321 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001322 if (cur_arg + 1 >= expected_args) {
1323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1324 << " args, found more (" << descriptor << ")";
1325 return false;
1326 }
1327
Ian Rogers7b078e82014-09-10 14:44:24 -07001328 const RegType* lo_half;
1329 const RegType* hi_half;
1330 if (descriptor[0] == 'J') {
1331 lo_half = &reg_types_.LongLo();
1332 hi_half = &reg_types_.LongHi();
1333 } else {
1334 lo_half = &reg_types_.DoubleLo();
1335 hi_half = &reg_types_.DoubleHi();
1336 }
1337 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 cur_arg++;
1339 break;
1340 }
1341 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1343 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 return false;
1345 }
1346 cur_arg++;
1347 }
1348 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1350 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 return false;
1352 }
1353 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1354 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1355 // format. Only major difference from the method argument format is that 'V' is supported.
1356 bool result;
1357 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1358 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001359 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 size_t i = 0;
1361 do {
1362 i++;
1363 } while (descriptor[i] == '['); // process leading [
1364 if (descriptor[i] == 'L') { // object array
1365 do {
1366 i++; // find closing ;
1367 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1368 result = descriptor[i] == ';';
1369 } else { // primitive array
1370 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1371 }
1372 } else if (descriptor[0] == 'L') {
1373 // could be more thorough here, but shouldn't be required
1374 size_t i = 0;
1375 do {
1376 i++;
1377 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1378 result = descriptor[i] == ';';
1379 } else {
1380 result = false;
1381 }
1382 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1384 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 }
1386 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001387}
1388
Ian Rogers776ac1f2012-04-13 23:36:36 -07001389bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 const uint16_t* insns = code_item_->insns_;
1391 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001392
jeffhaobdb76512011-09-07 11:43:16 -07001393 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 insn_flags_[0].SetChanged();
1395 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001396
jeffhaobdb76512011-09-07 11:43:16 -07001397 /* Continue until no instructions are marked "changed". */
1398 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001399 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1401 uint32_t insn_idx = start_guess;
1402 for (; insn_idx < insns_size; insn_idx++) {
1403 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001404 break;
1405 }
jeffhaobdb76512011-09-07 11:43:16 -07001406 if (insn_idx == insns_size) {
1407 if (start_guess != 0) {
1408 /* try again, starting from the top */
1409 start_guess = 0;
1410 continue;
1411 } else {
1412 /* all flags are clear */
1413 break;
1414 }
1415 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 // We carry the working set of registers from instruction to instruction. If this address can
1417 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1418 // "changed" flags, we need to load the set of registers from the table.
1419 // Because we always prefer to continue on to the next instruction, we should never have a
1420 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1421 // target.
1422 work_insn_idx_ = insn_idx;
1423 if (insn_flags_[insn_idx].IsBranchTarget()) {
1424 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001425 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001426 /*
1427 * Sanity check: retrieve the stored register line (assuming
1428 * a full table) and make sure it actually matches.
1429 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001431 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 if (work_line_->CompareLine(register_line) != 0) {
1433 Dump(std::cout);
1434 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001435 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001436 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001437 << " work_line=" << work_line_->Dump(this) << "\n"
1438 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 }
jeffhaobdb76512011-09-07 11:43:16 -07001440 }
jeffhaobdb76512011-09-07 11:43:16 -07001441 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001443 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001444 prepend += " failed to verify: ";
1445 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001446 return false;
1447 }
jeffhaobdb76512011-09-07 11:43:16 -07001448 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 insn_flags_[insn_idx].SetVisited();
1450 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001451 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001452
Ian Rogers1c849e52012-06-28 14:00:33 -07001453 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001454 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001455 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001456 * (besides the wasted space), but it indicates a flaw somewhere
1457 * down the line, possibly in the verifier.
1458 *
1459 * If we've substituted "always throw" instructions into the stream,
1460 * we are almost certainly going to have some dead code.
1461 */
1462 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001464 for (; insn_idx < insns_size;
1465 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001466 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001467 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001468 * may or may not be preceded by a padding NOP (for alignment).
1469 */
1470 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1471 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1472 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001473 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001474 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1475 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1476 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001478 }
1479
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001481 if (dead_start < 0)
1482 dead_start = insn_idx;
1483 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001484 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1485 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001486 dead_start = -1;
1487 }
1488 }
1489 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001490 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1491 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001492 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001493 // To dump the state of the verify after a method, do something like:
1494 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1495 // "boolean java.lang.String.equals(java.lang.Object)") {
1496 // LOG(INFO) << info_messages_.str();
1497 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001498 }
jeffhaobdb76512011-09-07 11:43:16 -07001499 return true;
1500}
1501
Ian Rogers776ac1f2012-04-13 23:36:36 -07001502bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001503 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1504 // We want the state _before_ the instruction, for the case where the dex pc we're
1505 // interested in is itself a monitor-enter instruction (which is a likely place
1506 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001507 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001508 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001509 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1510 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1511 }
1512 }
1513
jeffhaobdb76512011-09-07 11:43:16 -07001514 /*
1515 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001516 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001517 * control to another statement:
1518 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001519 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001520 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001521 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001522 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001523 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001524 * throw an exception that is handled by an encompassing "try"
1525 * block.
1526 *
1527 * We can also return, in which case there is no successor instruction
1528 * from this point.
1529 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001530 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001531 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1533 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001534 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001535
jeffhaobdb76512011-09-07 11:43:16 -07001536 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001537 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001538 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001540 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001541 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543
1544 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001545 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001546 * can throw an exception, we will copy/merge this into the "catch"
1547 * address rather than work_line, because we don't want the result
1548 * from the "successful" code path (e.g. a check-cast that "improves"
1549 * a type) to be visible to the exception handler.
1550 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001551 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001553 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001555 }
1556
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001557
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001558 // We need to ensure the work line is consistent while performing validation. When we spot a
1559 // peephole pattern we compute a new line for either the fallthrough instruction or the
1560 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001561 std::unique_ptr<RegisterLine> branch_line;
1562 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001563
Sebastien Hertz5243e912013-05-21 10:55:07 +02001564 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001565 case Instruction::NOP:
1566 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001567 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001568 * a signature that looks like a NOP; if we see one of these in
1569 * the course of executing code then we have a problem.
1570 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001573 }
1574 break;
1575
1576 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001577 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001578 break;
jeffhaobdb76512011-09-07 11:43:16 -07001579 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001580 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001581 break;
jeffhaobdb76512011-09-07 11:43:16 -07001582 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001583 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001584 break;
1585 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001586 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001587 break;
jeffhaobdb76512011-09-07 11:43:16 -07001588 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001589 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001590 break;
jeffhaobdb76512011-09-07 11:43:16 -07001591 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001592 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001593 break;
1594 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001595 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001596 break;
jeffhaobdb76512011-09-07 11:43:16 -07001597 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001598 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 break;
jeffhaobdb76512011-09-07 11:43:16 -07001600 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001601 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001602 break;
1603
1604 /*
1605 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001606 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001607 * might want to hold the result in an actual CPU register, so the
1608 * Dalvik spec requires that these only appear immediately after an
1609 * invoke or filled-new-array.
1610 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001611 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001612 * redundant with the reset done below, but it can make the debug info
1613 * easier to read in some cases.)
1614 */
1615 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001616 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001617 break;
1618 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001619 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001620 break;
1621 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001622 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001623 break;
1624
Ian Rogersd81871c2011-10-03 13:57:23 -07001625 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001626 /*
jeffhao60f83e32012-02-13 17:16:30 -08001627 * This statement can only appear as the first instruction in an exception handler. We verify
1628 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001629 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001630 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001631 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001632 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001633 }
jeffhaobdb76512011-09-07 11:43:16 -07001634 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001635 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001636 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 }
jeffhaobdb76512011-09-07 11:43:16 -07001639 }
1640 break;
1641 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001642 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001643 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001644 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1647 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 } else {
1649 // Compilers may generate synthetic functions that write byte values into boolean fields.
1650 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001652 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1654 ((return_type.IsBoolean() || return_type.IsByte() ||
1655 return_type.IsShort() || return_type.IsChar()) &&
1656 src_type.IsInteger()));
1657 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001658 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001659 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001660 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001661 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001662 }
jeffhaobdb76512011-09-07 11:43:16 -07001663 }
1664 }
1665 break;
1666 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001667 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001668 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001669 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001670 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 } else {
1673 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001674 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001675 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001676 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001677 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 }
jeffhaobdb76512011-09-07 11:43:16 -07001679 }
1680 }
1681 break;
1682 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001684 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001686 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 } else {
1688 /* return_type is the *expected* return type, not register value */
1689 DCHECK(!return_type.IsZero());
1690 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001692 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001693 // Disallow returning uninitialized values and verify that the reference in vAA is an
1694 // instance of the "return_type"
1695 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001696 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1697 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001698 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001699 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1700 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1701 << "' or '" << reg_type << "'";
1702 } else {
1703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1704 << "', but expected from declaration '" << return_type << "'";
1705 }
jeffhaobdb76512011-09-07 11:43:16 -07001706 }
1707 }
1708 }
1709 break;
1710
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001711 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 case Instruction::CONST_4: {
1713 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001714 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001715 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001716 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 }
1718 case Instruction::CONST_16: {
1719 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001721 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001722 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001724 case Instruction::CONST: {
1725 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001726 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001727 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001728 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001729 }
1730 case Instruction::CONST_HIGH16: {
1731 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001732 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001733 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001734 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001735 }
jeffhaobdb76512011-09-07 11:43:16 -07001736 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001737 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001739 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1740 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001741 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001742 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001743 }
1744 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001745 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001746 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1747 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001748 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001749 break;
1750 }
1751 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001753 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1754 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001755 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001756 break;
1757 }
1758 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001759 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001760 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1761 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001762 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001763 break;
1764 }
jeffhaobdb76512011-09-07 11:43:16 -07001765 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001766 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001767 break;
jeffhaobdb76512011-09-07 11:43:16 -07001768 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001772 // Get type from instruction if unresolved then we need an access check
1773 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001774 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001775 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001776 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001777 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001779 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 }
jeffhaobdb76512011-09-07 11:43:16 -07001781 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001782 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001783 break;
1784 case Instruction::MONITOR_EXIT:
1785 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001786 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001787 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001788 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001789 * to the need to handle asynchronous exceptions, a now-deprecated
1790 * feature that Dalvik doesn't support.)
1791 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001792 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001793 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001794 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001795 * structured locking checks are working, the former would have
1796 * failed on the -enter instruction, and the latter is impossible.
1797 *
1798 * This is fortunate, because issue 3221411 prevents us from
1799 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001800 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001801 * some catch blocks (which will show up as "dead" code when
1802 * we skip them here); if we can't, then the code path could be
1803 * "live" so we still need to check it.
1804 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001805 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001806 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
1808
Ian Rogers28ad40d2011-10-27 15:19:26 -07001809 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001811 /*
1812 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1813 * could be a "upcast" -- not expected, so we don't try to address it.)
1814 *
1815 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001816 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001817 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1819 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001820 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001821 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001822 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001823 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001824 if (klass != nullptr && klass->IsPrimitive()) {
1825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1826 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1827 << GetDeclaringClass();
1828 break;
1829 }
1830
Ian Rogersad0b3a32012-04-16 14:50:24 -07001831 DCHECK_NE(failures_.size(), 0U);
1832 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001833 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001834 }
1835 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001836 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001837 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001838 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001839 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001840 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 if (is_checkcast) {
1842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1843 } else {
1844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1845 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001846 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001847 if (is_checkcast) {
1848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1849 } else {
1850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1851 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001852 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001853 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001854 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001856 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001859 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 }
1861 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001862 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001863 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001864 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001867 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001869 } else {
1870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 }
1872 break;
1873 }
1874 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001875 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001876 if (res_type.IsConflict()) {
1877 DCHECK_NE(failures_.size(), 0U);
1878 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001879 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001880 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1881 // can't create an instance of an interface or abstract class */
1882 if (!res_type.IsInstantiableTypes()) {
1883 Fail(VERIFY_ERROR_INSTANTIATION)
1884 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001885 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001887 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001888 // Any registers holding previous allocations from this address that have not yet been
1889 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001891 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001892 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 break;
1894 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001895 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001896 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001897 break;
1898 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001900 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001902 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001904 just_set_result = true; // Filled new array range sets result register
1905 break;
jeffhaobdb76512011-09-07 11:43:16 -07001906 case Instruction::CMPL_FLOAT:
1907 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001909 break;
1910 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001911 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001912 break;
1913 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001915 break;
1916 case Instruction::CMPL_DOUBLE:
1917 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001920 break;
1921 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001922 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001923 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001924 break;
1925 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001927 break;
1928 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001930 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001931 break;
1932 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001934 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001935 break;
1936 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001937 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001938 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001941 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001942 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1943 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001944 }
1945 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 }
jeffhaobdb76512011-09-07 11:43:16 -07001947 case Instruction::GOTO:
1948 case Instruction::GOTO_16:
1949 case Instruction::GOTO_32:
1950 /* no effect on or use of registers */
1951 break;
1952
1953 case Instruction::PACKED_SWITCH:
1954 case Instruction::SPARSE_SWITCH:
1955 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001957 break;
1958
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 case Instruction::FILL_ARRAY_DATA: {
1960 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001961 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001962 /* array_type can be null if the reg type is Zero */
1963 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001964 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1966 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001967 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001968 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001969 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001970 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1972 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 } else {
jeffhao457cc512012-02-02 16:55:13 -08001974 // Now verify if the element width in the table matches the element width declared in
1975 // the array
1976 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1977 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001979 } else {
1980 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1981 // Since we don't compress the data in Dex, expect to see equal width of data stored
1982 // in the table and expected from the array class.
1983 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1985 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001986 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
1988 }
jeffhaobdb76512011-09-07 11:43:16 -07001989 }
1990 }
1991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 }
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001995 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1996 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 bool mismatch = false;
1998 if (reg_type1.IsZero()) { // zero then integral or reference expected
1999 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2000 } else if (reg_type1.IsReferenceTypes()) { // both references?
2001 mismatch = !reg_type2.IsReferenceTypes();
2002 } else { // both integral?
2003 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2004 }
2005 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2007 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002008 }
2009 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 }
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::IF_LT:
2012 case Instruction::IF_GE:
2013 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002015 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2016 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2019 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002020 }
2021 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 }
jeffhaobdb76512011-09-07 11:43:16 -07002023 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002025 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2028 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002030
2031 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002032 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002033 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002034 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002035 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002036 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002037 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002038 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2039 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2040 work_insn_idx_)) {
2041 break;
2042 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002043 } else {
2044 break;
2045 }
2046
Ian Rogers9b360392013-06-06 14:45:07 -07002047 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002048
2049 /* Check for peep-hole pattern of:
2050 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002051 * instance-of vX, vY, T;
2052 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002053 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002054 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002055 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002056 * and sharpen the type of vY to be type T.
2057 * Note, this pattern can't be if:
2058 * - if there are other branches to this branch,
2059 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002060 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002061 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002062 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2063 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2064 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002065 // Check the type of the instance-of is different than that of registers type, as if they
2066 // are the same there is no work to be done here. Check that the conversion is not to or
2067 // from an unresolved type as type information is imprecise. If the instance-of is to an
2068 // interface then ignore the type information as interfaces can only be treated as Objects
2069 // and we don't want to disallow field and other operations on the object. If the value
2070 // being instance-of checked against is known null (zero) then allow the optimization as
2071 // we didn't have type information. If the merge of the instance-of type with the original
2072 // type is assignable to the original then allow optimization. This check is performed to
2073 // ensure that subsequent merges don't lose type information - such as becoming an
2074 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002075 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002076 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002077
Ian Rogersebbdd872014-07-07 23:53:08 -07002078 if (!orig_type.Equals(cast_type) &&
2079 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002080 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002081 !cast_type.GetClass()->IsInterface() &&
2082 (orig_type.IsZero() ||
2083 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002084 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002085 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002086 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002087 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002088 branch_line.reset(update_line);
2089 }
2090 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002091 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002092 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2093 // See if instance-of was preceded by a move-object operation, common due to the small
2094 // register encoding space of instance-of, and propagate type information to the source
2095 // of the move-object.
2096 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002097 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002098 move_idx--;
2099 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002100 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2101 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2102 work_insn_idx_)) {
2103 break;
2104 }
Ian Rogers9b360392013-06-06 14:45:07 -07002105 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2106 switch (move_inst->Opcode()) {
2107 case Instruction::MOVE_OBJECT:
2108 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002110 }
2111 break;
2112 case Instruction::MOVE_OBJECT_FROM16:
2113 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002114 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002115 }
2116 break;
2117 case Instruction::MOVE_OBJECT_16:
2118 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002119 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002120 }
2121 break;
2122 default:
2123 break;
2124 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002125 }
2126 }
2127 }
2128
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 }
jeffhaobdb76512011-09-07 11:43:16 -07002131 case Instruction::IF_LTZ:
2132 case Instruction::IF_GEZ:
2133 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002135 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2138 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 }
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 }
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 break;
jeffhaobdb76512011-09-07 11:43:16 -07002145 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 break;
jeffhaobdb76512011-09-07 11:43:16 -07002148 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 break;
jeffhaobdb76512011-09-07 11:43:16 -07002151 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002155 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 break;
jeffhaobdb76512011-09-07 11:43:16 -07002157 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002158 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 break;
2160 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002162 break;
2163
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 break;
2167 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 break;
2170 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 break;
2173 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002177 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002180 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002183 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002184 break;
2185
jeffhaobdb76512011-09-07 11:43:16 -07002186 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002187 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 break;
jeffhaobdb76512011-09-07 11:43:16 -07002189 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002190 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 break;
jeffhaobdb76512011-09-07 11:43:16 -07002192 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002193 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 break;
jeffhaobdb76512011-09-07 11:43:16 -07002195 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002196 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 break;
2198 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002199 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
2201 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002202 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
2204 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002205 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2206 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 break;
jeffhaobdb76512011-09-07 11:43:16 -07002208
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002210 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 break;
2212 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002213 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 break;
2215 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002216 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 break;
2218 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002219 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
2221 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002222 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
2224 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002225 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 break;
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002228 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2229 false);
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231
jeffhaobdb76512011-09-07 11:43:16 -07002232 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002233 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 break;
jeffhaobdb76512011-09-07 11:43:16 -07002235 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002236 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 break;
jeffhaobdb76512011-09-07 11:43:16 -07002238 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002239 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 break;
jeffhaobdb76512011-09-07 11:43:16 -07002241 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002242 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 break;
2244 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002245 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
2247 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002248 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002251 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2252 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 break;
2254
2255 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002256 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 break;
2258 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002259 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 break;
2261 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002262 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 break;
2264 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002265 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002268 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002271 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002274 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2275 true);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277
2278 case Instruction::INVOKE_VIRTUAL:
2279 case Instruction::INVOKE_VIRTUAL_RANGE:
2280 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002282 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2283 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002284 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2285 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002286 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2287 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002288 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002289 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002290 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002291 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002292 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002293 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002294 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2295 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002296 return_type_class->CannotBeAssignedFromOtherTypes());
2297 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002298 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2299 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002300 }
2301 }
2302 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002304 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2305 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002306 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002307 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002308 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002309 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002310 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002312 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002314 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 }
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002320 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002321 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002322 const char* return_type_descriptor;
2323 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002324 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002325 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002327 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002328 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002329 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2330 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2331 } else {
2332 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002333 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002334 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002335 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002336 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002337 if (return_type_class != nullptr) {
2338 return_type = &reg_types_.FromClass(return_type_descriptor,
2339 return_type_class,
2340 return_type_class->CannotBeAssignedFromOtherTypes());
2341 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002342 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2343 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002344 }
Ian Rogers46685432012-06-03 22:26:43 -07002345 }
2346 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002347 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 * Some additional checks when calling a constructor. We know from the invocation arg check
2349 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2350 * that to require that called_method->klass is the same as this->klass or this->super,
2351 * allowing the latter only if the "this" argument is the same as the "this" argument to
2352 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002353 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002354 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002355 if (this_type.IsConflict()) // failure.
2356 break;
jeffhaobdb76512011-09-07 11:43:16 -07002357
jeffhaob57e9522012-04-26 18:08:21 -07002358 /* no null refs allowed (?) */
2359 if (this_type.IsZero()) {
2360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2361 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002362 }
jeffhaob57e9522012-04-26 18:08:21 -07002363
2364 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002365 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002366 // TODO: re-enable constructor type verification
2367 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002368 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002369 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2370 // break;
2371 // }
jeffhaob57e9522012-04-26 18:08:21 -07002372
2373 /* arg must be an uninitialized reference */
2374 if (!this_type.IsUninitializedTypes()) {
2375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2376 << this_type;
2377 break;
2378 }
2379
2380 /*
2381 * Replace the uninitialized reference with an initialized one. We need to do this for all
2382 * registers that have the same object instance in them, not just the "this" register.
2383 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002384 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002385 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002386 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002387 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2388 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002389 }
2390 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002391 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002392 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002393 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002394 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002395 just_set_result = true;
2396 break;
2397 }
2398 case Instruction::INVOKE_STATIC:
2399 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002400 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002401 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002402 METHOD_STATIC,
2403 is_range,
2404 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002405 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002407 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002408 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2409 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002410 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002411 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002412 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002413 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002414 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002415 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002416 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002417 } else {
2418 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2419 }
jeffhaobdb76512011-09-07 11:43:16 -07002420 just_set_result = true;
2421 }
2422 break;
jeffhaobdb76512011-09-07 11:43:16 -07002423 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002425 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002426 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002427 METHOD_INTERFACE,
2428 is_range,
2429 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002430 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002431 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002432 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2433 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2434 << PrettyMethod(abs_method) << "'";
2435 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002436 }
Ian Rogers0d604842012-04-16 14:50:24 -07002437 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002438 /* Get the type of the "this" arg, which should either be a sub-interface of called
2439 * interface or Object (see comments in RegType::JoinClass).
2440 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002442 if (this_type.IsZero()) {
2443 /* null pointer always passes (and always fails at runtime) */
2444 } else {
2445 if (this_type.IsUninitializedTypes()) {
2446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2447 << this_type;
2448 break;
2449 }
2450 // In the past we have tried to assert that "called_interface" is assignable
2451 // from "this_type.GetClass()", however, as we do an imprecise Join
2452 // (RegType::JoinClass) we don't have full information on what interfaces are
2453 // implemented by "this_type". For example, two classes may implement the same
2454 // interfaces and have a common parent that doesn't implement the interface. The
2455 // join will set "this_type" to the parent class and a test that this implements
2456 // the interface will incorrectly fail.
2457 }
2458 /*
2459 * We don't have an object instance, so we can't find the concrete method. However, all of
2460 * the type information is in the abstract method, so we're good.
2461 */
2462 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002463 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002464 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002465 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2466 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2467 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2468 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002469 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002470 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002471 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002472 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002474 } else {
2475 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002477 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002479 }
jeffhaobdb76512011-09-07 11:43:16 -07002480 case Instruction::NEG_INT:
2481 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 case Instruction::NEG_LONG:
2485 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002486 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002487 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002490 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002493 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002494 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002497 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002498 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002504 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002505 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002509 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002513 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002517 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002520 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002521 break;
2522 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002524 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002525 break;
2526 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002528 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002531 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002532 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002535 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002536 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002540 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002543 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
2545 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551
2552 case Instruction::ADD_INT:
2553 case Instruction::SUB_INT:
2554 case Instruction::MUL_INT:
2555 case Instruction::REM_INT:
2556 case Instruction::DIV_INT:
2557 case Instruction::SHL_INT:
2558 case Instruction::SHR_INT:
2559 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002560 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002561 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::AND_INT:
2564 case Instruction::OR_INT:
2565 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002567 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::ADD_LONG:
2570 case Instruction::SUB_LONG:
2571 case Instruction::MUL_LONG:
2572 case Instruction::DIV_LONG:
2573 case Instruction::REM_LONG:
2574 case Instruction::AND_LONG:
2575 case Instruction::OR_LONG:
2576 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002578 reg_types_.LongLo(), reg_types_.LongHi(),
2579 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002580 break;
2581 case Instruction::SHL_LONG:
2582 case Instruction::SHR_LONG:
2583 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002586 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::ADD_FLOAT:
2589 case Instruction::SUB_FLOAT:
2590 case Instruction::MUL_FLOAT:
2591 case Instruction::DIV_FLOAT:
2592 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2594 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596 case Instruction::ADD_DOUBLE:
2597 case Instruction::SUB_DOUBLE:
2598 case Instruction::MUL_DOUBLE:
2599 case Instruction::DIV_DOUBLE:
2600 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002601 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002602 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2603 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::ADD_INT_2ADDR:
2606 case Instruction::SUB_INT_2ADDR:
2607 case Instruction::MUL_INT_2ADDR:
2608 case Instruction::REM_INT_2ADDR:
2609 case Instruction::SHL_INT_2ADDR:
2610 case Instruction::SHR_INT_2ADDR:
2611 case Instruction::USHR_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::AND_INT_2ADDR:
2616 case Instruction::OR_INT_2ADDR:
2617 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002618 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2619 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002622 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2623 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::ADD_LONG_2ADDR:
2626 case Instruction::SUB_LONG_2ADDR:
2627 case Instruction::MUL_LONG_2ADDR:
2628 case Instruction::DIV_LONG_2ADDR:
2629 case Instruction::REM_LONG_2ADDR:
2630 case Instruction::AND_LONG_2ADDR:
2631 case Instruction::OR_LONG_2ADDR:
2632 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002633 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002634 reg_types_.LongLo(), reg_types_.LongHi(),
2635 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::SHL_LONG_2ADDR:
2638 case Instruction::SHR_LONG_2ADDR:
2639 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002640 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002641 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002642 break;
2643 case Instruction::ADD_FLOAT_2ADDR:
2644 case Instruction::SUB_FLOAT_2ADDR:
2645 case Instruction::MUL_FLOAT_2ADDR:
2646 case Instruction::DIV_FLOAT_2ADDR:
2647 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002648 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2649 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::ADD_DOUBLE_2ADDR:
2652 case Instruction::SUB_DOUBLE_2ADDR:
2653 case Instruction::MUL_DOUBLE_2ADDR:
2654 case Instruction::DIV_DOUBLE_2ADDR:
2655 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002656 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002657 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2658 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002661 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002662 case Instruction::MUL_INT_LIT16:
2663 case Instruction::DIV_INT_LIT16:
2664 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2666 true);
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::AND_INT_LIT16:
2669 case Instruction::OR_INT_LIT16:
2670 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002671 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2672 true);
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::ADD_INT_LIT8:
2675 case Instruction::RSUB_INT_LIT8:
2676 case Instruction::MUL_INT_LIT8:
2677 case Instruction::DIV_INT_LIT8:
2678 case Instruction::REM_INT_LIT8:
2679 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002680 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002681 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002682 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2683 false);
jeffhaobdb76512011-09-07 11:43:16 -07002684 break;
2685 case Instruction::AND_INT_LIT8:
2686 case Instruction::OR_INT_LIT8:
2687 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2689 false);
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002692 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002693 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002694 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2696 }
2697 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002698 // Note: the following instructions encode offsets derived from class linking.
2699 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2700 // meaning if the class linking and resolution were successful.
2701 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002702 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002703 break;
2704 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002705 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002706 break;
2707 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002708 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002709 break;
2710 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002711 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002712 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002713 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002714 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002715 break;
2716 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002717 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002718 break;
2719 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002720 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002721 break;
2722 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002723 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002724 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002725 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002726 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002727 break;
2728 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002729 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002730 break;
2731 case Instruction::INVOKE_VIRTUAL_QUICK:
2732 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2733 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002734 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002735 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002736 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002737 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002738 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002739 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002740 } else {
2741 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2742 }
2743 just_set_result = true;
2744 }
2745 break;
2746 }
2747
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002749 case Instruction::UNUSED_3E:
2750 case Instruction::UNUSED_3F:
2751 case Instruction::UNUSED_40:
2752 case Instruction::UNUSED_41:
2753 case Instruction::UNUSED_42:
2754 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002755 case Instruction::UNUSED_79:
2756 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002757 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002758 case Instruction::UNUSED_F0:
2759 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002760 case Instruction::UNUSED_F2:
2761 case Instruction::UNUSED_F3:
2762 case Instruction::UNUSED_F4:
2763 case Instruction::UNUSED_F5:
2764 case Instruction::UNUSED_F6:
2765 case Instruction::UNUSED_F7:
2766 case Instruction::UNUSED_F8:
2767 case Instruction::UNUSED_F9:
2768 case Instruction::UNUSED_FA:
2769 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002770 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002771 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002772 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002773 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776
2777 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002778 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002779 * complain if an instruction is missing (which is desirable).
2780 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002781 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002782
Ian Rogersad0b3a32012-04-16 14:50:24 -07002783 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002784 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002785 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002786 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002787 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002788 /* immediate failure, reject class */
2789 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2790 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002791 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002792 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002793 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002794 }
jeffhaobdb76512011-09-07 11:43:16 -07002795 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 * If we didn't just set the result register, clear it out. This ensures that you can only use
2797 * "move-result" immediately after the result is set. (We could check this statically, but it's
2798 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002799 */
2800 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002801 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002802 }
2803
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002804
jeffhaobdb76512011-09-07 11:43:16 -07002805
2806 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002807 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002808 *
2809 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002810 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002811 * somebody could get a reference field, check it for zero, and if the
2812 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002813 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002814 * that, and will reject the code.
2815 *
2816 * TODO: avoid re-fetching the branch target
2817 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002818 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002819 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002821 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002823 return false;
2824 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002825 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002826 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002827 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 }
jeffhaobdb76512011-09-07 11:43:16 -07002829 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002830 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002831 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002832 return false;
2833 }
2834 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002835 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002836 return false;
2837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 }
jeffhaobdb76512011-09-07 11:43:16 -07002839 }
2840
2841 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002842 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002843 *
2844 * We've already verified that the table is structurally sound, so we
2845 * just need to walk through and tag the targets.
2846 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002847 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002848 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2849 const uint16_t* switch_insns = insns + offset_to_switch;
2850 int switch_count = switch_insns[1];
2851 int offset_to_targets, targ;
2852
2853 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2854 /* 0 = sig, 1 = count, 2/3 = first key */
2855 offset_to_targets = 4;
2856 } else {
2857 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002858 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002859 offset_to_targets = 2 + 2 * switch_count;
2860 }
2861
2862 /* verify each switch target */
2863 for (targ = 0; targ < switch_count; targ++) {
2864 int offset;
2865 uint32_t abs_offset;
2866
2867 /* offsets are 32-bit, and only partly endian-swapped */
2868 offset = switch_insns[offset_to_targets + targ * 2] |
2869 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 abs_offset = work_insn_idx_ + offset;
2871 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002872 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002873 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002875 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002876 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002877 }
jeffhaobdb76512011-09-07 11:43:16 -07002878 }
2879 }
2880
2881 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2883 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002884 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002885 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002886 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002887 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002888
Andreas Gampef91baf12014-07-18 15:41:00 -07002889 // Need the linker to try and resolve the handled class to check if it's Throwable.
2890 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2891
Ian Rogers0571d352011-11-03 19:51:38 -07002892 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002893 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2894 if (handler_type_idx == DexFile::kDexNoIndex16) {
2895 has_catch_all_handler = true;
2896 } else {
2897 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002898 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2899 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002900 if (klass != nullptr) {
2901 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2902 has_catch_all_handler = true;
2903 }
2904 } else {
2905 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 DCHECK(self_->IsExceptionPending());
2907 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002908 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 }
jeffhaobdb76512011-09-07 11:43:16 -07002910 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2912 * "work_regs", because at runtime the exception will be thrown before the instruction
2913 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002914 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002915 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002916 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 }
jeffhaobdb76512011-09-07 11:43:16 -07002918 }
2919
2920 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2922 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002923 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002924 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002925 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 * The state in work_line reflects the post-execution state. If the current instruction is a
2927 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002928 * it will do so before grabbing the lock).
2929 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002930 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002931 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002933 return false;
2934 }
2935 }
2936 }
2937
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002938 /* Handle "continue". Tag the next consecutive instruction.
2939 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2940 * because it changes work_line_ when performing peephole optimization
2941 * and this change should not be used in those cases.
2942 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002943 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002944 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2945 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002946 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2948 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002949 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002950 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2951 // next instruction isn't one.
2952 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2953 return false;
2954 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002955 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002956 // Make workline consistent with fallthrough computed from peephole optimization.
2957 work_line_->CopyFromLine(fallthrough_line.get());
2958 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002959 if (insn_flags_[next_insn_idx].IsReturn()) {
2960 // For returns we only care about the operand to the return, all other registers are dead.
2961 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2962 Instruction::Code opcode = ret_inst->Opcode();
2963 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002965 } else {
2966 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002967 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002968 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002969 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002970 }
2971 }
2972 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002973 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002974 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002975 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2976 // needed. If the merge changes the state of the registers then the work line will be
2977 // updated.
2978 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002979 return false;
2980 }
2981 } else {
2982 /*
2983 * We're not recording register data for the next instruction, so we don't know what the
2984 * prior state was. We have to assume that something has changed and re-evaluate it.
2985 */
2986 insn_flags_[next_insn_idx].SetChanged();
2987 }
2988 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002989
jeffhaod1f0fde2011-09-08 17:25:33 -07002990 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002991 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 return false;
2994 }
jeffhaobdb76512011-09-07 11:43:16 -07002995 }
2996
2997 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002998 * Update start_guess. Advance to the next instruction of that's
2999 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003000 * neither of those exists we're in a return or throw; leave start_guess
3001 * alone and let the caller sort it out.
3002 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003003 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003004 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3005 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003006 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003007 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003009 }
3010
Ian Rogersd81871c2011-10-03 13:57:23 -07003011 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3012 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003013
3014 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003015} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003016
Ian Rogersd8f69b02014-09-10 21:43:52 +00003017const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003018 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003019 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003020 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003021 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003022 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3023 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 if (result.IsConflict()) {
3025 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3026 << "' in " << referrer;
3027 return result;
3028 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003030 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003031 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003032 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003033 // check at runtime if access is allowed and so pass here. If result is
3034 // primitive, skip the access check.
3035 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3036 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003037 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003039 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003040 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003041}
3042
Ian Rogersd8f69b02014-09-10 21:43:52 +00003043const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003045 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003046 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3048 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003049 CatchHandlerIterator iterator(handlers_ptr);
3050 for (; iterator.HasNext(); iterator.Next()) {
3051 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3052 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003053 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003055 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003056 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003057 if (exception.IsUnresolvedTypes()) {
3058 // We don't know enough about the type. Fail here and let runtime handle it.
3059 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3060 return exception;
3061 } else {
3062 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3063 return reg_types_.Conflict();
3064 }
Jeff Haob878f212014-04-24 16:25:36 -07003065 } else if (common_super == nullptr) {
3066 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003067 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003068 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003070 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003071 if (FailOrAbort(this,
3072 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3073 "java.lang.Throwable is not assignable-from common_super at ",
3074 work_insn_idx_)) {
3075 break;
3076 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 }
3078 }
3079 }
3080 }
Ian Rogers0571d352011-11-03 19:51:38 -07003081 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 }
3083 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003084 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003086 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003087 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003088 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003089 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003090}
3091
Brian Carlstromea46f952013-07-30 01:26:50 -07003092mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003093 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003094 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003095 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003096 if (klass_type.IsConflict()) {
3097 std::string append(" in attempt to access method ");
3098 append += dex_file_->GetMethodName(method_id);
3099 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003101 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003102 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003103 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003104 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003105 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003106 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003107 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003108 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003109 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003110 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003111
3112 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003114 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 res_method = klass->FindInterfaceMethod(name, signature);
3116 } else {
3117 res_method = klass->FindVirtualMethod(name, signature);
3118 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003119 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003120 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003121 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003122 // If a virtual or interface method wasn't found with the expected type, look in
3123 // the direct methods. This can happen when the wrong invoke type is used or when
3124 // a class has changed, and will be flagged as an error in later checks.
3125 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3126 res_method = klass->FindDirectMethod(name, signature);
3127 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003128 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003129 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3130 << PrettyDescriptor(klass) << "." << name
3131 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003132 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003133 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 }
3135 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003136 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3137 // enforce them here.
3138 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3140 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003141 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003143 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003144 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3146 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003148 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003149 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003150 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003151 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003152 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003153 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003154 }
jeffhaode0d9c92012-02-27 13:58:13 -08003155 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3156 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3158 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003159 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003160 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003161 // Check that interface methods match interface classes.
3162 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3163 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3164 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003166 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3167 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3168 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003169 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003170 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 // See if the method type implied by the invoke instruction matches the access flags for the
3172 // target method.
3173 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3174 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3175 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3176 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003177 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3178 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003179 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003181 return res_method;
3182}
3183
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003184template <class T>
3185mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3186 MethodType method_type,
3187 bool is_range,
3188 mirror::ArtMethod* res_method) {
3189 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3190 // match the call to the signature. Also, we might be calling through an abstract method
3191 // definition (which doesn't have register count values).
3192 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3193 /* caught by static verifier */
3194 DCHECK(is_range || expected_args <= 5);
3195 if (expected_args > code_item_->outs_size_) {
3196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3197 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3198 return nullptr;
3199 }
3200
3201 uint32_t arg[5];
3202 if (!is_range) {
3203 inst->GetVarArgs(arg);
3204 }
3205 uint32_t sig_registers = 0;
3206
3207 /*
3208 * Check the "this" argument, which must be an instance of the class that declared the method.
3209 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3210 * rigorous check here (which is okay since we have to do it at runtime).
3211 */
3212 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003214 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3215 CHECK(have_pending_hard_failure_);
3216 return nullptr;
3217 }
3218 if (actual_arg_type.IsUninitializedReference()) {
3219 if (res_method) {
3220 if (!res_method->IsConstructor()) {
3221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3222 return nullptr;
3223 }
3224 } else {
3225 // Check whether the name of the called method is "<init>"
3226 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003227 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3229 return nullptr;
3230 }
3231 }
3232 }
3233 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003234 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003235 if (res_method != nullptr) {
3236 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003237 std::string temp;
3238 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003239 klass->CannotBeAssignedFromOtherTypes());
3240 } else {
3241 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3242 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003243 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003244 dex_file_->StringByTypeIdx(class_idx),
3245 false);
3246 }
3247 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3248 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3249 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3250 << "' not instance of '" << *res_method_class << "'";
3251 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3252 // the compiler.
3253 if (have_pending_hard_failure_) {
3254 return nullptr;
3255 }
3256 }
3257 }
3258 sig_registers = 1;
3259 }
3260
3261 for ( ; it->HasNext(); it->Next()) {
3262 if (sig_registers >= expected_args) {
3263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3264 " arguments, found " << sig_registers << " or more.";
3265 return nullptr;
3266 }
3267
3268 const char* param_descriptor = it->GetDescriptor();
3269
3270 if (param_descriptor == nullptr) {
3271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3272 "component";
3273 return nullptr;
3274 }
3275
Ian Rogersd8f69b02014-09-10 21:43:52 +00003276 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003277 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3278 arg[sig_registers];
3279 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003280 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003281 if (!src_type.IsIntegralTypes()) {
3282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3283 << " but expected " << reg_type;
3284 return res_method;
3285 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003286 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003287 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3288 // compiler.
3289 if (have_pending_hard_failure_) {
3290 return res_method;
3291 }
3292 }
3293 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3294 }
3295 if (expected_args != sig_registers) {
3296 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3297 " arguments, found " << sig_registers;
3298 return nullptr;
3299 }
3300 return res_method;
3301}
3302
3303void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3304 MethodType method_type,
3305 bool is_range) {
3306 // As the method may not have been resolved, make this static check against what we expect.
3307 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3308 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3309 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3310 DexFileParameterIterator it(*dex_file_,
3311 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3312 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3313 nullptr);
3314}
3315
3316class MethodParamListDescriptorIterator {
3317 public:
3318 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3319 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3320 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3321 }
3322
3323 bool HasNext() {
3324 return pos_ < params_size_;
3325 }
3326
3327 void Next() {
3328 ++pos_;
3329 }
3330
3331 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3332 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3333 }
3334
3335 private:
3336 mirror::ArtMethod* res_method_;
3337 size_t pos_;
3338 const DexFile::TypeList* params_;
3339 const size_t params_size_;
3340};
3341
Brian Carlstromea46f952013-07-30 01:26:50 -07003342mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003343 MethodType method_type,
3344 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003345 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003346 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3347 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003348 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003349
Brian Carlstromea46f952013-07-30 01:26:50 -07003350 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003351 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003352 // Check what we can statically.
3353 if (!have_pending_hard_failure_) {
3354 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3355 }
3356 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003357 }
3358
Ian Rogersd81871c2011-10-03 13:57:23 -07003359 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3360 // has a vtable entry for the target method.
3361 if (is_super) {
3362 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003363 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003364 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003365 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003366 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003367 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003368 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003369 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003370 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003371 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003372 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003373 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003374 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003375 << "." << res_method->GetName()
3376 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003377 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 }
3379 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003380
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003381 // Process the target method's signature. This signature may or may not
3382 MethodParamListDescriptorIterator it(res_method);
3383 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3384 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003385}
3386
Brian Carlstromea46f952013-07-30 01:26:50 -07003387mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003388 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003389 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3390 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003391 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003392 if (!actual_arg_type.HasClass()) {
3393 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003394 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003395 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003396 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003397 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003398 if (klass->IsInterface()) {
3399 // Derive Object.class from Class.class.getSuperclass().
3400 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003401 if (FailOrAbort(this, object_klass->IsObjectClass(),
3402 "Failed to find Object class in quickened invoke receiver",
3403 work_insn_idx_)) {
3404 return nullptr;
3405 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003406 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003407 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003408 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003409 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003410 if (FailOrAbort(this, dispatch_class->HasVTable(),
3411 "Receiver class has no vtable for quickened invoke at ",
3412 work_insn_idx_)) {
3413 return nullptr;
3414 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003415 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe7c038102014-10-27 20:08:46 -07003416 if (FailOrAbort(this, static_cast<int32_t>(vtable_index) < dispatch_class->GetVTableLength(),
3417 "Receiver class has not enough vtable slots for quickened invoke at ",
3418 work_insn_idx_)) {
3419 return nullptr;
3420 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003421 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Andreas Gampe7c038102014-10-27 20:08:46 -07003422 if (FailOrAbort(this, !Thread::Current()->IsExceptionPending(),
3423 "Unexpected exception pending for quickened invoke at ",
3424 work_insn_idx_)) {
3425 return nullptr;
3426 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003427 return res_method;
3428}
3429
Brian Carlstromea46f952013-07-30 01:26:50 -07003430mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003431 bool is_range) {
3432 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003433 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003434 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003435 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003437 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003438 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003439 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3440 work_insn_idx_)) {
3441 return nullptr;
3442 }
3443 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3444 work_insn_idx_)) {
3445 return nullptr;
3446 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003447
3448 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3449 // match the call to the signature. Also, we might be calling through an abstract method
3450 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003451 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003452 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003453 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003454 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003455 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3456 /* caught by static verifier */
3457 DCHECK(is_range || expected_args <= 5);
3458 if (expected_args > code_item_->outs_size_) {
3459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3460 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003461 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003462 }
3463
3464 /*
3465 * Check the "this" argument, which must be an instance of the class that declared the method.
3466 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3467 * rigorous check here (which is okay since we have to do it at runtime).
3468 */
3469 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003471 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003472 }
3473 if (!actual_arg_type.IsZero()) {
3474 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003475 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003476 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003477 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003478 klass->CannotBeAssignedFromOtherTypes());
3479 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003480 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3481 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003482 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003483 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003484 }
3485 }
3486 /*
3487 * Process the target method's signature. This signature may or may not
3488 * have been verified, so we can't assume it's properly formed.
3489 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003490 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003491 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003492 uint32_t arg[5];
3493 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003494 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003495 }
3496 size_t actual_args = 1;
3497 for (size_t param_index = 0; param_index < params_size; param_index++) {
3498 if (actual_args >= expected_args) {
3499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003500 << "'. Expected " << expected_args
3501 << " arguments, processing argument " << actual_args
3502 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003503 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003504 }
3505 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003506 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003507 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003509 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003510 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003511 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003512 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003513 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003514 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003515 return res_method;
3516 }
3517 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3518 }
3519 if (actual_args != expected_args) {
3520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3521 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003522 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003523 } else {
3524 return res_method;
3525 }
3526}
3527
Ian Rogers62342ec2013-06-11 10:26:37 -07003528void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003529 uint32_t type_idx;
3530 if (!is_filled) {
3531 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3532 type_idx = inst->VRegC_22c();
3533 } else if (!is_range) {
3534 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3535 type_idx = inst->VRegB_35c();
3536 } else {
3537 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3538 type_idx = inst->VRegB_3rc();
3539 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003540 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003541 if (res_type.IsConflict()) { // bad class
3542 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003543 } else {
3544 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3545 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003547 } else if (!is_filled) {
3548 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003549 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003550 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003551 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003552 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003553 } else {
3554 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3555 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003556 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003557 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3558 uint32_t arg[5];
3559 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003560 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003561 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003562 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003563 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003564 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3565 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003566 return;
3567 }
3568 }
3569 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003570 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003571 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003572 }
3573 }
3574}
3575
Sebastien Hertz5243e912013-05-21 10:55:07 +02003576void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003577 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003578 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003579 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003582 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003583 if (array_type.IsZero()) {
3584 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3585 // instruction type. TODO: have a proper notion of bottom here.
3586 if (!is_primitive || insn_type.IsCategory1Types()) {
3587 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003588 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003589 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003590 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003591 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3592 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003593 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003594 }
jeffhaofc3144e2012-02-01 17:21:15 -08003595 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003596 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003597 } else {
3598 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003599 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003600 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003602 << " source for aget-object";
3603 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003605 << " source for category 1 aget";
3606 } else if (is_primitive && !insn_type.Equals(component_type) &&
3607 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003608 (insn_type.IsLong() && component_type.IsDouble()))) {
3609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3610 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003611 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003612 // Use knowledge of the field type which is stronger than the type inferred from the
3613 // instruction, which can't differentiate object types and ints from floats, longs from
3614 // doubles.
3615 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003616 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003617 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003618 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003619 component_type.HighHalf(&reg_types_));
3620 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 }
3622 }
3623 }
3624}
3625
Ian Rogersd8f69b02014-09-10 21:43:52 +00003626void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003627 const uint32_t vregA) {
3628 // Primitive assignability rules are weaker than regular assignability rules.
3629 bool instruction_compatible;
3630 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003631 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003632 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003633 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003634 value_compatible = value_type.IsIntegralTypes();
3635 } else if (target_type.IsFloat()) {
3636 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3637 value_compatible = value_type.IsFloatTypes();
3638 } else if (target_type.IsLong()) {
3639 instruction_compatible = insn_type.IsLong();
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.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3645 } else {
3646 value_compatible = false;
3647 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003648 } else if (target_type.IsDouble()) {
3649 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003650 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3651 // as target_type depends on the resolved type of the field.
3652 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003654 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3655 } else {
3656 value_compatible = false;
3657 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003658 } else {
3659 instruction_compatible = false; // reference with primitive store
3660 value_compatible = false; // unused
3661 }
3662 if (!instruction_compatible) {
3663 // This is a global failure rather than a class change failure as the instructions and
3664 // the descriptors for the type should have been consistent within the same file at
3665 // compile time.
3666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3667 << "' but expected type '" << target_type << "'";
3668 return;
3669 }
3670 if (!value_compatible) {
3671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3672 << " of type " << value_type << " but expected " << target_type << " for put";
3673 return;
3674 }
3675}
3676
Sebastien Hertz5243e912013-05-21 10:55:07 +02003677void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003678 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003679 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003680 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003683 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003684 if (array_type.IsZero()) {
3685 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3686 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003687 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003689 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003690 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003691 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003692 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003693 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003694 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003695 if (!component_type.IsReferenceTypes()) {
3696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3697 << " source for aput-object";
3698 } else {
3699 // The instruction agrees with the type of array, confirm the value to be stored does too
3700 // Note: we use the instruction type (rather than the component type) for aput-object as
3701 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003702 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003704 }
3705 }
3706 }
3707}
3708
Brian Carlstromea46f952013-07-30 01:26:50 -07003709mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003710 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3711 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003712 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003713 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003714 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3715 field_idx, dex_file_->GetFieldName(field_id),
3716 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003717 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003718 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003719 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003720 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003721 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003722 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003723 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3724 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003726 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003727 << dex_file_->GetFieldName(field_id) << ") in "
3728 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003729 DCHECK(self_->IsExceptionPending());
3730 self_->ClearException();
3731 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003732 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3733 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003734 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003735 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003736 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 } else if (!field->IsStatic()) {
3738 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003739 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003741 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003742}
3743
Ian Rogersd8f69b02014-09-10 21:43:52 +00003744mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003745 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3746 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003747 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003748 if (klass_type.IsConflict()) {
3749 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3750 field_idx, dex_file_->GetFieldName(field_id),
3751 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003752 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003753 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003754 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003755 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003756 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003757 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003758 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3759 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003760 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003761 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003762 << dex_file_->GetFieldName(field_id) << ") in "
3763 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003764 DCHECK(self_->IsExceptionPending());
3765 self_->ClearException();
3766 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003767 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3768 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003770 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 } else if (field->IsStatic()) {
3773 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3774 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 } else if (obj_type.IsZero()) {
3777 // Cannot infer and check type, however, access will cause null pointer exception
3778 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003779 } else if (!obj_type.IsReferenceTypes()) {
3780 // Trying to read a field from something that isn't a reference
3781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3782 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003784 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003785 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003786 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003787 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003788 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003789 if (obj_type.IsUninitializedTypes() &&
3790 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3791 !field_klass.Equals(GetDeclaringClass()))) {
3792 // Field accesses through uninitialized references are only allowable for constructors where
3793 // the field is declared in this class
3794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003795 << " of a not fully initialized object within the context"
3796 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003797 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003798 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3799 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3800 // of C1. For resolution to occur the declared class of the field must be compatible with
3801 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3802 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3803 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003804 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003805 } else {
3806 return field;
3807 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 }
3809}
3810
Andreas Gampe896df402014-10-20 22:25:29 -07003811template <MethodVerifier::FieldAccessType kAccType>
3812void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3813 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003814 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003815 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003816 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003817 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003818 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003819 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003820 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003821 if (UNLIKELY(have_pending_hard_failure_)) {
3822 return;
3823 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003824 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003825 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003827 if (kAccType == FieldAccessType::kAccPut) {
3828 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3829 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3830 << " from other class " << GetDeclaringClass();
3831 return;
3832 }
3833 }
3834
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003835 mirror::Class* field_type_class;
3836 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003837 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003838 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3839 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3840 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003841 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003842 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003843 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003844 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003845 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3846 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003847 }
Ian Rogers0d604842012-04-16 14:50:24 -07003848 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003849 if (field_type == nullptr) {
3850 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3851 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003852 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003853 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003854 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003855 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003856 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3857 "Unexpected third access type");
3858 if (kAccType == FieldAccessType::kAccPut) {
3859 // sput or iput.
3860 if (is_primitive) {
3861 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003862 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003863 if (!insn_type.IsAssignableFrom(*field_type)) {
3864 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3865 << " to be compatible with type '" << insn_type
3866 << "' but found type '" << *field_type
3867 << "' in put-object";
3868 return;
3869 }
3870 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003871 }
Andreas Gampe896df402014-10-20 22:25:29 -07003872 } else if (kAccType == FieldAccessType::kAccGet) {
3873 // sget or iget.
3874 if (is_primitive) {
3875 if (field_type->Equals(insn_type) ||
3876 (field_type->IsFloat() && insn_type.IsInteger()) ||
3877 (field_type->IsDouble() && insn_type.IsLong())) {
3878 // expected that read is of the correct primitive type or that int reads are reading
3879 // floats or long reads are reading doubles
3880 } else {
3881 // This is a global failure rather than a class change failure as the instructions and
3882 // the descriptors for the type should have been consistent within the same file at
3883 // compile time
3884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3885 << " to be of type '" << insn_type
3886 << "' but found type '" << *field_type << "' in get";
3887 return;
3888 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003889 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003890 if (!insn_type.IsAssignableFrom(*field_type)) {
3891 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3892 << " to be compatible with type '" << insn_type
3893 << "' but found type '" << *field_type
3894 << "' in get-object";
3895 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3896 return;
3897 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003898 }
Andreas Gampe896df402014-10-20 22:25:29 -07003899 if (!field_type->IsLowHalf()) {
3900 work_line_->SetRegisterType(this, vregA, *field_type);
3901 } else {
3902 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3903 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003904 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003905 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003906 }
3907}
3908
Brian Carlstromea46f952013-07-30 01:26:50 -07003909mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003910 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003911 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3912 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3913 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3914 inst->Opcode() == Instruction::IPUT_QUICK ||
3915 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
Hiroshi Yamauchi5f09be92014-09-26 10:43:59 -07003916 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3917 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3918 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3919 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3920 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003921 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003922 if (!object_type.HasClass()) {
3923 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3924 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003925 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003926 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003927 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3928 field_offset);
3929 if (f == nullptr) {
3930 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3931 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3932 }
3933 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003934}
3935
Andreas Gampe896df402014-10-20 22:25:29 -07003936template <MethodVerifier::FieldAccessType kAccType>
3937void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
3938 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003939 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003940
Brian Carlstromea46f952013-07-30 01:26:50 -07003941 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003942 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3944 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003945 }
Andreas Gampe896df402014-10-20 22:25:29 -07003946
3947 // For an IPUT_QUICK, we now test for final flag of the field.
3948 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003949 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3950 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003951 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003952 return;
3953 }
3954 }
Andreas Gampe896df402014-10-20 22:25:29 -07003955
3956 // Get the field type.
3957 const RegType* field_type;
3958 {
3959 mirror::Class* field_type_class;
3960 {
3961 StackHandleScope<1> hs(Thread::Current());
3962 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3963 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3964 }
3965
3966 if (field_type_class != nullptr) {
3967 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
3968 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003969 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003970 Thread* self = Thread::Current();
3971 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3972 self->ClearException();
3973 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3974 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003975 }
Andreas Gampe896df402014-10-20 22:25:29 -07003976 if (field_type == nullptr) {
3977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003978 return;
3979 }
Andreas Gampe896df402014-10-20 22:25:29 -07003980 }
3981
3982 const uint32_t vregA = inst->VRegA_22c();
3983 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3984 "Unexpected third access type");
3985 if (kAccType == FieldAccessType::kAccPut) {
3986 if (is_primitive) {
3987 // Primitive field assignability rules are weaker than regular assignability rules
3988 bool instruction_compatible;
3989 bool value_compatible;
3990 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
3991 if (field_type->IsIntegralTypes()) {
3992 instruction_compatible = insn_type.IsIntegralTypes();
3993 value_compatible = value_type.IsIntegralTypes();
3994 } else if (field_type->IsFloat()) {
3995 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3996 value_compatible = value_type.IsFloatTypes();
3997 } else if (field_type->IsLong()) {
3998 instruction_compatible = insn_type.IsLong();
3999 value_compatible = value_type.IsLongTypes();
4000 } else if (field_type->IsDouble()) {
4001 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4002 value_compatible = value_type.IsDoubleTypes();
4003 } else {
4004 instruction_compatible = false; // reference field with primitive store
4005 value_compatible = false; // unused
4006 }
4007 if (!instruction_compatible) {
4008 // This is a global failure rather than a class change failure as the instructions and
4009 // the descriptors for the type should have been consistent within the same file at
4010 // compile time
4011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4012 << " to be of type '" << insn_type
4013 << "' but found type '" << *field_type
4014 << "' in put";
4015 return;
4016 }
4017 if (!value_compatible) {
4018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4019 << " of type " << value_type
4020 << " but expected " << *field_type
4021 << " for store to " << PrettyField(field) << " in put";
4022 return;
4023 }
4024 } else {
4025 if (!insn_type.IsAssignableFrom(*field_type)) {
4026 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4027 << " to be compatible with type '" << insn_type
4028 << "' but found type '" << *field_type
4029 << "' in put-object";
4030 return;
4031 }
4032 work_line_->VerifyRegisterType(this, vregA, *field_type);
4033 }
4034 } else if (kAccType == FieldAccessType::kAccGet) {
4035 if (is_primitive) {
4036 if (field_type->Equals(insn_type) ||
4037 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4038 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4039 // expected that read is of the correct primitive type or that int reads are reading
4040 // floats or long reads are reading doubles
4041 } else {
4042 // This is a global failure rather than a class change failure as the instructions and
4043 // the descriptors for the type should have been consistent within the same file at
4044 // compile time
4045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4046 << " to be of type '" << insn_type
4047 << "' but found type '" << *field_type << "' in Get";
4048 return;
4049 }
4050 } else {
4051 if (!insn_type.IsAssignableFrom(*field_type)) {
4052 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4053 << " to be compatible with type '" << insn_type
4054 << "' but found type '" << *field_type
4055 << "' in get-object";
4056 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4057 return;
4058 }
4059 }
4060 if (!field_type->IsLowHalf()) {
4061 work_line_->SetRegisterType(this, vregA, *field_type);
4062 } else {
4063 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004064 }
4065 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004066 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004067 }
4068}
4069
Ian Rogers776ac1f2012-04-13 23:36:36 -07004070bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004071 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004073 return false;
4074 }
4075 return true;
4076}
4077
Stephen Kyle9bc61992014-09-22 13:53:15 +01004078bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4079 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4080 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4082 return false;
4083 }
4084 return true;
4085}
4086
4087bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4088 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4089}
4090
Ian Rogersebbdd872014-07-07 23:53:08 -07004091bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4092 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004093 bool changed = true;
4094 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4095 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004096 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004097 * We haven't processed this instruction before, and we haven't touched the registers here, so
4098 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4099 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004100 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004101 if (!insn_flags_[next_insn].IsReturn()) {
4102 target_line->CopyFromLine(merge_line);
4103 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004104 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004105 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004106 return false;
4107 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004108 // For returns we only care about the operand to the return, all other registers are dead.
4109 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4110 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4111 Instruction::Code opcode = ret_inst->Opcode();
4112 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004113 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004114 } else {
4115 target_line->CopyFromLine(merge_line);
4116 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004117 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004118 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004119 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004120 }
4121 }
4122 }
jeffhaobdb76512011-09-07 11:43:16 -07004123 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004124 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004125 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004126 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004127 if (gDebugVerify) {
4128 copy->CopyFromLine(target_line);
4129 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004130 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004131 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004132 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004133 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004134 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004135 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004136 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004137 << copy->Dump(this) << " MERGE\n"
4138 << merge_line->Dump(this) << " ==\n"
4139 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004140 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004141 if (update_merge_line && changed) {
4142 merge_line->CopyFromLine(target_line);
4143 }
jeffhaobdb76512011-09-07 11:43:16 -07004144 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004145 if (changed) {
4146 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004147 }
4148 return true;
4149}
4150
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004151InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004152 return &insn_flags_[work_insn_idx_];
4153}
4154
Ian Rogersd8f69b02014-09-10 21:43:52 +00004155const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004156 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004157 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004158 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004159 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004160 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4161 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004162 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004163 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004164 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4165 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004166 }
4167 }
4168 if (return_type_ == nullptr) {
4169 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4170 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4171 uint16_t return_type_idx = proto_id.return_type_idx_;
4172 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004173 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004174 }
4175 }
4176 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004177}
4178
Ian Rogersd8f69b02014-09-10 21:43:52 +00004179const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004180 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004181 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004182 const char* descriptor
4183 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004184 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004185 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004186 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4187 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004188 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004189 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004190 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004191 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004192 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004193}
4194
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004195std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4196 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004197 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004198 std::vector<int32_t> result;
4199 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004200 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004201 if (type.IsConstant()) {
4202 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004203 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4204 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004205 } else if (type.IsConstantLo()) {
4206 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004207 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4208 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004209 } else if (type.IsConstantHi()) {
4210 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004211 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4212 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004213 } else if (type.IsIntegralTypes()) {
4214 result.push_back(kIntVReg);
4215 result.push_back(0);
4216 } else if (type.IsFloat()) {
4217 result.push_back(kFloatVReg);
4218 result.push_back(0);
4219 } else if (type.IsLong()) {
4220 result.push_back(kLongLoVReg);
4221 result.push_back(0);
4222 result.push_back(kLongHiVReg);
4223 result.push_back(0);
4224 ++i;
4225 } else if (type.IsDouble()) {
4226 result.push_back(kDoubleLoVReg);
4227 result.push_back(0);
4228 result.push_back(kDoubleHiVReg);
4229 result.push_back(0);
4230 ++i;
4231 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4232 result.push_back(kUndefined);
4233 result.push_back(0);
4234 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004235 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004236 result.push_back(kReferenceVReg);
4237 result.push_back(0);
4238 }
4239 }
4240 return result;
4241}
4242
Ian Rogersd8f69b02014-09-10 21:43:52 +00004243const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004244 if (precise) {
4245 // Precise constant type.
4246 return reg_types_.FromCat1Const(value, true);
4247 } else {
4248 // Imprecise constant type.
4249 if (value < -32768) {
4250 return reg_types_.IntConstant();
4251 } else if (value < -128) {
4252 return reg_types_.ShortConstant();
4253 } else if (value < 0) {
4254 return reg_types_.ByteConstant();
4255 } else if (value == 0) {
4256 return reg_types_.Zero();
4257 } else if (value == 1) {
4258 return reg_types_.One();
4259 } else if (value < 128) {
4260 return reg_types_.PosByteConstant();
4261 } else if (value < 32768) {
4262 return reg_types_.PosShortConstant();
4263 } else if (value < 65536) {
4264 return reg_types_.CharConstant();
4265 } else {
4266 return reg_types_.IntConstant();
4267 }
4268 }
4269}
4270
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004271void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004272 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004273}
4274
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004275void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004276 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004277}
jeffhaod1224c72012-02-29 13:43:08 -08004278
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004279void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4280 RegTypeCache::VisitStaticRoots(callback, arg);
4281}
4282
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004283void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4284 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004285}
4286
Ian Rogersd81871c2011-10-03 13:57:23 -07004287} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004288} // namespace art