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