blob: 1e0b94de8141f10dd2d8e873d4f57dcdb5b7a9c7 [file] [log] [blame]
Vladimir Markoc7f83202014-01-24 17:55:18 +00001/*
2 * Copyright (C) 2013 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 */
16
17#include "verification_results.h"
18
Andreas Gampe57943812017-12-06 21:39:13 -080019#include <android-base/logging.h>
20
Vladimir Markoc7f83202014-01-24 17:55:18 +000021#include "base/mutex-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070022#include "base/stl_util.h"
Brian Carlstrom6449c622014-02-10 23:48:36 -080023#include "driver/compiler_driver.h"
24#include "driver/compiler_options.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070025#include "runtime.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070026#include "thread-current-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "thread.h"
Mathieu Chartier93764b82017-07-17 14:51:53 -070028#include "utils/atomic_dex_ref_map-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000029#include "verified_method.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "verifier/method_verifier-inl.h"
31
32namespace art {
33
Brian Carlstrom6449c622014-02-10 23:48:36 -080034VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
Ian Rogers1ff3c982014-08-12 02:30:58 -070035 : compiler_options_(compiler_options),
36 verified_methods_lock_("compiler verified methods lock"),
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080037 rejected_classes_lock_("compiler rejected classes lock") {}
Vladimir Markoc7f83202014-01-24 17:55:18 +000038
39VerificationResults::~VerificationResults() {
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080040 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080041 STLDeleteValues(&verified_methods_);
Mathieu Chartier93764b82017-07-17 14:51:53 -070042 atomic_verified_methods_.Visit([](const DexFileReference& ref ATTRIBUTE_UNUSED,
Mathieu Chartier9df89312016-11-23 13:28:16 -080043 const VerifiedMethod* method) {
44 delete method;
45 });
Vladimir Markoc7f83202014-01-24 17:55:18 +000046}
47
Andreas Gampe53e32d12015-12-09 21:03:23 -080048void VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 DCHECK(method_verifier != nullptr);
Vladimir Markoc7f83202014-01-24 17:55:18 +000050 MethodReference ref = method_verifier->GetMethodReference();
Nicolas Geoffrayc51c7ca2016-11-25 15:46:48 +000051 std::unique_ptr<const VerifiedMethod> verified_method(VerifiedMethod::Create(method_verifier));
Vladimir Markoc7f83202014-01-24 17:55:18 +000052 if (verified_method == nullptr) {
Andreas Gampe53e32d12015-12-09 21:03:23 -080053 // We'll punt this later.
54 return;
Vladimir Markoc7f83202014-01-24 17:55:18 +000055 }
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070056 AtomicMap::InsertResult result = atomic_verified_methods_.Insert(ref,
57 /*expected*/ nullptr,
58 verified_method.get());
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080059 const VerifiedMethod* existing = nullptr;
Mathieu Chartier9df89312016-11-23 13:28:16 -080060 bool inserted;
61 if (result != AtomicMap::kInsertResultInvalidDexFile) {
62 inserted = (result == AtomicMap::kInsertResultSuccess);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080063 if (!inserted) {
Mathieu Chartier9df89312016-11-23 13:28:16 -080064 // Rare case.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070065 CHECK(atomic_verified_methods_.Get(ref, &existing));
Mathieu Chartier9df89312016-11-23 13:28:16 -080066 CHECK_NE(verified_method.get(), existing);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080067 }
68 } else {
69 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
70 auto it = verified_methods_.find(ref);
71 inserted = it == verified_methods_.end();
72 if (inserted) {
73 verified_methods_.Put(ref, verified_method.get());
74 DCHECK(verified_methods_.find(ref) != verified_methods_.end());
75 } else {
76 existing = it->second;
77 }
78 }
79 if (inserted) {
80 // Successfully added, release the unique_ptr since we no longer have ownership.
81 DCHECK_EQ(GetVerifiedMethod(ref), verified_method.get());
82 verified_method.release();
83 } else {
Vladimir Markoc7f83202014-01-24 17:55:18 +000084 // TODO: Investigate why are we doing the work again for this method and try to avoid it.
David Sehr709b0702016-10-13 09:12:37 -070085 LOG(WARNING) << "Method processed more than once: " << ref.PrettyMethod();
Calin Juravleffc87072016-04-20 14:22:09 +010086 if (!Runtime::Current()->UseJitCompilation()) {
Andreas Gampe26699c62017-05-12 08:19:28 -070087 if (kIsDebugBuild) {
88 auto ex_set = existing->GetSafeCastSet();
89 auto ve_set = verified_method->GetSafeCastSet();
90 CHECK_EQ(ex_set == nullptr, ve_set == nullptr);
91 CHECK((ex_set == nullptr) || (ex_set->size() == ve_set->size()));
92 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080093 }
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080094 // Let the unique_ptr delete the new verified method since there was already an existing one
95 // registered. It is unsafe to replace the existing one since the JIT may be using it to
96 // generate a native GC map.
Vladimir Markoc7f83202014-01-24 17:55:18 +000097 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000098}
99
100const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800101 const VerifiedMethod* ret = nullptr;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700102 if (atomic_verified_methods_.Get(ref, &ret)) {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800103 return ret;
Mathieu Chartierfc2dd612016-11-21 15:05:23 -0800104 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000105 ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
106 auto it = verified_methods_.find(ref);
107 return (it != verified_methods_.end()) ? it->second : nullptr;
108}
109
Nicolas Geoffray51c17fa2016-11-25 15:56:12 +0000110void VerificationResults::CreateVerifiedMethodFor(MethodReference ref) {
111 // This method should only be called for classes verified at compile time,
112 // which have no verifier error, nor has methods that we know will throw
113 // at runtime.
Andreas Gampef45d61c2017-06-07 10:29:33 -0700114 std::unique_ptr<VerifiedMethod> verified_method = std::make_unique<VerifiedMethod>(
115 /* encountered_error_types */ 0, /* has_runtime_throw */ false);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700116 if (atomic_verified_methods_.Insert(ref,
Mathieu Chartier93764b82017-07-17 14:51:53 -0700117 /*expected*/ nullptr,
118 verified_method.get()) ==
Andreas Gampef45d61c2017-06-07 10:29:33 -0700119 AtomicMap::InsertResult::kInsertResultSuccess) {
120 verified_method.release();
121 }
Nicolas Geoffray51c17fa2016-11-25 15:56:12 +0000122}
123
Vladimir Markoc7f83202014-01-24 17:55:18 +0000124void VerificationResults::AddRejectedClass(ClassReference ref) {
125 {
126 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
127 rejected_classes_.insert(ref);
128 }
129 DCHECK(IsClassRejected(ref));
130}
131
132bool VerificationResults::IsClassRejected(ClassReference ref) {
133 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
134 return (rejected_classes_.find(ref) != rejected_classes_.end());
135}
136
Elliott Hughes956af0f2014-12-11 14:34:28 -0800137bool VerificationResults::IsCandidateForCompilation(MethodReference&,
Vladimir Markoc7f83202014-01-24 17:55:18 +0000138 const uint32_t access_flags) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100139 if (!compiler_options_->IsAotCompilationEnabled()) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700140 return false;
141 }
buzbeec8332992015-06-25 15:53:45 -0700142 // Don't compile class initializers unless kEverything.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000143 if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) &&
buzbeec8332992015-06-25 15:53:45 -0700144 ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000145 return false;
146 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800147 return true;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000148}
149
Mathieu Chartier9df89312016-11-23 13:28:16 -0800150void VerificationResults::AddDexFile(const DexFile* dex_file) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700151 atomic_verified_methods_.AddDexFile(dex_file);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -0800152 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
153 // There can be some verified methods that are already registered for the dex_file since we set
154 // up well known classes earlier. Remove these and put them in the array so that we don't
155 // accidentally miss seeing them.
156 for (auto it = verified_methods_.begin(); it != verified_methods_.end(); ) {
157 MethodReference ref = it->first;
158 if (ref.dex_file == dex_file) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700159 CHECK(atomic_verified_methods_.Insert(ref, nullptr, it->second) ==
Mathieu Chartier9df89312016-11-23 13:28:16 -0800160 AtomicMap::kInsertResultSuccess);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -0800161 it = verified_methods_.erase(it);
162 } else {
163 ++it;
164 }
165 }
Mathieu Chartierfc2dd612016-11-21 15:05:23 -0800166}
167
Vladimir Markoc7f83202014-01-24 17:55:18 +0000168} // namespace art