1 |
|
|
#include "node_binding.h" |
2 |
|
|
#include <atomic> |
3 |
|
|
#include "env-inl.h" |
4 |
|
|
#include "node_builtins.h" |
5 |
|
|
#include "node_errors.h" |
6 |
|
|
#include "node_external_reference.h" |
7 |
|
|
#include "util.h" |
8 |
|
|
|
9 |
|
|
#include <string> |
10 |
|
|
|
11 |
|
|
#if HAVE_OPENSSL |
12 |
|
|
#define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap) |
13 |
|
|
#else |
14 |
|
|
#define NODE_BUILTIN_OPENSSL_MODULES(V) |
15 |
|
|
#endif |
16 |
|
|
|
17 |
|
|
#if NODE_HAVE_I18N_SUPPORT |
18 |
|
|
#define NODE_BUILTIN_ICU_MODULES(V) V(icu) |
19 |
|
|
#else |
20 |
|
|
#define NODE_BUILTIN_ICU_MODULES(V) |
21 |
|
|
#endif |
22 |
|
|
|
23 |
|
|
#if HAVE_INSPECTOR |
24 |
|
|
#define NODE_BUILTIN_PROFILER_MODULES(V) V(profiler) |
25 |
|
|
#else |
26 |
|
|
#define NODE_BUILTIN_PROFILER_MODULES(V) |
27 |
|
|
#endif |
28 |
|
|
|
29 |
|
|
// A list of built-in modules. In order to do module registration |
30 |
|
|
// in node::Init(), need to add built-in modules in the following list. |
31 |
|
|
// Then in binding::RegisterBuiltinModules(), it calls modules' registration |
32 |
|
|
// function. This helps the built-in modules are loaded properly when |
33 |
|
|
// node is built as static library. No need to depend on the |
34 |
|
|
// __attribute__((constructor)) like mechanism in GCC. |
35 |
|
|
#define NODE_BUILTIN_STANDARD_MODULES(V) \ |
36 |
|
|
V(async_wrap) \ |
37 |
|
|
V(blob) \ |
38 |
|
|
V(block_list) \ |
39 |
|
|
V(buffer) \ |
40 |
|
|
V(builtins) \ |
41 |
|
|
V(cares_wrap) \ |
42 |
|
|
V(config) \ |
43 |
|
|
V(contextify) \ |
44 |
|
|
V(credentials) \ |
45 |
|
|
V(errors) \ |
46 |
|
|
V(fs) \ |
47 |
|
|
V(fs_dir) \ |
48 |
|
|
V(fs_event_wrap) \ |
49 |
|
|
V(heap_utils) \ |
50 |
|
|
V(http2) \ |
51 |
|
|
V(http_parser) \ |
52 |
|
|
V(inspector) \ |
53 |
|
|
V(js_stream) \ |
54 |
|
|
V(js_udp_wrap) \ |
55 |
|
|
V(messaging) \ |
56 |
|
|
V(module_wrap) \ |
57 |
|
|
V(mksnapshot) \ |
58 |
|
|
V(options) \ |
59 |
|
|
V(os) \ |
60 |
|
|
V(performance) \ |
61 |
|
|
V(pipe_wrap) \ |
62 |
|
|
V(process_wrap) \ |
63 |
|
|
V(process_methods) \ |
64 |
|
|
V(report) \ |
65 |
|
|
V(serdes) \ |
66 |
|
|
V(signal_wrap) \ |
67 |
|
|
V(spawn_sync) \ |
68 |
|
|
V(stream_pipe) \ |
69 |
|
|
V(stream_wrap) \ |
70 |
|
|
V(string_decoder) \ |
71 |
|
|
V(symbols) \ |
72 |
|
|
V(task_queue) \ |
73 |
|
|
V(tcp_wrap) \ |
74 |
|
|
V(timers) \ |
75 |
|
|
V(trace_events) \ |
76 |
|
|
V(tty_wrap) \ |
77 |
|
|
V(types) \ |
78 |
|
|
V(udp_wrap) \ |
79 |
|
|
V(url) \ |
80 |
|
|
V(util) \ |
81 |
|
|
V(uv) \ |
82 |
|
|
V(v8) \ |
83 |
|
|
V(wasi) \ |
84 |
|
|
V(wasm_web_api) \ |
85 |
|
|
V(watchdog) \ |
86 |
|
|
V(worker) \ |
87 |
|
|
V(zlib) |
88 |
|
|
|
89 |
|
|
#define NODE_BUILTIN_MODULES(V) \ |
90 |
|
|
NODE_BUILTIN_STANDARD_MODULES(V) \ |
91 |
|
|
NODE_BUILTIN_OPENSSL_MODULES(V) \ |
92 |
|
|
NODE_BUILTIN_ICU_MODULES(V) \ |
93 |
|
|
NODE_BUILTIN_PROFILER_MODULES(V) |
94 |
|
|
|
95 |
|
|
// This is used to load built-in modules. Instead of using |
96 |
|
|
// __attribute__((constructor)), we call the _register_<modname> |
97 |
|
|
// function for each built-in modules explicitly in |
98 |
|
|
// binding::RegisterBuiltinModules(). This is only forward declaration. |
99 |
|
|
// The definitions are in each module's implementation when calling |
100 |
|
|
// the NODE_MODULE_CONTEXT_AWARE_INTERNAL. |
101 |
|
|
#define V(modname) void _register_##modname(); |
102 |
|
|
NODE_BUILTIN_MODULES(V) |
103 |
|
|
#undef V |
104 |
|
|
|
105 |
|
|
#ifdef _AIX |
106 |
|
|
// On AIX, dlopen() behaves differently from other operating systems, in that |
107 |
|
|
// it returns unique values from each call, rather than identical values, when |
108 |
|
|
// loading the same handle. |
109 |
|
|
// We try to work around that by providing wrappers for the dlopen() family of |
110 |
|
|
// functions, and using st_dev and st_ino for the file that is to be loaded |
111 |
|
|
// as keys for a cache. |
112 |
|
|
|
113 |
|
|
namespace node { |
114 |
|
|
namespace dlwrapper { |
115 |
|
|
|
116 |
|
|
struct dl_wrap { |
117 |
|
|
uint64_t st_dev; |
118 |
|
|
uint64_t st_ino; |
119 |
|
|
uint64_t refcount; |
120 |
|
|
void* real_handle; |
121 |
|
|
|
122 |
|
|
struct hash { |
123 |
|
|
size_t operator()(const dl_wrap* wrap) const { |
124 |
|
|
return std::hash<uint64_t>()(wrap->st_dev) ^ |
125 |
|
|
std::hash<uint64_t>()(wrap->st_ino); |
126 |
|
|
} |
127 |
|
|
}; |
128 |
|
|
|
129 |
|
|
struct equal { |
130 |
|
|
bool operator()(const dl_wrap* a, |
131 |
|
|
const dl_wrap* b) const { |
132 |
|
|
return a->st_dev == b->st_dev && a->st_ino == b->st_ino; |
133 |
|
|
} |
134 |
|
|
}; |
135 |
|
|
}; |
136 |
|
|
|
137 |
|
|
static Mutex dlhandles_mutex; |
138 |
|
|
static std::unordered_set<dl_wrap*, dl_wrap::hash, dl_wrap::equal> |
139 |
|
|
dlhandles; |
140 |
|
|
static thread_local std::string dlerror_storage; |
141 |
|
|
|
142 |
|
|
char* wrapped_dlerror() { |
143 |
|
|
return &dlerror_storage[0]; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
void* wrapped_dlopen(const char* filename, int flags) { |
147 |
|
|
CHECK_NOT_NULL(filename); // This deviates from the 'real' dlopen(). |
148 |
|
|
Mutex::ScopedLock lock(dlhandles_mutex); |
149 |
|
|
|
150 |
|
|
uv_fs_t req; |
151 |
|
|
auto cleanup = OnScopeLeave([&]() { uv_fs_req_cleanup(&req); }); |
152 |
|
|
int rc = uv_fs_stat(nullptr, &req, filename, nullptr); |
153 |
|
|
|
154 |
|
|
if (rc != 0) { |
155 |
|
|
dlerror_storage = uv_strerror(rc); |
156 |
|
|
return nullptr; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
dl_wrap search = { |
160 |
|
|
req.statbuf.st_dev, |
161 |
|
|
req.statbuf.st_ino, |
162 |
|
|
0, nullptr |
163 |
|
|
}; |
164 |
|
|
|
165 |
|
|
auto it = dlhandles.find(&search); |
166 |
|
|
if (it != dlhandles.end()) { |
167 |
|
|
(*it)->refcount++; |
168 |
|
|
return *it; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
void* real_handle = dlopen(filename, flags); |
172 |
|
|
if (real_handle == nullptr) { |
173 |
|
|
dlerror_storage = dlerror(); |
174 |
|
|
return nullptr; |
175 |
|
|
} |
176 |
|
|
dl_wrap* wrap = new dl_wrap(); |
177 |
|
|
wrap->st_dev = req.statbuf.st_dev; |
178 |
|
|
wrap->st_ino = req.statbuf.st_ino; |
179 |
|
|
wrap->refcount = 1; |
180 |
|
|
wrap->real_handle = real_handle; |
181 |
|
|
dlhandles.insert(wrap); |
182 |
|
|
return wrap; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
int wrapped_dlclose(void* handle) { |
186 |
|
|
Mutex::ScopedLock lock(dlhandles_mutex); |
187 |
|
|
dl_wrap* wrap = static_cast<dl_wrap*>(handle); |
188 |
|
|
int ret = 0; |
189 |
|
|
CHECK_GE(wrap->refcount, 1); |
190 |
|
|
if (--wrap->refcount == 0) { |
191 |
|
|
ret = dlclose(wrap->real_handle); |
192 |
|
|
if (ret != 0) dlerror_storage = dlerror(); |
193 |
|
|
dlhandles.erase(wrap); |
194 |
|
|
delete wrap; |
195 |
|
|
} |
196 |
|
|
return ret; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
void* wrapped_dlsym(void* handle, const char* symbol) { |
200 |
|
|
if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) |
201 |
|
|
return dlsym(handle, symbol); |
202 |
|
|
dl_wrap* wrap = static_cast<dl_wrap*>(handle); |
203 |
|
|
return dlsym(wrap->real_handle, symbol); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
#define dlopen node::dlwrapper::wrapped_dlopen |
207 |
|
|
#define dlerror node::dlwrapper::wrapped_dlerror |
208 |
|
|
#define dlclose node::dlwrapper::wrapped_dlclose |
209 |
|
|
#define dlsym node::dlwrapper::wrapped_dlsym |
210 |
|
|
|
211 |
|
|
} // namespace dlwrapper |
212 |
|
|
} // namespace node |
213 |
|
|
|
214 |
|
|
#endif // _AIX |
215 |
|
|
|
216 |
|
|
#ifdef __linux__ |
217 |
|
19 |
static bool libc_may_be_musl() { |
218 |
✓✓✓✗
|
19 |
static std::atomic_bool retval; // Cache the return value. |
219 |
|
|
static std::atomic_bool has_cached_retval { false }; |
220 |
✓✓ |
19 |
if (has_cached_retval) return retval; |
221 |
|
18 |
retval = dlsym(RTLD_DEFAULT, "gnu_get_libc_version") == nullptr; |
222 |
|
18 |
has_cached_retval = true; |
223 |
|
18 |
return retval; |
224 |
|
|
} |
225 |
|
|
#else // __linux__ |
226 |
|
|
static bool libc_may_be_musl() { return false; } |
227 |
|
|
#endif // __linux__ |
228 |
|
|
|
229 |
|
|
namespace node { |
230 |
|
|
|
231 |
|
|
using v8::Context; |
232 |
|
|
using v8::Exception; |
233 |
|
|
using v8::Function; |
234 |
|
|
using v8::FunctionCallbackInfo; |
235 |
|
|
using v8::Local; |
236 |
|
|
using v8::Object; |
237 |
|
|
using v8::String; |
238 |
|
|
using v8::Value; |
239 |
|
|
|
240 |
|
|
// Globals per process |
241 |
|
|
static node_module* modlist_internal; |
242 |
|
|
static node_module* modlist_linked; |
243 |
|
|
static thread_local node_module* thread_local_modpending; |
244 |
|
|
|
245 |
|
|
// This is set by node::Init() which is used by embedders |
246 |
|
|
bool node_is_initialized = false; |
247 |
|
|
|
248 |
|
304182 |
extern "C" void node_module_register(void* m) { |
249 |
|
304182 |
struct node_module* mp = reinterpret_cast<struct node_module*>(m); |
250 |
|
|
|
251 |
✓✓ |
304182 |
if (mp->nm_flags & NM_F_INTERNAL) { |
252 |
|
304024 |
mp->nm_link = modlist_internal; |
253 |
|
304024 |
modlist_internal = mp; |
254 |
✓✓ |
158 |
} else if (!node_is_initialized) { |
255 |
|
|
// "Linked" modules are included as part of the node project. |
256 |
|
|
// Like builtins they are registered *before* node::Init runs. |
257 |
|
1 |
mp->nm_flags = NM_F_LINKED; |
258 |
|
1 |
mp->nm_link = modlist_linked; |
259 |
|
1 |
modlist_linked = mp; |
260 |
|
|
} else { |
261 |
|
157 |
thread_local_modpending = mp; |
262 |
|
|
} |
263 |
|
304182 |
} |
264 |
|
|
|
265 |
|
|
namespace binding { |
266 |
|
|
|
267 |
|
|
static struct global_handle_map_t { |
268 |
|
|
public: |
269 |
|
156 |
void set(void* handle, node_module* mod) { |
270 |
✗✓ |
156 |
CHECK_NE(handle, nullptr); |
271 |
|
156 |
Mutex::ScopedLock lock(mutex_); |
272 |
|
|
|
273 |
|
156 |
map_[handle].module = mod; |
274 |
|
|
// We need to store this flag internally to avoid a chicken-and-egg problem |
275 |
|
|
// during cleanup. By the time we actually use the flag's value, |
276 |
|
|
// the shared object has been unloaded, and its memory would be gone, |
277 |
|
|
// making it impossible to access fields of `mod` -- |
278 |
|
|
// unless `mod` *is* dynamically allocated, but we cannot know that |
279 |
|
|
// without checking the flag. |
280 |
|
156 |
map_[handle].wants_delete_module = mod->nm_flags & NM_F_DELETEME; |
281 |
|
156 |
map_[handle].refcount++; |
282 |
|
156 |
} |
283 |
|
|
|
284 |
|
6 |
node_module* get_and_increase_refcount(void* handle) { |
285 |
✗✓ |
6 |
CHECK_NE(handle, nullptr); |
286 |
|
12 |
Mutex::ScopedLock lock(mutex_); |
287 |
|
|
|
288 |
|
6 |
auto it = map_.find(handle); |
289 |
✓✓ |
6 |
if (it == map_.end()) return nullptr; |
290 |
|
5 |
it->second.refcount++; |
291 |
|
5 |
return it->second.module; |
292 |
|
|
} |
293 |
|
|
|
294 |
|
16 |
void erase(void* handle) { |
295 |
✗✓ |
16 |
CHECK_NE(handle, nullptr); |
296 |
|
16 |
Mutex::ScopedLock lock(mutex_); |
297 |
|
|
|
298 |
|
16 |
auto it = map_.find(handle); |
299 |
✓✓ |
16 |
if (it == map_.end()) return; |
300 |
✗✓ |
15 |
CHECK_GE(it->second.refcount, 1); |
301 |
✓✓ |
15 |
if (--it->second.refcount == 0) { |
302 |
✓✓ |
10 |
if (it->second.wants_delete_module) |
303 |
|
4 |
delete it->second.module; |
304 |
|
10 |
map_.erase(handle); |
305 |
|
|
} |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
private: |
309 |
|
|
Mutex mutex_; |
310 |
|
|
struct Entry { |
311 |
|
|
unsigned int refcount; |
312 |
|
|
bool wants_delete_module; |
313 |
|
|
node_module* module; |
314 |
|
|
}; |
315 |
|
|
std::unordered_map<void*, Entry> map_; |
316 |
|
|
} global_handle_map; |
317 |
|
|
|
318 |
|
170 |
DLib::DLib(const char* filename, int flags) |
319 |
|
170 |
: filename_(filename), flags_(flags), handle_(nullptr) {} |
320 |
|
|
|
321 |
|
|
#ifdef __POSIX__ |
322 |
|
170 |
bool DLib::Open() { |
323 |
|
170 |
handle_ = dlopen(filename_.c_str(), flags_); |
324 |
✓✓ |
170 |
if (handle_ != nullptr) return true; |
325 |
|
3 |
errmsg_ = dlerror(); |
326 |
|
3 |
return false; |
327 |
|
|
} |
328 |
|
|
|
329 |
|
22 |
void DLib::Close() { |
330 |
✓✓ |
22 |
if (handle_ == nullptr) return; |
331 |
|
|
|
332 |
✗✓ |
19 |
if (libc_may_be_musl()) { |
333 |
|
|
// musl libc implements dlclose() as a no-op which returns 0. |
334 |
|
|
// As a consequence, trying to re-load a previously closed addon at a later |
335 |
|
|
// point will not call its static constructors, which Node.js uses. |
336 |
|
|
// Therefore, when we may be using musl libc, we assume that the shared |
337 |
|
|
// object exists indefinitely and keep it in our handle map. |
338 |
|
|
return; |
339 |
|
|
} |
340 |
|
|
|
341 |
|
19 |
int err = dlclose(handle_); |
342 |
✓✗ |
19 |
if (err == 0) { |
343 |
✓✓ |
19 |
if (has_entry_in_global_handle_map_) |
344 |
|
16 |
global_handle_map.erase(handle_); |
345 |
|
|
} |
346 |
|
19 |
handle_ = nullptr; |
347 |
|
|
} |
348 |
|
|
|
349 |
|
22 |
void* DLib::GetSymbolAddress(const char* name) { |
350 |
|
22 |
return dlsym(handle_, name); |
351 |
|
|
} |
352 |
|
|
#else // !__POSIX__ |
353 |
|
|
bool DLib::Open() { |
354 |
|
|
int ret = uv_dlopen(filename_.c_str(), &lib_); |
355 |
|
|
if (ret == 0) { |
356 |
|
|
handle_ = static_cast<void*>(lib_.handle); |
357 |
|
|
return true; |
358 |
|
|
} |
359 |
|
|
errmsg_ = uv_dlerror(&lib_); |
360 |
|
|
uv_dlclose(&lib_); |
361 |
|
|
return false; |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
void DLib::Close() { |
365 |
|
|
if (handle_ == nullptr) return; |
366 |
|
|
if (has_entry_in_global_handle_map_) |
367 |
|
|
global_handle_map.erase(handle_); |
368 |
|
|
uv_dlclose(&lib_); |
369 |
|
|
handle_ = nullptr; |
370 |
|
|
} |
371 |
|
|
|
372 |
|
|
void* DLib::GetSymbolAddress(const char* name) { |
373 |
|
|
void* address; |
374 |
|
|
if (0 == uv_dlsym(&lib_, name, &address)) return address; |
375 |
|
|
return nullptr; |
376 |
|
|
} |
377 |
|
|
#endif // !__POSIX__ |
378 |
|
|
|
379 |
|
156 |
void DLib::SaveInGlobalHandleMap(node_module* mp) { |
380 |
|
156 |
has_entry_in_global_handle_map_ = true; |
381 |
|
156 |
global_handle_map.set(handle_, mp); |
382 |
|
156 |
} |
383 |
|
|
|
384 |
|
6 |
node_module* DLib::GetSavedModuleFromGlobalHandleMap() { |
385 |
|
6 |
has_entry_in_global_handle_map_ = true; |
386 |
|
6 |
return global_handle_map.get_and_increase_refcount(handle_); |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
using InitializerCallback = void (*)(Local<Object> exports, |
390 |
|
|
Local<Value> module, |
391 |
|
|
Local<Context> context); |
392 |
|
|
|
393 |
|
13 |
inline InitializerCallback GetInitializerCallback(DLib* dlib) { |
394 |
|
13 |
const char* name = "node_register_module_v" STRINGIFY(NODE_MODULE_VERSION); |
395 |
|
13 |
return reinterpret_cast<InitializerCallback>(dlib->GetSymbolAddress(name)); |
396 |
|
|
} |
397 |
|
|
|
398 |
|
9 |
inline napi_addon_register_func GetNapiInitializerCallback(DLib* dlib) { |
399 |
|
9 |
const char* name = |
400 |
|
|
STRINGIFY(NAPI_MODULE_INITIALIZER_BASE) STRINGIFY(NAPI_MODULE_VERSION); |
401 |
|
|
return reinterpret_cast<napi_addon_register_func>( |
402 |
|
9 |
dlib->GetSymbolAddress(name)); |
403 |
|
|
} |
404 |
|
|
|
405 |
|
|
// DLOpen is process.dlopen(module, filename, flags). |
406 |
|
|
// Used to load 'module.node' dynamically shared objects. |
407 |
|
|
// |
408 |
|
|
// FIXME(bnoordhuis) Not multi-context ready. TBD how to resolve the conflict |
409 |
|
|
// when two contexts try to load the same shared object. Maybe have a shadow |
410 |
|
|
// cache that's a plain C list or hash table that's shared across contexts? |
411 |
|
177 |
void DLOpen(const FunctionCallbackInfo<Value>& args) { |
412 |
|
177 |
Environment* env = Environment::GetCurrent(args); |
413 |
|
|
|
414 |
✓✓ |
177 |
if (env->no_native_addons()) { |
415 |
|
6 |
return THROW_ERR_DLOPEN_DISABLED( |
416 |
|
7 |
env, "Cannot load native addon because loading addons is disabled."); |
417 |
|
|
} |
418 |
|
|
|
419 |
|
171 |
auto context = env->context(); |
420 |
|
|
|
421 |
✗✓ |
171 |
CHECK_NULL(thread_local_modpending); |
422 |
|
|
|
423 |
✗✓ |
171 |
if (args.Length() < 2) { |
424 |
|
|
return THROW_ERR_MISSING_ARGS( |
425 |
|
|
env, "process.dlopen needs at least 2 arguments"); |
426 |
|
|
} |
427 |
|
|
|
428 |
|
171 |
int32_t flags = DLib::kDefaultFlags; |
429 |
✓✓✗✓ ✗✓ |
173 |
if (args.Length() > 2 && !args[2]->Int32Value(context).To(&flags)) { |
430 |
|
|
return THROW_ERR_INVALID_ARG_TYPE(env, "flag argument must be an integer."); |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
Local<Object> module; |
434 |
|
|
Local<Object> exports; |
435 |
|
|
Local<Value> exports_v; |
436 |
|
171 |
if (!args[0]->ToObject(context).ToLocal(&module) || |
437 |
✓✗✓✗
|
855 |
!module->Get(context, env->exports_string()).ToLocal(&exports_v) || |
438 |
✓✓✓✓
|
513 |
!exports_v->ToObject(context).ToLocal(&exports)) { |
439 |
|
1 |
return; // Exception pending. |
440 |
|
|
} |
441 |
|
|
|
442 |
|
170 |
node::Utf8Value filename(env->isolate(), args[1]); // Cast |
443 |
|
170 |
env->TryLoadAddon(*filename, flags, [&](DLib* dlib) { |
444 |
✓✓✓✗
|
170 |
static Mutex dlib_load_mutex; |
445 |
|
340 |
Mutex::ScopedLock lock(dlib_load_mutex); |
446 |
|
|
|
447 |
|
170 |
const bool is_opened = dlib->Open(); |
448 |
|
|
|
449 |
|
|
// Objects containing v14 or later modules will have registered themselves |
450 |
|
|
// on the pending list. Activate all of them now. At present, only one |
451 |
|
|
// module per object is supported. |
452 |
|
170 |
node_module* mp = thread_local_modpending; |
453 |
|
170 |
thread_local_modpending = nullptr; |
454 |
|
|
|
455 |
✓✓ |
170 |
if (!is_opened) { |
456 |
|
3 |
std::string errmsg = dlib->errmsg_.c_str(); |
457 |
|
3 |
dlib->Close(); |
458 |
|
|
#ifdef _WIN32 |
459 |
|
|
// Windows needs to add the filename into the error message |
460 |
|
|
errmsg += *filename; |
461 |
|
|
#endif // _WIN32 |
462 |
|
3 |
THROW_ERR_DLOPEN_FAILED(env, errmsg.c_str()); |
463 |
|
3 |
return false; |
464 |
|
|
} |
465 |
|
|
|
466 |
✓✓ |
167 |
if (mp != nullptr) { |
467 |
✓✓ |
157 |
if (mp->nm_context_register_func == nullptr) { |
468 |
✓✓ |
41 |
if (env->force_context_aware()) { |
469 |
|
1 |
dlib->Close(); |
470 |
|
1 |
THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env); |
471 |
|
1 |
return false; |
472 |
|
|
} |
473 |
|
|
} |
474 |
|
156 |
mp->nm_dso_handle = dlib->handle_; |
475 |
|
156 |
dlib->SaveInGlobalHandleMap(mp); |
476 |
|
|
} else { |
477 |
✓✓ |
10 |
if (auto callback = GetInitializerCallback(dlib)) { |
478 |
|
4 |
callback(exports, module, context); |
479 |
|
1 |
return true; |
480 |
✓✓ |
9 |
} else if (auto napi_callback = GetNapiInitializerCallback(dlib)) { |
481 |
|
3 |
napi_module_register_by_symbol(exports, module, context, napi_callback); |
482 |
|
3 |
return true; |
483 |
|
|
} else { |
484 |
|
6 |
mp = dlib->GetSavedModuleFromGlobalHandleMap(); |
485 |
✓✓✓✓
|
6 |
if (mp == nullptr || mp->nm_context_register_func == nullptr) { |
486 |
|
3 |
dlib->Close(); |
487 |
|
|
char errmsg[1024]; |
488 |
|
6 |
snprintf(errmsg, |
489 |
|
|
sizeof(errmsg), |
490 |
|
|
"Module did not self-register: '%s'.", |
491 |
|
3 |
*filename); |
492 |
|
3 |
THROW_ERR_DLOPEN_FAILED(env, errmsg); |
493 |
|
3 |
return false; |
494 |
|
|
} |
495 |
|
|
} |
496 |
|
|
} |
497 |
|
|
|
498 |
|
|
// -1 is used for N-API modules |
499 |
✓✓✓✓
|
159 |
if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) { |
500 |
|
|
// Even if the module did self-register, it may have done so with the |
501 |
|
|
// wrong version. We must only give up after having checked to see if it |
502 |
|
|
// has an appropriate initializer callback. |
503 |
✓✓ |
3 |
if (auto callback = GetInitializerCallback(dlib)) { |
504 |
|
2 |
callback(exports, module, context); |
505 |
|
2 |
return true; |
506 |
|
|
} |
507 |
|
|
char errmsg[1024]; |
508 |
|
1 |
snprintf(errmsg, |
509 |
|
|
sizeof(errmsg), |
510 |
|
|
"The module '%s'" |
511 |
|
|
"\nwas compiled against a different Node.js version using" |
512 |
|
|
"\nNODE_MODULE_VERSION %d. This version of Node.js requires" |
513 |
|
|
"\nNODE_MODULE_VERSION %d. Please try re-compiling or " |
514 |
|
|
"re-installing\nthe module (for instance, using `npm rebuild` " |
515 |
|
|
"or `npm install`).", |
516 |
|
|
*filename, |
517 |
|
|
mp->nm_version, |
518 |
|
|
NODE_MODULE_VERSION); |
519 |
|
|
|
520 |
|
|
// NOTE: `mp` is allocated inside of the shared library's memory, calling |
521 |
|
|
// `dlclose` will deallocate it |
522 |
|
1 |
dlib->Close(); |
523 |
|
1 |
THROW_ERR_DLOPEN_FAILED(env, errmsg); |
524 |
|
1 |
return false; |
525 |
|
|
} |
526 |
✗✓ |
156 |
CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0); |
527 |
|
|
|
528 |
|
|
// Do not keep the lock while running userland addon loading code. |
529 |
|
312 |
Mutex::ScopedUnlock unlock(lock); |
530 |
✓✓ |
156 |
if (mp->nm_context_register_func != nullptr) { |
531 |
|
118 |
mp->nm_context_register_func(exports, module, context, mp->nm_priv); |
532 |
✓✗ |
38 |
} else if (mp->nm_register_func != nullptr) { |
533 |
|
38 |
mp->nm_register_func(exports, module, mp->nm_priv); |
534 |
|
|
} else { |
535 |
|
|
dlib->Close(); |
536 |
|
|
THROW_ERR_DLOPEN_FAILED(env, "Module has no declared entry point."); |
537 |
|
|
return false; |
538 |
|
|
} |
539 |
|
|
|
540 |
|
156 |
return true; |
541 |
|
|
}); |
542 |
|
|
|
543 |
|
|
// Tell coverity that 'handle' should not be freed when we return. |
544 |
|
|
// coverity[leaked_storage] |
545 |
|
|
} |
546 |
|
|
|
547 |
|
87585 |
inline struct node_module* FindModule(struct node_module* list, |
548 |
|
|
const char* name, |
549 |
|
|
int flag) { |
550 |
|
|
struct node_module* mp; |
551 |
|
|
|
552 |
✓✓ |
2482852 |
for (mp = list; mp != nullptr; mp = mp->nm_link) { |
553 |
✓✓ |
2482034 |
if (strcmp(mp->nm_modname, name) == 0) break; |
554 |
|
|
} |
555 |
|
|
|
556 |
✓✓✗✓
|
87585 |
CHECK(mp == nullptr || (mp->nm_flags & flag) != 0); |
557 |
|
87585 |
return mp; |
558 |
|
|
} |
559 |
|
|
|
560 |
|
86757 |
static Local<Object> InitModule(Environment* env, |
561 |
|
|
node_module* mod, |
562 |
|
|
Local<String> module) { |
563 |
|
|
// Internal bindings don't have a "module" object, only exports. |
564 |
|
86757 |
Local<Function> ctor = env->binding_data_ctor_template() |
565 |
|
86757 |
->GetFunction(env->context()) |
566 |
|
86757 |
.ToLocalChecked(); |
567 |
|
86757 |
Local<Object> exports = ctor->NewInstance(env->context()).ToLocalChecked(); |
568 |
✗✓ |
86757 |
CHECK_NULL(mod->nm_register_func); |
569 |
✗✓ |
86757 |
CHECK_NOT_NULL(mod->nm_context_register_func); |
570 |
|
86757 |
Local<Value> unused = Undefined(env->isolate()); |
571 |
|
86757 |
mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv); |
572 |
|
86757 |
return exports; |
573 |
|
|
} |
574 |
|
|
|
575 |
|
87574 |
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) { |
576 |
|
87574 |
Environment* env = Environment::GetCurrent(args); |
577 |
|
|
|
578 |
✗✓ |
175148 |
CHECK(args[0]->IsString()); |
579 |
|
|
|
580 |
|
175148 |
Local<String> module = args[0].As<String>(); |
581 |
|
87574 |
node::Utf8Value module_v(env->isolate(), module); |
582 |
|
|
Local<Object> exports; |
583 |
|
|
|
584 |
|
87574 |
node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL); |
585 |
✓✓ |
87574 |
if (mod != nullptr) { |
586 |
|
86757 |
exports = InitModule(env, mod, module); |
587 |
|
86757 |
env->internal_bindings.insert(mod); |
588 |
✓✓ |
817 |
} else if (!strcmp(*module_v, "constants")) { |
589 |
|
775 |
exports = Object::New(env->isolate()); |
590 |
✗✓ |
2325 |
CHECK( |
591 |
|
|
exports->SetPrototype(env->context(), Null(env->isolate())).FromJust()); |
592 |
|
775 |
DefineConstants(env->isolate(), exports); |
593 |
✓✗ |
42 |
} else if (!strcmp(*module_v, "natives")) { |
594 |
|
42 |
exports = builtins::BuiltinLoader::GetSourceObject(env->context()); |
595 |
|
|
// Legacy feature: process.binding('natives').config contains stringified |
596 |
|
|
// config.gypi |
597 |
✗✓ |
168 |
CHECK(exports |
598 |
|
|
->Set(env->context(), |
599 |
|
|
env->config_string(), |
600 |
|
|
builtins::BuiltinLoader::GetConfigString(env->isolate())) |
601 |
|
|
.FromJust()); |
602 |
|
|
} else { |
603 |
|
|
char errmsg[1024]; |
604 |
|
|
snprintf(errmsg, sizeof(errmsg), "No such module: %s", *module_v); |
605 |
|
|
return THROW_ERR_INVALID_MODULE(env, errmsg); |
606 |
|
|
} |
607 |
|
|
|
608 |
|
175148 |
args.GetReturnValue().Set(exports); |
609 |
|
|
} |
610 |
|
|
|
611 |
|
10 |
void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) { |
612 |
|
10 |
Environment* env = Environment::GetCurrent(args); |
613 |
|
|
|
614 |
✗✓ |
20 |
CHECK(args[0]->IsString()); |
615 |
|
|
|
616 |
|
20 |
Local<String> module_name = args[0].As<String>(); |
617 |
|
|
|
618 |
|
10 |
node::Utf8Value module_name_v(env->isolate(), module_name); |
619 |
|
10 |
const char* name = *module_name_v; |
620 |
|
10 |
node_module* mod = nullptr; |
621 |
|
|
|
622 |
|
|
// Iterate from here to the nearest non-Worker Environment to see if there's |
623 |
|
|
// a linked binding defined locally rather than through the global list. |
624 |
|
10 |
Environment* cur_env = env; |
625 |
✓✓✓✓
|
20 |
while (mod == nullptr && cur_env != nullptr) { |
626 |
|
20 |
Mutex::ScopedLock lock(cur_env->extra_linked_bindings_mutex()); |
627 |
|
10 |
mod = FindModule(cur_env->extra_linked_bindings_head(), name, NM_F_LINKED); |
628 |
|
10 |
cur_env = cur_env->worker_parent_env(); |
629 |
|
|
} |
630 |
|
|
|
631 |
✓✓ |
10 |
if (mod == nullptr) |
632 |
|
1 |
mod = FindModule(modlist_linked, name, NM_F_LINKED); |
633 |
|
|
|
634 |
✗✓ |
10 |
if (mod == nullptr) { |
635 |
|
|
char errmsg[1024]; |
636 |
|
|
snprintf(errmsg, |
637 |
|
|
sizeof(errmsg), |
638 |
|
|
"No such module was linked: %s", |
639 |
|
|
*module_name_v); |
640 |
|
|
return THROW_ERR_INVALID_MODULE(env, errmsg); |
641 |
|
|
} |
642 |
|
|
|
643 |
|
10 |
Local<Object> module = Object::New(env->isolate()); |
644 |
|
10 |
Local<Object> exports = Object::New(env->isolate()); |
645 |
|
|
Local<String> exports_prop = |
646 |
|
10 |
String::NewFromUtf8Literal(env->isolate(), "exports"); |
647 |
|
10 |
module->Set(env->context(), exports_prop, exports).Check(); |
648 |
|
|
|
649 |
✓✗ |
10 |
if (mod->nm_context_register_func != nullptr) { |
650 |
|
20 |
mod->nm_context_register_func( |
651 |
|
|
exports, module, env->context(), mod->nm_priv); |
652 |
|
|
} else if (mod->nm_register_func != nullptr) { |
653 |
|
|
mod->nm_register_func(exports, module, mod->nm_priv); |
654 |
|
|
} else { |
655 |
|
|
return THROW_ERR_INVALID_MODULE( |
656 |
|
|
env, |
657 |
|
|
"Linked moduled has no declared entry point."); |
658 |
|
|
} |
659 |
|
|
|
660 |
|
|
auto effective_exports = |
661 |
|
20 |
module->Get(env->context(), exports_prop).ToLocalChecked(); |
662 |
|
|
|
663 |
|
20 |
args.GetReturnValue().Set(effective_exports); |
664 |
|
|
} |
665 |
|
|
|
666 |
|
|
// Call built-in modules' _register_<module name> function to |
667 |
|
|
// do module registration explicitly. |
668 |
|
5429 |
void RegisterBuiltinModules() { |
669 |
|
|
#define V(modname) _register_##modname(); |
670 |
|
5429 |
NODE_BUILTIN_MODULES(V) |
671 |
|
|
#undef V |
672 |
|
5429 |
} |
673 |
|
|
|
674 |
|
5357 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
675 |
|
5357 |
registry->Register(GetLinkedBinding); |
676 |
|
5357 |
registry->Register(GetInternalBinding); |
677 |
|
5357 |
} |
678 |
|
|
|
679 |
|
|
} // namespace binding |
680 |
|
|
} // namespace node |
681 |
|
|
|
682 |
|
5357 |
NODE_MODULE_EXTERNAL_REFERENCE(binding, |
683 |
|
|
node::binding::RegisterExternalReferences) |