• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_MODULE_MODULE_SNAPSHOT_H
16 #define ECMASCRIPT_MODULE_MODULE_SNAPSHOT_H
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "common_components/taskpool/taskpool.h"
20 #include "ecmascript/serializer/module_serializer.h"
21 
22 namespace panda::ecmascript {
23 class ModuleSnapshot {
24 public:
25     static constexpr std::string_view MODULE_SNAPSHOT_FILE_NAME = "Module.ams";
26     static constexpr std::string_view SNAPSHOT_FILE_SUFFIX = ".ams"; // ark module snapshot
27 
28     static void SerializeDataAndPostSavingJob(const EcmaVM *vm, const CString &path, const CString &version);
29     static bool DeserializeData(const EcmaVM *vm, const CString &path, const CString &version);
30 
31 protected:
32     static constexpr int BUFFER_SIZE_INDEX = 0;
33     static constexpr int BUFFER_CAPACITY_INDEX = 1;
34     static constexpr int REGULAR_SPACE_SIZE_INDEX = 2;
35     static constexpr int PIN_SPACE_SIZE_INDEX = 3;
36     static constexpr int OLD_SPACE_SIZE_INDEX = 4;
37     static constexpr int NONMOVABLE_SPACE_SIZE_INDEX = 5;
38     static constexpr int MACHINECODE_SPACE_SIZE_INDEX = 6;
39     static constexpr int SHARED_OLD_SPACE_SIZE_INDEX = 7;
40     static constexpr int SHARED_NONMOVABLE_SPACE_SIZE_INDEX = 8;
41     static constexpr int INCOMPLETE_DATA_INDEX = 9;
42     static constexpr int GROUP_SIZE = 10;
43     static constexpr int CMC_GC_REGION_SIZE = 2;
44 // Module snapshot layout
45 // +--------------------------------------+<-------- BaseInfo
46 // |       Application Version Code       |
47 // +--------------------------------------+
48 // |      System Version Code Length      |
49 // |         System Version Code          |
50 // +--------------------------------------+<-------- data
51 // |               dataIndex              |
52 // +--------------------------------------+
53 // |               sizeLimit              |
54 // +--------------------------------------+
55 // |              bufferSize              |
56 // |            bufferCapacity            |
57 // |           regularSpaceSize           |
58 // |             pinSpaceSize             |
59 // |             oldSpaceSize             |
60 // |          nonMovableSpaceSize         |
61 // |          machineCodeSpaceSize        |
62 // |           sharedOldSpaceSize         |
63 // |       sharedNonMovableSpaceSize      |
64 // |             incompleteData           |
65 // +--------------------------------------+<-------- CMCGC
66 // |     regularRemainSizeVector size     |
67 // |       regularRemainSizeVector        |
68 // |     regularRemainSizeVector size     |
69 // |         pinRemainSizeVector          |
70 // +--------------------------------------+<-------- GC
71 // |     regionRemainSizeVectors size     |
72 // |       regionRemainSizeVectors        |
73 // +--------------------------------------+<-------- data
74 // |                buffer                |
75 // +--------------------------------------+<-------- CheckSum
76 // |               CheckSum               |
77 // +--------------------------------------+
78     static JSHandle<TaggedArray> GetModuleSerializeArray(JSThread *thread);
79     static void RestoreUpdatedBinding(JSThread* thread, JSHandle<TaggedArray> serializeArray);
80     static void RemoveSnapshotFiles(const CString &path);
81     static std::unique_ptr<SerializeData> GetSerializeData(JSThread *thread);
82     static bool ReadDataFromFile(JSThread *thread, std::unique_ptr<SerializeData>& data, const CString &path,
83         const CString &version);
84     static bool WriteDataToFile(JSThread *thread, const std::unique_ptr<SerializeData>& data, const CString &filePath,
85         const CString &version);
86 
GetAlignUpPadding(const uint8_t * curPtr,void * originAddr,const size_t alignment)87     static size_t GetAlignUpPadding(const uint8_t *curPtr, void *originAddr, const size_t alignment)
88     {
89         const auto originPtr = static_cast<uint8_t *>(originAddr);
90         return AlignUp(curPtr - originPtr, alignment) - (curPtr - originPtr);
91     }
92 
93     class ModuleSnapshotTask : public common::Task {
94     public:
ModuleSnapshotTask(int32_t id,JSThread * thread,std::unique_ptr<SerializeData> & serializeData,const CString & path,const CString & version)95         ModuleSnapshotTask(int32_t id, JSThread* thread, std::unique_ptr<SerializeData>& serializeData,
96             const CString& path, const CString &version) : Task(id), thread_(thread),
97             serializeData_(std::move(serializeData)), path_(path), version_(version) {}
98         bool Run(uint32_t threadIndex) override;
99 
100         NO_COPY_SEMANTIC(ModuleSnapshotTask);
101         NO_MOVE_SEMANTIC(ModuleSnapshotTask);
102 
103     private:
104         JSThread* thread_ { nullptr };
105         std::unique_ptr<SerializeData> serializeData_ {};
106         CString path_ {};
107         CString version_ {};
108     };
109 };
110 }  // namespace panda::ecmascript
111 #endif // ECMASCRIPT_MODULE_MODULE_SNAPSHOT_H