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.

PR-URL: #53563
Refs: #50983
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung authored and aduh95 committed Jul 16, 2024
1 parent 8cda2db commit c181940
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1415,9 +1415,11 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
// To serialize the type field, save data in a EmbedderTypeInfo.
if (index == BaseObject::kEmbedderType) {
int size = sizeof(EmbedderTypeInfo);
char* data = new char[size];
// We need to use placement new because V8 calls delete[] on the returned
// data.
// The () syntax at the end would zero-initialize the block and make
// the padding reproducible.
char* data = new char[size]();
// TODO(joyeecheung): support cppgc objects.
new (data) EmbedderTypeInfo(obj->type(),
EmbedderTypeInfo::MemoryMode::kBaseObject);
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 c181940

Please sign in to comment.