1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H 16 #define ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H 17 18 #include <utility> 19 #include "ecmascript/compiler/aot_file/an_file_info.h" 20 #include "ecmascript/compiler/aot_file/stub_file_info.h" 21 #include "ecmascript/mem/c_string.h" 22 #include "ecmascript/platform/map.h" 23 24 namespace panda::ecmascript { 25 class AnFileDataManager { 26 public: 27 enum class Type : uint8_t { 28 STUB = 0, 29 AOT, 30 }; 31 32 static AnFileDataManager *GetInstance(); 33 ~AnFileDataManager(); 34 35 bool SafeLoad(const std::string &fileName, Type type, [[maybe_unused]] std::function<bool 36 (std::string fileName, uint8_t **buff, size_t *buffSize)> cb = nullptr); 37 uint32_t SafeGetFileInfoIndex(const std::string &fileName); 38 std::shared_ptr<AnFileInfo> SafeGetAnFileInfo(uint32_t index); 39 std::shared_ptr<StubFileInfo> SafeGetStubFileInfo(); 40 void SafeMergeChecksumInfo(const std::unordered_map<CString, uint32_t> &fileNameToChecksumMap); 41 void UnsafeMergeChecksumInfo(const std::unordered_map<CString, uint32_t> &fileNameToChecksumMap); 42 void VerifyChecksumOrSetToInvalid(const std::pair<CString, uint32_t> &newPair, 43 std::pair<const CString, uint32_t> &fullPair); 44 bool SafeCheckFilenameToChecksum(const CString &fileName, uint32_t checksum); 45 bool SafeTryReadLock(); 46 bool SafeInsideStub(uintptr_t pc); 47 bool SafeInsideAOT(uintptr_t pc); 48 AOTFileInfo::CallSiteInfo SafeCalCallSiteInfo(uintptr_t retAddr, bool isDeopt); 49 static void DestroyFileMapMem(MemMap &fileMapMem); 50 void SafeDestroyAllData(); 51 void SafeDestroyAnData(const std::string &fileName); 52 const std::unordered_map<CString, uint32_t> &SafeGetfullFileNameToChecksumMap(); 53 GetDir()54 const std::string &GetDir() const 55 { 56 return anDir_; 57 } 58 IsEnable()59 bool IsEnable() const 60 { 61 return anEnable_; 62 } 63 64 void Dump() const DUMP_API_ATTR; 65 66 // only main thread call this, only call once, no need to lock SetDir(std::string dir)67 void SetDir(std::string dir) 68 { 69 anDir_ = std::move(dir); 70 } 71 SetEnable(bool enable)72 void SetEnable(bool enable) 73 { 74 anEnable_ = enable; 75 } 76 77 private: 78 AnFileDataManager() = default; 79 std::shared_ptr<AnFileInfo> UnsafeFind(const std::string &fileName) const; 80 bool UnsafeLoadFromAOTInternal(const std::string &fileName, std::shared_ptr<AnFileInfo> &info); 81 bool UnsafeLoadFromAOT(const std::string &fileName); 82 #if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM) 83 bool UnsafeLoadFromAOT(const std::string &fileName, 84 std::function<bool(std::string fileName, uint8_t **buff, size_t *buffSize)> cb); 85 #endif 86 bool UnsafeLoadFromStub(const std::string &fileName); 87 bool UnsafeCheckFilenameToChecksum(const CString &fileName, uint32_t checksum); 88 uint32_t UnSafeGetFileInfoIndex(const std::string &fileName); 89 const std::unordered_map<CString, uint32_t> &UnsafeGetfullFileNameToChecksumMap() const; UnSafeGetAnFileInfo(uint32_t index)90 std::shared_ptr<AnFileInfo> UnSafeGetAnFileInfo(uint32_t index) 91 { 92 return loadedAn_.at(index); 93 } 94 95 RWLock lock_ {}; 96 std::unordered_map<std::string, uint32_t> anFileNameToIndexMap_ {}; 97 std::vector<std::shared_ptr<AnFileInfo>> loadedAn_ {}; 98 std::unordered_map<CString, uint32_t> fullFileNameToChecksumMap_ {}; 99 std::shared_ptr<StubFileInfo> loadedStub_ {nullptr}; 100 std::string anDir_; 101 bool anEnable_ {false}; 102 NO_COPY_SEMANTIC(AnFileDataManager); 103 NO_MOVE_SEMANTIC(AnFileDataManager); 104 }; 105 } // namespace panda::ecmascript 106 #endif // ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H 107