blob: 59ead892e1ec46e92a13e1b5588b6d6954aeb538 [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
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.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"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.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 Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.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"
Elliott Hughese222ee02012-12-13 14:41:43 -080043#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070046namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
Ian Rogers2c8a8572011-10-24 17:11:36 -070048static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070049// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070050
Ian Rogers7b3ddd22013-02-21 15:19:52 -080051void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070052 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070053 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070054 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070055 register_lines_.reset(new RegisterLine*[insns_size]());
56 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070057 for (uint32_t i = 0; i < insns_size; i++) {
58 bool interesting = false;
59 switch (mode) {
60 case kTrackRegsAll:
61 interesting = flags[i].IsOpcode();
62 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070063 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070064 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070065 break;
66 case kTrackRegsBranches:
67 interesting = flags[i].IsBranchTarget();
68 break;
69 default:
70 break;
71 }
72 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070073 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
74 }
75 }
76}
77
78PcToRegisterLineTable::~PcToRegisterLineTable() {
79 for (size_t i = 0; i < size_; i++) {
80 delete register_lines_[i];
81 if (kIsDebugBuild) {
82 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070083 }
84 }
85}
86
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070088 bool allow_soft_failures,
89 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070090 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070091 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070092 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080093 bool early_failure = false;
94 std::string failure_message;
Ian Rogersad0b3a32012-04-16 14:50:24 -070095 ClassHelper kh(klass);
96 const DexFile& dex_file = kh.GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -070097 const DexFile::ClassDef* class_def = kh.GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080098 mirror::Class* super = klass->GetSuperClass();
99 if (super == NULL && strcmp("Ljava/lang/Object;", kh.GetDescriptor()) != 0) {
100 early_failure = true;
101 failure_message = " that has no super class";
102 } else if (super != NULL && super->IsFinal()) {
103 early_failure = true;
104 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
105 } else if (class_def == NULL) {
106 early_failure = true;
107 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
108 }
109 if (early_failure) {
110 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
111 if (Runtime::Current()->IsCompiler()) {
112 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
113 AddRejectedClass(ref);
114 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700115 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700116 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700117 Thread* self = Thread::Current();
118 SirtRef<mirror::DexCache> dex_cache(self, kh.GetDexCache());
119 SirtRef<mirror::ClassLoader> class_loader(self, klass->GetClassLoader());
120 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700121}
122
Ian Rogers365c1022012-06-22 15:05:28 -0700123MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700124 SirtRef<mirror::DexCache>& dex_cache,
125 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 const DexFile::ClassDef* class_def,
127 bool allow_soft_failures,
128 std::string* error) {
129 DCHECK(class_def != nullptr);
130 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700131 if (class_data == NULL) {
132 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700133 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700134 }
jeffhaof56197c2012-03-05 18:01:54 -0800135 ClassDataItemIterator it(*dex_file, class_data);
136 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
137 it.Next();
138 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700139 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700140 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700142 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800143 while (it.HasNextDirectMethod()) {
144 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700145 if (method_idx == previous_direct_method_idx) {
146 // smali can create dex files with two encoded_methods sharing the same method_idx
147 // http://code.google.com/p/smali/issues/detail?id=119
148 it.Next();
149 continue;
150 }
151 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700152 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700153 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700155 if (method == NULL) {
156 DCHECK(Thread::Current()->IsExceptionPending());
157 // We couldn't resolve the method, but continue regardless.
158 Thread::Current()->ClearException();
159 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700160 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
161 dex_file,
162 dex_cache,
163 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700164 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700165 it.GetMethodCodeItem(),
166 method,
167 it.GetMemberAccessFlags(),
168 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700169 if (result != kNoFailure) {
170 if (result == kHardFailure) {
171 hard_fail = true;
172 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700173 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700174 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 *error = "Verifier rejected class ";
176 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
177 *error += " due to bad method ";
178 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700179 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800181 }
182 it.Next();
183 }
jeffhao9b0b1882012-10-01 16:51:22 -0700184 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800185 while (it.HasNextVirtualMethod()) {
186 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700187 if (method_idx == previous_virtual_method_idx) {
188 // smali can create dex files with two encoded_methods sharing the same method_idx
189 // http://code.google.com/p/smali/issues/detail?id=119
190 it.Next();
191 continue;
192 }
193 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700194 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700195 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700197 if (method == NULL) {
198 DCHECK(Thread::Current()->IsExceptionPending());
199 // We couldn't resolve the method, but continue regardless.
200 Thread::Current()->ClearException();
201 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700202 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
203 dex_file,
204 dex_cache,
205 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700206 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700207 it.GetMethodCodeItem(),
208 method,
209 it.GetMemberAccessFlags(),
210 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700211 if (result != kNoFailure) {
212 if (result == kHardFailure) {
213 hard_fail = true;
214 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700215 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700216 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700217 *error = "Verifier rejected class ";
218 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
219 *error += " due to bad method ";
220 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800223 }
224 it.Next();
225 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 if (error_count == 0) {
227 return kNoFailure;
228 } else {
229 return hard_fail ? kHardFailure : kSoftFailure;
230 }
jeffhaof56197c2012-03-05 18:01:54 -0800231}
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
234 const DexFile* dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700235 SirtRef<mirror::DexCache>& dex_cache,
236 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700237 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700239 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700240 uint32_t method_access_flags,
241 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700242 MethodVerifier::FailureKind result = kNoFailure;
243 uint64_t start_ns = NanoTime();
244
Mathieu Chartier590fee92013-09-13 13:46:47 -0700245 MethodVerifier verifier_(dex_file, &dex_cache, &class_loader, class_def, code_item,
246 method_idx, method, method_access_flags, true, allow_soft_failures);
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700247 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700248 // Verification completed, however failures may be pending that didn't cause the verification
249 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700250 CHECK(!verifier_.have_pending_hard_failure_);
251 if (verifier_.failures_.size() != 0) {
252 if (VLOG_IS_ON(verifier)) {
253 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
254 << PrettyMethod(method_idx, *dex_file) << "\n");
255 }
Ian Rogersc8982582012-09-07 16:53:25 -0700256 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800257 }
258 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700260 CHECK_NE(verifier_.failures_.size(), 0U);
261 CHECK(verifier_.have_pending_hard_failure_);
262 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700263 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800264 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700265 std::cout << "\n" << verifier_.info_messages_.str();
266 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800267 }
Ian Rogersc8982582012-09-07 16:53:25 -0700268 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800269 }
Ian Rogersc8982582012-09-07 16:53:25 -0700270 uint64_t duration_ns = NanoTime() - start_ns;
271 if (duration_ns > MsToNs(100)) {
272 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
273 << " took " << PrettyDuration(duration_ns);
274 }
275 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800276}
277
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800278void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700279 const DexFile* dex_file,
280 SirtRef<mirror::DexCache>& dex_cache,
281 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700282 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700284 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800285 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700286 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700287 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700288 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 verifier.DumpFailures(os);
290 os << verifier.info_messages_.str();
291 verifier.Dump(os);
292}
293
Mathieu Chartier590fee92013-09-13 13:46:47 -0700294MethodVerifier::MethodVerifier(const DexFile* dex_file, SirtRef<mirror::DexCache>* dex_cache,
295 SirtRef<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700296 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700297 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
298 mirror::ArtMethod* method, uint32_t method_access_flags,
299 bool can_load_classes, bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800300 : reg_types_(can_load_classes),
301 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800302 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700303 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700305 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800306 dex_file_(dex_file),
307 dex_cache_(dex_cache),
308 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800310 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700311 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700312 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700313 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700314 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700315 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800316 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800317 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700318 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200319 allow_soft_failures_(allow_soft_failures),
320 has_check_casts_(false),
321 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800322 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700323 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800324}
325
Mathieu Chartier590fee92013-09-13 13:46:47 -0700326MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800327 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700328 STLDeleteElements(&failure_messages_);
329}
330
Brian Carlstromea46f952013-07-30 01:26:50 -0700331void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800332 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700333 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700334 Thread* self = Thread::Current();
335 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
336 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
337 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
338 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
339 true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700340 verifier.interesting_dex_pc_ = dex_pc;
341 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
342 verifier.FindLocksAtDexPc();
343}
344
345void MethodVerifier::FindLocksAtDexPc() {
346 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700347 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700348
349 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
350 // verification. In practice, the phase we want relies on data structures set up by all the
351 // earlier passes, so we just run the full method verification and bail out early when we've
352 // got what we wanted.
353 Verify();
354}
355
Brian Carlstromea46f952013-07-30 01:26:50 -0700356mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200357 uint32_t dex_pc) {
358 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700359 Thread* self = Thread::Current();
360 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
361 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
362 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
363 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
364 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200365 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200366}
367
Brian Carlstromea46f952013-07-30 01:26:50 -0700368mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700369 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200370
371 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
372 // verification. In practice, the phase we want relies on data structures set up by all the
373 // earlier passes, so we just run the full method verification and bail out early when we've
374 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200375 bool success = Verify();
376 if (!success) {
377 return NULL;
378 }
379 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
380 if (register_line == NULL) {
381 return NULL;
382 }
383 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
384 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200385}
386
Brian Carlstromea46f952013-07-30 01:26:50 -0700387mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700388 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200389 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390 Thread* self = Thread::Current();
391 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
392 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
393 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
394 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
395 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200396 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200397}
398
Brian Carlstromea46f952013-07-30 01:26:50 -0700399mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700400 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200401
402 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
403 // verification. In practice, the phase we want relies on data structures set up by all the
404 // earlier passes, so we just run the full method verification and bail out early when we've
405 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200406 bool success = Verify();
407 if (!success) {
408 return NULL;
409 }
410 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
411 if (register_line == NULL) {
412 return NULL;
413 }
414 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
415 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
416 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200417}
418
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700420 // If there aren't any instructions, make sure that's expected, then exit successfully.
421 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700422 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700424 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 } else {
426 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700427 }
jeffhaobdb76512011-09-07 11:43:16 -0700428 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700429 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
430 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
432 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700433 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700434 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800436 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 // Run through the instructions and see if the width checks out.
438 bool result = ComputeWidthsAndCountOps();
439 // Flag instructions guarded by a "try" block and check exception handlers.
440 result = result && ScanTryCatchBlocks();
441 // Perform static instruction verification.
442 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700443 // Perform code-flow analysis and return.
444 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700445}
446
Ian Rogers776ac1f2012-04-13 23:36:36 -0700447std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448 switch (error) {
449 case VERIFY_ERROR_NO_CLASS:
450 case VERIFY_ERROR_NO_FIELD:
451 case VERIFY_ERROR_NO_METHOD:
452 case VERIFY_ERROR_ACCESS_CLASS:
453 case VERIFY_ERROR_ACCESS_FIELD:
454 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700455 case VERIFY_ERROR_INSTANTIATION:
456 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800457 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700458 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
459 // class change and instantiation errors into soft verification errors so that we re-verify
460 // at runtime. We may fail to find or to agree on access because of not yet available class
461 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
462 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
463 // paths" that dynamically perform the verification and cause the behavior to be that akin
464 // to an interpreter.
465 error = VERIFY_ERROR_BAD_CLASS_SOFT;
466 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700467 // If we fail again at runtime, mark that this instruction would throw and force this
468 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700469 have_pending_runtime_throw_failure_ = true;
470 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700472 // Indication that verification should be retried at runtime.
473 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700474 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475 have_pending_hard_failure_ = true;
476 }
477 break;
jeffhaod5347e02012-03-22 17:25:05 -0700478 // Hard verification failures at compile time will still fail at runtime, so the class is
479 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700480 case VERIFY_ERROR_BAD_CLASS_HARD: {
481 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700482 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
jeffhaod1224c72012-02-29 13:43:08 -0800483 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800484 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700485 have_pending_hard_failure_ = true;
486 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800487 }
488 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700489 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800490 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 work_insn_idx_));
492 std::ostringstream* failure_message = new std::ostringstream(location);
493 failure_messages_.push_back(failure_message);
494 return *failure_message;
495}
496
497void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
498 size_t failure_num = failure_messages_.size();
499 DCHECK_NE(failure_num, 0U);
500 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
501 prepend += last_fail_message->str();
502 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
503 delete last_fail_message;
504}
505
506void MethodVerifier::AppendToLastFailMessage(std::string append) {
507 size_t failure_num = failure_messages_.size();
508 DCHECK_NE(failure_num, 0U);
509 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
510 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800511}
512
Ian Rogers776ac1f2012-04-13 23:36:36 -0700513bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700514 const uint16_t* insns = code_item_->insns_;
515 size_t insns_size = code_item_->insns_size_in_code_units_;
516 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700517 size_t new_instance_count = 0;
518 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700520
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700522 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700523 switch (opcode) {
524 case Instruction::APUT_OBJECT:
525 case Instruction::CHECK_CAST:
526 has_check_casts_ = true;
527 break;
528 case Instruction::INVOKE_VIRTUAL:
529 case Instruction::INVOKE_VIRTUAL_RANGE:
530 case Instruction::INVOKE_INTERFACE:
531 case Instruction::INVOKE_INTERFACE_RANGE:
532 has_virtual_or_interface_invokes_ = true;
533 break;
534 case Instruction::MONITOR_ENTER:
535 monitor_enter_count++;
536 break;
537 case Instruction::NEW_INSTANCE:
538 new_instance_count++;
539 break;
540 default:
541 break;
jeffhaobdb76512011-09-07 11:43:16 -0700542 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 size_t inst_size = inst->SizeInCodeUnits();
544 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
545 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700546 inst = inst->Next();
547 }
548
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
551 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700552 return false;
553 }
554
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 new_instance_count_ = new_instance_count;
556 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700557 return true;
558}
559
Ian Rogers776ac1f2012-04-13 23:36:36 -0700560bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700562 if (tries_size == 0) {
563 return true;
564 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700566 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700567
568 for (uint32_t idx = 0; idx < tries_size; idx++) {
569 const DexFile::TryItem* try_item = &tries[idx];
570 uint32_t start = try_item->start_addr_;
571 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700572 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
574 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700575 return false;
576 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700578 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
579 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700580 return false;
581 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 for (uint32_t dex_pc = start; dex_pc < end;
583 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
584 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700585 }
586 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800587 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700588 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700589 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700590 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700591 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700592 CatchHandlerIterator iterator(handlers_ptr);
593 for (; iterator.HasNext(); iterator.Next()) {
594 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700596 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
597 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700598 return false;
599 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700600 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700601 // Ensure exception types are resolved so that they don't need resolution to be delivered,
602 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700603 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
605 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700606 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700607 if (exception_type == NULL) {
608 DCHECK(Thread::Current()->IsExceptionPending());
609 Thread::Current()->ClearException();
610 }
611 }
jeffhaobdb76512011-09-07 11:43:16 -0700612 }
Ian Rogers0571d352011-11-03 19:51:38 -0700613 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700614 }
jeffhaobdb76512011-09-07 11:43:16 -0700615 return true;
616}
617
Ian Rogers776ac1f2012-04-13 23:36:36 -0700618bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700620
Ian Rogers0c7abda2012-09-19 13:33:42 -0700621 /* 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 -0700622 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700623 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700624
625 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700626 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700627 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700628 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 return false;
630 }
631 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700632 // All invoke points are marked as "Throw" points already.
633 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000634 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700635 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000636 } else if (inst->IsReturn()) {
637 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 }
639 dex_pc += inst->SizeInCodeUnits();
640 inst = inst->Next();
641 }
642 return true;
643}
644
Ian Rogers776ac1f2012-04-13 23:36:36 -0700645bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800646 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 bool result = true;
648 switch (inst->GetVerifyTypeArgumentA()) {
649 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 break;
652 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800653 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 break;
655 }
656 switch (inst->GetVerifyTypeArgumentB()) {
657 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800658 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 break;
660 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800661 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 break;
663 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800664 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 break;
666 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800673 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800676 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 }
679 switch (inst->GetVerifyTypeArgumentC()) {
680 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800681 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 break;
683 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800684 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 break;
686 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800687 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 break;
689 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800690 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 break;
692 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800693 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 break;
695 }
696 switch (inst->GetVerifyExtraFlags()) {
697 case Instruction::kVerifyArrayData:
698 result = result && CheckArrayData(code_offset);
699 break;
700 case Instruction::kVerifyBranchTarget:
701 result = result && CheckBranchTarget(code_offset);
702 break;
703 case Instruction::kVerifySwitchTargets:
704 result = result && CheckSwitchTargets(code_offset);
705 break;
706 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800707 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 break;
709 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800710 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 break;
712 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 result = false;
715 break;
716 }
717 return result;
718}
719
Ian Rogers776ac1f2012-04-13 23:36:36 -0700720bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
723 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 return false;
725 }
726 return true;
727}
728
Ian Rogers776ac1f2012-04-13 23:36:36 -0700729bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
732 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 return false;
734 }
735 return true;
736}
737
Ian Rogers776ac1f2012-04-13 23:36:36 -0700738bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
741 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 return false;
743 }
744 return true;
745}
746
Ian Rogers776ac1f2012-04-13 23:36:36 -0700747bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
750 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 return false;
752 }
753 return true;
754}
755
Ian Rogers776ac1f2012-04-13 23:36:36 -0700756bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
759 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 return false;
761 }
762 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700763 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 return false;
767 }
768 return true;
769}
770
Ian Rogers776ac1f2012-04-13 23:36:36 -0700771bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
774 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 return false;
776 }
777 return true;
778}
779
Ian Rogers776ac1f2012-04-13 23:36:36 -0700780bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
783 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 return false;
785 }
786 return true;
787}
788
Ian Rogers776ac1f2012-04-13 23:36:36 -0700789bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
792 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 return false;
794 }
795 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700796 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 const char* cp = descriptor;
798 while (*cp++ == '[') {
799 bracket_count++;
800 }
801 if (bracket_count == 0) {
802 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700803 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
804 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 return false;
806 } else if (bracket_count > 255) {
807 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
809 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 return false;
811 }
812 return true;
813}
814
Ian Rogers776ac1f2012-04-13 23:36:36 -0700815bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
817 const uint16_t* insns = code_item_->insns_ + cur_offset;
818 const uint16_t* array_data;
819 int32_t array_data_offset;
820
821 DCHECK_LT(cur_offset, insn_count);
822 /* make sure the start of the array data table is in range */
823 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
824 if ((int32_t) cur_offset + array_data_offset < 0 ||
825 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700827 << ", data offset " << array_data_offset
828 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 return false;
830 }
831 /* offset to array data table is a relative branch-style offset */
832 array_data = insns + array_data_offset;
833 /* make sure the table is 32-bit aligned */
834 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
836 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 return false;
838 }
839 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700840 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
842 /* make sure the end of the switch is in range */
843 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
845 << ", data offset " << array_data_offset << ", end "
846 << cur_offset + array_data_offset + table_size
847 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 return false;
849 }
850 return true;
851}
852
Ian Rogers776ac1f2012-04-13 23:36:36 -0700853bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 int32_t offset;
855 bool isConditional, selfOkay;
856 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
857 return false;
858 }
859 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
861 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 return false;
863 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700864 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
865 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
868 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 return false;
870 }
871 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
872 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700873 if (abs_offset < 0 ||
874 (uint32_t) abs_offset >= insn_count ||
875 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700877 << reinterpret_cast<void*>(abs_offset) << ") at "
878 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 return false;
880 }
881 insn_flags_[abs_offset].SetBranchTarget();
882 return true;
883}
884
Ian Rogers776ac1f2012-04-13 23:36:36 -0700885bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 bool* selfOkay) {
887 const uint16_t* insns = code_item_->insns_ + cur_offset;
888 *pConditional = false;
889 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700890 switch (*insns & 0xff) {
891 case Instruction::GOTO:
892 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 break;
894 case Instruction::GOTO_32:
895 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 *selfOkay = true;
897 break;
898 case Instruction::GOTO_16:
899 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700900 break;
901 case Instruction::IF_EQ:
902 case Instruction::IF_NE:
903 case Instruction::IF_LT:
904 case Instruction::IF_GE:
905 case Instruction::IF_GT:
906 case Instruction::IF_LE:
907 case Instruction::IF_EQZ:
908 case Instruction::IF_NEZ:
909 case Instruction::IF_LTZ:
910 case Instruction::IF_GEZ:
911 case Instruction::IF_GTZ:
912 case Instruction::IF_LEZ:
913 *pOffset = (int16_t) insns[1];
914 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700915 break;
916 default:
917 return false;
918 break;
919 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700920 return true;
921}
922
Ian Rogers776ac1f2012-04-13 23:36:36 -0700923bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700925 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700927 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
929 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700931 << ", switch offset " << switch_offset
932 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700933 return false;
934 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700935 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 /* make sure the table is 32-bit aligned */
938 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
940 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700941 return false;
942 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 uint32_t switch_count = switch_insns[1];
944 int32_t keys_offset, targets_offset;
945 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
947 /* 0=sig, 1=count, 2/3=firstKey */
948 targets_offset = 4;
949 keys_offset = -1;
950 expected_signature = Instruction::kPackedSwitchSignature;
951 } else {
952 /* 0=sig, 1=count, 2..count*2 = keys */
953 keys_offset = 2;
954 targets_offset = 2 + 2 * switch_count;
955 expected_signature = Instruction::kSparseSwitchSignature;
956 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700958 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700959 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
960 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
961 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700962 return false;
963 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 /* make sure the end of the switch is in range */
965 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
967 << ", switch offset " << switch_offset
968 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700969 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700970 return false;
971 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700972 /* for a sparse switch, verify the keys are in ascending order */
973 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
975 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700976 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
977 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
978 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
980 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700981 return false;
982 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700983 last_key = key;
984 }
985 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700986 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 for (uint32_t targ = 0; targ < switch_count; targ++) {
988 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
989 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
990 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700991 if (abs_offset < 0 ||
992 abs_offset >= (int32_t) insn_count ||
993 !insn_flags_[abs_offset].IsOpcode()) {
994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
995 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
996 << reinterpret_cast<void*>(cur_offset)
997 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700998 return false;
999 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 insn_flags_[abs_offset].SetBranchTarget();
1001 }
1002 return true;
1003}
1004
Ian Rogers776ac1f2012-04-13 23:36:36 -07001005bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -07001007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 return false;
1009 }
1010 uint16_t registers_size = code_item_->registers_size_;
1011 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001012 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1014 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 return false;
1016 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001017 }
1018
1019 return true;
1020}
1021
Ian Rogers776ac1f2012-04-13 23:36:36 -07001022bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 uint16_t registers_size = code_item_->registers_size_;
1024 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1025 // integer overflow when adding them here.
1026 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1028 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001029 return false;
1030 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001031 return true;
1032}
1033
Ian Rogers776ac1f2012-04-13 23:36:36 -07001034bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 uint16_t registers_size = code_item_->registers_size_;
1036 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001037
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001039 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1040 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 }
1042 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001043 reg_table_.Init(kTrackCompilerInterestPoints,
1044 insn_flags_.get(),
1045 insns_size,
1046 registers_size,
1047 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001048
jeffhaobdb76512011-09-07 11:43:16 -07001049
Ian Rogersd0fbd852013-09-24 18:17:04 -07001050 work_line_.reset(RegisterLine::Create(registers_size, this));
1051 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001052
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 /* Initialize register types of method arguments. */
1054 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001055 DCHECK_NE(failures_.size(), 0U);
1056 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001057 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001058 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 return false;
1060 }
1061 /* Perform code flow verification. */
1062 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001063 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001065 }
1066
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001067 // Compute information for compiler.
1068 if (Runtime::Current()->IsCompiler()) {
1069 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001070 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001071 if (compile) {
1072 /* Generate a register map and add it to the method. */
Vladimir Marko8171fc32013-11-26 17:05:58 +00001073 const std::vector<uint8_t>* dex_gc_map = GenerateGcMap();
Vladimir Markoc255e972013-11-19 11:21:24 +00001074 if (dex_gc_map == NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001075 DCHECK_NE(failures_.size(), 0U);
1076 return false; // Not a real failure, but a failure to encode
1077 }
1078 if (kIsDebugBuild) {
Vladimir Marko8171fc32013-11-26 17:05:58 +00001079 VerifyGcMap(*dex_gc_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001080 }
Vladimir Markoc255e972013-11-19 11:21:24 +00001081 verifier::MethodVerifier::SetDexGcMap(ref, dex_gc_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001082 }
Logan Chiendd361c92012-04-10 23:40:37 +08001083
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001084 if (has_check_casts_) {
1085 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001086 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001087 SetSafeCastMap(ref, method_to_safe_casts);
1088 }
1089 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001090
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001091 if (has_virtual_or_interface_invokes_) {
1092 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001093 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001094 SetDevirtMap(ref, pc_to_concrete_method);
1095 }
1096 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001097 }
jeffhaobdb76512011-09-07 11:43:16 -07001098 return true;
1099}
1100
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1102 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001103 for (size_t i = 0; i < failures_.size(); ++i) {
1104 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001105 }
1106 return os;
1107}
1108
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001111 v->Dump(std::cerr);
1112}
1113
Ian Rogers776ac1f2012-04-13 23:36:36 -07001114void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001115 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001116 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 return;
jeffhaobdb76512011-09-07 11:43:16 -07001118 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001119 {
1120 os << "Register Types:\n";
1121 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1122 std::ostream indent_os(&indent_filter);
1123 reg_types_.Dump(indent_os);
1124 }
Ian Rogersb4903572012-10-11 11:52:56 -07001125 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001126 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1127 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 const Instruction* inst = Instruction::At(code_item_->insns_);
1129 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1130 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1132 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001133 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001134 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001135 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001136 const bool kDumpHexOfInstruction = false;
1137 if (kDumpHexOfInstruction) {
1138 indent_os << inst->DumpHex(5) << " ";
1139 }
1140 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001141 inst = inst->Next();
1142 }
jeffhaobdb76512011-09-07 11:43:16 -07001143}
1144
Ian Rogersd81871c2011-10-03 13:57:23 -07001145static bool IsPrimitiveDescriptor(char descriptor) {
1146 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001147 case 'I':
1148 case 'C':
1149 case 'S':
1150 case 'B':
1151 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001152 case 'F':
1153 case 'D':
1154 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001156 default:
1157 return false;
1158 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001159}
1160
Ian Rogers776ac1f2012-04-13 23:36:36 -07001161bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 RegisterLine* reg_line = reg_table_.GetLine(0);
1163 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1164 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001165
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001167 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001169 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1171 // argument as uninitialized. This restricts field access until the superclass constructor is
1172 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001173 const RegType& declaring_class = GetDeclaringClass();
1174 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 reg_line->SetRegisterType(arg_start + cur_arg,
1176 reg_types_.UninitializedThisArgument(declaring_class));
1177 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001178 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001179 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001181 }
1182
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001183 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001184 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001185 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001186
1187 for (; iterator.HasNext(); iterator.Next()) {
1188 const char* descriptor = iterator.GetDescriptor();
1189 if (descriptor == NULL) {
1190 LOG(FATAL) << "Null descriptor";
1191 }
1192 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1194 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 return false;
1196 }
1197 switch (descriptor[0]) {
1198 case 'L':
1199 case '[':
1200 // We assume that reference arguments are initialized. The only way it could be otherwise
1201 // (assuming the caller was verified) is if the current method is <init>, but in that case
1202 // it's effectively considered initialized the instant we reach here (in the sense that we
1203 // can return without doing anything or call virtual methods).
1204 {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001205 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
1206 false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001207 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 }
1209 break;
1210 case 'Z':
1211 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1212 break;
1213 case 'C':
1214 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1215 break;
1216 case 'B':
1217 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1218 break;
1219 case 'I':
1220 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1221 break;
1222 case 'S':
1223 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1224 break;
1225 case 'F':
1226 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1227 break;
1228 case 'J':
1229 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001230 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1231 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1232 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 cur_arg++;
1234 break;
1235 }
1236 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1238 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 return false;
1240 }
1241 cur_arg++;
1242 }
1243 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1245 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 return false;
1247 }
1248 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1249 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1250 // format. Only major difference from the method argument format is that 'V' is supported.
1251 bool result;
1252 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1253 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001254 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 size_t i = 0;
1256 do {
1257 i++;
1258 } while (descriptor[i] == '['); // process leading [
1259 if (descriptor[i] == 'L') { // object array
1260 do {
1261 i++; // find closing ;
1262 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1263 result = descriptor[i] == ';';
1264 } else { // primitive array
1265 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1266 }
1267 } else if (descriptor[0] == 'L') {
1268 // could be more thorough here, but shouldn't be required
1269 size_t i = 0;
1270 do {
1271 i++;
1272 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1273 result = descriptor[i] == ';';
1274 } else {
1275 result = false;
1276 }
1277 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1279 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 }
1281 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001282}
1283
Ian Rogers776ac1f2012-04-13 23:36:36 -07001284bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 const uint16_t* insns = code_item_->insns_;
1286 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001287
jeffhaobdb76512011-09-07 11:43:16 -07001288 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 insn_flags_[0].SetChanged();
1290 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001291
jeffhaobdb76512011-09-07 11:43:16 -07001292 /* Continue until no instructions are marked "changed". */
1293 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1295 uint32_t insn_idx = start_guess;
1296 for (; insn_idx < insns_size; insn_idx++) {
1297 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001298 break;
1299 }
jeffhaobdb76512011-09-07 11:43:16 -07001300 if (insn_idx == insns_size) {
1301 if (start_guess != 0) {
1302 /* try again, starting from the top */
1303 start_guess = 0;
1304 continue;
1305 } else {
1306 /* all flags are clear */
1307 break;
1308 }
1309 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 // We carry the working set of registers from instruction to instruction. If this address can
1311 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1312 // "changed" flags, we need to load the set of registers from the table.
1313 // Because we always prefer to continue on to the next instruction, we should never have a
1314 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1315 // target.
1316 work_insn_idx_ = insn_idx;
1317 if (insn_flags_[insn_idx].IsBranchTarget()) {
1318 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001319 } else {
1320#ifndef NDEBUG
1321 /*
1322 * Sanity check: retrieve the stored register line (assuming
1323 * a full table) and make sure it actually matches.
1324 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1326 if (register_line != NULL) {
1327 if (work_line_->CompareLine(register_line) != 0) {
1328 Dump(std::cout);
1329 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001330 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001331 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1332 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001333 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 }
jeffhaobdb76512011-09-07 11:43:16 -07001335 }
1336#endif
1337 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001339 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001340 prepend += " failed to verify: ";
1341 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 return false;
1343 }
jeffhaobdb76512011-09-07 11:43:16 -07001344 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 insn_flags_[insn_idx].SetVisited();
1346 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001347 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001348
Ian Rogers1c849e52012-06-28 14:00:33 -07001349 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001351 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001352 * (besides the wasted space), but it indicates a flaw somewhere
1353 * down the line, possibly in the verifier.
1354 *
1355 * If we've substituted "always throw" instructions into the stream,
1356 * we are almost certainly going to have some dead code.
1357 */
1358 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 uint32_t insn_idx = 0;
1360 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001361 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001362 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001363 * may or may not be preceded by a padding NOP (for alignment).
1364 */
1365 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1366 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1367 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001368 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001369 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1370 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1371 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001373 }
1374
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001376 if (dead_start < 0)
1377 dead_start = insn_idx;
1378 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001379 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1380 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001381 dead_start = -1;
1382 }
1383 }
1384 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001385 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1386 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001388 // To dump the state of the verify after a method, do something like:
1389 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1390 // "boolean java.lang.String.equals(java.lang.Object)") {
1391 // LOG(INFO) << info_messages_.str();
1392 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 }
jeffhaobdb76512011-09-07 11:43:16 -07001394 return true;
1395}
1396
Ian Rogers776ac1f2012-04-13 23:36:36 -07001397bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001398 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1399 // We want the state _before_ the instruction, for the case where the dex pc we're
1400 // interested in is itself a monitor-enter instruction (which is a likely place
1401 // for a thread to be suspended).
1402 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001403 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001404 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1405 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1406 }
1407 }
1408
jeffhaobdb76512011-09-07 11:43:16 -07001409 /*
1410 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001411 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001412 * control to another statement:
1413 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001414 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001415 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001416 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001417 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001418 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001419 * throw an exception that is handled by an encompassing "try"
1420 * block.
1421 *
1422 * We can also return, in which case there is no successor instruction
1423 * from this point.
1424 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001425 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001426 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1428 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001429 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001430
jeffhaobdb76512011-09-07 11:43:16 -07001431 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001432 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001433 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001435 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1436 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 }
jeffhaobdb76512011-09-07 11:43:16 -07001438
1439 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001440 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001441 * can throw an exception, we will copy/merge this into the "catch"
1442 * address rather than work_line, because we don't want the result
1443 * from the "successful" code path (e.g. a check-cast that "improves"
1444 * a type) to be visible to the exception handler.
1445 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001446 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001448 } else {
1449#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001451#endif
1452 }
1453
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001454
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001455 // We need to ensure the work line is consistent while performing validation. When we spot a
1456 // peephole pattern we compute a new line for either the fallthrough instruction or the
1457 // branch target.
1458 UniquePtr<RegisterLine> branch_line;
1459 UniquePtr<RegisterLine> fallthrough_line;
1460
Sebastien Hertz5243e912013-05-21 10:55:07 +02001461 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::NOP:
1463 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001464 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001465 * a signature that looks like a NOP; if we see one of these in
1466 * the course of executing code then we have a problem.
1467 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001468 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001470 }
1471 break;
1472
1473 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1475 break;
jeffhaobdb76512011-09-07 11:43:16 -07001476 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001477 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1478 break;
jeffhaobdb76512011-09-07 11:43:16 -07001479 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001480 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001481 break;
1482 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001483 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1484 break;
jeffhaobdb76512011-09-07 11:43:16 -07001485 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001486 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1487 break;
jeffhaobdb76512011-09-07 11:43:16 -07001488 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001489 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001490 break;
1491 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001492 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1493 break;
jeffhaobdb76512011-09-07 11:43:16 -07001494 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1496 break;
jeffhaobdb76512011-09-07 11:43:16 -07001497 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001498 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001499 break;
1500
1501 /*
1502 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001503 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001504 * might want to hold the result in an actual CPU register, so the
1505 * Dalvik spec requires that these only appear immediately after an
1506 * invoke or filled-new-array.
1507 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001508 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001509 * redundant with the reset done below, but it can make the debug info
1510 * easier to read in some cases.)
1511 */
1512 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001513 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001514 break;
1515 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001516 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001517 break;
1518 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001519 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001520 break;
1521
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001523 /*
jeffhao60f83e32012-02-13 17:16:30 -08001524 * This statement can only appear as the first instruction in an exception handler. We verify
1525 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001526 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001527 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001528 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001529 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 }
jeffhaobdb76512011-09-07 11:43:16 -07001531 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001532 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1533 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 }
jeffhaobdb76512011-09-07 11:43:16 -07001536 }
1537 break;
1538 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001539 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001540 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 const RegType& return_type = GetMethodReturnType();
1542 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1544 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 } else {
1546 // Compilers may generate synthetic functions that write byte values into boolean fields.
1547 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001548 const uint32_t vregA = inst->VRegA_11x();
1549 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1551 ((return_type.IsBoolean() || return_type.IsByte() ||
1552 return_type.IsShort() || return_type.IsChar()) &&
1553 src_type.IsInteger()));
1554 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001555 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001556 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001557 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001558 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 }
jeffhaobdb76512011-09-07 11:43:16 -07001560 }
1561 }
1562 break;
1563 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001564 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001565 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 const RegType& return_type = GetMethodReturnType();
1567 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 } else {
1570 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 const uint32_t vregA = inst->VRegA_11x();
1572 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001573 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001574 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 }
jeffhaobdb76512011-09-07 11:43:16 -07001576 }
1577 }
1578 break;
1579 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001580 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 const RegType& return_type = GetMethodReturnType();
1582 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 } else {
1585 /* return_type is the *expected* return type, not register value */
1586 DCHECK(!return_type.IsZero());
1587 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001588 const uint32_t vregA = inst->VRegA_11x();
1589 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001590 // Disallow returning uninitialized values and verify that the reference in vAA is an
1591 // instance of the "return_type"
1592 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001593 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1594 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001595 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001596 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1597 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1598 << "' or '" << reg_type << "'";
1599 } else {
1600 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1601 << "', but expected from declaration '" << return_type << "'";
1602 }
jeffhaobdb76512011-09-07 11:43:16 -07001603 }
1604 }
1605 }
1606 break;
1607
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001608 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 case Instruction::CONST_4: {
1610 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1611 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001612 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001613 }
1614 case Instruction::CONST_16: {
1615 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1616 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001617 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 }
jeffhaobdb76512011-09-07 11:43:16 -07001619 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 work_line_->SetRegisterType(inst->VRegA_31i(),
1621 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001622 break;
1623 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 work_line_->SetRegisterType(inst->VRegA_21h(),
1625 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001626 break;
jeffhaobdb76512011-09-07 11:43:16 -07001627 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001629 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1631 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001633 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 }
1635 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001637 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1638 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001640 break;
1641 }
1642 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001643 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1645 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001647 break;
1648 }
1649 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1652 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001654 break;
1655 }
jeffhaobdb76512011-09-07 11:43:16 -07001656 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1658 break;
jeffhaobdb76512011-09-07 11:43:16 -07001659 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001661 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001662 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001663 // Get type from instruction if unresolved then we need an access check
1664 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001666 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001668 res_type.IsConflict() ? res_type
1669 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001670 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 }
jeffhaobdb76512011-09-07 11:43:16 -07001672 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001673 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001674 break;
1675 case Instruction::MONITOR_EXIT:
1676 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001678 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001679 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001680 * to the need to handle asynchronous exceptions, a now-deprecated
1681 * feature that Dalvik doesn't support.)
1682 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001683 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001684 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001685 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001686 * structured locking checks are working, the former would have
1687 * failed on the -enter instruction, and the latter is impossible.
1688 *
1689 * This is fortunate, because issue 3221411 prevents us from
1690 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001691 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001692 * some catch blocks (which will show up as "dead" code when
1693 * we skip them here); if we can't, then the code path could be
1694 * "live" so we still need to check it.
1695 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001697 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001698 break;
1699
Ian Rogers28ad40d2011-10-27 15:19:26 -07001700 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001702 /*
1703 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1704 * could be a "upcast" -- not expected, so we don't try to address it.)
1705 *
1706 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001708 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1710 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1711 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001712 if (res_type.IsConflict()) {
1713 DCHECK_NE(failures_.size(), 0U);
1714 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001716 }
1717 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001718 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001719 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1721 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001722 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 if (is_checkcast) {
1724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1725 } else {
1726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1727 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001728 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 if (is_checkcast) {
1730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1731 } else {
1732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1733 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001734 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001735 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001739 }
jeffhaobdb76512011-09-07 11:43:16 -07001740 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001741 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 }
1743 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001744 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001745 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001746 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 }
1751 }
1752 break;
1753 }
1754 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001756 if (res_type.IsConflict()) {
1757 DCHECK_NE(failures_.size(), 0U);
1758 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001759 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001760 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1761 // can't create an instance of an interface or abstract class */
1762 if (!res_type.IsInstantiableTypes()) {
1763 Fail(VERIFY_ERROR_INSTANTIATION)
1764 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001765 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001767 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1768 // Any registers holding previous allocations from this address that have not yet been
1769 // initialized must be marked invalid.
1770 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1771 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001772 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 break;
1774 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001775 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001777 break;
1778 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001780 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001782 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001784 just_set_result = true; // Filled new array range sets result register
1785 break;
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::CMPL_FLOAT:
1787 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001789 break;
1790 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001792 break;
1793 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001795 break;
1796 case Instruction::CMPL_DOUBLE:
1797 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001800 break;
1801 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001804 break;
1805 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001806 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
1808 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001810 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001811 break;
1812 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001813 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001814 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001815 break;
1816 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001818 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001821 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001822 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1823 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001824 }
1825 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 }
jeffhaobdb76512011-09-07 11:43:16 -07001827 case Instruction::GOTO:
1828 case Instruction::GOTO_16:
1829 case Instruction::GOTO_32:
1830 /* no effect on or use of registers */
1831 break;
1832
1833 case Instruction::PACKED_SWITCH:
1834 case Instruction::SPARSE_SWITCH:
1835 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001837 break;
1838
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 case Instruction::FILL_ARRAY_DATA: {
1840 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001842 /* array_type can be null if the reg type is Zero */
1843 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001844 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1846 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001847 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001848 const RegType& component_type = reg_types_.GetComponentType(array_type,
1849 class_loader_->get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001850 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001851 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1853 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 } else {
jeffhao457cc512012-02-02 16:55:13 -08001855 // Now verify if the element width in the table matches the element width declared in
1856 // the array
1857 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1858 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001860 } else {
1861 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1862 // Since we don't compress the data in Dex, expect to see equal width of data stored
1863 // in the table and expected from the array class.
1864 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1866 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001867 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 }
1869 }
jeffhaobdb76512011-09-07 11:43:16 -07001870 }
1871 }
1872 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 }
jeffhaobdb76512011-09-07 11:43:16 -07001874 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001876 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1877 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 bool mismatch = false;
1879 if (reg_type1.IsZero()) { // zero then integral or reference expected
1880 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1881 } else if (reg_type1.IsReferenceTypes()) { // both references?
1882 mismatch = !reg_type2.IsReferenceTypes();
1883 } else { // both integral?
1884 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1885 }
1886 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1888 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001889 }
1890 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 }
jeffhaobdb76512011-09-07 11:43:16 -07001892 case Instruction::IF_LT:
1893 case Instruction::IF_GE:
1894 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001896 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1897 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1900 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001901 }
1902 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 }
jeffhaobdb76512011-09-07 11:43:16 -07001904 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001906 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1909 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001911
1912 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001913 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001914 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001915 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001916 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001917 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918 }
Ian Rogers9b360392013-06-06 14:45:07 -07001919 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001920 } else {
1921 break;
1922 }
1923
Ian Rogers9b360392013-06-06 14:45:07 -07001924 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001925
1926 /* Check for peep-hole pattern of:
1927 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001928 * instance-of vX, vY, T;
1929 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001930 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001931 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001932 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001933 * and sharpen the type of vY to be type T.
1934 * Note, this pattern can't be if:
1935 * - if there are other branches to this branch,
1936 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001937 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001938 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001939 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1940 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1941 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001942 // Check that the we are not attempting conversion to interface types,
1943 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001944 // Also don't change the type if it would result in an upcast.
1945 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001946 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001947
Jeff Haoa3faaf42013-09-03 19:07:00 -07001948 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1949 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001950 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001951 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001952 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001953 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001954 branch_line.reset(update_line);
1955 }
1956 update_line->CopyFromLine(work_line_.get());
1957 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1958 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1959 // See if instance-of was preceded by a move-object operation, common due to the small
1960 // register encoding space of instance-of, and propagate type information to the source
1961 // of the move-object.
1962 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001963 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001964 move_idx--;
1965 }
1966 CHECK(insn_flags_[move_idx].IsOpcode());
1967 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1968 switch (move_inst->Opcode()) {
1969 case Instruction::MOVE_OBJECT:
1970 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1971 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1972 }
1973 break;
1974 case Instruction::MOVE_OBJECT_FROM16:
1975 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1976 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1977 }
1978 break;
1979 case Instruction::MOVE_OBJECT_16:
1980 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1981 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1982 }
1983 break;
1984 default:
1985 break;
1986 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001987 }
1988 }
1989 }
1990
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 }
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::IF_LTZ:
1994 case Instruction::IF_GEZ:
1995 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2000 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 }
jeffhaobdb76512011-09-07 11:43:16 -07002002 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 }
jeffhaobdb76512011-09-07 11:43:16 -07002004 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002005 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 break;
jeffhaobdb76512011-09-07 11:43:16 -07002007 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002008 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 break;
jeffhaobdb76512011-09-07 11:43:16 -07002010 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002011 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 break;
jeffhaobdb76512011-09-07 11:43:16 -07002013 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002014 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002015 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 break;
jeffhaobdb76512011-09-07 11:43:16 -07002019 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002021 break;
2022 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 break;
2029 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 break;
2032 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 break;
2035 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
2038 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002039 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
2041 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002042 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002043 break;
2044 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
2047
jeffhaobdb76512011-09-07 11:43:16 -07002048 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 break;
jeffhaobdb76512011-09-07 11:43:16 -07002051 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 break;
jeffhaobdb76512011-09-07 11:43:16 -07002054 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 break;
2060 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
2066 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 break;
jeffhaobdb76512011-09-07 11:43:16 -07002069
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 break;
2073 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
2076 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 break;
2079 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 break;
jeffhaobdb76512011-09-07 11:43:16 -07002088 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
jeffhaobdb76512011-09-07 11:43:16 -07002095 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 break;
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 break;
jeffhaobdb76512011-09-07 11:43:16 -07002101 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 break;
2104 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 break;
2113
2114 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 break;
2117 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 break;
2120 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 break;
2123 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
2126 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002130 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002133 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
2135
2136 case Instruction::INVOKE_VIRTUAL:
2137 case Instruction::INVOKE_VIRTUAL_RANGE:
2138 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2141 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2142 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2143 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002144 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002145 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002146 const RegType* return_type = nullptr;
2147 if (called_method != nullptr) {
2148 MethodHelper mh(called_method);
2149 mirror::Class* return_type_class = mh.GetReturnType();
2150 if (return_type_class != nullptr) {
2151 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2152 return_type_class->CannotBeAssignedFromOtherTypes());
2153 } else {
2154 Thread* self = Thread::Current();
2155 DCHECK(self->IsExceptionPending());
2156 self->ClearException();
2157 }
2158 }
2159 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002161 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2162 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002163 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002164 return_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002165 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002166 if (!return_type->IsLowHalf()) {
2167 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002168 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002169 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002171 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002177 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002178 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002179 const char* return_type_descriptor;
2180 bool is_constructor;
2181 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002183 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002184 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002185 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2186 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2187 } else {
2188 is_constructor = called_method->IsConstructor();
2189 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2190 }
2191 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002192 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 * Some additional checks when calling a constructor. We know from the invocation arg check
2194 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2195 * that to require that called_method->klass is the same as this->klass or this->super,
2196 * allowing the latter only if the "this" argument is the same as the "this" argument to
2197 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002198 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002200 if (this_type.IsConflict()) // failure.
2201 break;
jeffhaobdb76512011-09-07 11:43:16 -07002202
jeffhaob57e9522012-04-26 18:08:21 -07002203 /* no null refs allowed (?) */
2204 if (this_type.IsZero()) {
2205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2206 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002207 }
jeffhaob57e9522012-04-26 18:08:21 -07002208
2209 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002210 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2211 // TODO: re-enable constructor type verification
2212 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002213 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002214 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2215 // break;
2216 // }
jeffhaob57e9522012-04-26 18:08:21 -07002217
2218 /* arg must be an uninitialized reference */
2219 if (!this_type.IsUninitializedTypes()) {
2220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2221 << this_type;
2222 break;
2223 }
2224
2225 /*
2226 * Replace the uninitialized reference with an initialized one. We need to do this for all
2227 * registers that have the same object instance in them, not just the "this" register.
2228 */
2229 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002230 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002231 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(),
2232 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002233 if (!return_type.IsLowHalf()) {
2234 work_line_->SetResultRegisterType(return_type);
2235 } else {
2236 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2237 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002238 just_set_result = true;
2239 break;
2240 }
2241 case Instruction::INVOKE_STATIC:
2242 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002244 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002245 METHOD_STATIC,
2246 is_range,
2247 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002248 const char* descriptor;
2249 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002251 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2252 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002253 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002254 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002255 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002256 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002257 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2258 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002259 if (!return_type.IsLowHalf()) {
2260 work_line_->SetResultRegisterType(return_type);
2261 } else {
2262 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2263 }
jeffhaobdb76512011-09-07 11:43:16 -07002264 just_set_result = true;
2265 }
2266 break;
jeffhaobdb76512011-09-07 11:43:16 -07002267 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002269 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002270 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002271 METHOD_INTERFACE,
2272 is_range,
2273 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002274 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002275 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002276 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2277 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2278 << PrettyMethod(abs_method) << "'";
2279 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002280 }
Ian Rogers0d604842012-04-16 14:50:24 -07002281 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002282 /* Get the type of the "this" arg, which should either be a sub-interface of called
2283 * interface or Object (see comments in RegType::JoinClass).
2284 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002285 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002286 if (this_type.IsZero()) {
2287 /* null pointer always passes (and always fails at runtime) */
2288 } else {
2289 if (this_type.IsUninitializedTypes()) {
2290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2291 << this_type;
2292 break;
2293 }
2294 // In the past we have tried to assert that "called_interface" is assignable
2295 // from "this_type.GetClass()", however, as we do an imprecise Join
2296 // (RegType::JoinClass) we don't have full information on what interfaces are
2297 // implemented by "this_type". For example, two classes may implement the same
2298 // interfaces and have a common parent that doesn't implement the interface. The
2299 // join will set "this_type" to the parent class and a test that this implements
2300 // the interface will incorrectly fail.
2301 }
2302 /*
2303 * We don't have an object instance, so we can't find the concrete method. However, all of
2304 * the type information is in the abstract method, so we're good.
2305 */
2306 const char* descriptor;
2307 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002309 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2310 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2311 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2312 } else {
2313 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2314 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002315 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2316 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 if (!return_type.IsLowHalf()) {
2318 work_line_->SetResultRegisterType(return_type);
2319 } else {
2320 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2321 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002322 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 }
jeffhaobdb76512011-09-07 11:43:16 -07002325 case Instruction::NEG_INT:
2326 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329 case Instruction::NEG_LONG:
2330 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002338 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
2345 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002353 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002354 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002358 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002359 break;
2360 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002361 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002362 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002365 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002369 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002372 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002373 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002381 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002384 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002385 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
2387 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002388 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002391 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
2396
2397 case Instruction::ADD_INT:
2398 case Instruction::SUB_INT:
2399 case Instruction::MUL_INT:
2400 case Instruction::REM_INT:
2401 case Instruction::DIV_INT:
2402 case Instruction::SHL_INT:
2403 case Instruction::SHR_INT:
2404 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002406 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408 case Instruction::AND_INT:
2409 case Instruction::OR_INT:
2410 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002412 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::ADD_LONG:
2415 case Instruction::SUB_LONG:
2416 case Instruction::MUL_LONG:
2417 case Instruction::DIV_LONG:
2418 case Instruction::REM_LONG:
2419 case Instruction::AND_LONG:
2420 case Instruction::OR_LONG:
2421 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002422 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002423 reg_types_.LongLo(), reg_types_.LongHi(),
2424 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426 case Instruction::SHL_LONG:
2427 case Instruction::SHR_LONG:
2428 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002430 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002431 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::ADD_FLOAT:
2434 case Instruction::SUB_FLOAT:
2435 case Instruction::MUL_FLOAT:
2436 case Instruction::DIV_FLOAT:
2437 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002438 work_line_->CheckBinaryOp(inst,
2439 reg_types_.Float(),
2440 reg_types_.Float(),
2441 reg_types_.Float(),
2442 false);
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::ADD_DOUBLE:
2445 case Instruction::SUB_DOUBLE:
2446 case Instruction::MUL_DOUBLE:
2447 case Instruction::DIV_DOUBLE:
2448 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002449 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002450 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2451 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::ADD_INT_2ADDR:
2454 case Instruction::SUB_INT_2ADDR:
2455 case Instruction::MUL_INT_2ADDR:
2456 case Instruction::REM_INT_2ADDR:
2457 case Instruction::SHL_INT_2ADDR:
2458 case Instruction::SHR_INT_2ADDR:
2459 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002460 work_line_->CheckBinaryOp2addr(inst,
2461 reg_types_.Integer(),
2462 reg_types_.Integer(),
2463 reg_types_.Integer(),
2464 false);
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::AND_INT_2ADDR:
2467 case Instruction::OR_INT_2ADDR:
2468 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002469 work_line_->CheckBinaryOp2addr(inst,
2470 reg_types_.Integer(),
2471 reg_types_.Integer(),
2472 reg_types_.Integer(),
2473 true);
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002476 work_line_->CheckBinaryOp2addr(inst,
2477 reg_types_.Integer(),
2478 reg_types_.Integer(),
2479 reg_types_.Integer(),
2480 false);
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::ADD_LONG_2ADDR:
2483 case Instruction::SUB_LONG_2ADDR:
2484 case Instruction::MUL_LONG_2ADDR:
2485 case Instruction::DIV_LONG_2ADDR:
2486 case Instruction::REM_LONG_2ADDR:
2487 case Instruction::AND_LONG_2ADDR:
2488 case Instruction::OR_LONG_2ADDR:
2489 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002491 reg_types_.LongLo(), reg_types_.LongHi(),
2492 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::SHL_LONG_2ADDR:
2495 case Instruction::SHR_LONG_2ADDR:
2496 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002497 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002498 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::ADD_FLOAT_2ADDR:
2501 case Instruction::SUB_FLOAT_2ADDR:
2502 case Instruction::MUL_FLOAT_2ADDR:
2503 case Instruction::DIV_FLOAT_2ADDR:
2504 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002505 work_line_->CheckBinaryOp2addr(inst,
2506 reg_types_.Float(),
2507 reg_types_.Float(),
2508 reg_types_.Float(),
2509 false);
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::ADD_DOUBLE_2ADDR:
2512 case Instruction::SUB_DOUBLE_2ADDR:
2513 case Instruction::MUL_DOUBLE_2ADDR:
2514 case Instruction::DIV_DOUBLE_2ADDR:
2515 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002516 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002517 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2518 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
2520 case Instruction::ADD_INT_LIT16:
2521 case Instruction::RSUB_INT:
2522 case Instruction::MUL_INT_LIT16:
2523 case Instruction::DIV_INT_LIT16:
2524 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002525 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002526 break;
2527 case Instruction::AND_INT_LIT16:
2528 case Instruction::OR_INT_LIT16:
2529 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002530 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::ADD_INT_LIT8:
2533 case Instruction::RSUB_INT_LIT8:
2534 case Instruction::MUL_INT_LIT8:
2535 case Instruction::DIV_INT_LIT8:
2536 case Instruction::REM_INT_LIT8:
2537 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002538 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002539 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002540 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 case Instruction::AND_INT_LIT8:
2543 case Instruction::OR_INT_LIT8:
2544 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002545 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
2547
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002548 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002549 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002550 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2551 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2553 }
2554 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002555 // Note: the following instructions encode offsets derived from class linking.
2556 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2557 // meaning if the class linking and resolution were successful.
2558 case Instruction::IGET_QUICK:
2559 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2560 break;
2561 case Instruction::IGET_WIDE_QUICK:
2562 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2563 break;
2564 case Instruction::IGET_OBJECT_QUICK:
2565 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2566 break;
2567 case Instruction::IPUT_QUICK:
2568 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2569 break;
2570 case Instruction::IPUT_WIDE_QUICK:
2571 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2572 break;
2573 case Instruction::IPUT_OBJECT_QUICK:
2574 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2575 break;
2576 case Instruction::INVOKE_VIRTUAL_QUICK:
2577 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2578 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002579 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002580 if (called_method != NULL) {
2581 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002582 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2583 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002584 if (!return_type.IsLowHalf()) {
2585 work_line_->SetResultRegisterType(return_type);
2586 } else {
2587 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2588 }
2589 just_set_result = true;
2590 }
2591 break;
2592 }
2593
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002595 case Instruction::UNUSED_3E:
2596 case Instruction::UNUSED_3F:
2597 case Instruction::UNUSED_40:
2598 case Instruction::UNUSED_41:
2599 case Instruction::UNUSED_42:
2600 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002601 case Instruction::UNUSED_79:
2602 case Instruction::UNUSED_7A:
2603 case Instruction::UNUSED_EB:
2604 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002605 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::UNUSED_EE:
2607 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002608 case Instruction::UNUSED_F0:
2609 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002610 case Instruction::UNUSED_F2:
2611 case Instruction::UNUSED_F3:
2612 case Instruction::UNUSED_F4:
2613 case Instruction::UNUSED_F5:
2614 case Instruction::UNUSED_F6:
2615 case Instruction::UNUSED_F7:
2616 case Instruction::UNUSED_F8:
2617 case Instruction::UNUSED_F9:
2618 case Instruction::UNUSED_FA:
2619 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002620 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002621 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002623 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626
2627 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002628 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002629 * complain if an instruction is missing (which is desirable).
2630 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002631 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002632
Ian Rogersad0b3a32012-04-16 14:50:24 -07002633 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002634 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002635 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002636 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002637 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002638 /* immediate failure, reject class */
2639 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2640 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002641 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002642 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002643 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002644 }
jeffhaobdb76512011-09-07 11:43:16 -07002645 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 * If we didn't just set the result register, clear it out. This ensures that you can only use
2647 * "move-result" immediately after the result is set. (We could check this statically, but it's
2648 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002649 */
2650 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002652 }
2653
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002654
jeffhaobdb76512011-09-07 11:43:16 -07002655
2656 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002657 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002658 *
2659 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002660 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002661 * somebody could get a reference field, check it for zero, and if the
2662 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002663 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002664 * that, and will reject the code.
2665 *
2666 * TODO: avoid re-fetching the branch target
2667 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002668 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002669 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002671 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002673 return false;
2674 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002675 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002676 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002677 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 }
jeffhaobdb76512011-09-07 11:43:16 -07002679 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680 if (NULL != branch_line.get()) {
2681 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2682 return false;
2683 }
2684 } else {
2685 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2686 return false;
2687 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 }
jeffhaobdb76512011-09-07 11:43:16 -07002689 }
2690
2691 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002692 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002693 *
2694 * We've already verified that the table is structurally sound, so we
2695 * just need to walk through and tag the targets.
2696 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002697 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002698 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2699 const uint16_t* switch_insns = insns + offset_to_switch;
2700 int switch_count = switch_insns[1];
2701 int offset_to_targets, targ;
2702
2703 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2704 /* 0 = sig, 1 = count, 2/3 = first key */
2705 offset_to_targets = 4;
2706 } else {
2707 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002708 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002709 offset_to_targets = 2 + 2 * switch_count;
2710 }
2711
2712 /* verify each switch target */
2713 for (targ = 0; targ < switch_count; targ++) {
2714 int offset;
2715 uint32_t abs_offset;
2716
2717 /* offsets are 32-bit, and only partly endian-swapped */
2718 offset = switch_insns[offset_to_targets + targ * 2] |
2719 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 abs_offset = work_insn_idx_ + offset;
2721 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002722 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002723 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 }
2725 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002726 return false;
2727 }
2728 }
2729
2730 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2732 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002733 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002734 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002736 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002737
Ian Rogers0571d352011-11-03 19:51:38 -07002738 for (; iterator.HasNext(); iterator.Next()) {
2739 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 within_catch_all = true;
2741 }
jeffhaobdb76512011-09-07 11:43:16 -07002742 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2744 * "work_regs", because at runtime the exception will be thrown before the instruction
2745 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002746 */
Ian Rogers0571d352011-11-03 19:51:38 -07002747 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002748 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 }
jeffhaobdb76512011-09-07 11:43:16 -07002750 }
2751
2752 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2754 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002755 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002757 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 * The state in work_line reflects the post-execution state. If the current instruction is a
2759 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002760 * it will do so before grabbing the lock).
2761 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002762 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002763 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002765 return false;
2766 }
2767 }
2768 }
2769
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002770 /* Handle "continue". Tag the next consecutive instruction.
2771 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2772 * because it changes work_line_ when performing peephole optimization
2773 * and this change should not be used in those cases.
2774 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002775 if ((opcode_flags & Instruction::kContinue) != 0) {
2776 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2777 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2779 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002780 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002781 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2782 // next instruction isn't one.
2783 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2784 return false;
2785 }
2786 if (NULL != fallthrough_line.get()) {
2787 // Make workline consistent with fallthrough computed from peephole optimization.
2788 work_line_->CopyFromLine(fallthrough_line.get());
2789 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002790 if (insn_flags_[next_insn_idx].IsReturn()) {
2791 // For returns we only care about the operand to the return, all other registers are dead.
2792 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2793 Instruction::Code opcode = ret_inst->Opcode();
2794 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2795 work_line_->MarkAllRegistersAsConflicts();
2796 } else {
2797 if (opcode == Instruction::RETURN_WIDE) {
2798 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2799 } else {
2800 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2801 }
2802 }
2803 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002804 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2805 if (next_line != NULL) {
2806 // Merge registers into what we have for the next instruction,
2807 // and set the "changed" flag if needed.
2808 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2809 return false;
2810 }
2811 } else {
2812 /*
2813 * We're not recording register data for the next instruction, so we don't know what the
2814 * prior state was. We have to assume that something has changed and re-evaluate it.
2815 */
2816 insn_flags_[next_insn_idx].SetChanged();
2817 }
2818 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002819
jeffhaod1f0fde2011-09-08 17:25:33 -07002820 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002821 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002822 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 return false;
2824 }
jeffhaobdb76512011-09-07 11:43:16 -07002825 }
2826
2827 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002828 * Update start_guess. Advance to the next instruction of that's
2829 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002830 * neither of those exists we're in a return or throw; leave start_guess
2831 * alone and let the caller sort it out.
2832 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002833 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002835 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002836 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002838 }
2839
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2841 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002842
2843 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002844} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002845
Ian Rogers776ac1f2012-04-13 23:36:36 -07002846const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002847 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002848 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002849 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002850 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002851 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2852 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartier590fee92013-09-13 13:46:47 -07002853 : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 if (result.IsConflict()) {
2855 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2856 << "' in " << referrer;
2857 return result;
2858 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002859 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002860 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002861 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002862 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002863 // check at runtime if access is allowed and so pass here. If result is
2864 // primitive, skip the access check.
2865 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2866 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002867 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002869 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002871}
2872
Ian Rogers776ac1f2012-04-13 23:36:36 -07002873const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002874 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002876 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2878 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002879 CatchHandlerIterator iterator(handlers_ptr);
2880 for (; iterator.HasNext(); iterator.Next()) {
2881 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2882 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002883 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002885 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002886 if (common_super == NULL) {
2887 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2888 // as that is caught at runtime
2889 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002890 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002891 if (exception.IsUnresolvedTypes()) {
2892 // We don't know enough about the type. Fail here and let runtime handle it.
2893 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2894 return exception;
2895 } else {
2896 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2897 return reg_types_.Conflict();
2898 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002899 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002900 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002902 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002903 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 }
2905 }
2906 }
2907 }
Ian Rogers0571d352011-11-03 19:51:38 -07002908 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 }
2910 }
2911 if (common_super == NULL) {
2912 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002913 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002916 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002917}
2918
Brian Carlstromea46f952013-07-30 01:26:50 -07002919mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002920 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002921 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002922 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002923 if (klass_type.IsConflict()) {
2924 std::string append(" in attempt to access method ");
2925 append += dex_file_->GetMethodName(method_id);
2926 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002927 return NULL;
2928 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002929 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002930 return NULL; // Can't resolve Class so no more to do here
2931 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002932 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002933 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002934 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002936 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002937 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002938
2939 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002941 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 res_method = klass->FindInterfaceMethod(name, signature);
2943 } else {
2944 res_method = klass->FindVirtualMethod(name, signature);
2945 }
2946 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002947 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002949 // If a virtual or interface method wasn't found with the expected type, look in
2950 // the direct methods. This can happen when the wrong invoke type is used or when
2951 // a class has changed, and will be flagged as an error in later checks.
2952 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2953 res_method = klass->FindDirectMethod(name, signature);
2954 }
2955 if (res_method == NULL) {
2956 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2957 << PrettyDescriptor(klass) << "." << name
2958 << " " << signature;
2959 return NULL;
2960 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 }
2962 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2964 // enforce them here.
2965 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2967 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 return NULL;
2969 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002970 // Disallow any calls to class initializers.
2971 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2973 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002974 return NULL;
2975 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002976 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002978 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002979 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002980 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002981 }
jeffhaode0d9c92012-02-27 13:58:13 -08002982 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2983 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2985 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002986 return NULL;
2987 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002988 // Check that interface methods match interface classes.
2989 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2990 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2991 << " is in an interface class " << PrettyClass(klass);
2992 return NULL;
2993 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2994 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2995 << " is in a non-interface class " << PrettyClass(klass);
2996 return NULL;
2997 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 // See if the method type implied by the invoke instruction matches the access flags for the
2999 // target method.
3000 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3001 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3002 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3003 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003004 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3005 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 return NULL;
3007 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003008 return res_method;
3009}
3010
Brian Carlstromea46f952013-07-30 01:26:50 -07003011mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003012 MethodType method_type,
3013 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003014 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003015 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3016 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003017 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003018 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003019 if (res_method == NULL) { // error or class is unresolved
3020 return NULL;
3021 }
3022
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3024 // has a vtable entry for the target method.
3025 if (is_super) {
3026 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003028 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003029 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003030 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003031 << " to super " << PrettyMethod(res_method);
3032 return NULL;
3033 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003034 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003035 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003036 MethodHelper mh(res_method);
3037 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003038 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003039 << " to super " << super
3040 << "." << mh.GetName()
3041 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 return NULL;
3043 }
3044 }
3045 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003046 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003048 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 /* caught by static verifier */
3050 DCHECK(is_range || expected_args <= 5);
3051 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3054 return NULL;
3055 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003056
jeffhaobdb76512011-09-07 11:43:16 -07003057 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003058 * Check the "this" argument, which must be an instance of the class that declared the method.
3059 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3060 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003061 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003062 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003064 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003065 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 return NULL;
3067 }
3068 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 return NULL;
3071 }
3072 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003073 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003074 const RegType& res_method_class =
3075 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3076 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003077 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003078 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3079 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003080 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 return NULL;
3082 }
3083 }
3084 actual_args++;
3085 }
3086 /*
3087 * Process the target method's signature. This signature may or may not
3088 * have been verified, so we can't assume it's properly formed.
3089 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003090 MethodHelper mh(res_method);
3091 const DexFile::TypeList* params = mh.GetParameterTypeList();
3092 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003093 uint32_t arg[5];
3094 if (!is_range) {
3095 inst->GetArgs(arg);
3096 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003097 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003098 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003100 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3101 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 return NULL;
3103 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003104 const char* descriptor =
3105 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3106 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003108 << " missing signature component";
3109 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003111 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003112 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003113 if (reg_type.IsIntegralTypes()) {
3114 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3115 if (!src_type.IsIntegralTypes()) {
3116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3117 << " but expected " << reg_type;
3118 return res_method;
3119 }
3120 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003121 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 }
3123 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3124 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003127 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 return NULL;
3129 } else {
3130 return res_method;
3131 }
3132}
3133
Brian Carlstromea46f952013-07-30 01:26:50 -07003134mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003135 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003136 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3137 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3138 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003139 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3140 return NULL;
Sebastien Hertz8249b422013-10-29 17:50:55 +01003141 } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further.
3142 return NULL;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003143 }
3144 mirror::Class* this_class = NULL;
3145 if (!actual_arg_type.IsUnresolvedTypes()) {
3146 this_class = actual_arg_type.GetClass();
3147 } else {
3148 const std::string& descriptor(actual_arg_type.GetDescriptor());
3149 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003150 this_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003151 if (this_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003152 Thread* self = Thread::Current();
3153 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003154 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003155 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3156 this_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003157 }
3158 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003159 if (this_class == NULL) {
3160 return NULL;
3161 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003162 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003163 CHECK(vtable != NULL);
3164 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3165 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003166 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003167 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003168 return res_method;
3169}
3170
Brian Carlstromea46f952013-07-30 01:26:50 -07003171mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003172 bool is_range) {
3173 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003174 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003175 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003176 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003178 return NULL;
3179 }
3180 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3181
3182 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3183 // match the call to the signature. Also, we might be calling through an abstract method
3184 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003185 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3186 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3187 return NULL;
3188 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3190 /* caught by static verifier */
3191 DCHECK(is_range || expected_args <= 5);
3192 if (expected_args > code_item_->outs_size_) {
3193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3194 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3195 return NULL;
3196 }
3197
3198 /*
3199 * Check the "this" argument, which must be an instance of the class that declared the method.
3200 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3201 * rigorous check here (which is okay since we have to do it at runtime).
3202 */
3203 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3205 return NULL;
3206 }
3207 if (!actual_arg_type.IsZero()) {
3208 mirror::Class* klass = res_method->GetDeclaringClass();
3209 const RegType& res_method_class =
3210 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3211 klass->CannotBeAssignedFromOtherTypes());
3212 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003213 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3214 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003215 << "' not instance of '" << res_method_class << "'";
3216 return NULL;
3217 }
3218 }
3219 /*
3220 * Process the target method's signature. This signature may or may not
3221 * have been verified, so we can't assume it's properly formed.
3222 */
3223 MethodHelper mh(res_method);
3224 const DexFile::TypeList* params = mh.GetParameterTypeList();
3225 size_t params_size = params == NULL ? 0 : params->Size();
3226 uint32_t arg[5];
3227 if (!is_range) {
3228 inst->GetArgs(arg);
3229 }
3230 size_t actual_args = 1;
3231 for (size_t param_index = 0; param_index < params_size; param_index++) {
3232 if (actual_args >= expected_args) {
3233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003234 << "'. Expected " << expected_args
3235 << " arguments, processing argument " << actual_args
3236 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003237 return NULL;
3238 }
3239 const char* descriptor =
3240 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3241 if (descriptor == NULL) {
3242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003243 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003244 return NULL;
3245 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003246 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003247 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3248 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3249 return res_method;
3250 }
3251 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3252 }
3253 if (actual_args != expected_args) {
3254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3255 << " expected " << expected_args << " arguments, found " << actual_args;
3256 return NULL;
3257 } else {
3258 return res_method;
3259 }
3260}
3261
Ian Rogers62342ec2013-06-11 10:26:37 -07003262void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003263 uint32_t type_idx;
3264 if (!is_filled) {
3265 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3266 type_idx = inst->VRegC_22c();
3267 } else if (!is_range) {
3268 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3269 type_idx = inst->VRegB_35c();
3270 } else {
3271 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3272 type_idx = inst->VRegB_3rc();
3273 }
3274 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003275 if (res_type.IsConflict()) { // bad class
3276 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003277 } else {
3278 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3279 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003281 } else if (!is_filled) {
3282 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003283 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003284 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003285 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3286 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003287 } else {
3288 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3289 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartier590fee92013-09-13 13:46:47 -07003290 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003291 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3292 uint32_t arg[5];
3293 if (!is_range) {
3294 inst->GetArgs(arg);
3295 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003296 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003297 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003298 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003299 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003300 return;
3301 }
3302 }
3303 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003304 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3305 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003306 }
3307 }
3308}
3309
Sebastien Hertz5243e912013-05-21 10:55:07 +02003310void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003311 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003312 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003313 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003316 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003317 if (array_type.IsZero()) {
3318 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3319 // instruction type. TODO: have a proper notion of bottom here.
3320 if (!is_primitive || insn_type.IsCategory1Types()) {
3321 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003322 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003324 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003325 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003326 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003327 }
jeffhaofc3144e2012-02-01 17:21:15 -08003328 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003330 } else {
3331 /* verify the class */
Mathieu Chartier590fee92013-09-13 13:46:47 -07003332 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
jeffhaofc3144e2012-02-01 17:21:15 -08003333 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003335 << " source for aget-object";
3336 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003338 << " source for category 1 aget";
3339 } else if (is_primitive && !insn_type.Equals(component_type) &&
3340 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003341 (insn_type.IsLong() && component_type.IsDouble()))) {
3342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3343 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003344 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003345 // Use knowledge of the field type which is stronger than the type inferred from the
3346 // instruction, which can't differentiate object types and ints from floats, longs from
3347 // doubles.
3348 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003349 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003350 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003351 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003352 component_type.HighHalf(&reg_types_));
3353 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 }
3355 }
3356 }
3357}
3358
Jeff Haofe1f7c82013-08-01 14:50:24 -07003359void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3360 const uint32_t vregA) {
3361 // Primitive assignability rules are weaker than regular assignability rules.
3362 bool instruction_compatible;
3363 bool value_compatible;
3364 const RegType& value_type = work_line_->GetRegisterType(vregA);
3365 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003366 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003367 value_compatible = value_type.IsIntegralTypes();
3368 } else if (target_type.IsFloat()) {
3369 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3370 value_compatible = value_type.IsFloatTypes();
3371 } else if (target_type.IsLong()) {
3372 instruction_compatible = insn_type.IsLong();
3373 value_compatible = value_type.IsLongTypes();
3374 } else if (target_type.IsDouble()) {
3375 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3376 value_compatible = value_type.IsDoubleTypes();
3377 } else {
3378 instruction_compatible = false; // reference with primitive store
3379 value_compatible = false; // unused
3380 }
3381 if (!instruction_compatible) {
3382 // This is a global failure rather than a class change failure as the instructions and
3383 // the descriptors for the type should have been consistent within the same file at
3384 // compile time.
3385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3386 << "' but expected type '" << target_type << "'";
3387 return;
3388 }
3389 if (!value_compatible) {
3390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3391 << " of type " << value_type << " but expected " << target_type << " for put";
3392 return;
3393 }
3394}
3395
Sebastien Hertz5243e912013-05-21 10:55:07 +02003396void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003398 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003401 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003402 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003403 if (array_type.IsZero()) {
3404 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3405 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003406 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003408 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003409 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003410 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003411 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003412 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003413 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003414 if (!component_type.IsReferenceTypes()) {
3415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3416 << " source for aput-object";
3417 } else {
3418 // The instruction agrees with the type of array, confirm the value to be stored does too
3419 // Note: we use the instruction type (rather than the component type) for aput-object as
3420 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003421 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003422 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 }
3424 }
3425 }
3426}
3427
Brian Carlstromea46f952013-07-30 01:26:50 -07003428mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003429 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3430 // Check access to class
3431 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003432 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003433 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3434 field_idx, dex_file_->GetFieldName(field_id),
3435 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003436 return NULL;
3437 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003438 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003439 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003440 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003441 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3442 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3443 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003445 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003446 << dex_file_->GetFieldName(field_id) << ") in "
3447 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 DCHECK(Thread::Current()->IsExceptionPending());
3449 Thread::Current()->ClearException();
3450 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003451 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3452 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003454 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 return NULL;
3456 } else if (!field->IsStatic()) {
3457 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3458 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003459 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003460 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003461}
3462
Brian Carlstromea46f952013-07-30 01:26:50 -07003463mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003464 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3465 // Check access to class
3466 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003467 if (klass_type.IsConflict()) {
3468 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3469 field_idx, dex_file_->GetFieldName(field_id),
3470 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003471 return NULL;
3472 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003473 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003474 return NULL; // Can't resolve Class so no more to do here
3475 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003476 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3477 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3478 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003479 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003480 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003481 << dex_file_->GetFieldName(field_id) << ") in "
3482 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003483 DCHECK(Thread::Current()->IsExceptionPending());
3484 Thread::Current()->ClearException();
3485 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003486 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3487 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003489 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 return NULL;
3491 } else if (field->IsStatic()) {
3492 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3493 << " to not be static";
3494 return NULL;
3495 } else if (obj_type.IsZero()) {
3496 // Cannot infer and check type, however, access will cause null pointer exception
3497 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003498 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003499 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003500 const RegType& field_klass =
3501 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003502 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003503 if (obj_type.IsUninitializedTypes() &&
3504 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3505 !field_klass.Equals(GetDeclaringClass()))) {
3506 // Field accesses through uninitialized references are only allowable for constructors where
3507 // the field is declared in this class
3508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003509 << " of a not fully initialized object within the context"
3510 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003511 return NULL;
3512 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3513 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3514 // of C1. For resolution to occur the declared class of the field must be compatible with
3515 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3516 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3517 << " from object of type " << obj_type;
3518 return NULL;
3519 } else {
3520 return field;
3521 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003522 }
3523}
3524
Sebastien Hertz5243e912013-05-21 10:55:07 +02003525void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3526 bool is_primitive, bool is_static) {
3527 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003528 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003529 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003530 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003531 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003532 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003533 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003534 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003535 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003536 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003537 FieldHelper fh(field);
3538 mirror::Class* field_type_class = fh.GetType(false);
3539 if (field_type_class != nullptr) {
3540 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3541 field_type_class->CannotBeAssignedFromOtherTypes());
3542 }
Ian Rogers0d604842012-04-16 14:50:24 -07003543 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003544 if (field_type == nullptr) {
3545 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3546 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003547 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003548 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003549 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003550 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003551 if (field_type->Equals(insn_type) ||
3552 (field_type->IsFloat() && insn_type.IsInteger()) ||
3553 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003554 // expected that read is of the correct primitive type or that int reads are reading
3555 // floats or long reads are reading doubles
3556 } else {
3557 // This is a global failure rather than a class change failure as the instructions and
3558 // the descriptors for the type should have been consistent within the same file at
3559 // compile time
3560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3561 << " to be of type '" << insn_type
3562 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003563 return;
3564 }
3565 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003566 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003567 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3568 << " to be compatible with type '" << insn_type
3569 << "' but found type '" << field_type
3570 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003571 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003572 return;
3573 }
3574 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003575 if (!field_type->IsLowHalf()) {
3576 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003577 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003578 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003579 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003580}
3581
Sebastien Hertz5243e912013-05-21 10:55:07 +02003582void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3583 bool is_primitive, bool is_static) {
3584 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003585 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003586 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003587 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003588 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003589 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003590 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003591 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003592 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003593 if (field != NULL) {
3594 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3595 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3596 << " from other class " << GetDeclaringClass();
3597 return;
3598 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003599 FieldHelper fh(field);
3600 mirror::Class* field_type_class = fh.GetType(false);
3601 if (field_type_class != nullptr) {
3602 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3603 field_type_class->CannotBeAssignedFromOtherTypes());
3604 }
3605 }
3606 if (field_type == nullptr) {
3607 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3608 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003609 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003610 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003611 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003612 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003613 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003614 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003615 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003616 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3617 << " to be compatible with type '" << insn_type
3618 << "' but found type '" << field_type
3619 << "' in put-object";
3620 return;
3621 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003622 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 }
3624}
3625
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003626// Look for an instance field with this offset.
3627// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003628static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003629 uint32_t field_offset)
3630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003631 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003632 if (instance_fields != NULL) {
3633 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003634 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003635 if (field->GetOffset().Uint32Value() == field_offset) {
3636 return field;
3637 }
3638 }
3639 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003640 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003641 if (klass->GetSuperClass() != NULL) {
3642 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3643 } else {
3644 return NULL;
3645 }
3646}
3647
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003648// Returns the access field of a quick field access (iget/iput-quick) or NULL
3649// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003650mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003651 RegisterLine* reg_line) {
3652 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3653 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3654 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3655 inst->Opcode() == Instruction::IPUT_QUICK ||
3656 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3657 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3658 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003659 mirror::Class* object_class = NULL;
3660 if (!object_type.IsUnresolvedTypes()) {
3661 object_class = object_type.GetClass();
3662 } else {
3663 // We need to resolve the class from its descriptor.
3664 const std::string& descriptor(object_type.GetDescriptor());
3665 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003666 Thread* self = Thread::Current();
3667 object_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003668 if (object_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003669 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003670 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003671 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3672 object_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003673 }
3674 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003675 if (object_class == NULL) {
3676 // Failed to get the Class* from reg type.
3677 LOG(WARNING) << "Failed to get Class* from " << object_type;
3678 return NULL;
3679 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003680 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003681 return FindInstanceFieldWithOffset(object_class, field_offset);
3682}
3683
3684void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3685 bool is_primitive) {
3686 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003687 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003688 if (field == NULL) {
3689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3690 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003691 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003692 FieldHelper fh(field);
3693 mirror::Class* field_type_class = fh.GetType(false);
3694 const RegType* field_type;
3695 if (field_type_class != nullptr) {
3696 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3697 field_type_class->CannotBeAssignedFromOtherTypes());
3698 } else {
3699 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3700 fh.GetTypeDescriptor(), false);
3701 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003702 const uint32_t vregA = inst->VRegA_22c();
3703 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003704 if (field_type->Equals(insn_type) ||
3705 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3706 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003707 // expected that read is of the correct primitive type or that int reads are reading
3708 // floats or long reads are reading doubles
3709 } else {
3710 // This is a global failure rather than a class change failure as the instructions and
3711 // the descriptors for the type should have been consistent within the same file at
3712 // compile time
3713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003714 << " to be of type '" << insn_type
3715 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003716 return;
3717 }
3718 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003719 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003720 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003721 << " to be compatible with type '" << insn_type
3722 << "' but found type '" << field_type
3723 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003724 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3725 return;
3726 }
3727 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003728 if (!field_type->IsLowHalf()) {
3729 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003730 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003731 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003732 }
3733}
3734
3735void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3736 bool is_primitive) {
3737 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003738 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003739 if (field == NULL) {
3740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3741 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003742 }
3743 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3744 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3745 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3746 if (field != NULL) {
3747 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3748 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003749 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003750 return;
3751 }
3752 }
3753 const uint32_t vregA = inst->VRegA_22c();
3754 if (is_primitive) {
3755 // Primitive field assignability rules are weaker than regular assignability rules
3756 bool instruction_compatible;
3757 bool value_compatible;
3758 const RegType& value_type = work_line_->GetRegisterType(vregA);
3759 if (field_type.IsIntegralTypes()) {
3760 instruction_compatible = insn_type.IsIntegralTypes();
3761 value_compatible = value_type.IsIntegralTypes();
3762 } else if (field_type.IsFloat()) {
3763 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3764 value_compatible = value_type.IsFloatTypes();
3765 } else if (field_type.IsLong()) {
3766 instruction_compatible = insn_type.IsLong();
3767 value_compatible = value_type.IsLongTypes();
3768 } else if (field_type.IsDouble()) {
3769 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3770 value_compatible = value_type.IsDoubleTypes();
3771 } else {
3772 instruction_compatible = false; // reference field with primitive store
3773 value_compatible = false; // unused
3774 }
3775 if (!instruction_compatible) {
3776 // This is a global failure rather than a class change failure as the instructions and
3777 // the descriptors for the type should have been consistent within the same file at
3778 // compile time
3779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003780 << " to be of type '" << insn_type
3781 << "' but found type '" << field_type
3782 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003783 return;
3784 }
3785 if (!value_compatible) {
3786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3787 << " of type " << value_type
3788 << " but expected " << field_type
3789 << " for store to " << PrettyField(field) << " in put";
3790 return;
3791 }
3792 } else {
3793 if (!insn_type.IsAssignableFrom(field_type)) {
3794 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003795 << " to be compatible with type '" << insn_type
3796 << "' but found type '" << field_type
3797 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003798 return;
3799 }
3800 work_line_->VerifyRegisterType(vregA, field_type);
3801 }
3802}
3803
Ian Rogers776ac1f2012-04-13 23:36:36 -07003804bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003807 return false;
3808 }
3809 return true;
3810}
3811
Ian Rogers776ac1f2012-04-13 23:36:36 -07003812bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003813 bool changed = true;
3814 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3815 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003816 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003817 * We haven't processed this instruction before, and we haven't touched the registers here, so
3818 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3819 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003820 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003821 if (!insn_flags_[next_insn].IsReturn()) {
3822 target_line->CopyFromLine(merge_line);
3823 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003824 // Verify that the monitor stack is empty on return.
3825 if (!merge_line->VerifyMonitorStackEmpty()) {
3826 return false;
3827 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003828 // For returns we only care about the operand to the return, all other registers are dead.
3829 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3830 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3831 Instruction::Code opcode = ret_inst->Opcode();
3832 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3833 target_line->MarkAllRegistersAsConflicts();
3834 } else {
3835 target_line->CopyFromLine(merge_line);
3836 if (opcode == Instruction::RETURN_WIDE) {
3837 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3838 } else {
3839 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3840 }
3841 }
3842 }
jeffhaobdb76512011-09-07 11:43:16 -07003843 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003844 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003845 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003846 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003847 if (gDebugVerify) {
3848 copy->CopyFromLine(target_line);
3849 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003851 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003853 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003854 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003855 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003856 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3857 << *copy.get() << " MERGE\n"
3858 << *merge_line << " ==\n"
3859 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003860 }
3861 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 if (changed) {
3863 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003864 }
3865 return true;
3866}
3867
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003868InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003869 return &insn_flags_[work_insn_idx_];
3870}
3871
Ian Rogersad0b3a32012-04-16 14:50:24 -07003872const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003873 if (return_type_ == nullptr) {
3874 if (mirror_method_ != NULL) {
3875 MethodHelper mh(mirror_method_);
3876 mirror::Class* return_type_class = mh.GetReturnType();
3877 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003878 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3879 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003880 } else {
3881 Thread* self = Thread::Current();
3882 DCHECK(self->IsExceptionPending());
3883 self->ClearException();
3884 }
3885 }
3886 if (return_type_ == nullptr) {
3887 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3888 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3889 uint16_t return_type_idx = proto_id.return_type_idx_;
3890 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003891 return_type_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003892 }
3893 }
3894 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003895}
3896
3897const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003898 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003899 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003900 const char* descriptor
3901 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003902 if (mirror_method_ != NULL) {
3903 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003904 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3905 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003906 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003907 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003908 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003909 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003910 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003911}
3912
Ian Rogers776ac1f2012-04-13 23:36:36 -07003913void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003914 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003915 size_t local_gc_points = 0;
3916 size_t max_insn = 0;
3917 size_t max_ref_reg = -1;
3918 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003919 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003920 local_gc_points++;
3921 max_insn = i;
3922 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003923 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003924 }
3925 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003926 *gc_points = local_gc_points;
3927 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3928 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003929 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003930 i++;
3931 }
3932 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003933}
3934
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003935MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3936 /*
3937 * Walks over the method code and adds any cast instructions in which
3938 * the type cast is implicit to a set, which is used in the code generation
3939 * to elide these casts.
3940 */
3941 if (!failure_messages_.empty()) {
3942 return NULL;
3943 }
3944 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003945 const Instruction* inst = Instruction::At(code_item_->insns_);
3946 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003947 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003948
3949 for (; inst < end; inst = inst->Next()) {
Ian Rogersa9a82542013-10-04 11:17:26 -07003950 Instruction::Code code = inst->Opcode();
3951 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
3952 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
3953 RegisterLine* line = reg_table_.GetLine(dex_pc);
3954 bool is_safe_cast = false;
3955 if (code == Instruction::CHECK_CAST) {
3956 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3957 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
3958 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
3959 } else {
3960 const RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
3961 // We only know its safe to assign to an array if the array type is precise. For example,
3962 // an Object[] can have any type of object stored in it, but it may also be assigned a
3963 // String[] in which case the stores need to be of Strings.
3964 if (array_type.IsPreciseReference()) {
3965 const RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003966 const RegType& component_type(reg_types_.GetComponentType(array_type,
3967 class_loader_->get()));
Ian Rogersa9a82542013-10-04 11:17:26 -07003968 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
3969 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003970 }
Ian Rogersa9a82542013-10-04 11:17:26 -07003971 if (is_safe_cast) {
3972 if (mscs.get() == NULL) {
3973 mscs.reset(new MethodSafeCastSet());
3974 }
3975 mscs->insert(dex_pc);
3976 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003977 }
3978 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003979 return mscs.release();
3980}
3981
Ian Rogersd0583802013-06-01 10:51:46 -07003982MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003983 // It is risky to rely on reg_types for sharpening in cases of soft
3984 // verification, we might end up sharpening to a wrong implementation. Just abort.
3985 if (!failure_messages_.empty()) {
3986 return NULL;
3987 }
3988
Ian Rogersd0583802013-06-01 10:51:46 -07003989 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003990 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003991 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003992 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003993
Ian Rogersd0583802013-06-01 10:51:46 -07003994 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003995 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3996 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3997 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3998 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3999
Brian Carlstromdf629502013-07-17 22:39:56 -07004000 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004001 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004002 }
Ian Rogersd0583802013-06-01 10:51:46 -07004003 // Get reg type for register holding the reference to the object that will be dispatched upon.
4004 uint32_t dex_pc = inst->GetDexPc(insns);
4005 RegisterLine* line = reg_table_.GetLine(dex_pc);
4006 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
4007 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
4008 const RegType&
4009 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004010
Ian Rogersd0583802013-06-01 10:51:46 -07004011 if (!reg_type.HasClass()) {
4012 // We will compute devirtualization information only when we know the Class of the reg type.
4013 continue;
4014 }
4015 mirror::Class* reg_class = reg_type.GetClass();
4016 if (reg_class->IsInterface()) {
4017 // We can't devirtualize when the known type of the register is an interface.
4018 continue;
4019 }
4020 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
4021 // We can't devirtualize abstract classes except on arrays of abstract classes.
4022 continue;
4023 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004024 mirror::ArtMethod* abstract_method = (*dex_cache_)->GetResolvedMethod(
4025 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07004026 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004027 // If the method is not found in the cache this means that it was never found
4028 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
4029 continue;
4030 }
4031 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07004032 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004033 if (is_interface) {
4034 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
4035 }
4036 if (is_virtual) {
4037 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
4038 }
Ian Rogersd0583802013-06-01 10:51:46 -07004039 if (concrete_method == NULL || concrete_method->IsAbstract()) {
4040 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004041 continue;
4042 }
Ian Rogersd0583802013-06-01 10:51:46 -07004043 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
4044 concrete_method->GetDeclaringClass()->IsFinal()) {
4045 // If we knew exactly the class being dispatched upon, or if the target method cannot be
4046 // overridden record the target to be used in the compiler driver.
4047 if (pc_to_concrete_method_map.get() == NULL) {
4048 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
4049 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07004050 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07004051 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
4052 concrete_method->GetDexMethodIndex());
4053 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
4054 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004055 }
Ian Rogersd0583802013-06-01 10:51:46 -07004056 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004057}
4058
Vladimir Marko8171fc32013-11-26 17:05:58 +00004059const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07004060 size_t num_entries, ref_bitmap_bits, pc_bits;
4061 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
4062 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08004063 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004064 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004066 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07004067 return NULL;
4068 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004069 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
4070 // There are 2 bytes to encode the number of entries
4071 if (num_entries >= 65536) {
4072 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004074 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004075 return NULL;
4076 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004077 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004078 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004079 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004080 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004081 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004082 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004083 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004084 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004085 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004088 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4089 return NULL;
4090 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004091 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004092 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004093 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004095 return NULL;
4096 }
Vladimir Marko8171fc32013-11-26 17:05:58 +00004097 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004098 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004099 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4100 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004101 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004102 table->push_back(num_entries & 0xFF);
4103 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004104 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004105 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004106 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004107 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004108 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004109 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004110 }
4111 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004112 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004113 }
4114 }
Vladimir Marko8171fc32013-11-26 17:05:58 +00004115 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004116 return table;
4117}
jeffhaoa0a764a2011-09-16 10:43:38 -07004118
Vladimir Marko8171fc32013-11-26 17:05:58 +00004119void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004120 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4121 // that the table data is well formed and all references are marked (or not) in the bitmap
Vladimir Marko8171fc32013-11-26 17:05:58 +00004122 DexPcToReferenceMap map(&data[0]);
4123 DCHECK_EQ(data.size(), map.RawSize());
Ian Rogersd81871c2011-10-03 13:57:23 -07004124 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004125 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004126 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004127 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004128 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004129 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004130 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4131 map_index++;
4132 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004133 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004134 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004135 CHECK_LT(j / 8, map.RegWidth());
4136 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4137 } else if ((j / 8) < map.RegWidth()) {
4138 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4139 } else {
4140 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4141 }
4142 }
4143 } else {
4144 CHECK(reg_bitmap == NULL);
4145 }
4146 }
4147}
jeffhaoa0a764a2011-09-16 10:43:38 -07004148
Vladimir Markoc255e972013-11-19 11:21:24 +00004149void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004150 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004151 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004152 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004153 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4154 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004155 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004156 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004157 }
Vladimir Markoc255e972013-11-19 11:21:24 +00004158 dex_gc_maps_->Put(ref, gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004159 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004160 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004161}
4162
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004163
Brian Carlstrom51c24672013-07-11 16:00:56 -07004164void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004165 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004166 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004167 SafeCastMap::iterator it = safecast_map_->find(ref);
4168 if (it != safecast_map_->end()) {
4169 delete it->second;
4170 safecast_map_->erase(it);
4171 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004172 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004173 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004174}
4175
Brian Carlstrom51c24672013-07-11 16:00:56 -07004176bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004177 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004178 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004179 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4180 if (it == safecast_map_->end()) {
4181 return false;
4182 }
4183
4184 // Look up the cast address in the set of safe casts
4185 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4186 return cast_it != it->second->end();
4187}
4188
Brian Carlstrom51c24672013-07-11 16:00:56 -07004189const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004190 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004191 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4192 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004193 CHECK(it != dex_gc_maps_->end())
4194 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4195 CHECK(it->second != NULL);
4196 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004197}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004198
Brian Carlstrom51c24672013-07-11 16:00:56 -07004199void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004200 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004201 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004202 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004203 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4204 if (it != devirt_maps_->end()) {
4205 delete it->second;
4206 devirt_maps_->erase(it);
4207 }
4208
4209 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004210 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004211}
4212
Brian Carlstrom51c24672013-07-11 16:00:56 -07004213const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004214 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004215 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004216 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004217 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4218 if (it == devirt_maps_->end()) {
4219 return NULL;
4220 }
4221
4222 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004223 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4224 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004225 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004226 return &(pc_to_concrete_method->second);
4227 } else {
4228 return NULL;
4229 }
4230}
4231
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004232std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4233 RegisterLine* line = reg_table_.GetLine(dex_pc);
4234 std::vector<int32_t> result;
4235 for (size_t i = 0; i < line->NumRegs(); ++i) {
4236 const RegType& type = line->GetRegisterType(i);
4237 if (type.IsConstant()) {
4238 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4239 result.push_back(type.ConstantValue());
4240 } else if (type.IsConstantLo()) {
4241 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4242 result.push_back(type.ConstantValueLo());
4243 } else if (type.IsConstantHi()) {
4244 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4245 result.push_back(type.ConstantValueHi());
4246 } else if (type.IsIntegralTypes()) {
4247 result.push_back(kIntVReg);
4248 result.push_back(0);
4249 } else if (type.IsFloat()) {
4250 result.push_back(kFloatVReg);
4251 result.push_back(0);
4252 } else if (type.IsLong()) {
4253 result.push_back(kLongLoVReg);
4254 result.push_back(0);
4255 result.push_back(kLongHiVReg);
4256 result.push_back(0);
4257 ++i;
4258 } else if (type.IsDouble()) {
4259 result.push_back(kDoubleLoVReg);
4260 result.push_back(0);
4261 result.push_back(kDoubleHiVReg);
4262 result.push_back(0);
4263 ++i;
4264 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4265 result.push_back(kUndefined);
4266 result.push_back(0);
4267 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004268 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004269 result.push_back(kReferenceVReg);
4270 result.push_back(0);
4271 }
4272 }
4273 return result;
4274}
4275
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004276bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004277 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004278#ifdef ART_SEA_IR_MODE
4279 bool use_sea = Runtime::Current()->IsSeaIRMode();
4280 use_sea = use_sea && (std::string::npos != PrettyMethod(
4281 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4282 if (use_sea) return true;
4283#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004284 // Don't compile class initializers, ever.
4285 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4286 return false;
4287 }
buzbeea024a062013-07-31 10:47:37 -07004288 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004289}
4290
Ian Rogers637c65b2013-05-31 11:46:00 -07004291ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004292MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004293
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004294ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004295MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4296
Ian Rogers637c65b2013-05-31 11:46:00 -07004297ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004298MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4299
Ian Rogersb8a0b942013-08-20 18:09:52 -07004300ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004301MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4302
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004303void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004304 if (Runtime::Current()->IsCompiler()) {
4305 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4306 Thread* self = Thread::Current();
4307 {
4308 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4309 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4310 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004311
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004312 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004313 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004314 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004315 safecast_map_ = new MethodVerifier::SafeCastMap();
4316 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004317
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004318 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004319
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004320 {
4321 WriterMutexLock mu(self, *devirt_maps_lock_);
4322 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4323 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004324
Ian Rogersb8a0b942013-08-20 18:09:52 -07004325 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004326 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004327 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004328 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4329 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004330 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004331 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004332}
4333
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004334void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004335 if (Runtime::Current()->IsCompiler()) {
4336 Thread* self = Thread::Current();
4337 {
4338 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4339 STLDeleteValues(dex_gc_maps_);
4340 delete dex_gc_maps_;
4341 dex_gc_maps_ = NULL;
4342 }
4343 delete dex_gc_maps_lock_;
4344 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004345
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004346 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004347 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004348 STLDeleteValues(safecast_map_);
4349 delete safecast_map_;
4350 safecast_map_ = NULL;
4351 }
4352 delete safecast_map_lock_;
4353 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004354
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004355 {
4356 WriterMutexLock mu(self, *devirt_maps_lock_);
4357 STLDeleteValues(devirt_maps_);
4358 delete devirt_maps_;
4359 devirt_maps_ = NULL;
4360 }
4361 delete devirt_maps_lock_;
4362 devirt_maps_lock_ = NULL;
4363
4364 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004365 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004366 delete rejected_classes_;
4367 rejected_classes_ = NULL;
4368 }
4369 delete rejected_classes_lock_;
4370 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004371 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004372 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004373}
jeffhaod1224c72012-02-29 13:43:08 -08004374
Brian Carlstrom51c24672013-07-11 16:00:56 -07004375void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004376 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004377 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004378 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004379 rejected_classes_->insert(ref);
4380 }
jeffhaod1224c72012-02-29 13:43:08 -08004381 CHECK(IsClassRejected(ref));
4382}
4383
Brian Carlstrom51c24672013-07-11 16:00:56 -07004384bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004385 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004386 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004387 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004388}
4389
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004390void MethodVerifier::VisitRoots(RootVisitor* visitor, void* arg) {
4391 reg_types_.VisitRoots(visitor, arg);
4392}
4393
Ian Rogersd81871c2011-10-03 13:57:23 -07004394} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004395} // namespace art