blob: 66846b500d3eefe0413c3a38a34dc24858dce80c [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;
139 if (Runtime::Current()->IsCompiler()) {
140 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,
289 need_precise_constants);
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,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700355 bool need_precise_constants, bool verify_to_dump)
Ian Rogers7b078e82014-09-10 14:44:24 -0700356 : self_(self),
357 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800358 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800359 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700360 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700362 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800363 dex_file_(dex_file),
364 dex_cache_(dex_cache),
365 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700366 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800367 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700368 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700369 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700370 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700371 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700372 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800373 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800374 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700375 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200376 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700377 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200378 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700379 has_virtual_or_interface_invokes_(false),
380 verify_to_dump_(verify_to_dump) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800381 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700382 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800383}
384
Mathieu Chartier590fee92013-09-13 13:46:47 -0700385MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800386 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700387 STLDeleteElements(&failure_messages_);
388}
389
Brian Carlstromea46f952013-07-30 01:26:50 -0700390void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700391 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700392 Thread* self = Thread::Current();
393 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700394 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
395 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700396 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700397 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700398 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
399 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700400 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700401 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700402 verifier.FindLocksAtDexPc();
403}
404
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700405static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
406 const Instruction* inst = Instruction::At(code_item->insns_);
407
408 uint32_t insns_size = code_item->insns_size_in_code_units_;
409 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
410 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
411 return true;
412 }
413
414 dex_pc += inst->SizeInCodeUnits();
415 inst = inst->Next();
416 }
417
418 return false;
419}
420
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700421void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700422 CHECK(monitor_enter_dex_pcs_ != nullptr);
423 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700424
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700425 // Quick check whether there are any monitor_enter instructions at all.
426 if (!HasMonitorEnterInstructions(code_item_)) {
427 return;
428 }
429
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700430 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
431 // verification. In practice, the phase we want relies on data structures set up by all the
432 // earlier passes, so we just run the full method verification and bail out early when we've
433 // got what we wanted.
434 Verify();
435}
436
Brian Carlstromea46f952013-07-30 01:26:50 -0700437mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700438 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700439 Thread* self = Thread::Current();
440 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700441 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
442 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700443 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700444 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700445 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
446 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200447 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200448}
449
Brian Carlstromea46f952013-07-30 01:26:50 -0700450mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700451 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200452
453 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
454 // verification. In practice, the phase we want relies on data structures set up by all the
455 // earlier passes, so we just run the full method verification and bail out early when we've
456 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200457 bool success = Verify();
458 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700459 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200460 }
461 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700462 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700463 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200464 }
465 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
466 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200467}
468
Brian Carlstromea46f952013-07-30 01:26:50 -0700469mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700470 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700471 Thread* self = Thread::Current();
472 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700473 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
474 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700475 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700476 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700477 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
478 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200479 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200480}
481
Brian Carlstromea46f952013-07-30 01:26:50 -0700482mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700483 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200484
485 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
486 // verification. In practice, the phase we want relies on data structures set up by all the
487 // earlier passes, so we just run the full method verification and bail out early when we've
488 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200489 bool success = Verify();
490 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700491 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200492 }
493 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700494 if (register_line == nullptr) {
495 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200496 }
497 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
498 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
499 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200500}
501
Ian Rogersad0b3a32012-04-16 14:50:24 -0700502bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700503 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700504 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700505 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700507 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 } else {
509 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700510 }
jeffhaobdb76512011-09-07 11:43:16 -0700511 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
513 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
515 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700517 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700518 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800519 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 // Run through the instructions and see if the width checks out.
521 bool result = ComputeWidthsAndCountOps();
522 // Flag instructions guarded by a "try" block and check exception handlers.
523 result = result && ScanTryCatchBlocks();
524 // Perform static instruction verification.
525 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700526 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000527 result = result && VerifyCodeFlow();
528 // Compute information for compiler.
529 if (result && Runtime::Current()->IsCompiler()) {
530 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
531 }
532 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700533}
534
Ian Rogers776ac1f2012-04-13 23:36:36 -0700535std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700536 switch (error) {
537 case VERIFY_ERROR_NO_CLASS:
538 case VERIFY_ERROR_NO_FIELD:
539 case VERIFY_ERROR_NO_METHOD:
540 case VERIFY_ERROR_ACCESS_CLASS:
541 case VERIFY_ERROR_ACCESS_FIELD:
542 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700543 case VERIFY_ERROR_INSTANTIATION:
544 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800545 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700546 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
547 // class change and instantiation errors into soft verification errors so that we re-verify
548 // at runtime. We may fail to find or to agree on access because of not yet available class
549 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
550 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
551 // paths" that dynamically perform the verification and cause the behavior to be that akin
552 // to an interpreter.
553 error = VERIFY_ERROR_BAD_CLASS_SOFT;
554 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700555 // If we fail again at runtime, mark that this instruction would throw and force this
556 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700557 have_pending_runtime_throw_failure_ = true;
558 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560 // Indication that verification should be retried at runtime.
561 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700562 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700563 have_pending_hard_failure_ = true;
564 }
565 break;
jeffhaod5347e02012-03-22 17:25:05 -0700566 // Hard verification failures at compile time will still fail at runtime, so the class is
567 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700568 case VERIFY_ERROR_BAD_CLASS_HARD: {
569 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700570 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000571 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800572 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700573 have_pending_hard_failure_ = true;
574 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800575 }
576 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700578 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700579 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700580 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700581 failure_messages_.push_back(failure_message);
582 return *failure_message;
583}
584
Ian Rogers576ca0c2014-06-06 15:58:22 -0700585std::ostream& MethodVerifier::LogVerifyInfo() {
586 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
587 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
588}
589
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
591 size_t failure_num = failure_messages_.size();
592 DCHECK_NE(failure_num, 0U);
593 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
594 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700595 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700596 delete last_fail_message;
597}
598
599void MethodVerifier::AppendToLastFailMessage(std::string append) {
600 size_t failure_num = failure_messages_.size();
601 DCHECK_NE(failure_num, 0U);
602 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
603 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800604}
605
Ian Rogers776ac1f2012-04-13 23:36:36 -0700606bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 const uint16_t* insns = code_item_->insns_;
608 size_t insns_size = code_item_->insns_size_in_code_units_;
609 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700610 size_t new_instance_count = 0;
611 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700613
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700615 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700616 switch (opcode) {
617 case Instruction::APUT_OBJECT:
618 case Instruction::CHECK_CAST:
619 has_check_casts_ = true;
620 break;
621 case Instruction::INVOKE_VIRTUAL:
622 case Instruction::INVOKE_VIRTUAL_RANGE:
623 case Instruction::INVOKE_INTERFACE:
624 case Instruction::INVOKE_INTERFACE_RANGE:
625 has_virtual_or_interface_invokes_ = true;
626 break;
627 case Instruction::MONITOR_ENTER:
628 monitor_enter_count++;
629 break;
630 case Instruction::NEW_INSTANCE:
631 new_instance_count++;
632 break;
633 default:
634 break;
jeffhaobdb76512011-09-07 11:43:16 -0700635 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700637 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700639 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700640 }
641
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
644 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700645 return false;
646 }
647
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 new_instance_count_ = new_instance_count;
649 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700650 return true;
651}
652
Ian Rogers776ac1f2012-04-13 23:36:36 -0700653bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700655 if (tries_size == 0) {
656 return true;
657 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700659 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700660
661 for (uint32_t idx = 0; idx < tries_size; idx++) {
662 const DexFile::TryItem* try_item = &tries[idx];
663 uint32_t start = try_item->start_addr_;
664 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700665 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
667 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700668 return false;
669 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700671 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
672 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700673 return false;
674 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700675 uint32_t dex_pc = start;
676 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
677 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700679 size_t insn_size = inst->SizeInCodeUnits();
680 dex_pc += insn_size;
681 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700682 }
683 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800684 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700685 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700686 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700687 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700688 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700689 CatchHandlerIterator iterator(handlers_ptr);
690 for (; iterator.HasNext(); iterator.Next()) {
691 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700693 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
694 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700695 return false;
696 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100697 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
698 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
699 << "exception handler begins with move-result* (" << dex_pc << ")";
700 return false;
701 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700703 // Ensure exception types are resolved so that they don't need resolution to be delivered,
704 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700705 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800706 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
707 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700708 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700709 if (exception_type == nullptr) {
710 DCHECK(self_->IsExceptionPending());
711 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700712 }
713 }
jeffhaobdb76512011-09-07 11:43:16 -0700714 }
Ian Rogers0571d352011-11-03 19:51:38 -0700715 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700716 }
jeffhaobdb76512011-09-07 11:43:16 -0700717 return true;
718}
719
Ian Rogers776ac1f2012-04-13 23:36:36 -0700720bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700722
Ian Rogers0c7abda2012-09-19 13:33:42 -0700723 /* 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 -0700724 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700725 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700726
727 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700728 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700730 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 return false;
732 }
733 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700734 // All invoke points are marked as "Throw" points already.
735 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000736 if (inst->IsBranch()) {
737 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
738 // The compiler also needs safepoints for fall-through to loop heads.
739 // Such a loop head must be a target of a branch.
740 int32_t offset = 0;
741 bool cond, self_ok;
742 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
743 DCHECK(target_ok);
744 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
745 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700746 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000747 } else if (inst->IsReturn()) {
748 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 }
750 dex_pc += inst->SizeInCodeUnits();
751 inst = inst->Next();
752 }
753 return true;
754}
755
Ian Rogers776ac1f2012-04-13 23:36:36 -0700756bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 bool result = true;
758 switch (inst->GetVerifyTypeArgumentA()) {
759 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700760 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 break;
762 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700763 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 break;
765 }
766 switch (inst->GetVerifyTypeArgumentB()) {
767 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700768 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 break;
770 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700771 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 break;
773 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700774 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 break;
776 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700777 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 break;
779 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700780 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 break;
782 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700783 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 break;
785 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700786 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 break;
788 }
789 switch (inst->GetVerifyTypeArgumentC()) {
790 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700791 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 break;
793 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700794 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 break;
796 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700797 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 break;
799 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700800 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 break;
802 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700803 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 break;
805 }
806 switch (inst->GetVerifyExtraFlags()) {
807 case Instruction::kVerifyArrayData:
808 result = result && CheckArrayData(code_offset);
809 break;
810 case Instruction::kVerifyBranchTarget:
811 result = result && CheckBranchTarget(code_offset);
812 break;
813 case Instruction::kVerifySwitchTargets:
814 result = result && CheckSwitchTargets(code_offset);
815 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700816 case Instruction::kVerifyVarArgNonZero:
817 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700818 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700819 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
821 "non-range invoke";
822 return false;
823 }
Ian Rogers29a26482014-05-02 15:27:29 -0700824 uint32_t args[Instruction::kMaxVarArgRegs];
825 inst->GetVarArgs(args);
826 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700828 }
Andreas Gampec3314312014-06-19 18:13:29 -0700829 case Instruction::kVerifyVarArgRangeNonZero:
830 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700832 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
833 inst->VRegA() <= 0) {
834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
835 "range invoke";
836 return false;
837 }
Ian Rogers29a26482014-05-02 15:27:29 -0700838 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 break;
840 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 result = false;
843 break;
844 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700845 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
847 result = false;
848 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 return result;
850}
851
Ian Rogers7b078e82014-09-10 14:44:24 -0700852inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
855 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 return false;
857 }
858 return true;
859}
860
Ian Rogers7b078e82014-09-10 14:44:24 -0700861inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
864 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 return false;
866 }
867 return true;
868}
869
Ian Rogers7b078e82014-09-10 14:44:24 -0700870inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
873 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 return false;
875 }
876 return true;
877}
878
Ian Rogers7b078e82014-09-10 14:44:24 -0700879inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
882 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 return false;
884 }
885 return true;
886}
887
Ian Rogers7b078e82014-09-10 14:44:24 -0700888inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
891 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 return false;
893 }
894 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700895 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 return false;
899 }
900 return true;
901}
902
Ian Rogers7b078e82014-09-10 14:44:24 -0700903inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
906 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 return false;
908 }
909 return true;
910}
911
Ian Rogers7b078e82014-09-10 14:44:24 -0700912inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
915 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 return false;
917 }
918 return true;
919}
920
Ian Rogers776ac1f2012-04-13 23:36:36 -0700921bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
924 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 return false;
926 }
927 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700928 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 const char* cp = descriptor;
930 while (*cp++ == '[') {
931 bracket_count++;
932 }
933 if (bracket_count == 0) {
934 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
936 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 return false;
938 } else if (bracket_count > 255) {
939 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700940 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
941 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 return false;
943 }
944 return true;
945}
946
Ian Rogers776ac1f2012-04-13 23:36:36 -0700947bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
949 const uint16_t* insns = code_item_->insns_ + cur_offset;
950 const uint16_t* array_data;
951 int32_t array_data_offset;
952
953 DCHECK_LT(cur_offset, insn_count);
954 /* make sure the start of the array data table is in range */
955 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
956 if ((int32_t) cur_offset + array_data_offset < 0 ||
957 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700959 << ", data offset " << array_data_offset
960 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 return false;
962 }
963 /* offset to array data table is a relative branch-style offset */
964 array_data = insns + array_data_offset;
965 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800966 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
968 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 return false;
970 }
971 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700972 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
974 /* make sure the end of the switch is in range */
975 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
977 << ", data offset " << array_data_offset << ", end "
978 << cur_offset + array_data_offset + table_size
979 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 return false;
981 }
982 return true;
983}
984
Ian Rogers776ac1f2012-04-13 23:36:36 -0700985bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 int32_t offset;
987 bool isConditional, selfOkay;
988 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
989 return false;
990 }
991 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
993 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return false;
995 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700996 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
997 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1000 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 return false;
1002 }
1003 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1004 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001005 if (abs_offset < 0 ||
1006 (uint32_t) abs_offset >= insn_count ||
1007 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001009 << reinterpret_cast<void*>(abs_offset) << ") at "
1010 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 return false;
1012 }
1013 insn_flags_[abs_offset].SetBranchTarget();
1014 return true;
1015}
1016
Ian Rogers776ac1f2012-04-13 23:36:36 -07001017bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 bool* selfOkay) {
1019 const uint16_t* insns = code_item_->insns_ + cur_offset;
1020 *pConditional = false;
1021 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001022 switch (*insns & 0xff) {
1023 case Instruction::GOTO:
1024 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001025 break;
1026 case Instruction::GOTO_32:
1027 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001028 *selfOkay = true;
1029 break;
1030 case Instruction::GOTO_16:
1031 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001032 break;
1033 case Instruction::IF_EQ:
1034 case Instruction::IF_NE:
1035 case Instruction::IF_LT:
1036 case Instruction::IF_GE:
1037 case Instruction::IF_GT:
1038 case Instruction::IF_LE:
1039 case Instruction::IF_EQZ:
1040 case Instruction::IF_NEZ:
1041 case Instruction::IF_LTZ:
1042 case Instruction::IF_GEZ:
1043 case Instruction::IF_GTZ:
1044 case Instruction::IF_LEZ:
1045 *pOffset = (int16_t) insns[1];
1046 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001047 break;
1048 default:
1049 return false;
1050 break;
1051 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001052 return true;
1053}
1054
Ian Rogers776ac1f2012-04-13 23:36:36 -07001055bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001057 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001059 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1061 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001063 << ", switch offset " << switch_offset
1064 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001065 return false;
1066 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001067 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001069 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001070 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1072 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001073 return false;
1074 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 uint32_t switch_count = switch_insns[1];
1076 int32_t keys_offset, targets_offset;
1077 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001078 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1079 /* 0=sig, 1=count, 2/3=firstKey */
1080 targets_offset = 4;
1081 keys_offset = -1;
1082 expected_signature = Instruction::kPackedSwitchSignature;
1083 } else {
1084 /* 0=sig, 1=count, 2..count*2 = keys */
1085 keys_offset = 2;
1086 targets_offset = 2 + 2 * switch_count;
1087 expected_signature = Instruction::kSparseSwitchSignature;
1088 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001090 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001091 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1092 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1093 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001094 return false;
1095 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001096 /* make sure the end of the switch is in range */
1097 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1099 << ", switch offset " << switch_offset
1100 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001101 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001102 return false;
1103 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001104 /* for a sparse switch, verify the keys are in ascending order */
1105 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1107 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001108 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1109 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1110 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1112 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 return false;
1114 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001115 last_key = key;
1116 }
1117 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001118 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 for (uint32_t targ = 0; targ < switch_count; targ++) {
1120 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1121 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1122 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001123 if (abs_offset < 0 ||
1124 abs_offset >= (int32_t) insn_count ||
1125 !insn_flags_[abs_offset].IsOpcode()) {
1126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1127 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1128 << reinterpret_cast<void*>(cur_offset)
1129 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 return false;
1131 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 insn_flags_[abs_offset].SetBranchTarget();
1133 }
1134 return true;
1135}
1136
Ian Rogers776ac1f2012-04-13 23:36:36 -07001137bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001138 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 return false;
1141 }
1142 uint16_t registers_size = code_item_->registers_size_;
1143 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001144 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1146 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 return false;
1148 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001149 }
1150
1151 return true;
1152}
1153
Ian Rogers776ac1f2012-04-13 23:36:36 -07001154bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 uint16_t registers_size = code_item_->registers_size_;
1156 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1157 // integer overflow when adding them here.
1158 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1160 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001161 return false;
1162 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001163 return true;
1164}
1165
Ian Rogers776ac1f2012-04-13 23:36:36 -07001166bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 uint16_t registers_size = code_item_->registers_size_;
1168 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001169
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001171 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1172 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 }
1174 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001175 reg_table_.Init(kTrackCompilerInterestPoints,
1176 insn_flags_.get(),
1177 insns_size,
1178 registers_size,
1179 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001180
jeffhaobdb76512011-09-07 11:43:16 -07001181
Ian Rogersd0fbd852013-09-24 18:17:04 -07001182 work_line_.reset(RegisterLine::Create(registers_size, this));
1183 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001184
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 /* Initialize register types of method arguments. */
1186 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001187 DCHECK_NE(failures_.size(), 0U);
1188 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001189 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001190 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 return false;
1192 }
1193 /* Perform code flow verification. */
1194 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001195 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001197 }
jeffhaobdb76512011-09-07 11:43:16 -07001198 return true;
1199}
1200
Ian Rogersad0b3a32012-04-16 14:50:24 -07001201std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1202 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001203 for (size_t i = 0; i < failures_.size(); ++i) {
1204 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001205 }
1206 return os;
1207}
1208
Ian Rogers776ac1f2012-04-13 23:36:36 -07001209void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001210 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001211 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 return;
jeffhaobdb76512011-09-07 11:43:16 -07001213 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001214 {
1215 os << "Register Types:\n";
1216 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1217 std::ostream indent_os(&indent_filter);
1218 reg_types_.Dump(indent_os);
1219 }
Ian Rogersb4903572012-10-11 11:52:56 -07001220 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001221 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1222 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 const Instruction* inst = Instruction::At(code_item_->insns_);
1224 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001225 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001227 if (reg_line != nullptr) {
1228 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001229 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001230 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001231 const bool kDumpHexOfInstruction = false;
1232 if (kDumpHexOfInstruction) {
1233 indent_os << inst->DumpHex(5) << " ";
1234 }
1235 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001236 inst = inst->Next();
1237 }
jeffhaobdb76512011-09-07 11:43:16 -07001238}
1239
Ian Rogersd81871c2011-10-03 13:57:23 -07001240static bool IsPrimitiveDescriptor(char descriptor) {
1241 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001242 case 'I':
1243 case 'C':
1244 case 'S':
1245 case 'B':
1246 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001247 case 'F':
1248 case 'D':
1249 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001251 default:
1252 return false;
1253 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001254}
1255
Ian Rogers776ac1f2012-04-13 23:36:36 -07001256bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 RegisterLine* reg_line = reg_table_.GetLine(0);
1258 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1259 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001260
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001262 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001264 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1266 // argument as uninitialized. This restricts field access until the superclass constructor is
1267 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001268 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001269 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001270 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 reg_types_.UninitializedThisArgument(declaring_class));
1272 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001273 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001274 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001275 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001276 }
1277
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001278 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001280 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001281
1282 for (; iterator.HasNext(); iterator.Next()) {
1283 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001284 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 LOG(FATAL) << "Null descriptor";
1286 }
1287 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1289 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 return false;
1291 }
1292 switch (descriptor[0]) {
1293 case 'L':
1294 case '[':
1295 // We assume that reference arguments are initialized. The only way it could be otherwise
1296 // (assuming the caller was verified) is if the current method is <init>, but in that case
1297 // it's effectively considered initialized the instant we reach here (in the sense that we
1298 // can return without doing anything or call virtual methods).
1299 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001300 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001301 if (!reg_type.IsNonZeroReferenceTypes()) {
1302 DCHECK(HasFailures());
1303 return false;
1304 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001305 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 }
1307 break;
1308 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001309 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 break;
1311 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001312 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 break;
1314 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001315 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 break;
1317 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001318 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 break;
1320 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001321 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 break;
1323 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001324 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 break;
1326 case 'J':
1327 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001328 if (cur_arg + 1 >= expected_args) {
1329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1330 << " args, found more (" << descriptor << ")";
1331 return false;
1332 }
1333
Ian Rogers7b078e82014-09-10 14:44:24 -07001334 const RegType* lo_half;
1335 const RegType* hi_half;
1336 if (descriptor[0] == 'J') {
1337 lo_half = &reg_types_.LongLo();
1338 hi_half = &reg_types_.LongHi();
1339 } else {
1340 lo_half = &reg_types_.DoubleLo();
1341 hi_half = &reg_types_.DoubleHi();
1342 }
1343 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 cur_arg++;
1345 break;
1346 }
1347 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1349 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 return false;
1351 }
1352 cur_arg++;
1353 }
1354 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1356 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 return false;
1358 }
1359 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1360 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1361 // format. Only major difference from the method argument format is that 'V' is supported.
1362 bool result;
1363 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1364 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001365 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 size_t i = 0;
1367 do {
1368 i++;
1369 } while (descriptor[i] == '['); // process leading [
1370 if (descriptor[i] == 'L') { // object array
1371 do {
1372 i++; // find closing ;
1373 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1374 result = descriptor[i] == ';';
1375 } else { // primitive array
1376 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1377 }
1378 } else if (descriptor[0] == 'L') {
1379 // could be more thorough here, but shouldn't be required
1380 size_t i = 0;
1381 do {
1382 i++;
1383 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1384 result = descriptor[i] == ';';
1385 } else {
1386 result = false;
1387 }
1388 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1390 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 }
1392 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001393}
1394
Ian Rogers776ac1f2012-04-13 23:36:36 -07001395bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 const uint16_t* insns = code_item_->insns_;
1397 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001398
jeffhaobdb76512011-09-07 11:43:16 -07001399 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 insn_flags_[0].SetChanged();
1401 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001402
jeffhaobdb76512011-09-07 11:43:16 -07001403 /* Continue until no instructions are marked "changed". */
1404 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001405 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1407 uint32_t insn_idx = start_guess;
1408 for (; insn_idx < insns_size; insn_idx++) {
1409 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001410 break;
1411 }
jeffhaobdb76512011-09-07 11:43:16 -07001412 if (insn_idx == insns_size) {
1413 if (start_guess != 0) {
1414 /* try again, starting from the top */
1415 start_guess = 0;
1416 continue;
1417 } else {
1418 /* all flags are clear */
1419 break;
1420 }
1421 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001422 // We carry the working set of registers from instruction to instruction. If this address can
1423 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1424 // "changed" flags, we need to load the set of registers from the table.
1425 // Because we always prefer to continue on to the next instruction, we should never have a
1426 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1427 // target.
1428 work_insn_idx_ = insn_idx;
1429 if (insn_flags_[insn_idx].IsBranchTarget()) {
1430 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001431 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001432 /*
1433 * Sanity check: retrieve the stored register line (assuming
1434 * a full table) and make sure it actually matches.
1435 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001437 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 if (work_line_->CompareLine(register_line) != 0) {
1439 Dump(std::cout);
1440 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001442 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001443 << " work_line=" << work_line_->Dump(this) << "\n"
1444 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 }
jeffhaobdb76512011-09-07 11:43:16 -07001446 }
jeffhaobdb76512011-09-07 11:43:16 -07001447 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001449 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001450 prepend += " failed to verify: ";
1451 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 return false;
1453 }
jeffhaobdb76512011-09-07 11:43:16 -07001454 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 insn_flags_[insn_idx].SetVisited();
1456 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001457 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001458
Ian Rogers1c849e52012-06-28 14:00:33 -07001459 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001460 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001461 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001462 * (besides the wasted space), but it indicates a flaw somewhere
1463 * down the line, possibly in the verifier.
1464 *
1465 * If we've substituted "always throw" instructions into the stream,
1466 * we are almost certainly going to have some dead code.
1467 */
1468 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001470 for (; insn_idx < insns_size;
1471 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001472 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001473 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001474 * may or may not be preceded by a padding NOP (for alignment).
1475 */
1476 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1477 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1478 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001479 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001480 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1481 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1482 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001484 }
1485
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001487 if (dead_start < 0)
1488 dead_start = insn_idx;
1489 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001490 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1491 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001492 dead_start = -1;
1493 }
1494 }
1495 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001496 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1497 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001498 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001499 // To dump the state of the verify after a method, do something like:
1500 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1501 // "boolean java.lang.String.equals(java.lang.Object)") {
1502 // LOG(INFO) << info_messages_.str();
1503 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001504 }
jeffhaobdb76512011-09-07 11:43:16 -07001505 return true;
1506}
1507
Ian Rogers776ac1f2012-04-13 23:36:36 -07001508bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001509 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1510 // We want the state _before_ the instruction, for the case where the dex pc we're
1511 // interested in is itself a monitor-enter instruction (which is a likely place
1512 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001513 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001514 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001515 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1516 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1517 }
1518 }
1519
jeffhaobdb76512011-09-07 11:43:16 -07001520 /*
1521 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001522 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001523 * control to another statement:
1524 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001525 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001526 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001527 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001528 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001529 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001530 * throw an exception that is handled by an encompassing "try"
1531 * block.
1532 *
1533 * We can also return, in which case there is no successor instruction
1534 * from this point.
1535 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001537 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1539 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001540 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001541
jeffhaobdb76512011-09-07 11:43:16 -07001542 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001543 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001544 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001546 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001547 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 }
jeffhaobdb76512011-09-07 11:43:16 -07001549
1550 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001551 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001552 * can throw an exception, we will copy/merge this into the "catch"
1553 * address rather than work_line, because we don't want the result
1554 * from the "successful" code path (e.g. a check-cast that "improves"
1555 * a type) to be visible to the exception handler.
1556 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001557 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001559 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001561 }
1562
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001563
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001564 // We need to ensure the work line is consistent while performing validation. When we spot a
1565 // peephole pattern we compute a new line for either the fallthrough instruction or the
1566 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001567 std::unique_ptr<RegisterLine> branch_line;
1568 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001569
Stephen Kyle7e541c92014-12-17 17:10:02 +00001570 /*
1571 * If we are in a constructor, and we currently have an UninitializedThis type
1572 * in a register somewhere, we need to make sure it isn't overwritten.
1573 */
1574 bool track_uninitialized_this = false;
1575 size_t uninitialized_this_loc = 0;
1576 if (IsConstructor()) {
1577 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1578 }
1579
Sebastien Hertz5243e912013-05-21 10:55:07 +02001580 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001581 case Instruction::NOP:
1582 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001583 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001584 * a signature that looks like a NOP; if we see one of these in
1585 * the course of executing code then we have a problem.
1586 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001587 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001589 }
1590 break;
1591
1592 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001593 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 break;
jeffhaobdb76512011-09-07 11:43:16 -07001595 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001596 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001597 break;
jeffhaobdb76512011-09-07 11:43:16 -07001598 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001599 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001600 break;
1601 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001602 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001603 break;
jeffhaobdb76512011-09-07 11:43:16 -07001604 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001605 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 break;
jeffhaobdb76512011-09-07 11:43:16 -07001607 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001608 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
1610 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001611 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001612 break;
jeffhaobdb76512011-09-07 11:43:16 -07001613 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001614 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001615 break;
jeffhaobdb76512011-09-07 11:43:16 -07001616 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001617 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001618 break;
1619
1620 /*
1621 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001622 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001623 * might want to hold the result in an actual CPU register, so the
1624 * Dalvik spec requires that these only appear immediately after an
1625 * invoke or filled-new-array.
1626 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001627 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001628 * redundant with the reset done below, but it can make the debug info
1629 * easier to read in some cases.)
1630 */
1631 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001632 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001633 break;
1634 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001635 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001636 break;
1637 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001638 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001639 break;
1640
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001642 /*
jeffhao60f83e32012-02-13 17:16:30 -08001643 * This statement can only appear as the first instruction in an exception handler. We verify
1644 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001645 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001646 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001647 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001648 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 }
jeffhaobdb76512011-09-07 11:43:16 -07001650 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001651 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001652 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 }
jeffhaobdb76512011-09-07 11:43:16 -07001655 }
1656 break;
1657 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001658 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001659 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001660 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1663 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 } else {
1665 // Compilers may generate synthetic functions that write byte values into boolean fields.
1666 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001668 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1670 ((return_type.IsBoolean() || return_type.IsByte() ||
1671 return_type.IsShort() || return_type.IsChar()) &&
1672 src_type.IsInteger()));
1673 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001674 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001675 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001676 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001677 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 }
jeffhaobdb76512011-09-07 11:43:16 -07001679 }
1680 }
1681 break;
1682 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001684 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001685 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 } else {
1689 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001691 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001692 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001693 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 }
1696 }
1697 break;
1698 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001699 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001700 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 } else {
1704 /* return_type is the *expected* return type, not register value */
1705 DCHECK(!return_type.IsZero());
1706 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001707 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001708 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001709 // Disallow returning uninitialized values and verify that the reference in vAA is an
1710 // instance of the "return_type"
1711 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1713 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001714 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001715 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1716 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1717 << "' or '" << reg_type << "'";
1718 } else {
1719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1720 << "', but expected from declaration '" << return_type << "'";
1721 }
jeffhaobdb76512011-09-07 11:43:16 -07001722 }
1723 }
1724 }
1725 break;
1726
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001727 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 case Instruction::CONST_4: {
1729 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001731 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001732 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001733 }
1734 case Instruction::CONST_16: {
1735 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001737 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001738 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001739 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001740 case Instruction::CONST: {
1741 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001742 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001743 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001744 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001745 }
1746 case Instruction::CONST_HIGH16: {
1747 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001748 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001749 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001750 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001751 }
jeffhaobdb76512011-09-07 11:43:16 -07001752 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001753 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001754 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001755 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1756 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001757 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001758 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001759 }
1760 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001762 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1763 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001764 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001765 break;
1766 }
1767 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001769 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1770 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001771 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001772 break;
1773 }
1774 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001775 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001776 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1777 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001779 break;
1780 }
jeffhaobdb76512011-09-07 11:43:16 -07001781 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001782 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 break;
jeffhaobdb76512011-09-07 11:43:16 -07001784 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001785 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001786 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001788 // Get type from instruction if unresolved then we need an access check
1789 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001790 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001791 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001792 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001793 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001794 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001795 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 }
jeffhaobdb76512011-09-07 11:43:16 -07001797 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001798 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001799 break;
1800 case Instruction::MONITOR_EXIT:
1801 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001802 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001803 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001804 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001805 * to the need to handle asynchronous exceptions, a now-deprecated
1806 * feature that Dalvik doesn't support.)
1807 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001808 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001809 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001810 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001811 * structured locking checks are working, the former would have
1812 * failed on the -enter instruction, and the latter is impossible.
1813 *
1814 * This is fortunate, because issue 3221411 prevents us from
1815 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001816 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001817 * some catch blocks (which will show up as "dead" code when
1818 * we skip them here); if we can't, then the code path could be
1819 * "live" so we still need to check it.
1820 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001821 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001822 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001823 break;
1824
Ian Rogers28ad40d2011-10-27 15:19:26 -07001825 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001827 /*
1828 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1829 * could be a "upcast" -- not expected, so we don't try to address it.)
1830 *
1831 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001832 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001833 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1835 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001836 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001837 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001838 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001839 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001840 if (klass != nullptr && klass->IsPrimitive()) {
1841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1842 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1843 << GetDeclaringClass();
1844 break;
1845 }
1846
Ian Rogersad0b3a32012-04-16 14:50:24 -07001847 DCHECK_NE(failures_.size(), 0U);
1848 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001849 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001850 }
1851 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001852 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001853 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001854 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001856 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 if (is_checkcast) {
1858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1859 } else {
1860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1861 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001862 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001863 if (is_checkcast) {
1864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1865 } else {
1866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1867 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001868 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001869 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001870 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001872 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001873 }
jeffhaobdb76512011-09-07 11:43:16 -07001874 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001875 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 }
1877 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001878 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001879 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001880 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001883 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001885 } else {
1886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 }
1888 break;
1889 }
1890 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001891 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001892 if (res_type.IsConflict()) {
1893 DCHECK_NE(failures_.size(), 0U);
1894 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001895 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001896 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1897 // can't create an instance of an interface or abstract class */
1898 if (!res_type.IsInstantiableTypes()) {
1899 Fail(VERIFY_ERROR_INSTANTIATION)
1900 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001901 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001903 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001904 // Any registers holding previous allocations from this address that have not yet been
1905 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001906 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001907 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 break;
1910 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001911 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001913 break;
1914 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001916 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001918 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001919 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001920 just_set_result = true; // Filled new array range sets result register
1921 break;
jeffhaobdb76512011-09-07 11:43:16 -07001922 case Instruction::CMPL_FLOAT:
1923 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001924 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001925 break;
1926 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001928 break;
1929 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001930 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001931 break;
1932 case Instruction::CMPL_DOUBLE:
1933 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001934 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001935 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001936 break;
1937 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001938 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001939 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001940 break;
1941 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001942 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001943 break;
1944 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001945 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001946 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001947 break;
1948 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001949 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001950 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001951 break;
1952 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001953 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001954 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001957 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001958 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1959 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001960 }
1961 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001962 }
jeffhaobdb76512011-09-07 11:43:16 -07001963 case Instruction::GOTO:
1964 case Instruction::GOTO_16:
1965 case Instruction::GOTO_32:
1966 /* no effect on or use of registers */
1967 break;
1968
1969 case Instruction::PACKED_SWITCH:
1970 case Instruction::SPARSE_SWITCH:
1971 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001972 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
1974
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 case Instruction::FILL_ARRAY_DATA: {
1976 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001977 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001978 /* array_type can be null if the reg type is Zero */
1979 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001980 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1982 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001983 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001984 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001985 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001986 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1988 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 } else {
jeffhao457cc512012-02-02 16:55:13 -08001990 // Now verify if the element width in the table matches the element width declared in
1991 // the array
1992 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1993 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001995 } else {
1996 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1997 // Since we don't compress the data in Dex, expect to see equal width of data stored
1998 // in the table and expected from the array class.
1999 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2001 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002002 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 }
2004 }
jeffhaobdb76512011-09-07 11:43:16 -07002005 }
2006 }
2007 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 }
jeffhaobdb76512011-09-07 11:43:16 -07002009 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002011 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2012 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 bool mismatch = false;
2014 if (reg_type1.IsZero()) { // zero then integral or reference expected
2015 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2016 } else if (reg_type1.IsReferenceTypes()) { // both references?
2017 mismatch = !reg_type2.IsReferenceTypes();
2018 } else { // both integral?
2019 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2020 }
2021 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2023 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002024 }
2025 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 }
jeffhaobdb76512011-09-07 11:43:16 -07002027 case Instruction::IF_LT:
2028 case Instruction::IF_GE:
2029 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002031 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2032 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2035 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002036 }
2037 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 }
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2044 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002046
2047 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002048 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002049 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002050 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002051 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002052 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002053 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002054 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2055 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2056 work_insn_idx_)) {
2057 break;
2058 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002059 } else {
2060 break;
2061 }
2062
Ian Rogers9b360392013-06-06 14:45:07 -07002063 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002064
2065 /* Check for peep-hole pattern of:
2066 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002067 * instance-of vX, vY, T;
2068 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002069 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002070 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002071 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002072 * and sharpen the type of vY to be type T.
2073 * Note, this pattern can't be if:
2074 * - if there are other branches to this branch,
2075 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002076 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002077 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002078 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2079 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2080 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002081 // Check the type of the instance-of is different than that of registers type, as if they
2082 // are the same there is no work to be done here. Check that the conversion is not to or
2083 // from an unresolved type as type information is imprecise. If the instance-of is to an
2084 // interface then ignore the type information as interfaces can only be treated as Objects
2085 // and we don't want to disallow field and other operations on the object. If the value
2086 // being instance-of checked against is known null (zero) then allow the optimization as
2087 // we didn't have type information. If the merge of the instance-of type with the original
2088 // type is assignable to the original then allow optimization. This check is performed to
2089 // ensure that subsequent merges don't lose type information - such as becoming an
2090 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002091 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002092 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002093
Ian Rogersebbdd872014-07-07 23:53:08 -07002094 if (!orig_type.Equals(cast_type) &&
2095 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002096 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002097 !cast_type.GetClass()->IsInterface() &&
2098 (orig_type.IsZero() ||
2099 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002100 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002101 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002102 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002103 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002104 branch_line.reset(update_line);
2105 }
2106 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002108 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2109 // See if instance-of was preceded by a move-object operation, common due to the small
2110 // register encoding space of instance-of, and propagate type information to the source
2111 // of the move-object.
2112 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002113 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002114 move_idx--;
2115 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002116 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2117 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2118 work_insn_idx_)) {
2119 break;
2120 }
Ian Rogers9b360392013-06-06 14:45:07 -07002121 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2122 switch (move_inst->Opcode()) {
2123 case Instruction::MOVE_OBJECT:
2124 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002126 }
2127 break;
2128 case Instruction::MOVE_OBJECT_FROM16:
2129 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002130 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002131 }
2132 break;
2133 case Instruction::MOVE_OBJECT_16:
2134 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002135 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002136 }
2137 break;
2138 default:
2139 break;
2140 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002141 }
2142 }
2143 }
2144
jeffhaobdb76512011-09-07 11:43:16 -07002145 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 }
jeffhaobdb76512011-09-07 11:43:16 -07002147 case Instruction::IF_LTZ:
2148 case Instruction::IF_GEZ:
2149 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002151 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2154 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 }
jeffhaobdb76512011-09-07 11:43:16 -07002156 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 }
jeffhaobdb76512011-09-07 11:43:16 -07002158 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 break;
jeffhaobdb76512011-09-07 11:43:16 -07002161 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 break;
jeffhaobdb76512011-09-07 11:43:16 -07002164 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 break;
jeffhaobdb76512011-09-07 11:43:16 -07002167 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 break;
jeffhaobdb76512011-09-07 11:43:16 -07002173 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 break;
2176 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002177 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 break;
2183 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002184 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 break;
2186 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 break;
2189 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002190 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002191 break;
2192 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002193 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
2195 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002196 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
2198 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
2201
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002203 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 break;
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002206 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 break;
jeffhaobdb76512011-09-07 11:43:16 -07002208 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002209 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 break;
jeffhaobdb76512011-09-07 11:43:16 -07002211 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002212 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 break;
2214 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002215 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002218 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002221 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2222 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 break;
jeffhaobdb76512011-09-07 11:43:16 -07002224
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002226 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 break;
2228 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002229 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 break;
2231 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002232 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 break;
2234 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002235 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
2237 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002238 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002239 break;
2240 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002241 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 break;
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002244 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2245 false);
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
2247
jeffhaobdb76512011-09-07 11:43:16 -07002248 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002249 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 break;
jeffhaobdb76512011-09-07 11:43:16 -07002251 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002252 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 break;
jeffhaobdb76512011-09-07 11:43:16 -07002254 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002255 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 break;
jeffhaobdb76512011-09-07 11:43:16 -07002257 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002258 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 break;
2260 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002261 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002262 break;
2263 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002264 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002265 break;
2266 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002267 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2268 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 break;
2270
2271 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002272 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 break;
2274 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002275 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
2277 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002278 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 break;
2280 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002281 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002284 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002287 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002290 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2291 true);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293
2294 case Instruction::INVOKE_VIRTUAL:
2295 case Instruction::INVOKE_VIRTUAL_RANGE:
2296 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2299 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002300 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2301 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002302 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2303 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002304 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002305 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002306 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002307 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002308 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002309 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002310 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2311 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002312 return_type_class->CannotBeAssignedFromOtherTypes());
2313 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002314 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2315 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002316 }
2317 }
2318 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002320 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2321 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002322 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002323 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002324 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002325 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002326 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002328 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002330 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 }
jeffhaobdb76512011-09-07 11:43:16 -07002333 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002336 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002337 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002338 const char* return_type_descriptor;
2339 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002340 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002341 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002343 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002344 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002345 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2346 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2347 } else {
2348 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002349 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002350 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002351 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002352 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002353 if (return_type_class != nullptr) {
2354 return_type = &reg_types_.FromClass(return_type_descriptor,
2355 return_type_class,
2356 return_type_class->CannotBeAssignedFromOtherTypes());
2357 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002358 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2359 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002360 }
Ian Rogers46685432012-06-03 22:26:43 -07002361 }
2362 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002363 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 * Some additional checks when calling a constructor. We know from the invocation arg check
2365 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2366 * that to require that called_method->klass is the same as this->klass or this->super,
2367 * allowing the latter only if the "this" argument is the same as the "this" argument to
2368 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002369 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002370 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002371 if (this_type.IsConflict()) // failure.
2372 break;
jeffhaobdb76512011-09-07 11:43:16 -07002373
jeffhaob57e9522012-04-26 18:08:21 -07002374 /* no null refs allowed (?) */
2375 if (this_type.IsZero()) {
2376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2377 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002378 }
jeffhaob57e9522012-04-26 18:08:21 -07002379
2380 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002381 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002382 // TODO: re-enable constructor type verification
2383 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002384 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002385 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2386 // break;
2387 // }
jeffhaob57e9522012-04-26 18:08:21 -07002388
2389 /* arg must be an uninitialized reference */
2390 if (!this_type.IsUninitializedTypes()) {
2391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2392 << this_type;
2393 break;
2394 }
2395
2396 /*
2397 * Replace the uninitialized reference with an initialized one. We need to do this for all
2398 * registers that have the same object instance in them, not just the "this" register.
2399 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002400 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002401 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002402 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002403 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2404 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002405 }
2406 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002407 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002408 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002409 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002411 just_set_result = true;
2412 break;
2413 }
2414 case Instruction::INVOKE_STATIC:
2415 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002416 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002417 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002418 METHOD_STATIC,
2419 is_range,
2420 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002421 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002422 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002424 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2425 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002426 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002427 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002428 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002429 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002430 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002431 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002433 } else {
2434 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2435 }
jeffhaobdb76512011-09-07 11:43:16 -07002436 just_set_result = true;
2437 }
2438 break;
jeffhaobdb76512011-09-07 11:43:16 -07002439 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002441 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002442 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002443 METHOD_INTERFACE,
2444 is_range,
2445 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002446 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002447 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002448 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2449 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2450 << PrettyMethod(abs_method) << "'";
2451 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002452 }
Ian Rogers0d604842012-04-16 14:50:24 -07002453 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002454 /* Get the type of the "this" arg, which should either be a sub-interface of called
2455 * interface or Object (see comments in RegType::JoinClass).
2456 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002457 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002458 if (this_type.IsZero()) {
2459 /* null pointer always passes (and always fails at runtime) */
2460 } else {
2461 if (this_type.IsUninitializedTypes()) {
2462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2463 << this_type;
2464 break;
2465 }
2466 // In the past we have tried to assert that "called_interface" is assignable
2467 // from "this_type.GetClass()", however, as we do an imprecise Join
2468 // (RegType::JoinClass) we don't have full information on what interfaces are
2469 // implemented by "this_type". For example, two classes may implement the same
2470 // interfaces and have a common parent that doesn't implement the interface. The
2471 // join will set "this_type" to the parent class and a test that this implements
2472 // the interface will incorrectly fail.
2473 }
2474 /*
2475 * We don't have an object instance, so we can't find the concrete method. However, all of
2476 * the type information is in the abstract method, so we're good.
2477 */
2478 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002479 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002480 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002481 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2482 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2483 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2484 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002485 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002486 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002487 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002488 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002489 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002490 } else {
2491 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2492 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002493 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 }
jeffhaobdb76512011-09-07 11:43:16 -07002496 case Instruction::NEG_INT:
2497 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002498 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::NEG_LONG:
2501 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002502 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002503 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002504 break;
2505 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002506 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
2508 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002509 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002510 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002513 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002514 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002517 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002520 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002521 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002524 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002525 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002526 break;
2527 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002528 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002529 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
2531 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002532 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002533 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002534 break;
2535 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002536 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002540 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002543 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002544 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002545 break;
2546 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002547 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002548 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002549 break;
2550 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002552 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002556 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002560 break;
2561 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002562 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567
2568 case Instruction::ADD_INT:
2569 case Instruction::SUB_INT:
2570 case Instruction::MUL_INT:
2571 case Instruction::REM_INT:
2572 case Instruction::DIV_INT:
2573 case Instruction::SHL_INT:
2574 case Instruction::SHR_INT:
2575 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002577 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::AND_INT:
2580 case Instruction::OR_INT:
2581 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002582 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002583 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::ADD_LONG:
2586 case Instruction::SUB_LONG:
2587 case Instruction::MUL_LONG:
2588 case Instruction::DIV_LONG:
2589 case Instruction::REM_LONG:
2590 case Instruction::AND_LONG:
2591 case Instruction::OR_LONG:
2592 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002594 reg_types_.LongLo(), reg_types_.LongHi(),
2595 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::SHL_LONG:
2598 case Instruction::SHR_LONG:
2599 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002601 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002602 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
2604 case Instruction::ADD_FLOAT:
2605 case Instruction::SUB_FLOAT:
2606 case Instruction::MUL_FLOAT:
2607 case Instruction::DIV_FLOAT:
2608 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2610 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::ADD_DOUBLE:
2613 case Instruction::SUB_DOUBLE:
2614 case Instruction::MUL_DOUBLE:
2615 case Instruction::DIV_DOUBLE:
2616 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002617 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002618 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2619 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::ADD_INT_2ADDR:
2622 case Instruction::SUB_INT_2ADDR:
2623 case Instruction::MUL_INT_2ADDR:
2624 case Instruction::REM_INT_2ADDR:
2625 case Instruction::SHL_INT_2ADDR:
2626 case Instruction::SHR_INT_2ADDR:
2627 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002628 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2629 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::AND_INT_2ADDR:
2632 case Instruction::OR_INT_2ADDR:
2633 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002634 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2635 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::DIV_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::ADD_LONG_2ADDR:
2642 case Instruction::SUB_LONG_2ADDR:
2643 case Instruction::MUL_LONG_2ADDR:
2644 case Instruction::DIV_LONG_2ADDR:
2645 case Instruction::REM_LONG_2ADDR:
2646 case Instruction::AND_LONG_2ADDR:
2647 case Instruction::OR_LONG_2ADDR:
2648 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002649 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002650 reg_types_.LongLo(), reg_types_.LongHi(),
2651 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::SHL_LONG_2ADDR:
2654 case Instruction::SHR_LONG_2ADDR:
2655 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002656 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002657 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002658 break;
2659 case Instruction::ADD_FLOAT_2ADDR:
2660 case Instruction::SUB_FLOAT_2ADDR:
2661 case Instruction::MUL_FLOAT_2ADDR:
2662 case Instruction::DIV_FLOAT_2ADDR:
2663 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002664 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2665 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002666 break;
2667 case Instruction::ADD_DOUBLE_2ADDR:
2668 case Instruction::SUB_DOUBLE_2ADDR:
2669 case Instruction::MUL_DOUBLE_2ADDR:
2670 case Instruction::DIV_DOUBLE_2ADDR:
2671 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002672 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002673 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2674 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002677 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002678 case Instruction::MUL_INT_LIT16:
2679 case Instruction::DIV_INT_LIT16:
2680 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002681 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2682 true);
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::AND_INT_LIT16:
2685 case Instruction::OR_INT_LIT16:
2686 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2688 true);
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::ADD_INT_LIT8:
2691 case Instruction::RSUB_INT_LIT8:
2692 case Instruction::MUL_INT_LIT8:
2693 case Instruction::DIV_INT_LIT8:
2694 case Instruction::REM_INT_LIT8:
2695 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002696 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002697 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002698 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2699 false);
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::AND_INT_LIT8:
2702 case Instruction::OR_INT_LIT8:
2703 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2705 false);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002708 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002709 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002710 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2712 }
2713 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002714 // Note: the following instructions encode offsets derived from class linking.
2715 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2716 // meaning if the class linking and resolution were successful.
2717 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002718 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002719 break;
2720 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002721 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002722 break;
2723 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002724 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002725 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002726 case Instruction::IGET_BOOLEAN_QUICK:
2727 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2728 break;
2729 case Instruction::IGET_BYTE_QUICK:
2730 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2731 break;
2732 case Instruction::IGET_CHAR_QUICK:
2733 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2734 break;
2735 case Instruction::IGET_SHORT_QUICK:
2736 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2737 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002738 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002739 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002740 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002741 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002742 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002743 break;
2744 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002745 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002746 break;
2747 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002748 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002749 break;
2750 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002751 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002752 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002753 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002754 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002755 break;
2756 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002757 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002758 break;
2759 case Instruction::INVOKE_VIRTUAL_QUICK:
2760 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2761 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002762 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002763 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002764 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002765 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002766 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002767 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002768 } else {
2769 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2770 }
2771 just_set_result = true;
2772 }
2773 break;
2774 }
2775
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002777 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2778 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002779 case Instruction::UNUSED_79:
2780 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783
2784 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002785 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002786 * complain if an instruction is missing (which is desirable).
2787 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002788 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002789
Stephen Kyle7e541c92014-12-17 17:10:02 +00002790 /*
2791 * If we are in a constructor, and we had an UninitializedThis type
2792 * in a register somewhere, we need to make sure it wasn't overwritten.
2793 */
2794 if (track_uninitialized_this) {
2795 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2796 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2797 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2798 was_invoke_direct)) {
2799 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2800 << "Constructor failed to initialize this object";
2801 }
2802 }
2803
Ian Rogersad0b3a32012-04-16 14:50:24 -07002804 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002805 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002806 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002807 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002808 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002809 /* immediate failure, reject class */
2810 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2811 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002812 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002813 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002814 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002815 }
jeffhaobdb76512011-09-07 11:43:16 -07002816 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 * If we didn't just set the result register, clear it out. This ensures that you can only use
2818 * "move-result" immediately after the result is set. (We could check this statically, but it's
2819 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002820 */
2821 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002822 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002823 }
2824
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002825
jeffhaobdb76512011-09-07 11:43:16 -07002826
2827 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002828 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002829 *
2830 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002831 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002832 * somebody could get a reference field, check it for zero, and if the
2833 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002834 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002835 * that, and will reject the code.
2836 *
2837 * TODO: avoid re-fetching the branch target
2838 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002839 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002840 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002842 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002844 return false;
2845 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002846 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002847 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002848 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 }
jeffhaobdb76512011-09-07 11:43:16 -07002850 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002851 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002852 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002853 return false;
2854 }
2855 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002856 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002857 return false;
2858 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 }
jeffhaobdb76512011-09-07 11:43:16 -07002860 }
2861
2862 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002863 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002864 *
2865 * We've already verified that the table is structurally sound, so we
2866 * just need to walk through and tag the targets.
2867 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002868 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002869 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2870 const uint16_t* switch_insns = insns + offset_to_switch;
2871 int switch_count = switch_insns[1];
2872 int offset_to_targets, targ;
2873
2874 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2875 /* 0 = sig, 1 = count, 2/3 = first key */
2876 offset_to_targets = 4;
2877 } else {
2878 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002879 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002880 offset_to_targets = 2 + 2 * switch_count;
2881 }
2882
2883 /* verify each switch target */
2884 for (targ = 0; targ < switch_count; targ++) {
2885 int offset;
2886 uint32_t abs_offset;
2887
2888 /* offsets are 32-bit, and only partly endian-swapped */
2889 offset = switch_insns[offset_to_targets + targ * 2] |
2890 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 abs_offset = work_insn_idx_ + offset;
2892 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002893 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002894 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002896 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002897 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002898 }
jeffhaobdb76512011-09-07 11:43:16 -07002899 }
2900 }
2901
2902 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2904 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002905 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002906 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002907 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002908 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002909
Andreas Gampef91baf12014-07-18 15:41:00 -07002910 // Need the linker to try and resolve the handled class to check if it's Throwable.
2911 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2912
Ian Rogers0571d352011-11-03 19:51:38 -07002913 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002914 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2915 if (handler_type_idx == DexFile::kDexNoIndex16) {
2916 has_catch_all_handler = true;
2917 } else {
2918 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002919 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2920 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002921 if (klass != nullptr) {
2922 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2923 has_catch_all_handler = true;
2924 }
2925 } else {
2926 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002927 DCHECK(self_->IsExceptionPending());
2928 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002929 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 }
jeffhaobdb76512011-09-07 11:43:16 -07002931 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2933 * "work_regs", because at runtime the exception will be thrown before the instruction
2934 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002935 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002936 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002937 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002938 }
jeffhaobdb76512011-09-07 11:43:16 -07002939 }
2940
2941 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2943 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002944 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002945 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002946 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 * The state in work_line reflects the post-execution state. If the current instruction is a
2948 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002949 * it will do so before grabbing the lock).
2950 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002951 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002952 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002953 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002954 return false;
2955 }
2956 }
2957 }
2958
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002959 /* Handle "continue". Tag the next consecutive instruction.
2960 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2961 * because it changes work_line_ when performing peephole optimization
2962 * and this change should not be used in those cases.
2963 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002964 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002965 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2966 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002967 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2969 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002970 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002971 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2972 // next instruction isn't one.
2973 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2974 return false;
2975 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002976 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002977 // Make workline consistent with fallthrough computed from peephole optimization.
2978 work_line_->CopyFromLine(fallthrough_line.get());
2979 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002980 if (insn_flags_[next_insn_idx].IsReturn()) {
2981 // For returns we only care about the operand to the return, all other registers are dead.
2982 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2983 Instruction::Code opcode = ret_inst->Opcode();
2984 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00002985 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00002986 } else {
2987 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002988 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002989 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002990 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002991 }
2992 }
2993 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002994 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002996 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2997 // needed. If the merge changes the state of the registers then the work line will be
2998 // updated.
2999 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003000 return false;
3001 }
3002 } else {
3003 /*
3004 * We're not recording register data for the next instruction, so we don't know what the
3005 * prior state was. We have to assume that something has changed and re-evaluate it.
3006 */
3007 insn_flags_[next_insn_idx].SetChanged();
3008 }
3009 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003010
jeffhaod1f0fde2011-09-08 17:25:33 -07003011 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003012 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 return false;
3015 }
jeffhaobdb76512011-09-07 11:43:16 -07003016 }
3017
3018 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003019 * Update start_guess. Advance to the next instruction of that's
3020 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003021 * neither of those exists we're in a return or throw; leave start_guess
3022 * alone and let the caller sort it out.
3023 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003024 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003025 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3026 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003027 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003028 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003029 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003030 }
3031
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3033 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003034
3035 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003036} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003037
Ian Rogersd8f69b02014-09-10 21:43:52 +00003038const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003039 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003040 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003041 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003042 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003043 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3044 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003045 if (result.IsConflict()) {
3046 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3047 << "' in " << referrer;
3048 return result;
3049 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003050 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003051 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003052 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003053 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003054 // check at runtime if access is allowed and so pass here. If result is
3055 // primitive, skip the access check.
3056 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3057 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003058 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003059 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003060 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003061 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003062}
3063
Ian Rogersd8f69b02014-09-10 21:43:52 +00003064const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003065 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003067 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3069 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003070 CatchHandlerIterator iterator(handlers_ptr);
3071 for (; iterator.HasNext(); iterator.Next()) {
3072 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3073 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003074 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003076 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003077 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003078 if (exception.IsUnresolvedTypes()) {
3079 // We don't know enough about the type. Fail here and let runtime handle it.
3080 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3081 return exception;
3082 } else {
3083 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3084 return reg_types_.Conflict();
3085 }
Jeff Haob878f212014-04-24 16:25:36 -07003086 } else if (common_super == nullptr) {
3087 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003088 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003089 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003090 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003091 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003092 if (FailOrAbort(this,
3093 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3094 "java.lang.Throwable is not assignable-from common_super at ",
3095 work_insn_idx_)) {
3096 break;
3097 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003098 }
3099 }
3100 }
3101 }
Ian Rogers0571d352011-11-03 19:51:38 -07003102 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 }
3104 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003105 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003107 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003108 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003109 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003110 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003111}
3112
Brian Carlstromea46f952013-07-30 01:26:50 -07003113mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003114 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003115 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003116 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 if (klass_type.IsConflict()) {
3118 std::string append(" in attempt to access method ");
3119 append += dex_file_->GetMethodName(method_id);
3120 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003121 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003122 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003123 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003125 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003126 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003127 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003128 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003129 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003130 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003131 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003132
3133 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003135 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003136 res_method = klass->FindInterfaceMethod(name, signature);
3137 } else {
3138 res_method = klass->FindVirtualMethod(name, signature);
3139 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003140 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003141 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003143 // If a virtual or interface method wasn't found with the expected type, look in
3144 // the direct methods. This can happen when the wrong invoke type is used or when
3145 // a class has changed, and will be flagged as an error in later checks.
3146 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3147 res_method = klass->FindDirectMethod(name, signature);
3148 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003150 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3151 << PrettyDescriptor(klass) << "." << name
3152 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003153 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003154 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 }
3156 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3158 // enforce them here.
3159 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3161 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003164 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003165 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3167 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003168 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003169 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003170 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003171 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003172 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003173 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003174 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003175 }
jeffhaode0d9c92012-02-27 13:58:13 -08003176 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3177 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3179 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003180 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003181 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003182 // Check that interface methods match interface classes.
3183 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3184 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3185 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003187 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3188 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3189 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003190 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003191 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 // See if the method type implied by the invoke instruction matches the access flags for the
3193 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003194 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3196 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3197 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003198 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3199 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003200 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003201 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003202 return res_method;
3203}
3204
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003205template <class T>
3206mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3207 MethodType method_type,
3208 bool is_range,
3209 mirror::ArtMethod* res_method) {
3210 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3211 // match the call to the signature. Also, we might be calling through an abstract method
3212 // definition (which doesn't have register count values).
3213 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3214 /* caught by static verifier */
3215 DCHECK(is_range || expected_args <= 5);
3216 if (expected_args > code_item_->outs_size_) {
3217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3218 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3219 return nullptr;
3220 }
3221
3222 uint32_t arg[5];
3223 if (!is_range) {
3224 inst->GetVarArgs(arg);
3225 }
3226 uint32_t sig_registers = 0;
3227
3228 /*
3229 * Check the "this" argument, which must be an instance of the class that declared the method.
3230 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3231 * rigorous check here (which is okay since we have to do it at runtime).
3232 */
3233 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003234 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003235 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3236 CHECK(have_pending_hard_failure_);
3237 return nullptr;
3238 }
3239 if (actual_arg_type.IsUninitializedReference()) {
3240 if (res_method) {
3241 if (!res_method->IsConstructor()) {
3242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3243 return nullptr;
3244 }
3245 } else {
3246 // Check whether the name of the called method is "<init>"
3247 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003248 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3250 return nullptr;
3251 }
3252 }
3253 }
3254 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003255 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003256 if (res_method != nullptr) {
3257 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003258 std::string temp;
3259 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003260 klass->CannotBeAssignedFromOtherTypes());
3261 } else {
3262 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3263 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003264 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003265 dex_file_->StringByTypeIdx(class_idx),
3266 false);
3267 }
3268 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3269 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3270 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3271 << "' not instance of '" << *res_method_class << "'";
3272 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3273 // the compiler.
3274 if (have_pending_hard_failure_) {
3275 return nullptr;
3276 }
3277 }
3278 }
3279 sig_registers = 1;
3280 }
3281
3282 for ( ; it->HasNext(); it->Next()) {
3283 if (sig_registers >= expected_args) {
3284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3285 " arguments, found " << sig_registers << " or more.";
3286 return nullptr;
3287 }
3288
3289 const char* param_descriptor = it->GetDescriptor();
3290
3291 if (param_descriptor == nullptr) {
3292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3293 "component";
3294 return nullptr;
3295 }
3296
Ian Rogersd8f69b02014-09-10 21:43:52 +00003297 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003298 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3299 arg[sig_registers];
3300 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003301 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003302 if (!src_type.IsIntegralTypes()) {
3303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3304 << " but expected " << reg_type;
3305 return res_method;
3306 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003307 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003308 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3309 // compiler.
3310 if (have_pending_hard_failure_) {
3311 return res_method;
3312 }
3313 }
3314 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3315 }
3316 if (expected_args != sig_registers) {
3317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3318 " arguments, found " << sig_registers;
3319 return nullptr;
3320 }
3321 return res_method;
3322}
3323
3324void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3325 MethodType method_type,
3326 bool is_range) {
3327 // As the method may not have been resolved, make this static check against what we expect.
3328 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3329 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3330 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3331 DexFileParameterIterator it(*dex_file_,
3332 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3333 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3334 nullptr);
3335}
3336
3337class MethodParamListDescriptorIterator {
3338 public:
3339 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3340 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3341 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3342 }
3343
3344 bool HasNext() {
3345 return pos_ < params_size_;
3346 }
3347
3348 void Next() {
3349 ++pos_;
3350 }
3351
3352 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3353 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3354 }
3355
3356 private:
3357 mirror::ArtMethod* res_method_;
3358 size_t pos_;
3359 const DexFile::TypeList* params_;
3360 const size_t params_size_;
3361};
3362
Brian Carlstromea46f952013-07-30 01:26:50 -07003363mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003364 MethodType method_type,
3365 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003366 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003367 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3368 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003369 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003370
Brian Carlstromea46f952013-07-30 01:26:50 -07003371 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003372 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003373 // Check what we can statically.
3374 if (!have_pending_hard_failure_) {
3375 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3376 }
3377 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003378 }
3379
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3381 // has a vtable entry for the target method.
3382 if (is_super) {
3383 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003384 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003385 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003386 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003387 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003388 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003389 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003390 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003391 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003392 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003393 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003394 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003395 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003396 << "." << res_method->GetName()
3397 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003398 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 }
3400 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003401
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003402 // Process the target method's signature. This signature may or may not
3403 MethodParamListDescriptorIterator it(res_method);
3404 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3405 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003406}
3407
Brian Carlstromea46f952013-07-30 01:26:50 -07003408mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003409 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003410 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3411 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003412 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003413 if (!actual_arg_type.HasClass()) {
3414 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003415 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003416 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003417 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003418 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003419 if (klass->IsInterface()) {
3420 // Derive Object.class from Class.class.getSuperclass().
3421 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003422 if (FailOrAbort(this, object_klass->IsObjectClass(),
3423 "Failed to find Object class in quickened invoke receiver",
3424 work_insn_idx_)) {
3425 return nullptr;
3426 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003427 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003428 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003429 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003430 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003431 if (FailOrAbort(this, dispatch_class->HasVTable(),
3432 "Receiver class has no vtable for quickened invoke at ",
3433 work_insn_idx_)) {
3434 return nullptr;
3435 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003436 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe7c038102014-10-27 20:08:46 -07003437 if (FailOrAbort(this, static_cast<int32_t>(vtable_index) < dispatch_class->GetVTableLength(),
3438 "Receiver class has not enough vtable slots for quickened invoke at ",
3439 work_insn_idx_)) {
3440 return nullptr;
3441 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003442 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier36b58f52014-12-10 12:06:45 -08003443 if (FailOrAbort(this, !self_->IsExceptionPending(),
Andreas Gampe7c038102014-10-27 20:08:46 -07003444 "Unexpected exception pending for quickened invoke at ",
3445 work_insn_idx_)) {
3446 return nullptr;
3447 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003448 return res_method;
3449}
3450
Brian Carlstromea46f952013-07-30 01:26:50 -07003451mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003452 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003453 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3454 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3455
Brian Carlstromea46f952013-07-30 01:26:50 -07003456 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003457 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003458 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003460 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003461 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003462 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3463 work_insn_idx_)) {
3464 return nullptr;
3465 }
3466 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3467 work_insn_idx_)) {
3468 return nullptr;
3469 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003470
3471 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3472 // match the call to the signature. Also, we might be calling through an abstract method
3473 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003474 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003475 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003476 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003477 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003478 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3479 /* caught by static verifier */
3480 DCHECK(is_range || expected_args <= 5);
3481 if (expected_args > code_item_->outs_size_) {
3482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3483 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003484 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003485 }
3486
3487 /*
3488 * Check the "this" argument, which must be an instance of the class that declared the method.
3489 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3490 * rigorous check here (which is okay since we have to do it at runtime).
3491 */
3492 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3493 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003494 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003495 }
3496 if (!actual_arg_type.IsZero()) {
3497 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003498 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003499 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003500 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003501 klass->CannotBeAssignedFromOtherTypes());
3502 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003503 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3504 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003505 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003506 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003507 }
3508 }
3509 /*
3510 * Process the target method's signature. This signature may or may not
3511 * have been verified, so we can't assume it's properly formed.
3512 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003513 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003514 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003515 uint32_t arg[5];
3516 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003517 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003518 }
3519 size_t actual_args = 1;
3520 for (size_t param_index = 0; param_index < params_size; param_index++) {
3521 if (actual_args >= expected_args) {
3522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003523 << "'. Expected " << expected_args
3524 << " arguments, processing argument " << actual_args
3525 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003526 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003527 }
3528 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003529 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003530 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003532 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003533 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003534 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003535 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003536 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003537 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003538 return res_method;
3539 }
3540 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3541 }
3542 if (actual_args != expected_args) {
3543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3544 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003545 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003546 } else {
3547 return res_method;
3548 }
3549}
3550
Ian Rogers62342ec2013-06-11 10:26:37 -07003551void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003552 uint32_t type_idx;
3553 if (!is_filled) {
3554 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3555 type_idx = inst->VRegC_22c();
3556 } else if (!is_range) {
3557 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3558 type_idx = inst->VRegB_35c();
3559 } else {
3560 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3561 type_idx = inst->VRegB_3rc();
3562 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003563 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003564 if (res_type.IsConflict()) { // bad class
3565 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003566 } else {
3567 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3568 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003570 } else if (!is_filled) {
3571 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003572 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003573 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003574 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003575 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003576 } else {
3577 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3578 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003579 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003580 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3581 uint32_t arg[5];
3582 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003583 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003584 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003585 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003586 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003587 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3588 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003589 return;
3590 }
3591 }
3592 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003593 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003594 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003595 }
3596 }
3597}
3598
Sebastien Hertz5243e912013-05-21 10:55:07 +02003599void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003600 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003601 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003602 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003604 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003605 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003606 if (array_type.IsZero()) {
3607 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3608 // instruction type. TODO: have a proper notion of bottom here.
3609 if (!is_primitive || insn_type.IsCategory1Types()) {
3610 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003613 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003614 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3615 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003616 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003617 }
jeffhaofc3144e2012-02-01 17:21:15 -08003618 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003619 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003620 } else {
3621 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003622 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003623 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003625 << " source for aget-object";
3626 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003628 << " source for category 1 aget";
3629 } else if (is_primitive && !insn_type.Equals(component_type) &&
3630 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003631 (insn_type.IsLong() && component_type.IsDouble()))) {
3632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3633 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003634 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003635 // Use knowledge of the field type which is stronger than the type inferred from the
3636 // instruction, which can't differentiate object types and ints from floats, longs from
3637 // doubles.
3638 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003639 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003640 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003641 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003642 component_type.HighHalf(&reg_types_));
3643 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 }
3645 }
3646 }
3647}
3648
Ian Rogersd8f69b02014-09-10 21:43:52 +00003649void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003650 const uint32_t vregA) {
3651 // Primitive assignability rules are weaker than regular assignability rules.
3652 bool instruction_compatible;
3653 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003654 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003655 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003656 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003657 value_compatible = value_type.IsIntegralTypes();
3658 } else if (target_type.IsFloat()) {
3659 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3660 value_compatible = value_type.IsFloatTypes();
3661 } else if (target_type.IsLong()) {
3662 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003663 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3664 // as target_type depends on the resolved type of the field.
3665 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003666 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003667 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3668 } else {
3669 value_compatible = false;
3670 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003671 } else if (target_type.IsDouble()) {
3672 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
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.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3678 } else {
3679 value_compatible = false;
3680 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003681 } else {
3682 instruction_compatible = false; // reference with primitive store
3683 value_compatible = false; // unused
3684 }
3685 if (!instruction_compatible) {
3686 // This is a global failure rather than a class change failure as the instructions and
3687 // the descriptors for the type should have been consistent within the same file at
3688 // compile time.
3689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3690 << "' but expected type '" << target_type << "'";
3691 return;
3692 }
3693 if (!value_compatible) {
3694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3695 << " of type " << value_type << " but expected " << target_type << " for put";
3696 return;
3697 }
3698}
3699
Sebastien Hertz5243e912013-05-21 10:55:07 +02003700void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003701 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003702 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003705 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003706 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003707 if (array_type.IsZero()) {
3708 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3709 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003710 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003712 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003713 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003714 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003715 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003716 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003717 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003718 if (!component_type.IsReferenceTypes()) {
3719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3720 << " source for aput-object";
3721 } else {
3722 // The instruction agrees with the type of array, confirm the value to be stored does too
3723 // Note: we use the instruction type (rather than the component type) for aput-object as
3724 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003726 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 }
3728 }
3729 }
3730}
3731
Brian Carlstromea46f952013-07-30 01:26:50 -07003732mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003733 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3734 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003735 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003736 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003737 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3738 field_idx, dex_file_->GetFieldName(field_id),
3739 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003740 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003741 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003742 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003743 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003744 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003745 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003746 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3747 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003748 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003749 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003750 << dex_file_->GetFieldName(field_id) << ") in "
3751 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003752 DCHECK(self_->IsExceptionPending());
3753 self_->ClearException();
3754 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003755 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3756 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003757 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003758 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003759 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003760 } else if (!field->IsStatic()) {
3761 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003762 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003763 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003764 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003765}
3766
Ian Rogersd8f69b02014-09-10 21:43:52 +00003767mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003768 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3769 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003770 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003771 if (klass_type.IsConflict()) {
3772 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3773 field_idx, dex_file_->GetFieldName(field_id),
3774 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003776 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003777 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003778 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003779 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003780 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003781 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3782 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003784 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003785 << dex_file_->GetFieldName(field_id) << ") in "
3786 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003787 DCHECK(self_->IsExceptionPending());
3788 self_->ClearException();
3789 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003790 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3791 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003792 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003793 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003794 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003795 } else if (field->IsStatic()) {
3796 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3797 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003798 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 } else if (obj_type.IsZero()) {
3800 // Cannot infer and check type, however, access will cause null pointer exception
3801 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003802 } else if (!obj_type.IsReferenceTypes()) {
3803 // Trying to read a field from something that isn't a reference
3804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3805 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003806 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003807 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003808 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003809 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003810 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003811 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003812 if (obj_type.IsUninitializedTypes() &&
3813 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3814 !field_klass.Equals(GetDeclaringClass()))) {
3815 // Field accesses through uninitialized references are only allowable for constructors where
3816 // the field is declared in this class
3817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003818 << " of a not fully initialized object within the context"
3819 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003820 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003821 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3822 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3823 // of C1. For resolution to occur the declared class of the field must be compatible with
3824 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3825 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3826 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003827 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003828 } else {
3829 return field;
3830 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003831 }
3832}
3833
Andreas Gampe896df402014-10-20 22:25:29 -07003834template <MethodVerifier::FieldAccessType kAccType>
3835void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3836 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003837 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003838 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003839 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003840 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003841 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003842 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003843 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003844 if (UNLIKELY(have_pending_hard_failure_)) {
3845 return;
3846 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003847 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003848 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003849 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003850 if (kAccType == FieldAccessType::kAccPut) {
3851 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3852 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3853 << " from other class " << GetDeclaringClass();
3854 return;
3855 }
3856 }
3857
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003858 mirror::Class* field_type_class;
3859 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003860 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003861 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08003862 field_type_class = h_field->GetType(can_load_classes_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003863 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003864 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003865 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003866 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003867 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003868 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3869 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003870 }
Ian Rogers0d604842012-04-16 14:50:24 -07003871 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003872 if (field_type == nullptr) {
3873 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3874 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003875 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003876 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003877 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003878 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003879 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3880 "Unexpected third access type");
3881 if (kAccType == FieldAccessType::kAccPut) {
3882 // sput or iput.
3883 if (is_primitive) {
3884 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003885 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003886 if (!insn_type.IsAssignableFrom(*field_type)) {
3887 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3888 << " to be compatible with type '" << insn_type
3889 << "' but found type '" << *field_type
3890 << "' in put-object";
3891 return;
3892 }
3893 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003894 }
Andreas Gampe896df402014-10-20 22:25:29 -07003895 } else if (kAccType == FieldAccessType::kAccGet) {
3896 // sget or iget.
3897 if (is_primitive) {
3898 if (field_type->Equals(insn_type) ||
3899 (field_type->IsFloat() && insn_type.IsInteger()) ||
3900 (field_type->IsDouble() && insn_type.IsLong())) {
3901 // expected that read is of the correct primitive type or that int reads are reading
3902 // floats or long reads are reading doubles
3903 } else {
3904 // This is a global failure rather than a class change failure as the instructions and
3905 // the descriptors for the type should have been consistent within the same file at
3906 // compile time
3907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3908 << " to be of type '" << insn_type
3909 << "' but found type '" << *field_type << "' in get";
3910 return;
3911 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003912 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003913 if (!insn_type.IsAssignableFrom(*field_type)) {
3914 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3915 << " to be compatible with type '" << insn_type
3916 << "' but found type '" << *field_type
3917 << "' in get-object";
3918 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3919 return;
3920 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003921 }
Andreas Gampe896df402014-10-20 22:25:29 -07003922 if (!field_type->IsLowHalf()) {
3923 work_line_->SetRegisterType(this, vregA, *field_type);
3924 } else {
3925 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3926 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003927 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003928 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003929 }
3930}
3931
Brian Carlstromea46f952013-07-30 01:26:50 -07003932mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003933 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003934 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3935 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3936 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003937 inst->Opcode() == Instruction::IGET_BOOLEAN_QUICK ||
3938 inst->Opcode() == Instruction::IGET_BYTE_QUICK ||
3939 inst->Opcode() == Instruction::IGET_CHAR_QUICK ||
3940 inst->Opcode() == Instruction::IGET_SHORT_QUICK ||
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003941 inst->Opcode() == Instruction::IPUT_QUICK ||
3942 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
Hiroshi Yamauchi5f09be92014-09-26 10:43:59 -07003943 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3944 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3945 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3946 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3947 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003948 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003949 if (!object_type.HasClass()) {
3950 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3951 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003952 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003953 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003954 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3955 field_offset);
3956 if (f == nullptr) {
3957 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3958 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3959 }
3960 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003961}
3962
Andreas Gampe896df402014-10-20 22:25:29 -07003963template <MethodVerifier::FieldAccessType kAccType>
3964void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
3965 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003966 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003967
Brian Carlstromea46f952013-07-30 01:26:50 -07003968 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003969 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3971 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003972 }
Andreas Gampe896df402014-10-20 22:25:29 -07003973
3974 // For an IPUT_QUICK, we now test for final flag of the field.
3975 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003976 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3977 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003978 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003979 return;
3980 }
3981 }
Andreas Gampe896df402014-10-20 22:25:29 -07003982
3983 // Get the field type.
3984 const RegType* field_type;
3985 {
3986 mirror::Class* field_type_class;
3987 {
3988 StackHandleScope<1> hs(Thread::Current());
3989 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08003990 field_type_class = h_field->GetType(can_load_classes_);
Andreas Gampe896df402014-10-20 22:25:29 -07003991 }
3992
3993 if (field_type_class != nullptr) {
3994 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
3995 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003996 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003997 Thread* self = Thread::Current();
3998 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3999 self->ClearException();
4000 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4001 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004002 }
Andreas Gampe896df402014-10-20 22:25:29 -07004003 if (field_type == nullptr) {
4004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004005 return;
4006 }
Andreas Gampe896df402014-10-20 22:25:29 -07004007 }
4008
4009 const uint32_t vregA = inst->VRegA_22c();
4010 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4011 "Unexpected third access type");
4012 if (kAccType == FieldAccessType::kAccPut) {
4013 if (is_primitive) {
4014 // Primitive field assignability rules are weaker than regular assignability rules
4015 bool instruction_compatible;
4016 bool value_compatible;
4017 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4018 if (field_type->IsIntegralTypes()) {
4019 instruction_compatible = insn_type.IsIntegralTypes();
4020 value_compatible = value_type.IsIntegralTypes();
4021 } else if (field_type->IsFloat()) {
4022 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4023 value_compatible = value_type.IsFloatTypes();
4024 } else if (field_type->IsLong()) {
4025 instruction_compatible = insn_type.IsLong();
4026 value_compatible = value_type.IsLongTypes();
4027 } else if (field_type->IsDouble()) {
4028 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4029 value_compatible = value_type.IsDoubleTypes();
4030 } else {
4031 instruction_compatible = false; // reference field with primitive store
4032 value_compatible = false; // unused
4033 }
4034 if (!instruction_compatible) {
4035 // This is a global failure rather than a class change failure as the instructions and
4036 // the descriptors for the type should have been consistent within the same file at
4037 // compile time
4038 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4039 << " to be of type '" << insn_type
4040 << "' but found type '" << *field_type
4041 << "' in put";
4042 return;
4043 }
4044 if (!value_compatible) {
4045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4046 << " of type " << value_type
4047 << " but expected " << *field_type
4048 << " for store to " << PrettyField(field) << " in put";
4049 return;
4050 }
4051 } else {
4052 if (!insn_type.IsAssignableFrom(*field_type)) {
4053 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4054 << " to be compatible with type '" << insn_type
4055 << "' but found type '" << *field_type
4056 << "' in put-object";
4057 return;
4058 }
4059 work_line_->VerifyRegisterType(this, vregA, *field_type);
4060 }
4061 } else if (kAccType == FieldAccessType::kAccGet) {
4062 if (is_primitive) {
4063 if (field_type->Equals(insn_type) ||
4064 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4065 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4066 // expected that read is of the correct primitive type or that int reads are reading
4067 // floats or long reads are reading doubles
4068 } else {
4069 // This is a global failure rather than a class change failure as the instructions and
4070 // the descriptors for the type should have been consistent within the same file at
4071 // compile time
4072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4073 << " to be of type '" << insn_type
4074 << "' but found type '" << *field_type << "' in Get";
4075 return;
4076 }
4077 } else {
4078 if (!insn_type.IsAssignableFrom(*field_type)) {
4079 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4080 << " to be compatible with type '" << insn_type
4081 << "' but found type '" << *field_type
4082 << "' in get-object";
4083 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4084 return;
4085 }
4086 }
4087 if (!field_type->IsLowHalf()) {
4088 work_line_->SetRegisterType(this, vregA, *field_type);
4089 } else {
4090 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004091 }
4092 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004093 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004094 }
4095}
4096
Ian Rogers776ac1f2012-04-13 23:36:36 -07004097bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004098 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004100 return false;
4101 }
4102 return true;
4103}
4104
Stephen Kyle9bc61992014-09-22 13:53:15 +01004105bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4106 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4107 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4109 return false;
4110 }
4111 return true;
4112}
4113
4114bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4115 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4116}
4117
Ian Rogersebbdd872014-07-07 23:53:08 -07004118bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4119 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004120 bool changed = true;
4121 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4122 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004123 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004124 * We haven't processed this instruction before, and we haven't touched the registers here, so
4125 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4126 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004127 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004128 if (!insn_flags_[next_insn].IsReturn()) {
4129 target_line->CopyFromLine(merge_line);
4130 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004131 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004132 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004133 return false;
4134 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004135 // For returns we only care about the operand to the return, all other registers are dead.
4136 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4137 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4138 Instruction::Code opcode = ret_inst->Opcode();
4139 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004140 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004141 } else {
4142 target_line->CopyFromLine(merge_line);
4143 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004144 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004145 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004146 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004147 }
4148 }
4149 }
jeffhaobdb76512011-09-07 11:43:16 -07004150 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004151 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004152 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004154 if (gDebugVerify) {
4155 copy->CopyFromLine(target_line);
4156 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004157 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004158 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004159 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004160 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004161 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004162 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004163 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004164 << copy->Dump(this) << " MERGE\n"
4165 << merge_line->Dump(this) << " ==\n"
4166 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004167 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004168 if (update_merge_line && changed) {
4169 merge_line->CopyFromLine(target_line);
4170 }
jeffhaobdb76512011-09-07 11:43:16 -07004171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004172 if (changed) {
4173 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004174 }
4175 return true;
4176}
4177
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004178InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004179 return &insn_flags_[work_insn_idx_];
4180}
4181
Ian Rogersd8f69b02014-09-10 21:43:52 +00004182const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004183 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004184 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004185 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004186 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004187 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4188 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004189 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004190 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004191 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4192 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004193 }
4194 }
4195 if (return_type_ == nullptr) {
4196 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4197 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4198 uint16_t return_type_idx = proto_id.return_type_idx_;
4199 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004200 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004201 }
4202 }
4203 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004204}
4205
Ian Rogersd8f69b02014-09-10 21:43:52 +00004206const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004207 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004208 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004209 const char* descriptor
4210 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004211 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004212 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004213 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4214 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004215 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004216 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004217 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004218 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004219 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004220}
4221
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004222std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4223 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004224 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004225 std::vector<int32_t> result;
4226 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004227 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004228 if (type.IsConstant()) {
4229 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004230 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4231 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004232 } else if (type.IsConstantLo()) {
4233 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4235 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004236 } else if (type.IsConstantHi()) {
4237 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004238 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4239 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004240 } else if (type.IsIntegralTypes()) {
4241 result.push_back(kIntVReg);
4242 result.push_back(0);
4243 } else if (type.IsFloat()) {
4244 result.push_back(kFloatVReg);
4245 result.push_back(0);
4246 } else if (type.IsLong()) {
4247 result.push_back(kLongLoVReg);
4248 result.push_back(0);
4249 result.push_back(kLongHiVReg);
4250 result.push_back(0);
4251 ++i;
4252 } else if (type.IsDouble()) {
4253 result.push_back(kDoubleLoVReg);
4254 result.push_back(0);
4255 result.push_back(kDoubleHiVReg);
4256 result.push_back(0);
4257 ++i;
4258 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4259 result.push_back(kUndefined);
4260 result.push_back(0);
4261 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004262 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004263 result.push_back(kReferenceVReg);
4264 result.push_back(0);
4265 }
4266 }
4267 return result;
4268}
4269
Ian Rogersd8f69b02014-09-10 21:43:52 +00004270const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004271 if (precise) {
4272 // Precise constant type.
4273 return reg_types_.FromCat1Const(value, true);
4274 } else {
4275 // Imprecise constant type.
4276 if (value < -32768) {
4277 return reg_types_.IntConstant();
4278 } else if (value < -128) {
4279 return reg_types_.ShortConstant();
4280 } else if (value < 0) {
4281 return reg_types_.ByteConstant();
4282 } else if (value == 0) {
4283 return reg_types_.Zero();
4284 } else if (value == 1) {
4285 return reg_types_.One();
4286 } else if (value < 128) {
4287 return reg_types_.PosByteConstant();
4288 } else if (value < 32768) {
4289 return reg_types_.PosShortConstant();
4290 } else if (value < 65536) {
4291 return reg_types_.CharConstant();
4292 } else {
4293 return reg_types_.IntConstant();
4294 }
4295 }
4296}
4297
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004298void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004299 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004300}
4301
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004302void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004303 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004304}
jeffhaod1224c72012-02-29 13:43:08 -08004305
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004306void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4307 RegTypeCache::VisitStaticRoots(callback, arg);
4308}
4309
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004310void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4311 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004312}
4313
Ian Rogersd81871c2011-10-03 13:57:23 -07004314} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004315} // namespace art