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     // Let the unique_ptr delete the new verified method since there was already an existing one
86     // registered. It is unsafe to replace the existing one since the JIT may be using it to
87     // generate a native GC map.
88   }
89 }
90 
GetVerifiedMethod(MethodReference ref) const91 const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) const {
92   const VerifiedMethod* ret = nullptr;
93   if (atomic_verified_methods_.Get(ref, &ret)) {
94     return ret;
95   }
96   ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
97   auto it = verified_methods_.find(ref);
98   return (it != verified_methods_.end()) ? it->second : nullptr;
99 }
100 
CreateVerifiedMethodFor(MethodReference ref)101 void VerificationResults::CreateVerifiedMethodFor(MethodReference ref) {
102   // This method should only be called for classes verified at compile time,
103   // which have no verifier error, nor has methods that we know will throw
104   // at runtime.
105   std::unique_ptr<VerifiedMethod> verified_method = std::make_unique<VerifiedMethod>(
106       /* encountered_error_types= */ 0, /* has_runtime_throw= */ false);
107   if (atomic_verified_methods_.Insert(ref,
108                                       /*expected*/ nullptr,
109                                       verified_method.get()) ==
110           AtomicMap::InsertResult::kInsertResultSuccess) {
111     verified_method.release();  // NOLINT b/117926937
112   }
113 }
114 
AddRejectedClass(ClassReference ref)115 void VerificationResults::AddRejectedClass(ClassReference ref) {
116   {
117     WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
118     rejected_classes_.insert(ref);
119   }
120   DCHECK(IsClassRejected(ref));
121 }
122 
IsClassRejected(ClassReference ref) const123 bool VerificationResults::IsClassRejected(ClassReference ref) const {
124   ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
125   return (rejected_classes_.find(ref) != rejected_classes_.end());
126 }
127 
IsCandidateForCompilation(MethodReference &,const uint32_t access_flags) const128 bool VerificationResults::IsCandidateForCompilation(MethodReference&,
129                                                     const uint32_t access_flags) const {
130   if (!compiler_options_->IsAotCompilationEnabled()) {
131     return false;
132   }
133   // Don't compile class initializers unless kEverything.
134   if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) &&
135      ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
136     return false;
137   }
138   return true;
139 }
140 
AddDexFile(const DexFile * dex_file)141 void VerificationResults::AddDexFile(const DexFile* dex_file) {
142   atomic_verified_methods_.AddDexFile(dex_file);
143   WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
144   // There can be some verified methods that are already registered for the dex_file since we set
145   // up well known classes earlier. Remove these and put them in the array so that we don't
146   // accidentally miss seeing them.
147   for (auto it = verified_methods_.begin(); it != verified_methods_.end(); ) {
148     MethodReference ref = it->first;
149     if (ref.dex_file == dex_file) {
150       CHECK(atomic_verified_methods_.Insert(ref, nullptr, it->second) ==
151           AtomicMap::kInsertResultSuccess);
152       it = verified_methods_.erase(it);
153     } else {
154       ++it;
155     }
156   }
157 }
158 
159 }  // namespace art
160