blob: 3223f6f7b9948ad8fdaf5a48e824652c1c545c2f [file] [log] [blame]
David Brazdilca3c8c32016-09-06 14:04:48 +01001/*
2 * Copyright (C) 2016 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_RUNTIME_VERIFIER_VERIFIER_DEPS_H_
18#define ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_
19
20#include <map>
21#include <set>
22#include <vector>
23
24#include "art_field.h"
25#include "art_method.h"
David Brazdil6f82fbd2016-09-14 11:55:26 +010026#include "base/array_ref.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010027#include "base/mutex.h"
28#include "method_resolution_kind.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070029#include "obj_ptr.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010030#include "os.h"
31
32namespace art {
33namespace verifier {
34
35// Verification dependencies collector class used by the MethodVerifier to record
36// resolution outcomes and type assignability tests of classes/methods/fields
37// not present in the set of compiled DEX files, that is classes/methods/fields
38// defined in the classpath.
39// The compilation driver initializes the class and registers all DEX files
40// which are being compiled. Classes defined in DEX files outside of this set
41// (or synthesized classes without associated DEX files) are considered being
42// in the classpath.
43// During code-flow verification, the MethodVerifier informs the VerifierDeps
44// singleton about the outcome of every resolution and assignability test, and
45// the singleton records them if their outcome may change with changes in the
46// classpath.
47class VerifierDeps {
48 public:
49 explicit VerifierDeps(const std::vector<const DexFile*>& dex_files)
50 REQUIRES(!Locks::verifier_deps_lock_);
51
52 // Record the outcome `klass` of resolving type `type_idx` from `dex_file`.
53 // If `klass` is null, the class is assumed unresolved.
54 static void MaybeRecordClassResolution(const DexFile& dex_file,
55 uint16_t type_idx,
56 mirror::Class* klass)
57 REQUIRES_SHARED(Locks::mutator_lock_)
58 REQUIRES(!Locks::verifier_deps_lock_);
59
60 // Record the outcome `field` of resolving field `field_idx` from `dex_file`.
61 // If `field` is null, the field is assumed unresolved.
62 static void MaybeRecordFieldResolution(const DexFile& dex_file,
63 uint32_t field_idx,
64 ArtField* field)
65 REQUIRES_SHARED(Locks::mutator_lock_)
66 REQUIRES(!Locks::verifier_deps_lock_);
67
68 // Record the outcome `method` of resolving method `method_idx` from `dex_file`
69 // using `res_kind` kind of method resolution algorithm. If `method` is null,
70 // the method is assumed unresolved.
71 static void MaybeRecordMethodResolution(const DexFile& dex_file,
72 uint32_t method_idx,
73 MethodResolutionKind res_kind,
74 ArtMethod* method)
75 REQUIRES_SHARED(Locks::mutator_lock_)
76 REQUIRES(!Locks::verifier_deps_lock_);
77
78 // Record the outcome `is_assignable` of type assignability test from `source`
79 // to `destination` as defined by RegType::AssignableFrom. `dex_file` is the
80 // owner of the method for which MethodVerifier performed the assignability test.
81 static void MaybeRecordAssignability(const DexFile& dex_file,
82 mirror::Class* destination,
83 mirror::Class* source,
84 bool is_strict,
85 bool is_assignable)
86 REQUIRES_SHARED(Locks::mutator_lock_)
87 REQUIRES(!Locks::verifier_deps_lock_);
88
David Brazdil6f82fbd2016-09-14 11:55:26 +010089 // Serialize the recorded dependencies and store the data into `buffer`.
90 void Encode(std::vector<uint8_t>* buffer) const
91 REQUIRES(!Locks::verifier_deps_lock_);
92
David Brazdilca3c8c32016-09-06 14:04:48 +010093 private:
94 static constexpr uint16_t kUnresolvedMarker = static_cast<uint16_t>(-1);
95
David Brazdil6f82fbd2016-09-14 11:55:26 +010096 // Only used in tests to reconstruct the data structure from serialized data.
97 VerifierDeps(const std::vector<const DexFile*>& dex_files, ArrayRef<uint8_t> data)
98 REQUIRES(!Locks::verifier_deps_lock_);
99
David Brazdilca3c8c32016-09-06 14:04:48 +0100100 using ClassResolutionBase = std::tuple<uint32_t, uint16_t>;
101 struct ClassResolution : public ClassResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100102 ClassResolution() = default;
103 ClassResolution(const ClassResolution&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100104 ClassResolution(uint32_t type_idx, uint16_t access_flags)
105 : ClassResolutionBase(type_idx, access_flags) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100106
107 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
108 uint32_t GetDexTypeIndex() const { return std::get<0>(*this); }
109 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
110 };
111
112 using FieldResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>;
113 struct FieldResolution : public FieldResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100114 FieldResolution() = default;
115 FieldResolution(const FieldResolution&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100116 FieldResolution(uint32_t field_idx, uint16_t access_flags, uint32_t declaring_class_idx)
117 : FieldResolutionBase(field_idx, access_flags, declaring_class_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100118
119 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
120 uint32_t GetDexFieldIndex() const { return std::get<0>(*this); }
121 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
122 uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); }
123 };
124
125 using MethodResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>;
126 struct MethodResolution : public MethodResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100127 MethodResolution() = default;
128 MethodResolution(const MethodResolution&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100129 MethodResolution(uint32_t method_idx, uint16_t access_flags, uint32_t declaring_class_idx)
130 : MethodResolutionBase(method_idx, access_flags, declaring_class_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100131
132 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
133 uint32_t GetDexMethodIndex() const { return std::get<0>(*this); }
134 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
135 uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); }
136 };
137
138 using TypeAssignabilityBase = std::tuple<uint32_t, uint32_t>;
139 struct TypeAssignability : public std::tuple<uint32_t, uint32_t> {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100140 TypeAssignability() = default;
141 TypeAssignability(const TypeAssignability&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100142 TypeAssignability(uint32_t destination_idx, uint32_t source_idx)
143 : TypeAssignabilityBase(destination_idx, source_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100144
145 uint32_t GetDestination() const { return std::get<0>(*this); }
146 uint32_t GetSource() const { return std::get<1>(*this); }
147 };
148
149 // Data structure representing dependencies collected during verification of
150 // methods inside one DexFile.
151 struct DexFileDeps {
152 // Vector of strings which are not present in the corresponding DEX file.
153 // These are referred to with ids starting with `NumStringIds()` of that DexFile.
154 std::vector<std::string> strings_;
155
156 // Set of class pairs recording the outcome of assignability test from one
157 // of the two types to the other.
158 std::set<TypeAssignability> assignable_types_;
159 std::set<TypeAssignability> unassignable_types_;
160
161 // Sets of recorded class/field/method resolutions.
162 std::set<ClassResolution> classes_;
163 std::set<FieldResolution> fields_;
164 std::set<MethodResolution> direct_methods_;
165 std::set<MethodResolution> virtual_methods_;
166 std::set<MethodResolution> interface_methods_;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100167
168 bool Equals(const DexFileDeps& rhs) const;
David Brazdilca3c8c32016-09-06 14:04:48 +0100169 };
170
171 // Finds the DexFileDep instance associated with `dex_file`, or nullptr if
172 // `dex_file` is not reported as being compiled.
173 // We disable thread safety analysis. The method only reads the key set of
174 // `dex_deps_` which stays constant after initialization.
175 DexFileDeps* GetDexFileDeps(const DexFile& dex_file)
176 NO_THREAD_SAFETY_ANALYSIS;
177
178 // Returns true if `klass` is null or not defined in any of dex files which
179 // were reported as being compiled.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700180 bool IsInClassPath(ObjPtr<mirror::Class> klass)
David Brazdilca3c8c32016-09-06 14:04:48 +0100181 REQUIRES_SHARED(Locks::mutator_lock_);
182
183 // Returns the index of `str`. If it is defined in `dex_file_`, this is the dex
184 // string ID. If not, an ID is assigned to the string and cached in `strings_`
185 // of the corresponding DexFileDeps structure (either provided or inferred from
186 // `dex_file`).
187 uint32_t GetIdFromString(const DexFile& dex_file, const std::string& str)
188 REQUIRES(Locks::verifier_deps_lock_);
189
190 // Returns the string represented by `id`.
191 std::string GetStringFromId(const DexFile& dex_file, uint32_t string_id)
192 REQUIRES(Locks::verifier_deps_lock_);
193
194 // Returns the bytecode access flags of `element` (bottom 16 bits), or
195 // `kUnresolvedMarker` if `element` is null.
196 template <typename T>
197 uint16_t GetAccessFlags(T* element)
198 REQUIRES_SHARED(Locks::mutator_lock_);
199
200 // Returns a string ID of the descriptor of the declaring class of `element`,
201 // or `kUnresolvedMarker` if `element` is null.
202 template <typename T>
203 uint32_t GetDeclaringClassStringId(const DexFile& dex_file, T* element)
204 REQUIRES_SHARED(Locks::mutator_lock_)
205 REQUIRES(Locks::verifier_deps_lock_);
206
207 void AddClassResolution(const DexFile& dex_file,
208 uint16_t type_idx,
209 mirror::Class* klass)
210 REQUIRES_SHARED(Locks::mutator_lock_)
211 REQUIRES(!Locks::verifier_deps_lock_);
212
213 void AddFieldResolution(const DexFile& dex_file,
214 uint32_t field_idx,
215 ArtField* field)
216 REQUIRES_SHARED(Locks::mutator_lock_)
217 REQUIRES(!Locks::verifier_deps_lock_);
218
219 void AddMethodResolution(const DexFile& dex_file,
220 uint32_t method_idx,
221 MethodResolutionKind res_kind,
222 ArtMethod* method)
223 REQUIRES_SHARED(Locks::mutator_lock_)
224 REQUIRES(!Locks::verifier_deps_lock_);
225
226 void AddAssignability(const DexFile& dex_file,
227 mirror::Class* destination,
228 mirror::Class* source,
229 bool is_strict,
230 bool is_assignable)
231 REQUIRES_SHARED(Locks::mutator_lock_)
232 REQUIRES(!Locks::verifier_deps_lock_);
233
David Brazdil6f82fbd2016-09-14 11:55:26 +0100234 bool Equals(const VerifierDeps& rhs) const
235 REQUIRES(!Locks::verifier_deps_lock_);
236
David Brazdilca3c8c32016-09-06 14:04:48 +0100237 // Map from DexFiles into dependencies collected from verification of their methods.
238 std::map<const DexFile*, std::unique_ptr<DexFileDeps>> dex_deps_
239 GUARDED_BY(Locks::verifier_deps_lock_);
240
241 friend class VerifierDepsTest;
242 ART_FRIEND_TEST(VerifierDepsTest, StringToId);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100243 ART_FRIEND_TEST(VerifierDepsTest, EncodeDecode);
David Brazdilca3c8c32016-09-06 14:04:48 +0100244};
245
246} // namespace verifier
247} // namespace art
248
249#endif // ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_