blob: 7380a50a5a2fa3d2ed1e489ffa91b6eb15bdcec1 [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.
Ian Rogersb8c78592013-07-25 23:52:52 +0000730 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700731 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000732 } else if (inst->IsReturn()) {
733 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 }
735 dex_pc += inst->SizeInCodeUnits();
736 inst = inst->Next();
737 }
738 return true;
739}
740
Ian Rogers776ac1f2012-04-13 23:36:36 -0700741bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 bool result = true;
743 switch (inst->GetVerifyTypeArgumentA()) {
744 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700745 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 break;
747 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700748 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 break;
750 }
751 switch (inst->GetVerifyTypeArgumentB()) {
752 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700753 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 break;
755 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700756 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 break;
758 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700759 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 break;
761 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700762 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 break;
764 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700765 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 break;
767 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700768 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 break;
770 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700771 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 break;
773 }
774 switch (inst->GetVerifyTypeArgumentC()) {
775 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700776 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 break;
778 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700779 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 break;
781 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700782 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 break;
784 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700785 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 break;
787 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700788 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 break;
790 }
791 switch (inst->GetVerifyExtraFlags()) {
792 case Instruction::kVerifyArrayData:
793 result = result && CheckArrayData(code_offset);
794 break;
795 case Instruction::kVerifyBranchTarget:
796 result = result && CheckBranchTarget(code_offset);
797 break;
798 case Instruction::kVerifySwitchTargets:
799 result = result && CheckSwitchTargets(code_offset);
800 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700801 case Instruction::kVerifyVarArgNonZero:
802 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700803 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700804 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
806 "non-range invoke";
807 return false;
808 }
Ian Rogers29a26482014-05-02 15:27:29 -0700809 uint32_t args[Instruction::kMaxVarArgRegs];
810 inst->GetVarArgs(args);
811 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700813 }
Andreas Gampec3314312014-06-19 18:13:29 -0700814 case Instruction::kVerifyVarArgRangeNonZero:
815 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700817 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
818 inst->VRegA() <= 0) {
819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
820 "range invoke";
821 return false;
822 }
Ian Rogers29a26482014-05-02 15:27:29 -0700823 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 break;
825 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 result = false;
828 break;
829 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700830 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
832 result = false;
833 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 return result;
835}
836
Ian Rogers7b078e82014-09-10 14:44:24 -0700837inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700838 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
840 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 return false;
842 }
843 return true;
844}
845
Ian Rogers7b078e82014-09-10 14:44:24 -0700846inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
849 << "+1 >= " << 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::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
858 << dex_file_->GetHeader().field_ids_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::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
867 << dex_file_->GetHeader().method_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::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
876 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return false;
878 }
879 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700880 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 return false;
884 }
885 return true;
886}
887
Ian Rogers7b078e82014-09-10 14:44:24 -0700888inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
891 << dex_file_->GetHeader().string_ids_size_ << ")";
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::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
900 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 return false;
902 }
903 return true;
904}
905
Ian Rogers776ac1f2012-04-13 23:36:36 -0700906bool MethodVerifier::CheckNewArray(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 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700913 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 const char* cp = descriptor;
915 while (*cp++ == '[') {
916 bracket_count++;
917 }
918 if (bracket_count == 0) {
919 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700920 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
921 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 return false;
923 } else if (bracket_count > 255) {
924 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700925 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
926 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 return false;
928 }
929 return true;
930}
931
Ian Rogers776ac1f2012-04-13 23:36:36 -0700932bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
934 const uint16_t* insns = code_item_->insns_ + cur_offset;
935 const uint16_t* array_data;
936 int32_t array_data_offset;
937
938 DCHECK_LT(cur_offset, insn_count);
939 /* make sure the start of the array data table is in range */
940 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
941 if ((int32_t) cur_offset + array_data_offset < 0 ||
942 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700944 << ", data offset " << array_data_offset
945 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 return false;
947 }
948 /* offset to array data table is a relative branch-style offset */
949 array_data = insns + array_data_offset;
950 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800951 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
953 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 return false;
955 }
956 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700957 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
959 /* make sure the end of the switch is in range */
960 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
962 << ", data offset " << array_data_offset << ", end "
963 << cur_offset + array_data_offset + table_size
964 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 }
967 return true;
968}
969
Ian Rogers776ac1f2012-04-13 23:36:36 -0700970bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 int32_t offset;
972 bool isConditional, selfOkay;
973 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
974 return false;
975 }
976 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
978 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 return false;
980 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700981 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
982 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
985 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 return false;
987 }
988 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
989 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700990 if (abs_offset < 0 ||
991 (uint32_t) abs_offset >= insn_count ||
992 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700994 << reinterpret_cast<void*>(abs_offset) << ") at "
995 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 return false;
997 }
998 insn_flags_[abs_offset].SetBranchTarget();
999 return true;
1000}
1001
Ian Rogers776ac1f2012-04-13 23:36:36 -07001002bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 bool* selfOkay) {
1004 const uint16_t* insns = code_item_->insns_ + cur_offset;
1005 *pConditional = false;
1006 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001007 switch (*insns & 0xff) {
1008 case Instruction::GOTO:
1009 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001010 break;
1011 case Instruction::GOTO_32:
1012 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001013 *selfOkay = true;
1014 break;
1015 case Instruction::GOTO_16:
1016 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001017 break;
1018 case Instruction::IF_EQ:
1019 case Instruction::IF_NE:
1020 case Instruction::IF_LT:
1021 case Instruction::IF_GE:
1022 case Instruction::IF_GT:
1023 case Instruction::IF_LE:
1024 case Instruction::IF_EQZ:
1025 case Instruction::IF_NEZ:
1026 case Instruction::IF_LTZ:
1027 case Instruction::IF_GEZ:
1028 case Instruction::IF_GTZ:
1029 case Instruction::IF_LEZ:
1030 *pOffset = (int16_t) insns[1];
1031 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001032 break;
1033 default:
1034 return false;
1035 break;
1036 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001037 return true;
1038}
1039
Ian Rogers776ac1f2012-04-13 23:36:36 -07001040bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001042 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001044 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1046 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001048 << ", switch offset " << switch_offset
1049 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001050 return false;
1051 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001052 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001054 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001055 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1057 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001058 return false;
1059 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 uint32_t switch_count = switch_insns[1];
1061 int32_t keys_offset, targets_offset;
1062 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001063 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1064 /* 0=sig, 1=count, 2/3=firstKey */
1065 targets_offset = 4;
1066 keys_offset = -1;
1067 expected_signature = Instruction::kPackedSwitchSignature;
1068 } else {
1069 /* 0=sig, 1=count, 2..count*2 = keys */
1070 keys_offset = 2;
1071 targets_offset = 2 + 2 * switch_count;
1072 expected_signature = Instruction::kSparseSwitchSignature;
1073 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001075 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1077 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1078 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001079 return false;
1080 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001081 /* make sure the end of the switch is in range */
1082 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1084 << ", switch offset " << switch_offset
1085 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001086 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001087 return false;
1088 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001089 /* for a sparse switch, verify the keys are in ascending order */
1090 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1092 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001093 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1094 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1095 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1097 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001098 return false;
1099 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001100 last_key = key;
1101 }
1102 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001103 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 for (uint32_t targ = 0; targ < switch_count; targ++) {
1105 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1106 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1107 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001108 if (abs_offset < 0 ||
1109 abs_offset >= (int32_t) insn_count ||
1110 !insn_flags_[abs_offset].IsOpcode()) {
1111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1112 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1113 << reinterpret_cast<void*>(cur_offset)
1114 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001115 return false;
1116 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 insn_flags_[abs_offset].SetBranchTarget();
1118 }
1119 return true;
1120}
1121
Ian Rogers776ac1f2012-04-13 23:36:36 -07001122bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001123 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 return false;
1126 }
1127 uint16_t registers_size = code_item_->registers_size_;
1128 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001129 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1131 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 return false;
1133 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001134 }
1135
1136 return true;
1137}
1138
Ian Rogers776ac1f2012-04-13 23:36:36 -07001139bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 uint16_t registers_size = code_item_->registers_size_;
1141 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1142 // integer overflow when adding them here.
1143 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1145 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001146 return false;
1147 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001148 return true;
1149}
1150
Ian Rogers776ac1f2012-04-13 23:36:36 -07001151bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 uint16_t registers_size = code_item_->registers_size_;
1153 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001154
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001156 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1157 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 }
1159 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001160 reg_table_.Init(kTrackCompilerInterestPoints,
1161 insn_flags_.get(),
1162 insns_size,
1163 registers_size,
1164 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001165
jeffhaobdb76512011-09-07 11:43:16 -07001166
Ian Rogersd0fbd852013-09-24 18:17:04 -07001167 work_line_.reset(RegisterLine::Create(registers_size, this));
1168 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001169
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 /* Initialize register types of method arguments. */
1171 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001172 DCHECK_NE(failures_.size(), 0U);
1173 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001174 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001175 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 return false;
1177 }
1178 /* Perform code flow verification. */
1179 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001180 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001182 }
jeffhaobdb76512011-09-07 11:43:16 -07001183 return true;
1184}
1185
Ian Rogersad0b3a32012-04-16 14:50:24 -07001186std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1187 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001188 for (size_t i = 0; i < failures_.size(); ++i) {
1189 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001190 }
1191 return os;
1192}
1193
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001196 v->Dump(std::cerr);
1197}
1198
Ian Rogers776ac1f2012-04-13 23:36:36 -07001199void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001200 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001201 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 return;
jeffhaobdb76512011-09-07 11:43:16 -07001203 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 {
1205 os << "Register Types:\n";
1206 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1207 std::ostream indent_os(&indent_filter);
1208 reg_types_.Dump(indent_os);
1209 }
Ian Rogersb4903572012-10-11 11:52:56 -07001210 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001211 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1212 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 const Instruction* inst = Instruction::At(code_item_->insns_);
1214 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001215 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001217 if (reg_line != nullptr) {
1218 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001219 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001220 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001221 const bool kDumpHexOfInstruction = false;
1222 if (kDumpHexOfInstruction) {
1223 indent_os << inst->DumpHex(5) << " ";
1224 }
1225 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001226 inst = inst->Next();
1227 }
jeffhaobdb76512011-09-07 11:43:16 -07001228}
1229
Ian Rogersd81871c2011-10-03 13:57:23 -07001230static bool IsPrimitiveDescriptor(char descriptor) {
1231 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001232 case 'I':
1233 case 'C':
1234 case 'S':
1235 case 'B':
1236 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001237 case 'F':
1238 case 'D':
1239 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001241 default:
1242 return false;
1243 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001244}
1245
Ian Rogers776ac1f2012-04-13 23:36:36 -07001246bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 RegisterLine* reg_line = reg_table_.GetLine(0);
1248 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1249 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001250
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001252 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001254 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1256 // argument as uninitialized. This restricts field access until the superclass constructor is
1257 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001258 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001259 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001260 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 reg_types_.UninitializedThisArgument(declaring_class));
1262 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001263 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001264 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001266 }
1267
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001268 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001269 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001270 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001271
1272 for (; iterator.HasNext(); iterator.Next()) {
1273 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001274 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001275 LOG(FATAL) << "Null descriptor";
1276 }
1277 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1279 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 return false;
1281 }
1282 switch (descriptor[0]) {
1283 case 'L':
1284 case '[':
1285 // We assume that reference arguments are initialized. The only way it could be otherwise
1286 // (assuming the caller was verified) is if the current method is <init>, but in that case
1287 // it's effectively considered initialized the instant we reach here (in the sense that we
1288 // can return without doing anything or call virtual methods).
1289 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001290 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001291 if (!reg_type.IsNonZeroReferenceTypes()) {
1292 DCHECK(HasFailures());
1293 return false;
1294 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001295 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 }
1297 break;
1298 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001299 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 break;
1301 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001302 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 break;
1304 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001305 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 break;
1307 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001308 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 break;
1310 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001311 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 break;
1313 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001314 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 break;
1316 case 'J':
1317 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001318 if (cur_arg + 1 >= expected_args) {
1319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1320 << " args, found more (" << descriptor << ")";
1321 return false;
1322 }
1323
Ian Rogers7b078e82014-09-10 14:44:24 -07001324 const RegType* lo_half;
1325 const RegType* hi_half;
1326 if (descriptor[0] == 'J') {
1327 lo_half = &reg_types_.LongLo();
1328 hi_half = &reg_types_.LongHi();
1329 } else {
1330 lo_half = &reg_types_.DoubleLo();
1331 hi_half = &reg_types_.DoubleHi();
1332 }
1333 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 cur_arg++;
1335 break;
1336 }
1337 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1339 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 return false;
1341 }
1342 cur_arg++;
1343 }
1344 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1346 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 return false;
1348 }
1349 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1350 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1351 // format. Only major difference from the method argument format is that 'V' is supported.
1352 bool result;
1353 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1354 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001355 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 size_t i = 0;
1357 do {
1358 i++;
1359 } while (descriptor[i] == '['); // process leading [
1360 if (descriptor[i] == 'L') { // object array
1361 do {
1362 i++; // find closing ;
1363 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1364 result = descriptor[i] == ';';
1365 } else { // primitive array
1366 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1367 }
1368 } else if (descriptor[0] == 'L') {
1369 // could be more thorough here, but shouldn't be required
1370 size_t i = 0;
1371 do {
1372 i++;
1373 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1374 result = descriptor[i] == ';';
1375 } else {
1376 result = false;
1377 }
1378 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1380 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 }
1382 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001383}
1384
Ian Rogers776ac1f2012-04-13 23:36:36 -07001385bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 const uint16_t* insns = code_item_->insns_;
1387 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001388
jeffhaobdb76512011-09-07 11:43:16 -07001389 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 insn_flags_[0].SetChanged();
1391 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001392
jeffhaobdb76512011-09-07 11:43:16 -07001393 /* Continue until no instructions are marked "changed". */
1394 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001395 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1397 uint32_t insn_idx = start_guess;
1398 for (; insn_idx < insns_size; insn_idx++) {
1399 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001400 break;
1401 }
jeffhaobdb76512011-09-07 11:43:16 -07001402 if (insn_idx == insns_size) {
1403 if (start_guess != 0) {
1404 /* try again, starting from the top */
1405 start_guess = 0;
1406 continue;
1407 } else {
1408 /* all flags are clear */
1409 break;
1410 }
1411 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 // We carry the working set of registers from instruction to instruction. If this address can
1413 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1414 // "changed" flags, we need to load the set of registers from the table.
1415 // Because we always prefer to continue on to the next instruction, we should never have a
1416 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1417 // target.
1418 work_insn_idx_ = insn_idx;
1419 if (insn_flags_[insn_idx].IsBranchTarget()) {
1420 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001421 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001422 /*
1423 * Sanity check: retrieve the stored register line (assuming
1424 * a full table) and make sure it actually matches.
1425 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001427 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 if (work_line_->CompareLine(register_line) != 0) {
1429 Dump(std::cout);
1430 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001431 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001432 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001433 << " work_line=" << work_line_->Dump(this) << "\n"
1434 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 }
jeffhaobdb76512011-09-07 11:43:16 -07001436 }
jeffhaobdb76512011-09-07 11:43:16 -07001437 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001439 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001440 prepend += " failed to verify: ";
1441 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001442 return false;
1443 }
jeffhaobdb76512011-09-07 11:43:16 -07001444 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 insn_flags_[insn_idx].SetVisited();
1446 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001447 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001448
Ian Rogers1c849e52012-06-28 14:00:33 -07001449 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001450 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001451 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001452 * (besides the wasted space), but it indicates a flaw somewhere
1453 * down the line, possibly in the verifier.
1454 *
1455 * If we've substituted "always throw" instructions into the stream,
1456 * we are almost certainly going to have some dead code.
1457 */
1458 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001460 for (; insn_idx < insns_size;
1461 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001462 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001463 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001464 * may or may not be preceded by a padding NOP (for alignment).
1465 */
1466 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1467 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1468 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001469 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001470 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1471 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1472 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001474 }
1475
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001477 if (dead_start < 0)
1478 dead_start = insn_idx;
1479 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001480 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1481 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001482 dead_start = -1;
1483 }
1484 }
1485 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001486 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1487 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001489 // To dump the state of the verify after a method, do something like:
1490 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1491 // "boolean java.lang.String.equals(java.lang.Object)") {
1492 // LOG(INFO) << info_messages_.str();
1493 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001494 }
jeffhaobdb76512011-09-07 11:43:16 -07001495 return true;
1496}
1497
Ian Rogers776ac1f2012-04-13 23:36:36 -07001498bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001499 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1500 // We want the state _before_ the instruction, for the case where the dex pc we're
1501 // interested in is itself a monitor-enter instruction (which is a likely place
1502 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001503 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001504 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001505 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1506 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1507 }
1508 }
1509
jeffhaobdb76512011-09-07 11:43:16 -07001510 /*
1511 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001512 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001513 * control to another statement:
1514 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001515 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001516 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001517 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001518 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001519 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001520 * throw an exception that is handled by an encompassing "try"
1521 * block.
1522 *
1523 * We can also return, in which case there is no successor instruction
1524 * from this point.
1525 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001526 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001527 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1529 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001530 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001531
jeffhaobdb76512011-09-07 11:43:16 -07001532 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001533 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001534 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001536 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001537 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 }
jeffhaobdb76512011-09-07 11:43:16 -07001539
1540 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001541 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001542 * can throw an exception, we will copy/merge this into the "catch"
1543 * address rather than work_line, because we don't want the result
1544 * from the "successful" code path (e.g. a check-cast that "improves"
1545 * a type) to be visible to the exception handler.
1546 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001547 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001549 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001551 }
1552
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001553
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001554 // We need to ensure the work line is consistent while performing validation. When we spot a
1555 // peephole pattern we compute a new line for either the fallthrough instruction or the
1556 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001557 std::unique_ptr<RegisterLine> branch_line;
1558 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001559
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001561 case Instruction::NOP:
1562 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001563 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001564 * a signature that looks like a NOP; if we see one of these in
1565 * the course of executing code then we have a problem.
1566 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001567 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001569 }
1570 break;
1571
1572 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001573 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001574 break;
jeffhaobdb76512011-09-07 11:43:16 -07001575 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001576 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001577 break;
jeffhaobdb76512011-09-07 11:43:16 -07001578 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001579 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001580 break;
1581 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001582 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001583 break;
jeffhaobdb76512011-09-07 11:43:16 -07001584 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001585 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001586 break;
jeffhaobdb76512011-09-07 11:43:16 -07001587 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001588 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001589 break;
1590 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001591 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001592 break;
jeffhaobdb76512011-09-07 11:43:16 -07001593 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001594 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001595 break;
jeffhaobdb76512011-09-07 11:43:16 -07001596 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001597 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001598 break;
1599
1600 /*
1601 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001602 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001603 * might want to hold the result in an actual CPU register, so the
1604 * Dalvik spec requires that these only appear immediately after an
1605 * invoke or filled-new-array.
1606 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001607 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001608 * redundant with the reset done below, but it can make the debug info
1609 * easier to read in some cases.)
1610 */
1611 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001612 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001613 break;
1614 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001615 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001616 break;
1617 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001618 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001619 break;
1620
Ian Rogersd81871c2011-10-03 13:57:23 -07001621 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001622 /*
jeffhao60f83e32012-02-13 17:16:30 -08001623 * This statement can only appear as the first instruction in an exception handler. We verify
1624 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001625 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001626 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001627 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001628 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001629 }
jeffhaobdb76512011-09-07 11:43:16 -07001630 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001631 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001632 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 }
jeffhaobdb76512011-09-07 11:43:16 -07001635 }
1636 break;
1637 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001638 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001639 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001640 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1643 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 } else {
1645 // Compilers may generate synthetic functions that write byte values into boolean fields.
1646 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001648 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1650 ((return_type.IsBoolean() || return_type.IsByte() ||
1651 return_type.IsShort() || return_type.IsChar()) &&
1652 src_type.IsInteger()));
1653 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001654 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001655 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001656 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001658 }
jeffhaobdb76512011-09-07 11:43:16 -07001659 }
1660 }
1661 break;
1662 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001663 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001664 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001665 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 } else {
1669 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001671 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001672 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001673 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 }
jeffhaobdb76512011-09-07 11:43:16 -07001675 }
1676 }
1677 break;
1678 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001679 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001680 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 } else {
1684 /* return_type is the *expected* return type, not register value */
1685 DCHECK(!return_type.IsZero());
1686 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001687 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001688 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001689 // Disallow returning uninitialized values and verify that the reference in vAA is an
1690 // instance of the "return_type"
1691 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001692 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1693 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001694 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001695 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1696 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1697 << "' or '" << reg_type << "'";
1698 } else {
1699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1700 << "', but expected from declaration '" << return_type << "'";
1701 }
jeffhaobdb76512011-09-07 11:43:16 -07001702 }
1703 }
1704 }
1705 break;
1706
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001707 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001708 case Instruction::CONST_4: {
1709 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001710 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001711 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001712 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 }
1714 case Instruction::CONST_16: {
1715 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001716 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001717 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001718 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001719 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001720 case Instruction::CONST: {
1721 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001722 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001723 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001724 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001725 }
1726 case Instruction::CONST_HIGH16: {
1727 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001728 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001729 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001730 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001733 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001735 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1736 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001737 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001738 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001739 }
1740 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001741 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001742 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1743 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001745 break;
1746 }
1747 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001748 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001749 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1750 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001751 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001752 break;
1753 }
1754 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001756 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1757 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001758 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001759 break;
1760 }
jeffhaobdb76512011-09-07 11:43:16 -07001761 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001762 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 break;
jeffhaobdb76512011-09-07 11:43:16 -07001764 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001765 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001768 // Get type from instruction if unresolved then we need an access check
1769 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001770 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001771 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001772 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001773 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001774 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 }
jeffhaobdb76512011-09-07 11:43:16 -07001777 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001779 break;
1780 case Instruction::MONITOR_EXIT:
1781 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001782 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001783 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001784 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001785 * to the need to handle asynchronous exceptions, a now-deprecated
1786 * feature that Dalvik doesn't support.)
1787 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001788 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001789 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001790 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001791 * structured locking checks are working, the former would have
1792 * failed on the -enter instruction, and the latter is impossible.
1793 *
1794 * This is fortunate, because issue 3221411 prevents us from
1795 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001796 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001797 * some catch blocks (which will show up as "dead" code when
1798 * we skip them here); if we can't, then the code path could be
1799 * "live" so we still need to check it.
1800 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001801 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001802 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
1804
Ian Rogers28ad40d2011-10-27 15:19:26 -07001805 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001807 /*
1808 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1809 * could be a "upcast" -- not expected, so we don't try to address it.)
1810 *
1811 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001812 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001813 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001814 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1815 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001816 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001817 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001818 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001819 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001820 if (klass != nullptr && klass->IsPrimitive()) {
1821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1822 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1823 << GetDeclaringClass();
1824 break;
1825 }
1826
Ian Rogersad0b3a32012-04-16 14:50:24 -07001827 DCHECK_NE(failures_.size(), 0U);
1828 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001829 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001830 }
1831 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001832 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001833 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001835 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001836 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001837 if (is_checkcast) {
1838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1839 } else {
1840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1841 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001842 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001843 if (is_checkcast) {
1844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1845 } else {
1846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1847 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001848 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001849 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001850 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001852 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001853 }
jeffhaobdb76512011-09-07 11:43:16 -07001854 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001855 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
1857 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001859 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001860 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001863 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001865 } else {
1866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 }
1868 break;
1869 }
1870 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001871 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001872 if (res_type.IsConflict()) {
1873 DCHECK_NE(failures_.size(), 0U);
1874 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001875 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001876 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1877 // can't create an instance of an interface or abstract class */
1878 if (!res_type.IsInstantiableTypes()) {
1879 Fail(VERIFY_ERROR_INSTANTIATION)
1880 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001881 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001883 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001884 // Any registers holding previous allocations from this address that have not yet been
1885 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001886 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001887 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001888 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 break;
1890 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001891 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001892 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001893 break;
1894 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001896 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001897 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001898 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001900 just_set_result = true; // Filled new array range sets result register
1901 break;
jeffhaobdb76512011-09-07 11:43:16 -07001902 case Instruction::CMPL_FLOAT:
1903 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001904 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001905 break;
1906 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001908 break;
1909 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001910 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001911 break;
1912 case Instruction::CMPL_DOUBLE:
1913 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001915 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001916 break;
1917 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_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 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001923 break;
1924 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001925 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001926 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001927 break;
1928 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_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 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001934 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001936 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001937 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001938 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1939 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001940 }
1941 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 }
jeffhaobdb76512011-09-07 11:43:16 -07001943 case Instruction::GOTO:
1944 case Instruction::GOTO_16:
1945 case Instruction::GOTO_32:
1946 /* no effect on or use of registers */
1947 break;
1948
1949 case Instruction::PACKED_SWITCH:
1950 case Instruction::SPARSE_SWITCH:
1951 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
1954
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 case Instruction::FILL_ARRAY_DATA: {
1956 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001957 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001958 /* array_type can be null if the reg type is Zero */
1959 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001960 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1962 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001963 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001964 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001965 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001966 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1968 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 } else {
jeffhao457cc512012-02-02 16:55:13 -08001970 // Now verify if the element width in the table matches the element width declared in
1971 // the array
1972 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1973 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001975 } else {
1976 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1977 // Since we don't compress the data in Dex, expect to see equal width of data stored
1978 // in the table and expected from the array class.
1979 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1981 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001982 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 }
1984 }
jeffhaobdb76512011-09-07 11:43:16 -07001985 }
1986 }
1987 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 }
jeffhaobdb76512011-09-07 11:43:16 -07001989 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001991 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1992 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 bool mismatch = false;
1994 if (reg_type1.IsZero()) { // zero then integral or reference expected
1995 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1996 } else if (reg_type1.IsReferenceTypes()) { // both references?
1997 mismatch = !reg_type2.IsReferenceTypes();
1998 } else { // both integral?
1999 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2000 }
2001 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2003 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002004 }
2005 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 }
jeffhaobdb76512011-09-07 11:43:16 -07002007 case Instruction::IF_LT:
2008 case Instruction::IF_GE:
2009 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002011 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2012 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2015 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002016 }
2017 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 }
jeffhaobdb76512011-09-07 11:43:16 -07002019 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002021 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2024 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002026
2027 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002028 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002029 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002030 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002031 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002032 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002033 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002034 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2035 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2036 work_insn_idx_)) {
2037 break;
2038 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002039 } else {
2040 break;
2041 }
2042
Ian Rogers9b360392013-06-06 14:45:07 -07002043 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002044
2045 /* Check for peep-hole pattern of:
2046 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002047 * instance-of vX, vY, T;
2048 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002049 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002050 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002051 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002052 * and sharpen the type of vY to be type T.
2053 * Note, this pattern can't be if:
2054 * - if there are other branches to this branch,
2055 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002056 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002057 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002058 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2059 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2060 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002061 // Check the type of the instance-of is different than that of registers type, as if they
2062 // are the same there is no work to be done here. Check that the conversion is not to or
2063 // from an unresolved type as type information is imprecise. If the instance-of is to an
2064 // interface then ignore the type information as interfaces can only be treated as Objects
2065 // and we don't want to disallow field and other operations on the object. If the value
2066 // being instance-of checked against is known null (zero) then allow the optimization as
2067 // we didn't have type information. If the merge of the instance-of type with the original
2068 // type is assignable to the original then allow optimization. This check is performed to
2069 // ensure that subsequent merges don't lose type information - such as becoming an
2070 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002071 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002072 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002073
Ian Rogersebbdd872014-07-07 23:53:08 -07002074 if (!orig_type.Equals(cast_type) &&
2075 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002076 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002077 !cast_type.GetClass()->IsInterface() &&
2078 (orig_type.IsZero() ||
2079 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002080 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002081 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002082 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002083 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002084 branch_line.reset(update_line);
2085 }
2086 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002087 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002088 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2089 // See if instance-of was preceded by a move-object operation, common due to the small
2090 // register encoding space of instance-of, and propagate type information to the source
2091 // of the move-object.
2092 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002093 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002094 move_idx--;
2095 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002096 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2097 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2098 work_insn_idx_)) {
2099 break;
2100 }
Ian Rogers9b360392013-06-06 14:45:07 -07002101 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2102 switch (move_inst->Opcode()) {
2103 case Instruction::MOVE_OBJECT:
2104 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002105 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002106 }
2107 break;
2108 case Instruction::MOVE_OBJECT_FROM16:
2109 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002110 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002111 }
2112 break;
2113 case Instruction::MOVE_OBJECT_16:
2114 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002115 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002116 }
2117 break;
2118 default:
2119 break;
2120 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002121 }
2122 }
2123 }
2124
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 }
jeffhaobdb76512011-09-07 11:43:16 -07002127 case Instruction::IF_LTZ:
2128 case Instruction::IF_GEZ:
2129 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002131 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2134 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 }
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 }
jeffhaobdb76512011-09-07 11:43:16 -07002138 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002139 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 break;
jeffhaobdb76512011-09-07 11:43:16 -07002141 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 break;
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 break;
jeffhaobdb76512011-09-07 11:43:16 -07002147 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 break;
jeffhaobdb76512011-09-07 11:43:16 -07002153 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 break;
2156 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 break;
2163 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 break;
2166 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 break;
2169 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
2181
jeffhaobdb76512011-09-07 11:43:16 -07002182 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002183 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 break;
jeffhaobdb76512011-09-07 11:43:16 -07002185 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002186 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 break;
jeffhaobdb76512011-09-07 11:43:16 -07002188 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002189 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 break;
jeffhaobdb76512011-09-07 11:43:16 -07002191 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002192 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 break;
2194 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002199 break;
2200 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 break;
jeffhaobdb76512011-09-07 11:43:16 -07002203
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002205 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 break;
2207 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002208 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 break;
2210 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002211 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 break;
2213 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002214 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002215 break;
2216 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
2219 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 break;
jeffhaobdb76512011-09-07 11:43:16 -07002222 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002224 break;
2225
jeffhaobdb76512011-09-07 11:43:16 -07002226 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002227 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 break;
jeffhaobdb76512011-09-07 11:43:16 -07002229 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 break;
jeffhaobdb76512011-09-07 11:43:16 -07002232 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002233 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 break;
jeffhaobdb76512011-09-07 11:43:16 -07002235 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002236 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 break;
2238 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002240 break;
2241 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002243 break;
2244 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002245 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 break;
2247
2248 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002249 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 break;
2251 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002252 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 break;
2254 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 break;
2257 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
2260 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002261 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002262 break;
2263 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002264 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002265 break;
2266 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002267 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
2269
2270 case Instruction::INVOKE_VIRTUAL:
2271 case Instruction::INVOKE_VIRTUAL_RANGE:
2272 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2275 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002276 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2277 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002278 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2279 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002280 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002281 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002282 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002283 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002284 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002285 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002286 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2287 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002288 return_type_class->CannotBeAssignedFromOtherTypes());
2289 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002290 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2291 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002292 }
2293 }
2294 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002296 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2297 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002298 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002299 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002300 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002301 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002302 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002304 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002306 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 }
jeffhaobdb76512011-09-07 11:43:16 -07002309 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002311 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002312 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002313 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002314 const char* return_type_descriptor;
2315 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002316 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002319 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002320 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002321 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2322 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2323 } else {
2324 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002325 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002326 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002327 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002328 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002329 if (return_type_class != nullptr) {
2330 return_type = &reg_types_.FromClass(return_type_descriptor,
2331 return_type_class,
2332 return_type_class->CannotBeAssignedFromOtherTypes());
2333 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002334 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2335 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002336 }
Ian Rogers46685432012-06-03 22:26:43 -07002337 }
2338 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002339 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 * Some additional checks when calling a constructor. We know from the invocation arg check
2341 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2342 * that to require that called_method->klass is the same as this->klass or this->super,
2343 * allowing the latter only if the "this" argument is the same as the "this" argument to
2344 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002345 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002346 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002347 if (this_type.IsConflict()) // failure.
2348 break;
jeffhaobdb76512011-09-07 11:43:16 -07002349
jeffhaob57e9522012-04-26 18:08:21 -07002350 /* no null refs allowed (?) */
2351 if (this_type.IsZero()) {
2352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2353 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002354 }
jeffhaob57e9522012-04-26 18:08:21 -07002355
2356 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002357 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002358 // TODO: re-enable constructor type verification
2359 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002360 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002361 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2362 // break;
2363 // }
jeffhaob57e9522012-04-26 18:08:21 -07002364
2365 /* arg must be an uninitialized reference */
2366 if (!this_type.IsUninitializedTypes()) {
2367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2368 << this_type;
2369 break;
2370 }
2371
2372 /*
2373 * Replace the uninitialized reference with an initialized one. We need to do this for all
2374 * registers that have the same object instance in them, not just the "this" register.
2375 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002376 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002377 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002378 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002379 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2380 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002381 }
2382 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002383 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002385 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002386 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002387 just_set_result = true;
2388 break;
2389 }
2390 case Instruction::INVOKE_STATIC:
2391 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002392 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002393 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002394 METHOD_STATIC,
2395 is_range,
2396 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002397 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002398 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002399 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002400 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2401 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002402 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002403 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002404 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002405 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002406 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002407 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002408 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002409 } else {
2410 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2411 }
jeffhaobdb76512011-09-07 11:43:16 -07002412 just_set_result = true;
2413 }
2414 break;
jeffhaobdb76512011-09-07 11:43:16 -07002415 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002418 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002419 METHOD_INTERFACE,
2420 is_range,
2421 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002422 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002423 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002424 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2425 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2426 << PrettyMethod(abs_method) << "'";
2427 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002428 }
Ian Rogers0d604842012-04-16 14:50:24 -07002429 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002430 /* Get the type of the "this" arg, which should either be a sub-interface of called
2431 * interface or Object (see comments in RegType::JoinClass).
2432 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002433 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002434 if (this_type.IsZero()) {
2435 /* null pointer always passes (and always fails at runtime) */
2436 } else {
2437 if (this_type.IsUninitializedTypes()) {
2438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2439 << this_type;
2440 break;
2441 }
2442 // In the past we have tried to assert that "called_interface" is assignable
2443 // from "this_type.GetClass()", however, as we do an imprecise Join
2444 // (RegType::JoinClass) we don't have full information on what interfaces are
2445 // implemented by "this_type". For example, two classes may implement the same
2446 // interfaces and have a common parent that doesn't implement the interface. The
2447 // join will set "this_type" to the parent class and a test that this implements
2448 // the interface will incorrectly fail.
2449 }
2450 /*
2451 * We don't have an object instance, so we can't find the concrete method. However, all of
2452 * the type information is in the abstract method, so we're good.
2453 */
2454 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002456 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002457 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2458 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2459 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2460 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002461 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002462 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002463 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002465 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002466 } else {
2467 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2468 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002469 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 }
jeffhaobdb76512011-09-07 11:43:16 -07002472 case Instruction::NEG_INT:
2473 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002474 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::NEG_LONG:
2477 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002478 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002479 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002480 break;
2481 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002485 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002486 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002489 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002490 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002493 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002496 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002497 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002500 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002501 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002504 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002505 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
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::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002515 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002516 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002519 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002520 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002521 break;
2522 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002524 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002525 break;
2526 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002528 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002531 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
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::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002535 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
2537 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002539 break;
2540 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002541 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002542 break;
2543
2544 case Instruction::ADD_INT:
2545 case Instruction::SUB_INT:
2546 case Instruction::MUL_INT:
2547 case Instruction::REM_INT:
2548 case Instruction::DIV_INT:
2549 case Instruction::SHL_INT:
2550 case Instruction::SHR_INT:
2551 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002552 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002553 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002554 break;
2555 case Instruction::AND_INT:
2556 case Instruction::OR_INT:
2557 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002558 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002559 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002560 break;
2561 case Instruction::ADD_LONG:
2562 case Instruction::SUB_LONG:
2563 case Instruction::MUL_LONG:
2564 case Instruction::DIV_LONG:
2565 case Instruction::REM_LONG:
2566 case Instruction::AND_LONG:
2567 case Instruction::OR_LONG:
2568 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002570 reg_types_.LongLo(), reg_types_.LongHi(),
2571 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::SHL_LONG:
2574 case Instruction::SHR_LONG:
2575 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002578 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002579 break;
2580 case Instruction::ADD_FLOAT:
2581 case Instruction::SUB_FLOAT:
2582 case Instruction::MUL_FLOAT:
2583 case Instruction::DIV_FLOAT:
2584 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2586 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::ADD_DOUBLE:
2589 case Instruction::SUB_DOUBLE:
2590 case Instruction::MUL_DOUBLE:
2591 case Instruction::DIV_DOUBLE:
2592 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002594 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2595 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::ADD_INT_2ADDR:
2598 case Instruction::SUB_INT_2ADDR:
2599 case Instruction::MUL_INT_2ADDR:
2600 case Instruction::REM_INT_2ADDR:
2601 case Instruction::SHL_INT_2ADDR:
2602 case Instruction::SHR_INT_2ADDR:
2603 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2605 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::AND_INT_2ADDR:
2608 case Instruction::OR_INT_2ADDR:
2609 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002610 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2611 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002614 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2615 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 case Instruction::ADD_LONG_2ADDR:
2618 case Instruction::SUB_LONG_2ADDR:
2619 case Instruction::MUL_LONG_2ADDR:
2620 case Instruction::DIV_LONG_2ADDR:
2621 case Instruction::REM_LONG_2ADDR:
2622 case Instruction::AND_LONG_2ADDR:
2623 case Instruction::OR_LONG_2ADDR:
2624 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002626 reg_types_.LongLo(), reg_types_.LongHi(),
2627 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::SHL_LONG_2ADDR:
2630 case Instruction::SHR_LONG_2ADDR:
2631 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002632 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002633 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 case Instruction::ADD_FLOAT_2ADDR:
2636 case Instruction::SUB_FLOAT_2ADDR:
2637 case Instruction::MUL_FLOAT_2ADDR:
2638 case Instruction::DIV_FLOAT_2ADDR:
2639 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002640 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2641 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002642 break;
2643 case Instruction::ADD_DOUBLE_2ADDR:
2644 case Instruction::SUB_DOUBLE_2ADDR:
2645 case Instruction::MUL_DOUBLE_2ADDR:
2646 case Instruction::DIV_DOUBLE_2ADDR:
2647 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002648 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002649 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2650 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002653 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002654 case Instruction::MUL_INT_LIT16:
2655 case Instruction::DIV_INT_LIT16:
2656 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2658 true);
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::AND_INT_LIT16:
2661 case Instruction::OR_INT_LIT16:
2662 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002663 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2664 true);
jeffhaobdb76512011-09-07 11:43:16 -07002665 break;
2666 case Instruction::ADD_INT_LIT8:
2667 case Instruction::RSUB_INT_LIT8:
2668 case Instruction::MUL_INT_LIT8:
2669 case Instruction::DIV_INT_LIT8:
2670 case Instruction::REM_INT_LIT8:
2671 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002672 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002673 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002674 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2675 false);
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::AND_INT_LIT8:
2678 case Instruction::OR_INT_LIT8:
2679 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002680 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2681 false);
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002684 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002685 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002686 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2688 }
2689 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002690 // Note: the following instructions encode offsets derived from class linking.
2691 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2692 // meaning if the class linking and resolution were successful.
2693 case Instruction::IGET_QUICK:
2694 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2695 break;
2696 case Instruction::IGET_WIDE_QUICK:
2697 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2698 break;
2699 case Instruction::IGET_OBJECT_QUICK:
2700 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2701 break;
2702 case Instruction::IPUT_QUICK:
2703 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2704 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002705 case Instruction::IPUT_BOOLEAN_QUICK:
2706 VerifyIPutQuick(inst, reg_types_.Boolean(), true);
2707 break;
2708 case Instruction::IPUT_BYTE_QUICK:
2709 VerifyIPutQuick(inst, reg_types_.Byte(), true);
2710 break;
2711 case Instruction::IPUT_CHAR_QUICK:
2712 VerifyIPutQuick(inst, reg_types_.Char(), true);
2713 break;
2714 case Instruction::IPUT_SHORT_QUICK:
2715 VerifyIPutQuick(inst, reg_types_.Short(), true);
2716 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002717 case Instruction::IPUT_WIDE_QUICK:
2718 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2719 break;
2720 case Instruction::IPUT_OBJECT_QUICK:
2721 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2722 break;
2723 case Instruction::INVOKE_VIRTUAL_QUICK:
2724 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2725 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002726 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002727 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002728 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002729 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002730 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002731 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002732 } else {
2733 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2734 }
2735 just_set_result = true;
2736 }
2737 break;
2738 }
2739
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002741 case Instruction::UNUSED_3E:
2742 case Instruction::UNUSED_3F:
2743 case Instruction::UNUSED_40:
2744 case Instruction::UNUSED_41:
2745 case Instruction::UNUSED_42:
2746 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002747 case Instruction::UNUSED_79:
2748 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002749 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002750 case Instruction::UNUSED_F0:
2751 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002752 case Instruction::UNUSED_F2:
2753 case Instruction::UNUSED_F3:
2754 case Instruction::UNUSED_F4:
2755 case Instruction::UNUSED_F5:
2756 case Instruction::UNUSED_F6:
2757 case Instruction::UNUSED_F7:
2758 case Instruction::UNUSED_F8:
2759 case Instruction::UNUSED_F9:
2760 case Instruction::UNUSED_FA:
2761 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002762 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002763 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002764 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002765 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768
2769 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002770 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002771 * complain if an instruction is missing (which is desirable).
2772 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002773 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002774
Ian Rogersad0b3a32012-04-16 14:50:24 -07002775 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002776 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002777 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002778 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002779 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002780 /* immediate failure, reject class */
2781 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2782 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002783 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002784 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002785 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002786 }
jeffhaobdb76512011-09-07 11:43:16 -07002787 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 * If we didn't just set the result register, clear it out. This ensures that you can only use
2789 * "move-result" immediately after the result is set. (We could check this statically, but it's
2790 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002791 */
2792 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002793 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002794 }
2795
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002796
jeffhaobdb76512011-09-07 11:43:16 -07002797
2798 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002799 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002800 *
2801 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002802 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002803 * somebody could get a reference field, check it for zero, and if the
2804 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002805 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002806 * that, and will reject the code.
2807 *
2808 * TODO: avoid re-fetching the branch target
2809 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002810 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002811 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002813 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002815 return false;
2816 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002817 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002818 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002819 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 }
jeffhaobdb76512011-09-07 11:43:16 -07002821 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002822 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002823 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002824 return false;
2825 }
2826 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002827 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002828 return false;
2829 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 }
jeffhaobdb76512011-09-07 11:43:16 -07002831 }
2832
2833 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002834 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002835 *
2836 * We've already verified that the table is structurally sound, so we
2837 * just need to walk through and tag the targets.
2838 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002839 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002840 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2841 const uint16_t* switch_insns = insns + offset_to_switch;
2842 int switch_count = switch_insns[1];
2843 int offset_to_targets, targ;
2844
2845 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2846 /* 0 = sig, 1 = count, 2/3 = first key */
2847 offset_to_targets = 4;
2848 } else {
2849 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002850 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002851 offset_to_targets = 2 + 2 * switch_count;
2852 }
2853
2854 /* verify each switch target */
2855 for (targ = 0; targ < switch_count; targ++) {
2856 int offset;
2857 uint32_t abs_offset;
2858
2859 /* offsets are 32-bit, and only partly endian-swapped */
2860 offset = switch_insns[offset_to_targets + targ * 2] |
2861 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 abs_offset = work_insn_idx_ + offset;
2863 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002864 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002865 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002867 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002868 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002869 }
jeffhaobdb76512011-09-07 11:43:16 -07002870 }
2871 }
2872
2873 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2875 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002876 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002877 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002878 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002879 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002880
Andreas Gampef91baf12014-07-18 15:41:00 -07002881 // Need the linker to try and resolve the handled class to check if it's Throwable.
2882 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2883
Ian Rogers0571d352011-11-03 19:51:38 -07002884 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002885 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2886 if (handler_type_idx == DexFile::kDexNoIndex16) {
2887 has_catch_all_handler = true;
2888 } else {
2889 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002890 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2891 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002892 if (klass != nullptr) {
2893 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2894 has_catch_all_handler = true;
2895 }
2896 } else {
2897 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002898 DCHECK(self_->IsExceptionPending());
2899 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002900 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 }
jeffhaobdb76512011-09-07 11:43:16 -07002902 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2904 * "work_regs", because at runtime the exception will be thrown before the instruction
2905 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002906 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002907 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002908 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 }
jeffhaobdb76512011-09-07 11:43:16 -07002910 }
2911
2912 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2914 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002915 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002916 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002917 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 * The state in work_line reflects the post-execution state. If the current instruction is a
2919 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002920 * it will do so before grabbing the lock).
2921 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002922 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002923 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002925 return false;
2926 }
2927 }
2928 }
2929
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002930 /* Handle "continue". Tag the next consecutive instruction.
2931 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2932 * because it changes work_line_ when performing peephole optimization
2933 * and this change should not be used in those cases.
2934 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002935 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002936 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2937 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002938 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2940 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002941 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002942 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2943 // next instruction isn't one.
2944 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2945 return false;
2946 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002947 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002948 // Make workline consistent with fallthrough computed from peephole optimization.
2949 work_line_->CopyFromLine(fallthrough_line.get());
2950 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002951 if (insn_flags_[next_insn_idx].IsReturn()) {
2952 // For returns we only care about the operand to the return, all other registers are dead.
2953 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2954 Instruction::Code opcode = ret_inst->Opcode();
2955 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002956 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002957 } else {
2958 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002959 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002960 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002961 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002962 }
2963 }
2964 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002965 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002966 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002967 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2968 // needed. If the merge changes the state of the registers then the work line will be
2969 // updated.
2970 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002971 return false;
2972 }
2973 } else {
2974 /*
2975 * We're not recording register data for the next instruction, so we don't know what the
2976 * prior state was. We have to assume that something has changed and re-evaluate it.
2977 */
2978 insn_flags_[next_insn_idx].SetChanged();
2979 }
2980 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002981
jeffhaod1f0fde2011-09-08 17:25:33 -07002982 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002983 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002984 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 return false;
2986 }
jeffhaobdb76512011-09-07 11:43:16 -07002987 }
2988
2989 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002990 * Update start_guess. Advance to the next instruction of that's
2991 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002992 * neither of those exists we're in a return or throw; leave start_guess
2993 * alone and let the caller sort it out.
2994 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002995 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002996 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2997 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002998 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002999 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003001 }
3002
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3004 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003005
3006 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003007} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003008
Ian Rogersd8f69b02014-09-10 21:43:52 +00003009const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003010 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003011 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003012 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003014 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3015 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003016 if (result.IsConflict()) {
3017 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3018 << "' in " << referrer;
3019 return result;
3020 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003021 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003022 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003023 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003025 // check at runtime if access is allowed and so pass here. If result is
3026 // primitive, skip the access check.
3027 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3028 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003029 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003031 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003032 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003033}
3034
Ian Rogersd8f69b02014-09-10 21:43:52 +00003035const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003038 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3040 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003041 CatchHandlerIterator iterator(handlers_ptr);
3042 for (; iterator.HasNext(); iterator.Next()) {
3043 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3044 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003045 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003047 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003048 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003049 if (exception.IsUnresolvedTypes()) {
3050 // We don't know enough about the type. Fail here and let runtime handle it.
3051 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3052 return exception;
3053 } else {
3054 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3055 return reg_types_.Conflict();
3056 }
Jeff Haob878f212014-04-24 16:25:36 -07003057 } else if (common_super == nullptr) {
3058 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003059 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003060 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003061 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003062 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003063 if (FailOrAbort(this,
3064 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3065 "java.lang.Throwable is not assignable-from common_super at ",
3066 work_insn_idx_)) {
3067 break;
3068 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 }
3070 }
3071 }
3072 }
Ian Rogers0571d352011-11-03 19:51:38 -07003073 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 }
3075 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003076 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003078 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003079 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003081 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003082}
3083
Brian Carlstromea46f952013-07-30 01:26:50 -07003084mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003085 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003086 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003087 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003088 if (klass_type.IsConflict()) {
3089 std::string append(" in attempt to access method ");
3090 append += dex_file_->GetMethodName(method_id);
3091 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003093 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003094 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003095 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003096 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003097 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003098 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003099 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003101 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003102 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003103
3104 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003105 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003106 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 res_method = klass->FindInterfaceMethod(name, signature);
3108 } else {
3109 res_method = klass->FindVirtualMethod(name, signature);
3110 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003111 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003112 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003114 // If a virtual or interface method wasn't found with the expected type, look in
3115 // the direct methods. This can happen when the wrong invoke type is used or when
3116 // a class has changed, and will be flagged as an error in later checks.
3117 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3118 res_method = klass->FindDirectMethod(name, signature);
3119 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003120 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003121 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3122 << PrettyDescriptor(klass) << "." << name
3123 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003125 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 }
3127 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3129 // enforce them here.
3130 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3132 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003133 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003135 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003136 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3138 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003139 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003140 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003141 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003142 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003143 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003144 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003145 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003146 }
jeffhaode0d9c92012-02-27 13:58:13 -08003147 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3148 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3150 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003151 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003152 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003153 // Check that interface methods match interface classes.
3154 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3155 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3156 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003157 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003158 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3159 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3160 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003161 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003162 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 // See if the method type implied by the invoke instruction matches the access flags for the
3164 // target method.
3165 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3166 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3167 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3168 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003169 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3170 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003171 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003173 return res_method;
3174}
3175
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003176template <class T>
3177mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3178 MethodType method_type,
3179 bool is_range,
3180 mirror::ArtMethod* res_method) {
3181 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3182 // match the call to the signature. Also, we might be calling through an abstract method
3183 // definition (which doesn't have register count values).
3184 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3185 /* caught by static verifier */
3186 DCHECK(is_range || expected_args <= 5);
3187 if (expected_args > code_item_->outs_size_) {
3188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3189 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3190 return nullptr;
3191 }
3192
3193 uint32_t arg[5];
3194 if (!is_range) {
3195 inst->GetVarArgs(arg);
3196 }
3197 uint32_t sig_registers = 0;
3198
3199 /*
3200 * Check the "this" argument, which must be an instance of the class that declared the method.
3201 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3202 * rigorous check here (which is okay since we have to do it at runtime).
3203 */
3204 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003206 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3207 CHECK(have_pending_hard_failure_);
3208 return nullptr;
3209 }
3210 if (actual_arg_type.IsUninitializedReference()) {
3211 if (res_method) {
3212 if (!res_method->IsConstructor()) {
3213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3214 return nullptr;
3215 }
3216 } else {
3217 // Check whether the name of the called method is "<init>"
3218 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003219 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3221 return nullptr;
3222 }
3223 }
3224 }
3225 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003226 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003227 if (res_method != nullptr) {
3228 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003229 std::string temp;
3230 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003231 klass->CannotBeAssignedFromOtherTypes());
3232 } else {
3233 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3234 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003235 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003236 dex_file_->StringByTypeIdx(class_idx),
3237 false);
3238 }
3239 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3240 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3241 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3242 << "' not instance of '" << *res_method_class << "'";
3243 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3244 // the compiler.
3245 if (have_pending_hard_failure_) {
3246 return nullptr;
3247 }
3248 }
3249 }
3250 sig_registers = 1;
3251 }
3252
3253 for ( ; it->HasNext(); it->Next()) {
3254 if (sig_registers >= expected_args) {
3255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3256 " arguments, found " << sig_registers << " or more.";
3257 return nullptr;
3258 }
3259
3260 const char* param_descriptor = it->GetDescriptor();
3261
3262 if (param_descriptor == nullptr) {
3263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3264 "component";
3265 return nullptr;
3266 }
3267
Ian Rogersd8f69b02014-09-10 21:43:52 +00003268 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003269 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3270 arg[sig_registers];
3271 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003272 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003273 if (!src_type.IsIntegralTypes()) {
3274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3275 << " but expected " << reg_type;
3276 return res_method;
3277 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003278 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003279 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3280 // compiler.
3281 if (have_pending_hard_failure_) {
3282 return res_method;
3283 }
3284 }
3285 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3286 }
3287 if (expected_args != sig_registers) {
3288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3289 " arguments, found " << sig_registers;
3290 return nullptr;
3291 }
3292 return res_method;
3293}
3294
3295void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3296 MethodType method_type,
3297 bool is_range) {
3298 // As the method may not have been resolved, make this static check against what we expect.
3299 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3300 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3301 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3302 DexFileParameterIterator it(*dex_file_,
3303 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3304 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3305 nullptr);
3306}
3307
3308class MethodParamListDescriptorIterator {
3309 public:
3310 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3311 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3312 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3313 }
3314
3315 bool HasNext() {
3316 return pos_ < params_size_;
3317 }
3318
3319 void Next() {
3320 ++pos_;
3321 }
3322
3323 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3324 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3325 }
3326
3327 private:
3328 mirror::ArtMethod* res_method_;
3329 size_t pos_;
3330 const DexFile::TypeList* params_;
3331 const size_t params_size_;
3332};
3333
Brian Carlstromea46f952013-07-30 01:26:50 -07003334mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003335 MethodType method_type,
3336 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003337 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003338 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3339 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003340 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003341
Brian Carlstromea46f952013-07-30 01:26:50 -07003342 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003344 // Check what we can statically.
3345 if (!have_pending_hard_failure_) {
3346 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3347 }
3348 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003349 }
3350
Ian Rogersd81871c2011-10-03 13:57:23 -07003351 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3352 // has a vtable entry for the target method.
3353 if (is_super) {
3354 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003355 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003356 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003357 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003358 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003359 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003360 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003361 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003362 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003363 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003364 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003365 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003366 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003367 << "." << res_method->GetName()
3368 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003369 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 }
3371 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003372
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003373 // Process the target method's signature. This signature may or may not
3374 MethodParamListDescriptorIterator it(res_method);
3375 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3376 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003377}
3378
Brian Carlstromea46f952013-07-30 01:26:50 -07003379mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003380 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003381 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3382 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003383 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003384 if (!actual_arg_type.HasClass()) {
3385 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003386 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003387 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003388 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003389 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003390 if (klass->IsInterface()) {
3391 // Derive Object.class from Class.class.getSuperclass().
3392 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003393 if (FailOrAbort(this, object_klass->IsObjectClass(),
3394 "Failed to find Object class in quickened invoke receiver",
3395 work_insn_idx_)) {
3396 return nullptr;
3397 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003398 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003399 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003400 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003401 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003402 if (FailOrAbort(this, dispatch_class->HasVTable(),
3403 "Receiver class has no vtable for quickened invoke at ",
3404 work_insn_idx_)) {
3405 return nullptr;
3406 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003407 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe7c038102014-10-27 20:08:46 -07003408 if (FailOrAbort(this, static_cast<int32_t>(vtable_index) < dispatch_class->GetVTableLength(),
3409 "Receiver class has not enough vtable slots for quickened invoke at ",
3410 work_insn_idx_)) {
3411 return nullptr;
3412 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003413 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Andreas Gampe7c038102014-10-27 20:08:46 -07003414 if (FailOrAbort(this, !Thread::Current()->IsExceptionPending(),
3415 "Unexpected exception pending for quickened invoke at ",
3416 work_insn_idx_)) {
3417 return nullptr;
3418 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003419 return res_method;
3420}
3421
Brian Carlstromea46f952013-07-30 01:26:50 -07003422mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003423 bool is_range) {
3424 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003425 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003426 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003427 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003429 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003430 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003431 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3432 work_insn_idx_)) {
3433 return nullptr;
3434 }
3435 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3436 work_insn_idx_)) {
3437 return nullptr;
3438 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003439
3440 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3441 // match the call to the signature. Also, we might be calling through an abstract method
3442 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003443 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003444 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003445 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003446 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003447 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3448 /* caught by static verifier */
3449 DCHECK(is_range || expected_args <= 5);
3450 if (expected_args > code_item_->outs_size_) {
3451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3452 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003453 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003454 }
3455
3456 /*
3457 * Check the "this" argument, which must be an instance of the class that declared the method.
3458 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3459 * rigorous check here (which is okay since we have to do it at runtime).
3460 */
3461 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003463 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003464 }
3465 if (!actual_arg_type.IsZero()) {
3466 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003467 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003468 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003469 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003470 klass->CannotBeAssignedFromOtherTypes());
3471 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003472 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3473 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003474 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003475 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003476 }
3477 }
3478 /*
3479 * Process the target method's signature. This signature may or may not
3480 * have been verified, so we can't assume it's properly formed.
3481 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003482 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003483 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003484 uint32_t arg[5];
3485 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003486 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003487 }
3488 size_t actual_args = 1;
3489 for (size_t param_index = 0; param_index < params_size; param_index++) {
3490 if (actual_args >= expected_args) {
3491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003492 << "'. Expected " << expected_args
3493 << " arguments, processing argument " << actual_args
3494 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003495 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003496 }
3497 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003498 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003499 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003501 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003502 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003503 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003504 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003505 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003506 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003507 return res_method;
3508 }
3509 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3510 }
3511 if (actual_args != expected_args) {
3512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3513 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003514 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003515 } else {
3516 return res_method;
3517 }
3518}
3519
Ian Rogers62342ec2013-06-11 10:26:37 -07003520void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003521 uint32_t type_idx;
3522 if (!is_filled) {
3523 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3524 type_idx = inst->VRegC_22c();
3525 } else if (!is_range) {
3526 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3527 type_idx = inst->VRegB_35c();
3528 } else {
3529 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3530 type_idx = inst->VRegB_3rc();
3531 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003532 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003533 if (res_type.IsConflict()) { // bad class
3534 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003535 } else {
3536 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3537 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003539 } else if (!is_filled) {
3540 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003541 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003542 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003543 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003544 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003545 } else {
3546 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3547 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003548 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003549 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3550 uint32_t arg[5];
3551 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003552 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003553 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003554 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003555 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003556 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3557 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003558 return;
3559 }
3560 }
3561 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003562 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003563 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003564 }
3565 }
3566}
3567
Sebastien Hertz5243e912013-05-21 10:55:07 +02003568void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003569 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003570 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003574 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003575 if (array_type.IsZero()) {
3576 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3577 // instruction type. TODO: have a proper notion of bottom here.
3578 if (!is_primitive || insn_type.IsCategory1Types()) {
3579 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003580 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003582 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003583 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3584 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003585 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003586 }
jeffhaofc3144e2012-02-01 17:21:15 -08003587 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003589 } else {
3590 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003591 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003592 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003594 << " source for aget-object";
3595 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003596 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003597 << " source for category 1 aget";
3598 } else if (is_primitive && !insn_type.Equals(component_type) &&
3599 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003600 (insn_type.IsLong() && component_type.IsDouble()))) {
3601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3602 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003603 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003604 // Use knowledge of the field type which is stronger than the type inferred from the
3605 // instruction, which can't differentiate object types and ints from floats, longs from
3606 // doubles.
3607 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003609 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003610 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003611 component_type.HighHalf(&reg_types_));
3612 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 }
3614 }
3615 }
3616}
3617
Ian Rogersd8f69b02014-09-10 21:43:52 +00003618void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003619 const uint32_t vregA) {
3620 // Primitive assignability rules are weaker than regular assignability rules.
3621 bool instruction_compatible;
3622 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003623 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003624 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003625 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003626 value_compatible = value_type.IsIntegralTypes();
3627 } else if (target_type.IsFloat()) {
3628 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3629 value_compatible = value_type.IsFloatTypes();
3630 } else if (target_type.IsLong()) {
3631 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003632 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3633 // as target_type depends on the resolved type of the field.
3634 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003635 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003636 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3637 } else {
3638 value_compatible = false;
3639 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003640 } else if (target_type.IsDouble()) {
3641 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003642 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3643 // as target_type depends on the resolved type of the field.
3644 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003645 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003646 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3647 } else {
3648 value_compatible = false;
3649 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003650 } else {
3651 instruction_compatible = false; // reference with primitive store
3652 value_compatible = false; // unused
3653 }
3654 if (!instruction_compatible) {
3655 // This is a global failure rather than a class change failure as the instructions and
3656 // the descriptors for the type should have been consistent within the same file at
3657 // compile time.
3658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3659 << "' but expected type '" << target_type << "'";
3660 return;
3661 }
3662 if (!value_compatible) {
3663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3664 << " of type " << value_type << " but expected " << target_type << " for put";
3665 return;
3666 }
3667}
3668
Sebastien Hertz5243e912013-05-21 10:55:07 +02003669void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003670 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003671 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003674 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003675 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003676 if (array_type.IsZero()) {
3677 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3678 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003679 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003681 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003682 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003683 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003684 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003685 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003686 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003687 if (!component_type.IsReferenceTypes()) {
3688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3689 << " source for aput-object";
3690 } else {
3691 // The instruction agrees with the type of array, confirm the value to be stored does too
3692 // Note: we use the instruction type (rather than the component type) for aput-object as
3693 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003694 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003695 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003696 }
3697 }
3698 }
3699}
3700
Brian Carlstromea46f952013-07-30 01:26:50 -07003701mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003702 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3703 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003704 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003705 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003706 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3707 field_idx, dex_file_->GetFieldName(field_id),
3708 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003709 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003710 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003711 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003712 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003713 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003714 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003715 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3716 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003717 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003718 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003719 << dex_file_->GetFieldName(field_id) << ") in "
3720 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003721 DCHECK(self_->IsExceptionPending());
3722 self_->ClearException();
3723 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003724 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3725 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003726 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003727 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003728 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003729 } else if (!field->IsStatic()) {
3730 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003731 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003733 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003734}
3735
Ian Rogersd8f69b02014-09-10 21:43:52 +00003736mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003737 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3738 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003739 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003740 if (klass_type.IsConflict()) {
3741 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3742 field_idx, dex_file_->GetFieldName(field_id),
3743 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003744 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003745 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003746 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003747 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003748 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003749 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003750 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3751 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003752 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003753 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003754 << dex_file_->GetFieldName(field_id) << ") in "
3755 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003756 DCHECK(self_->IsExceptionPending());
3757 self_->ClearException();
3758 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003759 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3760 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003762 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003763 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 } else if (field->IsStatic()) {
3765 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3766 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003767 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 } else if (obj_type.IsZero()) {
3769 // Cannot infer and check type, however, access will cause null pointer exception
3770 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003771 } else if (!obj_type.IsReferenceTypes()) {
3772 // Trying to read a field from something that isn't a reference
3773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3774 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003776 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003777 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003778 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003779 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003780 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003781 if (obj_type.IsUninitializedTypes() &&
3782 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3783 !field_klass.Equals(GetDeclaringClass()))) {
3784 // Field accesses through uninitialized references are only allowable for constructors where
3785 // the field is declared in this class
3786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003787 << " of a not fully initialized object within the context"
3788 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003789 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003790 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3791 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3792 // of C1. For resolution to occur the declared class of the field must be compatible with
3793 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3794 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3795 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003796 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003797 } else {
3798 return field;
3799 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003800 }
3801}
3802
Ian Rogersd8f69b02014-09-10 21:43:52 +00003803void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003804 bool is_primitive, bool is_static) {
3805 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003806 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003807 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003808 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003809 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003810 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003811 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003812 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003813 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003814 if (field != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003815 mirror::Class* field_type_class;
3816 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003817 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003818 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3819 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3820 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003821 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003822 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003823 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003824 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003825 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3826 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003827 }
Ian Rogers0d604842012-04-16 14:50:24 -07003828 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003829 if (field_type == nullptr) {
3830 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3831 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003832 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003833 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003834 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003835 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003836 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003837 if (field_type->Equals(insn_type) ||
3838 (field_type->IsFloat() && insn_type.IsInteger()) ||
3839 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003840 // expected that read is of the correct primitive type or that int reads are reading
3841 // floats or long reads are reading doubles
3842 } else {
3843 // This is a global failure rather than a class change failure as the instructions and
3844 // the descriptors for the type should have been consistent within the same file at
3845 // compile time
3846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3847 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003848 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003849 return;
3850 }
3851 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003852 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003853 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3854 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003855 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003856 << "' in Get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003857 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003858 return;
3859 }
3860 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003861 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003862 work_line_->SetRegisterType(this, vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003863 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003864 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003865 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003866}
3867
Ian Rogersd8f69b02014-09-10 21:43:52 +00003868void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003869 bool is_primitive, bool is_static) {
3870 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003871 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003872 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003873 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003874 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003875 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003876 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003877 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003878 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003879 if (field != nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003880 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3881 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3882 << " from other class " << GetDeclaringClass();
3883 return;
3884 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003885 mirror::Class* field_type_class;
3886 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003888 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3889 FieldHelper fh(h_field);
3890 field_type_class = fh.GetType(can_load_classes_);
3891 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003892 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003893 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003894 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003895 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003896 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3897 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003898 }
3899 }
3900 if (field_type == nullptr) {
3901 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3902 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003903 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003904 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003905 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003906 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003907 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003908 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003909 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003910 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003911 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3912 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003913 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003914 << "' in put-object";
3915 return;
3916 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003917 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003918 }
3919}
3920
Brian Carlstromea46f952013-07-30 01:26:50 -07003921mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003922 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003923 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3924 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3925 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3926 inst->Opcode() == Instruction::IPUT_QUICK ||
3927 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
Hiroshi Yamauchi5f09be92014-09-26 10:43:59 -07003928 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3929 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3930 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3931 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3932 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003933 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003934 if (!object_type.HasClass()) {
3935 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3936 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003937 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003938 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003939 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3940 field_offset);
3941 if (f == nullptr) {
3942 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3943 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3944 }
3945 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003946}
3947
Ian Rogersd8f69b02014-09-10 21:43:52 +00003948void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003949 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003950 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003951 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003952 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3954 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003955 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003956 mirror::Class* field_type_class;
3957 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003958 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003959 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3960 FieldHelper fh(h_field);
3961 field_type_class = fh.GetType(can_load_classes_);
3962 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003963 const RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003964 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003965 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003966 field_type_class->CannotBeAssignedFromOtherTypes());
3967 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003968 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3969 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003970 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003971 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003972 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003973 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003974 const uint32_t vregA = inst->VRegA_22c();
3975 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003976 if (field_type->Equals(insn_type) ||
3977 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3978 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003979 // expected that read is of the correct primitive type or that int reads are reading
3980 // floats or long reads are reading doubles
3981 } else {
3982 // This is a global failure rather than a class change failure as the instructions and
3983 // the descriptors for the type should have been consistent within the same file at
3984 // compile time
3985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003986 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003987 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003988 return;
3989 }
3990 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003991 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003992 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003993 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003994 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003995 << "' in get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003996 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003997 return;
3998 }
3999 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004000 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004001 work_line_->SetRegisterType(this, vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004002 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004003 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004004 }
4005}
4006
Ian Rogersd8f69b02014-09-10 21:43:52 +00004007void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004008 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004009 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07004010 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004011 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4013 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004014 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07004015 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004016 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004017 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004018 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004019 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4020 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004021 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004022 return;
4023 }
4024 }
4025 const uint32_t vregA = inst->VRegA_22c();
4026 if (is_primitive) {
4027 // Primitive field assignability rules are weaker than regular assignability rules
4028 bool instruction_compatible;
4029 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004030 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004031 if (field_type.IsIntegralTypes()) {
4032 instruction_compatible = insn_type.IsIntegralTypes();
4033 value_compatible = value_type.IsIntegralTypes();
4034 } else if (field_type.IsFloat()) {
4035 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4036 value_compatible = value_type.IsFloatTypes();
4037 } else if (field_type.IsLong()) {
4038 instruction_compatible = insn_type.IsLong();
4039 value_compatible = value_type.IsLongTypes();
4040 } else if (field_type.IsDouble()) {
4041 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4042 value_compatible = value_type.IsDoubleTypes();
4043 } else {
4044 instruction_compatible = false; // reference field with primitive store
4045 value_compatible = false; // unused
4046 }
4047 if (!instruction_compatible) {
4048 // This is a global failure rather than a class change failure as the instructions and
4049 // the descriptors for the type should have been consistent within the same file at
4050 // compile time
4051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004052 << " to be of type '" << insn_type
4053 << "' but found type '" << field_type
4054 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004055 return;
4056 }
4057 if (!value_compatible) {
4058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4059 << " of type " << value_type
4060 << " but expected " << field_type
4061 << " for store to " << PrettyField(field) << " in put";
4062 return;
4063 }
4064 } else {
4065 if (!insn_type.IsAssignableFrom(field_type)) {
4066 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004067 << " to be compatible with type '" << insn_type
4068 << "' but found type '" << field_type
4069 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004070 return;
4071 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004072 work_line_->VerifyRegisterType(this, vregA, field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004073 }
4074}
4075
Ian Rogers776ac1f2012-04-13 23:36:36 -07004076bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004077 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004079 return false;
4080 }
4081 return true;
4082}
4083
Stephen Kyle9bc61992014-09-22 13:53:15 +01004084bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4085 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4086 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4088 return false;
4089 }
4090 return true;
4091}
4092
4093bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4094 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4095}
4096
Ian Rogersebbdd872014-07-07 23:53:08 -07004097bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4098 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004099 bool changed = true;
4100 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4101 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004102 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004103 * We haven't processed this instruction before, and we haven't touched the registers here, so
4104 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4105 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004106 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004107 if (!insn_flags_[next_insn].IsReturn()) {
4108 target_line->CopyFromLine(merge_line);
4109 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004110 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004111 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004112 return false;
4113 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004114 // For returns we only care about the operand to the return, all other registers are dead.
4115 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4116 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4117 Instruction::Code opcode = ret_inst->Opcode();
4118 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004119 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004120 } else {
4121 target_line->CopyFromLine(merge_line);
4122 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004123 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004124 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004125 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004126 }
4127 }
4128 }
jeffhaobdb76512011-09-07 11:43:16 -07004129 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004130 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004131 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004132 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004133 if (gDebugVerify) {
4134 copy->CopyFromLine(target_line);
4135 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004136 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004137 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004138 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004139 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004140 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004141 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004142 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004143 << copy->Dump(this) << " MERGE\n"
4144 << merge_line->Dump(this) << " ==\n"
4145 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004146 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004147 if (update_merge_line && changed) {
4148 merge_line->CopyFromLine(target_line);
4149 }
jeffhaobdb76512011-09-07 11:43:16 -07004150 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004151 if (changed) {
4152 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004153 }
4154 return true;
4155}
4156
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004157InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004158 return &insn_flags_[work_insn_idx_];
4159}
4160
Ian Rogersd8f69b02014-09-10 21:43:52 +00004161const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004162 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004163 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004164 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004165 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004166 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4167 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004168 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004169 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004170 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4171 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004172 }
4173 }
4174 if (return_type_ == nullptr) {
4175 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4176 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4177 uint16_t return_type_idx = proto_id.return_type_idx_;
4178 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004179 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004180 }
4181 }
4182 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004183}
4184
Ian Rogersd8f69b02014-09-10 21:43:52 +00004185const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004186 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004187 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004188 const char* descriptor
4189 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004190 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004191 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004192 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4193 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004194 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004195 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004196 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004197 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004198 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004199}
4200
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004201std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4202 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004203 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004204 std::vector<int32_t> result;
4205 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004206 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004207 if (type.IsConstant()) {
4208 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004209 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4210 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004211 } else if (type.IsConstantLo()) {
4212 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004213 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4214 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004215 } else if (type.IsConstantHi()) {
4216 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004217 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4218 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004219 } else if (type.IsIntegralTypes()) {
4220 result.push_back(kIntVReg);
4221 result.push_back(0);
4222 } else if (type.IsFloat()) {
4223 result.push_back(kFloatVReg);
4224 result.push_back(0);
4225 } else if (type.IsLong()) {
4226 result.push_back(kLongLoVReg);
4227 result.push_back(0);
4228 result.push_back(kLongHiVReg);
4229 result.push_back(0);
4230 ++i;
4231 } else if (type.IsDouble()) {
4232 result.push_back(kDoubleLoVReg);
4233 result.push_back(0);
4234 result.push_back(kDoubleHiVReg);
4235 result.push_back(0);
4236 ++i;
4237 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4238 result.push_back(kUndefined);
4239 result.push_back(0);
4240 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004241 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004242 result.push_back(kReferenceVReg);
4243 result.push_back(0);
4244 }
4245 }
4246 return result;
4247}
4248
Ian Rogersd8f69b02014-09-10 21:43:52 +00004249const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004250 if (precise) {
4251 // Precise constant type.
4252 return reg_types_.FromCat1Const(value, true);
4253 } else {
4254 // Imprecise constant type.
4255 if (value < -32768) {
4256 return reg_types_.IntConstant();
4257 } else if (value < -128) {
4258 return reg_types_.ShortConstant();
4259 } else if (value < 0) {
4260 return reg_types_.ByteConstant();
4261 } else if (value == 0) {
4262 return reg_types_.Zero();
4263 } else if (value == 1) {
4264 return reg_types_.One();
4265 } else if (value < 128) {
4266 return reg_types_.PosByteConstant();
4267 } else if (value < 32768) {
4268 return reg_types_.PosShortConstant();
4269 } else if (value < 65536) {
4270 return reg_types_.CharConstant();
4271 } else {
4272 return reg_types_.IntConstant();
4273 }
4274 }
4275}
4276
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004277void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004278 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004279}
4280
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004281void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004282 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004283}
jeffhaod1224c72012-02-29 13:43:08 -08004284
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004285void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4286 RegTypeCache::VisitStaticRoots(callback, arg);
4287}
4288
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004289void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4290 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004291}
4292
Ian Rogersd81871c2011-10-03 13:57:23 -07004293} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004294} // namespace art