blob: e35af41e3ff584be94fcf959c0cbedbfa694e387 [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"
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +010028#include "indenter.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010029#include "method_resolution_kind.h"
Nicolas Geoffray08025182016-10-25 17:20:18 +010030#include "method_verifier.h" // For MethodVerifier::FailureKind.
Mathieu Chartier3398c782016-09-30 10:27:43 -070031#include "obj_ptr.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010032#include "os.h"
33
34namespace art {
35namespace verifier {
36
37// Verification dependencies collector class used by the MethodVerifier to record
38// resolution outcomes and type assignability tests of classes/methods/fields
39// not present in the set of compiled DEX files, that is classes/methods/fields
40// defined in the classpath.
41// The compilation driver initializes the class and registers all DEX files
42// which are being compiled. Classes defined in DEX files outside of this set
43// (or synthesized classes without associated DEX files) are considered being
44// in the classpath.
45// During code-flow verification, the MethodVerifier informs the VerifierDeps
46// singleton about the outcome of every resolution and assignability test, and
47// the singleton records them if their outcome may change with changes in the
48// classpath.
49class VerifierDeps {
50 public:
51 explicit VerifierDeps(const std::vector<const DexFile*>& dex_files)
52 REQUIRES(!Locks::verifier_deps_lock_);
53
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000054 VerifierDeps(const std::vector<const DexFile*>& dex_files,
55 ArrayRef<const uint8_t> data)
56 REQUIRES(!Locks::verifier_deps_lock_);
57
Nicolas Geoffray08025182016-10-25 17:20:18 +010058 // Record the verification status of the class at `type_idx`.
59 static void MaybeRecordVerificationStatus(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -080060 dex::TypeIndex type_idx,
Nicolas Geoffray08025182016-10-25 17:20:18 +010061 MethodVerifier::FailureKind failure_kind)
62 REQUIRES(!Locks::verifier_deps_lock_);
63
David Brazdilca3c8c32016-09-06 14:04:48 +010064 // Record the outcome `klass` of resolving type `type_idx` from `dex_file`.
65 // If `klass` is null, the class is assumed unresolved.
66 static void MaybeRecordClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -080067 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +010068 mirror::Class* klass)
69 REQUIRES_SHARED(Locks::mutator_lock_)
70 REQUIRES(!Locks::verifier_deps_lock_);
71
72 // Record the outcome `field` of resolving field `field_idx` from `dex_file`.
73 // If `field` is null, the field is assumed unresolved.
74 static void MaybeRecordFieldResolution(const DexFile& dex_file,
75 uint32_t field_idx,
76 ArtField* field)
77 REQUIRES_SHARED(Locks::mutator_lock_)
78 REQUIRES(!Locks::verifier_deps_lock_);
79
80 // Record the outcome `method` of resolving method `method_idx` from `dex_file`
81 // using `res_kind` kind of method resolution algorithm. If `method` is null,
82 // the method is assumed unresolved.
83 static void MaybeRecordMethodResolution(const DexFile& dex_file,
84 uint32_t method_idx,
85 MethodResolutionKind res_kind,
86 ArtMethod* method)
87 REQUIRES_SHARED(Locks::mutator_lock_)
88 REQUIRES(!Locks::verifier_deps_lock_);
89
90 // Record the outcome `is_assignable` of type assignability test from `source`
91 // to `destination` as defined by RegType::AssignableFrom. `dex_file` is the
92 // owner of the method for which MethodVerifier performed the assignability test.
93 static void MaybeRecordAssignability(const DexFile& dex_file,
94 mirror::Class* destination,
95 mirror::Class* source,
96 bool is_strict,
97 bool is_assignable)
98 REQUIRES_SHARED(Locks::mutator_lock_)
99 REQUIRES(!Locks::verifier_deps_lock_);
100
David Brazdil6f82fbd2016-09-14 11:55:26 +0100101 // Serialize the recorded dependencies and store the data into `buffer`.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100102 // `dex_files` provides the order of the dex files in which the dependencies
103 // should be emitted.
104 void Encode(const std::vector<const DexFile*>& dex_files, std::vector<uint8_t>* buffer) const
David Brazdil6f82fbd2016-09-14 11:55:26 +0100105 REQUIRES(!Locks::verifier_deps_lock_);
106
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100107 // NO_THREAD_SAFETY_ANALYSIS as Dump iterates over dex_deps_, which is guarded by
108 // verifier_deps_lock_, but we expect Dump to be called once the deps collection is done.
109 void Dump(VariableIndentationOutputStream* vios) const
110 NO_THREAD_SAFETY_ANALYSIS;
111
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000112 // Verify the encoded dependencies of this `VerifierDeps` are still valid.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100113 // NO_THREAD_SAFETY_ANALYSIS, as this must be called on a read-only `VerifierDeps`.
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000114 bool ValidateDependencies(Handle<mirror::ClassLoader> class_loader, Thread* self) const
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100115 NO_THREAD_SAFETY_ANALYSIS;
116
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000117 // NO_THREAD_SAFETY_ANALSYS, as this is queried when the VerifierDeps are
118 // fully created.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800119 const std::vector<dex::TypeIndex>& GetUnverifiedClasses(const DexFile& dex_file) const
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000120 NO_THREAD_SAFETY_ANALYSIS {
121 return GetDexFileDeps(dex_file)->unverified_classes_;
122 }
123
David Brazdilca3c8c32016-09-06 14:04:48 +0100124 private:
125 static constexpr uint16_t kUnresolvedMarker = static_cast<uint16_t>(-1);
126
Andreas Gampea5b09a62016-11-17 15:21:22 -0800127 using ClassResolutionBase = std::tuple<dex::TypeIndex, uint16_t>;
David Brazdilca3c8c32016-09-06 14:04:48 +0100128 struct ClassResolution : public ClassResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100129 ClassResolution() = default;
130 ClassResolution(const ClassResolution&) = default;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800131 ClassResolution(dex::TypeIndex type_idx, uint16_t access_flags)
David Brazdilca3c8c32016-09-06 14:04:48 +0100132 : ClassResolutionBase(type_idx, access_flags) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100133
134 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
Andreas Gampea5b09a62016-11-17 15:21:22 -0800135 dex::TypeIndex GetDexTypeIndex() const { return std::get<0>(*this); }
David Brazdilca3c8c32016-09-06 14:04:48 +0100136 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
137 };
138
139 using FieldResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>;
140 struct FieldResolution : public FieldResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100141 FieldResolution() = default;
142 FieldResolution(const FieldResolution&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100143 FieldResolution(uint32_t field_idx, uint16_t access_flags, uint32_t declaring_class_idx)
144 : FieldResolutionBase(field_idx, access_flags, declaring_class_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100145
146 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
147 uint32_t GetDexFieldIndex() const { return std::get<0>(*this); }
148 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
149 uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); }
150 };
151
152 using MethodResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>;
153 struct MethodResolution : public MethodResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100154 MethodResolution() = default;
155 MethodResolution(const MethodResolution&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100156 MethodResolution(uint32_t method_idx, uint16_t access_flags, uint32_t declaring_class_idx)
157 : MethodResolutionBase(method_idx, access_flags, declaring_class_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100158
159 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
160 uint32_t GetDexMethodIndex() const { return std::get<0>(*this); }
161 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
162 uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); }
163 };
164
165 using TypeAssignabilityBase = std::tuple<uint32_t, uint32_t>;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100166 struct TypeAssignability : public TypeAssignabilityBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100167 TypeAssignability() = default;
168 TypeAssignability(const TypeAssignability&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100169 TypeAssignability(uint32_t destination_idx, uint32_t source_idx)
170 : TypeAssignabilityBase(destination_idx, source_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100171
172 uint32_t GetDestination() const { return std::get<0>(*this); }
173 uint32_t GetSource() const { return std::get<1>(*this); }
174 };
175
176 // Data structure representing dependencies collected during verification of
177 // methods inside one DexFile.
178 struct DexFileDeps {
179 // Vector of strings which are not present in the corresponding DEX file.
180 // These are referred to with ids starting with `NumStringIds()` of that DexFile.
181 std::vector<std::string> strings_;
182
183 // Set of class pairs recording the outcome of assignability test from one
184 // of the two types to the other.
185 std::set<TypeAssignability> assignable_types_;
186 std::set<TypeAssignability> unassignable_types_;
187
188 // Sets of recorded class/field/method resolutions.
189 std::set<ClassResolution> classes_;
190 std::set<FieldResolution> fields_;
191 std::set<MethodResolution> direct_methods_;
192 std::set<MethodResolution> virtual_methods_;
193 std::set<MethodResolution> interface_methods_;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100194
Nicolas Geoffray08025182016-10-25 17:20:18 +0100195 // List of classes that were not fully verified in that dex file.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800196 std::vector<dex::TypeIndex> unverified_classes_;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100197
David Brazdil6f82fbd2016-09-14 11:55:26 +0100198 bool Equals(const DexFileDeps& rhs) const;
David Brazdilca3c8c32016-09-06 14:04:48 +0100199 };
200
201 // Finds the DexFileDep instance associated with `dex_file`, or nullptr if
202 // `dex_file` is not reported as being compiled.
203 // We disable thread safety analysis. The method only reads the key set of
204 // `dex_deps_` which stays constant after initialization.
205 DexFileDeps* GetDexFileDeps(const DexFile& dex_file)
206 NO_THREAD_SAFETY_ANALYSIS;
207
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100208 const DexFileDeps* GetDexFileDeps(const DexFile& dex_file) const
209 NO_THREAD_SAFETY_ANALYSIS;
210
David Brazdilca3c8c32016-09-06 14:04:48 +0100211 // Returns true if `klass` is null or not defined in any of dex files which
212 // were reported as being compiled.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100213 bool IsInClassPath(ObjPtr<mirror::Class> klass) const
David Brazdilca3c8c32016-09-06 14:04:48 +0100214 REQUIRES_SHARED(Locks::mutator_lock_);
215
216 // Returns the index of `str`. If it is defined in `dex_file_`, this is the dex
217 // string ID. If not, an ID is assigned to the string and cached in `strings_`
218 // of the corresponding DexFileDeps structure (either provided or inferred from
219 // `dex_file`).
220 uint32_t GetIdFromString(const DexFile& dex_file, const std::string& str)
221 REQUIRES(Locks::verifier_deps_lock_);
222
223 // Returns the string represented by `id`.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100224 std::string GetStringFromId(const DexFile& dex_file, uint32_t string_id) const
David Brazdilca3c8c32016-09-06 14:04:48 +0100225 REQUIRES(Locks::verifier_deps_lock_);
226
227 // Returns the bytecode access flags of `element` (bottom 16 bits), or
228 // `kUnresolvedMarker` if `element` is null.
229 template <typename T>
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100230 static uint16_t GetAccessFlags(T* element)
David Brazdilca3c8c32016-09-06 14:04:48 +0100231 REQUIRES_SHARED(Locks::mutator_lock_);
232
233 // Returns a string ID of the descriptor of the declaring class of `element`,
234 // or `kUnresolvedMarker` if `element` is null.
Mathieu Chartier32b50302016-11-17 13:08:35 -0800235 uint32_t GetMethodDeclaringClassStringId(const DexFile& dex_file,
236 uint32_t dex_method_idx,
237 ArtMethod* method)
238 REQUIRES_SHARED(Locks::mutator_lock_)
239 REQUIRES(Locks::verifier_deps_lock_);
240 uint32_t GetFieldDeclaringClassStringId(const DexFile& dex_file,
241 uint32_t dex_field_idx,
242 ArtField* field)
243 REQUIRES_SHARED(Locks::mutator_lock_)
244 REQUIRES(Locks::verifier_deps_lock_);
245
246 // Returns a string ID of the descriptor of the class.
247 uint32_t GetClassDescriptorStringId(const DexFile& dex_file, ObjPtr<mirror::Class> klass)
David Brazdilca3c8c32016-09-06 14:04:48 +0100248 REQUIRES_SHARED(Locks::mutator_lock_)
249 REQUIRES(Locks::verifier_deps_lock_);
250
251 void AddClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800252 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +0100253 mirror::Class* klass)
254 REQUIRES_SHARED(Locks::mutator_lock_)
255 REQUIRES(!Locks::verifier_deps_lock_);
256
257 void AddFieldResolution(const DexFile& dex_file,
258 uint32_t field_idx,
259 ArtField* field)
260 REQUIRES_SHARED(Locks::mutator_lock_)
261 REQUIRES(!Locks::verifier_deps_lock_);
262
263 void AddMethodResolution(const DexFile& dex_file,
264 uint32_t method_idx,
265 MethodResolutionKind res_kind,
266 ArtMethod* method)
267 REQUIRES_SHARED(Locks::mutator_lock_)
268 REQUIRES(!Locks::verifier_deps_lock_);
269
270 void AddAssignability(const DexFile& dex_file,
271 mirror::Class* destination,
272 mirror::Class* source,
273 bool is_strict,
274 bool is_assignable)
275 REQUIRES_SHARED(Locks::mutator_lock_)
276 REQUIRES(!Locks::verifier_deps_lock_);
277
David Brazdil6f82fbd2016-09-14 11:55:26 +0100278 bool Equals(const VerifierDeps& rhs) const
279 REQUIRES(!Locks::verifier_deps_lock_);
280
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100281 // Verify `dex_file` according to the `deps`, that is going over each
282 // `DexFileDeps` field, and checking that the recorded information still
283 // holds.
284 bool VerifyDexFile(Handle<mirror::ClassLoader> class_loader,
285 const DexFile& dex_file,
286 const DexFileDeps& deps,
287 Thread* self) const
288 REQUIRES_SHARED(Locks::mutator_lock_)
289 REQUIRES(Locks::verifier_deps_lock_);
290
291 bool VerifyAssignability(Handle<mirror::ClassLoader> class_loader,
292 const DexFile& dex_file,
293 const std::set<TypeAssignability>& assignables,
294 bool expected_assignability,
295 Thread* self) const
296 REQUIRES_SHARED(Locks::mutator_lock_)
297 REQUIRES(Locks::verifier_deps_lock_);
298
299 // Verify that the set of resolved classes at the point of creation
300 // of this `VerifierDeps` is still the same.
301 bool VerifyClasses(Handle<mirror::ClassLoader> class_loader,
302 const DexFile& dex_file,
303 const std::set<ClassResolution>& classes,
304 Thread* self) const
305 REQUIRES_SHARED(Locks::mutator_lock_)
306 REQUIRES(Locks::verifier_deps_lock_);
307
308 // Verify that the set of resolved fields at the point of creation
309 // of this `VerifierDeps` is still the same, and each field resolves to the
310 // same field holder and access flags.
311 bool VerifyFields(Handle<mirror::ClassLoader> class_loader,
312 const DexFile& dex_file,
313 const std::set<FieldResolution>& classes,
314 Thread* self) const
315 REQUIRES_SHARED(Locks::mutator_lock_)
316 REQUIRES(Locks::verifier_deps_lock_);
317
318 // Verify that the set of resolved methods at the point of creation
319 // of this `VerifierDeps` is still the same, and each method resolves to the
320 // same method holder, access flags, and invocation kind.
321 bool VerifyMethods(Handle<mirror::ClassLoader> class_loader,
322 const DexFile& dex_file,
323 const std::set<MethodResolution>& methods,
324 MethodResolutionKind kind,
325 Thread* self) const
326 REQUIRES_SHARED(Locks::mutator_lock_)
327 REQUIRES(Locks::verifier_deps_lock_);
328
David Brazdilca3c8c32016-09-06 14:04:48 +0100329 // Map from DexFiles into dependencies collected from verification of their methods.
330 std::map<const DexFile*, std::unique_ptr<DexFileDeps>> dex_deps_
331 GUARDED_BY(Locks::verifier_deps_lock_);
332
333 friend class VerifierDepsTest;
334 ART_FRIEND_TEST(VerifierDepsTest, StringToId);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100335 ART_FRIEND_TEST(VerifierDepsTest, EncodeDecode);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100336 ART_FRIEND_TEST(VerifierDepsTest, EncodeDecodeMulti);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100337 ART_FRIEND_TEST(VerifierDepsTest, VerifyDeps);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000338 ART_FRIEND_TEST(VerifierDepsTest, CompilerDriver);
David Brazdilca3c8c32016-09-06 14:04:48 +0100339};
340
341} // namespace verifier
342} // namespace art
343
344#endif // ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_