blob: 51a3d84382baad9217bf22a2fd6cabb193a7972a [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 Gampe0b9203e2015-01-22 20:39:27 -080019#include "base/logging.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000020#include "base/stl_util.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000021#include "base/mutex-inl.h"
Brian Carlstrom6449c622014-02-10 23:48:36 -080022#include "driver/compiler_driver.h"
23#include "driver/compiler_options.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000024#include "thread.h"
25#include "thread-inl.h"
26#include "verified_method.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000027#include "verifier/method_verifier-inl.h"
28
29namespace art {
30
Brian Carlstrom6449c622014-02-10 23:48:36 -080031VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
Ian Rogers1ff3c982014-08-12 02:30:58 -070032 : compiler_options_(compiler_options),
33 verified_methods_lock_("compiler verified methods lock"),
Vladimir Markoc7f83202014-01-24 17:55:18 +000034 verified_methods_(),
35 rejected_classes_lock_("compiler rejected classes lock"),
36 rejected_classes_() {
Ian Rogersa03de6d2014-03-08 23:37:07 +000037 UNUSED(compiler_options);
Vladimir Markoc7f83202014-01-24 17:55:18 +000038}
39
40VerificationResults::~VerificationResults() {
41 Thread* self = Thread::Current();
42 {
43 WriterMutexLock mu(self, verified_methods_lock_);
44 STLDeleteValues(&verified_methods_);
45 }
46}
47
48bool VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
Brian Carlstrom6449c622014-02-10 23:48:36 -080049 DCHECK(method_verifier != NULL);
Vladimir Markoc7f83202014-01-24 17:55:18 +000050 MethodReference ref = method_verifier->GetMethodReference();
51 bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
52 // TODO: Check also for virtual/interface invokes when DEX-to-DEX supports devirtualization.
53 if (!compile && !method_verifier->HasCheckCasts()) {
54 return true;
55 }
56
57 const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
58 if (verified_method == nullptr) {
Andreas Gampe6c170c92014-12-17 14:35:46 -080059 // Do not report an error to the verifier. We'll just punt this later.
60 return true;
Vladimir Markoc7f83202014-01-24 17:55:18 +000061 }
62
63 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
64 auto it = verified_methods_.find(ref);
65 if (it != verified_methods_.end()) {
66 // TODO: Investigate why are we doing the work again for this method and try to avoid it.
67 LOG(WARNING) << "Method processed more than once: "
68 << PrettyMethod(ref.dex_method_index, *ref.dex_file);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069 if (!Runtime::Current()->UseJit()) {
70 DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size());
71 DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
72 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000073 DCHECK_EQ(it->second->GetDexGcMap().size(), verified_method->GetDexGcMap().size());
74 delete it->second;
75 verified_methods_.erase(it);
76 }
77 verified_methods_.Put(ref, verified_method);
78 DCHECK(verified_methods_.find(ref) != verified_methods_.end());
79 return true;
80}
81
82const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
83 ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
84 auto it = verified_methods_.find(ref);
85 return (it != verified_methods_.end()) ? it->second : nullptr;
86}
87
Mathieu Chartierab972ef2014-12-03 17:38:22 -080088void VerificationResults::RemoveVerifiedMethod(MethodReference ref) {
89 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
90 auto it = verified_methods_.find(ref);
91 if (it != verified_methods_.end()) {
92 delete it->second;
93 verified_methods_.erase(it);
94 }
95}
96
Vladimir Markoc7f83202014-01-24 17:55:18 +000097void VerificationResults::AddRejectedClass(ClassReference ref) {
98 {
99 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
100 rejected_classes_.insert(ref);
101 }
102 DCHECK(IsClassRejected(ref));
103}
104
105bool VerificationResults::IsClassRejected(ClassReference ref) {
106 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
107 return (rejected_classes_.find(ref) != rejected_classes_.end());
108}
109
Elliott Hughes956af0f2014-12-11 14:34:28 -0800110bool VerificationResults::IsCandidateForCompilation(MethodReference&,
Vladimir Markoc7f83202014-01-24 17:55:18 +0000111 const uint32_t access_flags) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700112 if (!compiler_options_->IsCompilationEnabled()) {
113 return false;
114 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000115 // Don't compile class initializers, ever.
116 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
117 return false;
118 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800119 return true;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000120}
121
122} // namespace art