1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include <map> 21*795d594fSAndroid Build Coastguard Worker #include <set> 22*795d594fSAndroid Build Coastguard Worker #include <vector> 23*795d594fSAndroid Build Coastguard Worker 24*795d594fSAndroid Build Coastguard Worker #include "base/array_ref.h" 25*795d594fSAndroid Build Coastguard Worker #include "base/locks.h" 26*795d594fSAndroid Build Coastguard Worker #include "base/macros.h" 27*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_structs.h" 28*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_types.h" 29*795d594fSAndroid Build Coastguard Worker #include "handle.h" 30*795d594fSAndroid Build Coastguard Worker #include "obj_ptr.h" 31*795d594fSAndroid Build Coastguard Worker #include "thread.h" 32*795d594fSAndroid Build Coastguard Worker #include "verifier_enums.h" // For MethodVerifier::FailureKind. 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN { 35*795d594fSAndroid Build Coastguard Worker 36*795d594fSAndroid Build Coastguard Worker class ArtField; 37*795d594fSAndroid Build Coastguard Worker class ArtMethod; 38*795d594fSAndroid Build Coastguard Worker class DexFile; 39*795d594fSAndroid Build Coastguard Worker class VariableIndentationOutputStream; 40*795d594fSAndroid Build Coastguard Worker 41*795d594fSAndroid Build Coastguard Worker namespace mirror { 42*795d594fSAndroid Build Coastguard Worker class Class; 43*795d594fSAndroid Build Coastguard Worker class ClassLoader; 44*795d594fSAndroid Build Coastguard Worker } // namespace mirror 45*795d594fSAndroid Build Coastguard Worker 46*795d594fSAndroid Build Coastguard Worker namespace verifier { 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker class RegType; 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker // Verification dependencies collector class used by the MethodVerifier to record 51*795d594fSAndroid Build Coastguard Worker // resolution outcomes and type assignability tests of classes/methods/fields 52*795d594fSAndroid Build Coastguard Worker // not present in the set of compiled DEX files, that is classes/methods/fields 53*795d594fSAndroid Build Coastguard Worker // defined in the classpath. 54*795d594fSAndroid Build Coastguard Worker // The compilation driver initializes the class and registers all DEX files 55*795d594fSAndroid Build Coastguard Worker // which are being compiled. Classes defined in DEX files outside of this set 56*795d594fSAndroid Build Coastguard Worker // (or synthesized classes without associated DEX files) are considered being 57*795d594fSAndroid Build Coastguard Worker // in the classpath. 58*795d594fSAndroid Build Coastguard Worker // During code-flow verification, the MethodVerifier informs VerifierDeps 59*795d594fSAndroid Build Coastguard Worker // about the outcome of every resolution and assignability test, and 60*795d594fSAndroid Build Coastguard Worker // the VerifierDeps object records them if their outcome may change with 61*795d594fSAndroid Build Coastguard Worker // changes in the classpath. 62*795d594fSAndroid Build Coastguard Worker class VerifierDeps { 63*795d594fSAndroid Build Coastguard Worker public: 64*795d594fSAndroid Build Coastguard Worker EXPORT explicit VerifierDeps(const std::vector<const DexFile*>& dex_files, 65*795d594fSAndroid Build Coastguard Worker bool output_only = true); 66*795d594fSAndroid Build Coastguard Worker 67*795d594fSAndroid Build Coastguard Worker // Marker to know whether a class is verified. A non-verified class will have 68*795d594fSAndroid Build Coastguard Worker // this marker as its offset entry in the encoded data. 69*795d594fSAndroid Build Coastguard Worker static uint32_t constexpr kNotVerifiedMarker = std::numeric_limits<uint32_t>::max(); 70*795d594fSAndroid Build Coastguard Worker 71*795d594fSAndroid Build Coastguard Worker // Fill dependencies from stored data. Returns true on success, false on failure. 72*795d594fSAndroid Build Coastguard Worker EXPORT bool ParseStoredData(const std::vector<const DexFile*>& dex_files, 73*795d594fSAndroid Build Coastguard Worker ArrayRef<const uint8_t> data); 74*795d594fSAndroid Build Coastguard Worker 75*795d594fSAndroid Build Coastguard Worker // Merge `other` into this `VerifierDeps`'. `other` and `this` must be for the 76*795d594fSAndroid Build Coastguard Worker // same set of dex files. 77*795d594fSAndroid Build Coastguard Worker EXPORT void MergeWith(std::unique_ptr<VerifierDeps> other, 78*795d594fSAndroid Build Coastguard Worker const std::vector<const DexFile*>& dex_files); 79*795d594fSAndroid Build Coastguard Worker 80*795d594fSAndroid Build Coastguard Worker // Record information that a class was verified. 81*795d594fSAndroid Build Coastguard Worker // Note that this function is different from MaybeRecordVerificationStatus() which 82*795d594fSAndroid Build Coastguard Worker // looks up thread-local VerifierDeps first. 83*795d594fSAndroid Build Coastguard Worker void RecordClassVerified(const DexFile& dex_file, const dex::ClassDef& class_def) 84*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::verifier_deps_lock_); 85*795d594fSAndroid Build Coastguard Worker 86*795d594fSAndroid Build Coastguard Worker // Record the verification status of the class defined in `class_def`. 87*795d594fSAndroid Build Coastguard Worker EXPORT static void MaybeRecordVerificationStatus(VerifierDeps* verifier_deps, 88*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file, 89*795d594fSAndroid Build Coastguard Worker const dex::ClassDef& class_def, 90*795d594fSAndroid Build Coastguard Worker FailureKind failure_kind) 91*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::verifier_deps_lock_); 92*795d594fSAndroid Build Coastguard Worker 93*795d594fSAndroid Build Coastguard Worker // Record the outcome `is_assignable` of type assignability test from `source` 94*795d594fSAndroid Build Coastguard Worker // to `destination` as defined by RegType::AssignableFrom. `dex_file` is the 95*795d594fSAndroid Build Coastguard Worker // owner of the method for which MethodVerifier performed the assignability test. 96*795d594fSAndroid Build Coastguard Worker static void MaybeRecordAssignability(VerifierDeps* verifier_deps, 97*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file, 98*795d594fSAndroid Build Coastguard Worker const dex::ClassDef& class_def, 99*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> destination, 100*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> source) 101*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) 102*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::verifier_deps_lock_); 103*795d594fSAndroid Build Coastguard Worker 104*795d594fSAndroid Build Coastguard Worker // Record that `source` is assignable to `destination`. `dex_file` is the 105*795d594fSAndroid Build Coastguard Worker // owner of the method for which MethodVerifier performed the assignability test. 106*795d594fSAndroid Build Coastguard Worker static void MaybeRecordAssignability(VerifierDeps* verifier_deps, 107*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file, 108*795d594fSAndroid Build Coastguard Worker const dex::ClassDef& class_def, 109*795d594fSAndroid Build Coastguard Worker const RegType& destination, 110*795d594fSAndroid Build Coastguard Worker const RegType& source) 111*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) 112*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::verifier_deps_lock_); 113*795d594fSAndroid Build Coastguard Worker 114*795d594fSAndroid Build Coastguard Worker // Serialize the recorded dependencies and store the data into `buffer`. 115*795d594fSAndroid Build Coastguard Worker // `dex_files` provides the order of the dex files in which the dependencies 116*795d594fSAndroid Build Coastguard Worker // should be emitted. 117*795d594fSAndroid Build Coastguard Worker EXPORT void Encode(const std::vector<const DexFile*>& dex_files, 118*795d594fSAndroid Build Coastguard Worker std::vector<uint8_t>* buffer) const; 119*795d594fSAndroid Build Coastguard Worker 120*795d594fSAndroid Build Coastguard Worker EXPORT void Dump(VariableIndentationOutputStream* vios) const; 121*795d594fSAndroid Build Coastguard Worker 122*795d594fSAndroid Build Coastguard Worker // Verifies the encoded dependencies of this `VerifierDeps` are still valid. 123*795d594fSAndroid Build Coastguard Worker // Returns whether all dependencies were validated. 124*795d594fSAndroid Build Coastguard Worker EXPORT bool ValidateDependenciesAndUpdateStatus( 125*795d594fSAndroid Build Coastguard Worker Thread* self, 126*795d594fSAndroid Build Coastguard Worker Handle<mirror::ClassLoader> class_loader, 127*795d594fSAndroid Build Coastguard Worker const std::vector<const DexFile*>& dex_files) 128*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 129*795d594fSAndroid Build Coastguard Worker GetVerifiedClasses(const DexFile & dex_file)130*795d594fSAndroid Build Coastguard Worker const std::vector<bool>& GetVerifiedClasses(const DexFile& dex_file) const { 131*795d594fSAndroid Build Coastguard Worker return GetDexFileDeps(dex_file)->verified_classes_; 132*795d594fSAndroid Build Coastguard Worker } 133*795d594fSAndroid Build Coastguard Worker 134*795d594fSAndroid Build Coastguard Worker // Whether this `verifier_deps` has recorded that the given class is verified. 135*795d594fSAndroid Build Coastguard Worker bool HasRecordedVerifiedStatus(const DexFile& dex_file, const dex::ClassDef& class_def) 136*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::verifier_deps_lock_); 137*795d594fSAndroid Build Coastguard Worker OutputOnly()138*795d594fSAndroid Build Coastguard Worker bool OutputOnly() const { 139*795d594fSAndroid Build Coastguard Worker return output_only_; 140*795d594fSAndroid Build Coastguard Worker } 141*795d594fSAndroid Build Coastguard Worker ContainsDexFile(const DexFile & dex_file)142*795d594fSAndroid Build Coastguard Worker bool ContainsDexFile(const DexFile& dex_file) const { 143*795d594fSAndroid Build Coastguard Worker return GetDexFileDeps(dex_file) != nullptr; 144*795d594fSAndroid Build Coastguard Worker } 145*795d594fSAndroid Build Coastguard Worker 146*795d594fSAndroid Build Coastguard Worker // Parses raw VerifierDeps data to extract bitvectors of which class def indices 147*795d594fSAndroid Build Coastguard Worker // were verified or not. The given `dex_files` must match the order and count of 148*795d594fSAndroid Build Coastguard Worker // dex files used to create the VerifierDeps. 149*795d594fSAndroid Build Coastguard Worker static bool ParseVerifiedClasses( 150*795d594fSAndroid Build Coastguard Worker const std::vector<const DexFile*>& dex_files, 151*795d594fSAndroid Build Coastguard Worker ArrayRef<const uint8_t> data, 152*795d594fSAndroid Build Coastguard Worker /*out*/std::vector<std::vector<bool>>* verified_classes_per_dex); 153*795d594fSAndroid Build Coastguard Worker 154*795d594fSAndroid Build Coastguard Worker using TypeAssignabilityBase = std::tuple<dex::StringIndex, dex::StringIndex>; 155*795d594fSAndroid Build Coastguard Worker struct TypeAssignability : public TypeAssignabilityBase { 156*795d594fSAndroid Build Coastguard Worker TypeAssignability() = default; 157*795d594fSAndroid Build Coastguard Worker TypeAssignability(const TypeAssignability&) = default; TypeAssignabilityTypeAssignability158*795d594fSAndroid Build Coastguard Worker TypeAssignability(dex::StringIndex destination_idx, dex::StringIndex source_idx) 159*795d594fSAndroid Build Coastguard Worker : TypeAssignabilityBase(destination_idx, source_idx) {} 160*795d594fSAndroid Build Coastguard Worker GetDestinationTypeAssignability161*795d594fSAndroid Build Coastguard Worker dex::StringIndex GetDestination() const { return std::get<0>(*this); } GetSourceTypeAssignability162*795d594fSAndroid Build Coastguard Worker dex::StringIndex GetSource() const { return std::get<1>(*this); } 163*795d594fSAndroid Build Coastguard Worker }; 164*795d594fSAndroid Build Coastguard Worker 165*795d594fSAndroid Build Coastguard Worker private: 166*795d594fSAndroid Build Coastguard Worker // Data structure representing dependencies collected during verification of 167*795d594fSAndroid Build Coastguard Worker // methods inside one DexFile. 168*795d594fSAndroid Build Coastguard Worker struct DexFileDeps { DexFileDepsDexFileDeps169*795d594fSAndroid Build Coastguard Worker explicit DexFileDeps(size_t num_class_defs) 170*795d594fSAndroid Build Coastguard Worker : assignable_types_(num_class_defs), 171*795d594fSAndroid Build Coastguard Worker verified_classes_(num_class_defs) {} 172*795d594fSAndroid Build Coastguard Worker 173*795d594fSAndroid Build Coastguard Worker // Vector of strings which are not present in the corresponding DEX file. 174*795d594fSAndroid Build Coastguard Worker // These are referred to with ids starting with `NumStringIds()` of that DexFile. 175*795d594fSAndroid Build Coastguard Worker std::vector<std::string> strings_; 176*795d594fSAndroid Build Coastguard Worker 177*795d594fSAndroid Build Coastguard Worker // Vector that contains for each class def defined in a dex file, a set of class pairs recording 178*795d594fSAndroid Build Coastguard Worker // the outcome of assignability test from one of the two types to the other. 179*795d594fSAndroid Build Coastguard Worker std::vector<std::set<TypeAssignability>> assignable_types_; 180*795d594fSAndroid Build Coastguard Worker 181*795d594fSAndroid Build Coastguard Worker // Bit vector indexed by class def indices indicating whether the corresponding 182*795d594fSAndroid Build Coastguard Worker // class was successfully verified. 183*795d594fSAndroid Build Coastguard Worker std::vector<bool> verified_classes_; 184*795d594fSAndroid Build Coastguard Worker 185*795d594fSAndroid Build Coastguard Worker bool Equals(const DexFileDeps& rhs) const; 186*795d594fSAndroid Build Coastguard Worker }; 187*795d594fSAndroid Build Coastguard Worker 188*795d594fSAndroid Build Coastguard Worker // Helper function to share DexFileDeps decoding code. 189*795d594fSAndroid Build Coastguard Worker // Returns true on success, false on failure. 190*795d594fSAndroid Build Coastguard Worker template <bool kOnlyVerifiedClasses> 191*795d594fSAndroid Build Coastguard Worker static bool DecodeDexFileDeps(DexFileDeps& deps, 192*795d594fSAndroid Build Coastguard Worker const uint8_t** cursor, 193*795d594fSAndroid Build Coastguard Worker const uint8_t* data_start, 194*795d594fSAndroid Build Coastguard Worker const uint8_t* data_end, 195*795d594fSAndroid Build Coastguard Worker size_t num_class_defs); 196*795d594fSAndroid Build Coastguard Worker 197*795d594fSAndroid Build Coastguard Worker // Finds the DexFileDep instance associated with `dex_file`, or nullptr if 198*795d594fSAndroid Build Coastguard Worker // `dex_file` is not reported as being compiled. 199*795d594fSAndroid Build Coastguard Worker DexFileDeps* GetDexFileDeps(const DexFile& dex_file); 200*795d594fSAndroid Build Coastguard Worker 201*795d594fSAndroid Build Coastguard Worker EXPORT const DexFileDeps* GetDexFileDeps(const DexFile& dex_file) const; 202*795d594fSAndroid Build Coastguard Worker 203*795d594fSAndroid Build Coastguard Worker // Returns the index of `str`. If it is defined in `dex_file_`, this is the dex 204*795d594fSAndroid Build Coastguard Worker // string ID. If not, an ID is assigned to the string and cached in `strings_` 205*795d594fSAndroid Build Coastguard Worker // of the corresponding DexFileDeps structure (either provided or inferred from 206*795d594fSAndroid Build Coastguard Worker // `dex_file`). 207*795d594fSAndroid Build Coastguard Worker dex::StringIndex GetIdFromString(const DexFile& dex_file, const std::string& str) 208*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::verifier_deps_lock_); 209*795d594fSAndroid Build Coastguard Worker 210*795d594fSAndroid Build Coastguard Worker // Returns the string represented by `string_idx`. 211*795d594fSAndroid Build Coastguard Worker const char* GetStringFromIndex(const DexFile& dex_file, 212*795d594fSAndroid Build Coastguard Worker dex::StringIndex string_idx, 213*795d594fSAndroid Build Coastguard Worker /*out*/ size_t* utf8_length = nullptr) const; 214*795d594fSAndroid Build Coastguard Worker 215*795d594fSAndroid Build Coastguard Worker // Returns a string ID of the descriptor of the class. 216*795d594fSAndroid Build Coastguard Worker dex::StringIndex GetClassDescriptorStringId(const DexFile& dex_file, ObjPtr<mirror::Class> klass) 217*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) 218*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::verifier_deps_lock_); 219*795d594fSAndroid Build Coastguard Worker 220*795d594fSAndroid Build Coastguard Worker void AddAssignability(const DexFile& dex_file, 221*795d594fSAndroid Build Coastguard Worker const dex::ClassDef& class_def, 222*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> destination, 223*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> source) 224*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 225*795d594fSAndroid Build Coastguard Worker 226*795d594fSAndroid Build Coastguard Worker void AddAssignability(const DexFile& dex_file, 227*795d594fSAndroid Build Coastguard Worker const dex::ClassDef& class_def, 228*795d594fSAndroid Build Coastguard Worker const RegType& destination, 229*795d594fSAndroid Build Coastguard Worker const RegType& source) 230*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 231*795d594fSAndroid Build Coastguard Worker 232*795d594fSAndroid Build Coastguard Worker bool Equals(const VerifierDeps& rhs) const; 233*795d594fSAndroid Build Coastguard Worker 234*795d594fSAndroid Build Coastguard Worker // Verify `dex_file` according to the `deps`, that is going over each 235*795d594fSAndroid Build Coastguard Worker // `DexFileDeps` field, and checking that the recorded information still 236*795d594fSAndroid Build Coastguard Worker // holds. 237*795d594fSAndroid Build Coastguard Worker // Returns whether all dependencies were validated. 238*795d594fSAndroid Build Coastguard Worker bool VerifyDexFileAndUpdateStatus( 239*795d594fSAndroid Build Coastguard Worker Handle<mirror::ClassLoader> class_loader, 240*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file, 241*795d594fSAndroid Build Coastguard Worker DexFileDeps& deps, 242*795d594fSAndroid Build Coastguard Worker Thread* self) 243*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 244*795d594fSAndroid Build Coastguard Worker 245*795d594fSAndroid Build Coastguard Worker // Iterates over `dex_files` and tries to find a class def matching `descriptor`. 246*795d594fSAndroid Build Coastguard Worker // Returns true if such class def is found. 247*795d594fSAndroid Build Coastguard Worker bool IsInDexFiles(const char* descriptor, 248*795d594fSAndroid Build Coastguard Worker size_t hash, 249*795d594fSAndroid Build Coastguard Worker const std::vector<const DexFile*>& dex_files, 250*795d594fSAndroid Build Coastguard Worker /* out */ const DexFile** cp_dex_file) const; 251*795d594fSAndroid Build Coastguard Worker 252*795d594fSAndroid Build Coastguard Worker // Map from DexFiles into dependencies collected from verification of their methods. 253*795d594fSAndroid Build Coastguard Worker std::map<const DexFile*, std::unique_ptr<DexFileDeps>> dex_deps_; 254*795d594fSAndroid Build Coastguard Worker 255*795d594fSAndroid Build Coastguard Worker // Output only signifies if we are using the verifier deps to verify or just to generate them. 256*795d594fSAndroid Build Coastguard Worker const bool output_only_; 257*795d594fSAndroid Build Coastguard Worker 258*795d594fSAndroid Build Coastguard Worker friend class VerifierDepsTest; 259*795d594fSAndroid Build Coastguard Worker ART_FRIEND_TEST(VerifierDepsTest, StringToId); 260*795d594fSAndroid Build Coastguard Worker ART_FRIEND_TEST(VerifierDepsTest, EncodeDecode); 261*795d594fSAndroid Build Coastguard Worker ART_FRIEND_TEST(VerifierDepsTest, EncodeDecodeMulti); 262*795d594fSAndroid Build Coastguard Worker ART_FRIEND_TEST(VerifierDepsTest, VerifyDeps); 263*795d594fSAndroid Build Coastguard Worker ART_FRIEND_TEST(VerifierDepsTest, CompilerDriver); 264*795d594fSAndroid Build Coastguard Worker }; 265*795d594fSAndroid Build Coastguard Worker 266*795d594fSAndroid Build Coastguard Worker } // namespace verifier 267*795d594fSAndroid Build Coastguard Worker } // namespace art 268*795d594fSAndroid Build Coastguard Worker 269*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_VERIFIER_VERIFIER_DEPS_H_ 270