forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_binding.cc
More file actions
1083 lines (956 loc) · 38.4 KB
/
Copy pathnode_binding.cc
File metadata and controls
1083 lines (956 loc) · 38.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "node_binding.h"
#include <atomic>
#include "env-inl.h"
#include "node_builtins.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_url_pattern.h"
#include "permission/permission.h"
#include "util.h"
#include <string>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#if defined(__linux__)
#include <sys/mman.h>
#include <sys/syscall.h>
#endif
#endif
#if HAVE_OPENSSL
#define NODE_BUILTIN_OPENSSL_BINDINGS(V) V(crypto) V(tls_wrap)
#else
#define NODE_BUILTIN_OPENSSL_BINDINGS(V)
#endif
#if HAVE_INSPECTOR
#define NODE_BUILTIN_PROFILER_BINDINGS(V) V(profiler)
#else
#define NODE_BUILTIN_PROFILER_BINDINGS(V)
#endif
#ifdef DEBUG
#define NODE_BUILTIN_DEBUG_BINDINGS(V) V(debug)
#else
#define NODE_BUILTIN_DEBUG_BINDINGS(V)
#endif
// A list of built-in bindings. In order to do binding registration
// in node::Init(), need to add built-in bindings in the following list.
// Then in binding::RegisterBuiltinBindings(), it calls bindings' registration
// function. This helps the built-in bindings are loaded properly when
// node is built as static library. No need to depend on the
// __attribute__((constructor)) like mechanism in GCC.
// The binding IDs that start with 'internal_only' are not exposed to the user
// land even from internal/test/binding module under --expose-internals.
#define NODE_BUILTIN_STANDARD_BINDINGS(V) \
V(async_context_frame) \
V(async_wrap) \
V(blob) \
V(block_list) \
V(buffer) \
V(builtins) \
V(cares_wrap) \
V(cjs_lexer) \
V(config) \
V(constants) \
V(contextify) \
V(credentials) \
V(diagnostics_channel) \
V(encoding_binding) \
V(errors) \
V(fs) \
V(fs_dir) \
V(fs_event_wrap) \
V(heap_utils) \
V(http2) \
V(http_parser) \
V(inspector) \
V(internal_only_v8) \
V(ipc_serdes) \
V(js_stream) \
V(js_udp_wrap) \
V(locks) \
V(messaging) \
V(modules) \
V(module_wrap) \
V(mksnapshot) \
V(options) \
V(os) \
V(performance) \
V(permission) \
V(pipe_wrap) \
V(process_wrap) \
V(process_methods) \
V(report) \
V(sea) \
V(serdes) \
V(signal_wrap) \
V(spawn_sync) \
V(stream_pipe) \
V(stream_wrap) \
V(string_decoder) \
V(symbols) \
V(task_queue) \
V(tcp_wrap) \
V(timers) \
V(trace_events) \
V(tty_wrap) \
V(types) \
V(udp_wrap) \
V(url) \
V(url_pattern) \
V(util) \
V(uv) \
V(v8) \
V(wasi) \
V(wasm_web_api) \
V(watchdog) \
V(worker) \
V(zlib)
#define NODE_BUILTIN_BINDINGS(V) \
NODE_BUILTIN_STANDARD_BINDINGS(V) \
NODE_BUILTIN_OPENSSL_BINDINGS(V) \
NODE_BUILTIN_ICU_BINDINGS(V) \
NODE_BUILTIN_PROFILER_BINDINGS(V) \
NODE_BUILTIN_DEBUG_BINDINGS(V) \
NODE_BUILTIN_DTLS_BINDINGS(V) \
NODE_BUILTIN_QUIC_BINDINGS(V) \
NODE_BUILTIN_SQLITE_BINDINGS(V) \
NODE_BUILTIN_FFI_BINDINGS(V)
// This is used to load built-in bindings. Instead of using
// __attribute__((constructor)), we call the _register_<modname>
// function for each built-in bindings explicitly in
// binding::RegisterBuiltinBindings(). This is only forward declaration.
// The definitions are in each binding's implementation when calling
// the NODE_BINDING_CONTEXT_AWARE_INTERNAL.
#define V(modname) void _register_##modname();
NODE_BUILTIN_BINDINGS(V)
#undef V
#define V(modname) \
void _register_isolate_##modname(node::IsolateData* isolate_data, \
v8::Local<v8::ObjectTemplate> target);
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V
#ifdef _AIX
// On AIX, dlopen() behaves differently from other operating systems, in that
// it returns unique values from each call, rather than identical values, when
// loading the same handle.
// We try to work around that by providing wrappers for the dlopen() family of
// functions, and using st_dev and st_ino for the file that is to be loaded
// as keys for a cache.
namespace node {
namespace dlwrapper {
struct dl_wrap {
uint64_t st_dev;
uint64_t st_ino;
uint64_t refcount;
void* real_handle;
struct hash {
size_t operator()(const dl_wrap* wrap) const {
return std::hash<uint64_t>()(wrap->st_dev) ^
std::hash<uint64_t>()(wrap->st_ino);
}
};
struct equal {
bool operator()(const dl_wrap* a,
const dl_wrap* b) const {
return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
}
};
};
static Mutex dlhandles_mutex;
static std::unordered_set<dl_wrap*, dl_wrap::hash, dl_wrap::equal>
dlhandles;
static thread_local std::string dlerror_storage;
char* wrapped_dlerror() {
return &dlerror_storage[0];
}
void* wrapped_dlopen(const char* filename, int flags) {
CHECK_NOT_NULL(filename); // This deviates from the 'real' dlopen().
Mutex::ScopedLock lock(dlhandles_mutex);
uv_fs_t req;
auto cleanup = OnScopeLeave([&]() { uv_fs_req_cleanup(&req); });
int rc = uv_fs_stat(nullptr, &req, filename, nullptr);
if (rc != 0) {
dlerror_storage = uv_strerror(rc);
return nullptr;
}
dl_wrap search = {
req.statbuf.st_dev,
req.statbuf.st_ino,
0, nullptr
};
auto it = dlhandles.find(&search);
if (it != dlhandles.end()) {
(*it)->refcount++;
return *it;
}
void* real_handle = dlopen(filename, flags);
if (real_handle == nullptr) {
dlerror_storage = dlerror();
return nullptr;
}
dl_wrap* wrap = new dl_wrap();
wrap->st_dev = req.statbuf.st_dev;
wrap->st_ino = req.statbuf.st_ino;
wrap->refcount = 1;
wrap->real_handle = real_handle;
dlhandles.insert(wrap);
return wrap;
}
int wrapped_dlclose(void* handle) {
Mutex::ScopedLock lock(dlhandles_mutex);
dl_wrap* wrap = static_cast<dl_wrap*>(handle);
int ret = 0;
CHECK_GE(wrap->refcount, 1);
if (--wrap->refcount == 0) {
ret = dlclose(wrap->real_handle);
if (ret != 0) dlerror_storage = dlerror();
dlhandles.erase(wrap);
delete wrap;
}
return ret;
}
void* wrapped_dlsym(void* handle, const char* symbol) {
if (handle == RTLD_DEFAULT || handle == RTLD_NEXT)
return dlsym(handle, symbol);
dl_wrap* wrap = static_cast<dl_wrap*>(handle);
return dlsym(wrap->real_handle, symbol);
}
#define dlopen node::dlwrapper::wrapped_dlopen
#define dlerror node::dlwrapper::wrapped_dlerror
#define dlclose node::dlwrapper::wrapped_dlclose
#define dlsym node::dlwrapper::wrapped_dlsym
} // namespace dlwrapper
} // namespace node
#endif // _AIX
#ifdef __linux__
static bool libc_may_be_musl() {
static std::atomic_bool retval; // Cache the return value.
static std::atomic_bool has_cached_retval { false };
if (has_cached_retval) return retval;
retval = dlsym(RTLD_DEFAULT, "gnu_get_libc_version") == nullptr;
has_cached_retval = true;
return retval;
}
#elif defined(__POSIX__)
static bool libc_may_be_musl() { return false; }
#endif // __linux__
namespace node {
using v8::Context;
using v8::EscapableHandleScope;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;
// Globals per process
static node_module* modlist_internal;
static node_module* modlist_linked;
static thread_local node_module* thread_local_modpending;
// This is set by node::Init() which is used by embedders
bool node_is_initialized = false;
extern "C" void node_module_register(void* m) {
struct node_module* mp = reinterpret_cast<struct node_module*>(m);
if (mp->nm_flags & NM_F_INTERNAL) {
mp->nm_link = modlist_internal;
modlist_internal = mp;
} else if (!node_is_initialized) {
// "Linked" modules are included as part of the node project.
// Like builtins they are registered *before* node::Init runs.
mp->nm_flags = NM_F_LINKED;
mp->nm_link = modlist_linked;
modlist_linked = mp;
} else {
thread_local_modpending = mp;
}
}
namespace binding {
static struct global_handle_map_t {
public:
void set(void* handle, node_module* mod) {
CHECK_NE(handle, nullptr);
Mutex::ScopedLock lock(mutex_);
map_[handle].module = mod;
// We need to store this flag internally to avoid a chicken-and-egg problem
// during cleanup. By the time we actually use the flag's value,
// the shared object has been unloaded, and its memory would be gone,
// making it impossible to access fields of `mod` --
// unless `mod` *is* dynamically allocated, but we cannot know that
// without checking the flag.
map_[handle].wants_delete_module = mod->nm_flags & NM_F_DELETEME;
map_[handle].refcount++;
}
node_module* get_and_increase_refcount(void* handle) {
CHECK_NE(handle, nullptr);
Mutex::ScopedLock lock(mutex_);
auto it = map_.find(handle);
if (it == map_.end()) return nullptr;
it->second.refcount++;
return it->second.module;
}
void erase(void* handle) {
CHECK_NE(handle, nullptr);
Mutex::ScopedLock lock(mutex_);
auto it = map_.find(handle);
if (it == map_.end()) return;
CHECK_GE(it->second.refcount, 1);
if (--it->second.refcount == 0) {
if (it->second.wants_delete_module)
delete it->second.module;
map_.erase(handle);
}
}
private:
Mutex mutex_;
struct Entry {
unsigned int refcount;
bool wants_delete_module;
node_module* module;
};
std::unordered_map<void*, Entry> map_;
} global_handle_map;
DLib::DLib(const char* filename, int flags)
: filename_(filename), flags_(flags), handle_(nullptr) {}
#ifdef __POSIX__
bool DLib::Open() {
handle_ = dlopen(filename_.c_str(), flags_);
if (handle_ != nullptr) return true;
errmsg_ = dlerror();
return false;
}
void DLib::Close() {
if (handle_ == nullptr) return;
if (libc_may_be_musl()) {
// musl libc implements dlclose() as a no-op which returns 0.
// As a consequence, trying to re-load a previously closed addon at a later
// point will not call its static constructors, which Node.js uses.
// Therefore, when we may be using musl libc, we assume that the shared
// object exists indefinitely and keep it in our handle map.
return;
}
int err = dlclose(handle_);
if (err == 0) {
if (has_entry_in_global_handle_map_)
global_handle_map.erase(handle_);
}
handle_ = nullptr;
}
void* DLib::GetSymbolAddress(const char* name) {
return dlsym(handle_, name);
}
#else // !__POSIX__
bool DLib::Open() {
int ret = uv_dlopen(filename_.c_str(), &lib_);
if (ret == 0) {
handle_ = static_cast<void*>(lib_.handle);
return true;
}
errmsg_ = uv_dlerror(&lib_);
uv_dlclose(&lib_);
return false;
}
void DLib::Close() {
if (handle_ == nullptr) return;
if (has_entry_in_global_handle_map_)
global_handle_map.erase(handle_);
uv_dlclose(&lib_);
handle_ = nullptr;
}
void* DLib::GetSymbolAddress(const char* name) {
void* address;
if (0 == uv_dlsym(&lib_, name, &address)) return address;
return nullptr;
}
#endif // !__POSIX__
void DLib::SaveInGlobalHandleMap(node_module* mp) {
has_entry_in_global_handle_map_ = true;
global_handle_map.set(handle_, mp);
}
node_module* DLib::GetSavedModuleFromGlobalHandleMap() {
has_entry_in_global_handle_map_ = true;
return global_handle_map.get_and_increase_refcount(handle_);
}
using InitializerCallback = void (*)(Local<Object> exports,
Local<Value> module,
Local<Context> context);
inline InitializerCallback GetInitializerCallback(DLib* dlib) {
const char* name = "node_register_module_v" STRINGIFY(NODE_MODULE_VERSION);
return reinterpret_cast<InitializerCallback>(dlib->GetSymbolAddress(name));
}
inline napi_addon_register_func GetNapiInitializerCallback(DLib* dlib) {
const char* name =
STRINGIFY(NAPI_MODULE_INITIALIZER_BASE) STRINGIFY(NAPI_MODULE_VERSION);
return reinterpret_cast<napi_addon_register_func>(
dlib->GetSymbolAddress(name));
}
inline node_api_addon_get_api_version_func GetNapiAddonGetApiVersionCallback(
DLib* dlib) {
return reinterpret_cast<node_api_addon_get_api_version_func>(
dlib->GetSymbolAddress(STRINGIFY(NODE_API_MODULE_GET_API_VERSION)));
}
namespace {
#ifndef _WIN32
// Write the whole buffer to `fd`, retrying partial writes and EINTR.
bool WriteAllToFd(int fd, const char* data, size_t len) {
size_t off = 0;
while (off < len) {
ssize_t n = write(fd, data + off, len - off);
if (n < 0) {
if (errno == EINTR) continue;
return false;
}
off += static_cast<size_t>(n);
}
return true;
}
#if defined(__linux__)
int NodeMemfdCreate(const char* name, unsigned int flags) {
return static_cast<int>(syscall(SYS_memfd_create, name, flags));
}
#endif // __linux__
#endif // !_WIN32
// Materializes native-addon bytes into a form dlopen()/LoadLibrary() can load,
// with the smallest, most private on-disk footprint each platform allows:
// Linux: an anonymous in-memory memfd, loaded via /proc/self/fd/N -
// the bytes never touch the filesystem.
// other POSIX: a 0700 mkdtemp() directory plus an O_EXCL|O_NOFOLLOW file,
// unlink()ed right after the load (the mapping keeps it alive).
// Windows: a temp file opened FILE_FLAG_DELETE_ON_CLOSE; its handle is
// retained for the process lifetime so the file is removed
// automatically once the process (and the loaded DLL) exit.
// Used for an addon that lives somewhere dlopen() cannot open by path, such as
// a virtual file system.
class AddonImage {
public:
AddonImage() = default;
~AddonImage();
AddonImage(const AddonImage&) = delete;
AddonImage& operator=(const AddonImage&) = delete;
// The directory a temporary image would be written to, with a trailing
// separator; empty when it cannot be determined. Names the resource for the
// file-system permission check.
static std::string TempDir();
// On success sets path() to a real, loadable path for `data`.
bool Materialize(const char* data, size_t len);
const std::string& path() const { return path_; }
const std::string& errmsg() const { return errmsg_; }
// Call exactly once, right after DLib::Open(); `opened` says whether the load
// succeeded. Releases the transient resources that are no longer needed (a
// successful load holds its own mapping): on POSIX closes the memfd or
// unlinks the temp file; on Windows retains the delete-on-close handle for
// the process lifetime when opened, or closes it (deleting the file) on
// failure.
void AfterOpen(bool opened);
private:
std::string path_;
std::string errmsg_;
bool consumed_ = false;
#ifdef _WIN32
HANDLE handle_ = INVALID_HANDLE_VALUE;
#else
bool MaterializeTempFile(const char* data, size_t len);
int fd_ = -1;
std::string temp_dir_; // non-empty only for the temp-file (non-memfd) path
#endif
};
#ifdef _WIN32
// Delete-on-close handles kept alive until process exit so their temp files
// outlive the loaded DLLs and are removed once the process ends.
Mutex g_retained_addon_handles_mutex;
std::vector<HANDLE>* g_retained_addon_handles = nullptr;
// static
std::string AddonImage::TempDir() {
wchar_t dir[MAX_PATH + 1];
DWORD dir_len = GetTempPathW(MAX_PATH + 1, dir);
if (dir_len == 0 || dir_len > MAX_PATH) return std::string();
int size = WideCharToMultiByte(
CP_UTF8, 0, dir, dir_len, nullptr, 0, nullptr, nullptr);
if (size <= 0) return std::string();
std::string out(size, '\0');
WideCharToMultiByte(
CP_UTF8, 0, dir, dir_len, out.data(), size, nullptr, nullptr);
return out;
}
bool AddonImage::Materialize(const char* data, size_t len) {
wchar_t dir[MAX_PATH + 1];
DWORD dir_len = GetTempPathW(MAX_PATH + 1, dir);
if (dir_len == 0 || dir_len > MAX_PATH) {
errmsg_ = "could not locate the temporary directory";
return false;
}
wchar_t file[MAX_PATH + 1];
if (GetTempFileNameW(dir, L"nod", 0, file) == 0) {
errmsg_ = "could not create a temporary file name";
return false;
}
// Reopen the just-created file delete-on-close, sharing delete so the loader
// can map it while it is delete-pending; the file is removed when this handle
// and the loader's section are both released (i.e. at process exit).
handle_ = CreateFileW(file,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
nullptr);
if (handle_ == INVALID_HANDLE_VALUE) {
errmsg_ = "could not create a temporary file for the native addon";
return false;
}
size_t off = 0;
while (off < len) {
DWORD chunk =
len - off > MAXDWORD ? MAXDWORD : static_cast<DWORD>(len - off);
DWORD written = 0;
if (!WriteFile(handle_, data + off, chunk, &written, nullptr)) {
errmsg_ = "could not write the native addon to a temporary file";
CloseHandle(handle_);
handle_ = INVALID_HANDLE_VALUE;
return false;
}
off += written;
}
int utf8_len =
WideCharToMultiByte(CP_UTF8, 0, file, -1, nullptr, 0, nullptr, nullptr);
if (utf8_len <= 0) {
errmsg_ = "could not encode the temporary file path";
CloseHandle(handle_);
handle_ = INVALID_HANDLE_VALUE;
return false;
}
path_.resize(utf8_len - 1);
WideCharToMultiByte(
CP_UTF8, 0, file, -1, path_.data(), utf8_len, nullptr, nullptr);
return true;
}
void AddonImage::AfterOpen(bool opened) {
consumed_ = true;
if (handle_ == INVALID_HANDLE_VALUE) return;
if (!opened) {
CloseHandle(handle_); // delete-on-close removes the file
handle_ = INVALID_HANDLE_VALUE;
return;
}
Mutex::ScopedLock lock(g_retained_addon_handles_mutex);
if (g_retained_addon_handles == nullptr) {
g_retained_addon_handles = new std::vector<HANDLE>();
}
g_retained_addon_handles->push_back(handle_);
handle_ = INVALID_HANDLE_VALUE;
}
AddonImage::~AddonImage() {
// Materialized but Open() was never reached (e.g. an exception in between):
// closing the delete-on-close handle removes the file.
if (!consumed_ && handle_ != INVALID_HANDLE_VALUE) CloseHandle(handle_);
}
#else // !_WIN32
bool AddonImage::Materialize(const char* data, size_t len) {
#if defined(__linux__)
// Prefer an anonymous in-memory fd, loaded through /proc/self/fd: nothing
// reaches the filesystem, so there is no temp file to secure, unlink, or
// leak, and no noexec-mount problem. Request an executable memfd (MFD_EXEC,
// Linux 6.3+); retry without it on older kernels that reject the flag, then
// fall back to a temp file if memfd is unavailable entirely.
#ifndef MFD_CLOEXEC
#define MFD_CLOEXEC 0x0001U
#endif
#ifndef MFD_EXEC
#define MFD_EXEC 0x0010U
#endif
fd_ = NodeMemfdCreate("node-addon", MFD_CLOEXEC | MFD_EXEC);
if (fd_ == -1 && errno == EINVAL) {
fd_ = NodeMemfdCreate("node-addon", MFD_CLOEXEC);
}
if (fd_ != -1) {
if (!WriteAllToFd(fd_, data, len)) {
errmsg_ = "could not write the native addon to an in-memory file";
close(fd_);
fd_ = -1;
return false;
}
path_ = "/proc/self/fd/" + std::to_string(fd_);
return true;
}
#endif // __linux__
return MaterializeTempFile(data, len);
}
// static
std::string AddonImage::TempDir() {
const char* env_tmp = getenv("TMPDIR");
std::string tmpdir =
(env_tmp != nullptr && *env_tmp != '\0') ? env_tmp : "/tmp";
if (tmpdir.back() != '/') tmpdir.push_back('/');
return tmpdir;
}
bool AddonImage::MaterializeTempFile(const char* data, size_t len) {
// A private 0700 directory (mkdtemp) so no other user can pre-create the path
// as a symlink or swap the file between the write and the load; O_EXCL and
// O_NOFOLLOW harden the create against a race inside it.
std::string tmpl = TempDir() + "node-addon-XXXXXX";
std::vector<char> buf(tmpl.begin(), tmpl.end());
buf.push_back('\0');
if (mkdtemp(buf.data()) == nullptr) {
errmsg_ = "could not create a temporary directory for the native addon";
return false;
}
temp_dir_ = buf.data();
std::string file = temp_dir_ + "/addon.node";
fd_ = open(
file.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
if (fd_ == -1) {
errmsg_ = "could not create a temporary file for the native addon";
rmdir(temp_dir_.c_str());
temp_dir_.clear();
return false;
}
if (!WriteAllToFd(fd_, data, len)) {
errmsg_ = "could not write the native addon to a temporary file";
close(fd_);
fd_ = -1;
unlink(file.c_str());
rmdir(temp_dir_.c_str());
temp_dir_.clear();
return false;
}
// dlopen() reopens by path; the post-open mapping keeps the inode alive.
close(fd_);
fd_ = -1;
path_ = file;
return true;
}
void AddonImage::AfterOpen(bool opened) {
consumed_ = true;
// The right cleanup is the same whether or not the load worked.
(void)opened;
// memfd: the load's mapping (or nothing, on failure) owns it from here.
if (fd_ != -1) {
close(fd_);
fd_ = -1;
return;
}
if (!temp_dir_.empty()) {
// On success the mapping keeps the inode alive, so unlinking now leaves no
// on-disk trace; on failure this just cleans up.
unlink(path_.c_str());
rmdir(temp_dir_.c_str());
temp_dir_.clear();
}
}
AddonImage::~AddonImage() {
if (consumed_) return;
if (fd_ != -1) close(fd_);
if (!temp_dir_.empty()) {
unlink(path_.c_str());
rmdir(temp_dir_.c_str());
}
}
#endif // _WIN32
} // namespace
// Shared by process.dlopen() and the internal dlopenBinary(). `allow_binary`
// says whether args[3] may carry the addon's bytes; it is false for
// process.dlopen(), whose signature stays (module, filename[, flags]).
//
// FIXME(bnoordhuis) Not multi-context ready. TBD how to resolve the conflict
// when two contexts try to load the same shared object. Maybe have a shadow
// cache that's a plain C list or hash table that's shared across contexts?
static void DLOpenImpl(const FunctionCallbackInfo<Value>& args,
bool allow_binary) {
Environment* env = Environment::GetCurrent(args);
if (env->no_native_addons()) {
return THROW_ERR_DLOPEN_DISABLED(
env, "Cannot load native addon because loading addons is disabled.");
}
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kAddon, "");
auto context = env->context();
CHECK_NULL(thread_local_modpending);
if (args.Length() < 2) {
return THROW_ERR_MISSING_ARGS(
env, "process.dlopen needs at least 2 arguments");
}
int32_t flags = DLib::kDefaultFlags;
// On the internal path an undefined flags argument keeps the default, so a
// caller can pass the bytes without choosing flags. process.dlopen() keeps
// its original behaviour of rejecting any non-integer that is present.
if (args.Length() > 2 && !(allow_binary && args[2]->IsUndefined()) &&
!args[2]->Int32Value(context).To(&flags)) {
return THROW_ERR_INVALID_ARG_TYPE(env, "flag argument must be an integer.");
}
Local<Object> module;
Local<Object> exports;
Local<Value> exports_v;
if (!args[0]->ToObject(context).ToLocal(&module) ||
!module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
!exports_v->ToObject(context).ToLocal(&exports)) {
return; // Exception pending.
}
node::Utf8Value filename(env->isolate(), args[1]); // The addon's path,
// used for diagnostics.
// On the internal path args[3] carries the addon's bytes, for an addon that
// lives somewhere dlopen() cannot open by path (e.g. a virtual file system).
// Materialize them into a private, self-cleaning image and load that, while
// still reporting `filename` in any error.
AddonImage image;
const char* load_path = *filename;
if (allow_binary && args.Length() > 3 && !args[3]->IsUndefined()) {
if (!args[3]->IsArrayBufferView()) {
return THROW_ERR_INVALID_ARG_TYPE(
env, "binary must be a Buffer, TypedArray, or DataView");
}
// Loading from bytes materializes them into an image in the temporary
// directory, so this needs write access there on top of the addon
// permission checked above. The check does not depend on whether the image
// actually reaches the file system on this platform (Linux uses an
// anonymous memfd): what a program must be granted should not vary by
// platform.
THROW_IF_INSUFFICIENT_PERMISSIONS(
env,
permission::PermissionScope::kFileSystemWrite,
AddonImage::TempDir());
ArrayBufferViewContents<char> binary(args[3]);
if (!image.Materialize(binary.data(), binary.length())) {
return THROW_ERR_DLOPEN_FAILED(
env, "%s: %s", image.errmsg().c_str(), *filename);
}
load_path = image.path().c_str();
}
env->TryLoadAddon(load_path, flags, [&](DLib* dlib) {
static Mutex dlib_load_mutex;
Mutex::ScopedLock lock(dlib_load_mutex);
const bool is_opened = dlib->Open();
image.AfterOpen(is_opened);
// Objects containing v14 or later modules will have registered themselves
// on the pending list. Activate all of them now. At present, only one
// module per object is supported.
node_module* mp = thread_local_modpending;
thread_local_modpending = nullptr;
if (!is_opened) {
std::string errmsg = dlib->errmsg_.c_str();
dlib->Close();
#ifdef _WIN32
// Windows needs to add the filename into the error message
errmsg += filename.ToStringView();
#endif // _WIN32
THROW_ERR_DLOPEN_FAILED(env, "%s", errmsg);
return false;
}
if (mp != nullptr) {
if (mp->nm_context_register_func == nullptr) {
if (env->force_context_aware()) {
dlib->Close();
THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
return false;
}
}
mp->nm_dso_handle = dlib->handle_;
dlib->SaveInGlobalHandleMap(mp);
} else {
if (auto callback = GetInitializerCallback(dlib)) {
callback(exports, module, context);
return true;
} else if (auto napi_callback = GetNapiInitializerCallback(dlib)) {
int32_t module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION;
if (auto get_version = GetNapiAddonGetApiVersionCallback(dlib)) {
module_api_version = get_version();
}
napi_module_register_by_symbol(
exports, module, context, napi_callback, module_api_version);
return true;
} else {
mp = dlib->GetSavedModuleFromGlobalHandleMap();
if (mp == nullptr || mp->nm_context_register_func == nullptr) {
dlib->Close();
THROW_ERR_DLOPEN_FAILED(
env, "Module did not self-register: '%s'.", filename);
return false;
}
}
}
// -1 is used for Node-API modules
if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) {
// Even if the module did self-register, it may have done so with the
// wrong version. We must only give up after having checked to see if it
// has an appropriate initializer callback.
if (auto callback = GetInitializerCallback(dlib)) {
callback(exports, module, context);
return true;
}
const int actual_nm_version = mp->nm_version;
// NOTE: `mp` is allocated inside of the shared library's memory, calling
// `dlclose` will deallocate it
dlib->Close();
THROW_ERR_DLOPEN_FAILED(
env,
"The module '%s'"
"\nwas compiled against a different Node.js version using"
"\nNODE_MODULE_VERSION %d. This version of Node.js requires"
"\nNODE_MODULE_VERSION %d. Please try re-compiling or "
"re-installing\nthe module (for instance, using `npm rebuild` "
"or `npm install`).",
*filename,
actual_nm_version,
NODE_MODULE_VERSION);
return false;
}
CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0);
// Do not keep the lock while running userland addon loading code.
Mutex::ScopedUnlock unlock(lock);
if (mp->nm_context_register_func != nullptr) {
mp->nm_context_register_func(exports, module, context, mp->nm_priv);
} else if (mp->nm_register_func != nullptr) {
mp->nm_register_func(exports, module, mp->nm_priv);
} else {
dlib->Close();
THROW_ERR_DLOPEN_FAILED(env, "Module has no declared entry point.");
return false;
}
return true;
});
// Tell coverity that 'handle' should not be freed when we return.
// coverity[leaked_storage]
}
// process.dlopen(module, filename[, flags]). Used to load 'module.node'
// dynamically shared objects.
void DLOpen(const FunctionCallbackInfo<Value>& args) {
DLOpenImpl(args, false);
}
// Internal only, reached through the process_methods binding rather than the
// process object: dlopenBinary(module, filename, flags, binary) loads an addon
// from bytes already in memory. Used for addons served by a virtual file
// system, which the dynamic loader cannot open by path.
void DLOpenBinary(const FunctionCallbackInfo<Value>& args) {
DLOpenImpl(args, true);
}
inline struct node_module* FindModule(struct node_module* list,
const char* name,
int flag) {
struct node_module* mp;
for (mp = list; mp != nullptr; mp = mp->nm_link) {
if (strcmp(mp->nm_modname, name) == 0) break;
}
CHECK(mp == nullptr || (mp->nm_flags & flag) != 0);
return mp;
}
void CreateInternalBindingTemplates(IsolateData* isolate_data) {
#define V(modname) \
do { \
Local<ObjectTemplate> templ = \
ObjectTemplate::New(isolate_data->isolate()); \
templ->SetInternalFieldCount(BaseObject::kInternalFieldCount); \
_register_isolate_##modname(isolate_data, templ); \
isolate_data->set_##modname##_binding_template(templ); \
} while (0);
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V
}
static Local<Object> GetInternalBindingExportObject(IsolateData* isolate_data,
const char* mod_name,
Local<Context> context) {
Local<ObjectTemplate> templ;
#define V(name) \
if (strcmp(mod_name, #name) == 0) { \
templ = isolate_data->name##_binding_template(); \
} else // NOLINT(readability/braces)
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V
{
// Default template.
templ = isolate_data->binding_data_default_template();
}
Local<Object> obj = templ->NewInstance(context).ToLocalChecked();
return obj;
}
static Local<Object> InitInternalBinding(Realm* realm, node_module* mod) {
EscapableHandleScope scope(realm->isolate());
Local<Context> context = realm->context();
Local<Object> exports = GetInternalBindingExportObject(
realm->isolate_data(), mod->nm_modname, context);
CHECK_NULL(mod->nm_register_func);
CHECK_NOT_NULL(mod->nm_context_register_func);
Local<Value> unused = Undefined(realm->isolate());
// Internal bindings don't have a "module" object, only exports.
mod->nm_context_register_func(exports, unused, context, mod->nm_priv);
return scope.Escape(exports);
}
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
Isolate* isolate = realm->isolate();
HandleScope scope(isolate);
CHECK(args[0]->IsString());
Local<String> module = args[0].As<String>();
node::Utf8Value module_v(isolate, module);
Local<Object> exports;