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_SNAPSHOT_MEM_SNAPSHOT_H 17 #define ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_H 18 19 #include "ecmascript/compiler/aot_file/aot_version.h" 20 #include "ecmascript/common.h" 21 #include "ecmascript/mem/c_string.h" 22 #include "ecmascript/snapshot/mem/encode_bit.h" 23 #include "ecmascript/snapshot/mem/snapshot_processor.h" 24 25 #include "libpandabase/macros.h" 26 27 namespace panda::ecmascript { 28 class Program; 29 class EcmaVM; 30 class JSPandaFile; 31 32 class PUBLIC_API Snapshot { 33 public: Snapshot(EcmaVM * vm)34 explicit Snapshot(EcmaVM *vm) : vm_(vm) {} 35 ~Snapshot() = default; 36 37 void Serialize(const CString &fileName = "./snapshot"); 38 void Serialize(TaggedObject *objectHeader, const JSPandaFile *jsPandaFile, const CString &fileName = "./snapshot"); 39 void Serialize(uintptr_t startAddr, size_t size, const CString &fileName = "./snapshot"); 40 void SerializeBuiltins(const CString &fileName = "./snapshot"); 41 bool DeserializeInternal(SnapshotType type, const CString &snapshotFile, SnapshotProcessor &processor, 42 MemMap &fileMap); 43 bool Deserialize(SnapshotType type, const CString &snapshotFile, bool isBuiltins = false); 44 #if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM) 45 bool Deserialize(SnapshotType type, const CString &snapshotFile, [[maybe_unused]] std::function<bool 46 (std::string fileName, uint8_t **buff, size_t *buffSize)> ReadAOTCallBack, bool isBuiltins = false); 47 #endif GetDeserializeResultForUT()48 JSTaggedValue GetDeserializeResultForUT() const 49 { 50 // ONLY used in UT to get the deserialize value result 51 return result_; 52 } 53 54 protected: 55 struct SnapShotHeader : public base::FileHeaderBase { 56 public: SnapShotHeaderSnapShotHeader57 explicit SnapShotHeader(const VersionType &lastVersion) : base::FileHeaderBase(lastVersion) {} 58 VerifySnapShotHeader59 bool Verify(const VersionType &lastVersion) const 60 { 61 return VerifyVersion("snapshot file", lastVersion, AOTFileVersion::AI_STRICT_MATCH); 62 } 63 64 size_t regularObjSize {0}; 65 size_t pinnedObjSize {0}; 66 size_t largeObjSize {0}; 67 size_t oldSpaceObjSize {0}; 68 size_t nonMovableObjSize {0}; 69 size_t machineCodeObjSize {0}; 70 size_t snapshotObjSize {0}; 71 size_t hugeObjSize {0}; 72 size_t stringSize {0}; 73 uintptr_t pandaFileBegin {0}; 74 size_t rootObjectSize {0}; 75 }; 76 GetLastVersion()77 virtual const base::FileHeaderBase::VersionType &GetLastVersion() const 78 { 79 return AOTFileVersion::AI_VERSION; 80 } 81 82 private: 83 size_t AlignUpPageSize(size_t spaceSize); 84 void WriteToFile(std::fstream &writer, const JSPandaFile *jsPandaFile, size_t size, SnapshotProcessor &processor); 85 86 NO_MOVE_SEMANTIC(Snapshot); 87 NO_COPY_SEMANTIC(Snapshot); 88 89 EcmaVM *vm_; 90 // ONLY used in UT to get the deserialize value result 91 JSTaggedValue result_ {JSTaggedValue::Hole()}; 92 }; 93 } // namespace panda::ecmascript 94 #endif // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_H 95