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 "base/logging.h"
20 #include "base/stl_util.h"
21 #include "base/mutex-inl.h"
22 #include "driver/compiler_driver.h"
23 #include "driver/compiler_options.h"
24 #include "thread.h"
25 #include "thread-inl.h"
26 #include "utils/atomic_method_ref_map-inl.h"
27 #include "verified_method.h"
28 #include "verifier/method_verifier-inl.h"
29 
30 namespace art {
31 
VerificationResults(const CompilerOptions * compiler_options)32 VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
33     : compiler_options_(compiler_options),
34       verified_methods_lock_("compiler verified methods lock"),
35       rejected_classes_lock_("compiler rejected classes lock") {}
36 
~VerificationResults()37 VerificationResults::~VerificationResults() {
38   WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
39   STLDeleteValues(&verified_methods_);
40   atomic_verified_methods_.Visit([](const MethodReference& ref ATTRIBUTE_UNUSED,
41                                     const VerifiedMethod* method) {
42     delete method;
43   });
44 }
45 
ProcessVerifiedMethod(verifier::MethodVerifier * method_verifier)46 void VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
47   DCHECK(method_verifier != nullptr);
48   MethodReference ref = method_verifier->GetMethodReference();
49   std::unique_ptr<const VerifiedMethod> verified_method(VerifiedMethod::Create(method_verifier));
50   if (verified_method == nullptr) {
51     // We'll punt this later.
52     return;
53   }
54   AtomicMap::InsertResult result = atomic_verified_methods_.Insert(ref,
55                                                                    /*expected*/ nullptr,
56                                                                    verified_method.get());
57   const VerifiedMethod* existing = nullptr;
58   bool inserted;
59   if (result != AtomicMap::kInsertResultInvalidDexFile) {
60     inserted = (result == AtomicMap::kInsertResultSuccess);
61     if (!inserted) {
62       // Rare case.
63       CHECK(atomic_verified_methods_.Get(ref, &existing));
64       CHECK_NE(verified_method.get(), existing);
65     }
66   } else {
67     WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
68     auto it = verified_methods_.find(ref);
69     inserted = it == verified_methods_.end();
70     if (inserted) {
71       verified_methods_.Put(ref, verified_method.get());
72       DCHECK(verified_methods_.find(ref) != verified_methods_.end());
73     } else {
74       existing = it->second;
75     }
76   }
77   if (inserted) {
78     // Successfully added, release the unique_ptr since we no longer have ownership.
79     DCHECK_EQ(GetVerifiedMethod(ref), verified_method.get());
80     verified_method.release();
81   } else {
82     // TODO: Investigate why are we doing the work again for this method and try to avoid it.
83     LOG(WARNING) << "Method processed more than once: " << ref.PrettyMethod();
84     if (!Runtime::Current()->UseJitCompilation()) {
85       DCHECK_EQ(existing->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
86     }
87     // Let the unique_ptr delete the new verified method since there was already an existing one
88     // registered. It is unsafe to replace the existing one since the JIT may be using it to
89     // generate a native GC map.
90   }
91 }
92 
GetVerifiedMethod(MethodReference ref)93 const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
94   const VerifiedMethod* ret = nullptr;
95   if (atomic_verified_methods_.Get(ref, &ret)) {
96     return ret;
97   }
98   ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
99   auto it = verified_methods_.find(ref);
100   return (it != verified_methods_.end()) ? it->second : nullptr;
101 }
102 
CreateVerifiedMethodFor(MethodReference ref)103 void VerificationResults::CreateVerifiedMethodFor(MethodReference ref) {
104   // This method should only be called for classes verified at compile time,
105   // which have no verifier error, nor has methods that we know will throw
106   // at runtime.
107   atomic_verified_methods_.Insert(
108       ref,
109       /*expected*/ nullptr,
110       new VerifiedMethod(/* encountered_error_types */ 0, /* has_runtime_throw */ false));
111   // We don't check the result of `Insert` as we could insert twice for the same
112   // MethodReference in the presence of duplicate methods.
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)123 bool VerificationResults::IsClassRejected(ClassReference ref) {
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)128 bool VerificationResults::IsCandidateForCompilation(MethodReference&,
129                                                     const uint32_t access_flags) {
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