Skip to content

Commit

Permalink
src: explicitly register built-in modules
Browse files Browse the repository at this point in the history
Previously, built-in modules are registered before main() via
__attribute__((constructor)) mechanism in GCC and similiar
mechanism in MSVC. This causes some issues when node is built as
static library. Calling module registration function for built-in
modules in node::Init() helps to avoid the issues.

Signed-off-by: Yihong Wang <yh.wang@ibm.com>
Refs: #14986 (comment)
Backport-PR-URL: #17625
PR-URL: #16565
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
yhwang authored and MylesBorins committed Dec 12, 2017
1 parent cae6703 commit a37dce4
Show file tree
Hide file tree
Showing 35 changed files with 167 additions and 35 deletions.
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@
'defines': [ 'NODE_WANT_INTERNALS=1' ],

'sources': [
'test/cctest/node_module_reg.cc',
'test/cctest/node_test_fixture.cc',
'test/cctest/test_aliased_buffer.cc',
'test/cctest/test_base64.cc',
Expand Down
2 changes: 1 addition & 1 deletion src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,4 @@ void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(async_wrap, node::AsyncWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(async_wrap, node::AsyncWrap::Initialize)
2 changes: 1 addition & 1 deletion src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2259,4 +2259,4 @@ void Initialize(Local<Object> target,
} // namespace cares_wrap
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(cares_wrap, node::cares_wrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(cares_wrap, node::cares_wrap::Initialize)
2 changes: 1 addition & 1 deletion src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,4 @@ void FSEventWrap::Close(const FunctionCallbackInfo<Value>& args) {
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs_event_wrap, node::FSEventWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(fs_event_wrap, node::FSEventWrap::Initialize)
2 changes: 1 addition & 1 deletion src/inspector_js_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,5 @@ void InitInspectorBindings(Local<Object> target, Local<Value> unused,
} // namespace inspector
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector,
NODE_BUILTIN_MODULE_CONTEXT_AWARE(inspector,
node::inspector::InitInspectorBindings);
2 changes: 1 addition & 1 deletion src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,4 @@ void JSStream::Initialize(Local<Object> target,

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(js_stream, node::JSStream::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(js_stream, node::JSStream::Initialize)
24 changes: 22 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ typedef int mode_t;
extern char **environ;
#endif

// This is used to load built-in modules. Instead of using
// __attribute__((constructor)), we call the _register_<modname>
// function for each built-in modules explicitly in
// node::RegisterBuiltinModules(). This is only forward declaration.
// The definitions are in each module's implementation when calling
// the NODE_BUILTIN_MODULE_CONTEXT_AWARE.
#define V(modname) void _register_##modname();
NODE_BUILTIN_MODULES(V)
#undef V

namespace node {

using v8::Array;
Expand Down Expand Up @@ -4575,6 +4585,9 @@ void Init(int* argc,
// Initialize prog_start_time to get relative uptime.
prog_start_time = static_cast<double>(uv_now(uv_default_loop()));

// Register built-in modules
node::RegisterBuiltinModules();

// Make inherited handles noninheritable.
uv_disable_stdio_inheritance();

Expand Down Expand Up @@ -4984,11 +4997,18 @@ int Start(int argc, char** argv) {
return exit_code;
}

// Call built-in modules' _register_<module name> function to
// do module registration explicitly.
void RegisterBuiltinModules() {
#define V(modname) _register_##modname();
NODE_BUILTIN_MODULES(V)
#undef V
}

} // namespace node

#if !HAVE_INSPECTOR
static void InitEmptyBindings() {}
void InitEmptyBindings() {}

NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, InitEmptyBindings)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(inspector, InitEmptyBindings)
#endif // !HAVE_INSPECTOR
2 changes: 1 addition & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1304,4 +1304,4 @@ void Initialize(Local<Object> target,
} // namespace Buffer
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(buffer, node::Buffer::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(buffer, node::Buffer::Initialize)
2 changes: 1 addition & 1 deletion src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ static void InitConfig(Local<Object> target,

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(config, node::InitConfig)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(config, node::InitConfig)
2 changes: 1 addition & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1171,4 +1171,4 @@ void InitContextify(Local<Object> target,
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(contextify, node::InitContextify)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(contextify, node::InitContextify)
2 changes: 1 addition & 1 deletion src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6286,4 +6286,4 @@ void InitCrypto(Local<Object> target,
} // namespace crypto
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(crypto, node::crypto::InitCrypto)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(crypto, node::crypto::InitCrypto)
2 changes: 1 addition & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1473,4 +1473,4 @@ void InitFs(Local<Object> target,

} // end namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs, node::InitFs)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(fs, node::InitFs)
2 changes: 1 addition & 1 deletion src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2250,4 +2250,4 @@ HTTP_STATUS_CODES(V)
} // namespace http2
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(http2, node::http2::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(http2, node::http2::Initialize)
2 changes: 1 addition & 1 deletion src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -821,4 +821,4 @@ void InitHttpParser(Local<Object> target,
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(http_parser, node::InitHttpParser)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(http_parser, node::InitHttpParser)
2 changes: 1 addition & 1 deletion src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,6 @@ void Init(Local<Object> target,
} // namespace i18n
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(icu, node::i18n::Init)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(icu, node::i18n::Init)

#endif // NODE_HAVE_I18N_SUPPORT
85 changes: 83 additions & 2 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,81 @@ struct sockaddr;
constant_attributes).FromJust(); \
} while (0)


#if HAVE_OPENSSL
#define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap)
#else
#define NODE_BUILTIN_OPENSSL_MODULES(V)
#endif

#if NODE_HAVE_I18N_SUPPORT
#define NODE_BUILTIN_ICU_MODULES(V) V(icu)
#else
#define NODE_BUILTIN_ICU_MODULES(V)
#endif

// A list of built-in modules. In order to do module registration
// in node::Init(), need to add built-in modules in the following list.
// Then in node::RegisterBuiltinModules(), it calls modules' registration
// function. This helps the built-in modules are loaded properly when
// node is built as static library. No need to depends on the
// __attribute__((constructor)) like mechanism in GCC.
#define NODE_BUILTIN_STANDARD_MODULES(V) \
V(async_wrap) \
V(buffer) \
V(cares_wrap) \
V(config) \
V(contextify) \
V(fs) \
V(fs_event_wrap) \
V(http2) \
V(http_parser) \
V(inspector) \
V(js_stream) \
V(module_wrap) \
V(os) \
V(performance) \
V(pipe_wrap) \
V(process_wrap) \
V(serdes) \
V(signal_wrap) \
V(spawn_sync) \
V(stream_wrap) \
V(tcp_wrap) \
V(timer_wrap) \
V(tty_wrap) \
V(udp_wrap) \
V(url) \
V(util) \
V(uv) \
V(v8) \
V(zlib)

#define NODE_BUILTIN_MODULES(V) \
NODE_BUILTIN_STANDARD_MODULES(V) \
NODE_BUILTIN_OPENSSL_MODULES(V) \
NODE_BUILTIN_ICU_MODULES(V)

#define NODE_MODULE_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags) \
static node::node_module _module = { \
NODE_MODULE_VERSION, \
flags, \
nullptr, \
__FILE__, \
nullptr, \
(node::addon_context_register_func) (regfunc), \
NODE_STRINGIFY(modname), \
priv, \
nullptr \
}; \
void _register_ ## modname() { \
node_module_register(&_module); \
}


#define NODE_BUILTIN_MODULE_CONTEXT_AWARE(modname, regfunc) \
NODE_MODULE_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_BUILTIN)

namespace node {

// Set in node.cc by ParseArgs with the value of --openssl-config.
Expand Down Expand Up @@ -205,6 +280,12 @@ void SetupProcessObject(Environment* env,
int exec_argc,
const char* const* exec_argv);

// Call _register<module_name> functions for all of
// the built-in modules. Because built-in modules don't
// use the __attribute__((constructor)). Need to
// explicitly call the _register* functions.
void RegisterBuiltinModules();

enum Endianness {
kLittleEndian, // _Not_ LITTLE_ENDIAN, clashes with endian.h.
kBigEndian
Expand Down Expand Up @@ -328,8 +409,8 @@ class InternalCallbackScope {
bool closed_ = false;
};

#define NODE_MODULE_CONTEXT_AWARE_INTERNAL(modname, regfunc) \
NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, NM_F_INTERNAL) \
#define NODE_MODULE_CONTEXT_AWARE_INTERNAL(modname, regfunc) \
NODE_MODULE_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_INTERNAL)

} // namespace node

Expand Down
2 changes: 1 addition & 1 deletion src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,4 @@ void Initialize(Local<Object> target,
} // namespace os
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(os, node::os::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(os, node::os::Initialize)
2 changes: 1 addition & 1 deletion src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,4 @@ void Init(Local<Object> target,
} // namespace performance
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(performance, node::performance::Init)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(performance, node::performance::Init)
2 changes: 1 addition & 1 deletion src/node_serdes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,4 @@ void InitializeSerdesBindings(Local<Object> target,
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(serdes, node::InitializeSerdesBindings)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(serdes, node::InitializeSerdesBindings)
2 changes: 1 addition & 1 deletion src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2220,4 +2220,4 @@ static void Init(Local<Object> target,
} // namespace url
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(url, node::url::Init)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(url, node::url::Init)
2 changes: 1 addition & 1 deletion src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,4 @@ void Initialize(Local<Object> target,
} // namespace util
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(util, node::util::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(util, node::util::Initialize)
3 changes: 2 additions & 1 deletion src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "node.h"
#include "node_internals.h"
#include "env-inl.h"
#include "util-inl.h"
#include "v8.h"
Expand Down Expand Up @@ -200,4 +201,4 @@ void InitializeV8Bindings(Local<Object> target,

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(v8, node::InitializeV8Bindings)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(v8, node::InitializeV8Bindings)
2 changes: 1 addition & 1 deletion src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,4 @@ void InitZlib(Local<Object> target,
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(zlib, node::InitZlib)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(zlib, node::InitZlib)
2 changes: 1 addition & 1 deletion src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(pipe_wrap, node::PipeWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(pipe_wrap, node::PipeWrap::Initialize)
2 changes: 1 addition & 1 deletion src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,4 @@ class ProcessWrap : public HandleWrap {
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(process_wrap, node::ProcessWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(process_wrap, node::ProcessWrap::Initialize)
2 changes: 1 addition & 1 deletion src/signal_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ class SignalWrap : public HandleWrap {
} // namespace node


NODE_MODULE_CONTEXT_AWARE_BUILTIN(signal_wrap, node::SignalWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(signal_wrap, node::SignalWrap::Initialize)
2 changes: 1 addition & 1 deletion src/spawn_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1073,5 +1073,5 @@ void SyncProcessRunner::KillTimerCloseCallback(uv_handle_t* handle) {

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(spawn_sync,
NODE_BUILTIN_MODULE_CONTEXT_AWARE(spawn_sync,
node::SyncProcessRunner::Initialize)
2 changes: 1 addition & 1 deletion src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,5 @@ void LibuvStreamWrap::OnAfterWriteImpl(WriteWrap* w, void* ctx) {

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(stream_wrap,
NODE_BUILTIN_MODULE_CONTEXT_AWARE(stream_wrap,
node::LibuvStreamWrap::Initialize)
2 changes: 1 addition & 1 deletion src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,4 @@ Local<Object> AddressToJS(Environment* env,

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(tcp_wrap, node::TCPWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(tcp_wrap, node::TCPWrap::Initialize)
2 changes: 1 addition & 1 deletion src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ class TimerWrap : public HandleWrap {
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(timer_wrap, node::TimerWrap::Initialize)
2 changes: 1 addition & 1 deletion src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -982,4 +982,4 @@ void TLSWrap::Initialize(Local<Object> target,

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(tls_wrap, node::TLSWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(tls_wrap, node::TLSWrap::Initialize)
2 changes: 1 addition & 1 deletion src/tty_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ TTYWrap::TTYWrap(Environment* env,

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(tty_wrap, node::TTYWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(tty_wrap, node::TTYWrap::Initialize)
2 changes: 1 addition & 1 deletion src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,4 @@ uv_udp_t* UDPWrap::UVHandle() {

} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(udp_wrap, node::UDPWrap::Initialize)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(udp_wrap, node::UDPWrap::Initialize)
3 changes: 2 additions & 1 deletion src/uv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "uv.h"
#include "node.h"
#include "node_internals.h"
#include "env-inl.h"

namespace node {
Expand Down Expand Up @@ -58,4 +59,4 @@ void InitializeUV(Local<Object> target,
} // anonymous namespace
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(uv, node::InitializeUV)
NODE_BUILTIN_MODULE_CONTEXT_AWARE(uv, node::InitializeUV)
28 changes: 28 additions & 0 deletions test/cctest/node_module_reg.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Need to create empty definition for these modules'
// registration function for cctest. Because when
// building cctest, the definitions for the following
// registration functions are not included.
void _register_cares_wrap() {}
void _register_config() {}
void _register_contextify() {}
void _register_fs() {}
void _register_fs_event_wrap() {}
void _register_http2() {}
void _register_http_parser() {}
void _register_js_stream() {}
void _register_module_wrap() {}
void _register_os() {}
void _register_pipe_wrap() {}
void _register_process_wrap() {}
void _register_serdes() {}
void _register_signal_wrap() {}
void _register_spawn_sync() {}
void _register_stream_wrap() {}
void _register_tcp_wrap() {}
void _register_timer_wrap() {}
void _register_tty_wrap() {}
void _register_udp_wrap() {}
void _register_util() {}
void _register_uv() {}
void _register_v8() {}
void _register_zlib() {}

0 comments on commit a37dce4

Please sign in to comment.