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