verified_method.h revision c7f832061fea59fd6abd125f26c8ca1faec695a5
1/*
2 * Copyright (C) 2014 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#ifndef ART_COMPILER_DEX_VERIFIED_METHOD_H_
18#define ART_COMPILER_DEX_VERIFIED_METHOD_H_
19
20#include <vector>
21
22#include "method_reference.h"
23#include "safe_map.h"
24
25namespace art {
26
27namespace verifier {
28class MethodVerifier;
29}  // namespace verifier
30
31class VerifiedMethod {
32 public:
33  // Cast elision set type.
34  // Since we're adding the dex PCs to the set in increasing order, a sorted vector
35  // is better for performance (not just memory usage), especially for large sets.
36  typedef std::vector<uint32_t> SafeCastSet;
37
38  // Devirtualization map type maps dex offset to concrete method reference.
39  typedef SafeMap<uint32_t, MethodReference> DevirtualizationMap;
40
41  static const VerifiedMethod* Create(verifier::MethodVerifier* method_verifier, bool compile)
42      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
43  ~VerifiedMethod() = default;
44
45  const std::vector<uint8_t>& GetDexGcMap() const {
46    return dex_gc_map_;
47  }
48
49  const DevirtualizationMap& GetDevirtMap() const {
50    return devirt_map_;
51  }
52
53  const SafeCastSet& GetSafeCastSet() const {
54    return safe_cast_set_;
55  }
56
57  // Returns the devirtualization target method, or nullptr if none.
58  const MethodReference* GetDevirtTarget(uint32_t dex_pc) const;
59
60  // Returns true if the cast can statically be verified to be redundant
61  // by using the check-cast elision peephole optimization in the verifier.
62  bool IsSafeCast(uint32_t pc) const;
63
64 private:
65  VerifiedMethod() = default;
66
67  /*
68   * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
69   * verification). For type-precise determination we have all the data we need, so we just need to
70   * encode it in some clever fashion.
71   * Stores the data in dex_gc_map_, returns true on success and false on failure.
72   */
73  bool GenerateGcMap(verifier::MethodVerifier* method_verifier);
74
75  // Verify that the GC map associated with method_ is well formed.
76  static void VerifyGcMap(verifier::MethodVerifier* method_verifier,
77                          const std::vector<uint8_t>& data);
78
79  // Compute sizes for GC map data.
80  static void ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
81                                size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
82
83  // Generate devirtualizaion map into devirt_map_.
84  void GenerateDevirtMap(verifier::MethodVerifier* method_verifier)
85      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
87  // Generate safe case set into safe_cast_set_.
88  void GenerateSafeCastSet(verifier::MethodVerifier* method_verifier)
89      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90
91  std::vector<uint8_t> dex_gc_map_;
92  DevirtualizationMap devirt_map_;
93  SafeCastSet safe_cast_set_;
94};
95
96}  // namespace art
97
98#endif  // ART_COMPILER_DEX_VERIFIED_METHOD_H_
99