summaryrefslogtreecommitdiffstats
path: root/compiler/dex/verification_results.cc
blob: 8b4fa1a82d431d807e439a8a39fa9a343e335845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "verification_results.h"

#include "base/stl_util.h"
#include "base/mutex.h"
#include "base/mutex-inl.h"
#include "thread.h"
#include "thread-inl.h"
#include "verified_method.h"
#include "verifier/method_verifier.h"
#include "verifier/method_verifier-inl.h"

namespace art {

VerificationResults::VerificationResults()
    : verified_methods_lock_("compiler verified methods lock"),
      verified_methods_(),
      rejected_classes_lock_("compiler rejected classes lock"),
      rejected_classes_() {
}

VerificationResults::~VerificationResults() {
  Thread* self = Thread::Current();
  {
    WriterMutexLock mu(self, verified_methods_lock_);
    STLDeleteValues(&verified_methods_);
  }
}

bool VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
  MethodReference ref = method_verifier->GetMethodReference();
  bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
  // TODO: Check also for virtual/interface invokes when DEX-to-DEX supports devirtualization.
  if (!compile && !method_verifier->HasCheckCasts()) {
    return true;
  }

  const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
  if (verified_method == nullptr) {
    DCHECK(method_verifier->HasFailures());
    return false;
  }

  WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
  auto it = verified_methods_.find(ref);
  if (it != verified_methods_.end()) {
    // TODO: Investigate why are we doing the work again for this method and try to avoid it.
    LOG(WARNING) << "Method processed more than once: "
        << PrettyMethod(ref.dex_method_index, *ref.dex_file);
    DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size());
    DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
    DCHECK_EQ(it->second->GetDexGcMap().size(), verified_method->GetDexGcMap().size());
    delete it->second;
    verified_methods_.erase(it);
  }
  verified_methods_.Put(ref, verified_method);
  DCHECK(verified_methods_.find(ref) != verified_methods_.end());
  return true;
}

const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
  ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
  auto it = verified_methods_.find(ref);
  return (it != verified_methods_.end()) ? it->second : nullptr;
}

const std::vector<uint8_t>* VerificationResults::GetDexGcMap(MethodReference ref) {
  const VerifiedMethod* verified_method = GetVerifiedMethod(ref);
  CHECK(verified_method != nullptr)
    << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
  return &verified_method->GetDexGcMap();
}

const MethodReference* VerificationResults::GetDevirtMap(const MethodReference& ref,
                                                                    uint32_t dex_pc) {
  const VerifiedMethod* verified_method = GetVerifiedMethod(ref);
  return (verified_method != nullptr) ? verified_method->GetDevirtTarget(dex_pc) : nullptr;
}

bool VerificationResults::IsSafeCast(MethodReference ref, uint32_t pc) {
  const VerifiedMethod* verified_method = GetVerifiedMethod(ref);
  return (verified_method != nullptr) && (verified_method->IsSafeCast(pc));
}

void VerificationResults::AddRejectedClass(ClassReference ref) {
  {
    WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
    rejected_classes_.insert(ref);
  }
  DCHECK(IsClassRejected(ref));
}

bool VerificationResults::IsClassRejected(ClassReference ref) {
  ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
  return (rejected_classes_.find(ref) != rejected_classes_.end());
}

bool VerificationResults::IsCandidateForCompilation(MethodReference& method_ref,
                                                    const uint32_t access_flags) {
#ifdef ART_SEA_IR_MODE
    bool use_sea = Runtime::Current()->IsSeaIRMode();
    use_sea = use_sea && (std::string::npos != PrettyMethod(
                          method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
    if (use_sea) return true;
#endif
  // Don't compile class initializers, ever.
  if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
    return false;
  }
  return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
}

}  // namespace art