blob: 00a7d44bac909d8606277ae359fd2d32d08e0ca3 [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"
Mathieu Chartier9df89312016-11-23 13:28:16 -080026#include "utils/atomic_method_ref_map-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000027#include "verified_method.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000028#include "verifier/method_verifier-inl.h"
29
30namespace art {
31
Brian Carlstrom6449c622014-02-10 23:48:36 -080032VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
Ian Rogers1ff3c982014-08-12 02:30:58 -070033 : compiler_options_(compiler_options),
34 verified_methods_lock_("compiler verified methods lock"),
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080035 rejected_classes_lock_("compiler rejected classes lock") {}
Vladimir Markoc7f83202014-01-24 17:55:18 +000036
37VerificationResults::~VerificationResults() {
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080038 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080039 STLDeleteValues(&verified_methods_);
Mathieu Chartier9df89312016-11-23 13:28:16 -080040 atomic_verified_methods_.Visit([](const MethodReference& ref ATTRIBUTE_UNUSED,
41 const VerifiedMethod* method) {
42 delete method;
43 });
Vladimir Markoc7f83202014-01-24 17:55:18 +000044}
45
Andreas Gampe53e32d12015-12-09 21:03:23 -080046void VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 DCHECK(method_verifier != nullptr);
Vladimir Markoc7f83202014-01-24 17:55:18 +000048 MethodReference ref = method_verifier->GetMethodReference();
Nicolas Geoffrayc51c7ca2016-11-25 15:46:48 +000049 std::unique_ptr<const VerifiedMethod> verified_method(VerifiedMethod::Create(method_verifier));
Vladimir Markoc7f83202014-01-24 17:55:18 +000050 if (verified_method == nullptr) {
Andreas Gampe53e32d12015-12-09 21:03:23 -080051 // We'll punt this later.
52 return;
Vladimir Markoc7f83202014-01-24 17:55:18 +000053 }
Mathieu Chartier9df89312016-11-23 13:28:16 -080054 AtomicMap::InsertResult result = atomic_verified_methods_.Insert(ref,
55 /*expected*/ nullptr,
56 verified_method.get());
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080057 const VerifiedMethod* existing = nullptr;
Mathieu Chartier9df89312016-11-23 13:28:16 -080058 bool inserted;
59 if (result != AtomicMap::kInsertResultInvalidDexFile) {
60 inserted = (result == AtomicMap::kInsertResultSuccess);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080061 if (!inserted) {
Mathieu Chartier9df89312016-11-23 13:28:16 -080062 // Rare case.
63 CHECK(atomic_verified_methods_.Get(ref, &existing));
64 CHECK_NE(verified_method.get(), existing);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080065 }
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 {
Vladimir Markoc7f83202014-01-24 17:55:18 +000082 // TODO: Investigate why are we doing the work again for this method and try to avoid it.
David Sehr709b0702016-10-13 09:12:37 -070083 LOG(WARNING) << "Method processed more than once: " << ref.PrettyMethod();
Calin Juravleffc87072016-04-20 14:22:09 +010084 if (!Runtime::Current()->UseJitCompilation()) {
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080085 DCHECK_EQ(existing->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086 }
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080087 // 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.
Vladimir Markoc7f83202014-01-24 17:55:18 +000090 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000091}
92
93const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
Mathieu Chartier9df89312016-11-23 13:28:16 -080094 const VerifiedMethod* ret = nullptr;
95 if (atomic_verified_methods_.Get(ref, &ret)) {
96 return ret;
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080097 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000098 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
Nicolas Geoffray51c17fa2016-11-25 15:56:12 +0000103void 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 AtomicMap::InsertResult result = atomic_verified_methods_.Insert(
108 ref,
109 /*expected*/ nullptr,
110 new VerifiedMethod(/* encountered_error_types */ 0, /* has_runtime_throw */ false));
111 DCHECK_EQ(result, AtomicMap::kInsertResultSuccess);
112}
113
Vladimir Markoc7f83202014-01-24 17:55:18 +0000114void VerificationResults::AddRejectedClass(ClassReference ref) {
115 {
116 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
117 rejected_classes_.insert(ref);
118 }
119 DCHECK(IsClassRejected(ref));
120}
121
122bool VerificationResults::IsClassRejected(ClassReference ref) {
123 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
124 return (rejected_classes_.find(ref) != rejected_classes_.end());
125}
126
Elliott Hughes956af0f2014-12-11 14:34:28 -0800127bool VerificationResults::IsCandidateForCompilation(MethodReference&,
Vladimir Markoc7f83202014-01-24 17:55:18 +0000128 const uint32_t access_flags) {
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100129 if (!compiler_options_->IsBytecodeCompilationEnabled()) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700130 return false;
131 }
buzbeec8332992015-06-25 15:53:45 -0700132 // Don't compile class initializers unless kEverything.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000133 if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) &&
buzbeec8332992015-06-25 15:53:45 -0700134 ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000135 return false;
136 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800137 return true;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000138}
139
Mathieu Chartier9df89312016-11-23 13:28:16 -0800140void VerificationResults::AddDexFile(const DexFile* dex_file) {
141 atomic_verified_methods_.AddDexFile(dex_file);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -0800142 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
143 // There can be some verified methods that are already registered for the dex_file since we set
144 // up well known classes earlier. Remove these and put them in the array so that we don't
145 // accidentally miss seeing them.
146 for (auto it = verified_methods_.begin(); it != verified_methods_.end(); ) {
147 MethodReference ref = it->first;
148 if (ref.dex_file == dex_file) {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800149 CHECK(atomic_verified_methods_.Insert(ref, nullptr, it->second) ==
150 AtomicMap::kInsertResultSuccess);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -0800151 it = verified_methods_.erase(it);
152 } else {
153 ++it;
154 }
155 }
Mathieu Chartierfc2dd612016-11-21 15:05:23 -0800156}
157
Vladimir Markoc7f83202014-01-24 17:55:18 +0000158} // namespace art