1 /*
2  * Copyright (c) 2022 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 
16 #ifndef ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H
17 #define ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H
18 
19 #include "ecmascript/jspandafile/js_pandafile.h"
20 #include "ecmascript/jspandafile/panda_file_translator.h"
21 #include "ecmascript/jspandafile/debug_info_extractor.h"
22 #include "ecmascript/platform/mutex.h"
23 
24 namespace panda {
25 namespace ecmascript {
26 class Program;
27 
28 class PUBLIC_API JSPandaFileManager {
29 public:
30     static JSPandaFileManager *GetInstance();
31 
32     ~JSPandaFileManager();
33 
34     JSHandle<Program> GenerateProgram(EcmaVM *vm, const JSPandaFile *jsPandaFile, std::string_view entryPoint);
35 
36     std::shared_ptr<JSPandaFile> LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint,
37                                                  bool needUpdate = false);
38 
39     std::shared_ptr<JSPandaFile> LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint,
40                                                  const void *buffer, size_t size, bool needUpdate = false);
41 
42     // load pandafile from secure mem
43     std::shared_ptr<JSPandaFile> LoadJSPandaFileSecure(JSThread *thread, const CString &filename,
44                                                        std::string_view entryPoint, uint8_t *buffer, size_t size,
45                                                        bool needUpdate = false);
46 
47     std::shared_ptr<JSPandaFile> OpenJSPandaFile(const CString &filename);
48 
49     std::shared_ptr<JSPandaFile> OpenJSPandaFile(const CString &filename, const CString &desc);
50 
51     std::shared_ptr<JSPandaFile> OpenJSPandaFileFromBuffer(uint8_t *buffer, size_t size, const CString &filename);
52 
53     std::shared_ptr<JSPandaFile> NewJSPandaFile(const panda_file::File *pf, const CString &desc);
54 
55     DebugInfoExtractor *GetJSPtExtractor(const JSPandaFile *jsPandaFile);
56 
57     DebugInfoExtractor *GetJSPtExtractorAndExtract(const JSPandaFile *jsPandaFile);
58 
59     DebugInfoExtractor *CpuProfilerGetJSPtExtractor(const JSPandaFile *jsPandaFile);
60 
61     bool CheckFilePath(JSThread *thread, const CString &fileName);
62 
63     // for debugger
64     template<typename Callback>
EnumerateJSPandaFiles(Callback cb)65     void EnumerateJSPandaFiles(Callback cb)
66     {
67         LockHolder lock(jsPandaFileLock_);
68         for (const auto &item : loadedJSPandaFiles_) {
69             if (!cb(item.second)) {
70                 return;
71             }
72         }
73         for (const auto &item : oldJSPandaFiles_) {
74             if (!cb(item)) {
75                 return;
76             }
77         }
78     }
79 
80     template<typename Callback>
EnumerateNonVirtualJSPandaFiles(Callback cb)81     void EnumerateNonVirtualJSPandaFiles(Callback cb)
82     {
83         LockHolder lock(jsPandaFileLock_);
84         for (const auto &item : loadedJSPandaFiles_) {
85             if (!cb(item.second)) {
86                 return;
87             }
88         }
89         for (const auto &item : oldJSPandaFiles_) {
90             if (!cb(item)) {
91                 return;
92             }
93         }
94     }
95     std::shared_ptr<JSPandaFile> FindJSPandaFileByNormalizedName(const CString &normalizedName);
96     std::shared_ptr<JSPandaFile> FindJSPandaFileByMapBase(uintptr_t mapBase);
97     std::shared_ptr<JSPandaFile> FindJSPandaFile(const CString &filename);
98     std::shared_ptr<JSPandaFile> FindMergedJSPandaFile();
99     void AddJSPandaFile(const std::shared_ptr<JSPandaFile> &jsPandaFile);
100     void RemoveJSPandaFile(const JSPandaFile *jsPandaFile);
101     void ClearNameMap();
102 private:
103     JSPandaFileManager() = default;
104 
105     class JSPandaFileAllocator {
106     public:
107         static void *AllocateBuffer(size_t size);
108         static void FreeBuffer(void *mem);
109     };
110 
111     std::shared_ptr<JSPandaFile> GenerateJSPandaFile(JSThread *thread, const panda_file::File *pf, const CString &desc,
112                                                      std::string_view entryPoint);
113     std::shared_ptr<JSPandaFile> GetJSPandaFile(const panda_file::File *pf);
114     std::shared_ptr<JSPandaFile> FindJSPandaFileWithChecksum(const CString &filename, uint32_t checksum);
115     std::shared_ptr<JSPandaFile> FindJSPandaFileUnlocked(const CString &filename);
116     std::shared_ptr<JSPandaFile> GenerateJSPandafileFromBufferCache(JSThread *thread,
117                                                                     const CString &filename,
118                                                                     std::string_view entryPoint);
119     void ObsoleteLoadedJSPandaFile(const CString &filename);
120 
121     static void *AllocateBuffer(size_t size);
122     static void FreeBuffer(void *mem);
123 
124     RecursiveMutex jsPandaFileLock_;
125     // JSPandaFile was shared by all vm.
126     std::unordered_map<const CString, std::shared_ptr<JSPandaFile>, CStringHash> loadedJSPandaFiles_;
127     // for plugin update.
128     std::set<std::shared_ptr<JSPandaFile>> oldJSPandaFiles_;
129     std::unordered_map<const JSPandaFile *, std::unique_ptr<DebugInfoExtractor>> extractors_;
130 
131     friend class JSPandaFile;
132 };
133 }  // namespace ecmascript
134 }  // namespace panda
135 #endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H
136