forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeserializer.cpp
More file actions
243 lines (210 loc) · 8.56 KB
/
Deserializer.cpp
File metadata and controls
243 lines (210 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifdef HERMESVM_SERIALIZE
#include "hermes/ADT/CompactArray.h"
#include "hermes/VM/Deserializer.h"
#include "hermes/VM/GCPointer-inline.h"
#include "hermes/VM/GCPointer.h"
#include "hermes/VM/JSArrayBuffer.h"
#include "hermes/VM/JSDataView.h"
#include "hermes/VM/JSNativeFunctions.h"
#include "hermes/VM/JSTypedArray.h"
#include "hermes/VM/JSWeakMapImpl.h"
#include "hermes/VM/PrimitiveBox.h"
#include "hermes/VM/Runtime.h"
#include "JSLib/JSLibInternal.h"
#include "llvh/Support/Debug.h"
#define DEBUG_TYPE "serialize"
namespace hermes {
namespace vm {
using DeserializeCallBack = void(Deserializer &d, CellKind kind);
static DeserializeCallBack *deserializeImpl[] = {
#define CELL_KIND(name) name##Deserialize,
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
};
void Deserializer::deserializeCell(uint8_t kind) {
assert(
(CellKind)kind != CellKind::ArrayStorageKind &&
"ArrayStorage should be serialized/deserialized with its owner.");
deserializeImpl[kind](*this, (CellKind)kind);
}
void Deserializer::deserializeCompactTable(CompactTable &table) {
auto size = readInt<uint32_t>();
auto scale = (CompactArray::Scale)readInt<uint8_t>();
CompactArray tmp(size, scale);
for (uint32_t idx = 0; idx < size; ++idx)
tmp.set(idx, readInt<uint32_t>());
table.asArray().swap(tmp);
}
void Deserializer::flushRelocationQueue() {
while (!relocationQueue_.empty()) {
auto entry = relocationQueue_.front();
relocationQueue_.pop_front();
assert(entry.id < objectTable_.size() && "invalid relocation id");
void *ptr = objectTable_[entry.id];
assert(ptr && "pointer relocation cannot be resolved");
updateAddress(entry.address, ptr, entry.kind);
}
}
void Deserializer::init(
ExternalPointersVectorFunction *externalPointersVectorCallBack) {
// Do the sanity check of the header first.
readHeader();
// Relocation table size and string buffers are all at the end of the
// MemoryBuffer. Let's start reading from the back.
const char *ptr = buffer_->getBufferEnd();
uint32_t size;
// Read map size and resize relocation table.
size = readBackwards(ptr);
objectTable_.resize(size);
// Read size of char16Buf_.
// Note this is the total size of the buffer and may include padding.
size = readBackwards(ptr);
// Move ptr to the beginning of char16Buf_.
ptr -= size;
if (size > 0) {
// Has char16Buf_, reconstruct the buffer here.
assert(ptr >= buffer_->getBufferStart() && "wrong char16Buf_ size");
// size is buffer size in bytes. Let's calculate the end first before
// casting to char16_t *.
// Ensure we are aligned.
uint32_t aligningOffset = llvh::alignTo(size, alignof(char16_t)) - size;
assert(aligningOffset <= size && "aligning offset should not exceed size");
assert(
(intptr_t)(ptr + aligningOffset) % alignof(char16_t) == 0 &&
"Pointer should be aligned for char16");
char16Buf_ = ArrayRef<char16_t>(
(const char16_t *)(ptr + aligningOffset),
(const char16_t *)(ptr + size - aligningOffset));
}
// Read size of charBuf_.
size = readBackwards(ptr);
// Move ptr to the beginning of charBuf_.
ptr -= size;
if (size > 0) {
// Has charBuf_, reconstruct the buffer here.
assert(ptr >= buffer_->getBufferStart() && "wrong charBuf_ size");
charBuf_ = ArrayRef<char>(ptr, size);
}
// Map nullptr to 0.
objectTable_[0] = 0;
// Populate relocation table for native functions and constructors.
size_t idx = 1;
#define NATIVE_FUNCTION(func) \
assert(!objectTable_[idx]); \
objectTable_[idx] = (void *)func; \
LLVM_DEBUG( \
llvh::dbgs() << idx << ", " << #func << ", " << (void *)func << "\n"); \
idx++;
#define NATIVE_FUNCTION_TYPED(func, type) \
assert(!objectTable_[idx]); \
objectTable_[idx] = (void *)func<type>; \
LLVM_DEBUG( \
llvh::dbgs() << idx << ", " << #func << "<" << #type << ">, " \
<< (void *)func<type> << "\n"); \
idx++;
#define NATIVE_FUNCTION_TYPED_2(func, type, type2) \
assert(!objectTable_[idx]); \
objectTable_[idx] = (void *)func<type, type2>; \
LLVM_DEBUG( \
llvh::dbgs() << idx << ", " << #func << "<" << #type << ", " << #type2 \
<< ">, " << ((void *)func<type, type2>) << "\n"); \
idx++;
NativeConstructor::CreatorFunction *funcPtr;
#define NATIVE_CONSTRUCTOR(func) \
funcPtr = func; \
assert(!objectTable_[idx]); \
objectTable_[idx] = (void *)funcPtr; \
LLVM_DEBUG( \
llvh::dbgs() << idx << ", " << #func << ", " << (void *)funcPtr \
<< "\n"); \
idx++;
#define NATIVE_CONSTRUCTOR_TYPED(classname, type, type2, func) \
funcPtr = func<classname<type, type2>>; \
assert(!objectTable_[idx]); \
objectTable_[idx] = (void *)funcPtr; \
LLVM_DEBUG( \
llvh::dbgs() << idx << ", " << #func << "<" << #classname << "<" \
<< #type << ", " << #type2 << ">>" \
<< ", " << (void *)funcPtr << "\n"); \
idx++;
#include "hermes/VM/NativeFunctions.def"
#undef NATIVE_CONSTRUCTOR
// Map external function pointers.
for (auto *ptr : externalPointersVectorCallBack()) {
assert(!objectTable_[idx] && "External pointer should only be mapped once");
objectTable_[idx] = ptr;
idx++;
}
}
void Deserializer::readHeader() {
SerializeHeader readHeader;
readData(&readHeader, sizeof(SerializeHeader));
if (readHeader.magic != SD_MAGIC) {
hermes_fatal("Not a serialize file or endianness do not match");
}
if (readHeader.version != SD_HEADER_VERSION) {
hermes_fatal("Serialize header versions do not match");
}
if (readHeader.nativeFunctionTableVersion != NATIVE_FUNCTION_VERSION) {
hermes_fatal("Native function table versions do not match");
}
if (runtime_->getHeap().size() < readHeader.heapSize) {
hermes_fatal(
(llvh::Twine("Deserialize heap size less than Serialize heap size(") +
llvh::StringRef(std::to_string(readHeader.heapSize)) +
llvh::Twine(" bytes), try increase initial heap size"))
.str());
}
#define CHECK_HEADER_SET(header, field) \
if (!header.field) { \
hermes_fatal("Serialize/Deserialize configs do not match"); \
}
#define CHECK_HEADER_UNSET(header, field) \
if (header.field) { \
hermes_fatal("Serialize/Deserialize configs do not match"); \
}
#ifndef NDEBUG
CHECK_HEADER_SET(readHeader, isDebug); // isDebug
#else
CHECK_HEADER_UNSET(readHeader, isDebug);
#endif
#ifdef HERMES_ENABLE_DEBUGGER
CHECK_HEADER_SET(readHeader, isEnableDebugger); // isEnableDebugger.
#else
CHECK_HEADER_UNSET(readHeader, isEnableDebugger);
#endif
runtime_->checkHeaderRuntimeConfig(readHeader);
}
void Deserializer::readAndCheckOffset() {
size_t currentOffset = offset_;
size_t bytes = readInt<size_t>();
if (currentOffset != bytes) {
hermes_fatal("Deserializer sanity check failed: offset don't match");
}
}
void Deserializer::updateAddress(
void *address,
void *ptrVal,
RelocationKind kind) {
switch (kind) {
case RelocationKind::NativePointer:
*(void **)address = ptrVal;
return;
case RelocationKind::GCPointer:
((GCPointerBase *)address)->set(runtime_, ptrVal, &runtime_->getHeap());
return;
case RelocationKind::HermesValue:
((HermesValue *)address)->unsafeUpdatePointer(ptrVal);
return;
}
llvm_unreachable("Invalid relocation kind");
}
} // namespace vm
} // namespace hermes
#endif