mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on; what must not be shared is the path. Before the load, ask the loader with RTLD_NOLOAD whether the path is already taken, and if so move the fd to a higher number with F_DUPFD_CLOEXEC until it is free. Holding the fd from the check through the load keeps anyone else from taking the path in between, so this needs neither a counter nor a lock, and a library that has really been unloaded gives its number back. The fd is closed right after the load, whether or not it worked. Should no free number be left, the image goes to a temp file. The leak checks of test-vfs-addon and test-ffi-vfs compared the shared temporary directory before and after the load, so off Linux any other test loading from bytes at the same time could pass for a leak. Point TMPDIR at a directory each test owns instead. Signed-off-by: Philipp Dunkel <pip@pipobscure.com> PR-URL: https://github.com/nodejs/node/pull/67068 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
61 lines
2.3 KiB
JavaScript
61 lines
2.3 KiB
JavaScript
// Flags: --disable-warning=ExperimentalWarning
|
|
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('node:assert');
|
|
const { spawnSync } = require('node:child_process');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vfs = require('node:vfs');
|
|
|
|
const addonPath = (name) => path.join(
|
|
__dirname, '..', 'addons', name, 'build', common.buildType, 'binding.node');
|
|
const helloPath = addonPath('hello-world');
|
|
const functionPath = addonPath('hello-world-function-export');
|
|
if (!fs.existsSync(helloPath) || !fs.existsSync(functionPath)) {
|
|
common.skip('the hello-world addons are not built');
|
|
}
|
|
|
|
const myVfs = vfs.create();
|
|
myVfs.writeFileSync('/hello.node', fs.readFileSync(helloPath));
|
|
myVfs.writeFileSync('/function.node', fs.readFileSync(functionPath));
|
|
const mountPoint = myVfs.mount();
|
|
|
|
const hello = require(path.join(mountPoint, 'hello.node'));
|
|
assert.strictEqual(typeof hello, 'object');
|
|
assert.strictEqual(hello.hello(), 'world');
|
|
|
|
// hello-world-function-export replaces its exports with a function. Resolved
|
|
// to the first image, this would be another { hello } object instead.
|
|
const fn = require(path.join(mountPoint, 'function.node'));
|
|
assert.strictEqual(typeof fn, 'function');
|
|
assert.strictEqual(fn(), 'world');
|
|
assert.notStrictEqual(fn, hello);
|
|
|
|
myVfs.unmount();
|
|
|
|
// An addon on the main thread stays loaded and keeps the path it was loaded
|
|
// through, so enough of them leave no fd number to load another through. Loading
|
|
// has to carry on regardless, through the temp-file fallback.
|
|
if (common.isLinux) {
|
|
const limit = 128;
|
|
const child = `
|
|
const assert = require('node:assert');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vfs = require('node:vfs');
|
|
const myVfs = vfs.create();
|
|
myVfs.writeFileSync('/function.node', fs.readFileSync(${JSON.stringify(functionPath)}));
|
|
const mountPoint = myVfs.mount();
|
|
for (let i = 0; i < ${limit * 2}; i++) {
|
|
const addonPath = path.join(mountPoint, 'function.node');
|
|
assert.strictEqual(require(addonPath)(), 'world');
|
|
delete require.cache[addonPath];
|
|
}
|
|
`;
|
|
const { status, stderr } = spawnSync('/bin/sh', [
|
|
'-c', `ulimit -n ${limit} && exec "$0" "$@"`,
|
|
process.execPath, '--disable-warning=ExperimentalWarning', '-e', child,
|
|
], { encoding: 'utf8' });
|
|
assert.strictEqual(status, 0, stderr);
|
|
}
|