From 5f51729014e10ee304747815a17b8980efdf909a Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 11 Jun 2021 21:02:50 +0800 Subject: [PATCH] bootstrap: move event loop handle checking into snapshot builder This is only necessary for the snapshot builder (because we have no way to resurrect the handles at the moment). In addition, print the handles if the debug flag is set or if the queues are not empty after snapshot is created. PR-URL: https://github.com/nodejs/node/pull/39007 Refs: https://github.com/nodejs/node/issues/35711 Refs: https://github.com/nodejs/node/pull/38905 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- src/node_main_instance.cc | 2 -- src/node_snapshotable.cc | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index 2320f556e9f453..f232cd6a89f26a 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -224,8 +224,6 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code, } } - CHECK(env->req_wrap_queue()->IsEmpty()); - CHECK(env->handle_wrap_queue()->IsEmpty()); return env; } diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 1871cef443f312..35e0ed3f6df4bf 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -157,7 +157,22 @@ void SnapshotBuilder::Generate(SnapshotData* out, // Must be out of HandleScope out->blob = creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kClear); + + // We must be able to rehash the blob when we restore it or otherwise + // the hash seed would be fixed by V8, introducing a vulnerability. CHECK(out->blob.CanBeRehashed()); + + // We cannot resurrect the handles from the snapshot, so make sure that + // no handles are left open in the environment after the blob is created + // (which should trigger a GC and close all handles that can be closed). + if (!env->req_wrap_queue()->IsEmpty() + || !env->handle_wrap_queue()->IsEmpty() + || per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) { + PrintLibuvHandleInformation(env->event_loop(), stderr); + } + CHECK(env->req_wrap_queue()->IsEmpty()); + CHECK(env->handle_wrap_queue()->IsEmpty()); + // Must be done while the snapshot creator isolate is entered i.e. the // creator is still alive. FreeEnvironment(env);