vfs: add materializeSync() for FFI consumers - #65909
Conversation
The dlopen hook installed while a VFS is mounted always forwarded its flags parameter, so a two-argument process.dlopen() call for a real file system path reached the original implementation with `undefined` as the flags. That coerces to 0, which is not a valid dlopen(2) mode, instead of applying the default flags, and loading any addon from the real file system failed with EINVAL while a VFS was mounted. Signed-off-by: Matteo Collina <hello@matteocollina.com>
Native addons inside a mounted VFS already load through require(): the loader hands their bytes to an internal helper that writes them to a private image the dynamic loader can open. Libraries that open a binary by path themselves - FFI bindings that dlopen() a shared library, for example - never reach the module loader, so that route cannot help them. Expose the same mechanism as vfs.materializeSync(path): a path outside any VFS is returned unchanged, while a VFS path is read and its bytes are written to a private image - an anonymous in-memory memfd on Linux, a temporary file elsewhere - that stays loadable for the rest of the process lifetime. The returned real path can be handed to any consumer that opens it by path. Repeated calls with the same path return the same image, and unmounting drops the path from the cache while leaving already materialized images alive. Signed-off-by: Matteo Collina <hello@matteocollina.com>
|
LETM (Looks Excellent To Me 😃 ) |
|
The only question I have is whether we want to hide the detail that the path may need materializing inside And since |
Native addons inside a mounted VFS already load through
require(): the loader hands their bytes to an internal helper that writes them to a private image the dynamic loader can open. Libraries that open a binary by path themselves — FFI bindings thatdlopen()a shared library, for example — never reach the module loader, so that route cannot help them.This adds
vfs.materializeSync(path), exposing the same mechanism as a public API:The C++ side reuses
AddonImage(the materializer behinddlopenBinary) with a newRetain()consumption mode, exposed asmaterializeBinary()on theprocess_methodsbinding, with the same temp-dir write-permission check and a newERR_VFS_MATERIALIZE_FAILEDerror code.The new test simulates an FFI consumer by calling
process.dlopen()on the materialized path directly — the path lies outside the VFS, so the call takes the unhooked route straight to the OS loader, exactly like an FFI library would.Bug fix included
Writing that test exposed a pre-existing bug, fixed in the first commit: the dlopen hook installed while a VFS is mounted always forwarded its
flagsparameter, so a two-argumentprocess.dlopen()call for a real file-system path reached the original implementation withundefinedas the flags. That coerces to0, which is not a validdlopen(2)mode, and loading any addon from the real file system failed with EINVAL while a VFS was mounted.