• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/snapshot/mem/snapshot.h"
17 
18 #include <cerrno>
19 
20 #include "ecmascript/compiler/aot_file/aot_file_manager.h"
21 #include "ecmascript/ecma_vm.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/jobs/micro_job_queue.h"
24 #include "ecmascript/js_hclass.h"
25 #include "ecmascript/js_thread.h"
26 #include "ecmascript/jspandafile/js_pandafile_manager.h"
27 #include "ecmascript/jspandafile/program_object.h"
28 #include "ecmascript/mem/c_containers.h"
29 #include "ecmascript/mem/heap.h"
30 #include "ecmascript/object_factory.h"
31 #include "ecmascript/platform/file.h"
32 #include "ecmascript/snapshot/mem/snapshot_env.h"
33 
34 namespace panda::ecmascript {
Serialize(const CString & fileName)35 void Snapshot::Serialize(const CString &fileName)
36 {
37     kungfu::AOTSnapshot &aotSnapshot = vm_->GetJSThread()->GetCurrentEcmaContext()->GetPTManager()->GetAOTSnapshot();
38     JSTaggedValue root = aotSnapshot.GetSnapshotData();
39     if (root == JSTaggedValue::Hole()) {
40         // root equals hole means no data stored.
41         LOG_COMPILER(ERROR) << "error: no data for ai file generation!";
42         return;
43     }
44     Serialize(root.GetTaggedObject(), nullptr, fileName);
45 }
46 
Serialize(TaggedObject * objectHeader,const JSPandaFile * jsPandaFile,const CString & fileName)47 void Snapshot::Serialize(TaggedObject *objectHeader, const JSPandaFile *jsPandaFile, const CString &fileName)
48 {
49     std::string realPath;
50     if (!RealPath(std::string(fileName), realPath, false)) {
51         LOG_FULL(FATAL) << "snapshot file path error";
52     }
53     std::fstream writer(realPath.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
54     if (!writer.good()) {
55         writer.close();
56         LOG_FULL(ERROR) << "snapshot open file failed";
57         return;
58     }
59 
60     SnapshotProcessor processor(vm_);
61     processor.Initialize();
62 
63     std::unordered_map<uint64_t, std::pair<uint64_t, EncodeBit>> data;
64     CQueue<TaggedObject *> objectQueue;
65 
66     if (objectHeader->GetClass()->GetObjectType() == JSType::PROGRAM) {
67         processor.SetProgramSerializeStart();
68     }
69 
70     processor.EncodeTaggedObject(objectHeader, &objectQueue, &data);
71     size_t rootObjSize = objectQueue.size();
72     processor.ProcessObjectQueue(&objectQueue, &data);
73     WriteToFile(writer, jsPandaFile, rootObjSize, processor);
74 }
75 
Serialize(uintptr_t startAddr,size_t size,const CString & fileName)76 void Snapshot::Serialize(uintptr_t startAddr, size_t size, const CString &fileName)
77 {
78     std::string realPath;
79     if (!RealPath(std::string(fileName), realPath, false)) {
80         LOG_FULL(FATAL) << "snapshot file path error";
81     }
82     std::fstream writer(realPath.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
83     if (!writer.good()) {
84         writer.close();
85         LOG_FULL(ERROR) << "snapshot open file failed";
86         return;
87     }
88 
89     SnapshotProcessor processor(vm_);
90     processor.Initialize();
91 
92     std::unordered_map<uint64_t, std::pair<uint64_t, EncodeBit>> data;
93     CQueue<TaggedObject *> objectQueue;
94 
95     ObjectSlot start(startAddr);
96     ObjectSlot end(startAddr + size * sizeof(JSTaggedType));
97     processor.EncodeTaggedObjectRange(start, end, &objectQueue, &data);
98 
99     size_t rootObjSize = objectQueue.size();
100     processor.ProcessObjectQueue(&objectQueue, &data);
101     WriteToFile(writer, nullptr, rootObjSize, processor);
102 }
103 
SerializeBuiltins(const CString & fileName)104 void Snapshot::SerializeBuiltins(const CString &fileName)
105 {
106     std::string realPath;
107     if (!RealPath(std::string(fileName), realPath, false)) {
108         LOG_FULL(FATAL) << "snapshot file path error";
109     }
110     std::fstream write(realPath.c_str(), std::ios::out | std::ios::binary | std::ios::app);
111     if (!write.good()) {
112         write.close();
113         LOG_FULL(ERROR) << "snapshot open file failed";
114         return;
115     }
116     // if builtins.snapshot file has exist, return directly
117     if (write.tellg()) {
118         LOG_FULL(DEBUG) << "snapshot already exist";
119         write.close();
120         return;
121     }
122 
123     SnapshotProcessor processor(vm_);
124     processor.Initialize();
125     processor.SetBuiltinsSerializeStart();
126 
127     std::unordered_map<uint64_t, std::pair<uint64_t, EncodeBit>> data;
128     CQueue<TaggedObject *> objectQueue;
129 
130     auto globalEnvHandle = vm_->GetGlobalEnv();
131     auto constant = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
132     constant->VisitRangeSlot([&objectQueue, &data, &processor]([[maybe_unused]] Root type,
133                                                                ObjectSlot start, ObjectSlot end) {
134         processor.EncodeTaggedObjectRange(start, end, &objectQueue, &data);
135     });
136     processor.EncodeTaggedObject(*globalEnvHandle, &objectQueue, &data);
137     size_t rootObjSize = objectQueue.size();
138     processor.ProcessObjectQueue(&objectQueue, &data);
139     WriteToFile(write, nullptr, rootObjSize, processor);
140 }
141 
Deserialize(SnapshotType type,const CString & snapshotFile,bool isBuiltins)142 bool Snapshot::Deserialize(SnapshotType type, const CString &snapshotFile, bool isBuiltins)
143 {
144     std::string realPath;
145     if (!RealPath(std::string(snapshotFile), realPath, false)) {
146         LOG_FULL(FATAL) << "snapshot file path error";
147         UNREACHABLE();
148     }
149 
150     SnapshotProcessor processor(vm_);
151     if (isBuiltins) {
152         processor.SetBuiltinsDeserializeStart();
153     }
154 
155     MemMap fileMap = FileMap(realPath.c_str(), FILE_RDONLY, PAGE_PROT_READWRITE);
156     if (fileMap.GetOriginAddr() == nullptr) {
157         LOG_FULL(FATAL) << "file mmap failed";
158         UNREACHABLE();
159     }
160     auto readFile = ToUintPtr(fileMap.GetOriginAddr());
161     auto hdr = *ToNativePtr<const SnapShotHeader>(readFile);
162     if (!hdr.Verify(GetLastVersion())) {
163         FileUnMap(fileMap);
164         LOG_ECMA(ERROR) << "file verify failed.";
165         return false;
166     }
167     uintptr_t oldSpaceBegin = readFile + sizeof(SnapShotHeader);
168     processor.DeserializeObjectExcludeString(oldSpaceBegin, hdr.oldSpaceObjSize, hdr.nonMovableObjSize,
169                                              hdr.machineCodeObjSize, hdr.snapshotObjSize, hdr.hugeObjSize);
170     uintptr_t stringBegin = oldSpaceBegin + hdr.oldSpaceObjSize + hdr.nonMovableObjSize +
171         hdr.machineCodeObjSize + hdr.snapshotObjSize + hdr.hugeObjSize;
172     uintptr_t stringEnd = stringBegin + hdr.stringSize;
173     processor.DeserializeString(stringBegin, stringEnd);
174 
175     FileUnMap(MemMap(fileMap.GetOriginAddr(), hdr.pandaFileBegin));
176     std::shared_ptr<JSPandaFile> jsPandaFile;
177     if (static_cast<uint32_t>(fileMap.GetSize()) > hdr.pandaFileBegin) {
178         uintptr_t pandaFileMem = readFile + hdr.pandaFileBegin;
179         auto pf = panda_file::File::OpenFromMemory(os::mem::ConstBytePtr(ToNativePtr<std::byte>(pandaFileMem),
180             static_cast<uint32_t>(fileMap.GetSize()) - hdr.pandaFileBegin, os::mem::MmapDeleter));
181         jsPandaFile = JSPandaFileManager::GetInstance()->NewJSPandaFile(pf.release(), "");
182     }
183     // relocate object field
184     processor.Relocate(type, jsPandaFile.get(), hdr.rootObjectSize);
185     processor.AddRootObjectToAOTFileManager(type, snapshotFile);
186     LOG_COMPILER(INFO) << "loaded ai file: " << snapshotFile.c_str();
187     return true;
188 }
189 
AlignUpPageSize(size_t spaceSize)190 size_t Snapshot::AlignUpPageSize(size_t spaceSize)
191 {
192     if (spaceSize % Constants::PAGE_SIZE_ALIGN_UP == 0) {
193         return spaceSize;
194     }
195     return Constants::PAGE_SIZE_ALIGN_UP * (spaceSize / Constants::PAGE_SIZE_ALIGN_UP + 1);
196 }
197 
WriteToFile(std::fstream & writer,const JSPandaFile * jsPandaFile,size_t size,SnapshotProcessor & processor)198 void Snapshot::WriteToFile(std::fstream &writer, const JSPandaFile *jsPandaFile,
199                            size_t size, SnapshotProcessor &processor)
200 {
201     uint32_t totalStringSize = 0U;
202     CVector<uintptr_t> stringVector = processor.GetStringVector();
203     for (size_t i = 0; i < stringVector.size(); ++i) {
204         auto str = reinterpret_cast<EcmaString *>(stringVector[i]);
205         size_t objectSize = AlignUp(EcmaStringAccessor(str).ObjectSize(),
206             static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT));
207         totalStringSize += objectSize;
208     }
209 
210     std::vector<uint32_t> objSizeVector = processor.StatisticsObjectSize();
211     size_t totalObjSize = totalStringSize;
212     for (uint32_t objSize : objSizeVector) {
213         totalObjSize += objSize;
214     }
215     uint32_t pandaFileBegin = RoundUp(totalObjSize + sizeof(SnapShotHeader), Constants::PAGE_SIZE_ALIGN_UP);
216     SnapShotHeader hdr(GetLastVersion());
217     hdr.oldSpaceObjSize = objSizeVector[0]; // 0: oldSpaceObj
218     hdr.nonMovableObjSize = objSizeVector[1]; // 1: nonMovableObj
219     hdr.machineCodeObjSize = objSizeVector[2]; // 2: machineCodeObj
220     hdr.snapshotObjSize = objSizeVector[3]; // 3: snapshotObj
221     hdr.hugeObjSize = objSizeVector[4]; // 4: hugeObj
222     hdr.stringSize = totalStringSize;
223     hdr.pandaFileBegin = pandaFileBegin;
224     hdr.rootObjectSize = static_cast<uint32_t>(size);
225     writer.write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
226     processor.WriteObjectToFile(writer);
227 
228     for (size_t i = 0; i < stringVector.size(); ++i) {
229         auto str = reinterpret_cast<EcmaString *>(stringVector[i]);
230         size_t strSize = AlignUp(EcmaStringAccessor(str).ObjectSize(),
231             static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT));
232         int index = 0; // 0 represents the line string. Natural number 1 represents the constant string.
233         if (EcmaStringAccessor(str).IsConstantString()) {
234             index = 1;
235         }
236         // Write the index in the head of string.
237         uint8_t headerSize = JSTaggedValue::TaggedTypeSize();
238         JSTaggedType indexHeader = JSTaggedValue(index).GetRawData();
239         writer.write(reinterpret_cast<char *>(&indexHeader), headerSize);
240         writer.write(reinterpret_cast<char *>(str) + headerSize, strSize - headerSize);
241         writer.flush();
242     }
243 
244     ASSERT(static_cast<size_t>(writer.tellp()) == totalObjSize + sizeof(SnapShotHeader));
245     if (jsPandaFile) {
246         writer.seekp(pandaFileBegin);
247         writer.write(static_cast<const char *>(jsPandaFile->GetHeader()), jsPandaFile->GetFileSize());
248     }
249     writer.close();
250 }
251 }  // namespace panda::ecmascript
252