Skip to content

Commit c181940

Browse files
joyeecheungaduh95
authored andcommitted
src: zero-initialize data that are copied into the snapshot
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>
1 parent 8cda2db commit c181940

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/node_snapshotable.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1415,9 +1415,11 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
14151415
// To serialize the type field, save data in a EmbedderTypeInfo.
14161416
if (index == BaseObject::kEmbedderType) {
14171417
int size = sizeof(EmbedderTypeInfo);
1418-
char* data = new char[size];
14191418
// We need to use placement new because V8 calls delete[] on the returned
14201419
// data.
1420+
// The () syntax at the end would zero-initialize the block and make
1421+
// the padding reproducible.
1422+
char* data = new char[size]();
14211423
// TODO(joyeecheung): support cppgc objects.
14221424
new (data) EmbedderTypeInfo(obj->type(),
14231425
EmbedderTypeInfo::MemoryMode::kBaseObject);

src/node_snapshotable.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct InternalFieldInfoBase {
4747
std::is_same_v<InternalFieldInfoBase, T>,
4848
"Can only accept InternalFieldInfoBase subclasses");
4949
void* buf = ::operator new[](sizeof(T));
50+
memset(buf, 0, sizeof(T)); // Make the padding reproducible.
5051
T* result = new (buf) T;
5152
result->type = type;
5253
result->length = sizeof(T);

0 commit comments

Comments
 (0)