blob: a12071b8b6c89324176be349a851e0118c16cf51 [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.
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000045// During code-flow verification, the MethodVerifier informs VerifierDeps
46// about the outcome of every resolution and assignability test, and
47// the VerifierDeps object records them if their outcome may change with
48// changes in the classpath.
David Brazdilca3c8c32016-09-06 14:04:48 +010049class VerifierDeps {
50 public:
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000051 explicit VerifierDeps(const std::vector<const DexFile*>& dex_files);
David Brazdilca3c8c32016-09-06 14:04:48 +010052
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000053 VerifierDeps(const std::vector<const DexFile*>& dex_files, ArrayRef<const uint8_t> data);
54
55 // Merge `other` into this `VerifierDeps`'. `other` and `this` must be for the
56 // same set of dex files.
57 void MergeWith(const VerifierDeps& other, const std::vector<const DexFile*>& dex_files);
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000058
Nicolas Geoffray08025182016-10-25 17:20:18 +010059 // Record the verification status of the class at `type_idx`.
60 static void MaybeRecordVerificationStatus(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -080061 dex::TypeIndex type_idx,
Nicolas Geoffray08025182016-10-25 17:20:18 +010062 MethodVerifier::FailureKind failure_kind)
63 REQUIRES(!Locks::verifier_deps_lock_);
64
David Brazdilca3c8c32016-09-06 14:04:48 +010065 // Record the outcome `klass` of resolving type `type_idx` from `dex_file`.
66 // If `klass` is null, the class is assumed unresolved.
67 static void MaybeRecordClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -080068 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +010069 mirror::Class* klass)
70 REQUIRES_SHARED(Locks::mutator_lock_)
71 REQUIRES(!Locks::verifier_deps_lock_);
72
73 // Record the outcome `field` of resolving field `field_idx` from `dex_file`.
74 // If `field` is null, the field is assumed unresolved.
75 static void MaybeRecordFieldResolution(const DexFile& dex_file,
76 uint32_t field_idx,
77 ArtField* field)
78 REQUIRES_SHARED(Locks::mutator_lock_)
79 REQUIRES(!Locks::verifier_deps_lock_);
80
81 // Record the outcome `method` of resolving method `method_idx` from `dex_file`
82 // using `res_kind` kind of method resolution algorithm. If `method` is null,
83 // the method is assumed unresolved.
84 static void MaybeRecordMethodResolution(const DexFile& dex_file,
85 uint32_t method_idx,
86 MethodResolutionKind res_kind,
87 ArtMethod* method)
88 REQUIRES_SHARED(Locks::mutator_lock_)
89 REQUIRES(!Locks::verifier_deps_lock_);
90
91 // Record the outcome `is_assignable` of type assignability test from `source`
92 // to `destination` as defined by RegType::AssignableFrom. `dex_file` is the
93 // owner of the method for which MethodVerifier performed the assignability test.
94 static void MaybeRecordAssignability(const DexFile& dex_file,
95 mirror::Class* destination,
96 mirror::Class* source,
97 bool is_strict,
98 bool is_assignable)
99 REQUIRES_SHARED(Locks::mutator_lock_)
100 REQUIRES(!Locks::verifier_deps_lock_);
101
David Brazdil6f82fbd2016-09-14 11:55:26 +0100102 // Serialize the recorded dependencies and store the data into `buffer`.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100103 // `dex_files` provides the order of the dex files in which the dependencies
104 // should be emitted.
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000105 void Encode(const std::vector<const DexFile*>& dex_files, std::vector<uint8_t>* buffer) const;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100106
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000107 void Dump(VariableIndentationOutputStream* vios) const;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100108
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000109 // Verify the encoded dependencies of this `VerifierDeps` are still valid.
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000110 bool ValidateDependencies(Handle<mirror::ClassLoader> class_loader, Thread* self) const
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000111 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100112
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000113 const std::vector<dex::TypeIndex>& GetUnverifiedClasses(const DexFile& dex_file) const {
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000114 return GetDexFileDeps(dex_file)->unverified_classes_;
115 }
116
David Brazdilca3c8c32016-09-06 14:04:48 +0100117 private:
118 static constexpr uint16_t kUnresolvedMarker = static_cast<uint16_t>(-1);
119
Andreas Gampea5b09a62016-11-17 15:21:22 -0800120 using ClassResolutionBase = std::tuple<dex::TypeIndex, uint16_t>;
David Brazdilca3c8c32016-09-06 14:04:48 +0100121 struct ClassResolution : public ClassResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100122 ClassResolution() = default;
123 ClassResolution(const ClassResolution&) = default;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800124 ClassResolution(dex::TypeIndex type_idx, uint16_t access_flags)
David Brazdilca3c8c32016-09-06 14:04:48 +0100125 : ClassResolutionBase(type_idx, access_flags) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100126
127 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
Andreas Gampea5b09a62016-11-17 15:21:22 -0800128 dex::TypeIndex GetDexTypeIndex() const { return std::get<0>(*this); }
David Brazdilca3c8c32016-09-06 14:04:48 +0100129 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
130 };
131
132 using FieldResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>;
133 struct FieldResolution : public FieldResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100134 FieldResolution() = default;
135 FieldResolution(const FieldResolution&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100136 FieldResolution(uint32_t field_idx, uint16_t access_flags, uint32_t declaring_class_idx)
137 : FieldResolutionBase(field_idx, access_flags, declaring_class_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100138
139 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
140 uint32_t GetDexFieldIndex() const { return std::get<0>(*this); }
141 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
142 uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); }
143 };
144
145 using MethodResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>;
146 struct MethodResolution : public MethodResolutionBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100147 MethodResolution() = default;
148 MethodResolution(const MethodResolution&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100149 MethodResolution(uint32_t method_idx, uint16_t access_flags, uint32_t declaring_class_idx)
150 : MethodResolutionBase(method_idx, access_flags, declaring_class_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100151
152 bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; }
153 uint32_t GetDexMethodIndex() const { return std::get<0>(*this); }
154 uint16_t GetAccessFlags() const { return std::get<1>(*this); }
155 uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); }
156 };
157
158 using TypeAssignabilityBase = std::tuple<uint32_t, uint32_t>;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100159 struct TypeAssignability : public TypeAssignabilityBase {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100160 TypeAssignability() = default;
161 TypeAssignability(const TypeAssignability&) = default;
David Brazdilca3c8c32016-09-06 14:04:48 +0100162 TypeAssignability(uint32_t destination_idx, uint32_t source_idx)
163 : TypeAssignabilityBase(destination_idx, source_idx) {}
David Brazdilca3c8c32016-09-06 14:04:48 +0100164
165 uint32_t GetDestination() const { return std::get<0>(*this); }
166 uint32_t GetSource() const { return std::get<1>(*this); }
167 };
168
169 // Data structure representing dependencies collected during verification of
170 // methods inside one DexFile.
171 struct DexFileDeps {
172 // Vector of strings which are not present in the corresponding DEX file.
173 // These are referred to with ids starting with `NumStringIds()` of that DexFile.
174 std::vector<std::string> strings_;
175
176 // Set of class pairs recording the outcome of assignability test from one
177 // of the two types to the other.
178 std::set<TypeAssignability> assignable_types_;
179 std::set<TypeAssignability> unassignable_types_;
180
181 // Sets of recorded class/field/method resolutions.
182 std::set<ClassResolution> classes_;
183 std::set<FieldResolution> fields_;
184 std::set<MethodResolution> direct_methods_;
185 std::set<MethodResolution> virtual_methods_;
186 std::set<MethodResolution> interface_methods_;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100187
Nicolas Geoffray08025182016-10-25 17:20:18 +0100188 // List of classes that were not fully verified in that dex file.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800189 std::vector<dex::TypeIndex> unverified_classes_;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100190
David Brazdil6f82fbd2016-09-14 11:55:26 +0100191 bool Equals(const DexFileDeps& rhs) const;
David Brazdilca3c8c32016-09-06 14:04:48 +0100192 };
193
194 // Finds the DexFileDep instance associated with `dex_file`, or nullptr if
195 // `dex_file` is not reported as being compiled.
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000196 DexFileDeps* GetDexFileDeps(const DexFile& dex_file);
David Brazdilca3c8c32016-09-06 14:04:48 +0100197
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000198 const DexFileDeps* GetDexFileDeps(const DexFile& dex_file) const;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100199
David Brazdilca3c8c32016-09-06 14:04:48 +0100200 // Returns true if `klass` is null or not defined in any of dex files which
201 // were reported as being compiled.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100202 bool IsInClassPath(ObjPtr<mirror::Class> klass) const
David Brazdilca3c8c32016-09-06 14:04:48 +0100203 REQUIRES_SHARED(Locks::mutator_lock_);
204
205 // Returns the index of `str`. If it is defined in `dex_file_`, this is the dex
206 // string ID. If not, an ID is assigned to the string and cached in `strings_`
207 // of the corresponding DexFileDeps structure (either provided or inferred from
208 // `dex_file`).
209 uint32_t GetIdFromString(const DexFile& dex_file, const std::string& str)
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000210 REQUIRES(!Locks::verifier_deps_lock_);
David Brazdilca3c8c32016-09-06 14:04:48 +0100211
212 // Returns the string represented by `id`.
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000213 std::string GetStringFromId(const DexFile& dex_file, uint32_t string_id) const;
David Brazdilca3c8c32016-09-06 14:04:48 +0100214
215 // Returns the bytecode access flags of `element` (bottom 16 bits), or
216 // `kUnresolvedMarker` if `element` is null.
217 template <typename T>
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100218 static uint16_t GetAccessFlags(T* element)
David Brazdilca3c8c32016-09-06 14:04:48 +0100219 REQUIRES_SHARED(Locks::mutator_lock_);
220
221 // Returns a string ID of the descriptor of the declaring class of `element`,
222 // or `kUnresolvedMarker` if `element` is null.
Mathieu Chartier32b50302016-11-17 13:08:35 -0800223 uint32_t GetMethodDeclaringClassStringId(const DexFile& dex_file,
224 uint32_t dex_method_idx,
225 ArtMethod* method)
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000226 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier32b50302016-11-17 13:08:35 -0800227 uint32_t GetFieldDeclaringClassStringId(const DexFile& dex_file,
228 uint32_t dex_field_idx,
229 ArtField* field)
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000230 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier32b50302016-11-17 13:08:35 -0800231
232 // Returns a string ID of the descriptor of the class.
233 uint32_t GetClassDescriptorStringId(const DexFile& dex_file, ObjPtr<mirror::Class> klass)
David Brazdilca3c8c32016-09-06 14:04:48 +0100234 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000235 REQUIRES(!Locks::verifier_deps_lock_);
David Brazdilca3c8c32016-09-06 14:04:48 +0100236
237 void AddClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800238 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +0100239 mirror::Class* klass)
240 REQUIRES_SHARED(Locks::mutator_lock_)
241 REQUIRES(!Locks::verifier_deps_lock_);
242
243 void AddFieldResolution(const DexFile& dex_file,
244 uint32_t field_idx,
245 ArtField* field)
246 REQUIRES_SHARED(Locks::mutator_lock_)
247 REQUIRES(!Locks::verifier_deps_lock_);
248
249 void AddMethodResolution(const DexFile& dex_file,
250 uint32_t method_idx,
251 MethodResolutionKind res_kind,
252 ArtMethod* method)
253 REQUIRES_SHARED(Locks::mutator_lock_)
254 REQUIRES(!Locks::verifier_deps_lock_);
255
256 void AddAssignability(const DexFile& dex_file,
257 mirror::Class* destination,
258 mirror::Class* source,
259 bool is_strict,
260 bool is_assignable)
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000261 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdilca3c8c32016-09-06 14:04:48 +0100262
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000263 bool Equals(const VerifierDeps& rhs) const;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100264
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100265 // Verify `dex_file` according to the `deps`, that is going over each
266 // `DexFileDeps` field, and checking that the recorded information still
267 // holds.
268 bool VerifyDexFile(Handle<mirror::ClassLoader> class_loader,
269 const DexFile& dex_file,
270 const DexFileDeps& deps,
271 Thread* self) const
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000272 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100273
274 bool VerifyAssignability(Handle<mirror::ClassLoader> class_loader,
275 const DexFile& dex_file,
276 const std::set<TypeAssignability>& assignables,
277 bool expected_assignability,
278 Thread* self) const
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000279 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100280
281 // Verify that the set of resolved classes at the point of creation
282 // of this `VerifierDeps` is still the same.
283 bool VerifyClasses(Handle<mirror::ClassLoader> class_loader,
284 const DexFile& dex_file,
285 const std::set<ClassResolution>& classes,
286 Thread* self) const
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000287 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100288
289 // Verify that the set of resolved fields at the point of creation
290 // of this `VerifierDeps` is still the same, and each field resolves to the
291 // same field holder and access flags.
292 bool VerifyFields(Handle<mirror::ClassLoader> class_loader,
293 const DexFile& dex_file,
294 const std::set<FieldResolution>& classes,
295 Thread* self) const
296 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000297 REQUIRES(!Locks::verifier_deps_lock_);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100298
299 // Verify that the set of resolved methods at the point of creation
300 // of this `VerifierDeps` is still the same, and each method resolves to the
301 // same method holder, access flags, and invocation kind.
302 bool VerifyMethods(Handle<mirror::ClassLoader> class_loader,
303 const DexFile& dex_file,
304 const std::set<MethodResolution>& methods,
305 MethodResolutionKind kind,
306 Thread* self) const
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000307 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100308
David Brazdilca3c8c32016-09-06 14:04:48 +0100309 // Map from DexFiles into dependencies collected from verification of their methods.
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000310 std::map<const DexFile*, std::unique_ptr<DexFileDeps>> dex_deps_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100311
312 friend class VerifierDepsTest;
313 ART_FRIEND_TEST(VerifierDepsTest, StringToId);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100314 ART_FRIEND_TEST(VerifierDepsTest, EncodeDecode);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100315 ART_FRIEND_TEST(VerifierDepsTest, EncodeDecodeMulti);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100316 ART_FRIEND_TEST(VerifierDepsTest, VerifyDeps);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000317 ART_FRIEND_TEST(VerifierDepsTest, CompilerDriver);
David Brazdilca3c8c32016-09-06 14:04:48 +0100318};
319
320} // namespace verifier
321} // namespace art
322
323#endif // ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_