Repository navigation
vfs: load each memfd image under a path of its own - #67068
pipobscure wants to merge 2 commits into
Conversation
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd was closed right after a successful load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one. Keep a memfd open for the life of the process once it has been loaded, so no two images ever share a path; MFD_CLOEXEC keeps it out of child processes. A failed load still closes its memfd. The temp-file fallback is unaffected: each image there gets a fresh mkdtemp() directory, and Windows already keeps its images until exit. Signed-off-by: Philipp Dunkel <pip@pipobscure.com>
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #67068 +/- ##
==========================================
+ Coverage 90.65% 90.68% +0.03%
==========================================
Files 797 797
Lines 278268 278786 +518
Branches 53517 53626 +109
==========================================
+ Hits 252261 252816 +555
+ Misses 16386 16328 -58
- Partials 9621 9642 +21
🚀 New features to boost your workflow:
|
addaleax
left a comment
There was a problem hiding this comment.
Do we have to leak here? Can we just close after uv_dlclose()?
There is an expectation that Node.js instances clean up file descriptors opened by them unless explicitly told otherwise.
You might have a point. I'm reworking it with a slightly better idea that makes sure we don't leak. uv_dlclode() doesn't solve the problem, since that would just lead to reused Hold for changes comint |
Keeping a loaded memfd open until exit gave each image a path of its own, but held one file descriptor per load for the life of the process: ffi's close() did not release it, nor did a worker's exit, nor a load that failed after dlopen(), so a process opening libraries in a loop would grow without bound. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on. What has to last is the path, and that depends only on the number. So move each image's fd to a number no earlier image has had, using F_DUPFD_CLOEXEC with a process-wide counter that only goes up, and close it right after the load again. Unlike closing it after dlclose(), this also holds for an object that dlclose() leaves loaded, and for addons on the main thread, which are never unloaded. Should the numbers run out, the image goes to a temp file. Signed-off-by: Philipp Dunkel <pip@pipobscure.com>
|
You're right, we didn't have to leak, and I've pushed a fix. The memfd now closes right after dlopen(), so no fd outlives a load, whether it's ffi.close(), worker exit or a failed load. |
addaleax
left a comment
There was a problem hiding this comment.
I don't think this is a good permanent solution tbh, but I don't think it hurts either right now
| static Mutex mutex; | ||
| static int next_number = 0; | ||
| Mutex::ScopedLock lock(mutex); | ||
| const int moved = fcntl(fd, F_DUPFD_CLOEXEC, next_number); |
There was a problem hiding this comment.
This will reliably start to break once the fd number reaches the EMFILE limit (ulimit -n) – it doesn't matter how many file descriptors are actually open, the resource limit limits the maximum fd value.
There was a problem hiding this comment.
True. Do you have any idea how to solve that though? I'd actually like to solve this in a solid permanent way, so if you have any ideas I'd very much appreciate it!
|
Given the valid criticisms that @addaleax raised, and the fact that she has more than a point I'd like to solve this in a cleaner way. If anyone has any ideas I'd appreciate hearing them.
Thoughts very much appreciated! |
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd is closed right after the load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one.
Move each image's fd to a number no earlier image has had, using F_DUPFD_CLOEXEC with a process-wide counter that only goes up, and still close it right after the load. The fd is only needed until dlopen() returns, because the load's mapping keeps the memory alive from then on, so no file descriptor outlives a load. That covers ffi's close(), worker exit, and loads that fail, whether in dlopen() or at registration.
The first commit kept each loaded memfd open until exit instead, which leaked one fd per load. The second commit replaces that. Closing after dlclose() was not enough, because dlclose() can leave an object loaded (for example one with an STB_GNU_UNIQUE symbol or a thread_local destructor), and addons on the main thread are never unloaded.
Each load uses up an fd number for the life of the process. If the numbers run out, the image goes through the existing temp-file fallback. Windows is unchanged: it keeps its images until exit.