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