1 /* 2 * Copyright (C) 2020 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_SDK_CHECKER_H_ 18 #define ART_RUNTIME_SDK_CHECKER_H_ 19 20 #include "art_field.h" 21 #include "art_method.h" 22 #include "base/locks.h" 23 #include "base/macros.h" 24 #include "dex/dex_file.h" 25 26 namespace art HIDDEN { 27 28 /** 29 * The SdkChecker verifies if a given symbol is present in a given classpath. 30 * 31 * For convenience and future extensibility the classpath is given as set of 32 * dex files, simillar to a regular classpath the APKs use. 33 * 34 * The symbol (method, field, class) is checked based on its descriptor and not 35 * according the any access check semantic. 36 * 37 * This class is intended to be used during off-device AOT verification when 38 * only some predefined symbols should be resolved (e.g. belonging to some public 39 * API classpath). 40 */ 41 class SdkChecker { 42 public: 43 // Constructs and SDK Checker from the given public sdk paths. The public_sdk 44 // format is the same as the classpath format (e.g. `dex1:dex2:dex3`). The 45 // method will attempt to open the dex files and if there are errors it will 46 // return a nullptr and set the error_msg appropriately. 47 EXPORT static SdkChecker* Create(const std::string& public_sdk, std::string* error_msg); 48 49 // Verify if it should deny access to the given methods. 50 // The decision is based on whether or not any of the API dex files declares a method 51 // with the same signature. 52 // 53 // NOTE: This is an expensive check as it searches the dex files for the necessary type 54 // and string ids. This is OK because the functionality here is indended to be used 55 // only in AOT verification. 56 bool ShouldDenyAccess(ArtMethod* art_method) const REQUIRES_SHARED(Locks::mutator_lock_); 57 58 // Similar to ShouldDenyAccess(ArtMethod* art_method). 59 bool ShouldDenyAccess(ArtField* art_field) const REQUIRES_SHARED(Locks::mutator_lock_); 60 61 // Similar to ShouldDenyAccess(ArtMethod* art_method). 62 bool ShouldDenyAccess(std::string_view type_descriptor) const; 63 64 // Enabled/Disable the checks. SetEnabled(bool enabled)65 void SetEnabled(bool enabled) { enabled_ = enabled; } 66 67 private: 68 SdkChecker(); 69 70 std::vector<std::unique_ptr<const DexFile>> sdk_dex_files_; 71 72 bool enabled_; 73 }; 74 75 } // namespace art 76 77 #endif // ART_RUNTIME_SDK_CHECKER_H_ 78