Skip to content

Commit cb1329a

Browse files
joyeecheungRafaelGSS
authored andcommitted
src: use v8::(Des|S)erializeInternalFieldsCallback
Previously V8 would just try to serialize the context data fields "verbatim" by copying the pointer values. This patch makes use of the new callbacks so that at serialization, the embedder data for the context can at least be serialized in a meaningful way (which are all reset to empty for now). Otherwise the upstream may have difficulties serializing these pointer values "verbatim" especially with the introduction of external pointer tables, even though the verbatim pointer values from a previous process is meaningless to Node.js. For Node.js the callback currently just checks that the slots are know. We will reassign the pointers with newly created native structures during deserialization and there isn't much we can reuse for now. PR-URL: #53217 Refs: https://chromium-review.googlesource.com/c/v8/v8/+/5512712/comments/cfc2b28d_c921ac80?tab=comments Reviewed-By: Chengzhong Wu <[email protected]>
1 parent e66eb37 commit cb1329a

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/api/environment.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,13 @@ Environment* CreateEnvironment(
458458
if (use_snapshot) {
459459
context = Context::FromSnapshot(isolate,
460460
SnapshotData::kNodeMainContextIndex,
461-
{DeserializeNodeInternalFields, env})
461+
v8::DeserializeInternalFieldsCallback(
462+
DeserializeNodeInternalFields, env),
463+
nullptr,
464+
MaybeLocal<Value>(),
465+
nullptr,
466+
v8::DeserializeContextDataCallback(
467+
DeserializeNodeContextData, env))
462468
.ToLocalChecked();
463469

464470
CHECK(!context.IsEmpty());

src/node_snapshotable.cc

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,8 +1155,11 @@ ExitCode SnapshotBuilder::CreateSnapshot(SnapshotData* out,
11551155
CHECK_EQ(index, SnapshotData::kNodeVMContextIndex);
11561156
index = creator->AddContext(base_context);
11571157
CHECK_EQ(index, SnapshotData::kNodeBaseContextIndex);
1158-
index = creator->AddContext(main_context,
1159-
{SerializeNodeContextInternalFields, env});
1158+
index = creator->AddContext(
1159+
main_context,
1160+
v8::SerializeInternalFieldsCallback(SerializeNodeContextInternalFields,
1161+
env),
1162+
v8::SerializeContextDataCallback(SerializeNodeContextData, env));
11601163
CHECK_EQ(index, SnapshotData::kNodeMainContextIndex);
11611164
}
11621165

@@ -1255,6 +1258,41 @@ std::string SnapshotableObject::GetTypeName() const {
12551258
}
12561259
}
12571260

1261+
void DeserializeNodeContextData(Local<Context> holder,
1262+
int index,
1263+
StartupData payload,
1264+
void* callback_data) {
1265+
// This is unreachable for now. We will reset all the pointers in
1266+
// Environment::AssignToContext() via the realm constructor.
1267+
UNREACHABLE();
1268+
}
1269+
1270+
StartupData SerializeNodeContextData(Local<Context> holder,
1271+
int index,
1272+
void* callback_data) {
1273+
// For now we just reset all of them in Environment::AssignToContext().
1274+
// We return empty data here to make sure that the embedder data serialized
1275+
// into the snapshot is reproducible and V8 doesn't have to try to serialize
1276+
// the pointer values that won't be useful during deserialization.
1277+
switch (index) {
1278+
case ContextEmbedderIndex::kEnvironment:
1279+
case ContextEmbedderIndex::kContextifyContext:
1280+
case ContextEmbedderIndex::kRealm:
1281+
case ContextEmbedderIndex::kContextTag: {
1282+
void* data = holder->GetAlignedPointerFromEmbedderData(index);
1283+
per_process::Debug(
1284+
DebugCategory::MKSNAPSHOT,
1285+
"Serialize context data, index=%d, holder=%p, ptr=%p\n",
1286+
static_cast<int>(index),
1287+
*holder,
1288+
data);
1289+
return {nullptr, 0};
1290+
}
1291+
default:
1292+
UNREACHABLE();
1293+
}
1294+
}
1295+
12581296
void DeserializeNodeInternalFields(Local<Object> holder,
12591297
int index,
12601298
StartupData payload,

src/node_snapshotable.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,17 @@ class SnapshotableObject : public BaseObject {
126126
v8::StartupData SerializeNodeContextInternalFields(v8::Local<v8::Object> holder,
127127
int index,
128128
void* env);
129+
v8::StartupData SerializeNodeContextData(v8::Local<v8::Context> holder,
130+
int index,
131+
void* env);
129132
void DeserializeNodeInternalFields(v8::Local<v8::Object> holder,
130133
int index,
131134
v8::StartupData payload,
132135
void* env);
136+
void DeserializeNodeContextData(v8::Local<v8::Context> holder,
137+
int index,
138+
v8::StartupData payload,
139+
void* env);
133140
void SerializeSnapshotableObjects(Realm* realm,
134141
v8::SnapshotCreator* creator,
135142
RealmSerializeInfo* info);

0 commit comments

Comments
 (0)