• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::string SafeGetAnFileNameNoSuffix(uint32_t index);
40     std::shared_ptr<StubFileInfo> SafeGetStubFileInfo();
41     void SafeMergeChecksumInfo(const std::unordered_map<CString, uint32_t> &fileNameToChecksumMap);
42     void UnsafeMergeChecksumInfo(const std::unordered_map<CString, uint32_t> &fileNameToChecksumMap);
43     void VerifyChecksumOrSetToInvalid(const std::pair<CString, uint32_t> &newPair,
44                                       std::pair<const CString, uint32_t> &fullPair);
45     bool SafeCheckFilenameToChecksum(const CString &fileName, uint32_t checksum);
46     bool SafeTryReadLock();
47     bool SafeInsideStub(uintptr_t pc);
48     bool SafeInsideAOT(uintptr_t pc);
49     AOTFileInfo::CallSiteInfo SafeCalCallSiteInfo(uintptr_t retAddr, bool isDeopt);
50     static void DestroyFileMapMem(MemMap &fileMapMem);
51     void SafeDestroyAllData();
52     void SafeDestroyAnData(const std::string &fileName);
53     const std::unordered_map<CString, uint32_t> &SafeGetfullFileNameToChecksumMap();
54 
GetDir()55     const std::string &GetDir() const
56     {
57         return anDir_;
58     }
59 
IsEnable()60     bool IsEnable() const
61     {
62         return anEnable_;
63     }
64 
65     void Dump() const DUMP_API_ATTR;
66 
67     // only main thread call this, only call once, no need to lock
SetDir(std::string dir)68     void SetDir(std::string dir)
69     {
70         anDir_ = std::move(dir);
71     }
72 
SetEnable(bool enable)73     void SetEnable(bool enable)
74     {
75         anEnable_ = enable;
76     }
77 
78 private:
79     AnFileDataManager() = default;
80     std::shared_ptr<AnFileInfo> UnsafeFind(const std::string &fileName) const;
81     bool UnsafeLoadFromAOTInternal(const std::string &fileName, std::shared_ptr<AnFileInfo> &info);
82     bool UnsafeLoadFromAOT(const std::string &fileName);
83 #if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM)
84     bool UnsafeLoadFromAOT(const std::string &fileName,
85                            std::function<bool(std::string fileName, uint8_t **buff, size_t *buffSize)> cb);
86 #endif
87     bool UnsafeLoadFromStub(const std::string &fileName);
88     bool UnsafeCheckFilenameToChecksum(const CString &fileName, uint32_t checksum);
89     uint32_t UnSafeGetFileInfoIndex(const std::string &fileName);
90     const std::unordered_map<CString, uint32_t> &UnsafeGetfullFileNameToChecksumMap() const;
UnSafeGetAnFileInfo(uint32_t index)91     std::shared_ptr<AnFileInfo> UnSafeGetAnFileInfo(uint32_t index)
92     {
93         return loadedAn_.at(index);
94     }
95 
96     std::string UnSafeGetAnFileNameNoSuffix(uint32_t index);
97 
98     RWLock lock_ {};
99     std::vector<std::string> anFileNameVector_ {};
100     std::vector<std::shared_ptr<AnFileInfo>> loadedAn_ {};  // keep the same order with anFileNameVector_
101     std::unordered_map<CString, uint32_t> fullFileNameToChecksumMap_ {};
102     std::shared_ptr<StubFileInfo> loadedStub_ {nullptr};
103     std::string anDir_;
104     bool anEnable_ {false};
105     NO_COPY_SEMANTIC(AnFileDataManager);
106     NO_MOVE_SEMANTIC(AnFileDataManager);
107 };
108 }  // namespace panda::ecmascript
109 #endif  // ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H
110