1 /*
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
19 #include <android-base/logging.h>
20
21 #include "base/mutex-inl.h"
22 #include "base/stl_util.h"
23 #include "driver/compiler_options.h"
24 #include "runtime.h"
25 #include "thread-current-inl.h"
26 #include "thread.h"
27 #include "utils/atomic_dex_ref_map-inl.h"
28 #include "verified_method.h"
29 #include "verifier/method_verifier-inl.h"
30
31 namespace art {
32
VerificationResults(const CompilerOptions * compiler_options)33 VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
34 : compiler_options_(compiler_options),
35 verified_methods_lock_("compiler verified methods lock"),
36 rejected_classes_lock_("compiler rejected classes lock") {}
37
~VerificationResults()38 VerificationResults::~VerificationResults() {
39 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
40 STLDeleteValues(&verified_methods_);
41 atomic_verified_methods_.Visit([](const DexFileReference& ref ATTRIBUTE_UNUSED,
42 const VerifiedMethod* method) {
43 delete method;
44 });
45 }
46
ProcessVerifiedMethod(verifier::MethodVerifier * method_verifier)47 void VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
48 DCHECK(method_verifier != nullptr);
49 MethodReference ref = method_verifier->GetMethodReference();
50 std::unique_ptr<const VerifiedMethod> verified_method(VerifiedMethod::Create(method_verifier));
51 if (verified_method == nullptr) {
52 // We'll punt this later.
53 return;
54 }
55 AtomicMap::InsertResult result = atomic_verified_methods_.Insert(ref,
56 /*expected*/ nullptr,
57 verified_method.get());
58 const VerifiedMethod* existing = nullptr;
59 bool inserted;
60 if (result != AtomicMap::kInsertResultInvalidDexFile) {
61 inserted = (result == AtomicMap::kInsertResultSuccess);
62 if (!inserted) {
63 // Rare case.
64 CHECK(atomic_verified_methods_.Get(ref, &existing));
65 CHECK_NE(verified_method.get(), existing);
66 }
67 } else {
68 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
69 auto it = verified_methods_.find(ref);
70 inserted = it == verified_methods_.end();
71 if (inserted) {
72 verified_methods_.Put(ref, verified_method.get());
73 DCHECK(verified_methods_.find(ref) != verified_methods_.end());
74 } else {
75 existing = it->second;
76 }
77 }
78 if (inserted) {
79 // Successfully added, release the unique_ptr since we no longer have ownership.
80 DCHECK_EQ(GetVerifiedMethod(ref), verified_method.get());
81 verified_method.release(); // NOLINT b/117926937
82 } else {
83 // TODO: Investigate why are we doing the work again for this method and try to avoid it.
84 LOG(WARNING) << "Method processed more than once: " << ref.PrettyMethod();
85 if (!Runtime::Current()->UseJitCompilation()) {
86 if (kIsDebugBuild) {
87 auto ex_set = existing->GetSafeCastSet();
88 auto ve_set = verified_method->GetSafeCastSet();
89 CHECK_EQ(ex_set == nullptr, ve_set == nullptr);
90 CHECK((ex_set == nullptr) || (ex_set->size() == ve_set->size()));
91 }
92 }
93 // Let the unique_ptr delete the new verified method since there was already an existing one
94 // registered. It is unsafe to replace the existing one since the JIT may be using it to
95 // generate a native GC map.
96 }
97 }
98
GetVerifiedMethod(MethodReference ref) const99 const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) const {
100 const VerifiedMethod* ret = nullptr;
101 if (atomic_verified_methods_.Get(ref, &ret)) {
102 return ret;
103 }
104 ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
105 auto it = verified_methods_.find(ref);
106 return (it != verified_methods_.end()) ? it->second : nullptr;
107 }
108
CreateVerifiedMethodFor(MethodReference ref)109 void VerificationResults::CreateVerifiedMethodFor(MethodReference ref) {
110 // This method should only be called for classes verified at compile time,
111 // which have no verifier error, nor has methods that we know will throw
112 // at runtime.
113 std::unique_ptr<VerifiedMethod> verified_method = std::make_unique<VerifiedMethod>(
114 /* encountered_error_types= */ 0, /* has_runtime_throw= */ false);
115 if (atomic_verified_methods_.Insert(ref,
116 /*expected*/ nullptr,
117 verified_method.get()) ==
118 AtomicMap::InsertResult::kInsertResultSuccess) {
119 verified_method.release(); // NOLINT b/117926937
120 }
121 }
122
AddRejectedClass(ClassReference ref)123 void VerificationResults::AddRejectedClass(ClassReference ref) {
124 {
125 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
126 rejected_classes_.insert(ref);
127 }
128 DCHECK(IsClassRejected(ref));
129 }
130
IsClassRejected(ClassReference ref) const131 bool VerificationResults::IsClassRejected(ClassReference ref) const {
132 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
133 return (rejected_classes_.find(ref) != rejected_classes_.end());
134 }
135
IsCandidateForCompilation(MethodReference &,const uint32_t access_flags) const136 bool VerificationResults::IsCandidateForCompilation(MethodReference&,
137 const uint32_t access_flags) const {
138 if (!compiler_options_->IsAotCompilationEnabled()) {
139 return false;
140 }
141 // Don't compile class initializers unless kEverything.
142 if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) &&
143 ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
144 return false;
145 }
146 return true;
147 }
148
AddDexFile(const DexFile * dex_file)149 void VerificationResults::AddDexFile(const DexFile* dex_file) {
150 atomic_verified_methods_.AddDexFile(dex_file);
151 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
152 // There can be some verified methods that are already registered for the dex_file since we set
153 // up well known classes earlier. Remove these and put them in the array so that we don't
154 // accidentally miss seeing them.
155 for (auto it = verified_methods_.begin(); it != verified_methods_.end(); ) {
156 MethodReference ref = it->first;
157 if (ref.dex_file == dex_file) {
158 CHECK(atomic_verified_methods_.Insert(ref, nullptr, it->second) ==
159 AtomicMap::kInsertResultSuccess);
160 it = verified_methods_.erase(it);
161 } else {
162 ++it;
163 }
164 }
165 }
166
167 } // namespace art
168