|
9 | 9 | #include "util.h" |
10 | 10 |
|
11 | 11 | #include <string> |
| 12 | +#include <utility> |
12 | 13 | #include <vector> |
13 | 14 |
|
14 | 15 | #ifdef _WIN32 |
@@ -474,64 +475,29 @@ int NodeMemfdCreate(const char* name, unsigned int flags) { |
474 | 475 | return static_cast<int>(syscall(SYS_memfd_create, name, flags)); |
475 | 476 | } |
476 | 477 | #endif // __linux__ |
| 478 | +#else // _WIN32 |
| 479 | + |
| 480 | +// Delete-on-close handles kept alive until process exit so their temp files |
| 481 | +// outlive the loaded DLLs and are removed once the process ends. |
| 482 | +Mutex g_retained_addon_handles_mutex; |
| 483 | +std::vector<HANDLE>* g_retained_addon_handles = nullptr; |
| 484 | + |
477 | 485 | #endif // !_WIN32 |
478 | 486 |
|
479 | | -// Materializes native-addon bytes into a form dlopen()/LoadLibrary() can load, |
480 | | -// with the smallest, most private on-disk footprint each platform allows: |
481 | | -// Linux: an anonymous in-memory memfd, loaded via /proc/self/fd/N - |
482 | | -// the bytes never touch the filesystem. |
483 | | -// other POSIX: a 0700 mkdtemp() directory plus an O_EXCL|O_NOFOLLOW file, |
484 | | -// unlink()ed right after the load (the mapping keeps it alive). |
485 | | -// Windows: a temp file opened FILE_FLAG_DELETE_ON_CLOSE; its handle is |
486 | | -// retained for the process lifetime so the file is removed |
487 | | -// automatically once the process (and the loaded DLL) exit. |
488 | | -// Used for an addon that lives somewhere dlopen() cannot open by path, such as |
489 | | -// a virtual file system. |
490 | | -class AddonImage { |
491 | | - public: |
492 | | - AddonImage() = default; |
493 | | - ~AddonImage(); |
494 | | - AddonImage(const AddonImage&) = delete; |
495 | | - AddonImage& operator=(const AddonImage&) = delete; |
496 | | - |
497 | | - // The directory a temporary image would be written to, with a trailing |
498 | | - // separator; empty when it cannot be determined. Names the resource for the |
499 | | - // file-system permission check. |
500 | | - static std::string TempDir(); |
501 | | - |
502 | | - // On success sets path() to a real, loadable path for `data`. |
503 | | - bool Materialize(const char* data, size_t len); |
504 | | - const std::string& path() const { return path_; } |
505 | | - const std::string& errmsg() const { return errmsg_; } |
506 | | - |
507 | | - // Call exactly once, right after DLib::Open(); `opened` says whether the load |
508 | | - // succeeded. Releases the transient resources that are no longer needed (a |
509 | | - // successful load holds its own mapping): on POSIX closes the memfd or |
510 | | - // unlinks the temp file; on Windows retains the delete-on-close handle for |
511 | | - // the process lifetime when opened, or closes it (deleting the file) on |
512 | | - // failure. |
513 | | - void AfterOpen(bool opened); |
| 487 | +} // namespace |
514 | 488 |
|
515 | | - private: |
516 | | - std::string path_; |
517 | | - std::string errmsg_; |
518 | | - bool consumed_ = false; |
| 489 | +// AddonImage is declared in node_binding.h so that the other loader of |
| 490 | +// dynamically shared objects, node_ffi.cc, can reuse it; see the header for |
| 491 | +// the platform-by-platform description. |
| 492 | + |
| 493 | +AddonImage::AddonImage() { |
519 | 494 | #ifdef _WIN32 |
520 | | - HANDLE handle_ = INVALID_HANDLE_VALUE; |
521 | | -#else |
522 | | - bool MaterializeTempFile(const char* data, size_t len); |
523 | | - int fd_ = -1; |
524 | | - std::string temp_dir_; // non-empty only for the temp-file (non-memfd) path |
| 495 | + handle_ = INVALID_HANDLE_VALUE; |
525 | 496 | #endif |
526 | | -}; |
| 497 | +} |
527 | 498 |
|
528 | 499 | #ifdef _WIN32 |
529 | 500 |
|
530 | | -// Delete-on-close handles kept alive until process exit so their temp files |
531 | | -// outlive the loaded DLLs and are removed once the process ends. |
532 | | -Mutex g_retained_addon_handles_mutex; |
533 | | -std::vector<HANDLE>* g_retained_addon_handles = nullptr; |
534 | | - |
535 | 501 | // static |
536 | 502 | std::string AddonImage::TempDir() { |
537 | 503 | wchar_t dir[MAX_PATH + 1]; |
@@ -730,8 +696,6 @@ AddonImage::~AddonImage() { |
730 | 696 |
|
731 | 697 | #endif // _WIN32 |
732 | 698 |
|
733 | | -} // namespace |
734 | | - |
735 | 699 | // Shared by process.dlopen() and the internal dlopenBinary(). `allow_binary` |
736 | 700 | // says whether args[3] may carry the addon's bytes; it is false for |
737 | 701 | // process.dlopen(), whose signature stays (module, filename[, flags]). |
@@ -928,6 +892,7 @@ void DLOpenBinary(const FunctionCallbackInfo<Value>& args) { |
928 | 892 | DLOpenImpl(args, true); |
929 | 893 | } |
930 | 894 |
|
| 895 | + |
931 | 896 | inline struct node_module* FindModule(struct node_module* list, |
932 | 897 | const char* name, |
933 | 898 | int flag) { |
|
0 commit comments