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 "base/stl_util.h"
     20 #include "base/mutex.h"
     21 #include "base/mutex-inl.h"
     22 #include "driver/compiler_driver.h"
     23 #include "driver/compiler_options.h"
     24 #include "thread.h"
     25 #include "thread-inl.h"
     26 #include "verified_method.h"
     27 #include "verifier/method_verifier.h"
     28 #include "verifier/method_verifier-inl.h"
     29 
     30 namespace art {
     31 
     32 VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
     33     : compiler_options_(compiler_options),
     34       verified_methods_lock_("compiler verified methods lock"),
     35       verified_methods_(),
     36       rejected_classes_lock_("compiler rejected classes lock"),
     37       rejected_classes_() {
     38   UNUSED(compiler_options);
     39 }
     40 
     41 VerificationResults::~VerificationResults() {
     42   Thread* self = Thread::Current();
     43   {
     44     WriterMutexLock mu(self, verified_methods_lock_);
     45     STLDeleteValues(&verified_methods_);
     46   }
     47 }
     48 
     49 bool VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
     50   DCHECK(method_verifier != NULL);
     51   MethodReference ref = method_verifier->GetMethodReference();
     52   bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
     53   // TODO: Check also for virtual/interface invokes when DEX-to-DEX supports devirtualization.
     54   if (!compile && !method_verifier->HasCheckCasts()) {
     55     return true;
     56   }
     57 
     58   const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
     59   if (verified_method == nullptr) {
     60     // Do not report an error to the verifier. We'll just punt this later.
     61     return true;
     62   }
     63 
     64   WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
     65   auto it = verified_methods_.find(ref);
     66   if (it != verified_methods_.end()) {
     67     // TODO: Investigate why are we doing the work again for this method and try to avoid it.
     68     LOG(WARNING) << "Method processed more than once: "
     69         << PrettyMethod(ref.dex_method_index, *ref.dex_file);
     70     DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size());
     71     DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
     72     DCHECK_EQ(it->second->GetDexGcMap().size(), verified_method->GetDexGcMap().size());
     73     delete it->second;
     74     verified_methods_.erase(it);
     75   }
     76   verified_methods_.Put(ref, verified_method);
     77   DCHECK(verified_methods_.find(ref) != verified_methods_.end());
     78   return true;
     79 }
     80 
     81 const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
     82   ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
     83   auto it = verified_methods_.find(ref);
     84   return (it != verified_methods_.end()) ? it->second : nullptr;
     85 }
     86 
     87 void VerificationResults::RemoveVerifiedMethod(MethodReference ref) {
     88   WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
     89   auto it = verified_methods_.find(ref);
     90   if (it != verified_methods_.end()) {
     91     delete it->second;
     92     verified_methods_.erase(it);
     93   }
     94 }
     95 
     96 void VerificationResults::AddRejectedClass(ClassReference ref) {
     97   {
     98     WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
     99     rejected_classes_.insert(ref);
    100   }
    101   DCHECK(IsClassRejected(ref));
    102 }
    103 
    104 bool VerificationResults::IsClassRejected(ClassReference ref) {
    105   ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
    106   return (rejected_classes_.find(ref) != rejected_classes_.end());
    107 }
    108 
    109 bool VerificationResults::IsCandidateForCompilation(MethodReference& method_ref,
    110                                                     const uint32_t access_flags) {
    111 #ifdef ART_SEA_IR_MODE
    112   bool use_sea = compiler_options_->GetSeaIrMode();
    113   use_sea = use_sea && (std::string::npos != PrettyMethod(
    114                         method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
    115   if (use_sea) {
    116     return true;
    117   }
    118 #endif
    119   if (!compiler_options_->IsCompilationEnabled()) {
    120     return false;
    121   }
    122   // Don't compile class initializers, ever.
    123   if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
    124     return false;
    125   }
    126   return true;
    127 }
    128 
    129 }  // namespace art
    130