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 "verified_method.h"
27 #include "verifier/method_verifier-inl.h"
28
29 namespace art {
30
VerificationResults(const CompilerOptions * compiler_options)31 VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
32 : compiler_options_(compiler_options),
33 verified_methods_lock_("compiler verified methods lock"),
34 verified_methods_(),
35 rejected_classes_lock_("compiler rejected classes lock"),
36 rejected_classes_() {
37 }
38
~VerificationResults()39 VerificationResults::~VerificationResults() {
40 Thread* self = Thread::Current();
41 {
42 WriterMutexLock mu(self, verified_methods_lock_);
43 STLDeleteValues(&verified_methods_);
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 bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
51 const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
52 if (verified_method == nullptr) {
53 // We'll punt this later.
54 return;
55 }
56
57 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
58 auto it = verified_methods_.find(ref);
59 if (it != verified_methods_.end()) {
60 // TODO: Investigate why are we doing the work again for this method and try to avoid it.
61 LOG(WARNING) << "Method processed more than once: "
62 << PrettyMethod(ref.dex_method_index, *ref.dex_file);
63 if (!Runtime::Current()->UseJitCompilation()) {
64 DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size());
65 DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
66 }
67 // Delete the new verified method since there was already an existing one registered. It
68 // is unsafe to replace the existing one since the JIT may be using it to generate a
69 // native GC map.
70 delete verified_method;
71 return;
72 }
73 verified_methods_.Put(ref, verified_method);
74 DCHECK(verified_methods_.find(ref) != verified_methods_.end());
75 }
76
GetVerifiedMethod(MethodReference ref)77 const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
78 ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
79 auto it = verified_methods_.find(ref);
80 return (it != verified_methods_.end()) ? it->second : nullptr;
81 }
82
AddRejectedClass(ClassReference ref)83 void VerificationResults::AddRejectedClass(ClassReference ref) {
84 {
85 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
86 rejected_classes_.insert(ref);
87 }
88 DCHECK(IsClassRejected(ref));
89 }
90
IsClassRejected(ClassReference ref)91 bool VerificationResults::IsClassRejected(ClassReference ref) {
92 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
93 return (rejected_classes_.find(ref) != rejected_classes_.end());
94 }
95
IsCandidateForCompilation(MethodReference &,const uint32_t access_flags)96 bool VerificationResults::IsCandidateForCompilation(MethodReference&,
97 const uint32_t access_flags) {
98 if (!compiler_options_->IsBytecodeCompilationEnabled()) {
99 return false;
100 }
101 // Don't compile class initializers unless kEverything.
102 if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) &&
103 ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
104 return false;
105 }
106 return true;
107 }
108
109 } // namespace art
110