Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 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 | |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 20 | #include "base/stl_util.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 21 | #include "base/mutex-inl.h" |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 22 | #include "driver/compiler_driver.h" |
| 23 | #include "driver/compiler_options.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 24 | #include "thread.h" |
| 25 | #include "thread-inl.h" |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 26 | #include "utils/atomic_method_ref_map-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 27 | #include "verified_method.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 28 | #include "verifier/method_verifier-inl.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 32 | VerificationResults::VerificationResults(const CompilerOptions* compiler_options) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 33 | : compiler_options_(compiler_options), |
| 34 | verified_methods_lock_("compiler verified methods lock"), |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 35 | rejected_classes_lock_("compiler rejected classes lock") {} |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 36 | |
| 37 | VerificationResults::~VerificationResults() { |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 38 | WriterMutexLock mu(Thread::Current(), verified_methods_lock_); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 39 | STLDeleteValues(&verified_methods_); |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 40 | atomic_verified_methods_.Visit([](const MethodReference& ref ATTRIBUTE_UNUSED, |
| 41 | const VerifiedMethod* method) { |
| 42 | delete method; |
| 43 | }); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 46 | void VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 47 | DCHECK(method_verifier != nullptr); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 48 | MethodReference ref = method_verifier->GetMethodReference(); |
| 49 | bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags()); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 50 | std::unique_ptr<const VerifiedMethod> verified_method( |
| 51 | VerifiedMethod::Create(method_verifier, compile)); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 52 | if (verified_method == nullptr) { |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 53 | // We'll punt this later. |
| 54 | return; |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 55 | } |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 56 | AtomicMap::InsertResult result = atomic_verified_methods_.Insert(ref, |
| 57 | /*expected*/ nullptr, |
| 58 | verified_method.get()); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 59 | const VerifiedMethod* existing = nullptr; |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 60 | bool inserted; |
| 61 | if (result != AtomicMap::kInsertResultInvalidDexFile) { |
| 62 | inserted = (result == AtomicMap::kInsertResultSuccess); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 63 | if (!inserted) { |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 64 | // Rare case. |
| 65 | CHECK(atomic_verified_methods_.Get(ref, &existing)); |
| 66 | CHECK_NE(verified_method.get(), existing); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 67 | } |
| 68 | } else { |
| 69 | WriterMutexLock mu(Thread::Current(), verified_methods_lock_); |
| 70 | auto it = verified_methods_.find(ref); |
| 71 | inserted = it == verified_methods_.end(); |
| 72 | if (inserted) { |
| 73 | verified_methods_.Put(ref, verified_method.get()); |
| 74 | DCHECK(verified_methods_.find(ref) != verified_methods_.end()); |
| 75 | } else { |
| 76 | existing = it->second; |
| 77 | } |
| 78 | } |
| 79 | if (inserted) { |
| 80 | // Successfully added, release the unique_ptr since we no longer have ownership. |
| 81 | DCHECK_EQ(GetVerifiedMethod(ref), verified_method.get()); |
| 82 | verified_method.release(); |
| 83 | } else { |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 84 | // TODO: Investigate why are we doing the work again for this method and try to avoid it. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 85 | LOG(WARNING) << "Method processed more than once: " << ref.PrettyMethod(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 86 | if (!Runtime::Current()->UseJitCompilation()) { |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 87 | DCHECK_EQ(existing->GetDevirtMap().size(), verified_method->GetDevirtMap().size()); |
| 88 | DCHECK_EQ(existing->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 89 | } |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 90 | // Let the unique_ptr delete the new verified method since there was already an existing one |
| 91 | // registered. It is unsafe to replace the existing one since the JIT may be using it to |
| 92 | // generate a native GC map. |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 93 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) { |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 97 | const VerifiedMethod* ret = nullptr; |
| 98 | if (atomic_verified_methods_.Get(ref, &ret)) { |
| 99 | return ret; |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 100 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 101 | ReaderMutexLock mu(Thread::Current(), verified_methods_lock_); |
| 102 | auto it = verified_methods_.find(ref); |
| 103 | return (it != verified_methods_.end()) ? it->second : nullptr; |
| 104 | } |
| 105 | |
Nicolas Geoffray | 51c17fa | 2016-11-25 15:56:12 +0000 | [diff] [blame^] | 106 | void VerificationResults::CreateVerifiedMethodFor(MethodReference ref) { |
| 107 | // This method should only be called for classes verified at compile time, |
| 108 | // which have no verifier error, nor has methods that we know will throw |
| 109 | // at runtime. |
| 110 | AtomicMap::InsertResult result = atomic_verified_methods_.Insert( |
| 111 | ref, |
| 112 | /*expected*/ nullptr, |
| 113 | new VerifiedMethod(/* encountered_error_types */ 0, /* has_runtime_throw */ false)); |
| 114 | DCHECK_EQ(result, AtomicMap::kInsertResultSuccess); |
| 115 | } |
| 116 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 117 | void VerificationResults::AddRejectedClass(ClassReference ref) { |
| 118 | { |
| 119 | WriterMutexLock mu(Thread::Current(), rejected_classes_lock_); |
| 120 | rejected_classes_.insert(ref); |
| 121 | } |
| 122 | DCHECK(IsClassRejected(ref)); |
| 123 | } |
| 124 | |
| 125 | bool VerificationResults::IsClassRejected(ClassReference ref) { |
| 126 | ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_); |
| 127 | return (rejected_classes_.find(ref) != rejected_classes_.end()); |
| 128 | } |
| 129 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 130 | bool VerificationResults::IsCandidateForCompilation(MethodReference&, |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 131 | const uint32_t access_flags) { |
Vladimir Marko | f6d1e0f | 2016-05-23 15:32:42 +0100 | [diff] [blame] | 132 | if (!compiler_options_->IsBytecodeCompilationEnabled()) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 133 | return false; |
| 134 | } |
buzbee | c833299 | 2015-06-25 15:53:45 -0700 | [diff] [blame] | 135 | // Don't compile class initializers unless kEverything. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 136 | if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) && |
buzbee | c833299 | 2015-06-25 15:53:45 -0700 | [diff] [blame] | 137 | ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) { |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 140 | return true; |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 143 | void VerificationResults::AddDexFile(const DexFile* dex_file) { |
| 144 | atomic_verified_methods_.AddDexFile(dex_file); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 145 | WriterMutexLock mu(Thread::Current(), verified_methods_lock_); |
| 146 | // There can be some verified methods that are already registered for the dex_file since we set |
| 147 | // up well known classes earlier. Remove these and put them in the array so that we don't |
| 148 | // accidentally miss seeing them. |
| 149 | for (auto it = verified_methods_.begin(); it != verified_methods_.end(); ) { |
| 150 | MethodReference ref = it->first; |
| 151 | if (ref.dex_file == dex_file) { |
Mathieu Chartier | 9df8931 | 2016-11-23 13:28:16 -0800 | [diff] [blame] | 152 | CHECK(atomic_verified_methods_.Insert(ref, nullptr, it->second) == |
| 153 | AtomicMap::kInsertResultSuccess); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 154 | it = verified_methods_.erase(it); |
| 155 | } else { |
| 156 | ++it; |
| 157 | } |
| 158 | } |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 161 | } // namespace art |