• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_SERIALIZE_H
17 #define ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_SERIALIZE_H
18 
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 
23 #include "libpandabase/macros.h"
24 #include "ecmascript/snapshot/mem/slot_bit.h"
25 #include "ecmascript/js_tagged_value.h"
26 
27 namespace panda::ecmascript {
28 class EcmaVM;
29 
30 class SnapShotSerialize final {
31 public:
32     explicit SnapShotSerialize(EcmaVM *vm, bool serialize);
33     ~SnapShotSerialize();
34 
35     void Serialize(TaggedObject *objectHeader, CQueue<TaggedObject *> *queue,
36                    std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
37     void RedirectSlot(const panda_file::File *pf);
38     void SerializePandaFileMethod();
39 
SetProgramSerializeStart()40     void SetProgramSerializeStart()
41     {
42         programSerialize_ = true;
43     }
44 
45     void RegisterNativeMethod();
46     void GeneratedNativeMethod();
47 
48 private:
49     void SetObjectSlotField(uintptr_t obj, size_t offset, uint64_t value);
50     void SetObjectSlotFieldUint32(uintptr_t obj, size_t offset, uint32_t value);
51 
52     void NativePointerSerialize(TaggedObject *objectHeader, uintptr_t snapshotObj);
53     void JSObjectSerialize(TaggedObject *objectHeader, uintptr_t snapshotObj, size_t objectSize,
54                            CQueue<TaggedObject *> *queue,
55                            std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
56     void JSFunctionBaseSerialize(TaggedObject *objectHeader, uintptr_t snapshotObj, size_t objectSize,
57                                  CQueue<TaggedObject *> *queue,
58                                  std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
59     void JSProxySerialize(TaggedObject *objectHeader, uintptr_t snapshotObj, size_t objectSize,
60                           CQueue<TaggedObject *> *queue,
61                           std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
62     void DynClassSerialize(TaggedObject *objectHeader, uintptr_t snapshotObj, size_t objectSize,
63                            CQueue<TaggedObject *> *queue,
64                            std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
65     void DynArraySerialize(TaggedObject *objectHeader, uintptr_t snapshotObj, CQueue<TaggedObject *> *queue,
66                            std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
67     void DynStringSerialize(TaggedObject *objectHeader, uintptr_t snapshotObj);
68     void DynProgramSerialize(TaggedObject *objectHeader, uintptr_t snapshotObj, CQueue<TaggedObject *> *queue,
69                              std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
70 
71     void NativePointerDeserialize(uint64_t *objectHeader);
72     void JSObjectDeserialize(uint64_t *objectHeader, size_t objectSize);
73     void JSFunctionBaseDeserialize(uint64_t *objectHeader, size_t objectSize);
74     void JSProxyDeserialize(uint64_t *objectHeader, size_t objectSize);
75     void DynClassDeserialize(uint64_t *objectHeader);
76     void DynStringDeserialize(uint64_t *objectHeader);
77     void DynArrayDeserialize(uint64_t *objectHeader);
78     void DynProgramDeserialize(uint64_t *objectHeader, size_t objectSize);
79 
80     SlotBit HandleObjectHeader(TaggedObject *objectHeader, uint8_t objectType, size_t objectSize,
81                                CQueue<TaggedObject *> *queue,
82                                std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
83     uint64_t HandleTaggedField(JSTaggedType *tagged, CQueue<TaggedObject *> *queue,
84                                std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
85     void DeserializeHandleTaggedField(uint64_t *value);
86     void DeserializeHandleClassWord(TaggedObject *object);
87     void ExtendObjectArray();
88 
SetAddressToSlot(size_t index,uintptr_t value)89     void SetAddressToSlot(size_t index, uintptr_t value)
90     {
91         auto addr = reinterpret_cast<uintptr_t *>(addressSlot_ + index * ADDRESS_SIZE);
92         *addr = value;
93     }
94 
95     template<typename T>
GetAddress(size_t index)96     T GetAddress(size_t index)
97     {
98         return *reinterpret_cast<T *>(addressSlot_ + index * ADDRESS_SIZE);
99     }
100 
101     SlotBit NativePointerToSlotBit(void *nativePointer);
102     void *NativePointerSlotBitToAddr(SlotBit native);
103     void DeserializeRangeTaggedField(size_t beginAddr, int numOfFields);
104 
105     EcmaVM *vm_{nullptr};
106     bool serialize_{false};
107     bool programSerialize_{false};
108     int count_{0};
109     int objectArraySize_{0};
110     uintptr_t addressSlot_;
111     CVector<uintptr_t> pandaMethod_;
112 
113     NO_COPY_SEMANTIC(SnapShotSerialize);
114     NO_MOVE_SEMANTIC(SnapShotSerialize);
115 };
116 }  // namespace panda::ecmascript
117 
118 #endif  // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_SERIALIZE_H
119