David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 1 | /* |
| 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 Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 26 | #include "base/array_ref.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 27 | #include "base/mutex.h" |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 28 | #include "indenter.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 29 | #include "method_resolution_kind.h" |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 30 | #include "method_verifier.h" // For MethodVerifier::FailureKind. |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 31 | #include "obj_ptr.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 32 | #include "os.h" |
| 33 | |
| 34 | namespace art { |
| 35 | namespace 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. |
| 49 | class VerifierDeps { |
| 50 | public: |
| 51 | explicit VerifierDeps(const std::vector<const DexFile*>& dex_files) |
| 52 | REQUIRES(!Locks::verifier_deps_lock_); |
| 53 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame^] | 54 | VerifierDeps(const std::vector<const DexFile*>& dex_files, |
| 55 | ArrayRef<const uint8_t> data) |
| 56 | REQUIRES(!Locks::verifier_deps_lock_); |
| 57 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 58 | // Record the verification status of the class at `type_idx`. |
| 59 | static void MaybeRecordVerificationStatus(const DexFile& dex_file, |
| 60 | uint16_t type_idx, |
| 61 | MethodVerifier::FailureKind failure_kind) |
| 62 | REQUIRES(!Locks::verifier_deps_lock_); |
| 63 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 64 | // 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, |
| 67 | uint16_t type_idx, |
| 68 | 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 Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 101 | // Serialize the recorded dependencies and store the data into `buffer`. |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 102 | // `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 Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 105 | REQUIRES(!Locks::verifier_deps_lock_); |
| 106 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 107 | // 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 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 112 | private: |
| 113 | static constexpr uint16_t kUnresolvedMarker = static_cast<uint16_t>(-1); |
| 114 | |
| 115 | using ClassResolutionBase = std::tuple<uint32_t, uint16_t>; |
| 116 | struct ClassResolution : public ClassResolutionBase { |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 117 | ClassResolution() = default; |
| 118 | ClassResolution(const ClassResolution&) = default; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 119 | ClassResolution(uint32_t type_idx, uint16_t access_flags) |
| 120 | : ClassResolutionBase(type_idx, access_flags) {} |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 121 | |
| 122 | bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; } |
| 123 | uint32_t GetDexTypeIndex() const { return std::get<0>(*this); } |
| 124 | uint16_t GetAccessFlags() const { return std::get<1>(*this); } |
| 125 | }; |
| 126 | |
| 127 | using FieldResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>; |
| 128 | struct FieldResolution : public FieldResolutionBase { |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 129 | FieldResolution() = default; |
| 130 | FieldResolution(const FieldResolution&) = default; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 131 | FieldResolution(uint32_t field_idx, uint16_t access_flags, uint32_t declaring_class_idx) |
| 132 | : FieldResolutionBase(field_idx, access_flags, declaring_class_idx) {} |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 133 | |
| 134 | bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; } |
| 135 | uint32_t GetDexFieldIndex() const { return std::get<0>(*this); } |
| 136 | uint16_t GetAccessFlags() const { return std::get<1>(*this); } |
| 137 | uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); } |
| 138 | }; |
| 139 | |
| 140 | using MethodResolutionBase = std::tuple<uint32_t, uint16_t, uint32_t>; |
| 141 | struct MethodResolution : public MethodResolutionBase { |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 142 | MethodResolution() = default; |
| 143 | MethodResolution(const MethodResolution&) = default; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 144 | MethodResolution(uint32_t method_idx, uint16_t access_flags, uint32_t declaring_class_idx) |
| 145 | : MethodResolutionBase(method_idx, access_flags, declaring_class_idx) {} |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 146 | |
| 147 | bool IsResolved() const { return GetAccessFlags() != kUnresolvedMarker; } |
| 148 | uint32_t GetDexMethodIndex() const { return std::get<0>(*this); } |
| 149 | uint16_t GetAccessFlags() const { return std::get<1>(*this); } |
| 150 | uint32_t GetDeclaringClassIndex() const { return std::get<2>(*this); } |
| 151 | }; |
| 152 | |
| 153 | using TypeAssignabilityBase = std::tuple<uint32_t, uint32_t>; |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 154 | struct TypeAssignability : public TypeAssignabilityBase { |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 155 | TypeAssignability() = default; |
| 156 | TypeAssignability(const TypeAssignability&) = default; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 157 | TypeAssignability(uint32_t destination_idx, uint32_t source_idx) |
| 158 | : TypeAssignabilityBase(destination_idx, source_idx) {} |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 159 | |
| 160 | uint32_t GetDestination() const { return std::get<0>(*this); } |
| 161 | uint32_t GetSource() const { return std::get<1>(*this); } |
| 162 | }; |
| 163 | |
| 164 | // Data structure representing dependencies collected during verification of |
| 165 | // methods inside one DexFile. |
| 166 | struct DexFileDeps { |
| 167 | // Vector of strings which are not present in the corresponding DEX file. |
| 168 | // These are referred to with ids starting with `NumStringIds()` of that DexFile. |
| 169 | std::vector<std::string> strings_; |
| 170 | |
| 171 | // Set of class pairs recording the outcome of assignability test from one |
| 172 | // of the two types to the other. |
| 173 | std::set<TypeAssignability> assignable_types_; |
| 174 | std::set<TypeAssignability> unassignable_types_; |
| 175 | |
| 176 | // Sets of recorded class/field/method resolutions. |
| 177 | std::set<ClassResolution> classes_; |
| 178 | std::set<FieldResolution> fields_; |
| 179 | std::set<MethodResolution> direct_methods_; |
| 180 | std::set<MethodResolution> virtual_methods_; |
| 181 | std::set<MethodResolution> interface_methods_; |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 182 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 183 | // List of classes that were not fully verified in that dex file. |
| 184 | std::vector<uint16_t> unverified_classes_; |
| 185 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 186 | bool Equals(const DexFileDeps& rhs) const; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | // Finds the DexFileDep instance associated with `dex_file`, or nullptr if |
| 190 | // `dex_file` is not reported as being compiled. |
| 191 | // We disable thread safety analysis. The method only reads the key set of |
| 192 | // `dex_deps_` which stays constant after initialization. |
| 193 | DexFileDeps* GetDexFileDeps(const DexFile& dex_file) |
| 194 | NO_THREAD_SAFETY_ANALYSIS; |
| 195 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 196 | const DexFileDeps* GetDexFileDeps(const DexFile& dex_file) const |
| 197 | NO_THREAD_SAFETY_ANALYSIS; |
| 198 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 199 | // Returns true if `klass` is null or not defined in any of dex files which |
| 200 | // were reported as being compiled. |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 201 | bool IsInClassPath(ObjPtr<mirror::Class> klass) const |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 202 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 203 | |
| 204 | // Returns the index of `str`. If it is defined in `dex_file_`, this is the dex |
| 205 | // string ID. If not, an ID is assigned to the string and cached in `strings_` |
| 206 | // of the corresponding DexFileDeps structure (either provided or inferred from |
| 207 | // `dex_file`). |
| 208 | uint32_t GetIdFromString(const DexFile& dex_file, const std::string& str) |
| 209 | REQUIRES(Locks::verifier_deps_lock_); |
| 210 | |
| 211 | // Returns the string represented by `id`. |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 212 | std::string GetStringFromId(const DexFile& dex_file, uint32_t string_id) const |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 213 | REQUIRES(Locks::verifier_deps_lock_); |
| 214 | |
| 215 | // Returns the bytecode access flags of `element` (bottom 16 bits), or |
| 216 | // `kUnresolvedMarker` if `element` is null. |
| 217 | template <typename T> |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 218 | static uint16_t GetAccessFlags(T* element) |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 219 | 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. |
| 223 | template <typename T> |
| 224 | uint32_t GetDeclaringClassStringId(const DexFile& dex_file, T* element) |
| 225 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 226 | REQUIRES(Locks::verifier_deps_lock_); |
| 227 | |
| 228 | void AddClassResolution(const DexFile& dex_file, |
| 229 | uint16_t type_idx, |
| 230 | mirror::Class* klass) |
| 231 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 232 | REQUIRES(!Locks::verifier_deps_lock_); |
| 233 | |
| 234 | void AddFieldResolution(const DexFile& dex_file, |
| 235 | uint32_t field_idx, |
| 236 | ArtField* field) |
| 237 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 238 | REQUIRES(!Locks::verifier_deps_lock_); |
| 239 | |
| 240 | void AddMethodResolution(const DexFile& dex_file, |
| 241 | uint32_t method_idx, |
| 242 | MethodResolutionKind res_kind, |
| 243 | ArtMethod* method) |
| 244 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 245 | REQUIRES(!Locks::verifier_deps_lock_); |
| 246 | |
| 247 | void AddAssignability(const DexFile& dex_file, |
| 248 | mirror::Class* destination, |
| 249 | mirror::Class* source, |
| 250 | bool is_strict, |
| 251 | bool is_assignable) |
| 252 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 253 | REQUIRES(!Locks::verifier_deps_lock_); |
| 254 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 255 | bool Equals(const VerifierDeps& rhs) const |
| 256 | REQUIRES(!Locks::verifier_deps_lock_); |
| 257 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 258 | // Map from DexFiles into dependencies collected from verification of their methods. |
| 259 | std::map<const DexFile*, std::unique_ptr<DexFileDeps>> dex_deps_ |
| 260 | GUARDED_BY(Locks::verifier_deps_lock_); |
| 261 | |
| 262 | friend class VerifierDepsTest; |
| 263 | ART_FRIEND_TEST(VerifierDepsTest, StringToId); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 264 | ART_FRIEND_TEST(VerifierDepsTest, EncodeDecode); |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 265 | ART_FRIEND_TEST(VerifierDepsTest, EncodeDecodeMulti); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | } // namespace verifier |
| 269 | } // namespace art |
| 270 | |
| 271 | #endif // ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_ |