Skip to content

Commit

Permalink
src: zero-initialize data that are copied into the snapshot
Browse files Browse the repository at this point in the history
To prevent padding from making the snapshot unreproducible,
zero-initialize the data that are copied into the snapshot
so that the padding copied are all zeros. This is better
than enlarging the enums to align the fields since it doesn't
make the snapshot bigger than necessary, and it removes the
need of using static assertions to ensure alignment.
  • Loading branch information
joyeecheung committed Jun 23, 2024
1 parent 5c40956 commit af91d7d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,7 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
if (index == BaseObject::kEmbedderType) {
int size = sizeof(EmbedderTypeInfo);
char* data = new char[size];
memset(data, 0, size); // Make the padding reproducible.
// We need to use placement new because V8 calls delete[] on the returned
// data.
// TODO(joyeecheung): support cppgc objects.
Expand Down
1 change: 1 addition & 0 deletions src/node_snapshotable.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct InternalFieldInfoBase {
std::is_same_v<InternalFieldInfoBase, T>,
"Can only accept InternalFieldInfoBase subclasses");
void* buf = ::operator new[](sizeof(T));
memset(buf, 0, sizeof(T)); // Make the padding reproducible.
T* result = new (buf) T;
result->type = type;
result->length = sizeof(T);
Expand Down

0 comments on commit af91d7d

Please sign in to comment.