Skip to content

Commit 1ce19a5

Browse files
committed
src: collect external references for snapshot
Gather external references from the different native modules that are loaded during bootstrapping, for inclusion in the snapshot.
1 parent 3fce8a8 commit 1ce19a5

23 files changed

+373
-10
lines changed

node.gyp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@
616616
'src/pipe_wrap.cc',
617617
'src/process_wrap.cc',
618618
'src/signal_wrap.cc',
619+
'src/snapshot_support.cc',
619620
'src/spawn_sync.cc',
620621
'src/stream_base.cc',
621622
'src/stream_pipe.cc',
@@ -707,6 +708,8 @@
707708
'src/pipe_wrap.h',
708709
'src/req_wrap.h',
709710
'src/req_wrap-inl.h',
711+
'src/snapshot_support.h',
712+
'src/snapshot_support-inl.h',
710713
'src/spawn_sync.h',
711714
'src/stream_base.h',
712715
'src/stream_base-inl.h',

src/async_wrap.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "env-inl.h"
2525
#include "node_errors.h"
2626
#include "tracing/traced_value.h"
27+
#include "snapshot_support-inl.h"
2728
#include "util-inl.h"
2829

2930
#include "v8.h"
@@ -580,6 +581,23 @@ void AsyncWrap::Initialize(Local<Object> target,
580581
->GetFunction(env->context()).ToLocalChecked()).Check();
581582
}
582583

584+
static ExternalReferences external_references {
585+
__FILE__,
586+
SetupHooks,
587+
AsyncWrap::PushAsyncContext,
588+
AsyncWrap::PopAsyncContext,
589+
AsyncWrap::QueueDestroyAsyncId,
590+
EnablePromiseHook,
591+
DisablePromiseHook,
592+
RegisterDestroyHook,
593+
AsyncWrapObject::New,
594+
AsyncWrap::GetAsyncId,
595+
// AsyncReset is overloaded, pick the right one
596+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(
597+
AsyncWrap::AsyncReset),
598+
AsyncWrap::GetProviderType,
599+
PromiseWrap::getIsChainedPromise,
600+
};
583601

584602
AsyncWrap::AsyncWrap(Environment* env,
585603
Local<Object> object,

src/inspector_js_api.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "inspector_agent.h"
33
#include "inspector_io.h"
44
#include "memory_tracker-inl.h"
5+
#include "snapshot_support-inl.h"
56
#include "util-inl.h"
67
#include "v8.h"
78
#include "v8-inspector.h"
@@ -345,6 +346,31 @@ void Initialize(Local<Object> target, Local<Value> unused,
345346
JSBindingsConnection<MainThreadConnection>::Bind(env, target);
346347
}
347348

349+
static ExternalReferences external_references {
350+
__FILE__,
351+
InspectorConsoleCall,
352+
SetConsoleExtensionInstaller,
353+
CallAndPauseOnStart,
354+
Open,
355+
Url,
356+
WaitForDebugger,
357+
AsyncTaskScheduledWrapper,
358+
InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>,
359+
InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>,
360+
InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>,
361+
RegisterAsyncHookWrapper,
362+
IsEnabled,
363+
JSBindingsConnection<LocalConnection>::New,
364+
JSBindingsConnection<LocalConnection>::Dispatch,
365+
// Disconnect is overloaded, pick the right one
366+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(
367+
JSBindingsConnection<LocalConnection>::Disconnect),
368+
JSBindingsConnection<MainThreadConnection>::New,
369+
JSBindingsConnection<MainThreadConnection>::Dispatch,
370+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(
371+
JSBindingsConnection<MainThreadConnection>::Disconnect),
372+
};
373+
348374
} // namespace
349375
} // namespace inspector
350376
} // namespace node

src/node.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "node_revert.h"
4040
#include "node_v8_platform-inl.h"
4141
#include "node_version.h"
42+
#include "snapshot_support-inl.h"
4243

4344
#if HAVE_OPENSSL
4445
#include "node_crypto.h"
@@ -1028,16 +1029,14 @@ int Start(int argc, char** argv) {
10281029
{
10291030
Isolate::CreateParams params;
10301031
const std::vector<size_t>* indexes = nullptr;
1031-
std::vector<intptr_t> external_references;
1032+
std::vector<intptr_t> external_references = ExternalReferences::get_list();
1033+
external_references.push_back(ExternalReferences::kEnd);
10321034

10331035
bool force_no_snapshot =
10341036
per_process::cli_options->per_isolate->no_node_snapshot;
10351037
if (!force_no_snapshot) {
10361038
v8::StartupData* blob = NodeMainInstance::GetEmbeddedSnapshotBlob();
10371039
if (blob != nullptr) {
1038-
// TODO(joyeecheung): collect external references and set it in
1039-
// params.external_references.
1040-
external_references.push_back(reinterpret_cast<intptr_t>(nullptr));
10411040
params.external_references = external_references.data();
10421041
params.snapshot_blob = blob;
10431042
indexes = NodeMainInstance::GetIsolateDataIndexes();
@@ -1062,6 +1061,14 @@ int Stop(Environment* env) {
10621061
return 0;
10631062
}
10641063

1064+
static ExternalReferences external_references {
1065+
__FILE__,
1066+
binding::GetLinkedBinding,
1067+
binding::GetInternalBinding,
1068+
MarkBootstrapComplete,
1069+
};
1070+
1071+
10651072
} // namespace node
10661073

10671074
#if !HAVE_INSPECTOR

src/node_buffer.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "string_bytes.h"
2929
#include "string_search.h"
3030
#include "util-inl.h"
31+
#include "snapshot_support-inl.h"
3132
#include "v8.h"
3233

3334
#include <cstring>
@@ -1222,6 +1223,37 @@ void Initialize(Local<Object> target,
12221223
}
12231224
}
12241225

1226+
static ExternalReferences external_references {
1227+
__FILE__,
1228+
SetBufferPrototype,
1229+
CreateFromString,
1230+
ByteLengthUtf8,
1231+
Copy,
1232+
Compare,
1233+
CompareOffset,
1234+
Fill,
1235+
IndexOfBuffer,
1236+
IndexOfNumber,
1237+
IndexOfString,
1238+
Swap16,
1239+
Swap32,
1240+
Swap64,
1241+
EncodeInto,
1242+
EncodeUtf8String,
1243+
StringSlice<ASCII>,
1244+
StringSlice<BASE64>,
1245+
StringSlice<LATIN1>,
1246+
StringSlice<HEX>,
1247+
StringSlice<UCS2>,
1248+
StringSlice<UTF8>,
1249+
StringWrite<ASCII>,
1250+
StringWrite<BASE64>,
1251+
StringWrite<LATIN1>,
1252+
StringWrite<HEX>,
1253+
StringWrite<UCS2>,
1254+
StringWrite<UTF8>,
1255+
};
1256+
12251257
} // anonymous namespace
12261258
} // namespace Buffer
12271259
} // namespace node

src/node_credentials.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "env-inl.h"
22
#include "node_internals.h"
3+
#include "snapshot_support-inl.h"
34
#include "util-inl.h"
45

56
#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
@@ -401,6 +402,25 @@ static void Initialize(Local<Object> target,
401402
#endif // NODE_IMPLEMENTS_POSIX_CREDENTIALS
402403
}
403404

405+
static ExternalReferences external_references {
406+
__FILE__,
407+
// SafeGetenv is overloaded, pick the right one
408+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(SafeGetenv),
409+
#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
410+
GetUid,
411+
GetEUid,
412+
GetGid,
413+
GetEGid,
414+
GetGroups,
415+
InitGroups,
416+
SetEGid,
417+
SetEUid,
418+
SetGid,
419+
SetUid,
420+
SetGroups,
421+
#endif
422+
};
423+
404424
} // namespace credentials
405425
} // namespace node
406426

src/node_env_var.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "env-inl.h"
33
#include "node_errors.h"
44
#include "node_process.h"
5+
#include "snapshot_support-inl.h"
56

67
#include <time.h> // tzset(), _tzset()
78

@@ -385,4 +386,15 @@ MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context, Isolate* isolate) {
385386
PropertyHandlerFlags::kHasNoSideEffect));
386387
return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
387388
}
389+
390+
391+
static ExternalReferences external_references {
392+
__FILE__,
393+
EnvGetter,
394+
EnvSetter,
395+
EnvQuery,
396+
EnvDeleter,
397+
EnvEnumerator
398+
};
399+
388400
} // namespace node

src/node_errors.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "node_report.h"
88
#include "node_process.h"
99
#include "node_v8_platform-inl.h"
10+
#include "snapshot_support-inl.h"
1011
#include "util-inl.h"
1112

1213
namespace node {
@@ -848,6 +849,16 @@ void Initialize(Local<Object> target,
848849
env->SetMethod(target, "triggerUncaughtException", TriggerUncaughtException);
849850
}
850851

852+
static ExternalReferences external_references {
853+
__FILE__,
854+
SetPrepareStackTraceCallback,
855+
SetEnhanceStackForFatalException,
856+
NoSideEffectsToString,
857+
// TriggerUncaughtException is overloaded, pick the right one
858+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(
859+
TriggerUncaughtException),
860+
};
861+
851862
void DecorateErrorStack(Environment* env,
852863
const errors::TryCatchScope& try_catch) {
853864
Local<Value> exception = try_catch.Exception();

src/node_i18n.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "node_buffer.h"
5050
#include "node_errors.h"
5151
#include "node_internals.h"
52+
#include "snapshot_support-inl.h"
5253
#include "util-inl.h"
5354
#include "v8.h"
5455

@@ -825,6 +826,20 @@ void Initialize(Local<Object> target,
825826
env->SetMethod(target, "hasConverter", ConverterObject::Has);
826827
}
827828

829+
static ExternalReferences external_references {
830+
__FILE__,
831+
// Pick the right variant for overloaded names.
832+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(ToUnicode),
833+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(ToASCII),
834+
GetStringWidth,
835+
ICUErrorName,
836+
static_cast<void(*)(const FunctionCallbackInfo<Value>& args)>(Transcode),
837+
ConverterObject::Create,
838+
ConverterObject::Decode,
839+
ConverterObject::Has,
840+
};
841+
842+
828843
} // namespace i18n
829844
} // namespace node
830845

src/node_native_module_env.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node_native_module_env.h"
2+
#include "snapshot_support-inl.h"
23
#include "env-inl.h"
34

45
namespace node {
@@ -202,6 +203,15 @@ void NativeModuleEnv::Initialize(Local<Object> target,
202203
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
203204
}
204205

206+
static ExternalReferences external_references {
207+
__FILE__,
208+
NativeModuleEnv::ConfigStringGetter,
209+
NativeModuleEnv::ModuleIdsGetter,
210+
NativeModuleEnv::GetModuleCategories,
211+
NativeModuleEnv::GetCacheUsage,
212+
NativeModuleEnv::CompileFunction,
213+
};
214+
205215
} // namespace native_module
206216
} // namespace node
207217

0 commit comments

Comments
 (0)