Home | History | Annotate | Download | only in dex
      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 
     19 #include <android-base/logging.h>
     20 
     21 #include "base/mutex-inl.h"
     22 #include "base/stl_util.h"
     23 #include "driver/compiler_driver.h"
     24 #include "driver/compiler_options.h"
     25 #include "runtime.h"
     26 #include "thread-current-inl.h"
     27 #include "thread.h"
     28 #include "utils/atomic_dex_ref_map-inl.h"
     29 #include "verified_method.h"
     30 #include "verifier/method_verifier-inl.h"
     31 
     32 namespace art {
     33 
     34 VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
     35     : compiler_options_(compiler_options),
     36       verified_methods_lock_("compiler verified methods lock"),
     37       rejected_classes_lock_("compiler rejected classes lock") {}
     38 
     39 VerificationResults::~VerificationResults() {
     40   WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
     41   STLDeleteValues(&verified_methods_);
     42   atomic_verified_methods_.Visit([](const DexFileReference& ref ATTRIBUTE_UNUSED,
     43                                     const VerifiedMethod* method) {
     44     delete method;
     45   });
     46 }
     47 
     48 void VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
     49   DCHECK(method_verifier != nullptr);
     50   MethodReference ref = method_verifier->GetMethodReference();
     51   std::unique_ptr<const VerifiedMethod> verified_method(VerifiedMethod::Create(method_verifier));
     52   if (verified_method == nullptr) {
     53     // We'll punt this later.
     54     return;
     55   }
     56   AtomicMap::InsertResult result = atomic_verified_methods_.Insert(ref,
     57                                                                    /*expected*/ nullptr,
     58                                                                    verified_method.get());
     59   const VerifiedMethod* existing = nullptr;
     60   bool inserted;
     61   if (result != AtomicMap::kInsertResultInvalidDexFile) {
     62     inserted = (result == AtomicMap::kInsertResultSuccess);
     63     if (!inserted) {
     64       // Rare case.
     65       CHECK(atomic_verified_methods_.Get(ref, &existing));
     66       CHECK_NE(verified_method.get(), existing);
     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 {
     84     // TODO: Investigate why are we doing the work again for this method and try to avoid it.
     85     LOG(WARNING) << "Method processed more than once: " << ref.PrettyMethod();
     86     if (!Runtime::Current()->UseJitCompilation()) {
     87       if (kIsDebugBuild) {
     88         auto ex_set = existing->GetSafeCastSet();
     89         auto ve_set = verified_method->GetSafeCastSet();
     90         CHECK_EQ(ex_set == nullptr, ve_set == nullptr);
     91         CHECK((ex_set == nullptr) || (ex_set->size() == ve_set->size()));
     92       }
     93     }
     94     // Let the unique_ptr delete the new verified method since there was already an existing one
     95     // registered. It is unsafe to replace the existing one since the JIT may be using it to
     96     // generate a native GC map.
     97   }
     98 }
     99 
    100 const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
    101   const VerifiedMethod* ret = nullptr;
    102   if (atomic_verified_methods_.Get(ref, &ret)) {
    103     return ret;
    104   }
    105   ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
    106   auto it = verified_methods_.find(ref);
    107   return (it != verified_methods_.end()) ? it->second : nullptr;
    108 }
    109 
    110 void VerificationResults::CreateVerifiedMethodFor(MethodReference ref) {
    111   // This method should only be called for classes verified at compile time,
    112   // which have no verifier error, nor has methods that we know will throw
    113   // at runtime.
    114   std::unique_ptr<VerifiedMethod> verified_method = std::make_unique<VerifiedMethod>(
    115       /* encountered_error_types */ 0, /* has_runtime_throw */ false);
    116   if (atomic_verified_methods_.Insert(ref,
    117                                       /*expected*/ nullptr,
    118                                       verified_method.get()) ==
    119           AtomicMap::InsertResult::kInsertResultSuccess) {
    120     verified_method.release();
    121   }
    122 }
    123 
    124 void VerificationResults::AddRejectedClass(ClassReference ref) {
    125   {
    126     WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
    127     rejected_classes_.insert(ref);
    128   }
    129   DCHECK(IsClassRejected(ref));
    130 }
    131 
    132 bool VerificationResults::IsClassRejected(ClassReference ref) {
    133   ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
    134   return (rejected_classes_.find(ref) != rejected_classes_.end());
    135 }
    136 
    137 bool VerificationResults::IsCandidateForCompilation(MethodReference&,
    138                                                     const uint32_t access_flags) {
    139   if (!compiler_options_->IsAotCompilationEnabled()) {
    140     return false;
    141   }
    142   // Don't compile class initializers unless kEverything.
    143   if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) &&
    144      ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
    145     return false;
    146   }
    147   return true;
    148 }
    149 
    150 void VerificationResults::AddDexFile(const DexFile* dex_file) {
    151   atomic_verified_methods_.AddDexFile(dex_file);
    152   WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
    153   // There can be some verified methods that are already registered for the dex_file since we set
    154   // up well known classes earlier. Remove these and put them in the array so that we don't
    155   // accidentally miss seeing them.
    156   for (auto it = verified_methods_.begin(); it != verified_methods_.end(); ) {
    157     MethodReference ref = it->first;
    158     if (ref.dex_file == dex_file) {
    159       CHECK(atomic_verified_methods_.Insert(ref, nullptr, it->second) ==
    160           AtomicMap::kInsertResultSuccess);
    161       it = verified_methods_.erase(it);
    162     } else {
    163       ++it;
    164     }
    165   }
    166 }
    167 
    168 }  // namespace art
    169