blob: 474a066da8cb62ebf6400229346369a662438c0c [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 Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_field-inl.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070039#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070042#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080044#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
46namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070047namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070049static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070050static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070051// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070052
Ian Rogers7b3ddd22013-02-21 15:19:52 -080053void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070054 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070056 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070057 register_lines_.reset(new RegisterLine*[insns_size]());
58 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070059 for (uint32_t i = 0; i < insns_size; i++) {
60 bool interesting = false;
61 switch (mode) {
62 case kTrackRegsAll:
63 interesting = flags[i].IsOpcode();
64 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070065 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070066 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070067 break;
68 case kTrackRegsBranches:
69 interesting = flags[i].IsBranchTarget();
70 break;
71 default:
72 break;
73 }
74 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070075 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
76 }
77 }
78}
79
80PcToRegisterLineTable::~PcToRegisterLineTable() {
81 for (size_t i = 0; i < size_; i++) {
82 delete register_lines_[i];
83 if (kIsDebugBuild) {
84 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070085 }
86 }
87}
88
Andreas Gampe7c038102014-10-27 20:08:46 -070089// Note: returns true on failure.
90ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
91 const char* error_msg, uint32_t work_insn_idx) {
92 if (kIsDebugBuild) {
93 // In a debug build, abort if the error condition is wrong.
94 DCHECK(condition) << error_msg << work_insn_idx;
95 } else {
96 // In a non-debug build, just fail the class.
97 if (!condition) {
98 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
99 return true;
100 }
101 }
102
103 return false;
104}
105
Stephen Kyle7e541c92014-12-17 17:10:02 +0000106static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
107 if (verifier->IsConstructor()) {
108 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
109 reg_line->CheckConstructorReturn(verifier);
110 }
111 reg_line->MarkAllRegistersAsConflicts(verifier);
112}
113
Ian Rogers7b078e82014-09-10 14:44:24 -0700114MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
115 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700116 bool allow_soft_failures,
117 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700118 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700119 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700120 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800121 bool early_failure = false;
122 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700123 const DexFile& dex_file = klass->GetDexFile();
124 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800125 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700126 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700127 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800128 early_failure = true;
129 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700130 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800131 early_failure = true;
132 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700133 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800134 early_failure = true;
135 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
136 }
137 if (early_failure) {
138 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000139 if (Runtime::Current()->IsCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000141 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800142 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700143 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700144 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700146 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700149}
150
Ian Rogers7b078e82014-09-10 14:44:24 -0700151MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
152 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700153 Handle<mirror::DexCache> dex_cache,
154 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700155 const DexFile::ClassDef* class_def,
156 bool allow_soft_failures,
157 std::string* error) {
158 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700159 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700161 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700162 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700163 }
jeffhaof56197c2012-03-05 18:01:54 -0800164 ClassDataItemIterator it(*dex_file, class_data);
165 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
166 it.Next();
167 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700168 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700169 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700170 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700171 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800172 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700173 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800174 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700175 if (method_idx == previous_direct_method_idx) {
176 // smali can create dex files with two encoded_methods sharing the same method_idx
177 // http://code.google.com/p/smali/issues/detail?id=119
178 it.Next();
179 continue;
180 }
181 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700182 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700183 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700184 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
185 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700186 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700187 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700189 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700191 StackHandleScope<1> hs(self);
192 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700193 MethodVerifier::FailureKind result = VerifyMethod(self,
194 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700195 dex_file,
196 dex_cache,
197 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700198 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700199 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700200 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700201 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700202 allow_soft_failures,
203 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700204 if (result != kNoFailure) {
205 if (result == kHardFailure) {
206 hard_fail = true;
207 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700208 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700209 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700210 *error = "Verifier rejected class ";
211 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
212 *error += " due to bad method ";
213 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700214 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800216 }
217 it.Next();
218 }
jeffhao9b0b1882012-10-01 16:51:22 -0700219 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800220 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700221 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800222 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700223 if (method_idx == previous_virtual_method_idx) {
224 // smali can create dex files with two encoded_methods sharing the same method_idx
225 // http://code.google.com/p/smali/issues/detail?id=119
226 it.Next();
227 continue;
228 }
229 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700230 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700231 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700232 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
233 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700234 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700235 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700236 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700237 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700238 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700239 StackHandleScope<1> hs(self);
240 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700241 MethodVerifier::FailureKind result = VerifyMethod(self,
242 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700243 dex_file,
244 dex_cache,
245 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700246 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700247 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700248 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700249 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700250 allow_soft_failures,
251 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700252 if (result != kNoFailure) {
253 if (result == kHardFailure) {
254 hard_fail = true;
255 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700256 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700257 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700258 *error = "Verifier rejected class ";
259 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
260 *error += " due to bad method ";
261 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700263 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800264 }
265 it.Next();
266 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 if (error_count == 0) {
268 return kNoFailure;
269 } else {
270 return hard_fail ? kHardFailure : kSoftFailure;
271 }
jeffhaof56197c2012-03-05 18:01:54 -0800272}
273
Ian Rogers7b078e82014-09-10 14:44:24 -0700274MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700276 Handle<mirror::DexCache> dex_cache,
277 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700278 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700280 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700281 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700282 bool allow_soft_failures,
283 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700284 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700285 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700286
Ian Rogers7b078e82014-09-10 14:44:24 -0700287 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700288 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800289 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700290 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700291 // Verification completed, however failures may be pending that didn't cause the verification
292 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700293 CHECK(!verifier.have_pending_hard_failure_);
294 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700295 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700296 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700297 << PrettyMethod(method_idx, *dex_file) << "\n");
298 }
Ian Rogersc8982582012-09-07 16:53:25 -0700299 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800300 }
301 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700302 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700303 CHECK_NE(verifier.failures_.size(), 0U);
304 CHECK(verifier.have_pending_hard_failure_);
305 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700306 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800307 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700308 std::cout << "\n" << verifier.info_messages_.str();
309 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800310 }
Ian Rogersc8982582012-09-07 16:53:25 -0700311 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800312 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700313 if (kTimeVerifyMethod) {
314 uint64_t duration_ns = NanoTime() - start_ns;
315 if (duration_ns > MsToNs(100)) {
316 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
317 << " took " << PrettyDuration(duration_ns);
318 }
Ian Rogersc8982582012-09-07 16:53:25 -0700319 }
320 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800321}
322
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700323MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700324 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700325 Handle<mirror::DexCache> dex_cache,
326 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700327 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700329 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800330 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700331 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
332 class_def, code_item, dex_method_idx, method,
333 method_access_flags, true, true, true, true);
334 verifier->Verify();
335 verifier->DumpFailures(os);
336 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700337 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
338 // and querying any info is dangerous/can abort.
339 if (verifier->have_pending_hard_failure_) {
340 delete verifier;
341 return nullptr;
342 } else {
343 verifier->Dump(os);
344 return verifier;
345 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800346}
347
Ian Rogers7b078e82014-09-10 14:44:24 -0700348MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700349 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
350 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700351 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700352 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700353 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700354 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800355 bool need_precise_constants, bool verify_to_dump,
356 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700357 : self_(self),
358 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800359 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800360 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700361 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700362 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700363 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800364 dex_file_(dex_file),
365 dex_cache_(dex_cache),
366 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700367 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800368 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700369 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700370 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700371 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700372 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700373 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800374 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800375 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700376 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200377 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700378 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200379 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700380 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800381 verify_to_dump_(verify_to_dump),
382 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800383 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700384 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800385}
386
Mathieu Chartier590fee92013-09-13 13:46:47 -0700387MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800388 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389 STLDeleteElements(&failure_messages_);
390}
391
Brian Carlstromea46f952013-07-30 01:26:50 -0700392void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700394 Thread* self = Thread::Current();
395 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700396 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
397 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700398 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700399 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700400 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800401 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700402 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700403 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700404 verifier.FindLocksAtDexPc();
405}
406
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700407static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
408 const Instruction* inst = Instruction::At(code_item->insns_);
409
410 uint32_t insns_size = code_item->insns_size_in_code_units_;
411 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
412 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
413 return true;
414 }
415
416 dex_pc += inst->SizeInCodeUnits();
417 inst = inst->Next();
418 }
419
420 return false;
421}
422
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700423void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700424 CHECK(monitor_enter_dex_pcs_ != nullptr);
425 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700426
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700427 // Quick check whether there are any monitor_enter instructions at all.
428 if (!HasMonitorEnterInstructions(code_item_)) {
429 return;
430 }
431
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700432 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
433 // verification. In practice, the phase we want relies on data structures set up by all the
434 // earlier passes, so we just run the full method verification and bail out early when we've
435 // got what we wanted.
436 Verify();
437}
438
Brian Carlstromea46f952013-07-30 01:26:50 -0700439mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700440 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700441 Thread* self = Thread::Current();
442 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700443 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
444 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700445 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700446 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700447 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800448 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200449 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200450}
451
Brian Carlstromea46f952013-07-30 01:26:50 -0700452mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700453 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200454
455 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
456 // verification. In practice, the phase we want relies on data structures set up by all the
457 // earlier passes, so we just run the full method verification and bail out early when we've
458 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200459 bool success = Verify();
460 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700461 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200462 }
463 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700464 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700465 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200466 }
467 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
468 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200469}
470
Brian Carlstromea46f952013-07-30 01:26:50 -0700471mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700472 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700473 Thread* self = Thread::Current();
474 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700475 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
476 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700477 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700478 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700479 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800480 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200481 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200482}
483
Brian Carlstromea46f952013-07-30 01:26:50 -0700484mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700485 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200486
487 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
488 // verification. In practice, the phase we want relies on data structures set up by all the
489 // earlier passes, so we just run the full method verification and bail out early when we've
490 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200491 bool success = Verify();
492 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700493 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200494 }
495 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700496 if (register_line == nullptr) {
497 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200498 }
499 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
500 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
501 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200502}
503
Ian Rogersad0b3a32012-04-16 14:50:24 -0700504bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700506 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700507 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700509 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 } else {
511 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700512 }
jeffhaobdb76512011-09-07 11:43:16 -0700513 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700514 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
515 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
517 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700518 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700519 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800521 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 // Run through the instructions and see if the width checks out.
523 bool result = ComputeWidthsAndCountOps();
524 // Flag instructions guarded by a "try" block and check exception handlers.
525 result = result && ScanTryCatchBlocks();
526 // Perform static instruction verification.
527 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700528 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000529 result = result && VerifyCodeFlow();
530 // Compute information for compiler.
531 if (result && Runtime::Current()->IsCompiler()) {
532 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
533 }
534 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700535}
536
Ian Rogers776ac1f2012-04-13 23:36:36 -0700537std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700538 switch (error) {
539 case VERIFY_ERROR_NO_CLASS:
540 case VERIFY_ERROR_NO_FIELD:
541 case VERIFY_ERROR_NO_METHOD:
542 case VERIFY_ERROR_ACCESS_CLASS:
543 case VERIFY_ERROR_ACCESS_FIELD:
544 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700545 case VERIFY_ERROR_INSTANTIATION:
546 case VERIFY_ERROR_CLASS_CHANGE:
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000547 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700548 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
549 // class change and instantiation errors into soft verification errors so that we re-verify
550 // at runtime. We may fail to find or to agree on access because of not yet available class
551 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
552 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
553 // paths" that dynamically perform the verification and cause the behavior to be that akin
554 // to an interpreter.
555 error = VERIFY_ERROR_BAD_CLASS_SOFT;
556 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700557 // If we fail again at runtime, mark that this instruction would throw and force this
558 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700559 have_pending_runtime_throw_failure_ = true;
560 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700562 // Indication that verification should be retried at runtime.
563 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700564 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700565 have_pending_hard_failure_ = true;
566 }
567 break;
jeffhaod5347e02012-03-22 17:25:05 -0700568 // Hard verification failures at compile time will still fail at runtime, so the class is
569 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700570 case VERIFY_ERROR_BAD_CLASS_HARD: {
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000571 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700572 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000573 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800574 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700575 have_pending_hard_failure_ = true;
576 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800577 }
578 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700579 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700580 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700581 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700582 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700583 failure_messages_.push_back(failure_message);
584 return *failure_message;
585}
586
Ian Rogers576ca0c2014-06-06 15:58:22 -0700587std::ostream& MethodVerifier::LogVerifyInfo() {
588 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
589 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
590}
591
Ian Rogersad0b3a32012-04-16 14:50:24 -0700592void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
593 size_t failure_num = failure_messages_.size();
594 DCHECK_NE(failure_num, 0U);
595 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
596 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700597 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700598 delete last_fail_message;
599}
600
601void MethodVerifier::AppendToLastFailMessage(std::string append) {
602 size_t failure_num = failure_messages_.size();
603 DCHECK_NE(failure_num, 0U);
604 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
605 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800606}
607
Ian Rogers776ac1f2012-04-13 23:36:36 -0700608bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 const uint16_t* insns = code_item_->insns_;
610 size_t insns_size = code_item_->insns_size_in_code_units_;
611 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700612 size_t new_instance_count = 0;
613 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700615
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700617 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700618 switch (opcode) {
619 case Instruction::APUT_OBJECT:
620 case Instruction::CHECK_CAST:
621 has_check_casts_ = true;
622 break;
623 case Instruction::INVOKE_VIRTUAL:
624 case Instruction::INVOKE_VIRTUAL_RANGE:
625 case Instruction::INVOKE_INTERFACE:
626 case Instruction::INVOKE_INTERFACE_RANGE:
627 has_virtual_or_interface_invokes_ = true;
628 break;
629 case Instruction::MONITOR_ENTER:
630 monitor_enter_count++;
631 break;
632 case Instruction::NEW_INSTANCE:
633 new_instance_count++;
634 break;
635 default:
636 break;
jeffhaobdb76512011-09-07 11:43:16 -0700637 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700639 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700641 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700642 }
643
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
646 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700647 return false;
648 }
649
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 new_instance_count_ = new_instance_count;
651 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700652 return true;
653}
654
Ian Rogers776ac1f2012-04-13 23:36:36 -0700655bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700657 if (tries_size == 0) {
658 return true;
659 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700661 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700662
663 for (uint32_t idx = 0; idx < tries_size; idx++) {
664 const DexFile::TryItem* try_item = &tries[idx];
665 uint32_t start = try_item->start_addr_;
666 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700667 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
669 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700670 return false;
671 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700673 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
674 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700675 return false;
676 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700677 uint32_t dex_pc = start;
678 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
679 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700681 size_t insn_size = inst->SizeInCodeUnits();
682 dex_pc += insn_size;
683 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700684 }
685 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800686 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700687 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700688 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700689 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700690 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700691 CatchHandlerIterator iterator(handlers_ptr);
692 for (; iterator.HasNext(); iterator.Next()) {
693 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700695 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
696 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700697 return false;
698 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100699 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
700 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
701 << "exception handler begins with move-result* (" << dex_pc << ")";
702 return false;
703 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700705 // Ensure exception types are resolved so that they don't need resolution to be delivered,
706 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700707 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800708 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
709 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700710 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700711 if (exception_type == nullptr) {
712 DCHECK(self_->IsExceptionPending());
713 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700714 }
715 }
jeffhaobdb76512011-09-07 11:43:16 -0700716 }
Ian Rogers0571d352011-11-03 19:51:38 -0700717 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700718 }
jeffhaobdb76512011-09-07 11:43:16 -0700719 return true;
720}
721
Ian Rogers776ac1f2012-04-13 23:36:36 -0700722bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700724
Ian Rogers0c7abda2012-09-19 13:33:42 -0700725 /* 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 -0700726 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700727 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700728
729 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700730 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700732 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 return false;
734 }
735 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700736 // All invoke points are marked as "Throw" points already.
737 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000738 if (inst->IsBranch()) {
739 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
740 // The compiler also needs safepoints for fall-through to loop heads.
741 // Such a loop head must be a target of a branch.
742 int32_t offset = 0;
743 bool cond, self_ok;
744 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
745 DCHECK(target_ok);
746 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
747 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700748 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000749 } else if (inst->IsReturn()) {
750 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 }
752 dex_pc += inst->SizeInCodeUnits();
753 inst = inst->Next();
754 }
755 return true;
756}
757
Ian Rogers776ac1f2012-04-13 23:36:36 -0700758bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 bool result = true;
760 switch (inst->GetVerifyTypeArgumentA()) {
761 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700762 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 break;
764 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700765 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 break;
767 }
768 switch (inst->GetVerifyTypeArgumentB()) {
769 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700770 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 break;
772 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700773 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 break;
775 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700776 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 break;
778 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700779 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 break;
781 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700782 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 break;
784 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700785 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 break;
787 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700788 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 break;
790 }
791 switch (inst->GetVerifyTypeArgumentC()) {
792 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700793 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 break;
795 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700796 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 break;
798 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700799 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 break;
801 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700802 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 break;
804 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700805 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 break;
807 }
808 switch (inst->GetVerifyExtraFlags()) {
809 case Instruction::kVerifyArrayData:
810 result = result && CheckArrayData(code_offset);
811 break;
812 case Instruction::kVerifyBranchTarget:
813 result = result && CheckBranchTarget(code_offset);
814 break;
815 case Instruction::kVerifySwitchTargets:
816 result = result && CheckSwitchTargets(code_offset);
817 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700818 case Instruction::kVerifyVarArgNonZero:
819 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700820 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700821 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
823 "non-range invoke";
824 return false;
825 }
Ian Rogers29a26482014-05-02 15:27:29 -0700826 uint32_t args[Instruction::kMaxVarArgRegs];
827 inst->GetVarArgs(args);
828 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700830 }
Andreas Gampec3314312014-06-19 18:13:29 -0700831 case Instruction::kVerifyVarArgRangeNonZero:
832 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700834 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
835 inst->VRegA() <= 0) {
836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
837 "range invoke";
838 return false;
839 }
Ian Rogers29a26482014-05-02 15:27:29 -0700840 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 break;
842 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 result = false;
845 break;
846 }
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000847 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
849 result = false;
850 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 return result;
852}
853
Ian Rogers7b078e82014-09-10 14:44:24 -0700854inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
857 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 return false;
859 }
860 return true;
861}
862
Ian Rogers7b078e82014-09-10 14:44:24 -0700863inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
866 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 return false;
868 }
869 return true;
870}
871
Ian Rogers7b078e82014-09-10 14:44:24 -0700872inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
875 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 return false;
877 }
878 return true;
879}
880
Ian Rogers7b078e82014-09-10 14:44:24 -0700881inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
884 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 return false;
886 }
887 return true;
888}
889
Ian Rogers7b078e82014-09-10 14:44:24 -0700890inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
893 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 return false;
895 }
896 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700897 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 return false;
901 }
902 return true;
903}
904
Ian Rogers7b078e82014-09-10 14:44:24 -0700905inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
908 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 return false;
910 }
911 return true;
912}
913
Ian Rogers7b078e82014-09-10 14:44:24 -0700914inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
917 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 return false;
919 }
920 return true;
921}
922
Ian Rogers776ac1f2012-04-13 23:36:36 -0700923bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
926 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 return false;
928 }
929 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700930 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 const char* cp = descriptor;
932 while (*cp++ == '[') {
933 bracket_count++;
934 }
935 if (bracket_count == 0) {
936 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
938 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 } else if (bracket_count > 255) {
941 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
943 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
945 }
946 return true;
947}
948
Ian Rogers776ac1f2012-04-13 23:36:36 -0700949bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
951 const uint16_t* insns = code_item_->insns_ + cur_offset;
952 const uint16_t* array_data;
953 int32_t array_data_offset;
954
955 DCHECK_LT(cur_offset, insn_count);
956 /* make sure the start of the array data table is in range */
957 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
958 if ((int32_t) cur_offset + array_data_offset < 0 ||
959 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700961 << ", data offset " << array_data_offset
962 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 return false;
964 }
965 /* offset to array data table is a relative branch-style offset */
966 array_data = insns + array_data_offset;
967 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800968 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
970 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 return false;
972 }
973 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700974 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
976 /* make sure the end of the switch is in range */
977 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
979 << ", data offset " << array_data_offset << ", end "
980 << cur_offset + array_data_offset + table_size
981 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return false;
983 }
984 return true;
985}
986
Ian Rogers776ac1f2012-04-13 23:36:36 -0700987bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 int32_t offset;
989 bool isConditional, selfOkay;
990 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
991 return false;
992 }
993 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
995 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 return false;
997 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700998 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
999 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1002 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 return false;
1004 }
1005 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1006 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001007 if (abs_offset < 0 ||
1008 (uint32_t) abs_offset >= insn_count ||
1009 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001011 << reinterpret_cast<void*>(abs_offset) << ") at "
1012 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 return false;
1014 }
1015 insn_flags_[abs_offset].SetBranchTarget();
1016 return true;
1017}
1018
Ian Rogers776ac1f2012-04-13 23:36:36 -07001019bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 bool* selfOkay) {
1021 const uint16_t* insns = code_item_->insns_ + cur_offset;
1022 *pConditional = false;
1023 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001024 switch (*insns & 0xff) {
1025 case Instruction::GOTO:
1026 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001027 break;
1028 case Instruction::GOTO_32:
1029 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001030 *selfOkay = true;
1031 break;
1032 case Instruction::GOTO_16:
1033 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001034 break;
1035 case Instruction::IF_EQ:
1036 case Instruction::IF_NE:
1037 case Instruction::IF_LT:
1038 case Instruction::IF_GE:
1039 case Instruction::IF_GT:
1040 case Instruction::IF_LE:
1041 case Instruction::IF_EQZ:
1042 case Instruction::IF_NEZ:
1043 case Instruction::IF_LTZ:
1044 case Instruction::IF_GEZ:
1045 case Instruction::IF_GTZ:
1046 case Instruction::IF_LEZ:
1047 *pOffset = (int16_t) insns[1];
1048 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001049 break;
1050 default:
1051 return false;
1052 break;
1053 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001054 return true;
1055}
1056
Ian Rogers776ac1f2012-04-13 23:36:36 -07001057bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001059 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001061 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1063 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001065 << ", switch offset " << switch_offset
1066 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001067 return false;
1068 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001069 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001071 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001072 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1074 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001075 return false;
1076 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 uint32_t switch_count = switch_insns[1];
1078 int32_t keys_offset, targets_offset;
1079 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001080 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1081 /* 0=sig, 1=count, 2/3=firstKey */
1082 targets_offset = 4;
1083 keys_offset = -1;
1084 expected_signature = Instruction::kPackedSwitchSignature;
1085 } else {
1086 /* 0=sig, 1=count, 2..count*2 = keys */
1087 keys_offset = 2;
1088 targets_offset = 2 + 2 * switch_count;
1089 expected_signature = Instruction::kSparseSwitchSignature;
1090 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001092 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001093 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1094 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1095 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001096 return false;
1097 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001098 /* make sure the end of the switch is in range */
1099 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1101 << ", switch offset " << switch_offset
1102 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001103 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001104 return false;
1105 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001106 /* for a sparse switch, verify the keys are in ascending order */
1107 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1109 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001110 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1111 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1112 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1114 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001115 return false;
1116 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001117 last_key = key;
1118 }
1119 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001120 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 for (uint32_t targ = 0; targ < switch_count; targ++) {
1122 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1123 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1124 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001125 if (abs_offset < 0 ||
1126 abs_offset >= (int32_t) insn_count ||
1127 !insn_flags_[abs_offset].IsOpcode()) {
1128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1129 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1130 << reinterpret_cast<void*>(cur_offset)
1131 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 return false;
1133 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 insn_flags_[abs_offset].SetBranchTarget();
1135 }
1136 return true;
1137}
1138
Ian Rogers776ac1f2012-04-13 23:36:36 -07001139bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001140 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 return false;
1143 }
1144 uint16_t registers_size = code_item_->registers_size_;
1145 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001146 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1148 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 return false;
1150 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001151 }
1152
1153 return true;
1154}
1155
Ian Rogers776ac1f2012-04-13 23:36:36 -07001156bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 uint16_t registers_size = code_item_->registers_size_;
1158 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1159 // integer overflow when adding them here.
1160 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1162 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001163 return false;
1164 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001165 return true;
1166}
1167
Ian Rogers776ac1f2012-04-13 23:36:36 -07001168bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 uint16_t registers_size = code_item_->registers_size_;
1170 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001171
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001173 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1174 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 }
1176 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001177 reg_table_.Init(kTrackCompilerInterestPoints,
1178 insn_flags_.get(),
1179 insns_size,
1180 registers_size,
1181 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001182
jeffhaobdb76512011-09-07 11:43:16 -07001183
Ian Rogersd0fbd852013-09-24 18:17:04 -07001184 work_line_.reset(RegisterLine::Create(registers_size, this));
1185 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001186
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 /* Initialize register types of method arguments. */
1188 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001189 DCHECK_NE(failures_.size(), 0U);
1190 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001191 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001192 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 return false;
1194 }
1195 /* Perform code flow verification. */
1196 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001197 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001199 }
jeffhaobdb76512011-09-07 11:43:16 -07001200 return true;
1201}
1202
Ian Rogersad0b3a32012-04-16 14:50:24 -07001203std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1204 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001205 for (size_t i = 0; i < failures_.size(); ++i) {
1206 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001207 }
1208 return os;
1209}
1210
Ian Rogers776ac1f2012-04-13 23:36:36 -07001211void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001212 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001213 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 return;
jeffhaobdb76512011-09-07 11:43:16 -07001215 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001216 {
1217 os << "Register Types:\n";
1218 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1219 std::ostream indent_os(&indent_filter);
1220 reg_types_.Dump(indent_os);
1221 }
Ian Rogersb4903572012-10-11 11:52:56 -07001222 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001223 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1224 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 const Instruction* inst = Instruction::At(code_item_->insns_);
1226 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001227 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001229 if (reg_line != nullptr) {
1230 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001231 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001232 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001233 const bool kDumpHexOfInstruction = false;
1234 if (kDumpHexOfInstruction) {
1235 indent_os << inst->DumpHex(5) << " ";
1236 }
1237 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001238 inst = inst->Next();
1239 }
jeffhaobdb76512011-09-07 11:43:16 -07001240}
1241
Ian Rogersd81871c2011-10-03 13:57:23 -07001242static bool IsPrimitiveDescriptor(char descriptor) {
1243 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001244 case 'I':
1245 case 'C':
1246 case 'S':
1247 case 'B':
1248 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001249 case 'F':
1250 case 'D':
1251 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001253 default:
1254 return false;
1255 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001256}
1257
Ian Rogers776ac1f2012-04-13 23:36:36 -07001258bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 RegisterLine* reg_line = reg_table_.GetLine(0);
1260 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1261 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001262
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001264 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001266 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1268 // argument as uninitialized. This restricts field access until the superclass constructor is
1269 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001270 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001271 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001272 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 reg_types_.UninitializedThisArgument(declaring_class));
1274 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001275 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001276 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001278 }
1279
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001280 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001281 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001282 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001283
1284 for (; iterator.HasNext(); iterator.Next()) {
1285 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001286 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 LOG(FATAL) << "Null descriptor";
1288 }
1289 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1291 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 return false;
1293 }
1294 switch (descriptor[0]) {
1295 case 'L':
1296 case '[':
1297 // We assume that reference arguments are initialized. The only way it could be otherwise
1298 // (assuming the caller was verified) is if the current method is <init>, but in that case
1299 // it's effectively considered initialized the instant we reach here (in the sense that we
1300 // can return without doing anything or call virtual methods).
1301 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001302 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001303 if (!reg_type.IsNonZeroReferenceTypes()) {
1304 DCHECK(HasFailures());
1305 return false;
1306 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001307 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 }
1309 break;
1310 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001311 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 break;
1313 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001314 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 break;
1316 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001317 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 break;
1319 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001320 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 break;
1322 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001323 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 break;
1325 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001326 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 break;
1328 case 'J':
1329 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001330 if (cur_arg + 1 >= expected_args) {
1331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1332 << " args, found more (" << descriptor << ")";
1333 return false;
1334 }
1335
Ian Rogers7b078e82014-09-10 14:44:24 -07001336 const RegType* lo_half;
1337 const RegType* hi_half;
1338 if (descriptor[0] == 'J') {
1339 lo_half = &reg_types_.LongLo();
1340 hi_half = &reg_types_.LongHi();
1341 } else {
1342 lo_half = &reg_types_.DoubleLo();
1343 hi_half = &reg_types_.DoubleHi();
1344 }
1345 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 cur_arg++;
1347 break;
1348 }
1349 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1351 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 return false;
1353 }
1354 cur_arg++;
1355 }
1356 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1358 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 return false;
1360 }
1361 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1362 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1363 // format. Only major difference from the method argument format is that 'V' is supported.
1364 bool result;
1365 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1366 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001367 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 size_t i = 0;
1369 do {
1370 i++;
1371 } while (descriptor[i] == '['); // process leading [
1372 if (descriptor[i] == 'L') { // object array
1373 do {
1374 i++; // find closing ;
1375 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1376 result = descriptor[i] == ';';
1377 } else { // primitive array
1378 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1379 }
1380 } else if (descriptor[0] == 'L') {
1381 // could be more thorough here, but shouldn't be required
1382 size_t i = 0;
1383 do {
1384 i++;
1385 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1386 result = descriptor[i] == ';';
1387 } else {
1388 result = false;
1389 }
1390 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1392 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 }
1394 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001395}
1396
Ian Rogers776ac1f2012-04-13 23:36:36 -07001397bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 const uint16_t* insns = code_item_->insns_;
1399 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001400
jeffhaobdb76512011-09-07 11:43:16 -07001401 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 insn_flags_[0].SetChanged();
1403 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001404
jeffhaobdb76512011-09-07 11:43:16 -07001405 /* Continue until no instructions are marked "changed". */
1406 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001407 if (allow_thread_suspension_) {
1408 self_->AllowThreadSuspension();
1409 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1411 uint32_t insn_idx = start_guess;
1412 for (; insn_idx < insns_size; insn_idx++) {
1413 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001414 break;
1415 }
jeffhaobdb76512011-09-07 11:43:16 -07001416 if (insn_idx == insns_size) {
1417 if (start_guess != 0) {
1418 /* try again, starting from the top */
1419 start_guess = 0;
1420 continue;
1421 } else {
1422 /* all flags are clear */
1423 break;
1424 }
1425 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 // We carry the working set of registers from instruction to instruction. If this address can
1427 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1428 // "changed" flags, we need to load the set of registers from the table.
1429 // Because we always prefer to continue on to the next instruction, we should never have a
1430 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1431 // target.
1432 work_insn_idx_ = insn_idx;
1433 if (insn_flags_[insn_idx].IsBranchTarget()) {
1434 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001435 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001436 /*
1437 * Sanity check: retrieve the stored register line (assuming
1438 * a full table) and make sure it actually matches.
1439 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001441 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 if (work_line_->CompareLine(register_line) != 0) {
1443 Dump(std::cout);
1444 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001445 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001446 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001447 << " work_line=" << work_line_->Dump(this) << "\n"
1448 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 }
jeffhaobdb76512011-09-07 11:43:16 -07001450 }
jeffhaobdb76512011-09-07 11:43:16 -07001451 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001453 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001454 prepend += " failed to verify: ";
1455 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001456 return false;
1457 }
jeffhaobdb76512011-09-07 11:43:16 -07001458 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 insn_flags_[insn_idx].SetVisited();
1460 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001461 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001462
Ian Rogers1c849e52012-06-28 14:00:33 -07001463 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001464 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001465 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001466 * (besides the wasted space), but it indicates a flaw somewhere
1467 * down the line, possibly in the verifier.
1468 *
1469 * If we've substituted "always throw" instructions into the stream,
1470 * we are almost certainly going to have some dead code.
1471 */
1472 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001474 for (; insn_idx < insns_size;
1475 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001476 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001477 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001478 * may or may not be preceded by a padding NOP (for alignment).
1479 */
1480 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1481 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1482 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001483 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001484 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1485 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1486 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001488 }
1489
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001491 if (dead_start < 0)
1492 dead_start = insn_idx;
1493 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001494 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1495 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001496 dead_start = -1;
1497 }
1498 }
1499 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001500 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1501 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001502 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001503 // To dump the state of the verify after a method, do something like:
1504 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1505 // "boolean java.lang.String.equals(java.lang.Object)") {
1506 // LOG(INFO) << info_messages_.str();
1507 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001508 }
jeffhaobdb76512011-09-07 11:43:16 -07001509 return true;
1510}
1511
Ian Rogers776ac1f2012-04-13 23:36:36 -07001512bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001513 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1514 // We want the state _before_ the instruction, for the case where the dex pc we're
1515 // interested in is itself a monitor-enter instruction (which is a likely place
1516 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001517 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001518 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001519 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1520 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1521 }
1522 }
1523
jeffhaobdb76512011-09-07 11:43:16 -07001524 /*
1525 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001526 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001527 * control to another statement:
1528 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001529 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001530 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001531 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001532 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001533 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001534 * throw an exception that is handled by an encompassing "try"
1535 * block.
1536 *
1537 * We can also return, in which case there is no successor instruction
1538 * from this point.
1539 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001540 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001541 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1543 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001544 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001545
jeffhaobdb76512011-09-07 11:43:16 -07001546 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001547 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001548 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001550 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001551 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 }
jeffhaobdb76512011-09-07 11:43:16 -07001553
1554 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001555 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001556 * can throw an exception, we will copy/merge this into the "catch"
1557 * address rather than work_line, because we don't want the result
1558 * from the "successful" code path (e.g. a check-cast that "improves"
1559 * a type) to be visible to the exception handler.
1560 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001561 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001563 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001565 }
1566
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001567
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001568 // We need to ensure the work line is consistent while performing validation. When we spot a
1569 // peephole pattern we compute a new line for either the fallthrough instruction or the
1570 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001571 std::unique_ptr<RegisterLine> branch_line;
1572 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001573
Stephen Kyle7e541c92014-12-17 17:10:02 +00001574 /*
1575 * If we are in a constructor, and we currently have an UninitializedThis type
1576 * in a register somewhere, we need to make sure it isn't overwritten.
1577 */
1578 bool track_uninitialized_this = false;
1579 size_t uninitialized_this_loc = 0;
1580 if (IsConstructor()) {
1581 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1582 }
1583
Sebastien Hertz5243e912013-05-21 10:55:07 +02001584 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001585 case Instruction::NOP:
1586 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001587 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001588 * a signature that looks like a NOP; if we see one of these in
1589 * the course of executing code then we have a problem.
1590 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001591 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001593 }
1594 break;
1595
1596 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001597 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001598 break;
jeffhaobdb76512011-09-07 11:43:16 -07001599 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001600 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001601 break;
jeffhaobdb76512011-09-07 11:43:16 -07001602 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001603 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001604 break;
1605 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001606 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001607 break;
jeffhaobdb76512011-09-07 11:43:16 -07001608 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001609 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 break;
jeffhaobdb76512011-09-07 11:43:16 -07001611 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001612 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001613 break;
1614 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001615 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001616 break;
jeffhaobdb76512011-09-07 11:43:16 -07001617 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001618 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001619 break;
jeffhaobdb76512011-09-07 11:43:16 -07001620 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001621 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001622 break;
1623
1624 /*
1625 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001626 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001627 * might want to hold the result in an actual CPU register, so the
1628 * Dalvik spec requires that these only appear immediately after an
1629 * invoke or filled-new-array.
1630 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001631 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001632 * redundant with the reset done below, but it can make the debug info
1633 * easier to read in some cases.)
1634 */
1635 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001636 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001637 break;
1638 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001639 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001640 break;
1641 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001642 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001643 break;
1644
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001646 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1647 // where one entrypoint to the catch block is not actually an exception path.
1648 if (work_insn_idx_ == 0) {
1649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1650 break;
1651 }
jeffhaobdb76512011-09-07 11:43:16 -07001652 /*
jeffhao60f83e32012-02-13 17:16:30 -08001653 * This statement can only appear as the first instruction in an exception handler. We verify
1654 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001655 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001656 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001657 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001658 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 }
jeffhaobdb76512011-09-07 11:43:16 -07001660 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001662 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 }
jeffhaobdb76512011-09-07 11:43:16 -07001665 }
1666 break;
1667 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001668 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001669 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001670 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1673 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 } else {
1675 // Compilers may generate synthetic functions that write byte values into boolean fields.
1676 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001677 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001678 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1680 ((return_type.IsBoolean() || return_type.IsByte() ||
1681 return_type.IsShort() || return_type.IsChar()) &&
1682 src_type.IsInteger()));
1683 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001684 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001685 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001686 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001687 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 }
jeffhaobdb76512011-09-07 11:43:16 -07001689 }
1690 }
1691 break;
1692 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001693 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001694 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001695 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 } else {
1699 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001700 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001701 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001702 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 }
jeffhaobdb76512011-09-07 11:43:16 -07001705 }
1706 }
1707 break;
1708 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001709 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001710 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 } else {
1714 /* return_type is the *expected* return type, not register value */
1715 DCHECK(!return_type.IsZero());
1716 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001718 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001719 // Disallow returning uninitialized values and verify that the reference in vAA is an
1720 // instance of the "return_type"
1721 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001722 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1723 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001724 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001725 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1726 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1727 << "' or '" << reg_type << "'";
1728 } else {
1729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1730 << "', but expected from declaration '" << return_type << "'";
1731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 }
1733 }
1734 }
1735 break;
1736
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001737 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 case Instruction::CONST_4: {
1739 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001740 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001741 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001742 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 }
1744 case Instruction::CONST_16: {
1745 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001746 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001747 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001748 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001750 case Instruction::CONST: {
1751 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001753 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001754 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001755 }
1756 case Instruction::CONST_HIGH16: {
1757 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001758 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001759 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001760 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001761 }
jeffhaobdb76512011-09-07 11:43:16 -07001762 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001763 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001765 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1766 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001767 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001768 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001769 }
1770 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001772 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1773 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001774 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001775 break;
1776 }
1777 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001778 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001779 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1780 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001781 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001782 break;
1783 }
1784 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001786 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1787 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001788 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001789 break;
1790 }
jeffhaobdb76512011-09-07 11:43:16 -07001791 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001792 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001793 break;
jeffhaobdb76512011-09-07 11:43:16 -07001794 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001795 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001798 // Get type from instruction if unresolved then we need an access check
1799 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001800 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001801 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001802 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001803 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001804 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001805 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 }
jeffhaobdb76512011-09-07 11:43:16 -07001807 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001808 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001809 break;
1810 case Instruction::MONITOR_EXIT:
1811 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001812 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001813 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001814 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001815 * to the need to handle asynchronous exceptions, a now-deprecated
1816 * feature that Dalvik doesn't support.)
1817 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001818 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001819 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001820 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001821 * structured locking checks are working, the former would have
1822 * failed on the -enter instruction, and the latter is impossible.
1823 *
1824 * This is fortunate, because issue 3221411 prevents us from
1825 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001826 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001827 * some catch blocks (which will show up as "dead" code when
1828 * we skip them here); if we can't, then the code path could be
1829 * "live" so we still need to check it.
1830 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001831 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001832 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001833 break;
1834
Ian Rogers28ad40d2011-10-27 15:19:26 -07001835 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001837 /*
1838 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1839 * could be a "upcast" -- not expected, so we don't try to address it.)
1840 *
1841 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001842 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001843 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001844 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1845 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001846 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001847 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001848 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001849 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001850 if (klass != nullptr && klass->IsPrimitive()) {
1851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1852 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1853 << GetDeclaringClass();
1854 break;
1855 }
1856
Ian Rogersad0b3a32012-04-16 14:50:24 -07001857 DCHECK_NE(failures_.size(), 0U);
1858 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001859 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001860 }
1861 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001862 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001863 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001865 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001866 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001867 if (is_checkcast) {
1868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1869 } else {
1870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1871 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001872 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001873 if (is_checkcast) {
1874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1875 } else {
1876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1877 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001878 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001879 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001882 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001883 }
jeffhaobdb76512011-09-07 11:43:16 -07001884 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001885 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 }
1887 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001888 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001889 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001890 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001895 } else {
1896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 }
1898 break;
1899 }
1900 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001901 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001902 if (res_type.IsConflict()) {
1903 DCHECK_NE(failures_.size(), 0U);
1904 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001905 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001906 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1907 // can't create an instance of an interface or abstract class */
1908 if (!res_type.IsInstantiableTypes()) {
1909 Fail(VERIFY_ERROR_INSTANTIATION)
1910 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001911 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001913 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001914 // Any registers holding previous allocations from this address that have not yet been
1915 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001916 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001917 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 break;
1920 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001921 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001922 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001923 break;
1924 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001926 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001927 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001928 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001929 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001930 just_set_result = true; // Filled new array range sets result register
1931 break;
jeffhaobdb76512011-09-07 11:43:16 -07001932 case Instruction::CMPL_FLOAT:
1933 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001934 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001935 break;
1936 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001937 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001938 break;
1939 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001941 break;
1942 case Instruction::CMPL_DOUBLE:
1943 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001944 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001945 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001946 break;
1947 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001948 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001949 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001950 break;
1951 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
1954 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001955 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001956 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001957 break;
1958 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001960 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001961 break;
1962 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001963 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001964 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001967 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001968 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1969 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001970 }
1971 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 }
jeffhaobdb76512011-09-07 11:43:16 -07001973 case Instruction::GOTO:
1974 case Instruction::GOTO_16:
1975 case Instruction::GOTO_32:
1976 /* no effect on or use of registers */
1977 break;
1978
1979 case Instruction::PACKED_SWITCH:
1980 case Instruction::SPARSE_SWITCH:
1981 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001982 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001983 break;
1984
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 case Instruction::FILL_ARRAY_DATA: {
1986 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001987 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001988 /* array_type can be null if the reg type is Zero */
1989 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001990 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1992 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001993 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001994 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001995 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001996 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1998 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 } else {
jeffhao457cc512012-02-02 16:55:13 -08002000 // Now verify if the element width in the table matches the element width declared in
2001 // the array
2002 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2003 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002005 } else {
2006 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2007 // Since we don't compress the data in Dex, expect to see equal width of data stored
2008 // in the table and expected from the array class.
2009 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2011 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002012 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 }
2014 }
jeffhaobdb76512011-09-07 11:43:16 -07002015 }
2016 }
2017 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 }
jeffhaobdb76512011-09-07 11:43:16 -07002019 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002021 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2022 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 bool mismatch = false;
2024 if (reg_type1.IsZero()) { // zero then integral or reference expected
2025 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2026 } else if (reg_type1.IsReferenceTypes()) { // both references?
2027 mismatch = !reg_type2.IsReferenceTypes();
2028 } else { // both integral?
2029 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2030 }
2031 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2033 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002034 }
2035 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 }
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::IF_LT:
2038 case Instruction::IF_GE:
2039 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2042 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2045 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002046 }
2047 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 }
jeffhaobdb76512011-09-07 11:43:16 -07002049 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002051 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2054 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002056
2057 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002058 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002059 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002060 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002061 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002062 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002063 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002064 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2065 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2066 work_insn_idx_)) {
2067 break;
2068 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002069 } else {
2070 break;
2071 }
2072
Ian Rogers9b360392013-06-06 14:45:07 -07002073 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002074
2075 /* Check for peep-hole pattern of:
2076 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002077 * instance-of vX, vY, T;
2078 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002079 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002080 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002081 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002082 * and sharpen the type of vY to be type T.
2083 * Note, this pattern can't be if:
2084 * - if there are other branches to this branch,
2085 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002086 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002087 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002088 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2089 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2090 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002091 // Check the type of the instance-of is different than that of registers type, as if they
2092 // are the same there is no work to be done here. Check that the conversion is not to or
2093 // from an unresolved type as type information is imprecise. If the instance-of is to an
2094 // interface then ignore the type information as interfaces can only be treated as Objects
2095 // and we don't want to disallow field and other operations on the object. If the value
2096 // being instance-of checked against is known null (zero) then allow the optimization as
2097 // we didn't have type information. If the merge of the instance-of type with the original
2098 // type is assignable to the original then allow optimization. This check is performed to
2099 // ensure that subsequent merges don't lose type information - such as becoming an
2100 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002101 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002102 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002103
Ian Rogersebbdd872014-07-07 23:53:08 -07002104 if (!orig_type.Equals(cast_type) &&
2105 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002106 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002107 !cast_type.GetClass()->IsInterface() &&
2108 (orig_type.IsZero() ||
2109 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002110 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002111 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002112 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002113 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002114 branch_line.reset(update_line);
2115 }
2116 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002117 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002118 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2119 // See if instance-of was preceded by a move-object operation, common due to the small
2120 // register encoding space of instance-of, and propagate type information to the source
2121 // of the move-object.
2122 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002123 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002124 move_idx--;
2125 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002126 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2127 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2128 work_insn_idx_)) {
2129 break;
2130 }
Ian Rogers9b360392013-06-06 14:45:07 -07002131 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2132 switch (move_inst->Opcode()) {
2133 case Instruction::MOVE_OBJECT:
2134 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002135 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002136 }
2137 break;
2138 case Instruction::MOVE_OBJECT_FROM16:
2139 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002140 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002141 }
2142 break;
2143 case Instruction::MOVE_OBJECT_16:
2144 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002145 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002146 }
2147 break;
2148 default:
2149 break;
2150 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002151 }
2152 }
2153 }
2154
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 }
jeffhaobdb76512011-09-07 11:43:16 -07002157 case Instruction::IF_LTZ:
2158 case Instruction::IF_GEZ:
2159 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002161 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2164 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 }
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 }
jeffhaobdb76512011-09-07 11:43:16 -07002168 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 break;
jeffhaobdb76512011-09-07 11:43:16 -07002171 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 break;
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002175 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 break;
jeffhaobdb76512011-09-07 11:43:16 -07002177 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 break;
jeffhaobdb76512011-09-07 11:43:16 -07002183 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002184 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 break;
2186 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
2189
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002191 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 break;
2193 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002194 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 break;
2196 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 break;
2199 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002200 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
2202 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002203 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002204 break;
2205 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002207 break;
2208 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211
jeffhaobdb76512011-09-07 11:43:16 -07002212 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002213 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 break;
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002216 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 break;
jeffhaobdb76512011-09-07 11:43:16 -07002218 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002219 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 break;
jeffhaobdb76512011-09-07 11:43:16 -07002221 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002222 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 break;
2224 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002225 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002228 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002231 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2232 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 break;
jeffhaobdb76512011-09-07 11:43:16 -07002234
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002236 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 break;
2238 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002239 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 break;
2241 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002242 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 break;
2244 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002245 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
2247 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002248 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002251 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 break;
jeffhaobdb76512011-09-07 11:43:16 -07002253 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002254 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2255 false);
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257
jeffhaobdb76512011-09-07 11:43:16 -07002258 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002259 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 break;
jeffhaobdb76512011-09-07 11:43:16 -07002261 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002262 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 break;
jeffhaobdb76512011-09-07 11:43:16 -07002264 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002265 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 break;
jeffhaobdb76512011-09-07 11:43:16 -07002267 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002268 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 break;
2270 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002271 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002274 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002277 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2278 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 break;
2280
2281 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002282 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 break;
2284 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002285 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 break;
2287 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002288 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
2290 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002291 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002294 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
2296 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002297 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002300 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2301 true);
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
2303
2304 case Instruction::INVOKE_VIRTUAL:
2305 case Instruction::INVOKE_VIRTUAL_RANGE:
2306 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2309 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002310 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2311 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002312 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2313 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002314 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002315 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002316 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002317 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002318 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002319 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002320 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2321 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002322 return_type_class->CannotBeAssignedFromOtherTypes());
2323 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002324 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2325 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002326 }
2327 }
2328 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002330 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2331 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002332 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002333 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002334 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002335 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002336 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002338 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002340 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 }
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002345 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002346 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002347 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002348 const char* return_type_descriptor;
2349 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002350 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002351 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002353 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002354 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002355 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2356 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2357 } else {
2358 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002359 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002360 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002361 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002362 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002363 if (return_type_class != nullptr) {
2364 return_type = &reg_types_.FromClass(return_type_descriptor,
2365 return_type_class,
2366 return_type_class->CannotBeAssignedFromOtherTypes());
2367 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002368 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2369 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002370 }
Ian Rogers46685432012-06-03 22:26:43 -07002371 }
2372 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002373 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 * Some additional checks when calling a constructor. We know from the invocation arg check
2375 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2376 * that to require that called_method->klass is the same as this->klass or this->super,
2377 * allowing the latter only if the "this" argument is the same as the "this" argument to
2378 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002379 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002380 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002381 if (this_type.IsConflict()) // failure.
2382 break;
jeffhaobdb76512011-09-07 11:43:16 -07002383
jeffhaob57e9522012-04-26 18:08:21 -07002384 /* no null refs allowed (?) */
2385 if (this_type.IsZero()) {
2386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2387 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002388 }
jeffhaob57e9522012-04-26 18:08:21 -07002389
2390 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002391 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002392 // TODO: re-enable constructor type verification
2393 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002394 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002395 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2396 // break;
2397 // }
jeffhaob57e9522012-04-26 18:08:21 -07002398
2399 /* arg must be an uninitialized reference */
2400 if (!this_type.IsUninitializedTypes()) {
2401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2402 << this_type;
2403 break;
2404 }
2405
2406 /*
2407 * Replace the uninitialized reference with an initialized one. We need to do this for all
2408 * registers that have the same object instance in them, not just the "this" register.
2409 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002410 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002411 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002412 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002413 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2414 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002415 }
2416 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002417 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002418 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002419 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002420 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002421 just_set_result = true;
2422 break;
2423 }
2424 case Instruction::INVOKE_STATIC:
2425 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002426 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002427 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002428 METHOD_STATIC,
2429 is_range,
2430 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002431 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002433 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002434 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2435 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002436 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002437 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002438 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002439 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002440 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002441 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002442 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002443 } else {
2444 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2445 }
jeffhaobdb76512011-09-07 11:43:16 -07002446 just_set_result = true;
2447 }
2448 break;
jeffhaobdb76512011-09-07 11:43:16 -07002449 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002451 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002452 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002453 METHOD_INTERFACE,
2454 is_range,
2455 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002456 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002457 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002458 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2459 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2460 << PrettyMethod(abs_method) << "'";
2461 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 }
Ian Rogers0d604842012-04-16 14:50:24 -07002463 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002464 /* Get the type of the "this" arg, which should either be a sub-interface of called
2465 * interface or Object (see comments in RegType::JoinClass).
2466 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002467 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002468 if (this_type.IsZero()) {
2469 /* null pointer always passes (and always fails at runtime) */
2470 } else {
2471 if (this_type.IsUninitializedTypes()) {
2472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2473 << this_type;
2474 break;
2475 }
2476 // In the past we have tried to assert that "called_interface" is assignable
2477 // from "this_type.GetClass()", however, as we do an imprecise Join
2478 // (RegType::JoinClass) we don't have full information on what interfaces are
2479 // implemented by "this_type". For example, two classes may implement the same
2480 // interfaces and have a common parent that doesn't implement the interface. The
2481 // join will set "this_type" to the parent class and a test that this implements
2482 // the interface will incorrectly fail.
2483 }
2484 /*
2485 * We don't have an object instance, so we can't find the concrete method. However, all of
2486 * the type information is in the abstract method, so we're good.
2487 */
2488 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002489 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002491 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2492 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2493 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2494 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002495 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002496 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002497 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002498 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002499 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002500 } else {
2501 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2502 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002503 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002504 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 }
jeffhaobdb76512011-09-07 11:43:16 -07002506 case Instruction::NEG_INT:
2507 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002509 break;
2510 case Instruction::NEG_LONG:
2511 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002513 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002519 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002520 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002521 break;
2522 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002524 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002525 break;
2526 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002530 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002531 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002534 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002535 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
2537 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002542 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002543 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
2545 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002550 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002554 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002557 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002558 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002562 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002566 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
2571 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002572 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002573 break;
2574 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002575 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002576 break;
2577
2578 case Instruction::ADD_INT:
2579 case Instruction::SUB_INT:
2580 case Instruction::MUL_INT:
2581 case Instruction::REM_INT:
2582 case Instruction::DIV_INT:
2583 case Instruction::SHL_INT:
2584 case Instruction::SHR_INT:
2585 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002586 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002587 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002588 break;
2589 case Instruction::AND_INT:
2590 case Instruction::OR_INT:
2591 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002592 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002593 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595 case Instruction::ADD_LONG:
2596 case Instruction::SUB_LONG:
2597 case Instruction::MUL_LONG:
2598 case Instruction::DIV_LONG:
2599 case Instruction::REM_LONG:
2600 case Instruction::AND_LONG:
2601 case Instruction::OR_LONG:
2602 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002603 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002604 reg_types_.LongLo(), reg_types_.LongHi(),
2605 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::SHL_LONG:
2608 case Instruction::SHR_LONG:
2609 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002611 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002612 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002613 break;
2614 case Instruction::ADD_FLOAT:
2615 case Instruction::SUB_FLOAT:
2616 case Instruction::MUL_FLOAT:
2617 case Instruction::DIV_FLOAT:
2618 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002619 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2620 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::ADD_DOUBLE:
2623 case Instruction::SUB_DOUBLE:
2624 case Instruction::MUL_DOUBLE:
2625 case Instruction::DIV_DOUBLE:
2626 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002627 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002628 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2629 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::ADD_INT_2ADDR:
2632 case Instruction::SUB_INT_2ADDR:
2633 case Instruction::MUL_INT_2ADDR:
2634 case Instruction::REM_INT_2ADDR:
2635 case Instruction::SHL_INT_2ADDR:
2636 case Instruction::SHR_INT_2ADDR:
2637 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002638 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2639 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002640 break;
2641 case Instruction::AND_INT_2ADDR:
2642 case Instruction::OR_INT_2ADDR:
2643 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002644 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2645 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002648 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2649 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::ADD_LONG_2ADDR:
2652 case Instruction::SUB_LONG_2ADDR:
2653 case Instruction::MUL_LONG_2ADDR:
2654 case Instruction::DIV_LONG_2ADDR:
2655 case Instruction::REM_LONG_2ADDR:
2656 case Instruction::AND_LONG_2ADDR:
2657 case Instruction::OR_LONG_2ADDR:
2658 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002659 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002660 reg_types_.LongLo(), reg_types_.LongHi(),
2661 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002662 break;
2663 case Instruction::SHL_LONG_2ADDR:
2664 case Instruction::SHR_LONG_2ADDR:
2665 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002666 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002667 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002668 break;
2669 case Instruction::ADD_FLOAT_2ADDR:
2670 case Instruction::SUB_FLOAT_2ADDR:
2671 case Instruction::MUL_FLOAT_2ADDR:
2672 case Instruction::DIV_FLOAT_2ADDR:
2673 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002674 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2675 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::ADD_DOUBLE_2ADDR:
2678 case Instruction::SUB_DOUBLE_2ADDR:
2679 case Instruction::MUL_DOUBLE_2ADDR:
2680 case Instruction::DIV_DOUBLE_2ADDR:
2681 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002682 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002683 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2684 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002687 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002688 case Instruction::MUL_INT_LIT16:
2689 case Instruction::DIV_INT_LIT16:
2690 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002691 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2692 true);
jeffhaobdb76512011-09-07 11:43:16 -07002693 break;
2694 case Instruction::AND_INT_LIT16:
2695 case Instruction::OR_INT_LIT16:
2696 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002697 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2698 true);
jeffhaobdb76512011-09-07 11:43:16 -07002699 break;
2700 case Instruction::ADD_INT_LIT8:
2701 case Instruction::RSUB_INT_LIT8:
2702 case Instruction::MUL_INT_LIT8:
2703 case Instruction::DIV_INT_LIT8:
2704 case Instruction::REM_INT_LIT8:
2705 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002706 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002707 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002708 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2709 false);
jeffhaobdb76512011-09-07 11:43:16 -07002710 break;
2711 case Instruction::AND_INT_LIT8:
2712 case Instruction::OR_INT_LIT8:
2713 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002714 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2715 false);
jeffhaobdb76512011-09-07 11:43:16 -07002716 break;
2717
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002718 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002719 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002720 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2722 }
2723 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002724 // Note: the following instructions encode offsets derived from class linking.
2725 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2726 // meaning if the class linking and resolution were successful.
2727 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002728 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002729 break;
2730 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002731 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002732 break;
2733 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002734 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002735 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002736 case Instruction::IGET_BOOLEAN_QUICK:
2737 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2738 break;
2739 case Instruction::IGET_BYTE_QUICK:
2740 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2741 break;
2742 case Instruction::IGET_CHAR_QUICK:
2743 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2744 break;
2745 case Instruction::IGET_SHORT_QUICK:
2746 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2747 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002748 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002749 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002750 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002751 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002752 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002753 break;
2754 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002755 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002756 break;
2757 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002758 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002759 break;
2760 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002761 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002762 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002763 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002764 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002765 break;
2766 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002767 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002768 break;
2769 case Instruction::INVOKE_VIRTUAL_QUICK:
2770 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2771 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002772 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002773 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002774 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002775 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002776 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002777 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002778 } else {
2779 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2780 }
2781 just_set_result = true;
2782 }
2783 break;
2784 }
2785
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002787 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2788 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002789 case Instruction::UNUSED_79:
2790 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002792 break;
2793
2794 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002795 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002796 * complain if an instruction is missing (which is desirable).
2797 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002798 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002799
Stephen Kyle7e541c92014-12-17 17:10:02 +00002800 /*
2801 * If we are in a constructor, and we had an UninitializedThis type
2802 * in a register somewhere, we need to make sure it wasn't overwritten.
2803 */
2804 if (track_uninitialized_this) {
2805 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2806 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2807 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2808 was_invoke_direct)) {
2809 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2810 << "Constructor failed to initialize this object";
2811 }
2812 }
2813
Ian Rogersad0b3a32012-04-16 14:50:24 -07002814 if (have_pending_hard_failure_) {
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +00002815 if (Runtime::Current()->IsCompiler()) {
2816 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002817 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002818 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002819 /* immediate failure, reject class */
2820 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2821 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002822 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002823 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002824 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002825 }
jeffhaobdb76512011-09-07 11:43:16 -07002826 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 * If we didn't just set the result register, clear it out. This ensures that you can only use
2828 * "move-result" immediately after the result is set. (We could check this statically, but it's
2829 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002830 */
2831 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002832 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002833 }
2834
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002835
jeffhaobdb76512011-09-07 11:43:16 -07002836
2837 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002838 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002839 *
2840 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002841 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002842 * somebody could get a reference field, check it for zero, and if the
2843 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002844 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002845 * that, and will reject the code.
2846 *
2847 * TODO: avoid re-fetching the branch target
2848 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002849 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002850 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002852 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002854 return false;
2855 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002856 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002857 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002858 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 }
jeffhaobdb76512011-09-07 11:43:16 -07002860 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002861 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002862 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002863 return false;
2864 }
2865 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002866 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002867 return false;
2868 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 }
jeffhaobdb76512011-09-07 11:43:16 -07002870 }
2871
2872 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002873 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002874 *
2875 * We've already verified that the table is structurally sound, so we
2876 * just need to walk through and tag the targets.
2877 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002878 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002879 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2880 const uint16_t* switch_insns = insns + offset_to_switch;
2881 int switch_count = switch_insns[1];
2882 int offset_to_targets, targ;
2883
2884 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2885 /* 0 = sig, 1 = count, 2/3 = first key */
2886 offset_to_targets = 4;
2887 } else {
2888 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002889 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002890 offset_to_targets = 2 + 2 * switch_count;
2891 }
2892
2893 /* verify each switch target */
2894 for (targ = 0; targ < switch_count; targ++) {
2895 int offset;
2896 uint32_t abs_offset;
2897
2898 /* offsets are 32-bit, and only partly endian-swapped */
2899 offset = switch_insns[offset_to_targets + targ * 2] |
2900 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 abs_offset = work_insn_idx_ + offset;
2902 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002903 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002904 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002906 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002907 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002908 }
jeffhaobdb76512011-09-07 11:43:16 -07002909 }
2910 }
2911
2912 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2914 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002915 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002916 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002917 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002918 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002919
Andreas Gampef91baf12014-07-18 15:41:00 -07002920 // Need the linker to try and resolve the handled class to check if it's Throwable.
2921 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2922
Ian Rogers0571d352011-11-03 19:51:38 -07002923 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002924 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2925 if (handler_type_idx == DexFile::kDexNoIndex16) {
2926 has_catch_all_handler = true;
2927 } else {
2928 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002929 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2930 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002931 if (klass != nullptr) {
2932 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2933 has_catch_all_handler = true;
2934 }
2935 } else {
2936 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002937 DCHECK(self_->IsExceptionPending());
2938 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002939 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 }
jeffhaobdb76512011-09-07 11:43:16 -07002941 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2943 * "work_regs", because at runtime the exception will be thrown before the instruction
2944 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002945 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002946 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002947 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 }
jeffhaobdb76512011-09-07 11:43:16 -07002949 }
2950
2951 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2953 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002954 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002955 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002956 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 * The state in work_line reflects the post-execution state. If the current instruction is a
2958 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002959 * it will do so before grabbing the lock).
2960 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002961 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002962 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002964 return false;
2965 }
2966 }
2967 }
2968
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002969 /* Handle "continue". Tag the next consecutive instruction.
2970 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2971 * because it changes work_line_ when performing peephole optimization
2972 * and this change should not be used in those cases.
2973 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002974 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002975 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2976 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002977 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2979 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002980 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002981 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2982 // next instruction isn't one.
2983 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2984 return false;
2985 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002986 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002987 // Make workline consistent with fallthrough computed from peephole optimization.
2988 work_line_->CopyFromLine(fallthrough_line.get());
2989 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002990 if (insn_flags_[next_insn_idx].IsReturn()) {
2991 // For returns we only care about the operand to the return, all other registers are dead.
2992 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2993 Instruction::Code opcode = ret_inst->Opcode();
2994 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00002995 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00002996 } else {
2997 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002999 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003000 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003001 }
3002 }
3003 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003004 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003005 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003006 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3007 // needed. If the merge changes the state of the registers then the work line will be
3008 // updated.
3009 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003010 return false;
3011 }
3012 } else {
3013 /*
3014 * We're not recording register data for the next instruction, so we don't know what the
3015 * prior state was. We have to assume that something has changed and re-evaluate it.
3016 */
3017 insn_flags_[next_insn_idx].SetChanged();
3018 }
3019 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003020
jeffhaod1f0fde2011-09-08 17:25:33 -07003021 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003022 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003023 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 return false;
3025 }
jeffhaobdb76512011-09-07 11:43:16 -07003026 }
3027
3028 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003029 * Update start_guess. Advance to the next instruction of that's
3030 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003031 * neither of those exists we're in a return or throw; leave start_guess
3032 * alone and let the caller sort it out.
3033 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003034 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003035 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3036 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003038 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003040 }
3041
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3043 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003044
3045 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003046} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003047
Ian Rogersd8f69b02014-09-10 21:43:52 +00003048const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003049 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003050 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003051 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003052 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003053 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3054 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003055 if (result.IsConflict()) {
3056 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3057 << "' in " << referrer;
3058 return result;
3059 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003061 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003062 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003063 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003064 // check at runtime if access is allowed and so pass here. If result is
3065 // primitive, skip the access check.
3066 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3067 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003068 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003069 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003070 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003071 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003072}
3073
Ian Rogersd8f69b02014-09-10 21:43:52 +00003074const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003077 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3079 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003080 CatchHandlerIterator iterator(handlers_ptr);
3081 for (; iterator.HasNext(); iterator.Next()) {
3082 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3083 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003084 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003086 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003087 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003088 if (exception.IsUnresolvedTypes()) {
3089 // We don't know enough about the type. Fail here and let runtime handle it.
3090 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3091 return exception;
3092 } else {
3093 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3094 return reg_types_.Conflict();
3095 }
Jeff Haob878f212014-04-24 16:25:36 -07003096 } else if (common_super == nullptr) {
3097 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003098 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003099 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003101 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003102 if (FailOrAbort(this,
3103 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3104 "java.lang.Throwable is not assignable-from common_super at ",
3105 work_insn_idx_)) {
3106 break;
3107 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
3109 }
3110 }
3111 }
Ian Rogers0571d352011-11-03 19:51:38 -07003112 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 }
3114 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003115 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003117 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003118 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003120 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003121}
3122
Brian Carlstromea46f952013-07-30 01:26:50 -07003123mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003124 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003125 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003126 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003127 if (klass_type.IsConflict()) {
3128 std::string append(" in attempt to access method ");
3129 append += dex_file_->GetMethodName(method_id);
3130 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003132 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003133 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003134 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003135 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003136 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003137 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003138 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003139 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003140 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003141 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003142
3143 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003145 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003146 res_method = klass->FindInterfaceMethod(name, signature);
3147 } else {
3148 res_method = klass->FindVirtualMethod(name, signature);
3149 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003151 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003153 // If a virtual or interface method wasn't found with the expected type, look in
3154 // the direct methods. This can happen when the wrong invoke type is used or when
3155 // a class has changed, and will be flagged as an error in later checks.
3156 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3157 res_method = klass->FindDirectMethod(name, signature);
3158 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003159 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003160 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3161 << PrettyDescriptor(klass) << "." << name
3162 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003163 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003164 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 }
3166 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003167 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3168 // enforce them here.
3169 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3171 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003172 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003173 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003174 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003175 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3177 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003179 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003180 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003181 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003182 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003183 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003184 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003185 }
jeffhaode0d9c92012-02-27 13:58:13 -08003186 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3187 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3189 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003190 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003191 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003192 // Check that interface methods match interface classes.
3193 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3194 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3195 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003196 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003197 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3198 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3199 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003200 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003201 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003202 // See if the method type implied by the invoke instruction matches the access flags for the
3203 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003204 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3206 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3207 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003208 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3209 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003212 return res_method;
3213}
3214
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003215template <class T>
3216mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3217 MethodType method_type,
3218 bool is_range,
3219 mirror::ArtMethod* res_method) {
3220 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3221 // match the call to the signature. Also, we might be calling through an abstract method
3222 // definition (which doesn't have register count values).
3223 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3224 /* caught by static verifier */
3225 DCHECK(is_range || expected_args <= 5);
3226 if (expected_args > code_item_->outs_size_) {
3227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3228 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3229 return nullptr;
3230 }
3231
3232 uint32_t arg[5];
3233 if (!is_range) {
3234 inst->GetVarArgs(arg);
3235 }
3236 uint32_t sig_registers = 0;
3237
3238 /*
3239 * Check the "this" argument, which must be an instance of the class that declared the method.
3240 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3241 * rigorous check here (which is okay since we have to do it at runtime).
3242 */
3243 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003244 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003245 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3246 CHECK(have_pending_hard_failure_);
3247 return nullptr;
3248 }
3249 if (actual_arg_type.IsUninitializedReference()) {
3250 if (res_method) {
3251 if (!res_method->IsConstructor()) {
3252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3253 return nullptr;
3254 }
3255 } else {
3256 // Check whether the name of the called method is "<init>"
3257 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003258 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3260 return nullptr;
3261 }
3262 }
3263 }
3264 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003265 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003266 if (res_method != nullptr) {
3267 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003268 std::string temp;
3269 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003270 klass->CannotBeAssignedFromOtherTypes());
3271 } else {
3272 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3273 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003274 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003275 dex_file_->StringByTypeIdx(class_idx),
3276 false);
3277 }
3278 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3279 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3280 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3281 << "' not instance of '" << *res_method_class << "'";
3282 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3283 // the compiler.
3284 if (have_pending_hard_failure_) {
3285 return nullptr;
3286 }
3287 }
3288 }
3289 sig_registers = 1;
3290 }
3291
3292 for ( ; it->HasNext(); it->Next()) {
3293 if (sig_registers >= expected_args) {
3294 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3295 " arguments, found " << sig_registers << " or more.";
3296 return nullptr;
3297 }
3298
3299 const char* param_descriptor = it->GetDescriptor();
3300
3301 if (param_descriptor == nullptr) {
3302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3303 "component";
3304 return nullptr;
3305 }
3306
Ian Rogersd8f69b02014-09-10 21:43:52 +00003307 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003308 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3309 arg[sig_registers];
3310 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003311 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003312 if (!src_type.IsIntegralTypes()) {
3313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3314 << " but expected " << reg_type;
3315 return res_method;
3316 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003317 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003318 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3319 // compiler.
3320 if (have_pending_hard_failure_) {
3321 return res_method;
3322 }
3323 }
3324 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3325 }
3326 if (expected_args != sig_registers) {
3327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3328 " arguments, found " << sig_registers;
3329 return nullptr;
3330 }
3331 return res_method;
3332}
3333
3334void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3335 MethodType method_type,
3336 bool is_range) {
3337 // As the method may not have been resolved, make this static check against what we expect.
3338 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3339 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3340 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3341 DexFileParameterIterator it(*dex_file_,
3342 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3343 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3344 nullptr);
3345}
3346
3347class MethodParamListDescriptorIterator {
3348 public:
3349 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3350 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3351 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3352 }
3353
3354 bool HasNext() {
3355 return pos_ < params_size_;
3356 }
3357
3358 void Next() {
3359 ++pos_;
3360 }
3361
3362 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3363 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3364 }
3365
3366 private:
3367 mirror::ArtMethod* res_method_;
3368 size_t pos_;
3369 const DexFile::TypeList* params_;
3370 const size_t params_size_;
3371};
3372
Brian Carlstromea46f952013-07-30 01:26:50 -07003373mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003374 MethodType method_type,
3375 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003376 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003377 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3378 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003379 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003380
Brian Carlstromea46f952013-07-30 01:26:50 -07003381 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003382 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003383 // Check what we can statically.
3384 if (!have_pending_hard_failure_) {
3385 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3386 }
3387 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003388 }
3389
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3391 // has a vtable entry for the target method.
3392 if (is_super) {
3393 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003394 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003395 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003396 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003397 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003398 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003399 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003400 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003401 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003402 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003403 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003404 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003405 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003406 << "." << res_method->GetName()
3407 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003408 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003409 }
3410 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003411
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003412 // Process the target method's signature. This signature may or may not
3413 MethodParamListDescriptorIterator it(res_method);
3414 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3415 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003416}
3417
Brian Carlstromea46f952013-07-30 01:26:50 -07003418mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003419 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003420 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3421 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003422 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003423 if (!actual_arg_type.HasClass()) {
3424 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003425 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003426 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003427 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003428 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003429 if (klass->IsInterface()) {
3430 // Derive Object.class from Class.class.getSuperclass().
3431 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003432 if (FailOrAbort(this, object_klass->IsObjectClass(),
3433 "Failed to find Object class in quickened invoke receiver",
3434 work_insn_idx_)) {
3435 return nullptr;
3436 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003437 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003438 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003439 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003440 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003441 if (FailOrAbort(this, dispatch_class->HasVTable(),
3442 "Receiver class has no vtable for quickened invoke at ",
3443 work_insn_idx_)) {
3444 return nullptr;
3445 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003446 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe7c038102014-10-27 20:08:46 -07003447 if (FailOrAbort(this, static_cast<int32_t>(vtable_index) < dispatch_class->GetVTableLength(),
3448 "Receiver class has not enough vtable slots for quickened invoke at ",
3449 work_insn_idx_)) {
3450 return nullptr;
3451 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003452 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier36b58f52014-12-10 12:06:45 -08003453 if (FailOrAbort(this, !self_->IsExceptionPending(),
Andreas Gampe7c038102014-10-27 20:08:46 -07003454 "Unexpected exception pending for quickened invoke at ",
3455 work_insn_idx_)) {
3456 return nullptr;
3457 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003458 return res_method;
3459}
3460
Brian Carlstromea46f952013-07-30 01:26:50 -07003461mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003462 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003463 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3464 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3465
Brian Carlstromea46f952013-07-30 01:26:50 -07003466 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003467 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003468 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003470 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003471 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003472 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3473 work_insn_idx_)) {
3474 return nullptr;
3475 }
3476 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3477 work_insn_idx_)) {
3478 return nullptr;
3479 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003480
3481 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3482 // match the call to the signature. Also, we might be calling through an abstract method
3483 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003484 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003485 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003486 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003487 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003488 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3489 /* caught by static verifier */
3490 DCHECK(is_range || expected_args <= 5);
3491 if (expected_args > code_item_->outs_size_) {
3492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3493 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003494 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003495 }
3496
3497 /*
3498 * Check the "this" argument, which must be an instance of the class that declared the method.
3499 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3500 * rigorous check here (which is okay since we have to do it at runtime).
3501 */
3502 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003504 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003505 }
3506 if (!actual_arg_type.IsZero()) {
3507 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003508 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003509 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003510 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003511 klass->CannotBeAssignedFromOtherTypes());
3512 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003513 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3514 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003515 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003516 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003517 }
3518 }
3519 /*
3520 * Process the target method's signature. This signature may or may not
3521 * have been verified, so we can't assume it's properly formed.
3522 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003523 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003524 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003525 uint32_t arg[5];
3526 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003527 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003528 }
3529 size_t actual_args = 1;
3530 for (size_t param_index = 0; param_index < params_size; param_index++) {
3531 if (actual_args >= expected_args) {
3532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003533 << "'. Expected " << expected_args
3534 << " arguments, processing argument " << actual_args
3535 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003536 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003537 }
3538 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003539 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003540 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003542 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003543 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003544 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003545 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003546 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003547 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003548 return res_method;
3549 }
3550 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3551 }
3552 if (actual_args != expected_args) {
3553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3554 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003555 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003556 } else {
3557 return res_method;
3558 }
3559}
3560
Ian Rogers62342ec2013-06-11 10:26:37 -07003561void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003562 uint32_t type_idx;
3563 if (!is_filled) {
3564 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3565 type_idx = inst->VRegC_22c();
3566 } else if (!is_range) {
3567 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3568 type_idx = inst->VRegB_35c();
3569 } else {
3570 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3571 type_idx = inst->VRegB_3rc();
3572 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003573 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003574 if (res_type.IsConflict()) { // bad class
3575 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003576 } else {
3577 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3578 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003580 } else if (!is_filled) {
3581 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003582 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003583 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003584 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003585 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003586 } else {
3587 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3588 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003589 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003590 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3591 uint32_t arg[5];
3592 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003593 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003594 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003595 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003596 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003597 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3598 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003599 return;
3600 }
3601 }
3602 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003603 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003604 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003605 }
3606 }
3607}
3608
Sebastien Hertz5243e912013-05-21 10:55:07 +02003609void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003610 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003615 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003616 if (array_type.IsZero()) {
3617 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3618 // instruction type. TODO: have a proper notion of bottom here.
3619 if (!is_primitive || insn_type.IsCategory1Types()) {
3620 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003621 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003622 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003623 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003624 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3625 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003626 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003627 }
jeffhaofc3144e2012-02-01 17:21:15 -08003628 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003630 } else {
3631 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003632 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003633 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003634 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003635 << " source for aget-object";
3636 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003638 << " source for category 1 aget";
3639 } else if (is_primitive && !insn_type.Equals(component_type) &&
3640 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003641 (insn_type.IsLong() && component_type.IsDouble()))) {
3642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3643 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003644 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003645 // Use knowledge of the field type which is stronger than the type inferred from the
3646 // instruction, which can't differentiate object types and ints from floats, longs from
3647 // doubles.
3648 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003649 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003650 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003651 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003652 component_type.HighHalf(&reg_types_));
3653 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003654 }
3655 }
3656 }
3657}
3658
Ian Rogersd8f69b02014-09-10 21:43:52 +00003659void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003660 const uint32_t vregA) {
3661 // Primitive assignability rules are weaker than regular assignability rules.
3662 bool instruction_compatible;
3663 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003664 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003665 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003666 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003667 value_compatible = value_type.IsIntegralTypes();
3668 } else if (target_type.IsFloat()) {
3669 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3670 value_compatible = value_type.IsFloatTypes();
3671 } else if (target_type.IsLong()) {
3672 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003673 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3674 // as target_type depends on the resolved type of the field.
3675 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003676 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003677 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3678 } else {
3679 value_compatible = false;
3680 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003681 } else if (target_type.IsDouble()) {
3682 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003683 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3684 // as target_type depends on the resolved type of the field.
3685 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003686 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003687 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3688 } else {
3689 value_compatible = false;
3690 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003691 } else {
3692 instruction_compatible = false; // reference with primitive store
3693 value_compatible = false; // unused
3694 }
3695 if (!instruction_compatible) {
3696 // This is a global failure rather than a class change failure as the instructions and
3697 // the descriptors for the type should have been consistent within the same file at
3698 // compile time.
3699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3700 << "' but expected type '" << target_type << "'";
3701 return;
3702 }
3703 if (!value_compatible) {
3704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3705 << " of type " << value_type << " but expected " << target_type << " for put";
3706 return;
3707 }
3708}
3709
Sebastien Hertz5243e912013-05-21 10:55:07 +02003710void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003711 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003712 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003713 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003715 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003716 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003717 if (array_type.IsZero()) {
3718 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3719 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003720 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003722 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003723 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003724 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003725 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003726 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003728 if (!component_type.IsReferenceTypes()) {
3729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3730 << " source for aput-object";
3731 } else {
3732 // The instruction agrees with the type of array, confirm the value to be stored does too
3733 // Note: we use the instruction type (rather than the component type) for aput-object as
3734 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003735 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003736 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 }
3738 }
3739 }
3740}
3741
Brian Carlstromea46f952013-07-30 01:26:50 -07003742mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003743 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3744 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003745 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003746 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003747 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3748 field_idx, dex_file_->GetFieldName(field_id),
3749 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003750 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003751 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003752 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003753 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003754 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003755 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003756 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3757 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003758 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003759 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003760 << dex_file_->GetFieldName(field_id) << ") in "
3761 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003762 DCHECK(self_->IsExceptionPending());
3763 self_->ClearException();
3764 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003765 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3766 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003767 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003768 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003769 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003770 } else if (!field->IsStatic()) {
3771 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003772 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003773 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003774 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003775}
3776
Ian Rogersd8f69b02014-09-10 21:43:52 +00003777mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003778 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3779 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003780 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003781 if (klass_type.IsConflict()) {
3782 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3783 field_idx, dex_file_->GetFieldName(field_id),
3784 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003785 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003786 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003787 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003788 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003789 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003790 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003791 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3792 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003793 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003794 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003795 << dex_file_->GetFieldName(field_id) << ") in "
3796 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003797 DCHECK(self_->IsExceptionPending());
3798 self_->ClearException();
3799 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003800 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3801 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003803 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003804 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 } else if (field->IsStatic()) {
3806 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3807 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003808 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003809 } else if (obj_type.IsZero()) {
3810 // Cannot infer and check type, however, access will cause null pointer exception
3811 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003812 } else if (!obj_type.IsReferenceTypes()) {
3813 // Trying to read a field from something that isn't a reference
3814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3815 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003817 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003818 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003819 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003820 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003821 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003822 if (obj_type.IsUninitializedTypes() &&
3823 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3824 !field_klass.Equals(GetDeclaringClass()))) {
3825 // Field accesses through uninitialized references are only allowable for constructors where
3826 // the field is declared in this class
3827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003828 << " of a not fully initialized object within the context"
3829 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003830 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3832 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3833 // of C1. For resolution to occur the declared class of the field must be compatible with
3834 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3835 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3836 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003837 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003838 } else {
3839 return field;
3840 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 }
3842}
3843
Andreas Gampe896df402014-10-20 22:25:29 -07003844template <MethodVerifier::FieldAccessType kAccType>
3845void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3846 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003847 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003848 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003849 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003850 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003851 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003853 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003854 if (UNLIKELY(have_pending_hard_failure_)) {
3855 return;
3856 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003857 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003858 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003859 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003860 if (kAccType == FieldAccessType::kAccPut) {
3861 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3862 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3863 << " from other class " << GetDeclaringClass();
3864 return;
3865 }
3866 }
3867
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003868 mirror::Class* field_type_class;
3869 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003870 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003871 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08003872 field_type_class = h_field->GetType(can_load_classes_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003873 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003874 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003875 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003876 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003877 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003878 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3879 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003880 }
Ian Rogers0d604842012-04-16 14:50:24 -07003881 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003882 if (field_type == nullptr) {
3883 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3884 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003885 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003886 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003887 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003888 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003889 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3890 "Unexpected third access type");
3891 if (kAccType == FieldAccessType::kAccPut) {
3892 // sput or iput.
3893 if (is_primitive) {
3894 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003895 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003896 if (!insn_type.IsAssignableFrom(*field_type)) {
3897 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3898 << " to be compatible with type '" << insn_type
3899 << "' but found type '" << *field_type
3900 << "' in put-object";
3901 return;
3902 }
3903 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003904 }
Andreas Gampe896df402014-10-20 22:25:29 -07003905 } else if (kAccType == FieldAccessType::kAccGet) {
3906 // sget or iget.
3907 if (is_primitive) {
3908 if (field_type->Equals(insn_type) ||
3909 (field_type->IsFloat() && insn_type.IsInteger()) ||
3910 (field_type->IsDouble() && insn_type.IsLong())) {
3911 // expected that read is of the correct primitive type or that int reads are reading
3912 // floats or long reads are reading doubles
3913 } else {
3914 // This is a global failure rather than a class change failure as the instructions and
3915 // the descriptors for the type should have been consistent within the same file at
3916 // compile time
3917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3918 << " to be of type '" << insn_type
3919 << "' but found type '" << *field_type << "' in get";
3920 return;
3921 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003922 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003923 if (!insn_type.IsAssignableFrom(*field_type)) {
3924 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3925 << " to be compatible with type '" << insn_type
3926 << "' but found type '" << *field_type
3927 << "' in get-object";
3928 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3929 return;
3930 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003931 }
Andreas Gampe896df402014-10-20 22:25:29 -07003932 if (!field_type->IsLowHalf()) {
3933 work_line_->SetRegisterType(this, vregA, *field_type);
3934 } else {
3935 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3936 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003937 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003938 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003939 }
3940}
3941
Brian Carlstromea46f952013-07-30 01:26:50 -07003942mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003943 RegisterLine* reg_line) {
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +00003944 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3945 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3946 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3947 inst->Opcode() == Instruction::IGET_BOOLEAN_QUICK ||
3948 inst->Opcode() == Instruction::IGET_BYTE_QUICK ||
3949 inst->Opcode() == Instruction::IGET_CHAR_QUICK ||
3950 inst->Opcode() == Instruction::IGET_SHORT_QUICK ||
3951 inst->Opcode() == Instruction::IPUT_QUICK ||
3952 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3953 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3954 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3955 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3956 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3957 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003958 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003959 if (!object_type.HasClass()) {
3960 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3961 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003962 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003963 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +00003964 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3965 field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003966 if (f == nullptr) {
3967 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3968 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3969 }
3970 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003971}
3972
Andreas Gampe896df402014-10-20 22:25:29 -07003973template <MethodVerifier::FieldAccessType kAccType>
3974void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
3975 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003976 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003977
Brian Carlstromea46f952013-07-30 01:26:50 -07003978 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003979 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3981 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003982 }
Andreas Gampe896df402014-10-20 22:25:29 -07003983
3984 // For an IPUT_QUICK, we now test for final flag of the field.
3985 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003986 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3987 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003988 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003989 return;
3990 }
3991 }
Andreas Gampe896df402014-10-20 22:25:29 -07003992
3993 // Get the field type.
3994 const RegType* field_type;
3995 {
3996 mirror::Class* field_type_class;
3997 {
3998 StackHandleScope<1> hs(Thread::Current());
3999 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08004000 field_type_class = h_field->GetType(can_load_classes_);
Andreas Gampe896df402014-10-20 22:25:29 -07004001 }
4002
4003 if (field_type_class != nullptr) {
4004 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4005 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004006 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004007 Thread* self = Thread::Current();
4008 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4009 self->ClearException();
4010 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4011 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004012 }
Andreas Gampe896df402014-10-20 22:25:29 -07004013 if (field_type == nullptr) {
4014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004015 return;
4016 }
Andreas Gampe896df402014-10-20 22:25:29 -07004017 }
4018
4019 const uint32_t vregA = inst->VRegA_22c();
4020 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4021 "Unexpected third access type");
4022 if (kAccType == FieldAccessType::kAccPut) {
4023 if (is_primitive) {
4024 // Primitive field assignability rules are weaker than regular assignability rules
4025 bool instruction_compatible;
4026 bool value_compatible;
4027 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4028 if (field_type->IsIntegralTypes()) {
4029 instruction_compatible = insn_type.IsIntegralTypes();
4030 value_compatible = value_type.IsIntegralTypes();
4031 } else if (field_type->IsFloat()) {
4032 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4033 value_compatible = value_type.IsFloatTypes();
4034 } else if (field_type->IsLong()) {
4035 instruction_compatible = insn_type.IsLong();
4036 value_compatible = value_type.IsLongTypes();
4037 } else if (field_type->IsDouble()) {
4038 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4039 value_compatible = value_type.IsDoubleTypes();
4040 } else {
4041 instruction_compatible = false; // reference field with primitive store
4042 value_compatible = false; // unused
4043 }
4044 if (!instruction_compatible) {
4045 // This is a global failure rather than a class change failure as the instructions and
4046 // the descriptors for the type should have been consistent within the same file at
4047 // compile time
4048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4049 << " to be of type '" << insn_type
4050 << "' but found type '" << *field_type
4051 << "' in put";
4052 return;
4053 }
4054 if (!value_compatible) {
4055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4056 << " of type " << value_type
4057 << " but expected " << *field_type
4058 << " for store to " << PrettyField(field) << " in put";
4059 return;
4060 }
4061 } else {
4062 if (!insn_type.IsAssignableFrom(*field_type)) {
4063 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4064 << " to be compatible with type '" << insn_type
4065 << "' but found type '" << *field_type
4066 << "' in put-object";
4067 return;
4068 }
4069 work_line_->VerifyRegisterType(this, vregA, *field_type);
4070 }
4071 } else if (kAccType == FieldAccessType::kAccGet) {
4072 if (is_primitive) {
4073 if (field_type->Equals(insn_type) ||
4074 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4075 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4076 // expected that read is of the correct primitive type or that int reads are reading
4077 // floats or long reads are reading doubles
4078 } else {
4079 // This is a global failure rather than a class change failure as the instructions and
4080 // the descriptors for the type should have been consistent within the same file at
4081 // compile time
4082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4083 << " to be of type '" << insn_type
4084 << "' but found type '" << *field_type << "' in Get";
4085 return;
4086 }
4087 } else {
4088 if (!insn_type.IsAssignableFrom(*field_type)) {
4089 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4090 << " to be compatible with type '" << insn_type
4091 << "' but found type '" << *field_type
4092 << "' in get-object";
4093 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4094 return;
4095 }
4096 }
4097 if (!field_type->IsLowHalf()) {
4098 work_line_->SetRegisterType(this, vregA, *field_type);
4099 } else {
4100 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004101 }
4102 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004103 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004104 }
4105}
4106
Ian Rogers776ac1f2012-04-13 23:36:36 -07004107bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004108 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004110 return false;
4111 }
4112 return true;
4113}
4114
Stephen Kyle9bc61992014-09-22 13:53:15 +01004115bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4116 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4117 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4119 return false;
4120 }
4121 return true;
4122}
4123
4124bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4125 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4126}
4127
Ian Rogersebbdd872014-07-07 23:53:08 -07004128bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4129 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004130 bool changed = true;
4131 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4132 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004133 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004134 * We haven't processed this instruction before, and we haven't touched the registers here, so
4135 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4136 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004137 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004138 if (!insn_flags_[next_insn].IsReturn()) {
4139 target_line->CopyFromLine(merge_line);
4140 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004141 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004142 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004143 return false;
4144 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004145 // For returns we only care about the operand to the return, all other registers are dead.
4146 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4147 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4148 Instruction::Code opcode = ret_inst->Opcode();
4149 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004150 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004151 } else {
4152 target_line->CopyFromLine(merge_line);
4153 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004154 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004155 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004156 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004157 }
4158 }
4159 }
jeffhaobdb76512011-09-07 11:43:16 -07004160 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004161 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004162 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004163 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004164 if (gDebugVerify) {
4165 copy->CopyFromLine(target_line);
4166 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004167 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004168 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004169 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004170 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004171 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004172 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004173 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004174 << copy->Dump(this) << " MERGE\n"
4175 << merge_line->Dump(this) << " ==\n"
4176 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004177 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004178 if (update_merge_line && changed) {
4179 merge_line->CopyFromLine(target_line);
4180 }
jeffhaobdb76512011-09-07 11:43:16 -07004181 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004182 if (changed) {
4183 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004184 }
4185 return true;
4186}
4187
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004188InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004189 return &insn_flags_[work_insn_idx_];
4190}
4191
Ian Rogersd8f69b02014-09-10 21:43:52 +00004192const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004193 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004194 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004195 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004196 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004197 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4198 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004199 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004200 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004201 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4202 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004203 }
4204 }
4205 if (return_type_ == nullptr) {
4206 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4207 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4208 uint16_t return_type_idx = proto_id.return_type_idx_;
4209 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004210 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004211 }
4212 }
4213 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004214}
4215
Ian Rogersd8f69b02014-09-10 21:43:52 +00004216const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004217 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004218 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004219 const char* descriptor
4220 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004221 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004222 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004223 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4224 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004225 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004226 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004227 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004228 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004229 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004230}
4231
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004232std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4233 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004234 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004235 std::vector<int32_t> result;
4236 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004237 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004238 if (type.IsConstant()) {
4239 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004240 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4241 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004242 } else if (type.IsConstantLo()) {
4243 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004244 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4245 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004246 } else if (type.IsConstantHi()) {
4247 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004248 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4249 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004250 } else if (type.IsIntegralTypes()) {
4251 result.push_back(kIntVReg);
4252 result.push_back(0);
4253 } else if (type.IsFloat()) {
4254 result.push_back(kFloatVReg);
4255 result.push_back(0);
4256 } else if (type.IsLong()) {
4257 result.push_back(kLongLoVReg);
4258 result.push_back(0);
4259 result.push_back(kLongHiVReg);
4260 result.push_back(0);
4261 ++i;
4262 } else if (type.IsDouble()) {
4263 result.push_back(kDoubleLoVReg);
4264 result.push_back(0);
4265 result.push_back(kDoubleHiVReg);
4266 result.push_back(0);
4267 ++i;
4268 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4269 result.push_back(kUndefined);
4270 result.push_back(0);
4271 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004272 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004273 result.push_back(kReferenceVReg);
4274 result.push_back(0);
4275 }
4276 }
4277 return result;
4278}
4279
Ian Rogersd8f69b02014-09-10 21:43:52 +00004280const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004281 if (precise) {
4282 // Precise constant type.
4283 return reg_types_.FromCat1Const(value, true);
4284 } else {
4285 // Imprecise constant type.
4286 if (value < -32768) {
4287 return reg_types_.IntConstant();
4288 } else if (value < -128) {
4289 return reg_types_.ShortConstant();
4290 } else if (value < 0) {
4291 return reg_types_.ByteConstant();
4292 } else if (value == 0) {
4293 return reg_types_.Zero();
4294 } else if (value == 1) {
4295 return reg_types_.One();
4296 } else if (value < 128) {
4297 return reg_types_.PosByteConstant();
4298 } else if (value < 32768) {
4299 return reg_types_.PosShortConstant();
4300 } else if (value < 65536) {
4301 return reg_types_.CharConstant();
4302 } else {
4303 return reg_types_.IntConstant();
4304 }
4305 }
4306}
4307
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004308void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004309 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004310}
4311
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004312void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004313 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004314}
jeffhaod1224c72012-02-29 13:43:08 -08004315
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004316void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4317 RegTypeCache::VisitStaticRoots(callback, arg);
4318}
4319
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004320void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4321 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004322}
4323
Ian Rogersd81871c2011-10-03 13:57:23 -07004324} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004325} // namespace art