Repository navigation
resolver: close the store_fd directory handle when readdir fails #36878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4b86377
resolver: close the store_fd directory handle when readdir fails
robobun bcb1e1d
drop redundant had_handle factor; have the test prove the shim fired
robobun e1db020
trim guard comments
robobun 924be77
drop guard comment; variable names carry it
robobun af70e3a
ci: retrigger
robobun e3694ab
close caller-supplied handle on the not-yet-published branch too
robobun 688eb73
one-line doc for maybe_handle ownership
robobun faddcaa
[autofix.ci] apply automated fixes
autofix-ci[bot] df2f6a6
scanner test: prove the shim fired via unreachable sentinel test files
robobun e7c58f8
[autofix.ci] apply automated fixes
autofix-ci[bot] 3889f07
arm the close guard before the cache-hit / ? returns
robobun c67bf0f
set handle_published only when readdir actually stored the handle
robobun 1bc11bd
do not close on readdir Err when a non-void iterator may have dispatc…
robobun 3cb7ef2
one-line comment for the non-void iterator Err arm
robobun 4502d27
keep handle_published consistent with the defensive entries.fd fallback
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
test/js/bun/resolve/resolver-readdir-error-fd-leak.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // The resolver opens a directory and then iterates it via getdents64. When the | ||
| // open succeeds but iteration fails, and the resolver was asked to cache the | ||
| // fd (store_fd, set by bun test / --hot / --watch / install), the fd must be | ||
| // closed on that error path. The guard that closes it used to be gated on the | ||
| // "don't store" condition, so a store_fd run leaked one directory fd per | ||
| // failed read. | ||
| // | ||
| // getdents64 is reached through libc's variadic syscall(), so an LD_PRELOAD | ||
| // shim can make it fail with EIO for fds whose /proc/self/fd target contains a | ||
| // marker string. The inner process runs under --hot (store_fd=true), resolves | ||
| // one path per marker directory, then counts open fds that still point at a | ||
| // marker directory. | ||
| import { afterAll, beforeAll, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isLinux, isMusl, tempDir } from "harness"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const cc = Bun.which("cc") || Bun.which("gcc") || Bun.which("clang"); | ||
| const DIR_COUNT = 16; | ||
| const MARKER = "__FAIL_GETDENTS__"; | ||
|
|
||
| const SHIM_C = /* c */ ` | ||
| #define _GNU_SOURCE | ||
| #include <dlfcn.h> | ||
| #include <errno.h> | ||
| #include <stdarg.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <unistd.h> | ||
| #include <sys/syscall.h> | ||
|
|
||
| static long (*real_syscall)(long, long, long, long, long, long, long); | ||
|
|
||
| long syscall(long number, ...) { | ||
| va_list ap; | ||
| long a, b, c, d, e, f; | ||
| va_start(ap, number); | ||
| a = va_arg(ap, long); b = va_arg(ap, long); c = va_arg(ap, long); | ||
| d = va_arg(ap, long); e = va_arg(ap, long); f = va_arg(ap, long); | ||
| va_end(ap); | ||
| if (!real_syscall) | ||
| real_syscall = (long (*)(long, long, long, long, long, long, long))dlsym(RTLD_NEXT, "syscall"); | ||
| if (number == SYS_getdents64) { | ||
| char link[64], target[4096]; | ||
| snprintf(link, sizeof link, "/proc/self/fd/%ld", a); | ||
| ssize_t n = readlink(link, target, sizeof(target) - 1); | ||
| if (n > 0) { | ||
| target[n] = 0; | ||
| if (strstr(target, "${MARKER}")) { | ||
| errno = EIO; | ||
| return -1; | ||
| } | ||
| } | ||
| } | ||
| return real_syscall(number, a, b, c, d, e, f); | ||
| } | ||
| `; | ||
|
|
||
| const INNER = /* ts */ ` | ||
| import { readdirSync, readlinkSync } from "node:fs"; | ||
| const N = ${DIR_COUNT}; | ||
| let failed = 0; | ||
| for (let i = 0; i < N; i++) { | ||
| try { | ||
| require.resolve("./d" + i + "${MARKER}/m.ts"); | ||
| } catch { | ||
| failed++; | ||
| } | ||
| } | ||
| let leaked = 0; | ||
| for (const name of readdirSync("/proc/self/fd")) { | ||
| try { | ||
| const target = readlinkSync("/proc/self/fd/" + name); | ||
| if (target.includes("${MARKER}")) leaked++; | ||
| } catch {} | ||
| } | ||
| console.log("FAILED=" + failed + " LEAKED=" + leaked); | ||
| process.exit(0); | ||
| `; | ||
|
|
||
| let dir: ReturnType<typeof tempDir>; | ||
| let shimPath: string; | ||
|
|
||
| beforeAll(async () => { | ||
| if (!isLinux || isMusl || !cc) return; | ||
| const files: Record<string, string> = { | ||
| "shim.c": SHIM_C, | ||
| "inner.ts": INNER, | ||
| "package.json": "{}", | ||
| }; | ||
| for (let i = 0; i < DIR_COUNT; i++) files[`d${i}${MARKER}/m.ts`] = `export {};\n`; | ||
| dir = tempDir("resolver-readdir-fail-leak", files); | ||
| shimPath = join(String(dir), "shim.so"); | ||
| await using ccProc = Bun.spawn({ | ||
| cmd: [cc, "-shared", "-fPIC", "-o", shimPath, join(String(dir), "shim.c"), "-ldl"], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [ccOut, ccErr, ccExit] = await Promise.all([ccProc.stdout.text(), ccProc.stderr.text(), ccProc.exited]); | ||
| if (ccExit !== 0) throw new Error(`shim compile failed: ${ccErr || ccOut}`); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| dir?.[Symbol.dispose](); | ||
| }); | ||
|
|
||
| // LD_PRELOAD interposition requires a dynamic libc; the musl build links libc | ||
| // statically, so the shim cannot intercept syscall() there. | ||
| const skip = !isLinux || isMusl || !cc; | ||
|
|
||
| function shimEnv() { | ||
| const existing = bunEnv.LD_PRELOAD; | ||
| return { ...bunEnv, LD_PRELOAD: existing ? `${shimPath}:${existing}` : shimPath }; | ||
| } | ||
|
|
||
| test.skipIf(skip)( | ||
| "resolver closes the directory fd when readdir fails after a successful open (store_fd, fresh handle)", | ||
| async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "--hot", "inner.ts"], | ||
| cwd: String(dir), | ||
| env: shimEnv(), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| // FAILED=DIR_COUNT proves the shim actually fired; if bun stops routing | ||
| // getdents64 through libc's syscall() the resolves succeed and this trips. | ||
| expect({ stdout: stdout.trim(), stderr, exitCode }).toEqual({ | ||
| stdout: `FAILED=${DIR_COUNT} LEAKED=0`, | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| // The `bun test` scanner opens each subdirectory itself and hands the fd to | ||
| // read_directory_with_iterator(Some(fd)), which is the had_handle=true arm of | ||
| // the same guard. The scanner's Dir guard is disarmed via into_raw(), so the | ||
| // function owns the fd on that path too. | ||
| test.skipIf(skip)( | ||
| "resolver closes a caller-supplied directory fd when readdir fails (bun test scanner)", | ||
| async () => { | ||
| const files: Record<string, string> = { | ||
| "package.json": "{}", | ||
| "count.test.ts": /* ts */ ` | ||
| import { test } from "bun:test"; | ||
| import { readdirSync, readlinkSync } from "node:fs"; | ||
| test("count", () => { | ||
| let leaked = 0; | ||
| for (const name of readdirSync("/proc/self/fd")) { | ||
| try { | ||
| if (readlinkSync("/proc/self/fd/" + name).includes("${MARKER}")) leaked++; | ||
| } catch {} | ||
| } | ||
| console.log("LEAKED=" + leaked); | ||
| }); | ||
| `, | ||
| }; | ||
| for (let i = 0; i < DIR_COUNT; i++) files[`d${i}${MARKER}/noop.txt`] = ""; | ||
| using scanDir = tempDir("resolver-readdir-scanner-leak", files); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "test", "."], | ||
| cwd: String(scanDir), | ||
| env: shimEnv(), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| const m = stdout.match(/LEAKED=(\d+)/); | ||
| expect({ leaked: m?.[0], stderr: stderr.includes("1 pass"), exitCode }).toEqual({ | ||
| leaked: "LEAKED=0", | ||
| stderr: true, | ||
| exitCode: 0, | ||
| }); | ||
|
Check warning on line 175 in test/js/bun/resolve/resolver-readdir-error-fd-leak.test.ts
|
||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.