blob: 1b3cc8ff93afbd03d2abe9e5e0098c3d8fa54eb1 [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
Ian Rogers7b078e82014-09-10 14:44:24 -0700106MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
107 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700108 bool allow_soft_failures,
109 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700110 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700111 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700112 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800113 bool early_failure = false;
114 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700115 const DexFile& dex_file = klass->GetDexFile();
116 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800117 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700118 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700119 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800120 early_failure = true;
121 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700122 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800123 early_failure = true;
124 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700125 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800126 early_failure = true;
127 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
128 }
129 if (early_failure) {
130 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
131 if (Runtime::Current()->IsCompiler()) {
132 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000133 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800134 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700135 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700136 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700137 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700139 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700140 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700141}
142
Ian Rogers7b078e82014-09-10 14:44:24 -0700143MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
144 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700145 Handle<mirror::DexCache> dex_cache,
146 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700147 const DexFile::ClassDef* class_def,
148 bool allow_soft_failures,
149 std::string* error) {
150 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700151 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700152 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700153 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700154 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700155 }
jeffhaof56197c2012-03-05 18:01:54 -0800156 ClassDataItemIterator it(*dex_file, class_data);
157 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
158 it.Next();
159 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700160 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700161 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700162 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700163 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800164 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700165 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800166 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700167 if (method_idx == previous_direct_method_idx) {
168 // smali can create dex files with two encoded_methods sharing the same method_idx
169 // http://code.google.com/p/smali/issues/detail?id=119
170 it.Next();
171 continue;
172 }
173 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700175 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700176 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
177 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700178 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700179 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700181 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700182 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700183 StackHandleScope<1> hs(self);
184 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700185 MethodVerifier::FailureKind result = VerifyMethod(self,
186 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700187 dex_file,
188 dex_cache,
189 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700190 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700191 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700192 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700193 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700194 allow_soft_failures,
195 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700196 if (result != kNoFailure) {
197 if (result == kHardFailure) {
198 hard_fail = true;
199 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700200 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700201 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700202 *error = "Verifier rejected class ";
203 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
204 *error += " due to bad method ";
205 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700206 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800208 }
209 it.Next();
210 }
jeffhao9b0b1882012-10-01 16:51:22 -0700211 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800212 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700213 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800214 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700215 if (method_idx == previous_virtual_method_idx) {
216 // smali can create dex files with two encoded_methods sharing the same method_idx
217 // http://code.google.com/p/smali/issues/detail?id=119
218 it.Next();
219 continue;
220 }
221 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700222 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700223 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700224 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
225 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700226 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700227 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700229 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700230 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700231 StackHandleScope<1> hs(self);
232 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700233 MethodVerifier::FailureKind result = VerifyMethod(self,
234 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700235 dex_file,
236 dex_cache,
237 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700238 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700239 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700240 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700241 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700242 allow_soft_failures,
243 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700244 if (result != kNoFailure) {
245 if (result == kHardFailure) {
246 hard_fail = true;
247 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700248 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700249 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700250 *error = "Verifier rejected class ";
251 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
252 *error += " due to bad method ";
253 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700254 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700255 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800256 }
257 it.Next();
258 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700259 if (error_count == 0) {
260 return kNoFailure;
261 } else {
262 return hard_fail ? kHardFailure : kSoftFailure;
263 }
jeffhaof56197c2012-03-05 18:01:54 -0800264}
265
Ian Rogers7b078e82014-09-10 14:44:24 -0700266MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700268 Handle<mirror::DexCache> dex_cache,
269 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700272 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700273 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700274 bool allow_soft_failures,
275 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700276 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700277 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700278
Ian Rogers7b078e82014-09-10 14:44:24 -0700279 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700280 method_idx, method, method_access_flags, true, allow_soft_failures,
281 need_precise_constants);
Ian Rogers46960fe2014-05-23 10:43:43 -0700282 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 // Verification completed, however failures may be pending that didn't cause the verification
284 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700285 CHECK(!verifier.have_pending_hard_failure_);
286 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700287 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700288 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700289 << PrettyMethod(method_idx, *dex_file) << "\n");
290 }
Ian Rogersc8982582012-09-07 16:53:25 -0700291 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800292 }
293 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700294 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700295 CHECK_NE(verifier.failures_.size(), 0U);
296 CHECK(verifier.have_pending_hard_failure_);
297 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700298 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800299 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700300 std::cout << "\n" << verifier.info_messages_.str();
301 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800302 }
Ian Rogersc8982582012-09-07 16:53:25 -0700303 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800304 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700305 if (kTimeVerifyMethod) {
306 uint64_t duration_ns = NanoTime() - start_ns;
307 if (duration_ns > MsToNs(100)) {
308 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
309 << " took " << PrettyDuration(duration_ns);
310 }
Ian Rogersc8982582012-09-07 16:53:25 -0700311 }
312 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800313}
314
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700315MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700316 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700317 Handle<mirror::DexCache> dex_cache,
318 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700319 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700321 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800322 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700323 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
324 class_def, code_item, dex_method_idx, method,
325 method_access_flags, true, true, true, true);
326 verifier->Verify();
327 verifier->DumpFailures(os);
328 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700329 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
330 // and querying any info is dangerous/can abort.
331 if (verifier->have_pending_hard_failure_) {
332 delete verifier;
333 return nullptr;
334 } else {
335 verifier->Dump(os);
336 return verifier;
337 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800338}
339
Ian Rogers7b078e82014-09-10 14:44:24 -0700340MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700341 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
342 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700343 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700344 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700345 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700346 bool can_load_classes, bool allow_soft_failures,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700347 bool need_precise_constants, bool verify_to_dump)
Ian Rogers7b078e82014-09-10 14:44:24 -0700348 : self_(self),
349 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800350 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800351 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700352 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700353 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700354 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800355 dex_file_(dex_file),
356 dex_cache_(dex_cache),
357 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700358 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800359 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700360 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700361 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700362 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700363 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700364 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800365 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800366 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700367 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200368 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700369 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200370 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700371 has_virtual_or_interface_invokes_(false),
372 verify_to_dump_(verify_to_dump) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800373 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700374 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800375}
376
Mathieu Chartier590fee92013-09-13 13:46:47 -0700377MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800378 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700379 STLDeleteElements(&failure_messages_);
380}
381
Brian Carlstromea46f952013-07-30 01:26:50 -0700382void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700383 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700384 Thread* self = Thread::Current();
385 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700386 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
387 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700388 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700389 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700390 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
391 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700392 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700394 verifier.FindLocksAtDexPc();
395}
396
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700397static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
398 const Instruction* inst = Instruction::At(code_item->insns_);
399
400 uint32_t insns_size = code_item->insns_size_in_code_units_;
401 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
402 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
403 return true;
404 }
405
406 dex_pc += inst->SizeInCodeUnits();
407 inst = inst->Next();
408 }
409
410 return false;
411}
412
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700413void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700414 CHECK(monitor_enter_dex_pcs_ != nullptr);
415 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700416
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700417 // Quick check whether there are any monitor_enter instructions at all.
418 if (!HasMonitorEnterInstructions(code_item_)) {
419 return;
420 }
421
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700422 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
423 // verification. In practice, the phase we want relies on data structures set up by all the
424 // earlier passes, so we just run the full method verification and bail out early when we've
425 // got what we wanted.
426 Verify();
427}
428
Brian Carlstromea46f952013-07-30 01:26:50 -0700429mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700430 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700431 Thread* self = Thread::Current();
432 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700433 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
434 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700435 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700436 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700437 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
438 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200439 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200440}
441
Brian Carlstromea46f952013-07-30 01:26:50 -0700442mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700443 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200444
445 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
446 // verification. In practice, the phase we want relies on data structures set up by all the
447 // earlier passes, so we just run the full method verification and bail out early when we've
448 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200449 bool success = Verify();
450 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700451 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200452 }
453 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700454 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700455 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200456 }
457 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
458 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200459}
460
Brian Carlstromea46f952013-07-30 01:26:50 -0700461mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700462 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700463 Thread* self = Thread::Current();
464 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700465 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
466 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700467 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700469 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
470 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200471 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200472}
473
Brian Carlstromea46f952013-07-30 01:26:50 -0700474mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700475 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200476
477 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
478 // verification. In practice, the phase we want relies on data structures set up by all the
479 // earlier passes, so we just run the full method verification and bail out early when we've
480 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200481 bool success = Verify();
482 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700483 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200484 }
485 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700486 if (register_line == nullptr) {
487 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200488 }
489 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
490 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
491 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200492}
493
Ian Rogersad0b3a32012-04-16 14:50:24 -0700494bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700496 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700497 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700499 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700500 } else {
501 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700502 }
jeffhaobdb76512011-09-07 11:43:16 -0700503 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700504 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
505 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
507 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700509 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800511 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 // Run through the instructions and see if the width checks out.
513 bool result = ComputeWidthsAndCountOps();
514 // Flag instructions guarded by a "try" block and check exception handlers.
515 result = result && ScanTryCatchBlocks();
516 // Perform static instruction verification.
517 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700518 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000519 result = result && VerifyCodeFlow();
520 // Compute information for compiler.
521 if (result && Runtime::Current()->IsCompiler()) {
522 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
523 }
524 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700525}
526
Ian Rogers776ac1f2012-04-13 23:36:36 -0700527std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700528 switch (error) {
529 case VERIFY_ERROR_NO_CLASS:
530 case VERIFY_ERROR_NO_FIELD:
531 case VERIFY_ERROR_NO_METHOD:
532 case VERIFY_ERROR_ACCESS_CLASS:
533 case VERIFY_ERROR_ACCESS_FIELD:
534 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700535 case VERIFY_ERROR_INSTANTIATION:
536 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800537 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700538 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
539 // class change and instantiation errors into soft verification errors so that we re-verify
540 // at runtime. We may fail to find or to agree on access because of not yet available class
541 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
542 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
543 // paths" that dynamically perform the verification and cause the behavior to be that akin
544 // to an interpreter.
545 error = VERIFY_ERROR_BAD_CLASS_SOFT;
546 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700547 // If we fail again at runtime, mark that this instruction would throw and force this
548 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700549 have_pending_runtime_throw_failure_ = true;
550 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700551 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700552 // Indication that verification should be retried at runtime.
553 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700554 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700555 have_pending_hard_failure_ = true;
556 }
557 break;
jeffhaod5347e02012-03-22 17:25:05 -0700558 // Hard verification failures at compile time will still fail at runtime, so the class is
559 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560 case VERIFY_ERROR_BAD_CLASS_HARD: {
561 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700562 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000563 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800564 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700565 have_pending_hard_failure_ = true;
566 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800567 }
568 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700569 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700570 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700571 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700572 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700573 failure_messages_.push_back(failure_message);
574 return *failure_message;
575}
576
Ian Rogers576ca0c2014-06-06 15:58:22 -0700577std::ostream& MethodVerifier::LogVerifyInfo() {
578 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
579 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
580}
581
Ian Rogersad0b3a32012-04-16 14:50:24 -0700582void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
583 size_t failure_num = failure_messages_.size();
584 DCHECK_NE(failure_num, 0U);
585 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
586 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700587 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700588 delete last_fail_message;
589}
590
591void MethodVerifier::AppendToLastFailMessage(std::string append) {
592 size_t failure_num = failure_messages_.size();
593 DCHECK_NE(failure_num, 0U);
594 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
595 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800596}
597
Ian Rogers776ac1f2012-04-13 23:36:36 -0700598bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 const uint16_t* insns = code_item_->insns_;
600 size_t insns_size = code_item_->insns_size_in_code_units_;
601 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700602 size_t new_instance_count = 0;
603 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700605
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700607 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700608 switch (opcode) {
609 case Instruction::APUT_OBJECT:
610 case Instruction::CHECK_CAST:
611 has_check_casts_ = true;
612 break;
613 case Instruction::INVOKE_VIRTUAL:
614 case Instruction::INVOKE_VIRTUAL_RANGE:
615 case Instruction::INVOKE_INTERFACE:
616 case Instruction::INVOKE_INTERFACE_RANGE:
617 has_virtual_or_interface_invokes_ = true;
618 break;
619 case Instruction::MONITOR_ENTER:
620 monitor_enter_count++;
621 break;
622 case Instruction::NEW_INSTANCE:
623 new_instance_count++;
624 break;
625 default:
626 break;
jeffhaobdb76512011-09-07 11:43:16 -0700627 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700629 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700630 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700631 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700632 }
633
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
636 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700637 return false;
638 }
639
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 new_instance_count_ = new_instance_count;
641 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700642 return true;
643}
644
Ian Rogers776ac1f2012-04-13 23:36:36 -0700645bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700647 if (tries_size == 0) {
648 return true;
649 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700651 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700652
653 for (uint32_t idx = 0; idx < tries_size; idx++) {
654 const DexFile::TryItem* try_item = &tries[idx];
655 uint32_t start = try_item->start_addr_;
656 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700657 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
659 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700660 return false;
661 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700663 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
664 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700665 return false;
666 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700667 uint32_t dex_pc = start;
668 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
669 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700671 size_t insn_size = inst->SizeInCodeUnits();
672 dex_pc += insn_size;
673 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700674 }
675 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800676 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700677 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700678 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700679 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700680 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700681 CatchHandlerIterator iterator(handlers_ptr);
682 for (; iterator.HasNext(); iterator.Next()) {
683 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700685 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
686 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700687 return false;
688 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100689 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
690 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
691 << "exception handler begins with move-result* (" << dex_pc << ")";
692 return false;
693 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700695 // Ensure exception types are resolved so that they don't need resolution to be delivered,
696 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700697 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800698 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
699 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700700 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700701 if (exception_type == nullptr) {
702 DCHECK(self_->IsExceptionPending());
703 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700704 }
705 }
jeffhaobdb76512011-09-07 11:43:16 -0700706 }
Ian Rogers0571d352011-11-03 19:51:38 -0700707 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700708 }
jeffhaobdb76512011-09-07 11:43:16 -0700709 return true;
710}
711
Ian Rogers776ac1f2012-04-13 23:36:36 -0700712bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700714
Ian Rogers0c7abda2012-09-19 13:33:42 -0700715 /* 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 -0700716 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700717 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700718
719 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700720 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700722 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 return false;
724 }
725 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700726 // All invoke points are marked as "Throw" points already.
727 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000728 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700729 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000730 } else if (inst->IsReturn()) {
731 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 }
733 dex_pc += inst->SizeInCodeUnits();
734 inst = inst->Next();
735 }
736 return true;
737}
738
Ian Rogers776ac1f2012-04-13 23:36:36 -0700739bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 bool result = true;
741 switch (inst->GetVerifyTypeArgumentA()) {
742 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700743 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 break;
745 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700746 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 break;
748 }
749 switch (inst->GetVerifyTypeArgumentB()) {
750 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700751 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 break;
753 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700754 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 break;
756 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700757 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 break;
759 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700760 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 break;
762 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700763 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 break;
765 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700766 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 break;
768 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700769 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 break;
771 }
772 switch (inst->GetVerifyTypeArgumentC()) {
773 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700774 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 break;
776 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700777 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 break;
779 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700780 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 break;
782 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700783 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 break;
785 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700786 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 break;
788 }
789 switch (inst->GetVerifyExtraFlags()) {
790 case Instruction::kVerifyArrayData:
791 result = result && CheckArrayData(code_offset);
792 break;
793 case Instruction::kVerifyBranchTarget:
794 result = result && CheckBranchTarget(code_offset);
795 break;
796 case Instruction::kVerifySwitchTargets:
797 result = result && CheckSwitchTargets(code_offset);
798 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700799 case Instruction::kVerifyVarArgNonZero:
800 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700801 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700802 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
804 "non-range invoke";
805 return false;
806 }
Ian Rogers29a26482014-05-02 15:27:29 -0700807 uint32_t args[Instruction::kMaxVarArgRegs];
808 inst->GetVarArgs(args);
809 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700811 }
Andreas Gampec3314312014-06-19 18:13:29 -0700812 case Instruction::kVerifyVarArgRangeNonZero:
813 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700815 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
816 inst->VRegA() <= 0) {
817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
818 "range invoke";
819 return false;
820 }
Ian Rogers29a26482014-05-02 15:27:29 -0700821 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 break;
823 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 result = false;
826 break;
827 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700828 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
830 result = false;
831 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 return result;
833}
834
Ian Rogers7b078e82014-09-10 14:44:24 -0700835inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
838 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 return false;
840 }
841 return true;
842}
843
Ian Rogers7b078e82014-09-10 14:44:24 -0700844inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
847 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 return false;
849 }
850 return true;
851}
852
Ian Rogers7b078e82014-09-10 14:44:24 -0700853inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
856 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 return false;
858 }
859 return true;
860}
861
Ian Rogers7b078e82014-09-10 14:44:24 -0700862inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
865 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 return false;
867 }
868 return true;
869}
870
Ian Rogers7b078e82014-09-10 14:44:24 -0700871inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
874 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 return false;
876 }
877 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700878 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 return false;
882 }
883 return true;
884}
885
Ian Rogers7b078e82014-09-10 14:44:24 -0700886inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
889 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 return false;
891 }
892 return true;
893}
894
Ian Rogers7b078e82014-09-10 14:44:24 -0700895inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
898 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 return false;
900 }
901 return true;
902}
903
Ian Rogers776ac1f2012-04-13 23:36:36 -0700904bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
907 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 return false;
909 }
910 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700911 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 const char* cp = descriptor;
913 while (*cp++ == '[') {
914 bracket_count++;
915 }
916 if (bracket_count == 0) {
917 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700918 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
919 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 return false;
921 } else if (bracket_count > 255) {
922 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
924 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 return false;
926 }
927 return true;
928}
929
Ian Rogers776ac1f2012-04-13 23:36:36 -0700930bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
932 const uint16_t* insns = code_item_->insns_ + cur_offset;
933 const uint16_t* array_data;
934 int32_t array_data_offset;
935
936 DCHECK_LT(cur_offset, insn_count);
937 /* make sure the start of the array data table is in range */
938 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
939 if ((int32_t) cur_offset + array_data_offset < 0 ||
940 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700942 << ", data offset " << array_data_offset
943 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
945 }
946 /* offset to array data table is a relative branch-style offset */
947 array_data = insns + array_data_offset;
948 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800949 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
951 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 return false;
953 }
954 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700955 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
957 /* make sure the end of the switch is in range */
958 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
960 << ", data offset " << array_data_offset << ", end "
961 << cur_offset + array_data_offset + table_size
962 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 return false;
964 }
965 return true;
966}
967
Ian Rogers776ac1f2012-04-13 23:36:36 -0700968bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 int32_t offset;
970 bool isConditional, selfOkay;
971 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
972 return false;
973 }
974 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
976 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 return false;
978 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700979 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
980 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
983 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 return false;
985 }
986 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
987 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700988 if (abs_offset < 0 ||
989 (uint32_t) abs_offset >= insn_count ||
990 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700992 << reinterpret_cast<void*>(abs_offset) << ") at "
993 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return false;
995 }
996 insn_flags_[abs_offset].SetBranchTarget();
997 return true;
998}
999
Ian Rogers776ac1f2012-04-13 23:36:36 -07001000bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 bool* selfOkay) {
1002 const uint16_t* insns = code_item_->insns_ + cur_offset;
1003 *pConditional = false;
1004 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001005 switch (*insns & 0xff) {
1006 case Instruction::GOTO:
1007 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001008 break;
1009 case Instruction::GOTO_32:
1010 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001011 *selfOkay = true;
1012 break;
1013 case Instruction::GOTO_16:
1014 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001015 break;
1016 case Instruction::IF_EQ:
1017 case Instruction::IF_NE:
1018 case Instruction::IF_LT:
1019 case Instruction::IF_GE:
1020 case Instruction::IF_GT:
1021 case Instruction::IF_LE:
1022 case Instruction::IF_EQZ:
1023 case Instruction::IF_NEZ:
1024 case Instruction::IF_LTZ:
1025 case Instruction::IF_GEZ:
1026 case Instruction::IF_GTZ:
1027 case Instruction::IF_LEZ:
1028 *pOffset = (int16_t) insns[1];
1029 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001030 break;
1031 default:
1032 return false;
1033 break;
1034 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001035 return true;
1036}
1037
Ian Rogers776ac1f2012-04-13 23:36:36 -07001038bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001040 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001042 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1044 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001046 << ", switch offset " << switch_offset
1047 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001048 return false;
1049 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001050 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001052 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001053 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1055 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001056 return false;
1057 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 uint32_t switch_count = switch_insns[1];
1059 int32_t keys_offset, targets_offset;
1060 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001061 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1062 /* 0=sig, 1=count, 2/3=firstKey */
1063 targets_offset = 4;
1064 keys_offset = -1;
1065 expected_signature = Instruction::kPackedSwitchSignature;
1066 } else {
1067 /* 0=sig, 1=count, 2..count*2 = keys */
1068 keys_offset = 2;
1069 targets_offset = 2 + 2 * switch_count;
1070 expected_signature = Instruction::kSparseSwitchSignature;
1071 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001073 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001074 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1075 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1076 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001077 return false;
1078 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001079 /* make sure the end of the switch is in range */
1080 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1082 << ", switch offset " << switch_offset
1083 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001084 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001085 return false;
1086 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001087 /* for a sparse switch, verify the keys are in ascending order */
1088 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1090 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001091 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1092 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1093 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1095 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001096 return false;
1097 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001098 last_key = key;
1099 }
1100 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001101 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 for (uint32_t targ = 0; targ < switch_count; targ++) {
1103 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1104 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1105 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001106 if (abs_offset < 0 ||
1107 abs_offset >= (int32_t) insn_count ||
1108 !insn_flags_[abs_offset].IsOpcode()) {
1109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1110 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1111 << reinterpret_cast<void*>(cur_offset)
1112 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 return false;
1114 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 insn_flags_[abs_offset].SetBranchTarget();
1116 }
1117 return true;
1118}
1119
Ian Rogers776ac1f2012-04-13 23:36:36 -07001120bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001121 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 return false;
1124 }
1125 uint16_t registers_size = code_item_->registers_size_;
1126 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001127 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1129 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 return false;
1131 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 }
1133
1134 return true;
1135}
1136
Ian Rogers776ac1f2012-04-13 23:36:36 -07001137bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 uint16_t registers_size = code_item_->registers_size_;
1139 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1140 // integer overflow when adding them here.
1141 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001142 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1143 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001144 return false;
1145 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001146 return true;
1147}
1148
Ian Rogers776ac1f2012-04-13 23:36:36 -07001149bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 uint16_t registers_size = code_item_->registers_size_;
1151 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001152
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001154 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1155 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 }
1157 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001158 reg_table_.Init(kTrackCompilerInterestPoints,
1159 insn_flags_.get(),
1160 insns_size,
1161 registers_size,
1162 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001163
jeffhaobdb76512011-09-07 11:43:16 -07001164
Ian Rogersd0fbd852013-09-24 18:17:04 -07001165 work_line_.reset(RegisterLine::Create(registers_size, this));
1166 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001167
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 /* Initialize register types of method arguments. */
1169 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001170 DCHECK_NE(failures_.size(), 0U);
1171 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001172 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001173 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 return false;
1175 }
1176 /* Perform code flow verification. */
1177 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001178 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001180 }
jeffhaobdb76512011-09-07 11:43:16 -07001181 return true;
1182}
1183
Ian Rogersad0b3a32012-04-16 14:50:24 -07001184std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1185 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001186 for (size_t i = 0; i < failures_.size(); ++i) {
1187 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001188 }
1189 return os;
1190}
1191
Ian Rogers776ac1f2012-04-13 23:36:36 -07001192void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001193 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001194 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 return;
jeffhaobdb76512011-09-07 11:43:16 -07001196 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001197 {
1198 os << "Register Types:\n";
1199 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1200 std::ostream indent_os(&indent_filter);
1201 reg_types_.Dump(indent_os);
1202 }
Ian Rogersb4903572012-10-11 11:52:56 -07001203 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1205 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 const Instruction* inst = Instruction::At(code_item_->insns_);
1207 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001208 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001210 if (reg_line != nullptr) {
1211 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001212 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001213 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001214 const bool kDumpHexOfInstruction = false;
1215 if (kDumpHexOfInstruction) {
1216 indent_os << inst->DumpHex(5) << " ";
1217 }
1218 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001219 inst = inst->Next();
1220 }
jeffhaobdb76512011-09-07 11:43:16 -07001221}
1222
Ian Rogersd81871c2011-10-03 13:57:23 -07001223static bool IsPrimitiveDescriptor(char descriptor) {
1224 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001225 case 'I':
1226 case 'C':
1227 case 'S':
1228 case 'B':
1229 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001230 case 'F':
1231 case 'D':
1232 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001234 default:
1235 return false;
1236 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001237}
1238
Ian Rogers776ac1f2012-04-13 23:36:36 -07001239bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 RegisterLine* reg_line = reg_table_.GetLine(0);
1241 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1242 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001243
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001245 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001247 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1249 // argument as uninitialized. This restricts field access until the superclass constructor is
1250 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001251 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001252 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001253 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 reg_types_.UninitializedThisArgument(declaring_class));
1255 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001256 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001257 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001259 }
1260
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001261 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001262 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001263 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001264
1265 for (; iterator.HasNext(); iterator.Next()) {
1266 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001267 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 LOG(FATAL) << "Null descriptor";
1269 }
1270 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1272 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 return false;
1274 }
1275 switch (descriptor[0]) {
1276 case 'L':
1277 case '[':
1278 // We assume that reference arguments are initialized. The only way it could be otherwise
1279 // (assuming the caller was verified) is if the current method is <init>, but in that case
1280 // it's effectively considered initialized the instant we reach here (in the sense that we
1281 // can return without doing anything or call virtual methods).
1282 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001283 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001284 if (!reg_type.IsNonZeroReferenceTypes()) {
1285 DCHECK(HasFailures());
1286 return false;
1287 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001288 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 }
1290 break;
1291 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001292 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 break;
1294 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001295 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 break;
1297 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001298 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 break;
1300 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001301 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 break;
1303 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001304 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 break;
1306 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001307 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 break;
1309 case 'J':
1310 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001311 if (cur_arg + 1 >= expected_args) {
1312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1313 << " args, found more (" << descriptor << ")";
1314 return false;
1315 }
1316
Ian Rogers7b078e82014-09-10 14:44:24 -07001317 const RegType* lo_half;
1318 const RegType* hi_half;
1319 if (descriptor[0] == 'J') {
1320 lo_half = &reg_types_.LongLo();
1321 hi_half = &reg_types_.LongHi();
1322 } else {
1323 lo_half = &reg_types_.DoubleLo();
1324 hi_half = &reg_types_.DoubleHi();
1325 }
1326 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 cur_arg++;
1328 break;
1329 }
1330 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1332 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 return false;
1334 }
1335 cur_arg++;
1336 }
1337 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1339 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 return false;
1341 }
1342 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1343 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1344 // format. Only major difference from the method argument format is that 'V' is supported.
1345 bool result;
1346 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1347 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001348 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 size_t i = 0;
1350 do {
1351 i++;
1352 } while (descriptor[i] == '['); // process leading [
1353 if (descriptor[i] == 'L') { // object array
1354 do {
1355 i++; // find closing ;
1356 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1357 result = descriptor[i] == ';';
1358 } else { // primitive array
1359 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1360 }
1361 } else if (descriptor[0] == 'L') {
1362 // could be more thorough here, but shouldn't be required
1363 size_t i = 0;
1364 do {
1365 i++;
1366 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1367 result = descriptor[i] == ';';
1368 } else {
1369 result = false;
1370 }
1371 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1373 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 }
1375 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001376}
1377
Ian Rogers776ac1f2012-04-13 23:36:36 -07001378bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 const uint16_t* insns = code_item_->insns_;
1380 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001381
jeffhaobdb76512011-09-07 11:43:16 -07001382 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 insn_flags_[0].SetChanged();
1384 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001385
jeffhaobdb76512011-09-07 11:43:16 -07001386 /* Continue until no instructions are marked "changed". */
1387 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001388 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1390 uint32_t insn_idx = start_guess;
1391 for (; insn_idx < insns_size; insn_idx++) {
1392 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001393 break;
1394 }
jeffhaobdb76512011-09-07 11:43:16 -07001395 if (insn_idx == insns_size) {
1396 if (start_guess != 0) {
1397 /* try again, starting from the top */
1398 start_guess = 0;
1399 continue;
1400 } else {
1401 /* all flags are clear */
1402 break;
1403 }
1404 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 // We carry the working set of registers from instruction to instruction. If this address can
1406 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1407 // "changed" flags, we need to load the set of registers from the table.
1408 // Because we always prefer to continue on to the next instruction, we should never have a
1409 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1410 // target.
1411 work_insn_idx_ = insn_idx;
1412 if (insn_flags_[insn_idx].IsBranchTarget()) {
1413 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001414 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001415 /*
1416 * Sanity check: retrieve the stored register line (assuming
1417 * a full table) and make sure it actually matches.
1418 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001420 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 if (work_line_->CompareLine(register_line) != 0) {
1422 Dump(std::cout);
1423 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001424 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001425 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001426 << " work_line=" << work_line_->Dump(this) << "\n"
1427 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 }
jeffhaobdb76512011-09-07 11:43:16 -07001429 }
jeffhaobdb76512011-09-07 11:43:16 -07001430 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001432 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001433 prepend += " failed to verify: ";
1434 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 return false;
1436 }
jeffhaobdb76512011-09-07 11:43:16 -07001437 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 insn_flags_[insn_idx].SetVisited();
1439 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001440 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001441
Ian Rogers1c849e52012-06-28 14:00:33 -07001442 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001443 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001444 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001445 * (besides the wasted space), but it indicates a flaw somewhere
1446 * down the line, possibly in the verifier.
1447 *
1448 * If we've substituted "always throw" instructions into the stream,
1449 * we are almost certainly going to have some dead code.
1450 */
1451 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001453 for (; insn_idx < insns_size;
1454 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001455 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001456 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001457 * may or may not be preceded by a padding NOP (for alignment).
1458 */
1459 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1460 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1461 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001462 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001463 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1464 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1465 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001467 }
1468
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001470 if (dead_start < 0)
1471 dead_start = insn_idx;
1472 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001473 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1474 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001475 dead_start = -1;
1476 }
1477 }
1478 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001479 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1480 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001482 // To dump the state of the verify after a method, do something like:
1483 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1484 // "boolean java.lang.String.equals(java.lang.Object)") {
1485 // LOG(INFO) << info_messages_.str();
1486 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001487 }
jeffhaobdb76512011-09-07 11:43:16 -07001488 return true;
1489}
1490
Ian Rogers776ac1f2012-04-13 23:36:36 -07001491bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001492 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1493 // We want the state _before_ the instruction, for the case where the dex pc we're
1494 // interested in is itself a monitor-enter instruction (which is a likely place
1495 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001496 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001497 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001498 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1499 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1500 }
1501 }
1502
jeffhaobdb76512011-09-07 11:43:16 -07001503 /*
1504 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001505 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001506 * control to another statement:
1507 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001508 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001509 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001510 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001511 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001512 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001513 * throw an exception that is handled by an encompassing "try"
1514 * block.
1515 *
1516 * We can also return, in which case there is no successor instruction
1517 * from this point.
1518 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001520 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1522 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001523 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001524
jeffhaobdb76512011-09-07 11:43:16 -07001525 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001526 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001527 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001529 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001530 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 }
jeffhaobdb76512011-09-07 11:43:16 -07001532
1533 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001534 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001535 * can throw an exception, we will copy/merge this into the "catch"
1536 * address rather than work_line, because we don't want the result
1537 * from the "successful" code path (e.g. a check-cast that "improves"
1538 * a type) to be visible to the exception handler.
1539 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001540 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001542 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001544 }
1545
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001546
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001547 // We need to ensure the work line is consistent while performing validation. When we spot a
1548 // peephole pattern we compute a new line for either the fallthrough instruction or the
1549 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001550 std::unique_ptr<RegisterLine> branch_line;
1551 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001552
Sebastien Hertz5243e912013-05-21 10:55:07 +02001553 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001554 case Instruction::NOP:
1555 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001556 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001557 * a signature that looks like a NOP; if we see one of these in
1558 * the course of executing code then we have a problem.
1559 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001562 }
1563 break;
1564
1565 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001566 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001567 break;
jeffhaobdb76512011-09-07 11:43:16 -07001568 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001569 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001570 break;
jeffhaobdb76512011-09-07 11:43:16 -07001571 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001572 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001573 break;
1574 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001575 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001576 break;
jeffhaobdb76512011-09-07 11:43:16 -07001577 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001578 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001579 break;
jeffhaobdb76512011-09-07 11:43:16 -07001580 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001581 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001582 break;
1583 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001584 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001585 break;
jeffhaobdb76512011-09-07 11:43:16 -07001586 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001587 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001588 break;
jeffhaobdb76512011-09-07 11:43:16 -07001589 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001590 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001591 break;
1592
1593 /*
1594 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001595 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001596 * might want to hold the result in an actual CPU register, so the
1597 * Dalvik spec requires that these only appear immediately after an
1598 * invoke or filled-new-array.
1599 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001600 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001601 * redundant with the reset done below, but it can make the debug info
1602 * easier to read in some cases.)
1603 */
1604 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001605 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001606 break;
1607 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001608 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
1610 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001611 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001612 break;
1613
Ian Rogersd81871c2011-10-03 13:57:23 -07001614 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001615 /*
jeffhao60f83e32012-02-13 17:16:30 -08001616 * This statement can only appear as the first instruction in an exception handler. We verify
1617 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001618 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001619 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001620 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001621 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001622 }
jeffhaobdb76512011-09-07 11:43:16 -07001623 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001624 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001625 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001626 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 }
jeffhaobdb76512011-09-07 11:43:16 -07001628 }
1629 break;
1630 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001631 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001632 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001633 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1636 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 } else {
1638 // Compilers may generate synthetic functions that write byte values into boolean fields.
1639 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001641 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001642 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1643 ((return_type.IsBoolean() || return_type.IsByte() ||
1644 return_type.IsShort() || return_type.IsChar()) &&
1645 src_type.IsInteger()));
1646 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001647 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001648 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001649 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 }
jeffhaobdb76512011-09-07 11:43:16 -07001652 }
1653 }
1654 break;
1655 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001656 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001657 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001658 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 } else {
1662 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001663 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001664 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001665 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001666 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 }
jeffhaobdb76512011-09-07 11:43:16 -07001668 }
1669 }
1670 break;
1671 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001672 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001673 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 } else {
1677 /* return_type is the *expected* return type, not register value */
1678 DCHECK(!return_type.IsZero());
1679 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001680 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001681 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001682 // Disallow returning uninitialized values and verify that the reference in vAA is an
1683 // instance of the "return_type"
1684 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001685 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1686 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001687 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001688 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1689 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1690 << "' or '" << reg_type << "'";
1691 } else {
1692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1693 << "', but expected from declaration '" << return_type << "'";
1694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 }
1696 }
1697 }
1698 break;
1699
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001700 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001701 case Instruction::CONST_4: {
1702 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001703 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001704 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001705 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001706 }
1707 case Instruction::CONST_16: {
1708 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001709 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001710 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001711 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001713 case Instruction::CONST: {
1714 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001715 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001716 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001717 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001718 }
1719 case Instruction::CONST_HIGH16: {
1720 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001721 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001722 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001723 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001724 }
jeffhaobdb76512011-09-07 11:43:16 -07001725 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001726 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001728 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1729 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001731 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 }
1733 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001735 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1736 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001737 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001738 break;
1739 }
1740 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001741 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001742 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1743 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001745 break;
1746 }
1747 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001748 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001749 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1750 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001751 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001752 break;
1753 }
jeffhaobdb76512011-09-07 11:43:16 -07001754 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001755 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001756 break;
jeffhaobdb76512011-09-07 11:43:16 -07001757 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001758 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001761 // Get type from instruction if unresolved then we need an access check
1762 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001763 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001764 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001765 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001766 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001767 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001768 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 }
jeffhaobdb76512011-09-07 11:43:16 -07001770 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001771 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001772 break;
1773 case Instruction::MONITOR_EXIT:
1774 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001775 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001776 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001777 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001778 * to the need to handle asynchronous exceptions, a now-deprecated
1779 * feature that Dalvik doesn't support.)
1780 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001781 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001782 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001783 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001784 * structured locking checks are working, the former would have
1785 * failed on the -enter instruction, and the latter is impossible.
1786 *
1787 * This is fortunate, because issue 3221411 prevents us from
1788 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001789 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001790 * some catch blocks (which will show up as "dead" code when
1791 * we skip them here); if we can't, then the code path could be
1792 * "live" so we still need to check it.
1793 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001794 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001795 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797
Ian Rogers28ad40d2011-10-27 15:19:26 -07001798 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001800 /*
1801 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1802 * could be a "upcast" -- not expected, so we don't try to address it.)
1803 *
1804 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001805 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001806 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001807 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1808 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001809 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001810 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001811 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001812 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001813 if (klass != nullptr && klass->IsPrimitive()) {
1814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1815 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1816 << GetDeclaringClass();
1817 break;
1818 }
1819
Ian Rogersad0b3a32012-04-16 14:50:24 -07001820 DCHECK_NE(failures_.size(), 0U);
1821 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001822 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001823 }
1824 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001825 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001826 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001827 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001828 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001829 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 if (is_checkcast) {
1831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1832 } else {
1833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1834 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001835 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 if (is_checkcast) {
1837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1838 } else {
1839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1840 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001841 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001842 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001843 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001845 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001846 }
jeffhaobdb76512011-09-07 11:43:16 -07001847 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001848 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001849 }
1850 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001852 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001853 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001856 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001858 } else {
1859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 }
1861 break;
1862 }
1863 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001864 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001865 if (res_type.IsConflict()) {
1866 DCHECK_NE(failures_.size(), 0U);
1867 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001868 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001869 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1870 // can't create an instance of an interface or abstract class */
1871 if (!res_type.IsInstantiableTypes()) {
1872 Fail(VERIFY_ERROR_INSTANTIATION)
1873 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001874 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001876 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001877 // Any registers holding previous allocations from this address that have not yet been
1878 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001880 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001881 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 break;
1883 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001884 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001885 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001886 break;
1887 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001888 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001889 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001890 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001891 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001892 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001893 just_set_result = true; // Filled new array range sets result register
1894 break;
jeffhaobdb76512011-09-07 11:43:16 -07001895 case Instruction::CMPL_FLOAT:
1896 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001898 break;
1899 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001900 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001901 break;
1902 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001903 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
1905 case Instruction::CMPL_DOUBLE:
1906 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001908 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001909 break;
1910 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001911 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001912 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001913 break;
1914 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001915 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001916 break;
1917 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001920 break;
1921 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001922 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001923 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001924 break;
1925 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001927 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001930 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001931 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1932 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001933 }
1934 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 }
jeffhaobdb76512011-09-07 11:43:16 -07001936 case Instruction::GOTO:
1937 case Instruction::GOTO_16:
1938 case Instruction::GOTO_32:
1939 /* no effect on or use of registers */
1940 break;
1941
1942 case Instruction::PACKED_SWITCH:
1943 case Instruction::SPARSE_SWITCH:
1944 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001945 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001946 break;
1947
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 case Instruction::FILL_ARRAY_DATA: {
1949 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001950 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001951 /* array_type can be null if the reg type is Zero */
1952 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001953 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1955 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001956 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001957 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001958 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001959 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1961 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001962 } else {
jeffhao457cc512012-02-02 16:55:13 -08001963 // Now verify if the element width in the table matches the element width declared in
1964 // the array
1965 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1966 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001968 } else {
1969 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1970 // Since we don't compress the data in Dex, expect to see equal width of data stored
1971 // in the table and expected from the array class.
1972 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1974 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001975 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 }
1977 }
jeffhaobdb76512011-09-07 11:43:16 -07001978 }
1979 }
1980 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001984 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1985 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 bool mismatch = false;
1987 if (reg_type1.IsZero()) { // zero then integral or reference expected
1988 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1989 } else if (reg_type1.IsReferenceTypes()) { // both references?
1990 mismatch = !reg_type2.IsReferenceTypes();
1991 } else { // both integral?
1992 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1993 }
1994 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1996 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001997 }
1998 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 }
jeffhaobdb76512011-09-07 11:43:16 -07002000 case Instruction::IF_LT:
2001 case Instruction::IF_GE:
2002 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002004 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2005 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2008 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002009 }
2010 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 }
jeffhaobdb76512011-09-07 11:43:16 -07002012 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002014 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2017 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002019
2020 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002021 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002022 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002023 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002024 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002025 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002026 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002027 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2028 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2029 work_insn_idx_)) {
2030 break;
2031 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002032 } else {
2033 break;
2034 }
2035
Ian Rogers9b360392013-06-06 14:45:07 -07002036 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002037
2038 /* Check for peep-hole pattern of:
2039 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002040 * instance-of vX, vY, T;
2041 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002042 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002043 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002044 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002045 * and sharpen the type of vY to be type T.
2046 * Note, this pattern can't be if:
2047 * - if there are other branches to this branch,
2048 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002049 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002050 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002051 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2052 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2053 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002054 // Check the type of the instance-of is different than that of registers type, as if they
2055 // are the same there is no work to be done here. Check that the conversion is not to or
2056 // from an unresolved type as type information is imprecise. If the instance-of is to an
2057 // interface then ignore the type information as interfaces can only be treated as Objects
2058 // and we don't want to disallow field and other operations on the object. If the value
2059 // being instance-of checked against is known null (zero) then allow the optimization as
2060 // we didn't have type information. If the merge of the instance-of type with the original
2061 // type is assignable to the original then allow optimization. This check is performed to
2062 // ensure that subsequent merges don't lose type information - such as becoming an
2063 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002065 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002066
Ian Rogersebbdd872014-07-07 23:53:08 -07002067 if (!orig_type.Equals(cast_type) &&
2068 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002069 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002070 !cast_type.GetClass()->IsInterface() &&
2071 (orig_type.IsZero() ||
2072 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002073 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002074 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002075 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002076 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002077 branch_line.reset(update_line);
2078 }
2079 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002081 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2082 // See if instance-of was preceded by a move-object operation, common due to the small
2083 // register encoding space of instance-of, and propagate type information to the source
2084 // of the move-object.
2085 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002086 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002087 move_idx--;
2088 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002089 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2090 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2091 work_insn_idx_)) {
2092 break;
2093 }
Ian Rogers9b360392013-06-06 14:45:07 -07002094 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2095 switch (move_inst->Opcode()) {
2096 case Instruction::MOVE_OBJECT:
2097 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002098 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002099 }
2100 break;
2101 case Instruction::MOVE_OBJECT_FROM16:
2102 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002103 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002104 }
2105 break;
2106 case Instruction::MOVE_OBJECT_16:
2107 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002108 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002109 }
2110 break;
2111 default:
2112 break;
2113 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002114 }
2115 }
2116 }
2117
jeffhaobdb76512011-09-07 11:43:16 -07002118 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 }
jeffhaobdb76512011-09-07 11:43:16 -07002120 case Instruction::IF_LTZ:
2121 case Instruction::IF_GEZ:
2122 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002124 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2127 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 }
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 }
jeffhaobdb76512011-09-07 11:43:16 -07002131 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 break;
jeffhaobdb76512011-09-07 11:43:16 -07002134 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 break;
jeffhaobdb76512011-09-07 11:43:16 -07002137 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 break;
jeffhaobdb76512011-09-07 11:43:16 -07002140 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 break;
jeffhaobdb76512011-09-07 11:43:16 -07002146 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 break;
2149 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 break;
2156 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 break;
2159 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 break;
2162 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174
jeffhaobdb76512011-09-07 11:43:16 -07002175 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002176 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 break;
jeffhaobdb76512011-09-07 11:43:16 -07002178 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002179 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 break;
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002182 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002183 break;
jeffhaobdb76512011-09-07 11:43:16 -07002184 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002185 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 break;
2187 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002188 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002191 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
2193 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002194 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2195 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 break;
jeffhaobdb76512011-09-07 11:43:16 -07002197
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002199 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 break;
2201 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002202 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 break;
2204 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002205 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 break;
2207 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002208 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002209 break;
2210 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002211 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002212 break;
2213 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002214 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 break;
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002217 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2218 false);
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220
jeffhaobdb76512011-09-07 11:43:16 -07002221 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002222 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 break;
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002225 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 break;
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002228 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 break;
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002231 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 break;
2233 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002234 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
2236 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002237 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
2239 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002240 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2241 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 break;
2243
2244 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002245 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 break;
2247 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002248 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 break;
2250 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002251 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 break;
2253 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002254 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002257 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002258 break;
2259 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002260 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002261 break;
2262 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002263 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2264 true);
jeffhaobdb76512011-09-07 11:43:16 -07002265 break;
2266
2267 case Instruction::INVOKE_VIRTUAL:
2268 case Instruction::INVOKE_VIRTUAL_RANGE:
2269 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2272 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002273 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2274 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002275 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2276 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002277 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002278 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002279 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002280 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002281 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002282 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002283 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2284 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002285 return_type_class->CannotBeAssignedFromOtherTypes());
2286 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002287 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2288 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002289 }
2290 }
2291 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002292 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002293 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2294 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002295 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002296 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002297 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002298 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002299 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002301 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002302 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002303 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 }
jeffhaobdb76512011-09-07 11:43:16 -07002306 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002309 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002310 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002311 const char* return_type_descriptor;
2312 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002313 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002314 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002315 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002316 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002317 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002318 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2319 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2320 } else {
2321 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002322 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002323 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002324 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002325 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002326 if (return_type_class != nullptr) {
2327 return_type = &reg_types_.FromClass(return_type_descriptor,
2328 return_type_class,
2329 return_type_class->CannotBeAssignedFromOtherTypes());
2330 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002331 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2332 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002333 }
Ian Rogers46685432012-06-03 22:26:43 -07002334 }
2335 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002336 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 * Some additional checks when calling a constructor. We know from the invocation arg check
2338 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2339 * that to require that called_method->klass is the same as this->klass or this->super,
2340 * allowing the latter only if the "this" argument is the same as the "this" argument to
2341 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002342 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002343 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002344 if (this_type.IsConflict()) // failure.
2345 break;
jeffhaobdb76512011-09-07 11:43:16 -07002346
jeffhaob57e9522012-04-26 18:08:21 -07002347 /* no null refs allowed (?) */
2348 if (this_type.IsZero()) {
2349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2350 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002351 }
jeffhaob57e9522012-04-26 18:08:21 -07002352
2353 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002354 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002355 // TODO: re-enable constructor type verification
2356 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002357 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002358 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2359 // break;
2360 // }
jeffhaob57e9522012-04-26 18:08:21 -07002361
2362 /* arg must be an uninitialized reference */
2363 if (!this_type.IsUninitializedTypes()) {
2364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2365 << this_type;
2366 break;
2367 }
2368
2369 /*
2370 * Replace the uninitialized reference with an initialized one. We need to do this for all
2371 * registers that have the same object instance in them, not just the "this" register.
2372 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002373 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002374 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002375 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002376 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2377 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002378 }
2379 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002380 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002381 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002382 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002384 just_set_result = true;
2385 break;
2386 }
2387 case Instruction::INVOKE_STATIC:
2388 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002390 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002391 METHOD_STATIC,
2392 is_range,
2393 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002394 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002395 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002397 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2398 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002399 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002400 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002401 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002402 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002403 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002404 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002405 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002406 } else {
2407 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2408 }
jeffhaobdb76512011-09-07 11:43:16 -07002409 just_set_result = true;
2410 }
2411 break;
jeffhaobdb76512011-09-07 11:43:16 -07002412 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002414 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002415 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002416 METHOD_INTERFACE,
2417 is_range,
2418 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002419 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002420 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002421 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2422 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2423 << PrettyMethod(abs_method) << "'";
2424 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002425 }
Ian Rogers0d604842012-04-16 14:50:24 -07002426 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002427 /* Get the type of the "this" arg, which should either be a sub-interface of called
2428 * interface or Object (see comments in RegType::JoinClass).
2429 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002430 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002431 if (this_type.IsZero()) {
2432 /* null pointer always passes (and always fails at runtime) */
2433 } else {
2434 if (this_type.IsUninitializedTypes()) {
2435 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2436 << this_type;
2437 break;
2438 }
2439 // In the past we have tried to assert that "called_interface" is assignable
2440 // from "this_type.GetClass()", however, as we do an imprecise Join
2441 // (RegType::JoinClass) we don't have full information on what interfaces are
2442 // implemented by "this_type". For example, two classes may implement the same
2443 // interfaces and have a common parent that doesn't implement the interface. The
2444 // join will set "this_type" to the parent class and a test that this implements
2445 // the interface will incorrectly fail.
2446 }
2447 /*
2448 * We don't have an object instance, so we can't find the concrete method. However, all of
2449 * the type information is in the abstract method, so we're good.
2450 */
2451 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002452 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002453 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002454 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2455 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2456 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2457 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002458 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002459 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002460 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002461 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002462 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002463 } else {
2464 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2465 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002466 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 }
jeffhaobdb76512011-09-07 11:43:16 -07002469 case Instruction::NEG_INT:
2470 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002471 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::NEG_LONG:
2474 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002475 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002476 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002477 break;
2478 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002479 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002480 break;
2481 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002483 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002486 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002487 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002490 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002493 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002494 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002497 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002498 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002502 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002503 break;
2504 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002506 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
2508 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002509 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002513 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002517 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002520 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002521 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002524 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002525 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002526 break;
2527 case Instruction::DOUBLE_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_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
2531 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002532 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002535 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
2537 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002539 break;
2540
2541 case Instruction::ADD_INT:
2542 case Instruction::SUB_INT:
2543 case Instruction::MUL_INT:
2544 case Instruction::REM_INT:
2545 case Instruction::DIV_INT:
2546 case Instruction::SHL_INT:
2547 case Instruction::SHR_INT:
2548 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002550 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::AND_INT:
2553 case Instruction::OR_INT:
2554 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002556 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::ADD_LONG:
2559 case Instruction::SUB_LONG:
2560 case Instruction::MUL_LONG:
2561 case Instruction::DIV_LONG:
2562 case Instruction::REM_LONG:
2563 case Instruction::AND_LONG:
2564 case Instruction::OR_LONG:
2565 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002567 reg_types_.LongLo(), reg_types_.LongHi(),
2568 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
2570 case Instruction::SHL_LONG:
2571 case Instruction::SHR_LONG:
2572 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002574 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002575 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002576 break;
2577 case Instruction::ADD_FLOAT:
2578 case Instruction::SUB_FLOAT:
2579 case Instruction::MUL_FLOAT:
2580 case Instruction::DIV_FLOAT:
2581 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002582 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2583 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::ADD_DOUBLE:
2586 case Instruction::SUB_DOUBLE:
2587 case Instruction::MUL_DOUBLE:
2588 case Instruction::DIV_DOUBLE:
2589 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002590 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002591 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2592 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::ADD_INT_2ADDR:
2595 case Instruction::SUB_INT_2ADDR:
2596 case Instruction::MUL_INT_2ADDR:
2597 case Instruction::REM_INT_2ADDR:
2598 case Instruction::SHL_INT_2ADDR:
2599 case Instruction::SHR_INT_2ADDR:
2600 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002601 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2602 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
2604 case Instruction::AND_INT_2ADDR:
2605 case Instruction::OR_INT_2ADDR:
2606 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002607 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2608 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002611 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2612 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002613 break;
2614 case Instruction::ADD_LONG_2ADDR:
2615 case Instruction::SUB_LONG_2ADDR:
2616 case Instruction::MUL_LONG_2ADDR:
2617 case Instruction::DIV_LONG_2ADDR:
2618 case Instruction::REM_LONG_2ADDR:
2619 case Instruction::AND_LONG_2ADDR:
2620 case Instruction::OR_LONG_2ADDR:
2621 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002622 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002623 reg_types_.LongLo(), reg_types_.LongHi(),
2624 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626 case Instruction::SHL_LONG_2ADDR:
2627 case Instruction::SHR_LONG_2ADDR:
2628 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002629 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002630 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632 case Instruction::ADD_FLOAT_2ADDR:
2633 case Instruction::SUB_FLOAT_2ADDR:
2634 case Instruction::MUL_FLOAT_2ADDR:
2635 case Instruction::DIV_FLOAT_2ADDR:
2636 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002637 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2638 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::ADD_DOUBLE_2ADDR:
2641 case Instruction::SUB_DOUBLE_2ADDR:
2642 case Instruction::MUL_DOUBLE_2ADDR:
2643 case Instruction::DIV_DOUBLE_2ADDR:
2644 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002645 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2647 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002650 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002651 case Instruction::MUL_INT_LIT16:
2652 case Instruction::DIV_INT_LIT16:
2653 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002654 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2655 true);
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::AND_INT_LIT16:
2658 case Instruction::OR_INT_LIT16:
2659 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002660 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2661 true);
jeffhaobdb76512011-09-07 11:43:16 -07002662 break;
2663 case Instruction::ADD_INT_LIT8:
2664 case Instruction::RSUB_INT_LIT8:
2665 case Instruction::MUL_INT_LIT8:
2666 case Instruction::DIV_INT_LIT8:
2667 case Instruction::REM_INT_LIT8:
2668 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002669 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002670 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002671 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2672 false);
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::AND_INT_LIT8:
2675 case Instruction::OR_INT_LIT8:
2676 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2678 false);
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002681 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002682 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002683 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2685 }
2686 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002687 // Note: the following instructions encode offsets derived from class linking.
2688 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2689 // meaning if the class linking and resolution were successful.
2690 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002691 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002692 break;
2693 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002694 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002695 break;
2696 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002697 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002698 break;
2699 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002700 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002701 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002702 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002703 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002704 break;
2705 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002706 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002707 break;
2708 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002709 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002710 break;
2711 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002712 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002713 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002714 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002715 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002716 break;
2717 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002718 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002719 break;
2720 case Instruction::INVOKE_VIRTUAL_QUICK:
2721 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2722 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002723 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002724 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002725 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002726 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002727 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002728 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002729 } else {
2730 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2731 }
2732 just_set_result = true;
2733 }
2734 break;
2735 }
2736
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002738 case Instruction::UNUSED_3E:
2739 case Instruction::UNUSED_3F:
2740 case Instruction::UNUSED_40:
2741 case Instruction::UNUSED_41:
2742 case Instruction::UNUSED_42:
2743 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002744 case Instruction::UNUSED_79:
2745 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002746 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002747 case Instruction::UNUSED_F0:
2748 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002749 case Instruction::UNUSED_F2:
2750 case Instruction::UNUSED_F3:
2751 case Instruction::UNUSED_F4:
2752 case Instruction::UNUSED_F5:
2753 case Instruction::UNUSED_F6:
2754 case Instruction::UNUSED_F7:
2755 case Instruction::UNUSED_F8:
2756 case Instruction::UNUSED_F9:
2757 case Instruction::UNUSED_FA:
2758 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002759 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002760 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002761 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002762 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002764 break;
2765
2766 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002767 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002768 * complain if an instruction is missing (which is desirable).
2769 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002770 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002771
Ian Rogersad0b3a32012-04-16 14:50:24 -07002772 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002773 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002774 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002775 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002776 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002777 /* immediate failure, reject class */
2778 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2779 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002780 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002781 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002782 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002783 }
jeffhaobdb76512011-09-07 11:43:16 -07002784 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 * If we didn't just set the result register, clear it out. This ensures that you can only use
2786 * "move-result" immediately after the result is set. (We could check this statically, but it's
2787 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002788 */
2789 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002790 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002791 }
2792
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002793
jeffhaobdb76512011-09-07 11:43:16 -07002794
2795 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002796 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002797 *
2798 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002799 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002800 * somebody could get a reference field, check it for zero, and if the
2801 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002802 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002803 * that, and will reject the code.
2804 *
2805 * TODO: avoid re-fetching the branch target
2806 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002808 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002810 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002812 return false;
2813 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002814 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002815 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002816 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 }
jeffhaobdb76512011-09-07 11:43:16 -07002818 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002819 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002820 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002821 return false;
2822 }
2823 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002824 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002825 return false;
2826 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 }
jeffhaobdb76512011-09-07 11:43:16 -07002828 }
2829
2830 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002831 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002832 *
2833 * We've already verified that the table is structurally sound, so we
2834 * just need to walk through and tag the targets.
2835 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002836 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002837 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2838 const uint16_t* switch_insns = insns + offset_to_switch;
2839 int switch_count = switch_insns[1];
2840 int offset_to_targets, targ;
2841
2842 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2843 /* 0 = sig, 1 = count, 2/3 = first key */
2844 offset_to_targets = 4;
2845 } else {
2846 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002847 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002848 offset_to_targets = 2 + 2 * switch_count;
2849 }
2850
2851 /* verify each switch target */
2852 for (targ = 0; targ < switch_count; targ++) {
2853 int offset;
2854 uint32_t abs_offset;
2855
2856 /* offsets are 32-bit, and only partly endian-swapped */
2857 offset = switch_insns[offset_to_targets + targ * 2] |
2858 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 abs_offset = work_insn_idx_ + offset;
2860 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002861 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002862 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002864 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002865 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002866 }
jeffhaobdb76512011-09-07 11:43:16 -07002867 }
2868 }
2869
2870 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2872 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002873 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002874 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002875 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002876 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002877
Andreas Gampef91baf12014-07-18 15:41:00 -07002878 // Need the linker to try and resolve the handled class to check if it's Throwable.
2879 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2880
Ian Rogers0571d352011-11-03 19:51:38 -07002881 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002882 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2883 if (handler_type_idx == DexFile::kDexNoIndex16) {
2884 has_catch_all_handler = true;
2885 } else {
2886 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002887 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2888 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002889 if (klass != nullptr) {
2890 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2891 has_catch_all_handler = true;
2892 }
2893 } else {
2894 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 DCHECK(self_->IsExceptionPending());
2896 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002897 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 }
jeffhaobdb76512011-09-07 11:43:16 -07002899 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2901 * "work_regs", because at runtime the exception will be thrown before the instruction
2902 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002903 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002904 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002905 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 }
jeffhaobdb76512011-09-07 11:43:16 -07002907 }
2908
2909 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2911 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002912 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002913 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002914 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 * The state in work_line reflects the post-execution state. If the current instruction is a
2916 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002917 * it will do so before grabbing the lock).
2918 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002919 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002920 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002922 return false;
2923 }
2924 }
2925 }
2926
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002927 /* Handle "continue". Tag the next consecutive instruction.
2928 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2929 * because it changes work_line_ when performing peephole optimization
2930 * and this change should not be used in those cases.
2931 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002932 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002933 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2934 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002935 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2937 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002938 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002939 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2940 // next instruction isn't one.
2941 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2942 return false;
2943 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002944 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002945 // Make workline consistent with fallthrough computed from peephole optimization.
2946 work_line_->CopyFromLine(fallthrough_line.get());
2947 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002948 if (insn_flags_[next_insn_idx].IsReturn()) {
2949 // For returns we only care about the operand to the return, all other registers are dead.
2950 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2951 Instruction::Code opcode = ret_inst->Opcode();
2952 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002953 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002954 } else {
2955 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002956 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002957 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002958 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002959 }
2960 }
2961 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002962 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002963 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002964 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2965 // needed. If the merge changes the state of the registers then the work line will be
2966 // updated.
2967 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002968 return false;
2969 }
2970 } else {
2971 /*
2972 * We're not recording register data for the next instruction, so we don't know what the
2973 * prior state was. We have to assume that something has changed and re-evaluate it.
2974 */
2975 insn_flags_[next_insn_idx].SetChanged();
2976 }
2977 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002978
jeffhaod1f0fde2011-09-08 17:25:33 -07002979 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002980 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 return false;
2983 }
jeffhaobdb76512011-09-07 11:43:16 -07002984 }
2985
2986 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002987 * Update start_guess. Advance to the next instruction of that's
2988 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002989 * neither of those exists we're in a return or throw; leave start_guess
2990 * alone and let the caller sort it out.
2991 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002992 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2994 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002995 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002996 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002998 }
2999
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3001 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003002
3003 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003004} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003005
Ian Rogersd8f69b02014-09-10 21:43:52 +00003006const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003007 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003008 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003009 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003010 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003011 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3012 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003013 if (result.IsConflict()) {
3014 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3015 << "' in " << referrer;
3016 return result;
3017 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003018 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003019 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003020 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003021 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003022 // check at runtime if access is allowed and so pass here. If result is
3023 // primitive, skip the access check.
3024 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3025 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003026 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003028 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003029 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003030}
3031
Ian Rogersd8f69b02014-09-10 21:43:52 +00003032const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003033 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003035 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3037 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003038 CatchHandlerIterator iterator(handlers_ptr);
3039 for (; iterator.HasNext(); iterator.Next()) {
3040 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3041 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003042 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003044 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003045 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003046 if (exception.IsUnresolvedTypes()) {
3047 // We don't know enough about the type. Fail here and let runtime handle it.
3048 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3049 return exception;
3050 } else {
3051 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3052 return reg_types_.Conflict();
3053 }
Jeff Haob878f212014-04-24 16:25:36 -07003054 } else if (common_super == nullptr) {
3055 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003056 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003057 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003058 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003059 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003060 if (FailOrAbort(this,
3061 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3062 "java.lang.Throwable is not assignable-from common_super at ",
3063 work_insn_idx_)) {
3064 break;
3065 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 }
3067 }
3068 }
3069 }
Ian Rogers0571d352011-11-03 19:51:38 -07003070 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 }
3072 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003075 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003076 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003078 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003079}
3080
Brian Carlstromea46f952013-07-30 01:26:50 -07003081mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003082 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003083 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003084 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003085 if (klass_type.IsConflict()) {
3086 std::string append(" in attempt to access method ");
3087 append += dex_file_->GetMethodName(method_id);
3088 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003089 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003090 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003091 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003093 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003094 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003095 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003096 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003098 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003099 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003100
3101 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003103 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003104 res_method = klass->FindInterfaceMethod(name, signature);
3105 } else {
3106 res_method = klass->FindVirtualMethod(name, signature);
3107 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003108 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003109 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003111 // If a virtual or interface method wasn't found with the expected type, look in
3112 // the direct methods. This can happen when the wrong invoke type is used or when
3113 // a class has changed, and will be flagged as an error in later checks.
3114 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3115 res_method = klass->FindDirectMethod(name, signature);
3116 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003117 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003118 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3119 << PrettyDescriptor(klass) << "." << name
3120 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003121 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003122 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 }
3124 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3126 // enforce them here.
3127 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3129 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003132 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003133 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3135 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003136 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003137 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003138 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003139 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003140 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003141 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003142 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003143 }
jeffhaode0d9c92012-02-27 13:58:13 -08003144 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3145 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3147 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003148 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003149 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003150 // Check that interface methods match interface classes.
3151 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3152 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3153 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003155 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3156 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3157 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003158 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003159 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 // See if the method type implied by the invoke instruction matches the access flags for the
3161 // target method.
3162 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3163 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3164 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3165 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003166 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3167 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003168 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003170 return res_method;
3171}
3172
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003173template <class T>
3174mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3175 MethodType method_type,
3176 bool is_range,
3177 mirror::ArtMethod* res_method) {
3178 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3179 // match the call to the signature. Also, we might be calling through an abstract method
3180 // definition (which doesn't have register count values).
3181 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3182 /* caught by static verifier */
3183 DCHECK(is_range || expected_args <= 5);
3184 if (expected_args > code_item_->outs_size_) {
3185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3186 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3187 return nullptr;
3188 }
3189
3190 uint32_t arg[5];
3191 if (!is_range) {
3192 inst->GetVarArgs(arg);
3193 }
3194 uint32_t sig_registers = 0;
3195
3196 /*
3197 * Check the "this" argument, which must be an instance of the class that declared the method.
3198 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3199 * rigorous check here (which is okay since we have to do it at runtime).
3200 */
3201 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003202 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003203 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3204 CHECK(have_pending_hard_failure_);
3205 return nullptr;
3206 }
3207 if (actual_arg_type.IsUninitializedReference()) {
3208 if (res_method) {
3209 if (!res_method->IsConstructor()) {
3210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3211 return nullptr;
3212 }
3213 } else {
3214 // Check whether the name of the called method is "<init>"
3215 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003216 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3218 return nullptr;
3219 }
3220 }
3221 }
3222 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003223 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003224 if (res_method != nullptr) {
3225 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003226 std::string temp;
3227 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003228 klass->CannotBeAssignedFromOtherTypes());
3229 } else {
3230 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3231 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003232 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003233 dex_file_->StringByTypeIdx(class_idx),
3234 false);
3235 }
3236 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3237 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3238 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3239 << "' not instance of '" << *res_method_class << "'";
3240 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3241 // the compiler.
3242 if (have_pending_hard_failure_) {
3243 return nullptr;
3244 }
3245 }
3246 }
3247 sig_registers = 1;
3248 }
3249
3250 for ( ; it->HasNext(); it->Next()) {
3251 if (sig_registers >= expected_args) {
3252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3253 " arguments, found " << sig_registers << " or more.";
3254 return nullptr;
3255 }
3256
3257 const char* param_descriptor = it->GetDescriptor();
3258
3259 if (param_descriptor == nullptr) {
3260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3261 "component";
3262 return nullptr;
3263 }
3264
Ian Rogersd8f69b02014-09-10 21:43:52 +00003265 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003266 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3267 arg[sig_registers];
3268 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003269 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003270 if (!src_type.IsIntegralTypes()) {
3271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3272 << " but expected " << reg_type;
3273 return res_method;
3274 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003275 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003276 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3277 // compiler.
3278 if (have_pending_hard_failure_) {
3279 return res_method;
3280 }
3281 }
3282 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3283 }
3284 if (expected_args != sig_registers) {
3285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3286 " arguments, found " << sig_registers;
3287 return nullptr;
3288 }
3289 return res_method;
3290}
3291
3292void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3293 MethodType method_type,
3294 bool is_range) {
3295 // As the method may not have been resolved, make this static check against what we expect.
3296 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3297 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3298 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3299 DexFileParameterIterator it(*dex_file_,
3300 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3301 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3302 nullptr);
3303}
3304
3305class MethodParamListDescriptorIterator {
3306 public:
3307 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3308 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3309 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3310 }
3311
3312 bool HasNext() {
3313 return pos_ < params_size_;
3314 }
3315
3316 void Next() {
3317 ++pos_;
3318 }
3319
3320 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3321 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3322 }
3323
3324 private:
3325 mirror::ArtMethod* res_method_;
3326 size_t pos_;
3327 const DexFile::TypeList* params_;
3328 const size_t params_size_;
3329};
3330
Brian Carlstromea46f952013-07-30 01:26:50 -07003331mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003332 MethodType method_type,
3333 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003334 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003335 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3336 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003337 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003338
Brian Carlstromea46f952013-07-30 01:26:50 -07003339 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003340 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003341 // Check what we can statically.
3342 if (!have_pending_hard_failure_) {
3343 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3344 }
3345 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003346 }
3347
Ian Rogersd81871c2011-10-03 13:57:23 -07003348 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3349 // has a vtable entry for the target method.
3350 if (is_super) {
3351 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003352 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003353 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003354 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003355 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003356 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003357 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003358 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003359 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003360 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003361 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003362 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003363 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003364 << "." << res_method->GetName()
3365 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003366 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003367 }
3368 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003369
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003370 // Process the target method's signature. This signature may or may not
3371 MethodParamListDescriptorIterator it(res_method);
3372 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3373 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003374}
3375
Brian Carlstromea46f952013-07-30 01:26:50 -07003376mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003377 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003378 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3379 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003380 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003381 if (!actual_arg_type.HasClass()) {
3382 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003383 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003384 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003385 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003386 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003387 if (klass->IsInterface()) {
3388 // Derive Object.class from Class.class.getSuperclass().
3389 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003390 if (FailOrAbort(this, object_klass->IsObjectClass(),
3391 "Failed to find Object class in quickened invoke receiver",
3392 work_insn_idx_)) {
3393 return nullptr;
3394 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003395 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003396 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003397 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003398 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003399 if (FailOrAbort(this, dispatch_class->HasVTable(),
3400 "Receiver class has no vtable for quickened invoke at ",
3401 work_insn_idx_)) {
3402 return nullptr;
3403 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003404 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe7c038102014-10-27 20:08:46 -07003405 if (FailOrAbort(this, static_cast<int32_t>(vtable_index) < dispatch_class->GetVTableLength(),
3406 "Receiver class has not enough vtable slots for quickened invoke at ",
3407 work_insn_idx_)) {
3408 return nullptr;
3409 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003410 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Andreas Gampe7c038102014-10-27 20:08:46 -07003411 if (FailOrAbort(this, !Thread::Current()->IsExceptionPending(),
3412 "Unexpected exception pending for quickened invoke at ",
3413 work_insn_idx_)) {
3414 return nullptr;
3415 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003416 return res_method;
3417}
3418
Brian Carlstromea46f952013-07-30 01:26:50 -07003419mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003420 bool is_range) {
3421 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003422 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003423 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003424 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003427 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003428 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3429 work_insn_idx_)) {
3430 return nullptr;
3431 }
3432 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3433 work_insn_idx_)) {
3434 return nullptr;
3435 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003436
3437 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3438 // match the call to the signature. Also, we might be calling through an abstract method
3439 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003440 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003441 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003442 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003443 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003444 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3445 /* caught by static verifier */
3446 DCHECK(is_range || expected_args <= 5);
3447 if (expected_args > code_item_->outs_size_) {
3448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3449 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003450 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003451 }
3452
3453 /*
3454 * Check the "this" argument, which must be an instance of the class that declared the method.
3455 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3456 * rigorous check here (which is okay since we have to do it at runtime).
3457 */
3458 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003460 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003461 }
3462 if (!actual_arg_type.IsZero()) {
3463 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003464 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003465 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003466 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003467 klass->CannotBeAssignedFromOtherTypes());
3468 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003469 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3470 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003471 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003472 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003473 }
3474 }
3475 /*
3476 * Process the target method's signature. This signature may or may not
3477 * have been verified, so we can't assume it's properly formed.
3478 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003479 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003480 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003481 uint32_t arg[5];
3482 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003483 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003484 }
3485 size_t actual_args = 1;
3486 for (size_t param_index = 0; param_index < params_size; param_index++) {
3487 if (actual_args >= expected_args) {
3488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003489 << "'. Expected " << expected_args
3490 << " arguments, processing argument " << actual_args
3491 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003492 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003493 }
3494 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003495 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003496 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003498 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003499 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003500 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003501 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003502 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003503 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003504 return res_method;
3505 }
3506 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3507 }
3508 if (actual_args != expected_args) {
3509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3510 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003511 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003512 } else {
3513 return res_method;
3514 }
3515}
3516
Ian Rogers62342ec2013-06-11 10:26:37 -07003517void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003518 uint32_t type_idx;
3519 if (!is_filled) {
3520 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3521 type_idx = inst->VRegC_22c();
3522 } else if (!is_range) {
3523 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3524 type_idx = inst->VRegB_35c();
3525 } else {
3526 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3527 type_idx = inst->VRegB_3rc();
3528 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003529 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003530 if (res_type.IsConflict()) { // bad class
3531 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003532 } else {
3533 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3534 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003536 } else if (!is_filled) {
3537 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003538 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003539 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003540 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003541 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003542 } else {
3543 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3544 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003545 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003546 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3547 uint32_t arg[5];
3548 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003549 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003550 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003551 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003552 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003553 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3554 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003555 return;
3556 }
3557 }
3558 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003559 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003560 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003561 }
3562 }
3563}
3564
Sebastien Hertz5243e912013-05-21 10:55:07 +02003565void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003566 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003567 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003568 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003570 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003571 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003572 if (array_type.IsZero()) {
3573 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3574 // instruction type. TODO: have a proper notion of bottom here.
3575 if (!is_primitive || insn_type.IsCategory1Types()) {
3576 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003577 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003579 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003580 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3581 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003582 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003583 }
jeffhaofc3144e2012-02-01 17:21:15 -08003584 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003586 } else {
3587 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003588 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003589 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003591 << " source for aget-object";
3592 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003594 << " source for category 1 aget";
3595 } else if (is_primitive && !insn_type.Equals(component_type) &&
3596 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003597 (insn_type.IsLong() && component_type.IsDouble()))) {
3598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3599 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003600 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003601 // Use knowledge of the field type which is stronger than the type inferred from the
3602 // instruction, which can't differentiate object types and ints from floats, longs from
3603 // doubles.
3604 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003605 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003606 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003607 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003608 component_type.HighHalf(&reg_types_));
3609 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 }
3611 }
3612 }
3613}
3614
Ian Rogersd8f69b02014-09-10 21:43:52 +00003615void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003616 const uint32_t vregA) {
3617 // Primitive assignability rules are weaker than regular assignability rules.
3618 bool instruction_compatible;
3619 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003620 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003621 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003622 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003623 value_compatible = value_type.IsIntegralTypes();
3624 } else if (target_type.IsFloat()) {
3625 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3626 value_compatible = value_type.IsFloatTypes();
3627 } else if (target_type.IsLong()) {
3628 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003629 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3630 // as target_type depends on the resolved type of the field.
3631 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003632 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003633 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3634 } else {
3635 value_compatible = false;
3636 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003637 } else if (target_type.IsDouble()) {
3638 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003639 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3640 // as target_type depends on the resolved type of the field.
3641 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003643 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3644 } else {
3645 value_compatible = false;
3646 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003647 } else {
3648 instruction_compatible = false; // reference with primitive store
3649 value_compatible = false; // unused
3650 }
3651 if (!instruction_compatible) {
3652 // This is a global failure rather than a class change failure as the instructions and
3653 // the descriptors for the type should have been consistent within the same file at
3654 // compile time.
3655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3656 << "' but expected type '" << target_type << "'";
3657 return;
3658 }
3659 if (!value_compatible) {
3660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3661 << " of type " << value_type << " but expected " << target_type << " for put";
3662 return;
3663 }
3664}
3665
Sebastien Hertz5243e912013-05-21 10:55:07 +02003666void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003667 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003668 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003669 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003671 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003672 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003673 if (array_type.IsZero()) {
3674 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3675 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003676 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003678 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003679 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003680 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003681 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003682 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003683 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003684 if (!component_type.IsReferenceTypes()) {
3685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3686 << " source for aput-object";
3687 } else {
3688 // The instruction agrees with the type of array, confirm the value to be stored does too
3689 // Note: we use the instruction type (rather than the component type) for aput-object as
3690 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003691 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003692 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003693 }
3694 }
3695 }
3696}
3697
Brian Carlstromea46f952013-07-30 01:26:50 -07003698mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003699 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3700 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003701 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003702 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003703 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3704 field_idx, dex_file_->GetFieldName(field_id),
3705 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003706 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003707 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003708 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003709 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003710 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003711 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003712 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3713 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003715 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003716 << dex_file_->GetFieldName(field_id) << ") in "
3717 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003718 DCHECK(self_->IsExceptionPending());
3719 self_->ClearException();
3720 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003721 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3722 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003723 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003724 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003726 } else if (!field->IsStatic()) {
3727 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003728 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003729 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003730 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003731}
3732
Ian Rogersd8f69b02014-09-10 21:43:52 +00003733mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003734 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3735 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003736 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003737 if (klass_type.IsConflict()) {
3738 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3739 field_idx, dex_file_->GetFieldName(field_id),
3740 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003741 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003742 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003743 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003744 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003745 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003746 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003747 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3748 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003749 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003750 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003751 << dex_file_->GetFieldName(field_id) << ") in "
3752 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003753 DCHECK(self_->IsExceptionPending());
3754 self_->ClearException();
3755 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003756 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3757 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003759 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003760 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 } else if (field->IsStatic()) {
3762 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3763 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003764 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003765 } else if (obj_type.IsZero()) {
3766 // Cannot infer and check type, however, access will cause null pointer exception
3767 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003768 } else if (!obj_type.IsReferenceTypes()) {
3769 // Trying to read a field from something that isn't a reference
3770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3771 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003772 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003773 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003774 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003775 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003776 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003777 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003778 if (obj_type.IsUninitializedTypes() &&
3779 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3780 !field_klass.Equals(GetDeclaringClass()))) {
3781 // Field accesses through uninitialized references are only allowable for constructors where
3782 // the field is declared in this class
3783 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003784 << " of a not fully initialized object within the context"
3785 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003786 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003787 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3788 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3789 // of C1. For resolution to occur the declared class of the field must be compatible with
3790 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3791 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3792 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003793 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003794 } else {
3795 return field;
3796 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 }
3798}
3799
Andreas Gampe896df402014-10-20 22:25:29 -07003800template <MethodVerifier::FieldAccessType kAccType>
3801void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3802 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003803 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003804 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003805 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003806 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003807 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003808 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003809 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003810 if (UNLIKELY(have_pending_hard_failure_)) {
3811 return;
3812 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003813 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003814 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003816 if (kAccType == FieldAccessType::kAccPut) {
3817 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3818 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3819 << " from other class " << GetDeclaringClass();
3820 return;
3821 }
3822 }
3823
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003824 mirror::Class* field_type_class;
3825 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003827 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08003828 field_type_class = h_field->GetType(can_load_classes_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003829 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003830 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003831 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003832 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003833 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003834 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3835 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003836 }
Ian Rogers0d604842012-04-16 14:50:24 -07003837 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003838 if (field_type == nullptr) {
3839 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3840 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003841 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003842 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003843 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003844 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003845 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3846 "Unexpected third access type");
3847 if (kAccType == FieldAccessType::kAccPut) {
3848 // sput or iput.
3849 if (is_primitive) {
3850 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003851 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003852 if (!insn_type.IsAssignableFrom(*field_type)) {
3853 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3854 << " to be compatible with type '" << insn_type
3855 << "' but found type '" << *field_type
3856 << "' in put-object";
3857 return;
3858 }
3859 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003860 }
Andreas Gampe896df402014-10-20 22:25:29 -07003861 } else if (kAccType == FieldAccessType::kAccGet) {
3862 // sget or iget.
3863 if (is_primitive) {
3864 if (field_type->Equals(insn_type) ||
3865 (field_type->IsFloat() && insn_type.IsInteger()) ||
3866 (field_type->IsDouble() && insn_type.IsLong())) {
3867 // expected that read is of the correct primitive type or that int reads are reading
3868 // floats or long reads are reading doubles
3869 } else {
3870 // This is a global failure rather than a class change failure as the instructions and
3871 // the descriptors for the type should have been consistent within the same file at
3872 // compile time
3873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3874 << " to be of type '" << insn_type
3875 << "' but found type '" << *field_type << "' in get";
3876 return;
3877 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003878 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003879 if (!insn_type.IsAssignableFrom(*field_type)) {
3880 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3881 << " to be compatible with type '" << insn_type
3882 << "' but found type '" << *field_type
3883 << "' in get-object";
3884 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3885 return;
3886 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003887 }
Andreas Gampe896df402014-10-20 22:25:29 -07003888 if (!field_type->IsLowHalf()) {
3889 work_line_->SetRegisterType(this, vregA, *field_type);
3890 } else {
3891 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
3892 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003893 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003894 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07003895 }
3896}
3897
Brian Carlstromea46f952013-07-30 01:26:50 -07003898mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003899 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003900 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3901 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3902 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3903 inst->Opcode() == Instruction::IPUT_QUICK ||
3904 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
Hiroshi Yamauchi5f09be92014-09-26 10:43:59 -07003905 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3906 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3907 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3908 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3909 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003910 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003911 if (!object_type.HasClass()) {
3912 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3913 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003914 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003915 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003916 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3917 field_offset);
3918 if (f == nullptr) {
3919 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3920 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3921 }
3922 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003923}
3924
Andreas Gampe896df402014-10-20 22:25:29 -07003925template <MethodVerifier::FieldAccessType kAccType>
3926void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
3927 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003928 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003929
Brian Carlstromea46f952013-07-30 01:26:50 -07003930 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003931 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3933 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003934 }
Andreas Gampe896df402014-10-20 22:25:29 -07003935
3936 // For an IPUT_QUICK, we now test for final flag of the field.
3937 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003938 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3939 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003940 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003941 return;
3942 }
3943 }
Andreas Gampe896df402014-10-20 22:25:29 -07003944
3945 // Get the field type.
3946 const RegType* field_type;
3947 {
3948 mirror::Class* field_type_class;
3949 {
3950 StackHandleScope<1> hs(Thread::Current());
3951 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
Ian Rogers08f1f502014-12-02 15:04:37 -08003952 field_type_class = h_field->GetType(can_load_classes_);
Andreas Gampe896df402014-10-20 22:25:29 -07003953 }
3954
3955 if (field_type_class != nullptr) {
3956 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
3957 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003958 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003959 Thread* self = Thread::Current();
3960 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3961 self->ClearException();
3962 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3963 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003964 }
Andreas Gampe896df402014-10-20 22:25:29 -07003965 if (field_type == nullptr) {
3966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003967 return;
3968 }
Andreas Gampe896df402014-10-20 22:25:29 -07003969 }
3970
3971 const uint32_t vregA = inst->VRegA_22c();
3972 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3973 "Unexpected third access type");
3974 if (kAccType == FieldAccessType::kAccPut) {
3975 if (is_primitive) {
3976 // Primitive field assignability rules are weaker than regular assignability rules
3977 bool instruction_compatible;
3978 bool value_compatible;
3979 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
3980 if (field_type->IsIntegralTypes()) {
3981 instruction_compatible = insn_type.IsIntegralTypes();
3982 value_compatible = value_type.IsIntegralTypes();
3983 } else if (field_type->IsFloat()) {
3984 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3985 value_compatible = value_type.IsFloatTypes();
3986 } else if (field_type->IsLong()) {
3987 instruction_compatible = insn_type.IsLong();
3988 value_compatible = value_type.IsLongTypes();
3989 } else if (field_type->IsDouble()) {
3990 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3991 value_compatible = value_type.IsDoubleTypes();
3992 } else {
3993 instruction_compatible = false; // reference field with primitive store
3994 value_compatible = false; // unused
3995 }
3996 if (!instruction_compatible) {
3997 // This is a global failure rather than a class change failure as the instructions and
3998 // the descriptors for the type should have been consistent within the same file at
3999 // compile time
4000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4001 << " to be of type '" << insn_type
4002 << "' but found type '" << *field_type
4003 << "' in put";
4004 return;
4005 }
4006 if (!value_compatible) {
4007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4008 << " of type " << value_type
4009 << " but expected " << *field_type
4010 << " for store to " << PrettyField(field) << " in put";
4011 return;
4012 }
4013 } else {
4014 if (!insn_type.IsAssignableFrom(*field_type)) {
4015 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4016 << " to be compatible with type '" << insn_type
4017 << "' but found type '" << *field_type
4018 << "' in put-object";
4019 return;
4020 }
4021 work_line_->VerifyRegisterType(this, vregA, *field_type);
4022 }
4023 } else if (kAccType == FieldAccessType::kAccGet) {
4024 if (is_primitive) {
4025 if (field_type->Equals(insn_type) ||
4026 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4027 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4028 // expected that read is of the correct primitive type or that int reads are reading
4029 // floats or long reads are reading doubles
4030 } else {
4031 // This is a global failure rather than a class change failure as the instructions and
4032 // the descriptors for the type should have been consistent within the same file at
4033 // compile time
4034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4035 << " to be of type '" << insn_type
4036 << "' but found type '" << *field_type << "' in Get";
4037 return;
4038 }
4039 } else {
4040 if (!insn_type.IsAssignableFrom(*field_type)) {
4041 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4042 << " to be compatible with type '" << insn_type
4043 << "' but found type '" << *field_type
4044 << "' in get-object";
4045 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4046 return;
4047 }
4048 }
4049 if (!field_type->IsLowHalf()) {
4050 work_line_->SetRegisterType(this, vregA, *field_type);
4051 } else {
4052 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004053 }
4054 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004055 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004056 }
4057}
4058
Ian Rogers776ac1f2012-04-13 23:36:36 -07004059bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004060 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004062 return false;
4063 }
4064 return true;
4065}
4066
Stephen Kyle9bc61992014-09-22 13:53:15 +01004067bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4068 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4069 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4071 return false;
4072 }
4073 return true;
4074}
4075
4076bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4077 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4078}
4079
Ian Rogersebbdd872014-07-07 23:53:08 -07004080bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4081 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004082 bool changed = true;
4083 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4084 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004085 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 * We haven't processed this instruction before, and we haven't touched the registers here, so
4087 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4088 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004089 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004090 if (!insn_flags_[next_insn].IsReturn()) {
4091 target_line->CopyFromLine(merge_line);
4092 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004093 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004094 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004095 return false;
4096 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004097 // For returns we only care about the operand to the return, all other registers are dead.
4098 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4099 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4100 Instruction::Code opcode = ret_inst->Opcode();
4101 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004102 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004103 } else {
4104 target_line->CopyFromLine(merge_line);
4105 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004106 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004107 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004108 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004109 }
4110 }
4111 }
jeffhaobdb76512011-09-07 11:43:16 -07004112 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004113 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004114 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004115 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004116 if (gDebugVerify) {
4117 copy->CopyFromLine(target_line);
4118 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004119 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004120 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004121 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004122 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004123 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004124 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004125 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004126 << copy->Dump(this) << " MERGE\n"
4127 << merge_line->Dump(this) << " ==\n"
4128 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004129 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004130 if (update_merge_line && changed) {
4131 merge_line->CopyFromLine(target_line);
4132 }
jeffhaobdb76512011-09-07 11:43:16 -07004133 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004134 if (changed) {
4135 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004136 }
4137 return true;
4138}
4139
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004140InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004141 return &insn_flags_[work_insn_idx_];
4142}
4143
Ian Rogersd8f69b02014-09-10 21:43:52 +00004144const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004145 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004146 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004147 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004148 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004149 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4150 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004151 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004152 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4154 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004155 }
4156 }
4157 if (return_type_ == nullptr) {
4158 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4159 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4160 uint16_t return_type_idx = proto_id.return_type_idx_;
4161 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004162 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004163 }
4164 }
4165 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004166}
4167
Ian Rogersd8f69b02014-09-10 21:43:52 +00004168const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004169 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004170 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004171 const char* descriptor
4172 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004173 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004174 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004175 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4176 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004177 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004178 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004179 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004180 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004181 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004182}
4183
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004184std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4185 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004186 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004187 std::vector<int32_t> result;
4188 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004189 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004190 if (type.IsConstant()) {
4191 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004192 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4193 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004194 } else if (type.IsConstantLo()) {
4195 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004196 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4197 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004198 } else if (type.IsConstantHi()) {
4199 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004200 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4201 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004202 } else if (type.IsIntegralTypes()) {
4203 result.push_back(kIntVReg);
4204 result.push_back(0);
4205 } else if (type.IsFloat()) {
4206 result.push_back(kFloatVReg);
4207 result.push_back(0);
4208 } else if (type.IsLong()) {
4209 result.push_back(kLongLoVReg);
4210 result.push_back(0);
4211 result.push_back(kLongHiVReg);
4212 result.push_back(0);
4213 ++i;
4214 } else if (type.IsDouble()) {
4215 result.push_back(kDoubleLoVReg);
4216 result.push_back(0);
4217 result.push_back(kDoubleHiVReg);
4218 result.push_back(0);
4219 ++i;
4220 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4221 result.push_back(kUndefined);
4222 result.push_back(0);
4223 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004224 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004225 result.push_back(kReferenceVReg);
4226 result.push_back(0);
4227 }
4228 }
4229 return result;
4230}
4231
Ian Rogersd8f69b02014-09-10 21:43:52 +00004232const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004233 if (precise) {
4234 // Precise constant type.
4235 return reg_types_.FromCat1Const(value, true);
4236 } else {
4237 // Imprecise constant type.
4238 if (value < -32768) {
4239 return reg_types_.IntConstant();
4240 } else if (value < -128) {
4241 return reg_types_.ShortConstant();
4242 } else if (value < 0) {
4243 return reg_types_.ByteConstant();
4244 } else if (value == 0) {
4245 return reg_types_.Zero();
4246 } else if (value == 1) {
4247 return reg_types_.One();
4248 } else if (value < 128) {
4249 return reg_types_.PosByteConstant();
4250 } else if (value < 32768) {
4251 return reg_types_.PosShortConstant();
4252 } else if (value < 65536) {
4253 return reg_types_.CharConstant();
4254 } else {
4255 return reg_types_.IntConstant();
4256 }
4257 }
4258}
4259
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004260void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004261 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004262}
4263
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004264void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004265 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004266}
jeffhaod1224c72012-02-29 13:43:08 -08004267
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004268void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4269 RegTypeCache::VisitStaticRoots(callback, arg);
4270}
4271
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004272void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4273 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004274}
4275
Ian Rogersd81871c2011-10-03 13:57:23 -07004276} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004277} // namespace art