diff --git a/.claude/docs/landing-prs.md b/.claude/docs/landing-prs.md index d9643b5f42d4..a6c9cfb38db3 100644 --- a/.claude/docs/landing-prs.md +++ b/.claude/docs/landing-prs.md @@ -37,7 +37,7 @@ Companion to the "Landing PRs: What Bun Reviewers Catch" section in CLAUDE.md. T ## Cross-platform - **Never assume OS/ABI facts are portable.** errno meanings differ (EPERM is a sharing violation on Windows), event flags differ, Windows env vars are case-insensitive, Windows has no POSIX signals, blocking syscalls retry on EINTR. FFI/ABI: explicit calling conventions on BOTH sides; fixed-width or C-ABI types, never bare int read from c_ulong (LLP64 garbage on Windows); extern declarations diff'd parameter-by-parameter against definitions — they compile cleanly per side and crash only on the platform you didn't build; complete Windows error-translation tables with fallbacks instead of force-unwraps. -- **Platform parity is part of every fix.** When you fix one platform backend (POSIX vs kqueue vs epoll vs libuv), audit every sibling backend for the same defect and apply symmetrically — or state why a backend is unaffected ("kqueue register path is unhandled. You only patched unregister."). A POSIX-only API addition ships its Windows equivalent in the same PR. Enabling a feature on a new platform means grepping every gate, dispatch chain, parallel platform script, test skip, and allowlist. Platform-specific CI failures in files you touched are real merge-blocking bugs, never flakes. Comment WHY on every new platform exclusion. +- **Platform parity is part of every fix.** When you fix one platform backend (POSIX vs kqueue vs epoll vs IOCP), audit every sibling backend for the same defect and apply symmetrically — or state why a backend is unaffected ("kqueue register path is unhandled. You only patched unregister."). A POSIX-only API addition ships its Windows equivalent in the same PR. Enabling a feature on a new platform means grepping every gate, dispatch chain, parallel platform script, test skip, and allowlist. Platform-specific CI failures in files you touched are real merge-blocking bugs, never flakes. Comment WHY on every new platform exclusion. - **Write tests to pass on every CI platform** (Windows, macOS x64+arm64, Linux glibc and musl). Split on `/\r?\n/` (Windows CRLF); normalize separators in path assertions; never spawn shell builtins (echo, sleep are not programs on Windows — use bunExe() -e); no hardcoded /tmp or /bin; incidental servers bind 127.0.0.1, never ::1 (CI Linux may lack IPv6 — IPv6-specific tests gate on the harness IPv6 helper); exit codes not signal names for "did not crash" (Windows has no signals); when probing a limit, exceed the LARGEST platform limit to trip the guard and stay under the SMALLEST when constructing inputs (macOS PATH_MAX is 1024). Before skipping a platform, verify it genuinely lacks the capability (Windows supports AF_UNIX). Skip narrowly via test.skipIf with a reason; never fix one platform by loosening assertions for all. - **Decide explicitly: filesystem path or URL-like identifier.** Module specifiers, cache keys, sourcemap paths use forward slashes everywhere (posix path helpers). Filesystem paths use platform path APIs, never literal '/' concatenation. Windows: accept BOTH separators; drive-relative (C:foo) and UNC forms exist; PATH splits on ';'. On POSIX, backslash is a legal filename character. Splitting a posix-normalized string with the platform separator silently no-ops on Windows — feed the other separator style through every new API in tests. - **Beyond `rust:check-all` (required by CLAUDE.md) for platform-gated code:** verify link-time symbol resolution (a POSIX extern must still resolve on Windows even if runtime-gated); audit enum switches duplicated across platform arms; distrust lint sweeps — a cast redundant on your host may be load-bearing on another target. Trick: flip the platform condition locally to force the other branch through the type-checker. diff --git a/.claude/skills/rust-system-calls/SKILL.md b/.claude/skills/rust-system-calls/SKILL.md index 461c93429d7a..c95adce690da 100644 --- a/.claude/skills/rust-system-calls/SKILL.md +++ b/.claude/skills/rust-system-calls/SKILL.md @@ -37,7 +37,7 @@ pub fn write_file(path: &[u8], data: &[u8]) -> Result<(), bun_sys::Error> { | Aspect | bun_sys | std::fs / libc | | ----------- | ---------------------------------- | ---------------------- | | Return Type | `Maybe` with rich `Error` | `io::Error` (lossy) | -| Windows | Full support with libuv fallback | Incomplete/POSIX-ish | +| Windows | Full support (native Win32/NT) | Incomplete/POSIX-ish | | Error Info | errno, syscall tag, path, fd | errno only | | EINTR | Automatic retry | Manual handling | | Paths | `&[u8]` (WTF-8 safe) | `&Path` (UTF-8 lossy) | diff --git a/.github/workflows/CLAUDE.md b/.github/workflows/CLAUDE.md index 795124cd04b8..23df0b4c3cc3 100644 --- a/.github/workflows/CLAUDE.md +++ b/.github/workflows/CLAUDE.md @@ -22,7 +22,7 @@ The `format.yml` workflow runs code formatters (Prettier, clang-format, and `car **Important exclusions**: - `src/runtime/napi/` - Node API headers (third-party) -- `src/jsc/bindings/libuv/` - libuv headers (third-party) +- `src/jsc/bindings/libuv/` - libuv's `uv/errno.h` (third-party) - `src/jsc/bindings/sqlite/` - SQLite headers (third-party) - `src/runtime/ffi/ffi-*.h` - FFI headers (generated/third-party) - `src/deps/` - Dependencies (third-party) diff --git a/CLAUDE.md b/CLAUDE.md index 789ec881ff35..886c12deb326 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -147,7 +147,7 @@ The Rust side is a Cargo workspace of ~200 crates rooted at `Cargo.toml`. The ke #### Vendored Dependencies (`vendor/`) -Third-party native libraries are vendored locally and can be read from disk (not git submodules): boringssl (TLS/crypto), brotli, cares (async DNS), hdrhistogram, highway (SIMD), libarchive (tar/zip), libdeflate, libuv (Windows event loop), lolhtml (HTML rewriter), lshpack (HTTP/2 HPACK), lsqpack + lsquic (HTTP/3), mimalloc (allocator), nodejs (headers), picohttpparser, rust-argon2 (argon2 for `Bun.password`, Rust), tinycc (FFI JIT, fork: oven-sh/tinycc), WebKit (JavaScriptCore), zlib (zlib-ng), zstd. Build configuration for these is in `scripts/build/deps/*.ts`. +Third-party native libraries are vendored locally and can be read from disk (not git submodules): boringssl (TLS/crypto), brotli, cares (async DNS), hdrhistogram, highway (SIMD), libarchive (tar/zip), libdeflate, lolhtml (HTML rewriter), lshpack (HTTP/2 HPACK), lsqpack + lsquic (HTTP/3), mimalloc (allocator), nodejs (headers), picohttpparser, rust-argon2 (argon2 for `Bun.password`, Rust), tinycc (FFI JIT, fork: oven-sh/tinycc), WebKit (JavaScriptCore), zlib (zlib-ng), zstd. Build configuration for these is in `scripts/build/deps/*.ts`. ### JavaScript Class Implementation (C++) diff --git a/Cargo.lock b/Cargo.lock index cd424128a7f7..6914c3472b42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -332,8 +332,9 @@ version = "0.0.0" dependencies = [ "bun_alloc", "bun_core", - "bun_libuv_sys", + "bun_errno", "bun_opaque", + "bun_windows_sys", "libc", "strum", ] @@ -531,7 +532,6 @@ name = "bun_errno" version = "0.0.0" dependencies = [ "bun_core", - "bun_libuv_sys", "bun_windows_sys", "enum-map", "strum", @@ -731,6 +731,7 @@ dependencies = [ "bun_sha_hmac", "bun_simdutf_sys", "bun_spawn", + "bun_spawn_sys", "bun_sys", "bun_threading", "bun_transpiler", @@ -794,6 +795,7 @@ dependencies = [ "bun_sys", "bun_threading", "bun_uws_sys", + "bun_windows_sys", "enumset", "libc", "scopeguard", @@ -960,17 +962,6 @@ dependencies = [ "bun_opaque", ] -[[package]] -name = "bun_libuv_sys" -version = "0.0.0" -dependencies = [ - "bun_core", - "bun_windows_sys", - "bun_wyhash", - "libc", - "strum", -] - [[package]] name = "bun_lsquic_sys" version = "0.0.0" @@ -1065,7 +1056,6 @@ dependencies = [ "bun_collections", "bun_core", "bun_errno", - "bun_event_loop", "bun_paths", "bun_spawn", "bun_sys", @@ -1269,7 +1259,6 @@ dependencies = [ "bun_jsc", "bun_libarchive", "bun_libdeflate_sys", - "bun_libuv_sys", "bun_lsquic_sys", "bun_md", "bun_opaque", @@ -1294,6 +1283,7 @@ dependencies = [ "bun_sourcemap", "bun_sourcemap_jsc", "bun_spawn", + "bun_spawn_sys", "bun_sql_jsc", "bun_standalone_graph", "bun_sys", @@ -1450,19 +1440,18 @@ name = "bun_spawn" version = "0.0.0" dependencies = [ "bun_alloc", - "bun_analytics", "bun_core", "bun_crash_handler", "bun_dispatch", "bun_errno", "bun_event_loop", "bun_io", - "bun_opaque", "bun_output", "bun_ptr", "bun_spawn_sys", "bun_sys", "bun_threading", + "bun_uws_sys", "libc", "scopeguard", "thiserror", @@ -1475,7 +1464,6 @@ dependencies = [ "bstr", "bun_analytics", "bun_core", - "bun_libuv_sys", "bun_sys", "bun_windows_sys", "enumset", @@ -1572,7 +1560,6 @@ dependencies = [ "bun_alloc", "bun_core", "bun_errno", - "bun_libuv_sys", "bun_opaque", "bun_paths", "bun_windows_sys", @@ -1668,7 +1655,6 @@ dependencies = [ "bun_core", "bun_errno", "bun_http_types", - "bun_libuv_sys", "bun_opaque", "bun_paths", "bun_ptr", diff --git a/Cargo.toml b/Cargo.toml index 9e01506f6386..898df1717eaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,6 @@ members = [ "src/js_parser_jsc", "src/js_printer", "src/libdeflate_sys", - "src/libuv_sys", "src/ast", "src/ast_jsc", "src/picohttp", @@ -392,7 +391,6 @@ bun_js_parser = { path = "src/js_parser" } bun_js_parser_jsc = { path = "src/js_parser_jsc" } bun_js_printer = { path = "src/js_printer" } bun_libdeflate_sys = { path = "src/libdeflate_sys" } -bun_libuv_sys = { path = "src/libuv_sys" } bun_ast = { path = "src/ast" } bun_ast_jsc = { path = "src/ast_jsc" } bun_picohttp = { path = "src/picohttp" } diff --git a/LICENSE.md b/LICENSE.md index 3ae7d08305e1..16b3e07046dd 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -37,7 +37,7 @@ Bun statically links these libraries: | [`c-ares`](https://github.com/c-ares/c-ares) | MIT licensed | | [`libicu`](https://github.com/unicode-org/icu) 78 | [license here](https://github.com/unicode-org/icu/blob/main/icu4c/LICENSE) | | [`libbase64`](https://github.com/aklomp/base64/blob/master/LICENSE) | BSD 2-Clause | -| [`libuv`](https://github.com/libuv/libuv) (on Windows) | MIT | +| [`libuv`](https://github.com/libuv/libuv) (headers and ported functions) | MIT | | [`libdeflate`](https://github.com/ebiggers/libdeflate) | MIT | | [`libjpeg-turbo`](https://github.com/libjpeg-turbo/libjpeg-turbo) | [BSD 3-Clause / IJG / zlib](https://github.com/libjpeg-turbo/libjpeg-turbo/blob/main/LICENSE.md) | | [`libspng`](https://github.com/randy408/libspng) | BSD 2-Clause | diff --git a/docs/project/license.mdx b/docs/project/license.mdx index 67dc787c9943..8f58ad9301e9 100644 --- a/docs/project/license.mdx +++ b/docs/project/license.mdx @@ -42,7 +42,7 @@ Bun statically links these libraries: | [`c-ares`](https://github.com/c-ares/c-ares) | MIT licensed | | [`libicu`](https://github.com/unicode-org/icu) 78 | [ICU license](https://github.com/unicode-org/icu/blob/main/icu4c/LICENSE) | | [`libbase64`](https://github.com/aklomp/base64/blob/master/LICENSE) | BSD 2-Clause | -| [`libuv`](https://github.com/libuv/libuv) (on Windows) | MIT | +| [`libuv`](https://github.com/libuv/libuv) (headers and ported functions) | MIT | | [`libdeflate`](https://github.com/ebiggers/libdeflate) | MIT | | [`libjpeg-turbo`](https://github.com/libjpeg-turbo/libjpeg-turbo) | BSD 3-Clause / IJG / zlib | | [`libspng`](https://github.com/randy408/libspng) | BSD 2-Clause (portions derived from libpng, PNG Reference Library License v2) | diff --git a/docs/runtime/child-process.mdx b/docs/runtime/child-process.mdx index 9c2ea35ad4ff..3b9033b56ff9 100644 --- a/docs/runtime/child-process.mdx +++ b/docs/runtime/child-process.mdx @@ -446,7 +446,7 @@ When passing an existing `Terminal` object: - **No echo without a child process on Windows.** On POSIX, the kernel line discipline echoes `write()` input back to the `data` callback even with no process attached. ConPTY has no line discipline; it buffers input for the next reader. If you need echo, spawn a process that echoes. - **ConPTY re-encodes output.** ConPTY renders the child's output to a virtual screen and emits whatever VT sequences describe the result. The `data` callback therefore receives semantically equivalent, but not byte-identical, escape sequences. ConPTY preserves colors and text; it may reorder or coalesce cursor-positioning and reset sequences. ConPTY also emits a short VT init sequence (`\x1b[?9001h\x1b[?1004h…`) before any child output. - **Input `\r` is not translated to `\n` on Windows.** POSIX `ICRNL` maps carriage return to newline on input; ConPTY passes `\r` through unchanged. -- **`process.on('SIGWINCH')` in the child does not fire under ConPTY** unless the child is reading stdin in raw mode. `process.stdout.columns`/`rows` do update after `resize()`. The missing signal is a libuv limitation that affects any libuv-based child (Node.js included). +- **`process.on('SIGWINCH')` in the child does not fire under ConPTY** unless the child is reading stdin in raw mode: ConPTY reports a resize only to the reader of its input. A Bun child in raw mode gets `SIGWINCH` at once, as does a Node.js child (or any other libuv-based child). `process.stdout.columns`/`rows` do update after `resize()`. - On Windows before 11 24H2 (build 26100), `terminal.close()` may not terminate a still-running child promptly. The delay comes from [`ClosePseudoConsole`](https://learn.microsoft.com/en-us/windows/console/closepseudoconsole), which blocks on those versions until conhost has flushed its output through the pipe. Kill the attached process first if you need to tear down with a running child. - **Mouse tracking does not work on Windows 10 or Windows Server 2019.** The inbox ConPTY in those versions (conhost build 17763 through 19045) predates [microsoft/terminal#4856](https://github.com/microsoft/terminal/pull/4856) and drops the mouse-tracking enable sequences (`\x1b[?1000h`, `\x1b[?1002h`, `\x1b[?1006h`) that the child writes. They never reach the `data` callback, so the outer terminal never reports mouse events and the child never sees them. Windows 11 and Server 2022 relay the sequences, but only after the child has enabled VT input mode (for example with `process.stdin.setRawMode(true)`) before it writes them. diff --git a/mordant-baseline.toml b/mordant-baseline.toml index 78e960d63347..68a133db043a 100644 --- a/mordant-baseline.toml +++ b/mordant-baseline.toml @@ -75,12 +75,6 @@ "unused_pub:src/jsc/host_fn.rs" = 2 "unused_pub:src/jsc/lib.rs" = 1 -[bun_libuv_sys] -"reimplemented_helper:src/libuv_sys/libuv.rs" = 1 -"unused_pub:src/libuv_sys/lib.rs" = 2 -"unused_pub:src/libuv_sys/libuv.rs" = 41 -"unused_pub:src/libuv_sys/open_handles.rs" = 1 - [bun_opaque] "unused_pub:src/opaque/lib.rs" = 2 diff --git a/package.json b/package.json index 18306a94d38f..414d96078c21 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "build:types": "bun scripts/build.ts --mode=codegen --target=generated-types", "build:release:local": "bun scripts/build.ts --profile=release-local --build-dir=build/release-local", "run:linux": "docker run --rm -v \"$PWD:/root/bun/\" -w /root/bun ghcr.io/oven-sh/bun-development-docker-image", - "uv-posix-stubs": "bun run src/jsc/bindings/libuv/generate_uv_posix_stubs.ts", + "uv-stubs": "bun run src/jsc/bindings/libuv/generate_uv_stubs.ts", "orderfile": "bun scripts/orderfile/generate.ts", "jsc:build": "bun scripts/build.ts --profile=release-local --build-dir=build/release-local --target=WebKit", "jsc:build:debug": "bun scripts/build.ts --profile=debug-local --build-dir=build/debug-local --target=WebKit", diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index e6e36301b614..09fcc0fce1cd 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -2583,7 +2583,7 @@ declare module "bun" { * On macOS, `"getaddrinfo"` shouldn't be necessary because * `"system"` uses the same API underneath (except non-blocking). * - * On Windows, libuv's non-blocking DNS resolver is used by default, and + * On Windows, `GetAddrInfoW` runs in Bun's thread pool by default, and * when specifying backends "system", "libc", or "getaddrinfo". The c-ares * backend isn't supported on Windows. */ @@ -7627,8 +7627,8 @@ declare module "bun" { * - POSIX: calls `setsid()` so the child starts a new session and becomes * the process group leader. It can outlive the parent and receive * signals independently of the parent’s terminal/process group. - * - Windows: sets `UV_PROCESS_DETACHED`, allowing the child to outlive - * the parent and receive signals independently. + * - Windows: uses `DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP`, allowing + * the child to outlive the parent and receive signals independently. * * Note: stdio may keep the parent process alive. Pass `stdio: ["ignore", * "ignore", "ignore"]` to the spawn constructor to prevent this. @@ -7707,12 +7707,12 @@ declare module "bun" { * - `ArrayBufferView`: The process writes to the preallocated buffer. Not implemented. * - `number`: The process writes to the file descriptor * - * At indices >= 3, `"socket-fd"` (POSIX only) is also accepted: - * creates a socketpair like `"pipe"`, but the parent-end fd exposed - * via {@link Subprocess.stdio} is owned by the caller and is never - * closed by the subprocess. Use this when you wrap the fd in + * At indices >= 3, `"socket-fd"` is also accepted: creates a + * socketpair (a duplex named pipe on Windows) like `"pipe"`, but the + * parent end exposed via {@link Subprocess.stdio} (a file descriptor; + * on Windows the pipe's `HANDLE` value) is owned by the caller and is + * never closed by the subprocess. Use this when you wrap it in * something that will close it itself (e.g. `net.connect({fd})`). - * On Windows it behaves the same as `"pipe"`. * * @default ["ignore", "pipe", "inherit"] for `spawn` * ["ignore", "pipe", "pipe"] for `spawnSync` diff --git a/packages/bun-usockets/src/bsd.c b/packages/bun-usockets/src/bsd.c index a1190b3a6515..1369543cc00d 100644 --- a/packages/bun-usockets/src/bsd.c +++ b/packages/bun-usockets/src/bsd.c @@ -58,6 +58,7 @@ static void init_debug_logging() { #include #include #else /* _WIN32 */ +#include #include #endif @@ -72,10 +73,25 @@ extern int Bun__doesMacOSVersionSupportSendRecvMsgX(); #endif #if defined(_WIN32) -/* libuv initializes Winsock on first use; every entry point below that creates +/* Winsock is initialized on first use (WSAStartup costs about a millisecond, + * and not every process opens a socket); every entry point below that creates * a socket or resolves an address makes sure that has happened first. */ -extern void uv__winsock_ensure(void); -#define bsd_winsock_ensure() uv__winsock_ensure() +static BOOL CALLBACK bsd_winsock_init(PINIT_ONCE once, PVOID param, PVOID *context) { + (void) once; + (void) param; + (void) context; + WSADATA wsa_data; + /* Tried once: where it fails (safe mode without networking), every Winsock + * call reports WSANOTINITIALISED, which is the error to show. */ + WSAStartup(MAKEWORD(2, 2), &wsa_data); + return TRUE; +} + +void us_internal_winsock_ensure(void) { + static INIT_ONCE once = INIT_ONCE_STATIC_INIT; + InitOnceExecuteOnce(&once, bsd_winsock_init, NULL, NULL); +} +#define bsd_winsock_ensure() us_internal_winsock_ensure() #else #define bsd_winsock_ensure() ((void)0) #endif @@ -333,7 +349,7 @@ LIBUS_SOCKET_DESCRIPTOR apple_no_sigpipe(LIBUS_SOCKET_DESCRIPTOR fd) { static LIBUS_SOCKET_DESCRIPTOR win32_set_nonblocking(LIBUS_SOCKET_DESCRIPTOR fd) { #if _WIN32 if (fd != LIBUS_SOCKET_ERROR) { - // libuv will set non-blocking, but only on poll init! + // us_poll_start_rc sets non-blocking, but only on poll init! // we need it to be set on connect as well DWORD yes = 1; ioctlsocket(fd, FIONBIO, &yes); @@ -345,7 +361,7 @@ static LIBUS_SOCKET_DESCRIPTOR win32_set_nonblocking(LIBUS_SOCKET_DESCRIPTOR fd) } LIBUS_SOCKET_DESCRIPTOR bsd_set_nonblocking(LIBUS_SOCKET_DESCRIPTOR fd) { -/* Libuv will set windows sockets as non-blocking */ +/* us_poll_start_rc sets windows sockets as non-blocking */ #ifndef _WIN32 if (LIKELY(fd != LIBUS_SOCKET_ERROR)) { int flags = fcntl(fd, F_GETFL, 0); @@ -1245,11 +1261,14 @@ LIBUS_SOCKET_DESCRIPTOR bsd_socket_import(void *info, int *err) { bsd_winsock_ensure(); #ifdef _WIN32 SOCKET s = WSASocketW(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, - (WSAPROTOCOL_INFOW *) info, 0, WSA_FLAG_OVERLAPPED); + (WSAPROTOCOL_INFOW *) info, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); if (s == INVALID_SOCKET) { *err = WSAGetLastError(); return LIBUS_SOCKET_ERROR; } + /* The handle was put into this process by the sender's WSADuplicateSocketW, + * inheritable, and the flag above does not change one that exists. */ + SetHandleInformation((HANDLE) s, HANDLE_FLAG_INHERIT, 0); return s; #else (void) info; diff --git a/packages/bun-usockets/src/context.c b/packages/bun-usockets/src/context.c index 2cd6206381f7..a288158446c6 100644 --- a/packages/bun-usockets/src/context.c +++ b/packages/bun-usockets/src/context.c @@ -381,7 +381,7 @@ struct us_listen_socket_t *us_socket_group_listen(struct us_socket_group_t *grou struct us_poll_t *p = us_create_poll(group->loop, 0, sizeof(struct us_listen_socket_t)); us_poll_init(p, listen_socket_fd, POLL_TYPE_SEMI_SOCKET); - if (us_poll_start_rc(p, group->loop, LIBUS_SOCKET_READABLE) != 0) { + if (us_internal_poll_start_accepting(p, group->loop) != 0) { /* EPOLL_CTL_ADD failed (e.g. ENOSPC at fs.epoll.max_user_watches). * Report via both the out-param and thread-local errno: Bun.listen * reads *error, Bun.serve reads errno. */ @@ -420,7 +420,7 @@ struct us_listen_socket_t *us_socket_group_listen_fd(struct us_socket_group_t *g struct us_poll_t *p = us_create_poll(group->loop, 0, sizeof(struct us_listen_socket_t)); us_poll_init(p, fd, POLL_TYPE_SEMI_SOCKET); - if (us_poll_start_rc(p, group->loop, LIBUS_SOCKET_READABLE) != 0) { + if (us_internal_poll_start_accepting(p, group->loop) != 0) { int saved_errno = LIBUS_ERR; us_poll_free(p, group->loop); *error = saved_errno; @@ -447,7 +447,7 @@ struct us_listen_socket_t *us_socket_group_listen_unix(struct us_socket_group_t struct us_poll_t *p = us_create_poll(group->loop, 0, sizeof(struct us_listen_socket_t)); us_poll_init(p, listen_socket_fd, POLL_TYPE_SEMI_SOCKET); - if (us_poll_start_rc(p, group->loop, LIBUS_SOCKET_READABLE) != 0) { + if (us_internal_poll_start_accepting(p, group->loop) != 0) { int saved_errno = errno; bsd_close_socket(listen_socket_fd); us_poll_free(p, group->loop); @@ -643,11 +643,7 @@ void *us_socket_group_connect(struct us_socket_group_t *group, unsigned char kin c->port = port; us_internal_socket_group_link_connecting_socket(group, c); -#ifdef _WIN32 - loop->uv_loop->active_handles++; -#else loop->num_polls++; -#endif Bun__addrinfo_set(ai_req, c); @@ -723,7 +719,7 @@ int start_connections(struct us_connecting_socket_t *c, int count) { void us_internal_socket_after_resolve(struct us_connecting_socket_t *c) { /* close_all() may have run between the DNS thread queuing this callback and * us reaching it; c->group is NULL'd at close so it can't be touched. The - * keep-alive (num_polls/active_handles) was already balanced by the close + * keep-alive (num_polls) was already balanced by the close * path's Bun__addrinfo_cancel branch. */ c->pending_resolve_callback = 0; if (c->closed) { @@ -736,11 +732,7 @@ void us_internal_socket_after_resolve(struct us_connecting_socket_t *c) { } struct us_socket_group_t *group = c->group; -#ifdef _WIN32 - group->loop->uv_loop->active_handles--; -#else group->loop->num_polls--; -#endif struct addrinfo_result *result = Bun__addrinfo_getRequestResult(c->addrinfo_req); if (result->error) { /* Preserve the getaddrinfo failure so the connect-error callback can @@ -767,29 +759,6 @@ void us_internal_socket_after_resolve(struct us_connecting_socket_t *c) { void us_internal_socket_after_open(struct us_socket_t *s, int error) { struct us_connecting_socket_t *c = s->connect_state; - #if _WIN32 - if (error == 0) { - if (recv(us_poll_fd((struct us_poll_t*)s), NULL, 0, MSG_PUSH_IMMEDIATE) == SOCKET_ERROR) { - error = WSAGetLastError(); - switch (error) { - case WSAEWOULDBLOCK: - case WSAEINTR: { - error = 0; - break; - } - default: { - /* The probe only says the socket is not connected - * (WSAENOTCONN); SO_ERROR has why the connect failed. */ - int so_error = us_socket_get_error(s); - if (so_error > 0) { - error = so_error; - } - break; - } - } - } - } - #endif if (error) { if (c) { for (struct us_socket_t **next = &c->connecting_head; *next; next = &(*next)->connect_next) { diff --git a/packages/bun-usockets/src/crypto/openssl.c b/packages/bun-usockets/src/crypto/openssl.c index 529d81169e37..f6fc9cafc8f8 100644 --- a/packages/bun-usockets/src/crypto/openssl.c +++ b/packages/bun-usockets/src/crypto/openssl.c @@ -2294,8 +2294,7 @@ struct us_socket_t *us_internal_ssl_on_end(struct us_socket_t *s) { * responses still flush and the app's own end() completes the * shutdown. With SENT_SHUTDOWN both directions are closed; fall * through so a deferred graceful close completes (mirrors the - * ZERO_RETURN path; loop.c raw-closes this case first on the - * epoll/kqueue backend, the libuv one reaches here). */ + * ZERO_RETURN path). */ return s; } } @@ -2336,7 +2335,7 @@ struct us_socket_t *us_internal_ssl_on_writable(struct us_socket_t *s) { * uWS layer's flushed==0-after-FIN guard, or hasFullyDrained() when * nothing is buffered, then closes the connection on this dispatch) * and dispatch directly, bypassing the is_shut_down gate below that - * ssl_fatal_error would otherwise trip. On the libuv backend zero + * ssl_fatal_error would otherwise trip. On Windows zero * progress does not prove death (a stale SEND completion can run * after this loop turn refilled the buffer), so confirm with the * kernel before declaring the spill undrainable. */ diff --git a/packages/bun-usockets/src/eventing/epoll_kqueue.c b/packages/bun-usockets/src/eventing/epoll_kqueue.c index 0f861e510ee6..156008cebc4f 100644 --- a/packages/bun-usockets/src/eventing/epoll_kqueue.c +++ b/packages/bun-usockets/src/eventing/epoll_kqueue.c @@ -23,23 +23,12 @@ #include #if defined(LIBUS_USE_EPOLL) || defined(LIBUS_USE_KQUEUE) -void Bun__internal_dispatch_ready_poll(void* loop, void* poll); -// void Bun__internal_dispatch_ready_poll(void* loop, void* poll) {} - -#ifndef WIN32 -/* Cannot include this one on Windows */ #include #include #include #include // memset #include -#endif - -void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec* timeout, uint64_t now_ns); -/* Pointer tags are used to indicate a Bun pointer versus a uSockets pointer */ -#define UNSET_BITS_49_UNTIL_64 0x0000FFFFFFFFFFFF -#define CLEAR_POINTER_TAG(p) ((void *) ((uintptr_t) (p) & UNSET_BITS_49_UNTIL_64)) #define LIKELY(cond) __builtin_expect((_Bool)(cond), 1) #define UNLIKELY(cond) __builtin_expect((_Bool)(cond), 0) @@ -76,7 +65,6 @@ void us_poll_free(struct us_poll_t *p, struct us_loop_t *loop) { us_free(p); } -/* Todo: why have us_poll_create AND us_poll_init!? libuv legacy! */ void us_poll_init(struct us_poll_t *p, LIBUS_SOCKET_DESCRIPTOR fd, int poll_type) { p->state.fd = fd; p->state.poll_type = poll_type; @@ -219,7 +207,7 @@ static int bun_kevent64_wait(int kqfd, struct kevent64_s *eventlist, int nevents #endif /* Loop */ -struct us_loop_t *us_create_loop(void *hint, void (*wakeup_cb)(struct us_loop_t *loop), void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop), unsigned int ext_size) { +struct us_loop_t *us_create_loop(void (*wakeup_cb)(struct us_loop_t *loop), void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop), unsigned int ext_size) { struct us_loop_t *loop = (struct us_loop_t *) us_calloc(1, sizeof(struct us_loop_t) + ext_size); loop->num_polls = 0; /* These could be accessed if we close a poll before starting the loop */ @@ -254,7 +242,6 @@ struct us_loop_t *us_create_loop(void *hint, void (*wakeup_cb)(struct us_loop_t return loop; } -/* Shared dispatch loop for both us_loop_run and us_loop_run_bun_tick */ static void us_internal_dispatch_ready_polls(struct us_loop_t *loop) { #ifdef LIBUS_USE_EPOLL for (loop->current_ready_poll = 0; loop->current_ready_poll < loop->num_ready_polls; loop->current_ready_poll++) { @@ -408,7 +395,7 @@ static void us_internal_dispatch_ready_polls(struct us_loop_t *loop) { * Re-poll non-blocking and dispatch again before running pre/post callbacks, so a * single tick covers all pending I/O instead of one 1024-event slice per roundtrip. * Conditioned on saturation and capped at 48 iterations — matches libuv's uv__io_poll - * (vendor/libuv/src/unix/linux.c:1387,1590 and kqueue.c:253,451). */ + * (src/unix/linux.c, src/unix/kqueue.c). */ static void us_internal_drain_ready_polls(struct us_loop_t *loop) { int drain_count = 48; while (UNLIKELY(loop->num_ready_polls == LIBUS_MAX_READY_POLLS) && --drain_count != 0 && loop->num_polls > 0) { @@ -445,34 +432,7 @@ static const struct timespec *us_internal_clamp_to_sweep(struct us_loop_t *loop, return storage; } -void us_loop_run(struct us_loop_t *loop) { - /* While we have non-fallthrough polls we shouldn't fall through */ - while (loop->num_polls) { - loop->data.tick_depth++; - /* Emit pre callback */ - us_internal_loop_pre(loop); - - struct timespec sweep_ts; - const struct timespec *timeout = us_internal_clamp_to_sweep(loop, NULL, &sweep_ts); - - /* Fetch ready polls */ -#ifdef LIBUS_USE_EPOLL - loop->num_ready_polls = bun_epoll_pwait2(loop->fd, loop->ready_polls, LIBUS_MAX_READY_POLLS, timeout); -#else - loop->num_ready_polls = bun_kevent64_wait(loop->fd, loop->ready_polls, LIBUS_MAX_READY_POLLS, 0, timeout); -#endif - - us_internal_dispatch_ready_polls(loop); - us_internal_drain_ready_polls(loop); - us_internal_sweep_if_due(loop); - - /* Emit post callback */ - us_internal_loop_post(loop); - loop->data.tick_depth--; - } -} - -extern unsigned int Bun__JSC_onBeforeWait(void * _Nonnull jsc_vm, uint64_t now_ns, int * _Nullable released_heap_access); +extern void Bun__JSC_onBeforeWait(void * _Nonnull jsc_vm, int * _Nonnull released_heap_access); extern void Bun__JSC_acquireHeapAccessAfterWait(void * _Nonnull jsc_vm); void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec* timeout, uint64_t now_ns) { @@ -504,28 +464,18 @@ void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec* timeout const unsigned int had_wakeups = __atomic_exchange_n(&loop->pending_wakeups, 0, __ATOMIC_ACQUIRE); const int will_idle_inside_event_loop = had_wakeups == 0 && (!timeout || (timeout->tv_nsec != 0 || timeout->tv_sec != 0)); - /* `now_ns` is the reading the JS side took to pick `timeout` - * (timer::All::get_timeout), reused here to rate-limit the idle sweep; 0 - * if it had none to share. Nothing measures a deadline against it. */ int released_heap_access = 0; if (will_idle_inside_event_loop && loop->data.jsc_vm) - (void) Bun__JSC_onBeforeWait(loop->data.jsc_vm, now_ns, &released_heap_access); + Bun__JSC_onBeforeWait(loop->data.jsc_vm, &released_heap_access); /* The scavenger sweeps our heaps while we are in the kernel. Must come after * Bun__JSC_onBeforeWait, which allocates: nothing may touch our heaps until the matching - * _end. mimalloc paces the sweep itself, so this costs a compare-and-swap per tick. + * _end. mimalloc paces the sweep itself. * With no scavenger to hand off to, fall back to sweeping inline -- but only on a tick that * really parks, and rate-limited, because doing it between ticks is what we are avoiding. */ const int handed_off = mi_on_thread_idle_start(); - if (!handed_off && will_idle_inside_event_loop) { - static const uint64_t idle_sweep_interval_ns = 100 * 1000000ULL; - static _Thread_local uint64_t last_idle_sweep_ns = 0; - const uint64_t sweep_now_ns = now_ns ? now_ns : us_internal_monotonic_ns(); - if (sweep_now_ns >= last_idle_sweep_ns + idle_sweep_interval_ns) { - last_idle_sweep_ns = sweep_now_ns; - mi_on_thread_idle(); - } - } + if (!handed_off && will_idle_inside_event_loop) + us_internal_idle_sweep(now_ns); /* Fetch ready polls */ #ifdef LIBUS_USE_EPOLL @@ -785,6 +735,14 @@ void us_poll_stop(struct us_poll_t *p, struct us_loop_t *loop) { us_internal_loop_update_pending_ready_polls(loop, p, 0, old_events, new_events); } +int us_internal_poll_start_accepting(struct us_poll_t *p, struct us_loop_t *loop) { + return us_poll_start_rc(p, loop, LIBUS_SOCKET_READABLE); +} + +LIBUS_SOCKET_DESCRIPTOR us_internal_accept(struct us_poll_t *p, struct bsd_addr_t *addr) { + return bsd_accept_socket(us_poll_fd(p), addr); +} + size_t us_internal_accept_poll_event(struct us_poll_t *p) { #ifdef LIBUS_USE_EPOLL int fd = us_poll_fd(p); @@ -819,7 +777,6 @@ struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int f struct us_internal_callback_t *cb = (struct us_internal_callback_t *) p; cb->loop = loop; - cb->cb_expects_the_loop = 1; cb->leave_poll_ready = 1; /* Edge-triggered: skip reading eventfd on wakeup */ return (struct us_internal_async *) cb; @@ -872,7 +829,6 @@ void us_internal_async_wakeup(struct us_internal_async *a) { struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) { struct us_internal_callback_t *cb = us_calloc(1, sizeof(struct us_internal_callback_t) + ext_size); cb->loop = loop; - cb->cb_expects_the_loop = 1; cb->leave_poll_ready = 0; /* Bug: us_internal_poll_set_type does not SET the type, it only CHANGES it */ @@ -1011,7 +967,6 @@ void us_internal_async_wakeup(struct us_internal_async *a) { struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) { struct us_internal_callback_t *cb = us_calloc(1, sizeof(struct us_internal_callback_t) + ext_size); cb->loop = loop; - cb->cb_expects_the_loop = 1; cb->leave_poll_ready = 0; cb->p.state.poll_type = POLL_TYPE_POLLING_IN; @@ -1065,13 +1020,4 @@ void us_internal_async_wakeup(struct us_internal_async *a) { } #endif -int us_socket_get_error(struct us_socket_t *s) { - int error = 0; - socklen_t len = sizeof(error); - if (getsockopt(us_poll_fd((struct us_poll_t *) s), SOL_SOCKET, SO_ERROR, (char *) &error, &len) == -1) { - return errno; - } - return error; -} - #endif diff --git a/packages/bun-usockets/src/eventing/iocp.c b/packages/bun-usockets/src/eventing/iocp.c new file mode 100644 index 000000000000..99b0e879bbf0 --- /dev/null +++ b/packages/bun-usockets/src/eventing/iocp.c @@ -0,0 +1,1969 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libusockets.h" +#include "internal/internal.h" +#include "internal/fault_inject.h" + +#ifdef LIBUS_USE_IOCP + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Socket readiness on Windows comes from the AFD driver: an IOCTL_AFD_POLL + * issued on a \Device\Afd handle that is associated with the loop's completion + * port completes when one of the requested conditions holds for the socket. + * A poll is one-shot and level-triggered (a condition that already holds + * completes it at once), so interest is kept armed by submitting again after + * each completion. This is the mechanism behind wepoll, mio, c-ares and + * libuv's uv_poll_t. */ + +extern void Bun__JSC_onBeforeWait(void *_Nonnull jsc_vm, int *_Nonnull released_heap_access); +extern void Bun__JSC_acquireHeapAccessAfterWait(void *_Nonnull jsc_vm); +/* Closes what Bun still has open on `loop`: pipes, consoles, files, pipe + * connects and process exit waits. Each cancels its operations. */ +extern void Bun__closeAllForLoop(struct us_loop_t *_Nonnull loop); + +#ifndef STATUS_SUCCESS +#define STATUS_SUCCESS ((NTSTATUS) 0x00000000L) +#endif +#ifndef STATUS_PENDING +#define STATUS_PENDING ((NTSTATUS) 0x00000103L) +#endif +#ifndef STATUS_CANCELLED +#define STATUS_CANCELLED ((NTSTATUS) 0xC0000120L) +#endif +#ifndef STATUS_UNSUCCESSFUL +#define STATUS_UNSUCCESSFUL ((NTSTATUS) 0xC0000001L) +#endif +#ifndef STATUS_INVALID_HANDLE +#define STATUS_INVALID_HANDLE ((NTSTATUS) 0xC0000008L) +#endif +#ifndef STATUS_INVALID_PARAMETER +#define STATUS_INVALID_PARAMETER ((NTSTATUS) 0xC000000DL) +#endif +#ifndef STATUS_ACCESS_DENIED +#define STATUS_ACCESS_DENIED ((NTSTATUS) 0xC0000022L) +#endif +#ifndef STATUS_OBJECT_TYPE_MISMATCH +#define STATUS_OBJECT_TYPE_MISMATCH ((NTSTATUS) 0xC0000024L) +#endif +#ifndef NT_SUCCESS +#define NT_SUCCESS(status) (((NTSTATUS) (status)) >= 0) +#endif + +#define IOCTL_AFD_POLL 0x00012024 + +#define AFD_POLL_RECEIVE 0x0001 +#define AFD_POLL_RECEIVE_EXPEDITED 0x0002 +#define AFD_POLL_SEND 0x0004 +#define AFD_POLL_DISCONNECT 0x0008 +#define AFD_POLL_ABORT 0x0010 +#define AFD_POLL_LOCAL_CLOSE 0x0020 +/* AFD_POLL_CONNECT (0x0040) is never requested: it makes every poll of a + * connected socket complete immediately. */ +#define AFD_POLL_ACCEPT 0x0080 +#define AFD_POLL_CONNECT_FAIL 0x0100 + +typedef struct { + HANDLE Handle; + ULONG Events; + NTSTATUS Status; +} AFD_POLL_HANDLE_INFO; + +typedef struct { + LARGE_INTEGER Timeout; + ULONG NumberOfHandles; + ULONG Exclusive; + AFD_POLL_HANDLE_INFO Handles[1]; +} AFD_POLL_INFO; + +/* NtCancelIoFileEx walks the requests outstanding on the file object, so its + * cost grows with their number; spreading sockets over several \Device\Afd + * handles keeps it flat (measured: ~2us at 128 per handle, 65-125us with 8192 + * on one). */ +#define AFD_POLLS_PER_HELPER 128 + +/* Completion key of the wait timer's packet; every other packet has key 0. */ +#define HRTIMER_COMPLETION_KEY ((ULONG_PTR) 1) + +struct us_internal_afd_helper { + HANDLE handle; + unsigned int count; + struct us_internal_afd_helper *next; +}; + +enum { + AFD_POLL_STATE_IDLE = 0, + /* A poll is outstanding. */ + AFD_POLL_STATE_PENDING, + /* A poll is outstanding and a cancel was requested for it. */ + AFD_POLL_STATE_CANCELLED, +}; + +/* The kernel writes `iosb` and `info` when the completion packet is dequeued, + * so this is freed only from its own completion (or while no poll is + * outstanding), never when the owner goes away. */ +struct us_internal_afd_poll { + struct us_iocp_op op; + IO_STATUS_BLOCK iosb; + AFD_POLL_INFO info; + /* us_poll_t*, a Bun-tagged pointer, or NULL once the owner stopped polling. */ + void *owner; + struct us_loop_t *loop; + struct us_internal_afd_helper *helper; + SOCKET socket; + SOCKET base_socket; + struct us_internal_afd_poll *update_next; + /* LIBUS_SOCKET_* the owner wants. */ + int interest; + /* AFD_POLL_* of the outstanding poll. */ + ULONG pending_events; + unsigned char state; + unsigned char queued_for_update; + /* DISCONNECT and ABORT stay signalled once raised. A poll that is not + * reading leaves one out of its mask after reporting it, until the owner + * changes its interest. */ + unsigned char reported_disconnect; + unsigned char reported_abort; + /* The socket's provider is not AFD (a non-IFS LSP without a base handle): + * readiness comes from select() on a thread-pool thread instead, and + * `state` stays idle. A select() cannot be interrupted, so when the + * interest changes a second one runs alongside the first. */ + unsigned char slow; + unsigned char slow_requests; + int slow_submitted_interest; + /* The `slow_requests` that are out. */ + struct us_internal_slow_poll_req *slow_reqs[2]; +}; + +/* Who finishes a slow request. Its thread posts it, unless the loop stopped + * counting it first: then the thread frees it and posts nothing. */ +enum { + SLOW_REQ_PENDING = 0, + SLOW_REQ_POSTED = 1, + SLOW_REQ_ORPHANED = 2, +}; + +struct us_internal_slow_poll_req { + struct us_iocp_op op; + struct us_internal_afd_poll *poll; + /* The thread's own handle to the port: select() can return after the loop was freed. */ + HANDLE port; + SOCKET socket; + int interest; + int result_events; + int result_error; + /* The socket was in select()'s exceptfds. */ + int result_except; + volatile LONG state; + /* loop->slow_reqs. The loop thread's. */ + struct us_internal_slow_poll_req *prev, *next; +}; + +typedef NTSTATUS(NTAPI *nt_create_file_fn)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK, PLARGE_INTEGER, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG); +typedef NTSTATUS(NTAPI *nt_device_io_control_file_fn)(HANDLE, HANDLE, PVOID, PVOID, PIO_STATUS_BLOCK, ULONG, PVOID, ULONG, PVOID, ULONG); +typedef NTSTATUS(NTAPI *nt_cancel_io_file_ex_fn)(HANDLE, PIO_STATUS_BLOCK, PIO_STATUS_BLOCK); +typedef NTSTATUS(NTAPI *nt_create_wait_completion_packet_fn)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES); +typedef NTSTATUS(NTAPI *nt_associate_wait_completion_packet_fn)(HANDLE, HANDLE, HANDLE, PVOID, PVOID, NTSTATUS, ULONG_PTR, PBOOLEAN); +typedef NTSTATUS(NTAPI *nt_cancel_wait_completion_packet_fn)(HANDLE, BOOLEAN); + +static INIT_ONCE nt_once = INIT_ONCE_STATIC_INIT; +static nt_create_file_fn pNtCreateFile; +static nt_device_io_control_file_fn pNtDeviceIoControlFile; +static nt_cancel_io_file_ex_fn pNtCancelIoFileEx; +/* Absent under Wine; the callers fall back. */ +static nt_create_wait_completion_packet_fn pNtCreateWaitCompletionPacket; +static nt_associate_wait_completion_packet_fn pNtAssociateWaitCompletionPacket; +static nt_cancel_wait_completion_packet_fn pNtCancelWaitCompletionPacket; +static uint64_t qpc_frequency; + +static BOOL CALLBACK nt_init(PINIT_ONCE once, PVOID param, PVOID *context) { + (void) once; + (void) param; + (void) context; + HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); + pNtCreateFile = (nt_create_file_fn) (void *) GetProcAddress(ntdll, "NtCreateFile"); + pNtDeviceIoControlFile = (nt_device_io_control_file_fn) (void *) GetProcAddress(ntdll, "NtDeviceIoControlFile"); + pNtCancelIoFileEx = (nt_cancel_io_file_ex_fn) (void *) GetProcAddress(ntdll, "NtCancelIoFileEx"); + pNtCreateWaitCompletionPacket = (nt_create_wait_completion_packet_fn) (void *) GetProcAddress(ntdll, "NtCreateWaitCompletionPacket"); + pNtAssociateWaitCompletionPacket = (nt_associate_wait_completion_packet_fn) (void *) GetProcAddress(ntdll, "NtAssociateWaitCompletionPacket"); + pNtCancelWaitCompletionPacket = (nt_cancel_wait_completion_packet_fn) (void *) GetProcAddress(ntdll, "NtCancelWaitCompletionPacket"); + if (!pNtCreateWaitCompletionPacket || !pNtAssociateWaitCompletionPacket || !pNtCancelWaitCompletionPacket) { + pNtCreateWaitCompletionPacket = NULL; + } + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + qpc_frequency = (uint64_t) frequency.QuadPart; + return pNtCreateFile && pNtDeviceIoControlFile && pNtCancelIoFileEx; +} + +static int nt_ensure(void) { + return InitOnceExecuteOnce(&nt_once, nt_init, NULL, NULL) != 0; +} + +uint64_t us_internal_monotonic_ns(void) { + nt_ensure(); + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); + uint64_t ticks = (uint64_t) counter.QuadPart; + return (ticks / qpc_frequency) * 1000000000ULL + ((ticks % qpc_frequency) * 1000000000ULL) / qpc_frequency; +} + +HANDLE us_loop_iocp(struct us_loop_t *loop) { + return loop->iocp; +} + +void us_iocp_op_submitted(struct us_loop_t *loop) { + InterlockedIncrement((volatile LONG *) &loop->pending_ops); +} + +void us_iocp_op_abandoned(struct us_loop_t *loop) { + InterlockedDecrement((volatile LONG *) &loop->pending_ops); +} + +void us_iocp_op_ready(struct us_loop_t *loop, struct us_iocp_op *op) { + op->next_ready = NULL; + if (loop->ready_ops_tail) { + loop->ready_ops_tail->next_ready = op; + } else { + loop->ready_ops_head = op; + } + loop->ready_ops_tail = op; + loop->num_ready_ops++; +} + +void us_iocp_starved_link(struct us_loop_t *loop, struct us_iocp_starved *starved) { + starved->prev = NULL; + starved->next = loop->starved; + if (starved->next) { + starved->next->prev = starved; + } + loop->starved = starved; + us_internal_enable_sweep_timer(loop); +} + +void us_iocp_starved_unlink(struct us_loop_t *loop, struct us_iocp_starved *starved) { + if (starved->prev) { + starved->prev->next = starved->next; + } else { + loop->starved = starved->next; + } + if (starved->next) { + starved->next->prev = starved->prev; + } + us_internal_disable_sweep_timer(loop); +} + +static void us_internal_retry_starved(struct us_loop_t *loop) { + for (struct us_iocp_starved *starved = loop->starved, *next; starved; starved = next) { + next = starved->next; + starved->retry(starved); + } +} + +/* AFD helper handles */ + +static struct us_internal_afd_helper *afd_helper_acquire(struct us_loop_t *loop) { + for (struct us_internal_afd_helper *h = loop->afd_helpers; h; h = h->next) { + if (h->count < AFD_POLLS_PER_HELPER) { + h->count++; + return h; + } + } + + /* Any name under \Device\Afd opens the driver; the suffix only shows up in handle listings. */ + static const WCHAR name[] = L"\\Device\\Afd\\Bun"; + UNICODE_STRING device_name; + device_name.Buffer = (PWSTR) name; + device_name.Length = sizeof(name) - sizeof(WCHAR); + device_name.MaximumLength = sizeof(name); + OBJECT_ATTRIBUTES attributes; + InitializeObjectAttributes(&attributes, &device_name, 0, NULL, NULL); + + HANDLE handle = NULL; + IO_STATUS_BLOCK iosb; + NTSTATUS status = pNtCreateFile(&handle, SYNCHRONIZE, &attributes, &iosb, NULL, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, 0, NULL, 0); + if (!NT_SUCCESS(status)) { + return NULL; + } + if (CreateIoCompletionPort(handle, loop->iocp, 0, 0) == NULL) { + CloseHandle(handle); + return NULL; + } + + struct us_internal_afd_helper *h = us_calloc(1, sizeof(struct us_internal_afd_helper)); + h->handle = handle; + h->count = 1; + h->next = loop->afd_helpers; + loop->afd_helpers = h; + return h; +} + +/* The socket AFD knows about. A layered service provider hands out its own + * handle; SIO_BASE_HANDLE unwraps it, and SIO_BSP_HANDLE_POLL peels one layer + * for providers that intercept SIO_BASE_HANDLE. INVALID_SOCKET if the chain + * does not end at an AFD socket; `*not_a_socket` when the handle is no socket + * at all, which is an error rather than a case for the select() fallback. */ +static SOCKET afd_base_socket(SOCKET socket, int *not_a_socket) { + *not_a_socket = 0; + const SOCKET given = socket; + for (;;) { + SOCKET base = INVALID_SOCKET; + DWORD bytes; + if (WSAIoctl(socket, SIO_BASE_HANDLE, NULL, 0, &base, sizeof(base), &bytes, NULL, NULL) != SOCKET_ERROR) { + return base; + } + if (WSAGetLastError() == WSAENOTSOCK) { + *not_a_socket = socket == given; + return INVALID_SOCKET; + } + /* One layer down. A provider that answers with its own socket has none below it. */ + if (WSAIoctl(socket, SIO_BSP_HANDLE_POLL, NULL, 0, &base, sizeof(base), &bytes, NULL, NULL) == SOCKET_ERROR || base == socket) { + return INVALID_SOCKET; + } + socket = base; + } +} + +static void afd_poll_free(struct us_internal_afd_poll *poll) { + if (poll->helper) { + poll->helper->count--; + } + us_free(poll); +} + +static void afd_poll_queue_update(struct us_internal_afd_poll *poll) { + if (poll->queued_for_update) { + return; + } + poll->queued_for_update = 1; + poll->update_next = NULL; + if (poll->loop->afd_update_tail) { + poll->loop->afd_update_tail->update_next = poll; + } else { + poll->loop->afd_update_head = poll; + } + poll->loop->afd_update_tail = poll; +} + +static int afd_poll_owner_kind(struct us_internal_afd_poll *poll) { + if (CLEAR_POINTER_TAG(poll->owner) != poll->owner) { + return -1; + } + return us_internal_poll_type((struct us_poll_t *) poll->owner); +} + +static ULONG afd_poll_wanted_events(struct us_internal_afd_poll *poll) { + ULONG events = AFD_POLL_LOCAL_CLOSE; + if (poll->interest & LIBUS_SOCKET_READABLE) { + /* RECEIVE reports neither the peer's FIN nor its RST. A reader is told + * about both as readable for as long as they hold, so that recv() + * finds the end of the stream after whatever data came with it. */ + return events | AFD_POLL_RECEIVE | AFD_POLL_ACCEPT | AFD_POLL_DISCONNECT | AFD_POLL_ABORT | + ((poll->interest & LIBUS_SOCKET_WRITABLE) ? AFD_POLL_SEND | AFD_POLL_CONNECT_FAIL : 0); + } + if (poll->interest & LIBUS_SOCKET_WRITABLE) { + events |= AFD_POLL_SEND | AFD_POLL_CONNECT_FAIL; + } + /* Not reading (paused, half-open, connecting): still asked for, which is + * what epoll reports unasked as EPOLLHUP/EPOLLERR, but only until it has + * been reported once: both stay signalled, and nothing here consumes them. */ + if (!poll->reported_disconnect) { + events |= AFD_POLL_DISCONNECT; + } + if (!poll->reported_abort) { + events |= AFD_POLL_ABORT; + } + return events; +} + +static void afd_poll_complete(struct us_loop_t *loop, struct us_iocp_op *op); +static int slow_poll_submit(struct us_internal_afd_poll *poll); +static void slow_req_orphan(struct us_loop_t *loop, struct us_internal_slow_poll_req *req); +static void us_internal_resume_list_add(struct us_loop_t *loop); +static void us_internal_resume_list_remove(struct us_loop_t *loop); +static void acceptors_cancel(struct us_loop_t *loop); +static void acceptors_arm_dispatching(struct us_loop_t *loop); + +/* The Winsock error a refused poll is reported as: the callers read errno as one. */ +static int afd_poll_refusal_error(NTSTATUS status) { + switch (status) { + case STATUS_INVALID_HANDLE: + case STATUS_OBJECT_TYPE_MISMATCH: + return WSAENOTSOCK; + case STATUS_ACCESS_DENIED: + return WSAEACCES; + case STATUS_INVALID_PARAMETER: + return WSAEINVAL; + default: + return WSAENOBUFS; + } +} + +/* 0, or -1 with the Winsock error set when the kernel refused the poll (no packet follows). */ +static int afd_poll_submit(struct us_internal_afd_poll *poll) { +#if defined(LIBUS_SOCKET_FAULT_INJECTION) && LIBUS_SOCKET_FAULT_INJECTION + ssize_t injected = 0; + int unused = 0; + if (US_FAULT_CHECK(US_FAULT_POLL_START, poll->socket, injected, unused)) { + return -1; + } +#endif + if (poll->slow) { + return slow_poll_submit(poll); + } + + ULONG events = afd_poll_wanted_events(poll); + poll->info.Timeout.QuadPart = INT64_MAX; + poll->info.NumberOfHandles = 1; + poll->info.Exclusive = FALSE; + poll->info.Handles[0].Handle = (HANDLE) poll->base_socket; + poll->info.Handles[0].Events = events; + poll->info.Handles[0].Status = 0; + poll->iosb.Status = STATUS_PENDING; + + NTSTATUS status = pNtDeviceIoControlFile(poll->helper->handle, NULL, NULL, &poll->op, &poll->iosb, IOCTL_AFD_POLL, &poll->info, sizeof(poll->info), &poll->info, sizeof(poll->info)); + if (status != STATUS_PENDING && !NT_SUCCESS(status)) { + WSASetLastError(afd_poll_refusal_error(status)); + return -1; + } + /* A poll that completed synchronously still queues its packet. */ + poll->state = AFD_POLL_STATE_PENDING; + poll->pending_events = events; + us_iocp_op_submitted(poll->loop); + return 0; +} + +/* The kernel refused the poll. Its owner hears of it the way it hears of a + * poll that failed in the kernel: from the op's completion. */ +static void afd_poll_refused(struct us_internal_afd_poll *poll) { + poll->iosb.Status = STATUS_UNSUCCESSFUL; + poll->state = AFD_POLL_STATE_PENDING; + us_iocp_op_submitted(poll->loop); + us_iocp_op_ready(poll->loop, &poll->op); +} + +static void afd_poll_cancel(struct us_internal_afd_poll *poll) { + if (poll->state != AFD_POLL_STATE_PENDING || poll->slow) { + return; + } + poll->state = AFD_POLL_STATE_CANCELLED; + poll->loop->afd_cancelled_polls++; + /* The result says nothing reliable: the poll can still complete normally + * after a successful cancel, and STATUS_NOT_FOUND means its packet is + * already queued. Either way exactly one packet arrives. */ + IO_STATUS_BLOCK cancel_iosb; + pNtCancelIoFileEx(poll->helper->handle, &poll->iosb, &cancel_iosb); +} + +static void afd_poll_set_interest(struct us_internal_afd_poll *poll, int interest) { + poll->interest = interest; + poll->reported_disconnect = 0; + poll->reported_abort = 0; + if (poll->slow || poll->state == AFD_POLL_STATE_IDLE) { + afd_poll_queue_update(poll); + } else if (poll->state == AFD_POLL_STATE_PENDING && (afd_poll_wanted_events(poll) & ~poll->pending_events)) { + /* An outstanding poll cannot be modified, and of two polls a thread + * has on one socket the second does not hear what happens after the + * first completed: cancel it and submit the wider mask from its + * completion. */ + afd_poll_cancel(poll); + } +} + +static struct us_internal_afd_poll *afd_poll_create(struct us_loop_t *loop, void *owner, SOCKET socket, int interest) { + if (!nt_ensure()) { + WSASetLastError(WSASYSNOTREADY); + return NULL; + } + + struct us_internal_afd_poll *poll = us_calloc(1, sizeof(struct us_internal_afd_poll)); + poll->op.complete = afd_poll_complete; + poll->owner = owner; + poll->loop = loop; + poll->socket = socket; + poll->interest = interest; + int not_a_socket; + poll->base_socket = afd_base_socket(socket, ¬_a_socket); + + if (not_a_socket) { + us_free(poll); + WSASetLastError(WSAENOTSOCK); + return NULL; + } + if (poll->base_socket == INVALID_SOCKET) { + poll->slow = 1; + } else { + poll->helper = afd_helper_acquire(loop); + if (!poll->helper) { + us_free(poll); + WSASetLastError(WSAEMFILE); + return NULL; + } + } + + if (afd_poll_submit(poll) != 0) { + int err = WSAGetLastError(); + afd_poll_free(poll); + WSASetLastError(err); + return NULL; + } + return poll; +} + +static void afd_poll_stop(struct us_internal_afd_poll *poll) { + poll->owner = NULL; + for (int i = 0; i < 2; i++) { + if (poll->slow_reqs[i]) { + slow_req_orphan(poll->loop, poll->slow_reqs[i]); + } + } + if (poll->state != AFD_POLL_STATE_IDLE || poll->slow_requests) { + afd_poll_cancel(poll); + return; + } + if (!poll->queued_for_update) { + afd_poll_free(poll); + } +} + +static void afd_poll_dispatch(struct us_loop_t *loop, struct us_internal_afd_poll *poll, int error, int eof, int events) { + events &= poll->interest; + if ((!events && !error && !eof) || loop->closing) { + return; + } + if (CLEAR_POINTER_TAG(poll->owner) != poll->owner) { + loop->current_ready_events = events; + loop->current_ready_error = error; + loop->current_ready_eof = eof; + Bun__internal_dispatch_ready_poll(loop, poll->owner); + return; + } + us_internal_dispatch_ready_poll((struct us_poll_t *) poll->owner, error, eof, events); +} + +/* Windows does not reliably latch a received RST in SO_ERROR (POSIX does); + * the reset surfaces on the next I/O. A zero-byte send observes it without + * touching the stream: 0 on a healthy socket, SOCKET_ERROR with a fatal + * code once the connection died hard. */ +int us_internal_peer_reset_probe(LIBUS_SOCKET_DESCRIPTOR fd) { + if (send(fd, "", 0, 0) != SOCKET_ERROR) { + return 0; + } + int err = WSAGetLastError(); + /* WSAESHUTDOWN means our own shutdown(SD_SEND) ran; that is not a peer reset. */ + return err != WSAEWOULDBLOCK && err != WSAESHUTDOWN; +} + +static void afd_poll_report(struct us_loop_t *loop, struct us_internal_afd_poll *poll, ULONG afd_events) { + int events = 0; + int error = 0; + int eof = 0; + + if (afd_events & (AFD_POLL_RECEIVE | AFD_POLL_ACCEPT)) { + events |= LIBUS_SOCKET_READABLE; + } + if (afd_events & (AFD_POLL_SEND | AFD_POLL_CONNECT_FAIL)) { + events |= LIBUS_SOCKET_WRITABLE; + } + /* What epoll reports as EPOLLERR: the connecting path reads the cause from SO_ERROR. */ + if (afd_events & AFD_POLL_CONNECT_FAIL) { + error = 1; + } + + if (afd_events & (AFD_POLL_DISCONNECT | AFD_POLL_ABORT)) { + if (afd_events & AFD_POLL_DISCONNECT) poll->reported_disconnect = 1; + if (afd_events & AFD_POLL_ABORT) poll->reported_abort = 1; + + int kind = afd_poll_owner_kind(poll); + int reading = poll->interest & LIBUS_SOCKET_READABLE; + if (kind == POLL_TYPE_SOCKET_SHUT_DOWN) { + /* With our write side shut down no readable event follows the + * peer's FIN, and no data-bearing flow is left that an early EOF + * could truncate. Everywhere else recv() owns EOF discovery, + * because DISCONNECT can be raised while data is still queued. */ + eof = LIBUS_POLL_EOF; + events |= reading; + } else if (kind == POLL_TYPE_SOCKET && !reading) { + /* Paused, or half-open with its end already delivered. Reading + * here would pull bytes a paused caller deferred, or rediscover + * the same EOF. A reset closes the socket through the error path; + * a FIN waits for resume(), whose interest change asks for + * DISCONNECT again. */ + struct us_socket_t *s = us_internal_socket_follow_adopted((struct us_socket_t *) poll->owner); + if (!s->flags.is_closed && ((afd_events & AFD_POLL_ABORT) || us_socket_get_error(s) != 0 || us_internal_peer_reset_probe(poll->socket))) { + error = 1; + } + } else if (kind == -1 && !reading) { + /* What epoll tells a Bun-owned poll unasked as EPOLLERR/EPOLLHUP. */ + error = (afd_events & AFD_POLL_ABORT) != 0; + eof = (afd_events & AFD_POLL_DISCONNECT) != 0; + } else { + /* recv()/accept() reports what happened. */ + events |= LIBUS_SOCKET_READABLE; + } + } + + afd_poll_dispatch(loop, poll, error, eof, events); +} + +static void afd_poll_complete(struct us_loop_t *loop, struct us_iocp_op *op) { + struct us_internal_afd_poll *poll = (struct us_internal_afd_poll *) op; + if (poll->state == AFD_POLL_STATE_CANCELLED) { + loop->afd_cancelled_polls--; + } + poll->state = AFD_POLL_STATE_IDLE; + + if (!poll->owner) { + if (!poll->slow_requests && !poll->queued_for_update) { + afd_poll_free(poll); + } + return; + } + + NTSTATUS status = poll->iosb.Status; + if (status == STATUS_CANCELLED) { + /* Cancelled to widen its mask. */ + if (!loop->closing && afd_poll_submit(poll) != 0) { + afd_poll_refused(poll); + } + return; + } + if (!NT_SUCCESS(status)) { + afd_poll_dispatch(loop, poll, 1, 0, 0); + return; + } + + ULONG afd_events = poll->info.NumberOfHandles >= 1 ? poll->info.Handles[0].Events : 0; + if (afd_events & AFD_POLL_LOCAL_CLOSE) { + /* The socket was closed without stopping its poll first. Nothing is left to report or re-arm. */ + return; + } + + /* Re-armed by the flush before the next wait, after the callbacks of this + * batch ran: arming before the owner consumes the data completes again at + * once for the same data. */ + afd_poll_queue_update(poll); + afd_poll_report(loop, poll, afd_events); +} + +static void afd_flush_updates(struct us_loop_t *loop) { + while (loop->afd_update_head) { + struct us_internal_afd_poll *poll = loop->afd_update_head; + loop->afd_update_head = poll->update_next; + if (!loop->afd_update_head) { + loop->afd_update_tail = NULL; + } + poll->update_next = NULL; + poll->queued_for_update = 0; + + if (!poll->owner) { + if (poll->state == AFD_POLL_STATE_IDLE && !poll->slow_requests) { + afd_poll_free(poll); + } + continue; + } + if (poll->state != AFD_POLL_STATE_IDLE || loop->closing) { + continue; + } + if (poll->slow && poll->slow_requests && poll->slow_submitted_interest == poll->interest) { + continue; + } + if (afd_poll_submit(poll) != 0) { + afd_poll_refused(poll); + } + } +} + +/* select() fallback for sockets whose provider chain does not end at AFD. */ + +/* Takes `req` off the loop's list and its poll's. Only pointers are compared + * with `req`: it may be the thread's already. */ +static void slow_req_unlink(struct us_loop_t *loop, struct us_internal_afd_poll *poll, struct us_internal_slow_poll_req *req, struct us_internal_slow_poll_req *prev, struct us_internal_slow_poll_req *next) { + if (prev) { + prev->next = next; + } else { + loop->slow_reqs = next; + } + if (next) { + next->prev = prev; + } + if (poll->slow_reqs[0] == req) { + poll->slow_reqs[0] = NULL; + } else { + poll->slow_reqs[1] = NULL; + } + poll->slow_requests--; +} + +/* The select() behind `req` cannot be interrupted. Unless its answer is + * already on its way to the port, the request becomes its thread's to free + * and the loop stops counting it. */ +static void slow_req_orphan(struct us_loop_t *loop, struct us_internal_slow_poll_req *req) { + struct us_internal_afd_poll *poll = req->poll; + struct us_internal_slow_poll_req *prev = req->prev; + struct us_internal_slow_poll_req *next = req->next; + if (InterlockedCompareExchange(&req->state, SLOW_REQ_ORPHANED, SLOW_REQ_PENDING) != SLOW_REQ_PENDING) { + return; + } + /* `req` is the thread's from here on. */ + slow_req_unlink(loop, poll, req, prev, next); + us_iocp_op_abandoned(loop); +} + +static void slow_poll_complete(struct us_loop_t *loop, struct us_iocp_op *op) { + struct us_internal_slow_poll_req *req = (struct us_internal_slow_poll_req *) op; + struct us_internal_afd_poll *poll = req->poll; + int events = req->result_events; + int error = req->result_error; + int except = req->result_except; + int stale = req->interest != poll->interest; + slow_req_unlink(loop, poll, req, req->prev, req->next); + us_free(req); + + if (!poll->owner) { + if (!poll->slow_requests && !poll->queued_for_update) { + afd_poll_free(poll); + } + return; + } + if (except) { + /* A connect that failed, which AFD reports as AFD_POLL_CONNECT_FAIL. + * On a connected socket it is out-of-band data. */ + int kind = afd_poll_owner_kind(poll); + events |= LIBUS_SOCKET_WRITABLE; + error |= kind == POLL_TYPE_SEMI_SOCKET || kind == -1; + } + afd_poll_queue_update(poll); + if (!stale || error) { + afd_poll_dispatch(loop, poll, error, 0, events); + } +} + +static DWORD WINAPI slow_poll_thread(LPVOID param) { + struct us_internal_slow_poll_req *req = param; + fd_set rfds, wfds, efds; + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&efds); + if (req->interest & LIBUS_SOCKET_READABLE) { + FD_SET(req->socket, &rfds); + } + if (req->interest & LIBUS_SOCKET_WRITABLE) { + FD_SET(req->socket, &wfds); + FD_SET(req->socket, &efds); + } + /* Bounded so a request whose interest went away is eventually reclaimed; closing the socket ends it at once. */ + struct timeval timeout = {.tv_sec = 3 * 60, .tv_usec = 0}; + int rc = select(0, &rfds, &wfds, &efds, &timeout); + if (rc == SOCKET_ERROR) { + req->result_error = 1; + } else if (rc > 0) { + if (FD_ISSET(req->socket, &rfds)) req->result_events |= LIBUS_SOCKET_READABLE; + if (FD_ISSET(req->socket, &wfds)) req->result_events |= LIBUS_SOCKET_WRITABLE; + if (FD_ISSET(req->socket, &efds)) req->result_except = 1; + } + HANDLE port = req->port; + if (InterlockedCompareExchange(&req->state, SLOW_REQ_POSTED, SLOW_REQ_PENDING) == SLOW_REQ_PENDING) { + /* The completion frees `req`. */ + PostQueuedCompletionStatus(port, 0, 0, &req->op.overlapped); + } else { + us_free(req); + } + CloseHandle(port); + return 0; +} + +static int slow_poll_submit(struct us_internal_afd_poll *poll) { + /* select() reports nothing but readable and writable, and fails at once + * when given no socket at all. A change of interest submits again. */ + if (!(poll->interest & (LIBUS_SOCKET_READABLE | LIBUS_SOCKET_WRITABLE))) { + return 0; + } + /* Retried from the next completion. */ + if (poll->slow_requests >= 2) { + return 0; + } + struct us_internal_slow_poll_req *req = us_calloc(1, sizeof(struct us_internal_slow_poll_req)); + req->op.complete = slow_poll_complete; + req->poll = poll; + req->socket = poll->socket; + req->interest = poll->interest; + if (!DuplicateHandle(GetCurrentProcess(), poll->loop->iocp, GetCurrentProcess(), &req->port, 0, FALSE, DUPLICATE_SAME_ACCESS)) { + us_free(req); + WSASetLastError(WSAENOBUFS); + return -1; + } + if (!QueueUserWorkItem(slow_poll_thread, req, WT_EXECUTELONGFUNCTION)) { + CloseHandle(req->port); + us_free(req); + WSASetLastError(WSAENOBUFS); + return -1; + } + req->next = poll->loop->slow_reqs; + if (req->next) { + req->next->prev = req; + } + poll->loop->slow_reqs = req; + poll->slow_reqs[poll->slow_reqs[0] ? 1 : 0] = req; + poll->slow_requests++; + poll->slow_submitted_interest = poll->interest; + us_iocp_op_submitted(poll->loop); + return 0; +} + +/* Socket readiness for polls Bun owns */ + +struct us_internal_afd_poll *us_iocp_poll_socket(struct us_loop_t *loop, void *owner, SOCKET socket, int events) { + return afd_poll_create(loop, owner, socket, events); +} + +void us_iocp_poll_socket_change(struct us_loop_t *loop, struct us_internal_afd_poll *poll, int events) { + (void) loop; + afd_poll_set_interest(poll, events); +} + +void us_iocp_poll_socket_stop(struct us_loop_t *loop, struct us_internal_afd_poll *poll) { + (void) loop; + afd_poll_stop(poll); +} + +/* Waiting on a handle */ + +struct us_iocp_wait { + struct us_loop_t *loop; + /* Wait completion packet, or NULL when the thread-pool fallback is used. */ + HANDLE packet; + /* The fallback's own handle to the port: its callback runs on a + * thread-pool thread, which the loop's teardown does not wait for. */ + HANDLE port; + HANDLE registered_wait; + struct us_iocp_op *op; + volatile LONG fired; +}; + +struct us_iocp_wait *us_iocp_wait_create(struct us_loop_t *loop) { + nt_ensure(); + struct us_iocp_wait *wait = us_calloc(1, sizeof(struct us_iocp_wait)); + wait->loop = loop; + if (pNtCreateWaitCompletionPacket) { + if (!NT_SUCCESS(pNtCreateWaitCompletionPacket(&wait->packet, GENERIC_ALL, NULL))) { + wait->packet = NULL; + } + } + if (!wait->packet && !DuplicateHandle(GetCurrentProcess(), loop->iocp, GetCurrentProcess(), &wait->port, 0, FALSE, DUPLICATE_SAME_ACCESS)) { + us_free(wait); + return NULL; + } + return wait; +} + +static VOID CALLBACK iocp_wait_fired(PVOID context, BOOLEAN timed_out) { + (void) timed_out; + struct us_iocp_wait *wait = context; + InterlockedExchange(&wait->fired, 1); + PostQueuedCompletionStatus(wait->port, 0, 0, &wait->op->overlapped); +} + +int us_iocp_wait_start(struct us_iocp_wait *wait, HANDLE handle, struct us_iocp_op *op) { + wait->op = op; + if (wait->packet) { + /* An already-signalled handle sets `signalled` and queues the packet all the same. */ + BOOLEAN signalled = FALSE; + NTSTATUS status = pNtAssociateWaitCompletionPacket(wait->packet, wait->loop->iocp, handle, NULL, op, STATUS_SUCCESS, 0, &signalled); + if (!NT_SUCCESS(status)) { + return -1; + } + us_iocp_op_submitted(wait->loop); + return 0; + } + + /* A one-shot registration stays allocated after it fired. Its callback only posts, so this does not block for long. */ + if (wait->registered_wait) { + UnregisterWaitEx(wait->registered_wait, INVALID_HANDLE_VALUE); + wait->registered_wait = NULL; + } + wait->fired = 0; + if (!RegisterWaitForSingleObject(&wait->registered_wait, handle, iocp_wait_fired, wait, INFINITE, WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE)) { + wait->registered_wait = NULL; + return -1; + } + us_iocp_op_submitted(wait->loop); + return 0; +} + +int us_iocp_wait_stop(struct us_iocp_wait *wait) { + if (wait->packet) { + /* With the loop thread as the port's only reader this is STATUS_SUCCESS + * when the wait was armed or its packet was still queued (the packet is + * removed), and STATUS_CANCELLED when the packet was already dequeued. */ + if (pNtCancelWaitCompletionPacket(wait->packet, TRUE) == STATUS_SUCCESS) { + InterlockedDecrement((volatile LONG *) &wait->loop->pending_ops); + return 1; + } + return 0; + } + + if (!wait->registered_wait) { + return 0; + } + /* Blocks until a running callback returns; the callback only posts to the port. */ + UnregisterWaitEx(wait->registered_wait, INVALID_HANDLE_VALUE); + wait->registered_wait = NULL; + if (wait->fired) { + return 0; + } + InterlockedDecrement((volatile LONG *) &wait->loop->pending_ops); + return 1; +} + +void us_iocp_wait_free(struct us_iocp_wait *wait) { + if (wait->packet) { + CloseHandle(wait->packet); + } + if (wait->registered_wait) { + UnregisterWaitEx(wait->registered_wait, INVALID_HANDLE_VALUE); + } + if (wait->port) { + CloseHandle(wait->port); + } + us_free(wait); +} + +/* Loop */ + +struct us_loop_t *us_create_loop(void (*wakeup_cb)(struct us_loop_t *loop), void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop), unsigned int ext_size) { + if (!nt_ensure()) { + return NULL; + } + + struct us_loop_t *loop = (struct us_loop_t *) us_calloc(1, sizeof(struct us_loop_t) + ext_size); + + /* One thread runs the loop; the concurrency value only matters with several readers. */ + loop->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1); + if (loop->iocp == NULL) { + us_free(loop); + return NULL; + } + + /* A wait timeout is otherwise rounded to the 15.6ms scheduler tick. */ + if (pNtCreateWaitCompletionPacket) { + loop->hrtimer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); + if (loop->hrtimer && !NT_SUCCESS(pNtCreateWaitCompletionPacket(&loop->hrtimer_packet, GENERIC_ALL, NULL))) { + CloseHandle(loop->hrtimer); + loop->hrtimer = NULL; + loop->hrtimer_packet = NULL; + } + } + + if (us_internal_loop_data_init(loop, wakeup_cb, pre_cb, post_cb) != 0) { + if (loop->hrtimer) { + CloseHandle(loop->hrtimer_packet); + CloseHandle(loop->hrtimer); + } + CloseHandle(loop->iocp); + us_free(loop); + return NULL; + } + us_internal_resume_list_add(loop); + return loop; +} + +/* System resume. A wait's timeout does not advance while the machine sleeps, + * but the deadlines it was computed from (QueryPerformanceCounter) do: a loop + * that slept through a suspend would fire its timers late by the length of + * the suspend. An empty packet ends the wait and the caller works out its + * timers again. */ + +#ifndef PBT_APMRESUMESUSPEND +#define PBT_APMRESUMESUSPEND 0x0007 +#endif +#ifndef PBT_APMRESUMEAUTOMATIC +#define PBT_APMRESUMEAUTOMATIC 0x0012 +#endif +#ifndef DEVICE_NOTIFY_CALLBACK +#define DEVICE_NOTIFY_CALLBACK 2 +#endif + +struct us_internal_power_notify { + ULONG(CALLBACK *callback)(PVOID context, ULONG type, PVOID setting); + PVOID context; +}; +typedef DWORD(WINAPI *power_register_suspend_resume_fn)(DWORD flags, HANDLE recipient, PVOID *registration); + +static SRWLOCK resume_lock = SRWLOCK_INIT; +static struct us_loop_t *resume_loops; +static INIT_ONCE resume_once = INIT_ONCE_STATIC_INIT; + +static ULONG CALLBACK us_internal_on_power_event(PVOID context, ULONG type, PVOID setting) { + (void) context; + (void) setting; + if (type == PBT_APMRESUMESUSPEND || type == PBT_APMRESUMEAUTOMATIC) { + AcquireSRWLockShared(&resume_lock); + for (struct us_loop_t *loop = resume_loops; loop; loop = loop->resume_next) { + PostQueuedCompletionStatus(loop->iocp, 0, 0, NULL); + } + ReleaseSRWLockShared(&resume_lock); + } + return 0; +} + +static BOOL CALLBACK us_internal_resume_register(PINIT_ONCE once, PVOID param, PVOID *context) { + (void) once; + (void) param; + (void) context; + HMODULE powrprof = LoadLibraryExW(L"powrprof.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (powrprof) { + power_register_suspend_resume_fn reg = (power_register_suspend_resume_fn) (void *) GetProcAddress(powrprof, "PowerRegisterSuspendResumeNotification"); + if (reg) { + static struct us_internal_power_notify recipient = {us_internal_on_power_event, NULL}; + PVOID registration; + reg(DEVICE_NOTIFY_CALLBACK, &recipient, ®istration); + } + } + return TRUE; +} + +static void us_internal_resume_list_add(struct us_loop_t *loop) { + AcquireSRWLockExclusive(&resume_lock); + loop->resume_next = resume_loops; + if (resume_loops) { + resume_loops->resume_prev = loop; + } + resume_loops = loop; + ReleaseSRWLockExclusive(&resume_lock); +} + +static void us_internal_resume_list_remove(struct us_loop_t *loop) { + AcquireSRWLockExclusive(&resume_lock); + if (loop->resume_prev) { + loop->resume_prev->resume_next = loop->resume_next; + } else { + resume_loops = loop->resume_next; + } + if (loop->resume_next) { + loop->resume_next->resume_prev = loop->resume_prev; + } + ReleaseSRWLockExclusive(&resume_lock); +} + +/* A tick looks at the port before it runs its first callback and not again: + * whatever it delivers was there before anything it did, so none of it can be + * an answer to that. A caller that leaves the loop's callbacks and comes back + * (microtasks between ticks, a nested wait for a promise) relies on it. */ + +/* A dequeued packet exists nowhere else, and a callback can re-enter the loop + * with part of its batch undispatched: the nested tick's batch begins with it. */ +static void us_internal_begin_batch(struct us_loop_t *loop) { + const int held = loop->num_ready_polls - loop->current_ready_poll; + if (held > 0 && loop->current_ready_poll > 0) { + memmove(loop->ready_polls, loop->ready_polls + loop->current_ready_poll, (size_t) held * sizeof(OVERLAPPED_ENTRY)); + } + loop->num_ready_polls = held; + loop->current_ready_poll = 0; +} + +/* The entry is copied and the index moved past it before its callback runs: + * a nested tick moves the rest of the batch. */ +static void us_internal_dispatch_ready_polls(struct us_loop_t *loop) { + while (loop->current_ready_poll < loop->num_ready_polls) { + OVERLAPPED_ENTRY entry = loop->ready_polls[loop->current_ready_poll++]; + /* A packet without an OVERLAPPED only ends the wait: the wait timer fired, or + * the system resumed. A relative timer does not count the time suspended, so + * after a resume the armed one is later than the deadline it was armed for. */ + if (!entry.lpOverlapped) { + loop->hrtimer_deadline_ns = 0; + if (entry.lpCompletionKey == HRTIMER_COMPLETION_KEY) { + loop->hrtimer_packet_out = 0; + } + continue; + } + InterlockedDecrement((volatile LONG *) &loop->pending_ops); + struct us_iocp_op *op = (struct us_iocp_op *) entry.lpOverlapped; + op->complete(loop, op); + } +} + +/* Completes the ops that were ready when the tick began; one that becomes ready + * meanwhile is the next tick's. Each is taken off the list before it runs: a + * callback can re-enter the loop, and the nested tick carries on with the rest. */ +static void us_internal_complete_ready_ops(struct us_loop_t *loop) { + for (unsigned int budget = loop->num_ready_ops; budget && loop->ready_ops_head; budget--) { + struct us_iocp_op *op = loop->ready_ops_head; + loop->ready_ops_head = op->next_ready; + if (!loop->ready_ops_head) { + loop->ready_ops_tail = NULL; + } + loop->num_ready_ops--; + InterlockedDecrement((volatile LONG *) &loop->pending_ops); + op->complete(loop, op); + } +} + +/* Adds what is on the port to the batch. */ +static void us_internal_iocp_dequeue(struct us_loop_t *loop, DWORD timeout_ms) { + const int room = US_IOCP_MAX_ENTRIES - loop->num_ready_polls; + if (room == 0) { + return; + } + ULONG count = 0; + if (!GetQueuedCompletionStatusEx(loop->iocp, loop->ready_polls + loop->num_ready_polls, (ULONG) room, &count, timeout_ms, FALSE)) { + count = 0; + } + loop->num_ready_polls += (int) count; +} + +/* A poll cancelled to widen its mask goes back to the kernel as soon as its + * packet is dequeued, and the port is read once more behind it: what the wider + * poll already has to report is part of this batch. Returns the polls resubmitted + * among the entries from `from` on, which it takes out of the batch. */ +static int us_internal_resubmit_cancelled_polls(struct us_loop_t *loop, int from) { + int resubmitted = 0; + int kept = from; + for (int i = from; i < loop->num_ready_polls; i++) { + struct us_iocp_op *op = (struct us_iocp_op *) loop->ready_polls[i].lpOverlapped; + if (op && op->complete == afd_poll_complete) { + struct us_internal_afd_poll *poll = (struct us_internal_afd_poll *) op; + if (poll->owner && poll->state == AFD_POLL_STATE_CANCELLED && poll->iosb.Status == STATUS_CANCELLED) { + InterlockedDecrement((volatile LONG *) &loop->pending_ops); + afd_poll_complete(loop, op); + resubmitted++; + continue; + } + } + loop->ready_polls[kept++] = loop->ready_polls[i]; + } + loop->num_ready_polls = kept; + return resubmitted; +} + +/* Ends the tick's look at the port. No callback has run yet. */ +static void us_internal_complete_batch(struct us_loop_t *loop, int from) { + while (loop->afd_cancelled_polls && us_internal_resubmit_cancelled_polls(loop, from)) { + from = loop->num_ready_polls; + us_internal_iocp_dequeue(loop, 0); + } +} + +/* timeout_ns < 0 waits forever. Nothing in here touches the heap: the tick + * calls it after handing this thread's heaps to the mimalloc scavenger. */ +static void us_internal_iocp_wait(struct us_loop_t *loop, long long timeout_ns, uint64_t now_ns) { + DWORD timeout_ms; + uint64_t deadline_ns = 0; + if (timeout_ns < 0) { + timeout_ms = INFINITE; + } else if (timeout_ns == 0) { + timeout_ms = 0; + } else if (loop->hrtimer) { + /* `timeout_ns` counts from the reading the caller took to pick it, so + * for one pending timer `due_ns` is the same value tick after tick. + * A caller without a reading counts from here. */ + const uint64_t armed_at_ns = us_internal_monotonic_ns(); + const uint64_t due_ns = (now_ns ? now_ns : armed_at_ns) + (uint64_t) timeout_ns; + if (loop->hrtimer_deadline_ns && loop->hrtimer_deadline_ns <= due_ns) { + /* The armed timer ends this wait in time. If it is early, the tick + * it ends finds nothing due and the next one arms again; arming on + * every wait is three syscalls. */ + timeout_ms = INFINITE; + } else if (due_ns <= armed_at_ns) { + timeout_ms = 0; + } else { + /* Cancel, set, associate, in this order: a packet from the previous + * expiry is removed before the timer can fire again. */ + if (loop->hrtimer_packet_out) { + pNtCancelWaitCompletionPacket(loop->hrtimer_packet, TRUE); + } + LARGE_INTEGER due; + due.QuadPart = -(LONGLONG) ((due_ns - armed_at_ns + 99) / 100); + BOOLEAN signalled = FALSE; + if (SetWaitableTimer(loop->hrtimer, &due, 0, NULL, NULL, FALSE) && + NT_SUCCESS(pNtAssociateWaitCompletionPacket(loop->hrtimer_packet, loop->iocp, loop->hrtimer, (PVOID) HRTIMER_COMPLETION_KEY, NULL, STATUS_SUCCESS, 0, &signalled))) { + loop->hrtimer_deadline_ns = due_ns; + loop->hrtimer_packet_out = 1; + timeout_ms = INFINITE; + } else { + loop->hrtimer_deadline_ns = 0; + loop->hrtimer_packet_out = 0; + uint64_t ms = ((uint64_t) timeout_ns + 999999ULL) / 1000000ULL; + timeout_ms = ms >= INFINITE ? INFINITE - 1 : (DWORD) ms; + } + } + } else { + uint64_t ms = ((uint64_t) timeout_ns + 999999ULL) / 1000000ULL; + timeout_ms = ms >= INFINITE ? INFINITE - 1 : (DWORD) ms; + deadline_ns = us_internal_monotonic_ns() + (uint64_t) timeout_ns; + } + + for (;;) { + us_internal_iocp_dequeue(loop, timeout_ms); + if (loop->num_ready_polls != 0 || !deadline_ns) { + return; + } + /* A millisecond timeout can return up to a scheduler tick early. */ + uint64_t now = us_internal_monotonic_ns(); + if (now >= deadline_ns) { + return; + } + timeout_ms = (DWORD) ((deadline_ns - now + 999999ULL) / 1000000ULL); + } +} + +/* The Rust callers pass two int64_t; tv_nsec is a 32-bit long here. */ +_Static_assert(sizeof(struct timespec) == 16 && offsetof(struct timespec, tv_nsec) == 8, "struct timespec layout"); + +static long long us_internal_timespec_ns(const struct timespec *timeout) { + if (!timeout) { + return -1; + } + /* Only NULL waits forever: epoll_pwait2/kevent reject a negative timespec rather than block on it. */ + long long ns = (long long) timeout->tv_sec * 1000000000LL + (long long) timeout->tv_nsec; + return ns < 0 ? 0 : ns; +} + +/* Bound `timeout_ns` by the socket-timeout sweep deadline. */ +static long long us_internal_clamp_to_sweep(struct us_loop_t *loop, long long timeout_ns) { + long long sweep_ns = us_internal_sweep_timeout_ns(loop); + if (sweep_ns < 0) { + return timeout_ns; + } + if (timeout_ns >= 0 && timeout_ns <= sweep_ns) { + return timeout_ns; + } + return sweep_ns; +} + +void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec *timeout, uint64_t now_ns) { + /* An unref'd poll still counts in num_polls on every backend, so a tick + * still collects its events. Here the same goes for anything in flight on + * the port that is not a poll (a process-exit wait, a pipe read): with + * only those left, the caller's non-blocking tick must still dequeue them. */ + if (loop->num_polls == 0 && loop->pending_ops == 0 && !loop->starved) + return; + + loop->data.tick_depth++; + + us_internal_loop_pre(loop); + us_internal_retry_starved(loop); + if (loop->data.tick_depth > 1) { + acceptors_arm_dispatching(loop); + } + + /* Only a tick entered from a completion callback has anything left here. */ + us_internal_begin_batch(loop); + const int held_packets = loop->num_ready_polls; + + long long timeout_ns = us_internal_timespec_ns(timeout); + + /* loop_pre runs lsquic_engine_process_conns and stores the soonest + * earliest_adv_tick. The JS event loop folds this in itself; other callers + * (HTTP thread) pass NULL, so fold it here so QUIC retransmit/idle timers + * fire without other I/O waking us. */ + if (loop->data.quic_head && loop->data.quic_next_tick_us >= 0) { + long long quic_ns = loop->data.quic_next_tick_us * 1000LL; + if (timeout_ns < 0 || timeout_ns > quic_ns) { + timeout_ns = quic_ns; + } + } + + timeout_ns = us_internal_clamp_to_sweep(loop, timeout_ns); + + int released_heap_access = 0; + if (timeout_ns != 0 && !loop->ready_ops_head && !held_packets && loop->data.jsc_vm) + Bun__JSC_onBeforeWait(loop->data.jsc_vm, &released_heap_access); + + /* After the finalizers above, which stop polls: the flush frees them. + * Before the hand-off below for the same reason. */ + afd_flush_updates(loop); + + /* No packet stands behind either: nothing would end a wait for them. The + * flush adds to the ready list the polls the kernel refused. */ + if (loop->ready_ops_head || held_packets) + timeout_ns = 0; + + /* The scavenger sweeps our heaps while we are in the kernel. The hand-off wakes its + * thread (a syscall), and it sweeps one thread's heaps once in this long at most + * (mi_option_purge_holes_min_interval): a loop that parks more often than that hands + * them over that often. A park in between lasts until the next one is due at the + * longest, so heaps left by a burst are swept however long the quiet after it. */ + static const uint64_t idle_handoff_interval_ns = 100 * 1000000ULL; + static _Thread_local uint64_t idle_handoff_due_ns = 0; + int found_packets = 0; + int handed_off = 0; + if (timeout_ns != 0) { + const uint64_t idle_now_ns = now_ns ? now_ns : us_internal_monotonic_ns(); + if (idle_now_ns < idle_handoff_due_ns) { + const uint64_t left_ns = idle_handoff_due_ns - idle_now_ns; + if (timeout_ns < 0 || (uint64_t) timeout_ns > left_ns) + timeout_ns = (long long) left_ns; + } else { + /* Only on a tick that really parks: one that finds packets waiting takes the + * heaps back before they could be swept. */ + us_internal_iocp_dequeue(loop, 0); + found_packets = loop->num_ready_polls != 0; + if (!found_packets) { + idle_handoff_due_ns = idle_now_ns + idle_handoff_interval_ns; + /* Not at startup: this loads powrprof.dll, and only a wait that blocks needs it. */ + InitOnceExecuteOnce(&resume_once, us_internal_resume_register, NULL, NULL); + /* Must come after Bun__JSC_onBeforeWait, which allocates: nothing may touch + * our heaps until the matching _end. With no scavenger to hand off to, fall + * back to sweeping inline, rate-limited. */ + handed_off = mi_on_thread_idle_start(); + if (!handed_off) + us_internal_idle_sweep(now_ns); + } + } + } + + if (!found_packets) + us_internal_iocp_wait(loop, timeout_ns, now_ns); + + /* Before anything can allocate again. */ + if (handed_off) + mi_on_thread_idle_end(); + /* Before anything touches the JS heap again (this may run a finished collection's epilogue, i.e. destructors). */ + if (released_heap_access) + Bun__JSC_acquireHeapAccessAfterWait(loop->data.jsc_vm); + + us_internal_complete_batch(loop, held_packets); + + us_internal_complete_ready_ops(loop); + us_internal_dispatch_ready_polls(loop); + us_internal_sweep_if_due(loop); + + us_internal_loop_post(loop); + loop->data.tick_depth--; +} + +void us_loop_free(struct us_loop_t *loop) { + Bun__closeAllForLoop(loop); + + /* A poll whose owner never stopped it must not run that owner from the + * drain below, nor be re-armed by it. Cancelled here, its packet comes + * back at once. */ + loop->closing = 1; + us_internal_resume_list_remove(loop); + for (struct us_internal_afd_helper *helper = loop->afd_helpers; helper; helper = helper->next) { + IO_STATUS_BLOCK cancel_iosb; + pNtCancelIoFileEx(helper->handle, NULL, &cancel_iosb); + } + acceptors_cancel(loop); + /* Nothing waits for a select(): a request whose answer is not already on + * its way to the port becomes its thread's. */ + for (struct us_internal_slow_poll_req *req = loop->slow_reqs, *next; req; req = next) { + next = req->next; + struct us_internal_afd_poll *poll = req->poll; + slow_req_orphan(loop, req); + if (!poll->owner && !poll->slow_requests && poll->state == AFD_POLL_STATE_IDLE && !poll->queued_for_update) { + afd_poll_free(poll); + } + } + + us_internal_loop_data_free(loop); + + if (loop->hrtimer) { + pNtCancelWaitCompletionPacket(loop->hrtimer_packet, TRUE); + CloseHandle(loop->hrtimer_packet); + CloseHandle(loop->hrtimer); + } + + /* The memory of an op belongs to the kernel until its packet is dequeued. + * Every op the loop counts was cancelled, by its owner or above, and the + * packet of a cancelled op comes. What a thread that cannot be stopped + * has is not counted: that thread frees it. */ + for (;;) { + afd_flush_updates(loop); + us_internal_complete_ready_ops(loop); + if (loop->pending_ops == 0) { + break; + } + us_internal_begin_batch(loop); + us_internal_iocp_dequeue(loop, INFINITE); + us_internal_dispatch_ready_polls(loop); + } + + while (loop->afd_helpers) { + struct us_internal_afd_helper *next = loop->afd_helpers->next; + if (loop->afd_helpers->count == 0) { + CloseHandle(loop->afd_helpers->handle); + us_free(loop->afd_helpers); + } + loop->afd_helpers = next; + } + + CloseHandle(loop->iocp); + us_free(loop); +} + +/* Accepting */ + +/* A listening socket is not polled for readiness where AcceptEx works on it (see + * acceptor_create). accept() on a non-blocking socket is a readiness check + * followed by a wait for the connection, and when several acceptors share the + * socket (cluster workers, the loops of two threads) another one can take the + * connection in between, which leaves this thread blocked in that wait. An + * overlapped AcceptEx is handed a connection by the kernel or stays pending, + * however many acceptors there are. */ + +#define US_ACCEPT_ADDRESS_LENGTH ((DWORD) (sizeof(struct sockaddr_storage) + 16)) + +/* One AcceptEx is outstanding per listener. The kernel owns its OVERLAPPED and + * buffer until it completes, so this is freed only once the listener stopped + * and none is in flight. + * + * Each AcceptEx carries an event with its low bit set, which keeps its + * completion off whatever port the listener's file object belongs to (a file + * object has one, for good and for every process that holds the socket), and a + * wait on the event delivers it here. A call that finds a connection waiting + * has completed by the time it returns, still reporting that it is pending: the + * OVERLAPPED says so. The owner accepts until a call is really pending, which + * takes the whole backlog in one dispatch, as accept() until EWOULDBLOCK does + * on the other backends. */ +struct us_internal_acceptor { + struct us_iocp_op op; + struct us_poll_t *owner; + struct us_loop_t *loop; + SOCKET listener; + int family; + int type; + int protocol; + LPFN_ACCEPTEX accept_ex; + LPFN_GETACCEPTEXSOCKADDRS get_addresses; + HANDLE event; + struct us_iocp_wait *wait; + /* Handed to the outstanding AcceptEx, or accepted and not taken yet. */ + SOCKET socket; + /* A completion is on its way: the event's wait, or the op is on the ready list. */ + unsigned char in_flight; + unsigned char accepted; + /* AcceptEx could not be started: `retry` is linked. */ + unsigned char starved; + struct us_iocp_starved retry; + /* Depth of acceptor_complete frames dispatching to the owner: a + * callback can run the loop again, and close the listener from there. */ + unsigned int dispatching; + struct us_internal_acceptor *next; + struct us_internal_acceptor *prev; + char addresses[2 * US_ACCEPT_ADDRESS_LENGTH]; +}; + +static void acceptor_set_starved(struct us_internal_acceptor *a, int starved) { + if (a->starved == starved) { + return; + } + a->starved = (unsigned char) starved; + if (starved) { + us_iocp_starved_link(a->loop, &a->retry); + } else { + us_iocp_starved_unlink(a->loop, &a->retry); + } +} + +static void acceptor_maybe_free(struct us_internal_acceptor *a) { + if (a->owner || a->in_flight || a->dispatching) { + return; + } + acceptor_set_starved(a, 0); + if (a->socket != INVALID_SOCKET) { + closesocket(a->socket); + } + if (a->wait) { + us_iocp_wait_free(a->wait); + } + if (a->event) { + CloseHandle(a->event); + } + if (a->prev) { + a->prev->next = a->next; + } else { + a->loop->acceptors = a->next; + } + if (a->next) { + a->next->prev = a->prev; + } + us_free(a); +} + +enum { + US_ACCEPT_FAILED = -1, + /* A completion is on its way. */ + US_ACCEPT_PENDING = 0, + /* The call has completed (its status is in the OVERLAPPED), and nothing is on its way. */ + US_ACCEPT_TAKEN = 1, +}; + +static int acceptor_start(struct us_internal_acceptor *a) { + a->socket = WSASocketW(a->family, a->type, a->protocol, NULL, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); + if (a->socket == INVALID_SOCKET) { + return US_ACCEPT_FAILED; + } + memset(&a->op.overlapped, 0, sizeof(a->op.overlapped)); + /* The call clears the event before it starts. */ + a->op.overlapped.hEvent = (HANDLE) ((ULONG_PTR) a->event | 1); + DWORD bytes; + if (!a->accept_ex(a->listener, a->socket, a->addresses, 0, US_ACCEPT_ADDRESS_LENGTH, US_ACCEPT_ADDRESS_LENGTH, &bytes, &a->op.overlapped) && + WSAGetLastError() != ERROR_IO_PENDING) { + int err = WSAGetLastError(); + closesocket(a->socket); + a->socket = INVALID_SOCKET; + WSASetLastError(err); + return US_ACCEPT_FAILED; + } + if (HasOverlappedIoCompleted(&a->op.overlapped)) { + return US_ACCEPT_TAKEN; + } + if (us_iocp_wait_start(a->wait, a->event, &a->op) != 0) { + /* Nothing will announce the completion, so collect it here. */ + CancelIoEx((HANDLE) a->listener, &a->op.overlapped); + WaitForSingleObject(a->event, INFINITE); + closesocket(a->socket); + a->socket = INVALID_SOCKET; + WSASetLastError(WSAENOBUFS); + return US_ACCEPT_FAILED; + } + a->in_flight = 1; + return US_ACCEPT_PENDING; +} + +/* Whether the completed AcceptEx left a connected socket in a->socket. A failed + * one is the connection's failure (it was reset while it waited), not the + * listener's. */ +static int acceptor_adopt(struct us_internal_acceptor *a) { + if (!NT_SUCCESS((NTSTATUS) a->op.overlapped.Internal)) { + return 0; + } + SOCKET listener = a->listener; + return setsockopt(a->socket, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (const char *) &listener, sizeof(listener)) == 0; +} + +/* Reset, as the connections still in the backlog of a closed listener are. */ +static void acceptor_reset_accepted(struct us_internal_acceptor *a) { + a->accepted = 0; + struct linger reset = {.l_onoff = 1, .l_linger = 0}; + setsockopt(a->socket, SOL_SOCKET, SO_LINGER, (const char *) &reset, sizeof(reset)); + closesocket(a->socket); + a->socket = INVALID_SOCKET; +} + +/* Keep the listener accepting. A connection that was waiting is left in + * a->socket with a->accepted set when the caller is the owner's dispatch + * (`take`), and completes from the ready list otherwise. While AcceptEx cannot + * be started (out of sockets or memory) the listener is starved: nothing else + * can take a connection from it. */ +static void acceptor_arm(struct us_internal_acceptor *a, int take) { + for (;;) { + int started = acceptor_start(a); + if (started == US_ACCEPT_TAKEN) { + if (!take) { + a->in_flight = 1; + us_iocp_op_submitted(a->loop); + us_iocp_op_ready(a->loop, &a->op); + } else if (acceptor_adopt(a)) { + a->accepted = 1; + } else { + closesocket(a->socket); + a->socket = INVALID_SOCKET; + continue; + } + } + if (started != US_ACCEPT_FAILED) { + acceptor_set_starved(a, 0); + return; + } + /* A connection that was reset while it waited fails the call that would have taken it. */ + if (WSAGetLastError() != WSAECONNRESET) { + break; + } + } + acceptor_set_starved(a, 1); +} + +static void acceptor_retry(struct us_iocp_starved *starved) { + struct us_internal_acceptor *a = (struct us_internal_acceptor *) ((char *) starved - offsetof(struct us_internal_acceptor, retry)); + if (a->owner && !a->in_flight && !a->accepted) { + acceptor_arm(a, 0); + } +} + +static void acceptor_complete(struct us_loop_t *loop, struct us_iocp_op *op) { + (void) loop; + struct us_internal_acceptor *a = (struct us_internal_acceptor *) op; + a->in_flight = 0; + + if (!a->owner || a->loop->closing) { + acceptor_maybe_free(a); + return; + } + + if (acceptor_adopt(a)) { + a->accepted = 1; + } else { + closesocket(a->socket); + a->socket = INVALID_SOCKET; + acceptor_arm(a, 1); + } + + a->dispatching++; + while (a->accepted) { + us_internal_dispatch_ready_poll(a->owner, 0, 0, LIBUS_SOCKET_READABLE); + if (a->accepted) { + /* Not taken: the listener was closed first. */ + acceptor_reset_accepted(a); + } + /* The owner accepts until a call stays pending. When it stopped before + * that, this carries on, and dispatches what it finds waiting. */ + if (a->owner && !a->in_flight) { + acceptor_arm(a, 1); + } + } + a->dispatching--; + acceptor_maybe_free(a); +} + +static void acceptor_cancel(struct us_internal_acceptor *a) { + if (a->in_flight) { + CancelIoEx((HANDLE) a->listener, &a->op.overlapped); + } +} + +/* Between two calls of us_internal_accept the owner's callback runs with no + * AcceptEx out. A tick entered from there has to hear of the next connection. */ +static void acceptors_arm_dispatching(struct us_loop_t *loop) { + for (struct us_internal_acceptor *a = loop->acceptors; a; a = a->next) { + if (a->dispatching && a->owner && !a->in_flight && !a->accepted) { + acceptor_arm(a, 0); + } + } +} + +static void acceptors_cancel(struct us_loop_t *loop) { + for (struct us_internal_acceptor *a = loop->acceptors; a; a = a->next) { + acceptor_cancel(a); + } +} + +static void acceptor_stop(struct us_internal_acceptor *a) { + a->owner = NULL; + acceptor_set_starved(a, 0); + /* It has taken a connection, and the packet that says so is on its way. */ + if (a->in_flight && HasOverlappedIoCompleted(&a->op.overlapped) && acceptor_adopt(a)) { + acceptor_reset_accepted(a); + } + acceptor_cancel(a); + acceptor_maybe_free(a); +} + +/* NULL when the socket has to be polled and accept()ed instead: its provider + * is not AFD (a non-IFS LSP), or it is not listening yet. */ +static struct us_internal_acceptor *acceptor_create(struct us_loop_t *loop, struct us_poll_t *owner) { + SOCKET listener = owner->fd; + WSAPROTOCOL_INFOW info; + int info_length = sizeof(info); + int not_a_socket; + if (getsockopt(listener, SOL_SOCKET, SO_PROTOCOL_INFOW, (char *) &info, &info_length) != 0 || + afd_base_socket(listener, ¬_a_socket) == INVALID_SOCKET) { + return NULL; + } + GUID accept_ex_id = WSAID_ACCEPTEX; + GUID get_addresses_id = WSAID_GETACCEPTEXSOCKADDRS; + LPFN_ACCEPTEX accept_ex = NULL; + LPFN_GETACCEPTEXSOCKADDRS get_addresses = NULL; + DWORD bytes; + if (WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &accept_ex_id, sizeof(accept_ex_id), &accept_ex, sizeof(accept_ex), &bytes, NULL, NULL) != 0 || + WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &get_addresses_id, sizeof(get_addresses_id), &get_addresses, sizeof(get_addresses), &bytes, NULL, + NULL) != 0) { + return NULL; + } + + struct us_internal_acceptor *a = us_calloc(1, sizeof(struct us_internal_acceptor)); + a->owner = owner; + a->loop = loop; + a->listener = listener; + a->family = info.iAddressFamily; + a->type = info.iSocketType; + a->protocol = info.iProtocol; + a->accept_ex = accept_ex; + a->get_addresses = get_addresses; + a->next = loop->acceptors; + if (a->next) { + a->next->prev = a; + } + loop->acceptors = a; + + a->op.complete = acceptor_complete; + a->retry.retry = acceptor_retry; + a->socket = INVALID_SOCKET; + a->event = CreateEventW(NULL, TRUE, FALSE, NULL); + a->wait = a->event ? us_iocp_wait_create(loop) : NULL; + int started = a->wait != NULL; + if (started) { + int result = acceptor_start(a); + if (result == US_ACCEPT_TAKEN) { + a->in_flight = 1; + us_iocp_op_submitted(loop); + us_iocp_op_ready(loop, &a->op); + } + started = result != US_ACCEPT_FAILED; + } + if (!started) { + acceptor_stop(a); + return NULL; + } + return a; +} + +int us_internal_poll_start_accepting(struct us_poll_t *p, struct us_loop_t *loop) { +#if defined(LIBUS_SOCKET_FAULT_INJECTION) && LIBUS_SOCKET_FAULT_INJECTION + /* A listener's poll registration. */ + ssize_t injected = 0; + int unused = 0; + if (US_FAULT_CHECK(US_FAULT_POLL_START, p->fd, injected, unused)) { + errno = (int) -injected; + return (int) injected; + } +#endif + if (nt_ensure()) { + p->acceptor = acceptor_create(loop, p); + if (p->acceptor) { + p->poll_type = (unsigned char) (us_internal_poll_type(p) | POLL_TYPE_POLLING_IN); + return 0; + } + } + return us_poll_start_rc(p, loop, LIBUS_SOCKET_READABLE); +} + +LIBUS_SOCKET_DESCRIPTOR us_internal_accept(struct us_poll_t *p, struct bsd_addr_t *addr) { + struct us_internal_acceptor *a = p->acceptor; + if (!a) { + return bsd_accept_socket(p->fd, addr); + } + if (a->accepted) { + ssize_t injected = 0; + int unused = 0; + if (US_FAULT_CHECK(US_FAULT_ACCEPT, p->fd, injected, unused)) { + acceptor_reset_accepted(a); + acceptor_arm(a, 0); + return LIBUS_SOCKET_ERROR; + } + (void) injected; + (void) unused; + SOCKET accepted = a->socket; + a->accepted = 0; + a->socket = INVALID_SOCKET; + + struct sockaddr *local = NULL; + struct sockaddr *remote = NULL; + int local_length = 0; + int remote_length = 0; + a->get_addresses(a->addresses, 0, US_ACCEPT_ADDRESS_LENGTH, US_ACCEPT_ADDRESS_LENGTH, &local, &local_length, &remote, &remote_length); + if (remote_length < 0 || (size_t) remote_length > sizeof(addr->mem)) { + remote_length = 0; + } + memset(&addr->mem, 0, sizeof(addr->mem)); + if (remote_length) { + memcpy(&addr->mem, remote, (size_t) remote_length); + } + addr->len = remote_length; + internal_finalize_bsd_addr(addr); + + return accepted; + } + if (a->in_flight) { + /* An AcceptEx is outstanding: the next connection is the one it takes. */ + return LIBUS_SOCKET_ERROR; + } + /* What AcceptEx took has been handed over: the next call takes the oldest + * connection still waiting, or stays pending for the next one to arrive. */ + acceptor_arm(a, 1); + return a->accepted ? us_internal_accept(p, addr) : LIBUS_SOCKET_ERROR; +} + +/* Poll */ + +struct us_poll_t *us_create_poll(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) { + if (!fallthrough) { + loop->num_polls++; + } + struct us_poll_t *p = us_malloc(sizeof(struct us_poll_t) + ext_size); + p->afd = NULL; + p->acceptor = NULL; + return p; +} + +void us_poll_free(struct us_poll_t *p, struct us_loop_t *loop) { + if (p->afd) { + afd_poll_stop(p->afd); + } + if (p->acceptor) { + acceptor_stop(p->acceptor); + } + loop->num_polls--; + us_free(p); +} + +void us_poll_init(struct us_poll_t *p, LIBUS_SOCKET_DESCRIPTOR fd, int poll_type) { + p->afd = NULL; + p->acceptor = NULL; + p->fd = fd; + p->poll_type = (unsigned char) poll_type; +} + +int us_poll_events(struct us_poll_t *p) { + return ((p->poll_type & POLL_TYPE_POLLING_IN) ? LIBUS_SOCKET_READABLE : 0) | ((p->poll_type & POLL_TYPE_POLLING_OUT) ? LIBUS_SOCKET_WRITABLE : 0); +} + +LIBUS_SOCKET_DESCRIPTOR us_poll_fd(struct us_poll_t *p) { + return p->fd; +} + +int us_internal_poll_type(struct us_poll_t *p) { + return p->poll_type & POLL_TYPE_KIND_MASK; +} + +void us_internal_poll_set_type(struct us_poll_t *p, int poll_type) { + p->poll_type = (unsigned char) (poll_type | (p->poll_type & POLL_TYPE_POLLING_MASK)); +} + +struct us_poll_t *us_poll_resize(struct us_poll_t *p, struct us_loop_t *loop, unsigned int old_ext_size, unsigned int ext_size) { + unsigned int old_size = sizeof(struct us_poll_t) + old_ext_size; + unsigned int new_size = sizeof(struct us_poll_t) + ext_size; + if (new_size <= old_size) return p; + + struct us_poll_t *new_p = us_calloc(1, new_size); + memcpy(new_p, p, old_size); + + /* The old poll is freed separately, which decrements the count again. */ + loop->num_polls++; + + if (new_p->afd) { + new_p->afd->owner = new_p; + p->afd = NULL; + } + if (new_p->acceptor) { + new_p->acceptor->owner = new_p; + p->acceptor = NULL; + } + return new_p; +} + +int us_poll_start_rc(struct us_poll_t *p, struct us_loop_t *loop, int events) { + p->poll_type = (unsigned char) (us_internal_poll_type(p) | ((events & LIBUS_SOCKET_READABLE) ? POLL_TYPE_POLLING_IN : 0) | ((events & LIBUS_SOCKET_WRITABLE) ? POLL_TYPE_POLLING_OUT : 0)); + + /* Readiness only means something for a non-blocking socket. This is also + * where a descriptor that is not a socket gets rejected. */ + u_long nonblocking = 1; + if (ioctlsocket(p->fd, FIONBIO, &nonblocking) != SOCKET_ERROR) { + p->afd = afd_poll_create(loop, p, p->fd, events); + if (p->afd) { + return 0; + } + } + /* The callers report errno. */ + errno = WSAGetLastError(); + return -1; +} + +int us_poll_change(struct us_poll_t *p, struct us_loop_t *loop, int events) { + if (us_poll_events(p) == events) { + return 0; + } + p->poll_type = (unsigned char) (us_internal_poll_type(p) | ((events & LIBUS_SOCKET_READABLE) ? POLL_TYPE_POLLING_IN : 0) | ((events & LIBUS_SOCKET_WRITABLE) ? POLL_TYPE_POLLING_OUT : 0)); + (void) loop; + if (p->acceptor || !p->afd) { + return 0; + } + afd_poll_set_interest(p->afd, events); + return 0; +} + +void us_poll_stop(struct us_poll_t *p, struct us_loop_t *loop) { + (void) loop; + if (p->afd) { + afd_poll_stop(p->afd); + p->afd = NULL; + } + if (p->acceptor) { + acceptor_stop(p->acceptor); + p->acceptor = NULL; + } +} + +size_t us_internal_accept_poll_event(struct us_poll_t *p) { + (void) p; + return 0; +} + +/* Async (internal helper for loop's wakeup feature) */ + +struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) { + struct us_internal_callback_t *cb = us_calloc(1, sizeof(struct us_internal_callback_t) + ext_size); + cb->loop = loop; + cb->fallthrough = fallthrough; + us_poll_init(&cb->p, INVALID_SOCKET, POLL_TYPE_CALLBACK); + + if (!fallthrough) { + loop->num_polls++; + } + return (struct us_internal_async *) cb; +} + +static void us_internal_async_complete(struct us_loop_t *loop, struct us_iocp_op *op) { + (void) loop; + struct us_internal_callback_t *cb = (struct us_internal_callback_t *) ((char *) op - offsetof(struct us_internal_callback_t, op)); + /* Cleared before the callback: a wakeup sent while it runs must post again. */ + InterlockedExchange(&cb->posted, 0); + if (cb->closed) { + us_free(cb); + return; + } + cb->cb((struct us_internal_callback_t *) cb->loop); +} + +void us_internal_async_close(struct us_internal_async *a) { + struct us_internal_callback_t *cb = (struct us_internal_callback_t *) a; + if (!cb->fallthrough) { + cb->loop->num_polls--; + } + cb->closed = 1; + /* A posted packet still points here; its completion frees it. */ + if (!InterlockedCompareExchange(&cb->posted, 1, 0)) { + us_free(cb); + } +} + +void us_internal_async_set(struct us_internal_async *a, void (*cb)(struct us_internal_async *)) { + struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) a; + internal_cb->cb = (void (*)(struct us_internal_callback_t *)) cb; + internal_cb->op.complete = us_internal_async_complete; +} + +void us_internal_async_wakeup(struct us_internal_async *a) { + struct us_internal_callback_t *cb = (struct us_internal_callback_t *) a; + /* At most one packet in flight however many threads wake the loop. */ + if (InterlockedExchange(&cb->posted, 1) == 0) { + us_iocp_op_submitted(cb->loop); + if (!PostQueuedCompletionStatus(cb->loop->iocp, 0, 0, &cb->op.overlapped)) { + InterlockedDecrement((volatile LONG *) &cb->loop->pending_ops); + InterlockedExchange(&cb->posted, 0); + } + } +} + +#endif diff --git a/packages/bun-usockets/src/eventing/libuv.c b/packages/bun-usockets/src/eventing/libuv.c deleted file mode 100644 index a76d0e73f48d..000000000000 --- a/packages/bun-usockets/src/eventing/libuv.c +++ /dev/null @@ -1,560 +0,0 @@ -/* - * Authored by Alex Hultman, 2018-2021. - * Intellectual property of third-party. - - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - - * http://www.apache.org/licenses/LICENSE-2.0 - - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "internal/internal.h" -#include "internal/fault_inject.h" -#include "libusockets.h" -#include - -#ifdef LIBUS_USE_LIBUV - -/* Windows does not reliably latch a received RST in SO_ERROR (POSIX does); - * the reset surfaces on the next I/O. A zero-byte send observes it without - * touching the stream: 0 on a healthy socket, SOCKET_ERROR with a fatal - * code once the connection died hard. */ -int us_internal_libuv_peer_reset_probe(LIBUS_SOCKET_DESCRIPTOR fd) { - /* Raw winsock send: this file only builds on the libuv (Windows) path and - * bsd_send's Windows signature has no flags parameter. */ - if (send(fd, "", 0, 0) != SOCKET_ERROR) { - return 0; - } - int err = WSAGetLastError(); - /* WSAESHUTDOWN means our own shutdown(SD_SEND) ran; that is not a peer - * reset (us_socket_stalled_write_means_peer_gone can ask after one). */ - return err != WSAEWOULDBLOCK && err != WSAESHUTDOWN; -} - -/* The shared dispatch follows socket adoption (a tunneled/upgraded socket - * moves; the old allocation stays readable with flags.adopted set and prev - * pointing at the live one) and skips closed sockets. poll_cb's probes must - * honor the same contract - dereferencing the raw poll cast crashed the - * CONNECT-tunnel tests on the aarch64 agent. */ -static struct us_socket_t *us_internal_poll_cb_adopted_socket(struct us_poll_t *wp) { - return us_internal_socket_follow_adopted((struct us_socket_t *)wp); -} - -/* uv_poll_t->data always (except for most times after calling us_poll_stop) - * points to the us_poll_t */ -static void poll_cb(uv_poll_t *p, int status, int events) { - /* UV_DISCONNECT (Windows AFD): the peer closed its write side. A FIN - * arriving after this side already half-closed and stopped reading never - * fires another readable poll, and the socket (and server.close()) waits - * forever - so DISCONNECT is armed unconditionally in us_poll_start/change - * and surfaced as a readable dispatch: the read loop's recv() discovers - * the true end of stream (0) after consuming whatever is still queued. - * It is mapped to the eof hint ONLY for sockets whose write side we - * already shut down (see below): unlike kqueue's EV_EOF, which the kernel - * sets only alongside the final data, AFD can signal DISCONNECT while data - * is still in flight, and an unconditional eof mapping closed connections - * at a mid-stream EAGAIN (truncated bodies across the fetch/backpressure - * suites). One-shot: AFD keeps reporting DISCONNECT once signaled, so - * re-arm without it - us_poll_start/us_poll_change add it back on the next - * poll change. - * https://github.com/libuv/libuv/blob/v1.x/docs/src/poll.rst (UV_DISCONNECT - * is Windows-only and best-effort; readable polling stays the primary - * signal). */ - int eof = status == UV_EOF; - int error = status < 0 && status != UV_EOF; - if (events & (UV_DISCONNECT | UV_PRIORITIZED)) { - struct us_poll_t *wp = (struct us_poll_t *)p->data; - uv_poll_start(p, us_poll_events(wp), poll_cb); - int kind = us_internal_poll_type(wp) & POLL_TYPE_KIND_MASK; - /* For a socket whose write side we already shut down, AFD delivers no - * readable event for the peer's FIN at all - the exact half-closed state - * that hung server.close() - and with our writes closed there is no - * data-bearing flow left that an early EOF could truncate. Only there is - * DISCONNECT mapped to the eof hint (like kqueue's EV_EOF); every other - * socket keeps recv()-owned EOF discovery so mid-stream transfers are - * never cut at an EAGAIN. */ - if (kind == POLL_TYPE_SOCKET_SHUT_DOWN) { - eof = 1; - /* A paused socket keeps the hint only; the dispatcher leaves it for - * resume(), whose poll change re-arms DISCONNECT and lands here again. */ - events |= us_poll_events(wp) & LIBUS_SOCKET_READABLE; - } else if (kind == POLL_TYPE_SOCKET && - !(us_poll_events(wp) & LIBUS_SOCKET_READABLE)) { - /* A data socket that is not reading: paused, or half-open with its end - * already delivered (the EOF path moved its poll to WRITABLE-only, and - * us_poll_change re-adds UV_DISCONNECT unconditionally, so AFD keeps - * reporting the FIN's level-triggered DISCONNECT). Re-adding READABLE - * here would pull bytes a paused caller asked to defer, or rediscover - * the same EOF and busy-loop on_end; keeping DISCONNECT armed would - * complete instantly forever. A dead peer surfaces via SO_ERROR or the - * zero-byte send probe and goes through the shared error path (which - * reads off whatever is still queued and closes); a FIN, fresh on a - * paused socket or re-reported on a half-open one, quiesces with only - * the ABORT-only subscription (UV_PRIORITIZED) kept armed so a later - * RST still has an event to ride, and a paused socket meets the FIN - * again through recv() once resume() re-arms READABLE. Non-SOCKET kinds - * keep the unconditional READABLE below: SEMI_SOCKET checks error/eof - * (set from status) and listen polls READABLE only. */ - struct us_socket_t *sock = us_internal_poll_cb_adopted_socket(wp); - /* A reported UV_PRIORITIZED is AFD's own ABORT signal and needs no - * probe; the probe covers a reset that arrives while PRIORITIZED was - * not yet subscribed (reported as plain DISCONNECT, same as the FIN - * re-report) and a reset AFD has latched but only SO_ERROR shows. */ - if (!sock->flags.is_closed && - ((events & UV_PRIORITIZED) || - us_socket_get_error(sock) != 0 || - us_internal_libuv_peer_reset_probe(us_poll_fd(wp)))) { - error = 1; - } else { - uv_poll_start(p, us_poll_events(wp) | UV_PRIORITIZED, poll_cb); - } - } else { - events |= UV_READABLE; - } - } - if (!error && !eof && !(events & (UV_READABLE | UV_WRITABLE))) { - return; - } - us_internal_dispatch_ready_poll((struct us_poll_t *)p->data, error, eof, events); -} - -static void prepare_cb(uv_prepare_t *p) { - struct us_loop_t *loop = p->data; - us_internal_loop_pre(loop); -} - -/* Note: libuv timers execute AFTER the post callback */ -static void check_cb(uv_check_t *p) { - struct us_loop_t *loop = p->data; - us_internal_loop_post(loop); -} - -/* Not used for polls, since polls need two frees */ -static void close_cb_free(uv_handle_t *h) { us_free(h->data); } - -/* This one is different for polls, since we need two frees here */ -static void close_cb_free_poll(uv_handle_t *h) { - /* It is only in case we called us_poll_stop then quickly us_poll_free that we - * enter this. Most of the time, actual freeing is done by us_poll_free. */ - if (h->data) { - us_free(h->data); - us_free(h); - } -} - -static void timer_cb(uv_timer_t *t) { - struct us_internal_callback_t *cb = t->data; - cb->cb(cb); -} - -static void async_cb(uv_async_t *a) { - struct us_internal_callback_t *cb = a->data; - // internal asyncs give their loop, not themselves - cb->cb((struct us_internal_callback_t *)cb->loop); -} - -// poll -void us_poll_init(struct us_poll_t *p, LIBUS_SOCKET_DESCRIPTOR fd, - int poll_type) { - p->poll_type = poll_type; - p->fd = fd; -} - -void us_poll_free(struct us_poll_t *p, struct us_loop_t *loop) { - // poll was resized and dont own uv_poll_t anymore - if(!p->uv_p) { - us_free(p); - return; - } - /* The idea here is like so; in us_poll_stop we call uv_close after setting - * data of uv-poll to 0. This means that in close_cb_free we call free on 0 - * with does nothing, since us_poll_stop should not really free the poll. - * HOWEVER, if we then call us_poll_free while still closing the uv-poll, we - * simply change back the data to point to our structure so that we actually - * do free it like we should. */ - if (uv_is_closing((uv_handle_t *)p->uv_p)) { - p->uv_p->data = p; - } else { - us_free(p->uv_p); - us_free(p); - } -} - -int us_poll_start_rc(struct us_poll_t *p, struct us_loop_t *loop, int events) { - if(!p->uv_p) return 0; - p->poll_type = us_internal_poll_type(p) | - ((events & LIBUS_SOCKET_READABLE) ? POLL_TYPE_POLLING_IN : 0) | - ((events & LIBUS_SOCKET_WRITABLE) ? POLL_TYPE_POLLING_OUT : 0); - - /* uv_poll_init_socket (win/poll.c) can fail either before uv__handle_init - * (ioctlsocket FIONBIO) or after it (getsockopt SO_PROTOCOL_INFOW). The - * latter leaves the handle linked into loop->handle_queue with - * submitted_events_* still unset. Zero first so, on failure, ->type - * distinguishes the two states and the fields uv__poll_close reads are 0 - * rather than garbage. */ - memset(p->uv_p, 0, sizeof(uv_poll_t)); - p->uv_p->data = p; - - int rc; -#if defined(LIBUS_SOCKET_FAULT_INJECTION) && LIBUS_SOCKET_FAULT_INJECTION - ssize_t injected = 0; - int unused = 0; - if (US_FAULT_CHECK(US_FAULT_POLL_START, p->fd, injected, unused)) { - rc = (int) injected; - } else -#endif - rc = uv_poll_init_socket(loop->uv_loop, p->uv_p, p->fd); - if (rc < 0) { - int saved = LIBUS_ERR; - if (p->uv_p->type == UV_POLL) { - /* uv__handle_init ran: the handle is in loop->handle_queue. Close it - * through libuv so it is unlinked; the caller's us_poll_free sees - * uv_is_closing and hands ownership to close_cb_free_poll. */ - p->uv_p->data = 0; - uv_close((uv_handle_t *)p->uv_p, close_cb_free_poll); - } else { - /* Never reached uv__handle_init: uv_p is still our raw block. Free it - * here and null the pointer so the caller's us_poll_free takes the - * !uv_p fast path (its uv_is_closing check would read garbage). */ - us_free(p->uv_p); - p->uv_p = NULL; - } - errno = saved ? saved : -rc; - return rc; - } - // This unref is okay in the context of Bun's event loop, because sockets have - // a `Async.KeepAlive` associated with them, which is used instead of the - // usockets internals. usockets doesnt have a notion of ref-counted handles. - uv_unref((uv_handle_t *)p->uv_p); - /* Always ask for UV_DISCONNECT: a peer FIN must fire even when the poll is - * writable-only at that moment (a half-closed connection whose reads are - * paused is exactly the state that otherwise hangs; see poll_cb). */ - uv_poll_start(p->uv_p, events | UV_DISCONNECT, poll_cb); - return 0; -} - -void us_poll_start(struct us_poll_t *p, struct us_loop_t *loop, int events) { - us_poll_start_rc(p, loop, events); -} - -int us_poll_change(struct us_poll_t *p, struct us_loop_t *loop, int events) { - if(!p->uv_p) return 0; - if (us_poll_events(p) != events) { - p->poll_type = - us_internal_poll_type(p) | - ((events & LIBUS_SOCKET_READABLE) ? POLL_TYPE_POLLING_IN : 0) | - ((events & LIBUS_SOCKET_WRITABLE) ? POLL_TYPE_POLLING_OUT : 0); - /* The poll stays initialized across changes here (the dispatcher never - * parks a libuv poll), so this cannot hit the registration failure the - * epoll re-add can; uv_poll_start on a live poll only rejects bad args. */ - uv_poll_start(p->uv_p, events | UV_DISCONNECT, poll_cb); - } - return 0; -} - -void us_poll_stop(struct us_poll_t *p, struct us_loop_t *loop) { - if(!p->uv_p) return; - uv_poll_stop(p->uv_p); - - /* We normally only want to close the poll here, not free it. But if we stop - * it, then quickly "free" it with us_poll_free, we postpone the actual - * freeing to close_cb_free_poll whenever it triggers. That's why we set data - * to null here, so that us_poll_free can reset it if needed */ - p->uv_p->data = 0; - uv_close((uv_handle_t *)p->uv_p, close_cb_free_poll); -} - -int us_poll_events(struct us_poll_t *p) { - return ((p->poll_type & POLL_TYPE_POLLING_IN) ? LIBUS_SOCKET_READABLE : 0) | - ((p->poll_type & POLL_TYPE_POLLING_OUT) ? LIBUS_SOCKET_WRITABLE : 0); -} - -size_t us_internal_accept_poll_event(struct us_poll_t *p) { return 0; } - -int us_internal_poll_type(struct us_poll_t *p) { return p->poll_type & POLL_TYPE_KIND_MASK; } - -void us_internal_poll_set_type(struct us_poll_t *p, int poll_type) { - p->poll_type = poll_type | (p->poll_type & POLL_TYPE_POLLING_MASK); -} - -LIBUS_SOCKET_DESCRIPTOR us_poll_fd(struct us_poll_t *p) { return p->fd; } - -void us_loop_pump(struct us_loop_t *loop) { - /* POSIX parity: us_loop_run_bun_tick polls epoll/kqueue and dispatches - * regardless of ref state (it only early-outs on num_polls == 0). libuv's - * uv_run() skips its body when uv__loop_alive() is 0, so IOCP completions - * for unref'd handles (subprocess exit packets, socket events) and due - * timers are never processed. Bun's outer drive loops (wait_for_promise, - * bun:test) supply their own keep-going predicate, so force exactly one - * non-blocking iteration; UV_RUN_NOWAIT keeps the poll timeout at 0. */ - loop->uv_loop->active_handles++; - uv_run(loop->uv_loop, UV_RUN_NOWAIT); - loop->uv_loop->active_handles--; -} - -struct us_loop_t *us_create_loop(void *hint, - void (*wakeup_cb)(struct us_loop_t *loop), - void (*pre_cb)(struct us_loop_t *loop), - void (*post_cb)(struct us_loop_t *loop), - unsigned int ext_size) { - struct us_loop_t *loop = - (struct us_loop_t *)us_calloc(1, sizeof(struct us_loop_t) + ext_size); - - loop->uv_loop = hint ? hint : uv_loop_new(); - /* NULL when uv_loop_init fails (CreateIoCompletionPort under handle exhaustion). */ - if (!loop->uv_loop) { - us_free(loop); - return NULL; - } - loop->is_default = hint != 0; - - loop->uv_pre = us_malloc(sizeof(uv_prepare_t)); - uv_prepare_init(loop->uv_loop, loop->uv_pre); - uv_prepare_start(loop->uv_pre, prepare_cb); - uv_unref((uv_handle_t *)loop->uv_pre); - loop->uv_pre->data = loop; - - loop->uv_check = us_malloc(sizeof(uv_check_t)); - uv_check_init(loop->uv_loop, loop->uv_check); - uv_unref((uv_handle_t *)loop->uv_check); - uv_check_start(loop->uv_check, check_cb); - loop->uv_check->data = loop; - - // here we create two unreffed handles - timer and async - // Cannot fail here: this backend's us_internal_create_async acquires no OS resource. - (void)us_internal_loop_data_init(loop, wakeup_cb, pre_cb, post_cb); - - // if we do not own this loop, we need to integrate and set up timer - if (hint) { - us_loop_integrate(loop); - } - - return loop; -} - -// based on if this was default loop or not -void us_loop_free(struct us_loop_t *loop) { - // ref and close down prepare and check - uv_ref((uv_handle_t *)loop->uv_pre); - uv_prepare_stop(loop->uv_pre); - loop->uv_pre->data = loop->uv_pre; - uv_close((uv_handle_t *)loop->uv_pre, close_cb_free); - - uv_ref((uv_handle_t *)loop->uv_check); - uv_check_stop(loop->uv_check); - loop->uv_check->data = loop->uv_check; - uv_close((uv_handle_t *)loop->uv_check, close_cb_free); - - if (loop->idle_sweep_timer) { - us_timer_close(loop->idle_sweep_timer, 0); - } - - us_internal_loop_data_free(loop); - -// we need to run the loop one last round to call all close callbacks - // we cannot do this if we do not own the loop, default - if (!loop->is_default) { - uv_run(loop->uv_loop, UV_RUN_NOWAIT); - uv_loop_delete(loop->uv_loop); - } - - // now we can free our part - us_free(loop); -} - -extern unsigned int Bun__JSC_onBeforeWait(void *jsc_vm, uint64_t now_ns, int *released_heap_access); - -static void idle_sweep_again_cb(struct us_timer_t *t) {} - -void us_loop_run(struct us_loop_t *loop) { - us_loop_integrate(loop); - uv_update_time(loop->uv_loop); - - /* UV_RUN_ONCE may block in the poll phase (pending callbacks dispatch - * first), making this the JS thread's park hook, the counterpart of - * us_loop_run_bun_tick's. jsc_vm is only set on the JS thread's loop. */ - if (loop->data.jsc_vm) { - /* uv_update_time() above just refreshed libuv's cached monotonic clock, so - * uv_now() reads that cache rather than taking the clock again. */ - const unsigned int run_again_ms = - Bun__JSC_onBeforeWait(loop->data.jsc_vm, (uint64_t) uv_now(loop->uv_loop) * 1000000ULL, NULL); - if (run_again_ms) { - /* The allocator's idle sweep wants another turn even if nothing else ends - * the poll. Unref'd: it bounds the poll, it does not keep the loop alive. - * Counted from after the sweep, hence the second uv_update_time(). */ - if (!loop->idle_sweep_timer) { - loop->idle_sweep_timer = us_create_timer(loop, 1, 0); - } - uv_update_time(loop->uv_loop); - us_timer_set(loop->idle_sweep_timer, idle_sweep_again_cb, (int) run_again_ms, 0); - } - } - - uv_run(loop->uv_loop, UV_RUN_ONCE); -} - -struct us_poll_t *us_create_poll(struct us_loop_t *loop, int fallthrough, - unsigned int ext_size) { - struct us_poll_t *p = - (struct us_poll_t *)us_malloc(sizeof(struct us_poll_t) + ext_size); - p->uv_p = us_malloc(sizeof(uv_poll_t)); - p->uv_p->data = p; - return p; -} - -/* If we update our block position we have to update the uv_poll data to point - * to us */ -struct us_poll_t *us_poll_resize(struct us_poll_t *p, struct us_loop_t *loop, - unsigned int old_ext_size, unsigned int ext_size) { - - // cannot resize if we dont own uv_poll_t - if(!p->uv_p) return p; - - unsigned int old_size = sizeof(struct us_poll_t) + old_ext_size; - unsigned int new_size = sizeof(struct us_poll_t) + ext_size; - if(new_size <= old_size) return p; - - struct us_poll_t *new_p = us_calloc(1, new_size); - memcpy(new_p, p, old_size); - - new_p->uv_p->data = new_p; - p->uv_p = NULL; - - return new_p; -} - -// timer -struct us_timer_t *us_create_timer(struct us_loop_t *loop, int fallthrough, - unsigned int ext_size) { - struct us_internal_callback_t *cb = us_calloc( - 1, sizeof(struct us_internal_callback_t) + sizeof(uv_timer_t) + ext_size); - - cb->loop = loop; - cb->cb_expects_the_loop = 0; // never read? - cb->leave_poll_ready = 0; // never read? - - uv_timer_t *uv_timer = (uv_timer_t *)(cb + 1); - uv_timer_init(loop->uv_loop, uv_timer); - uv_timer->data = cb; - - if (fallthrough) { - uv_unref((uv_handle_t *)uv_timer); - } - - return (struct us_timer_t *)cb; -} - -void *us_timer_ext(struct us_timer_t *timer) { - return ((char *)timer) + sizeof(struct us_internal_callback_t) + - sizeof(uv_timer_t); -} - -void us_timer_close(struct us_timer_t *t, int fallthrough) { - struct us_internal_callback_t *cb = (struct us_internal_callback_t *)t; - - uv_timer_t *uv_timer = (uv_timer_t *)(cb + 1); - - // always ref the timer before closing it - uv_ref((uv_handle_t *)uv_timer); - - uv_timer_stop(uv_timer); - - uv_timer->data = cb; - uv_close((uv_handle_t *)uv_timer, close_cb_free); -} - -void us_timer_set(struct us_timer_t *t, void (*cb)(struct us_timer_t *t), - int ms, int repeat_ms) { - struct us_internal_callback_t *internal_cb = - (struct us_internal_callback_t *)t; - - // Match the epoll_kqueue backend: re-arming is allowed (uv_timer_start - // restarts an already-running timer). The one-shot guard only applies to - // the sweep timer, which is set with the same args from every new socket - // context — restarting it would skew the 4s tick. - if (internal_cb->loop->data.sweep_timer == t) { - if (internal_cb->has_added_timer_to_event_loop) { - return; - } - internal_cb->has_added_timer_to_event_loop = 1; - } - - internal_cb->cb = (void (*)(struct us_internal_callback_t *))cb; - - uv_timer_t *uv_timer = (uv_timer_t *)(internal_cb + 1); - if (!ms) { - uv_timer_stop(uv_timer); - } else { - uv_timer_start(uv_timer, timer_cb, ms, repeat_ms); - } -} - -struct us_loop_t *us_timer_loop(struct us_timer_t *t) { - struct us_internal_callback_t *internal_cb = - (struct us_internal_callback_t *)t; - - return internal_cb->loop; -} - -// async (internal only) -struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, - int fallthrough, - unsigned int ext_size) { - struct us_internal_callback_t *cb = us_calloc( - 1, sizeof(struct us_internal_callback_t) + sizeof(uv_async_t) + ext_size); - - cb->loop = loop; - return (struct us_internal_async *)cb; -} - -void us_internal_async_close(struct us_internal_async *a) { - struct us_internal_callback_t *cb = (struct us_internal_callback_t *)a; - - uv_async_t *uv_async = (uv_async_t *)(cb + 1); - - // always ref the async before closing it - uv_ref((uv_handle_t *)uv_async); - - uv_async->data = cb; - uv_close((uv_handle_t *)uv_async, close_cb_free); -} - -void us_internal_async_set(struct us_internal_async *a, - void (*cb)(struct us_internal_async *)) { - struct us_internal_callback_t *internal_cb = - (struct us_internal_callback_t *)a; - - internal_cb->cb = (void (*)(struct us_internal_callback_t *))cb; - - uv_async_t *uv_async = (uv_async_t *)(internal_cb + 1); - uv_async_init(internal_cb->loop->uv_loop, uv_async, async_cb); - uv_unref((uv_handle_t *)uv_async); - uv_async->data = internal_cb; -} - -void us_internal_async_wakeup(struct us_internal_async *a) { - struct us_internal_callback_t *internal_cb = - (struct us_internal_callback_t *)a; - - uv_async_t *uv_async = (uv_async_t *)(internal_cb + 1); - uv_async_send(uv_async); -} - -int us_socket_get_error(struct us_socket_t *s) { - int error = 0; - socklen_t len = sizeof(error); - if (getsockopt(us_poll_fd((struct us_poll_t *)s), SOL_SOCKET, SO_ERROR, - (char *)&error, &len) == -1) { - return LIBUS_ERR; - } - return error; -} - -#endif diff --git a/packages/bun-usockets/src/internal/eventing/iocp.h b/packages/bun-usockets/src/internal/eventing/iocp.h new file mode 100644 index 000000000000..d1ecbd1a0ba9 --- /dev/null +++ b/packages/bun-usockets/src/internal/eventing/iocp.h @@ -0,0 +1,178 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IOCP_H +#define IOCP_H + +#include "internal/loop_data.h" + +#include +#include + +#define LIBUS_SOCKET_READABLE 1 +#define LIBUS_SOCKET_WRITABLE 2 + +struct us_loop_t; + +/* Anything that completes through the loop's completion port. It is the first + * member of the owning struct and its address is what the kernel is given as + * the OVERLAPPED (or as the ApcContext of an NT call / wait completion packet), + * so a dequeued lpOverlapped is the op. The owner must stay allocated until + * `complete` has run for every packet it has in flight: cancelling or closing + * the handle only requests the completion, it does not wait for it. */ +struct us_iocp_op { + OVERLAPPED overlapped; + void (*complete)(struct us_loop_t *loop, struct us_iocp_op *op); + /* The op behind this one in the loop's ready_ops. */ + struct us_iocp_op *next_ready; +}; + +/* Something that could not start waiting for what it serves (a listener whose + * AcceptEx could not be started, a pipe server with an idle slot). While it is linked, + * `retry` runs at the start of every tick, and the loop's sweep timer is held so + * that ticks keep coming. `retry` may unlink its own node and no other. */ +struct us_iocp_starved { + void (*retry)(struct us_iocp_starved *starved); + struct us_iocp_starved *prev; + struct us_iocp_starved *next; +}; + +struct us_internal_afd_poll; +struct us_internal_afd_helper; +struct us_internal_acceptor; + +/* Packets taken per GetQueuedCompletionStatusEx call. The call's cost grows + * with the size of the array it is given, also when the port is empty. */ +#define US_IOCP_MAX_ENTRIES 128 + +/* Same leading fields, in the same order, as the epoll/kqueue us_loop_t: the + * Rust mirror in src/uws_sys/Loop.rs and the shared C code use them by name. */ +struct us_loop_t { + alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data; + + /* Number of non-fallthrough polls in the loop */ + int num_polls; + + /* Number of completion packets in the batch being dispatched */ + int num_ready_polls; + + /* Current index in the list of dequeued packets */ + int current_ready_poll; + + /* The loop's completion port */ + HANDLE iocp; + + /* Number of polls owned by bun */ + unsigned int bun_polls; + + /* Readiness of the Bun-owned poll being dispatched (LIBUS_SOCKET_* bits), + * and whether it failed / saw the peer's FIN. */ + int current_ready_events; + int current_ready_error; + int current_ready_eof; + + /* \Device\Afd handles the socket polls are issued on, and the polls whose + * interest changed since the last wait, oldest first: sockets are re-armed + * in the order they were served, so a busy batch keeps its order instead of + * reversing it every tick. */ + struct us_internal_afd_helper *afd_helpers; + struct us_internal_afd_poll *afd_update_head; + struct us_internal_afd_poll *afd_update_tail; + /* Listening sockets with an AcceptEx outstanding. */ + struct us_internal_acceptor *acceptors; + struct us_iocp_starved *starved; + /* Polls whose cancellation has not been dequeued yet. */ + unsigned int afd_cancelled_polls; + /* us_loop_free is collecting what is still in flight: nothing is reported or re-armed. */ + unsigned char closing; + + /* Every loop of the process, for waking them when the system resumes. */ + struct us_loop_t *resume_next; + struct us_loop_t *resume_prev; + + /* Ops submitted and not yet dequeued. The port is closed only at zero. */ + unsigned int pending_ops; + /* select() requests that are out for sockets AFD cannot poll. */ + struct us_internal_slow_poll_req *slow_reqs; + + /* Ops handed to us_iocp_op_ready and not completed yet, oldest first. */ + struct us_iocp_op *ready_ops_head; + struct us_iocp_op *ready_ops_tail; + unsigned int num_ready_ops; + + /* Sub-tick wait timeouts: a high-resolution waitable timer delivered into + * the port by a wait completion packet. NULL where either is unavailable. */ + HANDLE hrtimer; + HANDLE hrtimer_packet; + /* When the armed timer is due, on the clock of us_internal_monotonic_ns + * (which the tick's `now_ns` is a reading of); 0 while it is not armed. */ + uint64_t hrtimer_deadline_ns; + /* The wait packet is associated with the timer, or queued on the port. */ + unsigned char hrtimer_packet_out; + + alignas(LIBUS_EXT_ALIGNMENT) OVERLAPPED_ENTRY ready_polls[US_IOCP_MAX_ENTRIES]; +}; + +struct us_poll_t { + /* Kernel-visible poll state. Allocated separately because us_poll_resize + * moves the us_poll_t while a poll may be outstanding. */ + alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_afd_poll *afd; + /* A listening socket accepts through this instead of being polled. */ + struct us_internal_acceptor *acceptor; + LIBUS_SOCKET_DESCRIPTOR fd; + unsigned char poll_type; +}; + +/* The loop's completion port, for associating handles whose I/O is described + * by a us_iocp_op. */ +HANDLE us_loop_iocp(struct us_loop_t *loop); + +/* Accounting for ops the caller submits itself (ReadFile/WriteFile/ + * ConnectNamedPipe/... on a handle associated with us_loop_iocp). Call + * us_iocp_op_submitted once the kernel has accepted the op (it returned + * pending, or success without FILE_SKIP_COMPLETION_PORT_ON_SUCCESS); the loop + * balances it when the packet is dequeued. */ +void us_iocp_op_submitted(struct us_loop_t *loop); +/* The packet of a submitted op will not come: a thread that cannot be stopped + * was left to finish the op by itself, and frees it. */ +void us_iocp_op_abandoned(struct us_loop_t *loop); + +/* For an op whose outcome its owner already knows (the call that would have + * started it failed or finished on the spot) and whose `complete` has to run + * from the loop all the same. The next tick runs it before it takes packets + * from the port, in the order of these calls. Loop thread only. Counts as + * submitted: call us_iocp_op_submitted as for a packet. */ +void us_iocp_op_ready(struct us_loop_t *loop, struct us_iocp_op *op); + +/* Loop thread only. A linked node is unlinked before the loop is freed. */ +void us_iocp_starved_link(struct us_loop_t *loop, struct us_iocp_starved *starved); +void us_iocp_starved_unlink(struct us_loop_t *loop, struct us_iocp_starved *starved); + +/* Deliver `op` once when `handle` becomes signalled (process exit, event, + * console input...). us_iocp_wait_stop returns nonzero if the wait was removed + * before it fired, in which case `complete` will not run. */ +struct us_iocp_wait; +struct us_iocp_wait *us_iocp_wait_create(struct us_loop_t *loop); +int us_iocp_wait_start(struct us_iocp_wait *wait, HANDLE handle, struct us_iocp_op *op); +int us_iocp_wait_stop(struct us_iocp_wait *wait); +void us_iocp_wait_free(struct us_iocp_wait *wait); + +/* Socket readiness for a poll Bun owns (`owner` is a tagged pointer handed back + * through Bun__internal_dispatch_ready_poll, with the readiness in + * loop->current_ready_*). */ +struct us_internal_afd_poll *us_iocp_poll_socket(struct us_loop_t *loop, void *owner, SOCKET socket, int events); +void us_iocp_poll_socket_change(struct us_loop_t *loop, struct us_internal_afd_poll *poll, int events); +void us_iocp_poll_socket_stop(struct us_loop_t *loop, struct us_internal_afd_poll *poll); + +#endif // IOCP_H diff --git a/packages/bun-usockets/src/internal/eventing/libuv.h b/packages/bun-usockets/src/internal/eventing/libuv.h deleted file mode 100644 index 980f6c796539..000000000000 --- a/packages/bun-usockets/src/internal/eventing/libuv.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Authored by Alex Hultman, 2018-2019. - * Intellectual property of third-party. - - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - - * http://www.apache.org/licenses/LICENSE-2.0 - - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LIBUV_H -#define LIBUV_H - -#include "internal/loop_data.h" - -#include -#define LIBUS_SOCKET_READABLE UV_READABLE -#define LIBUS_SOCKET_WRITABLE UV_WRITABLE - -/* Defined in eventing/libuv.c; used by the sweep escalation in loop.c. */ -int us_internal_libuv_peer_reset_probe(LIBUS_SOCKET_DESCRIPTOR fd); - -struct us_loop_t { - alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data; - - uv_loop_t *uv_loop; - int is_default; - - uv_prepare_t *uv_pre; - uv_check_t *uv_check; - /* Created on first use by us_loop_run. */ - struct us_timer_t *idle_sweep_timer; -}; - -// it is no longer valid to cast a pointer to us_poll_t to a pointer of -// uv_poll_t -struct us_poll_t { - /* We need to hold a pointer to this uv_poll_t since we need to be able to - * resize our block */ - uv_poll_t *uv_p; - LIBUS_SOCKET_DESCRIPTOR fd; - unsigned char poll_type; -}; - -#endif // LIBUV_H \ No newline at end of file diff --git a/packages/bun-usockets/src/internal/fault_inject.h b/packages/bun-usockets/src/internal/fault_inject.h index 8668c1bb86ce..c2712b582ccc 100644 --- a/packages/bun-usockets/src/internal/fault_inject.h +++ b/packages/bun-usockets/src/internal/fault_inject.h @@ -43,8 +43,9 @@ enum us_fault_syscall { * us_internal_init_loop_ssl_data. Only US_FAULT_ERRNO applies — there is * no byte count to clamp and no zero return to fake. */ US_FAULT_SSL_LOOP_BUFFER, - /* Not a syscall: poll registration in us_poll_start_rc (uv_poll_init_socket - * on Windows/libuv, EPOLL_CTL_ADD / kevent on epoll/kqueue). The fd is + /* Not a syscall: poll registration in us_poll_start_rc (every hand-over of + * an AFD poll to the kernel and a listener's start on Windows, + * EPOLL_CTL_ADD / kevent on epoll/kqueue). The fd is * always fresh from the kernel here, so the failure path is unreachable * without fault injection. Only US_FAULT_ERRNO applies. */ US_FAULT_POLL_START, diff --git a/packages/bun-usockets/src/internal/internal.h b/packages/bun-usockets/src/internal/internal.h index 1cc9fe57875a..c6a31d85444e 100644 --- a/packages/bun-usockets/src/internal/internal.h +++ b/packages/bun-usockets/src/internal/internal.h @@ -59,8 +59,8 @@ void us_internal_loop_update_pending_ready_polls(struct us_loop_t *loop, #include "internal/eventing/epoll_kqueue.h" #endif -#ifdef LIBUS_USE_LIBUV -#include "internal/eventing/libuv.h" +#ifdef LIBUS_USE_IOCP +#include "internal/eventing/iocp.h" #endif #ifndef LIKELY @@ -84,7 +84,7 @@ extern void __attribute__((__noreturn__)) Bun__outOfMemory(void); #define IS_EINTR(rc) (rc == SOCKET_ERROR && WSAGetLastError() == WSAEINTR) #define LIBUS_ERR WSAGetLastError() #define LIBUS_ECONNRESET WSAECONNRESET -/* What libuv translates to UV_ECANCELED (uv_translate_sys_error). */ +/* What windows_errno.rs maps to ECANCELED, as libuv does. */ #define LIBUS_ECANCELED WSAEINTR #else #include @@ -174,16 +174,24 @@ extern struct addrinfo_result *Bun__addrinfo_getRequestResult(struct addrinfo_re #define LIBUS_POLL_EOF 1 #define LIBUS_POLL_HANGUP 2 void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, int events); +/* Pointer tags are used to indicate a Bun pointer versus a uSockets pointer */ +#define UNSET_BITS_49_UNTIL_64 0x0000FFFFFFFFFFFF +#define CLEAR_POINTER_TAG(p) ((void *) ((uintptr_t) (p) & UNSET_BITS_49_UNTIL_64)) +/* Dispatch of a ready poll that Bun owns (`poll` is tagged). */ +void Bun__internal_dispatch_ready_poll(void *loop, void *poll); void us_internal_timer_sweep(us_loop_r loop); void us_internal_enable_sweep_timer(struct us_loop_t *loop); void us_internal_disable_sweep_timer(struct us_loop_t *loop); -#ifndef LIBUS_USE_LIBUV -/* CLOCK_MONOTONIC in ns. The clock every deadline on the loop is measured - * against, so anything comparing against one must read it and not another. */ +/* Monotonic clock in ns (CLOCK_MONOTONIC, QueryPerformanceCounter on Windows). + * The clock every deadline on the loop is measured against, so anything + * comparing against one must read it and not another. */ uint64_t us_internal_monotonic_ns(void); long long us_internal_sweep_timeout_ns(struct us_loop_t *loop); void us_internal_sweep_if_due(struct us_loop_t *loop); -#endif +/* What a tick about to park does when mimalloc has no scavenger to hand this thread's heaps + * to: sweeps them here, at most once per 100ms. `now_ns` as for us_loop_run_bun_tick; nothing + * measures a deadline against it. */ +void us_internal_idle_sweep(uint64_t now_ns); void us_internal_free_closed_sockets(us_loop_r loop); void us_internal_loop_link_group(struct us_loop_t *loop, struct us_socket_group_t *group); void us_internal_loop_unlink_group(struct us_loop_t *loop, struct us_socket_group_t *group); @@ -223,6 +231,16 @@ void us_internal_async_wakeup(struct us_internal_async *a); /* Eventing related */ size_t us_internal_accept_poll_event(struct us_poll_t *p); +/* Start reporting the connections of the listening socket `p` as readable + * events; us_internal_accept takes them. Returns like us_poll_start_rc. */ +int us_internal_poll_start_accepting(struct us_poll_t *p, struct us_loop_t *loop); +/* The next connection of the listening socket `p`, or LIBUS_SOCKET_ERROR when + * there is none right now. */ +LIBUS_SOCKET_DESCRIPTOR us_internal_accept(struct us_poll_t *p, struct bsd_addr_t *addr); +#ifdef _WIN32 +/* Nonzero once the peer reset the connection, observed with a zero-byte send. */ +int us_internal_peer_reset_probe(LIBUS_SOCKET_DESCRIPTOR fd); +#endif int us_internal_poll_type(struct us_poll_t *p); void us_internal_poll_set_type(struct us_poll_t *p, int poll_type); @@ -450,7 +468,6 @@ struct us_udp_socket_t { struct us_internal_callback_t { alignas(LIBUS_EXT_ALIGNMENT) struct us_poll_t p; struct us_loop_t *loop; - int cb_expects_the_loop; int leave_poll_ready; void (*cb)(struct us_internal_callback_t *cb); mach_port_t port; @@ -462,11 +479,15 @@ struct us_internal_callback_t { struct us_internal_callback_t { alignas(LIBUS_EXT_ALIGNMENT) struct us_poll_t p; struct us_loop_t *loop; - int cb_expects_the_loop; int leave_poll_ready; void (*cb)(struct us_internal_callback_t *cb); -#ifdef LIBUS_USE_LIBUV - unsigned has_added_timer_to_event_loop; +#ifdef LIBUS_USE_IOCP + /* The wakeup packet posted to the completion port. `posted` keeps it to one + * in flight; a close while it is in flight frees from its completion. */ + struct us_iocp_op op; + volatile long posted; + unsigned char closed; + unsigned char fallthrough; #endif }; diff --git a/packages/bun-usockets/src/internal/loop_data.h b/packages/bun-usockets/src/internal/loop_data.h index a39aa4f9235a..c023e0b0dc46 100644 --- a/packages/bun-usockets/src/internal/loop_data.h +++ b/packages/bun-usockets/src/internal/loop_data.h @@ -37,20 +37,15 @@ struct us_quic_socket_context_s; struct us_nq_driver_s; struct us_internal_loop_data_t { -#ifdef LIBUS_USE_LIBUV - struct us_timer_t *sweep_timer; -#else /* Absolute monotonic ns of the next sweep, or -1. Folded into the poll * timeout — no timerfd, no EVFILT_TIMER. */ long long sweep_next_tick_ns; -#endif int sweep_timer_count; struct us_internal_async *wakeup_async; struct us_socket_group_t *head; /* QUIC engines on this loop. us_quic_loop_process walks the list from - * loop_post / drainMicrotasks; the lazy fallthrough timer only wakes the - * loop for lsquic's time-driven state (RTO, ACK delay) — its callback - * just calls us_quic_loop_process. */ + * loop_post / drainMicrotasks; quic_next_tick_us bounds the loop's wait + * for lsquic's time-driven state (RTO, ACK delay). */ struct us_quic_socket_context_s *quic_head; /* µs until lsquic next wants process_conns (min earliest_adv_tick * across engines), or -1 for "no deadline". Written by @@ -64,12 +59,6 @@ struct us_internal_loop_data_t { * full process pass only when it flagged pending work -- one engine pass * per loop turn instead of one per native call. */ struct us_nq_driver_s *nq_head; -#ifdef LIBUS_USE_LIBUV - /* A fallthrough us_timer_t armed to quic_next_tick_us so the uv loop wakes - * for lsquic's time-driven state. POSIX folds the deadline into the - * epoll_pwait2 timeout via getTimeout() instead. */ - struct us_timer_t *quic_timer; -#endif struct us_socket_group_t *iterator; char *recv_buf; char *send_buf; diff --git a/packages/bun-usockets/src/internal/networking/bsd.h b/packages/bun-usockets/src/internal/networking/bsd.h index e269a5ca81c5..21cff7758b73 100644 --- a/packages/bun-usockets/src/internal/networking/bsd.h +++ b/packages/bun-usockets/src/internal/networking/bsd.h @@ -35,6 +35,8 @@ #pragma comment(lib, "ws2_32.lib") #define SETSOCKOPT_PTR_TYPE const char * #define LIBUS_SOCKET_ERROR INVALID_SOCKET +/* WSAStartup, once. Anything that reaches ws2_32 without going through bsd.c calls it first. */ +void us_internal_winsock_ensure(void); #else /* POSIX */ #ifndef _GNU_SOURCE #define _GNU_SOURCE diff --git a/packages/bun-usockets/src/libusockets.h b/packages/bun-usockets/src/libusockets.h index a623ac7ad2ac..0d6b7a8dbae5 100644 --- a/packages/bun-usockets/src/libusockets.h +++ b/packages/bun-usockets/src/libusockets.h @@ -171,7 +171,6 @@ enum { /* Library types publicly available */ struct us_socket_t; struct us_connecting_socket_t; -struct us_timer_t; struct us_socket_group_t; struct us_socket_vtable_t; struct us_listen_socket_t; @@ -245,28 +244,6 @@ void *us_udp_socket_user(struct us_udp_socket_t *s); /* Binds the UDP socket to an interface and port */ int us_udp_socket_bind(struct us_udp_socket_t *s, const char *hostname, unsigned int port); -/* Public interfaces for timers. libuv (Windows) only — epoll/kqueue schedules - * on bun.JSC.EventLoopTimer, no file descriptor or syscall. */ -#ifdef _WIN32 - -/* Create a new high precision, low performance timer. May fail and return null */ -struct us_timer_t *us_create_timer(us_loop_r loop, int fallthrough, unsigned int ext_size); - -/* Returns user data extension for this timer */ -void *us_timer_ext(struct us_timer_t *timer); - -/* */ -void us_timer_close(struct us_timer_t *timer, int fallthrough); - -/* Arm a timer with a delay from now and eventually a repeat delay. - * Specify 0 as repeat delay to disable repeating. Specify both 0 to disarm. */ -void us_timer_set(struct us_timer_t *timer, void (*cb)(struct us_timer_t *t), int ms, int repeat_ms); - -/* Returns the loop for this timer */ -struct us_loop_t *us_timer_loop(struct us_timer_t *t); - -#endif - /* ────────────────────────────────────────────────────────────────────────── * Socket groups & dispatch * @@ -583,7 +560,7 @@ void *us_socket_session_sink(us_socket_r s); /* Public interfaces for loops */ /* Returns a new event loop with user data extension */ -struct us_loop_t *us_create_loop(void *hint, void (*wakeup_cb)(us_loop_r loop), +struct us_loop_t *us_create_loop(void (*wakeup_cb)(us_loop_r loop), void (*pre_cb)(us_loop_r loop), void (*post_cb)(us_loop_r loop), unsigned int ext_size); /* Frees the loop immediately */ @@ -592,17 +569,15 @@ void us_loop_free(us_loop_r loop) nonnull_fn_decl; /* Returns the loop user data extension */ void *us_loop_ext(us_loop_r loop) nonnull_fn_decl; -/* Blocks the calling thread and drives the event loop until no more non-fallthrough polls are scheduled */ -void us_loop_run(us_loop_r loop) nonnull_fn_decl; - +struct timespec; +/* One iteration of the loop: waits for at most `timeout` (NULL: until something is ready) and dispatches what is ready. + * `now_ns` is the us_internal_monotonic_ns reading `timeout` was picked from, or 0 when there is none to share. */ +void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec *timeout, uint64_t now_ns); /* Signals the loop from any thread to wake up and execute its wakeup handler from the loop's own running thread. * This is the only fully thread-safe function and serves as the basis for thread safety */ void us_wakeup_loop(us_loop_r loop) nonnull_fn_decl; -/* Hook up timers in existing loop */ -void us_loop_integrate(us_loop_r loop) nonnull_fn_decl; - /* Public interfaces for polls */ /* A fallthrough poll does not keep the loop running, it falls through */ @@ -723,7 +698,6 @@ struct us_listen_socket_t *us_listen_socket_next(struct us_listen_socket_t *ls) LIBUS_SOCKET_DESCRIPTOR us_socket_get_fd(us_socket_r s) nonnull_fn_decl; /* Bun extras */ -struct us_socket_t *us_socket_pair(us_socket_group_r group, unsigned char kind, int socket_ext_size, LIBUS_SOCKET_DESCRIPTOR *fds) nonnull_fn_decl; struct us_socket_t *us_socket_from_fd(us_socket_group_r group, unsigned char kind, struct ssl_ctx_st *ssl_ctx, int socket_ext_size, LIBUS_SOCKET_DESCRIPTOR fd, int options, int ipc) __attribute__((nonnull(1))); /* ssl_ctx nullable */ struct us_socket_t *us_socket_open(struct us_socket_t *s, int is_client, char *ip, int ip_length); @@ -735,15 +709,12 @@ int us_socket_get_error(us_socket_r s); /* A writable event's write made zero progress: does that prove the peer is * gone? On epoll/kqueue a writable event implies real send-buffer space, so * no progress means the send itself failed (EPIPE/ECONNRESET folded to 0) and - * the answer is always yes. The libuv backend's completion model can deliver - * a writable completion for space the same loop iteration already refilled, - * making a stall there routine backpressure, so it asks the kernel + * the answer is always yes. On Windows a writable completion is dequeued + * after the fact and can describe space the same loop iteration already + * refilled, making a stall there routine backpressure, so it asks the kernel * (SO_ERROR, then a zero-byte send probe). */ int us_socket_stalled_write_means_peer_gone(us_socket_r s); -void us_socket_ref(us_socket_r s); -void us_socket_unref(us_socket_r s); - void us_socket_nodelay(us_socket_r s, int enabled); int us_socket_keepalive(us_socket_r s, int enabled, unsigned int delay); /* IP type-of-service (IPv4 IP_TOS / IPv6 IPV6_TCLASS). set returns 0 or a @@ -758,9 +729,9 @@ void us_socket_pause(us_socket_r s); #endif /* Decide what eventing system to use by default */ -#if !defined(LIBUS_USE_EPOLL) && !defined(LIBUS_USE_LIBUV) && !defined(LIBUS_USE_GCD) && !defined(LIBUS_USE_KQUEUE) && !defined(LIBUS_USE_ASIO) +#if !defined(LIBUS_USE_EPOLL) && !defined(LIBUS_USE_IOCP) && !defined(LIBUS_USE_KQUEUE) #if defined(_WIN32) -#define LIBUS_USE_LIBUV +#define LIBUS_USE_IOCP #elif defined(__APPLE__) || defined(__FreeBSD__) #define LIBUS_USE_KQUEUE #else diff --git a/packages/bun-usockets/src/loop.c b/packages/bun-usockets/src/loop.c index 135ec4c879f4..0e60561bf1a1 100644 --- a/packages/bun-usockets/src/loop.c +++ b/packages/bun-usockets/src/loop.c @@ -22,6 +22,7 @@ #include #include #include +#include #ifndef WIN32 #include #include @@ -48,37 +49,15 @@ extern const size_t Bun__lock__size; extern void Bun__internal_ensureDateHeaderTimerIsEnabled(struct us_loop_t *loop); -#ifdef LIBUS_USE_LIBUV - -void sweep_timer_cb(struct us_internal_callback_t *cb); - -// when the sweep timer is disabled, we don't need to do anything -void sweep_timer_noop(struct us_timer_t *timer) {} - -void us_internal_enable_sweep_timer(struct us_loop_t *loop) { - loop->data.sweep_timer_count++; - if (loop->data.sweep_timer_count == 1) { - us_timer_set(loop->data.sweep_timer, (void (*)(struct us_timer_t *)) sweep_timer_cb, LIBUS_TIMEOUT_GRANULARITY * 1000, LIBUS_TIMEOUT_GRANULARITY * 1000); - Bun__internal_ensureDateHeaderTimerIsEnabled(loop); - } -} - -void us_internal_disable_sweep_timer(struct us_loop_t *loop) { - loop->data.sweep_timer_count--; - if (loop->data.sweep_timer_count == 0) { - us_timer_set(loop->data.sweep_timer, (void (*)(struct us_timer_t *)) sweep_timer_noop, 0, 0); - } -} - -#else - #define LIBUS_TIMEOUT_GRANULARITY_NS ((long long) LIBUS_TIMEOUT_GRANULARITY * 1000000000LL) +#ifndef _WIN32 uint64_t us_internal_monotonic_ns(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec; } +#endif void us_internal_enable_sweep_timer(struct us_loop_t *loop) { loop->data.sweep_timer_count++; @@ -118,18 +97,21 @@ void us_internal_sweep_if_due(struct us_loop_t *loop) { us_internal_timer_sweep(loop); } -#endif - +void us_internal_idle_sweep(uint64_t now_ns) { + static const uint64_t idle_sweep_interval_ns = 100 * 1000000ULL; + static _Thread_local uint64_t last_idle_sweep_ns = 0; + const uint64_t sweep_now_ns = now_ns ? now_ns : us_internal_monotonic_ns(); + if (sweep_now_ns >= last_idle_sweep_ns + idle_sweep_interval_ns) { + last_idle_sweep_ns = sweep_now_ns; + mi_on_thread_idle(); + } +} /* -1 if the wakeup async cannot be created; nothing is left allocated in loop->data. */ int us_internal_loop_data_init(struct us_loop_t *loop, void (*wakeup_cb)(struct us_loop_t *loop), void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop)) { // We allocate with calloc, so we only need to initialize the specific fields in use. -#ifdef LIBUS_USE_LIBUV - loop->data.sweep_timer = us_create_timer(loop, 1, 0); -#else loop->data.sweep_next_tick_ns = -1; -#endif loop->data.sweep_timer_count = 0; loop->data.recv_buf = us_malloc(LIBUS_RECV_BUFFER_LENGTH + LIBUS_RECV_BUFFER_PADDING * 2); loop->data.send_buf = us_malloc(LIBUS_SEND_BUFFER_LENGTH); @@ -142,9 +124,6 @@ int us_internal_loop_data_init(struct us_loop_t *loop, void (*wakeup_cb)(struct if (!loop->data.wakeup_async) { us_free(loop->data.recv_buf); us_free(loop->data.send_buf); -#ifdef LIBUS_USE_LIBUV - us_timer_close(loop->data.sweep_timer, 0); -#endif return -1; } us_internal_async_set(loop->data.wakeup_async, (void (*)(struct us_internal_async *)) wakeup_cb); @@ -164,15 +143,12 @@ void us_internal_loop_data_free(struct us_loop_t *loop) { us_free(loop->data.recv_buf); us_free(loop->data.send_buf); -#ifdef LIBUS_USE_LIBUV - us_timer_close(loop->data.sweep_timer, 0); - if (loop->data.quic_timer) us_timer_close(loop->data.quic_timer, 0); -#endif us_internal_async_close(loop->data.wakeup_async); } __attribute__((always_inline)) void us_wakeup_loop(struct us_loop_t *loop) { -#ifndef LIBUS_USE_LIBUV +#ifndef LIBUS_USE_IOCP + /* A completion port's wakeup is a packet, which the tick sees on the port. */ __atomic_fetch_add(&loop->pending_wakeups, 1, __ATOMIC_RELEASE); #endif us_internal_async_wakeup(loop->data.wakeup_async); @@ -430,12 +406,6 @@ void us_internal_free_closed_sockets(struct us_loop_t *loop) { loop->data.closed_connecting_head = NULL; } -#ifdef LIBUS_USE_LIBUV -void sweep_timer_cb(struct us_internal_callback_t *cb) { - us_internal_timer_sweep(cb->loop); -} -#endif - /* These may have somewhat different meaning depending on the underlying event library */ void us_internal_loop_pre(struct us_loop_t *loop) { loop->data.iteration_nr++; @@ -469,24 +439,15 @@ void us_internal_loop_post(struct us_loop_t *loop) { loop->data.post_cb(loop); } -#ifdef WIN32 -#define us_ioctl ioctlsocket -#else -#define us_ioctl ioctl -#endif - void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, int events) { switch (us_internal_poll_type(p)) { case POLL_TYPE_CALLBACK: { struct us_internal_callback_t *cb = (struct us_internal_callback_t *) p; /* Timers, asyncs should accept (read), while UDP sockets should obviously not */ if (!cb->leave_poll_ready) { - /* Let's just have this macro to silence the CodeQL alert regarding empty function when using libuv */ - #ifndef LIBUS_USE_LIBUV us_internal_accept_poll_event(p); - #endif } - cb->cb(cb->cb_expects_the_loop ? (struct us_internal_callback_t *) cb->loop : (struct us_internal_callback_t *) &cb->p); + cb->cb((struct us_internal_callback_t *) cb->loop); break; } case POLL_TYPE_SEMI_SOCKET: { @@ -518,7 +479,7 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in struct us_loop_t *loop = accept_group->loop; struct bsd_addr_t addr; - LIBUS_SOCKET_DESCRIPTOR client_fd = bsd_accept_socket(us_poll_fd(p), &addr); + LIBUS_SOCKET_DESCRIPTOR client_fd = us_internal_accept(p, &addr); if (client_fd == LIBUS_SOCKET_ERROR) { /* Todo: start timer here */ @@ -585,7 +546,7 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in break; } - } while ((client_fd = bsd_accept_socket(us_poll_fd(p), &addr)) != LIBUS_SOCKET_ERROR); + } while ((client_fd = us_internal_accept(p, &addr)) != LIBUS_SOCKET_ERROR); } } break; @@ -772,8 +733,6 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in : us_dispatch_data(s, loop->data.recv_buf + LIBUS_RECV_BUFFER_PADDING, length); /* After socket adoption, track the new socket; the old one becomes invalid */ s = us_internal_socket_follow_adopted(s); - // loop->num_ready_polls isn't accessible on Windows. - #ifndef WIN32 // rare case: we're reading a lot of data, there's more to be read, and either: // - the socket has hung up, so we will never get more data from it (only applies to macOS, as macOS will send the event the same tick but Linux will not.) // - the event loop isn't very busy, so we can read multiple times in a row @@ -809,38 +768,6 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in } } #undef LOOP_ISNT_VERY_BUSY_THRESHOLD - #else - /* Windows eof-drain, same as the POSIX branch above: - * poll_cb maps AFD DISCONNECT to the eof hint for a - * socket whose write side we already shut down, and - * AFD reports DISCONNECT with the tail of the peer's - * stream still queued in the kernel. Stopping here - * lets the is_shut_down raw-close below discard it - * (a half-closed TLS/net client dropping the end of - * a large response on Windows only). recv() returning - * 0 or WSAEWOULDBLOCK ends the loop, so this is - * bounded by the kernel receive buffer. */ - if (s && !us_socket_is_closed(s) && (error || (!s->flags.is_paused && eof))) { - continue; - } - /* Windows AFD_POLL_ABORT is not level-triggered the way - * epoll's EPOLLHUP|EPOLLERR are: a peer RST that lands - * while this poll_cb is on the stack — typically when an - * on_data JS handler drainMicrotasks() into a same-process - * fetch().abort() so the http-client thread RSTs over - * loopback before we return — falls between the completed - * AFD ioctl and its re-submission, and the next AFD poll - * never reports it. recv() does: the socket is already in - * a reset state, so a single non-blocking probe yields - * WSAECONNRESET (→ close below) or 0 (→ eof) instead of - * relying on the re-armed poll. The common case is - * WSAEWOULDBLOCK → break, costing one extra syscall per - * readable event. Skip if on_data paused/closed us so we - * don't pull bytes the caller asked to defer. */ - if (s && !us_socket_is_closed(s) && !s->flags.is_paused && repeat_recv_count++ == 0) { - continue; - } - #endif } else if (!length) { eof = LIBUS_POLL_EOF; // lets handle EOF in the same place read_fin = 1; @@ -967,7 +894,7 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in * for an established TCP socket, so clamp them defensively. * The fallback must be in LIBUS_ERR's numbering (internal.h): * Windows does not reliably latch a received RST in SO_ERROR - * (see us_internal_libuv_peer_reset_probe), so it is taken + * (see us_internal_peer_reset_probe), so it is taken * there, and the CRT's ECONNRESET reached JS as ESHUTDOWN. */ int socket_error = us_socket_get_error(s); s = us_internal_socket_close_raw(s, socket_error > 2 ? socket_error : LIBUS_ECONNRESET, NULL); @@ -1121,13 +1048,6 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in } } -/* Integration only requires the timer to be set up, but not automatically enabled */ -void us_loop_integrate(struct us_loop_t *loop) { - /* Timer is now controlled dynamically by socket count, not enabled automatically */ -} - __attribute__((always_inline)) void *us_loop_ext(struct us_loop_t *loop) { return loop + 1; } - -#undef us_ioctl diff --git a/packages/bun-usockets/src/quic.c b/packages/bun-usockets/src/quic.c index d74117b4bb75..c4aba3e6ad26 100644 --- a/packages/bun-usockets/src/quic.c +++ b/packages/bun-usockets/src/quic.c @@ -151,12 +151,6 @@ struct us_quic_stream_s { * free the stream while a method is still touching it. */ -#ifdef LIBUS_USE_LIBUV -static void us_quic_on_timer(struct us_timer_t *t) { - us_quic_loop_process(us_timer_loop(t)); -} -#endif - static void us_quic_after_engine_call(us_quic_socket_context_t *ctx); /* lsquic forbids entering the engine from a callback it made. Every engine @@ -185,19 +179,10 @@ void us_quic_loop_process(struct us_loop_t *loop) { have_tick = 1; } } - /* Relative µs from now (≤0 means "tick due"). On epoll/kqueue, - * getTimeout() in src/runtime/timer/mod.rs folds this into the epoll_pwait2 timeout — - * no timerfd. On libuv there's no equivalent hook into the poll - * timeout, so arm a fallthrough uv_timer instead. */ + /* Relative µs from now (≤0 means "tick due"). getTimeout() in + * src/runtime/timer/mod.rs folds this into the loop's wait timeout — no + * timer object. */ loop->data.quic_next_tick_us = have_tick ? (min_diff < 0 ? 0 : min_diff) : -1; -#ifdef LIBUS_USE_LIBUV - if (have_tick) { - if (!loop->data.quic_timer) - loop->data.quic_timer = us_create_timer(loop, 1, 0); - int ms = min_diff <= 0 ? 1 : (min_diff + 999) / 1000; - us_timer_set(loop->data.quic_timer, us_quic_on_timer, ms, 0); - } -#endif } /* Called after the deferred-task queue drains. Only does work when a @@ -621,13 +606,8 @@ static lsquic_conn_ctx_t *us_quic_on_new_conn(void *if_ctx, lsquic_conn_t *conn) qs->ctx = ctx; /* QUIC connections share one UDP fd, so they aren't real polls. Count * each as a virtual poll so the loop stays alive while conns are open — - * the same invariant H1 gets from each TCP socket being a us_poll_t. - * libuv loop liveness is per-handle (uv_ref) rather than per-poll-count; - * the listen socket's uv_poll_t already keeps the loop alive until - * conn_count drops to 0 and we close it. */ -#ifndef LIBUS_USE_LIBUV + * the same invariant H1 gets from each TCP socket being a us_poll_t. */ ctx->loop->num_polls++; -#endif ctx->conn_count++; /* Arm the sweep-timer refcount so DateHeaderTimer runs for h3-only * servers; the TCP path does this per-socket in context.c. */ @@ -649,9 +629,7 @@ static void us_quic_on_conn_closed(lsquic_conn_t *conn) { } us_free(qs->hostname); us_free(qs); -#ifndef LIBUS_USE_LIBUV ctx->loop->num_polls--; -#endif ctx->conn_count--; us_internal_disable_sweep_timer(ctx->loop); /* During graceful drain the UDP fd is the only thing left holding the diff --git a/packages/bun-usockets/src/socket.c b/packages/bun-usockets/src/socket.c index 869cebe88f70..5e9a23d00a57 100644 --- a/packages/bun-usockets/src/socket.c +++ b/packages/bun-usockets/src/socket.c @@ -227,11 +227,7 @@ void us_connecting_socket_close(struct us_connecting_socket_t *c) { // already set (the callback has fired or is about to), in which case // after_resolve will see c->closed and finish teardown. if (c->addrinfo_req && Bun__addrinfo_cancel(c->addrinfo_req, c)) { -#ifdef _WIN32 - group->loop->uv_loop->active_handles--; -#else group->loop->num_polls--; -#endif c->pending_resolve_callback = 0; Bun__addrinfo_freeRequest(c->addrinfo_req, 0); c->addrinfo_req = 0; @@ -243,11 +239,7 @@ void us_connecting_socket_close(struct us_connecting_socket_t *c) { * see c->closed and only push c to the loop's closed list without * touching the (possibly freed) group. Balance the keep-alive here * for the same reason. */ -#ifdef _WIN32 - group->loop->uv_loop->active_handles--; -#else group->loop->num_polls--; -#endif us_dispatch_connecting_error(c, c->error); us_internal_connecting_socket_detach(c, group->loop); } @@ -319,8 +311,7 @@ static struct us_socket_t *us_internal_socket_close_and_notify(struct us_socket_ /* Mark the socket as closed */ s->flags.is_closed = 1; - /* The fd is closed first: a handler may dial again (node:net autoSelectFamily), and on - * libuv a second uv_poll_t started while this one is active never fires. */ + /* The fd is closed first: a handler may dial again (node:net autoSelectFamily). */ struct us_socket_t *res = s; if (!(us_internal_poll_type(&s->p) & POLL_TYPE_SEMI_SOCKET)) { res = s->ssl ? us_internal_ssl_on_close(s, code, reason) @@ -364,18 +355,6 @@ __attribute__((always_inline)) struct us_socket_t *us_socket_close(struct us_soc return us_internal_socket_close_raw(s, code, reason); } -struct us_socket_t *us_socket_pair(struct us_socket_group_t *group, unsigned char kind, int socket_ext_size, LIBUS_SOCKET_DESCRIPTOR *fds) { -#if defined(LIBUS_USE_LIBUV) || defined(WIN32) - return 0; -#else - if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0) { - return 0; - } - - return us_socket_from_fd(group, kind, NULL, socket_ext_size, fds[0], 0, 0); -#endif -} - /* Re-arm writable for a backpressured write without resuming the read side of * a paused socket: us_poll_change sets absolute flags, so including READABLE * unconditionally would silently undo us_socket_pause mid-backpressure and @@ -385,15 +364,24 @@ static void us_internal_rearm_writable(struct us_socket_t *s) { LIBUS_SOCKET_WRITABLE | ((s->flags.is_paused || s->read_eof) ? 0 : LIBUS_SOCKET_READABLE)); } +int us_socket_get_error(struct us_socket_t *s) { + int error = 0; + socklen_t len = sizeof(error); + if (getsockopt(us_poll_fd(&s->p), SOL_SOCKET, SO_ERROR, (char *) &error, &len) == -1) { + return LIBUS_ERR; + } + return error; +} + /* See libusockets.h: whether a zero-progress write on a writable event proves - * the peer is gone. Only the libuv backend has to ask the kernel. */ + * the peer is gone. Only Windows has to ask the kernel. */ int us_socket_stalled_write_means_peer_gone(struct us_socket_t *s) { -#ifdef LIBUS_USE_LIBUV +#ifdef _WIN32 if (us_socket_is_closed(s)) { return 1; } return us_socket_get_error(s) != 0 || - us_internal_libuv_peer_reset_probe(us_poll_fd(&s->p)); + us_internal_peer_reset_probe(us_poll_fd(&s->p)); #else return 1; #endif @@ -781,13 +769,6 @@ unsigned int us_get_local_address_info(char *buf, struct us_socket_t *s, const c return length; } -void us_socket_ref(struct us_socket_t *s) { -#ifdef LIBUS_USE_LIBUV - uv_ref((uv_handle_t *) s->p.uv_p); -#endif - // do nothing if not using libuv -} - void us_socket_nodelay(struct us_socket_t *s, int enabled) { if (!us_socket_is_shut_down(s)) { bsd_socket_nodelay(us_poll_fd((struct us_poll_t *) s), enabled); @@ -816,9 +797,7 @@ int us_socket_get_tos(struct us_socket_t *s) { /// Returns 0 on success. Returned error values depend on the platform. /// - on posix, returns `errno` -/// - on windows, when libuv is used, returns a UV err code -/// - on windows, LIBUS_USE_LIBUV is set, returns `WSAGetLastError()` -/// - on windows, otherwise returns result of `WSAGetLastError` +/// - on windows, returns `WSAGetLastError()` int us_socket_keepalive(us_socket_r s, int enabled, unsigned int delay) { if (!us_socket_is_shut_down(s)) { return bsd_socket_keepalive(us_poll_fd((struct us_poll_t *) s), enabled, delay); @@ -826,13 +805,6 @@ int us_socket_keepalive(us_socket_r s, int enabled, unsigned int delay) { return 0; } -void us_socket_unref(struct us_socket_t *s) { -#ifdef LIBUS_USE_LIBUV - uv_unref((uv_handle_t *) s->p.uv_p); -#endif - // do nothing if not using libuv -} - void us_socket_pause(struct us_socket_t *s) { if (s->flags.is_paused) return; // closed cannot be paused because it is already closed diff --git a/packages/bun-usockets/src/udp.c b/packages/bun-usockets/src/udp.c index d9a9bfc0fe14..e241092495e4 100644 --- a/packages/bun-usockets/src/udp.c +++ b/packages/bun-usockets/src/udp.c @@ -177,11 +177,7 @@ struct us_udp_socket_t *us_create_udp_socket_from_fd( ) { if (bsd_prepare_adopted_udp_socket(fd)) { if (err != NULL) { -#ifdef _WIN32 - *err = WSAGetLastError(); -#else - *err = errno; -#endif + *err = LIBUS_ERR; } return 0; } diff --git a/packages/bun-uws/src/App.h b/packages/bun-uws/src/App.h index 6396a840d0d3..f41c77e130d5 100644 --- a/packages/bun-uws/src/App.h +++ b/packages/bun-uws/src/App.h @@ -777,11 +777,6 @@ struct TemplatedApp { httpContext->enableNodeHttpCompat(); } - TemplatedApp &&run() { - uWS::run(); - return std::move(*this); - } - TemplatedApp &&setUsingCustomExpectHandler(bool value) { httpContext->getSocketContextData()->flags.usingCustomExpectHandler = value; return std::move(*this); diff --git a/packages/bun-uws/src/HttpContext.h b/packages/bun-uws/src/HttpContext.h index 9c041bdf611c..4b1888bad0ec 100644 --- a/packages/bun-uws/src/HttpContext.h +++ b/packages/bun-uws/src/HttpContext.h @@ -318,9 +318,6 @@ struct HttpContext { template static us_socket_t *onData(us_socket_t *s, char *data, int length) { - // ref the socket to make sure we process it entirely before it is closed - us_socket_ref(s); - // total overhead is about 210k down to 180k // ~210k req/sec is the original perf with write in data // ~200k req/sec is with cork and formatting @@ -334,9 +331,6 @@ struct HttpContext { bool isHalfOpenTunnel = false; if constexpr (IsNodeHttp) isHalfOpenTunnel = httpResponseData->isConnectRequest; if (us_socket_is_shut_down((us_socket_t *) s) && !isHalfOpenTunnel) { - /* Balance the us_socket_ref above — every other return path - * reaches the unref via returnedData. */ - us_socket_unref(s); return s; } @@ -353,17 +347,14 @@ struct HttpContext { unsigned int n = (unsigned int) length < 24u - matched ? (unsigned int) length : 24u - matched; bool isPrefix = memcmp(data, preface + matched, n) == 0; if (isPrefix && matched + n >= 4) { - us_socket_unref(s); return httpContextData->onHttp2(httpContextData->http2Context, s, data, length, matched); } if (isPrefix) { httpResponseData->h2PrefaceMatched = (unsigned char) (matched + n); - us_socket_unref(s); return s; } httpResponseData->h2PrefaceMatched = HttpResponseData::PROTOCOL_DECIDED; if (!httpContextData->allowHttp1) { - us_socket_unref(s); return rejectHttp1(s); } if (matched) { @@ -382,7 +373,6 @@ struct HttpContext { if (httpContextData->onHttp2 && httpResponseData->h2PrefaceMatched != HttpResponseData::PROTOCOL_DECIDED) { httpResponseData->h2PrefaceMatched = HttpResponseData::PROTOCOL_DECIDED; if (!httpContextData->allowHttp1) { - us_socket_unref(s); return rejectHttp1(s); } } @@ -394,13 +384,11 @@ struct HttpContext { * parsed as HTTP and keep flowing below. */ if constexpr (IsNodeHttp) { if ((httpResponseData->state & HttpResponseData::HTTP_NODE_PARSING_STOPPED) && !httpResponseData->isConnectRequest) { - us_socket_unref(s); return s; } /* Same for bytes that arrive behind a finished response that closes the * connection while its body is still draining (onWritable closes then). */ if (httpResponseData->isDrainingBeforeClose() && !httpResponseData->isConnectRequest) [[unlikely]] { - us_socket_unref(s); return s; } } @@ -446,7 +434,6 @@ struct HttpContext { if constexpr (!IsNodeHttp) { httpResponseData->sawConnectionClose = true; } - us_socket_unref((us_socket_t *) s); ((AsyncSocket *) s)->uncork(); ((HttpResponse *) s)->closeIfDoneAndMarked(httpResponseData); return nullptr; @@ -738,15 +725,10 @@ struct HttpContext { if (IsNodeHttp && httpContextData->onClientError) { httpResponseData->state |= HttpResponseData::HTTP_NODE_PARSING_STOPPED; httpContextData->onClientError(SSL, s, result.parserError, data, length); - if (!us_socket_is_closed(s)) { - /* Balance the parsing ref taken at the top of onData (the - * success path does this through returnedData). */ - us_socket_unref(s); - /* JavaScript destroyed the socket earlier in this read: run the deferred close now. */ - if (httpResponseData->state & HttpResponseData::HTTP_NODE_CLOSE_AFTER_MESSAGE) { - closeDestroyedNodeHttpSocket(s, httpResponseData); - return s; - } + /* JavaScript destroyed the socket earlier in this read: run the deferred close now. */ + if (!us_socket_is_closed(s) && (httpResponseData->state & HttpResponseData::HTTP_NODE_CLOSE_AFTER_MESSAGE)) { + closeDestroyedNodeHttpSocket(s, httpResponseData); + return s; } /* Flush anything the 'clientError' handler wrote (uncorking a * closed socket is a no-op). */ @@ -784,9 +766,6 @@ struct HttpContext { /* We need to uncork in all cases, except for nullptr (closed socket, or upgraded socket) */ if (returnedData != nullptr) { - /* We don't want open sockets to keep the event loop alive between HTTP requests */ - us_socket_unref((us_socket_t *) returnedData); - if constexpr (IsNodeHttp) { if (httpResponseData->state & HttpResponseData::HTTP_NODE_NOTIFY_READ_PARSED) { httpResponseData->state &= ~HttpResponseData::HTTP_NODE_NOTIFY_READ_PARSED; @@ -898,7 +877,7 @@ struct HttpContext { /* onEnd deferred close for these bytes; a writable event that * moves nothing (EPIPE) means the peer is gone and this would * otherwise spin the writable dispatch until idle timeout. - * Except on libuv, where a stale SEND completion can move + * Except on Windows, where a stale SEND completion can move * nothing on a healthy socket; there the kernel is asked. */ if (flushed == 0 && (httpResponseData->state & HttpResponseData::HTTP_NODE_RECEIVED_FIN) @@ -944,7 +923,7 @@ struct HttpContext { /* Bun.serve: onEnd deferred close for a tryEnd tail (offset < total, * nothing in AsyncSocketData::buffer). A retry that moves zero bytes * after the peer's FIN is EPIPE; close instead of spinning. Except - * on libuv, where the retry can stall while the TLS layer's spill + * on Windows, where the retry can stall while the TLS layer's spill * is still blocked on a healthy socket; there the kernel is asked. */ if ((httpResponseData->state & HttpResponseData::HTTP_NODE_RECEIVED_FIN) && (httpResponseData->state & HttpResponseData::HTTP_RESPONSE_PENDING) @@ -1274,24 +1253,16 @@ struct HttpContext { return options | LIBUS_LISTEN_DEFER_ACCEPT; } - static us_listen_socket_t *unrefListenSocket(us_listen_socket_t *socket) { - // we dont depend on libuv ref for keeping it alive - if (socket) { - us_socket_unref(&socket->s); - } - return socket; - } - /* Listen to port using this HttpContext. ssl_ctx may be nullptr for plain HTTP. */ us_listen_socket_t *listen(struct ssl_ctx_st *sslCtx, const char *host, int port, int options) { int error = 0; - return unrefListenSocket(us_socket_group_listen(&group, socketKind(), sslCtx, host, port, tcpListenOptions(options), socketExtSize(), &error)); + return us_socket_group_listen(&group, socketKind(), sslCtx, host, port, tcpListenOptions(options), socketExtSize(), &error); } /* Listen to unix domain socket using this HttpContext */ us_listen_socket_t *listen_unix(struct ssl_ctx_st *sslCtx, const char *path, size_t pathlen, int options) { int error = 0; - return unrefListenSocket(us_socket_group_listen_unix(&group, socketKind(), sslCtx, path, pathlen, options, socketExtSize(), &error)); + return us_socket_group_listen_unix(&group, socketKind(), sslCtx, path, pathlen, options, socketExtSize(), &error); } }; diff --git a/packages/bun-uws/src/Loop.h b/packages/bun-uws/src/Loop.h index a8de89601228..e42302c0c94e 100644 --- a/packages/bun-uws/src/Loop.h +++ b/packages/bun-uws/src/Loop.h @@ -19,7 +19,7 @@ #ifndef UWS_LOOP_H #define UWS_LOOP_H -/* The loop is lazily created per-thread and run with run() */ +/* The loop is lazily created per-thread */ #include "LoopData.h" #include @@ -83,8 +83,8 @@ struct Loop { return this; } - static Loop *create(void *hint) { - Loop *loop = (Loop *) us_create_loop(hint, wakeupCb, preCb, postCb, sizeof(LoopData)); + static Loop *create() { + Loop *loop = (Loop *) us_create_loop(wakeupCb, preCb, postCb, sizeof(LoopData)); if (!loop) { /* The per-thread loop is not recoverable; every caller of get() * dereferences it. Only Bun.spawnSync's isolated loop (created @@ -95,19 +95,14 @@ struct Loop { return loop->init(); } - /* What to do with loops created with existingNativeLoop? */ struct LoopCleaner { ~LoopCleaner() { // There's no need to call this destructor if Bun is in the process of exiting. - // This is both a performance thing, and also to prevent freeing some things which are not meant to be freed - // such as uv_tty_t - if(loop && cleanMe && !bun_is_exiting()) { - cleanMe = false; + if(loop && !bun_is_exiting()) { loop->free(); } } Loop *loop = nullptr; - bool cleanMe = false; }; static LoopCleaner &getLazyLoop() { @@ -118,25 +113,15 @@ struct Loop { public: /* Lazily initializes a per-thread loop and returns it. * Will automatically free all initialized loops at exit. */ - static Loop *get(void *existingNativeLoop = nullptr) { + static Loop *get() { if (!getLazyLoop().loop) { - /* If we are given a native loop pointer we pass that to uSockets and let it deal with it */ - if (existingNativeLoop) { - /* Todo: here we want to pass the pointer, not a boolean */ - getLazyLoop().loop = create(existingNativeLoop); - /* We cannot register automatic free here, must be manually done */ - } else { - getLazyLoop().loop = create(nullptr); - getLazyLoop().cleanMe = true; - } + getLazyLoop().loop = create(); } return getLazyLoop().loop; } - /* A thread that ran a loop is exiting: free this thread's loop whether uSockets created the - * native loop (cleanMe) or was handed one (Windows: the thread's libuv loop, which the caller - * closes afterwards; us_loop_free leaves a borrowed native loop alone). */ + /* A thread that ran a loop is exiting: free this thread's loop. */ static void freeLoopAtThreadExit() { if (getLazyLoop().loop) { getLazyLoop().loop->free(); @@ -148,7 +133,6 @@ struct Loop { LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this); loopData->~LoopData(); - /* uSockets will track whether this loop is owned by us or a borrowed alien loop */ us_loop_free((us_loop_t *) this); /* Reset lazyLoop */ @@ -197,18 +181,8 @@ struct Loop { us_wakeup_loop((us_loop_t *) this); } - /* Actively block and run this loop */ - void run() { - us_loop_run((us_loop_t *) this); - } - }; -/* Can be called from any thread to run the thread local loop */ -inline void run() { - Loop::get()->run(); -} - } #endif // UWS_LOOP_H diff --git a/patches/libuv/win-poll-abort-with-disconnect.patch b/patches/libuv/win-poll-abort-with-disconnect.patch deleted file mode 100644 index 66ccc50537de..000000000000 --- a/patches/libuv/win-poll-abort-with-disconnect.patch +++ /dev/null @@ -1,51 +0,0 @@ ---- a/src/win/poll.c -+++ b/src/win/poll.c -@@ -115,9 +115,24 @@ - AFD_POLL_DISCONNECT | AFD_POLL_ACCEPT | AFD_POLL_ABORT; - } else { - if (handle->events & UV_DISCONNECT) { -- afd_poll_info->Handles[0].Events |= AFD_POLL_DISCONNECT; -+ /* A peer abort (RST) must wake a watcher that asked for disconnect -+ * even when it is not reading: AFD only reports subscribed events, so -+ * without ABORT here a reset against a write-only poll is never -+ * delivered and teardown waits forever (see the reporting hunk). */ -+ afd_poll_info->Handles[0].Events |= AFD_POLL_DISCONNECT | -+ AFD_POLL_ABORT; - } - } -+ if (handle->events & UV_PRIORITIZED) { -+ /* ABORT-only subscription. A graceful FIN leaves AFD_POLL_DISCONNECT -+ * signaled forever (AFD is level-triggered), so a watcher that already -+ * consumed the FIN cannot keep DISCONNECT armed without completing -+ * instantly in a loop - yet it still needs the peer's later RST. -+ * UV_PRIORITIZED, unused on Windows, subscribes exactly that remaining -+ * signal; the reporting below surfaces it as UV_PRIORITIZED so the -+ * requested-events mask keeps it. */ -+ afd_poll_info->Handles[0].Events |= AFD_POLL_ABORT; -+ } - if (handle->events & UV_WRITABLE) { - afd_poll_info->Handles[0].Events |= AFD_POLL_SEND | AFD_POLL_CONNECT_FAIL; - } -@@ -168,9 +183,19 @@ - if ((afd_poll_info->Handles[0].Events & (AFD_POLL_RECEIVE | - AFD_POLL_DISCONNECT | AFD_POLL_ACCEPT | AFD_POLL_ABORT)) != 0) { - events |= UV_READABLE; -- if ((afd_poll_info->Handles[0].Events & AFD_POLL_DISCONNECT) != 0) { -- events |= UV_DISCONNECT; -- } -+ } -+ if ((afd_poll_info->Handles[0].Events & -+ (AFD_POLL_DISCONNECT | AFD_POLL_ABORT)) != 0) { -+ /* Graceful FIN and abortive RST both mean the peer is gone. Report -+ * DISCONNECT for either, outside the READABLE block, so it survives -+ * the requested-events mask when only UV_DISCONNECT is armed. */ -+ events |= UV_DISCONNECT; -+ } -+ if ((afd_poll_info->Handles[0].Events & AFD_POLL_ABORT) != 0) { -+ /* The ABORT-only subscription reports as UV_PRIORITIZED: a watcher -+ * armed with UV_PRIORITIZED alone has UV_DISCONNECT stripped by the -+ * requested-events mask below. */ -+ events |= UV_PRIORITIZED; - } - if ((afd_poll_info->Handles[0].Events & (AFD_POLL_SEND | - AFD_POLL_CONNECT_FAIL)) != 0) { diff --git a/patches/libuv/win-poll-rearm-before-callback.patch b/patches/libuv/win-poll-rearm-before-callback.patch deleted file mode 100644 index d66234b8135f..000000000000 --- a/patches/libuv/win-poll-rearm-before-callback.patch +++ /dev/null @@ -1,36 +0,0 @@ ---- a/src/win/poll.c -+++ b/src/win/poll.c -@@ -186,11 +186,33 @@ - uv__handle_stop(handle); - } - -+ /* Re-arm the AFD poll **before** invoking poll_cb so an AFD ioctl is -+ * pending while user code runs. AFD is level-triggered (ReactOS -+ * AfdSelect: `Events & FCB->PollState` checked on IRP arrival), so a -+ * RST that lands during poll_cb is caught by the freshly-submitted req. -+ * Re-arming *after* poll_cb (the previous order) leaves a gap: an -+ * in-process loopback peer that RSTs from inside the callback — -+ * e.g. a JS handler that drainMicrotasks() into a same-process -+ * fetch().abort() — can land between completion and re-submission, -+ * and although AFD_POLL_ABORT is in PollState, no ioctl is pending -+ * to observe it until after the callback returns. wepoll does the -+ * same re-arm-before-return (port_wait → port__update_events_if_polling -+ * before LeaveCriticalSection). The dual-req + mask_events bookkeeping -+ * already handles poll_cb calling uv_poll_start/stop mid-flight. */ -+ if ((handle->events & ~(handle->submitted_events_1 | -+ handle->submitted_events_2)) != 0) { -+ uv__fast_poll_submit_poll_req(loop, handle); -+ } -+ - if (events != 0) { - handle->poll_cb(handle, 0, events); - } - } - -+ /* poll_cb may have called uv_poll_start (covered above by the dual-req -+ * submitting the other slot) or uv_poll_stop / uv_close. Only the -+ * close-endgame check needs to run post-callback. A re-submit here is -+ * still needed for the `events == 0` path (filtered-out completion). */ - if ((handle->events & ~(handle->submitted_events_1 | - handle->submitted_events_2)) != 0) { - uv__fast_poll_submit_poll_req(loop, handle); diff --git a/scripts/build/bun.ts b/scripts/build/bun.ts index 94b2d70c888e..ee31676be0eb 100644 --- a/scripts/build/bun.ts +++ b/scripts/build/bun.ts @@ -109,6 +109,8 @@ function systemLibs(cfg: Config): string[] { "crypt32.lib", "wsock32.lib", // ws2_32 + wsock32 — wsock32 has TransmitFile (sendfile equiv) "ws2_32.lib", + "iphlpapi.lib", + "advapi32.lib", "delayimp.lib", // required for /delayload: in release ); } @@ -325,7 +327,7 @@ export function emitBun(n: Ninja, cfg: Config, sources: Sources): BunOutput { // effect of the edge whose declared outputs are only lib*.a. Depfiles record // those headers, but ninja stats them BEFORE the sub-build runs — so with // order-only, any compile that #includes a dep header lags one build behind - // a dep rebuild (observed: uv-posix-*.c → wtf/Compiler.h). + // a dep rebuild (observed: uv-*.c → wtf/Compiler.h). // Implicit deps on the libs make "dep rebuilt" itself the invalidation // signal. Cost is negligible: if the libs changed you're relinking anyway. // diff --git a/scripts/build/deps/README.md b/scripts/build/deps/README.md index 0629ef5d8674..f6e6a75e5d5a 100644 --- a/scripts/build/deps/README.md +++ b/scripts/build/deps/README.md @@ -50,7 +50,7 @@ which is the cue to diff the upstream `CMakeLists.txt`. ## Iterating on a dep from a local checkout -To hack on a vendored dep (e.g. chase a bug in the mimalloc or libuv fork) +To hack on a vendored dep (e.g. chase a bug in the mimalloc fork) without cutting a commit and bumping the pin each round, point the build at a git clone: @@ -149,9 +149,9 @@ export const mydep: Dependency = { - **tinycc.ts** — direct build with a build-time codegen tool - **zlib.ts** — direct build with per-source SIMD `-m` flags + `.h.in` substitution - **libarchive.ts** / **cares.ts** — direct build with hand-written per-target config.h -- **boringssl.ts** — direct build with NASM assembly (win-x64) and a large gen/ manifest; `forbidUndefined` (with libuv.ts) keeps a dep that bun points at mimalloc from calling libc's allocator behind its back +- **boringssl.ts** — direct build with NASM assembly (win-x64) and a large gen/ manifest; `forbidUndefined` keeps a dep that bun points at mimalloc from calling libc's allocator behind its back - **sqlite.ts** — direct build, in-tree source (lives in `src/`, not `vendor/`) -- **libuv.ts** — `enabled: cfg => cfg.windows` for a platform-only dep +- **tinycc.ts** / **sqlite.ts** — `enabled: cfg => …` for a dep that only some configurations build - **lolhtml.ts** — cargo build with rustflags - **webkit.ts** — `nested-cmake` (`sourceSubdir`, `preBuild`) and `prebuilt` diff --git a/scripts/build/deps/boringssl.ts b/scripts/build/deps/boringssl.ts index 9db3b212578d..7565e5efa7cc 100644 --- a/scripts/build/deps/boringssl.ts +++ b/scripts/build/deps/boringssl.ts @@ -91,7 +91,7 @@ export const boringssl: Dependency = { // With the hooks on, nothing in BoringSSL may allocate from libc any // more (mem.cc's fallback is dead code the compiler drops); under ASAN // the fallbacks are live and libc is the point. - ...(!cfg.asan && { forbidUndefined: { symbols: LIBC_ALLOCATION_SYMBOLS } }), + ...(!cfg.asan && { forbidUndefined: LIBC_ALLOCATION_SYMBOLS }), }; return spec; }, diff --git a/scripts/build/deps/index.ts b/scripts/build/deps/index.ts index 4084e71f876c..19174b9058ae 100644 --- a/scripts/build/deps/index.ts +++ b/scripts/build/deps/index.ts @@ -20,7 +20,6 @@ import { libarchive } from "./libarchive.ts"; import { libdeflate } from "./libdeflate.ts"; import { libjpegTurbo } from "./libjpeg-turbo.ts"; import { libspng } from "./libspng.ts"; -import { libuv } from "./libuv.ts"; import { libwebp } from "./libwebp.ts"; import { lolhtml } from "./lolhtml.ts"; import { lshpack } from "./lshpack.ts"; @@ -60,7 +59,6 @@ export const allDeps: readonly Dependency[] = [ cares, hdrhistogram, highway, - libuv, lolhtml, rustArgon2, lshpack, @@ -89,7 +87,6 @@ export { libdeflate, libjpegTurbo, libspng, - libuv, libwebp, lolhtml, lshpack, diff --git a/scripts/build/deps/libuv.ts b/scripts/build/deps/libuv.ts deleted file mode 100644 index edcd6bf3b4cb..000000000000 --- a/scripts/build/deps/libuv.ts +++ /dev/null @@ -1,93 +0,0 @@ -/** - * libuv — cross-platform async I/O. Bun uses it on Windows ONLY, for the - * event loop and file I/O (Windows' IOCP model needs a proper abstraction - * layer). On unix, bun's event loop is custom (kqueue/epoll direct). - * - * On POSIX, node-api addons that reference libuv symbols are served by - * src/jsc/bindings/uv-posix-stubs.c + uv-posix-polyfills*.c, with headers - * from src/jsc/bindings/libuv/ (see flags.ts) — vendor libuv is not built. - */ - -import type { Dependency } from "../source.ts"; -import { LIBC_ALLOCATION_SYMBOLS } from "../source.ts"; - -// Tip of oven-sh/libuv's `bun` branch: upstream f3ce527e + win-pipe CancelIoEx -// fix, ConPTY uv_spawn, AppContainer pipe namespace (oven-sh/libuv#7), fs/tty -// fixes (oven-sh/libuv#8), high-res poll timeouts (oven-sh/libuv#9), -// FileModeInformation error return (oven-sh/libuv#10), error translation / -// propagation audit fixes (oven-sh/libuv#11), uv_spawn returns an error -// instead of aborting on AssignProcessToJobObject failure (oven-sh/libuv#12), -// closes the process/thread handles on that error path (oven-sh/libuv#13), and -// uv__split_path allocates with uv__malloc instead of _wcsdup so the buffer -// can be uv__free'd under uv_replace_allocator (oven-sh/libuv#14), Winsock / -// console-resize watcher / suspend-resume detection initialized on first use -// instead of in uv__init, with uv__winsock_ensure() for callers that reach -// ws2_32 directly (oven-sh/libuv#15), and LoadLibraryExW for those lazy -// loads (oven-sh/libuv#16). -// To bump, update `bun`. -const LIBUV_COMMIT = "8023581113b276e7c1aee3f82da57ca0893faab1"; - -// prettier-ignore -const SHARED = [ - "fs-poll", "idna", "inet", "random", "strscpy", "strtok", "thread-common", - "threadpool", "timer", "uv-common", "uv-data-getter-setters", "version", -]; - -// prettier-ignore -const WIN = [ - "async", "core", "detect-wakeup", "dl", "error", "fs", "fs-event", - "getaddrinfo", "getnameinfo", "handle", "loop-watcher", "pipe", "thread", - "poll", "process", "process-stdio", "signal", "snprintf", "stream", "tcp", - "tty", "udp", "util", "winapi", "winsock", -]; - -export const libuv: Dependency = { - name: "libuv", - - enabled: cfg => cfg.windows, - - source: () => ({ - kind: "github-archive", - repo: "oven-sh/libuv", - commit: LIBUV_COMMIT, - }), - - // Re-arm the AFD ioctl before poll_cb (matching wepoll's - // port__update_events_if_polling-before-return). AFD is level-triggered - // (ReactOS AfdSelect: `Events & FCB->PollState` checked on IRP arrival), - // so a peer RST that lands during poll_cb is caught by the freshly- - // submitted req. Upstream libuv re-arms *after* poll_cb, leaving a gap - // an in-process loopback fetch().abort() can fall into. To upstream: - // send to libuv/libuv with the wepoll/ReactOS references in the patch - // comment as the rationale. - patches: ["patches/libuv/win-poll-rearm-before-callback.patch", "patches/libuv/win-poll-abort-with-disconnect.patch"], - - build: () => ({ - kind: "direct", - sources: [...SHARED.map(s => `src/${s}.c`), ...WIN.map(s => `src/win/${s}.c`)], - includes: ["include", "src"], - defines: { - WIN32_LEAN_AND_MEAN: true, - _CRT_DECLARE_NONSTDC_NAMES: 0, - WIN32: true, - _WINDOWS: true, - }, - cflags: [ - // Hex literal required — sdkddkver.h token-pastes `ver##0000`. - "-D_WIN32_WINNT=0x0A00", - "/clang:-fno-strict-aliasing", - "-Wno-int-conversion", - "/wd4996", - ], - // bun swaps in mimalloc with uv_replace_allocator (src/runtime/bin_entry/mod.rs), so - // everything has to allocate through uv__malloc and friends; uv-common.c - // is the default table that swap replaces. The one stray CRT call there - // was got uv__free'd and crashed (oven-sh/libuv#14). - forbidUndefined: { symbols: LIBC_ALLOCATION_SYMBOLS, except: ["src/uv-common.c"] }, - }), - - provides: () => ({ - libs: [], - includes: ["include"], - }), -}; diff --git a/scripts/build/deps/mimalloc.ts b/scripts/build/deps/mimalloc.ts index 2984efa98b8a..c53c2e86925b 100644 --- a/scripts/build/deps/mimalloc.ts +++ b/scripts/build/deps/mimalloc.ts @@ -35,7 +35,7 @@ export const mimalloc: Dependency = { // alloc-override.c emits _expand/_msize/free which duplicate // against libucrt(d) at link time. The C deps that would // otherwise sit on the uCRT heap are pointed at mimalloc one - // by one instead (ICU and libuv in bun_runtime::bin_entry's + // by one instead (ICU in bun_runtime::bin_entry's // use_mimalloc_in_dependencies, BoringSSL via the hooks in // boringssl.ts, c-ares via ares_library_init_mem). const override = cfg.linux && !cfg.asan; diff --git a/scripts/build/deps/nodejs-headers.ts b/scripts/build/deps/nodejs-headers.ts index 704cfebe1932..716add4db7f0 100644 --- a/scripts/build/deps/nodejs-headers.ts +++ b/scripts/build/deps/nodejs-headers.ts @@ -3,7 +3,8 @@ * * Downloaded from nodejs.org releases. Headers-only (no libs). After * extraction we delete `openssl/` and `uv/` subdirs — bun uses BoringSSL - * (not OpenSSL) and its own libuv, and the bundled headers conflict. + * (not OpenSSL) and its own uv/errno.h (src/jsc/bindings/libuv), and the + * bundled headers conflict. */ import { resolve } from "node:path"; @@ -24,6 +25,9 @@ export const NODEJS_ABI_VERSION = "147"; /** V8 version reported by process.versions.v8 — must match the pinned Node.js version's. */ export const NODEJS_V8_VERSION = "14.6.202.34-node.20"; +/** libuv version reported by process.versions.uv — the pinned Node.js version's deps/uv/include/uv/version.h. */ +export const NODEJS_UV_VERSION = "1.52.1"; + export const nodejsHeaders: Dependency = { name: "nodejs", @@ -31,7 +35,7 @@ export const nodejsHeaders: Dependency = { kind: "prebuilt", url: `https://nodejs.org/dist/v${cfg.nodejsVersion}/node-v${cfg.nodejsVersion}-headers.tar.gz`, identity: cfg.nodejsVersion, - // Delete headers that conflict with BoringSSL / our libuv. + // Delete headers that conflict with BoringSSL / our uv/errno.h. // Tarball top-level is `node-v/` (hoisted), inside is `include/node/`. rmAfterExtract: ["include/node/openssl", "include/node/uv", "include/node/uv.h"], destDir: resolve(cfg.cacheDir, `nodejs-headers-${cfg.nodejsVersion}`), diff --git a/scripts/build/fetch-cli.ts b/scripts/build/fetch-cli.ts index eebc97cc79e0..a4dd60cb4261 100644 --- a/scripts/build/fetch-cli.ts +++ b/scripts/build/fetch-cli.ts @@ -184,7 +184,7 @@ function checkUndefined(name: string, nm: string, rspfile: string, stamp: string ); if (offenders.length > 0) { throw new BuildError(`${name}: objects reference symbols its build forbids:\n${offenders.join("\n")}`, { - hint: `The symbols and the objects allowed to use them are declared by forbidUndefined in scripts/build/deps/${name}.ts; the comment there says what the references have to go through instead.`, + hint: `The symbols are declared by forbidUndefined in scripts/build/deps/${name}.ts; the comment there says what the references have to go through instead.`, }); } // dep_check_undefined is restat=1: an existing stamp keeps its mtime. diff --git a/scripts/build/flags.ts b/scripts/build/flags.ts index 5aaf0b932a7f..65834e91ea76 100644 --- a/scripts/build/flags.ts +++ b/scripts/build/flags.ts @@ -13,6 +13,7 @@ import { join } from "node:path"; import { bunExeName, type Config } from "./config.ts"; +import { NODEJS_UV_VERSION } from "./deps/nodejs-headers.ts"; import { quote, slash } from "./shell.ts"; import { ucrtServicingLibDir } from "./winsysroot.ts"; @@ -449,12 +450,6 @@ export const globalFlags: Flag[] = [ when: c => c.unix, desc: "Skip unwind tables (we don't use C++ exceptions)", }, - { - // libuv stubs use C23 anonymous parameters - flag: "-Wno-c23-extensions", - when: c => c.unix, - desc: "Allow C23 extensions (libuv stubs use anonymous parameters)", - }, // ─── Sections (enables dead-code stripping at link) ─── { @@ -794,6 +789,10 @@ export const defines: Flag[] = [ flag: c => `REPORTED_NODEJS_V8_VERSION=\\"${c.nodejsV8Version}\\"`, desc: "V8 version string (process.versions.v8)", }, + { + flag: `REPORTED_NODEJS_UV_VERSION=\\"${NODEJS_UV_VERSION}\\"`, + desc: "libuv version string (process.versions.uv)", + }, { // Hardcoded ON — experimental flag not exposed in config flag: "USE_BUN_MIMALLOC=1", @@ -1739,10 +1738,9 @@ export function bunIncludes(cfg: Config): string[] { if (cfg.windows) { includes.push(join(cwd, "src/jsc/bindings/windows")); - } else { - // libuv stubs for unix (real libuv used on windows) - includes.push(join(cwd, "src/jsc/bindings/libuv")); } + // : the UV__E* numbers. + includes.push(join(cwd, "src/jsc/bindings/libuv")); // musl doesn't ship sys/queue.h (glibc-only BSDism). lshpack bundles // a compat copy for this case. diff --git a/scripts/build/source.ts b/scripts/build/source.ts index ee3163e9f42e..f405b9649628 100644 --- a/scripts/build/source.ts +++ b/scripts/build/source.ts @@ -144,7 +144,7 @@ export type Source = * Paths to delete (relative to destDir) after extraction. WebKit * deletes `include/unicode` on macOS (conflicts with system ICU * headers); nodejs-headers deletes openssl/uv (conflict with - * BoringSSL/our libuv). Most deps won't need it. + * BoringSSL/our uv headers). Most deps won't need it. * * Paths, not a shell command — cross-platform via fs.rm, no quoting * through ninja. @@ -255,19 +255,13 @@ export interface DirectBuild { codegen?: DirectCodegen; /** * Fail the build if any object of this dep still has an undefined - * reference to one of `symbols` (llvm-nm over the objects, once they - * exist). `except` lists sources, as spelled in `sources`, that may - * reference them. Names are matched with and without the Mach-O leading + * reference to one of these symbols (llvm-nm over the objects, once they + * exist). Names are matched with and without the Mach-O leading * underscore. The use so far: deps whose allocations bun routes to * mimalloc must not reach the C library's allocator behind its back, see * LIBC_ALLOCATION_SYMBOLS. Skipped when llvm-nm was not found (cfg.nm). */ - forbidUndefined?: ForbidUndefined; -} - -export interface ForbidUndefined { - symbols: readonly string[]; - except?: readonly string[]; + forbidUndefined?: readonly string[]; } /** @@ -452,7 +446,6 @@ export type DepName = | "libdeflate" | "libjpeg-turbo" | "libspng" - | "libuv" | "libwebp" | "lolhtml" | "lshpack" @@ -511,7 +504,7 @@ export interface Dependency { /** * Whether this dep participates in the build at all. Defaults to always-on. - * E.g. libuv is windows-only, tinycc is disabled on Android/FreeBSD. + * E.g. tinycc is disabled on Android/FreeBSD. */ enabled?: (cfg: Config) => boolean; @@ -1699,10 +1692,8 @@ function emitDirect( } /** - * The `forbidUndefined` edge: every object except the `except` sources' goes - * in, the stamp comes out. Returns the stamp, or nothing when llvm-nm is - * unavailable. The objects are in `spec.sources` order, which is how the - * exceptions are mapped onto them. + * The `forbidUndefined` edge: every object goes in, the stamp comes out. + * Returns the stamp, or nothing when llvm-nm is unavailable. */ function emitForbidUndefined( n: Ninja, @@ -1712,17 +1703,8 @@ function emitForbidUndefined( objects: string[], buildDir: string, ): string[] { - const { symbols, except = [] } = spec.forbidUndefined!; - assert(symbols.length > 0, `${name}: forbidUndefined.symbols is empty`); - const sourcePaths = spec.sources.map(src => (typeof src === "string" ? src : src.path)); - for (const exception of except) { - assert( - sourcePaths.includes(exception), - `${name}: forbidUndefined.except lists ${exception}, which is not one of its sources`, - ); - } - const checked = objects.filter((_, i) => !except.includes(sourcePaths[i]!)); - assert(checked.length > 0, `${name}: forbidUndefined excepts every source`); + const symbols = spec.forbidUndefined!; + assert(symbols.length > 0, `${name}: forbidUndefined is empty`); if (cfg.nm === undefined) return []; mkdirSync(buildDir, { recursive: true }); @@ -1730,7 +1712,7 @@ function emitForbidUndefined( n.build({ outputs: [stamp], rule: "dep_check_undefined", - inputs: checked, + inputs: objects, implicitInputs: [fetchCliPath], vars: { name, nm: quote(cfg.nm, cfg.host.os === "windows"), symbols: symbols.join(",") }, }); diff --git a/scripts/build/unified.ts b/scripts/build/unified.ts index 18eb4abcef62..bddf915ccd10 100644 --- a/scripts/build/unified.ts +++ b/scripts/build/unified.ts @@ -109,6 +109,12 @@ const noUnify: readonly string[] = [ // `fs.constants`, breaking Node.js parity and test/js/node/fs/fs.test.ts. "src/jsc/bindings/ProcessBindingConstants.cpp", + // declares a global `STRING` and then names it unqualified, + // which is ambiguous with JSC::STRING once an earlier sibling's + // `using namespace JSC` is in effect. + "src/jsc/bindings/BunWindowsProcess.cpp", + "src/jsc/bindings/OsBinding.cpp", + // Defines extern "C" replacements for platform symbols (strncasecmp, // fstat64, environ, ...). On Windows an earlier sibling can leak // `#define strncasecmp _strnicmp`, turning the definition here into a diff --git a/scripts/glob-sources.ts b/scripts/glob-sources.ts index 19878c368d76..b3a293e7a5af 100644 --- a/scripts/glob-sources.ts +++ b/scripts/glob-sources.ts @@ -120,8 +120,8 @@ const patterns = { "packages/bun-usockets/src/*.c", "packages/bun-usockets/src/eventing/*.c", "packages/bun-usockets/src/crypto/*.c", - "src/jsc/bindings/uv-posix-polyfills.c", - "src/jsc/bindings/uv-posix-stubs.c", + "src/jsc/bindings/uv-polyfills.c", + "src/jsc/bindings/uv-stubs.c", "src/jsc/bindings/node/http/llhttp/*.c", ], }, diff --git a/scripts/orderfile/functrace-windows.c b/scripts/orderfile/functrace-windows.c index 1d34712b4612..c84edbe193fc 100644 --- a/scripts/orderfile/functrace-windows.c +++ b/scripts/orderfile/functrace-windows.c @@ -15,7 +15,7 @@ // With BUN_FUNCTRACE_TTY set, the debuggee is started on a pseudo console // rather than our stdio, as ptyrun.c starts its child on a pty: our stdin is // typed into it and whatever it writes is forwarded to our stdout. Console -// stdio is a different path through bun (libuv's tty layer, console modes, +// stdio is a different path through bun (its tty layer, console modes, // WriteConsole) than a pipe, and the only way to reach it is to be a console. // // Function starts arrive as link-time addresses (a PE carries no symbol table; diff --git a/scripts/run-clang-format.sh b/scripts/run-clang-format.sh index 9043abf55f3b..ff6ed27d0692 100755 --- a/scripts/run-clang-format.sh +++ b/scripts/run-clang-format.sh @@ -36,7 +36,6 @@ while IFS= read -r -d '' file; do [[ "$file" =~ src/jsc/bindings/libuv/ ]] || \ [[ "$file" =~ src/jsc/bindings/sqlite/ ]] || \ [[ "$file" =~ src/jsc/bindings/node/http/llhttp/ ]] || \ - [[ "$file" =~ packages/bun-usockets/.*libuv ]] || \ [[ "$file" =~ vendor/ ]]; then continue fi diff --git a/src/bun_core/Global.rs b/src/bun_core/Global.rs index cd21cf9c49dc..aae276041e2b 100644 --- a/src/bun_core/Global.rs +++ b/src/bun_core/Global.rs @@ -757,16 +757,9 @@ pub fn raise_ignoring_panic_handler_raw(sig: c_int) -> ! { if CRASH_HANDLER_INSTALLED.load(Ordering::Relaxed) && !crate::env::ENABLE_ASAN { let handle = WINDOWS_SEGFAULT_HANDLE.swap(core::ptr::null_mut(), Ordering::Relaxed); if !handle.is_null() { - // `Handle` is an opaque cookie returned by - // `AddVectoredExceptionHandler`; the kernel validates it and - // returns 0 on a stale/garbage value — no memory-safety - // preconditions, so `safe fn` discharges the link-time proof. - unsafe extern "system" { - safe fn RemoveVectoredExceptionHandler(Handle: *mut core::ffi::c_void) -> u32; - safe fn SetUnhandledExceptionFilter( - f: Option i32>, - ) -> Option i32>; - } + use bun_windows_sys::kernel32::{ + RemoveVectoredExceptionHandler, SetUnhandledExceptionFilter, + }; let _ = RemoveVectoredExceptionHandler(handle); let _ = SetUnhandledExceptionFilter(None); } diff --git a/src/bun_core/Progress.rs b/src/bun_core/Progress.rs index a80dbf1ea1b7..c303af6e69da 100644 --- a/src/bun_core/Progress.rs +++ b/src/bun_core/Progress.rs @@ -21,42 +21,11 @@ use crate::Mutex; #[cfg(windows)] use crate::windows_sys as windows; -// `HANDLE` is an opaque kernel handle (kernel32 validates and returns 0/FALSE -// on a non-console handle); every out-param is `&mut T` to a `#[repr(C)]` POD, -// ABI-identical to the Win32 `LP*` pointer (thin non-null). The reference type -// encodes the only pointer-validity precondition, so `safe fn` discharges the -// link-time proof. (`bun_windows_sys::kernel32` declares these with `*mut`; -// redeclared locally so the legacy-conhost cursor path below is plain calls.) #[cfg(windows)] -#[link(name = "kernel32")] -unsafe extern "system" { - safe fn GetConsoleMode( - hConsoleHandle: windows::HANDLE, - lpMode: &mut windows::DWORD, - ) -> windows::BOOL; - safe fn GetConsoleScreenBufferInfo( - hConsoleOutput: windows::HANDLE, - lpConsoleScreenBufferInfo: &mut windows::CONSOLE_SCREEN_BUFFER_INFO, - ) -> windows::BOOL; - safe fn FillConsoleOutputAttribute( - hConsoleOutput: windows::HANDLE, - wAttribute: windows::WORD, - nLength: windows::DWORD, - dwWriteCoord: windows::COORD, - lpNumberOfAttrsWritten: &mut windows::DWORD, - ) -> windows::BOOL; - safe fn FillConsoleOutputCharacterW( - hConsoleOutput: windows::HANDLE, - cCharacter: windows::WCHAR, - nLength: windows::DWORD, - dwWriteCoord: windows::COORD, - lpNumberOfCharsWritten: &mut windows::DWORD, - ) -> windows::BOOL; - safe fn SetConsoleCursorPosition( - hConsoleOutput: windows::HANDLE, - dwCursorPosition: windows::COORD, - ) -> windows::BOOL; -} +use crate::windows_sys::kernel32::{ + FillConsoleOutputAttribute, FillConsoleOutputCharacterW, GetConsoleMode, + GetConsoleScreenBufferInfo, SetConsoleCursorPosition, +}; // Progress's terminal handle is the canonical `output::File` (vtable-backed // stderr/File from `OutputSinkVTable`). The duplicate `ProgressTerminalVTable` @@ -448,7 +417,10 @@ impl Progress { debug_assert!(self.is_windows_terminal); let mut info: windows::CONSOLE_SCREEN_BUFFER_INFO = crate::ffi::zeroed(); - if GetConsoleScreenBufferInfo(file.console_handle(), &mut info) != windows::TRUE + // SAFETY: `info` is a valid out-pointer; a handle that is not a + // console's fails the call. + if unsafe { GetConsoleScreenBufferInfo(file.console_handle(), &mut info) } + != windows::TRUE { // stop trying to write to this file self.terminal = None; diff --git a/src/bun_core/heap.rs b/src/bun_core/heap.rs index ffc41a79a3bc..d51567f61c5e 100644 --- a/src/bun_core/heap.rs +++ b/src/bun_core/heap.rs @@ -15,12 +15,11 @@ //! - `bun_threading::WorkPool::schedule_owned` / `OwnedTask` //! - `bun_event_loop::Task::from_boxed` / `ConcurrentTask::create_boxed` //! - `#[js_class]`-generated `T::to_js_boxed` -//! - `bun_libuv_sys::UvHandle::set_owned_data` / `take_owned_data` //! //! New code should reach for one of those. Direct `heap::into_raw`/`heap::take` //! calls are for the residual cases that don't fit a typed scheduler //! (intrusive refcounts, self-referential payloads where the raw pointer is -//! observed after hand-off, FFI ownership protocols outside the four above). +//! observed after hand-off, FFI ownership protocols outside the three above). //! //! All four are `#[inline(always)]` no-ops — identical machine code to the //! open-coded `Box::into_raw`/`from_raw`. @@ -62,7 +61,7 @@ pub fn into_raw(boxed: Box) -> *mut T { /// /// Prefer a paired typed helper that owns *both* halves of the round-trip when /// one applies (`bun_threading::WorkPool::schedule_owned`, -/// `bun_libuv_sys::UvHandle::set_owned_data`, `#[js_class]` `to_js_boxed`, …); +/// `#[js_class]` `to_js_boxed`, …); /// `release` is for the residual cases (intrusive-refcount finalizers, FFI /// ownership protocols) where no such helper exists. #[inline(always)] diff --git a/src/bun_core/lib.rs b/src/bun_core/lib.rs index 6f35c201eb53..5d40047e6041 100644 --- a/src/bun_core/lib.rs +++ b/src/bun_core/lib.rs @@ -465,9 +465,9 @@ pub mod vec { unsafe { core::slice::from_raw_parts_mut(spare.as_mut_ptr().cast::(), spare.len()) } } - /// `reserve(n)` then [`spare_bytes_mut`] — the libuv `uv_alloc_cb` shape - /// (and the dominant call pattern at every C-ABI fill site that wants "at - /// least `n` bytes of headroom"). Prefer this over `fill_spare` when the + /// `reserve(n)` then [`spare_bytes_mut`] — the dominant call pattern at + /// every C-ABI fill site that wants "at least `n` bytes of headroom". + /// Prefer this over `fill_spare` when the /// commit must happen on a separate control-flow arm from the obtain /// (e.g. across an `await`, or after an error-early-return). /// @@ -699,7 +699,7 @@ pub const unsafe fn container_of(field: *const F, offset: usize) -> *mut P /// Recover a typed `&mut T` from a C-callback's opaque user-data pointer. /// /// This is the canonical spelling for the ubiquitous trampoline pattern where -/// a C library (libarchive, c-ares, uWS, libuv, lol-html, BoringSSL, …) round- +/// a C library (libarchive, c-ares, uWS, lol-html, BoringSSL, …) round- /// trips a Rust object through a `void *user_data` slot and hands it back to /// an `extern "C" fn` thunk. Earlier ports open-coded this as /// `unsafe { &mut *ctx.cast::() }` at every site; centralising it here @@ -1781,6 +1781,40 @@ pub(crate) mod strings_impl { copy_utf16_into_utf8_with_utf8_len(buf, utf16, utf8_len) } + /// [`copy_utf16_into_utf8`] for a file name: an unpaired surrogate is + /// written as WTF-8 writes it, so the bytes still name the file. + pub fn copy_utf16_into_wtf8(buf: &mut [u8], utf16: &[u16]) -> EncodeIntoResult { + let mut result = EncodeIntoResult::default(); + loop { + let rest = &utf16[result.read as usize..]; + let mut paired = 0; + while paired < rest.len() { + if u16_is_lead(rest[paired]) + && rest.get(paired + 1).is_some_and(|&c| u16_is_trail(c)) + { + paired += 2; + } else if u16_is_lead(rest[paired]) || u16_is_trail(rest[paired]) { + break; + } else { + paired += 1; + } + } + let out = &mut buf[result.written as usize..]; + let copied = copy_utf16_into_utf8(out, &rest[..paired]); + result.read += copied.read; + result.written += copied.written; + let out = &mut out[copied.written as usize..]; + if (copied.read as usize) < paired || paired == rest.len() || out.len() < 3 { + return result; + } + let mut encoded = [0u8; 4]; + encode_wtf8_rune(&mut encoded, u32::from(rest[paired])); + out[..3].copy_from_slice(&encoded[..3]); + result.read += 1; + result.written += 3; + } + } + pub fn copy_utf16_into_utf8_with_utf8_len( buf: &mut [u8], utf16: &[u16], @@ -2503,7 +2537,7 @@ pub mod ffi { /// All-bits-zero value of `T` for `#[repr(C)]` FFI structs. /// - /// Single audited wrapper over `core::mem::zeroed()` so libc/uv/c-ares + /// Single audited wrapper over `core::mem::zeroed()` so libc/c-ares /// out-param init sites (`let mut x: libc::sigaction = zeroed();`) don't /// each open-code an `unsafe` block. /// @@ -2538,7 +2572,7 @@ pub mod ffi { pub unsafe trait Zeroable: Sized {} /// Unchecked all-bits-zero — escape hatch for types not yet proven - /// [`Zeroable`] (libuv handles, bindgen structs in `_sys` crates that + /// [`Zeroable`] (bindgen structs in `_sys` crates that /// don't depend on `bun_core`, generic `T` where the bound can't be /// threaded). Prefer [`zeroed`] + an `unsafe impl Zeroable` whenever the /// type is reachable. @@ -2693,8 +2727,14 @@ pub mod ffi { #[cfg(windows)] unsafe impl Zeroable for bun_windows_sys::externs::FILE_FS_DEVICE_INFORMATION {} #[cfg(windows)] + unsafe impl Zeroable for bun_windows_sys::externs::FILE_FS_FULL_SIZE_INFORMATION {} + #[cfg(windows)] unsafe impl Zeroable for bun_windows_sys::externs::FILE_FS_VOLUME_INFORMATION {} #[cfg(windows)] + unsafe impl Zeroable for bun_windows_sys::externs::FILE_STAT_BASIC_INFORMATION {} + #[cfg(windows)] + unsafe impl Zeroable for bun_windows_sys::externs::FILE_ID_FULL_DIR_INFORMATION {} + #[cfg(windows)] unsafe impl Zeroable for bun_windows_sys::externs::BY_HANDLE_FILE_INFORMATION {} #[cfg(windows)] unsafe impl Zeroable for bun_windows_sys::externs::WIN32_FILE_ATTRIBUTE_DATA {} @@ -2726,6 +2766,8 @@ pub mod ffi { unsafe impl Zeroable for bun_windows_sys::externs::OVERLAPPED {} #[cfg(windows)] unsafe impl Zeroable for bun_windows_sys::externs::PROCESS_INFORMATION {} + #[cfg(windows)] + unsafe impl Zeroable for bun_windows_sys::externs::OSVERSIONINFOW {} /// Conjure a value of a zero-sized type without `unsafe` at the call site. /// diff --git a/src/bun_core/output.rs b/src/bun_core/output.rs index 30f21e970eb2..8f494f8251fc 100644 --- a/src/bun_core/output.rs +++ b/src/bun_core/output.rs @@ -466,18 +466,7 @@ pub mod windows_stdio { // MOVE_DOWN: bun_sys::windows → crate::windows_sys (T0 leaf shim). use crate::windows_sys as w; use crate::windows_sys::kernel32 as c; - - // `HANDLE` is an opaque kernel handle (kernel32 validates and returns 0 on - // a non-console handle); `&mut DWORD` is ABI-identical to `LPDWORD` (thin - // non-null pointer). The reference type encodes the only pointer-validity - // precondition, so `safe fn` discharges the link-time proof. (`c::Get/Set - // ConsoleMode` from `bun_windows_sys` still take `*mut DWORD`; redeclared - // locally so the startup/restore paths below are plain calls.) - #[link(name = "kernel32")] - unsafe extern "system" { - safe fn GetConsoleMode(hConsoleHandle: w::HANDLE, lpMode: &mut w::DWORD) -> w::BOOL; - safe fn SetConsoleMode(hConsoleHandle: w::HANDLE, dwMode: w::DWORD) -> w::BOOL; - } + use crate::windows_sys::kernel32::{GetConsoleMode, SetConsoleMode}; /// At program start, we snapshot the console modes of standard in, out, and err /// so that we can restore them at program exit if they change. Restoration is @@ -491,11 +480,6 @@ pub mod windows_stdio { static CONSOLE_OUTPUT_CODEPAGE: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0); - #[unsafe(no_mangle)] - extern "C" fn Bun__restoreWindowsStdio() { - restore(); - } - pub(crate) fn restore() { // SAFETY: PEB access is sound on Windows; handles are valid for process // lifetime. `peb()` returns a raw pointer because the OS/CRT mutate the @@ -525,7 +509,7 @@ pub mod windows_stdio { } pub(crate) fn init() { - w::libuv::uv_disable_stdio_inheritance(); + w::disable_stdio_inheritance(); let stdin = w::GetStdHandle(w::STD_INPUT_HANDLE).unwrap_or(w::INVALID_HANDLE_VALUE); let stdout = w::GetStdHandle(w::STD_OUTPUT_HANDLE).unwrap_or(w::INVALID_HANDLE_VALUE); @@ -2535,7 +2519,6 @@ fn init_scoped_debug_writer_at_startup() { let _ = output_sink().make_path(Fd::cwd(), dir); } - // do not use libuv through this code path, since it might not be initialized yet. use std::io::Write as _; let mut pid = Vec::new(); write!(&mut pid, "{}", getpid()).expect("failed to allocate path"); diff --git a/src/bun_core/string/immutable.rs b/src/bun_core/string/immutable.rs index f5b37f380d78..99de0c0480ee 100644 --- a/src/bun_core/string/immutable.rs +++ b/src/bun_core/string/immutable.rs @@ -266,7 +266,7 @@ pub fn without_utf8_bom(bytes: &[u8]) -> &[u8] { pub use crate::string::w; pub use crate::strings_impl::{ EncodeIntoResult, copy_latin1_into_utf8, copy_utf16_into_utf8, - copy_utf16_into_utf8_with_utf8_len, element_length_latin1_into_utf8, + copy_utf16_into_utf8_with_utf8_len, copy_utf16_into_wtf8, element_length_latin1_into_utf8, element_length_utf16_into_utf8, encode_surrogate_pair, push_codepoint_utf16, to_utf8_alloc_z, to_utf8_from_latin1_z, u16_lead, u16_trail, }; @@ -2441,6 +2441,24 @@ pub fn convert_utf8_to_utf16_in_buffer<'a>(buf: &'a mut [u16], input: &[u8]) -> pub fn try_convert_utf8_to_utf16_in_buffer<'a>( buf: &'a mut [u16], input: &[u8], +) -> Option<&'a mut [u16]> { + convert_wtf8_to_utf16_in_buffer::(buf, input) +} + +/// As [`try_convert_utf8_to_utf16_in_buffer`], and `None` as well for input +/// that is not WTF-8: a byte that starts or continues no sequence is refused +/// instead of becoming U+FFFD (lone surrogates still pass). For names: two +/// different byte strings must not come out as one name. +pub fn try_convert_wtf8_to_utf16_in_buffer<'a>( + buf: &'a mut [u16], + input: &[u8], +) -> Option<&'a mut [u16]> { + convert_wtf8_to_utf16_in_buffer::(buf, input) +} + +fn convert_wtf8_to_utf16_in_buffer<'a, const STRICT: bool>( + buf: &'a mut [u16], + input: &[u8], ) -> Option<&'a mut [u16]> { if input.is_empty() { return Some(&mut buf[..0]); @@ -2467,6 +2485,10 @@ pub fn try_convert_utf8_to_utf16_in_buffer<'a>( i += 1; } else { let (cp, adv) = decode_wtf8_one(&input[i..]); + // U+FFFD itself takes three bytes. + if STRICT && cp == 0xFFFD && adv == 1 { + return None; + } if cp <= 0xFFFF { if written >= buf.len() { return None; diff --git a/src/bun_core/thread_id.rs b/src/bun_core/thread_id.rs index 2b8756deeb21..7b19098c890c 100644 --- a/src/bun_core/thread_id.rs +++ b/src/bun_core/thread_id.rs @@ -120,11 +120,7 @@ fn current_uncached() -> ThreadId { } #[cfg(target_os = "windows")] { - unsafe extern "system" { - // No preconditions; infallible Win32 intrinsic. - safe fn GetCurrentThreadId() -> u32; // kernel32 DWORD - } - return GetCurrentThreadId(); + return bun_windows_sys::GetCurrentThreadId(); } #[cfg(target_os = "freebsd")] { diff --git a/src/bun_core/util.rs b/src/bun_core/util.rs index d6d0754079c8..ad3f5def2217 100644 --- a/src/bun_core/util.rs +++ b/src/bun_core/util.rs @@ -810,7 +810,7 @@ pub fn dirname(path: &[u8]) -> Option<&[u8]> { // ─── Fd + fd module (from bun_sys::fd) ──────────────────────────────────── // TYPE_ONLY: bun_core needs only the handle wrapper + stdin/out/err/cwd ctors. -// Full method set (close, makeLibUVOwned, …) stays in bun_sys which re-exports +// Full method set (close, make_crt_owned_for_syscall, …) stays in bun_sys which re-exports // `pub use bun_core::Fd as FD;` and adds inherent impls there. // Backing int: c_int on posix, u64 on Windows. @@ -824,7 +824,7 @@ type FdBacking = u64; pub struct Fd(pub FdBacking); // Packed u64 { value: u63, kind: u1 } — fields are LSB-first, so -// `value` is bits 0..63, `kind` is bit 63. (.system=0, .uv=1) +// `value` is bits 0..63, `kind` is bit 63. (.system=0, .crt=1) #[cfg(windows)] const FD_KIND_BIT: u64 = 1u64 << 63; #[cfg(windows)] @@ -848,11 +848,11 @@ impl Fd { pub const fn from_native(v: FdBacking) -> Fd { Fd(v) } - /// libuv fd (== posix fd on non-windows; uv-tagged on windows). + /// C runtime fd (the posix fd on non-windows; a UCRT fd, crt-tagged, on windows). #[inline] - pub const fn from_uv(v: i32) -> Fd { + pub const fn from_crt(v: i32) -> Fd { #[cfg(windows)] - // kind=.uv (bit 63 = 1); uv_file is i32, store sign-extended into low 63. + // kind=.crt (bit 63 = 1); the CRT fd is i32, store sign-extended into low 63. { Fd(FD_KIND_BIT | ((v as i64 as u64) & FD_VALUE_MASK)) } @@ -869,9 +869,9 @@ impl Fd { Fd((h as u64) & FD_VALUE_MASK) } /// Native OS file descriptor (`fd_t`). On POSIX this is just the backing - /// `c_int`. On Windows, when `kind == Uv`, calls `uv_get_osfhandle` to - /// obtain the underlying HANDLE — so the returned value may not be safely - /// closed via libc; use `FdExt::close()` instead. + /// `c_int`. On Windows, when `kind == Crt`, this is the HANDLE the CRT fd + /// owns (`INVALID_HANDLE_VALUE` for a bad fd) — close it through + /// `FdExt::close()`, never directly. #[cfg(not(windows))] #[inline] pub const fn native(self) -> FdNative { @@ -882,7 +882,7 @@ impl Fd { pub fn native(self) -> FdNative { match self.decode_windows() { DecodeWindows::Windows(handle) => handle, - DecodeWindows::Uv(file_number) => fd::uv_get_osfhandle(file_number), + DecodeWindows::Crt(file_number) => fd::crt_get_osfhandle(file_number), } } /// Borrow this `Fd` as a [`std::os::fd::BorrowedFd`] for handing to APIs @@ -910,53 +910,26 @@ impl Fd { // borrow cannot outlive `&self`. unsafe { std::os::fd::BorrowedFd::borrow_raw(raw) } } - /// libuv c_int file number. On POSIX this equals `native()`. On Windows, - /// when kind=uv this extracts the stored uv_file; when kind=system this - /// maps stdio handles to 0/1/2 (checking both the cached statics and the - /// live `GetStdHandle` result) and **panics** otherwise — converting an - /// arbitrary HANDLE to a uv fd makes closing impossible. The supplier - /// should call `make_lib_uv_owned()` near where `open()` was called. + /// C runtime file number. On POSIX this equals `native()`. On Windows, + /// when kind=crt this extracts the stored CRT fd; when kind=system this + /// **panics** — converting a HANDLE to a CRT fd makes closing impossible. + /// The supplier should call `make_crt_owned_for_syscall()` near where + /// `open()` was called. #[cfg(not(windows))] #[inline] - pub const fn uv(self) -> i32 { + pub const fn crt(self) -> i32 { self.0 } #[cfg(windows)] - pub fn uv(self) -> i32 { + pub fn crt(self) -> i32 { match self.decode_windows() { - DecodeWindows::Uv(v) => v, - DecodeWindows::Windows(handle) => { - // `.stdin()`/`.stdout()`/`.stderr()` hand out the cached - // `WINDOWS_CACHED_STD{IN,OUT,ERR}` (snapshotted at startup), - // so round-trip against those first. Comparing only against - // the live `GetStdHandle` result panics if the process std - // handle was swapped after startup via `SetStdHandle`, - // `AllocConsole`, `AttachConsole`, etc. - if Some(self) == fd::WINDOWS_CACHED_STDIN.get().copied() { - return 0; - } - if Some(self) == fd::WINDOWS_CACHED_STDOUT.get().copied() { - return 1; - } - if Some(self) == fd::WINDOWS_CACHED_STDERR.get().copied() { - return 2; - } - if fd::is_stdio_handle(fd::STD_INPUT_HANDLE, handle) { - return 0; - } - if fd::is_stdio_handle(fd::STD_OUTPUT_HANDLE, handle) { - return 1; - } - if fd::is_stdio_handle(fd::STD_ERROR_HANDLE, handle) { - return 2; - } - panic!( - "Cast bun.FD.uv({}) makes closing impossible!\n\n\ - The supplier of fd FD should call 'FD.makeLibUVOwned',\n\ - probably where open() was called.", - self, - ); - } + DecodeWindows::Crt(v) => v, + DecodeWindows::Windows(_) => panic!( + "Fd::crt({}) on a HANDLE makes closing impossible!\n\n\ + The supplier of the fd should call `make_crt_owned_for_syscall()`,\n\ + probably where open() was called.", + self, + ), } } @@ -1006,7 +979,7 @@ impl Fd { .unwrap_or(Fd::INVALID) } /// The Windows `AT_FDCWD`; [`Fd::decode_windows`] maps it to the PEB's - /// current directory handle. Handles fit in 32 bits, bit 63 is the uv tag + /// current directory handle. Handles fit in 32 bits, bit 63 is the CRT tag /// and `INVALID_HANDLE_VALUE` masks to all of bits 0..63, so bit 62 alone /// is out of band. #[cfg(windows)] @@ -1026,7 +999,7 @@ impl Fd { } #[cfg(windows)] pub fn is_stdio(self) -> bool { - // Cache check first (matches `to_uv_index`): the cache reflects what the + // Cache check first: the cache reflects what the // process saw at startup, even after `SetStdHandle`/`AllocConsole`. if self == Self::stdin() || self == Self::stdout() || self == Self::stderr() { return true; @@ -1038,7 +1011,7 @@ impl Fd { || fd::is_stdio_handle(fd::STD_ERROR_HANDLE, handle) } - // ── Kind tag (Windows: bit 63 = uv/system) ─────────────────────────── + // ── Kind tag (Windows: bit 63 = crt/system) ─────────────────────────── #[cfg(not(windows))] #[inline] pub const fn kind(self) -> FdKind { @@ -1050,7 +1023,7 @@ impl Fd { if self.0 & FD_KIND_BIT == 0 { FdKind::System } else { - FdKind::Uv + FdKind::Crt } } @@ -1074,37 +1047,8 @@ impl Fd { }; DecodeWindows::Windows(h) } - // Direct extract — do NOT recurse into self.uv() (which calls decode_windows). - FdKind::Uv => DecodeWindows::Uv((self.0 & FD_VALUE_MASK) as u32 as i32), - } - } - - /// On Windows, convert a system-kind - /// `Fd` (raw `HANDLE`) into a libuv-kind `Fd` (CRT `_open_osfhandle`-backed - /// `int`) so libuv `uv_fs_*` APIs can consume it. uv-kind passes through. - /// On POSIX this is the identity (libuv fd == posix fd). - /// - /// Returns `Err(())` when - /// `uv_open_osfhandle` returns `-1`; the caller decides whether to close - /// the original handle (see `make_libuv_owned_for_syscall`). - #[inline] - pub fn make_libuv_owned(self) -> Result { - debug_assert!(self.is_valid()); - #[cfg(not(windows))] - { - Ok(self) - } - #[cfg(windows)] - match self.kind() { - FdKind::Uv => Ok(self), - FdKind::System => { - let crt_fd = fd::uv_open_osfhandle(self.native()); - if crt_fd == -1 { - Err(()) - } else { - Ok(Fd::from_uv(crt_fd)) - } - } + // Direct extract — do NOT recurse into self.crt() (which calls decode_windows). + FdKind::Crt => DecodeWindows::Crt((self.0 & FD_VALUE_MASK) as u32 as i32), } } @@ -1118,7 +1062,7 @@ impl Fd { { match self.kind() { FdKind::System => self.value_as_system() != 0, // INVALID_VALUE = minInt(u63) = 0 - FdKind::Uv => true, + FdKind::Crt => true, } } } @@ -1159,7 +1103,7 @@ impl Fd { None } } - DecodeWindows::Uv(n) => match n { + DecodeWindows::Crt(n) => match n { 0 => Some(Stdio::StdIn), 1 => Some(Stdio::StdOut), 2 => Some(Stdio::StdErr), @@ -1188,13 +1132,13 @@ pub enum FdKind { #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum FdKind { System = 0, - Uv = 1, + Crt = 1, } #[cfg(windows)] pub enum DecodeWindows { Windows(*mut core::ffi::c_void), - Uv(i32), + Crt(i32), } #[repr(u8)] @@ -1316,14 +1260,7 @@ pub unsafe fn fd_path_raw(fd: Fd, buf: *mut u8, cap: usize) -> isize { pub unsafe fn fd_path_raw_w(fd: Fd, buf: *mut u16, cap: usize) -> isize { #[cfg(windows)] { - unsafe extern "system" { - fn GetFinalPathNameByHandleW( - hFile: *mut core::ffi::c_void, - lpszFilePath: *mut u16, - cchFilePath: u32, - dwFlags: u32, - ) -> u32; - } + use bun_windows_sys::GetFinalPathNameByHandleW; // VOLUME_NAME_DOS (0) — matches `bun_sys::windows::GetFinalPathNameByHandle` default. // SAFETY: buf has `cap` u16 units; handle from Fd::native(). let n = unsafe { GetFinalPathNameByHandleW(fd.native(), buf, cap as u32, 0) } as usize; @@ -1411,14 +1348,13 @@ impl core::fmt::Display for Fd { } match fd.decode_windows() { DecodeWindows::Windows(_) => write!(w, "{}[handle]", fd.value_as_system()), - DecodeWindows::Uv(n) => write!(w, "{}[libuv]", n), + DecodeWindows::Crt(n) => write!(w, "{}[crt]", n), } } } } -/// Fd module-level statics + Windows libuv/PEB FFI shims (T0 → no -/// crate dep, just `extern` symbols; libuv is linked into the final binary). +/// Fd module-level statics + Windows UCRT/PEB FFI shims. pub mod fd { #[cfg(windows)] use super::Fd; @@ -1438,19 +1374,60 @@ pub mod fd { pub(crate) static WINDOWS_CACHED_FD_SET: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false); + // UCRT lowio. By-value ints only; a bad fd goes to the process's CRT + // invalid-parameter handler and is then reported as `-1`/`EBADF`. #[cfg(windows)] unsafe extern "C" { - /// libuv: convert C-runtime fd → OS HANDLE. By-value `c_int` in, opaque - /// HANDLE out — wraps `_get_osfhandle`, which validates the fd and - /// returns `INVALID_HANDLE_VALUE` on a bad index. No memory-safety - /// preconditions. - pub safe fn uv_get_osfhandle(fd: c_int) -> *mut c_void; - /// libuv: `_open_osfhandle(os_fd, 0)` — wraps a HANDLE in a CRT fd so - /// libuv `uv_fs_*` (which speak `uv_file == int`) can use it. Returns - /// `-1` on `EMFILE` (CRT fd table full) or invalid handle. The `*mut - /// c_void` is an opaque kernel HANDLE, never dereferenced; no - /// memory-safety preconditions. - pub safe fn uv_open_osfhandle(os_fd: *mut c_void) -> c_int; + safe fn _get_osfhandle(fd: c_int) -> isize; + safe fn _open_osfhandle(osfhandle: isize, flags: c_int) -> c_int; + safe fn _close(fd: c_int) -> c_int; + } + + /// The debug CRT asserts on a bad fd before anything else, which + /// `crtDebugReportHook` (c-bindings.cpp) lets pass. An assert that another + /// thread raises meanwhile it takes for a re-entrant one and breaks into + /// the debugger without asking the hook: one lowio call at a time there. + #[cfg(all(windows, debug_assertions))] + static LOWIO: super::Mutex<()> = super::Mutex::new(()); + /// The HANDLE a CRT fd owns; `INVALID_HANDLE_VALUE` for a bad fd, and for + /// a stdio fd with no stream attached (where the CRT reports `-2`). + #[cfg(windows)] + #[unsafe(export_name = "Bun__crtGetOsfhandle")] + pub extern "C" fn crt_get_osfhandle(fd: c_int) -> *mut c_void { + if fd < 0 { + return crate::windows_sys::INVALID_HANDLE_VALUE; + } + #[cfg(debug_assertions)] + let _one_at_a_time = LOWIO.lock(); + match _get_osfhandle(fd) { + -2 => crate::windows_sys::INVALID_HANDLE_VALUE, + h => h as *mut c_void, + } + } + + /// Wrap `handle` in a new CRT fd, which owns it from then on. `-1` when + /// the CRT fd table is full (`EMFILE`) or the handle is invalid. + #[cfg(windows)] + pub fn crt_open_osfhandle(handle: *mut c_void) -> c_int { + #[cfg(debug_assertions)] + let _one_at_a_time = LOWIO.lock(); + _open_osfhandle(handle as isize, 0) + } + + /// `_close`: closes the CRT fd and the HANDLE it owns. `-1` (`EBADF`) + /// when `fd` is not an open CRT fd. + #[cfg(windows)] + pub fn crt_close(fd: c_int) -> c_int { + if fd < 0 { + return -1; + } + // Only a bad fd asserts. The lock is not held across `_close`, which + // waits behind a read that is parked on a synchronous pipe. + #[cfg(debug_assertions)] + if crt_get_osfhandle(fd) == crate::windows_sys::INVALID_HANDLE_VALUE { + return -1; + } + _close(fd) } #[cfg(windows)] pub use crate::windows_sys::{STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE}; @@ -2641,11 +2618,12 @@ pub fn is_writable(fd: Fd) -> Pollable { // ── os_entropy ──────────────────────────────────────────────────────────── // Raw OS entropy source: getrandom(2) on Linux, getentropy on Darwin/BSD, // RtlGenRandom on Windows. bun_core sits below boringssl_sys in the crate -// graph so it cannot reach `RAND_bytes`; this is used only to seed -// `fast_random()`'s thread-local PRNG once per thread. Every other caller +// graph so it cannot reach `RAND_bytes`; this seeds `fast_random()`'s +// thread-local PRNG once per thread, and crates at this tier take from it +// what must not repeat across threads (a `mkdtemp` name). Every other caller // must use `bun_boringssl_sys::rand_bytes` (userspace DRBG, no syscall per // call). -fn os_entropy(bytes: &mut [u8]) { +pub fn os_entropy(bytes: &mut [u8]) { #[cfg(any(target_os = "linux", target_os = "android"))] { let mut filled = 0usize; @@ -4074,9 +4052,7 @@ fn getcwd_len(buf: &mut PathBuffer) -> crate::CrateResult { { // Windows: wrap // `kernel32.GetCurrentDirectoryW` and transcode WTF-16 → WTF-8. - unsafe extern "system" { - fn GetCurrentDirectoryW(nBufferLength: u32, lpBuffer: *mut u16) -> u32; - } + use bun_windows_sys::GetCurrentDirectoryW; let mut wbuf = WPathBuffer::ZEROED; // SAFETY: `wbuf` has `PATH_MAX_WIDE` writable u16 units. let n = unsafe { GetCurrentDirectoryW(wbuf.0.len() as u32, wbuf.0.as_mut_ptr()) } as usize; @@ -4313,12 +4289,7 @@ pub fn reload_process(clear_terminal: bool, may_return: bool) { { // Signal the watcher-manager parent via magic exit code. use crate::windows_sys::kernel32::{GetCurrentProcess, GetLastError}; - unsafe extern "system" { - // `h` is an opaque kernel HANDLE (never dereferenced in-process); - // the kernel validates it and returns FALSE on a bad handle. No - // memory-safety preconditions. - safe fn TerminateProcess(h: *mut core::ffi::c_void, code: u32) -> i32; - } + use bun_windows_sys::TerminateProcess; // = 3224497970. Parent // watcher-manager compares the child's exit code against exactly this. const WATCHER_RELOAD_EXIT: u32 = 0xC031_EF32; @@ -4427,8 +4398,7 @@ impl SpawnStatus { // This is the single source of truth for the request layout; `spawn_sys` // re-exports these types rather than re-declaring them. The #[repr(C)] data // mirrors are target-agnostic so the module compiles on all platforms; only -// the extern decl is `cfg(unix)` (Windows spawns go through libuv and never -// link this symbol). +// the extern decl is `cfg(unix)` (Windows never links this symbol). pub mod spawn_ffi { use core::ffi::{c_char, c_int}; @@ -4975,8 +4945,8 @@ impl Timespec { } #[cfg(windows)] { - // QPC via the c-bindings.cpp shim: the same monotonic clock libuv - // (uv_hrtime), uSockets' sweep and WTF::MonotonicTime::now use. + // QPC via the c-bindings.cpp shim: the same monotonic clock + // uSockets' sweep and WTF::MonotonicTime::now use. let mut sec: i64 = 0; let mut nsec: i64 = 0; clock_gettime_monotonic(&mut sec, &mut nsec); diff --git a/src/bun_core/windows_sys.rs b/src/bun_core/windows_sys.rs index 7a7e9cf4db49..347856759d24 100644 --- a/src/bun_core/windows_sys.rs +++ b/src/bun_core/windows_sys.rs @@ -3,8 +3,8 @@ //! `bun_core` is tier-0 and may not depend on `bun_sys` (cycle). Shared Win32 //! POD typedefs/structs, kernel32 externs, and the TEB→PEB chain are //! re-exported from the tier-0 leaf `bun_windows_sys` (which has zero `bun_*` -//! deps, so no cycle); only the `bun_core`-specific console consts and the -//! `Zeroable` impls live here. All declarations are zero-cost FFI +//! deps, so no cycle); only the standard-handle ids, the `GetStdHandle` +//! wrapper and the `Zeroable` impls live here. All declarations are zero-cost FFI //! (`extern "system"` = `__stdcall`, which on x64 is the same as `extern "C"`). #![cfg(windows)] #![allow(non_snake_case)] @@ -18,10 +18,9 @@ pub const STD_INPUT_HANDLE: DWORD = (-10i32) as DWORD; pub const STD_OUTPUT_HANDLE: DWORD = (-11i32) as DWORD; pub const STD_ERROR_HANDLE: DWORD = (-12i32) as DWORD; -// Console mode flags (consoleapi.h). -pub(crate) const ENABLE_PROCESSED_OUTPUT: DWORD = 0x0001; -pub(crate) const ENABLE_WRAP_AT_EOL_OUTPUT: DWORD = 0x0002; -pub(crate) const ENABLE_VIRTUAL_TERMINAL_PROCESSING: DWORD = 0x0004; +pub(crate) use bun_windows_sys::{ + ENABLE_PROCESSED_OUTPUT, ENABLE_VIRTUAL_TERMINAL_PROCESSING, ENABLE_WRAP_AT_EOL_OUTPUT, +}; /// Wrapper that returns `None` on `INVALID_HANDLE_VALUE` or a null handle. #[inline] @@ -37,7 +36,7 @@ pub fn GetStdHandle(std_handle: DWORD) -> Option { // ────────────────────────────────────────────────────────────────────────── // PEB access. `bun_core::output::windows_stdio` // reads `ProcessParameters.hStd{Input,Output,Error}` to snapshot the console -// handles before libuv touches them. Canonical structs/asm live in the tier-0 +// handles. Canonical structs/asm live in the tier-0 // `bun_windows_sys` leaf and are re-exported here for the // `crate::windows_sys::*` path used by callers. // ────────────────────────────────────────────────────────────────────────── @@ -54,11 +53,54 @@ unsafe impl crate::ffi::Zeroable for CONSOLE_SCREEN_BUFFER_INFO {} // so existing `crate::windows_sys::kernel32::*` callers resolve. pub use bun_windows_sys::kernel32; -/// `bun.windows.libuv` — only `uv_disable_stdio_inheritance` is called from -/// `bun_core`; declared directly to avoid a `bun_libuv_sys` dep at tier-0. -pub mod libuv { - unsafe extern "C" { - /// No preconditions; walks the CRT fd table and clears HANDLE_FLAG_INHERIT. - pub(crate) safe fn uv_disable_stdio_inheritance(); +/// Make every handle this process inherited as stdio non-inheritable, so +/// children only receive the handles a spawn passes explicitly: the three std +/// handles, plus the handles of the CRT fd block a parent passes in +/// `STARTUPINFOW.lpReserved2` (`int count; u8 crt_flags[count]; HANDLE +/// os_handle[count]`, unaligned). Failures are ignored — a std handle may be +/// invalid or already closed. +pub(crate) fn disable_stdio_inheritance() { + use bun_windows_sys::{ + GetStartupInfoW, HANDLE_FLAG_INHERIT, STARTUPINFOW, SetHandleInformation, + }; + + for id in [STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] { + if let Some(handle) = GetStdHandle(id) { + let _ = SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0); + } + } + + let mut si = core::mem::MaybeUninit::::zeroed(); + // SAFETY: `si` is a writable STARTUPINFOW, which GetStartupInfoW fills; it + // cannot fail, and all-zero is a valid STARTUPINFOW regardless. + let si = unsafe { + GetStartupInfoW(si.as_mut_ptr()); + si.assume_init() + }; + let buffer = si.lpReserved2.cast_const(); + let size = usize::from(si.cbReserved2); + const COUNT_SIZE: usize = core::mem::size_of::(); + const HANDLE_SIZE: usize = core::mem::size_of::(); + if buffer.is_null() || size < COUNT_SIZE { + return; + } + // SAFETY: `buffer` is valid for `size >= COUNT_SIZE` bytes for the life of + // the process (it is part of the process parameters block). + let count = unsafe { buffer.cast::().read_unaligned() } as usize; + if count > 256 || size < COUNT_SIZE + count * (1 + HANDLE_SIZE) { + return; + } + for i in 0..count { + // SAFETY: in bounds per the size check above; the handle array is + // unaligned. + let handle = unsafe { + buffer + .add(COUNT_SIZE + count + i * HANDLE_SIZE) + .cast::() + .read_unaligned() + }; + if handle != INVALID_HANDLE_VALUE { + let _ = SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0); + } } } diff --git a/src/bundler/BundleThread.rs b/src/bundler/BundleThread.rs index 3733ae7d87ad..78374b7b5078 100644 --- a/src/bundler/BundleThread.rs +++ b/src/bundler/BundleThread.rs @@ -8,10 +8,6 @@ use bun_threading::unbounded_queue::{Node, UnboundedQueue}; use crate::bundle_v2::{FileMap, JSBundlerPlugin, dispatch}; use crate::{BundleV2, Transpiler}; -/// Used to keep the bundle thread from spinning on Windows -#[cfg(windows)] -extern "C" fn timer_callback(_: *mut bun_sys::windows::libuv::Timer) {} - /// Port of `std.Thread.ResetEvent` — single-shot manual-reset event used to /// block `spawn()` until the bundle thread has initialized its `Waker`. // Re-exports `bun_threading::ResetEvent` (futex-backed); the futex impl @@ -175,8 +171,8 @@ impl BundleThread { // accesses the same struct concurrently, so we never materialize `&mut Self`. // `UnboundedQueue::push` takes `&self` (lock-free MPSC). `Waker::wake` takes // `&self` on all platforms and only reads a Copy field (eventfd, mach port, - // `WindowsLoop` pointer) to pass to a wake call that is safe from any thread - // (eventfd write, mach_msg send, uv_async_send), so the `&Waker` autoref is + // event handle) to pass to a wake call that is safe from any thread + // (eventfd write, mach_msg send, SetEvent), so the `&Waker` autoref is // sound alongside `wait(&self)` in `thread_main` and other `enqueue` callers. unsafe { (*instance).queue.push(completion); @@ -198,23 +194,6 @@ impl BundleThread { // SAFETY: raw-ptr field projection; spawning thread is blocked in `ready_event.wait()`. unsafe { (*instance).ready_event.set() }; - // The libuv Timer lives on stack for the lifetime of this never-returning fn. - // It MUST be declared at function scope (not inside the `#[cfg(windows)] { ... }` - // block below) because `timer.init()`/`timer.start()` register `&timer`'s address - // into the uv loop's intrusive handle queue / timer min-heap, and `waker.wait()` - // (→ `uv_run`) in the `loop {}` below dereferences that address. - #[cfg(windows)] - let mut timer: bun_sys::windows::libuv::Timer = bun_core::ffi::zeroed(); - #[cfg(windows)] - { - // SAFETY: raw place read of `waker.loop_.uv_loop` (Copy ptr); field is - // write-once in `Waker::init()` above and never mutated by `wake()`, so a - // concurrent `enqueue()` (possible now that `ready_event.set()` has fired) - // does not conflict. No `&Waker`/`&mut Waker` is materialized here. - timer.init(unsafe { (*instance).waker.uv_loop() }); - timer.start(u64::MAX, u64::MAX, Some(timer_callback)); - } - let mut has_bundled = false; loop { loop { diff --git a/src/cares_sys/Cargo.toml b/src/cares_sys/Cargo.toml index 2dbedb44a465..9de8b895b219 100644 --- a/src/cares_sys/Cargo.toml +++ b/src/cares_sys/Cargo.toml @@ -15,6 +15,7 @@ strum.workspace = true libc.workspace = true bun_alloc.workspace = true bun_core.workspace = true +bun_errno.workspace = true [target.'cfg(windows)'.dependencies] -bun_libuv_sys.workspace = true +bun_windows_sys.workspace = true diff --git a/src/cares_sys/c_ares.rs b/src/cares_sys/c_ares.rs index 09b41da74473..e2d163ec408b 100644 --- a/src/cares_sys/c_ares.rs +++ b/src/cares_sys/c_ares.rs @@ -757,12 +757,15 @@ impl Channel { let optmask: c_int = ARES_OPT_FLAGS | ARES_OPT_TIMEOUTMS | ARES_OPT_SOCK_STATE_CB | ARES_OPT_TRIES; - // SAFETY: idempotent Winsock init (uv_once); c-ares creates its sockets with - // ws2_32 directly and libuv otherwise initializes Winsock lazily. + // c-ares creates its sockets with ws2_32 directly, and Winsock is + // otherwise initialized on first use by uSockets. #[cfg(windows)] - unsafe { - bun_libuv_sys::uv__winsock_ensure() - }; + { + unsafe extern "C" { + safe fn us_internal_winsock_ensure(); + } + us_internal_winsock_ensure(); + } // SAFETY: c-ares FFI; opts/channel are valid stack pointers. let rc = unsafe { ares_init_options(&raw mut channel, &raw mut opts, optmask) }; if let Some(err) = Error::get(rc) { @@ -1654,39 +1657,25 @@ pub enum Error { EAI_AGAIN = ARES_ENOSERVER + 1, } -/// libuv's `UV_EAI_AGAIN`, the `errno` Node reports for getaddrinfo `EAI_AGAIN`. -const UV_EAI_AGAIN: i32 = -3001; - impl Error { // Deferred / toDeferred / toJSWithSyscall / toJSWithSyscallAndHostname // aliases deleted — live in bun_runtime::dns_jsc (extension trait). pub fn init_eai(rc: i32) -> Option { + // `EAI_*` are WSA codes (ws2tcpip.h). #[cfg(windows)] { - use bun_libuv_sys as libuv; - // https://github.com/nodejs/node/blob/2eff28fb7a93d3f672f80b582f664a7c701569fb/lib/internal/errors.js#L807-L815 - if rc == libuv::UV_EAI_NODATA || rc == libuv::UV_EAI_NONAME { - return Some(Error::ENOTFOUND); - } - // TODO: revisit this - return match rc { - 0 => None, - libuv::UV_EAI_AGAIN => Some(Error::EAI_AGAIN), - libuv::UV_EAI_ADDRFAMILY => Some(Error::EBADFAMILY), - libuv::UV_EAI_BADFLAGS => Some(Error::EBADFLAGS), - libuv::UV_EAI_BADHINTS => Some(Error::EBADHINTS), - libuv::UV_EAI_CANCELED => Some(Error::ECANCELLED), - libuv::UV_EAI_FAIL => Some(Error::ENOTFOUND), - libuv::UV_EAI_FAMILY => Some(Error::EBADFAMILY), - libuv::UV_EAI_MEMORY => Some(Error::ENOMEM), - libuv::UV_EAI_NODATA => Some(Error::ENODATA), - libuv::UV_EAI_NONAME => Some(Error::ENONAME), - libuv::UV_EAI_OVERFLOW => Some(Error::ENOMEM), - libuv::UV_EAI_PROTOCOL => Some(Error::EBADQUERY), - libuv::UV_EAI_SERVICE => Some(Error::ESERVICE), - libuv::UV_EAI_SOCKTYPE => Some(Error::ECONNREFUSED), - _ => Some(Error::ENOTFOUND), // UV_ENOENT and non documented errors + use bun_windows_sys::Win32Error as W; + return match u16::try_from(rc).map(W) { + Ok(W::SUCCESS) => None, + Ok(W::WSATRY_AGAIN) => Some(Error::EAI_AGAIN), + Ok(W::WSAEINVAL) => Some(Error::EBADFLAGS), + Ok(W::WSAEAFNOSUPPORT) => Some(Error::EBADFAMILY), + Ok(W::NOT_ENOUGH_MEMORY) => Some(Error::ENOMEM), + Ok(W::WSATYPE_NOT_FOUND) => Some(Error::ESERVICE), + Ok(W::WSAESOCKTNOSUPPORT) => Some(Error::ECONNREFUSED), + // `EAI_NONAME`, `EAI_FAIL` and non documented errors + _ => Some(Error::ENOTFOUND), }; } @@ -1738,11 +1727,11 @@ impl Error { pub fn eai_raw_by_name(name: &[u8]) -> Option { #[cfg(windows)] { - use bun_libuv_sys as libuv; + use bun_windows_sys::Win32Error as W; match name { - b"EAI_AGAIN" => Some(libuv::UV_EAI_AGAIN), - b"EAI_FAIL" => Some(libuv::UV_EAI_FAIL), - b"EAI_NONAME" => Some(libuv::UV_EAI_NONAME), + b"EAI_AGAIN" => Some(i32::from(W::WSATRY_AGAIN.0)), + b"EAI_FAIL" => Some(i32::from(W::WSANO_RECOVERY.0)), + b"EAI_NONAME" => Some(i32::from(W::WSAHOST_NOT_FOUND.0)), _ => None, } } @@ -1760,7 +1749,7 @@ impl Error { /// The JS-visible `errno` for this status. pub fn errno(self) -> i32 { match self { - Error::EAI_AGAIN => UV_EAI_AGAIN, + Error::EAI_AGAIN => bun_errno::uv_codes::UV_EAI_AGAIN, _ => self as i32, } } diff --git a/src/cares_sys/lib.rs b/src/cares_sys/lib.rs index 6ab66193880b..53df61881148 100644 --- a/src/cares_sys/lib.rs +++ b/src/cares_sys/lib.rs @@ -9,10 +9,8 @@ pub mod c_ares_draft; pub mod winsock { use core::ffi::c_int; pub(crate) type socklen_t = c_int; // ws2tcpip.h: `typedef int socklen_t;` - // Same nominal type as `bun_sys::posix::sockaddr*`; sin_addr is `in_addr{s_addr}` - // (vs the previous `[u8;4]`) but the only caller (c_ares.rs `get_sockaddr`) - // takes `&raw mut → cast`, so the field's nominal type is transparent. - pub(crate) use bun_libuv_sys::{sockaddr, sockaddr_in, sockaddr_in6}; + // Same nominal type as `bun_sys::posix::sockaddr*`. + pub(crate) use bun_windows_sys::ws2_32::{sockaddr, sockaddr_in, sockaddr_in6}; } /// `c_ares` and `c_ares_draft` resolve to the same module. diff --git a/src/collections/hive_array.rs b/src/collections/hive_array.rs index 51cfe23d8f23..a07c0f599438 100644 --- a/src/collections/hive_array.rs +++ b/src/collections/hive_array.rs @@ -434,7 +434,7 @@ pub struct HiveSlot<'h, T, const CAPACITY: usize> { impl<'h, T, const CAPACITY: usize> HiveSlot<'h, T, CAPACITY> { /// Stable address of the slot. Safe to capture (e.g. register as a - /// libuv/uws user-data pointer) **before** [`write`](Self::write), as long + /// uws user-data pointer) **before** [`write`](Self::write), as long /// as nothing dereferences it until after `write()`. #[inline] pub fn addr(&self) -> NonNull { diff --git a/src/collections/vec_ext.rs b/src/collections/vec_ext.rs index 0568c6902ad0..2ba152652a9a 100644 --- a/src/collections/vec_ext.rs +++ b/src/collections/vec_ext.rs @@ -122,8 +122,7 @@ pub trait VecExt: Sized { unsafe fn writable_slice_exact(&mut self, additional: usize) -> &mut [T]; /// Reserves `additional` and returns the first `additional` slots of /// spare capacity as `MaybeUninit`. Safe sibling of [`writable_slice`]: - /// caller writes some prefix then calls `set_len` (or [`uv_commit`] for - /// `Vec`) to commit. Unlike `spare_capacity_mut()` the returned slice + /// caller writes some prefix then calls `set_len` to commit. Unlike `spare_capacity_mut()` the returned slice /// is exactly `additional` long, not `capacity - len`. fn reserve_spare(&mut self, additional: usize) -> &mut [core::mem::MaybeUninit]; /// `reserve(additional)` then [`expand_to_capacity`], returning the @@ -463,24 +462,6 @@ pub trait ByteVecExt { fn write(&mut self, str: &[u8]) -> Result; fn write_latin1(&mut self, str: &[u8]) -> Result; fn write_utf16(&mut self, str: &[u16]) -> Result; - - /// libuv `uv_alloc_cb`-style: ensure **at least** `suggested` bytes of - /// spare capacity past `len()`, typed `&mut [u8]` so the result can be - /// used directly as a `uv_buf_t` / `read(2)` target without a per-site - /// cast. - /// - /// # Safety - /// The returned bytes are **uninitialised**. Caller must only treat the - /// prefix actually written by the FFI/syscall as initialised (typically by - /// committing with [`uv_commit`]); the bytes must not be read before then. - unsafe fn uv_alloc_spare_u8(&mut self, suggested: usize) -> &mut [u8]; - /// Commit `nread` bytes that the FFI/syscall just wrote into the slice - /// returned by [`uv_alloc_spare_u8`]: bumps `len` by `nread`. Debug-asserts `len + nread <= capacity`. - /// - /// # Safety - /// The `nread` bytes at `[len, len + nread)` must have been initialised by - /// the preceding write into the spare slice. - unsafe fn uv_commit(&mut self, nread: usize); } impl ByteVecExt for Vec { @@ -511,18 +492,6 @@ impl ByteVecExt for Vec { strings::convert_utf16_to_utf8_append(self, str); Ok((self.len() - initial) as u32) } - #[inline] - unsafe fn uv_alloc_spare_u8(&mut self, suggested: usize) -> &mut [u8] { - // SAFETY: caller contract on `uv_alloc_spare_u8` — the returned uninit - // bytes are only read after the FFI-written prefix is committed. - unsafe { bun_core::vec::reserve_spare_bytes(self, suggested) } - } - #[inline] - unsafe fn uv_commit(&mut self, nread: usize) { - // SAFETY: caller contract on `uv_commit` — `[len, len+nread)` was - // initialised by the preceding write into the spare slice. - unsafe { bun_core::vec::commit_spare(self, nread) } - } } impl crate::pool::ObjectPoolType for Vec { diff --git a/src/crash_handler/lib.rs b/src/crash_handler/lib.rs index 4ae24e90d6b9..082ba816de84 100644 --- a/src/crash_handler/lib.rs +++ b/src/crash_handler/lib.rs @@ -411,18 +411,7 @@ impl Write for StderrWriter { // per-fd lock, which can self-deadlock when the VEH crash handler // fires on a thread that faulted *inside* CRT stdio. WriteFile is // lock-free at the kernel32 layer. - // `WriteFile` is declared locally because `bun_windows_sys:: - // kernel32` does not (yet) export it (cf. src/sys/lib.rs). - #[link(name = "kernel32")] - unsafe extern "system" { - fn WriteFile( - hFile: bun_sys::windows::HANDLE, - lpBuffer: *const u8, - nNumberOfBytesToWrite: u32, - lpNumberOfBytesWritten: *mut u32, - lpOverlapped: *mut core::ffi::c_void, - ) -> i32; - } + use bun_sys::windows::kernel32::WriteFile; let h = bun_sys::windows::kernel32::GetStdHandle(bun_sys::windows::STD_ERROR_HANDLE); let mut written: u32 = 0; // SAFETY: `h` is the cached stderr HANDLE (or INVALID_HANDLE_VALUE, @@ -878,7 +867,7 @@ mod draft { .unwrap_or(b""); if write!( writer, - bun_core::pretty_fmt!("Bun encountered a crash when running a NAPI module that tried to call\nthe {s} libuv function.\n\nBun is actively working on supporting all libuv functions for POSIX\nsystems, please see this issue to track our progress:\n\nhttps://github.com/oven-sh/bun/issues/18546\n\n", true), + bun_core::pretty_fmt!("Bun encountered a crash when running a NAPI module that tried to call\nthe {s} libuv function.\n\nBun does not use libuv and implements only a few of its functions. This\nissue lists the functions and packages known to be affected:\n\nhttps://github.com/oven-sh/bun/issues/18546\n\n", true), bstr::BStr::new(name) ) .is_err() @@ -1108,7 +1097,7 @@ mod draft { .unwrap_or(b""); if write!( writer, - bun_core::pretty_fmt!("Bun encountered a crash when running a NAPI module that tried to call\nthe {s} libuv function.\n\nBun is actively working on supporting all libuv functions for POSIX\nsystems, please see this issue to track our progress:\n\nhttps://github.com/oven-sh/bun/issues/18546\n\n", true), + bun_core::pretty_fmt!("Bun encountered a crash when running a NAPI module that tried to call\nthe {s} libuv function.\n\nBun does not use libuv and implements only a few of its functions. This\nissue lists the functions and packages known to be affected:\n\nhttps://github.com/oven-sh/bun/issues/18546\n\n", true), bstr::BStr::new(name) ) .is_err() @@ -1910,17 +1899,13 @@ mod draft { let handle = bun_core::WINDOWS_SEGFAULT_HANDLE.swap(core::ptr::null_mut(), Ordering::Relaxed); if !handle.is_null() { - // SAFETY: handle was returned by AddVectoredExceptionHandler and - // not yet removed (atomically claimed via the swap above). - let rc = - unsafe { bun_sys::windows::kernel32::RemoveVectoredExceptionHandler(handle) }; + // `handle` was returned by AddVectoredExceptionHandler and not + // yet removed (atomically claimed via the swap above). + let rc = bun_sys::windows::kernel32::RemoveVectoredExceptionHandler(handle); debug_assert!(rc != 0); } - // SAFETY: no memory-safety preconditions; clears the top-level - // filter back to the OS default. - unsafe { - bun_sys::windows::kernel32::SetUnhandledExceptionFilter(None); - } + // Clears the top-level filter back to the OS default. + bun_sys::windows::kernel32::SetUnhandledExceptionFilter(None); return; } @@ -2423,10 +2408,19 @@ mod draft { let base_address = module as usize; - let mut temp: [u16; 512] = [0; 512]; - let name = bun_sys::windows::get_module_name_w(module, &mut temp)?; - - let image_path = bun_sys::windows::exe_path_w(); + let object = if base_address == bun_sys::windows::exe_image_range().start { + None + } else { + let mut temp: [u16; 512] = [0; 512]; + let name = bun_sys::windows::get_module_name_w(module, &mut temp)?; + // GetModuleFileNameW output never has a trailing separator + // or bare drive prefix, so `basename_windows`'s + // stripping is a no-op on this domain. + let basename = bun_paths::basename_windows(name); + Some(Box::<[u8]>::from( + &*strings::convert_utf16_to_utf8_in_buffer(name_bytes, basename), + )) + }; return Some(StackLine { // To remap this, `pdb-addr2line --exe bun.pdb 0x123456` @@ -2435,18 +2429,7 @@ mod draft { // handler (which would escalate to a double-panic and lose the // entire report). address: addr.wrapping_sub(base_address) as i32, - - object: if name != image_path.as_slice() { - // GetModuleFileNameW output never has a trailing separator - // or bare drive prefix, so `basename_windows`'s - // stripping is a no-op on this domain. - let basename = bun_paths::basename_windows(name); - Some(Box::<[u8]>::from( - &*strings::convert_utf16_to_utf8_in_buffer(name_bytes, basename), - )) - } else { - None - }, + object, }); } #[cfg(target_os = "macos")] diff --git a/src/dns/lib.rs b/src/dns/lib.rs index 8cec2841b134..3a6074c93695 100644 --- a/src/dns/lib.rs +++ b/src/dns/lib.rs @@ -343,10 +343,7 @@ impl GetAddrInfoResult { } Some(GetAddrInfoResult { // SAFETY: `ai_addr` is non-null and points to a valid sockaddr per - // getaddrinfo's contract. `.cast()` erases the nominal-type - // mismatch on Windows (ws2_32::sockaddr ↔ the libuv-sys mirror - // `bun_sys::posix::sockaddr` routes to) — both are the 16-byte - // ws2def.h `SOCKADDR`. + // getaddrinfo's contract. address: unsafe { Address::init_posix(sockaddr.cast()) }, // no TTL in POSIX getaddrinfo() ttl: 0, @@ -485,7 +482,7 @@ pub fn is_valid_hostname(name: &[u8]) -> bool { } /// The process-wide DNS -/// cache lives in `bun_runtime` (it owns libinfo/libuv worker threads + JSC +/// cache lives in `bun_runtime` (it owns the resolver backends + JSC /// stat counters). Lower-tier crates (`bun_http`, `bun_install`) reach it via /// the link-time `Bun__addrinfo_*` family — same mechanism usockets C uses — /// rather than a `bun_runtime` crate dep, which would cycle. diff --git a/src/errno/Cargo.toml b/src/errno/Cargo.toml index 689b112a09ec..52850a0b5cfa 100644 --- a/src/errno/Cargo.toml +++ b/src/errno/Cargo.toml @@ -13,5 +13,4 @@ workspace = true strum.workspace = true enum-map.workspace = true bun_core.workspace = true -bun_libuv_sys.workspace = true bun_windows_sys.workspace = true diff --git a/src/errno/darwin_errno.rs b/src/errno/darwin_errno.rs index b75b90ebc52b..aca46af94f5b 100644 --- a/src/errno/darwin_errno.rs +++ b/src/errno/darwin_errno.rs @@ -136,16 +136,16 @@ pub mod uv_e { // Darwin lacks (ECHARSET / ENONET / EREMOTEIO / EUNATCH). macro_rules! __v { (CHARSET, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (NONET, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (REMOTEIO, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (UNATCH, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; ($i:tt, $e:tt, $uv:tt) => { super::SystemErrno::$e as i32 diff --git a/src/errno/freebsd_errno.rs b/src/errno/freebsd_errno.rs index f475ece1a7c4..ee5534aa7d0c 100644 --- a/src/errno/freebsd_errno.rs +++ b/src/errno/freebsd_errno.rs @@ -132,22 +132,22 @@ pub mod uv_e { // FreeBSD lacks (ECHARSET / ENONET / ENOTSUP / EREMOTEIO / ENODATA / EUNATCH). macro_rules! __v { (CHARSET, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (NONET, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (NOTSUP, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (REMOTEIO, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (NODATA, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (UNATCH, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; ($i:tt, $e:tt, $uv:tt) => { super::SystemErrno::$e as i32 diff --git a/src/errno/lib.rs b/src/errno/lib.rs index 2943fc5cee16..e73e12a024d6 100644 --- a/src/errno/lib.rs +++ b/src/errno/lib.rs @@ -44,7 +44,7 @@ macro_rules! __decl_uv_e { ( $( $ident:ident = $value:expr => $display:literal ),+ $(,)? ) => { $( pub const $ident: i32 = $value; )+ - /// Full (negated code, name) table: libuv-synthetic codes (vendor/libuv/include/uv/errno.h) + /// Full (negated code, name) table: libuv-synthetic codes (libuv's include/uv/errno.h) /// first, then per-OS rows. Consumed by `name()` and node:util `getSystemErrorMap()`. pub static ENTRIES: &[(i32, &'static str)] = &[ (-4095, "EOF"), @@ -86,21 +86,24 @@ macro_rules! __decl_uv_e { // The (IDENT, "E…") column pair is byte-for-byte identical across // linux/darwin/freebsd/windows; only the middle `i32` value differs by design // (native `SystemErrno::$e as i32` on POSIX vs libuv-synthetic -// `-bun_libuv_sys::UV_E*` on Windows / for codes the host OS lacks). Rather +// `-uv_codes::UV_E*` on Windows / for codes the host OS lacks). Rather // than re-list the rows 4×, the caller supplies a tiny *value-producer* macro // `$cb!($id, $e, $uv) -> i32-expr` and this forwards each row to the existing -// `__decl_uv_e!` expander (consts + reverse `name()` fn). +// `__decl_uv_e!` expander (consts + reverse `name()` fn), or to the `$sink` named. // // `$id`/`$e`/`$uv` are passed as **literal** tokens (never captured as // `:ident`), so the per-OS `$cb` can override individual rows by literal-token -// match — e.g. `(CHARSET, $e:tt, $uv:tt) => { -::bun_libuv_sys::$uv }` — while +// match — e.g. `(CHARSET, $e:tt, $uv:tt) => { -$crate::uv_codes::$uv }` — while // a final `($i:tt, $e:tt, $uv:tt)` arm handles the native default. // ────────────────────────────────────────────────────────────────────────── #[macro_export] #[doc(hidden)] macro_rules! __uv_e_rows { ($cb:ident) => { - $crate::__decl_uv_e! { + $crate::__uv_e_rows!($cb => __decl_uv_e); + }; + ($cb:ident => $sink:ident) => { + $crate::$sink! { // Rust idents can't start with a digit → `_2BIG`. _2BIG = $cb!(_2BIG, E2BIG, UV_E2BIG) => "E2BIG", ACCES = $cb!(ACCES, EACCES, UV_EACCES) => "EACCES", @@ -175,6 +178,8 @@ macro_rules! __uv_e_rows { }; } +pub mod uv_codes; + #[cfg(target_os = "macos")] pub mod darwin_errno; #[cfg(target_os = "macos")] @@ -200,8 +205,8 @@ pub use windows_errno::{posix, *}; // // Landed here so the errno // crate stays leaf (T0) and bun_sys (T≥1) imports forward. Windows keeps its -// own divergent `mod posix` in windows_errno.rs (no `errno()` fn, unprefixed -// `E`); this block is the shared POSIX-target definition. +// own divergent `mod posix` in windows_errno.rs (no `errno()` fn); this block +// is the shared POSIX-target definition. // ────────────────────────────────────────────────────────────────────────── #[cfg(not(windows))] #[allow(non_camel_case_types)] @@ -257,15 +262,15 @@ pub fn last_error() -> E { /// Decode a Node-style **negated** errno (`c_int`, as written by /// `Error::to_system_error`) back into an `E`. The input crosses FFI /// (BunObject.cpp `SystemError__*`) and is NOT trusted to be a declared -/// discriminant. `0` is `SUCCESS`; an unmapped value is `EUNKNOWN`. +/// discriminant. `0` is `SUCCESS`; an unmapped value is `EUNKNOWN`. On Windows +/// what was written is a `UV_E*` number where libuv has one for the errno. #[inline] pub fn e_from_negated(errno: core::ffi::c_int) -> E { let n = errno.wrapping_neg(); #[cfg(windows)] { - u16::try_from(n) - .ok() - .and_then(E::try_from_raw) + uv_to_e(errno) + .or_else(|| u16::try_from(n).ok().and_then(E::from_repr)) .unwrap_or(E::EUNKNOWN) } #[cfg(not(windows))] @@ -344,9 +349,6 @@ fn system_errno_name(errno: i32) -> Option<&'static str> { } #[cfg(windows)] { - // Windows libuv errnos arrive negated; abs-normalise before the - // lookup. `from_repr` (strum::FromRepr) - // covers BOTH the dense 0..=137 range and the sparse UV_* tags. let n = errno.unsigned_abs(); if n == 0 { return None; @@ -358,8 +360,7 @@ fn system_errno_name(errno: i32) -> Option<&'static str> { } } -/// Length of the dense `0..MAX` prefix of `SystemErrno` (on Windows, the -/// dense head before the sparse UV_* range). Exposed so bun_core can pre-seed its +/// Length of the dense `0..MAX` range of `SystemErrno`. Exposed so bun_core can pre-seed its /// interned `ERRNO_MAP` without a second hand-written per-OS length table. #[inline] const fn system_errno_max_dense() -> u32 { @@ -443,7 +444,6 @@ mod errno_name_tests { ); } // One past the dense end → None. - #[cfg(not(windows))] assert_eq!(system_errno_name(max as i32), None); // Spot-check the last entry on each platform. @@ -455,12 +455,6 @@ mod errno_name_tests { #[cfg(windows)] { assert_eq!(system_errno_name(137), Some("EFTYPE")); - // Sparse UV_* range round-trips. - assert_eq!(system_errno_name(-4058), Some("UV_ENOENT")); - assert_eq!(system_errno_name(-4092), Some("UV_EACCES")); - assert_eq!(system_errno_name(-4095), Some("UV_EOF")); - assert_eq!(system_errno_name(-3008), Some("UV_EAI_NONAME")); - assert_eq!(system_errno_name(-5000), None); } #[cfg(target_os = "macos")] { @@ -533,16 +527,6 @@ mod errno_name_tests { } } - /// Matters on Windows, where `E` is a separate enum with unprefixed variant names. - #[test] - fn e_spells_like_system_errno() { - assert_eq!(<&'static str>::from(E::ENOENT), "ENOENT"); - assert_eq!(<&'static str>::from(E::E2BIG), "E2BIG"); - assert_eq!(<&'static str>::from(E::SUCCESS), "SUCCESS"); - #[cfg(windows)] - assert_eq!(<&'static str>::from(E::UV_ENOENT), "UV_ENOENT"); - } - #[test] fn coreutils_map() { assert_eq!( diff --git a/src/errno/linux_errno.rs b/src/errno/linux_errno.rs index 3e54e383e449..1fd21a4f81cf 100644 --- a/src/errno/linux_errno.rs +++ b/src/errno/linux_errno.rs @@ -171,10 +171,10 @@ pub mod uv_e { // Linux lacks (no kernel ECHARSET / EFTYPE). macro_rules! __v { (CHARSET, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; (FTYPE, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; ($i:tt, $e:tt, $uv:tt) => { super::SystemErrno::$e as i32 diff --git a/src/errno/uv_codes.rs b/src/errno/uv_codes.rs new file mode 100644 index 000000000000..fa049bc9e790 --- /dev/null +++ b/src/errno/uv_codes.rs @@ -0,0 +1,80 @@ +//! `UV_E*` error numbers (`uv/errno.h`, Windows values). +//! +//! These are what Node reports in `err.errno` on Windows and what +//! `process.binding("uv")` / `util.getSystemErrorMap()` expose on every +//! platform, so the values are fixed. + +use core::ffi::c_int; + +pub const UV_E2BIG: c_int = -4093; +pub const UV_EACCES: c_int = -4092; +pub const UV_EADDRINUSE: c_int = -4091; +pub const UV_EADDRNOTAVAIL: c_int = -4090; +pub const UV_EAFNOSUPPORT: c_int = -4089; +pub const UV_EAGAIN: c_int = -4088; +pub const UV_EAI_AGAIN: c_int = -3001; +pub const UV_EALREADY: c_int = -4084; +pub const UV_EBADF: c_int = -4083; +pub const UV_EBUSY: c_int = -4082; +pub const UV_ECANCELED: c_int = -4081; +pub const UV_ECHARSET: c_int = -4080; +pub const UV_ECONNABORTED: c_int = -4079; +pub const UV_ECONNREFUSED: c_int = -4078; +pub const UV_ECONNRESET: c_int = -4077; +pub const UV_EDESTADDRREQ: c_int = -4076; +pub const UV_EEXIST: c_int = -4075; +pub const UV_EFAULT: c_int = -4074; +pub const UV_EFBIG: c_int = -4036; +pub const UV_EHOSTUNREACH: c_int = -4073; +pub const UV_EINTR: c_int = -4072; +pub const UV_EINVAL: c_int = -4071; +pub const UV_EIO: c_int = -4070; +pub const UV_EISCONN: c_int = -4069; +pub const UV_EISDIR: c_int = -4068; +pub const UV_ELOOP: c_int = -4067; +pub const UV_EMFILE: c_int = -4066; +pub const UV_EMSGSIZE: c_int = -4065; +pub const UV_ENAMETOOLONG: c_int = -4064; +pub const UV_ENETDOWN: c_int = -4063; +pub const UV_ENETUNREACH: c_int = -4062; +pub const UV_ENFILE: c_int = -4061; +pub const UV_ENOBUFS: c_int = -4060; +pub const UV_ENODEV: c_int = -4059; +pub const UV_ENOENT: c_int = -4058; +pub const UV_ENOMEM: c_int = -4057; +pub const UV_ENONET: c_int = -4056; +pub const UV_ENOPROTOOPT: c_int = -4035; +pub const UV_ENOSPC: c_int = -4055; +pub const UV_ENOSYS: c_int = -4054; +pub const UV_ENOTCONN: c_int = -4053; +pub const UV_ENOTDIR: c_int = -4052; +pub const UV_ENOTEMPTY: c_int = -4051; +pub const UV_ENOTSOCK: c_int = -4050; +pub const UV_ENOTSUP: c_int = -4049; +pub const UV_EOVERFLOW: c_int = -4026; +pub const UV_EPERM: c_int = -4048; +pub const UV_EPIPE: c_int = -4047; +pub const UV_EPROTO: c_int = -4046; +pub const UV_EPROTONOSUPPORT: c_int = -4045; +pub const UV_EPROTOTYPE: c_int = -4044; +pub const UV_ERANGE: c_int = -4034; +pub const UV_EROFS: c_int = -4043; +pub const UV_ESHUTDOWN: c_int = -4042; +pub const UV_ESPIPE: c_int = -4041; +pub const UV_ESRCH: c_int = -4040; +pub const UV_ETIMEDOUT: c_int = -4039; +pub const UV_ETXTBSY: c_int = -4038; +pub const UV_EXDEV: c_int = -4037; +pub const UV_UNKNOWN: c_int = -4094; +pub const UV_EOF: c_int = -4095; +pub const UV_ENXIO: c_int = -4033; +pub const UV_EMLINK: c_int = -4032; +pub const UV_EHOSTDOWN: c_int = -4031; +pub const UV_EREMOTEIO: c_int = -4030; +pub const UV_ENOTTY: c_int = -4029; +pub const UV_EFTYPE: c_int = -4028; +pub const UV_EILSEQ: c_int = -4027; +pub const UV_ESOCKTNOSUPPORT: c_int = -4025; +pub const UV_ENODATA: c_int = -4024; +pub const UV_EUNATCH: c_int = -4023; +pub const UV_ENOEXEC: c_int = -4022; diff --git a/src/errno/windows_errno.rs b/src/errno/windows_errno.rs index 44e099ec4ef1..358ab896c22b 100644 --- a/src/errno/windows_errno.rs +++ b/src/errno/windows_errno.rs @@ -6,332 +6,14 @@ use core::ffi::c_int; -// `uv::UV_E*` constants come from `bun_libuv_sys` (leaf); +// `uv::UV_E*` constants come from `crate::uv_codes`; // `Win32Error` / `NTSTATUS` / the NTSTATUS→errno mapper live locally in this // module (their only external use is via `SystemErrno::init`, defined here). pub use self::windows::{NTSTATUS, Win32Error, Win32ErrorExt}; -use bun_libuv_sys as uv; +use crate::uv_codes as uv; -// ────────────────────────────────────────────────────────────────────────── -// UV_* errno X-macro -// -// Single source of truth for the 86 UV_* variants that form the tail of BOTH -// `enum E` and `enum SystemErrno`. Both enum tails are -// driven from this one list so they cannot drift. (The UV_*→E* fold-down -// lives in `bun_libuv_sys::uv_err_to_e_discriminant`.) -// -// Entry shape: -// [UV_X => EX] — UV_X has a non-UV_ counterpart `SystemErrno::EX` -// [UV_X] — no counterpart (EAI_* resolver codes, UNKNOWN, ERRNO_MAX) -// -// ORDER IS LOAD-BEARING: `enum_map::Enum` derives ordinals from declaration -// order, and `SystemErrno::to_e` maps by discriminant, so the two enums -// MUST stay in lockstep. Editing this list updates both atomically. -// ────────────────────────────────────────────────────────────────────────── - -/// X-macro: invokes `$cb! { $($pre)* @uv [UV_X => EX] [UV_Y] … }`. -macro_rules! for_each_uv_errno { - ($cb:ident { $($pre:tt)* }) => { - $cb! { $($pre)* @uv - [UV_E2BIG => E2BIG] [UV_EACCES => EACCES] [UV_EADDRINUSE => EADDRINUSE] - [UV_EADDRNOTAVAIL => EADDRNOTAVAIL] [UV_EAFNOSUPPORT => EAFNOSUPPORT] - [UV_EAGAIN => EAGAIN] [UV_EAI_ADDRFAMILY] [UV_EAI_AGAIN] [UV_EAI_BADFLAGS] - [UV_EAI_BADHINTS] [UV_EAI_CANCELED] [UV_EAI_FAIL] [UV_EAI_FAMILY] - [UV_EAI_MEMORY] [UV_EAI_NODATA] [UV_EAI_NONAME] [UV_EAI_OVERFLOW] - [UV_EAI_PROTOCOL] [UV_EAI_SERVICE] [UV_EAI_SOCKTYPE] [UV_EALREADY => EALREADY] - [UV_EBADF => EBADF] [UV_EBUSY => EBUSY] [UV_ECANCELED => ECANCELED] - [UV_ECHARSET => ECHARSET] [UV_ECONNABORTED => ECONNABORTED] - [UV_ECONNREFUSED => ECONNREFUSED] [UV_ECONNRESET => ECONNRESET] - [UV_EDESTADDRREQ => EDESTADDRREQ] [UV_EEXIST => EEXIST] [UV_EFAULT => EFAULT] - [UV_EFBIG => EFBIG] [UV_EHOSTUNREACH => EHOSTUNREACH] [UV_EINVAL => EINVAL] - [UV_EINTR => EINTR] [UV_EISCONN => EISCONN] [UV_EIO => EIO] [UV_ELOOP => ELOOP] - [UV_EISDIR => EISDIR] [UV_EMSGSIZE => EMSGSIZE] [UV_EMFILE => EMFILE] - [UV_ENETDOWN => ENETDOWN] [UV_ENAMETOOLONG => ENAMETOOLONG] [UV_ENFILE => ENFILE] - [UV_ENETUNREACH => ENETUNREACH] [UV_ENODEV => ENODEV] [UV_ENOBUFS => ENOBUFS] - [UV_ENOMEM => ENOMEM] [UV_ENOENT => ENOENT] [UV_ENOPROTOOPT => ENOPROTOOPT] - [UV_ENONET => ENONET] [UV_ENOSYS => ENOSYS] [UV_ENOSPC => ENOSPC] - [UV_ENOTDIR => ENOTDIR] [UV_ENOTCONN => ENOTCONN] [UV_ENOTSOCK => ENOTSOCK] - [UV_ENOTEMPTY => ENOTEMPTY] [UV_EOVERFLOW => EOVERFLOW] [UV_ENOTSUP => ENOTSUP] - [UV_EPIPE => EPIPE] [UV_EPERM => EPERM] [UV_EPROTONOSUPPORT => EPROTONOSUPPORT] - [UV_EPROTO => EPROTO] [UV_ERANGE => ERANGE] [UV_EPROTOTYPE => EPROTOTYPE] - [UV_ESHUTDOWN => ESHUTDOWN] [UV_EROFS => EROFS] [UV_ESRCH => ESRCH] - [UV_ESPIPE => ESPIPE] [UV_ETXTBSY => ETXTBSY] [UV_ETIMEDOUT => ETIMEDOUT] - [UV_UNKNOWN] [UV_EXDEV => EXDEV] [UV_ENXIO => ENXIO] [UV_EOF => EOF] - [UV_EHOSTDOWN => EHOSTDOWN] [UV_EMLINK => EMLINK] [UV_ENOTTY => ENOTTY] - [UV_EREMOTEIO => EREMOTEIO] [UV_EILSEQ => EILSEQ] [UV_EFTYPE => EFTYPE] - [UV_ENODATA => ENODATA] [UV_ESOCKTNOSUPPORT => ESOCKTNOSUPPORT] [UV_ERRNO_MAX] - [UV_EUNATCH => EUNATCH] [UV_ENOEXEC => ENOEXEC] - } - }; -} - -/// Callback: emit `$pre` enum decl with the UV_* tail appended verbatim. -macro_rules! __errno_enum_with_uv_tail { - ( - $(#[$m:meta])* $vis:vis enum $name:ident { $($head:tt)* } - @uv $( [ $uv:ident $(=> $sys:ident)? ] )* - ) => { - $(#[$m])* - $vis enum $name { - $($head)* - $( $uv = (-uv::$uv) as u16, )* - } - }; -} - -// ────────────────────────────────────────────────────────────────────────── -// E -// ────────────────────────────────────────────────────────────────────────── - -for_each_uv_errno! { __errno_enum_with_uv_tail { -#[repr(u16)] -#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, strum::FromRepr, enum_map::Enum)] -pub enum E { - SUCCESS = 0, - PERM = 1, - NOENT = 2, - SRCH = 3, - INTR = 4, - IO = 5, - NXIO = 6, - // Rust identifiers cannot start with a digit. - _2BIG = 7, - NOEXEC = 8, - BADF = 9, - CHILD = 10, - AGAIN = 11, - NOMEM = 12, - ACCES = 13, - FAULT = 14, - NOTBLK = 15, - BUSY = 16, - EXIST = 17, - XDEV = 18, - NODEV = 19, - NOTDIR = 20, - ISDIR = 21, - INVAL = 22, - NFILE = 23, - MFILE = 24, - NOTTY = 25, - TXTBSY = 26, - FBIG = 27, - NOSPC = 28, - SPIPE = 29, - ROFS = 30, - MLINK = 31, - PIPE = 32, - DOM = 33, - RANGE = 34, - DEADLK = 35, - NAMETOOLONG = 36, - NOLCK = 37, - NOSYS = 38, - NOTEMPTY = 39, - LOOP = 40, - WOULDBLOCK = 41, - NOMSG = 42, - IDRM = 43, - CHRNG = 44, - L2NSYNC = 45, - L3HLT = 46, - L3RST = 47, - LNRNG = 48, - UNATCH = 49, - NOCSI = 50, - L2HLT = 51, - BADE = 52, - BADR = 53, - XFULL = 54, - NOANO = 55, - BADRQC = 56, - BADSLT = 57, - DEADLOCK = 58, - BFONT = 59, - NOSTR = 60, - NODATA = 61, - TIME = 62, - NOSR = 63, - NONET = 64, - NOPKG = 65, - REMOTE = 66, - NOLINK = 67, - ADV = 68, - SRMNT = 69, - COMM = 70, - PROTO = 71, - MULTIHOP = 72, - DOTDOT = 73, - BADMSG = 74, - OVERFLOW = 75, - NOTUNIQ = 76, - BADFD = 77, - REMCHG = 78, - LIBACC = 79, - LIBBAD = 80, - LIBSCN = 81, - LIBMAX = 82, - LIBEXEC = 83, - ILSEQ = 84, - RESTART = 85, - STRPIPE = 86, - USERS = 87, - NOTSOCK = 88, - DESTADDRREQ = 89, - MSGSIZE = 90, - PROTOTYPE = 91, - NOPROTOOPT = 92, - PROTONOSUPPORT = 93, - SOCKTNOSUPPORT = 94, - NOTSUP = 95, - PFNOSUPPORT = 96, - AFNOSUPPORT = 97, - ADDRINUSE = 98, - ADDRNOTAVAIL = 99, - NETDOWN = 100, - NETUNREACH = 101, - NETRESET = 102, - CONNABORTED = 103, - CONNRESET = 104, - NOBUFS = 105, - ISCONN = 106, - NOTCONN = 107, - SHUTDOWN = 108, - TOOMANYREFS = 109, - TIMEDOUT = 110, - CONNREFUSED = 111, - HOSTDOWN = 112, - HOSTUNREACH = 113, - ALREADY = 114, - INPROGRESS = 115, - STALE = 116, - UCLEAN = 117, - NOTNAM = 118, - NAVAIL = 119, - ISNAM = 120, - REMOTEIO = 121, - DQUOT = 122, - NOMEDIUM = 123, - MEDIUMTYPE = 124, - CANCELED = 125, - NOKEY = 126, - KEYEXPIRED = 127, - KEYREVOKED = 128, - KEYREJECTED = 129, - OWNERDEAD = 130, - NOTRECOVERABLE = 131, - RFKILL = 132, - HWPOISON = 133, - UNKNOWN = 134, - CHARSET = 135, - EOF = 136, - FTYPE = 137, -} -}} // ← UV_* tail appended by `for_each_uv_errno!` - -impl E { - /// An undeclared discriminant maps to `UNKNOWN`. - #[inline] - pub const fn from_raw(n: u16) -> Self { - match Self::from_repr(n) { - Some(e) => e, - None => Self::UNKNOWN, - } - } - - /// Checked discriminant lookup — - /// returns `None` for any `n` that is not a declared variant. The `E` enum - /// is sparse (dense 0..=137, then isolated UV_* values in the 3000–4095 - /// range), so a `< UV_ERRNO_MAX` range check is NOT sufficient. - #[inline] - pub fn try_from_raw(n: u16) -> Option { - // `strum::FromRepr` generates a checked `match` on every discriminant. - E::from_repr(n) - } - - // Cross-platform aliases: on POSIX `E` is a `type` alias for `SystemErrno` - // (whose variants are `EPERM`/`ENOENT`/…), so call sites uniformly write - // `E::ENOENT`. On Windows `E` is its own enum with bare names; expose the - // E-prefixed spellings as zero-cost associated consts so the same source - // compiles on both targets. - pub const EPERM: E = E::PERM; - pub const ENOENT: E = E::NOENT; - pub const ESRCH: E = E::SRCH; - pub const EINTR: E = E::INTR; - pub const EIO: E = E::IO; - pub const ENXIO: E = E::NXIO; - pub const E2BIG: E = E::_2BIG; - pub const ENOEXEC: E = E::NOEXEC; - pub const EBADF: E = E::BADF; - pub const ECHILD: E = E::CHILD; - pub const EAGAIN: E = E::AGAIN; - pub const ENOMEM: E = E::NOMEM; - pub const EACCES: E = E::ACCES; - pub const EFAULT: E = E::FAULT; - pub const EBUSY: E = E::BUSY; - pub const EEXIST: E = E::EXIST; - pub const EXDEV: E = E::XDEV; - pub const ENODEV: E = E::NODEV; - pub const ENOTDIR: E = E::NOTDIR; - pub const EISDIR: E = E::ISDIR; - pub const EINVAL: E = E::INVAL; - pub const ENFILE: E = E::NFILE; - pub const EMFILE: E = E::MFILE; - pub const ENOTTY: E = E::NOTTY; - pub const ETXTBSY: E = E::TXTBSY; - pub const EFBIG: E = E::FBIG; - pub const ENOSPC: E = E::NOSPC; - pub const ESPIPE: E = E::SPIPE; - pub const EROFS: E = E::ROFS; - pub const EMLINK: E = E::MLINK; - pub const EPIPE: E = E::PIPE; - pub const ERANGE: E = E::RANGE; - pub const ENAMETOOLONG: E = E::NAMETOOLONG; - pub const ENOSYS: E = E::NOSYS; - pub const ENOTEMPTY: E = E::NOTEMPTY; - pub const ELOOP: E = E::LOOP; - pub const EWOULDBLOCK: E = E::WOULDBLOCK; - pub const EOVERFLOW: E = E::OVERFLOW; - pub const ENOTSOCK: E = E::NOTSOCK; - pub const EMSGSIZE: E = E::MSGSIZE; - pub const EPROTONOSUPPORT: E = E::PROTONOSUPPORT; - pub const ENOTSUP: E = E::NOTSUP; - pub const EOPNOTSUPP: E = E::NOTSUP; - pub const EAFNOSUPPORT: E = E::AFNOSUPPORT; - pub const EADDRINUSE: E = E::ADDRINUSE; - pub const EADDRNOTAVAIL: E = E::ADDRNOTAVAIL; - pub const ENETUNREACH: E = E::NETUNREACH; - pub const ECONNABORTED: E = E::CONNABORTED; - pub const ECONNRESET: E = E::CONNRESET; - pub const ENOBUFS: E = E::NOBUFS; - pub const EISCONN: E = E::ISCONN; - pub const ENOTCONN: E = E::NOTCONN; - pub const ETIMEDOUT: E = E::TIMEDOUT; - pub const ECONNREFUSED: E = E::CONNREFUSED; - pub const EHOSTUNREACH: E = E::HOSTUNREACH; - pub const EALREADY: E = E::ALREADY; - pub const EINPROGRESS: E = E::INPROGRESS; - pub const ECANCELED: E = E::CANCELED; - pub const EUNKNOWN: E = E::UNKNOWN; - pub const ECHARSET: E = E::CHARSET; - pub const EFTYPE: E = E::FTYPE; -} - -/// `ENOENT`, as the POSIX alias `E = SystemErrno` spells it, not the variant name `NOENT`. -impl From for &'static str { - #[inline] - fn from(e: E) -> &'static str { - // Same discriminant set; `SystemErrno::to_e` is the reverse cast. - <&'static str>::from(SystemErrno::from_raw(e as u16)) - } -} - -impl From<&E> for &'static str { - #[inline] - fn from(e: &E) -> &'static str { - <&'static str>::from(*e) - } -} +/// As on POSIX. +pub type E = SystemErrno; /// Mirrors `bun_errno::posix` on POSIX targets so callers can `use /// bun_errno::posix::*` unconditionally. Windows has no real `mode_t`/kernel @@ -378,7 +60,6 @@ pub fn last_error() -> E { // SystemErrno // ────────────────────────────────────────────────────────────────────────── -for_each_uv_errno! { __errno_enum_with_uv_tail { #[repr(u16)] #[derive( Copy, @@ -536,13 +217,12 @@ pub enum SystemErrno { EOF = 136, EFTYPE = 137, } -}} // ← UV_* tail appended by `for_each_uv_errno!` /// Type-dispatch shim for `SystemErrno::init`. /// Covers every concrete type the codebase actually passes — `i64` /// (POSIX-shaped shared call sites), `u32`/`DWORD` (a Win32/WSA code carried -/// as an integer), and `c_int` (`Bun__errnoName`, which may receive a libuv -/// code). A typed `Win32Error` uses `Win32ErrorExt` instead. +/// as an integer), and `c_int` (`Bun__errnoName`, which may receive a negative +/// `UV_E*` number). A typed `Win32Error` uses `Win32ErrorExt` instead. pub trait SystemErrnoInit { fn into_system_errno(self) -> Option; } @@ -594,12 +274,6 @@ impl SystemErrno { /// `EOPNOTSUPP` name so cross-platform `match` arms compile unchanged. pub const EOPNOTSUPP: SystemErrno = SystemErrno::ENOTSUP; - #[inline] - pub const fn to_e(self) -> E { - // SystemErrno and E share identical #[repr(u16)] discriminant sets. - E::from_raw(self as u16) - } - /// Cross-platform `SystemErrno::init` — POSIX targets define a single /// `init(i64)`; Windows dispatches on the integer type via /// `SystemErrnoInit` so shared call sites can keep writing @@ -634,7 +308,9 @@ impl SystemErrno { return Self::init_win32_error(Win32Error::from_raw(code)); } // uv error codes (negated to positive u16 in the SystemErrno discriminant space) - if let Some(mapped) = uv_code_to_system_errno(code) { + if let Some(mapped) = uv_to_e(-c_int::from(code)) + && mapped != SystemErrno::EUNKNOWN + { return Some(mapped); } if cfg!(debug_assertions) { @@ -647,7 +323,7 @@ impl SystemErrno { pub(crate) fn init_win32_error(code: Win32Error) -> Option { use Win32Error as W; Some(match code { - W::NOACCESS => SystemErrno::EACCES, + W::NOACCESS => SystemErrno::EFAULT, W::WSAEACCES => SystemErrno::EACCES, W::ELEVATION_REQUIRED => SystemErrno::EACCES, W::CANT_ACCESS_FILE => SystemErrno::EACCES, @@ -660,6 +336,7 @@ impl SystemErrno { W::INVALID_FLAGS => SystemErrno::EBADF, W::INVALID_HANDLE => SystemErrno::EBADF, W::LOCK_VIOLATION => SystemErrno::EBUSY, + W::DELETE_PENDING => SystemErrno::EBUSY, W::PIPE_BUSY => SystemErrno::EBUSY, W::SHARING_VIOLATION => SystemErrno::EBUSY, W::OPERATION_ABORTED => SystemErrno::ECANCELED, @@ -673,7 +350,7 @@ impl SystemErrno { W::WSAECONNRESET => SystemErrno::ECONNRESET, W::ALREADY_EXISTS => SystemErrno::EEXIST, W::FILE_EXISTS => SystemErrno::EEXIST, - W::BUFFER_OVERFLOW => SystemErrno::EFAULT, + W::BUFFER_OVERFLOW => SystemErrno::ENAMETOOLONG, W::WSAEFAULT => SystemErrno::EFAULT, W::HOST_UNREACHABLE => SystemErrno::EHOSTUNREACH, W::WSAEHOSTUNREACH => SystemErrno::EHOSTUNREACH, @@ -709,7 +386,7 @@ impl SystemErrno { W::WSAENETUNREACH => SystemErrno::ENETUNREACH, W::WSAENOBUFS => SystemErrno::ENOBUFS, W::BAD_PATHNAME => SystemErrno::ENOENT, - W::DIRECTORY => SystemErrno::ENOTDIR, + W::DIRECTORY => SystemErrno::ENOENT, W::ENVVAR_NOT_FOUND => SystemErrno::ENOENT, W::FILE_NOT_FOUND => SystemErrno::ENOENT, W::INVALID_NAME => SystemErrno::ENOENT, @@ -732,11 +409,11 @@ impl SystemErrno { W::WSAENOTSOCK => SystemErrno::ENOTSOCK, W::NOT_SUPPORTED => SystemErrno::ENOTSUP, W::WSAEOPNOTSUPP => SystemErrno::ENOTSUP, - W::BROKEN_PIPE => SystemErrno::EPIPE, + W::BROKEN_PIPE => SystemErrno::EOF, W::ACCESS_DENIED => SystemErrno::EPERM, W::PRIVILEGE_NOT_HELD => SystemErrno::EPERM, W::BAD_PIPE => SystemErrno::EPIPE, - W::NO_DATA => SystemErrno::EPIPE, + W::NO_DATA => SystemErrno::EAGAIN, W::PIPE_NOT_CONNECTED => SystemErrno::EPIPE, W::WSAESHUTDOWN => SystemErrno::EPIPE, W::WSAEPROTONOSUPPORT => SystemErrno::EPROTONOSUPPORT, @@ -747,29 +424,57 @@ impl SystemErrno { W::INVALID_FUNCTION => SystemErrno::EISDIR, W::META_EXPANSION_TOO_LONG => SystemErrno::E2BIG, W::WSAESOCKTNOSUPPORT => SystemErrno::ESOCKTNOSUPPORT, - W::DELETE_PENDING => SystemErrno::EBUSY, W::BAD_EXE_FORMAT => SystemErrno::EFTYPE, _ => return None, }) } } -/// A negative libuv return code → `E`; a code libuv does not define is `UNKNOWN`. -pub fn translate_uv_error_to_e(code: c_int) -> E { - uv::uv_err_to_e_discriminant(code) - .and_then(E::try_from_raw) - .unwrap_or(E::UNKNOWN) +/// A Win32 error code → the negative `UV_E*` number Node reports for it +/// (`UV_UNKNOWN` when the table has no row). A value that is already `<= 0` +/// is returned as is. +#[unsafe(no_mangle)] +pub extern "C" fn Bun__translateWin32ErrorToUV(code: u32) -> c_int { + if code as c_int <= 0 { + return code as c_int; + } + let Ok(code) = u16::try_from(code) else { + return uv::UV_UNKNOWN; + }; + e_to_uv(Win32Error(code).to_e() as u16).unwrap_or(uv::UV_UNKNOWN) } -// Thin adapter over the canonical row table in `bun_libuv_sys::uv_err_to_e_discriminant`. -#[inline] -fn uv_code_to_system_errno(mag: u16) -> Option { - let d = uv::uv_err_to_e_discriminant(-c_int::from(mag))?; - // UV_EAI_* (≥3000) / UV_UNKNOWN have no non-UV_ counterpart. - if d >= 3000 || d == SystemErrno::EUNKNOWN as u16 { - return None; - } - SystemErrno::from_repr(d) +/// `__uv_e_rows!` sink: the rows as `(errno, UV_E*)` pairs. +#[macro_export] +#[doc(hidden)] +macro_rules! __decl_uv_pairs { + ( $( $ident:ident = $pair:expr => $display:literal ),+ $(,)? ) => { + static UV_PAIRS: &[(SystemErrno, c_int)] = &[ + $( $pair, )+ + (SystemErrno::EOF, uv::UV_EOF), + (SystemErrno::EUNKNOWN, uv::UV_UNKNOWN), + ]; + }; +} +macro_rules! __pair { + ($i:tt, $e:tt, $uv:tt) => { + (SystemErrno::$e, uv::$uv) + }; +} +crate::__uv_e_rows!(__pair => __decl_uv_pairs); + +/// A `SystemErrno` discriminant → the negative `UV_E*` number Node reports for +/// it in `err.errno` on Windows (`2` → `UV_ENOENT`, -4058). +pub fn e_to_uv(errno: u16) -> Option { + UV_PAIRS + .iter() + .find(|(e, _)| *e as u16 == errno) + .map(|&(_, uv)| uv) +} + +/// The reverse of [`e_to_uv`]. +pub fn uv_to_e(code: c_int) -> Option { + UV_PAIRS.iter().find(|(_, uv)| *uv == code).map(|&(e, _)| e) } // ────────────────────────────────────────────────────────────────────────── @@ -781,17 +486,14 @@ pub mod uv_e { // libuv-synthetic `-UV_E*` constant. macro_rules! __v { ($i:tt, $e:tt, $uv:tt) => { - -::bun_libuv_sys::$uv + -$crate::uv_codes::$uv }; } crate::__uv_e_rows!(__v); } // ────────────────────────────────────────────────────────────────────────── -// `windows` — Win32Error / NTSTATUS / kernel32 surface moved DOWN from -// `bun_sys::windows` (cycle-break per PORTING.md §Dep-cycle fixes). Only the -// subset referenced by `SystemErrno::init` / `last_error` is mirrored; the full -// 1100-variant table stays in `bun_sys::windows` and re-exports this newtype. +// `windows` — `Win32Error` / `NTSTATUS` mappings that need `SystemErrno`. // ────────────────────────────────────────────────────────────────────────── pub mod windows { use super::{E, SystemErrno}; @@ -805,23 +507,19 @@ pub mod windows { /// `NTSTATUS` — `enum(u32) { …, _ }`. Same provenance as `Win32Error`. pub use bun_windows_sys::NTSTATUS; - /// Extension trait for `Win32Error` → `SystemErrno`/`E` mapping. + /// Extension trait for the `Win32Error` → `E` mapping. /// `bun_windows_sys` is tier-0 and cannot name `SystemErrno`, so the - /// mapping surfaces here as extension methods instead. + /// mapping surfaces here as an extension method instead. pub trait Win32ErrorExt: Copy { /// The errno for a failed call whose `GetLastError()` is `self`: its row /// in the Win32→errno table (`init_win32_error`), else `EUNKNOWN` — /// including `SUCCESS`, a failure that set no code. Compare against /// `Win32Error::SUCCESS` first when the call may have succeeded. - fn to_system_errno(self) -> SystemErrno; - #[inline] - fn to_e(self) -> E { - self.to_system_errno().to_e() - } + fn to_e(self) -> E; } impl Win32ErrorExt for Win32Error { #[inline] - fn to_system_errno(self) -> SystemErrno { + fn to_e(self) -> E { SystemErrno::init_win32_error(self).unwrap_or(SystemErrno::EUNKNOWN) } } @@ -830,27 +528,27 @@ pub mod windows { pub fn translate_ntstatus_to_errno(err: NTSTATUS) -> E { match err { NTSTATUS::SUCCESS => E::SUCCESS, - NTSTATUS::ACCESS_DENIED => E::PERM, - NTSTATUS::INVALID_HANDLE => E::BADF, - NTSTATUS::INVALID_PARAMETER => E::INVAL, - NTSTATUS::OBJECT_NAME_COLLISION => E::EXIST, - NTSTATUS::FILE_IS_A_DIRECTORY => E::ISDIR, - NTSTATUS::OBJECT_PATH_NOT_FOUND | NTSTATUS::OBJECT_NAME_NOT_FOUND => E::NOENT, - NTSTATUS::NOT_A_DIRECTORY => E::NOTDIR, - NTSTATUS::RETRY => E::AGAIN, - NTSTATUS::DIRECTORY_NOT_EMPTY => E::NOTEMPTY, - NTSTATUS::FILE_TOO_LARGE => E::_2BIG, - NTSTATUS::NOT_SAME_DEVICE => E::XDEV, - NTSTATUS::DELETE_PENDING => E::BUSY, - NTSTATUS::SHARING_VIOLATION => E::BUSY, - NTSTATUS::OBJECT_NAME_INVALID => E::INVAL, - NTSTATUS::CANNOT_DELETE => E::PERM, + NTSTATUS::ACCESS_DENIED => E::EPERM, + NTSTATUS::INVALID_HANDLE => E::EBADF, + NTSTATUS::INVALID_PARAMETER => E::EINVAL, + NTSTATUS::OBJECT_NAME_COLLISION => E::EEXIST, + NTSTATUS::FILE_IS_A_DIRECTORY => E::EISDIR, + NTSTATUS::OBJECT_PATH_NOT_FOUND | NTSTATUS::OBJECT_NAME_NOT_FOUND => E::ENOENT, + NTSTATUS::NOT_A_DIRECTORY => E::ENOTDIR, + NTSTATUS::RETRY => E::EAGAIN, + NTSTATUS::DIRECTORY_NOT_EMPTY => E::ENOTEMPTY, + NTSTATUS::FILE_TOO_LARGE => E::E2BIG, + NTSTATUS::NOT_SAME_DEVICE => E::EXDEV, + NTSTATUS::DELETE_PENDING => E::EBUSY, + NTSTATUS::SHARING_VIOLATION => E::EBUSY, + NTSTATUS::OBJECT_NAME_INVALID => E::EINVAL, + NTSTATUS::CANNOT_DELETE => E::EPERM, // Any other error status: ask ntdll for the equivalent Win32 error // and run it through the same libuv-derived table Node.js uses. // Filter drivers and cloud-sync placeholders return many NTSTATUS // codes that are not enumerated above; without this fallthrough // they would all surface as `UNKNOWN`. Codes `RtlNtStatusToDosError` - // cannot map still fall back to `E::UNKNOWN` via `to_e()`. + // cannot map still fall back to `E::EUNKNOWN` via `to_e()`. // // Exception: the libuv Win32 table maps `ERROR_INVALID_FUNCTION` // to `EISDIR` (because Win32 `DeleteFileW` returns it when called @@ -863,12 +561,12 @@ pub mod windows { // is a directory. Returning `EISDIR` here would make recursive // `fs.rm` flip `treat_as_dir` forever, so override it to `ENOTSUP`. _ => match Win32Error::from_ntstatus(err).to_e() { - E::ISDIR => E::NOTSUP, + E::EISDIR => E::ENOTSUP, e => e, }, } } } -// `Win32Error::{to_system_errno, to_e}` are provided via `windows::Win32ErrorExt` +// `Win32Error::to_e` is provided via `windows::Win32ErrorExt` // (extension trait — `Win32Error` is now a foreign type from `bun_windows_sys`). diff --git a/src/event_loop/AnyEventLoop.rs b/src/event_loop/AnyEventLoop.rs index e27568119d06..737328450abc 100644 --- a/src/event_loop/AnyEventLoop.rs +++ b/src/event_loop/AnyEventLoop.rs @@ -84,13 +84,6 @@ impl AnyEventLoop { } } - /// Convert to an owned [`EventLoopHandle`]. Thin alias for - /// [`EventLoopHandle::from_any`]. - #[inline] - pub fn as_handle(this: &mut AnyEventLoop) -> EventLoopHandle { - EventLoopHandle::from_any(this) - } - pub fn init() -> AnyEventLoop { AnyEventLoop::Mini(Box::new(MiniEventLoop::init())) } @@ -189,13 +182,6 @@ impl AnyEventLoop { self.r#loop() } - /// Platform-native loop pointer (`us_loop_t*` on POSIX, `uv_loop_t*` on - /// Windows). See [`bun_io::uws_to_native`]. - #[inline] - pub fn native_loop(&mut self) -> *mut bun_io::Loop { - bun_io::uws_to_native(self.r#loop()) - } - #[inline] pub fn wakeup(&mut self) { // SAFETY: `r#loop()` returns a valid live loop pointer. @@ -462,23 +448,6 @@ impl EventLoopHandle { self.r#loop() } - /// Platform-native loop pointer (`us_loop_t*` on POSIX, `uv_loop_t*` on - /// Windows). See [`bun_io::uws_to_native`] — collapses the per-site - /// `#[cfg(windows)]` `.uv_loop` projection that previously appeared at - /// every `BufferedReaderParent::loop_` impl. - #[inline] - pub fn native_loop(self) -> *mut bun_io::Loop { - bun_io::uws_to_native(self.r#loop()) - } - - /// Windows convenience alias for [`native_loop`](Self::native_loop) - /// (kept for existing `cfg(windows)` callers that spell `uv_loop`). - #[cfg(windows)] - #[inline] - pub fn uv_loop(self) -> *mut bun_io::Loop { - self.native_loop() - } - pub fn env(self) -> *mut DotEnvLoader { match self { EventLoopHandle::Js { owner } => owner.env(), diff --git a/src/event_loop/ConcurrentTask.rs b/src/event_loop/ConcurrentTask.rs index b08674e5eaa3..00280c03d21c 100644 --- a/src/event_loop/ConcurrentTask.rs +++ b/src/event_loop/ConcurrentTask.rs @@ -72,11 +72,8 @@ pub mod task_tag { BundleV2PluginLoadDeferred, // bun.bundle_v2.Load (`.defer()` notice to the bundle's loop) BundleV2ParseTaskResult, // bun.bundle_v2.ParseTask.Result ChromePipeEvent, - CopyFileWindowsMkdirp, - WriteFileWindowsMkdirp, DnsErrorDeferred, ShellYesTask, // shell.Interpreter.Builtin.Yes.YesTask - Close, CppTask, DuplexUpgradeContext, FetchTasklet, @@ -84,7 +81,6 @@ pub mod task_tag { FetchTaskletPromiseSettle, FetchTaskletRequestDrain, FSWatchTask, - GetAddrInfoLibuvComplete, GraphContextStopAgain, GraphContextStopAndFree, DeadContextStopAgain, @@ -100,13 +96,10 @@ pub mod task_tag { NativeBrotli, NativeZlib, NativeZstd, - Open, PollPendingModulesTask, PosixSignalTask, MemoryPressureTask, ProcessWaiterThreadTask, - Read, - Readv, FlushPendingFileSinkTask, RunTestsTask, RuntimeTranspilerStore, @@ -132,7 +125,6 @@ pub mod task_tag { ShellRmDirTask, ShellRmTask, ShellTouchTask, - StatFS, StatWatcherTimerUpdate, StatWatcherHop, AsyncCpTask, @@ -142,8 +134,6 @@ pub mod task_tag { ValkeyDeferredClose, ValkeyDeferredFailure, WindowsNamedPipeContext, - Write, - Writev, } } diff --git a/src/event_loop/EventLoopTimer.rs b/src/event_loop/EventLoopTimer.rs index e69dfb58dc63..3af6236561e4 100644 --- a/src/event_loop/EventLoopTimer.rs +++ b/src/event_loop/EventLoopTimer.rs @@ -192,6 +192,7 @@ pub enum Tag { DNSResolver, DnsSdConnection, WindowsNamedPipe, + WindowsNamedPipeEndOfWrite, WTFTimer, PostgresSQLConnectionTimeout, PostgresSQLConnectionMaxLifetime, diff --git a/src/event_loop/README.md b/src/event_loop/README.md index c5c569ddd5a7..df64683d67b4 100644 --- a/src/event_loop/README.md +++ b/src/event_loop/README.md @@ -4,7 +4,7 @@ This document explains how Bun's event loop works, including task draining, micr ## Overview -Bun's event loop is built on top of **uSockets** (a cross-platform event loop based on epoll/kqueue) and integrates with **JavaScriptCore's** microtask queue and a custom **process.nextTick** queue. The event loop processes tasks in a specific order to ensure correct JavaScript semantics while maximizing performance. +Bun's event loop is built on top of **uSockets** (a cross-platform event loop based on epoll/kqueue/IOCP) and integrates with **JavaScriptCore's** microtask queue and a custom **process.nextTick** queue. The event loop processes tasks in a specific order to ensure correct JavaScript semantics while maximizing performance. ## Core Components @@ -88,15 +88,15 @@ This is called when the event loop is active and needs to wait for I/O: │ ▼ ┌─────────────────────────────────────┐ -│ 4. Poll I/O via uSockets │ ← epoll_wait/kevent with timeout -│ (epoll_kqueue.c:251-320) │ +│ 4. Poll I/O via uSockets │ ← epoll/kevent/IOCP wait with timeout +│ (epoll_kqueue.c, iocp.c) │ │ - Dispatch ready polls │ │ - Each I/O event treated as task│ └──────────────┬──────────────────────┘ │ ▼ ┌─────────────────────────────────────┐ -│ 5. Drain timers (POSIX) │ ← setTimeout/setInterval callbacks +│ 5. Drain timers │ ← setTimeout/setInterval callbacks └──────────────┬──────────────────────┘ │ ▼ @@ -230,7 +230,7 @@ The queue maintains a map of `(pointer, task_fn)` pairs and runs each task. If a ## I/O Polling Integration -### uSockets Event Loop (`epoll_kqueue.c:251-320`) +### uSockets Event Loop (`epoll_kqueue.c`, `iocp.c`) The I/O poll is integrated into the event loop via `us_loop_run_bun_tick()`: @@ -245,7 +245,8 @@ The I/O poll is integrated into the event loop via `us_loop_run_bun_tick()`: │ │ │ 3. POLL I/O │ │ ├─> epoll_pwait2() [Linux] │ -│ └─> kevent64() [macOS/BSD] │ +│ ├─> kevent64() [macOS/BSD] │ +│ └─> GetQueuedCompletionStatusEx() [Windows] │ │ └─> Block with timeout until I/O ready │ │ │ │ 4. FOR EACH READY POLL: │ @@ -279,24 +280,16 @@ When I/O becomes ready (socket readable/writable, file descriptor ready): ## setTimeout and setInterval Ordering -Timers are handled differently based on platform: - -### POSIX (`event_loop.rs:396`) - ```rust ctx.timer.drain_timers(ctx); ``` -Timers are drained after I/O polling. Each timer callback: +Timers are drained after I/O polling, on every platform. Each timer callback: 1. Is wrapped in `enter()`/`exit()` 2. Triggers microtask draining after execution 3. Can enqueue new tasks -### Windows - -Uses the uv_timer_t mechanism integrated into the uSockets loop. - ### Timer vs. setImmediate Ordering ```javascript @@ -339,7 +332,7 @@ This ensures microtasks are only drained once per top-level event loop task, eve The Bun event loop processes work in this order: 1. **Immediate tasks** (setImmediate) -2. **I/O polling** (epoll/kqueue) +2. **I/O polling** (epoll/kqueue/IOCP) 3. **Timer callbacks** (setTimeout/setInterval) 4. **Regular tasks** from the task queue - For each task: diff --git a/src/event_loop/SpawnSyncEventLoop.rs b/src/event_loop/SpawnSyncEventLoop.rs index c8280d9f415b..1a954cdf115d 100644 --- a/src/event_loop/SpawnSyncEventLoop.rs +++ b/src/event_loop/SpawnSyncEventLoop.rs @@ -7,33 +7,22 @@ //! - We don't recursively run the main event loop //! //! Implementation approach: -//! - Creates a separate uws.Loop instance with its own kqueue/epoll fd (POSIX) or libuv loop (Windows) +//! - Creates a separate uws.Loop instance with its own kqueue/epoll fd (POSIX) or completion port (Windows) //! - Wraps it in a full jsc.EventLoop instance whose `uws_loop` is the isolated loop //! - Temporarily overrides vm.event_loop_handle to point to the isolated loop //! - Minimal handler callbacks (wakeup/pre/post are no-ops) //! //! Similar to Node.js's approach in vendor/node/src/spawn_sync.cc but adapted for Bun's architecture. -use core::cell::Cell; use core::ptr::NonNull; use bun_core::{Timespec, TimespecMockMode}; -#[cfg(windows)] -use bun_sys::windows::libuv; -#[cfg(windows)] -// `ref_`/`unref`/`close` are `UvHandle` default trait methods; bring it into -// scope so method resolution finds them on `Timer`. -use bun_sys::windows::libuv::UvHandle as _; use bun_uws as uws; // MOVE-IN: EventLoopHandle relocated from bun_jsc — see AnyEventLoop.rs. use crate::EventLoopHandle; -// On POSIX this is `?*uws.Loop`, on Windows `?*libuv.Loop`. -#[cfg(unix)] pub type VmEventLoopHandle = Option>; -#[cfg(windows)] -pub type VmEventLoopHandle = Option>; // LAYERING: `bun_event_loop` sits below `bun_jsc`, so it cannot name // `jsc::EventLoop` / `jsc::VirtualMachine`. The bodies live in `bun_jsc` as @@ -100,14 +89,6 @@ pub struct SpawnSyncEventLoop { /// `prepare` overrides the VM's event_loop_handle; the original, restored /// by `cleanup`. original_event_loop_handle: VmEventLoopHandle, - - #[cfg(windows)] - uv_timer: Option>, - // ALIASING: `Cell` because on Windows the libuv timer callback (`on_uv_timer`) writes this - // field re-entrantly from inside `tick_with_timeout`'s uws tick while that frame still holds - // `&mut self` (LLVM `noalias`). The field must be - // interior-mutable so the re-entrant write is sound under Stacked Borrows. - did_timeout: Cell, } /// Minimal handler for the isolated loop @@ -158,9 +139,6 @@ impl SpawnSyncEventLoop { this.write(Self { uws_loop: loop_, original_event_loop_handle: None, // overwritten in `prepare` - #[cfg(windows)] - uv_timer: None, - did_timeout: Cell::new(false), event_loop, vm, }); @@ -193,30 +171,7 @@ impl SpawnSyncEventLoop { self.event_loop } - /// Shared borrow of the isolated `uws::Loop`. - /// - /// # Safety (invariant) - /// `uws_loop` is set in `init` and freed only in `Drop`, so it is valid for - /// all of `self`'s lifetime. The loop is only mutated through `&mut self` - /// paths (`uws_loop_mut`), so a shared borrow tied to `&self` cannot - /// overlap a unique borrow. - #[inline] - pub fn uws_loop(&self) -> &uws::Loop { - // SAFETY: see doc invariant above — non-null, owned for `self`'s lifetime, - // no `&mut` alias while `&self` is held. - unsafe { self.uws_loop.as_ref() } - } - /// Unique borrow of the isolated `uws::Loop`. - /// - /// Re-entrancy hazard: do **NOT** call this between the Windows - /// `timer.data = self as *mut Self` store and the uws tick in - /// `tick_with_timeout`. The `&mut self` receiver reborrow here is a Unique - /// retag over all of `*self` under Stacked Borrows, which pops the raw - /// pointer's tag at `did_timeout`'s bytes and makes the re-entrant - /// `on_uv_timer` write UB. `tick_with_timeout` therefore copies - /// `self.uws_loop` out *before* that store and ticks via the raw pointer - /// directly. This accessor is for non-re-entrant call sites (e.g. `init`). #[inline] pub(crate) fn uws_loop_mut(&mut self) -> &mut uws::Loop { // SAFETY: `uws_loop` is non-null and exclusively owned by `self` for its @@ -224,55 +179,10 @@ impl SpawnSyncEventLoop { // guarantees no other safe borrow of the loop is live. unsafe { self.uws_loop.as_mut() } } - - /// Unique borrow of the heap-owned libuv timeout timer (Windows only). - /// - /// Single deref site for the `uv_timer: Option>` field — - /// collapses the per-site `(*t.as_ptr()).method()` raw derefs in - /// `prepare_timer_on_windows` / `cleanup` / `tick_with_timeout`. The - /// pointee is `Box`-allocated in [`prepare_timer_on_windows`] (via - /// `heap::into_raw_nn`) and freed only by the `on_close_uv_timer` callback - /// scheduled in `Drop`, so it is valid for all of `self`'s lifetime - /// whenever the field is `Some`. `&mut self` ensures the returned - /// `&mut Timer` is the sole live Rust reference. - /// - /// Same re-entrancy hazard as [`uws_loop_mut`](Self::uws_loop_mut): do - /// **NOT** call between the `timer.data = self as *mut Self` store and the - /// uws tick in `tick_with_timeout` — the `&mut self` receiver reborrow is - /// a Unique retag over `*self` and would pop the raw `*mut Self`'s - /// Stacked-Borrows tag at `did_timeout`'s bytes. - #[cfg(windows)] - #[inline] - fn uv_timer_mut(&mut self) -> Option<&mut libuv::Timer> { - // SAFETY: see doc — heap-owned, valid while `Some`, `&mut self` ⇒ - // exclusive Rust access (libuv only touches the handle from inside - // `uv_run`, never concurrently with a caller of this accessor). - self.uv_timer.as_mut().map(|p| unsafe { p.as_mut() }) - } -} - -#[cfg(windows)] -extern "C" fn on_close_uv_timer(timer: *mut libuv::Timer) { - // SAFETY: `timer` was allocated via `heap::alloc` in `prepare_timer_on_windows`. - drop(unsafe { bun_core::heap::take(timer) }); } impl Drop for SpawnSyncEventLoop { fn drop(&mut self) { - #[cfg(windows)] - { - if let Some(timer) = self.uv_timer.take() { - // SAFETY: timer is a live libuv handle owned by this struct. - unsafe { - (*timer.as_ptr()).stop(); - (*timer.as_ptr()).unref(); - // `UvHandle::close` already does the `*mut Timer` → - // `*mut uv_handle_t` cb cast internally. - (*timer.as_ptr()).close(on_close_uv_timer); - } - } - } - // Destroy the event loop before the uws loop. __bun_spawn_sync_destroy_event_loop(self.event_loop); // SAFETY: uws_loop was returned by `us_create_loop` in `init` and not yet freed. @@ -284,52 +194,15 @@ impl SpawnSyncEventLoop { /// Configure the event loop for a specific VM context pub fn prepare(&mut self, vm: *mut () /* SAFETY: erased *mut VirtualMachine */) { __bun_spawn_sync_event_loop_set_vm(self.event_loop, vm); - self.did_timeout.set(false); self.vm = vm; self.original_event_loop_handle = __bun_spawn_sync_vm_get_event_loop_handle(vm); - #[cfg(unix)] - let new_handle: VmEventLoopHandle = Some(self.uws_loop); - #[cfg(windows)] - let new_handle: VmEventLoopHandle = Some( - NonNull::new(self.uws_loop().uv_loop) - .expect("uv_loop is set by us_create_loop for the loop's lifetime"), - ); - __bun_spawn_sync_vm_set_event_loop_handle(vm, new_handle); + __bun_spawn_sync_vm_set_event_loop_handle(vm, Some(self.uws_loop)); } /// Restore the original event loop handle after spawnSync completes pub fn cleanup(&mut self, vm: *mut () /* SAFETY: erased *mut VirtualMachine */) { __bun_spawn_sync_vm_set_event_loop_handle(vm, self.original_event_loop_handle); - - #[cfg(windows)] - { - if let Some(timer) = self.uv_timer_mut() { - timer.stop(); - timer.unref(); - } - } - } -} - -#[cfg(windows)] -extern "C" fn on_uv_timer(timer_: *mut libuv::Timer) { - // SAFETY: `data` was set to `self` in `tick_with_timeout` immediately before the uws tick; - // the SpawnSyncEventLoop outlives the timer (timer is stopped/closed in `cleanup`/`Drop`). - // - // ALIASING: this callback fires re-entrantly from inside `tick_with_timeout`'s uws tick - // (uv_run) while that frame still holds `&mut self` (LLVM `noalias`) AND a live - // `&mut uws::Loop` (Loop::tick_with_timeout takes `&mut self`). We must not: - // (a) materialize a second `&mut SpawnSyncEventLoop` here, nor - // (b) read `(*this).uws_loop` — the outer frame's `&mut self` access to `uws_loop` at the - // tick call popped the raw `*mut Self`'s Stacked-Borrows tag at those bytes, and the - // `&mut uws::Loop` it produced is still live around us. - // So: touch only `(*this).did_timeout` (a `Cell`, interior-mutable), and obtain the uv loop - // from the timer handle itself rather than routing through `*this`. - unsafe { - let this: *mut SpawnSyncEventLoop = (*timer_).data.cast::(); - (*this).did_timeout.set(true); - (*libuv::uv_handle_get_loop(timer_.cast())).stop(); } } @@ -340,34 +213,6 @@ pub enum TickState { } impl SpawnSyncEventLoop { - #[cfg(windows)] - fn prepare_timer_on_windows(&mut self, ts: &Timespec) { - if self.uv_timer.is_none() { - let uv_timer: Box = Box::new(bun_core::ffi::zeroed()); - // Leak to raw *before* `uv_timer_init` so libuv's stored handle - // pointer derives from the post-`into_raw` provenance (not a - // `Box`-`noalias` reborrow that `into_raw` would later pop). - self.uv_timer = Some(bun_core::heap::into_raw_nn(uv_timer)); - // `uv_loop` is set by C `us_create_loop`. Read it (a `*mut`, Copy) - // before borrowing `self` mutably via the timer accessor. - let uv_loop = self.uws_loop().uv_loop; - self.uv_timer_mut().expect("just set").init(uv_loop); - } - - // Refresh the loop's cached clock: this loop only runs while a spawnSync call is in - // flight, so `loop->time` (which `uv_timer_start` computes the due time from) can be - // staler than the timeout, which would make the timer fire immediately. - // SAFETY: `uv_loop` is the live initialized loop owned by `self.uws_loop`. - unsafe { libuv::uv_update_time(self.uws_loop().uv_loop) }; - - // NOTE: `timer.data` is assigned later in `tick_with_timeout`, immediately before the - // uws tick, so the stored `*mut Self` derives directly from that frame's live `&mut self` - // (not from this function's reborrow, which would be invalidated on return). - let timer = self.uv_timer_mut().expect("set above"); - timer.start(ts.ms_unsigned(), 0, Some(on_uv_timer)); - timer.ref_(); - } - /// Tick the isolated event loop with an optional timeout /// This is similar to the main event loop's tick but completely isolated /// @@ -376,85 +221,35 @@ impl SpawnSyncEventLoop { let duration_storage: Option; let duration: Option<&Timespec> = match timeout { Some(ts) => { - duration_storage = - Some(ts.duration(&Timespec::now(TimespecMockMode::ForceRealTime))); + let now = Timespec::now(TimespecMockMode::ForceRealTime); + // A deadline that has passed is a non-blocking tick. + duration_storage = Some(if now.order(ts) == core::cmp::Ordering::Less { + ts.duration(&now) + } else { + Timespec::EPOCH + }); duration_storage.as_ref() } None => None, }; - #[cfg(windows)] - { - if let Some(ts) = duration { - self.prepare_timer_on_windows(ts); - } - } - // Suppress microtask drain for the entire tick, including the uws loop tick. - // On Windows, uv_run() fires callbacks inline (e.g. uv_process exit, pipe I/O) - // which call onProcessExit → onExit. If any code path in those callbacks - // reaches drainMicrotasksWithGlobal, we must already have the flag set. - // On POSIX, the uws tick only polls I/O; callbacks are dispatched later - // via the task queue, but we set the flag here uniformly for safety. + // Callbacks dispatched from it (process exit, pipe I/O) call onProcessExit → + // onExit; if any code path in those callbacks reaches + // drainMicrotasksWithGlobal, we must already have the flag set. let _suppress = SuppressMicrotaskDrain::new(self.vm); // Tick the isolated uws loop with the specified timeout // This will only process I/O related to this subprocess // and will NOT interfere with the main event loop - // - // ALIASING: hoist the `uws_loop` pointer *before* storing `*mut Self` into `timer.data` - // below, so that between that store and the re-entrant `on_uv_timer` callback we touch - // *no* bytes of `*self` at all. Do NOT route the tick through `self.uws_loop_mut()` here: - // its `&mut self` receiver reborrow is a Unique retag over the full extent of `*self` - // under Stacked Borrows, which would pop the SharedReadWrite tag of the raw pointer just - // stored into `timer.data` at `did_timeout`'s bytes — making the callback's - // `(*this).did_timeout.set(true)` UB. The `uws::Loop` lives in a separate allocation, so - // forming `&mut uws::Loop` from the copied `NonNull` does not touch `*self`'s borrow - // stacks. - let loop_ = self.uws_loop; - #[cfg(windows)] - if let Some(t) = self.uv_timer { - // ALIASING: store `*mut Self` here (not in `prepare_timer_on_windows`) so its - // provenance is a direct child of *this* frame's `&mut self`. Between this store and - // the re-entrant `on_uv_timer` callback, no field of `*self` is accessed (`loop_` was - // copied out above), so the raw tag at `did_timeout`'s bytes survives under Stacked - // Borrows. - // SAFETY: `t` is a valid initialized libuv timer handle owned by `self`. - unsafe { (*t.as_ptr()).data = (core::ptr::from_mut(self)).cast() }; - } - // SAFETY: `uws_loop` is non-null and exclusively owned by `self` (created in `init`, - // freed in `Drop`); `&mut self` guarantees no other safe borrow of the loop is live. - unsafe { (*loop_.as_ptr()).tick_with_timeout(duration, uws::NOW_NS_UNKNOWN) }; - - if let Some(ts) = timeout { - #[cfg(windows)] - let _ = ts; - #[cfg(windows)] - { - // `uv_timer` is `Some` when `timeout` is `Some` (set in - // `prepare_timer_on_windows`). The re-entrant `on_uv_timer` - // callback can only fire from inside the uws tick above; once - // that returns no callback runs until the next `uv_run`, so the - // `&mut self` receiver reborrow in `uv_timer_mut` is sound here - // (the raw `*mut Self` in `timer.data` is dead until restarted). - let t = self - .uv_timer_mut() - .expect("set in prepare_timer_on_windows"); - t.unref(); - t.stop(); - } - #[cfg(not(windows))] - { - self.did_timeout.set( - Timespec::now(TimespecMockMode::ForceRealTime).order(ts) - != core::cmp::Ordering::Less, - ); - } - } + self.uws_loop_mut() + .tick_with_timeout(duration, uws::NOW_NS_UNKNOWN); - __bun_spawn_sync_event_loop_tick_tasks_only(self.event_loop); + let did_timeout = timeout.is_some_and(|ts| { + Timespec::now(TimespecMockMode::ForceRealTime).order(ts) != core::cmp::Ordering::Less + }); - let did_timeout = self.did_timeout.replace(false); + __bun_spawn_sync_event_loop_tick_tasks_only(self.event_loop); if did_timeout { return TickState::Timeout; diff --git a/src/event_loop/lib.rs b/src/event_loop/lib.rs index bba6badac531..6621cb1d5ef4 100644 --- a/src/event_loop/lib.rs +++ b/src/event_loop/lib.rs @@ -8,9 +8,7 @@ pub mod EventLoopTimer; // ──────────────────────────────────────────────────────────────────────────── // AnyEventLoop / SpawnSyncEventLoop / MiniEventLoop. // The parent event loop is wired via the lower-tier `set_parent_raw(tag, ptr)` -// + `EventLoopHandle::into_tag_ptr()`. The Windows-only `uv_loop` projection -// lives on `EventLoopHandle::uv_loop` (`#[cfg(windows)]`); the POSIX build is -// gate-free. +// + `EventLoopHandle::into_tag_ptr()`. // ──────────────────────────────────────────────────────────────────────────── #[path = "MiniEventLoop.rs"] diff --git a/src/http/HTTPThread.rs b/src/http/HTTPThread.rs index b4f2c2f208df..b5bf4a6aecd2 100644 --- a/src/http/HTTPThread.rs +++ b/src/http/HTTPThread.rs @@ -1306,14 +1306,7 @@ mod _event_loop_draft { impl HttpThread { fn process_events(&mut self) -> ! { let uws_loop = self.uws_loop_mut(); - #[cfg(unix)] - { - uws_loop.num_polls = uws_loop.num_polls.max(2); - } - #[cfg(windows)] - { - uws_loop.inc(); - } + uws_loop.num_polls = uws_loop.num_polls.max(2); loop { if SHUTDOWN_REQUESTED.load(Ordering::Acquire) { diff --git a/src/install/Cargo.toml b/src/install/Cargo.toml index a8a45c65c7e7..d7bded0a692e 100644 --- a/src/install/Cargo.toml +++ b/src/install/Cargo.toml @@ -67,3 +67,6 @@ bun_url.workspace = true bun_uws.workspace = true bun_which.workspace = true bun_wyhash.workspace = true + +[target.'cfg(windows)'.dependencies] +bun_spawn_sys.workspace = true diff --git a/src/install/PackageInstall.rs b/src/install/PackageInstall.rs index 8219093b1540..67087e717f55 100644 --- a/src/install/PackageInstall.rs +++ b/src/install/PackageInstall.rs @@ -2008,34 +2008,11 @@ impl<'a> PackageInstall<'a> { } pub(crate) fn is_dangling_symlink(path: &ZStr) -> bool { - #[cfg(any(target_os = "linux", target_os = "android"))] - { - match sys::open(path, sys::O::PATH, 0) { - Err(_) => return true, - Ok(fd) => { - fd.close(); - return false; - } - } - } - #[cfg(windows)] - { - match sys::sys_uv::open(path, 0, 0) { - Err(_) => return true, - Ok(fd) => { - fd.close(); - return false; - } - } - } - #[cfg(not(any(target_os = "linux", target_os = "android", windows)))] - { - match sys::open(path, sys::O::PATH, 0) { - Err(_) => return true, - Ok(fd) => { - fd.close(); - return false; - } + match sys::open(path, sys::O::PATH, 0) { + Err(_) => true, + Ok(fd) => { + fd.close(); + false } } } diff --git a/src/install/PackageManager.rs b/src/install/PackageManager.rs index 49e49cb8a7ac..5e8b8e1797d5 100644 --- a/src/install/PackageManager.rs +++ b/src/install/PackageManager.rs @@ -30,13 +30,6 @@ use bun_threading::{ThreadPool, UnboundedQueue, thread_pool}; use bun_transpiler as transpiler; use bun_url::URL; -// `bun.spawn.process.WaiterThread` — the force-waiter-thread flag was moved -// down into `bun_spawn::process` (MOVE_DOWN b0); install just flips it during -// init. The full waiter-thread machinery (queue, signalfd, loop) lives in -// `bun_runtime::api::bun::process` and *reads* the same flag. -#[cfg(unix)] -use bun_spawn::process::WaiterThread; - use crate::RunCommand; /// `Command::Context` shim — the option-carrying `ContextData` shape was lifted @@ -1978,7 +1971,7 @@ pub fn init( #[cfg(unix)] if env.get(b"BUN_FEATURE_FLAG_FORCE_WAITER_THREAD").is_some() { - WaiterThread::set_should_use_waiter_thread(); + bun_spawn::waiter_thread_flag::set(); } if bun_core::env_var::feature_flag::BUN_FEATURE_FLAG_FORCE_WINDOWS_JUNCTIONS @@ -2199,17 +2192,12 @@ pub fn init( let evl = unsafe { &mut (*manager_ptr).event_loop }; if let AnyEventLoop::Mini(mini) = evl { let mini_ptr: *mut MiniEventLoop = &raw mut **mini; - // Set ONLY `MiniEventLoop.global`, - // NOT `globalInitialized`. The distinction is load-bearing: a later - // `initGlobal(env, top_level_dir)` (e.g. from `bun pm pack` / - // `pm version` lifecycle scripts → RunCommand::run_package_script_*) - // checks `globalInitialized` and, when false, allocates a FRESH mini - // with env/top_level_dir/uv-loop fully wired, then that becomes the - // global. If we flip `GLOBAL_INITIALIZED` here, that call returns - // *this* embedded mini instead — which was constructed without env, - // without top_level_dir, and (on Windows) without going through - // `init_global`'s uv-loop setup. The shell's IOWriter then opens - // stdout/stderr against an under-initialised loop → EBADF (exit 9). + // Set only `GLOBAL`, not `GLOBAL_INITIALIZED`: a later + // `init_global(env, top_level_dir)` (the lifecycle scripts of + // `bun pm pack` / `pm version`) must still allocate a fresh mini + // with env, cwd and loop wired up. This embedded one has none of + // them, and the shell's IOWriter would open stdout/stderr against + // it → EBADF (exit 9). mini_event_loop::GLOBAL.with(|g| g.set(mini_ptr)); } } diff --git a/src/install/PackageManager/patchPackage.rs b/src/install/PackageManager/patchPackage.rs index 22f89dfd3146..fedfafde777d 100644 --- a/src/install/PackageManager/patchPackage.rs +++ b/src/install/PackageManager/patchPackage.rs @@ -475,8 +475,7 @@ pub fn do_patch_commit( } }; let paths = bun_patch::git_diff_preprocess_paths(old_folder, new_folder); - let (opts, _envp_guard) = - bun_patch::spawn_opts(&paths[0], &paths[1], cwd, git, &mut manager.event_loop); + let (opts, _envp_guard) = bun_patch::spawn_opts(&paths[0], &paths[1], cwd, git); let mut spawn_result = match bun_spawn::sync::spawn(&opts) { Err(e) => { diff --git a/src/install/PackageManager/security_scanner.rs b/src/install/PackageManager/security_scanner.rs index fbf68d7c5c13..53d048b164b8 100644 --- a/src/install/PackageManager/security_scanner.rs +++ b/src/install/PackageManager/security_scanner.rs @@ -4,11 +4,6 @@ use std::io::Write as _; use bstr::BStr; -// `BufferedReaderParent::loop_` is typed `*mut bun_uws::Loop` (the -// uws wrapper — `WindowsLoop` on Windows, `PosixLoop` on POSIX), not -// `bun_io::Loop` is the trait's nominal: `us_loop_t` on POSIX, `uv_loop_t` -// on Windows. The inherent `loop_()` projects `.uv_loop` from the uws wrapper -// on Windows so `BufferedReaderParent::loop_` returns the libuv loop directly. use crate::Error; use crate::bun_fs::FileSystem; use crate::bun_json::{Expr, ExprData}; @@ -20,14 +15,12 @@ use bun_event_loop::EventLoopHandle; use bun_install::{ DependencyID, PackageID, PackageManager, invalid_dependency_id, invalid_package_id, }; -use bun_io::Loop as AsyncLoop; #[cfg(unix)] -use bun_io::pipe_reader::PosixFlags; +use bun_io::pipe_reader::ReaderFlags; use bun_io::{BufferedReader, ReadState}; use bun_ptr::{RefCount, RefPtr}; -#[cfg(not(windows))] use bun_spawn::SpawnResultExt as _; -use bun_spawn::subprocess::{self, StdioResult}; +use bun_spawn::subprocess; use bun_spawn::{ self as spawn, Exited, Process, ProcessExit, ProcessExitKind, ProcessHandle, Rusage, SpawnOptions, Status, Stdio, @@ -999,7 +992,7 @@ bun_io::impl_buffered_reader_parent! { on_read_chunk = |this, chunk, has_more| (*this).on_read_chunk(&chunk, has_more); on_reader_done = |this| (*this).on_reader_done(); on_reader_error = |this, err| (*this).on_reader_error(err); - loop_ = |this| (*this).loop_(); + loop_ = |this| (*this).manager.event_loop.loop_(); event_loop = |this| (*this).event_loop_handle.as_event_loop_ctx(); } @@ -1017,12 +1010,6 @@ impl<'a> SecurityScanSubprocess<'a> { // command-line length limits (>1MB), and we can't use stdin because scanners // may need stdin for their own setup (e.g. interactive prompts). - // fd 3 output pipe: bun.sys.pipe() + .pipe (inherit_fd) on both platforms. - let ipc_output_fds = match bun_sys::pipe() { - Err(_) => return Err(crate::Error::IPCPipeFailed), - Ok(fds) => fds, - }; - let exec_path = bun_core::self_exe_path()?; // Build @@ -1050,31 +1037,21 @@ impl<'a> SecurityScanSubprocess<'a> { #[cfg(windows)] { - self.spawn_windows(&mut argv, ipc_output_fds)?; + self.spawn_windows(&mut argv)?; } #[cfg(not(windows))] { - self.spawn_posix(&mut argv, ipc_output_fds)?; + self.spawn_posix(&mut argv)?; } Ok(()) } - /// Posix fd 4: .buffer stdio creates a nonblocking socketpair inside the - /// spawn machinery. The child's end is dup'd to fd 4 and closed in the - /// parent by spawn's to_close_at_end list. The parent's - /// end comes back via spawned.extra_pipes. - #[cfg(unix)] - fn spawn_posix( - &mut self, + /// Start the scanner with `extra_fds` at fd 3 and 4. + fn spawn_scanner( argv: &mut [*const core::ffi::c_char; 5], - ipc_output_fds: [Fd; 2], - ) -> Result<(), Error> { - let extra_fds: Box<[Stdio]> = Box::new([ - Stdio::Pipe(ipc_output_fds[1]), // fd 3: child inherits write end - Stdio::Buffer, // fd 4: socketpair, parent's end in extra_pipes - ]); - + extra_fds: Box<[Stdio]>, + ) -> Result { let spawn_options = SpawnOptions { stdout: Stdio::Inherit, stderr: Stdio::Inherit, @@ -1086,167 +1063,99 @@ impl<'a> SecurityScanSubprocess<'a> { // SAFETY: `argv` is a local null-terminated C-string array with a // non-null argv[0]; `environ_ptr()` is the process environ block. - let mut spawned = unsafe { + Ok(unsafe { spawn::spawn_process( &spawn_options, argv.as_mut_ptr().cast(), bun_sys::environ_ptr(), ) }? - .map_err(|e| e.to_zig_err())?; - // `defer spawned.extra_pipes.deinit()` — drops at scope exit. + .map_err(|e| e.to_zig_err())?) + } + + /// fd 3 is a plain pipe whose write end the child inherits. fd 4 is a + /// `Stdio::Buffer` socketpair made by spawn; the parent's end comes back in + /// `extra_pipes`. + #[cfg(unix)] + fn spawn_posix(&mut self, argv: &mut [*const core::ffi::c_char; 5]) -> Result<(), Error> { + let ipc_output_fds = match bun_sys::pipe() { + Err(_) => return Err(crate::Error::IPCPipeFailed), + Ok(fds) => fds, + }; + + let extra_fds: Box<[Stdio]> = Box::new([ + Stdio::Pipe(ipc_output_fds[1]), // fd 3: child inherits write end + Stdio::Buffer, // fd 4: socketpair, parent's end in extra_pipes + ]); + let spawned = Self::spawn_scanner(argv, extra_fds)?; ipc_output_fds[1].close(); let _ = bun_sys::set_nonblocking(ipc_output_fds[0]); - self.ipc_reader.flags.insert(PosixFlags::NONBLOCKING); - self.ipc_reader.flags.remove(PosixFlags::SOCKET); + self.ipc_reader.flags.insert(ReaderFlags::NONBLOCKING); + self.ipc_reader.flags.remove(ReaderFlags::SOCKET); let json_fd = spawned.extra_pipes[1].fd(); - self.finish_spawn(&mut spawned, ipc_output_fds[0], move || { - subprocess::stdio_result_from_fd(json_fd) - }) + self.finish_spawn(spawned, ipc_output_fds[0], json_fd) } - /// Windows fd 4: .buffer stdio for extra_fds sets UV_OVERLAPPED_PIPE on the - /// child's handle, which breaks sync reads in the child. - /// Instead, create the pipe ourselves with asymmetric flags so only the - /// parent's write end is overlapped. Child inherits the non-overlapped read - /// end via .pipe (inherit_fd); parent wraps the overlapped write end in a - /// uv.Pipe for IOCP-based async writes. + /// The child reads fd 4 and writes fd 3 synchronously, so its ends must not + /// be overlapped, and spawn gives the child an overlapped end for a + /// `Stdio::Buffer` above fd 2. Both pipes are made here instead: the + /// parent ends overlapped for the loop, the child ends synchronous. #[cfg(windows)] - fn spawn_windows( - &mut self, - argv: &mut [*const core::ffi::c_char; 5], - ipc_output_fds: [Fd; 2], - ) -> Result<(), Error> { - use bun_sys::ReturnCodeExt as _; - use bun_sys::windows::libuv as uv; - - let mut json_fds: [uv::uv_file; 2] = [0; 2]; - // SAFETY: FFI — `json_fds` is a 2-element out-array; flags are valid. - let pipe_rc = unsafe { uv::uv_pipe(&mut json_fds, 0, uv::UV_NONBLOCK_PIPE as i32) }; - if let Some(e) = pipe_rc.errno() { - ipc_output_fds[0].close(); - ipc_output_fds[1].close(); - return Err(bun_errno::from_errno(e as i32).into()); - } - // Track ownership with optionals: None means the fd has been transferred - // or closed, so the errdefer skips it. Prevents double-close on error paths - // after pipe.open() takes ownership or after the explicit closes below. - // State is moved INTO the guard so later `= None` mutations are observed - // by the cleanup closure (PORTING.md: errdefer side-effects → scopeguard state). - let mut fds = scopeguard::guard( - ( - Some(Fd::from_uv(json_fds[0])), // .0 = child_read_fd - Some(Fd::from_uv(json_fds[1])), // .1 = parent_write_fd - ), - |(child_read, parent_write)| { - if let Some(fd) = child_read { - fd.close(); - } - if let Some(fd) = parent_write { + fn spawn_windows(&mut self, argv: &mut [*const core::ffi::c_char; 5]) -> Result<(), Error> { + use bun_spawn_sys::windows::stdio::{ChildPipe, create_pipe_pair}; + use bun_spawn_sys::windows::win32; + + // [fd 3 parent end, fd 3 child end, fd 4 parent end, fd 4 child end] + let mut pipes = scopeguard::guard([Fd::INVALID; 4], |fds| { + for fd in fds { + if fd != Fd::INVALID { fd.close(); } - }, - ); - - let pipe_ptr: *mut uv::Pipe = - bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed::())); - // errdefer pipe.closeAndDestroy() until `StaticPipeWriter` takes the - // pipe (inside `finish_spawn`); from then on the writer's `Drop` closes - // it. libuv's close callback frees the allocation, so do NOT re-box on - // the cleanup path. - let pipe_taken = core::cell::Cell::new(false); - let mut pipe = scopeguard::guard(pipe_ptr, |p| { - if !pipe_taken.get() { - // SAFETY: p is the live Box-allocated uv_pipe_t; close_and_destroy - // schedules uv_close + frees the allocation. - unsafe { uv::Pipe::close_and_destroy(p) }; } }); - // `self.loop_()` already projects to the libuv `uv_loop_t*` on - // Windows (see the `.uv_loop` projection in `loop_()`); pass through. - let uv_loop = self.loop_(); - // SAFETY: *pipe was just heap-allocated above and is non-null. - if let Some(e) = unsafe { (**pipe).init(uv_loop, false) }.to_error(bun_sys::Tag::pipe) { - return Err(e.into()); - } - if let Some(e) = unsafe { (**pipe).open(fds.1.unwrap().uv()) }.to_error(bun_sys::Tag::open) - { - return Err(e.into()); - } - fds.1 = None; // pipe owns it now + + let Ok(ipc) = create_pipe_pair(ChildPipe { + readable: false, + writable: true, + overlapped: false, + }) else { + return Err(crate::Error::IPCPipeFailed); + }; + (pipes[0], pipes[1]) = (Fd::from_system(ipc.parent), Fd::from_system(ipc.child)); + + let json = create_pipe_pair(ChildPipe { + readable: true, + writable: false, + overlapped: false, + }) + .map_err(|code| win32::sys_error(code, bun_sys::Tag::pipe))?; + (pipes[2], pipes[3]) = (Fd::from_system(json.parent), Fd::from_system(json.child)); let extra_fds: Box<[Stdio]> = Box::new([ - Stdio::Pipe(ipc_output_fds[1]), // fd 3: child inherits write end - Stdio::Pipe(fds.0.unwrap()), // fd 4: child inherits non-overlapped read end + Stdio::Pipe(pipes[1]), // fd 3: child inherits write end + Stdio::Pipe(pipes[3]), // fd 4: child inherits read end ]); + let spawned = Self::spawn_scanner(argv, extra_fds)?; - let spawn_options = SpawnOptions { - stdout: Stdio::Inherit, - stderr: Stdio::Inherit, - stdin: Stdio::Inherit, - cwd: Box::from(FileSystem::instance().top_level_dir()), - extra_fds, - windows: spawn::WindowsOptions { - loop_: EventLoopHandle::from_any(&mut self.manager.event_loop), - ..Default::default() - }, - ..Default::default() - }; - - // SAFETY: `argv` is a local null-terminated C-string array with a - // non-null argv[0]; `environ_ptr()` is the process environ block. - let mut spawned = unsafe { - spawn::spawn_process( - &spawn_options, - argv.as_mut_ptr().cast(), - bun_sys::environ_ptr(), - ) - }? - .map_err(|e| e.to_zig_err())?; - // `defer spawned.extra_pipes.deinit()` — drops at scope exit. + // `finish_spawn` takes the parent ends whether or not it succeeds. + let [ipc_read_fd, ipc_child_fd, json_write_fd, json_child_fd] = + scopeguard::ScopeGuard::into_inner(pipes); + ipc_child_fd.close(); + json_child_fd.close(); - ipc_output_fds[1].close(); - fds.0.unwrap().close(); - fds.0 = None; - - self.ipc_reader - .flags - .insert(bun_io::pipe_reader::WindowsFlags::NONBLOCKING); - - // Hand the pipe to StaticPipeWriter lazily: the closure reconstitutes - // the Box at the exact `StaticPipeWriter::create` call site inside - // `finish_spawn`. If `finish_spawn` errors before that point - // (`ipc_reader.start()`), the closure drops as a no-op and the - // still-armed cleanup guard performs `close_and_destroy`. - self.finish_spawn(&mut spawned, ipc_output_fds[0], || { - pipe_taken.set(true); - // SAFETY: `pipe_ptr` is the same allocation produced by - // heap::alloc above and has not been freed; ownership transfers - // here exactly once. - StdioResult::Buffer(unsafe { bun_core::heap::take(pipe_ptr) }) - })?; - - // fd slots are already None. - scopeguard::ScopeGuard::into_inner(fds); - Ok(()) + self.finish_spawn(spawned, ipc_read_fd, json_write_fd) } /// Common post-spawn setup: start the fd 3 reader, attach the process, /// start the fd 4 JSON writer, and begin watching for exit. fn finish_spawn( &mut self, - spawned: &mut spawn::SpawnResult, + spawned: spawn::SpawnResult, ipc_read_fd: Fd, - // Deferred constructor: a by-value - // `WindowsStdioResult::Buffer(Box)` would auto-free the - // allocation without `uv_close()` if `ipc_reader.start()` below failed, - // leaking a registered libuv handle. Taking a thunk and calling it only - // at the `StaticPipeWriter::create` site keeps the caller's - // `close_and_destroy` errdefer authoritative for the pre-writer window. - make_json_stdio: impl FnOnce() -> StdioResult, + json_write_fd: Fd, ) -> Result<(), Error> { // Allocate the blob copy before registering any event loop callbacks. If // this fails, nothing is registered yet and the caller's defer can safely @@ -1260,15 +1169,9 @@ impl<'a> SecurityScanSubprocess<'a> { // isDone() returns true, otherwise we risk freeing this struct while // StaticPipeWriter still holds a pointer to it (child crash case). self.remaining_fds = 2; - self.ipc_reader - .start(ipc_read_fd, true) - .map_err(|e| e.to_zig_err())?; - // `to_process` consumes `SpawnResult` by value on POSIX (and - // `&mut self` on Windows); take ownership of the result and let the - // moved-from `*spawned` drop empty (`extra_pipes` already read). let event_loop = EventLoopHandle::from_any(&mut self.manager.event_loop); - let process_handle = std::mem::take(spawned).to_process_handle(event_loop); + let process_handle = spawned.to_process_handle(event_loop); let process: *mut Process = process_handle.as_ptr(); // Derive the raw backref once and use it for all subsequent field @@ -1285,13 +1188,22 @@ impl<'a> SecurityScanSubprocess<'a> { (*parent).process = Some(process_handle); } + // SAFETY: see `parent` note above. + if let Err(e) = unsafe { (*parent).ipc_reader.start(ipc_read_fd, true) } { + // Windows only: POSIX reports a failed start through + // `on_reader_error`. Neither fd has an owner yet. + ipc_read_fd.close(); + json_write_fd.close(); + return Err(e.to_zig_err().into()); + } + // Assign the field BEFORE `start()`. `start()` may complete the write synchronously // (small JSON fits the 64KB pipe buffer on POSIX) and re-enter // `on_close_io` via the `parent` backref; that callback must observe // `json_writer.is_some()` to decrement `remaining_fds`, otherwise // `is_done()` never returns true and `sleep_until` hangs. let writer = - StaticPipeWriter::create(event_loop, parent.cast(), make_json_stdio(), json_source); + StaticPipeWriter::create(event_loop, parent.cast(), Some(json_write_fd), json_source); // Keep a duped ref locally so no borrow on `(*parent).json_writer` is // held across `start()` — `on_close_io` may `.take()` the field. let writer_local = writer.clone(); @@ -1357,10 +1269,6 @@ impl<'a> SecurityScanSubprocess<'a> { self.exit_status.is_some() && self.remaining_fds == 0 } - pub(crate) fn loop_(&mut self) -> *mut AsyncLoop { - self.manager.event_loop.native_loop() - } - pub(crate) fn on_reader_done(&mut self) { self.has_received_ipc = true; self.remaining_fds -= 1; @@ -1405,10 +1313,15 @@ impl<'a> SecurityScanSubprocess<'a> { // has no pending task, so it ticks uws, the poll delivers // readable+HUP, `read_with_fn` drains to `Ok(0)`, and // `on_reader_done` decrements `remaining_fds` exactly once. - // - // Windows reads via libuv (async) and the fd here is a uv-owned - // pipe handle — skip the sync drain there and keep the spec's - // teardown (the libuv exit/read ordering is not the failing path). + #[cfg(windows)] + { + // SAFETY: the live reader, outside its dispatches. + unsafe { BufferedReader::drain(&raw mut self.ipc_reader) }; + if self.has_received_ipc { + // The end of the pipe follows from the loop. + return; + } + } #[cfg(not(windows))] { let fd = self.ipc_reader.get_fd(); diff --git a/src/install/bin.rs b/src/install/bin.rs index d28f5941115b..9fc30fe5823b 100644 --- a/src/install/bin.rs +++ b/src/install/bin.rs @@ -1,5 +1,4 @@ use core::fmt; -use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use crate::Error; use bun_alloc::AllocError; @@ -831,24 +830,7 @@ pub struct Linker<'a> { pub skipped_due_to_missing_bin: bool, } -static UMASK: AtomicU32 = AtomicU32::new(0); -static HAS_SET_UMASK: AtomicBool = AtomicBool::new(false); - impl<'a> Linker<'a> { - pub fn ensure_umask() { - // Single-winner gate: only the thread that flips false->true performs - // the temporary umask(0)/umask(prev) probe. A bare load+store would let - // two threads interleave the probe and leave the process umask wrong. - if HAS_SET_UMASK - .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) - .is_ok() - { - let prev = sys::umask(0); - sys::umask(prev); - UMASK.store(prev as u32, Ordering::Release); - } - } - fn unlink_bin_or_shim(abs_dest: &ZStr) { #[cfg(not(windows))] { @@ -1322,7 +1304,7 @@ impl<'a> Linker<'a> { fn chmod_on_ok(err: Option, abs_target: &ZStr) { // hoisted from `defer` block in create_symlink if err.is_none() { - let mode = 0o777 & !(UMASK.load(Ordering::Acquire) as Mode); + let mode = 0o777 & !sys::get_umask(); let _ = sys::lchmod(abs_target, mode); } } diff --git a/src/install/error.rs b/src/install/error.rs index 1b2e2e464587..e4d4b71b7428 100644 --- a/src/install/error.rs +++ b/src/install/error.rs @@ -10,8 +10,6 @@ pub enum Error { NameTooLong, #[error("SymLinkLoop")] SymLinkLoop, - #[error("SystemFdQuotaExceeded")] - SystemFdQuotaExceeded, #[error("SystemResources")] SystemResources, #[error("TarballHTTP400")] @@ -246,7 +244,6 @@ impl Error { Self::NotDir => "NotDir", Self::NameTooLong => "NameTooLong", Self::SymLinkLoop => "SymLinkLoop", - Self::SystemFdQuotaExceeded => "SystemFdQuotaExceeded", Self::SystemResources => "SystemResources", Self::TarballHTTP400 => "TarballHTTP400", Self::TarballHTTP401 => "TarballHTTP401", @@ -402,7 +399,6 @@ impl From for Error { bun_libarchive::Error::Fail => Self::Fail, bun_libarchive::Error::Sys(s) => Self::Sys(s), bun_libarchive::Error::Alloc(a) => Self::Alloc(a), - bun_libarchive::Error::MakeLibUvOwned(_) => Self::SystemFdQuotaExceeded, bun_libarchive::Error::Paths(p) => Self::Paths(p), } } diff --git a/src/install/extract_tarball.rs b/src/install/extract_tarball.rs index d1e039353b49..bb83fe6626cc 100644 --- a/src/install/extract_tarball.rs +++ b/src/install/extract_tarball.rs @@ -586,10 +586,10 @@ impl ExtractTarball { bun_sys::Result::Err(err) => { if retries < MAX_RETRIES { match err.get_errno() { - sys::Errno::NOTEMPTY - | sys::Errno::PERM - | sys::Errno::BUSY - | sys::Errno::EXIST => { + sys::Errno::ENOTEMPTY + | sys::Errno::EPERM + | sys::Errno::EBUSY + | sys::Errno::EEXIST => { // before we attempt to delete the destination, let's close the source dir. let _ = sys::close(dir_to_move); @@ -836,13 +836,7 @@ impl ExtractTarball { &[name, dest_name], ); - if sys::sys_uv::symlink_uv( - final_path, - dest_path, - bun_sys::windows::libuv::UV_FS_SYMLINK_JUNCTION, - ) - .is_err() - { + if sys::junction(final_path, dest_path).is_err() { break 'create_index; } } diff --git a/src/install/git_runner.rs b/src/install/git_runner.rs index a1a0cada2c40..b0650219e425 100644 --- a/src/install/git_runner.rs +++ b/src/install/git_runner.rs @@ -12,15 +12,12 @@ use bun_core::{Output, strings}; use bun_event_loop::EventLoopHandle; use bun_io::BufferedReader; #[cfg(unix)] -use bun_io::{FilePollFlag, PosixFlags}; +use bun_io::ReaderFlags; use bun_paths as Path; use bun_ptr::{BackRef, JsCell, RefPtr, ThisPtr}; -#[cfg(unix)] use bun_spawn::SpawnResultExt as _; use bun_spawn::{Process, ProcessHandle, Rusage, SpawnEnv, SpawnOptions, Status}; -use bun_sys::Fd; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; +use bun_sys::{Fd, FdExt as _}; use bun_threading::thread_pool as ThreadPool; use crate::install::{ExtractData, ExtractDataJson}; @@ -614,54 +611,21 @@ impl GitSubprocess { this.exit_status.set(None); this.remaining_fds.set(0); - // Windows: the `uv::Pipe` allocations are ours until the spawn succeeds. let spawn_options = SpawnOptions { stdin: bun_spawn::Stdio::Ignore, - #[cfg(unix)] stdout: bun_spawn::Stdio::Buffer, - #[cfg(unix)] stderr: bun_spawn::Stdio::Buffer, - #[cfg(windows)] - stdout: bun_spawn::Stdio::Buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - )) as bun_spawn::windows::UvPipePtr), - #[cfg(windows)] - stderr: bun_spawn::Stdio::Buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - )) as bun_spawn::windows::UvPipePtr), - #[cfg(windows)] - windows: bun_spawn::WindowsOptions { - loop_: this.event_loop, - ..Default::default() - }, stream: false, // The kernel kills the child when bun dies, so no git outlives the install. #[cfg(any(target_os = "linux", target_os = "android"))] linux_pdeathsig: Some(bun_sys::SignalCode::SIGKILL.0), ..Default::default() }; - #[cfg(windows)] - let mut spawn_options = spawn_options; - let spawned = - match bun_spawn::spawn_process_cstr(&spawn_options, &argv, SpawnEnv::Strings(&envp)) { - Ok(Ok(spawned)) => spawned, - res => { - #[cfg(windows)] - { - spawn_options.stdout.deinit(); - spawn_options.stderr.deinit(); - } - res??; - unreachable!(); - } - }; - #[cfg(windows)] - let mut spawned = spawned; + bun_spawn::spawn_process_cstr(&spawn_options, &argv, SpawnEnv::Strings(&envp))??; // A reader that fails to start reports through `on_reader_error`, which only // touches `remaining_fds` / `read_error`, never the reader cell being borrowed. - #[cfg(unix)] for (reader, fd, is_memfd) in [ (&this.stdout, spawned.stdout, spawned.memfds[1]), (&this.stderr, spawned.stderr, spawned.memfds[2]), @@ -669,36 +633,23 @@ impl GitSubprocess { let Some(fd) = fd else { continue }; reader.with_mut(|r| r.set_parent(this_ptr)); if is_memfd { + #[cfg(unix)] reader.with_mut(|r| r.start_memfd(fd)); continue; } - let _ = bun_sys::set_nonblocking(fd); this.remaining_fds.set(this.remaining_fds.get() + 1); + // POSIX: the parent end is a socketpair half, which spawn made nonblocking. + #[cfg(unix)] reader.with_mut(|r| { - r.flags.insert(PosixFlags::NONBLOCKING | PosixFlags::SOCKET); - r.start(fd, true) - })?; - reader.with_mut(|r| { - if let Some(poll) = r.handle.get_poll() { - poll.set_flag(FilePollFlag::Socket); - } - }); - } - #[cfg(windows)] - for (reader, pipe) in [ - (&this.stdout, spawned.stdout.take()), - (&this.stderr, spawned.stderr.take()), - ] { - // Take sole ownership of each pipe before `spawned` drops. - let bun_spawn::SpawnedStdio::Buffer(pipe) = pipe else { - continue; - }; - reader.with_mut(|r| { - r.set_source(bun_io::Source::Pipe(pipe)); - r.set_parent(this_ptr); + r.flags + .insert(ReaderFlags::NONBLOCKING | ReaderFlags::SOCKET) }); - this.remaining_fds.set(this.remaining_fds.get() + 1); - reader.with_mut(|r| r.start_with_current_pipe())?; + if let Err(err) = reader.with_mut(|r| r.start(fd, true)) { + // Windows only: POSIX reports a failed start through `on_reader_error` + // itself. The reader did not take `fd`. + fd.close(); + Self::on_reader_error(this, err); + } } debug_assert!(this.process.get().is_none()); @@ -972,6 +923,6 @@ bun_io::impl_buffered_reader_parent! { on_reader_done = |this| GitSubprocess::on_reader_done(ThisPtr::new(this)); // SAFETY: `this` is the live runner registered via `set_parent`. on_reader_error = |this, err| GitSubprocess::on_reader_error(ThisPtr::new(this), err); - loop_ = |this| (*this).event_loop.native_loop(); + loop_ = |this| (*this).event_loop.loop_(); event_loop = |this| (*this).event_loop.as_event_loop_ctx(); } diff --git a/src/install/hoisted_install.rs b/src/install/hoisted_install.rs index d58a3032f247..6b7d452e2b1d 100644 --- a/src/install/hoisted_install.rs +++ b/src/install/hoisted_install.rs @@ -265,11 +265,6 @@ pub(crate) fn install_hoisted_packages( buf_strings.as_slice(), ); - #[cfg(unix)] - { - bin::Linker::ensure_umask(); - } - let mut installer: PackageInstaller = 'brk: { let (completed_trees, tree_ids_to_trees_the_id_depends_on) = 'trees: { let trees = this.lockfile.buffers.trees.as_slice(); diff --git a/src/install/isolated_install/Hardlinker.rs b/src/install/isolated_install/Hardlinker.rs index 4856f2de0c90..c2b429ef984f 100644 --- a/src/install/isolated_install/Hardlinker.rs +++ b/src/install/isolated_install/Hardlinker.rs @@ -72,7 +72,7 @@ impl Hardlinker { Ok(s) => &*s, Err(_) => { return Ok(sys::Result::Err(sys::Error::from_code( - sys::E::ACCES, + sys::E::EACCES, sys::Tag::link, ))); } @@ -149,7 +149,7 @@ impl Hardlinker { match sys::link_w(self.src.slice_z(), destfile_path) { sys::Result::Ok(()) => {} sys::Result::Err(link_err1) => match link_err1.get_errno() { - sys::E::UV_EEXIST | sys::E::EEXIST => { + sys::E::EEXIST => { if crate::PackageManager::verbose_install() { bun_core::pretty_errorln!( "Hardlinking {} to a path that already exists: {}", @@ -182,7 +182,7 @@ impl Hardlinker { } } } - sys::E::UV_ENOENT | sys::E::ENOENT => { + sys::E::ENOENT => { if crate::PackageManager::verbose_install() { bun_core::pretty_errorln!( "Hardlinking {} to a path that doesn't exist: {}", diff --git a/src/install/isolated_install/Symlinker.rs b/src/install/isolated_install/Symlinker.rs index 52c90735abba..d29e49fcfd4d 100644 --- a/src/install/isolated_install/Symlinker.rs +++ b/src/install/isolated_install/Symlinker.rs @@ -110,7 +110,7 @@ impl Symlinker { }; let mut current_link: &[u8] = ¤t_link_buf[..current_link_len]; - // libuv adds a trailing slash to junctions. + // A junction's stored target may end in a slash. current_link = strings::without_trailing_slash(current_link); if strings::eql_long(current_link, self.target.slice_z().as_bytes(), true) { diff --git a/src/install/lifecycle_script_runner.rs b/src/install/lifecycle_script_runner.rs index 6a272a41171e..3e7fc0c475d8 100644 --- a/src/install/lifecycle_script_runner.rs +++ b/src/install/lifecycle_script_runner.rs @@ -9,23 +9,18 @@ use crate::lockfile_real::package::scripts::List as ScriptsList; use crate::package_manager_real::ProgressStrings; use bun_core::{Global, Output}; use bun_io::BufferedReader; -use bun_io::heap as io_heap; #[cfg(unix)] -use bun_io::{FilePollFlag, PosixFlags}; +use bun_io::ReaderFlags; +use bun_io::heap as io_heap; use bun_core::ZStr; -#[cfg(unix)] use bun_spawn::SpawnResultExt as _; use bun_spawn::{ Process, ProcessExit, ProcessExitKind, ProcessHandle, Rusage, SpawnOptions, Status, }; #[cfg(unix)] use bun_sys::Fd; -// `BufferedReaderParent::loop_` is typed `*mut bun_uws::Loop` (the -// `bun_io::Loop` is the trait's nominal: `us_loop_t` on POSIX, `uv_loop_t` -// on Windows. `AnyEventLoop::native_loop()` projects through the uws wrapper -// (`WindowsLoop::uv_loop`) on Windows so both paths hand back the same shape -// `BufferedReaderParent::loop_` expects. +use bun_sys::FdExt as _; bun_output::declare_scope!(Script, visible); @@ -346,9 +341,6 @@ impl<'a> LifecycleScriptSubprocess<'a> { } } -#[cfg(windows)] -use bun_sys::windows::libuv as uv; - pub type OutputReader = BufferedReader; impl<'a> LifecycleScriptSubprocess<'a> { @@ -404,16 +396,14 @@ impl<'a> LifecycleScriptSubprocess<'a> { self.handle_exit(status); } - /// Posix-only: re-prime a recycled `PosixBufferedReader` for a fresh socket fd. - /// Only called from the `#[cfg(unix)]` branch of [`spawn_next_script_inner`]; on Windows - /// the `OutputReader` is a `WindowsBufferedReader` (libuv-pipe-backed) and this fn is dead. + /// Re-prime a recycled `BufferedReader` for a fresh socket fd. #[cfg(unix)] fn reset_output_flags(output: &mut OutputReader, fd: Fd) { output .flags - .insert(PosixFlags::NONBLOCKING | PosixFlags::SOCKET); + .insert(ReaderFlags::NONBLOCKING | ReaderFlags::SOCKET); output.flags.remove( - PosixFlags::MEMFD | PosixFlags::RECEIVED_EOF | PosixFlags::CLOSED_WITHOUT_REPORTING, + ReaderFlags::MEMFD | ReaderFlags::RECEIVED_EOF | ReaderFlags::CLOSED_WITHOUT_REPORTING, ); #[cfg(debug_assertions)] @@ -585,17 +575,6 @@ impl<'a> LifecycleScriptSubprocess<'a> { core::mem::size_of::<[*const c_char; 4]>() == 4 * core::mem::size_of::() ); - // OWNERSHIP: - // `bun_io::Source::Pipe` owns a `Box` AND - // `spawn_process_windows` does `heap::take(ptr)` on the - // `Stdio::Buffer` pointer to produce a SECOND `Box` in - // `WindowsStdioResult::Buffer` — pre-stashing here would create two - // `Box`es over one allocation (UAF + double-free when `spawned` - // drops). Instead allocate the raw heap pipe inline in the - // `Stdio::Buffer` arm below (so it is only allocated when actually - // passed to libuv) and take SOLE ownership from - // `spawned.stdout/stderr` after spawn — see the `#[cfg(windows)]` - // block below and `filter_run.rs` for the canonical pattern. let spawn_options = SpawnOptions { stdin: if (*this).foreground { bun_spawn::Stdio::Inherit @@ -608,47 +587,16 @@ impl<'a> LifecycleScriptSubprocess<'a> { } else if (*manager).options.log_level.is_verbose() || (*this).foreground { bun_spawn::Stdio::Inherit } else { - #[cfg(unix)] - { - bun_spawn::Stdio::Buffer - } - #[cfg(not(unix))] - { - // Ownership of this raw heap allocation transfers to - // `spawn_process_windows`, which `heap::take`s it into - // `spawned.stdout`. - bun_spawn::Stdio::Buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - )) - as bun_spawn::windows::UvPipePtr) - } + bun_spawn::Stdio::Buffer }, stderr: if (*manager).options.log_level == crate::LogLevel::Silent { bun_spawn::Stdio::Ignore } else if (*manager).options.log_level.is_verbose() || (*this).foreground { bun_spawn::Stdio::Inherit } else { - #[cfg(unix)] - { - bun_spawn::Stdio::Buffer - } - #[cfg(not(unix))] - { - // Ownership transfers to `spawned.stderr`. - bun_spawn::Stdio::Buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - )) - as bun_spawn::windows::UvPipePtr) - } + bun_spawn::Stdio::Buffer }, cwd: Box::<[u8]>::from(cwd), - - #[cfg(windows)] - windows: bun_spawn::WindowsOptions { - loop_: bun_event_loop::EventLoopHandle::from_any(&mut (*manager).event_loop), - ..Default::default() - }, - stream: false, ..Default::default() }; @@ -661,93 +609,39 @@ impl<'a> LifecycleScriptSubprocess<'a> { (*manager) .active_lifecycle_scripts .insert(this.cast::>()); - let spawned = match bun_spawn::spawn_process( + let spawned = bun_spawn::spawn_process( &spawn_options, // argv is `[*const c_char; 4]` with trailing null — exactly the // `[*:null]?[*:0]const u8` layout `spawn_process` expects (1 word/elt). argv.as_mut_ptr().cast(), (*this).envp.as_ptr().cast::<*const c_char>(), - ) { - Ok(Ok(s)) => s, - res => { - #[cfg(windows)] - { - // `spawn_process_windows` only `heap::take`s the `Stdio::Buffer` - // raw `*mut uv::Pipe` allocations on the SUCCESS path; on every - // error return (uv_pipe_init failure, uv_spawn failure) ownership - // stays with the caller. `WindowsStdio` has no `Drop`, so reclaim - // and `uv_close`+free them explicitly here — otherwise the heap - // `uv::Pipe`s leak (and, if already `uv_pipe_init`'d, remain - // linked in the libuv loop's handle queue forever). Allocation - // happens inline (see OWNERSHIP note above), so the error - // path must be handled explicitly. - let mut spawn_options = spawn_options; - spawn_options.stdout.deinit(); - spawn_options.stderr.deinit(); - } - res??; - unreachable!(); - } - }; - #[cfg(windows)] - let mut spawned = spawned; - - #[cfg(unix)] - { - if let Some(stdout) = spawned.stdout { - if !spawned.memfds[1] { - (*this).stdout.set_parent(this.cast::()); - let _ = bun_sys::set_nonblocking(stdout); - (*this).remaining_fds += 1; - - Self::reset_output_flags(&mut (*this).stdout, stdout); - (*this).stdout.start(stdout, true)?; - if let Some(poll) = (*this).stdout.handle.get_poll() { - poll.set_flag(FilePollFlag::Socket); - } - } else { - (*this).stdout.set_parent(this.cast::()); - (*this).stdout.start_memfd(stdout); - } - } - if let Some(stderr) = spawned.stderr { - if !spawned.memfds[2] { - (*this).stderr.set_parent(this.cast::()); - let _ = bun_sys::set_nonblocking(stderr); - (*this).remaining_fds += 1; - - Self::reset_output_flags(&mut (*this).stderr, stderr); - (*this).stderr.start(stderr, true)?; - if let Some(poll) = (*this).stderr.handle.get_poll() { - poll.set_flag(FilePollFlag::Socket); - } - } else { - (*this).stderr.set_parent(this.cast::()); - (*this).stderr.start_memfd(stderr); - } - } - } - #[cfg(windows)] - { - // `spawn_process_windows` has already `heap::take`n the raw pipe - // pointers out of `Stdio::Buffer` into `spawned.{stdout,stderr}` - // as `WindowsStdioResult::Buffer(Box)`. Take that Box - // out *here* (sole owner) and stash it in `source` BEFORE - // `start_with_current_pipe` (which reads `source.?.pipe`) and - // BEFORE `spawned` drops — otherwise the `Box` is freed - // while libuv still has the handle queued (UAF) and the later - // `close_impl`→`on_pipe_close`→`heap::take` double-frees. - if let bun_spawn::SpawnedStdio::Buffer(pipe) = spawned.stdout.take() { - (*this).stdout.set_source(bun_io::Source::Pipe(pipe)); - (*this).stdout.set_parent(this.cast::()); - (*this).remaining_fds += 1; - (*this).stdout.start_with_current_pipe()?; + )??; + + for (is_stderr, fd, is_memfd) in [ + (false, spawned.stdout, spawned.memfds[1]), + (true, spawned.stderr, spawned.memfds[2]), + ] { + let Some(fd) = fd else { continue }; + let output: *mut OutputReader = if is_stderr { + &raw mut (*this).stderr + } else { + &raw mut (*this).stdout + }; + (*output).set_parent(this.cast::()); + if is_memfd { + #[cfg(unix)] + (*output).start_memfd(fd); + continue; } - if let bun_spawn::SpawnedStdio::Buffer(pipe) = spawned.stderr.take() { - (*this).stderr.set_source(bun_io::Source::Pipe(pipe)); - (*this).stderr.set_parent(this.cast::()); - (*this).remaining_fds += 1; - (*this).stderr.start_with_current_pipe()?; + (*this).remaining_fds += 1; + // POSIX: the parent end is a socketpair half, which spawn made nonblocking. + #[cfg(unix)] + Self::reset_output_flags(&mut *output, fd); + if let Err(err) = (*output).start(fd, true) { + // Windows only: POSIX reports a failed start through + // `on_reader_error` itself. The reader did not take `fd`. + fd.close(); + (*this).on_reader_error(&err); } } @@ -1175,7 +1069,7 @@ bun_io::impl_buffered_reader_parent! { has_on_read_chunk = false; on_reader_done = |this| (*this).on_reader_done(); on_reader_error = |this, err| (*this).on_reader_error(&err); - loop_ = |this| (*(*this).manager.as_ptr()).event_loop.native_loop(); + loop_ = |this| (*(*this).manager.as_ptr()).event_loop.loop_(); event_loop = |this| bun_event_loop::EventLoopHandle::from_any( &mut (*(*this).manager.as_ptr()).event_loop, ).as_event_loop_ctx(); diff --git a/src/install/windows-shim/Cargo.toml b/src/install/windows-shim/Cargo.toml index c99a9dc82815..f02376c7e61c 100644 --- a/src/install/windows-shim/Cargo.toml +++ b/src/install/windows-shim/Cargo.toml @@ -21,7 +21,7 @@ # `#![no_std]`, zero `#[no_mangle]`) are the ONLY workspace crates. Pulling # `bun_core` / `bun_sys` / `bun_string` would drag in their `#[no_mangle]` # C-ABI surface (`Bun__*`, `__bun_dispatch__*`), which references -# libuv/simdutf/highway/ICU and makes those undefined symbols link roots — the +# simdutf/highway/ICU and makes those undefined symbols link roots — the # shim can't satisfy them and shouldn't carry them. `main.rs` provides local # stand-ins for the handful of helpers the shared source reaches for # (`bun_core::ffi::{slice,slice_mut,zeroed}`, `RacyCell`, `bun_str::w!`, diff --git a/src/install/windows-shim/bun_shim_impl.rs b/src/install/windows-shim/bun_shim_impl.rs index 48fdda8a5872..e58e009c94a9 100644 --- a/src/install/windows-shim/bun_shim_impl.rs +++ b/src/install/windows-shim/bun_shim_impl.rs @@ -49,7 +49,7 @@ use core::marker::ConstParamTy; use core::mem::{MaybeUninit, size_of}; // Standalone PE: depend ONLY on `bun_windows_sys` (leaf, no native C, no -// `#[no_mangle]` exports) so the link has no libuv/simdutf/ICU roots and the +// `#[no_mangle]` exports) so the link has no simdutf/ICU roots and the // binary stays tiny. `crate::compat` // re-exports `bun_windows_sys::*` and locally declares the few items // (`CreateProcessW`, `STARTUPINFOW`, `TEB`/`teb()`, …) that otherwise live in @@ -62,9 +62,7 @@ use crate::bun_core; use crate::compat as w; #[cfg(not(feature = "shim_standalone"))] use bun_sys::windows as w; -use w::{ - BOOL, DWORD, HANDLE, IO_STATUS_BLOCK, LARGE_INTEGER, NTSTATUS, PVOID, ULONG, UNICODE_STRING, -}; +use w::{BOOL, DWORD, FILE_GENERIC_READ, HANDLE, IO_STATUS_BLOCK, NTSTATUS, UNICODE_STRING}; use super::_bin_linking_shim::Flags; @@ -77,104 +75,21 @@ const IS_STANDALONE: bool = cfg!(feature = "shim_standalone"); #[cfg(not(feature = "shim_standalone"))] bun_output::declare_scope!(bun_shim_impl, hidden); -/// A copy of all ntdll declarations this program uses +/// The ntdll functions this program uses. mod nt { use super::*; - pub(super) type Status = NTSTATUS; - - /// https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntcreatefile - pub(super) use w::ntdll::NtCreateFile; - - // SAFETY: ntdll syscalls; signatures match WDK headers. Declared locally as - // `safe fn` (vs. re-exporting the `unsafe fn` from `w::ntdll`) because - // neither has memory-safety preconditions: all arguments are by-value, - // `HANDLE` is an opaque kernel token validated kernel-side (bad handle → - // `STATUS_INVALID_HANDLE`, not UB), and `RtlExitUserProcess` diverges - // (matches `ExitProcess`, already `safe fn` in `bun_windows_sys`). This - // freestanding `no_std` shim owns every handle it closes; no - // `OwnedHandle`-style I/O-safety invariant exists to violate. - #[link(name = "ntdll")] - unsafe extern "system" { - /// undocumented - pub(super) safe fn RtlExitUserProcess(ExitStatus: u32) -> !; - - /// https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntclose - pub(super) safe fn NtClose(Handle: HANDLE) -> Status; - } - - // Declared locally (not in `bun_sys::windows::ntdll`) so the standalone PE - // build, whose `w` alias is `crate::compat`, needs no extra re-export. - // SAFETY: ntdll syscalls; signatures match WDK headers. Kept `unsafe fn` - // (not `safe fn`) because both write through caller-supplied out-pointers - // (`IoStatusBlock`, `Buffer`) — validity is a genuine caller precondition. - unsafe extern "system" { - /// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntreadfile - #[link_name = "NtReadFile"] - pub(super) fn NtReadFile( - FileHandle: HANDLE, // [in] - // `Option<*mut c_void>` is not pointer-sized (raw pointers can - // already be null → no niche → 16-byte tagged enum, passed - // by-reference under Win64 ABI). Use a plain HANDLE and pass - // null_mut() for "no event". - Event: HANDLE, // [in, optional] - ApcRoutine: *mut c_void, // [in, optional] - ApcContext: PVOID, // [in, optional] - IoStatusBlock: *mut IO_STATUS_BLOCK, // [out] - Buffer: PVOID, // [out] - Length: ULONG, // [in] - ByteOffset: *const LARGE_INTEGER, // [in, optional] - Key: *const ULONG, // [in, optional] - ) -> Status; - - /// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntwritefile - #[link_name = "NtWriteFile"] - pub(super) fn NtWriteFile( - FileHandle: HANDLE, // [in] - Event: HANDLE, // [in, optional] (see NtReadFile note re: Option) - ApcRoutine: *mut c_void, // [in, optional] - ApcContext: PVOID, // [in, optional] - IoStatusBlock: *mut IO_STATUS_BLOCK, // [out] - Buffer: *const u8, // [in] - Length: ULONG, // [in] - ByteOffset: *const LARGE_INTEGER, // [in, optional] - Key: *const ULONG, // [in, optional] - ) -> Status; - } + pub(super) use w::ntdll::{NtClose, NtCreateFile, NtReadFile, NtWriteFile, RtlExitUserProcess}; } -/// A copy of all kernel32 declarations this program uses +/// The kernel32 functions this program uses. mod k32 { use super::*; - pub(super) use w::kernel32::CreateProcessW; - /// https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror - pub(super) use w::kernel32::GetLastError; - /// https://learn.microsoft.com/en-us/windows/console/setconsolectrlhandler - pub(super) use w::kernel32::SetConsoleCtrlHandler; - - // SAFETY: kernel32 externs; signatures match SDK. Declared locally as - // `safe fn` (vs. re-exporting `unsafe fn` from `w::kernel32`) because - // none has a memory-safety precondition the type system can't encode: - // `HANDLE` is opaque and validated kernel-side (bad handle → `WAIT_FAILED` - // / `FALSE` + `GetLastError`, not UB); by-value scalars are trivially - // sound; the two `LPDWORD` out-params are taken as `&mut DWORD` (ABI- - // identical to `*mut DWORD`, but Rust guarantees non-null/aligned/valid- - // for-write so the kernel write cannot fault). - #[link(name = "kernel32")] - unsafe extern "system" { - /// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject - pub(super) safe fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD; - - /// https://learn.microsoft.com/en-us/windows/console/setconsolemode - pub(super) safe fn SetConsoleMode(hConsoleHandle: HANDLE, dwMode: DWORD) -> BOOL; - - /// https://learn.microsoft.com/en-us/windows/console/getconsolemode - pub(super) safe fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: &mut DWORD) -> BOOL; - - /// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess - pub(super) safe fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: &mut DWORD) -> BOOL; - } + pub(super) use w::kernel32::{ + CreateProcessW, GetConsoleMode, GetExitCodeProcess, GetLastError, SetConsoleCtrlHandler, + SetConsoleMode, WaitForSingleObject, + }; } macro_rules! debug { @@ -208,12 +123,6 @@ unsafe fn unicode_string_to_u16<'a>(str: &'a UNICODE_STRING) -> &'a [u16] { unsafe { bun_core::ffi::slice(str.Buffer, (str.Length / 2) as usize) } } -const FILE_GENERIC_READ: u32 = w::STANDARD_RIGHTS_READ - | w::FILE_READ_DATA - | w::FILE_READ_ATTRIBUTES - | w::FILE_READ_EA - | w::SYNCHRONIZE; - #[derive(Clone, Copy, PartialEq, Eq)] pub enum FailReason { NoDirname, @@ -349,7 +258,7 @@ pub(crate) fn write_to_handle(handle: HANDLE, data: &[u8]) -> usize { core::ptr::null_mut(), core::ptr::null_mut(), &mut io, - data.as_ptr(), + data.as_ptr().cast::(), u32::try_from(data.len()).expect("int cast"), core::ptr::null(), core::ptr::null(), @@ -1278,16 +1187,7 @@ fn launcher(bun_ctx: Ctx) -> LauncherRet // Gated on `Launch` so `ReadWithoutLaunch` does not irreversibly mutate the // parent process's stdio state. bun_core::output::source::stdio::restore(); - // Declared locally as `safe fn` (the `bun_sys::windows` re-export - // forwards `bun_windows_sys::externs::windows_enable_stdio_inheritance`, - // which is not yet `safe`-qualified): zero args, zero memory-safety - // preconditions — the C++ shim only flips `HANDLE_FLAG_INHERIT` on the - // three process-lifetime standard handles. Matches the local `safe fn` - // redecl already used in `bun_sys::windows::become_watcher_manager`. - unsafe extern "C" { - safe fn windows_enable_stdio_inheritance(); - } - windows_enable_stdio_inheritance(); + w::windows_enable_stdio_inheritance(); } // I attempted to use lower level methods for this, but it really seems @@ -1387,7 +1287,7 @@ fn launcher(bun_ctx: Ctx) -> LauncherRet if IS_STANDALONE { 0 } else { - 0x0000_0400 /* CREATE_UNICODE_ENVIRONMENT */ + w::CREATE_UNICODE_ENVIRONMENT }, if IS_STANDALONE { core::ptr::null_mut() diff --git a/src/install/windows-shim/main.rs b/src/install/windows-shim/main.rs index 81a5e663f8c7..a0462b314df9 100644 --- a/src/install/windows-shim/main.rs +++ b/src/install/windows-shim/main.rs @@ -11,7 +11,7 @@ //! workspace dep is `bun_windows_sys` (leaf Win32 externs — no native C, //! no `#[no_mangle]` exports). Depending on the real crates would make //! their `#[no_mangle]` C-ABI surface (`Bun__*`, `__bun_dispatch__*`) a -//! link root referencing libuv/simdutf/highway/ICU; the shim can't +//! link root referencing simdutf/highway/ICU; the shim can't //! satisfy those and shouldn't carry them. //! //! `BinLinkingShim.rs` is path-included for `Flags`/`VersionFlag` only; its @@ -143,14 +143,7 @@ pub(crate) extern "C" fn shim_main() -> ! { #[cfg(windows)] #[panic_handler] fn panic(_: &core::panic::PanicInfo<'_>) -> ! { - // Declared locally as `safe fn` (the `bun_windows_sys::ntdll` re-export - // is not yet `safe`-qualified): no memory-safety preconditions — by-value - // `u32`, diverges. Matches `ExitProcess`, already `safe fn` upstream. - #[link(name = "ntdll")] - unsafe extern "system" { - safe fn RtlExitUserProcess(ExitStatus: u32) -> !; - } - RtlExitUserProcess(255) + bun_windows_sys::ntdll::RtlExitUserProcess(255) } // Non-Windows: the build system only ever builds this crate for @@ -271,20 +264,13 @@ pub mod bun_core { /// `bun_sys::windows` stand-in. Re-exports the leaf `bun_windows_sys` surface /// (which now owns CreateProcessW, STARTUPINFOW / PROCESS_INFORMATION, the -/// TEB→PEB→ProcessParameters chain, and `teb()`/`peb()`); only the shim-local -/// `PVOID` alias and console-mode flag remain here. +/// TEB→PEB→ProcessParameters chain, and `teb()`/`peb()`). #[cfg(windows)] pub mod compat { - use core::ffi::c_void; - pub use bun_windows_sys::*; // Distinct sub-module so `w::ntdll::NtClose` etc. resolve. pub use bun_windows_sys::ntdll; - // ── aliases / consts not yet in bun_windows_sys ── - pub(crate) type PVOID = *mut c_void; - pub(crate) const ENABLE_VIRTUAL_TERMINAL_PROCESSING: DWORD = 0x0004; - // ── kernel32 surface (bun_sys::windows::kernel32 layers extras on top // of bun_windows_sys::kernel32; mirror just what the shim calls) ── pub mod kernel32 { diff --git a/src/io/Cargo.toml b/src/io/Cargo.toml index ef315b5137cb..f397554b73a1 100644 --- a/src/io/Cargo.toml +++ b/src/io/Cargo.toml @@ -26,3 +26,6 @@ bun_sys.workspace = true bun_uws_sys.workspace = true bun_errno.workspace = true bun_threading.workspace = true + +[target.'cfg(windows)'.dependencies] +bun_windows_sys.workspace = true diff --git a/src/io/MaxBuf.rs b/src/io/MaxBuf.rs index 959a9ff3f2f7..cc1587bf4d9c 100644 --- a/src/io/MaxBuf.rs +++ b/src/io/MaxBuf.rs @@ -167,15 +167,21 @@ impl MaxBuf { /// Trims `buf` to the remaining budget plus `OVERREAD_ALLOWANCE`: unclamped, /// a 256 KB scratch read off a socketpair overshoots `maxBuffer` by hundreds /// of KB. Never empties a non-empty `buf` (that would read as EOF). + #[cfg(unix)] pub(crate) fn clamp_read_buf(this: Option>, buf: &mut [u8]) -> &mut [u8] { + let len = Self::clamp_read_len(this, buf.len()); + &mut buf[..len] + } + + /// [`clamp_read_buf`](Self::clamp_read_buf) for a read whose buffer is not the caller's to slice. + pub(crate) fn clamp_read_len(this: Option>, len: usize) -> usize { let Some(this) = this else { - return buf; + return len; }; let remaining = u64::try_from(Self::live(&this).remaining_bytes.get()).unwrap_or(0); let limit = usize::try_from(remaining.saturating_add(OVERREAD_ALLOWANCE)).unwrap_or(usize::MAX); - let len = buf.len().min(limit); - &mut buf[..len] + len.min(limit) } } diff --git a/src/io/PipeReader.rs b/src/io/PipeReader.rs index 6373abeaf783..797394687e13 100644 --- a/src/io/PipeReader.rs +++ b/src/io/PipeReader.rs @@ -4,39 +4,21 @@ use core::ptr::NonNull; use bun_sys::{self as sys, Fd}; -use crate::{EventLoopHandle, FilePollKind, FilePollRef, Owner, PollTag}; -// `bun.Async.Loop` — on POSIX the uws `us_loop_t`, on Windows the embedded -// `uv_loop_t` (`bun_io::Loop` is the cfg-aliased nominal that picks the -// right one). `BufferedReaderParent::loop_` returns this so callers in T3+ -// can hand it to libuv/uws without a cross-crate cast. -// -// Public so trait implementors in `bun_runtime` can name the same type in -// their `loop_` signature without duplicating the cfg-split. -#[cfg(not(windows))] +use crate::EventLoopHandle; +#[cfg(unix)] +use crate::{FilePollKind, FilePollRef, Owner, PollTag}; +// Public so trait implementors in `bun_runtime` can name the type in their +// `loop_` signature. pub type Loop = bun_uws_sys::Loop; -#[cfg(windows)] -pub type Loop = bun_sys::windows::libuv::Loop; -/// `bun_io::poll_tag::BUFFERED_READER` — every `FilePoll` allocated by this -/// module stores a `*mut BufferedReader` (erased) as its owner; the per-tag -/// dispatch in `bun_runtime::dispatch::__bun_run_file_poll` recovers the type -/// from this constant. T2 cannot name `bun_io`, so the value is mirrored. use crate::max_buf::MaxBuf; -use crate::pipes::{Chunk, FileType, PollOrFd, ReadState}; +use crate::pipes::{Chunk, ReadState}; +#[cfg(unix)] +use crate::pipes::{FileType, PollOrFd}; #[cfg(windows)] use crate::source::Source; - #[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; -#[cfg(windows)] -// `close`/`set_data`/`is_closed` are default trait methods; bring traits into -// scope so method resolution finds them on `Pipe`/`uv_tty_t`/`fs_t`. -use bun_sys::windows::libuv::UvHandle as _; - -// All logging in this module goes through `bun.sys.syslog` (the `SYS` scope) -// or `libuv::log!`. +use crate::windows::{PipeOrigin, ReadEvent}; // ────────────────────────────────────────────────────────────────────────── // BufferedReaderVTable @@ -106,6 +88,7 @@ impl BufferedReaderVTable { unsafe { crate::BufferedReaderParentLink::new(self.kind, self.parent) } } + #[cfg(unix)] fn event_loop(&self) -> EventLoopHandle { self.link().event_loop() } @@ -161,6 +144,7 @@ impl ReadLimit { self.0.map_or(len, |remaining| len.min(remaining)) } + #[cfg(unix)] fn clamp(self, buf: &mut [u8]) -> &mut [u8] { let len = self.clamp_len(buf.len()); &mut buf[..len] @@ -181,18 +165,33 @@ impl ReadLimit { } // ────────────────────────────────────────────────────────────────────────── -// PosixBufferedReader +// BufferedReader // ────────────────────────────────────────────────────────────────────────── -pub struct PosixBufferedReader { +/// On POSIX the fd is polled and read when it is readable. On Windows reads +/// complete through the loop's completion port into a buffer the source owns, +/// and arrive here as `ReadEvent`s; pausing never cancels a read that is +/// already with the kernel: whatever it produces is kept by the source until +/// the reader resumes. +pub struct BufferedReader { + #[cfg(unix)] pub handle: PollOrFd, /// Set once `preadv2(RWF_NOWAIT)` said this fd's file type does not support it (tty), so we stop asking. + #[cfg(unix)] rwf_unsupported: core::cell::Cell, + /// `None` before `start` and after `close`. + #[cfg(windows)] + pub source: Option, + /// A reader that accumulates (no `on_read_chunk`) lends this to its pipe + /// between chunks, so each read lands in place (`lent`). pub _buffer: Vec, + /// How much of `_buffer` is with the pipe (`Pipe::lend_read_buffer`). + #[cfg(windows)] + lent: usize, pub(crate) _offset: usize, limit: ReadLimit, pub(crate) vtable: BufferedReaderVTable, - pub flags: PosixFlags, + pub flags: ReaderFlags, // MaxBuf uses hand-rolled dual-ownership (Subprocess + reader) via // `add_to_pipereader`/`remove_from_pipereader`, not Arc — see MaxBuf.rs. pub maxbuf: Option>, @@ -200,7 +199,7 @@ pub struct PosixBufferedReader { bitflags::bitflags! { #[derive(Clone, Copy, Default)] - pub struct PosixFlags: u16 { + pub struct ReaderFlags: u16 { const IS_DONE = 1 << 0; const POLLABLE = 1 << 1; const NONBLOCKING = 1 << 2; @@ -212,35 +211,96 @@ bitflags::bitflags! { const USE_PREAD = 1 << 8; const IS_PAUSED = 1 << 9; const KEEP_ALIVE = 1 << 10; // default true - /// A read failed with a non-retry errno. Set before the bytes read ahead of the failure are delivered, so a pull from inside that delivery cannot read the fd past the error. Never cleared: `start()`, `unpause()` and `from()` keep it, and only a reader from `init()` reads again. + /// A read failed with a non-retry errno. Set before the bytes read ahead of the failure are delivered, so a pull from inside that delivery cannot read the fd past the error. `start()`, `unpause()` and `from()` keep it: a reader from `init()` reads again, and one whose owner takes the flag off. const READ_FAILED = 1 << 11; + /// Windows: the fd is a duplicate of one JS has, and shares its file position. + const SHARES_POSITION = 1 << 12; } } -impl PosixFlags { +impl ReaderFlags { pub(crate) const fn new() -> Self { - Self::from_bits_truncate(PosixFlags::CLOSE_HANDLE.bits() | PosixFlags::KEEP_ALIVE.bits()) + Self::from_bits_truncate(ReaderFlags::CLOSE_HANDLE.bits() | ReaderFlags::KEEP_ALIVE.bits()) } } -impl PosixBufferedReader { - pub fn init() -> PosixBufferedReader { - PosixBufferedReader { +impl BufferedReader { + pub fn init() -> BufferedReader { + BufferedReader { + #[cfg(unix)] handle: PollOrFd::Closed, + #[cfg(unix)] rwf_unsupported: core::cell::Cell::new(false), + #[cfg(windows)] + source: None, _buffer: Vec::new(), + #[cfg(windows)] + lent: 0, _offset: 0, limit: ReadLimit::NONE, vtable: BufferedReaderVTable::init::(), - flags: PosixFlags::new(), + flags: ReaderFlags::new(), maxbuf: None, } } + #[inline] + pub fn is_done(&self) -> bool { + self.flags.intersects( + ReaderFlags::IS_DONE + | ReaderFlags::RECEIVED_EOF + | ReaderFlags::CLOSED_WITHOUT_REPORTING, + ) + } + + pub fn memory_cost(&self) -> usize { + #[cfg(windows)] + let lent = self.lent; + #[cfg(not(windows))] + let lent = 0; + mem::size_of::() + self._buffer.capacity() + lent + } + + pub fn take_buffer(&mut self) -> Vec { + mem::take(self.buffer()) + } + + pub fn buffer(&mut self) -> &mut Vec { + #[cfg(windows)] + self.recall_buffer(); + &mut self._buffer + } + + pub fn disable_keeping_process_alive(&mut self, _event_loop_ctx: C) { + self.update_ref(false); + } + + pub fn start_file_offset(&mut self, fd: Fd, poll: bool, offset: usize) -> sys::Result<()> { + self._offset = offset; + self.flags.insert(ReaderFlags::USE_PREAD); + self.start(fd, poll) + } + + /// Ends the reader after the next `len` bytes of the source as if they were followed by EOF (`ReadLimit`); `None` reads to EOF. Set before starting. + pub fn set_limit(&mut self, len: Option) { + self.limit = ReadLimit(len); + } + + /// Charges `bytes_read` against the `maxBuffer` budget; `true` once it is gone. The overflow callback only kills the child, which takes effect asynchronously, so the caller must also stop reading. + fn charge_max_buffer(&mut self, bytes_read: usize) -> bool { + let Some(maxbuf) = self.maxbuf else { + return false; + }; + MaxBuf::on_read_bytes(maxbuf, bytes_read as u64) + } +} + +#[cfg(unix)] +impl BufferedReader { pub fn update_ref(&mut self, value: bool) { // Remember the ref state so a poll created later (lazy start) honours // an unref() that preceded the first registration. - self.flags.set(PosixFlags::KEEP_ALIVE, value); + self.flags.set(ReaderFlags::KEEP_ALIVE, value); let Some(poll) = self.handle.get_poll() else { return; }; @@ -251,20 +311,9 @@ impl PosixBufferedReader { poll.set_keeping_process_alive(self.vtable.event_loop(), value); } - #[inline] - pub fn is_done(&self) -> bool { - self.flags.intersects( - PosixFlags::IS_DONE | PosixFlags::RECEIVED_EOF | PosixFlags::CLOSED_WITHOUT_REPORTING, - ) - } - - pub fn memory_cost(&self) -> usize { - mem::size_of::() + self._buffer.capacity() - } - - pub fn from(&mut self, other: &mut PosixBufferedReader, parent: *mut c_void) { + pub fn from(&mut self, other: &mut BufferedReader, parent: *mut c_void) { let kind = self.vtable.kind; - *self = PosixBufferedReader { + *self = BufferedReader { handle: mem::replace(&mut other.handle, PollOrFd::Closed), rwf_unsupported: other.rwf_unsupported.clone(), _buffer: mem::take(other.buffer()), @@ -274,7 +323,7 @@ impl PosixBufferedReader { vtable: BufferedReaderVTable { kind, parent }, maxbuf: None, }; - other.flags.insert(PosixFlags::IS_DONE); + other.flags.insert(ReaderFlags::IS_DONE); other._offset = 0; other.limit = ReadLimit::NONE; MaxBuf::transfer_to_pipereader(&mut other.maxbuf, &mut self.maxbuf); @@ -298,18 +347,18 @@ impl PosixBufferedReader { } pub fn start_memfd(&mut self, fd: Fd) { - self.flags.insert(PosixFlags::MEMFD); + self.flags.insert(ReaderFlags::MEMFD); self.handle = PollOrFd::Fd(fd); } pub(crate) fn get_file_type(&self) -> FileType { let flags = self.flags; - if flags.contains(PosixFlags::SOCKET) { + if flags.contains(ReaderFlags::SOCKET) { return FileType::Socket; } - if flags.contains(PosixFlags::POLLABLE) { - if flags.contains(PosixFlags::NONBLOCKING) { + if flags.contains(ReaderFlags::POLLABLE) { + if flags.contains(ReaderFlags::NONBLOCKING) { return FileType::NonblockingPipe; } @@ -338,9 +387,9 @@ impl PosixBufferedReader { fn close_without_reporting(&mut self) { if self.get_fd() != Fd::INVALID { - debug_assert!(!self.flags.contains(PosixFlags::CLOSED_WITHOUT_REPORTING)); - self.flags.insert(PosixFlags::CLOSED_WITHOUT_REPORTING); - if self.flags.contains(PosixFlags::CLOSE_HANDLE) { + debug_assert!(!self.flags.contains(ReaderFlags::CLOSED_WITHOUT_REPORTING)); + self.flags.insert(ReaderFlags::CLOSED_WITHOUT_REPORTING); + if self.flags.contains(ReaderFlags::CLOSE_HANDLE) { let owner = std::ptr::from_mut(self).cast::(); self.handle.close(Some(owner), None::); } @@ -352,10 +401,10 @@ impl PosixBufferedReader { } pub fn pause(&mut self) { - if self.flags.contains(PosixFlags::IS_PAUSED) { + if self.flags.contains(ReaderFlags::IS_PAUSED) { return; } - self.flags.insert(PosixFlags::IS_PAUSED); + self.flags.insert(ReaderFlags::IS_PAUSED); // Unregister the FilePoll if it's registered if let PollOrFd::Poll(poll) = &mut self.handle { @@ -366,23 +415,15 @@ impl PosixBufferedReader { } pub fn unpause(&mut self) { - if !self.flags.contains(PosixFlags::IS_PAUSED) { + if !self.flags.contains(ReaderFlags::IS_PAUSED) { return; } - self.flags.remove(PosixFlags::IS_PAUSED); + self.flags.remove(ReaderFlags::IS_PAUSED); // The next read() call will re-register the poll if needed } - pub fn take_buffer(&mut self) -> Vec { - mem::take(&mut self._buffer) - } - - pub fn buffer(&mut self) -> &mut Vec { - &mut self._buffer - } - pub fn final_buffer(&mut self) -> &mut Vec { - if self.flags.contains(PosixFlags::MEMFD) { + if self.flags.contains(ReaderFlags::MEMFD) { if let PollOrFd::Fd(fd) = self.handle { // The handle is closed after the read regardless of result. // `self.handle` owns the fd; @@ -401,15 +442,11 @@ impl PosixBufferedReader { self.buffer() } - pub fn disable_keeping_process_alive(&mut self, _event_loop_ctx: C) { - self.update_ref(false); - } - fn finish(&mut self) { if !matches!(self.handle, PollOrFd::Closed) - || self.flags.contains(PosixFlags::CLOSED_WITHOUT_REPORTING) + || self.flags.contains(ReaderFlags::CLOSED_WITHOUT_REPORTING) { - if self.flags.contains(PosixFlags::CLOSE_HANDLE) { + if self.flags.contains(ReaderFlags::CLOSE_HANDLE) { // SAFETY: `self` is live. Note: this `&mut self` receiver still carries // a protector across the (maybe-freeing) done dispatch — pre-existing // on the parent chain, tracked with the raw-dispatch follow-up. @@ -418,8 +455,8 @@ impl PosixBufferedReader { return; } - debug_assert!(!self.flags.contains(PosixFlags::IS_DONE)); - self.flags.insert(PosixFlags::IS_DONE); + debug_assert!(!self.flags.contains(ReaderFlags::IS_DONE)); + self.flags.insert(ReaderFlags::IS_DONE); self._buffer.shrink_to_fit(); } @@ -430,12 +467,15 @@ impl PosixBufferedReader { /// receiver protector. unsafe fn close_handle(this: *mut Self) { // SAFETY: caller contract; borrows end at each `;`. - let deferred_report = - unsafe { (*this).flags.contains(PosixFlags::CLOSED_WITHOUT_REPORTING) }; + let deferred_report = unsafe { + (*this) + .flags + .contains(ReaderFlags::CLOSED_WITHOUT_REPORTING) + }; if deferred_report { // SAFETY: caller contract; borrow ends before the (maybe-freeing) done. unsafe { - (*this).flags.remove(PosixFlags::CLOSED_WITHOUT_REPORTING); + (*this).flags.remove(ReaderFlags::CLOSED_WITHOUT_REPORTING); Self::done(this); } return; @@ -445,11 +485,11 @@ impl PosixBufferedReader { // The close callback receives the same raw pointer, so its `done` also // runs without a receiver borrow. unsafe { - if (*this).flags.contains(PosixFlags::CLOSE_HANDLE) { + if (*this).flags.contains(ReaderFlags::CLOSE_HANDLE) { (*this).handle.close( Some(this.cast::()), // SAFETY: ctx is the live reader raw pointer passed above. - Some(|ctx: *mut c_void| Self::done(ctx.cast::())), + Some(|ctx: *mut c_void| Self::done(ctx.cast::())), ); } } @@ -463,12 +503,15 @@ impl PosixBufferedReader { // SAFETY: caller contract; borrows end at each `;`. unsafe { if !matches!((*this).handle, PollOrFd::Closed) - && (*this).flags.contains(PosixFlags::CLOSE_HANDLE) + && (*this).flags.contains(ReaderFlags::CLOSE_HANDLE) { Self::close_handle(this); return; - } else if (*this).flags.contains(PosixFlags::CLOSED_WITHOUT_REPORTING) { - (*this).flags.remove(PosixFlags::CLOSED_WITHOUT_REPORTING); + } else if (*this) + .flags + .contains(ReaderFlags::CLOSED_WITHOUT_REPORTING) + { + (*this).flags.remove(ReaderFlags::CLOSED_WITHOUT_REPORTING); } (*this).finish(); } @@ -514,7 +557,7 @@ impl PosixBufferedReader { fn try_register_poll(&mut self) -> Result<(), sys::Error> { // pause() may land from inside on_read_chunk's JS re-entry while the // loop's own re-arm is still ahead on the stack. - if self.flags.contains(PosixFlags::IS_PAUSED) { + if self.flags.contains(ReaderFlags::IS_PAUSED) { return Ok(()); } // Hoist vtable-derived scalars and @@ -525,7 +568,7 @@ impl PosixBufferedReader { let owner_ptr = std::ptr::from_mut(self).cast::(); if let PollOrFd::Fd(fd) = self.handle { - if !self.flags.contains(PosixFlags::POLLABLE) { + if !self.flags.contains(ReaderFlags::POLLABLE) { return Ok(()); } self.handle = PollOrFd::Poll(FilePollRef::init( @@ -540,7 +583,7 @@ impl PosixBufferedReader { poll.set_owner(Owner::new(PollTag::BufferedReader, owner_ptr.cast())); // Re-applied on every arm: `pause()` unregisters, which drops it. - if self.flags.contains(PosixFlags::KEEP_ALIVE) { + if self.flags.contains(ReaderFlags::KEEP_ALIVE) { poll.enable_keeping_process_alive(ev); } @@ -553,17 +596,17 @@ impl PosixBufferedReader { pub fn start(&mut self, fd: Fd, is_pollable: bool) -> sys::Result<()> { if !is_pollable { self.buffer().clear(); - self.flags.remove(PosixFlags::IS_DONE); + self.flags.remove(ReaderFlags::IS_DONE); self.handle.close(None, None::); self.handle = PollOrFd::Fd(fd); return sys::Result::Ok(()); } - self.flags.insert(PosixFlags::POLLABLE); + self.flags.insert(ReaderFlags::POLLABLE); if self.get_fd() != fd { self.handle = PollOrFd::Fd(fd); } // With nothing left to read the fd is never waited on: like a non-pollable source, the parent's first read request ends the reader. - if !self.flags.contains(PosixFlags::IS_PAUSED) && !self.limit.reached() { + if !self.flags.contains(ReaderFlags::IS_PAUSED) && !self.limit.reached() { // SAFETY: `self` is live. Note: this `&mut self` receiver still carries // a protector across the (maybe-freeing) error dispatch — pre-existing // on the parent chain, tracked with the raw-dispatch follow-up. @@ -573,18 +616,12 @@ impl PosixBufferedReader { sys::Result::Ok(()) } - pub fn start_file_offset(&mut self, fd: Fd, poll: bool, offset: usize) -> sys::Result<()> { - self._offset = offset; - self.flags.insert(PosixFlags::USE_PREAD); - self.start(fd, poll) - } - - /// Ends the reader after the next `len` bytes of the source as if they were followed by EOF (`ReadLimit`); `None` reads to EOF. Set before starting. - pub fn set_limit(&mut self, len: Option) { - self.limit = ReadLimit(len); + /// Whether `buffer()` has to be left alone because a read is on its way + /// into it. + pub fn buffer_is_awaiting_read(&self) -> bool { + self.has_pending_read() } - // Exists for consistently with Windows. pub fn has_pending_read(&self) -> bool { // `is_watching()` (registered && !needs-rearm) rather than // `is_registered()`: a one-shot poll that has fired but not been @@ -594,7 +631,7 @@ impl PosixBufferedReader { } pub fn watch(&mut self) { - if self.flags.contains(PosixFlags::POLLABLE) + if self.flags.contains(ReaderFlags::POLLABLE) && !matches!(&self.handle, PollOrFd::Poll(poll) if poll.is_watching()) { // SAFETY: `self` is live. Note: this `&mut self` receiver still carries @@ -646,7 +683,7 @@ impl PosixBufferedReader { /// # Safety /// `this` is the live reader registered as the poll's user data; see /// [`Self::read`] for why the entry is raw. - pub unsafe fn on_poll(this: *mut PosixBufferedReader, size_hint: isize, received_hup: bool) { + pub unsafe fn on_poll(this: *mut BufferedReader, size_hint: isize, received_hup: bool) { // SAFETY: caller contract — `this` is live; borrows end at each `;`. let Some((fd, file_type, vtable)) = (unsafe { (*this).begin_read() }) else { return; @@ -660,25 +697,17 @@ impl PosixBufferedReader { fn begin_read(&self) -> Option<(Fd, FileType, BufferedReaderVTable)> { if self .flags - .intersects(PosixFlags::IS_PAUSED | PosixFlags::READ_FAILED) + .intersects(ReaderFlags::IS_PAUSED | ReaderFlags::READ_FAILED) { return None; } Some((self.get_fd(), self.get_file_type(), self.vtable)) } - /// Charges `bytes_read` against the `maxBuffer` budget; `true` once it is gone. The overflow callback only kills the child, so the caller must also stop reading. - fn charge_max_buffer(&mut self, bytes_read: usize) -> bool { - let Some(maxbuf) = self.maxbuf else { - return false; - }; - MaxBuf::on_read_bytes(maxbuf, bytes_read as u64) - } - /// Every kind uses its non-blocking primitive: `RWF_NOWAIT`/poll-guarded reads for pipes, `MSG_DONTWAIT` for sockets; regular files cannot block. fn sys_read(&self, file_type: FileType, fd: Fd, buf: &mut [u8]) -> sys::Result { match file_type { - FileType::File if self.flags.contains(PosixFlags::USE_PREAD) => { + FileType::File if self.flags.contains(ReaderFlags::USE_PREAD) => { sys::pread(fd, buf, i64::try_from(self._offset).expect("int cast")) } FileType::File => sys::read(fd, buf), @@ -727,7 +756,7 @@ impl PosixBufferedReader { } sys::Result::Err(err) if err.is_retry() => ReadOnce::Stop(Stop::WouldBlock), sys::Result::Err(err) => { - self.flags.insert(PosixFlags::READ_FAILED); + self.flags.insert(ReaderFlags::READ_FAILED); ReadOnce::Stop(Stop::Error(err)) } } @@ -787,7 +816,7 @@ impl PosixBufferedReader { /// dispatch below. `on_error()` / `done()` MAY free the parent, so both /// are dispatched in tail position. unsafe fn read_loop( - this: *mut PosixBufferedReader, + this: *mut BufferedReader, file_type: FileType, fd: Fd, mut received_hup: bool, @@ -857,7 +886,7 @@ impl PosixBufferedReader { Some(Stop::Eof | Stop::OverBudget) => { // SAFETY: caller contract; `done()` is the tail. unsafe { - if !(*this).flags.contains(PosixFlags::IS_DONE) { + if !(*this).flags.contains(ReaderFlags::IS_DONE) { Self::done(this); } } @@ -972,7 +1001,7 @@ impl PosixBufferedReader { // SAFETY: caller contract; `done()` may free the parent, nothing of `*this` is touched after. unsafe { (*this).close_without_reporting(); - if !(*this).flags.contains(PosixFlags::IS_DONE) { + if !(*this).flags.contains(ReaderFlags::IS_DONE) { Self::done(this); } } @@ -994,6 +1023,7 @@ impl PosixBufferedReader { } } +#[cfg(unix)] enum Stop { Eof, OverBudget, @@ -1001,108 +1031,33 @@ enum Stop { Error(sys::Error), } +#[cfg(unix)] enum ReadOnce { Read(usize, Option), Stop(Stop), } -impl Drop for PosixBufferedReader { +impl Drop for BufferedReader { fn drop(&mut self) { MaxBuf::remove_from_pipereader(&mut self.maxbuf); + #[cfg(unix)] self.close_without_reporting(); } } -// ────────────────────────────────────────────────────────────────────────── -// WindowsBufferedReader -// ────────────────────────────────────────────────────────────────────────── - -#[cfg(windows)] -pub struct WindowsBufferedReader { - /// The pointer to this pipe must be stable. - /// It cannot change because we don't know what libuv will do with it. - pub source: Option, - pub(crate) _offset: usize, - limit: ReadLimit, - pub _buffer: Vec, - // for compatibility with Linux - pub flags: WindowsFlags, - pub maxbuf: Option>, - - pub(crate) vtable: BufferedReaderVTable, -} - -bitflags::bitflags! { - #[derive(Clone, Copy, Default)] - pub struct WindowsFlags: u16 { - const IS_DONE = 1 << 0; - const POLLABLE = 1 << 1; - const NONBLOCKING = 1 << 2; - const RECEIVED_EOF = 1 << 3; - const CLOSED_WITHOUT_REPORTING = 1 << 4; - const CLOSE_HANDLE = 1 << 5; // default true - const IS_PAUSED = 1 << 6; // default true - const HAS_INFLIGHT_READ = 1 << 7; - const USE_PREAD = 1 << 8; - /// When true, wait for the file operation callback before calling done(). - /// Used to ensure proper cleanup ordering when closing during cancellation. - const DEFER_DONE_CALLBACK = 1 << 9; - } -} - #[cfg(windows)] -impl WindowsFlags { - pub(crate) const fn new() -> Self { - Self::from_bits_truncate(WindowsFlags::CLOSE_HANDLE.bits() | WindowsFlags::IS_PAUSED.bits()) - } -} - -#[cfg(windows)] -impl WindowsBufferedReader { - pub fn memory_cost(&self) -> usize { - mem::size_of::() + self._buffer.capacity() - } - - pub fn init() -> WindowsBufferedReader { - WindowsBufferedReader { - source: None, - _offset: 0, - limit: ReadLimit::NONE, - _buffer: Vec::new(), - flags: WindowsFlags::new(), - maxbuf: None, - vtable: BufferedReaderVTable::init::(), - } - } - - #[inline] - pub fn is_done(&self) -> bool { - self.flags.intersects( - WindowsFlags::IS_DONE - | WindowsFlags::RECEIVED_EOF - | WindowsFlags::CLOSED_WITHOUT_REPORTING, - ) - } - - pub fn from(&mut self, other: &mut WindowsBufferedReader, parent: *mut c_void) { +impl BufferedReader { + pub fn from(&mut self, other: &mut BufferedReader, parent: *mut c_void) { debug_assert!(other.source.is_some() && self.source.is_none()); - // Keep self.vtable; move other's state in. self.flags = other.flags; self._buffer = mem::take(other.buffer()); self._offset = other._offset; self.limit = other.limit; - // Ownership of the handle (or listed file) moves with the source; - // `set_parent` below re-records this reader as the one a VM teardown - // stops it through. self.source = other.source.take(); - other.flags.insert(WindowsFlags::IS_DONE); + other.flags.insert(ReaderFlags::IS_DONE); other._offset = 0; other.limit = ReadLimit::NONE; - // other._buffer / other.source already cleared by mem::take above. - // The field-by-field assigns above leave `self.maxbuf` untouched, so - // drop any prior owner-count first to avoid leaking a MaxBuf ref when - // the destination already held one. MaxBuf::remove_from_pipereader(&mut self.maxbuf); MaxBuf::transfer_to_pipereader(&mut other.maxbuf, &mut self.maxbuf); self.set_parent(parent); @@ -1116,23 +1071,32 @@ impl WindowsBufferedReader { } pub fn watch(&mut self) { - // No-op on windows. + // Reads are re-issued as they complete. } + /// The source reports to this reader by address, so a reader that moved + /// (or took over another reader's source) registers itself again. pub fn set_parent(&mut self, parent: *mut c_void) { self.vtable.parent = parent; - if !self.flags.contains(WindowsFlags::IS_DONE) { - // `Source::set_data` only writes the libuv `.data` field (raw ptr - // store); take a raw self-pointer first to dodge the - // immutable-then-mutable-borrow conflict. - let self_ptr = core::ptr::from_mut(self).cast::(); - if let Some(source) = self.source.as_mut() { - source.set_owner(self_ptr, Self::stop_for_vm_teardown); + if self.flags.contains(ReaderFlags::IS_DONE) { + return; + } + let this: *mut Self = self; + match self.source.as_mut() { + Some(Source::Pipe(pipe)) if pipe.is_reading() => { + let _ = pipe.read_start(this, Self::on_source_read); } + Some(Source::Tty(tty)) if tty.is_reading() => { + let _ = tty.read_start(this, Self::on_source_read); + } + Some(Source::File(file)) => file.set_reader(this, Self::on_source_read), + _ => {} } } pub fn update_ref(&mut self, value: bool) { + // Remembered so a source opened later honours an unref() that came first. + self.flags.set(ReaderFlags::KEEP_ALIVE, value); if let Some(source) = self.source.as_mut() { if value { source.ref_(); @@ -1142,18 +1106,6 @@ impl WindowsBufferedReader { } } - pub fn disable_keeping_process_alive(&mut self, _: C) { - self.update_ref(false); - } - - pub fn take_buffer(&mut self) -> Vec { - mem::take(&mut self._buffer) - } - - pub fn buffer(&mut self) -> &mut Vec { - &mut self._buffer - } - pub fn final_buffer(&mut self) -> &mut Vec { self.buffer() } @@ -1165,88 +1117,89 @@ impl WindowsBufferedReader { source.is_active() } + /// A read was asked for and its result has not been reported yet. pub fn has_pending_read(&self) -> bool { - if self.flags.contains(WindowsFlags::HAS_INFLIGHT_READ) { - return true; + self.source.as_ref().is_some_and(Source::is_reading) + } + + /// Whether `buffer()` has to be left alone because a read is on its way + /// into it. Never: the allocation a read lands in is the source's until + /// the read is over, and `buffer()` takes back what was lent of it. + pub fn buffer_is_awaiting_read(&self) -> bool { + false + } + + /// Lend `_buffer` to the pipe so the next chunk lands after what it holds. + fn lend_buffer(&mut self) { + if self._buffer.is_empty() || self.vtable.is_streaming_enabled() { + return; } - let Some(source) = &self.source else { - return false; + let Some(Source::Pipe(pipe)) = self.source.as_mut() else { + return; }; - match source { - Source::File(file) | Source::SyncFile(file) => { - file.state != crate::source::FileState::Deinitialized - } - _ => false, + let len = self._buffer.len(); + match pipe.lend_read_buffer(mem::take(&mut self._buffer)) { + Ok(()) => self.lent = len, + Err(buffer) => self._buffer = buffer, } } - /// Charges `bytes_read` against the `maxBuffer` budget, returning `true` - /// once it is gone. The overflow callback only kills the child, which takes - /// effect asynchronously, so the caller must also close the handle. - fn charge_max_buffer(&mut self, bytes_read: usize) -> bool { - let Some(maxbuf) = self.maxbuf else { - return false; - }; - MaxBuf::on_read_bytes(maxbuf, bytes_read as u64) + /// Undo `lend_buffer`. + fn recall_buffer(&mut self) { + if mem::take(&mut self.lent) == 0 { + return; + } + if let Some(Source::Pipe(pipe)) = self.source.as_mut() { + debug_assert!(self._buffer.is_empty()); + self._buffer = pipe.take_read_buffer(); + } } - fn on_read_chunk(&mut self, has_more: ReadState) -> bool { + /// Dispatches what is in `_buffer`. + fn on_read_chunk(&mut self, has_more: ReadState) { if has_more == ReadState::Eof { - self.flags.insert(WindowsFlags::RECEIVED_EOF); + self.flags.insert(ReaderFlags::RECEIVED_EOF); } - - // The read that produced this chunk is complete; if the flag is set again - // below, a `start_reading` issued from the dispatch set it. - self.flags.remove(WindowsFlags::HAS_INFLIGHT_READ); - if !self.vtable.is_streaming_enabled() { - return true; + return; } // `on_read_chunk` re-enters JS, which can reach this reader through its parent; go raw across the dispatch so nothing of `self` is cached over it. let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self)); // SAFETY: `this` aliases the live `&mut self`; the reader is an inline field of its parent (never freed mid-call). Borrows end at each `;`. let (vtable, mut buffer) = unsafe { ((*this).vtable, mem::take(&mut (*this)._buffer)) }; - let result = if buffer.is_empty() { - true - } else if has_more == ReadState::Eof { - vtable.on_read_chunk(Chunk::Owned(buffer), has_more) - } else { - let result = vtable.on_read_chunk(Chunk::Buffer(&mut buffer), has_more); + // Parents that want the reader paused call `reader().pause()` themselves: what they return is for the POSIX read loop. + if has_more == ReadState::Eof { + if !buffer.is_empty() { + vtable.on_read_chunk(Chunk::Owned(buffer), has_more); + } + } else if !buffer.is_empty() { + vtable.on_read_chunk(Chunk::Buffer(&mut buffer), has_more); buffer.clear(); // SAFETY: `this` is still live (see above). unsafe { - // An in-flight read owns `_buffer`: libuv holds a pointer into it (#39890). - if !(*this).flags.contains(WindowsFlags::HAS_INFLIGHT_READ) - && (*this)._buffer.is_empty() - { + if (*this)._buffer.is_empty() { (*this)._buffer = buffer; } } - result - }; + } core::hint::black_box(this); - result } fn finish(&mut self) { - self.flags.remove(WindowsFlags::HAS_INFLIGHT_READ); - self.flags.insert(WindowsFlags::IS_DONE); + self.recall_buffer(); + self.flags.insert(ReaderFlags::IS_DONE); self._buffer.shrink_to_fit(); } fn done(&mut self) { - if let Some(source) = &self.source { - debug_assert!(source.is_closed()); - } - + debug_assert!(self.source.is_none()); self.finish(); - self.vtable.on_reader_done(); } /// # Safety - /// `this` is live; raw for parity with the POSIX entry so the - /// (maybe-freeing) error dispatch runs under no receiver protector. + /// `this` is live. `on_reader_error` may free it, so it runs with no + /// borrow of `*this` held. pub unsafe fn on_error(this: *mut Self, err: sys::Error) { // SAFETY: caller contract; `finish`'s receiver borrow ends when it // returns, and the (Copy) vtable is copied out before the dispatch. @@ -1257,688 +1210,281 @@ impl WindowsBufferedReader { vtable.on_reader_error(err); } - fn get_read_buffer_with_stable_memory_address(&mut self, suggested_size: usize) -> &mut [u8] { - self.flags.insert(WindowsFlags::HAS_INFLIGHT_READ); - // Spare capacity grows well past `suggested_size`, so an unclamped read - // overshoots the limit or `maxBuffer` by however much the buffer had room for. - let maxbuf = self.maxbuf; - let limit = self.limit; - // Never empty: reads are only issued while the limit has not been reached (`start_reading`, `on_read`), and libuv treats an empty buffer as an error. - debug_assert!(!limit.reached()); - let size = limit.clamp_len(suggested_size); - // Tty reads must not target `_buffer`: libuv can retain the pointer - // past reader teardown (see `uv::Tty::read_scratch` for the contract). - if matches!(self.source, Some(Source::Tty(_))) { - let scratch = self - .source - .as_mut() - .and_then(|s| s.tty_read_scratch(size)) - .expect("tty source matched above"); - return MaxBuf::clamp_read_buf(maxbuf, limit.clamp(scratch)); - } - self._buffer.reserve(size); - // SAFETY: returning spare capacity for libuv to write into; len updated in on_read. - let buf = unsafe { bun_core::vec::spare_bytes_mut(&mut self._buffer) }; - MaxBuf::clamp_read_buf(maxbuf, limit.clamp(buf)) - } - - pub fn start_with_current_pipe(&mut self) -> sys::Result<()> { - debug_assert!(!self.source.as_ref().unwrap().is_closed()); - let self_ptr = core::ptr::from_mut(self).cast::(); - self.source - .as_mut() - .unwrap() - .set_owner(self_ptr, Self::stop_for_vm_teardown); - self.buffer().clear(); - self.flags.remove(WindowsFlags::IS_DONE); - // Debug-only fault injection for test/js/bun/spawn/spawn-pipe-start-error.test.ts: - // a real uv_read_start failure on a freshly-spawned stdio pipe cannot be - // triggered from JS, so the test exercises the consumer's error path this way. - #[cfg(debug_assertions)] - if bun_core::env_var::feature_flag::BUN_INTERNAL_FAIL_PIPE_READER_START.get() == Some(true) - { - return sys::Result::Err(sys::Error::from_code(sys::E::INVAL, sys::Tag::open)); - } - self.start_reading() - } - - /// SAFETY: `pipe` must be a `Box`-allocated pointer; ownership - /// transfers to `self.source` (later freed via `close_and_destroy`). - pub unsafe fn start_with_pipe(&mut self, pipe: *mut uv::Pipe) -> sys::Result<()> { - // SAFETY: caller contract — Box-allocated, ownership transfers. - self.set_source(Source::Pipe(unsafe { bun_core::heap::take(pipe) })); - self.start_with_current_pipe() - } - - /// Take ownership of `source` (reading starts later, or never). For a pipe - /// or tty this reader is from now on the one a VM teardown stops the handle - /// through — whether or not it ever starts reading — so nothing else may - /// close that handle while it sits here. - pub fn set_source(&mut self, source: Source) { - debug_assert!(self.source.is_none()); - self.source = Some(source); - let self_ptr = core::ptr::from_mut(self).cast::(); - if let Some(source) = self.source.as_mut() { - // A read over a file is a uv request with no handle to close: list - // the boxed File so a thread teardown closes this reader (`close()` - // below) before draining the loop. Unlisted where the box leaves - // this reader (`close_impl`, `Drop`). - if let Some(file) = source.file_key() { - uv::open_handles::add_file(file); - } - source.set_owner(self_ptr, Self::stop_for_vm_teardown); - } + /// Bytes the next read may produce without overshooting the limit or the + /// `maxBuffer` budget by more than the reader tolerates. + fn next_read_len(&self) -> usize { + MaxBuf::clamp_read_len( + self.maxbuf, + self.limit + .clamp_len(crate::pipe_read_scratch::PIPE_READ_BUFFER_SIZE), + ) + .max(1) } - /// `uv::open_handles` closes this reader's stream through here at teardown. - unsafe fn stop_for_vm_teardown(this: *mut c_void) { - // SAFETY: recorded via `Source::set_owner` by this live reader; the slot - // is replaced/dropped before the reader goes away (close_impl / from / Drop). - unsafe { (*this.cast::()).close() }; + /// See [`PipeOrigin::from_is_pollable`] for `is_pollable`. On `Err` the + /// reader holds nothing; `fd` is still the caller's to close. + pub fn start(&mut self, fd: Fd, is_pollable: bool) -> sys::Result<()> { + let source = self.open_source(fd, PipeOrigin::from_is_pollable(is_pollable))?; + self.start_with_source(source) } - pub fn start(&mut self, fd: Fd, _: bool) -> sys::Result<()> { + /// The first half of [`start`](Self::start), for + /// [`start_with_source`](Self::start_with_source). Once this has opened a + /// [`PipeOrigin::Created`] `fd`, the loop's port has its file object, + /// whatever becomes of the source. + pub fn open_source(&mut self, fd: Fd, origin: PipeOrigin) -> sys::Result { debug_assert!(self.source.is_none()); - // Use the event loop from the parent, not the global one - // This is critical for spawnSync to use its isolated loop + // The parent's loop, not the thread's: `spawnSync` reads on its own. let loop_ = self.vtable.loop_(); - let source = match Source::open(loop_.cast(), fd) { - sys::Result::Err(err) => return sys::Result::Err(err), - sys::Result::Ok(source) => source, - }; - self.set_source(source); - self.start_with_current_pipe() - } - - pub fn start_file_offset(&mut self, fd: Fd, poll: bool, offset: usize) -> sys::Result<()> { - self._offset = offset; - self.flags.insert(WindowsFlags::USE_PREAD); - self.start(fd, poll) - } - - /// See `PosixBufferedReader::set_limit`. - pub fn set_limit(&mut self, len: Option) { - self.limit = ReadLimit(len); - } - - pub fn set_raw_mode(&mut self, value: bool) -> sys::Result<()> { - let Some(source) = self.source.as_mut() else { - return sys::Result::Err(sys::Error { - errno: sys::E::BADF as _, - syscall: sys::Tag::uv_tty_set_mode, - ..Default::default() - }); - }; - source.set_raw_mode(value) + let close_fd = self.flags.contains(ReaderFlags::CLOSE_HANDLE); + let source = Source::open(loop_, fd, origin, close_fd)?; + self.flags.set( + ReaderFlags::POLLABLE, + matches!(origin, PipeOrigin::Created | PipeOrigin::Associated), + ); + Ok(source) } - extern "C" fn on_stream_alloc( - handle: *mut uv::Handle, - suggested_size: usize, - buf: *mut uv::uv_buf_t, - ) { - // SAFETY: libuv alloc_cb — `handle.data` was set to `*mut Self` in - // `set_data`/`start_with_current_pipe`. libuv invokes this from the - // event loop with no other Rust borrow of the reader live, so this is - // the sole `&mut` to the allocation (single-owner). - let this = unsafe { bun_ptr::callback_ctx::((*handle).data) }; - let result = this.get_read_buffer_with_stable_memory_address(suggested_size); - // SAFETY: buf is a valid out-pointer from libuv. - unsafe { - *buf = uv::uv_buf_t::init(result); + /// Read from what [`open_source`](Self::open_source) opened. + pub fn start_with_source(&mut self, source: Source) -> sys::Result<()> { + debug_assert!(self.source.is_none()); + if !self.flags.contains(ReaderFlags::KEEP_ALIVE) { + source.unref(); } - } - - extern "C" fn on_stream_read( - stream: *mut uv::uv_stream_t, - nread: uv::ReturnCodeI64, - buf: *const uv::uv_buf_t, - ) { - // SAFETY: libuv read_cb — `stream.data` was set to `*mut Self` in - // `set_data`. Invoked from the event loop with no other Rust borrow of - // the reader live (single-owner). - let this = unsafe { bun_ptr::callback_ctx::((*stream).data) }; - let _parent = this.vtable.ref_parent(); - - let nread_int = nread.int(); - - bun_sys::syslog!( - "onStreamRead(0x{}) = {}", - core::ptr::from_mut(this) as usize, - nread_int - ); - - // NOTE: pipes/tty need to call stopReading on errors (yeah) - match nread_int { - 0 => { - // EAGAIN or EWOULDBLOCK or canceled (buf is not safe to access here) - // With libuv 1.51.0+, calling onRead(.drained) here causes a race condition - // where subsequent reads return truncated data (see logs showing 6024 instead - // of 74468 bytes). Just ignore 0-byte reads and let libuv continue. - return; - } - v if v == uv::UV_EOF as i64 => { - let _ = this.stop_reading(); - // EOF (buf is not safe to access here) - return this.on_read(sys::Result::Ok(0), &mut [], ReadState::Eof); - } - _ => { - if let Some(err) = nread.to_error(sys::Tag::recv) { - let _ = this.stop_reading(); - // ERROR (buf is not safe to access here) - this.on_read(sys::Result::Err(err), &mut [], ReadState::Progress); - return; - } - // we got some data we can slice the buffer! - let len: usize = usize::try_from(nread_int).expect("int cast"); - // SAFETY: buf is valid when nread > 0. `uv_buf_t` is `Copy` — - // take a local copy so `slice_mut` can borrow `&mut self` - // (libuv's `read_cb` hands us `*const`). - let mut b = unsafe { *buf }; - let slice = unsafe { b.slice_mut() }; - let data = &mut slice[..len]; - if matches!(this.source, Some(Source::Tty(_))) { - // Tty chunks arrive in the tty-owned scratch; stage them - // into `_buffer` so `on_read` commits them like a pipe chunk. - this._buffer.reserve(len); - // SAFETY: `_buffer` has `len` spare bytes, disjoint from - // `this` and the scratch. - let staged = unsafe { - let dst = bun_core::vec::spare_bytes_mut(&mut this._buffer).as_mut_ptr(); - core::ptr::copy_nonoverlapping(data.as_ptr(), dst, len); - core::slice::from_raw_parts_mut(dst, len) - }; - this.on_read(sys::Result::Ok(len), staged, ReadState::Progress); - } else { - this.on_read(sys::Result::Ok(len), data, ReadState::Progress); - } - } + let mut source = source; + if let Source::File(file) = &mut source + && self.flags.contains(ReaderFlags::SHARES_POSITION) + { + file.shares_position(); } - } - - /// Callback fired when a file read operation completes or is canceled. - /// Handles cleanup, cancellation, and normal read processing. - extern "C" fn on_file_read(fs: *mut uv::fs_t) { - // SAFETY: libuv fs_cb — `fs` is the `uv_fs_t` field of a heap-boxed - // `source::File` (separate allocation from `Self`). Invoked from the - // event loop with no other Rust borrow of it live (single-owner). - // `from_fs_callback` snapshots `result`/`data` then container_of's the - // owning `&mut File`; that borrow does not overlap the later - // `&mut WindowsBufferedReader` (distinct heap allocations). - let (file, result, parent_ptr) = unsafe { crate::source::File::from_fs_callback(fs) }; - let nread_int = result.int(); - let was_canceled = nread_int == uv::UV_ECANCELED as i64; - - bun_sys::syslog!( - "onFileRead({}) = {}", - // SAFETY: `uv_fs_read` populated the `fd` arm of the `file` union. - Fd::from_uv(unsafe { file.fs.file_fd() }), - nread_int - ); - - // ALWAYS complete the read first (cleans up fs_t, updates state) - file.complete(was_canceled); - - if parent_ptr.is_null() { - if file.state != crate::source::FileState::Closing { - // detach_borrowed_fd path: no close scheduled, so reclaim here. - // SAFETY: sole &mut to the into_raw'd Box; no fs callback left. - drop(unsafe { bun_core::heap::take(core::ptr::from_mut(file)) }); - } - // else: detach() set close_after_operation; on_close_complete frees. - return; + self.source = Some(source); + self.buffer().clear(); + // What the source before this one ended with says nothing about this one. + self.flags + .remove(ReaderFlags::IS_DONE | ReaderFlags::RECEIVED_EOF); + // Debug-only fault injection for test/js/bun/spawn/spawn-pipe-start-error.test.ts: + // a real failure to start reading a freshly-spawned stdio pipe cannot be + // triggered from JS, so the test exercises the consumer's error path this way. + #[cfg(debug_assertions)] + if bun_core::env_var::feature_flag::BUN_INTERNAL_FAIL_PIPE_READER_START.get() == Some(true) + { + self.release_source(); + return sys::Result::Err(sys::Error::from_code(sys::E::EINVAL, sys::Tag::open)); } - - // SAFETY: `parent_ptr` (= `fs.data`) is `*mut Self` set via `set_data`. - // `file` above points into the boxed `source::File` — a separate heap - // allocation — and its borrow ends (NLL) before this point in the - // non-null path, so this is the sole live `&mut` to the reader - // (single-owner). - let this: &mut WindowsBufferedReader = - unsafe { bun_ptr::callback_ctx::(parent_ptr) }; - let _parent = this.vtable.ref_parent(); - - // Mark no longer in flight - this.flags.remove(WindowsFlags::HAS_INFLIGHT_READ); - - // Cancelled, or `close()` was asked for while this read was out (the - // cancel need not have won): finish the close, deliver nothing. - if was_canceled || this.flags.contains(WindowsFlags::DEFER_DONE_CALLBACK) { - if this.flags.contains(WindowsFlags::DEFER_DONE_CALLBACK) { - this.flags.remove(WindowsFlags::DEFER_DONE_CALLBACK); - // Now safe to call done - buffer will be freed by deinit - this.close_impl::(); - } else { - this.buffer().clear(); - } - return; + // With nothing left to read the source is never read: like POSIX, the parent's first read request ends the reader. + if self.flags.contains(ReaderFlags::IS_PAUSED) || self.limit.reached() { + return sys::Result::Ok(()); } - - if this.flags.contains(WindowsFlags::IS_DONE) { - return; + let result = self.start_reading(); + if result.is_err() { + self.release_source(); } + result + } - match nread_int { - // 0 actually means EOF too - v if v == 0 || v == uv::UV_EOF as i64 => { - this.flags.insert(WindowsFlags::IS_PAUSED); - this.on_read(sys::Result::Ok(0), &mut [], ReadState::Eof); - } - // UV_ECANCELED needs to be on the top so we avoid UAF - v if v == uv::UV_ECANCELED as i64 => unreachable!(), - _ => { - if let Some(err) = result.to_error(sys::Tag::read) { - this.flags.insert(WindowsFlags::IS_PAUSED); - this.on_read(sys::Result::Err(err), &mut [], ReadState::Progress); - return; - } - - let len: usize = usize::try_from(nread_int).expect("int cast"); - this._offset += len; - // we got some data lets get the current iov - // - // BORROW_PARAM (raw-ptr break): `on_read` takes `&mut self` - // *and* a slice borrowed from `self.source.File.iov`; under - // Stacked Borrows that's a self-mut + field-shared conflict. - // The boxed `File` lives in its own heap allocation, so a - // `*mut File` snapshot is provenance-disjoint from `&mut self`. - let file_raw: *mut crate::source::File = match this.source.as_mut() { - Some(Source::File(f)) => f.as_mut() as *mut _, - _ => core::ptr::null_mut(), - }; - if !file_raw.is_null() { - // SAFETY: `file_raw` points into the boxed File owned by - // `this.source`; live for the duration of this callback. - let buf = unsafe { (*file_raw).iov.slice_mut() }; - this.on_read(sys::Result::Ok(len), &mut buf[..len], ReadState::Progress); - } else { - // ops we should not hit this lets fail with EPIPE - debug_assert!(false); - this.on_read( - sys::Result::Err(sys::Error::from_code(sys::E::PIPE, sys::Tag::read)), - &mut [], - ReadState::Progress, - ); - } - - // Shared epilogue: both body paths above fall through here. - // if we are not paused we keep reading until EOF or err - if !this.flags.contains(WindowsFlags::IS_PAUSED) { - // Re-snapshot — `on_read` may have mutated `this.source`. - let this_ptr = core::ptr::from_mut(this).cast::(); - let file_raw: *mut crate::source::File = match this.source.as_mut() { - Some(Source::File(f)) => f.as_mut() as *mut _, - _ => core::ptr::null_mut(), - }; - if !file_raw.is_null() { - // SAFETY (each access below): see the snapshot above — - // `file_raw` points into the boxed `File`, a heap - // allocation disjoint from `*this`, so the scoped - // borrows never overlap the `this` accesses interleaved - // here. - // Can only start if file is in deinitialized state - if unsafe { (*file_raw).can_start() } { - // SAFETY: see above. - unsafe { (*file_raw).fs.data = this_ptr }; - // SAFETY: see above. - unsafe { (*file_raw).prepare() }; - let buf = this.get_read_buffer_with_stable_memory_address(64 * 1024); - // SAFETY: see above. - unsafe { (*file_raw).iov = uv::uv_buf_t::init(buf) }; - this.flags.insert(WindowsFlags::HAS_INFLIGHT_READ); - - let offset = if this.flags.contains(WindowsFlags::USE_PREAD) { - i64::try_from(this._offset).expect("int cast") - } else { - -1 - }; - // SAFETY: the file is fully initialized; libuv - // stores the cb and fires it on the event loop. - if let Some(err) = unsafe { - uv::uv_fs_read( - this.vtable.loop_().cast(), - &mut (*file_raw).fs, - (*file_raw).file, - &(*file_raw).iov, - 1, - offset, - Some(Self::on_file_read), - ) - } - // Tagged `.write` even though the syscall is - // `uv_fs_read`, so user-visible `error.syscall` - // stays bit-identical with previous releases. - .to_error(sys::Tag::write) - { - // SAFETY: see above. - unsafe { (*file_raw).complete(false) }; - this.flags.remove(WindowsFlags::HAS_INFLIGHT_READ); - this.flags.insert(WindowsFlags::IS_PAUSED); - // we should inform the error if we are unable to keep reading - this.on_read(sys::Result::Err(err), &mut [], ReadState::Progress); - } - } - } - } - } + /// Let go of a source that failed to start; its fd stays the caller's. + fn release_source(&mut self) { + if let Some(mut source) = self.source.take() { + source.disown(); } } fn start_reading(&mut self) -> sys::Result<()> { - // A used-up limit stays paused: `start` has nothing to read and `unpause` reports it as EOF instead. - if self.flags.contains(WindowsFlags::IS_DONE) - || !self.flags.contains(WindowsFlags::IS_PAUSED) - || self.limit.reached() - { + if self.flags.contains(ReaderFlags::IS_DONE) || self.limit.reached() { return sys::Result::Ok(()); } - self.flags.remove(WindowsFlags::IS_PAUSED); - // BORROW_PARAM (raw-ptr break): the body needs `&mut self` (for - // `get_read_buffer_…`/`flags`) while also holding `&mut File` borrowed - // out of `self.source`. The boxed `File` is its own heap allocation, so - // a `*mut File` snapshot is provenance-disjoint from `&mut self`. - let self_ptr = self as *mut Self as *mut c_void; - let Some(source) = self.source.as_mut() else { - return sys::Result::Err(sys::Error::from_code(sys::E::BADF, sys::Tag::read)); - }; - debug_assert!(!source.is_closed()); - - match source { - Source::File(file) | Source::SyncFile(file) => { - let file_raw: *mut crate::source::File = file.as_mut(); - // SAFETY (each access below): `file_raw` points into the boxed - // File owned by `self.source` — a heap allocation disjoint - // from `*self` — and is live until `self.source` is replaced; - // the scoped borrows never overlap the `self` accesses - // interleaved here. - // If already reading, just set data and unpause - unsafe { (*file_raw).fs.data = self_ptr }; - // SAFETY: see above. - if !unsafe { (*file_raw).can_start() } { + let this: *mut Self = self; + let len = self.next_read_len(); + let offset = self + .flags + .contains(ReaderFlags::USE_PREAD) + .then_some(self._offset as u64); + match self.source.as_mut() { + None => sys::Result::Err(sys::Error::from_code(sys::E::EBADF, sys::Tag::read)), + Some(Source::Pipe(pipe)) => { + pipe.set_read_size(len); + pipe.read_start(this, Self::on_source_read) + } + Some(Source::Tty(tty)) => tty.read_start(this, Self::on_source_read), + Some(Source::File(file)) => { + if file.is_busy() { + // The read that was out when the reader paused; its result is still coming. + file.set_reader(this, Self::on_source_read); return sys::Result::Ok(()); } + file.read(len, offset, this, Self::on_source_read) + } + } + } - // Start new read - set data before prepare - // SAFETY: see above. - unsafe { (*file_raw).prepare() }; - let buf = self.get_read_buffer_with_stable_memory_address(64 * 1024); - // SAFETY: see above. - unsafe { (*file_raw).iov = uv::uv_buf_t::init(buf) }; - self.flags.insert(WindowsFlags::HAS_INFLIGHT_READ); - - let offset = if self.flags.contains(WindowsFlags::USE_PREAD) { - i64::try_from(self._offset).expect("int cast") - } else { - -1 - }; - // SAFETY: the file is fully initialized; libuv stores cb and - // fires it on the event loop. - if let Some(err) = unsafe { - uv::uv_fs_read( - self.vtable.loop_().cast(), - &mut (*file_raw).fs, - (*file_raw).file, - &(*file_raw).iov, - 1, - offset, - Some(Self::on_file_read), - ) - } - // Tagged `.write` even though the syscall is `uv_fs_read`, so - // user-visible `error.syscall` stays bit-identical with - // previous releases. - .to_error(sys::Tag::write) - { - // SAFETY: see above. - unsafe { (*file_raw).complete(false) }; - self.flags.remove(WindowsFlags::HAS_INFLIGHT_READ); - return sys::Result::Err(err); + /// # Safety + /// `this` is the reader registered with the source, an inline field of its + /// parent. Raw because the dispatches below re-enter JS, which can reach + /// the reader again through the parent. + unsafe fn on_source_read(this: *mut Self, event: ReadEvent<'_>) { + // SAFETY: caller contract — `this` is live; borrows end at each `;`. + // The parent must outlive the dispatches, which touch `*this` after. + let _parent = unsafe { (*this).vtable }.ref_parent(); + // SAFETY: as above, for every `(*this)` below. + let is_file = unsafe { matches!((*this).source, Some(Source::File(_))) }; + match event { + ReadEvent::Data(data) => { + // SAFETY: as above. + unsafe { + // `data` starts with what was lent, which is back with it. + let len = data.len() - mem::take(&mut (*this).lent); + if (*this)._buffer.is_empty() { + mem::swap(&mut (*this)._buffer, data); + } else { + (*this)._buffer.extend_from_slice(data); + } + if is_file { + (*this)._offset += len; + } + (*this).on_read(len, ReadState::Progress); } } - Source::Pipe(_) | Source::Tty(_) => { - // SAFETY: source is a live Pipe/Tty stream handle. - if let Some(err) = unsafe { - uv::uv_read_start( - source.to_stream(), - Some(Self::on_stream_alloc), - Some(Self::on_stream_read), - ) - } - .to_error(sys::Tag::open) - { - // Routed through `bun.windows.libuv.log` (the `uv` debug - // scope, toggled by `BUN_DEBUG_uv=1`), not `SYS`. - bun_sys::windows::libuv::log!( - "uv_read_start() = {}", - bstr::BStr::new(err.name()), - ); - return sys::Result::Err(err); + // SAFETY: as above. + ReadEvent::Eof | ReadEvent::EndOfWrite => unsafe { (*this).on_read(0, ReadState::Eof) }, + // A file read withdrawn by `pause()` before it ran; the reader may have resumed since. + ReadEvent::Err(err) if is_file && err.get_errno() == sys::E::ECANCELED => { + // SAFETY: as above; nothing of `*this` is touched after `on_error`. + unsafe { + if !(*this).flags.contains(ReaderFlags::IS_PAUSED) + && let sys::Result::Err(err) = (*this).start_reading() + { + Self::on_error(this, err); + } } } + ReadEvent::Err(err) => { + // SAFETY: live reader; the error dispatch may free the parent, nothing of `*this` is touched after. + unsafe { Self::on_error(this, err) }; + } } - - sys::Result::Ok(()) } - pub fn stop_reading(&mut self) -> sys::Result<()> { - if self.flags.contains(WindowsFlags::IS_DONE) - || self.flags.contains(WindowsFlags::IS_PAUSED) - { - return sys::Result::Ok(()); - } - self.flags.insert(WindowsFlags::IS_PAUSED); - let Some(source) = self.source.as_mut() else { - return sys::Result::Ok(()); + fn on_read(&mut self, amount: usize, has_more: ReadState) { + // Using the limit up is this reader's EOF (`ReadLimit`); so is exhausting `maxBuffer`. + let limit_reached = self.limit.charge(amount); + let over_budget = self.charge_max_buffer(amount); + let has_more = if limit_reached || over_budget { + ReadState::Eof + } else { + has_more }; - match source { - Source::File(file) | Source::SyncFile(file) => { - file.stop(); + // Stopping here on the parent's say-so could free a parent whose caller still holds `this` (FileResponseStream on abort). + self.on_read_chunk(has_more); + + if has_more == ReadState::Eof { + self.close(); + return; + } + if self.flags.contains(ReaderFlags::IS_PAUSED) || self.is_done() { + return; + } + // A pipe or console keeps delivering on its own; it only needs to know how much the next read may take. A file is read one request at a time. + let len = self.next_read_len(); + match self.source.as_mut() { + Some(Source::Pipe(pipe)) => { + pipe.set_read_size(len); + self.lend_buffer(); } - Source::Pipe(_) | Source::Tty(_) => { - // SAFETY: stream handle is live (just matched a stream source). - unsafe { uv::uv_read_stop(source.to_stream()) }; + Some(Source::File(_)) => { + if let sys::Result::Err(err) = self.start_reading() { + // SAFETY: live reader; nothing of `*self` is touched after. + unsafe { Self::on_error(core::ptr::from_mut(self), err) }; + } } + _ => {} } - sys::Result::Ok(()) } - pub fn close_impl(&mut self) { - if let Some(source) = self.source.take() { - match source { - Source::SyncFile(mut file) | Source::File(mut file) => { - uv::open_handles::remove_file(core::ptr::from_mut(&mut *file).cast()); - // Hand the Box off to libuv: detach() leaves either an - // in-flight uv_fs_read (on_file_read) or a scheduled - // uv_fs_close (on_close_complete) pending; the callback - // reclaims the allocation via heap::take. Dropping the - // Box here would free the uv_fs_t out from under libuv. - let raw = bun_core::heap::into_raw(file); - // SAFETY: raw is a live heap File*; the pending fs callback - // is the sole reclaimer (heap::take in on_close_complete / - // on_file_read's detached path) when one is left pending. - unsafe { - // A read in flight writes into `self._buffer` (via - // `iov`) whenever it completes; this reader may be - // dropped before then, so the buffer goes with the File. - if self.flags.contains(WindowsFlags::HAS_INFLIGHT_READ) { - (*raw).orphaned_read_buf = core::mem::take(&mut self._buffer); - } - if self.flags.contains(WindowsFlags::CLOSE_HANDLE) { - (*raw).detach(); - } else if !(*raw).detach_borrowed_fd() { - // Idle and the fd is parent-owned: nothing pending, - // nothing to close. Reclaim and drop the Box. - drop(bun_core::heap::take(raw)); - } - } - } - Source::Pipe(pipe) => { - // Hand the Box off to libuv; the close cb reclaims it. - let raw = bun_core::heap::into_raw(pipe); - // SAFETY: raw is a live uv::Pipe*; on_pipe_close frees it. - unsafe { - (*raw).data = raw.cast::(); - self.flags.insert(WindowsFlags::IS_PAUSED); - (*raw).close(Self::on_pipe_close); - } - } - Source::Tty(tty) => { - let p = tty.as_ptr(); - if crate::source::stdin_tty::is_stdin_tty(p) { - // Node only ever closes stdin on process exit. - } else { - // SAFETY: tty is a live heap-allocated Tty*; - // `Tty::close` keeps whole-struct provenance so - // on_tty_close may reclaim the Box. - unsafe { - (*p).uv.data = p.cast::(); - crate::source::Tty::close(p, Self::on_tty_close); - } - } - - self.flags.insert(WindowsFlags::IS_PAUSED); - } - } - // self.source already None via take(). - if CALL_DONE { - self.done(); - } + fn close_impl(&mut self) { + // Dropping the source cancels what it has in flight and releases the + // handle once that has been collected; nothing reports here again. + // `IS_PAUSED` stays the owner's: a reader started again reads unless + // the owner paused it. + self.recall_buffer(); + if self.source.take().is_some() && CALL_DONE { + self.done(); } } /// Close the reader and call the done callback. - /// If a file operation is in progress, defers the done callback until - /// the operation completes to ensure proper cleanup ordering. pub fn close(&mut self) { - let _ = self.stop_reading(); - - // Check if we have a pending file operation - if let Some(source) = &self.source { - if matches!(source, Source::File(_) | Source::SyncFile(_)) { - let file = source.file(); - // Defer done if operation is in progress (whether cancel succeeded or failed) - if file.state == crate::source::FileState::Canceling - || file.state == crate::source::FileState::Operating - { - self.flags.insert(WindowsFlags::DEFER_DONE_CALLBACK); - return; // Don't call closeImpl yet - wait for operation callback - } - } - } - self.close_impl::(); } /// Explicit teardown that does **not** fire `on_reader_done` (unlike - /// [`close`]). Safe to call - /// before Drop; both paths are idempotent over an already-taken source. + /// [`close`]). Safe to call before Drop; both paths are idempotent over an + /// already-taken source. pub fn deinit(&mut self) { MaxBuf::remove_from_pipereader(&mut self.maxbuf); - if let Some(source) = self.source.take() { - if !source.is_closed() { - // closeImpl will take care of freeing the source. - // Dropping the `Box` here would free a uv_pipe_t still - // linked into the loop's handle queue → UAF. Restore the source - // so close_impl can do the proper take + hand-off to libuv - // (into_raw + uv_close). close_impl also parks `_buffer` on - // the File for an in-flight uv_fs_read (orphaned_read_buf), - // which is why `_buffer` is freed after this, not before. - self.source = Some(source); - self.close_impl::(); - } else { - // Already closing/closed: a uv close callback may still be - // pending on this allocation; dropping the Box would free - // memory libuv still owns, so leak it instead. - core::mem::forget(source); - } - } + // What was lent goes with the source. + self.lent = 0; + self.close_impl::(); self._buffer = Vec::new(); } - extern "C" fn on_pipe_close(handle: *mut uv::Pipe) { - // `close_impl` set `handle.data = handle` and called `uv_close(handle)`; - // libuv passes the same pointer back, so `handle` *is* the boxed Pipe - // ptr — no need to round-trip through `.data`. - // SAFETY: pipe was Box-allocated (into_raw in close_impl); reclaim. - drop(unsafe { bun_core::heap::take(handle) }); - } - - extern "C" fn on_tty_close(handle: *mut uv::uv_tty_t) { - // `close_impl` set `handle.data = handle` and called `uv_close(handle)`; - // libuv passes the same pointer back; `Tty::from_uv` recovers the - // owning `Tty`. Caller gates on `!is_stdin_tty`, so it is heap-owned, - // and no request is pending once this runs (`uv::Tty::read_scratch`). - let tty = crate::source::Tty::from_uv(handle); - debug_assert!(!crate::source::stdin_tty::is_stdin_tty(tty)); - // SAFETY: non-stdin tty is heap-allocated; sole owner after uv_close. - drop(unsafe { bun_core::heap::take(tty) }); - } - - fn on_read(&mut self, amount: sys::Result, slice: &mut [u8], has_more: ReadState) { - if let sys::Result::Err(err) = amount { - // SAFETY: live reader. Note: this `&mut self` receiver still carries - // a protector across the (maybe-freeing) error dispatch — pre-existing - // on the parent chain, tracked with the raw-dispatch follow-up. - unsafe { Self::on_error(std::ptr::from_mut(self), err) }; + pub fn pause(&mut self) { + if self.flags.contains(ReaderFlags::IS_DONE) || self.flags.contains(ReaderFlags::IS_PAUSED) + { return; } - let amount_result = match amount { - sys::Result::Ok(n) => n, - sys::Result::Err(_) => unreachable!(), - }; - - // Address arithmetic: `slice` covers spare (uninit) capacity, so no `&[u8]` over the Vec may be formed for the check. - debug_assert!( - slice.is_empty() - || (slice.as_ptr() as usize >= self._buffer.as_ptr() as usize - && slice.as_ptr() as usize + slice.len() - <= self._buffer.as_ptr() as usize + self._buffer.capacity()), - "uv_read_cb: buf is not in buffer! This is a bug in bun. Please report it." - ); - - // move cursor foward - // SAFETY: slice is inside _buffer's spare capacity; libuv wrote `amount_result` bytes. - unsafe { bun_core::vec::commit_spare(&mut self._buffer, amount_result) }; - - // Using the limit up is this reader's EOF (`ReadLimit`); so is exhausting `maxBuffer`. - let limit_reached = self.limit.charge(amount_result); - let over_budget = self.charge_max_buffer(amount_result); - let has_more = if limit_reached || over_budget { - ReadState::Eof - } else { - has_more - }; - // Parents that want the reader paused call `reader().pause()` themselves; stopping here could free a parent whose caller still holds `this` (FileResponseStream on abort). - let _ = self.on_read_chunk(has_more); - - if has_more == ReadState::Eof { - self.close(); + self.flags.insert(ReaderFlags::IS_PAUSED); + match self.source.as_mut() { + Some(Source::Pipe(pipe)) => pipe.read_stop(), + Some(Source::Tty(tty)) => tty.read_stop(), + Some(Source::File(file)) => { + let _ = file.cancel(); + } + None => {} } } - pub fn pause(&mut self) { - let _ = self.stop_reading(); - } - pub fn unpause(&mut self) { if self.limit.reached() && !self.is_done() { // Nothing left to read: report EOF the way a completed read does instead of issuing one. - self.on_read(sys::Result::Ok(0), &mut [], ReadState::Eof); + self.flags.remove(ReaderFlags::IS_PAUSED); + self.on_read(0, ReadState::Eof); + return; + } + if !self.flags.contains(ReaderFlags::IS_PAUSED) { return; } - let _ = self.start_reading(); + self.flags.remove(ReaderFlags::IS_PAUSED); + if let sys::Result::Err(err) = self.start_reading() { + // SAFETY: live reader; nothing of `*self` is touched after. + unsafe { Self::on_error(core::ptr::from_mut(self), err) }; + } + } + + /// Hand the parent, from inside this call, what a pipe Bun created holds + /// now ([`Pipe::drain`](crate::windows::Pipe::drain)): what a child that + /// has exited left in it, say. + /// + /// # Safety + /// `this` is the live reader, and the caller is not inside one of its + /// dispatches. They re-enter the parent, which can reach the reader again. + pub unsafe fn drain(this: *mut Self) { + // SAFETY: caller contract; the borrow of the source ends where the + // pipe's own state is reached. + if let Some(Source::Pipe(pipe)) = unsafe { (*this).source.as_mut() } { + pipe.drain(); + } } + /// Windows reads complete through the loop; this unpauses the reader. + /// /// # Safety - /// `this` is the live reader. Raw for signature parity with the POSIX - /// entry (callers dispatch through a `*mut`); the body only unpauses. + /// `this` is the live reader. pub unsafe fn read(this: *mut Self) { - // we cannot sync read pipes on Windows so we just check if we are paused to resume the reading // SAFETY: caller contract; borrow scoped to the call. unsafe { (*this).unpause() }; } - /// Windows reads complete through libuv, never synchronously; this just makes sure one is in flight. + /// Windows reads complete through the loop, never synchronously; this just makes sure one is in flight. /// /// # Safety /// `this` is the live reader. @@ -1949,42 +1495,5 @@ impl WindowsBufferedReader { } } -// Keep boolean state in the `WindowsFlags` bitflags field — no loose `bool` -// fields on `WindowsBufferedReader`. - -#[cfg(windows)] -impl Drop for WindowsBufferedReader { - fn drop(&mut self) { - MaxBuf::remove_from_pipereader(&mut self.maxbuf); - // Do NOT take() source here and let it drop: Box/Box own - // live uv handles registered with the loop. Let close_impl perform the - // take + into_raw hand-off so the uv close callback reclaims them. - // Skip close_impl when the source is already closed — a uv_close is - // already pending on that allocation, so closing again would - // double-close and freeing the Box would UAF the handle libuv still - // references. Same as deinit(): leak the already-closing handle. - if let Some(source) = self.source.take() { - if !source.is_closed() { - self.source = Some(source); - self.close_impl::(); - } else { - let mut source = source; - if let Some(file) = source.file_key() { - uv::open_handles::remove_file(file); - } - core::mem::forget(source); - } - } - } -} - -// ────────────────────────────────────────────────────────────────────────── -// Platform alias -// ────────────────────────────────────────────────────────────────────────── - -#[cfg(unix)] -pub type BufferedReader = PosixBufferedReader; -#[cfg(windows)] -pub type BufferedReader = WindowsBufferedReader; #[cfg(not(any(unix, windows)))] compile_error!("Unsupported platform"); diff --git a/src/io/PipeWriter.rs b/src/io/PipeWriter.rs index 4b949b9ddcfd..02bc33ca9d46 100644 --- a/src/io/PipeWriter.rs +++ b/src/io/PipeWriter.rs @@ -4,14 +4,6 @@ use core::mem; use bun_collections::ByteVecExt; use bun_core::OOM; use bun_ptr::LaunderedSelf; // brings `Self::r` into scope for all 4 writers -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; -#[cfg(windows)] -// `close`/`set_data`/`ref_` are default trait methods; bring traits into scope -// so method resolution finds them on `Pipe`/`uv_tty_t`/`fs_t`. -use bun_sys::windows::libuv::UvHandle as _; use bun_sys::{self as sys, Fd}; use crate::{EventLoopHandle, FilePollFlag, FilePollKind, FilePollRef, Owner, PollTag}; @@ -19,6 +11,8 @@ use crate::{EventLoopHandle, FilePollFlag, FilePollKind, FilePollRef, Owner, Pol use crate::pipes::{FileType, PollOrFd}; #[cfg(windows)] use crate::source::Source; +#[cfg(windows)] +use crate::windows::{PipeOrigin, Refusal}; bun_core::define_scoped_log!(log, PipeWriter, hidden); @@ -275,7 +269,7 @@ fn write_to_socket(fd: Fd, buf: &[u8]) -> sys::Result { // PosixBufferedWriter // ────────────────────────────────────────────────────────────────────────── -/// Function table for `PosixBufferedWriter`; +/// Function table for `BufferedWriter`; /// in many cases the function table can be the same as `Parent`. /// /// All methods take `*mut Self` (not `&mut self`) because the writer is an @@ -283,7 +277,7 @@ fn write_to_socket(fd: Fd, buf: &[u8]) -> sys::Result { /// Materializing `&mut Parent` while a `&mut writer` is live would alias under /// Stacked Borrows, so we use raw /// pointers and never form a `&mut Parent` inside the writer. -pub trait PosixBufferedWriterParent { +pub trait BufferedWriterParent { /// `bun_io::poll_tag` constant for this writer's `FilePoll` owner. The /// per-tag dispatch in `bun_runtime::dispatch::__bun_run_file_poll` /// recovers `*mut PosixBufferedWriter` from this. @@ -294,19 +288,30 @@ pub trait PosixBufferedWriterParent { /// # Safety /// `this` must point to a live `Self`. unsafe fn on_error(this: *mut Self, err: sys::Error); - const HAS_ON_CLOSE: bool; /// # Safety /// `this` must point to a live `Self`. - unsafe fn on_close(_this: *mut Self) {} + unsafe fn on_close(this: *mut Self); + /// On Windows the bytes must not move or be freed while a write is in + /// flight. /// # Safety /// `this` must point to a live `Self`; returned slice borrows from it. unsafe fn get_buffer<'a>(this: *mut Self) -> &'a [u8]; /// # Safety /// `this` must point to a live `Self`. unsafe fn event_loop(this: *mut Self) -> EventLoopHandle; + /// A write in flight borrows `get_buffer`'s bytes until its completion is + /// dequeued, so the writer holds a parent ref across it. + /// # Safety + /// `this` must point to a live `Self`. + #[cfg(windows)] + unsafe fn ref_(this: *mut Self); + /// # Safety + /// `this` must point to a live `Self`. + #[cfg(windows)] + unsafe fn deref(this: *mut Self); } -pub struct PosixBufferedWriter { +pub struct PosixBufferedWriter { pub handle: PollOrFd, /// `None` only between `Default` and `set_parent`; every dispatch path /// assumes it is set (see SAFETY comments at the call sites). @@ -318,7 +323,7 @@ pub struct PosixBufferedWriter { rwf_unsupported: core::cell::Cell, } -impl Default for PosixBufferedWriter { +impl Default for PosixBufferedWriter { fn default() -> Self { Self { handle: PollOrFd::Closed, @@ -332,7 +337,7 @@ impl Default for PosixBufferedWriter } } -impl PosixPipeWriter for PosixBufferedWriter { +impl PosixPipeWriter for PosixBufferedWriter { fn get_fd(&self) -> Fd { self.handle.get_fd() } @@ -364,12 +369,9 @@ impl PosixPipeWriter for PosixBufferedWriter< // SAFETY: writer is an intrusive field of `Parent`; `Parent::on_write` // re-entry writes `is_done`/`handle` but never frees it; single JS thread. -unsafe impl bun_ptr::LaunderedSelf - for PosixBufferedWriter -{ -} +unsafe impl bun_ptr::LaunderedSelf for PosixBufferedWriter {} -impl PosixBufferedWriter { +impl PosixBufferedWriter { /// Raw backref to the owning `Parent`. Returned as `*mut` (never `&mut`) /// because this writer is an intrusive field of `Parent` and a `&mut Parent` /// would alias the live `&mut self` under Stacked Borrows. All vtable @@ -515,20 +517,18 @@ impl PosixBufferedWriter { } pub fn close(&mut self) { - if Parent::HAS_ON_CLOSE { - if self.closed_without_reporting { - self.closed_without_reporting = false; - // SAFETY: parent BACKREF valid. - unsafe { Parent::on_close(self.parent()) }; - } else { - let parent = self.parent(); - self.handle.close_impl( - Some(parent.cast()), - // SAFETY: parent was set via set_parent with a *mut Parent. - Some(|ctx: *mut c_void| unsafe { Parent::on_close(ctx.cast::()) }), - self.close_fd, - ); - } + if self.closed_without_reporting { + self.closed_without_reporting = false; + // SAFETY: parent BACKREF valid. + unsafe { Parent::on_close(self.parent()) }; + } else { + let parent = self.parent(); + self.handle.close_impl( + Some(parent.cast()), + // SAFETY: parent was set via set_parent with a *mut Parent. + Some(|ctx: *mut c_void| unsafe { Parent::on_close(ctx.cast::()) }), + self.close_fd, + ); } } @@ -560,9 +560,6 @@ impl PosixBufferedWriter { } } - /// On POSIX a `MovableIfWindowsFd` never transfers ownership, so callers - /// pass the plain `Fd` (via `MovableIfWindowsFd::get_posix()` when needed). - /// /// On `Err` the writer holds nothing; `fd` is still the caller's to close. pub fn start(&mut self, rawfd: Fd, pollable: bool) -> sys::Result<()> { let fd = rawfd; @@ -1136,397 +1133,198 @@ impl Drop for PosixStreamingWriter { } // ────────────────────────────────────────────────────────────────────────── -// BaseWindowsPipeWriter +// Windows writers // ────────────────────────────────────────────────────────────────────────── -/// Will provide base behavior for pipe writers. -/// The implementor type should provide: -/// source: Option, -/// parent: *mut Parent, -/// is_done: bool, -/// owns_fd: bool, -/// fn start_with_current_pipe(&mut self) -> sys::Result<()>, -/// fn on_close_source(&mut self), +/// Common parent requirements for Windows writers (loop access + ref counting). +/// +/// A write that has been handed to the kernel borrows its bytes from the +/// parent until the completion is dequeued, so the writer holds a parent ref +/// across every write in flight. +/// +/// All methods take `*mut Self` (not `&self`) because the writer is an +/// intrusive *field of* the parent (it holds a raw `parent` back-pointer). +/// Materializing `&Parent`/`&mut Parent` while a `&mut writer` is live would +/// alias under Stacked Borrows, so we use +/// raw pointers and never form a Rust reference to `Parent` inside the +/// writer. #[cfg(windows)] -pub trait BaseWindowsPipeWriter: Sized { - type Parent: WindowsWriterParent; - - fn source(&self) -> &Option; - fn source_mut(&mut self) -> &mut Option; - fn parent_ptr(&self) -> *mut Self::Parent; - fn set_parent_ptr(&mut self, p: *mut Self::Parent); - fn is_done(&self) -> bool; - fn set_is_done(&mut self, v: bool); - fn owns_fd(&self) -> bool; - fn start_with_current_pipe(&mut self) -> sys::Result<()>; - fn on_close_source(&mut self); - fn closed_without_reporting(&self) -> bool; - fn set_closed_without_reporting(&mut self, v: bool); - - /// `uv::open_handles` closes this writer's stream through here at teardown. - unsafe fn stop_for_vm_teardown(this: *mut c_void) { - // SAFETY: recorded via `Source::set_owner` by this live writer; cleared - // when the writer closes (source taken → uv_close → off the list). - unsafe { (*this.cast::()).close() }; - } - - /// Close the source without invoking `Parent::on_close` — for `Drop`, where - /// the parent is mid-teardown. Error paths use `close()` so the parent - /// still observes `on_close`. - fn close_without_reporting(&mut self) { - if self.source().is_some() { - self.set_closed_without_reporting(true); - // Last: `close()` may drop the parent's final ref and free `self`. - self.close(); - } - } +pub trait WindowsWriterParent { + /// # Safety + /// `this` must point to a live `Self`. + unsafe fn loop_(this: *mut Self) -> *mut bun_uws_sys::Loop; + /// # Safety + /// `this` must point to a live `Self`. + unsafe fn ref_(this: *mut Self); + /// # Safety + /// `this` must point to a live `Self`. + unsafe fn deref(this: *mut Self); +} - fn enable_keeping_process_alive(&mut self, event_loop: EventLoopHandle) { - self.update_ref(event_loop, true); - } +/// A writer's place, while a write of its is out, in its thread's list of them. +/// +/// The ref a writer holds on its parent for such a write is released when the +/// result arrives. The loop of a VM that is being torn down does not turn any +/// more, so it never does: [`abandon_writes_out`] releases those refs. +#[cfg(windows)] +pub(crate) struct WriteOut { + prev: *mut WriteOut, + next: *mut WriteOut, + listed: bool, + /// The writer this is a field of lets go of its source and of the ref. + abandon: unsafe fn(*mut WriteOut), +} - fn disable_keeping_process_alive(&mut self, event_loop: EventLoopHandle) { - self.update_ref(event_loop, false); - } +#[cfg(windows)] +thread_local! { + static WRITES_OUT: core::cell::Cell<*mut WriteOut> = const { core::cell::Cell::new(core::ptr::null_mut()) }; +} - fn close(&mut self) { - self.set_is_done(true); - let Some(source) = self.source_mut().take() else { - return; - }; - // Check for in-flight file write before detaching. detach() nulls - // fs.data so on_fs_write_complete can't recover the writer to call - // deref(); balance the ref taken when the write was submitted here. - let has_inflight_write = match &source { - Source::SyncFile(file) | Source::File(file) => { - file.state == crate::source::FileState::Operating - || file.state == crate::source::FileState::Canceling - } - _ => false, - }; - match source { - Source::SyncFile(file) | Source::File(file) => { - // Hand the Box off to libuv; the embedded uv_fs_t may still have - // an in-flight write (on_fs_write_complete) or will receive an - // async uv_fs_close callback (File::on_close_complete). Dropping - // the Box here would free that memory before the callback fires. - // Leak via into_raw; the on_close_detached path - // reclaims via heap::take in File::on_close_complete. - let raw = bun_core::heap::into_raw(file); - // SAFETY: raw is heap-allocated by Source::open_file; libuv holds - // the only remaining reference via the fs_t it points into. - unsafe { - if self.owns_fd() { - // Use state machine to handle close after operation completes. - // detach() schedules start_close() (now or after the pending - // op completes); on_close_complete heap::take()s `raw`. - (*raw).detach(); - } else if !(*raw).detach_borrowed_fd() { - // Idle and the fd is parent-owned: nothing pending, - // nothing to close. Reclaim and drop the Box. - drop(bun_core::heap::take(raw)); - } - // else: on_fs_write_complete heap::take()s the detached Box. - } - } - Source::Pipe(pipe) => { - // Hand the Box off to libuv; on_pipe_close reclaims it. - let raw = bun_core::heap::into_raw(pipe); - // SAFETY: raw is heap-allocated by Source::open; freed in on_pipe_close. - unsafe { - (*raw).data = raw.cast::(); - (*raw).close(on_pipe_close); - } - } - Source::Tty(tty) => { - let p = tty.as_ptr(); - // SAFETY: tty is heap-allocated (via open_tty) or the - // process-static stdin tty; freed in on_tty_close (gated on is_stdin_tty). - unsafe { (*p).uv.data = p.cast::() }; - // SAFETY: tty is a live uv handle; `Tty::close` keeps - // whole-struct provenance so on_tty_close may reclaim the Box. - unsafe { crate::source::Tty::close(p, on_tty_close) }; - } - } - *self.source_mut() = None; - self.on_close_source(); - // Deref last — this may free the parent and `self`. - if has_inflight_write { - // SAFETY: parent BACKREF valid until deref drops it. - unsafe { Self::Parent::deref(self.parent_ptr()) }; +#[cfg(windows)] +impl WriteOut { + fn new(abandon: unsafe fn(*mut WriteOut)) -> WriteOut { + WriteOut { + prev: core::ptr::null_mut(), + next: core::ptr::null_mut(), + listed: false, + abandon, } } - fn update_ref(&mut self, _event_loop: EventLoopHandle, value: bool) { - if let Some(pipe) = self.source_mut().as_mut() { - if value { - pipe.ref_(); - } else { - pipe.unref(); + /// # Safety + /// `this` is not listed and stays where it is until it is removed, on + /// this thread. + unsafe fn insert(this: *mut WriteOut) { + let head = WRITES_OUT.get(); + // SAFETY: caller contract; `head` is a listed (live) entry or null. + unsafe { + debug_assert!(!(*this).listed); + (*this).listed = true; + (*this).prev = core::ptr::null_mut(); + (*this).next = head; + if !head.is_null() { + (*head).prev = this; } } + WRITES_OUT.set(this); } - /// Also the single point where this writer records itself as the owner a - /// VM teardown stops its pipe/tty through: every way a source is installed - /// (`start`, `start_with_pipe`, `set_pipe`, `start_sync`, `start_with_file`) - /// funnels through here. - fn set_parent(&mut self, parent: *mut Self::Parent) { - self.set_parent_ptr(parent); - if !self.is_done() { - // raw self-ptr first to dodge the immutable-then-mutable conflict - let self_ptr = core::ptr::from_mut(self).cast::(); - if let Some(source) = self.source_mut().as_mut() { - source.set_owner(self_ptr, Self::stop_for_vm_teardown); + /// # Safety + /// `this` is live, on the thread that listed it. + unsafe fn remove(this: *mut WriteOut) { + // SAFETY: caller contract; neighbours are listed (live) entries. + unsafe { + if !mem::take(&mut (*this).listed) { + return; + } + let (prev, next) = ((*this).prev, (*this).next); + if prev.is_null() { + WRITES_OUT.set(next); + } else { + (*prev).next = next; + } + if !next.is_null() { + (*next).prev = prev; } } } - - fn watch(&mut self) { - // no-op - } - - /// SAFETY: `pipe` must be a `Box`-allocated pointer; ownership - /// transfers to `self.source` (later freed via `close_and_destroy`). - unsafe fn start_with_pipe(&mut self, pipe: *mut uv::Pipe) -> sys::Result<()> { - debug_assert!(self.source().is_none()); - // SAFETY: caller contract — Box-allocated, ownership transfers. - *self.source_mut() = Some(Source::Pipe(unsafe { bun_core::heap::take(pipe) })); - let p = self.parent_ptr(); - self.set_parent(p); - self.start_with_current_pipe() - } - - fn start_sync(&mut self, fd: Fd, _pollable: bool) -> sys::Result<()> { - debug_assert!(self.source().is_none()); - let mut source = Source::SyncFile(Source::open_file(fd)); - source.set_data(core::ptr::from_mut(self).cast::()); - *self.source_mut() = Some(source); - let p = self.parent_ptr(); - self.set_parent(p); - self.start_with_current_pipe() - } - - fn start_with_file(&mut self, fd: Fd) -> sys::Result<()> { - debug_assert!(self.source().is_none()); - let mut source = Source::File(Source::open_file(fd)); - source.set_data(core::ptr::from_mut(self).cast::()); - *self.source_mut() = Some(source); - let p = self.parent_ptr(); - self.set_parent(p); - self.start_with_current_pipe() - } - - // TODO: MovableIfWindowsFd overload — add a separate start_movable(). - fn start(&mut self, rawfd: Fd, _pollable: bool) -> sys::Result<()> { - let fd = rawfd; - debug_assert!(self.source().is_none()); - // Use the event loop from the parent, not the global one - // This is critical for spawnSync to use its isolated loop - // SAFETY: parent is BACKREF set via set_parent; valid while writer alive. - let loop_ = unsafe { Self::Parent::loop_(self.parent_ptr()) }; - let mut source = match Source::open(loop_, fd) { - sys::Result::Ok(source) => source, - sys::Result::Err(err) => return sys::Result::Err(err), - }; - // Creating a uv_pipe/uv_tty takes ownership of the file descriptor - // TODO: Change the type of the parameter and update all places to - // use MovableFD - // TODO: take ownership of the fd for pipe/tty sources via a MovableFd - // overload. - let _ = matches!(source, Source::Pipe(_) | Source::Tty(_)); - source.set_data(core::ptr::from_mut(self).cast::()); - *self.source_mut() = Some(source); - let p = self.parent_ptr(); - self.set_parent(p); - self.start_with_current_pipe() - } - - /// SAFETY: `pipe` must be a `Box`-allocated pointer. - unsafe fn set_pipe(&mut self, pipe: *mut uv::Pipe) { - // The assignment below would Drop the prior Box WITHOUT uv_close, leaving - // libuv with a dangling handle → UAF on next loop tick. All other - // start_* paths assert empty; enforce the same invariant here. - debug_assert!(self.source().is_none()); - // SAFETY: caller contract — Box-allocated, ownership transfers. - *self.source_mut() = Some(Source::Pipe(unsafe { bun_core::heap::take(pipe) })); - let p = self.parent_ptr(); - self.set_parent(p); - } - - fn get_stream(&mut self) -> Option<*mut uv::uv_stream_t> { - let source = self.source_mut().as_mut()?; - // `Source::to_stream()` is `unreachable!()` for both File and - // SyncFile, so exclude both to avoid panic. - if matches!(source, Source::File(_) | Source::SyncFile(_)) { - return None; - } - Some(source.to_stream()) - } } +/// This thread's VM is being torn down and its loop has stopped turning, with +/// the JS heap still there: a parent that goes with its last ref drops the JS +/// handles it holds. #[cfg(windows)] -extern "C" fn on_pipe_close(handle: *mut uv::Pipe) { - // `close()` set `handle.data = handle` and then called `uv_close(handle)`; - // libuv passes the same pointer back, so `handle` *is* the boxed Pipe ptr - // — no need to round-trip through `.data`. - // SAFETY: `handle` is the Box leaked via into_raw in close(). - drop(unsafe { bun_core::heap::take(handle) }); +pub fn abandon_writes_out() { + loop { + let head = WRITES_OUT.get(); + if head.is_null() { + return; + } + // SAFETY: listed entries are live; `abandon` takes `head` off the list. + unsafe { ((*head).abandon)(head) }; + } } +/// Hand `data` to `source`; `on_write(ctx, ..)` runs from the loop afterwards. +/// `refusal` is for a pipe: only its peer can have a write refused. +/// +/// # Safety +/// `data` and `ctx` stay valid until `on_write` has run. #[cfg(windows)] -extern "C" fn on_tty_close(handle: *mut uv::uv_tty_t) { - // `close()` set `handle.data = handle` and then called `uv_close(handle)`; - // libuv passes the same pointer back; `Tty::from_uv` recovers the owning - // `Tty`. The stdin tty (fd 0) lives in static storage; never free it. - let tty = crate::source::Tty::from_uv(handle); - if !crate::source::stdin_tty::is_stdin_tty(tty) { - // SAFETY: non-stdin tty is heap-allocated (open_tty). - drop(unsafe { bun_core::heap::take(tty) }); +unsafe fn submit_write( + source: &mut Source, + data: &[u8], + refusal: Refusal, + ctx: *mut T, + on_write: unsafe fn(*mut T, sys::Result), +) -> sys::Result<()> { + match source { + // SAFETY: caller contract. + Source::Pipe(pipe) => unsafe { pipe.write(data, refusal, ctx, on_write) }, + Source::Tty(tty) => tty.write(data, ctx, on_write), + // SAFETY: caller contract. + Source::File(file) => unsafe { file.write(data, ctx, on_write) }, } } -/// Common parent requirements for Windows writers (event loop access + ref counting). -/// -/// All methods take `*mut Self` (not `&self`) because the writer is an -/// intrusive *field of* the parent (it holds a raw `parent` back-pointer). -/// Materializing `&Parent`/`&mut Parent` while a `&mut writer` is live would -/// alias under Stacked Borrows, so we use -/// raw pointers and never form a Rust reference to `Parent` inside the -/// writer. +/// Write all of `data` before returning. #[cfg(windows)] -pub trait WindowsWriterParent { - /// # Safety - /// `this` must point to a live `Self`. - unsafe fn loop_(this: *mut Self) -> *mut uv::Loop; - /// # Safety - /// `this` must point to a live `Self`. - unsafe fn ref_(this: *mut Self); - /// # Safety - /// `this` must point to a live `Self`. - unsafe fn deref(this: *mut Self); +fn write_blocking(source: &mut Source, data: &[u8]) -> sys::Result { + match source { + Source::Pipe(pipe) => pipe.write_blocking(data), + Source::Tty(tty) => tty.try_write(data), + Source::File(file) => { + let fd = file.fd(); + let mut written = 0usize; + while written < data.len() { + match sys::write(fd, &data[written..])? { + 0 => break, + n => written += n, + } + } + Ok(written) + } + } } // ────────────────────────────────────────────────────────────────────────── // WindowsBufferedWriter // ────────────────────────────────────────────────────────────────────────── -/// Function table for `WindowsBufferedWriter`. -/// -/// All methods take `*mut Self` — see [`WindowsWriterParent`] for rationale. -#[cfg(windows)] -pub trait WindowsBufferedWriterParent: WindowsWriterParent { - /// # Safety - /// `this` must point to a live `Self`. - unsafe fn on_write(this: *mut Self, amount: usize, status: WriteStatus); - /// # Safety - /// `this` must point to a live `Self`. - unsafe fn on_error(this: *mut Self, err: sys::Error); - const HAS_ON_CLOSE: bool; - /// # Safety - /// `this` must point to a live `Self`. - unsafe fn on_close(_this: *mut Self) {} - /// # Safety - /// `this` must point to a live `Self`; returned slice borrows from it. - unsafe fn get_buffer<'a>(this: *mut Self) -> &'a [u8]; - const HAS_ON_WRITABLE: bool; - /// # Safety - /// `this` must point to a live `Self`. - unsafe fn on_writable(_this: *mut Self) {} -} - #[cfg(windows)] -pub struct WindowsBufferedWriter { +pub struct WindowsBufferedWriter { pub source: Option, - pub owns_fd: bool, + pub close_fd: bool, pub(crate) parent: *mut Parent, pub(crate) is_done: bool, - /// Set by `Drop`: the parent is going away, close without `on_close`. - pub(crate) closed_without_reporting: bool, - // we use only one write_req, any queued data in outgoing will be flushed after this ends - pub(crate) write_req: uv::uv_write_t, - pub(crate) write_buffer: uv::uv_buf_t, + /// One write at a time; whatever the parent queues meanwhile goes out after it. pub(crate) pending_payload_size: usize, + out: WriteOut, } #[cfg(windows)] -impl Default for WindowsBufferedWriter { +impl Default for WindowsBufferedWriter { fn default() -> Self { Self { source: None, - owns_fd: true, + close_fd: true, parent: core::ptr::null_mut(), is_done: false, - closed_without_reporting: false, - write_req: bun_core::ffi::zeroed(), - write_buffer: uv::uv_buf_t::init(b""), pending_payload_size: 0, + out: WriteOut::new(Self::abandon), } } } #[cfg(windows)] -impl BaseWindowsPipeWriter for WindowsBufferedWriter { - type Parent = Parent; - - fn source(&self) -> &Option { - &self.source - } - fn source_mut(&mut self) -> &mut Option { - &mut self.source - } - fn parent_ptr(&self) -> *mut Parent { - self.parent - } - fn set_parent_ptr(&mut self, p: *mut Parent) { - self.parent = p; - } - fn is_done(&self) -> bool { - self.is_done - } - fn set_is_done(&mut self, v: bool) { - self.is_done = v; - } - fn owns_fd(&self) -> bool { - self.owns_fd - } - - fn on_close_source(&mut self) { - if self.closed_without_reporting { - return; - } - if Parent::HAS_ON_CLOSE { - // SAFETY: parent is BACKREF set via set_parent; valid while writer alive. - unsafe { Parent::on_close(self.parent) }; - } - } - fn closed_without_reporting(&self) -> bool { - self.closed_without_reporting - } - fn set_closed_without_reporting(&mut self, v: bool) { - self.closed_without_reporting = v; - } - - fn start_with_current_pipe(&mut self) -> sys::Result<()> { - debug_assert!(self.source.is_some()); - self.is_done = false; - self.write(); - sys::Result::Ok(()) - } -} - -#[cfg(windows)] -// SAFETY: libuv write-complete callbacks re-enter via `FileSink::on_write` → -// JS → `writer.with_mut(|w| w.end())`; writer is intrusive in `Parent`, kept -// alive across the callback by the parent ref taken in `write()` (derefed via -// the callback-end scopeguards); single JS thread. -unsafe impl bun_ptr::LaunderedSelf - for WindowsBufferedWriter -{ -} +// SAFETY: write completions re-enter via `Parent::on_write` (`IOWriter`, +// `StaticPipeWriter`), which may end the writer; writer is intrusive in +// `Parent`, kept alive across the callback by the parent ref taken in +// `write()` (derefed via the callback-end scopeguards); single JS thread. +unsafe impl bun_ptr::LaunderedSelf for WindowsBufferedWriter {} #[cfg(windows)] -impl WindowsBufferedWriter { +impl WindowsBufferedWriter { /// Raw backref to the owning `Parent`. Returned as `*mut` (never `&mut`) /// because this writer is an intrusive field of `Parent` and a `&mut Parent` /// would alias the live `&mut self` under Stacked Borrows. All vtable @@ -1536,29 +1334,9 @@ impl WindowsBufferedWriter { self.parent } - /// Single nonnull-asref dispatch for the set-once `parent` backref. - /// Type invariant (encapsulated `unsafe`): see - /// [`PosixBufferedWriter::parent_on_error`] — same shape, same proof. - /// Laundered (`(*this)`) sites in `on_write_complete` / - /// `on_fs_write_complete` route through [`r_on_error`](Self::r_on_error) - /// instead so no `&self` protector is held across re-entry. - #[inline] - fn parent_on_error(&self, err: sys::Error) { - // SAFETY: type invariant — set-once parent backref outlives writer. - unsafe { Parent::on_error(self.parent(), err) } - } - - /// Laundered-receiver variant of [`parent_on_error`](Self::parent_on_error). - /// - /// Type invariant (encapsulated `unsafe`): `self.parent` is populated by - /// [`set_parent`](BaseWindowsPipeWriter::set_parent) before any write path - /// is reached, and the writer is an intrusive field of `*parent` so the - /// pointee strictly outlives `self`. Takes the R-2 `*mut Self` so the field - /// read completes before dispatch and no Rust borrow of `*this` is live - /// across the (re-entrant) `Parent::on_error` call. Collapses the two - /// identical dispatch blocks in `on_write_complete` / - /// `on_fs_write_complete` into one — mirrors - /// [`WindowsStreamingWriter::r_on_error`]. + /// Takes `*mut Self` so the field read completes before dispatch and no + /// Rust borrow of `*this` is live across the (re-entrant) + /// `Parent::on_error` call. #[inline(always)] fn r_on_error(this: *mut Self, err: sys::Error) { let parent = Self::r(this).parent; @@ -1566,9 +1344,8 @@ impl WindowsBufferedWriter { unsafe { Parent::on_error(parent, err) } } - /// See [`r_on_error`](Self::r_on_error). Reads `self.parent` at guard - /// execution so a re-entrant `set_parent` cannot over-deref a stale - /// pointer — mirrors [`WindowsStreamingWriter::r_deref`]. + /// Reads `self.parent` at guard execution so a re-entrant `set_parent` + /// cannot over-deref a stale pointer. #[inline(always)] fn r_deref(this: *mut Self) { let parent = Self::r(this).parent; @@ -1578,34 +1355,160 @@ impl WindowsBufferedWriter { } pub fn memory_cost(&self) -> usize { - mem::size_of::() + self.write_buffer.len as usize + mem::size_of::() } - fn on_write_complete(&mut self, status: uv::ReturnCode) { - // PORT_NOTES_PLAN R-2: `&mut self` carries LLVM `noalias`, but - // `Parent::on_write` (e.g. `FileSink::on_write`) re-enters JS via - // promise resolution and may call back into this writer through a fresh - // `&mut Self` derived from the parent's intrusive `writer` field - // (`writer.with_mut(|w| w.end())`), writing `self.is_done`. With - // `noalias`, LLVM may cache the pre-call `is_done`/`parent` and reuse - // them after the call. Launder so post-`on_write` reads see fresh - // state. - let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self)); + pub fn set_parent(&mut self, parent: *mut Parent) { + self.parent = parent; + } + + pub fn watch(&mut self) { + // Writes complete on their own; there is nothing to arm. + } + + pub fn update_ref(&self, _event_loop: EventLoopHandle, value: bool) { + if let Some(source) = &self.source { + if value { + source.ref_(); + } else { + source.unref(); + } + } + } + + pub fn disable_keeping_process_alive(&self, event_loop: EventLoopHandle) { + self.update_ref(event_loop, false); + } + + /// Open `fd` and start writing what the parent has. See + /// [`PipeOrigin::from_is_pollable`] for `is_pollable`. + /// + /// `Err` is a source that cannot be opened or used; the writer then holds + /// nothing and `fd` is still the caller's to close. A reader that is gone + /// already is a failed write like any later one: the parent hears + /// `on_error` from the loop, as it does from the POSIX writer, whose + /// `start` writes nothing. + pub fn start(&mut self, fd: Fd, is_pollable: bool) -> sys::Result<()> { + self.start_with_origin(fd, PipeOrigin::from_is_pollable(is_pollable)) + } + + /// As [`start`](Self::start), by a caller that knows where `fd` came from. + pub fn start_with_origin(&mut self, fd: Fd, origin: PipeOrigin) -> sys::Result<()> { + let source = Source::open(self.parent_loop(), fd, origin, self.close_fd)?; + self.start_on(source) + } + + /// As [`start_with_origin`](Self::start_with_origin), by a caller that has + /// asked what `fd` is already (`bun_sys::File::kind`). + pub fn start_with_kind( + &mut self, + fd: Fd, + origin: PipeOrigin, + kind: sys::FileKind, + ) -> sys::Result<()> { + let source = Source::open_as(self.parent_loop(), fd, origin, kind, self.close_fd)?; + self.start_on(source) + } + + /// The parent's loop, not the thread's: `spawnSync` writes on its own. + fn parent_loop(&self) -> *mut bun_uws_sys::Loop { + // SAFETY: parent is the BACKREF set via set_parent; valid while the writer is. + unsafe { Parent::event_loop(self.parent) }.loop_() + } + + fn start_on(&mut self, source: Source) -> sys::Result<()> { + debug_assert!(self.source.is_none()); + self.source = Some(source); + self.is_done = false; + let submitted = self.submit(Refusal::Posted); + if submitted.is_err() { + self.is_done = true; + if let Some(mut source) = self.source.take() { + source.disown(); + } + } + submitted + } + + /// `report`: whether the parent hears `on_close`, and whether a write still + /// in flight reports back (it must not once the parent is being dropped). + fn close_source(&mut self, report: bool) { + self.is_done = true; + let Some(mut source) = self.source.take() else { + return; + }; + if !self.close_fd { + source.disown(); + } + if report { + source.close(); + } else { + drop(source); + } + if report { + // SAFETY: parent is BACKREF set via set_parent; valid while writer alive. + unsafe { Parent::on_close(self.parent) }; + } + } + + pub fn close(&mut self) { + self.close_source(true); + } + + /// Close the source without invoking `Parent::on_close` — for a parent + /// that is mid-teardown. + pub fn close_without_reporting(&mut self) { + self.close_source(false); + } + + /// [`WriteOut::abandon`]. + unsafe fn abandon(out: *mut WriteOut) { + // SAFETY: `out` is the field of a writer with a write out, which the + // parent ref released last keeps alive. + unsafe { + let this = out.byte_sub(mem::offset_of!(Self, out)).cast::(); + WriteOut::remove(out); + (*this).pending_payload_size = 0; + (*this).close_without_reporting(); + Self::r_deref(this); + } + } + + /// # Safety + /// `this` is the writer that submitted the write, kept alive by the + /// parent ref taken in `write`. + unsafe fn on_write_result(this: *mut Self, result: sys::Result) { + // `Parent::on_write` (e.g. `IOWriter::on_write`) runs the shell on, + // which may call back into this writer through a fresh `&mut Self` + // derived from the parent's intrusive `writer` field, writing + // `self.is_done`. Launder so post-`on_write` reads see fresh state. + let this: *mut Self = core::hint::black_box(this); + // SAFETY: fn contract. + unsafe { WriteOut::remove(&raw mut (*this).out) }; // Scopeguard deref to balance write()'s ref: `Parent::on_write` may // drop the last external strong ref, and the trailing `is_done` / // `close()` reads below need the parent (and `self`, inside it) alive. let _g = scopeguard::guard(this, |s| Self::r_deref(s)); - let written = Self::r(this).pending_payload_size; + let submitted = Self::r(this).pending_payload_size; Self::r(this).pending_payload_size = 0; - if let Some(err) = status.to_error(sys::Tag::write) { - Self::r(this).close(); - Self::r_on_error(this, err); + // Closed with the write still out; `close()` told the parent already. + // A write that could not be cancelled any more ends with its own result. + if Self::r(this).source.is_none() { return; } + let written = match result { + sys::Result::Err(err) if err.get_errno() == sys::E::ECANCELED => return, + sys::Result::Err(err) => { + Self::r(this).close(); + Self::r_on_error(this, err); + return; + } + sys::Result::Ok(written) => written.min(submitted), + }; let pending = Self::r(this).get_buffer_internal(); - // `close()` may have run before this callback (exit-first ordering) and - // cleared the parent's buffer to 0 while `written` still carries the - // submitted size; treat that as no pending data rather than underflowing. + // `close()` may have run before this callback and cleared the parent's + // buffer while `written` still carries the submitted size; treat that + // as no pending data rather than underflowing. let has_pending_data = pending.len().saturating_sub(written) != 0; let is_done_before = Self::r(this).is_done; // SAFETY: parent BACKREF valid. @@ -1627,163 +1530,64 @@ impl WindowsBufferedWriter { if Self::r(this).is_done && !has_pending_data { // already done and end was called Self::r(this).close(); - return; - } - - if Parent::HAS_ON_WRITABLE { - // SAFETY: parent BACKREF valid. - unsafe { Parent::on_writable(Self::r(this).parent()) }; - } - } - - extern "C" fn on_fs_write_complete(fs: *mut uv::fs_t) { - // SAFETY: libuv fs_cb — `fs` is the `uv_fs_t` field at offset 0 of a - // boxed `source::File`; `from_fs_callback` snapshots `result`/`data` - // and recovers `&mut File` via container_of. Single-threaded dispatch, - // no other Rust borrow of the boxed `File` is live. - let (file, result, parent_ptr) = unsafe { crate::source::File::from_fs_callback(fs) }; - let was_canceled = result.int() == uv::UV_ECANCELED as i64; - - // ALWAYS complete first — the boxed `source::File` outlives this - // callback (detach()/close() gates free). - file.complete(was_canceled); - - // If detached, file may be closing (owned fd) or just stopped (non-owned fd). - // The deref to balance write()'s ref was already done in close(). - if parent_ptr.is_null() { - // owns_fd detach() path: complete() already kicked off start_close() - // (state == Closing) and on_close_complete will heap::take the Box. - // !owns_fd close() path: complete() left state == Deinitialized and - // nothing else will reclaim the Box; this callback is the sole - // remaining owner, so free it here. - if file.state == crate::source::FileState::Deinitialized { - // SAFETY: `file` is the Box leaked in close() via into_raw. - drop(unsafe { bun_core::heap::take(core::ptr::from_mut(file)) }); - } - return; - } - - // PORT_NOTES_PLAN R-2: launder `*this` for the same reason as the - // Streaming sibling above — `close()` → `Parent::on_close` → JS may - // re-enter via `with_mut(|w| ..)`; the post-call `(*this).parent()` - // must reload. - // SAFETY: data was set to `self as *mut Self` in write(); libuv invokes - // this callback on the single-threaded event loop with no other Rust - // borrow of `*this` live, so this is the sole access path. - let this: *mut Self = core::hint::black_box(parent_ptr.cast::()); - - if was_canceled { - // Canceled write - clear pending state and balance write()'s ref. - Self::r(this).pending_payload_size = 0; - Self::r_deref(this); - return; } + } - if let Some(err) = result.to_error(sys::Tag::write) { - // Balance write()'s ref — lazy `.parent` read at guard execution - // in case close()/on_error re-enter and swap the parent pointer. - let _g = scopeguard::guard(this, |s| Self::r_deref(s)); - // close() may re-enter JS. - Self::r(this).close(); - core::hint::black_box(this); - // `r_on_error` re-reads `.parent` after the close() re-entry. - Self::r_on_error(this, err); - return; + /// Write what the parent has, unless a write is out already. A write the + /// source refuses at once is this call's error: the source is let go of + /// and the parent hears neither `on_error` nor `on_close` for it. + pub fn write(&mut self) -> sys::Result<()> { + let submitted = self.submit(Refusal::Returned); + if submitted.is_err() { + self.close_source(false); } - - // on_write_complete handles the deref (and is itself laundered). - Self::r(this).on_write_complete(uv::ReturnCode::zero()); + submitted } - pub fn write(&mut self) { - let buffer = self.get_buffer_internal(); + fn submit(&mut self, refusal: Refusal) -> sys::Result<()> { // if we are already done or if we have some pending payload we just wait until next write - if self.is_done || self.pending_payload_size > 0 || buffer.len() == 0 { - return; + // Before `get_buffer`: a parent may rebuild the buffer it returns, and + // the write in flight borrows the one it returned last. + if self.is_done || self.pending_payload_size > 0 { + return sys::Result::Ok(()); } - - // Snapshot the slice into an owned `uv_buf_t` (ptr + len, `Copy`) now; - // this ends the `&self` borrow held by `buffer` so the `&mut self` - // accesses below (`self.source.as_mut()`, field writes) are unencumbered. - // The underlying storage is not reallocated before libuv consumes it - // (only handed to libuv via uv_buf_t / write_req). - let buffer_len = buffer.len(); - let write_buf = uv::uv_buf_t::init(buffer); - - // BORROW_PARAM (raw-ptr break): the match arms mutate `self` while - // borrowing into `self.source`. The boxed `File`/`Pipe` live in their - // own heap allocations, so a `*mut` snapshot is provenance-disjoint - // from `&mut self` and stays valid across `self.*` writes. - let (file_raw, stream_raw): (*mut crate::source::File, *mut uv::uv_stream_t) = - match self.source.as_mut() { - None => return, - Some(Source::SyncFile(_)) => { - panic!("This code path shouldn't be reached - sync_file in PipeWriter.rs"); - } - Some(Source::File(f)) => (f.as_mut() as *mut _, core::ptr::null_mut()), - Some(s) => (core::ptr::null_mut(), s.to_stream()), - }; - - if !file_raw.is_null() { - // SAFETY: see raw-ptr break note above. - let file = unsafe { &mut *file_raw }; - // BufferedWriter ensures pending_payload_size blocks concurrent writes - debug_assert!(file.can_start()); - - self.pending_payload_size = buffer_len; - file.fs.data = core::ptr::from_mut(self).cast::(); - file.prepare(); - self.write_buffer = write_buf; - - // SAFETY: file is fully initialized; libuv stores the cb and fires - // it on the event loop. parent BACKREF valid. - if let Some(err) = unsafe { - uv::uv_fs_write( - Parent::loop_(self.parent()), - &mut file.fs, - file.file, - &self.write_buffer, - 1, - -1, - Some(Self::on_fs_write_complete), - ) - } - .to_error(sys::Tag::write) - { - file.complete(false); - self.close(); - self.parent_on_error(err); - } else { - // Ref the parent to prevent it from being freed while the async - // write is in flight. The matching deref is in on_write_complete, - // on_fs_write_complete, or close() (mid-flight teardown). - // SAFETY: parent is BACKREF set via set_parent; valid while writer alive. - unsafe { Parent::ref_(self.parent()) }; - } - } else { - // the buffered version should always have a stable ptr - self.pending_payload_size = buffer_len; - self.write_buffer = write_buf; - let self_ptr = self as *mut Self; - if let Some(write_err) = self - .write_req - // SAFETY: `p` is `self_ptr`; libuv invokes on the loop thread with no - // other Rust borrow of `*p` live, so `&mut *p` is the sole alias. - .write(stream_raw, &self.write_buffer, self_ptr, |p, s| unsafe { - (*p).on_write_complete(s) - }) - .to_error(sys::Tag::write) - { - self.close(); - self.parent_on_error(write_err); - } else { - // Keep the parent alive while the stream write is in flight; - // the write callback (incl. ECANCELED after close) always - // fires, and on_write_complete's guard runs the deref. - // SAFETY: parent BACKREF valid; intrusive refcount bump. - unsafe { Parent::ref_(self.parent()) }; - } + // SAFETY: parent is a BACKREF set via set_parent; valid while writer is + // alive. Not through `get_buffer_internal`, whose result borrows `self`. + let buffer: &[u8] = unsafe { Parent::get_buffer(self.parent()) }; + if buffer.is_empty() { + return sys::Result::Ok(()); + } + let this: *mut Self = self; + let Some(source) = self.source.as_mut() else { + return sys::Result::Ok(()); + }; + let len = buffer.len(); + match source { + // `close_source` cannot hand the source bytes that are the + // parent's, so a write it could not recall either gets its own. + Source::Pipe(pipe) if pipe.writes_outlive_cancel() => { + pipe.write_owned(buffer.to_vec(), refusal, this, Self::on_write_result)?; + } + Source::File(file) => { + file.write_owned(buffer.to_vec(), this, Self::on_write_result)?; + } + // SAFETY: the parent keeps `buffer` in place while a write is in + // flight (`get_buffer` contract) and is itself kept alive by the + // ref taken below; `this` is a field of it. + source => { + unsafe { submit_write(source, buffer, refusal, this, Self::on_write_result) }? + } + } + self.pending_payload_size = len; + // The matching deref is in `on_write_result`, which a cancelled write + // reaches too, or in `abandon`. + // SAFETY: parent BACKREF valid; intrusive refcount bump. The parent + // stays where it is while it is referenced. + unsafe { + Parent::ref_(self.parent()); + WriteOut::insert(&raw mut self.out); } + sys::Result::Ok(()) } fn get_buffer_internal(&self) -> &[u8] { @@ -1805,6 +1609,13 @@ impl WindowsBufferedWriter { } } +#[cfg(windows)] +impl Drop for WindowsBufferedWriter { + fn drop(&mut self) { + self.close_source(false); + } +} + // ────────────────────────────────────────────────────────────────────────── // StreamBuffer // ────────────────────────────────────────────────────────────────────────── @@ -1963,7 +1774,6 @@ pub trait WindowsStreamingWriterParent: WindowsWriterParent { /// # Safety /// `this` must point to a live `Self`. unsafe fn on_error(this: *mut Self, err: sys::Error); - const HAS_ON_WRITABLE: bool; /// # Safety /// `this` must point to a live `Self`. unsafe fn on_writable(_this: *mut Self) {} @@ -1975,24 +1785,26 @@ pub trait WindowsStreamingWriterParent: WindowsWriterParent { #[cfg(windows)] pub struct WindowsStreamingWriter { pub source: Option, - /// if the source of this writer is a file descriptor, calling end() will not close it. - /// if it is a path, then we claim ownership and the backing fd will be closed by end(). - pub owns_fd: bool, pub parent: *mut Parent, pub is_done: bool, - // we use only one write_req, any queued data in outgoing will be flushed after this ends - pub(crate) write_req: uv::uv_write_t, - pub(crate) write_buffer: uv::uv_buf_t, + /// Write on the calling thread and report the result from `write*` itself. + pub force_sync: bool, // queue any data that we want to write here pub outgoing: StreamBuffer, - // libuv requires a stable ptr when doing async so we swap buffers + // the bytes of the write in flight must not move, so the buffers are swapped pub(crate) current_payload: StreamBuffer, + /// The bytes of `current_payload` while a write has it (its list, which + /// the write gives back, and its cursor, kept in `lent_cursor`); 0 + /// otherwise. `current_payload` itself is empty meanwhile. + lent_len: usize, + lent_cursor: usize, // we preserve the last write result for simplicity pub(crate) last_write_result: WriteResult, - // Set only by `close_without_reporting()` (i.e. `Drop`) to suppress - // `Parent::on_close` while the parent is mid-teardown. + // Set only by `close_without_reporting()` to suppress `Parent::on_close` + // while the parent is mid-teardown. pub closed_without_reporting: bool, + out: WriteOut, } #[cfg(windows)] @@ -2000,69 +1812,20 @@ impl Default for WindowsStreamingWriter Self { Self { source: None, - owns_fd: true, parent: core::ptr::null_mut(), is_done: false, - write_req: bun_core::ffi::zeroed(), - write_buffer: uv::uv_buf_t::init(b""), + force_sync: false, outgoing: StreamBuffer::default(), current_payload: StreamBuffer::default(), + lent_len: 0, + lent_cursor: 0, last_write_result: WriteResult::Wrote(0), closed_without_reporting: false, + out: WriteOut::new(Self::abandon), } } } -#[cfg(windows)] -impl BaseWindowsPipeWriter - for WindowsStreamingWriter -{ - type Parent = Parent; - - fn source(&self) -> &Option { - &self.source - } - fn source_mut(&mut self) -> &mut Option { - &mut self.source - } - fn parent_ptr(&self) -> *mut Parent { - self.parent - } - fn set_parent_ptr(&mut self, p: *mut Parent) { - self.parent = p; - } - fn is_done(&self) -> bool { - self.is_done - } - fn set_is_done(&mut self, v: bool) { - self.is_done = v; - } - fn owns_fd(&self) -> bool { - self.owns_fd - } - - fn on_close_source(&mut self) { - self.source = None; - if self.closed_without_reporting { - return; - } - // SAFETY: parent is BACKREF set via set_parent; valid while writer alive. - unsafe { Parent::on_close(self.parent) }; - } - fn closed_without_reporting(&self) -> bool { - self.closed_without_reporting - } - fn set_closed_without_reporting(&mut self, v: bool) { - self.closed_without_reporting = v; - } - - fn start_with_current_pipe(&mut self) -> sys::Result<()> { - debug_assert!(self.source.is_some()); - self.is_done = false; - sys::Result::Ok(()) - } -} - #[cfg(windows)] // SAFETY: see `WindowsBufferedWriter`'s `LaunderedSelf` impl — identical shape. unsafe impl bun_ptr::LaunderedSelf @@ -2081,20 +1844,9 @@ impl WindowsStreamingWriter { self.parent } - /// Single nonnull-asref dispatch for the set-once `parent` backref, - /// laundered-receiver variant. - /// - /// Type invariant (encapsulated `unsafe`): `self.parent` is populated by - /// [`set_parent`](BaseWindowsPipeWriter::set_parent) before any write path - /// is reached, and the writer is an intrusive field of `*parent` so the - /// pointee strictly outlives `self`. Unlike a `&self` accessor (which would - /// place a `readonly`/SB-protector on `*self` for the duration of the - /// re-entrant `Parent::on_error` call — see the `parent_on_error` note on - /// [`WindowsBufferedWriter`]), this takes the R-2 `*mut Self`: the field - /// read completes before dispatch, so no Rust borrow of `*this` is live - /// across the (re-entrant) call. Collapses the five identical - /// `Parent::on_error(Self::r(this).parent(), err)` dispatch blocks in - /// `on_write_complete` / `on_fs_write_complete` / `process_send` into one. + /// Laundered-receiver dispatch: takes `*mut Self` so the field + /// read completes before dispatch and no Rust borrow of `*this` is live + /// across the (re-entrant) `Parent::on_error` call. #[inline(always)] fn r_on_error(this: *mut Self, err: sys::Error) { let parent = Self::r(this).parent; @@ -2102,9 +1854,6 @@ impl WindowsStreamingWriter { unsafe { Parent::on_error(parent, err) } } - /// See [`r_on_error`](Self::r_on_error) for the encapsulated type - /// invariant and laundered-receiver rationale. Collapses the two - /// `Parent::on_write` arms in `on_write_complete` into one `unsafe`. #[inline(always)] fn r_on_write(this: *mut Self, written: usize, status: WriteStatus) { let parent = Self::r(this).parent; @@ -2112,12 +1861,8 @@ impl WindowsStreamingWriter { unsafe { Parent::on_write(parent, written, status) } } - /// See [`r_on_error`](Self::r_on_error) for the encapsulated type - /// invariant and laundered-receiver rationale. Reads `self.parent` - /// **before** dispatch so the (potentially freeing) `Parent::deref` - /// runs with no borrow of `*this` live — matching the lazy read order - /// at each scopeguard site. Collapses the three - /// `Parent::deref` blocks into one `unsafe`. + /// Reads `self.parent` **before** dispatch so the (potentially freeing) + /// `Parent::deref` runs with no borrow of `*this` live. #[inline(always)] fn r_deref(this: *mut Self) { let parent = Self::r(this).parent; @@ -2128,58 +1873,188 @@ impl WindowsStreamingWriter { } pub fn memory_cost(&self) -> usize { - mem::size_of::() + self.current_payload.memory_cost() + self.outgoing.memory_cost() + mem::size_of::() + + self.current_payload.memory_cost() + + self.lent_len + + self.outgoing.memory_cost() } pub fn has_pending_data(&self) -> bool { - self.outgoing.is_not_empty() || self.current_payload.is_not_empty() + self.outgoing.is_not_empty() || self.current_payload.is_not_empty() || self.lent_len > 0 } - /// process_send found a uv_write already in flight (current_payload alone is not backpressure). + /// process_send found a write already in flight (current_payload alone is not backpressure). pub fn is_backed_up(&self) -> bool { self.outgoing.is_not_empty() } /// Bytes accepted from callers that have not reached the fd yet: queued in - /// `outgoing` or handed to libuv in `current_payload`. + /// `outgoing` or handed to the kernel in `current_payload`. pub fn buffered_len(&self) -> usize { - self.outgoing.size() + self.current_payload.size() + self.outgoing.size() + self.current_payload.size() + self.lent_len } - fn on_write_complete(&mut self, status: uv::ReturnCode) { - // PORT_NOTES_PLAN R-2: `&mut self` carries LLVM `noalias`, but - // `Parent::on_write` (e.g. `FileSink::on_write`) re-enters JS via - // promise resolution and may call back into this writer through a fresh - // `&mut Self` derived from the parent's intrusive `writer` field - // (`writer.with_mut(|w| w.end())` or `.write(..)`), writing - // `self.is_done` / `self.outgoing` / `self.parent`. With `noalias`, - // LLVM may cache pre-call field loads and reuse them after the call. - // Launder so all post-`on_write` field accesses see fresh state. - let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self)); + pub fn set_parent(&mut self, parent: *mut Parent) { + self.parent = parent; + } + pub fn update_ref(&self, _event_loop: EventLoopHandle, value: bool) { + if let Some(source) = &self.source { + if value { + source.ref_(); + } else { + source.unref(); + } + } + } + + pub fn enable_keeping_process_alive(&self, event_loop: EventLoopHandle) { + self.update_ref(event_loop, true); + } + + pub fn disable_keeping_process_alive(&self, event_loop: EventLoopHandle) { + self.update_ref(event_loop, false); + } + + /// See [`PipeOrigin::from_is_pollable`] for `is_pollable`. On `Err` the + /// writer holds nothing; `fd` is still the caller's to close. + pub fn start(&mut self, fd: Fd, is_pollable: bool) -> sys::Result<()> { + debug_assert!(self.source.is_none()); + // The parent's loop, not the thread's: `spawnSync` writes on its own. + // SAFETY: parent is the BACKREF set via set_parent; valid while the writer is. + let loop_ = unsafe { Parent::loop_(self.parent) }; + let origin = PipeOrigin::from_is_pollable(is_pollable); + let source = Source::open(loop_, fd, origin, true)?; + self.start_with_source(source); + sys::Result::Ok(()) + } + + /// Write to a source that is already open (an accepted or connected named + /// pipe). The source stays reachable through `self.source`, which is how a + /// duplex owner reads from the same pipe. + pub fn start_with_source(&mut self, source: Source) { + debug_assert!(self.source.is_none()); + self.source = Some(source); + self.is_done = false; + } + + /// `report`: whether the parent hears `on_close`, and whether a write still + /// in flight reports back (it must not once the parent is being dropped). + fn close_source(&mut self, report: bool) { + self.is_done = true; + let Some(mut source) = self.source.take() else { + return; + }; + if report { + source.close(); + // SAFETY: parent is BACKREF set via set_parent; valid while writer alive. + unsafe { Parent::on_close(self.parent) }; + } else { + // Nobody will be told when the write in flight is done with its + // bytes, so they go with the source. + match &mut source { + Source::Pipe(pipe) => { + pipe.adopt_write_buffer(mem::take(&mut self.current_payload.list)); + } + Source::File(file) => { + file.adopt_write_buffer(mem::take(&mut self.current_payload.list)); + file.forget_writer(); + } + Source::Tty(_) => {} + } + drop(source); + } + } + + pub fn close(&mut self) { + let report = !self.closed_without_reporting; + self.close_source(report); + } + + /// [`WriteOut::abandon`]. + unsafe fn abandon(out: *mut WriteOut) { + // SAFETY: `out` is the field of a writer with a write out, which the + // parent ref released last keeps alive. + unsafe { + let this = out.byte_sub(mem::offset_of!(Self, out)).cast::(); + WriteOut::remove(out); + (*this).close_without_reporting(); + (*this).lent_len = 0; + (*this).current_payload.reset(); + Self::r_deref(this); + } + } + + /// Close the source without invoking `Parent::on_close` — for a parent + /// that is mid-teardown. + pub fn close_without_reporting(&mut self) { + if self.source.is_some() { + self.closed_without_reporting = true; + self.close_source(false); + } + } + + /// # Safety + /// `this` is the writer that submitted the write, kept alive by the + /// parent ref taken in `process_send`. + unsafe fn on_write_result(this: *mut Self, result: sys::Result) { + // `Parent::on_write` (e.g. `FileSink::on_write`) + // re-enters JS via promise resolution and may call back into this + // writer through a fresh `&mut Self` derived from the parent's + // intrusive `writer` field (`writer.with_mut(|w| w.end())` or + // `.write(..)`), writing `self.is_done` / `self.outgoing` / + // `self.parent`. Launder so all post-`on_write` field accesses see + // fresh state. + let this: *mut Self = core::hint::black_box(this); + // SAFETY: fn contract. + unsafe { WriteOut::remove(&raw mut (*this).out) }; // Deref the parent at the end to balance the ref taken in - // process_send before submitting the async write request. - // Capturing `self.parent` by value here would snapshot the old - // pointer and over-deref it if a re-entrant callback set_parent()s. - // Capture the laundered `*mut Self` and read `.parent` at guard - // execution instead — the `black_box` above also ensures the guard's - // read is not folded with any pre-call load. + // process_send. Capturing `self.parent` by value here would snapshot + // the old pointer and over-deref it if a re-entrant callback + // set_parent()s; read `.parent` at guard execution instead. let _g = scopeguard::guard(this, |s| Self::r_deref(s)); - if let Some(err) = status.to_error(sys::Tag::write) { - log!("onWrite() = {}", bstr::BStr::new(err.name())); - Self::r(this).last_write_result = WriteResult::Err(err.clone()); - Self::r_on_error(this, err); - core::hint::black_box(this); - // `close()`, not `close_without_reporting()`: the parent must still - // observe `on_close` after `on_error` (the `PosixStreamingWriter` - // contract). FileSink's stream teardown only runs from `on_close`. - Self::r(this).close(); + if mem::take(&mut Self::r(this).lent_len) > 0 { + let returned = match Self::r(this).source.as_mut() { + Some(Source::Pipe(pipe)) => pipe.take_lent_buffer(), + _ => None, + }; + let cursor = mem::take(&mut Self::r(this).lent_cursor); + // A list that does not come back (a thread that could not be + // stopped still has it) leaves nothing for the cursor to be in. + Self::r(this).current_payload = match returned { + Some(list) if cursor <= list.len() => StreamBuffer { list, cursor }, + _ => StreamBuffer::default(), + }; + } + // Closed with the write still out; `close()` told the parent already. + // A write that could not be cancelled any more ends with its own result. + if Self::r(this).source.is_none() { + Self::r(this).current_payload.reset(); return; } + let submitted = Self::r(this).current_payload.size(); + let written = match result { + sys::Result::Err(err) if err.get_errno() == sys::E::ECANCELED => { + Self::r(this).current_payload.reset(); + return; + } + sys::Result::Err(err) => { + log!("onWrite() = {}", bstr::BStr::new(err.name())); + Self::fail_send(this, err); + return; + } + sys::Result::Ok(written) => written.min(submitted), + }; - // success means that we send all the data inside current_payload - let written = Self::r(this).current_payload.size(); + Self::r(this).current_payload.wrote(written); + if Self::r(this).current_payload.is_not_empty() { + // The source took only part of it; the rest goes out next. + Self::r_on_write(this, written, WriteStatus::Pending); + core::hint::black_box(this); + Self::r(this).send_current_payload(); + return; + } Self::r(this).current_payload.reset(); // if we dont have more outgoing data we report done in onWrite @@ -2219,211 +2094,132 @@ impl WindowsStreamingWriter { // process pending outgoing data if any Self::r(this).process_send(); - // TODO: should we report writable? - if Parent::HAS_ON_WRITABLE { - // SAFETY: parent BACKREF valid. - unsafe { Parent::on_writable(Self::r(this).parent()) }; - } - } - - extern "C" fn on_fs_write_complete(fs: *mut uv::fs_t) { - // SAFETY: libuv fs_cb — `fs` is the `uv_fs_t` field at offset 0 of a - // boxed `source::File`; `from_fs_callback` snapshots `result`/`data` - // and recovers `&mut File` via container_of. Single-threaded dispatch, - // no other Rust borrow of the boxed `File` is live. - let (file, result, parent_ptr) = unsafe { crate::source::File::from_fs_callback(fs) }; - let was_canceled = result.int() == uv::UV_ECANCELED as i64; - - // ALWAYS complete first — the boxed `source::File` outlives this - // callback (detach()/close() gates free). - file.complete(was_canceled); - - // If detached, file may be closing (owned fd) or just stopped (non-owned fd). - // The deref to balance processSend's ref was already done in close(). - if parent_ptr.is_null() { - // owns_fd detach() path: complete() already kicked off start_close() - // (state == Closing) and on_close_complete will heap::take the Box. - // !owns_fd close() path: complete() left state == Deinitialized and - // nothing else will reclaim the Box; this callback is the sole - // remaining owner, so free it here. - if file.state == crate::source::FileState::Deinitialized { - // SAFETY: `file` is the Box leaked in close() via into_raw. - drop(unsafe { bun_core::heap::take(core::ptr::from_mut(file)) }); - } - return; - } + // SAFETY: parent BACKREF valid. + unsafe { Parent::on_writable(Self::r(this).parent()) }; + } + + /// A write failed, or could not be started. + fn fail_send(this: *mut Self, err: sys::Error) { + Self::r(this).last_write_result = WriteResult::Err(err.clone()); + // Before the parent hears, as in `PosixStreamingWriter`: what it calls + // from `on_error` finds a writer that is done, not a write in flight. + Self::r(this).is_done = true; + Self::r(this).current_payload.reset(); + Self::r(this).outgoing.reset(); + Self::r_on_error(this, err); + core::hint::black_box(this); + // `close()`, not `close_without_reporting()`: the parent must still + // observe `on_close` after `on_error` (the `PosixStreamingWriter` + // contract). FileSink's stream teardown only runs from `on_close`. + Self::r(this).close(); + } - // PORT_NOTES_PLAN R-2: launder `*this`. `this.on_write_complete()` / - // `this.close()` below both reach `Parent::on_write`/`on_close` → - // FileSink → JS, which can `self.writer.with_mut(|w| w.end()/close())` - // forming a fresh aliased `&mut WindowsStreamingWriter`. The - // `callback_ctx` `&mut` itself isn't a fn parameter (no `noalias` - // attribute), but `this.on_write_complete(..)` *passes* `&mut self` - // and that callee parameter IS `noalias` — `on_write_complete` is - // already laundered, so the success path is covered. The error path - // (`close()` → `on_error(this.parent())` → guard deref) reads - // `this.parent` after re-entry; route those through a black-boxed raw - // ptr so any inlined call chain cannot store-forward across the JS - // re-entry. - // SAFETY: data was set to `self as *mut Self` in process_send(); libuv - // invokes this callback on the single-threaded event loop with no other - // Rust borrow of `*this` live, so this is the sole access path. - let this: *mut Self = core::hint::black_box(parent_ptr.cast::()); - - if was_canceled { - // Canceled write - reset buffers and deref to balance process_send ref + /// Hand `current_payload` to the source. + fn send_current_payload(&mut self) { + // The error arm calls `Parent::on_error`, which + // re-enters JS and may reach this writer through a fresh `&mut Self`. + let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self)); + let Some(source) = Self::r(this).source.as_mut() else { + Self::fail_send(this, sys::Error::from_code(sys::E::EPIPE, sys::Tag::pipe)); + return; + }; + let submitted = match source { + // A write this pipe could not recall has to own its bytes: the + // payload is lent to it, and `on_write_result` takes it back. + Source::Pipe(pipe) if pipe.writes_outlive_cancel() => { + // The list and its cursor leave together: what stays behind + // is an empty buffer, whose size is 0. + let StreamBuffer { list, cursor } = mem::take(&mut Self::r(this).current_payload); + let lent_len = list.len() - cursor; + match pipe.write_lent(list, cursor, this, Self::on_write_result) { + Ok(()) => { + Self::r(this).lent_len = lent_len; + Self::r(this).lent_cursor = cursor; + sys::Result::Ok(()) + } + Err((err, list)) => { + if cursor <= list.len() { + Self::r(this).current_payload = StreamBuffer { list, cursor }; + } + sys::Result::Err(err) + } + } + } + // SAFETY: `current_payload` is not touched until the write's result + // arrives, and the parent ref taken below keeps the writer (a field + // of the parent) alive until then. `(*this)` raw deref so the + // payload borrow does not overlap the `source` borrow. + source => unsafe { + submit_write( + source, + (*this).current_payload.slice(), + Refusal::Returned, + this, + Self::on_write_result, + ) + }, + }; + if let sys::Result::Err(err) = submitted { Self::r(this).current_payload.reset(); - Self::r_deref(this); + Self::fail_send(this, err); return; } - - if let Some(err) = result.to_error(sys::Tag::write) { - // deref to balance process_send ref — read `.parent` LAZILY at - // guard execution, not eagerly, in case - // close()/on_error re-enter and swap the parent pointer. - let _g = scopeguard::guard(this, |s| Self::r_deref(s)); - // close() may re-enter JS — every post-call `r(this)` reborrow - // reloads (laundered raw ptr, no noalias). - Self::r(this).close(); - core::hint::black_box(this); - Self::r_on_error(this, err); - return; + // The matching deref is in `on_write_result`, which a cancelled write + // reaches too, or in `abandon`. + // SAFETY: parent is BACKREF set via set_parent; valid while writer + // alive, and it stays where it is while it is referenced. + unsafe { + Parent::ref_(Self::r(this).parent()); + WriteOut::insert(&raw mut (*this).out); } - - // on_write_complete handles the deref (and is itself laundered). - Self::r(this).on_write_complete(uv::ReturnCode::zero()); + Self::r(this).last_write_result = WriteResult::Pending(0); } /// this tries to send more data returning if we are writable or not after this fn process_send(&mut self) { log!("processSend"); - // PORT_NOTES_PLAN R-2: same noalias re-entry hazard as - // `on_write_complete` above. The three synchronous-error arms call - // `Parent::on_error` (re-enters JS via FileSink::on_error → promise - // reject; user callback may `writer.with_mut(|w| w.end())`/`.close()` - // forming a fresh aliased `&mut Self`) and then read - // `self.{source, is_done, closed_without_reporting}` via `close()`. - // With `&mut self` `noalias`, LLVM may forward pre-`on_error` field - // loads across the call. Launder this entry point too — it is reached - // from `on_write_complete` with a fresh `&mut`. - let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self)); - // `this` is the only access path to `*self` for the rest of this - // function; every `r(this)` reborrow is sole-aliased on the JS thread. - if Self::r(this).current_payload.is_not_empty() { + if self.current_payload.is_not_empty() || self.lent_len > 0 { // we have some pending async request, the next outgoing data will be processed after this finish - Self::r(this).last_write_result = WriteResult::Pending(0); + self.last_write_result = WriteResult::Pending(0); return; } - let bytes_len = Self::r(this).outgoing.slice().len(); // nothing todo (we assume we are writable until we try to write something) - if bytes_len == 0 { - Self::r(this).last_write_result = WriteResult::Wrote(0); + if self.outgoing.is_empty() { + self.last_write_result = WriteResult::Wrote(0); return; } - // BORROW_PARAM (raw-ptr break): match arms mutate `*this` while - // borrowing into `(*this).source`. The boxed `File`/`Pipe` are separate - // heap allocations, so a `*mut` snapshot is provenance-disjoint. - let (file_raw, stream_raw): (*mut crate::source::File, *mut uv::uv_stream_t) = - match Self::r(this).source.as_mut() { - None => { - let err = sys::Error::from_code(sys::E::PIPE, sys::Tag::pipe); - Self::r(this).last_write_result = WriteResult::Err(err.clone()); - Self::r_on_error(this, err); - core::hint::black_box(this); - // See `on_write_complete`: the parent must get `on_close`. - Self::r(this).close(); - return; - } - Some(Source::SyncFile(_)) => { - panic!("sync_file pipe write should not be reachable"); - } - Some(Source::File(f)) => (f.as_mut() as *mut _, core::ptr::null_mut()), - Some(s) => (core::ptr::null_mut(), s.to_stream()), - }; - // current payload is empty we can just swap with outgoing - { - let s = Self::r(this); - mem::swap(&mut s.current_payload, &mut s.outgoing); - } - // Snapshot the post-swap payload into an owned `uv_buf_t` (ptr + len, - // `Copy`); the underlying storage is not reallocated until - // `on_write_complete` resets it, and libuv reads it via this same - // ptr/len. `current_payload` was just swapped from `outgoing`, so its - // slice length equals `bytes_len` captured above. - let write_buf = { - let s = Self::r(this); - debug_assert_eq!(s.current_payload.slice().len(), bytes_len); - uv::uv_buf_t::init(s.current_payload.slice()) - }; + mem::swap(&mut self.current_payload, &mut self.outgoing); + self.send_current_payload(); + } - if !file_raw.is_null() { - // SAFETY: see raw-ptr break note above. - let file = unsafe { &mut *file_raw }; - // StreamingWriter ensures current_payload blocks concurrent writes - debug_assert!(file.can_start()); - - file.fs.data = this.cast::(); - file.prepare(); - Self::r(this).write_buffer = write_buf; - - // SAFETY: file is fully initialized; libuv stores the cb and fires - // it on the event loop. parent BACKREF valid. `(*this)` raw deref - // (not `r()`) so the `&write_buffer` borrow is not invalidated by a - // sibling Unique tag from the `parent()` arg under Stacked Borrows. - if let Some(err) = unsafe { - uv::uv_fs_write( - Parent::loop_((*this).parent()), - &mut file.fs, - file.file, - &(*this).write_buffer, - 1, - -1, - Some(Self::on_fs_write_complete), - ) - } - .to_error(sys::Tag::write) - { - file.complete(false); - Self::r(this).last_write_result = WriteResult::Err(err.clone()); - Self::r_on_error(this, err); - core::hint::black_box(this); - // See `on_write_complete`: the parent must get `on_close`. - Self::r(this).close(); - return; - } - } else { - // enqueue the write - Self::r(this).write_buffer = write_buf; - // SAFETY: `(*this)` raw deref (not `r()`) so the two field borrows - // (`write_req`, `write_buffer`) coexist under Stacked Borrows. The - // closure's `(*p)` is the libuv callback ctx — `p` is `this` and - // libuv invokes on the loop thread with no other Rust borrow live. - if let Some(err) = unsafe { - (*this) - .write_req - .write(stream_raw, &(*this).write_buffer, this, |p, s| { - (*p).on_write_complete(s) - }) - } - .to_error(sys::Tag::write) - { - Self::r(this).last_write_result = WriteResult::Err(err.clone()); - Self::r_on_error(this, err); - core::hint::black_box(this); - // See `on_write_complete`: the parent must get `on_close`. - Self::r(this).close(); - return; - } + /// `force_sync`: everything is written before this returns. + fn write_sync( + &mut self, + buffer_u8: Option<&[u8]>, + buffer_u16: Option<&[u16]>, + kind: WriteKind, + ) -> WriteResult { + let Some(source) = self.source.as_mut() else { + return WriteResult::Err(sys::Error::from_code(sys::E::EPIPE, sys::Tag::pipe)); + }; + let written = match (source, kind) { + // A console takes UTF-16: no detour through UTF-8. + (Source::Tty(tty), WriteKind::Utf16) => tty.try_write_utf16(buffer_u16.unwrap()), + (Source::Tty(tty), WriteKind::Latin1) => tty.try_write_latin1(buffer_u8.unwrap()), + (source, _) => match self.outgoing.write_or_fallback(buffer_u8, buffer_u16, kind) { + Err(_) => sys::Result::Err(sys::Error::oom()), + Ok(bytes) => write_blocking(source, bytes), + }, + }; + self.outgoing.reset(); + match written { + sys::Result::Err(err) => WriteResult::Err(err), + sys::Result::Ok(0) => WriteResult::Done(0), + sys::Result::Ok(wrote) => WriteResult::Wrote(wrote), } - // Ref the parent to prevent it from being freed while the async - // write is in flight. The matching deref is in on_write_complete - // or on_fs_write_complete. - // SAFETY: parent is BACKREF set via set_parent; valid while writer alive. - unsafe { Parent::ref_(Self::r(this).parent()) }; - Self::r(this).last_write_result = WriteResult::Pending(0); } fn write_internal_u8(&mut self, buffer: &[u8], kind: WriteKind) -> WriteResult { @@ -2431,39 +2227,8 @@ impl WindowsStreamingWriter { return WriteResult::Done(0); } - if matches!(self.source, Some(Source::SyncFile(_))) { - let result = (|| { - let remain = match self.outgoing.write_or_fallback(Some(buffer), None, kind) { - Ok(r) => r, - Err(_) => return WriteResult::Err(sys::Error::oom()), - }; - let initial_len = remain.len(); - let mut remain = remain; - let fd = Fd::from_uv(match &self.source { - Some(Source::SyncFile(f)) => f.file, - _ => unreachable!(), - }); - - while remain.len() > 0 { - match sys::write(fd, remain) { - sys::Result::Err(err) => return WriteResult::Err(err), - sys::Result::Ok(wrote) => { - remain = &remain[wrote..]; - if wrote == 0 { - break; - } - } - } - } - - let wrote = initial_len - remain.len(); - if wrote == 0 { - return WriteResult::Done(wrote); - } - WriteResult::Wrote(wrote) - })(); - self.outgoing.reset(); - return result; + if self.force_sync { + return self.write_sync(Some(buffer), None, kind); } let had_buffered_data = self.outgoing.is_not_empty(); @@ -2487,43 +2252,8 @@ impl WindowsStreamingWriter { return WriteResult::Done(0); } - if matches!(self.source, Some(Source::SyncFile(_))) { - let result = (|| { - let remain = - match self - .outgoing - .write_or_fallback(None, Some(buffer), WriteKind::Utf16) - { - Ok(r) => r, - Err(_) => return WriteResult::Err(sys::Error::oom()), - }; - let initial_len = remain.len(); - let mut remain = remain; - let fd = Fd::from_uv(match &self.source { - Some(Source::SyncFile(f)) => f.file, - _ => unreachable!(), - }); - - while remain.len() > 0 { - match sys::write(fd, remain) { - sys::Result::Err(err) => return WriteResult::Err(err), - sys::Result::Ok(wrote) => { - remain = &remain[wrote..]; - if wrote == 0 { - break; - } - } - } - } - - let wrote = initial_len - remain.len(); - if wrote == 0 { - return WriteResult::Done(wrote); - } - WriteResult::Wrote(wrote) - })(); - self.outgoing.reset(); - return result; + if self.force_sync { + return self.write_sync(None, Some(buffer), WriteKind::Utf16); } let had_buffered_data = self.outgoing.is_not_empty(); @@ -2570,31 +2300,15 @@ impl WindowsStreamingWriter { self.is_done = true; if !self.has_pending_data() { - if !self.owns_fd { - return; - } self.close(); } } } -#[cfg(windows)] -impl Drop for WindowsBufferedWriter { - fn drop(&mut self) { - // A parent dropping an open writer (e.g. `end()` deferred the close for a - // pending write): hand the handle to libuv like any close, minus the - // report to the parent that is going away. - self.close_without_reporting(); - } -} - #[cfg(windows)] impl Drop for WindowsStreamingWriter { fn drop(&mut self) { - // Close the pipe first to cancel any in-flight writes before - // freeing the buffers they reference. self.close_without_reporting(); - // outgoing & current_payload dropped automatically } } @@ -2618,17 +2332,17 @@ pub type StreamingWriter

= WindowsStreamingWriter

; // // The `*WriterParent` traits are monomorphic function tables whose every // method is `unsafe fn(this: *mut Self, ..)` -// that derefs the BACKREF and forwards to an inherent method. Every concrete -// parent (FileSink, Terminal, WindowsNamedPipe, shell IOWriter, -// StaticPipeWriter) was hand-stamping the same triple of cfg-gated impls -// (POSIX + WindowsWriterParent + Windows{Streaming,Buffered}WriterParent), -// differing only in: +// that derefs the BACKREF and forwards to an inherent method. The impls of the +// concrete parents (FileSink, shell IOWriter, StaticPipeWriter) differ only +// in: // (a) the inherent-method names the vtable forwards to, // (b) how the callback is dispatched off `*mut Self` — as `&mut`, `&`, or // a raw-ptr method call (re-entrancy under Stacked/Tree Borrows — see // `borrow = shared` / `borrow = ptr` callers), -// (c) the `event_loop` / `loop_` / refcount accessor expressions. -// These macros stamp that triple once per parent. +// (c) the `event_loop` / `loop_` / refcount accessor expressions. The +// refcount accessors are used on Windows only, where a write in flight +// borrows the parent's bytes until its completion is dequeued. +// These macros stamp the impls once per parent. // // `borrow = mut` → bodies form `&mut *this` (unique access for the // callback's duration; the writer never holds @@ -2652,8 +2366,6 @@ pub type StreamingWriter

= WindowsStreamingWriter

; #[doc(hidden)] pub mod __parent_macro { pub use ::bun_sys::Error as SysError; - #[cfg(windows)] - pub use ::bun_sys::windows::libuv::Loop as UvLoop; pub use ::bun_uws_sys::Loop as UwsLoop; } @@ -2677,7 +2389,6 @@ macro_rules! impl_streaming_writer_parent { on_close = $on_close:ident, event_loop = |$el_this:ident| $el:expr, uws_loop = |$uws_this:ident| $uws:expr, - uv_loop = |$uv_this:ident| $uv:expr, ref_ = |$ref_this:ident| $ref_:expr, deref = |$deref_this:ident| $deref:expr, ) => { @@ -2727,11 +2438,11 @@ macro_rules! impl_streaming_writer_parent { #[cfg(windows)] impl $($gen)* $crate::pipe_writer::WindowsWriterParent for $Ty { #[inline] - unsafe fn loop_(this: *mut Self) -> *mut $crate::pipe_writer::__parent_macro::UvLoop { + unsafe fn loop_(this: *mut Self) -> *mut $crate::pipe_writer::__parent_macro::UwsLoop { // SAFETY: BACKREF set via `set_parent`; shared-only read. - let $uv_this = this; + let $uws_this = this; #[allow(unused_unsafe)] - unsafe { $uv } + unsafe { $uws } } #[inline] unsafe fn ref_(this: *mut Self) { @@ -2752,7 +2463,6 @@ macro_rules! impl_streaming_writer_parent { #[cfg(windows)] impl $($gen)* $crate::pipe_writer::WindowsStreamingWriterParent for $Ty { // Same body as POSIX `on_ready`. - const HAS_ON_WRITABLE: bool = true; #[inline] unsafe fn on_write(this: *mut Self, amount: usize, status: $crate::WriteStatus) { // SAFETY: BACKREF set via `set_parent`; see borrow-mode note. @@ -2795,8 +2505,7 @@ macro_rules! impl_streaming_writer_parent { }; } -/// Stamp `PosixBufferedWriterParent` + `WindowsWriterParent` + -/// `WindowsBufferedWriterParent` for a parent type. See module comment above. +/// Stamp `BufferedWriterParent` for a parent type. See module comment above. #[macro_export] macro_rules! impl_buffered_writer_parent { (@borrow mut $p:expr) => { &mut *$p }; @@ -2811,12 +2520,10 @@ macro_rules! impl_buffered_writer_parent { on_close = $on_close:ident, get_buffer = |$gb_this:ident| $gb:expr, event_loop = |$el_this:ident| $el:expr, - uv_loop = |$uv_this:ident| $uv:expr, ref_ = |$ref_this:ident| $ref_:expr, deref = |$deref_this:ident| $deref:expr, ) => { - #[cfg(not(windows))] - impl $($gen)* $crate::pipe_writer::PosixBufferedWriterParent for $Ty { + impl $($gen)* $crate::pipe_writer::BufferedWriterParent for $Ty { const POLL_OWNER_TAG: $crate::PollTag = $poll_tag; #[inline] unsafe fn on_write(this: *mut Self, amount: usize, status: $crate::WriteStatus) { @@ -2830,7 +2537,6 @@ macro_rules! impl_buffered_writer_parent { // SAFETY: see on_write. unsafe { ($crate::impl_buffered_writer_parent!(@borrow $borrow this)).$on_error(&err) }; } - const HAS_ON_CLOSE: bool = true; #[inline] unsafe fn on_close(this: *mut Self) { // SAFETY: see on_write. @@ -2850,60 +2556,23 @@ macro_rules! impl_buffered_writer_parent { #[allow(unused_unsafe)] unsafe { $el } } - } - - #[cfg(windows)] - impl $($gen)* $crate::pipe_writer::WindowsWriterParent for $Ty { - #[inline] - unsafe fn loop_(this: *mut Self) -> *mut $crate::pipe_writer::__parent_macro::UvLoop { - // SAFETY: BACKREF set via `set_parent`; shared-only read. - let $uv_this = this; - #[allow(unused_unsafe)] - unsafe { $uv } - } + #[cfg(windows)] #[inline] unsafe fn ref_(this: *mut Self) { - // SAFETY: see loop_. Intrusive refcount bump. + // SAFETY: see on_write. Intrusive refcount bump. let $ref_this = this; #[allow(unused_unsafe)] unsafe { $ref_ }; } + #[cfg(windows)] #[inline] unsafe fn deref(this: *mut Self) { - // SAFETY: see loop_. May free `this`. + // SAFETY: see on_write. May free `this`. let $deref_this = this; #[allow(unused_unsafe)] unsafe { $deref }; } } - - #[cfg(windows)] - impl $($gen)* $crate::pipe_writer::WindowsBufferedWriterParent for $Ty { - #[inline] - unsafe fn on_write(this: *mut Self, amount: usize, status: $crate::WriteStatus) { - // SAFETY: BACKREF set via `set_parent`; see borrow-mode note. - unsafe { ($crate::impl_buffered_writer_parent!(@borrow $borrow this)).$on_write(amount, status) }; - } - #[inline] - unsafe fn on_error(this: *mut Self, err: $crate::pipe_writer::__parent_macro::SysError) { - // SAFETY: see on_write. - unsafe { ($crate::impl_buffered_writer_parent!(@borrow $borrow this)).$on_error(&err) }; - } - const HAS_ON_CLOSE: bool = true; - #[inline] - unsafe fn on_close(this: *mut Self) { - // SAFETY: see on_write. - unsafe { ($crate::impl_buffered_writer_parent!(@borrow $borrow this)).$on_close() }; - } - #[inline] - unsafe fn get_buffer<'a>(this: *mut Self) -> &'a [u8] { - // SAFETY: see on_write. - let $gb_this = this; - #[allow(unused_unsafe)] - unsafe { $gb } - } - const HAS_ON_WRITABLE: bool = false; - } }; // Public entry — generic parent. diff --git a/src/io/keep_alive.rs b/src/io/keep_alive.rs index b45fbb8a007b..98a4c9e40235 100644 --- a/src/io/keep_alive.rs +++ b/src/io/keep_alive.rs @@ -1,9 +1,4 @@ -//! Single cross-platform `KeepAlive`. -//! -//! The few methods that diverge per platform keep their behaviour via -//! `#[cfg]` arms inline below — no caller-visible contract changes (all -//! external users go through `bun_io::KeepAlive` and only touch the -//! identical-signature methods). +//! `KeepAlive`: a ref on the event loop that is either held or not. use crate::EventLoopCtx; use crate::posix_event_loop::js_vm_ctx; @@ -45,10 +40,7 @@ impl KeepAlive { return; } self.status = Status::Inactive; - #[cfg(not(windows))] event_loop_ctx.loop_unref(); - #[cfg(windows)] - event_loop_ctx.loop_sub_active(1); } /// Prevent a poll from keeping the process alive on the next tick. @@ -57,11 +49,7 @@ impl KeepAlive { return; } self.status = Status::Inactive; - // vm.pending_unref_counter +|= 1; - #[cfg(not(windows))] event_loop_ctx.increment_pending_unref_counter(); - #[cfg(windows)] - event_loop_ctx.loop_dec(); } /// Allow a poll to keep the process alive. diff --git a/src/io/lib.rs b/src/io/lib.rs index af271cd87741..251b0c047704 100644 --- a/src/io/lib.rs +++ b/src/io/lib.rs @@ -5,8 +5,10 @@ // ════════════════════════════════════════════════════════════════════════════ // Loop / Poll / Waker / Closer / FilePoll-vtable / heap / pipes / MaxBuf / -// openForWriting / PipeReader / PipeWriter compile on POSIX. `source` and the -// Windows*Reader/Writer impls are `#[cfg(windows)]`-gated (libuv-only). +// openForWriting / PipeReader / PipeWriter are shared. `windows`, `source`, the +// Windows half of `BufferedReader` and the `Windows*Writer`s are +// `#[cfg(windows)]`-gated: pipes, consoles and files there complete through the +// loop's port instead of being polled. // ════════════════════════════════════════════════════════════════════════════ #![allow(unsafe_op_in_unsafe_fn)] @@ -28,15 +30,10 @@ pub use error::Error; // crate, `EventLoopHandle` is `EventLoopCtx` (the by-value `{kind, owner}` // pair) and the seam is type-checked. -#[cfg(windows)] -pub mod windows_event_loop; - -// `posix_event_loop` also defines the *shared* event-loop scaffolding -// (`EventLoopCtx`, `AllocatorType`, `Owner`, `Flags`, `PollTag`, `Store`, -// `OpaqueCallback`); `windows_event_loop` re-uses those types and only -// overrides `FilePoll`/`KeepAlive`/`Closer`/`Loop`/`Waker`. The platform- -// specific bits inside (kqueue/epoll wakers, fd polling) are individually -// `#[cfg(unix)]`-gated so the module still compiles on Windows. +// `posix_event_loop` is the event-loop scaffolding of every platform +// (`EventLoopCtx`, `FilePoll`, `Store`, `Owner`, `Flags`, `PollTag`); only the +// registration primitive inside differs (epoll, kqueue, or the loop's AFD +// socket poll on Windows). mod keep_alive; pub mod posix_event_loop; pub use keep_alive::KeepAlive; @@ -117,8 +114,8 @@ pub mod parent_death_watchdog { static JOB_ASSIGNED: AtomicBool = AtomicBool::new(false); /// Self-assign to a kill-on-close Job Object without `SILENT_BREAKAWAY_OK`, - /// so membership is inherited by every descendant (unlike libuv's spawn - /// Job). Idempotent; handle leaked for process lifetime. Used by + /// so membership is inherited by every descendant (unlike the Job spawned + /// children are put in). Idempotent; handle leaked for process lifetime. Used by /// `--no-orphans` and unconditionally by `bun --filter`/`--parallel`. #[cold] #[inline(never)] @@ -128,7 +125,7 @@ pub mod parent_death_watchdog { } // SAFETY: Win32 FFI; null args are documented-valid (anonymous Job). unsafe { - let job = windows::CreateJobObjectA(core::ptr::null_mut(), core::ptr::null()); + let job = windows::CreateJobObjectW(core::ptr::null_mut(), core::ptr::null()); if !job.is_null() { let mut jeli: windows::JOBOBJECT_EXTENDED_LIMIT_INFORMATION = bun_core::ffi::zeroed(); @@ -155,7 +152,8 @@ pub mod parent_death_watchdog { /// us via `CreateProcessAsUser`), skip the parent watch best-effort; the /// Job Object half already covers descendants. unsafe fn arm_parent_watch() { - const ERROR_INVALID_PARAMETER: u32 = 87; + const ERROR_INVALID_PARAMETER: u32 = + bun_windows_sys::Win32Error::INVALID_PARAMETER.0 as u32; let ppid = getppid(); if ppid == 0 { return; @@ -204,9 +202,7 @@ pub mod parent_death_watchdog { } } - /// `InheritedFromUniqueProcessId` via direct ntdll call. Not - /// `uv_os_getppid`: libuv loads `NtQueryInformationProcess` lazily in - /// `uv__winapi_init`, and `enable()` runs from `main()` before that. + /// `InheritedFromUniqueProcessId`. fn getppid() -> u32 { // SAFETY: out-param is a valid `PROCESS_BASIC_INFORMATION`; the pseudo // handle needs no access rights. @@ -269,34 +265,8 @@ pub use parent_death_watchdog as ParentDeathWatchdog; // ─── public surface (was bun_io's crate root) ────────────────────────────── -#[cfg(not(windows))] -pub use posix_event_loop::{FilePoll, Loop}; -#[cfg(windows)] -pub use windows_event_loop::{FilePoll, Loop}; - -/// Project a `*mut bun_uws_sys::Loop` (the uws wrapper — `PosixLoop` / -/// `WindowsLoop`) to the platform-native [`Loop`] (`us_loop_t*` on POSIX, -/// `uv_loop_t*` on Windows). -/// -/// On POSIX `bun_io::Loop` **is** `bun_uws_sys::Loop` (nominal identity), so -/// this is the identity. On Windows the wrapper stores the libuv loop in its -/// `uv_loop` field — set once in C at `us_create_loop` and immutable -/// thereafter — which we project here so callers needn't open an `unsafe` -/// block per site just to read a set-once field. -#[inline] -pub fn uws_to_native(uws: *mut bun_uws_sys::Loop) -> *mut Loop { - #[cfg(not(windows))] - { - uws - } - #[cfg(windows)] - // SAFETY: `uws` is the live `us_loop` allocated by `us_create_loop`; - // `uv_loop` is initialised in C before any Rust caller can observe the - // handle and is never mutated. - { - unsafe { (*uws).uv_loop } - } -} +pub use bun_uws_sys::Loop; +pub use posix_event_loop::{FilePoll, Store}; pub use posix_event_loop::{AllocatorType, Owner, PollTag, get_vm_ctx, js_vm_ctx}; @@ -304,13 +274,7 @@ pub type OpaqueCallback = unsafe extern "C" fn(*mut core::ffi::c_void); // At crate root so the per-method `$crate::__EventLoopCtx__*` type aliases the // macro emits (and the impl-macro reads back) actually resolve from impl -// crates. `Store`/`FilePoll` here are the *platform* re-exports above. -// -// `platform_event_loop_ptr` is typed `*mut bun_uws_sys::Loop` (the uws -// wrapper — `PosixLoop`/`WindowsLoop`), NOT the cfg-aliased `crate::Loop` -// re-export. On POSIX those coincide, but on Windows `crate::Loop` is the raw -// `uv_loop_t` whereas the impl bodies -// (`VirtualMachine::uws_loop` / `MiniEventLoop::loop_ptr`) hand back the wrapper. +// crates. bun_dispatch::link_interface! { pub EventLoopCtx[Js, Mini] { fn platform_event_loop_ptr() -> *mut bun_uws_sys::Loop; @@ -356,8 +320,8 @@ impl EventLoopCtx { // `FilePoll::activate`/`deactivate`, `unregister` — that consumes the // borrow before returning and never re-enters `EventLoopCtx`, so no two // `&mut Loop` ever coexist. Widened from impl-private to crate-private so - // `posix_event_loop`/`windows_event_loop` route their N identical - // `ctx.platform_event_loop()` derefs through this single accessor. + // `posix_event_loop` routes its N identical `ctx.platform_event_loop()` + // derefs through this single accessor. #[inline] fn loop_mut(&self) -> &'static mut bun_uws_sys::Loop { // SAFETY: per-thread set-once pointer (the uws loop singleton); the @@ -371,8 +335,8 @@ impl EventLoopCtx { /// another `&mut Store` (or a `&mut FilePoll` that lives inside the inline /// hive buffer) is live. Every in-crate caller is a leaf op that decays /// any conflicting `&mut FilePoll` to a raw slot pointer first - /// (`deinit_possibly_defer`) or holds none (`init_with_owner`, - /// `alloc_file_poll`), so no two `&mut Store` ever coexist. + /// (`deinit_possibly_defer`) or holds none (`alloc_file_poll`), so no two + /// `&mut Store` ever coexist. #[inline] fn file_polls_mut(&self) -> &'static mut Store { // SAFETY: per-thread set-once pointer (`BackRef`-shaped); the event @@ -382,6 +346,7 @@ impl EventLoopCtx { unsafe { &mut *self.file_polls_ptr() } } /// Claims the per-loop pipe-read scratch; `None` while a read further up the stack holds it. + #[cfg(unix)] #[inline] fn claim_pipe_read_scratch(&self) -> Option> { // SAFETY: per-thread scratch owned by the VM/Mini loop, which outlives every read. @@ -392,16 +357,10 @@ impl EventLoopCtx { self.loop_mut().ref_(); } #[inline] - #[cfg(not(windows))] pub(crate) fn loop_unref(&self) { self.loop_mut().unref(); } #[inline] - #[cfg(windows)] - pub(crate) fn loop_dec(&self) { - self.loop_mut().dec(); - } - #[inline] pub(crate) fn loop_add_active(&self, n: u32) { self.loop_mut().add_active(n); } @@ -409,14 +368,12 @@ impl EventLoopCtx { pub(crate) fn loop_sub_active(&self, n: u32) { self.loop_mut().sub_active(n); } - #[cfg(not(windows))] #[inline] pub(crate) fn alloc_file_poll(&self, value: FilePoll) -> core::ptr::NonNull { self.file_polls_mut().get_init(value) } #[inline] - #[cfg(not(windows))] pub(crate) fn is_js(&self) -> bool { self.is(EventLoopCtxKind::Js) } @@ -424,24 +381,14 @@ impl EventLoopCtx { pub fn loop_(&self) -> *mut bun_uws_sys::Loop { self.platform_event_loop_ptr() } - /// Platform-native loop pointer (`us_loop_t*` / `uv_loop_t*`); see - /// [`uws_to_native`]. - #[inline] - pub fn native_loop(&self) -> *mut Loop { - uws_to_native(self.platform_event_loop_ptr()) - } } -#[cfg(not(windows))] -pub use posix_event_loop::Store; -#[cfg(windows)] -pub use windows_event_loop::Store; /// Mirrors posix_event_loop::Flags. pub use posix_event_loop::Flags as PollFlag; /// Mirrors poll kind enum used by process.rs. pub use posix_event_loop::Flags as PollKind; -/// `file_poll` module — real one lives in {posix,windows}_event_loop.rs. +/// `file_poll` module — the real one lives in `posix_event_loop.rs`. pub mod file_poll { pub use super::Store; pub use super::posix_event_loop::{Flags, FlagsSet}; @@ -451,8 +398,6 @@ pub mod file_poll { #[path = "heap.rs"] pub mod heap; -// `source.rs` is Windows-only (libuv pipe/tty/file wrappers). On POSIX the -// `Source` type is never constructed; callers are themselves `#[cfg(windows)]`. #[path = "MaxBuf.rs"] pub mod max_buf; #[path = "openForWriting.rs"] @@ -468,6 +413,8 @@ pub use pipe_read_scratch::{PipeReadScratch, PipeReadScratchGuard}; #[cfg(windows)] #[path = "source.rs"] pub mod source; +#[cfg(windows)] +pub mod windows; #[path = "write.rs"] pub mod write; @@ -599,7 +546,7 @@ macro_rules! __impl_buffered_reader_parent_body { unsafe { $re } } #[allow(unused_unsafe, clippy::macro_metavars_in_unsafe)] - unsafe fn loop_($l_this: *mut Self) -> *mut $crate::pipe_reader::Loop { + unsafe fn loop_($l_this: *mut Self) -> *mut $crate::Loop { unsafe { $lp } } #[allow(unused_unsafe, clippy::macro_metavars_in_unsafe)] @@ -653,14 +600,13 @@ macro_rules! buffered_reader_parent_link { } }; } +#[cfg(windows)] +pub use pipe_writer::abandon_writes_out; pub use pipe_writer::{BufferedWriter, StreamBuffer, StreamingWriter, WriteResult, WriteStatus}; #[cfg(windows)] pub use source::Source; -// Stub for never-constructed-on-POSIX `Source` so cross-platform sigs -// (`Option`) typecheck. - -pub use pipe_reader::{BufferedReader, BufferedReaderParent, PosixFlags}; +pub use pipe_reader::{BufferedReader, BufferedReaderParent, ReaderFlags}; pub use open_for_writing_mod::{open_for_writing, open_for_writing_impl}; @@ -764,7 +710,7 @@ type EventType = KEvent; // The bare-kqueue/epoll request loop that backs // `Bun.file(path).text()` / `Bun.write()` & friends (and nothing else; see the // crate doc above). NOT the main event loop. Renamed from `Loop` so this -// crate's `Loop` (= `posix_event_loop::Loop` = the uws `us_loop_t` everyone +// crate's `Loop` (= `bun_uws_sys::Loop`, the uws `us_loop_t` everyone // actually means by "the loop") keeps its short name. Only one external caller // (`bun_runtime::webcore::Blob`). @@ -1281,45 +1227,6 @@ macro_rules! intrusive_io_request { }; } -/// Windows analogue of [`IntrusiveIoRequest`] for types that embed an -/// intrusive `io_request: uv::fs_t` and recover the parent in -/// `extern "C" fn(*mut uv::fs_t)` libuv callbacks. -/// -/// Implement via [`intrusive_uv_fs!`]. -/// -/// # Safety -/// `UV_FS_OFFSET` MUST equal `core::mem::offset_of!(Self, )`. -#[cfg(windows)] -pub unsafe trait IntrusiveUvFs: Sized { - /// `core::mem::offset_of!(Self, io_request)`. - const UV_FS_OFFSET: usize; - - /// Recover `*mut Self` from the `*mut uv::fs_t` libuv passes back. - /// - /// # Safety - /// `req` must point to the `fs_t` field at `Self::UV_FS_OFFSET` inside a - /// live `Self` allocation, and the pointer's provenance must cover the - /// whole allocation. - #[inline(always)] - unsafe fn from_uv_fs(req: *mut bun_sys::windows::libuv::fs_t) -> *mut Self { - // SAFETY: caller upholds the trait safety contract above. - unsafe { bun_core::container_of::(req, Self::UV_FS_OFFSET) } - } -} - -/// Implements [`IntrusiveUvFs`] for a struct that embeds an intrusive -/// `io_request: uv::fs_t` field. -#[cfg(windows)] -#[macro_export] -macro_rules! intrusive_uv_fs { - ($ty:ty, $field:ident) => { - // SAFETY: `UV_FS_OFFSET` is `offset_of!($ty, $field)`. - unsafe impl $crate::IntrusiveUvFs for $ty { - const UV_FS_OFFSET: usize = ::core::mem::offset_of!($ty, $field); - } - }; -} - // Intrusive MPSC queue keyed on the `next` field. // // `next` is stored as `AtomicPtr`; the non-atomic accessor @@ -1769,7 +1676,6 @@ impl Poll { pub const RETRY: E = E::EAGAIN; -#[cfg(not(windows))] use crate::posix_event_loop::OneShotFlag; use crate::posix_event_loop::{Flags as PollFlags, FlagsSet as PollFlagsSet}; @@ -1844,21 +1750,10 @@ impl FilePollRef { // SAFETY: type invariant — see doc comment above. unsafe { &mut *loop_ } } + #[cfg(unix)] #[inline] pub(crate) fn unregister(self, loop_: *mut bun_uws_sys::Loop, force: bool) -> sys::Result<()> { - let loop_ = Self::uws_loop_mut(loop_); - #[cfg(not(windows))] - { - self.inner().unregister(loop_, force) - } - #[cfg(windows)] - { - let _ = force; - // The windows `FilePoll::unregister` always returns true — the - // bool is vestigial, so there is no error path to surface here. - let _ = self.inner().unregister(loop_); - Ok(()) - } + self.inner().unregister(Self::uws_loop_mut(loop_), force) } #[inline] pub(crate) fn register_with_fd( @@ -1871,20 +1766,8 @@ impl FilePollRef { FilePollKind::Readable => PollFlags::Readable, FilePollKind::Writable => PollFlags::Writable, }; - #[cfg(not(windows))] - { - self.inner().register_with_fd( - Self::uws_loop_mut(loop_), - flag, - OneShotFlag::Dispatch, - fd, - ) - } - #[cfg(windows)] - { - let _ = (loop_, flag, fd); - unreachable!("FilePoll fd registration is POSIX-only"); - } + self.inner() + .register_with_fd(Self::uws_loop_mut(loop_), flag, OneShotFlag::Dispatch, fd) } #[inline] pub(crate) fn has_flag(self, f: FilePollFlag) -> bool { @@ -1896,14 +1779,7 @@ impl FilePollRef { } #[inline] pub(crate) fn file_type(self) -> crate::pipes::FileType { - #[cfg(not(windows))] - { - self.inner().file_type() - } - #[cfg(windows)] - { - crate::pipes::FileType::File - } + self.inner().file_type() } #[inline] pub fn is_registered(self) -> bool { @@ -1913,6 +1789,7 @@ impl FilePollRef { pub fn is_watching(self) -> bool { self.inner().is_watching() } + #[cfg(unix)] #[inline] pub(crate) fn is_active(self) -> bool { self.inner().is_active() @@ -2109,116 +1986,73 @@ pub mod waker { } } - // ── Windows (uws WindowsLoop wakeup) ────────────────────────────────────── + // ── Windows (event object) ──────────────────────────────────────────────── + /// An auto-reset event: `wait()` parks until some thread has called + /// `wake()` since the last return, however many times. #[cfg(windows)] pub struct WindowsWaker { - /// Process-global `WindowsLoop` singleton. `BackRef` invariant (pointee - /// outlives holder) holds trivially: the loop is never freed. `None` - /// only between [`placeholder`] and [`init`]; every dispatch path - /// (`wake`/`wait`/`uv_loop`) unwraps and would have UB-derefed the old - /// raw null anyway. - /// - /// [`placeholder`]: Self::placeholder - /// [`init`]: Self::init - pub loop_: Option>, + event: bun_windows_sys::HANDLE, } + // SAFETY: an event handle may be set and waited on from any thread. + #[cfg(windows)] + unsafe impl Send for WindowsWaker {} + // SAFETY: as above; both methods take `&self` and only pass the handle on. + #[cfg(windows)] + unsafe impl Sync for WindowsWaker {} + #[cfg(windows)] impl WindowsWaker { /// Stand-in until `init()` runs (e.g. a `BundleThread` allocated before - /// its real waker is created). `loop_` is `None` and must never be - /// dereferenced — overwrite via `ptr::write` before first - /// `wake()`/`wait()`/`uv_loop()`. Mirrors `LinuxWaker::placeholder` / - /// `KEventWaker::placeholder` so cross-platform call sites don't fall - /// back to `mem::zeroed()` (UB for the niche-optimised `Option`). + /// its real waker is created). Never wait or wake through it. pub const fn placeholder() -> Self { - Self { loop_: None } - } - - pub fn init() -> crate::Result { - Ok(Self { - loop_: Some(bun_ptr::BackRef::from( - core::ptr::NonNull::new(bun_uws_sys::WindowsLoop::get()) - .expect("WindowsLoop::get() singleton"), - )), - }) + Self { + event: core::ptr::null_mut(), + } } - /// Unwrap the back-reference. Panics on a `placeholder()` waker, which - /// is the same precondition the previous raw-pointer deref carried - /// (just loud instead of UB). - #[inline] - fn loop_ref(&self) -> bun_ptr::BackRef { - self.loop_.expect("WindowsWaker used before init()") + pub fn init() -> crate::error::Result { + // SAFETY: plain Win32 call; auto-reset, initially unsignalled, unnamed. + let event = unsafe { + crate::windows::sys::CreateEventW(core::ptr::null_mut(), 0, 0, core::ptr::null()) + }; + if event.is_null() { + return Err(crate::Error::Unexpected); + } + Ok(Self { event }) } pub fn wait(&self) { - // Do NOT route through `WindowsLoop::wait(&mut self)`: that would - // materialize a `&mut WindowsLoop` over the process-global - // singleton for the entire duration of `us_loop_run`/`uv_run`, - // and a concurrent `wake()` from a worker thread (BundleThread, - // HTTPThread) would alias it — two live `&mut T` to one - // allocation is UB under Stacked/Tree Borrows. Call the C entry - // point with the raw pointer directly so no Rust reference is - // ever formed. - // SAFETY: `loop_` is the live `WindowsLoop::get()` singleton, - // non-null after `init()`. - unsafe { bun_uws_sys::loop_::us_loop_run(self.loop_ref().as_const_ptr().cast_mut()) }; + debug_assert!(!self.event.is_null()); + bun_sys::windows::kernel32::WaitForSingleObject(self.event, bun_sys::windows::INFINITE); } pub fn wake(&self) { - // See `wait()` — this is the cross-thread wake path; forming a - // `&mut WindowsLoop` here would alias the event-loop thread's - // borrow held across `us_loop_run`. Pass the raw pointer to the - // thread-safe C wake (`uv_async_send`) instead. - // SAFETY: `loop_` is the live `WindowsLoop::get()` singleton; - // `us_wakeup_loop` → `uv_async_send` is documented thread-safe. - unsafe { - bun_uws_sys::loop_::us_wakeup_loop(self.loop_ref().as_const_ptr().cast_mut()) - }; - } - - /// Raw libuv `uv_loop_t*` underlying this waker's `WindowsLoop`. - /// - /// `loop_` is the process-global singleton from `WindowsLoop::get()` - /// (set in [`init`]), so the returned pointer has process lifetime — - /// safe to hand to `uv::Timer::init` and friends without an `unsafe` - /// block at the call site. - #[inline] - pub fn uv_loop(&self) -> *mut bun_sys::windows::libuv::Loop { - // `BackRef` deref is safe (process-lifetime singleton); `uv_loop` - // is a `Copy` field set once by C `us_create_loop`. - self.loop_ref().uv_loop + debug_assert!(!self.event.is_null()); + // SAFETY: `event` is the handle created in `init`. + unsafe { crate::windows::sys::SetEvent(self.event) }; } } } // ─── Closer (moved from bun_io) ───────────────────────────────────────────── // -// Schedules an async fd close on the -// thread pool (POSIX) or via uv_fs_close (Windows). io (T2) owns it so +// Schedules an fd close on the thread pool. io (T2) owns it so // `pipes::PollOrFd::close` has no upward dep on bun_io (T3). pub mod closer { use bun_sys::Fd; - - // ── POSIX ──────────────────────────────────────────────────────────────── - - #[cfg(not(windows))] use bun_threading::work_pool::{Task as WorkPoolTask, WorkPool}; - #[cfg(not(windows))] #[repr(C)] pub struct Closer { pub(crate) fd: Fd, task: WorkPoolTask, } - #[cfg(not(windows))] bun_threading::intrusive_work_task!(Closer, task); // SAFETY: `Closer` is `Send` (`Fd` + intrusive `Task`). - #[cfg(not(windows))] unsafe impl bun_threading::work_pool::OwnedTask for Closer { fn run(self: Box) { use bun_sys::FdExt; @@ -2226,10 +2060,8 @@ pub mod closer { } } - #[cfg(not(windows))] impl Closer { - /// `_compat`: for signature compatibility with the Windows version. - pub fn close(fd: Fd, _compat: ()) { + pub fn close(fd: Fd) { debug_assert!(fd.is_valid()); WorkPool::schedule_owned(Box::new(Closer { fd, @@ -2240,70 +2072,4 @@ pub mod closer { })); } } - - // ── Windows ────────────────────────────────────────────────────────────── - - #[cfg(windows)] - use crate::IntrusiveUvFs as _; - #[cfg(windows)] - use bun_sys::ReturnCodeExt as _; - #[cfg(windows)] - use bun_sys::windows::libuv as uv; - #[cfg(windows)] - use core::ffi::c_void; - - #[cfg(windows)] - #[repr(C)] - pub struct Closer { - io_request: uv::fs_t, - } - #[cfg(windows)] - crate::intrusive_uv_fs!(Closer, io_request); - - #[cfg(windows)] - impl Closer { - pub fn close(fd: Fd, loop_: *mut uv::Loop) { - let io_request: uv::fs_t = bun_core::ffi::zeroed(); - let closer = bun_core::heap::into_raw(Box::new(Closer { io_request })); - // data is not overridden by libuv when calling uv_fs_close, its ok to set it here - // SAFETY: closer is a freshly-boxed valid pointer. - unsafe { - (*closer).io_request.data = closer.cast::(); - if let Some(err) = uv::uv_fs_close( - loop_, - &mut (*closer).io_request, - fd.uv(), - Some(Self::on_close), - ) - .errno() - { - bun_core::debug_warn!("libuv close() failed = {:?}", err); - drop(bun_core::heap::take(closer)); - } - } - } - - extern "C" fn on_close(req: *mut uv::fs_t) { - // SAFETY: req points to Closer.io_request (set in `close` above). - let closer: *mut Closer = unsafe { Closer::from_uv_fs(req) }; - // SAFETY: req.data was set to `closer` in `close`; both valid for the callback. - unsafe { - debug_assert!(closer == (*req).data.cast::()); - bun_sys::syslog!( - "uv_fs_close({}) = {}", - // SAFETY: `uv_fs_close` populated the `fd` arm of the union. - Fd::from_uv((*req).file_fd()), - (*req).result - ); - - #[cfg(debug_assertions)] - if let Some(err) = (*closer).io_request.result.errno() { - bun_core::debug_warn!("libuv close() failed = {:?}", err); - } - - (*req).deinit(); - drop(bun_core::heap::take(closer)); - } - } - } } diff --git a/src/io/openForWriting.rs b/src/io/openForWriting.rs index 8a29721c44d1..7e11e1beefa7 100644 --- a/src/io/openForWriting.rs +++ b/src/io/openForWriting.rs @@ -16,6 +16,7 @@ pub trait OpenForWritingInput { ) -> bun_sys::Result; } +#[cfg(unix)] impl OpenForWritingInput for crate::PathOrFileDescriptor<'_> { fn open_for_writing_result( &self, @@ -98,13 +99,7 @@ pub fn open_for_writing_impl( where P: OpenForWritingInput, { - #[cfg(windows)] - { - let _ = (is_socket, out_nonblocking, ctx, on_tty, is_pollable); - } // TODO: this should be concurrent. - #[cfg(unix)] - let mut isatty = false; let mut is_nonblocking = false; let result = input_path.open_for_writing_result(dir, input_flags, mode, &mut is_nonblocking, &openat); @@ -112,6 +107,7 @@ where #[cfg(unix)] { + let mut isatty = false; match bun_sys::fstat(fd) { Err(err) => { fd.close(); @@ -167,9 +163,19 @@ where #[cfg(windows)] { - *pollable = (bun_sys::windows::GetFileType(fd.native()) & bun_sys::windows::FILE_TYPE_PIPE) - != 0 - && !force_sync; + // `pollable` tells a Windows writer that the HANDLE is an overlapped + // pipe end Bun created, which one opened by path never is: the writer + // classifies it on its own. + let _ = ( + is_socket, + force_sync, + out_nonblocking, + ctx, + on_tty, + is_pollable, + is_nonblocking, + ); + *pollable = false; return Ok(fd); } } diff --git a/src/io/pipes.rs b/src/io/pipes.rs index 5cc50a9079e6..b3a320df9d30 100644 --- a/src/io/pipes.rs +++ b/src/io/pipes.rs @@ -1,7 +1,6 @@ use core::ffi::c_void; use bun_sys::Fd; -#[cfg(not(windows))] use bun_sys::FdExt; #[cfg(target_os = "macos")] @@ -44,6 +43,7 @@ impl PollOrFd { } } + #[cfg(unix)] pub(crate) fn get_poll_mut(&mut self) -> Option { match self { PollOrFd::Poll(poll) => Some(*poll), @@ -62,7 +62,7 @@ impl PollOrFd { let fd = self.get_fd(); #[cfg(target_os = "macos")] let mut close_async = true; - #[cfg(all(not(target_os = "macos"), not(windows)))] + #[cfg(not(target_os = "macos"))] let close_async = true; if matches!(self, PollOrFd::Poll(_)) { // workaround kqueue bug. @@ -95,22 +95,10 @@ impl PollOrFd { if fd != Fd::INVALID { *self = PollOrFd::Closed; - // TODO: We should make this call compatible using bun.FD - #[cfg(windows)] - { - if close_fd { - crate::closer::Closer::close(fd, bun_sys::windows::libuv::Loop::get()); - } - } - #[cfg(not(windows))] - { - if close_async && close_fd { - crate::closer::Closer::close(fd, ()); - } else { - if close_fd { - let _ = fd.close_allowing_bad_file_descriptor(None); - } - } + if close_async && close_fd { + crate::closer::Closer::close(fd); + } else if close_fd { + let _ = fd.close_allowing_bad_file_descriptor(None); } if let Some(f) = on_close_fn { // SAFETY: caller guarantees ctx is Some and properly aligned diff --git a/src/io/posix_event_loop.rs b/src/io/posix_event_loop.rs index e802cd0154c7..041c556f1280 100644 --- a/src/io/posix_event_loop.rs +++ b/src/io/posix_event_loop.rs @@ -1,36 +1,12 @@ #[cfg(unix)] use core::ffi::c_int; -#[cfg(not(windows))] use core::ffi::c_void; -#[cfg(not(windows))] use core::fmt; -#[cfg(unix)] use core::ptr; -#[cfg(not(windows))] -use bun_sys::{self as sys, Fd}; -use bun_uws_sys::Loop as UwsLoop; - -pub type Loop = UwsLoop; - -// Note: `bun_uws_sys::Loop` only exposes `inc`/`dec`/`ref_`/`unref`. The -// `active` counter is a public field, so inline the saturating math here until -// `bun_uws_sys` grows `add_active`/`sub_active`. On Windows the uws loop has no -// such counter (libuv tracks active handles itself); `posix_event_loop` is only -// reachable from non-Windows `Loop` consumers, so the Windows arm is a no-op. -#[cfg(not(windows))] -#[inline] -fn loop_add_active(loop_: &mut Loop, value: u32) { - loop_.active = loop_.active.saturating_add(value); -} -#[cfg(not(windows))] -#[inline] -fn loop_sub_active(loop_: &mut Loop, value: u32) { - loop_.active = loop_.active.saturating_sub(value); -} - -#[cfg(not(windows))] use bun_sys::syslog; +use bun_sys::{self as sys, Fd}; +use bun_uws_sys::Loop; /// Local `errno_sys` helper. `bun_sys` /// does not yet expose this helper on `Result`; once it does, drop this and @@ -133,9 +109,6 @@ pub fn js_vm_ctx() -> EventLoopCtx { get_vm_ctx(AllocatorType::Js) } -// `KeepAlive` (struct + 14-method impl) was duplicated here and in -// `windows_event_loop.rs`; both copies now live in `crate::keep_alive`. - // ────────────────────────────────────────────────────────────────────────── // FilePoll // ────────────────────────────────────────────────────────────────────────── @@ -143,7 +116,7 @@ pub fn js_vm_ctx() -> EventLoopCtx { // `KQueueGenerationNumber` is `usize` on macOS-debug, else a zero-size sentinel. #[cfg(all(target_os = "macos", debug_assertions))] type KQueueGenerationNumber = usize; -#[cfg(all(unix, not(all(target_os = "macos", debug_assertions))))] +#[cfg(not(all(target_os = "macos", debug_assertions)))] type KQueueGenerationNumber = u8; // Note: conceptually zero-width; smallest Rust int is u8. Gated by cfg below. // Debug-only diagnostic; `Relaxed` (no synchronization implied). @@ -246,7 +219,6 @@ pub struct Owner { } impl Owner { - #[cfg(not(windows))] pub(crate) const NULL: Owner = Owner { tag: PollTag::Null, ptr: core::ptr::null_mut(), @@ -260,7 +232,6 @@ impl Owner { self.ptr.is_null() } #[inline] - #[cfg(not(windows))] pub(crate) fn clear(&mut self) { *self = Self::NULL; } @@ -282,11 +253,8 @@ pub enum AllocatorType { Mini, } -// `FilePoll`/`Store` here are POSIX-specific (kqueue/epoll registration, -// generation_number, allocator_type). On Windows the variants live in -// `windows_event_loop`; the shared `EventLoopCtxVTable` above names -// `crate::FilePoll`/`crate::Store` so the right one is picked. -#[cfg(not(windows))] +/// On Windows only a SOCKET can be polled (the loop asks AFD for its +/// readiness); pipes, consoles and files complete through `crate::windows`. pub struct FilePoll { pub fd: Fd, pub flags: FlagsSet, @@ -301,9 +269,12 @@ pub struct FilePoll { pub(crate) next_to_free: *mut FilePoll, pub(crate) allocator_type: AllocatorType, + + /// The loop's readiness poll for `fd`, while registered. + #[cfg(windows)] + pub(crate) socket_poll: *mut bun_uws_sys::iocp::SocketPoll, } -#[cfg(not(windows))] impl FilePoll { fn update_flags(&mut self, updated: FlagsSet) { let mut flags = self.flags; @@ -363,6 +334,14 @@ impl FilePoll { self.on_update(0); } + #[cfg(windows)] + pub(crate) fn on_socket_event(&mut self, event: bun_uws_sys::loop_::EventType) { + // The loop re-arms a socket poll by itself. + debug_assert!(!self.flags.contains(Flags::OneShot)); + self.update_flags(Flags::from_socket_event(event)); + self.on_update(0); + } + pub fn is_readable(&mut self) -> bool { let readable = self.flags.contains(Flags::Readable); self.flags.remove(Flags::Readable); @@ -437,6 +416,7 @@ impl FilePoll { unsafe { __bun_run_file_poll(self, size_or_offset) }; } + #[cfg(unix)] #[inline] pub(crate) fn is_active(&self) -> bool { self.flags.contains(Flags::HasIncrementedPollCount) @@ -479,10 +459,7 @@ impl FilePoll { } self.flags.remove(Flags::HasIncrementedPollCount); - loop_sub_active( - loop_, - self.flags.contains(Flags::HasIncrementedActiveCount) as u32, - ); + loop_.sub_active(self.flags.contains(Flags::HasIncrementedActiveCount) as u32); self.flags.remove(Flags::KeepsEventLoopAlive); self.flags.remove(Flags::HasIncrementedActiveCount); } @@ -497,10 +474,7 @@ impl FilePoll { self.flags.insert(Flags::HasIncrementedPollCount); if self.flags.contains(Flags::KeepsEventLoopAlive) { - loop_add_active( - loop_, - (!self.flags.contains(Flags::HasIncrementedActiveCount)) as u32, - ); + loop_.add_active((!self.flags.contains(Flags::HasIncrementedActiveCount)) as u32); self.flags.insert(Flags::HasIncrementedActiveCount); } } @@ -528,6 +502,8 @@ impl FilePoll { .wrapping_add(1), #[cfg(not(all(target_os = "macos", debug_assertions)))] generation_number: 0, + #[cfg(windows)] + socket_poll: ptr::null_mut(), } } @@ -565,38 +541,7 @@ impl FilePoll { one_shot: OneShotFlag, fd: Fd, ) -> sys::Result<()> { - #[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - ))] - return self.register_with_fd_impl(loop_, flag, one_shot, fd); - #[cfg(not(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - )))] - { - let _ = (loop_, flag, one_shot, fd); - sys::Result::Ok(()) - } - } - - #[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - ))] - fn register_with_fd_impl( - &mut self, - loop_: &mut Loop, - flag: Flags, - one_shot: OneShotFlag, - fd: Fd, - ) -> sys::Result<()> { + #[cfg(not(windows))] let watcher_fd = loop_.fd; syslog!( @@ -837,6 +782,54 @@ impl FilePoll { return err; } } + #[cfg(windows)] + { + use bun_uws_sys::iocp; + let mut events = match flag { + Flags::Readable => iocp::SOCKET_READABLE, + Flags::Writable => iocp::SOCKET_WRITABLE, + // Nothing but a socket has readiness here. + _ => { + return sys::Result::Err(sys::Error::from_code( + sys::E::EOPNOTSUPP, + sys::Tag::poll, + )); + } + }; + // A socket has one poll; it carries both directions. + if flag == Flags::Readable && self.flags.contains(Flags::PollWritable) { + events |= iocp::SOCKET_WRITABLE; + } + if flag == Flags::Writable && self.flags.contains(Flags::PollReadable) { + events |= iocp::SOCKET_READABLE; + } + let loop_ptr: *mut Loop = loop_; + if self.socket_poll.is_null() { + // SAFETY: `loop_ptr` is the live loop; `fd` is the caller's open + // SOCKET; the tagged owner is handed back to + // `Bun__internal_dispatch_ready_poll` until the poll is stopped. + let poll = unsafe { + iocp::us_iocp_poll_socket( + loop_ptr, + Pollable::init(self).ptr(), + fd.native() as usize, + events, + ) + }; + self.flags.insert(Flags::WasEverRegistered); + if poll.is_null() { + let err = bun_sys::windows::Win32Error::get(); + self.deactivate(loop_); + return sys::Result::Err( + sys::Error::from_win32(err, sys::Tag::poll).with_fd(fd), + ); + } + self.socket_poll = poll; + } else { + // SAFETY: `socket_poll` is this poll's live registration on `loop_ptr`. + unsafe { iocp::us_iocp_poll_socket_change(loop_ptr, self.socket_poll, events) }; + } + } self.activate(loop_); self.flags.insert(match flag { @@ -873,40 +866,20 @@ impl FilePoll { ) -> sys::Result<()> { // Note: compute the syscall result first, then unconditionally // deactivate. Avoids a raw-pointer scopeguard. - #[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - ))] let result = self.unregister_with_fd_impl(loop_, fd, force_unregister); - #[cfg(not(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - )))] - let result: sys::Result<()> = { - let _ = (fd, force_unregister); - sys::Result::Ok(()) - }; self.deactivate(loop_); result } - #[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - ))] fn unregister_with_fd_impl( &mut self, loop_: &mut Loop, fd: Fd, force_unregister: bool, ) -> sys::Result<()> { - debug_assert!(fd.native() >= 0 && fd != INVALID_FD); + #[cfg(not(windows))] + debug_assert!(fd.native() >= 0); + debug_assert!(fd != INVALID_FD); let registered = self.flags.contains(Flags::PollReadable) || self.flags.contains(Flags::PollWritable) @@ -919,6 +892,7 @@ impl FilePoll { return sys::Result::Ok(()); } + #[cfg(not(windows))] let watcher_fd = loop_.fd; let both_directions = disarmed_only || (self.flags.contains(Flags::PollReadable) @@ -1143,6 +1117,14 @@ impl FilePoll { e => return sys::Result::Err(sys::Error::from_code(e, sys::Tag::kevent)), } } + #[cfg(windows)] + if !self.socket_poll.is_null() { + // SAFETY: `socket_poll` is this poll's live registration on + // `loop_`. The loop releases it once the kernel is done with it and + // does not use the tagged owner again. + unsafe { bun_uws_sys::iocp::us_iocp_poll_socket_stop(loop_, self.socket_poll) }; + self.socket_poll = ptr::null_mut(); + } self.flags.remove(Flags::NeedsRearm); self.flags.remove(Flags::OneShot); @@ -1156,7 +1138,6 @@ impl FilePoll { } } -#[cfg(not(windows))] impl fmt::Display for FilePoll { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -1274,12 +1255,29 @@ impl Flags { } flags } + + #[cfg(windows)] + pub(crate) fn from_socket_event(event: bun_uws_sys::loop_::EventType) -> FlagsSet { + use bun_uws_sys::iocp; + let mut flags = FlagsSet::empty(); + if event.events & iocp::SOCKET_READABLE != 0 { + flags.insert(Flags::Readable); + } + if event.events & iocp::SOCKET_WRITABLE != 0 { + flags.insert(Flags::Writable); + } + if event.error { + flags.insert(Flags::Eof); + } + if event.eof { + flags.insert(Flags::Hup); + } + flags + } } -#[cfg(not(windows))] pub(crate) struct FlagsFormatter(pub FlagsSet); -#[cfg(not(windows))] impl fmt::Display for FlagsFormatter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut is_first = true; @@ -1301,21 +1299,17 @@ impl fmt::Display for FlagsFormatter { // `bun_alloc::heap_breakdown` is a no-op outside macOS Instruments // heap-breakdown builds, so the 128-slot hive is unconditional here (same // choice as `RuntimeTranspilerStore`'s TranspilerJob hive). -#[cfg(not(windows))] const HIVE_SIZE: usize = 128; -#[cfg(not(windows))] type FilePollHive = bun_collections::hive_array::Fallback; /// We defer freeing FilePoll until the end of the next event loop iteration /// This ensures that we don't free a FilePoll before the next callback is called -#[cfg(not(windows))] pub struct Store { hive: FilePollHive, pending_free_head: *mut FilePoll, pending_free_tail: *mut FilePoll, } -#[cfg(not(windows))] impl Store { pub fn init() -> Store { Store { @@ -1418,13 +1412,11 @@ impl Store { // `impl TypeList for (FilePoll,)`, which trips the orphan rule (foreign trait // on a tuple). Since the union has exactly one variant, wrap the raw // `TaggedPtr` directly with the same tag scheme (`1024 - index`). -#[cfg(not(windows))] #[derive(Copy, Clone)] pub(crate) struct Pollable { repr: bun_collections::TaggedPtr, } -#[cfg(not(windows))] impl Pollable { /// Tag value for `FilePoll` (index 0 → `1024 - 0`). pub(crate) const FILE_POLL_TAG: u16 = 1024; @@ -1459,15 +1451,6 @@ impl Pollable { } } -// `current_ready_poll`/`ready_polls` only exist on the POSIX uws loop layout; -// on Windows the libuv loop drives readiness, so this entry point is never -// linked there. Restrict to the platforms where the fields are present. -#[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" -))] #[unsafe(no_mangle)] /// # Safety /// uWS C callback: `loop_` is the live per-thread `us_loop_t`; `tagged_pointer` @@ -1501,6 +1484,8 @@ unsafe extern "C" fn Bun__internal_dispatch_ready_poll( file_poll.on_kqueue_event(&ev); #[cfg(any(target_os = "linux", target_os = "android"))] file_poll.on_epoll_event(&ev); + #[cfg(windows)] + file_poll.on_socket_event(ev); } #[cfg(target_os = "macos")] @@ -1517,7 +1502,6 @@ pub enum OneShotFlag { None, } -#[cfg(not(windows))] const INVALID_FD: Fd = Fd::INVALID; #[cfg(all(test, not(windows)))] diff --git a/src/io/source.rs b/src/io/source.rs index 9eafd8ffd555..6dcdb37800f0 100644 --- a/src/io/source.rs +++ b/src/io/source.rs @@ -1,573 +1,150 @@ -use core::ffi::{c_int, c_void}; -use core::mem::MaybeUninit; -use core::sync::atomic::{AtomicBool, Ordering}; +use core::ffi::c_int; -use bun_sys::windows::libuv as uv; -// `is_closed`/`is_active`/`fd` are default trait methods on `UvHandle`; -// the trait must be in scope for method resolution on `Box`/`Tty`. -use bun_sys::windows::libuv::UvHandle as _; -// `to_error` on `ReturnCode`/`ReturnCodeI64` lives in `bun_sys` (layering). -use bun_sys::Fd; -use bun_sys::ReturnCodeExt as _; +use bun_sys::{Fd, FileKind}; +use bun_uws_sys::Loop; -bun_core::declare_scope!(PipeSource, hidden); +use crate::windows::{self, File, Pipe, PipeOrigin, Tty}; -pub type Pipe = uv::Pipe; -pub use uv::Tty; +bun_core::declare_scope!(PipeSource, hidden); +/// What a reader or writer on Windows drives, by kind of HANDLE. pub enum Source { - Pipe(Box), - /// `BackRef` not `Box`: the stdin tty (fd 0) lives in static storage - /// (`stdin_tty::value()`), and Box-from-static is UB. Heap-allocated ttys - /// use `heap::alloc`; destroy paths gate `heap::take` on `!is_stdin_tty()`. - /// In both cases the `Tty` strictly outlives every `Source` that holds it - /// (process-static, or freed only by the libuv close callback after the - /// `Source` is dropped), so the `BackRef` invariant holds and `Deref` - /// yields `&Tty` without a per-site `unsafe`. - Tty(bun_ptr::BackRef), - File(Box), - SyncFile(Box), -} - -/// File source for async file I/O operations using libuv. -/// -/// Manages a single `uv_fs_t` through a state machine that ensures: -/// - Only one operation uses the `fs` field at a time -/// - The `fs` is properly deinitialized before reuse -/// - Cancellation is only attempted when an operation is in-flight -/// -/// Typical usage pattern: -/// 1. Check `can_start()` - returns true if ready for a new operation -/// 2. Call `prepare()` - marks fs as in-use -/// 3. Set up buffer and call `uv_fs_read()` or `uv_fs_write()` -/// 4. In callback, call `complete()` first to clean up -/// 5. Process the result -/// -/// Cancellation: -/// - Call `stop()` to cancel an in-flight operation -/// - The callback will still fire with UV_ECANCELED -/// - Always call `complete()` in the callback regardless of cancellation -/// -/// Cleanup: -/// - Call `detach()` if parent is destroyed before operation completes -/// - File will automatically close itself after the operation finishes -#[repr(C)] -pub struct File { - /// The fs_t for I/O operations (reads/writes) and state-machine-managed closes. - /// State machine ensures this is only used for one operation at a time. - pub(crate) fs: uv::fs_t, - - /// Buffer descriptor for the current read operation (unused by writers). - pub(crate) iov: uv::uv_buf_t, - - /// The file descriptor. - pub(crate) file: uv::uv_file, - - /// Current state of the fs_t request. - pub(crate) state: FileState, - - /// When true, file will close itself when the current operation completes. - pub(crate) close_after_operation: bool, - - /// A read still in flight when its reader let go of this file (`iov` - /// points into it): the reader's buffer, kept alive here until the - /// detached completion frees the Box, so the pending ReadFile never lands - /// in freed memory. - pub(crate) orphaned_read_buf: Vec, -} - -#[repr(u8)] -#[derive(Copy, Clone, PartialEq, Eq, Default)] -pub enum FileState { - /// fs.deinit() called, ready for next operation - #[default] - Deinitialized, - /// read or write operation in progress - Operating, - /// cancel requested, waiting for callback - Canceling, - /// close operation in progress - Closing, -} - -impl Default for File { - fn default() -> Self { - // Hand-written equivalent of zero-initialization because `state` is an enum field - // (PORTING.md forbids blanket zeroed() over enums). FileState::Deinitialized == 0. - Self { - fs: bun_core::ffi::zeroed(), - iov: bun_core::ffi::zeroed(), - file: 0, - state: FileState::Deinitialized, - close_after_operation: false, - orphaned_read_buf: Vec::new(), - } - } -} - -impl File { - /// Get the File struct from an fs_t pointer using field offset. - pub(crate) unsafe fn from_fs(fs: *mut uv::fs_t) -> *mut File { - // SAFETY: fs points to File.fs; recover the parent via offset_of. - unsafe { bun_core::from_field_ptr!(File, fs, fs) } - } - - /// Backref-deref accessor for libuv `fs_t` completion callbacks: snapshot - /// the `result` / `data` POD fields, then recover the owning `&mut File` - /// via `container_of`. Collapses the open-coded raw-deref prelude in each - /// `on_fs_*_complete` / `on_file_read` callback into one call site. - /// - /// # Safety - /// `fs` must be the live `uv_fs_t*` libuv handed to a completion callback - /// for an operation started on a heap-boxed `File` (i.e. it points at - /// `self.fs`). No other `&`/`&mut File` may be live for `'a` — satisfied by - /// libuv's single-threaded callback dispatch (sole re-entry point). - #[inline] - pub(crate) unsafe fn from_fs_callback<'a>( - fs: *mut uv::fs_t, - ) -> (&'a mut File, uv::ReturnCodeI64, *mut c_void) { - // SAFETY: caller contract — `fs` is live; read the POD `result`/`data` - // before forming `&mut File` so the short raw read is dead (NLL) by the - // time the parent borrow covers the same bytes. - let (result, data) = unsafe { ((*fs).result, (*fs).data) }; - // SAFETY: caller contract — `fs` is `File.fs`; `from_fs` container_of - // recovers the boxed parent, which outlives `'a` (callback contract). - (unsafe { &mut *Self::from_fs(fs) }, result, data) - } - - /// Returns true if ready to start a new operation. - pub(crate) fn can_start(&self) -> bool { - self.state == FileState::Deinitialized && !self.fs.data.is_null() - } - - /// Mark the file as in-use for an operation. - /// Must only be called when can_start() returns true. - pub(crate) fn prepare(&mut self) { - debug_assert!(self.state == FileState::Deinitialized); - debug_assert!(!self.fs.data.is_null()); - self.state = FileState::Operating; - self.close_after_operation = false; - } - - /// Request cancellation of the current operation. - /// If successful, the callback will fire with UV_ECANCELED. - /// If cancel fails, the operation completes normally. - pub(crate) fn stop(&mut self) { - if self.state != FileState::Operating { - return; - } - - // SAFETY: &mut self.fs is a valid uv_fs_t request; uv_req_t is its base. - let cancel_result = - unsafe { uv::uv_cancel(core::ptr::from_mut::(&mut self.fs).cast()) }; - if cancel_result == 0 { - self.state = FileState::Canceling; - } - } - - /// Detach from parent and schedule automatic cleanup. - /// If an operation is in progress, it will complete and then close the file. - /// If idle, closes the file immediately. - pub(crate) fn detach(&mut self) { - self.fs.data = core::ptr::null_mut(); - self.close_after_operation = true; - self.stop(); - - if self.state == FileState::Deinitialized { - self.close_after_operation = false; - self.start_close(); - } - } - - /// Detach without closing the parent-owned fd. Returns true when an - /// operation is in flight (its callback frees the Box); false when idle - /// (caller drops the Box). - pub(crate) fn detach_borrowed_fd(&mut self) -> bool { - self.fs.data = core::ptr::null_mut(); - self.stop(); - self.state != FileState::Deinitialized - } - - /// Mark the operation as complete and clean up. - /// Must be called first in the callback before processing data. - pub(crate) fn complete(&mut self, was_canceled: bool) { - debug_assert!(self.state == FileState::Operating || self.state == FileState::Canceling); - if was_canceled { - debug_assert!(self.state == FileState::Canceling); - } - - self.fs.deinit(); - self.state = FileState::Deinitialized; - - if self.close_after_operation { - self.close_after_operation = false; - self.start_close(); - } - } - - fn start_close(&mut self) { - debug_assert!(self.state == FileState::Deinitialized); - self.state = FileState::Closing; - // SAFETY: self is heap-allocated (Box) and outlives the close callback, - // which frees it in on_close_complete. - // Derive the fs_t pointer from the whole `*mut File` (fs is the first - // #[repr(C)] field, offset 0) so the pointer carries full-struct - // provenance — `on_close_complete` recovers `*mut File` via `from_fs` - // and reads/frees bytes outside the `fs` field. `&mut self.fs` would - // narrow provenance to the field under SB/TB and make that UB. - unsafe { - let fs_ptr = core::ptr::from_mut::(self).cast::(); - uv::uv_fs_close( - uv::Loop::get(), - fs_ptr, - self.file, - Some(Self::on_close_complete), - ); - } - } - - extern "C" fn on_close_complete(fs: *mut uv::fs_t) { - // SAFETY: fs points to the .fs field of a Box allocated in open_file(). - // Unique ownership: by the time libuv fires this callback the parent has - // detached (fs.data == null) and no Rust `&mut File` is live; this callback - // is the sole owner and reclaims the Box below. - let file = unsafe { &mut *File::from_fs(fs) }; - debug_assert!(file.state == FileState::Closing); - file.fs.deinit(); - // SAFETY: file was allocated via Box::new in open_file(); reclaim and drop. - drop(unsafe { bun_core::heap::take(file as *mut File) }); - } + Pipe(Pipe), + Tty(Tty), + File(File), } impl Source { - /// Exclusive borrow of the `Tty` arm. `BackRef` already gives safe `Deref` - /// for shared reads; mutation still needs the per-site exclusivity - /// guarantee (single-threaded uv loop, no other `&Tty` live), so this - /// remains the one centralised `unsafe` for tty mutation. - #[inline] - fn tty_mut(tty: &mut bun_ptr::BackRef) -> &mut Tty { - // SAFETY: `BackRef` invariant guarantees liveness/alignment; the uv - // loop is single-threaded and `&mut Source` (or the sole `BackRef` - // returned from `open_tty`) is the only access path, so no `&Tty` - // overlaps this `&mut Tty`. - unsafe { tty.get_mut() } - } - - /// For a tty source, hand libuv the handle-owned read buffer with at - /// least `size` spare bytes (`uv::Tty::read_scratch`); `None` otherwise. - pub(crate) fn tty_read_scratch(&mut self, size: usize) -> Option<&mut [u8]> { - let Source::Tty(tty) = self else { return None }; - let scratch = &mut Self::tty_mut(tty).read_scratch; - scratch.clear(); - scratch.reserve(size); - // SAFETY: spare capacity handed to libuv to write into; the read - // callback copies out the written prefix. - Some(unsafe { bun_core::vec::spare_bytes_mut(scratch) }) + /// Take `fd` over. `Foreign` and `InheritedUnshared` leave what `fd` is to + /// be found out first, and say whose it is if it turns out to be a pipe; + /// every other `origin` says it is one. `fd` is closed with the source when + /// `close_fd` is set, except a standard handle, which is never closed. On + /// `Err` the caller still owns `fd`. + pub fn open( + loop_: *mut Loop, + fd: Fd, + origin: PipeOrigin, + close_fd: bool, + ) -> bun_sys::Result { + if matches!( + origin, + PipeOrigin::Created | PipeOrigin::Associated | PipeOrigin::CreatedSynchronous + ) { + bun_core::scoped_log!(PipeSource, "open(fd: {}, {:?})", fd, origin); + return Pipe::open(loop_, fd, origin, close_fd).map(Source::Pipe); + } + let kind = bun_sys::File::borrow(&fd).kind().map_err(|err| { + bun_sys::Error { + syscall: bun_sys::Tag::open, + ..err + } + .with_fd(fd) + })?; + Self::open_as(loop_, fd, origin, kind, close_fd) } - pub fn is_closed(&self) -> bool { - match self { - Source::Pipe(pipe) => pipe.is_closed(), - Source::Tty(tty) => tty.uv.is_closed(), - Source::SyncFile(file) | Source::File(file) => file.file == -1, + /// [`open`](Self::open) by a caller that has asked what `fd` is already + /// (`bun_sys::File::kind`). + pub fn open_as( + loop_: *mut Loop, + fd: Fd, + origin: PipeOrigin, + kind: FileKind, + close_fd: bool, + ) -> bun_sys::Result { + bun_core::scoped_log!(PipeSource, "open(fd: {}, {:?})", fd, kind); + match kind { + FileKind::NamedPipe => { + Pipe::open_classified(loop_, fd, origin, close_fd).map(Source::Pipe) + } + // `NUL` and serial ports are character devices too. + FileKind::CharacterDevice if bun_sys::windows::fs::is_console(fd.native()) => { + Tty::open(loop_, fd, close_fd).map(Source::Tty) + } + _ => File::open(loop_, fd, close_fd, kind == FileKind::File).map(Source::File), } } pub(crate) fn is_active(&self) -> bool { match self { Source::Pipe(pipe) => pipe.is_active(), - Source::Tty(tty) => tty.uv.is_active(), - Source::SyncFile(_) | Source::File(_) => true, - } - } - - pub(crate) fn to_stream(&mut self) -> *mut uv::uv_stream_t { - match self { - // SAFETY: uv::Pipe / `Tty` (via its first field, `uv::uv_tty_t`) embed - // uv_stream_t as their first member. - // `&mut self` so the returned `*mut` carries write provenance. - Source::Pipe(pipe) => core::ptr::from_mut::(pipe.as_mut()).cast(), - Source::Tty(tty) => tty.as_ptr().cast(), - Source::SyncFile(_) | Source::File(_) => unreachable!(), - } - } - - pub(crate) fn get_fd(&self) -> Fd { - match self { - // `UvHandle::fd()` returns the raw `uv_os_fd_t` (a HANDLE on - // Windows); tag kind=system so callers can round-trip through - // `Fd::native()`. - Source::Pipe(pipe) => Fd::from_system(pipe.fd()), - Source::Tty(tty) => Fd::from_system(tty.uv.fd()), - Source::SyncFile(file) | Source::File(file) => Fd::from_uv(file.file), + Source::Tty(tty) => tty.is_active(), + Source::File(_) => true, } } - pub fn set_data(&mut self, data: *mut c_void) { + /// A system-kind `Fd` for a pipe or console; the `Fd` it was opened with + /// for a file. + pub fn get_fd(&self) -> Fd { match self { - Source::Pipe(pipe) => pipe.data = data, - Source::Tty(tty) => Self::tty_mut(tty).uv.data = data, - Source::SyncFile(file) | Source::File(file) => file.fs.data = data, - } - } - - /// `owner` (a reader or writer) now drives this source: point the uv - /// handle's `data` at it and record it as the one a thread teardown closes - /// the source through (`uv::open_handles`). A file is listed only by a - /// reader (`WindowsBufferedReader::set_source`); for anything else the - /// file arm just sets `data`. - pub fn set_owner( - &mut self, - owner: *mut c_void, - close_via_owner: uv::open_handles::CloseViaOwner, - ) { - self.set_data(owner); - match self { - Source::Pipe(pipe) => uv::open_handles::set_owner( - core::ptr::from_mut::(pipe).cast(), - owner, - Some(close_via_owner), - ), - Source::Tty(tty) => { - uv::open_handles::set_owner(tty.as_ptr().cast(), owner, Some(close_via_owner)) - } - Source::SyncFile(file) | Source::File(file) => uv::open_handles::set_file_owner( - core::ptr::from_mut::(file).cast(), - owner, - close_via_owner, - ), + Source::Pipe(pipe) => pipe.fd(), + Source::Tty(tty) => tty.fd(), + Source::File(file) => file.fd(), } } - /// The boxed `File`'s address — the key a reader lists it under. - pub fn file_key(&mut self) -> Option<*mut c_void> { - match self { - Source::SyncFile(file) | Source::File(file) => { - Some(core::ptr::from_mut::(file).cast()) - } - _ => None, - } - } - - pub fn ref_(&mut self) { + /// Whether the source keeps the loop alive while it reads. A file + /// operation in flight always does. + pub fn ref_(&self) { match self { Source::Pipe(pipe) => pipe.ref_(), - Source::Tty(tty) => Self::tty_mut(tty).uv.ref_(), - Source::SyncFile(_) | Source::File(_) => {} + Source::Tty(tty) => tty.ref_(), + Source::File(_) => {} } } - pub fn unref(&mut self) { + pub fn unref(&self) { match self { Source::Pipe(pipe) => pipe.unref(), - Source::Tty(tty) => Self::tty_mut(tty).uv.unref(), - Source::SyncFile(_) | Source::File(_) => {} + Source::Tty(tty) => tty.unref(), + Source::File(_) => {} } } - pub(crate) fn open_pipe(loop_: *mut uv::Loop, fd: Fd) -> bun_sys::Result> { - bun_core::scoped_log!(PipeSource, "openPipe (fd = {})", fd); - let mut pipe: Box = Box::new(bun_core::ffi::zeroed::()); - // we should never init using IPC here - if let Some(err) = pipe.init(loop_, false).to_error(bun_sys::Tag::pipe) { - drop(pipe); - return bun_sys::Result::Err(err); - } - - if let Some(err) = pipe.open(fd.uv()).to_error(bun_sys::Tag::open) { - // close_and_destroy() schedules a libuv close whose callback frees - // the allocation. Hand the Box to libuv via into_raw so Drop does not double-free. - let raw = bun_core::heap::into_raw(pipe); - // SAFETY: raw is a valid initialized uv::Pipe; ownership passes to libuv. - unsafe { uv::Pipe::close_and_destroy(raw) }; - return bun_sys::Result::Err(err); - } - - bun_sys::Result::Ok(pipe) - } - - pub(crate) fn open_tty( - loop_: *mut uv::Loop, - fd: Fd, - ) -> bun_sys::Result> { - bun_core::scoped_log!(PipeSource, "openTTY (fd = {})", fd); - - let uv_fd = fd.uv(); - - if uv_fd == 0 { - return stdin_tty::get_stdin_tty(loop_); - } - - // Not `boxed_zeroed`: a zeroed `Vec` is UB. - let mut tty: Box = Box::new(Tty { - uv: bun_core::ffi::zeroed(), - read_scratch: Vec::new(), - }); - if let Some(err) = tty.init(loop_, uv_fd).to_error(bun_sys::Tag::open) { - drop(tty); - return bun_sys::Result::Err(err); - } - - // Heap-allocated tty: ownership is handed to libuv (the close callback - // `heap::take`s it). The `BackRef` invariant — pointee outlives every - // holder — is upheld because the only holder is the `Source::Tty` arm, - // which is dropped before the close callback fires. - // SAFETY: heap-owned `Tty` (leaked box); write provenance from `into_raw_nn`. - bun_sys::Result::Ok(unsafe { - bun_ptr::BackRef::from_raw_mut(bun_core::heap::into_raw_nn(tty).as_ptr()) - }) - } - - pub fn open_file(fd: Fd) -> Box { - debug_assert!(fd.is_valid() && fd.uv() != -1); - bun_core::scoped_log!(PipeSource, "openFile (fd = {})", fd); - let mut file: Box = Box::new(File::default()); - file.file = fd.uv(); - file - } - - pub(crate) fn open(loop_: *mut uv::Loop, fd: Fd) -> bun_sys::Result { - let rc = uv::uv_guess_handle(fd.uv()); - bun_core::scoped_log!( - PipeSource, - "open(fd: {}, type: {})", - fd, - <&'static str>::from(rc) - ); - - match rc { - uv::HandleType::NamedPipe => match Self::open_pipe(loop_, fd) { - bun_sys::Result::Ok(pipe) => bun_sys::Result::Ok(Source::Pipe(pipe)), - bun_sys::Result::Err(err) => bun_sys::Result::Err(err), - }, - uv::HandleType::Tty => match Self::open_tty(loop_, fd) { - bun_sys::Result::Ok(tty) => bun_sys::Result::Ok(Source::Tty(tty)), - bun_sys::Result::Err(err) => bun_sys::Result::Err(err), - }, - uv::HandleType::File => bun_sys::Result::Ok(Source::File(Self::open_file(fd))), - _ => { - let err = bun_sys::windows::Win32Error::get(); - if err == bun_sys::windows::Win32Error::SUCCESS { - return bun_sys::Result::Ok(Source::File(Self::open_file(fd))); - } - bun_sys::Result::Err(bun_sys::Error::from_win32(err, bun_sys::Tag::open)) - } - } - } - - /// Direct accessor for the `File`/`SyncFile` arm. - /// Panics on Pipe/Tty — callers gate on `matches!(.., File | SyncFile)`. - pub(crate) fn file(&self) -> &File { + /// Leave the `Fd` the source was opened with open when the source closes. + pub fn disown(&mut self) { match self { - Source::SyncFile(file) | Source::File(file) => file, - _ => unreachable!("Source::file() on non-file source"), + Source::Pipe(pipe) => pipe.disown(), + Source::Tty(tty) => tty.disown(), + Source::File(file) => file.disown(), } } - pub(crate) fn set_raw_mode(&mut self, value: bool) -> bun_sys::Result<()> { + /// Close, letting writes that have not completed report back (`ECANCELED` + /// for a pipe). Dropping the source instead calls nobody back, except for + /// a file write whose writer was not forgotten: its callback is what + /// releases the bytes it borrowed. + pub fn close(self) { match self { - Source::Tty(tty) => { - if let Some(err) = Self::tty_mut(tty) - .uv - .set_mode(if value { - uv::TtyMode::Raw - } else { - uv::TtyMode::Normal - }) - .to_error(bun_sys::Tag::uv_tty_set_mode) - { - bun_sys::Result::Err(err) - } else { - bun_sys::Result::Ok(()) - } - } - _ => bun_sys::Result::Err(bun_sys::Error { - errno: bun_sys::E::NOTSUP as _, - syscall: bun_sys::Tag::uv_tty_set_mode, - fd: self.get_fd(), - ..Default::default() - }), + Source::Pipe(pipe) => pipe.close(), + Source::Tty(tty) => tty.close(), + Source::File(file) => drop(file), } } -} - -pub(crate) mod stdin_tty { - use super::*; - - // PORTING.md §Global mutable state: init guarded by `LOCK` + `INITIALIZED`; - // afterwards only accessed by uv on the loop thread. RacyCell. - static DATA: bun_core::RacyCell> = - bun_core::RacyCell::new(MaybeUninit::uninit()); - static LOCK: bun_threading::Mutex = bun_threading::Mutex::new(); - static INITIALIZED: AtomicBool = AtomicBool::new(false); - - #[inline] - fn value() -> *mut Tty { - DATA.get().cast::() - } - pub(crate) fn is_stdin_tty(tty: *const Tty) -> bool { - core::ptr::eq(tty, value()) - } - - pub(super) fn get_stdin_tty( - loop_: *mut uv::Loop, - ) -> bun_sys::Result> { - // bun_threading::Mutex::lock() returns `()` — must use lock_guard() for RAII - // unlock-on-drop, otherwise the mutex is held forever and the next call - // (e.g. Source__setRawModeStdin → open_tty(stdin)) deadlocks/UB-relocks. - let _guard = LOCK.lock_guard(); - - if !INITIALIZED.swap(true, Ordering::Relaxed) { - let p = value(); - // SAFETY: value() points to static storage sized for Tty; lock - // held. uv_tty_init fills the `uv` half; `read_scratch` starts - // empty via a raw write (the storage is uninit, so a plain - // assignment would drop garbage). - let rc = unsafe { uv::uv_tty_init(loop_, core::ptr::addr_of_mut!((*p).uv), 0, 0) }; - if let Some(err) = rc.to_error(bun_sys::Tag::open) { - INITIALIZED.store(false, Ordering::Relaxed); - return bun_sys::Result::Err(err); - } - // SAFETY: as above; lock held, first initialization. - unsafe { core::ptr::addr_of_mut!((*p).read_scratch).write(Vec::new()) }; + pub(crate) fn is_reading(&self) -> bool { + match self { + Source::Pipe(pipe) => pipe.is_reading(), + Source::Tty(tty) => tty.is_reading(), + Source::File(file) => file.is_busy(), } - - // Destroy path must gate `heap::take` on `!is_stdin_tty(ptr)`. - // SAFETY: `value()` is the process-global static tty (never null, - // never freed); uv writes through it. - bun_sys::Result::Ok(unsafe { bun_ptr::BackRef::from_raw_mut(value()) }) } } -/// The uv loop is taken as a parameter (reading it from the VM directly would -/// be a T6 dependency); the C++ caller -/// (`ProcessBindingTTYWrap.cpp`) supplies `defaultGlobalObject()->uvLoop()`. +/// `process.stdin.setRawMode`. Raw mode asks the console to produce VT input +/// sequences itself where it can, which is also what makes sequences such as +/// bracketed paste reach the process at all; Node's readline handles both +/// forms. #[unsafe(no_mangle)] -extern "C" fn Source__setRawModeStdin(uv_loop: *mut uv::Loop, raw: bool) -> c_int { - let mut tty = match Source::open_tty(uv_loop, Fd::stdin()) { - bun_sys::Result::Ok(tty) => tty, - bun_sys::Result::Err(e) => return e.errno as c_int, +extern "C" fn Source__setRawModeStdin(raw: bool) -> c_int { + let stdin = Fd::stdin().native(); + let mode = if raw { + windows::tty::Mode::RawVt + } else { + windows::tty::Mode::Normal }; - // UV_TTY_MODE_RAW_VT is a variant of UV_TTY_MODE_RAW that enables control - // sequence processing on the TTY implementer side, rather than having libuv - // translate keypress events into control sequences, aligning behavior more - // closely with POSIX platforms. This is also required to support some - // control sequences at all on Windows, such as bracketed paste mode. The - // Node.js readline implementation handles differences between these modes. - // `tty` is the static stdin tty (fd 0 → `get_stdin_tty`), live for the - // process — same invariant the `Source::Tty` arm relies on, so reuse the - // shared `tty_mut` accessor. - if let Some(err) = Source::tty_mut(&mut tty) - .uv - .set_mode(if raw { - uv::TtyMode::Vt - } else { - uv::TtyMode::Normal - }) - .to_error(bun_sys::Tag::uv_tty_set_mode) - { - return err.errno as c_int; + match windows::tty::set_console_mode(stdin, mode) { + Ok(()) => 0, + Err(err) => err.errno as c_int, } - 0 } diff --git a/src/io/windows/README.md b/src/io/windows/README.md new file mode 100644 index 000000000000..8ddb605dc2ea --- /dev/null +++ b/src/io/windows/README.md @@ -0,0 +1,162 @@ +# Windows I/O (`bun_io::windows`) + +Pipes, the console and plain files on Windows, driven by the loop's completion +port (`packages/bun-usockets/src/eventing/iocp.c`, `bun_uws_sys::iocp`). The +module docs in `mod.rs` and `pipe.rs` describe the ownership rules. + +## Synchronous pipe handles + +A child process normally inherits its stdin/stdout/stderr as synchronous +(non-overlapped) pipe handles, because ordinary programs call +`ReadFile(h, .., NULL)` on them. A synchronous file object cannot do overlapped +I/O, cannot be associated with a completion port, and cannot be converted: + +| attempt | result | +| ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- | +| `ReOpenFile(h, .., FILE_FLAG_OVERLAPPED)` | `ERROR_PIPE_BUSY`: a reopen is a new client connection | +| `NtOpenFile` relative to the handle, empty name | `STATUS_PIPE_NOT_AVAILABLE` | +| `CreateIoCompletionPort(h, port)` | `ERROR_INVALID_PARAMETER` | +| `NtSetInformationFile(FileModeInformation)` clearing the synchronous bit | `STATUS_INVALID_PARAMETER` | +| `ReadFileEx`, or `ReadFile` with an `OVERLAPPED` | blocks the caller | +| `WaitForSingleObject(h)` | always signalled; not a data-arrival signal | +| `FSCTL_PIPE_ASSIGN_EVENT` | `STATUS_NOT_SUPPORTED` (removed from NPFS) | +| `SetNamedPipeHandleState(PIPE_NOWAIT)` | the mode belongs to the pipe end, so every process sharing it sees it; no arrival notification | +| `KernelBase!NamedPipeEventSelect` (undocumented) | needs `PIPE_NOWAIT`; on Windows 10 also `FILE_WRITE_DATA`, which a read-only stdin lacks | + +So reads of a `Mode::Sync` pipe block on the pipe's own reader thread +(`pipe.rs`, `SyncReader`). It waits with a zero-byte read and then takes at +most what the pipe says it holds: the wait consumes nothing, so +pausing leaves the data for whoever reads the handle next (a child that +inherits stdin); a zero-byte read can be cancelled without the peer's write +losing anything, where cancelling an N-byte read can; a pending N-byte read +is charged against the writer's `WriteQuotaAvailable`, which has made +Cygwin/MSYS writers believe the pipe is full; and the first write to arrive +completes a pending N-byte read alone, however much is written behind it. + +The doc comment of `SyncReader` describes how the loop and that thread take +turns. Input leaves the pipe only after the loop thread has run with it +waiting, as with a readiness poll: while the loop is blocked (a synchronous +spawn whose child inherits the handle), it stays for the child. libuv does the +same: its pool thread only ever does the zero-byte read. And the loop thread +waits while the bytes are taken and hands them over at once, so nothing of the +owner's runs with bytes out of the pipe that it has not been given: it can stop +reading from anywhere and lose nothing for a child. The wait for the thread to +start is short, and it is not what decides: the thread takes bytes only from a +request the loop is still waiting on. One that comes to it later (it had no CPU, +or was queued behind another process's read) takes nothing and says again, with +a packet, that the pipe is readable, which is what keeps the loop from blocking +where libuv's does. + +### Finding out whether a handle is synchronous + +Whoever made a pipe says what it is (`PipeOrigin`). For somebody else's handle +the kernel has to be asked, and every way of asking takes the lock of a +synchronous file object, which is held for as long as any thread of any process +has I/O in flight on it (a parked read of an inherited stdin, a writer blocked +on a full stdout). With another thread parked in `ReadFile(h, _, 0)`: + +| call on a duplicate of the handle | result | +| ------------------------------------------------------------------------ | ----------------------------------- | +| `NtQueryInformationFile(FileModeInformation)` | blocks | +| `SetNamedPipeHandleState`, `GetNamedPipeHandleState`, `GetNamedPipeInfo` | block | +| `PeekNamedPipe` | blocks | +| `CreateIoCompletionPort(h, port)` | blocks, then refuses as above | +| `GetFileType`, `NtQueryInformationFile(FileAccessInformation)` | return at once | +| `NtQueryObject`, `DuplicateHandle`, `CompareObjectHandles` | return at once | +| `CancelSynchronousIo` on a thread that is waiting for that lock | `ERROR_NOT_FOUND`, the thread stays | + +None of the calls that do not block tells the two kinds apart, and an +overlapped file object has no such lock. So a handle of unknown kind +(`Mode::Unknown`) is classified by the helper thread that does its first read or +write, before that thread's first I/O (`classify`), for as long as that takes. +The three standard handles are remembered once classified. The loop thread asks +only where it costs nothing more: when it is about to block on the same lock +anyway (a synchronous write: `process.stdout`, `Bun.write(fd)`), and of an end +nobody else does I/O on (`PipeOrigin::InheritedUnshared`, the IPC channel), +whose lock is free: it tries `CreateIoCompletionPort`, and success says +overlapped. + +### What the reader thread costs + +1 GiB through a synchronous pipe into a completion-port loop, MiB/s, with +0 / 20 / 100 µs of loop work per 64 KiB chunk and a writer that keeps the pipe +full: + +| mechanism | MiB/s | why not | +| ------------------------------------------------------------------------------------------- | ----------------- | ------------------------------------------------------------------------------------------------ | +| I/O ring read | 6700 / 2456 / 588 | see Follow-ups | +| libuv: zero-byte read on a pool thread, then peek + `ReadFile` on the loop thread | 4984 / 2310 / 581 | the loop thread blocks in `ReadFile` when another reader of the pipe gets there first | +| reader thread that takes the next chunk while the loop handles the current one | 4342 / 2591 / 605 | that chunk is gone from the pipe if the owner pauses in its callback | +| reader thread that takes what is there as soon as it is asked, else waits and says readable | 3116 / 1600 / 515 | asked, the owner may still pause before the loop comes round: what was taken is gone for a child | +| a pool work item per chunk (wait, peek, read, post) | 2730 / 1579 / 494 | same as the row above | +| reader thread: wait, say readable, be asked, peek + read, post | 1891 / 1099 / 478 | two loop round trips per chunk, and the second has the window of the two rows above | + +With a writer slower than the reader (the pipe drains between chunks) every row +runs at the writer's speed. + +That window is real. A process reads a bulk transfer, calls `pause()` from a +timer and starts a child that inherits stdin. Runs of 40 in which the child +missed a chunk: 4 to 7 for the last row, 40 for the row that takes as soon as it +is asked (29 with `setImmediate`), none for libuv. + +In use (`SyncReader`): wait and say readable, as in the last row; then the loop +thread asks for the bytes and waits for them, hands them over, and asks again +for as long as there are any, up to `TAKES_PER_TICK` times. The hops are those +of the row that takes as soon as it is asked and the window is gone: none of 380 +runs missed anything, 300 of them with several at once on one or two CPUs. + +libuv's number comes from reading on, 64 KiB at a time, for as long as reads +come back full: one pair of thread hops for a burst, not for a chunk. The reader +thread gets the same from taking more at once: room is made for twice what the +pipe held the last time, up to the 256 KiB a POSIX reader takes +(`PIPE_READ_BUFFER_SIZE`), so a trickle keeps its 64 KiB buffers. `process.stdin` +of a child, 1 GiB, against libuv: 0.9 to 1.3 times its speed on 24 CPUs, 0.96 to +1.29 with the process tree held to 1, 2 or 4. + +Spinning on the reader thread for about 30 µs before it parks hides the cost of +waking an idle core: +26 % with 24 idle CPUs, and -10 % to -50 % on 1, 2 or 4, +where it competes with the writer and the loop. Not done. + +## Follow-ups + +### I/O rings for synchronous handles (Windows 11+) + +Windows 11's I/O rings (`CreateIoRing`, `BuildIoRingReadFile`, +`BuildIoRingWriteFile`, `BuildIoRingCancelRequest`, `SubmitIoRing`, +`PopIoRingCompletion`, `SetIoRingCompletionEvent`; `ioringapi.h`, exported from +kernelbase.dll) do asynchronous I/O on a synchronous pipe handle as it is, with +no thread. Measured on build 26300, ring version 400, feature flags `0x2`: + +- Submit returns in microseconds with the pipe empty; the read completes when + the peer writes. `SubmitIoRing` does not block even while another thread is + blocked in a plain `ReadFile` on the same handle, and a pending ring read does + not block `PeekNamedPipe` from other threads. +- A zero-byte ring read completes on data arrival and consumes nothing, and + leaves the writer's `WriteQuotaAvailable` alone (a pending 64 KiB ring read + drops it to 0). It also completes on a zero-buffer pipe once a writer blocks. +- `BuildIoRingCancelRequest` and `CancelIoEx(h, NULL)` both complete the read + with `ERROR_OPERATION_ABORTED`; nothing is consumed. A closed writer completes + it with `ERROR_BROKEN_PIPE`. +- Writes work the same way: 64 KiB into a full 4 KiB pipe returns from submit at + once and completes when the reader drains. Today a slow reader of a + synchronous stdout blocks a helper thread per write. +- `CloseIoRing` does not cancel in-flight operations, and neither does the exit + of the submitting thread: cancel and drain before freeing buffers. +- Works inside an AppContainer. +- Throughput: see the table under "What the reader thread costs". + +What stands in the way of relying on it: + +- Reads need Windows 11 21H2 (build 22000); writes need ring version 300 + (22H2, build 22621). No Windows 10 and no Server 2022 has it, so the reader + thread stays as the fallback. +- The exports are absent on older systems: resolve them with `GetProcAddress` + from kernelbase. A static import breaks process start there. +- Microsoft documents nothing about which handle kinds ring operations accept. + The only hint is "similar to calling ReadFileEx" (which does require an + overlapped handle). It works; it is not promised; nobody is publicly known to + use rings for stdio. Any failure has to fall back silently. +- The only completion signal is the event from `SetIoRingCompletionEvent`, which + fires on the completion queue's empty-to-non-empty transition: wait on it with + a wait completion packet on the loop's port, re-arm before popping, and pop + until `S_FALSE`. Ring versions before 2 can miss the signal. diff --git a/src/io/windows/file.rs b/src/io/windows/file.rs new file mode 100644 index 000000000000..d6d94b6e2680 --- /dev/null +++ b/src/io/windows/file.rs @@ -0,0 +1,602 @@ +//! Anything that is neither a pipe nor a console: regular files, `NUL`, other +//! devices. There is no readiness or completion to wait for, so each read or +//! write runs on the work pool and its result comes back through the loop's +//! port. One operation at a time. A small write to a disk file, which goes to +//! the file cache, costs less than the two thread hops: it is made by the +//! caller, as on POSIX, and only its result comes from the loop. +//! +//! A request that is out belongs to the work pool until it has handed the +//! result to the port. `state` is how it changes hands: the pool thread moves +//! it forward with a compare-exchange at each step, and the loop thread claims +//! it the same way when it no longer wants the result, so exactly one side +//! finds out that the other got there first. + +use core::ptr::NonNull; +use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; +use std::sync::Arc; + +use bun_sys::{self as sys, E, Fd, FdExt as _, Tag}; +use bun_threading::work_pool::{IntrusiveWorkTask as _, Task, WorkPool}; +use bun_uws_sys::Loop; +use bun_uws_sys::iocp::Op; + +use super::pipe::ReadEvent; +use super::{Callback, Link, Port, ReadCallback}; + +/// The owner's handle to a file. Dropping it lets an operation that is already +/// running finish unobserved, then closes the fd if it is owned; with nothing +/// running, an fd that was only read from is closed by the time `drop` returns. +pub struct File { + inner: NonNull, +} + +const IDLE: u32 = 0; +/// On the work pool's queue: [`File::cancel`] can still win. +const QUEUED: u32 = 1; +const RUNNING: u32 = 2; +/// [`File::cancel`] won: the pool thread reports `ECANCELED` without doing the I/O. +const CANCELED: u32 = 3; +/// The pool thread is done and is handing the result to the port. +const POSTED: u32 = 4; +/// The loop is gone and nobody will dequeue a result: the pool thread lets go +/// of the request instead of posting it. +const ORPHANED: u32 = 5; + +/// The largest write to a disk file that the calling thread makes itself. +const INLINE_WRITE_MAX: usize = 64 * 1024; + +enum Request { + None, + Read { len: usize, offset: Option }, + Write { data: WriteData }, +} + +enum WriteData { + /// The owner's, valid as [`File::write`] asks. + Borrowed(*const u8, usize), + Owned(Vec), +} + +impl WriteData { + fn len(&self) -> usize { + match self { + WriteData::Borrowed(_, len) => *len, + WriteData::Owned(data) => data.len(), + } + } +} + +impl Request { + fn tag(&self) -> Tag { + match self { + Request::None | Request::Read { .. } => Tag::read, + Request::Write { .. } => Tag::write, + } + } +} + +#[repr(C)] +struct Inner { + op: Op, + link: Link, + task: Task, + fd: Fd, + close_fd: bool, + /// Not a device, which can take as long as it likes over a write. + disk: bool, + /// A write was asked for. + wrote: bool, + /// See [`File::shares_position`]. + shares_position: bool, + port: Arc, + state: AtomicU32, + /// After the request was orphaned, the owner and the pool thread each let + /// go once; the second one frees. + released: AtomicBool, + request: Request, + /// Who hears about reads. Kept apart from the request so it can change + /// while the work pool holds the request. + reader: Option, + /// Who hears about the write that is out. Apart from the request likewise. + writer: Option>>, + buf: Vec, + /// [`File::adopt_write_buffer`]: freed with `self`. + adopted: Vec, + result: sys::Result, + owner_gone: bool, + /// The loop was torn down ([`Link::shut`]): an operation still out + /// finishes unobserved, since its callback would run in a dead VM. + detached: bool, + /// Inside the owner's callback: the allocation and `buf` are in use by + /// `complete`'s frame, so a request made now is scheduled when it returns. + completing: bool, + deferred: bool, +} + +bun_threading::intrusive_work_task!(Inner, task); + +impl File { + /// `fd` is closed with the file when `close_fd` is set. `disk`: it is a + /// file on a disk. + pub fn open(loop_: *mut Loop, fd: Fd, close_fd: bool, disk: bool) -> sys::Result { + // SAFETY: `loop_` is the caller's live loop. + let Some(port) = (unsafe { super::port_for(loop_) }) else { + return Err(sys::Error::from_win32(super::sys::last_error(), Tag::open).with_fd(fd)); + }; + let inner = bun_core::heap::into_raw(Box::new(Inner { + op: Op::new(Inner::complete), + link: Link::new(loop_, Inner::shut), + task: Task { + node: Default::default(), + callback: Inner::run, + }, + fd, + close_fd, + disk, + wrote: false, + shares_position: false, + port, + state: AtomicU32::new(IDLE), + released: AtomicBool::new(false), + request: Request::None, + reader: None, + writer: None, + buf: Vec::new(), + adopted: Vec::new(), + result: Ok(0), + owner_gone: false, + detached: false, + completing: false, + deferred: false, + })); + // SAFETY: `inner` is at its final address. + unsafe { Link::insert(&raw mut (*inner).link) }; + // SAFETY: `into_raw` never returns null. + Ok(File { + inner: unsafe { NonNull::new_unchecked(inner) }, + }) + } + + pub fn fd(&self) -> Fd { + // SAFETY: `inner` is live while the owner's `File` is. + unsafe { (*self.inner.as_ptr()).fd } + } + + /// Leave the `Fd` open when the file goes away. + pub fn disown(&mut self) { + // SAFETY: `inner` is live while the owner's `File` is; the work pool + // looks at `close_fd` only after the owner has let go. + unsafe { (*self.inner.as_ptr()).close_fd = false }; + } + + /// An operation has been started and its callback has not run yet. + pub fn is_busy(&self) -> bool { + // SAFETY: `inner` is live while the owner's `File` is. + unsafe { (*self.inner.as_ptr()).state.load(Ordering::Acquire) != IDLE } + } + + /// The HANDLE is a duplicate of a descriptor somebody else reads at its + /// position, which a read at an offset leaves where it was. + pub fn shares_position(&mut self) { + // SAFETY: `inner` is live while the owner's `File` is, and no request is out. + unsafe { (*self.inner.as_ptr()).shares_position = true }; + } + + /// Read up to `len` bytes, at `offset` or at the file position, then call + /// `on_read(ctx, ..)` from the loop. `ctx` must stay valid until then or + /// until the `File` is dropped. + pub fn read( + &mut self, + len: usize, + offset: Option, + ctx: *mut T, + on_read: unsafe fn(*mut T, ReadEvent<'_>), + ) -> sys::Result<()> { + self.start(Request::Read { + len: len.max(1), + offset, + })?; + self.set_reader(ctx, on_read); + Ok(()) + } + + /// Who the read that is out (if any) reports to: for an owner that moved. + pub fn set_reader(&mut self, ctx: *mut T, on_read: unsafe fn(*mut T, ReadEvent<'_>)) { + // SAFETY: `inner` is live while the owner's `File` is; the work pool + // never looks at `reader`. + unsafe { (*self.inner.as_ptr()).reader = Some(ReadCallback::new(ctx, on_read)) }; + } + + /// Write all of `data` at the file position, then call `on_write(ctx, ..)` + /// from the loop. + /// + /// # Safety + /// `data` must stay valid until `on_write` runs, also when the `File` is + /// dropped first: `on_write` still runs then. Once the loop has been torn + /// down ([`super::close_all_for_loop`]) it does not run, and a write that + /// was running goes on reading `data`: an owner that lets go of the bytes + /// after that gives them to the file first + /// ([`adopt_write_buffer`](Self::adopt_write_buffer)). + pub unsafe fn write( + &mut self, + data: &[u8], + ctx: *mut T, + on_write: unsafe fn(*mut T, sys::Result), + ) -> sys::Result<()> { + self.start_write( + WriteData::Borrowed(data.as_ptr(), data.len()), + Callback::new(ctx, on_write), + ) + } + + /// As [`write`](Self::write), for bytes the request should own: nothing + /// needs to outlive the call. + pub fn write_owned( + &mut self, + data: Vec, + ctx: *mut T, + on_write: unsafe fn(*mut T, sys::Result), + ) -> sys::Result<()> { + self.start_write(WriteData::Owned(data), Callback::new(ctx, on_write)) + } + + fn start_write( + &mut self, + data: WriteData, + writer: Callback>, + ) -> sys::Result<()> { + self.start(Request::Write { data })?; + // SAFETY: `inner` is live while the owner's `File` is; the work pool + // never looks at `writer`. + unsafe { (*self.inner.as_ptr()).writer = Some(writer) }; + Ok(()) + } + + /// The write that is out (if any) ends without its `on_write` running: for + /// an owner that will not be there. Its bytes still have to outlive it + /// ([`adopt_write_buffer`](Self::adopt_write_buffer)). + pub fn forget_writer(&mut self) { + // SAFETY: as in `start_write`. + unsafe { (*self.inner.as_ptr()).writer = None }; + } + + /// `buffer` is kept until the file itself is freed: for an owner about to + /// go away while a write that borrows `buffer` may still be running. + pub fn adopt_write_buffer(&mut self, buffer: Vec) { + // SAFETY: `inner` is live while the owner's `File` is; the work pool + // never looks at `adopted`, and a write that is out borrows the heap + // buffer, which does not move with the `Vec`. + unsafe { (*self.inner.as_ptr()).adopted = buffer }; + } + + fn start(&mut self, request: Request) -> sys::Result<()> { + let this = self.inner.as_ptr(); + // SAFETY: `inner` is live while the owner's `File` is, and idle (checked + // first), so the work pool is not looking at it. + unsafe { + if (*this).detached { + return Err(sys::Error::from_code(E::EBADF, request.tag()).with_fd((*this).fd)); + } + if (*this).state.load(Ordering::Acquire) != IDLE { + return Err(sys::Error::from_code(E::EBUSY, request.tag()).with_fd((*this).fd)); + } + (*this).wrote |= matches!(request, Request::Write { .. }); + (*this).request = request; + (*this).state.store(QUEUED, Ordering::Release); + if (*this).completing { + (*this).deferred = true; + } else { + Inner::schedule(this); + } + } + Ok(()) + } + + /// Ask the pending operation not to happen. `true` if it had not started: + /// its callback then reports `ECANCELED`. Otherwise it completes normally. + pub fn cancel(&mut self) -> bool { + // SAFETY: `inner` is live while the owner's `File` is. + unsafe { + (*self.inner.as_ptr()) + .state + .compare_exchange(QUEUED, CANCELED, Ordering::AcqRel, Ordering::Acquire) + .is_ok() + } + } +} + +impl Drop for File { + fn drop(&mut self) { + let this = self.inner.as_ptr(); + // SAFETY: `inner` is live; once `owner_gone` is set and an operation is + // out, whoever finishes it is the only one to touch it again. + unsafe { + (*this).owner_gone = true; + if (*this).state.load(Ordering::Acquire) == ORPHANED { + Inner::release(this); + return; + } + if (*this).state.load(Ordering::Acquire) == IDLE { + if (*this).completing { + // `complete` frees it once the owner's callback returns. + (*this).close(); + } else { + Inner::destroy(this); + } + return; + } + let _ = self.cancel(); + } + } +} + +impl Inner { + /// [`Link::shut`]: the loop is about to be freed. + unsafe fn shut(link: *mut Link) { + // SAFETY: `link` is the `link` field of a listed (live) `Inner`. + unsafe { + let this = link + .byte_sub(core::mem::offset_of!(Inner, link)) + .cast::(); + (*this).detached = true; + (*this).reader = None; + Link::remove(link); + if core::mem::take(&mut (*this).deferred) { + // Asked for from inside the owner's callback; the pool has not seen it. + (*this).request = Request::None; + (*this).state.store(IDLE, Ordering::Release); + return; + } + loop { + let state = (*this).state.load(Ordering::SeqCst); + match state { + // A result that is on its way to the port is collected by + // the loop's teardown drain. + IDLE | POSTED => return, + // The pool thread is left to finish (or skip) the request + // by itself, however long the device takes. + _ => { + if (*this) + .state + .compare_exchange(state, ORPHANED, Ordering::SeqCst, Ordering::SeqCst) + .is_ok() + { + break; + } + } + } + } + // What `schedule` added. + super::op_abandoned((*this).link.loop_); + (*(*this).link.loop_).sub_active(1); + if (*this).owner_gone { + Self::release(this); + } + } + } + + /// One of the two that still know about an orphaned request lets go. + /// + /// # Safety + /// `this` is live and orphaned; the caller does not touch it afterwards. + unsafe fn release(this: *mut Inner) { + // SAFETY: caller contract; the second caller is the only one left. + unsafe { + if (*this).released.swap(true, Ordering::AcqRel) { + let inner = bun_core::heap::take(this); + if inner.close_fd { + inner.fd.close(); + } + } + } + } + + /// # Safety + /// `this` is idle and its owner is gone. + unsafe fn destroy(this: *mut Inner) { + // SAFETY: caller contract; `remove` is idempotent. + unsafe { Link::remove(&raw mut (*this).link) }; + // SAFETY: caller contract. + let mut inner = unsafe { bun_core::heap::take(this) }; + inner.close(); + } + + /// No operation is out and none will be. What was only read from is closed + /// here and now: an owner that goes on to say the file has ended says so of + /// a file that is no longer open, which on Windows is what lets it be + /// replaced and its directory renamed. Closing what was written to is when + /// filter drivers (virus scanners) look at it, so that is not waited for. + fn close(&mut self) { + if !core::mem::take(&mut self.close_fd) { + return; + } + if self.wrote { + crate::closer::Closer::close(self.fd); + } else { + self.fd.close(); + } + } + + /// # Safety + /// `this` is live with a request in `QUEUED` (or `CANCELED`) state. + unsafe fn schedule(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + let loop_ = (*this).link.loop_; + (*loop_).add_active(1); + if let Request::Write { data } = &(*this).request { + if (*this).disk + && data.len() <= INLINE_WRITE_MAX + && (*this) + .state + .compare_exchange(QUEUED, RUNNING, Ordering::SeqCst, Ordering::SeqCst) + .is_ok() + { + (*this).result = Self::perform(this); + (*this).state.store(POSTED, Ordering::SeqCst); + return super::complete_from_loop(loop_, &raw mut (*this).op); + } + WorkPool::owe_write(); + if !(*this).disk { + WorkPool::write_held_up(); + } + } + super::op_submitted(loop_); + if (*this).disk { + WorkPool::schedule(&raw mut (*this).task); + } else { + WorkPool::schedule_wait(&raw mut (*this).task); + } + } + } + + /// Work-pool half. + unsafe fn run(task: *mut Task) { + // SAFETY: `task` is the field of an `Inner` that `File::start` scheduled. + // Until `state` leaves `RUNNING` (or `QUEUED`/`CANCELED`) this thread + // owns `request`, `buf` and `result`; the loop thread touches only + // `state`, `reader`, `owner_gone` and the teardown flags in the + // meantime. + unsafe { + let this = Inner::from_task_ptr(task); + let fd = (*this).fd; + let owed = matches!((*this).request, Request::Write { .. }); + let claimed = + (*this) + .state + .compare_exchange(QUEUED, RUNNING, Ordering::SeqCst, Ordering::SeqCst); + if claimed.is_ok() { + (*this).result = Self::perform(this); + } + if owed && (*this).disk { + WorkPool::write_settled(); + } else if owed { + WorkPool::held_up_write_settled(); + } + let from = match claimed { + Ok(_) => RUNNING, + Err(CANCELED) => { + (*this).result = + Err(sys::Error::from_code(E::ECANCELED, (*this).request.tag()).with_fd(fd)); + CANCELED + } + Err(_) => return Self::release(this), + }; + // The loop thread frees `this` when it has dequeued the packet, + // which can be before `post` returns: nothing of `this` is + // borrowed across it. + let port = (*this).port.clone(); + if (*this) + .state + .compare_exchange(from, POSTED, Ordering::SeqCst, Ordering::SeqCst) + .is_err() + { + return Self::release(this); + } + port.post(&raw mut (*this).op); + } + } + + /// # Safety + /// `this` is live and `RUNNING`, by the calling thread. + unsafe fn perform(this: *mut Inner) -> sys::Result { + // SAFETY: caller contract. + unsafe { + let fd = (*this).fd; + match &(*this).request { + Request::None => Ok(0), + Request::Read { len, offset, .. } => { + let buf = &mut (*this).buf; + buf.clear(); + buf.reserve(*len); + let spare = bun_core::vec::spare_bytes_mut(buf); + let spare = &mut spare[..*len]; + let result = match offset { + Some(offset) if (*this).shares_position => { + sys::windows::fs::pread_keeping_position(fd, spare, *offset) + } + Some(offset) => sys::pread(fd, spare, *offset as i64), + None => sys::read(fd, spare), + }; + if let Ok(n) = result { + bun_core::vec::commit_spare(buf, n); + } + result + } + Request::Write { data, .. } => { + let data: &[u8] = match data { + WriteData::Borrowed(data, len) => core::slice::from_raw_parts(*data, *len), + WriteData::Owned(data) => data, + }; + let mut written = 0usize; + let mut failure = None; + while written < data.len() { + match sys::write(fd, &data[written..]) { + Ok(0) => break, + Ok(n) => written += n, + Err(err) => { + failure = Some(err); + break; + } + } + } + match failure { + Some(err) if written == 0 => Err(err), + _ => Ok(written), + } + } + } + } + } + + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op) { + let this = op.cast::(); + // SAFETY: `op` is the first field of the `Inner` whose worker posted + // this packet; the worker is done with it. + unsafe { + (*loop_).sub_active(1); + (*this).state.store(IDLE, Ordering::Release); + (*this).completing = true; + let request = core::mem::replace(&mut (*this).request, Request::None); + let result = core::mem::replace(&mut (*this).result, Ok(0)); + match request { + _ if (*this).detached => {} + Request::None => {} + // Nobody is left to hear about a read once the owner is gone; + // a write's callback still runs because it is what releases + // the borrowed bytes. + Request::Read { .. } => { + if let Some(reader) = (*this).reader + && !(*this).owner_gone + { + let event = match result { + Ok(0) => ReadEvent::Eof, + Ok(_) => ReadEvent::Data(&mut (*this).buf), + Err(err) => ReadEvent::Err(err), + }; + reader.invoke(event); + } + } + Request::Write { .. } => { + if let Some(writer) = (*this).writer.take() { + writer.invoke(result); + } + } + } + (*this).completing = false; + // The callback may have dropped the owner's `File`, or asked for + // the next operation. + if core::mem::take(&mut (*this).deferred) { + if !(*this).detached { + Self::schedule(this); + return; + } + (*this).request = Request::None; + (*this).state.store(IDLE, Ordering::Release); + } + if (*this).owner_gone { + Self::destroy(this); + } + } + } +} diff --git a/src/io/windows/ipc_frame.rs b/src/io/windows/ipc_frame.rs new file mode 100644 index 000000000000..e23ad2b9b897 --- /dev/null +++ b/src/io/windows/ipc_frame.rs @@ -0,0 +1,138 @@ +//! The framing Node.js-compatible IPC pipes carry on Windows. It is libuv's +//! (`uv_pipe_t` opened with `ipc = 1`), so a Bun process and a Node process on +//! either end of a channel agree on it byte for byte: +//! +//! ```text +//! frame := header [socket_transfer] [data] +//! header := u32 flags, u32 reserved1, u32 data_length, u32 reserved2 (little-endian) +//! ``` +//! +//! `reserved2` must be zero, `data_length` must be zero without +//! [`FLAG_HAS_DATA`], and [`FLAG_TRANSFER_IS_CONNECTION`] is only valid with +//! [`FLAG_HAS_SOCKET_TRANSFER`]. A socket transfer is a `WSAPROTOCOL_INFOW` +//! from `WSADuplicateSocketW` followed by the sender's deferred listen error. +//! Frame boundaries mean nothing to the payload, which is one byte stream. + +pub const HEADER_LEN: usize = 16; +pub const SOCKET_TRANSFER_LEN: usize = 632; +/// Offset of the `u32` deferred error inside a socket transfer. +pub const SOCKET_TRANSFER_PROTOCOL_INFO_LEN: usize = 628; + +const FLAG_HAS_DATA: u32 = 0x01; +const FLAG_HAS_SOCKET_TRANSFER: u32 = 0x02; +const FLAG_TRANSFER_IS_CONNECTION: u32 = 0x04; +const VALID_FLAGS: u32 = 0x07; + +/// The header of a frame that carries `data_len` payload bytes and no socket. +/// Header and payload must reach the pipe in one write so frames from +/// different writers cannot interleave. +pub fn data_header(data_len: u32) -> [u8; HEADER_LEN] { + let mut header = [0u8; HEADER_LEN]; + let flags = if data_len == 0 { 0 } else { FLAG_HAS_DATA }; + header[0..4].copy_from_slice(&flags.to_le_bytes()); + header[8..12].copy_from_slice(&data_len.to_le_bytes()); + header +} + +/// The peer broke the framing; the channel cannot be resynchronized. +pub struct InvalidFrame; + +pub enum Event<'a> { + /// The next bytes of the payload stream. + Data(&'a [u8]), + /// A socket the peer handed over. It exists as soon as the peer sent it: + /// a receiver that does not want it still has to materialize it + /// (`WSASocketW(FROM_PROTOCOL_INFO…)`) and close it. + SocketTransfer { info: &'a [u8; SOCKET_TRANSFER_LEN] }, +} + +/// Incremental decoder for the receiving side. Input may be split anywhere. +pub struct Decoder { + header: [u8; HEADER_LEN], + header_len: usize, + transfer: Option>, + transfer_len: usize, + transfer_remaining: usize, + payload_remaining: usize, +} + +impl Default for Decoder { + fn default() -> Self { + Self::new() + } +} + +impl Decoder { + pub const fn new() -> Self { + Self { + header: [0; HEADER_LEN], + header_len: 0, + transfer: None, + transfer_len: 0, + transfer_remaining: 0, + payload_remaining: 0, + } + } + + /// Decode `input`, handing each piece to `sink` in wire order. + pub fn feed( + &mut self, + mut input: &[u8], + mut sink: impl FnMut(Event<'_>), + ) -> Result<(), InvalidFrame> { + while !input.is_empty() { + if self.transfer_remaining > 0 { + let take = self.transfer_remaining.min(input.len()); + let transfer = self + .transfer + .get_or_insert_with(|| Box::new([0; SOCKET_TRANSFER_LEN])); + transfer[self.transfer_len..self.transfer_len + take] + .copy_from_slice(&input[..take]); + self.transfer_len += take; + self.transfer_remaining -= take; + input = &input[take..]; + if self.transfer_remaining == 0 { + self.transfer_len = 0; + sink(Event::SocketTransfer { info: transfer }); + } + continue; + } + if self.payload_remaining > 0 { + let take = self.payload_remaining.min(input.len()); + sink(Event::Data(&input[..take])); + self.payload_remaining -= take; + input = &input[take..]; + continue; + } + + let take = (HEADER_LEN - self.header_len).min(input.len()); + self.header[self.header_len..self.header_len + take].copy_from_slice(&input[..take]); + self.header_len += take; + input = &input[take..]; + if self.header_len < HEADER_LEN { + break; + } + self.header_len = 0; + + let field = |index: usize| { + let mut bytes = [0u8; 4]; + bytes.copy_from_slice(&self.header[index * 4..index * 4 + 4]); + u32::from_le_bytes(bytes) + }; + let (flags, data_length, reserved2) = (field(0), field(2), field(3)); + if flags & !VALID_FLAGS != 0 + || reserved2 != 0 + || (flags & FLAG_TRANSFER_IS_CONNECTION != 0 + && flags & FLAG_HAS_SOCKET_TRANSFER == 0) + || (flags & FLAG_HAS_DATA == 0 && data_length != 0) + { + return Err(InvalidFrame); + } + if flags & FLAG_HAS_SOCKET_TRANSFER != 0 { + self.transfer_remaining = SOCKET_TRANSFER_LEN; + } + self.payload_remaining = data_length as usize; + } + Ok(()) + } +} diff --git a/src/io/windows/mod.rs b/src/io/windows/mod.rs new file mode 100644 index 000000000000..86dfc328d70c --- /dev/null +++ b/src/io/windows/mod.rs @@ -0,0 +1,296 @@ +//! Pipes, the console and plain files on Windows, driven by the loop's +//! completion port (`bun_uws_sys::iocp`). +//! +//! Memory the kernel may write to (an `OVERLAPPED`, a read buffer) belongs to +//! the kernel from submit until the completion packet is dequeued. Every +//! operation therefore lives in its own allocation that is freed only from its +//! own completion; an owner that goes away first orphans it. Cancelling and +//! closing only ask for the completion. + +use core::cell::{Cell, RefCell}; +use core::ffi::c_void; +use std::sync::Arc; + +use bun_uws_sys::Loop; +use bun_uws_sys::iocp; + +pub mod file; +pub mod ipc_frame; +pub mod pipe; +pub mod pipe_server; +pub(crate) mod sys; +pub mod tty; +mod tty_input; +mod tty_output; + +pub use file::File; +pub use pipe::{ConnectRequest, Pipe, PipeOrigin, ReadEvent, Refusal}; +pub use pipe_server::PipeServer; +pub use tty::Tty; + +// ────────────────────────────────────────────────────────────────────────── +// Loop bookkeeping +// ────────────────────────────────────────────────────────────────────────── + +/// A packet that leads to `op` is now owed to `loop_`, which must keep turning +/// and must not close its port until it is dequeued. The loop takes it off the +/// count as it dequeues it. (`us_iocp_wait_start` counts its own.) +pub(crate) use iocp::us_iocp_op_submitted as op_submitted; + +/// The packet counted by [`op_submitted`] will not come: a helper thread that +/// cannot be stopped was left to finish the operation by itself, and frees it. +pub(crate) use iocp::us_iocp_op_abandoned as op_abandoned; + +/// Have `op`'s `complete` run from the loop rather than re-entrantly: from +/// its next tick, before that tick takes packets from the port, in the order of +/// these calls. No kernel I/O stands behind it: `complete` learns how the +/// operation went from what the caller stored in it, not from its OVERLAPPED. +/// +/// # Safety +/// `loop_` is the live loop of the calling thread; `op` stays allocated until +/// its `complete` has run, and is not on its way through here already. +pub(crate) unsafe fn complete_from_loop(loop_: *mut Loop, op: *mut iocp::Op) { + // SAFETY: caller contract. + unsafe { + op_submitted(loop_); + iocp::us_iocp_op_ready(loop_, op); + } +} + +/// The loop's completion port as seen from other threads. It is a duplicate of +/// the loop's handle, so a helper thread that finishes after the loop is gone +/// posts into a port nobody reads instead of into a closed (or reused) handle. +pub(crate) struct Port(sys::HANDLE); + +// SAFETY: a completion port handle may be posted to from any thread. +unsafe impl Send for Port {} +// SAFETY: as above; `post` takes `&self` and the handle is immutable. +unsafe impl Sync for Port {} + +impl Port { + /// Hand `op` to the loop thread. The loop-side accounting + /// ([`op_submitted`]) was done by the loop thread when it started the work. + /// + /// The port is this `Port`'s own open handle, so the one thing the call + /// can fail for is the kernel having no memory for the packet. The loop + /// would wait for `op` forever, and the calling thread has no other way + /// to reach it: that is the process out of memory. + /// + /// # Safety + /// `op` stays allocated until its `complete` has run. + pub(crate) unsafe fn post(&self, op: *mut iocp::Op) { + // SAFETY: caller contract. + if unsafe { sys::PostQueuedCompletionStatus(self.0, 0, 0, op.cast()) } == 0 { + bun_core::out_of_memory(); + } + } +} + +impl Drop for Port { + fn drop(&mut self) { + // SAFETY: `self.0` is the duplicate made in `port_for`. + unsafe { sys::CloseHandle(self.0) }; + } +} + +thread_local! { + static PORTS: RefCell)>> = const { RefCell::new(Vec::new()) }; + static OPEN: Cell<*mut Link> = const { Cell::new(core::ptr::null_mut()) }; +} + +/// The cross-thread handle to `loop_`'s port, made on first use. +/// +/// # Safety +/// `loop_` is the live loop of the calling thread. +pub(crate) unsafe fn port_for(loop_: *mut Loop) -> Option> { + PORTS.with_borrow_mut(|ports| { + if let Some((_, port)) = ports.iter().find(|(l, _)| *l == loop_) { + return Some(port.clone()); + } + // SAFETY: caller contract. + let dup = sys::duplicate(unsafe { iocp::us_loop_iocp(loop_) }).ok()?; + let port = Arc::new(Port(dup)); + ports.push((loop_, port.clone())); + Some(port) + }) +} + +// ────────────────────────────────────────────────────────────────────────── +// What a loop's thread has open +// ────────────────────────────────────────────────────────────────────────── + +/// First field of every pipe, pipe server and console state. The states cache +/// their loop and are freed from completions that loop dispatches, so a loop +/// that is about to be freed (a Worker's, `spawnSync`'s) has to close whatever +/// is still open on it first: [`close_all_for_loop`] walks this list. +#[repr(C)] +pub(crate) struct Link { + pub(crate) loop_: *mut Loop, + prev: *mut Link, + next: *mut Link, + /// Cancel the state's I/O and detach it from the loop. The state stays + /// allocated for its owner, which finds it closed. + shut: unsafe fn(*mut Link), +} + +impl Link { + pub(crate) const fn new(loop_: *mut Loop, shut: unsafe fn(*mut Link)) -> Link { + Link { + loop_, + prev: core::ptr::null_mut(), + next: core::ptr::null_mut(), + shut, + } + } + + /// # Safety + /// `this` is at its final address and not listed. + pub(crate) unsafe fn insert(this: *mut Link) { + let head = OPEN.get(); + // SAFETY: caller contract; `head` is a listed (live) link or null. + unsafe { + (*this).prev = core::ptr::null_mut(); + (*this).next = head; + if !head.is_null() { + (*head).prev = this; + } + } + OPEN.set(this); + } + + /// # Safety + /// `this` is live and was inserted on this thread. Idempotent. + pub(crate) unsafe fn remove(this: *mut Link) { + // SAFETY: caller contract; neighbours are listed (live) links. + unsafe { + let (prev, next) = ((*this).prev, (*this).next); + if prev.is_null() { + if OPEN.get() != this { + return; + } + OPEN.set(next); + } else { + (*prev).next = next; + } + if !next.is_null() { + (*next).prev = prev; + } + (*this).prev = core::ptr::null_mut(); + (*this).next = core::ptr::null_mut(); + } + } +} + +/// Close every pipe, pipe server, console, file and pipe connect still open +/// on `loop_`, on the loop's thread. `us_loop_free` starts with this +/// (`Bun__closeAllForLoop`) and then collects the cancelled operations. +pub fn close_all_for_loop(loop_: *mut Loop) { + let mut cursor = OPEN.get(); + while !cursor.is_null() { + // SAFETY: listed links are live; `shut` unlinks `cursor`, so the next + // pointer is read first. + unsafe { + let next = (*cursor).next; + if (*cursor).loop_ == loop_ { + ((*cursor).shut)(cursor); + } + cursor = next; + } + } + // A loop can be freed from its thread's exit, after this thread-local is gone. + let _ = PORTS.try_with(|ports| ports.borrow_mut().retain(|(l, _)| *l != loop_)); +} + +// ────────────────────────────────────────────────────────────────────────── +// Type-erased callbacks +// ────────────────────────────────────────────────────────────────────────── + +/// `unsafe fn(*mut T, A)` plus its `*mut T`, with `T` erased. +pub(crate) struct Callback { + ctx: *mut c_void, + f: *const (), + call: unsafe fn(*const (), *mut c_void, A), +} + +impl Clone for Callback { + fn clone(&self) -> Self { + *self + } +} +impl Copy for Callback {} + +impl Callback { + pub(crate) fn new(ctx: *mut T, f: unsafe fn(*mut T, A)) -> Self { + unsafe fn call(f: *const (), ctx: *mut c_void, arg: A) { + // SAFETY: `f` was erased from exactly this type in `new`. + let f = unsafe { core::mem::transmute::<*const (), unsafe fn(*mut T, A)>(f) }; + // SAFETY: the owner that supplied `ctx` keeps it valid for the callback. + unsafe { f(ctx.cast::(), arg) } + } + Callback { + ctx: ctx.cast(), + f: f as *const (), + call: call::, + } + } + + /// # Safety + /// The `ctx` given to `new` is still valid. + pub(crate) unsafe fn invoke(self, arg: A) { + // SAFETY: caller contract. + unsafe { (self.call)(self.f, self.ctx, arg) } + } +} + +/// [`Callback`] for a [`ReadEvent`], whose lifetime `Callback` cannot name. +#[derive(Clone, Copy)] +pub(crate) struct ReadCallback { + ctx: *mut c_void, + f: *const (), + call: unsafe fn(*const (), *mut c_void, ReadEvent<'_>), +} + +impl ReadCallback { + pub(crate) fn new(ctx: *mut T, f: unsafe fn(*mut T, ReadEvent<'_>)) -> Self { + unsafe fn call(f: *const (), ctx: *mut c_void, event: ReadEvent<'_>) { + // SAFETY: `f` was erased from exactly this type in `new`. + let f = + unsafe { core::mem::transmute::<*const (), unsafe fn(*mut T, ReadEvent<'_>)>(f) }; + // SAFETY: the owner that supplied `ctx` keeps it valid while it reads. + unsafe { f(ctx.cast::(), event) } + } + ReadCallback { + ctx: ctx.cast(), + f: f as *const (), + call: call::, + } + } + + /// # Safety + /// The `ctx` given to `new` is still valid. + pub(crate) unsafe fn invoke(self, event: ReadEvent<'_>) { + // SAFETY: caller contract. + unsafe { (self.call)(self.f, self.ctx, event) } + } +} + +/// UTF-8 → NUL-terminated UTF-16 for a Win32 `W` call. +pub(crate) fn to_wide_z(bytes: &[u8]) -> Vec { + bun_core::handle_oom(bun_core::strings::to_utf16_alloc_for_real( + bytes, false, true, + )) +} + +/// Run `f(context)` on one of the system's long-running worker threads: for +/// calls that can block for as long as a peer likes, which must not occupy +/// Bun's own work pool. +/// +/// # Safety +/// `f` must be sound to run on another thread with `context`. +pub(crate) unsafe fn queue_blocking_work( + f: sys::LPTHREAD_START_ROUTINE, + context: *mut c_void, +) -> bool { + // SAFETY: caller contract. + unsafe { sys::QueueUserWorkItem(f, context, sys::WT_EXECUTELONGFUNCTION) != 0 } +} diff --git a/src/io/windows/pipe.rs b/src/io/windows/pipe.rs new file mode 100644 index 000000000000..ad5d96e21280 --- /dev/null +++ b/src/io/windows/pipe.rs @@ -0,0 +1,3779 @@ +//! A pipe HANDLE driven by the loop. +//! +//! How a pipe is driven depends on where the HANDLE came from: +//! +//! - [`Mode::Owned`]: an overlapped end Bun created. It is associated with the +//! loop's completion port and every operation is a plain overlapped call. +//! - [`Mode::Event`]: an overlapped end somebody else created (inherited stdio, +//! an fd adopted from JS). It is never associated with the port — the +//! association belongs to the file object, which other processes share — so +//! each operation carries an event with the low bit set (no packet is queued +//! anywhere) and a wait on that event delivers it to the loop. +//! - [`Mode::Sync`]: a synchronous end. Reads block on the pipe's own reader +//! thread ([`SyncReader`]) and writes on a helper thread; each posts its +//! result to the loop. +//! - [`Mode::Unknown`]: somebody else's end that nobody has classified yet. +//! Finding out takes the file object's lock ([`classify`]), so the helper +//! thread of its first read or write does that before anything else, and the +//! loop moves the pipe to `Event` or `Sync` when it hears. +//! +//! What is pending on a pipe is a zero-byte read. Bytes leave the pipe when +//! they are about to be handed to the owner, so a peer that is told they were +//! read (`FlushFileBuffers`) is told so of bytes the owner has, an owner that +//! wants what the pipe holds now can have it ([`Pipe::drain`]), and one that +//! pauses leaves everything in the pipe. The exception is a message-type pipe +//! somebody else serves: a zero-length message, which says its writer has +//! finished, ends only a read that asks for bytes. That read is left to finish +//! when its owner stops reading, and what it brings is kept. + +use core::cell::Cell; +use core::ffi::c_void; +use core::ptr::{self, NonNull}; +use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, Ordering}; +use std::rc::Rc; +use std::sync::Arc; + +use bun_sys::{self as sys, E, Fd, FdExt as _, Tag}; +use bun_uws_sys::Loop; +use bun_uws_sys::iocp::{self, Op, Wait}; +use bun_windows_sys::ntdll::NtFsControlFile; +use bun_windows_sys::{FILE_PIPE_PEEK_BUFFER, FSCTL_PIPE_PEEK}; + +use super::sys as win; +use super::sys::{HANDLE, INVALID_HANDLE_VALUE, Win32Error}; +use super::{Callback, Link, Port, ReadCallback}; + +bun_core::declare_scope!(WinPipe, hidden); + +/// Room made for a read, unless the pipe is known to hold more and the owner +/// takes more at a time ([`Pipe::set_read_size`]). +pub const DEFAULT_READ_SIZE: usize = 64 * 1024; + +/// `WriteFile` takes a `DWORD`; larger buffers go out in several calls. +const MAX_WRITE_CHUNK: usize = 0x7fff_f000; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Mode { + Owned, + Event, + Sync, + Unknown, +} + +/// What [`classify`] found a pipe HANDLE to be. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Kind { + Synchronous, + Overlapped, + /// Not something this module can drive; the `errno` its I/O fails with. + Unusable(E), +} + +/// What the first classification of each standard handle found. A standard +/// handle is the same file object for the life of the process, and the pipe +/// mode [`classify`] set belongs to the pipe end, so both hold for every later +/// open: the second and third stream, every Worker. +static STD_MODES: [AtomicU8; 3] = [AtomicU8::new(0), AtomicU8::new(0), AtomicU8::new(0)]; +const STD_MODE_EVENT: u8 = 1; +const STD_MODE_SYNC: u8 = 2; + +/// Where a pipe HANDLE came from, which decides how it is driven. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum PipeOrigin { + /// An overlapped end of a pipe Bun created (a spawned child's stdio, a + /// pseudoconsole pipe). It is associated with the loop's completion port. + Created, + /// A HANDLE to a file object that was opened as [`Created`](Self::Created) + /// on this loop before (a duplicate): the port has it already, and a file + /// object is associated once. + Associated, + /// A synchronous byte-mode end Bun created (`bun_sys::pipe()`). Nothing + /// about it has to be found out. + CreatedSynchronous, + /// An end somebody else created (inherited stdio, an fd that came from + /// JS). A standard handle is duplicated and the original is left alone. + Foreign, + /// An end this process inherited and that nobody else does I/O on, by the + /// protocol it came with (the IPC channel a parent names in + /// `NODE_CHANNEL_FD`). A completion port on its file object then affects + /// nobody else, so an overlapped end is driven through the loop's port + /// like a pipe Bun created. A standard handle is [`Foreign`](Self::Foreign) + /// all the same: others in this process use it by number too. + InheritedUnshared, +} + +impl PipeOrigin { + /// What `is_pollable` of a reader's or writer's `start` says on Windows: + /// set for an overlapped pipe end Bun created; clear for anything else, + /// which [`Source::open`](crate::source::Source::open) classifies and, if + /// it is a pipe, treats as somebody else's. + pub(crate) fn from_is_pollable(is_pollable: bool) -> PipeOrigin { + if is_pollable { + PipeOrigin::Created + } else { + PipeOrigin::Foreign + } + } +} + +/// How a write hears that the kernel refused it at once (the reader of the +/// pipe is gone, say). +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Refusal { + /// From the `write` call itself; `on_write` never runs. + Returned, + /// As of a write that fails later: from `on_write`, from the loop. For a + /// caller with nowhere to take a failure to yet. + Posted, +} + +/// What a read produced. +pub enum ReadEvent<'a> { + /// Bytes from the pipe, after whatever + /// [`lend_read_buffer`](Pipe::lend_read_buffer) handed over. The `Vec` may + /// be taken (`mem::take`, `mem::swap` with an empty one); whatever is left + /// in it is discarded and its capacity reused for the next read. + Data(&'a mut Vec), + /// The write side is gone. Reading has stopped. + Eof, + /// The other end has finished writing and is still open: on a + /// message-type pipe it sent the zero-length message that says so (what + /// [`write_end_marker`](Pipe::write_end_marker) sends). Reading has + /// stopped; the pipe can still be written to. + EndOfWrite, + /// Reading has stopped. + Err(sys::Error), +} + +/// The owner's handle to a pipe. Dropping it closes the pipe without telling +/// anyone: in-flight operations are cancelled and orphaned, and the HANDLE is +/// released once the last of them has been collected. +pub struct Pipe { + inner: NonNull, +} + +bitflags::bitflags! { + #[derive(Clone, Copy)] + struct Flags: u16 { + /// The owner wants `ReadEvent`s. + const READING = 1 << 0; + const REFD = 1 << 1; + /// Counted in the loop's `active`. + const KEEPING_ALIVE = 1 << 2; + /// `close` ran; nothing new is submitted and nobody is called back. + const CLOSING = 1 << 3; + /// EOF or a read error was delivered. + const READ_ENDED = 1 << 4; + /// The loop is going away: nothing may touch it again. + const DETACHED = 1 << 5; + /// The owner's `Pipe` is gone; free `Inner` when nothing refers to it. + const OWNER_GONE = 1 << 6; + /// The owner went away without asking to hear about unfinished writes. + const SILENT = 1 << 7; + /// `handle` is this pipe's own duplicate of a standard handle. + const DUPLICATED = 1 << 8; + /// `MESSAGE_TYPE` has been looked up. + const TYPE_KNOWN = 1 << 9; + /// The pipe was created as a message-type pipe. + const MESSAGE_TYPE = 1 << 10; + /// The kernel refused a write because the other end is gone. + const PEER_GONE = 1 << 11; + } +} + +#[repr(C)] +struct Inner { + link: Link, + handle: HANDLE, + /// How the HANDLE is released; `None` leaves it to whoever lent it. + close_fd: Option, + mode: Mode, + flags: Flags, + port: Option>, + /// The [`STD_MODES`] entry of the standard handle this is a duplicate of. + std_slot: Option<&'static AtomicU8>, + + reader: Option, + read_op: *mut ReadOp, + read_size: usize, + sync_reader: Option, + /// `Sync` and `Unknown` modes: the HANDLE the write threads use. + sync_handle: Option>, + /// The write a helper thread has. + sync_write: Option>, + /// The last one, to use again once its thread has let go of it. + sync_spare: Option>, + + /// Accepted, not yet handed to the kernel (every mode but `Owned` runs one + /// write at a time). + write_head: *mut WriteOp, + write_tail: *mut WriteOp, + /// The one write the kernel has in `Event` mode. + event_write: *mut WriteOp, + writes_in_flight: u32, + /// A write longer than one `WriteFile` can take that still has chunks to + /// go: nothing queued behind it may reach the kernel before its last one. + chunked_write: *mut WriteOp, + /// A finished write kept for the next one. + spare_write: *mut WriteOp, + /// The [`Pipe::flush_peer`] request that is out. + flush_op: *mut FlushOp, + write_lane: Lane, + /// Buffers of writes that outlived the owner who lent them. + adopted: Vec>, + /// The buffer of a lent write that is over, for its owner to take back + /// from the write's callback or from `write_lent`'s refusal. + lent_back: Option>, + blocking_event: HANDLE, + + /// Packets that will dereference this allocation when dequeued. + pending: u32, + /// Stack frames that are inside a callback and will touch this allocation + /// when it returns. + pins: u32, +} + +/// The event an `Event`-mode operation completes through and the wait that +/// brings it to the loop. +struct Lane { + event: HANDLE, + wait: *mut Wait, +} + +impl Lane { + const NONE: Lane = Lane { + event: ptr::null_mut(), + wait: ptr::null_mut(), + }; + + /// # Safety + /// `loop_` is the live loop of the calling thread. + unsafe fn ensure(&mut self, loop_: *mut Loop) -> Result<(), Win32Error> { + if !self.event.is_null() { + return Ok(()); + } + // SAFETY: plain Win32 call; manual-reset so the kernel's own reset at + // the start of each operation is the only one. + let event = unsafe { win::CreateEventW(ptr::null_mut(), 1, 0, ptr::null()) }; + if event.is_null() { + return Err(win::last_error()); + } + // SAFETY: caller contract. + let wait = unsafe { iocp::us_iocp_wait_create(loop_) }; + if wait.is_null() { + // SAFETY: `event` was just created. + unsafe { win::CloseHandle(event) }; + return Err(Win32Error::NOT_ENOUGH_MEMORY); + } + self.event = event; + self.wait = wait; + Ok(()) + } + + /// `op` pended on `handle` with this lane's event and no wait could be + /// registered for it, so nothing would ever announce its completion: take + /// it back from the kernel. The wait here is for the cancellation, which + /// the pipe driver completes by itself; no peer has a say in it. + /// + /// # Safety + /// `op` is the pending operation; it carries this lane's event. + unsafe fn take_back(&self, handle: HANDLE, op: *mut Op) -> (Win32Error, usize) { + // SAFETY: caller contract. + unsafe { + win::CancelIoEx(handle, op.cast()); + bun_sys::windows::kernel32::WaitForSingleObject(self.event, bun_sys::windows::INFINITE); + // The I/O is over, which is what fills the OVERLAPPED in. + match win::status_to_win32((*op).status()) { + Win32Error::OPERATION_ABORTED => { + (Win32Error::NOT_ENOUGH_MEMORY, (*op).bytes_transferred()) + } + raced => (raced, (*op).bytes_transferred()), + } + } + } + + /// # Safety + /// No wait is armed and no operation references the event. + unsafe fn release(&mut self) { + if !self.wait.is_null() { + // SAFETY: caller contract. + unsafe { iocp::us_iocp_wait_free(self.wait) }; + self.wait = ptr::null_mut(); + } + if !self.event.is_null() { + // SAFETY: caller contract. + unsafe { win::CloseHandle(self.event) }; + self.event = ptr::null_mut(); + } + } +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum ReadState { + Idle, + InFlight, + /// Completed while the owner was not reading; `outcome` is what it found. + Held, + /// A held result on its way back through the port. + Replaying, + /// Inside the owner's callback. + Delivering, +} + +enum Outcome { + None, + Data, + Eof, + EndOfWrite, + Err(sys::Error), +} + +#[repr(C)] +struct ReadOp { + op: Op, + /// Outlives the operation: a pipe is freed only once `pending` is zero. + pipe: *mut Inner, + state: ReadState, + outcome: Outcome, + buf: Vec, + /// What `buf` held when the read was submitted (see + /// [`Pipe::lend_read_buffer`]). The read lands after it, in spare + /// capacity reserved by the loop thread, and only the loop thread touches + /// the `Vec` itself. + kept: usize, + /// [`Pipe::take_read_buffer`] copied those bytes out from under a read in + /// flight. + kept_taken: bool, + /// Where the read in flight lands: `buf`'s spare capacity. + dest: *mut u8, + posted: Posted, + /// From the reader thread of an `Unknown` pipe, with its first answer. + verdict: Option, + lane: Lane, + /// The most this read may take: the owner's read size when it was + /// submitted. Every mode reads against this one number. + max_len: u32, + /// `read_stop` took the wait out of the kernel: an aborted completion is + /// not an error. + stopped: bool, + /// The operation out waits for data (a zero-byte read) and takes none. Its + /// completion says the pipe is readable; the loop then asks for the bytes, + /// if its owner still wants them. + zero_wait: bool, + /// `Sync` mode: what the pipe held when the thread last took from it. + held: usize, + /// `Sync` and `Unknown` modes, once the reader thread has taken the + /// request: who finishes it (`READ_*`). + finisher: AtomicU8, +} + +const READ_REQUESTED: u8 = 0; +/// A packet for it is on its way to the loop: the reader thread is handing +/// the result to the port, or the loop took the request back unanswered. +const READ_POSTED: u8 = 1; +/// The pipe closed while the thread had the request: the thread frees it. +const READ_ORPHANED: u8 = 2; +/// The loop thread is waiting for the answer ([`SyncReader::take`]), which is +/// signalled to it and has no packet. +const READ_AWAITED: u8 = 3; +/// As `READ_AWAITED`, and the reader thread is reading bytes it found in the +/// pipe: the loop thread waits until it has them. +const READ_TAKING: u8 = 4; + +/// How long the loop thread waits for the reader thread to start on what is in +/// the pipe: microseconds, unless the thread has no CPU or another process is +/// ahead of it on the HANDLE. +const TAKE_WAIT_MS: u32 = 1; +/// How often in one tick of the loop a `Sync` pipe is read. +const TAKES_PER_TICK: usize = 8; + +type WriteResult = sys::Result; + +/// Bytes waiting in the pipe `handle`. What `PeekNamedPipe` does in four system +/// calls, three of them for an event of its own, in one. `event` is what to +/// wait on should the call pend on an overlapped HANDLE; on a synchronous one, +/// which needs none, the call itself returns when it is over. +/// +/// # Safety +/// `handle` is open, and `event` is null or an event nothing else uses now. +unsafe fn bytes_in_pipe(handle: HANDLE, event: HANDLE) -> Result { + const STATUS_PENDING: win::NTSTATUS = win::NTSTATUS(0x103); + let mut peek = FILE_PIPE_PEEK_BUFFER { + NamedPipeState: 0, + ReadDataAvailable: 0, + NumberOfMessages: 0, + MessageLength: 0, + }; + let mut iosb: win::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + // SAFETY: caller contract; the out-params are live locals, which outlive + // the call because it is waited for, and the output length is `peek`'s. + // With no context, no packet is queued on the port of `handle`. + let mut status = unsafe { + NtFsControlFile( + handle, + event, + ptr::null_mut(), + ptr::null_mut(), + &raw mut iosb, + FSCTL_PIPE_PEEK, + ptr::null_mut(), + 0, + (&raw mut peek).cast(), + size_of::() as u32, + ) + }; + if status == STATUS_PENDING { + bun_sys::windows::kernel32::WaitForSingleObject(event, bun_sys::windows::INFINITE); + status = win::NTSTATUS(iosb.Status as u32); + } + // BUFFER_OVERFLOW: there are bytes, and no room was offered for them. + if status == win::NTSTATUS::SUCCESS || status == win::NTSTATUS::BUFFER_OVERFLOW { + Ok(peek.ReadDataAvailable) + } else { + Err(Win32Error::from_ntstatus(status)) + } +} + +/// How an operation went when Bun queued its packet itself, because the call +/// finished or failed without the kernel queuing one, or a helper thread made +/// it: the result and the bytes moved. `None` on an operation means the +/// kernel's packet (or the wait packet of an I/O that pended) brought it, and +/// only then does its OVERLAPPED say how it went. +type Posted = Option<(Win32Error, usize)>; + +#[repr(C)] +struct WriteOp { + op: Op, + pipe: *mut Inner, + next: *mut WriteOp, + data: *const u8, + len: usize, + done: usize, + chunk: u32, + /// The bytes when the operation owns them (`data` points into it). While + /// a helper thread has the write they are in its [`SyncWrite`]. + owned: Vec, + /// The bytes are the caller's buffer, which it takes back when the write + /// is over ([`Pipe::write_lent`]). + lent: bool, + callback: Option>, + posted: Posted, + /// What ended the write short of `len`. + error: Option, + /// `Unknown` mode: what the helper thread found; nothing was written + /// unless that is `Synchronous`. + verdict: Option, +} + +// ────────────────────────────────────────────────────────────────────────── +// Opening +// ────────────────────────────────────────────────────────────────────────── + +impl Pipe { + /// Take over the pipe end `fd`. It is closed with the pipe when `close_fd` + /// is set (a standard handle never is). On `Err` the caller still owns `fd`. + /// + /// An `fd` that came from outside (`Foreign`, `InheritedUnshared`) may be + /// no HANDLE at all, or a HANDLE to something else: that fails here. + pub fn open(loop_: *mut Loop, fd: Fd, origin: PipeOrigin, close_fd: bool) -> sys::Result { + if matches!(origin, PipeOrigin::Foreign | PipeOrigin::InheritedUnshared) { + // `GetFileType` does not take the file object's lock. + match bun_sys::File::borrow(&fd).kind() { + Ok(bun_sys::FileKind::NamedPipe) => {} + Ok(_) => return Err(sys::Error::from_code(E::ENOTSOCK, Tag::open).with_fd(fd)), + Err(err) => { + return Err(sys::Error { + syscall: Tag::open, + ..err + } + .with_fd(fd)); + } + } + } + Self::open_classified(loop_, fd, origin, close_fd) + } + + /// [`open`](Self::open) by a caller that `GetFileType` has told `fd` is a + /// pipe. + pub(crate) fn open_classified( + loop_: *mut Loop, + fd: Fd, + origin: PipeOrigin, + close_fd: bool, + ) -> sys::Result { + match origin { + PipeOrigin::Created => Self::open_created(loop_, fd, close_fd).map(Self::of_byte_type), + PipeOrigin::Associated => Ok(Self::of_byte_type(Self::create( + loop_, + fd.native(), + close_fd.then_some(fd), + Mode::Owned, + ))), + PipeOrigin::CreatedSynchronous => Ok(Self::create( + loop_, + fd.native(), + close_fd.then_some(fd), + Mode::Sync, + )), + PipeOrigin::Foreign => Self::open_foreign(loop_, fd, close_fd), + PipeOrigin::InheritedUnshared => Self::open_inherited_unshared(loop_, fd, close_fd), + } + } + + fn open_created(loop_: *mut Loop, fd: Fd, close_fd: bool) -> sys::Result { + let handle = fd.native(); + // SAFETY: `loop_` is the caller's live loop. + let port = unsafe { iocp::us_loop_iocp(loop_) }; + if bun_sys::windows::CreateIoCompletionPort(handle, port, 0, 0).is_err() { + return Err(sys::Error::from_win32(win::last_error(), Tag::open).with_fd(fd)); + } + Ok(Self::create( + loop_, + handle, + close_fd.then_some(fd), + Mode::Owned, + )) + } + + /// A pipe-server instance that was associated with `loop_`'s port when it + /// was created. + pub(crate) fn from_associated(loop_: *mut Loop, handle: HANDLE) -> Pipe { + Self::of_byte_type(Self::create( + loop_, + handle, + Some(Fd::from_system(handle)), + Mode::Owned, + )) + } + + /// Every pipe Bun makes is byte-type: nothing has to be asked. + fn of_byte_type(self) -> Pipe { + // SAFETY: `inner` is live while `self` is. + unsafe { (*self.raw()).flags.insert(Flags::TYPE_KNOWN) }; + self + } + + fn open_foreign(loop_: *mut Loop, fd: Fd, close_fd: bool) -> sys::Result { + let std_slot = fd.stdio_tag().map(|tag| &STD_MODES[tag as usize]); + let (handle, close_with) = if std_slot.is_some() { + let dup = sys::dup(fd)?; + (dup.native(), Some(dup)) + } else { + (fd.native(), close_fd.then_some(fd)) + }; + let mode = match std_slot.map(|slot| slot.load(Ordering::Acquire)) { + Some(STD_MODE_EVENT) => Mode::Event, + Some(STD_MODE_SYNC) => Mode::Sync, + _ => Mode::Unknown, + }; + let pipe = Self::create(loop_, handle, close_with, mode); + // SAFETY: `inner` is live while `pipe` is. + unsafe { + (*pipe.raw()).std_slot = std_slot; + if std_slot.is_some() { + (*pipe.raw()).flags.insert(Flags::DUPLICATED); + } + } + Ok(pipe) + } + + fn open_inherited_unshared(loop_: *mut Loop, fd: Fd, close_fd: bool) -> sys::Result { + if fd.stdio_tag().is_some() { + return Self::open_foreign(loop_, fd, close_fd); + } + let handle = fd.native(); + // SAFETY: `loop_` is the caller's live loop. + let port = unsafe { iocp::us_loop_iocp(loop_) }; + // Refused for a synchronous file object, and for one the parent gave a + // port already. The call takes a synchronous file object's lock, as + // `classify` does; nobody holds it, since nobody else does I/O on this + // end (the origin's contract) and this process has done none yet. + let mode = if bun_sys::windows::CreateIoCompletionPort(handle, port, 0, 0).is_ok() { + // Overlapped, then: it has no lock for this to wait on. + if let Err(errno) = set_byte_wait_mode(handle) { + return Err(sys::Error::from_code(errno, Tag::open).with_fd(fd)); + } + Mode::Owned + } else { + Mode::Unknown + }; + Ok(Self::create(loop_, handle, close_fd.then_some(fd), mode)) + } + + fn create(loop_: *mut Loop, handle: HANDLE, close_fd: Option, mode: Mode) -> Pipe { + let inner = bun_core::heap::into_raw(Box::new(Inner { + link: Link::new(loop_, Inner::shut), + handle, + close_fd, + mode, + flags: Flags::REFD, + port: None, + std_slot: None, + reader: None, + read_op: ptr::null_mut(), + read_size: DEFAULT_READ_SIZE, + sync_reader: None, + sync_handle: None, + sync_write: None, + sync_spare: None, + write_head: ptr::null_mut(), + write_tail: ptr::null_mut(), + event_write: ptr::null_mut(), + writes_in_flight: 0, + chunked_write: ptr::null_mut(), + spare_write: ptr::null_mut(), + flush_op: ptr::null_mut(), + write_lane: Lane::NONE, + adopted: Vec::new(), + lent_back: None, + blocking_event: ptr::null_mut(), + pending: 0, + pins: 0, + })); + // SAFETY: `inner` is at its final address; `link` is its first field. + unsafe { Link::insert(inner.cast()) }; + bun_core::scoped_log!(WinPipe, "open {:p} {:?}", handle, mode); + // SAFETY: `into_raw` never returns null. + Pipe { + inner: unsafe { NonNull::new_unchecked(inner) }, + } + } + + #[inline] + fn raw(&self) -> *mut Inner { + self.inner.as_ptr() + } +} + +/// Whether `handle` is a synchronous file object, putting the pipe end in +/// byte-read blocking mode on the way. +/// +/// Both calls take the lock of a synchronous file object, which is held for as +/// long as any thread of any process has I/O in flight on it (a parked read of +/// an inherited stdin): only a thread that may block that long calls this. +fn classify(handle: HANDLE) -> Kind { + let Some(synchronous) = bun_sys::windows::fs::is_synchronous(Fd::from_system(handle)) else { + return Kind::Unusable(E::EBADF); + }; + if let Err(errno) = set_byte_wait_mode(handle) { + return Kind::Unusable(errno); + } + if synchronous { + Kind::Synchronous + } else { + Kind::Overlapped + } +} + +/// `PIPE_READMODE_BYTE | PIPE_WAIT` for the pipe end behind `handle`, which is +/// what every read and write in this module assumes. Takes a synchronous file +/// object's lock, as [`classify`] does. +fn set_byte_wait_mode(handle: HANDLE) -> Result<(), E> { + let mut pipe_mode = win::PIPE_READMODE_BYTE | win::PIPE_WAIT; + // SAFETY: `pipe_mode` is a live local; the other parameters are optional. + let ok = unsafe { + win::SetNamedPipeHandleState(handle, &raw mut pipe_mode, ptr::null_mut(), ptr::null_mut()) + }; + if ok != 0 { + return Ok(()); + } + match win::last_error() { + // The handle lacks FILE_WRITE_ATTRIBUTES; that is fine as long as the + // pipe end is in that mode already. + Win32Error::ACCESS_DENIED => { + let mut state: u32 = 0; + // SAFETY: `state` is a live local; the rest is optional. + let ok = unsafe { + win::GetNamedPipeHandleStateW( + handle, + &raw mut state, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + 0, + ) + }; + if ok == 0 { + Err(E::EBADF) + } else if state & win::PIPE_NOWAIT != 0 { + Err(E::EACCES) + } else if state & win::PIPE_READMODE_MESSAGE != 0 { + // A zero-byte read of a message-mode end fails with MORE_DATA + // whenever a message is waiting, so waiting that way cannot + // work. + Err(E::EBADF) + } else { + Ok(()) + } + } + // FILE_TYPE_PIPE, yet not a pipe: a socket. + Win32Error::INVALID_PARAMETER => Err(E::ENOTSOCK), + _ => Err(E::EBADF), + } +} + +// ────────────────────────────────────────────────────────────────────────── +// Owner-facing API +// ────────────────────────────────────────────────────────────────────────── + +impl Pipe { + pub fn handle(&self) -> HANDLE { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { (*self.raw()).handle } + } + + /// The HANDLE as a system-kind `Fd`; `Fd::INVALID` once closed. + pub fn fd(&self) -> Fd { + let handle = self.handle(); + if handle == INVALID_HANDLE_VALUE { + return Fd::INVALID; + } + Fd::from_system(handle) + } + + /// `read_start` without a `read_stop`, EOF or error since. + pub fn is_reading(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Pipe` is. + let inner = unsafe { &*self.raw() }; + !inner.flags.contains(Flags::DETACHED) && inner.flags.contains(Flags::READING) + } + + /// Reading, or writes not yet completed. + pub fn is_active(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Pipe` is. + let inner = unsafe { &*self.raw() }; + !inner.flags.contains(Flags::DETACHED) + && (inner.flags.contains(Flags::READING) || inner.has_writes()) + } + + /// Let this pipe keep the loop alive while it reads (the default). + /// Unfinished writes keep it alive either way. + pub fn ref_(&self) { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { + (*self.raw()).flags.insert(Flags::REFD); + Inner::update_keep_alive(self.raw()); + } + } + + pub fn unref(&self) { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { + (*self.raw()).flags.remove(Flags::REFD); + Inner::update_keep_alive(self.raw()); + } + } + + /// Leave the caller's `Fd` open when the pipe closes. A duplicate made for + /// a standard handle is still released. + pub fn disown(&mut self) { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { + if !(*self.raw()).flags.contains(Flags::DUPLICATED) { + (*self.raw()).close_fd = None; + } + } + } + + /// The most the owner takes at a time (at least one byte). + pub fn set_read_size(&mut self, size: usize) { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { (*self.raw()).read_size = size.clamp(1, u32::MAX as usize) }; + } + + /// Deliver what the pipe produces to `on_read(ctx, ..)`, always from the + /// loop and never from inside this call. `ctx` must stay valid until + /// `read_stop`, the `Eof`/`Err` event, or the `Pipe` is closed or dropped. + pub fn read_start( + &mut self, + ctx: *mut T, + on_read: unsafe fn(*mut T, ReadEvent<'_>), + ) -> sys::Result<()> { + let this = self.raw(); + // SAFETY: `inner` is live while the owner's `Pipe` is; no reference + // into it is held across the calls below. + unsafe { + if (*this).flags.contains(Flags::DETACHED) { + return Err(sys::Error::from_code(E::EBADF, Tag::read)); + } + (*this).reader = Some(ReadCallback::new(ctx, on_read)); + if (*this).flags.contains(Flags::READING) { + return Ok(()); + } + (*this).flags.insert(Flags::READING); + (*this).flags.remove(Flags::READ_ENDED); + Inner::update_keep_alive(this); + Inner::pump_read(this).map_err(|err| { + (*this).flags.remove(Flags::READING); + Inner::update_keep_alive(this); + sys::Error::from_win32(err, Tag::read) + }) + } + } + + /// Stop delivering. What is out is a zero-byte wait: nothing has left the + /// pipe. On a HANDLE somebody else created (inherited stdin), which another + /// process may read next, it is taken back, which costs the peer's write + /// nothing. The read of a message-type pipe is left to finish, and what it + /// brings is kept for the next `read_start`. + pub fn read_stop(&mut self) { + // SAFETY: `inner` is live while the owner's `Pipe` is; `read_op` is + // owned by it while non-null. + unsafe { + let this = self.raw(); + (*this).flags.remove(Flags::READING); + let read = (*this).read_op; + if !read.is_null() && (*read).state == ReadState::InFlight { + if let Some(reader) = &(*this).sync_reader { + (*read).stopped = true; + let taken_back = reader.disarm(); + Inner::complete_taken_back(this, taken_back); + } else if (*this).mode == Mode::Event && (*read).zero_wait { + (*read).stopped = true; + win::CancelIoEx((*this).handle, (&raw mut (*read).op).cast()); + } + } + if (*this).flags.contains(Flags::PEER_GONE) && !(*this).gone() { + Inner::pump_writes(this, ptr::null_mut()); + } + Inner::update_keep_alive(this); + } + } + + /// Deliver what a HANDLE Bun created holds now, from inside this call: for + /// an owner that has heard elsewhere that it was written (its writer + /// exited, or said so on another pipe) and is not inside `on_read`. + pub fn drain(&mut self) { + let this = self.raw(); + // SAFETY: `inner` is live while the owner's `Pipe` is; `read_op` is + // owned by it while non-null, and the packet its wait is owed keeps + // both allocated whatever the owner does from `on_read`. + unsafe { + let op = (*this).read_op; + if (*this).gone() + || (*this).mode != Mode::Owned + || !(*this).flags.contains(Flags::READING) + || op.is_null() + || (*op).state != ReadState::InFlight + || !(*op).zero_wait + { + return; + } + let mut behind = match Inner::peek(this) { + Ok(behind @ 1..) => behind, + // What is wrong with a pipe that cannot be asked, its wait says. + Ok(0) | Err(_) => return, + }; + (*op).outcome = ReadOp::read_present(this, op, &mut behind); + let open = ReadOp::deliver(this, op, behind, ReadState::InFlight); + if (*op).zero_wait { + // Its packet carries on, or frees the read of a closed pipe. + (*op).state = ReadState::InFlight; + } else if open { + (*op).state = ReadState::Idle; + ReadOp::resume(this, op); + } else { + (*this).read_op = ptr::null_mut(); + ReadOp::destroy(op); + Inner::maybe_finish(this); + } + } + } + + /// Have the reads from here on land after `buffer`'s contents, in its + /// spare capacity, and their [`ReadEvent::Data`] carry the whole of it: a + /// reader that accumulates never copies a chunk. The allocation is the + /// read's while the kernel has it; [`take_read_buffer`](Self::take_read_buffer) + /// says how it comes back early. `Err(buffer)` when a read is out or its + /// result is waiting to be delivered. + pub fn lend_read_buffer(&mut self, buffer: Vec) -> Result<(), Vec> { + // SAFETY: `inner` is live while the owner's `Pipe` is; `read_op` is + // owned by it while non-null. + unsafe { + let this = self.raw(); + let op = (*this).read_op; + if (*this).gone() + || op.is_null() + || !matches!((*op).state, ReadState::Idle | ReadState::Delivering) + { + return Err(buffer); + } + debug_assert!(!(*op).kept_taken); + (*op).kept = buffer.len(); + (*op).buf = buffer; + Ok(()) + } + } + + /// What [`lend_read_buffer`](Self::lend_read_buffer) handed over, back. It + /// is copied out while a read has the allocation; bytes of a read not yet + /// delivered stay with the pipe. + pub fn take_read_buffer(&mut self) -> Vec { + // SAFETY: as `lend_read_buffer`. A read in flight only ever writes + // past `kept`, within capacity the loop thread reserved, and nothing + // but the loop thread touches the `Vec` itself. + unsafe { + let op = (*self.raw()).read_op; + if op.is_null() { + return Vec::new(); + } + match (*op).state { + ReadState::Idle | ReadState::Delivering => { + (*op).kept = 0; + core::mem::take(&mut (*op).buf) + } + ReadState::InFlight => { + // The read still lands after it; `outcome` drops the gap. + (*op).kept_taken = true; + (&(*op).buf)[..(*op).kept].to_vec() + } + ReadState::Held | ReadState::Replaying => { + let kept = core::mem::take(&mut (*op).kept); + let undelivered = (&(*op).buf)[kept..].to_vec(); + let mut lent = core::mem::replace(&mut (*op).buf, undelivered); + lent.truncate(kept); + lent + } + } + } + } + + /// Write `data`, then call `on_write(ctx, result)` from the loop — also + /// when the pipe is closed first (`ECANCELED`). `result` is the byte count, + /// which is `data.len()` unless the write failed. `Err` means `on_write` + /// will not run: the pipe is closed, or the kernel refused the write and + /// `refusal` asked to hear of that here. + /// + /// # Safety + /// `data` and `ctx` must stay valid until `on_write` runs. A `Pipe` that is + /// dropped (rather than [`close`](Self::close)d) never calls back; give it + /// the bytes first ([`adopt_write_buffer`](Self::adopt_write_buffer)). A + /// lender that cannot do that uses [`write_owned`](Self::write_owned) + /// where [`writes_outlive_cancel`](Self::writes_outlive_cancel) says so. + pub unsafe fn write( + &mut self, + data: &[u8], + refusal: Refusal, + ctx: *mut T, + on_write: unsafe fn(*mut T, WriteResult), + ) -> sys::Result<()> { + // SAFETY: caller contract. + unsafe { + Inner::write( + self.raw(), + data.as_ptr(), + data.len(), + Vec::new(), + false, + Some(Callback::new(ctx, on_write)), + refusal, + ) + } + } + + /// As [`write`](Self::write), for bytes the operation should own: nothing + /// needs to outlive the call. + pub fn write_owned( + &mut self, + data: Vec, + refusal: Refusal, + ctx: *mut T, + on_write: unsafe fn(*mut T, WriteResult), + ) -> sys::Result<()> { + // SAFETY: the operation owns `data`, whose heap buffer does not move. + unsafe { + Inner::write( + self.raw(), + data.as_ptr(), + data.len(), + data, + false, + Some(Callback::new(ctx, on_write)), + refusal, + ) + } + } + + /// Write `buffer[start..]`, which the operation owns for as long as it + /// runs, without copying it. The buffer comes back from + /// [`take_lent_buffer`](Self::take_lent_buffer) inside `on_write`, and in + /// `Err` when the write is refused here. For a pipe whose + /// [`writes_outlive_cancel`](Self::writes_outlive_cancel). + pub fn write_lent( + &mut self, + buffer: Vec, + start: usize, + ctx: *mut T, + on_write: unsafe fn(*mut T, WriteResult), + ) -> Result<(), (sys::Error, Vec)> { + let this = self.raw(); + debug_assert!(start <= buffer.len()); + // SAFETY: `inner` is live while the owner's `Pipe` is. The operation + // owns `buffer`, whose heap buffer does not move. + unsafe { + (*this).lent_back = None; + let result = Inner::write( + this, + buffer.as_ptr().add(start), + buffer.len() - start, + buffer, + true, + Some(Callback::new(ctx, on_write)), + Refusal::Returned, + ); + result.map_err(|err| (err, (*this).lent_back.take().unwrap_or_default())) + } + } + + /// The buffer of the lent write whose `on_write` is running: empty when a + /// helper thread that could not be stopped still has the bytes. + pub fn take_lent_buffer(&mut self) -> Option> { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { (*self.raw()).lent_back.take() } + } + + /// Whether a write can still be reading its bytes after the pipe was + /// dropped: a helper thread's blocking `WriteFile` cannot be recalled. + pub fn writes_outlive_cancel(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Pipe` is. + matches!(unsafe { (*self.raw()).mode }, Mode::Sync | Mode::Unknown) + } + + /// Write all of `data` before returning, blocking the calling thread for + /// as long as the reader takes. For a pipe whose owner writes no other + /// way: nothing orders this against a [`write`](Self::write) still out. + pub fn write_blocking(&mut self, data: &[u8]) -> sys::Result { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { Inner::write_blocking(self.raw(), data) } + } + + /// Whether the pipe was created as a message-type pipe, where a + /// zero-length write reaches the other end as a read of no bytes. On a + /// byte-type pipe such a write is dropped. + pub fn is_message_type(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { Inner::is_message_type(self.raw()) } + } + + /// Hear, once, when the other end has read everything written so far + /// (`Ok`), or that it closed first (`Err`). Only for a HANDLE Bun created: + /// the request would block the calling thread on any other, which gets + /// `ENOTSUP`. Closing or dropping the pipe cancels it without a call. + pub fn flush_peer( + &mut self, + ctx: *mut T, + on_flushed: unsafe fn(*mut T, sys::Result<()>), + ) -> sys::Result<()> { + let this = self.raw(); + // SAFETY: `inner` is live while the owner's `Pipe` is. The OVERLAPPED + // lives in the op, which is freed only from its own completion. + unsafe { + if (*this).gone() { + return Err(sys::Error::from_code(E::EBADF, Tag::fsync)); + } + if (*this).mode != Mode::Owned { + return Err(sys::Error::from_code(E::ENOTSUP, Tag::fsync)); + } + if !(*this).flush_op.is_null() { + return Err(sys::Error::from_code(E::EBUSY, Tag::fsync)); + } + let op = bun_core::heap::into_raw(Box::new(FlushOp { + op: Op::new(FlushOp::complete), + pipe: this, + callback: Callback::new(ctx, on_flushed), + })); + // The OVERLAPPED's first two fields are the IO_STATUS_BLOCK; its + // address is what the port hands back. + let overlapped = &raw mut (*op).op.overlapped; + let status = NtFsControlFile( + (*this).handle, + ptr::null_mut(), + ptr::null_mut(), + overlapped.cast(), + overlapped.cast(), + FSCTL_PIPE_FLUSH, + ptr::null_mut(), + 0, + ptr::null_mut(), + 0, + ); + // A call that fails outright queues no packet. + if status.0 >= 0xC000_0000 { + drop(bun_core::heap::take(op)); + return Err(sys::Error::from_win32( + Win32Error::from_ntstatus(status), + Tag::fsync, + )); + } + super::op_submitted((*this).link.loop_); + (*this).pending += 1; + (*this).flush_op = op; + Ok(()) + } + } + + /// Tell the other end of a message-type pipe that nothing more will be + /// written, with the pipe left open: a zero-length message, which a + /// reader sees as a read of no bytes. Readers that know the convention + /// (.NET streams, go-winio, WCF) take it as the end of the stream. It has + /// to follow a completed [`flush_peer`](Self::flush_peer): written behind + /// unread bytes, a byte-mode reader gets it merged into them. Nobody hears + /// how it went: the write may end after the pipe's owner is gone. + pub fn write_end_marker(&mut self) -> sys::Result<()> { + // SAFETY: a write of no bytes reads nothing through its pointer. + unsafe { + Inner::write( + self.raw(), + ptr::NonNull::::dangling().as_ptr(), + 0, + Vec::new(), + false, + None, + Refusal::Posted, + ) + } + } + + /// Whether the pipe holds bytes its owner has not been given. + pub fn has_unread(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { !(*self.raw()).gone() && Inner::peek(self.raw()).is_ok_and(|bytes| bytes > 0) } + } + + /// The process on the other end of a named pipe. + pub fn peer_pid(&self) -> Option { + let handle = self.handle(); + let mut pid: u32 = 0; + // SAFETY: `pid` is a live local. + unsafe { + if win::GetNamedPipeClientProcessId(handle, &raw mut pid) == 0 { + return None; + } + // A client end asking for "the client" gets itself. + if pid == bun_sys::windows::GetCurrentProcessId() + && win::GetNamedPipeServerProcessId(handle, &raw mut pid) == 0 + { + return None; + } + } + Some(pid) + } + + /// Close. Writes that had not completed still call back, with `ECANCELED` + /// (dropping the `Pipe` instead calls nobody back); reads report nothing. + pub fn close(self) { + let this = self.raw(); + core::mem::forget(self); + // SAFETY: `this` is live until `Inner::close` decides otherwise. + unsafe { Inner::close(this, true) }; + } + + /// `buffer` is kept until the pipe itself is freed: for an owner about to + /// go away while a write that borrows `buffer` may still be in flight. + pub fn adopt_write_buffer(&mut self, buffer: Vec) { + // SAFETY: `inner` is live while the owner's `Pipe` is. It is freed only + // after every operation has been collected, which is as long as any of + // them can look at `buffer`. + unsafe { (*self.raw()).adopted.push(buffer) }; + } +} + +impl Drop for Pipe { + fn drop(&mut self) { + // SAFETY: `inner` is live until `Inner::close` decides otherwise. + unsafe { Inner::close(self.raw(), false) }; + } +} + +// ────────────────────────────────────────────────────────────────────────── +// Inner +// ────────────────────────────────────────────────────────────────────────── + +fn read_error(err: Win32Error) -> sys::Error { + // A server that disconnected its instance, as opposed to closing it, left + // the conversation without an end: EPIPE, as libuv and Node report it. + if err == Win32Error::NO_DATA || err == Win32Error::PIPE_NOT_CONNECTED { + return sys::Error::from_code(E::EPIPE, Tag::read); + } + sys::Error::from_win32(err, Tag::read) +} + +/// A pipe whose reader is gone refuses a write with one of three codes, +/// depending on how it went. +fn is_peer_gone(err: Win32Error) -> bool { + err == Win32Error::BROKEN_PIPE + || err == Win32Error::NO_DATA + || err == Win32Error::PIPE_NOT_CONNECTED +} + +fn write_error(err: Win32Error) -> sys::Error { + if is_peer_gone(err) { + return sys::Error::from_code(E::EPIPE, Tag::write); + } + if err == Win32Error::OPERATION_ABORTED { + return sys::Error::from_code(E::ECANCELED, Tag::write); + } + sys::Error::from_win32(err, Tag::write) +} + +fn is_eof(err: Win32Error) -> bool { + err == Win32Error::BROKEN_PIPE || err == Win32Error::HANDLE_EOF +} + +/// How the operation behind a dequeued packet went: what `posted` says, else +/// what the kernel left in its OVERLAPPED. +fn completed(op: &Op, posted: Posted) -> (Win32Error, usize) { + posted.unwrap_or_else(|| (win::status_to_win32(op.status()), op.bytes_transferred())) +} + +impl Inner { + fn has_writes(&self) -> bool { + self.writes_in_flight > 0 || !self.write_head.is_null() + } + + /// # Safety + /// `this` is live. + unsafe fn is_message_type(this: *mut Inner) -> bool { + /// `FILE_PIPE_LOCAL_INFORMATION`, whose first field is the pipe's type. + const FILE_PIPE_LOCAL_INFORMATION_CLASS: win::FILE_INFORMATION_CLASS = + win::FILE_INFORMATION_CLASS(24); + /// One bit of it: `FILE_PIPE_REJECT_REMOTE_CLIENTS` is another. + const FILE_PIPE_MESSAGE_TYPE: u32 = 1; + // SAFETY: caller contract; the out-parameters are live locals of the + // class's size (ten `ULONG`s). + unsafe { + if !(*this).flags.contains(Flags::TYPE_KNOWN) { + let mut info = [0u32; 10]; + let mut iosb: win::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let status = win::NtQueryInformationFile( + (*this).handle, + &raw mut iosb, + info.as_mut_ptr().cast(), + size_of::<[u32; 10]>() as u32, + FILE_PIPE_LOCAL_INFORMATION_CLASS, + ); + (*this).flags.insert(Flags::TYPE_KNOWN); + if status == win::NTSTATUS::SUCCESS && info[0] & FILE_PIPE_MESSAGE_TYPE != 0 { + (*this).flags.insert(Flags::MESSAGE_TYPE); + } + } + (*this).flags.contains(Flags::MESSAGE_TYPE) + } + } + + #[inline] + fn gone(&self) -> bool { + self.flags.intersects(Flags::CLOSING | Flags::DETACHED) + } + + /// # Safety + /// `this` is live. Must run on the loop's thread. + unsafe fn update_keep_alive(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + let inner = &mut *this; + if inner.flags.contains(Flags::DETACHED) { + return; + } + let wanted = !inner.flags.contains(Flags::CLOSING) + && ((inner.flags.contains(Flags::REFD) && inner.flags.contains(Flags::READING)) + || inner.has_writes()); + if wanted == inner.flags.contains(Flags::KEEPING_ALIVE) { + return; + } + inner.flags.set(Flags::KEEPING_ALIVE, wanted); + if wanted { + (*inner.link.loop_).add_active(1); + } else { + (*inner.link.loop_).sub_active(1); + } + } + } + + /// # Safety + /// `this` is live and not detached. + unsafe fn port(this: *mut Inner) -> Option> { + // SAFETY: caller contract. + unsafe { + if (*this).port.is_none() { + (*this).port = super::port_for((*this).link.loop_); + } + (*this).port.clone() + } + } + + // ── reading ────────────────────────────────────────────────────────── + + /// Make progress towards the next `ReadEvent`. + /// + /// # Safety + /// `this` is live, not closing, and `READING` is set. + unsafe fn pump_read(this: *mut Inner) -> Result<(), Win32Error> { + // SAFETY: caller contract; `read_op` is owned by `this` while non-null + // and its `pipe` points back here. + unsafe { + if (*this).flags.contains(Flags::READ_ENDED) { + return Ok(()); + } + if (*this).read_op.is_null() { + (*this).read_op = bun_core::heap::into_raw(Box::new(ReadOp { + op: Op::new(ReadOp::complete), + pipe: this, + state: ReadState::Idle, + outcome: Outcome::None, + buf: Vec::new(), + kept: 0, + kept_taken: false, + dest: ptr::null_mut(), + posted: None, + verdict: None, + lane: Lane::NONE, + max_len: 0, + stopped: false, + zero_wait: false, + held: 0, + finisher: AtomicU8::new(READ_REQUESTED), + })); + } + let op = (*this).read_op; + match (*op).state { + ReadState::InFlight | ReadState::Replaying | ReadState::Delivering => Ok(()), + ReadState::Held => { + // The owner hears of it from the loop, not from inside + // `read_start`. + super::complete_from_loop((*this).link.loop_, &raw mut (*op).op); + (*this).pending += 1; + (*op).state = ReadState::Replaying; + Ok(()) + } + ReadState::Idle => Self::submit_read(this, op), + } + } + } + + /// The room to make for a read with `waiting` bytes known to be in the pipe. + /// + /// # Safety + /// `this` is live. + unsafe fn room(this: *mut Inner, waiting: usize) -> usize { + // SAFETY: caller contract. + waiting + .max(DEFAULT_READ_SIZE) + .min(unsafe { (*this).read_size }) + } + + /// The bytes in the pipe. + /// + /// # Safety + /// `this` is live and not closing. + unsafe fn peek(this: *mut Inner) -> Result { + // SAFETY: caller contract. + unsafe { bytes_in_pipe((*this).handle, Self::blocking_event(this)?) } + .map(|bytes| bytes as usize) + } + + /// The event of an operation this thread waits for itself. + /// + /// # Safety + /// `this` is live. + unsafe fn blocking_event(this: *mut Inner) -> Result { + // SAFETY: caller contract; plain Win32 call. + unsafe { + if (*this).blocking_event.is_null() { + let event = win::CreateEventW(ptr::null_mut(), 1, 0, ptr::null()); + if event.is_null() { + return Err(win::last_error()); + } + (*this).blocking_event = event; + } + Ok((*this).blocking_event) + } + } + + /// Read, to `dest`, at most `len` bytes of what is in an `Owned` pipe now: + /// none, and no error, if it is empty. It queues no packet. + /// + /// # Safety + /// `this` is live and not closing; `dest` has room for `len` bytes. + unsafe fn read_now(this: *mut Inner, dest: *mut u8, len: usize) -> (Win32Error, usize) { + // SAFETY: caller contract. The OVERLAPPED may live on this stack frame + // because the frame does not return before the operation completes. + unsafe { + let event = match Self::blocking_event(this) { + Ok(event) => event, + Err(err) => return (err, 0), + }; + let mut overlapped: win::OVERLAPPED = bun_core::ffi::zeroed(); + // Low bit set: no packet for this one, the event is all. + overlapped.hEvent = (event as usize | 1) as HANDLE; + let ok = win::ReadFile( + (*this).handle, + dest, + len as u32, + ptr::null_mut(), + (&raw mut overlapped).cast(), + ); + if ok == 0 { + if win::last_error() != Win32Error::IO_PENDING { + return (win::last_error(), 0); + } + // Empty. Bytes that arrive before the cancellation are read. + win::CancelIoEx((*this).handle, (&raw mut overlapped).cast()); + bun_sys::windows::kernel32::WaitForSingleObject(event, bun_sys::windows::INFINITE); + } + match win::status_to_win32(overlapped.Internal as i32) { + Win32Error::OPERATION_ABORTED => (Win32Error::SUCCESS, 0), + err => (err, overlapped.InternalHigh), + } + } + } + + /// One overlapped `ReadFile` of `len` bytes to `op.dest` on an `Owned` + /// HANDLE. `true`: a packet brings its completion. `false`: it failed at + /// once, and `op.posted` says how. + /// + /// # Safety + /// `this` is live in `Owned` mode, `op` is its read with nothing out, and + /// `op.dest` has room for `len` bytes. + unsafe fn port_read(this: *mut Inner, op: *mut ReadOp, len: u32) -> bool { + // SAFETY: caller contract. The buffer and OVERLAPPED handed to the + // kernel live in `op`, which is freed only from its own completion. + unsafe { + (*op).posted = None; + (*op).op.reset(ptr::null_mut()); + let ok = win::ReadFile( + (*this).handle, + (*op).dest, + len, + ptr::null_mut(), + (&raw mut (*op).op).cast(), + ); + if ok != 0 || win::last_error() == Win32Error::IO_PENDING { + // A read that finished at once still queues its packet. + super::op_submitted((*this).link.loop_); + return true; + } + (*op).posted = Some((win::last_error(), 0)); + false + } + } + + /// One overlapped `ReadFile` of `len` bytes to `op.dest` on an `Event` + /// HANDLE. `Ok(true)`: it is pending and its completion arrives as a + /// packet. `Ok(false)`: it is over already and `op.posted` says how. + /// + /// # Safety + /// `this` is live in `Event` mode, `op` is its read with nothing out, and + /// `op.dest` has room for `len` bytes. + unsafe fn event_read(this: *mut Inner, op: *mut ReadOp, len: u32) -> Result { + // SAFETY: caller contract. The buffer and OVERLAPPED handed to the + // kernel live in `op`, which is freed only from its own completion. + unsafe { + let loop_ = (*this).link.loop_; + (*op).lane.ensure(loop_)?; + (*op).posted = None; + // Low bit set: the completion queues no packet on whatever port + // the file object may be associated with. + (*op).op.reset(((*op).lane.event as usize | 1) as HANDLE); + let ok = win::ReadFile( + (*this).handle, + (*op).dest, + len, + ptr::null_mut(), + (&raw mut (*op).op).cast(), + ); + if ok == 0 && win::last_error() == Win32Error::IO_PENDING { + if iocp::us_iocp_wait_start((*op).lane.wait, (*op).lane.event, &raw mut (*op).op) + != 0 + { + (*op).posted = Some((*op).lane.take_back((*this).handle, &raw mut (*op).op)); + return Ok(false); + } + return Ok(true); + } + (*op).posted = Some(if ok == 0 { + (win::last_error(), 0) + } else { + // A call that returned TRUE filled the OVERLAPPED in. + (Win32Error::SUCCESS, (*op).op.bytes_transferred()) + }); + Ok(false) + } + } + + /// A request taken back from the reader thread ends as one the thread + /// answered as aborted would: from the loop, with the packet it was + /// already counted for. + /// + /// # Safety + /// `this` is live; `op` is null or its read, taken from the reader. + unsafe fn complete_taken_back(this: *mut Inner, op: *mut ReadOp) { + if op.is_null() { + return; + } + // SAFETY: caller contract; the thread never looked inside `op`. + unsafe { + (*op).zero_wait = false; + (*op).posted = Some((Win32Error::OPERATION_ABORTED, 0)); + // No longer the thread's: a close before the packet is dequeued + // leaves the read to its packet. + *(*op).finisher.get_mut() = READ_POSTED; + iocp::us_iocp_op_ready((*this).link.loop_, &raw mut (*op).op); + } + } + + /// The zero-byte wait of an overlapped HANDLE completed: read what is there. + /// `behind` is what an `Owned` pipe holds beyond that. Returns as + /// [`event_read`](Self::event_read). + /// + /// # Safety + /// `this` is live in `Owned` or `Event` mode; `op` is its read with nothing + /// out. + unsafe fn fetch( + this: *mut Inner, + op: *mut ReadOp, + behind: &mut usize, + ) -> Result { + // SAFETY: caller contract. + unsafe { + if (*this).mode == Mode::Owned { + // Nobody else reads it: what the wait was for is there, and a + // read that comes back short has emptied the pipe. + ReadOp::make_room(op, Self::room(this, 0)); + let len = (*op).max_len as usize; + let (err, bytes) = Self::read_now(this, (*op).dest, len); + if err == Win32Error::SUCCESS && bytes == 0 { + // `drain` has had it. + (*op).zero_wait = true; + return Ok(Self::port_read(this, op, 0)); + } + (*op).posted = Some((err, bytes)); + // What is wrong with a pipe that cannot be asked, its next wait says. + if bytes == len + && let Ok(more) = Self::peek(this) + { + *behind = more; + } + return Ok(false); + } + let available = match Self::peek(this) { + Ok(available) => available, + Err(err) => { + (*op).posted = Some((err, 0)); + return Ok(false); + } + }; + ReadOp::make_room(op, Self::room(this, available)); + let len = available.min((*op).max_len as usize); + // Nothing: another reader of the pipe got there first. Wait again. + (*op).zero_wait = len == 0; + Self::event_read(this, op, len as u32) + } + } + + /// # Safety + /// `this` and its idle `op` are live; `this` is not closing. + unsafe fn submit_read(this: *mut Inner, op: *mut ReadOp) -> Result<(), Win32Error> { + // SAFETY: caller contract. The buffer and OVERLAPPED handed to the + // kernel live in `op`, which is freed only from its own completion. + unsafe { + let loop_ = (*this).link.loop_; + ReadOp::make_room(op, Self::room(this, 0)); + (*op).outcome = Outcome::None; + (*op).posted = None; + + // Wait with a zero-byte read and take the data when it completes. + match (*this).mode { + Mode::Owned => { + (*op).zero_wait = !Self::is_message_type(this); + let len = if (*op).zero_wait { 0 } else { (*op).max_len }; + if !Self::port_read(this, op, len) { + super::complete_from_loop(loop_, &raw mut (*op).op); + } + } + Mode::Event => { + (*op).zero_wait = true; + if !Self::event_read(this, op, 0)? { + super::complete_from_loop(loop_, &raw mut (*op).op); + } + } + Mode::Sync | Mode::Unknown => { + if (*this).sync_reader.is_none() { + let Some(port) = Self::port(this) else { + return Err(win::last_error()); + }; + let classify = (*this).mode == Mode::Unknown; + (*this).sync_reader = + Some(SyncReader::start((*this).handle, port, classify)?); + } + // Nothing leaves the pipe until this loop has seen that + // data is there and still wants it: see `complete`. + (*op).zero_wait = true; + (*op).held = 0; + if let Some(reader) = &(*this).sync_reader { + reader.request(op); + } + super::op_submitted(loop_); + } + } + (*this).pending += 1; + (*op).state = ReadState::InFlight; + Ok(()) + } + } + + // ── writing ────────────────────────────────────────────────────────── + + /// `lent`: `owned` goes back to the caller through `lent_back` when the + /// write is over or refused. + /// + /// # Safety + /// `this` is live. `data..data+len` stays valid until the callback runs, + /// or points into `owned`. + unsafe fn write( + this: *mut Inner, + data: *const u8, + len: usize, + owned: Vec, + lent: bool, + callback: Option>, + refusal: Refusal, + ) -> sys::Result<()> { + // SAFETY: caller contract. + unsafe { + if (*this).gone() { + if lent { + (*this).lent_back = Some(owned); + } + return Err(sys::Error::from_code(E::EBADF, Tag::write)); + } + let mut op = core::mem::replace(&mut (*this).spare_write, ptr::null_mut()); + if op.is_null() { + op = bun_core::heap::into_raw(Box::new(WriteOp { + op: Op::new(WriteOp::complete), + pipe: this, + next: ptr::null_mut(), + data: ptr::null(), + len: 0, + done: 0, + chunk: 0, + owned: Vec::new(), + lent: false, + callback: None, + posted: None, + error: None, + verdict: None, + })); + } + (*op).data = data; + (*op).len = len; + (*op).done = 0; + (*op).owned = owned; + (*op).lent = lent; + (*op).callback = callback; + (*op).error = None; + if (*this).write_tail.is_null() { + (*this).write_head = op; + } else { + (*(*this).write_tail).next = op; + } + (*this).write_tail = op; + let fresh = match refusal { + Refusal::Returned => op, + Refusal::Posted => ptr::null_mut(), + }; + let refused = Self::pump_writes(this, fresh); + Self::update_keep_alive(this); + match refused { + Some(err) => Err(write_error(err)), + None => Ok(()), + } + } + } + + /// Hand queued writes to the kernel: all of them on a HANDLE Bun owns + /// (the kernel keeps them in order), one at a time otherwise. + /// + /// `fresh` is the write a [`Refusal::Returned`] caller queued just now, or + /// null. When the kernel refuses that one at once, it is dropped without + /// its callback and the refusal is returned: the caller of `write` hears + /// of it before it can close the pipe. Any other write that cannot start + /// reports through the port: its `write` call returned long ago, or asked + /// for that. + /// + /// A write refused because the other end is gone stays queued while the + /// owner reads: an owner closes the pipe on that failure, and what the + /// other end wrote before it left may be in a read whose packet the loop + /// has not come to yet. The end of the reading starts the write again. + /// + /// # Safety + /// `this` is live and not closing. + unsafe fn pump_writes(this: *mut Inner, fresh: *mut WriteOp) -> Option { + // SAFETY: caller contract; queued ops are owned by `this`. + unsafe { + loop { + let op = (*this).write_head; + if op.is_null() + || !(*this).chunked_write.is_null() + || ((*this).mode != Mode::Owned && (*this).writes_in_flight > 0) + || (*this).flags.contains(Flags::PEER_GONE | Flags::READING) + { + return None; + } + (*this).write_head = (*op).next; + if (*this).write_head.is_null() { + (*this).write_tail = ptr::null_mut(); + } + (*op).next = ptr::null_mut(); + (*this).writes_in_flight += 1; + (*this).pending += 1; + if (*op).len - (*op).done > MAX_WRITE_CHUNK { + (*this).chunked_write = op; + } + match Self::start_write(this, op) { + None => {} + Some((err, _)) + if is_peer_gone(err) + && (*this).mode == Mode::Owned + && (*this).flags.contains(Flags::READING) => + { + if (*this).chunked_write == op { + (*this).chunked_write = ptr::null_mut(); + } + (*this).writes_in_flight -= 1; + (*this).pending -= 1; + (*op).next = (*this).write_head; + (*this).write_head = op; + if (*this).write_tail.is_null() { + (*this).write_tail = op; + } + (*this).flags.insert(Flags::PEER_GONE); + } + Some((err, _)) if op == fresh && err != Win32Error::SUCCESS => { + if (*this).chunked_write == op { + (*this).chunked_write = ptr::null_mut(); + } + (*this).writes_in_flight -= 1; + (*this).pending -= 1; + (*this).lent_back = WriteOp::reclaim(op); + Self::recycle_write(this, op); + return Some(err); + } + Some(finished) => Self::post_finished_write(this, op, finished), + } + } + } + } + + /// `op` is over: drop what it carried and keep the allocation for the + /// next write, unless one is kept already or there will be none. + /// + /// # Safety + /// `this` is live; `op` is its write, referenced by nothing else. + unsafe fn recycle_write(this: *mut Inner, op: *mut WriteOp) { + // SAFETY: caller contract. + unsafe { + if (*this).gone() || !(*this).spare_write.is_null() { + drop(bun_core::heap::take(op)); + return; + } + (*op).owned = Vec::new(); + (*op).lent = false; + (*op).callback = None; + (*this).spare_write = op; + } + } + + /// A helper thread found out what this `Unknown` HANDLE is. + /// + /// # Safety + /// `this` is live. Must run on the loop's thread. + unsafe fn classified(this: *mut Inner, kind: Kind) { + // SAFETY: caller contract. + unsafe { + if (*this).mode != Mode::Unknown { + return; + } + let (mode, std_mode) = match kind { + Kind::Synchronous => (Mode::Sync, STD_MODE_SYNC), + Kind::Overlapped => (Mode::Event, STD_MODE_EVENT), + Kind::Unusable(_) => return, + }; + (*this).mode = mode; + if let Some(slot) = (*this).std_slot { + slot.store(std_mode, Ordering::Release); + } + bun_core::scoped_log!(WinPipe, "classified {:p} {:?}", (*this).handle, mode); + } + } + + /// Start (or continue) `op` and, when no packet of the kernel's will + /// announce how it went, queue one that does. + /// + /// # Safety + /// As [`start_write`](Self::start_write). + unsafe fn submit_write(this: *mut Inner, op: *mut WriteOp) { + // SAFETY: caller contract. + unsafe { + if let Some(finished) = Self::start_write(this, op) { + Self::post_finished_write(this, op, finished); + } + } + } + + /// # Safety + /// `this` and `op` are live; `op` is counted in `writes_in_flight` and + /// `pending`, and no packet for it is on its way. + unsafe fn post_finished_write( + this: *mut Inner, + op: *mut WriteOp, + finished: (Win32Error, usize), + ) { + // SAFETY: caller contract. + unsafe { + (*op).posted = Some(finished); + super::complete_from_loop((*this).link.loop_, &raw mut (*op).op); + } + } + + /// Hand `op`'s next chunk to the kernel. `None`: a packet will announce + /// how it went. Otherwise it is over already, with this result and count. + /// + /// # Safety + /// `this` and `op` are live; `op` is counted in `writes_in_flight` and + /// `pending`. + unsafe fn start_write(this: *mut Inner, op: *mut WriteOp) -> Option<(Win32Error, usize)> { + // SAFETY: caller contract. The OVERLAPPED lives in `op`, freed only + // from its own completion. + unsafe { + let loop_ = (*this).link.loop_; + let remaining = (*op).len - (*op).done; + let chunk = remaining.min(MAX_WRITE_CHUNK) as u32; + (*op).chunk = chunk; + (*op).posted = None; + (*op).op.reset(ptr::null_mut()); + let data = (*op).data.add((*op).done); + + // Every arm returns `None` once a packet is on its way. + let finished: (Win32Error, usize) = match (*this).mode { + Mode::Owned => { + let ok = win::WriteFile( + (*this).handle, + data, + chunk, + ptr::null_mut(), + (&raw mut (*op).op).cast(), + ); + if ok != 0 || win::last_error() == Win32Error::IO_PENDING { + super::op_submitted(loop_); + return None; + } + (win::last_error(), 0) + } + Mode::Event => match (*this).write_lane.ensure(loop_) { + Err(err) => (err, 0), + Ok(()) => { + let event = (*this).write_lane.event; + (*op).op.overlapped.hEvent = (event as usize | 1) as HANDLE; + let ok = win::WriteFile( + (*this).handle, + data, + chunk, + ptr::null_mut(), + (&raw mut (*op).op).cast(), + ); + if ok == 0 && win::last_error() == Win32Error::IO_PENDING { + if iocp::us_iocp_wait_start( + (*this).write_lane.wait, + event, + &raw mut (*op).op, + ) == 0 + { + (*this).event_write = op; + return None; + } + (*this) + .write_lane + .take_back((*this).handle, &raw mut (*op).op) + } else if ok == 0 { + (win::last_error(), 0) + } else { + // A call that returned TRUE filled the OVERLAPPED in. + (Win32Error::SUCCESS, (*op).op.bytes_transferred()) + } + } + }, + Mode::Sync | Mode::Unknown => match SyncWrite::start(this, op, chunk) { + Ok(()) => { + super::op_submitted(loop_); + return None; + } + Err(err) => (err, 0), + }, + }; + Some(finished) + } + } + + /// # Safety + /// `this` is live. + unsafe fn write_blocking(this: *mut Inner, data: &[u8]) -> sys::Result { + // SAFETY: caller contract. The OVERLAPPED may live on this stack frame + // because the frame does not return before the operation completes. + unsafe { + if (*this).gone() { + return Err(sys::Error::from_code(E::EBADF, Tag::write)); + } + debug_assert!(!(*this).has_writes()); + if (*this).mode == Mode::Unknown { + // This call may block for as long as the write itself would. + let kind = classify((*this).handle); + Self::classified(this, kind); + if let Kind::Unusable(errno) = kind { + return Err(sys::Error::from_code(errno, Tag::write)); + } + } + let event = Self::blocking_event(this).map_err(write_error)?; + let mut written = 0usize; + while written < data.len() { + let chunk = (data.len() - written).min(MAX_WRITE_CHUNK) as u32; + // Right for either kind of HANDLE, whichever this is: a + // synchronous one returns when the write is over, an + // overlapped one may pend and is waited for. + let mut overlapped: win::OVERLAPPED = bun_core::ffi::zeroed(); + // Low bit set: no packet for this one, the event is all. + overlapped.hEvent = (event as usize | 1) as HANDLE; + let ok = win::WriteFile( + (*this).handle, + data.as_ptr().add(written), + chunk, + ptr::null_mut(), + (&raw mut overlapped).cast(), + ); + if ok == 0 { + if win::last_error() != Win32Error::IO_PENDING { + return Err(write_error(win::last_error())); + } + bun_sys::windows::kernel32::WaitForSingleObject( + event, + bun_sys::windows::INFINITE, + ); + } + let status = win::status_to_win32(overlapped.Internal as i32); + if status != Win32Error::SUCCESS { + return Err(write_error(status)); + } + let n = overlapped.InternalHigh; + if n == 0 { + break; + } + written += n; + } + Ok(written) + } + } + + // ── closing ────────────────────────────────────────────────────────── + + /// The owner is done with the pipe. + /// + /// # Safety + /// `this` is live; the owner's `Pipe` is consumed or being dropped. + unsafe fn close(this: *mut Inner, report_writes: bool) { + // SAFETY: caller contract. + unsafe { + (*this).flags.insert(Flags::OWNER_GONE); + (*this).reader = None; + if !report_writes { + (*this).flags.insert(Flags::SILENT); + } + if !(*this).gone() { + Self::cancel_everything(this); + (*this).flags.insert(Flags::CLOSING); + (*this).flags.remove(Flags::READING); + if (*this).mode == Mode::Owned { + // A server-side instance holds the pipe's name until its + // HANDLE is closed, and a server may listen on that name + // again as soon as its connections report `close`. What + // was cancelled above still completes through the port. + Self::release_handle(this); + } + Self::update_keep_alive(this); + } + Self::maybe_finish(this); + } + } + + /// [`Link::shut`]: the loop is about to be freed. + unsafe fn shut(link: *mut Link) { + let this = link.cast::(); + // SAFETY: `link` is the first field of a listed (live) `Inner`. + unsafe { + if !(*this).gone() { + Self::cancel_everything(this); + (*this).flags.remove(Flags::READING); + } + (*this).flags.insert(Flags::CLOSING | Flags::SILENT); + (*this).reader = None; + Self::update_keep_alive(this); + (*this).flags.insert(Flags::DETACHED); + Link::remove(link); + Self::maybe_finish(this); + } + } + + /// Ask every in-flight operation to complete and fail every queued one. + /// + /// # Safety + /// `this` is live and neither closing nor detached. + unsafe fn cancel_everything(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + let loop_ = (*this).link.loop_; + let handle = (*this).handle; + + // Before an idle read is freed below: the thread answers the one it + // was given (as aborted) and touches no other. + if let Some(reader) = (*this).sync_reader.take() { + let taken_back = reader.stop(); + Inner::complete_taken_back(this, taken_back); + } + let read = (*this).read_op; + if !read.is_null() { + match (*read).state { + ReadState::InFlight => match (*this).mode { + Mode::Owned | Mode::Event => { + win::CancelIoEx(handle, (&raw mut (*read).op).cast()); + } + // The reader thread has it, and may be queued behind + // another reader of the pipe for as long as that one + // likes. Unless a packet for it is on its way already, + // the read is the thread's to free from here on. + Mode::Sync | Mode::Unknown + if (*read) + .finisher + .compare_exchange( + READ_REQUESTED, + READ_ORPHANED, + Ordering::SeqCst, + Ordering::SeqCst, + ) + .is_ok() => + { + (*this).read_op = ptr::null_mut(); + (*this).pending -= 1; + super::op_abandoned((*this).link.loop_); + } + Mode::Sync | Mode::Unknown => {} + }, + ReadState::Idle | ReadState::Held => { + (*this).read_op = ptr::null_mut(); + ReadOp::destroy(read); + } + // `drain` delivers with the wait still out. + ReadState::Delivering if (*read).zero_wait => { + win::CancelIoEx(handle, (&raw mut (*read).op).cast()); + } + ReadState::Replaying | ReadState::Delivering => {} + } + } + + if !(*this).flush_op.is_null() { + win::CancelIoEx(handle, (&raw mut (*(*this).flush_op).op).cast()); + } + if (*this).mode == Mode::Owned && (*this).writes_in_flight > 0 { + // Everything this process has out on a HANDLE it owns is this + // pipe's, and the read was dealt with above. + win::CancelIoEx(handle, ptr::null_mut()); + } else if !(*this).event_write.is_null() { + // Somebody else's HANDLE may carry other I/O from this process + // (stdout written to directly): cancel this write only. + win::CancelIoEx(handle, (&raw mut (*(*this).event_write).op).cast()); + } else if let Some(write) = (*this).sync_write.take() + && let Some(op) = write.cancel() + { + // The thread has the write and cannot be made to give it up. + // It ends here as cancelled, with the packet it was counted for. + (*op).posted = Some((Win32Error::OPERATION_ABORTED, 0)); + iocp::us_iocp_op_ready((*this).link.loop_, &raw mut (*op).op); + } + // Queued writes never reached the kernel; they complete as + // cancelled, in order, from the loop. + let mut op = core::mem::replace(&mut (*this).write_head, ptr::null_mut()); + (*this).write_tail = ptr::null_mut(); + while !op.is_null() { + let next = (*op).next; + (*op).next = ptr::null_mut(); + (*op).posted = Some((Win32Error::OPERATION_ABORTED, 0)); + (*this).writes_in_flight += 1; + (*this).pending += 1; + super::complete_from_loop(loop_, &raw mut (*op).op); + op = next; + } + } + } + + /// # Safety + /// `this` is live and no operation will use the HANDLE again. + unsafe fn release_handle(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + if let Some(fd) = (*this).close_fd.take() { + fd.close(); + } + (*this).handle = INVALID_HANDLE_VALUE; + } + } + + /// Release the HANDLE and the allocation once nothing can refer to them. + /// + /// # Safety + /// `this` is live. + unsafe fn maybe_finish(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + if !(*this).flags.contains(Flags::CLOSING) || (*this).pending > 0 || (*this).pins > 0 { + return; + } + let detached = (*this).flags.contains(Flags::DETACHED); + Self::release_handle(this); + if !(*this).flags.contains(Flags::OWNER_GONE) { + return; + } + if !detached { + Self::update_keep_alive(this); + Link::remove(this.cast()); + } + debug_assert!((*this).sync_reader.is_none()); + if !(*this).spare_write.is_null() { + drop(bun_core::heap::take((*this).spare_write)); + } + (*this).write_lane.release(); + if !(*this).blocking_event.is_null() { + win::CloseHandle((*this).blocking_event); + } + drop(bun_core::heap::take(this)); + } + } +} + +// ────────────────────────────────────────────────────────────────────────── +// Completions +// ────────────────────────────────────────────────────────────────────────── + +impl ReadOp { + /// # Safety + /// `op` is not referenced by the kernel, a helper thread or a queued packet. + unsafe fn destroy(op: *mut ReadOp) { + // SAFETY: caller contract. + unsafe { + (*op).lane.release(); + drop(bun_core::heap::take(op)); + } + } + + unsafe extern "C" fn complete(_loop: *mut Loop, op: *mut Op) { + let this = op.cast::(); + // SAFETY: `op` is the first field of the `ReadOp` this packet was + // submitted for; the packet is what kept it allocated. + unsafe { + let pipe = (*this).pipe; + (*pipe).pending -= 1; + if (*this).state == ReadState::Delivering { + // The wait `drain` delivers under, heard of from a loop that + // the owner's callback runs. `drain` goes on from here. + (*this).zero_wait = false; + return; + } + if (*pipe).gone() { + (*pipe).read_op = ptr::null_mut(); + Self::destroy(this); + Inner::maybe_finish(pipe); + return; + } + + let mut behind = 0; + let verdict = (*this).verdict.take(); + if let Some(kind) = verdict { + Inner::classified(pipe, kind); + } + if let Some(kind) = verdict + && kind != Kind::Synchronous + { + // The thread did no I/O for this request and has left. + if let Some(reader) = (*pipe).sync_reader.take() { + let taken_back = reader.stop(); + debug_assert!(taken_back.is_null()); + } + (*this).zero_wait = false; + (*this).stopped = false; + (*this).posted = None; + Self::drop_taken(this); + match kind { + Kind::Unusable(errno) => { + (*this).outcome = Outcome::Err(sys::Error::from_code(errno, Tag::read)); + } + _ => { + (*this).state = ReadState::Idle; + Self::resume(pipe, this); + return; + } + } + } else if (*this).state == ReadState::InFlight { + if core::mem::take(&mut (*this).zero_wait) + && Self::result(this).0 == Win32Error::SUCCESS + { + if !(*pipe).flags.contains(Flags::READING) { + // Stopped since: what arrived stays in the pipe. + (*this).stopped = false; + Self::drop_taken(this); + (*this).state = ReadState::Idle; + return; + } + if (*pipe).sync_reader.is_some() { + // Its thread found data and took none of it. It is asked + // for the bytes from here, so a loop that is blocked (in + // a synchronous spawn that shares the HANDLE, say) + // leaves them in the pipe. + if Self::take_and_deliver(pipe, this) + && (*pipe).flags.contains(Flags::PEER_GONE) + { + Inner::pump_writes(pipe, ptr::null_mut()); + } + return; + } + match Inner::fetch(pipe, this, &mut behind) { + Ok(true) => { + (*pipe).pending += 1; + return; + } + Ok(false) => {} + Err(err) => (*this).posted = Some((err, 0)), + } + } + let aborted = Self::result(this).0 == Win32Error::OPERATION_ABORTED; + (*this).outcome = Self::outcome(this); + if core::mem::take(&mut (*this).stopped) && aborted { + // `read_stop` interrupted the wait: nothing was read. + (*this).outcome = Outcome::None; + if !(*pipe).flags.contains(Flags::READING) { + (*this).state = ReadState::Idle; + return; + } + } + } + if !(*pipe).flags.contains(Flags::READING) { + (*this).state = ReadState::Held; + return; + } + + if !Self::deliver(pipe, this, behind, ReadState::Idle) { + (*pipe).read_op = ptr::null_mut(); + Self::destroy(this); + Inner::maybe_finish(pipe); + return; + } + Self::resume(pipe, this); + if (*pipe).flags.contains(Flags::PEER_GONE) { + Inner::pump_writes(pipe, ptr::null_mut()); + } + } + } + + /// A `Sync` pipe is readable and its owner reads. Each chunk is handed over + /// as soon as the reader thread has taken it, with the loop thread waiting + /// meanwhile: nothing of the owner's runs while bytes are out of the pipe + /// and not with it yet, so an owner that stops reading, whenever it does, + /// leaves the rest for whoever reads the HANDLE next. `false`: the owner + /// closed the pipe. + /// + /// # Safety + /// As [`deliver`](Self::deliver); `pipe` has its reader thread, which has no + /// request. + unsafe fn take_and_deliver(pipe: *mut Inner, this: *mut ReadOp) -> bool { + // SAFETY: caller contract. + unsafe { + let loop_ = (*pipe).link.loop_; + for _ in 0..TAKES_PER_TICK { + // A writer that keeps ahead has put more there by the next time. + Self::make_room(this, Inner::room(pipe, (*this).held * 2)); + (*this).state = ReadState::InFlight; + let Some(reader) = &(*pipe).sync_reader else { + break; + }; + if !reader.take(this) { + super::op_submitted(loop_); + (*pipe).pending += 1; + return true; + } + (*this).outcome = Self::outcome(this); + if matches!((*this).outcome, Outcome::Data) && (*this).buf.len() == (*this).kept { + // Another reader of the pipe got there first. + (*this).outcome = Outcome::None; + (*this).state = ReadState::Idle; + Self::resume(pipe, this); + return true; + } + if !Self::deliver(pipe, this, 0, ReadState::Idle) { + (*pipe).read_op = ptr::null_mut(); + Self::destroy(this); + Inner::maybe_finish(pipe); + return false; + } + if !(*pipe).flags.contains(Flags::READING) || (*this).state != ReadState::Idle { + return true; + } + } + // The rest of the loop has its turn. What is in the pipe stays there + // until this comes round again, as for a pipe just found readable. + (*this).zero_wait = true; + (*this).posted = Some((Win32Error::SUCCESS, 0)); + *(*this).finisher.get_mut() = READ_POSTED; + (*this).state = ReadState::InFlight; + (*pipe).pending += 1; + super::complete_from_loop(loop_, &raw mut (*this).op); + true + } + } + + /// Hand `this.outcome` to the owner, and after it the `behind` bytes an + /// `Owned` pipe held beyond it: whoever wrote them may have gone on to say + /// so elsewhere (it exited, it wrote to another pipe), which the loop + /// comes to next. `this` is left in `state`. `false`: the owner closed the + /// pipe meanwhile. + /// + /// # Safety + /// `this` is `pipe`'s read, whose `buf` no operation has; `pipe` is live, + /// not closing, and has a reader. + unsafe fn deliver( + pipe: *mut Inner, + this: *mut ReadOp, + mut behind: usize, + state: ReadState, + ) -> bool { + // SAFETY: caller contract. + unsafe { + loop { + let reader = (*pipe).reader.expect("READING"); + (*this).state = ReadState::Delivering; + (*pipe).pins += 1; + match core::mem::replace(&mut (*this).outcome, Outcome::None) { + Outcome::None => {} + Outcome::Data if (*this).buf.len() == (*this).kept => {} + Outcome::Data => { + // Before the call, which may lend the next one. + (*this).kept = 0; + reader.invoke(ReadEvent::Data(&mut (*this).buf)); + } + Outcome::Eof => { + (*pipe).flags.remove(Flags::READING); + (*pipe).flags.insert(Flags::READ_ENDED); + reader.invoke(ReadEvent::Eof); + } + Outcome::EndOfWrite => { + (*pipe).flags.remove(Flags::READING); + (*pipe).flags.insert(Flags::READ_ENDED); + reader.invoke(ReadEvent::EndOfWrite); + } + Outcome::Err(err) => { + (*pipe).flags.remove(Flags::READING); + (*pipe).flags.insert(Flags::READ_ENDED); + reader.invoke(ReadEvent::Err(err)); + } + } + (*pipe).pins -= 1; + if (*pipe).gone() { + return false; + } + (*this).state = state; + Inner::update_keep_alive(pipe); + if behind == 0 || !(*pipe).flags.contains(Flags::READING) { + return true; + } + (*this).outcome = Self::read_present(pipe, this, &mut behind); + } + } + } + + /// Room in `buf`, behind what was lent, for a read of `want` bytes. + /// + /// # Safety + /// `this` is live and no operation has its `buf`. + unsafe fn make_room(this: *mut ReadOp, want: usize) { + // SAFETY: caller contract. + unsafe { + let buf = &mut (*this).buf; + buf.truncate((*this).kept); + if buf.capacity() - buf.len() < want { + if buf.is_empty() { + buf.reserve_exact(want); + } else { + buf.reserve(want); + } + } + (*this).dest = buf.as_mut_ptr().add(buf.len()); + (*this).max_len = want as u32; + } + } + + /// Read as much of the `behind` bytes that are in an `Owned` pipe as the + /// owner takes at a time, and take them off `behind`. + /// + /// # Safety + /// As [`deliver`](Self::deliver). + unsafe fn read_present(pipe: *mut Inner, this: *mut ReadOp, behind: &mut usize) -> Outcome { + // SAFETY: caller contract. + unsafe { + let len = (*behind).min((*pipe).read_size); + Self::make_room(this, Inner::room(pipe, len)); + *behind -= len; + (*this).posted = Some(Inner::read_now(pipe, (*this).dest, len)); + Self::outcome(this) + } + } + + /// Read on, if the owner still wants to. + /// + /// # Safety + /// `this` is `pipe`'s idle read; `pipe` is live and not closing. + unsafe fn resume(pipe: *mut Inner, this: *mut ReadOp) { + // SAFETY: caller contract. + unsafe { + if (*pipe).flags.contains(Flags::READING) + && let Err(err) = Inner::pump_read(pipe) + { + (*this).outcome = Outcome::Err(sys::Error::from_win32(err, Tag::read)); + (*this).state = ReadState::Held; + let _ = Inner::pump_read(pipe); + } + } + } + + /// Let go of the lent bytes that [`Pipe::take_read_buffer`] copied out + /// from under the read: every way out of `InFlight` that keeps the buffer + /// comes through here. + /// + /// # Safety + /// `this` just completed: only the loop thread refers to `buf`. + unsafe fn drop_taken(this: *mut ReadOp) { + // SAFETY: caller contract. + unsafe { + if core::mem::take(&mut (*this).kept_taken) { + let kept = core::mem::take(&mut (*this).kept); + (*this).buf.drain(..kept); + } + } + } + + /// How the operation whose packet was just dequeued went. + /// + /// # Safety + /// `this` just completed. + unsafe fn result(this: *mut ReadOp) -> (Win32Error, usize) { + // SAFETY: caller contract. + unsafe { completed(&(*this).op, (*this).posted) } + } + + /// # Safety + /// `this` just completed; `dest` holds what the kernel wrote. + unsafe fn outcome(this: *mut ReadOp) -> Outcome { + // SAFETY: caller contract. The kernel initialized `bytes` bytes of the + // buffer's spare capacity, which `submit_read` reserved. + unsafe { + let (err, bytes) = Self::result(this); + (*this).posted = None; + let buf = &mut (*this).buf; + if err == Win32Error::SUCCESS { + debug_assert!(bytes <= buf.capacity() - buf.len()); + buf.set_len(buf.len() + bytes.min(buf.capacity() - buf.len())); + } + Self::drop_taken(this); + if err == Win32Error::SUCCESS { + // A read that asked for bytes and completed with none took a + // zero-length message, which only a message-type pipe delivers. + let pipe = (*this).pipe; + if bytes == 0 && (*pipe).mode == Mode::Owned && Inner::is_message_type(pipe) { + return Outcome::EndOfWrite; + } + return Outcome::Data; + } + if is_eof(err) { + return Outcome::Eof; + } + Outcome::Err(read_error(err)) + } + } +} + +// ────────────────────────────────────────────────────────────────────────── +// The reader thread of a synchronous HANDLE +// ────────────────────────────────────────────────────────────────────────── + +/// What the loop and the reader thread of a `Sync` pipe share. +struct SyncShared { + /// The thread's own duplicate of the pipe HANDLE, so the pipe's can be + /// closed without waiting for the thread to notice `shutdown`. + handle: HANDLE, + port: Arc, + /// Auto-reset; set after a change the thread may be parked on. + wake: HANDLE, + /// Auto-reset; set when a `READ_AWAITED` request has its answer. + answered: HANDLE, + lock: bun_threading::Mutex, + /// The thread is inside its cancellable zero-byte read. + waiting: AtomicBool, + /// The owner wants data: the thread may wait for it and take it. + armed: AtomicBool, + shutdown: AtomicBool, + /// The read the loop is waiting for. The loop stores one only while this + /// is null. Whoever swaps it out has it: the thread, to answer it, or the + /// loop, to take it back unanswered. The thread looks inside only once it + /// has taken it. + request: AtomicPtr, + /// What `request` asks for: to hear that the pipe is readable (nothing is + /// taken, and the request's memory is not needed), or the bytes. + zero_wait: AtomicBool, + /// The pipe is `Unknown`: [`classify`] `handle` before anything else. + classify: bool, + /// The thread's: what `classify` found, until an answer has carried it. + verdict: Cell>, +} + +// SAFETY: the HANDLEs are used from the thread that the field docs name, the +// rest is atomics, and a `ReadOp` behind `request` is the thread's alone. +unsafe impl Send for SyncShared {} +// SAFETY: as above. +unsafe impl Sync for SyncShared {} + +impl Drop for SyncShared { + fn drop(&mut self) { + // SAFETY: both were created in `SyncReader::start` and nothing uses + // them once the loop's and the thread's references are gone. + unsafe { + win::CloseHandle(self.handle); + win::CloseHandle(self.wake); + win::CloseHandle(self.answered); + } + } +} + +/// The loop's side of the one thread that reads a `Sync` pipe for as long as +/// the pipe is open. +/// +/// A request is answered in two steps. The thread waits in a zero-byte read, +/// which consumes nothing and can be interrupted without the peer losing +/// anything, and reports that the pipe is readable. The loop asks again, and +/// the thread takes what the pipe says it holds. The bytes leave the pipe only +/// after the loop thread has asked for them with them waiting, as with a +/// readiness poll: while it is blocked, or once its owner has stopped reading, +/// they stay for whoever else reads the HANDLE. `disarm` stops anything more +/// being taken. +/// +/// The loop thread waits for that second answer ([`take`](Self::take)) and hands +/// the bytes over at once, and the thread takes bytes only while it waits +/// (`READ_TAKING`), so its owner cannot stop reading with bytes out of the pipe +/// that it has not been given. It does not wait long for the thread to start: one +/// that comes to the request later takes nothing and says again, with a packet, +/// that the pipe is readable. +/// +/// For an `Unknown` pipe the thread classifies the HANDLE first and its first +/// answer carries the verdict. Unless that is `Synchronous`, the answer is all +/// it does. +struct SyncReader { + shared: Arc, + /// For `CancelSynchronousIo`. + thread: HANDLE, +} + +impl SyncReader { + fn start(pipe: HANDLE, port: Arc, classify: bool) -> Result { + use std::os::windows::io::IntoRawHandle; + let handle = win::duplicate(pipe)?; + // SAFETY: plain Win32 calls. + let (wake, answered) = unsafe { + let wake = win::CreateEventW(ptr::null_mut(), 0, 0, ptr::null()); + let answered = win::CreateEventW(ptr::null_mut(), 0, 0, ptr::null()); + if wake.is_null() || answered.is_null() { + let err = win::last_error(); + for made in [handle, wake, answered] { + if !made.is_null() { + win::CloseHandle(made); + } + } + return Err(err); + } + (wake, answered) + }; + let shared = Arc::new(SyncShared { + handle, + port, + wake, + answered, + lock: bun_threading::Mutex::new(), + waiting: AtomicBool::new(false), + armed: AtomicBool::new(false), + shutdown: AtomicBool::new(false), + request: AtomicPtr::new(ptr::null_mut()), + zero_wait: AtomicBool::new(false), + classify, + verdict: Cell::new(None), + }); + let for_thread = shared.clone(); + match std::thread::Builder::new() + .stack_size(256 * 1024) + .spawn(move || for_thread.run()) + { + Ok(join) => Ok(SyncReader { + shared, + thread: join.into_raw_handle().cast(), + }), + Err(err) => Err(err + .raw_os_error() + .map_or(Win32Error::NOT_ENOUGH_MEMORY, |code| { + Win32Error::from_u32(code as u32) + })), + } + } + + /// # Safety + /// `op` stays allocated until the packet the thread posts for it is + /// dequeued, and no request is outstanding. + unsafe fn request(&self, op: *mut ReadOp) { + // SAFETY: caller contract. + unsafe { self.request_as(op, READ_REQUESTED) }; + } + + /// Ask for the bytes and wait until the thread has taken them. `true`: `op` + /// is the loop's again, with its answer, which is no bytes if the pipe was + /// empty. `false`: the thread is held up. It takes nothing, and answers with + /// a packet. + /// + /// # Safety + /// As [`request`](Self::request). + unsafe fn take(&self, op: *mut ReadOp) -> bool { + use bun_sys::windows::{INFINITE, WAIT_OBJECT_0, kernel32::WaitForSingleObject}; + // SAFETY: caller contract. The thread frees `op` only once it is + // orphaned, which this thread does not get to do from in here. + unsafe { + (*op).zero_wait = false; + self.request_as(op, READ_AWAITED); + if WaitForSingleObject(self.shared.answered, TAKE_WAIT_MS) == WAIT_OBJECT_0 { + return true; + } + if (*op) + .finisher + .compare_exchange( + READ_AWAITED, + READ_REQUESTED, + Ordering::SeqCst, + Ordering::SeqCst, + ) + .is_ok() + { + return false; + } + // Answered as the wait ran out, or about to be: the thread is in a + // read of bytes that are there. + WaitForSingleObject(self.shared.answered, INFINITE); + true + } + } + + /// # Safety + /// As [`request`](Self::request). + unsafe fn request_as(&self, op: *mut ReadOp, finisher: u8) { + debug_assert!(self.shared.request.load(Ordering::Relaxed).is_null()); + // SAFETY: caller contract; `op` is the loop's until it is stored. + let zero_wait = unsafe { + *(*op).finisher.get_mut() = finisher; + (*op).zero_wait + }; + self.shared.zero_wait.store(zero_wait, Ordering::Release); + self.shared.armed.store(true, Ordering::Release); + self.shared.request.store(op, Ordering::Release); + // SAFETY: `wake` is open while `shared` is. + unsafe { win::SetEvent(self.shared.wake) }; + } + + /// Nothing more leaves the pipe until the next `request`. Returns the + /// request when the thread had not taken it: it is the caller's again, + /// unanswered. Null: none was out, or the thread has it and answers it + /// with what it read. + /// + /// Never waits for the thread. A synchronous HANDLE serves one call at a + /// time, whichever process makes it, and a call queued behind another + /// cannot be cancelled: there is nothing to wait for that is sure to come. + #[must_use] + fn disarm(&self) -> *mut ReadOp { + self.shared.lock.lock(); + self.shared.armed.store(false, Ordering::Release); + let op = self.shared.request.swap(ptr::null_mut(), Ordering::AcqRel); + // The thread leaves `waiting` only through this lock, so the cancel + // lands on its zero-byte read or on nothing. It lands on nothing when + // the thread is not inside the call yet, or is queued behind another + // reader of the pipe: the thread then stays in a read that takes + // nothing, and looks at `armed` and `shutdown` when it returns. + if self.shared.waiting.load(Ordering::Acquire) { + // SAFETY: `thread` is this struct's open thread HANDLE. + unsafe { win::CancelSynchronousIo(self.thread) }; + } + self.shared.lock.unlock(); + op + } + + /// As `disarm`, and the thread exits. + #[must_use] + fn stop(self) -> *mut ReadOp { + self.shared.shutdown.store(true, Ordering::Release); + let op = self.disarm(); + // SAFETY: both HANDLEs are open; `thread` is this struct's to close. + unsafe { + win::SetEvent(self.shared.wake); + win::CloseHandle(self.thread); + } + op + } +} + +/// What [`SyncShared::read`] did. +enum Taken { + /// How it went, how many bytes were taken and how many the pipe held. + Answer(Win32Error, u32, u32), + /// The pipe is empty, or the owner stopped. + Nothing, + /// There are bytes, and the loop thread no longer waits for them: whether + /// its owner still wants them is for the loop to say. + NotAwaited, +} + +impl SyncShared { + fn run(&self) { + if self.classify { + let kind = classify(self.handle); + self.verdict.set(Some(kind)); + if kind != Kind::Synchronous { + // The verdict goes with an answer. A request that is seen here + // can be taken back (`disarm`) before it is taken. + while !self.abort_request() && !self.shutdown.load(Ordering::Acquire) { + self.park(); + } + return; + } + } + while !self.shutdown.load(Ordering::Acquire) { + let op = self.request.load(Ordering::Acquire); + if !self.armed.load(Ordering::Acquire) { + if op.is_null() { + self.park(); + } else { + self.abort_request(); + } + continue; + } + if op.is_null() { + self.park(); + continue; + } + if self.zero_wait.load(Ordering::Acquire) { + match self.wait_for_data() { + Some(Win32Error::SUCCESS) => self.announce(), + Some(err) => { + self.finish_request(err); + } + None => {} + } + continue; + } + // The bytes land in the request's buffer: it is taken first, and + // the loop cannot take it back from here on. + let op = self.request.swap(ptr::null_mut(), Ordering::AcqRel); + if op.is_null() { + continue; + } + // SAFETY: `op` was taken from `request`: it is this thread's until + // it is posted or stored again. + unsafe { + match self.read(op) { + Taken::Answer(err, bytes, held) => { + (*op).zero_wait = false; + (*op).held = held as usize; + self.answer(op, err, bytes); + } + // Another reader of the pipe took it. + Taken::Nothing if (*op).finisher.load(Ordering::SeqCst) == READ_AWAITED => { + (*op).held = 0; + self.answer(op, Win32Error::SUCCESS, 0); + } + Taken::Nothing => self.wait_again(op), + Taken::NotAwaited => { + (*op).zero_wait = true; + self.answer(op, Win32Error::SUCCESS, 0); + } + } + } + } + self.abort_request(); + } + + /// Turn the data request `op`, which found the pipe empty, back into a + /// wait for it to be readable; or answer it as aborted if the loop has + /// stopped reading since. + /// + /// # Safety + /// `op` was taken from `request`. + unsafe fn wait_again(&self, op: *mut ReadOp) { + // Under `lock`, as `SyncReader::take_back` is: either that finds the + // request stored again, or this finds `armed` cleared. + self.lock.lock(); + let go = self.armed.load(Ordering::Acquire) && !self.shutdown.load(Ordering::Acquire); + if go { + // SAFETY: caller contract. + unsafe { (*op).zero_wait = true }; + self.zero_wait.store(true, Ordering::Release); + self.request.store(op, Ordering::Release); + } + self.lock.unlock(); + if !go { + // SAFETY: caller contract. + unsafe { + (*op).zero_wait = false; + self.answer(op, Win32Error::OPERATION_ABORTED, 0); + } + } + } + + fn park(&self) { + bun_sys::windows::kernel32::WaitForSingleObject(self.wake, bun_sys::windows::INFINITE); + } + + /// # Safety + /// `op` was taken from `request`; it is not touched after this. + unsafe fn answer(&self, op: *mut ReadOp, err: Win32Error, bytes: u32) { + // SAFETY: caller contract. The thread took `op` from `request`, so it + // is the thread's until this decides: the loop reads it once it + // dequeues the packet, or the pipe closed meanwhile and left it here. + unsafe { + (*op).verdict = self.verdict.take(); + (*op).posted = Some((err, bytes as usize)); + let mut state = (*op).finisher.load(Ordering::SeqCst); + loop { + if state == READ_ORPHANED { + // No lane: that is for an `Event` pipe, which has no thread. + drop(bun_core::heap::take(op)); + return; + } + match (*op).finisher.compare_exchange( + state, + READ_POSTED, + Ordering::SeqCst, + Ordering::SeqCst, + ) { + Ok(READ_AWAITED | READ_TAKING) => { + win::SetEvent(self.answered); + return; + } + Ok(_) => return self.port.post(&raw mut (*op).op), + // The loop thread stopped waiting, or closed the pipe. + Err(now) => state = now, + } + } + } + } + + /// The pipe is readable; nothing was taken. + fn announce(&self) { + let op = self.request.swap(ptr::null_mut(), Ordering::AcqRel); + if !op.is_null() { + // SAFETY: `op` was taken from `request`; `zero_wait` stays set. + unsafe { self.answer(op, Win32Error::SUCCESS, 0) }; + } + } + + fn abort_request(&self) -> bool { + self.finish_request(Win32Error::OPERATION_ABORTED) + } + + /// The request is over, with `err`. `false`: there was none. + fn finish_request(&self, err: Win32Error) -> bool { + let op = self.request.swap(ptr::null_mut(), Ordering::AcqRel); + if op.is_null() { + return false; + } + // SAFETY: `op` was taken from `request`. + unsafe { + (*op).zero_wait = false; + self.answer(op, err, 0); + } + true + } + + /// Take what `op` asks for, without waiting for any to come. + /// + /// # Safety + /// `op` was taken from `request`. + unsafe fn read(&self, op: *mut ReadOp) -> Taken { + if !self.armed.load(Ordering::Acquire) { + return Taken::Nothing; + } + // SAFETY: `handle` is open while `self` is, and synchronous. + let available = match unsafe { bytes_in_pipe(self.handle, ptr::null_mut()) } { + Ok(available) => available, + Err(err) => return Taken::Answer(err, 0, 0), + }; + if available == 0 { + return Taken::Nothing; + } + let mut n: u32 = 0; + // SAFETY: caller contract; `handle` is open while `self` is, and `dest` + // has room for `max_len` bytes. + let ok = unsafe { + // After the peek, which can be queued behind another process's read + // for as long as that one likes. + if (*op) + .finisher + .compare_exchange( + READ_AWAITED, + READ_TAKING, + Ordering::SeqCst, + Ordering::SeqCst, + ) + .is_err() + { + return Taken::NotAwaited; + } + win::ReadFile( + self.handle, + (*op).dest, + available.min((*op).max_len), + &raw mut n, + ptr::null_mut(), + ) + }; + if ok == 0 { + return Taken::Answer(win::last_error(), 0, 0); + } + Taken::Answer(Win32Error::SUCCESS, n, available) + } + + /// The cancellable zero-byte read. `None`: interrupted, or not to start. + fn wait_for_data(&self) -> Option { + self.lock.lock(); + let go = self.armed.load(Ordering::Acquire) && !self.shutdown.load(Ordering::Acquire); + if go { + self.waiting.store(true, Ordering::Release); + } + self.lock.unlock(); + if !go { + return None; + } + let mut n: u32 = 0; + let mut probe = [0u8; 1]; + // SAFETY: `handle` is open while `self` is; a zero-byte read writes nothing. + let waited = unsafe { + if win::ReadFile( + self.handle, + probe.as_mut_ptr(), + 0, + &raw mut n, + ptr::null_mut(), + ) == 0 + { + win::last_error() + } else { + Win32Error::SUCCESS + } + }; + // See `SyncReader::take_back`: a cancel that is being issued has + // returned before any other call of this thread's can take it. + self.waiting.store(false, Ordering::Release); + self.lock.lock(); + self.lock.unlock(); + + (waited != Win32Error::OPERATION_ABORTED).then_some(waited) + } +} + +impl WriteOp { + unsafe extern "C" fn complete(_loop: *mut Loop, op: *mut Op) { + let this = op.cast::(); + // SAFETY: `op` is the first field of the `WriteOp` this packet was + // submitted for; the packet is what kept it allocated. + unsafe { + let pipe = (*this).pipe; + // Its thread let go of the job before it posted this packet. + if let Some(mut write) = (*pipe).sync_write.take() + && let Some(job) = Arc::get_mut(&mut write) + { + (*this).owned = core::mem::take(&mut job.bytes); + (*pipe).sync_spare = Some(write); + } + let (mut err, bytes) = completed(&(*this).op, (*this).posted.take()); + if let Some(kind) = (*this).verdict.take() { + Inner::classified(pipe, kind); + match kind { + Kind::Synchronous => {} + // Nothing was written. + Kind::Overlapped if !(*pipe).gone() => { + Inner::submit_write(pipe, this); + return; + } + Kind::Overlapped => err = Win32Error::OPERATION_ABORTED, + Kind::Unusable(errno) => { + (*this).error = Some(sys::Error::from_code(errno, Tag::write)); + Self::finish(this); + return; + } + } + } + if err != Win32Error::SUCCESS { + (*this).error = Some(write_error(err)); + } else { + (*this).done += bytes; + // The rest of a buffer that did not fit one `WriteFile`. + if bytes > 0 && (*this).done < (*this).len && !(*pipe).gone() { + Inner::submit_write(pipe, this); + return; + } + } + Self::finish(this); + } + } + + /// The buffer of a lent write, for its owner: empty when a helper thread + /// that could not be stopped still has the bytes. `None` for any other + /// write. + /// + /// # Safety + /// `this` is live and no longer with the kernel. + unsafe fn reclaim(this: *mut WriteOp) -> Option> { + // SAFETY: caller contract. + unsafe { + if !core::mem::take(&mut (*this).lent) { + return None; + } + Some(core::mem::take(&mut (*this).owned)) + } + } + + /// # Safety + /// `this` is live, counted in its pipe, and referenced by nothing else. + unsafe fn finish(this: *mut WriteOp) { + // SAFETY: caller contract. + unsafe { + let pipe = (*this).pipe; + if (*pipe).chunked_write == this { + (*pipe).chunked_write = ptr::null_mut(); + } + let result: WriteResult = match (*this).error.take() { + Some(err) => Err(err), + None => Ok((*this).done), + }; + let callback = (*this).callback.take(); + let lent = Self::reclaim(this); + Inner::recycle_write(pipe, this); + (*pipe).pending -= 1; + (*pipe).writes_in_flight -= 1; + if (*pipe).event_write == this { + (*pipe).event_write = ptr::null_mut(); + } + + if let Some(callback) = callback + && !(*pipe).flags.contains(Flags::SILENT) + { + (*pipe).lent_back = lent; + (*pipe).pins += 1; + callback.invoke(result); + (*pipe).pins -= 1; + (*pipe).lent_back = None; + } + if (*pipe).gone() { + Inner::maybe_finish(pipe); + return; + } + Inner::pump_writes(pipe, ptr::null_mut()); + Inner::update_keep_alive(pipe); + } + } +} + +// ────────────────────────────────────────────────────────────────────────── +// The write thread of a synchronous HANDLE +// ────────────────────────────────────────────────────────────────────────── + +/// A duplicate of a synchronous pipe HANDLE for the threads that write to it. +/// A thread can be inside a call on it when the pipe closes, and closing the +/// last handle of a synchronous file object waits for such a call: the pipe's +/// own HANDLE is never the last one while a thread has this. +struct SyncHandle(HANDLE); + +// SAFETY: a HANDLE may be used from any thread. +unsafe impl Send for SyncHandle {} +// SAFETY: as above. +unsafe impl Sync for SyncHandle {} + +impl Drop for SyncHandle { + fn drop(&mut self) { + // SAFETY: the duplicate made in `SyncWrite::start`. + unsafe { win::CloseHandle(self.0) }; + } +} + +/// The calling thread's real HANDLE, for `CancelSynchronousIo`. +struct OwnThread(HANDLE); + +impl Drop for OwnThread { + fn drop(&mut self) { + if !self.0.is_null() { + // SAFETY: the duplicate made below. + unsafe { win::CloseHandle(self.0) }; + } + } +} + +thread_local! { + // SAFETY: returns the pseudo-handle. + static OWN_THREAD: OwnThread = + OwnThread(win::duplicate(unsafe { win::GetCurrentThread() }).unwrap_or(ptr::null_mut())); +} + +const SYNC_WRITE_RUNNING: u8 = 0; +/// The thread is handing the result to the port. +const SYNC_WRITE_POSTED: u8 = 1; +/// The loop ended the write itself: the thread lets go without posting. +const SYNC_WRITE_ORPHANED: u8 = 2; + +/// One chunk of a write to a `Sync` or `Unknown` pipe, on a thread-pool +/// thread. A blocking `WriteFile` that another writer of the pipe is ahead of, +/// in whatever process, cannot be cancelled, so the loop never waits for the +/// thread: what the thread needs is here, the bytes included, and outlives the +/// pipe and the loop. +struct SyncWrite { + handle: Arc, + port: Arc, + /// The write's bytes, which the thread may still be reading after the + /// pipe and the loop are gone. The chunk is `bytes[offset..offset + len]`. + bytes: Vec, + offset: usize, + len: usize, + /// [`classify`] `handle` before writing. + classify: bool, + /// The thread's only once it has won `SYNC_WRITE_POSTED`. + op: *mut WriteOp, + state: AtomicU8, + lock: bun_threading::Mutex, + /// The thread is inside `WriteFile`, and `thread` is its HANDLE. + writing: AtomicBool, + cancelled: AtomicBool, + thread: AtomicPtr, +} + +// SAFETY: `op` is touched by one side at a time, as `state` says; the rest is +// atomics and shared handles. +unsafe impl Send for SyncWrite {} +// SAFETY: as above. +unsafe impl Sync for SyncWrite {} + +impl SyncWrite { + /// Hand the next `chunk` bytes of `op` to a thread. + /// + /// # Safety + /// `this` and `op` are live. + unsafe fn start(this: *mut Inner, op: *mut WriteOp, chunk: u32) -> Result<(), Win32Error> { + // SAFETY: caller contract. + unsafe { + // The last job again, if its thread has let go of it. + let mut write = match (*this).sync_spare.take() { + Some(spare) if Arc::strong_count(&spare) == 1 => spare, + _ => { + let Some(port) = Inner::port(this) else { + return Err(win::last_error()); + }; + let handle = match &(*this).sync_handle { + Some(handle) => handle.clone(), + None => { + let handle = win::duplicate((*this).handle)?; + (*this) + .sync_handle + .insert(Arc::new(SyncHandle(handle))) + .clone() + } + }; + Arc::new(SyncWrite { + handle, + port, + bytes: Vec::new(), + offset: 0, + len: 0, + classify: false, + op: ptr::null_mut(), + state: AtomicU8::new(SYNC_WRITE_RUNNING), + lock: bun_threading::Mutex::new(), + writing: AtomicBool::new(false), + cancelled: AtomicBool::new(false), + thread: AtomicPtr::new(ptr::null_mut()), + }) + } + }; + let Some(job) = Arc::get_mut(&mut write) else { + return Err(Win32Error::INVALID_HANDLE); + }; + // The write's bytes go with the job, not copied. Bytes the write + // only borrows are: their owner may be gone before the thread is. + let mut bytes = core::mem::take(&mut (*op).owned); + if bytes.is_empty() && (*op).len > 0 { + bytes = core::slice::from_raw_parts((*op).data, (*op).len).to_vec(); + (*op).data = bytes.as_ptr(); + } + job.offset = (*op).data.add((*op).done) as usize - bytes.as_ptr() as usize; + job.len = chunk as usize; + job.bytes = bytes; + job.classify = (*this).mode == Mode::Unknown; + job.op = op; + *job.state.get_mut() = SYNC_WRITE_RUNNING; + *job.writing.get_mut() = false; + *job.cancelled.get_mut() = false; + + let for_thread = Arc::into_raw(write.clone()); + if !super::queue_blocking_work(SyncWrite::run, for_thread.cast_mut().cast()) { + let err = win::last_error(); + drop(Arc::from_raw(for_thread)); + if let Some(job) = Arc::get_mut(&mut write) { + (*op).owned = core::mem::take(&mut job.bytes); + } + return Err(err); + } + debug_assert!((*this).sync_write.is_none()); + (*this).sync_write = Some(write); + Ok(()) + } + } + + /// The pipe is closing. One attempt to get the thread out of `WriteFile`: + /// it takes while the thread is inside the call with the pipe its to write + /// to. Otherwise the write is the loop's to end, unless the thread is + /// posting it already: returns it then. + fn cancel(&self) -> Option<*mut WriteOp> { + self.lock.lock(); + self.cancelled.store(true, Ordering::Release); + // The thread leaves `writing` only through this lock, so the cancel + // lands on this write or on nothing. + let took = self.writing.load(Ordering::Acquire) + // SAFETY: `thread` is the writing thread's open HANDLE while `writing` is set. + && unsafe { win::CancelSynchronousIo(self.thread.load(Ordering::Acquire)) } != 0; + self.lock.unlock(); + if took { + return None; + } + self.state + .compare_exchange( + SYNC_WRITE_RUNNING, + SYNC_WRITE_ORPHANED, + Ordering::SeqCst, + Ordering::SeqCst, + ) + .ok() + .map(|_| self.op) + } + + /// `false`: the pipe closed, do not write. + fn enter(&self) -> bool { + self.lock.lock(); + let go = !self.cancelled.load(Ordering::Acquire); + if go { + self.thread + .store(OWN_THREAD.with(|own| own.0), Ordering::Release); + self.writing.store(true, Ordering::Release); + } + self.lock.unlock(); + go + } + + fn leave(&self) { + self.writing.store(false, Ordering::Release); + // A cancel that is being issued has returned before anything else + // this thread calls can be taken for the write. + self.lock.lock(); + self.lock.unlock(); + } + + unsafe extern "system" fn run(context: *mut c_void) -> u32 { + // SAFETY: `context` is the reference `start` made for this thread. + let write = unsafe { Arc::from_raw(context.cast::().cast_const()) }; + let mut verdict = None; + let mut error = Win32Error::SUCCESS; + let mut written = 0usize; + 'work: { + if write.classify { + let kind = classify(write.handle.0); + verdict = Some(kind); + if kind != Kind::Synchronous { + break 'work; + } + } + let chunk = &write.bytes[write.offset..write.offset + write.len]; + while written < chunk.len() { + if !write.enter() { + error = Win32Error::OPERATION_ABORTED; + break; + } + let mut n: u32 = 0; + // SAFETY: `handle` is open while `write` is; the range is within `chunk`. + let failed = unsafe { + win::WriteFile( + write.handle.0, + chunk.as_ptr().add(written), + (chunk.len() - written) as u32, + &raw mut n, + ptr::null_mut(), + ) == 0 + } + .then(win::last_error); + write.leave(); + if let Some(err) = failed { + error = err; + break; + } + // A `PIPE_NOWAIT` end with no room takes nothing and says so by + // succeeding; the mode is the pipe end's, which whoever shares + // it can change at any time. + if n == 0 { + break; + } + written += n as usize; + } + } + if write + .state + .compare_exchange( + SYNC_WRITE_RUNNING, + SYNC_WRITE_POSTED, + Ordering::SeqCst, + Ordering::SeqCst, + ) + .is_ok() + { + let op = write.op; + let port = write.port.clone(); + // Before the packet: the loop uses the job again once it has it. + drop(write); + // SAFETY: the loop counts the packet posted here as owed, which + // keeps `op` allocated until it is dequeued; the loop does not + // touch `verdict` or `posted` meanwhile. + unsafe { + (*op).verdict = verdict; + (*op).posted = Some((error, written)); + port.post(&raw mut (*op).op); + } + } + 0 + } +} + +// ────────────────────────────────────────────────────────────────────────── +// Connecting +// ────────────────────────────────────────────────────────────────────────── + +/// A connection attempt to a named pipe. Dropping it abandons the attempt: the +/// callback does not run, and the loop is no longer kept alive for it. +pub struct ConnectRequest { + state: Rc, + loop_: *mut Loop, +} + +/// What the request and its operation share. Both are the loop thread's. +struct ConnectState { + abandoned: Cell, + /// The attempt holds the loop alive. Whichever of the request's drop, the + /// loop's teardown and the completion comes first gives that up. + holds_loop: Cell, + /// The pipe device, while a wait for an instance is out on it. + device: Cell, +} + +impl ConnectState { + /// The attempt is over for its owner: no callback, no hold on the loop, + /// and a wait that is out is cancelled, which brings its packet at once. + /// + /// # Safety + /// `loop_` is the live loop the attempt was started on, unless the hold on + /// it was given up before. + unsafe fn abandon(&self, loop_: *mut Loop) { + self.abandoned.set(true); + // SAFETY: caller contract. + unsafe { self.release_loop(loop_) }; + let device = self.device.get(); + if device != INVALID_HANDLE_VALUE { + // SAFETY: `device` is open until the wait's packet is dequeued, + // and the wait is the one operation on it. + unsafe { win::CancelIoEx(device, ptr::null_mut()) }; + } + } + + /// # Safety + /// As [`abandon`](Self::abandon). + unsafe fn release_loop(&self, loop_: *mut Loop) { + if self.holds_loop.replace(false) { + // SAFETY: caller contract. + unsafe { (*loop_).sub_active(1) }; + } + } +} + +#[repr(C)] +struct ConnectOp { + op: Op, + link: Link, + name: Vec, + opened: Result, + state: Rc, + callback: Callback>, +} + +/// How long a connecting client waits for a busy server to offer an instance. +const CONNECT_BUSY_WAIT_MS: i64 = 30_000; + +/// `FSCTL_PIPE_FLUSH` (`ntifs.h`): `FlushFileBuffers` on a pipe, as a request +/// that can be pending. It completes when the other end has read what was +/// written before it. +const FSCTL_PIPE_FLUSH: u32 = 0x0011_8040; + +/// One [`Pipe::flush_peer`] request. +#[repr(C)] +struct FlushOp { + op: Op, + pipe: *mut Inner, + callback: Callback>, +} + +impl FlushOp { + unsafe extern "C" fn complete(_loop: *mut Loop, op: *mut Op) { + let this = op.cast::(); + // SAFETY: `op` is the first field of the `FlushOp` this packet was + // submitted for; the packet is what kept it allocated. + unsafe { + let this = bun_core::heap::take(this); + let pipe = this.pipe; + (*pipe).pending -= 1; + (*pipe).flush_op = ptr::null_mut(); + let (err, _) = completed(&this.op, None); + if !(*pipe).gone() { + (*pipe).pins += 1; + this.callback.invoke(if err == Win32Error::SUCCESS { + Ok(()) + } else { + Err(sys::Error::from_win32(err, Tag::fsync)) + }); + (*pipe).pins -= 1; + } + if (*pipe).gone() { + Inner::maybe_finish(pipe); + return; + } + Inner::update_keep_alive(pipe); + } + } +} + +/// `FSCTL_PIPE_WAIT` (`ntifs.h`): what `WaitNamedPipeW` sends to the pipe +/// device. `DeviceIoControl` cannot issue it: it takes the FSCTL path only for +/// `FILE_DEVICE_FILE_SYSTEM` codes, and this one is `FILE_DEVICE_NAMED_PIPE`. +const FSCTL_PIPE_WAIT: u32 = 0x0011_0018; +/// `FILE_PIPE_WAIT_FOR_BUFFER`: `LARGE_INTEGER Timeout; ULONG NameLength; +/// BOOLEAN TimeoutSpecified; WCHAR Name[]`, the name starting at this offset. +const PIPE_WAIT_NAME_OFFSET: usize = 14; + +impl Pipe { + /// Connect to the named pipe `name` (`\\.\pipe\…`). `on_connect(ctx, ..)` + /// runs from the loop with the connected pipe or the error. The loop is + /// kept alive until then. + pub fn connect( + loop_: *mut Loop, + name: &[u8], + ctx: *mut T, + on_connect: unsafe fn(*mut T, sys::Result), + ) -> sys::Result { + let name = super::to_wide_z(name); + let opened = open_client(&name); + let busy = opened == Err(Win32Error::PIPE_BUSY); + let state = Rc::new(ConnectState { + abandoned: Cell::new(false), + holds_loop: Cell::new(false), + device: Cell::new(INVALID_HANDLE_VALUE), + }); + let op = bun_core::heap::into_raw(Box::new(ConnectOp { + op: Op::new(ConnectOp::complete), + link: Link::new(loop_, ConnectOp::shut), + name, + opened, + state: state.clone(), + callback: Callback::new(ctx, on_connect), + })); + // SAFETY: `op` is live; `loop_` is the caller's live loop. The op is + // freed only by `ConnectOp::complete`. + unsafe { + if busy { + // Every instance was busy: wait for one and try again. + if let Err(err) = ConnectOp::wait_for_instance(op) { + let device = state.device.replace(INVALID_HANDLE_VALUE); + if device != INVALID_HANDLE_VALUE { + win::CloseHandle(device); + } + drop(bun_core::heap::take(op)); + return Err(sys::Error::from_win32(err, Tag::connect)); + } + } else { + // The outcome is known; it is still reported from the loop. + super::complete_from_loop(loop_, &raw mut (*op).op); + } + Link::insert(&raw mut (*op).link); + (*loop_).add_active(1); + state.holds_loop.set(true); + Ok(ConnectRequest { state, loop_ }) + } + } +} + +impl Drop for ConnectRequest { + fn drop(&mut self) { + // SAFETY: the attempt holds the loop only until its packet is + // dequeued or the loop's teardown shut it, and the request's owner + // drops it before its loop goes otherwise. + unsafe { self.state.abandon(self.loop_) }; + } +} + +/// A client end: duplex if the server allows, else whichever direction it does. +/// No `SECURITY_SQOS_PRESENT`: the server is somebody else's (an ssh agent, a +/// service) and may identify or impersonate its client. +fn open_client(name: &[u16]) -> Result { + let flags = win::FILE_FLAG_OVERLAPPED; + let attempts = [ + win::GENERIC_READ | win::GENERIC_WRITE, + // The opposite direction's *_ATTRIBUTES right is what lets the pipe's + // state be queried and set on a one-way end. + win::GENERIC_READ | win::FILE_WRITE_ATTRIBUTES, + win::GENERIC_WRITE | win::FILE_READ_ATTRIBUTES, + ]; + let mut last = Win32Error::ACCESS_DENIED; + for access in attempts { + // SAFETY: `name` is NUL-terminated. + let handle = unsafe { + win::CreateFileW( + name.as_ptr(), + access, + 0, + ptr::null_mut(), + win::OPEN_EXISTING, + flags, + ptr::null_mut(), + ) + }; + if handle != INVALID_HANDLE_VALUE { + // `\\.\pipe\..\C:\file` is a file to Win32. + if matches!( + bun_sys::File::borrow(&Fd::from_system(handle)).kind(), + Ok(bun_sys::FileKind::NamedPipe) + ) { + return Ok(handle); + } + // SAFETY: `handle` was opened above. + unsafe { win::CloseHandle(handle) }; + return Err(Win32Error::WSAENOTSOCK); + } + last = win::last_error(); + // A one-way pipe refuses the duplex open with ACCESS_DENIED. + if last != Win32Error::ACCESS_DENIED { + break; + } + } + Err(last) +} + +/// The pipe device `name` (NUL-terminated) is served by, as `CreateFileW` +/// takes it, and the pipe's name on that device: what `WaitNamedPipeW` opens +/// and sends. `\\.\pipe\a\b` is `a\b` on `\\.\pipe\`; `\\server\pipe\a` is `a` +/// on `\\server\pipe`. `\\?\pipe\…`, which `WaitNamedPipeW` refuses and +/// `CreateFileW` takes, is the local device too. +fn pipe_device_and_name(name: &[u16]) -> Option<(Vec, Vec)> { + const BACKSLASH: u16 = b'\\' as u16; + let is_sep = |unit: u16| unit == BACKSLASH || unit == u16::from(b'/'); + let name = name.strip_suffix(&[0]).unwrap_or(name); + if name.len() < 2 || !is_sep(name[0]) || !is_sep(name[1]) { + return None; + } + let rest = &name[2..]; + let server_len = rest.iter().position(|&unit| is_sep(unit))?; + let (server, rest) = (&rest[..server_len], &rest[server_len + 1..]); + let pipe_len = rest.iter().position(|&unit| is_sep(unit))?; + let (pipe, pipe_name) = (&rest[..pipe_len], &rest[pipe_len + 1..]); + let is_pipe = pipe.len() == 4 + && pipe.iter().zip(b"pipe").all(|(&unit, &ascii)| { + unit == u16::from(ascii) || unit == u16::from(ascii.to_ascii_uppercase()) + }); + if server.is_empty() || !is_pipe || pipe_name.is_empty() { + return None; + } + let verbatim = server == [u16::from(b'?')]; + let local = verbatim || server == [u16::from(b'.')]; + let mut device: Vec = Vec::with_capacity(server.len() + 9); + device.extend([BACKSLASH, BACKSLASH]); + const DOT: [u16; 1] = [b'.' as u16]; + device.extend(if local { &DOT[..] } else { server }); + device.push(BACKSLASH); + device.extend(b"pipe".iter().map(|&ascii| u16::from(ascii))); + if local { + device.push(BACKSLASH); + } + device.push(0); + // Win32 turns `/` into `\` on the way to the device, except in a `\\?\` name. + let pipe_name = pipe_name + .iter() + .map(|&unit| { + if !verbatim && is_sep(unit) { + BACKSLASH + } else { + unit + } + }) + .collect(); + Some((device, pipe_name)) +} + +impl ConnectOp { + /// Ask the pipe device to say when the server offers an instance. The + /// answer is a packet on the loop's port: [`complete`](Self::complete) + /// tries the open again. + /// + /// # Safety + /// `this` is live, with no wait out; its loop is the calling thread's. + unsafe fn wait_for_instance(this: *mut ConnectOp) -> Result<(), Win32Error> { + // SAFETY: caller contract. + unsafe { + let loop_ = (*this).link.loop_; + let state: &ConnectState = &(*this).state; + let Some((device_path, pipe_name)) = pipe_device_and_name(&(*this).name) else { + return Err(Win32Error::BAD_PATHNAME); + }; + let mut device = state.device.get(); + if device == INVALID_HANDLE_VALUE { + device = win::CreateFileW( + device_path.as_ptr(), + win::FILE_READ_ATTRIBUTES | win::SYNCHRONIZE, + win::FILE_SHARE_READ | win::FILE_SHARE_WRITE, + ptr::null_mut(), + win::OPEN_EXISTING, + win::FILE_FLAG_OVERLAPPED, + ptr::null_mut(), + ); + if device == INVALID_HANDLE_VALUE { + return Err(win::last_error()); + } + if bun_sys::windows::CreateIoCompletionPort(device, iocp::us_loop_iocp(loop_), 0, 0) + .is_err() + { + let err = win::last_error(); + win::CloseHandle(device); + return Err(err); + } + state.device.set(device); + } + + let name_bytes = pipe_name.len() * 2; + let mut request = vec![0u8; PIPE_WAIT_NAME_OFFSET + name_bytes]; + // Relative, in 100 ns units. Left unspecified, the timeout is the + // one the server gave the pipe. + request[0..8].copy_from_slice(&(-(CONNECT_BUSY_WAIT_MS * 10_000)).to_le_bytes()); + request[8..12].copy_from_slice(&(name_bytes as u32).to_le_bytes()); + request[12] = 1; + for (i, unit) in pipe_name.iter().enumerate() { + let at = PIPE_WAIT_NAME_OFFSET + i * 2; + request[at..at + 2].copy_from_slice(&unit.to_le_bytes()); + } + + // The OVERLAPPED's first two fields are the IO_STATUS_BLOCK, as for + // every Win32 overlapped call; its address is what the port hands + // back. The input is copied before the call returns. + (*this).op.reset(ptr::null_mut()); + let overlapped = &raw mut (*this).op.overlapped; + let status = NtFsControlFile( + device, + ptr::null_mut(), + ptr::null_mut(), + overlapped.cast(), + overlapped.cast(), + FSCTL_PIPE_WAIT, + request.as_mut_ptr().cast(), + request.len() as u32, + ptr::null_mut(), + 0, + ); + // A call that fails outright queues no packet. + if status.0 >= 0xC000_0000 { + return Err(Win32Error::from_ntstatus(status)); + } + super::op_submitted(loop_); + Ok(()) + } + } + + /// [`Link::shut`]: the loop is about to be freed. + unsafe fn shut(link: *mut Link) { + // SAFETY: `link` is the `link` field of a listed (live) `ConnectOp`, + // whose loop is still live. + unsafe { + let this = link + .byte_sub(core::mem::offset_of!(ConnectOp, link)) + .cast::(); + Link::remove(link); + let state: &ConnectState = &(*this).state; + state.abandon((*link).loop_); + } + } + + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op) { + let this = op.cast::(); + // SAFETY: `op` is the first field of the `ConnectOp` this packet was + // posted for; the packet is what kept it allocated. + unsafe { + let state: &ConnectState = &(*this).state; + let abandoned = state.abandoned.get(); + if state.device.get() != INVALID_HANDLE_VALUE { + // The packet is the wait's. + let waited = win::status_to_win32((*this).op.status()); + (*this).opened = if !abandoned && waited == Win32Error::SUCCESS { + match open_client(&(*this).name) { + // Another client took the instance first. + Err(Win32Error::PIPE_BUSY) => match Self::wait_for_instance(this) { + Ok(()) => return, + Err(err) => Err(err), + }, + opened => opened, + } + } else { + Err(waited) + }; + win::CloseHandle(state.device.replace(INVALID_HANDLE_VALUE)); + } + + Link::remove(&raw mut (*this).link); + let this = bun_core::heap::take(this); + this.state.release_loop(loop_); + if abandoned { + if let Ok(handle) = this.opened { + win::CloseHandle(handle); + } + return; + } + this.callback.invoke(match this.opened { + // Of the type its server made it. + Ok(handle) => { + Pipe::open_created(loop_, Fd::from_system(handle), true).inspect_err(|_| { + win::CloseHandle(handle); + }) + } + Err(err) => Err(sys::Error::from_win32(err, Tag::connect)), + }); + } + } +} diff --git a/src/io/windows/pipe_server.rs b/src/io/windows/pipe_server.rs new file mode 100644 index 000000000000..f57a9d14d645 --- /dev/null +++ b/src/io/windows/pipe_server.rs @@ -0,0 +1,358 @@ +//! A named-pipe server: a fixed number of instances wait for clients, and each +//! one a client takes is replaced. +//! +//! A slot whose instance cannot be made to wait for a client is left idle and +//! tried again at the start of each tick of the loop ([`Starved`]); the owner +//! does not hear of it, as with libuv and so Node. + +use core::ptr::{self, NonNull}; + +use bun_sys::{self as sys, E, Tag}; +use bun_uws_sys::Loop; +use bun_uws_sys::iocp::{self, Op, Starved}; + +use super::sys as win; +use super::sys::{HANDLE, INVALID_HANDLE_VALUE, Win32Error}; +use super::{Callback, Link, Pipe}; + +/// Instances kept waiting for clients. A burst of more clients than this finds +/// the pipe busy and waits for the next one. +const PENDING_INSTANCES: usize = 4; + +const PIPE_BUFFER_SIZE: u32 = 65536; + +/// The owner's handle to a listening pipe. Dropping it stops listening without +/// telling anyone. Listening does not keep the loop alive: that is the owner's. +pub struct PipeServer { + inner: NonNull, +} + +#[repr(C)] +struct Inner { + link: Link, + starved: Starved, + /// `starved` is linked: a slot is idle. + is_starved: bool, + name: Vec, + slots: Vec<*mut AcceptOp>, + on_connection: Callback, + closing: bool, + owner_gone: bool, + pending: u32, + pins: u32, +} + +#[repr(C)] +struct AcceptOp { + op: Op, + server: *mut Inner, + handle: HANDLE, + /// `arm` found a client connected already and queued the packet itself. + /// Otherwise the packet out is the kernel's and the OVERLAPPED has the + /// status. + posted: bool, + in_flight: bool, +} + +impl PipeServer { + /// Create the pipe `name` and wait for clients. `on_connection(ctx, pipe)` + /// runs from the loop each time a client has connected. + /// + /// `EADDRINUSE` when another server already owns the name, `EACCES` when + /// the name is not one a pipe can have. + pub fn listen( + loop_: *mut Loop, + name: &[u8], + ctx: *mut T, + on_connection: unsafe fn(*mut T, Pipe), + ) -> sys::Result { + if name.is_empty() || bun_core::strings::contains_char(name, 0) { + return Err(sys::Error::from_code(E::EINVAL, Tag::listen)); + } + let name = super::to_wide_z(name); + // The first instance proves nobody else serves this name. + let first = match create_instance(loop_, &name, true) { + Ok(handle) => handle, + Err(err) => { + let errno = if err == Win32Error::ACCESS_DENIED || err == Win32Error::PIPE_BUSY { + E::EADDRINUSE + } else if err == Win32Error::PATH_NOT_FOUND || err == Win32Error::INVALID_NAME { + E::EACCES + } else { + return Err(sys::Error::from_win32(err, Tag::listen)); + }; + return Err(sys::Error::from_code(errno, Tag::listen)); + } + }; + + let inner = bun_core::heap::into_raw(Box::new(Inner { + link: Link::new(loop_, Inner::shut), + starved: Starved::new(Inner::retry), + is_starved: false, + name, + slots: Vec::with_capacity(PENDING_INSTANCES), + on_connection: Callback::new(ctx, on_connection), + closing: false, + owner_gone: false, + pending: 0, + pins: 0, + })); + // SAFETY: `inner` is at its final address with `link` first; the slots + // are owned by it and freed only from their own completions. + unsafe { + Link::insert(inner.cast()); + for index in 0..PENDING_INSTANCES { + let slot = bun_core::heap::into_raw(Box::new(AcceptOp { + op: Op::new(AcceptOp::complete), + server: inner, + handle: if index == 0 { + first + } else { + INVALID_HANDLE_VALUE + }, + posted: false, + in_flight: false, + })); + (*inner).slots.push(slot); + Inner::arm(inner, slot); + } + Inner::update_starved(inner); + Ok(PipeServer { + inner: NonNull::new_unchecked(inner), + }) + } + } +} + +impl Drop for PipeServer { + fn drop(&mut self) { + // SAFETY: `inner` is live until `Inner::close` decides otherwise. + unsafe { Inner::close(self.inner.as_ptr()) }; + } +} + +/// One more instance of `name`, associated with the loop's port. +fn create_instance(loop_: *mut Loop, name: &[u16], first: bool) -> Result { + // WRITE_DAC as libuv asks for it: nothing here changes the access control. + let open_mode = win::PIPE_ACCESS_DUPLEX + | win::FILE_FLAG_OVERLAPPED + | win::WRITE_DAC + | if first { + win::FILE_FLAG_FIRST_PIPE_INSTANCE + } else { + 0 + }; + // SAFETY: `name` is NUL-terminated; `loop_` is the caller's live loop. + unsafe { + let handle = win::CreateNamedPipeW( + name.as_ptr(), + open_mode, + win::PIPE_TYPE_BYTE | win::PIPE_READMODE_BYTE | win::PIPE_WAIT, + win::PIPE_UNLIMITED_INSTANCES, + PIPE_BUFFER_SIZE, + PIPE_BUFFER_SIZE, + 0, + ptr::null_mut(), + ); + if handle == INVALID_HANDLE_VALUE { + return Err(win::last_error()); + } + if bun_sys::windows::CreateIoCompletionPort(handle, iocp::us_loop_iocp(loop_), 0, 0) + .is_err() + { + let err = win::last_error(); + win::CloseHandle(handle); + return Err(err); + } + Ok(handle) + } +} + +impl Inner { + /// Wait for a client on `slot`, creating its instance first if it has none. + /// A slot that cannot be made to wait stays idle, without an instance. + /// + /// # Safety + /// `this` and the idle `slot` are live; `this` is not closing. + unsafe fn arm(this: *mut Inner, slot: *mut AcceptOp) { + // SAFETY: caller contract. The OVERLAPPED lives in `slot`, which is + // freed only from its own completion. + unsafe { + let loop_ = (*this).link.loop_; + if (*slot).handle == INVALID_HANDLE_VALUE { + match create_instance(loop_, &(*this).name, false) { + Ok(handle) => (*slot).handle = handle, + Err(_) => return, + } + } + (*slot).op.reset(ptr::null_mut()); + // A call that succeeded at once queues its packet like one that pends. + let queued = win::ConnectNamedPipe((*slot).handle, (&raw mut (*slot).op).cast()) != 0 + || match win::last_error() { + Win32Error::IO_PENDING => true, + // A client got in between the instance's creation and this + // call, and nothing is queued. `NO_DATA`: it has closed its + // end already; what it wrote is still there to read. + Win32Error::PIPE_CONNECTED | Win32Error::NO_DATA => false, + _ => { + win::CloseHandle((*slot).handle); + (*slot).handle = INVALID_HANDLE_VALUE; + return; + } + }; + (*slot).posted = !queued; + // The owner hears of every client from the loop. + if queued { + super::op_submitted(loop_); + } else { + super::complete_from_loop(loop_, &raw mut (*slot).op); + } + (*slot).in_flight = true; + (*this).pending += 1; + } + } + + /// Link or unlink `starved` to match whether a slot is idle. + /// + /// # Safety + /// `this` is live and its loop is too. Must run on the loop's thread. + unsafe fn update_starved(this: *mut Inner) { + // SAFETY: caller contract; slots are live while listed. + unsafe { + let wanted = !(*this).closing && (*this).slots.iter().any(|&slot| !(*slot).in_flight); + if wanted == (*this).is_starved { + return; + } + (*this).is_starved = wanted; + if wanted { + iocp::us_iocp_starved_link((*this).link.loop_, &raw mut (*this).starved); + } else { + iocp::us_iocp_starved_unlink((*this).link.loop_, &raw mut (*this).starved); + } + } + } + + /// The start of a tick with a slot idle. No owner callback runs from here: + /// a client `arm` finds connected is reported from the ready list. + unsafe extern "C" fn retry(starved: *mut Starved) { + // SAFETY: `starved` is the field of a live `Inner` that linked it, + // which is listening. `arm` leaves `slots` alone. + unsafe { + let this = starved + .byte_sub(core::mem::offset_of!(Inner, starved)) + .cast::(); + for index in 0..(&(*this).slots).len() { + let slot = (&(*this).slots)[index]; + if !(*slot).in_flight { + Self::arm(this, slot); + } + } + Self::update_starved(this); + } + } + + /// # Safety + /// `this` is live; the owner's `PipeServer` is consumed or being dropped. + unsafe fn close(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + (*this).owner_gone = true; + if !(*this).closing { + Self::stop(this); + (*this).closing = true; + Self::update_starved(this); + } + Self::maybe_finish(this); + } + } + + /// [`Link::shut`]: the loop is about to be freed. + unsafe fn shut(link: *mut Link) { + let this = link.cast::(); + // SAFETY: `link` is the first field of a listed (live) `Inner`. + unsafe { + if !(*this).closing { + Self::stop(this); + (*this).closing = true; + Self::update_starved(this); + } + Link::remove(link); + Self::maybe_finish(this); + } + } + + /// # Safety + /// `this` is live and not closing. + unsafe fn stop(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + // Closed now, not when the packets come back: the name is free + // for the next listener as soon as this returns. Closing aborts + // the pending `ConnectNamedPipe`, whose packet still arrives. + for &slot in &(*this).slots { + if (*slot).handle != INVALID_HANDLE_VALUE { + win::CloseHandle((*slot).handle); + (*slot).handle = INVALID_HANDLE_VALUE; + } + } + } + } + + /// # Safety + /// `this` is live. + unsafe fn maybe_finish(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + if !(*this).closing || (*this).pending > 0 || (*this).pins > 0 { + return; + } + // `stop` closed their instances. + for slot in (*this).slots.drain(..) { + drop(bun_core::heap::take(slot)); + } + if !(*this).owner_gone { + return; + } + Link::remove(this.cast()); + drop(bun_core::heap::take(this)); + } + } +} + +impl AcceptOp { + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op) { + let slot = op.cast::(); + // SAFETY: `op` is the first field of the `AcceptOp` this packet was + // submitted for. The server outlives its slots' packets (`pending`). + unsafe { + let this = (*slot).server; + (*slot).in_flight = false; + (*this).pending -= 1; + if (*this).closing { + Inner::maybe_finish(this); + return; + } + let connected = core::mem::take(&mut (*slot).posted) + || win::status_to_win32((*slot).op.status()) == Win32Error::SUCCESS; + if !connected { + // The wait failed and took the instance with it. + win::CloseHandle((*slot).handle); + (*slot).handle = INVALID_HANDLE_VALUE; + Inner::update_starved(this); + return; + } + + let pipe = Pipe::from_associated(loop_, (*slot).handle); + (*slot).handle = INVALID_HANDLE_VALUE; + // A fresh instance waits for the next client before the owner + // hears of this one. + Inner::arm(this, slot); + Inner::update_starved(this); + let on_connection = (*this).on_connection; + (*this).pins += 1; + on_connection.invoke(pipe); + (*this).pins -= 1; + Inner::maybe_finish(this); + } + } +} diff --git a/src/io/windows/sys.rs b/src/io/windows/sys.rs new file mode 100644 index 000000000000..508d85f72de0 --- /dev/null +++ b/src/io/windows/sys.rs @@ -0,0 +1,126 @@ +//! Win32 surface used by the pipe, console and file sources that +//! `bun_windows_sys` does not declare. + +#![allow(non_snake_case, non_camel_case_types, clippy::upper_case_acronyms)] + +use core::ffi::c_void; + +pub(crate) use bun_windows_sys::GetCurrentThread; +pub(crate) use bun_windows_sys::kernel32::{ + CreateNamedPipeW, DuplicateHandle, GetCurrentProcess, ReadFile, WriteFile, +}; +pub(crate) use bun_windows_sys::ntdll::NtQueryInformationFile; +pub(crate) use bun_windows_sys::{ + BOOL, CancelIoEx, CloseHandle, ConnectNamedPipe, CreateEventW, CreateFileW, + DUPLICATE_SAME_ACCESS, DWORD, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, + ENABLE_VIRTUAL_TERMINAL_INPUT, FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, + FILE_INFORMATION_CLASS, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, FILE_SHARE_WRITE, + FILE_WRITE_ATTRIBUTES, GENERIC_READ, GENERIC_WRITE, GetConsoleMode, HANDLE, INPUT_RECORD, + INVALID_HANDLE_VALUE, IO_STATUS_BLOCK, KEY_EVENT, LEFT_CTRL_PRESSED, NTSTATUS, OPEN_EXISTING, + OVERLAPPED, PIPE_READMODE_BYTE, PIPE_TYPE_BYTE, PIPE_WAIT, SYNCHRONIZE, SetConsoleMode, + SetEvent, WRITE_DAC, Win32Error, +}; + +pub(crate) const PIPE_ACCESS_DUPLEX: DWORD = 0x0000_0003; +pub(crate) const PIPE_UNLIMITED_INSTANCES: DWORD = 255; +pub(crate) const PIPE_NOWAIT: DWORD = 0x0000_0001; +pub(crate) const PIPE_READMODE_MESSAGE: DWORD = 0x0000_0002; +pub(crate) const WT_EXECUTELONGFUNCTION: u32 = 0x0000_0010; + +#[repr(C)] +pub(crate) struct CONSOLE_READCONSOLE_CONTROL { + pub nLength: u32, + pub nInitialChars: u32, + pub dwCtrlWakeupMask: u32, + pub dwControlKeyState: u32, +} + +pub(crate) const ENABLE_WINDOW_INPUT: DWORD = 0x0008; + +pub(crate) type LPTHREAD_START_ROUTINE = unsafe extern "system" fn(*mut c_void) -> DWORD; + +#[link(name = "kernel32")] +unsafe extern "system" { + pub(crate) fn CancelSynchronousIo(hThread: HANDLE) -> BOOL; + pub(crate) fn PostQueuedCompletionStatus( + CompletionPort: HANDLE, + dwNumberOfBytesTransferred: DWORD, + dwCompletionKey: usize, + lpOverlapped: *mut OVERLAPPED, + ) -> BOOL; + pub(crate) fn GetNamedPipeClientProcessId(Pipe: HANDLE, ClientProcessId: *mut u32) -> BOOL; + pub(crate) fn GetNamedPipeServerProcessId(Pipe: HANDLE, ServerProcessId: *mut u32) -> BOOL; + pub(crate) fn SetNamedPipeHandleState( + hNamedPipe: HANDLE, + lpMode: *mut DWORD, + lpMaxCollectionCount: *mut DWORD, + lpCollectDataTimeout: *mut DWORD, + ) -> BOOL; + pub(crate) fn GetNamedPipeHandleStateW( + hNamedPipe: HANDLE, + lpState: *mut DWORD, + lpCurInstances: *mut DWORD, + lpMaxCollectionCount: *mut DWORD, + lpCollectDataTimeout: *mut DWORD, + lpUserName: *mut u16, + nMaxUserNameSize: DWORD, + ) -> BOOL; + pub(crate) fn QueueUserWorkItem( + Function: LPTHREAD_START_ROUTINE, + Context: *mut c_void, + Flags: u32, + ) -> BOOL; + pub(crate) fn ReadConsoleW( + hConsoleInput: HANDLE, + lpBuffer: *mut c_void, + nNumberOfCharsToRead: DWORD, + lpNumberOfCharsRead: *mut DWORD, + pInputControl: *mut CONSOLE_READCONSOLE_CONTROL, + ) -> BOOL; + pub(crate) fn WriteConsoleInputW( + hConsoleInput: HANDLE, + lpBuffer: *const INPUT_RECORD, + nLength: DWORD, + lpNumberOfEventsWritten: *mut DWORD, + ) -> BOOL; + pub(crate) fn GetNumberOfConsoleInputEvents( + hConsoleInput: HANDLE, + lpcNumberOfEvents: *mut DWORD, + ) -> BOOL; +} + +/// `GetLastError()` of the call that just failed. +#[inline] +pub(crate) fn last_error() -> Win32Error { + Win32Error::get() +} + +/// A duplicate of `handle` with the same access, which children do not inherit. +pub(crate) fn duplicate(handle: HANDLE) -> Result { + let mut duplicate: HANDLE = core::ptr::null_mut(); + // SAFETY: `duplicate` is a live local; a `handle` that is not one is an error. + let ok = unsafe { + DuplicateHandle( + GetCurrentProcess(), + handle, + GetCurrentProcess(), + &raw mut duplicate, + 0, + 0, + DUPLICATE_SAME_ACCESS, + ) + }; + if ok == 0 { + return Err(last_error()); + } + Ok(duplicate) +} + +/// The Win32 error a completed overlapped operation's NTSTATUS stands for. +#[inline] +pub(crate) fn status_to_win32(status: i32) -> Win32Error { + if status == 0 { + return Win32Error::SUCCESS; + } + Win32Error::from_ntstatus(NTSTATUS(status as u32)) +} diff --git a/src/io/windows/tty.rs b/src/io/windows/tty.rs new file mode 100644 index 000000000000..ab1813e81b49 --- /dev/null +++ b/src/io/windows/tty.rs @@ -0,0 +1,1451 @@ +//! A console handle driven by the loop. +//! +//! Console handles cannot do overlapped I/O, but an input handle is signalled +//! while its queue holds records. In raw mode the loop waits on the handle and +//! reads the records itself; in line mode the console only hands out whole +//! edited lines through a blocking `ReadConsoleW`, which runs on a helper +//! thread. Output is synchronous. +//! +//! The input mode belongs to the console, not to a handle, so it is +//! process-wide state here too: every reader picks its mechanism from it when +//! it arms, and a mode change wakes the readers so they re-arm: a line reader +//! through the wake key, a raw reader through [`LINE_MODE`]. +//! +//! The console cannot abandon a line read, but it returns from one when a +//! chosen control character is entered (`dwCtrlWakeupMask`). Ending a read +//! therefore means typing that key into the console's input queue, which every +//! reader of the console shares. [`LINE_LOCK`] keeps the key addressed: one +//! `ReadConsoleW` of this process at a time, the key typed only while that call +//! is out and once per call, and the console's mode changed only between calls +//! so the call is always a cooked one. + +use core::ffi::c_void; +use core::ptr::{self, NonNull}; +use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, AtomicU32, AtomicU64, Ordering}; +use std::collections::VecDeque; +use std::sync::Arc; + +use bun_sys::{self as sys, E, Fd, FdExt as _, Tag}; +use bun_uws_sys::Loop; +use bun_uws_sys::iocp::{self, CompleteFn, Op, Wait}; + +use super::pipe::ReadEvent; +use super::sys as win; +use super::sys::{HANDLE, INVALID_HANDLE_VALUE, Win32Error}; +use super::tty_input::{self, RawInputState}; +use super::tty_output::{self, OutputState}; +use super::{Callback, Link, Port, ReadCallback}; + +/// How console input is delivered. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[repr(u8)] +pub enum Mode { + /// Whole lines, edited and echoed by the console. + Normal = 0, + /// Every key as it is pressed, translated to VT sequences here. + Raw = 1, + /// As `Raw`, with the console doing the VT translation when it can. + RawVt = 2, +} + +static INPUT_MODE: AtomicU8 = AtomicU8::new(Mode::Normal as u8); +/// The console's input mode before the first change; `u32::MAX` until then. +static ORIGINAL_INPUT_MODE: AtomicU32 = AtomicU32::new(u32::MAX); +/// [`set_console_mode`] found the process's stdin to be a console's input. Every +/// question put to a console is a round trip to another process. Remembered for +/// that HANDLE alone, which stays open: the number of one that was closed is +/// given out again. +static STDIN_IS_INPUT: AtomicBool = AtomicBool::new(false); +/// A manual-reset event that is set while the console is in line mode, made by +/// the first change to a raw mode. Every raw reader waits on it next to the +/// console's input. +static LINE_MODE: AtomicPtr = AtomicPtr::new(ptr::null_mut()); +static ON_RESIZE: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut()); + +/// Held to change anything below, to change the console's mode, and to type +/// the wake key. +static LINE_LOCK: bun_threading::Mutex = bun_threading::Mutex::new(); +/// A helper thread waits here for [`LINE_READER`] to become free, or to be +/// told that its read is not wanted any more. +static LINE_TURN: bun_threading::Condition = bun_threading::Condition::new(); +/// The read whose helper thread is inside `ReadConsoleW`. +static LINE_READER: AtomicPtr = AtomicPtr::new(ptr::null_mut()); +/// The wake key has been typed for [`LINE_READER`]'s call. +static WAKE_TYPED: AtomicBool = AtomicBool::new(false); +/// Console mode flags (wanted in the high half, fallback in the low half) that +/// [`LINE_READER`]'s helper sets when its call returns; `u64::MAX` for none. +static PENDING_FLAGS: AtomicU64 = AtomicU64::new(u64::MAX); + +/// Ends a blocked line read without touching the screen: the console returns +/// from `ReadConsoleW` when this control character is entered +/// (`dwCtrlWakeupMask`) and stores it where the cursor was. What was typed left +/// of the cursor is in front of it; what follows it is not the text right of +/// the cursor on every console host. +const WAKE_CHAR: u16 = 0x1d; +/// No keyboard produces this virtual key, so the record below is never a key +/// the user pressed (Ctrl+] is the same character). The line editor only looks +/// at the character. +const WAKE_VIRTUAL_KEY: u16 = 0xFF; + +#[link(name = "kernel32")] +unsafe extern "system" { + fn ResetEvent(hEvent: HANDLE) -> win::BOOL; +} + +/// UTF-16 units asked of one line read. Converted to UTF-8 they stay within +/// the 8 KiB the console handles well. +const LINE_READ_CHARS: usize = (8192 - 1) / 3; + +fn current_mode() -> Mode { + match INPUT_MODE.load(Ordering::Acquire) { + 1 => Mode::Raw, + 2 => Mode::RawVt, + _ => Mode::Normal, + } +} + +/// `SetConsoleMode` flags for `mode`: the ones wanted, and the ones to settle +/// for on a console that refuses those. +fn mode_flags(mode: Mode) -> (u32, u32) { + match mode { + Mode::Normal => { + let flags = + win::ENABLE_ECHO_INPUT | win::ENABLE_LINE_INPUT | win::ENABLE_PROCESSED_INPUT; + (flags, flags) + } + Mode::Raw => (win::ENABLE_WINDOW_INPUT, win::ENABLE_WINDOW_INPUT), + // A console without VT input: the key table does the translation. + Mode::RawVt => ( + win::ENABLE_WINDOW_INPUT | win::ENABLE_VIRTUAL_TERMINAL_INPUT, + win::ENABLE_WINDOW_INPUT, + ), + } +} + +/// # Safety +/// `input` is a console input handle. +unsafe fn apply_flags(input: HANDLE, wanted: u32, fallback: u32) -> Result<(), Win32Error> { + if win::SetConsoleMode(input, wanted) != 0 + || (fallback != wanted && win::SetConsoleMode(input, fallback) != 0) + { + return Ok(()); + } + Err(win::last_error()) +} + +/// Set the input mode of the console `input` belongs to. Readers on any thread +/// switch between the line and raw mechanisms on their own. +pub fn set_console_mode(input: HANDLE, mode: Mode) -> sys::Result<()> { + let fail = |err: Win32Error| sys::Error::from_win32(err, Tag::uv_tty_set_mode); + let (wanted, fallback) = mode_flags(mode); + let _lock = LINE_LOCK.lock_guard(); + let mut previous: u32 = 0; + let is_stdin = input == Fd::stdin().native(); + if !(is_stdin && STDIN_IS_INPUT.load(Ordering::Acquire)) { + // A screen buffer has a mode too, with other bits. + let mut events: u32 = 0; + // SAFETY: `events` is a live local. + if unsafe { win::GetNumberOfConsoleInputEvents(input, &raw mut events) } == 0 { + return Err(if win::GetConsoleMode(input, &mut previous) == 0 { + fail(win::last_error()) + } else { + sys::Error::from_code(E::EINVAL, Tag::uv_tty_set_mode) + }); + } + if is_stdin { + STDIN_IS_INPUT.store(true, Ordering::Release); + } + } + if ORIGINAL_INPUT_MODE.load(Ordering::Acquire) == u32::MAX + && win::GetConsoleMode(input, &mut previous) == 0 + { + return Err(fail(win::last_error())); + } + if mode == Mode::Normal && current_mode() == Mode::Normal { + // The console is as it was found. Its other bits (QuickEdit, insert + // mode, mouse input) are the user's. + return Ok(()); + } + if mode != Mode::Normal && LINE_MODE.load(Ordering::Acquire).is_null() { + // SAFETY: plain Win32 call: manual-reset, not set, unnamed. + let event = unsafe { win::CreateEventW(ptr::null_mut(), 1, 0, ptr::null()) }; + if event.is_null() { + return Err(fail(win::last_error())); + } + LINE_MODE.store(event, Ordering::Release); + } + if ORIGINAL_INPUT_MODE + .compare_exchange(u32::MAX, previous, Ordering::AcqRel, Ordering::Acquire) + .is_ok() + { + bun_core::add_exit_callback(Bun__Windows__resetConsoleMode); + } + if mode == Mode::Normal || LINE_READER.load(Ordering::Acquire).is_null() { + // SAFETY: `GetConsoleMode` accepted `input`. + unsafe { apply_flags(input, wanted, fallback) }.map_err(fail)?; + PENDING_FLAGS.store(u64::MAX, Ordering::Release); + } else { + // A `ReadConsoleW` that finds the console out of line mode hands back + // whatever is queued, the wake key anywhere among it. The helper makes + // the change when its call, woken here, has returned. + PENDING_FLAGS.store( + u64::from(wanted) << 32 | u64::from(fallback), + Ordering::Release, + ); + type_wake_key(); + } + let was = current_mode(); + if mode != Mode::Normal { + signal_line_mode(false); + } + INPUT_MODE.store(mode as u8, Ordering::Release); + if mode == Mode::Normal { + signal_line_mode(true); + } + if was == Mode::Normal && mode != Mode::Normal { + // Helpers still waiting for their turn have nothing to wait for. + LINE_TURN.broadcast(); + } + Ok(()) +} + +/// Put back the input mode the console had before [`set_console_mode`] first +/// changed it. The console keeps its mode after the process is gone, so this +/// runs on exit. +pub fn reset_console_mode() { + let original = ORIGINAL_INPUT_MODE.load(Ordering::Acquire); + if original == u32::MAX { + return; + } + // By name: a handle the mode was changed through may have been closed + // since, and its value reused for something else. + let console = open_console_input(); + if console == INVALID_HANDLE_VALUE { + return; + } + { + let _lock = LINE_LOCK.lock_guard(); + // A line read that is out wants the console in line mode, which is + // what this is about to be: nothing to wait for. + PENDING_FLAGS.store(u64::MAX, Ordering::Release); + // SAFETY: `console` is the console's input. + let _ = unsafe { apply_flags(console, original, original) }; + INPUT_MODE.store(Mode::Normal as u8, Ordering::Release); + signal_line_mode(true); + } + // SAFETY: opened above. + unsafe { win::CloseHandle(console) }; +} + +/// The input buffer of this process's console, for reading and writing; +/// `INVALID_HANDLE_VALUE` when it has none. +fn open_console_input() -> HANDLE { + // SAFETY: plain Win32 call; the name is NUL-terminated. + unsafe { + win::CreateFileW( + bun_core::w!("CONIN$\0").as_ptr(), + win::GENERIC_READ | win::GENERIC_WRITE, + bun_sys::windows::FILE_SHARE_READ | bun_sys::windows::FILE_SHARE_WRITE, + ptr::null_mut(), + win::OPEN_EXISTING, + 0, + ptr::null_mut(), + ) + } +} + +/// [`set_console_mode`] for `wtf-bindings.cpp`. `mode` is 0 normal, 1 raw, +/// 2 "IO" (not supported) or 3 raw with VT input, the numbers of libuv's +/// `uv_tty_mode_t`; the result is 0 or a negative `UV_E*` number. +#[unsafe(no_mangle)] +extern "C" fn Bun__Windows__setConsoleMode( + input: HANDLE, + mode: core::ffi::c_int, +) -> core::ffi::c_int { + let mode = match mode { + 0 => Mode::Normal, + 1 => Mode::Raw, + 2 => return bun_errno::uv_codes::UV_ENOTSUP, + 3 => Mode::RawVt, + _ => return bun_errno::uv_codes::UV_EINVAL, + }; + match set_console_mode(input, mode) { + Ok(()) => 0, + Err(err) => err.uv_errno(), + } +} + +/// [`reset_console_mode`] for C++. +#[unsafe(no_mangle)] +extern "C" fn Bun__Windows__resetConsoleMode() { + reset_console_mode(); +} + +/// `listener` runs on a reading loop's thread when the console reports that its +/// window changed size. Only a raw-mode reader sees those reports. +pub fn set_resize_listener(listener: Option) { + ON_RESIZE.store( + listener.map_or(ptr::null_mut(), |f| f as *mut ()), + Ordering::Release, + ); +} + +/// Set or reset [`LINE_MODE`]. With [`LINE_LOCK`] held; reset before +/// `INPUT_MODE` leaves `Normal` and set after it is `Normal` again, so the +/// event is never set while `INPUT_MODE` says raw. +fn signal_line_mode(line_mode: bool) { + let event = LINE_MODE.load(Ordering::Acquire); + if event.is_null() { + return; + } + // SAFETY: `event` is the event made by `set_console_mode`, never closed. + unsafe { + if line_mode { + win::SetEvent(event); + } else { + ResetEvent(event); + } + } +} + +/// End [`LINE_READER`]'s `ReadConsoleW`. With [`LINE_LOCK`] held. +fn type_wake_key() { + let reader = LINE_READER.load(Ordering::Acquire); + if reader.is_null() || WAKE_TYPED.load(Ordering::Acquire) { + return; + } + let record = wake_record(); + let type_into = |console: HANDLE| { + let mut written: u32 = 0; + // SAFETY: `console` is an open console input handle; `written` is a live local. + let ok = + unsafe { win::WriteConsoleInputW(console, &raw const record, 1, &raw mut written) } + != 0; + ok && written == 1 + }; + // SAFETY: `reader` stays allocated while it is `LINE_READER`, which only + // changes under the lock the caller holds. + let mut typed = type_into(unsafe { (*reader).handle }); + if !typed { + // A console input handle opened without `GENERIC_WRITE` (a parent's + // `CreateFileW("CONIN$", GENERIC_READ)`, cmd.exe's `< CON`) refuses the + // record with `ERROR_ACCESS_DENIED`. The input buffer is the console's, + // not the handle's: by name it opens for writing. + let console = open_console_input(); + if console != INVALID_HANDLE_VALUE { + typed = type_into(console); + // SAFETY: opened above. + unsafe { win::CloseHandle(console) }; + } + } + WAKE_TYPED.store(typed, Ordering::Release); +} + +pub(super) fn wake_record() -> win::INPUT_RECORD { + // SAFETY: an all-zero `INPUT_RECORD` is a valid (empty) record. + let mut record: win::INPUT_RECORD = unsafe { bun_core::ffi::zeroed_unchecked() }; + record.EventType = win::KEY_EVENT; + record.Event.KeyEvent = bun_windows_sys::KEY_EVENT_RECORD { + bKeyDown: 1, + wRepeatCount: 1, + wVirtualKeyCode: WAKE_VIRTUAL_KEY, + wVirtualScanCode: 0, + uChar: bun_windows_sys::KEY_EVENT_RECORD_uChar { + UnicodeChar: WAKE_CHAR, + }, + dwControlKeyState: win::LEFT_CTRL_PRESSED, + }; + record +} + +/// Whether `key` is [`wake_record`] as the console hands it to a raw reader. +pub(super) fn is_wake_key(key: &bun_windows_sys::KEY_EVENT_RECORD) -> bool { + // SAFETY: both members of the union are plain integers. + key.wVirtualKeyCode == WAKE_VIRTUAL_KEY && unsafe { key.uChar.UnicodeChar } == WAKE_CHAR +} + +/// The owner's handle to a console. Dropping it closes without telling anyone. +pub struct Tty { + inner: NonNull, +} + +bitflags::bitflags! { + #[derive(Clone, Copy)] + struct Flags: u8 { + const READABLE = 1 << 0; + const READING = 1 << 1; + const REFD = 1 << 2; + const KEEPING_ALIVE = 1 << 3; + const CLOSING = 1 << 4; + const DETACHED = 1 << 5; + const OWNER_GONE = 1 << 6; + const SILENT = 1 << 7; + } +} + +#[repr(C)] +struct Inner { + link: Link, + handle: HANDLE, + close_fd: Option, + /// `handle` is this tty's own duplicate of a standard handle. + duplicated: bool, + flags: Flags, + + reader: Option, + raw_state: RawInputState, + /// The wait on the console's input, and what was read when it fired. + raw_op: *mut WaitOp, + raw_buf: Vec, + /// The wait on [`LINE_MODE`], armed with `raw_op`. + mode_op: *mut WaitOp, + line_op: *mut LineOp, + /// A line that arrived while the owner was not reading. + held: Vec, + held_error: Option, + /// See [`Tty::end_input_at_ctrl_z`]. + ctrl_z_ends_input: bool, + /// A line that started with Ctrl-Z arrived, or Ctrl+C ended the read; + /// nothing was read after it. + held_end: bool, + + output: OutputState, + /// Writes whose callback is due, oldest first. + written: VecDeque<(Callback>, sys::Result)>, + /// Held input is due to a reader that resumed. + held_due: bool, + posted: PostedOp, + + pending: u32, + pins: u32, +} + +/// A wait on a handle: the console's input, or [`LINE_MODE`]. +#[repr(C)] +struct WaitOp { + op: Op, + tty: *mut Inner, + wait: *mut Wait, + armed: bool, +} + +#[repr(C)] +struct LineOp { + op: Op, + tty: *mut Inner, + handle: HANDLE, + port: Option>, + in_flight: bool, + /// The owner wants this read over: it is closing (`cancel`) or stopped + /// reading (`stop`). A mode change is seen through `INPUT_MODE` instead. + /// Raised with [`LINE_LOCK`] held. + cancel: AtomicBool, + stop: AtomicBool, + /// `chars[..carry]` is what the user had typed when a read was woken. The + /// console took it out of its line editor, so the next read hands it back + /// through `nInitialChars`. + chars: Vec, + carry: usize, + bytes: Vec, + error: u32, + woken: bool, + /// Who finishes the read that is out (`LINE_*`). + finisher: AtomicU8, + /// What closes `handle`, once the read is the helper thread's to free. + own: Option, +} + +const LINE_REQUESTED: u8 = 0; +/// The helper thread is handing the result to the port. +const LINE_POSTED: u8 = 1; +/// The loop went away while the console had the read: the thread frees it. +const LINE_ORPHANED: u8 = 2; + +/// A packet that carries no I/O: it delivers `written` and held input. +#[repr(C)] +struct PostedOp { + op: Op, + tty: *mut Inner, + /// On its way through `complete_from_loop`. + out: bool, +} + +impl Tty { + /// Take over the console handle behind `fd`. Standard handles are + /// duplicated and the original is left alone; any other `fd` is closed + /// with the tty when `close_fd` is set. On `Err` the caller still owns `fd`. + pub fn open(loop_: *mut Loop, fd: Fd, close_fd: bool) -> sys::Result { + let original = fd.native(); + let (handle, close_with) = if fd.stdio_tag().is_some() { + let dup = sys::dup(fd)?; + (dup.native(), Some(dup)) + } else { + (original, close_fd.then_some(fd)) + }; + + let mut events: u32 = 0; + // SAFETY: `events` is a live local. Only an input handle answers this. + let readable = unsafe { win::GetNumberOfConsoleInputEvents(handle, &raw mut events) } != 0; + if !readable && let Err(code) = tty_output::init_output_handle(handle) { + if handle != original { + // SAFETY: `handle` is the duplicate made above. + unsafe { win::CloseHandle(handle) }; + } + return Err(sys::Error::from_win32(Win32Error::from_u32(code), Tag::open).with_fd(fd)); + } + + let inner = bun_core::heap::into_raw(Box::new(Inner { + link: Link::new(loop_, Inner::shut), + handle, + close_fd: close_with, + duplicated: handle != original, + flags: if readable { + Flags::REFD | Flags::READABLE + } else { + Flags::REFD + }, + reader: None, + raw_state: RawInputState::new(), + raw_op: ptr::null_mut(), + raw_buf: Vec::new(), + mode_op: ptr::null_mut(), + line_op: ptr::null_mut(), + held: Vec::new(), + held_error: None, + ctrl_z_ends_input: false, + held_end: false, + output: OutputState::new(), + written: VecDeque::new(), + held_due: false, + posted: PostedOp { + op: Op::new(PostedOp::complete), + tty: ptr::null_mut(), + out: false, + }, + pending: 0, + pins: 0, + })); + // SAFETY: `inner` is at its final address; `link` is its first field. + unsafe { + (*inner).posted.tty = inner; + Link::insert(inner.cast()); + Ok(Tty { + inner: NonNull::new_unchecked(inner), + }) + } + } + + #[inline] + fn raw(&self) -> *mut Inner { + self.inner.as_ptr() + } + + pub fn handle(&self) -> HANDLE { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { (*self.raw()).handle } + } + + /// The HANDLE as a system-kind `Fd`; `Fd::INVALID` once closed. + pub fn fd(&self) -> Fd { + let handle = self.handle(); + if handle == INVALID_HANDLE_VALUE { + return Fd::INVALID; + } + Fd::from_system(handle) + } + + pub fn is_reading(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Tty` is. + let inner = unsafe { &*self.raw() }; + !inner.flags.contains(Flags::DETACHED) && inner.flags.contains(Flags::READING) + } + + /// Leave the caller's `Fd` open when the tty closes. A duplicate made for + /// a standard handle is still released. + pub fn disown(&mut self) { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { + if !(*self.raw()).duplicated { + (*self.raw()).close_fd = None; + } + } + } + + pub fn is_active(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Tty` is. + let inner = unsafe { &*self.raw() }; + !inner.flags.contains(Flags::DETACHED) + && (inner.flags.contains(Flags::READING) || !inner.written.is_empty()) + } + + /// Let reading keep the loop alive (the default). + pub fn ref_(&self) { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { + (*self.raw()).flags.insert(Flags::REFD); + Inner::update_keep_alive(self.raw()); + } + } + + pub fn unref(&self) { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { + (*self.raw()).flags.remove(Flags::REFD); + Inner::update_keep_alive(self.raw()); + } + } + + /// A line that starts with Ctrl-Z ends the input (`ReadEvent::Eof`), and + /// the rest of that line is dropped: what a `ReadFile` of a console in line + /// mode does, and so what `type con`, `more` and the C runtime's `read` do. + /// For an owner that reads the console as a stream of bytes. Reading again + /// starts with the next line. + pub fn end_input_at_ctrl_z(&mut self) { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { (*self.raw()).ctrl_z_ends_input = true }; + } + + /// Deliver console input to `on_read(ctx, ..)`, always from the loop. `ctx` + /// must stay valid until `read_stop`, an `Err` or `Eof` event, or the `Tty` + /// is closed or dropped. A console has no end of file but the one + /// [`end_input_at_ctrl_z`](Self::end_input_at_ctrl_z) asks for. + pub fn read_start( + &mut self, + ctx: *mut T, + on_read: unsafe fn(*mut T, ReadEvent<'_>), + ) -> sys::Result<()> { + let this = self.raw(); + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { + if (*this).flags.contains(Flags::DETACHED) || !(*this).flags.contains(Flags::READABLE) { + return Err(sys::Error::from_code(E::EBADF, Tag::read)); + } + (*this).reader = Some(ReadCallback::new(ctx, on_read)); + if (*this).flags.contains(Flags::READING) { + return Ok(()); + } + (*this).flags.insert(Flags::READING); + Inner::update_keep_alive(this); + Inner::arm(this).map_err(|err| { + (*this).flags.remove(Flags::READING); + Inner::update_keep_alive(this); + sys::Error::from_win32(err, Tag::read) + }) + } + } + + /// Stop delivering, and stop taking input: whoever reads the console next + /// (a child that inherits it) gets the next line. What the user had typed + /// of the current one comes back with the next `read_start`. + pub fn read_stop(&mut self) { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { + let this = self.raw(); + (*this).flags.remove(Flags::READING); + let line = (*this).line_op; + if !line.is_null() && (*line).in_flight { + LineOp::wake(line, &(*line).stop); + } + Inner::update_keep_alive(this); + } + } + + /// Write `data` to the screen buffer now, then call `on_write(ctx, ..)` + /// from the loop. Nothing needs to outlive this call but `ctx`. On `Err` + /// nothing was written and `on_write` is not called. + pub fn write( + &mut self, + data: &[u8], + ctx: *mut T, + on_write: unsafe fn(*mut T, sys::Result), + ) -> sys::Result<()> { + let this = self.raw(); + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { + if (*this).flags.intersects(Flags::DETACHED | Flags::READABLE) { + return Err(sys::Error::from_code(E::EBADF, Tag::write)); + } + let result = self.try_write(data); + (*this) + .written + .push_back((Callback::new(ctx, on_write), result)); + Inner::post(this); + Inner::update_keep_alive(this); + } + Ok(()) + } + + /// Write UTF-8 `data` to the screen buffer now. + pub fn try_write(&mut self, data: &[u8]) -> sys::Result { + self.write_now(data.len(), |handle, output| { + tty_output::write(handle, output, data) + }) + } + + /// Write UTF-16 `data` to the screen buffer now. The count is the length + /// of the text as UTF-8, as [`try_write`](Self::try_write) would report it. + pub fn try_write_utf16(&mut self, data: &[u16]) -> sys::Result { + self.write_now( + bun_core::strings::element_length_utf16_into_utf8(data), + |handle, output| tty_output::write_utf16(handle, output, data), + ) + } + + /// As [`try_write_utf16`](Self::try_write_utf16) for Latin-1 `data`. + pub fn try_write_latin1(&mut self, data: &[u8]) -> sys::Result { + self.write_now( + bun_core::strings::element_length_latin1_into_utf8(data), + |handle, output| tty_output::write_latin1(handle, output, data), + ) + } + + fn write_now( + &mut self, + utf8_len: usize, + write: impl FnOnce(HANDLE, &mut OutputState) -> Result<(), u32>, + ) -> sys::Result { + let this = self.raw(); + // SAFETY: `inner` is live while the owner's `Tty` is; the console lock + // inside `tty_output` serializes every writer in the process. + unsafe { + if (*this).flags.intersects(Flags::DETACHED | Flags::READABLE) { + return Err(sys::Error::from_code(E::EBADF, Tag::write)); + } + match write((*this).handle, &mut (*this).output) { + Ok(()) => Ok(utf8_len), + Err(code) => Err(sys::Error::from_win32( + Win32Error::from_u32(code), + Tag::write, + )), + } + } + } + + /// Close. Writes whose callback has not run yet still call back (dropping + /// the `Tty` instead calls nobody back). + pub fn close(self) { + let this = self.raw(); + core::mem::forget(self); + // SAFETY: `this` is live until `Inner::close` decides otherwise. + unsafe { Inner::close(this, true) }; + } +} + +impl Drop for Tty { + fn drop(&mut self) { + // SAFETY: `inner` is live until `Inner::close` decides otherwise. + unsafe { Inner::close(self.raw(), false) }; + } +} + +impl Inner { + #[inline] + fn gone(&self) -> bool { + self.flags.intersects(Flags::CLOSING | Flags::DETACHED) + } + + /// # Safety + /// `this` is live. Must run on the loop's thread. + unsafe fn update_keep_alive(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + let inner = &mut *this; + if inner.flags.contains(Flags::DETACHED) { + return; + } + let wanted = !inner.flags.contains(Flags::CLOSING) + && (inner.flags.contains(Flags::REFD | Flags::READING) + || !inner.written.is_empty()); + if wanted == inner.flags.contains(Flags::KEEPING_ALIVE) { + return; + } + inner.flags.set(Flags::KEEPING_ALIVE, wanted); + if wanted { + (*inner.link.loop_).add_active(1); + } else { + (*inner.link.loop_).sub_active(1); + } + } + } + + /// Start whichever read the console's mode calls for, unless one is out. + /// + /// # Safety + /// `this` is live, reading and not closing. + unsafe fn arm(this: *mut Inner) -> Result<(), Win32Error> { + // SAFETY: caller contract; the ops are owned by `this` and freed only + // when no packet or thread refers to them. + unsafe { + let loop_ = (*this).link.loop_; + // One read for every decision below: a change by another thread in + // between would arm a raw wait without its `LINE_MODE` wait. + let mode = current_mode(); + if mode != Mode::Normal { + WaitOp::start( + &raw mut (*this).mode_op, + this, + LINE_MODE.load(Ordering::Acquire), + WaitOp::line_mode_set, + )?; + } + let line_out = !(*this).line_op.is_null() && (*(*this).line_op).in_flight; + let raw_out = !(*this).raw_op.is_null() && (*(*this).raw_op).armed; + if line_out || raw_out { + return Ok(()); + } + + let line = (*this).line_op; + if mode != Mode::Normal && !line.is_null() && (*line).carry > 0 { + // Typed in line mode and never entered: a raw reader gets + // those keys as they are. + let carried = &(&(*line).chars)[..(*line).carry]; + (*this) + .held + .extend_from_slice(&bun_core::strings::to_utf8_alloc_with_type(carried)); + (*line).carry = 0; + } + + if !(*this).held.is_empty() || (*this).held_error.is_some() || (*this).held_end { + // From the loop, not from inside `read_start`. + (*this).held_due = true; + Self::post(this); + return Ok(()); + } + + if mode == Mode::Normal { + if (*this).line_op.is_null() { + (*this).line_op = bun_core::heap::into_raw(Box::new(LineOp { + op: Op::new(LineOp::complete), + tty: this, + handle: (*this).handle, + port: None, + in_flight: false, + cancel: AtomicBool::new(false), + stop: AtomicBool::new(false), + chars: Vec::new(), + carry: 0, + bytes: Vec::new(), + error: 0, + woken: false, + finisher: AtomicU8::new(LINE_REQUESTED), + own: None, + })); + } + let op = (*this).line_op; + let Some(port) = super::port_for(loop_) else { + return Err(win::last_error()); + }; + (*op).port = Some(port); + (*op).error = 0; + (*op).woken = false; + (*op).bytes.clear(); + (*op).cancel.store(false, Ordering::Release); + (*op).stop.store(false, Ordering::Release); + (*op).finisher.store(LINE_REQUESTED, Ordering::Release); + if !super::queue_blocking_work(LineOp::read_thread, op.cast()) { + let err = win::last_error(); + (*op).port = None; + return Err(err); + } + super::op_submitted(loop_); + (*op).in_flight = true; + (*this).pending += 1; + } else { + WaitOp::start( + &raw mut (*this).raw_op, + this, + (*this).handle, + WaitOp::input_ready, + )?; + } + Ok(()) + } + } + + /// Have [`PostedOp::complete`] run from the loop's next tick. + /// + /// # Safety + /// `this` is live. Must run on the loop's thread. + unsafe fn post(this: *mut Inner) { + // SAFETY: caller contract; `posted` is part of `this`, which `pending` + // keeps allocated until the packet is dequeued. + unsafe { + if (*this).posted.out { + return; + } + (*this).posted.out = true; + super::complete_from_loop((*this).link.loop_, &raw mut (*this).posted.op); + (*this).pending += 1; + } + } + + /// Every call out to the owner goes through here: the owner may close or + /// drop its `Tty` from inside one. Returns whether `this` is still open; + /// when it is not, it may be freed already. + /// + /// # Safety + /// `this` is live. + #[must_use] + unsafe fn with_owner(this: *mut Inner, call: impl FnOnce()) -> bool { + // SAFETY: caller contract; `pins` keeps `this` allocated across `call`. + unsafe { + (*this).pins += 1; + call(); + (*this).pins -= 1; + if (*this).gone() { + Self::maybe_finish(this); + return false; + } + } + true + } + + /// # Safety + /// `this` is live, inside [`with_owner`](Self::with_owner), and reading. + unsafe fn deliver(this: *mut Inner, event: ReadEvent<'_>) { + // SAFETY: caller contract. + unsafe { + if let Some(reader) = (*this).reader { + reader.invoke(event); + } + } + } + + /// After a read finished: report an arming failure like a read error. + /// `this` may be freed when this returns. + /// + /// # Safety + /// `this` is live and not closing. + unsafe fn rearm(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + if !(*this).flags.contains(Flags::READING) { + return; + } + if let Err(err) = Self::arm(this) { + (*this).flags.remove(Flags::READING); + Self::update_keep_alive(this); + let _ = Self::with_owner(this, || { + Self::deliver(this, ReadEvent::Err(sys::Error::from_win32(err, Tag::read))); + }); + } + } + } + + /// # Safety + /// `this` is live; the owner's `Tty` is consumed or being dropped. + unsafe fn close(this: *mut Inner, report_writes: bool) { + // SAFETY: caller contract. + unsafe { + (*this).flags.insert(Flags::OWNER_GONE); + if !report_writes { + (*this).flags.insert(Flags::SILENT); + } + (*this).reader = None; + if !(*this).gone() { + Self::cancel_reads(this); + (*this).flags.insert(Flags::CLOSING); + (*this).flags.remove(Flags::READING); + Self::update_keep_alive(this); + } + Self::maybe_finish(this); + } + } + + /// [`Link::shut`]: the loop is about to be freed. + unsafe fn shut(link: *mut Link) { + let this = link.cast::(); + // SAFETY: `link` is the first field of a listed (live) `Inner`. + unsafe { + if !(*this).gone() { + Self::cancel_reads(this); + } + // When a line read returns is the console's to say: one of its + // popups keeps the key that ends it from it. The loop does not + // wait for that. + let line = (*this).line_op; + if !line.is_null() && (*line).in_flight { + (*line).own = (*this).close_fd.take(); + if (*line) + .finisher + .compare_exchange( + LINE_REQUESTED, + LINE_ORPHANED, + Ordering::AcqRel, + Ordering::Acquire, + ) + .is_ok() + { + (*this).line_op = ptr::null_mut(); + (*this).pending -= 1; + super::op_abandoned((*this).link.loop_); + } else { + (*this).close_fd = (*line).own.take(); + } + } + (*this).flags.insert(Flags::CLOSING | Flags::SILENT); + (*this).flags.remove(Flags::READING); + (*this).reader = None; + Self::update_keep_alive(this); + (*this).flags.insert(Flags::DETACHED); + Link::remove(link); + Self::maybe_finish(this); + } + } + + /// # Safety + /// `this` is live and neither closing nor detached. + unsafe fn cancel_reads(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + WaitOp::stop((*this).raw_op); + WaitOp::stop((*this).mode_op); + let line = (*this).line_op; + if !line.is_null() && (*line).in_flight { + // The handle stays open until the read has returned. + LineOp::wake(line, &(*line).cancel); + } + } + } + + /// # Safety + /// `this` is live. + unsafe fn maybe_finish(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + if !(*this).flags.contains(Flags::CLOSING) || (*this).pending > 0 || (*this).pins > 0 { + return; + } + if let Some(fd) = (*this).close_fd.take() { + fd.close(); + } + (*this).handle = INVALID_HANDLE_VALUE; + if !(*this).flags.contains(Flags::OWNER_GONE) { + return; + } + if !(*this).flags.contains(Flags::DETACHED) { + Self::update_keep_alive(this); + Link::remove(this.cast()); + } + for op in [(*this).raw_op, (*this).mode_op] { + if !op.is_null() { + let op = bun_core::heap::take(op); + iocp::us_iocp_wait_free(op.wait); + } + } + if !(*this).line_op.is_null() { + drop(bun_core::heap::take((*this).line_op)); + } + drop(bun_core::heap::take(this)); + } + } +} + +impl WaitOp { + /// Arm `*slot`, made on first use, on `handle`; nothing to do while it is + /// armed. + /// + /// # Safety + /// `tty` is live and owns `slot`. Must run on the loop's thread. + unsafe fn start( + slot: *mut *mut WaitOp, + tty: *mut Inner, + handle: HANDLE, + complete: CompleteFn, + ) -> Result<(), Win32Error> { + // SAFETY: caller contract; the op is freed with `tty`, which `pending` + // keeps until the wait has fired or was taken back. + unsafe { + let loop_ = (*tty).link.loop_; + if (*slot).is_null() { + let wait = iocp::us_iocp_wait_create(loop_); + if wait.is_null() { + return Err(Win32Error::NOT_ENOUGH_MEMORY); + } + *slot = bun_core::heap::into_raw(Box::new(WaitOp { + op: Op::new(complete), + tty, + wait, + armed: false, + })); + } + let op = *slot; + if (*op).armed { + return Ok(()); + } + if iocp::us_iocp_wait_start((*op).wait, handle, &raw mut (*op).op) != 0 { + return Err(win::last_error()); + } + (*op).armed = true; + (*tty).pending += 1; + Ok(()) + } + } + + /// Take the wait back, unless its packet is already on the way. + /// + /// # Safety + /// `op` is null or a live op of a live tty. Must run on the loop's thread. + unsafe fn stop(op: *mut WaitOp) { + // SAFETY: caller contract. + unsafe { + if !op.is_null() && (*op).armed && iocp::us_iocp_wait_stop((*op).wait) != 0 { + // Removed before it fired: no packet will come for it. + (*op).armed = false; + let tty = (*op).tty; + (*tty).pending -= 1; + } + } + } + + /// The wait's packet was dequeued. The tty, unless that was the last thing + /// a closed one waited for. + /// + /// # Safety + /// `op` is the first field of a `WaitOp` whose wait fired. + unsafe fn fired(op: *mut Op) -> Option<*mut Inner> { + let wait = op.cast::(); + // SAFETY: caller contract; the tty outlives its packets (`pending`). + unsafe { + (*wait).armed = false; + let this = (*wait).tty; + (*this).pending -= 1; + if (*this).gone() { + Inner::maybe_finish(this); + return None; + } + Some(this) + } + } + + /// [`LINE_MODE`] was set: the console's input is not this reader's to wait + /// on any more. + unsafe extern "C" fn line_mode_set(_loop: *mut Loop, op: *mut Op) { + // SAFETY: `op` is the first field of the `WaitOp` whose wait fired. + unsafe { + let Some(this) = Self::fired(op) else { + return; + }; + if current_mode() == Mode::Normal { + Self::stop((*this).raw_op); + } + Inner::rearm(this); + } + } + + /// The console's input queue holds records. + unsafe extern "C" fn input_ready(_loop: *mut Loop, op: *mut Op) { + // SAFETY: `op` is the first field of the `WaitOp` whose wait fired. + unsafe { + let Some(this) = Self::fired(op) else { + return; + }; + // Not reading: the records stay queued (the handle stays + // signalled), and `read_start` arms again. + if !(*this).flags.contains(Flags::READING) { + return; + } + if current_mode() == Mode::Normal { + Inner::rearm(this); + return; + } + + (*this).raw_buf.clear(); + let result = + tty_input::read_raw((*this).handle, &mut (*this).raw_state, &mut (*this).raw_buf); + if matches!(&result, Ok(read) if read.wake_key) { + // If a line read is waiting for that key, this read got to + // the queue ahead of it. + let _lock = LINE_LOCK.lock_guard(); + if WAKE_TYPED.swap(false, Ordering::AcqRel) { + type_wake_key(); + } + } + if matches!(&result, Ok(read) if read.resized) { + let listener = ON_RESIZE.load(Ordering::Acquire); + if !listener.is_null() { + // SAFETY: non-null `ON_RESIZE` is a `fn()` stored by + // `set_resize_listener`. + core::mem::transmute::<*mut (), fn()>(listener)(); + } + } + let open = Inner::with_owner(this, || { + if !(*this).raw_buf.is_empty() && (*this).flags.contains(Flags::READING) { + Inner::deliver(this, ReadEvent::Data(&mut (*this).raw_buf)); + } + if let Err(code) = result + && (*this).flags.contains(Flags::READING) + { + (*this).flags.remove(Flags::READING); + Inner::deliver( + this, + ReadEvent::Err(sys::Error::from_win32( + Win32Error::from_u32(code), + Tag::read, + )), + ); + } + }); + if !open { + return; + } + Inner::update_keep_alive(this); + Inner::rearm(this); + } + } +} + +impl LineOp { + /// The console cannot abandon a line read; it can be made to return. + /// + /// # Safety + /// `line` is live with its read in flight; `why` is its `cancel` or `stop`. + unsafe fn wake(line: *mut LineOp, why: &AtomicBool) { + let _lock = LINE_LOCK.lock_guard(); + why.store(true, Ordering::Release); + if LINE_READER.load(Ordering::Acquire) == line { + type_wake_key(); + } else { + // Not in the call: its helper looks at `why` before it enters. + LINE_TURN.broadcast(); + } + } + + /// Wait until no other `ReadConsoleW` of this process is out, then claim + /// the console for `op`'s. `false` if the read stopped being wanted first. + /// + /// # Safety + /// `op` is live; called from its helper thread. + unsafe fn enter_call(op: *mut LineOp) -> bool { + let _lock = LINE_LOCK.lock_guard(); + loop { + // SAFETY: caller contract. + let unwanted = unsafe { + (*op).cancel.load(Ordering::Acquire) || (*op).stop.load(Ordering::Acquire) + }; + if unwanted || current_mode() != Mode::Normal { + return false; + } + if LINE_READER.load(Ordering::Acquire).is_null() { + LINE_READER.store(op, Ordering::Release); + WAKE_TYPED.store(false, Ordering::Release); + return true; + } + LINE_TURN.wait(&LINE_LOCK); + } + } + + /// The call returned. `true` if somebody wants this read over. + /// + /// # Safety + /// `op` is live and is [`LINE_READER`]; called from its helper thread. + unsafe fn leave_call(op: *mut LineOp) -> bool { + let _lock = LINE_LOCK.lock_guard(); + LINE_READER.store(ptr::null_mut(), Ordering::Release); + let pending = PENDING_FLAGS.swap(u64::MAX, Ordering::AcqRel); + if pending != u64::MAX { + // SAFETY: caller contract; `handle` is this console's input. The + // mode was accepted as far as `set_console_mode` could tell. + let _ = unsafe { apply_flags((*op).handle, (pending >> 32) as u32, pending as u32) }; + } + LINE_TURN.broadcast(); + // SAFETY: caller contract. + unsafe { + (*op).cancel.load(Ordering::Acquire) + || (*op).stop.load(Ordering::Acquire) + || current_mode() != Mode::Normal + } + } + + unsafe extern "system" fn read_thread(context: *mut c_void) -> u32 { + let op = context.cast::(); + // SAFETY: `context` is the `LineOp` submitted by `arm`, which stays + // allocated until the packet posted below is dequeued; the loop thread + // touches only `cancel` and `stop` (atomic) in the meantime. + unsafe { + let chars = &mut (*op).chars; + let mut kept = (*op).carry.min(LINE_READ_CHARS - 1); + chars.truncate(kept); + chars.reserve(LINE_READ_CHARS); + loop { + if !Self::enter_call(op) { + (*op).woken = true; + break; + } + let mut control = win::CONSOLE_READCONSOLE_CONTROL { + nLength: size_of::() as u32, + nInitialChars: kept as u32, + dwCtrlWakeupMask: 1 << WAKE_CHAR, + dwControlKeyState: 0, + }; + let mut read: u32 = 0; + let error = if win::ReadConsoleW( + (*op).handle, + chars.as_mut_ptr().cast(), + LINE_READ_CHARS as u32, + &raw mut read, + &raw mut control, + ) == 0 + { + u32::from(win::last_error().int()) + } else { + 0 + }; + let unwanted = Self::leave_call(op); + if error != 0 { + (*op).error = error; + break; + } + // The count covers the characters carried in through + // `nInitialChars` as well. + let total = (read as usize).min(LINE_READ_CHARS); + chars.set_len(total); + let Some(wake_at) = bun_core::strings::index_of_any16(chars, &[WAKE_CHAR]) else { + break; + }; + // Entered with the cursor inside the line, the key is not + // last: only what is in front of it is the line so far. + kept = wake_at; + if unwanted { + (*op).woken = true; + break; + } + // The user entered it: carry on editing the same line. + if kept + 1 >= LINE_READ_CHARS { + chars.set_len(kept); + break; + } + } + if (*op).woken { + chars.truncate(kept); + (*op).carry = kept; + } else { + (*op).carry = 0; + if (*op).error == 0 { + (*op).bytes = bun_core::strings::to_utf8_alloc_with_type(chars); + } + } + if (*op) + .finisher + .compare_exchange( + LINE_REQUESTED, + LINE_POSTED, + Ordering::AcqRel, + Ordering::Acquire, + ) + .is_err() + { + if let Some(fd) = bun_core::heap::take(op).own { + fd.close(); + } + return 0; + } + if let Some(port) = (*op).port.take() { + port.post(&raw mut (*op).op); + } + } + 0 + } + + unsafe extern "C" fn complete(_loop: *mut Loop, op: *mut Op) { + let line = op.cast::(); + // SAFETY: `op` is the first field of the `LineOp` whose helper thread + // posted this packet and is done with it; the tty outlives its + // packets (`pending`). + unsafe { + (*line).in_flight = false; + let this = (*line).tty; + (*this).pending -= 1; + if (*this).gone() { + Inner::maybe_finish(this); + return; + } + if (*line).error != 0 { + (*this).held_error = Some(Win32Error::from_u32((*line).error)); + } else if !(*line).woken { + // The console ends a line read at Ctrl+C or Ctrl+Break with no + // characters, which a `ReadFile` reports as it does the end. + if (*this).ctrl_z_ends_input + && (*line).bytes.first().is_none_or(|&first| first == 0x1A) + { + (*line).bytes.clear(); + (*this).held_end = true; + } else { + (*this).held.append(&mut (*line).bytes); + } + } + Inner::flush_held(this); + } + } +} + +impl Inner { + /// Hand over what line reads produced, if the owner is reading, and keep + /// reading. + /// + /// # Safety + /// `this` is live and not closing. + unsafe fn flush_held(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + if !(*this).flags.contains(Flags::READING) { + return; + } + let open = Self::with_owner(this, || { + if !(*this).held.is_empty() { + let mut data = core::mem::take(&mut (*this).held); + Self::deliver(this, ReadEvent::Data(&mut data)); + } + if let Some(err) = (*this).held_error.take() + && (*this).flags.contains(Flags::READING) + { + (*this).flags.remove(Flags::READING); + Self::deliver(this, ReadEvent::Err(sys::Error::from_win32(err, Tag::read))); + } + if (*this).held_end && (*this).flags.contains(Flags::READING) { + (*this).held_end = false; + (*this).flags.remove(Flags::READING); + Self::deliver(this, ReadEvent::Eof); + } + }); + if !open { + return; + } + Self::update_keep_alive(this); + Self::rearm(this); + } + } +} + +impl PostedOp { + unsafe extern "C" fn complete(_loop: *mut Loop, op: *mut Op) { + // SAFETY: `op` is the first field of the `PostedOp` of a tty, which + // outlives its packets (`pending`). + unsafe { + let this = (*op.cast::()).tty; + (*this).posted.out = false; + (*this).pending -= 1; + + // A write made from a callback is the next tick's. + let due = (*this).written.len(); + let held_due = core::mem::take(&mut (*this).held_due); + let open = Inner::with_owner(this, || { + for _ in 0..due { + let Some((callback, result)) = (*this).written.pop_front() else { + break; + }; + if !(*this).flags.contains(Flags::SILENT) { + callback.invoke(result); + } + } + }); + if !open { + return; + } + Inner::update_keep_alive(this); + if held_due { + Inner::flush_held(this); + } + } + } +} diff --git a/src/io/windows/tty_input.rs b/src/io/windows/tty_input.rs new file mode 100644 index 000000000000..8ecf5953dfcf --- /dev/null +++ b/src/io/windows/tty_input.rs @@ -0,0 +1,287 @@ +//! Raw-mode console input: `INPUT_RECORD`s from `ReadConsoleInputW` translated to the byte +//! stream a VT terminal would send. The key mappings and modifier rules are libuv's +//! (`uv_process_tty_read_raw_req` / `get_vt100_fn_key` in `src/win/tty.c`). + +use core::mem::MaybeUninit; + +use bun_windows_sys::{DWORD, GetLastError, HANDLE, INPUT_RECORD, KEY_EVENT_RECORD, WORD}; + +use super::sys::GetNumberOfConsoleInputEvents; +use super::tty::is_wake_key; + +/// Translation state that must survive between calls: a pending UTF-16 high surrogate. +pub(crate) struct RawInputState { + /// 0 when none is pending. + high_surrogate: u16, +} + +impl RawInputState { + pub(crate) const fn new() -> Self { + Self { high_surrogate: 0 } + } +} + +#[derive(Default)] +pub(crate) struct RawInputResult { + /// A `WINDOW_BUFFER_SIZE_EVENT` record was seen. Its `dwSize` is the screen buffer, not + /// the window, in a classic console, so the caller re-reads the real size. + pub resized: bool, + /// The key that ends a line read was in the queue. It is not input. + pub wake_key: bool, +} + +const BATCH_RECORDS: usize = 128; + +/// Drains the console input queue without blocking and appends the translated bytes to `out`. +/// Must be called on the thread that owns `state`. On `Err(GetLastError())`, `out` keeps the +/// bytes translated before the failure. +pub(crate) fn read_raw( + handle: HANDLE, + state: &mut RawInputState, + out: &mut Vec, +) -> Result { + let mut available: DWORD = 0; + // SAFETY: `available` is a valid out-pointer; a bad `handle` makes the call fail. + if unsafe { GetNumberOfConsoleInputEvents(handle, &raw mut available) } == 0 { + return Err(GetLastError()); + } + + let mut result = RawInputResult::default(); + let mut records = [const { MaybeUninit::::uninit() }; BATCH_RECORDS]; + while available > 0 { + // `ReadConsoleInputW` waits for the first record only, so asking for no more than + // what is queued returns immediately. + let want = available.min(BATCH_RECORDS as DWORD); + let mut read: DWORD = 0; + // SAFETY: `records` has room for `want` records and `read` is a valid out-pointer. + let ok = unsafe { + ffi::ReadConsoleInputW(handle, records.as_mut_ptr().cast(), want, &raw mut read) + }; + if ok == 0 { + return Err(GetLastError()); + } + if read == 0 { + break; + } + // SAFETY: the call initialized the first `read` (<= `want`) records. + let batch = unsafe { core::slice::from_raw_parts(records.as_ptr().cast(), read as usize) }; + translate_all(state, batch, out, &mut result); + available = available.saturating_sub(read); + } + Ok(result) +} + +fn translate_all( + state: &mut RawInputState, + records: &[INPUT_RECORD], + out: &mut Vec, + result: &mut RawInputResult, +) { + for record in records { + match record.EventType { + ffi::WINDOW_BUFFER_SIZE_EVENT => result.resized = true, + ffi::KEY_EVENT => { + // SAFETY: `EventType == KEY_EVENT` selects the `KeyEvent` member. + let key = unsafe { &record.Event.KeyEvent }; + if is_wake_key(key) { + result.wake_key = true; + } else { + translate_key(state, key, out); + } + } + _ => {} + } + } +} + +fn translate_key(state: &mut RawInputState, key: &KEY_EVENT_RECORD, out: &mut Vec) { + // SAFETY: both members of the union are plain integers; `ReadConsoleInputW` fills the + // UTF-16 one. + let unit = unsafe { key.uChar.UnicodeChar }; + let key_down = key.bKeyDown != 0; + let vk = key.wVirtualKeyCode; + let modifiers = key.dwControlKeyState; + let alt = modifiers & (ffi::LEFT_ALT_PRESSED | ffi::RIGHT_ALT_PRESSED) != 0; + let ctrl = modifiers & (ffi::LEFT_CTRL_PRESSED | ffi::RIGHT_CTRL_PRESSED) != 0; + + // A character composed with Alt+numpad is delivered on the key-up of Alt, and so is a + // non-BMP character that arrives through a pseudoconsole. Every other key-up is dropped. + if !key_down && (vk != ffi::VK_MENU || unit == 0) { + return; + } + + // Numpad keys pressed while left Alt is held are the digits of an Alt+numpad composition + // (the navigation cluster sets ENHANCED_KEY; the numpad with NumLock off does not). + if modifiers & ffi::LEFT_ALT_PRESSED != 0 + && modifiers & ffi::ENHANCED_KEY == 0 + && matches!( + vk, + ffi::VK_INSERT + | ffi::VK_END + | ffi::VK_DOWN + | ffi::VK_NEXT + | ffi::VK_LEFT + | ffi::VK_CLEAR + | ffi::VK_RIGHT + | ffi::VK_HOME + | ffi::VK_UP + | ffi::VK_PRIOR + | ffi::VK_NUMPAD0..=ffi::VK_NUMPAD9 + ) + { + return; + } + + let start = out.len(); + if unit != 0 { + if (0xD800..0xDC00).contains(&unit) { + state.high_surrogate = unit; + return; + } + // AltGr reports as Ctrl+Alt and must not get the prefix; neither must the key-up of + // Alt that ends a composition. + if alt && !ctrl && key_down { + out.push(ESC); + } + let high = core::mem::take(&mut state.high_surrogate); + if high == 0 { + push_wtf8(out, u32::from(unit)); + } else if (0xDC00..0xE000).contains(&unit) { + push_wtf8( + out, + 0x10000 + ((u32::from(high) - 0xD800) << 10) + (u32::from(unit) - 0xDC00), + ); + } else { + push_wtf8(out, u32::from(high)); + push_wtf8(out, u32::from(unit)); + } + } else { + let Some(sequence) = vt100_fn_key(vk, modifiers & ffi::SHIFT_PRESSED != 0, ctrl) else { + return; + }; + if alt { + out.push(ESC); + } + out.extend_from_slice(sequence); + } + + let end = out.len(); + for _ in 1..key.wRepeatCount { + out.extend_from_within(start..end); + } +} + +const ESC: u8 = 0x1B; + +/// Generalized UTF-8: a surrogate code point is encoded as its own three-byte sequence. +fn push_wtf8(out: &mut Vec, code_point: u32) { + let mut bytes = [0u8; 4]; + let len = bun_core::strings::encode_wtf8_rune(&mut bytes, code_point); + out.extend_from_slice(&bytes[..len]); +} + +/// The same mappings as Cygwin: unmodified keypad keys follow the Linux console, modifiers +/// follow xterm, F1-F12 and Shift+F1-F10 follow the Linux console, F6-F12 with and without +/// modifiers follow rxvt. +fn vt100_fn_key(vk: WORD, shift: bool, ctrl: bool) -> Option<&'static [u8]> { + let column = usize::from(shift) | usize::from(ctrl) << 1; + macro_rules! row { + ($normal:literal, $shift:literal, $ctrl:literal, $shift_ctrl:literal) => { + [ + concat!("\x1b", $normal), + concat!("\x1b", $shift), + concat!("\x1b", $ctrl), + concat!("\x1b", $shift_ctrl), + ][column] + }; + } + let sequence: &'static str = match vk { + ffi::VK_INSERT | ffi::VK_NUMPAD0 => row!("[2~", "[2;2~", "[2;5~", "[2;6~"), + ffi::VK_END | ffi::VK_NUMPAD1 => row!("[4~", "[4;2~", "[4;5~", "[4;6~"), + ffi::VK_DOWN | ffi::VK_NUMPAD2 => row!("[B", "[1;2B", "[1;5B", "[1;6B"), + ffi::VK_NEXT | ffi::VK_NUMPAD3 => row!("[6~", "[6;2~", "[6;5~", "[6;6~"), + ffi::VK_LEFT | ffi::VK_NUMPAD4 => row!("[D", "[1;2D", "[1;5D", "[1;6D"), + ffi::VK_CLEAR | ffi::VK_NUMPAD5 => row!("[G", "[1;2G", "[1;5G", "[1;6G"), + ffi::VK_RIGHT | ffi::VK_NUMPAD6 => row!("[C", "[1;2C", "[1;5C", "[1;6C"), + ffi::VK_UP | ffi::VK_NUMPAD7 => row!("[A", "[1;2A", "[1;5A", "[1;6A"), + ffi::VK_HOME | ffi::VK_NUMPAD8 => row!("[1~", "[1;2~", "[1;5~", "[1;6~"), + ffi::VK_PRIOR | ffi::VK_NUMPAD9 => row!("[5~", "[5;2~", "[5;5~", "[5;6~"), + ffi::VK_DELETE | ffi::VK_DECIMAL => row!("[3~", "[3;2~", "[3;5~", "[3;6~"), + ffi::VK_F1 => row!("[[A", "[23~", "[11^", "[23^"), + ffi::VK_F2 => row!("[[B", "[24~", "[12^", "[24^"), + ffi::VK_F3 => row!("[[C", "[25~", "[13^", "[25^"), + ffi::VK_F4 => row!("[[D", "[26~", "[14^", "[26^"), + ffi::VK_F5 => row!("[[E", "[28~", "[15^", "[28^"), + ffi::VK_F6 => row!("[17~", "[29~", "[17^", "[29^"), + ffi::VK_F7 => row!("[18~", "[31~", "[18^", "[31^"), + ffi::VK_F8 => row!("[19~", "[32~", "[19^", "[32^"), + ffi::VK_F9 => row!("[20~", "[33~", "[20^", "[33^"), + ffi::VK_F10 => row!("[21~", "[34~", "[21^", "[34^"), + ffi::VK_F11 => row!("[23~", "[23$", "[23^", "[23@"), + ffi::VK_F12 => row!("[24~", "[24$", "[24^", "[24@"), + _ => return None, + }; + Some(sequence.as_bytes()) +} + +mod ffi { + use bun_windows_sys::{BOOL, DWORD, HANDLE, INPUT_RECORD, WORD}; + + pub(super) use bun_windows_sys::{KEY_EVENT, LEFT_CTRL_PRESSED}; + + // `INPUT_RECORD::EventType` + pub(super) const WINDOW_BUFFER_SIZE_EVENT: WORD = 0x0004; + + // `KEY_EVENT_RECORD::dwControlKeyState` + pub(super) const RIGHT_ALT_PRESSED: DWORD = 0x0001; + pub(super) const LEFT_ALT_PRESSED: DWORD = 0x0002; + pub(super) const RIGHT_CTRL_PRESSED: DWORD = 0x0004; + pub(super) const SHIFT_PRESSED: DWORD = 0x0010; + pub(super) const ENHANCED_KEY: DWORD = 0x0100; + + pub(super) const VK_CLEAR: WORD = 0x0C; + pub(super) const VK_MENU: WORD = 0x12; + pub(super) const VK_PRIOR: WORD = 0x21; + pub(super) const VK_NEXT: WORD = 0x22; + pub(super) const VK_END: WORD = 0x23; + pub(super) const VK_HOME: WORD = 0x24; + pub(super) const VK_LEFT: WORD = 0x25; + pub(super) const VK_UP: WORD = 0x26; + pub(super) const VK_RIGHT: WORD = 0x27; + pub(super) const VK_DOWN: WORD = 0x28; + pub(super) const VK_INSERT: WORD = 0x2D; + pub(super) const VK_DELETE: WORD = 0x2E; + pub(super) const VK_NUMPAD0: WORD = 0x60; + pub(super) const VK_NUMPAD1: WORD = 0x61; + pub(super) const VK_NUMPAD2: WORD = 0x62; + pub(super) const VK_NUMPAD3: WORD = 0x63; + pub(super) const VK_NUMPAD4: WORD = 0x64; + pub(super) const VK_NUMPAD5: WORD = 0x65; + pub(super) const VK_NUMPAD6: WORD = 0x66; + pub(super) const VK_NUMPAD7: WORD = 0x67; + pub(super) const VK_NUMPAD8: WORD = 0x68; + pub(super) const VK_NUMPAD9: WORD = 0x69; + pub(super) const VK_DECIMAL: WORD = 0x6E; + pub(super) const VK_F1: WORD = 0x70; + pub(super) const VK_F2: WORD = 0x71; + pub(super) const VK_F3: WORD = 0x72; + pub(super) const VK_F4: WORD = 0x73; + pub(super) const VK_F5: WORD = 0x74; + pub(super) const VK_F6: WORD = 0x75; + pub(super) const VK_F7: WORD = 0x76; + pub(super) const VK_F8: WORD = 0x77; + pub(super) const VK_F9: WORD = 0x78; + pub(super) const VK_F10: WORD = 0x79; + pub(super) const VK_F11: WORD = 0x7A; + pub(super) const VK_F12: WORD = 0x7B; + + #[link(name = "kernel32")] + unsafe extern "system" { + pub(super) fn ReadConsoleInputW( + hConsoleInput: HANDLE, + lpBuffer: *mut INPUT_RECORD, + nLength: DWORD, + lpNumberOfEventsRead: *mut DWORD, + ) -> BOOL; + } +} diff --git a/src/io/windows/tty_output.rs b/src/io/windows/tty_output.rs new file mode 100644 index 000000000000..53540bf9870b --- /dev/null +++ b/src/io/windows/tty_output.rs @@ -0,0 +1,1177 @@ +//! Console output. Text (UTF-8, UTF-16 or Latin-1) is written with `WriteConsoleW`, with `\n` +//! converted to `\r\n`. On a console that cannot do virtual-terminal processing (pre-1511 +//! Windows 10, "legacy console mode"), ANSI escape sequences are interpreted here and applied +//! through the classic console API instead. + +use core::ffi::c_void; +use core::marker::PhantomData; +use core::mem::MaybeUninit; +use core::ptr; + +use bun_sys::windows::ENABLE_VIRTUAL_TERMINAL_PROCESSING; +use bun_threading::{Guarded, GuardedLock, Mutex}; +use bun_windows_sys::kernel32::{ + FillConsoleOutputAttribute, FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo, + SetConsoleCursorPosition, +}; +use bun_windows_sys::{ + CONSOLE_SCREEN_BUFFER_INFO, COORD, DWORD, GetConsoleMode, GetLastError, HANDLE, SetConsoleMode, + WORD, Win32Error, +}; + +use ffi::{ + BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_INTENSITY, BACKGROUND_RED, + COMMON_LVB_REVERSE_VIDEO, CONSOLE_CURSOR_INFO, FOREGROUND_BLUE, FOREGROUND_GREEN, + FOREGROUND_INTENSITY, FOREGROUND_RED, +}; + +/// `WriteConsoleW` is given at most this many UTF-16 units per call. +const MAX_CONSOLE_CHARS: usize = 8192; +const MAX_CSI_ARGS: usize = 4; +const REPLACEMENT_CHARACTER: u32 = 0xFFFD; +const CURSOR_SIZE_SMALL: DWORD = 25; +const CURSOR_SIZE_LARGE: DWORD = 100; +const ERROR_SUCCESS: DWORD = Win32Error::SUCCESS.0 as DWORD; +const ERROR_INVALID_PARAMETER: DWORD = Win32Error::INVALID_PARAMETER.0 as DWORD; + +#[derive(Clone, Copy, PartialEq, Eq)] +enum VtermState { + Supported, + Unsupported, +} + +/// One coordinate of a position on the screen. +#[derive(Clone, Copy)] +enum Offset { + FromCursor(i32), + /// From the top-left of the virtual window. + FromOrigin(i32), +} +use Offset::{FromCursor, FromOrigin}; + +#[derive(Clone, Copy)] +enum AnsiState { + Normal, + EscapeSeen, + /// After `ESC [` or 0x9B. `in_arg`: the last character was a digit of the current argument. + /// `extension`: the sequence started with `?`. + Csi { + in_arg: bool, + extension: bool, + }, + /// Inside PM/APC/DCS/OSC, which run until BEL or `ESC \`. + StControl { + escape_seen: bool, + }, + /// Inside a `"`-quoted string of such a control string, where BEL and `ESC \` do not terminate. + StString { + backslash_seen: bool, + }, + /// Swallowing the rest of an unsupported CSI sequence, up to its final byte. + Ignore, + /// After `ESC [ args SP`, waiting for the final byte. + Decscusr, +} + +pub(crate) struct OutputState { + utf8_bytes_left: u8, + utf8_codepoint: u32, + /// The least a sequence of this length is the shortest form of. + utf8_least: u32, + /// `\n` or `\r` if that was the last character written, else 0. + previous_eol: u8, + ansi_state: AnsiState, + csi_argc: usize, + csi_argv: [u16; MAX_CSI_ARGS], + /// Column, and row relative to the virtual window. + saved_position: Option, + /// The two intensity bits only. + saved_attributes: Option, +} + +impl OutputState { + pub(crate) const fn new() -> Self { + Self { + utf8_bytes_left: 0, + utf8_codepoint: 0, + utf8_least: 0, + previous_eol: 0, + ansi_state: AnsiState::Normal, + csi_argc: 0, + csi_argv: [0; MAX_CSI_ARGS], + saved_position: None, + saved_attributes: None, + } + } + + fn begin_csi(&mut self) { + self.ansi_state = AnsiState::Csi { + in_arg: false, + extension: false, + }; + self.csi_argc = 0; + } + + fn first_arg_or(&self, default: u16) -> u16 { + if self.csi_argc > 0 { + self.csi_argv[0] + } else { + default + } + } + + /// A 1-based coordinate argument as a 0-based one; missing and 0 both mean the first cell. + fn coordinate_arg(&self, index: usize) -> i32 { + if index < self.csi_argc && self.csi_argv[index] != 0 { + i32::from(self.csi_argv[index]) - 1 + } else { + 0 + } + } +} + +/// State of the one console this process is attached to, shared by every output stream. +/// +/// Console cursor addressing is relative to the whole screen buffer, scrollback included, so +/// absolute cursor movement would overwrite history. Movement is confined to a virtual window +/// instead: as wide as the screen buffer, as tall as the visible window, its top at the row the +/// cursor was on when the first output handle was initialized (or as far down as fits). When +/// output scrolls past its bottom the window is dragged down, never resized. +struct Console { + virtual_offset: i32, + virtual_height: i32, + virtual_width: i32, + default_text_attributes: WORD, + /// ANSI colour numbers: red = 1, green = 2, blue = 4. + default_fg_color: u8, + default_bg_color: u8, + default_fg_bright: bool, + default_bg_bright: bool, + default_inverse: bool, + default_cursor_info: CONSOLE_CURSOR_INFO, + need_check_vterm_state: bool, + vterm_state: VtermState, + style_captured: bool, +} + +static CONSOLE: Guarded = Guarded::new(Console::new()); + +pub(crate) struct OutputLock { + console: GuardedLock<'static, Console, Mutex>, + /// The lock has to be released by the thread that took it. + _not_send: PhantomData<*const ()>, +} + +/// The process-wide console output lock. Every console write and all of the emulator's shared +/// state are serialized by it. Not re-entrant. +#[must_use = "the console output lock is released when the guard is dropped"] +pub(crate) fn lock_output() -> OutputLock { + OutputLock { + console: CONSOLE.lock(), + _not_send: PhantomData, + } +} + +pub(crate) fn init_output_handle(handle: HANDLE) -> Result<(), u32> { + let info = screen_buffer_info(handle)?; + let cursor_info = cursor_info(handle)?; + + let mut lock = lock_output(); + let console = &mut *lock.console; + if console.need_check_vterm_state { + console.determine_vterm_state(handle); + } + console.capture_initial_style(&info, cursor_info); + console.update_virtual_window(&info); + Ok(()) +} + +/// UTF-8 in. All of `data` is always parsed, so `state` stays consistent; after the first +/// console API failure (the returned error) no further console calls are made. +pub(crate) fn write(handle: HANDLE, state: &mut OutputState, data: &[u8]) -> Result<(), u32> { + with_writer(handle, state, |writer| writer.write(data)) +} + +/// As [`write`] for text that is UTF-16 already. Unpaired surrogates are let through. +pub(crate) fn write_utf16( + handle: HANDLE, + state: &mut OutputState, + data: &[u16], +) -> Result<(), u32> { + with_writer(handle, state, |writer| writer.write_utf16(data)) +} + +/// As [`write`] for Latin-1: every byte is the code point of that value. +pub(crate) fn write_latin1( + handle: HANDLE, + state: &mut OutputState, + data: &[u8], +) -> Result<(), u32> { + with_writer(handle, state, |writer| writer.write_latin1(data)) +} + +fn with_writer( + handle: HANDLE, + state: &mut OutputState, + write: impl FnOnce(&mut Writer<'_>), +) -> Result<(), u32> { + let mut lock = lock_output(); + let console = &mut *lock.console; + let mut writer = Writer { + handle, + passthrough: console.vterm_state == VtermState::Supported, + console, + state, + error: ERROR_SUCCESS, + used: 0, + buf: [const { MaybeUninit::uninit() }; MAX_CONSOLE_CHARS], + }; + write(&mut writer); + writer.flush_text(); + if writer.error == ERROR_SUCCESS { + Ok(()) + } else { + Err(writer.error) + } +} + +fn screen_buffer_info(handle: HANDLE) -> Result { + let mut info = MaybeUninit::::uninit(); + // SAFETY: `info` is a valid out-pointer; a bad `handle` makes the call fail. + if unsafe { GetConsoleScreenBufferInfo(handle, info.as_mut_ptr()) } == 0 { + return Err(GetLastError()); + } + // SAFETY: the call succeeded, so it filled the struct. + Ok(unsafe { info.assume_init() }) +} + +fn cursor_info(handle: HANDLE) -> Result { + let mut info = MaybeUninit::::uninit(); + // SAFETY: `info` is a valid out-pointer; a bad `handle` makes the call fail. + if unsafe { ffi::GetConsoleCursorInfo(handle, info.as_mut_ptr()) } == 0 { + return Err(GetLastError()); + } + // SAFETY: the call succeeded, so it filled the struct. + Ok(unsafe { info.assume_init() }) +} + +fn set_cursor_info(handle: HANDLE, info: CONSOLE_CURSOR_INFO) -> Result<(), DWORD> { + // SAFETY: `info` is a valid in-pointer for the duration of the call. + if unsafe { ffi::SetConsoleCursorInfo(handle, &raw const info) } == 0 { + return Err(GetLastError()); + } + Ok(()) +} + +fn set_text_attribute(handle: HANDLE, attributes: WORD) -> Result<(), DWORD> { + // SAFETY: by-value arguments only. + if unsafe { ffi::SetConsoleTextAttribute(handle, attributes) } == 0 { + return Err(GetLastError()); + } + Ok(()) +} + +/// What a coordinate is checked against. A console call that fails with +/// `ERROR_INVALID_PARAMETER` is tried again only while this differs from the failed attempt's: +/// the console was resized after the screen buffer info was read. +fn geometry(info: &CONSOLE_SCREEN_BUFFER_INFO) -> [i16; 6] { + [ + info.dwSize.X, + info.dwSize.Y, + info.srWindow.Left, + info.srWindow.Top, + info.srWindow.Right, + info.srWindow.Bottom, + ] +} + +/// Blanks the run of cells `region` selects (start, cell count, attributes). `region` is asked +/// again with fresh screen buffer info when the console is resized underneath the call. +fn blank_cells( + handle: HANDLE, + mut region: impl FnMut(&CONSOLE_SCREEN_BUFFER_INFO) -> (COORD, DWORD, WORD), +) -> Result { + let mut failed_with = None; + loop { + let info = screen_buffer_info(handle)?; + let (start, count, attributes) = region(&info); + let mut written: DWORD = 0; + let filled = + FillConsoleOutputCharacterW(handle, u16::from(b' '), count, start, &mut written) != 0 + && FillConsoleOutputAttribute(handle, attributes, written, start, &mut written) + != 0; + if filled { + return Ok(info); + } + let err = GetLastError(); + if err != ERROR_INVALID_PARAMETER || failed_with == Some(geometry(&info)) { + return Err(err); + } + failed_with = Some(geometry(&info)); + } +} + +fn ansi_color(attributes: WORD, red: WORD, green: WORD, blue: WORD) -> u8 { + u8::from(attributes & red != 0) + | u8::from(attributes & green != 0) << 1 + | u8::from(attributes & blue != 0) << 2 +} + +fn set_ansi_color(attributes: &mut WORD, color: u8, red: WORD, green: WORD, blue: WORD) { + *attributes &= !(red | green | blue); + if color & 1 != 0 { + *attributes |= red; + } + if color & 2 != 0 { + *attributes |= green; + } + if color & 4 != 0 { + *attributes |= blue; + } +} + +fn set_flag(attributes: &mut WORD, flag: WORD, on: bool) { + if on { + *attributes |= flag; + } else { + *attributes &= !flag; + } +} + +/// With `COMMON_LVB_REVERSE_VIDEO` set the console stores the colours swapped; SGR edits are +/// made on the logical colours, between two swaps. +fn flip_fg_bg(attributes: WORD) -> WORD { + let fg = attributes & 0x0F; + let bg = attributes & 0xF0; + (attributes & 0xFF00) | (fg << 4) | (bg >> 4) +} + +impl Console { + const fn new() -> Self { + Self { + virtual_offset: -1, + virtual_height: -1, + virtual_width: -1, + default_text_attributes: FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, + default_fg_color: 7, + default_bg_color: 0, + default_fg_bright: false, + default_bg_bright: false, + default_inverse: false, + default_cursor_info: CONSOLE_CURSOR_INFO { + dwSize: 0, + bVisible: 0, + }, + need_check_vterm_state: true, + vterm_state: VtermState::Unsupported, + style_captured: false, + } + } + + /// Turns virtual-terminal processing on for the console (it stays on) if the console can do + /// it: Windows 10 1511 and later, unless legacy console mode is selected. + fn determine_vterm_state(&mut self, handle: HANDLE) { + self.need_check_vterm_state = false; + let mut mode: DWORD = 0; + let enabled = GetConsoleMode(handle, &mut mode) != 0 + && SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0; + if enabled { + self.vterm_state = VtermState::Supported; + } + } + + fn capture_initial_style( + &mut self, + info: &CONSOLE_SCREEN_BUFFER_INFO, + cursor_info: CONSOLE_CURSOR_INFO, + ) { + if self.style_captured { + return; + } + self.style_captured = true; + + // Black on black would make everything written after a reset invisible. + let attributes = if info.wAttributes == 0 { + 7 + } else { + info.wAttributes + }; + self.default_text_attributes = attributes; + self.default_fg_color = ansi_color( + attributes, + FOREGROUND_RED, + FOREGROUND_GREEN, + FOREGROUND_BLUE, + ); + self.default_bg_color = ansi_color( + attributes, + BACKGROUND_RED, + BACKGROUND_GREEN, + BACKGROUND_BLUE, + ); + self.default_fg_bright = attributes & FOREGROUND_INTENSITY != 0; + self.default_bg_bright = attributes & BACKGROUND_INTENSITY != 0; + self.default_inverse = attributes & COMMON_LVB_REVERSE_VIDEO != 0; + self.default_cursor_info = cursor_info; + } + + fn update_virtual_window(&mut self, info: &CONSOLE_SCREEN_BUFFER_INFO) { + self.virtual_width = i32::from(info.dwSize.X); + self.virtual_height = i32::from(info.srWindow.Bottom) - i32::from(info.srWindow.Top) + 1; + + let cursor_y = i32::from(info.dwCursorPosition.Y); + if self.virtual_offset == -1 { + self.virtual_offset = cursor_y; + } else if self.virtual_offset < cursor_y - self.virtual_height + 1 { + // The cursor is below the window, so the output has scrolled. + self.virtual_offset = cursor_y - self.virtual_height + 1; + } + if self.virtual_offset + self.virtual_height > i32::from(info.dwSize.Y) { + self.virtual_offset = i32::from(info.dwSize.Y) - self.virtual_height; + } + if self.virtual_offset < 0 { + self.virtual_offset = 0; + } + } + + /// Screen buffer coordinates for a position, clipped to the virtual window. + fn make_real_coord( + &mut self, + info: &CONSOLE_SCREEN_BUFFER_INFO, + x: Offset, + y: Offset, + ) -> COORD { + self.update_virtual_window(info); + + let mut y = match y { + FromCursor(y) => i32::from(info.dwCursorPosition.Y) + y, + FromOrigin(y) => self.virtual_offset + y, + }; + if y < self.virtual_offset { + y = self.virtual_offset; + } else if y >= self.virtual_offset + self.virtual_height { + y = self.virtual_offset + self.virtual_height - 1; + } + + let mut x = match x { + FromCursor(x) => i32::from(info.dwCursorPosition.X) + x, + FromOrigin(x) => x, + }; + if x < 0 { + x = 0; + } else if x >= self.virtual_width { + x = self.virtual_width - 1; + } + + COORD { + X: x as i16, + Y: y as i16, + } + } +} + +#[derive(Default)] +struct StyleChange { + fg_color: Option, + bg_color: Option, + fg_bright: Option, + bg_bright: Option, + inverse: Option, +} + +impl StyleChange { + fn reset(&mut self, console: &Console) { + self.fg_color = Some(console.default_fg_color); + self.bg_color = Some(console.default_bg_color); + self.fg_bright = Some(console.default_fg_bright); + self.bg_bright = Some(console.default_bg_bright); + self.inverse = Some(console.default_inverse); + } + + fn is_empty(&self) -> bool { + self.fg_color.is_none() + && self.bg_color.is_none() + && self.fg_bright.is_none() + && self.bg_bright.is_none() + && self.inverse.is_none() + } +} + +/// One `write` call. Holds the output lock (through `console`) for its whole life. +struct Writer<'a> { + handle: HANDLE, + console: &'a mut Console, + state: &'a mut OutputState, + /// The console interprets escape sequences itself. + passthrough: bool, + /// First console API failure, or `ERROR_SUCCESS`. + error: DWORD, + used: usize, + buf: [MaybeUninit; MAX_CONSOLE_CHARS], +} + +impl Writer<'_> { + /// Surrogates are let through. A non-shortest form is not: it would spell a control + /// character (`C0 9B` for ESC) in bytes that a check for that character has passed. + fn write(&mut self, data: &[u8]) { + for &c in data { + if self.state.utf8_bytes_left != 0 { + if c & 0xC0 == 0x80 { + self.state.utf8_bytes_left -= 1; + self.state.utf8_codepoint = + (self.state.utf8_codepoint << 6) | u32::from(c & 0x3F); + if self.state.utf8_bytes_left == 0 { + let codepoint = self.state.utf8_codepoint; + self.put_codepoint( + if (self.state.utf8_least..=0x10FFFF).contains(&codepoint) { + codepoint + } else { + REPLACEMENT_CHARACTER + }, + ); + } + continue; + } + // The sequence was cut short; `c` starts a new one. + self.state.utf8_bytes_left = 0; + self.put_codepoint(REPLACEMENT_CHARACTER); + } + + match c.leading_ones() { + 0 => self.put_codepoint(u32::from(c)), + ones @ 2..=4 => { + self.state.utf8_codepoint = u32::from(c) & (0xFF >> (ones + 1)); + self.state.utf8_bytes_left = (ones - 1) as u8; + self.state.utf8_least = [0x80, 0x800, 0x10000][ones as usize - 2]; + } + // A continuation byte, or the start of nothing UTF-8 has. + _ => self.put_codepoint(REPLACEMENT_CHARACTER), + } + } + } + + /// A UTF-8 sequence that an earlier [`write`](Self::write) left unfinished cannot be + /// continued by text in another encoding. + fn abandon_utf8_sequence(&mut self) { + if self.state.utf8_bytes_left != 0 { + self.state.utf8_bytes_left = 0; + self.put_codepoint(REPLACEMENT_CHARACTER); + } + } + + fn write_utf16(&mut self, data: &[u16]) { + self.abandon_utf8_sequence(); + if !self.passthrough { + let mut units = data.iter().copied().peekable(); + while let Some(unit) = units.next() { + let low = units.peek().copied().filter(|low| { + bun_core::strings::u16_is_lead(unit) && bun_core::strings::u16_is_trail(*low) + }); + let Some(low) = low else { + self.put_codepoint(u32::from(unit)); + continue; + }; + units.next(); + self.put_codepoint( + 0x10000 + ((u32::from(unit) - 0xD800) << 10) + (u32::from(low) - 0xDC00), + ); + } + return; + } + + // Only line ends need a look; what is between them is copied as it is. + let mut rest = data; + while !rest.is_empty() { + let run = bun_core::strings::index_of_any16(rest, &[0x0A, 0x0D]).unwrap_or(rest.len()); + if run == 0 { + self.put_codepoint(u32::from(rest[0])); + rest = &rest[1..]; + continue; + } + self.push_run(&rest[..run]); + self.state.previous_eol = 0; + rest = &rest[run..]; + } + } + + fn write_latin1(&mut self, data: &[u8]) { + self.abandon_utf8_sequence(); + for &c in data { + self.put_codepoint(u32::from(c)); + } + } + + /// Text without line ends. Both halves of a surrogate pair go out in the same + /// `WriteConsoleW` call. + fn push_run(&mut self, mut run: &[u16]) { + while !run.is_empty() { + let mut take = run.len().min(MAX_CONSOLE_CHARS - self.used); + if take > 0 + && take < run.len() + && bun_core::strings::u16_is_lead(run[take - 1]) + && bun_core::strings::u16_is_trail(run[take]) + { + take -= 1; + } + if take == 0 { + self.flush_text(); + continue; + } + // SAFETY: `take` units fit behind `used`, `MaybeUninit` has the layout of + // `u16`, and `run` is not part of `buf`. + unsafe { + ptr::copy_nonoverlapping( + run.as_ptr(), + self.buf.as_mut_ptr().add(self.used).cast::(), + take, + ); + } + self.used += take; + run = &run[take..]; + } + } + + fn put_codepoint(&mut self, codepoint: u32) { + if !self.passthrough && self.parse_ansi(codepoint) { + return; + } + + match codepoint { + 0x0A | 0x0D => { + if codepoint == 0x0A && self.state.previous_eol != 0x0D { + self.ensure_buffer_space(2); + self.push(u16::from(b'\r')); + self.push(u16::from(b'\n')); + } else if codepoint == 0x0D && self.state.previous_eol == 0x0A { + // "\n\r" or "\r\n\r": this "\r" was already written along with the "\n". + } else { + self.ensure_buffer_space(1); + self.push(codepoint as u16); + } + self.state.previous_eol = codepoint as u8; + } + 0..=0xFFFF => { + self.ensure_buffer_space(1); + self.push(codepoint as u16); + self.state.previous_eol = 0; + } + _ => { + // Both halves go out in the same `WriteConsoleW` call. + self.ensure_buffer_space(2); + let offset = codepoint - 0x10000; + self.push((offset / 0x400 + 0xD800) as u16); + self.push((offset % 0x400 + 0xDC00) as u16); + self.state.previous_eol = 0; + } + } + } + + fn ensure_buffer_space(&mut self, needed: usize) { + if needed > MAX_CONSOLE_CHARS - self.used { + self.flush_text(); + } + } + + fn push(&mut self, unit: u16) { + self.buf[self.used].write(unit); + self.used += 1; + } + + fn flush_text(&mut self) { + if self.used == 0 { + return; + } + let len = self.used as DWORD; + self.used = 0; + if self.error != ERROR_SUCCESS { + return; + } + let mut written: DWORD = 0; + // SAFETY: `push` initialized the first `len` units of `buf`; `written` is a valid + // out-pointer. + let ok = unsafe { + ffi::WriteConsoleW( + self.handle, + self.buf.as_ptr().cast::(), + len, + &raw mut written, + ptr::null_mut(), + ) + }; + if ok == 0 { + self.error = GetLastError(); + } + } + + /// Runs a console API operation at the current point of the output, unless one has failed. + fn emulate(&mut self, op: impl FnOnce(&mut Self) -> Result<(), DWORD>) { + self.flush_text(); + if self.error == ERROR_SUCCESS { + if let Err(err) = op(self) { + self.error = err; + } + } + } + + /// Returns whether the character was consumed by the escape sequence parser. + fn parse_ansi(&mut self, codepoint: u32) -> bool { + let ch = char::from_u32(codepoint).unwrap_or(char::REPLACEMENT_CHARACTER); + + match self.state.ansi_state { + AnsiState::Normal => match ch { + '\x1b' => { + self.state.ansi_state = AnsiState::EscapeSeen; + true + } + '\u{9b}' => { + self.state.begin_csi(); + true + } + _ => false, + }, + + AnsiState::EscapeSeen => { + match ch { + '[' => self.state.begin_csi(), + '^' | '_' | 'P' | ']' => { + self.state.ansi_state = AnsiState::StControl { escape_seen: false }; + } + '\x1b' => {} + 'c' => { + self.state.ansi_state = AnsiState::Normal; + self.emulate(Self::reset); + } + '7' => { + self.state.ansi_state = AnsiState::Normal; + self.emulate(|w| w.save_state(true)); + } + '8' => { + self.state.ansi_state = AnsiState::Normal; + self.emulate(|w| w.restore_state(true)); + } + // Any other single-character control. + '@'..='_' => self.state.ansi_state = AnsiState::Normal, + _ => { + self.state.ansi_state = AnsiState::Normal; + return false; + } + } + true + } + + AnsiState::Ignore => { + if matches!(ch, '@'..='~') { + self.state.ansi_state = AnsiState::Normal; + } + true + } + + AnsiState::Decscusr => { + if !matches!(ch, '@'..='~') { + // libuv prints this character and swallows only what follows it. + self.state.ansi_state = AnsiState::Ignore; + return false; + } + self.state.ansi_state = AnsiState::Normal; + if ch == 'q' { + let style = self.state.first_arg_or(1); + if style <= 6 { + self.emulate(|w| w.set_cursor_shape(style)); + } + } + true + } + + AnsiState::Csi { in_arg, extension } => { + self.parse_csi(ch, in_arg, extension); + true + } + + AnsiState::StControl { escape_seen } => { + self.state.ansi_state = match ch { + '\x07' => AnsiState::Normal, + '\\' if escape_seen => AnsiState::Normal, + '\x1b' => AnsiState::StControl { escape_seen: true }, + '"' => AnsiState::StString { + backslash_seen: false, + }, + _ => AnsiState::StControl { escape_seen: false }, + }; + true + } + + AnsiState::StString { backslash_seen } => { + self.state.ansi_state = match ch { + _ if backslash_seen => AnsiState::StString { + backslash_seen: false, + }, + '"' => AnsiState::StControl { escape_seen: false }, + '\\' => AnsiState::StString { + backslash_seen: true, + }, + _ => AnsiState::StString { + backslash_seen: false, + }, + }; + true + } + } + } + + fn parse_csi(&mut self, ch: char, in_arg: bool, extension: bool) { + let state = &mut *self.state; + match ch { + '0'..='9' => { + let digit = ch as u16 - u16::from(b'0'); + if !in_arg { + if state.csi_argc >= MAX_CSI_ARGS { + state.ansi_state = AnsiState::Ignore; + return; + } + state.ansi_state = AnsiState::Csi { + in_arg: true, + extension, + }; + state.csi_argv[state.csi_argc] = digit; + state.csi_argc += 1; + } else { + let arg = &mut state.csi_argv[state.csi_argc - 1]; + match arg.checked_mul(10) { + Some(value) => *arg = value.wrapping_add(digit), + None => state.ansi_state = AnsiState::Ignore, + } + } + } + + ';' => { + if in_arg { + state.ansi_state = AnsiState::Csi { + in_arg: false, + extension, + }; + } else if state.csi_argc >= MAX_CSI_ARGS { + state.ansi_state = AnsiState::Ignore; + } else { + // An empty argument is 0. + state.csi_argv[state.csi_argc] = 0; + state.csi_argc += 1; + } + } + + '?' if !in_arg && !extension && state.csi_argc == 0 => { + state.ansi_state = AnsiState::Csi { + in_arg, + extension: true, + }; + } + + ' ' if !extension => state.ansi_state = AnsiState::Decscusr, + + '@'..='~' => { + state.ansi_state = AnsiState::Normal; + if extension { + self.run_private_csi_command(ch); + } else { + self.run_csi_command(ch); + } + } + + // Private-mode characters and intermediates are not supported. + _ => state.ansi_state = AnsiState::Ignore, + } + } + + /// `ESC [ ? args command` + fn run_private_csi_command(&mut self, command: char) { + let visible = match command { + 'h' => true, + 'l' => false, + _ => return, + }; + if self.state.csi_argc == 1 && self.state.csi_argv[0] == 25 { + self.emulate(|w| w.set_cursor_visibility(visible)); + } + } + + /// `ESC [ args command` + fn run_csi_command(&mut self, command: char) { + let count = i32::from(self.state.first_arg_or(1)); + match command { + 'A' => self.emulate(|w| w.move_caret(FromCursor(0), FromCursor(-count))), + 'B' => self.emulate(|w| w.move_caret(FromCursor(0), FromCursor(count))), + 'C' => self.emulate(|w| w.move_caret(FromCursor(count), FromCursor(0))), + 'D' => self.emulate(|w| w.move_caret(FromCursor(-count), FromCursor(0))), + 'E' => self.emulate(|w| w.move_caret(FromOrigin(0), FromCursor(count))), + 'F' => self.emulate(|w| w.move_caret(FromOrigin(0), FromCursor(-count))), + 'G' => { + let x = self.state.coordinate_arg(0); + self.emulate(|w| w.move_caret(FromOrigin(x), FromCursor(0))); + } + 'H' | 'f' => { + let y = self.state.coordinate_arg(0); + let x = self.state.coordinate_arg(1); + self.emulate(|w| w.move_caret(FromOrigin(x), FromOrigin(y))); + } + 'J' | 'K' => { + let direction = self.state.first_arg_or(0); + if direction <= 2 { + self.emulate(|w| w.clear(direction, command == 'J')); + } else { + self.flush_text(); + } + } + 'm' => self.emulate(Self::set_style), + 's' => self.emulate(|w| w.save_state(false)), + 'u' => self.emulate(|w| w.restore_state(false)), + _ => {} + } + } + + fn move_caret(&mut self, x: Offset, y: Offset) -> Result<(), DWORD> { + let mut failed_with = None; + loop { + let info = screen_buffer_info(self.handle)?; + let position = self.console.make_real_coord(&info, x, y); + if SetConsoleCursorPosition(self.handle, position) != 0 { + return Ok(()); + } + let err = GetLastError(); + if err != ERROR_INVALID_PARAMETER || failed_with == Some(geometry(&info)) { + return Err(err); + } + failed_with = Some(geometry(&info)); + } + } + + /// `ESC c` + fn reset(&mut self) -> Result<(), DWORD> { + const ORIGIN: COORD = COORD { X: 0, Y: 0 }; + let attributes = self.console.default_text_attributes; + + set_text_attribute(self.handle, attributes)?; + if SetConsoleCursorPosition(self.handle, ORIGIN) == 0 { + return Err(GetLastError()); + } + let info = blank_cells(self.handle, |info| { + let count = i32::from(info.dwSize.X) * i32::from(info.dwSize.Y); + (ORIGIN, count as DWORD, attributes) + })?; + + self.console.virtual_offset = 0; + self.console.update_virtual_window(&info); + + set_cursor_info(self.handle, self.console.default_cursor_info) + } + + /// `direction`: 0 = cursor to end, 1 = start to cursor, 2 = everything; of the virtual + /// window if `entire_screen`, else of the cursor's row. + fn clear(&mut self, direction: u16, entire_screen: bool) -> Result<(), DWORD> { + let x1 = if direction == 0 { + FromCursor(0) + } else { + FromOrigin(0) + }; + // The far end is past any real width; `make_real_coord` clips it. + let x2 = if direction == 1 { + FromCursor(0) + } else { + FromOrigin(0xFFFF) + }; + let (y1, y2) = if entire_screen { + (x1, x2) + } else { + (FromCursor(0), FromCursor(0)) + }; + + let console = &mut *self.console; + blank_cells(self.handle, |info| { + let start = console.make_real_coord(info, x1, y1); + let end = console.make_real_coord(info, x2, y2); + let width = i32::from(info.dwSize.X); + let count = (i32::from(end.Y) * width + i32::from(end.X)) + - (i32::from(start.Y) * width + i32::from(start.X)) + + 1; + (start, count as DWORD, info.wAttributes) + })?; + Ok(()) + } + + /// SGR. There is no 256-colour or RGB support, and "blink" (5/25) is bright background. + fn set_style(&mut self) -> Result<(), DWORD> { + let console = &*self.console; + let mut change = StyleChange::default(); + + if self.state.csi_argc == 0 { + change.reset(console); + } + for &arg in &self.state.csi_argv[..self.state.csi_argc] { + match arg { + 0 => change.reset(console), + 1 => change.fg_bright = Some(true), + 2 => { + change.fg_bright = Some(false); + change.bg_bright = Some(false); + } + 5 => change.bg_bright = Some(true), + 7 => change.inverse = Some(true), + 21 | 22 => change.fg_bright = Some(false), + 25 => change.bg_bright = Some(false), + 27 => change.inverse = Some(false), + 30..=37 => change.fg_color = Some((arg - 30) as u8), + 39 => { + change.fg_color = Some(console.default_fg_color); + change.fg_bright = Some(console.default_fg_bright); + } + 40..=47 => change.bg_color = Some((arg - 40) as u8), + 49 => { + change.bg_color = Some(console.default_bg_color); + change.bg_bright = Some(console.default_bg_bright); + } + 90..=97 => { + change.fg_bright = Some(true); + change.fg_color = Some((arg - 90) as u8); + } + 100..=107 => { + change.bg_bright = Some(true); + change.bg_color = Some((arg - 100) as u8); + } + _ => {} + } + } + if change.is_empty() { + return Ok(()); + } + + let mut attributes = screen_buffer_info(self.handle)?.wAttributes; + if attributes & COMMON_LVB_REVERSE_VIDEO != 0 { + attributes = flip_fg_bg(attributes); + } + + if let Some(color) = change.fg_color { + set_ansi_color( + &mut attributes, + color, + FOREGROUND_RED, + FOREGROUND_GREEN, + FOREGROUND_BLUE, + ); + } + if let Some(bright) = change.fg_bright { + set_flag(&mut attributes, FOREGROUND_INTENSITY, bright); + } + if let Some(color) = change.bg_color { + set_ansi_color( + &mut attributes, + color, + BACKGROUND_RED, + BACKGROUND_GREEN, + BACKGROUND_BLUE, + ); + } + if let Some(bright) = change.bg_bright { + set_flag(&mut attributes, BACKGROUND_INTENSITY, bright); + } + if let Some(inverse) = change.inverse { + set_flag(&mut attributes, COMMON_LVB_REVERSE_VIDEO, inverse); + } + + if attributes & COMMON_LVB_REVERSE_VIDEO != 0 { + attributes = flip_fg_bg(attributes); + } + set_text_attribute(self.handle, attributes) + } + + fn save_state(&mut self, save_attributes: bool) -> Result<(), DWORD> { + let info = screen_buffer_info(self.handle)?; + self.console.update_virtual_window(&info); + + self.state.saved_position = Some(COORD { + X: info.dwCursorPosition.X, + Y: (i32::from(info.dwCursorPosition.Y) - self.console.virtual_offset) as i16, + }); + if save_attributes { + self.state.saved_attributes = + Some(info.wAttributes & (FOREGROUND_INTENSITY | BACKGROUND_INTENSITY)); + } + Ok(()) + } + + fn restore_state(&mut self, restore_attributes: bool) -> Result<(), DWORD> { + if let Some(position) = self.state.saved_position { + self.move_caret( + FromOrigin(i32::from(position.X)), + FromOrigin(i32::from(position.Y)), + )?; + } + if restore_attributes { + if let Some(saved) = self.state.saved_attributes { + let current = screen_buffer_info(self.handle)?.wAttributes; + let attributes = (current & !(FOREGROUND_INTENSITY | BACKGROUND_INTENSITY)) | saved; + set_text_attribute(self.handle, attributes)?; + } + } + Ok(()) + } + + fn set_cursor_visibility(&mut self, visible: bool) -> Result<(), DWORD> { + let mut info = cursor_info(self.handle)?; + info.bVisible = visible.into(); + set_cursor_info(self.handle, info) + } + + /// DECSCUSR: 0 = the console's original size, 1-2 = block, 3-6 = underline and bar. + fn set_cursor_shape(&mut self, style: u16) -> Result<(), DWORD> { + let mut info = cursor_info(self.handle)?; + info.dwSize = match style { + 0 => self.console.default_cursor_info.dwSize, + 1 | 2 => CURSOR_SIZE_LARGE, + _ => CURSOR_SIZE_SMALL, + }; + set_cursor_info(self.handle, info) + } +} + +mod ffi { + use core::ffi::c_void; + + use bun_windows_sys::{BOOL, DWORD, HANDLE, WORD}; + + pub(super) const FOREGROUND_BLUE: WORD = 0x0001; + pub(super) const FOREGROUND_GREEN: WORD = 0x0002; + pub(super) const FOREGROUND_RED: WORD = 0x0004; + pub(super) const FOREGROUND_INTENSITY: WORD = 0x0008; + pub(super) const BACKGROUND_BLUE: WORD = 0x0010; + pub(super) const BACKGROUND_GREEN: WORD = 0x0020; + pub(super) const BACKGROUND_RED: WORD = 0x0040; + pub(super) const BACKGROUND_INTENSITY: WORD = 0x0080; + pub(super) const COMMON_LVB_REVERSE_VIDEO: WORD = 0x4000; + + #[repr(C)] + #[derive(Clone, Copy)] + #[allow(non_snake_case)] + pub(super) struct CONSOLE_CURSOR_INFO { + /// Percentage of the character cell the cursor fills, 1-100. + pub(super) dwSize: DWORD, + pub(super) bVisible: BOOL, + } + + #[link(name = "kernel32")] + unsafe extern "system" { + pub(super) fn WriteConsoleW( + hConsoleOutput: HANDLE, + lpBuffer: *const c_void, + nNumberOfCharsToWrite: DWORD, + lpNumberOfCharsWritten: *mut DWORD, + lpReserved: *mut c_void, + ) -> BOOL; + pub(super) fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) -> BOOL; + pub(super) fn GetConsoleCursorInfo( + hConsoleOutput: HANDLE, + lpConsoleCursorInfo: *mut CONSOLE_CURSOR_INFO, + ) -> BOOL; + pub(super) fn SetConsoleCursorInfo( + hConsoleOutput: HANDLE, + lpConsoleCursorInfo: *const CONSOLE_CURSOR_INFO, + ) -> BOOL; + } +} diff --git a/src/io/windows_event_loop.rs b/src/io/windows_event_loop.rs deleted file mode 100644 index 5683f2a3796c..000000000000 --- a/src/io/windows_event_loop.rs +++ /dev/null @@ -1,254 +0,0 @@ -use core::ffi::c_void; -use core::ptr; - -use bun_sys::Fd; -use bun_sys::windows::libuv as uv; -use bun_uws_sys::WindowsLoop; - -use crate::posix_event_loop as posix; -// Shared scaffolding lives in `posix_event_loop` (platform-agnostic types); -// only `FilePoll`/`Store`/`KeepAlive`/`Closer`/`Loop`/`Waker` are redefined -// here. `Flags`/`Owner`/etc. are re-aliased below from `posix` for callers -// that name them via this module. -pub use crate::posix_event_loop::{EventLoopCtx, OpaqueCallback, js_vm_ctx}; - -// `Loop` here is the raw -// `uv_loop_t`. (`WindowsLoop` is the uws wrapper that *owns* a `*mut uv::Loop` -// in its `.uv_loop` field; callers that hold a `WindowsLoop*` project that -// field themselves. See `VirtualMachine::event_loop_handle` / -// `SpawnSyncEventLoop` which store/compare the inner `uv::Loop` pointer.) -pub type Loop = uv::Loop; - -// `KeepAlive` (struct + 14-method impl) was duplicated here and in -// `posix_event_loop.rs`; both copies now live in `crate::keep_alive`. - -pub(crate) type Flags = posix::Flags; -pub type FlagsSet = posix::FlagsSet; -pub type Owner = posix::Owner; - -pub struct FilePoll { - pub(crate) fd: Fd, - pub(crate) owner: Owner, - pub(crate) flags: FlagsSet, - pub(crate) next_to_free: *mut FilePoll, -} - -impl FilePoll { - #[inline] - pub(crate) fn is_active(&self) -> bool { - self.flags.contains(Flags::HasIncrementedPollCount) - } - - #[inline] - pub(crate) fn is_watching(&self) -> bool { - !self.flags.contains(Flags::NeedsRearm) - && (self.flags.contains(Flags::PollReadable) - || self.flags.contains(Flags::PollWritable) - || self.flags.contains(Flags::PollProcess)) - } - - pub(crate) fn is_registered(&self) -> bool { - self.flags.contains(Flags::PollWritable) - || self.flags.contains(Flags::PollReadable) - || self.flags.contains(Flags::PollProcess) - || self.flags.contains(Flags::PollMachport) - } - - /// Decrements the active counter if it was previously incremented. - pub(crate) fn disable_keeping_process_alive(&mut self, vm: EventLoopCtx) { - if self.flags.contains(Flags::Closed) { - return; - } - self.flags.insert(Flags::Closed); - - vm.loop_sub_active(self.flags.contains(Flags::HasIncrementedPollCount) as u32); - } - - pub(crate) fn init(vm: EventLoopCtx, fd: Fd, flags: FlagsSet, owner: Owner) -> *mut FilePoll { - Self::init_with_owner(vm, fd, flags, owner) - } - - pub(crate) fn init_with_owner( - vm: EventLoopCtx, - fd: Fd, - flags: FlagsSet, - owner: Owner, - ) -> *mut FilePoll { - // Crate-private backref-deref accessor — single live `&mut Store` borrow. - vm.file_polls_mut() - .get_init(FilePoll { - fd, - flags, - owner, - next_to_free: ptr::null_mut(), - }) - .as_ptr() - } - - // Note: not `impl Drop` — FilePoll lives in a HiveArray pool slot, not a Box; - // teardown returns the slot to the pool via `Store::put`. - pub fn deinit(&mut self) { - self.deinit_with_vm(js_vm_ctx()); - } - - pub(crate) fn deinit_force_unregister(&mut self) { - self.deinit() - } - - pub(crate) fn unregister(&mut self, _loop: &mut WindowsLoop) -> bool { - // TODO: This cast is extremely suspicious. At best, `fd` is - // the wrong type (it should be a uv handle), at worst this code is a - // crash due to invalid memory access. - // - // SAFETY: in practice this call is unreachable. On Windows nothing - // ever sets the `Poll*` registration flags after construction (this - // module defines no `register`), and every in-tree constructor passes - // empty/default flags, so `is_registered()` stays false and - // `deinit_possibly_defer` — the only path here — never takes the - // `unregister` branch. If a Windows registration path is ever added, - // this cast must be replaced with a real `uv_handle_t` pointer first - // (see TODO above); `uv_unref` dereferences its argument. - unsafe { - uv::uv_unref(self.fd.0 as *mut uv::uv_handle_t); - } - true - } - - fn deinit_possibly_defer(&mut self, vm: EventLoopCtx, loop_: &mut WindowsLoop) { - if self.is_registered() { - let _ = self.unregister(loop_); - } - - let was_ever_registered = self.flags.contains(Flags::WasEverRegistered); - self.flags = FlagsSet::default(); - self.fd = Fd::INVALID; - // All `self` field writes are done. Decay `self` to a raw slot pointer - // *before* materializing `&mut Store` so the `&mut Store` borrow (which - // covers the inline hive buffer) is the only live unique reference into - // that allocation when `Store::put` runs. `self` is never touched after - // this line — `Store::put` itself accesses `this` only via raw-pointer ops. - let this: ptr::NonNull = ptr::NonNull::from(&mut *self); - // `file_polls_mut()` is the per-thread set-once `Store` back-pointer - // (`BackRef`-shaped); `&mut self` has been retired to `this` above so - // the `&mut Store` it produces is the sole unique borrow into the hive. - vm.file_polls_mut().put(this, vm, was_ever_registered); - } - - pub(crate) fn deinit_with_vm(&mut self, vm: EventLoopCtx) { - // `loop_mut()` — crate-private nonnull-asref accessor (single deref in - // `EventLoopCtx`); the uws loop is a disjoint allocation from `self`. - // Stacked-Borrows: `self` may live inside `Store.hive`'s inline buffer, - // so `&mut Store` is materialised only *after* `&mut self` is retired - // inside `deinit_possibly_defer` (via `file_polls_mut()`). - let loop_ = vm.loop_mut(); - self.deinit_possibly_defer(vm, loop_); - } - - pub(crate) fn enable_keeping_process_alive(&mut self, vm: EventLoopCtx) { - if !self.flags.contains(Flags::Closed) { - return; - } - self.flags.remove(Flags::Closed); - - vm.loop_add_active(self.flags.contains(Flags::HasIncrementedPollCount) as u32); - } -} - -type FilePollHiveArray = bun_collections::hive_array::Fallback; - -pub struct Store { - hive: FilePollHiveArray, - pending_free_head: *mut FilePoll, - pending_free_tail: *mut FilePoll, -} - -impl Store { - pub fn init() -> Store { - Store { - hive: FilePollHiveArray::init(), - pending_free_head: ptr::null_mut(), - pending_free_tail: ptr::null_mut(), - } - } - - #[inline] - pub(crate) fn get_init(&mut self, value: FilePoll) -> ptr::NonNull { - self.hive.get_init(value) - } - - pub(crate) fn process_deferred_frees(&mut self) { - let mut next = self.pending_free_head; - while !next.is_null() { - let current = next; - // SAFETY: intrusive deferred-free list; nodes are valid HiveArray slots - // until put(). Walk via raw-pointer reads/writes only — materializing a - // `&mut FilePoll` here would alias the `&mut self.hive` borrow taken by - // `put()` below (the slot may live inside the inline hive buffer). - unsafe { - next = (*current).next_to_free; - (*current).next_to_free = ptr::null_mut(); - // FilePoll has no drop glue; `put` is a no-op drop + recycle. - self.hive.put(current); - } - } - self.pending_free_head = ptr::null_mut(); - self.pending_free_tail = ptr::null_mut(); - } - - /// `poll` is a live, fully-initialized slot in `self.hive`. Touched only - /// through raw pointer ops to avoid forming a `&mut FilePoll` that would - /// alias `&mut self` (the hive buffer is inline storage). - pub fn put(&mut self, poll: ptr::NonNull, vm: EventLoopCtx, ever_registered: bool) { - let poll = poll.as_ptr(); - if !ever_registered { - // SAFETY: `poll` is a fully-initialized hive slot; FilePoll has no - // drop glue, so `put` is a no-op drop + recycle. - unsafe { self.hive.put(poll) }; - return; - } - - // SAFETY: `poll` is a valid HiveArray slot pointer. It may live inside - // `self.hive.buffer`, so we access it via raw pointer only (no `&mut FilePoll` - // materialized) to avoid aliasing `&mut self`. - debug_assert!(unsafe { (*poll).next_to_free }.is_null()); - - let tail = self.pending_free_tail; - if !tail.is_null() { - debug_assert!(!self.pending_free_head.is_null()); - // SAFETY: `tail` is a valid slot in the intrusive deferred-free list; - // raw-ptr access avoids a second `&mut FilePoll` overlapping `poll`/`self`. - debug_assert!(unsafe { (*tail).next_to_free }.is_null()); - unsafe { (*tail).next_to_free = poll }; - } - - if self.pending_free_head.is_null() { - self.pending_free_head = poll; - debug_assert!(self.pending_free_tail.is_null()); - } - - // SAFETY: see above — short-lived field borrow through raw `poll`, no overlap held. - unsafe { (*poll).flags.insert(Flags::IgnoreUpdates) }; - self.pending_free_tail = poll; - - let callback: OpaqueCallback = Self::process_deferred_frees_thunk; - debug_assert!( - vm.after_event_loop_callback().is_none() - || vm.after_event_loop_callback().map(|f| f as usize) == Some(callback as usize) - ); - vm.set_after_event_loop_callback( - Some(callback), - core::ptr::NonNull::new(core::ptr::from_mut::(self).cast::()), - ); - } - - // Safe fn item: module-private thunk, only coerced to the C-ABI - // `OpaqueCallback` fn-pointer type — never callable by name outside - // `Store`. Body wraps its raw-ptr op explicitly. - extern "C" fn process_deferred_frees_thunk(ctx: *mut c_void) { - // SAFETY: `ctx` was set to `self as *mut Store` in `put` above. The thunk fires - // from the event loop's after-tick hook with no other `&mut Store` borrow live, - // so this is the unique accessor (safe-single-owner). - let this = unsafe { bun_ptr::callback_ctx::(ctx) }; - this.process_deferred_frees(); - } -} diff --git a/src/js/builtins.d.ts b/src/js/builtins.d.ts index 0bbd12346182..cfa40b8c4d8f 100644 --- a/src/js/builtins.d.ts +++ b/src/js/builtins.d.ts @@ -303,7 +303,6 @@ declare function $resolveSync( ): string; declare function $self(): TODO; declare function $size(): TODO; -declare function $start(): TODO; declare function $status(): TODO; declare function $stream(): TODO; declare function $syscall(): TODO; diff --git a/src/js/internal-for-testing.ts b/src/js/internal-for-testing.ts index 68183d47f35e..89bf780a3374 100644 --- a/src/js/internal-for-testing.ts +++ b/src/js/internal-for-testing.ts @@ -735,7 +735,7 @@ export const getEventLoopStats: () => { numPolls: number; loopActive: boolean; eventLoopAlive: boolean; - /** usockets/libuv loop iterations so far (us_internal_loop_pre count). */ + /** usockets loop iterations so far (us_internal_loop_pre count). */ iteration: number; } = $newRustFunction("event_loop.rs", "getActiveTasks", 0); diff --git a/src/js/internal/streams/native-readable.ts b/src/js/internal/streams/native-readable.ts index 3d62e724416f..394eca82fd4b 100644 --- a/src/js/internal/streams/native-readable.ts +++ b/src/js/internal/streams/native-readable.ts @@ -39,7 +39,6 @@ interface NativeReadable extends NodeReadable { length: number; }; $bunNativePtr: NativePtr | undefined; - $start?: typeof ensureConstructed; ref: typeof ref; unref: typeof unref; [kRefCount]: number; @@ -92,10 +91,6 @@ function constructNativeReadable(readableStream: ReadableStream, options): Nativ stream.ref = ref; stream.unref = unref; - if (process.platform === "win32") { - // Only used by node:tty on Windows - stream.$start = ensureConstructed; - } // https://github.com/oven-sh/bun/pull/12801 // https://github.com/oven-sh/bun/issues/9555 @@ -109,17 +104,6 @@ function constructNativeReadable(readableStream: ReadableStream, options): Nativ return stream; } -function ensureConstructed(this: NativeReadable, cb: null | (() => void)) { - $debug(`[${this.debugId}] ensureConstructed`); - if (this[kConstructed]) return; - this[kConstructed] = true; - const ptr = this.$bunNativePtr; - if (!ptr) return; - $assert(typeof ptr.start === "function", "NativeReadable.start is not a function"); - ptr.start(this[kHighWaterMark]); - if (cb) cb(); -} - // maxToRead can be the highWaterMark (by default) or the remaining amount of the stream to read // This is so the consumer of the stream can terminate the stream early if they know // how many bytes they want to read (ie. when reading only part of a file) diff --git a/src/js/node/child_process.ts b/src/js/node/child_process.ts index 6395dda0e408..d343fbc5774f 100644 --- a/src/js/node/child_process.ts +++ b/src/js/node/child_process.ts @@ -1198,6 +1198,15 @@ class ChildProcess extends EventEmitter { } else if (stderr && this.#stdioOptions[2] === "pipe" && !stderr.destroyed && stderr.readable) { stderr.resume?.(); } + + // 'close' waits for the pipes above stderr too, and one that holds what nobody reads never ends. + const stdio = this.#stdioObject; + if (stdio) { + for (let i = 3; i < stdio.length; i++) { + const stream = stdio[i]; + if (stream && !stream.destroyed && stream.readable) stream.resume(); + } + } } const spawnfile = this.spawnfile; @@ -1317,16 +1326,16 @@ class ChildProcess extends EventEmitter { } } default: - switch (io) { - case "pipe": - case "socket-fd": - if (!NetModule) NetModule = require("node:net"); - // #spawn mapped "pipe" at i>=3 to "socket-fd", so the parent-end - // fd in handle.stdio[i] is UnownedFd: we own it and - // net.connect({fd}) -> usockets will close it on socket close. - const fd = handle && handle.stdio[i]; - if (fd == null) return null; - return NetModule.connect({ fd }); + if (io === "socket-fd") { + if (!NetModule) NetModule = require("node:net"); + // handle.stdio[i] is ours to close (UnownedFd, see spawn()); the socket closes it. + // On Windows it is the HANDLE of a pipe end Bun.spawn made. + const fd = handle && handle.stdio[i]; + if (fd == null) return null; + const socket = NetModule.connect(process.platform === "win32" ? { fd, fdIsSpawnedPipe: true } : { fd }); + this.#closesNeeded++; + socket.once("close", () => this.#maybeClose()); + return socket; } return null; } @@ -1404,17 +1413,22 @@ class ChildProcess extends EventEmitter { const stdio = options.stdio || ["pipe", "pipe", "pipe"]; const bunStdio = getBunStdioFromOptions(stdio); - // Extra "pipe" slots (i >= 3) are wrapped in a net.Socket by - // #getBunSpawnIo, which hands the fd to usockets (usockets closes it on - // socket close). Use Bun.spawn's "socket-fd" so the parent end is stored - // as UnownedFd from the start and Subprocess.finalize_streams never - // double-closes it. Async path only: spawnSync never wraps extra fds in - // net.Socket (no .stdio on Bun.spawnSync's result yet) and must keep - // them OwnedFd so finalize_streams still closes them. On Windows extra - // stdio is a libuv pipe handle with no raw-fd handoff, so leave as "pipe". - if (process.platform !== "win32") { - for (let i = 3; i < bunStdio.length; i++) { - if (bunStdio[i] === "pipe") bunStdio[i] = "socket-fd"; + // #getBunSpawnIo wraps extra "pipe" slots (i >= 3) in a net.Socket that closes + // the fd, so use "socket-fd": the parent end is UnownedFd and + // Subprocess.finalize_streams does not close it again. Async path only: + // spawnSync never wraps extra fds and must keep them OwnedFd so + // finalize_streams closes them. + for (let i = 3; i < bunStdio.length; i++) { + const option = bunStdio[i]; + // The child's end of a pipe at i >= 3 is overlapped on Windows whichever of the two was asked for. + if (option === "pipe" || option === "overlapped") bunStdio[i] = "socket-fd"; + } + // This side of an "overlapped" pipe is a pipe like any other. + let stdioOptions = bunStdio; + if (process.platform === "win32") { + stdioOptions = ArrayPrototypeSlice.$call(bunStdio); + for (let i = 0; i < 3; i++) { + if (stdioOptions[i] === "overlapped") stdioOptions[i] = "pipe"; } } @@ -1431,7 +1445,7 @@ class ChildProcess extends EventEmitter { var env = options[kBunEnv] || parseEnvPairs(envPairs) || process.env; const detachedOption = options.detached; - this.#stdioOptions = bunStdio; + this.#stdioOptions = stdioOptions; const stdioCount = stdio.length; const hasSocketsToEagerlyLoad = stdioCount >= 3; @@ -1734,7 +1748,8 @@ class ChildProcess extends EventEmitter { const nodeToBunLookup = { ignore: null, pipe: "pipe", - overlapped: "pipe", // TODO: this may need to work differently for Windows + // A pipe whose child end is opened for overlapped I/O, which only Windows has. + overlapped: process.platform === "win32" ? "overlapped" : "pipe", inherit: "inherit", ipc: "ipc", }; @@ -1847,7 +1862,7 @@ function getBunStdioFromOptions(stdio) { // Node options: // pipe: just a pipe // ipc = can only be one in array - // overlapped -- same as pipe on Unix based systems + // overlapped -- same as pipe on Unix based systems; on Windows the child's end is opened for overlapped I/O // inherit -- 'inherit': equivalent to ['inherit', 'inherit', 'inherit'] or [0, 1, 2] // ignore -- > /dev/null, more or less same as null option for Bun.spawn stdio // TODO: Stream -- use this stream @@ -1862,7 +1877,7 @@ function getBunStdioFromOptions(stdio) { // Translations: node -> bun // pipe -> pipe - // overlapped -> pipe + // overlapped -> overlapped on Windows, pipe elsewhere // ignore -> null // inherit -> inherit (stdin/stdout/stderr) // Stream -> throw err for now diff --git a/src/js/node/net.ts b/src/js/node/net.ts index 624507183323..3ae325aa8698 100644 --- a/src/js/node/net.ts +++ b/src/js/node/net.ts @@ -386,12 +386,14 @@ function writeErrnoException(negErrno) { } return er; } -function endNT(socket, callback, err) { +function endNT(socket, callback, allowHalfOpen) { // Node's _final half-closes the writable side (sends FIN) and leaves the // readable side open; the Duplex's allowHalfOpen drives the eventual destroy. // https://github.com/nodejs/node/blob/614050b657e9757c1097aa85f92f2cb51149dc0d/lib/net.js#L500 - socket.shutdown(); - callback(err); + // A Windows named pipe cannot be half-closed: unless it is to stay half-open, + // it is closed once the other end has been silent for a moment, as in Node. + socket.shutdown(false, allowHalfOpen); + callback(); } function emitCloseNT(self, hasError) { self.emit("close", hasError); @@ -2113,6 +2115,7 @@ Socket.prototype.connect = function connect(...args) { data: this, fd: fd, ...(options.fdIsRawSocket === true ? { fdIsRawSocket: true } : {}), + ...(options.fdIsSpawnedPipe === true ? { fdIsSpawnedPipe: true } : {}), socket: SocketHandlers, // Always half-open natively; see kConnect. allowHalfOpen: true, @@ -2474,7 +2477,7 @@ Socket.prototype._final = function _final(callback) { if (!socket) return callback(); // emit FIN allowHalfOpen only allow the readable side to close first - process.nextTick(endNT, socket, callback); + process.nextTick(endNT, socket, callback, this.allowHalfOpen); }; Object.defineProperty(Socket.prototype, "localAddress", { diff --git a/src/js/node/tty.ts b/src/js/node/tty.ts index cad1915116f7..314f8b6fe8e6 100644 --- a/src/js/node/tty.ts +++ b/src/js/node/tty.ts @@ -56,45 +56,11 @@ Object.defineProperty(ReadStream, "prototype", { Prototype.setRawMode = function (flag) { flag = !!flag; - // On windows, this goes through the stream handle itself, as it must call - // uv_tty_set_mode on the uv_tty_t. - // - // On POSIX, I tried to use the same approach, but it didn't work reliably, - // so we just use the file descriptor and use termios APIs directly. - if (process.platform === "win32") { - // Special case for stdin, as it has a shared uv_tty handle - // and it's stream is constructed differently - if (this.fd === 0) { - const err = ttySetMode(flag); - if (err) { - this.emit("error", new Error("setRawMode failed with errno: " + err)); - return this; - } - } else { - const handle = this.$bunNativePtr; - if (!handle) { - this.emit("error", new Error("setRawMode failed because it was called on something that is not a TTY")); - return this; - } - - // If you call setRawMode before you call on('data'), the stream will - // not be constructed, leading to EBADF - // This corresponds to the `ensureConstructed` function in `native-readable.ts` - this.$start(); - - const err = handle.setRawMode(flag); - if (err) { - this.emit("error", err); - return this; - } - } - } else { - const state = (this[kRawModeState] ??= new Uint8Array(rawModeStateSize)); - const err = ttySetMode(this.fd, flag, state); - if (err) { - this.emit("error", new Error("setRawMode failed with errno: " + err)); - return this; - } + const state = (this[kRawModeState] ??= new Uint8Array(rawModeStateSize)); + const err = ttySetMode(this.fd, flag, state); + if (err) { + this.emit("error", new Error("setRawMode failed with errno: " + err)); + return this; } this.isRaw = flag; diff --git a/src/js/private.d.ts b/src/js/private.d.ts index 0cc104cad778..c96e17dc5579 100644 --- a/src/js/private.d.ts +++ b/src/js/private.d.ts @@ -96,7 +96,8 @@ declare module "bun" { // stdio entries, and `onDisconnect` told whether the channel closed cleanly. namespace Spawn { // `globalThis.`: inside this module the bare name is Bun's narrower `ArrayBufferView` alias. - type NodeStdio = (Bun.SpawnOptions.Writable | globalThis.ArrayBufferView | "ipc" | "socket-fd")[]; + // `"overlapped"`: a pipe whose child end is opened for overlapped I/O (Windows). + type NodeStdio = (Bun.SpawnOptions.Writable | globalThis.ArrayBufferView | "ipc" | "socket-fd" | "overlapped")[]; interface NodeSpawnOptions extends Omit< Bun.Spawn.SpawnOptions, diff --git a/src/jsc/Debugger.rs b/src/jsc/Debugger.rs index fdb9311bc9b7..f1c4de55abd2 100644 --- a/src/jsc/Debugger.rs +++ b/src/jsc/Debugger.rs @@ -87,7 +87,6 @@ pub type DebuggerId = bun_core::GenericIndex; #[derive(Copy, Clone, Eq, PartialEq)] pub enum Wait { Off, - Shortly, Forever, } @@ -188,7 +187,7 @@ struct DebuggerThreadInit { impl Debugger { /// `Debugger.waitForDebuggerIfNecessary(vm)` — block on the futex until /// `start()` (debugger thread) signals, then run the wait-loop until a - /// frontend connects (`Debugger__didConnect`) or the deadline elapses. + /// frontend connects (`Debugger__didConnect`). /// /// Aliasing: `this.debugger` is read through a raw pointer /// with fresh short-lived borrows because `event_loop().tick()` / @@ -241,64 +240,6 @@ impl Debugger { Bun__ensureDebugger(ctx_id, wait != Wait::Off); - // Sleep up to 30ms for automatic inspection. - const WAIT_FOR_CONNECTION_DELAY_MS: i64 = 30; - - let deadline: bun_core::Timespec = if wait == Wait::Shortly { - bun_core::Timespec::now(bun_core::TimespecMockMode::ForceRealTime) - .add_ms(WAIT_FOR_CONNECTION_DELAY_MS) - } else { - // Placeholder — never read on the `.forever` path. - bun_core::Timespec { sec: 0, nsec: 0 } - }; - - #[cfg(windows)] - { - // Arm a one-shot libuv timer that unrefs `poll_ref` after the - // delay (Windows lacks a working `tickWithTimeout`). TODO: remove - // this when tickWithTimeout actually works properly on Windows. - use bun_sys::windows::libuv as uv; - use bun_sys::windows::libuv::UvHandle as _; - if wait == Wait::Shortly { - let uv_loop = this.uv_loop(); - // SAFETY: `uv_loop` is a live initialized `uv_loop_t`. - unsafe { uv::uv_update_time(uv_loop) }; - let timer: *mut uv::Timer = - bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed())); - // SAFETY: `timer` freshly allocated; `uv_loop` valid. - unsafe { (*timer).init(uv_loop) }; - - extern "C" fn on_debugger_timer(handle: *mut uv::Timer) { - // SAFETY: `vm` is the per-thread singleton; called on the - // JS thread (libuv timer callback). Unwinding across - // `extern "C"` is UB so we early-return if no debugger. - if let Some(d) = VirtualMachine::get().as_mut().debugger.as_deref_mut() { - d.poll_ref.unref(get_vm_ctx(AllocatorType::Js)); - } - // SAFETY: `handle` is a live `uv_timer_t` (`uv_handle_t` - // at offset 0); `deinit_timer` matches `uv_close_cb`. - unsafe { - uv::uv_close(handle.cast(), Some(deinit_timer)); - } - } - extern "C" fn deinit_timer(handle: *mut uv::uv_handle_t) { - // SAFETY: `handle` is the `Box` allocated above - // (cast through `uv_handle_t` at offset 0); this is the - // sole owner reclaiming it after `uv_close` completes. - drop(unsafe { bun_core::heap::take(handle.cast::()) }); - } - // SAFETY: `timer` initialized above. - unsafe { - (*timer).start( - WAIT_FOR_CONNECTION_DELAY_MS as u64, - 0, - Some(on_debugger_timer), - ); - (*timer).ref_(); - } - } - } - // Drop the long-lived `&mut Debugger` before re-entering JS — see // the aliasing note on this fn. Each loop iteration re-fetches via `debugger_mut()` // so re-entrant JS may independently borrow the VM. @@ -328,37 +269,6 @@ impl Debugger { ); } } - Wait::Shortly => { - // Handle .incrementRefConcurrently - #[cfg(unix)] - { - let pending_unref = this.take_pending_unref(); - if pending_unref > 0 { - this.uws_loop_mut().unref_count(pending_unref); - } - } - - this.uws_loop_mut() - .tick_with_timeout(Some(&deadline), bun_uws::NOW_NS_UNKNOWN); - - if bun_core::Environment::ENABLE_LOGS { - bun_core::scoped_log!( - debugger, - "waited: {}ns", - (bun_core::time::nano_timestamp() - bun_core::start_time()) as i64 - ); - } - - let elapsed = - bun_core::Timespec::now(bun_core::TimespecMockMode::ForceRealTime); - if elapsed.order(&deadline) != core::cmp::Ordering::Less { - if let Some(d) = this.debugger_mut() { - d.poll_ref.unref(get_vm_ctx(AllocatorType::Js)); - } - bun_core::scoped_log!(debugger, "Timed out waiting for the debugger"); - break; - } - } Wait::Off => break, } } diff --git a/src/jsc/GarbageCollectionController.rs b/src/jsc/GarbageCollectionController.rs index 96ff316eafbc..86760cd779ad 100644 --- a/src/jsc/GarbageCollectionController.rs +++ b/src/jsc/GarbageCollectionController.rs @@ -214,11 +214,8 @@ impl GarbageCollectionController { let grew = vm_ref.jsc_vm().block_bytes_allocated() > prev_heap_size + idle_growth_slack; let (full, idle_gc_due_in) = this.idle_tick(vm_ref, grew, interval); this.perform_gc(full); - // Growth is activity; a shrinking heap is a collection (possibly the one requested above) doing its job. Where the - // event loop cannot let an idle collection finish while parked (Windows: Bun__JSC_onBeforeWait), it proceeds at - // this timer's ticks instead, fast ones for the next 30. - let needs_ticks_to_finish = full && cfg!(windows); - if !grew && !needs_ticks_to_finish { + // Growth is activity; a shrinking heap is a collection (possibly the one requested above) doing its job. + if !grew { let ticks = this .heap_size_didnt_change_for_repeating_timer_ticks_count .get() diff --git a/src/jsc/PosixSignalHandle.rs b/src/jsc/PosixSignalHandle.rs index 92a68668c57f..8afa38f2e14f 100644 --- a/src/jsc/PosixSignalHandle.rs +++ b/src/jsc/PosixSignalHandle.rs @@ -1,25 +1,19 @@ use core::sync::atomic::{AtomicBool, AtomicU8, AtomicU32, Ordering}; -use crate::JSGlobalObject; -#[cfg(unix)] use crate::VirtualMachineRef as VirtualMachine; -#[cfg(unix)] -use crate::{Task, event_loop::EventLoop}; +use crate::event_loop::EventLoop; +use crate::{JSGlobalObject, Task}; use bun_event_loop::{Taskable, task_tag}; -#[cfg(unix)] use bun_threading::SignalRing; -#[cfg(unix)] const BUFFER_SIZE: usize = 8192; /// Signal numbers queued by signal handlers on any thread for the main loop. -#[cfg(unix)] #[derive(Default)] pub struct PosixSignalHandle { ring: SignalRing, } -#[cfg(unix)] impl PosixSignalHandle { // `pub const new = bun.TrivialNew(@This());` pub(crate) fn new(init: Self) -> Box { @@ -45,44 +39,43 @@ impl PosixSignalHandle { /// This is the signal handler entry point. Calls enqueue on the ring buffer. /// Note: Must be minimal logic here. Only do atomics & signal-safe calls. +/// On Windows there is no signal context: the callers are the console control +/// handler's thread and whichever thread notices a console resize. #[unsafe(no_mangle)] -extern "C" fn Bun__onPosixSignal(number: i32) { +pub extern "C" fn Bun__onPosixSignal(number: i32) { + // Watch-mode SIGINT with no JS listener: node's watcher (its own + // process, idle loop) exits 0 immediately even when the script is + // busy; `_exit` is async-signal-safe, the queued path would not run. #[cfg(unix)] + if number == i32::from(SIGINT_NUMBER) + && WATCH_MODE_KILL_SIGNAL.load(Ordering::Relaxed) != 0 + && WATCH_SIGINT_LISTENERS.load(Ordering::Acquire) == 0 { - // Watch-mode SIGINT with no JS listener: node's watcher (its own - // process, idle loop) exits 0 immediately even when the script is - // busy; `_exit` is async-signal-safe, the queued path would not run. - if number == i32::from(SIGINT_NUMBER) - && WATCH_MODE_KILL_SIGNAL.load(Ordering::Relaxed) != 0 - && WATCH_SIGINT_LISTENERS.load(Ordering::Acquire) == 0 - { - // SAFETY: `_exit(2)` is async-signal-safe and takes no pointers. - unsafe { libc::_exit(0) }; - } - let Some(vm) = VirtualMachine::get_main_thread_vm() else { + // SAFETY: `_exit(2)` is async-signal-safe and takes no pointers. + unsafe { libc::_exit(0) }; + } + let Some(vm) = VirtualMachine::get_main_thread_vm() else { + return; + }; + // SAFETY: `vm` and its event loop are process-lifetime; raw place + // projection reads only the `signal_handler` slot (no `&EventLoop` + // formed — the main thread may hold `&mut EventLoop` concurrently). + let handler = unsafe { (*(*vm).event_loop()).signal_handler }; + if let Some(handler) = handler { + // `BackRef::deref` is the centralised set-once-NonNull proof; the + // pointee is all-atomic (`Sync`), so a `&PosixSignalHandle` from + // async-signal context is sound. + // No panic path in a signal handler: drop a number outside 1..=255. + let Some(signal) = u8::try_from(number).ok().filter(|&s| s != 0) else { return; }; - // SAFETY: `vm` and its event loop are process-lifetime; raw place - // projection reads only the `signal_handler` slot (no `&EventLoop` - // formed — the main thread may hold `&mut EventLoop` concurrently). - let handler = unsafe { (*(*vm).event_loop()).signal_handler }; - if let Some(handler) = handler { - // `BackRef::deref` is the centralised set-once-NonNull proof; the - // pointee is all-atomic (`Sync`), so a `&PosixSignalHandle` from - // async-signal context is sound. - // No panic path in a signal handler: drop a number outside 1..=255. - let Some(signal) = u8::try_from(number).ok().filter(|&s| s != 0) else { - return; - }; - if handler.enqueue(signal) { - // SAFETY: same process-lifetime event loop as above; `wakeup` - // is one async-signal-safe write to the loop's wakeup fd. - unsafe { (*(*vm).event_loop()).wakeup() }; - } + if handler.enqueue(signal) { + // SAFETY: same process-lifetime event loop as above; `wakeup` + // is one async-signal-safe write to the loop's wakeup fd (one + // completion packet posted to its port on Windows). + unsafe { (*(*vm).event_loop()).wakeup() }; } } - #[cfg(not(unix))] - let _ = number; } pub struct PosixSignalTask; @@ -211,16 +204,12 @@ impl PosixSignalTask { #[unsafe(no_mangle)] extern "C" fn Bun__ensureSignalHandler() { - #[cfg(unix)] - { - if let Some(vm) = VirtualMachine::get_main_thread_vm() { - // SAFETY: `vm` and its event loop are process-lifetime. - let this = unsafe { &mut *(*vm).event_loop() }; - if this.signal_handler.is_none() { - let boxed = PosixSignalHandle::new(PosixSignalHandle::default()); - this.signal_handler = - Some(bun_ptr::BackRef::from(bun_core::heap::into_raw_nn(boxed))); - } + if let Some(vm) = VirtualMachine::get_main_thread_vm() { + // SAFETY: `vm` and its event loop are process-lifetime. + let this = unsafe { &mut *(*vm).event_loop() }; + if this.signal_handler.is_none() { + let boxed = PosixSignalHandle::new(PosixSignalHandle::default()); + this.signal_handler = Some(bun_ptr::BackRef::from(bun_core::heap::into_raw_nn(boxed))); } } } diff --git a/src/jsc/RuntimeTranspilerCache.rs b/src/jsc/RuntimeTranspilerCache.rs index 1ba5bf2d76bb..5c22a88beadb 100644 --- a/src/jsc/RuntimeTranspilerCache.rs +++ b/src/jsc/RuntimeTranspilerCache.rs @@ -355,7 +355,7 @@ impl Entry { let mut total: usize = 0; for v in vecs { debug_assert!(v.len > 0); - // `uv_buf_t::len` is `ULONG` (u32) on Windows, `usize` on POSIX. + // `PlatformIoVec::len` is `ULONG` (u32) on Windows, `usize` on POSIX. total += v.len as usize; } debug_assert!(file_len == total); @@ -742,12 +742,8 @@ impl RuntimeTranspilerCache { input_stat_size: u64, ) -> crate::CrateResult { let mut metadata_bytes_buf = [0u8; Metadata::SIZE]; - // NONBLOCK: a FIFO must not block the open. On Windows it would make the handle overlapped. - #[cfg(unix)] - let open_flags = sys::O::RDONLY | sys::O::NONBLOCK; - #[cfg(not(unix))] - let open_flags = sys::O::RDONLY; - let cache_fd = sys::open(cache_file_path, open_flags, 0)?; + // NONBLOCK: a FIFO must not block the open. + let cache_fd = sys::open(cache_file_path, sys::O::RDONLY | sys::O::NONBLOCK, 0)?; let file = sys::File::from_fd(cache_fd); // On any error, delete the cache file. let unlink_guard = scopeguard::guard(cache_file_path, |p| { @@ -815,14 +811,7 @@ impl RuntimeTranspilerCache { if !dirname.is_empty() { let dir = sys::Dir::cwd().make_open_path(dirname, sys::OpenDirOptions::default())?; - let dfd = dir.into_raw(); - break 'brk match dfd.make_lib_uv_owned() { - Ok(f) => f, - Err(e) => { - dfd.close(); - return Err(e.into()); - } - }; + break 'brk dir.into_raw(); } break 'brk Fd::cwd(); diff --git a/src/jsc/VirtualMachine.rs b/src/jsc/VirtualMachine.rs index a01b4ec35595..f5b6b7d9d0e4 100644 --- a/src/jsc/VirtualMachine.rs +++ b/src/jsc/VirtualMachine.rs @@ -19,7 +19,7 @@ use crate::rare_data::RareData; use crate::saved_source_map::SavedSourceMap; use crate::{ self as jsc, Exception, JSGlobalObject, JSInternalPromise, JSValue, JsResult, OpaqueCallback, - PlatformEventLoop, ResolvedSource, VM, ZigException, + ResolvedSource, VM, ZigException, }; pub use crate::process_auto_killer as ProcessAutoKiller; @@ -171,11 +171,9 @@ pub struct VirtualMachine { /// both types are owned by `bun_runtime` (forward dep). Access goes through /// [`RuntimeHooks::timer_insert`] / [`RuntimeHooks::body_value_hive_ref`]. pub runtime_state: *mut c_void, - pub event_loop_handle: Option<*mut PlatformEventLoop>, + pub event_loop_handle: Option<*mut uws::Loop>, /// Pending `unref` count drained by the event-loop thread. Atomic because - /// `KeepAlive::unref_on_next_tick` increments it from OTHER threads - /// (POSIX only; on Windows that path calls `loop_dec()` instead and the - /// counter stays at zero). + /// `KeepAlive::unref_on_next_tick` increments it from OTHER threads. pub pending_unref_counter: core::sync::atomic::AtomicI32, pub preload: Vec>, pub unhandled_pending_rejection_to_capture: Option<*mut JSValue>, @@ -326,6 +324,7 @@ pub struct VirtualMachine { pub child_workers: Vec<*mut crate::web_worker::WebWorker>, /// This VM's live pool jobs (`bun_jsc::job`); JS thread only, zero-valid. pub(crate) jobs: crate::JsCell, + pub(crate) fd_jobs: crate::JsCell, /// The door out of this thread (`bun_jsc::vm_handle`): tickets for work /// that leaves it are taken here, and `teardown` waits on it. handle: core::mem::ManuallyDrop, @@ -993,8 +992,7 @@ impl VirtualMachine { VM.get() } - /// The signal handler path (unix only) reaches the main VM through this. - #[cfg(unix)] + /// The signal handler path reaches the main VM through this. pub(crate) fn get_main_thread_vm() -> Option<*mut VirtualMachine> { let p = MAIN_THREAD_VM.load(core::sync::atomic::Ordering::Acquire); if p.is_null() { None } else { Some(p) } @@ -1593,20 +1591,17 @@ impl VirtualMachine { unsafe { &mut *self.uws_loop() } } - /// Safe `&mut PlatformEventLoop` accessor for `event_loop_handle` (the - /// uws loop on POSIX, libuv loop on Windows). `None` only before - /// `ensure_waker()` runs. Consolidates the open-coded raw deref of - /// `self.event_loop_handle.unwrap()` at the `EventLoop::tick*` / - /// `update_counts` call sites into one SAFETY block. + /// `event_loop_handle` as `&mut uws::Loop`; `None` only before + /// `ensure_waker()` runs. /// /// Same single-JS-thread soundness contract as [`Self::uws_loop_mut`] — - /// the `PlatformEventLoop` is a separate heap allocation (uws/uv-owned), + /// the loop is a separate heap allocation (uws-owned), /// so the returned `&mut` cannot alias any field of `self`. #[inline(always)] #[allow(clippy::mut_from_ref)] - pub(crate) fn platform_loop_opt(&self) -> Option<&mut PlatformEventLoop> { + pub(crate) fn platform_loop_opt(&self) -> Option<&mut uws::Loop> { // SAFETY: when `Some`, `event_loop_handle` was set in `init()` / - // `ensure_waker()` to the live per-VM uws/uv loop and remains valid + // `ensure_waker()` to the live per-VM uws loop and remains valid // for the VM lifetime. Single-JS-thread invariant per `unsafe impl // Sync` — only the owning JS thread reborrows mutably. self.event_loop_handle.map(|h| unsafe { &mut *h }) @@ -1616,7 +1611,6 @@ impl VirtualMachine { /// `increment_pending_unref_counter()` from another thread can't be lost /// between the read and the reset. #[inline] - #[cfg(not(windows))] pub(crate) fn take_pending_unref(&self) -> i32 { self.pending_unref_counter .swap(0, core::sync::atomic::Ordering::Relaxed) @@ -1734,20 +1728,13 @@ impl VirtualMachine { /// `event_loop_handle.is_some()`. #[inline(always)] pub fn uws_loop(&self) -> *mut uws::Loop { - #[cfg(unix)] - { - debug_assert!( - self.event_loop_handle.is_some(), - "uws event_loop_handle is null" - ); - // SAFETY: set in `init()` on the JS thread before any host_fn / - // event-loop tick runs; never cleared while the VM is live. - unsafe { self.event_loop_handle.unwrap_unchecked() } - } - #[cfg(not(unix))] - { - uws::Loop::get() - } + debug_assert!( + self.event_loop_handle.is_some(), + "uws event_loop_handle is null" + ); + // SAFETY: set in `init()` on the JS thread before any host_fn / + // event-loop tick runs; never cleared while the VM is live. + unsafe { self.event_loop_handle.unwrap_unchecked() } } pub fn on_after_event_loop(&mut self) { @@ -2270,6 +2257,9 @@ impl VirtualMachine { self.event_loop_mut().deferred_tasks.run(); self.is_inside_deferred_task_queue.set(false); } + if self.is_main_thread() { + bun_threading::WorkPool::wait_for_writes(); + } self.is_shutting_down = true; @@ -2359,20 +2349,21 @@ impl VirtualMachine { /// requested), WebCore stop phase (child workers asked to terminate; /// ports/channels/WebSockets closed without events; listeners stripped), /// everything registered as stoppable closed natively (servers, - /// listeners, sockets, watchers, subprocess/pipe/tty handles, in-flight - /// fetch/S3/Bun.build aborted or cancelled), socket groups and the DNS - /// channel closed. + /// listeners, sockets, watchers, in-flight fetch/S3/Bun.build aborted + /// or cancelled), socket groups and the DNS channel closed. /// B. release — cron, user timers and GC-controller timers cancelled; child /// workers joined; this VM's sqlite connections closed; **the wait**: /// every ticket held off-thread comes back while queued work is /// released without running; the handle closes; main: HTTP thread - /// parked; RareData's JS handles released. + /// parked; (Windows) writes still out let go of; RareData's JS handles + /// released. /// C. JSC VM destroyed (finalizers close what only they own; JSC's RunLoop /// timers ride the timer heap until ~VM returns); sockets they closed - /// drained; then the timer heap's loop handles closed. - /// D. worker: keep-alive delta folded, uSockets loop freed and (Windows) - /// the uv loop closed — every handle unlinks while its owner is still - /// allocated. Main proceeds to process exit instead. + /// drained. + /// D. worker: keep-alive delta folded, (Windows) the pipes, consoles and + /// process-exit waits still open on the loop closed, uSockets loop + /// freed — while their owners are still allocated. Main proceeds to + /// process exit instead. /// E. `destroy()`: RareData, runtime state, event loop. /// /// # Safety @@ -2397,44 +2388,25 @@ impl VirtualMachine { Zig__GlobalObject__prepareForDestruction(vm.global()); // SAFETY: fn contract. let sweep = || unsafe { - let _ = Self::stop_phase_sweep(this, kind); - let second = Self::stop_phase_sweep(this, kind); + let _ = Self::stop_phase_sweep(this); + let second = Self::stop_phase_sweep(this); debug_assert!( second == SweepResult::Idle, "a native close path registered a stoppable resource during teardown" ); }; sweep(); - // A worker closes its uv loop below (D), so requests still in flight - // complete here, against this live VM: their handles were just closed, - // so what remains finishes on its own (threadpool work), and a - // completion may start more — open a handle, schedule pool work (still - // accepted, and awaited in B) — hence sweep again after each drain. - // The exiting main thread neither closes its loop nor may nest uv_run - // here: process.exit() can be running inside a libuv completion callback. - #[cfg(windows)] - if matches!(kind, Teardown::Worker) { - while bun_sys::windows::libuv::Loop::drain_requests() { - sweep(); - } - } teardown_log!("teardown: stopped"); // ---- B. release ------------------------------------------------------ - #[cfg(windows)] - if let Some(t) = vm.event_loop_mut().forever_timer.take() { - // SAFETY: live usockets timer from `hold_forever_poll`; closed like - // any us_timer (freed by its close callback when the loop turns). - unsafe { uws::Timer::close::(t.as_ptr()) }; - } if let Some(hooks) = hooks { // SAFETY: fn contract (statement-scoped exclusive access). (hooks.stop_cron_for_vm_teardown)(unsafe { &mut *this }); // Drop every TimeoutObject/ImmediateObject's heap node, JS pin and // +1 while runtime state and the JSC heap are alive. The heap itself - // (and the loop handle it embeds) stays up: JSC's own RunLoop timers - // — GC activity callbacks, sweeper, deferred work — are WTFTimers on - // this heap and keep being scheduled until ~VM returns. + // stays up: JSC's own RunLoop timers (GC activity callbacks, sweeper, + // deferred work) are WTFTimers on this heap and keep being scheduled + // until ~VM returns. // SAFETY: fn contract. unsafe { (hooks.cancel_timers)(this, None) }; // And unlink every other kind of EventLoopTimer (socket timeouts, @@ -2453,7 +2425,7 @@ impl VirtualMachine { // checkpoint and close, before finalizers could. vm.close_sqlite_databases_for_exit(); // The one place the invariant is enforced: nothing below runs until - // everything that left this thread (pool jobs, fetches, uv work, C++ + // everything that left this thread (pool jobs, fetches, C++ // work-queue tasks, child threads) has come back. Whatever arrives // meanwhile — and whatever was already queued — is released here, on // this thread with the heap alive, never run. @@ -2467,7 +2439,7 @@ impl VirtualMachine { // SAFETY: fn contract (statement-scoped exclusive access). vm.handle.close_and_wait(|| unsafe { (*this).release_queued_work(); - let _ = Self::stop_phase_sweep(this, kind); + let _ = Self::stop_phase_sweep(this); }); // The exiting main thread now parks the process-wide HTTP thread — // after the children it also served are joined and this VM's own @@ -2477,6 +2449,10 @@ impl VirtualMachine { teardown_log!("teardown: HTTP thread unresponsive; skipping to process exit"); return; } + // The loop does not turn again: what a writer holds until the result + // of its write arrives is released here, with the heap alive. + #[cfg(windows)] + bun_io::abandon_writes_out(); // SAFETY: fn contract (statement-scoped exclusive access). unsafe { if let Some(rare) = (*this).rare_data.as_deref_mut() { @@ -2493,19 +2469,11 @@ impl VirtualMachine { // Finalizers just closed the sockets only they owned; `us_socket_close` // queues onto `loop->data.closed_head`, normally freed on the next tick. vm.uws_loop_mut().drain_closed_sockets(); - // Nothing schedules a WTFTimer any more: the timer heap's loop handles - // can go (Windows uv_timer/uv_idle), before the loop close unlinks them. - if let Some(hooks) = hooks { - // SAFETY: fn contract; runtime state still installed. - unsafe { (hooks.close_timer_loop_handles_after_vm_destroyed)(this) }; - } teardown_log!("teardown: JSC VM destroyed"); // ---- D. loops (worker; main exits the process instead) ---------------- if matches!(kind, Teardown::Worker) { - // Whatever B/C unref'd through the concurrent counter, folded now: - // on Windows it shares `active_handles` with libuv and a residue - // would keep the loop close below spinning. + // Whatever B/C unref'd through the concurrent counter, folded now. vm.event_loop_mut().apply_concurrent_ref_delta(); // SAFETY: fn contract (statement-scoped exclusive access). if let Some(rare) = unsafe { (*this).rare_data.as_deref_mut() } { @@ -2515,9 +2483,6 @@ impl VirtualMachine { unsafe { (*vm.uws_loop()).internal_loop_data.jsc_vm = core::ptr::null_mut() }; bun_uws::free_thread_loop(); teardown_log!("teardown: uSockets loop freed"); - #[cfg(windows)] - bun_sys::windows::libuv::Loop::close_thread_loop(); - teardown_log!("teardown: loops closed"); } // ---- E. free owners -------------------------------------------------- @@ -2528,13 +2493,12 @@ impl VirtualMachine { impl VirtualMachine { /// One stop-phase sweep: armed handles (servers, listeners, watchers, - /// duplex/named-pipe sockets, resolvers), a worker's uv stream/process - /// handles, every socket group, the VM-global dns channel. Reports whether - /// it found anything. + /// duplex/named-pipe sockets, resolvers), every socket group, the + /// VM-global dns channel. Reports whether it found anything. /// /// # Safety /// As [`teardown`](Self::teardown): sole owner on the owning thread, heap alive. - unsafe fn stop_phase_sweep(this: *mut Self, kind: Teardown) -> SweepResult { + unsafe fn stop_phase_sweep(this: *mut Self) -> SweepResult { let hooks = runtime_hooks(); let mut result = SweepResult::Idle; // Pool jobs parked on something external (a pipe that never becomes @@ -2548,18 +2512,6 @@ impl VirtualMachine { None => (*this).stop_context_handles(crate::StopReason::VmTeardown), } }); - // A worker's uv loop is closed in D, so every pipe / tty / child-process - // handle open on it closes now — through whoever drives it (reader, - // writer, IPC channel, named pipe, Process), or directly if nothing - // adopted it — so pending writes complete (ECANCELED) against a live VM - // and no request on them can hold up the loop drain in B. The exiting - // main thread keeps its loop (the OS reclaims the handles); sweeping - // them there only re-enters stream owners under still-running script. - #[cfg(windows)] - if matches!(kind, Teardown::Worker) { - bun_sys::windows::libuv::open_handles::stop_all_for_vm_teardown(); - } - let _ = kind; // `close_all_socket_groups` walks the loop's group list through the VM // and never touches `rare_data`, so the two accesses are disjoint. // SAFETY: fn contract. @@ -2908,13 +2860,6 @@ pub struct RuntimeHooks { pub stop_active_handles_for_vm_teardown: unsafe fn(vm: *mut VirtualMachine) -> SweepResult, /// Teardown only (never on a live VM): unlink every remaining EventLoopTimer. pub disarm_all_timers_for_vm_teardown: unsafe fn(vm: *mut VirtualMachine), - /// Teardown-only, after ~VM (JSC's RunLoop timers use the heap until then): - /// close the loop handles the timer heap embeds (Windows uv_timer/uv_idle) - /// so the loop close unlinks them before the runtime state is freed. - /// - /// # Safety - /// JS thread; `runtime_state` installed. - pub close_timer_loop_handles_after_vm_destroyed: unsafe fn(vm: *mut VirtualMachine), } /// Canonical `EventLoopCtx` vtable for a `*mut VirtualMachine` owner — the JS @@ -3209,6 +3154,7 @@ impl VirtualMachine { // canonical empty value via `ptr::write` (no Drop of zeroed bytes). addr_of_mut!((*vm).preload).write(Vec::new()); addr_of_mut!((*vm).child_workers).write(Vec::new()); + addr_of_mut!((*vm).fd_jobs).write(Default::default()); addr_of_mut!((*vm).handle) .write(core::mem::ManuallyDrop::new(crate::VmHandle::new(vm))); addr_of_mut!((*vm).argv).write(Vec::new()); @@ -4156,20 +4102,6 @@ impl VirtualMachine { .unwrap_or(UnhandledRejections::Bun) } - /// Returns this VM's libuv event loop handle (must already be initialized). - pub fn uv_loop(&self) -> *mut Async::Loop { - #[cfg(debug_assertions)] - { - return self - .event_loop_handle - .expect("libuv event_loop_handle is null"); - } - #[cfg(not(debug_assertions))] - { - self.event_loop_handle.unwrap() - } - } - /// Whether TLS certificate verification is enforced, from the cached override or the env loader. pub fn get_tls_reject_unauthorized(&self) -> bool { if let Some(v) = self.default_tls_reject_unauthorized { @@ -4264,7 +4196,7 @@ impl VirtualMachine { { Some(fd) => { self.pending_ipc = Some(PendingIpc { - fd: bun_sys::Fd::from_uv(fd), + fd: bun_sys::Fd::from_crt(fd), advanced, }) } @@ -4287,7 +4219,7 @@ impl VirtualMachine { // depend on. #[cfg(unix)] if map.get(b"BUN_FEATURE_FLAG_FORCE_WAITER_THREAD").is_some() { - bun_spawn::process::WaiterThread::set_should_use_waiter_thread(); + bun_spawn::waiter_thread_flag::set(); } // Only allowed for testing if map.get(b"BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING").is_some() { @@ -5471,6 +5403,7 @@ impl VirtualMachine { drop(core::mem::take(&mut self.resolved_path_dups)); drop(core::mem::take(&mut self.main_resolved_path)); + drop(self.fd_jobs.take()); self.overridden_main.deinit(); self.pending_internal_promise.deinit(); diff --git a/src/jsc/array_buffer.rs b/src/jsc/array_buffer.rs index 149d4671e90c..2e94f84cbb39 100644 --- a/src/jsc/array_buffer.rs +++ b/src/jsc/array_buffer.rs @@ -263,7 +263,7 @@ impl ArrayBuffer { // It creates a new memory mapping. // If there is a lot of repetitive memory allocations in a tight loop, it performs poorly. // So we clone it when it's small. - // `stat.st_size` is `i64` on POSIX, `u64` on the libuv stat struct. + // `stat.st_size` is `i64` on POSIX, `u64` on Windows. if (size as i64) < Self::MMAP_THRESHOLD as i64 { let result = Self::to_js_buffer_from_fd(fd, usize::try_from(size).expect("int cast"), global); diff --git a/src/jsc/bindings/BunJSCEventLoop.cpp b/src/jsc/bindings/BunJSCEventLoop.cpp index f30711a7741d..12425ee731d8 100644 --- a/src/jsc/bindings/BunJSCEventLoop.cpp +++ b/src/jsc/bindings/BunJSCEventLoop.cpp @@ -6,20 +6,6 @@ #include #include -#if USE(MIMALLOC) -// Matches oven-sh/mimalloc's mi_attr_noexcept declaration; bmalloc's -// vendored mimalloc.h predates this entry point. -extern "C" void mi_on_thread_idle(void) noexcept; -#if OS(WINDOWS) -extern "C" bool mi_on_thread_idle_pending(void) noexcept; -#else -// uSockets' CLOCK_MONOTONIC reading (packages/bun-usockets/src/loop.c). Must be -// the same clock the caller's `nowNs` came from, or the rate limit below -// compares two epochs. Windows always passes a reading, so it needs no fallback. -extern "C" uint64_t us_internal_monotonic_ns(void); -#endif -#endif - // Rust-side `AtomicI32` static (src/jsc/VirtualMachine.rs). Same layout as a plain // int32_t, but Rust writes it (env parsing) while this thread reads it, so read // it as an atomic rather than through a plain `int`. @@ -30,19 +16,15 @@ extern "C" void Bun__JSC_acquireHeapAccessAfterWait(JSC::VM* _Nonnull vm) vm->heap.acquireAccess(); } -// Returns in how many milliseconds the caller is to call this again even if nothing else -// wakes it (0: no need). Only the libuv loop gets anything but 0. -// -// `releasedHeapAccess` (null where the loop cannot take access back before it dispatches: libuv): set when this gave up -// heap access for the wait, in which case Bun__JSC_acquireHeapAccessAfterWait must run before anything touches the JS -// heap. It does that while an idle collection GarbageCollectionController requested is unfinished -// (JSVMClientData::idleCollectionsPending): a requested collection only advances at this thread's safepoints while this +// `releasedHeapAccess`: set when this gave up heap access for the wait, in which case +// Bun__JSC_acquireHeapAccessAfterWait must run before anything touches the JS heap. It does that while an idle +// collection GarbageCollectionController requested is unfinished (JSVMClientData::idleCollectionsPending): a +// requested collection only advances at this thread's safepoints while this // thread holds the collector's conn, and a parked thread has none; without access, the conn goes to the collector thread, // which finishes the collection while this one sleeps. -extern "C" unsigned int Bun__JSC_onBeforeWait(JSC::VM* _Nonnull vm, uint64_t nowNs, int* _Nullable releasedHeapAccess) +extern "C" void Bun__JSC_onBeforeWait(JSC::VM* _Nonnull vm, int* _Nonnull releasedHeapAccess) { ASSERT(vm); - unsigned int runAgainInMs = 0; const bool previouslyHadAccess = vm->heap.hasHeapAccess(); // sanity check for debug builds to ensure we're not doing a // use-after-free here @@ -86,20 +68,11 @@ extern "C" unsigned int Bun__JSC_onBeforeWait(JSC::VM* _Nonnull vm, uint64_t now static thread_local int remainingRunsUntilSkipReleaseAccess = 0; -#if USE(MIMALLOC) && OS(WINDOWS) - // mimalloc wants three more calls after the first at the most; counted from the last time JS ran. - static constexpr unsigned maxIdleFollowUpSweeps = 3; - static thread_local unsigned idleFollowUpSweeps = 0; -#endif - // Note: usage of `didEnterVM` in JSC::VM conflicts with Options::validateDFGClobberize // We don't need to use that option, so it should be fine. if (vm->didEnterVM) { vm->didEnterVM = false; remainingRunsUntilSkipReleaseAccess = defaultRemainingRunsUntilSkipReleaseAccess; -#if USE(MIMALLOC) && OS(WINDOWS) - idleFollowUpSweeps = 0; -#endif } if (remainingRunsUntilSkipReleaseAccess-- > 0) { @@ -107,37 +80,13 @@ extern "C" unsigned int Bun__JSC_onBeforeWait(JSC::VM* _Nonnull vm, uint64_t now // > If you are not moving a VM to the different thread, then you can aquire the access and do not need to release vm->heap.stopIfNecessary(); vm->didEnterVM = false; - -#if USE(MIMALLOC) && OS(WINDOWS) - // Collect retired pages, punch free-block holes, hand the arena purge to - // the scavenger. Rate-limited; nowNs is the tick's shared reading (0 = take - // one), compared by addition so an out-of-order reading cannot underflow. - // - // Windows only: everywhere else `us_loop_run_bun_tick` hands the heaps to the - // scavenger across the poll instead, so this thread never does the sweep itself. - // The libuv loop has no handoff: uv_run() dispatches completions right after its poll, - // inside the same call, and libuv allocates from mimalloc. So it keeps paying for it here. - static constexpr uint64_t idleSweepIntervalNs = 100 * 1000000ULL; - static thread_local uint64_t lastIdleSweepNs = 0; - if (nowNs >= lastIdleSweepNs + idleSweepIntervalNs) { - lastIdleSweepNs = nowNs; - mi_on_thread_idle(); - // One sweep leaves the free blocks of a large page that was just allocated - // from, and nothing comes back for them once libuv blocks. - if (mi_on_thread_idle_pending() && idleFollowUpSweeps < maxIdleFollowUpSweeps) { - idleFollowUpSweeps++; - runAgainInMs = idleSweepIntervalNs / 1000000; - } - } -#endif } } - if (releasedHeapAccess && previouslyHadAccess && WebCore::clientData(*vm)->idleCollectionsPending.load()) { + if (previouslyHadAccess && WebCore::clientData(*vm)->idleCollectionsPending.load()) { // If the collector handed the conn back for a stop-the-world phase while this thread was awake, run that phase // now rather than at the next time JS happens to run; then park without access so the rest goes on without us. vm->heap.stopIfNecessary(); vm->heap.releaseAccess(); *releasedHeapAccess = 1; } - return runAgainInMs; } diff --git a/src/jsc/bindings/BunProcess.cpp b/src/jsc/bindings/BunProcess.cpp index efd62913ade4..5fd90f1cabcd 100644 --- a/src/jsc/bindings/BunProcess.cpp +++ b/src/jsc/bindings/BunProcess.cpp @@ -84,12 +84,14 @@ #include #include #else -#include +#include "BunWindowsProcess.h" +#include "BunWindowsSignals.h" +#include +#include #include #include -// Using the same typedef and define for `mode_t` and `umask` as node on windows. +// Using the same typedef for `mode_t` as node on windows. // https://github.com/nodejs/node/blob/ad5e2dab4c8306183685973387829c2f69e793da/src/node_process_methods.cc#L29 -#define umask _umask typedef int mode_t; #endif #include "JSNextTickQueue.h" @@ -149,7 +151,10 @@ extern "C" bool Bun__getEnvValue(JSC::JSGlobalObject* globalObject, const Encode extern "C" bool Bun__Node__ProcessThrowDeprecation; extern "C" bool Bun__Node__ProcessPendingDeprecation; extern "C" void Bun__writeProfilesBeforeSelfKill(bool signalEndsProcess); -#if !OS(WINDOWS) +#if OS(WINDOWS) +// 0, or the negative UV_E* number libuv's uv_kill() reports. +extern "C" int Bun__Process__kill(int pid, int signum); +#else extern "C" void onExitSignal(int); #endif extern "C" int32_t bun_stdio_tty[3]; @@ -295,18 +300,13 @@ static JSValue constructVersions(VM& vm, JSObject* processObject) // Use commit hash for zstd (semantic version extraction not working yet) { "zstd", BUN_VERSION_ZSTD_HASH }, { "v8", REPORTED_NODEJS_V8_VERSION }, -#if !OS(WINDOWS) - { "uv", "1.48.0" }, -#endif + { "uv", REPORTED_NODEJS_UV_VERSION }, }; auto putVersion = [&](const char* name, String&& version) { object->putDirect(vm, JSC::Identifier::fromString(vm, ASCIILiteral::fromLiteralUnsafe(name)), JSC::jsOwnedString(vm, version), 0); }; for (auto& entry : versions) putVersion(entry.name, String(ASCIILiteral::fromLiteralUnsafe(entry.version))); -#if OS(WINDOWS) - putDirectNamed(vm, object, "uv"_s, JSValue(JSC::jsOwnedString(vm, String::fromLatin1(uv_version_string())))); -#endif #define STRINGIFY_IMPL(x) #x #define STRINGIFY(x) STRINGIFY_IMPL(x) putVersion("napi", STRINGIFY(NODE_API_SUPPORTED_VERSION_MAX) ""_s); @@ -817,12 +817,13 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__(( return JSValue::encode(resultValue); } +extern "C" uint32_t Bun__getUmask(); +extern "C" uint32_t Bun__setUmask(uint32_t); + JSC_DEFINE_HOST_FUNCTION(Process_functionUmask, (JSGlobalObject * globalObject, CallFrame* callFrame)) { if (callFrame->argumentCount() == 0 || callFrame->argument(0).isUndefined()) { - mode_t currentMask = umask(0); - umask(currentMask); - return JSValue::encode(jsNumber(currentMask)); + return JSValue::encode(jsNumber(Bun__getUmask())); } auto& vm = JSC::getVM(globalObject); @@ -843,7 +844,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionUmask, (JSGlobalObject * globalObject, newUmask = JSC::toUInt32(value.asNumber()); } - return JSC::JSValue::encode(JSC::jsNumber(umask(newUmask))); + return JSC::JSValue::encode(JSC::jsNumber(Bun__setUmask(static_cast(newUmask)))); } extern "C" uint64_t Bun__readOriginTimer(void*); @@ -1041,13 +1042,8 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionChdir, (JSC::JSGlobalObject * globalObj static HashMap* signalNumberToNameMap = nullptr; static HashMap* signalNameToNumberMap = nullptr; -// On windows, signals need to have a handle to the uv_signal_t. When sigaction is used, this is kept track globally for you. -struct SignalHandleValue { -#if OS(WINDOWS) - uv_signal_t* handle; -#endif -}; -static HashMap* signalToContextIdsMap = nullptr; +// The signals that have a process.on() listener and an installed handler. +static HashSet* signalsWithListeners = nullptr; static const NeverDestroyed* getSignalNames() { @@ -1098,7 +1094,7 @@ static void loadSignalNumberMap() signalNameToNumberMap = new HashMap(); signalNameToNumberMap->reserveInitialCapacity(31); #if OS(WINDOWS) - // libuv-supported console-control signals on Windows: + // The signals Node can watch or send on Windows: // CTRL_C_EVENT → SIGINT, CTRL_BREAK_EVENT → SIGBREAK, // CTRL_CLOSE_EVENT → SIGHUP, plus SIGWINCH on console resize. signalNameToNumberMap->add(signalNames[0], SIGHUP); @@ -1294,34 +1290,6 @@ extern "C" bool Bun__onSignalForJS(int signalNumber, Zig::GlobalObject* globalOb return process->wrapped().emitForBindings(signalNameIdentifier, args); } -#if OS(WINDOWS) -extern "C" uv_signal_t* Bun__UVSignalHandle__init(JSC::JSGlobalObject* lexicalGlobalObject, int signalNumber, void (*callback)(uv_signal_t*, int)); -extern "C" uv_signal_t* Bun__UVSignalHandle__close(uv_signal_t*); -#endif - -#if !OS(WINDOWS) -void signalHandler(int signalNumber) -#else -void signalHandler(uv_signal_t* signal, int signalNumber) -#endif -{ -#if OS(WINDOWS) - if (signalNumberToNameMap->find(signalNumber) == signalNumberToNameMap->end()) [[unlikely]] - return; - - auto* context = ScriptExecutionContext::getMainThreadScriptExecutionContext(); - if (!context) [[unlikely]] - return; - // uv_signal_t callbacks fire on the uv_run thread (JS thread), but defer to avoid - // re-entering JS from inside the libuv poll loop - context->postTaskConcurrently([signalNumber](ScriptExecutionContext& context) { - Bun__onSignalForJS(signalNumber, uncheckedDowncast(context.jsGlobalObject())); - }); -#else - -#endif -}; - extern "C" void Bun__logUnhandledException(JSC::EncodedJSValue exception); extern "C" int Bun__handleUncaughtException(JSC::JSGlobalObject* lexicalGlobalObject, JSC::JSValue exception, int isRejection) @@ -1548,12 +1516,14 @@ extern "C" bool Bun__isMainThreadVM(); extern "C" void Bun__onPosixSignal(int signalNumber); extern "C" void Bun__onSignalListenerCountChanged(int signalNumber, int listenerCount); +#if !OS(WINDOWS) __attribute__((noinline)) static void forwardSignal(int signalNumber) { // We want a function that's equivalent to Bun__onPosixSignal but whose address is different. // This is so that we can be sure not to uninstall signal handlers that we didn't install here. Bun__onPosixSignal(signalNumber); } +#endif // `bun run --watch` keeps this signal's handler installed for the process // lifetime (node's watcher process owns SIGINT the same way), so the @@ -1578,6 +1548,11 @@ extern "C" void Bun__installWatchModeSignalHandler(int signalNumber) watchModeStickySignal = signalNumber; installForwardSignalHandler(signalNumber); } +#else +// Windows has no signal handlers: the console control handler +// (windows_signal.rs) reports watched events through Bun__onPosixSignal. +extern "C" void Bun__watchWindowsSignal(int signalNumber); +extern "C" void Bun__unwatchWindowsSignal(int signalNumber); #endif extern "C" void Bun__MemoryPressure__install(JSC::JSGlobalObject* global); @@ -1627,8 +1602,8 @@ static void onDidChangeListeners(EventEmitter& eventEmitter, const Identifier& e loadSignalNumberMap(); loadSignalNumberToNameMap(); - if (!signalToContextIdsMap) { - signalToContextIdsMap = new HashMap(); + if (!signalsWithListeners) { + signalsWithListeners = new HashSet(); } if (auto signalNumber = signalNameToNumberMap->get(eventName.string())) { @@ -1650,31 +1625,19 @@ static void onDidChangeListeners(EventEmitter& eventEmitter, const Identifier& e #endif if (isAdded) { - if (!signalToContextIdsMap->contains(signalNumber)) { - SignalHandleValue signal_handle = { -#if OS(WINDOWS) - .handle = nullptr, -#endif - }; -#if !OS(WINDOWS) + if (!signalsWithListeners->contains(signalNumber)) { Bun__ensureSignalHandler(); +#if !OS(WINDOWS) installForwardSignalHandler(signalNumber); #else - signal_handle.handle = Bun__UVSignalHandle__init( - eventEmitter.scriptExecutionContext()->jsGlobalObject(), - signalNumber, - &signalHandler); - - if (!signal_handle.handle) [[unlikely]] - return; + Bun__watchWindowsSignal(signalNumber); #endif - - signalToContextIdsMap->set(signalNumber, signal_handle); + signalsWithListeners->add(signalNumber); } } else { - if (signalToContextIdsMap->find(signalNumber) != signalToContextIdsMap->end() && listenerCount == 0) { + if (signalsWithListeners->contains(signalNumber) && listenerCount == 0) { // The watch-mode sticky signal keeps its OS handler installed; only the - // handler teardown is skipped. The map entry is still removed — it is the + // handler teardown is skipped. The set entry is still removed — it is the // "has JS listeners" source of truth that e.g. self-kill flush consults. if (signalNumber != watchModeStickySignal) { #if !OS(WINDOWS) @@ -1683,11 +1646,10 @@ static void onDidChangeListeners(EventEmitter& eventEmitter, const Identifier& e signal(signalNumber, oldHandler); } #else - SignalHandleValue signal_handle = signalToContextIdsMap->get(signalNumber); - Bun__UVSignalHandle__close(signal_handle.handle); + Bun__unwatchWindowsSignal(signalNumber); #endif } - signalToContextIdsMap->remove(signalNumber); + signalsWithListeners->remove(signalNumber); } } } @@ -3014,28 +2976,13 @@ static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, JSC: ASSERT_WITH_MESSAGE(JSC::isJSArray(result), "Expected an array from getStdioWriteStream"); JSC::JSArray* resultObject = uncheckedDowncast(result); - // process.stdout and process.stderr differ from other Node.js streams in important ways: - // 1. They are used internally by console.log() and console.error(), respectively. - // 2. Writes may be synchronous depending on what the stream is connected to and whether the system is Windows or POSIX: - // Files: synchronous on Windows and POSIX - // TTYs (Terminals): asynchronous on Windows, synchronous on POSIX - // Pipes (and sockets): synchronous on Windows, asynchronous on POSIX - bool forceSync = false; -#if OS(WINDOWS) - forceSync = fdType == BunProcessStdinFdType::file || fdType == BunProcessStdinFdType::pipe; -#else - // Note: files are always sync anyway. - // forceSync = fdType == BunProcessStdinFdType::file || bun_stdio_tty[fd]; - + // process.stdout and process.stderr are used internally by console.log() and console.error(), and + // their writes are synchronous whatever they are connected to. // TODO: once console.* is wired up to write/read through the same buffering mechanism as FileSink for process.stdout, process.stderr, we can make this non-blocking for sockets on POSIX. // Until then, we have to force it to be sync EVEN for sockets or else console.log() may flush at a different time than process.stdout.write. - forceSync = true; -#endif - if (forceSync) { - JSValue sink = resultObject->getIndex(globalObject, 1); - RETURN_IF_EXCEPTION(scope, jsUndefined()); - Bun__ForceFileSinkToBeSynchronousForProcessObjectStdio(globalObject, JSValue::encode(sink)); - } + JSValue sink = resultObject->getIndex(globalObject, 1); + RETURN_IF_EXCEPTION(scope, jsUndefined()); + Bun__ForceFileSinkToBeSynchronousForProcessObjectStdio(globalObject, JSValue::encode(sink)); JSValue stream = resultObject->getIndex(globalObject, 0); RETURN_IF_EXCEPTION(scope, jsUndefined()); @@ -3138,7 +3085,7 @@ JSC_DEFINE_CUSTOM_GETTER(processPpid, (JSC::JSGlobalObject * globalObject, JSC:: // (e.g. after the original parent dies and the child is // reparented to init). Matches Node.js behavior. #if OS(WINDOWS) - return JSValue::encode(jsNumber(uv_os_getppid())); + return JSValue::encode(jsNumber(Bun__getParentProcessId())); #else return JSValue::encode(jsNumber(getppid())); #endif @@ -3882,9 +3829,8 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionResourceUsage, (JSC::JSGlobalObject * g return {}; } #else - uv_rusage_t rusage; - int err = uv_getrusage(&rusage); - if (err) { + Bun::ResourceUsage rusage; + if (int err = Bun::getResourceUsage(rusage)) { throwSystemError(throwScope, globalObject, "uv_getrusage"_s, err); return {}; } @@ -3926,28 +3872,29 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionCpuUsage, (JSC::JSGlobalObject * global { auto& vm = JSC::getVM(globalObject); auto throwScope = DECLARE_THROW_SCOPE(vm); + double user, system; #if !OS(WINDOWS) struct rusage rusage; if (getrusage(RUSAGE_SELF, &rusage) != 0) { throwSystemError(throwScope, globalObject, "Failed to get CPU usage"_s, "getrusage"_s, errno); return {}; } + user = std::chrono::microseconds::period::den * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; + system = std::chrono::microseconds::period::den * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec; #else - uv_rusage_t rusage; - int err = uv_getrusage(&rusage); - if (err) { + Bun::CpuTimes cpuTimes; + if (int err = Bun::getProcessCpuTimes(cpuTimes)) { throwSystemError(throwScope, globalObject, "Failed to get CPU usage"_s, "uv_getrusage"_s, err); return {}; } + user = cpuTimes.user; + system = cpuTimes.system; #endif auto* process = getProcessObject(globalObject, callFrame->thisValue()); Structure* cpuUsageStructure = process->cpuUsageStructure(); - double user = std::chrono::microseconds::period::den * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; - double system = std::chrono::microseconds::period::den * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec; - if (callFrame->argumentCount() > 0) { JSValue comparatorValue = callFrame->argument(0); if (!comparatorValue.isUndefined()) { @@ -4054,9 +4001,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionThreadCpuUsage, (JSC::JSGlobalObject * user = 1e6 * info.user_time.seconds + info.user_time.microseconds; system = 1e6 * info.system_time.seconds + info.system_time.microseconds; #elif OS(LINUX) || OS(FREEBSD) - // FreeBSD has supported RUSAGE_THREAD since 8.1; the #else branch must - // stay Windows-only because uv_getrusage_thread is an aborting stub in - // uv-posix-stubs.c on the POSIX targets that link it. + // FreeBSD has supported RUSAGE_THREAD since 8.1. struct rusage threadUsage; if (getrusage(RUSAGE_THREAD, &threadUsage) != 0) { throwSystemError(throwScope, globalObject, "Failed to get thread CPU usage"_s, "getrusage"_s, errno); @@ -4064,15 +4009,16 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionThreadCpuUsage, (JSC::JSGlobalObject * } user = 1e6 * threadUsage.ru_utime.tv_sec + threadUsage.ru_utime.tv_usec; system = 1e6 * threadUsage.ru_stime.tv_sec + threadUsage.ru_stime.tv_usec; -#else - uv_rusage_t threadUsage; - int err = uv_getrusage_thread(&threadUsage); - if (err) { +#elif OS(WINDOWS) + Bun::CpuTimes threadTimes; + if (int err = Bun::getThreadCpuTimes(threadTimes)) { throwSystemError(throwScope, globalObject, "Failed to get thread CPU usage"_s, "uv_getrusage_thread"_s, err); return {}; } - user = 1e6 * threadUsage.ru_utime.tv_sec + threadUsage.ru_utime.tv_usec; - system = 1e6 * threadUsage.ru_stime.tv_sec + threadUsage.ru_stime.tv_usec; + user = threadTimes.user; + system = threadTimes.system; +#else +#error "Unknown platform" #endif user -= userComparator; @@ -4174,7 +4120,11 @@ extern "C" int getRSS(size_t* rss) *rss = static_cast(kinfo.ki_rssize) * static_cast(getpagesize()); return 0; #elif OS(WINDOWS) - return uv_resident_set_memory(rss); + PROCESS_MEMORY_COUNTERS counters; + if (!GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters))) + return Bun__translateWin32ErrorToUV(GetLastError()); + *rss = counters.WorkingSetSize; + return 0; #else #error "Unknown platform" #endif @@ -4191,12 +4141,10 @@ extern "C" int getPeakRSS(size_t* peak) *peak = static_cast(info.ledger_phys_footprint_peak); return 0; #elif OS(WINDOWS) - uv_rusage_t rusage; - int err = uv_getrusage(&rusage); - if (err) - return err; - // libuv converts PeakWorkingSetSize to kilobytes. - *peak = static_cast(rusage.ru_maxrss) * 1024; + PROCESS_MEMORY_COUNTERS counters; + if (!GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters))) + return Bun__translateWin32ErrorToUV(GetLastError()); + *peak = counters.PeakWorkingSetSize; return 0; #else struct rusage rusage; @@ -4672,6 +4620,11 @@ JSC_DEFINE_CUSTOM_SETTER(setProcessDebugPort, (JSC::JSGlobalObject * globalObjec return true; } +#if OS(WINDOWS) +// In WCHARs, terminator included. Node truncates longer titles the same way. +static constexpr size_t maxConsoleTitleLength = 8192; +#endif + JSC_DEFINE_CUSTOM_GETTER(processTitle, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) { auto& vm = JSC::getVM(globalObject); @@ -4683,21 +4636,21 @@ JSC_DEFINE_CUSTOM_GETTER(processTitle, (JSC::JSGlobalObject * globalObject, JSC: #else // When a title was explicitly set (`--title` CLI flag or a prior // `process.title = ...`), the store is authoritative — the console title - // that uv_get_process_title reads is unavailable in console-less - // processes (CI) and never reflects the CLI flag. + // is unavailable in console-less processes (CI) and never reflects the + // CLI flag. if (Bun__Process__hasTitle()) { auto* result = jsString(vm, Bun__Process__getTitle(globalObject).transferToWTFString()); RETURN_IF_EXCEPTION(scope, {}); RELEASE_AND_RETURN(scope, JSValue::encode(result)); } - char title[1024]; - title[0] = '\0'; // Initialize buffer to empty string - if (uv_get_process_title(title, sizeof(title)) != 0 || title[0] == '\0') { + WCHAR buffer[maxConsoleTitleLength]; + // The return value is the title's length, also when only part of it fit. + DWORD length = std::min(GetConsoleTitleW(buffer, static_cast(std::size(buffer))), static_cast(std::size(buffer) - 1)); + if (!length) RELEASE_AND_RETURN(scope, JSValue::encode(jsString(vm, String("bun"_s)))); - } - auto* result = jsString(vm, WTF::String::fromUTF8(title)); + auto* result = jsString(vm, String(std::span { reinterpret_cast(buffer), static_cast(length) })); RETURN_IF_EXCEPTION(scope, {}); RELEASE_AND_RETURN(scope, JSValue::encode(result)); #endif @@ -4719,12 +4672,14 @@ JSC_DEFINE_CUSTOM_SETTER(setProcessTitle, (JSC::JSGlobalObject * globalObject, J Bun__Process__setTitle(globalObject, &str); return true; #else - // Update the store first so the getter reflects the assignment; the uv - // call is best-effort (it fails in console-less processes). + // Update the store first so the getter reflects the assignment; the + // console title is best-effort (there may be no console). BunString str = Bun::toString(wtfStr); Bun__Process__setTitle(globalObject, &str); - CString cstr = wtfStr.utf8(); - uv_set_process_title(cstr.data()); + Vector wide = wtfStr.wideCharacters(); + if (wide.size() > maxConsoleTitleLength) + wide[maxConsoleTitleLength - 1] = 0; + SetConsoleTitleW(wide.span().data()); return true; #endif } @@ -4784,7 +4739,7 @@ static void bypassCrashHandlerForSelfSentSignal(int pid, int ownPid, int signalN static bool selfSentSignalEndsProcess(int pid, int ownPid, int signal) { #if OS(WINDOWS) - // What uv_kill() ends the process for. + // What Bun__Process__kill() ends the process for. return (pid == ownPid || !pid) && (signal == SIGINT || signal == SIGTERM || signal == SIGKILL || signal == SIGQUIT); #else if (!killReachesThisProcess(pid, ownPid)) @@ -4824,13 +4779,13 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionReallyKill, (JSC::JSGlobalObject * glob #if !OS(WINDOWS) int ownPid = getpid(); #else - int ownPid = uv_os_getpid(); + int ownPid = static_cast(GetCurrentProcessId()); #endif // Node's Kill binding runs RunAtExit for a self-directed unhandled signal, so flush profiles - // first. `signalToContextIdsMap` is mutated only on the main thread; workers never set + // first. `signalsWithListeners` is mutated only on the main thread; workers never set // profiler configs, so skipping the flush there avoids a rehash race. if (signal > 0 && (pid == 0 || pid == -1 || pid == ownPid || pid == -ownPid) - && !(Bun__isMainThreadVM() && signalToContextIdsMap && signalToContextIdsMap->contains(signal))) { + && !(Bun__isMainThreadVM() && signalsWithListeners && signalsWithListeners->contains(signal))) { Bun__writeProfilesBeforeSelfKill(selfSentSignalEndsProcess(pid, ownPid, signal)); } @@ -4840,7 +4795,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionReallyKill, (JSC::JSGlobalObject * glob if (result < 0) result = errno; #else - int result = uv_kill(pid, signal); + int result = Bun__Process__kill(pid, signal); #endif RELEASE_AND_RETURN(scope, JSValue::encode(jsNumber(result))); diff --git a/src/jsc/bindings/BunProcessReportObjectWindows.cpp b/src/jsc/bindings/BunProcessReportObjectWindows.cpp index 08f51d753148..21a4595b21ab 100644 --- a/src/jsc/bindings/BunProcessReportObjectWindows.cpp +++ b/src/jsc/bindings/BunProcessReportObjectWindows.cpp @@ -28,7 +28,7 @@ #include "JavaScriptCore/StackFrame.h" #include "JavaScriptCore/Interpreter.h" #include "wtf/text/OrdinalNumber.h" -#include +#include "OsBinding.h" #include #include #include @@ -41,6 +41,24 @@ using namespace JSC; // External functions extern "C" EncodedJSValue Bun__Process__createExecArgv(JSGlobalObject*); +// Calls `fill(buffer, capacity)` with a growing buffer until the string fits. `fill` returns the +// string's length without the terminator, 0 when it fails, and at least `capacity` when the +// buffer is too small. +template +static String stringFromWin32(const Fill& fill) +{ + Vector buffer(MAX_PATH); + for (;;) { + DWORD capacity = static_cast(buffer.size()); + DWORD length = fill(buffer.mutableSpan().data(), capacity); + if (!length) + return String(); + if (length < capacity) + return String(std::span { reinterpret_cast(buffer.span().data()), static_cast(length) }); + buffer.grow(std::max(length, static_cast(capacity) * 2)); + } +} + JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Process* process) { auto scope = DECLARE_THROW_SCOPE(vm); @@ -72,13 +90,8 @@ JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Pr // Working directory { - WCHAR cwd[MAX_PATH]; - DWORD len = GetCurrentDirectoryW(MAX_PATH, cwd); - if (len > 0 && len < MAX_PATH) { - Bun::putDirectNamed(vm, header, "cwd"_s, jsString(vm, String({ reinterpret_cast(cwd), static_cast(len) }))); - } else { - Bun::putDirectNamed(vm, header, "cwd"_s, jsString(vm, String("."_s))); - } + String cwd = stringFromWin32([](WCHAR* buffer, DWORD capacity) { return GetCurrentDirectoryW(capacity, buffer); }); + Bun::putDirectNamed(vm, header, "cwd"_s, jsString(vm, cwd.isNull() ? String("."_s) : cwd)); } // Command line @@ -102,7 +115,7 @@ JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Pr JSObject* versions = constructEmptyObject(globalObject, globalObject->objectPrototype()); Bun::putDirectNamed(vm, versions, "node"_s, jsString(vm, String(REPORTED_NODEJS_VERSION ""_s))); Bun::putDirectNamed(vm, versions, "v8"_s, jsString(vm, String(ASCIILiteral::fromLiteralUnsafe(REPORTED_NODEJS_V8_VERSION)))); - Bun::putDirectNamed(vm, versions, "uv"_s, jsString(vm, String::fromLatin1(uv_version_string()))); + Bun::putDirectNamed(vm, versions, "uv"_s, jsString(vm, String(REPORTED_NODEJS_UV_VERSION ""_s))); Bun::putDirectNamed(vm, versions, "modules"_s, jsString(vm, String(ASCIILiteral::fromLiteralUnsafe(STRINGIFY(REPORTED_NODEJS_ABI_VERSION))))); Bun::putDirectNamed(vm, header, "componentVersions"_s, versions); RETURN_IF_EXCEPTION(scope, {}); @@ -156,11 +169,10 @@ JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Pr } } - // CPU info using libuv - uv_cpu_info_t* cpu_infos; + BunCpuInfo* cpu_infos; int count; - if (uv_cpu_info(&cpu_infos, &count) == 0) { - auto freeCpuInfos = WTF::makeScopeExit([&] { uv_free_cpu_info(cpu_infos, count); }); + if (Bun__Os__cpuInfo(&cpu_infos, &count) == 0) { + auto freeCpuInfos = WTF::makeScopeExit([&] { Bun__Os__freeCpuInfo(cpu_infos, count); }); JSArray* cpuArray = constructEmptyArray(globalObject, nullptr, count); RETURN_IF_EXCEPTION(scope, {}); @@ -184,10 +196,9 @@ JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Pr } RETURN_IF_EXCEPTION(scope, {}); - // Network interfaces using libuv - uv_interface_address_t* interfaces; - if (uv_interface_addresses(&interfaces, &count) == 0) { - auto freeInterfaces = WTF::makeScopeExit([&] { uv_free_interface_addresses(interfaces, count); }); + BunInterfaceAddress* interfaces; + if (Bun__Os__interfaceAddresses(&interfaces, &count) == 0) { + auto freeInterfaces = WTF::makeScopeExit([&] { Bun__Os__freeInterfaceAddresses(interfaces, count); }); JSArray* interfacesArray = constructEmptyArray(globalObject, nullptr, count); RETURN_IF_EXCEPTION(scope, {}); @@ -196,24 +207,18 @@ JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Pr Bun::putDirectNamed(vm, iface, "name"_s, jsString(vm, String::fromUTF8(interfaces[i].name))); Bun::putDirectNamed(vm, iface, "internal"_s, jsBoolean(interfaces[i].is_internal)); - char addr[INET6_ADDRSTRLEN]; + auto formatted = [&](const void* address) { + char text[64]; + size_t length = Bun__Os__formatAddress(address, text, sizeof(text)); + return jsString(vm, String::fromUTF8(std::span { text, length })); + }; if (interfaces[i].address.address4.sin_family == AF_INET) { - uv_inet_ntop(AF_INET, &interfaces[i].address.address4.sin_addr, addr, sizeof(addr)); - Bun::putDirectNamed(vm, iface, "address"_s, jsString(vm, String::fromUTF8(addr))); - - char netmask[INET_ADDRSTRLEN]; - uv_inet_ntop(AF_INET, &interfaces[i].netmask.netmask4.sin_addr, netmask, sizeof(netmask)); - Bun::putDirectNamed(vm, iface, "netmask"_s, jsString(vm, String::fromUTF8(netmask))); - + Bun::putDirectNamed(vm, iface, "address"_s, formatted(&interfaces[i].address)); + Bun::putDirectNamed(vm, iface, "netmask"_s, formatted(&interfaces[i].netmask)); Bun::putDirectNamed(vm, iface, "family"_s, jsString(vm, String::fromLatin1("IPv4"))); } else if (interfaces[i].address.address6.sin6_family == AF_INET6) { - uv_inet_ntop(AF_INET6, &interfaces[i].address.address6.sin6_addr, addr, sizeof(addr)); - Bun::putDirectNamed(vm, iface, "address"_s, jsString(vm, String::fromUTF8(addr))); - - char netmask[INET6_ADDRSTRLEN]; - uv_inet_ntop(AF_INET6, &interfaces[i].netmask.netmask6.sin6_addr, netmask, sizeof(netmask)); - Bun::putDirectNamed(vm, iface, "netmask"_s, jsString(vm, String::fromUTF8(netmask))); - + Bun::putDirectNamed(vm, iface, "address"_s, formatted(&interfaces[i].address)); + Bun::putDirectNamed(vm, iface, "netmask"_s, formatted(&interfaces[i].netmask)); Bun::putDirectNamed(vm, iface, "family"_s, jsString(vm, String::fromLatin1("IPv6"))); Bun::putDirectNamed(vm, iface, "scopeid"_s, jsNumber(interfaces[i].address.address6.sin6_scope_id)); } @@ -376,10 +381,9 @@ JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Pr DWORD bytes = std::min(needed, static_cast(sizeof(modules))); int count = static_cast(bytes / sizeof(HMODULE)); for (int i = 0; i < count; i++) { - WCHAR modName[MAX_PATH]; - DWORD len = GetModuleFileNameExW(GetCurrentProcess(), modules[i], modName, static_cast(std::size(modName))); - if (len > 0) { - sharedObjects->push(globalObject, jsString(vm, String({ reinterpret_cast(modName), static_cast(len) }))); + String name = stringFromWin32([&](WCHAR* buffer, DWORD capacity) { return GetModuleFileNameExW(GetCurrentProcess(), modules[i], buffer, capacity); }); + if (!name.isNull()) { + sharedObjects->push(globalObject, jsString(vm, name)); RETURN_IF_EXCEPTION(scope, {}); } } diff --git a/src/jsc/bindings/BunWindowsPosixConstants.h b/src/jsc/bindings/BunWindowsPosixConstants.h new file mode 100644 index 000000000000..0a2fc7e02047 --- /dev/null +++ b/src/jsc/bindings/BunWindowsPosixConstants.h @@ -0,0 +1,38 @@ +#pragma once + +#include "root.h" + +#if OS(WINDOWS) + +// POSIX constants that Node has on Windows (fs.constants, os.constants.signals) +// and that the UCRT spells differently or does not have. + +#include // _S_IREAD _S_IWRITE +#include + +#ifndef S_IRUSR +#define S_IRUSR _S_IREAD +#endif // S_IRUSR +#ifndef S_IWUSR +#define S_IWUSR _S_IWRITE +#endif // S_IWUSR +// The UCRT only defines the underscore-prefixed _S_IFIFO; whether the plain +// spelling is visible otherwise depends on what happened to be defined earlier +// in the unified source. +#if !defined(S_IFIFO) && defined(_S_IFIFO) +#define S_IFIFO _S_IFIFO +#endif // S_IFIFO +// Not in the UCRT. Node has them on Windows with these values. +#ifndef S_IFLNK +#define S_IFLNK 0xA000 +#endif // S_IFLNK +#ifndef F_OK +#define F_OK 0 +#define R_OK 4 +#define W_OK 2 +#define X_OK 1 +#endif // F_OK + +#include "BunWindowsSignals.h" + +#endif // OS(WINDOWS) diff --git a/src/jsc/bindings/BunWindowsProcess.cpp b/src/jsc/bindings/BunWindowsProcess.cpp new file mode 100644 index 000000000000..9295c6204e40 --- /dev/null +++ b/src/jsc/bindings/BunWindowsProcess.cpp @@ -0,0 +1,153 @@ +#include "root.h" + +#if OS(WINDOWS) + +#include "BunWindowsProcess.h" +#include +#include +#include +#include + +extern "C" int32_t Bun__getParentProcessId() +{ + PROCESS_BASIC_INFORMATION info; + if (!NT_SUCCESS(NtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &info, sizeof(info), nullptr))) + return -1; + // winternl.h names InheritedFromUniqueProcessId "Reserved3". + return static_cast(reinterpret_cast(info.Reserved3)); +} + +namespace Bun { + +// The priority and resource usage functions are ports of libuv's uv_os_getpriority(), +// uv_os_setpriority() and uv_getrusage() (src/win/util.c), MIT. + +static int openProcess(int pid, DWORD access, HANDLE* handle) +{ + *handle = pid == 0 ? GetCurrentProcess() : OpenProcess(access, FALSE, static_cast(pid)); + if (*handle) + return 0; + + DWORD error = GetLastError(); + // OpenProcess reports a pid that does not exist as ERROR_INVALID_PARAMETER. + return error == ERROR_INVALID_PARAMETER ? UV__ESRCH : Bun__translateWin32ErrorToUV(error); +} + +int getProcessPriority(int pid, int* priority) +{ + HANDLE handle; + if (int err = openProcess(pid, PROCESS_QUERY_LIMITED_INFORMATION, &handle)) + return err; + + int result = 0; + switch (GetPriorityClass(handle)) { + case 0: + result = Bun__translateWin32ErrorToUV(GetLastError()); + break; + case REALTIME_PRIORITY_CLASS: + *priority = -20; + break; + case HIGH_PRIORITY_CLASS: + *priority = -14; + break; + case ABOVE_NORMAL_PRIORITY_CLASS: + *priority = -7; + break; + case NORMAL_PRIORITY_CLASS: + *priority = 0; + break; + case BELOW_NORMAL_PRIORITY_CLASS: + *priority = 10; + break; + default: // IDLE_PRIORITY_CLASS + *priority = 19; + break; + } + + CloseHandle(handle); + return result; +} + +int setProcessPriority(int pid, int priority) +{ + DWORD priorityClass; + if (priority < -20 || priority > 19) + return UV__EINVAL; + else if (priority < -14) + priorityClass = REALTIME_PRIORITY_CLASS; + else if (priority < -7) + priorityClass = HIGH_PRIORITY_CLASS; + else if (priority < 0) + priorityClass = ABOVE_NORMAL_PRIORITY_CLASS; + else if (priority < 10) + priorityClass = NORMAL_PRIORITY_CLASS; + else if (priority < 19) + priorityClass = BELOW_NORMAL_PRIORITY_CLASS; + else + priorityClass = IDLE_PRIORITY_CLASS; + + HANDLE handle; + if (int err = openProcess(pid, PROCESS_SET_INFORMATION, &handle)) + return err; + + int result = 0; + if (!SetPriorityClass(handle, priorityClass)) + result = Bun__translateWin32ErrorToUV(GetLastError()); + + CloseHandle(handle); + return result; +} + +// FILETIME counts 100ns intervals. +static uint64_t fileTimeToMicroseconds(const FILETIME& time) +{ + return ((static_cast(time.dwHighDateTime) << 32) | time.dwLowDateTime) / 10; +} + +int getProcessCpuTimes(CpuTimes& times) +{ + FILETIME creation, exit, kernel, user; + if (!GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user)) + return Bun__translateWin32ErrorToUV(GetLastError()); + times.user = fileTimeToMicroseconds(user); + times.system = fileTimeToMicroseconds(kernel); + return 0; +} + +int getThreadCpuTimes(CpuTimes& times) +{ + FILETIME creation, exit, kernel, user; + if (!GetThreadTimes(GetCurrentThread(), &creation, &exit, &kernel, &user)) + return Bun__translateWin32ErrorToUV(GetLastError()); + times.user = fileTimeToMicroseconds(user); + times.system = fileTimeToMicroseconds(kernel); + return 0; +} + +int getResourceUsage(ResourceUsage& usage) +{ + CpuTimes cpu; + if (int err = getProcessCpuTimes(cpu)) + return err; + + PROCESS_MEMORY_COUNTERS memory; + if (!GetProcessMemoryInfo(GetCurrentProcess(), &memory, sizeof(memory))) + return Bun__translateWin32ErrorToUV(GetLastError()); + + IO_COUNTERS io; + if (!GetProcessIoCounters(GetCurrentProcess(), &io)) + return Bun__translateWin32ErrorToUV(GetLastError()); + + usage = {}; + usage.ru_utime = { cpu.user / 1000000, cpu.user % 1000000 }; + usage.ru_stime = { cpu.system / 1000000, cpu.system % 1000000 }; + usage.ru_maxrss = memory.PeakWorkingSetSize / 1024; + usage.ru_majflt = memory.PageFaultCount; + usage.ru_inblock = io.ReadOperationCount; + usage.ru_oublock = io.WriteOperationCount; + return 0; +} + +} // namespace Bun + +#endif // OS(WINDOWS) diff --git a/src/jsc/bindings/BunWindowsProcess.h b/src/jsc/bindings/BunWindowsProcess.h new file mode 100644 index 000000000000..76631a304fdb --- /dev/null +++ b/src/jsc/bindings/BunWindowsProcess.h @@ -0,0 +1,43 @@ +#pragma once + +#include "root.h" + +#if OS(WINDOWS) + +// A Win32 error code as the negative UV_E* number JS sees in `err.errno`. +extern "C" int Bun__translateWin32ErrorToUV(uint32_t win32Error); + +extern "C" int32_t Bun__getParentProcessId(); + +namespace Bun { + +// Every function returning `int` returns 0 or a negative UV_E* number. + +// `pid` 0 is the current process. Priorities are Unix nice values; see +// os.constants.priority. +int getProcessPriority(int pid, int* priority); +int setProcessPriority(int pid, int priority); + +// Microseconds. +struct CpuTimes { + uint64_t user; + uint64_t system; +}; +int getProcessCpuTimes(CpuTimes&); +int getThreadCpuTimes(CpuTimes&); + +// `struct rusage` for process.resourceUsage(). Windows has a source for +// ru_utime, ru_stime, ru_maxrss (kilobytes), ru_majflt (every page fault, soft +// or hard) and ru_inblock/ru_oublock (I/O operations); the rest is 0. +struct ResourceUsage { + struct { + uint64_t tv_sec; + uint64_t tv_usec; + } ru_utime, ru_stime; + uint64_t ru_maxrss, ru_ixrss, ru_idrss, ru_isrss, ru_minflt, ru_majflt, ru_nswap, ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals, ru_nvcsw, ru_nivcsw; +}; +int getResourceUsage(ResourceUsage&); + +} // namespace Bun + +#endif // OS(WINDOWS) diff --git a/src/jsc/bindings/BunWindowsSignals.h b/src/jsc/bindings/BunWindowsSignals.h new file mode 100644 index 000000000000..35d55606fd07 --- /dev/null +++ b/src/jsc/bindings/BunWindowsSignals.h @@ -0,0 +1,27 @@ +#pragma once + +#include "root.h" + +#if OS(WINDOWS) + +#include + +// The CRT defines SIGINT (2), SIGILL (4), SIGABRT_COMPAT (6), SIGFPE (8), +// SIGSEGV (11), SIGTERM (15), SIGBREAK (21) and SIGABRT (22). Node has these +// four on Windows as well, with the numbers Linux and Darwin use. They are +// JS-visible: os.constants.signals, process.kill(), process.on(). +#ifndef SIGHUP +#define SIGHUP 1 +#endif +#ifndef SIGQUIT +#define SIGQUIT 3 +#endif +#ifndef SIGKILL +#define SIGKILL 9 +#endif +#ifndef SIGWINCH +#define SIGWINCH 28 +#endif +static_assert(SIGHUP == 1 && SIGQUIT == 3 && SIGKILL == 9 && SIGWINCH == 28, "another header defines these with different numbers"); + +#endif // OS(WINDOWS) diff --git a/src/jsc/bindings/NodeURL.cpp b/src/jsc/bindings/NodeURL.cpp index 27289f3d3b92..7b73dc9af74a 100644 --- a/src/jsc/bindings/NodeURL.cpp +++ b/src/jsc/bindings/NodeURL.cpp @@ -208,6 +208,20 @@ JSC_DEFINE_HOST_FUNCTION(jsDomainToUnicode, (JSC::JSGlobalObject * globalObject, return JSC::JSValue::encode(JSC::jsString(vm, unicode)); } +// For name lookups that hand the name to the system resolver, which would +// otherwise encode it by its own rules (Windows: IDNA2003, which maps ß to ss). +// Writes the ASCII form of the UTF-16 `name` to `out` as UTF-16 and returns +// its length, or -1 when the name has none or it does not fit. +extern "C" int32_t Bun__domainToASCII16(const char16_t* name, int32_t length, char16_t* out, int32_t capacity) +{ + auto result = icuToASCII(String(std::span { name, static_cast(length) }), IDNAMode::Default); + if (result.isNull() || static_cast(result.length()) > capacity) + return -1; + for (unsigned i = 0; i < result.length(); i++) + out[i] = result[i]; + return static_cast(result.length()); +} + // Standalone UTS #46 domain-to-ascii (Node's encoding_binding toASCII, i.e. // ada::idna::to_ascii): returns "" on failure. url.parse's IDNA step — unlike // domainToASCII, no host parsing (no IPv4 canonicalization, ':' allowed, ...). diff --git a/src/jsc/bindings/OsBinding.cpp b/src/jsc/bindings/OsBinding.cpp index a61dfb27e8a5..ad2dd35f40db 100644 --- a/src/jsc/bindings/OsBinding.cpp +++ b/src/jsc/bindings/OsBinding.cpp @@ -80,7 +80,7 @@ static uint64_t bunReadProcMeminfo(const char* what) return rc * 1024; } -// Matches libuv's uv_get_free_memory (vendor/libuv/src/unix/linux.c): prefer +// Matches libuv's uv_get_free_memory (src/unix/linux.c): prefer // MemAvailable from /proc/meminfo (kernel's estimate of memory available for // new allocations, including reclaimable page cache) and only fall back to // sysinfo.freeram (which excludes page cache) when /proc/meminfo cannot be @@ -101,11 +101,247 @@ extern "C" uint64_t Bun__Os__getFreeMemory(void) #endif #if OS(WINDOWS) -extern "C" uint64_t uv_get_available_memory(void); +#include "OsBinding.h" +#include "BunWindowsProcess.h" +#include +#include +#include +#include +#include +#include extern "C" uint64_t Bun__Os__getFreeMemory(void) { - return uv_get_available_memory(); + MEMORYSTATUSEX status; + status.dwLength = sizeof(status); + if (!GlobalMemoryStatusEx(&status)) + return 0; + return status.ullAvailPhys; +} + +// UTF-8 length of `length` UTF-16 code units, excluding any terminator. +static size_t utf8Length(const WCHAR* string, int length) +{ + if (length == 0) + return 0; + return static_cast(WideCharToMultiByte(CP_UTF8, 0, string, length, nullptr, 0, nullptr, nullptr)); +} + +// Writes `length` UTF-16 code units as NUL-terminated UTF-8 and returns the +// number of bytes written, terminator included. `capacity` is at least 1. +static size_t writeUTF8(const WCHAR* string, int length, char* out, size_t capacity) +{ + // Given a size of 0, WideCharToMultiByte writes nothing and returns the size needed. + int room = static_cast(capacity - 1); + int written = length == 0 || room == 0 ? 0 : WideCharToMultiByte(CP_UTF8, 0, string, length, out, room, nullptr, nullptr); + out[written] = '\0'; + return static_cast(written) + 1; +} + +// Port of libuv's uv_cpu_info() (src/win/util.c), MIT. +extern "C" int Bun__Os__cpuInfo(BunCpuInfo** cpuInfos, int* count) +{ + *cpuInfos = nullptr; + *count = 0; + + // Logical processors of this process's processor group. + SYSTEM_INFO systemInfo; + GetSystemInfo(&systemInfo); + DWORD cpuCount = systemInfo.dwNumberOfProcessors; + + // ProcessorNameString is at most 256 WCHARs; a UTF-16 code unit is at most 3 UTF-8 bytes. + static constexpr size_t maxBrand = 256; + static constexpr size_t modelCapacity = maxBrand * 3 + 1; + + void* allocation = nullptr; + size_t performanceSize = cpuCount * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION); + if (!WTF::tryFastMalloc(cpuCount * (sizeof(BunCpuInfo) + modelCapacity) + performanceSize).getValue(allocation)) + return UV__ENOMEM; + + // The model strings go last: their size is odd, and NtQuerySystemInformation + // fails with STATUS_DATATYPE_MISALIGNMENT for a buffer that is not aligned. + auto* infos = static_cast(allocation); + auto* performance = reinterpret_cast(infos + cpuCount); + char* models = reinterpret_cast(performance + cpuCount); + static_assert(alignof(BunCpuInfo) >= alignof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)); + + DWORD error; + ULONG resultSize; + NTSTATUS status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, performance, static_cast(performanceSize), &resultSize); + if (!NT_SUCCESS(status)) { + error = RtlNtStatusToDosError(status); + goto fail; + } + ASSERT(resultSize == performanceSize); + + for (DWORD i = 0; i < cpuCount; i++) { + WCHAR keyName[128]; + _snwprintf(keyName, std::size(keyName), L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%lu", i); + + HKEY processorKey; + error = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyName, 0, KEY_QUERY_VALUE, &processorKey); + if (error != ERROR_SUCCESS) + goto fail; + + // A value shorter than a DWORD leaves the rest of `speed` unwritten. + DWORD speed = 0; + DWORD speedSize = sizeof(speed); + error = RegQueryValueExW(processorKey, L"~MHz", nullptr, nullptr, reinterpret_cast(&speed), &speedSize); + if (error != ERROR_SUCCESS) { + RegCloseKey(processorKey); + goto fail; + } + + WCHAR brand[maxBrand]; + DWORD brandSize = sizeof(brand); + error = RegQueryValueExW(processorKey, L"ProcessorNameString", nullptr, nullptr, reinterpret_cast(brand), &brandSize); + RegCloseKey(processorKey); + // Without the value the model is empty. + if (error == ERROR_FILE_NOT_FOUND) + brandSize = 0; + else if (error != ERROR_SUCCESS) + goto fail; + + BunCpuInfo& info = infos[i]; + info.model = models + i * modelCapacity; + // Untrimmed: the registry value is padded with trailing spaces on some CPUs and Node reports them. + writeUTF8(brand, static_cast(wcsnlen(brand, brandSize / sizeof(WCHAR))), info.model, modelCapacity); + info.speed = static_cast(speed); + // 100ns units to milliseconds. KernelTime includes IdleTime. + info.cpu_times.user = performance[i].UserTime.QuadPart / 10000; + info.cpu_times.nice = 0; + info.cpu_times.sys = (performance[i].KernelTime.QuadPart - performance[i].IdleTime.QuadPart) / 10000; + info.cpu_times.idle = performance[i].IdleTime.QuadPart / 10000; + // winternl.h names DpcTime and InterruptTime "Reserved1". + info.cpu_times.irq = performance[i].Reserved1[1].QuadPart / 10000; + } + + *cpuInfos = infos; + *count = static_cast(cpuCount); + return 0; + +fail: + WTF::fastFree(allocation); + return Bun__translateWin32ErrorToUV(error); +} + +extern "C" void Bun__Os__freeCpuInfo(BunCpuInfo* cpuInfos, int) +{ + WTF::fastFree(cpuInfos); +} + +static bool isReported(const IP_ADAPTER_ADDRESSES* adapter) +{ + return adapter->OperStatus == IfOperStatusUp && adapter->FirstUnicastAddress; +} + +// Port of libuv's uv_interface_addresses() (src/win/util.c), MIT. +extern "C" int Bun__Os__interfaceAddresses(BunInterfaceAddress** addresses, int* count) +{ + *addresses = nullptr; + *count = 0; + + ULONG adaptersSize = 0; + IP_ADAPTER_ADDRESSES* adapters = nullptr; + for (;;) { + ULONG result = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, nullptr, adapters, &adaptersSize); + if (result == ERROR_SUCCESS) + break; + + WTF::fastFree(adapters); + adapters = nullptr; + + switch (result) { + case ERROR_BUFFER_OVERFLOW: { + void* allocation; + if (!WTF::tryFastMalloc(adaptersSize).getValue(allocation)) + return UV__ENOMEM; + adapters = static_cast(allocation); + continue; + } + case ERROR_NO_DATA: + // No adapters. + return 0; + case ERROR_ADDRESS_NOT_ASSOCIATED: + return UV__EAGAIN; + case ERROR_INVALID_PARAMETER: + // The arguments are valid, which leaves "the address information is greater than ULONG_MAX". + return UV__ENOBUFS; + default: + return Bun__translateWin32ErrorToUV(result); + } + } + + // One entry per unicast address of every adapter that is up, followed by the adapter names. + size_t addressCount = 0; + size_t namesSize = 0; + for (auto* adapter = adapters; adapter; adapter = adapter->Next) { + if (!isReported(adapter)) + continue; + namesSize += utf8Length(adapter->FriendlyName, static_cast(wcslen(adapter->FriendlyName))) + 1; + for (auto* unicast = adapter->FirstUnicastAddress; unicast; unicast = unicast->Next) + addressCount++; + } + + void* allocation = nullptr; + // Never zero bytes: the caller frees a non-null array. + if (!WTF::tryFastMalloc(addressCount * sizeof(BunInterfaceAddress) + namesSize + 1).getValue(allocation)) { + WTF::fastFree(adapters); + return UV__ENOMEM; + } + + auto* address = static_cast(allocation); + char* name = reinterpret_cast(address + addressCount); + char* namesEnd = name + namesSize + 1; + + for (auto* adapter = adapters; adapter; adapter = adapter->Next) { + if (!isReported(adapter)) + continue; + + size_t nameSize = writeUTF8(adapter->FriendlyName, static_cast(wcslen(adapter->FriendlyName)), name, static_cast(namesEnd - name)); + + for (auto* unicast = adapter->FirstUnicastAddress; unicast; unicast = unicast->Next) { + const sockaddr* socketAddress = unicast->Address.lpSockaddr; + // An illegal length is reported as 255; the mask has room for the bits of the address. + const ULONG addressBits = socketAddress->sa_family == AF_INET6 ? 128 : 32; + const ULONG prefixLength = std::min(unicast->OnLinkPrefixLength, addressBits); + + memset(address, 0, sizeof(*address)); + address->name = name; + + if (adapter->PhysicalAddressLength == sizeof(address->phys_addr)) + memcpy(address->phys_addr, adapter->PhysicalAddress, sizeof(address->phys_addr)); + + address->is_internal = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK; + + if (socketAddress->sa_family == AF_INET6) { + address->address.address6 = *reinterpret_cast(socketAddress); + address->netmask.netmask6.sin6_family = AF_INET6; + memset(address->netmask.netmask6.sin6_addr.s6_addr, 0xff, prefixLength >> 3); + if (prefixLength % 8) + address->netmask.netmask6.sin6_addr.s6_addr[prefixLength >> 3] = static_cast(0xff << (8 - prefixLength % 8)); + } else { + address->address.address4 = *reinterpret_cast(socketAddress); + address->netmask.netmask4.sin_family = AF_INET; + address->netmask.netmask4.sin_addr.s_addr = prefixLength > 0 ? htonl(0xffffffff << (32 - prefixLength)) : 0; + } + + address++; + } + + name += nameSize; + } + + WTF::fastFree(adapters); + + *addresses = static_cast(allocation); + *count = static_cast(addressCount); + return 0; +} + +extern "C" void Bun__Os__freeInterfaceAddresses(BunInterfaceAddress* addresses, int) +{ + WTF::fastFree(addresses); } #endif diff --git a/src/jsc/bindings/OsBinding.h b/src/jsc/bindings/OsBinding.h new file mode 100644 index 000000000000..31687efb2805 --- /dev/null +++ b/src/jsc/bindings/OsBinding.h @@ -0,0 +1,51 @@ +#pragma once + +#include "root.h" + +#if OS(WINDOWS) + +#include +#include + +extern "C" { + +// Layout of libuv's uv_cpu_info_t. Times are milliseconds. +struct BunCpuInfo { + char* model; // UTF-8, NUL-terminated + int speed; // MHz + struct { + uint64_t user; + uint64_t nice; + uint64_t sys; + uint64_t idle; + uint64_t irq; + } cpu_times; +}; + +// Layout of libuv's uv_interface_address_t. +struct BunInterfaceAddress { + char* name; // UTF-8, NUL-terminated + char phys_addr[6]; + int is_internal; + union { + struct sockaddr_in address4; + struct sockaddr_in6 address6; + } address; + union { + struct sockaddr_in netmask4; + struct sockaddr_in6 netmask6; + } netmask; +}; + +// Both return 0 or a negative UV_E* number. On success the array, which may be +// null when `*count` is 0, is released with the matching free function. +int Bun__Os__cpuInfo(BunCpuInfo** cpuInfos, int* count); +void Bun__Os__freeCpuInfo(BunCpuInfo* cpuInfos, int count); +int Bun__Os__interfaceAddresses(BunInterfaceAddress** addresses, int* count); +// Writes `address` (a sockaddr_in or sockaddr_in6) the way `os.networkInterfaces()` prints it and +// returns the length; 0 if it does not fit. +size_t Bun__Os__formatAddress(const void* address, char* buffer, size_t capacity); +void Bun__Os__freeInterfaceAddresses(BunInterfaceAddress* addresses, int count); +} + +#endif // OS(WINDOWS) diff --git a/src/jsc/bindings/ProcessBindingConstants.cpp b/src/jsc/bindings/ProcessBindingConstants.cpp index 60c8b1417f7b..d7a88070cd3b 100644 --- a/src/jsc/bindings/ProcessBindingConstants.cpp +++ b/src/jsc/bindings/ProcessBindingConstants.cpp @@ -23,24 +23,7 @@ #endif #if OS(WINDOWS) - -#include // _S_IREAD _S_IWRITE -#ifndef S_IRUSR -#define S_IRUSR _S_IREAD -#endif // S_IRUSR -#ifndef S_IWUSR -#define S_IWUSR _S_IWRITE -#endif // S_IWUSR -// The UCRT only defines the underscore-prefixed _S_IFIFO; whether the plain -// spelling is visible here otherwise depends on what happened to be defined -// earlier in the unified source. Node exposes fs.constants.S_IFIFO (4096) on -// Windows, so pin it to the CRT value instead of relying on header luck. -#if !defined(S_IFIFO) && defined(_S_IFIFO) -#define S_IFIFO _S_IFIFO -#endif // S_IFIFO - -#include - +#include "BunWindowsPosixConstants.h" #else // OS(WINDOWS) #include #endif diff --git a/src/jsc/bindings/ProcessBindingTTYWrap.cpp b/src/jsc/bindings/ProcessBindingTTYWrap.cpp index b4d2bd7809c1..b9b2f5e4c43d 100644 --- a/src/jsc/bindings/ProcessBindingTTYWrap.cpp +++ b/src/jsc/bindings/ProcessBindingTTYWrap.cpp @@ -23,38 +23,16 @@ #include #include #else -#include +#include #include #include #endif #if OS(WINDOWS) - -extern "C" int Source__setRawModeStdin(uv_loop_t* uv_loop, bool raw); - -namespace UV { - -class TTY { -public: - uv_tty_t handle {}; - - uv_tty_t* tty() { return &handle; } - - void close() - { - uv_close((uv_handle_t*)(tty()), [](uv_handle_t* handle) -> void { - uv_tty_t* ttyHandle = (uv_tty_t*)handle; - ptrdiff_t offset = offsetof(UV::TTY, handle); - - UV::TTY* tty = (UV::TTY*)((char*)ttyHandle - offset); - delete tty; - }); - } -}; - -} - +extern "C" int Source__setRawModeStdin(bool raw); +// `_get_osfhandle`; INVALID_HANDLE_VALUE for an fd that has no HANDLE. +extern "C" void* Bun__crtGetOsfhandle(int fd); #endif namespace Bun { @@ -85,7 +63,8 @@ static bool getWindowSize(int fd, size_t* width, size_t* height) if (!GetConsoleScreenBufferInfo(handle, &csbi)) return false; - *width = csbi.srWindow.Right - csbi.srWindow.Left + 1; + // Text wraps at the width of the screen buffer, which a window can be narrower than. + *width = csbi.dwSize.X; *height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; return true; #else @@ -139,22 +118,8 @@ class TTYWrapObject final : public JSC::JSDestructibleObject { static_cast(cell)->TTYWrapObject::~TTYWrapObject(); } - ~TTYWrapObject() - { -#if OS(WINDOWS) - if (handle) { - handle->close(); - } -#endif - } - int fd = -1; - -#if OS(WINDOWS) - UV::TTY* handle; -#else BunTTYState ttyState {}; -#endif private: TTYWrapObject(JSC::VM& vm, JSC::Structure* structure, const int fd) @@ -162,9 +127,6 @@ class TTYWrapObject final : public JSC::JSDestructibleObject { , fd(fd) { -#if OS(WINDOWS) - handle = nullptr; -#endif } void finishCreation(JSC::VM& vm) @@ -175,10 +137,6 @@ class TTYWrapObject final : public JSC::JSDestructibleObject { } }; -#if OS(WINDOWS) -extern "C" void Bun__setCTRLHandler(BOOL add); -#endif - const ClassInfo TTYWrapObject::s_info = { "LibuvStreamWrap"_s, @@ -187,15 +145,6 @@ const ClassInfo TTYWrapObject::s_info = { JSC_DEFINE_HOST_FUNCTION(jsTTYSetMode, (JSC::JSGlobalObject * globalObject, CallFrame* callFrame)) { -#if OS(WINDOWS) - ASSERT(callFrame->argumentCount() == 1); - auto flag = callFrame->argument(0); - bool raw = flag.asBoolean(); - - Zig::GlobalObject* global = uncheckedDowncast(globalObject); - - return JSValue::encode(jsNumber(Source__setRawModeStdin(global->uvLoop(), raw))); -#else auto& vm = JSC::getVM(globalObject); auto scope = DECLARE_THROW_SCOPE(vm); @@ -227,9 +176,15 @@ JSC_DEFINE_HOST_FUNCTION(jsTTYSetMode, (JSC::JSGlobalObject * globalObject, Call throwTypeError(globalObject, scope, "state must be a Uint8Array of rawModeStateSize bytes"_s); return {}; } +#if OS(WINDOWS) + if (fdToUse == 0) + return JSValue::encode(jsNumber(Source__setRawModeStdin(mode_ != 0))); + // As node, which asks libuv for UV_TTY_MODE_RAW_VT. + if (mode_) + mode_ = 3; +#endif int err = Bun__ttySetMode(fdToUse, mode_, state->typedVector(), 1); return JSValue::encode(jsNumber(err)); -#endif } JSC_DEFINE_HOST_FUNCTION(TTYWrap_functionSetMode, @@ -257,16 +212,8 @@ JSC_DEFINE_HOST_FUNCTION(TTYWrap_functionSetMode, } int modeInt = JSC::toInt32(mode.asNumber()); -#if OS(WINDOWS) - if (modeInt == 0) { - Bun__setCTRLHandler(1); - } - - int err = uv_tty_set_mode(ttyWrap->handle->tty(), modeInt); -#else // Nodejs does not throw when ttySetMode fails. An Error event is emitted instead. int err = Bun__ttySetMode(fd, modeInt, &ttyWrap->ttyState, 1); -#endif return JSValue::encode(jsNumber(err)); } @@ -457,29 +404,19 @@ class TTYWrapConstructor final : public JSC::InternalFunction { } #if OS(WINDOWS) - auto* handle = new UV::TTY(); - memset(handle, 0, sizeof(UV::TTY)); - int rc = uv_tty_init(uncheckedDowncast(globalObject)->uvLoop(), handle->tty(), fd, 0); - if (rc < 0) { - delete handle; - throwTypeError(globalObject, scope, "Failed to initialize TTY handle"_s); - return {}; - } - ASSERT(handle->tty()->loop); + DWORD consoleMode; + bool isTTY = GetConsoleMode(Bun__crtGetOsfhandle(fd), &consoleMode); #else - if (!isatty(fd)) { + bool isTTY = isatty(fd); +#endif + if (!isTTY) { throwTypeError(globalObject, scope, makeString("fd"_s, fd, " is not a tty"_s)); return {}; } -#endif auto* structure = TTYWrapObject::createStructure(vm, globalObject, prototypeValue.getObject()); auto* object = TTYWrapObject::create(vm, globalObject, structure, fd); -#if OS(WINDOWS) - object->handle = handle; -#endif - return JSValue::encode(object); } diff --git a/src/jsc/bindings/ProcessBindingUV.cpp b/src/jsc/bindings/ProcessBindingUV.cpp index 55821835ef4b..dd9303c43b5b 100644 --- a/src/jsc/bindings/ProcessBindingUV.cpp +++ b/src/jsc/bindings/ProcessBindingUV.cpp @@ -10,13 +10,10 @@ // clang-format off -// The numeric values come from libuv's own uv_errno_t (UV_ERRNO_MAP in uv.h): -// on POSIX every UV_E* equals -E* of the host, and on Windows libuv defines -// its own synthetic codes (e.g. UV_ENOENT == -4058), which is what node -// reports in err.errno. Negating the compiling host's values -- the -// previous approach -- silently produced CRT-style codes on Windows (-2 for -// ENOENT) that match neither node nor the errors bun's fs emits there. -#include +// The numeric values are libuv's (src/jsc/bindings/libuv/uv/errno.h): -E* of +// the host on POSIX, libuv's own codes on Windows (UV__ENOENT == -4058), which +// is what node reports in err.errno. +#include #define BUN_UV_ERRNO_MAP(macro) \ macro(E2BIG, "argument list too long") \ @@ -118,11 +115,20 @@ struct UVErrnoEntry { }; static constexpr UVErrnoEntry uvErrnoEntries[] = { -#define UV_ERRNO_ENTRY(name, desc) { #name ""_s, desc ""_s, UV_##name }, +#define UV_ERRNO_ENTRY(name, desc) { #name ""_s, desc ""_s, UV__##name }, BUN_UV_ERRNO_MAP(UV_ERRNO_ENTRY) #undef UV_ERRNO_ENTRY }; +String errorMessage(int err) +{ + for (auto& entry : uvErrnoEntries) { + if (err == entry.value) + return String(entry.description); + } + return makeString("Unknown system error "_s, err); +} + JSC_DEFINE_HOST_FUNCTION(jsErrname, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { auto& vm = JSC::getVM(globalObject); diff --git a/src/jsc/bindings/ProcessBindingUV.h b/src/jsc/bindings/ProcessBindingUV.h index 92f1a506004d..74ba3f608b31 100644 --- a/src/jsc/bindings/ProcessBindingUV.h +++ b/src/jsc/bindings/ProcessBindingUV.h @@ -9,5 +9,8 @@ JSC_DECLARE_HOST_FUNCTION(jsGetErrorMap); JSC::JSObject* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject); +// libuv's uv_strerror(): the message for a UV_E* number. +WTF::String errorMessage(int err); + } // namespace ProcessBindingUV } // namespace Bun diff --git a/src/jsc/bindings/ZigGlobalObject.h b/src/jsc/bindings/ZigGlobalObject.h index 1d04744f57b8..7158f31b0a36 100644 --- a/src/jsc/bindings/ZigGlobalObject.h +++ b/src/jsc/bindings/ZigGlobalObject.h @@ -92,11 +92,6 @@ extern "C" void Bun__reportUnhandledError(JSC::JSGlobalObject*, JSC::EncodedJSVa extern "C" bool Bun__VirtualMachine__isShuttingDown(void* /* BunVM */); -#if OS(WINDOWS) -#include -extern "C" uv_loop_t* Bun__ZigGlobalObject__uvLoop(void* /* BunVM */); -#endif - namespace Zig { class JSCStackTrace; @@ -346,12 +341,6 @@ class GlobalObject : public Bun::GlobalScope { void visitGeneratedLazyClasses(GlobalObject*, Visitor&); ALWAYS_INLINE void* bunVM() const { return m_bunVM; } -#if OS(WINDOWS) - uv_loop_t* uvLoop() const - { - return Bun__ZigGlobalObject__uvLoop(m_bunVM); - } -#endif bool isThreadLocalDefaultGlobalObject = false; JSObject* subtleCrypto() { return m_subtleCryptoObject.getInitializedOnMainThread(this); } diff --git a/src/jsc/bindings/c-bindings.cpp b/src/jsc/bindings/c-bindings.cpp index a3fd0268c052..c6d52acadcda 100644 --- a/src/jsc/bindings/c-bindings.cpp +++ b/src/jsc/bindings/c-bindings.cpp @@ -23,9 +23,11 @@ #include #endif #else -#include #include #include +#include +#include +#include "BunWindowsProcess.h" #include #include #include @@ -38,7 +40,7 @@ extern "C" int32_t get_process_priority(int32_t pid) { #if OS(WINDOWS) int priority = 0; - if (uv_os_getpriority(pid, &priority)) + if (Bun::getProcessPriority(pid, &priority)) return std::numeric_limits::max(); return priority; #else @@ -53,7 +55,7 @@ extern "C" int32_t get_process_priority(int32_t pid) extern "C" int32_t set_process_priority(int32_t pid, int32_t priority) { #if OS(WINDOWS) - return uv_os_setpriority(pid, priority); + return Bun::setProcessPriority(pid, priority); #else return setpriority(PRIO_PROCESS, pid, priority); #endif // OS(WINDOWS) @@ -642,22 +644,7 @@ extern "C" void onExitSignal(int sig) #endif #if OS(WINDOWS) -extern "C" void Bun__restoreWindowsStdio(); -BOOL WINAPI Ctrlhandler(DWORD signal) -{ - - if (signal == CTRL_C_EVENT) { - Bun__restoreWindowsStdio(); - SetConsoleCtrlHandler(Ctrlhandler, FALSE); - } - - return FALSE; -} - -extern "C" void Bun__setCTRLHandler(BOOL add) -{ - SetConsoleCtrlHandler(Ctrlhandler, add); -} +extern "C" void Bun__installWindowsSignalHandler(); // Held, never released, across ExitProcess: a WTF suspender it kills between // SuspendThread and ResumeThread of this thread would leave it suspended forever. @@ -686,8 +673,40 @@ extern "C" int __cxa_atexit(void (*)(void*), void*, void*); extern "C" struct mach_header __dso_handle; #endif +#if OS(WINDOWS) +#if defined(_DEBUG) +// The debug CRT asserts on an invalid fd before it calls the invalid-parameter +// handler. Those asserts come from the CRT's lowio sources, e.g. +// "minkernel\crts\ucrt\src\appcrt\lowio\osfinfo.cpp(260) : Assertion failed: ...". +static int crtDebugReportHook(int reportType, char* message, int* returnValue) +{ + if (reportType != _CRT_ASSERT || !message || !strstr(message, "\\lowio\\")) + return FALSE; + if (returnValue) + *returnValue = 0; + return TRUE; +} +#endif + +static void crtInvalidParameterHandler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t) +{ +} +#endif + extern "C" void bun_initialize_process() { +#if OS(WINDOWS) + // Inherited by child processes. + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); + + // Without a handler, any CRT call on a bad fd (_get_osfhandle, _close, ...) + // terminates the process with STATUS_STACK_BUFFER_OVERRUN instead of failing with EBADF. + _set_invalid_parameter_handler(crtInvalidParameterHandler); +#if defined(_DEBUG) + _CrtSetReportHook(crtDebugReportHook); +#endif +#endif + // Disable printf() buffering. We buffer it ourselves. setvbuf(stdout, nullptr, _IONBF, 0); setvbuf(stderr, nullptr, _IONBF, 0); @@ -771,7 +790,7 @@ extern "C" void bun_initialize_process() } #elif OS(WINDOWS) for (int fd = 0; fd <= 2; ++fd) { - auto handle = reinterpret_cast(uv_get_osfhandle(fd)); + auto handle = reinterpret_cast(_get_osfhandle(fd)); if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) == FILE_TYPE_UNKNOWN) { // Ignore _close result. If it fails or not depends on used Windows // version. We will just check _open result. @@ -780,32 +799,15 @@ extern "C" void bun_initialize_process() if (fd != _open("nul", O_RDWR)) { RELEASE_ASSERT_NOT_REACHED(); } else { - switch (fd) { - case 0: { - SetStdHandle(STD_INPUT_HANDLE, uv_get_osfhandle(fd)); - ASSERT(GetStdHandle(STD_INPUT_HANDLE) == uv_get_osfhandle(fd)); - break; - } - case 1: { - SetStdHandle(STD_OUTPUT_HANDLE, uv_get_osfhandle(fd)); - ASSERT(GetStdHandle(STD_OUTPUT_HANDLE) == uv_get_osfhandle(fd)); - break; - } - case 2: { - SetStdHandle(STD_ERROR_HANDLE, uv_get_osfhandle(fd)); - ASSERT(GetStdHandle(STD_ERROR_HANDLE) == uv_get_osfhandle(fd)); - break; - } - default: { - ASSERT_NOT_REACHED(); - } - } + static constexpr DWORD stdHandles[3] = { STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE }; + handle = reinterpret_cast(_get_osfhandle(fd)); + SetStdHandle(stdHandles[fd], handle); + ASSERT(GetStdHandle(stdHandles[fd]) == handle); } } } - // add ctrl+c handler on windows - Bun__setCTRLHandler(1); + Bun__installWindowsSignalHandler(); #endif #if OS(DARWIN) @@ -820,13 +822,7 @@ extern "C" void bun_initialize_process() #endif } -#if OS(WINDOWS) -extern "C" int32_t open_as_nonblocking_tty(int32_t fd, int32_t mode) -{ - RELEASE_ASSERT_NOT_REACHED(); -} - -#else +#if !OS(WINDOWS) static bool can_open_as_nonblocking_tty(int32_t fd) { diff --git a/src/jsc/bindings/helpers.cpp b/src/jsc/bindings/helpers.cpp index 1f15a716abc1..b7f9c999b680 100644 --- a/src/jsc/bindings/helpers.cpp +++ b/src/jsc/bindings/helpers.cpp @@ -3,7 +3,7 @@ #include "BunClientData.h" #include #ifdef _WIN32 -#include +#include "ProcessBindingUV.h" #endif using namespace JSC; @@ -23,11 +23,11 @@ JSValue createSystemError(JSGlobalObject* global, ASCIILiteral syscall, int err) { auto errstr = String::fromLatin1(Bun__errnoName(err)); #ifdef _WIN32 - auto strerr = uv_strerror(err); + auto strerr = Bun::ProcessBindingUV::errorMessage(err); #else - auto strerr = strerror(err); + auto strerr = String::fromLatin1(strerror(err)); #endif - auto* instance = JSC::createError(global, makeString(syscall, "() failed: "_s, errstr, ": "_s, String::fromLatin1(strerr))); + auto* instance = JSC::createError(global, makeString(syscall, "() failed: "_s, errstr, ": "_s, strerr)); auto& vm = global->vm(); auto& builtinNames = WebCore::builtinNames(vm); instance->putDirect(vm, builtinNames.syscallPublicName(), jsString(vm, String(syscall)), 0); diff --git a/src/jsc/bindings/libuv/README.md b/src/jsc/bindings/libuv/README.md index eae3d3825964..50897dafe0e7 100644 --- a/src/jsc/bindings/libuv/README.md +++ b/src/jsc/bindings/libuv/README.md @@ -1,7 +1,8 @@ -# libuv copied headers +# libuv's names and error numbers -These are copied headers from libuv which are used by `bun uv-posix-stubs` to generate stubs which crash with a helpful error message when a NAPI -module tries to access a libuv function which is not supported in Bun. +Bun does not link libuv on any platform. It exports every `uv_*` function all the same, so that a NAPI module which uses one loads: the handful Bun implements (`uv-polyfills.c`), and a stub for each of the others (`uv-stubs.c`) which crashes with a helpful error message when it is called. + +`uv/errno.h` is copied from libuv. It has libuv's error numbers, which are what `err.errno` is on Windows. libuv commit hash: bb706f5fe71827f667f0bce532e95ce0698a498d @@ -9,6 +10,5 @@ libuv commit hash: bb706f5fe71827f667f0bce532e95ce0698a498d 1. Clone libuv repo using the above hash 2. Use the following command to get the list of symbols: `llvm-nm -g libuv.dylib | grep _uv &> symbols.txt`, you're gonna have to clean them up a bit this is not automated sorry ( ͡° ͜ʖ ͡°) -3. Update `src/symbols.txt` and `src/linker.lds` -4. Update the `symbols` list in `generate_uv_posix_stubs.ts` -5. Run `bun uv-posix-stubs` +3. Update the `symbols` list in `generate_uv_stubs_constants.ts` (`polyfills` lists the functions Bun implements itself) +4. Run `bun uv-stubs`. It regenerates `src/jsc/bindings/uv-stubs.c` and `test/napi/uv-stub-stuff/plugin.c`, and rewrites the `uv_*` names in `src/symbols.def`, `src/symbols.txt`, `src/linker.lds` and `src/linker-freebsd.lds` when they differ from the lists diff --git a/src/jsc/bindings/libuv/bun.lock b/src/jsc/bindings/libuv/bun.lock deleted file mode 100644 index fa0b67c99521..000000000000 --- a/src/jsc/bindings/libuv/bun.lock +++ /dev/null @@ -1,20 +0,0 @@ -{ - "lockfileVersion": 1, - "workspaces": { - "": { - "dependencies": { - "tree-sitter": "^0.22.4", - "tree-sitter-c": "^0.23.5", - }, - }, - }, - "packages": { - "node-addon-api": ["node-addon-api@8.3.1", "", {}, "sha512-lytcDEdxKjGJPTLEfW4mYMigRezMlyJY8W4wxJK8zE533Jlb8L8dRuObJFWg2P+AuOIxoCgKF+2Oq4d4Zd0OUA=="], - - "node-gyp-build": ["node-gyp-build@4.8.4", "", { "bin": { "node-gyp-build": "bin.js", "node-gyp-build-optional": "optional.js", "node-gyp-build-test": "build-test.js" } }, "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ=="], - - "tree-sitter": ["tree-sitter@0.22.4", "", { "dependencies": { "node-addon-api": "^8.3.0", "node-gyp-build": "^4.8.4" } }, "sha512-usbHZP9/oxNsUY65MQUsduGRqDHQOou1cagUSwjhoSYAmSahjQDAVsh9s+SlZkn8X8+O1FULRGwHu7AFP3kjzg=="], - - "tree-sitter-c": ["tree-sitter-c@0.23.5", "", { "dependencies": { "node-addon-api": "^8.3.0", "node-gyp-build": "^4.8.4" }, "peerDependencies": { "tree-sitter": "^0.22.1" }, "optionalPeers": ["tree-sitter"] }, "sha512-riDWhqVIt8J14R7G0YMKlUy8E7eYR0Vp6DSGL90nX5CTAXkORCyp4WaOgNtfo8dEsHyZF5e/4E9Z9kWj+qLnTQ=="], - } -} diff --git a/src/jsc/bindings/libuv/generate_uv_posix_stubs.ts b/src/jsc/bindings/libuv/generate_uv_posix_stubs.ts deleted file mode 100644 index 43bcb3015108..000000000000 --- a/src/jsc/bindings/libuv/generate_uv_posix_stubs.ts +++ /dev/null @@ -1,392 +0,0 @@ -import { join } from "path"; -import { symbols, test_skipped } from "./generate_uv_posix_stubs_constants"; - -import Parser from "tree-sitter"; -import C from "tree-sitter-c"; - -const parser = new Parser(); -parser.setLanguage(C); - -const overrides = { - uv_setup_args: { - args: ["argc", "argv"], - decls: ["int argc", "char **argv"], - }, - uv_udp_try_send2: { - args: ["arg0", "arg1", "arg2", "arg3", "arg4", "arg5"], - decls: [ - "uv_udp_t* arg0", - "unsigned int arg1", - "uv_buf_t** arg2", - "unsigned int* arg3", - "struct sockaddr** arg4", - "unsigned int arg5", - ], - }, -}; - -type TestInfo = { - decls: string[]; - args: string[]; -}; - -function assert(condition: boolean, message: string) { - if (!condition) { - console.error(message); - process.exit(1); - } -} - -/** - * 1. Use ripgrep to find the filename + line number of the symbol's declaration is in the libuv headers - * 2. Find the range of text which makes up the declaration - * 3. Generate and return stub - */ -async function generate(symbol_name: string): Promise<[stub: string, symbol_name: string, types: TestInfo]> { - console.log("Looking for", symbol_name); - - const HEADER_PATH = import.meta.dir; - - const output = await Bun.$`rg -n ${symbol_name + "\\("}`.cwd(HEADER_PATH).text(); - - if (!output.includes("UV_EXTERN")) { - console.error("Symbol not found!"); - process.exit(1); - } - - let matches: { filename: string; lineNumber: number; rest: string }[] = []; - - const lines = output.split("\n"); - for (let i = 0; i < lines.length; i++) { - const line = lines[i]; - const match = parseOutput(line); - if (!match) continue; - const { filename, lineNumber, rest } = match; - if (!rest.includes("UV_EXTERN")) continue; - - // ending in one line - if (rest.indexOf(";") == rest.length - 1) { - // console.log("Found single liner!"); - matches.push({ filename, lineNumber, rest }); - } else { - const absoluteFilepath = join(HEADER_PATH, filename); - const fileContents = await Bun.file(absoluteFilepath).text(); - console.log(absoluteFilepath, "Found multi-liner!", lineNumber); - const fileLines = fileContents.split("\n"); - let found = false; - let j = lineNumber; - while (j < fileLines.length) { - if (fileLines[j].indexOf(";") == fileLines[j].length - 1) { - found = true; - break; - } - console.log("j", j); - j++; - } - if (!found) { - console.error("Multi-liner end not found!"); - process.exit(1); - } - const multiLine = fileLines.slice(lineNumber, j + 1).join("\n"); - console.log(`MULTILINE (${lineNumber} -> ${j + 1})`); - matches.push({ filename, lineNumber, rest: multiLine }); - // console.log(matches[matches.length - 1]); - i = j; - } - } - - if (matches.length !== 1) { - console.error("Found", matches.length, "matches!"); - console.error(matches); - process.exit(1); - } - - const { filename, lineNumber, rest } = matches[0]; - - function extractParameterTypes(decl: string): TestInfo { - if (overrides[symbol_name]) return overrides[symbol_name]; - console.log("DECL", decl); - decl = decl.replace("UV_EXTERN", ""); - const rootNode = parser.parse(decl).rootNode; - assert(rootNode.children[0].type === "declaration", "Root node must be a declaration"); - const declNode = rootNode.children[0]; - console.log("DECL NODE", declNode.children); - let functionDeclNode = declNode.children.find(n => n.type === "function_declarator")!; - // it can be a PointerDeclaratorNode: - // uv_loop_t* uv_default_loop - if (!functionDeclNode) { - const pointerDeclaratorNode = declNode.children.find(n => n.type === "pointer_declarator")!; - assert(!!pointerDeclaratorNode, "Pointer declarator not found"); - console.log("POINTER DECLARATOR", pointerDeclaratorNode.children); - functionDeclNode = pointerDeclaratorNode.children.find(n => n.type === "function_declarator")!; - } - assert(!!functionDeclNode, "Function declarator not found"); - const parameterListNode = functionDeclNode.children.find(n => n.type === "parameter_list")!; - assert(!!parameterListNode, "Parameter list not found"); - const parameterDeclarationNodes = parameterListNode.children.filter(n => n.type === "parameter_declaration")!; - assert(parameterDeclarationNodes.length > 0, "Must have exactly one parameter declaration"); - - let decls: string[] = []; - let args: string[] = []; - - let i = 0; - for (const parameterDeclarationNode of parameterDeclarationNodes) { - console.log("PARAM", parameterDeclarationNode.children.length, parameterDeclarationNode.text); - const last_idx = parameterDeclarationNode.children.length - 1; - const last = parameterDeclarationNode.children[last_idx]; - if (last.type === "primitive_type") { - if (last.text === "void") { - decls.push("(void) 0"); - args.push("void"); - continue; - } - const arg = `arg${i++}`; - decls.push(`${last.text} ${arg}`); - args.push(arg); - continue; - } - - if (last.type === "array_declarator") { - const arg = `arg${i++}`; - const ident = last.children[0].text; - const array_declarator = last.children - .slice(1) - .map(n => n.text) - .join(""); - const type = parameterDeclarationNode.children - .slice(0, last_idx) - .map(n => n.text) - .join(" "); - console.log("IDENT", ident, "TYPE", type, "ARRAY DECLARATOR", array_declarator); - decls.push(`${type} *${arg}`); - args.push(arg); - continue; - } - - // function pointer - if (last.type === "function_declarator") { - console.log("FUNCTION DECLARATOR", last.children); - const return_ty = parameterDeclarationNode.children[0].text; - // console.log("LMAO", ); - const arg = `arg${i++}`; - const param_list = last.children[1]; - if (param_list.type !== "parameter_list") { - throw new Error("expect param list man"); - } - args.push(arg); - const decl = `${return_ty} (*${arg})${param_list.text}`; - decls.push(decl); - continue; - } - - assert( - last.type === "identifier" || last.type === "pointer_declarator" || last.type === "abstract_pointer_declarator", // || - // last.type === "array_declarator", - `${symbol_name} Inalid param type, but got: ` + last.type, - ); - - let type = ""; - for (let i = 0; i < last_idx; i++) { - type += parameterDeclarationNode.children[i].text; - type += " "; - } - - console.log(type, "LAST TYPE lol", last.type); - if (last.type === "pointer_declarator" || last.type === "abstract_pointer_declarator") { - let cur = last; - do { - type += "*"; - assert(cur.children[0].type === "*", "Pointer declarator must have a *"); - cur = cur.children[1]; - } while (!!cur && cur.type === "pointer_declarator" && cur.children.length > 0); - } - - const arg = `arg${i++}`; - decls.push(`${type} ${arg}`); - args.push(arg); - } - - // function extractParam(node: Parser.SyntaxNode): [decl: string, arg: string] {} - - return { decls, args }; - } - - function addStub(symbolName: string, decl: string): [stub: string, symbol_name: string, types: TestInfo] { - assert(decl.includes("UV_EXTERN"), "Must include UV_EXTERN: \n" + decl); - - const types = extractParameterTypes(decl); - - // For stub generation, we need semicolons but no initialization - const stub_types = { ...types }; - stub_types.decls = stub_types.decls.map(d => d + ";"); - if (stub_types.args.length === 1 && stub_types.args[0] === "void") { - stub_types.decls = []; - stub_types.args = []; - } - - // For test plugin generation, we need initialization - if (types.args.length === 1 && types.args[0] === "void") { - types.decls = []; - types.args = []; - } else { - types.decls = types.decls.map(d => { - if (d.includes("argv") || d.includes("argc")) { - return d.trim() + ";"; - } - - // Initialize function pointers and multi-pointers to NULL, everything else to {0} - if (d.includes("**") || d.includes("(*") || d.includes("_cb ")) { - return d + " = NULL;"; - } - - return d + " = {0};"; - }); - } - - const decl_without_semicolon = decl.replaceAll(";", "").trim(); - console.log(decl_without_semicolon); - - const contents = `${decl_without_semicolon} { - __bun_throw_not_implemented("${symbolName}"); - __builtin_unreachable(); -}`; - - // await Bun.write(stubPath, contents); - return [contents, symbolName, types]; - } - - function parseOutput(line: string): { filename: string; lineNumber: number; rest: string } | null { - // Match pattern: filename:linenumber:rest - const match = line.match(/^([^:]+):(\d+):(.*)$/); - - if (!match) { - return null; - } - - return { - filename: match[1], - lineNumber: parseInt(match[2], 10) - 1, - rest: match[3], - }; - } - - return addStub(symbol_name, rest); -} - -// const symbols = ["uv_async_init"]; - -if (!Bun.which("rg")) { - console.error("You need ripgrep bro"); - process.exit(1); -} - -for (const symbol of symbols) { - await generate(symbol); -} - -let parts = await Promise.all(symbols.map(s => generate(s))); - -const final_contents = `// GENERATED CODE - DO NOT MODIFY BY HAND -#include "uv-posix-polyfills.h" - - -#if OS(LINUX) || OS(DARWIN) -${parts.map(([stub, _]) => stub).join("\n\n")} -#endif - -`; - -await Bun.write(join(import.meta.dir, "../", "uv-posix-stubs.c"), final_contents); -if (Bun.which("clang-format")) { - await Bun.$`clang-format -i ${join(import.meta.dir, "../", "uv-posix-stubs.c")}`; -} - -const test_plugin_contents = ` // GENERATED CODE ... NO TOUCHY!! - #include - -#include -#include -#include -#include -#include - -napi_value call_uv_func(napi_env env, napi_callback_info info) { - napi_status status; - - size_t argc = 2; - napi_value args[2]; - status = napi_get_cb_info(env, info, &argc, args, NULL, NULL); - if (status != napi_ok) { - napi_throw_error(env, NULL, "Failed to parse arguments"); - return NULL; - } - - if (argc < 1) { - napi_throw_error(env, NULL, "Wrong number of arguments"); - return NULL; - } - - napi_value arg = args[0]; - char buffer[256]; - size_t buffer_size = sizeof(buffer); - size_t copied; - - status = napi_get_value_string_utf8(env, arg, buffer, buffer_size, &copied); - if (status != napi_ok) { - napi_throw_error(env, NULL, "Failed to get string value"); - return NULL; - } - - buffer[copied] = '\\0'; - printf("Got string: %s\\n", buffer); - -${parts - .map(([_, symbol_name, types]) => { - if (test_skipped.includes(symbol_name)) return ""; - return ` -if (strcmp(buffer, "${symbol_name}") == 0) { - ${types.decls.join("\n")} - - ${symbol_name}(${types.args.join(", ")}); - return NULL; -} -`; - }) - .join("\n\n")} - - napi_throw_error(env, NULL, "Function not found"); - - return NULL; -} - -napi_value Init(napi_env env, napi_value exports) { - napi_status status; - napi_value fn_call_uv_func; - - // Register call_uv_func function - status = - napi_create_function(env, NULL, 0, call_uv_func, NULL, &fn_call_uv_func); - if (status != napi_ok) { - napi_throw_error(env, NULL, "Failed to create call_uv_func function"); - return NULL; - } - - status = napi_set_named_property(env, exports, "callUVFunc", fn_call_uv_func); - if (status != napi_ok) { - napi_throw_error(env, NULL, - "Failed to add call_uv_func function to exports"); - return NULL; - } - - return exports; -} - -NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) -`; - -const plugin_path_ = join(import.meta.dir, "../", "../", "../", "../", "test", "napi", "uv-stub-stuff", "plugin.c"); -await Bun.write(plugin_path_, test_plugin_contents); - -if (Bun.which("clang-format")) { - await Bun.$`clang-format -i ${plugin_path_}`.quiet(); -} diff --git a/src/jsc/bindings/libuv/generate_uv_stubs.ts b/src/jsc/bindings/libuv/generate_uv_stubs.ts new file mode 100644 index 000000000000..a1630c6f6b08 --- /dev/null +++ b/src/jsc/bindings/libuv/generate_uv_stubs.ts @@ -0,0 +1,121 @@ +import { join } from "path"; +import { exported, symbols } from "./generate_uv_stubs_constants"; + +function assert(condition: boolean, message: string) { + if (!condition) { + console.error(message); + process.exit(1); + } +} + +async function writeC(path: string, contents: string) { + await Bun.write(path, contents); + if (Bun.which("clang-format")) { + await Bun.$`clang-format -i ${path}`.quiet(); + } +} + +await writeC( + join(import.meta.dir, "../uv-stubs.c"), + `// GENERATED CODE - DO NOT MODIFY BY HAND +#include "uv-polyfills.h" + +// A stub reads none of its arguments and does not return, so it has no use for +// the function's prototype. +#define STUB(name) UV_EXTERN void name(void) { __bun_throw_not_implemented(#name); __builtin_unreachable(); } + +${symbols.map(name => `STUB(${name})`).join("\n")} +`, +); + +/** + * The linker export lists each carry the uv_* names as one contiguous run of + * lines. Make that run hold exactly `exported`: names already listed keep + * their place, removed ones go, new ones are appended. + */ +async function syncExportList(file: string, line: (name: string) => string) { + const path = join(import.meta.dir, "../../../", file); + const text = await Bun.file(path).text(); + const eol = text.includes("\r\n") ? "\r\n" : "\n"; + const lines = text.split(eol); + const isUv = (l: string) => /^\s*_?uv_\w+;?$/.test(l); + const first = lines.findIndex(isUv); + const last = lines.findLastIndex(isUv); + assert(first !== -1, `${file}: no uv_* names found`); + assert(lines.slice(first, last + 1).every(isUv), `${file}: uv_* names are not contiguous`); + const wanted = new Set(exported); + const kept = lines + .slice(first, last + 1) + .map(l => l.match(/uv_\w+/)![0]) + .filter(name => wanted.has(name)); + const listed = new Set(kept); + const names = [...kept, ...exported.filter(name => !listed.has(name))]; + lines.splice(first, last - first + 1, ...names.map(line)); + await Bun.write(path, lines.join(eol)); +} + +await syncExportList("symbols.def", name => ` ${name}`); +await syncExportList("symbols.txt", name => `_${name}`); +await syncExportList("linker.lds", name => ` ${name};`); +await syncExportList("linker-freebsd.lds", name => ` ${name};`); + +await writeC( + join(import.meta.dir, "../../../../test/napi/uv-stub-stuff/plugin.c"), + `// GENERATED CODE - DO NOT MODIFY BY HAND +#include + +#include +#include + +#define UV_FUNCTIONS(X) \\ +${symbols.map(name => ` X(${name})`).join(" \\\n")} + +// Declared and called without arguments: a stub reads none. +#define DECLARE(name) extern void name(void); +UV_FUNCTIONS(DECLARE) + +#define ENTRY(name) { #name, name }, +static const struct { + const char* name; + void (*function)(void); +} uv_functions[] = { UV_FUNCTIONS(ENTRY) }; + +napi_value call_uv_func(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value arg; + if (napi_get_cb_info(env, info, &argc, &arg, NULL, NULL) != napi_ok || argc < 1) { + napi_throw_error(env, NULL, "Wrong number of arguments"); + return NULL; + } + + char name[256]; + if (napi_get_value_string_utf8(env, arg, name, sizeof(name), NULL) != napi_ok) { + napi_throw_error(env, NULL, "Failed to get string value"); + return NULL; + } + printf("Got string: %s\\n", name); + + for (size_t i = 0; i < sizeof(uv_functions) / sizeof(uv_functions[0]); i++) { + if (strcmp(name, uv_functions[i].name) == 0) { + uv_functions[i].function(); + return NULL; + } + } + + napi_throw_error(env, NULL, "Function not found"); + return NULL; +} + +napi_value Init(napi_env env, napi_value exports) { + napi_value function; + if (napi_create_function(env, NULL, 0, call_uv_func, NULL, &function) != napi_ok || + napi_set_named_property(env, exports, "callUVFunc", function) != napi_ok) { + napi_throw_error(env, NULL, "Failed to export callUVFunc"); + return NULL; + } + return exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) +`, +); diff --git a/src/jsc/bindings/libuv/generate_uv_posix_stubs_constants.ts b/src/jsc/bindings/libuv/generate_uv_stubs_constants.ts similarity index 89% rename from src/jsc/bindings/libuv/generate_uv_posix_stubs_constants.ts rename to src/jsc/bindings/libuv/generate_uv_stubs_constants.ts index b28f652ed91c..3eadf85c3e94 100644 --- a/src/jsc/bindings/libuv/generate_uv_posix_stubs_constants.ts +++ b/src/jsc/bindings/libuv/generate_uv_stubs_constants.ts @@ -1,15 +1,21 @@ -export const test_skipped = [ - "uv_getrusage_thread", - "uv_thread_detach", - "uv_thread_getname", - "uv_thread_getpriority", - "uv_thread_setname", - "uv_udp_try_send2", - "uv_utf16_length_as_wtf8", - "uv_utf16_to_wtf8", - "uv_wtf8_length_as_utf16", - "uv_wtf8_to_utf16", - "uv_thread_setpriority", +/** + * uv functions bun implements. Everything in `symbols` is a stub that reports + * "unsupported uv function" and aborts. + */ +export const polyfills = [ + // uv-polyfills.c + "uv_hrtime", + "uv_mutex_destroy", + "uv_mutex_init", + "uv_mutex_init_recursive", + "uv_mutex_lock", + "uv_mutex_trylock", + "uv_mutex_unlock", + "uv_once", + "uv_os_getpid", + "uv_os_getppid", + // wtf-bindings.cpp + "uv_tty_reset_mode", ]; export const symbols = [ @@ -123,8 +129,6 @@ export const symbols = [ "uv_handle_size", "uv_handle_type_name", "uv_has_ref", - // Defined in uv-posix-polyfills.cpp - // "uv_hrtime", "uv_idle_init", "uv_idle_start", "uv_idle_stop", @@ -162,16 +166,7 @@ export const symbols = [ "uv_loop_size", "uv_metrics_idle_time", "uv_metrics_info", - // Defined in uv-posix-polyfills.cpp - // "uv_mutex_destroy", - // "uv_mutex_init", - // "uv_mutex_init_recursive", - // "uv_mutex_lock", - // "uv_mutex_trylock", - // "uv_mutex_unlock", "uv_now", - // Defined in uv-posix-polyfills.cpp - // "uv_once", "uv_open_osfhandle", "uv_os_environ", "uv_os_free_environ", @@ -182,10 +177,6 @@ export const symbols = [ "uv_os_get_passwd2", "uv_os_getenv", "uv_os_gethostname", - // Defined in uv-posix-polyfills.cpp - // "uv_os_getpid", - // Defined in uv-posix-polyfills.cpp - // "uv_os_getppid", "uv_os_getpriority", "uv_os_homedir", "uv_os_setenv", @@ -297,8 +288,6 @@ export const symbols = [ "uv_tty_get_vterm_state", "uv_tty_get_winsize", "uv_tty_init", - // Defined in wtf-bindings.cpp - // "uv_tty_reset_mode", "uv_tty_set_mode", "uv_tty_set_vterm_state", "uv_udp_bind", @@ -336,3 +325,6 @@ export const symbols = [ "uv_wtf8_length_as_utf16", "uv_wtf8_to_utf16", ]; + +/** Every uv_* name bun exports: src/symbols.def, src/symbols.txt, src/linker.lds, src/linker-freebsd.lds. */ +export const exported = [...symbols, ...polyfills].sort(); diff --git a/src/jsc/bindings/libuv/package.json b/src/jsc/bindings/libuv/package.json deleted file mode 100644 index 3dae5e9c9436..000000000000 --- a/src/jsc/bindings/libuv/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "dependencies": { - "tree-sitter": "^0.22.4", - "tree-sitter-c": "^0.23.5" - } -} diff --git a/src/jsc/bindings/libuv/uv.h b/src/jsc/bindings/libuv/uv.h deleted file mode 100644 index 3c3076397dfa..000000000000 --- a/src/jsc/bindings/libuv/uv.h +++ /dev/null @@ -1,1953 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -/* See https://github.com/libuv/libuv#documentation for documentation. */ - -#ifndef UV_H -#define UV_H -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(BUILDING_UV_SHARED) && defined(USING_UV_SHARED) -#error "Define either BUILDING_UV_SHARED or USING_UV_SHARED, not both." -#endif - -#ifndef UV_EXTERN -#ifdef _WIN32 -/* Windows - set up dll import/export decorators. */ -#if defined(BUILDING_UV_SHARED) -/* Building shared library. */ -#define UV_EXTERN __declspec(dllexport) -#elif defined(USING_UV_SHARED) -/* Using shared library. */ -#define UV_EXTERN __declspec(dllimport) -#else -/* Building static library. */ -#define UV_EXTERN /* nothing */ -#endif -#elif __GNUC__ >= 4 -#define UV_EXTERN __attribute__((visibility("default"))) -#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550) /* Sun Studio >= 8 */ -#define UV_EXTERN __global -#else -#define UV_EXTERN /* nothing */ -#endif -#endif /* UV_EXTERN */ - -#include "uv/errno.h" -#include "uv/version.h" -#include -#include -#include -#include - -/* Internal type, do not use. */ -struct uv__queue { - struct uv__queue* next; - struct uv__queue* prev; -}; - -/* This directory is only on the include path for non-Windows builds; Windows links the real libuv. */ -#include "uv/unix.h" - -/* Expand this list if necessary. */ -#define UV_ERRNO_MAP(XX) \ - XX(E2BIG, "argument list too long") \ - XX(EACCES, "permission denied") \ - XX(EADDRINUSE, "address already in use") \ - XX(EADDRNOTAVAIL, "address not available") \ - XX(EAFNOSUPPORT, "address family not supported") \ - XX(EAGAIN, "resource temporarily unavailable") \ - XX(EAI_ADDRFAMILY, "address family not supported") \ - XX(EAI_AGAIN, "temporary failure") \ - XX(EAI_BADFLAGS, "bad ai_flags value") \ - XX(EAI_BADHINTS, "invalid value for hints") \ - XX(EAI_CANCELED, "request canceled") \ - XX(EAI_FAIL, "permanent failure") \ - XX(EAI_FAMILY, "ai_family not supported") \ - XX(EAI_MEMORY, "out of memory") \ - XX(EAI_NODATA, "no address") \ - XX(EAI_NONAME, "unknown node or service") \ - XX(EAI_OVERFLOW, "argument buffer overflow") \ - XX(EAI_PROTOCOL, "resolved protocol is unknown") \ - XX(EAI_SERVICE, "service not available for socket type") \ - XX(EAI_SOCKTYPE, "socket type not supported") \ - XX(EALREADY, "connection already in progress") \ - XX(EBADF, "bad file descriptor") \ - XX(EBUSY, "resource busy or locked") \ - XX(ECANCELED, "operation canceled") \ - XX(ECHARSET, "invalid Unicode character") \ - XX(ECONNABORTED, "software caused connection abort") \ - XX(ECONNREFUSED, "connection refused") \ - XX(ECONNRESET, "connection reset by peer") \ - XX(EDESTADDRREQ, "destination address required") \ - XX(EEXIST, "file already exists") \ - XX(EFAULT, "bad address in system call argument") \ - XX(EFBIG, "file too large") \ - XX(EHOSTUNREACH, "host is unreachable") \ - XX(EINTR, "interrupted system call") \ - XX(EINVAL, "invalid argument") \ - XX(EIO, "i/o error") \ - XX(EISCONN, "socket is already connected") \ - XX(EISDIR, "illegal operation on a directory") \ - XX(ELOOP, "too many symbolic links encountered") \ - XX(EMFILE, "too many open files") \ - XX(EMSGSIZE, "message too long") \ - XX(ENAMETOOLONG, "name too long") \ - XX(ENETDOWN, "network is down") \ - XX(ENETUNREACH, "network is unreachable") \ - XX(ENFILE, "file table overflow") \ - XX(ENOBUFS, "no buffer space available") \ - XX(ENODEV, "no such device") \ - XX(ENOENT, "no such file or directory") \ - XX(ENOMEM, "not enough memory") \ - XX(ENONET, "machine is not on the network") \ - XX(ENOPROTOOPT, "protocol not available") \ - XX(ENOSPC, "no space left on device") \ - XX(ENOSYS, "function not implemented") \ - XX(ENOTCONN, "socket is not connected") \ - XX(ENOTDIR, "not a directory") \ - XX(ENOTEMPTY, "directory not empty") \ - XX(ENOTSOCK, "socket operation on non-socket") \ - XX(ENOTSUP, "operation not supported on socket") \ - XX(EOVERFLOW, "value too large for defined data type") \ - XX(EPERM, "operation not permitted") \ - XX(EPIPE, "broken pipe") \ - XX(EPROTO, "protocol error") \ - XX(EPROTONOSUPPORT, "protocol not supported") \ - XX(EPROTOTYPE, "protocol wrong type for socket") \ - XX(ERANGE, "result too large") \ - XX(EROFS, "read-only file system") \ - XX(ESHUTDOWN, "cannot send after transport endpoint shutdown") \ - XX(ESPIPE, "invalid seek") \ - XX(ESRCH, "no such process") \ - XX(ETIMEDOUT, "connection timed out") \ - XX(ETXTBSY, "text file is busy") \ - XX(EXDEV, "cross-device link not permitted") \ - XX(UNKNOWN, "unknown error") \ - XX(EOF, "end of file") \ - XX(ENXIO, "no such device or address") \ - XX(EMLINK, "too many links") \ - XX(EHOSTDOWN, "host is down") \ - XX(EREMOTEIO, "remote I/O error") \ - XX(ENOTTY, "inappropriate ioctl for device") \ - XX(EFTYPE, "inappropriate file type or format") \ - XX(EILSEQ, "illegal byte sequence") \ - XX(ESOCKTNOSUPPORT, "socket type not supported") \ - XX(ENODATA, "no data available") \ - XX(EUNATCH, "protocol driver not attached") \ - XX(ENOEXEC, "exec format error") - -#define UV_HANDLE_TYPE_MAP(XX) \ - XX(ASYNC, async) \ - XX(CHECK, check) \ - XX(FS_EVENT, fs_event) \ - XX(FS_POLL, fs_poll) \ - XX(HANDLE, handle) \ - XX(IDLE, idle) \ - XX(NAMED_PIPE, pipe) \ - XX(POLL, poll) \ - XX(PREPARE, prepare) \ - XX(PROCESS, process) \ - XX(STREAM, stream) \ - XX(TCP, tcp) \ - XX(TIMER, timer) \ - XX(TTY, tty) \ - XX(UDP, udp) \ - XX(SIGNAL, signal) - -#define UV_REQ_TYPE_MAP(XX) \ - XX(REQ, req) \ - XX(CONNECT, connect) \ - XX(WRITE, write) \ - XX(SHUTDOWN, shutdown) \ - XX(UDP_SEND, udp_send) \ - XX(FS, fs) \ - XX(WORK, work) \ - XX(GETADDRINFO, getaddrinfo) \ - XX(GETNAMEINFO, getnameinfo) \ - XX(RANDOM, random) - -typedef enum { -#define XX(code, _) UV_##code = UV__##code, - UV_ERRNO_MAP(XX) -#undef XX - UV_ERRNO_MAX - = UV__EOF - 1 -} uv_errno_t; - -typedef enum { - UV_UNKNOWN_HANDLE = 0, -#define XX(uc, lc) UV_##uc, - UV_HANDLE_TYPE_MAP(XX) -#undef XX - UV_FILE, - UV_HANDLE_TYPE_MAX -} uv_handle_type; - -typedef enum { - UV_UNKNOWN_REQ = 0, -#define XX(uc, lc) UV_##uc, - UV_REQ_TYPE_MAP(XX) -#undef XX - UV_REQ_TYPE_PRIVATE - UV_REQ_TYPE_MAX -} uv_req_type; - -/* Handle types. */ -typedef struct uv_loop_s uv_loop_t; -typedef struct uv_handle_s uv_handle_t; -typedef struct uv_dir_s uv_dir_t; -typedef struct uv_stream_s uv_stream_t; -typedef struct uv_tcp_s uv_tcp_t; -typedef struct uv_udp_s uv_udp_t; -typedef struct uv_pipe_s uv_pipe_t; -typedef struct uv_tty_s uv_tty_t; -typedef struct uv_poll_s uv_poll_t; -typedef struct uv_timer_s uv_timer_t; -typedef struct uv_prepare_s uv_prepare_t; -typedef struct uv_check_s uv_check_t; -typedef struct uv_idle_s uv_idle_t; -typedef struct uv_async_s uv_async_t; -typedef struct uv_process_s uv_process_t; -typedef struct uv_fs_event_s uv_fs_event_t; -typedef struct uv_fs_poll_s uv_fs_poll_t; -typedef struct uv_signal_s uv_signal_t; - -/* Request types. */ -typedef struct uv_req_s uv_req_t; -typedef struct uv_getaddrinfo_s uv_getaddrinfo_t; -typedef struct uv_getnameinfo_s uv_getnameinfo_t; -typedef struct uv_shutdown_s uv_shutdown_t; -typedef struct uv_write_s uv_write_t; -typedef struct uv_connect_s uv_connect_t; -typedef struct uv_udp_send_s uv_udp_send_t; -typedef struct uv_fs_s uv_fs_t; -typedef struct uv_work_s uv_work_t; -typedef struct uv_random_s uv_random_t; - -/* None of the above. */ -typedef struct uv_env_item_s uv_env_item_t; -typedef struct uv_cpu_info_s uv_cpu_info_t; -typedef struct uv_interface_address_s uv_interface_address_t; -typedef struct uv_dirent_s uv_dirent_t; -typedef struct uv_passwd_s uv_passwd_t; -typedef struct uv_group_s uv_group_t; -typedef struct uv_utsname_s uv_utsname_t; -typedef struct uv_statfs_s uv_statfs_t; - -typedef struct uv_metrics_s uv_metrics_t; - -typedef enum { - UV_LOOP_BLOCK_SIGNAL = 0, - UV_METRICS_IDLE_TIME, - UV_LOOP_USE_IO_URING_SQPOLL -#define UV_LOOP_USE_IO_URING_SQPOLL UV_LOOP_USE_IO_URING_SQPOLL -} uv_loop_option; - -typedef enum { - UV_RUN_DEFAULT = 0, - UV_RUN_ONCE, - UV_RUN_NOWAIT -} uv_run_mode; - -UV_EXTERN unsigned int uv_version(void); -UV_EXTERN const char* uv_version_string(void); - -typedef void* (*uv_malloc_func)(size_t size); -typedef void* (*uv_realloc_func)(void* ptr, size_t size); -typedef void* (*uv_calloc_func)(size_t count, size_t size); -typedef void (*uv_free_func)(void* ptr); - -UV_EXTERN void uv_library_shutdown(void); - -UV_EXTERN int uv_replace_allocator(uv_malloc_func malloc_func, - uv_realloc_func realloc_func, - uv_calloc_func calloc_func, - uv_free_func free_func); - -UV_EXTERN uv_loop_t* uv_default_loop(void); -UV_EXTERN int uv_loop_init(uv_loop_t* loop); -UV_EXTERN int uv_loop_close(uv_loop_t* loop); -/* - * NOTE: - * This function is DEPRECATED, users should - * allocate the loop manually and use uv_loop_init instead. - */ -UV_EXTERN uv_loop_t* uv_loop_new(void); -/* - * NOTE: - * This function is DEPRECATED. Users should use - * uv_loop_close and free the memory manually instead. - */ -UV_EXTERN void uv_loop_delete(uv_loop_t*); -UV_EXTERN size_t uv_loop_size(void); -UV_EXTERN int uv_loop_alive(const uv_loop_t* loop); -UV_EXTERN int uv_loop_configure(uv_loop_t* loop, uv_loop_option option, ...); -UV_EXTERN int uv_loop_fork(uv_loop_t* loop); - -UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode); -UV_EXTERN void uv_stop(uv_loop_t*); - -UV_EXTERN void uv_ref(uv_handle_t*); -UV_EXTERN void uv_unref(uv_handle_t*); -UV_EXTERN int uv_has_ref(const uv_handle_t*); - -UV_EXTERN void uv_update_time(uv_loop_t*); -UV_EXTERN uint64_t uv_now(const uv_loop_t*); - -UV_EXTERN int uv_backend_fd(const uv_loop_t*); -UV_EXTERN int uv_backend_timeout(const uv_loop_t*); - -typedef void (*uv_alloc_cb)(uv_handle_t* handle, - size_t suggested_size, - uv_buf_t* buf); -typedef void (*uv_read_cb)(uv_stream_t* stream, - ssize_t nread, - const uv_buf_t* buf); -typedef void (*uv_write_cb)(uv_write_t* req, int status); -typedef void (*uv_connect_cb)(uv_connect_t* req, int status); -typedef void (*uv_shutdown_cb)(uv_shutdown_t* req, int status); -typedef void (*uv_connection_cb)(uv_stream_t* server, int status); -typedef void (*uv_close_cb)(uv_handle_t* handle); -typedef void (*uv_poll_cb)(uv_poll_t* handle, int status, int events); -typedef void (*uv_timer_cb)(uv_timer_t* handle); -typedef void (*uv_async_cb)(uv_async_t* handle); -typedef void (*uv_prepare_cb)(uv_prepare_t* handle); -typedef void (*uv_check_cb)(uv_check_t* handle); -typedef void (*uv_idle_cb)(uv_idle_t* handle); -typedef void (*uv_exit_cb)(uv_process_t*, int64_t exit_status, int term_signal); -typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg); -typedef void (*uv_fs_cb)(uv_fs_t* req); -typedef void (*uv_work_cb)(uv_work_t* req); -typedef void (*uv_after_work_cb)(uv_work_t* req, int status); -typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req, - int status, - struct addrinfo* res); -typedef void (*uv_getnameinfo_cb)(uv_getnameinfo_t* req, - int status, - const char* hostname, - const char* service); -typedef void (*uv_random_cb)(uv_random_t* req, - int status, - void* buf, - size_t buflen); - -typedef enum { - UV_CLOCK_MONOTONIC, - UV_CLOCK_REALTIME -} uv_clock_id; - -/* XXX(bnoordhuis) not 2038-proof, https://github.com/libuv/libuv/issues/3864 */ -typedef struct { - long tv_sec; - long tv_nsec; -} uv_timespec_t; - -typedef struct { - int64_t tv_sec; - int32_t tv_nsec; -} uv_timespec64_t; - -/* XXX(bnoordhuis) not 2038-proof, https://github.com/libuv/libuv/issues/3864 */ -typedef struct { - long tv_sec; - long tv_usec; -} uv_timeval_t; - -typedef struct { - int64_t tv_sec; - int32_t tv_usec; -} uv_timeval64_t; - -typedef struct { - uint64_t st_dev; - uint64_t st_mode; - uint64_t st_nlink; - uint64_t st_uid; - uint64_t st_gid; - uint64_t st_rdev; - uint64_t st_ino; - uint64_t st_size; - uint64_t st_blksize; - uint64_t st_blocks; - uint64_t st_flags; - uint64_t st_gen; - uv_timespec_t st_atim; - uv_timespec_t st_mtim; - uv_timespec_t st_ctim; - uv_timespec_t st_birthtim; -} uv_stat_t; - -typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, - const char* filename, - int events, - int status); - -typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle, - int status, - const uv_stat_t* prev, - const uv_stat_t* curr); - -typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum); - -typedef enum { - UV_LEAVE_GROUP = 0, - UV_JOIN_GROUP -} uv_membership; - -UV_EXTERN int uv_translate_sys_error(int sys_errno); - -UV_EXTERN const char* uv_strerror(int err); -UV_EXTERN char* uv_strerror_r(int err, char* buf, size_t buflen); - -UV_EXTERN const char* uv_err_name(int err); -UV_EXTERN char* uv_err_name_r(int err, char* buf, size_t buflen); - -#define UV_REQ_FIELDS \ - /* public */ \ - void* data; \ - /* read-only */ \ - uv_req_type type; \ - /* private */ \ - void* reserved[6]; \ - UV_REQ_PRIVATE_FIELDS - -/* Abstract base class of all requests. */ -struct uv_req_s { - UV_REQ_FIELDS -}; - -/* Platform-specific request types. */ -UV_PRIVATE_REQ_TYPES - -UV_EXTERN int uv_shutdown(uv_shutdown_t* req, - uv_stream_t* handle, - uv_shutdown_cb cb); - -struct uv_shutdown_s { - UV_REQ_FIELDS - uv_stream_t* handle; - uv_shutdown_cb cb; - UV_SHUTDOWN_PRIVATE_FIELDS -}; - -#define UV_HANDLE_FIELDS \ - /* public */ \ - void* data; \ - /* read-only */ \ - uv_loop_t* loop; \ - uv_handle_type type; \ - /* private */ \ - uv_close_cb close_cb; \ - struct uv__queue handle_queue; \ - union { \ - int fd; \ - void* reserved[4]; \ - } u; \ - UV_HANDLE_PRIVATE_FIELDS - -/* The abstract base class of all handles. */ -struct uv_handle_s { - UV_HANDLE_FIELDS -}; - -UV_EXTERN size_t uv_handle_size(uv_handle_type type); -UV_EXTERN uv_handle_type uv_handle_get_type(const uv_handle_t* handle); -UV_EXTERN const char* uv_handle_type_name(uv_handle_type type); -UV_EXTERN void* uv_handle_get_data(const uv_handle_t* handle); -UV_EXTERN uv_loop_t* uv_handle_get_loop(const uv_handle_t* handle); -UV_EXTERN void uv_handle_set_data(uv_handle_t* handle, void* data); - -UV_EXTERN size_t uv_req_size(uv_req_type type); -UV_EXTERN void* uv_req_get_data(const uv_req_t* req); -UV_EXTERN void uv_req_set_data(uv_req_t* req, void* data); -UV_EXTERN uv_req_type uv_req_get_type(const uv_req_t* req); -UV_EXTERN const char* uv_req_type_name(uv_req_type type); - -UV_EXTERN int uv_is_active(const uv_handle_t* handle); - -UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg); - -/* Helpers for ad hoc debugging, no API/ABI stability guaranteed. */ -UV_EXTERN void uv_print_all_handles(uv_loop_t* loop, FILE* stream); -UV_EXTERN void uv_print_active_handles(uv_loop_t* loop, FILE* stream); - -UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb); - -UV_EXTERN int uv_send_buffer_size(uv_handle_t* handle, int* value); -UV_EXTERN int uv_recv_buffer_size(uv_handle_t* handle, int* value); - -UV_EXTERN int uv_fileno(const uv_handle_t* handle, uv_os_fd_t* fd); - -UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len); - -UV_EXTERN int uv_pipe(uv_file fds[2], int read_flags, int write_flags); -UV_EXTERN int uv_socketpair(int type, - int protocol, - uv_os_sock_t socket_vector[2], - int flags0, - int flags1); - -#define UV_STREAM_FIELDS \ - /* number of bytes queued for writing */ \ - size_t write_queue_size; \ - uv_alloc_cb alloc_cb; \ - uv_read_cb read_cb; \ - /* private */ \ - UV_STREAM_PRIVATE_FIELDS - -/* - * uv_stream_t is a subclass of uv_handle_t. - * - * uv_stream is an abstract class. - * - * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t and uv_tty_t. - */ -struct uv_stream_s { - UV_HANDLE_FIELDS - UV_STREAM_FIELDS -}; - -UV_EXTERN size_t uv_stream_get_write_queue_size(const uv_stream_t* stream); - -UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb); -UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client); - -UV_EXTERN int uv_read_start(uv_stream_t*, - uv_alloc_cb alloc_cb, - uv_read_cb read_cb); -UV_EXTERN int uv_read_stop(uv_stream_t*); - -UV_EXTERN int uv_write(uv_write_t* req, - uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - uv_write_cb cb); -UV_EXTERN int uv_write2(uv_write_t* req, - uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - uv_stream_t* send_handle, - uv_write_cb cb); -UV_EXTERN int uv_try_write(uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs); -UV_EXTERN int uv_try_write2(uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - uv_stream_t* send_handle); - -/* uv_write_t is a subclass of uv_req_t. */ -struct uv_write_s { - UV_REQ_FIELDS - uv_write_cb cb; - uv_stream_t* send_handle; /* TODO: make private and unix-only in v2.x. */ - uv_stream_t* handle; - UV_WRITE_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_is_readable(const uv_stream_t* handle); -UV_EXTERN int uv_is_writable(const uv_stream_t* handle); - -UV_EXTERN int uv_stream_set_blocking(uv_stream_t* handle, int blocking); - -UV_EXTERN int uv_is_closing(const uv_handle_t* handle); - -/* - * uv_tcp_t is a subclass of uv_stream_t. - * - * Represents a TCP stream or TCP server. - */ -struct uv_tcp_s { - UV_HANDLE_FIELDS - UV_STREAM_FIELDS - UV_TCP_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle); -UV_EXTERN int uv_tcp_init_ex(uv_loop_t*, uv_tcp_t* handle, unsigned int flags); -UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock); -UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable); -UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle, - int enable, - unsigned int delay); -UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable); - -enum uv_tcp_flags { - /* Used with uv_tcp_bind, when an IPv6 address is used. */ - UV_TCP_IPV6ONLY = 1, - - /* Enable SO_REUSEPORT socket option when binding the handle. - * This allows completely duplicate bindings by multiple processes - * or threads if they all set SO_REUSEPORT before binding the port. - * Incoming connections are distributed across the participating - * listener sockets. - * - * This flag is available only on Linux 3.9+, DragonFlyBSD 3.6+, - * FreeBSD 12.0+, Solaris 11.4, and AIX 7.2.5+ for now. - */ - UV_TCP_REUSEPORT = 2, -}; - -UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, - const struct sockaddr* addr, - unsigned int flags); -UV_EXTERN int uv_tcp_getsockname(const uv_tcp_t* handle, - struct sockaddr* name, - int* namelen); -UV_EXTERN int uv_tcp_getpeername(const uv_tcp_t* handle, - struct sockaddr* name, - int* namelen); -UV_EXTERN int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb); -UV_EXTERN int uv_tcp_connect(uv_connect_t* req, - uv_tcp_t* handle, - const struct sockaddr* addr, - uv_connect_cb cb); - -/* uv_connect_t is a subclass of uv_req_t. */ -struct uv_connect_s { - UV_REQ_FIELDS - uv_connect_cb cb; - uv_stream_t* handle; - UV_CONNECT_PRIVATE_FIELDS -}; - -/* - * UDP support. - */ - -enum uv_udp_flags { - /* Disables dual stack mode. */ - UV_UDP_IPV6ONLY = 1, - /* - * Indicates message was truncated because read buffer was too small. The - * remainder was discarded by the OS. Used in uv_udp_recv_cb. - */ - UV_UDP_PARTIAL = 2, - /* - * Indicates if SO_REUSEADDR will be set when binding the handle. - * This sets the SO_REUSEPORT socket flag on the BSDs (except for - * DragonFlyBSD), OS X, and other platforms where SO_REUSEPORTs don't - * have the capability of load balancing, as the opposite of what - * UV_UDP_REUSEPORT would do. On other Unix platforms, it sets the - * SO_REUSEADDR flag. What that means is that multiple threads or - * processes can bind to the same address without error (provided - * they all set the flag) but only the last one to bind will receive - * any traffic, in effect "stealing" the port from the previous listener. - */ - UV_UDP_REUSEADDR = 4, - /* - * Indicates that the message was received by recvmmsg, so the buffer provided - * must not be freed by the recv_cb callback. - */ - UV_UDP_MMSG_CHUNK = 8, - /* - * Indicates that the buffer provided has been fully utilized by recvmmsg and - * that it should now be freed by the recv_cb callback. When this flag is set - * in uv_udp_recv_cb, nread will always be 0 and addr will always be NULL. - */ - UV_UDP_MMSG_FREE = 16, - /* - * Indicates if IP_RECVERR/IPV6_RECVERR will be set when binding the handle. - * This sets IP_RECVERR for IPv4 and IPV6_RECVERR for IPv6 UDP sockets on - * Linux. This stops the Linux kernel from suppressing some ICMP error - * messages and enables full ICMP error reporting for faster failover. - * This flag is no-op on platforms other than Linux. - */ - UV_UDP_LINUX_RECVERR = 32, - /* - * Indicates if SO_REUSEPORT will be set when binding the handle. - * This sets the SO_REUSEPORT socket option on supported platforms. - * Unlike UV_UDP_REUSEADDR, this flag will make multiple threads or - * processes that are binding to the same address and port "share" - * the port, which means incoming datagrams are distributed across - * the receiving sockets among threads or processes. - * - * This flag is available only on Linux 3.9+, DragonFlyBSD 3.6+, - * FreeBSD 12.0+, Solaris 11.4, and AIX 7.2.5+ for now. - */ - UV_UDP_REUSEPORT = 64, - /* - * Indicates that recvmmsg should be used, if available. - */ - UV_UDP_RECVMMSG = 256 -}; - -typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status); -typedef void (*uv_udp_recv_cb)(uv_udp_t* handle, - ssize_t nread, - const uv_buf_t* buf, - const struct sockaddr* addr, - unsigned flags); - -/* uv_udp_t is a subclass of uv_handle_t. */ -struct uv_udp_s { - UV_HANDLE_FIELDS - /* read-only */ - /* - * Number of bytes queued for sending. This field strictly shows how much - * information is currently queued. - */ - size_t send_queue_size; - /* - * Number of send requests currently in the queue awaiting to be processed. - */ - size_t send_queue_count; - UV_UDP_PRIVATE_FIELDS -}; - -/* uv_udp_send_t is a subclass of uv_req_t. */ -struct uv_udp_send_s { - UV_REQ_FIELDS - uv_udp_t* handle; - uv_udp_send_cb cb; - UV_UDP_SEND_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle); -UV_EXTERN int uv_udp_init_ex(uv_loop_t*, uv_udp_t* handle, unsigned int flags); -UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock); -UV_EXTERN int uv_udp_bind(uv_udp_t* handle, - const struct sockaddr* addr, - unsigned int flags); -UV_EXTERN int uv_udp_connect(uv_udp_t* handle, const struct sockaddr* addr); - -UV_EXTERN int uv_udp_getpeername(const uv_udp_t* handle, - struct sockaddr* name, - int* namelen); -UV_EXTERN int uv_udp_getsockname(const uv_udp_t* handle, - struct sockaddr* name, - int* namelen); -UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle, - const char* multicast_addr, - const char* interface_addr, - uv_membership membership); -UV_EXTERN int uv_udp_set_source_membership(uv_udp_t* handle, - const char* multicast_addr, - const char* interface_addr, - const char* source_addr, - uv_membership membership); -UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on); -UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl); -UV_EXTERN int uv_udp_set_multicast_interface(uv_udp_t* handle, - const char* interface_addr); -UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on); -UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl); -UV_EXTERN int uv_udp_send(uv_udp_send_t* req, - uv_udp_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - const struct sockaddr* addr, - uv_udp_send_cb send_cb); -UV_EXTERN int uv_udp_try_send(uv_udp_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - const struct sockaddr* addr); -UV_EXTERN int uv_udp_try_send2(uv_udp_t* handle, - unsigned int count, - uv_buf_t* bufs[/*count*/], - unsigned int nbufs[/*count*/], - struct sockaddr* addrs[/*count*/], - unsigned int flags); -UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, - uv_alloc_cb alloc_cb, - uv_udp_recv_cb recv_cb); -UV_EXTERN int uv_udp_using_recvmmsg(const uv_udp_t* handle); -UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle); -UV_EXTERN size_t uv_udp_get_send_queue_size(const uv_udp_t* handle); -UV_EXTERN size_t uv_udp_get_send_queue_count(const uv_udp_t* handle); - -/* - * uv_tty_t is a subclass of uv_stream_t. - * - * Representing a stream for the console. - */ -struct uv_tty_s { - UV_HANDLE_FIELDS - UV_STREAM_FIELDS - UV_TTY_PRIVATE_FIELDS -}; - -typedef enum { - /* Initial/normal terminal mode */ - UV_TTY_MODE_NORMAL, - /* - * Raw input mode (On Windows, ENABLE_WINDOW_INPUT is also enabled). - * May become equivalent to UV_TTY_MODE_RAW_VT in future libuv versions. - */ - UV_TTY_MODE_RAW, - /* Binary-safe I/O mode for IPC (Unix-only) */ - UV_TTY_MODE_IO, - /* Raw input mode. On Windows ENABLE_VIRTUAL_TERMINAL_INPUT is also set. */ - UV_TTY_MODE_RAW_VT -} uv_tty_mode_t; - -typedef enum { - /* - * The console supports handling of virtual terminal sequences - * (Windows10 new console, ConEmu) - */ - UV_TTY_SUPPORTED, - /* The console cannot process the virtual terminal sequence. (Legacy - * console) - */ - UV_TTY_UNSUPPORTED -} uv_tty_vtermstate_t; - -UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable); -UV_EXTERN int uv_tty_set_mode(uv_tty_t*, uv_tty_mode_t mode); -UV_EXTERN int uv_tty_reset_mode(void); -UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height); -UV_EXTERN void uv_tty_set_vterm_state(uv_tty_vtermstate_t state); -UV_EXTERN int uv_tty_get_vterm_state(uv_tty_vtermstate_t* state); - -#ifdef __cplusplus -extern "C++" { - -inline int uv_tty_set_mode(uv_tty_t* handle, int mode) -{ - return uv_tty_set_mode(handle, static_cast(mode)); -} -} -#endif - -UV_EXTERN uv_handle_type uv_guess_handle(uv_file file); - -enum { - UV_PIPE_NO_TRUNCATE = 1u << 0 -}; - -/* - * uv_pipe_t is a subclass of uv_stream_t. - * - * Representing a pipe stream or pipe server. On Windows this is a Named - * Pipe. On Unix this is a Unix domain socket. - */ -struct uv_pipe_s { - UV_HANDLE_FIELDS - UV_STREAM_FIELDS - int ipc; /* non-zero if this pipe is used for passing handles */ - UV_PIPE_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc); -UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file); -UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name); -UV_EXTERN int uv_pipe_bind2(uv_pipe_t* handle, - const char* name, - size_t namelen, - unsigned int flags); -UV_EXTERN void uv_pipe_connect(uv_connect_t* req, - uv_pipe_t* handle, - const char* name, - uv_connect_cb cb); -UV_EXTERN int uv_pipe_connect2(uv_connect_t* req, - uv_pipe_t* handle, - const char* name, - size_t namelen, - unsigned int flags, - uv_connect_cb cb); -UV_EXTERN int uv_pipe_getsockname(const uv_pipe_t* handle, - char* buffer, - size_t* size); -UV_EXTERN int uv_pipe_getpeername(const uv_pipe_t* handle, - char* buffer, - size_t* size); -UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count); -UV_EXTERN int uv_pipe_pending_count(uv_pipe_t* handle); -UV_EXTERN uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle); -UV_EXTERN int uv_pipe_chmod(uv_pipe_t* handle, int flags); - -struct uv_poll_s { - UV_HANDLE_FIELDS - uv_poll_cb poll_cb; - UV_POLL_PRIVATE_FIELDS -}; - -enum uv_poll_event { - UV_READABLE = 1, - UV_WRITABLE = 2, - UV_DISCONNECT = 4, - UV_PRIORITIZED = 8 -}; - -UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd); -UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, - uv_poll_t* handle, - uv_os_sock_t socket); -UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb); -UV_EXTERN int uv_poll_stop(uv_poll_t* handle); - -struct uv_prepare_s { - UV_HANDLE_FIELDS - UV_PREPARE_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare); -UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb); -UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare); - -struct uv_check_s { - UV_HANDLE_FIELDS - UV_CHECK_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check); -UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb); -UV_EXTERN int uv_check_stop(uv_check_t* check); - -struct uv_idle_s { - UV_HANDLE_FIELDS - UV_IDLE_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle); -UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb); -UV_EXTERN int uv_idle_stop(uv_idle_t* idle); - -struct uv_async_s { - UV_HANDLE_FIELDS - UV_ASYNC_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_async_init(uv_loop_t*, - uv_async_t* async, - uv_async_cb async_cb); -UV_EXTERN int uv_async_send(uv_async_t* async); - -/* - * uv_timer_t is a subclass of uv_handle_t. - * - * Used to get woken up at a specified time in the future. - */ -struct uv_timer_s { - UV_HANDLE_FIELDS - UV_TIMER_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle); -UV_EXTERN int uv_timer_start(uv_timer_t* handle, - uv_timer_cb cb, - uint64_t timeout, - uint64_t repeat); -UV_EXTERN int uv_timer_stop(uv_timer_t* handle); -UV_EXTERN int uv_timer_again(uv_timer_t* handle); -UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat); -UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle); -UV_EXTERN uint64_t uv_timer_get_due_in(const uv_timer_t* handle); - -/* - * uv_getaddrinfo_t is a subclass of uv_req_t. - * - * Request object for uv_getaddrinfo. - */ -struct uv_getaddrinfo_s { - UV_REQ_FIELDS - /* read-only */ - uv_loop_t* loop; - /* struct addrinfo* addrinfo is marked as private, but it really isn't. */ - UV_GETADDRINFO_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop, - uv_getaddrinfo_t* req, - uv_getaddrinfo_cb getaddrinfo_cb, - const char* node, - const char* service, - const struct addrinfo* hints); -UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai); - -/* - * uv_getnameinfo_t is a subclass of uv_req_t. - * - * Request object for uv_getnameinfo. - */ -struct uv_getnameinfo_s { - UV_REQ_FIELDS - /* read-only */ - uv_loop_t* loop; - /* host and service are marked as private, but they really aren't. */ - UV_GETNAMEINFO_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_getnameinfo(uv_loop_t* loop, - uv_getnameinfo_t* req, - uv_getnameinfo_cb getnameinfo_cb, - const struct sockaddr* addr, - int flags); - -/* uv_spawn() options. */ -typedef enum { - UV_IGNORE = 0x00, - UV_CREATE_PIPE = 0x01, - UV_INHERIT_FD = 0x02, - UV_INHERIT_STREAM = 0x04, - - /* - * When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE - * determine the direction of flow, from the child process' perspective. Both - * flags may be specified to create a duplex data stream. - */ - UV_READABLE_PIPE = 0x10, - UV_WRITABLE_PIPE = 0x20, - - /* - * When UV_CREATE_PIPE is specified, specifying UV_NONBLOCK_PIPE opens the - * handle in non-blocking mode in the child. This may cause loss of data, - * if the child is not designed to handle to encounter this mode, - * but can also be significantly more efficient. - */ - UV_NONBLOCK_PIPE = 0x40, - UV_OVERLAPPED_PIPE = 0x40 /* old name, for compatibility */ -} uv_stdio_flags; - -typedef struct uv_stdio_container_s { - uv_stdio_flags flags; - - union { - uv_stream_t* stream; - int fd; - } data; -} uv_stdio_container_t; - -typedef struct uv_process_options_s { - uv_exit_cb exit_cb; /* Called after the process exits. */ - const char* file; /* Path to program to execute. */ - /* - * Command line arguments. args[0] should be the path to the program. On - * Windows this uses CreateProcess which concatenates the arguments into a - * string this can cause some strange errors. See the note at - * windows_verbatim_arguments. - */ - char** args; - /* - * This will be set as the environ variable in the subprocess. If this is - * NULL then the parents environ will be used. - */ - char** env; - /* - * If non-null this represents a directory the subprocess should execute - * in. Stands for current working directory. - */ - const char* cwd; - /* - * Various flags that control how uv_spawn() behaves. See the definition of - * `enum uv_process_flags` below. - */ - unsigned int flags; - /* - * The `stdio` field points to an array of uv_stdio_container_t structs that - * describe the file descriptors that will be made available to the child - * process. The convention is that stdio[0] points to stdin, fd 1 is used for - * stdout, and fd 2 is stderr. - * - * Note that on windows file descriptors greater than 2 are available to the - * child process only if the child processes uses the MSVCRT runtime. - */ - int stdio_count; - uv_stdio_container_t* stdio; - /* - * Libuv can change the child process' user/group id. This happens only when - * the appropriate bits are set in the flags fields. This is not supported on - * windows; uv_spawn() will fail and set the error to UV_ENOTSUP. - */ - uv_uid_t uid; - uv_gid_t gid; -} uv_process_options_t; - -/* - * These are the flags that can be used for the uv_process_options.flags field. - */ -enum uv_process_flags { - /* - * Set the child process' user id. The user id is supplied in the `uid` field - * of the options struct. This does not work on windows; setting this flag - * will cause uv_spawn() to fail. - */ - UV_PROCESS_SETUID = (1 << 0), - /* - * Set the child process' group id. The user id is supplied in the `gid` - * field of the options struct. This does not work on windows; setting this - * flag will cause uv_spawn() to fail. - */ - UV_PROCESS_SETGID = (1 << 1), - /* - * Do not wrap any arguments in quotes, or perform any other escaping, when - * converting the argument list into a command line string. This option is - * only meaningful on Windows systems. On Unix it is silently ignored. - */ - UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2), - /* - * Spawn the child process in a detached state - this will make it a process - * group leader, and will effectively enable the child to keep running after - * the parent exits. Note that the child process will still keep the - * parent's event loop alive unless the parent process calls uv_unref() on - * the child's process handle. - */ - UV_PROCESS_DETACHED = (1 << 3), - /* - * Hide the subprocess window that would normally be created. This option is - * only meaningful on Windows systems. On Unix it is silently ignored. - */ - UV_PROCESS_WINDOWS_HIDE = (1 << 4), - /* - * Hide the subprocess console window that would normally be created. This - * option is only meaningful on Windows systems. On Unix it is silently - * ignored. - */ - UV_PROCESS_WINDOWS_HIDE_CONSOLE = (1 << 5), - /* - * Hide the subprocess GUI window that would normally be created. This - * option is only meaningful on Windows systems. On Unix it is silently - * ignored. - */ - UV_PROCESS_WINDOWS_HIDE_GUI = (1 << 6), - /* - * On Windows, if the path to the program to execute, specified in - * uv_process_options_t's file field, has a directory component, - * search for the exact file name before trying variants with - * extensions like '.exe' or '.cmd'. - */ - UV_PROCESS_WINDOWS_FILE_PATH_EXACT_NAME = (1 << 7) -}; - -/* - * uv_process_t is a subclass of uv_handle_t. - */ -struct uv_process_s { - UV_HANDLE_FIELDS - uv_exit_cb exit_cb; - int pid; - UV_PROCESS_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_spawn(uv_loop_t* loop, - uv_process_t* handle, - const uv_process_options_t* options); -UV_EXTERN int uv_process_kill(uv_process_t*, int signum); -UV_EXTERN int uv_kill(int pid, int signum); -UV_EXTERN uv_pid_t uv_process_get_pid(const uv_process_t*); - -/* - * uv_work_t is a subclass of uv_req_t. - */ -struct uv_work_s { - UV_REQ_FIELDS - uv_loop_t* loop; - uv_work_cb work_cb; - uv_after_work_cb after_work_cb; - UV_WORK_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_queue_work(uv_loop_t* loop, - uv_work_t* req, - uv_work_cb work_cb, - uv_after_work_cb after_work_cb); - -UV_EXTERN int uv_cancel(uv_req_t* req); - -struct uv_cpu_times_s { - uint64_t user; /* milliseconds */ - uint64_t nice; /* milliseconds */ - uint64_t sys; /* milliseconds */ - uint64_t idle; /* milliseconds */ - uint64_t irq; /* milliseconds */ -}; - -struct uv_cpu_info_s { - char* model; - int speed; - struct uv_cpu_times_s cpu_times; -}; - -struct uv_interface_address_s { - char* name; - char phys_addr[6]; - int is_internal; - union { - struct sockaddr_in address4; - struct sockaddr_in6 address6; - } address; - union { - struct sockaddr_in netmask4; - struct sockaddr_in6 netmask6; - } netmask; -}; - -struct uv_passwd_s { - char* username; - unsigned long uid; - unsigned long gid; - char* shell; - char* homedir; -}; - -struct uv_group_s { - char* groupname; - unsigned long gid; - char** members; -}; - -struct uv_utsname_s { - char sysname[256]; - char release[256]; - char version[256]; - char machine[256]; - /* This struct does not contain the nodename and domainname fields present in - the utsname type. domainname is a GNU extension. Both fields are referred - to as meaningless in the docs. */ -}; - -struct uv_statfs_s { - uint64_t f_type; - uint64_t f_bsize; - uint64_t f_blocks; - uint64_t f_bfree; - uint64_t f_bavail; - uint64_t f_files; - uint64_t f_ffree; - uint64_t f_spare[4]; -}; - -typedef enum { - UV_DIRENT_UNKNOWN, - UV_DIRENT_FILE, - UV_DIRENT_DIR, - UV_DIRENT_LINK, - UV_DIRENT_FIFO, - UV_DIRENT_SOCKET, - UV_DIRENT_CHAR, - UV_DIRENT_BLOCK -} uv_dirent_type_t; - -struct uv_dirent_s { - const char* name; - uv_dirent_type_t type; -}; - -UV_EXTERN char** uv_setup_args(int argc, char** argv); -UV_EXTERN int uv_get_process_title(char* buffer, size_t size); -UV_EXTERN int uv_set_process_title(const char* title); -UV_EXTERN int uv_resident_set_memory(size_t* rss); -UV_EXTERN int uv_uptime(double* uptime); -UV_EXTERN uv_os_fd_t uv_get_osfhandle(int fd); -UV_EXTERN int uv_open_osfhandle(uv_os_fd_t os_fd); - -typedef struct { - uv_timeval_t ru_utime; /* user CPU time used */ - uv_timeval_t ru_stime; /* system CPU time used */ - uint64_t ru_maxrss; /* maximum resident set size */ - uint64_t ru_ixrss; /* integral shared memory size */ - uint64_t ru_idrss; /* integral unshared data size */ - uint64_t ru_isrss; /* integral unshared stack size */ - uint64_t ru_minflt; /* page reclaims (soft page faults) */ - uint64_t ru_majflt; /* page faults (hard page faults) */ - uint64_t ru_nswap; /* swaps */ - uint64_t ru_inblock; /* block input operations */ - uint64_t ru_oublock; /* block output operations */ - uint64_t ru_msgsnd; /* IPC messages sent */ - uint64_t ru_msgrcv; /* IPC messages received */ - uint64_t ru_nsignals; /* signals received */ - uint64_t ru_nvcsw; /* voluntary context switches */ - uint64_t ru_nivcsw; /* involuntary context switches */ -} uv_rusage_t; - -UV_EXTERN int uv_getrusage(uv_rusage_t* rusage); -UV_EXTERN int uv_getrusage_thread(uv_rusage_t* rusage); - -UV_EXTERN int uv_os_homedir(char* buffer, size_t* size); -UV_EXTERN int uv_os_tmpdir(char* buffer, size_t* size); -UV_EXTERN int uv_os_get_passwd(uv_passwd_t* pwd); -UV_EXTERN void uv_os_free_passwd(uv_passwd_t* pwd); -UV_EXTERN int uv_os_get_passwd2(uv_passwd_t* pwd, uv_uid_t uid); -UV_EXTERN int uv_os_get_group(uv_group_t* grp, uv_uid_t gid); -UV_EXTERN void uv_os_free_group(uv_group_t* grp); -UV_EXTERN uv_pid_t uv_os_getpid(void); -UV_EXTERN uv_pid_t uv_os_getppid(void); - -#if defined(__PASE__) -/* On IBM i PASE, the highest process priority is -10 */ -#define UV_PRIORITY_LOW 39 /* RUNPTY(99) */ -#define UV_PRIORITY_BELOW_NORMAL 15 /* RUNPTY(50) */ -#define UV_PRIORITY_NORMAL 0 /* RUNPTY(20) */ -#define UV_PRIORITY_ABOVE_NORMAL -4 /* RUNTY(12) */ -#define UV_PRIORITY_HIGH -7 /* RUNPTY(6) */ -#define UV_PRIORITY_HIGHEST -10 /* RUNPTY(1) */ -#else -#define UV_PRIORITY_LOW 19 -#define UV_PRIORITY_BELOW_NORMAL 10 -#define UV_PRIORITY_NORMAL 0 -#define UV_PRIORITY_ABOVE_NORMAL -7 -#define UV_PRIORITY_HIGH -14 -#define UV_PRIORITY_HIGHEST -20 -#endif - -UV_EXTERN int uv_os_getpriority(uv_pid_t pid, int* priority); -UV_EXTERN int uv_os_setpriority(uv_pid_t pid, int priority); - -enum { - UV_THREAD_PRIORITY_HIGHEST = 2, - UV_THREAD_PRIORITY_ABOVE_NORMAL = 1, - UV_THREAD_PRIORITY_NORMAL = 0, - UV_THREAD_PRIORITY_BELOW_NORMAL = -1, - UV_THREAD_PRIORITY_LOWEST = -2, -}; - -UV_EXTERN int uv_thread_getpriority(uv_thread_t tid, int* priority); -UV_EXTERN int uv_thread_setpriority(uv_thread_t tid, int priority); - -UV_EXTERN unsigned int uv_available_parallelism(void); -UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count); -UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count); -UV_EXTERN int uv_cpumask_size(void); - -UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses, - int* count); -UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses, - int count); - -struct uv_env_item_s { - char* name; - char* value; -}; - -UV_EXTERN int uv_os_environ(uv_env_item_t** envitems, int* count); -UV_EXTERN void uv_os_free_environ(uv_env_item_t* envitems, int count); -UV_EXTERN int uv_os_getenv(const char* name, char* buffer, size_t* size); -UV_EXTERN int uv_os_setenv(const char* name, const char* value); -UV_EXTERN int uv_os_unsetenv(const char* name); - -#ifdef MAXHOSTNAMELEN -#define UV_MAXHOSTNAMESIZE (MAXHOSTNAMELEN + 1) -#else -/* - Fallback for the maximum hostname size, including the null terminator. The - Windows gethostname() documentation states that 256 bytes will always be - large enough to hold the null-terminated hostname. -*/ -#define UV_MAXHOSTNAMESIZE 256 -#endif - -UV_EXTERN int uv_os_gethostname(char* buffer, size_t* size); - -UV_EXTERN int uv_os_uname(uv_utsname_t* buffer); - -struct uv_metrics_s { - uint64_t loop_count; - uint64_t events; - uint64_t events_waiting; - /* private */ - uint64_t* reserved[13]; -}; - -UV_EXTERN int uv_metrics_info(uv_loop_t* loop, uv_metrics_t* metrics); -UV_EXTERN uint64_t uv_metrics_idle_time(uv_loop_t* loop); - -typedef enum { - UV_FS_UNKNOWN = -1, - UV_FS_CUSTOM, - UV_FS_OPEN, - UV_FS_CLOSE, - UV_FS_READ, - UV_FS_WRITE, - UV_FS_SENDFILE, - UV_FS_STAT, - UV_FS_LSTAT, - UV_FS_FSTAT, - UV_FS_FTRUNCATE, - UV_FS_UTIME, - UV_FS_FUTIME, - UV_FS_ACCESS, - UV_FS_CHMOD, - UV_FS_FCHMOD, - UV_FS_FSYNC, - UV_FS_FDATASYNC, - UV_FS_UNLINK, - UV_FS_RMDIR, - UV_FS_MKDIR, - UV_FS_MKDTEMP, - UV_FS_RENAME, - UV_FS_SCANDIR, - UV_FS_LINK, - UV_FS_SYMLINK, - UV_FS_READLINK, - UV_FS_CHOWN, - UV_FS_FCHOWN, - UV_FS_REALPATH, - UV_FS_COPYFILE, - UV_FS_LCHOWN, - UV_FS_OPENDIR, - UV_FS_READDIR, - UV_FS_CLOSEDIR, - UV_FS_STATFS, - UV_FS_MKSTEMP, - UV_FS_LUTIME -} uv_fs_type; - -struct uv_dir_s { - uv_dirent_t* dirents; - size_t nentries; - void* reserved[4]; - UV_DIR_PRIVATE_FIELDS -}; - -/* uv_fs_t is a subclass of uv_req_t. */ -struct uv_fs_s { - UV_REQ_FIELDS - uv_fs_type fs_type; - uv_loop_t* loop; - uv_fs_cb cb; - ssize_t result; - void* ptr; - const char* path; - uv_stat_t statbuf; /* Stores the result of uv_fs_stat() and uv_fs_fstat(). */ - UV_FS_PRIVATE_FIELDS -}; - -UV_EXTERN uv_fs_type uv_fs_get_type(const uv_fs_t*); -UV_EXTERN ssize_t uv_fs_get_result(const uv_fs_t*); -UV_EXTERN int uv_fs_get_system_error(const uv_fs_t*); -UV_EXTERN void* uv_fs_get_ptr(const uv_fs_t*); -UV_EXTERN const char* uv_fs_get_path(const uv_fs_t*); -UV_EXTERN uv_stat_t* uv_fs_get_statbuf(uv_fs_t*); - -UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req); -UV_EXTERN int uv_fs_close(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb); -UV_EXTERN int uv_fs_open(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int flags, - int mode, - uv_fs_cb cb); -UV_EXTERN int uv_fs_read(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - const uv_buf_t bufs[], - unsigned int nbufs, - int64_t offset, - uv_fs_cb cb); -UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_write(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - const uv_buf_t bufs[], - unsigned int nbufs, - int64_t offset, - uv_fs_cb cb); -/* - * This flag can be used with uv_fs_copyfile() to return an error if the - * destination already exists. - */ -#define UV_FS_COPYFILE_EXCL 0x0001 - -/* - * This flag can be used with uv_fs_copyfile() to attempt to create a reflink. - * If copy-on-write is not supported, a fallback copy mechanism is used. - */ -#define UV_FS_COPYFILE_FICLONE 0x0002 - -/* - * This flag can be used with uv_fs_copyfile() to attempt to create a reflink. - * If copy-on-write is not supported, an error is returned. - */ -#define UV_FS_COPYFILE_FICLONE_FORCE 0x0004 - -UV_EXTERN int uv_fs_copyfile(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - int flags, - uv_fs_cb cb); -UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int mode, - uv_fs_cb cb); -UV_EXTERN int uv_fs_mkdtemp(uv_loop_t* loop, - uv_fs_t* req, - const char* tpl, - uv_fs_cb cb); -UV_EXTERN int uv_fs_mkstemp(uv_loop_t* loop, - uv_fs_t* req, - const char* tpl, - uv_fs_cb cb); -UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_scandir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int flags, - uv_fs_cb cb); -UV_EXTERN int uv_fs_scandir_next(uv_fs_t* req, - uv_dirent_t* ent); -UV_EXTERN int uv_fs_opendir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, - uv_fs_t* req, - uv_dir_t* dir, - uv_fs_cb cb); -UV_EXTERN int uv_fs_closedir(uv_loop_t* loop, - uv_fs_t* req, - uv_dir_t* dir, - uv_fs_cb cb); -UV_EXTERN int uv_fs_stat(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb); -UV_EXTERN int uv_fs_rename(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb); -UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb); -UV_EXTERN int uv_fs_ftruncate(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - int64_t offset, - uv_fs_cb cb); -UV_EXTERN int uv_fs_sendfile(uv_loop_t* loop, - uv_fs_t* req, - uv_file out_fd, - uv_file in_fd, - int64_t in_offset, - size_t length, - uv_fs_cb cb); -UV_EXTERN int uv_fs_access(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int mode, - uv_fs_cb cb); -UV_EXTERN int uv_fs_chmod(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int mode, - uv_fs_cb cb); -#define UV_FS_UTIME_NOW (INFINITY) -#define UV_FS_UTIME_OMIT (NAN) -UV_EXTERN int uv_fs_utime(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - double atime, - double mtime, - uv_fs_cb cb); -UV_EXTERN int uv_fs_futime(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - double atime, - double mtime, - uv_fs_cb cb); -UV_EXTERN int uv_fs_lutime(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - double atime, - double mtime, - uv_fs_cb cb); -UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_link(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - uv_fs_cb cb); - -/* - * This flag can be used with uv_fs_symlink() on Windows to specify whether - * path argument points to a directory. - */ -#define UV_FS_SYMLINK_DIR 0x0001 - -/* - * This flag can be used with uv_fs_symlink() on Windows to specify whether - * the symlink is to be created using junction points. - */ -#define UV_FS_SYMLINK_JUNCTION 0x0002 - -UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - int flags, - uv_fs_cb cb); -UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_realpath(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); -UV_EXTERN int uv_fs_fchmod(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - int mode, - uv_fs_cb cb); -UV_EXTERN int uv_fs_chown(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_uid_t uid, - uv_gid_t gid, - uv_fs_cb cb); -UV_EXTERN int uv_fs_fchown(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_uid_t uid, - uv_gid_t gid, - uv_fs_cb cb); -UV_EXTERN int uv_fs_lchown(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_uid_t uid, - uv_gid_t gid, - uv_fs_cb cb); -UV_EXTERN int uv_fs_statfs(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb); - -enum uv_fs_event { - UV_RENAME = 1, - UV_CHANGE = 2 -}; - -struct uv_fs_event_s { - UV_HANDLE_FIELDS - /* private */ - char* path; - UV_FS_EVENT_PRIVATE_FIELDS -}; - -/* - * uv_fs_stat() based polling file watcher. - */ -struct uv_fs_poll_s { - UV_HANDLE_FIELDS - /* Private, don't touch. */ - void* poll_ctx; -}; - -UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle); -UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle, - uv_fs_poll_cb poll_cb, - const char* path, - unsigned int interval); -UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle); -UV_EXTERN int uv_fs_poll_getpath(uv_fs_poll_t* handle, - char* buffer, - size_t* size); - -struct uv_signal_s { - UV_HANDLE_FIELDS - uv_signal_cb signal_cb; - int signum; - UV_SIGNAL_PRIVATE_FIELDS -}; - -UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle); -UV_EXTERN int uv_signal_start(uv_signal_t* handle, - uv_signal_cb signal_cb, - int signum); -UV_EXTERN int uv_signal_start_oneshot(uv_signal_t* handle, - uv_signal_cb signal_cb, - int signum); -UV_EXTERN int uv_signal_stop(uv_signal_t* handle); - -UV_EXTERN void uv_loadavg(double avg[3]); - -/* - * Flags to be passed to uv_fs_event_start(). - */ -enum uv_fs_event_flags { - /* - * By default, if the fs event watcher is given a directory name, we will - * watch for all events in that directory. This flags overrides this behavior - * and makes fs_event report only changes to the directory entry itself. This - * flag does not affect individual files watched. - * This flag is currently not implemented yet on any backend. - */ - UV_FS_EVENT_WATCH_ENTRY = 1, - - /* - * By default uv_fs_event will try to use a kernel interface such as inotify - * or kqueue to detect events. This may not work on remote filesystems such - * as NFS mounts. This flag makes fs_event fall back to calling stat() on a - * regular interval. - * This flag is currently not implemented yet on any backend. - */ - UV_FS_EVENT_STAT = 2, - - /* - * By default, event watcher, when watching directory, is not registering - * (is ignoring) changes in it's subdirectories. - * This flag will override this behaviour on platforms that support it. - */ - UV_FS_EVENT_RECURSIVE = 4 -}; - -UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle); -UV_EXTERN int uv_fs_event_start(uv_fs_event_t* handle, - uv_fs_event_cb cb, - const char* path, - unsigned int flags); -UV_EXTERN int uv_fs_event_stop(uv_fs_event_t* handle); -UV_EXTERN int uv_fs_event_getpath(uv_fs_event_t* handle, - char* buffer, - size_t* size); - -UV_EXTERN int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr); -UV_EXTERN int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr); - -UV_EXTERN int uv_ip4_name(const struct sockaddr_in* src, char* dst, size_t size); -UV_EXTERN int uv_ip6_name(const struct sockaddr_in6* src, char* dst, size_t size); -UV_EXTERN int uv_ip_name(const struct sockaddr* src, char* dst, size_t size); - -UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size); -UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst); - -struct uv_random_s { - UV_REQ_FIELDS - /* read-only */ - uv_loop_t* loop; - /* private */ - int status; - void* buf; - size_t buflen; - uv_random_cb cb; - struct uv__work work_req; -}; - -UV_EXTERN int uv_random(uv_loop_t* loop, - uv_random_t* req, - void* buf, - size_t buflen, - unsigned flags, /* For future extension; must be 0. */ - uv_random_cb cb); - -#if defined(IF_NAMESIZE) -#define UV_IF_NAMESIZE (IF_NAMESIZE + 1) -#elif defined(IFNAMSIZ) -#define UV_IF_NAMESIZE (IFNAMSIZ + 1) -#else -#define UV_IF_NAMESIZE (16 + 1) -#endif - -UV_EXTERN int uv_if_indextoname(unsigned int ifindex, - char* buffer, - size_t* size); -UV_EXTERN int uv_if_indextoiid(unsigned int ifindex, - char* buffer, - size_t* size); - -UV_EXTERN int uv_exepath(char* buffer, size_t* size); - -UV_EXTERN int uv_cwd(char* buffer, size_t* size); - -UV_EXTERN int uv_chdir(const char* dir); - -UV_EXTERN uint64_t uv_get_free_memory(void); -UV_EXTERN uint64_t uv_get_total_memory(void); -UV_EXTERN uint64_t uv_get_constrained_memory(void); -UV_EXTERN uint64_t uv_get_available_memory(void); - -UV_EXTERN int uv_clock_gettime(uv_clock_id clock_id, uv_timespec64_t* ts); -UV_EXTERN uint64_t uv_hrtime(void); -UV_EXTERN void uv_sleep(unsigned int msec); - -UV_EXTERN void uv_disable_stdio_inheritance(void); - -UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib); -UV_EXTERN void uv_dlclose(uv_lib_t* lib); -UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr); -UV_EXTERN const char* uv_dlerror(const uv_lib_t* lib); - -UV_EXTERN int uv_mutex_init(uv_mutex_t* handle); -UV_EXTERN int uv_mutex_init_recursive(uv_mutex_t* handle); -UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle); -UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle); -UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle); -UV_EXTERN void uv_mutex_unlock(uv_mutex_t* handle); - -UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock); -UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock); -UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock); -UV_EXTERN int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock); -UV_EXTERN void uv_rwlock_rdunlock(uv_rwlock_t* rwlock); -UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock); -UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock); -UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock); - -UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value); -UV_EXTERN void uv_sem_destroy(uv_sem_t* sem); -UV_EXTERN void uv_sem_post(uv_sem_t* sem); -UV_EXTERN void uv_sem_wait(uv_sem_t* sem); -UV_EXTERN int uv_sem_trywait(uv_sem_t* sem); - -UV_EXTERN int uv_cond_init(uv_cond_t* cond); -UV_EXTERN void uv_cond_destroy(uv_cond_t* cond); -UV_EXTERN void uv_cond_signal(uv_cond_t* cond); -UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond); - -UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count); -UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier); -UV_EXTERN int uv_barrier_wait(uv_barrier_t* barrier); - -UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex); -UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, - uv_mutex_t* mutex, - uint64_t timeout); - -UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void)); - -UV_EXTERN int uv_key_create(uv_key_t* key); -UV_EXTERN void uv_key_delete(uv_key_t* key); -UV_EXTERN void* uv_key_get(uv_key_t* key); -UV_EXTERN void uv_key_set(uv_key_t* key, void* value); - -UV_EXTERN int uv_gettimeofday(uv_timeval64_t* tv); - -typedef void (*uv_thread_cb)(void* arg); - -UV_EXTERN int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg); -UV_EXTERN int uv_thread_detach(uv_thread_t* tid); - -typedef enum { - UV_THREAD_NO_FLAGS = 0x00, - UV_THREAD_HAS_STACK_SIZE = 0x01 -} uv_thread_create_flags; - -struct uv_thread_options_s { - unsigned int flags; - size_t stack_size; - /* More fields may be added at any time. */ -}; - -typedef struct uv_thread_options_s uv_thread_options_t; - -UV_EXTERN int uv_thread_create_ex(uv_thread_t* tid, - const uv_thread_options_t* params, - uv_thread_cb entry, - void* arg); -UV_EXTERN int uv_thread_setaffinity(uv_thread_t* tid, - char* cpumask, - char* oldmask, - size_t mask_size); -UV_EXTERN int uv_thread_getaffinity(uv_thread_t* tid, - char* cpumask, - size_t mask_size); -UV_EXTERN int uv_thread_getcpu(void); -UV_EXTERN uv_thread_t uv_thread_self(void); -UV_EXTERN int uv_thread_join(uv_thread_t* tid); -UV_EXTERN int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2); -UV_EXTERN int uv_thread_setname(const char* name); -UV_EXTERN int uv_thread_getname(uv_thread_t* tid, char* name, size_t size); - -/* The presence of these unions force similar struct layout. */ -#define XX(_, name) uv_##name##_t name; -union uv_any_handle { - UV_HANDLE_TYPE_MAP(XX) -}; - -union uv_any_req { - UV_REQ_TYPE_MAP(XX) -}; -#undef XX - -struct uv_loop_s { - /* User data - use this for whatever. */ - void* data; - /* Loop reference counting. */ - unsigned int active_handles; - struct uv__queue handle_queue; - union { - void* unused; - unsigned int count; - } active_reqs; - /* Internal storage for future extensions. */ - void* internal_fields; - /* Internal flag to signal loop stop. */ - unsigned int stop_flag; - UV_LOOP_PRIVATE_FIELDS -}; - -UV_EXTERN void* uv_loop_get_data(const uv_loop_t*); -UV_EXTERN void uv_loop_set_data(uv_loop_t*, void* data); - -/* Unicode utilities needed for dealing with Windows. */ -UV_EXTERN size_t uv_utf16_length_as_wtf8(const uint16_t* utf16, - ssize_t utf16_len); -UV_EXTERN int uv_utf16_to_wtf8(const uint16_t* utf16, - ssize_t utf16_len, - char** wtf8_ptr, - size_t* wtf8_len_ptr); -UV_EXTERN ssize_t uv_wtf8_length_as_utf16(const char* wtf8); -UV_EXTERN void uv_wtf8_to_utf16(const char* wtf8, - uint16_t* utf16, - size_t utf16_len); - -/* Don't export the private CPP symbols. */ -#undef UV_HANDLE_TYPE_PRIVATE -#undef UV_REQ_TYPE_PRIVATE -#undef UV_REQ_PRIVATE_FIELDS -#undef UV_STREAM_PRIVATE_FIELDS -#undef UV_TCP_PRIVATE_FIELDS -#undef UV_PREPARE_PRIVATE_FIELDS -#undef UV_CHECK_PRIVATE_FIELDS -#undef UV_IDLE_PRIVATE_FIELDS -#undef UV_ASYNC_PRIVATE_FIELDS -#undef UV_TIMER_PRIVATE_FIELDS -#undef UV_GETADDRINFO_PRIVATE_FIELDS -#undef UV_GETNAMEINFO_PRIVATE_FIELDS -#undef UV_FS_REQ_PRIVATE_FIELDS -#undef UV_WORK_PRIVATE_FIELDS -#undef UV_FS_EVENT_PRIVATE_FIELDS -#undef UV_SIGNAL_PRIVATE_FIELDS -#undef UV_LOOP_PRIVATE_FIELDS -#undef UV_LOOP_PRIVATE_PLATFORM_FIELDS -#undef UV__ERR - -#ifdef __cplusplus -} -#endif -#endif /* UV_H */ diff --git a/src/jsc/bindings/libuv/uv/bsd.h b/src/jsc/bindings/libuv/uv/bsd.h deleted file mode 100644 index fd4585c1d848..000000000000 --- a/src/jsc/bindings/libuv/uv/bsd.h +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef UV_BSD_H -#define UV_BSD_H - -#define UV_PLATFORM_FS_EVENT_FIELDS \ - uv__io_t event_watcher; - -#define UV_IO_PRIVATE_PLATFORM_FIELDS \ - int rcount; \ - int wcount; - -#define UV_HAVE_KQUEUE 1 - -#endif /* UV_BSD_H */ diff --git a/src/jsc/bindings/libuv/uv/darwin.h b/src/jsc/bindings/libuv/uv/darwin.h deleted file mode 100644 index 471609b32336..000000000000 --- a/src/jsc/bindings/libuv/uv/darwin.h +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef UV_DARWIN_H -#define UV_DARWIN_H - -#if defined(__APPLE__) && defined(__MACH__) -#include -#include -#include -#include -#define UV_PLATFORM_SEM_T semaphore_t -#endif - -#define UV_IO_PRIVATE_PLATFORM_FIELDS \ - int rcount; \ - int wcount; - -#define UV_PLATFORM_LOOP_FIELDS \ - uv_thread_t cf_thread; \ - void* _cf_reserved; \ - void* cf_state; \ - uv_mutex_t cf_mutex; \ - uv_sem_t cf_sem; \ - struct uv__queue cf_signals; - -#define UV_PLATFORM_FS_EVENT_FIELDS \ - uv__io_t event_watcher; \ - char* realpath; \ - int realpath_len; \ - int cf_flags; \ - uv_async_t* cf_cb; \ - struct uv__queue cf_events; \ - struct uv__queue cf_member; \ - int cf_error; \ - uv_mutex_t cf_mutex; - -#define UV_STREAM_PRIVATE_PLATFORM_FIELDS \ - void* select; - -#define UV_HAVE_KQUEUE 1 - -#endif /* UV_DARWIN_H */ diff --git a/src/jsc/bindings/libuv/uv/linux.h b/src/jsc/bindings/libuv/uv/linux.h deleted file mode 100644 index 3a3a111a96d0..000000000000 --- a/src/jsc/bindings/libuv/uv/linux.h +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef UV_LINUX_H -#define UV_LINUX_H - -#define UV_PLATFORM_LOOP_FIELDS \ - uv__io_t inotify_read_watcher; \ - void* inotify_watchers; \ - int inotify_fd; - -#define UV_PLATFORM_FS_EVENT_FIELDS \ - struct uv__queue watchers; \ - int wd; - -#endif /* UV_LINUX_H */ diff --git a/src/jsc/bindings/libuv/uv/threadpool.h b/src/jsc/bindings/libuv/uv/threadpool.h deleted file mode 100644 index 9cf8754b5234..000000000000 --- a/src/jsc/bindings/libuv/uv/threadpool.h +++ /dev/null @@ -1,37 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -/* - * This file is private to libuv. It provides common functionality to both - * Windows and Unix backends. - */ - -#ifndef UV_THREADPOOL_H_ -#define UV_THREADPOOL_H_ - -struct uv__work { - void (*work)(struct uv__work* w); - void (*done)(struct uv__work* w, int status); - struct uv_loop_s* loop; - struct uv__queue wq; -}; - -#endif /* UV_THREADPOOL_H_ */ diff --git a/src/jsc/bindings/libuv/uv/unix.h b/src/jsc/bindings/libuv/uv/unix.h deleted file mode 100644 index 4c6de8c35a5e..000000000000 --- a/src/jsc/bindings/libuv/uv/unix.h +++ /dev/null @@ -1,493 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef UV_UNIX_H -#define UV_UNIX_H - -#include -#include -#include -#include - -#include -#include -#include -#include -#include /* MAXHOSTNAMELEN on Solaris */ - -#include -#include - -#if !defined(__MVS__) -#include -#include /* MAXHOSTNAMELEN on Linux and the BSDs */ -#endif -#include -#include - -#include "uv/threadpool.h" - -#if defined(__linux__) -#include "uv/linux.h" -#elif defined(__APPLE__) -#include "uv/darwin.h" -#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) -#include "uv/bsd.h" -#endif - -#ifndef NI_MAXHOST -#define NI_MAXHOST 1025 -#endif - -#ifndef NI_MAXSERV -#define NI_MAXSERV 32 -#endif - -#ifndef UV_IO_PRIVATE_PLATFORM_FIELDS -#define UV_IO_PRIVATE_PLATFORM_FIELDS /* empty */ -#endif - -struct uv__io_s; -struct uv_loop_s; - -typedef void (*uv__io_cb)(struct uv_loop_s* loop, - struct uv__io_s* w, - unsigned int events); -typedef struct uv__io_s uv__io_t; - -struct uv__io_s { - uv__io_cb cb; - struct uv__queue pending_queue; - struct uv__queue watcher_queue; - unsigned int pevents; /* Pending event mask i.e. mask at next tick. */ - unsigned int events; /* Current event mask. */ - int fd; - UV_IO_PRIVATE_PLATFORM_FIELDS -}; - -#ifndef UV_PLATFORM_SEM_T -#define UV_PLATFORM_SEM_T sem_t -#endif - -#ifndef UV_PLATFORM_LOOP_FIELDS -#define UV_PLATFORM_LOOP_FIELDS /* empty */ -#endif - -#ifndef UV_PLATFORM_FS_EVENT_FIELDS -#define UV_PLATFORM_FS_EVENT_FIELDS /* empty */ -#endif - -#ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS -#define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */ -#endif - -/* Note: May be cast to struct iovec. See writev(2). */ -typedef struct uv_buf_t { - char* base; - size_t len; -} uv_buf_t; - -typedef int uv_file; -typedef int uv_os_sock_t; -typedef int uv_os_fd_t; -typedef pid_t uv_pid_t; - -#define UV_ONCE_INIT PTHREAD_ONCE_INIT - -typedef pthread_once_t uv_once_t; -typedef pthread_t uv_thread_t; -typedef pthread_mutex_t uv_mutex_t; -typedef pthread_rwlock_t uv_rwlock_t; -typedef UV_PLATFORM_SEM_T uv_sem_t; -typedef pthread_cond_t uv_cond_t; -typedef pthread_key_t uv_key_t; - -/* Note: guard clauses should match uv_barrier_init's in src/unix/thread.c. */ -#if defined(_AIX) || defined(__OpenBSD__) || !defined(PTHREAD_BARRIER_SERIAL_THREAD) -/* TODO(bnoordhuis) Merge into uv_barrier_t in v2. */ -struct _uv_barrier { - uv_mutex_t mutex; - uv_cond_t cond; - unsigned threshold; - unsigned in; - unsigned out; -}; - -typedef struct { - struct _uv_barrier* b; -#if defined(PTHREAD_BARRIER_SERIAL_THREAD) - /* TODO(bnoordhuis) Remove padding in v2. */ - char pad[sizeof(pthread_barrier_t) - sizeof(struct _uv_barrier*)]; -#endif -} uv_barrier_t; -#else -typedef pthread_barrier_t uv_barrier_t; -#endif - -/* Platform-specific definitions for uv_spawn support. */ -typedef gid_t uv_gid_t; -typedef uid_t uv_uid_t; - -typedef struct dirent uv__dirent_t; - -#define UV_DIR_PRIVATE_FIELDS \ - DIR* dir; - -#if defined(DT_UNKNOWN) -#define HAVE_DIRENT_TYPES -#if defined(DT_REG) -#define UV__DT_FILE DT_REG -#else -#define UV__DT_FILE -1 -#endif -#if defined(DT_DIR) -#define UV__DT_DIR DT_DIR -#else -#define UV__DT_DIR -2 -#endif -#if defined(DT_LNK) -#define UV__DT_LINK DT_LNK -#else -#define UV__DT_LINK -3 -#endif -#if defined(DT_FIFO) -#define UV__DT_FIFO DT_FIFO -#else -#define UV__DT_FIFO -4 -#endif -#if defined(DT_SOCK) -#define UV__DT_SOCKET DT_SOCK -#else -#define UV__DT_SOCKET -5 -#endif -#if defined(DT_CHR) -#define UV__DT_CHAR DT_CHR -#else -#define UV__DT_CHAR -6 -#endif -#if defined(DT_BLK) -#define UV__DT_BLOCK DT_BLK -#else -#define UV__DT_BLOCK -7 -#endif -#endif - -/* Platform-specific definitions for uv_dlopen support. */ -#define UV_DYNAMIC /* empty */ - -typedef struct { - void* handle; - char* errmsg; -} uv_lib_t; - -#define UV_LOOP_PRIVATE_FIELDS \ - unsigned long flags; \ - int backend_fd; \ - struct uv__queue pending_queue; \ - struct uv__queue watcher_queue; \ - uv__io_t** watchers; \ - unsigned int nwatchers; \ - unsigned int nfds; \ - struct uv__queue wq; \ - uv_mutex_t wq_mutex; \ - uv_async_t wq_async; \ - uv_rwlock_t cloexec_lock; \ - uv_handle_t* closing_handles; \ - struct uv__queue process_handles; \ - struct uv__queue prepare_handles; \ - struct uv__queue check_handles; \ - struct uv__queue idle_handles; \ - struct uv__queue async_handles; \ - void (*async_unused)(void); /* TODO(bnoordhuis) Remove in libuv v2. */ \ - uv__io_t async_io_watcher; \ - int async_wfd; \ - struct { \ - void* min; \ - unsigned int nelts; \ - } timer_heap; \ - uint64_t timer_counter; \ - uint64_t time; \ - int signal_pipefd[2]; \ - uv__io_t signal_io_watcher; \ - uv_signal_t child_watcher; \ - int emfile_fd; \ - UV_PLATFORM_LOOP_FIELDS - -#define UV_REQ_TYPE_PRIVATE /* empty */ - -#define UV_REQ_PRIVATE_FIELDS /* empty */ - -#define UV_PRIVATE_REQ_TYPES /* empty */ - -#define UV_WRITE_PRIVATE_FIELDS \ - struct uv__queue queue; \ - unsigned int write_index; \ - uv_buf_t* bufs; \ - unsigned int nbufs; \ - int error; \ - uv_buf_t bufsml[4]; - -#define UV_CONNECT_PRIVATE_FIELDS \ - struct uv__queue queue; - -#define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */ - -#define UV_UDP_SEND_PRIVATE_FIELDS \ - struct uv__queue queue; \ - union { \ - struct sockaddr addr; \ - struct sockaddr_storage storage; \ - } u; \ - unsigned int nbufs; \ - uv_buf_t* bufs; \ - ssize_t status; \ - uv_udp_send_cb send_cb; \ - uv_buf_t bufsml[4]; - -#define UV_HANDLE_PRIVATE_FIELDS \ - uv_handle_t* next_closing; \ - unsigned int flags; - -#define UV_STREAM_PRIVATE_FIELDS \ - uv_connect_t* connect_req; \ - uv_shutdown_t* shutdown_req; \ - uv__io_t io_watcher; \ - struct uv__queue write_queue; \ - struct uv__queue write_completed_queue; \ - uv_connection_cb connection_cb; \ - int delayed_error; \ - int accepted_fd; \ - void* queued_fds; \ - UV_STREAM_PRIVATE_PLATFORM_FIELDS - -#define UV_TCP_PRIVATE_FIELDS /* empty */ - -#define UV_UDP_PRIVATE_FIELDS \ - uv_alloc_cb alloc_cb; \ - uv_udp_recv_cb recv_cb; \ - uv__io_t io_watcher; \ - struct uv__queue write_queue; \ - struct uv__queue write_completed_queue; - -#define UV_PIPE_PRIVATE_FIELDS \ - const char* pipe_fname; /* NULL or strdup'ed */ - -#define UV_POLL_PRIVATE_FIELDS \ - uv__io_t io_watcher; - -#define UV_PREPARE_PRIVATE_FIELDS \ - uv_prepare_cb prepare_cb; \ - struct uv__queue queue; - -#define UV_CHECK_PRIVATE_FIELDS \ - uv_check_cb check_cb; \ - struct uv__queue queue; - -#define UV_IDLE_PRIVATE_FIELDS \ - uv_idle_cb idle_cb; \ - struct uv__queue queue; - -#define UV_ASYNC_PRIVATE_FIELDS \ - uv_async_cb async_cb; \ - struct uv__queue queue; \ - int pending; - -#define UV_TIMER_PRIVATE_FIELDS \ - uv_timer_cb timer_cb; \ - union { \ - void* heap[3]; \ - struct uv__queue queue; \ - } node; \ - uint64_t timeout; \ - uint64_t repeat; \ - uint64_t start_id; - -#define UV_GETADDRINFO_PRIVATE_FIELDS \ - struct uv__work work_req; \ - uv_getaddrinfo_cb cb; \ - struct addrinfo* hints; \ - char* hostname; \ - char* service; \ - struct addrinfo* addrinfo; \ - int retcode; - -#define UV_GETNAMEINFO_PRIVATE_FIELDS \ - struct uv__work work_req; \ - uv_getnameinfo_cb getnameinfo_cb; \ - struct sockaddr_storage storage; \ - int flags; \ - char host[NI_MAXHOST]; \ - char service[NI_MAXSERV]; \ - int retcode; - -#define UV_PROCESS_PRIVATE_FIELDS \ - struct uv__queue queue; \ - int status; - -#define UV_FS_PRIVATE_FIELDS \ - const char* new_path; \ - uv_file file; \ - int flags; \ - mode_t mode; \ - unsigned int nbufs; \ - uv_buf_t* bufs; \ - off_t off; \ - uv_uid_t uid; \ - uv_gid_t gid; \ - double atime; \ - double mtime; \ - struct uv__work work_req; \ - uv_buf_t bufsml[4]; - -#define UV_WORK_PRIVATE_FIELDS \ - struct uv__work work_req; - -#define UV_TTY_PRIVATE_FIELDS \ - struct termios orig_termios; \ - int mode; - -#define UV_SIGNAL_PRIVATE_FIELDS \ - /* RB_ENTRY(uv_signal_s) tree_entry; */ \ - struct { \ - struct uv_signal_s* rbe_left; \ - struct uv_signal_s* rbe_right; \ - struct uv_signal_s* rbe_parent; \ - int rbe_color; \ - } tree_entry; \ - /* Use two counters here so we don have to fiddle with atomics. */ \ - unsigned int caught_signals; \ - unsigned int dispatched_signals; - -#define UV_FS_EVENT_PRIVATE_FIELDS \ - uv_fs_event_cb cb; \ - UV_PLATFORM_FS_EVENT_FIELDS - -/* fs open() flags supported on this platform: */ -#if defined(O_APPEND) -#define UV_FS_O_APPEND O_APPEND -#else -#define UV_FS_O_APPEND 0 -#endif -#if defined(O_CREAT) -#define UV_FS_O_CREAT O_CREAT -#else -#define UV_FS_O_CREAT 0 -#endif - -#if defined(__linux__) && defined(__arm__) -#define UV_FS_O_DIRECT 0x10000 -#elif defined(__linux__) && defined(__m68k__) -#define UV_FS_O_DIRECT 0x10000 -#elif defined(__linux__) && defined(__mips__) -#define UV_FS_O_DIRECT 0x08000 -#elif defined(__linux__) && defined(__powerpc__) -#define UV_FS_O_DIRECT 0x20000 -#elif defined(__linux__) && defined(__s390x__) -#define UV_FS_O_DIRECT 0x04000 -#elif defined(__linux__) && defined(__x86_64__) -#define UV_FS_O_DIRECT 0x04000 -#elif defined(__linux__) && defined(__loongarch__) -#define UV_FS_O_DIRECT 0x04000 -#elif defined(O_DIRECT) -#define UV_FS_O_DIRECT O_DIRECT -#else -#define UV_FS_O_DIRECT 0 -#endif - -#if defined(O_DIRECTORY) -#define UV_FS_O_DIRECTORY O_DIRECTORY -#else -#define UV_FS_O_DIRECTORY 0 -#endif -#if defined(O_DSYNC) -#define UV_FS_O_DSYNC O_DSYNC -#else -#define UV_FS_O_DSYNC 0 -#endif -#if defined(O_EXCL) -#define UV_FS_O_EXCL O_EXCL -#else -#define UV_FS_O_EXCL 0 -#endif -#if defined(O_EXLOCK) -#define UV_FS_O_EXLOCK O_EXLOCK -#else -#define UV_FS_O_EXLOCK 0 -#endif -#if defined(O_NOATIME) -#define UV_FS_O_NOATIME O_NOATIME -#else -#define UV_FS_O_NOATIME 0 -#endif -#if defined(O_NOCTTY) -#define UV_FS_O_NOCTTY O_NOCTTY -#else -#define UV_FS_O_NOCTTY 0 -#endif -#if defined(O_NOFOLLOW) -#define UV_FS_O_NOFOLLOW O_NOFOLLOW -#else -#define UV_FS_O_NOFOLLOW 0 -#endif -#if defined(O_NONBLOCK) -#define UV_FS_O_NONBLOCK O_NONBLOCK -#else -#define UV_FS_O_NONBLOCK 0 -#endif -#if defined(O_RDONLY) -#define UV_FS_O_RDONLY O_RDONLY -#else -#define UV_FS_O_RDONLY 0 -#endif -#if defined(O_RDWR) -#define UV_FS_O_RDWR O_RDWR -#else -#define UV_FS_O_RDWR 0 -#endif -#if defined(O_SYMLINK) -#define UV_FS_O_SYMLINK O_SYMLINK -#else -#define UV_FS_O_SYMLINK 0 -#endif -#if defined(O_SYNC) -#define UV_FS_O_SYNC O_SYNC -#else -#define UV_FS_O_SYNC 0 -#endif -#if defined(O_TRUNC) -#define UV_FS_O_TRUNC O_TRUNC -#else -#define UV_FS_O_TRUNC 0 -#endif -#if defined(O_WRONLY) -#define UV_FS_O_WRONLY O_WRONLY -#else -#define UV_FS_O_WRONLY 0 -#endif - -/* fs open() flags supported on other platforms: */ -#define UV_FS_O_FILEMAP 0 -#define UV_FS_O_RANDOM 0 -#define UV_FS_O_SHORT_LIVED 0 -#define UV_FS_O_SEQUENTIAL 0 -#define UV_FS_O_TEMPORARY 0 - -#endif /* UV_UNIX_H */ diff --git a/src/jsc/bindings/libuv/uv/version.h b/src/jsc/bindings/libuv/uv/version.h deleted file mode 100644 index 2db20807de1c..000000000000 --- a/src/jsc/bindings/libuv/uv/version.h +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef UV_VERSION_H -#define UV_VERSION_H - -/* - * Versions with the same major number are ABI stable. API is allowed to - * evolve between minor releases, but only in a backwards compatible way. - * Make sure you update the -soname directives in configure.ac - * whenever you bump UV_VERSION_MAJOR or UV_VERSION_MINOR (but - * not UV_VERSION_PATCH.) - */ - -#define UV_VERSION_MAJOR 1 -#define UV_VERSION_MINOR 51 -#define UV_VERSION_PATCH 0 -#define UV_VERSION_IS_RELEASE 1 -#define UV_VERSION_SUFFIX "" - -#define UV_VERSION_HEX ((UV_VERSION_MAJOR << 16) | (UV_VERSION_MINOR << 8) | (UV_VERSION_PATCH)) - -#endif /* UV_VERSION_H */ diff --git a/src/jsc/bindings/node/http/JSConnectionsList.h b/src/jsc/bindings/node/http/JSConnectionsList.h index 57fd425a3f73..3d10cf2b678d 100644 --- a/src/jsc/bindings/node/http/JSConnectionsList.h +++ b/src/jsc/bindings/node/http/JSConnectionsList.h @@ -5,6 +5,10 @@ #include #include "BunClientData.h" +// Monotonic nanoseconds (uv-polyfills.c): the clock of HTTPParser::m_lastMessageStart and of the +// timeouts that ConnectionsList.prototype.expired compares it with. +extern "C" uint64_t uv_hrtime(void); + namespace Bun { class JSConnectionsList final : public JSC::JSNonFinalObject { diff --git a/src/jsc/bindings/node/http/JSConnectionsListPrototype.cpp b/src/jsc/bindings/node/http/JSConnectionsListPrototype.cpp index 50d6f6e71859..5fccb1cd2032 100644 --- a/src/jsc/bindings/node/http/JSConnectionsListPrototype.cpp +++ b/src/jsc/bindings/node/http/JSConnectionsListPrototype.cpp @@ -2,7 +2,6 @@ #include "JSConnectionsList.h" #include "ErrorCode.h" #include "JSDOMExceptionHandling.h" -#include "uv.h" namespace Bun { diff --git a/src/jsc/bindings/node/http/NodeHTTPParser.cpp b/src/jsc/bindings/node/http/NodeHTTPParser.cpp index 55b4b4b90da2..566c3112f42b 100644 --- a/src/jsc/bindings/node/http/NodeHTTPParser.cpp +++ b/src/jsc/bindings/node/http/NodeHTTPParser.cpp @@ -5,7 +5,6 @@ #include "JSConnectionsList.h" #include "JSHTTPParser.h" #include "ZigGlobalObject.h" -#include "uv.h" namespace Bun { diff --git a/src/jsc/bindings/root.h b/src/jsc/bindings/root.h index 3c62835febcd..e595316ca7de 100644 --- a/src/jsc/bindings/root.h +++ b/src/jsc/bindings/root.h @@ -70,6 +70,14 @@ #include +// The UCRT has no ssize_t. Same guards as the other headers that supply one. +#if OS(WINDOWS) && !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED) +#include +typedef intptr_t ssize_t; +#define _SSIZE_T_ +#define _SSIZE_T_DEFINED +#endif + #ifdef __cplusplus #if OS(LINUX) #include diff --git a/src/jsc/bindings/uv-posix-polyfills-darwin.c b/src/jsc/bindings/uv-polyfills-darwin.c similarity index 84% rename from src/jsc/bindings/uv-posix-polyfills-darwin.c rename to src/jsc/bindings/uv-polyfills-darwin.c index 7497ae74bdf9..37cd3b82e5c6 100644 --- a/src/jsc/bindings/uv-posix-polyfills-darwin.c +++ b/src/jsc/bindings/uv-polyfills-darwin.c @@ -1,5 +1,5 @@ -#include "uv-posix-polyfills.h" +#include "uv-polyfills.h" #include #include #include @@ -13,7 +13,7 @@ static void uv__hrtime_init_once(void) abort(); } -uint64_t uv__hrtime(uv_clocktype_t type) +uint64_t uv__hrtime(void) { uv_once(&once, uv__hrtime_init_once); return mach_continuous_time() * timebase.numer / timebase.denom; diff --git a/src/jsc/bindings/uv-polyfills-linux.c b/src/jsc/bindings/uv-polyfills-linux.c new file mode 100644 index 000000000000..4247caf2dd3e --- /dev/null +++ b/src/jsc/bindings/uv-polyfills-linux.c @@ -0,0 +1,13 @@ + +#include "uv-polyfills.h" +#include + +uint64_t uv__hrtime(void) +{ + struct timespec t; + + if (clock_gettime(CLOCK_MONOTONIC, &t)) + return 0; /* Not really possible. */ + + return t.tv_sec * (uint64_t)1e9 + t.tv_nsec; +} diff --git a/src/jsc/bindings/uv-posix-polyfills-posix.c b/src/jsc/bindings/uv-polyfills-posix.c similarity index 73% rename from src/jsc/bindings/uv-posix-polyfills-posix.c rename to src/jsc/bindings/uv-polyfills-posix.c index afaaf396df04..6b3bae4e53b4 100644 --- a/src/jsc/bindings/uv-posix-polyfills-posix.c +++ b/src/jsc/bindings/uv-polyfills-posix.c @@ -1,10 +1,10 @@ -#include "uv-posix-polyfills.h" +#include "uv-polyfills.h" #include #include #include -uint64_t uv__hrtime(uv_clocktype_t type) +uint64_t uv__hrtime(void) { struct timespec t; diff --git a/src/jsc/bindings/uv-polyfills-windows.c b/src/jsc/bindings/uv-polyfills-windows.c new file mode 100644 index 000000000000..09a753491d6f --- /dev/null +++ b/src/jsc/bindings/uv-polyfills-windows.c @@ -0,0 +1,73 @@ +// The uv_mutex_* functions are libuv's (src/win/thread.c). +#include "uv-polyfills.h" + +UV_EXTERN uv_pid_t uv_os_getpid(void) +{ + return GetCurrentProcessId(); +} + +int32_t Bun__getParentProcessId(void); + +UV_EXTERN uv_pid_t uv_os_getppid(void) +{ + return Bun__getParentProcessId(); +} + +static BOOL WINAPI uv__once_inner(INIT_ONCE* once, void* param, void** context) +{ + ((void (*)(void))param)(); + return TRUE; +} + +UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void)) +{ + InitOnceExecuteOnce(&guard->init_once, uv__once_inner, (void*)callback, NULL); +} + +UV_EXTERN uint64_t uv_hrtime(void) +{ + LARGE_INTEGER frequency; + LARGE_INTEGER counter; + + // Reads a word every process has mapped: nothing to gain from keeping it. + QueryPerformanceFrequency(&frequency); + QueryPerformanceCounter(&counter); + + // Split so the multiplication cannot overflow. + uint64_t seconds = (uint64_t)counter.QuadPart / (uint64_t)frequency.QuadPart; + uint64_t remainder = (uint64_t)counter.QuadPart % (uint64_t)frequency.QuadPart; + return seconds * 1000000000ull + remainder * 1000000000ull / (uint64_t)frequency.QuadPart; +} + +UV_EXTERN void uv_mutex_destroy(uv_mutex_t* mutex) +{ + DeleteCriticalSection(mutex); +} + +UV_EXTERN int uv_mutex_init(uv_mutex_t* mutex) +{ + InitializeCriticalSection(mutex); + return 0; +} + +UV_EXTERN int uv_mutex_init_recursive(uv_mutex_t* mutex) +{ + return uv_mutex_init(mutex); +} + +UV_EXTERN void uv_mutex_lock(uv_mutex_t* mutex) +{ + EnterCriticalSection(mutex); +} + +UV_EXTERN int uv_mutex_trylock(uv_mutex_t* mutex) +{ + if (TryEnterCriticalSection(mutex)) + return 0; + return UV__EBUSY; +} + +UV_EXTERN void uv_mutex_unlock(uv_mutex_t* mutex) +{ + LeaveCriticalSection(mutex); +} diff --git a/src/jsc/bindings/uv-posix-polyfills.c b/src/jsc/bindings/uv-polyfills.c similarity index 79% rename from src/jsc/bindings/uv-posix-polyfills.c rename to src/jsc/bindings/uv-polyfills.c index 19ae7e5040c8..2766bcb767ab 100644 --- a/src/jsc/bindings/uv-posix-polyfills.c +++ b/src/jsc/bindings/uv-polyfills.c @@ -1,44 +1,38 @@ -#include "uv-posix-polyfills.h" - -#if OS(LINUX) || OS(DARWIN) || OS(FREEBSD) - -#include -#include -#include - -// libuv does the annoying thing of #undef'ing these -#include -#if EDOM > 0 -#define UV__ERR(x) (-(x)) -#else -#define UV__ERR(x) (x) -#endif +#include "uv-polyfills.h" void __bun_throw_not_implemented(const char* symbol_name) { CrashHandler__unsupportedUVFunction(symbol_name); } +#if OS(WINDOWS) +#include "uv-polyfills-windows.c" +#else + +#include +#include +#include + // Internals -uint64_t uv__hrtime(uv_clocktype_t type); +uint64_t uv__hrtime(void); #if defined(__linux__) -#include "uv-posix-polyfills-linux.c" +#include "uv-polyfills-linux.c" #elif defined(__APPLE__) -#include "uv-posix-polyfills-darwin.c" +#include "uv-polyfills-darwin.c" #elif defined(__FreeBSD__) -#include "uv-posix-polyfills-posix.c" +#include "uv-polyfills-posix.c" #elif defined(__CYGWIN__) || defined(__MSYS__) || defined(__HAIKU__) || defined(__QNX__) || defined(__GNU__) -#include "uv-posix-polyfills-posix.c" +#include "uv-polyfills-posix.c" #endif -uv_pid_t uv_os_getpid() +UV_EXTERN uv_pid_t uv_os_getpid(void) { return getpid(); } -uv_pid_t uv_os_getppid() +UV_EXTERN uv_pid_t uv_os_getppid(void) { return getppid(); } @@ -51,7 +45,7 @@ UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void)) UV_EXTERN uint64_t uv_hrtime(void) { - return uv__hrtime(UV_CLOCK_PRECISE); + return uv__hrtime(); } // Copy-pasted from libuv @@ -117,7 +111,7 @@ UV_EXTERN int uv_mutex_trylock(uv_mutex_t* mutex) if (err) { if (err != EBUSY && err != EAGAIN) abort(); - return UV_EBUSY; + return UV__EBUSY; } return 0; diff --git a/src/jsc/bindings/uv-polyfills.h b/src/jsc/bindings/uv-polyfills.h new file mode 100644 index 000000000000..7bed0ae4ce3f --- /dev/null +++ b/src/jsc/bindings/uv-polyfills.h @@ -0,0 +1,41 @@ +#pragma once + +#include "root.h" +#include +#include + +// These functions are called by the stubs to crash with a nice error message +// when accessing a libuv function which we do not support +void CrashHandler__unsupportedUVFunction(const char* function_name); +void __bun_throw_not_implemented(const char* symbol_name); + +#if OS(WINDOWS) +// Exported through src/symbols.def. +#define UV_EXTERN +#else +#define UV_EXTERN __attribute__((visibility("default"))) __attribute__((used)) +#endif + +#include + +// The types of the functions Bun implements, as uv/win.h and uv/unix.h define +// them. An addon is compiled against Node's copy of those. +#if OS(WINDOWS) +#include +typedef int uv_pid_t; +typedef CRITICAL_SECTION uv_mutex_t; +typedef struct uv_once_s { + unsigned char unused; + INIT_ONCE init_once; +} uv_once_t; +#else +#include +#include +typedef pid_t uv_pid_t; +typedef pthread_mutex_t uv_mutex_t; +typedef pthread_once_t uv_once_t; +#define UV_ONCE_INIT PTHREAD_ONCE_INIT +#endif + +// uv-polyfills-darwin.c calls it. +UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void)); diff --git a/src/jsc/bindings/uv-posix-polyfills-linux.c b/src/jsc/bindings/uv-posix-polyfills-linux.c deleted file mode 100644 index 690c4a8439be..000000000000 --- a/src/jsc/bindings/uv-posix-polyfills-linux.c +++ /dev/null @@ -1,42 +0,0 @@ - -#include "uv-posix-polyfills.h" -#include -#include -#include - -uint64_t uv__hrtime(uv_clocktype_t type) -{ - static _Atomic clock_t fast_clock_id = -1; - struct timespec t; - clock_t clock_id; - - /* Prefer CLOCK_MONOTONIC_COARSE if available but only when it has - * millisecond granularity or better. CLOCK_MONOTONIC_COARSE is - * serviced entirely from the vDSO, whereas CLOCK_MONOTONIC may - * decide to make a costly system call. - */ - /* TODO(bnoordhuis) Use CLOCK_MONOTONIC_COARSE for UV_CLOCK_PRECISE - * when it has microsecond granularity or better (unlikely). - */ - clock_id = CLOCK_MONOTONIC; - if (type != UV_CLOCK_FAST) - goto done; - - clock_id = atomic_load_explicit(&fast_clock_id, memory_order_relaxed); - if (clock_id != -1) - goto done; - - clock_id = CLOCK_MONOTONIC; - if (0 == clock_getres(CLOCK_MONOTONIC_COARSE, &t)) - if (t.tv_nsec <= 1 * 1000 * 1000) - clock_id = CLOCK_MONOTONIC_COARSE; - - atomic_store_explicit(&fast_clock_id, clock_id, memory_order_relaxed); - -done: - - if (clock_gettime(clock_id, &t)) - return 0; /* Not really possible. */ - - return t.tv_sec * (uint64_t)1e9 + t.tv_nsec; -} diff --git a/src/jsc/bindings/uv-posix-polyfills.h b/src/jsc/bindings/uv-posix-polyfills.h deleted file mode 100644 index a2f6fd160007..000000000000 --- a/src/jsc/bindings/uv-posix-polyfills.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include "root.h" -#include -#include - -#if OS(LINUX) || OS(DARWIN) || OS(FREEBSD) - -// These functions are called by the stubs to crash with a nice error message -// when accessing a libuv functin which we do not support on posix -void CrashHandler__unsupportedUVFunction(const char* function_name); -void __bun_throw_not_implemented(const char* symbol_name); - -// libuv headers will use UV_EXTERN -#define UV_EXTERN __attribute__((visibility("default"))) __attribute__((used)) - -#include - -typedef enum { - UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */ - UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */ -} uv_clocktype_t; - -#endif diff --git a/src/jsc/bindings/uv-posix-stubs.c b/src/jsc/bindings/uv-posix-stubs.c deleted file mode 100644 index 04868a91cf9e..000000000000 --- a/src/jsc/bindings/uv-posix-stubs.c +++ /dev/null @@ -1,2120 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND -#include "uv-posix-polyfills.h" - -#if OS(LINUX) || OS(DARWIN) || OS(FREEBSD) -UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client) -{ - __bun_throw_not_implemented("uv_accept"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_async_init(uv_loop_t*, - uv_async_t* async, - uv_async_cb async_cb) -{ - __bun_throw_not_implemented("uv_async_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_async_send(uv_async_t* async) -{ - __bun_throw_not_implemented("uv_async_send"); - __builtin_unreachable(); -} - -UV_EXTERN unsigned int uv_available_parallelism(void) -{ - __bun_throw_not_implemented("uv_available_parallelism"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_backend_fd(const uv_loop_t*) -{ - __bun_throw_not_implemented("uv_backend_fd"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_backend_timeout(const uv_loop_t*) -{ - __bun_throw_not_implemented("uv_backend_timeout"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier) -{ - __bun_throw_not_implemented("uv_barrier_destroy"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count) -{ - __bun_throw_not_implemented("uv_barrier_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_barrier_wait(uv_barrier_t* barrier) -{ - __bun_throw_not_implemented("uv_barrier_wait"); - __builtin_unreachable(); -} - -UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len) -{ - __bun_throw_not_implemented("uv_buf_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_cancel(uv_req_t* req) -{ - __bun_throw_not_implemented("uv_cancel"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_chdir(const char* dir) -{ - __bun_throw_not_implemented("uv_chdir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check) -{ - __bun_throw_not_implemented("uv_check_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb) -{ - __bun_throw_not_implemented("uv_check_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_check_stop(uv_check_t* check) -{ - __bun_throw_not_implemented("uv_check_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_clock_gettime(uv_clock_id clock_id, uv_timespec64_t* ts) -{ - __bun_throw_not_implemented("uv_clock_gettime"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb) -{ - __bun_throw_not_implemented("uv_close"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond) -{ - __bun_throw_not_implemented("uv_cond_broadcast"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_cond_destroy(uv_cond_t* cond) -{ - __bun_throw_not_implemented("uv_cond_destroy"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_cond_init(uv_cond_t* cond) -{ - __bun_throw_not_implemented("uv_cond_init"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_cond_signal(uv_cond_t* cond) -{ - __bun_throw_not_implemented("uv_cond_signal"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, - uv_mutex_t* mutex, - uint64_t timeout) -{ - __bun_throw_not_implemented("uv_cond_timedwait"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex) -{ - __bun_throw_not_implemented("uv_cond_wait"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) -{ - __bun_throw_not_implemented("uv_cpu_info"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_cpumask_size(void) -{ - __bun_throw_not_implemented("uv_cpumask_size"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_cwd(char* buffer, size_t* size) -{ - __bun_throw_not_implemented("uv_cwd"); - __builtin_unreachable(); -} - -UV_EXTERN uv_loop_t* uv_default_loop(void) -{ - __bun_throw_not_implemented("uv_default_loop"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_disable_stdio_inheritance(void) -{ - __bun_throw_not_implemented("uv_disable_stdio_inheritance"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_dlclose(uv_lib_t* lib) -{ - __bun_throw_not_implemented("uv_dlclose"); - __builtin_unreachable(); -} - -UV_EXTERN const char* uv_dlerror(const uv_lib_t* lib) -{ - __bun_throw_not_implemented("uv_dlerror"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib) -{ - __bun_throw_not_implemented("uv_dlopen"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) -{ - __bun_throw_not_implemented("uv_dlsym"); - __builtin_unreachable(); -} - -UV_EXTERN const char* uv_err_name(int err) -{ - __bun_throw_not_implemented("uv_err_name"); - __builtin_unreachable(); -} - -UV_EXTERN char* uv_err_name_r(int err, char* buf, size_t buflen) -{ - __bun_throw_not_implemented("uv_err_name_r"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_exepath(char* buffer, size_t* size) -{ - __bun_throw_not_implemented("uv_exepath"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fileno(const uv_handle_t* handle, uv_os_fd_t* fd) -{ - __bun_throw_not_implemented("uv_fileno"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) -{ - __bun_throw_not_implemented("uv_free_cpu_info"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses, - int count) -{ - __bun_throw_not_implemented("uv_free_interface_addresses"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai) -{ - __bun_throw_not_implemented("uv_freeaddrinfo"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_access(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int mode, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_access"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_chmod(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int mode, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_chmod"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_chown(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_uid_t uid, - uv_gid_t gid, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_chown"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_close(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_close"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_closedir(uv_loop_t* loop, - uv_fs_t* req, - uv_dir_t* dir, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_closedir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_copyfile(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - int flags, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_copyfile"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_event_getpath(uv_fs_event_t* handle, - char* buffer, - size_t* size) -{ - __bun_throw_not_implemented("uv_fs_event_getpath"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle) -{ - __bun_throw_not_implemented("uv_fs_event_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_event_start(uv_fs_event_t* handle, - uv_fs_event_cb cb, - const char* path, - unsigned int flags) -{ - __bun_throw_not_implemented("uv_fs_event_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_event_stop(uv_fs_event_t* handle) -{ - __bun_throw_not_implemented("uv_fs_event_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_fchmod(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - int mode, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_fchmod"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_fchown(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_uid_t uid, - uv_gid_t gid, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_fchown"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_fdatasync"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_fstat"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_fsync"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_ftruncate(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - int64_t offset, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_ftruncate"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_futime(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - double atime, - double mtime, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_futime"); - __builtin_unreachable(); -} - -UV_EXTERN const char* uv_fs_get_path(const uv_fs_t*) -{ - __bun_throw_not_implemented("uv_fs_get_path"); - __builtin_unreachable(); -} - -UV_EXTERN void* uv_fs_get_ptr(const uv_fs_t*) -{ - __bun_throw_not_implemented("uv_fs_get_ptr"); - __builtin_unreachable(); -} - -UV_EXTERN ssize_t uv_fs_get_result(const uv_fs_t*) -{ - __bun_throw_not_implemented("uv_fs_get_result"); - __builtin_unreachable(); -} - -UV_EXTERN uv_stat_t* uv_fs_get_statbuf(uv_fs_t*) -{ - __bun_throw_not_implemented("uv_fs_get_statbuf"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_get_system_error(const uv_fs_t*) -{ - __bun_throw_not_implemented("uv_fs_get_system_error"); - __builtin_unreachable(); -} - -UV_EXTERN uv_fs_type uv_fs_get_type(const uv_fs_t*) -{ - __bun_throw_not_implemented("uv_fs_get_type"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_lchown(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_uid_t uid, - uv_gid_t gid, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_lchown"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_link(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_link"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_lstat"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_lutime(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - double atime, - double mtime, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_lutime"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int mode, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_mkdir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_mkdtemp(uv_loop_t* loop, - uv_fs_t* req, - const char* tpl, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_mkdtemp"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_mkstemp(uv_loop_t* loop, - uv_fs_t* req, - const char* tpl, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_mkstemp"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_open(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int flags, - int mode, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_open"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_opendir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_opendir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_poll_getpath(uv_fs_poll_t* handle, - char* buffer, - size_t* size) -{ - __bun_throw_not_implemented("uv_fs_poll_getpath"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle) -{ - __bun_throw_not_implemented("uv_fs_poll_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle, - uv_fs_poll_cb poll_cb, - const char* path, - unsigned int interval) -{ - __bun_throw_not_implemented("uv_fs_poll_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle) -{ - __bun_throw_not_implemented("uv_fs_poll_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_read(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - const uv_buf_t bufs[], - unsigned int nbufs, - int64_t offset, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_read"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, - uv_fs_t* req, - uv_dir_t* dir, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_readdir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_readlink"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_realpath(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_realpath"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_rename(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_rename"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req) -{ - __bun_throw_not_implemented("uv_fs_req_cleanup"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_rmdir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_scandir(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - int flags, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_scandir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_scandir_next(uv_fs_t* req, - uv_dirent_t* ent) -{ - __bun_throw_not_implemented("uv_fs_scandir_next"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_sendfile(uv_loop_t* loop, - uv_fs_t* req, - uv_file out_fd, - uv_file in_fd, - int64_t in_offset, - size_t length, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_sendfile"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_stat(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_stat"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_statfs(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_statfs"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - const char* new_path, - int flags, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_symlink"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_unlink"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_utime(uv_loop_t* loop, - uv_fs_t* req, - const char* path, - double atime, - double mtime, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_utime"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_fs_write(uv_loop_t* loop, - uv_fs_t* req, - uv_file file, - const uv_buf_t bufs[], - unsigned int nbufs, - int64_t offset, - uv_fs_cb cb) -{ - __bun_throw_not_implemented("uv_fs_write"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_get_available_memory(void) -{ - __bun_throw_not_implemented("uv_get_available_memory"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_get_constrained_memory(void) -{ - __bun_throw_not_implemented("uv_get_constrained_memory"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_get_free_memory(void) -{ - __bun_throw_not_implemented("uv_get_free_memory"); - __builtin_unreachable(); -} - -UV_EXTERN uv_os_fd_t uv_get_osfhandle(int fd) -{ - __bun_throw_not_implemented("uv_get_osfhandle"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_get_process_title(char* buffer, size_t size) -{ - __bun_throw_not_implemented("uv_get_process_title"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_get_total_memory(void) -{ - __bun_throw_not_implemented("uv_get_total_memory"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop, - uv_getaddrinfo_t* req, - uv_getaddrinfo_cb getaddrinfo_cb, - const char* node, - const char* service, - const struct addrinfo* hints) -{ - __bun_throw_not_implemented("uv_getaddrinfo"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_getnameinfo(uv_loop_t* loop, - uv_getnameinfo_t* req, - uv_getnameinfo_cb getnameinfo_cb, - const struct sockaddr* addr, - int flags) -{ - __bun_throw_not_implemented("uv_getnameinfo"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_getrusage(uv_rusage_t* rusage) -{ - __bun_throw_not_implemented("uv_getrusage"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_getrusage_thread(uv_rusage_t* rusage) -{ - __bun_throw_not_implemented("uv_getrusage_thread"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_gettimeofday(uv_timeval64_t* tv) -{ - __bun_throw_not_implemented("uv_gettimeofday"); - __builtin_unreachable(); -} - -UV_EXTERN uv_handle_type uv_guess_handle(uv_file file) -{ - __bun_throw_not_implemented("uv_guess_handle"); - __builtin_unreachable(); -} - -UV_EXTERN void* uv_handle_get_data(const uv_handle_t* handle) -{ - __bun_throw_not_implemented("uv_handle_get_data"); - __builtin_unreachable(); -} - -UV_EXTERN uv_loop_t* uv_handle_get_loop(const uv_handle_t* handle) -{ - __bun_throw_not_implemented("uv_handle_get_loop"); - __builtin_unreachable(); -} - -UV_EXTERN uv_handle_type uv_handle_get_type(const uv_handle_t* handle) -{ - __bun_throw_not_implemented("uv_handle_get_type"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_handle_set_data(uv_handle_t* handle, void* data) -{ - __bun_throw_not_implemented("uv_handle_set_data"); - __builtin_unreachable(); -} - -UV_EXTERN size_t uv_handle_size(uv_handle_type type) -{ - __bun_throw_not_implemented("uv_handle_size"); - __builtin_unreachable(); -} - -UV_EXTERN const char* uv_handle_type_name(uv_handle_type type) -{ - __bun_throw_not_implemented("uv_handle_type_name"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_has_ref(const uv_handle_t*) -{ - __bun_throw_not_implemented("uv_has_ref"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle) -{ - __bun_throw_not_implemented("uv_idle_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb) -{ - __bun_throw_not_implemented("uv_idle_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_idle_stop(uv_idle_t* idle) -{ - __bun_throw_not_implemented("uv_idle_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_if_indextoiid(unsigned int ifindex, - char* buffer, - size_t* size) -{ - __bun_throw_not_implemented("uv_if_indextoiid"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_if_indextoname(unsigned int ifindex, - char* buffer, - size_t* size) -{ - __bun_throw_not_implemented("uv_if_indextoname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size) -{ - __bun_throw_not_implemented("uv_inet_ntop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst) -{ - __bun_throw_not_implemented("uv_inet_pton"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses, - int* count) -{ - __bun_throw_not_implemented("uv_interface_addresses"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr) -{ - __bun_throw_not_implemented("uv_ip4_addr"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_ip4_name(const struct sockaddr_in* src, char* dst, size_t size) -{ - __bun_throw_not_implemented("uv_ip4_name"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr) -{ - __bun_throw_not_implemented("uv_ip6_addr"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_ip6_name(const struct sockaddr_in6* src, char* dst, size_t size) -{ - __bun_throw_not_implemented("uv_ip6_name"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_ip_name(const struct sockaddr* src, char* dst, size_t size) -{ - __bun_throw_not_implemented("uv_ip_name"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_is_active(const uv_handle_t* handle) -{ - __bun_throw_not_implemented("uv_is_active"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_is_closing(const uv_handle_t* handle) -{ - __bun_throw_not_implemented("uv_is_closing"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_is_readable(const uv_stream_t* handle) -{ - __bun_throw_not_implemented("uv_is_readable"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_is_writable(const uv_stream_t* handle) -{ - __bun_throw_not_implemented("uv_is_writable"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_key_create(uv_key_t* key) -{ - __bun_throw_not_implemented("uv_key_create"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_key_delete(uv_key_t* key) -{ - __bun_throw_not_implemented("uv_key_delete"); - __builtin_unreachable(); -} - -UV_EXTERN void* uv_key_get(uv_key_t* key) -{ - __bun_throw_not_implemented("uv_key_get"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_key_set(uv_key_t* key, void* value) -{ - __bun_throw_not_implemented("uv_key_set"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_kill(int pid, int signum) -{ - __bun_throw_not_implemented("uv_kill"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_library_shutdown(void) -{ - __bun_throw_not_implemented("uv_library_shutdown"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) -{ - __bun_throw_not_implemented("uv_listen"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_loadavg(double avg[3]) -{ - __bun_throw_not_implemented("uv_loadavg"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_loop_alive(const uv_loop_t* loop) -{ - __bun_throw_not_implemented("uv_loop_alive"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_loop_close(uv_loop_t* loop) -{ - __bun_throw_not_implemented("uv_loop_close"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_loop_configure(uv_loop_t* loop, uv_loop_option option, ...) -{ - __bun_throw_not_implemented("uv_loop_configure"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_loop_delete(uv_loop_t*) -{ - __bun_throw_not_implemented("uv_loop_delete"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_loop_fork(uv_loop_t* loop) -{ - __bun_throw_not_implemented("uv_loop_fork"); - __builtin_unreachable(); -} - -UV_EXTERN void* uv_loop_get_data(const uv_loop_t*) -{ - __bun_throw_not_implemented("uv_loop_get_data"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_loop_init(uv_loop_t* loop) -{ - __bun_throw_not_implemented("uv_loop_init"); - __builtin_unreachable(); -} - -UV_EXTERN uv_loop_t* uv_loop_new(void) -{ - __bun_throw_not_implemented("uv_loop_new"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_loop_set_data(uv_loop_t*, void* data) -{ - __bun_throw_not_implemented("uv_loop_set_data"); - __builtin_unreachable(); -} - -UV_EXTERN size_t uv_loop_size(void) -{ - __bun_throw_not_implemented("uv_loop_size"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_metrics_idle_time(uv_loop_t* loop) -{ - __bun_throw_not_implemented("uv_metrics_idle_time"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_metrics_info(uv_loop_t* loop, uv_metrics_t* metrics) -{ - __bun_throw_not_implemented("uv_metrics_info"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_now(const uv_loop_t*) -{ - __bun_throw_not_implemented("uv_now"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_open_osfhandle(uv_os_fd_t os_fd) -{ - __bun_throw_not_implemented("uv_open_osfhandle"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_environ(uv_env_item_t** envitems, int* count) -{ - __bun_throw_not_implemented("uv_os_environ"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_os_free_environ(uv_env_item_t* envitems, int count) -{ - __bun_throw_not_implemented("uv_os_free_environ"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_os_free_group(uv_group_t* grp) -{ - __bun_throw_not_implemented("uv_os_free_group"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_os_free_passwd(uv_passwd_t* pwd) -{ - __bun_throw_not_implemented("uv_os_free_passwd"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_get_group(uv_group_t* grp, uv_uid_t gid) -{ - __bun_throw_not_implemented("uv_os_get_group"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_get_passwd(uv_passwd_t* pwd) -{ - __bun_throw_not_implemented("uv_os_get_passwd"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_get_passwd2(uv_passwd_t* pwd, uv_uid_t uid) -{ - __bun_throw_not_implemented("uv_os_get_passwd2"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_getenv(const char* name, char* buffer, size_t* size) -{ - __bun_throw_not_implemented("uv_os_getenv"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_gethostname(char* buffer, size_t* size) -{ - __bun_throw_not_implemented("uv_os_gethostname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_getpriority(uv_pid_t pid, int* priority) -{ - __bun_throw_not_implemented("uv_os_getpriority"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_homedir(char* buffer, size_t* size) -{ - __bun_throw_not_implemented("uv_os_homedir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_setenv(const char* name, const char* value) -{ - __bun_throw_not_implemented("uv_os_setenv"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_setpriority(uv_pid_t pid, int priority) -{ - __bun_throw_not_implemented("uv_os_setpriority"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_tmpdir(char* buffer, size_t* size) -{ - __bun_throw_not_implemented("uv_os_tmpdir"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_uname(uv_utsname_t* buffer) -{ - __bun_throw_not_implemented("uv_os_uname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_os_unsetenv(const char* name) -{ - __bun_throw_not_implemented("uv_os_unsetenv"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe(uv_file fds[2], int read_flags, int write_flags) -{ - __bun_throw_not_implemented("uv_pipe"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name) -{ - __bun_throw_not_implemented("uv_pipe_bind"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_bind2(uv_pipe_t* handle, - const char* name, - size_t namelen, - unsigned int flags) -{ - __bun_throw_not_implemented("uv_pipe_bind2"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_chmod(uv_pipe_t* handle, int flags) -{ - __bun_throw_not_implemented("uv_pipe_chmod"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_pipe_connect(uv_connect_t* req, - uv_pipe_t* handle, - const char* name, - uv_connect_cb cb) -{ - __bun_throw_not_implemented("uv_pipe_connect"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_connect2(uv_connect_t* req, - uv_pipe_t* handle, - const char* name, - size_t namelen, - unsigned int flags, - uv_connect_cb cb) -{ - __bun_throw_not_implemented("uv_pipe_connect2"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_getpeername(const uv_pipe_t* handle, - char* buffer, - size_t* size) -{ - __bun_throw_not_implemented("uv_pipe_getpeername"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_getsockname(const uv_pipe_t* handle, - char* buffer, - size_t* size) -{ - __bun_throw_not_implemented("uv_pipe_getsockname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc) -{ - __bun_throw_not_implemented("uv_pipe_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file) -{ - __bun_throw_not_implemented("uv_pipe_open"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_pipe_pending_count(uv_pipe_t* handle) -{ - __bun_throw_not_implemented("uv_pipe_pending_count"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count) -{ - __bun_throw_not_implemented("uv_pipe_pending_instances"); - __builtin_unreachable(); -} - -UV_EXTERN uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle) -{ - __bun_throw_not_implemented("uv_pipe_pending_type"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) -{ - __bun_throw_not_implemented("uv_poll_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, - uv_poll_t* handle, - uv_os_sock_t socket) -{ - __bun_throw_not_implemented("uv_poll_init_socket"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb) -{ - __bun_throw_not_implemented("uv_poll_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_poll_stop(uv_poll_t* handle) -{ - __bun_throw_not_implemented("uv_poll_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare) -{ - __bun_throw_not_implemented("uv_prepare_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb) -{ - __bun_throw_not_implemented("uv_prepare_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare) -{ - __bun_throw_not_implemented("uv_prepare_stop"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_print_active_handles(uv_loop_t* loop, FILE* stream) -{ - __bun_throw_not_implemented("uv_print_active_handles"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_print_all_handles(uv_loop_t* loop, FILE* stream) -{ - __bun_throw_not_implemented("uv_print_all_handles"); - __builtin_unreachable(); -} - -UV_EXTERN uv_pid_t uv_process_get_pid(const uv_process_t*) -{ - __bun_throw_not_implemented("uv_process_get_pid"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_process_kill(uv_process_t*, int signum) -{ - __bun_throw_not_implemented("uv_process_kill"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_queue_work(uv_loop_t* loop, - uv_work_t* req, - uv_work_cb work_cb, - uv_after_work_cb after_work_cb) -{ - __bun_throw_not_implemented("uv_queue_work"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_random(uv_loop_t* loop, - uv_random_t* req, - void* buf, - size_t buflen, - unsigned flags, /* For future extension must be 0. */ - uv_random_cb cb) -{ - __bun_throw_not_implemented("uv_random"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_read_start(uv_stream_t*, - uv_alloc_cb alloc_cb, - uv_read_cb read_cb) -{ - __bun_throw_not_implemented("uv_read_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_read_stop(uv_stream_t*) -{ - __bun_throw_not_implemented("uv_read_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_recv_buffer_size(uv_handle_t* handle, int* value) -{ - __bun_throw_not_implemented("uv_recv_buffer_size"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_ref(uv_handle_t*) -{ - __bun_throw_not_implemented("uv_ref"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_replace_allocator(uv_malloc_func malloc_func, - uv_realloc_func realloc_func, - uv_calloc_func calloc_func, - uv_free_func free_func) -{ - __bun_throw_not_implemented("uv_replace_allocator"); - __builtin_unreachable(); -} - -UV_EXTERN void* uv_req_get_data(const uv_req_t* req) -{ - __bun_throw_not_implemented("uv_req_get_data"); - __builtin_unreachable(); -} - -UV_EXTERN uv_req_type uv_req_get_type(const uv_req_t* req) -{ - __bun_throw_not_implemented("uv_req_get_type"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_req_set_data(uv_req_t* req, void* data) -{ - __bun_throw_not_implemented("uv_req_set_data"); - __builtin_unreachable(); -} - -UV_EXTERN size_t uv_req_size(uv_req_type type) -{ - __bun_throw_not_implemented("uv_req_size"); - __builtin_unreachable(); -} - -UV_EXTERN const char* uv_req_type_name(uv_req_type type) -{ - __bun_throw_not_implemented("uv_req_type_name"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_resident_set_memory(size_t* rss) -{ - __bun_throw_not_implemented("uv_resident_set_memory"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode) -{ - __bun_throw_not_implemented("uv_run"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_destroy"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_init"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_rdlock"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_rwlock_rdunlock(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_rdunlock"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_tryrdlock"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_trywrlock"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_wrlock"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock) -{ - __bun_throw_not_implemented("uv_rwlock_wrunlock"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_sem_destroy(uv_sem_t* sem) -{ - __bun_throw_not_implemented("uv_sem_destroy"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value) -{ - __bun_throw_not_implemented("uv_sem_init"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_sem_post(uv_sem_t* sem) -{ - __bun_throw_not_implemented("uv_sem_post"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_sem_trywait(uv_sem_t* sem) -{ - __bun_throw_not_implemented("uv_sem_trywait"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_sem_wait(uv_sem_t* sem) -{ - __bun_throw_not_implemented("uv_sem_wait"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_send_buffer_size(uv_handle_t* handle, int* value) -{ - __bun_throw_not_implemented("uv_send_buffer_size"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_set_process_title(const char* title) -{ - __bun_throw_not_implemented("uv_set_process_title"); - __builtin_unreachable(); -} - -UV_EXTERN char** uv_setup_args(int argc, char** argv) -{ - __bun_throw_not_implemented("uv_setup_args"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_shutdown(uv_shutdown_t* req, - uv_stream_t* handle, - uv_shutdown_cb cb) -{ - __bun_throw_not_implemented("uv_shutdown"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) -{ - __bun_throw_not_implemented("uv_signal_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_signal_start(uv_signal_t* handle, - uv_signal_cb signal_cb, - int signum) -{ - __bun_throw_not_implemented("uv_signal_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_signal_start_oneshot(uv_signal_t* handle, - uv_signal_cb signal_cb, - int signum) -{ - __bun_throw_not_implemented("uv_signal_start_oneshot"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_signal_stop(uv_signal_t* handle) -{ - __bun_throw_not_implemented("uv_signal_stop"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_sleep(unsigned int msec) -{ - __bun_throw_not_implemented("uv_sleep"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_socketpair(int type, - int protocol, - uv_os_sock_t socket_vector[2], - int flags0, - int flags1) -{ - __bun_throw_not_implemented("uv_socketpair"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_spawn(uv_loop_t* loop, - uv_process_t* handle, - const uv_process_options_t* options) -{ - __bun_throw_not_implemented("uv_spawn"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_stop(uv_loop_t*) -{ - __bun_throw_not_implemented("uv_stop"); - __builtin_unreachable(); -} - -UV_EXTERN size_t uv_stream_get_write_queue_size(const uv_stream_t* stream) -{ - __bun_throw_not_implemented("uv_stream_get_write_queue_size"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_stream_set_blocking(uv_stream_t* handle, int blocking) -{ - __bun_throw_not_implemented("uv_stream_set_blocking"); - __builtin_unreachable(); -} - -UV_EXTERN const char* uv_strerror(int err) -{ - __bun_throw_not_implemented("uv_strerror"); - __builtin_unreachable(); -} - -UV_EXTERN char* uv_strerror_r(int err, char* buf, size_t buflen) -{ - __bun_throw_not_implemented("uv_strerror_r"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, - const struct sockaddr* addr, - unsigned int flags) -{ - __bun_throw_not_implemented("uv_tcp_bind"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb) -{ - __bun_throw_not_implemented("uv_tcp_close_reset"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_connect(uv_connect_t* req, - uv_tcp_t* handle, - const struct sockaddr* addr, - uv_connect_cb cb) -{ - __bun_throw_not_implemented("uv_tcp_connect"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_getpeername(const uv_tcp_t* handle, - struct sockaddr* name, - int* namelen) -{ - __bun_throw_not_implemented("uv_tcp_getpeername"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_getsockname(const uv_tcp_t* handle, - struct sockaddr* name, - int* namelen) -{ - __bun_throw_not_implemented("uv_tcp_getsockname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle) -{ - __bun_throw_not_implemented("uv_tcp_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_init_ex(uv_loop_t*, uv_tcp_t* handle, unsigned int flags) -{ - __bun_throw_not_implemented("uv_tcp_init_ex"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle, - int enable, - unsigned int delay) -{ - __bun_throw_not_implemented("uv_tcp_keepalive"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable) -{ - __bun_throw_not_implemented("uv_tcp_nodelay"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock) -{ - __bun_throw_not_implemented("uv_tcp_open"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable) -{ - __bun_throw_not_implemented("uv_tcp_simultaneous_accepts"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg) -{ - __bun_throw_not_implemented("uv_thread_create"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_create_ex(uv_thread_t* tid, - const uv_thread_options_t* params, - uv_thread_cb entry, - void* arg) -{ - __bun_throw_not_implemented("uv_thread_create_ex"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_detach(uv_thread_t* tid) -{ - __bun_throw_not_implemented("uv_thread_detach"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2) -{ - __bun_throw_not_implemented("uv_thread_equal"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_getaffinity(uv_thread_t* tid, - char* cpumask, - size_t mask_size) -{ - __bun_throw_not_implemented("uv_thread_getaffinity"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_getcpu(void) -{ - __bun_throw_not_implemented("uv_thread_getcpu"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_getname(uv_thread_t* tid, char* name, size_t size) -{ - __bun_throw_not_implemented("uv_thread_getname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_getpriority(uv_thread_t tid, int* priority) -{ - __bun_throw_not_implemented("uv_thread_getpriority"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_join(uv_thread_t* tid) -{ - __bun_throw_not_implemented("uv_thread_join"); - __builtin_unreachable(); -} - -UV_EXTERN uv_thread_t uv_thread_self(void) -{ - __bun_throw_not_implemented("uv_thread_self"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_setaffinity(uv_thread_t* tid, - char* cpumask, - char* oldmask, - size_t mask_size) -{ - __bun_throw_not_implemented("uv_thread_setaffinity"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_setname(const char* name) -{ - __bun_throw_not_implemented("uv_thread_setname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_thread_setpriority(uv_thread_t tid, int priority) -{ - __bun_throw_not_implemented("uv_thread_setpriority"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_timer_again(uv_timer_t* handle) -{ - __bun_throw_not_implemented("uv_timer_again"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_timer_get_due_in(const uv_timer_t* handle) -{ - __bun_throw_not_implemented("uv_timer_get_due_in"); - __builtin_unreachable(); -} - -UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle) -{ - __bun_throw_not_implemented("uv_timer_get_repeat"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle) -{ - __bun_throw_not_implemented("uv_timer_init"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat) -{ - __bun_throw_not_implemented("uv_timer_set_repeat"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_timer_start(uv_timer_t* handle, - uv_timer_cb cb, - uint64_t timeout, - uint64_t repeat) -{ - __bun_throw_not_implemented("uv_timer_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_timer_stop(uv_timer_t* handle) -{ - __bun_throw_not_implemented("uv_timer_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_translate_sys_error(int sys_errno) -{ - __bun_throw_not_implemented("uv_translate_sys_error"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_try_write(uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs) -{ - __bun_throw_not_implemented("uv_try_write"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_try_write2(uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - uv_stream_t* send_handle) -{ - __bun_throw_not_implemented("uv_try_write2"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tty_get_vterm_state(uv_tty_vtermstate_t* state) -{ - __bun_throw_not_implemented("uv_tty_get_vterm_state"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height) -{ - __bun_throw_not_implemented("uv_tty_get_winsize"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable) -{ - __bun_throw_not_implemented("uv_tty_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_tty_set_mode(uv_tty_t*, uv_tty_mode_t mode) -{ - __bun_throw_not_implemented("uv_tty_set_mode"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_tty_set_vterm_state(uv_tty_vtermstate_t state) -{ - __bun_throw_not_implemented("uv_tty_set_vterm_state"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_bind(uv_udp_t* handle, - const struct sockaddr* addr, - unsigned int flags) -{ - __bun_throw_not_implemented("uv_udp_bind"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_connect(uv_udp_t* handle, const struct sockaddr* addr) -{ - __bun_throw_not_implemented("uv_udp_connect"); - __builtin_unreachable(); -} - -UV_EXTERN size_t uv_udp_get_send_queue_count(const uv_udp_t* handle) -{ - __bun_throw_not_implemented("uv_udp_get_send_queue_count"); - __builtin_unreachable(); -} - -UV_EXTERN size_t uv_udp_get_send_queue_size(const uv_udp_t* handle) -{ - __bun_throw_not_implemented("uv_udp_get_send_queue_size"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_getpeername(const uv_udp_t* handle, - struct sockaddr* name, - int* namelen) -{ - __bun_throw_not_implemented("uv_udp_getpeername"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_getsockname(const uv_udp_t* handle, - struct sockaddr* name, - int* namelen) -{ - __bun_throw_not_implemented("uv_udp_getsockname"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle) -{ - __bun_throw_not_implemented("uv_udp_init"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_init_ex(uv_loop_t*, uv_udp_t* handle, unsigned int flags) -{ - __bun_throw_not_implemented("uv_udp_init_ex"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) -{ - __bun_throw_not_implemented("uv_udp_open"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, - uv_alloc_cb alloc_cb, - uv_udp_recv_cb recv_cb) -{ - __bun_throw_not_implemented("uv_udp_recv_start"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle) -{ - __bun_throw_not_implemented("uv_udp_recv_stop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_send(uv_udp_send_t* req, - uv_udp_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - const struct sockaddr* addr, - uv_udp_send_cb send_cb) -{ - __bun_throw_not_implemented("uv_udp_send"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on) -{ - __bun_throw_not_implemented("uv_udp_set_broadcast"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle, - const char* multicast_addr, - const char* interface_addr, - uv_membership membership) -{ - __bun_throw_not_implemented("uv_udp_set_membership"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_set_multicast_interface(uv_udp_t* handle, - const char* interface_addr) -{ - __bun_throw_not_implemented("uv_udp_set_multicast_interface"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on) -{ - __bun_throw_not_implemented("uv_udp_set_multicast_loop"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) -{ - __bun_throw_not_implemented("uv_udp_set_multicast_ttl"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_set_source_membership(uv_udp_t* handle, - const char* multicast_addr, - const char* interface_addr, - const char* source_addr, - uv_membership membership) -{ - __bun_throw_not_implemented("uv_udp_set_source_membership"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl) -{ - __bun_throw_not_implemented("uv_udp_set_ttl"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_try_send(uv_udp_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - const struct sockaddr* addr) -{ - __bun_throw_not_implemented("uv_udp_try_send"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_try_send2(uv_udp_t* handle, - unsigned int count, - uv_buf_t* bufs[/*count*/], - unsigned int nbufs[/*count*/], - struct sockaddr* addrs[/*count*/], - unsigned int flags) -{ - __bun_throw_not_implemented("uv_udp_try_send2"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_udp_using_recvmmsg(const uv_udp_t* handle) -{ - __bun_throw_not_implemented("uv_udp_using_recvmmsg"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_unref(uv_handle_t*) -{ - __bun_throw_not_implemented("uv_unref"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_update_time(uv_loop_t*) -{ - __bun_throw_not_implemented("uv_update_time"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_uptime(double* uptime) -{ - __bun_throw_not_implemented("uv_uptime"); - __builtin_unreachable(); -} - -UV_EXTERN size_t uv_utf16_length_as_wtf8(const uint16_t* utf16, - ssize_t utf16_len) -{ - __bun_throw_not_implemented("uv_utf16_length_as_wtf8"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_utf16_to_wtf8(const uint16_t* utf16, - ssize_t utf16_len, - char** wtf8_ptr, - size_t* wtf8_len_ptr) -{ - __bun_throw_not_implemented("uv_utf16_to_wtf8"); - __builtin_unreachable(); -} - -UV_EXTERN unsigned int uv_version(void) -{ - __bun_throw_not_implemented("uv_version"); - __builtin_unreachable(); -} - -UV_EXTERN const char* uv_version_string(void) -{ - __bun_throw_not_implemented("uv_version_string"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg) -{ - __bun_throw_not_implemented("uv_walk"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_write(uv_write_t* req, - uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - uv_write_cb cb) -{ - __bun_throw_not_implemented("uv_write"); - __builtin_unreachable(); -} - -UV_EXTERN int uv_write2(uv_write_t* req, - uv_stream_t* handle, - const uv_buf_t bufs[], - unsigned int nbufs, - uv_stream_t* send_handle, - uv_write_cb cb) -{ - __bun_throw_not_implemented("uv_write2"); - __builtin_unreachable(); -} - -UV_EXTERN ssize_t uv_wtf8_length_as_utf16(const char* wtf8) -{ - __bun_throw_not_implemented("uv_wtf8_length_as_utf16"); - __builtin_unreachable(); -} - -UV_EXTERN void uv_wtf8_to_utf16(const char* wtf8, - uint16_t* utf16, - size_t utf16_len) -{ - __bun_throw_not_implemented("uv_wtf8_to_utf16"); - __builtin_unreachable(); -} -#endif diff --git a/src/jsc/bindings/uv-stubs.c b/src/jsc/bindings/uv-stubs.c new file mode 100644 index 000000000000..ce80e7319271 --- /dev/null +++ b/src/jsc/bindings/uv-stubs.c @@ -0,0 +1,317 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND +#include "uv-polyfills.h" + +// A stub reads none of its arguments and does not return, so it has no use for +// the function's prototype. +#define STUB(name) \ + UV_EXTERN void name(void) \ + { \ + __bun_throw_not_implemented(#name); \ + __builtin_unreachable(); \ + } + +STUB(uv_accept) +STUB(uv_async_init) +STUB(uv_async_send) +STUB(uv_available_parallelism) +STUB(uv_backend_fd) +STUB(uv_backend_timeout) +STUB(uv_barrier_destroy) +STUB(uv_barrier_init) +STUB(uv_barrier_wait) +STUB(uv_buf_init) +STUB(uv_cancel) +STUB(uv_chdir) +STUB(uv_check_init) +STUB(uv_check_start) +STUB(uv_check_stop) +STUB(uv_clock_gettime) +STUB(uv_close) +STUB(uv_cond_broadcast) +STUB(uv_cond_destroy) +STUB(uv_cond_init) +STUB(uv_cond_signal) +STUB(uv_cond_timedwait) +STUB(uv_cond_wait) +STUB(uv_cpu_info) +STUB(uv_cpumask_size) +STUB(uv_cwd) +STUB(uv_default_loop) +STUB(uv_disable_stdio_inheritance) +STUB(uv_dlclose) +STUB(uv_dlerror) +STUB(uv_dlopen) +STUB(uv_dlsym) +STUB(uv_err_name) +STUB(uv_err_name_r) +STUB(uv_exepath) +STUB(uv_fileno) +STUB(uv_free_cpu_info) +STUB(uv_free_interface_addresses) +STUB(uv_freeaddrinfo) +STUB(uv_fs_access) +STUB(uv_fs_chmod) +STUB(uv_fs_chown) +STUB(uv_fs_close) +STUB(uv_fs_closedir) +STUB(uv_fs_copyfile) +STUB(uv_fs_event_getpath) +STUB(uv_fs_event_init) +STUB(uv_fs_event_start) +STUB(uv_fs_event_stop) +STUB(uv_fs_fchmod) +STUB(uv_fs_fchown) +STUB(uv_fs_fdatasync) +STUB(uv_fs_fstat) +STUB(uv_fs_fsync) +STUB(uv_fs_ftruncate) +STUB(uv_fs_futime) +STUB(uv_fs_get_path) +STUB(uv_fs_get_ptr) +STUB(uv_fs_get_result) +STUB(uv_fs_get_statbuf) +STUB(uv_fs_get_system_error) +STUB(uv_fs_get_type) +STUB(uv_fs_lchown) +STUB(uv_fs_link) +STUB(uv_fs_lstat) +STUB(uv_fs_lutime) +STUB(uv_fs_mkdir) +STUB(uv_fs_mkdtemp) +STUB(uv_fs_mkstemp) +STUB(uv_fs_open) +STUB(uv_fs_opendir) +STUB(uv_fs_poll_getpath) +STUB(uv_fs_poll_init) +STUB(uv_fs_poll_start) +STUB(uv_fs_poll_stop) +STUB(uv_fs_read) +STUB(uv_fs_readdir) +STUB(uv_fs_readlink) +STUB(uv_fs_realpath) +STUB(uv_fs_rename) +STUB(uv_fs_req_cleanup) +STUB(uv_fs_rmdir) +STUB(uv_fs_scandir) +STUB(uv_fs_scandir_next) +STUB(uv_fs_sendfile) +STUB(uv_fs_stat) +STUB(uv_fs_statfs) +STUB(uv_fs_symlink) +STUB(uv_fs_unlink) +STUB(uv_fs_utime) +STUB(uv_fs_write) +STUB(uv_get_available_memory) +STUB(uv_get_constrained_memory) +STUB(uv_get_free_memory) +STUB(uv_get_osfhandle) +STUB(uv_get_process_title) +STUB(uv_get_total_memory) +STUB(uv_getaddrinfo) +STUB(uv_getnameinfo) +STUB(uv_getrusage) +STUB(uv_getrusage_thread) +STUB(uv_gettimeofday) +STUB(uv_guess_handle) +STUB(uv_handle_get_data) +STUB(uv_handle_get_loop) +STUB(uv_handle_get_type) +STUB(uv_handle_set_data) +STUB(uv_handle_size) +STUB(uv_handle_type_name) +STUB(uv_has_ref) +STUB(uv_idle_init) +STUB(uv_idle_start) +STUB(uv_idle_stop) +STUB(uv_if_indextoiid) +STUB(uv_if_indextoname) +STUB(uv_inet_ntop) +STUB(uv_inet_pton) +STUB(uv_interface_addresses) +STUB(uv_ip4_addr) +STUB(uv_ip4_name) +STUB(uv_ip6_addr) +STUB(uv_ip6_name) +STUB(uv_ip_name) +STUB(uv_is_active) +STUB(uv_is_closing) +STUB(uv_is_readable) +STUB(uv_is_writable) +STUB(uv_key_create) +STUB(uv_key_delete) +STUB(uv_key_get) +STUB(uv_key_set) +STUB(uv_kill) +STUB(uv_library_shutdown) +STUB(uv_listen) +STUB(uv_loadavg) +STUB(uv_loop_alive) +STUB(uv_loop_close) +STUB(uv_loop_configure) +STUB(uv_loop_delete) +STUB(uv_loop_fork) +STUB(uv_loop_get_data) +STUB(uv_loop_init) +STUB(uv_loop_new) +STUB(uv_loop_set_data) +STUB(uv_loop_size) +STUB(uv_metrics_idle_time) +STUB(uv_metrics_info) +STUB(uv_now) +STUB(uv_open_osfhandle) +STUB(uv_os_environ) +STUB(uv_os_free_environ) +STUB(uv_os_free_group) +STUB(uv_os_free_passwd) +STUB(uv_os_get_group) +STUB(uv_os_get_passwd) +STUB(uv_os_get_passwd2) +STUB(uv_os_getenv) +STUB(uv_os_gethostname) +STUB(uv_os_getpriority) +STUB(uv_os_homedir) +STUB(uv_os_setenv) +STUB(uv_os_setpriority) +STUB(uv_os_tmpdir) +STUB(uv_os_uname) +STUB(uv_os_unsetenv) +STUB(uv_pipe) +STUB(uv_pipe_bind) +STUB(uv_pipe_bind2) +STUB(uv_pipe_chmod) +STUB(uv_pipe_connect) +STUB(uv_pipe_connect2) +STUB(uv_pipe_getpeername) +STUB(uv_pipe_getsockname) +STUB(uv_pipe_init) +STUB(uv_pipe_open) +STUB(uv_pipe_pending_count) +STUB(uv_pipe_pending_instances) +STUB(uv_pipe_pending_type) +STUB(uv_poll_init) +STUB(uv_poll_init_socket) +STUB(uv_poll_start) +STUB(uv_poll_stop) +STUB(uv_prepare_init) +STUB(uv_prepare_start) +STUB(uv_prepare_stop) +STUB(uv_print_active_handles) +STUB(uv_print_all_handles) +STUB(uv_process_get_pid) +STUB(uv_process_kill) +STUB(uv_queue_work) +STUB(uv_random) +STUB(uv_read_start) +STUB(uv_read_stop) +STUB(uv_recv_buffer_size) +STUB(uv_ref) +STUB(uv_replace_allocator) +STUB(uv_req_get_data) +STUB(uv_req_get_type) +STUB(uv_req_set_data) +STUB(uv_req_size) +STUB(uv_req_type_name) +STUB(uv_resident_set_memory) +STUB(uv_run) +STUB(uv_rwlock_destroy) +STUB(uv_rwlock_init) +STUB(uv_rwlock_rdlock) +STUB(uv_rwlock_rdunlock) +STUB(uv_rwlock_tryrdlock) +STUB(uv_rwlock_trywrlock) +STUB(uv_rwlock_wrlock) +STUB(uv_rwlock_wrunlock) +STUB(uv_sem_destroy) +STUB(uv_sem_init) +STUB(uv_sem_post) +STUB(uv_sem_trywait) +STUB(uv_sem_wait) +STUB(uv_send_buffer_size) +STUB(uv_set_process_title) +STUB(uv_setup_args) +STUB(uv_shutdown) +STUB(uv_signal_init) +STUB(uv_signal_start) +STUB(uv_signal_start_oneshot) +STUB(uv_signal_stop) +STUB(uv_sleep) +STUB(uv_socketpair) +STUB(uv_spawn) +STUB(uv_stop) +STUB(uv_stream_get_write_queue_size) +STUB(uv_stream_set_blocking) +STUB(uv_strerror) +STUB(uv_strerror_r) +STUB(uv_tcp_bind) +STUB(uv_tcp_close_reset) +STUB(uv_tcp_connect) +STUB(uv_tcp_getpeername) +STUB(uv_tcp_getsockname) +STUB(uv_tcp_init) +STUB(uv_tcp_init_ex) +STUB(uv_tcp_keepalive) +STUB(uv_tcp_nodelay) +STUB(uv_tcp_open) +STUB(uv_tcp_simultaneous_accepts) +STUB(uv_thread_create) +STUB(uv_thread_create_ex) +STUB(uv_thread_detach) +STUB(uv_thread_equal) +STUB(uv_thread_getaffinity) +STUB(uv_thread_getcpu) +STUB(uv_thread_getname) +STUB(uv_thread_getpriority) +STUB(uv_thread_join) +STUB(uv_thread_self) +STUB(uv_thread_setaffinity) +STUB(uv_thread_setname) +STUB(uv_thread_setpriority) +STUB(uv_timer_again) +STUB(uv_timer_get_due_in) +STUB(uv_timer_get_repeat) +STUB(uv_timer_init) +STUB(uv_timer_set_repeat) +STUB(uv_timer_start) +STUB(uv_timer_stop) +STUB(uv_translate_sys_error) +STUB(uv_try_write) +STUB(uv_try_write2) +STUB(uv_tty_get_vterm_state) +STUB(uv_tty_get_winsize) +STUB(uv_tty_init) +STUB(uv_tty_set_mode) +STUB(uv_tty_set_vterm_state) +STUB(uv_udp_bind) +STUB(uv_udp_connect) +STUB(uv_udp_get_send_queue_count) +STUB(uv_udp_get_send_queue_size) +STUB(uv_udp_getpeername) +STUB(uv_udp_getsockname) +STUB(uv_udp_init) +STUB(uv_udp_init_ex) +STUB(uv_udp_open) +STUB(uv_udp_recv_start) +STUB(uv_udp_recv_stop) +STUB(uv_udp_send) +STUB(uv_udp_set_broadcast) +STUB(uv_udp_set_membership) +STUB(uv_udp_set_multicast_interface) +STUB(uv_udp_set_multicast_loop) +STUB(uv_udp_set_multicast_ttl) +STUB(uv_udp_set_source_membership) +STUB(uv_udp_set_ttl) +STUB(uv_udp_try_send) +STUB(uv_udp_try_send2) +STUB(uv_udp_using_recvmmsg) +STUB(uv_unref) +STUB(uv_update_time) +STUB(uv_uptime) +STUB(uv_utf16_length_as_wtf8) +STUB(uv_utf16_to_wtf8) +STUB(uv_version) +STUB(uv_version_string) +STUB(uv_walk) +STUB(uv_write) +STUB(uv_write2) +STUB(uv_wtf8_length_as_utf16) +STUB(uv_wtf8_to_utf16) diff --git a/src/jsc/bindings/v8/node.cpp b/src/jsc/bindings/v8/node.cpp index 7389e978c201..3693a9792ed1 100644 --- a/src/jsc/bindings/v8/node.cpp +++ b/src/jsc/bindings/v8/node.cpp @@ -42,14 +42,10 @@ void RemoveEnvironmentCleanupHook(v8::Isolate* isolate, struct uv_loop_s* GetCurrentEventLoop(v8::Isolate* isolate) { -#if OS(WINDOWS) - return reinterpret_cast(isolate->globalObject()->uvLoop()); -#else - // Bun does not run a libuv event loop on POSIX; per node.h this may return + // Bun does not run a libuv event loop; per node.h this may return // nullptr when the context is not associated with a Node instance. (void)isolate; return nullptr; -#endif } async_id AsyncHooksGetExecutionAsyncId(v8::Local context) diff --git a/src/jsc/bindings/vm/Semaphore.cpp b/src/jsc/bindings/vm/Semaphore.cpp index f1eec7ef05fc..97b9a22cab54 100644 --- a/src/jsc/bindings/vm/Semaphore.cpp +++ b/src/jsc/bindings/vm/Semaphore.cpp @@ -1,11 +1,16 @@ #include "Semaphore.h" +#if OS(WINDOWS) +#include +#endif + namespace Bun { Semaphore::Semaphore(unsigned int value) { #if OS(WINDOWS) - uv_sem_init(&m_semaphore, value); + m_semaphore = CreateSemaphoreW(nullptr, static_cast(value), LONG_MAX, nullptr); + RELEASE_ASSERT(m_semaphore); #elif OS(DARWIN) semaphore_create(mach_task_self(), &m_semaphore, SYNC_POLICY_FIFO, value); #else @@ -16,7 +21,7 @@ Semaphore::Semaphore(unsigned int value) Semaphore::~Semaphore() { #if OS(WINDOWS) - uv_sem_destroy(&m_semaphore); + CloseHandle(m_semaphore); #elif OS(DARWIN) semaphore_destroy(mach_task_self(), m_semaphore); #else @@ -27,8 +32,7 @@ Semaphore::~Semaphore() bool Semaphore::signal() { #if OS(WINDOWS) - uv_sem_post(&m_semaphore); - return true; + return ReleaseSemaphore(m_semaphore, 1, nullptr); #elif OS(DARWIN) return semaphore_signal(m_semaphore) == KERN_SUCCESS; #else @@ -39,8 +43,7 @@ bool Semaphore::signal() bool Semaphore::wait() { #if OS(WINDOWS) - uv_sem_wait(&m_semaphore); - return true; + return WaitForSingleObject(m_semaphore, INFINITE) == WAIT_OBJECT_0; #elif OS(DARWIN) return semaphore_wait(m_semaphore) == KERN_SUCCESS; #else diff --git a/src/jsc/bindings/vm/Semaphore.h b/src/jsc/bindings/vm/Semaphore.h index cb3ae7d93c6b..99d5a21b3a13 100644 --- a/src/jsc/bindings/vm/Semaphore.h +++ b/src/jsc/bindings/vm/Semaphore.h @@ -2,12 +2,10 @@ #include "root.h" -#if OS(WINDOWS) -#include -#elif OS(DARWIN) +#if OS(DARWIN) #include #include -#else +#elif !OS(WINDOWS) #include #endif @@ -23,7 +21,7 @@ class Semaphore { private: #if OS(WINDOWS) - uv_sem_t m_semaphore; + void* m_semaphore; // HANDLE #elif OS(DARWIN) semaphore_t m_semaphore; #else diff --git a/src/jsc/bindings/webcore/JSWorker.cpp b/src/jsc/bindings/webcore/JSWorker.cpp index 1f64a20e4496..e3031ee81f14 100644 --- a/src/jsc/bindings/webcore/JSWorker.cpp +++ b/src/jsc/bindings/webcore/JSWorker.cpp @@ -25,7 +25,7 @@ #include "ActiveDOMObject.h" #include "BunCPUProfiler.h" #if OS(WINDOWS) -#include +#include "BunWindowsProcess.h" #else #include #if defined(__APPLE__) @@ -901,10 +901,10 @@ static inline JSC::EncodedJSValue jsWorkerPrototypeFunction_cpuUsageInternalBody double user = 0; double sys = 0; #if OS(WINDOWS) - uv_rusage_t ru; - if (uv_getrusage_thread(&ru) == 0) { - user = static_cast(ru.ru_utime.tv_sec) * 1e6 + static_cast(ru.ru_utime.tv_usec); - sys = static_cast(ru.ru_stime.tv_sec) * 1e6 + static_cast(ru.ru_stime.tv_usec); + Bun::CpuTimes times; + if (Bun::getThreadCpuTimes(times) == 0) { + user = static_cast(times.user); + sys = static_cast(times.system); } #elif defined(__APPLE__) // Darwin has no RUSAGE_THREAD; RUSAGE_SELF would report whole-process diff --git a/src/jsc/bindings/workaround-missing-symbols.cpp b/src/jsc/bindings/workaround-missing-symbols.cpp index 0ee12bafda18..4c9e4b4ca051 100644 --- a/src/jsc/bindings/workaround-missing-symbols.cpp +++ b/src/jsc/bindings/workaround-missing-symbols.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -36,11 +35,6 @@ extern "C" int stat64( return _stat64(_FileName, _Stat); } -extern "C" int kill(int pid, int sig) -{ - return uv_kill(pid, sig); -} - #endif #if !defined(WIN32) @@ -498,6 +492,7 @@ extern "C" int __wrap___libc_start_main(int (*main)(int, char**, char**), int ar #include #include +#include #include #include #include @@ -528,10 +523,11 @@ void std::__libcpp_verbose_abort(char const* format, ...) BUN_VERBOSE_ABORT_NOEX va_list list; va_start(list, format); char buffer[1024]; - size_t len = vsnprintf(buffer, sizeof(buffer), format, list); + // The return value is the length of the whole message, also when only part of it fit. + int formatted = vsnprintf(buffer, sizeof(buffer), format, list); va_end(list); - Bun__panic(buffer, len); + Bun__panic(buffer, formatted < 0 ? 0 : std::min(formatted, sizeof(buffer) - 1)); } #undef BUN_VERBOSE_ABORT_NOEXCEPT diff --git a/src/jsc/bindings/wtf-bindings.cpp b/src/jsc/bindings/wtf-bindings.cpp index 4c6344fe0050..d4583540c837 100644 --- a/src/jsc/bindings/wtf-bindings.cpp +++ b/src/jsc/bindings/wtf-bindings.cpp @@ -13,8 +13,23 @@ static_assert(WTF::maxECMAScriptTime == 8.64e15, "bun_jsc::wtf::MAX_ECMASCRIPT_TIME in WTF.rs must match"); #include "wtf/SIMDUTF.h" + #if OS(WINDOWS) -#include +#include + +// The console is owned by bun_io (src/io/windows/tty.rs). `mode` is a libuv +// uv_tty_mode_t; the result is 0 or a negative UV_E* number. +extern "C" int Bun__Windows__setConsoleMode(void* inputHandle, int mode); +extern "C" void Bun__Windows__resetConsoleMode(); +// `_get_osfhandle`; INVALID_HANDLE_VALUE for an fd that has no HANDLE. +extern "C" void* Bun__crtGetOsfhandle(int fd); + +// Exported through src/symbols.def. +extern "C" int uv_tty_reset_mode(void) +{ + Bun__Windows__resetConsoleMode(); + return 0; +} #endif #if !OS(WINDOWS) @@ -216,11 +231,11 @@ extern "C" int Bun__ttySetMode(int fd, int mode, void* rawState, int drain) memcpy(rawState, &state, sizeof(state)); return rc; #else - UNUSED_PARAM(fd); - UNUSED_PARAM(mode); - UNUSED_PARAM(rawState); UNUSED_PARAM(drain); - return 0; + // Nothing to remember per stream: the mode belongs to the console, which + // another stream or uv_tty_reset_mode can have changed since. + UNUSED_PARAM(rawState); + return Bun__Windows__setConsoleMode(Bun__crtGetOsfhandle(fd), mode); #endif } diff --git a/src/jsc/btjs.rs b/src/jsc/btjs.rs index 61f518497239..616893724fe7 100644 --- a/src/jsc/btjs.rs +++ b/src/jsc/btjs.rs @@ -47,13 +47,7 @@ mod tty { // CRT keeps its own narrow-string env cache that is not updated by // `SetEnvironmentVariableW`, which is how Bun mutates env vars at // runtime — so `libc::getenv` would silently miss those. - unsafe extern "system" { - fn GetEnvironmentVariableW( - lpName: *const u16, - lpBuffer: *mut u16, - nSize: u32, - ) -> u32; - } + use bun_sys::windows::kernel32::GetEnvironmentVariableW; // `name` is a compile-time ASCII C string (c"NO_COLOR" / c"CLICOLOR_FORCE"); // widen byte-by-byte into a NUL-terminated WCHAR buffer on the stack. let bytes = name.to_bytes(); diff --git a/src/jsc/error.rs b/src/jsc/error.rs index 27bb7a4045c6..1de85f5048e2 100644 --- a/src/jsc/error.rs +++ b/src/jsc/error.rs @@ -63,8 +63,6 @@ pub enum Error { #[error(transparent)] Resolver(#[from] bun_resolver::Error), #[error(transparent)] - MakeLibUvOwned(#[from] bun_sys::MakeLibUvOwnedError), - #[error(transparent)] Path(#[from] bun_paths::path_options::Error), #[error(transparent)] Bundler(#[from] bun_bundler::Error), @@ -121,7 +119,6 @@ impl Error { Self::Alloc(_) => "OutOfMemory", Self::Core(e) => e.name(), Self::Resolver(e) => e.name(), - Self::MakeLibUvOwned(e) => <&'static str>::from(e), Self::Path(e) => <&'static str>::from(e), Self::Bundler(e) => e.name(), Self::Watcher(e) => e.name(), diff --git a/src/jsc/event_loop.rs b/src/jsc/event_loop.rs index 15d846420dd6..43d83dc8d397 100644 --- a/src/jsc/event_loop.rs +++ b/src/jsc/event_loop.rs @@ -36,9 +36,7 @@ pub use bun_threading::work_pool::{Task as WorkPoolTask, WorkPool}; pub use crate::cpp_task::{ConcurrentCppTask, CppTask}; pub use crate::garbage_collection_controller::GarbageCollectionController; pub use crate::jsc_scheduler as JSCScheduler; -#[cfg(unix)] -pub use crate::posix_signal_handle::PosixSignalHandle; -pub use crate::posix_signal_handle::PosixSignalTask; +pub use crate::posix_signal_handle::{PosixSignalHandle, PosixSignalTask}; bun_core::declare_scope!(EventLoop, hidden); @@ -83,9 +81,6 @@ pub struct EventLoop { pub virtual_machine: Option>, pub waker: Option, // see `hold_forever_poll` - #[cfg(windows)] - pub forever_timer: Option>, - #[cfg(not(windows))] pub holds_forever_poll: bool, pub deferred_tasks: DeferredTaskQueue::DeferredTaskQueue, /// The uws loop this `EventLoop` runs on: the process loop for the VM's @@ -103,7 +98,6 @@ pub struct EventLoop { /// (link-time `__bun_run_wtf_timer`) casts it back. pub imminent_gc_timer: AtomicPtr<()>, - #[cfg(unix)] /// Boxed `PosixSignalHandle` ring buffer, leaked once by /// `Bun__ensureSignalHandler` and live for the process lifetime. Stored as /// a [`bun_ptr::BackRef`] so the per-tick `drain()` / signal-context @@ -125,16 +119,12 @@ impl Default for EventLoop { global: None, virtual_machine: None, waker: None, - #[cfg(windows)] - forever_timer: None, - #[cfg(not(windows))] holds_forever_poll: false, deferred_tasks: DeferredTaskQueue::DeferredTaskQueue::default(), uws_loop: None, entered_event_loop_count: 0, concurrent_ref: AtomicI32::new(0), imminent_gc_timer: AtomicPtr::new(core::ptr::null_mut()), - #[cfg(unix)] signal_handler: None, } } @@ -436,9 +426,6 @@ impl EventLoop { self.deferred_tasks.run(); vm.is_inside_deferred_task_queue.set(false); - // Guard on `event_loop_handle` being set, but drain via `uws_loop_mut()`: - // on Windows the uSockets loop (`uws::Loop::get()`) is NOT - // `event_loop_handle` (which is the libuv loop). if vm.event_loop_handle.is_some() { vm.uws_loop_mut().drain_quic_if_necessary(); } @@ -606,15 +593,12 @@ impl EventLoop { pub fn tick_concurrent_with_count(&mut self) -> usize { self.apply_concurrent_ref_delta(); - #[cfg(unix)] - { - if let Some(signal_handler) = self.signal_handler { - // `signal_handler` is a `BackRef` to the leaked process-lifetime - // `PosixSignalHandle` (see field doc); the ring-buffer backing is - // disjoint from `*self`, so the `&PosixSignalHandle` materialised - // by `BackRef::deref` does not alias the `&mut self` passed here. - signal_handler.drain(self); - } + if let Some(signal_handler) = self.signal_handler { + // `signal_handler` is a `BackRef` to the leaked process-lifetime + // `PosixSignalHandle` (see field doc); the ring-buffer backing is + // disjoint from `*self`, so the `&PosixSignalHandle` materialised + // by `BackRef::deref` does not alias the `&mut self` passed here. + signal_handler.drain(self); } self.run_imminent_gc_timer(); @@ -694,34 +678,23 @@ impl EventLoop { /// ports/channels/sockets on a loop that no longer ticks) so the loop is not /// torn down still believing something keeps it alive. /// - /// Targets `self.native_loop()`, never `vm.event_loop_handle`: `Bun.spawnSync` + /// Targets `self.usockets_loop()`, never `vm.event_loop_handle`: `Bun.spawnSync` /// points the latter at its private loop, and a GC inside it still refs /// this loop (FinalizationRegistry, MessagePort). pub(crate) fn apply_concurrent_ref_delta(&self) { let delta = self.concurrent_ref.swap(0, Ordering::SeqCst); - // SAFETY: `native_loop()` is live for this loop's lifetime; JS thread only. - let loop_ = unsafe { &mut *self.native_loop() }; - #[cfg(windows)] - { - if delta > 0 { - loop_.add_active(u32::try_from(delta).expect("int cast")); - } else { - loop_.sub_active(u32::try_from(-delta).expect("int cast")); - } - } - #[cfg(not(windows))] - { - if delta > 0 { - loop_.num_polls += delta; - loop_.active = loop_ - .active - .saturating_add(u32::try_from(delta).expect("int cast")); - } else { - loop_.num_polls -= -delta; - loop_.active = loop_ - .active - .saturating_sub(u32::try_from(-delta).expect("int cast")); - } + // SAFETY: `usockets_loop()` is live for this loop's lifetime; JS thread only. + let loop_ = unsafe { &mut *self.usockets_loop() }; + if delta > 0 { + loop_.num_polls += delta; + loop_.active = loop_ + .active + .saturating_add(u32::try_from(delta).expect("int cast")); + } else { + loop_.num_polls -= -delta; + loop_.active = loop_ + .active + .saturating_sub(u32::try_from(-delta).expect("int cast")); } } @@ -732,19 +705,6 @@ impl EventLoop { .as_ptr() } - /// [`usockets_loop`](Self::usockets_loop) as the platform-native loop - /// (`us_loop_t*` on POSIX, its `uv_loop_t*` on Windows). - #[inline] - pub fn native_loop(&self) -> *mut crate::PlatformEventLoop { - Async::uws_to_native(self.usockets_loop()) - } - - #[cfg(windows)] - #[inline] - pub fn uv_loop(&self) -> *mut crate::PlatformEventLoop { - self.native_loop() - } - #[inline] pub fn process_gc_timer(&mut self) { self.vm_ref().as_mut().gc_controller.process_gc_timer(); @@ -1064,7 +1024,6 @@ impl EventLoop { if self.uws_loop.is_none() { // The VM's embedded loops run on the thread's loop, the one // `vm.event_loop_handle` names below. - debug_assert_eq!(Async::uws_to_native(uws::Loop::get()), Async::Loop::get()); self.uws_loop = NonNull::new(uws::Loop::get()); } if self.vm_ref().event_loop_handle.is_none() { @@ -1228,8 +1187,8 @@ impl EventLoop { pub unsafe fn tick_while_paused(&mut self, done: *const bool) { // SAFETY: see fn contract — `done` is a live FFI bool written by C++. while !unsafe { done.read_volatile() } { - // SAFETY: `native_loop()` is live for this loop's lifetime; JS thread. - unsafe { (*self.native_loop()).tick() }; + // SAFETY: `usockets_loop()` is live for this loop's lifetime; JS thread. + unsafe { (*self.usockets_loop()).tick() }; } } @@ -1256,7 +1215,6 @@ impl EventLoop { /// Keep one poll registered with the loop so `us_loop_run_bun_tick` parks /// instead of returning immediately on `num_polls == 0`. - #[cfg(not(windows))] fn hold_forever_poll(&mut self, loop_: &mut uws::Loop) { if !self.holds_forever_poll { loop_.inc(); @@ -1264,37 +1222,14 @@ impl EventLoop { } } - #[cfg(windows)] - fn hold_forever_poll(&mut self, loop_: &mut uws::Loop) { - if self.forever_timer.is_none() { - let mut t = uws::Timer::create( - loop_, - std::ptr::from_mut::(self).cast::(), - ); - // SAFETY: t is a fresh non-null timer handle - unsafe { - t.as_mut().set( - std::ptr::from_mut::(self).cast::(), - Some(noop_forever_timer), - 1000 * 60 * 4, - 1000 * 60 * 4, - ) - }; - self.forever_timer = Some(t); - } - } - pub fn tick_possibly_forever(&mut self) { let loop_ptr = self.usockets_loop(); - #[cfg(unix)] - { - let pending_unref = self.vm_ref().take_pending_unref(); - if pending_unref > 0 { - // SAFETY: usockets_loop() returns a live uws loop for the VM - // lifetime; borrow scoped to this call. - unsafe { (*loop_ptr).unref_count(pending_unref) }; - } + let pending_unref = self.vm_ref().take_pending_unref(); + if pending_unref > 0 { + // SAFETY: usockets_loop() returns a live uws loop for the VM + // lifetime; borrow scoped to this call. + unsafe { (*loop_ptr).unref_count(pending_unref) }; } // SAFETY: as above. @@ -1305,16 +1240,11 @@ impl EventLoop { self.process_gc_timer(); // `tick()` below can start work (e.g. a --hot reload) whose only wake - // source is a cross-thread `wakeup()`; bound the park, same as the GC - // timerfd used to. libuv's `tick_with_timeout` ignores the argument. + // source is a cross-thread `wakeup()`; bound the park. + let timeout = Some(&bun_core::Timespec { sec: 1, nsec: 0 }); // SAFETY: as above — the tick runs loop callbacks that reach the loop // themselves, so the exclusive borrow is scoped to this call only. - unsafe { - (*loop_ptr).tick_with_timeout( - Some(&bun_core::Timespec { sec: 1, nsec: 0 }), - uws::NOW_NS_UNKNOWN, - ) - }; + unsafe { (*loop_ptr).tick_with_timeout(timeout, uws::NOW_NS_UNKNOWN) }; self.vm_ref().as_mut().on_after_event_loop(); self.tick_concurrent(); @@ -1399,12 +1329,6 @@ pub fn get_active_tasks(global_object: &JSGlobalObject, _frame: &CallFrame) -> J b"concurrentRef", JSValue::js_number(event_loop.concurrent_ref.load(Ordering::SeqCst) as f64), ); - #[cfg(windows)] - // SAFETY: `Loop::get()` returns the live process-global `uv_loop_t`. - let num_polls: i32 = - i32::try_from(unsafe { (*bun_sys::windows::libuv::Loop::get()).active_handles }) - .expect("int cast"); - #[cfg(not(windows))] // SAFETY: uws::Loop::get() returns a live process-global loop. let num_polls: i32 = unsafe { (*uws::Loop::get()).num_polls }; result.put( @@ -1421,11 +1345,6 @@ pub fn get_active_tasks(global_object: &JSGlobalObject, _frame: &CallFrame) -> J Ok(result) } -#[cfg(windows)] -extern "C" fn noop_forever_timer(_: *mut uws::Timer) { - // do nothing -} - // HOST_EXPORT(Bun__EventLoop__runCallback2, c) pub fn event_loop_run_callback2( global: &JSGlobalObject, diff --git a/src/jsc/job.rs b/src/jsc/job.rs index 362059249164..3e205cab6da7 100644 --- a/src/jsc/job.rs +++ b/src/jsc/job.rs @@ -21,7 +21,10 @@ use core::marker::PhantomData; use core::mem::ManuallyDrop; use core::ptr::NonNull; +use core::sync::atomic::{AtomicPtr, Ordering}; +use bun_collections::HashMap; +use bun_core::Fd; use bun_io::KeepAlive; use bun_threading::work_pool::{Task as WorkPoolTask, WorkPool}; @@ -206,9 +209,32 @@ pub trait JobContext: Sized + 'static { /// can wait on something external. Only such jobs are tracked by the VM. const CANCELLABLE: bool = false; + /// [`run`](Self::run) gives back something of the process's, which is to + /// happen also when the VM that asked for it is on its way out. + const RUNS_CANCELLED: bool = false; + + /// [`run`](Self::run) writes what script takes for written when the call + /// that made the job returns: `process.exit()` waits for it + /// ([`WorkPool::owe_write`]), and if it [`waits`](Self::waits), not for long + /// ([`WorkPool::write_held_up`]). + const OWED_AT_EXIT: bool = false; + + /// [`waits`](Self::waits) is true whatever the job was given: nothing has + /// to be asked, and it goes to those threads directly. + const ALWAYS_WAITS: bool = false; + + /// Pool thread, before [`run`](Self::run): whether that may wait, for as + /// long as it takes, on something outside the process. It then runs on + /// [`WorkPool::schedule_wait`]'s threads. + fn waits(off: &Self::OffThread) -> bool { + let _ = off; + false + } + /// Pool thread, VM not yet in its final wait when the pool reached the job /// (a job reached later is handed back unrun, as Node's environment - /// cleanup `uv_cancel`s queued work). Return `done` to complete now; keep + /// cleanup `uv_cancel`s queued work, unless it + /// [`RUNS_CANCELLED`](Self::RUNS_CANCELLED)). Return `done` to complete now; keep /// it (e.g. across async I/O that finishes on another thread) and call /// [`Completion::finish`] later to complete then. `done.ticket()` is the /// job's ticket: proof the VM is alive (for [`JsPtr::under_ticket`]), and @@ -217,6 +243,19 @@ pub trait JobContext: Sized + 'static { /// [`Completion::finish`] queues it: do not touch it after finishing. fn run(off: &mut Self::OffThread, done: Completion) -> Option>; + /// JS thread, in place of [`run`](Self::run): the descriptor the job was + /// given ([`Job::schedule_on_fd`]) is closed as far as it goes. What `run` + /// would have found, without going near the number. + fn closed(off: &mut Self::OffThread) { + let _ = off; + unreachable!("not a job on a descriptor"); + } + + /// JS thread: the job is back, whether or not [`then`](Self::then) follows. + fn back(off: &Self::OffThread, vm: &VirtualMachine) { + let _ = (off, vm); + } + /// JS thread, VM still running script: the completion. Both halves are /// handed over to use and drop normally. fn then(off: Self::OffThread, js: Self::Js, cx: &JsThread<'_>) -> JsResult<()>; @@ -312,6 +351,218 @@ impl JobList { } } +/// What a job does with a file descriptor that script handed it. +#[derive(Clone, Copy)] +pub enum FdUse { + Uses(Fd), + /// Writes at the descriptor's position, so what it writes is to come + /// after what the jobs of this kind that were given the descriptor before + /// it write: it goes to the pool when the last of them is done. + Appends(Fd), + /// Goes to the pool once every job that was given the descriptor before it + /// is back. The pool starts jobs in no particular order, and a number that + /// is closed early is the next file's: what was still to be written would + /// land there. + Closes(Fd), +} + +impl FdUse { + pub fn fd(self) -> Fd { + match self { + Self::Uses(fd) | Self::Appends(fd) | Self::Closes(fd) => fd, + } + } + + /// Whether jobs are out on the descriptor: what the JS thread did to it + /// now would be ahead of them. + pub fn is_behind_jobs(self, vm: &VirtualMachine) -> bool { + vm.fd_jobs + .with_mut(|jobs| jobs.lines.contains_key(&self.fd())) + } + + /// The JS thread is about to do this itself, at once. A close does not + /// wait for what is out on the descriptor, which then is not counted + /// against the next file to get the number. + pub fn on_js_thread(self, vm: &VirtualMachine) { + if let Self::Closes(fd) = self { + vm.fd_jobs.with_mut(|jobs| jobs.end(fd)); + } + } +} + +/// Where a job that uses a descriptor is counted. +#[derive(Clone, Copy)] +struct FdPlace { + fd: Fd, + line: u64, +} + +/// The jobs that are out on a descriptor. +struct FdLine { + /// Tells it from the lines the number had before. + id: u64, + /// Handed to the pool and not back yet; never 0. + running: u32, + /// Goes to the pool when they are back. + close: Option<*mut WorkPoolTask>, + /// [`Job::next_append`] of the last [`FdUse::Appends`] job, while it is out. + last_append: *const AtomicPtr, +} + +/// In [`Job::next_append`]: the job is done, and nothing can be put behind it. +const APPENDED: *mut WorkPoolTask = core::ptr::without_provenance_mut(1); + +/// What is to become of a job that was given a descriptor. +enum Entered { + /// The pool's now. + Goes(Option), + /// Behind the job whose [`Job::next_append`] this is: that one gives it to + /// the pool when it is done, if it is told of it before then. + Follows(FdPlace, *const AtomicPtr), + /// A close: the pool's when the jobs of this line are back. + Waits(u64), + /// Given the descriptor behind a close that is waiting. The number is + /// still open, so it is not another file's yet: this is the descriptor + /// that is being closed. + Closed, +} + +/// A VM's live jobs on file descriptors (JS thread only, and without a system +/// call: one on a network file system can take as long as the server likes). +/// +/// Once a close is the pool's, its number can be the next file's at any moment, +/// and nothing that is given the number is held up or turned away. +#[derive(Default)] +pub struct FdJobs { + lines: HashMap, + ids: u64, +} + +impl FdJobs { + /// `next_append` is the job's own. + fn enter( + &mut self, + task: *mut WorkPoolTask, + next_append: *const AtomicPtr, + fd_use: FdUse, + ) -> Entered { + let fd = fd_use.fd(); + if matches!(fd_use, FdUse::Closes(_)) { + return match self.lines.get_mut(&fd) { + None => Entered::Goes(None), + Some(FdLine { close: Some(_), .. }) => Entered::Closed, + Some(line) => { + line.close = Some(task); + Entered::Waits(line.id) + } + }; + } + let line = self.lines.entry(fd).or_insert_with(|| { + self.ids += 1; + FdLine { + id: self.ids, + running: 0, + close: None, + last_append: core::ptr::null(), + } + }); + if line.close.is_some() { + return Entered::Closed; + } + line.running += 1; + let place = FdPlace { fd, line: line.id }; + if matches!(fd_use, FdUse::Appends(_)) { + let last = core::mem::replace(&mut line.last_append, next_append); + if !last.is_null() { + return Entered::Follows(place, last); + } + } + Entered::Goes(Some(place)) + } + + /// A job that was counted is back from the pool. + fn leave(&mut self, place: FdPlace, next_append: *const AtomicPtr) { + // A close went ahead of it otherwise. + if let Some(line) = self.lines.get_mut(&place.fd) + && line.id == place.line + { + if line.last_append == next_append { + line.last_append = core::ptr::null(); + } + line.running -= 1; + if line.running == 0 { + self.end(place.fd); + } + } + } + + /// The close that waits for the jobs of `line` goes ahead of them. They + /// are not counted any more: they can outlive the descriptor, into the life + /// of the next file to get the number. + fn stop_waiting(&mut self, place: FdPlace) { + if self + .lines + .get(&place.fd) + .is_some_and(|line| line.id == place.line) + { + self.end(place.fd); + } + } + + fn end(&mut self, fd: Fd) { + if let Some(FdLine { + close: Some(task), .. + }) = self.lines.remove(&fd) + { + WorkPool::schedule(task); + } + } +} + +#[cfg(windows)] +fn is_on_disk(fd: Fd) -> bool { + bun_sys::windows::fs::is_disk_file(fd) +} + +#[cfg(unix)] +fn is_on_disk(fd: Fd) -> bool { + bun_sys::fstat(fd).is_ok_and(|stat| { + matches!( + bun_sys::kind_from_mode(stat.st_mode as _), + bun_sys::FileKind::File | bun_sys::FileKind::Directory + ) + }) +} + +/// Whether a close has to wait for the jobs that are out on its descriptor. +/// Those on a file or a directory come back by themselves. On a pipe, a +/// console or a device one can be out for as long as the other end likes. +enum FdKind {} + +struct FdKindQuery { + place: FdPlace, + on_disk: bool, +} + +impl JobContext for FdKind { + type OffThread = FdKindQuery; + type Js = (); + fn run(query: &mut FdKindQuery, done: Completion) -> Option> { + query.on_disk = is_on_disk(query.place.fd); + Some(done) + } + /// Not `then`: the close is on its way also when the script that asked for + /// it is not there to hear of it any more. + fn back(query: &FdKindQuery, vm: &VirtualMachine) { + if !query.on_disk { + vm.fd_jobs.with_mut(|jobs| jobs.stop_waiting(query.place)); + } + } + fn then(_: FdKindQuery, _: (), _: &JsThread<'_>) -> JsResult<()> { + Ok(()) + } +} + /// One pool-then-complete job. Heap-allocated by [`Job::schedule`]; freed on /// the JS thread by its completion or by the teardown's release. #[repr(C)] @@ -324,6 +575,15 @@ pub struct Job { ticket: Option, task: WorkPoolTask, keep_alive: KeepAlive, + /// Its places in the VM's [`FdJobs`]. + places: [Option; 2], + /// Counted by [`WorkPool::owe_write`]. The pool's with the job. + owed: bool, + /// Counted by [`WorkPool::write_held_up`] instead. + held_up: bool, + /// The [`FdUse::Appends`] job that goes to the pool when this one is done, + /// or [`APPENDED`]. + next_append: AtomicPtr, off: C::OffThread, js: C::Js, } @@ -346,6 +606,26 @@ impl Job { /// JS thread: build the job, keep the loop alive for it, hand it to the pool. #[track_caller] pub fn schedule(cx: &JsThread<'_>, off: C::OffThread, js: C::Js) { + Self::schedule_on_fds(cx, off, js, [None, None]); + } + + /// [`schedule`](Self::schedule) for a job that may be working on a file + /// descriptor of script's. + #[track_caller] + pub fn schedule_on_fd(cx: &JsThread<'_>, off: C::OffThread, js: C::Js, fd_use: Option) { + Self::schedule_on_fds(cx, off, js, [fd_use, None]); + } + + /// [`schedule_on_fd`](Self::schedule_on_fd) for one that may be working on + /// two. A close has no other, and [`FdUse::Appends`] comes last: a job + /// that was put behind another is that one's to start. + #[track_caller] + pub fn schedule_on_fds( + cx: &JsThread<'_>, + off: C::OffThread, + js: C::Js, + fd_uses: [Option; 2], + ) { let mut keep_alive = KeepAlive::default(); keep_alive.ref_(bun_io::js_vm_ctx()); let job = bun_core::heap::into_raw(Box::new(Self { @@ -367,22 +647,124 @@ impl Job { ticket: Some(cx.vm().ticket()), task: WorkPoolTask { node: Default::default(), - callback: Self::run_on_pool, + callback: if C::ALWAYS_WAITS { + Self::run + } else { + Self::run_on_pool + }, }, keep_alive, + places: [None, None], + owed: false, + held_up: false, + next_append: AtomicPtr::new(core::ptr::null_mut()), off, js, })); - // SAFETY: live until completed/released on this thread; the pool owns it now. + // SAFETY: live until completed/released on this thread; the pool's from + // here, or from its turn on the descriptor. unsafe { if C::CANCELLABLE { cx.vm().jobs.with_mut(|j| j.push(&raw mut (*job).header)); } - WorkPool::schedule(&raw mut (*job).task); + let task = &raw mut (*job).task; + let next_append = &raw const (*job).next_append; + let mut follows = core::ptr::null(); + for (index, fd_use) in fd_uses.into_iter().enumerate() { + let Some(fd_use) = fd_use else { continue }; + let entered = cx + .vm() + .fd_jobs + .with_mut(|jobs| jobs.enter(task, next_append, fd_use)); + match entered { + Entered::Goes(place) => (*job).places[index] = place, + Entered::Follows(place, last) => { + (*job).places[index] = Some(place); + follows = last; + } + Entered::Waits(line) => { + let place = FdPlace { + fd: fd_use.fd(), + line, + }; + let query = FdKindQuery { + place, + on_disk: true, + }; + return Job::::schedule(cx, query, ()); + } + Entered::Closed => { + C::closed(&mut (*job).off); + return Completion { + job: NonNull::new(job).expect("job"), + ticket: (*job).ticket.take().expect("job"), + } + .finish(); + } + } + } + if C::ALWAYS_WAITS { + return WorkPool::schedule_wait(task); + } + // Last: told of it, the job ahead can have given it to the pool, and + // the pool can be done with it, before this returns. That job takes + // itself out of `last_append` on this thread before it is freed. + if let Some(last) = follows.as_ref() { + (*task).callback = Self::run_handed_over; + if last + .compare_exchange( + core::ptr::null_mut(), + task, + Ordering::AcqRel, + Ordering::Acquire, + ) + .is_ok() + { + return; + } + (*task).callback = Self::run_on_pool; + } + if C::OWED_AT_EXIT { + (*job).owed = true; + WorkPool::owe_write(); + } + WorkPool::schedule(task); } } + /// The pool's from the job ahead of it, which counted it + /// ([`Completion::finish`]). Not counted while it was behind that one, + /// which can be a write to a pipe that nobody reads. + fn run_handed_over(task: *mut WorkPoolTask) { + #[cfg(windows)] + if C::OWED_AT_EXIT { + // SAFETY: as in `run`. + unsafe { (*bun_core::from_field_ptr!(Self, task, task)).owed = true }; + } else { + WorkPool::write_settled(); + } + Self::run_on_pool(task); + } + fn run_on_pool(task: *mut WorkPoolTask) { + // SAFETY: as in `run`; the job is exclusively the pool's for this callback. + unsafe { + let this: *mut Self = bun_core::from_field_ptr!(Self, task, task); + // A job that `run` hands back unrun has nothing to ask about. + let cancelled = (*this).ticket.as_ref().expect("job").cancelled(); + if !cancelled && C::waits(&(*this).off) { + if core::mem::take(&mut (*this).owed) { + (*this).held_up = true; + WorkPool::write_held_up(); + } + (*task).callback = Self::run; + return WorkPool::schedule_wait(task); + } + } + Self::run(task); + } + + fn run(task: *mut WorkPoolTask) { // SAFETY: only reachable through the `task.callback` slot wired in // `schedule`; the pool calls back with exactly that field of a live job. let this: *mut Self = unsafe { bun_core::from_field_ptr!(Self, task, task) }; @@ -393,7 +775,7 @@ impl Job { job: NonNull::new(this).expect("job"), ticket, }; - if done.ticket().cancelled() { + if !C::RUNS_CANCELLED && done.ticket().cancelled() { return done.finish(); } if let Some(done) = C::run(off, done) { @@ -412,13 +794,20 @@ impl Job { vm.jobs .with_mut(|j| j.unlink(unsafe { &raw mut (*this).header })); } + // SAFETY: fn contract. Compared below, not followed. + let next_append = unsafe { &raw const (*this).next_append }; // SAFETY: fn contract. let Job { mut keep_alive, + places, off, js, .. } = unsafe { *Box::from_raw(this) }; + for place in places.into_iter().flatten() { + vm.fd_jobs.with_mut(|jobs| jobs.leave(place, next_append)); + } + C::back(&off, vm); keep_alive.unref(bun_io::js_vm_ctx()); (off, js) } @@ -451,12 +840,42 @@ impl Completion { pub fn finish(self) { // Consumed: the obligation is met here, so its Drop check must not run. let me = ManuallyDrop::new(self); + // SAFETY: the job is this thread's until it is posted below. + let job = unsafe { &mut *me.job.as_ptr() }; + let next = job.next_append.swap(APPENDED, Ordering::AcqRel); + if !next.is_null() { + // Before this one is settled: an exit that waits for it goes on to + // wait for what was written behind it. + #[cfg(windows)] + WorkPool::owe_write(); + WorkPool::schedule(next); + } + if C::OWED_AT_EXIT && core::mem::take(&mut job.owed) { + WorkPool::write_settled(); + } + if C::OWED_AT_EXIT && core::mem::take(&mut job.held_up) { + WorkPool::held_up_write_settled(); + } // SAFETY: moving the field out of a value that is never dropped. let ticket = unsafe { core::ptr::read(&raw const me.ticket) }; ticket.post(bun_event_loop::ConcurrentTask::ConcurrentTask::create_from( me.job.as_ptr(), )); } + /// The job found that it may wait on something outside the process, which + /// [`JobContext::waits`] could not tell: an exit does not wait long for it + /// ([`JobContext::OWED_AT_EXIT`]). + pub fn held_up_at_exit(&self) { + // SAFETY: the job is the holder's until it is finished. + unsafe { + let job = self.job.as_ptr(); + if core::mem::take(&mut (*job).owed) { + (*job).held_up = true; + WorkPool::write_held_up(); + } + } + } + /// The job's ticket: its VM is alive while this is held. #[inline] pub fn ticket(&self) -> &Ticket { diff --git a/src/jsc/lib.rs b/src/jsc/lib.rs index 7c9a1c4dd872..561a95f5a720 100644 --- a/src/jsc/lib.rs +++ b/src/jsc/lib.rs @@ -1243,11 +1243,7 @@ pub use self::event_loop::{ DeferredTaskQueue, EventLoopHandle, EventLoopTask, GarbageCollectionController, MiniEventLoop, PosixSignalTask, Stopped, Task, WorkPool, WorkPoolTask, }; -pub use self::job::{Completion, Job, JobContext, JsPtr, JsThread, Protected}; -#[cfg(unix)] -pub type PlatformEventLoop = bun_uws::Loop; -#[cfg(not(unix))] -pub type PlatformEventLoop = bun_io::Loop; +pub use self::job::{Completion, FdUse, Job, JobContext, JsPtr, JsThread, Protected}; pub use self::array_buffer::JSTypedArrayBytesDeallocator; diff --git a/src/jsc/modules/NodeConstantsModule.h b/src/jsc/modules/NodeConstantsModule.h index b9cf38a3416b..81ca4c64aaaf 100644 --- a/src/jsc/modules/NodeConstantsModule.h +++ b/src/jsc/modules/NodeConstantsModule.h @@ -35,21 +35,7 @@ #endif #if defined(_WIN32) -#include // _S_IREAD _S_IWRITE -#ifndef S_IRUSR -#define S_IRUSR _S_IREAD -#endif // S_IRUSR -#ifndef S_IWUSR -#define S_IWUSR _S_IWRITE -#endif // S_IWUSR -// The UCRT only defines the underscore-prefixed _S_IFIFO; whether the plain -// spelling is visible here otherwise depends on what happened to be defined -// earlier in the unified source. Node exposes constants.S_IFIFO (4096) on -// Windows, so pin it to the CRT value instead of relying on header luck. -// Keep in sync with ProcessBindingConstants.cpp. -#if !defined(S_IFIFO) && defined(_S_IFIFO) -#define S_IFIFO _S_IFIFO -#endif // S_IFIFO +#include "BunWindowsPosixConstants.h" #else #include #endif diff --git a/src/jsc/modules/NodeTTYModule.cpp b/src/jsc/modules/NodeTTYModule.cpp index 9cc7b04803b2..2d7832bfd2c4 100644 --- a/src/jsc/modules/NodeTTYModule.cpp +++ b/src/jsc/modules/NodeTTYModule.cpp @@ -2,6 +2,13 @@ #include "NodeTTYModule.h" +#if OS(WINDOWS) +#include + +// `_get_osfhandle`; INVALID_HANDLE_VALUE for an fd that has no HANDLE. +extern "C" void* Bun__crtGetOsfhandle(int fd); +#endif + using namespace JSC; namespace Zig { @@ -20,13 +27,12 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionTty_isatty, (JSGlobalObject * globalObject, C #if !OS(WINDOWS) bool isTTY = isatty(fd); #else + // A character device that is not a console (NUL, a serial port) is not a tty. bool isTTY = false; - switch (uv_guess_handle(fd)) { - case UV_TTY: - isTTY = true; - break; - default: - break; + if (fd >= 0) { + HANDLE handle = Bun__crtGetOsfhandle(fd); + DWORD mode; + isTTY = GetFileType(handle) == FILE_TYPE_CHAR && GetConsoleMode(handle, &mode); } #endif diff --git a/src/jsc/modules/NodeTTYModule.h b/src/jsc/modules/NodeTTYModule.h index b29a30bb4a66..ed0dc3935e9b 100644 --- a/src/jsc/modules/NodeTTYModule.h +++ b/src/jsc/modules/NodeTTYModule.h @@ -3,10 +3,6 @@ #include "JSBuffer.h" #include "_NativeModule.h" -#if OS(WINDOWS) -#include -#endif - namespace Zig { using namespace WebCore; diff --git a/src/jsc/rare_data.rs b/src/jsc/rare_data.rs index 9243d3a5d913..d29f570a372f 100644 --- a/src/jsc/rare_data.rs +++ b/src/jsc/rare_data.rs @@ -958,7 +958,7 @@ impl RareData { pub fn stderr(&mut self) -> *mut c_void { bun_analytics::features::bun_stderr.fetch_add(1, Ordering::Relaxed); if self.stderr_store.is_none() { - let fd = Fd::from_uv(2); + let fd = Fd::from_crt(2); let mode: Mode = match syscall::fstat(fd) { Ok(stat) => stat.st_mode as Mode, Err(_) => 0, @@ -977,7 +977,7 @@ impl RareData { pub fn stdout(&mut self) -> *mut c_void { bun_analytics::features::bun_stdout.fetch_add(1, Ordering::Relaxed); if self.stdout_store.is_none() { - let fd = Fd::from_uv(1); + let fd = Fd::from_crt(1); let mode: Mode = match syscall::fstat(fd) { Ok(stat) => stat.st_mode as Mode, Err(_) => 0, @@ -996,7 +996,7 @@ impl RareData { pub fn stdin(&mut self) -> *mut c_void { bun_analytics::features::bun_stdin.fetch_add(1, Ordering::Relaxed); if self.stdin_store.is_none() { - let fd = Fd::from_uv(0); + let fd = Fd::from_crt(0); let mode: Mode = match syscall::fstat(fd) { Ok(stat) => stat.st_mode as Mode, Err(_) => 0, diff --git a/src/jsc/virtual_machine_exports.rs b/src/jsc/virtual_machine_exports.rs index 15e371b99ce9..9de3ad8c9489 100644 --- a/src/jsc/virtual_machine_exports.rs +++ b/src/jsc/virtual_machine_exports.rs @@ -10,9 +10,6 @@ use bun_core::String as BunString; use bun_sourcemap::SourceProviderMap; use bun_sourcemap::parsed_source_map::AnySourceProvider; -// `Bun__ZigGlobalObject__uvLoop` is Windows-only: `#[cfg(windows)]` on the fn -// definition itself. -// // `#[unsafe(no_mangle)] extern "C"` thunks for everything below are emitted by // `src/codegen/generate-host-exports.ts` from the `// HOST_EXPORT(Sym, c)` // markers; the bodies here take safe `&VirtualMachine` / `&JSGlobalObject` / @@ -195,12 +192,6 @@ pub fn on_did_append_plugin(jsc_vm: &mut VirtualMachine, global: &JSGlobalObject jsc_vm.transpiler.linker.plugin_runner = Some(std::ptr::from_mut::(runner)); } -#[cfg(windows)] -#[unsafe(no_mangle)] -extern "C" fn Bun__ZigGlobalObject__uvLoop(jsc_vm: &mut VirtualMachine) -> *mut c_void { - jsc_vm.uv_loop().cast() -} - // HOST_EXPORT(Bun__setTLSRejectUnauthorizedValue, c) pub fn set_tls_reject_unauthorized_value(value: i32) { // SAFETY: VM singleton is process-lifetime. diff --git a/src/jsc/webcore_types.rs b/src/jsc/webcore_types.rs index 591d3e8e2238..9320a3fb851e 100644 --- a/src/jsc/webcore_types.rs +++ b/src/jsc/webcore_types.rs @@ -660,45 +660,6 @@ pub mod store { } } - /// Takes ownership of a `Box<[u8]>` (global allocator, `cap == len`). - /// Paired with [`Bytes::into_boxed_slice`] for round-tripping the - /// `is_temporary` handoff in `read_file`. - pub fn init_owned(bytes: Box<[u8]>) -> Bytes { - let len = bytes.len(); - let ptr = bun_core::heap::into_raw(bytes).cast::(); - Bytes { - ptr: NonNull::new(ptr), - len: len as SizeType, - cap: len as SizeType, - allocator: bun_alloc::basic::C_ALLOCATOR, - stored_name: Box::default(), - } - } - - /// Reclaim the buffer as a `Box<[u8]>`, shrinking if `cap > len`. - /// - /// Only valid for global-allocator-backed storage (the `init`/ - /// `init_owned` paths) — asserts on a custom allocator (mmap/memfd). - pub fn into_boxed_slice(self) -> Box<[u8]> { - let mut this = core::mem::ManuallyDrop::new(self); - // `ManuallyDrop` suppresses the `Drop` impl, so free `stored_name` - // here explicitly (the buffer itself is reclaimed below). - drop(core::mem::take(&mut this.stored_name)); - let Some(ptr) = this.ptr else { - return Box::new([]); - }; - debug_assert!( - core::ptr::eq(this.allocator.vtable, bun_alloc::basic::C_ALLOCATOR.vtable), - "Bytes::into_boxed_slice on non-global allocator", - ); - // SAFETY: `ptr[..cap]` is the live global-allocator allocation - // recorded by `init`/`init_owned`; `len <= cap`. `Vec::from_raw_parts` - // reconstitutes ownership, `into_boxed_slice` reallocates iff - // `cap > len` so the result has the canonical `Box<[u8]>` layout. - unsafe { Vec::from_raw_parts(ptr.as_ptr(), this.len as usize, this.cap as usize) } - .into_boxed_slice() - } - /// Construct from a raw `(ptr, len, cap)` triple owned by `allocator`. /// /// # Safety diff --git a/src/libarchive/error.rs b/src/libarchive/error.rs index f91a5c69c153..9ee82d7591f0 100644 --- a/src/libarchive/error.rs +++ b/src/libarchive/error.rs @@ -7,8 +7,6 @@ pub enum Error { #[error(transparent)] Alloc(#[from] bun_alloc::AllocError), #[error(transparent)] - MakeLibUvOwned(#[from] bun_sys::MakeLibUvOwnedError), - #[error(transparent)] Paths(#[from] bun_paths::Error), } @@ -18,7 +16,6 @@ impl Error { Self::Fail => "Fail", Self::Sys(e) => <&'static str>::from(e), Self::Alloc(_) => "OutOfMemory", - Self::MakeLibUvOwned(e) => <&'static str>::from(e), Self::Paths(e) => e.name(), } } diff --git a/src/libarchive/lib.rs b/src/libarchive/lib.rs index 4125c91f1399..9b5f59620f6b 100644 --- a/src/libarchive/lib.rs +++ b/src/libarchive/lib.rs @@ -1689,7 +1689,7 @@ impl Archiver { let flags = bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::TRUNC; #[cfg(windows)] - let file_handle_native: Fd = + let file_handle: Fd = match bun_sys::openat_windows(dir_fd, path_slice, flags, 0) { Ok(fd) => fd, Err(e) => match e.get_errno() { @@ -1709,7 +1709,7 @@ impl Archiver { }; #[cfg(not(windows))] - let file_handle_native: Fd = { + let file_handle: Fd = { // dir.createFileZ(.{truncate, mode}) → bun_sys::openat // SAFETY: normalized_buf[path_slice.len()] == 0 (written above). let path_z: &ZStr = unsafe { @@ -1733,16 +1733,6 @@ impl Archiver { } }; - let file_handle: Fd = { - // errdefer file_handle_native.close() - let guard = scopeguard::guard(file_handle_native, |fd| { - fd.close(); - }); - let owned = (*guard).make_lib_uv_owned()?; - scopeguard::ScopeGuard::into_inner(guard); - owned - }; - // reshaped for borrowck — `plucked_file` is captured by // the guard tuple; mutate via close_guard.1. let mut close_guard = diff --git a/src/libuv_sys/Cargo.toml b/src/libuv_sys/Cargo.toml deleted file mode 100644 index 7ed649605ed6..000000000000 --- a/src/libuv_sys/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "bun_libuv_sys" -version.workspace = true -edition.workspace = true - -[lib] -path = "lib.rs" - -[lints] -workspace = true - -[dependencies] -strum.workspace = true -libc.workspace = true - -[target.'cfg(windows)'.dependencies] -bun_windows_sys.workspace = true -bun_wyhash.workspace = true -bun_core.workspace = true diff --git a/src/libuv_sys/lib.rs b/src/libuv_sys/lib.rs deleted file mode 100644 index 73ed217ff7ba..000000000000 --- a/src/libuv_sys/lib.rs +++ /dev/null @@ -1,46 +0,0 @@ -#![warn(unused_must_use)] -//! Raw libuv FFI (Windows only). Re-exports the `libuv` module's contents at -//! crate root so callers can write `bun_libuv_sys::fs_t` / -//! `bun_sys::windows::libuv::uv_fs_open`. -pub mod libuv; -#[cfg(windows)] -pub use libuv::*; - -// ────────────────────────────────────────────────────────────────────────── -// `uv_dirent_type_t` (uv.h) — ABI constants for `uv_dirent_t::type`. The -// Windows-only `libuv` module above is `#![cfg(windows)]`, but these tag -// values are platform-invariant integers and are consumed cross-platform by -// `node::types::Dirent::to_js` (which surfaces them to JS as -// `process.binding('constants').fs.UV_DIRENT_*`). -// ────────────────────────────────────────────────────────────────────────── -pub const UV_DIRENT_UNKNOWN: core::ffi::c_int = 0; -pub const UV_DIRENT_FILE: core::ffi::c_int = 1; -pub const UV_DIRENT_DIR: core::ffi::c_int = 2; -pub const UV_DIRENT_LINK: core::ffi::c_int = 3; -pub const UV_DIRENT_FIFO: core::ffi::c_int = 4; -pub const UV_DIRENT_SOCKET: core::ffi::c_int = 5; -pub const UV_DIRENT_CHAR: core::ffi::c_int = 6; -pub const UV_DIRENT_BLOCK: core::ffi::c_int = 7; - -// ────────────────────────────────────────────────────────────────────────── -// libuv synthetic errno literals (uv/errno.h `UV__E*` fallbacks). These are -// platform-invariant ABI constants libuv assigns when the host OS lacks a -// native errno for the condition. On Windows the full `UV_E*` table is -// re-exported from `libuv::*` above; on posix we surface only the synthetic -// subset so `bun_errno`'s per-OS `uv_e` modules can reference a single source -// of truth instead of inlining the magic numbers. -// ────────────────────────────────────────────────────────────────────────── -#[cfg(not(windows))] -pub const UV_ECHARSET: core::ffi::c_int = -4080; -#[cfg(not(windows))] -pub const UV_ENONET: core::ffi::c_int = -4056; -#[cfg(not(windows))] -pub const UV_ENOTSUP: core::ffi::c_int = -4049; -#[cfg(not(windows))] -pub const UV_EREMOTEIO: core::ffi::c_int = -4030; -#[cfg(not(windows))] -pub const UV_EFTYPE: core::ffi::c_int = -4028; -#[cfg(not(windows))] -pub const UV_ENODATA: core::ffi::c_int = -4024; -#[cfg(not(windows))] -pub const UV_EUNATCH: core::ffi::c_int = -4023; diff --git a/src/libuv_sys/libuv.rs b/src/libuv_sys/libuv.rs deleted file mode 100644 index 9175e67e2677..000000000000 --- a/src/libuv_sys/libuv.rs +++ /dev/null @@ -1,3132 +0,0 @@ -//! Zero-cost, zero-alloc FFI bindings for libuv on **Windows**. -//! -//! Field-accurate `#[repr(C)]` mirrors of every `uv_*_t` Bun touches, derived -//! from `vendor/libuv/include/uv.h` + `uv/win.h`. All structs are POD (`Copy` where -//! `union`s permit), all-zero is a valid bit-pattern (libuv expects callers to -//! `memset(0)` before `uv_*_init`), and no method allocates. -//! -//! Layouts are layout-asserted at the bottom of this file against the -//! authoritative `sizeof`s from a Windows-x64 build of libuv. -#![cfg(windows)] -#![allow( - non_camel_case_types, - non_snake_case, - non_upper_case_globals, - clippy::missing_safety_doc -)] - -use core::cell::{Cell, UnsafeCell}; -use core::ffi::{c_char, c_int, c_long, c_uint, c_ulong, c_ushort, c_void}; -use core::mem::MaybeUninit; -use core::{fmt, mem, ptr}; - -// ────────────────────────────────────────────────────────────────────────── -// Debug log scope (`bun.Output.scoped(.uv, .hidden)`). This crate is leaf -// (no `bun_output` dep): an `eprintln!` gated by `BUN_DEBUG_uv` in debug, a -// constant-false branch in release — the arguments stay type-checked (and -// count as used) in both, like `bun_core::scoped_log!`. -// ────────────────────────────────────────────────────────────────────────── -#[doc(hidden)] -#[inline] -pub fn __uv_log_enabled() -> bool { - if !cfg!(debug_assertions) { - return false; - } - // `Output.scoped` reads the env var once at startup; `inc/dec` are on the - // per-handle ref/unref hot path, so cache the lookup instead of paying a - // GetEnvironmentVariableW syscall + alloc per tick. - static ENABLED: ::std::sync::OnceLock = ::std::sync::OnceLock::new(); - *ENABLED.get_or_init(|| ::std::env::var_os("BUN_DEBUG_uv").is_some()) -} -#[doc(hidden)] -#[macro_export] -macro_rules! __uv_log { - ($($arg:tt)*) => {{ - if ::core::cfg!(debug_assertions) && $crate::__uv_log_enabled() { - ::std::eprintln!("[uv] {}", ::std::format_args!($($arg)*)); - } - }}; -} -/// `bun.windows.libuv.log` — re-exported under the conventional name. -pub use crate::__uv_log as log; - -// ────────────────────────────────────────────────────────────────────────── -// Win32 ABI typedefs. Shared POD structs/typedefs come from the tier-0 -// `bun_windows_sys` leaf so the same nominal types flow through libuv, -// `bun_sys`, and the runtime without cross-crate mismatch. -// ────────────────────────────────────────────────────────────────────────── -pub use bun_windows_sys::{ - BOOL, COORD, CRITICAL_SECTION, DWORD, HANDLE, INPUT_RECORD, INVALID_HANDLE_VALUE, - LARGE_INTEGER, LONG, OVERLAPPED, SHORT, ULONG, ULONG_PTR, WCHAR, WORD, -}; -// Kept local — NOT re-exported from `bun_windows_sys`: -// libuv wants plain i32, `bun_windows_sys::NTSTATUS` is a newtype. -pub type NTSTATUS = i32; -/// Win32 `SOCKET` is `UINT_PTR` (an integer), not a pointer. A raw-pointer type would give -/// `Option` an unwanted niche (None ↔ 0 collides with socket 0) and -/// force int-to-ptr provenance for `INVALID_SOCKET`. -pub type SOCKET = usize; -type LPFN_ACCEPTEX = *const c_void; -type LPFN_CONNECTEX = *const c_void; -type LPFN_WSARECV = *const c_void; -type LPFN_WSARECVFROM = *const c_void; -pub type uv_mutex_t = CRITICAL_SECTION; - -// Socket address types (ws2def.h). The canonical `#[repr(C)]` definitions live -// in `bun_windows_sys::ws2_32` so the same nominal type flows through libuv, -// uws, and the runtime — keeps `set_membership(&sockaddr_storage)` etc. from -// becoming a cross-crate type mismatch. -pub use bun_windows_sys::ws2_32::{ - addrinfo, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, -}; - -// ────────────────────────────────────────────────────────────────────────── -// libuv scalar typedefs. -// ────────────────────────────────────────────────────────────────────────── -pub type uv_file = c_int; -pub type uv_os_sock_t = SOCKET; -pub type uv_os_fd_t = HANDLE; -pub type uv_pid_t = c_int; -pub type uv_uid_t = u8; -pub type uv_gid_t = u8; -pub type uv_req_type = c_uint; -pub type uv_fs_type = c_int; -pub(crate) type uv_tty_mode_t = c_uint; -/// `uv_tty_mode_t` (uv.h) — typed wrapper for `uv_tty_set_mode` callers. -#[repr(u32)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum TtyMode { - Normal = 0, // UV_TTY_MODE_NORMAL - Raw = 1, // UV_TTY_MODE_RAW - Io = 2, // UV_TTY_MODE_IO - /// `UV_TTY_MODE_RAW_VT` — raw mode with VT input processing left to the - /// terminal (Windows ENABLE_VIRTUAL_TERMINAL_INPUT). Aligns with POSIX raw. - Vt = 3, -} -pub type uv_stdio_flags = c_uint; - -// ────────────────────────────────────────────────────────────────────────── -// `uv__queue` / `uv__work` — internal intrusive list / threadpool work item. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv__queue { - pub next: *mut uv__queue, - pub prev: *mut uv__queue, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv__work { - pub work: Option, - pub done: Option, - pub loop_: *mut Loop, - pub wq: uv__queue, -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_buf_t` (uv/win.h) — `{ ULONG len; char* base; }` (Windows order is -// len-then-base, opposite of Unix). -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_buf_t { - pub len: ULONG, - pub base: *mut u8, -} -impl uv_buf_t { - #[inline] - pub fn init(input: &[u8]) -> uv_buf_t { - debug_assert!(input.len() <= ULONG::MAX as usize); - uv_buf_t { - len: input.len() as ULONG, - base: input.as_ptr().cast_mut(), - } - } - #[inline] - pub fn slice(&self) -> &[u8] { - // `from_raw_parts` requires non-null, aligned even for len==0. - // libuv routinely hands back `{len:0, base:NULL}` (declined alloc_cb, - // `uv_buf_init(NULL,0)`), so guard the empty/null case explicitly. - if self.len == 0 || self.base.is_null() { - return &[]; - } - // SAFETY: caller-supplied (base, len); valid for the buffer's lifetime. - unsafe { core::slice::from_raw_parts(self.base, self.len as usize) } - } - /// Mutable view of the buffer. - /// - /// SAFETY: caller asserts that `(base, len)` came from a writeable - /// allocation and that no other `&mut`/`&` to that storage is live for the - /// returned slice's lifetime. Takes `&mut self` so borrowck rejects the - /// obvious double-call aliasing footgun at the type level; this does *not* - /// by itself make the call safe (the pointee may still be aliased - /// elsewhere). - #[inline] - pub unsafe fn slice_mut(&mut self) -> &mut [u8] { - // See `slice()`: guard `(null, 0)` — `from_raw_parts_mut` requires a - // non-null pointer even for zero-length slices. - if self.len == 0 || self.base.is_null() { - return &mut []; - } - unsafe { core::slice::from_raw_parts_mut(self.base, self.len as usize) } - } -} - -// ────────────────────────────────────────────────────────────────────────── -// `UV_REQ_PRIVATE_FIELDS` (uv/win.h) — every `uv_req_t` embeds this tail. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct req_u_io { - pub overlapped: OVERLAPPED, - pub queued_bytes: usize, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct req_u_connect { - pub result: ULONG_PTR, - pub pipeHandle: HANDLE, - pub duplex_flags: DWORD, - pub name: *mut WCHAR, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub union req_u { - pub io: req_u_io, - pub connect: req_u_connect, -} - -/// `uv_req_t` — base request type. Every `*_req` struct begins with this -/// header (`UV_REQ_FIELDS`). -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_req_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_handle_t` header — every `uv_*_t` handle begins with this. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub union handle_u { - pub fd: c_int, - pub reserved: [*mut c_void; 4], -} - -/// `uv_handle_t` (`UV_HANDLE_FIELDS`). All concrete handle types are -/// layout-prefixed with these exact fields, so a `*mut Pipe` / `*mut Timer` -/// is castable to `*mut Handle`. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct Handle { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut Handle, - pub(crate) flags: c_uint, -} -pub type uv_handle_t = Handle; - -/// `uv_handle_type` (uv.h). `#[repr(C)]` so it round-trips through -/// `uv_guess_handle` / `uv_handle_get_type`. -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Debug, strum::IntoStaticStr)] -pub enum HandleType { - Unknown = 0, - Async = 1, - Check = 2, - FsEvent = 3, - FsPoll = 4, - Handle = 5, - Idle = 6, - NamedPipe = 7, - Poll = 8, - Prepare = 9, - Process = 10, - Stream = 11, - Tcp = 12, - Timer = 13, - Tty = 14, - Udp = 15, - Signal = 16, - File = 17, -} -pub type uv_handle_type = HandleType; -pub const UV_TTY: HandleType = HandleType::Tty; -pub const UV_NAMED_PIPE: HandleType = HandleType::NamedPipe; -pub const UV_UNKNOWN_HANDLE: HandleType = HandleType::Unknown; - -/// Safe `uv_guess_handle` wrapper. The FFI symbol returns `c_int` (see -/// [`uv_guess_handle_raw`]); range-check before producing a [`HandleType`] so an -/// unexpected discriminant (e.g. `UV_HANDLE_TYPE_MAX` or a future libuv -/// variant) degrades to `Unknown` instead of triggering enum-transmute UB. -#[inline] -pub fn uv_guess_handle(file: uv_file) -> uv_handle_type { - let raw = uv_guess_handle_raw(file); - if (HandleType::Unknown as c_int..=HandleType::File as c_int).contains(&raw) { - // SAFETY: `HandleType` is `#[repr(C)]` with contiguous discriminants - // 0..=17 and `raw` was just range-checked into that interval. - unsafe { mem::transmute::(raw) } - } else { - HandleType::Unknown - } -} -pub const UV_HANDLE_TYPE_MAX: c_int = 18; - -/// `RunMode` — `uv_run` mode argument. -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub enum RunMode { - Default = 0, - Once = 1, - NoWait = 2, -} - -// Callback types. -pub type uv_close_cb = Option; -pub type uv_alloc_cb = Option; -pub type uv_read_cb = - Option; -pub type uv_connection_cb = Option; -pub type uv_shutdown_cb = Option; -pub type uv_write_cb = Option; -pub type uv_connect_cb = Option; -pub type uv_timer_cb = Option; -pub type uv_async_cb = Option; -pub type uv_prepare_cb = Option; -pub type uv_check_cb = Option; -pub type uv_idle_cb = Option; -pub type uv_poll_cb = Option; -pub type uv_signal_cb = Option; -pub type uv_exit_cb = Option; -pub(crate) type uv_walk_cb = Option; -pub type uv_fs_cb = Option; -pub type uv_fs_event_cb = - Option; -pub type uv_udp_send_cb = Option; -pub type uv_udp_recv_cb = - Option; -pub type uv_getaddrinfo_cb = - Option; -pub type uv_work_cb = Option; -pub type uv_after_work_cb = Option; -pub type uv_malloc_func = Option *mut c_void>; -pub type uv_realloc_func = Option *mut c_void>; -pub type uv_calloc_func = Option *mut c_void>; -pub type uv_free_func = Option; - -// ────────────────────────────────────────────────────────────────────────── -// `Loop` — `uv_loop_t` (uv/win.h). Field-accurate; `active_handles` is read -// directly by `bun_event_loop` so this CANNOT be opaque. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -union active_reqs_u { - unused: *mut c_void, - count: c_uint, -} -#[repr(C)] -pub struct Loop { - pub data: *mut c_void, - pub active_handles: c_uint, - pub handle_queue: uv__queue, - active_reqs: active_reqs_u, - pub internal_fields: *mut c_void, - pub stop_flag: c_uint, - pub iocp: HANDLE, - pub time: u64, - pub pending_reqs_tail: *mut uv_req_t, - pub endgame_handles: *mut uv_handle_t, - pub timer_heap: *mut c_void, - pub prepare_handles: *mut uv_prepare_t, - pub check_handles: *mut uv_check_t, - pub idle_handles: *mut uv_idle_t, - pub next_prepare_handle: *mut uv_prepare_t, - pub next_check_handle: *mut uv_check_t, - pub next_idle_handle: *mut uv_idle_t, - pub poll_peer_sockets: [SOCKET; 4], - pub active_tcp_streams: c_uint, - pub active_udp_streams: c_uint, - pub timer_counter: u64, - pub wq: uv__queue, - pub wq_mutex: uv_mutex_t, - pub wq_async: uv_async_t, -} -pub type uv_loop_t = Loop; - -// `Loop::get()` escapes a raw pointer into this TLS slot out of the -// `LocalKey::with` closure and hands it to libuv for the thread lifetime. -// That is sound only because the slot has NO destructor: with no `Drop`, std -// registers no TLS dtor, so the `LocalKey` storage outlives every per-thread -// caller and the escaped address stays valid until thread exit. Guard the -// no-Drop invariant at compile time so adding `impl Drop for Loop` (or -// wrapping the cell) fails loudly here rather than becoming silent UB. -const _: () = assert!(!core::mem::needs_drop::()); -const _: () = assert!(!core::mem::needs_drop::>>()); - -thread_local! { - /// Static TLS storage (zero-alloc). `MaybeUninit` because the slot is - /// only valid after `uv_loop_init`. - static THREADLOCAL_LOOP_DATA: UnsafeCell> = - const { UnsafeCell::new(MaybeUninit::uninit()) }; - /// `threadlocal var threadlocal_loop: ?*Loop = null` — null until `get()` - /// initializes `THREADLOCAL_LOOP_DATA`. - static THREADLOCAL_LOOP: Cell<*mut Loop> = const { Cell::new(ptr::null_mut()) }; -} - -// ────────────────────────────────────────────────────────────────────────── -// Open stream/process handles on this thread — Bun's HandleWrap list. -// -// Every `uv_pipe_t`, `uv_tty_t` (except the process-static stdin tty) and -// `uv_process_t` this thread initialises is listed here from `init`/`spawn` -// until the `uv_close` for it is issued (`UvHandle::close`, -// `Pipe::close_and_destroy`). Whoever currently drives the handle records -// itself with [`open_handles::set_owner`] — readers/writers do so through -// `Source::set_owner`, IPC / named pipes / Process when they take the handle — -// so a thread teardown can close each handle through its owner's ordinary -// close path (parents observe the close; pending writes finish ECANCELED) -// while the VM is alive, or directly if nothing ever adopted it. Keyed by the -// handle's address, which is stable for its life (boxed / embedded in a boxed -// owner), so ownership moving between objects needs no re-registration. -// ────────────────────────────────────────────────────────────────────────── -#[path = "open_handles.rs"] -pub mod open_handles; - -impl Loop { - /// Returns this thread's - /// libuv loop, lazily `uv_loop_init`ing it on first call. Each thread owns - /// its own loop; sharing `uv_default_loop()` across worker threads is - /// unsound for the per-worker shutdown path. - pub fn get() -> *mut Loop { - THREADLOCAL_LOOP.with(|slot| { - let existing = slot.get(); - if !existing.is_null() { - return existing; - } - // SAFETY: TLS slot is per-thread; no aliasing. `uv_loop_init` - // accepts uninitialized storage (it zero-fills internally). - // Escaping the pointer past `.with()` is intentional: the slot is - // const-initialized POD with no TLS destructor (static-asserted - // above), so its address is stable for the thread lifetime. - let ptr_ = THREADLOCAL_LOOP_DATA.with(|data| unsafe { (*data.get()).as_mut_ptr() }); - // SAFETY: `ptr_` is `sizeof(Loop)` TLS storage owned by this thread. - if let Some(err) = unsafe { uv_loop_init(ptr_) }.raw_errno() { - panic!("Failed to initialize libuv loop: errno {err}"); - } - slot.set(ptr_); - ptr_ - }) - } - - /// Complete every in-flight request on this thread's loop (fs, write, - /// connect): requests cannot be cancelled, and their callbacks expect the - /// VM that issued them — and may start more work (a shell pipeline moving - /// to its next builtin) — so teardown runs this in its stop phase, script - /// forbidden but the VM alive and still accepting (and later awaiting) - /// off-thread work (Node's `CleanupHandles`). `true` if anything ran. - pub fn drain_requests() -> bool { - THREADLOCAL_LOOP.with(|slot| { - let loop_ = slot.get(); - if loop_.is_null() { - return false; - } - let mut ran = false; - // SAFETY: live per-thread loop; `count` is the active arm of the - // union whenever the loop is initialised (uv/win.h). - unsafe { - while (*loop_).active_reqs.count > 0 { - log!("drain_requests: {} in flight", (*loop_).active_reqs.count); - uv_run(loop_, RunMode::Once); - ran = true; - } - } - ran - }) - } - - /// Closes this thread's libuv loop. Called from `WebWorker::shutdown` after - /// the thread's uws loop has been freed and the event loop's pending - /// keep-alive delta has been folded (Bun's virtual keep-alive count shares - /// `active_handles` with libuv, so an unbalanced ref would keep the loop - /// alive forever). Every handle Bun registered on the loop must have been - /// closed while its owner was alive; what remains here is uSockets' own - /// pre/check/async/timer, closed by us_loop_free and freed by their close - /// callbacks when the loop next turns. - pub fn close_thread_loop() { - THREADLOCAL_LOOP.with(|slot| { - let loop_ = slot.get(); - if loop_.is_null() { - return; - } - // SAFETY: `loop_` is the live per-thread loop initialized in `get()`. - if let Some(err) = unsafe { uv_loop_close(loop_) }.raw_errno() { - // Only EBUSY means handles are still linked; walk + close any not - // already closing, run to flush close callbacks and endgames, then - // close again (must succeed). `uv_loop_close` documents no other - // failure code. - if err == (UV_EBUSY as c_int).unsigned_abs() as u16 { - // Anything open and not already closing here was left by an - // owner that never closed it; name it under BUN_DEBUG_uv. - // SAFETY: every linked handle's storage is still allocated - // (owners are freed only after this returns). - unsafe { uv_walk(loop_, Some(log_unclosed_cb), ptr::null_mut()) }; - unsafe { uv_walk(loop_, Some(close_walk_cb), ptr::null_mut()) }; - // Everything is closing now; only close callbacks / endgames - // remain. Turn the loop without blocking until they have run — - // RunMode::Default would also wait on ref'd-but-idle state - // (Bun's virtual keep-alive count lives in active_handles) and - // never return. - let mut rc = ReturnCode::ZERO; - for _ in 0..64 { - // SAFETY: this thread's initialised loop; nothing else drives it. - let _ = unsafe { uv_run(loop_, RunMode::NoWait) }; - // SAFETY: as above. - rc = unsafe { uv_loop_close(loop_) }; - if rc == ReturnCode::ZERO { - break; - } - } - debug_assert_eq!( - rc, - ReturnCode::ZERO, - "uv loop still busy after closing every handle" - ); - } - } - slot.set(ptr::null_mut()); - }); - } - - /// `Loop.subActive` — saturating decrement to mirror PosixLoop semantics - /// (avoid underflow during teardown when Bun's virtual keep-alive refs and - /// libuv's own accounting momentarily disagree). - #[inline] - pub fn sub_active(&mut self, value: u32) { - log!("subActive({}) - {}", value, self.active_handles); - self.active_handles = self.active_handles.saturating_sub(value); - } - #[inline] - pub fn add_active(&mut self, value: u32) { - log!("addActive({})", value); - self.active_handles = self.active_handles.saturating_add(value); - } - #[inline] - pub fn inc(&mut self) { - log!("inc - {}", self.active_handles.saturating_add(1)); - self.active_handles = self.active_handles.saturating_add(1); - } - #[inline] - pub fn dec(&mut self) { - log!("dec"); - self.active_handles = self.active_handles.saturating_sub(1); - } - #[inline] - pub fn stop(&mut self) { - log!("stop"); - // SAFETY: self is a live loop. - unsafe { uv_stop(self) }; - } - #[inline] - pub fn is_active(&self) -> bool { - // SAFETY: self is a live loop. - unsafe { uv_loop_alive(self) != 0 } - } - #[inline] - pub fn tick(&mut self) { - // SAFETY: self is a live loop. - let _ = unsafe { uv_run(self, RunMode::Default) }; - } -} - -/// `Loop::close_thread_loop` diagnostics: which handles keep the worker's loop busy. -unsafe extern "C" fn log_unclosed_cb(handle: *mut uv_handle_t, data: *mut c_void) { - // SAFETY: libuv passes live handles. - if unsafe { uv_is_closing(handle) } == 0 { - // SAFETY: as above. - unsafe { log_walk_cb(handle, data) }; - } -} - -/// # Safety -/// `handle` is a live libuv handle (only its header is read). -unsafe fn handle_type_name<'a>(handle: *mut uv_handle_t) -> &'a str { - // SAFETY: fn contract; libuv returns a static C string or null. - unsafe { - let name = uv_handle_type_name(uv_handle_get_type(handle)); - if name.is_null() { - "?" - } else { - core::ffi::CStr::from_ptr(name).to_str().unwrap_or("?") - } - } -} - -unsafe extern "C" fn log_walk_cb(handle: *mut uv_handle_t, _data: *mut c_void) { - // SAFETY: libuv passes a live handle; these calls only read its header. - unsafe { - log!( - "handle left open by its owner: {} @{:p} active={} closing={} ref={} data={:p}", - handle_type_name(handle), - handle, - uv_is_active(handle), - uv_is_closing(handle), - uv_has_ref(handle), - (*handle).data - ); - } -} - -unsafe extern "C" fn close_walk_cb(handle: *mut uv_handle_t, _data: *mut c_void) { - // SAFETY: libuv passes a live handle. - if unsafe { uv_is_closing(handle) } == 0 { - unsafe { uv_close(handle, None) }; - } -} - -// ────────────────────────────────────────────────────────────────────────── -// Handle mixin — a generic trait every -// handle type opts into. All methods are `#[inline]` zero-cost casts. -// ────────────────────────────────────────────────────────────────────────── -/// Marker for `#[repr(C)]` structs whose first fields are exactly -/// `UV_HANDLE_FIELDS` (i.e. layout-prefixed with [`Handle`]). -/// -/// SAFETY: `Self` must be `#[repr(C)]` and start with the same fields as -/// [`Handle`], so `*mut Self` is castable to `*mut uv_handle_t`. -pub unsafe trait UvHandle: Sized { - #[inline] - fn as_handle(&self) -> *const uv_handle_t { - (self as *const Self).cast() - } - #[inline] - fn as_handle_mut(&mut self) -> *mut uv_handle_t { - (self as *mut Self).cast() - } - #[inline] - fn data(&self) -> *mut c_void { - // SAFETY: handle prefix; `data` is at offset 0. - unsafe { (*self.as_handle()).data } - } - #[inline] - fn get_data(&self) -> *mut T { - // SAFETY: handle prefix invariant. - unsafe { uv_handle_get_data(self.as_handle()).cast() } - } - #[inline] - fn set_data(&mut self, ptr_: *mut c_void) { - // SAFETY: handle prefix invariant. - unsafe { uv_handle_set_data(self.as_handle_mut(), ptr_) }; - } - /// Typed `Box`-taking sibling of [`set_data`] for the common case where - /// `handle->data` owns a heap allocation freed in the close callback. - /// Ownership of `data` transfers to the handle; reclaim with - /// [`take_owned_data`](UvHandle::take_owned_data) (typically in the - /// `uv_close_cb`). This is the centralized `Box::into_raw` for the - /// owned-`handle->data` pattern — callers never spell the - /// raw round-trip themselves. - #[inline] - fn set_owned_data(&mut self, data: Box) { - self.set_data(Box::into_raw(data).cast::()); - } - /// Reclaim a `Box` previously installed via [`set_owned_data`]. Clears - /// the `data` slot to null so a second call (or a later `uv_close_cb`) - /// observes `None` rather than a dangling pointer. - /// - /// # Safety - /// The handle's `data` must either be null or the unique live pointer - /// from a prior [`set_owned_data::`](UvHandle::set_owned_data) call - /// with the **same** `T`. - #[inline] - unsafe fn take_owned_data(&mut self) -> Option> { - let p = self.get_data::(); - if p.is_null() { - return None; - } - self.set_data(ptr::null_mut()); - // SAFETY: caller contract — `p` came from `set_owned_data::`. - Some(unsafe { Box::from_raw(p) }) - } - #[inline] - fn get_loop(&self) -> *mut Loop { - // SAFETY: handle prefix invariant. - unsafe { uv_handle_get_loop(self.as_handle()) } - } - /// `HandleMixin::close` — `cb` receives the same pointer cast back to - /// `*mut Self`. ABI-identical to `uv_close_cb` modulo the pointee type. - #[inline] - fn close(&mut self, cb: unsafe extern "C" fn(*mut Self)) { - open_handles::remove(self.as_handle_mut()); - // SAFETY: `Self` embeds `uv_handle_t` at offset 0; cb is ABI-identical. - unsafe { - uv_close( - self.as_handle_mut(), - Some(mem::transmute::< - unsafe extern "C" fn(*mut Self), - unsafe extern "C" fn(*mut uv_handle_t), - >(cb)), - ); - } - } - #[inline] - fn has_ref(&self) -> bool { - // SAFETY: handle prefix invariant. - unsafe { uv_has_ref(self.as_handle()) != 0 } - } - #[inline] - fn ref_(&mut self) { - // SAFETY: handle prefix invariant. - unsafe { uv_ref(self.as_handle_mut()) }; - } - #[inline] - fn unref(&mut self) { - // SAFETY: handle prefix invariant. - unsafe { uv_unref(self.as_handle_mut()) }; - } - #[inline] - fn is_closing(&self) -> bool { - // SAFETY: handle prefix invariant. - unsafe { uv_is_closing(self.as_handle()) != 0 } - } - #[inline] - fn is_closed(&self) -> bool { - // SAFETY: handle prefix invariant. - uv_is_closed(unsafe { &*self.as_handle() }) - } - #[inline] - fn is_active(&self) -> bool { - // SAFETY: handle prefix invariant. - unsafe { uv_is_active(self.as_handle()) != 0 } - } - /// `HandleMixin::fd` — returns the OS handle, or `INVALID_HANDLE_VALUE` if - /// none. (Higher-tier crates wrap this in `bun_sys::Fd`.) - #[inline] - fn fd(&self) -> uv_os_fd_t { - let mut fd_: uv_os_fd_t = INVALID_HANDLE_VALUE; - // SAFETY: handle prefix invariant; out-param is valid. - let _ = unsafe { uv_fileno(self.as_handle(), &mut fd_) }; - fd_ - } -} -// SAFETY: all of these are `#[repr(C)]` with `UV_HANDLE_FIELDS` first. -unsafe impl UvHandle for Handle {} -unsafe impl UvHandle for uv_stream_t {} -unsafe impl UvHandle for Pipe {} -unsafe impl UvHandle for uv_tcp_t {} -unsafe impl UvHandle for uv_tty_t {} -unsafe impl UvHandle for uv_udp_t {} -unsafe impl UvHandle for Timer {} -unsafe impl UvHandle for uv_async_t {} -unsafe impl UvHandle for uv_prepare_t {} -unsafe impl UvHandle for uv_check_t {} -unsafe impl UvHandle for uv_idle_t {} -unsafe impl UvHandle for uv_poll_t {} -unsafe impl UvHandle for uv_signal_t {} -unsafe impl UvHandle for Process {} -unsafe impl UvHandle for uv_fs_event_t {} -unsafe impl UvHandle for uv_fs_poll_t {} - -/// Marker for `#[repr(C)]` structs prefixed with `UV_STREAM_FIELDS` -/// (`uv_stream_t`, `Pipe`, `uv_tcp_t`, `uv_tty_t`). -pub unsafe trait UvStream: UvHandle { - #[inline] - fn as_stream(&mut self) -> *mut uv_stream_t { - (self as *mut Self).cast() - } - #[inline] - fn get_write_queue_size(&self) -> usize { - // SAFETY: stream prefix invariant. - unsafe { uv_stream_get_write_queue_size((self as *const Self).cast()) } - } - #[inline] - fn read_start(&mut self, alloc_cb: uv_alloc_cb, read_cb: uv_read_cb) -> ReturnCode { - // SAFETY: stream prefix invariant. - unsafe { uv_read_start(self.as_stream(), alloc_cb, read_cb) } - } - #[inline] - fn read_stop(&mut self) { - // SAFETY: always succeeds (uv docs). - let _ = unsafe { uv_read_stop(self.as_stream()) }; - } - #[inline] - fn try_write(&mut self, bufs: &[uv_buf_t]) -> ReturnCode { - // SAFETY: stream prefix invariant. - unsafe { uv_try_write(self.as_stream(), bufs.as_ptr(), bufs.len() as c_uint) } - } - #[inline] - fn is_readable(&self) -> bool { - // SAFETY: stream prefix invariant. - unsafe { uv_is_readable((self as *const Self).cast()) != 0 } - } - #[inline] - fn is_writable(&self) -> bool { - // SAFETY: stream prefix invariant. - unsafe { uv_is_writable((self as *const Self).cast()) != 0 } - } - /// High-level wrapper - /// over `uv_read_start` that thunks Rust callbacks through a monomorphised - /// `extern "C"` trampoline. `context` is stashed in `handle.data` and - /// recovered in the trampoline; the three callbacks are baked into the - /// monomorphisation via the [`StreamReader`] trait (associated fns, so the - /// trampoline stays zero-alloc and `Handle` needs no spare storage). - /// - /// `error_cb` receives the - /// raw negative libuv errno (`c_int`); this crate is layered below - /// `bun_sys` so it can't name `E`. Callers map via - /// `bun_sys::windows::translate_uv_error_to_e`. Returns the raw - /// [`ReturnCode`] from `uv_read_start`; callers apply - /// `.to_error(Tag::listen)` themselves. - #[inline] - fn read_start_ctx(&mut self, context: *mut T) -> ReturnCode { - // SAFETY: stream prefix invariant — `&mut Self` reinterprets as - // `&mut Handle` for the leading `UV_HANDLE_FIELDS`. - let h: &mut Handle = unsafe { &mut *(self as *mut Self).cast::() }; - h.data = context.cast(); - - unsafe extern "C" fn uv_allocb( - req: *mut uv_handle_t, - suggested_size: usize, - buffer: *mut uv_buf_t, - ) { - // SAFETY: `req.data` was set to `context` above; libuv calls this - // on the loop thread before `uv_readcb`. - let ctx: &mut T = unsafe { bun_core::callback_ctx::((*req).data) }; - let buf = T::on_read_alloc(ctx, suggested_size); - // SAFETY: `buffer` is libuv's out-param. - unsafe { *buffer = uv_buf_t::init(buf) }; - } - unsafe extern "C" fn uv_readcb( - req: *mut uv_stream_t, - nreads: ReturnCodeI64, - buffer: *const uv_buf_t, - ) { - // Keep `ctx` raw — `(*buffer).base` was derived from the `&mut T` - // borrow taken in `uv_allocb`, so materialising a fresh `&mut T` - // here would pop that pointer's Stacked-Borrows tag before we - // read through it. Recover the raw `*mut T`, build the slice - // first, and hand the raw pointer to `on_read` so the impl owns - // the reborrow ordering. - // SAFETY: `req.data` was set to `context` above. - let ctx: *mut T = unsafe { (*req).data.cast::() }; - let n = nreads.int(); - if n == 0 { - return; // EAGAIN / EWOULDBLOCK - } - if n < 0 { - // SAFETY: stream prefix invariant. - let _ = unsafe { uv_read_stop(req) }; - // SAFETY: `ctx` is the live context stashed in `handle.data`. - T::on_read_error(unsafe { &mut *ctx }, n as c_int); - } else { - // SAFETY: `buffer` was filled by `uv_allocb` above with a - // slice of length `>= n`. - let slice = - unsafe { core::slice::from_raw_parts((*buffer).base.cast::(), n as usize) }; - // SAFETY: `ctx` is the live context stashed in `handle.data`. - unsafe { T::on_read(ctx, slice) }; - } - } - // SAFETY: stream prefix invariant. - unsafe { uv_read_start(self.as_stream(), Some(uv_allocb::), Some(uv_readcb::)) } - } -} - -/// Callback bundle for [`UvStream::read_start_ctx`]. -/// The `extern "C"` trampolines are monomorphised over -/// this trait so the callbacks are baked into the codegen (zero-alloc, no -/// per-handle storage). -pub trait StreamReader: Sized { - fn on_read_alloc(this: &mut Self, suggested_size: usize) -> &mut [u8]; - /// `err` is the raw negative libuv errno (e.g. `UV_EOF`). Map via - /// `bun_sys::windows::translate_uv_error_to_e` if `bun_sys::E` is needed. - fn on_read_error(this: &mut Self, err: c_int); - /// `this` is raw because `data` typically points *into* `*this` (it was - /// returned from [`on_read_alloc`]). Forming `&mut Self` in the trampoline - /// would alias with `data` under Stacked Borrows; the implementor decides - /// how to split the borrow. - /// - /// # Safety - /// `this` is the live context passed to [`UvStream::read_start_ctx`]. - unsafe fn on_read(this: *mut Self, data: &[u8]); -} -// SAFETY: all of these are `#[repr(C)]` with `UV_STREAM_FIELDS` prefix. -unsafe impl UvStream for uv_stream_t {} -unsafe impl UvStream for Pipe {} -unsafe impl UvStream for uv_tcp_t {} -unsafe impl UvStream for uv_tty_t {} - -// ────────────────────────────────────────────────────────────────────────── -// Stream / Read / Shutdown / Write / Connect requests. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -union stream_conn_serv { - conn: stream_conn, - serv: stream_serv, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct stream_conn { - write_reqs_pending: c_uint, - shutdown_req: *mut uv_shutdown_t, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct stream_serv { - connection_cb: uv_connection_cb, -} - -/// `uv_read_t` (uv/win.h) — `UV_REQ_FIELDS` + `event_handle` + `wait_handle`. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_read_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub event_handle: HANDLE, - pub wait_handle: HANDLE, -} - -/// `uv_shutdown_t`. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_shutdown_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub handle: *mut uv_stream_t, - pub cb: uv_shutdown_cb, -} - -/// `uv_stream_t` (`UV_HANDLE_FIELDS` + `UV_STREAM_FIELDS`). -#[repr(C)] -pub struct uv_stream_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub write_queue_size: usize, - pub alloc_cb: uv_alloc_cb, - pub read_cb: uv_read_cb, - pub reqs_pending: c_uint, - pub activecnt: c_int, - pub read_req: uv_read_t, - stream: stream_conn_serv, -} - -/// `uv_write_t`. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_write_t { - pub(crate) data: *mut c_void, - pub type_: uv_req_type, - pub(crate) reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub cb: uv_write_cb, - pub send_handle: *mut uv_stream_t, - pub handle: *mut uv_stream_t, - pub coalesced: c_int, - pub write_buffer: uv_buf_t, - pub event_handle: HANDLE, - pub wait_handle: HANDLE, -} -impl uv_write_t { - /// Context-aware `uv_write`. Stores `context` in `req.data`; - /// the trampoline recovers it and dispatches to `on_write` as a plain Rust - /// `&mut`. Generic monomorphisation gives one `extern "C"` thunk per ``. - /// - /// Without a `bun_sys` dependency / unstable const-generic fn pointers, - /// this (a) keeps `on_write` runtime-dispatched but stashes it as a `usize` - /// (fn-ptr ↔ integer is well-defined; fn-ptr ↔ data-ptr is not — Miri - /// rejects the latter), and (b) returns the raw [`ReturnCode`]; callers - /// apply `.to_error(Tag::write)` themselves. The `bun.sys.syslog` line is - /// emitted via this crate's `[uv]` log scope. - #[inline] - pub fn write( - &mut self, - stream: *mut uv_stream_t, - input: &uv_buf_t, - context: *mut T, - on_write: fn(*mut T, ReturnCode), - ) -> ReturnCode { - // Stash the Rust fn-pointer in `reserved[0]` (libuv never touches the - // 6-slot `reserved` array on `uv_req_t`) as a `usize`, recovered in the - // thunk below. - self.data = context.cast(); - self.reserved[0] = on_write as usize as *mut c_void; - unsafe extern "C" fn thunk(req: *mut uv_write_t, status: ReturnCode) { - // SAFETY: `data`/`reserved[0]` were set immediately before - // `uv_write` below; libuv invokes this exactly once with the same - // `req` pointer. The `usize` → `fn` cast round-trips the address - // written by `on_write as usize` above (Win64: same width). - // Pass the raw `*mut T` straight through - // — callers commonly free the `T` allocation inside the callback, - // so materialising `&mut T` here would leave that reference - // dangling across the dealloc (UB). - unsafe { - let cb: fn(*mut T, ReturnCode) = - mem::transmute::((*req).reserved[0] as usize); - cb((*req).data.cast::(), status); - } - } - // SAFETY: caller guarantees `self` lives until the cb fires and - // `stream` is a live stream handle. - let rc = unsafe { uv_write(self, stream, input, 1, Some(thunk::)) }; - crate::__uv_log!("uv_write({}) = {}", input.len, rc.int()); - rc - } -} - -/// `uv_connect_t`. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_connect_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub cb: uv_connect_cb, - pub handle: *mut uv_stream_t, -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_tcp_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_tcp_accept_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub accept_socket: SOCKET, - pub accept_buffer: [u8; 288], - pub event_handle: HANDLE, - pub wait_handle: HANDLE, - pub next_pending: *mut uv_tcp_accept_t, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct tcp_serv { - accept_reqs: *mut uv_tcp_accept_t, - processed_accepts: c_uint, - pending_accepts: *mut uv_tcp_accept_t, - func_acceptex: LPFN_ACCEPTEX, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct tcp_conn { - read_buffer: uv_buf_t, - func_connectex: LPFN_CONNECTEX, -} -#[repr(C)] -#[derive(Clone, Copy)] -union tcp_u { - serv: tcp_serv, - conn: tcp_conn, -} -#[repr(C)] -pub struct uv_tcp_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub write_queue_size: usize, - pub alloc_cb: uv_alloc_cb, - pub read_cb: uv_read_cb, - pub reqs_pending: c_uint, - pub activecnt: c_int, - pub read_req: uv_read_t, - stream: stream_conn_serv, - pub socket: SOCKET, - pub delayed_error: c_int, - tcp: tcp_u, -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_udp_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -pub struct uv_udp_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub send_queue_size: usize, - pub send_queue_count: usize, - pub socket: SOCKET, - pub reqs_pending: c_uint, - pub activecnt: c_int, - pub recv_req: uv_req_t, - pub recv_buffer: uv_buf_t, - pub recv_from: sockaddr_storage, - pub recv_from_len: c_int, - pub recv_cb: uv_udp_recv_cb, - pub alloc_cb: uv_alloc_cb, - pub func_wsarecv: LPFN_WSARECV, - pub func_wsarecvfrom: LPFN_WSARECVFROM, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_udp_send_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub handle: *mut uv_udp_t, - pub cb: uv_udp_send_cb, -} - -// ────────────────────────────────────────────────────────────────────────── -// `Pipe` (`uv_pipe_t`). -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_pipe_accept_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub pipeHandle: HANDLE, - pub next_pending: *mut uv_pipe_accept_t, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct pipe_serv { - pending_instances: c_int, - accept_reqs: *mut uv_pipe_accept_t, - pending_accepts: *mut uv_pipe_accept_t, -} -#[repr(C)] -#[derive(Clone, Copy)] -union ipc_data_frame { - payload_remaining: u32, - dummy: u64, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct pipe_conn { - eof_timer: *mut Timer, - dummy: uv_write_t, - ipc_remote_pid: DWORD, - ipc_data_frame: ipc_data_frame, - ipc_xfer_queue: uv__queue, - ipc_xfer_queue_length: c_int, - non_overlapped_writes_tail: *mut uv_write_t, - readfile_thread_lock: CRITICAL_SECTION, - readfile_thread_handle: HANDLE, -} -#[repr(C)] -#[derive(Clone, Copy)] -union pipe_u { - serv: pipe_serv, - conn: pipe_conn, -} -#[repr(C)] -pub struct Pipe { - pub data: *mut c_void, - pub(crate) loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub write_queue_size: usize, - pub alloc_cb: uv_alloc_cb, - pub read_cb: uv_read_cb, - pub reqs_pending: c_uint, - pub activecnt: c_int, - pub read_req: uv_read_t, - stream: stream_conn_serv, - pub ipc: c_int, - pub handle: HANDLE, - pub name: *mut WCHAR, - pipe: pipe_u, -} - -impl Pipe { - #[inline] - pub fn ipc_remote_pid(&self) -> DWORD { - // SAFETY: `conn` is the active variant for a connected IPC pipe (init - unsafe { self.pipe.conn.ipc_remote_pid } - } - /// `uv_pipe_init` wrapper. Returns the raw `ReturnCode`; callers - /// in higher tiers map to `bun_sys::Result` themselves so this crate stays - /// free of `bun_sys`. - #[inline] - pub fn init(&mut self, loop_: *mut Loop, ipc: bool) -> ReturnCode { - // SAFETY: `self` is a valid `uv_pipe_t`-sized allocation. - let rc = unsafe { uv_pipe_init(loop_, self, if ipc { 1 } else { 0 }) }; - if rc.0 == 0 { - open_handles::add_pipe(self); - } - rc - } - #[inline] - pub fn open(&mut self, file: uv_file) -> ReturnCode { - // SAFETY: pipe was `init`ed. - unsafe { uv_pipe_open(self, file) } - } - #[inline] - pub(crate) fn bind(&mut self, named_pipe: &[u8], flags: c_uint) -> ReturnCode { - // SAFETY: pipe was `init`ed; libuv copies the name. - unsafe { uv_pipe_bind2(self, named_pipe.as_ptr(), named_pipe.len(), flags) } - } - /// Caller supplies a plain - /// `uv_connection_cb` and recovers its context from `handle.data` itself. - #[inline] - pub(crate) fn listen( - &mut self, - backlog: i32, - context: *mut c_void, - on_connect: unsafe extern "C" fn(*mut uv_stream_t, ReturnCode), - ) -> ReturnCode { - self.data = context; - // SAFETY: `Pipe` is layout-prefixed with `uv_stream_t`. - unsafe { uv_listen(self.as_stream(), backlog, Some(on_connect)) } - } - /// `Pipe::listenNamedPipe` — bind + listen. - #[inline] - pub fn listen_named_pipe( - &mut self, - named_pipe: &[u8], - backlog: i32, - context: *mut c_void, - on_connect: unsafe extern "C" fn(*mut uv_stream_t, ReturnCode), - ) -> ReturnCode { - let rc = self.bind(named_pipe, UV_PIPE_NO_TRUNCATE); - if rc.is_err() { - return rc; - } - self.listen(backlog, context, on_connect) - } - #[inline] - pub fn connect( - &mut self, - req: &mut uv_connect_t, - name: &[u8], - context: *mut c_void, - on_connect: unsafe extern "C" fn(*mut uv_connect_t, ReturnCode), - ) -> ReturnCode { - self.data = context; - // SAFETY: pipe was `init`ed; libuv copies the name. - unsafe { - uv_pipe_connect2( - req, - self, - name.as_ptr(), - name.len(), - UV_PIPE_NO_TRUNCATE, - Some(on_connect), - ) - } - } - #[inline] - pub fn accept(&mut self, client: &mut Pipe) -> ReturnCode { - // SAFETY: both pipes embed `uv_stream_t` at offset 0. - unsafe { uv_accept(self.as_stream(), client.as_stream()) } - } - /// Close the pipe handle (if - /// needed) and then `Box::from_raw`-drop it. Handles all states: - /// never-initialized (`loop_ == null`), already closing, or active. After - /// `uv_pipe_init` the handle is in the event loop's `handle_queue`; - /// freeing without `uv_close` corrupts that list. - /// - /// SAFETY: `this` must be a `Box`-allocated pointer (the close - /// callback reclaims it via `Box::from_raw`). Caller relinquishes - /// ownership. Receiver is `*mut Pipe` (not `&mut self`) because the - /// never-initialized branch deallocates the pointee — holding a live - /// `&mut self` across that drop would dangle. In the already-closing - /// branch the allocation is *not* reclaimed here: the previously - /// registered `uv_close` callback is assumed to free the box; - /// if a non-freeing callback was registered, the pipe leaks. - pub unsafe fn close_and_destroy(this: *mut Pipe) { - open_handles::remove(this.cast()); - // SAFETY: caller contract. - unsafe { Self::close_and_destroy_unlisted(this) } - } - - /// [`close_and_destroy`] for a pipe already taken off the open-handles list. - /// - /// # Safety - /// As [`close_and_destroy`]. - pub(crate) unsafe fn close_and_destroy_unlisted(this: *mut Pipe) { - unsafe extern "C" fn on_close_destroy(handle: *mut Pipe) { - // SAFETY: handle was Box-allocated; callback fires exactly once. - drop(unsafe { Box::from_raw(handle) }); - } - // SAFETY: caller contract — `this` is a live Box-allocated Pipe. - if unsafe { (*this).loop_.is_null() } { - // Never initialized — safe to free directly. - // SAFETY: caller contract — Box-allocated; no `&mut` borrow held. - drop(unsafe { Box::from_raw(this) }); - } else if !unsafe { (*this).is_closing() } { - // Initialized and not yet closing — must uv_close first. - // SAFETY: `this` is live until the close cb fires. - unsafe { (*this).close(on_close_destroy) }; - } - // else: already closing — the pending close callback owns the lifetime. - } -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_tty_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -struct tty_rd { - unused_: HANDLE, - read_line_buffer: uv_buf_t, - read_raw_wait: HANDLE, - last_key: [u8; 8], - last_key_offset: u8, - last_key_len: u8, - last_utf16_high_surrogate: WCHAR, - last_input_record: INPUT_RECORD, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct tty_wr { - utf8_codepoint: c_uint, - utf8_bytes_left: u8, - previous_eol: u8, - ansi_parser_state: c_ushort, - ansi_csi_argc: u8, - ansi_csi_argv: [c_ushort; 4], - saved_position: COORD, - saved_attributes: WORD, -} -#[repr(C)] -#[derive(Clone, Copy)] -union tty_u { - rd: tty_rd, - wr: tty_wr, -} -#[repr(C)] -pub struct uv_tty_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub write_queue_size: usize, - pub alloc_cb: uv_alloc_cb, - pub read_cb: uv_read_cb, - pub reqs_pending: c_uint, - pub activecnt: c_int, - pub read_req: uv_read_t, - stream: stream_conn_serv, - pub handle: HANDLE, - tty: tty_u, -} -/// A console handle plus the read buffer whose lifetime libuv ties to the -/// handle rather than to any reader. `uv` must remain the first field -/// (`#[repr(C)]`): callbacks and `open_handles` traffic in the inner -/// `uv_tty_t*`, and [`Tty::from_uv`] relies on both being the same address. -#[repr(C)] -pub struct Tty { - pub uv: uv_tty_t, - - /// Destination buffer for this handle's console reads. A cooked-mode - /// line read parks a worker thread in `ReadConsoleW` writing into the - /// `alloc_cb` buffer, and a read cancelled by `uv_read_stop` is never - /// handed back through `read_cb` — so the buffer must be owned by - /// something libuv guarantees outlives the read. The handle is that - /// thing: its close callback only runs once no requests are pending, - /// and the process-static stdin tty is never closed at all. - pub read_scratch: Vec, -} - -impl Tty { - /// Recover the owning `Tty` from the `uv_tty_t*` libuv hands back - /// (same address: `uv` is the first `#[repr(C)]` field). - #[inline] - pub fn from_uv(handle: *mut uv_tty_t) -> *mut Tty { - handle.cast() - } - - #[inline] - pub fn init(&mut self, loop_: *mut Loop, file: uv_file) -> ReturnCode { - // Init and register through a whole-struct pointer: the close paths - // reclaim the full `Tty` from this address, and `&mut self.uv` would - // narrow provenance to the field (same rule as `File::start_close`). - let uv_ptr = core::ptr::from_mut(self).cast::(); - // SAFETY: `uv` is the first `#[repr(C)]` field, sized for uv_tty_t. - let rc = unsafe { uv_tty_init(loop_, uv_ptr, file, 0) }; - // fd 0 is the process-static stdin tty (never freed, shared across - // threads by design); everything else is a heap tty owned by this thread. - if rc.0 == 0 && file != 0 { - open_handles::add_tty(uv_ptr); - } - rc - } - - /// `uv_close` through a whole-struct pointer so the close callback may - /// `Box::from_raw` the wrapper (`(*this).uv.close(..)` would autoref - /// `&mut uv_tty_t` and narrow provenance to the field). - /// - /// # Safety - /// `this` is a live, initialised tty that is not already closing. - pub unsafe fn close(this: *mut Self, cb: unsafe extern "C" fn(*mut uv_tty_t)) { - let handle = this.cast::(); - open_handles::remove(handle); - // SAFETY: `Tty` embeds `uv_handle_t` at offset 0; cb is ABI-identical. - unsafe { - uv_close( - handle, - Some(mem::transmute::< - unsafe extern "C" fn(*mut uv_tty_t), - unsafe extern "C" fn(*mut uv_handle_t), - >(cb)), - ); - } - } -} - -impl uv_tty_t { - #[inline] - pub fn set_mode(&mut self, mode: TtyMode) -> ReturnCode { - // SAFETY: tty was `init`ed. - unsafe { uv_tty_set_mode(self, mode as uv_tty_mode_t) } - } -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_poll_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct AFD_POLL_HANDLE_INFO { - pub Handle: HANDLE, - pub Events: ULONG, - pub Status: NTSTATUS, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct AFD_POLL_INFO { - pub Timeout: LARGE_INTEGER, - pub NumberOfHandles: ULONG, - pub Exclusive: ULONG, - pub Handles: [AFD_POLL_HANDLE_INFO; 1], -} -#[repr(C)] -pub struct uv_poll_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub poll_cb: uv_poll_cb, - pub socket: SOCKET, - pub peer_socket: SOCKET, - pub afd_poll_info_1: AFD_POLL_INFO, - pub afd_poll_info_2: AFD_POLL_INFO, - pub poll_req_1: uv_req_t, - pub poll_req_2: uv_req_t, - pub submitted_events_1: u8, - pub submitted_events_2: u8, - pub mask_events_1: u8, - pub mask_events_2: u8, - pub events: u8, -} - -// ────────────────────────────────────────────────────────────────────────── -// `Timer` (`uv_timer_t`). -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -pub struct Timer { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub heap_node: [*mut c_void; 3], - pub unused: c_int, - pub timeout: u64, - pub repeat: u64, - pub start_id: u64, - pub timer_cb: uv_timer_cb, -} -impl Timer { - #[inline] - pub fn init(&mut self, loop_: *mut Loop) { - // SAFETY: `self` is a valid `uv_timer_t`-sized allocation. - if unsafe { uv_timer_init(loop_, self) } != 0 { - panic!("internal error: uv_timer_init failed"); - } - } - #[inline] - pub fn start(&mut self, timeout: u64, repeat: u64, callback: uv_timer_cb) { - // SAFETY: timer was `init`ed. - if unsafe { uv_timer_start(self, callback, timeout, repeat) } != 0 { - panic!("internal error: uv_timer_start failed"); - } - } - #[inline] - pub fn stop(&mut self) { - // SAFETY: timer was `init`ed. - if unsafe { uv_timer_stop(self) } != 0 { - panic!("internal error: uv_timer_stop failed"); - } - } -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_prepare_t` / `uv_check_t` / `uv_idle_t` / `uv_async_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -pub struct uv_prepare_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub prepare_prev: *mut uv_prepare_t, - pub prepare_next: *mut uv_prepare_t, - pub prepare_cb: uv_prepare_cb, -} -#[repr(C)] -pub struct uv_check_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub check_prev: *mut uv_check_t, - pub check_next: *mut uv_check_t, - pub check_cb: uv_check_cb, -} -#[repr(C)] -pub struct uv_idle_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub idle_prev: *mut uv_idle_t, - pub idle_next: *mut uv_idle_t, - pub idle_cb: uv_idle_cb, -} -impl uv_idle_t { - #[inline] - pub fn init(&mut self, loop_: *mut Loop) { - // SAFETY: `self` is `#[repr(C)]` POD; all-zero is valid. - unsafe { ptr::write_bytes(self, 0, 1) }; - // SAFETY: self is a valid `uv_idle_t`-sized allocation. - if unsafe { uv_idle_init(loop_, self) } != 0 { - panic!("internal error: uv_idle_init failed"); - } - } - #[inline] - pub fn start(&mut self, cb: uv_idle_cb) { - // SAFETY: idle was `init`ed. - let _ = unsafe { uv_idle_start(self, cb) }; - } - #[inline] - pub fn stop(&mut self) { - // SAFETY: idle was `init`ed. - let _ = unsafe { uv_idle_stop(self) }; - } -} -#[repr(C)] -pub struct uv_async_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub async_req: uv_req_t, - pub async_cb: uv_async_cb, - pub async_sent: u8, -} - -// ────────────────────────────────────────────────────────────────────────── -// `Process` (`uv_process_t`) + spawn options. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_process_exit_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, -} -#[repr(C)] -pub struct Process { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub exit_cb: uv_exit_cb, - pub pid: c_int, - pub exit_req: uv_process_exit_t, - pub unused: *mut c_void, - pub exit_signal: c_int, - pub wait_handle: HANDLE, - pub process_handle: HANDLE, - pub exit_cb_pending: u8, -} -pub type uv_process_t = Process; -impl Process { - #[inline] - pub fn spawn(&mut self, loop_: *mut Loop, options: *const uv_process_options_t) -> ReturnCode { - // SAFETY: `self` is a valid `uv_process_t`-sized allocation. - let rc = unsafe { uv_spawn(loop_, self, options) }; - if rc.0 == 0 { - open_handles::add_process(self); - } - rc - } - #[inline] - pub fn kill(&mut self, signum: c_int) -> ReturnCode { - // SAFETY: process was spawned. - unsafe { uv_process_kill(self, signum) } - } -} - -#[repr(C)] -#[derive(Clone, Copy)] -pub union uv_stdio_container_data { - pub stream: *mut uv_stream_t, - pub fd: c_int, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_stdio_container_t { - pub flags: uv_stdio_flags, - pub data: uv_stdio_container_data, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_process_options_t { - pub exit_cb: uv_exit_cb, - pub file: *const c_char, - pub args: *const *const c_char, - pub env: *const *const c_char, - pub cwd: *const c_char, - pub flags: c_uint, - pub stdio_count: c_int, - pub stdio: *mut uv_stdio_container_t, - pub uid: uv_uid_t, - pub gid: uv_gid_t, - /// Windows only: HPCON from CreatePseudoConsole. When non-null, the child - /// is attached to the pseudoconsole and stdio[] is not inherited. - pub pseudoconsole: *mut c_void, -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_fs_event_t` / `uv_fs_poll_t` / `uv_signal_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_fs_event_req_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, -} -#[repr(C)] -pub struct uv_fs_event_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub path: *mut c_char, - pub req: uv_fs_event_req_t, - pub dir_handle: HANDLE, - pub req_pending: c_int, - pub cb: uv_fs_event_cb, - pub filew: *mut WCHAR, - pub short_filew: *mut WCHAR, - pub(crate) dirw: *mut WCHAR, - pub buffer: *mut u8, -} -impl uv_fs_event_t { - #[inline] - pub fn is_dir(&self) -> bool { - !self.dirw.is_null() - } - - /// Wyhash over - /// `path ?? "null"`, `events` bytes, `filename`, `status` bytes. - pub fn hash(&self, filename: &[u8], events: c_int, status: ReturnCode) -> u64 { - let mut hasher = bun_wyhash::Wyhash::init(0); - if self.path.is_null() { - hasher.update(b"null"); - } else { - // SAFETY: `path` is a valid NUL-terminated C string owned by libuv - // for the lifetime of the open handle. - hasher.update(unsafe { core::ffi::CStr::from_ptr(self.path) }.to_bytes()); - } - hasher.update(&events.to_ne_bytes()); - hasher.update(filename); - hasher.update(&status.0.to_ne_bytes()); - hasher.final_() - } -} -#[repr(C)] -pub struct uv_fs_poll_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub poll_ctx: *mut c_void, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct signal_tree_entry { - rbe_left: *mut uv_signal_t, - rbe_right: *mut uv_signal_t, - rbe_parent: *mut uv_signal_t, - rbe_color: c_int, -} -#[repr(C)] -pub struct uv_signal_t { - pub data: *mut c_void, - pub loop_: *mut Loop, - pub type_: HandleType, - pub close_cb: uv_close_cb, - pub handle_queue: uv__queue, - pub u: handle_u, - pub endgame_next: *mut uv_handle_t, - pub flags: c_uint, - pub signal_cb: uv_signal_cb, - pub signum: c_int, - tree_entry: signal_tree_entry, - pub signal_req: uv_req_t, - pub pending_signum: c_ulong, -} - -// ────────────────────────────────────────────────────────────────────────── -// `uv_getaddrinfo_t` / `uv_work_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -pub struct uv_getaddrinfo_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub loop_: *mut Loop, - pub work_req: uv__work, - pub getaddrinfo_cb: uv_getaddrinfo_cb, - pub alloc: *mut c_void, - pub node: *mut WCHAR, - pub service: *mut WCHAR, - pub addrinfow: *mut c_void, - pub addrinfo: *mut addrinfo, - pub retcode: ReturnCode, -} -#[repr(C)] -pub struct uv_work_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub loop_: *mut Loop, - pub work_cb: uv_work_cb, - pub after_work_cb: uv_after_work_cb, - pub work_req: uv__work, -} - -// ────────────────────────────────────────────────────────────────────────── -// `fs_t` (`uv_fs_t`) + `uv_stat_t`. -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_timespec_t { - pub sec: c_long, - pub nsec: c_long, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_timeval_t { - pub sec: c_long, - pub usec: c_long, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_stat_t { - // Field names match libuv's `uv_stat_t` (uv.h) — `st_*`, not bare `mode` — - // so this is field-compatible with `libc::stat` for cross-platform code - // that does `stat.st_mode` / `stat.st_size`. Layout asserts at bottom of - // file lock the C ABI; only the Rust-side names changed (round 3). - pub st_dev: u64, - pub st_mode: u64, - pub st_nlink: u64, - pub st_uid: u64, - pub st_gid: u64, - pub st_rdev: u64, - pub st_ino: u64, - pub st_size: u64, - pub st_blksize: u64, - pub st_blocks: u64, - pub st_flags: u64, - pub st_gen: u64, - pub atim: uv_timespec_t, - pub mtim: uv_timespec_t, - pub ctim: uv_timespec_t, - pub birthtim: uv_timespec_t, -} -impl uv_stat_t { - #[inline] - pub fn mtime(&self) -> uv_timespec_t { - self.mtim - } - // Un-prefixed accessors so cross-platform code that pattern-matches on - // POSIX `stat.mode`/`stat.size` can call - // through without `cfg` arms. - #[inline] - pub fn mode(&self) -> u64 { - self.st_mode - } - #[inline] - pub fn size(&self) -> u64 { - self.st_size - } -} - -#[repr(C)] -#[derive(Clone, Copy)] -union fs_file { - pathw: *mut WCHAR, - fd: c_int, -} -#[repr(C)] -#[derive(Clone, Copy)] -struct fs_info { - mode: c_int, - new_pathw: *mut WCHAR, - file_flags: c_int, - fd_out: c_int, - nbufs: c_uint, - bufs: *mut uv_buf_t, - offset: i64, - bufsml: [uv_buf_t; 4], -} -#[repr(C)] -#[derive(Clone, Copy)] -struct fs_time { - atime: f64, - mtime: f64, -} -#[repr(C)] -#[derive(Clone, Copy)] -union fs_fs { - info: fs_info, - time: fs_time, -} - -/// `uv_fs_t` (Windows layout). -#[repr(C)] -pub struct fs_t { - pub data: *mut c_void, - pub type_: uv_req_type, - pub reserved: [*mut c_void; 6], - pub u: req_u, - pub next_req: *mut uv_req_t, - pub fs_type: uv_fs_type, - pub loop_: *mut Loop, - pub cb: uv_fs_cb, - pub result: ReturnCodeI64, - pub(crate) ptr: *mut c_void, - pub path: *const c_char, - pub statbuf: uv_stat_t, - pub work_req: uv__work, - pub(crate) flags: c_int, - pub sys_errno_: DWORD, - file: fs_file, - fs: fs_fs, -} -pub type uv_fs_t = fs_t; - -impl fs_t { - #[cfg(debug_assertions)] - const UV_FS_CLEANEDUP: c_int = 0x0010; - - /// Debug sentinel: `loop_` is poisoned so `deinit()` can assert that libuv - /// actually wrote the request before we try to clean it up. - /// Kept as a fn rather than a `const` (raw-pointer fields - /// in nested unions block a true `const`); `#[inline(always)]` so the - /// ~440-byte zero-fill on every sync `uv_fs_*` call optimises to a - /// `.rodata` copy. - #[inline(always)] - pub fn uninitialized() -> fs_t { - let mut v: fs_t = bun_core::ffi::zeroed(); - v.loop_ = 0xAAAA_AAAA_AAAA_0000usize as *mut Loop; - v - } - - #[inline] - pub fn deinit(&mut self) { - self.assert_initialized(); - // SAFETY: `self` was passed to a `uv_fs_*` call (assert above). - unsafe { uv_fs_req_cleanup(self) }; - self.assert_cleaned_up(); - } - #[inline] - fn assert_initialized(&self) { - #[cfg(debug_assertions)] - if self.loop_ as usize == 0xAAAA_AAAA_AAAA_0000usize { - panic!("uv_fs_t was not initialized"); - } - } - #[inline] - pub fn assert_cleaned_up(&self) { - #[cfg(debug_assertions)] - { - if self.loop_ as usize == 0xAAAA_AAAA_AAAA_0000usize { - return; - } - if (self.flags & Self::UV_FS_CLEANEDUP) != 0 { - return; - } - panic!( - "uv_fs_t was not cleaned up. it is expected to call .deinit() on the fs_t here." - ); - } - } - #[inline] - pub unsafe fn ptr_as(&self) -> *const T { - self.assert_initialized(); - self.ptr.cast::() - } - /// `req.file.fd` union arm. The union is private - /// because the active variant is path-dependent (`uv_fs_open` writes `fd`; - /// path-taking ops write `pathw`); callers reading the wrong arm get UB. - /// SAFETY: only valid after a `uv_fs_*` call that populated the `fd` arm. - #[inline] - pub unsafe fn file_fd(&self) -> uv_file { - unsafe { self.file.fd } - } -} - -// ────────────────────────────────────────────────────────────────────────── -// Misc info structs (`uv_cpu_info_t`, `uv_interface_address_t`, …). -// ────────────────────────────────────────────────────────────────────────── -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_cpu_times_t { - pub user: u64, - pub nice: u64, - pub sys: u64, - pub idle: u64, - pub irq: u64, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_cpu_info_t { - pub model: *mut c_char, - pub speed: c_int, - pub cpu_times: uv_cpu_times_t, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub union addr_union { - pub address4: sockaddr_in, - pub address6: sockaddr_in6, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub union netmask_union { - pub netmask4: sockaddr_in, - pub netmask6: sockaddr_in6, -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_interface_address_t { - pub name: *mut c_char, - pub phys_addr: [u8; 6], - pub is_internal: c_int, - pub address: addr_union, - pub netmask: netmask_union, -} -#[repr(C)] -pub struct uv_utsname_t { - pub sysname: [u8; 256], - pub release: [u8; 256], - pub version: [u8; 256], - pub machine: [u8; 256], -} -pub type uv_utsname_s = uv_utsname_t; -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_statfs_t { - pub f_type: u64, - pub f_bsize: u64, - pub f_blocks: u64, - pub f_bfree: u64, - pub f_bavail: u64, - pub f_files: u64, - pub f_ffree: u64, - pub f_spare: [u64; 4], -} -#[repr(C)] -#[derive(Clone, Copy)] -pub struct uv_rusage_t { - pub ru_utime: uv_timeval_t, - pub ru_stime: uv_timeval_t, - pub ru_maxrss: u64, - pub ru_ixrss: u64, - pub ru_idrss: u64, - pub ru_isrss: u64, - pub ru_minflt: u64, - pub ru_majflt: u64, - pub ru_nswap: u64, - pub ru_inblock: u64, - pub ru_oublock: u64, - pub ru_msgsnd: u64, - pub ru_msgrcv: u64, - pub ru_nsignals: u64, - pub ru_nvcsw: u64, - pub ru_nivcsw: u64, -} - -// ────────────────────────────────────────────────────────────────────────── -// `ReturnCode` / `ReturnCodeI64` — `enum(c_int)` newtypes; libuv return codes -// are `0` on success, `-errno` on failure. -// ────────────────────────────────────────────────────────────────────────── -/// Map a negative `UV_E*` libuv error code to the stable `bun.sys.E` / -/// `bun_errno::E` discriminant (e.g. `UV_ENOENT (-4058)` → `2`). -/// -/// Layering forbids depending on `bun_errno` here, so -/// the integer discriminants are inlined; they are ABI-stable POSIX values -/// plus a fixed Bun-assigned tail (`UNKNOWN=134`..`FTYPE=137`). Unmapped -/// codes return `None`. -/// -/// Keep in sync with `bun_errno::E` (src/errno/windows_errno.rs). -#[inline] -pub const fn uv_err_to_e_discriminant(code: c_int) -> Option { - Some(match code { - UV_EPERM => 1, // E::PERM - UV_ENOENT => 2, // E::NOENT - UV_ESRCH => 3, // E::SRCH - UV_EINTR => 4, // E::INTR - UV_EIO => 5, // E::IO - UV_ENXIO => 6, // E::NXIO - UV_E2BIG => 7, // E::_2BIG - UV_ENOEXEC => 8, // E::NOEXEC - UV_EBADF => 9, // E::BADF - UV_EAGAIN => 11, // E::AGAIN - UV_ENOMEM => 12, // E::NOMEM - UV_EACCES => 13, // E::ACCES - UV_EFAULT => 14, // E::FAULT - UV_EBUSY => 16, // E::BUSY - UV_EEXIST => 17, // E::EXIST - UV_EXDEV => 18, // E::XDEV - UV_ENODEV => 19, // E::NODEV - UV_ENOTDIR => 20, // E::NOTDIR - UV_EISDIR => 21, // E::ISDIR - UV_EINVAL => 22, // E::INVAL - UV_ENFILE => 23, // E::NFILE - UV_EMFILE => 24, // E::MFILE - UV_ENOTTY => 25, // E::NOTTY - UV_EFTYPE => 137, // E::FTYPE - UV_ETXTBSY => 26, // E::TXTBSY - UV_EFBIG => 27, // E::FBIG - UV_ENOSPC => 28, // E::NOSPC - UV_ESPIPE => 29, // E::SPIPE - UV_EROFS => 30, // E::ROFS - UV_EMLINK => 31, // E::MLINK - UV_EPIPE => 32, // E::PIPE - UV_ERANGE => 34, // E::RANGE - UV_ENAMETOOLONG => 36, // E::NAMETOOLONG - UV_ENOSYS => 38, // E::NOSYS - UV_ENOTEMPTY => 39, // E::NOTEMPTY - UV_ELOOP => 40, // E::LOOP - UV_EUNATCH => 49, // E::UNATCH - UV_ENODATA => 61, // E::NODATA - UV_ENONET => 64, // E::NONET - UV_EPROTO => 71, // E::PROTO - UV_EOVERFLOW => 75, // E::OVERFLOW - UV_EILSEQ => 84, // E::ILSEQ - UV_ENOTSOCK => 88, // E::NOTSOCK - UV_EDESTADDRREQ => 89, // E::DESTADDRREQ - UV_EMSGSIZE => 90, // E::MSGSIZE - UV_EPROTOTYPE => 91, // E::PROTOTYPE - UV_ENOPROTOOPT => 92, // E::NOPROTOOPT - UV_EPROTONOSUPPORT => 93, // E::PROTONOSUPPORT - UV_ESOCKTNOSUPPORT => 94, // E::SOCKTNOSUPPORT - UV_ENOTSUP => 95, // E::NOTSUP - UV_EAFNOSUPPORT => 97, // E::AFNOSUPPORT - UV_EADDRINUSE => 98, // E::ADDRINUSE - UV_EADDRNOTAVAIL => 99, // E::ADDRNOTAVAIL - UV_ENETDOWN => 100, // E::NETDOWN - UV_ENETUNREACH => 101, // E::NETUNREACH - UV_ECONNABORTED => 103, // E::CONNABORTED - UV_ECONNRESET => 104, // E::CONNRESET - UV_ENOBUFS => 105, // E::NOBUFS - UV_EISCONN => 106, // E::ISCONN - UV_ENOTCONN => 107, // E::NOTCONN - UV_ESHUTDOWN => 108, // E::SHUTDOWN - UV_ETIMEDOUT => 110, // E::TIMEDOUT - UV_ECONNREFUSED => 111, // E::CONNREFUSED - UV_EHOSTDOWN => 112, // E::HOSTDOWN - UV_EHOSTUNREACH => 113, // E::HOSTUNREACH - UV_EALREADY => 114, // E::ALREADY - UV_EREMOTEIO => 121, // E::REMOTEIO - UV_ECANCELED => 125, // E::CANCELED - UV_ECHARSET => 135, // E::CHARSET - UV_EOF => 136, // E::EOF - UV_UNKNOWN => 134, // E::UNKNOWN - // EAI_* codes — `bun_errno::E::UV_EAI_*` discriminants are defined as - // `(-UV_EAI_*) as u16`, i.e. the raw magnitude is the discriminant. - UV_EAI_ADDRFAMILY => (-UV_EAI_ADDRFAMILY) as u16, - UV_EAI_AGAIN => (-UV_EAI_AGAIN) as u16, - UV_EAI_BADFLAGS => (-UV_EAI_BADFLAGS) as u16, - UV_EAI_BADHINTS => (-UV_EAI_BADHINTS) as u16, - UV_EAI_CANCELED => (-UV_EAI_CANCELED) as u16, - UV_EAI_FAIL => (-UV_EAI_FAIL) as u16, - UV_EAI_FAMILY => (-UV_EAI_FAMILY) as u16, - UV_EAI_MEMORY => (-UV_EAI_MEMORY) as u16, - UV_EAI_NODATA => (-UV_EAI_NODATA) as u16, - UV_EAI_NONAME => (-UV_EAI_NONAME) as u16, - UV_EAI_OVERFLOW => (-UV_EAI_OVERFLOW) as u16, - UV_EAI_PROTOCOL => (-UV_EAI_PROTOCOL) as u16, - UV_EAI_SERVICE => (-UV_EAI_SERVICE) as u16, - UV_EAI_SOCKTYPE => (-UV_EAI_SOCKTYPE) as u16, - _ => return None, - }) -} - -/// Reverse of [`uv_err_to_e_discriminant`]: map a `bun.sys.E` / `bun_errno::E` -/// discriminant to the negative `UV_E*` code node reports in `err.errno` on -/// Windows (`2` → `UV_ENOENT (-4058)`). Same layering rule (no `bun_errno` -/// dep) and same keep-in-sync note as the forward table above; the arms are -/// its rows flipped. Unmapped discriminants return `None`. -#[inline] -pub const fn e_discriminant_to_uv(discriminant: u16) -> Option { - Some(match discriminant { - 1 => UV_EPERM, // E::PERM - 2 => UV_ENOENT, // E::NOENT - 3 => UV_ESRCH, // E::SRCH - 4 => UV_EINTR, // E::INTR - 5 => UV_EIO, // E::IO - 6 => UV_ENXIO, // E::NXIO - 7 => UV_E2BIG, // E::_2BIG - 8 => UV_ENOEXEC, // E::NOEXEC - 9 => UV_EBADF, // E::BADF - 11 => UV_EAGAIN, // E::AGAIN - 12 => UV_ENOMEM, // E::NOMEM - 13 => UV_EACCES, // E::ACCES - 14 => UV_EFAULT, // E::FAULT - 16 => UV_EBUSY, // E::BUSY - 17 => UV_EEXIST, // E::EXIST - 18 => UV_EXDEV, // E::XDEV - 19 => UV_ENODEV, // E::NODEV - 20 => UV_ENOTDIR, // E::NOTDIR - 21 => UV_EISDIR, // E::ISDIR - 22 => UV_EINVAL, // E::INVAL - 23 => UV_ENFILE, // E::NFILE - 24 => UV_EMFILE, // E::MFILE - 25 => UV_ENOTTY, // E::NOTTY - 137 => UV_EFTYPE, // E::FTYPE - 26 => UV_ETXTBSY, // E::TXTBSY - 27 => UV_EFBIG, // E::FBIG - 28 => UV_ENOSPC, // E::NOSPC - 29 => UV_ESPIPE, // E::SPIPE - 30 => UV_EROFS, // E::ROFS - 31 => UV_EMLINK, // E::MLINK - 32 => UV_EPIPE, // E::PIPE - 34 => UV_ERANGE, // E::RANGE - 36 => UV_ENAMETOOLONG, // E::NAMETOOLONG - 38 => UV_ENOSYS, // E::NOSYS - 39 => UV_ENOTEMPTY, // E::NOTEMPTY - 40 => UV_ELOOP, // E::LOOP - 49 => UV_EUNATCH, // E::UNATCH - 61 => UV_ENODATA, // E::NODATA - 64 => UV_ENONET, // E::NONET - 71 => UV_EPROTO, // E::PROTO - 75 => UV_EOVERFLOW, // E::OVERFLOW - 84 => UV_EILSEQ, // E::ILSEQ - 88 => UV_ENOTSOCK, // E::NOTSOCK - 89 => UV_EDESTADDRREQ, // E::DESTADDRREQ - 90 => UV_EMSGSIZE, // E::MSGSIZE - 91 => UV_EPROTOTYPE, // E::PROTOTYPE - 92 => UV_ENOPROTOOPT, // E::NOPROTOOPT - 93 => UV_EPROTONOSUPPORT, // E::PROTONOSUPPORT - 94 => UV_ESOCKTNOSUPPORT, // E::SOCKTNOSUPPORT - 95 => UV_ENOTSUP, // E::NOTSUP - 97 => UV_EAFNOSUPPORT, // E::AFNOSUPPORT - 98 => UV_EADDRINUSE, // E::ADDRINUSE - 99 => UV_EADDRNOTAVAIL, // E::ADDRNOTAVAIL - 100 => UV_ENETDOWN, // E::NETDOWN - 101 => UV_ENETUNREACH, // E::NETUNREACH - 103 => UV_ECONNABORTED, // E::CONNABORTED - 104 => UV_ECONNRESET, // E::CONNRESET - 105 => UV_ENOBUFS, // E::NOBUFS - 106 => UV_EISCONN, // E::ISCONN - 107 => UV_ENOTCONN, // E::NOTCONN - 108 => UV_ESHUTDOWN, // E::SHUTDOWN - 110 => UV_ETIMEDOUT, // E::TIMEDOUT - 111 => UV_ECONNREFUSED, // E::CONNREFUSED - 112 => UV_EHOSTDOWN, // E::HOSTDOWN - 113 => UV_EHOSTUNREACH, // E::HOSTUNREACH - 114 => UV_EALREADY, // E::ALREADY - 121 => UV_EREMOTEIO, // E::REMOTEIO - 125 => UV_ECANCELED, // E::CANCELED - 135 => UV_ECHARSET, // E::CHARSET - 136 => UV_EOF, // E::EOF - 134 => UV_UNKNOWN, // E::UNKNOWN - d if d == (-UV_EAI_ADDRFAMILY) as u16 => UV_EAI_ADDRFAMILY, - d if d == (-UV_EAI_AGAIN) as u16 => UV_EAI_AGAIN, - d if d == (-UV_EAI_BADFLAGS) as u16 => UV_EAI_BADFLAGS, - d if d == (-UV_EAI_BADHINTS) as u16 => UV_EAI_BADHINTS, - d if d == (-UV_EAI_CANCELED) as u16 => UV_EAI_CANCELED, - d if d == (-UV_EAI_FAIL) as u16 => UV_EAI_FAIL, - d if d == (-UV_EAI_FAMILY) as u16 => UV_EAI_FAMILY, - d if d == (-UV_EAI_MEMORY) as u16 => UV_EAI_MEMORY, - d if d == (-UV_EAI_NODATA) as u16 => UV_EAI_NODATA, - d if d == (-UV_EAI_NONAME) as u16 => UV_EAI_NONAME, - d if d == (-UV_EAI_OVERFLOW) as u16 => UV_EAI_OVERFLOW, - d if d == (-UV_EAI_PROTOCOL) as u16 => UV_EAI_PROTOCOL, - d if d == (-UV_EAI_SERVICE) as u16 => UV_EAI_SERVICE, - d if d == (-UV_EAI_SOCKTYPE) as u16 => UV_EAI_SOCKTYPE, - _ => return None, - }) -} - -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq, Eq)] -pub struct ReturnCode(pub(crate) c_int); - -// ────────────────────────────────────────────────────────────────────────── -// `bun_core::ffi::Zeroable` impls (S021). Every libuv handle/request struct -// above is `#[repr(C)]` POD whose fields are integers, raw pointers, -// `Option` callbacks, nested POD unions, or `HandleType` (a -// `#[repr(C)]` enum with `Unknown = 0`). The all-zero bit pattern is therefore -// a valid pre-`uv_*_init` state. Auditing the bound once per type here lets every -// `Box::new(zeroed())` / stack out-param site drop its `unsafe` block. -// -// SAFETY (per type): audited against the field list in this file — no -// `NonNull`/`NonZero`/reference/bare-fn-ptr fields; every enum field has a -// `= 0` discriminant (`HandleType::Unknown`, `uv_req_type`/`uv_fs_type` are -// plain `c_uint`/`c_int`). -unsafe impl bun_core::ffi::Zeroable for uv_buf_t {} -unsafe impl bun_core::ffi::Zeroable for uv_req_t {} -unsafe impl bun_core::ffi::Zeroable for uv_write_t {} -unsafe impl bun_core::ffi::Zeroable for uv_connect_t {} -unsafe impl bun_core::ffi::Zeroable for Handle {} -unsafe impl bun_core::ffi::Zeroable for Timer {} -unsafe impl bun_core::ffi::Zeroable for Pipe {} -unsafe impl bun_core::ffi::Zeroable for uv_idle_t {} -unsafe impl bun_core::ffi::Zeroable for uv_poll_t {} -unsafe impl bun_core::ffi::Zeroable for uv_fs_event_t {} -unsafe impl bun_core::ffi::Zeroable for uv_getaddrinfo_t {} -unsafe impl bun_core::ffi::Zeroable for uv_tty_t {} -unsafe impl bun_core::ffi::Zeroable for fs_t {} -unsafe impl bun_core::ffi::Zeroable for uv_stat_t {} -impl ReturnCode { - pub const ZERO: ReturnCode = ReturnCode(0); - #[inline] - pub const fn zero() -> ReturnCode { - ReturnCode(0) - } - #[inline] - pub const fn int(self) -> c_int { - self.0 - } - /// `Some(|UV_E*|)` when negative — the **raw** libuv error magnitude - /// (e.g. 4082 for `UV_EBUSY`), for logging. `bun_sys::ReturnCodeExt` - /// has the errno translation. - #[inline] - pub(crate) const fn raw_errno(self) -> Option { - if self.0 < 0 { - Some(self.0.unsigned_abs() as u16) - } else { - None - } - } - /// Layer-free `< 0` check. - /// For the tagged `bun_sys::Error` use [`ReturnCodeExt::to_error`]. - #[inline] - pub const fn is_err(self) -> bool { - self.0 < 0 - } -} -impl fmt::Debug for ReturnCode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} -impl fmt::Display for ReturnCode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub struct ReturnCodeI64(pub(crate) i64); -impl ReturnCodeI64 { - #[inline] - pub const fn int(self) -> i64 { - self.0 - } - /// `req.result` after a successful `uv_fs_open` is the - /// CRT fd. Returns the raw `uv_file`; caller wraps with `Fd::from_uv` - /// (`bun_core::Fd` is a higher-tier type). - #[inline] - pub fn to_fd(self) -> uv_file { - debug_assert!(self.0 >= 0); - self.0 as uv_file - } -} -impl fmt::Display for ReturnCodeI64 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -// ────────────────────────────────────────────────────────────────────────── -// `O` — `UV_FS_O_*` flag namespace + `from_bun_o`/`to_bun_o` translation. -// The `bun.O.*` POSIX-like values are passed in by the -// caller as a raw `i32` (Bun's `O` on Windows normalises to Linux-style -// octal constants, NOT MSVC `libc::O_*`); -// these fns map to/from libuv's MSVC `_O_*` values that `uv_fs_open` expects. -// ────────────────────────────────────────────────────────────────────────── -pub mod O { - pub(crate) const APPEND: i32 = 0x0008; - pub const CREAT: i32 = 0x0100; - pub(crate) const EXCL: i32 = 0x0400; - pub(crate) const FILEMAP: i32 = 0x2000_0000; - pub const RANDOM: i32 = 0x0010; - pub const RDONLY: i32 = 0x0000; - pub const RDWR: i32 = 0x0002; - pub const SEQUENTIAL: i32 = 0x0020; - pub const SHORT_LIVED: i32 = 0x1000; - pub const TEMPORARY: i32 = 0x0040; - pub const TRUNC: i32 = 0x0200; - pub const WRONLY: i32 = 0x0001; - pub(crate) const DIRECT: i32 = 0x0200_0000; - pub(crate) const DSYNC: i32 = 0x0400_0000; - pub(crate) const SYNC: i32 = 0x0800_0000; - // No-ops on Windows. - pub const DIRECTORY: i32 = 0; - pub const EXLOCK: i32 = 0x1000_0000; - pub const NOATIME: i32 = 0; - pub const NOCTTY: i32 = 0; - pub(crate) const NOFOLLOW: i32 = 0; - pub const NONBLOCK: i32 = 0; - pub const SYMLINK: i32 = 0; - - // `bun.O.*` — POSIX-shaped flag values Bun normalises to internally. - // - // ⚠ These are hard-coded Linux-style octal constants. They do **NOT** match - // `bun_sys::O` if that crate is built against MSVC `libc::O_*` - // (CREAT=0x100, EXCL=0x400, APPEND=0x8). `bun_sys::O` on Windows must - // mirror these values — cross-crate static asserts live in `bun_sys` (this - // crate stays leaf). The constants are stable. - mod bun_o { - pub(super) const WRONLY: i32 = 0o1; - pub(super) const RDWR: i32 = 0o2; - pub(super) const CREAT: i32 = 0o100; - pub(super) const EXCL: i32 = 0o200; - pub(super) const TRUNC: i32 = 0o1000; - pub(super) const APPEND: i32 = 0o2000; - pub(super) const NONBLOCK: i32 = 0o4000; - pub(super) const DSYNC: i32 = 0o10000; - pub(super) const DIRECT: i32 = 0o40000; - pub(super) const NOFOLLOW: i32 = 0o400000; - pub(super) const SYNC: i32 = 0o4010000; - } - - /// Convert from internal `bun.O` flags to libuv/Windows flags. - pub fn from_bun_o(c_flags: i32) -> i32 { - let mut flags: i32 = 0; - if c_flags & bun_o::WRONLY != 0 { - flags |= WRONLY; - } - if c_flags & bun_o::RDWR != 0 { - flags |= RDWR; - } - if c_flags & bun_o::CREAT != 0 { - flags |= CREAT; - } - if c_flags & bun_o::EXCL != 0 { - flags |= EXCL; - } - if c_flags & bun_o::TRUNC != 0 { - flags |= TRUNC; - } - if c_flags & bun_o::APPEND != 0 { - flags |= APPEND; - } - if c_flags & bun_o::NONBLOCK != 0 { - flags |= NONBLOCK; - } - // SYNC and DSYNC must be mutually exclusive for libuv on Windows. - // `bun.O.SYNC` (0o4010000) is a superset of `DSYNC` (0o10000), so check - // SYNC first to emit only `UV_FS_O_SYNC` when both bits are present. - // NOTE: `& != 0` is an any-overlap check; - // a DSYNC-only input also takes this branch. - if c_flags & bun_o::SYNC != 0 { - flags |= SYNC; - } else if c_flags & bun_o::DSYNC != 0 { - flags |= DSYNC; - } - if c_flags & bun_o::NOFOLLOW != 0 { - flags |= NOFOLLOW; - } - if c_flags & bun_o::DIRECT != 0 { - flags |= DIRECT; - } - if c_flags & FILEMAP != 0 { - flags |= FILEMAP; - } - flags - } - - /// Convert from libuv/Windows MSVC `_O_*` flags to internal `bun.O` flags. - /// Inverse of [`from_bun_o`]; needed because `fs.constants` exposes the - /// platform's native C values to JavaScript, but internally Bun normalises - /// all flags to the `bun.O` (POSIX-like) representation. - pub fn to_bun_o(uv_flags: i32) -> i32 { - let mut flags: i32 = 0; - if uv_flags & WRONLY != 0 { - flags |= bun_o::WRONLY; - } - if uv_flags & RDWR != 0 { - flags |= bun_o::RDWR; - } - if uv_flags & CREAT != 0 { - flags |= bun_o::CREAT; - } - if uv_flags & EXCL != 0 { - flags |= bun_o::EXCL; - } - if uv_flags & TRUNC != 0 { - flags |= bun_o::TRUNC; - } - if uv_flags & APPEND != 0 { - flags |= bun_o::APPEND; - } - if uv_flags & SYNC != 0 { - flags |= bun_o::SYNC; - } else if uv_flags & DSYNC != 0 { - flags |= bun_o::DSYNC; - } - if uv_flags & DIRECT != 0 { - flags |= bun_o::DIRECT; - } - if uv_flags & FILEMAP != 0 { - flags |= FILEMAP; - } - flags - } -} - -// ────────────────────────────────────────────────────────────────────────── -// Error constants (uv-errno.h, Windows values). -// ────────────────────────────────────────────────────────────────────────── -pub const UV__EOF: c_int = -4095; -pub const UV__UNKNOWN: c_int = -4094; -pub const UV__ECHARSET: c_int = -4080; - -pub const UV_E2BIG: c_int = -4093; -pub const UV_EACCES: c_int = -4092; -pub const UV_EADDRINUSE: c_int = -4091; -pub const UV_EADDRNOTAVAIL: c_int = -4090; -pub const UV_EAFNOSUPPORT: c_int = -4089; -pub const UV_EAGAIN: c_int = -4088; -pub const UV_EAI_ADDRFAMILY: c_int = -3000; -pub const UV_EAI_AGAIN: c_int = -3001; -pub const UV_EAI_BADFLAGS: c_int = -3002; -pub const UV_EAI_BADHINTS: c_int = -3013; -pub const UV_EAI_CANCELED: c_int = -3003; -pub const UV_EAI_FAIL: c_int = -3004; -pub const UV_EAI_FAMILY: c_int = -3005; -pub const UV_EAI_MEMORY: c_int = -3006; -pub const UV_EAI_NODATA: c_int = -3007; -pub const UV_EAI_NONAME: c_int = -3008; -pub const UV_EAI_OVERFLOW: c_int = -3009; -pub const UV_EAI_PROTOCOL: c_int = -3014; -pub const UV_EAI_SERVICE: c_int = -3010; -pub const UV_EAI_SOCKTYPE: c_int = -3011; -pub const UV_EALREADY: c_int = -4084; -pub const UV_EBADF: c_int = -4083; -pub const UV_EBUSY: c_int = -4082; -pub const UV_ECANCELED: c_int = -4081; -pub const UV_ECHARSET: c_int = -4080; -pub const UV_ECONNABORTED: c_int = -4079; -pub const UV_ECONNREFUSED: c_int = -4078; -pub const UV_ECONNRESET: c_int = -4077; -pub const UV_EDESTADDRREQ: c_int = -4076; -pub const UV_EEXIST: c_int = -4075; -pub const UV_EFAULT: c_int = -4074; -pub const UV_EFBIG: c_int = -4036; -pub const UV_EHOSTUNREACH: c_int = -4073; -pub const UV_EINTR: c_int = -4072; -pub const UV_EINVAL: c_int = -4071; -pub const UV_EIO: c_int = -4070; -pub const UV_EISCONN: c_int = -4069; -pub const UV_EISDIR: c_int = -4068; -pub const UV_ELOOP: c_int = -4067; -pub const UV_EMFILE: c_int = -4066; -pub const UV_EMSGSIZE: c_int = -4065; -pub const UV_ENAMETOOLONG: c_int = -4064; -pub const UV_ENETDOWN: c_int = -4063; -pub const UV_ENETUNREACH: c_int = -4062; -pub const UV_ENFILE: c_int = -4061; -pub const UV_ENOBUFS: c_int = -4060; -pub const UV_ENODEV: c_int = -4059; -pub const UV_ENOENT: c_int = -4058; -pub const UV_ENOMEM: c_int = -4057; -pub const UV_ENONET: c_int = -4056; -pub const UV_ENOPROTOOPT: c_int = -4035; -pub const UV_ENOSPC: c_int = -4055; -pub const UV_ENOSYS: c_int = -4054; -pub const UV_ENOTCONN: c_int = -4053; -pub const UV_ENOTDIR: c_int = -4052; -pub const UV_ENOTEMPTY: c_int = -4051; -pub const UV_ENOTSOCK: c_int = -4050; -pub const UV_ENOTSUP: c_int = -4049; -pub const UV_EOVERFLOW: c_int = -4026; -pub const UV_EPERM: c_int = -4048; -pub const UV_EPIPE: c_int = -4047; -pub const UV_EPROTO: c_int = -4046; -pub const UV_EPROTONOSUPPORT: c_int = -4045; -pub const UV_EPROTOTYPE: c_int = -4044; -pub const UV_ERANGE: c_int = -4034; -pub const UV_EROFS: c_int = -4043; -pub const UV_ESHUTDOWN: c_int = -4042; -pub const UV_ESPIPE: c_int = -4041; -pub const UV_ESRCH: c_int = -4040; -pub const UV_ETIMEDOUT: c_int = -4039; -pub const UV_ETXTBSY: c_int = -4038; -pub const UV_EXDEV: c_int = -4037; -pub const UV_UNKNOWN: c_int = -4094; -pub const UV_EOF: c_int = -4095; -pub const UV_ENXIO: c_int = -4033; -pub const UV_EMLINK: c_int = -4032; -pub const UV_EHOSTDOWN: c_int = -4031; -pub const UV_EREMOTEIO: c_int = -4030; -pub const UV_ENOTTY: c_int = -4029; -pub const UV_EFTYPE: c_int = -4028; -pub const UV_EILSEQ: c_int = -4027; -pub const UV_ESOCKTNOSUPPORT: c_int = -4025; -pub const UV_ENODATA: c_int = -4024; -pub const UV_EUNATCH: c_int = -4023; -pub const UV_ENOEXEC: c_int = -4022; -pub const UV_ERRNO_MAX: c_int = -4096; - -// `uv_dirent_type_t` discriminants — compared against -// `uv_dirent_t.type_` by Windows `fs.readdir`. -pub const UV_DIRENT_UNKNOWN: c_int = 0; -pub const UV_DIRENT_FILE: c_int = 1; -pub const UV_DIRENT_DIR: c_int = 2; -pub const UV_DIRENT_LINK: c_int = 3; -pub const UV_DIRENT_FIFO: c_int = 4; -pub const UV_DIRENT_SOCKET: c_int = 5; -pub const UV_DIRENT_CHAR: c_int = 6; -pub const UV_DIRENT_BLOCK: c_int = 7; - -// Misc flag constants. -pub const UV_READABLE: c_int = 1; -pub const UV_WRITABLE: c_int = 2; -pub const UV_DISCONNECT: c_int = 4; -pub const UV_PRIORITIZED: c_int = 8; -pub(crate) const UV_PIPE_NO_TRUNCATE: c_uint = 1; -pub const UV_FS_SYMLINK_DIR: c_int = 0x0001; -pub const UV_FS_SYMLINK_JUNCTION: c_int = 0x0002; -pub const UV_RENAME: c_int = 1; -pub const UV_CHANGE: c_int = 2; -pub const UV_FS_EVENT_WATCH_ENTRY: c_int = 1; -pub const UV_FS_EVENT_STAT: c_int = 2; -pub const UV_FS_EVENT_RECURSIVE: c_int = 4; - -// Stdio / process flags. -pub const UV_IGNORE: c_uint = 0; -pub const UV_CREATE_PIPE: c_uint = 1; -pub const UV_INHERIT_FD: c_uint = 2; -pub const UV_INHERIT_STREAM: c_uint = 4; -pub const UV_READABLE_PIPE: c_uint = 16; -pub const UV_WRITABLE_PIPE: c_uint = 32; -pub const UV_NONBLOCK_PIPE: c_uint = 64; -pub const UV_OVERLAPPED_PIPE: c_uint = 64; -// `uv_process_flags` — `c_uint` to match `uv_process_options_t.flags` so -// `flags |= UV_PROCESS_*` typechecks (uv.h declares the enum unsigned). -pub const UV_PROCESS_SETUID: c_uint = 1; -pub const UV_PROCESS_SETGID: c_uint = 2; -pub const UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS: c_uint = 4; -pub const UV_PROCESS_DETACHED: c_uint = 8; -pub const UV_PROCESS_WINDOWS_HIDE: c_uint = 16; -pub const UV_PROCESS_WINDOWS_HIDE_CONSOLE: c_uint = 32; -pub const UV_PROCESS_WINDOWS_HIDE_GUI: c_uint = 64; - -pub const SIGHUP: c_int = 1; -pub const SIGQUIT: c_int = 3; -pub const SIGKILL: c_int = 9; -pub const SIGWINCH: c_int = 28; - -pub(crate) const UV_HANDLE_CLOSED: c_uint = 0x0000_0002; - -/// Non-ABI helper: `flags & UV_HANDLE_CLOSED != 0`. -#[inline] -pub(crate) fn uv_is_closed(handle: &uv_handle_t) -> bool { - handle.flags & UV_HANDLE_CLOSED != 0 -} - -// ────────────────────────────────────────────────────────────────────────── -// extern "C" — full libuv surface Bun uses. -// ────────────────────────────────────────────────────────────────────────── -unsafe extern "C" { - // version / loop - pub fn uv_replace_allocator( - malloc_func: uv_malloc_func, - realloc_func: uv_realloc_func, - calloc_func: uv_calloc_func, - free_func: uv_free_func, - ) -> c_int; - pub fn uv_default_loop() -> *mut Loop; - pub fn uv_loop_init(loop_: *mut Loop) -> ReturnCode; - pub fn uv_loop_close(loop_: *mut Loop) -> ReturnCode; - pub fn uv_loop_alive(loop_: *const Loop) -> c_int; - pub fn uv_run(loop_: *mut Loop, mode: RunMode) -> c_int; - pub fn uv_stop(loop_: *mut Loop); - pub fn uv_ref(handle: *mut uv_handle_t); - pub fn uv_unref(handle: *mut uv_handle_t); - pub fn uv_has_ref(handle: *const uv_handle_t) -> c_int; - pub fn uv_update_time(loop_: *mut Loop); - pub fn uv_now(loop_: *const Loop) -> u64; - - // handle/req - pub fn uv_handle_size(type_: uv_handle_type) -> usize; - pub fn uv_handle_get_type(handle: *const uv_handle_t) -> uv_handle_type; - pub fn uv_handle_type_name(type_: uv_handle_type) -> *const c_char; - pub fn uv_handle_get_data(handle: *const uv_handle_t) -> *mut c_void; - pub fn uv_handle_get_loop(handle: *const uv_handle_t) -> *mut Loop; - pub fn uv_handle_set_data(handle: *mut uv_handle_t, data: *mut c_void); - pub fn uv_is_active(handle: *const uv_handle_t) -> c_int; - pub fn uv_walk(loop_: *mut Loop, walk_cb: uv_walk_cb, arg: *mut c_void); - pub fn uv_close(handle: *mut uv_handle_t, close_cb: uv_close_cb); - pub fn uv_fileno(handle: *const uv_handle_t, fd: *mut uv_os_fd_t) -> c_int; - pub fn uv_buf_init(base: *mut c_char, len: c_uint) -> uv_buf_t; - pub fn uv_pipe(fds: *mut [uv_file; 2], read_flags: c_int, write_flags: c_int) -> ReturnCode; - pub fn uv_is_closing(handle: *const uv_handle_t) -> c_int; - pub fn uv_cancel(req: *mut uv_req_t) -> c_int; - - // stream - pub fn uv_stream_get_write_queue_size(stream: *const uv_stream_t) -> usize; - pub fn uv_listen(stream: *mut uv_stream_t, backlog: c_int, cb: uv_connection_cb) -> ReturnCode; - pub fn uv_accept(server: *mut uv_stream_t, client: *mut uv_stream_t) -> ReturnCode; - pub fn uv_read_start( - stream: *mut uv_stream_t, - alloc_cb: uv_alloc_cb, - read_cb: uv_read_cb, - ) -> ReturnCode; - pub fn uv_read_stop(stream: *mut uv_stream_t) -> ReturnCode; - pub fn uv_write( - req: *mut uv_write_t, - handle: *mut uv_stream_t, - bufs: *const uv_buf_t, - nbufs: c_uint, - cb: uv_write_cb, - ) -> ReturnCode; - pub fn uv_try_write( - handle: *mut uv_stream_t, - bufs: *const uv_buf_t, - nbufs: c_uint, - ) -> ReturnCode; - pub fn uv_is_readable(handle: *const uv_stream_t) -> c_int; - pub fn uv_is_writable(handle: *const uv_stream_t) -> c_int; - pub fn uv_stream_set_blocking(handle: *mut uv_stream_t, blocking: c_int) -> ReturnCode; - - // tty - pub fn uv_tty_init( - loop_: *mut Loop, - handle: *mut uv_tty_t, - fd: uv_file, - readable: c_int, - ) -> ReturnCode; - pub fn uv_tty_set_mode(handle: *mut uv_tty_t, mode: uv_tty_mode_t) -> ReturnCode; - /// Raw `uv_guess_handle`. Declared `-> c_int` (not [`HandleType`]) because - /// returning a `#[repr(C)]` enum from a `safe extern fn` is a soundness - /// hole: any out-of-range discriminant (e.g. `UV_HANDLE_TYPE_MAX = 18`, or - /// a future libuv variant) would be instant UB with no `unsafe` at the call - /// site. The range-checked safe wrapper is [`uv_guess_handle`]. - #[link_name = "uv_guess_handle"] - pub safe fn uv_guess_handle_raw(file: uv_file) -> c_int; - - // pipe - pub fn uv_pipe_init(loop_: *mut Loop, handle: *mut Pipe, ipc: c_int) -> ReturnCode; - pub fn uv_pipe_open(handle: *mut Pipe, file: uv_file) -> ReturnCode; - pub fn uv_pipe_bind2( - handle: *mut Pipe, - name: *const u8, - namelen: usize, - flags: c_uint, - ) -> ReturnCode; - pub fn uv_pipe_connect2( - req: *mut uv_connect_t, - handle: *mut Pipe, - name: *const u8, - namelen: usize, - flags: c_uint, - cb: uv_connect_cb, - ) -> ReturnCode; - - // poll - pub fn uv_poll_init_socket( - loop_: *mut Loop, - handle: *mut uv_poll_t, - socket: uv_os_sock_t, - ) -> c_int; - pub fn uv_poll_start(handle: *mut uv_poll_t, events: c_int, cb: uv_poll_cb) -> c_int; - - // idle/async - pub fn uv_idle_init(loop_: *mut Loop, idle: *mut uv_idle_t) -> c_int; - pub fn uv_idle_start(idle: *mut uv_idle_t, cb: uv_idle_cb) -> c_int; - pub fn uv_idle_stop(idle: *mut uv_idle_t) -> c_int; - - // timer - pub fn uv_timer_init(loop_: *mut Loop, handle: *mut Timer) -> c_int; - pub fn uv_timer_start(handle: *mut Timer, cb: uv_timer_cb, timeout: u64, repeat: u64) -> c_int; - pub fn uv_timer_stop(handle: *mut Timer) -> c_int; - pub fn uv_timer_get_due_in(handle: *const Timer) -> u64; - - /// Winsock is initialized on first use inside libuv; anything that calls - /// ws2_32 directly (or through c-ares/uSockets) must call this first. - /// Thread-safe and idempotent (uv_once). - pub fn uv__winsock_ensure(); - - // dns - pub fn uv_getaddrinfo( - loop_: *mut Loop, - req: *mut uv_getaddrinfo_t, - getaddrinfo_cb: uv_getaddrinfo_cb, - node: *const c_char, - service: *const c_char, - hints: *const c_void, - ) -> ReturnCode; - pub fn uv_freeaddrinfo(ai: *mut c_void); - - // process - pub fn uv_spawn( - loop_: *mut Loop, - handle: *mut Process, - options: *const uv_process_options_t, - ) -> ReturnCode; - pub fn uv_process_kill(handle: *mut Process, signum: c_int) -> ReturnCode; - - // misc - pub fn uv_uptime(uptime: *mut f64) -> c_int; - pub fn uv_getrusage(rusage: *mut uv_rusage_t) -> c_int; - pub fn uv_os_homedir(buffer: *mut u8, size: *mut usize) -> ReturnCode; - pub fn uv_os_getppid() -> uv_pid_t; - pub fn uv_os_getpriority(pid: uv_pid_t, priority: *mut c_int) -> c_int; - pub fn uv_translate_sys_error(sys_errno: c_int) -> c_int; - pub fn uv_cpu_info(cpu_infos: *mut *mut uv_cpu_info_t, count: *mut c_int) -> c_int; - pub fn uv_free_cpu_info(cpu_infos: *mut uv_cpu_info_t, count: c_int); - pub fn uv_interface_addresses( - addresses: *mut *mut uv_interface_address_t, - count: *mut c_int, - ) -> c_int; - pub fn uv_free_interface_addresses(addresses: *mut uv_interface_address_t, count: c_int); - pub fn uv_os_uname(buffer: *mut uv_utsname_t) -> c_int; - pub fn uv_cwd(buffer: *mut c_char, size: *mut usize) -> c_int; - pub fn uv_get_total_memory() -> u64; - pub fn uv_disable_stdio_inheritance(); - - // fs - pub fn uv_fs_req_cleanup(req: *mut fs_t); - pub fn uv_fs_close(loop_: *mut Loop, req: *mut fs_t, file: uv_file, cb: uv_fs_cb) - -> ReturnCode; - pub fn uv_fs_open( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - flags: c_int, - mode: c_int, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_read( - loop_: *mut Loop, - req: *mut fs_t, - file: uv_file, - bufs: *const uv_buf_t, - nbufs: c_uint, - offset: i64, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_unlink( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_write( - loop_: *mut Loop, - req: *mut fs_t, - file: uv_file, - bufs: *const uv_buf_t, - nbufs: c_uint, - offset: i64, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_copyfile( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - new_path: *const c_char, - flags: c_int, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_mkdir( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - mode: c_int, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_mkdtemp( - loop_: *mut Loop, - req: *mut fs_t, - tpl: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_rmdir( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_stat( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_fstat(loop_: *mut Loop, req: *mut fs_t, file: uv_file, cb: uv_fs_cb) - -> ReturnCode; - pub fn uv_fs_rename( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - new_path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_fsync(loop_: *mut Loop, req: *mut fs_t, file: uv_file, cb: uv_fs_cb) - -> ReturnCode; - pub fn uv_fs_fdatasync( - loop_: *mut Loop, - req: *mut fs_t, - file: uv_file, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_ftruncate( - loop_: *mut Loop, - req: *mut fs_t, - file: uv_file, - offset: i64, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_chmod( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - mode: c_int, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_utime( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - atime: f64, - mtime: f64, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_futime( - loop_: *mut Loop, - req: *mut fs_t, - file: uv_file, - atime: f64, - mtime: f64, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_lutime( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - atime: f64, - mtime: f64, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_lstat( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_link( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - new_path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_symlink( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - new_path: *const c_char, - flags: c_int, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_readlink( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_realpath( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_fchmod( - loop_: *mut Loop, - req: *mut fs_t, - file: uv_file, - mode: c_int, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_chown( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - uid: uv_uid_t, - gid: uv_gid_t, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_fchown( - loop_: *mut Loop, - req: *mut fs_t, - file: uv_file, - uid: uv_uid_t, - gid: uv_gid_t, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_lchown( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - uid: uv_uid_t, - gid: uv_gid_t, - cb: uv_fs_cb, - ) -> ReturnCode; - pub fn uv_fs_statfs( - loop_: *mut Loop, - req: *mut fs_t, - path: *const c_char, - cb: uv_fs_cb, - ) -> ReturnCode; - - // fs_event / signal - pub fn uv_signal_init(loop_: *mut Loop, handle: *mut uv_signal_t) -> ReturnCode; - pub fn uv_signal_start( - handle: *mut uv_signal_t, - signal_cb: uv_signal_cb, - signum: c_int, - ) -> ReturnCode; - pub fn uv_signal_stop(handle: *mut uv_signal_t) -> ReturnCode; - pub fn uv_fs_event_init(loop_: *mut Loop, handle: *mut uv_fs_event_t) -> ReturnCode; - pub fn uv_fs_event_start( - handle: *mut uv_fs_event_t, - cb: uv_fs_event_cb, - path: *const c_char, - flags: c_uint, - ) -> ReturnCode; - pub fn uv_fs_event_stop(handle: *mut uv_fs_event_t) -> c_int; -} - -// ────────────────────────────────────────────────────────────────────────── -// Layout assertions (Windows x64). -// -// Authoritative `sizeof`s taken from a `cl.exe`-compiled probe against -// `vendor/libuv/include` (uv 1.51.0). If any of these fire, the field-order -// above has drifted from the C header. -// ────────────────────────────────────────────────────────────────────────── -#[cfg(all(target_arch = "x86_64", target_os = "windows"))] -const _: () = { - macro_rules! assert_size { - ($t:ty, $n:expr) => { - assert!( - mem::size_of::<$t>() == $n, - concat!("layout drift: sizeof(", stringify!($t), ")") - ); - }; - } - macro_rules! assert_offset { - ($t:ty, $f:ident, $n:expr) => { - assert!( - mem::offset_of!($t, $f) == $n, - concat!( - "layout drift: offsetof(", - stringify!($t), - ".", - stringify!($f), - ")" - ) - ); - }; - } - - // Win32 primitives. - assert_size!(OVERLAPPED, 32); - assert_size!(CRITICAL_SECTION, 40); - assert_size!(INPUT_RECORD, 20); - assert_size!(sockaddr_storage, 128); - assert_size!(sockaddr_in, 16); - assert_size!(sockaddr_in6, 28); - assert_size!(addrinfo, 48); - - // Platform-invariant uv types. - assert_size!(uv_buf_t, 16); - assert_size!(uv__queue, 16); - assert_size!(uv__work, 40); - assert_size!(uv_timespec_t, 8); - assert_size!(uv_stat_t, 128); - assert_size!(uv_statfs_t, 88); - assert_size!(uv_utsname_t, 256 * 4); - assert_size!(uv_rusage_t, 128); - assert_size!(uv_cpu_info_t, 56); - assert_size!(uv_interface_address_t, 80); - - // `UV_REQ_FIELDS` header — every req-derived struct shares this prefix, - // so asserting `uv_req_t` and field offsets covers all of them. - assert_size!(req_u, 40); - assert_size!(uv_req_t, 112); - assert_offset!(uv_req_t, data, 0); - assert_offset!(uv_req_t, u, 64); - assert_offset!(uv_req_t, next_req, 104); - assert_size!(uv_read_t, 128); - assert_size!(uv_shutdown_t, 128); - assert_size!(uv_connect_t, 128); - assert_size!(uv_write_t, 176); - assert_size!(uv_process_exit_t, 112); - assert_size!(uv_udp_send_t, 128); - - // `UV_HANDLE_FIELDS` header. - assert_size!(handle_u, 32); - assert_size!(Handle, 96); - assert_offset!(Handle, data, 0); - assert_offset!(Handle, loop_, 8); - assert_offset!(Handle, type_, 16); - assert_offset!(Handle, close_cb, 24); - assert_offset!(Handle, handle_queue, 32); - assert_offset!(Handle, u, 48); - assert_offset!(Handle, endgame_next, 80); - assert_offset!(Handle, flags, 88); - - // `UV_STREAM_FIELDS` header. - assert_size!(uv_stream_t, 272); - assert_offset!(uv_stream_t, write_queue_size, 96); - assert_offset!(uv_stream_t, alloc_cb, 104); - assert_offset!(uv_stream_t, read_cb, 112); - assert_offset!(uv_stream_t, reqs_pending, 120); - assert_offset!(uv_stream_t, activecnt, 124); - assert_offset!(uv_stream_t, read_req, 128); - - // Concrete handles. Sizes here are derived from the field list above; they - // serve as a tripwire against accidental field reordering. (Full - // cross-validation against `uv_handle_size()` happens at runtime in - // debug builds via `bun_sys::windows::assert_uv_layout()`.) - assert_size!(Timer, 160); - assert_size!(uv_prepare_t, 120); - assert_size!(uv_check_t, 120); - assert_size!(uv_idle_t, 120); - assert_size!(uv_async_t, 224); - assert_size!(uv_fs_poll_t, 104); - assert_offset!(Loop, active_handles, 8); - assert_offset!(Loop, iocp, 56); - assert_offset!(Loop, wq_async, 248); - - // Reqs with payload. - assert_size!(uv_work_t, 176); - assert_size!(fs_t, 456); - assert_offset!(fs_t, fs_type, 112); - assert_offset!(fs_t, loop_, 120); - assert_offset!(fs_t, result, 136); - assert_offset!(fs_t, ptr, 144); - assert_offset!(fs_t, path, 152); - assert_offset!(fs_t, statbuf, 160); - assert_offset!(fs_t, work_req, 288); - assert_offset!(fs_t, flags, 328); - - // `UvHandle` trait invariant: every implementor has `data` at offset 0. - assert_offset!(Pipe, data, 0); - assert_offset!(uv_tcp_t, data, 0); - assert_offset!(uv_tty_t, data, 0); - assert_offset!(Timer, data, 0); - assert_offset!(uv_async_t, data, 0); - assert_offset!(Process, data, 0); - assert_offset!(uv_signal_t, data, 0); - assert_offset!(uv_poll_t, data, 0); - assert_offset!(uv_udp_t, data, 0); -}; diff --git a/src/libuv_sys/open_handles.rs b/src/libuv_sys/open_handles.rs deleted file mode 100644 index b12c5472c5af..000000000000 --- a/src/libuv_sys/open_handles.rs +++ /dev/null @@ -1,228 +0,0 @@ -//! Registry of the uv handles Bun itself opened on this thread's loop (pipes, -//! ttys, processes …), each with the owner that drives it, so a worker's -//! teardown can close every one *through its owner* before the loop is closed -//! — pending writes then complete (ECANCELED) against a live VM and nothing is -//! left for `uv_loop_close` to trip over. -//! -//! This is Node's mechanism: every `HandleWrap` links itself into -//! `Environment::handle_wrap_queue_` (node/src/handle_wrap.h, env.h) at -//! construction and `Environment::CleanupHandles()` (node/src/env.cc) walks that -//! list calling `Close()`; `uv_walk` alone is not enough because it yields bare -//! `uv_handle_t*`s with no typed owner to close through. Insert on open, remove -//! on close — both keyed by address, so neither costs more as handles pile up. -//! -//! A reader over a *file* has no handle — its `uv_fs_read` is a request, which -//! cannot be closed and completes only when the loop is drained — so the reader -//! lists the boxed `File` it reads through (`add_file`, a stable address however -//! the reader itself moves) with itself as owner; the stop phase closes it the -//! same way, and the drained completion then finds a closed reader instead of a -//! parent that is gone or may no longer run script. - -use super::*; - -use core::cell::RefCell; -use std::collections::HashMap; - -/// How a teardown closes a handle that has an owner: `close(owner)`. -pub type CloseViaOwner = unsafe fn(owner: *mut c_void); - -struct Entry { - kind: Kind, - owner: *mut c_void, - close_via_owner: Option, -} - -#[derive(Clone, Copy, PartialEq, Eq)] -enum Kind { - Pipe, - Tty, - Process, -} - -/// A file a reader holds: closed through that reader. -struct FileEntry { - owner: *mut c_void, - close_via_owner: Option, -} - -#[derive(Default)] -struct Open { - /// By handle address. - handles: HashMap<*mut uv_handle_t, Entry>, - /// By the boxed `File`'s address (what the reader's `Source` owns). - files: HashMap<*mut c_void, FileEntry>, -} - -std::thread_local! { - static OPEN: RefCell = RefCell::new(Open::default()); -} - -fn add(handle: *mut uv_handle_t, kind: Kind) { - OPEN.with(|o| { - let previous = o.borrow_mut().handles.insert( - handle, - Entry { - kind, - owner: ptr::null_mut(), - close_via_owner: None, - }, - ); - debug_assert!(previous.is_none(), "uv handle registered twice"); - }); -} - -pub(super) fn add_pipe(p: *mut Pipe) { - add(p.cast(), Kind::Pipe); -} -pub(super) fn add_tty(t: *mut uv_tty_t) { - add(t.cast(), Kind::Tty); -} -pub(super) fn add_process(p: *mut Process) { - add(p.cast(), Kind::Process); -} - -/// The `uv_close` for `handle` has been (or is about to be) issued. -pub(super) fn remove(handle: *mut uv_handle_t) { - OPEN.with(|o| { - o.borrow_mut().handles.remove(&handle); - }); -} - -/// A reader took a file as its source: list the boxed `File` (its address is -/// stable however the reader moves). The owner follows via [`set_file_owner`]; -/// [`remove_file`] when the reader hands the box to libuv or drops it. -pub fn add_file(file: *mut c_void) { - OPEN.with(|o| { - o.borrow_mut().files.entry(file).or_insert(FileEntry { - owner: ptr::null_mut(), - close_via_owner: None, - }); - }); -} - -pub fn remove_file(file: *mut c_void) { - OPEN.with(|o| { - o.borrow_mut().files.remove(&file); - }); -} - -/// The reader that now drives `file` (called again whenever that reader -/// settles at a new address). No-op for a file no reader listed — writers -/// share `Source::set_owner` and never list. -pub fn set_file_owner(file: *mut c_void, owner: *mut c_void, close: CloseViaOwner) { - OPEN.with(|o| { - if let Some(e) = o.borrow_mut().files.get_mut(&file) { - e.owner = owner; - e.close_via_owner = if owner.is_null() { None } else { Some(close) }; - } - }); -} - -/// `owner` now drives `handle` and closes it via `close(owner)`; pass a -/// null `owner` to clear. No-op for handles not listed (never initialised -/// on this thread, already closing, or the process-static stdin tty). -pub fn set_owner(handle: *mut uv_handle_t, owner: *mut c_void, close: Option) { - OPEN.with(|o| { - if let Some(e) = o.borrow_mut().handles.get_mut(&handle) { - e.owner = owner; - e.close_via_owner = if owner.is_null() { None } else { close }; - } - }); -} - -#[cfg(debug_assertions)] -pub fn count() -> usize { - OPEN.with(|o| { - let o = o.borrow(); - o.handles.len() + o.files.len() - }) -} - -enum Next { - Handle(*mut uv_handle_t, Entry), - File(*mut c_void, FileEntry), -} - -/// One entry out of the registry, if any is left. Owners may close other -/// handles from their callbacks, so the stop phase takes one at a time and -/// never holds the borrow across a close. -fn take_next() -> Option { - OPEN.with(|o| { - let mut o = o.borrow_mut(); - if let Some(&file) = o.files.keys().next() { - let entry = o.files.remove(&file).unwrap(); - return Some(Next::File(file, entry)); - } - let &handle = o.handles.keys().next()?; - let entry = o.handles.remove(&handle).unwrap(); - Some(Next::Handle(handle, entry)) - }) -} - -/// Thread teardown's stop phase (VM alive, script forbidden): close every open pipe / -/// tty / process handle and every reader mid file-read — through its owner when -/// it has one, directly when nothing adopted it. -pub fn stop_all_for_vm_teardown() { - while let Some(next) = take_next() { - let (handle, e) = match next { - Next::File( - file, - FileEntry { - owner, - close_via_owner, - }, - ) => { - log!( - "teardown: closing reader over file @{:p} (owner {:p})", - file, - owner - ); - // A listed file always has its reader as owner: `set_source` - // lists and records it in one step, `set_parent` keeps it current. - let close = close_via_owner.expect("file listed without its reader"); - // SAFETY: the reader unlists the file before handing it off or - // dropping it, and re-records itself whenever it moves. - unsafe { close(owner) }; - continue; - } - Next::Handle(handle, e) => (handle, e), - }; - log!( - "teardown: closing open {} handle @{:p} (owner {:p})", - match e.kind { - Kind::Pipe => "pipe", - Kind::Tty => "tty", - Kind::Process => "process", - }, - handle, - e.owner - ); - match (e.close_via_owner, e.kind) { - // SAFETY: the owner recorded itself for this live handle and clears - // or replaces the slot before it goes away (set_owner contract). - (Some(close), _) => unsafe { close(e.owner) }, - // SAFETY: listed ⇒ initialised on this thread and not closing; a - // pipe/tty nobody adopted is a leaked Box handed to libuv here. - (None, Kind::Pipe) => unsafe { Pipe::close_and_destroy_unlisted(handle.cast()) }, - // SAFETY: as above (a leaked Box nobody adopted). - (None, Kind::Tty) => unsafe { - unsafe extern "C" fn free_tty(t: *mut uv_tty_t) { - // SAFETY: heap `Box` (stdin's static tty is never - // listed); `from_uv` recovers the owning box. - drop(unsafe { Box::from_raw(super::Tty::from_uv(t)) }); - } - uv_close( - handle, - Some(mem::transmute::< - unsafe extern "C" fn(*mut uv_tty_t), - unsafe extern "C" fn(*mut uv_handle_t), - >(free_tty)), - ); - }, - // A process handle is embedded in its owner and always adopted at - // spawn; an unowned one cannot be freed safely — close in place. - // SAFETY: listed ⇒ an initialised, not-closing handle on this loop. - (None, Kind::Process) => unsafe { uv_close(handle, None) }, - } - } -} diff --git a/src/patch/Cargo.toml b/src/patch/Cargo.toml index 21d0d27e33f2..fa0fdbbf9ce6 100644 --- a/src/patch/Cargo.toml +++ b/src/patch/Cargo.toml @@ -17,7 +17,6 @@ thiserror.workspace = true bun_errno.workspace = true bun_core.workspace = true bun_collections.workspace = true -bun_event_loop.workspace = true bun_paths.workspace = true # bun_spawn is a workspace member but not in [workspace.dependencies] yet. bun_spawn = { path = "../spawn" } diff --git a/src/patch/lib.rs b/src/patch/lib.rs index 412246cb876b..fdc6986833db 100644 --- a/src/patch/lib.rs +++ b/src/patch/lib.rs @@ -1632,7 +1632,6 @@ pub fn spawn_opts( new_folder: &[u8], cwd: &ZStr, git: &ZStr, - loop_: &mut bun_event_loop::AnyEventLoop, ) -> (bun_spawn::sync::Options, Vec<*const core::ffi::c_char>) { let argv: Vec> = { const ARGV: &[&[u8]] = &[ @@ -1685,22 +1684,12 @@ pub fn spawn_opts( envp_buf }; - #[cfg(not(windows))] - let _ = loop_; - let opts = bun_spawn::sync::Options { stdout: bun_spawn::sync::Stdio::Buffer, stderr: bun_spawn::sync::Stdio::Buffer, cwd: cwd.as_bytes().into(), envp: Some(envp_buf.as_ptr()), argv, - #[cfg(windows)] - windows: bun_spawn::sync::WindowsOptions { - // `as_handle` owns the handle conversion so variant internals - // stay encapsulated. - loop_: bun_event_loop::AnyEventLoop::as_handle(loop_), - ..Default::default() - }, ..Default::default() }; @@ -1756,14 +1745,13 @@ pub fn git_diff_preprocess_paths(old_folder_: &[u8], new_folder_: &[u8]) -> [Vec pub fn git_diff_internal( old_folder_: &[u8], new_folder_: &[u8], - loop_: &mut bun_event_loop::AnyEventLoop, ) -> crate::Result, Vec>> { let paths = git_diff_preprocess_paths(old_folder_, new_folder_); let old_folder = &paths[0][..]; let new_folder = &paths[1][..]; - // `bun_spawn::sync` execs argv[0] verbatim (execve, no PATH search), so - // resolve `git` here — same as `patchCommit`'s `bun.which` call. + // On POSIX `bun_spawn::sync` execs argv[0] verbatim (execve, no PATH search), + // so resolve `git` here — same as `patchCommit`'s `bun.which` call. let mut gitbuf = bun_paths::path_buffer_pool::get(); let git = bun_which::which( &mut gitbuf, @@ -1817,23 +1805,11 @@ pub fn git_diff_internal( } envp_buf.push(core::ptr::null()); // sentinel - #[cfg(not(windows))] - let _ = loop_; - let opts = bun_spawn::sync::Options { stdout: bun_spawn::sync::Stdio::Buffer, stderr: bun_spawn::sync::Stdio::Buffer, envp: Some(envp_buf.as_ptr()), argv, - // This routes through `bun_spawn::sync::spawn`, whose Windows path - // unconditionally derefs `windows.loop_` (process.rs spawn_windows_*). - // `WindowsOptions::default()` is `zeroed_unchecked()`, so leaving this - // defaulted is a null deref on Windows — supply the caller's loop. - #[cfg(windows)] - windows: bun_spawn::sync::WindowsOptions { - loop_: bun_event_loop::AnyEventLoop::as_handle(loop_), - ..Default::default() - }, ..Default::default() }; diff --git a/src/patch_jsc/testing.rs b/src/patch_jsc/testing.rs index 3bfe8452f7db..86ff55aa0526 100644 --- a/src/patch_jsc/testing.rs +++ b/src/patch_jsc/testing.rs @@ -29,11 +29,7 @@ impl TestingAPIs { let old_folder = old_folder_bunstr.to_utf8(); let new_folder = new_folder_bunstr.to_utf8(); - // `git_diff_internal` routes through `bun_spawn::sync`, which on - // Windows derefs `WindowsOptions.loop_` — supply the JS event loop. - // `global.bun_vm().event_loop()` is the live per-thread `jsc::EventLoop`. - let mut loop_ = bun_jsc::AnyEventLoop::js(global.bun_vm().event_loop().cast()); - let diff = match git_diff_internal(old_folder.slice(), new_folder.slice(), &mut loop_) { + let diff = match git_diff_internal(old_folder.slice(), new_folder.slice()) { Ok(d) => d, Err(e) => return Err(global.throw_error(e, "failed to make diff")), }; diff --git a/src/paths/string_paths.rs b/src/paths/string_paths.rs index cfc7dd71f529..503bbdad7991 100644 --- a/src/paths/string_paths.rs +++ b/src/paths/string_paths.rs @@ -304,6 +304,13 @@ pub fn to_w_path<'a>(wbuf: &'a mut [u16], utf8: &[u8]) -> &'a WStr { to_w_path_maybe_dir::(wbuf, utf8) } +/// As [`to_w_path`]; `None` for a path that is not WTF-8 or does not fit. +#[cfg(windows)] +pub fn try_to_w_path<'a>(wbuf: &'a mut [u16], utf8: &[u8]) -> Option<&'a WStr> { + let len = w_path_units::(wbuf, utf8)?; + Some(wstr_in_buf(wbuf, len)) +} + pub fn to_w_dir_path<'a>(wbuf: &'a mut [u16], utf8: &[u8]) -> &'a WStr { to_w_path_maybe_dir::(wbuf, utf8) } @@ -336,13 +343,25 @@ pub fn fits_in_wide_path_buffer(utf8: &[u8]) -> bool { } pub fn to_kernel32_path<'a>(wbuf: &'a mut [u16], utf8: &[u8]) -> &'a WStr { + let len = kernel32_path_units::(wbuf, utf8).unwrap_or(0); + wstr_in_buf(wbuf, len) +} + +/// As [`to_kernel32_path`]; `None` for a path that is not WTF-8 or does not fit. +#[cfg(windows)] +pub fn try_to_kernel32_path<'a>(wbuf: &'a mut [u16], utf8: &[u8]) -> Option<&'a WStr> { + let len = kernel32_path_units::(wbuf, utf8)?; + Some(wstr_in_buf(wbuf, len)) +} + +fn kernel32_path_units(wbuf: &mut [u16], utf8: &[u8]) -> Option { let path = if utf8.starts_with(&windows::NT_OBJECT_PREFIX_U8) { &utf8[windows::NT_OBJECT_PREFIX_U8.len()..] } else { utf8 }; if path.starts_with(&windows::LONG_PATH_PREFIX_U8) { - return to_w_path(wbuf, path); + return w_path_units::(wbuf, path); } if utf8.len() > 2 && resolve_path::is_drive_letter(utf8[0]) @@ -350,16 +369,26 @@ pub fn to_kernel32_path<'a>(wbuf: &'a mut [u16], utf8: &[u8]) -> &'a WStr { && resolve_path::is_sep_any(utf8[2]) { wbuf[..4].copy_from_slice(&windows::LONG_PATH_PREFIX); - let n = to_w_path(&mut wbuf[4..], path).len(); - return wstr_in_buf(wbuf, n + 4); + let n = w_path_units::(&mut wbuf[4..], path)?; + return Some(n + 4); } - to_w_path(wbuf, path) + w_path_units::(wbuf, path) } fn to_w_path_maybe_dir<'a, const ADD_TRAILING_LASH: bool>( wbuf: &'a mut [u16], utf8: &[u8], ) -> &'a WStr { + let len = w_path_units::(wbuf, utf8).unwrap_or(0); + wstr_in_buf(wbuf, len) +} + +/// Writes the NUL-terminated wide form of `utf8` and returns its length. +/// What cannot be converted is `""`: `None` with `STRICT`, `Some(0)` without. +fn w_path_units( + wbuf: &mut [u16], + utf8: &[u8], +) -> Option { debug_assert!(!wbuf.is_empty()); let cap = wbuf.len().saturating_sub(1 + (ADD_TRAILING_LASH as usize)); @@ -376,11 +405,14 @@ fn to_w_path_maybe_dir<'a, const ADD_TRAILING_LASH: bool>( // `runtime/node/types.rs`, via `fits_in_wide_path_buffer`). Prefixing // wrappers (`to_kernel32_path`, `to_nt_path`, …) may then yield just // their prefix, which likewise fails at the syscall. + // Bytes that are not WTF-8 are no name: replacing them would make two + // byte strings one file. Like a path that does not fit they come out as + // `""`, which the call the path goes to refuses (libuv: ERROR_INVALID_NAME). let Some(converted) = - crate::strings::try_convert_utf8_to_utf16_in_buffer(&mut wbuf[..cap], utf8) + crate::strings::try_convert_wtf8_to_utf16_in_buffer(&mut wbuf[..cap], utf8) else { wbuf[0] = 0; - return wstr_in_buf(wbuf, 0); + return if STRICT { None } else { Some(0) }; }; let mut count = converted.len(); @@ -399,7 +431,7 @@ fn to_w_path_maybe_dir<'a, const ADD_TRAILING_LASH: bool>( wbuf[count] = 0; - wstr_in_buf(wbuf, count) + Some(count) } pub fn clone_normalizing_separators(input: &[u8]) -> Vec { diff --git a/src/ptr/parent_ref.rs b/src/ptr/parent_ref.rs index e3f10a3bb53f..e1fbcd622785 100644 --- a/src/ptr/parent_ref.rs +++ b/src/ptr/parent_ref.rs @@ -68,18 +68,6 @@ impl ParentRef { _provenance: core::marker::PhantomData, } } - - /// Convenience: `Some(from_raw(p))` if `p` is non-null, else `None`. - /// - /// # Safety - /// If `p` is non-null, the [`from_raw`](Self::from_raw) contract applies. - #[inline] - pub unsafe fn from_nullable(p: *const T) -> Option { - NonNull::new(p.cast_mut()).map(|nn| Self { - ptr: nn, - _provenance: core::marker::PhantomData, - }) - } } impl ParentRef { diff --git a/src/ptr/ref_count.rs b/src/ptr/ref_count.rs index 646a5f68866e..d168a51521dc 100644 --- a/src/ptr/ref_count.rs +++ b/src/ptr/ref_count.rs @@ -508,7 +508,7 @@ pub unsafe trait CellRefCounted: Sized { /// /// Holding a `RefPtr` *is* holding one ref: `Drop` releases it (destroying `T` /// if it was the last), `Clone` takes another. To hand the ref to a raw-pointer -/// consumer (C++ `m_ctx`, a uws/libuv userdata slot) use +/// consumer (C++ `m_ctx`, a uws userdata slot) use /// [`into_raw`](Self::into_raw) and reclaim it later with /// [`from_raw`](Self::from_raw). #[must_use = "dropping a RefPtr releases its ref"] diff --git a/src/resolver/lib.rs b/src/resolver/lib.rs index 25986a502f8d..14f390f69ddb 100644 --- a/src/resolver/lib.rs +++ b/src/resolver/lib.rs @@ -1399,7 +1399,14 @@ pub mod fs { { use bun_sys::windows as w; let _ = (existing_fd, store_fd); - let file = bun_sys::get_file_attributes(absolute_path_c) + // The name comes from a directory listing: it is the entry's + // as it stands, trailing dot or space included. + let mut wbuf = bun_paths::w_path_buffer_pool::get(); + let wpath = bun_paths::strings::paths::to_kernel32_path( + &mut wbuf.0[..], + absolute_path_c.as_bytes(), + ); + let file = bun_sys::get_file_attributes_w(wpath) .ok_or(crate::Error::Sys(bun_errno::SystemErrno::ENOENT))?; // A Windows reparse point carries FILE_ATTRIBUTE_DIRECTORY iff // the link is a directory link (junctions always do; symlinks @@ -1430,11 +1437,6 @@ pub mod fs { // error was swallowed at `Entry.kind`, and a directory symlink // was permanently misclassified as `.file` — surfacing as // EISDIR at module load time. - let mut wbuf = bun_paths::w_path_buffer_pool::get(); - let wpath = bun_paths::strings::paths::to_kernel32_path( - &mut wbuf.0[..], - absolute_path_c.as_bytes(), - ); // SAFETY: `wpath` is NUL-terminated UTF-16; null security/template handles. let handle = unsafe { w::CreateFileW( diff --git a/src/runtime/Cargo.toml b/src/runtime/Cargo.toml index 99537ff09947..91482ca2b1c2 100644 --- a/src/runtime/Cargo.toml +++ b/src/runtime/Cargo.toml @@ -75,6 +75,7 @@ bun_md.workspace = true bun_options_types.workspace = true bun_output.workspace = true bun_spawn.workspace = true +bun_spawn_sys.workspace = true bun_sql_jsc.workspace = true bun_paths.workspace = true bun_picohttp.workspace = true @@ -95,7 +96,6 @@ bun_standalone_graph.workspace = true bun_bunfig.workspace = true bun_sys.workspace = true bun_sys_jsc.workspace = true -bun_libuv_sys.workspace = true bun_tcc_sys.workspace = true bun_threading.workspace = true bun_url.workspace = true diff --git a/src/runtime/JSONLineBuffer.rs b/src/runtime/JSONLineBuffer.rs index 336e8af7a671..5e68c2652955 100644 --- a/src/runtime/JSONLineBuffer.rs +++ b/src/runtime/JSONLineBuffer.rs @@ -119,20 +119,6 @@ impl JSONLineBuffer { self.compact(); } } - - /// Notify the buffer that `nread` bytes were written directly into the - /// tail of `data` (via `data.uv_alloc_spare_u8()`). - /// - /// Takes a length, not a `&[u8]`, because the only caller's slice would - /// alias `&mut self.data` — and only the length is used here. Passing the - /// slice through would re-introduce the Stacked-Borrows hazard the - /// `on_read` refactor removed. - #[cfg(windows)] - pub(crate) fn notify_written(&mut self, nread: usize) { - // SAFETY: caller (libuv on_read) wrote `nread` bytes into the uv_alloc_spare* slice. - unsafe { self.data.uv_commit(nread) }; - self.scan_for_newline(); - } } // `pub fn deinit` dropped: Vec's Drop frees the backing allocation (global mimalloc). diff --git a/src/runtime/api.rs b/src/runtime/api.rs index 2bb6ee68caec..193c6c482bd0 100644 --- a/src/runtime/api.rs +++ b/src/runtime/api.rs @@ -79,7 +79,7 @@ pub(crate) mod yaml_object; // inline `mod bun { }` below is a re-export façade only — module bodies are // declared flat to avoid the non-mod-rs nested-path resolution rules. -// Process struct + posix_spawn/uv_spawn machinery. +// Process struct + spawn machinery. #[path = "api/bun/process.rs"] pub(crate) mod bun_process; diff --git a/src/runtime/api/BunObject.rs b/src/runtime/api/BunObject.rs index d5dea22f877e..164ebd82c9cb 100644 --- a/src/runtime/api/BunObject.rs +++ b/src/runtime/api/BunObject.rs @@ -2837,7 +2837,7 @@ mod stdio_stores { } fn build_store(uv_fd: i32, is_atty: bool) -> RefPtr { - let fd = bun_sys::Fd::from_uv(uv_fd); + let fd = bun_sys::Fd::from_crt(uv_fd); let mode: bun_sys::Mode = match bun_sys::fstat(fd) { Ok(stat) => stat.st_mode as bun_sys::Mode, Err(_) => 0, @@ -2877,7 +2877,7 @@ mod stdio_stores { } pub(super) fn stdin(global_this: &JSGlobalObject) -> JSValue { - let is_atty = bun_sys::isatty(bun_sys::Fd::from_uv(0)); + let is_atty = bun_sys::isatty(bun_sys::Fd::from_crt(0)); make_blob( global_this, &STDIN, diff --git a/src/runtime/api/bun/Terminal.rs b/src/runtime/api/bun/Terminal.rs index a41db046700e..d58df63a530d 100644 --- a/src/runtime/api/bun/Terminal.rs +++ b/src/runtime/api/bun/Terminal.rs @@ -13,16 +13,13 @@ use core::cell::Cell; #[cfg(not(windows))] use core::ffi::c_int; use core::ffi::c_void; -#[cfg(windows)] -use core::sync::atomic::{AtomicU32, Ordering}; use crate::node::StringOrBuffer; use bun_core::EncodedSlice; use bun_core::SignalCode; -use bun_io::Loop as AsyncLoop; use bun_io::pipe_reader::BufferedReaderParent; #[cfg(unix)] -use bun_io::pipe_reader::PosixFlags; +use bun_io::pipe_reader::ReaderFlags; use bun_io::{BufferedReader, ReadState, StreamingWriter, WriteStatus}; use bun_jsc::EncodedSliceJsc as _; use bun_jsc::{ @@ -31,8 +28,6 @@ use bun_jsc::{ }; use bun_sys::{self as sys, Fd, FdExt}; -#[cfg(windows)] -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; #[cfg(windows)] use bun_sys::windows; @@ -119,8 +114,8 @@ pub(crate) struct Terminal { /// The slave side of the PTY (used by child processes). Unused on Windows. slave_fd: Cell, - /// Windows ConPTY handle. Used for resize and passed to uv_spawn via - /// uv_process_options_t.pseudoconsole. + /// Windows ConPTY handle. Used for resize and passed to spawn as + /// `SpawnOptions.pseudoconsole`. #[cfg(windows)] hpcon: Cell>, @@ -500,7 +495,7 @@ impl Terminal { if let Some(poll) = r.handle.get_poll() { // PTY behaves like a pipe, not a socket r.flags - .insert(PosixFlags::NONBLOCKING | PosixFlags::POLLABLE); + .insert(ReaderFlags::NONBLOCKING | ReaderFlags::POLLABLE); poll.set_flag(bun_io::FilePollFlag::Nonblocking); } }); @@ -642,8 +637,8 @@ impl Terminal { self.close_internal(); } - /// Windows: get the ConPTY handle to pass to uv_spawn via - /// uv_process_options_t.pseudoconsole. + /// Windows: get the ConPTY handle to pass to spawn as + /// `SpawnOptions.pseudoconsole`. #[cfg(windows)] pub(crate) fn get_pseudoconsole(&self) -> Option { self.hpcon.get() @@ -767,12 +762,10 @@ impl Terminal { // detached: JoinHandle dropped without join → thread runs to completion. } Err(_) => { - // CreateThread failed — the process is in a bad state. Close the - // reader so onReaderDone fires next loop tick (releasing the reader - // ref) instead of hanging on an EOF that will never come. Leak hpcon; - // calling ClosePseudoConsole here would deadlock since reader.close() - // is async (uv_close) and the pipe HANDLE is still open. Conhost sees - // broken-pipe once libuv's deferred close runs. + // CreateThread failed. Close the reader so onReaderDone fires + // instead of waiting for an EOF that never comes. Leak hpcon: + // ClosePseudoConsole would block on the pipe the cancelled read + // still holds open. let flags = self.flags.get(); if flags.contains(Flags::READER_STARTED) && !flags.contains(Flags::READER_DONE) { self.reader.with_mut(|r| r.close()); @@ -1065,12 +1058,6 @@ fn create_pty_posix(cols: u16, rows: u16) -> Result { }) } -#[cfg(windows)] -struct PipePair { - pub server: windows::HANDLE, - pub client: windows::HANDLE, -} - /// Inbox kernel32's HPCON layout. Stable ABI since build 17763: documented as /// "part of an ABI shared with the rest of the operating system" in /// microsoft/terminal `src/winconpty/winconpty.h`. @@ -1082,219 +1069,66 @@ struct PseudoConsole { h_conpty_process: windows::HANDLE, } -/// Create one end of a pipe pair as an overlapped named pipe (server) and the -/// other as a synchronous client. Returns both raw HANDLEs. Caller closes -/// both on error. The "server" end is suitable for libuv (uv_pipe_open) and -/// the "client" end is suitable for ConPTY (which uses synchronous I/O). #[cfg(windows)] -fn create_overlapped_pipe_pair( - // PIPE_ACCESS_INBOUND: server reads, client writes. - // PIPE_ACCESS_OUTBOUND: server writes, client reads. - server_access: u32, -) -> Result { - use windows::kernel32 as k32; - const FILE_FLAG_FIRST_PIPE_INSTANCE: u32 = 0x00080000; - - let pid: u32 = windows::GetCurrentProcessId(); - let counter = PIPE_SERIAL.fetch_add(1, Ordering::Relaxed); - let mut name_utf8_buf = [0u8; 96]; - let name = { - use std::io::Write; - let mut cursor = &mut name_utf8_buf[..]; - // An AppContainer may only create server pipes under `\\.\pipe\LOCAL\`; - // insert the segment only then so the name is unchanged outside one - // (matches libuv's uv__unique_pipe_name). - let local = if windows::is_app_container() { - r"LOCAL\" - } else { - "" - }; - if write!(cursor, r"\\.\pipe\{local}bun-conpty-{pid}-{counter}").is_err() { - return Err(CreatePtyError::OpenPtyFailed); - } - let written = 96 - cursor.len(); - &name_utf8_buf[..written] - }; - let mut name_w_buf = [0u16; 97]; // [96:0]u16 - let name_w_len = bun_core::convert_utf8_to_utf16_in_buffer(&mut name_w_buf, name).len(); - name_w_buf[name_w_len] = 0; - // SAFETY: name_w_buf[name_w_len] == 0 written above. - let name_w = bun_core::WStr::from_buf(&name_w_buf[..], name_w_len); - - // SAFETY: name_w is NUL-terminated; all other params are valid per Win32. - let server = unsafe { - k32::CreateNamedPipeW( - name_w.as_ptr(), - server_access | windows::FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE, - windows::PIPE_TYPE_BYTE | windows::PIPE_READMODE_BYTE | windows::PIPE_WAIT, - 1, - 65536, - 65536, - 0, - core::ptr::null_mut(), - ) - }; - if server == windows::INVALID_HANDLE_VALUE { - return Err(CreatePtyError::OpenPtyFailed); - } - let server_guard = scopeguard::guard(server, |h| unsafe { - // SAFETY: h is a valid open HANDLE on the error path. - let _ = windows::CloseHandle(h); - }); +fn create_pty_windows(cols: u16, rows: u16) -> Result { + use bun_spawn_sys::windows::InheritableHandles; + use bun_spawn_sys::windows::stdio::{ChildPipe, create_pipe_pair}; - let client_access: u32 = if server_access == windows::PIPE_ACCESS_INBOUND { - windows::GENERIC_WRITE - } else { - windows::GENERIC_READ + let close = |handles: [windows::HANDLE; 2]| { + for handle in handles { + // SAFETY: both are open handles this function still owns. + let _ = unsafe { windows::CloseHandle(handle) }; + } }; - // SAFETY: name_w is NUL-terminated; all other params are valid per Win32. - let client = unsafe { - k32::CreateFileW( - name_w.as_ptr(), - client_access, - 0, - core::ptr::null_mut(), - windows::OPEN_EXISTING, - 0, - core::ptr::null_mut(), - ) + // The pseudoconsole's ends are synchronous, ours are overlapped. + // Output: the pseudoconsole writes, we read. + let out = create_pipe_pair(ChildPipe { + readable: false, + writable: true, + overlapped: false, + }) + .map_err(|_| CreatePtyError::OpenPtyFailed)?; + // Input: we write, the pseudoconsole reads. + let input = match create_pipe_pair(ChildPipe { + readable: true, + writable: false, + overlapped: false, + }) { + Ok(pair) => pair, + Err(_) => { + close([out.parent, out.child]); + return Err(CreatePtyError::OpenPtyFailed); + } }; - if client == windows::INVALID_HANDLE_VALUE { - return Err(CreatePtyError::OpenPtyFailed); - } - - let server = scopeguard::ScopeGuard::into_inner(server_guard); - Ok(PipePair { server, client }) -} - -#[cfg(windows)] -static PIPE_SERIAL: AtomicU32 = AtomicU32::new(0); - -#[cfg(windows)] -fn create_pty_windows(cols: u16, rows: u16) -> Result { - // Track ownership explicitly: handles are nulled out as they are closed or - // transferred so the errdefer cleanup never double-closes. - let mut out_server: Option = None; - let mut out_client: Option = None; - let mut in_server: Option = None; - let mut in_client: Option = None; - let mut hpcon: Option = None; - - // errdefer block: scopeguard captures &mut to all of the above. - // Cleanup is inlined at each early-return point (a single drop-time - // closure reading the Option cells would require interior mutability); - // it must run on every `return Err`. - macro_rules! cleanup { - () => { - // SAFETY: every Some(h) is a valid open Win32 handle still owned by - // this fn (not yet transferred); ClosePseudoConsole/CloseHandle are - // safe on those values. - unsafe { - if let Some(h) = hpcon { - windows::ClosePseudoConsole(h); - } - if let Some(h) = out_server { - let _ = windows::CloseHandle(h); - } - if let Some(h) = out_client { - let _ = windows::CloseHandle(h); - } - if let Some(h) = in_server { - let _ = windows::CloseHandle(h); - } - if let Some(h) = in_client { - let _ = windows::CloseHandle(h); - } - } - }; - } - - // Output pipe: ConPTY writes (client), we read (overlapped server). - { - let pair = match create_overlapped_pipe_pair(windows::PIPE_ACCESS_INBOUND) { - Ok(p) => p, - Err(e) => { - cleanup!(); - return Err(e); - } - }; - out_server = Some(pair.server); - out_client = Some(pair.client); - } - - // Input pipe: we write (overlapped server), ConPTY reads (client). - { - let pair = match create_overlapped_pipe_pair(windows::PIPE_ACCESS_OUTBOUND) { - Ok(p) => p, - Err(e) => { - cleanup!(); - return Err(e); - } - }; - in_server = Some(pair.server); - in_client = Some(pair.client); - } let size = windows::COORD { X: clamp_to_coord(cols), Y: clamp_to_coord(rows), }; - { - let mut pc: windows::HPCON = core::ptr::null_mut(); - // SAFETY: in_client/out_client are valid open HANDLEs; pc is a valid out-ptr. - if unsafe { - windows::CreatePseudoConsole(size, in_client.unwrap(), out_client.unwrap(), 0, &mut pc) - } < 0 - { - cleanup!(); - return Err(CreatePtyError::OpenPtyFailed); - } - hpcon = Some(pc); - } - - // ConPTY duplicated the client handles internally; close our copies. - // SAFETY: in_client/out_client are valid open HANDLEs. - unsafe { - let _ = windows::CloseHandle(in_client.take().unwrap()); - let _ = windows::CloseHandle(out_client.take().unwrap()); - } - - // Wrap server (overlapped) ends as libuv-owned FDs so they can be passed - // to BufferedReader/StreamingWriter.start() which calls uv_pipe_open. - // Do not .take() until after success — on Err the cleanup! must still see - // Some(h) so the HANDLE isn't leaked; clear `out_server` only after the - // fallible call succeeds. - let read_fd = match Fd::from_system(out_server.unwrap()).make_libuv_owned() { - Ok(fd) => { - out_server = None; - fd - } - Err(_) => { - cleanup!(); - return Err(CreatePtyError::DupFailed); - } - }; - // errdefer read_fd.close() - let read_fd_guard = scopeguard::guard(read_fd, |fd| fd.close()); - - let write_fd = match Fd::from_system(in_server.unwrap()).make_libuv_owned() { - Ok(fd) => fd, - Err(_) => { - cleanup!(); - return Err(CreatePtyError::DupFailed); - } + let mut hpcon: windows::HPCON = core::ptr::null_mut(); + let created = { + // `CreatePseudoConsole` hands conhost the pipes through inheritable + // handles, which exist in this process for the length of the call. + let _inheritable = InheritableHandles::lock(); + // SAFETY: both ends are valid open HANDLEs; `hpcon` is a valid out-ptr. + let result = + unsafe { windows::CreatePseudoConsole(size, input.child, out.child, 0, &mut hpcon) }; + result >= 0 }; - - let result_hpcon = hpcon.take().unwrap(); - let read_fd = scopeguard::ScopeGuard::into_inner(read_fd_guard); + // The pseudoconsole has its own duplicates. + close([input.child, out.child]); + if !created { + close([input.parent, out.parent]); + return Err(CreatePtyError::OpenPtyFailed); + } Ok(PtyResult { master: Fd::INVALID, - read_fd, - write_fd, + read_fd: Fd::from_system(out.parent), + write_fd: Fd::from_system(input.parent), slave: Fd::INVALID, - hpcon: result_hpcon, + hpcon, }) } @@ -1983,17 +1817,6 @@ impl Terminal { true // Continue reading } - fn loop_(&self) -> *mut AsyncLoop { - #[cfg(windows)] - { - self.event_loop_handle.uv_loop() - } - #[cfg(not(windows))] - { - self.event_loop_handle.r#loop().cast() - } - } - pub(crate) fn finalize(&self) { bun_output::scoped_log!(Terminal, "finalize"); jsc::mark_binding(); @@ -2037,10 +1860,7 @@ impl BufferedReaderParent for Terminal { Self::from_parent_ptr(this).on_reader_error(&err); } unsafe fn loop_(this: *mut Self) -> *mut bun_io::pipe_reader::Loop { - // Delegate to the inherent `Terminal::loop_()` which is cfg-split: - // on Windows it projects `.uv_loop()` (the `*mut uv_loop_t` field of - // `WindowsLoop`), NOT a raw cast of the `bun_uws::Loop` wrapper. - Self::from_parent_ptr(this).loop_().cast() + Self::from_parent_ptr(this).event_loop_handle.r#loop() } unsafe fn event_loop(this: *mut Self) -> bun_io::EventLoopHandle { Self::from_parent_ptr(this) @@ -2079,9 +1899,9 @@ impl bun_io::pipe_writer::PosixStreamingWriterParent for Terminal { #[cfg(windows)] impl bun_io::pipe_writer::WindowsWriterParent for Terminal { - unsafe fn loop_(this: *mut Self) -> *mut bun_libuv_sys::Loop { + unsafe fn loop_(this: *mut Self) -> *mut bun_uws_sys::Loop { // SAFETY: BACKREF set via writer.parent; shared-only read. - unsafe { (*this).event_loop_handle.uv_loop() } + unsafe { (*this).event_loop_handle.r#loop() } } unsafe fn ref_(this: *mut Self) { // SAFETY: see loop_. Intrusive refcount bump via raw pointer — do NOT @@ -2098,7 +1918,6 @@ impl bun_io::pipe_writer::WindowsWriterParent for Terminal { #[cfg(windows)] impl bun_io::pipe_writer::WindowsStreamingWriterParent for Terminal { - const HAS_ON_WRITABLE: bool = true; unsafe fn on_write(this: *mut Self, amount: usize, status: WriteStatus) { Self::from_parent_ptr(this).on_write(amount, status); } diff --git a/src/runtime/api/bun/js_bun_spawn_bindings.rs b/src/runtime/api/bun/js_bun_spawn_bindings.rs index c282ccd985f0..025108cb1176 100644 --- a/src/runtime/api/bun/js_bun_spawn_bindings.rs +++ b/src/runtime/api/bun/js_bun_spawn_bindings.rs @@ -4,9 +4,7 @@ use core::ptr::NonNull; use std::io::Write as _; use crate::ipc as IPC; -#[cfg(not(windows))] -use bun_core::StackCheck; -use bun_core::{Output, Timespec, TimespecMockMode, ZBox, fmt as bun_fmt}; +use bun_core::{Output, StackCheck, Timespec, TimespecMockMode, ZBox, fmt as bun_fmt}; use bun_core::{String as BunString, ZStr, strings}; use bun_event_loop::SpawnSyncEventLoop::TickState; use bun_io::max_buf::MaxBuf; @@ -15,17 +13,13 @@ use bun_jsc::{ JsResult, SystemError, }; use bun_jsc::{JsCell, SysErrorJsc as _}; -#[cfg(unix)] use bun_sys::Fd; use bun_sys::UV_E; use bun_sys::{self as sys, FdExt as _, SignalCode}; // Process / spawn machinery is local to this crate (api/bun/process.rs). -#[cfg(unix)] -use crate::api::bun_process::ExtraPipe; -#[cfg(not(windows))] use crate::api::bun_process::SpawnResultExt as _; -use crate::api::bun_process::{self as spawn, CStrPtr, Rusage, SpawnOptions}; +use crate::api::bun_process::{self as spawn, CStrPtr, ExtraPipe, Rusage, SpawnOptions}; // User-facing JS `Stdio` enum (extract/as_spawn_option/is_piped). use crate::api::bun_spawn::stdio::{self, Stdio}; use crate::api::bun_subprocess::{ @@ -60,13 +54,6 @@ fn subprocess_ipc_owner(ptr: *mut SubprocessT<'_>) -> Option, bun_test_deadline: &mut Option, ) -> JsResult { - if is_sync { - // We skip this on Windows due to test failures. - #[cfg(not(windows))] - { - // Since the event loop is recursively called, we need to check if it's safe to recurse. - if !StackCheck::init().is_safe_to_recurse() { - return Err(cx.global().throw_stack_overflow()); - } - } + // Since the event loop is recursively called, we need to check if it's safe to recurse. + if is_sync && !StackCheck::init().is_safe_to_recurse() { + return Err(cx.global().throw_stack_overflow()); } // PERF: argv/env strings are allocated per-iteration; profile if hot. @@ -351,8 +332,7 @@ fn spawn_maybe_sync( let mut args = args_; let mut maybe_ipc_mode: Option = None; let mut ipc_callback: JSValue = JSValue::ZERO; - let mut extra_fds: Vec = Vec::new(); - #[cfg(not(windows))] + let mut extra_fds: Vec = Vec::new(); let mut socket_fd_indices: Vec = Vec::new(); let mut argv0: Option<*const c_char> = None; let mut ipc_channel: i32 = -1; @@ -368,6 +348,8 @@ fn spawn_maybe_sync( let mut windows_hide: bool = false; #[cfg(windows)] let mut windows_verbatim_arguments: bool = false; + // 'overlapped' at stdin/stdout/stderr; only Windows has such a thing. + let mut overlapped_stdio = [false; 3]; let mut abort_signal: Option = None; let mut terminal_info: Option = None; let mut existing_terminal: Option> = None; // Existing terminal passed by user @@ -561,7 +543,8 @@ fn spawn_maybe_sync( }; override_env = true; - // If the env object does not include a $PATH, it must disable path lookup for argv[0] + // If the env object does not include a $PATH, argv[0] is not looked up here. + // (On Windows the spawn itself then looks for a bare name: `search_path.rs`.) let mut new_path: &[u8] = b""; // `JSObject` is an `opaque_ffi!` ZST handle; `opaque_ref` is the // centralised non-null-ZST deref proof. @@ -591,7 +574,14 @@ fn spawn_maybe_sync( let mut stdio_iter = stdio_val.array_iterator(cx.global())?; let mut i: i32 = 0; while let Some(value) = stdio_iter.next()? { - Stdio::extract(&mut stdio[i as usize], cx, i, value, is_sync)?; + Stdio::extract( + &mut stdio[i as usize], + cx, + i, + value, + is_sync, + &mut overlapped_stdio[i as usize], + )?; if i == 2 { break; } @@ -603,7 +593,8 @@ fn spawn_maybe_sync( // extract() leaves `out_stdio` untouched when `value` is undefined, so this // must be initialized to a sane default instead of `undefined`. let mut new_item: Stdio = Stdio::Ignore; - Stdio::extract(&mut new_item, cx, i, value, is_sync)?; + // A pipe at fd 3 and above is overlapped for the child either way. + Stdio::extract(&mut new_item, cx, i, value, is_sync, &mut false)?; let opt = match new_item.as_spawn_option(i) { stdio::ResultT::Result(opt) => opt, @@ -611,11 +602,7 @@ fn spawn_maybe_sync( return Err(e.throw_js(cx.global())); } }; - #[cfg(not(windows))] - let is_ipc = matches!(opt, SpawnOptionsStdio::Ipc); - #[cfg(windows)] - let is_ipc = matches!(opt, SpawnOptionsStdio::Ipc(_)); - if is_ipc { + if matches!(opt, spawn::Stdio::Ipc) { ipc_channel = i32::try_from(extra_fds.len()).expect("int cast"); } extra_fds.push(opt); @@ -629,15 +616,36 @@ fn spawn_maybe_sync( } } else { if let Some(value) = args.get(cx.global(), "stdin")? { - Stdio::extract(&mut stdio[0], cx, 0, value, is_sync)?; + Stdio::extract( + &mut stdio[0], + cx, + 0, + value, + is_sync, + &mut overlapped_stdio[0], + )?; } if let Some(value) = args.get(cx.global(), "stderr")? { - Stdio::extract(&mut stdio[2], cx, 2, value, is_sync)?; + Stdio::extract( + &mut stdio[2], + cx, + 2, + value, + is_sync, + &mut overlapped_stdio[2], + )?; } if let Some(value) = args.get(cx.global(), "stdout")? { - Stdio::extract(&mut stdio[1], cx, 1, value, is_sync)?; + Stdio::extract( + &mut stdio[1], + cx, + 1, + value, + is_sync, + &mut overlapped_stdio[1], + )?; } } @@ -1001,7 +1009,7 @@ fn spawn_maybe_sync( // - No auto killer (for tests) // - No IPC // - No inspector (since they might want to press pause or step) - let can_block_entire_thread_to_reduce_cpu_usage_in_fast_path = (cfg!(unix) && is_sync) + let can_block_entire_thread_to_reduce_cpu_usage_in_fast_path = is_sync && abort_signal.is_none() && timeout.is_none() && max_buffer.is_none() @@ -1034,11 +1042,6 @@ fn spawn_maybe_sync( .rare_data() .spawn_sync_event_loop(&mut *jsc_vm_ptr) else { - // `WindowsStdio` has no `Drop`; free the pipes `as_spawn_option` allocated. - #[cfg(windows)] - for e in &mut extra_fds { - e.deinit(); - } return Err(throw_spawn_sync_loop_init_failed(cx.global())); }; sync_loop.prepare(jsc_vm_ptr.cast()); @@ -1080,7 +1083,7 @@ fn spawn_maybe_sync( None => None, }; - let mut spawn_options = SpawnOptions { + let spawn_options = SpawnOptions { // Empty means "inherit the parent's working directory". Only chdir // when the user asked for it: the stored cwd path string can be stale // if the directory was renamed out from under the process (#33819). @@ -1107,13 +1110,12 @@ fn spawn_maybe_sync( extra_fds: { // Record which extra-stdio slots are 'socket-fd' so we can // downgrade them from OwnedFd to UnownedFd after all fallible - // init below succeeds. spawn_process_posix pushes OwnedFd for + // init below succeeds. spawn_process pushes OwnedFd for // SocketFd so every error path's finalize_streams still closes // the bun-created fd; the caller-owns-it contract only begins // once the Subprocess is returned and .stdio[i] is readable. - #[cfg(not(windows))] for (j, e) in extra_fds.iter().enumerate() { - if matches!(e, SpawnOptionsStdio::SocketFd) { + if matches!(e, spawn::Stdio::SocketFd) { socket_fd_indices.push(j); } } @@ -1138,7 +1140,7 @@ fn spawn_maybe_sync( windows: spawn::WindowsOptions { hide_window: windows_hide, verbatim_arguments: windows_verbatim_arguments, - loop_: loop_handle, + overlapped_stdio, }, #[cfg(any(target_os = "linux", target_os = "android"))] cgroup_fd: cgroup_dir.as_ref().map(|c| c.dir.handle()), @@ -1154,12 +1156,6 @@ fn spawn_maybe_sync( if err == bun_spawn::Error::Sys(bun_errno::SystemErrno::EMFILE) || err == bun_spawn::Error::Sys(bun_errno::SystemErrno::ENFILE) => { - // Windows: close+free the heap `uv::Pipe` handles that - // `as_spawn_option` allocated and `spawn_process_windows` may have - // `uv_pipe_init`-registered on the spawn-sync loop. Skipping this - // leaks them and trips `assert(err == 0)` in `uv_loop_delete` at - // `SpawnSyncEventLoop::Drop`. POSIX: no-op. - spawn_options.deinit(); let display_path: &ZStr = if !argv.is_empty() && !argv[0].is_null() { // SAFETY: argv[0] is non-null and points at a NUL-terminated // string we built above (lives in `cstr_storage`). @@ -1187,16 +1183,12 @@ fn spawn_maybe_sync( .throw_value(SystemError::from(systemerror).to_error_instance(cx.global()))); } Err(err) => { - // See EMFILE arm above. - spawn_options.deinit(); return Err(cx .global() .throw_error(crate::Error::from(err), ": failed to spawn process")); } Ok(maybe) => match maybe { sys::Result::Err(err) => { - // See EMFILE arm above. - spawn_options.deinit(); #[cfg(any(target_os = "linux", target_os = "android"))] if let Some(c) = &cgroup_dir && err.syscall == sys::Tag::clone3 @@ -1239,9 +1231,10 @@ fn spawn_maybe_sync( // Use the isolated loop for spawnSync operations // - // Note: `PosixSpawnResult::to_process` consumes `self` but only reads - // `pid`/`pidfd`/`has_exited`. `stdin/stdout/stderr/extra_pipes` are still - // needed afterward, so take those fields out first so the partial move is + // Note: `SpawnResult::to_process` consumes `self` but uses only what + // identifies the process (`pid`, `pidfd`, the process handle) and + // `has_exited`. `stdin/stdout/stderr/extra_pipes` are still needed + // afterward, so take those fields out first so the partial move is // explicit. let spawned_stdin = spawned.stdin.take(); let spawned_stdout = spawned.stdout.take(); @@ -1249,8 +1242,7 @@ fn spawn_maybe_sync( let mut spawned_extra_pipes = core::mem::take(&mut spawned.extra_pipes); let process = spawned.to_process(loop_handle); - #[cfg(unix)] - let posix_ipc_fd = if !is_sync && maybe_ipc_mode.is_some() { + let ipc_fd = if !is_sync && maybe_ipc_mode.is_some() { spawned_extra_pipes[usize::try_from(ipc_channel).expect("int cast")].fd() } else { Fd::INVALID @@ -1311,8 +1303,6 @@ fn spawn_maybe_sync( })); // SAFETY: subprocess_ptr is a freshly-boxed Subprocess; we hold the only reference. let subprocess = unsafe { &mut *subprocess_ptr }; - #[cfg(windows)] - SubprocessT::record_stdio_pipe_ownership(subprocess_ptr); // Erase the borrow lifetime to 'static for the intrusive back-pointer // (PipeReader stores it as raw NonNull). subprocess_ptr is non-null (just boxed). let subprocess_nn: NonNull> = @@ -1332,17 +1322,6 @@ fn spawn_maybe_sync( subprocess.stdout_maxbuf.set(mb); } - #[cfg(windows)] - if !is_sync { - if let Some(ipc_mode) = maybe_ipc_mode { - subprocess.ipc_data.set(Some(IPC::SendQueue::new( - ipc_mode, - subprocess_ipc_owner(subprocess_ptr), - IPC::SocketUnion::Uninitialized, - ))); - } - } - let mut promise_for_stream: JSValue = JSValue::ZERO; match Writable::init( @@ -1362,44 +1341,18 @@ fn spawn_maybe_sync( // run. `finalizeStreams()` here only closes `stdio_pipes` and the // pidfd; stdin/stdout/stderr are `.ignore` so their `closeIO` is a // no-op. - #[cfg(unix)] - { - if let Some(fd) = spawned_stdout { - if !stdio[1].borrows_caller_fd() { - fd.close(); - } - } - if let Some(fd) = spawned_stderr { - if !stdio[2].borrows_caller_fd() { - fd.close(); - } + if let Some(fd) = spawned_stdout { + if !stdio[1].borrows_caller_fd() { + fd.close(); } } - #[cfg(not(unix))] - { - use bun_libuv_sys::UvHandle as _; - for r in [spawned_stdout, spawned_stderr] { - match r { - spawn::WindowsStdioResult::Buffer(pipe) => { - // `uv_close` is async — libuv keeps the raw handle pointer - // until the next loop tick and then calls `on_pipe_close`, - // which reclaims the allocation via `heap::take`. Leak the - // Box so it outlives this scope; dropping it here would be - // a use-after-free + double-free when the callback fires. - Box::leak(pipe).close(Subprocess::on_pipe_close) - } - spawn::WindowsStdioResult::BufferFd(fd) => fd.close(), - spawn::WindowsStdioResult::UnownedFd(_) - | spawn::WindowsStdioResult::Unavailable => {} - } + if let Some(fd) = spawned_stderr { + if !stdio[2].borrows_caller_fd() { + fd.close(); } } subprocess.finalize_streams(); subprocess.process_mut().detach(); - if let Some(ipc_data) = subprocess.ipc_data.take() { - // Nothing else holds it yet (no socket wired, no task scheduled). - ipc_data.detach(); - } let mut mb = subprocess.stdout_maxbuf.get(); MaxBuf::remove_from_subprocess(&mut mb); subprocess.stdout_maxbuf.set(mb); @@ -1505,7 +1458,7 @@ fn spawn_maybe_sync( bun_uws::SocketKind::SpawnIpc, None, core::mem::size_of::<*mut IPC::SendQueue>() as core::ffi::c_int, - posix_ipc_fd.native(), + ipc_fd.native(), 0, true, ); @@ -1521,58 +1474,56 @@ fn spawn_maybe_sync( } } - if let Some(ipc_data) = subprocess.ipc() { + #[cfg(windows)] + if !is_sync { + if let Some(mode) = maybe_ipc_mode { + subprocess.ipc_data.set(Some(IPC::SendQueue::new( + mode, + subprocess_ipc_owner(subprocess_ptr), + IPC::SocketUnion::Uninitialized, + ))); + } + } + + // A channel that could not be opened is reported further down, once the + // wrapper and the exit watch exist: they are what releases a started child. + let ipc_open_err: Option = subprocess.ipc().and_then(|ipc_data| { #[cfg(unix)] - { - if let Some(posix_ipc_info) = posix_ipc_info { - if let Some(ctx) = posix_ipc_info.ext::<*mut IPC::SendQueue>() { - // SAFETY: `ctx` is the live ext-slot pointer returned by uSockets; - // it stays valid for the socket's lifetime. - unsafe { *ctx = ipc_data.as_ctx_ptr() }; - ipc_data.socket.set(IPC::SocketUnion::Open(posix_ipc_info)); - } + if let Some(posix_ipc_info) = posix_ipc_info { + if let Some(ctx) = posix_ipc_info.ext::<*mut IPC::SendQueue>() { + // SAFETY: `ctx` is the live ext-slot pointer returned by uSockets; + // it stays valid for the socket's lifetime. + unsafe { *ctx = ipc_data.as_ctx_ptr() }; + ipc_data.socket.set(IPC::SocketUnion::Open(posix_ipc_info)); } - // uws owns the fd now (owns_fd=1); neutralize the slot so finalizeStreams doesn't double-close. - subprocess.stdio_pipes.with_mut(|v| { - v[usize::try_from(ipc_channel).expect("int cast")] = ExtraPipe::Unavailable; - }); } - #[cfg(not(unix))] - { - let idx = usize::try_from(ipc_channel).expect("int cast"); - // The IPC channel is always a `buffer` pipe on Windows. - // Ownership of the heap `uv::Pipe` transfers to `ipc_data.socket`; - // neutralize the slot up front so `finalizeStreams` can't - // double-close it (the Box would otherwise drop on reassignment). - let ipc_pipe: *mut bun_libuv_sys::Pipe = subprocess.stdio_pipes.with_mut(|pipes| { - match core::mem::take(&mut pipes[idx]) { - spawn::WindowsStdioResult::Buffer(pipe) => bun_core::heap::into_raw(pipe), - other => { - // Restore the slot before panicking so the - // `Subprocess` finalizer still sees the original - // variant. Use - // `unreachable!` (NOT `debug_assert!` — that would - // compile out in release and feed null to - // `windows_configure_server`, which immediately - // dereferences it). - pipes[idx] = other; - unreachable!("IPC channel stdio is not a buffer pipe"); - } - } - }); - // `windows_configure_server` stores the pointer in `uv_handle_t.data` - // for the pipe's lifetime, so it must be the allocation root - // (write provenance), never one re-derived from `&SendQueue`. - // SAFETY: `ipc_data` is the live SendQueue owned by `subprocess`. - if let Err(err) = - unsafe { IPC::SendQueue::windows_configure_server(ipc_data.as_ctx_ptr(), ipc_pipe) } - { - let err_js = err.to_js(cx.global()); - subprocess.deref(); - return Err(cx.global().throw_value(err_js)); - } + // SAFETY: `as_ctx_ptr` is the root pointer of the live SendQueue + // owned by `subprocess`; `ipc_fd` is the overlapped pipe end spawn + // created for the channel. + #[cfg(windows)] + if let Err(err) = unsafe { + IPC::SendQueue::open_pipe( + ipc_data.as_ctx_ptr(), + loop_handle.loop_(), + ipc_fd, + bun_io::windows::PipeOrigin::Created, + ) + } { + return Some(err); } + // The channel (uws, or the pipe on Windows) owns the fd now; neutralize + // the slot so finalizeStreams doesn't double-close. + subprocess.stdio_pipes.with_mut(|v| { + v[usize::try_from(ipc_channel).expect("int cast")] = ExtraPipe::Unavailable; + }); ipc_data.write_version_packet(cx.global()); + None + }); + if ipc_open_err.is_some() { + // Nothing was opened: the pipe end is still the `stdio_pipes` slot's to close. + if let Some(queue) = subprocess.ipc_data.take() { + queue.detach(); + } } if matches!(subprocess.stdin.get(), Writable::Pipe(_)) && promise_for_stream == JSValue::ZERO { @@ -1723,11 +1674,14 @@ fn spawn_maybe_sync( }; if let Some(err) = stdin_start_err { // An unstarted writer never reports on_close; a Buffer left here pins the wrapper. - #[cfg(not(windows))] // Windows adopts the pipe at create and start() cannot fail there. subprocess.on_close_io(Subprocess::StdioKind::Stdin); let _ = subprocess.try_kill(subprocess.kill_signal); return Err(cx.global().throw_value(err.to_js(cx.global()))); } + if let Some(err) = ipc_open_err { + let _ = subprocess.try_kill(subprocess.kill_signal); + return Err(cx.global().throw_value(err.to_js(cx.global()))); + } // Every `return Err` above is past; the Subprocess will be returned to // JS. Downgrade 'socket-fd' slots from OwnedFd to UnownedFd so @@ -1737,7 +1691,6 @@ fn spawn_maybe_sync( // open-socket failure all throw after populating stdio_pipes; on those // paths the caller never receives the Subprocess, so the OwnedFd slot // must remain for the GC'd wrapper's finalize_streams to close. - #[cfg(not(windows))] if !socket_fd_indices.is_empty() { subprocess.stdio_pipes.with_mut(|pipes| { for j in &socket_fd_indices { @@ -2057,7 +2010,7 @@ fn throw_spawn_sync_loop_init_failed(global_this: &JSGlobalObject) -> JsError { errno: -UV_E::MFILE, path: BunString::EMPTY, #[cfg(windows)] - syscall: BunString::static_("uv_loop_init"), + syscall: BunString::static_("CreateIoCompletionPort"), #[cfg(any(target_os = "linux", target_os = "android"))] syscall: BunString::static_("epoll_create1"), #[cfg(any(target_os = "macos", target_os = "freebsd"))] diff --git a/src/runtime/api/bun/process.rs b/src/runtime/api/bun/process.rs index 6f8fc48c09f1..62d2624c96de 100644 --- a/src/runtime/api/bun/process.rs +++ b/src/runtime/api/bun/process.rs @@ -1,20 +1,14 @@ -//! MOVE_DOWN: the full `Process` / `Poller` / `WaiterThread` / -//! `spawn_process` / `sync` implementation now lives in the `bun_spawn` -//! workspace crate (`src/spawn/process.rs`). It was moved out of `bun_runtime` -//! so that `bun_install` (lifecycle scripts, security scanner, git -//! repositories), `bun_jsc` (`ProcessAutoKiller`), and `bun_patch` can spawn -//! and track child processes without the `bun_runtime → bun_install`/`bun_jsc` -//! dependency cycle. +//! `Process` / `Poller` / `WaiterThread` / `spawn_process` / `sync` belong to +//! the `bun_spawn` crate (`src/spawn/process.rs`), so that `bun_install` +//! (lifecycle scripts, security scanner, git repositories), `bun_jsc` +//! (`ProcessAutoKiller`) and `bun_patch` can spawn and track child processes +//! without a `bun_runtime → bun_install`/`bun_jsc` dependency cycle. //! -//! This file is a thin re-export façade so existing -//! `crate::api::bun_process::*` paths keep working. +//! This file re-exports them as `crate::api::bun_process::*`. pub(crate) use bun_spawn::process::sync; pub(crate) use bun_spawn::process::*; -// `event_loop_handle_to_ctx` was `pub(crate)` in the original; re-export under -// `pub(crate)` here so other `bun_runtime` modules (e.g. `subprocess.rs`) can -// keep calling it via `crate::api::bun_process::event_loop_handle_to_ctx`. pub(crate) use bun_spawn::process::event_loop_handle_to_ctx; #[cfg(not(windows))] pub(crate) use bun_spawn::process::spawn_sys; diff --git a/src/runtime/api/bun/spawn.rs b/src/runtime/api/bun/spawn.rs index 02034df94dee..6eb6642026fa 100644 --- a/src/runtime/api/bun/spawn.rs +++ b/src/runtime/api/bun/spawn.rs @@ -1,12 +1,10 @@ -//! MOVE_DOWN: the `posix_spawn`(2) FFI wrappers (`Actions`, `Attr`, `spawn_z`, -//! `wait4`) and the `bun_spawn` `Action`/`Attr` structs now live in the -//! `bun_spawn` workspace crate (`src/spawn/posix_spawn.rs`). They were moved -//! out of `bun_runtime` so that `bun_spawn::process` (which `bun_install` / -//! `bun_jsc` depend on) can call them without a `bun_runtime` dependency. +//! The `posix_spawn`(2) FFI wrappers (`Actions`, `Attr`, `spawn_z`, `wait4`) +//! and the `bun_spawn` `Action`/`Attr` structs belong to `bun_spawn_sys` +//! (`src/spawn_sys/posix_spawn.rs`), below `bun_spawn::process`, which +//! `bun_install` / `bun_jsc` use without depending on `bun_runtime`. //! -//! This file re-exports them for existing `crate::api::bun_spawn::*` paths and -//! keeps the `stdio` submodule (which depends on the JSC-tier `Subprocess` -//! type and so must stay in `bun_runtime`). +//! This file re-exports them as `crate::api::bun_spawn::*` and holds the +//! `stdio` submodule, which needs the JSC-tier `Subprocess` type. #![warn(unused_must_use)] diff --git a/src/runtime/api/bun/spawn/stdio.rs b/src/runtime/api/bun/spawn/stdio.rs index b27a514e0968..fcd4e4361125 100644 --- a/src/runtime/api/bun/spawn/stdio.rs +++ b/src/runtime/api/bun/spawn/stdio.rs @@ -3,8 +3,6 @@ use bun_collections::VecExt; use bun_jsc::{self as jsc, JSGlobalObject, JSValue, JsResult}; #[cfg(any(target_os = "linux", target_os = "android"))] use bun_sys::FdExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; use bun_sys::{self as sys, Fd}; // `bun.jsc.WebCore` lives in this crate (not `bun_jsc`); alias so the body can @@ -17,14 +15,6 @@ use crate::webcore::node_types::{PathLike, PathOrFileDescriptor}; // keep `process` leaf; `subprocess` re-exports it). use crate::api::bun_process::{self as process, Dup2 as ProcessDup2, StdioKind}; -// `SpawnOptions.Stdio` is platform-dependent: process.rs exposes `PosixStdio` / -// `WindowsStdio`; alias the active one as `SpawnOptionsStdio` so the body stays -// platform-neutral. -#[cfg(not(windows))] -pub(crate) type SpawnOptionsStdio = process::PosixStdio; -#[cfg(windows)] -pub(crate) type SpawnOptionsStdio = process::WindowsStdio; - // `bun.FD.Stdio` (the StdIn/StdOut/StdErr tag enum) is `bun_core::Stdio`, // re-exported through `bun_sys`. use sys::Stdio as FdStdio; @@ -63,12 +53,13 @@ pub(crate) enum Stdio { #[cfg(any(target_os = "linux", target_os = "android"))] Memfd(Fd), Pipe, - /// Like `Pipe` at indices >= 3, but the parent end of the socketpair is - /// stored as `ExtraPipe::UnownedFd` so `Subprocess::finalize_streams` - /// never closes it; the caller reads the fd from `.stdio[i]` and is - /// responsible for closing it. Used by `node:child_process` which wraps - /// extra `"pipe"` slots in `net.connect({fd})` (usockets then owns the - /// fd). Only valid at indices >= 3. + /// Like `Pipe` at indices >= 3, but the parent end of the socketpair (a + /// duplex pipe on Windows) is stored as `ExtraPipe::UnownedFd` so + /// `Subprocess::finalize_streams` never closes it; the caller reads the fd + /// from `.stdio[i]` and is responsible for closing it. Used by + /// `node:child_process` which wraps extra `"pipe"` slots in + /// `net.connect({fd})` (the socket then owns the fd). Only valid at + /// indices >= 3. SocketFd, Ipc, ReadableStream(webcore::ReadableStream), @@ -81,7 +72,7 @@ pub(crate) enum ResultT { Err(ToSpawnOptsError), } -pub(crate) type Result = ResultT; +pub(crate) type Result = ResultT; pub(crate) enum ToSpawnOptsError { StdinUsedAsOut, @@ -193,30 +184,7 @@ impl Stdio { } } - /// On windows this function allocates a `*mut uv::Pipe` (via `heap::alloc`); - /// the caller must transfer ownership (e.g. into `WindowsStdioResult::Buffer` - /// via `heap::take`) or free it with `close_and_destroy`. pub(crate) fn as_spawn_option(&mut self, i: i32) -> Result { - // `SpawnOptionsStdio` is already a cfg-gated alias to PosixStdio / - // WindowsStdio; only three variant *constructors* differ in arity - // between targets, so spell those per-cfg and share the rest. - #[cfg(not(windows))] - fn buffer() -> SpawnOptionsStdio { - SpawnOptionsStdio::Buffer - } - #[cfg(windows)] - fn buffer() -> SpawnOptionsStdio { - SpawnOptionsStdio::Buffer(create_zeroed_pipe()) - } - #[cfg(not(windows))] - fn ipc() -> SpawnOptionsStdio { - SpawnOptionsStdio::Ipc - } - #[cfg(windows)] - fn ipc() -> SpawnOptionsStdio { - SpawnOptionsStdio::Ipc(create_zeroed_pipe()) - } - let result = match self { Self::Blob(blob) => 'brk: { let fd = FdStdio::from_int(i).map(FdStdio::fd); @@ -226,7 +194,7 @@ impl Stdio { match file.pathlike { PathOrFileDescriptor::Fd(store_fd) => { if Some(store_fd) == fd { - break 'brk SpawnOptionsStdio::Inherit; + break 'brk process::Stdio::Inherit; } if let Some(tag) = store_fd.stdio_tag() { @@ -248,10 +216,10 @@ impl Stdio { } } - break 'brk SpawnOptionsStdio::Pipe(store_fd); + break 'brk process::Stdio::Pipe(store_fd); } PathOrFileDescriptor::Path(ref path) => { - break 'brk SpawnOptionsStdio::Path( + break 'brk process::Stdio::Path( path.slice().to_vec().into_boxed_slice(), ); } @@ -264,41 +232,34 @@ impl Stdio { return ResultT::Err(ToSpawnOptsError::BlobUsedAsOut); } - buffer() + process::Stdio::Buffer } - Self::Dup2(d) => SpawnOptionsStdio::Dup2(ProcessDup2 { + Self::Dup2(d) => process::Stdio::Dup2(ProcessDup2 { out: d.out, to: d.to, }), - Self::Capture(_) | Self::Pipe | Self::ReadableStream(_) => buffer(), - #[cfg(not(windows))] - Self::SocketFd => SpawnOptionsStdio::SocketFd, - // Windows extra-stdio is a libuv pipe handle (no raw-fd ownership - // to transfer), so `socket-fd` behaves identically to `pipe` there. - #[cfg(windows)] - Self::SocketFd => buffer(), - Self::Ipc => ipc(), - Self::Fd(fd) => SpawnOptionsStdio::Pipe(*fd), + Self::Capture(_) | Self::Pipe | Self::ReadableStream(_) => process::Stdio::Buffer, + Self::SocketFd => process::Stdio::SocketFd, + Self::Ipc => process::Stdio::Ipc, + Self::Fd(fd) => process::Stdio::Pipe(*fd), #[cfg(any(target_os = "linux", target_os = "android"))] - Self::Memfd(fd) => SpawnOptionsStdio::Pipe(*fd), + Self::Memfd(fd) => process::Stdio::Pipe(*fd), Self::Path(pathlike) => { - SpawnOptionsStdio::Path(pathlike.slice().to_vec().into_boxed_slice()) + process::Stdio::Path(pathlike.slice().to_vec().into_boxed_slice()) } - Self::Inherit => SpawnOptionsStdio::Inherit, - Self::Ignore => SpawnOptionsStdio::Ignore, + Self::Inherit => process::Stdio::Inherit, + Self::Ignore => process::Stdio::Ignore, }; ResultT::Result(result) } pub(crate) fn is_piped(&self) -> bool { - match self { - Self::Capture(_) | Self::Blob(_) | Self::Pipe | Self::ReadableStream(_) => true, - Self::Ipc => cfg!(windows), - _ => false, - } + matches!( + self, + Self::Capture(_) | Self::Blob(_) | Self::Pipe | Self::ReadableStream(_) + ) } - #[cfg(not(windows))] pub(crate) fn borrows_caller_fd(&self) -> bool { matches!(self, Self::Fd(_)) } @@ -396,6 +357,9 @@ impl Stdio { i: i32, value: JSValue, is_sync: bool, + // Set for 'overlapped': a pipe whose child end is opened for + // overlapped I/O on Windows, and a plain pipe elsewhere. + overlapped: &mut bool, ) -> JsResult<()> { if value.is_empty() { return Ok(()); @@ -414,8 +378,11 @@ impl Stdio { *out_stdio = Stdio::Inherit; } else if str.eq_ascii(b"ignore") { *out_stdio = Stdio::Ignore; - } else if str.eq_ascii(b"pipe") || str.eq_ascii(b"overlapped") { + } else if str.eq_ascii(b"pipe") { *out_stdio = Stdio::Pipe; + } else if str.eq_ascii(b"overlapped") { + *out_stdio = Stdio::Pipe; + *overlapped = true; } else if str.eq_ascii(b"socket-fd") { if i < 3 { return Err(cx.global().throw_invalid_arguments(format_args!( @@ -439,10 +406,8 @@ impl Stdio { } return Ok(()); } else if value.is_number() { - // `bun.FD.fromUV(this.toInt32())` inlined here since the - // upstream `bun_jsc::JSValue` doesn't expose a wrapper. - let fd = Fd::from_uv(value.to_int32()); - let file_fd = fd.uv(); + let fd = Fd::from_crt(value.to_int32()); + let file_fd = fd.crt(); if file_fd < 0 { return Err(cx.global().throw_invalid_arguments(format_args!( "file descriptor must be a positive integer" @@ -669,15 +634,3 @@ impl Drop for Stdio { } } } - -/// Allocate a zero-initialized uv.Pipe. Zero-init ensures `pipe.loop` is null -/// for pipes that never reach `uv_pipe_init`, so `closeAndDestroy` can tell -/// whether `uv_close` is needed. -#[cfg(windows)] -fn create_zeroed_pipe() -> *mut uv::Pipe { - // `bun.new` → heap::alloc(Box::new(..)). WindowsSpawnOptions.Stdio.{buffer,ipc} - // store the pipe as a raw FFI-owned `*mut uv::Pipe` so `spawn_process_windows` - // can transfer sole ownership into `WindowsStdioResult::Buffer` via - // `heap::take` without aliasing a live `Box` (which would double-free). - bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed::())) -} diff --git a/src/runtime/api/bun/subprocess.rs b/src/runtime/api/bun/subprocess.rs index e4e6a456198b..05837baff7b4 100644 --- a/src/runtime/api/bun/subprocess.rs +++ b/src/runtime/api/bun/subprocess.rs @@ -11,26 +11,16 @@ use bun_jsc::{ VirtualMachine, }; use bun_jsc::{JsClass, SysErrorJsc}; -#[cfg(not(windows))] use bun_sys::FdExt as _; use bun_sys::{self, SignalCode}; use enumset::{EnumSet, EnumSetType}; -// Process / spawn machinery lives in this crate (api/bun/process.rs), not in an -// external `bun_spawn` crate. The `bun_spawn` workspace crate only carries the -// platform-thin `Stdio`/`Status` shims used by `bun.spawnSync` callers. use crate::api::bun::Terminal; -#[cfg(windows)] -use crate::api::bun_process as spawn_process; -#[cfg(not(windows))] -use crate::api::bun_process::ExtraPipe; -use crate::api::bun_process::{Process, Rusage, Status}; +use crate::api::bun_process::{ExtraPipe, Process, Rusage, Status}; use crate::ipc as IPC; use crate::node::node_cluster_binding; use crate::timer::{EventLoopTimer, EventLoopTimerState}; use crate::webcore::{self, AbortSignal, FileSink}; -#[cfg(windows)] -use bun_libuv_sys::UvHandle as _; #[path = "subprocess/ResourceUsage.rs"] pub(crate) mod resource_usage; @@ -74,14 +64,6 @@ pub(crate) mod js { ); } -/// Platform-dependent stdio result type. -pub(crate) use bun_spawn::subprocess::StdioResult; - -#[cfg(windows)] -type StdioPipeItem = StdioResult; -#[cfg(not(windows))] -type StdioPipeItem = ExtraPipe; - pub(crate) type StaticPipeWriter<'a> = NewStaticPipeWriter>; impl<'a> static_pipe_writer::StaticPipeWriterProcess for Subprocess<'a> { @@ -122,7 +104,7 @@ pub struct Subprocess<'a> { pub(crate) stdin: JsCell>, pub(crate) stdout: JsCell, pub(crate) stderr: JsCell, - pub(crate) stdio_pipes: JsCell>, + pub(crate) stdio_pipes: JsCell>, pub(crate) pid_rusage: Cell>, /// Terminal attached to this subprocess (if spawned with terminal option) @@ -296,18 +278,12 @@ bitflags::bitflags! { } } -// `StdioResult` is `Option` (Copy) on unix but a non-Copy enum on windows; -// a fn would have to pick by-value (moves on windows) or by-ref -// (clippy::trivially_copy_pass_by_ref on unix). -macro_rules! assert_stdio_result { - ($result:expr) => {{ - #[cfg(all(debug_assertions, unix))] - if let Some(fd) = &$result { - debug_assert!(fd.is_valid()); - } - }}; +#[inline] +pub(crate) fn assert_stdio_result(result: Option) { + if let Some(fd) = result { + debug_assert!(fd.is_valid()); + } } -pub(crate) use assert_stdio_result; bun_jsc::impl_abort_handle_owner!(Subprocess<'static>, abort_handle, |this, cause| { // SAFETY: trait contract — `this` is live. @@ -371,18 +347,11 @@ impl Subprocess<'_> { break 'brk r; } + // A live process can be asked on Windows; POSIX only learns it from `wait4`. #[cfg(windows)] - { - let rusage = - if let spawn_process::Poller::Uv(uv_proc) = &mut self.process_mut().poller { - Some(spawn_process::uv_getrusage(uv_proc)) - } else { - None - }; - if let Some(r) = rusage { - self.pid_rusage.set(Some(r)); - break 'brk r; - } + if let Some(r) = self.process().rusage() { + self.pid_rusage.set(Some(r)); + break 'brk r; } return Ok(JSValue::UNDEFINED); @@ -821,7 +790,9 @@ impl Subprocess<'_> { }; // `ipc()` centralises the single unsafe `JsCell` deref; `do_send` may // re-enter JS, but only the SendQueue is borrowed, not `*self`. - crate::ipc_host::do_send(this.ipc(), global, call_frame, context, this.pid() as u32) + crate::ipc_host::do_send(this.ipc(), global, call_frame, context, &|| { + this.pid() as u32 + }) } pub(crate) fn disconnect_ipc(&self, next_tick: bool) { @@ -868,64 +839,25 @@ impl Subprocess<'_> { array.push(global, JSValue::NULL)?; // TODO: align this with options array.push(global, JSValue::NULL)?; // TODO: align this with options - // Once the values are visible to JS the caller owns them (it hands - // them to `net.connect({ fd })`, which closes them with the socket). - // Our `uv_pipe_t` would close the same HANDLE again when this - // Subprocess is finalized, so expose a duplicate instead. The pipe is - // closed right away: as long as our handle stayed open, the child - // would not see EOF when the caller closes its copy. The duplicate is - // kept so later reads return the same value. - #[cfg(windows)] - this.stdio_pipes.with_mut(|pipes| { - for slot in pipes.iter_mut() { - let buffer = match core::mem::take(slot) { - StdioResult::Buffer(buffer) => buffer, - other => { - *slot = other; - continue; - } - }; - let handle = buffer.fd(); - // On failure the slot stays `Unavailable` and reads as null. - if handle != bun_sys::windows::libuv::INVALID_HANDLE_VALUE { - if let Ok(dup) = bun_sys::dup(bun_sys::Fd::from_system(handle)) { - *slot = StdioResult::UnownedFd(dup); - } - } - // `uv_close` is async; `close_and_destroy` frees the pipe from - // the close callback. - // SAFETY: Box-allocated uv::Pipe owned by this slot until now. - unsafe { bun_sys::windows::libuv::Pipe::close_and_destroy(Box::into_raw(buffer)) }; - } - }); - for item in this.stdio_pipes.get().iter() { - #[cfg(windows)] - { - if let StdioResult::UnownedFd(fd) = item { - // Expose the numeric HANDLE value. - let handle: usize = fd.native() as usize; - array.push(global, JSValue::js_number(handle as f64))?; - } else { - array.push(global, JSValue::NULL)?; + match item { + ExtraPipe::OwnedFd(fd) | ExtraPipe::UnownedFd(fd) => { + // On Windows the number is the HANDLE value, which is what + // `net.connect({ fd })` takes there. + #[cfg(windows)] + let number = fd.native() as usize as f64; + #[cfg(not(windows))] + let number = fd.native() as f64; + array.push(global, JSValue::js_number(number))?; } - } - #[cfg(not(windows))] - { - match item { - ExtraPipe::OwnedFd(fd) | ExtraPipe::UnownedFd(fd) => { - array.push(global, JSValue::js_number(fd.native() as f64))?; - } - ExtraPipe::Unavailable => { - array.push(global, JSValue::NULL)?; - } + ExtraPipe::Unavailable => { + array.push(global, JSValue::NULL)?; } } } // The raw fd numbers are now visible to JS and the caller owns them. // Downgrade so finalize_streams never closes a number JS may have // already closed (whose value the kernel may have since recycled). - #[cfg(not(windows))] this.stdio_pipes.with_mut(|pipes| { for slot in pipes.iter_mut() { if let ExtraPipe::OwnedFd(fd) = *slot { @@ -1051,6 +983,8 @@ impl Subprocess<'_> { unsafe { (*reader).unpause(); bun_io::BufferedReader::read(reader); + #[cfg(windows)] + bun_io::BufferedReader::drain(reader); } } } @@ -1062,6 +996,8 @@ impl Subprocess<'_> { unsafe { (*reader).unpause(); bun_io::BufferedReader::read(reader); + #[cfg(windows)] + bun_io::BufferedReader::drain(reader); } } } @@ -1278,26 +1214,13 @@ impl Subprocess<'_> { self.close_io(StdioKind::Stdout); self.close_io(StdioKind::Stderr); - #[cfg(windows)] - for item in self.stdio_pipes.replace(Vec::new()) { - if let StdioResult::Buffer(buffer) = item { - // `uv_close` is async — the pipe must outlive this scope until the - // close callback reclaims the allocation; `close_and_destroy` also - // copes with a pipe that never got `uv_pipe_init`'d. - // SAFETY: Box-allocated uv::Pipe owned by this slot until now. - unsafe { bun_sys::windows::libuv::Pipe::close_and_destroy(Box::into_raw(buffer)) }; + for item in self.stdio_pipes.get().iter() { + match item { + ExtraPipe::OwnedFd(fd) => fd.close(), + ExtraPipe::UnownedFd(_) | ExtraPipe::Unavailable => {} } } - #[cfg(not(windows))] - { - for item in self.stdio_pipes.get().iter() { - match item { - ExtraPipe::OwnedFd(fd) => fd.close(), - ExtraPipe::UnownedFd(_) | ExtraPipe::Unavailable => {} - } - } - self.stdio_pipes.with_mut(|v| v.clear()); - } + self.stdio_pipes.with_mut(|v| v.clear()); self.stdio_pipes.with_mut(|v| v.shrink_to_fit()); } @@ -1519,58 +1442,15 @@ pub(crate) fn source_from_blob(b: webcore::AnyBlob) -> Source { Source::Any(Box::new(b)) } -/// Windows: the extra stdio pipes (`stdio_pipes`) are uv handles this -/// Subprocess owns without a reader/writer in front of them; record that so a -/// thread teardown closes them through us (and `finalize_streams` then finds -/// the slots empty) instead of anyone closing them twice. -#[cfg(windows)] -impl Subprocess<'_> { - pub(crate) fn record_stdio_pipe_ownership(this: *mut Self) { - // SAFETY: `this` is the live boxed Subprocess (stable address). - let me = unsafe { &*this }; - for item in me.stdio_pipes.get().iter() { - if let StdioResult::Buffer(buffer) = item { - bun_sys::windows::libuv::open_handles::set_owner( - core::ptr::from_ref::(&**buffer) - .cast_mut() - .cast(), - this.cast(), - Some(Self::stop_for_vm_teardown), - ); - } - } - } - - /// `uv::open_handles` entry point: close every stdio pipe still held here. - unsafe fn stop_for_vm_teardown(this: *mut core::ffi::c_void) { - // SAFETY: recorded by `record_stdio_pipe_ownership` for this live - // Subprocess; each pipe leaves the list as its uv_close is issued. - let me = unsafe { &*this.cast::() }; - for item in me.stdio_pipes.replace(Vec::new()) { - if let StdioResult::Buffer(buffer) = item { - // SAFETY: Box-allocated uv::Pipe owned by this slot until now. - unsafe { bun_sys::windows::libuv::Pipe::close_and_destroy(Box::into_raw(buffer)) }; - } - } - } -} - -#[cfg(windows)] -pub(crate) extern "C" fn on_pipe_close(this: *mut bun_sys::windows::libuv::Pipe) { - // safely free the pipes - // SAFETY: pipe was heap-allocated when created; we are the close callback owner. - drop(unsafe { bun_core::heap::take(this) }); -} - pub(crate) mod testing_apis { use super::*; /// Inject a synthetic read error into a subprocess's stdout/stderr - /// PipeReader, as if the underlying read() syscall (Posix) or libuv read - /// callback (Windows) had failed with EBADF. Used by tests to exercise + /// PipeReader, as if the underlying read() syscall (Posix) or pipe read + /// completion (Windows) had failed with EBADF. Used by tests to exercise /// the onReaderError cleanup path, which is otherwise very hard to /// trigger deterministically — on Windows in particular, peer death on - /// a named pipe maps to UV_EOF rather than an error. + /// a named pipe reads as EOF rather than an error. /// /// Returns true if an error was injected, false if the given stdio is /// not (or no longer) a buffered pipe reader. @@ -1602,13 +1482,11 @@ pub(crate) mod testing_apis { return Ok(JSValue::FALSE); }; - // Mirror what the real error path does (onStreamRead on Windows, - // read() on Posix) so the teardown exercised is identical. + // Mirror what the real error path does (a failed read has already + // stopped the source on Windows) so the teardown exercised is identical. let fake_err = bun_sys::Error::from_code(bun_sys::Errno::EBADF, bun_sys::Tag::read); #[cfg(windows)] - { - let _ = Readable::pipe_reader_mut(pipe).reader.stop_reading(); - } + Readable::pipe_reader_mut(pipe).reader.pause(); let reader = &raw mut Readable::pipe_reader_mut(pipe).reader; // SAFETY: live pipe reader; `on_error` is the raw entry so the // (maybe-freeing) error dispatch runs under no receiver protector. @@ -1616,15 +1494,12 @@ pub(crate) mod testing_apis { Ok(JSValue::TRUE) } - /// Close the stdin pipe's writer as a Windows worker's stop phase does: no JS wrapper, no ref. + /// Close the stdin pipe's writer with no JS wrapper and no ref. #[bun_jsc::host_fn] pub(crate) fn close_stdin_writer( global_this: &JSGlobalObject, callframe: &CallFrame, ) -> JsResult { - #[cfg(windows)] - use bun_io::pipe_writer::BaseWindowsPipeWriter as _; - let [subprocess_value] = callframe.arguments_as_array::<1>(); let Some(subprocess_ptr) = Subprocess::from_js(subprocess_value) else { return Err(global_this.throw(format_args!("first argument must be a Subprocess"))); diff --git a/src/runtime/api/bun/subprocess/Readable.rs b/src/runtime/api/bun/subprocess/Readable.rs index 19b9cefaf402..b7bf85efc4f7 100644 --- a/src/runtime/api/bun/subprocess/Readable.rs +++ b/src/runtime/api/bun/subprocess/Readable.rs @@ -14,8 +14,8 @@ use bun_io::max_buf::MaxBuf; use bun_ptr::RefPtr; use bun_ptr::cow_slice::CowSlice; +use super::Subprocess; use super::subprocess_pipe_reader::PipeReader; -use super::{StdioResult, Subprocess}; // `bun.ptr.CowString` — owned/borrowed byte slice (has // `init_owned` / `length` / `take_slice`). @@ -91,51 +91,24 @@ impl Readable { stdio: Stdio, event_loop: NonNull, process: NonNull>, - result: StdioResult, + result: Option, max_size: Option>, _is_sync: bool, ) -> Readable { - super::assert_stdio_result!(result); + super::assert_stdio_result(result); #[cfg(any(target_os = "linux", target_os = "android"))] let mut stdio = stdio; - #[cfg(unix)] - { - if matches!(stdio, Stdio::Pipe) { - let _ = bun_sys::set_nonblocking(result.unwrap()); - } - } - match &stdio { Stdio::Inherit => Readable::Inherit, Stdio::Ignore | Stdio::Ipc | Stdio::Path(..) => Readable::Ignore, - Stdio::Fd(fd) => { - #[cfg(unix)] - { - let _ = fd; - Readable::Fd(result.unwrap()) - } - #[cfg(not(unix))] - { - Readable::Fd(*fd) - } - } + Stdio::Fd(fd) => Readable::Fd(*fd), #[cfg(any(target_os = "linux", target_os = "android"))] Stdio::Memfd(_) => { // Ownership of the fd moves into the Readable; `Stdio`'s Drop would close it. Readable::Memfd(stdio.take_memfd().unwrap()) } - Stdio::Dup2(dup2) => { - #[cfg(unix)] - { - let _ = dup2; - panic!("TODO: implement dup2 support in Stdio readable"); - } - #[cfg(not(unix))] - { - Readable::Fd(dup2.out.to_fd()) - } - } + Stdio::Dup2(_) => panic!("TODO: implement dup2 support in Stdio readable"), Stdio::Pipe => { Readable::Pipe(PipeReader::create(event_loop, process, result, max_size)) } @@ -181,24 +154,21 @@ impl Readable { let Readable::Pipe(pipe) = mem::replace(self, Readable::Closed) else { unreachable!() }; - #[cfg(unix)] - { - let release_start_ref = { - let reader = Self::pipe_reader_mut(&pipe); - if reader.process.is_some() - && matches!(reader.state, super::subprocess_pipe_reader::State::Pending) - && reader.ref_count.get() > 1 - { - reader.reader.deinit(); - true - } else { - false - } - }; - if release_start_ref { - // SAFETY: guard above proved a second ref exists; this deref cannot reach zero. - unsafe { PipeReader::deref(pipe.as_ptr()) }; + let release_start_ref = { + let reader = Self::pipe_reader_mut(&pipe); + if reader.process.is_some() + && matches!(reader.state, super::subprocess_pipe_reader::State::Pending) + && reader.ref_count.get() > 1 + { + reader.reader.deinit(); + true + } else { + false } + }; + if release_start_ref { + // SAFETY: guard above proved a second ref exists; this deref cannot reach zero. + unsafe { PipeReader::deref(pipe.as_ptr()) }; } Self::pipe_reader_mut(&pipe).process = None; } diff --git a/src/runtime/api/bun/subprocess/SubprocessPipeReader.rs b/src/runtime/api/bun/subprocess/SubprocessPipeReader.rs index 815491f94bd3..82d8756cd4ae 100644 --- a/src/runtime/api/bun/subprocess/SubprocessPipeReader.rs +++ b/src/runtime/api/bun/subprocess/SubprocessPipeReader.rs @@ -2,18 +2,16 @@ use core::ptr::NonNull; use crate::webcore::ReadableStream; use bun_io::BufferedReader; -#[cfg(unix)] -use bun_io::FilePollFlag; use bun_io::Loop as AsyncLoop; use bun_io::max_buf::MaxBuf; -#[cfg(unix)] -use bun_io::pipe_reader::PosixFlags; +use bun_io::pipe_reader::ReaderFlags; use bun_jsc::event_loop::EventLoop; use bun_jsc::{JSGlobalObject, JSValue, JsResult}; use bun_ptr::{ParentRef, RefCount, RefPtr}; +use bun_sys::{self, Fd, FdExt as _}; use super::readable::Readable; -use super::{StdioKind, StdioResult, Subprocess}; +use super::{StdioKind, Subprocess}; pub(crate) type IOReader = BufferedReader; @@ -46,7 +44,7 @@ pub(crate) struct PipeReader { pub(crate) event_loop_handle: bun_jsc::EventLoopHandle, pub(crate) ref_count: RefCount, pub(crate) state: State, - pub(crate) stdio_result: StdioResult, + pub(crate) stdio_result: Option, } // `pub const ref/deref = RefCount.ref/deref` — thin forwarders so existing call @@ -103,7 +101,7 @@ impl PipeReader { pub(crate) fn create( event_loop: NonNull, process: NonNull>, - result: StdioResult, + result: Option, limit: Option>, ) -> RefPtr { let mut this = Box::new(PipeReader { @@ -116,16 +114,6 @@ impl PipeReader { state: State::Pending, }); MaxBuf::add_to_pipereader(limit, &mut this.reader.maxbuf); - #[cfg(windows)] - { - // On Windows `StdioResult` is the `WindowsStdioResult` enum and the - // `.buffer` payload is a heap-allocated `uv::Pipe`. Ownership - // transfers to `reader.source`; `stdio_result` is left `Unavailable`. - if let StdioResult::Buffer(pipe) = this.stdio_result.take() { - this.reader.set_source(bun_io::Source::Pipe(pipe)); - } - } - let raw: *mut PipeReader = bun_core::heap::into_raw(this); // SAFETY: `raw` is a valid, freshly-boxed PipeReader. unsafe { @@ -152,75 +140,42 @@ impl PipeReader { self.process = Some(ParentRef::from(process)); self.event_loop = event_loop.into(); self.event_loop_handle = bun_jsc::EventLoopHandle::init(event_loop.as_ptr().cast::<()>()); - #[cfg(windows)] - { - if lazy { - // Leave IS_PAUSED set (the init default) so uv_read_start is - // deferred until JS first pulls; the kernel pipe buffer then - // provides backpressure and the child blocks. - let reader_ptr = core::ptr::from_mut(&mut self.reader).cast::(); - if let Some(source) = self.reader.source.as_mut() { - source.set_data(reader_ptr); - } - self.reader - .flags - .remove(bun_io::pipe_reader::WindowsFlags::IS_DONE); - return; - } - // Hold one more ref so `self` survives the on_reader_error() teardown - // below long enough to return; matches the POSIX keepalive. - // - // SAFETY: `self` is live; RefPtr bumps the intrusive refcount and - // derefs on Drop. The deref may free `*self`, but no borrow of `self` - // outlives the guard's drop on return. - let _guard = unsafe { RefPtr::init_ref(std::ptr::from_mut::(self)) }; - if let bun_sys::Result::Err(err) = self.reader.start_with_current_pipe() { - // Route through the same teardown as a read-callback error - // (matches POSIX's register_poll failure path): state=Err, - // detach from the Subprocess via on_close_io, release the - // start() ref, and let the caller proceed to the sibling pipe. - // Returning Err would have the caller throw after try_kill - // without unwinding this pipe or the never-started sibling, - // and on_process_exit's later drain then double-derefs them. - self.on_reader_error(err); - } + if lazy { + // Defer reading until JS first pulls so the kernel pipe buffer + // provides backpressure and the child blocks. + self.reader.flags.insert(ReaderFlags::IS_PAUSED); + } + // On POSIX `start()` always returns Ok(()); if poll registration fails + // it synchronously invokes onReaderError() first, which drops both the + // Readable.pipe ref (via onCloseIO) and the ref we just took above. + // Hold one more ref so `this` survives long enough to check state + // after start() returns. + // + // SAFETY: `self` is live; RefPtr bumps the intrusive refcount and + // derefs on Drop. The deref may free `*self`, but no borrow of `self` + // outlives the guard's drop on return. + let _guard = unsafe { RefPtr::init_ref(std::ptr::from_mut::(self)) }; + + let fd = self.stdio_result.unwrap(); + if let bun_sys::Result::Err(err) = self.reader.start(fd, true) { + // Windows returns a failed start instead of reporting it, and the + // reader did not take `fd`. Route it through the same teardown: + // state=Err, detach from the Subprocess via on_close_io, release + // the start() ref, and let the caller proceed to the sibling pipe. + fd.close(); + self.on_reader_error(err); } - #[cfg(not(windows))] + #[cfg(unix)] { - if lazy { - // Defer poll registration until JS first pulls so the kernel - // pipe buffer provides backpressure and the child blocks. - self.reader.flags.insert(PosixFlags::IS_PAUSED); - } - // PosixBufferedReader.start() always returns Ok(()); if poll - // registration fails it synchronously invokes onReaderError() first, - // which drops both the Readable.pipe ref (via onCloseIO) and the ref we - // just took above. Hold one more ref so `this` survives long enough to - // check state after start() returns. - // - // SAFETY: `self` is live; RefPtr bumps the intrusive refcount and - // derefs on Drop. The deref may free `*self`, but no borrow of `self` - // outlives the guard's drop on return. - let _guard = unsafe { RefPtr::init_ref(std::ptr::from_mut::(self)) }; - - let _ = self.reader.start(self.stdio_result.unwrap(), true); - - #[cfg(unix)] - { - if matches!(self.state, State::Err(..)) { - // onReaderError already ran; `_guard`'s Drop on return - // will drop the last ref and deinit() closes the handle. - return; - } - if let Some(poll) = self.reader.handle.get_poll() { - poll.set_flag(FilePollFlag::Socket); - poll.set_flag(FilePollFlag::Nonblocking); - } - self.reader - .flags - .insert(PosixFlags::SOCKET | PosixFlags::NONBLOCKING | PosixFlags::POLLABLE); + if matches!(self.state, State::Err(..)) { + // onReaderError already ran; `_guard`'s Drop on return + // will drop the last ref and deinit() closes the handle. + return; } + self.reader + .flags + .insert(ReaderFlags::SOCKET | ReaderFlags::NONBLOCKING | ReaderFlags::POLLABLE); } } @@ -371,16 +326,7 @@ impl PipeReader { .virtual_machine .map(bun_ptr::BackRef::from) .expect("event_loop.virtual_machine"); - let uws = vm.uws_loop(); - #[cfg(windows)] - { - // SAFETY: uws loop pointer is live for the VM lifetime. - unsafe { (*uws).uv_loop } - } - #[cfg(not(windows))] - { - uws.cast() - } + vm.uws_loop().cast() } } diff --git a/src/runtime/api/bun/subprocess/Writable.rs b/src/runtime/api/bun/subprocess/Writable.rs index b9a5c9b90137..bdb089c4a093 100644 --- a/src/runtime/api/bun/subprocess/Writable.rs +++ b/src/runtime/api/bun/subprocess/Writable.rs @@ -2,7 +2,6 @@ use core::ffi::c_void; use bun_jsc::{JSGlobalObject, JSValue, event_loop::EventLoop}; use bun_ptr::RefPtr; -#[cfg(not(windows))] use bun_sys::FdExt; use bun_sys::{self, Fd}; @@ -11,10 +10,8 @@ use crate::node::types::FdJsc; use crate::webcore::file_sink::{self, FileSink}; use crate::webcore::sink; use crate::webcore::streams::SourceHandle; -#[cfg(windows)] -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; -use super::{Flags, StaticPipeWriter, StdioResult, Subprocess, js}; +use super::{Flags, StaticPipeWriter, Subprocess, js}; pub(crate) enum Writable<'a> { Pipe(RefPtr), @@ -124,10 +121,10 @@ impl<'a> Writable<'a> { stdio: &mut Stdio, event_loop: &EventLoop, subprocess: &mut Subprocess<'a>, - result: StdioResult, + result: Option, promise_for_stream: &mut JSValue, ) -> crate::Result> { - super::assert_stdio_result!(result); + super::assert_stdio_result(result); let global = event_loop.global_ref(); @@ -142,101 +139,6 @@ impl<'a> Writable<'a> { .cast::<()>(), ); - #[cfg(windows)] - { - match stdio { - Stdio::Pipe | Stdio::ReadableStream(_) => { - if let StdioResult::Buffer(buffer) = result { - // Ownership of the `Box` transfers to the - // FileSink's writer (the sink takes over the heap - // pointer). - let uv_pipe: *mut _ = bun_core::heap::into_raw(buffer); - let pipe_ref = FileSink::create_with_pipe(evtloop, uv_pipe); - let pipe = Self::pipe_sink_mut(&pipe_ref); - - match pipe.writer.with_mut(|w| w.start_with_current_pipe()) { - bun_sys::Result::Ok(()) => {} - bun_sys::Result::Err(_err) => { - if let Stdio::ReadableStream(rs) = stdio { - rs.cancel(global)?; - } - return Err(crate::Error::UnexpectedCreatingStdin); - } - } - pipe.writer.with_mut(|w| w.set_parent(pipe_ref.as_ptr())); - subprocess - .weak_file_sink_stdin_ptr - .set(Some(pipe_ref.as_non_null())); - subprocess.ref_(); - subprocess.update_flags(|f| { - f.set(Flags::DEREF_ON_STDIN_DESTROYED, true); - f.set(Flags::HAS_STDIN_DESTRUCTOR_CALLED, false); - }); - - if let Stdio::ReadableStream(rs) = stdio { - let assign_result = pipe.assign_to_stream(rs, global); - if let Some(err_val) = assign_result.to_error() { - subprocess.weak_file_sink_stdin_ptr.set(None); - subprocess.update_flags(|f| { - f.set(Flags::DEREF_ON_STDIN_DESTROYED, false) - }); - subprocess.deref(); - let _ = global.throw_value(err_val); - return Err(crate::Error::JSError); - } - *promise_for_stream = assign_result; - } - - return Ok(Writable::Pipe(pipe_ref)); - } - return Ok(Writable::Inherit); - } - - Stdio::Blob(_) => { - // See the unix arm below: Stdio has Drop, so move the - // payload out via ManuallyDrop + ptr::read. - let owned = - core::mem::ManuallyDrop::new(core::mem::replace(stdio, Stdio::Ignore)); - let blob = match &*owned { - // SAFETY: owned is ManuallyDrop; payload moved exactly once. - Stdio::Blob(b) => unsafe { core::ptr::read(b) }, - _ => unreachable!(), - }; - return Ok(Writable::Buffer(StaticPipeWriter::create( - evtloop, - subprocess as *mut Subprocess<'a>, - result, - super::source_from_blob(blob), - ))); - } - Stdio::Fd(fd) => { - return Ok(Writable::Fd(*fd)); - } - Stdio::Dup2(dup2) => { - return Ok(Writable::Fd(dup2.to.to_fd())); - } - Stdio::Inherit => { - return Ok(Writable::Inherit); - } - Stdio::Path(_) | Stdio::Ignore => { - return Ok(Writable::Ignore); - } - Stdio::Ipc | Stdio::Capture(_) => { - return Ok(Writable::Ignore); - } - // Rejected at i < 3 in Stdio::extract(); stdin never sees this. - Stdio::SocketFd => unreachable!("SocketFd at stdin"), - } - } - - #[cfg(unix)] - { - if matches!(stdio, Stdio::Pipe) { - let _ = bun_sys::set_nonblocking(result.unwrap()); - } - } - - #[cfg(not(windows))] match stdio { Stdio::Dup2(_) => panic!("TODO dup2 stdio"), Stdio::Pipe | Stdio::ReadableStream(_) => { @@ -258,6 +160,7 @@ impl<'a> Writable<'a> { // `handle` is `PollOrFd` (enum); flag mutation goes // through the FilePoll vtable shim. + #[cfg(unix)] pipe.writer.with_mut(|w| { if let Some(poll) = w.handle.get_poll() { poll.set_flag(bun_io::FilePollFlag::Socket); @@ -319,7 +222,7 @@ impl<'a> Writable<'a> { debug_assert!(*fd != Fd::INVALID); Ok(Writable::Memfd(*fd)) } - Stdio::Fd(_) => Ok(Writable::Fd(result.unwrap())), + Stdio::Fd(fd) => Ok(Writable::Fd(*fd)), Stdio::Inherit => Ok(Writable::Inherit), Stdio::Path(_) | Stdio::Ignore => Ok(Writable::Ignore), Stdio::Ipc | Stdio::Capture(_) => Ok(Writable::Ignore), diff --git a/src/runtime/api/cron.rs b/src/runtime/api/cron.rs index 3f6c57acbc4f..02e311d31118 100644 --- a/src/runtime/api/cron.rs +++ b/src/runtime/api/cron.rs @@ -33,7 +33,6 @@ use bun_resolver::fs::FileSystem; #[cfg(not(target_os = "macos"))] use bun_resolver::fs::RealFS; -#[cfg(not(windows))] use crate::api::bun::process::SpawnResultExt as _; use crate::api::bun::process::{ self as spawn, Process, ProcessHandle, Rusage, SpawnOptions, Status, @@ -169,10 +168,7 @@ trait CronJobBase: Sized + bun_ptr::AnyRefCounted { } fn loop_(&self) -> *mut AsyncLoop { - // `VirtualMachine::uv_loop` already returns the native loop on both - // targets (jsc/VirtualMachine.rs:2975); the prior POSIX arm's - // `bun_uws::Loop::get()` named the same per-thread singleton. - vm_mut().uv_loop() + vm_mut().uws_loop() } fn note_reader_done(&self) { @@ -2154,57 +2150,22 @@ fn spawn_cmd_prepare( } }; - // Ownership note: BOTH - // `Source::Pipe` and `WindowsStdioResult::Buffer` own a `Box`, - // and `spawn_process_windows` `heap::take`s the raw `Stdio::Buffer` - // pointer into `WindowsStdioResult::Buffer` on success. Pre-stashing the - // Box in `stderr_reader.source` here (the original transliteration) would - // create TWO `Box` over one allocation — UB under Stacked - // Borrows even with a `mem::forget` of the duplicate, because moving the - // first Box into `Source::Pipe` reasserts its `Unique` tag and kills the - // raw pointer's provenance before `spawn_process_windows` ever - // dereferences it. Instead hand the raw heap pointer to `Stdio::Buffer` - // alone (sole owner), let `spawn_process_windows` round-trip it through - // `heap::take`, and stash the returned Box in `stderr_reader.source` - // AFTER spawn — see the `#[cfg(windows)]` block below and - // `lifecycle_script_runner.rs` / `filter_run.rs` for the canonical - // pattern. On spawn error, `WindowsStdio` has no `Drop`; reclaim - // explicitly via `spawn_options.stderr.deinit()`. - #[cfg(windows)] - let stderr_pipe_ptr: *mut bun_sys::windows::libuv::Pipe = - bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed::< - bun_sys::windows::libuv::Pipe, - >())); let cwd = FileSystem::get().top_level_dir; let spawn_options = SpawnOptions { stdin: stdin_opt, stdout: stdout_opt, #[cfg(windows)] - stderr: spawn::Stdio::Buffer(stderr_pipe_ptr), + stderr: spawn::Stdio::Buffer, #[cfg(not(windows))] stderr: spawn::Stdio::Ignore, cwd: cwd.into(), argv0: resolved_argv0, - #[cfg(windows)] - windows: spawn::WindowsOptions { - loop_: EventLoopHandle::init(vm_mut().event_loop().cast::<()>()), - ..Default::default() - }, ..SpawnOptions::default() }; - // `mut` only for the Windows error-path `spawn_options.stderr.deinit()`. - #[cfg(windows)] - let mut spawn_options = spawn_options; let spawned = match spawn::spawn_process_cstr(&spawn_options, argv, env) { Ok(Ok(sp)) => sp, Ok(Err(err)) => { - // `spawn_process_windows` only `heap::take`s the `Stdio::Buffer` - // raw `*mut uv::Pipe` on the SUCCESS path; on every error return - // ownership stays with the caller and `WindowsStdio` has no - // `Drop`. Reclaim it (uv_close + free if init'd) here. - #[cfg(windows)] - spawn_options.stderr.deinit(); this.set_err(format_args!( "Failed to spawn process: {}", bstr::BStr::new(err.name()) @@ -2212,14 +2173,11 @@ fn spawn_cmd_prepare( return Err(()); } Err(e) => { - #[cfg(windows)] - spawn_options.stderr.deinit(); this.set_err(format_args!("Failed to spawn process: {}", e.name())); return Err(()); } }; - #[cfg(windows)] - let mut spawned = spawned; + let ev_handle = EventLoopHandle::init(vm_mut().event_loop().cast::<()>()); #[cfg(unix)] { @@ -2229,12 +2187,13 @@ fn spawn_cmd_prepare( let _ = sys::set_nonblocking(stdout); this.remaining_fds().set(this.remaining_fds().get() + 1); let started = this.stdout_reader().with_mut(|r| { - use bun_io::pipe_reader::PosixFlags; - r.flags.insert(PosixFlags::NONBLOCKING | PosixFlags::SOCKET); + use bun_io::pipe_reader::ReaderFlags; + r.flags + .insert(ReaderFlags::NONBLOCKING | ReaderFlags::SOCKET); r.flags.remove( - PosixFlags::MEMFD - | PosixFlags::RECEIVED_EOF - | PosixFlags::CLOSED_WITHOUT_REPORTING, + ReaderFlags::MEMFD + | ReaderFlags::RECEIVED_EOF + | ReaderFlags::CLOSED_WITHOUT_REPORTING, ); r.start(stdout, true) }); @@ -2242,11 +2201,6 @@ fn spawn_cmd_prepare( this.set_err(format_args!("Failed to start reading stdout")); return Err(()); } - this.stdout_reader().with_mut(|r| { - if let Some(p) = r.handle.get_poll() { - p.set_flag(bun_io::FilePollFlag::Socket); - } - }); } else { this.stdout_reader().with_mut(|r| { r.set_parent(this_ptr); @@ -2256,34 +2210,24 @@ fn spawn_cmd_prepare( } } #[cfg(windows)] - { - // `spawn_process_windows` has `heap::take`n `stderr_pipe_ptr` out of - // `Stdio::Buffer` into `spawned.stderr` as - // `WindowsStdioResult::Buffer(Box)`. Take that Box out - // *here* (sole owner — single `into_raw` → `from_raw` round-trip, no - // aliasing Box) and stash it in `stderr_reader.source` BEFORE - // `start_with_current_pipe` (which reads `source.?.pipe`) and BEFORE - // `spawned` drops — otherwise `WindowsSpawnResult::Drop` would - // `uv_close`+free the live, libuv-registered handle (UAF in the read - // callback + double-free on reader close). - if let spawn::WindowsStdioResult::Buffer(pipe) = spawned.stderr.take() { - debug_assert!(core::ptr::eq(Box::as_ref(&pipe), stderr_pipe_ptr)); - this.stderr_reader().with_mut(|r| { - r.set_source(bun_io::Source::Pipe(pipe)); - r.set_parent(this_ptr); - }); - this.remaining_fds().set(this.remaining_fds().get() + 1); - let started = this - .stderr_reader() - .with_mut(|r| r.start_with_current_pipe()); - if started.is_err() { - this.set_err(format_args!("Failed to start reading stderr")); - return Err(()); - } + if let Some(stderr) = spawned.stderr { + this.stderr_reader().with_mut(|r| r.set_parent(this_ptr)); + this.remaining_fds().set(this.remaining_fds().get() + 1); + if this + .stderr_reader() + .with_mut(|r| r.start(stderr, true)) + .is_err() + { + // The reader did not take the pipe, and nothing will watch the + // process: the `Process` closes its handle when it drops. + use bun_sys::FdExt as _; + stderr.close(); + drop(spawned.to_process_handle(ev_handle)); + this.set_err(format_args!("Failed to start reading stderr")); + return Err(()); } } - let ev_handle = EventLoopHandle::init(vm_mut().event_loop().cast::<()>()); Ok(spawned.to_process_handle(ev_handle)) } diff --git a/src/runtime/api/streams.classes.ts b/src/runtime/api/streams.classes.ts index b3e09b0533d5..a4f3104cab81 100644 --- a/src/runtime/api/streams.classes.ts +++ b/src/runtime/api/streams.classes.ts @@ -72,10 +72,6 @@ function source(name) { : {}), ...(name === "File" ? { - setRawMode: { - fn: "setRawModeFromJS", - length: 1, - }, setFlowing: { fn: "setFlowingFromJS", length: 1, diff --git a/src/runtime/bake/DevServer.rs b/src/runtime/bake/DevServer.rs index b3353e38ff64..a9302bdbc518 100644 --- a/src/runtime/bake/DevServer.rs +++ b/src/runtime/bake/DevServer.rs @@ -798,8 +798,8 @@ pub(crate) fn init(options: Options) -> JsResult> { Ok(s) => s, Err(e) => Output::panic(format_args!("unhandled {}", e)), }; - // Note: `sys::Stat` is `libc::stat` on POSIX / `uv_stat_t` on - // Windows (where mtime is `mtim.sec`). Debug-only cache-bust key. + // Note: `sys::Stat` is `libc::stat` on POSIX / `windows::fs::Stat` + // on Windows (where mtime is `mtim.sec`). Debug-only cache-bust key. #[cfg(not(windows))] bun_core::write_any_to_hasher(&mut h, stat.st_mtime as i64); #[cfg(windows)] diff --git a/src/runtime/bin_entry/mod.rs b/src/runtime/bin_entry/mod.rs index b1f3c207d77e..ce0967fb4b54 100644 --- a/src/runtime/bin_entry/mod.rs +++ b/src/runtime/bin_entry/mod.rs @@ -170,6 +170,9 @@ pub(crate) unsafe extern "C" fn main(argc: c_int, argv: *const *const c_char) -> use_mimalloc_in_dependencies(); + // Before any thread: reading the umask sets it for a moment. + bun_sys::init_umask(); + // SIGPIPE/SIGXFSZ → SIG_IGN. // SAFETY: `SIGPIPE`/`SIGXFSZ` are valid signal numbers and `SIG_IGN` is a // valid disposition; called once on the main thread before any other @@ -242,19 +245,4 @@ fn use_mimalloc_in_dependencies() { safe fn Bun__useMimallocForICU(); } Bun__useMimallocForICU(); - - // libuv (only linked on Windows). Must precede the first uv call. Left on - // the CRT heap under ASAN like ICU and BoringSSL, so the sanitizer sees - // its allocations too (the global allocator is `System` there as well). - // SAFETY: mimalloc fns match the libuv allocator signatures; called - // exactly once before any uv handle is created. - #[cfg(all(windows, not(bun_asan)))] - unsafe { - let _ = bun_sys::windows::libuv::uv_replace_allocator( - Some(bun_alloc::mimalloc::mi_malloc), - Some(bun_alloc::mimalloc::mi_realloc), - Some(bun_alloc::mimalloc::mi_calloc), - Some(bun_alloc::mimalloc::mi_free), - ); - } } diff --git a/src/runtime/cli/bunx_command.rs b/src/runtime/cli/bunx_command.rs index 42af649bb36a..271ab3b701bd 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -1373,29 +1373,6 @@ impl BunxCommand { stdout: proc_sync::SyncStdio::Inherit, stdin: proc_sync::SyncStdio::Inherit, - #[cfg(windows)] - windows: proc_sync::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini( - bun_event_loop::MiniEventLoop::init_global( - // `this_transpiler.env` is the process-lifetime loader - // singleton populated during transpiler init. - // - // Aliasing: do NOT call `this_transpiler.env_mut()` here — - // `env_loader` (line 594) is still live and is used again below at the - // post-install `Run::run_binary` calls. A second `env_mut()` would - // `unsafe { &mut *self.env }` from the raw field, popping `env_loader`'s - // Unique tag under Stacked Borrows (UB on later use). Instead reborrow - // *through* `env_loader` so the new `&mut` is a child of its tag; the - // child is consumed by `init_global` (converted to `NonNull`) before - // `env_loader` is touched again. - // SAFETY: `env_loader` is a valid `&'static mut Loader`; this is a - // stacked reborrow, not a sibling alias. - Some(unsafe { &mut *(env_loader as *mut _) }), - None, - ), - ), - ..Default::default() - }, ..Default::default() }) { Err(err) => { diff --git a/src/runtime/cli/create/SourceFileProjectGenerator.rs b/src/runtime/cli/create/SourceFileProjectGenerator.rs index ed058cfcde94..59edc702e861 100644 --- a/src/runtime/cli/create/SourceFileProjectGenerator.rs +++ b/src/runtime/cli/create/SourceFileProjectGenerator.rs @@ -189,13 +189,6 @@ fn run_install(argv: &mut Vec<&[u8]>) -> Result<(), crate::Error> { stdout: spawn_sync::SyncStdio::Inherit, stdin: spawn_sync::SyncStdio::Inherit, - #[cfg(windows)] - windows: bun_process::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini(bun_event_loop::MiniEventLoop::init_global( - None, None, - )), - ..Default::default() - }, ..Default::default() }) { Ok(p) => p, @@ -363,13 +356,6 @@ pub(crate) fn generate_files( stdout: spawn_sync::SyncStdio::Inherit, stdin: spawn_sync::SyncStdio::Inherit, - #[cfg(windows)] - windows: bun_process::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini( - bun_event_loop::MiniEventLoop::init_global(None, None), - ), - ..Default::default() - }, ..Default::default() }) { Ok(p) => p, @@ -425,13 +411,6 @@ pub(crate) fn generate_files( stdout: spawn_sync::SyncStdio::Inherit, stdin: spawn_sync::SyncStdio::Inherit, - #[cfg(windows)] - windows: bun_process::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini(bun_event_loop::MiniEventLoop::init_global( - None, None, - )), - ..Default::default() - }, ..Default::default() }) { Ok(p) => p, diff --git a/src/runtime/cli/create_command.rs b/src/runtime/cli/create_command.rs index 9c8214daee11..de0aaed88d6c 100644 --- a/src/runtime/cli/create_command.rs +++ b/src/runtime/cli/create_command.rs @@ -113,15 +113,6 @@ fn exec_task(task_: &[u8], cwd: &[u8], _path: &[u8], npm_client: Option ProcessHandle<'a> { core::ptr::null(), ]; let start_time = Instant::now(); - let spawned: spawn::SpawnProcessResult = 'brk: { + let spawned: spawn::SpawnResult = 'brk: { // Get the envp with the PATH configured // There's probably a more optimal way to do this where you have a Vec shared // instead of creating a new one for each process @@ -131,53 +130,24 @@ impl<'a> ProcessHandle<'a> { }??; // `_guard` drops here (or on `?` above), restoring PATH. }; - #[cfg(unix)] let (stdout_fd, stderr_fd) = (spawned.stdout, spawned.stderr); - // Windows: `spawn_process_windows` has already moved the heap pipe out of - // `options.stdout/stderr` (via `heap::take`) into `spawned.stdout/stderr` - // as `WindowsStdioResult::Buffer(Box)`. The raw `*mut Pipe` left in - // `options` is dangling-by-design — re-`heap::take`ing it here would be a - // double `Box::from_raw` (UAF + double-free). Take the Box from the - // *result* instead, before `to_process` consumes `spawned`. - #[cfg(windows)] - let mut spawned = spawned; - #[cfg(windows)] - let (stdout_pipe, stderr_pipe) = (spawned.stdout.take(), spawned.stderr.take()); let process = spawned.to_process_handle(EventLoopHandle::init_mini(state.event_loop)); let handle_ptr = std::ptr::from_mut::>(handle).cast::(); handle.stdout.set_parent(handle_ptr); handle.stderr.set_parent(handle_ptr); - #[cfg(windows)] - { - if let spawn::WindowsStdioResult::Buffer(pipe) = stdout_pipe { - handle.stdout.set_source(bun_io::Source::Pipe(pipe)); - } - if let spawn::WindowsStdioResult::Buffer(pipe) = stderr_pipe { - handle.stderr.set_source(bun_io::Source::Pipe(pipe)); - } - } - - #[cfg(unix)] - { - if let Some(stdout) = stdout_fd { - let _ = sys::set_nonblocking(stdout); - handle.remaining_fds += 1; - handle.stdout.start(stdout, true)?; - } - if let Some(stderr) = stderr_fd { - let _ = sys::set_nonblocking(stderr); - handle.remaining_fds += 1; - handle.stderr.start(stderr, true)?; - } - } - #[cfg(not(unix))] - { - handle.remaining_fds += 1; - handle.stdout.start_with_current_pipe()?; + for (fd, reader) in [ + (stdout_fd, &mut handle.stdout), + (stderr_fd, &mut handle.stderr), + ] { + let Some(fd) = fd else { continue }; handle.remaining_fds += 1; - handle.stderr.start_with_current_pipe()?; + if let Err(err) = reader.start(fd, true) { + // A reader that fails to start (Windows only) has not taken the fd. + fd.close(); + return Err(err.into()); + } } handle.process = Some(ProcessInfo { @@ -212,8 +182,7 @@ impl<'a> ProcessHandle<'a> { /// On process exit, read what the pipes already hold, then force-end any /// pipe a leftover child still keeps open: its EOF may never come and - /// must not stall the finish. Windows has no synchronous drain, so only - /// the force-end applies there. + /// must not stall the finish. /// /// # Safety /// `this` is the live handle; the reader callbacks re-enter @@ -228,6 +197,10 @@ impl<'a> ProcessHandle<'a> { if !(*reader).is_done() && (*reader).get_fd() != sys::Fd::INVALID { BufferedReader::read(reader); } + #[cfg(windows)] + if !(*reader).is_done() { + BufferedReader::drain(reader); + } if !(*reader).is_done() { (*reader).deinit(); } @@ -287,7 +260,7 @@ bun_spawn::link_impl_ProcessExit! { impl<'a> ProcessHandle<'a> { fn loop_(&self) -> *mut bun_io::Loop { // SAFETY: state backref valid; event_loop is the live MiniEventLoop singleton. - bun_io::uws_to_native(unsafe { (*self.state.event_loop).loop_ }) + unsafe { (*self.state.event_loop).loop_ptr() } } } @@ -677,7 +650,7 @@ impl<'a> State<'a> { } } - fn finalize(&mut self) -> u8 { + fn finalize(&mut self) -> u32 { if self.aborted { let _ = self.redraw(true); } @@ -690,7 +663,7 @@ impl<'a> State<'a> { } } Status::Signaled(signal) => { - return bun_sys::SignalCode(*signal).to_exit_code(); + return bun_sys::SignalCode(*signal).to_exit_code().into(); } _ => return 1, } @@ -924,7 +897,8 @@ pub(crate) fn run_scripts_with_filter( None, ); // Windows: recursive kill-on-close Job so cmd.exe/.cmd-shim grandchildren - // (which escape libuv's SILENT_BREAKAWAY job) die with us. POSIX: no-op. + // (which silently break away from the job every spawned child is put in) + // die with us. POSIX: no-op. bun_io::ParentDeathWatchdog::ensure_kill_on_close_job(); // --no-orphans: register the macOS kqueue parent watch on this MiniEventLoop // (the VirtualMachine.init path is never reached for --filter). Linux is @@ -999,27 +973,12 @@ pub(crate) fn run_scripts_with_filter( process: None, options: SpawnOptions { stdin: spawn::Stdio::Ignore, - #[cfg(unix)] stdout: spawn::Stdio::Buffer, - #[cfg(not(unix))] - stdout: spawn::Stdio::Buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - ))), - #[cfg(unix)] stderr: spawn::Stdio::Buffer, - #[cfg(not(unix))] - stderr: spawn::Stdio::Buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - ))), cwd: bun_paths::resolve_path::dirname::( &script.package_json_path, ) .into(), - #[cfg(windows)] - windows: spawn::WindowsOptions { - loop_: EventLoopHandle::init_mini(event_loop), - ..Default::default() - }, stream: true, ..Default::default() }, @@ -1119,7 +1078,7 @@ pub(crate) fn run_scripts_with_filter( let status = state.finalize(); - Global::exit(status as u32); + Global::exit(status); } fn has_cycle(current: &mut ProcessHandle) -> bool { diff --git a/src/runtime/cli/link_command.rs b/src/runtime/cli/link_command.rs index 37e454a1ce58..e2cf12246eb5 100644 --- a/src/runtime/cli/link_command.rs +++ b/src/runtime/cli/link_command.rs @@ -114,7 +114,6 @@ fn link(ctx: command::Context) -> crate::Result<()> { // Step 2. Setup the global directory let node_modules: Dir = 'brk: { - bin::Linker::ensure_umask(); let explicit_global_dir: &[u8] = match &ctx.install { Some(install_) => install_.global_dir.as_deref().unwrap_or(b""), None => b"", @@ -180,11 +179,7 @@ fn link(ctx: command::Context) -> crate::Result<()> { let global_path = pm::global_link_dir_path(manager); let dest_path = resolve_path::join_abs_string_z::(global_path, &[name]); - match bun_sys::sys_uv::symlink_uv( - link_path, - dest_path, - bun_sys::windows::libuv::UV_FS_SYMLINK_JUNCTION, - ) { + match bun_sys::junction(link_path, dest_path) { Err(e) => { bun_core::pretty_errorln!( "error: failed to create junction to node_modules in global dir due to error {}", @@ -201,8 +196,6 @@ fn link(ctx: command::Context) -> crate::Result<()> { if let Err(e) = node_modules.sym_link( FileSystem::instance().top_level_dir_without_trailing_slash(), name, - // is_directory - true, ) { if manager.options.log_level != LogLevel::Silent { bun_core::pretty_errorln!( diff --git a/src/runtime/cli/multi_run.rs b/src/runtime/cli/multi_run.rs index 83faeab5d470..1cc04db3a12b 100644 --- a/src/runtime/cli/multi_run.rs +++ b/src/runtime/cli/multi_run.rs @@ -12,6 +12,7 @@ use bun_event_loop::MiniEventLoop::MiniEventLoop; use bun_install::package_manager::workspace_selection; use bun_io::BufferedReader; use bun_paths as path; +use bun_sys::FdExt as _; use crate::Command; use crate::filter_arg as FilterArg; @@ -19,11 +20,8 @@ use crate::run_command::{ConfigureEnvOptions, RunCommand}; // `bun.spawn` (Process/Status/SpawnOptions/Rusage/spawnProcess) — // lives under crate::api::bun::process. -#[cfg(unix)] use crate::api::bun::process::SpawnResultExt as _; -use crate::api::bun::process::{ - self as spawn, Rusage, SpawnOptions, SpawnProcessResult, Status, event_loop_handle_to_ctx, -}; +use crate::api::bun::process::{self as spawn, Rusage, SpawnOptions, SpawnResult, Status}; use bun_collections::index_sort; use bun_dotenv::Loader as DotEnvLoader; type OutputWriter = bun_core::io::Writer; @@ -98,7 +96,7 @@ bun_io::impl_buffered_reader_parent! { let state = &mut *(*handle).state.cast_mut(); let _ = state.maybe_finish(&mut *handle); }; - loop_ = |this| bun_io::uws_to_native((*(*this).event_loop_ptr()).loop_); + loop_ = |this| (*(*this).event_loop_ptr()).loop_ptr(); event_loop = |this| (*(*(*this).handle).state).event_loop_handle.as_event_loop_ctx(); } @@ -156,7 +154,7 @@ impl<'a> ProcessHandle<'a> { let start_time = Instant::now(); let envp; let env_ptr = state.env; - let spawned: SpawnProcessResult = { + let spawned: SpawnResult = { // SAFETY: state.env points at the process-lifetime DotEnv loader. let env = unsafe { &mut *env_ptr }; let original_path: Box<[u8]> = env.map.get(b"PATH").map(Box::from).unwrap_or_default(); @@ -178,17 +176,8 @@ impl<'a> ProcessHandle<'a> { }? .map_err(Error::from)? }; - // `mut` needed on Windows where `WindowsSpawnResult::to_process` takes `&mut self` - // and the stdout/stderr pipes are `.take()`n below; on POSIX `to_process` consumes - // `self` by value. - #[cfg(windows)] - let mut spawned = spawned; - // POSIX-only: pipe FDs are read before `to_process` consumes `spawned`. - // On Windows the readers are wired via `Source::Pipe` taken from - // `spawned.stdout/stderr` below, and `WindowsStdioResult` is not `Copy`. - #[cfg(unix)] + // Pipe FDs are read before `to_process` consumes `spawned`. let stdout_fd = spawned.stdout; - #[cfg(unix)] let stderr_fd = spawned.stderr; let process = spawned.to_process_handle(EventLoopHandle::init_mini(state.event_loop)); @@ -201,55 +190,16 @@ impl<'a> ProcessHandle<'a> { let stderr_parent = (&raw mut self.stderr_reader).cast::(); self.stderr_reader.reader.set_parent(stderr_parent); - #[cfg(windows)] - { - // `spawn_process_windows` has *already* reclaimed - // sole ownership of that heap pipe into - // `WindowsStdioResult::Buffer(Box)` (see - // src/spawn/process.rs WindowsStdio::Buffer doc). Reconstructing a - // second Box from `self.options.stdout` here would alias the same - // allocation and double-free when `spawned` drops. Instead, move - // the Box out of the spawn *result* — `WindowsStdioResult::take()` - // leaves `Unavailable` behind so `spawned`'s drop is a no-op. - if let spawn::WindowsStdioResult::Buffer(pipe) = spawned.stdout.take() { - self.stdout_reader - .reader - .set_source(bun_io::Source::Pipe(pipe)); - } - if let spawn::WindowsStdioResult::Buffer(pipe) = spawned.stderr.take() { - self.stderr_reader - .reader - .set_source(bun_io::Source::Pipe(pipe)); - } - } - - #[cfg(unix)] - { - if let Some(stdout_fd) = stdout_fd { - let _ = bun_sys::set_nonblocking(stdout_fd); - self.stdout_reader - .reader - .start(stdout_fd, true) - .map_err(Error::from)?; + for (fd, reader) in [ + (stdout_fd, &mut self.stdout_reader.reader), + (stderr_fd, &mut self.stderr_reader.reader), + ] { + let Some(fd) = fd else { continue }; + if let Err(err) = reader.start(fd, true) { + // A reader that fails to start (Windows only) has not taken the fd. + fd.close(); + return Err(Error::from(err)); } - if let Some(stderr_fd) = stderr_fd { - let _ = bun_sys::set_nonblocking(stderr_fd); - self.stderr_reader - .reader - .start(stderr_fd, true) - .map_err(Error::from)?; - } - } - #[cfg(not(unix))] - { - self.stdout_reader - .reader - .start_with_current_pipe() - .map_err(Error::from)?; - self.stderr_reader - .reader - .start_with_current_pipe() - .map_err(Error::from)?; } self.process = Some(ProcessSlot { @@ -285,8 +235,7 @@ impl<'a> ProcessHandle<'a> { /// On process exit, read what the pipes already hold, then force-end any /// pipe a leftover child still keeps open: its EOF may never come and - /// must not stall the finish. Windows has no synchronous drain, so only - /// the force-end applies there. + /// must not stall the finish. /// /// # Safety /// `this` is the live handle; the reader callbacks re-enter @@ -304,6 +253,10 @@ impl<'a> ProcessHandle<'a> { // EOF here dispatches `on_reader_done`, setting `ended`. BufferedReader::read(&raw mut (*pipe).reader); } + #[cfg(windows)] + if !(*pipe).ended { + BufferedReader::drain(&raw mut (*pipe).reader); + } if !(*pipe).ended { (*pipe).ended = true; // `deinit` fires no callback; `ended` is the accounting. @@ -588,7 +541,7 @@ impl<'a> State<'a> { } } - fn finalize(&self) -> u8 { + fn finalize(&self) -> u32 { for handle in self.handles.iter() { if let Some(proc) = &handle.process { match &proc.status { @@ -598,7 +551,7 @@ impl<'a> State<'a> { } } Status::Signaled(signal) => { - return bun_sys::SignalCode(*signal).to_exit_code(); + return bun_sys::SignalCode(*signal).to_exit_code().into(); } _ => return 1, } @@ -898,14 +851,15 @@ pub(crate) fn run(ctx: &mut Command::ContextData) -> Result = if cfg!(unix) { // SAFETY: env_ptr is the process-lifetime DotEnv loader; the &mut borrow passed to @@ -1167,24 +1121,9 @@ pub(crate) fn run(ctx: &mut Command::ContextData) -> Result(), - ))), - #[cfg(unix)] stderr: spawn::Stdio::Buffer, - #[cfg(not(unix))] - stderr: spawn::Stdio::Buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - ))), cwd: config.cwd.clone(), - #[cfg(windows)] - windows: spawn::WindowsOptions { - loop_: EventLoopHandle::init_mini(event_loop), - ..Default::default() - }, stream: true, ..Default::default() }, @@ -1257,7 +1196,7 @@ pub(crate) fn run(ctx: &mut Command::ContextData) -> Result bool { diff --git a/src/runtime/cli/open.rs b/src/runtime/cli/open.rs index a3792e292ba1..09d321450988 100644 --- a/src/runtime/cli/open.rs +++ b/src/runtime/cli/open.rs @@ -401,14 +401,6 @@ fn auto_close(spawned: *mut SpawnedEditorContext) { argv[j] = unsafe { bun_core::ffi::slice(p, l) }; } - // FIXME(windows-leak): the sync::spawn path - // requires a `WindowsOptions.loop_`; `MiniEventLoop::init_global` heap-allocates a - // MiniEventLoop + uv_loop_t into a thread-local that is NEVER torn down. Because this - // runs on a fresh detached std::thread per `Editor::open()` call, every editor-open on - // Windows leaks one MiniEventLoop + uv_loop_t (+ DotEnv Loader/Map if env was null). - // Proper fix needs either (a) a MiniEventLoop teardown helper (none exists today), or - // (b) plumbing the caller's existing EventLoopHandle through SpawnedEditorContext - // (signature change to Editor::open + callers). Both are out-of-scope for this file. let owned_argv: Vec> = argv[0..spawned.argc] .iter() .map(|s| s.to_vec().into_boxed_slice()) @@ -419,13 +411,6 @@ fn auto_close(spawned: *mut SpawnedEditorContext) { stderr: sync::SyncStdio::Inherit, stdout: sync::SyncStdio::Inherit, stdin: sync::SyncStdio::Inherit, - #[cfg(windows)] - windows: crate::api::bun::process::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini(bun_event_loop::MiniEventLoop::init_global( - None, None, - )), - ..Default::default() - }, ..Default::default() }); } diff --git a/src/runtime/cli/pack_command.rs b/src/runtime/cli/pack_command.rs index b320b76c3ee7..2f30f6604826 100644 --- a/src/runtime/cli/pack_command.rs +++ b/src/runtime/cli/pack_command.rs @@ -31,9 +31,7 @@ use bun_core::{ZStr, strings}; use bun_paths::resolve_path; use bun_semver as Semver; use bun_sha_hmac::sha; -use bun_sys::{ - self, CloseOnDrop, Dir, Fd, FdDirExt as _, FdExt as _, File, dir_iterator as DirIterator, -}; +use bun_sys::{self, CloseOnDrop, Dir, Fd, FdDirExt as _, File, dir_iterator as DirIterator}; // ─────────────────────────────────────────────────────────────────────────── // local shims for upstream-stub gaps @@ -2620,7 +2618,7 @@ pub(crate) fn pack( } while let Some(item) = pack_queue.remove_or_null() { - let file = match bun_sys::openat( + let fd = match bun_sys::openat( Fd::from_std_dir(&root_dir), &item.path, bun_sys::O::RDONLY, @@ -2646,23 +2644,9 @@ pub(crate) fn pack( } }; - let fd: Fd = match file - .make_lib_uv_owned_for_syscall(bun_sys::Tag::open, bun_sys::ErrorCase::CloseOnFail) - { - Ok(fd) => fd, - Err(err) => { - Output::err( - err, - "failed to open file: \"{}\"", - format_args!("{}", bstr::BStr::new(item.path.as_bytes())), - ); - Global::crash(); - } - }; - let _close_fd = CloseOnDrop::new(fd); - let stat = match bun_sys::sys_uv::fstat(fd) { + let stat = match bun_sys::fstat(fd) { Ok(s) => s, Err(err) => { Output::err( diff --git a/src/runtime/cli/pm_version_command.rs b/src/runtime/cli/pm_version_command.rs index caa1e9e1ece2..477ed2aa9fe3 100644 --- a/src/runtime/cli/pm_version_command.rs +++ b/src/runtime/cli/pm_version_command.rs @@ -701,8 +701,6 @@ impl PmVersionCommand { stdin: Stdio::Ignore, cwd: Box::<[u8]>::from(cwd), envp: None, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }) { Ok(p) => p, @@ -740,8 +738,6 @@ impl PmVersionCommand { stdin: Stdio::Ignore, cwd: Box::<[u8]>::from(cwd), envp: None, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }) { Ok(p) => p, @@ -805,8 +801,6 @@ impl PmVersionCommand { stderr: Stdio::Buffer, stdin: Stdio::Ignore, envp: None, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }) { Ok(p) => p, @@ -823,8 +817,8 @@ impl PmVersionCommand { } Ok(result) => { if !result.is_ok() { - let exit_code: i32 = match &result.status { - ProcStatus::Exited(e) => i32::from(e.code), + let exit_code: i64 = match &result.status { + ProcStatus::Exited(e) => i64::from(e.code), _ => -1, }; Output::err_generic("Git add failed with exit code {}", (exit_code,)); @@ -847,8 +841,6 @@ impl PmVersionCommand { stderr: Stdio::Buffer, stdin: Stdio::Ignore, envp: None, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }) { Ok(p) => p, @@ -888,8 +880,6 @@ impl PmVersionCommand { stderr: Stdio::Buffer, stdin: Stdio::Ignore, envp: None, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }) { Ok(p) => p, @@ -929,14 +919,3 @@ fn fmt_bytes(args: core::fmt::Arguments<'_>) -> Vec { fn build_argv(parts: &[&[u8]]) -> Vec> { parts.iter().map(|p| Box::<[u8]>::from(*p)).collect() } - -#[cfg(windows)] -#[inline] -fn spawn_windows_options() -> crate::api::bun::process::WindowsOptions { - crate::api::bun::process::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini(bun_event_loop::MiniEventLoop::init_global( - None, None, - )), - ..Default::default() - } -} diff --git a/src/runtime/cli/repl.rs b/src/runtime/cli/repl.rs index 99f8a2f90f1e..f15fbb1dad71 100644 --- a/src/runtime/cli/repl.rs +++ b/src/runtime/cli/repl.rs @@ -1070,10 +1070,7 @@ impl<'a> Repl<'a> { #[cfg(windows)] { if let Some(mode) = self.original_windows_mode { - // SAFETY: stdin handle is valid console handle - unsafe { - let _ = bun_sys::windows::SetConsoleMode(Fd::stdin().native(), mode); - } + let _ = bun_sys::windows::SetConsoleMode(Fd::stdin().native(), mode); self.original_windows_mode = None; } } diff --git a/src/runtime/cli/run_command.rs b/src/runtime/cli/run_command.rs index 7cca76d9e9c7..c89a675bc140 100644 --- a/src/runtime/cli/run_command.rs +++ b/src/runtime/cli/run_command.rs @@ -24,7 +24,7 @@ use bun_paths::WPathBuffer; use bun_paths::strings; use bun_paths::{self as paths, DELIMITER, MAX_PATH_BYTES, PathBuffer, SEP}; use bun_resolver::package_json::PackageJSON; -use bun_sys::{self as sys, Fd, FdExt as _}; +use bun_sys::{self as sys, Fd}; use bun_which::which; use crate::cli; @@ -404,17 +404,6 @@ Full documentation is available at https://bun.com/docs/cli/run stdout: sync::SyncStdio::Inherit, stdin: sync::SyncStdio::Inherit, ipc: ipc_fd, - #[cfg(windows)] - windows: crate::api::bun_process::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini( - bun_event_loop::MiniEventLoop::init_global( - // SAFETY: `env` outlives the mini event loop. - Some(unsafe { &mut *::core::ptr::from_mut::(env) }), - None, - ), - ), - ..Default::default() - }, ..Default::default() }) { Err(err) => { @@ -468,8 +457,8 @@ Full documentation is available at https://bun.com/docs/cli/run // cmd.exe exits 0 after abandoning a line whose command was Ctrl+C'd. #[cfg(windows)] - if exit_code.raw == bun_sys::windows::STATUS_CONTROL_C_EXIT - || (bun_spawn::ctrl_c::take_received() && exit_code.raw == 0) + if exit_code.is_ctrl_c_exit() + || (bun_spawn::ctrl_c::take_received() && exit_code.code == 0) { bun_spawn::ctrl_c::exit_like_child(); } @@ -484,7 +473,7 @@ Full documentation is available at https://bun.com/docs/cli/run Output::flush(); } - Global::exit(exit_code.code as u32); + Global::exit(exit_code.code); } } @@ -1574,7 +1563,7 @@ impl Run<'_> { } // These create undefined references to externally-defined C symbols - // (uv_* posix stubs, v8:: shims) so the linker pulls those archive + // (uv_* stubs, v8:: shims) so the linker pulls those archive // members from libbun.a in CI's split link-only mode and keeps them // through `--gc-sections`. Without them, dlopen'd NAPI modules see // `undefined symbol: uv_*` instead of the friendly crash message. @@ -2098,17 +2087,6 @@ impl RunCommand { stdout: sync::SyncStdio::Inherit, stdin: sync::SyncStdio::Inherit, use_execve_on_macos: silent, - #[cfg(windows)] - windows: crate::api::bun_process::WindowsOptions { - loop_: bun_jsc::EventLoopHandle::init_mini( - bun_event_loop::MiniEventLoop::init_global( - // SAFETY: env loader is process-lifetime. - Some(unsafe { &mut *::core::ptr::from_mut::(env) }), - None, - ), - ), - ..Default::default() - }, ..Default::default() }) { Ok(r) => r, @@ -2213,8 +2191,7 @@ impl RunCommand { )); } - #[cfg(windows)] - if exit_code.raw == bun_sys::windows::STATUS_CONTROL_C_EXIT { + if exit_code.is_ctrl_c_exit() { bun_spawn::ctrl_c::exit_like_child(); } @@ -2590,17 +2567,23 @@ impl RunCommand { ) } as usize; 'try_bunx_file: { - if cwd_len == 0 { + let prefix = bun_core::w!("\\node_modules\\.bin\\"); + let ext = bun_core::w!(".bunx"); + // 0 is failure, and a directory that did not fit is reported as + // the length it needs. `target_name` is at most one UTF-16 unit + // per byte; the last unit is for the terminator. + if cwd_len == 0 + || root.len() + cwd_len + prefix.len() + target_name.len() + ext.len() + >= buf.len() + { break 'try_bunx_file; } let mut ptr = root.len() + cwd_len; - let prefix = bun_core::w!("\\node_modules\\.bin\\"); buf[ptr..ptr + prefix.len()].copy_from_slice(prefix); ptr += prefix.len(); let encoded = strings::convert_utf8_to_utf16_in_buffer(&mut buf[ptr..], target_name); ptr += encoded.len(); - let ext = bun_core::w!(".bunx"); buf[ptr..ptr + ext.len()].copy_from_slice(ext); ptr += ext.len(); buf[ptr] = 0; @@ -2769,12 +2752,6 @@ impl RunCommand { let Ok(fd) = bun_sys::open(open_z, bun_sys::O::RDONLY, 0) else { return false; }; - // `.makeLibUVOwnedForSyscall(.open, .close_on_fail)` — hands the - // HANDLE off to libuv ownership on Windows; pass-through on POSIX. - let Ok(fd) = fd.make_lib_uv_owned_for_syscall(sys::Tag::open, sys::ErrorCase::CloseOnFail) - else { - return false; - }; // fstat: directories cannot be run. if only there was a faster way to // check this @@ -2946,8 +2923,8 @@ impl RunCommand { if ctx.positionals.is_empty() { // Node: bare `node` on a TTY starts the REPL. Only in emulation // mode; bun's own `bun` with no args stays the help text. Use - // Output's cached stdio flag (set at startup via libuv's handle - // probe), which is the same check `bun update --interactive` uses. + // Output's cached stdio flag (set at startup), which is the same + // check `bun update --interactive` uses. if Output::is_stdin_tty() { return Self::exec_node_repl(ctx); } diff --git a/src/runtime/cli/test/ChangedFilesFilter.rs b/src/runtime/cli/test/ChangedFilesFilter.rs index 8dadbbe91b48..322d7efc017e 100644 --- a/src/runtime/cli/test/ChangedFilesFilter.rs +++ b/src/runtime/cli/test/ChangedFilesFilter.rs @@ -25,8 +25,6 @@ use bun_core::{self, Global, Output, env_var, fmt as bun_fmt}; use bun_core::{ZBox, ZStr, getenv_z}; #[cfg(not(windows))] use bun_jsc as jsc; -#[cfg(windows)] -use bun_jsc::EventLoopHandle; use bun_jsc::virtual_machine::VirtualMachine; #[cfg(not(windows))] use bun_paths::SEP; @@ -585,15 +583,6 @@ fn run_git(git_path: &[u8], cwd: &[u8], args: &[&[u8]]) -> GitResult { stderr: spawn_sync::SyncStdio::Buffer, stdin: spawn_sync::SyncStdio::Ignore, envp: None, - // The test command has a JSC VM running; reuse its event loop on - // Windows rather than spinning up a MiniEventLoop. - #[cfg(windows)] - windows: spawn_sync::WindowsOptions { - // `init` takes the erased `*mut ()` event-loop pointer - // directly, so unwrap it here. - loop_: EventLoopHandle::init(VirtualMachine::get().event_loop().cast()), - ..Default::default() - }, ..Default::default() }) { Ok(p) => p, diff --git a/src/runtime/cli/test/parallel/Channel.rs b/src/runtime/cli/test/parallel/Channel.rs index a64d8abf3895..705136b3c8fd 100644 --- a/src/runtime/cli/test/parallel/Channel.rs +++ b/src/runtime/cli/test/parallel/Channel.rs @@ -5,11 +5,12 @@ //! `on_channel_frame(kind, &mut Frame::Reader)` and `on_channel_done()`. //! //! POSIX backend: `uws::NewSocketHandler` adopted from a socketpair fd. -//! Windows backend: `uv::Pipe` over the inherited duplex named-pipe end (same -//! mechanism as `Bun.spawn({ipc})` / `process.send()`). +//! Windows backend: a `bun_io::windows::Pipe` over an end of the duplex pipe +//! spawn makes for fd 3. Both ends are this binary, so the bytes on the pipe +//! are the frames themselves. //! //! Lifetime: a `Channel` is embedded as a field in an owner that outlives all -//! uv/usockets callbacks (the coordinator's `Worker[]`, or the worker's +//! pipe/usockets callbacks (the coordinator's `Worker[]`, or the worker's //! `WorkerLoop` which lives for the process). The owner is recovered via //! `container_of` (field offset) so the channel default-inits without a //! self-pointer. `Drop` assumes no write is in flight — true for both call @@ -27,21 +28,15 @@ use core::ffi::c_void; use core::marker::PhantomData; use bun_collections::VecExt; +#[cfg(windows)] +use bun_io::windows::{Pipe, PipeOrigin, ReadEvent, Refusal}; use bun_jsc::JsCell; use bun_jsc::virtual_machine::VirtualMachine; use bun_sys::Fd; -#[cfg(not(windows))] use bun_sys::FdExt as _; #[cfg(not(windows))] use bun_uws as uws; -#[cfg(windows)] -use bun_libuv_sys::UvStream as _; -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; - use super::frame; /// The owner implements [`bun_core::IntrusiveField>`] @@ -63,8 +58,7 @@ pub(crate) struct Channel { pub out: JsCell>, pub(crate) done: Cell, /// The peer's byte stream stopped decoding as frames, i.e. something in - /// the peer wrote straight to fd 3: a length `ingest` rejected, or on - /// Windows a read error from libuv's own IPC framing underneath ours. Set + /// the peer wrote straight to fd 3: a length `ingest` rejected. Set /// together with `done`, transport still attached. The coordinator kills /// such a worker and reports this rather than the exit status it caused. pub(crate) corrupt_frame: Cell, @@ -155,102 +149,69 @@ impl Channel { } } -// -- Windows (uv.Pipe) ------------------------------------------------------- +// -- Windows (pipe) ---------------------------------------------------------- #[cfg(windows)] +#[derive(Default)] pub(crate) struct WindowsBackend { - pub(crate) pipe: Cell<*mut uv::Pipe>, - /// Read scratch — libuv asks us to allocate before each read. - /// Wrapped so every byte is interior-mutable: libuv forms - /// `&mut Channel` from the stored root on each read; a plain array here - /// would be the one field where that retag pops the shared views held - /// during frame decoding. - pub(crate) read_chunk: JsCell<[u8; 16 * 1024]>, - /// Payload owned by the in-flight uv_write; must stay stable until the - /// callback. New writes go to `out` until this completes, then the buffers - /// swap. - pub(crate) inflight: JsCell>, - pub(crate) write_req: JsCell, - pub(crate) write_buf: JsCell, -} - -#[cfg(windows)] -impl Default for WindowsBackend { - fn default() -> Self { - Self { - pipe: Cell::new(core::ptr::null_mut()), - read_chunk: JsCell::new([0u8; 16 * 1024]), - inflight: JsCell::new(Vec::new()), - write_req: JsCell::new(bun_core::ffi::zeroed::()), - write_buf: JsCell::new(uv::uv_buf_t::init(b"")), - } - } + pub(crate) pipe: JsCell>, + /// A write is with the pipe; `out` collects what follows it. + writing: Cell, } // -- adopt ------------------------------------------------------------------- impl Channel { /// Adopt a duplex fd into the channel and start reading. POSIX: the - /// socketpair end. Windows: the inherited named-pipe end (worker side). - // callers (`runner.rs`, `Worker.rs`) only hold `&VirtualMachine`; - // the upstream `rare_data()` / `test_parallel_ipc_group()` accessors require - // `&mut`. Take a raw `*const` and cast - // away const locally — single-threaded init path. A `&VirtualMachine` - // parameter would trip `invalid_reference_casting` on the `&T → &mut T` - // promotion; the raw-pointer route sidesteps that lint while keeping both - // call sites (which pass `&`/`&mut` and coerce) unchanged. + /// socketpair end. Windows: an end of the pipe spawn made for fd 3; + /// `inherited` says it is the worker's: the coordinator made it for this + /// process alone. Takes `fd` either way. /// `this` is the channel's address derived from the owner's `&mut`. - pub(crate) fn adopt(this: *mut Self, vm: *const VirtualMachine, fd: Fd) -> bool { + pub(crate) fn adopt(this: *mut Self, fd: Fd, inherited: bool) -> bool { // SAFETY: caller passes `&raw mut owner.channel` (live for the call). let self_ = unsafe { &*this }; self_.root.set(this); - Self::adopt_impl(self_, this, vm, fd) + Self::adopt_impl(self_, this, fd, inherited) } - fn adopt_impl(&self, this: *mut Self, _vm: *const VirtualMachine, fd: Fd) -> bool { + fn adopt_impl(&self, this: *mut Self, fd: Fd, inherited: bool) -> bool { #[cfg(windows)] { - let _ = this; // registered via `adopt_pipe_impl` from `self.root` - // With ipc=true - // libuv wraps reads/writes in its own framing; both ends use it so - // the wrapping is transparent and our payload bytes pass through - // unchanged. With ipc=false the parent end (created by uv_spawn for - // the .ipc stdio container, which always inits with ipc=true) and - // child end disagree on framing and the channel never delivers a - // frame. - let mut pipe = Box::new(bun_core::ffi::zeroed::()); - if let Some(e) = pipe - .init(uv::Loop::get(), true) - .to_error(bun_sys::Tag::pipe) - { - bun_core::debug_warn!( - "Channel.adopt: uv_pipe_init failed: {}", - e.name().escape_ascii(), - ); - drop(pipe); - return false; - } - if let Some(e) = pipe.open(fd.uv()).to_error(bun_sys::Tag::open) { - bun_core::debug_warn!( - "Channel.adopt: uv_pipe_open({}) failed: {}", - fd.uv(), - e.name().escape_ascii(), - ); - // SAFETY: Box-allocated; close_and_destroy reclaims via heap::take. - unsafe { uv::Pipe::close_and_destroy(bun_core::heap::into_raw(pipe)) }; - return false; - } - let pipe = bun_core::heap::into_raw(pipe); - if !self.adopt_pipe_impl(pipe) { - // Caller still owns `pipe` on adopt_pipe failure. - // SAFETY: Box-allocated; close_and_destroy reclaims via heap::take. - unsafe { uv::Pipe::close_and_destroy(pipe) }; - return false; - } - return true; + let loop_ = VirtualMachine::get().as_mut().uws_loop(); + let origin = if inherited { + PipeOrigin::InheritedUnshared + } else { + PipeOrigin::Created + }; + let opened = Pipe::open(loop_, fd, origin, true); + // The pipe stays ref'd so the loop blocks for the peer's first frame. + let started = + opened.and_then(|mut pipe| match pipe.read_start(this, Self::on_pipe_read) { + Ok(()) => Ok(pipe), + Err(e) => { + pipe.disown(); + Err(e) + } + }); + return match started { + Ok(pipe) => { + self.backend.pipe.set(Some(pipe)); + true + } + Err(e) => { + bun_core::debug_warn!( + "Channel.adopt: opening the pipe failed: {}", + e.name().escape_ascii(), + ); + // Leaving the endpoint open keeps the peer process alive. + fd.close(); + false + } + }; } #[cfg(not(windows))] { + let _ = inherited; // VM is process-singleton and accessed only from the main // thread here; route through the safe singleton accessor. let vm: &mut VirtualMachine = VirtualMachine::get().as_mut(); @@ -267,43 +228,6 @@ impl Channel { } } - /// Windows-only: adopt a `uv::Pipe` already initialized by spawn (the - /// `.ipc` extra-fd parent end, or the worker's just-opened pipe). Starts - /// reading. On failure the caller still owns `pipe`. The pipe stays ref'd - /// so the loop blocks for the peer's first frame; `Drop` closes it. - #[cfg(windows)] - pub(crate) fn adopt_pipe( - this: *mut Self, - _vm: *const VirtualMachine, - pipe: *mut uv::Pipe, - ) -> bool { - // SAFETY: caller passes `&raw mut owner.channel` (live for the call). - let self_ = unsafe { &*this }; - self_.root.set(this); - self_.adopt_pipe_impl(pipe) - } - - #[cfg(windows)] - fn adopt_pipe_impl(&self, pipe: *mut uv::Pipe) -> bool { - // The read callbacks are expressed via the `StreamReader` trait impl - // below and routed through `read_start_ctx`, which stashes `self` in - // `handle.data`. - // SAFETY: `pipe` is a live, init'ed `Box` allocation owned by the - // caller; we only borrow it to start reading. - let rc = unsafe { (*pipe).read_start_ctx::(self.root.get()) }; - if let Some(e) = rc.to_error(bun_sys::Tag::listen) { - bun_core::debug_warn!( - "Channel.adoptPipe: readStart failed: {}", - e.name().escape_ascii(), - ); - // Caller still owns `pipe` on failure and is responsible - // for `close_and_destroy`. - return false; - } - self.backend.pipe.set(pipe); - true - } - // -- write --------------------------------------------------------------- /// Queue and write a complete encoded frame. If the kernel accepts only @@ -336,80 +260,70 @@ impl Channel { } } + /// A pipe takes a write whole and reports when it is through, so frames + /// queue in `out` while one is in flight and go out together after it. #[cfg(windows)] fn send_windows(&self, frame_bytes: &[u8]) { - // A uv_write is in flight — queue behind it. - if !self.backend.inflight.get().is_empty() { - self.out.with_mut(|out| out.extend_from_slice(frame_bytes)); - return; - } - let pipe = self.backend.pipe.get(); - if pipe.is_null() { + self.out.with_mut(|out| out.extend_from_slice(frame_bytes)); + self.submit_windows_write(); + } + + #[cfg(windows)] + fn submit_windows_write(&self) { + if self.backend.writing.get() || self.out.get().is_empty() || self.done.get() { return; } - // Try a synchronous write first. uv_try_write on a Windows - // UV_NAMED_PIPE always returns EAGAIN (vendor/libuv/src/win/stream.c), - // so this currently always falls through to submit_windows_write — - // kept because EBADF/EPIPE here mean the pipe is dead and must not - // silently drop the frame. - let buf = uv::uv_buf_t::init(frame_bytes); - // SAFETY: `pipe` is the live Box-allocated uv_pipe_t owned by this channel. - let rc = unsafe { (*pipe).try_write(core::slice::from_ref(&buf)) }; - let w: usize = match rc.to_error(bun_sys::Tag::try_write) { - None => rc.int() as usize, - Some(e) => { - if e.get_errno() == bun_sys::E::AGAIN { - 0 - } else { - self.mark_done(); - return; - } - } - }; - if w >= frame_bytes.len() { - return; + let bytes = self.out.replace(Vec::new()); + let submitted = self.backend.pipe.with_mut(|pipe| match pipe { + Some(pipe) => pipe + .write_owned( + bytes, + Refusal::Returned, + self.root.get(), + Self::on_pipe_write, + ) + .is_ok(), + None => false, + }); + if submitted { + self.backend.writing.set(true); + } else { + self.mark_done(); } - self.out - .with_mut(|out| out.extend_from_slice(&frame_bytes[w..])); - self.submit_windows_write(); } + /// # Safety + /// `this` is the channel's root pointer; a pipe only reports while the + /// channel has it open. #[cfg(windows)] - fn submit_windows_write(&self) { - if self.out.get().is_empty() || !self.backend.inflight.get().is_empty() || self.done.get() { + unsafe fn on_pipe_write(this: *mut Self, result: bun_sys::Result) { + // SAFETY: caller contract. + let self_ = unsafe { &*this }; + self_.backend.writing.set(false); + if self_.done.get() { return; } - let pipe = self.backend.pipe.get(); - if pipe.is_null() { + if result.is_err() { + self_.mark_done(); return; } - // Swap: out → inflight (stable for uv_write), out becomes empty. - let out = self.out.replace(Vec::new()); - let prev_inflight = self.backend.inflight.replace(out); - self.out.set(prev_inflight); - self.backend - .write_buf - .set(uv::uv_buf_t::init(self.backend.inflight.get().as_slice())); - let this: *mut Self = self.root.get(); - // SAFETY: `p` is the `this` handed to `write` below — the live Channel - // stashed for the callback; every Channel method is `&self`. - let on_write: fn(*mut Self, uv::ReturnCode) = - |p, s| unsafe { WindowsHandlers::::on_write(&*p, s) }; - // SAFETY: `pipe` is the live Box-allocated uv_pipe_t owned by this - // channel; `write_req`/`write_buf`/`inflight` live in `self`, which is - // address-stable and outlives the write, so libuv may hold them until - // `on_write` fires. - let rc = unsafe { - (*self.backend.write_req.as_ptr()).write( - (*pipe).as_stream(), - &*self.backend.write_buf.as_ptr(), - this, - on_write, - ) - }; - if rc.is_err() { - self.backend.inflight.with_mut(|inflight| inflight.clear()); - self.mark_done(); + self_.submit_windows_write(); + } + + /// # Safety + /// As [`on_pipe_write`](Self::on_pipe_write). + #[cfg(windows)] + unsafe fn on_pipe_read(this: *mut Self, event: ReadEvent<'_>) { + // SAFETY: caller contract. + let self_ = unsafe { &*this }; + match event { + ReadEvent::Data(bytes) => self_.ingest(bytes.as_slice()), + // The peer closed its end (or the pipe broke): detach first so it + // reads as a close, like the POSIX on_close path. + ReadEvent::Eof | ReadEvent::EndOfWrite | ReadEvent::Err(_) => { + self_.backend.pipe.set(None); + self_.mark_done(); + } } } @@ -419,7 +333,7 @@ impl Channel { pub(crate) fn is_attached(&self) -> bool { #[cfg(windows)] { - return !self.backend.pipe.get().is_null(); + return self.backend.pipe.get().is_some(); } #[cfg(not(windows))] { @@ -431,7 +345,7 @@ impl Channel { pub(crate) fn has_pending_writes(&self) -> bool { #[cfg(windows)] { - !self.out.get().is_empty() || !self.backend.inflight.get().is_empty() + !self.out.get().is_empty() || self.backend.writing.get() } #[cfg(not(windows))] { @@ -537,15 +451,7 @@ impl Channel { impl Drop for Channel { fn drop(&mut self) { self.done.set(true); - #[cfg(windows)] - { - let p = self.backend.pipe.replace(core::ptr::null_mut()); - if !p.is_null() { - // SAFETY: Box-allocated; close_and_destroy reclaims via heap::take. - unsafe { uv::Pipe::close_and_destroy(p) }; - } - // `inflight` Vec drops automatically. - } + // Windows: the pipe closes as the backend drops. #[cfg(not(windows))] { let sock = self.backend.socket.replace(Socket::DETACHED); @@ -639,68 +545,3 @@ impl PosixHandlers { s } } - -#[cfg(windows)] -struct WindowsHandlers(PhantomData); - -#[cfg(windows)] -impl WindowsHandlers { - fn on_alloc(self_: &mut Channel, suggested: usize) -> &mut [u8] { - let _ = suggested; - // SAFETY: hands libuv the cell payload; the buffer is only written by - // libuv until `on_read` consumes it, all on this thread. - let buf: &mut [u8; 16 * 1024] = unsafe { &mut *self_.backend.read_chunk.as_ptr() }; - &mut buf[..] - } - fn on_error(self_: &Channel, err: bun_sys::E) { - // libuv frames this pipe itself (ipc=1), so bytes the peer writes - // straight to fd 3 fail its frame-header check and surface here as a - // read error instead of reaching `ingest`; report it the way `ingest` - // reports a bad frame, before detaching. EOF is the peer closing its - // end: mirror the POSIX on_close path and detach first so it reads as - // a clean close. - if err != bun_sys::E::EOF { - self_.mark_corrupt(); - } - let p = self_.backend.pipe.replace(core::ptr::null_mut()); - if !p.is_null() { - // SAFETY: Box-allocated; close_and_destroy reclaims via heap::take. - unsafe { uv::Pipe::close_and_destroy(p) }; - } - self_.mark_done(); - } - fn on_write(self_: &Channel, status: uv::ReturnCode) { - self_.backend.inflight.with_mut(|inflight| inflight.clear()); - if self_.done.get() { - return; - } - if status.is_err() { - self_.mark_done(); - return; - } - self_.submit_windows_write(); - } -} - -/// Adapter from `UvStream::read_start_ctx` to `WindowsHandlers`; expressed as -/// a trait impl so the `extern "C"` trampoline stays zero-alloc. -#[cfg(windows)] -impl uv::StreamReader for Channel { - #[inline] - fn on_read_alloc(this: &mut Self, suggested_size: usize) -> &mut [u8] { - WindowsHandlers::::on_alloc(this, suggested_size) - } - #[inline] - fn on_read_error(this: &mut Self, err: core::ffi::c_int) { - let e = bun_sys::windows::translate_uv_error_to_e(err); - WindowsHandlers::::on_error(this, e); - } - #[inline] - unsafe fn on_read(this: *mut Self, data: &[u8]) { - // SAFETY: `this` is the live `Channel` stashed in `handle.data` by - // `read_start_ctx`; `data` points into its `read_chunk` and is only - // read (copied by `ingest`). - let this = unsafe { &*this }; - this.ingest(data); - } -} diff --git a/src/runtime/cli/test/parallel/Coordinator.rs b/src/runtime/cli/test/parallel/Coordinator.rs index 25e1a381ad91..5d2d5291cccc 100644 --- a/src/runtime/cli/test/parallel/Coordinator.rs +++ b/src/runtime/cli/test/parallel/Coordinator.rs @@ -222,8 +222,8 @@ impl<'a> Coordinator<'a> { terminate_process_group(p.pid); #[cfg(not(unix))] { - // SIGKILL → TerminateProcess; libuv-win ENOSYSes signals - // other than SIGQUIT/SIGTERM/SIGKILL/SIGINT. + // SIGKILL → TerminateProcess; Windows has no signals other + // than SIGQUIT/SIGTERM/SIGKILL/SIGINT to send (ENOSYS). let _ = p.kill(9); } } @@ -438,6 +438,7 @@ impl<'a> Coordinator<'a> { if let Some(file) = self.test_records.get_mut(idx as usize) { file.tests.push(Box::from(rd.p)); } + w.drain_output(); self.flush_captured(w); if formatted.is_empty() { return; // e.g. pass under --only-failures @@ -587,6 +588,7 @@ impl<'a> Coordinator<'a> { // Decrement here (not in onProcessExit) so drive() keeps pumping until // the IPC pipe has been drained and this reap actually runs. self.live_workers -= 1; + w.drain_output(); self.flush_captured(w); // Exited before the IPC handshake. `inflight` is None for these, so // the mid-file handling below never fires; the per-slot cap is what @@ -834,8 +836,8 @@ impl<'a> Coordinator<'a> { terminate_process_group(p.pid); #[cfg(not(unix))] { - // SIGKILL → TerminateProcess (libuv-win ENOSYSes most - // signals, so e.g. kill(1) would leave the sibling running + // SIGKILL → TerminateProcess (most signals are ENOSYS on + // Windows, so e.g. kill(1) would leave the sibling running // past the banner); it reaps as Signaled(9) → // "aborted: sibling worker panicked". let _ = p.kill(9); @@ -899,7 +901,7 @@ impl<'a> Coordinator<'a> { use bun_sys::windows; // SAFETY: Win32 FFI calls. unsafe { - let job = windows::CreateJobObjectA(core::ptr::null_mut(), core::ptr::null_mut()); + let job = windows::CreateJobObjectW(core::ptr::null_mut(), core::ptr::null_mut()); if job.is_null() { return None; } @@ -939,8 +941,7 @@ fn terminate_process_group(pid: libc::pid_t) { /// /// Windows delivers no signals: an unhandled exception or `__fastfail` /// exits with the NTSTATUS as the exit code, so recognized fatal values of -/// `Exited.raw` (the untruncated code; `Exited.code` is `u8`) classify as -/// panics too. A fault Bun's crash handler catches still exits with code +/// `Exited.code` classify as panics too. A fault Bun's crash handler catches still exits with code /// 3, indistinguishable from process.exit(3), and stays a per-file /// failure, recognizable only by its banner in stderr. fn is_panic_status(status: &SpawnStatus) -> bool { @@ -960,7 +961,7 @@ fn is_panic_status(status: &SpawnStatus) -> bool { } #[cfg(windows)] if let SpawnStatus::Exited(e) = status { - return is_fatal_windows_exit_code(e.raw); + return is_fatal_windows_exit_code(e.code); } false } @@ -1001,12 +1002,9 @@ fn is_fatal_windows_exit_code(code: u32) -> bool { fn describe_status<'b>(buf: &'b mut [u8; 32], status: &SpawnStatus) -> &'b [u8] { match status { SpawnStatus::Exited(e) => { - // Windows: report the untruncated code; NTSTATUS values print in - // hex ("exit code 0xC0000409"), the form Windows tooling uses. - #[cfg(windows)] - let code: u32 = e.raw; - #[cfg(not(windows))] - let code: u32 = u32::from(e.code); + // NTSTATUS values print in hex ("exit code 0xC0000409"), the form + // Windows tooling uses. + let code = e.code; let mut cursor: &mut [u8] = &mut buf[..]; if code >= 0x8000_0000 { write!(cursor, "exit code 0x{code:08X}").expect("unreachable"); diff --git a/src/runtime/cli/test/parallel/Worker.rs b/src/runtime/cli/test/parallel/Worker.rs index fd4dd124303a..11a6eddf6058 100644 --- a/src/runtime/cli/test/parallel/Worker.rs +++ b/src/runtime/cli/test/parallel/Worker.rs @@ -5,16 +5,10 @@ use core::ffi::c_void; -#[cfg(unix)] -use crate::api::bun::process::PosixStdio as Stdio; -#[cfg(unix)] use crate::api::bun::process::SpawnResultExt as _; -#[cfg(not(unix))] -use crate::api::bun::process::WindowsStdio as Stdio; -use crate::api::bun::process::{self as spawn, Process, Rusage, SpawnOptions, Status}; +use crate::api::bun::process::{self as spawn, Process, Rusage, SpawnOptions, Status, Stdio}; use bun_core::{self, Output}; -#[cfg(windows)] -use bun_jsc as jsc; +use bun_sys::{self, FdExt as _}; use super::channel::{Channel, ChannelOwner}; use super::coordinator::Coordinator; @@ -40,7 +34,7 @@ pub(crate) struct Worker { pub(crate) process: Option, /// Bidirectional IPC over fd 3. POSIX: usockets adopted from a socketpair. - /// Windows: `uv.Pipe` (the parent end of `.buffer` extra-fd, full-duplex). + /// Windows: `bun_io::windows::Pipe` (the parent end of `.buffer` extra-fd, full-duplex). /// Commands and results both flow through this channel; backpressure is /// handled by the loop, so a busy worker writing thousands of `test_done` /// frames never truncates and the coordinator never blocks. @@ -116,170 +110,71 @@ impl Worker { this.err = WorkerPipe::new(self_ptr); }); - #[cfg(unix)] - { - // `.buffer` extra_fd creates an AF_UNIX socketpair; the parent end is - // adopted into a usockets `Channel`. - let options = SpawnOptions { - stdin: Stdio::Ignore, - stdout: Stdio::Buffer, - stderr: Stdio::Buffer, - extra_fds: vec![Stdio::Buffer].into_boxed_slice(), - cwd: coord.cwd.to_vec().into_boxed_slice(), - stream: true, - // Own pgrp so abortAll can kill(-pid, SIGTERM) the worker and - // anything it spawned. PDEATHSIG is the SIGKILL safety net on - // Linux for the worker itself. - new_process_group: true, - #[cfg(any(target_os = "linux", target_os = "android"))] - linux_pdeathsig: Some(libc::SIGKILL as u8), - #[cfg(not(any(target_os = "linux", target_os = "android")))] - linux_pdeathsig: None, - ..Default::default() - }; - // SAFETY: `coord.argv`/`coord.envps[..]` are null-terminated - // C-string arrays with argv[0] non-null; valid for this call. - let mut spawned = unsafe { - spawn::spawn_process( - &options, - coord.argv.as_ptr(), - coord.envps[this.idx as usize].as_ptr(), - ) - }? - .map_err(|e| { - Output::err(e, "spawnProcess failed for test worker", ()); - crate::Error::SpawnFailed - })?; - let stdout = spawned.stdout; - let stderr = spawned.stderr; - let extra_pipes = core::mem::take(&mut spawned.extra_pipes); - this.process = Some( - spawned.to_process_handle(bun_event_loop::EventLoopHandle::init( - coord.vm.event_loop().cast(), - )), - ); - if let Some(fd) = stdout { - this.out - .reader - .start(fd, true) - .map_err(|_| crate::Error::PipeStartFailed)?; - } - if let Some(fd) = stderr { - this.err - .reader - .start(fd, true) - .map_err(|_| crate::Error::PipeStartFailed)?; - } - if !extra_pipes.is_empty() { - // coord.vm backref valid for worker lifetime; adopt() mutates the - // loop's socket context via interior mutability on the C side. - if !Channel::adopt(&raw mut this.ipc, coord.vm, extra_pipes[0].fd()) { - return Err(crate::Error::ChannelAdoptFailed); + // `.buffer` extra_fd creates an AF_UNIX socketpair (a duplex pipe + // on Windows); the parent end is adopted into the `Channel`. + let options = SpawnOptions { + stdin: Stdio::Ignore, + stdout: Stdio::Buffer, + stderr: Stdio::Buffer, + extra_fds: vec![Stdio::Buffer].into_boxed_slice(), + cwd: coord.cwd.to_vec().into_boxed_slice(), + stream: true, + // Own pgrp so abortAll can kill(-pid, SIGTERM) the worker and + // anything it spawned. PDEATHSIG is the SIGKILL safety net on + // Linux for the worker itself. + new_process_group: true, + #[cfg(any(target_os = "linux", target_os = "android"))] + linux_pdeathsig: Some(libc::SIGKILL as u8), + ..Default::default() + }; + // SAFETY: `coord.argv`/`coord.envps[..]` are null-terminated + // C-string arrays with argv[0] non-null; valid for this call. + let mut spawned = unsafe { + spawn::spawn_process( + &options, + coord.argv.as_ptr(), + coord.envps[this.idx as usize].as_ptr(), + ) + }? + .map_err(|e| { + Output::err(e, "spawnProcess failed for test worker", ()); + crate::Error::SpawnFailed + })?; + let stdout = spawned.stdout; + let stderr = spawned.stderr; + let extra_pipes = core::mem::take(&mut spawned.extra_pipes); + this.process = Some( + spawned.to_process_handle(bun_event_loop::EventLoopHandle::init( + coord.vm.event_loop().cast(), + )), + ); + // The parent ends nothing has taken yet, closed when a step below fails. + let mut unadopted = scopeguard::guard( + [stdout, stderr, extra_pipes.first().map(|pipe| pipe.fd())], + |fds| { + for fd in fds.into_iter().flatten() { + fd.close(); } - } else { - this.ipc.done.set(true); - } - } - #[cfg(not(unix))] + }, + ); { - // Windows: `.ipc` extra_fd creates a duplex `uv.Pipe` (named pipe - // under the hood, UV_READABLE | UV_WRITABLE | UV_OVERLAPPED) and - // initialises the parent end with uv_pipe_init(loop, ipc=1) — the - // same dance Bun.spawn({ipc}) / process.send() use. The child opens - // CRT fd 3 with uv_pipe_init(ipc=1) + uv_pipe_open in Channel.adopt. - // Both ends agreeing on the libuv IPC framing is what matters; our - // own [u32 len][u8 kind] frames ride inside it unchanged. - use bun_sys::windows::libuv as uv; - - let ipc_pipe = bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed::())); - // The guard owns the raw Box ptr; `close_and_destroy` handles both - // never-initialized (loop_ null → free directly) and initialized - // (uv_close + free in callback). Disarmed only after `adopt_pipe` - // succeeds and the Channel takes ownership. The guard captures only - // the raw ptr, so it nests cleanly under the outer `this` guard. - let ipc_pipe_guard = scopeguard::guard(ipc_pipe, |p| { - // SAFETY: `p` is the live Box-allocated uv_pipe_t; sole owner - // on every error path (extra_pipes is drained back to raw below). - unsafe { uv::Pipe::close_and_destroy(p) }; - }); - - let options = SpawnOptions { - stdin: Stdio::Ignore, - stdout: Stdio::Buffer(bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed::< - uv::Pipe, - >()))), - stderr: Stdio::Buffer(bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed::< - uv::Pipe, - >()))), - extra_fds: vec![Stdio::Ipc(ipc_pipe)].into_boxed_slice(), - cwd: coord.cwd.to_vec().into_boxed_slice(), - windows: spawn::WindowsOptions { - loop_: jsc::EventLoopHandle::init(coord.vm.event_loop().cast()), - ..Default::default() - }, - stream: true, - ..Default::default() - }; - // SAFETY: `coord.argv`/`coord.envps[..]` are null-terminated - // C-string arrays with argv[0] non-null; valid for this call. - let mut spawned = unsafe { - spawn::spawn_process( - &options, - coord.argv.as_ptr(), - coord.envps[this.idx as usize].as_ptr(), - ) - }? - .map_err(|e| { - Output::err(e, "spawnProcess failed for test worker", ()); - crate::Error::SpawnFailed - })?; - // `WindowsStdioResult::Buffer` holds `Box`, and - // `spawn_process_windows` does `heap::take(ipc_pipe)` into it — so - // `extra_pipes` holds a second `Box` to the SAME heap address that - // `ipc_pipe_guard` / `adopt_pipe` claim. Drain the Vec and release - // each Box back to a raw ptr so the Vec drop is inert and - // `ipc_pipe_guard` remains the sole owner across the - // `start_with_pipe` error window below. - for item in core::mem::take(&mut spawned.extra_pipes) { - if let spawn::WindowsStdioResult::Buffer(p) = item { - let raw = bun_core::heap::into_raw(p); - debug_assert_eq!(raw, ipc_pipe, "extra_pipes Box must wrap ipc_pipe"); - } - } - this.process = Some(spawned.to_process_handle(coord.vm.event_loop())); - - if let spawn::WindowsStdioResult::Buffer(pipe) = spawned.stdout.take() { - // SAFETY: `pipe` is a Box just produced by spawn_process; - // ownership transfers into the reader's `Source` (heap::take inside). - unsafe { - this.out - .reader - .start_with_pipe(bun_core::heap::into_raw(pipe)) + let worker: &mut Worker = &mut **this; + for (i, pipe) in [&mut worker.out, &mut worker.err].into_iter().enumerate() { + let Some(fd) = unadopted[i] else { continue }; + // A reader that fails to start (Windows only) has not taken the fd. + if pipe.reader.start(fd, true).is_err() { + return Err(crate::Error::PipeStartFailed); } - .map_err(|_| crate::Error::PipeStartFailed)?; + unadopted[i] = None; } - if let spawn::WindowsStdioResult::Buffer(pipe) = spawned.stderr.take() { - // SAFETY: see stdout above. - unsafe { - this.err - .reader - .start_with_pipe(bun_core::heap::into_raw(pipe)) - } - .map_err(|_| crate::Error::PipeStartFailed)?; - } - // `ipc_pipe` was Box-allocated via heap::into_raw above and - // initialised by spawn_process; ownership of the *mut Pipe transfers - // to the Channel on success (it does the Box::from_raw internally). - // On failure the caller still owns it (Channel.rs:294) and the - // `ipc_pipe_guard` errdefer performs `close_and_destroy`. - if !Channel::adopt_pipe(&raw mut this.ipc, coord.vm, ipc_pipe) { + } + // `adopt` closes the fd when it fails. + if let Some(ipc_fd) = unadopted[2].take() { + if !Channel::adopt(&raw mut this.ipc, ipc_fd, false) { return Err(crate::Error::ChannelAdoptFailed); } - // Channel now owns the Box; disarm the errdefer so end-of-block - // doesn't double-close. Any later error (watch_or_reap) is handled - // by the outer `this` guard, whose `Channel::default()` assignment - // drops the old Channel and `close_and_destroy`s the pipe via Drop. - let _ = scopeguard::ScopeGuard::into_inner(ipc_pipe_guard); + } else { + this.ipc.done.set(true); } let process: *mut Process = this.process.as_ref().expect("set above").as_ptr(); @@ -288,11 +183,10 @@ impl Worker { #[cfg(windows)] { if let Some(job) = coord.windows_job { - if let spawn::Poller::Uv(ref uv) = process.poller { - // SAFETY: FFI call; handles are valid (just spawned). - unsafe { - let _ = bun_sys::windows::AssignProcessToJobObject(job, uv.process_handle); - } + // SAFETY: FFI call; handles are valid (just spawned). + unsafe { + let _ = + bun_sys::windows::AssignProcessToJobObject(job, process.process_handle()); } } } @@ -385,6 +279,20 @@ impl ChannelOwner for Worker { } } +impl Worker { + /// What the worker printed before the frame that is being handled, or + /// before it exited, is in its pipes by now, and the loop may come to + /// those after this: take it. + pub(crate) fn drain_output(&mut self) { + #[cfg(windows)] + for pipe in [&raw mut self.out, &raw mut self.err] { + // SAFETY: live readers, outside their dispatches, which touch + // `captured` and `done` alone. + unsafe { bun_io::BufferedReader::drain(&raw mut (*pipe).reader) }; + } + } +} + /// Reads worker stdout/stderr. Accumulates into the worker's `captured` buffer /// and flushes atomically with the next test result so console output from /// concurrent files never interleaves. @@ -434,7 +342,6 @@ bun_io::impl_buffered_reader_parent! { on_read_chunk = |this, chunk, state| (*this).on_read_chunk(&chunk, state); on_reader_done = |this| (*this).on_reader_done(); on_reader_error = |this, err| (*this).on_reader_error(err); - // `vm.uv_loop()` is `*mut bun_io::Loop` on every target. - loop_ = |this| (*(*(*this).worker).coord).vm.uv_loop(); + loop_ = |this| (*(*(*this).worker).coord).vm.uws_loop(); event_loop = |this| (*(*(*this).worker).coord).event_loop_handle.as_event_loop_ctx(); } diff --git a/src/runtime/cli/test/parallel/runner.rs b/src/runtime/cli/test/parallel/runner.rs index d591d588b4e9..7496c75837df 100644 --- a/src/runtime/cli/test/parallel/runner.rs +++ b/src/runtime/cli/test/parallel/runner.rs @@ -475,7 +475,7 @@ fn jsx_runtime_tag_name(r: bun_options_types::schema::api::JsxRuntime) -> &'stat /// through, async dispose, etc.) gets to run, and on macOS — where there's no /// PDEATHSIG — coordinator death surfaces as channel close. Same `Channel` /// abstraction as the coordinator side: usockets over the socketpair on POSIX, -/// `uv.Pipe` over the inherited duplex named-pipe on Windows. +/// `bun_io::windows::Pipe` over the inherited duplex named-pipe on Windows. pub(crate) struct WorkerCommands { pub(crate) channel: Channel, /// Coordinator dispatches one `.run` and waits for `.file_done` before @@ -523,7 +523,7 @@ impl<'a> WorkerLoop<'a> { fn begin(&mut self) { // SAFETY: vm pointer is valid for the worker's lifetime. let vm = unsafe { &mut *self.vm }; - if !Channel::adopt(&raw mut self.cmds.channel, vm, Fd::from_uv(3)) { + if !Channel::adopt(&raw mut self.cmds.channel, Fd::from_crt(3), true) { bun_core::pretty_errorln!("error: test worker failed to adopt IPC fd"); Global::exit(1); } @@ -672,15 +672,17 @@ pub(crate) fn run_as_worker( worker_flush_aggregates(wloop.reporter, vm_ref, ctx, &mut wloop.cmds); // Drain any backpressure-buffered frames before exit so the coordinator // sees repeat_bufs / coverage_file. - while wloop.cmds.channel.has_pending_writes() && !wloop.cmds.channel.done.get() { - // SAFETY: event_loop pointer is valid while vm lives. - unsafe { (*vm_ref.event_loop()).tick() }; - if !wloop.cmds.channel.has_pending_writes() || wloop.cmds.channel.done.get() { - break; + vm_ref.run_with_api_lock(|| { + while wloop.cmds.channel.has_pending_writes() && !wloop.cmds.channel.done.get() { + // SAFETY: event_loop pointer is valid while vm lives. + unsafe { (*(*vm).event_loop()).tick() }; + if !wloop.cmds.channel.has_pending_writes() || wloop.cmds.channel.done.get() { + break; + } + // SAFETY: event_loop pointer is valid while vm lives. + unsafe { (*(*vm).event_loop()).auto_tick() }; } - // SAFETY: event_loop pointer is valid while vm lives. - unsafe { (*vm_ref.event_loop()).auto_tick() }; - } + }); // Mirror TestCommand::exec's exit path so BUN_DESTRUCT_VM_ON_EXIT teardown // (lastChanceToFinalize) runs; bypassing it leaks JSC-owned native state. vm_ref.exit_handler.exit_code = 0; diff --git a/src/runtime/cli/unlink_command.rs b/src/runtime/cli/unlink_command.rs index bac275fdb361..1f42cc680c38 100644 --- a/src/runtime/cli/unlink_command.rs +++ b/src/runtime/cli/unlink_command.rs @@ -136,7 +136,6 @@ fn unlink(ctx: &mut ContextData) -> crate::Result<()> { // Step 2. Setup the global directory let node_modules: Dir = 'brk: { - bin::Linker::ensure_umask(); let explicit_global_dir: &[u8] = match &ctx.install { Some(install_) => install_.global_dir.as_deref().unwrap_or(b""), None => b"", diff --git a/src/runtime/cli/upgrade_command.rs b/src/runtime/cli/upgrade_command.rs index 01ebf06778db..cceb6a025e2b 100644 --- a/src/runtime/cli/upgrade_command.rs +++ b/src/runtime/cli/upgrade_command.rs @@ -31,17 +31,6 @@ fn build_argv(parts: &[&[u8]]) -> Vec> { parts.iter().map(|p| Box::<[u8]>::from(*p)).collect() } -#[cfg(windows)] -#[inline] -fn spawn_windows_options() -> crate::api::bun::process::WindowsOptions { - crate::api::bun::process::WindowsOptions { - loop_: bun_event_loop::EventLoopHandle::init_mini( - bun_event_loop::MiniEventLoop::init_global(None, None), - ), - ..Default::default() - } -} - // `bun_resolver::fs::FileSystem` (the inline canonical type surface // in `resolver/lib.rs`) does not yet expose `tmpdir()`; the full impl lives in // the un-exported `fs_full` module. Shim it locally — open @@ -945,7 +934,6 @@ impl UpgradeCommand { stderr: spawn_sync::SyncStdio::Inherit, stdout: spawn_sync::SyncStdio::Inherit, stdin: spawn_sync::SyncStdio::Inherit, - windows: spawn_windows_options(), ..Default::default() }); let spawn_res = match spawn_res { @@ -989,8 +977,6 @@ impl UpgradeCommand { stdout: spawn_sync::SyncStdio::Buffer, stderr: spawn_sync::SyncStdio::Ignore, stdin: spawn_sync::SyncStdio::Ignore, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }); // Any spawn-time failure (allocator/OOM surfaces as @@ -1035,7 +1021,7 @@ impl UpgradeCommand { if !result.status.is_ok() { let _ = save_dir_.delete_tree(&version_name); let exit_code: u32 = match &result.status { - Status::Exited(e) => u32::from(e.code), + Status::Exited(e) => e.code, Status::Signaled(sig) => 128 + u32::from(*sig), _ => 1, }; @@ -1325,8 +1311,6 @@ impl UpgradeCommand { stdout: spawn_sync::SyncStdio::Buffer, stderr: spawn_sync::SyncStdio::Buffer, stdin: spawn_sync::SyncStdio::Ignore, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }); } diff --git a/src/runtime/dispatch.rs b/src/runtime/dispatch.rs index 8623f45f375c..9249cfe05f76 100644 --- a/src/runtime/dispatch.rs +++ b/src/runtime/dispatch.rs @@ -27,10 +27,6 @@ pub(crate) mod js2native; use bun_event_loop::{Task, task_tag}; -// `FilePoll::on_update` dispatch is POSIX-only (the symbol is declared -// `extern "Rust"` in `aio::posix_event_loop` and never referenced on Windows, -// where libuv drives I/O readiness directly). -#[cfg(not(windows))] use bun_io::posix_event_loop::{FilePoll, Flags as PollFlag, poll_tag}; use bun_event_loop::EventLoopTimer::{ @@ -42,25 +38,6 @@ use bun_jsc::task::report_error_or_terminate; use bun_jsc::virtual_machine::VirtualMachine; use bun_jsc::{JSGlobalObject, JsResult}; -/// X-macro: the `node:fs` ops that are libuv requests on Windows -/// (`UVFSRequest`); they complete on the JS thread and re-enter through the -/// task queue under a per-op tag. Every other async fs op is a `bun_jsc::Job`. -/// Row shape: `$tag $ty;` (`task_tag::*` const, `fs_async::*` alias). -#[cfg(windows)] -macro_rules! for_each_fs_uv_op { - ($m:ident) => { - $m! { - Open Open; Close Close; Read Read; Write Write; Readv Readv; - Writev Writev; StatFS Statfs; - } - }; -} -/// Expand the fs-op table to an or-pattern over `task_tag::*` (pattern position). -#[cfg(windows)] -macro_rules! __fs_pat { - ($($tag:ident $ty:ident;)*) => { $(task_tag::$tag)|* }; -} - // ── per-variant payload types ──────────────────────────────────────────────── // (high-tier owns them all; grouped by source module) @@ -108,8 +85,6 @@ use crate::bake::dev_server::DevServer; use crate::bake::dev_server::HotReloadEvent as BakeHotReloadEvent; use crate::bake::dev_server::source_map_store::SourceMapStore; -#[cfg(windows)] -use crate::node::fs::async_ as fs_async; use crate::node::node_fs_stat_watcher::StatWatcherScheduler; use crate::node::node_fs_watcher::FSWatchTask; use crate::node::node_zlib_binding; @@ -270,11 +245,6 @@ pub(crate) fn run_task( )) }; } - #[cfg(windows)] - task_tag::GetAddrInfoLibuvComplete => { - // SAFETY: boxed in `on_raw_libuv_complete`; the arm consumes it. - unsafe { bun_core::heap::take(cast_ptr!(crate::dns_jsc::LibuvCompleteHolder)) }.run(); - } task_tag::ValkeyDeferredClose => { // SAFETY: boxed at the enqueue site; the arm consumes it. unsafe { @@ -425,24 +395,6 @@ pub(crate) fn run_task( }; } #[cfg(windows)] - task_tag::CopyFileWindowsMkdirp => { - // SAFETY: the live copy `on_mkdirp_complete_concurrent` posted. - unsafe { - crate::webcore::blob::copy_file::CopyFileWindowsMkdirp::run(cast_ptr!( - crate::webcore::blob::copy_file::CopyFileWindowsMkdirp<'_> - )) - }; - } - #[cfg(windows)] - task_tag::WriteFileWindowsMkdirp => { - // SAFETY: the live write `on_mkdirp_complete_concurrent` posted. - unsafe { - crate::webcore::blob::write_file::WriteFileWindowsMkdirp::run(cast_ptr!( - crate::webcore::blob::write_file::WriteFileWindowsMkdirp - )) - }; - } - #[cfg(windows)] task_tag::ChromePipeEvent => { // SAFETY: boxed in `PipeEvent::post`; the arm consumes it. unsafe { bun_core::heap::take(cast_ptr!(crate::webview::chrome_process::QueuedEvent)) } @@ -547,20 +499,6 @@ pub(crate) fn run_task( ran?; } - // ── node:fs libuv-request ops (Windows) ────────────────────────── - #[cfg(windows)] - for_each_fs_uv_op!(__fs_pat) => { - macro_rules! __fs_run { - ($($tag:ident $ty:ident;)*) => { match task.tag { - // SAFETY: §Dispatch — tag identifies pointee. The task frees itself, so it takes the raw pointer. - $(task_tag::$tag => unsafe { fs_async::$ty::run_from_js_thread(cast_ptr!(fs_async::$ty)) }?,)* - // SAFETY: outer arm guard proves one of the table tags matched. - _ => unsafe { core::hint::unreachable_unchecked() }, - }}; - } - for_each_fs_uv_op!(__fs_run); - } - // ── compression streams ────────────────────────────────────────── task_tag::NativeZlib => compression_arm!(NativeZlib), task_tag::NativeBrotli => compression_arm!(NativeBrotli), @@ -727,7 +665,7 @@ fn run_task_cold(task: Task) { /// `release_task_unrun` track `bun_event_loop::task_tag::COUNT`. Bump when /// adding a variant — and give it an arm in both. const _: () = assert!( - task_tag::COUNT == 83, + task_tag::COUNT == 73, "dispatch::run_task / release_task_unrun arm count out of sync with bun_event_loop::task_tag", ); @@ -797,10 +735,13 @@ pub(crate) fn tick_queue_with_count( /// calls this directly (link-time resolved) so it never names `Subprocess` / /// `FileSink` / `DNSResolver` / etc. /// +/// On Windows a `FilePoll` only ever watches a socket (pipes, consoles and +/// process exits complete through the loop's port instead), so only the owners +/// that poll sockets are dispatched there. +/// /// # Safety /// `poll` must point at a live [`FilePoll`] for the duration of the call /// (guaranteed by `FilePoll::on_update`, the only caller). -#[cfg(not(windows))] #[unsafe(no_mangle)] pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i64) { // SAFETY: contract above. @@ -811,6 +752,7 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 debug_assert!(!owner.is_null()); /// `ptr.as(T)` — recover the typed owner. + #[cfg(not(windows))] macro_rules! owner_as { ($ty:ty) => {{ // SAFETY: tag set with this pointee type at `FilePoll::init`. @@ -822,6 +764,7 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 /// pick their own deref mode without aliasing UB) then runs `$body`. The /// 1-arg form is the plain `on_poll(size_or_offset, hup)` call that /// covers most tags. + #[cfg(not(windows))] macro_rules! poll_arm { ($Ty:ty) => { poll_arm!($Ty, |h| { @@ -838,6 +781,7 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 } match owner.tag() { + #[cfg(not(windows))] poll_tag::BUFFERED_READER => poll_arm!(bun_io::BufferedReader, |h| { // SAFETY: tag matched, so `owner.ptr` is a live `*mut BufferedReader` // set at `FilePoll::init`. Passed raw: `on_poll`'s read loops run @@ -845,6 +789,7 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 // dispatch. unsafe { bun_io::BufferedReader::on_poll(h, size_or_offset as isize, hup) } }), + #[cfg(not(windows))] poll_tag::PROCESS => { // Bypass `owner_as!` (which yields `&mut`) — `Process` may be freed // by the trailing `deref`, so keep raw provenance end-to-end. @@ -852,10 +797,12 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 // SAFETY: `proc` carries the +1 ref taken at queue time; this drops it. unsafe { Process::on_wait_pid_from_event_loop_task(proc) }; } + #[cfg(not(windows))] poll_tag::MEMORY_PRESSURE => { // SAFETY: `poll` is live per `__bun_run_file_poll`'s contract. crate::node::memory_pressure::on_poll(unsafe { &mut *poll }, size_or_offset); } + #[cfg(not(windows))] poll_tag::PARENT_DEATH_WATCHDOG => { let wd = owner_as!(bun_io::parent_death_watchdog::ParentDeathWatchdog); // Mac-only — debug-assert elsewhere (Linux uses prctl(PR_SET_PDEATHSIG)). @@ -868,15 +815,20 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 } } + #[cfg(not(windows))] poll_tag::FILE_SINK => poll_arm!(FileSinkPoll), + #[cfg(not(windows))] poll_tag::STATIC_PIPE_WRITER => poll_arm!(StaticPipeWriterPoll>), + #[cfg(not(windows))] poll_tag::SHELL_STATIC_PIPE_WRITER => { poll_arm!(StaticPipeWriterPoll) } + #[cfg(not(windows))] poll_tag::SECURITY_SCAN_STATIC_PIPE_WRITER => { poll_arm!(StaticPipeWriterPoll>) } // `bun.shell.Interpreter.IOWriter.Poll` + #[cfg(not(windows))] poll_tag::SHELL_BUFFERED_WRITER => poll_arm!(ShellBufferedWriterPoll, |h| { // SAFETY: tag matched, so `owner.ptr` is a live `*mut ShellBufferedWriterPoll` // set at `FilePoll::init`; exclusive for this dispatch. @@ -890,6 +842,7 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 // SAFETY: `poll` outlives this call (caller contract). resolver.on_dns_poll(unsafe { &mut *poll }); } + #[cfg(not(windows))] poll_tag::GET_ADDR_INFO_REQUEST => { #[cfg(target_os = "macos")] { @@ -901,8 +854,10 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 debug_assert!(false, "dns_sd SharedConnection poll on non-mac"); } } + #[cfg(not(windows))] poll_tag::TERMINAL_POLL => poll_arm!(TerminalPoll), // `OutputReader = BufferedReader` in install crate — separate tag for ownership. + #[cfg(not(windows))] poll_tag::LIFECYCLE_SCRIPT_SUBPROCESS_OUTPUT_READER => { poll_arm!(bun_io::BufferedReader, |h| { // SAFETY: tag matched, so `owner.ptr` is a live `*mut BufferedReader` @@ -916,6 +871,8 @@ pub(crate) unsafe fn __bun_run_file_poll(poll: *mut FilePoll, size_or_offset: i6 // when it was null; here we just no-op the unknown tag. let _ = (size_or_offset, hup); } + #[cfg(windows)] + _ => debug_assert!(false, "FilePoll owner that does not poll a socket"), } } @@ -1199,6 +1156,22 @@ pub(crate) unsafe fn __bun_fire_timer( Ok(()) } } + EventLoopTimerTag::WindowsNamedPipeEndOfWrite => { + #[cfg(windows)] + { + let container = owner!(WindowsNamedPipe, end_of_write_timer); + // SAFETY: per fn contract. + unsafe { (*container).on_end_of_write_idle() }; + Ok(()) + } + #[cfg(not(windows))] + { + if cfg!(debug_assertions) { + unreachable!("WindowsNamedPipe timer on non-Windows"); + } + Ok(()) + } + } EventLoopTimerTag::PostgresSQLConnectionTimeout => { // SAFETY: §Dispatch — tag set together with the container at // construction; `t` is the connection's `timer` field. @@ -1484,57 +1457,18 @@ fn __bun_release_task_unrun(task: bun_event_loop::Task) { release!(crate::valkey_jsc::js_valkey::ValkeyDeferredClose) } // ── Windows-only producers ─────────────────────────────────────── - task_tag::GetAddrInfoLibuvComplete => { - #[cfg(windows)] - release!(crate::dns_jsc::LibuvCompleteHolder); - #[cfg(not(windows))] - unreachable!("windows-only tag"); - } task_tag::WindowsNamedPipeContext => { #[cfg(windows)] release!(crate::socket::WindowsNamedPipeContext); #[cfg(not(windows))] unreachable!("windows-only tag"); } - task_tag::CopyFileWindowsMkdirp => { - #[cfg(windows)] - release!(crate::webcore::blob::copy_file::CopyFileWindowsMkdirp<'_>); - #[cfg(not(windows))] - unreachable!("windows-only tag"); - } - task_tag::WriteFileWindowsMkdirp => { - #[cfg(windows)] - release!(crate::webcore::blob::write_file::WriteFileWindowsMkdirp); - #[cfg(not(windows))] - unreachable!("windows-only tag"); - } task_tag::ChromePipeEvent => { #[cfg(windows)] release!(crate::webview::chrome_process::QueuedEvent); #[cfg(not(windows))] unreachable!("windows-only tag"); } - task_tag::Open - | task_tag::Close - | task_tag::Read - | task_tag::Readv - | task_tag::Write - | task_tag::Writev - | task_tag::StatFS => { - #[cfg(windows)] - { - macro_rules! __fs_release { - ($($tag:ident $ty:ident;)*) => { match task.tag { - $(task_tag::$tag => release!(fs_async::$ty),)* - // SAFETY: the outer arm proves one of the table tags matched. - _ => unsafe { core::hint::unreachable_unchecked() }, - }}; - } - for_each_fs_uv_op!(__fs_release); - } - #[cfg(not(windows))] - unreachable!("windows-only tag (libuv fs request)"); - } // Every tag has an arm above (`task_tag::COUNT` is asserted); a value // outside the range is a producer bug. _ => unreachable!("task tag out of range: {}", task.tag.0), diff --git a/src/runtime/dns_jsc/dns.rs b/src/runtime/dns_jsc/dns.rs index 5634a548d18d..1aff8f15ed06 100644 --- a/src/runtime/dns_jsc/dns.rs +++ b/src/runtime/dns_jsc/dns.rs @@ -8,17 +8,14 @@ use core::ptr::{self, NonNull}; use core::sync::atomic::{AtomicUsize, Ordering}; use bun_collections::{ArrayHashMap, HiveArray}; -#[cfg(not(windows))] use bun_core::Output; use bun_core::{self as bun, env_var, fmt as bun_fmt}; use bun_core::{ZStr, strings}; -#[cfg(not(windows))] use bun_dns::ResultList as GetAddrInfoResultList; use bun_dns::{ self, Backend as GetAddrInfoBackend, GetAddrInfo, GetAddrInfoResult, Options as GetAddrInfoOptions, ResultAny as GetAddrInfoResultAny, }; -#[cfg(not(windows))] use bun_io::FilePoll; use bun_io::{self as Async, KeepAlive}; use bun_jsc::bun_string_jsc; @@ -28,9 +25,6 @@ use bun_jsc::{ SystemError, host_fn, }; use bun_ptr::RefPtr; -#[cfg(windows)] -use bun_sys::windows::libuv; -#[cfg(not(windows))] use bun_sys::{self as sys}; use bun_uws::{ConnectingSocket, Loop}; use bun_wyhash::hash as wyhash; @@ -42,8 +36,8 @@ use bun_cares_sys::c_ares_draft as c_ares; // `sockaddr_storage` / `addrinfo` / `AF_*` / `AI_*` are absent from `libc` on // the MSVC target; route through a single `netc` shim so call sites stay -// target-agnostic. Windows values come from ws2def.h via the libuv-sys mirror -// (layout-identical: `ADDRINFOA`, 128-byte 8-aligned `sockaddr_storage`). +// target-agnostic. Windows values come from ws2def.h (`ADDRINFOA`, 128-byte +// 8-aligned `sockaddr_storage`). #[cfg(not(windows))] pub(crate) mod netc { pub(crate) use bun_dns::AI_ADDRCONFIG; @@ -54,12 +48,12 @@ pub(crate) mod netc { } #[cfg(windows)] pub(crate) mod netc { - pub(crate) use bun_libuv_sys::{ - addrinfo, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, + pub(crate) use bun_sys::windows::ws2_32::{ + AF_INET, AF_INET6, AF_UNSPEC, SOCK_STREAM, addrinfo, sockaddr, sockaddr_in, sockaddr_in6, + sockaddr_storage, }; - pub(crate) use bun_sys::windows::ws2_32::{AF_INET, AF_INET6, AF_UNSPEC, SOCK_STREAM}; - /// The libuv spelling: `c_ares::Error::init_eai` reads UV_EAI_* codes on Windows. - pub(crate) const EAI_NONAME: core::ffi::c_int = bun_libuv_sys::UV_EAI_NONAME; + pub(crate) const EAI_NONAME: core::ffi::c_int = + bun_sys::windows::Win32Error::WSAHOST_NOT_FOUND.0 as core::ffi::c_int; } type SockaddrStorage = netc::sockaddr_storage; type AddrInfo = netc::addrinfo; @@ -96,10 +90,9 @@ unsafe impl Send for SendPtr {} /// loop, so the global `Js` ctx is the correct erasure here. #[inline] fn js_event_loop_ctx() -> Async::EventLoopCtx { - Async::posix_event_loop::get_vm_ctx(Async::AllocatorType::Js) + Async::get_vm_ctx(Async::AllocatorType::Js) } -bun_output::declare_scope!(LibUVBackend, visible); bun_output::declare_scope!(ResolveInfoRequest, hidden); bun_output::declare_scope!(GetHostByAddrInfoRequest, visible); bun_output::declare_scope!(CAresNameInfo, hidden); @@ -126,10 +119,9 @@ const IANA_DNS_PORT: i32 = 53; pub(crate) mod dns_sd; // ────────────────────────────────────────────────────────────────────────── -// LibC (blocking getaddrinfo on a worker thread; non-Windows) +// LibC (blocking getaddrinfo on a worker thread) // ────────────────────────────────────────────────────────────────────────── -#[cfg(not(windows))] mod lib_c { use super::*; @@ -180,147 +172,6 @@ mod lib_c { } } -// ────────────────────────────────────────────────────────────────────────── -// LibUVBackend (Windows uv_getaddrinfo) -// ────────────────────────────────────────────────────────────────────────── - -/// The windows implementation borrows the struct used for libc getaddrinfo -#[cfg(windows)] -pub(crate) mod lib_uv_backend { - use super::*; - - pub(crate) struct LibuvCompleteHolder { - uv_info: *mut libuv::uv_getaddrinfo_t, - } - - impl LibuvCompleteHolder { - pub(crate) fn run(self: Box) { - GetAddrInfoRequest::on_libuv_complete(self.uv_info); - } - } - impl bun_event_loop::Taskable for LibuvCompleteHolder { - const TAG: bun_event_loop::TaskTag = bun_event_loop::task_tag::GetAddrInfoLibuvComplete; - /// A uv_getaddrinfo the stop phase cancelled and drained into the queue: - /// its completion is what frees the request and its cache slot, and it - /// only settles promises (no callback runs; script is forbidden), so run it. - unsafe fn release_unrun(this: *mut Self) { - // SAFETY: fn contract — the box `on_raw_libuv_complete` queued. - unsafe { bun_core::heap::take(this) }.run(); - } - /// A lookup is shared by the realm; each promise waiting on it is settled for its own context. - unsafe fn context(_: *const Self) -> bun_event_loop::ContextId { - bun_event_loop::ContextId::NONE - } - } - - extern "C" fn on_raw_libuv_complete( - uv_info: *mut libuv::uv_getaddrinfo_t, - _status: c_int, - _res: *mut libuv::addrinfo, - ) { - // TODO: We schedule a task to run because otherwise the promise will not be solved, we need to investigate this - // SAFETY: data was set to the GetAddrInfoRequest pointer before uv_getaddrinfo - let this: *mut GetAddrInfoRequest = unsafe { (*uv_info).data.cast() }; - - let task = jsc::Task::from_boxed(Box::new(LibuvCompleteHolder { uv_info })); - // SAFETY: `this` is the live GetAddrInfoRequest set as uv data. - unsafe { - (*this) - .head - .global_this() - .bun_vm() - .as_mut() - .enqueue_task(task); - } - } - - pub(crate) fn lookup( - this: &Resolver, - query: GetAddrInfo, - global_this: &JSGlobalObject, - context: bun_jsc::ContextId, - ) -> JsResult { - let key = get_addr_info_request::PendingCacheKey::init(&query); - - let cache = - this.get_or_put_into_pending_cache(&key, PendingCacheField::PendingHostCacheNative); - if let CacheHit::Inflight(inflight) = cache { - let dns_lookup = DNSLookup::init(this.as_ctx_ptr(), global_this, context); - unsafe { (*inflight).append(dns_lookup) }; - return Ok(unsafe { (*dns_lookup).promise.value() }); - } - - let request = GetAddrInfoRequest::init( - cache, - get_addr_info_request::Backend::Libc(get_addr_info_request::LibcBackend::uv_uninit()), - Some(this.as_ctx_ptr()), - global_this, - context, - PendingCacheField::PendingHostCacheNative, - ); - - let hints = query.options.to_libc(); - let mut port_buf = [0u8; 128]; - let port_len = bun_fmt::print_int(&mut port_buf, query.port); - port_buf[port_len] = 0; - // SAFETY: port_buf[port_len] == 0 written above - let port_z = ZStr::from_buf(&port_buf[..], port_len); - - let mut hostname = bun_paths::path_buffer_pool::get(); - // Reserve the last byte for the NUL terminator so the index below can never - // exceed the buffer even if the upstream length guard in `doLookup` is bypassed. - let cap = hostname.len() - 1; - // `strings::copy` returns a slice borrowing `hostname`; take only its length - // so the mutable borrow ends immediately and `hostname` can be indexed again. - let copied_len = strings::copy(&mut hostname[..cap], query.name.as_ref()).len(); - hostname[copied_len] = 0; - // SAFETY: hostname[copied_len] == 0 written above - let host = ZStr::from_buf(&hostname[..], copied_len); - - // SAFETY: request lives until completion; backend.libc.uv is the embedded uv_getaddrinfo_t - let promise = unsafe { - (*request).backend.as_libc_uv_mut().data = request.cast::(); - let promise = (*request).head.promise.value(); - let rc = libuv::uv_getaddrinfo( - this.vm().uv_loop(), - (*request).backend.as_libc_uv_mut(), - Some(on_raw_libuv_complete), - host.as_ptr().cast::(), - port_z.as_ptr().cast::(), - hints - .as_ref() - .map_or(ptr::null(), |h| (h as *const AddrInfo).cast()), - ); - if rc.int() < 0 { - // uv_getaddrinfo can fail synchronously before it queues any work - // (e.g. UV_EINVAL from the 256-byte IDNA buffer for long hostnames, - // or UV_ENOMEM). Route the error through the same path the async - // completion would have taken so the pending-cache slot is released - // and the promise is rejected with a DNSException. - if let Some(resolver) = (*request).resolver_for_caching { - if let Some(pos) = (*request).pending_slot { - (*resolver).drain_pending_host_native( - pos, - (*request).head.global_this(), - rc.int(), - &GetAddrInfoResultAny::Addrinfo(ptr::null_mut()), - ); - return Ok(promise); - } - } - // Consume the request and move `head` out by value; `ptr::read` - // + `heap::take` would double-Drop `DNSLookup` (impls Drop). - let owned = *bun_core::heap::take(request); - let mut head = owned.head; - DNSLookup::process_get_addr_info_native(&mut head, rc.int(), ptr::null_mut()); - return Ok(promise); - } - promise - }; - Ok(promise) - } -} - // ────────────────────────────────────────────────────────────────────────── // normalizeDNSName // ────────────────────────────────────────────────────────────────────────── @@ -946,7 +797,6 @@ pub(crate) mod get_addr_info_request { use super::*; /// The blocking `getaddrinfo` of one libc-backend lookup, run on the pool. - #[cfg(not(windows))] pub(crate) struct LibcLookup { pub(crate) backend: LibcBackend, } @@ -956,12 +806,9 @@ pub(crate) mod get_addr_info_request { /// cache points at. Consumed by the completion; dropped unconsumed only /// when the VM tears down first, in which case everything is freed and /// nothing is settled. - #[cfg(not(windows))] pub(crate) struct LibcRequest(pub(crate) NonNull); // SAFETY: only the JS thread touches the request (see type doc). - #[cfg(not(windows))] unsafe impl bun_jsc::job::JsAffine for LibcRequest {} - #[cfg(not(windows))] impl Drop for LibcRequest { fn drop(&mut self) { let req = self.0.as_ptr(); @@ -986,10 +833,10 @@ pub(crate) mod get_addr_info_request { } } - #[cfg(not(windows))] impl bun_jsc::JobContext for LibcLookup { type OffThread = Self; type Js = LibcRequest; + const ALWAYS_WAITS: bool = true; fn run( this: &mut Self, done: bun_jsc::Completion, @@ -1053,15 +900,14 @@ pub(crate) mod get_addr_info_request { } } - /// Non-Windows libc backend (worker-thread blocking getaddrinfo). - #[cfg(not(windows))] + /// The libc backend (worker-thread blocking getaddrinfo). pub(crate) enum LibcBackend { Success(GetAddrInfoResultList), + /// An `EAI_*` code. Err(i32), Query(GetAddrInfo), } - #[cfg(not(windows))] impl LibcBackend { pub(crate) fn run(&mut self) { let LibcBackend::Query(query) = self else { @@ -1075,32 +921,41 @@ pub(crate) mod get_addr_info_request { // SAFETY: NUL written at port_buf[port_len] let port_z = ZStr::from_buf(&port_buf[..], port_len); - let mut hostname = bun_paths::path_buffer_pool::get(); - // Reserve the last byte for the NUL terminator so the index below - // can never exceed the buffer even if the upstream length guard in - // `doLookup` is bypassed. - let cap = hostname.len() - 1; - let copied_len = strings::copy(&mut hostname[..cap], &query_name).len(); - hostname[copied_len] = 0; let mut addrinfo: *mut AddrInfo = ptr::null_mut(); - // SAFETY: hostname[copied_len] == 0 - let host = ZStr::from_buf(&hostname[..], copied_len); let debug_timer = Output::DebugTimer::start(); - // SAFETY: FFI; all pointers valid for the call duration - let err = unsafe { - libc::getaddrinfo( - host.as_ptr().cast::(), - if port_len > 0 { - port_z.as_ptr().cast::() - } else { - ptr::null() - }, - hints - .as_ref() - .map(std::ptr::from_ref) - .unwrap_or(ptr::null()), - &raw mut addrinfo, - ) + #[cfg(windows)] + let err = windows_get_addr_info( + &query_name, + port_z.as_bytes(), + hints.as_ref(), + &raw mut addrinfo, + ); + #[cfg(not(windows))] + let err = { + let mut hostname = bun_paths::path_buffer_pool::get(); + // Reserve the last byte for the NUL terminator so the index below + // can never exceed the buffer even if the upstream length guard in + // `doLookup` is bypassed. + let cap = hostname.len() - 1; + let copied_len = strings::copy(&mut hostname[..cap], &query_name).len(); + hostname[copied_len] = 0; + let host = ZStr::from_buf(&hostname[..], copied_len); + // SAFETY: FFI; all pointers valid for the call duration + unsafe { + libc::getaddrinfo( + host.as_ptr().cast::(), + if port_len > 0 { + port_z.as_ptr().cast::() + } else { + ptr::null() + }, + hints + .as_ref() + .map(std::ptr::from_ref) + .unwrap_or(ptr::null()), + &raw mut addrinfo, + ) + } }; sys::syslog!( "getaddrinfo({}, {}) = {} ({})", @@ -1117,8 +972,16 @@ pub(crate) mod get_addr_info_request { // do not free addrinfo when err != 0: getaddrinfo only allocates the // result list on success, so the out-pointer is unspecified on error. let _free = scopeguard::guard(addrinfo, |a| { - // SAFETY: `a` was returned by libc::getaddrinfo (non-null per the check above). - unsafe { bun_dns::freeaddrinfo(a) } + // SAFETY: `a` was returned by the call above (non-null per the check above). + #[cfg(windows)] + unsafe { + FreeAddrInfoW(a) + }; + // SAFETY: as above. + #[cfg(not(windows))] + unsafe { + bun_dns::freeaddrinfo(a) + } }); // SAFETY: addrinfo is non-null (checked above); freed by `_free` guard after copy. @@ -1126,19 +989,88 @@ pub(crate) mod get_addr_info_request { } } - /// Windows libc backend wraps a uv_getaddrinfo_t. #[cfg(windows)] - pub(crate) struct LibcBackend { - pub(crate) uv: libuv::uv_getaddrinfo_t, - } + #[link(name = "ws2_32")] + unsafe extern "system" { + /// `ADDRINFOW` has `ADDRINFOA`'s layout; only `ai_canonname`'s pointee + /// differs, and nothing here reads it. + fn GetAddrInfoW( + node: *const u16, + service: *const u16, + hints: *const AddrInfo, + result: *mut *mut AddrInfo, + ) -> c_int; + fn FreeAddrInfoW(info: *mut AddrInfo); + } + + /// `GetAddrInfoW`: it resolves Unicode host names, which `getaddrinfo` + /// would read in the ANSI code page. #[cfg(windows)] - impl LibcBackend { - pub(crate) fn uv_uninit() -> Self { - Self { - uv: bun_core::ffi::zeroed(), - } + fn windows_get_addr_info( + host: &[u8], + service: &[u8], + hints: Option<&AddrInfo>, + result: *mut *mut AddrInfo, + ) -> c_int { + bun_uws_sys::iocp::us_internal_winsock_ensure(); + + let mut host_buf = bun_paths::w_path_buffer_pool::get(); + let mut service_buf = [0u16; 8]; + let host_cap = host_buf.len() - 1; + let Some(host_len) = + strings::try_convert_utf8_to_utf16_in_buffer(&mut host_buf[..host_cap], host) + .map(|w| w.len()) + else { + return netc::EAI_NONAME; + }; + // The resolver would encode a name that is not ASCII by its own rules, + // which are not the ones URLs and Node go by. + let mut ascii_buf = [0u16; 256]; + let host_ptr = if host.is_ascii() { + host_buf[host_len] = 0; + host_buf.as_ptr() + } else { + unsafe extern "C" { + fn Bun__domainToASCII16( + name: *const u16, + length: i32, + out: *mut u16, + capacity: i32, + ) -> i32; + } + // SAFETY: `host_buf[..host_len]` is initialized; `ascii_buf` has + // room for the capacity passed, less one for the terminator. + let len = unsafe { + Bun__domainToASCII16( + host_buf.as_ptr(), + host_len as i32, + ascii_buf.as_mut_ptr(), + (ascii_buf.len() - 1) as i32, + ) + }; + let Ok(len) = usize::try_from(len) else { + return netc::EAI_NONAME; + }; + ascii_buf[len] = 0; + ascii_buf.as_ptr() + }; + // The service is the decimal port. + for (dst, src) in service_buf.iter_mut().zip(service) { + *dst = u16::from(*src); + } + + // SAFETY: both strings are NUL-terminated; `hints` is null or valid; + // `result` is a valid out-pointer. + unsafe { + GetAddrInfoW( + host_ptr, + service_buf.as_ptr(), + hints.map_or(ptr::null(), std::ptr::from_ref), + result, + ) } } + pub(crate) enum Backend { CAres, #[cfg(target_os = "macos")] @@ -1154,13 +1086,6 @@ pub(crate) mod get_addr_info_request { _ => unreachable!(), } } - #[cfg(windows)] - pub(crate) fn as_libc_uv_mut(&mut self) -> &mut libuv::uv_getaddrinfo_t { - match self { - Backend::Libc(l) => &mut l.uv, - _ => unreachable!(), - } - } } } @@ -1283,7 +1208,6 @@ impl GetAddrInfoRequest { } } - #[cfg(not(windows))] /// # Safety /// `this` must be the live heap `GetAddrInfoRequest` whose `run` already /// completed; consumed (freed) on every path. @@ -1378,58 +1302,6 @@ impl GetAddrInfoRequest { DNSLookup::process_get_addr_info(&raw mut head, err_, timeout, result); } } - - #[cfg(windows)] - pub(crate) fn on_libuv_complete(uv_info: *mut libuv::uv_getaddrinfo_t) { - unsafe { - let retcode = (*uv_info).retcode.int(); - bun_output::scoped_log!(GetAddrInfoRequest, "onLibUVComplete: status={}", retcode); - let this: *mut Self = (*uv_info).data.cast(); - debug_assert!(uv_info == core::ptr::from_mut((*this).backend.as_libc_uv_mut())); - - // On Windows, libuv's `uv_getaddrinfo` calls `GetAddrInfoW` then - // re-packs the wide result into a single ANSI block allocated via - // `uv__malloc`; that block must be released with `uv_freeaddrinfo` - // (== `uv__free`). `GetAddrInfoResultAny::Addrinfo`'s `Drop` calls - // `ws2_32!freeaddrinfo`, which is the wrong allocator here and - // would corrupt the heap. Convert to an owned `List` immediately, free the libuv - // buffer with the correct deallocator, and pass `List` downstream - // so `ResultAny::Drop` never sees libuv-owned memory. - let addrinfo = (*uv_info).addrinfo; - let result_any = if addrinfo.is_null() { - GetAddrInfoResultAny::Addrinfo(ptr::null_mut()) - } else { - let list = GetAddrInfoResult::to_list(&*addrinfo); - libuv::uv_freeaddrinfo(addrinfo.cast()); - GetAddrInfoResultAny::List(list) - }; - - if let Some(resolver) = (*this).resolver_for_caching { - if let Some(pos) = (*this).pending_slot { - (*resolver).drain_pending_host_native( - pos, - (*this).head.global_this(), - retcode, - &result_any, - ); - return; - } - } - - // Consume the request and move `head` out by value; `ptr::read` - // + `heap::take` would double-Drop `DNSLookup` (impls Drop). - let owned = *bun_core::heap::take(this); - let mut head = owned.head; - // Inline `process_get_addr_info_native` so the success path can - // reuse the owned `List` instead of re-wrapping the (now-freed) - // raw `addrinfo` pointer. - if c_ares::Error::init_eai(retcode).is_some() { - DNSLookup::process_get_addr_info_native(&raw mut head, retcode, ptr::null_mut()); - } else { - DNSLookup::on_complete_native(&raw mut head, &result_any); - } - } - } } // Wires `GetAddrInfoRequest` into `Channel::get_addr_info`. @@ -1922,7 +1794,7 @@ impl Outcome { } /// The resolver backends' completion callbacks (c-ares poll, libinfo, - /// libuv) land here to settle the lookup's promise with a value built by + /// the work pool) land here to settle the lookup's promise with a value built by /// the resolver: this is their fold for what settling leaves pending /// (allocation failure, a terminating VM). fn settle( @@ -1953,9 +1825,8 @@ impl Drop for DNSLookup { let _ = self.global_this(); // DNSLookup is always created on the JS event loop (it holds a JSGlobalObject), // so the Js-arm vtable is the correct EventLoopCtx for KeepAlive::unref. - self.poll_ref.unref(Async::posix_event_loop::get_vm_ctx( - Async::AllocatorType::Js, - )); + self.poll_ref + .unref(Async::get_vm_ctx(Async::AllocatorType::Js)); } } @@ -1985,32 +1856,7 @@ impl Resolver { /// `DNSLookup::global_this` (to enqueue the rejection task) and the hive /// `FilePoll` (to unregister it from the loop). Running this after either /// is freed is a UAF (Node `test-worker-dns-terminate.js`). - /// Windows: `uv_getaddrinfo` requests are uv *requests* on this thread's - /// loop, which the teardown drains before closing the loop; cancel the ones - /// still in flight so that drain is prompt (each completes through its - /// callback with UV_ECANCELED against the still-live VM). - #[cfg(windows)] - pub(crate) fn cancel_pending_uv_requests_for_teardown(&self) { - // SAFETY: JS thread; no other borrow of the cache is live during the - // stop phase (completions run later, from the loop drain). - let cache = unsafe { self.pending_host_cache_native.get_mut() }; - let mut set = cache.used.iter_set(); - while let Some(index) = set.next() { - // SAFETY: a set slot is an initialised `PendingCacheKey`; JS thread. - let lookup = unsafe { (*cache.ptr_at(index)).lookup }; - if lookup.is_null() { - continue; - } - // SAFETY: `lookup` is the live boxed request until its completion - // callback removes it from the cache. - unsafe { - if let get_addr_info_request::Backend::Libc(l) = &mut (*lookup).backend { - let _ = libuv::uv_cancel(core::ptr::from_mut(&mut l.uv).cast()); - } - } - } - } - + /// /// `Stopped` if a channel was open (its pending queries just failed with /// `ARES_EDESTRUCTION` into their callbacks). /// @@ -2652,11 +2498,8 @@ pub(crate) mod internal { fn after_result(req: *mut Request, info: *mut AddrInfo, err: c_int) { let results: Option> = if !info.is_null() { let res = process_results(info); - // ws2_32!getaddrinfo-allocated on Windows — free via the matching - // ws2_32!freeaddrinfo (NOT uv_freeaddrinfo: different allocator). - // `.cast()` is identity on POSIX, libuv_sys→ws2_32 addrinfo on Windows. // SAFETY: `info` is non-null (checked above) and owned by getaddrinfo. - unsafe { bun_dns::freeaddrinfo(info.cast()) }; + unsafe { bun_dns::freeaddrinfo(info) }; Some(res) } else { None @@ -2702,9 +2545,11 @@ pub(crate) mod internal { }; #[cfg(windows)] + // SAFETY: FFI getaddrinfo; `req.key.host` is the owned NUL-terminated host + // set at construction, `wsa_hints`/`addrinfo` are stack locals. unsafe { use bun_sys::windows::ws2_32 as wsa; - libuv::uv__winsock_ensure(); + bun_uws_sys::iocp::us_internal_winsock_ensure(); let mut wsa_hints: wsa::addrinfo = bun_core::ffi::zeroed(); wsa_hints.ai_family = wsa::AF_UNSPEC; wsa_hints.ai_socktype = wsa::SOCK_STREAM; @@ -2721,7 +2566,7 @@ pub(crate) mod internal { &wsa_hints, &mut addrinfo, ); - after_result(req, addrinfo.cast(), err); + after_result(req, addrinfo, err); } #[cfg(not(windows))] // SAFETY: FFI getaddrinfo; `req.key.host` is the owned NUL-terminated host @@ -3165,12 +3010,12 @@ pub(crate) mod internal { Some(req) } - /// getaddrinfo() on the work pool; the result reaches every waiter through + /// getaddrinfo() on the work pool's threads for waits; the result reaches every waiter through /// the global cache, whichever thread asked. Also how a lookup whose /// per-thread mDNSResponder connection went away with its thread is /// finished (see `SharedConnection::close_for_terminate`). pub(super) fn run_on_work_pool(req: *mut Request) { - let _ = bun_threading::work_pool::WorkPool::go(SendPtr(req), |r: SendPtr| { + let _ = bun_threading::work_pool::WorkPool::go_wait(SendPtr(req), |r: SendPtr| { work_pool_callback(r.0) }); } @@ -3701,12 +3546,7 @@ type AnyPendingCache = type AddrPendingCache = HiveArray; type NameInfoPendingCache = HiveArray; -#[cfg(windows)] -type PollType = UvDnsPoll; -#[cfg(not(windows))] -type PollType = FilePoll; - -type PollsMap = ArrayHashMap; +type PollsMap = ArrayHashMap; // R-2 (host-fn re-entrancy): every JS-exposed method takes `&self`; per-field // interior mutability via `Cell` (Copy) / `JsCell` (non-Copy). c-ares @@ -3767,35 +3607,6 @@ impl Drop for Resolver { } } -#[cfg(windows)] -pub(crate) struct UvDnsPoll { - // BACKREF — stored mut because the poll callback hands it to - // `Resolver::deref`, which may write/free `*this`. - pub parent: *mut Resolver, - pub socket: c_ares::ares_socket_t, - pub poll: libuv::uv_poll_t, -} - -#[cfg(windows)] -impl UvDnsPoll { - fn new(parent: *mut Resolver, socket: c_ares::ares_socket_t) -> *mut Self { - bun_core::heap::into_raw(Box::new(Self { - parent, - socket, - poll: bun_core::ffi::zeroed(), - })) - } - - fn destroy(this: *mut Self) { - unsafe { drop(bun_core::heap::take(this)) }; - } - - fn from_poll(poll: *mut libuv::uv_poll_t) -> *mut Self { - // SAFETY: poll points to UvDnsPoll.poll - unsafe { bun_core::from_field_ptr!(UvDnsPoll, poll, poll) } - } -} - #[derive(Clone, Copy)] pub(crate) enum CacheHit { Inflight(*mut get_addr_info_request::PendingCacheKey), // BORROW_FIELD into resolver buffer @@ -4778,58 +4589,7 @@ impl Resolver { // ───────────── poll callbacks ───────────── - #[cfg(windows)] - pub(crate) extern "C" fn on_dns_poll_uv( - watcher: *mut libuv::uv_poll_t, - status: c_int, - events: c_int, - ) { - let poll = UvDnsPoll::from_poll(watcher); - // SAFETY: `poll` is the live `UvDnsPoll` recovered from libuv's `watcher` - // via `from_poll` (libuv guarantees the handle outlives this callback). - // `parent` is the heap-allocated Resolver back-ptr (set in - // `on_dns_socket_state`); it is kept alive across `Channel::process` by the - // `_guard` below. `channel` is non-null because c-ares - // must have been initialized for this poll callback to fire. - unsafe { - let parent: *mut Resolver = (*poll).parent; - let vm = (*parent).vm.get(); - let _exit = vm.enter_event_loop_scope(); - // SAFETY: `parent` is the live heap-allocated Resolver back-ptr. - let _guard = RefPtr::init_ref(parent); - // channel must be non-null here as c_ares must have been initialized if we're receiving callbacks - let channel = (*parent).channel.get().unwrap(); - if status < 0 { - // an error occurred. just pretend that the socket is both readable and writable. - // https://github.com/nodejs/node/blob/8a41d9b636be86350cd32847c3f89d327c4f6ff7/src/cares_wrap.cc#L93 - (*channel).process((*poll).socket, true, true); - } else { - (*channel).process( - (*poll).socket, - events & libuv::UV_READABLE != 0, - events & libuv::UV_WRITABLE != 0, - ); - } - - // See `on_dns_poll` for why this re-check follows `ares_process_fd`. - if !(*parent).any_requests_pending() { - (*parent).remove_timer(); - } - } - } - - #[cfg(windows)] - pub(crate) unsafe extern "C" fn on_close_uv(watcher: *mut libuv::uv_handle_t) { - // SAFETY: libuv invokes the close cb with the same handle pointer passed - // to `uv_close`, which was `&mut UvDnsPoll::poll` (a `uv_poll_t` whose - // header is `uv_handle_t`); `from_poll` recovers the containing struct. - let poll = UvDnsPoll::from_poll(watcher.cast()); - UvDnsPoll::destroy(poll); - } - - /// POSIX `FilePoll` callback (kqueue/epoll). Windows drives c-ares via - /// libuv (`on_dns_poll_uv`) instead, and the only caller - /// (`dispatch::__bun_run_file_poll`) is itself `#[cfg(not(windows))]`. + /// `FilePoll` callback for a c-ares socket. /// /// R-2: `&self` (no `noalias`). `Channel::process` (== `ares_process_fd`) /// synchronously fires c-ares completion callbacks which re-enter this @@ -4838,13 +4598,13 @@ impl Resolver { /// UnsafeCell-backed, LLVM cannot cache `ref_count` across the FFI call — /// the structural fix for the previously ASM-verified PROVEN_CACHED /// miscompile that needed `black_box` laundering under `&mut self`. - #[cfg(not(windows))] pub(crate) fn on_dns_poll(&self, poll: &mut FilePoll) { let vm = self.vm(); let _exit = vm.enter_event_loop_scope(); + let socket = poll.fd.native() as c_ares::ares_socket_t; let Some(channel) = self.channel.get() else { self.polls.with_mut(|p| { - let _ = p.remove(&poll.fd.native()); + let _ = p.remove(&socket); }); poll.deinit(); return; @@ -4852,11 +4612,18 @@ impl Resolver { let _guard = self.ref_guard(); + // A poll that failed or hung up reports no direction; give c-ares both, like Node: + // https://github.com/nodejs/node/blob/8a41d9b636be86350cd32847c3f89d327c4f6ff7/src/cares_wrap.cc#L93 + let failed = + poll.flags.contains(Async::PollFlag::Eof) || poll.flags.contains(Async::PollFlag::Hup); + let readable = poll.is_readable() || failed; + let writable = poll.is_writable() || failed; + // SAFETY: `channel` is the live c-ares channel owned by `self`; no `&mut` // to `*self` is held across this re-entrant call (all fields are // UnsafeCell-backed). unsafe { - (*channel).process(poll.fd.native(), poll.is_readable(), poll.is_writable()); + (*channel).process(socket, readable, writable); } // c-ares detaches a query only *after* its callback returns, so @@ -4872,139 +4639,63 @@ impl Resolver { readable: bool, writable: bool, ) { - #[cfg(windows)] - { - use libuv as uv; - if !readable && !writable { - // cleanup — `remove` is the ordered, value-returning variant. - if let Some(entry) = self.polls.with_mut(|p| p.remove(&fd)) { - // SAFETY: `entry` is the heap `UvDnsPoll` we inserted below; - // libuv takes ownership of the handle until `on_close_uv` - // frees the allocation. - unsafe { - uv::uv_close( - core::ptr::from_mut(&mut (*entry).poll).cast(), - Some(Self::on_close_uv), - ) - }; - } - return; + let ctx = js_event_loop_ctx(); + + if !readable && !writable { + // read == 0 and write == 0 this is c-ares's way of notifying us that + // the socket is now closed. We must free the data associated with + // socket. + if let Some(value) = self.polls.with_mut(|p| p.remove(&fd)) { + // SAFETY: `value` is the heap-allocated FilePoll for this fd. + unsafe { (*value).deinit_with_vm(ctx) }; } + return; + } - // Capture `self` as a raw backref for `UvDnsPoll::parent`. - let this_ptr: *mut Self = self.as_ctx_ptr(); - // SAFETY: single-JS-thread; the `&mut PollsMap` borrow does not span - // any re-entrant call (libuv `uv_poll_*` below do not call back into - // this resolver synchronously). - let polls = unsafe { self.polls.get_mut() }; - let poll_entry = bun_core::handle_oom(polls.get_or_put(fd)); - let poll: *mut UvDnsPoll = if poll_entry.found_existing { - *poll_entry.value_ptr - } else { - let new_poll = UvDnsPoll::new(this_ptr, fd); - // Publish into the map first so the `GetOrPutResult` borrow can - // end (NLL) before we may need to `swap_remove` on init failure. - *poll_entry.value_ptr = new_poll; - // SAFETY: `Loop::get()` is the live per-thread uws loop; - // `new_poll` is a fresh heap allocation with a zeroed `uv_poll_t`. - if unsafe { - uv::uv_poll_init_socket((*Loop::get()).uv_loop, &mut (*new_poll).poll, fd as _) - } < 0 - { - UvDnsPoll::destroy(new_poll); - let _ = polls.swap_remove(&fd); - return; - } - new_poll - }; + let owner = Async::Owner::new(Async::PollTag::DnsResolver, self.as_ctx_ptr().cast::<()>()); + // SAFETY: `event_loop_handle` is set once VM is initialized; live for VM lifetime. + let loop_ = unsafe { &mut *self.vm().event_loop_handle.unwrap() }; + // SAFETY: single-JS-thread; the `&mut PollsMap` borrow does not span + // any re-entrant call (`FilePoll::register` is a syscall wrapper). + let polls = unsafe { self.polls.get_mut() }; + let poll_entry = polls.get_or_put(fd).expect("unreachable"); - let uv_events = (if readable { uv::UV_READABLE } else { 0 }) - | (if writable { uv::UV_WRITABLE } else { 0 }); - // SAFETY: `poll` is the live entry just inserted/looked up above. - if unsafe { - uv::uv_poll_start(&mut (*poll).poll, uv_events, Some(Self::on_dns_poll_uv)) - } < 0 - { - let _ = polls.swap_remove(&fd); - // SAFETY: handle was successfully `uv_poll_init_socket`-ed, so - // `uv_close` is the required teardown path; `on_close_uv` frees - // the `UvDnsPoll` box. - unsafe { - uv::uv_close( - core::ptr::from_mut(&mut (*poll).poll).cast(), - Some(Self::on_close_uv), - ) - }; - } + if !poll_entry.found_existing { + // A c-ares socket is a SOCKET on Windows and an fd elsewhere. + #[cfg(windows)] + let socket_fd = sys::Fd::from_system(fd as *mut c_void); + #[cfg(not(windows))] + let socket_fd = sys::Fd::from_native(fd); + *poll_entry.value_ptr = FilePoll::init(ctx, socket_fd, Default::default(), owner); } - #[cfg(not(windows))] - { - let ctx = js_event_loop_ctx(); - - if !readable && !writable { - // read == 0 and write == 0 this is c-ares's way of notifying us that - // the socket is now closed. We must free the data associated with - // socket. - if let Some(value) = self.polls.with_mut(|p| p.remove(&fd)) { - // SAFETY: `value` is the heap-allocated FilePoll for this fd. - unsafe { (*value).deinit_with_vm(ctx) }; - } - return; - } - let owner = Async::Owner::new( - Async::posix_event_loop::poll_tag::DNS_RESOLVER, - self.as_ctx_ptr().cast::<()>(), - ); - // SAFETY: `event_loop_handle` is set once VM is initialized; live for VM lifetime. - let loop_ = unsafe { &mut *self.vm().event_loop_handle.unwrap() }; - // SAFETY: single-JS-thread; the `&mut PollsMap` borrow does not span - // any re-entrant call (`FilePoll::register` is a syscall wrapper). - let polls = unsafe { self.polls.get_mut() }; - let poll_entry = polls.get_or_put(fd).expect("unreachable"); - - if !poll_entry.found_existing { - *poll_entry.value_ptr = - FilePoll::init(ctx, sys::Fd::from_native(fd), Default::default(), owner); - } - - // SAFETY: `value_ptr` points at a slot just initialized above (or a - // previously-initialized live FilePoll hive slot); JS-thread exclusive. - let poll = unsafe { &mut **poll_entry.value_ptr }; - - // c-ares reports the full desired (readable, writable) set for this - // fd; sync the poll's registration to match. FilePoll now supports - // both directions on one poll (epoll: combined mask via CTL_MOD; - // kqueue: two filters on the same ident, both EV_DELETEd on - // unregister). - let have_readable = poll.flags.contains(Async::PollFlag::PollReadable); - let have_writable = poll.flags.contains(Async::PollFlag::PollWritable); - - if (have_readable && !readable) || (have_writable && !writable) { - // Dropping a direction. FilePoll has no per-direction - // unregister (epoll CTL_DEL removes both; a targeted kqueue - // EV_DELETE would need a new API), and leaving the unwanted - // direction armed would busy-loop on level-triggered writable - // once the socket connects. Full resync is the simplest - // correct path and c-ares DNS fds are short-lived. - let _ = poll.unregister(loop_, false); - if readable { - let _ = poll.register(loop_, Async::PollKind::Readable, false); - } - if writable { - let _ = poll.register(loop_, Async::PollKind::Writable, false); - } - } else { - // Only adding directions (or no change). register() issues a - // single CTL_MOD on epoll that preserves the other direction; - // on kqueue EV_ADD creates a separate (ident, filter) knote - // without disturbing the existing one. - if readable && !have_readable { - let _ = poll.register(loop_, Async::PollKind::Readable, false); - } - if writable && !have_writable { - let _ = poll.register(loop_, Async::PollKind::Writable, false); - } + // SAFETY: `value_ptr` points at a slot just initialized above (or a + // previously-initialized live FilePoll hive slot); JS-thread exclusive. + let poll = unsafe { &mut **poll_entry.value_ptr }; + + // c-ares reports the full desired (readable, writable) set for this + // fd; sync the poll's registration to match. + let have_readable = poll.flags.contains(Async::PollFlag::PollReadable); + let have_writable = poll.flags.contains(Async::PollFlag::PollWritable); + + if (have_readable && !readable) || (have_writable && !writable) { + // Dropping a direction. FilePoll has no per-direction unregister, + // and a stale writable registration busy-loops once the socket + // connects, so resync. + let _ = poll.unregister(loop_, false); + if readable { + let _ = poll.register(loop_, Async::PollKind::Readable, false); + } + if writable { + let _ = poll.register(loop_, Async::PollKind::Writable, false); + } + } else { + // Only adding directions (or no change). + if readable && !have_readable { + let _ = poll.register(loop_, Async::PollKind::Readable, false); + } + if writable && !have_writable { + let _ = poll.register(loop_, Async::PollKind::Writable, false); } } } @@ -5314,26 +5005,13 @@ impl Resolver { GetAddrInfoBackend::CAres => { self.c_ares_lookup_with_normalized_name(&query, global_this, context)? } - GetAddrInfoBackend::Libc => { - #[cfg(windows)] - { - lib_uv_backend::lookup(self, query, global_this, context)? - } - #[cfg(not(windows))] - { - lib_c::lookup(self, &query, global_this, context) - } - } + GetAddrInfoBackend::Libc => lib_c::lookup(self, &query, global_this, context), GetAddrInfoBackend::System => { #[cfg(target_os = "macos")] { dns_sd::lookup(self, &query, global_this, context) } - #[cfg(windows)] - { - lib_uv_backend::lookup(self, query, global_this, context)? - } - #[cfg(all(not(target_os = "macos"), not(windows)))] + #[cfg(not(target_os = "macos"))] { lib_c::lookup(self, &query, global_this, context) } diff --git a/src/runtime/dns_jsc/mod.rs b/src/runtime/dns_jsc/mod.rs index f4683c938c12..04d3324bbe87 100644 --- a/src/runtime/dns_jsc/mod.rs +++ b/src/runtime/dns_jsc/mod.rs @@ -2,7 +2,7 @@ //! //! The full body — `Resolver` with the //! c-ares channel, all `resolve*`/`reverse`/`getServers`/`setServers` host -//! functions, libinfo/libuv/system getaddrinfo backends, and the process-wide +//! functions, libinfo/libc/system getaddrinfo backends, and the process-wide //! `internal` cache used by the usockets connect path — lives in `dns.rs` //! (mounted here as `dns_body`). This module is the public surface: it //! re-exports the real types and methods so callers (`dispatch.rs`, @@ -10,8 +10,6 @@ #[path = "dns.rs"] mod dns_body; -#[cfg(windows)] -pub(crate) use dns_body::lib_uv_backend::LibuvCompleteHolder; pub(crate) use dns_body::netc; #[path = "cares_jsc.rs"] diff --git a/src/runtime/image/Image.rs b/src/runtime/image/Image.rs index b31492c3c963..be571ae91ac1 100644 --- a/src/runtime/image/Image.rs +++ b/src/runtime/image/Image.rs @@ -1235,7 +1235,7 @@ impl Image { // ───────────────────────────── worker task ────────────────────────────────── /// `.blob` source: ask the Blob for its bytes via the store-agnostic -/// `Blob.readBytesToHandler` (file → ReadFile/ReadFileUV, S3 → S3.download, +/// `Blob.readBytesToHandler` (file → ReadFile, S3 → S3.download, /// memory → dupe), receive the owned `[]u8` directly — never wrapped in a /// JSValue — swap it into `image.source = .owned`, and re-enter `schedule()`. /// Promise-of-promise flattens, so the caller sees one `await` for @@ -1557,15 +1557,8 @@ impl PipelineTask { // • st_size cap → file-based decompression-bomb fails up // front with a clear error instead of materialising a // multi-GB encoded buffer before `maxPixels` even runs. - // O_NONBLOCK so the open itself can't block on a FIFO. POSIX-only: - // on Windows it omits FILE_SYNCHRONOUS_IO_NONALERT (overlapped - // handle) and the subsequent sync read fails EINVAL. Windows has - // no open-blocking FIFOs in the same sense; the !S_ISREG check - // below still rejects pipes/devices. - #[cfg(unix)] + // O_NONBLOCK so the open itself can't block on a FIFO. let oflags = sys::O::RDONLY | sys::O::NONBLOCK; - #[cfg(not(unix))] - let oflags = sys::O::RDONLY; let file = match sys::File::openat(sys::Fd::cwd(), p, oflags, 0) { sys::Result::Ok(f) => f, sys::Result::Err(e) => { diff --git a/src/runtime/image/backend_wic.rs b/src/runtime/image/backend_wic.rs index 6ad5736f1215..73a9ef48ecc7 100644 --- a/src/runtime/image/backend_wic.rs +++ b/src/runtime/image/backend_wic.rs @@ -339,16 +339,7 @@ pub(crate) fn encode( // parent interface's slots, then the interface's own. Only slots we call are // typed; the rest are `*const c_void` placeholders so offsets stay correct. -type HRESULT = i32; - -#[repr(C)] -#[derive(Clone, Copy, PartialEq, Eq)] -struct GUID { - d1: u32, - d2: u16, - d3: u16, - d4: [u8; 8], -} +use windows::{GUID, HRESULT}; #[repr(C)] struct IUnknownVTable { @@ -766,46 +757,46 @@ struct IWICBitmapFrameEncodeVTable { // ───────────────────────────── GUIDs ──────────────────────────────────────── const CLSID_WICImagingFactory: GUID = GUID { - d1: 0xcacaf262, - d2: 0x9370, - d3: 0x4615, - d4: [0xa1, 0x3b, 0x9f, 0x55, 0x39, 0xda, 0x4c, 0x0a], + Data1: 0xcacaf262, + Data2: 0x9370, + Data3: 0x4615, + Data4: [0xa1, 0x3b, 0x9f, 0x55, 0x39, 0xda, 0x4c, 0x0a], }; const IID_IWICImagingFactory: GUID = GUID { - d1: 0xec5ec8a9, - d2: 0xc395, - d3: 0x4314, - d4: [0x9c, 0x77, 0x54, 0xd7, 0xa9, 0x35, 0xff, 0x70], + Data1: 0xec5ec8a9, + Data2: 0xc395, + Data3: 0x4314, + Data4: [0x9c, 0x77, 0x54, 0xd7, 0xa9, 0x35, 0xff, 0x70], }; const GUID_WICPixelFormat32bppRGBA: GUID = GUID { - d1: 0xf5c7ad2d, - d2: 0x6a8d, - d3: 0x43dd, - d4: [0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9], + Data1: 0xf5c7ad2d, + Data2: 0x6a8d, + Data3: 0x43dd, + Data4: [0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9], }; const GUID_ContainerFormatJpeg: GUID = GUID { - d1: 0x19e4a5aa, - d2: 0x5662, - d3: 0x4fc5, - d4: [0xa0, 0xc0, 0x17, 0x58, 0x02, 0x8e, 0x10, 0x57], + Data1: 0x19e4a5aa, + Data2: 0x5662, + Data3: 0x4fc5, + Data4: [0xa0, 0xc0, 0x17, 0x58, 0x02, 0x8e, 0x10, 0x57], }; const GUID_ContainerFormatPng: GUID = GUID { - d1: 0x1b7cfaf4, - d2: 0x713f, - d3: 0x473c, - d4: [0xbb, 0xcd, 0x61, 0x37, 0x42, 0x5f, 0xae, 0xaf], + Data1: 0x1b7cfaf4, + Data2: 0x713f, + Data3: 0x473c, + Data4: [0xbb, 0xcd, 0x61, 0x37, 0x42, 0x5f, 0xae, 0xaf], }; const GUID_ContainerFormatWebp: GUID = GUID { - d1: 0xe094b0e2, - d2: 0x67f2, - d3: 0x45b3, - d4: [0xb0, 0xea, 0x11, 0x53, 0x37, 0xca, 0x7c, 0xf3], + Data1: 0xe094b0e2, + Data2: 0x67f2, + Data3: 0x45b3, + Data4: [0xb0, 0xea, 0x11, 0x53, 0x37, 0xca, 0x7c, 0xf3], }; const GUID_ContainerFormatHeif: GUID = GUID { - d1: 0xe1e62521, - d2: 0x6787, - d3: 0x405b, - d4: [0xa3, 0x39, 0x50, 0x07, 0x15, 0xb5, 0x76, 0x3f], + Data1: 0xe1e62521, + Data2: 0x6787, + Data3: 0x405b, + Data4: [0xa3, 0x39, 0x50, 0x07, 0x15, 0xb5, 0x76, 0x3f], }; fn container_guid(f: codecs::Format) -> Option<*const GUID> { diff --git a/src/runtime/ipc.rs b/src/runtime/ipc.rs index 9f4ac201827f..564b0d8261d1 100644 --- a/src/runtime/ipc.rs +++ b/src/runtime/ipc.rs @@ -11,19 +11,13 @@ use bun_core::{Output, handle_oom}; use bun_core::{String as BunString, strings}; use bun_io::KeepAlive; use bun_io::StreamBuffer; +#[cfg(windows)] +use bun_io::windows::{Pipe, PipeOrigin, ReadEvent, Refusal, ipc_frame}; use bun_jsc as jsc; use bun_jsc::js_value::Protected; -#[cfg(windows)] -use bun_jsc::virtual_machine::VirtualMachine; use bun_jsc::{JSGlobalObject, JSValue, JsError, JsResult, SerializedFlags, StringJsc as _, Task}; use bun_sys::Fd; use bun_sys::FdExt; -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; -#[cfg(windows)] -use bun_sys::windows::libuv::{UvHandle as _, UvStream as _}; // `bun.cpp.*` — generated C++ dispatch shims for IPC handle (de)serialization // (`IPCSerialize` / `IPCParse`) are declared once in `bun_jsc::cpp` and called @@ -645,6 +639,7 @@ pub(crate) fn get_nack_packet(mode: Mode) -> &'static [u8] { // `bun_uws::SocketHandler` is an alias for `NewSocketHandler` // (uws_sys/socket.rs); `` is the non-SSL handler. +#[cfg(not(windows))] pub(crate) type Socket = bun_uws::SocketHandler; pub(crate) struct Handle { @@ -841,33 +836,23 @@ fn close_sent_handle_fn(global: &JSGlobalObject) -> JSValue { // SendHandle.deinit: all fields Drop; no explicit impl needed. +/// A Windows channel is a named pipe that speaks libuv's IPC framing, which is +/// what a Node.js process on the other end reads and writes. #[cfg(windows)] -pub(crate) struct WindowsWrite { - pub(crate) write_req: uv::uv_write_t, - pub(crate) write_buffer: uv::uv_buf_t, - pub(crate) write_slice: Box<[u8]>, - pub(crate) owner: Option<*mut SendQueue>, -} - -#[cfg(windows)] -impl WindowsWrite { - pub(crate) fn destroy(this: *mut WindowsWrite) { - // SAFETY: `this` was produced by heap::alloc in SendQueue::write; - // libuv guarantees the write callback fires exactly once. - let _ = unsafe { bun_core::heap::take(this) }; - // write_slice freed by Box<[u8]> Drop. - } +#[derive(Default)] +struct IncomingFrames { + decoder: ipc_frame::Decoder, + /// Scratch for one read's frames; kept for its capacity. + events: Vec, } #[cfg(windows)] -#[derive(Default)] -pub(crate) struct WindowsState { - /// Non-owning raw pointer. The allocation - /// is `heap::alloc`'d in `write` and freed exactly once by - /// `windows_on_write_complete` via `WindowsWrite::destroy`. Nulling this - /// field never frees. - pub(crate) windows_write: Option<*mut WindowsWrite>, - pub(crate) try_close_after_write: bool, +enum FrameEvent { + /// A range of the read chunk that is payload. + Data(core::ops::Range), + /// A socket a Node.js peer handed over with the frame, materialized in + /// this process. `None` when the transfer block could not be imported. + Socket(Option), } #[derive(Copy, Clone, Eq, PartialEq)] @@ -920,9 +905,15 @@ pub(crate) struct SendQueue { pub(crate) pending_after_close: Cell, pub(crate) write_in_progress: Cell, pub close_event_sent: Cell, + /// This side is done with the channel (disconnect(), a broken stream) and + /// the close itself comes later: what the peer sends meanwhile is dropped. + input_stopped: Cell, + /// Input that could not be decoded closed the channel: what follows it in + /// the same read is not delivered. + input_failed: Cell, #[cfg(windows)] - pub windows: JsCell, + incoming_frames: JsCell, } #[derive(Copy, Clone)] @@ -991,7 +982,7 @@ pub(crate) enum SendQueueOwnerKind { } #[cfg(windows)] -pub(crate) type SocketType = *mut uv::Pipe; +pub(crate) type SocketType = Pipe; #[cfg(not(windows))] pub(crate) type SocketType = Socket; @@ -1045,8 +1036,10 @@ impl SendQueue { pending_after_close: Cell::new(false), write_in_progress: Cell::new(false), close_event_sent: Cell::new(false), + input_stopped: Cell::new(false), + input_failed: Cell::new(false), #[cfg(windows)] - windows: JsCell::new(WindowsState::default()), + incoming_frames: JsCell::new(IncomingFrames::default()), }); this.root.set(Some(this.as_non_null())); this @@ -1068,14 +1061,37 @@ impl SendQueue { } pub(crate) fn is_connected(&self) -> bool { - #[cfg(windows)] - if self.windows.get().try_close_after_write { - return false; - } self.socket_is_open() && !self.pending_close.get() && !self.close_after_flush.get() } - fn close_socket(&self, reason: CloseReason, from: CloseFrom) { + /// Whether more is read from the peer. What one read already brought in + /// is delivered to its end, as Node does: `disconnect()` from a message + /// handler stops the reads that follow, not the messages in hand. + #[cfg(not(windows))] + #[inline] + fn accepts_input(&self) -> bool { + self.socket_is_open() && !self.input_stopped.get() + } + + /// Take nothing more from the peer. The socket stays open for a close that + /// comes later (the deferred task, the end of the write in flight). + fn stop_input(&self) { + self.input_stopped.set(true); + // What arrives from here on stays in the pipe. + #[cfg(windows)] + self.socket.with_mut(|socket| { + if let SocketUnion::Open(pipe) = socket { + pipe.read_stop(); + } + }); + } + + fn close_socket( + &self, + // A pipe has no close code. + #[cfg_attr(windows, allow(unused_variables))] reason: CloseReason, + from: CloseFrom, + ) { log!( "SendQueue#closeSocket {}", match from { @@ -1083,63 +1099,33 @@ impl SendQueue { CloseFrom::Deinit => "deinit", } ); - let open = match *self.socket.get() { - SocketUnion::Open(s) => Some(s), - _ => None, - }; - match open { - Some(s) => { - #[cfg(windows)] - { - let pipe: *mut uv::Pipe = s; - // SAFETY: pipe is a live uv_pipe_t owned until _windowsOnClosed fires. - let stream: *mut uv::uv_stream_t = unsafe { (*pipe).as_stream() }; - unsafe { (*stream).read_stop() }; - - let write_pending = self.windows.get().windows_write.is_some(); - if write_pending && from != CloseFrom::Deinit { - log!("SendQueue#closeSocket -> mark ready for close"); - // currently writing; wait for the write to complete - self.windows.with_mut(|w| w.try_close_after_write = true); - } else { - log!("SendQueue#closeSocket -> close now"); - self.windows_close(from != CloseFrom::Deinit); - } - } - #[cfg(not(windows))] - { - s.close(match reason { - CloseReason::Normal => bun_uws::CloseCode::Normal, - CloseReason::Failure => bun_uws::CloseCode::Failure, - }); - self.socket_closed_notify(from != CloseFrom::Deinit); - } - } - None => { - self.socket_closed_notify(from != CloseFrom::Deinit); - } + // Closing the pipe cancels a write the kernel still holds, and the + // peer would lose a message this side already reported as sent. + #[cfg(windows)] + if self.socket_is_open() && self.write_in_progress.get() && from != CloseFrom::Deinit { + log!("SendQueue#closeSocket -> close after the write"); + self.pending_close.set(true); + self.stop_input(); + return; + } + #[cfg(not(windows))] + if let SocketUnion::Open(s) = *self.socket.get() { + s.close(match reason { + CloseReason::Normal => bun_uws::CloseCode::Normal, + CloseReason::Failure => bun_uws::CloseCode::Failure, + }); } - let _ = reason; // suppress unused on windows + // On Windows, replacing the `Open` value drops the pipe, which closes it. + self.socket_closed_notify(from != CloseFrom::Deinit); } + #[cfg(not(windows))] fn socket_closed(&self) { self.socket_closed_notify(true); } fn socket_closed_notify(&self, notify: bool) { log!("SendQueue#_socketClosed"); - #[cfg(windows)] - { - let windows_write = self.windows.get().windows_write; - if let Some(windows_write) = windows_write { - // SAFETY: `windows_write` was leaked via `heap::alloc` in - // `write`; libuv still holds it and will free it in - // `windows_on_write_complete`. We only clear the backref so - // the callback doesn't touch a dead `SendQueue`. - unsafe { (*windows_write).owner = None }; - } - self.windows.with_mut(|w| w.windows_write = None); - } self.keep_alive.with_mut(|k| k.disable()); let was_open = self.socket_is_open(); self.socket.set(SocketUnion::Closed); @@ -1211,38 +1197,6 @@ impl SendQueue { unsafe { ::deref(this) }; } - /// `uv::open_handles` closes the channel's pipe through here at a thread - /// teardown: close now (pending writes finish ECANCELED) and let the owner - /// observe the disconnect, rather than waiting for writes as a user close does. - #[cfg(windows)] - unsafe fn stop_for_vm_teardown(this: *mut c_void) { - // SAFETY: recorded at configure time by this live SendQueue; the pipe - // leaves the list when `windows_close` issues its uv_close. - let this = unsafe { &*this.cast::() }; - this.windows_close(true); - } - - #[cfg(windows)] - fn windows_close(&self, notify: bool) { - log!("SendQueue#_windowsClose"); - let SocketUnion::Open(pipe) = *self.socket.get() else { - return; - }; - // SAFETY: pipe is live until the close cb fires. - unsafe { - (*pipe).data = pipe.cast(); - (*pipe).close(Self::windows_on_closed); - } - self.socket_closed_notify(notify); - } - - #[cfg(windows)] - extern "C" fn windows_on_closed(windows: *mut uv::Pipe) { - log!("SendQueue#_windowsOnClosed"); - // SAFETY: pipe was heap-allocated in windowsConfigureClient / created by caller. - let _ = unsafe { bun_core::heap::take(windows) }; - } - pub(crate) fn close_socket_next_tick(&self, next_tick: bool) { log!("SendQueue#closeSocketNextTick"); if !self.socket_is_open() { @@ -1250,7 +1204,9 @@ impl SendQueue { return; } // Peer-gone and exit paths land here too: a postponed disconnect never outranks them. - self.close_after_flush.set(false); + if self.close_after_flush.replace(false) { + self.stop_input(); + } if self.pending_close.get() { return; // close already requested } @@ -1271,6 +1227,7 @@ impl SendQueue { self.close_after_flush.set(true); return; } + self.stop_input(); self.close_socket_next_tick(true); } @@ -1517,7 +1474,7 @@ impl SendQueue { debug_assert!(!self.write_in_progress.get()); self.write_in_progress.set(true); self.write(fd); - // the write is queued. this._onWriteComplete() will be called when the write completes. + // the write is queued. `on_write_complete` will be called when the write completes. self.update_ref(global); } } @@ -1561,7 +1518,7 @@ impl SendQueue { // shift the queue and try to send the next item immediately. Done::Completed(queue.remove(0)) } - } else if n > 0 && n < i32::try_from(first.data.list.len()).expect("int cast") { + } else if n > 0 && (n as usize) < to_send_len { // the item was partially sent; update the cursor and wait for writable to send the rest // (if we tried to send a handle, a partial write means the handle wasn't sent yet.) first.data.cursor += usize::try_from(n).expect("int cast"); @@ -1671,91 +1628,55 @@ impl SendQueue { } } - fn get_socket(&self) -> Option { - match *self.socket.get() { - SocketUnion::Open(s) => Some(s), - _ => None, - } - } - #[cfg(windows)] pub(crate) fn ipc_peer_pid(&self) -> u32 { - match *self.socket.get() { - // SAFETY: `p` is a live uv_pipe_t owned until _windowsOnClosed. - SocketUnion::Open(p) => unsafe { (*p).ipc_remote_pid() as u32 }, + match self.socket.get() { + SocketUnion::Open(pipe) => pipe.peer_pid().unwrap_or(0), _ => 0, } } - /// starts a write request. on posix, this always calls _onWriteComplete immediately. on windows, it may - /// call _onWriteComplete later. + /// starts a write request. on posix, this always calls `on_write_complete` immediately. on windows, it + /// calls `on_write_complete` later, when the pipe has taken the bytes. /// /// The outbound bytes are read from `queue[0]` *inside* this method. fn write(&self, fd: Option) { - let Some(socket) = self.get_socket() else { - self.on_write_complete(-1); - return; - }; #[cfg(windows)] { + // A socket travels inside the message on Windows (`$winSocketInfo`). let _ = fd; - let pipe: *mut uv::Pipe = socket; - - let write_req_slice: Box<[u8]> = self.queue.with_mut(|queue| { + // One frame per write: header and payload must reach the pipe together. + let frame: Vec = self.queue.with_mut(|queue| { let first = &queue[0]; let data = &first.data.list[first.data.cursor..]; log!("SendQueue#write len {}", data.len()); - let write_len = data.len().min(i32::MAX as usize); - Box::from(&data[0..write_len]) + let data = &data[..data.len().min(i32::MAX as usize)]; + let mut frame = Vec::with_capacity(ipc_frame::HEADER_LEN + data.len()); + frame.extend_from_slice(&ipc_frame::data_header(data.len() as u32)); + frame.extend_from_slice(data); + frame }); - - // create write request - let mut write_req = Box::new(WindowsWrite { - owner: Some(self.root_ptr()), - write_slice: write_req_slice, - write_req: bun_core::ffi::zeroed(), - write_buffer: uv::uv_buf_t::init(b""), // re-init below after slice address is stable + let submitted = self.socket.with_mut(|socket| match socket { + SocketUnion::Open(pipe) => pipe + .write_owned( + frame, + Refusal::Returned, + self.root_ptr(), + Self::on_pipe_write, + ) + .is_ok(), + _ => false, }); - write_req.write_buffer = uv::uv_buf_t::init(&write_req.write_slice); - // Hand ownership to libuv; reclaimed exactly once by - // `windows_on_write_complete` via `WindowsWrite::destroy`. - let write_req: *mut WindowsWrite = bun_core::heap::into_raw(write_req); - debug_assert!(self.windows.get().windows_write.is_none()); - self.windows.with_mut(|w| w.windows_write = Some(write_req)); - - // SAFETY: pipe is live (socket == .open). - unsafe { (*pipe).ref_() }; // ref on write - // SAFETY: `write_req` is a freshly-leaked Box; libuv owns it until - // the write callback fires. - let result = unsafe { - (*write_req).write_req.write( - (*pipe).as_stream(), - &(*write_req).write_buffer, - write_req, - // `write()` stores a *Rust* fn pointer (`fn(*mut T, ReturnCode)`) - // and thunks it through libuv. The callback receives the - // raw `*mut WindowsWrite` (NOT `&mut`) because - // `windows_on_write_complete` deallocates the request via - // `WindowsWrite::destroy`. - |req: *mut WindowsWrite, rc| SendQueue::windows_on_write_complete(req, rc), - ) - }; - if result.to_error(bun_sys::Tag::write).is_some() { - WindowsWrite::destroy(write_req); - self.windows.with_mut(|w| w.windows_write = None); - // SAFETY: pipe is live (socket == .open); pairs with the - // `(*pipe).ref_()` above. - unsafe { (*pipe).unref() }; + if !submitted { self.on_write_complete(-1); - if self.windows.get().try_close_after_write { - self.close_socket(CloseReason::Normal, CloseFrom::User); - } - return; } - // write request is queued. it will call _onWriteComplete when it completes. } #[cfg(not(windows))] { + let SocketUnion::Open(socket) = *self.socket.get() else { + self.on_write_complete(-1); + return; + }; let n: i32 = self.queue.with_mut(|queue| { let first = &queue[0]; let data = &first.data.list[first.data.cursor..]; @@ -1770,176 +1691,148 @@ impl SendQueue { } } + /// # Safety + /// `this` is the root pointer given to `write_owned`. The pipe calls back + /// only while it is open, and the queue owns the pipe. #[cfg(windows)] - fn windows_on_write_complete(write_req: *mut WindowsWrite, status: uv::ReturnCode) { - log!("SendQueue#_windowsOnWriteComplete"); - // SAFETY: write_req was passed to uv_write as the data ptr; libuv hands it back here. - // Explicit `&` so the slice `.len()` autoref doesn't trigger - // `dangerous_implicit_autorefs` on the raw-ptr place. - let write_len = unsafe { (&(*write_req).write_slice).len() }; - let this: *mut SendQueue = 'blk: { - let owner = unsafe { (*write_req).owner }; - WindowsWrite::destroy(write_req); - match owner { - Some(o) => break 'blk o, - None => return, // orelse case if disconnected before the write completes - } - }; - let vm = VirtualMachine::get(); - let _scope = vm.enter_event_loop_scope(); - - // SAFETY: owner is a BACKREF into the live SendQueue (cleared in - // socket_closed if not); every method takes `&self`. + unsafe fn on_pipe_write(this: *mut SendQueue, result: bun_sys::Result) { + log!("SendQueue#onPipeWrite"); + // SAFETY: caller contract. let this = unsafe { &*this }; - this.windows.with_mut(|w| w.windows_write = None); - if let Some(socket) = this.get_socket() { - // SAFETY: `socket` is the live `uv_pipe_t` place (matches the - // `(*pipe).ref_()` site in `write`). - unsafe { (*socket).unref() }; // write complete; unref - } - let n = if status.to_error(bun_sys::Tag::write).is_some() { - -1 - } else { - i32::try_from(write_len).expect("int cast") + let global_this = this.get_global_this(); + let _scope = global_this.bun_vm().enter_event_loop_scope(); + + let n = match result { + Ok(written) => { + i32::try_from(written.saturating_sub(ipc_frame::HEADER_LEN)).expect("int cast") + } + Err(_) => -1, }; this.on_write_complete(n); - if this.windows.get().try_close_after_write { + if this.pending_close.replace(false) { + // The close that waited for this write. this.close_socket(CloseReason::Normal, CloseFrom::User); + } else if !this.write_in_progress.get() + && this + .queue + .get() + .first() + .is_some_and(|first| first.data.cursor != 0) + { + // The rest of a message too large for one frame. + this.continue_send(&global_this, ContinueSendReason::OnWritable); } - // The event-loop exit is handled by `_scope` drop. } + fn get_global_this(&self) -> bun_jsc::GlobalRef { let owner = self.owner_ref().expect("SendQueue used after detach"); bun_jsc::GlobalRef::from(JSGlobalObject::opaque_ref(owner.global_this())) } + /// Make `fd` this queue's channel and start reading from it. `origin` is + /// `Created` for the overlapped end spawn made for a child and + /// `InheritedUnshared` for the end this process inherited as + /// `NODE_CHANNEL_FD`. The channel closes `fd` either way. On `Err` the + /// caller still owns `fd`. + /// /// # Safety - /// `this` must point at a live `SendQueue` and must derive from the - /// allocation's root raw pointer (SharedReadWrite provenance), NOT from a - /// `&mut` reborrow: the pointer is stashed in `uv_handle_t.data` for the - /// pipe's lifetime. Mirrors [`windows_configure_client`]. + /// `this` must be the root pointer of a live `SendQueue` + /// ([`as_ctx_ptr`](Self::as_ctx_ptr)): the pipe keeps it for its callbacks. #[cfg(windows)] - pub(crate) unsafe fn windows_configure_server( + pub(crate) unsafe fn open_pipe( this: *mut Self, - ipc_pipe: *mut uv::Pipe, + loop_: *mut bun_io::Loop, + fd: Fd, + origin: PipeOrigin, ) -> bun_sys::Result<()> { - log!("configureServer"); - // SAFETY: ipc_pipe is a live uv_pipe_t handed in by the caller; `this` - // is the root-raw SendQueue pointer per the fn safety contract. - unsafe { - (*ipc_pipe).data = this.cast(); - (*ipc_pipe).unref(); - } - // SAFETY: caller contract — `this` is a live SendQueue. - let self_ = unsafe { &*this }; - self_.socket.set(SocketUnion::Open(ipc_pipe)); - uv::open_handles::set_owner( - ipc_pipe.cast(), - this.cast(), - Some(Self::stop_for_vm_teardown), - ); - // SAFETY: pipe is the live uv handle just stored in the socket cell. - unsafe { (*ipc_pipe).data = this.cast() }; - - // SAFETY: pipe is the live uv handle just stored in the socket cell. - let stream: *mut uv::uv_stream_t = unsafe { (*ipc_pipe).as_stream() }; - - // SAFETY: stream points to the live uv handle; `this` is the root-raw - // context pointer (see fn safety contract) so storing it in - // `handle.data` is sound for the handle's lifetime. Routes through the - // `StreamReader for SendQueue` impl below (wraps the - // `IPCHandlers::WindowsNamedPipe` callbacks). - let read_start_result = - unsafe { (*stream).read_start_ctx::(this) }.to_error(bun_sys::Tag::listen); - if let Some(err) = read_start_result { - self_.close_socket(CloseReason::Failure, CloseFrom::User); + log!("SendQueue#openPipe"); + let mut pipe = Pipe::open(loop_, fd, origin, true)?; + // The loop is kept alive by `keep_alive`, not by the pending read. + pipe.unref(); + if let Err(err) = pipe.read_start(this, Self::on_pipe_read) { + pipe.disown(); return Err(err); } - bun_sys::Result::Ok(()) + // SAFETY: caller contract. + unsafe { &*this }.socket.set(SocketUnion::Open(pipe)); + Ok(()) } /// # Safety - /// `this` must point at a live `SendQueue` and must derive from the - /// allocation's root raw pointer (SharedReadWrite provenance), NOT from a - /// `&mut` reborrow: the pointer is stashed in `uv_handle_t.data` for the - /// pipe's lifetime. + /// `this` is the root pointer given to `read_start`; the pipe only calls + /// back while the queue, which owns it, has it open. #[cfg(windows)] - pub(crate) unsafe fn windows_configure_client( - this: *mut Self, - pipe_fd: Fd, - ) -> Result<(), bun_jsc::CrateError> { - log!("configureClient"); - let ipc_pipe: *mut uv::Pipe = - bun_core::heap::into_raw(Box::new(bun_core::ffi::zeroed::())); - // SAFETY: ipc_pipe just allocated above. - if let Some(err) = - unsafe { (*ipc_pipe).init(uv::Loop::get(), true) }.to_error(bun_sys::Tag::pipe) - { - // SAFETY: ipc_pipe was heap-allocated above and init failed before libuv took ownership. - let _ = unsafe { bun_core::heap::take(ipc_pipe) }; - return Err(err.into()); - } - // SAFETY: ipc_pipe is a live initialized uv_pipe_t. - if let Some(err) = unsafe { (*ipc_pipe).open(pipe_fd.uv()) }.to_error(bun_sys::Tag::open) { - // SAFETY: ipc_pipe is a live initialized uv_pipe_t; close_and_destroy frees the Box. - unsafe { uv::Pipe::close_and_destroy(ipc_pipe) }; - return Err(err.into()); - } - // SAFETY: ipc_pipe is a live initialized uv_pipe_t. - unsafe { (*ipc_pipe).unref() }; - // SAFETY: caller contract — `this` is a live SendQueue. - let self_ = unsafe { &*this }; - self_.socket.set(SocketUnion::Open(ipc_pipe)); - uv::open_handles::set_owner( - ipc_pipe.cast(), - this.cast(), - Some(Self::stop_for_vm_teardown), - ); - - // SAFETY: ipc_pipe is the live uv handle just stored in the socket cell. - let stream = unsafe { (*ipc_pipe).as_stream() }; + unsafe fn on_pipe_read(this: *mut SendQueue, event: ReadEvent<'_>) { + // SAFETY: caller contract. + let this = unsafe { &*this }; + let chunk = match event { + ReadEvent::Data(chunk) => chunk, + ReadEvent::Eof | ReadEvent::EndOfWrite => { + log!("SendQueue#onPipeRead EOF"); + return this.close_socket_next_tick(true); + } + ReadEvent::Err(err) => { + log!("SendQueue#onPipeRead {:?}", err.get_errno()); + return this.close_socket_next_tick(true); + } + }; + log!("SendQueue#onPipeRead {}", chunk.len()); + + // Split the chunk into frames first and deliver afterwards: delivery + // runs JS, which must not find the decoder borrowed. + let (mut events, valid) = this.incoming_frames.with_mut(|frames| { + let mut events = core::mem::take(&mut frames.events); + let base = chunk.as_ptr() as usize; + let valid = frames + .decoder + .feed(chunk.as_slice(), |event| match event { + ipc_frame::Event::Data(bytes) => { + let start = bytes.as_ptr() as usize - base; + events.push(FrameEvent::Data(start..start + bytes.len())); + } + ipc_frame::Event::SocketTransfer { info } => { + events.push(FrameEvent::Socket(import_transferred_socket(info))); + } + }) + .is_ok(); + (events, valid) + }); - // SAFETY: stream points to the live uv handle; `this` is the root-raw - // context pointer (see fn safety contract) so storing it in - // `handle.data` is sound for the handle's lifetime. - if let Some(err) = - unsafe { (*stream).read_start_ctx::(this) }.to_error(bun_sys::Tag::listen) - { - self_.close_socket(CloseReason::Failure, CloseFrom::User); - return Err(err.into()); + let global_this = this.get_global_this(); + let _scope = global_this.bun_vm().enter_event_loop_scope(); + for event in events.drain(..) { + // Node is handed one frame at a time, so a handler that + // disconnects has seen the last one. + if this.input_failed.get() || this.input_stopped.get() { + // Nothing more is delivered: a socket that came with the rest + // has no taker. + if let FrameEvent::Socket(Some(unclaimed)) = event { + let _ = unclaimed.close_allowing_standard_io(None); + } + continue; + } + match event { + FrameEvent::Data(range) => on_data2(this, &chunk[range]), + // As an fd received over a POSIX channel: the `NODE_HANDLE` + // message that follows in the same frame takes it. + FrameEvent::Socket(fd) => { + if let Some(unclaimed) = this.incoming_fd.replace(fd) { + let _ = unclaimed.close_allowing_standard_io(None); + } + } + } + } + this.incoming_frames.with_mut(|frames| { + if frames.events.capacity() < events.capacity() { + frames.events = events; + } + }); + if !valid { + // The stream cannot be resynchronized. Node.js treats a channel + // that breaks the framing as disconnected. + this.close_socket(CloseReason::Failure, CloseFrom::User); } - Ok(()) - } -} - -/// Adapter from `UvStream::read_start_ctx` to the `IPCHandlers::WindowsNamedPipe` -/// callbacks. The three fns are baked -/// into the trait impl so the `extern "C"` trampoline is monomorphised over -/// `SendQueue` with zero per-handle storage. -#[cfg(windows)] -impl uv::StreamReader for SendQueue { - #[inline] - fn on_read_alloc(this: &mut Self, suggested_size: usize) -> &mut [u8] { - IPCHandlers::WindowsNamedPipe::on_read_alloc(this, suggested_size) - } - #[inline] - fn on_read_error(this: &mut Self, err: core::ffi::c_int) { - // Map the raw libuv errno - // to `bun_sys::E`, defaulting to CANCELED for unmapped codes. - let e = bun_sys::windows::translate_uv_error_to_e(err); - IPCHandlers::WindowsNamedPipe::on_read_error(this, e); - } - #[inline] - unsafe fn on_read(this: *mut Self, data: &[u8]) { - // `data` points into `(*this).incoming` (it was returned from - // `on_read_alloc`); the callee re-derives the written tail from - // `incoming` itself, so only the length is forwarded and only a shared - // view of `*this` is formed. - let nread = data.len(); - // SAFETY: `this` is the live `SendQueue` stashed in `handle.data` by - // `read_start_ctx`; a shared reborrow only, and `data` is not used after. - IPCHandlers::WindowsNamedPipe::on_read(unsafe { &*this }, nread); } } @@ -2023,7 +1916,7 @@ fn import_windows_socket_payload( return Ok(None); } let mut err: c_int = 0; - // SAFETY: `info` is a live buffer of export_size() bytes holding the + // SAFETY: `info` is a live buffer of export_size() bytes. let sock = unsafe { bun_uws::socket_transfer::bsd_socket_import(info.as_mut_ptr().cast::(), &mut err) }; @@ -2041,6 +1934,54 @@ fn import_windows_socket_payload( Ok(Some(fd)) } +/// The socket behind a libuv socket-transfer block. The peer duplicated it +/// into this process when it sent the frame, so it exists whether or not +/// anything here wants it, and has to be materialized to be closed. +#[cfg(windows)] +fn import_transferred_socket(transfer: &[u8; ipc_frame::SOCKET_TRANSFER_LEN]) -> Option { + // `WSASocketW` wants an aligned, writable `WSAPROTOCOL_INFOW`. + let mut info = [0u32; ipc_frame::SOCKET_TRANSFER_PROTOCOL_INFO_LEN / 4]; + // SAFETY: both regions are `SOCKET_TRANSFER_PROTOCOL_INFO_LEN` bytes and do not overlap. + unsafe { + core::ptr::copy_nonoverlapping( + transfer.as_ptr(), + info.as_mut_ptr().cast::(), + ipc_frame::SOCKET_TRANSFER_PROTOCOL_INFO_LEN, + ); + } + debug_assert!( + bun_uws::socket_transfer::bsd_socket_export_size() as usize + == ipc_frame::SOCKET_TRANSFER_PROTOCOL_INFO_LEN + ); + let mut err: c_int = 0; + // SAFETY: `info` is a live buffer of export_size() bytes. + let sock = unsafe { + bun_uws::socket_transfer::bsd_socket_import(info.as_mut_ptr().cast::(), &mut err) + }; + if sock == bun_uws::LIBUS_SOCKET_DESCRIPTOR::MAX { + log!("importTransferredSocket: WSASocketW failed: {}", err); + return None; + } + Some(Fd::from_system(sock as *mut c_void)) +} + +/// A Bun peer on Windows sends a socket inside the message that announces it +/// (a Node.js peer sends it with the frame); either way it becomes +/// `incoming_fd`, where an fd received over a POSIX channel is. +#[cfg(windows)] +fn take_windows_socket_payload( + send_queue: &SendQueue, + global: &JSGlobalObject, + msg_data: JSValue, +) -> JsResult<()> { + if let Some(fd) = import_windows_socket_payload(global, msg_data)? + && let Some(unclaimed) = send_queue.incoming_fd.replace(Some(fd)) + { + let _ = unclaimed.close_allowing_standard_io(None); + } + Ok(()) +} + fn received_fd_to_js(fd: Fd) -> JSValue { #[cfg(windows)] { @@ -2053,7 +1994,7 @@ fn received_fd_to_js(fd: Fd) -> JSValue { } #[cfg(not(windows))] { - JSValue::js_number_from_int32(fd.uv()) + JSValue::js_number_from_int32(fd.crt()) } } @@ -2118,10 +2059,7 @@ fn handle_ipc_message( match icmd { IPCCommand::Handle(msg_data) => { #[cfg(windows)] - let imported = import_windows_socket_payload(global_this, msg_data)?; - #[cfg(windows)] - let ack = imported.is_some(); - #[cfg(not(windows))] + take_windows_socket_payload(send_queue, global_this, msg_data)?; let ack = send_queue.incoming_fd.get().is_some(); let packet = if ack { @@ -2148,9 +2086,6 @@ fn handle_ipc_message( } // Get file descriptor and clear it - #[cfg(windows)] - let fd: Fd = imported.unwrap(); - #[cfg(not(windows))] let fd: Fd = send_queue.incoming_fd.take().unwrap(); let Some(owner) = send_queue.owner_ref() else { @@ -2199,10 +2134,7 @@ fn handle_ipc_message( && marker.to_boolean() { #[cfg(windows)] - let imported = import_windows_socket_payload(global_this, msg_data)?; - #[cfg(windows)] - let ack = imported.is_some(); - #[cfg(not(windows))] + take_windows_socket_payload(send_queue, global_this, msg_data)?; let ack = send_queue.incoming_fd.get().is_some(); let packet = if ack { get_ack_packet(send_queue.mode) @@ -2221,9 +2153,6 @@ fn handle_ipc_message( if !ack { return Ok(()); } - #[cfg(windows)] - let fd = imported.unwrap(); - #[cfg(not(windows))] let fd = send_queue.incoming_fd.take().unwrap(); received_fd = Some(fd); handle_js = received_fd_to_js(fd); @@ -2257,6 +2186,7 @@ fn finish_decode(send_queue: &SendQueue, step: &DecodeStep) { } DecodeStep::Fail(IPCDecodeError::Js(JsError::OutOfMemory)) => { Output::print_errorln("IPC message is too long."); + send_queue.input_failed.set(true); send_queue.close_socket(CloseReason::Failure, CloseFrom::User); } // Materializing the message (structured-clone deserialize, buffer @@ -2265,9 +2195,11 @@ fn finish_decode(send_queue: &SendQueue, step: &DecodeStep) { // input. DecodeStep::Fail(IPCDecodeError::Js(err)) => { crate::dispatch::fold(Err(*err)); + send_queue.input_failed.set(true); send_queue.close_socket(CloseReason::Failure, CloseFrom::User); } DecodeStep::Fail(_) => { + send_queue.input_failed.set(true); send_queue.close_socket(CloseReason::Failure, CloseFrom::User); } } @@ -2419,166 +2351,59 @@ fn on_data2(send_queue: &SendQueue, all_data: &[u8]) { } } -/// Used on POSIX -#[allow(non_snake_case)] -pub(crate) mod IPCHandlers { +/// The uSockets callbacks of a POSIX IPC socket. +#[cfg(not(windows))] +pub(crate) mod posix_socket { use super::*; - pub(crate) mod PosixSocket { - use super::*; - - pub(crate) fn on_close( - send_queue: &SendQueue, - _: Socket, - _: c_int, - _: Option<*mut c_void>, - ) { - // uSockets has already freed the underlying socket - log!("NewSocketIPCHandler#onClose\n"); - send_queue.socket_closed(); - } - - pub(crate) fn on_data(send_queue: &SendQueue, _: Socket, all_data: &[u8]) { - let global_this = send_queue.get_global_this(); - // RAII: `enter()` now, `exit()` on drop. The guard holds the raw - // `*mut EventLoop` so `&mut EventLoop` isn't held across `on_data2`. - let _scope = global_this.bun_vm().enter_event_loop_scope(); - on_data2(send_queue, all_data); - } + pub(crate) fn on_close(send_queue: &SendQueue, _: Socket, _: c_int, _: Option<*mut c_void>) { + // uSockets has already freed the underlying socket + log!("NewSocketIPCHandler#onClose\n"); + send_queue.socket_closed(); + } - pub(crate) fn on_fd(send_queue: &SendQueue, _: Socket, fd: c_int) { - // SCM_RIGHTS is POSIX-only; on Windows this arm is unreachable but - // still type-checked, and `FD.fromNative` takes `*anyopaque` there. - #[cfg(windows)] - { - let _ = (send_queue, fd); - return; - } - #[cfg(not(windows))] - { - log!("onFd: {}", fd); - if let Some(existing_fd) = send_queue.incoming_fd.take() { - log!("onFd: incoming_fd already set; overwriting"); - let _ = existing_fd.close_allowing_standard_io(None); - } - send_queue.incoming_fd.set(Some(Fd::from_native(fd))); + pub(crate) fn on_data(send_queue: &SendQueue, _: Socket, all_data: &[u8]) { + if !send_queue.accepts_input() { + // A descriptor that came with the bytes has nobody to take it. + if let Some(fd) = send_queue.incoming_fd.take() { + let _ = fd.close_allowing_standard_io(None); } + return; } + let global_this = send_queue.get_global_this(); + // RAII: `enter()` now, `exit()` on drop. The guard holds the raw + // `*mut EventLoop` so `&mut EventLoop` isn't held across `on_data2`. + let _scope = global_this.bun_vm().enter_event_loop_scope(); + on_data2(send_queue, all_data); + } - pub(crate) fn on_writable(send_queue: &SendQueue, _: Socket) { - log!("onWritable"); - - let global_this = send_queue.get_global_this(); - // RAII: see `on_data`. - let _scope = global_this.bun_vm().enter_event_loop_scope(); - log!("IPC call continueSend() from onWritable"); - send_queue.continue_send(&global_this, ContinueSendReason::OnWritable); + pub(crate) fn on_fd(send_queue: &SendQueue, _: Socket, fd: c_int) { + log!("onFd: {}", fd); + if let Some(existing_fd) = send_queue.incoming_fd.take() { + log!("onFd: incoming_fd already set; overwriting"); + let _ = existing_fd.close_allowing_standard_io(None); } + send_queue.incoming_fd.set(Some(Fd::from_native(fd))); + } - pub(crate) fn on_timeout(_: &SendQueue, _: Socket) { - log!("onTimeout"); - // unref if needed - } + pub(crate) fn on_writable(send_queue: &SendQueue, _: Socket) { + log!("onWritable"); - pub(crate) fn on_end(send_queue: &SendQueue, _: Socket) { - log!("onEnd"); - send_queue.close_socket(CloseReason::Failure, CloseFrom::User); - } + let global_this = send_queue.get_global_this(); + // RAII: see `on_data`. + let _scope = global_this.bun_vm().enter_event_loop_scope(); + log!("IPC call continueSend() from onWritable"); + send_queue.continue_send(&global_this, ContinueSendReason::OnWritable); } - #[cfg(windows)] - pub(crate) mod WindowsNamedPipe { - use super::*; - - pub(crate) fn on_read_alloc(send_queue: &SendQueue, suggested_size: usize) -> &mut [u8] { - log!("NewNamedPipeIPCHandler#onReadAlloc {}", suggested_size); - // SAFETY: the returned region is the buffer's spare capacity, - // handed to libuv for the pending read; nothing else touches - // `incoming` until `on_read` commits the byte count. - let inc = unsafe { &mut *send_queue.incoming.as_ptr() }; - match inc { - IncomingBuffer::Json(json_buf) => { - // SAFETY: libuv writes into this region before notify_written reads. - let spare = unsafe { json_buf.data.uv_alloc_spare_u8(suggested_size) }; - &mut spare[..suggested_size] - } - IncomingBuffer::Advanced(adv_buf) => { - // SAFETY: libuv writes into this region before on_read commits. - let spare = unsafe { adv_buf.uv_alloc_spare_u8(suggested_size) }; - &mut spare[..suggested_size] - } - } - } - - pub(crate) fn on_read_error(send_queue: &SendQueue, err: bun_sys::E) { - log!("NewNamedPipeIPCHandler#onReadError {:?}", err); - send_queue.close_socket_next_tick(true); - } - - /// `nread` is the byte count libuv reported into the slice handed out - /// by `on_read_alloc` (i.e. the tail of `send_queue.incoming` past its - /// current `len`). - pub(crate) fn on_read(send_queue: &SendQueue, nread: usize) { - log!("NewNamedPipeIPCHandler#onRead {}", nread); - let global_this = send_queue.get_global_this(); - let _scope = global_this.bun_vm().enter_event_loop_scope(); - - match send_queue.mode { - Mode::Json => { - // For JSON mode on Windows, use notifyWritten to update length and scan for newlines - send_queue.incoming.with_mut(|inc| { - let IncomingBuffer::Json(json_buf) = inc else { - unreachable!() - }; - debug_assert!(json_buf.data.len() + nread <= json_buf.data.capacity()); - // libuv wrote `nread` bytes at `data[old_len..]` via the - // slice returned from `on_read_alloc`; only the count is - // forwarded. - json_buf.notify_written(nread); - }); + pub(crate) fn on_timeout(_: &SendQueue, _: Socket) { + log!("onTimeout"); + // unref if needed + } - // Process complete messages using next() - avoids O(n²) re-scanning - loop { - match decode_next_json(&send_queue.incoming, &global_this) { - DecodeStep::Message(result) => { - crate::dispatch::fold(handle_ipc_message( - send_queue, - result.message, - &global_this, - )); - } - step => return finish_decode(send_queue, &step), - } - } - } - Mode::Advanced => { - send_queue.incoming.with_mut(|inc| { - let IncomingBuffer::Advanced(adv_buf) = inc else { - unreachable!() - }; - // SAFETY: `on_read_alloc` reserved ≥ nread bytes; libuv initialised them. - unsafe { adv_buf.uv_commit(nread) }; - }); - let mut slice_start: usize = 0; - loop { - match decode_next_advanced( - &send_queue.incoming, - &global_this, - &mut slice_start, - ) { - DecodeStep::Message(result) => { - crate::dispatch::fold(handle_ipc_message( - send_queue, - result.message, - &global_this, - )); - } - step => return finish_decode(send_queue, &step), - } - } - } - } - } + pub(crate) fn on_end(send_queue: &SendQueue, _: Socket) { + log!("onEnd"); + send_queue.close_socket(CloseReason::Failure, CloseFrom::User); } } diff --git a/src/runtime/ipc_host.rs b/src/runtime/ipc_host.rs index 60482323e23f..b8af5ad61458 100644 --- a/src/runtime/ipc_host.rs +++ b/src/runtime/ipc_host.rs @@ -21,6 +21,9 @@ bun_core::define_scoped_log!(log, IPC, visible); // `node_cluster_binding.rs`. unsafe extern "C" { safe fn Process__emitErrorEvent(global: &JSGlobalObject, value: JSValue); + /// `BunWindowsProcess.cpp`; negative when the parent cannot be determined. + #[cfg(windows)] + safe fn Bun__getParentProcessId() -> i32; } #[derive(Copy, Clone, Eq, PartialEq)] @@ -91,7 +94,8 @@ pub(crate) fn do_send( global_object: &JSGlobalObject, call_frame: &CallFrame, from: FromEnum, - peer_pid: u32, + // Windows: only a message that carries a handle asks who is on the other end. + peer_pid: &dyn Fn() -> u32, ) -> JsResult { let [mut message, mut handle, options_, mut callback] = call_frame.arguments_as_array::<4>(); #[cfg(not(windows))] @@ -262,6 +266,7 @@ pub(crate) fn do_send( #[cfg(windows)] if let Some(h) = &mut zig_handle { + let peer_pid = peer_pid(); match attach_windows_socket_payload(global_object, message, h.fd, peer_pid)? { Some(hex) => { h.win_export_hex = Some(hex); @@ -369,18 +374,17 @@ fn Bun__Process__send(global: &JSGlobalObject, frame: &CallFrame) -> JsResult, } -/// One channel per JS thread (a VM is thread-bound; workers re-detect their -/// own inherited fd). +/// One channel per JS thread (a VM is thread-bound). #[thread_local] static CHANNEL: core::cell::Cell>> = core::cell::Cell::new(None); @@ -495,25 +498,24 @@ pub(crate) fn get_ipc_instance( vm.event_loop_mut().ensure_waker(); + let instance = IPCInstance::new(IPCInstance { + data: SendQueue::new(mode, None, IPC::SocketUnion::Uninitialized), + }); + // SAFETY: `instance` was just boxed; `send_queue` is its live SendQueue. + let send_queue: *mut SendQueue = unsafe { (*instance).data.as_ptr() }; + // SAFETY: as above. + unsafe { + (*send_queue).set_owner(IPC::SendQueueOwner::Instance( + core::ptr::NonNull::new_unchecked(instance), + )) + }; + // SAFETY: `instance` was just boxed above and is non-null. + CHANNEL.set(Some(unsafe { core::ptr::NonNull::new_unchecked(instance) })); + #[cfg(not(windows))] - let instance: *mut IPCInstance = { + let opened = { let loop_ = vm.uws_loop(); let group: *mut bun_uws::SocketGroup = vm.rare_data().spawn_ipc_group(loop_); - - let instance = IPCInstance::new(IPCInstance { - data: SendQueue::new(mode, None, IPC::SocketUnion::Uninitialized), - }); - // SAFETY: `instance` was just boxed; `send_queue` is its live SendQueue. - let send_queue: *mut SendQueue = unsafe { (*instance).data.as_ptr() }; - // SAFETY: as above. - unsafe { - (*send_queue).set_owner(IPC::SendQueueOwner::Instance( - core::ptr::NonNull::new_unchecked(instance), - )) - }; - // SAFETY: `instance` was just boxed above and is non-null. - CHANNEL.set(Some(unsafe { core::ptr::NonNull::new_unchecked(instance) })); - // SAFETY: `group` is the live per-VM SocketGroup; `send_queue` is // the freshly-allocated SendQueue (root raw pointer, stored in the // socket ext slot for the socket's lifetime). @@ -526,53 +528,38 @@ pub(crate) fn get_ipc_instance( true, ) }; - let Some(socket) = socket else { - // SAFETY: `instance` was produced by `IPCInstance::new` - // (heap::alloc) above and is not yet aliased. - unsafe { IPCInstance::deinit(instance) }; - CHANNEL.set(None); - bun_core::warn!("Unable to start IPC socket"); - return None; - }; - socket.set_timeout(0); - - // SAFETY: `send_queue` is live (owned by `instance`). - unsafe { (*send_queue).socket.set(IPC::SocketUnion::Open(socket)) }; - - instance - }; - - #[cfg(windows)] - let instance: *mut IPCInstance = { - let instance = IPCInstance::new(IPCInstance { - data: SendQueue::new(mode, None, IPC::SocketUnion::Uninitialized), - }); - // SAFETY: `instance` was just boxed; `send_queue` is its live SendQueue. - let send_queue: *mut SendQueue = unsafe { (*instance).data.as_ptr() }; - // SAFETY: as above. - unsafe { - (*send_queue).set_owner(IPC::SendQueueOwner::Instance( - core::ptr::NonNull::new_unchecked(instance), - )) - }; - // SAFETY: `instance` was just boxed above and is non-null. - CHANNEL.set(Some(unsafe { core::ptr::NonNull::new_unchecked(instance) })); - - // `windows_configure_client` STORES the `*mut SendQueue` in - // `uv_handle_t.data` for the pipe's lifetime; `send_queue` is the - // allocation's root raw pointer. - // SAFETY: `send_queue` is the live SendQueue owned by `instance`. - if let Err(_) = unsafe { SendQueue::windows_configure_client(send_queue, fd) } { - // SAFETY: `instance` was produced by `IPCInstance::new` - // (heap::alloc) above and is not yet aliased. - unsafe { IPCInstance::deinit(instance) }; - CHANNEL.set(None); - bun_core::output::warn(&format_args!("Unable to start IPC pipe '{:?}'", fd)); - return None; + if let Some(socket) = socket { + socket.set_timeout(0); + // SAFETY: `send_queue` is live (owned by `instance`). + unsafe { (*send_queue).socket.set(IPC::SocketUnion::Open(socket)) }; } - - instance + socket.is_some() }; + // The inherited end is whatever the parent made it. + // SAFETY: `send_queue` is the root pointer of the live SendQueue owned by + // `instance`. + #[cfg(windows)] + let opened = unsafe { + SendQueue::open_pipe( + send_queue, + vm.uws_loop(), + fd, + bun_io::windows::PipeOrigin::InheritedUnshared, + ) + } + .is_ok(); + + if !opened { + // SAFETY: `instance` was produced by `IPCInstance::new` (heap::alloc) + // above and is not yet aliased. + unsafe { IPCInstance::deinit(instance) }; + CHANNEL.set(None); + #[cfg(not(windows))] + bun_core::warn!("Unable to start IPC socket"); + #[cfg(windows)] + bun_core::output::warn(&format_args!("Unable to start IPC pipe '{:?}'", fd)); + return None; + } // SAFETY: `instance` is the live boxed IPCInstance. unsafe { (*instance).data().write_version_packet(vm.global()) }; diff --git a/src/runtime/jsc_hooks.rs b/src/runtime/jsc_hooks.rs index 9f565cf3f15b..177ba09d16cc 100644 --- a/src/runtime/jsc_hooks.rs +++ b/src/runtime/jsc_hooks.rs @@ -62,7 +62,7 @@ use crate::webcore::blob::BlobExt as _; /// `VirtualMachine.rs`); until those slots widen to `*mut c_void`, the /// thread-local is the recovery path. pub(crate) struct RuntimeState { - /// setTimeout/setInterval heap + uv timers. + /// setTimeout/setInterval heap. pub(crate) timer: timer::All, /// `RareData.{mysql,postgresql}_context` — concrete SQL state. The /// `bun_jsc::rare_data::RareData` slots for these are opaque ZSTs (cycle @@ -929,7 +929,7 @@ unsafe fn ensure_debugger(vm: *mut VirtualMachine, block_until_connected: bool) /// `eventLoop().autoTick()`. Needs /// `timer::All` for the poll-timeout calculation, hence dispatched here. /// -/// PERF: the one fn-ptr indirection is dwarfed by the kqueue/epoll syscall it +/// PERF: the one fn-ptr indirection is dwarfed by the loop's wait syscall it /// gates. /// /// # Safety @@ -950,26 +950,18 @@ unsafe fn auto_tick(vm: *mut VirtualMachine) { unsafe { (*el).tick_immediate_tasks(vm) }; // SAFETY: as above. let has_yielded_tasks = unsafe { (*el).promote_yield_tasks() }; - #[cfg(windows)] - if has_yielded_tasks || !unsafe { &*el }.immediate_tasks.is_empty() { - // SAFETY: `el` is the live per-thread event loop. - unsafe { (*el).wakeup() }; - } // ── pending unref ─────────────────────────────────────────────────── - #[cfg(unix)] - { - // SAFETY: per fn contract. `swap(0)` so a concurrent - // `increment_pending_unref_counter()` (cross-thread, see - // `KeepAlive::unref_on_next_tick`) can't be lost between - // the read and the reset. - let pending_unref = unsafe { &*vm } - .pending_unref_counter - .swap(0, core::sync::atomic::Ordering::Relaxed); - if pending_unref > 0 { - // SAFETY: `loop_` is the live per-thread uws loop. - unsafe { (*loop_).unref_count(pending_unref) }; - } + // SAFETY: per fn contract. `swap(0)` so a concurrent + // `increment_pending_unref_counter()` (cross-thread, see + // `KeepAlive::unref_on_next_tick`) can't be lost between + // the read and the reset. + let pending_unref = unsafe { &*vm } + .pending_unref_counter + .swap(0, core::sync::atomic::Ordering::Relaxed); + if pending_unref > 0 { + // SAFETY: `loop_` is the live per-thread uws loop. + unsafe { (*loop_).unref_count(pending_unref) }; } // ── DateHeaderTimer / imminent-GC ─────────────────────────────────── @@ -994,7 +986,7 @@ unsafe fn auto_tick(vm: *mut VirtualMachine) { // No high-tier state (unit test) — fall back to a non-blocking I/O // poll. The uws loop must always be polled // (`tick_with_timeout`/`tick_without_idle`); `EventLoop::tick()` would only - // drain JS tasks and never touch kqueue/epoll. + // drain JS tasks and never poll for I/O. // SAFETY: `loop_` is the live per-thread uws loop. unsafe { (*loop_).tick_without_idle() }; // Still run the post-poll hooks. @@ -1014,7 +1006,6 @@ unsafe fn auto_tick(vm: *mut VirtualMachine) { // `tickImmediateTasks` swaps `next_immediate_tasks` in, so this // reflects next-tick immediates (queued during the drain above). // SAFETY: `el` is the live per-thread event loop. - // SAFETY: `el` is the live per-thread event loop. let has_pending_immediate = has_yielded_tasks || !unsafe { &*el }.immediate_tasks.is_empty() || unsafe { &*el }.has_pending_tasks(); @@ -1044,7 +1035,7 @@ unsafe fn auto_tick(vm: *mut VirtualMachine) { // `timer::All::get_timeout` forms short-lived `&mut` only around // heap ops that cannot re-enter JS, releasing the borrow before // invoking `fire()`. - // `get_timeout` reads CLOCK_MONOTONIC to compare against the timer heap; hand that + // `get_timeout` reads the monotonic clock to compare against the timer heap; hand that // same reading to the tick for the park hook's idle-sweep rate limit. It is lazy, // and so is the hook: NOW_NS_UNKNOWN means it took none. let mut now: Option = None; @@ -1072,17 +1063,14 @@ unsafe fn auto_tick(vm: *mut VirtualMachine) { } } - #[cfg(unix)] - { - // Note (§Forbidden aliased-&mut): `drain_timers` fires user - // `setTimeout` callbacks which may re-enter `timer::All::insert`/ - // `remove` via `runtime_state()`. Pass raw `*mut Self` so no - // long-lived `&mut (*state).timer` is held across `fire()`; - // `drain_timers` forms short-lived `&mut` only around heap pop/peek. - // SAFETY: `state` is the live per-thread `RuntimeState`; the `timer` - // field address is stable for the VM lifetime. - unsafe { timer::All::drain_timers(&mut (*state).timer, vm.cast()) }; - } + // Note (§Forbidden aliased-&mut): `drain_timers` fires user + // `setTimeout` callbacks which may re-enter `timer::All::insert`/ + // `remove` via `runtime_state()`. Pass raw `*mut Self` so no + // long-lived `&mut (*state).timer` is held across `fire()`; + // `drain_timers` forms short-lived `&mut` only around heap pop/peek. + // SAFETY: `state` is the live per-thread `RuntimeState`; the `timer` + // field address is stable for the VM lifetime. + unsafe { timer::All::drain_timers(&mut (*state).timer, vm.cast()) }; // SAFETY: per fn contract. unsafe { (*vm).on_after_event_loop() }; @@ -1109,25 +1097,17 @@ unsafe fn auto_tick_active(vm: *mut VirtualMachine) { unsafe { (*el).tick_immediate_tasks(vm) }; // SAFETY: as above. let has_yielded_tasks = unsafe { (*el).promote_yield_tasks() }; - #[cfg(windows)] - if has_yielded_tasks || !unsafe { &*el }.immediate_tasks.is_empty() { - // SAFETY: `el` is the live per-thread event loop. - unsafe { (*el).wakeup() }; - } - #[cfg(unix)] - { - // SAFETY: per fn contract. `swap(0)` so a concurrent - // `increment_pending_unref_counter()` (cross-thread, see - // `KeepAlive::unref_on_next_tick`) can't be lost between - // the read and the reset. - let pending_unref = unsafe { &*vm } - .pending_unref_counter - .swap(0, core::sync::atomic::Ordering::Relaxed); - if pending_unref > 0 { - // SAFETY: `loop_` is the live per-thread uws loop. - unsafe { (*loop_).unref_count(pending_unref) }; - } + // SAFETY: per fn contract. `swap(0)` so a concurrent + // `increment_pending_unref_counter()` (cross-thread, see + // `KeepAlive::unref_on_next_tick`) can't be lost between + // the read and the reset. + let pending_unref = unsafe { &*vm } + .pending_unref_counter + .swap(0, core::sync::atomic::Ordering::Relaxed); + if pending_unref > 0 { + // SAFETY: `loop_` is the live per-thread uws loop. + unsafe { (*loop_).unref_count(pending_unref) }; } let state = runtime_state(); @@ -1149,7 +1129,6 @@ unsafe fn auto_tick_active(vm: *mut VirtualMachine) { } { - // SAFETY: `el` is the live per-thread event loop. // SAFETY: `el` is the live per-thread event loop. let has_pending_immediate = has_yielded_tasks || !unsafe { &*el }.immediate_tasks.is_empty() @@ -1169,7 +1148,7 @@ unsafe fn auto_tick_active(vm: *mut VirtualMachine) { // Before `get_timeout` — see the matching call in `auto_tick`. // SAFETY: `el` is the live per-thread event loop. unsafe { (*el).process_gc_timer() }; - // `get_timeout` reads CLOCK_MONOTONIC to compare against the timer heap; hand that + // `get_timeout` reads the monotonic clock to compare against the timer heap; hand that // same reading to the tick for the park hook's idle-sweep rate limit. It is lazy, // and so is the hook: NOW_NS_UNKNOWN means it took none. let mut now: Option = None; @@ -1197,12 +1176,9 @@ unsafe fn auto_tick_active(vm: *mut VirtualMachine) { } } - #[cfg(unix)] - { - // SAFETY: `state` is the live per-thread `RuntimeState`; see Note - // on `auto_tick` re: aliased-&mut across `fire()`. - unsafe { timer::All::drain_timers(&mut (*state).timer, vm.cast()) }; - } + // SAFETY: `state` is the live per-thread `RuntimeState`; see Note + // on `auto_tick` re: aliased-&mut across `fire()`. + unsafe { timer::All::drain_timers(&mut (*state).timer, vm.cast()) }; // SAFETY: per fn contract. unsafe { (*vm).on_after_event_loop() }; @@ -1513,7 +1489,6 @@ static __BUN_RUNTIME_HOOKS: RuntimeHooks = RuntimeHooks { stop_dns_for_vm_teardown, stop_active_handles_for_vm_teardown: stop_active_handles_for_vm_teardown_hook, disarm_all_timers_for_vm_teardown, - close_timer_loop_handles_after_vm_destroyed, }; // ════════════════════════════════════════════════════════════════════════════ @@ -1661,21 +1636,6 @@ unsafe fn cancel_timers(vm: *mut VirtualMachine, only: Option SweepResult { result = result.and(unsafe { crate::dns_jsc::Resolver::close_channel_for_terminate(gd.resolver.as_ctx_ptr()) }); - #[cfg(windows)] - gd.resolver.cancel_pending_uv_requests_for_teardown(); } #[cfg(target_os = "macos")] crate::dns_jsc::dns_sd::SharedConnection::close_for_terminate(); diff --git a/src/runtime/napi/napi_body.rs b/src/runtime/napi/napi_body.rs index 76421fc09949..f7e98b7e230c 100644 --- a/src/runtime/napi/napi_body.rs +++ b/src/runtime/napi/napi_body.rs @@ -2366,9 +2366,6 @@ extern "C" fn napi_get_node_version( env.ok() } -#[cfg(windows)] -type napi_event_loop = *mut bun_sys::windows::libuv::Loop; -#[cfg(not(windows))] type napi_event_loop = *mut EventLoop; #[unsafe(no_mangle)] @@ -2376,20 +2373,8 @@ extern "C" fn napi_get_uv_event_loop(env_: napi_env, loop_: *mut napi_event_loop bun_output::scoped_log!(napi, "napi_get_uv_event_loop"); let env = get_env!(env_); let loop_out = get_out!(env, loop_); - #[cfg(windows)] - { - // A past alignment assertion here fired spuriously. - // TODO(@190n) investigate - *loop_out = VirtualMachine::get().uv_loop(); - } - #[cfg(not(windows))] - { - // there is no uv event loop on posix, we use our event loop handle. - // SAFETY: `VirtualMachine::event_loop` already yields `*mut EventLoop`; - // no const→mut cast needed. - // SAFETY: bun_vm() never null for a Bun-owned global. - *loop_out = env.to_js().bun_vm().event_loop(); - } + // There is no uv event loop; we use our event loop handle. + *loop_out = env.to_js().bun_vm().event_loop(); env.ok() } @@ -3736,10 +3721,6 @@ mod v8_api { pub(super) fn _ZNK2v814CpuProfileNode8GetChildEi() -> *mut c_void; pub(super) fn _ZN2v83Map3SetENS_5LocalINS_7ContextEEENS1_INS_5ValueEEES5_() -> *mut c_void; pub(super) fn _ZN2v83Map6DeleteENS_5LocalINS_7ContextEEENS1_INS_5ValueEEE() -> *mut c_void; - // NOTE: return type omitted to match the `uv_functions_to_export` declarations - // below (avoids `clashing_extern_declarations`); only the symbol address is used. - pub(super) fn uv_os_getpid(); - pub(super) fn uv_os_getppid(); } } @@ -4216,334 +4197,6 @@ mod posix_platform_specific_v8_apis { } } -// ────────────────────────────────────────────────────────────────────────── -// uv_* symbol references (posix DCE suppression) -// ────────────────────────────────────────────────────────────────────────── - -#[cfg(unix)] -mod uv_functions_to_export { - unsafe extern "C" { - pub(super) fn uv_accept(); - pub(super) fn uv_async_init(); - pub(super) fn uv_async_send(); - pub(super) fn uv_available_parallelism(); - pub(super) fn uv_backend_fd(); - pub(super) fn uv_backend_timeout(); - pub(super) fn uv_barrier_destroy(); - pub(super) fn uv_barrier_init(); - pub(super) fn uv_barrier_wait(); - pub(super) fn uv_buf_init(); - pub(super) fn uv_cancel(); - pub(super) fn uv_chdir(); - pub(super) fn uv_check_init(); - pub(super) fn uv_check_start(); - pub(super) fn uv_check_stop(); - pub(super) fn uv_clock_gettime(); - pub(super) fn uv_close(); - pub(super) fn uv_cond_broadcast(); - pub(super) fn uv_cond_destroy(); - pub(super) fn uv_cond_init(); - pub(super) fn uv_cond_signal(); - pub(super) fn uv_cond_timedwait(); - pub(super) fn uv_cond_wait(); - pub(super) fn uv_cpu_info(); - pub(super) fn uv_cpumask_size(); - pub(super) fn uv_cwd(); - pub(super) fn uv_default_loop(); - pub(super) fn uv_disable_stdio_inheritance(); - pub(super) fn uv_dlclose(); - pub(super) fn uv_dlerror(); - pub(super) fn uv_dlopen(); - pub(super) fn uv_dlsym(); - pub(super) fn uv_err_name(); - pub(super) fn uv_err_name_r(); - pub(super) fn uv_exepath(); - pub(super) fn uv_fileno(); - pub(super) fn uv_free_cpu_info(); - pub(super) fn uv_free_interface_addresses(); - pub(super) fn uv_freeaddrinfo(); - pub(super) fn uv_fs_access(); - pub(super) fn uv_fs_chmod(); - pub(super) fn uv_fs_chown(); - pub(super) fn uv_fs_close(); - pub(super) fn uv_fs_closedir(); - pub(super) fn uv_fs_copyfile(); - pub(super) fn uv_fs_event_getpath(); - pub(super) fn uv_fs_event_init(); - pub(super) fn uv_fs_event_start(); - pub(super) fn uv_fs_event_stop(); - pub(super) fn uv_fs_fchmod(); - pub(super) fn uv_fs_fchown(); - pub(super) fn uv_fs_fdatasync(); - pub(super) fn uv_fs_fstat(); - pub(super) fn uv_fs_fsync(); - pub(super) fn uv_fs_ftruncate(); - pub(super) fn uv_fs_futime(); - pub(super) fn uv_fs_get_path(); - pub(super) fn uv_fs_get_ptr(); - pub(super) fn uv_fs_get_result(); - pub(super) fn uv_fs_get_statbuf(); - pub(super) fn uv_fs_get_system_error(); - pub(super) fn uv_fs_get_type(); - pub(super) fn uv_fs_lchown(); - pub(super) fn uv_fs_link(); - pub(super) fn uv_fs_lstat(); - pub(super) fn uv_fs_lutime(); - pub(super) fn uv_fs_mkdir(); - pub(super) fn uv_fs_mkdtemp(); - pub(super) fn uv_fs_mkstemp(); - pub(super) fn uv_fs_open(); - pub(super) fn uv_fs_opendir(); - pub(super) fn uv_fs_poll_getpath(); - pub(super) fn uv_fs_poll_init(); - pub(super) fn uv_fs_poll_start(); - pub(super) fn uv_fs_poll_stop(); - pub(super) fn uv_fs_read(); - pub(super) fn uv_fs_readdir(); - pub(super) fn uv_fs_readlink(); - pub(super) fn uv_fs_realpath(); - pub(super) fn uv_fs_rename(); - pub(super) fn uv_fs_req_cleanup(); - pub(super) fn uv_fs_rmdir(); - pub(super) fn uv_fs_scandir(); - pub(super) fn uv_fs_scandir_next(); - pub(super) fn uv_fs_sendfile(); - pub(super) fn uv_fs_stat(); - pub(super) fn uv_fs_statfs(); - pub(super) fn uv_fs_symlink(); - pub(super) fn uv_fs_unlink(); - pub(super) fn uv_fs_utime(); - pub(super) fn uv_fs_write(); - pub(super) fn uv_get_available_memory(); - pub(super) fn uv_get_constrained_memory(); - pub(super) fn uv_get_free_memory(); - pub(super) fn uv_get_osfhandle(); - pub(super) fn uv_get_process_title(); - pub(super) fn uv_get_total_memory(); - pub(super) fn uv_getaddrinfo(); - pub(super) fn uv_getnameinfo(); - pub(super) fn uv_getrusage(); - pub(super) fn uv_getrusage_thread(); - pub(super) fn uv_gettimeofday(); - pub(super) fn uv_guess_handle(); - pub(super) fn uv_handle_get_data(); - pub(super) fn uv_handle_get_loop(); - pub(super) fn uv_handle_get_type(); - pub(super) fn uv_handle_set_data(); - pub(super) fn uv_handle_size(); - pub(super) fn uv_handle_type_name(); - pub(super) fn uv_has_ref(); - pub(super) fn uv_hrtime(); - pub(super) fn uv_idle_init(); - pub(super) fn uv_idle_start(); - pub(super) fn uv_idle_stop(); - pub(super) fn uv_if_indextoiid(); - pub(super) fn uv_if_indextoname(); - pub(super) fn uv_inet_ntop(); - pub(super) fn uv_inet_pton(); - pub(super) fn uv_interface_addresses(); - pub(super) fn uv_ip_name(); - pub(super) fn uv_ip4_addr(); - pub(super) fn uv_ip4_name(); - pub(super) fn uv_ip6_addr(); - pub(super) fn uv_ip6_name(); - pub(super) fn uv_is_active(); - pub(super) fn uv_is_closing(); - pub(super) fn uv_is_readable(); - pub(super) fn uv_is_writable(); - pub(super) fn uv_key_create(); - pub(super) fn uv_key_delete(); - pub(super) fn uv_key_get(); - pub(super) fn uv_key_set(); - pub(super) fn uv_kill(); - pub(super) fn uv_library_shutdown(); - pub(super) fn uv_listen(); - pub(super) fn uv_loadavg(); - pub(super) fn uv_loop_alive(); - pub(super) fn uv_loop_close(); - pub(super) fn uv_loop_configure(); - pub(super) fn uv_loop_delete(); - pub(super) fn uv_loop_fork(); - pub(super) fn uv_loop_get_data(); - pub(super) fn uv_loop_init(); - pub(super) fn uv_loop_new(); - pub(super) fn uv_loop_set_data(); - pub(super) fn uv_loop_size(); - pub(super) fn uv_metrics_idle_time(); - pub(super) fn uv_metrics_info(); - pub(super) fn uv_mutex_destroy(); - pub(super) fn uv_mutex_init(); - pub(super) fn uv_mutex_init_recursive(); - pub(super) fn uv_mutex_lock(); - pub(super) fn uv_mutex_trylock(); - pub(super) fn uv_mutex_unlock(); - pub(super) fn uv_now(); - pub(super) fn uv_once(); - pub(super) fn uv_open_osfhandle(); - pub(super) fn uv_os_environ(); - pub(super) fn uv_os_free_environ(); - pub(super) fn uv_os_free_group(); - pub(super) fn uv_os_free_passwd(); - pub(super) fn uv_os_get_group(); - pub(super) fn uv_os_get_passwd(); - pub(super) fn uv_os_get_passwd2(); - pub(super) fn uv_os_getenv(); - pub(super) fn uv_os_gethostname(); - pub(super) fn uv_os_getpid(); - pub(super) fn uv_os_getppid(); - pub(super) fn uv_os_getpriority(); - pub(super) fn uv_os_homedir(); - pub(super) fn uv_os_setenv(); - pub(super) fn uv_os_setpriority(); - pub(super) fn uv_os_tmpdir(); - pub(super) fn uv_os_uname(); - pub(super) fn uv_os_unsetenv(); - pub(super) fn uv_pipe(); - pub(super) fn uv_pipe_bind(); - pub(super) fn uv_pipe_bind2(); - pub(super) fn uv_pipe_chmod(); - pub(super) fn uv_pipe_connect(); - pub(super) fn uv_pipe_connect2(); - pub(super) fn uv_pipe_getpeername(); - pub(super) fn uv_pipe_getsockname(); - pub(super) fn uv_pipe_init(); - pub(super) fn uv_pipe_open(); - pub(super) fn uv_pipe_pending_count(); - pub(super) fn uv_pipe_pending_instances(); - pub(super) fn uv_pipe_pending_type(); - pub(super) fn uv_poll_init(); - pub(super) fn uv_poll_init_socket(); - pub(super) fn uv_poll_start(); - pub(super) fn uv_poll_stop(); - pub(super) fn uv_prepare_init(); - pub(super) fn uv_prepare_start(); - pub(super) fn uv_prepare_stop(); - pub(super) fn uv_print_active_handles(); - pub(super) fn uv_print_all_handles(); - pub(super) fn uv_process_get_pid(); - pub(super) fn uv_process_kill(); - pub(super) fn uv_queue_work(); - pub(super) fn uv_random(); - pub(super) fn uv_read_start(); - pub(super) fn uv_read_stop(); - pub(super) fn uv_recv_buffer_size(); - pub(super) fn uv_ref(); - pub(super) fn uv_replace_allocator(); - pub(super) fn uv_req_get_data(); - pub(super) fn uv_req_get_type(); - pub(super) fn uv_req_set_data(); - pub(super) fn uv_req_size(); - pub(super) fn uv_req_type_name(); - pub(super) fn uv_resident_set_memory(); - pub(super) fn uv_run(); - pub(super) fn uv_rwlock_destroy(); - pub(super) fn uv_rwlock_init(); - pub(super) fn uv_rwlock_rdlock(); - pub(super) fn uv_rwlock_rdunlock(); - pub(super) fn uv_rwlock_tryrdlock(); - pub(super) fn uv_rwlock_trywrlock(); - pub(super) fn uv_rwlock_wrlock(); - pub(super) fn uv_rwlock_wrunlock(); - pub(super) fn uv_sem_destroy(); - pub(super) fn uv_sem_init(); - pub(super) fn uv_sem_post(); - pub(super) fn uv_sem_trywait(); - pub(super) fn uv_sem_wait(); - pub(super) fn uv_send_buffer_size(); - pub(super) fn uv_set_process_title(); - pub(super) fn uv_setup_args(); - pub(super) fn uv_shutdown(); - pub(super) fn uv_signal_init(); - pub(super) fn uv_signal_start(); - pub(super) fn uv_signal_start_oneshot(); - pub(super) fn uv_signal_stop(); - pub(super) fn uv_sleep(); - pub(super) fn uv_socketpair(); - pub(super) fn uv_spawn(); - pub(super) fn uv_stop(); - pub(super) fn uv_stream_get_write_queue_size(); - pub(super) fn uv_stream_set_blocking(); - pub(super) fn uv_strerror(); - pub(super) fn uv_strerror_r(); - pub(super) fn uv_tcp_bind(); - pub(super) fn uv_tcp_close_reset(); - pub(super) fn uv_tcp_connect(); - pub(super) fn uv_tcp_getpeername(); - pub(super) fn uv_tcp_getsockname(); - pub(super) fn uv_tcp_init(); - pub(super) fn uv_tcp_init_ex(); - pub(super) fn uv_tcp_keepalive(); - pub(super) fn uv_tcp_nodelay(); - pub(super) fn uv_tcp_open(); - pub(super) fn uv_tcp_simultaneous_accepts(); - pub(super) fn uv_thread_create(); - pub(super) fn uv_thread_create_ex(); - pub(super) fn uv_thread_detach(); - pub(super) fn uv_thread_equal(); - pub(super) fn uv_thread_getaffinity(); - pub(super) fn uv_thread_getcpu(); - pub(super) fn uv_thread_getname(); - pub(super) fn uv_thread_getpriority(); - pub(super) fn uv_thread_join(); - pub(super) fn uv_thread_self(); - pub(super) fn uv_thread_setaffinity(); - pub(super) fn uv_thread_setname(); - pub(super) fn uv_thread_setpriority(); - pub(super) fn uv_timer_again(); - pub(super) fn uv_timer_get_due_in(); - pub(super) fn uv_timer_get_repeat(); - pub(super) fn uv_timer_init(); - pub(super) fn uv_timer_set_repeat(); - pub(super) fn uv_timer_start(); - pub(super) fn uv_timer_stop(); - pub(super) fn uv_translate_sys_error(); - pub(super) fn uv_try_write(); - pub(super) fn uv_try_write2(); - pub(super) fn uv_tty_get_vterm_state(); - pub(super) fn uv_tty_get_winsize(); - pub(super) fn uv_tty_init(); - pub(super) fn uv_tty_reset_mode(); - pub(super) fn uv_tty_set_mode(); - pub(super) fn uv_tty_set_vterm_state(); - pub(super) fn uv_udp_bind(); - pub(super) fn uv_udp_connect(); - pub(super) fn uv_udp_get_send_queue_count(); - pub(super) fn uv_udp_get_send_queue_size(); - pub(super) fn uv_udp_getpeername(); - pub(super) fn uv_udp_getsockname(); - pub(super) fn uv_udp_init(); - pub(super) fn uv_udp_init_ex(); - pub(super) fn uv_udp_open(); - pub(super) fn uv_udp_recv_start(); - pub(super) fn uv_udp_recv_stop(); - pub(super) fn uv_udp_send(); - pub(super) fn uv_udp_set_broadcast(); - pub(super) fn uv_udp_set_membership(); - pub(super) fn uv_udp_set_multicast_interface(); - pub(super) fn uv_udp_set_multicast_loop(); - pub(super) fn uv_udp_set_multicast_ttl(); - pub(super) fn uv_udp_set_source_membership(); - pub(super) fn uv_udp_set_ttl(); - pub(super) fn uv_udp_try_send(); - pub(super) fn uv_udp_try_send2(); - pub(super) fn uv_udp_using_recvmmsg(); - pub(super) fn uv_unref(); - pub(super) fn uv_update_time(); - pub(super) fn uv_uptime(); - pub(super) fn uv_utf16_length_as_wtf8(); - pub(super) fn uv_utf16_to_wtf8(); - pub(super) fn uv_version(); - pub(super) fn uv_version_string(); - pub(super) fn uv_walk(); - pub(super) fn uv_write(); - pub(super) fn uv_write2(); - pub(super) fn uv_wtf8_length_as_utf16(); - pub(super) fn uv_wtf8_to_utf16(); - } -} -#[cfg(not(unix))] -mod uv_functions_to_export {} - // ────────────────────────────────────────────────────────────────────────── // fix_dead_code_elimination // ────────────────────────────────────────────────────────────────────────── @@ -4702,335 +4355,9 @@ pub(crate) fn fix_dead_code_elimination() { node_api_is_sharedarraybuffer, ); - // uv_functions_to_export - // This list is hand-maintained — keep it in sync with the - // `uv_functions_to_export` module above. - #[cfg(unix)] - { - use uv_functions_to_export::*; - keep_symbols!( - uv_accept, - uv_async_init, - uv_async_send, - uv_available_parallelism, - uv_backend_fd, - uv_backend_timeout, - uv_barrier_destroy, - uv_barrier_init, - uv_barrier_wait, - uv_buf_init, - uv_cancel, - uv_chdir, - uv_check_init, - uv_check_start, - uv_check_stop, - uv_clock_gettime, - uv_close, - uv_cond_broadcast, - uv_cond_destroy, - uv_cond_init, - uv_cond_signal, - uv_cond_timedwait, - uv_cond_wait, - uv_cpu_info, - uv_cpumask_size, - uv_cwd, - uv_default_loop, - uv_disable_stdio_inheritance, - uv_dlclose, - uv_dlerror, - uv_dlopen, - uv_dlsym, - uv_err_name, - uv_err_name_r, - uv_exepath, - uv_fileno, - uv_free_cpu_info, - uv_free_interface_addresses, - uv_freeaddrinfo, - uv_fs_access, - uv_fs_chmod, - uv_fs_chown, - uv_fs_close, - uv_fs_closedir, - uv_fs_copyfile, - uv_fs_event_getpath, - uv_fs_event_init, - uv_fs_event_start, - uv_fs_event_stop, - uv_fs_fchmod, - uv_fs_fchown, - uv_fs_fdatasync, - uv_fs_fstat, - uv_fs_fsync, - uv_fs_ftruncate, - uv_fs_futime, - uv_fs_get_path, - uv_fs_get_ptr, - uv_fs_get_result, - uv_fs_get_statbuf, - uv_fs_get_system_error, - uv_fs_get_type, - uv_fs_lchown, - uv_fs_link, - uv_fs_lstat, - uv_fs_lutime, - uv_fs_mkdir, - uv_fs_mkdtemp, - uv_fs_mkstemp, - uv_fs_open, - uv_fs_opendir, - uv_fs_poll_getpath, - uv_fs_poll_init, - uv_fs_poll_start, - uv_fs_poll_stop, - uv_fs_read, - uv_fs_readdir, - uv_fs_readlink, - uv_fs_realpath, - uv_fs_rename, - uv_fs_req_cleanup, - uv_fs_rmdir, - uv_fs_scandir, - uv_fs_scandir_next, - uv_fs_sendfile, - uv_fs_stat, - uv_fs_statfs, - uv_fs_symlink, - uv_fs_unlink, - uv_fs_utime, - uv_fs_write, - uv_get_available_memory, - uv_get_constrained_memory, - uv_get_free_memory, - uv_get_osfhandle, - uv_get_process_title, - uv_get_total_memory, - uv_getaddrinfo, - uv_getnameinfo, - uv_getrusage, - uv_getrusage_thread, - uv_gettimeofday, - uv_guess_handle, - uv_handle_get_data, - uv_handle_get_loop, - uv_handle_get_type, - uv_handle_set_data, - uv_handle_size, - uv_handle_type_name, - uv_has_ref, - uv_hrtime, - uv_idle_init, - uv_idle_start, - uv_idle_stop, - uv_if_indextoiid, - uv_if_indextoname, - uv_inet_ntop, - uv_inet_pton, - uv_interface_addresses, - uv_ip_name, - uv_ip4_addr, - uv_ip4_name, - uv_ip6_addr, - uv_ip6_name, - uv_is_active, - uv_is_closing, - uv_is_readable, - uv_is_writable, - uv_key_create, - uv_key_delete, - uv_key_get, - uv_key_set, - uv_kill, - uv_library_shutdown, - uv_listen, - uv_loadavg, - uv_loop_alive, - uv_loop_close, - uv_loop_configure, - uv_loop_delete, - uv_loop_fork, - uv_loop_get_data, - uv_loop_init, - uv_loop_new, - uv_loop_set_data, - uv_loop_size, - uv_metrics_idle_time, - uv_metrics_info, - uv_mutex_destroy, - uv_mutex_init, - uv_mutex_init_recursive, - uv_mutex_lock, - uv_mutex_trylock, - uv_mutex_unlock, - uv_now, - uv_once, - uv_open_osfhandle, - uv_os_environ, - uv_os_free_environ, - uv_os_free_group, - uv_os_free_passwd, - uv_os_get_group, - uv_os_get_passwd, - uv_os_get_passwd2, - uv_os_getenv, - uv_os_gethostname, - uv_os_getpid, - uv_os_getppid, - uv_os_getpriority, - uv_os_homedir, - uv_os_setenv, - uv_os_setpriority, - uv_os_tmpdir, - uv_os_uname, - uv_os_unsetenv, - uv_pipe, - uv_pipe_bind, - uv_pipe_bind2, - uv_pipe_chmod, - uv_pipe_connect, - uv_pipe_connect2, - uv_pipe_getpeername, - uv_pipe_getsockname, - uv_pipe_init, - uv_pipe_open, - uv_pipe_pending_count, - uv_pipe_pending_instances, - uv_pipe_pending_type, - uv_poll_init, - uv_poll_init_socket, - uv_poll_start, - uv_poll_stop, - uv_prepare_init, - uv_prepare_start, - uv_prepare_stop, - uv_print_active_handles, - uv_print_all_handles, - uv_process_get_pid, - uv_process_kill, - uv_queue_work, - uv_random, - uv_read_start, - uv_read_stop, - uv_recv_buffer_size, - uv_ref, - uv_replace_allocator, - uv_req_get_data, - uv_req_get_type, - uv_req_set_data, - uv_req_size, - uv_req_type_name, - uv_resident_set_memory, - uv_run, - uv_rwlock_destroy, - uv_rwlock_init, - uv_rwlock_rdlock, - uv_rwlock_rdunlock, - uv_rwlock_tryrdlock, - uv_rwlock_trywrlock, - uv_rwlock_wrlock, - uv_rwlock_wrunlock, - uv_sem_destroy, - uv_sem_init, - uv_sem_post, - uv_sem_trywait, - uv_sem_wait, - uv_send_buffer_size, - uv_set_process_title, - uv_setup_args, - uv_shutdown, - uv_signal_init, - uv_signal_start, - uv_signal_start_oneshot, - uv_signal_stop, - uv_sleep, - uv_socketpair, - uv_spawn, - uv_stop, - uv_stream_get_write_queue_size, - uv_stream_set_blocking, - uv_strerror, - uv_strerror_r, - uv_tcp_bind, - uv_tcp_close_reset, - uv_tcp_connect, - uv_tcp_getpeername, - uv_tcp_getsockname, - uv_tcp_init, - uv_tcp_init_ex, - uv_tcp_keepalive, - uv_tcp_nodelay, - uv_tcp_open, - uv_tcp_simultaneous_accepts, - uv_thread_create, - uv_thread_create_ex, - uv_thread_detach, - uv_thread_equal, - uv_thread_getaffinity, - uv_thread_getcpu, - uv_thread_getname, - uv_thread_getpriority, - uv_thread_join, - uv_thread_self, - uv_thread_setaffinity, - uv_thread_setname, - uv_thread_setpriority, - uv_timer_again, - uv_timer_get_due_in, - uv_timer_get_repeat, - uv_timer_init, - uv_timer_set_repeat, - uv_timer_start, - uv_timer_stop, - uv_translate_sys_error, - uv_try_write, - uv_try_write2, - uv_tty_get_vterm_state, - uv_tty_get_winsize, - uv_tty_init, - uv_tty_reset_mode, - uv_tty_set_mode, - uv_tty_set_vterm_state, - uv_udp_bind, - uv_udp_connect, - uv_udp_get_send_queue_count, - uv_udp_get_send_queue_size, - uv_udp_getpeername, - uv_udp_getsockname, - uv_udp_init, - uv_udp_init_ex, - uv_udp_open, - uv_udp_recv_start, - uv_udp_recv_stop, - uv_udp_send, - uv_udp_set_broadcast, - uv_udp_set_membership, - uv_udp_set_multicast_interface, - uv_udp_set_multicast_loop, - uv_udp_set_multicast_ttl, - uv_udp_set_source_membership, - uv_udp_set_ttl, - uv_udp_try_send, - uv_udp_try_send2, - uv_udp_using_recvmmsg, - uv_unref, - uv_update_time, - uv_uptime, - uv_utf16_length_as_wtf8, - uv_utf16_to_wtf8, - uv_version, - uv_version_string, - uv_walk, - uv_write, - uv_write2, - uv_wtf8_length_as_utf16, - uv_wtf8_to_utf16, - ); - } - // V8API - // Hand-maintained for the same reason as the uv list above (no reflection - // over extern blocks) — keep in sync with the `v8_api` module. + // Hand-maintained (no reflection over extern blocks) — keep in sync with + // the `v8_api` module. #[cfg(not(windows))] { use v8_api::*; @@ -5147,7 +4474,7 @@ pub(crate) fn fix_dead_code_elimination() { _ZN2v812api_internal23GetFunctionTemplateDataEPNS_7IsolateENS_5LocalINS_4DataEEE, _ZNK2v88Function7GetNameEv, _ZNK2v85Value10IsFunctionEv, _ZNK2v85Value5IsMapEv, _ZNK2v85Value7IsArrayEv, _ZNK2v85Value7IsInt32Ev, _ZNK2v85Value8IsBigIntEv, - _ZN2v812api_internal17FromJustIsNothingEv, uv_os_getpid, uv_os_getppid, + _ZN2v812api_internal17FromJustIsNothingEv, _ZN2v87Integer3NewEPNS_7IsolateEi, _ZN2v87Integer15NewFromUnsignedEPNS_7IsolateEj, _ZNK2v87Integer5ValueEv, diff --git a/src/runtime/node.rs b/src/runtime/node.rs index 73d5e8fa5b61..9029d046a6fa 100644 --- a/src/runtime/node.rs +++ b/src/runtime/node.rs @@ -2,7 +2,7 @@ // Rust only compiles a `.rs` file if it is reachable via a `mod` declaration — // `#[no_mangle]` alone does NOT make an orphaned file link. Every Windows-only -// sibling (`uv_signal_handle_windows`, `win_watcher`) must have a +// sibling (`windows_signal`) must have a // `#[cfg(windows)] pub mod` entry here or its C-ABI exports will be missing at // link time. @@ -82,15 +82,6 @@ pub(crate) mod fs; // fs.watch() / fs.watchFile() backends — declared here so `fs::watch` / // `fs::watch_file` can reach the real `Arguments` / `FSWatcher` / // `StatWatcher` types instead of opaque local stand-ins. -#[cfg(not(windows))] -#[path = "node/path_watcher.rs"] -pub(crate) mod path_watcher; -#[cfg(windows)] -#[path = "node/win_watcher.rs"] -pub(crate) mod win_watcher; -// Force-references `Bun__UVSignalHandle__init` / `Bun__UVSignalHandle__close` -// for C++ (`src/jsc/bindings/BunProcess.cpp`). Must be `mod`-declared or the -// `#[no_mangle]` exports are never compiled into the binary. #[path = "node/memory_pressure.rs"] pub(crate) mod memory_pressure; #[path = "node/node_fs_binding.rs"] @@ -99,9 +90,11 @@ pub(crate) mod node_fs_binding; pub(crate) mod node_fs_stat_watcher; #[path = "node/node_fs_watcher.rs"] pub(crate) mod node_fs_watcher; +#[path = "node/path_watcher.rs"] +pub(crate) mod path_watcher; #[cfg(windows)] -#[path = "node/uv_signal_handle_windows.rs"] -pub(crate) mod uv_signal_handle_windows; +#[path = "node/windows_signal.rs"] +pub(crate) mod windows_signal; // Type defs + non-JSC FFI bodies are live; every `#[bun_jsc::host_fn]` / // `#[bun_jsc::JsClass]` item is wrapped in ` mod _impl` inside @@ -156,12 +149,5 @@ pub(crate) mod zlib { // ─── submodule re-exports ───────────────────────────────────────────────── -#[cfg(unix)] -pub(crate) type uid_t = libc::uid_t; -#[cfg(not(unix))] -pub(crate) type uid_t = bun_sys::windows::libuv::uv_uid_t; - -#[cfg(unix)] -pub(crate) type gid_t = libc::gid_t; -#[cfg(not(unix))] -pub(crate) type gid_t = bun_sys::windows::libuv::uv_gid_t; +pub(crate) type uid_t = u32; +pub(crate) type gid_t = u32; diff --git a/src/runtime/node/Stat.rs b/src/runtime/node/Stat.rs index e5b9e1d66750..0e0cd51122a3 100644 --- a/src/runtime/node/Stat.rs +++ b/src/runtime/node/Stat.rs @@ -3,9 +3,8 @@ use bun_core::Timespec; use bun_jsc::{CallFrame, JSGlobalObject, JSValue, JsResult}; -// `bun.sys.PosixStat` — uv-shaped stat struct. Re-exported from `bun_sys` now -// that the crate declares it; `PosixStat::init(&bun_sys::Stat)` handles the -// libc-stat → uv_stat_t field copy on both POSIX and Windows there. +// `PosixStat::init(&bun_sys::Stat)` normalises the platform stat struct into +// one shape with 64-bit fields and `Timespec` times. pub(crate) use bun_sys::PosixStat; /// Stats and BigIntStats classes from node:fs. `BIG` selects BigIntStats vs Stats. @@ -24,7 +23,11 @@ impl StatType { Self { value: *stat_ } } - /// Matches Node's `static_cast` of stat times: 32-bit wrap on win32, signed-preserving elsewhere. + /// Node on Windows casts the seconds and nanoseconds of a stat time to + /// `unsigned long`, 32 bits there (`SET_FIELD_WITH_TIME_STAT` in + /// node_file-inl.h), so a time outside 1970..2106 wraps: a file dated 1960 + /// reads as 2096. `Stats` and `BigIntStats` report the same numbers as Node; + /// programs compare them with what Node stored or reported. #[inline] fn timespec_parts(ts: StatTimespec) -> (i64, i64) { #[cfg(windows)] diff --git a/src/runtime/node/StatFS.rs b/src/runtime/node/StatFS.rs index 91d3bc314068..85368a126d81 100644 --- a/src/runtime/node/StatFS.rs +++ b/src/runtime/node/StatFS.rs @@ -1,14 +1,6 @@ //! StatFS and BigIntStatFS classes from node:fs use bun_jsc::{JSGlobalObject, JSValue, JsResult}; -// On POSIX this is `libc::statfs`; on Windows it's `uv_statfs_t` (the value -// `sys_uv::statfs` returns / `uv_fs_statfs` writes into `req.ptr`). Field -// names match (`f_type`/`f_bsize`/…); widths differ (u64 vs platform-specific) -// but `init` widens each field with `as i64` so either shape works. -#[cfg(unix)] -pub(crate) type RawStatFS = libc::statfs; -#[cfg(not(unix))] -pub(crate) type RawStatFS = bun_sys::StatFS; macro_rules! define_statfs_type { ($name:ident, big = $big:expr) => { @@ -60,7 +52,7 @@ macro_rules! define_statfs_type { )) } - pub(crate) fn init(statfs_: &RawStatFS) -> Self { + pub(crate) fn init(statfs_: &bun_sys::StatFS) -> Self { #[cfg(any( target_os = "linux", target_os = "android", @@ -140,7 +132,7 @@ pub(crate) enum StatFS { impl StatFS { #[inline] - pub(crate) fn init(stat_: &RawStatFS, big: bool) -> StatFS { + pub(crate) fn init(stat_: &bun_sys::StatFS, big: bool) -> StatFS { if big { StatFS::Big(StatFSBig::init(stat_)) } else { diff --git a/src/runtime/node/dir_iterator.rs b/src/runtime/node/dir_iterator.rs index 9a5f56a5e834..006fa92fdfeb 100644 --- a/src/runtime/node/dir_iterator.rs +++ b/src/runtime/node/dir_iterator.rs @@ -598,7 +598,7 @@ mod platform { if rc == w::NTSTATUS::INVALID_PARAMETER { sys::syslog!("NtQueryDirectoryFile({}) = INVALID_PARAMETER", self.dir); return Err(sys::Error::from_code( - SystemErrno::ENOTDIR.to_e(), + SystemErrno::ENOTDIR, Tag::NtQueryDirectoryFile, )); } diff --git a/src/runtime/node/memory_pressure.rs b/src/runtime/node/memory_pressure.rs index f5c89fdd6c19..68435cf44166 100644 --- a/src/runtime/node/memory_pressure.rs +++ b/src/runtime/node/memory_pressure.rs @@ -358,10 +358,11 @@ mod windows { use bun_jsc::JSGlobalObject; use bun_jsc::virtual_machine::VirtualMachine; - type HANDLE = *mut c_void; - type BOOL = i32; - type DWORD = u32; - const WAIT_OBJECT_0: DWORD = 0; + use bun_sys::windows::kernel32::{ + CreateEventW, SetEvent, WaitForMultipleObjects, WaitForSingleObject, + }; + use bun_sys::windows::{CloseHandle, DWORD, HANDLE, WAIT_OBJECT_0}; + const LOW_MEMORY_RESOURCE_NOTIFICATION: i32 = 0; /// The notification handle stays signalled while memory is low; after /// posting once we wait on `shutdown` alone for this long before @@ -370,16 +371,6 @@ mod windows { unsafe extern "system" { fn CreateMemoryResourceNotification(kind: i32) -> HANDLE; - fn CreateEventW( - attrs: *mut c_void, - manual_reset: BOOL, - initial: BOOL, - name: *const u16, - ) -> HANDLE; - fn SetEvent(h: HANDLE) -> BOOL; - fn WaitForSingleObject(h: HANDLE, ms: DWORD) -> DWORD; - fn WaitForMultipleObjects(n: DWORD, h: *const HANDLE, wait_all: BOOL, ms: DWORD) -> DWORD; - fn CloseHandle(h: HANDLE) -> BOOL; } /// Owns a kernel HANDLE; closes on drop. @@ -420,8 +411,7 @@ mod windows { unsafe { drop(bun_core::heap::take(task.as_ptr())) }; break; } - // SAFETY: `shutdown` is valid for the thread's lifetime. - if unsafe { WaitForSingleObject(handles[0], HOLDOFF_MS) } == WAIT_OBJECT_0 { + if WaitForSingleObject(handles[0], HOLDOFF_MS) == WAIT_OBJECT_0 { break; } } diff --git a/src/runtime/node/node_cluster_binding.rs b/src/runtime/node/node_cluster_binding.rs index 8eb044595d0e..ce3e3ad3351e 100644 --- a/src/runtime/node/node_cluster_binding.rs +++ b/src/runtime/node/node_cluster_binding.rs @@ -105,7 +105,7 @@ pub(crate) fn send_helper_primary(global: &JSGlobalObject, frame: &CallFrame) -> if raw_fd < 0 { return Ok(JSValue::NULL); } - bun_sys::Fd::from_uv(raw_fd) + bun_sys::Fd::from_crt(raw_fd) }; #[cfg(windows)] let native_fd = { @@ -368,7 +368,8 @@ pub(crate) fn cluster_raw_bind(global: &JSGlobalObject, frame: &CallFrame) -> Js &mut err, ) }; - const WSAEADDRINUSE: core::ffi::c_int = 10048; + const WSAEADDRINUSE: core::ffi::c_int = + bun_sys::windows::Win32Error::WSAEADDRINUSE.0 as core::ffi::c_int; if fd == bun_uws::LIBUS_SOCKET_DESCRIPTOR::MAX && err != WSAEADDRINUSE { if let Some(v4) = fallback_host { let mut err2: core::ffi::c_int = 0; @@ -389,13 +390,12 @@ pub(crate) fn cluster_raw_bind(global: &JSGlobalObject, frame: &CallFrame) -> Js } } if fd == bun_uws::LIBUS_SOCKET_DESCRIPTOR::MAX { - // SAFETY: pure translation function. - let uv_err = unsafe { bun_libuv_sys::uv_translate_sys_error(err) }; - return Ok(JSValue::js_number_from_int32(if uv_err != 0 { - uv_err - } else { - -4094 - })); + // JS reads 0 as success; a failure that left no code is UNKNOWN. + let code = match bun_errno::Bun__translateWin32ErrorToUV(err as u32) { + 0 => bun_errno::uv_codes::UV_UNKNOWN, + code => code, + }; + return Ok(JSValue::js_number_from_int32(code)); } let obj = JSValue::create_empty_object(global, 2); diff --git a/src/runtime/node/node_fs.rs b/src/runtime/node/node_fs.rs index eb4d3298dae2..e9f165476586 100644 --- a/src/runtime/node/node_fs.rs +++ b/src/runtime/node/node_fs.rs @@ -13,6 +13,7 @@ use bun_core::{String as BunString, Utf8WithString, ZStr}; use bun_event_loop::AnyTaskWithExtraContext::AnyTaskWithExtraContext; use bun_io::KeepAlive; use bun_jsc::AbortSignal; +use bun_jsc::FdUse; use bun_jsc::debugger::AsyncTaskTracker; use bun_jsc::virtual_machine::VirtualMachine; use bun_jsc::{ @@ -21,7 +22,7 @@ use bun_jsc::{ }; use bun_paths::{self as paths, OSPathBuffer, OSPathChar, OSPathSliceZ, PathBuffer}; use bun_sys::FdExt as _; -use bun_sys::{self as sys, E, Fd as FD, Maybe, Mode, SystemErrno}; +use bun_sys::{self as sys, E, Fd as FD, Maybe, Mode, PosixStat, SystemErrno}; use bun_threading::UnboundedQueue; use bun_threading::work_pool::{IntrusiveWorkTask as _, Task as WorkPoolTask, WorkPool}; @@ -34,7 +35,6 @@ use bun_threading::work_pool::{IntrusiveWorkTask as _, Task as WorkPoolTask, Wor pub(crate) trait MaybeSysResultExt: Sized { #[cfg(not(target_os = "freebsd"))] fn get_errno(&self) -> E; - fn errno_sys(rc: Rc, syscall: sys::Tag) -> Option; fn errno_sys_fd(rc: Rc, syscall: sys::Tag, fd: FD) -> Option; fn errno_sys_p( rc: Rc, @@ -59,17 +59,6 @@ impl MaybeSysResultExt for Maybe { } } #[inline] - fn errno_sys(rc: Rc, syscall: sys::Tag) -> Option { - match sys::get_errno(rc) { - E::SUCCESS => None, - e => Some(Err(sys::Error { - errno: (e as u16), - syscall, - ..Default::default() - })), - } - } - #[inline] fn errno_sys_fd(rc: Rc, syscall: sys::Tag, fd: FD) -> Option { match sys::get_errno(rc) { E::SUCCESS => None, @@ -117,17 +106,6 @@ impl MaybeSysResultExt for Maybe { } } -/// Convert the runtime `node::time_like::TimeLike` (== `libc::timespec` on -/// POSIX) into the `bun_sys::TimeLike` data shape that the `Syscall::*utimens` -/// wrappers consume. On Windows both are `f64`. -#[cfg(not(windows))] -#[inline] -fn to_sys_time_like(t: super::time_like::TimeLike) -> sys::TimeLike { - sys::TimeLike { - sec: t.tv_sec as i64, - nsec: t.tv_nsec as i64, - } -} use bun_event_loop::ConcurrentTask; /// `webcore.Blob.SizeType` — logically a 52-bit unsigned integer. @@ -203,11 +181,6 @@ pub(crate) use super::node_fs_constant as constants; pub(crate) use super::node_fs_stat_watcher as StatWatcher; pub(crate) use super::node_fs_watcher as Watcher; -/// `Binding` is the JSC-class instance that owns the per-thread `NodeFS` -/// (`super::node_fs_binding::Binding`). Re-exported so the async `create()` -/// entry points keep their `&mut Binding` signature. -pub(crate) use super::node_fs_binding::Binding; - /// `jsc.JSPromise.Strong` — re-exported under its Rust crate name: /// `bun_jsc::js_promise::Strong` / the `JSPromiseStrong` alias. use bun_jsc::JSPromiseStrong; @@ -216,25 +189,13 @@ use super::dir_iterator as DirIterator; #[cfg(not(windows))] use bun_resolver::fs::FileSystem; -// On POSIX the libuv-backed code paths (`UVFSRequest`, `uv_fs_*`) are absent: -// `UVFSRequest` aliases `AsyncFSTask` and every `uv::*` reference is gated -// behind `#[cfg(windows)]`. There is intentionally **no** POSIX stub module -// here so misuse is a compile error, not a silent null. #[cfg(windows)] -use bun_sys::{ - ReturnCodeExt as _, - windows::{self, libuv as uv}, -}; - -// Syscall = `bun_sys::sys_uv` on Windows, `bun_sys` otherwise -#[cfg(not(windows))] -use bun_sys as Syscall; -#[cfg(windows)] -use bun_sys::sys_uv as Syscall; +use bun_sys::windows; // Kernel limit on iovec count for a single readv(2)/writev(2). libuv's // `uv__getiovmax()` prefers compile-time `IOV_MAX`; Linux headers spell it -// `UIO_MAXIOV`. Windows has no kernel iovec limit (sys_uv chunks internally). +// `UIO_MAXIOV`. Windows has no kernel iovec limit (`bun_sys` issues one +// ReadFile/WriteFile per buffer). #[cfg(any(target_os = "linux", target_os = "android"))] const IOV_MAX: usize = libc::UIO_MAXIOV as usize; #[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))] @@ -242,47 +203,6 @@ const IOV_MAX: usize = libc::IOV_MAX as usize; #[cfg(windows)] const IOV_MAX: usize = core::ffi::c_uint::MAX as usize; -/// In-place RAII wrapper for a libuv `fs_t` request. -/// -/// `scopeguard::guard(fs_t, |mut r| r.deinit())` is *wrong* here: its `Drop` -/// `ManuallyDrop::take`s the value into the closure parameter, relocating the -/// ~440-byte request to a new stack address before `uv_fs_req_cleanup` runs. -/// libuv stores self-referential pointers (`req->fs.info.bufs` may point at -/// `req->fs.info.bufsml`), so the request must not move between init and -/// cleanup. A real `Drop` impl runs in place at the original address. -#[cfg(windows)] -#[repr(transparent)] -struct UvFsReq(uv::fs_t); -#[cfg(windows)] -impl UvFsReq { - #[inline] - fn new() -> Self { - Self(uv::fs_t::uninitialized()) - } -} -#[cfg(windows)] -impl Drop for UvFsReq { - #[inline] - fn drop(&mut self) { - self.0.deinit(); - } -} -#[cfg(windows)] -impl core::ops::Deref for UvFsReq { - type Target = uv::fs_t; - #[inline] - fn deref(&self) -> &uv::fs_t { - &self.0 - } -} -#[cfg(windows)] -impl core::ops::DerefMut for UvFsReq { - #[inline] - fn deref_mut(&mut self) -> &mut uv::fs_t { - &mut self.0 - } -} - // ────────────────────────────────────────────────────────────────────────── // Local cross-crate shims // @@ -297,21 +217,6 @@ fn without_nt_prefix(path: &[T]) -> &[T] { bun_paths::string_paths::without_nt_prefix(path) } -/// Empty `OSPathChar` literal. -/// Only the empty-string case is used in this file. `OSPathSliceZ` is a DST -/// (`ZStr`/`WStr`), so callers borrow it. -#[inline] -fn os_path_literal_empty() -> &'static OSPathSliceZ { - #[cfg(windows)] - { - bun_core::WStr::EMPTY - } - #[cfg(not(windows))] - { - ZStr::EMPTY - } -} - #[inline] fn standalone_module_graph() -> Option<&'static bun_standalone_graph::Graph> { bun_standalone_graph::Graph::get_ref() @@ -361,13 +266,6 @@ fn encoding_to_node(e: Encoding) -> bun_core::NodeEncoding { } } -/// uv-shaped stat struct. `Stats::init` (from -/// `super::stat`) takes its sibling `PosixStat` by reference, so route through -/// that definition rather than `bun_sys::PosixStat` to keep the parameter -/// type exact. Both are `#[repr(C)]` mirrors of `uv_stat_t`; once -/// `super::stat` swaps to `pub use bun_sys::PosixStat` this alias collapses. -use super::stat::PosixStat; - /// Node `fs.rm` mapping helper — maps an error-set *name* string back to a /// `crate::Error` variant so the callers' `map_anyerror_to_errno*` tables (which /// match on `err.name()`) keep round-tripping. @@ -410,19 +308,11 @@ const CLONE_NOFOLLOW: u32 = 0x0001; /// Path-length field width. type PathInt = u32; -/// `Syscall.mkdirOSPath` / `Syscall.openatOSPath` — on POSIX `OSPathSliceZ` is -/// `&ZStr`, so these are pure forwarders to the byte-path entry points. On -/// Windows they would route through `sys_uv` (handled by `#[cfg(windows)]` -/// branches at the call sites). +/// `mkdir` on an OS-width path; `mode` is unused on Windows. #[cfg(not(windows))] #[inline] fn mkdir_os_path(path: &OSPathSliceZ, mode: Mode) -> Maybe<()> { - Syscall::mkdir(path, mode) -} -#[cfg(not(windows))] -#[inline] -fn openat_os_path(dirfd: FD, path: &OSPathSliceZ, flags: i32, mode: Mode) -> Maybe { - Syscall::openat(dirfd, path, flags, mode) + sys::mkdir(path, mode) } #[cfg(windows)] #[inline] @@ -430,11 +320,6 @@ fn mkdir_os_path(path: &OSPathSliceZ, mode: Mode) -> Maybe<()> { let _ = mode; sys::mkdir_w(path) } -#[cfg(windows)] -#[inline] -fn openat_os_path(dirfd: FD, path: &OSPathSliceZ, flags: i32, mode: Mode) -> Maybe { - sys::openat_windows(dirfd, path.as_slice(), flags, mode) -} /// Check whether a directory exists at `(fd, path)` — dispatches on path element width. On /// Windows `OSPathSliceZ` is already `&WStr`, so forward to the wide overload @@ -475,514 +360,23 @@ pub(crate) const DEFAULT_PERMISSION: Mode = 0; // ────────────────────────────────────────────────────────────────────────── // Async task type aliases // ────────────────────────────────────────────────────────────────────────── -// AsyncFSTask / UVFSRequest / NewAsyncCpTask / AsyncReaddirRecursiveTask are +// AsyncFSTask / NewAsyncCpTask / AsyncReaddirRecursiveTask are // the thread-pool wrappers that back every `fs.promises.*` call (and the shell // `cp` builtin). mod _async_tasks { use super::*; + /// The tasks created outside `node_fs_bindings!`. pub(crate) mod async_ { use super::*; - pub(crate) type Access = - AsyncFSTask, { NodeFSFunctionEnum::Access }>; - pub(crate) type AppendFile = AsyncFSTask< - ret::AppendFile, - args::AppendFile<'static>, - { NodeFSFunctionEnum::AppendFile }, - >; - pub(crate) type Chmod = - AsyncFSTask, { NodeFSFunctionEnum::Chmod }>; - pub(crate) type Chown = - AsyncFSTask, { NodeFSFunctionEnum::Chown }>; - pub(crate) type Close = UVFSRequest; - pub(crate) type CopyFile = - AsyncFSTask, { NodeFSFunctionEnum::CopyFile }>; - pub(crate) type Exists = - AsyncFSTask, { NodeFSFunctionEnum::Exists }>; - pub(crate) type Fchmod = - AsyncFSTask; - pub(crate) type Fchown = - AsyncFSTask; - pub(crate) type Fdatasync = - AsyncFSTask; pub(crate) type Fstat = AsyncFSTask; - pub(crate) type Fsync = AsyncFSTask; - pub(crate) type Ftruncate = - AsyncFSTask; - pub(crate) type Futimes = - AsyncFSTask; - pub(crate) type Lchmod = - AsyncFSTask, { NodeFSFunctionEnum::Lchmod }>; - pub(crate) type Lchown = - AsyncFSTask, { NodeFSFunctionEnum::Lchown }>; - pub(crate) type Link = - AsyncFSTask, { NodeFSFunctionEnum::Link }>; - pub(crate) type Lstat = - AsyncFSTask, { NodeFSFunctionEnum::Lstat }>; - pub(crate) type Lutimes = - AsyncFSTask, { NodeFSFunctionEnum::Lutimes }>; - pub(crate) type Mkdir = - AsyncFSTask, { NodeFSFunctionEnum::Mkdir }>; - pub(crate) type Mkdtemp = - AsyncFSTask, { NodeFSFunctionEnum::Mkdtemp }>; - pub(crate) type Open = - UVFSRequest, { NodeFSFunctionEnum::Open }>; - pub(crate) type Read = UVFSRequest; pub(crate) type Readdir = AsyncFSTask, { NodeFSFunctionEnum::Readdir }>; - pub(crate) type ReadFile = - AsyncFSTask, { NodeFSFunctionEnum::ReadFile }>; - pub(crate) type Readlink = - AsyncFSTask, { NodeFSFunctionEnum::Readlink }>; - pub(crate) type Readv = UVFSRequest; - pub(crate) type Realpath = - AsyncFSTask, { NodeFSFunctionEnum::Realpath }>; - pub(crate) type RealpathNonNative = AsyncFSTask< - ret::Realpath, - args::Realpath<'static>, - { NodeFSFunctionEnum::RealpathNonNative }, - >; - pub(crate) type Rename = - AsyncFSTask, { NodeFSFunctionEnum::Rename }>; - pub(crate) type Rm = AsyncFSTask, { NodeFSFunctionEnum::Rm }>; - pub(crate) type Rmdir = - AsyncFSTask, { NodeFSFunctionEnum::Rmdir }>; pub(crate) type Stat = AsyncFSTask, { NodeFSFunctionEnum::Stat }>; - pub(crate) type Symlink = - AsyncFSTask, { NodeFSFunctionEnum::Symlink }>; - pub(crate) type Truncate = - AsyncFSTask, { NodeFSFunctionEnum::Truncate }>; pub(crate) type Unlink = AsyncFSTask, { NodeFSFunctionEnum::Unlink }>; - pub(crate) type Utimes = - AsyncFSTask, { NodeFSFunctionEnum::Utimes }>; - pub(crate) type Write = - UVFSRequest, { NodeFSFunctionEnum::Write }>; - pub(crate) type WriteFile = AsyncFSTask< - ret::WriteFile, - args::WriteFile<'static>, - { NodeFSFunctionEnum::WriteFile }, - >; - pub(crate) type Writev = - UVFSRequest; - pub(crate) type Statfs = - UVFSRequest, { NodeFSFunctionEnum::Statfs }>; - - const _: () = assert!(ReadFile::HAVE_ABORT_SIGNAL); - const _: () = assert!(WriteFile::HAVE_ABORT_SIGNAL); - - #[cfg(windows)] - /// Used internally. Not from JavaScript. - pub(crate) struct AsyncMkdirp { - pub(crate) completion_ctx: *mut (), - /// Pool thread; `ticket` is this task's, for the callee to post its - /// hop back through. - pub(crate) completion: fn(*mut (), Maybe<()>, &bun_jsc::Ticket), - /// Memory is not owned by this struct - pub path: *const [u8], // BORROW: not owned - pub(crate) ticket: bun_jsc::Ticket, - pub task: WorkPoolTask, - } - - #[cfg(windows)] - bun_threading::owned_task!(AsyncMkdirp, task); - - #[cfg(windows)] - impl AsyncMkdirp { - /// Heap-allocate and hand the task to the work pool, which owns the - /// allocation and frees it after `run_owned` returns. - pub(crate) fn schedule(init: AsyncMkdirp) { - WorkPool::schedule_new(init); - } - - #[allow(clippy::boxed_local)] - fn run_owned(self: Box) { - let mut node_fs = NodeFS::default(); - // SAFETY: the scheduling caller keeps `path` alive until `completion` - // runs (it points into caller-owned state, not this box). - let path = unsafe { &*self.path }; - let result = node_fs.mkdir_recursive(&args::Mkdir { - path: PathLike::borrowed(path), - recursive: true, - ..Default::default() - }); - match result { - Err(err) => { - (self.completion)( - self.completion_ctx, - // `with_path` already clones into a fresh `Box<[u8]>`; pass the - // existing path slice. - Err(err.with_path(&err.path)), - &self.ticket, - ); - } - Ok(_) => { - (self.completion)(self.completion_ctx, Ok(()), &self.ticket); - } - } - } - } - } - - // ────────────────────────────────────────────────────────────────────────── - // NewUVFSRequest — Windows-only async wrapper around libuv fs requests. - // On non-Windows it is just AsyncFSTask. - // ────────────────────────────────────────────────────────────────────────── - - #[cfg(not(windows))] - pub(crate) type UVFSRequest = AsyncFSTask; - - #[cfg(windows)] - pub(crate) struct UVFSRequest { - pub(crate) completion: FsCompletion, - pub args: ThreadIsolated, - pub(crate) global_object: bun_ptr::BackRef, - pub(crate) req: uv::fs_t, - pub(crate) result: Maybe, - pub(crate) r#ref: KeepAlive, - pub(crate) tracker: AsyncTaskTracker, - /// The context of the script that called. - pub(crate) context: bun_jsc::ContextId, - } - - #[cfg(windows)] - impl UVFSRequest - where - Op<{ F }>: NodeFSDispatch, - { - /// Deref the raw `global_object` pointer. - /// - /// Invariant: set from a live `&JSGlobalObject` in `create()` and never - /// null; the JSC global outlives every task (JSC_BORROW per LIFETIMES.tsv). - #[inline] - pub(crate) fn global_object(&self) -> &JSGlobalObject { - self.global_object.get() - } - - /// `callback`: as for [`AsyncFSTask::create`]. - pub(crate) fn create( - cx: &bun_jsc::JsThread<'_>, - binding: &Binding, - task_args: ThreadIsolated, - vm: &mut VirtualMachine, - callback: Option, - ) -> JSValue { - let task = Box::new(Self { - completion: FsCompletion::new(cx.global(), callback), - args: task_args, - // Sentinel — overwritten by `uv_callback` (or the early-return arms - // below) before any read on the JS thread. `Maybe` is - // `Result` and may be niche-optimised for arbitrary - // `R`; never construct an all-zero `Result` value. - result: Err(sys::Error::default()), - global_object: bun_ptr::BackRef::new(cx.global()), - req: bun_core::ffi::zeroed(), - r#ref: KeepAlive::default(), - tracker: AsyncTaskTracker::init(vm), - context: cx.context().id(), - }); - // Transfer ownership to libuv: the box outlives the async request and is - // reclaimed in `destroy()` (from `run_from_js_thread`). `heap::release` - // names that hand-off — it is `Box::leak` under the hood; the reclaim - // happens in `destroy()`, not in this scope. - let task: &mut Self = bun_core::heap::release(task); - // KeepAlive::ref_ now takes the type-erased aio EventLoopCtx; the JS - // event loop is the only one that owns AsyncFSTask/UVFSRequest. - task.r#ref.ref_(bun_io::js_vm_ctx()); - task.tracker.did_schedule(cx.global()); - - let loop_ = uv::Loop::get(); - task.req.data = core::ptr::from_mut::(task).cast::(); - - // The match resolves at compile time (`F` is a const generic), but - // each arm's body needs `A` re-asserted to its concrete `args::*` - // type — same identity-cast pattern as `NodeFS::dispatch` (per the - // `async_::*` aliases, `A == $Args` for the matched `F`). - macro_rules! args_as { - ($Args:ty) => {{ - debug_assert_eq!(core::mem::size_of::(), core::mem::size_of::<$Args>()); - // SAFETY: identity cast — `A == $Args` for this `F` (see `async_::*`). - // `ThreadIsolated` is `repr(transparent)`; deref through it for the inner `A`. - unsafe { &*(&*task.args as *const A as *const $Args) } - }}; - } - match F { - NodeFSFunctionEnum::Open => { - let args: &args::Open = args_as!(args::Open); - let path = if strings::eql_comptime(args.path.slice(), b"/dev/null") { - ZStr::from_static(b"\\\\.\\NUL\0") - } else { - // SAFETY (R-2): single-JS-thread `JsCell` projection of the - // scratch path buffer; the borrow is held only across the - // libuv enqueue below (which copies `path` internally) and - // never across a JS re-entry point. - args.path - .slice_z(unsafe { &mut binding.node_fs.get_mut().sync_error_buf }) - }; - let mut flags: c_int = args.flags.as_int(); - flags = uv::O::from_bun_o(flags); - let mut mode: c_int = args.mode as c_int; - if mode == 0 { - mode = 0o644; - } - // SAFETY: libuv async request; `task.req` and `path` outlive the - // call (path is copied internally by libuv before return). - let rc = unsafe { - uv::uv_fs_open( - loop_, - &mut task.req, - path.as_ptr(), - flags, - mode, - Some(Self::uv_callback), - ) - }; - debug_assert!(rc == uv::ReturnCode::ZERO); - sys::syslog!( - "uv open({}, {}, {}) = scheduled", - ::bstr::BStr::new(path.as_bytes()), - flags, - mode - ); - } - NodeFSFunctionEnum::Close => { - let args: &args::Close = args_as!(args::Close); - let fd = args.fd.uv(); - // SAFETY: libuv async request. - let rc = unsafe { - uv::uv_fs_close(loop_, &mut task.req, fd, Some(Self::uv_callback)) - }; - debug_assert!(rc == uv::ReturnCode::ZERO); - sys::syslog!("uv close({}) = scheduled", fd); - } - NodeFSFunctionEnum::Read => { - let args: &args::Read = args_as!(args::Read); - let fd = args.fd.uv(); - let buf = args.buffer.slice(); - let off = (buf.len()).min(args.offset as usize); - let buf = &buf[off..]; - let buf = &buf[..buf.len().min(args.length as usize)]; - let bufs = [uv::uv_buf_t::init(buf)]; - // SAFETY: libuv copies the iovec descriptor before return; the - // backing Buffer is pinned and rooted (`ReadBuffer::PinnedBuffer`). - let rc = unsafe { - uv::uv_fs_read( - loop_, - &mut task.req, - fd, - bufs.as_ptr(), - 1, - args.position.map(|p| p as i64).unwrap_or(-1), - Some(Self::uv_callback), - ) - }; - debug_assert!(rc == uv::ReturnCode::ZERO); - sys::syslog!("uv read({}) = scheduled", fd); - } - NodeFSFunctionEnum::Write => { - let args: &args::Write = args_as!(args::Write); - let fd = args.fd.uv(); - let buf = args.buffer.slice(); - let off = (buf.len()).min(args.offset as usize); - let buf = &buf[off..]; - let buf = &buf[..buf.len().min(args.length as usize)]; - let bufs = [uv::uv_buf_t::init(buf)]; - // SAFETY: see Read arm. - let rc = unsafe { - uv::uv_fs_write( - loop_, - &mut task.req, - fd, - bufs.as_ptr(), - 1, - args.position.map(|p| p as i64).unwrap_or(-1), - Some(Self::uv_callback), - ) - }; - debug_assert!(rc == uv::ReturnCode::ZERO); - sys::syslog!("uv write({}) = scheduled", fd); - } - NodeFSFunctionEnum::Readv => { - let args: &args::Readv = args_as!(args::Readv); - let fd = args.fd.uv(); - let bufs = &args.buffers.buffers; - let pos: i64 = args.position.map(|p| p as i64).unwrap_or(-1); - let sum: u64 = bufs.iter().map(|b| b.slice().len() as u64).sum(); - // SAFETY: `bufs` (Vec == Vec) lives in - // the leaked task; libuv copies the array before return. - let rc = unsafe { - uv::uv_fs_read( - loop_, - &mut task.req, - fd, - bufs.as_ptr().cast(), - c_uint::try_from(bufs.len()).expect("int cast"), - pos, - Some(Self::uv_callback), - ) - }; - debug_assert!(rc == uv::ReturnCode::ZERO); - sys::syslog!( - "uv readv({}, {:p}, {}, {}, {} total bytes) = scheduled", - fd, - bufs.as_ptr(), - bufs.len(), - pos, - sum - ); - } - NodeFSFunctionEnum::Writev => { - let args: &args::Writev = args_as!(args::Writev); - let fd = args.fd.uv(); - let bufs = &args.buffers.buffers; - if bufs.is_empty() { - // SAFETY: identity write — `R == ret::Writev == ret::Write` for this `F`. - unsafe { - core::ptr::write( - &mut task.result as *mut Maybe as *mut Maybe, - Ok(ret::Write { bytes_written: 0 }), - ) - }; - let task_ptr: *mut Self = task; - task.global_object() - .bun_vm() - .event_loop_mut() - .enqueue_task(bun_jsc::Task::init(task_ptr)); - return task.completion.value(); - } - let pos: i64 = args.position.map(|p| p as i64).unwrap_or(-1); - let sum: u64 = bufs.iter().map(|b| b.slice().len() as u64).sum(); - // SAFETY: see Readv arm. - let rc = unsafe { - uv::uv_fs_write( - loop_, - &mut task.req, - fd, - bufs.as_ptr().cast(), - c_uint::try_from(bufs.len()).expect("int cast"), - pos, - Some(Self::uv_callback), - ) - }; - debug_assert!(rc == uv::ReturnCode::ZERO); - sys::syslog!( - "uv writev({}, {:p}, {}, {}, {} total bytes) = scheduled", - fd, - bufs.as_ptr(), - bufs.len(), - pos, - sum - ); - } - NodeFSFunctionEnum::Statfs => { - let args: &args::StatFS = args_as!(args::StatFS); - // SAFETY (R-2): single-JS-thread `JsCell` projection; held only - // across the libuv enqueue (copies `path` internally). - let path = args - .path - .slice_z(unsafe { &mut binding.node_fs.get_mut().sync_error_buf }); - // SAFETY: libuv copies `path` internally before return. - let rc = unsafe { - uv::uv_fs_statfs( - loop_, - &mut task.req, - path.as_ptr(), - Some(Self::uv_callbackreq), - ) - }; - debug_assert!(rc == uv::ReturnCode::ZERO); - sys::syslog!("uv statfs({}) = ~~", ::bstr::BStr::new(path.as_bytes())); - } - _ => unreachable!("UVFSRequest type not implemented"), - } - - task.completion.value() - } - - extern "C" fn uv_callback(req: *mut uv::fs_t) { - // SAFETY: req points to a live uv::fs_t passed by libuv; cleanup is the documented pair - scopeguard::defer! { unsafe { uv::uv_fs_req_cleanup(req) } }; - // SAFETY: req.data was set to the Box::leak'd `*mut Self` in create() - let this: &mut Self = unsafe { bun_ptr::callback_ctx::((*req).data) }; - let mut node_fs = NodeFS::default(); - // `req` aliases `this.req` (see create(): `task.req.data = from_mut(task)`); once - // `this: &mut Self` is live, re-deriving through the raw `req` would create a - // second overlapping `&mut` (Stacked-Borrows UB). Go through `this.req` instead. - this.result = NodeFS::uv_dispatch::(&mut node_fs, &this.args, this.req.result); - let this_ptr: *mut Self = this; - this.global_object() - .bun_vm() - .event_loop_mut() - .enqueue_task(bun_jsc::Task::init(this_ptr)); - } - - extern "C" fn uv_callbackreq(req: *mut uv::fs_t) { - // Same as uv_callback but passes `req` through to the dispatch fn (statfs needs req.ptr). - // SAFETY: req points to a live uv::fs_t passed by libuv; cleanup is the documented pair - scopeguard::defer! { unsafe { uv::uv_fs_req_cleanup(req) } }; - // SAFETY: req.data was set to the Box::leak'd `*mut Self` in create() - let this: &mut Self = unsafe { bun_ptr::callback_ctx::((*req).data) }; - let mut node_fs = NodeFS::default(); - // `req` aliases `this.req`; once `this: &mut Self` is live, re-deriving `&mut *req` - // would overlap it (Stacked-Borrows UB). Go through `this.req` instead — disjoint-field - // borrow alongside `&this.args` / `this.result =`. Hoist the result read so it isn't - // evaluated after `&mut this.req` is formed in the same call expression. - let rc = this.req.result; - this.result = - NodeFS::uv_dispatch_req::(&mut node_fs, &this.args, &mut this.req, rc); - let this_ptr: *mut Self = this; - this.global_object() - .bun_vm() - .event_loop_mut() - .enqueue_task(bun_jsc::Task::init(this_ptr)); - } - - /// SAFETY: `this` must be the pointer Box::leak'd in `create()`. The body frees it before it calls back, so it is not `&mut self`. - pub(crate) unsafe fn run_from_js_thread(this: *mut Self) -> JsResult<()> { - // SAFETY: fn contract. The borrow ends before `destroy` below. - let (result, completion, global_ref, tracker) = unsafe { - let task = &mut *this; - ( - core::mem::replace(&mut task.result, Err(sys::Error::default())), - core::mem::replace( - &mut task.completion, - FsCompletion::Promise(JSPromiseStrong::empty()), - ), - task.global_object, - task.tracker, - ) - }; - // The arguments pin their buffers. A callback runs inside `resolve`/`reject`, and a pinned buffer cannot be transferred. - // SAFETY: fn contract. `this` is not used again. - unsafe { Self::destroy(this) }; - - let global_object = global_ref.get(); - let success = matches!(result, Ok(_)); - let converted = match result { - Err(err) => completion.error_to_js(global_object, &err), - Ok(res) => FsReturn::fs_to_js(res, global_object), - }; - completion.ensure_still_alive(); - - let _dispatch = tracker.dispatch(global_object); - - completion.settle(global_object, converted, success) - } - - /// SAFETY: `this` must be the pointer Box::leak'd in `create()`; called exactly once. - pub(crate) unsafe fn destroy(this: *mut Self) { - // SAFETY: caller guarantees `this` is the live Box-leaked allocation; - // reclaim ownership (paired with the Box::leak in create()). - let mut task = unsafe { bun_core::heap::take(this) }; - // A result nobody took (the request's context stopped: released unrun). - if let Ok(result) = core::mem::replace(&mut task.result, Err(sys::Error::default())) { - result.discard(); - } - // `bun_sys::Error` frees its path on Drop. - task.r#ref.unref(bun_io::js_vm_ctx()); - } } // ────────────────────────────────────────────────────────────────────────── @@ -1011,24 +405,43 @@ mod _async_tasks { fn signal(&self) -> Option<&AbortSignal> { None } + /// The file descriptor the operation is on, if it is on one. + fn fd_use(&self) -> Option { + None + } + } + + fn uses_fd(file: &PathOrFileDescriptor<'_>) -> Option { + match file { + PathOrFileDescriptor::Fd(fd) => Some(FdUse::Uses(*fd)), + PathOrFileDescriptor::Path(_) => None, + } + } + + fn appends_to_fd(file: &PathOrFileDescriptor<'_>) -> Option { + match file { + PathOrFileDescriptor::Fd(fd) => Some(FdUse::Appends(*fd)), + PathOrFileDescriptor::Path(_) => None, + } } /// Forward [`FsArgument`] to the inherent `from_js` each `args::*` struct - /// already defines. + /// already defines. `=> fd_use` is the body of [`FsArgument::fd_use`]. macro_rules! impl_fs_argument { - ( $( $ty:ty ),+ $(,)? ) => { + ( $( $ty:ty $( => |$this:ident| $fd_use:expr )? ),+ $(,)? ) => { $( // SAFETY: `from_js_async` parses paths and data thread-isolated / pinned // and rooted; the remaining fields are plain data. unsafe impl ThreadIsolatedArg for $ty {} impl FsArgument for $ty { #[inline] fn from_js(ctx: &JSGlobalObject, arguments: &mut ArgumentsSlice) -> JsResult { <$ty>::from_js(ctx, arguments) } + $( #[inline] fn fd_use(&self) -> Option { let $this = self; $fd_use } )? } )+ }; } impl_fs_argument!( args::Rename<'static>, - args::Truncate<'static>, + args::Truncate<'static> => |args| uses_fd(&args.path), args::Chown<'static>, args::Lutimes<'static>, args::Chmod<'static>, @@ -1051,21 +464,26 @@ mod _async_tasks { args::Cp<'static>, ); impl_fs_argument!( - args::FdVectorIo, - args::FTruncate, - args::Write<'static>, - args::Read, - args::Fchown, - args::FChmod, - args::Fstat, - args::Futimes, - args::FdataSync, - args::Fsync, - args::Close, + args::FdVectorIo => |args| Some(match args.position { + Some(_) => FdUse::Uses(args.fd), + None => FdUse::Appends(args.fd), + }), + args::FTruncate => |args| Some(FdUse::Uses(args.fd)), + args::Write<'static> => |args| Some(match args.position { + Some(_) => FdUse::Uses(args.fd), + None => FdUse::Appends(args.fd), + }), + args::Read => |args| Some(FdUse::Uses(args.fd)), + args::Fchown => |args| Some(FdUse::Uses(args.fd)), + args::FChmod => |args| Some(FdUse::Uses(args.fd)), + args::Fstat => |args| Some(FdUse::Uses(args.fd)), + args::Futimes => |args| Some(FdUse::Uses(args.fd)), + args::FdataSync => |args| Some(FdUse::Uses(args.fd)), + args::Fsync => |args| Some(FdUse::Uses(args.fd)), + args::Close => |args| Some(FdUse::Closes(args.fd)), ); - // `ReadFile`/`WriteFile` carry an `AbortSignal` field — opt them in so the - // `const _ = assert!(…::HAVE_ABORT_SIGNAL)` invariants in `async_` hold and - // `signal()` exposes it to `AsyncFSTask::run_from_js_thread`. + // `ReadFile`/`WriteFile`/`AppendFile` carry an `AbortSignal` field: opt them in + // so `signal()` exposes it to `AsyncFSTask::then`. // SAFETY: as for `impl_fs_argument!`; the `AbortSignal` ref is thread-safe. unsafe impl ThreadIsolatedArg for args::ReadFile<'static> {} // SAFETY: see above. @@ -1082,6 +500,10 @@ mod _async_tasks { fn signal(&self) -> Option<&AbortSignal> { self.signal.as_deref() } + #[inline] + fn fd_use(&self) -> Option { + uses_fd(&self.path) + } } impl FsArgument for args::WriteFile<'static> { const HAVE_ABORT_SIGNAL: bool = true; @@ -1093,6 +515,10 @@ mod _async_tasks { fn signal(&self) -> Option<&AbortSignal> { self.signal.as_deref() } + #[inline] + fn fd_use(&self) -> Option { + appends_to_fd(&self.file) + } } impl FsArgument for args::AppendFile<'static> { const HAVE_ABORT_SIGNAL: bool = true; @@ -1105,7 +531,13 @@ mod _async_tasks { fn signal(&self) -> Option<&AbortSignal> { self.0.signal.as_deref() } + #[inline] + fn fd_use(&self) -> Option { + appends_to_fd(&self.0.file) + } } + const _: () = assert!( as FsArgument>::HAVE_ABORT_SIGNAL); + const _: () = assert!( as FsArgument>::HAVE_ABORT_SIGNAL); /// Convert an async-FS result payload to a `JSValue`. /// Each `ret::*` type implements this by forwarding to its inherent method. @@ -1203,28 +635,6 @@ mod _async_tasks { } } - /// `Taskable` glue for the libuv-request ops (Windows), which complete on - /// the JS thread and re-enter through the task queue under a per-`F` tag. - #[cfg(windows)] - impl bun_event_loop::Taskable - for UVFSRequest - where - Op<{ F }>: NodeFSDispatch, - { - const TAG: bun_event_loop::TaskTag = F.task_tag(); - /// A libuv fs request that completed into the queue after the last - /// tick: destroy releases its promise handle and keep-alive. - unsafe fn release_unrun(this: *mut Self) { - // SAFETY: fn contract — `Box::leak`'d in `UVFSRequest::create`. - unsafe { Self::destroy(this) } - } - /// The context whose script asked. - unsafe fn context(this: *const Self) -> bun_event_loop::ContextId { - // SAFETY: fn contract. - unsafe { (*this).context } - } - } - /// One `fs.promises.*` operation on the work pool. The arguments' JS-backed /// buffers are pinned and rooted (`ThreadIsolated`) and read under the job's ticket. pub(crate) struct AsyncFSTask { @@ -1368,6 +778,20 @@ mod _async_tasks { { type OffThread = Self; type Js = AsyncFSJs; + const RUNS_CANCELLED: bool = matches!(F, NodeFSFunctionEnum::Close); + /// Where `waits` tells a write that can block from one that cannot. + const OWED_AT_EXIT: bool = + cfg!(windows) && matches!(F, NodeFSFunctionEnum::Write | NodeFSFunctionEnum::Writev); + + /// A read of a pipe or a console lasts until there is something to + /// read, and whatever else is asked of that handle meanwhile waits + /// behind it. + #[cfg(windows)] + fn waits(this: &Self) -> bool { + this.args + .fd_use() + .is_some_and(|fd_use| !sys::windows::fs::is_disk_file(fd_use.fd())) + } fn run( this: &mut Self, @@ -1378,6 +802,27 @@ mod _async_tasks { Some(done) } + fn closed(this: &mut Self) { + use NodeFSFunctionEnum::*; + let syscall = match F { + Close => sys::Tag::close, + Fchmod => sys::Tag::fchmod, + Fchown => sys::Tag::fchown, + Fdatasync => sys::Tag::fdatasync, + Fstat => sys::Tag::fstat, + Fsync => sys::Tag::fsync, + Ftruncate | Truncate => sys::Tag::ftruncate, + Futimes => sys::Tag::futime, + Read | ReadFile | Readv => sys::Tag::read, + _ => sys::Tag::write, + }; + let mut err = sys::Error::from_code(sys::E::EBADF, syscall); + if let Some(fd_use) = this.args.fd_use() { + err = err.with_fd(fd_use.fd()); + } + this.result = Err(err); + } + fn then( mut this: Self, js: AsyncFSJs, @@ -1427,7 +872,6 @@ mod _async_tasks { /// `callback`: the `node:fs` callback to call with the result. `None` returns a promise. pub(crate) fn create( cx: &bun_jsc::JsThread<'_>, - _binding: &Binding, args: ThreadIsolated, vm: &mut VirtualMachine, callback: Option, @@ -1436,7 +880,14 @@ mod _async_tasks { tracker.did_schedule(cx.global()); let completion = FsCompletion::new(cx.global(), callback); let value = completion.value(); - bun_jsc::Job::::schedule( + let fd_use = match args.fd_use() { + // `FdVectorIo` is `readv`'s too. + Some(FdUse::Appends(fd)) if matches!(F, NodeFSFunctionEnum::Readv) => { + Some(FdUse::Uses(fd)) + } + fd_use => fd_use, + }; + bun_jsc::Job::::schedule_on_fd( cx, Self { args, @@ -1448,6 +899,7 @@ mod _async_tasks { completion, tracker, }, + fd_use, ); value } @@ -1653,7 +1105,6 @@ mod _async_tasks { /// tracker, and this VM's loop. pub(crate) fn create( cx: &bun_jsc::JsThread<'_>, - _binding: &Binding, cp_args: ThreadIsolated>, vm: &mut VirtualMachine, ) -> JSValue { @@ -1985,7 +1436,7 @@ mod _async_tasks { } #[cfg(not(windows))] { - let stat_ = match Syscall::lstat(src) { + let stat_ = match sys::lstat(src) { Ok(result) => result, Err(err) => { nodefs.sync_error_buf[..src.len()].copy_from_slice(src.as_bytes()); @@ -2100,7 +1551,7 @@ mod _async_tasks { } let open_flags = sys::O::DIRECTORY | sys::O::RDONLY | sys::O::NOFOLLOW; - let fd = match openat_os_path(FD::cwd(), src, open_flags, 0) { + let fd = match sys::openat_os_path(FD::cwd(), src, open_flags, 0) { Err(err) => { this_ref.finish_concurrently(Err( err.with_path(nodefs.os_path_into_sync_error_buf(src)) @@ -2145,13 +1596,8 @@ mod _async_tasks { } } - // On POSIX directory entries are always UTF-8, so monomorphise the - // const-generic path type on `U8` and let the Windows branch (gated - // above) handle the wide path. - #[cfg(windows)] - let mut iterator = DirIterator::iterate::(fd); - #[cfg(not(windows))] - let mut iterator = DirIterator::iterate::(fd); + // Entry names in the OS path width: u16 on Windows, u8 elsewhere. + let mut iterator = DirIterator::iterate::<{ cfg!(windows) }>(fd); let mut entry = iterator.next(); loop { let current = match entry { @@ -2721,7 +2167,8 @@ mod _async_tasks { } } // mod _async_tasks pub(crate) use _async_tasks::{ - AsyncCpTask, AsyncReaddirRecursiveTask, FsArgument, FsReturn, ShellAsyncCpTask, async_, + AsyncCpTask, AsyncFSTask, AsyncReaddirRecursiveTask, FsArgument, FsReturn, ShellAsyncCpTask, + async_, }; // ────────────────────────────────────────────────────────────────────────── @@ -2795,8 +2242,7 @@ pub(crate) mod args { /// Shared layout for `fs.writev` / `fs.readv` arguments. One concrete /// struct; we re-export both /// names as type aliases so every `args::Writev` / `args::Readv` caller - /// (UVFSRequest params, `readv`/`writev`/`preadv_inner`/`pwritev_inner`, - /// uv dispatch arms) is untouched. + /// (`readv`/`writev`/`preadv_inner`/`pwritev_inner`) is untouched. pub(crate) struct FdVectorIo { pub(crate) fd: FD, pub(crate) buffers: VectorArrayBuffer, @@ -2880,7 +2326,7 @@ pub(crate) mod args { return Err(ctx.throw_invalid_arguments(format_args!("uid is required"))); }; arguments.eat(); - break 'brk wrap_to::(validators::validate_integer( + break 'brk wrap_to(validators::validate_integer( ctx, uid_value, "uid", @@ -2893,7 +2339,7 @@ pub(crate) mod args { return Err(ctx.throw_invalid_arguments(format_args!("gid is required"))); }; arguments.eat(); - break 'brk wrap_to::(validators::validate_integer( + break 'brk wrap_to(validators::validate_integer( ctx, gid_value, "gid", @@ -2921,7 +2367,7 @@ pub(crate) mod args { return Err(ctx.throw_invalid_arguments(format_args!("uid is required"))); }; arguments.eat(); - break 'brk wrap_to::(validators::validate_integer( + break 'brk wrap_to(validators::validate_integer( ctx, uid_value, "uid", @@ -2934,7 +2380,7 @@ pub(crate) mod args { return Err(ctx.throw_invalid_arguments(format_args!("gid is required"))); }; arguments.eat(); - break 'brk wrap_to::(validators::validate_integer( + break 'brk wrap_to(validators::validate_integer( ctx, gid_value, "gid", @@ -2946,18 +2392,9 @@ pub(crate) mod args { } } - /// Only ever instantiated with `uid_t`/`gid_t` — `u32` on POSIX, `u8` on - /// Windows (libuv's `uv_uid_t`/`uv_gid_t` are `unsigned char`). Hard-code - /// the per-platform wrap rather than pulling `num_traits`. - #[cfg(not(windows))] - #[inline] - fn wrap_to>(in_: i64) -> T { - T::from(in_ as u32) - } - #[cfg(windows)] #[inline] - fn wrap_to>(in_: i64) -> T { - T::from(in_ as u8) + fn wrap_to(in_: i64) -> u32 { + in_ as u32 } pub(crate) type LChown<'a> = Chown<'a>; @@ -3121,8 +2558,8 @@ pub(crate) mod args { }) } - /// `fs.stat(path)` of Rust-owned bytes, for a work-pool job. - pub(crate) fn owned(path: Vec) -> ThreadIsolated { + /// `Bun.file(path).stat()`, for a work-pool job. + pub(crate) fn of_bun_file(path: Vec) -> ThreadIsolated { // SAFETY: owned path only. unsafe { ThreadIsolated::new(Stat { @@ -3343,8 +2780,8 @@ pub(crate) mod args { Ok(Unlink { path }) } - /// `fs.unlink(path)` of Rust-owned bytes, for a work-pool job. - pub(crate) fn owned(path: Vec) -> ThreadIsolated { + /// `Bun.file(path).delete()`, for a work-pool job. + pub(crate) fn of_bun_file(path: Vec) -> ThreadIsolated { // SAFETY: owned path only. unsafe { ThreadIsolated::new(Unlink { @@ -3477,6 +2914,10 @@ pub(crate) mod args { pub(crate) mode: Mode, /// If set to true, the return value is never set to a string pub(crate) always_return_none: bool, + /// `path` leads a `Bun.file`'s path (recursive only): every level is + /// named as the open of that file names it. See + /// [`PathLikeExt::slice_z_as_written`]. + pub(crate) as_written: bool, } impl Mkdir<'_> { pub(crate) const DEFAULT_MODE: Mode = 0o777; @@ -3488,6 +2929,7 @@ pub(crate) mod args { recursive: false, mode: Self::DEFAULT_MODE, always_return_none: false, + as_written: false, } } } @@ -3521,6 +2963,7 @@ pub(crate) mod args { recursive, mode, always_return_none: false, + as_written: false, }) } } @@ -4691,7 +4134,7 @@ impl NodeFS { } else { args.path.slice_z(&mut self.sync_error_buf) }; - match Syscall::access(path, args.mode.as_int()) { + match sys::access(path, args.mode.as_int()) { Err(err) => Err(err.with_path(args.path.slice())), Ok(_) => Ok(Null), } @@ -4707,17 +4150,18 @@ impl NodeFS { match &args.file { PathOrFileDescriptor::Fd(fd) => { while !data.is_empty() { - let written = Syscall::write(*fd, data)?; + let written = sys::write(*fd, data)?; data = &data[written..]; } Ok(()) } PathOrFileDescriptor::Path(path_) => { let path = path_.slice_z(&mut self.sync_error_buf); - let fd = Syscall::open(path, args.flag.as_int(), args.mode)?; + let fd = sys::open(path, args.flag.as_int(), args.mode) + .map_err(|err| err.with_path(path_.slice()))?; let _close = scopeguard::guard(fd, |fd| fd.close()); while !data.is_empty() { - let written = Syscall::write(fd, data)?; + let written = sys::write(fd, data)?; data = &data[written..]; } Ok(()) @@ -4736,18 +4180,6 @@ impl NodeFS { } } - #[cfg(windows)] - pub(crate) fn uv_close( - &mut self, - args: &args::Close, - rc: uv::ReturnCodeI64, - ) -> Maybe { - if let Some(err) = rc.to_error(sys::Tag::close) { - return Err(err.with_fd(args.fd)); - } - Ok(()) - } - // since we use a 64 KB stack buffer, we should not let this function get inlined #[inline(never)] #[cfg(not(windows))] @@ -4771,7 +4203,7 @@ impl NodeFS { const STACK_BUF_LEN: usize = 64 * 1024; let mut stack_buf = bun_core::vec::UninitBuf::::uninit(); let mut buf_to_free: Vec = Vec::new(); - // SAFETY: `Syscall::read` is the only writer of `buf`; each iteration reads back only `buf[..amt]`. + // SAFETY: `sys::read` is the only writer of `buf`; each iteration reads back only `buf[..amt]`. let mut buf: &mut [u8] = unsafe { stack_buf.as_bytes_mut() }; 'maybe_allocate_large_temp_buf: { @@ -4781,7 +4213,7 @@ impl NodeFS { // The slab must stay uninitialised: `Vec::resize` here was a // debug-build hot path (byte-by-byte `extend_with`). Use // `expand_to_capacity` instead — the slab is write-only, - // `Syscall::read` fills it from the kernel. + // `sys::read` fills it from the kernel. use bun_collections::vec_ext::VecExt as _; if buf_to_free.try_reserve_exact(clamped_size).is_err() { break 'maybe_allocate_large_temp_buf; @@ -4802,7 +4234,7 @@ impl NodeFS { let mut broke = false; 'toplevel: while remain > 0 { let read_len = (buf.len() as u64).min(remain) as usize; - let amt = match Syscall::read(src_fd, &mut buf[..read_len]) { + let amt = match sys::read(src_fd, &mut buf[..read_len]) { Ok(result) => result, Err(err) => { return Err(if !src.is_empty() { @@ -4822,7 +4254,7 @@ impl NodeFS { let mut slice = &buf[..amt]; while !slice.is_empty() { - let written = match Syscall::write(dest_fd, slice) { + let written = match sys::write(dest_fd, slice) { Ok(result) => result, Err(err) => { return Err(if !dest.is_empty() { @@ -4841,7 +4273,7 @@ impl NodeFS { } if !broke { 'outer: loop { - let amt = match Syscall::read(src_fd, buf) { + let amt = match sys::read(src_fd, buf) { Ok(result) => result, Err(err) => { return Err(if !src.is_empty() { @@ -4860,7 +4292,7 @@ impl NodeFS { let mut slice = &buf[..amt]; while !slice.is_empty() { - let written = match Syscall::write(dest_fd, slice) { + let written = match sys::write(dest_fd, slice) { Ok(result) => result, Err(err) => { return Err(if !dest.is_empty() { @@ -4947,7 +4379,7 @@ impl NodeFS { ) .unwrap_or(Ok(())); } else { - let stat_ = match Syscall::stat(src) { + let stat_ = match sys::stat(src) { Ok(result) => result, Err(err) => return Err(err.with_path(src)), }; @@ -4965,7 +4397,7 @@ impl NodeFS { if stat_.st_size > 128 * 1024 { if !args.mode.shouldnt_overwrite() { // clonefile() will fail if it already exists - let _ = Syscall::unlink(dest); + let _ = sys::unlink(dest); } if Maybe::::errno_sys_p( bun_sys::c::clonefile_rc(src, dest, 0), @@ -4974,11 +4406,11 @@ impl NodeFS { ) .is_none() { - let _ = Syscall::chmod(dest, stat_.st_mode as u32); + let _ = sys::chmod(dest, stat_.st_mode as u32); return Ok(()); } } else { - let src_fd = match Syscall::open(src, sys::O::RDONLY, 0o644) { + let src_fd = match sys::open(src, sys::O::RDONLY, 0o644) { Ok(result) => result, Err(err) => return Err(err.with_path(args.src.slice())), }; @@ -4996,7 +4428,7 @@ impl NodeFS { flags |= sys::O::EXCL; } - let dest_fd = match Syscall::open(dest, flags, stat_.st_mode as Mode) { + let dest_fd = match sys::open(dest, flags, stat_.st_mode as Mode) { Ok(result) => result, Err(err) => return Err(err.with_path(args.dest.slice())), }; @@ -5009,8 +4441,8 @@ impl NodeFS { stat_.st_size.max(0) as usize, &mut wrote, ); - let _ = Syscall::ftruncate(dest_fd, (wrote & ((1u64 << 63) - 1)) as i64); - let _ = Syscall::fchmod(dest_fd, stat_.st_mode as u32); + let _ = sys::ftruncate(dest_fd, (wrote & ((1u64 << 63) - 1)) as i64); + let _ = sys::fchmod(dest_fd, stat_.st_mode as u32); dest_fd.close(); return result; } @@ -5046,13 +4478,13 @@ impl NodeFS { }); } - let src_fd = match Syscall::open(src, sys::O::RDONLY, 0) { + let src_fd = match sys::open(src, sys::O::RDONLY, 0) { Ok(result) => result, Err(err) => return Err(err.with_path(args.src.slice())), }; let _close_src = scopeguard::guard(src_fd, |fd| fd.close()); - let stat_ = match Syscall::fstat(src_fd) { + let stat_ = match sys::fstat(src_fd) { Ok(result) => result, Err(err) => return Err(err), }; @@ -5068,7 +4500,7 @@ impl NodeFS { if args.mode.shouldnt_overwrite() { flags |= sys::O::EXCL; } - let dest_fd = match Syscall::open(dest, flags, stat_.st_mode as Mode) { + let dest_fd = match sys::open(dest, flags, stat_.st_mode as Mode) { Ok(result) => result, Err(err) => return Err(err), }; @@ -5077,7 +4509,7 @@ impl NodeFS { // Don't O_TRUNC at open: if src and dest resolve to the same // inode, that would zero the file before the first read. Match // Node by checking inodes after both are open and refusing. - if let Ok(dst_stat) = Syscall::fstat(dest_fd) { + if let Ok(dst_stat) = sys::fstat(dest_fd) { if stat_.st_ino == dst_stat.st_ino && stat_.st_dev == dst_stat.st_dev { return Err(sys::Error { errno: SystemErrno::EINVAL as _, @@ -5087,7 +4519,7 @@ impl NodeFS { }); } } - let _ = Syscall::ftruncate(dest_fd, 0); + let _ = sys::ftruncate(dest_fd, 0); // FreeBSD 13+ has copy_file_range(2). Try the kernel-side copy // first; fall back to read/write on cross-device or unsupported @@ -5110,7 +4542,7 @@ impl NodeFS { match sys::get_errno(rc) { E::SUCCESS => { if rc == 0 { - let _ = Syscall::fchmod(dest_fd, stat_.st_mode as Mode); + let _ = sys::fchmod(dest_fd, stat_.st_mode as Mode); return Ok(()); } } @@ -5139,7 +4571,7 @@ impl NodeFS { let _ = sys::unlink(dest); return Err(err); } - let _ = Syscall::fchmod(dest_fd, stat_.st_mode as Mode); + let _ = sys::fchmod(dest_fd, stat_.st_mode as Mode); return Ok(()); } @@ -5150,10 +4582,10 @@ impl NodeFS { let src = args.src.slice_z(&mut src_buf); let dest = args.dest.slice_z(&mut dest_buf); - let src_fd = Syscall::open(src, sys::O::RDONLY, 0o644)?; + let src_fd = sys::open(src, sys::O::RDONLY, 0o644)?; let _close_src = scopeguard::guard(src_fd, |fd| fd.close()); - let stat_ = Syscall::fstat(src_fd)?; + let stat_ = sys::fstat(src_fd)?; if !sys::S::ISREG(stat_.st_mode as u32) { return Err(sys::Error { @@ -5175,7 +4607,7 @@ impl NodeFS { flags |= sys::O::EXCL; } - let dest_fd = Syscall::open(dest, flags, stat_.st_mode as Mode)?; + let dest_fd = sys::open(dest, flags, stat_.st_mode as Mode)?; let mut size: usize = stat_.st_size.max(0) as usize; @@ -5191,7 +4623,7 @@ impl NodeFS { let _ = sys::unlink(dest); return err; } - let _ = Syscall::fchmod(dest_fd, stat_.st_mode as u32); + let _ = sys::fchmod(dest_fd, stat_.st_mode as u32); dest_fd.close(); return Ok(()); } @@ -5200,7 +4632,7 @@ impl NodeFS { if sys::S::ISREG(stat_.st_mode as u32) && sys::copy_file::can_use_ioctl_ficlone() { let rc = sys::linux::ioctl_ficlone(dest_fd, src_fd); if rc == 0 { - let _ = Syscall::fchmod(dest_fd, stat_.st_mode as u32); + let _ = sys::fchmod(dest_fd, stat_.st_mode as u32); dest_fd.close(); return Ok(()); } @@ -5213,8 +4645,8 @@ impl NodeFS { scopeguard::guard((dest_fd, stat_.st_mode, &wrote), |(fd, m, wrote)| { // ftruncate/fchmod take only ints — no memory-safety preconditions; route // through the existing `bun_sys` safe wrappers (same as lines above). - let _ = Syscall::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); - let _ = Syscall::fchmod(fd, m as u32); + let _ = sys::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); + let _ = sys::fchmod(fd, m as u32); fd.close(); }); @@ -5316,24 +4748,35 @@ impl NodeFS { #[cfg(windows)] { - // Paths whose UTF-16 form exceeds the wide buffers can't exist on - // disk; reject instead of overflowing the conversion below. - for path in [&args.src, &args.dest] { + // Paths whose UTF-16 form exceeds the wide buffers can't exist on disk. + let too_long = |path: &PathLike| sys::Error { + errno: E::ENAMETOOLONG as _, + syscall: sys::Tag::copyfile, + path: path.slice().into(), + ..Default::default() + }; + // Win32 resolves a relative path itself, from how it is written: + // `./aux` is a file and `./a:s` a stream, `aux` a device and `a:s` + // a path on drive A. + fn convert<'a>( + path: &'a PathLike, + buf: &'a mut paths::PathBuffer, + ) -> Result<&'a OSPathSliceZ, NameTooLong> { + if paths::is_absolute(path.slice()) { + return path.os_path_kernel32(buf); + } if !strings::fits_in_wide_path_buffer(path.slice()) { - return Err(sys::Error { - errno: E::ENAMETOOLONG as _, - syscall: sys::Tag::copyfile, - path: path.slice().into(), - ..Default::default() - }); + return Err(NameTooLong); } + crate::node::types::kernel32_path_past_max_path(buf, path.slice()) } - let mut dest_buf = paths::os_path_buffer_pool::get(); - let src = strings::to_kernel32_path( - bun_core::cast_slice_mut::(&mut self.sync_error_buf), - args.src.slice(), - ); - let dest = strings::to_kernel32_path(&mut *dest_buf, args.dest.slice()); + let mut dest_buf = paths::path_buffer_pool::get(); + let Ok(src) = convert(&args.src, &mut self.sync_error_buf) else { + return Err(too_long(&args.src)); + }; + let Ok(dest) = convert(&args.dest, &mut dest_buf) else { + return Err(too_long(&args.dest)); + }; // SAFETY: src/dest are NUL-terminated wide paths; CopyFileW is the Win32 FFI if unsafe { windows::CopyFileW( @@ -5378,7 +4821,7 @@ impl NodeFS { } let slice = if path.slice().is_empty() { - os_path_literal_empty() + OSPathSliceZ::EMPTY } else { match path.os_path_kernel32(&mut self.sync_error_buf) { Ok(p) => p, @@ -5391,52 +4834,30 @@ impl NodeFS { } pub(crate) fn chown(&mut self, args: &args::Chown, _: Flavor) -> Maybe { - #[cfg(windows)] - { - return match Syscall::chown( - args.path.slice_z(&mut self.sync_error_buf), - args.uid, - args.gid, - ) { - Err(err) => Err(err.with_path(args.path.slice())), - Ok(res) => Ok(res), - }; - } - #[cfg(not(windows))] - { - let path = args.path.slice_z(&mut self.sync_error_buf); - Syscall::chown(path, args.uid, args.gid) - } + let path = args.path.slice_z(&mut self.sync_error_buf); + sys::chown(path, args.uid, args.gid).map_err(|err| err.with_path(args.path.slice())) } pub(crate) fn chmod(&mut self, args: &args::Chmod, _: Flavor) -> Maybe { let path = args.path.slice_z(&mut self.sync_error_buf); - #[cfg(windows)] - { - return match Syscall::chmod(path, args.mode) { - Err(err) => Err(err.with_path(args.path.slice())), - Ok(res) => Ok(res), - }; - } - #[cfg(not(windows))] - match Syscall::chmod(path, args.mode) { + match sys::chmod(path, args.mode) { Err(err) => Err(err.with_path(args.path.slice())), Ok(_) => Ok(()), } } pub(crate) fn fchmod(&mut self, args: &args::FChmod, _: Flavor) -> Maybe { - Syscall::fchmod(args.fd, args.mode) + sys::fchmod(args.fd, args.mode) } pub(crate) fn fchown(&mut self, args: &args::Fchown, _: Flavor) -> Maybe { - Syscall::fchown(args.fd, args.uid, args.gid) + sys::fchown(args.fd, args.uid, args.gid) } pub(crate) fn fdatasync(&mut self, args: &args::FdataSync, _: Flavor) -> Maybe { #[cfg(windows)] { - return Syscall::fdatasync(args.fd); + return sys::fdatasync(args.fd); } #[cfg(not(windows))] { @@ -5464,59 +4885,22 @@ impl NodeFS { Err(err) => Err(err), }; } - match Syscall::fstat(args.fd) { + match sys::fstat(args.fd) { Ok(result) => Ok(Stats::init(&PosixStat::init(&result), args.big_int)), Err(err) => Err(err), } } pub(crate) fn fsync(&mut self, args: &args::Fsync, _: Flavor) -> Maybe { - #[cfg(windows)] - { - return Syscall::fsync(args.fd); - } - #[cfg(not(windows))] - { - // `fsync(int)` has no memory-safety preconditions (a bad fd just yields - // EBADF), so declare it `safe fn` instead of routing through - // `libc::fsync` (which is blanket-`unsafe`). Mirrors `fdatasync` above. - unsafe extern "C" { - safe fn fsync(fd: libc::c_int) -> libc::c_int; - } - Maybe::::errno_sys(fsync(args.fd.native()), sys::Tag::fsync) - .unwrap_or(Ok(())) - } + sys::fsync(args.fd) } pub(crate) fn ftruncate(&mut self, args: &args::FTruncate, _: Flavor) -> Maybe { - Syscall::ftruncate(args.fd, args.len.unwrap_or(0) as i64) + sys::ftruncate(args.fd, args.len.unwrap_or(0) as i64) } pub(crate) fn futimes(&mut self, args: &args::Futimes, _: Flavor) -> Maybe { - #[cfg(windows)] - { - let mut req = UvFsReq::new(); - let rc = unsafe { - uv::uv_fs_futime( - uv::Loop::get(), - &mut *req, - args.fd.uv(), - args.atime, - args.mtime, - None, - ) - }; - if let Some(err) = rc.to_error(sys::Tag::futime) { - return Err(err.with_fd(args.fd)); - } - return Ok(()); - } - #[cfg(not(windows))] - match Syscall::futimens( - args.fd, - to_sys_time_like(args.atime), - to_sys_time_like(args.mtime), - ) { + match sys::futimens(args.fd, args.atime, args.mtime) { // `err.syscall` must be node's operation name, not `futimens(2)`. Err(mut err) => { err.syscall = sys::Tag::futime; @@ -5546,7 +4930,7 @@ impl NodeFS { #[cfg(not(any(windows, target_os = "android")))] { let path = args.path.slice_z(&mut self.sync_error_buf); - match Syscall::lchmod(path, args.mode) { + match sys::lchmod(path, args.mode) { Err(err) => Err(err.with_path(args.path.slice())), Ok(_) => Ok(()), } @@ -5554,10 +4938,9 @@ impl NodeFS { } pub(crate) fn lchown(&mut self, args: &args::LChown, _: Flavor) -> Maybe { - // On Windows `Syscall::lchown` routes through uv_fs_lchown, which is - // a no-op success, matching Node. + // On Windows `sys::lchown` is a no-op success, matching Node. let path = args.path.slice_z(&mut self.sync_error_buf); - match Syscall::lchown(path, args.uid, args.gid) { + match sys::lchown(path, args.uid, args.gid) { Err(err) => Err(err.with_path(args.path.slice())), Ok(_) => Ok(()), } @@ -5569,7 +4952,7 @@ impl NodeFS { let to = args.new_path.slice_z(&mut to_buf); #[cfg(windows)] { - return match Syscall::link(from, to) { + return match sys::link(from, to) { Err(err) => Err(err.with_path_dest(args.old_path.slice(), args.new_path.slice())), Ok(result) => Ok(result), }; @@ -5610,7 +4993,7 @@ impl NodeFS { } }; } - match Syscall::lstat(path) { + match sys::lstat(path) { Ok(result) => Ok(StatOrNotFound::Stats(Box::new(Stats::init( &PosixStat::init(&result), args.big_int, @@ -5643,7 +5026,7 @@ impl NodeFS { // Node doesn't absolute the path so we don't have to either pub(crate) fn mkdir_non_recursive(&mut self, args: &args::Mkdir) -> Maybe { let path = args.path.slice_z(&mut self.sync_error_buf); - match Syscall::mkdir(path, args.mode) { + match sys::mkdir(path, args.mode) { Ok(_) => Ok(StringOrUndefined::None), Err(err) => Err(err.with_path(args.path.slice())), } @@ -5658,6 +5041,26 @@ impl NodeFS { args: &args::Mkdir, ctx: &Ctx, ) -> Maybe { + #[cfg(windows)] + if args.as_written { + let path = match sys::windows::fs::WPath::directory(args.path.slice()) { + Ok(path) => path, + Err(err) => { + return Err( + sys::Error::from_win32(err, sys::Tag::mkdir).with_path(args.path.slice()) + ); + } + }; + let result = if args.always_return_none { + self.mkdir_recursive_os_path_impl::(ctx, path.as_wstr(), args.mode) + } else { + self.mkdir_recursive_os_path_impl::(ctx, path.as_wstr(), args.mode) + }; + // The walk names the Win32 form it worked on. + return result.map_err(|err| err.with_path(args.path.slice())); + } + #[cfg(not(windows))] + let _ = args.as_written; let mut buf = paths::path_buffer_pool::get(); let path = match args.path.os_path_kernel32(&mut *buf) { Ok(p) => p, @@ -5949,23 +5352,10 @@ impl NodeFS { #[cfg(windows)] { - let mut req = UvFsReq::new(); - let rc = unsafe { - uv::uv_fs_mkdtemp( - bun_io::Loop::get(), - &mut *req, - prefix_buf.as_ptr().cast(), - None, - ) + return match sys::mkdtemp(&mut prefix_buf[..len + 6]) { + Ok(()) => Ok(encode_path_result(&prefix_buf[..len + 6], args.encoding)), + Err(err) => Err(err), }; - if let Some(err) = rc.to_error(sys::Tag::mkdtemp) { - return Err(err.with_path(&prefix_buf[..len + 6])); - } - // SAFETY: on success libuv populates `req.path` with a NUL-terminated - // UTF-8 string owned by the request; `UvFsReq::drop` runs - // `uv_fs_req_cleanup` in place after we've copied the bytes out. - let bytes = unsafe { bun_core::ffi::cstr(req.path) }.to_bytes(); - return Ok(encode_path_result(bytes, args.encoding)); } #[cfg(not(windows))] @@ -5980,13 +5370,10 @@ impl NodeFS { return Ok(encode_path_result(bytes, args.encoding)); } - let errno = sys::last_errno(); - Err(sys::Error { - errno: errno as _, - syscall: sys::Tag::mkdtemp, - path: prefix_buf[..len + 6].into(), - ..Default::default() - }) + Err( + sys::Error::from_code_int(sys::last_errno(), sys::Tag::mkdtemp) + .with_path(&prefix_buf[..len + 6]), + ) } } @@ -5997,39 +5384,11 @@ impl NodeFS { } else { args.path.slice_z(&mut self.sync_error_buf) }; - match Syscall::open(path, args.flags.as_int(), args.mode) { - Err(err) => Err(err.with_path(args.path.slice())), - Ok(fd) => Ok(fd), - } - } - - #[cfg(windows)] - pub(crate) fn uv_open(&mut self, args: &args::Open, rc: uv::ReturnCodeI64) -> Maybe { - if let Some(err) = rc.to_error(sys::Tag::open) { - return Err(err.with_path(args.path.slice())); - } - Ok(FD::from_uv(rc.to_fd())) - } - - #[cfg(windows)] - pub(crate) fn uv_statfs( - &mut self, - args: &args::StatFS, - req: &mut uv::fs_t, - rc: uv::ReturnCodeI64, - ) -> Maybe { - if let Some(err) = rc.to_error(sys::Tag::statfs) { - return Err(err.with_path(args.path.slice())); - } - // libuv stores - // a `uv_statfs_t*` in `req.ptr` on success. The struct is unaligned in - // the request buffer, hence `read_unaligned`. - // SAFETY: `rc >= 0` ⇒ libuv populated `req.ptr` with a valid - // `uv_statfs_t` (= `RawStatFS` on Windows); we copy it out by value - // before `uv_fs_req_cleanup` releases the backing storage. - let statfs_: super::statfs::RawStatFS = - unsafe { core::ptr::read_unaligned(req.ptr_as::()) }; - Ok(ret::StatFS::init(&statfs_, args.big_int)) + // JS sees a CRT fd on Windows. Running out of those is this call's + // `EMFILE`, so the conversion happens where the path is known. + sys::open(path, args.flags.as_int(), args.mode) + .and_then(|fd| fd.make_crt_owned_for_syscall()) + .map_err(|err| err.with_path(args.path.slice())) } fn read_inner(&mut self, args: &args::Read) -> Maybe { @@ -6043,7 +5402,7 @@ impl NodeFS { buf = &mut buf[off..]; let l = (args.length as usize).min(buf.len()); buf = &mut buf[..l]; - match Syscall::read(args.fd, buf) { + match sys::read(args.fd, buf) { Err(err) => Err(err), Ok(amt) => Ok(ret::Read { bytes_read: amt as u64, @@ -6059,7 +5418,7 @@ impl NodeFS { buf = &mut buf[off..]; let l = (args.length as usize).min(buf.len()); buf = &mut buf[..l]; - match Syscall::pread(args.fd, buf, args.position.unwrap()) { + match sys::pread(args.fd, buf, args.position.unwrap()) { Err(err) => Err(sys::Error { errno: err.errno, fd: args.fd, @@ -6085,30 +5444,6 @@ impl NodeFS { } } - #[cfg(windows)] - pub(crate) fn uv_read(&mut self, args: &args::Read, rc: uv::ReturnCodeI64) -> Maybe { - if let Some(err) = rc.to_error(sys::Tag::read) { - return Err(err.with_fd(args.fd)); - } - Ok(ret::Read { - bytes_read: rc.int() as u64, - }) - } - - #[cfg(windows)] - pub(crate) fn uv_readv( - &mut self, - args: &args::Readv, - rc: uv::ReturnCodeI64, - ) -> Maybe { - if let Some(err) = rc.to_error(sys::Tag::readv) { - return Err(err.with_fd(args.fd)); - } - Ok(ret::Readv { - bytes_read: rc.int() as u64, - }) - } - pub(crate) fn readv(&mut self, args: &args::Readv, _: Flavor) -> Maybe { if args.buffers.buffers.is_empty() { return Ok(ret::Readv { bytes_read: 0 }); @@ -6139,41 +5474,13 @@ impl NodeFS { } } - #[cfg(windows)] - pub(crate) fn uv_write( - &mut self, - args: &args::Write, - rc: uv::ReturnCodeI64, - ) -> Maybe { - if let Some(err) = rc.to_error(sys::Tag::write) { - return Err(err.with_fd(args.fd)); - } - Ok(ret::Write { - bytes_written: rc.int() as u64, - }) - } - - #[cfg(windows)] - pub(crate) fn uv_writev( - &mut self, - args: &args::Writev, - rc: uv::ReturnCodeI64, - ) -> Maybe { - if let Some(err) = rc.to_error(sys::Tag::writev) { - return Err(err.with_fd(args.fd)); - } - Ok(ret::Writev { - bytes_written: rc.int() as u64, - }) - } - fn write_inner(&mut self, args: &args::Write) -> Maybe { let mut buf = args.buffer.slice(); let off = (args.offset as usize).min(buf.len()); buf = &buf[off..]; let l = (args.length as usize).min(buf.len()); buf = &buf[..l]; - match Syscall::write(args.fd, buf) { + match sys::write(args.fd, buf) { Err(err) => Err(err), Ok(amt) => Ok(ret::Write { bytes_written: amt as u64, @@ -6188,7 +5495,7 @@ impl NodeFS { buf = &buf[off..]; let l = (args.length as usize).min(buf.len()); buf = &buf[..l]; - match Syscall::pwrite(args.fd, buf, position) { + match sys::pwrite(args.fd, buf, position) { Err(err) => Err(sys::Error { errno: err.errno, fd: args.fd, @@ -6206,7 +5513,7 @@ impl NodeFS { let bufs = args.buffers.buffers.as_slice(); // libuv `uv__fs_read`: cap `nbufs` at IOV_MAX and issue one syscall. let bufs = &bufs[..bufs.len().min(IOV_MAX)]; - match Syscall::preadv(args.fd, bufs, position as i64) { + match sys::preadv(args.fd, bufs, position as i64) { Err(err) => Err(err), Ok(amt) => Ok(ret::Readv { bytes_read: amt as u64, @@ -6218,7 +5525,7 @@ impl NodeFS { let bufs = args.buffers.buffers.as_slice(); // libuv `uv__fs_read`: cap `nbufs` at IOV_MAX and issue one syscall. let bufs = &bufs[..bufs.len().min(IOV_MAX)]; - match Syscall::readv(args.fd, bufs) { + match sys::readv(args.fd, bufs) { Err(err) => Err(err), Ok(amt) => Ok(ret::Readv { bytes_read: amt as u64, @@ -6229,7 +5536,7 @@ impl NodeFS { fn pwritev_inner(&mut self, args: &args::Writev) -> Maybe { let mut position = args.position.unwrap() as i64; // `PlatformIoVec` - // and `PlatformIoVecConst` are layout-identical (`{ *void, usize }`); the + // and `PlatformIoVecConst` are layout-identical; the // kernel never writes through `iov_base` for pwritev(2). // SAFETY: layout-compatible reinterpretation, asserted in `bun_sys`. let vecs: &[sys::PlatformIoVecConst] = unsafe { @@ -6249,7 +5556,7 @@ impl NodeFS { while !remaining.is_empty() { let chunk_len = remaining.len().min(IOV_MAX); let chunk = &remaining[..chunk_len]; - match Syscall::pwritev(args.fd, chunk, position) { + match sys::pwritev(args.fd, chunk, position) { Err(err) if total == 0 => return Err(err), Err(_) => break, Ok(0) => break, @@ -6272,8 +5579,8 @@ impl NodeFS { fn writev_inner(&mut self, args: &args::Writev) -> Maybe { // The mutable iovec slice doubles as `iovec_const` for writev(2); the kernel // never writes through `iov_base`. `PlatformIoVec` and - // `PlatformIoVecConst` are layout-identical (`{ *void, usize }`), so - // pass the slice through `Syscall::writev` as-is. + // `PlatformIoVecConst` are layout-identical, so + // pass the slice through `sys::writev` as-is. // libuv `uv__fs_write_all`: loop IOV_MAX-sized batches until every // buffer is written; an error after the first batch returns the // accumulated total instead of the error. @@ -6282,7 +5589,7 @@ impl NodeFS { while !remaining.is_empty() { let chunk_len = remaining.len().min(IOV_MAX); let chunk = &remaining[..chunk_len]; - match Syscall::writev(args.fd, chunk) { + match sys::writev(args.fd, chunk) { Err(err) if total == 0 => return Err(err), Err(_) => break, Ok(0) => break, @@ -6471,7 +5778,7 @@ impl NodeFS { async_task.root_fd }; #[cfg(not(windows))] - let open_res = Syscall::openat(atfd, basename, flags, 0); + let open_res = sys::openat(atfd, basename, flags, 0); #[cfg(windows)] // the plain Windows open wrapper does not pass iterable=true let open_res = sys::open_dir_at_windows_a( @@ -6680,7 +5987,7 @@ impl NodeFS { // item was stored with trailing NUL (see push site). ZStr::from_slice_with_nul(&item) }; - let fd = match Syscall::openat(atfd, basename_z, flags, 0) { + let fd = match sys::openat(atfd, basename_z, flags, 0) { Err(err) => { if *root_fd == FD::INVALID { return Err(err.with_path(args.path.slice())); @@ -6862,7 +6169,7 @@ impl NodeFS { #[cfg(not(windows))] let flags = sys::O::DIRECTORY | sys::O::RDONLY; #[cfg(not(windows))] - let open_res = Syscall::open(path, flags, 0); + let open_res = sys::open(path, flags, 0); #[cfg(windows)] let open_res = sys::open_dir_at_windows_a( FD::cwd(), @@ -7003,7 +6310,7 @@ impl NodeFS { string_type: ReadFileStringType, ) -> Maybe { let path_is_path = matches!(args.path, PathOrFileDescriptor::Path(_)); - let fd_maybe_windows: FD = match &args.path { + let fd: FD = match &args.path { PathOrFileDescriptor::Path(p) => { let path = p.slice_z(&mut self.sync_error_buf); @@ -7053,19 +6360,6 @@ impl NodeFS { } PathOrFileDescriptor::Fd(fd) => *fd, }; - let fd: FD = match fd_maybe_windows.make_lib_uv_owned() { - Ok(fd) => fd, - Err(_) => { - if path_is_path { - fd_maybe_windows.close(); - } - return Err(sys::Error { - errno: E::EMFILE as _, - syscall: sys::Tag::open, - ..Default::default() - }); - } - }; let _close = scopeguard::guard((fd, path_is_path), |(fd, is_path)| { if is_path { fd.close(); @@ -7091,7 +6385,7 @@ impl NodeFS { // The sync case claims the per-VM pipe-read scratch when it is free; // otherwise (async, no VM, or a read further up the stack holds it) // a heap buffer stands in. It stays uninitialised: it is write-only, - // `Syscall::read` hands it straight to the kernel. + // `sys::read` hands it straight to the kernel. use bun_collections::vec_ext::VecExt as _; let mut scratch = match self.vm { // SAFETY: `self.vm` is the live owning `*mut VirtualMachine` (single-threaded VM), which outlives this call. @@ -7118,7 +6412,7 @@ impl NodeFS { let temporary_read_buffer_before_stat_call: &[u8] = { let mut available: &mut [u8] = &mut pre_stat_buf[..]; while !available.is_empty() { - let amt = Syscall::read(fd, available)?; + let amt = sys::read(fd, available)?; if amt == 0 { did_succeed = true; break; @@ -7202,7 +6496,7 @@ impl NodeFS { return Err(abort_err()); } - let stat_ = Syscall::fstat(fd)?; + let stat_ = sys::fstat(fd)?; // For certain files, the size might be 0 but the file might still have contents. // https://github.com/oven-sh/bun/issues/1220 @@ -7246,7 +6540,7 @@ impl NodeFS { // Read into the uninitialised tail. `Vec::resize(cap, 0)` is *not* equivalent in // debug builds: it goes through `extend_with`'s byte-by-byte loop (no memset // specialisation), which dominated `readFileSync` of large files. Use - // `VecExt::expand_to_capacity` (the tail is write-only — `Syscall::read` + // `VecExt::expand_to_capacity` (the tail is write-only — `sys::read` // hands it straight to the kernel, which only stores into it). // SAFETY: `u8` has no validity invariant; the buffer is handed straight // to the kernel which only stores into it. @@ -7264,7 +6558,7 @@ impl NodeFS { // Do NOT pre-grow here; growth happens only in the `total > size && amt != 0 && // !has_max_size` arm below. let upper = (buf.capacity() as u64).min(max_size) as usize; - let amt = Syscall::read(fd, &mut buf[total..upper])?; + let amt = sys::read(fd, &mut buf[total..upper])?; total += amt; if args.limit_size_for_javascript { @@ -7401,7 +6695,6 @@ impl NodeFS { } let mut buf = args.data.slice(); - #[cfg(not(windows))] let mut written: usize = 0; // Attempt to pre-allocate large files @@ -7427,7 +6720,7 @@ impl NodeFS { let offset: usize = if is_path { 0 } else { - match Syscall::lseek(fd, 0, libc::SEEK_CUR) { + match sys::lseek(fd, 0, libc::SEEK_CUR) { Err(_) => break 'preallocate, Ok(pos) => usize::try_from(pos).expect("int cast"), } @@ -7452,10 +6745,7 @@ impl NodeFS { } Ok(amt) => { buf = &buf[amt..]; - #[cfg(not(windows))] - { - written += amt; - } + written += amt; if amt == 0 { break; } @@ -7473,14 +6763,7 @@ impl NodeFS { { // If this errors, we silently ignore it. // Not all files are seekable (and thus, not all files can be truncated). - #[cfg(windows)] - { - let _ = unsafe { windows::SetEndOfFile(fd.native()) }; - } - #[cfg(not(windows))] - { - let _ = Syscall::ftruncate(fd, (written as u64 & ((1u64 << 63) - 1)) as i64); - } + let _ = sys::ftruncate(fd, (written as u64 & ((1u64 << 63) - 1)) as i64); } if let Some(err) = write_err { @@ -7488,14 +6771,7 @@ impl NodeFS { } if args.flush { - #[cfg(windows)] - { - let _ = unsafe { windows::kernel32::FlushFileBuffers(fd.native()) }; - } - #[cfg(not(windows))] - { - let _ = Syscall::fsync(fd); - } + let _ = sys::fsync(fd); } Ok(()) @@ -7513,9 +6789,6 @@ impl NodeFS { let mut outbuf = bun_paths::path_buffer_pool::get(); let inbuf = &mut self.sync_error_buf; let path = args.path.slice_z(inbuf); - // PORT: `Syscall` (= `sys_uv` on Windows) returns the link slice - // directly there but `usize` on POSIX. `bun_sys::readlink` is the - // length-normalised wrapper on every platform. let link_len = match sys::readlink(path, &mut outbuf[..]) { Err(err) => return Err(err.with_path(args.path.slice())), Ok(result) => result, @@ -7568,57 +6841,26 @@ impl NodeFS { args: &args::Realpath, variant: RealpathVariant, ) -> Maybe { + let mut outbuf = bun_paths::path_buffer_pool::get(); #[cfg(windows)] - { - let mut req = UvFsReq::new(); - let rc = unsafe { - uv::uv_fs_realpath( - bun_io::Loop::get(), - &mut *req, - args.path.slice_z(&mut self.sync_error_buf).as_ptr(), - None, - ) - }; - if let Some(err) = rc.to_error(sys::Tag::realpath) { - return Err(err.with_path(args.path.slice())); - } - // `fs_t.ptr` *is* the nullable C - // string pointer (libuv stores the realpath result directly), so - // `ptr_as::()` yields the value, not a pointer-to-Option. - // SAFETY: `rc` was not an error ⇒ libuv populated `req.ptr`. - let ptr: *const c_char = unsafe { req.ptr_as::() }; - if ptr.is_null() { - return Err(sys::Error { - errno: E::ENOENT as _, - syscall: sys::Tag::realpath, - path: args.path.slice().into(), - ..Default::default() - }); - } - let mut buf = unsafe { bun_core::ffi::cstr(ptr) }.to_bytes(); + let buf: &[u8] = { + let mut buf = + match sys::realpath(args.path.slice_z(&mut self.sync_error_buf), &mut outbuf) { + Err(err) => return Err(err.with_path(args.path.slice())), + Ok(buf_) => buf_, + }; if variant == RealpathVariant::Emulated { // remove the trailing slash - // - // `buf` is an immutable view and every consumer below copies by - // length, so just shrink the slice — writing a NUL back through - // `ptr.cast_mut()` while `buf` is live would be Stacked-Borrows UB. if buf.last() == Some(&b'\\') { buf = &buf[..buf.len() - 1]; } } - if args.encoding == Encoding::Utf8 { - if let PathLike::String(s) = &args.path { - if strings::eql_long(s.slice(), buf, true) { - return Ok(StringOrBuffer::String(s.clone())); - } - } - } - return Ok(encode_path_result(buf, args.encoding)); - } + buf + }; #[cfg(not(windows))] - { - let mut outbuf = bun_paths::path_buffer_pool::get(); + let buf: &[u8] = { + let _ = variant; let inbuf = &mut self.sync_error_buf; // SAFETY: single-threaded init flag (resolver/fs.rs). debug_assert!( @@ -7653,21 +6895,20 @@ impl NodeFS { }; let _close = scopeguard::guard(fd, |fd| fd.close()); - let buf = match Syscall::get_fd_path(fd, &mut outbuf) { + match sys::get_fd_path(fd, &mut outbuf) { Err(err) => return Err(err.with_path(path)), Ok(buf_) => buf_, - }; + } + }; - let _ = variant; - if args.encoding == Encoding::Utf8 { - if let PathLike::String(s) = &args.path { - if strings::eql_long(s.slice(), buf, true) { - return Ok(StringOrBuffer::String(s.clone())); - } + if args.encoding == Encoding::Utf8 { + if let PathLike::String(s) = &args.path { + if strings::eql_long(s.slice(), buf, true) { + return Ok(StringOrBuffer::String(s.clone())); } } - Ok(encode_path_result(buf, args.encoding)) } + Ok(encode_path_result(buf, args.encoding)) } pub(crate) fn rename(&mut self, args: &args::Rename, _: Flavor) -> Maybe { @@ -7675,7 +6916,7 @@ impl NodeFS { let mut to_buf = bun_paths::path_buffer_pool::get(); let from = args.old_path.slice_z(from_buf); let to = args.new_path.slice_z(&mut to_buf); - match Syscall::rename(from, to) { + match sys::rename(from, to) { Ok(result) => Ok(result), Err(err) => Err(err.with_path_dest(args.old_path.slice(), args.new_path.slice())), } @@ -7685,13 +6926,16 @@ impl NodeFS { if args.recursive { // On Windows a rooted-but-driveless path ("/tmp/foo") must resolve // against the cwd drive. - // Our dt_* helpers go through Syscall::*at → to_nt_path / + // Our dt_* helpers go through sys::*at → to_nt_path / // normalize_path_windows, which do NOT add the cwd drive, turning // "/tmp/foo" into a nonexistent NT name (ENOENT). Pre-resolve with // slice_z so the path already carries a drive letter, the same way // existsSync/statSync/unlinkSync see it. #[cfg(windows)] - let resolved = args.path.slice_z(&mut self.sync_error_buf).as_bytes(); + let resolved = match tree_path_windows(&args.path, &mut self.sync_error_buf) { + Ok(resolved) => resolved, + Err(err) => return Err(err.with_path(args.path.slice())), + }; #[cfg(not(windows))] let resolved = args.path.slice(); if let Err(err) = zig_delete_tree(&sys::Dir::cwd(), resolved, sys::FileKind::Directory) @@ -7706,7 +6950,7 @@ impl NodeFS { } #[cfg(windows)] { - return match Syscall::rmdir(args.path.slice_z(&mut self.sync_error_buf)) { + return match sys::rmdir(args.path.slice_z(&mut self.sync_error_buf)) { Err(err) => Err(err.with_path(args.path.slice())), Ok(result) => Ok(result), }; @@ -7726,10 +6970,13 @@ impl NodeFS { if args.recursive { // See the matching comment in `rmdir`: pre-resolve the path on // Windows so rooted-but-driveless paths ("/tmp/foo") get the cwd - // drive prepended before reaching the dt_* / Syscall::*at helpers, + // drive prepended before reaching the dt_* / sys::*at helpers, // which do not do that themselves. #[cfg(windows)] - let resolved = args.path.slice_z(&mut self.sync_error_buf).as_bytes(); + let resolved = match tree_path_windows(&args.path, &mut self.sync_error_buf) { + Ok(resolved) => resolved, + Err(err) => return Err(err.with_path(args.path.slice())), + }; #[cfg(not(windows))] let resolved = args.path.slice(); if let Err(err) = zig_delete_tree(&sys::Dir::cwd(), resolved, sys::FileKind::File) { @@ -7772,9 +7019,9 @@ impl NodeFS { } pub(crate) fn statfs(&mut self, args: &args::StatFS, _: Flavor) -> Maybe { - match Syscall::statfs(args.path.slice_z(&mut self.sync_error_buf)) { + match sys::statfs(args.path.slice_z(&mut self.sync_error_buf)) { Ok(result) => Ok(ret::StatFS::init(&result, args.big_int)), - Err(err) => Err(err), + Err(err) => Err(err.with_path(args.path.slice())), } } @@ -7803,7 +7050,7 @@ impl NodeFS { } }; } - match Syscall::stat(path) { + match sys::stat(path) { Ok(result) => Ok(StatOrNotFound::Stats(Box::new(Stats::init( &PosixStat::init(&result), args.big_int, @@ -7821,8 +7068,6 @@ impl NodeFS { let mut to_buf = bun_paths::path_buffer_pool::get(); #[cfg(windows)] { - const UV_FS_SYMLINK_DIR: c_int = 0x0001; - const UV_FS_SYMLINK_JUNCTION: c_int = 0x0002; #[derive(Clone, Copy, PartialEq, Eq)] enum ResolvedLinkType { File, @@ -7906,15 +7151,12 @@ impl NodeFS { ); break 'target ZStr::from_buf(&self.sync_error_buf[..], target_path.len()); }; - return match Syscall::symlink_uv( - processed_target, - args.new_path.slice_z(&mut to_buf), - match resolved_link_type { - ResolvedLinkType::File => 0, - ResolvedLinkType::Dir => UV_FS_SYMLINK_DIR, - ResolvedLinkType::Junction => UV_FS_SYMLINK_JUNCTION, - }, - ) { + let new_path = args.new_path.slice_z(&mut to_buf); + return match match resolved_link_type { + ResolvedLinkType::File => sys::symlink(processed_target, new_path), + ResolvedLinkType::Dir => sys::symlink_dir(processed_target, new_path), + ResolvedLinkType::Junction => sys::junction(processed_target, new_path), + } { Err(err) => { Err(err.with_path_dest(args.target_path.slice(), args.new_path.slice())) } @@ -7922,7 +7164,7 @@ impl NodeFS { }; } #[cfg(not(windows))] - match Syscall::symlink( + match sys::symlink( args.target_path.slice_z(&mut self.sync_error_buf), args.new_path.slice_z(&mut to_buf), ) { @@ -7938,25 +7180,12 @@ impl NodeFS { let len_i64 = (len & ((1u64 << 63) - 1)) as i64; #[cfg(windows)] { - let file = sys::open( - path.slice_z(&mut self.sync_error_buf), - sys::O::WRONLY | flags, - 0o644, - ); - let Ok(fd) = file else { - let Err(e) = file else { unreachable!() }; - return Err(sys::Error { - errno: e.errno, - path: path.slice().into(), - syscall: sys::Tag::truncate, - ..Default::default() - }); - }; + let path_z = path.slice_z(&mut self.sync_error_buf); + let fd = sys::open(path_z, sys::O::WRONLY | flags, 0o644) + .map_err(|err| err.with_path_and_syscall(path.slice(), sys::Tag::truncate))?; let _close = scopeguard::guard(fd, |fd| fd.close()); - return match Syscall::ftruncate(fd, len_i64) { - Ok(r) => Ok(r), - Err(err) => Err(err.with_path_and_syscall(path.slice(), sys::Tag::truncate)), - }; + return sys::ftruncate(fd, len_i64) + .map_err(|err| err.with_path_and_syscall(path.slice(), sys::Tag::truncate)); } #[cfg(not(windows))] { @@ -7980,28 +7209,18 @@ impl NodeFS { match &args.path { // Mask off the top bit so the i64 cast can't panic. PathOrFileDescriptor::Fd(fd) => { - Syscall::ftruncate(*fd, (args.len & ((1u64 << 63) - 1)) as i64) + sys::ftruncate(*fd, (args.len & ((1u64 << 63) - 1)) as i64) } PathOrFileDescriptor::Path(p) => self.truncate_inner(p, args.len, args.flags), } } pub(crate) fn unlink(&mut self, args: &args::Unlink, _: Flavor) -> Maybe { - #[cfg(windows)] - { - return match Syscall::unlink(args.path.slice_z(&mut self.sync_error_buf)) { - Err(err) => Err(err.with_path(args.path.slice())), - Ok(result) => Ok(result), - }; + let path = args.path.slice_z(&mut self.sync_error_buf); + match sys::unlink(path) { + Err(err) => Err(err.with_path(args.path.slice())), + Ok(result) => Ok(result), } - // SAFETY: path is NUL-terminated by slice_z; unlink(2) is the libc FFI - #[cfg(not(windows))] - Maybe::::errno_sys_p( - unsafe { libc::unlink(args.path.slice_z(&mut self.sync_error_buf).as_ptr().cast()) }, - sys::Tag::unlink, - args.path.slice(), - ) - .unwrap_or(Ok(())) } pub(crate) fn watch_file( @@ -8040,29 +7259,10 @@ impl NodeFS { } pub(crate) fn utimes(&mut self, args: &args::Utimes, _: Flavor) -> Maybe { - #[cfg(windows)] - { - let mut req = UvFsReq::new(); - let rc = unsafe { - uv::uv_fs_utime( - bun_io::Loop::get(), - &mut *req, - args.path.slice_z(&mut self.sync_error_buf).as_ptr(), - args.atime, - args.mtime, - None, - ) - }; - if let Some(err) = rc.to_error(sys::Tag::utime) { - return Err(err.with_path(args.path.slice())); - } - return Ok(()); - } - #[cfg(not(windows))] - match Syscall::utimens( + match sys::utimens( args.path.slice_z(&mut self.sync_error_buf), - to_sys_time_like(args.atime), - to_sys_time_like(args.mtime), + args.atime, + args.mtime, ) { // `err.syscall` must be node's operation name, not `utimensat(2)`. Err(err) => Err(err.with_path_and_syscall(args.path.slice(), sys::Tag::utime)), @@ -8071,29 +7271,10 @@ impl NodeFS { } pub(crate) fn lutimes(&mut self, args: &args::Lutimes, _: Flavor) -> Maybe { - #[cfg(windows)] - { - let mut req = UvFsReq::new(); - let rc = unsafe { - uv::uv_fs_lutime( - bun_io::Loop::get(), - &mut *req, - args.path.slice_z(&mut self.sync_error_buf).as_ptr(), - args.atime, - args.mtime, - None, - ) - }; - if let Some(err) = rc.to_error(sys::Tag::lutime) { - return Err(err.with_path(args.path.slice())); - } - return Ok(()); - } - #[cfg(not(windows))] - match Syscall::lutimens( + match sys::lutimens( args.path.slice_z(&mut self.sync_error_buf), - to_sys_time_like(args.atime), - to_sys_time_like(args.mtime), + args.atime, + args.mtime, ) { // `err.syscall` must be node's operation name, not `utimensat(2)`. Err(err) => Err(err.with_path_and_syscall(args.path.slice(), sys::Tag::lutime)), @@ -8180,6 +7361,7 @@ impl NodeFS { #[cfg(windows)] { + // SAFETY: FFI; `src` is a NUL-terminated wide path. let attributes = unsafe { sys::c::GetFileAttributesW(src.as_ptr()) }; if attributes == sys::c::INVALID_FILE_ATTRIBUTES { return Err(sys::Error { @@ -8213,7 +7395,7 @@ impl NodeFS { } #[cfg(not(windows))] { - let stat_ = match Syscall::lstat(src) { + let stat_ = match sys::lstat(src) { Ok(result) => result, Err(err) => { self.sync_error_buf[..sd].copy_from_slice(src.as_bytes()); @@ -8277,7 +7459,7 @@ impl NodeFS { } } - let fd = match openat_os_path( + let fd = match sys::openat_os_path( FD::cwd(), src, sys::O::DIRECTORY | sys::O::RDONLY | sys::O::NOFOLLOW, @@ -8293,12 +7475,9 @@ impl NodeFS { Ok(_) => {} } - // The OSPathBuffer copy below is generic over `OSPathChar`, so on Windows - // this needs the wide (u16) iterator; the u8 path is correct for POSIX. - #[cfg(windows)] - let mut iterator = DirIterator::WrappedIteratorW::init(fd); - #[cfg(not(windows))] - let mut iterator = DirIterator::WrappedIterator::init(fd); + // The `OSPathBuffer` copy below is over `OSPathChar`: u16 entry names on + // Windows, u8 elsewhere. + let mut iterator = DirIterator::NewWrappedIterator::<{ cfg!(windows) }>::init(fd); loop { let current = match iterator.next() { @@ -8390,14 +7569,14 @@ impl NodeFS { result: Maybe, ) -> Maybe { let Err(ref e) = result else { return result }; - if e.get_errno() != E::BUSY { + if e.get_errno() != E::EBUSY { return result; } let mut buf = bun_paths::path_buffer_pool::get(); - let Ok(statbuf) = Syscall::stat(src.slice_z(&mut buf)) else { + let Ok(statbuf) = sys::stat(src.slice_z(&mut buf)) else { return result; }; - let Ok(new_statbuf) = Syscall::stat(dest.slice_z(&mut buf)) else { + let Ok(new_statbuf) = sys::stat(dest.slice_z(&mut buf)) else { return result; }; if statbuf.st_dev == new_statbuf.st_dev && statbuf.st_ino == new_statbuf.st_ino { @@ -8409,9 +7588,8 @@ impl NodeFS { #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] fn cp_symlink(&mut self, src: &ZStr, dest: &ZStr) -> Maybe { let mut target_buf = bun_paths::path_buffer_pool::get(); - // `bun_sys::readlink` returns the byte length on every - // platform (the `Syscall` alias = `sys_uv` on Windows would return the - // slice itself); reconstruct the NUL-terminated view from `target_buf`. + // `bun_sys::readlink` returns the byte length; reconstruct the + // NUL-terminated view from `target_buf`. let link_len = match sys::readlink(src, &mut target_buf[..]) { Ok(result) => result, Err(err) => { @@ -8423,7 +7601,7 @@ impl NodeFS { // SAFETY: NUL written at `target_buf[link_len]`. let link_target = ZStr::from_buf(&target_buf[..], link_len); if paths::is_absolute(link_target.as_bytes()) { - return Syscall::symlink(link_target, dest); + return sys::symlink(link_target, dest); } let mut cwd_buf = bun_paths::path_buffer_pool::get(); let mut resolved_buf = bun_paths::path_buffer_pool::get(); @@ -8431,7 +7609,7 @@ impl NodeFS { let Ok(cwd_len) = sys::getcwd(&mut cwd_buf[..]) else { // If we can't resolve cwd, preserve the link target as-is rather // than pointing the copied link back at the source path. - return Syscall::symlink(link_target, dest); + return sys::symlink(link_target, dest); }; let cwd = &cwd_buf[..cwd_len]; let resolved_buf_len = resolved_buf.len(); @@ -8453,7 +7631,7 @@ impl NodeFS { let resolved_len = resolved.len(); resolved_buf[resolved_len] = 0; // SAFETY: NUL written at `resolved_buf[resolved_len]`. - Syscall::symlink(ZStr::from_buf(&resolved_buf[..], resolved_len), dest) + sys::symlink(ZStr::from_buf(&resolved_buf[..], resolved_len), dest) } /// This is `copyFile`, but it copies symlinks as-is @@ -8483,7 +7661,7 @@ impl NodeFS { } let stat_ = match reuse_stat { Some(s) => *s, - None => match Syscall::lstat(src) { + None => match sys::lstat(src) { Ok(result) => result, Err(err) => { self.sync_error_buf[..src.len()].copy_from_slice(src.as_bytes()); @@ -8521,7 +7699,7 @@ impl NodeFS { if stat_.st_size > 128 * 1024 { if !mode.shouldnt_overwrite() { // clonefile() will fail if it already exists - let _ = Syscall::unlink(dest); + let _ = sys::unlink(dest); } if Maybe::::errno_sys_p( bun_sys::c::clonefile_rc(src, dest, 0), @@ -8530,11 +7708,11 @@ impl NodeFS { ) .is_none() { - let _ = Syscall::chmod(dest, stat_.st_mode as u32); + let _ = sys::chmod(dest, stat_.st_mode as u32); return Ok(()); } } else { - let src_fd = match Syscall::open(src, sys::O::RDONLY, 0o644) { + let src_fd = match sys::open(src, sys::O::RDONLY, 0o644) { Ok(result) => result, Err(err) => { self.sync_error_buf[..src.len()].copy_from_slice(src.as_bytes()); @@ -8553,8 +7731,8 @@ impl NodeFS { Self::cp_open_dest_with_mkdir(self, dest, flags, stat_.st_mode as Mode)?; let _close_dest = scopeguard::guard((dest_fd, stat_.st_mode, &wrote), |(fd, m, wrote)| { - let _ = Syscall::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); - let _ = Syscall::fchmod(fd, m as u32); + let _ = sys::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); + let _ = sys::fchmod(fd, m as u32); fd.close(); }); @@ -8611,7 +7789,7 @@ impl NodeFS { return Err(sys::Error::todo()); } - let src_fd = match Syscall::open(src, sys::O::RDONLY | sys::O::NOFOLLOW, 0o644) { + let src_fd = match sys::open(src, sys::O::RDONLY | sys::O::NOFOLLOW, 0o644) { Ok(result) => result, Err(err) => { if err.get_errno() == E::ELOOP { @@ -8624,7 +7802,7 @@ impl NodeFS { }; let _close_src = scopeguard::guard(src_fd, |fd| fd.close()); - let stat_ = match Syscall::fstat(src_fd) { + let stat_ = match sys::fstat(src_fd) { Ok(result) => result, Err(err) => return Err(err.with_fd(src_fd)), }; @@ -8650,7 +7828,7 @@ impl NodeFS { if sys::S::ISREG(stat_.st_mode as u32) && sys::copy_file::can_use_ioctl_ficlone() { let rc = sys::linux::ioctl_ficlone(dest_fd, src_fd); if rc == 0 { - let _ = Syscall::fchmod(dest_fd, stat_.st_mode as u32); + let _ = sys::fchmod(dest_fd, stat_.st_mode as u32); dest_fd.close(); return Ok(()); } @@ -8660,8 +7838,8 @@ impl NodeFS { let _close_dest = scopeguard::guard( (dest_fd, stat_.st_mode as Mode, &wrote), |(fd, m, wrote)| { - let _ = Syscall::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); - let _ = Syscall::fchmod(fd, m); + let _ = sys::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); + let _ = sys::fchmod(fd, m); fd.close(); }, ); @@ -8782,7 +7960,7 @@ impl NodeFS { }); } - let src_fd = match Syscall::open(src, sys::O::RDONLY | sys::O::NOFOLLOW, 0o644) { + let src_fd = match sys::open(src, sys::O::RDONLY | sys::O::NOFOLLOW, 0o644) { Ok(result) => result, Err(err) => { // O_NOFOLLOW on a symlink → recreate the link. FreeBSD's @@ -8796,7 +7974,7 @@ impl NodeFS { }; let _close_src = scopeguard::guard(src_fd, |fd| fd.close()); - let stat_ = match Syscall::fstat(src_fd) { + let stat_ = match sys::fstat(src_fd) { Ok(result) => result, Err(err) => return Err(err.with_fd(src_fd)), }; @@ -8822,7 +8000,7 @@ impl NodeFS { // No O_TRUNC at open: if src and dest resolve to the same inode, // that would zero the file before the first read. - if let Ok(dst_stat) = Syscall::fstat(dest_fd) { + if let Ok(dst_stat) = sys::fstat(dest_fd) { if stat_.st_ino == dst_stat.st_ino && stat_.st_dev == dst_stat.st_dev { dest_fd.close(); self.sync_error_buf[..src.len()].copy_from_slice(src.as_bytes()); @@ -8838,8 +8016,8 @@ impl NodeFS { let _close_dest = scopeguard::guard( (dest_fd, stat_.st_mode as Mode, &wrote), |(fd, m, wrote)| { - let _ = Syscall::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); - let _ = Syscall::fchmod(fd, m); + let _ = sys::ftruncate(fd, (wrote.get() & ((1u64 << 63) - 1)) as i64); + let _ = sys::fchmod(fd, m); fd.close(); }, ); @@ -8914,6 +8092,7 @@ impl NodeFS { let stat_ = match reuse_stat { Some(a) => a, None => { + // SAFETY: FFI; `src` is a NUL-terminated wide path. let a = unsafe { sys::c::GetFileAttributesW(src.as_ptr()) }; if a == sys::c::INVALID_FILE_ATTRIBUTES { return Err(sys::Error::from_win32( @@ -8926,6 +8105,7 @@ impl NodeFS { } }; if stat_ & sys::c::FILE_ATTRIBUTE_REPARSE_POINT == 0 { + // SAFETY: FFI; `src` and `dest` are NUL-terminated wide paths. if unsafe { sys::c::CopyFileW( src.as_ptr(), @@ -8940,6 +8120,7 @@ impl NodeFS { &sys::Dir::cwd(), paths::dirname_w(dest.as_slice()), ); + // SAFETY: as above. if unsafe { sys::c::CopyFileW( src.as_ptr(), @@ -8967,6 +8148,7 @@ impl NodeFS { }; let _close = scopeguard::guard(handle, |fd| fd.close()); let mut wbuf = paths::os_path_buffer_pool::get(); + // SAFETY: FFI; `wbuf` is writable for `wbuf.len()` units. let len = unsafe { windows::GetFinalPathNameByHandleW( handle.native(), @@ -9042,7 +8224,7 @@ impl NodeFS { /// `dest` copied into `sync_error_buf`. #[cfg(not(windows))] fn cp_open_dest_with_mkdir(&mut self, dest: &ZStr, flags: i32, mode: Mode) -> Maybe { - match Syscall::open(dest, flags, mode) { + match sys::open(dest, flags, mode) { Ok(result) => Ok(result), Err(err) => { if err.get_errno() == E::ENOENT { @@ -9058,7 +8240,7 @@ impl NodeFS { ..Default::default() }); mkdir_result?; - if let Ok(result) = Syscall::open(dest, flags, mode) { + if let Ok(result) = sys::open(dest, flags, mode) { return Ok(result); } } @@ -9086,36 +8268,6 @@ impl NodeFS { { as NodeFSDispatch>::run(self, args, flavor) } - - #[cfg(windows)] - #[inline] - pub(crate) fn uv_dispatch( - &mut self, - args: &A, - rc: uv::ReturnCodeI64, - ) -> Maybe - where - Op<{ F }>: NodeFSDispatch, - { - as NodeFSDispatch>::run_uv(self, args, rc) - } - - /// Variant of [`Self::uv_dispatch`] for `uv_callbackreq` — passes the live - /// `uv::fs_t` through so the handler can read `req.ptr` (only `statfs` - /// needs it). - #[cfg(windows)] - #[inline] - pub(crate) fn uv_dispatch_req( - &mut self, - args: &A, - req: &mut uv::fs_t, - rc: uv::ReturnCodeI64, - ) -> Maybe - where - Op<{ F }>: NodeFSDispatch, - { - as NodeFSDispatch>::run_uv_req(self, args, req, rc) - } } /// Type-level marker for [`NodeFSDispatch`] — one ZST per `NodeFSFunctionEnum` @@ -9124,31 +8276,16 @@ impl NodeFS { pub(crate) struct Op; /// Per-`F` binding from `(R, A)` to its `NodeFS` method. Every -/// `AsyncFSTask` / `UVFSRequest` instantiation in -/// `async_::*` has exactly one impl, so the `where Op<{F}>: NodeFSDispatch` -/// bound is always satisfied at every monomorphised call site. +/// `AsyncFSTask` instantiation has exactly one impl, so the +/// `where Op<{F}>: NodeFSDispatch` bound is always satisfied at every +/// monomorphised call site. pub(crate) trait NodeFSDispatch { fn run(fs: &mut NodeFS, args: &A, flavor: Flavor) -> Maybe; - #[cfg(windows)] - fn run_uv(_fs: &mut NodeFS, _args: &A, _rc: uv::ReturnCodeI64) -> Maybe { - unreachable!("uv_dispatch: not a UVFSRequest variant") - } - #[cfg(windows)] - fn run_uv_req( - _fs: &mut NodeFS, - _args: &A, - _req: &mut uv::fs_t, - _rc: uv::ReturnCodeI64, - ) -> Maybe { - unreachable!("uv_dispatch_req: not a req-passing UVFSRequest variant") - } } macro_rules! node_fs_ops { ($( $Variant:ident => $method:ident, $Args:ty, $Ret:ty - $(, uv = $uv_method:ident)? - $(, uv_req = $uv_req_method:ident)? );+ $(;)?) => { $( impl NodeFSDispatch<$Ret, $Args> for Op<{ NodeFSFunctionEnum::$Variant }> { @@ -9156,20 +8293,6 @@ macro_rules! node_fs_ops { fn run(fs: &mut NodeFS, args: &$Args, flavor: Flavor) -> Maybe<$Ret> { fs.$method(args, flavor) } - $( - #[cfg(windows)] - #[inline] - fn run_uv(fs: &mut NodeFS, args: &$Args, rc: uv::ReturnCodeI64) -> Maybe<$Ret> { - fs.$uv_method(args, rc) - } - )? - $( - #[cfg(windows)] - #[inline] - fn run_uv_req(fs: &mut NodeFS, args: &$Args, req: &mut uv::fs_t, rc: uv::ReturnCodeI64) -> Maybe<$Ret> { - fs.$uv_req_method(args, req, rc) - } - )? } )+ }; @@ -9180,7 +8303,7 @@ node_fs_ops! { AppendFile => append_file, args::AppendFile<'static>, ret::AppendFile; Chmod => chmod, args::Chmod<'static>, ret::Chmod; Chown => chown, args::Chown<'static>, ret::Chown; - Close => close, args::Close, ret::Close, uv = uv_close; + Close => close, args::Close, ret::Close; CopyFile => copy_file, args::CopyFile<'static>, ret::CopyFile; Exists => exists, args::Exists<'static>, ret::Exists; Fchmod => fchmod, args::FChmod, ret::Fchmod; @@ -9197,26 +8320,26 @@ node_fs_ops! { Lutimes => lutimes, args::Lutimes<'static>, ret::Lutimes; Mkdir => mkdir, args::Mkdir<'static>, ret::Mkdir; Mkdtemp => mkdtemp, args::MkdirTemp<'static>, ret::Mkdtemp; - Open => open, args::Open<'static>, ret::Open, uv = uv_open; - Read => read, args::Read, ret::Read, uv = uv_read; + Open => open, args::Open<'static>, ret::Open; + Read => read, args::Read, ret::Read; Readdir => readdir, args::Readdir<'static>, ret::Readdir; ReadFile => read_file, args::ReadFile<'static>, ret::ReadFile; Readlink => readlink, args::Readlink<'static>, ret::Readlink; - Readv => readv, args::Readv, ret::Readv, uv = uv_readv; + Readv => readv, args::Readv, ret::Readv; Realpath => realpath, args::Realpath<'static>, ret::Realpath; RealpathNonNative => realpath_non_native, args::Realpath<'static>, ret::Realpath; Rename => rename, args::Rename<'static>, ret::Rename; Rm => rm, args::Rm<'static>, ret::Rm; Rmdir => rmdir, args::RmDir<'static>, ret::Rmdir; Stat => stat, args::Stat<'static>, ret::Stat; - Statfs => statfs, args::StatFS<'static>, ret::StatFS, uv_req = uv_statfs; + Statfs => statfs, args::StatFS<'static>, ret::StatFS; Symlink => symlink, args::Symlink<'static>, ret::Symlink; Truncate => truncate, args::Truncate<'static>, ret::Truncate; Unlink => unlink, args::Unlink<'static>, ret::Unlink; Utimes => utimes, args::Utimes<'static>, ret::Utimes; - Write => write, args::Write<'static>, ret::Write, uv = uv_write; + Write => write, args::Write<'static>, ret::Write; WriteFile => write_file, args::WriteFile<'static>, ret::WriteFile; - Writev => writev, args::Writev, ret::Writev, uv = uv_writev; + Writev => writev, args::Writev, ret::Writev; } #[derive(Copy, Clone, PartialEq, Eq)] @@ -9574,7 +8697,7 @@ fn dt_open_dir(parent: &sys::Dir, name: &[u8]) -> Result { path_buf[len] = 0; // SAFETY: NUL written at [len]. let z = ZStr::from_buf(&path_buf[..], len); - match Syscall::openat( + match sys::openat( parent.fd, z, sys::O::DIRECTORY | sys::O::RDONLY | sys::O::NOFOLLOW, @@ -9593,7 +8716,7 @@ fn dt_delete_file(parent: &sys::Dir, name: &[u8]) -> Result<(), E> { path_buf[len] = 0; // SAFETY: NUL written at [len]. let z = ZStr::from_buf(&path_buf[..], len); - match Syscall::unlinkat(parent.fd, z) { + match sys::unlinkat(parent.fd, z) { Ok(()) => Ok(()), Err(e) => { let errno = e.get_errno(); @@ -9614,7 +8737,7 @@ fn dt_delete_file(parent: &sys::Dir, name: &[u8]) -> Result<(), E> { if matches!(errno, E::EPERM | E::EACCES) { // No-follow stat — don't follow symlinks, to match unlinkat. // `z` (a `&ZStr`, `Copy`) is still valid — `unlinkat` only borrowed it. - if let Ok(st) = Syscall::lstatat(parent.fd, z) { + if let Ok(st) = sys::lstatat(parent.fd, z) { if sys::S::ISDIR(st.st_mode as u32) { return Err(E::EISDIR); } @@ -9633,11 +8756,7 @@ fn dt_delete_dir(parent: &sys::Dir, name: &[u8]) -> Result<(), E> { path_buf[len] = 0; // SAFETY: NUL written at [len]. let z = ZStr::from_buf(&path_buf[..], len); - #[cfg(unix)] - let flags: i32 = libc::AT_REMOVEDIR; - #[cfg(not(unix))] - let flags = 0x200; // AT_REMOVEDIR — Windows path goes through sys_uv which maps this. - match Syscall::unlinkat_with_flags(parent.fd, z, flags) { + match sys::unlinkat_with_flags(parent.fd, z, sys::AT_REMOVEDIR) { Ok(()) => Ok(()), Err(e) => Err(e.get_errno()), } @@ -9653,6 +8772,45 @@ struct DeleteTreeStackItem { /// owner; this is just the raw fd for `Dir::borrow` at call sites. parent_dir: FD, iter: DirIterator::WrappedIterator, + /// How often the directory was found not empty after all of it was deleted. + rescans: u8, +} + +/// A directory that is not empty after everything in it was deleted is gone +/// through again: something may have been added meanwhile. Not for ever: +/// where deletes lack POSIX semantics (FAT, exFAT), a file that is open +/// elsewhere stays listed until it is closed. +const DELETE_TREE_MAX_RESCANS: u8 = 50; + +/// `path` for [`zig_delete_tree`]. See `rmdir` for a rooted path. A relative one +/// with `.` or `..` in it is resolved here, off the spelling of the current +/// directory, as Win32 and Node resolve it: the `sys::*at` helpers resolve it off +/// the directory's own path, which is where a link in that spelling leads. +#[cfg(windows)] +fn tree_path_windows<'a>( + path: &'a PathLike, + buf: &'a mut bun_paths::PathBuffer, +) -> Maybe<&'a [u8]> { + let written = path.slice(); + let has_drive = + written.len() >= 2 && bun_paths::is_drive_letter(written[0]) && written[1] == b':'; + if has_drive + || bun_paths::is_absolute(written) + || !strings::split_any(written, b"/\\").any(|name| name == b"." || name == b"..") + { + return Ok(path.slice_z(buf).as_bytes()); + } + let mut cwd = bun_paths::path_buffer_pool::get(); + let cwd_len = sys::getcwd(&mut cwd[..])?; + let joined = bun_paths::resolve_path::join_abs_string_buf_checked::( + &cwd[..cwd_len], + &mut buf[..], + &[written], + ); + let Some(len) = joined.map(<[u8]>::len) else { + return Err(sys::Error::from_code(E::ENAMETOOLONG, sys::Tag::rm)); + }; + Ok(&buf[..len]) } pub(crate) fn zig_delete_tree( @@ -9684,6 +8842,7 @@ pub(crate) fn zig_delete_tree( name_is_borrowed: true, parent_dir: self_.fd, iter: DirIterator::WrappedIterator::init(initial_iterable_dir.into_raw()), + rescans: 0, }); 'process_stack: while !stack.is_empty() { @@ -9714,6 +8873,7 @@ pub(crate) fn zig_delete_tree( iter: DirIterator::WrappedIterator::init( iterable_dir.into_raw(), ), + rescans: 0, }); continue 'process_stack; } @@ -9845,6 +9005,9 @@ pub(crate) fn zig_delete_tree( } if need_to_retry { + if top.rescans == DELETE_TREE_MAX_RESCANS { + return Err(dt_err(E::ENOTEMPTY)); + } // Since we closed the handle that the previous iterator used, we // need to re-open the dir and re-create the iterator. let mut treat_as_dir = true; @@ -9888,6 +9051,7 @@ pub(crate) fn zig_delete_tree( name_is_borrowed: top.name_is_borrowed, parent_dir, iter: DirIterator::WrappedIterator::init(iterable_dir.into_raw()), + rescans: top.rescans + 1, }); continue 'process_stack; } @@ -9929,6 +9093,7 @@ fn zig_delete_tree_min_stack_size_with_kind_hint( sub_path: &[u8], kind_hint: sys::FileKind, ) -> crate::Result<()> { + let mut rescans = 0; 'start_over: loop { let mut dir = match zig_delete_tree_open_initial_subpath(self_, sub_path, kind_hint)? { Some(d) => d, @@ -10013,8 +9178,12 @@ fn zig_delete_tree_min_stack_size_with_kind_hint( }; if let Some(d) = cleanup_dir_parent { match dt_delete_dir(&d, dir_name) { - Ok(()) | Err(E::ENOENT) | Err(E::ENOTEMPTY) | Err(E::EEXIST) => { - // These two things can happen due to file system race conditions. + Ok(()) | Err(E::ENOENT) => { + rescans = 0; + continue 'start_over; + } + Err(E::ENOTEMPTY) | Err(E::EEXIST) if rescans < DELETE_TREE_MAX_RESCANS => { + rescans += 1; continue 'start_over; } Err(e) => { @@ -10024,7 +9193,10 @@ fn zig_delete_tree_min_stack_size_with_kind_hint( } else { match dt_delete_dir(self_, sub_path) { Ok(()) | Err(E::ENOENT) => return Ok(()), - Err(E::ENOTEMPTY) | Err(E::EEXIST) => continue 'start_over, + Err(E::ENOTEMPTY) | Err(E::EEXIST) if rescans < DELETE_TREE_MAX_RESCANS => { + rescans += 1; + continue 'start_over; + } Err(e) => return Err(dt_err(e)), } } @@ -10081,26 +9253,6 @@ pub(crate) enum NodeFSFunctionEnum { Writev, } -impl NodeFSFunctionEnum { - /// The event-loop [`TaskTag`] of the ops that are libuv requests on - /// Windows (`UVFSRequest`) and so re-enter through the task queue; every - /// other async op is a `bun_jsc::Job` and needs none. - #[cfg(windows)] - pub(crate) const fn task_tag(self) -> bun_event_loop::TaskTag { - use bun_event_loop::task_tag; - match self { - NodeFSFunctionEnum::Open => task_tag::Open, - NodeFSFunctionEnum::Close => task_tag::Close, - NodeFSFunctionEnum::Read => task_tag::Read, - NodeFSFunctionEnum::Write => task_tag::Write, - NodeFSFunctionEnum::Readv => task_tag::Readv, - NodeFSFunctionEnum::Writev => task_tag::Writev, - NodeFSFunctionEnum::Statfs => task_tag::StatFS, - _ => panic!("not a libuv-request fs op"), - } - } -} - /// `i52` — 52-bit integer used for `ReadPosition` coercion bounds. #[allow(non_camel_case_types)] struct i52; diff --git a/src/runtime/node/node_fs_binding.rs b/src/runtime/node/node_fs_binding.rs index 5696b42e8d77..39b017478554 100644 --- a/src/runtime/node/node_fs_binding.rs +++ b/src/runtime/node/node_fs_binding.rs @@ -7,18 +7,17 @@ use bun_sys_jsc::SystemErrorJsc as _; use crate::node::ThreadIsolated; use crate::node::fs::{ - self, AsyncCpTask, AsyncReaddirRecursiveTask, Flavor, FsArgument, FsReturn, NodeFS, - NodeFSDispatch, NodeFSFunctionEnum, Op, args, async_, ret, + self, AsyncCpTask, AsyncFSTask, AsyncReaddirRecursiveTask, Flavor, FsArgument, FsReturn, + NodeFS, NodeFSDispatch, NodeFSFunctionEnum, Op, args, async_, ret, }; /// Signature of every generated NodeFS host function. pub(crate) type NodeFSFunction = fn(this: &Binding, global: &JSGlobalObject, frame: &CallFrame) -> JsResult; -// The (`args::*`, `ret::*`, `NodeFS::`, `async_::*`) quadruples are -// spelled out once in `node_fs.rs` (the `NodeFS::dispatch` table + -// `async_::*` aliases) and reused here via the `node_fs_bindings!` macro at -// the bottom of this file. +// The (`args::*`, `ret::*`, `NodeFS::`) triples are spelled out once in +// `node_fs.rs` (the `NodeFS::dispatch` table) and reused here via the +// `node_fs_bindings!` macro at the bottom of this file. /// Returns bindings to call jsc.Node.fs.NodeFS.. /// Async calls use a thread pool. @@ -37,6 +36,9 @@ where let vm: &VirtualMachine = global.bun_vm(); let mut slice = ArgumentsSlice::init(vm, frame.arguments()); let args = ::from_js(global, &mut slice)?; + if let Some(fd_use) = args.fd_use() { + fd_use.on_js_thread(vm); + } // R-2: `JsCell::with_mut` scopes the `&mut NodeFS` to the blocking // syscall; `dispatch` never re-enters JS, and `Maybe` is fully owned @@ -77,33 +79,22 @@ fn split_callback<'a>( /// `Bindings(FunctionEnum).runAsync` for every operation except `.cp` / /// `.readdir` (those have bespoke entry points below). -/// -/// `create_task` is `async_::::create` — passed in because the -/// Windows path picks `UVFSRequest` for a handful of fds-only ops while -/// everything else uses `AsyncFSTask`, and that choice is encoded in the -/// `async_::*` type aliases rather than derivable from `F` alone. -fn run_async( - this: &Binding, +fn run_async( global: &JSGlobalObject, frame: &CallFrame, arm: AsyncArm, - create_task: fn( - &bun_jsc::JsThread<'_>, - &Binding, - ThreadIsolated, - &mut VirtualMachine, - Option, - ) -> JSValue, -) -> JsResult { +) -> JsResult +where + Op<{ F }>: NodeFSDispatch, +{ let (callback, arguments) = split_callback(global, frame, arm)?; let args = match parse_async_args::(global, arguments)? { ParsedAsyncArgs::Args(args) => args, ParsedAsyncArgs::Rejected(error) => return reject_before_schedule(global, callback, error), }; let vm: &mut VirtualMachine = global.bun_vm().as_mut(); - Ok(create_task( + Ok(AsyncFSTask::::create( &global.js_thread_of_caller(frame), - this, args, vm, callback, @@ -212,7 +203,11 @@ impl Binding { // ── Hand-written bindings for ops outside `NodeFSFunctionEnum` ──────── /// `callAsync(.cp)`. - pub(crate) fn cp(this: &Self, global: &JSGlobalObject, frame: &CallFrame) -> JsResult { + pub(crate) fn cp( + _this: &Self, + global: &JSGlobalObject, + frame: &CallFrame, + ) -> JsResult { let cp_args = match parse_async_args::>(global, frame.arguments())? { ParsedAsyncArgs::Args(args) => args, ParsedAsyncArgs::Rejected(error) => return reject_before_schedule(global, None, error), @@ -220,7 +215,6 @@ impl Binding { let vm: &mut VirtualMachine = global.bun_vm().as_mut(); Ok(AsyncCpTask::create( &global.js_thread_of_caller(frame), - this, cp_args, vm, )) @@ -249,27 +243,22 @@ impl Binding { /// `callAsync(.readdir)` — `args.recursive` selects /// `AsyncReaddirRecursiveTask` instead of the generic `AsyncFSTask`. pub(crate) fn readdir( - this: &Self, + _this: &Self, global: &JSGlobalObject, frame: &CallFrame, ) -> JsResult { - Self::run_readdir(this, global, frame, AsyncArm::Promise) + Self::run_readdir(global, frame, AsyncArm::Promise) } pub(crate) fn readdir_cb( - this: &Self, + _this: &Self, global: &JSGlobalObject, frame: &CallFrame, ) -> JsResult { - Self::run_readdir(this, global, frame, AsyncArm::Callback) + Self::run_readdir(global, frame, AsyncArm::Callback) } - fn run_readdir( - this: &Self, - global: &JSGlobalObject, - frame: &CallFrame, - arm: AsyncArm, - ) -> JsResult { + fn run_readdir(global: &JSGlobalObject, frame: &CallFrame, arm: AsyncArm) -> JsResult { let (callback, arguments) = split_callback(global, frame, arm)?; let rd_args = match parse_async_args::>(global, arguments)? { ParsedAsyncArgs::Args(args) => args, @@ -291,7 +280,6 @@ impl Binding { } Ok(async_::Readdir::create( &global.js_thread_of_caller(frame), - this, rd_args, vm, callback, @@ -359,18 +347,26 @@ macro_rules! node_fs_bindings { pub(crate) const $sync: NodeFSFunction = call_sync::<$Ret, $Args, { NodeFSFunctionEnum::$F }>(); pub(crate) fn $async_( - this: &Self, + _this: &Self, global: &JSGlobalObject, frame: &CallFrame, ) -> JsResult { - run_async::<$Args>(this, global, frame, AsyncArm::Promise, async_::$F::create) + run_async::<$Ret, $Args, { NodeFSFunctionEnum::$F }>( + global, + frame, + AsyncArm::Promise, + ) } pub(crate) fn $callback( - this: &Self, + _this: &Self, global: &JSGlobalObject, frame: &CallFrame, ) -> JsResult { - run_async::<$Args>(this, global, frame, AsyncArm::Callback, async_::$F::create) + run_async::<$Ret, $Args, { NodeFSFunctionEnum::$F }>( + global, + frame, + AsyncArm::Callback, + ) } )* } @@ -484,14 +480,7 @@ pub(crate) fn string_to_flags_for_testing( use crate::node::types::FileSystemFlags; let [val] = frame.arguments_as_array::<1>(); let flags = FileSystemFlags::from_js(global, val)?.unwrap_or(FileSystemFlags::R); - // On Windows the internal bun.O bits are POSIX-shaped and translated to the - // MSVCRT `_O_*` values at the open boundary; node's stringToFlags and - // fs.constants both speak MSVCRT, so translate here too. - #[cfg(windows)] - let bits = bun_sys::windows::libuv::O::from_bun_o(flags.as_int()); - #[cfg(not(windows))] - let bits = flags.as_int(); - Ok(JSValue::js_number_from_int32(bits)) + Ok(JSValue::js_number_from_int32(flags.as_int())) } #[bun_jsc::host_fn] diff --git a/src/runtime/node/node_fs_watcher.rs b/src/runtime/node/node_fs_watcher.rs index dfc1ea04c13c..4d0b5ff7fc50 100644 --- a/src/runtime/node/node_fs_watcher.rs +++ b/src/runtime/node/node_fs_watcher.rs @@ -1,12 +1,10 @@ use core::cell::Cell; use core::ffi::c_void; -#[cfg(not(windows))] use core::mem::MaybeUninit; use core::sync::atomic::{AtomicU32, Ordering}; use bun_core::Output; use bun_core::strings; -#[cfg(not(windows))] use bun_event_loop::ConcurrentTask::ConcurrentTask; use bun_event_loop::{Task, TaskTag, Taskable, task_tag}; use bun_io::KeepAlive; @@ -27,10 +25,7 @@ use crate::webcore::encoding as Encoder; bun_output::declare_scope!(fs_watch, hidden); -#[cfg(not(windows))] use super::path_watcher; -#[cfg(windows)] -use super::win_watcher as path_watcher; // TODO: make this a top-level struct // R-2 (host-fn re-entrancy): every JS-exposed method takes `&self`; per-field @@ -46,12 +41,9 @@ pub(crate) struct FSWatcher { /// JS-thread uses only. ctx: *mut VirtualMachine, /// How the (process-wide) watcher thread delivers event batches to the - /// VM while this watcher is attached (POSIX; on Windows libuv delivers fs - /// events on the JS thread). Weak: `detach()` — close, the VM's stop + /// VM while this watcher is attached. Weak: `detach()` — close, the VM's stop /// phase, or finalize — is what ends the thread's access to `self`. - #[cfg(not(windows))] handle: bun_jsc::VmHandle, - #[cfg(not(windows))] loop_kind: bun_jsc::LoopKind, verbose: bool, @@ -65,8 +57,7 @@ pub(crate) struct FSWatcher { /// `has_pending_activity()` while the watcher is open; a strong ref here /// would self-pin it forever. Cleared by `detach()`. js_this: JsCell, - // pub(super): read directly by `win_watcher::PathWatcher::emit`. - pub(super) encoding: Encoding, + encoding: Encoding, /// User can call close and pre-detach so we need to track this closed: Cell, @@ -95,15 +86,6 @@ pub(crate) mod js { } impl FSWatcher { - /// JS thread only (Windows delivers fs events on the loop thread). - #[cfg(windows)] - #[inline] - fn vm(&self) -> &mut VirtualMachine { - // SAFETY: `ctx` is the live per-thread VM (set in `init`); every caller - // is on its JS thread. - unsafe { &mut *self.ctx } - } - #[inline] fn vm_ctx(&self) -> bun_io::EventLoopCtx { // SAFETY: `self.ctx` is the live per-thread VM singleton backref. @@ -113,7 +95,6 @@ impl FSWatcher { /// Watcher thread → JS thread. `task` is the intrusive node of a heap batch /// task; the queue takes ownership unless the VM has been torn down, in /// which case the caller gets it back. - #[cfg(not(windows))] pub(crate) fn post( &self, task: core::ptr::NonNull, @@ -138,16 +119,7 @@ impl FSWatcher { } } -#[cfg(windows)] -pub(crate) type FSWatchTask = FSWatchTaskWindows; -#[cfg(not(windows))] -pub(crate) type FSWatchTask = FSWatchTaskPosix; - -// `Event::Rename`/`Change` carry `StringOrBytesToDecode` on Windows, which -// does not coerce to the `&[u8]` `emit()` expects — gate the whole posix task -// to keep the Windows build sound. -#[cfg(not(windows))] -pub(crate) struct FSWatchTaskPosix { +pub(crate) struct FSWatchTask { /// `None` only during `FSWatcher::init` two-phase construction (the task is /// embedded as `current_task` before the boxed `FSWatcher` address is /// known); patched to `Some` immediately after. @@ -158,8 +130,7 @@ pub(crate) struct FSWatchTaskPosix { concurrent_task: ConcurrentTask, } -#[cfg(not(windows))] -impl Taskable for FSWatchTaskPosix { +impl Taskable for FSWatchTask { const TAG: TaskTag = task_tag::FSWatchTask; /// A batch of events the watcher thread posted that nobody will emit: /// free it (its entries own their paths) and drop the activity unit it @@ -179,14 +150,12 @@ impl Taskable for FSWatchTaskPosix { } } -#[cfg(not(windows))] pub(crate) struct Entry { event: Event, needs_free: bool, } -#[cfg(not(windows))] -impl FSWatchTaskPosix { +impl FSWatchTask { fn ctx(&self) -> &FSWatcher { // BACKREF — `ctx` is the live owning FSWatcher (set right after // boxing in `init`); FSWatcher outlives all its tasks. @@ -256,7 +225,7 @@ impl FSWatchTaskPosix { // if false is closed or detached (can still contain valid refs but will not create a new one) if self.ctx().ref_task() { // Reshaped for borrowck — clone self into a heap task, then reset. - let that = bun_core::heap::into_raw(Box::new(FSWatchTaskPosix { + let that = bun_core::heap::into_raw(Box::new(FSWatchTask { ctx: self.ctx, count: self.count, entries: core::mem::replace( @@ -302,9 +271,8 @@ impl FSWatchTaskPosix { } } -#[cfg(not(windows))] -impl FSWatchTaskPosix { - /// `FSWatchTaskPosix.deinit`. **Not** `impl Drop`: +impl FSWatchTask { + /// **Not** `impl Drop`: /// this is only ever called on heap clones produced by `enqueue()` (via the /// task dispatcher), never on the embedded `FSWatcher.current_task` field — /// the assert below enforces that. A `Drop` impl would also fire on @@ -326,9 +294,6 @@ impl FSWatchTaskPosix { } } -#[cfg(windows)] -pub(crate) type EventPathString = StringOrBytesToDecode; -#[cfg(not(windows))] pub(crate) type EventPathString = Box<[u8]>; /// The kind of change a watcher backend reports for a path, before it becomes a JS event. @@ -359,8 +324,7 @@ pub(crate) enum Event { close: bool, }, /// An event with no filename, surfaced to JS with `null`, matching node: - /// `Change` when the OS event queue overflowed and changes were lost, - /// `Rename` when libuv could not convert a name to UTF-8 (Windows). + /// the OS event queue overflowed and changes were lost. #[cfg(not(any(target_os = "macos", target_os = "freebsd")))] NoFilename(WatchEventKind), Abort, @@ -385,172 +349,12 @@ unsafe extern "C" { safe fn Bun__domEventNameToJS(global: &JSGlobalObject, event_type: EventType) -> JSValue; } -#[cfg(windows)] -pub(crate) struct FSWatchTaskWindows { - event: Event, - ctx: Option>, -} - -#[cfg(windows)] -impl Taskable for FSWatchTaskWindows { - const TAG: TaskTag = task_tag::FSWatchTask; - /// As the POSIX task: free the batch, drop the activity unit. - unsafe fn release_unrun(this: *mut Self) { - // SAFETY: fn contract; the FSWatcher outlives its tasks. - unsafe { - let ctx = (*this).ctx; - Self::deinit(this); - ctx.expect("FSWatchTask.ctx unset").get().unref_task(); - } - } - /// As `FSWatchTaskPosix`. - unsafe fn context(_: *const Self) -> bun_event_loop::ContextId { - bun_event_loop::ContextId::NONE - } -} - -#[cfg(windows)] -impl Default for FSWatchTaskWindows { - fn default() -> Self { - Self { - event: Event::Error { - err: bun_sys::Error { - errno: SystemErrno::EINVAL as _, - syscall: bun_sys::Tag::watch, - ..Default::default() - }, - close: true, - }, - ctx: None, - } - } -} - -#[cfg(windows)] -pub(crate) enum StringOrBytesToDecode { - String(bun_core::String), - BytesToFree(Box<[u8]>), -} - -// `PathWatcher::emit` and `Event::dupe` take a borrowed `&[u8]` rel-path and box -// it into the owned `bytes_to_free` arm so the Windows task can carry it across -// the thread hop. -#[cfg(windows)] -impl From<&[u8]> for StringOrBytesToDecode { - #[inline] - fn from(bytes: &[u8]) -> Self { - StringOrBytesToDecode::BytesToFree(Box::<[u8]>::from(bytes)) - } -} - -#[cfg(windows)] -impl core::fmt::Display for StringOrBytesToDecode { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - StringOrBytesToDecode::String(s) => write!(f, "{}", s), - StringOrBytesToDecode::BytesToFree(utf8) => { - write!(f, "{}", bstr::BStr::new(utf8)) - } - } - } -} - -#[cfg(windows)] -impl FSWatchTaskWindows { - pub(crate) fn append_abort(&mut self) { - let ctx = self.ctx; - // Balance the `ctx.unrefTask()` at the end of `run()` (matches - // `onPathUpdateWindows` and the posix `enqueue()` path). - // SAFETY: ParentRef — `ctx` is the live owning FSWatcher set at - // construction; FSWatcher outlives every task it enqueues. - // R-2: `ref_task` takes `&self`; ParentRef Derefs to `&FSWatcher`. - if !ctx.expect("FSWatchTask.ctx unset").ref_task() { - return; - } - let task = bun_core::heap::into_raw(Box::new(FSWatchTaskWindows { - ctx, - event: Event::Abort, - })); - - // `ctx` is the live owning `ParentRef` (BACKREF); `vm()` → - // `event_loop_mut()` is the audited safe `&mut EventLoop` accessor. - // Ownership of `task` transfers to the queue (drained on the same thread). - ctx.expect("FSWatchTask.ctx unset") - .vm() - .event_loop_mut() - .enqueue_task(Task::init(task)); - } - - /// this runs on JS Context Thread - pub(crate) fn run(&mut self) -> JsResult<()> { - // BACKREF — `self.ctx` is the live owning FSWatcher (set at - // construction), outliving every task it enqueues. R-2: all FSWatcher - // methods below take `&self`, so a single `&FSWatcher` held across the - // match is sound (aliased shared borrows are fine; the old `*mut Self` - // re-derive dance is no longer needed). `ParentRef` Derefs to `&T`. - let ctx: &FSWatcher = &self.ctx.expect("FSWatchTask.ctx unset"); - let _unref = scopeguard::guard((), |()| ctx.unref_task()); - match &mut self.event { - Event::Rename(path) => Self::run_path::<{ EventType::Rename }>(ctx, path), - Event::Change(path) => Self::run_path::<{ EventType::Change }>(ctx, path), - Event::Error { err, close } => { - ctx.emit_error(err, *close); - Ok(()) - } - Event::NoFilename(event_type) => { - ctx.emit_null_filename(*event_type); - Ok(()) - } - Event::Abort => { - ctx.emit_if_aborted(); - Ok(()) - } - } - } - - fn run_path( - ctx: &FSWatcher, - path: &mut StringOrBytesToDecode, - ) -> JsResult<()> { - use bun_jsc::StringJsc; - if ctx.encoding == Encoding::Utf8 { - let StringOrBytesToDecode::String(s) = path else { - // Producer invariant (win_watcher::on_path_update_windows): when - // `ctx.encoding == Utf8` the payload is always the `String` - // variant, and `encoding` is immutable after init. - unreachable!() - }; - let js = core::mem::take(s).into_js(&ctx.global_this)?; - ctx.emit_with_filename::(js); - Ok(()) - } else { - let StringOrBytesToDecode::BytesToFree(bytes_ref) = path else { - unreachable!() - }; - let bytes = core::mem::take(bytes_ref); - ctx.emit::(&bytes) - } - } - - /// `FSWatchTaskWindows.deinit`. Explicit, not - /// `impl Drop`, to mirror `FSWatchTaskPosix::deinit` so the dispatcher can - /// call `FSWatchTask::deinit` uniformly. - /// - /// # Safety - /// `this` must be the unique `heap::alloc` pointer produced by - /// `append_abort()` / `on_path_update_windows()`. - pub(crate) unsafe fn deinit(this: *mut Self) { - // SAFETY: paired with `heap::alloc` at the enqueue site. - drop(unsafe { bun_core::heap::take(this) }); - } -} - impl FSWatcher { /// Recover `&FSWatcher` from the `*mut c_void` userdata stashed in `init`. /// /// Centralises the set-once `Option<*mut c_void> → &FSWatcher` deref so the - /// three watcher-backend callbacks (`on_path_update_*`, `on_update_end`) - /// stay safe at the call site. R-2: deref as shared — all `FSWatcher` + /// two watcher-backend callbacks (`on_path_update`, `on_update_end`) + /// stay safe at the call site. Deref as shared: all `FSWatcher` /// mutation goes through `Cell`/`JsCell`. #[inline] fn from_ctx<'a>(ctx: Option<*mut c_void>) -> &'a FSWatcher { @@ -561,8 +365,7 @@ impl FSWatcher { unsafe { &*ctx.unwrap().cast::() } } - #[cfg(not(windows))] - pub(crate) fn on_path_update_posix(ctx: Option<*mut c_void>, event: Event, is_file: bool) { + pub(crate) fn on_path_update(ctx: Option<*mut c_void>, event: Event, is_file: bool) { let this = Self::from_ctx(ctx); if this.verbose { @@ -587,56 +390,13 @@ impl FSWatcher { this.current_task.with_mut(|t| t.append(event, true)); } - #[cfg(windows)] - pub(crate) fn on_path_update_windows(ctx: Option<*mut c_void>, event: Event, is_file: bool) { - let this = Self::from_ctx(ctx); - - if this.verbose { - match &event { - Event::Rename(value) | Event::Change(value) => { - if is_file { - bun_core::pretty_errorln!(" File changed: {}", value); - } else { - bun_core::pretty_errorln!(" Dir changed: {}", value); - } - } - _ => {} - } - } - - if !this.ref_task() { - return; - } - - let task = bun_core::heap::into_raw(Box::new(FSWatchTaskWindows { - // SAFETY: `this` is the live owning `&FSWatcher` (BACKREF) recovered - // from the registered userdata; outlives every task it enqueues. - ctx: Some(unsafe { bun_ptr::ParentRef::from_raw(this.as_ctx_ptr()) }), - event, - })); - // `vm()` is the BACKREF accessor; `event_loop_mut()` is the audited - // safe `&mut EventLoop` accessor. Ownership of `task` transfers to the - // queue. - this.vm().event_loop_mut().enqueue_task(Task::init(task)); - } - - #[cfg(windows)] - pub(crate) const ON_PATH_UPDATE: fn(Option<*mut c_void>, Event, bool) = - Self::on_path_update_windows; - #[cfg(not(windows))] - pub(crate) const ON_PATH_UPDATE: fn(Option<*mut c_void>, Event, bool) = - Self::on_path_update_posix; - pub(crate) fn on_update_end(ctx: Option<*mut c_void>) { let this = Self::from_ctx(ctx); if this.verbose { Output::flush(); } - #[cfg(unix)] - { - // we only enqueue after all events are processed - this.current_task.with_mut(|t| t.enqueue()); - } + // we only enqueue after all events are processed + this.current_task.with_mut(|t| t.enqueue()); } } @@ -1101,7 +861,7 @@ impl FSWatcher { self.abort_handle.leave(); if let Some(watcher) = self.path_watcher.take() { - // Both backends expose `detach` as an associated fn over `*mut PathWatcher` + // `detach` is an associated fn over `*mut PathWatcher` // (it self-destroys via `heap::take` on the last handler, so it cannot // soundly take `&mut self`). `watcher` is the live pointer returned by // `path_watcher::watch`. @@ -1167,9 +927,7 @@ impl FSWatcher { let ctx = bun_core::heap::into_raw(Box::new(FSWatcher { ctx: vm, - #[cfg(not(windows))] handle: vm_ref.handle(), - #[cfg(not(windows))] loop_kind: vm_ref.current_loop_kind(), current_task: JsCell::new(FSWatchTask { ctx: None, @@ -1198,20 +956,7 @@ impl FSWatcher { ctx_ref .path_watcher .set(if args.signal.is_none_or(|s| !s.aborted()) { - // The two backends take different arities (the Windows - // backend dropped the callback parameters — only one valid - // value each), so the call is cfg-split. - #[cfg(windows)] - let r = path_watcher::watch(vm_ref, file_path, args.recursive, ctx as *mut c_void); - #[cfg(not(windows))] - let r = path_watcher::watch( - vm_ref, - file_path, - args.recursive, - FSWatcher::ON_PATH_UPDATE, - FSWatcher::on_update_end, - ctx.cast::(), - ); + let r = path_watcher::watch(file_path, args.recursive, ctx.cast::()); match r { Ok(r) => Some(r), Err(err) => { @@ -1243,8 +988,7 @@ impl FSWatcher { } } -#[cfg(not(windows))] -impl Default for FSWatchTaskPosix { +impl Default for FSWatchTask { fn default() -> Self { Self { ctx: None, diff --git a/src/runtime/node/node_os.rs b/src/runtime/node/node_os.rs index f5abb8887154..50f9707e4dc6 100644 --- a/src/runtime/node/node_os.rs +++ b/src/runtime/node/node_os.rs @@ -10,6 +10,8 @@ unsafe extern "C" { safe fn bun_sysconf__SC_NPROCESSORS_ONLN() -> i32; } +/// Milliseconds. The layout of `BunCpuInfo::cpu_times` (`OsBinding.h`). +#[repr(C)] #[derive(Default, Clone, Copy)] pub(crate) struct CPUTimes { pub user: u64, @@ -37,14 +39,139 @@ mod _impl { use bun_core::{env_var, fmt as bun_fmt}; use bun_jsc::{CallFrame, JSArray, StringJsc as _, SysErrorJsc as _, SystemError}; - #[cfg(windows)] - use bun_sys::ReturnCodeExt as _; #[cfg(not(windows))] use bun_sys::c; #[cfg(windows)] - use bun_sys::windows::{self, libuv}; + use bun_sys::windows; use std::io::Write as _; + /// Win32 declarations for `node:os`, and the C++ exports + /// (`OsBinding.cpp`) behind `os.cpus()` / `os.networkInterfaces()`. + #[cfg(windows)] + #[allow(non_snake_case, clippy::upper_case_acronyms)] + mod win32 { + pub(super) use bun_sys::windows::kernel32::{GetEnvironmentVariableW, GetTickCount64}; + use bun_sys::windows::ws2_32::{sockaddr_in, sockaddr_in6}; + use bun_sys::windows::{BOOL, DWORD, HANDLE, RtlGetVersion}; + pub(super) use bun_sys::windows::{ + HKEY_LOCAL_MACHINE, OSVERSIONINFOW, OpenProcessToken, RegGetValueW, + }; + use core::ffi::{c_char, c_int}; + + pub(super) const TOKEN_READ: DWORD = 0x0002_0008; + pub(super) const RRF_RT_REG_SZ: DWORD = 0x0000_0002; + + #[repr(C)] + pub(super) struct MEMORYSTATUSEX { + pub(super) dwLength: DWORD, + pub(super) dwMemoryLoad: DWORD, + pub(super) ullTotalPhys: u64, + pub(super) ullAvailPhys: u64, + pub(super) ullTotalPageFile: u64, + pub(super) ullAvailPageFile: u64, + pub(super) ullTotalVirtual: u64, + pub(super) ullAvailVirtual: u64, + pub(super) ullAvailExtendedVirtual: u64, + } + + #[link(name = "kernel32")] + unsafe extern "system" { + pub(super) fn GlobalMemoryStatusEx(buffer: *mut MEMORYSTATUSEX) -> BOOL; + } + #[link(name = "userenv")] + unsafe extern "system" { + pub(super) fn GetUserProfileDirectoryW( + token: HANDLE, + profile_dir: *mut u16, + size: *mut DWORD, + ) -> BOOL; + } + /// `BunCpuInfo` (`OsBinding.h`). Times are milliseconds. + #[repr(C)] + pub(super) struct CpuInfo { + pub(super) model: *mut c_char, + pub(super) speed: c_int, + pub(super) cpu_times: super::CPUTimes, + } + + /// `BunInterfaceAddress` (`OsBinding.h`). + #[repr(C)] + pub(super) struct InterfaceAddress { + pub(super) name: *mut c_char, + pub(super) phys_addr: [u8; 6], + pub(super) is_internal: c_int, + pub(super) address: InterfaceSockaddr, + pub(super) netmask: InterfaceNetmask, + } + #[repr(C)] + pub(super) union InterfaceSockaddr { + pub(super) address4: sockaddr_in, + pub(super) address6: sockaddr_in6, + } + #[repr(C)] + pub(super) union InterfaceNetmask { + pub(super) netmask4: sockaddr_in, + pub(super) netmask6: sockaddr_in6, + } + + unsafe extern "C" { + // `Bun__Os__cpuInfo` and `Bun__Os__interfaceAddresses` return 0 or a + // negative `UV_E*` number; on success the array is released with the + // matching free function. + pub(super) fn Bun__Os__cpuInfo( + cpu_infos: *mut *mut CpuInfo, + count: *mut c_int, + ) -> c_int; + pub(super) fn Bun__Os__freeCpuInfo(cpu_infos: *mut CpuInfo, count: c_int); + pub(super) fn Bun__Os__interfaceAddresses( + addresses: *mut *mut InterfaceAddress, + count: *mut c_int, + ) -> c_int; + pub(super) fn Bun__Os__freeInterfaceAddresses( + addresses: *mut InterfaceAddress, + count: c_int, + ); + } + + /// `process.report` prints an interface's addresses as + /// `os.networkInterfaces()` does. `address` is a `sockaddr_in` or a + /// `sockaddr_in6`, read as far as its family says. + #[unsafe(no_mangle)] + pub(super) unsafe extern "C" fn Bun__Os__formatAddress( + address: *const core::ffi::c_void, + buffer: *mut u8, + capacity: usize, + ) -> usize { + // SAFETY: caller contract. + let address = unsafe { bun_sys::net::Address::init_posix(address.cast()) }; + // SAFETY: `buffer` is valid for `capacity` bytes. + let into = unsafe { core::slice::from_raw_parts_mut(buffer, capacity) }; + // `format_ip` strips the brackets, so the text need not start at `into[0]`. + let start = into.as_ptr() as usize; + let Ok(text) = bun_core::fmt::format_ip(&address, into) else { + return 0; + }; + let (offset, len) = (text.as_ptr() as usize - start, text.len()); + into.copy_within(offset..offset + len, 0); + len + } + + /// `RtlGetVersion`, which unlike `GetVersionExW` is not subject to + /// the application manifest's compatibility shims. + pub(super) fn os_version() -> OSVERSIONINFOW { + let mut info = OSVERSIONINFOW { + dwOSVersionInfoSize: core::mem::size_of::() as DWORD, + dwMajorVersion: 0, + dwMinorVersion: 0, + dwBuildNumber: 0, + dwPlatformId: 0, + szCSDVersion: [0; 128], + }; + RtlGetVersion(&mut info); + info + } + } + // ─── local shims for upstream API gaps (Phase D) ────────────────────────── /// Unified error for `cpus_impl_*` so `?` works on both `JsResult` and @@ -583,35 +710,27 @@ mod _impl { #[cfg(windows)] fn cpus_impl_windows(global_this: &JSGlobalObject) -> Result { - let mut cpu_infos: *mut libuv::uv_cpu_info_t = core::ptr::null_mut(); + let mut cpu_infos: *mut win32::CpuInfo = core::ptr::null_mut(); let mut count: c_int = 0; // SAFETY: valid out-pointers - let err = unsafe { libuv::uv_cpu_info(&mut cpu_infos, &mut count) }; + let err = unsafe { win32::Bun__Os__cpuInfo(&mut cpu_infos, &mut count) }; if err != 0 { return Err(OsError::Any); } scopeguard::defer! { - // SAFETY: returned by uv_cpu_info - unsafe { libuv::uv_free_cpu_info(cpu_infos, count) }; + // SAFETY: returned by Bun__Os__cpuInfo + unsafe { win32::Bun__Os__freeCpuInfo(cpu_infos, count) }; }; let values = JSValue::create_empty_array(global_this, usize::try_from(count).expect("int cast"))?; - // SAFETY: cpu_infos points to `count` entries per uv_cpu_info contract + // SAFETY: cpu_infos points to `count` entries per the Bun__Os__cpuInfo contract let infos = unsafe { bun_core::ffi::slice(cpu_infos, usize::try_from(count).expect("int cast")) }; for (i, cpu_info) in infos.iter().enumerate() { - let times = CPUTimes { - user: cpu_info.cpu_times.user, - nice: cpu_info.cpu_times.nice, - sys: cpu_info.cpu_times.sys, - idle: cpu_info.cpu_times.idle, - irq: cpu_info.cpu_times.irq, - }; - let cpu = JSValue::create_empty_object(global_this, 3); - // SAFETY: cpu_info.model is a NUL-terminated C string from libuv + // SAFETY: cpu_info.model is a NUL-terminated C string let model = unsafe { bun_core::ffi::cstr(cpu_info.model) }.to_bytes(); cpu.put( global_this, @@ -623,7 +742,11 @@ mod _impl { b"speed", JSValue::js_number(cpu_info.speed as f64), ); - cpu.put(global_this, b"times", times.to_value(global_this)); + cpu.put( + global_this, + b"times", + cpu_info.cpu_times.to_value(global_this), + ); values.put_index(global_this, u32::try_from(i).expect("int cast"), cpu)?; } @@ -641,10 +764,7 @@ mod _impl { let err = SystemError { message: BunString::static_("no such process"), code: BunString::static_("ESRCH"), - #[cfg(not(windows))] - errno: -(bun_sys::posix::E::ESRCH as c_int), - #[cfg(windows)] - errno: libuv::UV_ESRCH, + errno: -bun_sys::UV_E::SRCH, syscall: BunString::static_("uv_os_getpriority"), ..Default::default() }; @@ -657,15 +777,7 @@ mod _impl { // In Node.js, this is a wrapper around uv_os_homedir. #[cfg(windows)] { - let mut out = bun_paths::path_buffer_pool::get(); - let mut size: usize = out.len(); - // SAFETY: valid buffer + size out-param - if let Some(err) = unsafe { libuv::uv_os_homedir(out.as_mut_ptr(), &mut size) } - .to_error(bun_sys::Tag::uv_os_homedir) - { - return Err(global.throw_value(err.to_js(global))); - } - return Ok(BunString::clone_utf8(&out[0..size])); + return homedir_windows().map_err(|err| global.throw_value(err.to_js(global))); } #[cfg(not(windows))] { @@ -756,12 +868,66 @@ mod _impl { } } + /// `%USERPROFILE%`, else the profile directory of the process token's user + /// (libuv's `uv_os_homedir`). + #[cfg(windows)] + fn homedir_windows() -> bun_sys::Result { + use bun_sys::{E, Error, Tag}; + let mut buf = bun_paths::w_path_buffer_pool::get(); + + windows::kernel32::SetLastError(0); + // SAFETY: the name is NUL-terminated; `buf` is writable for its length. + let len = unsafe { + win32::GetEnvironmentVariableW( + bun_core::w!("USERPROFILE\0").as_ptr(), + buf.as_mut_ptr(), + buf.len() as windows::DWORD, + ) + } as usize; + if len > 0 && len < buf.len() { + // A value too short to be a path is an error, not a miss. + if len < 3 { + return Err(Error::from_code(E::ENOENT, Tag::uv_os_homedir)); + } + return Ok(BunString::clone_utf16(&buf[..len])); + } + match windows::Win32Error::get() { + windows::Win32Error::ENVVAR_NOT_FOUND => {} + // Set, but empty. + windows::Win32Error::SUCCESS => { + return Err(Error::from_code(E::ENOENT, Tag::uv_os_homedir)); + } + err => return Err(Error::from_win32(err, Tag::uv_os_homedir)), + } + + let mut token: windows::HANDLE = core::ptr::null_mut(); + // SAFETY: `token` is a valid out-pointer. + if unsafe { + win32::OpenProcessToken(windows::GetCurrentProcess(), win32::TOKEN_READ, &mut token) + } == 0 + { + return Err(Error::from_win32( + windows::Win32Error::get(), + Tag::uv_os_homedir, + )); + } + let mut size = buf.len() as windows::DWORD; + // SAFETY: `token` is live; `buf` is writable for `size` units. + let ok = unsafe { win32::GetUserProfileDirectoryW(token, buf.as_mut_ptr(), &mut size) }; + let err = windows::Win32Error::get(); + // SAFETY: `token` is the live handle opened above. + unsafe { windows::CloseHandle(token) }; + if ok == 0 { + return Err(Error::from_win32(err, Tag::uv_os_homedir)); + } + Ok(BunString::clone_utf16(slice_to_nul_u16(&buf))) + } + pub(crate) fn hostname(global: &JSGlobalObject) -> JsResult { #[cfg(windows)] { let mut name_buffer: [u16; 130] = [0; 130]; // [129:0]u16 → 130 u16s with NUL at [129] - // SAFETY: idempotent Winsock init (libuv defers it to first use). - unsafe { windows::libuv::uv__winsock_ensure() }; + bun_uws_sys::iocp::us_internal_winsock_ensure(); // SAFETY: valid buffer if unsafe { windows::GetHostNameW(name_buffer.as_mut_ptr(), 129) } == 0 { return BunString::clone_utf16(slice_to_nul_u16(&name_buffer)).into_js(global); @@ -1141,10 +1307,10 @@ mod _impl { #[cfg(windows)] pub(crate) fn network_interfaces_windows(global_this: &JSGlobalObject) -> JsResult { - let mut ifaces: *mut libuv::uv_interface_address_t = core::ptr::null_mut(); + let mut ifaces: *mut win32::InterfaceAddress = core::ptr::null_mut(); let mut count: c_int = 0; // SAFETY: valid out-pointers - let err = unsafe { libuv::uv_interface_addresses(&mut ifaces, &mut count) }; + let err = unsafe { win32::Bun__Os__interfaceAddresses(&mut ifaces, &mut count) }; if err != 0 { let sys_err = SystemError { message: BunString::static_("uv_interface_addresses failed").into(), @@ -1157,8 +1323,8 @@ mod _impl { return Err(global_this.throw_value(sys_err.to_error_instance(global_this))); } scopeguard::defer! { - // SAFETY: returned by uv_interface_addresses - unsafe { libuv::uv_free_interface_addresses(ifaces, count) }; + // SAFETY: returned by Bun__Os__interfaceAddresses + unsafe { win32::Bun__Os__freeInterfaceAddresses(ifaces, count) }; }; let ret = JSValue::create_empty_object(global_this, 8); @@ -1166,7 +1332,7 @@ mod _impl { // 65 comes from: https://stackoverflow.com/questions/39443413/why-is-inet6-addrstrlen-defined-as-46-in-c let mut ip_buf = [0u8; 65]; - // SAFETY: ifaces points to `count` entries per uv_interface_addresses contract + // SAFETY: ifaces points to `count` entries per the Bun__Os__interfaceAddresses contract let iface_slice = unsafe { bun_core::ffi::slice(ifaces, usize::try_from(count).expect("int cast")) }; for iface in iface_slice { @@ -1299,7 +1465,7 @@ mod _impl { } // Does this entry already exist? - // SAFETY: iface.name is a NUL-terminated C string from libuv + // SAFETY: iface.name is a NUL-terminated C string let interface_name = unsafe { bun_core::ffi::cstr(iface.name) }.to_bytes(); if let Some(array) = ret.get(global_this, interface_name)? { // Add this interface entry to the existing array @@ -1336,17 +1502,17 @@ mod _impl { bun_core::slice_to_nul(&name_buffer) }; #[cfg(windows)] - let value: &[u8] = 'slice: { - // SAFETY: zeroed POD - let mut info: libuv::uv_utsname_s = unsafe { bun_core::ffi::zeroed_unchecked() }; - // SAFETY: valid out-pointer - let err = unsafe { libuv::uv_os_uname(&mut info) }; - if err != 0 { - break 'slice b"unknown"; - } - let value = bun_core::slice_to_nul(&info.release); - name_buffer[0..value.len()].copy_from_slice(value); - &name_buffer[0..value.len()] + let value: &[u8] = { + let info = win32::os_version(); + let mut cursor = &mut name_buffer[..]; + let _ = write!( + cursor, + "{}.{}.{}", + info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber + ); + let remaining = cursor.len(); + let len = name_buffer.len() - remaining; + &name_buffer[..len] }; BunString::clone_utf8(value) @@ -1365,9 +1531,9 @@ mod _impl { if code == 0 { return bun_sys::E::SUCCESS; } - // POSIX `setpriority` returns -1 and sets errno; Windows returns a libuv code. + // POSIX `setpriority` returns -1 and sets errno; Windows returns a `UV_E*` number. #[cfg(windows)] - return bun_sys::windows::translate_uv_error_to_e(code); + return bun_sys::windows::uv_to_e(code).unwrap_or(bun_sys::E::EUNKNOWN); #[cfg(not(windows))] return bun_sys::get_errno(code); } @@ -1416,28 +1582,30 @@ mod _impl { } #[cfg(windows)] { - // SAFETY: pure FFI getter - return unsafe { libuv::uv_get_total_memory() }; + let mut status = win32::MEMORYSTATUSEX { + dwLength: core::mem::size_of::() as windows::DWORD, + dwMemoryLoad: 0, + ullTotalPhys: 0, + ullAvailPhys: 0, + ullTotalPageFile: 0, + ullAvailPageFile: 0, + ullTotalVirtual: 0, + ullAvailVirtual: 0, + ullAvailExtendedVirtual: 0, + }; + // SAFETY: `status` is a valid out-pointer with `dwLength` set. + if unsafe { win32::GlobalMemoryStatusEx(&mut status) } == 0 { + return 0; + } + return status.ullTotalPhys; } } pub(crate) fn uptime(global: &JSGlobalObject) -> JsResult { #[cfg(windows)] { - let mut uptime_value: f64 = 0.0; - // SAFETY: valid out-pointer - let err = unsafe { libuv::uv_uptime(&mut uptime_value) }; - if err != 0 { - let sys_err = SystemError { - message: BunString::static_("failed to get system uptime").into(), - code: BunString::static_("ERR_SYSTEM_ERROR").into(), - errno: err, - syscall: BunString::static_("uv_uptime").into(), - ..Default::default() - }; - return Err(global.throw_value(sys_err.to_error_instance(global))); - } - return Ok(uptime_value); + let _ = global; + return Ok(win32::GetTickCount64() as f64 / 1000.0); } #[cfg(any(target_os = "macos", target_os = "freebsd"))] { @@ -1510,6 +1678,7 @@ mod _impl { Ok(result) } + #[cfg(not(windows))] pub(crate) fn version() -> JsResult { let mut name_buffer = [0u8; HOST_NAME_MAX]; @@ -1528,21 +1697,57 @@ mod _impl { name_buffer[..result.len()].copy_from_slice(result); &name_buffer[0..result.len()] }; - #[cfg(windows)] - let slice: &[u8] = 'slice: { - // SAFETY: zeroed POD - let mut info: libuv::uv_utsname_s = unsafe { bun_core::ffi::zeroed_unchecked() }; - // SAFETY: valid out-pointer - let err = unsafe { libuv::uv_os_uname(&mut info) }; - if err != 0 { - break 'slice b"unknown"; - } - let s = bun_core::slice_to_nul(&info.version); - name_buffer[0..s.len()].copy_from_slice(s); - &name_buffer[0..s.len()] + Ok(BunString::clone_utf8(slice)) + } + + /// The registry's `ProductName`, then the service pack if there is one + /// (libuv's `uv_os_uname`). + #[cfg(windows)] + pub(crate) fn version() -> JsResult { + /// Room for `ProductName`, in UTF-16 units. + const PRODUCT_NAME_MAX: usize = 256; + let info = win32::os_version(); + // The product name, a space, the service pack (`szCSDVersion`). + let mut version = [0u16; PRODUCT_NAME_MAX + 1 + 128]; + let mut len: usize = 0; + + let mut size = (PRODUCT_NAME_MAX * core::mem::size_of::()) as windows::DWORD; + // SAFETY: both names are NUL-terminated; `version` is writable for `size` bytes. + let rc = unsafe { + win32::RegGetValueW( + win32::HKEY_LOCAL_MACHINE, + bun_core::w!("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\0").as_ptr(), + bun_core::w!("ProductName\0").as_ptr(), + win32::RRF_RT_REG_SZ, + core::ptr::null_mut(), + version.as_mut_ptr().cast(), + &mut size, + ) }; + if rc == 0 { + len = slice_to_nul_u16(&version[..PRODUCT_NAME_MAX]).len(); + // Windows 11 kept `dwMajorVersion` 10 and the "Windows 10" product + // name; its builds start at 22000. + let windows_10 = bun_core::w!("Windows 10"); + if info.dwMajorVersion == 10 + && info.dwBuildNumber >= 22000 + && version[..len].starts_with(windows_10) + { + version[windows_10.len() - 1] = u16::from(b'1'); + } + } - Ok(BunString::clone_utf8(slice)) + let service_pack = slice_to_nul_u16(&info.szCSDVersion); + if !service_pack.is_empty() { + if len > 0 { + version[len] = u16::from(b' '); + len += 1; + } + version[len..len + service_pack.len()].copy_from_slice(service_pack); + len += service_pack.len(); + } + + Ok(BunString::clone_utf16(&version[..len])) } } // mod _impl pub(crate) use _impl::*; diff --git a/src/runtime/node/node_process.rs b/src/runtime/node/node_process.rs index 48fd367d7a14..5e262fb45ef3 100644 --- a/src/runtime/node/node_process.rs +++ b/src/runtime/node/node_process.rs @@ -99,6 +99,19 @@ extern "C" fn Bun__NODE_NO_WARNINGS() -> bool { env_var::NODE_NO_WARNINGS.get() == Some(b"1") } +/// `process.kill()` on Windows: 0, or the negative `UV_E*` number. +#[cfg(windows)] +#[unsafe(no_mangle)] +extern "C" fn Bun__Process__kill( + pid: core::ffi::c_int, + signum: core::ffi::c_int, +) -> core::ffi::c_int { + match bun_spawn_sys::windows::kill_pid(pid, signum) { + Ok(()) => 0, + Err(err) => err.uv_errno(), + } +} + /// `--redirect-warnings=` value; `Dead` when unset. #[unsafe(no_mangle)] pub(crate) extern "C" fn Bun__Node__getRedirectWarnings() -> bun_core::String { @@ -169,18 +182,13 @@ mod _impl { }; #[cfg(windows)] - unsafe extern "C" { - // SAFETY precondition: `name` must point to a NUL-terminated wide string; - // `value` must be either null (delete) or a NUL-terminated wide string. - // Raw-pointer contract — cannot be `safe fn`. - fn SetEnvironmentVariableW(name: *const u16, value: *const u16) -> i32; - } + use bun_sys::windows::SetEnvironmentVariableW; // ───────────────────────────── title ───────────────────────────── // Windows `process.title` getter support: the C++ getter needs to know // whether a title was explicitly set (CLI `--title` or assignment) so it - // can prefer the store over `uv_get_process_title` without comparing + // can prefer the store over the console title without comparing // against the "bun" default string. #[unsafe(export_name = "Bun__Process__hasTitle")] extern "C" fn has_title() -> bool { diff --git a/src/runtime/node/node_util_binding.rs b/src/runtime/node/node_util_binding.rs index 4c5f190d98d2..c3c7c82a175c 100644 --- a/src/runtime/node/node_util_binding.rs +++ b/src/runtime/node/node_util_binding.rs @@ -70,13 +70,9 @@ pub(crate) fn uv_translate_sys_error( } #[cfg(windows)] { - // SAFETY: pure translation function. - let uv_err = unsafe { bun_libuv_sys::uv_translate_sys_error(n) }; - return Ok(JSValue::js_number_from_int32(if uv_err != 0 { - uv_err - } else { - -UV_E::INVAL - })); + return Ok(JSValue::js_number_from_int32( + bun_errno::Bun__translateWin32ErrorToUV(n as u32), + )); } #[cfg(not(windows))] { diff --git a/src/runtime/node/path_watcher.rs b/src/runtime/node/path_watcher.rs index 41bfd4ac8a5d..4d5e47a9e9a4 100644 --- a/src/runtime/node/path_watcher.rs +++ b/src/runtime/node/path_watcher.rs @@ -1,21 +1,18 @@ -//! POSIX backend for `fs.watch()`. +//! Backend for `fs.watch()`. //! //! This is deliberately independent of `bun.Watcher` (the bundler/--watch/--hot //! watcher). `bun.Watcher` is shaped around a module graph — its WatchItem carries //! `options.Loader`, `*PackageJSON`, a filesystem handle, and on Windows is pinned -//! to `top_level_dir`. None of that applies to `fs.watch()`, and routing `fs.watch()` -//! through it required a 1k-line shim (the old version of this file) full of -//! lock-ordering workarounds, a WorkPool directory crawler, and a bolted-on FSEvents -//! side-channel. +//! to `top_level_dir`. None of that applies to `fs.watch()`. //! -//! The Windows backend (`win_watcher.rs`, libuv `uv_fs_event`) never went through -//! `bun.Watcher` and is a quarter of the size; this file gives Linux/macOS/FreeBSD -//! the same shape: +//! Every platform has the same shape: //! //! PathWatcherManager process-global, lazy, owns the OS resource //! ├─ Linux: one inotify fd + one reader thread, wd → PathWatcher map //! ├─ macOS: delegates to fs_events.rs (one CFRunLoop thread, one FSEventStream) -//! └─ FreeBSD: one kqueue fd + one reader thread, fd → PathWatcher map +//! ├─ FreeBSD: one kqueue fd + one reader thread, fd → PathWatcher map +//! └─ Windows: one completion port + one reader thread, one +//! `ReadDirectoryChangesW` request per PathWatcher //! //! PathWatcher one per unique (realpath, recursive) — deduped //! └─ handlers[] the JS FSWatcher contexts sharing this watch @@ -26,7 +23,7 @@ use core::cell::{Cell, UnsafeCell}; use core::ffi::c_void; -#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] +#[cfg(not(target_os = "macos"))] use core::sync::atomic::{AtomicBool, Ordering}; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -35,7 +32,7 @@ use bun_collections::{ArrayHashMap, StringArrayHashMap}; use bun_core::ZBox; #[cfg(any(target_os = "linux", target_os = "android"))] use bun_core::strings; -#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] +#[cfg(not(target_os = "macos"))] use bun_core::{Output, zstr}; use bun_core::{ZStr, handle_oom}; use bun_paths as path; @@ -43,14 +40,12 @@ use bun_paths as path; use bun_paths::platform; #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] use bun_paths::resolve_path::join_z_buf_spill; -#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] +#[cfg(not(target_os = "macos"))] use bun_sys::FdExt; use bun_sys::{self as sys, E, Fd, Tag}; use bun_threading::Mutex; use bun_wyhash::hash; -use bun_jsc::VirtualMachineRef as VirtualMachine; - use crate::node::node_fs_watcher::{Event, FSWatcher, WatchEventKind}; #[cfg(target_os = "macos")] @@ -59,7 +54,7 @@ use crate::node::fs_events as fsevents; bun_output::define_scoped_log!(log, fs_watch, hidden); /// Process-global manager. Created on first `fs.watch()`, never destroyed (matches -/// the FSEvents loop and Windows libuv loop lifetimes). +/// the FSEvents loop lifetime). // PORTING.md §Global mutable state: init-once-then-read-only → `OnceLock`. // `DEFAULT_MANAGER_MUTEX` still serializes the *fallible* init path so a failed // `Platform::init` can be retried on a later `get()` without two threads @@ -76,8 +71,7 @@ static DEFAULT_MANAGER_MUTEX: Mutex = Mutex::new(); pub(crate) struct PathWatcherManager { /// Guards `watchers` and all per-platform dispatch maps. The reader thread holds /// this while dispatching, so `detach()` on the JS thread cannot free a PathWatcher - /// mid-emit. A single lock here replaces the three interacting mutexes of the old - /// design. + /// mid-emit. mutex: Mutex, /// Dedup map: dedup key → PathWatcher. The key is the resolved path with a one-byte @@ -95,15 +89,16 @@ pub(crate) struct PathWatcherManager { #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] platform: UnsafeCell, - /// inotify/kqueue fd. Set once in `Platform::init` *before* the reader thread + /// inotify/kqueue fd, or the completion port on Windows. Set once in + /// `Platform::init` *before* the reader thread /// spawns, never reassigned (process-lifetime singleton, no teardown). Hoisted /// out of `UnsafeCell` so reads are safe `Cell::get()` instead of /// raw deref; thread-spawn happens-before makes the cross-thread read sound. - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(not(target_os = "macos"))] platform_fd: Cell, /// Reader-thread loop flag. Initialized `true`, never cleared (no teardown). - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(not(target_os = "macos"))] running: AtomicBool, /// Monotonic kevent generation counter (FreeBSD). Bumped under `mutex`. @@ -131,9 +126,9 @@ impl Default for PathWatcherManager { watchers: UnsafeCell::new(StringArrayHashMap::default()), #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] platform: UnsafeCell::new(Platform::default()), - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(not(target_os = "macos"))] platform_fd: Cell::new(Fd::INVALID), - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(not(target_os = "macos"))] running: AtomicBool::new(true), #[cfg(target_os = "freebsd")] next_gen: Cell::new(1), @@ -191,17 +186,18 @@ pub(crate) struct PathWatcher { /// Canonical absolute path (realpath of the user-supplied path). Owned. path: ZBox, recursive: bool, - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(not(target_os = "macos"))] is_file: bool, /// JS `FSWatcher` contexts sharing this OS watch. Each gets its own ChangeEvent - /// for per-handler duplicate suppression (same as `win_watcher.rs`). Guarded by - /// `manager.mutex` on all platforms — every emit path (inotify/kqueue reader + /// for per-handler duplicate suppression. Guarded by + /// `manager.mutex` on all platforms — every emit path (the reader /// threads and the Darwin FSEvents callback) holds it while iterating, so /// attach/detach can never race with dispatch. handlers: ArrayHashMap<*mut c_void, ChangeEvent>, - /// Per-platform per-watch state (inotify wds, kqueue fds, or the FSEventsWatcher). + /// Per-platform per-watch state (inotify wds, kqueue fds, the + /// FSEventsWatcher, or the directory handle's change request). platform: PlatformWatch, } @@ -210,8 +206,7 @@ pub(crate) struct PathWatcher { /// Suppresses only exact duplicates: same path hash *and* same event type /// within a 1ms window. Distinct files changed in the same millisecond must /// each emit — node delivers both (see test/js/node/test/parallel -/// fs-watch tests that write two files back-to-back). Kept identical to -/// `win_watcher.rs` so POSIX and Windows agree on which bursts are coalesced. +/// fs-watch tests that write two files back-to-back). /// /// Fields are `Cell` so `should_emit` takes `&self` — the emit paths then only /// ever need shared access to a `PathWatcher`. @@ -239,9 +234,6 @@ impl ChangeEvent { } } -pub(crate) type Callback = fn(ctx: Option<*mut c_void>, event: Event, is_file: bool); -pub(crate) type UpdateEndCallback = fn(ctx: Option<*mut c_void>); - impl PathWatcher { /// Heap-allocate and return a raw pointer. fn new(init: PathWatcher) -> *mut PathWatcher { @@ -249,19 +241,17 @@ impl PathWatcher { } /// Called from the platform reader thread with `manager.mutex` held. - /// `rel_path` is borrowed — `onPathUpdatePosix` dupes it before enqueuing. + /// `rel_path` is borrowed — `to_event` dupes it before enqueuing. /// `&self`: per-handler state is `Cell`-based, so the emit paths never /// need an exclusive `PathWatcher` borrow. fn emit(&self, event_type: WatchEventKind, rel_path: &[u8], is_file: bool) { - let timestamp = bun_core::time::milli_timestamp(); + // Monotonic: a wall clock set back would hold back every repeat of the + // last event for as long as the step. + let timestamp = bun_core::Timespec::now(bun_core::TimespecMockMode::ForceRealTime).ms(); let h = hash(rel_path); for (&ctx, ev) in self.handlers.iter() { if ev.should_emit(h, timestamp, event_type) { - (FSWatcher::ON_PATH_UPDATE)( - Some(ctx), - event_type.to_event(rel_path.into()), - is_file, - ); + FSWatcher::on_path_update(Some(ctx), event_type.to_event(rel_path.into()), is_file); } } } @@ -274,27 +264,24 @@ impl PathWatcher { #[cfg(any(target_os = "linux", target_os = "android"))] fn emit_unsuppressed(&self, event_type: WatchEventKind, rel_path: &[u8], is_file: bool) { for &ctx in self.handlers.keys() { - (FSWatcher::ON_PATH_UPDATE)(Some(ctx), event_type.to_event(rel_path.into()), is_file); + FSWatcher::on_path_update(Some(ctx), event_type.to_event(rel_path.into()), is_file); } } - /// The shared inotify queue overflowed and events were lost; every handler - /// gets `('change', null)`. No duplicate suppression — a loss signal must - /// always be delivered. Caller holds `manager.mutex`. - #[cfg(any(target_os = "linux", target_os = "android"))] + /// The OS event queue (the shared inotify queue, this watch's + /// `ReadDirectoryChangesW` buffer) overflowed and events were lost; every + /// handler gets `('change', null)`. No duplicate suppression — a loss signal + /// must always be delivered. Caller holds `manager.mutex`. + #[cfg(any(target_os = "linux", target_os = "android", windows))] fn emit_overflow(&self) { for &ctx in self.handlers.keys() { - (FSWatcher::ON_PATH_UPDATE)( - Some(ctx), - Event::NoFilename(WatchEventKind::Change), - false, - ); + FSWatcher::on_path_update(Some(ctx), Event::NoFilename(WatchEventKind::Change), false); } } fn emit_error(&self, err: &sys::Error, close: bool) { for &ctx in self.handlers.keys() { - (FSWatcher::ON_PATH_UPDATE)( + FSWatcher::on_path_update( Some(ctx), Event::Error { err: err.clone(), @@ -406,57 +393,14 @@ impl PathWatcher { // ──────────────────────────────────────────────────────────────────────────────── pub(crate) fn watch( - vm: &VirtualMachine, path: &ZStr, recursive: bool, - callback: Callback, - update_end: UpdateEndCallback, ctx: *mut c_void, ) -> sys::Result<*mut PathWatcher> { - // Assert the callback/updateEnd are what node_fs_watcher passes. - // Compare against the *exact* fn pointers `FSWatcher` passes (not local wrappers, - // which would be distinct fn items with distinct addresses). - debug_assert!(callback as usize == FSWatcher::ON_PATH_UPDATE as usize); - debug_assert!(update_end as usize == (FSWatcher::on_update_end as UpdateEndCallback) as usize); - let _ = vm; - let manager = PathWatcherManager::get()?; - // Resolve to a canonical path so `fs.watch("./x")` and `fs.watch("/abs/x")` dedup; - // FSEvents reports events by realpath so macOS needs this for prefix matching too. - // - // Open with O_PATH|O_DIRECTORY first and retry without O_DIRECTORY on ENOTDIR — - // that tells us file-vs-dir without a separate stat, follows symlinks, and the - // resulting fd feeds `getFdPath` for the realpath. One or two syscalls instead - // of lstat + open + (stat) in the old code. `O.PATH` is 0 on macOS (degrades to - // O_RDONLY, which is what F_GETPATH needs anyway). let mut resolve_buf = path::path_buffer_pool::get(); - let mut is_file = false; - let probe_fd: Fd = match sys::open(path, sys::O::PATH | sys::O::DIRECTORY | sys::O::CLOEXEC, 0) - { - Ok(f) => f, - Err(e) => { - if e.get_errno() == E::ENOTDIR { - is_file = true; - match sys::open(path, sys::O::PATH | sys::O::CLOEXEC, 0) { - Ok(f) => f, - Err(e2) => return Err(e2.without_path()), - } - } else { - return Err(e.without_path()); - } - } - }; - let _close_probe = sys::CloseOnDrop::new(probe_fd); - let resolved: &ZStr = match sys::get_fd_path(probe_fd, &mut resolve_buf) { - Err(_) => path, // fall back to the caller's path; best effort - Ok(r) => { - let len = r.len(); - resolve_buf[len] = 0; - // SAFETY: resolve_buf[len] == 0 written above; buf lives for the rest of this fn. - ZStr::from_buf(&resolve_buf[..], len) - } - }; + let (resolved, is_file) = resolve(path, &mut resolve_buf)?; let mut key_buf = path::path_buffer_pool::get(); let key = PathWatcherManager::make_key(key_buf.as_mut_slice(), resolved.as_bytes(), recursive); @@ -467,19 +411,28 @@ pub(crate) fn watch( // scoped to this lookup. if let Some(&existing) = unsafe { (*manager.watchers.get()).get(key) } { // SAFETY: existing is a live PathWatcher under manager.mutex. - unsafe { handle_oom((*existing).handlers.put(ctx, ChangeEvent::default())) }; - manager.mutex.unlock(); - return Ok(existing); + #[cfg(windows)] + let reusable = + unsafe { (*existing).is_file == is_file && Platform::watches_its_path(&*existing) }; + #[cfg(not(windows))] + let reusable = true; + if reusable { + // SAFETY: existing is a live PathWatcher under manager.mutex. + unsafe { handle_oom((*existing).handlers.put(ctx, ChangeEvent::default())) }; + manager.mutex.unlock(); + return Ok(existing); + } + manager.unlink_watcher_locked(existing); } - #[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd")))] + #[cfg(target_os = "macos")] let _ = is_file; // New watcher: own the key and path. let watcher = PathWatcher::new(PathWatcher { manager: Some(manager), path: ZBox::from_bytes(resolved.as_bytes()), recursive, - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(not(target_os = "macos"))] is_file, handlers: ArrayHashMap::default(), platform: PlatformWatch::default(), @@ -561,6 +514,42 @@ pub(crate) fn watch( } } +/// Resolve to a canonical path so `fs.watch("./x")` and `fs.watch("/abs/x")` dedup; +/// FSEvents reports events by realpath so macOS needs this for prefix matching too. +/// Also reports whether the path is a file. +/// +/// Open with O_PATH|O_DIRECTORY first and retry without O_DIRECTORY on ENOTDIR — +/// that tells us file-vs-dir without a separate stat, follows symlinks, and the +/// resulting fd feeds `getFdPath` for the realpath. One or two syscalls instead +/// of lstat + open + (stat). `O.PATH` is 0 on macOS (degrades to +/// O_RDONLY, which is what F_GETPATH needs anyway). +#[cfg(not(windows))] +fn resolve<'a>( + path: &'a ZStr, + resolve_buf: &'a mut path::PathBuffer, +) -> sys::Result<(&'a ZStr, bool)> { + let mut is_file = false; + let probe_fd: Fd = match sys::open(path, sys::O::PATH | sys::O::DIRECTORY | sys::O::CLOEXEC, 0) + { + Ok(f) => f, + Err(e) => { + if e.get_errno() == E::ENOTDIR { + is_file = true; + match sys::open(path, sys::O::PATH | sys::O::CLOEXEC, 0) { + Ok(f) => f, + Err(e2) => return Err(e2.without_path()), + } + } else { + return Err(e.without_path()); + } + } + }; + let _close_probe = sys::CloseOnDrop::new(probe_fd); + // Best effort: the caller's path when the OS cannot name the descriptor. + let resolved = sys::get_fd_path_z(probe_fd, resolve_buf).unwrap_or(path); + Ok((resolved, is_file)) +} + // ──────────────────────────────────────────────────────────────────────────────── // Platform backends // ──────────────────────────────────────────────────────────────────────────────── @@ -643,6 +632,11 @@ type Platform = Kqueue; #[cfg(target_os = "freebsd")] type PlatformWatch = KqueueWatch; +#[cfg(windows)] +type Platform = Windows; +#[cfg(windows)] +type PlatformWatch = WindowsWatch; + #[cfg(target_arch = "wasm32")] compile_error!("path_watcher: unsupported target"); @@ -964,6 +958,11 @@ impl Linux { // SAFETY: o.watcher live under manager.mutex; shared // access only — `emit_unsuppressed` takes `&self`. let w = unsafe { &*o.watcher }; + if o.subpath.as_bytes().is_empty() { + // The path names nothing this watcher watches any more: + // the next `watch()` of it has to start a new one. + manager.unlink_watcher_locked(o.watcher); + } if o.subpath.as_bytes().is_empty() && (w.is_file || !w.recursive) { w.emit_unsuppressed( WatchEventKind::Rename, @@ -1193,8 +1192,7 @@ use bun_watcher::inotify_watcher::Event as InotifyEvent; /// FSEventsWatcher's opaque ctx — `fs_events.rs` calls back via `onFSEvent` below, /// and we fan out to the JS handlers. /// -/// Unlike the old design, FSEvents is used for both files and directories (same as -/// libuv), so `fs.watch()` no longer spins up a second kqueue thread. +/// FSEvents is used for both files and directories (same as libuv). #[cfg(target_os = "macos")] #[derive(Default)] pub(crate) struct Darwin { @@ -1602,6 +1600,11 @@ impl Kqueue { watcher.emit(event_type, rel, entry.is_file); let _ = handle_oom(touched.get_or_put(entry.watcher)); + if entry.subpath.is_empty() && kev.fflags & (NOTE::DELETE | NOTE::REVOKE) != 0 { + // The path names nothing this watcher watches any more: + // the next `watch()` of it has to start a new one. + manager.unlink_watcher_locked(entry.watcher); + } } for &w in touched.keys() { @@ -1614,5 +1617,845 @@ impl Kqueue { } // ──────────────────────────────────────────────────────────────────────────────── -// Windows stub +// Windows // ──────────────────────────────────────────────────────────────────────────────── + +/// Windows: one completion port, one blocking reader thread, and per PathWatcher a +/// directory handle with one outstanding `ReadDirectoryChangesW` request. +/// `recursive` is the request's `bWatchSubtree`. A file is watched through its +/// parent directory, keeping only the records that name it. +#[cfg(windows)] +#[derive(Default)] +pub(crate) struct Windows {} + +#[cfg(windows)] +pub(crate) struct WindowsWatch { + /// Null until `add_watch` succeeds and after `remove_watch`. + request: *mut windows_impl::DirRequest, +} + +#[cfg(windows)] +impl Default for WindowsWatch { + fn default() -> Self { + Self { + request: core::ptr::null_mut(), + } + } +} + +#[cfg(windows)] +fn resolve<'a>( + path: &'a ZStr, + resolve_buf: &'a mut path::PathBuffer, +) -> sys::Result<(&'a ZStr, bool)> { + use bun_sys::windows as w; + let mut wbuf = path::w_path_buffer_pool::get(); + let wpath = path::string_paths::to_kernel32_path(&mut wbuf[..], path.as_bytes()); + // SAFETY: `wpath` is NUL-terminated. + let attributes = unsafe { sys::c::GetFileAttributesW(wpath.as_ptr()) }; + if attributes == w::INVALID_FILE_ATTRIBUTES { + return Err(sys::Error::from_win32(w::Win32Error::get(), Tag::watch)); + } + let is_file = attributes & w::FILE_ATTRIBUTE_DIRECTORY == 0; + // No access rights: only the name is wanted, and links are followed. + // SAFETY: `wpath` is NUL-terminated; the other pointers are null. + let handle = unsafe { + w::CreateFileW( + wpath.as_ptr(), + 0, + w::FILE_SHARE_READ | w::FILE_SHARE_WRITE | w::FILE_SHARE_DELETE, + core::ptr::null_mut(), + w::OPEN_EXISTING, + w::FILE_FLAG_BACKUP_SEMANTICS, + core::ptr::null_mut(), + ) + }; + if handle == w::INVALID_HANDLE_VALUE { + return Err(sys::Error::from_win32(w::Win32Error::get(), Tag::watch)); + } + let _close = sys::CloseOnDrop::new(Fd::from_system(handle)); + // Best effort: the caller's path when the OS cannot name the handle. + let resolved = sys::get_fd_path_z(Fd::from_system(handle), resolve_buf).unwrap_or(path); + Ok((resolved, is_file)) +} + +#[cfg(windows)] +mod windows_impl { + use super::*; + use bun_sys::windows as w; + use bun_sys::windows::kernel32::{CompareStringOrdinal, GetShortPathNameW}; + use bun_sys::windows::{BOOL, CSTR_EQUAL, DWORD, HANDLE, OVERLAPPED}; + + /// libuv's `uv_directory_watcher_buffer_size`, so a burst of changes + /// overflows into `('change', null)` where it does in Node. + const BUFFER_SIZE: usize = 4096; + + #[repr(C)] + #[allow(non_snake_case)] + struct FILE_STANDARD_INFO { + AllocationSize: i64, + EndOfFile: i64, + NumberOfLinks: DWORD, + DeletePending: u8, + Directory: u8, + } + /// `FILE_INFO_BY_HANDLE_CLASS::FileStandardInfo` + const FILE_STANDARD_INFO_CLASS: u32 = 1; + + #[repr(C)] + #[allow(non_snake_case)] + #[derive(PartialEq, Eq)] + struct FILE_ID_INFO { + VolumeSerialNumber: u64, + FileId: [u8; 16], + } + /// `FILE_INFO_BY_HANDLE_CLASS::FileIdInfo` + const FILE_ID_INFO_CLASS: u32 = 18; + + /// `None` where the file system has no ids to give. + fn file_id(file: HANDLE) -> Option { + let mut info = FILE_ID_INFO { + VolumeSerialNumber: 0, + FileId: [0; 16], + }; + // SAFETY: `file` is the caller's live handle; `info` is a valid out-buffer of the given size. + let ok = unsafe { + GetFileInformationByHandleEx( + file, + FILE_ID_INFO_CLASS, + core::ptr::from_mut(&mut info).cast(), + core::mem::size_of::() as DWORD, + ) + } != 0; + ok.then_some(info) + } + + /// `dir_path` as `CreateFileW` takes it. A drive root keeps its separator + /// (`C:` alone is the drive's current directory). + fn dir_path_z(dir_path: &[u16]) -> Vec { + let mut dir_z: Vec = Vec::with_capacity(dir_path.len() + 2); + dir_z.extend_from_slice(dir_path); + if dir_path.last() == Some(&u16::from(b':')) { + dir_z.push(u16::from(b'\\')); + } + dir_z.push(0); + dir_z + } + + #[link(name = "kernel32")] + unsafe extern "system" { + fn GetFileInformationByHandleEx( + file: HANDLE, + class: u32, + info: *mut c_void, + size: DWORD, + ) -> BOOL; + } + + /// One directory handle and its `ReadDirectoryChangesW` request. + /// + /// Its own allocation rather than part of the `PathWatcher`: the kernel + /// writes `overlapped` and `buffer` until the reader thread dequeues the + /// request's completion, which can be after the `PathWatcher` is gone. The + /// reader thread frees it then. + #[repr(C)] + pub(super) struct DirRequest { + /// First field: the completion's `OVERLAPPED*` is the `DirRequest*`. + overlapped: OVERLAPPED, + /// At offset 32, so `FILE_NOTIFY_INFORMATION` (DWORD-aligned) records + /// can be read in place. Only what a completion reports written is + /// initialized. + buffer: [core::mem::MaybeUninit; BUFFER_SIZE], + dir: HANDLE, + /// Null once the owner detached. Guarded by `manager.mutex`. + watcher: *mut PathWatcher, + /// A request was issued whose completion the reader thread has not + /// handled yet. Guarded by `manager.mutex`. + pending: bool, + recursive: bool, + /// The watched directory, without a trailing separator or NUL. + /// Long-form: it comes from the watcher's resolved path. + dir_path: Box<[u16]>, + /// File watch only: the names the file's records can carry. + file_name: Option, + } + + struct FileName { + long: Box<[u16]>, + /// The 8.3 alias; a change made through it is reported under it. + short: Option>, + } + + const _: () = assert!( + core::mem::offset_of!(DirRequest, buffer) + % core::mem::align_of::() + == 0 + ); + + #[inline] + fn is_separator(c: u16) -> bool { + c == u16::from(b'\\') || c == u16::from(b'/') + } + + fn last_separator(path: &[u16]) -> Option { + let mut i = path.len(); + while i > 0 { + i -= 1; + if is_separator(path[i]) { + return Some(i); + } + } + None + } + + fn eql_ignore_case(a: &[u16], b: &[u16]) -> bool { + // SAFETY: both slices are valid for their lengths. + a.len() == b.len() + && unsafe { + CompareStringOrdinal(a.as_ptr(), a.len() as i32, b.as_ptr(), b.len() as i32, 1) + } == CSTR_EQUAL + } + + impl PathWatcherManager { + #[inline] + fn port(&self) -> HANDLE { + self.platform_fd.get().native() + } + } + + impl Windows { + pub(super) fn init() -> sys::Result<&'static PathWatcherManager> { + let port = w::kernel32::CreateIoCompletionPort( + w::INVALID_HANDLE_VALUE, + core::ptr::null_mut(), + 0, + 1, + ); + if port.is_null() { + return Err(sys::Error::from_win32(w::Win32Error::get(), Tag::watch)); + } + let manager_ptr = bun_core::heap::into_raw(Box::new(PathWatcherManager::default())); + // SAFETY: just allocated and exclusively owned; published only on Ok. + let manager: &'static PathWatcherManager = unsafe { &*manager_ptr }; + manager.platform_fd.set(Fd::from_system(port)); + // The manager is process-global and never torn down, so the reader thread is + // a daemon — detach it instead of stashing a handle we'd never join. + match std::thread::Builder::new().spawn(move || Windows::thread_main(manager)) { + Ok(handle) => drop(handle), + Err(_) => { + manager.platform_fd.get().close(); + // SAFETY: the thread never started and the manager was never published. + drop(unsafe { bun_core::heap::take(manager_ptr) }); + return Err(sys::Error::from_code(E::ENOMEM, Tag::watch)); + } + } + Ok(manager) + } + + /// Whether the directory `watcher` reads changes from is still the one + /// its path names: not once that directory was deleted or moved away, + /// whether or not the reader thread has heard of it yet. Caller holds + /// `manager.mutex`. + pub(super) fn watches_its_path(watcher: &PathWatcher) -> bool { + let request = watcher.platform.request; + // SAFETY: a watcher's request is live under `manager.mutex`. + let (dir, dir_z) = unsafe { + if request.is_null() || (*request).dir == w::INVALID_HANDLE_VALUE { + return false; + } + ((*request).dir, dir_path_z(&(*request).dir_path)) + }; + // No access rights: only the id is wanted. + // SAFETY: `dir_z` is NUL-terminated; the other pointers are null. + let named = unsafe { + w::CreateFileW( + dir_z.as_ptr(), + 0, + w::FILE_SHARE_READ | w::FILE_SHARE_WRITE | w::FILE_SHARE_DELETE, + core::ptr::null_mut(), + w::OPEN_EXISTING, + w::FILE_FLAG_BACKUP_SEMANTICS, + core::ptr::null_mut(), + ) + }; + if named == w::INVALID_HANDLE_VALUE { + return false; + } + let same = matches!((file_id(dir), file_id(named)), (Some(a), Some(b)) if a == b); + // SAFETY: `named` is the live handle opened above. + unsafe { w::CloseHandle(named) }; + same + } + + /// Caller holds `manager.mutex`. + pub(super) fn add_watch( + manager: &'static PathWatcherManager, + watcher: &mut PathWatcher, + ) -> sys::Result<()> { + let win32_err = |e: w::Win32Error| sys::Error::from_win32(e, Tag::watch); + + let mut wbuf = path::w_path_buffer_pool::get(); + // NUL-terminated in `wbuf`. + let wpath: &[u16] = + path::string_paths::to_kernel32_path(&mut wbuf[..], watcher.path.as_bytes()); + let (dir_path, file_name): (&[u16], Option) = if watcher.is_file { + let sep = last_separator(wpath) + .ok_or_else(|| sys::Error::from_code(E::EINVAL, Tag::watch))?; + let mut short_buf = path::w_path_buffer_pool::get(); + // SAFETY: `wpath` is NUL-terminated; `short_buf` is writable for its length. + let short_len = unsafe { + GetShortPathNameW( + wpath.as_ptr(), + short_buf.as_mut_ptr(), + short_buf.len() as DWORD, + ) + } as usize; + let short = if short_len > 0 && short_len < short_buf.len() { + let short_path = &short_buf[..short_len]; + last_separator(short_path).map(|i| Box::<[u16]>::from(&short_path[i + 1..])) + } else { + None + }; + ( + &wpath[..sep], + Some(FileName { + long: Box::from(&wpath[sep + 1..]), + short, + }), + ) + } else { + (wpath, None) + }; + let dir_path = match dir_path.split_last() { + Some((&last, rest)) if is_separator(last) => rest, + _ => dir_path, + }; + + let dir_z = dir_path_z(dir_path); + + // SAFETY: `dir_z` is NUL-terminated; the other pointers are null. + let dir = unsafe { + w::CreateFileW( + dir_z.as_ptr(), + w::FILE_LIST_DIRECTORY, + w::FILE_SHARE_READ | w::FILE_SHARE_WRITE | w::FILE_SHARE_DELETE, + core::ptr::null_mut(), + w::OPEN_EXISTING, + w::FILE_FLAG_BACKUP_SEMANTICS | w::FILE_FLAG_OVERLAPPED, + core::ptr::null_mut(), + ) + }; + if dir == w::INVALID_HANDLE_VALUE { + return Err(win32_err(w::Win32Error::get())); + } + if w::kernel32::CreateIoCompletionPort(dir, manager.port(), 0, 0).is_null() { + let err = w::Win32Error::get(); + // SAFETY: `dir` is the live handle opened above. + unsafe { w::CloseHandle(dir) }; + return Err(win32_err(err)); + } + + let request: *mut DirRequest = { + let mut uninit = Box::::new_uninit(); + let p = uninit.as_mut_ptr(); + // SAFETY: every field but `buffer`, which is `MaybeUninit`, is + // written in place before `assume_init`. + unsafe { + core::ptr::addr_of_mut!((*p).overlapped).write(bun_core::ffi::zeroed()); + core::ptr::addr_of_mut!((*p).dir).write(dir); + core::ptr::addr_of_mut!((*p).watcher).write(watcher); + core::ptr::addr_of_mut!((*p).pending).write(false); + core::ptr::addr_of_mut!((*p).recursive).write(watcher.recursive); + core::ptr::addr_of_mut!((*p).dir_path).write(Box::from(dir_path)); + core::ptr::addr_of_mut!((*p).file_name).write(file_name); + bun_core::heap::into_raw(uninit.assume_init()) + } + }; + // SAFETY: `request` is live and not yet shared with the reader thread. + if let Err(err) = unsafe { DirRequest::issue(request) } { + // SAFETY: no request is outstanding, so nothing else refers to it. + unsafe { DirRequest::destroy(request) }; + return Err(win32_err(err)); + } + watcher.platform.request = request; + Ok(()) + } + + /// Caller holds `manager.mutex`. + pub(super) fn remove_watch(_: &'static PathWatcherManager, watcher: &mut PathWatcher) { + let request = core::mem::replace(&mut watcher.platform.request, core::ptr::null_mut()); + if request.is_null() { + return; + } + // SAFETY: `request` is live: only this function and the reader thread + // (for a request whose `watcher` is already null) free one, both + // under `manager.mutex`. + unsafe { + (*request).watcher = core::ptr::null_mut(); + if (*request).pending { + // Closing the handle completes the request; the reader + // thread frees it when it dequeues that completion. + w::CloseHandle((*request).dir); + (*request).dir = w::INVALID_HANDLE_VALUE; + } else { + DirRequest::destroy(request); + } + } + } + + fn thread_main(manager: &'static PathWatcherManager) { + Output::Source::configure_named_thread(zstr!("fs.watch")); + let port = manager.port(); + let mut name_buf = path::path_buffer_pool::get(); + let mut long_buf = path::w_path_buffer_pool::get(); + + while manager.running.load(Ordering::Acquire) { + let mut bytes: DWORD = 0; + let mut key: w::ULONG_PTR = 0; + let mut overlapped: *mut OVERLAPPED = core::ptr::null_mut(); + // SAFETY: `port` is the live completion port; the out-pointers are locals. + let ok = unsafe { + w::kernel32::GetQueuedCompletionStatus( + port, + &mut bytes, + &mut key, + &mut overlapped, + w::INFINITE, + ) + } != 0; + let error = if ok { None } else { Some(w::Win32Error::get()) }; + + if overlapped.is_null() { + let Some(error) = error else { continue }; + // The completion port itself failed: surface to every watcher, then exit the thread. + let err = sys::Error::from_win32(error, Tag::watch); + manager.mutex.lock(); + // SAFETY: holding manager.mutex. + let watchers = unsafe { &*manager.watchers.get() }; + for &watcher in watchers.values() { + // SAFETY: holding manager.mutex; `watcher` is live. + unsafe { + (*watcher).emit_error(&err, true); + (*watcher).flush(); + } + } + manager.mutex.unlock(); + return; + } + + let request = overlapped.cast::(); + manager.mutex.lock(); + // SAFETY: a dequeued completion's request is live until this + // thread frees it; its fields are guarded by `manager.mutex`. + unsafe { + let watcher = (*request).watcher; + if watcher.is_null() { + DirRequest::destroy(request); + manager.mutex.unlock(); + continue; + } + (*request).pending = false; + let watcher = &*watcher; + DirRequest::on_completion( + request, + watcher, + bytes as usize, + error, + &mut name_buf, + &mut long_buf[..], + ); + watcher.flush(); + } + manager.mutex.unlock(); + } + } + } + + /// Whether `component` has the shape of the 8.3 alias a file system makes + /// for a longer name: at most eight characters with a `~` among them, then + /// at most one dot with at most three after it. Without the `~` every + /// `index.js` and `src` would be looked up. An alias of another shape is one + /// that `SetFileShortNameW` gave the file, and is reported as it is. + fn is_short_shaped(component: &[u16]) -> bool { + let mut parts = component.split(|&unit| unit == u16::from(b'.')); + let base = parts.next().unwrap_or(&[]); + let extension = parts.next(); + parts.next().is_none() + && (1..=8).contains(&base.len()) + && base.contains(&u16::from(b'~')) + && extension.is_none_or(|extension| extension.len() <= 3) + } + + fn may_hold_short_name(name: &[u16]) -> bool { + name.split(|&unit| is_separator(unit)).any(is_short_shaped) + } + + /// `FileNamesInformation`: an entry's name and nothing else. + const FILE_NAMES_INFORMATION_CLASS: w::FILE_INFORMATION_CLASS = w::FILE_INFORMATION_CLASS(12); + const OBJ_CASE_INSENSITIVE: u32 = 0x40; + + #[repr(C)] + #[allow(non_snake_case)] + struct FILE_NAMES_INFORMATION { + NextEntryOffset: u32, + FileIndex: u32, + FileNameLength: u32, + FileName: [u16; 1], + } + + /// Room for one `FILE_NAMES_INFORMATION` with a 255-character name. + type NameEntry = [u64; 80]; + + fn unicode_string(name: &[u16]) -> Option { + let bytes = u16::try_from(name.len() * 2).ok()?; + Some(w::UNICODE_STRING { + Length: bytes, + MaximumLength: bytes, + Buffer: name.as_ptr().cast_mut(), + }) + } + + /// A synchronous handle to the directory `name` of `parent`, for + /// [`query_long_name`]. An empty `name` is `parent` itself, which need not be + /// synchronous. + fn open_for_queries(parent: HANDLE, name: &[u16]) -> Option { + let mut object_name = unicode_string(name)?; + let mut attributes = w::OBJECT_ATTRIBUTES { + Length: core::mem::size_of::() as u32, + RootDirectory: parent, + ObjectName: &mut object_name, + Attributes: OBJ_CASE_INSENSITIVE, + SecurityDescriptor: core::ptr::null_mut(), + SecurityQualityOfService: core::ptr::null_mut(), + }; + let mut handle: HANDLE = w::INVALID_HANDLE_VALUE; + let mut io: w::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + // SAFETY: every pointer is to a live local; `parent` is an open directory. + let status = unsafe { + w::ntdll::NtCreateFile( + &mut handle, + w::FILE_LIST_DIRECTORY | w::SYNCHRONIZE, + &mut attributes, + &mut io, + core::ptr::null_mut(), + 0, + w::FILE_SHARE_READ | w::FILE_SHARE_WRITE | w::FILE_SHARE_DELETE, + w::FILE_OPEN, + w::FILE_DIRECTORY_FILE + | w::FILE_SYNCHRONOUS_IO_NONALERT + | w::FILE_OPEN_FOR_BACKUP_INTENT, + core::ptr::null_mut(), + 0, + ) + }; + (status == w::NTSTATUS::SUCCESS).then_some(handle) + } + + /// The long name of the entry of `parent` that `component` names. A name + /// without wildcards matches the one entry that has it as its long or its + /// short name, whatever the case. + fn query_long_name<'a>( + parent: HANDLE, + component: &[u16], + entry: &'a mut NameEntry, + ) -> Option<&'a [u16]> { + let mut pattern = unicode_string(component)?; + let mut io: w::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + // SAFETY: `parent` is a synchronous directory handle; `entry` is writable + // for its size and 8-byte aligned; the other pointers are to live locals. + let status = unsafe { + w::ntdll::NtQueryDirectoryFile( + parent, + core::ptr::null_mut(), + core::ptr::null_mut(), + core::ptr::null_mut(), + &mut io, + entry.as_mut_ptr().cast(), + core::mem::size_of::() as u32, + FILE_NAMES_INFORMATION_CLASS, + 1, + &mut pattern, + 1, + ) + }; + if status != w::NTSTATUS::SUCCESS { + return None; + } + let info = entry.as_ptr().cast::(); + // SAFETY: the call filled one entry; its name follows the header inside `entry`. + unsafe { + let units = (*info).FileNameLength as usize / 2; + let name = core::ptr::addr_of!((*info).FileName).cast::(); + let offset = core::mem::offset_of!(FILE_NAMES_INFORMATION, FileName); + if offset + units * 2 > core::mem::size_of::() { + return None; + } + Some(core::slice::from_raw_parts(name, units)) + } + } + + impl DirRequest { + /// # Safety + /// `this` is live with no request outstanding; the caller holds + /// `manager.mutex` or has not shared `this` yet. + unsafe fn issue(this: *mut DirRequest) -> Result<(), w::Win32Error> { + use w::FileNotifyChangeFilter as Filter; + // SAFETY: caller contract. `overlapped` and `buffer` stay valid until + // the completion is dequeued because `this` is only freed then. + unsafe { + (*this).overlapped = bun_core::ffi::zeroed(); + if w::kernel32::ReadDirectoryChangesW( + (*this).dir, + core::ptr::addr_of_mut!((*this).buffer).cast(), + BUFFER_SIZE as DWORD, + BOOL::from((*this).recursive), + Filter::FILE_NAME + | Filter::DIR_NAME + | Filter::ATTRIBUTES + | Filter::SIZE + | Filter::LAST_WRITE + | Filter::LAST_ACCESS + | Filter::CREATION + | Filter::SECURITY, + core::ptr::null_mut(), + core::ptr::addr_of_mut!((*this).overlapped), + None, + ) == 0 + { + return Err(w::Win32Error::get()); + } + (*this).pending = true; + } + Ok(()) + } + + /// # Safety + /// `this` came from `add_watch` and no request is outstanding. + pub(super) unsafe fn destroy(this: *mut DirRequest) { + // SAFETY: caller contract. + let this = unsafe { bun_core::heap::take(this) }; + if this.dir != w::INVALID_HANDLE_VALUE { + // SAFETY: `dir` is the live handle this request owns. + unsafe { w::CloseHandle(this.dir) }; + } + } + + /// Reader thread, `manager.mutex` held, the owner still attached. + /// + /// # Safety + /// `this` is live and its request's completion was just dequeued. + unsafe fn on_completion( + this: *mut DirRequest, + watcher: &PathWatcher, + bytes: usize, + error: Option, + name_buf: &mut path::PathBuffer, + long_buf: &mut [u16], + ) { + // SAFETY: caller contract; the kernel is done with `buffer`. + let request = unsafe { &*this }; + let error = match error { + Some(error) => error, + None => { + if bytes > 0 { + request.emit_records(watcher, bytes, name_buf, long_buf); + } else { + // More changed than `buffer` holds. + watcher.emit_overflow(); + } + // A directory deleted between two requests refuses the + // next one; deleted during one, it fails that one. + // SAFETY: caller contract; no request is outstanding. + match unsafe { DirRequest::issue(this) } { + Ok(()) => return, + Err(error) => error, + } + } + }; + // The next `watch()` of this path has to start a new one. + if let Some(manager) = watcher.manager { + manager.unlink_watcher_locked(core::ptr::from_ref(watcher).cast_mut()); + } + if !(error == w::Win32Error::ACCESS_DENIED && request.is_deleted_directory()) { + watcher.emit_error(&sys::Error::from_win32(error, Tag::watch), true); + return; + } + // The watched directory was deleted: a rename of its full path, and + // nothing is left to ask for. Where deletes do not have POSIX + // semantics the name stays taken while a handle to the directory + // is open. + watcher.emit(WatchEventKind::Rename, watcher.path.as_bytes(), false); + // SAFETY: caller contract; no request is outstanding on `dir`. + unsafe { + w::CloseHandle((*this).dir); + (*this).dir = w::INVALID_HANDLE_VALUE; + } + } + + fn is_deleted_directory(&self) -> bool { + if self.file_name.is_some() { + return false; + } + let mut info = FILE_STANDARD_INFO { + AllocationSize: 0, + EndOfFile: 0, + NumberOfLinks: 0, + DeletePending: 0, + Directory: 0, + }; + // SAFETY: `dir` is live; `info` is a valid out-buffer of the given size. + let ok = unsafe { + GetFileInformationByHandleEx( + self.dir, + FILE_STANDARD_INFO_CLASS, + core::ptr::from_mut(&mut info).cast(), + core::mem::size_of::() as DWORD, + ) + } != 0; + ok && info.Directory != 0 && info.DeletePending != 0 + } + + fn emit_records( + &self, + watcher: &PathWatcher, + bytes: usize, + name_buf: &mut path::PathBuffer, + long_buf: &mut [u16], + ) { + let name_offset = core::mem::offset_of!(w::FILE_NOTIFY_INFORMATION, FileName); + let mut offset: usize = 0; + loop { + if offset + name_offset > bytes { + break; + } + // SAFETY: the kernel wrote `bytes` bytes of DWORD-aligned records; + // `offset` only advances by its `NextEntryOffset` values, and the + // header was bounds-checked above. + let (action, next, name_len) = unsafe { + let info = self + .buffer + .as_ptr() + .add(offset) + .cast::(); + ( + (*info).Action, + (*info).NextEntryOffset as usize, + (*info).FileNameLength as usize, + ) + }; + let name_start = offset + name_offset; + if name_start + name_len > bytes { + break; + } + // SAFETY: within the `bytes` the kernel wrote (checked above), and + // `FileName` is at a DWORD-aligned offset of a DWORD-aligned record. + let name: &[u16] = unsafe { + core::slice::from_raw_parts( + self.buffer.as_ptr().add(name_start).cast::(), + name_len / 2, + ) + }; + + let event_type = match action { + w::FILE_ACTION_MODIFIED => Some(WatchEventKind::Change), + w::FILE_ACTION_ADDED + | w::FILE_ACTION_REMOVED + | w::FILE_ACTION_RENAMED_OLD_NAME + | w::FILE_ACTION_RENAMED_NEW_NAME => Some(WatchEventKind::Rename), + _ => None, + }; + if let Some(event_type) = event_type { + let reported: Option<&[u16]> = match &self.file_name { + Some(file_name) => (eql_ignore_case(name, &file_name.long) + || file_name + .short + .as_deref() + .is_some_and(|short| eql_ignore_case(name, short))) + .then_some(&file_name.long[..]), + // A record can carry an 8.3 alias. For a name that may + // still exist, report its long form like libuv. While + // this thread is busy the changes pile up in the 4 KiB + // the system keeps for the directory, so it only asks + // about a name that can be an alias. + None if action != w::FILE_ACTION_REMOVED + && action != w::FILE_ACTION_RENAMED_OLD_NAME + && may_hold_short_name(name) => + { + Some(self.long_name(name, long_buf).unwrap_or(name)) + } + None => Some(name), + }; + if let Some(reported) = reported { + let reported = path::string_paths::from_w_path(&mut name_buf[..], reported); + watcher.emit(event_type, reported.as_bytes(), watcher.is_file); + } + } + + if next == 0 { + break; + } + offset += next; + } + } + + /// `name` (relative to the watched directory) with every component in + /// its long form, or `None` when the path no longer resolves. Only a + /// component that can be an alias is looked up, in its own directory, + /// which is reached from the watched one by handle: neither the path to + /// the watched directory nor what it is called now matters. + fn long_name<'a>(&self, name: &[u16], long_buf: &'a mut [u16]) -> Option<&'a [u16]> { + let last_lookup = name + .split(|&unit| is_separator(unit)) + .enumerate() + .filter(|(_, component)| is_short_shaped(component)) + .last()? + .0; + if self.dir == w::INVALID_HANDLE_VALUE { + return None; + } + // Every directory of the walk is opened for it and asked about one + // name: a file system may keep the first name a handle was asked + // about and answer every later query with it. `dir` itself is not + // asked: a query on it would complete through the port. + let mut directory: HANDLE = open_for_queries(self.dir, &[])?; + let mut entry: NameEntry = [0; 80]; + let mut len = 0usize; + let mut resolved_all = true; + for (index, component) in name.split(|&unit| is_separator(unit)).enumerate() { + let long: &[u16] = if index <= last_lookup && is_short_shaped(component) { + match query_long_name(directory, component, &mut entry) { + Some(long) => long, + None => { + resolved_all = false; + break; + } + } + } else { + component + }; + let separator = usize::from(index > 0); + if len + separator + long.len() > long_buf.len() { + resolved_all = false; + break; + } + if index > 0 { + long_buf[len] = u16::from(b'\\'); + } + long_buf[len + separator..len + separator + long.len()].copy_from_slice(long); + len += separator + long.len(); + if index < last_lookup { + let Some(child) = open_for_queries(directory, component) else { + resolved_all = false; + break; + }; + // SAFETY: opened by this walk. + unsafe { w::CloseHandle(directory) }; + directory = child; + } + } + // SAFETY: opened by this walk. + unsafe { w::CloseHandle(directory) }; + resolved_all.then_some(&long_buf[..len]) + } + } +} diff --git a/src/runtime/node/time_like.rs b/src/runtime/node/time_like.rs index ec1c626afced..6fc63ee19f10 100644 --- a/src/runtime/node/time_like.rs +++ b/src/runtime/node/time_like.rs @@ -1,14 +1,9 @@ use bun_jsc::{JSGlobalObject, JSType as JsType, JSValue, JsResult}; -/// On windows, this is what libuv expects -/// On unix it is what the utimens api expects -#[cfg(windows)] -pub(crate) type TimeLike = f64; -#[cfg(not(windows))] -pub(crate) type TimeLike = libc::timespec; +pub(crate) type TimeLike = bun_sys::TimeLike; -const NS_PER_S: f64 = bun_core::time::NS_PER_S as f64; #[cfg(not(windows))] +const NS_PER_S: f64 = bun_core::time::NS_PER_S as f64; const MS_PER_S: f64 = bun_core::time::MS_PER_S as f64; #[cfg(not(windows))] const NS_PER_MS: f64 = bun_core::time::NS_PER_MS as f64; @@ -18,6 +13,10 @@ const NS_PER_MS: f64 = bun_core::time::NS_PER_MS as f64; // Node.js docs: // > Values can be either numbers representing Unix epoch time in seconds, Dates, or a numeric string like '123456789.0'. // > If the value can not be converted to a number, or is NaN, Infinity, or -Infinity, an Error will be thrown. +// +// A `Date` or a string goes to libuv as the number it converts to, and there a +// NaN is `UV_FS_UTIME_OMIT` and an infinity is `UV_FS_UTIME_NOW`: +// https://github.com/libuv/libuv/blob/v1.52.1/include/uv.h#L1601-L1602 pub(crate) fn from_js( global_object: &JSGlobalObject, value: JSValue, @@ -36,15 +35,19 @@ pub(crate) fn from_js( match value.js_type() { JsType::JSDate => { let milliseconds = value.get_unix_timestamp(); - if milliseconds.is_finite() { - return Ok(Some(from_milliseconds(milliseconds))); + if milliseconds.is_nan() { + return Ok(Some(omit())); } + return Ok(Some(from_milliseconds(milliseconds))); } JsType::String => { let seconds = value.to_number(global_object)?; if seconds.is_finite() { return Ok(Some(from_seconds(seconds))); } + if seconds.is_infinite() { + return Ok(Some(from_now())); + } } _ => {} } @@ -52,9 +55,28 @@ pub(crate) fn from_js( Ok(None) } +/// The time Node on Windows stores: libuv's `TIME_T_TO_FILETIME` evaluates the +/// FILETIME, `seconds * 1e7 + `, as a double: +/// https://github.com/libuv/libuv/blob/v1.52.1/src/win/fs.c#L138-L143 #[cfg(windows)] fn from_seconds(seconds: f64) -> TimeLike { - seconds + const TICKS_PER_S: i64 = 10_000_000; + const UNIX_EPOCH_TICKS: i64 = 116_444_736_000_000_000; + const I64_LIMIT: f64 = 9_223_372_036_854_775_808.0; + let filetime = seconds * TICKS_PER_S as f64 + UNIX_EPOCH_TICKS as f64; + if !(0.0..I64_LIMIT).contains(&filetime) { + // Not a FILETIME. Saturated, so that `bun_sys` fails the conversion + // with `EINVAL`, which is what `SetFileTime` gives Node. + return TimeLike { + sec: if filetime < 0.0 { i64::MIN } else { i64::MAX }, + nsec: 0, + }; + } + let ticks = filetime as i64 - UNIX_EPOCH_TICKS; + TimeLike { + sec: ticks.div_euclid(TICKS_PER_S), + nsec: ticks.rem_euclid(TICKS_PER_S) * 100, + } } #[cfg(not(windows))] @@ -68,33 +90,33 @@ fn from_seconds(seconds: f64) -> TimeLike { nsec -= NS_PER_S; sec += 1.0; } - libc::timespec { + TimeLike { // `as` saturates on overflow/NaN. - tv_sec: sec as _, - tv_nsec: nsec as _, + sec: sec as i64, + nsec: nsec as i64, } } #[cfg(windows)] fn from_milliseconds(milliseconds: f64) -> TimeLike { - milliseconds / 1000.0 + from_seconds(milliseconds / MS_PER_S) } #[cfg(not(windows))] fn from_milliseconds(milliseconds: f64) -> TimeLike { - libc::timespec { - tv_sec: milliseconds.div_euclid(MS_PER_S) as _, - tv_nsec: (milliseconds.rem_euclid(MS_PER_S) * NS_PER_MS) as _, + TimeLike { + sec: milliseconds.div_euclid(MS_PER_S) as i64, + nsec: (milliseconds.rem_euclid(MS_PER_S) * NS_PER_MS) as i64, } } -#[cfg(windows)] -fn from_now() -> TimeLike { - let nanos = bun_core::time::nano_timestamp(); - (nanos as f64) / NS_PER_S +fn omit() -> TimeLike { + TimeLike { + sec: 0, + nsec: bun_sys::UTIME_OMIT, + } } -#[cfg(not(windows))] fn from_now() -> TimeLike { // Permissions requirements // To set both file timestamps to the current time (i.e., times is @@ -115,11 +137,8 @@ fn from_now() -> TimeLike { // If both tv_nsec fields are specified as UTIME_OMIT, then no file // ownership or permission checks are performed, and the file // timestamps are not modified, but other error conditions may still - libc::timespec { - tv_sec: 0, - #[cfg(any(target_os = "linux", target_os = "android"))] - tv_nsec: libc::UTIME_NOW as _, - #[cfg(not(any(target_os = "linux", target_os = "android")))] - tv_nsec: bun_sys::c::UTIME_NOW as _, + TimeLike { + sec: 0, + nsec: bun_sys::UTIME_NOW, } } diff --git a/src/runtime/node/types.rs b/src/runtime/node/types.rs index 7ce362789056..ada5e9999783 100644 --- a/src/runtime/node/types.rs +++ b/src/runtime/node/types.rs @@ -816,6 +816,16 @@ pub(crate) trait PathLikeExt { where Self: Sized; fn slice_z<'a>(&'a self, buf: &'a mut PathBuffer) -> &'a ZStr + where + Self: Sized; + /// The path as it was written, NUL-terminated, for a call that applies the + /// platform's own rules for names. On Windows those are Win32's: a device + /// name (`NUL`, `CON`) is the device, trailing dots and spaces are dropped, + /// and the call puts a path past `MAX_PATH` in the long form itself. + /// `Bun.write(path, data)` opens its destination this way. + /// [`slice_z`](Self::slice_z), which everything else uses, names the file of + /// an absolute path literally. + fn slice_z_as_written<'a>(&'a self, buf: &'a mut PathBuffer) -> &'a ZStr where Self: Sized; #[cfg(windows)] @@ -882,6 +892,66 @@ pub(crate) trait PathOrFdExt { Self: Sized; } +/// `normal` as a NUL-terminated wide path in `buf`: `\\?\`-prefixed when it, +/// together with the current directory if it is relative, reaches `MAX_PATH` +/// (`bun_sys::windows::fs::kernel32_path`). +#[cfg(windows)] +pub(crate) fn kernel32_path_past_max_path<'a>( + buf: &'a mut PathBuffer, + normal: &[u8], +) -> Result<&'a OSPathSliceZ, NameTooLong> { + // SAFETY: reinterpreting PathBuffer ([u8; N]) as [u16] — 2-byte alignment + // is runtime-asserted inside `bytes_as_slice_mut`. + let buf_u16 = unsafe { bun_core::bytes_as_slice_mut::(&mut buf[..]) }; + let len = match bun_sys::windows::fs::kernel32_path(buf_u16, normal) { + Ok(len) => len, + Err(bun_sys::windows::Win32Error::FILENAME_EXCED_RANGE) => return Err(NameTooLong), + // `GetFullPathNameW` rejected the path; the call it goes to does too. + Err(_) => strings::to_kernel32_path(buf_u16, normal).len(), + }; + Ok(WStr::from_buf(buf_u16, len)) +} + +/// `sliced` with a NUL after it: itself when it has one, in `buf` otherwise. +fn plain_z<'a, const FORCE: bool>(sliced: &'a [u8], buf: &'a mut PathBuffer) -> &'a ZStr { + if sliced.is_empty() { + if !FORCE { + return ZStr::EMPTY; + } + + buf[0] = 0; + // SAFETY: buf[0] == 0 written above. + return ZStr::from_buf(&buf[..], 0); + } + + if !FORCE { + if sliced[sliced.len() - 1] == 0 { + // SAFETY: last byte is NUL. + return ZStr::from_slice_with_nul(sliced); + } + } + + if sliced.len() >= buf.len() { + bun_core::debug_warn!( + "path too long: {} bytes exceeds PathBuffer capacity of {}\n", + sliced.len(), + buf.len() + ); + if !FORCE { + return ZStr::EMPTY; + } + + buf[0] = 0; + // SAFETY: buf[0] == 0 written above. + return ZStr::from_buf(&buf[..], 0); + } + + buf[..sliced.len()].copy_from_slice(sliced); + buf[sliced.len()] = 0; + // SAFETY: buf[sliced.len()] == 0 written above. + ZStr::from_buf(&buf[..], sliced.len()) +} + impl PathLikeExt for PathLike<'_> { // Const-generics can't change return mutability, so this always returns // `&ZStr`. A future force=true caller that needs `&mut ZStr` will need a @@ -942,42 +1012,19 @@ impl PathLikeExt for PathLike<'_> { } } - if sliced.is_empty() { - if !FORCE { - return ZStr::EMPTY; - } - - buf[0] = 0; - // SAFETY: buf[0] == 0 written above. - return ZStr::from_buf(&buf[..], 0); - } + plain_z::(sliced, buf) + } - if !FORCE { - if sliced[sliced.len() - 1] == 0 { - // SAFETY: last byte is NUL. - return ZStr::from_slice_with_nul(sliced); - } + #[inline] + fn slice_z_as_written<'a>(&'a self, buf: &'a mut PathBuffer) -> &'a ZStr { + #[cfg(windows)] + { + plain_z::(self.slice(), buf) } - - if sliced.len() >= buf.len() { - bun_core::debug_warn!( - "path too long: {} bytes exceeds PathBuffer capacity of {}\n", - sliced.len(), - buf.len() - ); - if !FORCE { - return ZStr::EMPTY; - } - - buf[0] = 0; - // SAFETY: buf[0] == 0 written above. - return ZStr::from_buf(&buf[..], 0); + #[cfg(not(windows))] + { + self.slice_z(buf) } - - buf[..sliced.len()].copy_from_slice(sliced); - buf[sliced.len()] = 0; - // SAFETY: buf[sliced.len()] == 0 written above. - ZStr::from_buf(&buf[..], sliced.len()) } #[inline] @@ -1065,15 +1112,11 @@ impl PathLikeExt for PathLike<'_> { return Err(NameTooLong); } // `resolve`'s borrow of `buf` ended at the line above (NLL). - // SAFETY: same alignment note as above. - let buf_u16 = unsafe { bun_core::bytes_as_slice_mut::(&mut buf[..]) }; - return Ok(strings::to_kernel32_path(buf_u16, normal)); + return kernel32_path_past_max_path(buf, normal); } // Handle "." specially since normalizeStringBuf strips it to an empty string if s.len() == 1 && s[0] == b'.' { - // SAFETY: see alignment note above (PathBuffer reinterpreted as [u16]). - let buf_u16 = unsafe { bun_core::bytes_as_slice_mut::(&mut buf[..]) }; - return Ok(strings::to_kernel32_path(buf_u16, b".")); + return kernel32_path_past_max_path(buf, b"."); } let normal = bun_paths::resolve_path::normalize_string_buf::< true, @@ -1083,9 +1126,7 @@ impl PathLikeExt for PathLike<'_> { if !strings::fits_in_wide_path_buffer(normal) { return Err(NameTooLong); } - // SAFETY: see alignment note above (PathBuffer reinterpreted as [u16]). - let buf_u16 = unsafe { bun_core::bytes_as_slice_mut::(&mut buf[..]) }; - return Ok(strings::to_kernel32_path(buf_u16, normal)); + return kernel32_path_past_max_path(buf, normal); } #[cfg(not(windows))] @@ -1543,18 +1584,9 @@ impl FileSystemFlags { // arrive double-boxed and must not be rejected. let number = validators::validate_int32(ctx, val, "flags", None, None)?; let flags = number.max(0); - // On Windows, numeric flags from fs.constants (e.g. O_CREAT=0x100) - // use the platform's native MSVC/libuv values which differ from the - // internal bun.O representation. Convert them here so downstream - // code that operates on bun.O flags works correctly. #[cfg(windows)] - { - return Ok(Some(FileSystemFlags(bun_libuv_sys::O::to_bun_o(flags)))); - } - #[cfg(not(windows))] - { - return Ok(Some(FileSystemFlags(flags))); - } + let flags = bun_sys::windows::O::from_js(flags); + return Ok(Some(FileSystemFlags(flags))); } if val.is_undefined_or_null() { @@ -1717,10 +1749,16 @@ impl Dirent { global_object: &JSGlobalObject, cached_previous_path_jsvalue: Option<&mut *mut jsc::JSString>, ) -> JsResult { - use bun_libuv_sys::{ - UV_DIRENT_BLOCK, UV_DIRENT_CHAR, UV_DIRENT_DIR, UV_DIRENT_FIFO, UV_DIRENT_FILE, - UV_DIRENT_LINK, UV_DIRENT_SOCKET, UV_DIRENT_UNKNOWN, - }; + // `uv_dirent_type_t`, shared with `Bun__Dirent__toJS` and + // `process.binding('constants').fs.UV_DIRENT_*`. + const UV_DIRENT_UNKNOWN: i32 = 0; + const UV_DIRENT_FILE: i32 = 1; + const UV_DIRENT_DIR: i32 = 2; + const UV_DIRENT_LINK: i32 = 3; + const UV_DIRENT_FIFO: i32 = 4; + const UV_DIRENT_SOCKET: i32 = 5; + const UV_DIRENT_CHAR: i32 = 6; + const UV_DIRENT_BLOCK: i32 = 7; let kind_int: i32 = match self.kind { DirentKind::File => UV_DIRENT_FILE, DirentKind::BlockDevice => UV_DIRENT_BLOCK, diff --git a/src/runtime/node/uv_signal_handle_windows.rs b/src/runtime/node/uv_signal_handle_windows.rs deleted file mode 100644 index e1d990c5602c..000000000000 --- a/src/runtime/node/uv_signal_handle_windows.rs +++ /dev/null @@ -1,66 +0,0 @@ -//! Windows-only `uv_signal_t` lifecycle exported for `BunProcess.cpp`. -//! Lives under `runtime/` because `init` takes a `*JSGlobalObject` to reach -//! the VM's libuv loop; the rest of `sys/windows/` is JSC-free. - -use core::ffi::c_int; - -use bun_jsc::JSGlobalObject; -use bun_sys::windows::libuv; - -#[unsafe(no_mangle)] -extern "C" fn Bun__UVSignalHandle__init( - global: &JSGlobalObject, - signal_num: i32, - callback: unsafe extern "C" fn(sig: *mut libuv::uv_signal_t, num: c_int), -) -> *mut libuv::uv_signal_t { - // SAFETY: uv_signal_t is #[repr(C)] POD; uv_signal_init fully initializes it below. - let signal: *mut libuv::uv_signal_t = - bun_core::heap::into_raw(Box::::new_uninit()).cast(); - - // SAFETY: `signal` is a freshly heap-allocated, properly aligned uv_signal_t and - // `uv_loop()` returns the VM's live libuv loop. - let mut rc = unsafe { libuv::uv_signal_init(global.bun_vm().uv_loop(), signal) }; - if rc.is_err() { - // SAFETY: `signal` was just allocated via heap::into_raw above and never handed out. - drop(unsafe { bun_core::heap::take(signal) }); - return core::ptr::null_mut(); - } - - // SAFETY: `signal` was successfully initialized by uv_signal_init above. - rc = unsafe { libuv::uv_signal_start(signal, Some(callback), signal_num) }; - if rc.is_err() { - // SAFETY: `signal` is an initialized handle; uv_close will invoke the cb once - // the handle is fully closed, at which point we free the allocation. - unsafe { libuv::uv_close(signal.cast(), Some(free_with_default_allocator)) }; - return core::ptr::null_mut(); - } - - // SAFETY: `signal` is an active, initialized handle. - unsafe { libuv::uv_unref(signal.cast()) }; - - signal -} - -extern "C" fn free_with_default_allocator(handle: *mut libuv::uv_handle_t) { - // Body discharges its own precondition; safe `extern "C" fn` coerces to - // libuv's `uv_close_cb` pointer type. - // SAFETY: `handle` was allocated via heap::into_raw(Box) in - // Bun__UVSignalHandle__init; uv_close guarantees the handle is no longer in use. - drop(unsafe { bun_core::heap::take(handle.cast::()) }); -} - -// C++ declaration (`BunProcess.cpp:1177`): -// extern "C" uv_signal_t* Bun__UVSignalHandle__close(uv_signal_t*); -// The caller discards the return, but the Rust side must still match the -// signature — a `void`-vs-pointer return is an ABI mismatch (different -// register usage on Win64). Return null (handle is being torn down). -#[unsafe(no_mangle)] -extern "C" fn Bun__UVSignalHandle__close( - signal: *mut libuv::uv_signal_t, -) -> *mut libuv::uv_signal_t { - // SAFETY: `signal` is a live handle previously returned by Bun__UVSignalHandle__init. - let _ = unsafe { libuv::uv_signal_stop(signal) }; - // SAFETY: `signal` is an initialized handle; the close cb frees the backing allocation. - unsafe { libuv::uv_close(signal.cast(), Some(free_with_default_allocator)) }; - core::ptr::null_mut() -} diff --git a/src/runtime/node/win_watcher.rs b/src/runtime/node/win_watcher.rs deleted file mode 100644 index 0e75fd8d6cc7..000000000000 --- a/src/runtime/node/win_watcher.rs +++ /dev/null @@ -1,547 +0,0 @@ -//! Windows-only filesystem watcher backed by libuv `uv_fs_event_t`. - -#![cfg(windows)] - -use core::cell::Cell; -use core::ffi::{c_char, c_int, c_void}; -use core::ptr; - -use bun_collections::{ArrayHashMap, StringArrayHashMap}; -use bun_core::{String as BunString, ZStr}; -use bun_jsc as jsc; -use bun_jsc::JsCell; -use bun_sys as sys; -use bun_sys::ReturnCodeExt as _; -use bun_sys::windows::libuv as uv; -use bun_sys::windows::libuv::UvHandle as _; -use bun_threading::Mutex; - -use super::node_fs_watcher::WatchEventKind; -// The callbacks are *associated functions* on `FSWatcher`, not free fns. -use crate::node::node_fs_watcher::{Event, FSWatcher, StringOrBytesToDecode}; -#[allow(non_upper_case_globals)] -const on_path_update_fn: fn(Option<*mut c_void>, Event, bool) = FSWatcher::ON_PATH_UPDATE; -#[allow(non_upper_case_globals)] -const on_update_end_fn: fn(Option<*mut c_void>) = FSWatcher::on_update_end; - -bun_output::declare_scope!(PathWatcherManager, visible); -// Rust identifiers cannot contain '.', so -// the static is declared by hand (instead of via `declare_scope!`) with the -// tag string, keeping `BUN_DEBUG_fs.watch` env matching and the -// `[fs.watch]` log prefix. -#[allow(non_upper_case_globals)] -pub(crate) static fs_watch: bun_output::ScopedLogger = - bun_output::ScopedLogger::new("fs.watch", bun_output::Visibility::Visible); - -// ────────────────────────────────────────────────────────────────────────── - -// PORTING.md §Global mutable state: singleton ptr → `AtomicCell`, guarded by -// `DEFAULT_MANAGER_MUTEX`. `fs.watch()` is reachable from Worker JS threads -// (each Worker is its own OS thread + VM), so an unguarded read+write -// would be a data race. Mirror the posix -// `path_watcher.rs` pattern: every `DEFAULT_MANAGER` access holds the mutex. -// `AtomicCell<*mut _>` (Acquire/Release on the pointer word) means even an -// unsynchronized racing reader observes either null or a fully-published -// pointer — and lets every load/store be safe code (`RacyCell` required an -// `unsafe` block per access for the same single-word op). -// -// NOTE: the manager binds to one VM's `uv_loop`, so it is a per-VM resource — -// `watch()` allocates a fresh manager whenever the caller's `vm` differs from -// the one stored here (last caller wins the slot), so a Worker never mutates -// another VM's manager or drives its uv_loop cross-thread. Promoting this to -// true per-VM storage (e.g. `RareData`) is the longer-term fix. -static DEFAULT_MANAGER: bun_core::AtomicCell<*mut PathWatcherManager> = - bun_core::AtomicCell::new(ptr::null_mut()); -static DEFAULT_MANAGER_MUTEX: Mutex = Mutex::new(); - -// TODO: make this a generic so we can reuse code with path_watcher -// TODO: we probably should use native instead of libuv abstraction here for better performance -pub(crate) struct PathWatcherManager { - // Keys are owned path bytes, values are raw heap - // PathWatcher ptrs. `StringArrayHashMap` lets `get`/`insert` take `&[u8]` borrows. - watchers: JsCell>, - // LIFETIMES.tsv: JSC_BORROW → `&VirtualMachine`. The manager is heap-allocated and stored in a - // process-global, so we spell the borrow as `'static`; soundness relies on - // the owning VM outliving the manager (watchers are torn down before the VM). - vm: &'static jsc::VirtualMachineRef, - deinit_on_last_watcher: Cell, -} - -impl PathWatcherManager { - fn init(vm: &'static jsc::VirtualMachineRef) -> *mut PathWatcherManager { - bun_core::heap::into_raw(Box::new(PathWatcherManager { - watchers: JsCell::new(StringArrayHashMap::default()), - vm, - // A manager can be displaced from `DEFAULT_MANAGER` by a `watch()` - // call from a different VM; without this the displaced manager - // would never be freed. Set here — on the owning thread, before the - // pointer is published — to avoid a cross-thread write at - // displacement time. - deinit_on_last_watcher: Cell::new(true), - })) - } - - fn unregister_watcher(this: *mut PathWatcherManager, watcher: *mut PathWatcher, path: &ZStr) { - #[cfg(not(debug_assertions))] - let _ = path; - let should_deinit = { - // SAFETY: `this` is the live heap pointer produced by `init`; the - // borrow ends before `deinit` may free it. - let me = unsafe { &*this }; - me.watchers.with_mut(|watchers| { - if let Some(index) = watchers.values().iter().position(|&w| w == watcher) { - #[cfg(debug_assertions)] - { - if !path.as_bytes().is_empty() { - debug_assert!(&*watchers.keys()[index] == path.as_bytes()); - } - } - - // Key is `Box<[u8]>`; swap_remove drops it (replaces `allocator.free(keys[index])`). - watchers.swap_remove_at(index); - } - }); - me.deinit_on_last_watcher.get() && me.watchers.get().len() == 0 - }; - if should_deinit { - // SAFETY: `this` was heap-allocated in `init`; no borrow of it is live here. - unsafe { Self::deinit(this) }; - } - } - - /// Tear down the manager. Takes a raw pointer because it frees `self`. - /// - /// NOTE: not `impl Drop` — this type is always held via `*mut` (global static + BACKREF from - /// PathWatcher) and self-frees via `heap::take`. - unsafe fn deinit(this: *mut PathWatcherManager) { - // enable to create a new manager - { - let _g = DEFAULT_MANAGER_MUTEX.lock_guard(); - if DEFAULT_MANAGER.load() == this { - DEFAULT_MANAGER.store(ptr::null_mut()); - } - } - - { - // SAFETY: caller guarantees `this` is a live heap-allocated pointer (see `init`); - // the borrow ends before the free below. - let me = unsafe { &*this }; - - if me.watchers.get().len() != 0 { - me.deinit_on_last_watcher.set(true); - return; - } - } - - // Keys (`Box<[u8]>`) are dropped by the map's Drop — replaces the explicit - // `allocator.free(path)` loop + `watchers.deinit(allocator)`. - // SAFETY: `this` was produced by heap::alloc in `init`. - drop(unsafe { bun_core::heap::take(this) }); - } -} - -// ────────────────────────────────────────────────────────────────────────── - -pub(crate) struct PathWatcher { - handle: uv::uv_fs_event_t, - // LIFETIMES.tsv: BACKREF → Option<*mut PathWatcherManager> - manager: Cell>, - emit_in_progress: Cell, - handlers: JsCell>, -} - -#[derive(Clone, Copy)] -pub(crate) struct ChangeEvent { - hash: bun_watcher::HashType, - event_type: WatchEventKind, - timestamp: u64, -} - -impl Default for ChangeEvent { - fn default() -> Self { - Self { - hash: 0, - event_type: WatchEventKind::Change, - timestamp: 0, - } - } -} - -impl ChangeEvent { - fn emit( - &mut self, - hash: bun_watcher::HashType, - timestamp: u64, - event_type: WatchEventKind, - ) -> bool { - let time_diff = timestamp.saturating_sub(self.timestamp); - // skip consecutive exact duplicates (same path and event type) only - if self.timestamp == 0 - || time_diff > 1 - || self.event_type != event_type - || self.hash != hash - { - self.timestamp = timestamp; - self.event_type = event_type; - self.hash = hash; - return true; - } - false - } -} - -impl PathWatcher { - extern "C" fn uv_event_callback( - event: *mut uv::uv_fs_event_t, - filename: *const c_char, - events: c_int, - status: uv::ReturnCode, - ) { - // SAFETY: libuv guarantees `event` is the handle we registered. - if unsafe { (*event).data }.is_null() { - bun_core::debug_warn!("uvEventCallback called with null data"); - return; - } - // SAFETY: event points to PathWatcher.handle; recover the parent via offset_of. - let this: *mut PathWatcher = - unsafe { bun_core::from_field_ptr!(PathWatcher, handle, event) }; - // SAFETY: `this` was heap-allocated in `init` and is kept alive until - // uv_close fires; only shared access is taken and `me` is not used past - // any call that can free it. - let me = unsafe { &*this }; - #[cfg(debug_assertions)] - { - debug_assert!(me.handle.data == this.cast::()); - } - - // SAFETY: libuv contract — `loop_` is valid while the handle is open. - let timestamp = unsafe { (*me.handle.loop_).time }; - - if let Some(err) = status.to_error(sys::Tag::watch) { - me.emit_in_progress.set(true); - - let keys: Vec<_> = me.handlers.get().keys().iter().copied().collect(); - for ctx in keys { - if !me.handlers.get().contains_key(&ctx) { - continue; - } - on_path_update_fn( - Some(ctx), - Event::Error { - err: err.clone(), - close: true, - }, - false, - ); - on_update_end_fn(Some(ctx)); - } - - me.emit_in_progress.set(false); - Self::maybe_deinit(this); - return; - } - - let event_type = if events & uv::UV_RENAME != 0 { - WatchEventKind::Rename - } else { - WatchEventKind::Change - }; - - if filename.is_null() { - // ReadDirectoryChangesW overflowed and changes were lost (always - // UV_CHANGE), or libuv could not convert the name to UTF-8. - // Forward `(event, null)` to every handler like node, unsuppressed. - me.emit_in_progress.set(true); - let keys: Vec<_> = me.handlers.get().keys().iter().copied().collect(); - for ctx in keys { - if !me.handlers.get().contains_key(&ctx) { - continue; - } - on_path_update_fn(Some(ctx), Event::NoFilename(event_type), false); - on_update_end_fn(Some(ctx)); - } - me.emit_in_progress.set(false); - Self::maybe_deinit(this); - return; - } - // SAFETY: libuv passes a valid NUL-terminated string when non-null. - let path = ZStr::from_cstr(unsafe { core::ffi::CStr::from_ptr(filename) }); - - // Intentional wrap to bun_watcher::HashType - let hash = me.handle.hash(path.as_bytes(), events, status) as bun_watcher::HashType; - let is_file = !me.handle.is_dir(); - Self::emit(this, path.as_bytes(), hash, timestamp, is_file, event_type); - } - - fn emit( - this: *mut PathWatcher, - path: &[u8], - hash: bun_watcher::HashType, - timestamp: u64, - is_file: bool, - event_type: WatchEventKind, - ) { - { - // SAFETY: `this` is the live heap pointer from `init`; the borrow ends - // before `maybe_deinit` may free it. - let me = unsafe { &*this }; - me.emit_in_progress.set(true); - #[cfg(debug_assertions)] - let mut debug_count: usize = 0; - - let keys: Vec<_> = me.handlers.get().keys().iter().copied().collect(); - for key in keys { - let Some(fire) = me - .handlers - .with_mut(|h| h.get_mut(&key).map(|v| v.emit(hash, timestamp, event_type))) - else { - continue; - }; - if fire { - let ctx: *mut FSWatcher = key.cast::(); - // SAFETY: handlers keys are `*mut FSWatcher` erased to `*mut c_void` in `watch()`. - let encoding = unsafe { (*ctx).encoding }; - // `EventPathString` on Windows is `StringOrBytesToDecode`. - let payload = match encoding { - crate::node::Encoding::Utf8 => { - StringOrBytesToDecode::String(BunString::clone_utf8(path)) - } - _ => StringOrBytesToDecode::BytesToFree(Box::<[u8]>::from(path)), - }; - on_path_update_fn(Some(ctx.cast()), event_type.to_event(payload), is_file); - #[cfg(debug_assertions)] - { - debug_count += 1; - } - on_update_end_fn(Some(ctx.cast())); - } - } - - #[cfg(debug_assertions)] - bun_output::scoped_log!( - fs_watch, - "emit({}, {}, {}, at {}) x {}", - bstr::BStr::new(path), - if is_file { "file" } else { "dir" }, - <&'static str>::from(event_type), - timestamp, - debug_count, - ); - - me.emit_in_progress.set(false); - } - Self::maybe_deinit(this); - } - - fn init( - manager: *mut PathWatcherManager, - path: &ZStr, - recursive: bool, - ) -> sys::Result<*mut PathWatcher> { - let mut outbuf = bun_paths::path_buffer_pool::get(); - // Windows `sys::readlink` returns the byte length; the link target is - // written into `outbuf[..len]` with `outbuf[len] == 0` (sys_uv NUL-terminates). Reconstruct - // the NUL-terminated string via `ZStr::from_buf`. - let readlink_result = sys::readlink(path, &mut outbuf); - let event_path: &ZStr = match readlink_result { - sys::Result::Err(err) => 'brk: { - if err.errno == sys::E::NOENT as _ { - return sys::Result::Err(sys::Error { - errno: err.errno, - syscall: sys::Tag::open, - ..Default::default() - }); - } - break 'brk path; - } - sys::Result::Ok(len) => ZStr::from_buf(outbuf.as_slice(), len), - }; - - // SAFETY: `manager` is the live heap pointer for the calling VM's - // manager; `watch()` holds DEFAULT_MANAGER_MUTEX, serializing this - // registration against concurrent Worker `watch()` calls. - let mgr = unsafe { &*manager }; - - if let Some(&existing) = mgr.watchers.get().get(event_path.as_bytes()) { - return sys::Result::Ok(existing); - } - - let this_box = Box::new(PathWatcher { - handle: bun_core::ffi::zeroed(), - manager: Cell::new(Some(manager)), - emit_in_progress: Cell::new(false), - handlers: JsCell::new(ArrayHashMap::default()), - }); - let this = bun_core::heap::into_raw(this_box); - - // uv_fs_event_init on Windows unconditionally returns 0 (vendor/libuv/src/win/fs-event.c). - // bun.assert evaluates its argument before the inline early-return, so this runs in release too. - // SAFETY: `this` is a freshly-allocated valid pointer; uv_loop comes from the VM. - unsafe { - // `ptr::addr_of_mut!` (not `&mut (*this).handle`): libuv stashes this pointer and - // hands it back to `uv_event_callback`, which `from_field_ptr!`-offsets it to recover - // the parent `PathWatcher`. Deriving via `addr_of_mut!` keeps `this`'s whole-allocation - // provenance so that container-of access stays in-bounds under Stacked Borrows. - let rc = uv::uv_fs_event_init(mgr.vm.uv_loop(), ptr::addr_of_mut!((*this).handle)); - debug_assert!(rc == uv::ReturnCode::zero()); - (*this).handle.data = this.cast::(); - } - - // UV_FS_EVENT_RECURSIVE only works for Windows and OSX - // SAFETY: `(*this).handle` was initialized by uv_fs_event_init above; event_path is NUL-terminated. - let start_rc = unsafe { - uv::uv_fs_event_start( - ptr::addr_of_mut!((*this).handle), - Some(PathWatcher::uv_event_callback), - event_path.as_ptr().cast::(), - if recursive { - uv::UV_FS_EVENT_RECURSIVE as u32 - } else { - 0 - }, - ) - }; - if let Some(err) = start_rc.to_error(sys::Tag::watch) { - // Clean up the half-initialized watcher inline (see #26254). No map - // entry was inserted yet (see reshape above), so there is nothing - // to swap_remove here. - // SAFETY: `this` is the freshly heap-allocated pointer above; deinit consumes it. - unsafe { - (*this).manager.set(None); // prevent deinit() from re-entering unregister_watcher - PathWatcher::deinit(this); - } - return sys::Result::Err(err); - } - // we handle this in node_fs_watcher - // SAFETY: handle is open (uv_fs_event_start succeeded); uv_unref only flips the ref flag. - unsafe { uv::uv_unref(ptr::addr_of_mut!((*this).handle).cast()) }; - - // Owned key: dupe of event_path bytes (the sentinel NUL is not part of the - // slice's `.len`, so the StringArrayHashMap key compares equal to `event_path.as_bytes()`). - mgr.watchers.with_mut(|watchers| { - watchers.insert(event_path.as_bytes(), this); - }); - - sys::Result::Ok(this) - } - - extern "C" fn uv_closed_callback(handler: *mut uv::uv_handle_t) { - // Body discharges its own preconditions; safe `extern "C" fn` coerces - // to libuv's `uv_close_cb` pointer type. - bun_output::scoped_log!(fs_watch, "onClose"); - // SAFETY: `uv_fs_event_t` is `#[repr(C)]` and prefixed with `uv_handle_t` (UvHandle impl); - // libuv passes back the same pointer registered in `uv_close`. - let event = handler.cast::(); - // SAFETY: event.data was set to the PathWatcher* in `init`. - let this = unsafe { (*event).data.cast::() }; - // SAFETY: `this` was heap-allocated in `init`. - drop(unsafe { bun_core::heap::take(this) }); - } - - /// JS-thread entry point from `FSWatcher.detach()`. Signature matches the posix - /// `path_watcher::PathWatcher::detach` (associated fn over `*mut Self`) so the - /// caller in `node_fs_watcher.rs` is platform-agnostic. - pub(crate) fn detach(this: *mut PathWatcher, handler: *mut c_void) { - // SAFETY: `this` is the live `heap::alloc`'d pointer returned from `watch()`; - // it stays valid until `maybe_deinit` self-destroys on the last handler. - let removed = unsafe { &*this } - .handlers - .with_mut(|h| h.swap_remove(&handler)); - if removed { - Self::maybe_deinit(this); - } - } - - fn maybe_deinit(this: *mut PathWatcher) { - // SAFETY: `this` is the live heap pointer from `init`; the borrow ends - // before `deinit` may free it. - let should_deinit = { - let me = unsafe { &*this }; - me.handlers.get().len() == 0 && !me.emit_in_progress.get() - }; - if should_deinit { - // SAFETY: `this` was heap-allocated in `init`; no borrow of it is live here. - unsafe { Self::deinit(this) }; - } - } - - /// NOTE: not `impl Drop` — destruction is deferred through `uv_close` and the close callback - /// frees the box, so this type is always managed via raw `*mut PathWatcher`. - unsafe fn deinit(this: *mut PathWatcher) { - bun_output::scoped_log!(fs_watch, "deinit"); - { - // SAFETY: caller guarantees `this` is a live heap-allocated pointer (see `init`); - // the borrow ends before the free below. - let me = unsafe { &*this }; - me.handlers.with_mut(|h| h.clear()); - - if let Some(manager) = me.manager.take() { - let path: &ZStr = if !me.handle.path.is_null() { - // SAFETY: handle.path is a NUL-terminated C string owned by libuv. - ZStr::from_cstr(unsafe { core::ffi::CStr::from_ptr(me.handle.path) }) - } else { - ZStr::EMPTY - }; - PathWatcherManager::unregister_watcher(manager, this, path); - } - } - - // `UvHandle::is_closed` reads `flags & UV_HANDLE_CLOSED` via the handle prefix. - // SAFETY: `this` is still the live heap allocation from `init`. - if unsafe { (*this).handle.is_closed() } { - // SAFETY: `this` was heap-allocated in `init`. - drop(unsafe { bun_core::heap::take(this) }); - } else { - // SAFETY: handle is open and not yet closing; stop/close are valid in that state. - unsafe { - uv::uv_fs_event_stop(ptr::addr_of_mut!((*this).handle)); - uv::uv_close( - ptr::addr_of_mut!((*this).handle).cast(), - Some(PathWatcher::uv_closed_callback), - ); - } - } - } -} - -// ────────────────────────────────────────────────────────────────────────── - -pub(crate) fn watch( - vm: &'static jsc::VirtualMachineRef, - path: &ZStr, - recursive: bool, - ctx: *mut c_void, -) -> sys::Result<*mut PathWatcher> { - // DEFAULT_MANAGER is only read/written while holding DEFAULT_MANAGER_MUTEX - // (see static decl). The guard covers the whole registration — not just the - // slot load — because `PathWatcher::init` below mutates the manager's - // `watchers` map, and `fs.watch()` is reachable from Worker threads. - let _g = DEFAULT_MANAGER_MUTEX.lock_guard(); - let existing = DEFAULT_MANAGER.load(); - // The manager is bound to one VM's uv_loop; reusing it from a different VM - // (Worker) would mutate its watcher map and drive libuv cross-thread. - // Allocate a fresh manager for this VM instead; the displaced one frees - // itself once its last watcher unregisters (`deinit_on_last_watcher`). - // SAFETY: `existing` is a non-null pointer published under - // DEFAULT_MANAGER_MUTEX (which we hold) by `init` below on a prior call; - // the allocation lives until `deinit` clears the slot, so it is valid here. - // `vm` is written once at construction and never mutated, so reading it - // cannot race with the owning VM's thread. - let manager = if existing.is_null() || !core::ptr::eq(unsafe { (*existing).vm }, vm) { - let m = PathWatcherManager::init(vm); - DEFAULT_MANAGER.store(m); - m - } else { - existing - }; - - let watcher = match PathWatcher::init(manager, path, recursive) { - sys::Result::Err(err) => return sys::Result::Err(err), - sys::Result::Ok(w) => w, - }; - // SAFETY: watcher is a valid freshly-returned heap pointer. - unsafe { &*watcher }.handlers.with_mut(|h| { - h.insert(ctx, ChangeEvent::default()); - }); - sys::Result::Ok(watcher) -} diff --git a/src/runtime/node/windows_signal.rs b/src/runtime/node/windows_signal.rs new file mode 100644 index 000000000000..493b21571b67 --- /dev/null +++ b/src/runtime/node/windows_signal.rs @@ -0,0 +1,427 @@ +//! Where `process.on()` gets its signals on Windows. Console control +//! events stand in for SIGINT, SIGBREAK and SIGHUP, and a change of the +//! console's size for SIGWINCH. Each is reported through `Bun__onPosixSignal`, +//! so from the signal ring onwards a signal takes the path it takes on POSIX. +//! SIGTERM and SIGQUIT can be listened for and are never raised. +//! +//! The console reports a change of its size in two ways. Whoever reads its +//! input in raw mode gets a record for it, which `bun_io`'s reader passes on. A +//! console that has a window also raises the `EVENT_CONSOLE_LAYOUT` WinEvent, +//! which reaches [`layout_thread`] whatever this process reads. A pseudoconsole +//! raises no WinEvents: under one, a process hears of a resize only while it +//! reads the console's input in raw mode. + +use core::ffi::{c_int, c_void}; +use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU32, AtomicU64, Ordering}; + +use bun_jsc::posix_signal_handle::Bun__onPosixSignal; +use bun_sys::windows as win; + +// The numbers `os.constants.signals` reports on Windows. +const SIGHUP: c_int = 1; +const SIGINT: c_int = 2; +const SIGBREAK: c_int = 21; +const SIGWINCH: c_int = 28; + +#[link(name = "kernel32")] +unsafe extern "system" { + safe fn Sleep(dwMilliseconds: win::DWORD); +} + +/// Bit `n` is set while signal `n`, one a console control event stands for, +/// has a listener. +static WATCHED: AtomicU32 = AtomicU32::new(0); + +/// The process's only console control routine. It runs on a thread the system +/// creates for each event. While the event's signal has no listener it is left +/// to the default routine, which ends the process without running anything +/// registered for exit, so the console's modes are put back first. +extern "system" fn on_console_ctrl(ctrl_type: win::DWORD) -> win::BOOL { + let signal = match ctrl_type { + win::CTRL_C_EVENT => SIGINT, + win::CTRL_BREAK_EVENT => SIGBREAK, + win::CTRL_CLOSE_EVENT => SIGHUP, + // Logoff and shutdown are only sent to services, which have their own + // notification for them. + _ => return win::FALSE, + }; + if WATCHED.load(Ordering::Acquire) & (1 << signal) == 0 { + bun_core::output::source::stdio::restore(); + bun_io::windows::tty::reset_console_mode(); + return win::FALSE; + } + Bun__onPosixSignal(signal); + if ctrl_type == win::CTRL_CLOSE_EVENT { + // The system terminates the process when this handler returns, and + // after about five seconds if it does not. Not returning is what gives + // the listener that time (libuv: uv__signal_control_handler). + Sleep(win::INFINITE); + } + win::TRUE +} + +/// Once, at startup. Windows calls control routines newest first, so one +/// added at the first `process.on("SIGINT")` would come before every routine +/// registered earlier, such as the one `vm`'s `breakOnSigint` installs. +#[unsafe(no_mangle)] +pub(crate) extern "C" fn Bun__installWindowsSignalHandler() { + let _ = win::SetConsoleCtrlHandler(Some(on_console_ctrl), win::TRUE); +} + +/// First `process.on()` listener for `signum` on the main thread. +#[unsafe(no_mangle)] +pub(crate) extern "C" fn Bun__watchWindowsSignal(signum: c_int) { + match signum { + SIGINT | SIGBREAK | SIGHUP => { + WATCHED.fetch_or(1 << signum, Ordering::Release); + } + SIGWINCH => watch_console_size(), + _ => {} + } +} + +/// Last listener for `signum` removed. +#[unsafe(no_mangle)] +pub(crate) extern "C" fn Bun__unwatchWindowsSignal(signum: c_int) { + match signum { + SIGINT | SIGBREAK | SIGHUP => { + WATCHED.fetch_and(!(1 << signum), Ordering::Release); + } + SIGWINCH => unwatch_console_size(), + _ => {} + } +} + +// ────────────────────────────────────────────────────────────────────────── +// SIGWINCH +// ────────────────────────────────────────────────────────────────────────── + +/// `CONOUT$`, opened by the first SIGWINCH listener and kept. +static CONSOLE_OUTPUT: AtomicPtr = AtomicPtr::new(core::ptr::null_mut()); +/// The size SIGWINCH was last raised for, as `process.stdout.columns` and +/// `rows` report it: columns in the high half, rows in the low half. +static CONSOLE_SIZE: AtomicU64 = AtomicU64::new(0); + +fn console_output() -> Option { + let cached = CONSOLE_OUTPUT.load(Ordering::Acquire); + if !cached.is_null() { + return Some(cached); + } + // SAFETY: plain Win32 call; the name is NUL-terminated. + let console = unsafe { + win::CreateFileW( + bun_core::w!("CONOUT$\0").as_ptr(), + win::GENERIC_READ | win::GENERIC_WRITE, + win::FILE_SHARE_READ | win::FILE_SHARE_WRITE, + core::ptr::null_mut(), + win::OPEN_EXISTING, + 0, + core::ptr::null_mut(), + ) + }; + if console == win::INVALID_HANDLE_VALUE { + return None; + } + match CONSOLE_OUTPUT.compare_exchange( + core::ptr::null_mut(), + console, + Ordering::AcqRel, + Ordering::Acquire, + ) { + Ok(_) => Some(console), + Err(theirs) => { + // SAFETY: opened above and handed to nobody. + unsafe { win::CloseHandle(console) }; + Some(theirs) + } + } +} + +fn console_size() -> Option { + let console = console_output()?; + let mut info: win::CONSOLE_SCREEN_BUFFER_INFO = bun_core::ffi::zeroed(); + // SAFETY: `info` is a live local. + if unsafe { win::kernel32::GetConsoleScreenBufferInfo(console, &raw mut info) } == 0 { + return None; + } + // Text wraps at the width of the screen buffer, which a window can be + // narrower than. + let columns = info.dwSize.X as u16; + let rows = (info.srWindow.Bottom - info.srWindow.Top + 1) as u16; + Some(u64::from(columns) << 32 | u64::from(rows)) +} + +/// Raise SIGWINCH if the console is not the size it was last raised for. Any +/// thread: [`layout_thread`], and whichever loop reads raw console input when +/// the console reports a resize there. +fn raise_if_console_resized() { + let Some(size) = console_size() else { + return; + }; + if CONSOLE_SIZE.swap(size, Ordering::AcqRel) != size { + Bun__onPosixSignal(SIGWINCH); + } +} + +/// Held to read or change the three below. +static LAYOUT_LOCK: bun_threading::Mutex = bun_threading::Mutex::new(); +/// SIGWINCH has a listener. +static LAYOUT_WANTED: AtomicBool = AtomicBool::new(false); +/// A [`layout_thread`] exists that has not decided to end. +static LAYOUT_THREAD_RUNNING: AtomicBool = AtomicBool::new(false); +/// That thread, once a message can be posted to it; 0 otherwise. +static LAYOUT_THREAD_ID: AtomicU32 = AtomicU32::new(0); +/// A layout event has come in since [`layout_thread`] last compared sizes. Only +/// that thread touches it: [`on_console_layout`] runs on it. +static LAYOUT_CHANGED: AtomicBool = AtomicBool::new(false); + +fn watch_console_size() { + // No console, nothing to watch. + let Some(size) = console_size() else { + return; + }; + CONSOLE_SIZE.store(size, Ordering::Release); + bun_io::windows::tty::set_resize_listener(Some(raise_if_console_resized)); + + let _lock = LAYOUT_LOCK.lock_guard(); + LAYOUT_WANTED.store(true, Ordering::Release); + if !LAYOUT_THREAD_RUNNING.load(Ordering::Acquire) { + let spawned = std::thread::Builder::new() + .name("ConsoleLayout".into()) + .spawn(layout_thread) + .is_ok(); + LAYOUT_THREAD_RUNNING.store(spawned, Ordering::Release); + } +} + +fn unwatch_console_size() { + bun_io::windows::tty::set_resize_listener(None); + + let _lock = LAYOUT_LOCK.lock_guard(); + LAYOUT_WANTED.store(false, Ordering::Release); + let thread = LAYOUT_THREAD_ID.load(Ordering::Acquire); + if thread != 0 + && let Some(Some(user32)) = USER32.get() + { + // Any message: it gets the thread out of `GetMessageW` to look at + // `LAYOUT_WANTED`. + // SAFETY: plain Win32 call. + unsafe { (user32.PostThreadMessageW)(thread, WM_NULL, 0, 0) }; + } +} + +const EVENT_CONSOLE_LAYOUT: u32 = 0x4005; +const WINEVENT_OUTOFCONTEXT: u32 = 0; +const WM_NULL: u32 = 0; +const WM_USER: u32 = 0x0400; +const PM_NOREMOVE: u32 = 0; +const PM_REMOVE: u32 = 1; +const LOAD_LIBRARY_SEARCH_SYSTEM32: win::DWORD = 0x0000_0800; +/// `PROCESSINFOCLASS`: the process id of the console host, with flags in the +/// low two bits. +const PROCESS_CONSOLE_HOST_PROCESS: u32 = 49; + +#[repr(C)] +#[allow(non_snake_case)] +struct MSG { + hwnd: *mut c_void, + message: u32, + wParam: usize, + lParam: isize, + time: u32, + pt: [i32; 2], +} + +type WinEventProc = extern "system" fn( + hook: *mut c_void, + event: u32, + hwnd: *mut c_void, + id_object: i32, + id_child: i32, + event_thread: u32, + event_time: u32, +); + +/// What [`layout_thread`] needs of user32.dll, looked up there so that +/// nothing else loads the library or needs it to be installed. +#[allow(non_snake_case)] +struct User32 { + SetWinEventHook: unsafe extern "system" fn( + eventMin: u32, + eventMax: u32, + hmodWinEventProc: *mut c_void, + pfnWinEventProc: WinEventProc, + idProcess: u32, + idThread: u32, + dwFlags: u32, + ) -> *mut c_void, + UnhookWinEvent: unsafe extern "system" fn(hWinEventHook: *mut c_void) -> win::BOOL, + PeekMessageW: unsafe extern "system" fn( + lpMsg: *mut MSG, + hWnd: *mut c_void, + wMsgFilterMin: u32, + wMsgFilterMax: u32, + wRemoveMsg: u32, + ) -> win::BOOL, + GetMessageW: unsafe extern "system" fn( + lpMsg: *mut MSG, + hWnd: *mut c_void, + wMsgFilterMin: u32, + wMsgFilterMax: u32, + ) -> win::BOOL, + PostThreadMessageW: unsafe extern "system" fn( + idThread: u32, + Msg: u32, + wParam: usize, + lParam: isize, + ) -> win::BOOL, +} + +static USER32: bun_core::Once> = bun_core::Once::new(); + +impl User32 { + fn load() -> Option { + // SAFETY: plain Win32 call; the name is NUL-terminated. + let module = unsafe { + win::kernel32::LoadLibraryExW( + bun_core::w!("user32.dll\0").as_ptr(), + core::ptr::null_mut(), + LOAD_LIBRARY_SEARCH_SYSTEM32, + ) + }; + if module.is_null() { + return None; + } + let find = |name: &core::ffi::CStr| { + // SAFETY: `module` is loaded and never freed; `name` is NUL-terminated. + let symbol = unsafe { win::GetProcAddress(module, name.as_ptr()) }; + (!symbol.is_null()).then_some(symbol) + }; + // SAFETY: each symbol is the user32 export of that name, whose + // signature is the field's type. + unsafe { + Some(User32 { + SetWinEventHook: core::mem::transmute::<*mut c_void, _>(find(c"SetWinEventHook")?), + UnhookWinEvent: core::mem::transmute::<*mut c_void, _>(find(c"UnhookWinEvent")?), + PeekMessageW: core::mem::transmute::<*mut c_void, _>(find(c"PeekMessageW")?), + GetMessageW: core::mem::transmute::<*mut c_void, _>(find(c"GetMessageW")?), + PostThreadMessageW: core::mem::transmute::<*mut c_void, _>(find( + c"PostThreadMessageW", + )?), + }) + } + } +} + +extern "system" fn on_console_layout( + _hook: *mut c_void, + _event: u32, + _hwnd: *mut c_void, + _id_object: i32, + _id_child: i32, + _event_thread: u32, + _event_time: u32, +) { + if LAYOUT_CHANGED.load(Ordering::Acquire) { + return; + } + // Scrolling output raises these a line at a time. `GetMessageW` runs this + // without returning, so the first event of a burst posts a message that + // makes it return; one look at the size then covers the whole burst. + // SAFETY: plain Win32 call. + let posted = USER32 + .get() + .and_then(Option::as_ref) + .is_some_and(|user32| unsafe { + (user32.PostThreadMessageW)(win::kernel32::GetCurrentThreadId(), WM_NULL, 0, 0) != 0 + }); + if posted { + LAYOUT_CHANGED.store(true, Ordering::Release); + } else { + raise_if_console_resized(); + } +} + +/// Hook the console host's layout events for this thread. `None` without +/// user32.dll, or when the system will not say which process hosts the console. +fn hook_console_layout() -> Option<(&'static User32, *mut c_void)> { + let user32 = USER32.get_or_init(User32::load).as_ref()?; + let mut console_host: usize = 0; + // SAFETY: `console_host` is a live local of the size passed. + let status = unsafe { + win::ntdll::NtQueryInformationProcess( + win::GetCurrentProcess(), + PROCESS_CONSOLE_HOST_PROCESS, + (&raw mut console_host).cast(), + size_of::() as u32, + core::ptr::null_mut(), + ) + }; + if (status.0 as i32) < 0 { + return None; + } + // SAFETY: plain Win32 calls; `message` is a live local. + unsafe { + // An out-of-context hook delivers through its thread's message queue, + // which the first call that looks at the queue creates. + let mut message: MSG = bun_core::ffi::zeroed_unchecked(); + (user32.PeekMessageW)( + &raw mut message, + core::ptr::null_mut(), + WM_USER, + WM_USER, + PM_NOREMOVE, + ); + let hook = (user32.SetWinEventHook)( + EVENT_CONSOLE_LAYOUT, + EVENT_CONSOLE_LAYOUT, + core::ptr::null_mut(), + on_console_layout, + (console_host & !3) as u32, + 0, + WINEVENT_OUTOFCONTEXT, + ); + (!hook.is_null()).then_some((user32, hook)) + } +} + +/// Lives while SIGWINCH has a listener. [`on_console_layout`] runs on it, from +/// inside `GetMessageW` and `PeekMessageW`. +fn layout_thread() { + let Some((user32, hook)) = hook_console_layout() else { + let _lock = LAYOUT_LOCK.lock_guard(); + LAYOUT_THREAD_RUNNING.store(false, Ordering::Release); + return; + }; + { + let _lock = LAYOUT_LOCK.lock_guard(); + LAYOUT_THREAD_ID.store(win::kernel32::GetCurrentThreadId(), Ordering::Release); + } + // A resize between the listener's look at the size and the hook. + raise_if_console_resized(); + let mut pumping = true; + loop { + { + let _lock = LAYOUT_LOCK.lock_guard(); + if !pumping || !LAYOUT_WANTED.load(Ordering::Acquire) { + LAYOUT_THREAD_ID.store(0, Ordering::Release); + LAYOUT_THREAD_RUNNING.store(false, Ordering::Release); + break; + } + } + // SAFETY: plain Win32 calls; `message` is a live local. + unsafe { + let mut message: MSG = bun_core::ffi::zeroed_unchecked(); + pumping = (user32.GetMessageW)(&raw mut message, core::ptr::null_mut(), 0, 0) != -1; + // Events that are already queued run their callback in here. + while (user32.PeekMessageW)(&raw mut message, core::ptr::null_mut(), 0, 0, PM_REMOVE) + != 0 + {} + } + if LAYOUT_CHANGED.swap(false, Ordering::AcqRel) { + raise_if_console_resized(); + } + } + // SAFETY: `hook` was set by this thread and is unhooked once. + unsafe { (user32.UnhookWinEvent)(hook) }; +} diff --git a/src/runtime/server/DirectoryRoute.rs b/src/runtime/server/DirectoryRoute.rs index 2b322ff2751b..d7c1264ef1a9 100644 --- a/src/runtime/server/DirectoryRoute.rs +++ b/src/runtime/server/DirectoryRoute.rs @@ -245,7 +245,6 @@ impl DirectoryRoute { resp, vm: bun_ptr::BackRef::new(server.vm()), file_type: FileType::File, - pollable: false, offset: body_offset, length: Some(body_len), idle_timeout: server.config().idle_timeout, @@ -293,24 +292,13 @@ impl DirectoryRoute { fn open_beneath(&self, rel: &[u8]) -> Option { let mut buf = bun_paths::path_buffer_pool::get(); let zrel = resolve_path::z(rel, &mut *buf); - // NONBLOCK so opening a FIFO without a writer cannot block the event - // loop on POSIX. Not on Windows: there `openat` maps it to omitting - // FILE_SYNCHRONOUS_IO_NONALERT, which breaks the synchronous reads - // FileResponseStream issues. - #[cfg(not(windows))] + // NONBLOCK so opening a FIFO without a writer cannot block the event loop. let flags = bun_sys::O::RDONLY | bun_sys::O::CLOEXEC | bun_sys::O::NONBLOCK; - #[cfg(windows)] - let flags = bun_sys::O::RDONLY | bun_sys::O::CLOEXEC; #[cfg(any(target_os = "linux", target_os = "android"))] let fd = bun_sys::openat2_in_root(self.root_fd.get(), zrel, flags, 0).ok()?; #[cfg(not(any(target_os = "linux", target_os = "android")))] let fd = bun_sys::openat(self.root_fd.get(), zrel, flags, 0).ok()?; - // Windows `openat` returns a HANDLE; `FileResponseStream` needs a - // libuv fd. `make_lib_uv_owned` is a no-op on POSIX. - use bun_sys::FdExt; - fd.make_lib_uv_owned_for_syscall(bun_sys::Tag::open, bun_sys::ErrorCase::CloseOnFail) - .ok() - .map(File::from_fd) + Some(File::from_fd(fd)) } fn stat_cache_lookup(&self, rel: &[u8], stat: &bun_sys::Stat) -> (u64, [u8; 32], usize) { diff --git a/src/runtime/server/FileResponseStream.rs b/src/runtime/server/FileResponseStream.rs index 6c092fcfb2c0..18b444b574cc 100644 --- a/src/runtime/server/FileResponseStream.rs +++ b/src/runtime/server/FileResponseStream.rs @@ -12,11 +12,9 @@ use core::cell::Cell; use core::ffi::c_void; use bun_io::Closer; -#[cfg(windows)] -use bun_io::pipe_reader::WindowsFlags as ReaderFlags; -use bun_io::{BufferedReader, FileType, ReadState}; #[cfg(unix)] -use bun_io::{FilePollFlag, PosixFlags as ReaderFlags}; +use bun_io::FilePollFlag; +use bun_io::{BufferedReader, FileType, ReadState, ReaderFlags}; use bun_jsc::JsCell; use bun_ptr::RefPtr; use bun_sys::{self as sys, Fd}; @@ -103,7 +101,6 @@ pub(crate) struct StartOptions { pub resp: AnyResponse, pub vm: bun_ptr::BackRef, pub file_type: FileType, - pub pollable: bool, /// Byte offset into the file to begin reading from. pub offset: u64, /// Maximum bytes to send; `None` reads to EOF. For regular files this @@ -223,13 +220,16 @@ impl FileResponseStream { return; } + let is_stream = opts.file_type != FileType::File; + // On Windows the reader's `is_pollable` means "an overlapped pipe end + // Bun created", which a served file's handle never is. + let pollable = cfg!(not(windows)) && is_stream; + // BufferedReader path this_ref.reader.with_mut(|reader| { reader.flags.remove(ReaderFlags::CLOSE_HANDLE); // we own fd via auto_close - reader.flags.set(ReaderFlags::POLLABLE, opts.pollable); - reader - .flags - .set(ReaderFlags::NONBLOCKING, opts.file_type != FileType::File); + reader.flags.set(ReaderFlags::POLLABLE, pollable); + reader.flags.set(ReaderFlags::NONBLOCKING, is_stream); #[cfg(unix)] if opts.file_type == FileType::Socket { reader.flags.insert(ReaderFlags::SOCKET); @@ -243,9 +243,9 @@ impl FileResponseStream { // the parent pointer (`loop_`/`event_loop`), so no cell borrow spans them. let reader = this_ref.reader_mut(); let start_result = if opts.offset > 0 { - reader.start_file_offset(opts.fd, opts.pollable, opts.offset as usize) + reader.start_file_offset(opts.fd, pollable, opts.offset as usize) } else { - reader.start(opts.fd, opts.pollable) + reader.start(opts.fd, pollable) }; if let Err(err) = start_result { this_ref.fail_with(err); @@ -600,24 +600,10 @@ impl FileResponseStream { fn event_loop(&self) -> EventLoopHandle { EventLoopHandle::init(self.vm.get().event_loop().cast::<()>()) } - - fn r#loop(&self) -> *mut bun_io::Loop { - #[cfg(windows)] - { - // SAFETY: `r#loop()` returns the live uws WindowsLoop; its `uv_loop` - // is set by C `us_create_loop` and valid for the loop's lifetime. - return unsafe { (*self.event_loop().r#loop()).uv_loop }; - } - #[cfg(not(windows))] - { - self.event_loop().r#loop() - } - } } // BufferedReader vtable parent. -// `loop_` delegates to the inherent `r#loop()` which already does the -// cfg(windows) `.uv_loop` projection. The read/done/error arms take a ref for +// The read/done/error arms take a ref for // the duration of the handler since it can end in `finish()`. bun_io::impl_buffered_reader_parent! { FileResponseStream for FileResponseStream; @@ -634,7 +620,7 @@ bun_io::impl_buffered_reader_parent! { let _guard = RefPtr::init_ref(this); (*this).on_reader_error(err) }; - loop_ = |this| (*this).r#loop(); + loop_ = |this| (*this).event_loop().r#loop(); event_loop = |this| (*this).event_loop_handle.get().as_event_loop_ctx(); // The reader still uses itself (embedded here) after dispatching the `on_reader_done` that releases the owning ref. ref_ = |this| (*this).ref_(); @@ -648,10 +634,7 @@ impl Drop for FileResponseStream { // field — closes the poll handle. `bun.destroy(this)` is owned by // `heap::take` in `deref`, not here. if self.auto_close.get() { - #[cfg(windows)] - Closer::close(self.fd.get(), bun_sys::windows::libuv::Loop::get()); - #[cfg(not(windows))] - Closer::close(self.fd.get(), ()); + Closer::close(self.fd.get()); } } } diff --git a/src/runtime/server/FileRoute.rs b/src/runtime/server/FileRoute.rs index 09dad42332e9..d303050d97d2 100644 --- a/src/runtime/server/FileRoute.rs +++ b/src/runtime/server/FileRoute.rs @@ -66,7 +66,6 @@ enum Serve { /// Hand the fd to a `FileResponseStream`. Stream { file_type: FileType, - pollable: bool, offset: u64, length: Option, }, @@ -257,25 +256,20 @@ impl FileRoute { let open_flags = bun_sys::O::RDONLY | bun_sys::O::CLOEXEC | bun_sys::O::NONBLOCK; - let fd_result: bun_sys::Result = { - #[cfg(windows)] - { - let mut path_buffer = bun_paths::path_buffer_pool::get(); - path_buffer[..path.len()].copy_from_slice(path); - path_buffer[path.len()] = 0; - bun_sys::open( - bun_core::ZStr::from_buf(&path_buffer[..], path.len()), - open_flags, - 0, - ) - } - #[cfg(not(windows))] - { - bun_sys::open_a(path, open_flags, 0) - } + let mut path_buf = bun_paths::path_buffer_pool::get(); + let opened = if path.len() < path_buf.len() { + bun_sys::open( + bun_paths::resolve_path::z(path, &mut path_buf), + open_flags, + 0, + ) + } else { + Err(bun_sys::Error::from_code( + bun_sys::E::ENAMETOOLONG, + bun_sys::Tag::open, + )) }; - - let Ok(fd) = fd_result else { + let Ok(fd) = opened else { req.set_yield(true); route.on_response_complete(resp); return; @@ -288,15 +282,11 @@ impl FileRoute { // which branch ran. match route.serve(fd, path, &mut req, resp, method) { Serve::Done => { - #[cfg(windows)] - Closer::close(fd, bun_sys::windows::libuv::Loop::get()); - #[cfg(not(windows))] - Closer::close(fd, ()); + Closer::close(fd); route.on_response_complete(resp); } Serve::Stream { file_type, - pollable, offset, length, } => { @@ -307,7 +297,6 @@ impl FileRoute { resp, vm: bun_ptr::BackRef::new(server.vm()), file_type, - pollable, offset, length, idle_timeout: server.config().idle_timeout, @@ -325,11 +314,11 @@ impl FileRoute { resp: AnyResponse, method: Method, ) -> Serve { - let (can_serve_file, offset, size, file_type, pollable) = 'brk: { + let (can_serve_file, offset, size, file_type) = 'brk: { let stat = match bun_sys::fstat(fd) { Ok(s) => s, // file_type is never read because can_serve_file == false - Err(_) => break 'brk (false, 0, 0, FileType::File, false), + Err(_) => break 'brk (false, 0, 0, FileType::File), }; let stat_size: u64 = u64::try_from(stat.st_size.max(0)).expect("int cast"); @@ -338,7 +327,7 @@ impl FileRoute { let mode = stat.st_mode as bun_sys::Mode; if bun_sys::S::ISDIR(mode) { - break 'brk (false, 0, 0, FileType::File, false); + break 'brk (false, 0, 0, FileType::File); } // `Cell::take` → mutate → `set`: single-threaded event loop, no @@ -348,14 +337,14 @@ impl FileRoute { self.stat_hash.set(sh); if bun_sys::S::ISFIFO(mode) || bun_sys::S::ISCHR(mode) { - break 'brk (true, offset, size, FileType::Pipe, true); + break 'brk (true, offset, size, FileType::Pipe); } if bun_sys::S::ISSOCK(mode) { - break 'brk (true, offset, size, FileType::Socket, true); + break 'brk (true, offset, size, FileType::Socket); } - break 'brk (true, offset, size, FileType::File, false); + break 'brk (true, offset, size, FileType::File); }; if !can_serve_file { @@ -454,7 +443,6 @@ impl FileRoute { Serve::Stream { file_type, - pollable, offset: body_offset, length: body_len, } diff --git a/src/runtime/server/RequestContext.rs b/src/runtime/server/RequestContext.rs index 813b8502a282..8e23d0d231de 100644 --- a/src/runtime/server/RequestContext.rs +++ b/src/runtime/server/RequestContext.rs @@ -1852,12 +1852,12 @@ where let mode = stat.st_mode as bun_sys::Mode; let is_regular = bun_sys::S::ISREG(mode); - let (file_type, pollable): (bun_io::FileType, bool) = 'brk: { + let file_type: bun_io::FileType = 'brk: { if bun_sys::S::ISFIFO(mode) || bun_sys::S::ISCHR(mode) { - break 'brk (bun_io::FileType::Pipe, true); + break 'brk bun_io::FileType::Pipe; } if bun_sys::S::ISSOCK(mode) { - break 'brk (bun_io::FileType::Socket, true); + break 'brk bun_io::FileType::Socket; } if bun_sys::S::ISDIR(mode) { if auto_close { @@ -1881,7 +1881,7 @@ where sys.message = BunString::static_("Cannot stream a directory as a response body"); return self.run_error_handler(sys.to_error_instance(global_this)); } - (bun_io::FileType::File, false) + bun_io::FileType::File }; let (original_size, blob_offset) = match blob_ref { @@ -2025,7 +2025,6 @@ where resp, vm: bun_ptr::BackRef::new(server.vm()), file_type, - pollable, offset: sendfile.offset as u64, length: if is_regular { Some(sendfile.remain as u64) diff --git a/src/runtime/server/mod.rs b/src/runtime/server/mod.rs index 7284a0a2b9c7..67e92fc30d9d 100644 --- a/src/runtime/server/mod.rs +++ b/src/runtime/server/mod.rs @@ -1777,8 +1777,8 @@ impl NewServer { self.unref(); } // A graceful stop with work in flight keeps the ref (deinit_if_we_can - // unrefs when the drain completes): on Windows uv_run skips I/O with - // zero ref'd handles, so unrefing here wedged server.close() teardown. + // unrefs when the drain completes): a loop with nothing ref'd does not + // wait for I/O, so unrefing here would wedge server.close() teardown. if !SSL { // SAFETY: `listener` is a live uws ListenSocket FFI handle just taken diff --git a/src/runtime/shell/Builtin.rs b/src/runtime/shell/Builtin.rs index 24e6dfb90ca4..3bc438212850 100644 --- a/src/runtime/shell/Builtin.rs +++ b/src/runtime/shell/Builtin.rs @@ -388,6 +388,19 @@ impl BuiltinIO { } } + /// [`enqueue`](Self::enqueue) for bytes the caller is done with. + pub(crate) fn enqueue_owned( + &mut self, + child: io_writer::ChildPtr, + buf: Vec, + _safeguard: OutputNeedsIOSafeGuard, + ) -> Yield { + match self { + BuiltinIO::Fd(fd) => fd.writer.enqueue_owned(child, fd.captured, buf), + _ => unreachable!("enqueue_owned() on non-fd output; caller must check needs_io()"), + } + } + /// Format with the optional `"{kind}: "` prefix and enqueue on the /// underlying IOWriter. pub(crate) fn enqueue_fmt( @@ -412,11 +425,6 @@ impl BuiltinInput { InKind::Ignore => BuiltinInput::Ignore, } } - - #[inline] - pub(crate) fn needs_io(&self) -> bool { - matches!(self, BuiltinInput::Fd(_)) - } } impl Builtin { @@ -429,7 +437,6 @@ impl Builtin { /// impl the heap sweep already deleted; see /// `ShellSubprocess::defuse_array_buffer_unpins`. VM-shutdown finalizer /// only. - #[cfg(not(windows))] pub(crate) fn defuse_array_buf_pins(&mut self) { if let BuiltinInput::ArrayBuf { buf, .. } = &mut self.stdin { buf.defuse(); @@ -553,7 +560,6 @@ impl Builtin { let path = bun_core::ZStr::from_slice_with_nul(&path_buf[..]); let perm: bun_sys::Mode = 0o666; let cwd_fd = Self::cwd(interp, cmd); - let evtloop = interp.event_loop; let mut pollable = false; let mut is_socket = false; @@ -603,62 +609,28 @@ impl Builtin { ), )); } - Ok(f) => { - #[cfg(windows)] - { - use bun_sys::FdExt as _; - match f.make_lib_uv_owned_for_syscall( - bun_sys::Tag::open, - bun_sys::ErrorCase::CloseOnFail, - ) { - Err(e) => { - let sys = e.to_shell_system_error(); - return Some(Self::cmd_write_failing_error( - interp, - cmd, - format_args!( - "bun: {}: {}", - bstr::BStr::new(sys.message.byte_slice()), - bstr::BStr::new(path.as_bytes()), - ), - )); - } - Ok(f2) => f2, - } - } - #[cfg(not(windows))] - { - f - } - } + Ok(f) => f, } }; - let interp_ptr: *mut Interpreter = interp.as_ctx_ptr(); if redirect.stdin() { - let r = IOReader::init(redirfd, evtloop); - r.set_interp(interp_ptr); - Self::of_mut(interp, cmd).stdin = BuiltinInput::Fd(r); + Self::of_mut(interp, cmd).stdin = + BuiltinInput::Fd(IOReader::init_opened(redirfd, interp)); } if !redirect.stdout() && !redirect.stderr() { return None; } + // `open_for_writing_impl` classifies the fd on POSIX only. + #[cfg(windows)] + let flags = io_writer::Flags::unclassified(); // Honor the `pollable` computed by `open_for_writing_impl` on // POSIX so a FIFO/socket target (whose fd is now O_NONBLOCK) - // takes the pollable path; Windows keeps the async writer. - let redirect_writer = IOWriter::init( - redirfd, - io_writer::Flags { - pollable: if cfg!(windows) { true } else { pollable }, - nonblock: is_nonblocking, - is_socket, - ..Default::default() - }, - evtloop, - ); - redirect_writer.set_interp(interp_ptr); + // takes the pollable path. + #[cfg(not(windows))] + let flags = io_writer::Flags::classified(pollable, is_nonblocking, is_socket); + let redirect_writer = IOWriter::init(redirfd, flags, interp); if redirect.stdout() { let me = Self::of_mut(interp, cmd); @@ -816,7 +788,7 @@ impl Builtin { let child = io_writer::ChildPtr::new(cmd, io_writer::WriterTag::Cmd); // SAFETY: `OutKind::Fd` guaranteed by `needs_io()`. if let OutKind::Fd(fd) = &interp.as_cmd(cmd).io.stderr { - return fd.writer.enqueue(child, fd.captured, &buf); + return fd.writer.enqueue_owned(child, fd.captured, buf); } unreachable!() } @@ -842,6 +814,23 @@ impl Builtin { pub(crate) fn done(interp: &Interpreter, cmd: NodeId, exit_code: ExitCode) -> Yield { // Output is written through immediately in `write_no_io`, so there // is nothing to flush here. + // + // A queued chunk calls back into this Cmd by `NodeId`, which is free + // for reuse after this. + #[cfg(debug_assertions)] + { + let child = io_writer::ChildPtr::new(cmd, io_writer::WriterTag::Builtin); + let me = Self::of(interp, cmd); + for out in [&me.stdout, &me.stderr] { + if let BuiltinIO::Fd(fd) = out { + debug_assert!( + !fd.writer.has_live_chunks(child), + "builtin {} finished with a chunk still queued", + me.kind.as_str(), + ); + } + } + } Cmd::on_exec_done(interp, cmd, exit_code) } @@ -1077,12 +1066,10 @@ impl Builtin { ) -> Yield { if let Some(safeguard) = Self::of(interp, cmd).stderr.needs_io() { let child = io_writer::ChildPtr::new(cmd, io_writer::WriterTag::Builtin); - // Clone buf so the &mut on - // `stderr` doesn't overlap a borrow into `err_buf`. - let owned = buf.to_vec(); + // `buf` may borrow `err_buf`, next to the `stderr` borrowed here. return Self::of_mut(interp, cmd) .stderr - .enqueue(child, &owned, safeguard); + .enqueue_owned(child, buf.to_vec(), safeguard); } let _ = Self::write_no_io(interp, cmd, IoKind::Stderr, buf); Self::done(interp, cmd, exit_code) diff --git a/src/runtime/shell/IO.rs b/src/runtime/shell/IO.rs index 6ad8ac242ee3..faccfda16c90 100644 --- a/src/runtime/shell/IO.rs +++ b/src/runtime/shell/IO.rs @@ -148,18 +148,7 @@ impl OutKind { buf: cap, }) } else { - // `IOWriter::fd()` (IOWriter.rs) returns `Fd::INVALID` - // once the fd has been handed off to libuv, so the - // sentinel compare here checks for that hand-off state. - let fd = val.writer.fd(); - if fd != bun_sys::Fd::INVALID { - Stdio::Fd(fd) - } else { - // Windows: fd was moved to libuv → inherit (libuv - // already manages it). On POSIX `IOWriter::fd()` is - // always the live fd, so this branch is unreachable. - Stdio::Inherit - } + Stdio::Fd(val.writer.fd()) } } OutKind::Pipe => Stdio::Pipe, diff --git a/src/runtime/shell/IOReader.rs b/src/runtime/shell/IOReader.rs index 2ce9560ad8eb..af28b80474e8 100644 --- a/src/runtime/shell/IOReader.rs +++ b/src/runtime/shell/IOReader.rs @@ -15,9 +15,8 @@ use crate::shell::yield_::Yield; // ChildPtr // ────────────────────────────────────────────────────────────────────────── -/// In the NodeId-arena port, listeners are identified by `(NodeId, ReaderTag)` -/// — the node id of the owning Cmd plus a tag saying which builtin impl to -/// dispatch the `on_read_chunk`/`on_reader_done` callback to. +/// A listener: the node id of the owning Cmd plus a tag saying which builtin +/// impl to dispatch the `on_read_chunk`/`on_reader_done` callback to. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) struct ChildPtr { pub node: NodeId, @@ -39,25 +38,39 @@ type Readers = Vec; pub(crate) type ReaderImpl = bun_io::BufferedReader; +/// What whoever makes an `IOReader` knows about its fd. +#[derive(Clone, Copy)] +enum Made { + Elsewhere, + ForThisReader, + SynchronousPipe, + #[cfg(windows)] + OverlappedPipe, +} + struct State { fd: Fd, - buf: Vec, + /// What `fd` is, as far as whoever made this reader knows. + #[cfg(windows)] + origin: bun_io::windows::PipeOrigin, + /// [`IOReader::init_opened`]. + #[cfg(windows)] + fd_is_private: bool, readers: Readers, - /// The raw `sys::Error`. `SystemError` is not `Clone` - /// in the Rust port yet, so we keep the source error to re-derive a fresh - /// `SystemError` per callee in `on_reader_done_cb`. + /// What the reader has failed with since it was last started; an + /// `on_reader_done` after it carries it. raw_err: Option, evtloop: EventLoopHandle, - #[cfg(windows)] - is_reading: bool, /// Weak self-ref so `keepalive()` can bump the strong count from `&self` /// without unsafe Arc-pointer reconstruction. Set via `Arc::new_cyclic` in /// `init()` (the sole constructor). self_weak: std::sync::Weak, read_guards: Vec>, - /// Backref so async read callbacks can drive `Yield::run`. See - /// `IOWriter::interp`. - interp: Option>, + /// The interpreter whose nodes `readers` names. It outlives this reader: + /// every `Arc` is held by its `root_io`, by one of its nodes, or + /// on the stack of a callback running under those, and nothing the reader + /// has in flight holds one. + interp: bun_ptr::ParentRef, } pub(crate) struct IOReader { @@ -119,35 +132,60 @@ impl IOReader { self.state().read_guards.pop() } - pub(crate) fn init(fd: Fd, evtloop: EventLoopHandle) -> std::sync::Arc { - let mut reader = ReaderImpl::init::(); + /// A reader of `fd` (closed with it) whose listeners are nodes of `interp`. + pub(crate) fn init(fd: Fd, interp: &Interpreter) -> std::sync::Arc { + Self::new(fd, interp, Made::Elsewhere) + } + + /// [`init`](Self::init) for an `fd` opened for this reader: nothing else + /// asks for it, and it is read to its end once. + pub(crate) fn init_opened(fd: Fd, interp: &Interpreter) -> std::sync::Arc { + Self::new(fd, interp, Made::ForThisReader) + } + + /// [`init`](Self::init) for the read end of a `bun_sys::pipe()`. + pub(crate) fn init_created_pipe(fd: Fd, interp: &Interpreter) -> std::sync::Arc { + Self::new(fd, interp, Made::SynchronousPipe) + } + + /// [`init`](Self::init) for an overlapped pipe end the shell created, which + /// nothing else has opened on this loop. + #[cfg(windows)] + pub(crate) fn init_overlapped_pipe(fd: Fd, interp: &Interpreter) -> std::sync::Arc { + Self::new(fd, interp, Made::OverlappedPipe) + } + + fn new(fd: Fd, interp: &Interpreter, made: Made) -> std::sync::Arc { #[cfg(not(windows))] - { - reader - .flags - .remove(bun_io::pipe_reader::PosixFlags::CLOSE_HANDLE); - } - #[cfg(windows)] - { - reader.set_source(bun_io::Source::File(bun_io::Source::open_file(fd))); - } + let _ = made; let this = std::sync::Arc::new_cyclic(|w| IOReader { - reader: UnsafeCell::new(reader), + reader: UnsafeCell::new(ReaderImpl::init::()), state: UnsafeCell::new(State { fd, - buf: Vec::new(), + #[cfg(windows)] + origin: match made { + Made::Elsewhere | Made::ForThisReader => bun_io::windows::PipeOrigin::Foreign, + Made::SynchronousPipe => bun_io::windows::PipeOrigin::CreatedSynchronous, + Made::OverlappedPipe => bun_io::windows::PipeOrigin::Created, + }, + #[cfg(windows)] + fd_is_private: matches!(made, Made::ForThisReader), readers: Readers::new(), raw_err: None, - evtloop, - #[cfg(windows)] - is_reading: false, + evtloop: interp.event_loop, self_weak: std::sync::Weak::clone(w), read_guards: Vec::new(), - interp: None, + interp: bun_ptr::ParentRef::new(interp), }), }); - // NOTE: set the parent backref after Arc allocation so the - // address is stable. + // `fd` stays this IOReader's to close. On Windows the reader closes + // what it reads from: a HANDLE of its own (see `start_reader`). + #[cfg(not(windows))] + this.reader() + .flags + .remove(bun_io::pipe_reader::ReaderFlags::CLOSE_HANDLE); + // The parent backref is set after the Arc allocation so the address + // is stable. let parent: *const IOReader = std::sync::Arc::as_ptr(&this); // SAFETY: `Arc::as_ptr` yields `*const IOReader`, but every field of // `IOReader` is `UnsafeCell`, so all mutation flows through interior @@ -160,16 +198,6 @@ impl IOReader { this } - /// # Safety - /// `interp` must be null or point to the live owning `Interpreter` (it - /// owns the IO struct that holds this `Arc`) for the lifetime of this - /// reader; single-threaded. - #[inline] - pub(crate) fn set_interp(&self, interp: *mut Interpreter) { - // SAFETY: precondition above. - self.state().interp = unsafe { bun_ptr::ParentRef::from_nullable(interp) }; - } - #[inline] pub(crate) fn fd(&self) -> Fd { self.state().fd @@ -177,62 +205,93 @@ impl IOReader { pub(crate) fn memory_cost(&self) -> usize { let s = self.state(); - core::mem::size_of::() - + s.buf.capacity() - + s.readers.capacity() * core::mem::size_of::() + core::mem::size_of::() + s.readers.capacity() * core::mem::size_of::() } - /// `bun_io::EventLoopHandle` is an opaque `*mut c_void` that the io-layer - /// `FilePollVTable` round-trips back to the runtime. We pass the address of - /// the stored `bun_event_loop::EventLoopHandle` so the (runtime-registered) - /// vtable can recover it. #[inline] fn io_evtloop(&self) -> bun_io::EventLoopHandle { - // SAFETY: `bun_io::EventLoopHandle` stores `*mut c_void` purely for - // type-erasure; vtable consumers treat the pointee as read-only self.state().evtloop.as_event_loop_ctx() } - /// Only does things on windows. - #[inline] - fn set_reading(&self, reading: bool) { - #[cfg(windows)] - { - self.state().is_reading = reading; - } - let _ = reading; - } - - /// Idempotent function to start the reading. + /// Idempotent function to start the reading. A reader that has reported + /// EOF or an error reads again, for the listeners added since. pub(crate) fn start(&self) -> Yield { + let r = self.reader(); + // The poll is one-shot: once it has delivered EOF nothing arms it again, + // and it still counts as registered. #[cfg(not(windows))] - { - let r = self.reader(); - let need_start = match &r.handle { - bun_io::pipes::PollOrFd::Closed => true, - bun_io::pipes::PollOrFd::Poll(p) => !p.is_registered(), - bun_io::pipes::PollOrFd::Fd(_) => true, - }; - if need_start { - let fd = self.state().fd; - if let Err(e) = r.start(fd, true) { - self.on_reader_error(&e); - } - } - return Yield::suspended(); - } + let need_start = match &r.handle { + bun_io::pipes::PollOrFd::Closed => true, + bun_io::pipes::PollOrFd::Poll(p) => !p.is_watching(), + bun_io::pipes::PollOrFd::Fd(_) => true, + }; + // A source exists from the first read until EOF; after an error it is + // still there, finished. #[cfg(windows)] - { + let need_start = + r.source.is_none() || r.flags.contains(bun_io::pipe_reader::ReaderFlags::IS_DONE); + if need_start { let s = self.state(); - if s.is_reading { - return Yield::suspended(); - } - s.is_reading = true; - if let Err(e) = self.reader().start_with_current_pipe() { + s.raw_err = None; + #[cfg(not(windows))] + let started = Self::start_reader(r, s.fd); + #[cfg(windows)] + let started = Self::start_reader(r, s); + if let Err(e) = started { self.on_reader_error(&e); } - Yield::suspended() } + Yield::suspended() + } + + #[cfg(not(windows))] + fn start_reader(r: &mut ReaderImpl, fd: Fd) -> sys::Result<()> { + // A listener that came after an error reads the fd again. + r.flags + .remove(bun_io::pipe_reader::ReaderFlags::READ_FAILED); + r.start(fd, true) + } + + /// A Windows source releases its HANDLE only once the loop has collected + /// its last operation, which can be after this `IOReader` (and `fd`) is + /// gone, so it reads through a HANDLE of its own: the same file object, + /// so what `origin` says of `fd` holds for it. A private `fd` becomes that + /// HANDLE, and is closed when the source has reached its end: a listener + /// that hears of the end hears it of a file that is no longer open. + #[cfg(windows)] + fn start_reader(r: &mut ReaderImpl, s: &mut State) -> sys::Result<()> { + use bun_io::windows::PipeOrigin; + use bun_sys::FdExt as _; + let origin = &mut s.origin; + // Lets go of a source that ended with an error. + r.deinit(); + let own = if s.fd_is_private { + core::mem::replace(&mut s.fd, Fd::INVALID) + } else { + sys::dup(s.fd)? + }; + let source = match r.open_source(own, *origin) { + Ok(source) => source, + Err(e) => { + own.close(); + return Err(e); + } + }; + // The port has the file object from here on, whether or not reading + // starts; a later start reads through another duplicate of it. + if *origin == PipeOrigin::Created { + *origin = PipeOrigin::Associated; + } + let started = r.start_with_source(source); + if started.is_err() { + own.close(); + } + // A builtin reads bytes until they end; at a console the user ends + // them with Ctrl-Z, as for any other program that reads it so. + if let Some(bun_io::Source::Tty(tty)) = &mut r.source { + tty.end_input_at_ctrl_z(); + } + started } /// Only adds if not already present. @@ -251,102 +310,87 @@ impl IOReader { } } - /// The `BufferedReader.onReadChunk` hook. - fn on_read_chunk_cb(&self, chunk: &[u8], has_more: bun_io::ReadState) -> bool { + /// The `BufferedReader.onReadChunk` hook. The last listener gets `chunk` + /// itself, to take the bytes if it wants them; the ones before it a view. + fn on_read_chunk_cb( + &self, + chunk: bun_io::pipes::Chunk<'_>, + has_more: bun_io::ReadState, + ) -> bool { // `dispatch_read_chunk` → `Cat::on_io_reader_chunk` may drop the last - // external Arc; hold one across the whole body so the trailing - // `state()` accesses (and `run_yield`'s re-read of `interp`) see live - // memory. + // external Arc; hold one across the whole body. let _keepalive = self.keepalive(); - self.set_reading(false); - // NOTE: reshaped for borrowck — `dispatch_read_chunk`/`run_yield` - // both re-derive `state()` (and the interpreter callback may re-enter - // `add_reader`/`remove_reader`), so we must NOT hold a long-lived - // `&mut State` across the dispatch. Re-derive `state()` per access - // instead. + let interp = self.state().interp; + // No `&mut State` is held across a dispatch: the callee may re-enter + // `add_reader`/`remove_reader`. + let mut remaining = self.state().readers.len(); + let mut chunk = Some(chunk); let mut i = 0usize; - while i < self.state().readers.len() { - let r = self.state().readers[i]; - let interp = self.state().interp; + while remaining > 0 { + remaining -= 1; + let Some(&r) = self.state().readers.get(i) else { + break; + }; + let piece = if remaining == 0 { + chunk.take() + } else { + chunk.as_deref().map(bun_io::pipes::Chunk::Scratch) + }; + let Some(piece) = piece else { break }; let mut remove = false; - self.run_yield(dispatch_read_chunk(r, chunk, &mut remove, interp)); + self.run_yield(dispatch_read_chunk(r, piece, &mut remove, &interp)); + let readers = &mut self.state().readers; + if readers.get(i) != Some(&r) { + // It took itself off the list. + continue; + } if remove { - self.state().readers.swap_remove(i); + readers.swap_remove(i); } else { i += 1; } } - let should_continue = has_more != bun_io::ReadState::Eof; - if should_continue && !self.state().readers.is_empty() { - self.set_reading(true); - // NOTE: no explicit re-arm (`registerPoll()` on posix / - // `startWithCurrentPipe()` on windows) here: that would re-derive - // a second `&mut ReaderImpl` while the bun_io read loop still - // holds one on its stack (PipeReader.rs aliasing contract) — - // Stacked-Borrows UB. - // On posix the re-arm is redundant: the read loop re-registers - // itself after the callback returns based on the `bool` we return - // (PipeReader.rs:731/755/846/920/986). On Windows the re-arm is - // also handled by the caller (`on_file_read`'s defer block / - // `uv_read_start` for streams) — but `startWithCurrentPipe()` had - // a SECOND load-bearing side effect: `buffer().clearRetainingCapacity()`, - // which keeps `WindowsBufferedReader._buffer` bounded between - // chunks. That clear is now performed by - // `WindowsBufferedReader::on_read` after the streaming chunk is - // consumed, so we still do nothing here. - } - should_continue + // No explicit re-arm here: that would re-derive a second + // `&mut ReaderImpl` while the bun_io read loop still holds one on its + // stack (PipeReader.rs aliasing contract). The read loop continues by + // itself after the callback returns. + has_more != bun_io::ReadState::Eof } fn on_reader_error(&self, err: &sys::Error) { // `dispatch_reader_done` may drop the last external Arc; keep `self` // alive across the loop. let _keepalive = self.keepalive(); - self.set_reading(false); let s = self.state(); s.raw_err = Some(err.clone()); - // NOTE: reshaped for borrowck — copy out before dispatching. + // Copied out: a callee may add or remove listeners. let readers: Vec = s.readers.clone(); let interp = s.interp; for r in readers { - // Re-derive a fresh SystemError per callee (see - // IOWriter.on_error note). - let ee = err.to_shell_system_error(); - self.run_yield(dispatch_reader_done(r, Some(ee), interp)); + self.run_yield(dispatch_reader_done(r, Some(err), &interp)); } } fn on_reader_done_cb(&self) { // `dispatch_reader_done` → `Cat::on_io_reader_done` drops Cat's - // `Arc`; if that was the last external ref, `self` is freed - // mid-loop and `run_yield`'s `state().interp` reads 0xdfdf poison. - // Hold a strong ref across the body. + // `Arc`, which may be the last external one: hold a strong + // ref across the body. let _keepalive = self.keepalive(); - self.set_reading(false); let s = self.state(); let readers: Vec = s.readers.clone(); let interp = s.interp; - // `SystemError` isn't `Clone` yet, so we keep the source `sys::Error` - // (which IS `Clone`) and re-derive a fresh `SystemError` per callee — - // same approach as `on_reader_error`. let raw_err = s.raw_err.clone(); for r in readers { - let ee = raw_err.as_ref().map(|e| e.to_shell_system_error()); - self.run_yield(dispatch_reader_done(r, ee, interp)); + self.run_yield(dispatch_reader_done(r, raw_err.as_ref(), &interp)); } } fn run_yield(&self, y: Yield) { - let Some(interp) = self.state().interp else { - debug_assert!( - matches!(y, Yield::Done | Yield::Suspended), - "IOReader async callback fired without interp backref" - ); + if matches!(y, Yield::Done | Yield::Suspended) { return; - }; - // `ParentRef: Deref` — the interpreter owns the IO - // struct holding this Arc and outlives every IOReader. Single-threaded. + } + let interp = self.state().interp; y.run(&interp); } } @@ -363,10 +407,10 @@ impl IOReader { bun_io::impl_buffered_reader_parent! { ShellIoReader for IOReader; has_on_read_chunk = true; - on_read_chunk = |this, chunk, has_more| (*this).on_read_chunk_cb(&chunk, has_more); + on_read_chunk = |this, chunk, has_more| (*this).on_read_chunk_cb(chunk, has_more); on_reader_done = |this| (*this).on_reader_done_cb(); on_reader_error = |this, err| (*this).on_reader_error(&err); - loop_ = |this| (*this).io_evtloop().native_loop(); + loop_ = |this| (*this).io_evtloop().loop_(); event_loop = |this| (*this).io_evtloop(); ref_ = |this| (*this).push_read_guard(); deref = |this| drop((*this).pop_read_guard()); @@ -383,24 +427,15 @@ impl Drop for IOReader { // drops while BufferedReader is still iterating. let s = self.state.get_mut(); let r = self.reader.get_mut(); - if s.fd != Fd::INVALID { - #[cfg(windows)] - { - // windows reader closes the file descriptor - if r.source.is_some() && !r.source.as_ref().is_some_and(|src| src.is_closed()) { - r.close_impl::(); - } - } - #[cfg(not(windows))] - { - // We cleared CLOSE_HANDLE in init(), so reader Drop will not - // return the FilePoll to its pool. Do it explicitly (without - // closing the fd — we own that and close it ourselves below). - if matches!(r.handle, bun_io::pipes::PollOrFd::Poll(_)) { - r.handle.close_impl(None, None::, false); - } - let _ = sys::close(s.fd); - } + // Without `CLOSE_HANDLE` (see `init`) the reader's `Drop` does not + // return the FilePoll to its pool; do that here, leaving the fd open + // for the `close` below. + #[cfg(not(windows))] + if matches!(r.handle, bun_io::pipes::PollOrFd::Poll(_)) { + r.handle.close_impl(None, None::, false); + } + if s.fd.is_valid() { + let _ = sys::close(s.fd); } r.disable_keeping_process_alive(()); // `reader` Drop handles its own deinit. @@ -408,20 +443,15 @@ impl Drop for IOReader { } // ────────────────────────────────────────────────────────────────────────── -// Hoisted dispatch (NodeId-arena port of `IOReaderChildPtr.onReadChunk` / -// `.onReaderDone`) +// Dispatch to a listener by tag // ────────────────────────────────────────────────────────────────────────── fn dispatch_read_chunk( child: ChildPtr, - chunk: &[u8], + chunk: bun_io::pipes::Chunk<'_>, remove: &mut bool, - interp: Option>, + interp: &Interpreter, ) -> Yield { - let Some(interp) = interp else { - return Yield::suspended(); - }; - let interp = interp.get(); match child.tag { ReaderTag::Cat => { crate::shell::builtins::cat::Cat::on_io_reader_chunk(interp, child.node, chunk, remove) @@ -429,15 +459,7 @@ fn dispatch_read_chunk( } } -fn dispatch_reader_done( - child: ChildPtr, - err: Option, - interp: Option>, -) -> Yield { - let Some(interp) = interp else { - return Yield::suspended(); - }; - let interp = interp.get(); +fn dispatch_reader_done(child: ChildPtr, err: Option<&sys::Error>, interp: &Interpreter) -> Yield { match child.tag { ReaderTag::Cat => { crate::shell::builtins::cat::Cat::on_io_reader_done(interp, child.node, err) diff --git a/src/runtime/shell/IOWriter.rs b/src/runtime/shell/IOWriter.rs index 199f617f2afb..254429e5882c 100644 --- a/src/runtime/shell/IOWriter.rs +++ b/src/runtime/shell/IOWriter.rs @@ -9,16 +9,15 @@ //! //! So `IOWriter` is essentially a writer queue to a file descriptor. //! -//! We also make `IOWriter` reference counted (via `Arc` in the Rust port), -//! this simplifies management of the file descriptor. +//! `IOWriter` is reference counted (`Arc`), which simplifies management of the +//! file descriptor. use bun_collections::VecExt; use core::cell::UnsafeCell; #[cfg(not(windows))] use core::ffi::c_void; +use std::collections::VecDeque; -#[cfg(windows)] -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; use bun_sys::{self as sys, E, Fd}; use crate::shell::interpreter::{EventLoopHandle, Interpreter, NodeId}; @@ -96,29 +95,110 @@ pub(crate) enum WriterTag { // Flags / Writer queue entry // ────────────────────────────────────────────────────────────────────────── -#[derive(Clone, Copy, Default)] +#[derive(Clone, Copy)] pub(crate) struct Flags { + /// Whether what the fd is has been asked; the first chunk asks + /// (`IOWriter::classify`). `pollable` means nothing before that. + classified: bool, + /// A write may wait for whoever is at the other end (a pipe, a socket, a + /// terminal), so it goes through the event loop. A write to anything else + /// is made in place: a disk file, and on POSIX a device such as + /// `/dev/null` too. pub(crate) pollable: bool, + #[cfg(not(windows))] pub(crate) nonblock: bool, + #[cfg(not(windows))] pub(crate) is_socket: bool, + /// Where a pipe came from, for the source that writes to it. + #[cfg(windows)] + pub(crate) origin: bun_io::windows::PipeOrigin, + /// What `classify` found the fd to be, for the source that writes to it. + #[cfg(windows)] + kind: Option, pub(crate) broken_pipe: bool, } -/// One queued chunk: which child enqueued it, how many bytes (in `buf`), how -/// many of those have been written so far, and an optional `Vec` to tee -/// into. +impl Flags { + /// An fd nobody has asked anything about: a duplicate of the process's + /// stdout or stderr, a redirect target on Windows. + pub(crate) fn unclassified() -> Flags { + Flags { + classified: false, + pollable: false, + #[cfg(not(windows))] + nonblock: false, + #[cfg(not(windows))] + is_socket: false, + #[cfg(windows)] + origin: bun_io::windows::PipeOrigin::Foreign, + #[cfg(windows)] + kind: None, + broken_pipe: false, + } + } + + /// A redirect target, which opening it has classified. + #[cfg(not(windows))] + pub(crate) fn classified(pollable: bool, nonblock: bool, is_socket: bool) -> Flags { + Flags { + classified: true, + pollable, + nonblock, + is_socket, + broken_pipe: false, + } + } + + /// The write end of a pipe between two commands whose writing command may + /// turn out to be a child process, which inherits it: a socketpair end on + /// POSIX, a synchronous pipe end on Windows. + pub(crate) fn pipe() -> Flags { + Flags { + classified: true, + pollable: true, + #[cfg(not(windows))] + nonblock: false, + #[cfg(not(windows))] + is_socket: true, + #[cfg(windows)] + origin: bun_io::windows::PipeOrigin::CreatedSynchronous, + #[cfg(windows)] + kind: None, + broken_pipe: false, + } + } + + /// The write end of a pipe between two commands that only a builtin + /// writes to: an overlapped end, which runs on the loop's port. + #[cfg(windows)] + pub(crate) fn overlapped_pipe() -> Flags { + Flags { + classified: true, + pollable: true, + origin: bun_io::windows::PipeOrigin::Created, + kind: None, + broken_pipe: false, + } + } +} + +/// One queued chunk: which child enqueued it, its bytes, how many of those +/// have been written so far, and an optional `Vec` to tee into. struct Writer { ptr: ChildPtr, - len: usize, + data: Vec, written: usize, bytelist: Option<*mut Vec>, } impl Writer { - #[cfg(not(windows))] + #[inline] + fn remaining(&self) -> &[u8] { + &self.data[self.written..] + } #[inline] fn wrote_everything(&self) -> bool { - self.written >= self.len + self.written >= self.data.len() } #[inline] fn is_dead(&self) -> bool { @@ -128,27 +208,25 @@ impl Writer { fn set_dead(&mut self) { self.ptr = ChildPtr::NULL; } - /// Tee `chunk` into the optional capture buffer. + /// The next `amount` bytes were written: tee them into the optional + /// capture buffer and count them. /// /// `bytelist` (when set) points into a live `ShellExecEnv` `Bufio` - /// (`OutFd::captured` — see its doc); the env outlives every queued - /// `Writer`. Localises the per-callsite raw deref in - /// `do_file_write` / `on_write_pollable`. + /// (`OutFd::captured` — see its doc); the env outlives every live queued + /// `Writer` (`IOWriter::orphan` kills the ones it does not). #[inline] - fn tee(&self, chunk: &[u8]) { + fn advance(&mut self, amount: usize) { if let Some(bl) = self.bytelist { // SAFETY: see doc comment. - let _ = unsafe { (*bl).append_slice(chunk) }; + let _ = unsafe { (*bl).append_slice(&self.data[self.written..self.written + amount]) }; } + self.written += amount; } } -// PERF: an inline small-vec may be worth it — profile if hot; smallvec crate. -type Writers = Vec; - -/// ~128kb. We shrink `buf` when we reach the last writer, but if that never -/// happens we shrink when it exceeds this threshold. -const SHRINK_THRESHOLD: usize = 1024 * 128; +/// The front is the chunk being written. A chunk leaves the queue when it is +/// done, so its bytes never move while the kernel may be reading them. +type Writers = VecDeque; // ────────────────────────────────────────────────────────────────────────── // IOWriter @@ -184,12 +262,17 @@ struct State { writer: WriterImpl, fd: Fd, writers: Writers, - buf: Vec, - /// quick hack to get windows working; ideally this should be removed. + /// The chunks that were queued when the writer failed and whose failure + /// has yet to be delivered, oldest first; null for one that was cancelled + /// meanwhile. Every chunk gets its own completion: a builtin that queues + /// several counts them. + failed: VecDeque, + /// A Windows write has the front chunk's bytes: set when `get_buffer` + /// hands them out, cleared when that write reports back (`on_write_pollable`, + /// `fail_pending_writers`). The front chunk stays in the queue meanwhile, + /// dead or not. #[cfg(windows)] - winbuf: Vec, - writer_idx: usize, - total_bytes_written: usize, + in_flight: bool, /// Set (and never cleared) by `fail_pending_writers`. A writer with a /// stored error is dead: `enqueue`/`enqueue_fmt_bltn` must reject new /// chunks with this error instead of queueing them (see @@ -198,16 +281,16 @@ struct State { /// `SystemError`. err: Option, evtloop: EventLoopHandle, - is_writing: bool, started: bool, flags: Flags, /// Weak self-ref so `keepalive()` can bump the strong count from `&self` /// without unsafe Arc-pointer reconstruction. Set via `Arc::new_cyclic` in /// `init()` (the sole constructor). self_weak: std::sync::Weak, - /// Backref to the owning interpreter for async-poll callbacks (which must - /// drive `Yield::run`). Set by the first `enqueue`/`set_interp`; `None` - /// until then. + /// The interpreter whose nodes the queued chunks call back into, for + /// completions that arrive from the event loop. It lists this writer until + /// `Drop` and, when it goes first, clears this through `orphan`: `Some` + /// means alive. interp: Option>, } @@ -253,36 +336,28 @@ impl IOWriter { self.state().flags.is_socket } - pub(crate) fn init(fd: Fd, flags: Flags, evtloop: EventLoopHandle) -> std::sync::Arc { + /// A writer on `fd` (closed with it) whose completions drive `interp`. + pub(crate) fn init(fd: Fd, flags: Flags, interp: &Interpreter) -> std::sync::Arc { let mut writer = WriterImpl::default(); // Tell the PipeWriter impl to *not* close the file descriptor. - #[cfg(not(windows))] - { - writer.close_fd = false; - } - #[cfg(windows)] - { - writer.owns_fd = false; - } + writer.close_fd = false; let this = std::sync::Arc::new_cyclic(|w| IOWriter { state: UnsafeCell::new(State { writer, fd, writers: Writers::new(), - buf: Vec::new(), + failed: VecDeque::new(), #[cfg(windows)] - winbuf: Vec::new(), - writer_idx: 0, - total_bytes_written: 0, + in_flight: false, err: None, - evtloop, - is_writing: false, + evtloop: interp.event_loop, started: false, flags, self_weak: std::sync::Weak::clone(w), - interp: None, + interp: Some(bun_ptr::ParentRef::new(interp)), }), }); + interp.register_io_writer(std::sync::Arc::as_ptr(&this)); // Set the parent backref after Arc allocation so the address is stable. // SAFETY: `Arc::as_ptr` yields `*const IOWriter`; cast to `*mut` only // because the `BufferedWriterParent` callback ABI is `*mut Self`. The @@ -296,16 +371,16 @@ impl IOWriter { this } - /// Stash the interpreter backref so async poll callbacks can drive - /// `Yield::run`. Idempotent. - /// - /// # Safety - /// `interp` must be null or point to the live owning `Interpreter` (which - /// owns the IO struct holding this `Arc`) and outlive it; single-threaded. - #[inline] - pub(crate) fn set_interp(&self, interp: *mut Interpreter) { - // SAFETY: caller contract above. - self.state().interp = unsafe { bun_ptr::ParentRef::from_nullable(interp) }; + /// The interpreter is going away while this writer stays (a Windows write + /// in flight holds a ref on it): nothing queued may call back or tee into + /// the interpreter's buffers any more. + pub(crate) fn orphan(&self) { + let s = self.state(); + s.interp = None; + for w in &mut s.writers { + w.set_dead(); + } + s.failed.clear(); } #[inline] @@ -313,34 +388,17 @@ impl IOWriter { self.state().fd } - #[inline] - #[cfg(windows)] - pub(crate) fn evtloop(&self) -> EventLoopHandle { - self.state().evtloop - } - pub(crate) fn memory_cost(&self) -> usize { let s = self.state(); let mut cost = core::mem::size_of::(); - cost += s.buf.capacity(); - #[cfg(windows)] - { - cost += s.winbuf.capacity(); - } cost += s.writers.capacity() * core::mem::size_of::(); + cost += s.writers.iter().map(|w| w.data.capacity()).sum::(); cost += s.writer.memory_cost(); cost } - /// `bun_io::EventLoopHandle` is an opaque `*mut c_void` that the io-layer - /// `FilePollVTable` round-trips back to the runtime. We pass the address of - /// the stored `bun_event_loop::EventLoopHandle` so the (runtime-registered) - /// vtable can recover it. - #[cfg(not(windows))] #[inline] fn io_evtloop(&self) -> bun_io::EventLoopHandle { - // SAFETY: `bun_io::EventLoopHandle` stores `*mut c_void` purely for - // type-erasure; vtable consumers treat the pointee as read-only self.state().evtloop.as_event_loop_ctx() } @@ -349,7 +407,14 @@ impl IOWriter { fn __start(&self) -> sys::Result<()> { let s = self.state(); crate::shell_log!("IOWriter(fd={}) __start()", s.fd); - if let Err(e) = s.writer.start(s.fd, s.flags.pollable) { + #[cfg(windows)] + let started = match s.flags.kind { + Some(kind) => s.writer.start_with_kind(s.fd, s.flags.origin, kind), + None => s.writer.start_with_origin(s.fd, s.flags.origin), + }; + #[cfg(not(windows))] + let started = s.writer.start(s.fd, s.flags.pollable); + if let Err(e) = started { #[cfg(not(windows))] { // We get this if we pass in a file descriptor that is not @@ -379,41 +444,8 @@ impl IOWriter { } } } - #[cfg(windows)] - { - // This might happen if the file descriptor points to NUL. - // On Windows GetFileType(NUL) returns FILE_TYPE_CHAR, so - // `this.writer.start()` will try to open it as a tty with - // uv_tty_init, but this returns EBADF. As a workaround, - // we'll try opening the file descriptor as a file. - if e.get_errno() == E::EBADF { - s.flags.pollable = false; - s.flags.nonblock = false; - s.flags.is_socket = false; - return s.writer.start_with_file(s.fd); - } - } return Err(e); } - #[cfg(windows)] - { - // When `Source::open` produced a uv pipe/tty, libuv has TAKEN - // OWNERSHIP of the underlying HANDLE - // (`uv_pipe_open`/`uv_tty_init`) and `uv_close` (issued by - // `s.writer.close()` in Drop) will close it. - // `BaseWindowsPipeWriter::start` does not invalidate the stored - // fd (TODO at PipeWriter.rs:1277), so disarm the Drop close here - // instead. The `Source::File`/`SyncFile` case (incl. the - // EBADF→`start_with_file` fallback above, which `return`s early) - // keeps `s.fd` valid: with `owns_fd=false` PipeWriter does NOT - // close it there, so Drop must. - if matches!( - s.writer.source, - Some(bun_io::Source::Pipe(_) | bun_io::Source::Tty(_)) - ) { - s.fd = Fd::INVALID; - } - } #[cfg(not(windows))] { use bun_io::FilePollFlag; @@ -446,7 +478,6 @@ impl IOWriter { /// re-entering `Yield::run` (see `DbgDepthGuard`). fn write(&self) -> WriteOutcome { let s = self.state(); - #[cfg(not(windows))] debug_assert!(s.flags.pollable); if !s.started { @@ -477,15 +508,12 @@ impl IOWriter { #[cfg(windows)] { - crate::shell_log!("IOWriter(fd={}) write() is_writing={}", s.fd, s.is_writing); - if s.is_writing { - return WriteOutcome::Suspended; - } - s.is_writing = true; - if let Err(e) = s.writer.start_with_current_pipe() { - return WriteOutcome::Failed(e); - } - return WriteOutcome::Suspended; + // Does nothing while a write is in flight; its completion + // continues with whatever was queued meanwhile. + return match s.writer.write() { + Ok(()) => WriteOutcome::Suspended, + Err(e) => WriteOutcome::Failed(e), + }; } #[cfg(not(windows))] @@ -513,48 +541,30 @@ impl IOWriter { /// Cancel the chunks enqueued by the given child by marking them as dead. pub(crate) fn cancel_chunks(&self, ptr: ChildPtr) { let s = self.state(); - if s.writers.is_empty() { - return; - } - let idx = s.writer_idx; - if idx >= s.writers.len() { - return; - } - for w in &mut s.writers[idx..] { + for w in &mut s.writers { if w.ptr == ptr { w.set_dead(); } } - } - - /// Skips over dead children and increments `total_bytes_written` by the - /// amount they would have written so the buf is skipped as well. - fn skip_dead(&self) { - let s = self.state(); - while s.writer_idx < s.writers.len() { - let w = &s.writers[s.writer_idx]; - if w.is_dead() { - s.total_bytes_written += w.len - w.written; - s.writer_idx += 1; - continue; + for failed in &mut s.failed { + if *failed == ptr { + *failed = ChildPtr::NULL; } - return; } } - fn wrote_everything(&self) -> bool { + /// Whether a chunk `ptr` enqueued has yet to call back. + #[cfg(debug_assertions)] + pub(crate) fn has_live_chunks(&self, ptr: ChildPtr) -> bool { let s = self.state(); - s.total_bytes_written >= s.buf.len() + s.writers.iter().any(|w| w.ptr == ptr) || s.failed.contains(&ptr) } - /// Only does things on windows. - #[inline] - fn set_writing(&self, writing: bool) { - #[cfg(windows)] - { - self.state().is_writing = writing; + /// Drops the dead chunks at the front; their bytes are never written. + fn skip_dead(s: &mut State) { + while s.writers.front().is_some_and(Writer::is_dead) { + s.writers.pop_front(); } - let _ = writing; } // ── buffer slicing ────────────────────────────────────────────────── @@ -562,224 +572,137 @@ impl IOWriter { /// Returns the buffer of data that needs to be written for the *current* /// writer. fn get_buffer(&self) -> &[u8] { - let result = self.get_buffer_impl(); + let s = self.state(); + // The Windows writer also asks when a write completes, before + // `on_write`: the front chunk is then still the one that write was for. #[cfg(windows)] - { - let s = self.state(); - s.winbuf.clear(); - s.winbuf.extend_from_slice(result); - // `state()` ties `s` to `&self`, so the slice borrow already has - // the `'self` lifetime the signature wants — no raw-parts needed. - return s.winbuf.as_slice(); - } + let skip = !s.in_flight; #[cfg(not(windows))] - result - } - - fn get_buffer_impl(&self) -> &[u8] { - // NOTE: reshaped for borrowck — re-derive `state()` after - // `skip_dead()` instead of holding one `&mut State` across it. - { - let s = self.state(); - if s.writer_idx >= s.writers.len() { - return &[]; - } - if s.writers[s.writer_idx].is_dead() { - self.skip_dead(); - } + let skip = true; + if skip { + Self::skip_dead(s); } - let s = self.state(); - if s.writer_idx >= s.writers.len() { + let Some(front) = s.writers.front() else { return &[]; - } - let remaining = { - let writer = &s.writers[s.writer_idx]; - debug_assert!(writer.len != writer.written); - writer.len - writer.written }; - // `state()` already ties `s` to `&self`, so a plain slice borrow has - // the right lifetime. `buf` is not reallocated until after the - // caller's write syscall completes. - let start = s.total_bytes_written; - &s.buf[start..start + remaining] + debug_assert!(!front.wrote_everything()); + #[cfg(windows)] + { + s.in_flight = true; + } + front.remaining() } // ── bump (chunk completed) ────────────────────────────────────────── - /// Advance past `current_writer`, shrinking `buf` if appropriate, and - /// return the `Yield` for the child's `on_io_writer_chunk` callback. - fn bump(&self, current_idx: usize) -> Yield { - // NOTE: reshaped for borrowck — `skip_dead()` re-derives `state()`, - // so we must drop `s` before calling it and re-derive after, otherwise - // two `&mut State` are live simultaneously (UB under Stacked Borrows). - let (is_dead, written, child_ptr) = { - let s = self.state(); - let w = &s.writers[current_idx]; - (w.is_dead(), w.written, w.ptr) - }; - - if is_dead { - self.skip_dead(); - } else { - let s = self.state(); - debug_assert!(s.writers[current_idx].written == s.writers[current_idx].len); - s.writer_idx += 1; - } - + /// Take the front chunk, which is fully written or dead, off the queue + /// (freeing its bytes) and return the `Yield` for its child's + /// `on_io_writer_chunk` callback. + fn bump(&self) -> Yield { let s = self.state(); - if s.writer_idx >= s.writers.len() { - s.buf.clear(); - s.writer_idx = 0; - s.writers.clear(); - s.total_bytes_written = 0; - } else if s.total_bytes_written >= SHRINK_THRESHOLD { - s.buf.drain_front(s.total_bytes_written); - s.total_bytes_written = 0; - // Drop the *prefix* of the writers queue: Vec::drain(..idx). - s.writers.drain(..s.writer_idx); - s.writer_idx = 0; - if cfg!(debug_assertions) && !s.writers.is_empty() { - debug_assert!(s.buf.len() >= s.writers[0].len); - } + let done = s.writers.pop_front().expect("bump on an empty queue"); + Self::skip_dead(s); + if done.is_dead() { + return Yield::done(); } - - if !is_dead { - return Yield::OnIoWriterChunk { - child: child_ptr, - written, - err: None, - }; + debug_assert!(done.wrote_everything()); + Yield::OnIoWriterChunk { + child: done.ptr, + written: done.written, + err: None, } - Yield::done() } // ── file write (non-pollable sync path) ───────────────────────────── - /// Tee `amt` bytes from the current buffer position into `writers[idx]`'s - /// capture and advance its `written` / `total_bytes_written` counters. - #[cfg(not(windows))] - fn record_write_progress(&self, idx: usize, amt: usize) { + fn do_file_write(&self) -> Yield { let s = self.state(); - let lo = s.total_bytes_written; - s.writers[idx].tee(&s.buf[lo..lo + amt]); - s.total_bytes_written += amt; - s.writers[idx].written += amt; - } + debug_assert!(!s.flags.pollable); + Self::skip_dead(s); + let fd = s.fd; + let front = s.writers.front_mut().expect("enqueue pushed a chunk"); - /// POSIX-only. `child` is the writer being enqueued (see `on_sync_error`). - #[cfg(not(windows))] - fn do_file_write(&self, child: ChildPtr) -> Yield { - { - let s = self.state(); - debug_assert!(!s.flags.pollable); - debug_assert!(s.writer_idx < s.writers.len()); - } - - scopeguard::defer! { self.set_writing(false); } - self.skip_dead(); - - let idx = self.state().writer_idx; - debug_assert!(!self.state().writers[idx].is_dead()); - - let buf = self.get_buffer(); - debug_assert!(!buf.is_empty()); - - let result = drain_buffered_data(self, buf, u32::MAX as usize); - // NOTE: re-derive `state()` after `drain_buffered_data` instead of - // holding a stale `&mut`. - let amt = match result { + let amt = match write_to_file(fd, front.remaining()) { bun_io::WriteResult::Done(amt) | bun_io::WriteResult::Wrote(amt) => amt, + #[cfg(not(windows))] bun_io::WriteResult::Pending(amt) => { // EAGAIN from a target that was classified non-pollable (a // FIFO or chardev opened by path with O_NONBLOCK). Record the // partial write and restart this writer on the pollable path. - self.record_write_progress(idx, amt); - let s = self.state(); + front.advance(amt); s.flags.pollable = true; s.flags.nonblock = true; s.started = false; return match self.write() { WriteOutcome::Suspended => Yield::suspended(), - WriteOutcome::IsActuallyFile => self - .on_sync_error(child, &sys::Error::from_code(E::EAGAIN, sys::Tag::write)), - WriteOutcome::Failed(e) => self.on_sync_error(child, &e), + WriteOutcome::IsActuallyFile => { + self.on_sync_error(&sys::Error::from_code(E::EAGAIN, sys::Tag::write)) + } + WriteOutcome::Failed(e) => self.on_sync_error(&e), }; } + // Only a disk file is written here. + #[cfg(windows)] + bun_io::WriteResult::Pending(_) => { + return self.on_sync_error(&sys::Error::from_code(E::EAGAIN, sys::Tag::write)); + } // The caller is inside the enqueuing child's trampoline, so the // error completion is returned, not `Yield::run` from here. - bun_io::WriteResult::Err(e) => return self.on_sync_error(child, &e), + bun_io::WriteResult::Err(e) => return self.on_sync_error(&e), }; - self.record_write_progress(idx, amt); - if !self.state().writers[idx].wrote_everything() { + front.advance(amt); + if !front.wrote_everything() { // The only case where we get partial writes is when an error is // encountered, which returns above. unreachable!( "IOWriter.doFileWrite: child.wroteEverything() is false. This is unexpected behavior and indicates a bug in Bun. Please file a GitHub issue." ); } - self.bump(idx) + self.bump() } // ── poll callback ─────────────────────────────────────────────────── - /// The `BufferedWriter.onWrite` hook. Runs on the event loop when the fd - /// is writable. + /// The `BufferedWriter.onWrite` hook: `amount` more bytes of the front + /// chunk were written. Runs on the event loop. fn on_write_pollable(&self, amount: usize, status: bun_io::WriteStatus) { - // NOTE: `set_writing` re-derives `state()` on Windows, which would - // invalidate `s` under Stacked Borrows; do it before binding `s` - // (matches the ordering in `on_error`). - self.set_writing(false); let s = self.state(); - #[cfg(not(windows))] debug_assert!(s.flags.pollable); + #[cfg(windows)] + { + s.in_flight = false; + } - if s.writer_idx >= s.writers.len() { + let queued = s.writers.len(); + let Some(front) = s.writers.front_mut() else { return; - } - let idx = s.writer_idx; - if s.writers[idx].is_dead() { - self.run_yield(self.bump(idx)); + }; + if front.is_dead() { + self.run_yield(self.bump()); } else { - let lo = s.total_bytes_written; - s.writers[idx].tee(&s.buf[lo..lo + amount]); - s.total_bytes_written += amount; - s.writers[idx].written += amount; + front.advance(amount); + let wrote_everything = front.wrote_everything(); if status == bun_io::WriteStatus::EndOfFile { - // NOTE: inline `is_last_idx` instead of calling - // `self.is_last_idx(idx)` — that re-derives `state()` while `s` - // is still live, which is two simultaneous `&mut State` (UB). - let last = idx == s.writers.len().saturating_sub(1); - let not_fully_written = if last { - true - } else { - s.writers[idx].written < s.writers[idx].len - }; - if !not_fully_written { + if queued > 1 && wrote_everything { return; } // Other end of the socket/pipe closed and we got EPIPE // (e.g. `ls | echo`). Quick hack: have all writers see an // error. - s.flags.broken_pipe = true; self.broken_pipe_for_writers(); return; } - if s.writers[idx].written >= s.writers[idx].len { - self.run_yield(self.bump(idx)); + if wrote_everything { + self.run_yield(self.bump()); } } - let wrote_everything = self.wrote_everything(); + // `bump` left a live chunk at the front, or none at all. let s = self.state(); - if !wrote_everything && s.writer_idx < s.writers.len() { + if !s.writers.is_empty() { #[cfg(windows)] - { - // NOTE: inline `set_writing(true)` instead of calling the - // helper — the helper re-derives `state()` while `s` is live, - // which is two simultaneous `&mut State` (UB under Stacked - // Borrows). Same discipline as the top of this fn. - s.is_writing = true; - s.writer.write(); + if let Err(e) = s.writer.write() { + self.on_error(&e); } #[cfg(not(windows))] { @@ -789,42 +712,26 @@ impl IOWriter { } } + /// The reader of this pipe went away: every queued chunk fails with `EPIPE`. fn broken_pipe_for_writers(&self) { - let s = self.state(); - debug_assert!(s.flags.broken_pipe); - // NOTE: reshaped for borrowck — collect targets first so we don't - // hold `&mut s.writers` across `cancel_chunks`/`run_yield`. - let mut targets: Vec = Vec::new(); - for w in &s.writers[s.writer_idx..] { - if w.is_dead() { - continue; - } - if !targets.contains(&w.ptr) { - targets.push(w.ptr); - } - } - for ptr in targets { - let err = sys::Error::from_code(E::EPIPE, sys::Tag::write).to_system_error(); + let err = sys::Error::from_code(E::EPIPE, sys::Tag::write); + self.fail_pending_writers(&err); + while let Some(ptr) = self.next_failed() { self.run_yield(Yield::OnIoWriterChunk { child: ptr, written: 0, - err: Some(err), + err: Some(err.to_system_error()), }); - self.cancel_chunks(ptr); } - let s = self.state(); - s.total_bytes_written = 0; - s.writers.clear(); - s.buf.clear(); - s.writer_idx = 0; } - /// Shared failure bookkeeping: mark broken pipes, reset the queue, and - /// return the still-pending children that have to be told their chunk - /// failed. The queue is reset *before* any of them runs so that a child - /// re-enqueueing from its callback is not wiped afterwards. - fn fail_pending_writers(&self, err: &sys::Error) -> Vec { - self.set_writing(false); + /// Shared failure bookkeeping: mark broken pipes and move the queue to + /// `failed`, from where `next_failed` hands out the chunks that have to be + /// told. The queue is empty *before* any of them runs so that a child + /// re-enqueueing from its callback is rejected, not queued. No write is in + /// flight: this runs from a write's completion, or for one that could not + /// be submitted. + fn fail_pending_writers(&self, err: &sys::Error) { let s = self.state(); if err.get_errno() == E::EPIPE { s.flags.broken_pipe = true; @@ -834,31 +741,32 @@ impl IOWriter { // must be rejected by `handle_dead_writer`, not queued onto a writer // whose handle the error path is tearing down. s.err = Some(err.clone()); - // Writers before writer_idx have already had their callback fired and - // may have been freed; only notify the still-pending ones, dedup'd. - let mut pending: Vec = Vec::new(); - for w in &s.writers[s.writer_idx..] { - if !w.is_dead() && !pending.contains(&w.ptr) { - pending.push(w.ptr); + s.failed.extend(s.writers.drain(..).map(|w| w.ptr)); + #[cfg(windows)] + { + s.in_flight = false; + } + } + + /// The oldest failed chunk that is still live. Taken one at a time: the + /// callback of one may cancel those behind it (`cancel_chunks`). + fn next_failed(&self) -> Option { + let s = self.state(); + while let Some(ptr) = s.failed.pop_front() { + if !ptr.is_null() { + return Some(ptr); } } - s.total_bytes_written = 0; - s.writer_idx = 0; - s.buf.clear(); - s.writers.clear(); - pending + None } - /// Write failure reported by the `bun_io` writer callbacks. Each pending - /// child's error completion is driven through its own `Yield::run`; on - /// POSIX these callbacks only fire from the event loop, with no trampoline - /// on the stack. On Windows uv can also deliver a synchronous submission - /// failure from under `write()` (`start_with_current_pipe` returns `Ok` - /// unconditionally), a re-entry `write()` cannot turn into a - /// `WriteOutcome::Failed`. + /// Write failure reported by the `bun_io` writer callbacks, which only + /// fire from the event loop, with no trampoline on the stack: each pending + /// chunk's error completion is driven through its own `Yield::run`. fn on_error(&self, err: &sys::Error) { let _keepalive = self.keepalive(); - for ptr in self.fail_pending_writers(err) { + self.fail_pending_writers(err); + while let Some(ptr) = self.next_failed() { // `SystemError` owns `bun_core::String`s by value (no shared // refcount yet), so re-derive a fresh one per callee instead of // cloning the stored error. @@ -871,19 +779,22 @@ impl IOWriter { } } - /// Synchronous write failure while `child`'s `enqueue` call (and therefore - /// its trampoline) is still on the stack. `child`'s error completion is - /// *returned* so that trampoline delivers it after `enqueue` unwinds; - /// calling `on_error` here instead would re-enter `Yield::run` once per - /// failing command and fire `child`'s callback from inside its own - /// `enqueue`. Usually `child`'s chunk is the only pending one (a - /// synchronous failure is the first write attempt of a batch); if a poll - /// re-registration fails while other children are still queued, those are - /// dispatched the way the async path dispatches them. - fn on_sync_error(&self, child: ChildPtr, err: &sys::Error) -> Yield { + /// Synchronous write failure while a child's `enqueue` call (and therefore + /// its trampoline) is still on the stack. The completion of the chunk it + /// just pushed, the last in the queue, is *returned* so that trampoline + /// delivers it after `enqueue` unwinds; calling `on_error` here instead + /// would re-enter `Yield::run` once per failing command and fire the + /// callback from inside its own `enqueue`. Usually that chunk is the only + /// one (a synchronous failure is the first write attempt of a batch); if a + /// poll re-registration fails while others are still queued, those are + /// dispatched the way the async path dispatches them, oldest first. + fn on_sync_error(&self, err: &sys::Error) -> Yield { let _keepalive = self.keepalive(); - let mut completion = None; - for ptr in self.fail_pending_writers(err) { + self.fail_pending_writers(err); + loop { + let Some(ptr) = self.next_failed() else { + return Yield::done(); + }; // `SystemError` owns `bun_core::String`s by value (no shared // refcount yet), so re-derive a fresh one per callee. let y = Yield::OnIoWriterChunk { @@ -891,49 +802,33 @@ impl IOWriter { written: 0, err: Some(err.to_shell_system_error()), }; - if completion.is_none() && ptr == child { - completion = Some(y); - } else { - self.run_yield(y); + if !self.state().failed.iter().any(|later| !later.is_null()) { + return y; } + self.run_yield(y); } - // The writer `enqueue` just pushed for `child` is live and at or past - // `writer_idx`, so it is always in the pending list. - debug_assert!(completion.is_some()); - completion.unwrap_or_else(Yield::done) } - fn on_close(&self) { - self.set_writing(false); - } + fn on_close(&self) {} - /// Drive a `Yield` from inside an async poll callback. Requires `interp` - /// to have been set; if not, the chunk-complete is dropped (debug-asserts). + /// Drive a `Yield` from a completion that arrived from the event loop. fn run_yield(&self, y: Yield) { + if matches!(y, Yield::Done | Yield::Suspended) { + return; + } + // `orphan` killed every chunk before it cleared `interp`, and a dead + // chunk completes with `Yield::Done`. let Some(interp) = self.state().interp else { - debug_assert!( - matches!(y, Yield::Done), - "IOWriter async callback fired without interp backref" - ); + debug_assert!(false, "a live chunk completed on an orphaned IOWriter"); return; }; - // SAFETY: interp outlives every IOWriter (it owns the IO struct that - // holds the Arc). Single-threaded; R-2: `Interpreter::run` takes - // `&self` now — `ParentRef: Deref` yields the - // shared borrow without `assume_mut()`. y.run(&interp); } // ── enqueue ───────────────────────────────────────────────────────── - /// A writer that already reported a fatal error must not accept new - /// chunks: `PosixBufferedWriter::_on_error` closes the handle after - /// `on_error` returns, so a chunk queued from inside the completion - /// callbacks (or any later one) would wait on a poll that is being torn - /// down, and a later `write()` would run with `handle == Closed` (the - /// pollable path asserts `handle == Poll`). Broken pipes are the EPIPE - /// flavor of the same thing. Report the error to the child instead of - /// queueing the chunk. + /// A writer that reported a fatal error (or a broken pipe) rejects new + /// chunks with that error. fn handle_dead_writer(&self, ptr: ChildPtr) -> Option { let s = self.state(); if s.flags.broken_pipe { @@ -956,63 +851,97 @@ impl IOWriter { None } - #[cfg(not(windows))] - fn enqueue_file(&self, child: ChildPtr) -> Yield { - let s = self.state(); - if s.is_writing { - return Yield::suspended(); - } + fn enqueue_file(&self) -> Yield { // The pollable path sets `started` in write(); the non-pollable file // path bypasses write() entirely, so set it here. - s.started = true; - self.set_writing(true); - self.do_file_write(child) + self.state().started = true; + self.do_file_write() + } + + /// Ask what the fd is. A query that fails leaves it to the source, whose + /// open fails the same way and is the write's error. + fn classify(s: &mut State) { + s.flags.classified = true; + #[cfg(windows)] + { + s.flags.kind = sys::File::borrow(&s.fd).kind().ok(); + s.flags.pollable = s.flags.kind != Some(sys::FileKind::File); + } + #[cfg(not(windows))] + { + s.flags.pollable = crate::shell::interpreter::is_pollable(s.fd); + } } - /// You MUST have already added the data to `self.buf`! - /// `child` is the writer that was just pushed (see `on_sync_error`). - fn enqueue_internal(&self, child: ChildPtr) -> Yield { + /// For the chunk that was just pushed. + fn enqueue_internal(&self) -> Yield { debug_assert!(!self.state().flags.broken_pipe); debug_assert!(self.state().err.is_none()); - #[cfg(not(windows))] + if !self.state().flags.classified { + Self::classify(self.state()); + } if !self.state().flags.pollable { - return self.enqueue_file(child); + return self.enqueue_file(); } match self.write() { WriteOutcome::Suspended => Yield::suspended(), #[cfg(not(windows))] - WriteOutcome::IsActuallyFile => self.enqueue_file(child), - WriteOutcome::Failed(e) => self.on_sync_error(child, &e), + WriteOutcome::IsActuallyFile => self.enqueue_file(), + WriteOutcome::Failed(e) => self.on_sync_error(&e), } } - /// Queue `buf` for writing; when the chunk completes (or errors), - /// `child`'s `on_io_writer_chunk` fires. + /// The completion of a chunk of `len` bytes that is not queued: the + /// writer's stored error, or nothing to write. + fn complete_unqueued(&self, child: ChildPtr, len: usize) -> Option { + if let Some(y) = self.handle_dead_writer(child) { + return Some(y); + } + (len == 0).then_some(Yield::OnIoWriterChunk { + child, + written: 0, + err: None, + }) + } + + fn push(&self, child: ChildPtr, bytelist: Option<*mut Vec>, data: Vec) -> Yield { + self.state().writers.push_back(Writer { + ptr: child, + data, + written: 0, + bytelist, + }); + self.enqueue_internal() + } + + /// Queue a copy of `buf` for writing; when the chunk completes (or + /// errors), `child`'s `on_io_writer_chunk` fires. pub(crate) fn enqueue( &self, child: ChildPtr, bytelist: Option<*mut Vec>, buf: &[u8], ) -> Yield { - if let Some(y) = self.handle_dead_writer(child) { + if let Some(y) = self.complete_unqueued(child, buf.len()) { return y; } - if buf.is_empty() { - return Yield::OnIoWriterChunk { - child, - written: 0, - err: None, - }; + self.push(child, bytelist, buf.to_vec()) + } + + /// [`enqueue`](Self::enqueue) for bytes the caller is done with: they are + /// written from where they are. + pub(crate) fn enqueue_owned( + &self, + child: ChildPtr, + bytelist: Option<*mut Vec>, + mut buf: Vec, + ) -> Yield { + if let Some(y) = self.complete_unqueued(child, buf.len()) { + return y; } - let s = self.state(); - s.buf.extend_from_slice(buf); - s.writers.push(Writer { - ptr: child, - len: buf.len(), - written: 0, - bytelist, - }); - self.enqueue_internal(child) + // A reader hands over its whole buffer for what may be a short line. + buf.shrink_to_fit(); + self.push(child, bytelist, buf) } /// Prefix `"{kind}: "` then format. @@ -1024,41 +953,15 @@ impl IOWriter { args: core::fmt::Arguments<'_>, ) -> Yield { use std::io::Write as _; - let s = self.state(); - let start = s.buf.len(); - if let Some(k) = kind { - let _ = write!(&mut s.buf, "{}: ", k.as_str()); - } - let _ = s.buf.write_fmt(args); - // `buf` is written *before* the dead-writer checks (the bytes are dead - // on the error path but no `Writer` references them, and an errored - // writer never drains again). - // NOTE: inline `handle_dead_writer` instead of calling the helper — - // the helper re-derives `state()` while `s` is still live, which is two - // simultaneous `&mut State` (UB under Stacked Borrows). - if s.flags.broken_pipe { - let err = sys::Error::from_code(E::EPIPE, sys::Tag::write).to_system_error(); - return Yield::OnIoWriterChunk { - child, - written: 0, - err: Some(err), - }; + if let Some(y) = self.handle_dead_writer(child) { + return y; } - if let Some(err) = &s.err { - return Yield::OnIoWriterChunk { - child, - written: 0, - err: Some(err.to_shell_system_error()), - }; + let mut buf = Vec::new(); + if let Some(k) = kind { + let _ = write!(&mut buf, "{}: ", k.as_str()); } - let end = s.buf.len(); - s.writers.push(Writer { - ptr: child, - len: end - start, - written: 0, - bytelist, - }); - self.enqueue_internal(child) + let _ = buf.write_fmt(args); + self.enqueue_owned(child, bytelist, buf) } } @@ -1085,31 +988,27 @@ bun_io::impl_buffered_writer_parent! { on_close = on_close, get_buffer = |this| (*this).get_buffer(), event_loop = |this| (*this).io_evtloop(), - uv_loop = |this| (*(*this).evtloop().loop_()).uv_loop, // INVARIANT: `this` is `Arc::as_ptr` stashed via `writer.set_parent` in // `IOWriter::init` (sole constructor); passing a non-Arc ptr is UB. - ref_ = |this| std::sync::Arc::increment_strong_count(this as *const Self), - deref = |this| std::sync::Arc::decrement_strong_count(this as *const Self), + ref_ = |this| std::sync::Arc::increment_strong_count(this.cast_const()), + deref = |this| std::sync::Arc::decrement_strong_count(this.cast_const()), } // ────────────────────────────────────────────────────────────────────────── -// drainBufferedData / tryWrite (POSIX file path) +// File path // ────────────────────────────────────────────────────────────────────────── -#[cfg(not(windows))] -fn try_write_with_write_fn( - fd: Fd, - buf: &[u8], - write_fn: fn(Fd, &[u8]) -> sys::Maybe, -) -> bun_io::WriteResult { +/// Writes all of `buf` unless the fd stops taking bytes. A failure is an +/// error even after a partial write: `do_file_write` fails the whole chunk +/// either way. +fn write_to_file(fd: Fd, buf: &[u8]) -> bun_io::WriteResult { let mut offset: usize = 0; while offset < buf.len() { - match write_fn(fd, &buf[offset..]) { + match sys::write(fd, &buf[offset..]) { Err(err) => { if err.is_retry() { return bun_io::WriteResult::Pending(offset); } - // Return EPIPE as an error so it propagates properly. return bun_io::WriteResult::Err(err); } Ok(wrote) => { @@ -1123,58 +1022,18 @@ fn try_write_with_write_fn( bun_io::WriteResult::Wrote(offset) } -/// TODO: This function and `try_write_with_write_fn` are copy-pastes from -/// PipeWriter; it would be nice to not have to do that. -#[cfg(not(windows))] -fn drain_buffered_data( - parent: &IOWriter, - buf: &[u8], - max_write_size: usize, -) -> bun_io::WriteResult { - let trimmed = if max_write_size < buf.len() && max_write_size > 0 { - &buf[..max_write_size] - } else { - buf - }; - let mut drained: usize = 0; - while drained < trimmed.len() { - match try_write_with_write_fn(parent.state().fd, buf, sys::write) { - bun_io::WriteResult::Pending(pending) => { - drained += pending; - return bun_io::WriteResult::Pending(drained); - } - bun_io::WriteResult::Wrote(amt) => { - drained += amt; - } - bun_io::WriteResult::Err(err) => { - // Reported as an error even after a partial write: the caller - // (`do_file_write`) fails the whole chunk either way, and it - // must not dispatch the failure from under the trampoline. - return bun_io::WriteResult::Err(err); - } - bun_io::WriteResult::Done(amt) => { - drained += amt; - return bun_io::WriteResult::Done(drained); - } - } - } - bun_io::WriteResult::Wrote(drained) -} - // ────────────────────────────────────────────────────────────────────────── // Drop // ────────────────────────────────────────────────────────────────────────── impl Drop for IOWriter { fn drop(&mut self) { - // With `Arc` the last ref drops *after* the callback returns, so the - // synchronous path is safe (PipeWriter cannot touch us after free). - // TODO: if a PipeWriter callback is on the stack when the last - // Arc drops (possible via re-entrant child deinit), we need the async - // hop. Revisit once `bun_event_loop::EventLoopTask` is wired to the - // shell's `EventLoopHandle` shim. + let this: *const IOWriter = self; let s = self.state.get_mut(); crate::shell_log!("IOWriter(fd={}) deinit", s.fd); + if let Some(interp) = s.interp { + interp.forget_io_writer(this); + } #[cfg(not(windows))] { if matches!(s.writer.handle, bun_io::pipes::PollOrFd::Poll(_)) { @@ -1183,13 +1042,13 @@ impl Drop for IOWriter { .close_impl(None, None::, false); } } + // The source goes before the fd it was opened on. #[cfg(windows)] - { - s.writer.close(); - } - if s.fd != Fd::INVALID { - let _ = sys::close(s.fd); - } + s.writer.close_without_reporting(); + // Closed here, not on another thread: a redirect target is closed by + // the time the command that wrote it settles, so whatever runs next + // can open, move or delete the file. + let _ = sys::close(s.fd); s.writer .disable_keeping_process_alive(s.evtloop.as_event_loop_ctx()); } diff --git a/src/runtime/shell/Yield.rs b/src/runtime/shell/Yield.rs index 4614c0166f74..9adefc0fe4db 100644 --- a/src/runtime/shell/Yield.rs +++ b/src/runtime/shell/Yield.rs @@ -34,7 +34,7 @@ pub enum Yield { // the throw boundary. err: Option, }, - /// Execution is waiting on async IO (epoll/kqueue/uv). The caller's task + /// Execution is waiting on async IO (epoll/kqueue/IOCP). The caller's task /// callback will resume by calling `.run()` again later. Suspended, /// The node threw a JS exception; `Interpreter::fail` winds the script down. diff --git a/src/runtime/shell/builtin/basename.rs b/src/runtime/shell/builtin/basename.rs index dc408de88efc..aaf9024b5773 100644 --- a/src/runtime/shell/builtin/basename.rs +++ b/src/runtime/shell/builtin/basename.rs @@ -40,7 +40,7 @@ impl Basename { let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout - .enqueue(child, &owned, safeguard); + .enqueue_owned(child, owned, safeguard); } let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); Builtin::done(interp, cmd, 0) diff --git a/src/runtime/shell/builtin/cat.rs b/src/runtime/shell/builtin/cat.rs index 6a444713556d..cbbc5032d91c 100644 --- a/src/runtime/shell/builtin/cat.rs +++ b/src/runtime/shell/builtin/cat.rs @@ -14,31 +14,71 @@ pub(crate) struct Cat { pub(crate) state: CatState, } +/// Where the inputs and the chunks written for them stand. +#[derive(Default)] +pub(crate) struct Progress { + /// The current input is over: its reader reported EOF or an error, or it + /// could not be opened. + in_done: bool, + /// Chunks queued on the stdout and stderr writers, and how many of them + /// have called back. A queued chunk calls back into this Cmd by `NodeId`, + /// so the Cmd has to stay until the last one has. + chunks_queued: usize, + chunks_done: usize, + /// An input could not be read: the exit code is 1. + failed: bool, +} + +impl Progress { + fn finished(&self) -> bool { + self.in_done && self.chunks_done >= self.chunks_queued + } + + fn exit_code(&self) -> ExitCode { + ExitCode::from(self.failed) + } +} + #[derive(Default)] pub(crate) enum CatState { #[default] Idle, - ExecStdin { - in_done: bool, - chunks_queued: usize, - chunks_done: usize, - errno: ExitCode, - }, + ExecStdin(Progress), ExecFilepathArgs { /// Index into argv where filepath args start. args_start: usize, - /// Current index into the filepath args. + /// How many of the filepath args have been started. idx: usize, - /// Per-file reader. + /// The current file's reader. reader: Option>, - chunks_queued: usize, - chunks_done: usize, - out_done: bool, - in_done: bool, + progress: Progress, }, + /// The option parser's message is on its way to stderr. WaitingWriteErr, } +impl CatState { + fn progress(&mut self) -> Option<&mut Progress> { + match self { + CatState::ExecStdin(progress) | CatState::ExecFilepathArgs { progress, .. } => { + Some(progress) + } + CatState::Idle | CatState::WaitingWriteErr => None, + } + } + + /// What follows a reader or writer completion. + fn step(&mut self) -> Step { + match self { + CatState::ExecStdin(progress) if progress.finished() => { + Step::Done(progress.exit_code()) + } + CatState::ExecFilepathArgs { progress, .. } if progress.finished() => Step::Next, + _ => Step::Suspend, + } + } +} + /// Internal: what to do after dropping the &mut state borrow. pub(crate) enum Step { Suspend, @@ -50,7 +90,7 @@ impl Step { fn run(self, interp: &Interpreter, cmd: NodeId) -> Yield { match self { Step::Suspend => Yield::suspended(), - Step::Done(code) => Builtin::done(interp, cmd, code), + Step::Done(code) => Cat::finish(interp, cmd, code), Step::Next => Cat::next(interp, cmd), } } @@ -73,166 +113,143 @@ impl Cat { }; let argc = Builtin::of(interp, cmd).args_slice().len(); - let should_read_from_stdin = filepath_start.is_none() || filepath_start == Some(argc); - - Self::state_mut(interp, cmd).state = if should_read_from_stdin { - CatState::ExecStdin { - in_done: false, - chunks_queued: 0, - chunks_done: 0, - errno: 0, + match filepath_start.filter(|start| *start < argc) { + None => { + Self::state_mut(interp, cmd).state = CatState::ExecStdin(Progress::default()); + Self::read_stdin(interp, cmd) } - } else { - CatState::ExecFilepathArgs { - args_start: filepath_start.unwrap(), - idx: 0, - reader: None, - chunks_queued: 0, - chunks_done: 0, - out_done: false, - in_done: false, + Some(args_start) => { + Self::state_mut(interp, cmd).state = CatState::ExecFilepathArgs { + args_start, + idx: 0, + reader: None, + progress: Progress::default(), + }; + Self::next(interp, cmd) } - }; - - Self::next(interp, cmd) + } } - fn write_failing_error( - interp: &Interpreter, - cmd: NodeId, - buf: &[u8], - exit_code: ExitCode, - ) -> Yield { - if let Some(safeguard) = Builtin::of(interp, cmd).stderr.needs_io() { - Self::state_mut(interp, cmd).state = CatState::WaitingWriteErr; - let child = ChildPtr::new(cmd, WriterTag::Builtin); - return Builtin::of_mut(interp, cmd) - .stderr - .enqueue(child, buf, safeguard); + /// The Cmd leaves the readers it listens to, then finishes. + fn finish(interp: &Interpreter, cmd: NodeId, exit_code: ExitCode) -> Yield { + let listener = ReaderChildPtr { + node: cmd, + tag: ReaderTag::Cat, + }; + if let CatState::ExecFilepathArgs { reader, .. } = &mut Self::state_mut(interp, cmd).state + && let Some(reader) = reader.take() + { + reader.remove_reader(listener); + } + // The shell's stdin reader outlives this Cmd and goes on to serve the + // next one. + if let BuiltinInput::Fd(stdin) = &Builtin::of(interp, cmd).stdin { + stdin.remove_reader(listener); } - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stderr, buf); Builtin::done(interp, cmd, exit_code) } - pub(crate) fn next(interp: &Interpreter, cmd: NodeId) -> Yield { - // Read scalars, drop the borrow, then act. - enum Branch { - Stdin, - FileArg { args_start: usize, idx: usize }, - WaitingErr, + /// Queue `buf` on stdout or stderr, counted, or write it right away when + /// the stream is not a fd. `Some`: the chunk's completion arrives through + /// `on_io_writer_chunk`. + fn write(interp: &Interpreter, cmd: NodeId, io_kind: IoKind, buf: Vec) -> Option { + let me = Builtin::of(interp, cmd); + let needs_io = match io_kind { + IoKind::Stdout => me.stdout.needs_io(), + IoKind::Stderr => me.stderr.needs_io(), + }; + let Some(safeguard) = needs_io else { + let _ = Builtin::write_no_io(interp, cmd, io_kind, &buf); + return None; + }; + if let Some(progress) = Self::state_mut(interp, cmd).state.progress() { + progress.chunks_queued += 1; } - let branch = match &Self::state_mut(interp, cmd).state { - CatState::Idle => panic!("Invalid state"), - CatState::ExecStdin { .. } => Branch::Stdin, - CatState::ExecFilepathArgs { - args_start, idx, .. - } => Branch::FileArg { - args_start: *args_start, - idx: *idx, - }, - CatState::WaitingWriteErr => Branch::WaitingErr, + let child = ChildPtr::new(cmd, WriterTag::Builtin); + let out = match io_kind { + IoKind::Stdout => &mut Builtin::of_mut(interp, cmd).stdout, + IoKind::Stderr => &mut Builtin::of_mut(interp, cmd).stderr, }; - match branch { - Branch::Stdin => { - // Stdin doesn't need IO (captured/ignored): read it all - // synchronously and write straight to stdout. - let stdin_needs_io = Builtin::of(interp, cmd).stdin.needs_io(); - if !stdin_needs_io { - if let CatState::ExecStdin { in_done, .. } = - &mut Self::state_mut(interp, cmd).state - { - *in_done = true; - } - // Copy stdin bytes so the &mut on `stdout`/`write_no_io` - // doesn't overlap a borrow of `stdin`. - let buf = Builtin::read_stdin_no_io(interp, cmd).to_vec(); - if let Some(safeguard) = Builtin::of(interp, cmd).stdout.needs_io() { - let child = ChildPtr::new(cmd, WriterTag::Builtin); - return Builtin::of_mut(interp, cmd) - .stdout - .enqueue(child, &buf, safeguard); - } - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); - return Builtin::done(interp, cmd, 0); - } - // Clone the `Arc` - // out of `stdin` so we hold no borrow of `interp` across - // `start()` (which may re-enter via the raw interp backref). - let interp_ptr: *mut Interpreter = interp.as_ctx_ptr(); - let reader = match &Builtin::of(interp, cmd).stdin { - BuiltinInput::Fd(r) => Arc::clone(r), - _ => unreachable!("needs_io() returned true"), - }; - reader.set_interp(interp_ptr); - reader.add_reader(ReaderChildPtr { - node: cmd, - tag: ReaderTag::Cat, - }); - reader.start() - } - Branch::FileArg { args_start, idx } => { - let argc = Builtin::of(interp, cmd).args_slice().len(); - let n_files = argc - args_start; - if idx >= n_files { - // Drop the reader if any. - if let CatState::ExecFilepathArgs { reader, .. } = - &mut Self::state_mut(interp, cmd).state - { - *reader = None; - } - return Builtin::done(interp, cmd, 0); + Some(out.enqueue_owned(child, buf, safeguard)) + } + + /// The current input could not be opened or read: say so on stderr and + /// carry on with the next one, like `cat`. `err` names the input in its + /// path. `Some` as for [`Self::write`]. + fn input_failed(interp: &Interpreter, cmd: NodeId, err: &bun_sys::Error) -> Option { + if let Some(progress) = Self::state_mut(interp, cmd).state.progress() { + progress.in_done = true; + progress.failed = true; + } + let message = Builtin::task_error_to_string(interp, cmd, Kind::Cat, err).to_vec(); + Self::write(interp, cmd, IoKind::Stderr, message) + } + + fn read_stdin(interp: &Interpreter, cmd: NodeId) -> Yield { + let reader = match &Builtin::of(interp, cmd).stdin { + BuiltinInput::Fd(reader) => Arc::clone(reader), + // Not a fd (captured or ignored): all of it is here already. + _ => { + if let Some(progress) = Self::state_mut(interp, cmd).state.progress() { + progress.in_done = true; } - if let CatState::ExecFilepathArgs { reader, .. } = - &mut Self::state_mut(interp, cmd).state - { - *reader = None; + let buf = Builtin::read_stdin_no_io(interp, cmd).to_vec(); + if let Some(y) = Self::write(interp, cmd, IoKind::Stdout, buf) { + return y; } + return Self::finish(interp, cmd, 0); + } + }; + reader.add_reader(ReaderChildPtr { + node: cmd, + tag: ReaderTag::Cat, + }); + reader.start() + } - let path = Builtin::of(interp, cmd).arg_zstr(args_start + idx); - - if let CatState::ExecFilepathArgs { idx: i, .. } = - &mut Self::state_mut(interp, cmd).state - { - *i += 1; + /// Start on the next file argument, or finish after the last. + pub(crate) fn next(interp: &Interpreter, cmd: NodeId) -> Yield { + loop { + let (arg, exit_code) = match &mut Self::state_mut(interp, cmd).state { + CatState::ExecFilepathArgs { + args_start, + idx, + reader, + progress, + } => { + *reader = None; + progress.in_done = false; + let arg = *args_start + *idx; + *idx += 1; + (arg, progress.exit_code()) } + _ => panic!("Invalid state"), + }; + if arg >= Builtin::of(interp, cmd).args_slice().len() { + return Self::finish(interp, cmd, exit_code); + } - let dir = Builtin::cwd(interp, cmd); - let fd = match shell_openat(dir, path, bun_sys::O::RDONLY, 0) { - Ok(fd) => fd, - Err(e) => { - let buf = - Builtin::task_error_to_string(interp, cmd, Kind::Cat, &e).to_vec(); - // The reader was already taken to `None` above. - return Self::write_failing_error(interp, cmd, &buf, 1); - } - }; + let path = Builtin::of(interp, cmd).arg_zstr(arg); + let dir = Builtin::cwd(interp, cmd); + let fd = match shell_openat(dir, path, bun_sys::O::RDONLY, 0) { + Ok(fd) => fd, + Err(e) => match Self::input_failed(interp, cmd, &e) { + Some(y) => return y, + None => continue, + }, + }; - let evtloop = Builtin::event_loop(interp, cmd); - let interp_ptr: *mut Interpreter = interp.as_ctx_ptr(); - let reader = IOReader::init(fd, evtloop); - reader.set_interp(interp_ptr); - if let CatState::ExecFilepathArgs { - reader: slot, - chunks_done, - chunks_queued, - in_done, - out_done, - .. - } = &mut Self::state_mut(interp, cmd).state - { - *chunks_done = 0; - *chunks_queued = 0; - *in_done = false; - *out_done = false; - *slot = Some(Arc::clone(&reader)); - } - reader.add_reader(ReaderChildPtr { - node: cmd, - tag: ReaderTag::Cat, - }); - reader.start() + let reader = IOReader::init_opened(fd, interp); + if let CatState::ExecFilepathArgs { reader: slot, .. } = + &mut Self::state_mut(interp, cmd).state + { + *slot = Some(Arc::clone(&reader)); } - Branch::WaitingErr => Yield::suspended(), + reader.add_reader(ReaderChildPtr { + node: cmd, + tag: ReaderTag::Cat, + }); + return reader.start(); } } @@ -243,72 +260,26 @@ impl Cat { err: Option, ) -> Yield { if let Some(e) = err { - let errno = e.get_errno() as ExitCode; - let rchild = ReaderChildPtr { - node: cmd, - tag: ReaderTag::Cat, - }; - // Writing to stdout errored: cancel everything and finish. - // Pull the reader `Arc` out of - // state before calling `remove_reader`, then drop it. - match &mut Self::state_mut(interp, cmd).state { - CatState::ExecStdin { - in_done, - errno: st_errno, - .. - } => { - *st_errno = errno; - let was_done = core::mem::replace(in_done, true); - if !was_done { - if let BuiltinInput::Fd(r) = &Builtin::of(interp, cmd).stdin { - r.remove_reader(rchild); - } - } - } - CatState::ExecFilepathArgs { reader, .. } => { - if let Some(r) = reader.take() { - r.remove_reader(rchild); - } + // Writing failed: nothing more is read or written. The failed + // writer dropped this Cmd's chunks; the other one may hold some. + let child = ChildPtr::new(cmd, WriterTag::Builtin); + let me = Builtin::of(interp, cmd); + for out in [&me.stdout, &me.stderr] { + if let BuiltinIO::Fd(fd) = out { + fd.writer.cancel_chunks(child); } - CatState::WaitingWriteErr => {} - _ => panic!("Invalid state"), } - return Builtin::done(interp, cmd, errno); + return Self::finish(interp, cmd, e.get_errno() as ExitCode); } - let step = match &mut Self::state_mut(interp, cmd).state { - CatState::ExecStdin { - chunks_queued, - chunks_done, - in_done, - .. - } => { - *chunks_done += 1; - if *in_done && *chunks_done >= *chunks_queued { - Step::Done(0) - } else { - Step::Suspend - } - } - CatState::ExecFilepathArgs { - chunks_queued, - chunks_done, - in_done, - out_done, - .. - } => { - *chunks_done += 1; - if *chunks_done >= *chunks_queued { - *out_done = true; - } - if *in_done && *out_done { - Step::Next - } else { - Step::Suspend - } + let state = &mut Self::state_mut(interp, cmd).state; + let step = match state { + CatState::ExecStdin(progress) | CatState::ExecFilepathArgs { progress, .. } => { + progress.chunks_done += 1; + state.step() } CatState::WaitingWriteErr => Step::Done(1), - _ => panic!("Invalid state"), + CatState::Idle => panic!("Invalid state"), }; step.run(interp, cmd) } @@ -316,91 +287,48 @@ impl Cat { pub(crate) fn on_io_reader_chunk( interp: &Interpreter, cmd: NodeId, - chunk: &[u8], + chunk: bun_io::pipes::Chunk<'_>, remove: &mut bool, ) -> Yield { *remove = false; - let stdout_needs_io = Builtin::of(interp, cmd).stdout.needs_io(); - match &mut Self::state_mut(interp, cmd).state { - CatState::ExecStdin { chunks_queued, .. } - | CatState::ExecFilepathArgs { chunks_queued, .. } => { - if let Some(safeguard) = stdout_needs_io { - *chunks_queued += 1; - let child = ChildPtr::new(cmd, WriterTag::Builtin); - return Builtin::of_mut(interp, cmd) - .stdout - .enqueue(child, chunk, safeguard); - } - } - _ => panic!("Invalid state"), + if Builtin::of(interp, cmd).stdout.needs_io().is_none() { + let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &chunk); + return Yield::done(); } - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, chunk); - Yield::done() + Self::write(interp, cmd, IoKind::Stdout, chunk.take()).unwrap_or_else(Yield::done) } pub(crate) fn on_io_reader_done( interp: &Interpreter, cmd: NodeId, - err: Option, + err: Option<&bun_sys::Error>, ) -> Yield { - let errno: ExitCode = err.map(|e| e.get_errno() as ExitCode).unwrap_or(0); - let stdout_needs_io = Builtin::of(interp, cmd).stdout.needs_io().is_some(); - let mut cancel = false; - let step = match &mut Self::state_mut(interp, cmd).state { - CatState::ExecStdin { - chunks_queued, - chunks_done, - in_done, - errno: st_errno, - } => { - *st_errno = errno; - *in_done = true; - if errno != 0 { - if *chunks_done >= *chunks_queued || !stdout_needs_io { - Step::Done(errno) - } else { - cancel = true; - Step::Suspend - } - } else if *chunks_done >= *chunks_queued || !stdout_needs_io { - Step::Done(0) - } else { - Step::Suspend - } + let arg = match &mut Self::state_mut(interp, cmd).state { + CatState::ExecStdin(progress) => { + progress.in_done = true; + None } CatState::ExecFilepathArgs { - chunks_queued, - chunks_done, - in_done, - out_done, - reader, + args_start, + idx, + progress, .. } => { - *in_done = true; - if errno != 0 { - if *out_done || !stdout_needs_io { - // Drop the reader ref. - *reader = None; - Step::Done(errno) - } else { - cancel = true; - Step::Suspend - } - } else if *out_done || *chunks_done >= *chunks_queued || !stdout_needs_io { - Step::Next - } else { - Step::Suspend - } + progress.in_done = true; + Some(*args_start + *idx - 1) } - CatState::WaitingWriteErr | CatState::Idle => Step::Suspend, + CatState::WaitingWriteErr | CatState::Idle => return Yield::suspended(), }; - if cancel { - let wchild = ChildPtr::new(cmd, WriterTag::Builtin); - if let BuiltinIO::Fd(fd) = &Builtin::of(interp, cmd).stdout { - fd.writer.cancel_chunks(wchild); - } + let name: &[u8] = match arg { + Some(arg) => Builtin::of(interp, cmd).arg_bytes(arg), + None => b"stdin", + }; + if let Some(e) = err + && let Some(y) = Self::input_failed(interp, cmd, &e.with_path(name)) + { + return y; } - step.run(interp, cmd) + Self::state_mut(interp, cmd).state.step().run(interp, cmd) } } diff --git a/src/runtime/shell/builtin/cp.rs b/src/runtime/shell/builtin/cp.rs index 2c2c8b25644e..52cd45d5b319 100644 --- a/src/runtime/shell/builtin/cp.rs +++ b/src/runtime/shell/builtin/cp.rs @@ -368,15 +368,13 @@ impl OutputTaskVTable for Cp { if let Some(safeguard) = Builtin::of(interp, cmd).stdout.needs_io() { Self::state_mut(interp, cmd).output_queue.push_back(child); let childptr = ChildPtr::new(cmd, WriterTag::Builtin); - let buf = output.slice().to_vec(); - return Some( - Builtin::of_mut(interp, cmd) - .stdout - .enqueue(childptr, &buf, safeguard), - ); + return Some(Builtin::of_mut(interp, cmd).stdout.enqueue_owned( + childptr, + output.take(), + safeguard, + )); } - let buf = output.slice().to_vec(); - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); + let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, output.slice()); None } fn on_write_out(interp: &Interpreter, cmd: NodeId) { diff --git a/src/runtime/shell/builtin/dirname.rs b/src/runtime/shell/builtin/dirname.rs index 32df36e5cb5c..311f6e970bdc 100644 --- a/src/runtime/shell/builtin/dirname.rs +++ b/src/runtime/shell/builtin/dirname.rs @@ -42,7 +42,7 @@ impl Dirname { let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout - .enqueue(child, &owned, safeguard); + .enqueue_owned(child, owned, safeguard); } let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); Builtin::done(interp, cmd, 0) diff --git a/src/runtime/shell/builtin/echo.rs b/src/runtime/shell/builtin/echo.rs index 609caab921c2..5b8966d3dcb3 100644 --- a/src/runtime/shell/builtin/echo.rs +++ b/src/runtime/shell/builtin/echo.rs @@ -5,10 +5,6 @@ use crate::shell::yield_::Yield; #[derive(Default)] pub(crate) struct Echo { - /// The fully-rendered output (joined argv + optional trailing newline). - /// Kept on the state so the async IOWriter path can borrow it across - /// yields. - pub(crate) output: Vec, state: State, } @@ -87,18 +83,15 @@ impl Echo { } out }; - Self::state_mut(interp, cmd).output = output; if let Some(safeguard) = Builtin::of(interp, cmd).stdout.needs_io() { Self::state_mut(interp, cmd).state = State::WaitingIo; - let buf = Self::state_mut(interp, cmd).output.clone(); let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout - .enqueue(child, &buf, safeguard); + .enqueue_owned(child, output, safeguard); } - let buf = Self::state_mut(interp, cmd).output.clone(); - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); + let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &output); Self::state_mut(interp, cmd).state = State::Done; Builtin::done(interp, cmd, 0) } @@ -110,11 +103,10 @@ impl Echo { err: Option, ) -> Yield { Self::state_mut(interp, cmd).state = State::Done; - Builtin::done( - interp, - cmd, - err.map(|e| e.errno as crate::shell::ExitCode).unwrap_or(0), - ) + if let Some(_err) = err { + return Builtin::done(interp, cmd, 1); + } + Builtin::done(interp, cmd, 0) } } diff --git a/src/runtime/shell/builtin/export.rs b/src/runtime/shell/builtin/export.rs index 8e03dac1ed17..44709fe6f61b 100644 --- a/src/runtime/shell/builtin/export.rs +++ b/src/runtime/shell/builtin/export.rs @@ -69,7 +69,7 @@ impl Export { let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout - .enqueue(child, &buf, safeguard); + .enqueue_owned(child, buf, safeguard); } let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); Builtin::done(interp, cmd, 0) diff --git a/src/runtime/shell/builtin/ls.rs b/src/runtime/shell/builtin/ls.rs index 635157a978cc..4341108ba895 100644 --- a/src/runtime/shell/builtin/ls.rs +++ b/src/runtime/shell/builtin/ls.rs @@ -319,15 +319,13 @@ impl OutputTaskVTable for Ls { exec.output_queue.push_back(child); } let childptr = ChildPtr::new(cmd, WriterTag::Builtin); - let buf = output.slice().to_vec(); - return Some( - Builtin::of_mut(interp, cmd) - .stdout - .enqueue(childptr, &buf, safeguard), - ); + return Some(Builtin::of_mut(interp, cmd).stdout.enqueue_owned( + childptr, + output.take(), + safeguard, + )); } - let buf = output.slice().to_vec(); - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); + let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, output.slice()); None } fn on_write_out(interp: &Interpreter, cmd: NodeId) { diff --git a/src/runtime/shell/builtin/mkdir.rs b/src/runtime/shell/builtin/mkdir.rs index a50d8b9aca49..64d3c39830e7 100644 --- a/src/runtime/shell/builtin/mkdir.rs +++ b/src/runtime/shell/builtin/mkdir.rs @@ -234,15 +234,13 @@ impl OutputTaskVTable for Mkdir { exec.output_queue.push_back(child); } let childptr = ChildPtr::new(cmd, WriterTag::Builtin); - let buf = output.slice().to_vec(); - return Some( - Builtin::of_mut(interp, cmd) - .stdout - .enqueue(childptr, &buf, safeguard), - ); + return Some(Builtin::of_mut(interp, cmd).stdout.enqueue_owned( + childptr, + output.take(), + safeguard, + )); } - let buf = output.slice().to_vec(); - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); + let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, output.slice()); None } @@ -328,6 +326,7 @@ impl ShellMkdirTask { recursive: this.opts.parents, mode: fs_args::Mkdir::DEFAULT_MODE, always_return_none: true, + as_written: false, }; if this.opts.parents { diff --git a/src/runtime/shell/builtin/mv.rs b/src/runtime/shell/builtin/mv.rs index 2d143b92a882..7e73e0fb5754 100644 --- a/src/runtime/shell/builtin/mv.rs +++ b/src/runtime/shell/builtin/mv.rs @@ -507,6 +507,14 @@ impl ShellMvBatchedTask { ) -> Result<(), bun_sys::Error> { use bun_sys::{Dir, E, File, O, S, Tag}; + // `NUL` opens as a directory, and what is copied below it is gone. + #[cfg(windows)] + if !matches!(File::borrow(&dst_dir).kind(), Ok(bun_sys::FileKind::File)) { + return Err( + bun_sys::Error::from_code(E::ENOTDIR, Tag::rename).with_path(dst.as_bytes()) + ); + } + let st = bun_sys::lstatat(src_dir, src)?; let mode = st.st_mode as bun_core::Mode; diff --git a/src/runtime/shell/builtin/pwd.rs b/src/runtime/shell/builtin/pwd.rs index 0fa42dbf8061..b5f6c9e9574e 100644 --- a/src/runtime/shell/builtin/pwd.rs +++ b/src/runtime/shell/builtin/pwd.rs @@ -54,7 +54,7 @@ impl Pwd { let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout - .enqueue(child, &cwd, safeguard); + .enqueue_owned(child, cwd, safeguard); } let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &cwd); Self::state_mut(interp, cmd).state = State::Done; diff --git a/src/runtime/shell/builtin/rm.rs b/src/runtime/shell/builtin/rm.rs index b8d5421f3097..f20076fd0ff5 100644 --- a/src/runtime/shell/builtin/rm.rs +++ b/src/runtime/shell/builtin/rm.rs @@ -421,7 +421,7 @@ impl Rm { let child = ChildPtr::new(cmd, WriterTag::Builtin); Builtin::of_mut(interp, cmd) .stderr - .enqueue(child, &s, safeguard) + .enqueue_owned(child, s, safeguard) .run(interp); return; } @@ -477,7 +477,7 @@ impl Rm { let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout - .enqueue(child, &buf, safeguard); + .enqueue_owned(child, buf, safeguard); } let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); let done = match &mut Self::state_mut(interp, cmd).state { diff --git a/src/runtime/shell/builtin/seq.rs b/src/runtime/shell/builtin/seq.rs index 66d3270438bf..337a6426f3aa 100644 --- a/src/runtime/shell/builtin/seq.rs +++ b/src/runtime/shell/builtin/seq.rs @@ -192,7 +192,7 @@ impl Seq { let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout - .enqueue(child, &out, safeguard); + .enqueue_owned(child, out, safeguard); } let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &out); Builtin::done(interp, cmd, 0) diff --git a/src/runtime/shell/builtin/touch.rs b/src/runtime/shell/builtin/touch.rs index ca306ceea23d..e31c3d3e9231 100644 --- a/src/runtime/shell/builtin/touch.rs +++ b/src/runtime/shell/builtin/touch.rs @@ -211,15 +211,13 @@ impl OutputTaskVTable for Touch { exec.output_queue.push_back(child); } let childptr = ChildPtr::new(cmd, WriterTag::Builtin); - let buf = output.slice().to_vec(); - return Some( - Builtin::of_mut(interp, cmd) - .stdout - .enqueue(childptr, &buf, safeguard), - ); + return Some(Builtin::of_mut(interp, cmd).stdout.enqueue_owned( + childptr, + output.take(), + safeguard, + )); } - let buf = output.slice().to_vec(); - let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, &buf); + let _ = Builtin::write_no_io(interp, cmd, IoKind::Stdout, output.slice()); None } fn on_write_out(interp: &Interpreter, cmd: NodeId) { @@ -280,8 +278,7 @@ impl ShellTouchTask { ) }; - // Call the bun_sys layer directly (uv_fs_utime on Windows) to avoid - // the heavyweight NodeFS state. + // Call the bun_sys layer directly to avoid the heavyweight NodeFS state. let milliseconds = bun_core::time::milli_timestamp(); let atime = bun_sys::TimeLike { sec: milliseconds.div_euclid(1_000), diff --git a/src/runtime/shell/builtin/which.rs b/src/runtime/shell/builtin/which.rs index 27a3e606443c..8fb8bb1bc940 100644 --- a/src/runtime/shell/builtin/which.rs +++ b/src/runtime/shell/builtin/which.rs @@ -178,8 +178,8 @@ impl Which { _: usize, e: Option, ) -> Yield { - if let Some(err) = e { - return Builtin::done(interp, cmd, err.errno as crate::shell::ExitCode); + if let Some(_err) = e { + return Builtin::done(interp, cmd, 1); } match Self::state_mut(interp, cmd).state { State::OneArg => Builtin::done(interp, cmd, 1), diff --git a/src/runtime/shell/builtin/yes.rs b/src/runtime/shell/builtin/yes.rs index 32a019ccc1c6..ea320a1e4c96 100644 --- a/src/runtime/shell/builtin/yes.rs +++ b/src/runtime/shell/builtin/yes.rs @@ -23,6 +23,8 @@ pub(crate) struct Yes { /// out to ~BUFSIZ. pub(crate) buffer: Vec, pub(crate) buffer_used: usize, + /// Chunks that completed in place since the event loop last had a turn. + pub(crate) chunks_since_bounce: u8, /// Populated in `start()`. pub task: Option, } @@ -85,8 +87,34 @@ impl Yes { Self::write_no_io_loop(interp, cmd) } - /// Write 4 chunks then bounce to the event loop so we don't hog the main - /// thread. + /// A burst of chunks the event loop gets a turn after: a sink that takes a + /// chunk without waiting (no IO, a disk file) completes it inside this + /// call, so nothing else would ever run. + const CHUNKS_PER_BOUNCE: u8 = 4; + + /// Carry on after the event loop has had its turn. + fn resume(interp: &Interpreter, cmd: NodeId) -> Yield { + match Builtin::of(interp, cmd).stdout.needs_io() { + Some(safeguard) => Self::enqueue_chunk(interp, cmd, safeguard), + None => Self::write_no_io_loop(interp, cmd), + } + } + + /// Bounce back via the event loop so we don't block the main thread. + fn bounce(interp: &Interpreter, cmd: NodeId) -> Yield { + let task: *mut YesTask = Self::state_mut(interp, cmd) + .task + .as_mut() + .expect("YesTask set in start()"); + // SAFETY: `task` was set in `start()`; `Yes` lives in a `Box` inside + // the interpreter arena, so the address is stable across the enqueue + // and the later main-thread callback. + unsafe { YesTask::enqueue(task) }; + Yield::suspended() + } + + /// Write a burst of chunks then bounce to the event loop so we don't hog + /// the main thread. fn write_no_io_loop(interp: &Interpreter, cmd: NodeId) -> Yield { // Split-borrow the Cmd so the tiled buffer (in `impl_`) and `stdout` // are accessible simultaneously — the buffer is written zero-copy, @@ -100,7 +128,7 @@ impl Yes { let (stdout, yes) = Self::split_stdout_state(me); let chunk = &yes.buffer[..yes.buffer_used]; let mut err = None; - for _ in 0..4 { + for _ in 0..Self::CHUNKS_PER_BOUNCE { // SAFETY: `shell` is `cmd_node.base.shell`, live for the Cmd. if let Err(e) = unsafe { stdout.write_no_io_to(shell, chunk) } { err = Some(e); @@ -119,18 +147,7 @@ impl Yes { .to_vec(); return Self::write_failing_error(interp, cmd, &buf, 1); } - // Bounce back via the event loop so we don't block the main thread. - let task: *mut YesTask = Self::state_mut(interp, cmd) - .task - .as_mut() - .expect("YesTask set in start()"); - // SAFETY: `task` was set in `start()`; `Yes` lives in a `Box` inside - // the interpreter arena, so the address is stable across the enqueue - // and the later main-thread callback. `enqueue` ticks the event loop - // and may re-enter shell dispatch — we hold no `&mut` derived from - // `interp` across the call. - unsafe { YesTask::enqueue(task) }; - Yield::suspended() + Self::bounce(interp, cmd) } fn enqueue_chunk( @@ -142,7 +159,12 @@ impl Yes { // `stdout` and `impl_` are disjoint fields of `Builtin` — split-borrow // so the tiled buffer is enqueued zero-copy. let (stdout, yes) = Self::split_stdout_state(Builtin::of_mut(interp, cmd)); - stdout.enqueue(child, &yes.buffer[..yes.buffer_used], safeguard) + let next = stdout.enqueue(child, &yes.buffer[..yes.buffer_used], safeguard); + if matches!(next, Yield::Suspended) { + // The event loop completes this chunk, so it gets its turn. + yes.chunks_since_bounce = 0; + } + next } fn write_failing_error( @@ -169,6 +191,12 @@ impl Yes { return Builtin::done(interp, cmd, 1); } debug_assert!(Builtin::of(interp, cmd).stdout.needs_io().is_some()); + let me = Self::state_mut(interp, cmd); + me.chunks_since_bounce += 1; + if me.chunks_since_bounce == Self::CHUNKS_PER_BOUNCE { + me.chunks_since_bounce = 0; + return Self::bounce(interp, cmd); + } Self::enqueue_chunk(interp, cmd, OutputNeedsIOSafeGuard::OutputNeedsIo) } @@ -186,8 +214,8 @@ impl Yes { // `buffer: Vec` drops with the owning `Box`; no explicit `Drop` impl // needed (PORTING.md §Allocators). -/// Re-queues `yes` onto the event loop after a burst of no-IO writes so we -/// don't block the main thread forever. +/// Re-queues `yes` onto the event loop after a burst of writes so we don't +/// block the main thread forever. #[repr(C)] pub(crate) struct YesTask { /// Back-ref to the owning [`Interpreter`]. @@ -225,7 +253,6 @@ impl YesTask { owner.enqueue_task_after_yield(bun_jsc::Task::init(this)); } EventLoopHandle::Mini(mut mini) => { - (*mini.loop_).tick(); let at = core::ptr::NonNull::new_unchecked(match &mut (*this).concurrent_task { EventLoopTask::Mini(at) => { @@ -246,7 +273,7 @@ impl YesTask { pub(crate) fn run_from_main_thread(this: &Self) { // SAFETY: `interp` was set in `Yes::start` and outlives the task. let (interp, cmd) = unsafe { (&*this.interp, this.cmd) }; - Yes::write_no_io_loop(interp, cmd).run(interp); + Yes::resume(interp, cmd).run(interp); } /// Signature matches @@ -254,7 +281,15 @@ impl YesTask { /// callback shape (`fn(*mut T, *mut ())`). fn run_from_main_thread_mini(this: *mut Self, _: *mut ()) { // SAFETY: dispatch contract — `this` is the live task previously passed - // to `enqueue`; see `run_from_main_thread`. - Self::run_from_main_thread(unsafe { &*this }) + // to `enqueue`; see `run_from_main_thread`. `mini` is a live event-loop + // backref, and tasks run outside the loop's own dispatch. + unsafe { + // The mini loop looks for I/O only when it has no task to run, and + // this one is queued again every time it runs. + if let EventLoopHandle::Mini(mini) = (*this).evtloop { + (*mini.loop_).tick_without_idle(); + } + Self::run_from_main_thread(&*this) + } } } diff --git a/src/runtime/shell/interpreter.rs b/src/runtime/shell/interpreter.rs index d1735dad6f74..2d28a1cfcdbb 100644 --- a/src/runtime/shell/interpreter.rs +++ b/src/runtime/shell/interpreter.rs @@ -225,7 +225,7 @@ node_accessors! { // Small types // ──────────────────────────────────────────────────────────────────────────── -pub(crate) type ExitCode = u16; +pub(crate) type ExitCode = u32; pub(crate) type Pipe = [Fd; 2]; #[repr(u8)] @@ -291,6 +291,11 @@ pub(crate) struct Interpreter { pub(crate) root_shell: JsCell, pub(crate) root_io: JsCell, + /// Every live `IOWriter` made for this interpreter; each points back at it. + /// A Windows write in flight keeps its writer alive on its own, so `Drop` + /// orphans the ones still listed. + io_writers: JsCell>, + pub(crate) has_pending_activity: AtomicU32, pub(crate) keep_alive: JsCell, /// The `Bun.ModuleGraph` context whose script started this (`run_from_js`), if any. Every @@ -318,6 +323,16 @@ pub(crate) struct Interpreter { pub(crate) command_ctx: *mut bun_options_types::context::ContextData, } +impl Drop for Interpreter { + fn drop(&mut self) { + for writer in self.io_writers.take() { + // SAFETY: a listed writer is live: it takes itself off the list + // when it drops. + unsafe { (*writer).orphan() }; + } + } +} + #[repr(transparent)] #[derive(Clone, Copy, Default)] pub(crate) struct InterpreterFlags(u8); @@ -526,20 +541,9 @@ impl Interpreter { // ── stdin ────────────────────────────────────────────────────────── log!("Duping stdin"); let stdin_fd_res = if bun_core::output::stdio::is_stdin_null() { - #[cfg(unix)] - { - bun_sys::open( - bun_core::ZStr::from_static(b"/dev/null\0"), - bun_sys::O::RDONLY, - 0, - ) - } - #[cfg(windows)] - { - bun_sys::open(bun_core::ZStr::from_static(b"NUL\0"), bun_sys::O::RDONLY, 0) - } + open_null_device() } else { - shell_dup(Fd::stdin()) + bun_sys::dup(Fd::stdin()) }; let stdin_fd = match stdin_fd_res { Ok(fd) => fd, @@ -549,8 +553,6 @@ impl Interpreter { } }; - let stdin_reader = IOReader::init(stdin_fd, event_loop); - // ── assemble ─────────────────────────────────────────────────────── let interpreter = Box::new(Interpreter { nodes: JsCell::new(Vec::new()), @@ -569,7 +571,8 @@ impl Interpreter { cwd_fd, }), root_io: JsCell::new(IO { - stdin: crate::shell::io::InKind::Fd(stdin_reader), + // Filled in below: the reader points back at the interpreter. + stdin: crate::shell::io::InKind::Ignore, // By default stdout/stderr should be IOWriters on dup'd // stdout/stderr, but if the user later calls `.setQuiet(true)` // that work is wasted. So they start as `.pipe` and `run()` @@ -577,6 +580,7 @@ impl Interpreter { stdout: crate::shell::io::OutKind::Pipe, stderr: crate::shell::io::OutKind::Pipe, }), + io_writers: JsCell::new(Vec::new()), has_pending_activity: AtomicU32::new(0), keep_alive: JsCell::new(bun_io::KeepAlive::default()), context: Cell::new(None), @@ -592,13 +596,10 @@ impl Interpreter { vm_args_utf8: JsCell::new(Vec::new()), command_ctx: ctx, }); - // Wire the interpreter backref into root stdin so async poll - // callbacks can drive `Yield::run`. - let interp_ptr: *mut Interpreter = Interpreter::as_ctx_ptr(&interpreter); - if let crate::shell::io::InKind::Fd(ref r) = interpreter.root_io.get().stdin { - // SAFETY: `interp_ptr` is the live `Interpreter` just constructed. - r.set_interp(interp_ptr); - } + let stdin_reader = IOReader::init(stdin_fd, &interpreter); + interpreter + .root_io + .with_mut(|io| io.stdin = crate::shell::io::InKind::Fd(stdin_reader)); // ── optional cwd override ─────────────────────────────────────────── if let Some(c) = cwd_ { @@ -619,8 +620,10 @@ impl Interpreter { } /// Full teardown for the standalone (`MiniEventLoop`) path. Drops root IO - /// refcounts, frees the root shell env, and consumes the box. - fn deinit_from_exec(self) { + /// refcounts, frees the root shell env, and consumes the box. Through the + /// box: readers and writers point back at the interpreter where it is. + #[allow(clippy::boxed_local)] + fn deinit_from_exec(self: Box) { log!("deinit interpreter"); self.this_jsvalue.set(crate::jsc::JSValue::ZERO); // `root_io` holds `Arc`/`Arc`; replacing with @@ -840,6 +843,20 @@ impl Interpreter { std::ptr::from_ref::(self).cast_mut() } + /// `IOWriter::init`: `writer` points back at this interpreter from here on. + pub(crate) fn register_io_writer(&self, writer: *const IOWriter) { + self.io_writers.with_mut(|writers| writers.push(writer)); + } + + /// `IOWriter::drop`. + pub(crate) fn forget_io_writer(&self, writer: *const IOWriter) { + self.io_writers.with_mut(|writers| { + if let Some(idx) = writers.iter().position(|w| core::ptr::eq(*w, writer)) { + writers.swap_remove(idx); + } + }); + } + /// Read-modify-write the packed `Cell` through `&self`. #[inline] fn update_flags(&self, f: impl FnOnce(&mut InterpreterFlags)) { @@ -1189,7 +1206,7 @@ impl Interpreter { let stdout_fd = if bun_core::output::stdio::is_stdout_null() { open_null_device()? } else { - shell_dup(Fd::stdout())? + bun_sys::dup(Fd::stdout())? }; // ── dup stderr (errdefer closes stdout on failure) ──────────────── @@ -1197,7 +1214,7 @@ impl Interpreter { let stderr_fd_res = if bun_core::output::stdio::is_stderr_null() { open_null_device() } else { - shell_dup(Fd::stderr()) + bun_sys::dup(Fd::stderr()) }; let stderr_fd = match stderr_fd_res { Ok(fd) => fd, @@ -1207,27 +1224,16 @@ impl Interpreter { } }; - let interp_ptr: *mut Interpreter = self.as_ctx_ptr(); let stdout_writer = IOWriter::init( stdout_fd, - crate::shell::io_writer::Flags { - pollable: is_pollable(stdout_fd), - ..Default::default() - }, - event_loop, + crate::shell::io_writer::Flags::unclassified(), + self, ); - // SAFETY: `interp_ptr` is the live `Interpreter` being initialized. - stdout_writer.set_interp(interp_ptr); let stderr_writer = IOWriter::init( stderr_fd, - crate::shell::io_writer::Flags { - pollable: is_pollable(stderr_fd), - ..Default::default() - }, - event_loop, + crate::shell::io_writer::Flags::unclassified(), + self, ); - // SAFETY: `interp_ptr` is the live `Interpreter` being initialized. - stderr_writer.set_interp(interp_ptr); // On the JS event loop, hook captured buffers so the JS // `Bun.$` API can read stdout/stderr after completion. The mini path @@ -1322,7 +1328,7 @@ impl Interpreter { global_this, JSValue::UNDEFINED, &[ - JSValue::js_number_from_int32(i32::from(exit_code)), + JSValue::js_number(f64::from(exit_code)), buffered_stdout, buffered_stderr, ], @@ -1562,9 +1568,7 @@ impl Interpreter { // resources: deinit every live `Cmd` (kills the child, frees // the `ShellSubprocess`, readers, redirection fd). Slots stay // occupied so the env walk below still sees pipeline-duped - // Cmd envs. Windows: leak-over-UAF, see - // `ShellSubprocess::abort_after_failed_start`. - #[cfg(not(windows))] + // Cmd envs. { let node_count = this.nodes.get().len(); for i in 0..node_count { @@ -2249,48 +2253,32 @@ pub(crate) use crate::shell::states::assigns::AssignCtx; /// writable: `setup_io_before_run` installs the result as the stdout/stderr /// `IOWriter` target (and `init` uses it for stdin), so `RDWR` covers both. fn open_null_device() -> bun_sys::Result { - #[cfg(unix)] - { - bun_sys::open( - bun_core::ZStr::from_static(b"/dev/null\0"), - bun_sys::O::RDWR, - 0, - ) - } - #[cfg(windows)] - { - // Windows NUL is bidirectional regardless of the open flags. - bun_sys::open(bun_core::ZStr::from_static(b"nul\0"), bun_sys::O::RDWR, 0) - } + // Windows NUL is bidirectional regardless of the open flags. + const NULL_DEVICE: &bun_core::ZStr = if cfg!(windows) { + bun_core::zstr!("nul") + } else { + bun_core::zstr!("/dev/null") + }; + bun_sys::open(NULL_DEVICE, bun_sys::O::RDWR, 0) } -/// Note: takes a pre-cached `mode` from `event_loop.stdout().data -/// .file.mode`; `EventLoopHandle` is still a shim, so we `fstat` the (already -/// dup'd) fd here instead. On `fstat` failure we conservatively return `false` -/// (non-pollable → synchronous write path), matching Windows behavior. -fn is_pollable(fd: Fd) -> bool { - #[cfg(windows)] - { - let _ = fd; - false - } - #[cfg(unix)] +/// `false` when `fstat` fails (non-pollable → synchronous write path). +#[cfg(not(windows))] +pub(crate) fn is_pollable(fd: Fd) -> bool { + let mode = match bun_sys::fstat(fd) { + Ok(st) => st.st_mode, + Err(_) => return false, + }; + let fmt = mode & libc::S_IFMT; + #[cfg(target_os = "macos")] { - let mode = match bun_sys::fstat(fd) { - Ok(st) => st.st_mode, - Err(_) => return false, - }; - let fmt = mode & libc::S_IFMT; - #[cfg(target_os = "macos")] - { - // macOS allows polling regular files, but our IOWriter has a - // better dedicated path for them — exclude S_ISREG explicitly. - if fmt == libc::S_IFREG { - return false; - } + // macOS allows polling regular files, but our IOWriter has a + // better dedicated path for them — exclude S_ISREG explicitly. + if fmt == libc::S_IFREG { + return false; } - fmt == libc::S_IFIFO || fmt == libc::S_IFSOCK || bun_sys::isatty(fd) } + fmt == libc::S_IFIFO || fmt == libc::S_IFSOCK || bun_sys::isatty(fd) } /// Same test as [`is_pollable`] minus the `isatty()` check — used when the @@ -2326,23 +2314,6 @@ pub(crate) fn closefd(fd: Fd) { let _ = fd.close_allowing_bad_file_descriptor(None); } -/// Same as `bun_sys::dup` on POSIX; on Windows the duped handle is converted -/// to a libuv-owned fd via `makeLibUVOwnedForSyscall(.dup, .close_on_fail)` so -/// the IOWriter/IOReader uv-based async write/read paths receive a uv fd -/// instead of a raw NT handle. -pub(crate) fn shell_dup(fd: Fd) -> bun_sys::Result { - #[cfg(windows)] - { - use bun_sys::FdExt; - bun_sys::dup(fd)? - .make_lib_uv_owned_for_syscall(bun_sys::Tag::dup, bun_sys::ErrorCase::CloseOnFail) - } - #[cfg(not(windows))] - { - bun_sys::dup(fd) - } -} - /// Windows-only: rewrite shell paths so POSIX-absolute `/foo` resolves onto /// `dirfd`'s drive root, `/dev/null` maps to `NUL`, and relative paths are /// joined against `dirfd`'s real path. Returns a NUL-terminated slice that @@ -2418,9 +2389,47 @@ pub(crate) fn shell_lstatat(dir: Fd, path_: &bun_core::ZStr) -> bun_sys::Result< /// POSIX: `bun_sys::openat` with the error tagged `.with_path(path)`. /// Windows: for `O_DIRECTORY` opens, rewrite POSIX-absolute paths via -/// `shell_get_path` and use `openDirAtWindowsA(.iterable=true)` + -/// `makeLibUVOwnedForSyscall`; for file opens, resolve via `shell_get_path` -/// then `bun_sys::open`. +/// `shell_get_path` and use `openDirAtWindowsA(.iterable=true)`; for file +/// opens, a path Win32 would not rewrite is opened relative to `dir`, anything +/// else is resolved via `shell_get_path` and then `bun_sys::open`. +/// Whether `path` is relative and names the same file to Win32 as to +/// `NtCreateFile` relative to a directory, which takes every name as written. +/// Win32 drops trailing dots and spaces, resolves `.` and `..`, refuses a +/// trailing separator on a file, and reads `a:s` as a stream or a drive. +#[cfg(windows)] +fn is_same_name_to_win32(path: &[u8]) -> bool { + !path + .iter() + .any(|&c| c < b' ' || matches!(c, b':' | b'<' | b'>' | b'"' | b'|' | b'?' | b'*')) + && bun_core::strings::split_any(path, b"/\\").all(|name| { + !matches!(name.last(), None | Some(b'.' | b' ')) && !may_name_dos_device(name) + }) +} + +/// Whether Win32 may read `name` as a DOS device (`nul`, `Con`, `com1.txt`, +/// `aux .c`). Which of these it does depends on the version of Windows. +#[cfg(windows)] +fn may_name_dos_device(name: &[u8]) -> bool { + let stem = + &name[..bun_core::strings::index_of_char(name, b'.').map_or(name.len(), |i| i as usize)]; + let stem = stem.trim_ascii_end(); + let is = |device: &[u8]| stem.eq_ignore_ascii_case(device); + // The digit may be a superscript one, two or three. + let numbered = |device: &[u8]| { + stem.len() > 3 + && stem[..3].eq_ignore_ascii_case(device) + && matches!(&stem[3..], [b'0'..=b'9'] | [0xc2, 0xb9 | 0xb2 | 0xb3]) + }; + is(b"nul") + || is(b"con") + || is(b"prn") + || is(b"aux") + || is(b"conin$") + || is(b"conout$") + || numbered(b"com") + || numbered(b"lpt") +} + pub(crate) fn shell_openat( dir: Fd, path: &bun_core::ZStr, @@ -2429,7 +2438,6 @@ pub(crate) fn shell_openat( ) -> bun_sys::Result { #[cfg(windows)] { - use bun_sys::FdExt; if flags & bun_sys::O::DIRECTORY != 0 { if bun_paths::Platform::Posix.is_absolute(path.as_bytes()) { let mut buf = bun_paths::path_buffer_pool::get(); @@ -2443,11 +2451,7 @@ pub(crate) fn shell_openat( ..Default::default() }, ) - .map_err(|e| e.with_path(path.as_bytes()))? - .make_lib_uv_owned_for_syscall( - bun_sys::Tag::open, - bun_sys::ErrorCase::CloseOnFail, - ); + .map_err(|e| e.with_path(path.as_bytes())); } return bun_sys::open_dir_at_windows_a( dir, @@ -2458,13 +2462,14 @@ pub(crate) fn shell_openat( ..Default::default() }, ) - .map_err(|e| e.with_path(path.as_bytes()))? - .make_lib_uv_owned_for_syscall(bun_sys::Tag::open, bun_sys::ErrorCase::CloseOnFail); + .map_err(|e| e.with_path(path.as_bytes())); + } + let bytes = path.as_bytes(); + if is_same_name_to_win32(bytes) { + return bun_sys::openat(dir, path, flags, perm).map_err(|e| e.with_path(bytes)); } let mut buf = bun_paths::path_buffer_pool::get(); let p = shell_get_path(dir, path, &mut buf)?; - // No `makeLibUVOwnedForSyscall` here: `bun_sys::open` on Windows - // routes through `sys_uv` and already yields a uv-owned fd. return bun_sys::open(p, flags, perm); } #[cfg(not(windows))] @@ -2587,6 +2592,13 @@ impl OutputSrc { OutputSrc::Arrlist(v) => v.as_slice(), } } + + /// The bytes, for the one write that consumes them. + pub(crate) fn take(&mut self) -> Vec { + match self { + OutputSrc::Arrlist(v) => core::mem::take(v), + } + } } #[derive(Clone, Copy, PartialEq, Eq)] diff --git a/src/runtime/shell/states/Cmd.rs b/src/runtime/shell/states/Cmd.rs index aaf8a1f47af4..1c4814cfcc82 100644 --- a/src/runtime/shell/states/Cmd.rs +++ b/src/runtime/shell/states/Cmd.rs @@ -512,7 +512,7 @@ impl Cmd { }; // CreateProcessW runs `.bat`/`.cmd` files through `cmd.exe`, which // re-tokenizes the command line with shell metacharacter rules - // (BatBadBut). libuv's MSVCRT-style quoting cannot make that safe, so + // (BatBadBut). MSVCRT-style quoting cannot make that safe, so // reject arguments that cmd.exe would reinterpret. if cfg!(windows) && bun_which::is_batch_file(&resolved) { let unsafe_arg: Option> = interp @@ -874,7 +874,6 @@ impl Cmd { /// [`Self::deinit`] for the VM-shutdown finalizer: defuses the /// `> ${arraybuffer}` unpins first — the heap sweep already deleted the /// `JSC::ArrayBuffer` impls they would write to. - #[cfg(not(windows))] pub(crate) fn deinit_from_finalizer(interp: &Interpreter, this: NodeId) { { let me = interp.as_cmd_mut(this); @@ -936,7 +935,6 @@ impl Cmd { (*child).unref::(); // Stop any still-active stdio before the drop (only the // VM-shutdown path reaches here in flight). - #[cfg(not(windows))] ShellSubprocess::deinit_in_flight_io(child); drop(bun_core::heap::take(child)); } diff --git a/src/runtime/shell/states/CondExpr.rs b/src/runtime/shell/states/CondExpr.rs index a4b72b3c0101..82872c9c98f1 100644 --- a/src/runtime/shell/states/CondExpr.rs +++ b/src/runtime/shell/states/CondExpr.rs @@ -301,7 +301,7 @@ impl CondExpr { let child = io_writer::ChildPtr::new(this, io_writer::WriterTag::CondExpr); // `OutKind::Fd` guaranteed by `needs_io()`. if let OutKind::Fd(fd) = &interp.as_condexpr(this).io.stderr { - return fd.writer.enqueue(child, fd.captured, &buf); + return fd.writer.enqueue_owned(child, fd.captured, buf); } unreachable!() } diff --git a/src/runtime/shell/states/Pipeline.rs b/src/runtime/shell/states/Pipeline.rs index 51a80d441d75..50859c3f1848 100644 --- a/src/runtime/shell/states/Pipeline.rs +++ b/src/runtime/shell/states/Pipeline.rs @@ -46,6 +46,48 @@ impl Default for PipelineState { } } +/// Whether `item` runs as a builtin whatever its words expand to: a command +/// whose name is a literal word that names one. Any other stage may spawn a +/// child process, which inherits the stage's ends of the pipes. +#[cfg(windows)] +fn is_certainly_builtin(item: &ast::PipelineItem) -> bool { + let ast::PipelineItem::Cmd(cmd) = item else { + return false; + }; + matches!( + cmd.name_and_args.first(), + Some(ast::Atom::Simple(ast::SimpleAtom::Text(name))) + if crate::shell::builtin::Kind::from_argv0(name).is_some() + ) +} + +/// The pipe between two neighbouring stages. An end that only a builtin uses +/// is overlapped and runs on the loop's port; an end that a child process may +/// inherit is synchronous, which is what a program expects of its stdio. +#[cfg(windows)] +fn create_pipe(writer_is_builtin: bool, reader_is_builtin: bool) -> bun_sys::Result { + use bun_spawn_sys::windows::stdio::{ChildPipe, create_pipe_pair}; + if !writer_is_builtin && !reader_is_builtin { + return bun_sys::pipe(); + } + // `parent` is always overlapped; `child` is the end the other stage gets. + let pair = create_pipe_pair(ChildPipe { + readable: writer_is_builtin, + writable: !writer_is_builtin, + overlapped: writer_is_builtin && reader_is_builtin, + }) + .map_err(|code| bun_spawn_sys::windows::win32::sys_error(code, bun_sys::Tag::pipe))?; + let (read, write) = if writer_is_builtin { + (pair.child, pair.parent) + } else { + (pair.parent, pair.child) + }; + Ok([ + bun_sys::Fd::from_system(read), + bun_sys::Fd::from_system(write), + ]) +} + impl Pipeline { pub(crate) fn init( interp: &Interpreter, @@ -134,9 +176,9 @@ impl Pipeline { /// failed pipe or dup finishes the pipeline (`Some(yield)`) while no child /// runs a subtree that `deinit` cannot reach. fn setup_commands(interp: &Interpreter, this: NodeId) -> Option { - let (node, parent_shell, evtloop) = { + let (node, parent_shell) = { let me = interp.as_pipeline(this); - (me.node, me.base.shell, interp.event_loop) + (me.node, me.base.shell) }; let items: &[ast::PipelineItem] = node.items; let cmd_count = items @@ -149,16 +191,24 @@ impl Pipeline { return Some(Self::finish(interp, this, 0)); } + // By runnable child, like `pipes[]` and `cmds[]`. + #[cfg(windows)] + let runs_builtin: Vec = items + .iter() + .filter(|it| !matches!(it, ast::PipelineItem::Assigns(_))) + .map(is_certainly_builtin) + .collect(); + let mut pipes: Vec = Vec::with_capacity(cmd_count - 1); - for _ in 0..cmd_count - 1 { + while pipes.len() < cmd_count - 1 { // On POSIX use a // UNIX stream socketpair via `socketpairForShell` — on macOS // that variant intentionally skips SO_NOSIGPIPE so the // subprocess writing to a closed read end is killed by SIGPIPE // (like a real shell) instead of seeing EPIPE and printing - // "Broken pipe" to stderr; on Windows use pipe(). + // "Broken pipe" to stderr. #[cfg(windows)] - let r = bun_sys::pipe(); + let r = create_pipe(runs_builtin[pipes.len()], runs_builtin[pipes.len() + 1]); #[cfg(unix)] let r = bun_sys::socketpair_for_shell(libc::AF_UNIX, libc::SOCK_STREAM, 0, false); match r { @@ -178,7 +228,6 @@ impl Pipeline { } } - let interp_ptr: *mut Interpreter = interp.as_ctx_ptr(); let mut cmds: Vec = Vec::with_capacity(cmd_count); for item in items { if matches!(item, ast::PipelineItem::Assigns(_)) { @@ -192,25 +241,29 @@ impl Pipeline { let stdin = if cmd_idx == 0 { me.io.stdin.clone() } else { - let r = IOReader::init(pipes[cmd_idx - 1][0], evtloop); - r.set_interp(interp_ptr); - InKind::Fd(r) + let fd = pipes[cmd_idx - 1][0]; + #[cfg(windows)] + let reader = if runs_builtin[cmd_idx] { + IOReader::init_overlapped_pipe(fd, interp) + } else { + IOReader::init_created_pipe(fd, interp) + }; + #[cfg(unix)] + let reader = IOReader::init_created_pipe(fd, interp); + InKind::Fd(reader) }; let stdout = if cmd_idx == cmd_count - 1 { me.io.stdout.clone() } else { - // `is_socket` is set on POSIX — the POSIX - // pipe is actually a socketpair end (see above). - let w = IOWriter::init( - pipes[cmd_idx][1], - io_writer::Flags { - pollable: true, - is_socket: cfg!(unix), - ..Default::default() - }, - evtloop, - ); - w.set_interp(interp_ptr); + #[cfg(windows)] + let flags = if runs_builtin[cmd_idx] { + io_writer::Flags::overlapped_pipe() + } else { + io_writer::Flags::pipe() + }; + #[cfg(unix)] + let flags = io_writer::Flags::pipe(); + let w = IOWriter::init(pipes[cmd_idx][1], flags, interp); OutKind::Fd(crate::shell::io::OutFd { writer: w, captured: None, @@ -292,7 +345,7 @@ impl Pipeline { // Only the fd arm transitions state. interp.as_pipeline_mut(this).state = PipelineState::WaitingWriteErr; let child = io_writer::ChildPtr::new(this, io_writer::WriterTag::Pipeline); - return writer.enqueue(child, captured, &buf); + return writer.enqueue_owned(child, captured, buf); } if let OutKind::Pipe = &interp.as_pipeline(this).io.stderr { // SAFETY: single trampoline frame; no other borrow of the env's diff --git a/src/runtime/shell/subproc.rs b/src/runtime/shell/subproc.rs index 0e2829249d4e..fad4fdf6fd2d 100644 --- a/src/runtime/shell/subproc.rs +++ b/src/runtime/shell/subproc.rs @@ -1,26 +1,19 @@ use core::ffi::{c_char, c_void}; use std::sync::Arc; -#[cfg(unix)] use crate::api::bun::process::SpawnResultExt as _; use crate::api::bun::process::{self as bun_process, Process, SpawnOptions, Status}; -#[cfg(windows)] -use crate::api::bun::process::{WindowsOptions, WindowsStdioResult}; use crate::api::bun::subprocess as JscSubprocess; -use crate::shell::interpreter::{Interpreter, NodeId}; +use crate::shell::interpreter::{ExitCode, Interpreter, NodeId}; use crate::shell::io_writer::{self, IOWriter}; use crate::shell::states::cmd::Cmd as ShellCmd; use crate::shell::{self as sh, Yield}; -use crate::webcore::{self, FileSink}; use bun_alloc::Arena; use bun_collections::VecExt; use bun_io::Loop as AsyncLoop; -#[cfg(windows)] -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; use bun_io::{BufferedReader, ReadState}; use bun_jsc::{self as jsc, EventLoopHandle}; use bun_ptr::RefPtr; -#[cfg(not(windows))] use bun_sys::FdExt; use bun_sys::{self, Fd, SystemError}; use enumset::EnumSet; @@ -119,11 +112,6 @@ pub(crate) use JscSubprocess::StdioKind; use crate::shell::ShellErr; -#[cfg(windows)] -pub(crate) type StdioResult = WindowsStdioResult; -#[cfg(not(windows))] -pub(crate) type StdioResult = Option; - bun_output::define_scoped_log!(log, SHELL_SUBPROC, visible); /// Used for captured writer @@ -183,7 +171,7 @@ impl CmdHandle { } } -pub struct ShellSubprocess { +pub(crate) struct ShellSubprocess { pub(crate) cmd_parent: CmdHandle, /// `None` once closed. @@ -407,60 +395,43 @@ impl ShellSubprocess { /// Tear down a subprocess whose stdio start() failed. Marks pending pipe readers as /// errored so PipeReader.deinit's done-assert passes, drops the exit handler so a /// later onProcessExit doesn't touch the freed Subprocess, then deinits. - /// - /// Windows: PipeReader.deinit asserts the libuv source is closed. Whether the source - /// is uv-initialized depends on how far startWithCurrentPipe got, so a blind close or - /// destroy is unsafe. Fall back to leaking the Subprocess (pre-existing behavior) - /// rather than risk closing an uninitialized handle. fn abort_after_failed_start(this: *mut Self) { - #[cfg(windows)] - { - // SAFETY: `this` is the live allocation; it is deliberately leaked below, - // so release the Ctrl+C accounting by hand. - unsafe { (*this).ctrl_c_child = None }; - return; - } - #[cfg(not(windows))] - { - // SAFETY: `this` was created via `heap::alloc` in `spawn` and is - // uniquely owned here; reclaim and tear down. - let mut subproc = unsafe { bun_core::heap::take(this) }; - for r in [&mut subproc.stdout, &mut subproc.stderr] { - if let Readable::Pipe(pipe) = r { - // `start()` failed before any reader callback registered, - // so the `Arc` is expected to be uniquely held. Write - // unconditionally rather than via - // `Arc::get_mut`, which would silently skip the state - // transition if a future change bumped the strong count. - debug_assert_eq!(Arc::strong_count(pipe), 1); - let p = arc_as_mut_ptr(pipe); - // SAFETY: see `arc_as_mut_ptr` — single-threaded shell; no - // other borrow live. Accesses scoped to this statement. - unsafe { - if matches!((*p).state, PipeReaderState::Pending) { - (*p).state = PipeReaderState::Err(None); - } + // SAFETY: `this` was created via `heap::alloc` in `spawn` and is + // uniquely owned here; reclaim and tear down. + let mut subproc = unsafe { bun_core::heap::take(this) }; + for r in [&mut subproc.stdout, &mut subproc.stderr] { + if let Readable::Pipe(pipe) = r { + // `start()` failed before any reader callback registered, + // so the `Arc` is expected to be uniquely held. Write + // unconditionally rather than via + // `Arc::get_mut`, which would silently skip the state + // transition if a future change bumped the strong count. + debug_assert_eq!(Arc::strong_count(pipe), 1); + let p = arc_as_mut_ptr(pipe); + // SAFETY: see `arc_as_mut_ptr` — single-threaded shell; no + // other borrow live. Accesses scoped to this statement. + unsafe { + if matches!((*p).state, PipeReaderState::Pending) { + (*p).state = PipeReaderState::Err(None); } } } - subproc.proc().set_exit_handler_default(); - // Dropping `subproc` runs `ShellSubprocess::drop` → `finalize_sync`. } + subproc.proc().set_exit_handler_default(); + // Dropping `subproc` runs `ShellSubprocess::drop` → `finalize_sync`. } /// Stop stdio still active because the `Cmd` is deinited mid-flight (VM /// shutdown); a no-op after a normal close. Readers stop without firing /// `on_reader_done`, queued capture chunks are cancelled (the `IOWriter` /// queue holds a raw pointer into the freed `PipeReader`), a pending - /// buffer-stdin writer is closed. POSIX-only, same tradeoff as - /// [`Self::abort_after_failed_start`]. + /// buffer-stdin writer is closed. /// /// # Safety /// `this` must be the live `heap::alloc`'d subprocess with no outstanding /// borrows; single-threaded shell. Raw (not `&mut self`) because the /// stdin close re-enters `on_stdin_writer_close` through the writer's /// process backref. - #[cfg(not(windows))] pub(crate) unsafe fn deinit_in_flight_io(this: *mut Self) { // Claim `start()`'s +1, `close()` (fires `on_close` → `on_stdin_writer_close`: // slot → `Ignore`, `create()`'s ref released), release the claimed @@ -523,7 +494,6 @@ impl ShellSubprocess { /// # Safety /// Same contract as [`Self::deinit_in_flight_io`]; VM-shutdown finalizer /// only (on a live heap this would leak the pin and GC root). - #[cfg(not(windows))] pub(crate) unsafe fn defuse_array_buffer_unpins(this: *mut Self) { // SAFETY: disjoint field projections of the live subprocess. let slots = unsafe { [&raw mut (*this).stdout, &raw mut (*this).stderr] }; @@ -625,54 +595,34 @@ impl ShellSubprocess { true }; - // Hoist asSpawnOption results so a later one failing doesn't strand an earlier - // Windows *uv.Pipe in an unbound temporary inside the struct initializer. - // `mut` only for the Windows-only `.deinit()` rollback below. - #[cfg_attr(not(windows), allow(unused_mut))] - let mut stdin_opt = match stdio_guard[0].as_spawn_option(0) { + let stdin_opt = match stdio_guard[0].as_spawn_option(0) { stdio::ResultT::Result(opt) => opt, stdio::ResultT::Err(e) => { return Err(ShellErr::Custom(Box::<[u8]>::from(e.to_str()))); } }; - #[cfg_attr(not(windows), allow(unused_mut))] - let mut stdout_opt = match stdio_guard[1].as_spawn_option(1) { + let stdout_opt = match stdio_guard[1].as_spawn_option(1) { stdio::ResultT::Result(opt) => opt, stdio::ResultT::Err(e) => { - #[cfg(windows)] - stdin_opt.deinit(); return Err(ShellErr::Custom(Box::<[u8]>::from(e.to_str()))); } }; let stderr_opt = match stdio_guard[2].as_spawn_option(2) { stdio::ResultT::Result(opt) => opt, stdio::ResultT::Err(e) => { - #[cfg(windows)] - { - stdin_opt.deinit(); - stdout_opt.deinit(); - } return Err(ShellErr::Custom(Box::<[u8]>::from(e.to_str()))); } }; - let mut spawn_options = SpawnOptions { + let spawn_options = SpawnOptions { cwd: spawn_args.cwd.into(), stdin: stdin_opt, stdout: stdout_opt, stderr: stderr_opt, - #[cfg(windows)] - windows: WindowsOptions { - hide_window: true, - loop_: event_loop, - ..Default::default() - }, + #[cfg(unix)] + no_sigpipe, ..Default::default() }; - #[cfg(unix)] - { - spawn_options.no_sigpipe = no_sigpipe; - } // Backref so PipeReader callbacks can drive `Yield::run` from async I/O // completion; plumbed explicitly through `SpawnArgs`. @@ -697,20 +647,6 @@ impl ShellSubprocess { ) } { Err(err) => { - // WindowsSpawnOptions has no Drop - // (its Stdio::Buffer/Ipc carry FFI-owned `*mut uv::Pipe` already - // `uv_pipe_init`ed by spawn_process_windows before uv_spawn fails), - // so an implicit `drop(spawn_options)` is a no-op and leaks the - // pipe handles open in the uv loop. POSIX deinit is a no-op. - #[cfg(windows)] - { - spawn_options.stdin.deinit(); - spawn_options.stdout.deinit(); - spawn_options.stderr.deinit(); - for extra in spawn_options.extra_fds.iter_mut() { - extra.deinit(); - } - } drop(spawn_options); let mut msg = Vec::::new(); use std::io::Write; @@ -719,15 +655,6 @@ impl ShellSubprocess { } Ok(r) => match r { bun_sys::Result::Err(err) => { - #[cfg(windows)] - { - spawn_options.stdin.deinit(); - spawn_options.stdout.deinit(); - spawn_options.stderr.deinit(); - for extra in spawn_options.extra_fds.iter_mut() { - extra.deinit(); - } - } drop(spawn_options); return Err(ShellErr::Sys(err.to_shell_system_error())); } @@ -833,37 +760,6 @@ impl ShellSubprocess { } } - // Wire the FileSink's close-signal back to the enclosing `Writable` so - // `Writable::on_close` (drops the `Arc`) runs when the sink - // finishes. `stdin` lives inside the Box-allocated `Subprocess` at a - // stable address, so the self-referential raw pointer is sound for the - // life of the subprocess. Only reachable on Windows (POSIX - // `Writable::init` never returns `Pipe` for shell stdio). - { - // Derive `stdin_ptr` from the raw heap pointer (`subprocess`), not - // the local `subproc: &mut` reborrow — the pointer is stored - // long-term in `FileSink::source` and dereferenced from - // `Writable::on_close` after this frame returns. Under Stacked - // Borrows a child of `subproc`'s tag would be invalidated when - // that borrow ends; rooting in the allocation's provenance keeps - // it valid for the box's lifetime. - // SAFETY: `subprocess` is the live, fully-initialised heap alloc. - let stdin_ptr: *mut Writable = unsafe { &raw mut (*subprocess).stdin }; - // SAFETY: reborrow as a child of `stdin_ptr` so it does not - // invalidate the sibling we store in `source`. - if let Writable::Pipe(pipe) = unsafe { &mut *stdin_ptr } { - // SAFETY: shell is single-threaded; the FileSink allocation is - // disjoint from `*stdin_ptr`. `stdin_ptr` outlives the sink — - // the Subprocess owns both and `Writable::on_close` is the only - // path that drops it. - pipe.source - .set(webcore::streams::SourceHandle::ShellWritable( - // SAFETY: `stdin_ptr` is the live `&raw mut` writable (write provenance). - unsafe { bun_ptr::BackRef::from_raw_mut(stdin_ptr) }, - )); - } - } - // SAFETY: scoped access; `watch` does not re-enter the subprocess. match unsafe { (*subprocess).proc().watch() } { bun_sys::Result::Ok(()) => {} @@ -933,11 +829,10 @@ impl ShellSubprocess { // SAFETY: caller contract; the borrow ends at the `;`. let interrupted = unsafe { (*this).ctrl_c_child.take() }.is_some() && bun_spawn::ctrl_c::child_died_of_it(status); - let exit_code: Option = 'brk: { + let exit_code: Option = 'brk: { if let Status::Exited(exited) = &status { - #[cfg(windows)] - if exited.raw == bun_sys::windows::STATUS_CONTROL_C_EXIT { - break 'brk Some(bun_sys::SignalCode::SIGINT.to_exit_code()); + if exited.is_ctrl_c_exit() { + break 'brk Some(bun_sys::SignalCode::SIGINT.to_exit_code().into()); } break 'brk Some(exited.code); } @@ -947,7 +842,7 @@ impl ShellSubprocess { } if let Some(code) = status.signal().map(|signal| signal.to_exit_code()) { - break 'brk Some(code); + break 'brk Some(code.into()); } break 'brk None; @@ -960,7 +855,7 @@ impl ShellSubprocess { // before the Yield runs. let cmd = unsafe { handle.cmd_mut() }; cmd.base.interrupted |= interrupted; - let y = cmd.on_exit(code.into()); + let y = cmd.on_exit(code); // May free `*this`. y.run(&handle.interp); } @@ -976,9 +871,8 @@ pub enum WritableInitError { UnexpectedCreatingStdin, } -pub enum Writable { - Pipe(RefPtr), - Fd(Fd), +pub(crate) enum Writable { + Fd, Buffer(RefPtr), #[cfg(any(target_os = "linux", target_os = "android"))] Memfd(Fd), @@ -986,155 +880,72 @@ pub enum Writable { Ignore, } -impl Writable { - // When the stream has closed we need to be notified to prevent a use-after-free - // We can test for this use-after-free by enabling hot module reloading on a file and then saving it twice - pub fn on_close(&mut self, _: Option) { - match self { - Writable::Buffer(_) | Writable::Pipe(_) => { - // Dropping the Arc on reassignment below derefs. - } - _ => {} - } - *self = Writable::Ignore; - } -} - impl Writable { pub(crate) fn init( stdio: Stdio, event_loop: EventLoopHandle, subprocess: *mut Subprocess, - result: StdioResult, + result: Option, ) -> Result { - assert_stdio_result!(result); + assert_stdio_result(result); // Note: `Stdio` impls Drop, so we cannot partially move out via // match (E0509). Dispatch on `&mut` and `mem::take` / ManuallyDrop the // non-Copy payloads. let mut stdio = stdio; - #[cfg(windows)] - { - match &mut stdio { - Stdio::Pipe | Stdio::ReadableStream(_) => { - if let StdioResult::Buffer(buf) = result { - // Ownership of the `Box` transfers into the - // FileSink's writer. - let uv_pipe: *mut _ = bun_core::heap::into_raw(buf); - let pipe = FileSink::create_with_pipe(event_loop, uv_pipe); - if let bun_sys::Result::Err(_err) = - pipe.writer.with_mut(|w| w.start_with_current_pipe()) - { - return Err(WritableInitError::UnexpectedCreatingStdin); - } - - // TODO: uncoment this when is ready, commented because was not compiling - // subprocess.weak_file_sink_stdin_ptr = pipe; - // subprocess.flags.has_stdin_destructor_called = false; - - return Ok(Writable::Pipe(pipe)); - } - return Ok(Writable::Inherit); - } + match &mut stdio { + Stdio::Dup2(_) => { + // The shell never uses this + panic!("Unimplemented stdin dup2"); + } + Stdio::Pipe => { + // The shell never uses this + panic!("Unimplemented stdin pipe"); + } - Stdio::Blob(_) => { - // E0509: `Stdio` impls `Drop`, so the payload cannot be - // destructure-moved out. Take ownership via ManuallyDrop + - // ptr::read; the wrapper suppresses the Stdio destructor so - // the blob is moved exactly once. - let old = - core::mem::ManuallyDrop::new(core::mem::replace(&mut stdio, Stdio::Ignore)); + Stdio::Blob(_) => { + // E0509: `Stdio` impls `Drop`, so the payload cannot be + // destructure-moved out. Take ownership via ManuallyDrop + + // ptr::read; the wrapper suppresses the Stdio destructor so + // the blob is moved exactly once. + let old = + core::mem::ManuallyDrop::new(core::mem::replace(&mut stdio, Stdio::Ignore)); + let blob = match &*old { // SAFETY: `old` is Blob (matched above) and ManuallyDrop // prevents its Drop from running, so this is the sole move. - let blob = match &*old { - Stdio::Blob(b) => unsafe { core::ptr::read(b) }, - _ => unreachable!(), - }; - return Ok(Writable::Buffer(StaticPipeWriter::create( - event_loop, - subprocess, - result, - JscSubprocess::source_from_blob(blob), - ))); - } - Stdio::Fd(fd) => { - return Ok(Writable::Fd(*fd)); - } - Stdio::Dup2(dup2) => { - return Ok(Writable::Fd(dup2.to.to_fd())); - } - Stdio::Inherit => { - return Ok(Writable::Inherit); - } - Stdio::Path(_) | Stdio::Ignore => { - return Ok(Writable::Ignore); - } - Stdio::Ipc | Stdio::Capture(_) => { - return Ok(Writable::Ignore); - } - Stdio::SocketFd => { - // The shell never uses this; rejected at i < 3 anyway. - panic!("Unimplemented stdin socket-fd"); - } + Stdio::Blob(b) => unsafe { core::ptr::read(b) }, + _ => unreachable!(), + }; + Ok(Writable::Buffer(StaticPipeWriter::create( + event_loop, + subprocess, + result, + JscSubprocess::source_from_blob(blob), + ))) } - } - #[cfg(not(windows))] - { - match &mut stdio { - Stdio::Dup2(_) => { - // The shell never uses this - panic!("Unimplemented stdin dup2"); - } - Stdio::Pipe => { - // The shell never uses this - panic!("Unimplemented stdin pipe"); - } - - Stdio::Blob(_) => { - // E0509: `Stdio` impls `Drop`, so the payload cannot be - // destructure-moved out. Take ownership via ManuallyDrop + - // ptr::read; the wrapper suppresses the Stdio destructor so - // the blob is moved exactly once. - let old = - core::mem::ManuallyDrop::new(core::mem::replace(&mut stdio, Stdio::Ignore)); - let blob = match &*old { - // SAFETY: `old` is Blob (matched above) and ManuallyDrop - // prevents its Drop from running, so this is the sole move. - Stdio::Blob(b) => unsafe { core::ptr::read(b) }, - _ => unreachable!(), - }; - Ok(Writable::Buffer(StaticPipeWriter::create( - event_loop, - subprocess, - result, - JscSubprocess::source_from_blob(blob), - ))) - } - #[cfg(any(target_os = "linux", target_os = "android"))] - Stdio::Memfd(memfd) => { - debug_assert!(memfd.is_valid()); - let fd = *memfd; - // Ownership of the fd transfers to `Writable::Memfd`. - // Swap in `Ignore` and suppress the old value's destructor - // so `Stdio::Drop` doesn't close the fd we just took - // (`stdio = Stdio::Ignore` alone would drop+close the old - // `Stdio::Memfd`). - let _ = - core::mem::ManuallyDrop::new(core::mem::replace(&mut stdio, Stdio::Ignore)); - Ok(Writable::Memfd(fd)) - } - Stdio::Fd(_) => Ok(Writable::Fd(result.unwrap())), - Stdio::Inherit => Ok(Writable::Inherit), - Stdio::Path(_) | Stdio::Ignore => Ok(Writable::Ignore), - Stdio::Ipc | Stdio::Capture(_) => Ok(Writable::Ignore), - Stdio::ReadableStream(_) => { - // The shell never uses this - panic!("Unimplemented stdin readable_stream"); - } - Stdio::SocketFd => { - // The shell never uses this; rejected at i < 3 anyway. - panic!("Unimplemented stdin socket-fd"); - } + #[cfg(any(target_os = "linux", target_os = "android"))] + Stdio::Memfd(memfd) => { + debug_assert!(memfd.is_valid()); + let fd = *memfd; + // Ownership of the fd transfers to `Writable::Memfd`. + // Swap in `Ignore` and suppress the old value's destructor + // so `Stdio::Drop` doesn't close the fd we just took + // (`stdio = Stdio::Ignore` alone would drop+close the old + // `Stdio::Memfd`). + let _ = core::mem::ManuallyDrop::new(core::mem::replace(&mut stdio, Stdio::Ignore)); + Ok(Writable::Memfd(fd)) + } + Stdio::Fd(_) => Ok(Writable::Fd), + Stdio::Inherit => Ok(Writable::Inherit), + Stdio::Path(_) | Stdio::Ignore => Ok(Writable::Ignore), + Stdio::Ipc | Stdio::Capture(_) => Ok(Writable::Ignore), + Stdio::ReadableStream(_) => { + // The shell never uses this + panic!("Unimplemented stdin readable_stream"); + } + Stdio::SocketFd => { + // The shell never uses this; rejected at i < 3 anyway. + panic!("Unimplemented stdin socket-fd"); } } } @@ -1142,12 +953,8 @@ impl Writable { // Note: there is intentionally no `Writable::toJS` here — the shell never // exposes its stdin Writable to JS. - pub fn finalize(&mut self) { + pub(crate) fn finalize(&mut self) { match self { - Writable::Pipe(_) => { - // deref via drop-on-reassign - *self = Writable::Ignore; - } Writable::Buffer(_) => { let Writable::Buffer(buffer) = core::mem::replace(self, Writable::Ignore) else { unreachable!() @@ -1163,7 +970,7 @@ impl Writable { *self = Writable::Ignore; } Writable::Ignore => {} - Writable::Fd(_) | Writable::Inherit => {} + Writable::Fd | Writable::Inherit => {} } } } @@ -1221,9 +1028,8 @@ impl Readable { pub(crate) fn r#ref(&mut self) { if let Readable::Pipe(pipe) = self { - // SAFETY: see `arc_as_mut_ptr` — single-threaded shell; Windows - // `BufferedReader::update_ref` needs `&mut` to touch the libuv - // `Source` ref/unref. `update_ref` does not re-enter shell code. + // SAFETY: see `arc_as_mut_ptr` — single-threaded shell; + // `update_ref` does not re-enter shell code. unsafe { &mut *arc_as_mut_ptr(pipe) }.update_ref(true); } } @@ -1247,12 +1053,12 @@ impl Readable { shellio: Option>, event_loop: EventLoopHandle, process: *mut ShellSubprocess, - result: StdioResult, + result: Option, interp: *mut crate::shell::interpreter::Interpreter, _max_size: u32, _is_sync: bool, ) -> Readable { - assert_stdio_result!(result); + assert_stdio_result(result); debug_assert!(redirect_buf.is_none() || matches!(stdio, Stdio::Pipe | Stdio::Capture(_))); let buffered_output = match redirect_buf { @@ -1261,82 +1067,44 @@ impl Readable { }; // Note: `Stdio` impls Drop, so dispatch on `&mut` instead of partial moves (E0509). let mut stdio = stdio; - #[cfg(windows)] - { - return match &mut stdio { - Stdio::Inherit => Readable::Inherit, - Stdio::Ipc | Stdio::Dup2(_) | Stdio::Ignore => Readable::Ignore, - Stdio::Path(_) => Readable::Ignore, - Stdio::Fd(_) => Readable::Fd, - // blobs are immutable, so we should only ever get the case - // where the user passed in a Blob with an fd - Stdio::Blob(_) => Readable::Ignore, - Stdio::Pipe => Readable::Pipe(PipeReader::create( - event_loop, - process, - result, - None, - buffered_output, - out_type, - interp, - )), - Stdio::Capture(_) => Readable::Pipe(PipeReader::create( - event_loop, - process, - result, - shellio, - buffered_output, - out_type, - interp, - )), - Stdio::ReadableStream(_) => Readable::Ignore, // Shell doesn't use readable_stream - // The shell never uses this; rejected at i < 3 anyway. - Stdio::SocketFd => Readable::Ignore, - }; - } - - #[cfg(not(windows))] - { - match &mut stdio { - Stdio::Inherit => Readable::Inherit, - Stdio::Ipc | Stdio::Dup2(_) | Stdio::Ignore => Readable::Ignore, - Stdio::Path(_) => Readable::Ignore, - Stdio::Fd(_) => Readable::Fd, - // blobs are immutable, so we should only ever get the case - // where the user passed in a Blob with an fd - Stdio::Blob(_) => Readable::Ignore, - #[cfg(any(target_os = "linux", target_os = "android"))] - Stdio::Memfd(memfd) => { - let fd = *memfd; - // Ownership of the fd transfers to `Readable::Memfd`. Swap in - // `Ignore` and suppress the old value's destructor so - // `Stdio::Drop` doesn't close the fd we just took. - let _ = - core::mem::ManuallyDrop::new(core::mem::replace(&mut stdio, Stdio::Ignore)); - Readable::Memfd(fd) - } - Stdio::Pipe => Readable::Pipe(PipeReader::create( - event_loop, - process, - result, - None, - buffered_output, - out_type, - interp, - )), - Stdio::Capture(_) => Readable::Pipe(PipeReader::create( - event_loop, - process, - result, - shellio, - buffered_output, - out_type, - interp, - )), - Stdio::ReadableStream(_) => Readable::Ignore, // Shell doesn't use readable_stream - // The shell never uses this; rejected at i < 3 anyway. - Stdio::SocketFd => Readable::Ignore, + match &mut stdio { + Stdio::Inherit => Readable::Inherit, + Stdio::Ipc | Stdio::Dup2(_) | Stdio::Ignore => Readable::Ignore, + Stdio::Path(_) => Readable::Ignore, + Stdio::Fd(_) => Readable::Fd, + // blobs are immutable, so we should only ever get the case + // where the user passed in a Blob with an fd + Stdio::Blob(_) => Readable::Ignore, + #[cfg(any(target_os = "linux", target_os = "android"))] + Stdio::Memfd(memfd) => { + let fd = *memfd; + // Ownership of the fd transfers to `Readable::Memfd`. Swap in + // `Ignore` and suppress the old value's destructor so + // `Stdio::Drop` doesn't close the fd we just took. + let _ = core::mem::ManuallyDrop::new(core::mem::replace(&mut stdio, Stdio::Ignore)); + Readable::Memfd(fd) } + Stdio::Pipe => Readable::Pipe(PipeReader::create( + event_loop, + process, + result, + None, + buffered_output, + out_type, + interp, + )), + Stdio::Capture(_) => Readable::Pipe(PipeReader::create( + event_loop, + process, + result, + shellio, + buffered_output, + out_type, + interp, + )), + Stdio::ReadableStream(_) => Readable::Ignore, // Shell doesn't use readable_stream + // The shell never uses this; rejected at i < 3 anyway. + Stdio::SocketFd => Readable::Ignore, } } @@ -1502,8 +1270,7 @@ pub(crate) struct PipeReader { pub(crate) process: Option<*mut ShellSubprocess>, pub(crate) event_loop: EventLoopHandle, pub(crate) state: PipeReaderState, - #[cfg(not(windows))] - pub(crate) stdio_result: StdioResult, + pub(crate) stdio_result: Option, pub(crate) out_type: OutKind, pub(crate) captured_writer: CapturedWriter, pub(crate) buffered_output: BufferedOutput, @@ -1638,6 +1405,13 @@ impl CapturedWriter { e.syscall ); self.err = Some(e); + // This is the last completion taken: the reader can be freed by + // what follows, and the chunks still queued name it. + let this = + io_writer::ChildPtr::subproc_capture(std::ptr::from_mut(self).cast::()); + if let Some(writer) = &self.writer { + writer.cancel_chunks(this); + } } else if !all_written { return Yield::Suspended; } @@ -1727,7 +1501,7 @@ impl PipeReader { pub(crate) fn create( event_loop: EventLoopHandle, process: *mut ShellSubprocess, - result: StdioResult, + result: Option, capture: Option>, buffered_output: BufferedOutput, out_type: OutKind, @@ -1739,22 +1513,7 @@ impl PipeReader { captured_writer.dead = false; } - #[cfg(not(windows))] let reader = IOReader::init::(); - // With `Box` the pipe cannot be aliased, so ownership transfers to - // `reader.source`; `start()` goes through `start_with_current_pipe`. - #[cfg(windows)] - let reader = { - let mut reader = IOReader::init::(); - match result { - StdioResult::Buffer(buf) => reader.set_source(bun_io::Source::Pipe(buf)), - StdioResult::BufferFd(fd) => { - reader.set_source(bun_io::Source::File(bun_io::Source::open_file(fd))) - } - StdioResult::UnownedFd(_) | StdioResult::Unavailable => panic!("Shouldn't happen."), - } - reader - }; // Allocate directly into the Arc so the address is stable BEFORE we // hand it to `reader.set_parent` / `container_of` consumers. @@ -1771,7 +1530,6 @@ impl PipeReader { process: Some(process), reader, event_loop, - #[cfg(not(windows))] stdio_result: result, out_type, state: PipeReaderState::Pending, @@ -1808,15 +1566,14 @@ impl PipeReader { // self.ref(); self.process = Some(process); self.event_loop = event_loop; - #[cfg(windows)] - { - return self.reader.start_with_current_pipe(); - } - - // `reader` owns the fd from here; `Drop` closes an un-started one. - #[cfg(not(windows))] - match self.reader.start(self.stdio_result.take().unwrap(), true) { - bun_sys::Result::Err(err) => bun_sys::Result::Err(err), + // On `Ok` the reader owns the fd; `Drop` closes an un-started one. + let fd = self.stdio_result.take().unwrap(); + match self.reader.start(fd, true) { + bun_sys::Result::Err(err) => { + // The reader did not take `fd`. + self.stdio_result = Some(fd); + bun_sys::Result::Err(err) + } bun_sys::Result::Ok(()) => { // `reader.start` reports a poll-registration failure through // `on_reader_error` (not its return value), so the reader may @@ -1826,15 +1583,9 @@ impl PipeReader { return Ok(()); } #[cfg(unix)] - { - // TODO: are these flags correct - if let Some(poll) = self.reader.handle.get_poll() { - poll.set_flag(bun_io::FilePollFlag::Socket); - } - self.reader - .flags - .insert(bun_io::pipe_reader::PosixFlags::SOCKET); - } + self.reader + .flags + .insert(bun_io::pipe_reader::ReaderFlags::SOCKET); Ok(()) } @@ -1855,18 +1606,16 @@ impl PipeReader { self.captured_writer.do_write(chunk); - // No explicit re-arm here (`register_poll()` on POSIX / - // `start_with_current_pipe()` on Windows). This callback runs from + // No explicit re-arm here (`register_poll()` on POSIX). This callback runs from // inside the bun_io read loop, which still holds `&mut self.reader` // on its stack and re-registers the poll itself based on the bool we - // return (`IOReader::on_read_chunk_cb` and - // `WindowsBufferedReader::on_read` document the same contract). + // return (`IOReader::on_read_chunk_cb` documents the same contract). // // Re-arming from here also violates `BufferedReaderParent`'s // requirement that `on_read_chunk` never frees the reader: // `register_poll()`'s failure path dispatches `on_reader_error`, // which drops the last `Arc` and frees the - // `PosixBufferedReader` the loop is still reading through. + // `BufferedReader` the loop is still reading through. has_more != ReadState::Eof } @@ -2104,14 +1853,7 @@ impl PipeReader { } pub(crate) fn r#loop(&self) -> *mut AsyncLoop { - #[cfg(windows)] - { - self.event_loop.uv_loop() - } - #[cfg(not(windows))] - { - self.event_loop.r#loop() - } + self.event_loop.r#loop() } // Helper accessor used above to paper over Arc interior mutability. @@ -2149,20 +1891,10 @@ impl Drop for PipeReader { std::ptr::from_mut(self) as usize, out_kind_str(self.out_type) ); - #[cfg(unix)] - { - debug_assert!(self.reader.is_done() || matches!(self.state, PipeReaderState::Err(_))); - // Never started: the parent end is still ours to close. - if let Some(fd) = self.stdio_result.take() { - fd.close(); - } - } - - #[cfg(windows)] - { - debug_assert!( - self.reader.source.is_none() || self.reader.source.as_ref().unwrap().is_closed() - ); + debug_assert!(self.reader.is_done() || matches!(self.state, PipeReaderState::Err(_))); + // Never started: the parent end is still ours to close. + if let Some(fd) = self.stdio_result.take() { + fd.close(); } // PipeReaderState::Done(Box<[u8]>) drops its buffer automatically. @@ -2202,18 +1934,7 @@ bun_io::impl_buffered_reader_parent! { // this file so the `StaticPipeWriterProcess` trait impl uses the exact same // enum the trait was declared with. -// `StdioResult` is `Option` (8-byte Copy) on unix but a non-Copy enum -// (`Buffer(Box)`) on windows; a fn would have to pick by-value -// (moves on windows) or by-ref (clippy::trivially_copy_pass_by_ref on unix). -macro_rules! assert_stdio_result { - ($result:expr) => {{ - #[cfg(all(debug_assertions, unix))] - if let Some(fd) = &$result { - debug_assert!(fd.is_valid()); - } - }}; -} -pub(crate) use assert_stdio_result; +use JscSubprocess::assert_stdio_result; unsafe extern "C" { // `_PATH_DEFPATH` string literal emitted from C; immutable, load-time diff --git a/src/runtime/shell/util.rs b/src/runtime/shell/util.rs index 8471b910b8be..45c83fd62d1c 100644 --- a/src/runtime/shell/util.rs +++ b/src/runtime/shell/util.rs @@ -6,6 +6,6 @@ pub(crate) enum OutKind { } // The user-facing stdio type from `crate::api::bun_spawn::stdio`, NOT the -// low-level `PosixStdio`/`WindowsStdio` spawn-option shape that the +// low-level `Stdio` spawn-option shape that the // `bun_spawn` *crate* re-exports under the same name. pub(crate) use crate::api::bun_spawn::stdio::Stdio; diff --git a/src/runtime/socket/Handlers.rs b/src/runtime/socket/Handlers.rs index 16498c50be56..10f3809c4b98 100644 --- a/src/runtime/socket/Handlers.rs +++ b/src/runtime/socket/Handlers.rs @@ -520,7 +520,7 @@ impl SocketConfig { } #[cfg(not(windows))] { - Fd::from_uv(v) + Fd::from_crt(v) } }), ssl, diff --git a/src/runtime/socket/Listener.rs b/src/runtime/socket/Listener.rs index 056788e3b556..f1af01c906e4 100644 --- a/src/runtime/socket/Listener.rs +++ b/src/runtime/socket/Listener.rs @@ -35,12 +35,6 @@ use crate::node::path as node_path; use bun_boringssl as boringssl; #[cfg(windows)] use bun_core::strings; -#[cfg(windows)] -use bun_jsc::GlobalRef; -#[cfg(windows)] -use bun_libuv_sys::UvHandle as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; bun_output::define_scoped_log!(log, Listener, visible); @@ -111,11 +105,10 @@ bun_jsc::impl_abort_handle_owner!(Listener, abort_handle, |this, _cause| { pub(crate) enum ListenerType { Uws(*mut uws_sys::ListenSocket), /// Raw heap pointer (not `Box`) to a `WindowsNamedPipeListeningContext`. - /// The context's address is registered with libuv (`uv_pipe.data`) for the - /// lifetime of the handle, so we must never assert `noalias` over it via a - /// Box move or `&mut Listener` that transitively covers the context — that - /// would invalidate the pointer libuv holds under Stacked Borrows. Ownership - /// is still unique; freed via `close_pipe_and_deinit` → `on_pipe_closed` → `deinit`. + /// The context's address is the pipe server's callback context for as + /// long as it listens, so we must never assert `noalias` over it via a + /// Box move or `&mut Listener` that transitively covers the context. + /// Ownership is still unique; freed via `deinit`. #[cfg(windows)] NamedPipe(NonNull), #[default] @@ -270,17 +263,13 @@ impl Listener { .strong_data .set(Strong::create(default_data, cx.global())); } - // TODO: server_name is not supported on named pipes, I belive its , lets wait for - // someone to ask for it + // TODO: server_name is not supported on named pipes. - // we need to add support for the backlog parameter on listen here we use the - // default value of nodejs match WindowsNamedPipeListeningContext::listen( cx.global(), &pipe_buf[..pipe_len], - 511, ssl_cfg_taken.as_ref(), - this, + this_ref, ) { Ok(named_pipe) => { this_ref.listener.set(ListenerType::NamedPipe( @@ -296,14 +285,16 @@ impl Listener { // Surface coded syscall failures the way node:net // does (EADDRINUSE vs EACCES need different caller // handling) rather than an invalid-arguments TypeError. - if let ListenPipeError::Sys(sys_err, uv_errno) = &e { - // get_error_code_tag_name does not reject EUNKNOWN / - // UV_EAI_* (>=3000); neither is a node-style code, so - // route those through the generic error below. - if let Some((name, se)) = sys_err.get_error_code_tag_name() { - if se != bun_sys::SystemErrno::EUNKNOWN && (se as u16) < 3000 { + if let ListenPipeError::Sys(sys_err) = &e { + // get_error_code_tag_name does not reject EUNKNOWN, + // which is not a node-style code: that one goes + // through the generic error below. + if let Some((name, se)) = sys_err.get_error_code_tag_name() + && let Some(uv_errno) = bun_errno::e_to_uv(sys_err.errno) + { + if se != bun_sys::SystemErrno::EUNKNOWN { let err = jsc::SystemError { - errno: *uv_errno, + errno: uv_errno, code: bun_core::String::static_(name).into(), message: bun_core::String::clone_utf8( format!( @@ -327,7 +318,7 @@ impl Listener { } let detail = match &e { ListenPipeError::Other(err) => err.name(), - // Sys whose errno has no node-style code (EUNKNOWN / UV_EAI_*). + // Sys whose errno has no node-style code (EUNKNOWN). ListenPipeError::Sys(..) => "UNKNOWN", }; return Err(cx.global().throw_invalid_arguments(format_args!( @@ -919,11 +910,8 @@ impl Listener { ListenerType::Uws(socket) => bun_opaque::opaque_deref_mut(socket).close(), #[cfg(windows)] ListenerType::NamedPipe(named_pipe) => { - // SAFETY: named_pipe is the unique owner; close_pipe_and_deinit - // schedules the libuv close → on_pipe_closed → deinit chain. - unsafe { - WindowsNamedPipeListeningContext::close_pipe_and_deinit(named_pipe.as_ptr()) - }; + // SAFETY: the slot was cleared above, so this is the unique owner. + unsafe { WindowsNamedPipeListeningContext::deinit(named_pipe.as_ptr()) }; } ListenerType::None => {} } @@ -943,11 +931,8 @@ impl Listener { } #[cfg(windows)] ListenerType::NamedPipe(named_pipe) => { - // SAFETY: named_pipe is the unique owner; close_pipe_and_deinit - // schedules the libuv close → on_pipe_closed → deinit chain. - unsafe { - WindowsNamedPipeListeningContext::close_pipe_and_deinit(named_pipe.as_ptr()) - }; + // SAFETY: the slot was cleared above, so this is the unique owner. + unsafe { WindowsNamedPipeListeningContext::deinit(named_pipe.as_ptr()) }; } ListenerType::None => {} } @@ -1033,11 +1018,11 @@ impl Listener { // S008: `ListenSocket` is an `opaque_ffi!` ZST — safe deref. let socket = bun_opaque::opaque_deref_mut(uws_listener).socket::(); // On Windows the listening socket fd is a system-kind SOCKET - // handle; routing it through `.uv()` panics for anything but + // handle; routing it through `.crt()` panics for anything but // stdio. The sys_jsc helper branches on kind - // (system→u64, uv→i32, posix→i32). + // (system→u64, crt→i32, posix→i32). use bun_sys_jsc::FdJsc as _; - socket.fd().to_js_without_making_lib_uv_owned() + socket.fd().to_js_without_making_crt_owned() } _ => JSValue::js_number(-1.0), } @@ -1125,20 +1110,28 @@ impl Listener { vm.event_loop_ref().ensure_waker(); + // `fd` is the HANDLE of a pipe end `Bun.spawn` made for a child's stdio + // and nothing has opened yet (node:child_process's extra stdio pipes). + #[cfg(windows)] + let fd_is_spawned_pipe = opts + .get_truthy(cx.global(), "fdIsSpawnedPipe")? + .is_some_and(|v| v.to_boolean()); + let connection: UnixOrHost = 'blk: { if let Some(fd_) = opts.get_truthy(cx.global(), "fd")? { if fd_.is_number() { #[cfg(windows)] - let fd = if opts - .get_truthy(cx.global(), "fdIsRawSocket")? - .is_some_and(|v| v.to_boolean()) + let fd = if fd_is_spawned_pipe + || opts + .get_truthy(cx.global(), "fdIsRawSocket")? + .is_some_and(|v| v.to_boolean()) { Fd::from_system(fd_.to_int32() as u32 as usize as *mut c_void) } else { - Fd::from_uv(fd_.to_int32()) + Fd::from_crt(fd_.to_int32()) }; #[cfg(not(windows))] - let fd = Fd::from_uv(fd_.to_int32()); + let fd = Fd::from_crt(fd_.to_int32()); break 'blk UnixOrHost::Fd(fd); } } @@ -1204,7 +1197,6 @@ impl Listener { #[cfg(windows)] { use crate::socket::windows_named_pipe_context::SocketType as PipeSocketType; - use bun_sys::FdExt as _; let mut buf = bun_paths::path_buffer_pool::get(); // Note: reshaped for borrowck — `normalize_pipe_name` borrows @@ -1220,28 +1212,24 @@ impl Listener { } None => false, }, + UnixOrHost::Fd(_) if fd_is_spawned_pipe => true, UnixOrHost::Fd(fd) if fd.kind() == bun_core::FdKind::System => false, - UnixOrHost::Fd(fd) => { - let uvfd = fd.uv(); - let fd_type = uv::uv_guess_handle(uvfd); - if fd_type == uv::HandleType::NamedPipe { - true - } else if fd_type == uv::HandleType::Unknown { - // is not a libuv fd, check if it's a named pipe - let osfd: uv::uv_os_fd_t = uvfd as usize as uv::uv_os_fd_t; - if bun_sys::windows::GetFileType(osfd) == bun_sys::windows::FILE_TYPE_PIPE { - // yay its a named pipe lets make it a libuv fd - *fd = Fd::from_system(osfd) - .make_lib_uv_owned() - .unwrap_or_else(|_| panic!("failed to allocate file descriptor")); + UnixOrHost::Fd(fd) => match bun_sys::windows::GetFileType(fd.native()) { + bun_sys::windows::FILE_TYPE_PIPE => true, + // FILE_TYPE_UNKNOWN: not an open CRT fd, so the number may + // be an OS HANDLE. + 0 => { + let handle = fd.crt() as usize as bun_sys::windows::HANDLE; + if bun_sys::windows::GetFileType(handle) == bun_sys::windows::FILE_TYPE_PIPE + { + *fd = Fd::from_system(handle); true } else { false } - } else { - false } - } + _ => false, + }, _ => false, }; if is_named_pipe { @@ -1333,6 +1321,7 @@ impl Listener { UnixOrHost::Fd(fd) => WindowsNamedPipeContext::open( cx, *fd, + fd_is_spawned_pipe, ssl_taken.take(), ctx_for_pipe, PipeSocketType::Tls(tls_ref), @@ -1419,6 +1408,7 @@ impl Listener { UnixOrHost::Fd(fd) => WindowsNamedPipeContext::open( cx, *fd, + fd_is_spawned_pipe, None, None, PipeSocketType::Tcp(tcp_ref), @@ -1433,6 +1423,8 @@ impl Listener { return Ok(promise_value); } }; + // SAFETY: the pipe of the context just made, which is live. + unsafe { &*named_pipe }.set_stays_half_open(socket_config.allow_half_open); tcp_ref.socket.set(uws::NewSocketHandler { socket: uws::InternalSocket::Pipe(named_pipe.cast()), }); @@ -1802,48 +1794,36 @@ fn normalize_pipe_name<'a>(pipe_name: &[u8], buffer: &'a mut [u8]) -> Option<&'a #[cfg(windows)] pub(crate) struct WindowsNamedPipeListeningContext { - pub(crate) uv_pipe: uv::Pipe, - /// BACKREF: the parent `Listener` heap-allocated this context in - /// `listen_named_pipe` and outlives it (cleared to `None` in - /// `close_pipe_and_deinit` before the listener is torn down). `BackRef` - /// centralises the safe deref so call sites don't open-code a raw - /// `NonNull::as_ref`. - pub(crate) listener: Option>, - pub global_this: GlobalRef, - /// JSC_BORROW: process-lifetime singleton; `&'static` so call sites read - /// `self.vm.is_shutting_down()` without a raw-pointer deref. - pub(crate) vm: &'static VirtualMachine, + /// `None` only until `listen` has the context's address to give it. + server: Option, + /// BACKREF: the parent `Listener` owns this context and frees it + /// (`deinit`) before it goes away itself. + listener: bun_ptr::BackRef, /// Every accept wraps its pipe with this context. - pub ctx: JsCell>, + ctx: JsCell>, } -/// `c_int`: raw libuv return code so JS `err.errno` is the platform-correct UV value. #[cfg(windows)] enum ListenPipeError { - Sys(bun_sys::Error, c_int), + Sys(bun_sys::Error), Other(crate::Error), } #[cfg(windows)] impl WindowsNamedPipeListeningContext { - fn on_client_connect(this: *mut Self, status: uv::ReturnCode) { - // SAFETY: `this` is the `data` pointer libuv hands back; it was set to a - // live heap `WindowsNamedPipeListeningContext` in `listen_named_pipe`. - // Shared borrow — `on_name_pipe_created` re-enters JS; the one `&mut` - // (the `uv_pipe` field) is taken through the root pointer below. - let this_ref = unsafe { &*this }; - if status != uv::ReturnCode::ZERO || this_ref.listener.is_none() { - // connection dropped, or we are deiniting/closing - return; - } + /// A client connected. `stop()` from a socket handler frees `this`, so + /// nothing reads it once one may have run. + /// + /// # Safety + /// `this` is the live context given to `PipeServer::listen`. + unsafe fn on_connection(this: *mut Self, pipe: bun_io::windows::Pipe) { + // SAFETY: fn contract; the borrows end before any handler runs. + let (listener_ref, ssl_ctx) = unsafe { ((*this).listener, (*this).ctx.get().clone()) }; // `BackRef` deref — owner `Listener` outlives this context (see field doc). - let listener_ref = this_ref.listener.unwrap(); let listener: &Listener = listener_ref.get(); // An accepted pipe is the listening script's. - let entered = this_ref.vm.enter_context(listener.context); + let entered = listener.handlers.vm.enter_context(listener.context); use crate::socket::windows_named_pipe_context::SocketType as PipeSocketType; - // Owned for the whole accept: JS below can replace the slot through setSecureContext(). - let ssl_ctx = this_ref.ctx.get().clone(); let socket: PipeSocketType = if ssl_ctx.is_some() { PipeSocketType::Tls(Listener::on_name_pipe_created::(listener)) } else { @@ -1851,91 +1831,34 @@ impl WindowsNamedPipeListeningContext { }; let client = WindowsNamedPipeContext::create( - &this_ref.global_this.js_thread(entered.context()), + &listener.handlers.global_object.js_thread(entered.context()), socket, ); - // SAFETY: `client` was just heap-allocated by `create()`; exclusive - // here. The `&mut` to `uv_pipe` comes from the root pointer, scoped to - // this call — `this_ref` (shared) never touches `uv_pipe`. - let result = unsafe { - (*client) - .named_pipe - .get_accepted_by(&mut (*this).uv_pipe, ssl_ctx.as_ref()) - }; + // SAFETY: `client` was just heap-allocated by `create()`; exclusive here. + let result = unsafe { (*client).named_pipe.accepted(pipe, ssl_ctx) }; if result.is_err() { // connection dropped - // Release the only ref, which goes 1→0 → schedule_deinit → next-tick free. The - // deferred path is required because `get_accepted_by` may have already `uv_pipe_init`'d - // the client's inner handle on the loop; freeing the backing storage in-callback - // before `uv_close` completes is the exact pattern libuv forbids. // SAFETY: `client` was just allocated via `WindowsNamedPipeContext::create` - // with refcount==1; releasing the only ref schedules deinit. + // with refcount==1 and no handler ran; releasing the only ref schedules deinit. unsafe { WindowsNamedPipeContext::deref(client) }; } } - /// `uv_connection_cb` trampoline — recovers `*Self` from `handle.data` - /// (set by `Pipe::listen`) and forwards to [`on_client_connect`]. - /// Only ever invoked by libuv (coerces to the `uv_connection_cb` fn-pointer - /// type at the `Pipe::listen_named_pipe` call site); body wraps its derefs - /// explicitly — matches the `extern "C" fn` callback convention used in - /// `udp_socket.rs` / `bun_io::BufferedReader`. - extern "C" fn uv_on_client_connect(handle: *mut uv::uv_stream_t, status: uv::ReturnCode) { - // SAFETY: `data` was set to `*mut Self` by `Pipe::listen` below. - let this = unsafe { (*handle).data.cast::() }; - Self::on_client_connect(this, status); - } - - /// `uv_close_cb` trampoline. Only ever invoked by libuv (coerces to the - /// `uv_close_cb` fn-pointer type at the `Pipe::close` call site); body - /// wraps its deref explicitly. - extern "C" fn on_pipe_closed(pipe: *mut uv::Pipe) { - // SAFETY: `pipe.data` was set to `this` in `close_pipe_and_deinit`. - let this = unsafe { (*pipe).data.cast::() }; - Self::deinit(this); - } - - /// # Safety - /// `this` must be the unique owner (the `ListenerType::NamedPipe` slot was - /// already cleared by the caller). - unsafe fn close_pipe_and_deinit(this: *mut Self) { - // SAFETY: caller contract — `this` is a live heap allocation. - unsafe { - (*this).listener = None; - (*this).uv_pipe.data = this.cast::(); - (*this).uv_pipe.close(Self::on_pipe_closed); - } - } - fn listen( global_this: &JSGlobalObject, path: &[u8], - backlog: i32, ssl_config: Option<&SSLConfig>, - listener: *mut Listener, + listener: &Listener, ) -> Result<*mut WindowsNamedPipeListeningContext, ListenPipeError> { - // Heap-allocate at the final address so libuv can - // store a pointer back into `uv_pipe`. + // Heap-allocate at the final address: it is the server's callback context. let this = bun_core::heap::into_raw(Box::new(WindowsNamedPipeListeningContext { - uv_pipe: bun_core::ffi::zeroed(), - listener: NonNull::new(listener).map(bun_ptr::BackRef::from), - global_this: GlobalRef::from(global_this), - vm: global_this.bun_vm(), + server: None, + listener: bun_ptr::BackRef::new(listener), ctx: JsCell::new(None), })); - // Cleanup guard: once the uv pipe handle is registered with the loop it must be closed via - // uv_close; before that point we can free the struct directly. `deinit()` also - // frees the SSL context if one was created. State `.1` flips once `uv_pipe_init` - // succeeds; disarmed via `into_inner` on success. - let mut cleanup = scopeguard::guard((this, false), |(this, pipe_initialized)| { - if pipe_initialized { - // SAFETY: pipe is registered with the loop; close → on_pipe_closed → deinit. - unsafe { Self::close_pipe_and_deinit(this) }; - } else { - Self::deinit(this); - } - }); + // SAFETY: `this` was just allocated above and is not shared yet. + let cleanup = scopeguard::guard(this, |this| unsafe { Self::deinit(this) }); if let Some(ssl_options) = ssl_config { boringssl::load(); @@ -1952,78 +1875,36 @@ impl WindowsNamedPipeListeningContext { } } - // SAFETY: `this` was just allocated above; `&mut uv_pipe` is scoped to - // this call. - let init_result = unsafe { (*this).uv_pipe.init((*this).vm.uv_loop().cast(), false) }; - if init_result.is_err() { - return Err(ListenPipeError::Other(crate::Error::FailedToInitPipe)); - } - cleanup.1 = true; - - let listen_rc = if path[path.len() - 1] == 0 { - // is already null terminated - // SAFETY: `this` is live; `&mut uv_pipe` is scoped to this call. - unsafe { - (*this).uv_pipe.listen_named_pipe( - &path[..path.len() - 1], - backlog, - this.cast::(), - Self::uv_on_client_connect, - ) - } - } else { - let mut path_buf = bun_paths::path_buffer_pool::get(); - // we need to null terminate the path - let len = path.len().min(path_buf.len() - 1); - path_buf[..len].copy_from_slice(&path[..len]); - path_buf[len] = 0; - // SAFETY: `this` is live; `&mut uv_pipe` is scoped to this call. - unsafe { - (*this).uv_pipe.listen_named_pipe( - &path_buf[..len], - backlog, - this.cast::(), - Self::uv_on_client_connect, - ) - } - }; - if listen_rc.is_err() { - // Surface the real error code: EADDRINUSE (name taken) vs - // EACCES (pipe namespace denied) need different caller - // handling, and a generic bind failure hides that. - use bun_sys::ReturnCodeExt as _; - let raw = listen_rc.int(); - return Err(match listen_rc.to_error(bun_sys::Tag::listen) { - Some(err) => ListenPipeError::Sys(err, raw), - // Unreachable in practice: the uv→errno mapping is total. - None => ListenPipeError::Other(crate::Error::FailedToBindPipe), - }); - } + // A trailing NUL is a terminator. + let path = path.strip_suffix(&[0]).unwrap_or(path); + // `backlog` does not apply to a pipe: a client that finds every + // waiting instance taken waits for the next one. + let server = bun_io::windows::PipeServer::listen( + global_this.bun_vm().uws_loop(), + path, + this, + Self::on_connection, + ) + // Surface the real error code: EADDRINUSE (name taken) vs + // EACCES (pipe namespace denied) need different caller + // handling, and a generic bind failure hides that. + .map_err(ListenPipeError::Sys)?; //TODO: add readableAll and writableAll support if someone needs it - // if(uv.uv_pipe_chmod(&this.uvPipe, uv.UV_WRITABLE | uv.UV_READABLE) != 0) { - // this.closePipeAndDeinit(); - // return error.FailedChmodPipe; - //} - - // `uv_listen` made the pipe an active+ref'd uv handle. Strip libuv's - // loop ref so the owning `Listener`'s `poll_ref` is the only thing - // keeping the process alive (the contract usockets' libuv backend - // applies to its handles); otherwise `server.unref()` drops the - // `poll_ref` but the uv handle still pins `uv_loop_alive` and the - // process never exits. - // SAFETY: `this` is live; `&mut uv_pipe` is scoped to this call. - unsafe { (*this).uv_pipe.unref() }; - - let (this, _) = scopeguard::ScopeGuard::into_inner(cleanup); - Ok(this) + + // SAFETY: `this` is live, and clients are only reported from the loop. + unsafe { (*this).server = Some(server) }; + + Ok(scopeguard::ScopeGuard::into_inner(cleanup)) } - fn deinit(this: *mut Self) { - // SAFETY: `this` is a live `heap::alloc` allocation; this is the last owner. - unsafe { - (*this).listener = None; - drop(bun_core::heap::take(this)); - } + /// Stop listening and free the context. Connections already accepted are + /// not affected. + /// + /// # Safety + /// `this` came from `listen` and the caller is its unique owner. + unsafe fn deinit(this: *mut Self) { + // SAFETY: fn contract. + drop(unsafe { bun_core::heap::take(this) }); } } diff --git a/src/runtime/socket/SSLConfig.rs b/src/runtime/socket/SSLConfig.rs index 1132693c7aff..e33998d21aa3 100644 --- a/src/runtime/socket/SSLConfig.rs +++ b/src/runtime/socket/SSLConfig.rs @@ -305,8 +305,8 @@ fn handle_path( ) -> JsResult<*const c_char> { let name = string.to_owned_slice_z(); // `bun_sys::access` routes to `access(2)` on POSIX and - // `GetFileAttributesW` on Windows (via `sys_uv`), so this is the - // cross-platform existence probe. + // `GetFileAttributesW` on Windows, so this is the cross-platform + // existence probe. if bun_sys::access(&name, bun_sys::posix::F_OK).is_err() { // Error path: free_sensitive(name) — zero before drop. Route through // the canonical helper so the secure-zero core stays single-sourced. diff --git a/src/runtime/socket/UpgradedDuplex.rs b/src/runtime/socket/UpgradedDuplex.rs index 106748ac3e9a..96cb41a3a88a 100644 --- a/src/runtime/socket/UpgradedDuplex.rs +++ b/src/runtime/socket/UpgradedDuplex.rs @@ -76,6 +76,62 @@ pub(crate) struct CertError { } // `Box` drops automatically — no explicit Drop needed. +impl CertError { + /// An owned copy of what a handshake reported; `code` and `reason` only + /// when there is an error. + pub(crate) fn from_verify_error(ssl_error: &us_bun_verify_error_t) -> CertError { + CertError { + error_no: ssl_error.error_no, + code: ssl_error + .code() + .filter(|_| ssl_error.error_no != 0) + .map(Into::into), + reason: ssl_error + .reason() + .filter(|_| ssl_error.error_no != 0) + .map(Into::into), + } + } + + /// The C view, borrowing `self`'s strings (`""` when absent). + pub(crate) fn as_verify_error(&self) -> us_bun_verify_error_t { + us_bun_verify_error_t { + error_no: self.error_no, + code: self.code.as_deref().map_or(c"".as_ptr(), |c| c.as_ptr()), + reason: self.reason.as_deref().map_or(c"".as_ptr(), |c| c.as_ptr()), + } + } +} + +/// Re-arm (`ms > 0`) or stop (`ms == 0`) the idle timer of a software socket +/// and remember `ms` for the next reset. +pub(crate) fn set_socket_timeout( + event_loop_timer: &JsCell, + current_timeout: &Cell, + ms: c_uint, +) { + if event_loop_timer.get().state == EventLoopTimerState::ACTIVE { + timer_all().remove(event_loop_timer.as_ptr()); + } + current_timeout.set(ms); + + // if the interval is 0 means that we stop the timer + if ms == 0 { + return; + } + + // reschedule the timer + let next = + bun_core::Timespec::ms_from_now(bun_core::TimespecMockMode::ForceRealTime, ms as i64); + event_loop_timer.with_mut(|t| { + t.next = ElTimespec { + sec: next.sec, + nsec: next.nsec, + }; + }); + timer_all().insert(event_loop_timer.as_ptr()); +} + type WrapperType = SSLWrapper<*mut UpgradedDuplex>; /// Server-side peer-certificate policy for a duplex TLS upgrade, resolved in @@ -186,17 +242,7 @@ impl UpgradedDuplex { bun_output::scoped_log!(UpgradedDuplex, "onHandshake"); // SAFETY: see handler note above. let this = unsafe { &*this }; - this.ssl_error.set(CertError { - error_no: ssl_error.error_no, - code: ssl_error - .code() - .filter(|_| ssl_error.error_no != 0) - .map(Into::into), - reason: ssl_error - .reason() - .filter(|_| ssl_error.error_no != 0) - .map(Into::into), - }); + this.ssl_error.set(CertError::from_verify_error(&ssl_error)); (this.handlers.on_handshake)(this.handlers.ctx, handshake_success, ssl_error); // Retry writes parked during the handshake, like openssl.c's `ssl_write_wants_read`. if handshake_success && !this.is_shutdown() { @@ -587,14 +633,7 @@ impl UpgradedDuplex { #[uws_callback(export = "UpgradedDuplex__ssl_error", no_catch)] pub(crate) fn ssl_error(&self) -> us_bun_verify_error_t { - let err = self.ssl_error.get(); - us_bun_verify_error_t { - error_no: err.error_no, - code: err.code.as_deref().map_or(c"".as_ptr(), |c| c.as_ptr()), - reason: err.reason.as_deref().map_or(c"".as_ptr(), |c| c.as_ptr()), - // `struct us_bun_verify_error_t` (libusockets.h) has exactly these - // three fields: { int error; const char* code; const char* reason }. - } + self.ssl_error.get().as_verify_error() } fn reset_timeout(&self) { @@ -602,32 +641,7 @@ impl UpgradedDuplex { } fn set_timeout_in_milliseconds(&self, ms: c_uint) { - if self.event_loop_timer.get().state == EventLoopTimerState::ACTIVE { - timer_all().remove(self.event_loop_timer.as_ptr()); - } - self.current_timeout.set(ms); - - // if the interval is 0 means that we stop the timer - if ms == 0 { - return; - } - - // reschedule the timer - // Note: `EventLoopTimer.next` is the lower-tier `ElTimespec` stub; - // bridge from `bun_core::Timespec` until the lower tier switches. - let next = - bun_core::Timespec::ms_from_now(bun_core::TimespecMockMode::ForceRealTime, ms as i64); - self.event_loop_timer.with_mut(|t| { - t.next = ElTimespec { - sec: next.sec, - nsec: next.nsec, - }; - }); - timer_all().insert( - core::ptr::addr_of!(self.event_loop_timer) - .cast::() - .cast_mut(), - ); + set_socket_timeout(&self.event_loop_timer, &self.current_timeout, ms); } #[uws_callback(export = "UpgradedDuplex__set_timeout")] diff --git a/src/runtime/socket/WindowsNamedPipe.rs b/src/runtime/socket/WindowsNamedPipe.rs index 6c892b05f2a8..5cc0cbcd7e55 100644 --- a/src/runtime/socket/WindowsNamedPipe.rs +++ b/src/runtime/socket/WindowsNamedPipe.rs @@ -1,78 +1,104 @@ -//! Wrapper that provides a socket-like API for Windows Named Pipes. +//! A socket-like interface over a Windows named pipe, so that a pipe path can +//! be connected to and listened on like a Unix domain socket path. The owner +//! supplies the same set of handlers a socket has (open, data, handshake, +//! timeout, error, close…); TLS goes through `SSLWrapper`, timeouts through +//! the VM's timer heap, and writes through a `StreamingWriter`. //! -//! This allows us to use the same networking interface and event handling -//! patterns across platforms, treating Named Pipes as if they were regular -//! sockets. The wrapper translates between µWebSockets' socket-based API -//! and Windows Named Pipe operations, enabling seamless cross-platform -//! IPC without requiring separate code paths for Windows vs Unix domain sockets. -//! -//! Integration with µWebSockets/uSockets: -//! - Uses the same event loop and timer mechanisms as other socket types -//! - Implements compatible handlers (onOpen, onData, onClose, etc.) that match uSockets callbacks -//! - Supports SSL/TLS wrapping through the same BoringSSL integration used by TCP sockets -//! - Provides streaming writer interface that mirrors uSockets' write operations -//! - Maintains the same connection lifecycle and state management as network sockets -//! - Enables transparent use of Named Pipes in contexts expecting standard socket APIs -//! -//! Uses libuv for the underlying Named Pipe operations while maintaining compatibility -//! with µWebSockets, bridging the gap between libuv's pipe handling and uSockets' -//! unified socket interface. +//! A named pipe cannot be polled for readiness the way a socket can, so it is +//! not a uSockets socket: reads and writes are overlapped operations on a +//! `bun_io::windows::Pipe`, which completes them through the same loop. use core::cell::Cell; use core::ffi::{c_uint, c_void}; -use core::ptr::NonNull; use bun_boringssl_sys as boringssl; -use bun_collections::ByteVecExt; -use bun_core::timespec; -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; -use bun_io::{StreamingWriter, WriteStatus}; +use bun_io::windows::{ConnectRequest, Pipe, PipeOrigin, ReadEvent}; +use bun_io::{Source, StreamingWriter, WriteStatus}; use bun_jsc::JsCell; use bun_jsc::virtual_machine::VirtualMachine; -use bun_libuv_sys::{UvHandle as _, UvStream as _}; -use bun_sys::windows::libuv as uv; -use bun_sys::{self, Fd, ReturnCodeExt as _}; +use bun_sys::{self, Fd}; use bun_uws::us_bun_verify_error_t; use crate::socket::SSLConfig; use crate::socket::ssl_wrapper::{self, SSLWrapper}; -use crate::timer::{ElTimespec, EventLoopTimer, EventLoopTimerState, EventLoopTimerTag}; +use crate::timer::{EventLoopTimer, EventLoopTimerState, EventLoopTimerTag}; bun_output::declare_scope!(WindowsNamedPipe, visible); +/// How long a pipe is still read after [`shutdown`], with nothing arriving, +/// before it is closed: libuv's `eof_timeout` (`src/win/pipe.c`), which is +/// what `socket.end()` does to a named pipe in Node. +/// +/// A named pipe has one state for both directions, so the other end cannot be +/// told that writing is over while this end stays open to read: all it can see +/// is this end closing, and nothing says whether it will write again. This is +/// the only case without an event to wait for. See [`EndOfWrite`]. +/// +/// [`shutdown`]: WindowsNamedPipe::shutdown +const END_OF_WRITE_IDLE_MS: i64 = 50; + +/// How far `shutdown()` of a plain (non-TLS) pipe has come. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum EndOfWrite { + /// `shutdown` has not been called. + Open, + /// Queued writes are still being written. + Writing, + /// Waiting to hear that the other end has read everything. + Flushing, + /// A message-type pipe: the other end has been sent the zero-length + /// message that means the end of writing, and closes when it is done. That + /// meaning is a convention (go-winio's, .NET's) which not every server + /// knows, and one that does not waits for this end to close: so this is + /// [`Idle`](Self::Idle) too, unless the owner asked for a half-open pipe. + Told, + /// A byte-type pipe, or a HANDLE that cannot be asked whether the other + /// end has read everything: closed once nothing has arrived for + /// [`END_OF_WRITE_IDLE_MS`]. + Idle, +} + pub(crate) type CertError = crate::socket::upgraded_duplex::CertError; type WrapperType = SSLWrapper<*mut WindowsNamedPipe>; -use crate::jsc_hooks::timer_all_mut as timer_all; - pub(crate) struct WindowsNamedPipe { pub(crate) wrapper: JsCell>, pub(crate) deferred_writer_close: Cell, pub(crate) root: Cell<*mut WindowsNamedPipe>, - /// Non-owning alias of the heap `uv::Pipe`. The owning - /// `Box` is leaked in [`from`] and adopted by - /// `self.writer.source` (`Source::Pipe`) inside [`start`]; this field only - /// ever observes/null-checks the handle, never frees it. Cleared by - /// [`Self::on_close`] before the writer's async close frees the Box. - pub(crate) pipe: Cell>>, // any duplex + /// The connect in flight. Dropping it abandons the attempt: `on_connect` + /// does not run. + pub(crate) connect_req: JsCell>, /// The per-thread VM singleton outlives this struct (it is torn down only /// at thread exit, after every named pipe is closed), so `&'static` is the /// honest model here rather than a threaded lifetime. pub(crate) vm: &'static VirtualMachine, + + /// Owns the open pipe (`writer.source`); reads go through it too. pub(crate) writer: JsCell>, - pub(crate) incoming: JsCell>, // Maybe we should use IPCBuffer here as well pub(crate) ssl_error: JsCell, pub(crate) handlers: Handlers, - pub(crate) connect_req: JsCell, pub(crate) event_loop_timer: JsCell, pub(crate) current_timeout: Cell, pub(crate) flags: Cell, + + pub(crate) end_of_write: Cell, + /// Armed while the pipe [`closes_when_idle`](Self::closes_when_idle) and + /// is being read. + pub(crate) end_of_write_timer: JsCell, + /// The other end sent its end-of-write message. + pub(crate) peer_ended_writing: Cell, + /// `allowHalfOpen`: once told of the end of writing, the other end has as + /// long as it likes. + stays_half_open: Cell, } -bun_event_loop::impl_timer_owner!(WindowsNamedPipe; from_timer_ptr => event_loop_timer); +bun_event_loop::impl_timer_owner!(WindowsNamedPipe; + from_timer_ptr => event_loop_timer, + from_end_of_write_timer_ptr => end_of_write_timer, +); bitflags::bitflags! { #[repr(transparent)] @@ -80,16 +106,9 @@ bitflags::bitflags! { pub struct Flags: u8 { const DISCONNECTED = 1 << 0; const IS_CLOSED = 1 << 1; - const IS_CLIENT = 1 << 2; const IS_SSL = 1 << 3; - /// Rust-only bookkeeping: set once `start_with_pipe` adopts the - /// `Box` leaked in [`from`]. Lets `Drop` reclaim the orphan - /// allocation on early-error paths (before adoption) without risking a - /// double-free once the writer owns it. - const PIPE_ADOPTED = 1 << 4; - const WRAPPER_BUSY = 1 << 5; - const WRITER_BUSY = 1 << 6; - // _: u2 padding + const WRAPPER_BUSY = 1 << 4; + const WRITER_BUSY = 1 << 5; } } @@ -156,39 +175,6 @@ impl WindowsNamedPipe { Some(result) } - #[inline] - fn uv_pipe(&self) -> Option<*mut uv::Pipe> { - self.pipe.get().map(NonNull::as_ptr) - } - - /// Reclaim the leaked `Box` on an early-error path **before** - /// [`start`] hands it to `self.writer.source` via `start_with_pipe`. - /// - /// [`from`] `Box::leak`s the allocation and records only a non-owning - /// `NonNull` in `self.pipe`; until adoption the writer's `Drop` - /// (`close_without_reporting`) is a no-op (`source == None`), so any - /// `connect`/`open`/`get_accepted_by` early return would leak the box and — - /// if `uv_pipe_init` had already run — leave the handle in the libuv - /// `handle_queue` with no `uv_close` ever scheduled (loop never drains). - /// `close_and_destroy` covers both states via its `loop_.is_null()` branch. - /// - /// MUST NOT be called once `start_with_pipe` has adopted the allocation - /// (would double-free against `writer.source`'s `Box`). - fn discard_unadopted_pipe(&self) { - debug_assert!( - self.writer.get().source.is_none(), - "pipe already adopted by writer.source; discard would double-free" - ); - if let Some(pipe) = self.pipe.take() { - // SAFETY: `pipe` is the `NonNull` recorded from `Box::leak` in - // `from()` and not yet re-materialised (asserted above); - // `close_and_destroy` reclaims via `Box::from_raw` either - // immediately (never-init'd, `loop_ == null`) or in the `uv_close` - // callback (init'd). Ownership transfers here exactly once. - unsafe { uv::Pipe::close_and_destroy(pipe.as_ptr()) }; - } - } - /// Holds a ref on the owning context until the returned guard drops. /// /// The context frees itself from a task it queues when [`on_close`] releases @@ -217,25 +203,29 @@ impl WindowsNamedPipe { (self.handlers.on_writable)(self.handlers.ctx); } - fn on_read(&self, nread: usize) { - bun_output::scoped_log!(WindowsNamedPipe, "onRead ({})", nread); + /// # Safety + /// `this` is the live pipe that called `read_start`. + unsafe fn on_read_event(this: *mut Self, event: ReadEvent<'_>) { + // SAFETY: fn contract. + let this = unsafe { &*this }; + match event { + ReadEvent::Data(data) => this.on_read(data), + ReadEvent::Eof => this.on_read_end(), + ReadEvent::EndOfWrite => this.on_peer_end_of_write(), + ReadEvent::Err(err) => this.on_read_error(err), + } + } + + fn on_read(&self, data: &[u8]) { + bun_output::scoped_log!(WindowsNamedPipe, "onRead ({})", data.len()); let _keep_alive = self.keep_alive(); - // SAFETY: `nread` bytes written by libuv into on_read_alloc's slice. - self.incoming - .with_mut(|incoming| unsafe { incoming.uv_commit(nread) }); self.reset_timeout(); + self.arm_end_of_write_timer(); - let mut data = self.incoming.replace(Vec::new()); - - if self - .with_wrapper(|w| w.receive_data(data.as_slice())) - .is_none() - { - (self.handlers.on_data)(self.handlers.ctx, data.as_slice()); + if self.with_wrapper(|w| w.receive_data(data)).is_none() { + (self.handlers.on_data)(self.handlers.ctx, data); } - data.clear(); - self.incoming.set(data); } fn on_write(&self, amount: usize, status: WriteStatus) { @@ -252,17 +242,7 @@ impl WindowsNamedPipe { match status { WriteStatus::Pending => {} - WriteStatus::Drained => { - // unref after sending all data - self.writer.with_mut(|w| { - if let Some(source) = w.source.as_mut() { - // `Source` is an enum; - // `unref()` matches the active variant (always `Pipe` here - // via `start_with_pipe`). - source.unref(); - } - }); - } + WriteStatus::Drained => self.continue_end_of_write(), WriteStatus::EndOfFile => { // we send FIN so we close after this self.close_writer(); @@ -294,16 +274,150 @@ impl WindowsNamedPipe { r } - fn on_read_error(&self, err: bun_sys::E) { - bun_output::scoped_log!(WindowsNamedPipe, "onReadError"); + fn on_read_end(&self) { + bun_output::scoped_log!(WindowsNamedPipe, "onReadEnd"); let _keep_alive = self.keep_alive(); - if err == bun_sys::E::EOF { - // we received FIN but we dont allow half-closed connections right now - (self.handlers.on_end)(self.handlers.ctx); + // The other end is gone: there is nobody left to write to. + (self.handlers.on_end)(self.handlers.ctx); + self.close_writer(); + } + + /// The other end of a message-type pipe has finished writing and is still + /// open. The owner hears `on_end`; the pipe stays open to write to, and is + /// closed once this end has finished writing too. + fn on_peer_end_of_write(&self) { + bun_output::scoped_log!(WindowsNamedPipe, "onPeerEndOfWrite"); + let _keep_alive = self.keep_alive(); + self.peer_ended_writing.set(true); + (self.handlers.on_end)(self.handlers.ctx); + // As a socket that is not `allowHalfOpen` ends with its peer. + if !self.stays_half_open.get() { + self.begin_end_of_write(); + } + if self.end_of_write.get() == EndOfWrite::Told { self.close_writer(); + } + } + + /// `shutdown` of a plain pipe, once the connection is up. + fn begin_end_of_write(&self) { + if self.end_of_write.get() != EndOfWrite::Open { + return; + } + self.end_of_write.set(EndOfWrite::Writing); + self.continue_end_of_write(); + } + + /// Queued writes are done: ask to hear when the other end has read them. + fn continue_end_of_write(&self) { + if self.end_of_write.get() != EndOfWrite::Writing || self.writer.get().has_pending_data() { return; } - self.on_error(bun_sys::Error::from_code(err, bun_sys::Tag::read)); + let this: *mut Self = self.root_ptr(); + match self.with_pipe(|pipe| pipe.flush_peer(this, Self::on_peer_flushed)) { + Some(Ok(())) => self.end_of_write.set(EndOfWrite::Flushing), + Some(Err(_)) => self.finish_end_of_write(false), + None => {} + } + } + + /// Writing is over and the other end has read it all (`flushed`), or + /// cannot be asked. What is left is telling the other end, or closing. + fn finish_end_of_write(&self, flushed: bool) { + let _keep_alive = self.keep_alive(); + if self.peer_ended_writing.get() { + // Both directions are done. + self.end_of_write.set(EndOfWrite::Told); + self.close_writer(); + return; + } + if flushed && self.with_pipe(|pipe| pipe.is_message_type()) == Some(true) { + self.end_of_write.set(EndOfWrite::Told); + let _ = self.with_pipe(|pipe| pipe.write_end_marker()); + } else { + self.end_of_write.set(EndOfWrite::Idle); + } + self.arm_end_of_write_timer(); + } + + /// Whether the pipe is closed once nothing has arrived for + /// [`END_OF_WRITE_IDLE_MS`]. + fn closes_when_idle(&self) -> bool { + match self.end_of_write.get() { + EndOfWrite::Idle => true, + EndOfWrite::Told => !self.stays_half_open.get(), + EndOfWrite::Open | EndOfWrite::Writing | EndOfWrite::Flushing => false, + } + } + + /// # Safety + /// `this` is the live pipe that called `flush_peer`. + unsafe fn on_peer_flushed(this: *mut Self, flushed: bun_sys::Result<()>) { + // SAFETY: fn contract. + let this = unsafe { &*this }; + if this.end_of_write.get() != EndOfWrite::Flushing || this.flags.get().is_closed() { + return; + } + this.finish_end_of_write(flushed.is_ok()); + } + + /// (Re)start the idle timer. It runs only while the pipe is being read: a + /// paused pipe is not closed under its owner. + fn arm_end_of_write_timer(&self) { + if !self.closes_when_idle() { + return; + } + self.cancel_end_of_write_timer(); + if self.with_pipe(|pipe| pipe.is_reading()) != Some(true) { + return; + } + let next = bun_core::Timespec::ms_from_now( + bun_core::TimespecMockMode::ForceRealTime, + END_OF_WRITE_IDLE_MS, + ); + self.end_of_write_timer.with_mut(|t| { + t.next = crate::timer::ElTimespec { + sec: next.sec, + nsec: next.nsec, + }; + }); + crate::jsc_hooks::timer_all_mut().insert(self.end_of_write_timer.as_ptr()); + } + + fn cancel_end_of_write_timer(&self) { + if self.end_of_write_timer.get().state == EventLoopTimerState::ACTIVE { + crate::jsc_hooks::timer_all_mut().remove(self.end_of_write_timer.as_ptr()); + } + } + + /// Nothing arrived for [`END_OF_WRITE_IDLE_MS`]: close, which is the one + /// end of stream every server can see. The owner hears `on_end` first, as + /// it does when the other end closes. + pub(crate) fn on_end_of_write_idle(&self) { + bun_output::scoped_log!(WindowsNamedPipe, "onEndOfWriteIdle"); + self.end_of_write_timer.with_mut(|t| { + t.state = EventLoopTimerState::FIRED; + t.heap = Default::default(); + }); + if !self.closes_when_idle() || self.flags.get().is_closed() { + return; + } + // What has arrived is delivered next, and re-arms this. + if self.with_pipe(|pipe| pipe.has_unread()) == Some(true) { + return; + } + self.on_read_end(); + } + + fn on_read_error(&self, err: bun_sys::Error) { + bun_output::scoped_log!(WindowsNamedPipe, "onReadError"); + // A server that answers the end of writing with `DisconnectNamedPipe` + // has ended the conversation, as one that closes its handle has. + if self.end_of_write.get() == EndOfWrite::Told && err.get_errno() == bun_sys::E::EPIPE { + return self.on_read_end(); + } + let _keep_alive = self.keep_alive(); + self.on_error(err); self.close_writer(); } @@ -393,17 +507,7 @@ impl WindowsNamedPipe { bun_output::scoped_log!(WindowsNamedPipe, "onHandshake"); let _keep_alive = self.keep_alive(); - self.ssl_error.set(CertError { - error_no: ssl_error.error_no, - code: ssl_error - .code() - .filter(|_| ssl_error.error_no != 0) - .map(Into::into), - reason: ssl_error - .reason() - .filter(|_| ssl_error.error_no != 0) - .map(Into::into), - }); + self.ssl_error.set(CertError::from_verify_error(&ssl_error)); (self.handlers.on_handshake)(self.handlers.ctx, handshake_success, ssl_error); // Retry writes parked during the handshake; a TLS 1.2 client's completion sends nothing. if handshake_success && !self.is_shutdown() { @@ -417,7 +521,6 @@ impl WindowsNamedPipe { return; } bun_output::scoped_log!(WindowsNamedPipe, "onClose"); - self.pipe.set(None); if !self.flags.get().is_closed() { self.update_flags(|f| f.set(Flags::IS_CLOSED, true)); // only call onClose once (self.handlers.on_close)(self.handlers.ctx); @@ -425,68 +528,48 @@ impl WindowsNamedPipe { } } - fn call_write_or_end(&self, data: Option<&[u8]>, msg_more: bool) { - if let Some(bytes) = data { - if !bytes.is_empty() { - // ref because we have pending data - self.writer.with_mut(|w| { - if let Some(source) = w.source.as_mut() { - // See `on_write` for the active-variant note. - source.ref_(); - } - }); - if self.flags.get().disconnected() { - // enqueue to be sent after connecting - self.writer - .with_mut(|w| bun_core::handle_oom(w.outgoing.write(bytes))); - } else { - // write will enqueue the data if it cannot be sent - let _ = self.writer.with_mut(|w| w.write(bytes)); - } - } - } + fn internal_write(&self, encoded_data: &[u8]) { + self.reset_timeout(); - if !msg_more { - let _ = self.with_wrapper(|w| { - let _ = w.shutdown(false); - }); - self.with_writer(|w| w.end()); + if encoded_data.is_empty() { + return; + } + if self.flags.get().disconnected() { + // enqueue to be sent after connecting + self.writer + .with_mut(|w| bun_core::handle_oom(w.outgoing.write(encoded_data))); + } else { + // write will enqueue the data if it cannot be sent + let _ = self.writer.with_mut(|w| w.write(encoded_data)); } } - fn internal_write(&self, encoded_data: &[u8]) { - self.reset_timeout(); + /// The open pipe; `None` before the connect completes and once closed. + fn with_pipe(&self, f: impl FnOnce(&mut Pipe) -> R) -> Option { + self.writer.with_mut(|w| match w.source.as_mut() { + Some(Source::Pipe(pipe)) => Some(f(pipe)), + _ => None, + }) + } - // Possible scenarios: - // Scenario 1: will not write if is not connected yet but will enqueue the data - // Scenario 2: will not write if a exception is thrown (will be handled by onError) - // Scenario 3: will be queued in memory and will be flushed later - // Scenario 4: no write/end function exists (will be handled by onError) - self.call_write_or_end(Some(encoded_data), true); + fn read_start(&self) -> Option> { + let this: *mut Self = self.root_ptr(); + self.with_pipe(|pipe| pipe.read_start(this, Self::on_read_event)) } #[bun_uws::uws_callback(export = "WindowsNamedPipe__resume_stream")] pub(crate) fn resume_stream(&self) -> bool { - let Some(stream) = self.writer.with_mut(|w| w.get_stream()) else { - return false; - }; - let this: *mut Self = self.root_ptr(); - // SAFETY: `stream` is the live `*mut uv_stream_t` for our pipe - // (returned by `writer.get_stream()`); the `StreamReader` impl - // below routes the trampolines back to `self`. - let read_start_result = - unsafe { (*stream).read_start_ctx::(this) }.to_result(bun_sys::Tag::listen); - read_start_result.is_ok() + let resumed = matches!(self.read_start(), Some(Ok(()))); + self.arm_end_of_write_timer(); + resumed } + /// A read the kernel already has is left to finish; what it produces is + /// delivered after `resume_stream`. #[bun_uws::uws_callback(export = "WindowsNamedPipe__pause_stream")] pub(crate) fn pause_stream(&self) -> bool { - let Some(pipe) = self.uv_pipe() else { - return false; - }; - // SAFETY: live libuv handle alias; see `pipe`. - unsafe { (*pipe).read_stop() }; - true + self.cancel_end_of_write_timer(); + self.with_pipe(Pipe::read_stop).is_some() } #[bun_uws::uws_callback(export = "WindowsNamedPipe__flush")] @@ -517,35 +600,35 @@ impl WindowsNamedPipe { (self.handlers.on_timeout)(self.handlers.ctx); } - pub(crate) fn from( - pipe: Box, - handlers: Handlers, - vm: &'static VirtualMachine, - ) -> WindowsNamedPipe { + pub(crate) fn init(handlers: Handlers, vm: &'static VirtualMachine) -> WindowsNamedPipe { WindowsNamedPipe { vm, - // Leak the `Box` and keep only a non-owning `NonNull` alias. - // Ownership of the allocation is later transferred to - // `self.writer.source` via `start_with_pipe` in `start()`, which - // re-materialises the `Box` and is responsible for freeing it. - pipe: Cell::new(Some(NonNull::from(Box::leak(pipe)))), + connect_req: JsCell::new(None), wrapper: JsCell::new(None), deferred_writer_close: Cell::new(false), root: Cell::new(core::ptr::null_mut()), handlers, // defaults: writer: JsCell::new(StreamingWriter::default()), - incoming: JsCell::new(Vec::new()), ssl_error: JsCell::new(CertError::default()), - connect_req: JsCell::new(bun_core::ffi::zeroed::()), event_loop_timer: JsCell::new(EventLoopTimer::init_paused( EventLoopTimerTag::WindowsNamedPipe, )), current_timeout: Cell::new(0), flags: Cell::new(Flags::DISCONNECTED), // disconnected: bool = true is the only non-false default + end_of_write: Cell::new(EndOfWrite::Open), + end_of_write_timer: JsCell::new(EventLoopTimer::init_paused( + EventLoopTimerTag::WindowsNamedPipeEndOfWrite, + )), + peer_ended_writing: Cell::new(false), + stays_half_open: Cell::new(false), } } + pub(crate) fn set_stays_half_open(&self, stays_half_open: bool) { + self.stays_half_open.set(stays_half_open); + } + pub(crate) fn r#ref(&self) { (self.handlers.ref_ctx)(self.handlers.ctx); } @@ -554,43 +637,32 @@ impl WindowsNamedPipe { (self.handlers.deref_ctx)(self.handlers.ctx); } - /// `extern "C"` trampoline matching `uv_connect_cb` (`Pipe::connect`'s - /// `on_connect` parameter). Recovers `*mut Self` from `req->data` (set in - /// `connect()`) and forwards to the safe `&self` body. Only ever invoked - /// by libuv (coerces to the `uv_connect_cb` fn-pointer type at the - /// `Pipe::connect` call site). - extern "C" fn uv_on_connect(req: *mut uv::uv_connect_t, status: uv::ReturnCode) { - // SAFETY: `req` is `self.connect_req`, whose `data` was set to - // `self as *mut Self` in `connect`; the owning struct is kept alive by - // the `r#ref()` taken there until this callback runs. - let this = unsafe { (*req).data.cast::() }; - // SAFETY: as above. - unsafe { &*this }.on_connect(status); - } - - fn on_connect(&self, status: uv::ReturnCode) { - if let Some(pipe) = self.uv_pipe() { - // SAFETY: live libuv handle alias; see `pipe`. - unsafe { (*pipe).unref() }; + /// # Safety + /// `this` is the live pipe that called `Pipe::connect`, kept alive by the + /// ref `connect` took. + unsafe fn on_connect(this: *mut Self, result: bun_sys::Result) { + // SAFETY: fn contract. + let this = unsafe { &*this }; + this.connect_req.set(None); + match result { + Ok(pipe) => this.on_connected(pipe), + Err(err) => this.on_connect_error(err), } + this.deref(); + } - if let Some(err) = status.to_error(bun_sys::Tag::connect) { - // The writer never adopted the pipe (`start_with_pipe` only runs on - // the success branch below), so `on_error → close → writer.end()` - // has no source to close: it neither frees the pipe nor reports - // `on_close`. Do both here. `discard_unadopted_pipe` schedules the - // `uv_close` that frees the `Box`, like the synchronous early-error - // paths in `connect`/`open`/`get_accepted_by`, and `on_close` is - // what makes the owner (`handlers.on_close`) release its ref. - self.discard_unadopted_pipe(); - self.on_error(err); - self.on_close(); - self.deref(); - return; - } + /// No pipe was opened, so there is nothing to close: the owner hears the + /// error and then the close, where it (`handlers.on_close`) releases its + /// ref. + fn on_connect_error(&self, err: bun_sys::Error) { + let _keep_alive = self.keep_alive(); + (self.handlers.on_error)(self.handlers.ctx, err); + self.on_close(); + } + fn on_connected(&self, pipe: Pipe) { self.update_flags(|f| f.set(Flags::DISCONNECTED, false)); - if self.start(true) { + if self.start(pipe) { if self.is_tls() { // trigger onOpen and start the handshake let _ = self.with_wrapper(|w| w.start()); @@ -600,24 +672,22 @@ impl WindowsNamedPipe { } } self.flush(); - self.deref(); } - pub(crate) fn get_accepted_by( + /// Take over a client the listening pipe accepted. On `Err` the pipe is + /// closed and no handler has run. + pub(crate) fn accepted( &self, - server: &mut uv::Pipe, - ssl_ctx: Option<&boringssl::OwnedSslCtx>, + pipe: Pipe, + ssl_ctx: Option, ) -> bun_sys::Result<()> { - debug_assert!(self.pipe.get().is_some()); let _keep_alive = self.keep_alive(); - self.update_flags(|f| f.set(Flags::DISCONNECTED, true)); if let Some(tls) = ssl_ctx { self.update_flags(|f| f.set(Flags::IS_SSL, true)); - match WrapperType::init_with_ctx(tls.clone(), false, self.wrapper_handlers()) { + match WrapperType::init_with_ctx(tls, false, self.wrapper_handlers()) { Ok(w) => self.wrapper.set(Some(w)), Err(_) => { - self.discard_unadopted_pipe(); return bun_sys::Result::Err(bun_sys::Error { errno: bun_sys::E::EPIPE as _, syscall: bun_sys::Tag::connect, @@ -626,32 +696,9 @@ impl WindowsNamedPipe { } } } - let uv_loop = self.vm.uv_loop(); - let pipe = self.uv_pipe().unwrap(); - // SAFETY: live libuv handle alias; see `pipe`. - if let Err(e) = unsafe { (*pipe).init(uv_loop, false) }.to_result(bun_sys::Tag::pipe) { - self.discard_unadopted_pipe(); - return Err(e); - } - // Until the writer adopts it (start_with_pipe), a thread teardown closes - // this pipe through us; afterwards the writer re-records itself as owner. - uv::open_handles::set_owner( - pipe.cast(), - self.root_ptr().cast(), - Some(Self::stop_for_vm_teardown), - ); - - // SAFETY: as above. - if let Err(e) = server - .accept(unsafe { &mut *pipe }) - .to_result(bun_sys::Tag::accept) - { - self.discard_unadopted_pipe(); - return Err(e); - } self.update_flags(|f| f.set(Flags::DISCONNECTED, false)); - if self.start(false) { + if self.start(pipe) { if self.is_tls() { // trigger onOpen and start the handshake let _ = self.with_wrapper(|w| w.start()); @@ -663,104 +710,66 @@ impl WindowsNamedPipe { bun_sys::Result::Ok(()) } + /// Adopt an open pipe end. `created_here` says whose it is: the overlapped + /// end `Bun.spawn` made for a child's stdio and nothing has opened since, or + /// one somebody else opened. `fd` is closed with the socket; on `Err` it is + /// still the caller's. pub(crate) fn open( &self, fd: Fd, + created_here: bool, ssl_options: Option, owned_ctx: Option, ) -> bun_sys::Result<()> { - debug_assert!(self.pipe.get().is_some()); - self.update_flags(|f| f.set(Flags::DISCONNECTED, true)); - if let Some(result) = self.init_tls_wrapper(ssl_options, owned_ctx) { - if result.is_err() { - self.discard_unadopted_pipe(); - return result; - } - } - let uv_loop = self.vm.uv_loop(); - let pipe = self.uv_pipe().unwrap(); - // SAFETY: live libuv handle alias; see `pipe`. - if let Err(e) = unsafe { (*pipe).init(uv_loop, false) }.to_result(bun_sys::Tag::pipe) { - self.discard_unadopted_pipe(); - return Err(e); - } - // Until the writer adopts it (start_with_pipe), a thread teardown closes - // this pipe through us; afterwards the writer re-records itself as owner. - uv::open_handles::set_owner( - pipe.cast(), - self.root_ptr().cast(), - Some(Self::stop_for_vm_teardown), - ); - - // SAFETY: as above. - if let Err(e) = unsafe { (*pipe).open(fd.uv()) }.to_result(bun_sys::Tag::open) { - self.discard_unadopted_pipe(); - return Err(e); + result?; } + let loop_ = self.vm.uws_loop(); + let origin = if created_here { + PipeOrigin::Created + } else { + PipeOrigin::Foreign + }; + let pipe = Pipe::open(loop_, fd, origin, true)?; - self.r#ref(); - self.on_connect(uv::ReturnCode::ZERO); + let _keep_alive = self.keep_alive(); + self.on_connected(pipe); bun_sys::Result::Ok(()) } + /// The outcome, failures included, is reported from the loop. pub(crate) fn connect( &self, path: &[u8], ssl_options: Option, owned_ctx: Option, ) -> bun_sys::Result<()> { - debug_assert!(self.pipe.get().is_some()); - self.update_flags(|f| f.set(Flags::DISCONNECTED, true)); - let pipe = self.uv_pipe().unwrap(); - // ref because we are connecting - // SAFETY: live libuv handle alias; see `pipe`. - unsafe { (*pipe).ref_() }; - - if let Some(result) = self.init_tls_wrapper(ssl_options, owned_ctx) { - if result.is_err() { - self.discard_unadopted_pipe(); - return result; - } - } - let uv_loop = self.vm.uv_loop(); - // SAFETY: as above. - if let Err(e) = unsafe { (*pipe).init(uv_loop, false) }.to_result(bun_sys::Tag::pipe) { - self.discard_unadopted_pipe(); - return Err(e); + // A trailing NUL is a terminator; any other would truncate the name. + let path = path.strip_suffix(&[0]).unwrap_or(path); + if bun_core::strings::contains_char(path, 0) { + return Err(bun_sys::Error::from_code( + bun_sys::E::EINVAL, + bun_sys::Tag::connect, + )); } - // Until the writer adopts it (start_with_pipe), a thread teardown closes - // this pipe through us; afterwards the writer re-records itself as owner. - uv::open_handles::set_owner( - pipe.cast(), - self.root_ptr().cast(), - Some(Self::stop_for_vm_teardown), - ); - - let ctx: *mut Self = self.root_ptr(); - let req: *mut uv::uv_connect_t = self.connect_req.as_ptr(); - // SAFETY: `req` is our own cell storage; libuv stashes `req`/`ctx` until - // the connect callback fires (this struct outlives that). - unsafe { (*req).data = ctx.cast::() }; - // SAFETY: `pipe` is live (see above) and `req`/`pipe` are disjoint. - if let Some(err) = - unsafe { (*pipe).connect(&mut *req, path, ctx.cast::(), Self::uv_on_connect) } - .to_error(bun_sys::Tag::connect2) - { - self.discard_unadopted_pipe(); - return Err(err); + if let Some(result) = self.init_tls_wrapper(ssl_options, owned_ctx) { + result?; } + // The loop is kept alive until `on_connect`. + let request = Pipe::connect(self.vm.uws_loop(), path, self.root_ptr(), Self::on_connect)?; + self.connect_req.set(Some(request)); + // Released by `on_connect`. self.r#ref(); Ok(()) } /// Set up the in-process SSL wrapper for `connect`/`open`. Prefers a prebuilt /// `SSL_CTX` (moved into `wrapper`) so a memoised `tls.createSecureContext` - /// reaches this path with its CA bundle intact; on this branch `[buntls]` - /// returns `{secureContext}` and no longer spreads `{ca,cert,key}`, so the + /// reaches this path with its CA bundle intact; `[buntls]` returns + /// `{secureContext}` and does not spread `{ca,cert,key}`, so the /// `SSLConfig` fallback alone would build a CTX with an empty trust store /// and fail `DEPTH_ZERO_SELF_SIGNED_CERT`. - /// Returns null when neither input requested TLS. + /// Returns `None` when neither input requested TLS. fn init_tls_wrapper( &self, ssl_options: Option, @@ -797,42 +806,15 @@ impl WindowsNamedPipe { None } - pub(crate) fn start(&self, is_client: bool) -> bool { - self.update_flags(|f| f.set(Flags::IS_CLIENT, is_client)); - let Some(pipe_nn) = self.pipe.get() else { - return false; - }; - // SAFETY: live libuv handle alias; see `pipe`. - unsafe { (*pipe_nn.as_ptr()).unref() }; + fn start(&self, pipe: Pipe) -> bool { + // Reading does not hold the loop; the JS socket's `poll_ref` does. + pipe.unref(); let this: *mut Self = self.root_ptr(); - self.update_flags(|f| f.insert(Flags::PIPE_ADOPTED)); - let start_pipe_result = self.writer.with_mut(|w| { + self.writer.with_mut(|w| { w.set_parent(this); - // SAFETY: `start_with_pipe`'s contract is "Box-allocated pointer; - // ownership transfers to `self.source`". `pipe_nn` is the - // `NonNull` recorded from the `Box` leaked in `from()` - // and not yet adopted (asserted by `start_with_pipe`'s - // `debug_assert!(source.is_none())`). - unsafe { w.start_with_pipe(pipe_nn.as_ptr()) } + w.start_with_source(Source::Pipe(pipe)); }); - if let bun_sys::Result::Err(err) = start_pipe_result { - self.on_error(err); - return false; - } - let Some(stream) = self.writer.with_mut(|w| w.get_stream()) else { - self.on_error(bun_sys::Error::from_code( - bun_sys::E::PIPE, - bun_sys::Tag::read, - )); - return false; - }; - - // SAFETY: `stream` is the live `*mut uv_stream_t` for our pipe - // (returned by `writer.get_stream()`); the `StreamReader` impl - // below routes the trampolines back to `self`. - let read_start_result = - unsafe { (*stream).read_start_ctx::(this) }.to_result(bun_sys::Tag::listen); - if let bun_sys::Result::Err(err) = read_start_result { + if let Some(bun_sys::Result::Err(err)) = self.read_start() { self.on_error(err); return false; } @@ -859,27 +841,18 @@ impl WindowsNamedPipe { i32::try_from(encoded_data.len()).expect("int cast") } - /// `uv::open_handles` closes a not-yet-adopted pipe through here at teardown. - unsafe fn stop_for_vm_teardown(this: *mut core::ffi::c_void) { - // SAFETY: recorded right after `pipe.init` by this live object; replaced by - // the writer at adoption or dropped with the pipe (discard_unadopted_pipe). - unsafe { &*this.cast::() }.close_or_cancel_connect(); - } - - /// `close`, for a pipe that may still be connecting. That one is not the writer's yet, so - /// `close` has nothing to end and the connection would open later all the same: discarding the - /// pipe cancels the connect (libuv completes it with `UV_ECANCELED`), and `on_connect` reports - /// the error and the close. - pub(crate) fn close_or_cancel_connect(&self) { - if !self.flags.get().contains(Flags::PIPE_ADOPTED) { - self.discard_unadopted_pipe(); - return; - } - self.close(); - } - + /// A connect still in flight is abandoned and fails the way a cancelled + /// one does; its callback will not run. #[bun_uws::uws_callback(export = "WindowsNamedPipe__close")] pub(crate) fn close(&self) { + if self.connect_req.replace(None).is_some() { + self.on_connect_error(bun_sys::Error::from_code( + bun_sys::E::ECANCELED, + bun_sys::Tag::connect, + )); + self.deref(); + return; + } let _ = self.with_wrapper(|w| { let _ = w.shutdown(false); }); @@ -892,12 +865,13 @@ impl WindowsNamedPipe { let _ = w.shutdown(false); }); if handled.is_none() { - // Plain (non-TLS) named pipe: half-close the write side so the peer - // observes EOF. Without this, Socket.prototype.end() over a Windows - // named pipe (endNT → shutdown()) never signals the peer, and an - // allowHalfOpen peer waiting on 'end' hangs. `writer.end()` is - // idempotent and mirrors `close`'s unconditional writer teardown. - self.with_writer(|w| w.end()); + if self.flags.get().disconnected() { + // Not connected yet: what is queued is written and the pipe + // closed once it is. + self.with_writer(|w| w.end()); + } else { + self.begin_end_of_write(); + } } } @@ -906,11 +880,7 @@ impl WindowsNamedPipe { if let Some(wrapper) = self.wrapper_ref() { wrapper.shutdown_read(); } else { - if let Some(stream) = self.writer.with_mut(|w| w.get_stream()) { - // SAFETY: `stream` is the live pipe stream; `uv_read_stop` - // always succeeds and is a no-op if not reading. - unsafe { (*stream).read_stop() }; - } + let _ = self.with_pipe(Pipe::read_stop); } } @@ -920,7 +890,9 @@ impl WindowsNamedPipe { return wrapper.is_shutdown(); } - self.flags.get().disconnected() || self.writer.get().is_done + self.flags.get().disconnected() + || self.writer.get().is_done + || self.end_of_write.get() != EndOfWrite::Open } #[bun_uws::uws_callback(export = "WindowsNamedPipe__is_closed", no_catch)] @@ -944,13 +916,7 @@ impl WindowsNamedPipe { #[bun_uws::uws_callback(export = "WindowsNamedPipe__ssl_error", no_catch)] pub(crate) fn ssl_error(&self) -> us_bun_verify_error_t { - let err = self.ssl_error.get(); - us_bun_verify_error_t { - error_no: err.error_no, - // CertError.code/.reason are owned `Box`s; fall back to "" when absent. - code: err.code.as_deref().map_or(c"".as_ptr(), |c| c.as_ptr()), - reason: err.reason.as_deref().map_or(c"".as_ptr(), |c| c.as_ptr()), - } + self.ssl_error.get().as_verify_error() } pub(crate) fn reset_timeout(&self) { @@ -958,30 +924,10 @@ impl WindowsNamedPipe { } pub(crate) fn set_timeout_in_milliseconds(&self, ms: c_uint) { - if self.event_loop_timer.get().state == EventLoopTimerState::ACTIVE { - timer_all().remove(self.event_loop_timer.as_ptr()); - } - self.current_timeout.set(ms); - - // if the interval is 0 means that we stop the timer - if ms == 0 { - return; - } - - // reschedule the timer - // `EventLoopTimer.next` is the lower-tier `ElTimespec` stub; - // bridge from `bun_core::Timespec` until the lower tier switches. - let next = timespec::ms_from_now(bun_core::TimespecMockMode::ForceRealTime, ms as i64); - self.event_loop_timer.with_mut(|t| { - t.next = ElTimespec { - sec: next.sec, - nsec: next.nsec, - }; - }); - timer_all().insert( - core::ptr::addr_of!(self.event_loop_timer) - .cast::() - .cast_mut(), + crate::socket::upgraded_duplex::set_socket_timeout( + &self.event_loop_timer, + &self.current_timeout, + ms, ); } @@ -992,35 +938,19 @@ impl WindowsNamedPipe { } /// Free internal resources, it can be called multiple times. - // Private idempotent helper invoked from on_close and Drop. - // Owned fields (writer, wrapper, ssl_error) free themselves via their own Drop impls; only - // the side effects (timer cancel, read_stop, take()) remain explicit here. fn release_resources(&self) { bun_output::scoped_log!(WindowsNamedPipe, "deinit"); - // clear the timer + // clear the timers self.set_timeout(0); - // "The source is already - // closed by the time on_close reaches here (that close is what fired the - // callback)" is true ONLY for the writer-initiated close path - // (`WindowsStreamingWriterParent::on_close`). It is FALSE when we arrive - // via `ssl_on_close` (TLS close_notify): the underlying `uv_pipe_t` is - // still open and in libuv's handle_queue, so without an explicit close - // here the HANDLE survives ≥ one extra event-loop tick (until the - // embedding context's refcount hits 0 and `WindowsStreamingWriter::Drop` - // finally runs). Inline `close_without_reporting()` (private on the - // writer) so the source pipe is `uv_close`d NOW; the `get_fd() != INVALID` - // guard makes this a no-op on the writer-initiated path where the source - // was already taken, and `closed_without_reporting = true` keeps - // `on_close_source()` from re-entering `Parent::on_close` (we're already - // inside it). `current_payload` may still back an in-flight `uv_write` - // (cancelled async by `uv_close`) so it is left to the writer's own Drop. - if let Some(stream) = self.writer.with_mut(|w| w.get_stream()) { - // SAFETY: `stream` is the live pipe stream; `uv_read_stop` - // always succeeds and is a no-op if not reading. - unsafe { (*stream).read_stop() }; - } - self.writer.with_mut(|w| w.close_without_reporting()); - self.writer.with_mut(|w| w.outgoing = Default::default()); + self.cancel_end_of_write_timer(); + // A TLS close_notify gets here (`ssl_on_close`) with the pipe still + // open. `close`, not `close_without_reporting`: a write still in + // flight has to report back for the writer to release the ref it took + // for it. The `on_close` this reports is a no-op by now (`IS_CLOSED`). + self.writer.with_mut(|w| { + w.close(); + w.outgoing = Default::default(); + }); if !self.flags.get().contains(Flags::WRAPPER_BUSY) { self.wrapper.set(None); } @@ -1030,26 +960,10 @@ impl WindowsNamedPipe { impl Drop for WindowsNamedPipe { fn drop(&mut self) { - self.release_resources(); - // Reclaim the `Box` leaked in `from()` if it was never - // adopted by `self.writer.source` (early-error returns from - // `connect`/`open`/`get_accepted_by` before `start()` runs). Once - // `PIPE_ADOPTED` is set the writer is the sole owner and frees via - // its libuv close callback — touching it here would double-free. - // `close_and_destroy` handles both un-adopted states: if `pipe.init()` - // never ran (`loop_` still null) it just `Box::from_raw`-drops the - // allocation; if init() DID run before the later open/accept/connect - // failure it `uv_close`s first so the handle is unlinked from libuv's - // `handle_queue` before the heap block is freed. - if !self.flags.get().contains(Flags::PIPE_ADOPTED) { - if let Some(pipe) = self.pipe.take() { - // SAFETY: `pipe` is the `NonNull` from `Box::leak` in `from()`, - // never adopted (gated on `!PIPE_ADOPTED`); `close_and_destroy` - // is the unique reclaim and accepts both never-init'd and - // init'd-but-unowned handles. - unsafe { uv::Pipe::close_and_destroy(pipe.as_ptr()) }; - } - } + // Everything else was released by `on_close`, or never existed: the + // owner's last ref goes away either there or before a pipe was opened. + self.set_timeout(0); + self.cancel_end_of_write_timer(); } } @@ -1086,9 +1000,9 @@ pub(crate) extern "C" fn WindowsNamedPipe__ssl(this: *const c_void) -> *mut bori impl bun_io::pipe_writer::WindowsWriterParent for WindowsNamedPipe { #[inline] - unsafe fn loop_(this: *mut Self) -> *mut uv::Loop { + unsafe fn loop_(this: *mut Self) -> *mut bun_uws_sys::Loop { // SAFETY: BACKREF set via `set_parent`; shared-only read. - unsafe { (*this).vm.uv_loop() } + unsafe { (*this).vm.uws_loop() } } #[inline] unsafe fn ref_(this: *mut Self) { @@ -1103,7 +1017,6 @@ impl bun_io::pipe_writer::WindowsWriterParent for WindowsNamedPipe { } impl bun_io::pipe_writer::WindowsStreamingWriterParent for WindowsNamedPipe { - const HAS_ON_WRITABLE: bool = true; #[inline] unsafe fn on_write(this: *mut Self, amount: usize, status: WriteStatus) { // SAFETY: BACKREF set via `set_parent`; the callbacks take `&self`. @@ -1125,40 +1038,3 @@ impl bun_io::pipe_writer::WindowsStreamingWriterParent for WindowsNamedPipe { unsafe { (*this).on_close() } } } - -/// The three `stream.readStart` callbacks (alloc/error/read) baked into a -/// trait so the -/// `extern "C"` libuv trampoline is monomorphised over `WindowsNamedPipe`. -impl uv::StreamReader for WindowsNamedPipe { - #[inline] - fn on_read_alloc(this: &mut Self, suggested_size: usize) -> &mut [u8] { - let this: &Self = this; - // SAFETY: the returned region is the buffer's spare capacity handed to - // libuv for the pending read; nothing else touches `incoming` until - // `on_read` commits the byte count. - let incoming = unsafe { &mut *this.incoming.as_ptr() }; - // SAFETY: libuv writes into this region before `on_read` commits it. - let spare = unsafe { incoming.uv_alloc_spare_u8(suggested_size) }; - &mut spare[..suggested_size] - } - #[inline] - fn on_read_error(this: &mut Self, err: core::ffi::c_int) { - // The trampoline only reaches this arm when `nreads < 0`, and for any - // negative code `translate_uv_error_to_e` already - // yields a concrete `E` (falling back to `UNKNOWN` for unmapped - // codes). Pass it straight through; do NOT remap UNKNOWN→CANCELED. - let e = bun_sys::windows::translate_uv_error_to_e(err); - let this: &Self = this; - this.on_read_error(e); - } - #[inline] - unsafe fn on_read(this: *mut Self, data: &[u8]) { - // `data` points into `(*this).incoming` (it was returned from - // `on_read_alloc`). Capture the only thing the body needs (length) - // and drop the slice before touching `*this`. - let nread = data.len(); - // SAFETY: `this` is the live context stashed in `handle.data` by - // `read_start_ctx`; `data` is no longer live. - unsafe { &*this }.on_read(nread); - } -} diff --git a/src/runtime/socket/WindowsNamedPipeContext.rs b/src/runtime/socket/WindowsNamedPipeContext.rs index e50147e70be8..7871679313d4 100644 --- a/src/runtime/socket/WindowsNamedPipeContext.rs +++ b/src/runtime/socket/WindowsNamedPipeContext.rs @@ -8,11 +8,9 @@ use crate::socket::SSLConfig; use crate::socket::windows_named_pipe::{Handlers as NamedPipeHandlers, WindowsNamedPipe}; use crate::socket::{TCPSocket, TLSSocket}; use bun_boringssl_sys as boringssl; -use bun_core::ZStr; use bun_event_loop::Task; use bun_jsc::virtual_machine::VirtualMachine; use bun_jsc::{GlobalRef, SysErrorJsc}; -use bun_sys::windows::libuv as uv; use bun_sys::{self, Error as SysError, Fd, SystemErrno}; use bun_uws::{self as uws, us_bun_verify_error_t}; @@ -34,14 +32,12 @@ pub(crate) struct WindowsNamedPipeContext { // deref'd in `Drop` before the `named_pipe` field drops — teardown order // must stay socket.deref() then named_pipe deinit. socket: SocketType, - /// `pub(super)` so `WindowsNamedPipeListeningContext::on_client_connect` - /// (sibling module) can call `get_accepted_by` on the freshly-created - /// client. + /// `pub(super)` so `WindowsNamedPipeListeningContext::on_connection` + /// (sibling module) can call `accepted` on the freshly-created client. pub(super) named_pipe: WindowsNamedPipe, vm: &'static VirtualMachine, global_this: GlobalRef, - task_event: EventState, is_open: bool, /// A socket over a Windows named pipe is in no uSockets group: the context /// that opened it closes it through this owner when it stops. @@ -51,34 +47,25 @@ pub(crate) struct WindowsNamedPipeContext { bun_jsc::impl_abort_handle_owner!(WindowsNamedPipeContext, abort_handle, |this, _cause| { // SAFETY: trait contract — `this` is live; `close` re-enters `on_close`, // which may free `this`. - unsafe { (*ptr::addr_of_mut!((*this).named_pipe)).close_or_cancel_connect() } + unsafe { (*ptr::addr_of_mut!((*this).named_pipe)).close() } }); /// Reached from `on_close` → `Self::deref` while `WindowsNamedPipe::on_close` -/// still holds a live `&mut (*this).named_pipe` and uses it after we return, so -/// project raw fields only — same constraint as the `on_*` handlers below. +/// still holds `&(*this).named_pipe`, so project raw fields only, as the `on_*` +/// handlers below do. fn schedule_deinit(this: *mut WindowsNamedPipeContext) { // SAFETY: called from `deref()` at count zero; `this` is live until the task fires. - // `task_event`/`vm`/`task` are disjoint from the caller's `&mut named_pipe`, and - // `vm` is `&'static` (JSC_BORROW) so `enqueue_task`'s `&mut` goes through a raw cast. + // `vm` is disjoint from the caller's `&named_pipe`, and is `&'static` + // (JSC_BORROW) so `enqueue_task`'s `&mut` goes through a raw cast. unsafe { - debug_assert!((*this).task_event != EventState::Deinit); - (*this).task_event = EventState::Deinit; let vm = ptr::from_ref::((*this).vm).cast_mut(); (*vm).enqueue_task(Task::init(this)); } } -#[repr(u8)] -#[derive(Copy, Clone, PartialEq, Eq)] -pub(crate) enum EventState { - Deinit, - None, -} - /// Intrusive-refcounted self-pointers into the wrapped JS socket (a *different* /// allocation from this context, so `ThisPtr`'s `Deref` is sound on them). -/// `Copy` so matching by value avoids `&self.socket` aliasing `&mut self.named_pipe`. +/// `Copy`, so the handlers read it by value through a raw place. #[derive(Copy, Clone)] pub(crate) enum SocketType { Tls(bun_ptr::ThisPtr), @@ -86,14 +73,8 @@ pub(crate) enum SocketType { None, } -/// Build a `uws::NewSocketHandler` from the wrapped named pipe. -/// -/// Takes a raw `*mut WindowsNamedPipe` (NOT `&mut`) because every caller is a -/// `NamedPipeHandlers` callback invoked *from* `WindowsNamedPipe::on_*`, which -/// already holds a live `&mut WindowsNamedPipe` to the same field and touches -/// it again after the callback returns. Forming a second `&mut` here would -/// retag the field and invalidate the caller's reference (Stacked Borrows). -/// The handler only needs the raw address to stuff into `InternalSocket::Pipe`. +/// Build a `uws::NewSocketHandler` from the wrapped named pipe; +/// `InternalSocket::Pipe` stores the raw address. #[inline] fn socket_from_named_pipe( pipe: *mut WindowsNamedPipe, @@ -103,16 +84,8 @@ fn socket_from_named_pipe( } } -/// Dispatch a `SocketType` value to a single body written generically over -/// `NewSocket`. Binds the inner `ThisPtr>` as `$s` -/// and a per-arm `const $ssl: bool` so the body can call -/// `NewSocket::on_x($s, socket_from_named_pipe::<$ssl>(..), ..)` once instead -/// of hand-duplicating the `Tls`/`Tcp` arms. `SocketType::None` is a no-op. -/// -/// Takes the `SocketType` by *value* (Copy) — not `*mut Self` — so callers -/// that must snapshot before mutating (`on_close`) or branch and re-match -/// (`on_error`) pass their saved copy; see the Stacked-Borrows note on the -/// `on_*` block below. +/// Run `$body` for the `Tls` or `Tcp` arm of a `SocketType` value, with `$s` +/// the socket and `$ssl` its `SSL` const. `SocketType::None` is a no-op. macro_rules! match_socket { ($scrutinee:expr, |$s:ident: NewSocket<$ssl:ident>| $body:expr) => { // This context is the named-pipe sockets' trampoline: what a handler @@ -135,19 +108,9 @@ macro_rules! match_socket { // ── NamedPipeHandlers callbacks ────────────────────────────────────────────── // -// All eight `on_*` handlers below take `this: *mut Self` (NOT `&mut self`). -// They are invoked from `WindowsNamedPipe::on_*` via `(self.handlers.on_x)(ctx, ..)` -// where the caller already holds a live `&mut WindowsNamedPipe` — i.e. a -// `&mut (*this).named_pipe` — and *uses it again after the handler returns* -// (e.g. `self.incoming.clear()`, `self.close()`, `self.release_resources()`). -// -// Forming `&mut *this` (or `&mut (*this).named_pipe`) here would retag from the -// allocation-root provenance and pop the caller's Unique tag off the borrow -// stack → Stacked Borrows UB / LLVM `noalias` violation when control returns. -// -// Instead each handler projects only the disjoint fields it needs (`socket`, -// `is_open`, `global_this`) via raw-pointer place expressions, and passes -// `addr_of_mut!((*this).named_pipe)` as a raw pointer without retagging. +// The `on_*` handlers take `this: *mut Self`: `WindowsNamedPipe::on_*` calls +// them through its `&self`, a live `&(*this).named_pipe`, so none may form +// `&mut Self`. Each reads and writes the fields it needs through raw places. /// Fails the pending connect and releases `create()`'s sole ref, unless /// `disarm()` runs first. struct FailAndRelease(Option<*mut WindowsNamedPipeContext>); @@ -174,7 +137,7 @@ impl WindowsNamedPipeContext { fn on_open(this: *mut Self) { // SAFETY: `this` is the live ctx ptr registered in `create()`; `is_open`, // `socket` and the `named_pipe` field *address* are all reachable without - // forming a reference that overlaps the caller's `&mut named_pipe`. + // forming a reference that overlaps the caller's `&named_pipe`. let (socket, pipe) = unsafe { (*this).is_open = true; ((*this).socket, ptr::addr_of_mut!((*this).named_pipe)) @@ -256,7 +219,7 @@ impl WindowsNamedPipeContext { if is_open { match_socket!(socket, |s: NewSocket| { // SAFETY: `this` is live; `global_this` is disjoint from the caller's - // `&mut named_pipe` and the borrow ends before `handle_error` runs JS. + // `&named_pipe` and the borrow ends before `handle_error` runs JS. let js_err = err.to_js(unsafe { &(*this).global_this }); s.handle_error(js_err) }); @@ -315,19 +278,14 @@ impl WindowsNamedPipeContext { unsafe { Self::deref(this) }; } + /// The task `schedule_deinit` queued: frees the context. + /// /// # Safety - /// `this` is the live queued pointer; the call may free it. + /// `this` is the live queued pointer; the call frees it. pub(crate) unsafe fn run_event(this: *mut Self) { - // SAFETY: called from the `task_tag::WindowsNamedPipeContext` dispatch - // arm; `this` is the live ctx pointer registered in create() - match unsafe { (*this).task_event } { - EventState::Deinit => { - // SAFETY: `this` was allocated via heap::alloc in create(); refcount hit zero - // and this deferred task is the sole remaining owner. Drop runs field destructors. - drop(unsafe { bun_core::heap::take(this) }); - } - EventState::None => panic!("Invalid event state"), - } + // SAFETY: `this` was allocated via heap::alloc in create(); refcount hit zero + // and this deferred task is the sole remaining owner. Drop runs field destructors. + drop(unsafe { bun_core::heap::take(this) }); } /// Owns the freshly-`create()`d context until `disarm()`: on any early @@ -356,20 +314,14 @@ impl WindowsNamedPipeContext { >::new_uninit()) .cast(); - // named_pipe owns the pipe (PipeWriter owns the pipe and will close and deinit it) // Non-capturing closures coerce to `fn(*mut c_void, …)`; each casts the - // erased ctx ptr back to `*mut Self` and forwards it RAW — the callee - // must not form `&mut Self` (see the doc-comment on the `on_*` block - // above for the Stacked-Borrows constraint vs the caller's - // `&mut WindowsNamedPipe`). + // erased ctx ptr back to `*mut Self` and forwards it raw (see the note + // above the `on_*` handlers). let handlers = NamedPipeHandlers { ctx: this.cast::(), // SAFETY: `p` is the `ctx` set above (`this.cast()`); the // WindowsNamedPipe never invokes a handler after `on_close` // schedules deinit, so the allocation is live for the call. - // `rc_ref` projects `ref_count` via raw place — `(*p).ref_()` would - // autoref `&Self` over the whole struct, but `WindowsNamedPipe::r#ref` - // holds `&mut (*this).named_pipe` across this callback. ref_ctx: |p| unsafe { ::rc_ref(p.cast::()) }, // SAFETY: `p` is the `ctx` set above (`this.cast()`); the allocation is live for the call (see `ref_ctx`). deref_ctx: |p| unsafe { Self::deref(p.cast::()) }, @@ -385,10 +337,7 @@ impl WindowsNamedPipeContext { on_keylog: |p, d| Self::on_keylog(p.cast::(), d), server_identity: |p, ssl| Self::server_identity(p.cast::(), ssl), }; - let named_pipe = { - let pipe = Box::new(bun_core::ffi::zeroed::()); - WindowsNamedPipe::from(pipe, handlers, vm) - }; + let named_pipe = WindowsNamedPipe::init(handlers, vm); // SAFETY: `this` is freshly allocated uninit storage exclusively owned here; we write // every field exactly once before any read. unsafe { @@ -400,7 +349,6 @@ impl WindowsNamedPipeContext { named_pipe, vm, global_this, - task_event: EventState::None, is_open: false, abort_handle: bun_jsc::AbortHandle::for_owner::(), }, @@ -426,10 +374,11 @@ impl WindowsNamedPipeContext { /// `owned_ctx` is moved into `named_pipe.open`. Prefer it over `ssl_config` so a /// memoised `tls.createSecureContext` reaches this path with its trust store intact — /// on this branch `[buntls]` returns `{secureContext}` only, so `ssl_config` - /// alone would be empty. + /// alone would be empty. `created_here`: see [`WindowsNamedPipe::open`]. pub(crate) fn open( cx: &bun_jsc::JsThread<'_>, fd: Fd, + created_here: bool, ssl_config: Option, owned_ctx: Option, socket: SocketType, @@ -442,7 +391,11 @@ impl WindowsNamedPipeContext { let mut guard = Self::armed(this); // SAFETY: `this` is live and exclusively accessed here - unsafe { (*guard.get()).named_pipe.open(fd, ssl_config, owned_ctx) }?; + unsafe { + (*guard.get()) + .named_pipe + .open(fd, created_here, ssl_config, owned_ctx) + }?; let this = guard.disarm(); // SAFETY: `this` is live; returning interior pointer to heap-allocated field (BACKREF) @@ -463,24 +416,11 @@ impl WindowsNamedPipeContext { let mut guard = Self::armed(this); // SAFETY: `this` is live and exclusively accessed here - let named_pipe = unsafe { &mut (*guard.get()).named_pipe }; - - if path[path.len() - 1] == 0 { - // is already null terminated - // SAFETY: path[path.len()-1] == 0 checked above - let slice_z = ZStr::from_slice_with_nul(path); - named_pipe.connect(slice_z, ssl_config, owned_ctx)?; - } else { - let mut path_buf = bun_paths::path_buffer_pool::get(); - // we need to null terminate the path - let len = path.len().min(path_buf.len() - 1); - - path_buf[..len].copy_from_slice(&path[..len]); - path_buf[len] = 0; - // SAFETY: path_buf[len] == 0 written above - let slice_z = ZStr::from_buf(&path_buf[..], len); - named_pipe.connect(slice_z, ssl_config, owned_ctx)?; - } + unsafe { + (*guard.get()) + .named_pipe + .connect(path, ssl_config, owned_ctx) + }?; let this = guard.disarm(); // SAFETY: `this` is live; returning interior pointer to heap-allocated field (BACKREF) @@ -507,7 +447,7 @@ impl Drop for WindowsNamedPipeContext { impl bun_event_loop::Taskable for WindowsNamedPipeContext { const TAG: bun_event_loop::TaskTag = bun_event_loop::task_tag::WindowsNamedPipeContext; - /// A `Deinit` hop (refcount already zero) that will not run: `this` is the + /// A `schedule_deinit` hop (refcount already zero) that will not run: `this` is the /// heap context, freed by nobody else — do what the hop does, script-free. unsafe fn release_unrun(this: *mut Self) { // SAFETY: fn contract. diff --git a/src/runtime/socket/socket_body.rs b/src/runtime/socket/socket_body.rs index c9a5cebd21fa..0e400b30dab8 100644 --- a/src/runtime/socket/socket_body.rs +++ b/src/runtime/socket/socket_body.rs @@ -1262,8 +1262,8 @@ impl NewSocket { BunString::static_("ECONNREFUSED") }; #[cfg(windows)] - let errno_ = -sys::windows::libuv::e_discriminant_to_uv(errno_ as u16) - .unwrap_or(sys::windows::libuv::UV_ECONNREFUSED); + let errno_ = + -bun_errno::e_to_uv(errno_ as u16).unwrap_or(bun_errno::uv_codes::UV_ECONNREFUSED); SystemError { errno: -errno_, message: BunString::static_("Failed to connect"), @@ -3244,6 +3244,17 @@ impl NewSocket { if callframe.arguments_count() > 0 && arg.to_boolean() { this.socket.get().shutdown_read(); } else { + // node:net makes every native socket half-open and keeps the + // user's `allowHalfOpen` to itself. + #[cfg(windows)] + if let uws::InternalSocket::Pipe(pipe) = this.socket.get().socket + && let [_, stays_half_open] = callframe.arguments_as_array::<2>() + && stays_half_open.is_boolean() + { + // SAFETY: a socket's pipe is live while the socket refers to it. + unsafe { &*pipe.cast::() } + .set_stays_half_open(stays_half_open.to_boolean()); + } this.socket.get().shutdown(); } @@ -3391,10 +3402,10 @@ impl NewSocket { #[bun_jsc::host_fn(getter)] pub(crate) fn get_fd(this: &Self, _global: &JSGlobalObject) -> JSValue { // On Windows the fd is a system-kind SOCKET handle; routing it through - // `.uv()` panics for anything but stdio. The sys_jsc helper branches on - // kind (system→u64, uv→i32, posix→i32). + // `.crt()` panics for anything but stdio. The sys_jsc helper branches on + // kind (system→u64, crt→i32, posix→i32). use bun_sys_jsc::FdJsc as _; - this.socket.get().fd().to_js_without_making_lib_uv_owned() + this.socket.get().fd().to_js_without_making_crt_owned() } #[bun_jsc::host_fn(getter)] diff --git a/src/runtime/socket/udp_socket.rs b/src/runtime/socket/udp_socket.rs index d919f2af5184..4943b3f727bc 100644 --- a/src/runtime/socket/udp_socket.rs +++ b/src/runtime/socket/udp_socket.rs @@ -17,7 +17,7 @@ use bun_ptr::BackRef; use crate::node::validators; use bun_cares_sys::c_ares_draft as c_ares; #[cfg(windows)] -use bun_libuv_sys::sockaddr_storage; +use bun_sys::windows::ws2_32::sockaddr_storage; use bun_sys::{self, SystemErrno}; use bun_uws as uws; #[cfg(not(windows))] diff --git a/src/runtime/socket/uws_dispatch.rs b/src/runtime/socket/uws_dispatch.rs index d3602bf5d854..905552d6e272 100644 --- a/src/runtime/socket/uws_dispatch.rs +++ b/src/runtime/socket/uws_dispatch.rs @@ -68,7 +68,10 @@ static TABLES: std::sync::LazyLock<[Option<&'static VTable>; SOCKET_KIND_COUNT]> t[SocketKind::ValkeyTls as usize] = Some(vtable::make::>()); // IPC - t[SocketKind::SpawnIpc as usize] = Some(vtable::make::()); + #[cfg(not(windows))] + { + t[SocketKind::SpawnIpc as usize] = Some(vtable::make::()); + } t }); diff --git a/src/runtime/socket/uws_handlers.rs b/src/runtime/socket/uws_handlers.rs index a79633665617..a959f00560d6 100644 --- a/src/runtime/socket/uws_handlers.rs +++ b/src/runtime/socket/uws_handlers.rs @@ -10,7 +10,7 @@ use bun_jsc::JsResult; use bun_ptr::{RefPtr, ThisPtr}; -use core::ffi::{c_int, c_void}; +use core::ffi::c_void; use core::ptr::NonNull; use bun_uws::{ConnectingSocket, NewSocketHandler}; @@ -19,6 +19,7 @@ use bun_uws_sys::vtable::Handler as VHandler; use bun_uws_sys::{us_bun_verify_error_t, us_socket_t}; use crate::api; +#[cfg(not(windows))] use crate::ipc as IPC; use crate::valkey_jsc::js_valkey; use bun_http_jsc::websocket_client; @@ -716,11 +717,15 @@ pub(crate) type Valkey = // Ext is `*IPC.SendQueue` for both child-side `process.send` and parent-side // `Bun.spawn({ipc})`. The IPC handlers are free functions, not // methods on SendQueue, so we adapt manually here. +#[cfg(not(windows))] pub(crate) struct SpawnIPC; -use IPC::IPCHandlers::PosixSocket as IpcH; +#[cfg(not(windows))] +use IPC::posix_socket as IpcH; +#[cfg(not(windows))] type IpcS = NewSocketHandler; +#[cfg(not(windows))] impl VHandler for SpawnIPC { type Ext = ExtSlot; @@ -737,7 +742,7 @@ impl VHandler for SpawnIPC { let Some(this) = ext.owner_ref() else { return }; IpcH::on_data(this, IpcS::from(s), data); } - fn on_fd(ext: &mut Self::Ext, s: *mut us_socket_t, fd: c_int) { + fn on_fd(ext: &mut Self::Ext, s: *mut us_socket_t, fd: core::ffi::c_int) { let Some(this) = ext.owner_ref() else { return }; IpcH::on_fd(this, IpcS::from(s), fd); } diff --git a/src/runtime/timer/mod.rs b/src/runtime/timer/mod.rs index ec72b1beed2b..1e5ceace2cd5 100644 --- a/src/runtime/timer/mod.rs +++ b/src/runtime/timer/mod.rs @@ -3,10 +3,6 @@ use bun_collections::ArrayHashMap; use bun_core::{Timespec, TimespecMockMode}; -#[cfg(windows)] -use bun_libuv_sys::UvHandle as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; use bun_threading::Guarded; // Low-tier timer node + tag (per §Dispatch hot-path list, the `match tag` @@ -586,8 +582,6 @@ pub(crate) struct All { pub(crate) thread_id: std::thread::ThreadId, pub(crate) timers: TimerHeap, pub(crate) active_timer_count: i32, - #[cfg(windows)] - pub(crate) uv_timer: bun_sys::windows::libuv::Timer, /// Whether we have emitted a warning for passing a negative timeout duration pub(crate) warned_negative_number: bool, /// Whether we have emitted a warning for passing NaN for the timeout duration @@ -596,8 +590,6 @@ pub(crate) struct All { /// TimerObjectInternals.epoch. Masked to 25 bits on increment. pub(crate) epoch: u32, pub(crate) immediate_ref_count: i32, - #[cfg(windows)] - pub(crate) uv_idle: bun_sys::windows::libuv::uv_idle_t, pub(crate) event_loop_delay: EventLoopDelayMonitor, pub(crate) fake_timers: FakeTimers, pub(crate) maps: Maps, @@ -612,14 +604,10 @@ impl All { thread_id: std::thread::current().id(), timers: TimerHeap::default(), active_timer_count: 0, - #[cfg(windows)] - uv_timer: bun_core::ffi::zeroed(), warned_negative_number: false, warned_not_number: false, epoch: 0, immediate_ref_count: 0, - #[cfg(windows)] - uv_idle: bun_core::ffi::zeroed(), event_loop_delay: EventLoopDelayMonitor::default(), fake_timers: FakeTimers::default(), maps: Maps::default(), @@ -666,126 +654,9 @@ impl All { (*timer).state = EventLoopTimerState::ACTIVE; (*timer).in_heap = InHeap::Regular; } - #[cfg(windows)] - self.ensure_uv_timer(); } } - /// The owning thread's JSC VM is gone (nothing schedules a WTFTimer any - /// more) and the timeout objects are drained: hand the embedded - /// `uv_timer_t`/`uv_idle_t` to `uv_close` so their nodes leave the loop's - /// handle queue when the teardown closes the loop — before this struct's - /// storage is freed. - #[cfg(windows)] - pub(crate) fn close_loop_handles_for_vm_teardown(&mut self) { - unsafe extern "C" fn timer_closed(_: *mut uv::Timer) {} - unsafe extern "C" fn idle_closed(_: *mut uv::uv_idle_t) {} - if !self.uv_timer.data.is_null() { - self.uv_timer.stop(); - self.uv_timer.close(timer_closed); - } - if !self.uv_idle.data.is_null() { - self.uv_idle.stop(); - self.uv_idle.close(idle_closed); - } - } - - /// Lazily `uv_timer_init` the - /// per-`All` libuv timer, then (re)start it for the soonest deadline - /// across both heaps. On Windows there is no epoll/kqueue fallback; this - /// `uv_timer_t` is the ONLY thing that wakes `uv_run` for JS timers. - #[cfg(windows)] - fn ensure_uv_timer(&mut self) { - // `vm` here means the OWNING VM (the one this timer is embedded in), - // not the calling thread's. Guard the TLS fallback so a cross-thread - // caller fails loudly instead of silently arming a fresh `uv_loop_t` - // on the wrong thread. - debug_assert!( - self.thread_id == std::thread::current().id(), - "ensure_uv_timer: called off the owning JS thread; TLS loop/VM would diverge from vm.event_loop_handle", - ); - if self.uv_timer.data.is_null() { - self.uv_timer.init(uv::Loop::get()); - self.uv_timer.data = - bun_jsc::virtual_machine::VirtualMachine::get_mut_ptr().cast::(); - self.uv_timer.unref(); - } - debug_assert!( - !self.uv_timer.is_closing(), - "timer scheduled after teardown closed the heap's uv timer" - ); - - let reg_next = self.timers.peek().map(|timer| { - // SAFETY: `peek` returns a live heap node. - let next = unsafe { &(*timer).next }; - Timespec { - sec: next.sec, - nsec: next.nsec, - } - }); - let wtf_next = self.wtf_timers.lock().peek().map(|timer| { - // SAFETY: `peek` returns a live heap node. - let next = unsafe { &(*timer).next }; - Timespec { - sec: next.sec, - nsec: next.nsec, - } - }); - let Some(next_ts) = Self::soonest(reg_next, wtf_next) else { - return; - }; - - // SAFETY: `uv_timer.data` is non-null past the lazy-init block, so - // `uv_timer_init` has run and the handle's `loop` field points at - // the owning VM's live `uv_loop_t` (== `vm.uvLoop()` per spec). - unsafe { uv::uv_update_time(self.uv_timer.get_loop()) }; - let now = Timespec::now(TimespecMockMode::ForceRealTime); - let wait = if next_ts.greater(&now) { - next_ts.duration(&now) - } else { - Timespec { sec: 0, nsec: 0 } - }; - - // minimum 1ms - // https://github.com/nodejs/node/blob/f552c86fecd6c2ba9e832ea129b731dd63abdbe2/src/env.cc#L1512 - let wait_ms = core::cmp::max(1, wait.ms_unsigned()); - - // SAFETY: `uv_timer_init` ran above; the handle is live. - let due_in = unsafe { uv::uv_timer_get_due_in(&self.uv_timer) }; - // Restarting an overdue handle shifts the wakeup out by 1ms. Done - // on every insert, the already-due callback never runs. - if !(self.uv_timer.is_active() && due_in <= wait_ms) { - self.uv_timer.start(wait_ms, 0, Some(Self::on_uv_timer)); - } - - if self.active_timer_count > 0 { - self.uv_timer.ref_(); - } else { - self.uv_timer.unref(); - } - } - - /// libuv timer callback; drain due - /// timers then re-arm for the next deadline. Only ever invoked by libuv - /// (coerces to the `uv_timer_cb` fn-pointer type at the `Timer::start` - /// call site); body wraps its derefs explicitly. - #[cfg(windows)] - extern "C" fn on_uv_timer(uv_timer_t: *mut uv::Timer) { - // SAFETY: `uv_timer_t` is the address of `All.uv_timer` (libuv passes - // back exactly the handle pointer we registered in `ensure_uv_timer`); - // recover the containing `All` via container_of. - let all: *mut All = unsafe { bun_core::from_field_ptr!(All, uv_timer, uv_timer_t) }; - // SAFETY: `data` was set to the VM ptr in `ensure_uv_timer` (non-null). - let vm: *mut () = unsafe { (*uv_timer_t).data.cast() }; - // SAFETY: callback fires on the JS thread (libuv invokes on the loop's - // thread); `all` is live for the VM lifetime. `drain_timers` may - // re-enter `(*runtime_state()).timer` — it forms only short-lived - // `&mut All` around heap pop/peek, so the raw-ptr deref here is sound. - unsafe { (*all).drain_timers(vm) }; - // SAFETY: see above; re-arm for the next-soonest deadline (if any). - unsafe { (*all).ensure_uv_timer() }; - } - pub(crate) fn remove(&mut self, timer: *mut EventLoopTimer) { self.assert_js_thread(); // SAFETY: caller guarantees `timer` is a valid live EventLoopTimer. @@ -858,10 +729,6 @@ impl All { (*timer).state = EventLoopTimerState::ACTIVE; } } - #[cfg(windows)] - if self.thread_id == std::thread::current().id() { - self.ensure_uv_timer(); - } } fn wtf_disarm(&mut self, timer: *mut EventLoopTimer) { @@ -927,7 +794,7 @@ impl All { } } - /// Called from `EventLoop::auto_tick` to compute the epoll/kqueue timeout. + /// Called from `EventLoop::auto_tick` to compute the loop's wait timeout. /// Returns `true` if `spec` was written. `now_out` receives the monotonic reading this /// took, if any, for the caller to share with the tick (see `NOW_NS_UNKNOWN`). /// @@ -947,13 +814,10 @@ impl All { vm: *mut (), /* erased *mut VirtualMachine, forwarded to fire() */ now_out: &mut Option, ) -> bool { - #[cfg(unix)] if has_pending_immediate { *spec = Timespec { sec: 0, nsec: 0 }; return true; } - #[cfg(not(unix))] - let _ = has_pending_immediate; let this: *mut Self = self; let maybe_now: &mut Option = now_out; @@ -1095,43 +959,12 @@ impl All { let new = old + delta; self.immediate_ref_count = new; if old <= 0 && new > 0 { - #[cfg(not(windows))] // SAFETY: caller passes the VM's live uws loop unsafe { &mut *uws_loop }.ref_(); - #[cfg(windows)] - { - // Lazy-init the idle handle and start - // it with a no-op callback so `uv_run` does not block in poll - // while immediates are pending (matches Node.js). - if self.uv_idle.data.is_null() { - self.uv_idle.init(uv::Loop::get()); - // Note: `data` is only used as a - // non-null "initialized" sentinel — never dereferenced. - self.uv_idle.data = bun_jsc::virtual_machine::VirtualMachine::get_mut_ptr() - .cast::(); - } - self.uv_idle.start(Some(Self::on_uv_idle_noop)); - } } else if old > 0 && new <= 0 { - #[cfg(not(windows))] // SAFETY: caller passes the VM's live uws loop unsafe { &mut *uws_loop }.unref(); - #[cfg(windows)] - if !self.uv_idle.data.is_null() { - self.uv_idle.stop(); - } } - #[cfg(windows)] - let _ = uws_loop; - } - - /// Empty `uv_idle` callback. Its presence alone - /// keeps `uv_run` from blocking in the poll phase; the body is a no-op. - /// No preconditions (the handle pointer is unused), so the fn is safe; the - /// safe fn item coerces into the `uv_idle_cb` fn-pointer slot. - #[cfg(windows)] - extern "C" fn on_uv_idle_noop(_: *mut uv::uv_idle_t) { - // prevent libuv from polling forever } /// # Safety @@ -1145,25 +978,12 @@ impl All { debug_assert!(new >= 0); self.active_timer_count = new; if old <= 0 && new > 0 { - #[cfg(not(windows))] // SAFETY: caller passes the VM's live uws loop unsafe { &mut *uws_loop }.ref_(); - // `uv_timer.ref()` is intentionally unconditional (no `data != - // null` guard). Invariant: every path that reaches a positive - // `active_timer_count` first inserts a timer, and `insert` - // → `ensure_uv_timer` lazily `uv_timer_init`s the handle. Guarding - // here would silently drop the ref and let the loop exit early. - #[cfg(windows)] - self.uv_timer.ref_(); } else if old > 0 && new <= 0 { - #[cfg(not(windows))] // SAFETY: caller passes the VM's live uws loop unsafe { &mut *uws_loop }.unref(); - #[cfg(windows)] - self.uv_timer.unref(); } - #[cfg(windows)] - let _ = uws_loop; } /// VM teardown, after `cancel_all_timeout_objects`: unlink every timer still diff --git a/src/runtime/webcore.rs b/src/runtime/webcore.rs index b5fe603a2ab3..d712a448ccea 100644 --- a/src/runtime/webcore.rs +++ b/src/runtime/webcore.rs @@ -337,7 +337,7 @@ pub(crate) enum PathOrFileDescriptor { // Held by ByteStream; dispatches write()/end() to the native sink. #[derive(Copy, Clone, Default)] -pub enum SinkHandle { +pub(crate) enum SinkHandle { #[default] None, ServerResponse(crate::server::AnyRequestContext), @@ -352,18 +352,18 @@ pub enum SinkHandle { impl SinkHandle { #[inline] - pub fn is_none(&self) -> bool { + pub(crate) fn is_none(&self) -> bool { matches!(self, SinkHandle::None) } #[inline] - pub fn is_some(&self) -> bool { + pub(crate) fn is_some(&self) -> bool { !self.is_none() } /// SAFETY: every non-None variant's pointee is kept alive by the hook-in site for as long /// as this handle is installed. - pub fn write(&self, data: &streams::Result) -> streams::Writable { + pub(crate) fn write(&self, data: &streams::Result) -> streams::Writable { match *self { SinkHandle::None => streams::Writable::Done, SinkHandle::ServerResponse(any) => any.write_chunk(data), @@ -385,7 +385,7 @@ impl SinkHandle { /// Signal end-of-stream (or terminal error) to the attached sink. /// /// SAFETY: same pointee-liveness invariant as [`Self::write`]. - pub fn end(&self, err: Option) { + pub(crate) fn end(&self, err: Option) { match *self { SinkHandle::None => {} SinkHandle::ServerResponse(any) => any.end_chunk(err.as_ref()), diff --git a/src/runtime/webcore/ArrayBufferSink.rs b/src/runtime/webcore/ArrayBufferSink.rs index 86dc9c7f330e..96f5fdcfc4f3 100644 --- a/src/runtime/webcore/ArrayBufferSink.rs +++ b/src/runtime/webcore/ArrayBufferSink.rs @@ -8,7 +8,7 @@ use bun_sys as syscall; // impl in `Sink.rs` (see `array_buffer_sink_abi`). #[derive(Default)] -pub struct ArrayBufferSink { +pub(crate) struct ArrayBufferSink { pub(crate) bytes: Vec, // allocator field dropped — global mimalloc (non-AST crate, see PORTING.md §Allocators) pub(crate) done: bool, @@ -43,7 +43,7 @@ impl ArrayBufferSink { Ok(()) } - pub fn flush(&mut self) -> bun_sys::Result<()> { + pub(crate) fn flush(&mut self) -> bun_sys::Result<()> { Ok(()) } @@ -78,7 +78,7 @@ impl ArrayBufferSink { }); } - pub fn write(&mut self, data: &streams::Result) -> streams::result::Writable { + pub(crate) fn write(&mut self, data: &streams::Result) -> streams::result::Writable { let len = match self.bytes.write(data.slice()) { Ok(len) => len, Err(_) => return streams::result::Writable::Err(syscall::Error::oom()), diff --git a/src/runtime/webcore/Blob.rs b/src/runtime/webcore/Blob.rs index 540a17195ec0..543edc4d210b 100644 --- a/src/runtime/webcore/Blob.rs +++ b/src/runtime/webcore/Blob.rs @@ -432,52 +432,25 @@ impl BlobExt for Blob { let handler = bun_core::heap::into_raw(Box::new(Handler::<'_, F>::new(self.dupe(), cx.global()))); - #[cfg(windows)] - { - // SAFETY: handler was just boxed; sole owner. - unsafe { (*handler).promise = jsc::JSPromiseStrong::init(cx.global()) }; - let promise_value = unsafe { (*handler).promise.value() }; - promise_value.ensure_still_alive(); - - read_file::ReadFileUV::start::>( - // `bun_vm()` returns the live VM for this global; the event - // loop outlives any in-flight async fs request. - cx.vm().event_loop(), - cx.context(), - self.store().expect("infallible: store present").clone(), - self.offset.get(), - self.size.get(), - handler, - ); - return promise_value; - } + let file_read = read_file::ReadFile::create( + self.store().expect("infallible: store present").clone(), + self.offset.get(), + self.size.get(), + ) + .unwrap_or_else(|e| bun_core::handle_oom(Err(e))); + // Create the Promise only after the store has been ref()'d. + // SAFETY: handler was just boxed; sole owner. + unsafe { (*handler).promise = jsc::JSPromiseStrong::init(cx.global()) }; + // SAFETY: same `handler` as above; still solely owned here. + let promise_value = unsafe { (*handler).promise.value() }; + promise_value.ensure_still_alive(); - #[cfg(not(windows))] - { - let file_read = read_file::ReadFile::create( - self.store().expect("infallible: store present").clone(), - self.offset.get(), - self.size.get(), - ) - .unwrap_or_else(|e| bun_core::handle_oom(Err(e))); - // Create the Promise only after the store has been ref()'d. - // SAFETY: handler was just boxed; sole owner. - unsafe { (*handler).promise = jsc::JSPromiseStrong::init(cx.global()) }; - // SAFETY: same `handler` as above; still solely owned here. - let promise_value = unsafe { (*handler).promise.value() }; - promise_value.ensure_still_alive(); - - read_file::ReadFile::schedule( - file_read, - read_file::ReadFileCompletionFns::of(handler), - cx, - ); + read_file::ReadFile::schedule(file_read, read_file::ReadFileCompletionFns::of(handler), cx); - debug!("doReadFile: read_file_task scheduled"); - promise_value - } + debug!("doReadFile: read_file_task scheduled"); + promise_value } - /// Read this Blob's bytes — file (`ReadFile`/`ReadFileUV`), S3 (`S3.download`), + /// Read this Blob's bytes — file (`ReadFile`), S3 (`S3.download`), /// or in-memory — and deliver them to `Handler::on_read_bytes(ctx, result)` on the /// JS thread without ever materialising a JSValue. `.ok` bytes are /// global-allocator-OWNED by the callback. The point is to give callers @@ -667,32 +640,17 @@ impl BlobExt for Blob { ctx: *mut C, cx: &bun_jsc::JsThread<'_>, ) { - #[cfg(windows)] - { - return read_file::ReadFileUV::start_with_ctx( - // SAFETY: `bun_vm()` returns the live VM for this global. - cx.vm().event_loop(), - cx.context(), - self.store().expect("infallible: store present").clone(), - self.offset.get(), - self.size.get(), - NewInternalReadFileHandler::::completion(ctx), - ); - } - #[cfg(not(windows))] - { - let file_read = read_file::ReadFile::create( - self.store().expect("infallible: store present").clone(), - self.offset.get(), - self.size.get(), - ) - .unwrap_or_else(|e| bun_core::handle_oom(Err(e))); - read_file::ReadFile::schedule( - file_read, - NewInternalReadFileHandler::::completion(ctx), - cx, - ); - } + let file_read = read_file::ReadFile::create( + self.store().expect("infallible: store present").clone(), + self.offset.get(), + self.size.get(), + ) + .unwrap_or_else(|e| bun_core::handle_oom(Err(e))); + read_file::ReadFile::schedule( + file_read, + NewInternalReadFileHandler::::completion(ctx), + cx, + ); } fn get_content_type(&self) -> Option> { let ct = self.content_type_slice(); @@ -1027,12 +985,12 @@ impl BlobExt for Blob { PathOrFileDescriptor::Fd(fd) => { #[cfg(windows)] match fd.decode_windows() { - bun_sys::fd::DecodeWindows::Uv(uv_file) => { + bun_sys::fd::DecodeWindows::Crt(crt_fd) => { bun_core::write_pretty!( writer, ENABLE_ANSI_COLORS, " (fd: {d})", - uv_file, + crt_fd, )?; } bun_sys::fd::DecodeWindows::Windows(handle) => { @@ -1429,134 +1387,40 @@ impl BlobExt for Blob { .to_js()); } - let file_sink: RefPtr = 'brk_sink: { - #[cfg(windows)] - { - let pathlike = &store.data.as_file().pathlike; - let fd: Fd = if let PathOrFileDescriptor::Fd(fd) = pathlike { - *fd - } else { - let mut file_path = bun_paths::path_buffer_pool::get(); - let path = pathlike.path().slice_z(&mut file_path); - let flags = bun_sys::O::WRONLY - | bun_sys::O::CREAT - | bun_sys::O::TRUNC - | bun_sys::O::NONBLOCK; - let mode = options.mode.unwrap_or(WRITE_PERMISSIONS); - let mut result = bun_sys::open(path, flags, mode); - if let bun_sys::Result::Err(err) = &result { - if err.get_errno() == bun_sys::E::ENOENT - && options.mkdirp_if_not_exists.unwrap_or(true) - { - result = match mkdirp_parent(path.as_bytes()) { - Ok(()) => bun_sys::open(path, flags, mode), - Err(err) => Err(err), - }; - } - } - match result { - bun_sys::Result::Ok(result) => result, - bun_sys::Result::Err(err) => { - return Ok(JSPromise::rejected_promise( - cx.global(), - err.with_path(path).to_js(cx.global()), - ) - .to_js()); - } - } - }; - - let is_stdout_or_stderr = 'brk: { - if !matches!(pathlike, PathOrFileDescriptor::Fd(_)) { - break 'brk false; - } - - if let Some(rare) = cx.vm().rare_data.as_ref() { - // `RareData::std{out,err}_store` is `Option>` - // (type-erased `*Blob.Store`); compare on raw pointer - // identity exactly like the POSIX arm below. - let store_ptr = store.as_ptr().cast::(); - if rare.stdout_store.map(|p| p.as_ptr()) == Some(store_ptr) { - break 'brk true; - } - if rare.stderr_store.map(|p| p.as_ptr()) == Some(store_ptr) { - break 'brk true; - } - } - - if let Some(tag) = fd.stdio_tag() { - matches!(tag, bun_sys::Stdio::StdOut | bun_sys::Stdio::StdErr) - } else { - false - } - }; - let sink = webcore::FileSink::init( - fd, - jsc::EventLoopHandle::init( - self.global_this() - .expect("Blob.global_this set at construction") - .bun_vm() - .as_mut() - .event_loop() as *mut (), - ), - ); - sink.writer - .with_mut(|w| w.owns_fd = !matches!(pathlike, PathOrFileDescriptor::Fd(_))); + let file_sink: RefPtr = { + let sink = webcore::FileSink::init( + Fd::INVALID, + jsc::EventLoopHandle::init( + self.global_this() + .expect("Blob.global_this set at construction") + .bun_vm() + .as_mut() + .event_loop() + .cast::<()>(), + ), + ); - use bun_io::pipe_writer::BaseWindowsPipeWriter as _; - let started = sink.writer.with_mut(|w| { - if is_stdout_or_stderr { - w.start_sync(fd, false) - } else { - w.start(fd, true) - } - }); - if let bun_sys::Result::Err(err) = started { - return Ok( - JSPromise::rejected_promise(cx.global(), err.to_js(cx.global())).to_js(), - ); - } + let input_path: webcore::PathOrFileDescriptor = match &store.data.as_file().pathlike { + PathOrFileDescriptor::Fd(fd) => webcore::PathOrFileDescriptor::Fd(*fd), + PathOrFileDescriptor::Path(p) => webcore::PathOrFileDescriptor::Path( + bun_core::Utf8Bytes::Owned(p.slice().to_vec()), + ), + }; - break 'brk_sink sink; - } + let stream_start = streams::Start::FileSink(streams::FileSinkOptions { + truncate: matches!(input_path, webcore::PathOrFileDescriptor::Path(_)), + mkdirp: options.mkdirp_if_not_exists.unwrap_or(true), + mode: options.mode.unwrap_or(WRITE_PERMISSIONS), + input_path, + ..Default::default() + }); - #[cfg(not(windows))] - { - let sink = webcore::FileSink::init( - Fd::INVALID, - jsc::EventLoopHandle::init( - self.global_this() - .expect("Blob.global_this set at construction") - .bun_vm() - .as_mut() - .event_loop() - .cast::<()>(), - ), + if let bun_sys::Result::Err(err) = sink.start(&stream_start, cx.context()) { + return Ok( + JSPromise::rejected_promise(cx.global(), err.to_js(cx.global())).to_js(), ); - - let input_path: webcore::PathOrFileDescriptor = match &store.data.as_file().pathlike - { - PathOrFileDescriptor::Fd(fd) => webcore::PathOrFileDescriptor::Fd(*fd), - PathOrFileDescriptor::Path(p) => webcore::PathOrFileDescriptor::Path( - bun_core::Utf8Bytes::Owned(p.slice().to_vec()), - ), - }; - - let stream_start = streams::Start::FileSink(streams::FileSinkOptions { - truncate: matches!(input_path, webcore::PathOrFileDescriptor::Path(_)), - mkdirp: options.mkdirp_if_not_exists.unwrap_or(true), - mode: options.mode.unwrap_or(WRITE_PERMISSIONS), - input_path, - ..Default::default() - }); - - if let bun_sys::Result::Err(err) = sink.start(&stream_start, cx.context()) { - return Ok( - JSPromise::rejected_promise(cx.global(), err.to_js(cx.global())).to_js(), - ); - } - break 'brk_sink sink; } + sink }; // `pipe_stream` takes its own refs; init's +1 drops with `file_sink` on return. @@ -1653,129 +1517,46 @@ impl BlobExt for Blob { ); } - #[cfg(windows)] - { - use bun_io::pipe_writer::BaseWindowsPipeWriter as _; - - let pathlike = &store.data.as_file().pathlike; - // SAFETY: bun_vm() never returns null for a Bun-owned global. - let vm = global_this.bun_vm().as_mut(); - let fd: Fd = match pathlike { - PathOrFileDescriptor::Fd(fd) => *fd, - PathOrFileDescriptor::Path(p) => { - let mut file_path = bun_paths::path_buffer_pool::get(); - match bun_sys::open( - p.slice_z(&mut file_path), - bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::NONBLOCK, - WRITE_PERMISSIONS, - ) { - bun_sys::Result::Ok(result) => result, - bun_sys::Result::Err(err) => { - return Err(global_this - .throw_value(err.with_path(p.slice()).to_js(global_this))); - } - } - } - }; - - let is_stdout_or_stderr = 'brk: { - if !matches!(pathlike, PathOrFileDescriptor::Fd(_)) { - break 'brk false; - } - if let Some(rare) = vm.rare_data.as_ref() { - let store_ptr = store.as_ptr().cast::(); - if rare.stdout_store.map(|p| p.as_ptr()) == Some(store_ptr) { - break 'brk true; - } - if rare.stderr_store.map(|p| p.as_ptr()) == Some(store_ptr) { - break 'brk true; - } - } - matches!( - fd.stdio_tag(), - Some(bun_core::Stdio::StdOut) | Some(bun_core::Stdio::StdErr) - ) - }; - - let sink = webcore::FileSink::init( - fd, - jsc::EventLoopHandle::init( - self.global_this() - .expect("Blob.global_this set at construction") - .bun_vm() - .as_mut() - .event_loop() as *mut (), - ), - ); - // `to_js` takes its own per-wrapper +1; init's ref drops at scope end. - sink.writer - .with_mut(|w| w.owns_fd = !matches!(pathlike, PathOrFileDescriptor::Fd(_))); - - let start_result = sink.writer.with_mut(|w| { - if is_stdout_or_stderr { - w.start_sync(fd, false) - } else { - w.start(fd, true) - } - }); - if let bun_sys::Result::Err(err) = start_result { - return Err(global_this.throw_value(err.to_js(global_this))); - } - if matches!(pathlike, PathOrFileDescriptor::Path(_)) { - sink.close_with_graph(context); + let sink = webcore::FileSink::init( + bun_sys::Fd::INVALID, + jsc::EventLoopHandle::init( + self.global_this() + .expect("Blob.global_this set at construction") + .bun_vm() + .as_mut() + .event_loop() + .cast::<()>(), + ), + ); + // `to_js` takes its own per-wrapper +1; init's ref drops at scope end. + let input_path: webcore::PathOrFileDescriptor = match &store.data.as_file().pathlike { + PathOrFileDescriptor::Fd(fd) => webcore::PathOrFileDescriptor::Fd(*fd), + PathOrFileDescriptor::Path(p) => { + webcore::PathOrFileDescriptor::Path(bun_core::Utf8Bytes::Owned(p.slice().to_vec())) } + }; - // SAFETY: `&mut` scoped to the call. - let js = unsafe { (*sink.as_ptr()).to_js(global_this) }; - return Ok(js); + // `webcore::PathOrFileDescriptor` is not `Clone`; build user + // options first, then move `input_path` in once. + let mut stream_start = if has_args && arg0.is_object() { + streams::Start::from_js_with_tag::<{ streams::StartTag::FileSink }>(global_this, arg0)? + } else { + streams::Start::FileSink(streams::FileSinkOptions { + input_path: webcore::PathOrFileDescriptor::Fd(Fd::INVALID), + ..Default::default() + }) + }; + if let streams::Start::FileSink(ref mut opts) = stream_start { + opts.input_path = input_path; } - #[cfg(not(windows))] - { - let sink = webcore::FileSink::init( - bun_sys::Fd::INVALID, - jsc::EventLoopHandle::init( - self.global_this() - .expect("Blob.global_this set at construction") - .bun_vm() - .as_mut() - .event_loop() - .cast::<()>(), - ), - ); - // `to_js` takes its own per-wrapper +1; init's ref drops at scope end. - let input_path: webcore::PathOrFileDescriptor = match &store.data.as_file().pathlike { - PathOrFileDescriptor::Fd(fd) => webcore::PathOrFileDescriptor::Fd(*fd), - PathOrFileDescriptor::Path(p) => webcore::PathOrFileDescriptor::Path( - bun_core::Utf8Bytes::Owned(p.slice().to_vec()), - ), - }; - - // `webcore::PathOrFileDescriptor` is not `Clone`; build user - // options first, then move `input_path` in once. - let mut stream_start = if has_args && arg0.is_object() { - streams::Start::from_js_with_tag::<{ streams::StartTag::FileSink }>( - global_this, - arg0, - )? - } else { - streams::Start::FileSink(streams::FileSinkOptions { - input_path: webcore::PathOrFileDescriptor::Fd(Fd::INVALID), - ..Default::default() - }) - }; - if let streams::Start::FileSink(ref mut opts) = stream_start { - opts.input_path = input_path; - } - - if let bun_sys::Result::Err(err) = sink.start(&stream_start, context) { - return Err(global_this.throw_value(err.to_js(global_this))); - } - - // SAFETY: `&mut` scoped to the call. - let js = unsafe { (*sink.as_ptr()).to_js(global_this) }; - Ok(js) + if let bun_sys::Result::Err(err) = sink.start(&stream_start, context) { + return Err(global_this.throw_value(err.to_js(global_this))); } + + // SAFETY: `&mut` scoped to the call. + let js = unsafe { (*sink.as_ptr()).to_js(global_this) }; + Ok(js) } fn get_slice_from( &self, @@ -2031,35 +1812,19 @@ impl BlobExt for Blob { .as_file(); match &file.pathlike { PathOrFileDescriptor::Path(path_like) => { - // SAFETY: bun_vm() returns the live VM for this global. - let vm = global_this.bun_vm().as_mut(); - // SAFETY: lazily-initialised per-VM NodeFS binding; never null after init. - let binding = unsafe { - &*vm.node_fs().cast::() - }; Ok(crate::node::fs::async_::Stat::create( &global_this.js_thread_of_caller(callback), - binding, - crate::node::fs::args::Stat::owned(path_like.slice().to_vec()), - vm, - None, - )) - } - PathOrFileDescriptor::Fd(fd) => { - // SAFETY: bun_vm() returns the live VM for this global. - let vm = global_this.bun_vm().as_mut(); - // SAFETY: lazily-initialised per-VM NodeFS binding; never null after init. - let binding = unsafe { - &*vm.node_fs().cast::() - }; - Ok(crate::node::fs::async_::Fstat::create( - &global_this.js_thread_of_caller(callback), - binding, - crate::node::fs::args::Fstat::for_fd(*fd), - vm, + crate::node::fs::args::Stat::of_bun_file(path_like.slice().to_vec()), + global_this.bun_vm().as_mut(), None, )) } + PathOrFileDescriptor::Fd(fd) => Ok(crate::node::fs::async_::Fstat::create( + &global_this.js_thread_of_caller(callback), + crate::node::fs::args::Fstat::for_fd(*fd), + global_this.bun_vm().as_mut(), + None, + )), } } store::DataTag::S3 => crate::webcore::s3_file::get_stat(self, global_this, callback), @@ -3492,7 +3257,7 @@ impl NewInternalReadFileHandler where F: InternalReadFileFn, { - /// The erased `(ctx, run, cancel)` a `ReadFile`/`ReadFileUV` carries for this handler. + /// The erased `(ctx, run, cancel)` a `ReadFile` carries for this handler. pub(crate) fn completion(ctx: *mut C) -> read_file::ReadFileCompletionFns { fn run>( ctx: *mut c_void, @@ -4023,35 +3788,36 @@ pub(crate) fn write_format_for_size bun_sys::Result<()> { +pub(crate) fn mkdirp_parent(path: &[u8], as_written: bool) -> bun_sys::Result<()> { let Some(dirname) = bun_core::dirname(path) else { - return Err(bun_sys::Error::from_code( - bun_sys::E::ENOENT, - bun_sys::Tag::mkdir, - )); + return Err( + bun_sys::Error::from_code(bun_sys::E::ENOENT, bun_sys::Tag::mkdir).with_path(path), + ); }; - node::fs::NodeFS::default() - .mkdir_recursive(&node::fs::args::Mkdir { - path: PathLike::borrowed(dirname), - recursive: true, - always_return_none: true, - ..Default::default() - }) - .map(|_| ()) + match node::fs::NodeFS::default().mkdir_recursive(&node::fs::args::Mkdir { + path: PathLike::borrowed(dirname), + recursive: true, + always_return_none: true, + as_written, + ..Default::default() + }) { + Ok(_) => Ok(()), + Err(err) => Err(err.with_path(path)), + } } // TODO: move this to bun_sys? #[inline(never)] -#[cfg(not(windows))] pub(crate) fn mkdir_if_not_exists( this: &mut T, err: &bun_sys::Error, @@ -4059,14 +3825,14 @@ pub(crate) fn mkdir_if_not_exists( err_path: &[u8], ) -> Retry { if err.get_errno() == bun_sys::E::ENOENT && this.mkdirp_if_not_exists() { - match mkdirp_parent(path_string.as_bytes()) { + match mkdirp_parent(path_string.as_bytes(), true) { bun_sys::Result::Ok(()) => { this.set_mkdirp_if_not_exists(false); return Retry::Continue; } bun_sys::Result::Err(err2) => { this.set_errno_if_present(bun_errno::from_errno(err2.errno as i32).into()); - this.set_system_error(err.with_path(err_path).to_system_error()); + this.set_system_error(err2.with_path(err_path).to_system_error()); this.set_opened_fd_if_present(Fd::INVALID); return Retry::Fail; } @@ -4092,7 +3858,6 @@ fn sys_error_with_path_like( /// Receiver trait for `mkdir_if_not_exists`; impls optionally /// write `errno` / `opened_fd` via the defaulted setters. -#[cfg(not(windows))] pub(crate) trait MkdirpTarget { fn mkdirp_if_not_exists(&self) -> bool; fn set_mkdirp_if_not_exists(&mut self, v: bool); @@ -4162,75 +3927,47 @@ fn write_file_with_empty_source_to_destination( ); if let bun_sys::Result::Err(ref mut err) = result { - let errno = err.get_errno(); - let mut was_eperm = false; 'err: { - let mut current = errno; - loop { - match current { - // truncate might return EPERM when the parent directory doesn't exist - // #6336 - bun_sys::E::EPERM => { - was_eperm = true; - err.errno = bun_sys::E::ENOENT as _; - current = bun_sys::E::ENOENT; - continue; - } - bun_sys::E::ENOENT => { - if options.mkdirp_if_not_exists == Some(false) { - break 'err; - } - let dirpath: &[u8] = match &file.pathlike { - PathOrFileDescriptor::Path(path) => { - match bun_core::dirname(path.slice()) { - Some(d) => d, - None => break 'err, - } - } - PathOrFileDescriptor::Fd(_) => { - // NOTE: if this is an fd, it means the file - // exists, so we shouldn't try to mkdir it - if was_eperm { - err.errno = bun_sys::E::EPERM as _; - } - break 'err; - } - }; - let mkdir_result = - node_fs.mkdir_recursive(&node::fs::args::Mkdir { - path: PathLike::borrowed(dirpath), - recursive: true, - always_return_none: true, - ..Default::default() - }); - if let bun_sys::Result::Err(e) = mkdir_result { - *err = e; - break 'err; - } + if err.get_errno() != bun_sys::E::ENOENT + || options.mkdirp_if_not_exists == Some(false) + { + break 'err; + } + // An fd is a file that exists: there is nothing to mkdir. + let PathOrFileDescriptor::Path(path) = &file.pathlike else { + break 'err; + }; + let Some(dirpath) = bun_core::dirname(path.slice()) else { + break 'err; + }; + let mkdir_result = node_fs.mkdir_recursive(&node::fs::args::Mkdir { + path: PathLike::borrowed(dirpath), + recursive: true, + always_return_none: true, + ..Default::default() + }); + if let bun_sys::Result::Err(e) = mkdir_result { + *err = e; + break 'err; + } - // SAFETY: we check if `file.pathlike` is an fd above, returning if it is. - let mut buf = bun_paths::path_buffer_pool::get(); - let mode: bun_sys::Mode = - options.mode.unwrap_or(node::fs::DEFAULT_PERMISSION); - match bun_sys::File::open( - file.pathlike.path().slice_z(&mut buf), - bun_sys::O::CREAT | bun_sys::O::TRUNC, - mode, - ) { - bun_sys::Result::Err(e) => { - *err = e; - break 'err; - } - bun_sys::Result::Ok(f) => { - let _ = f.close(); // close error is non-actionable - return Ok(JSPromise::resolved_promise_value( - cx.global(), - JSValue::js_number(0.0), - )); - } - } - } - _ => break 'err, + let mut buf = bun_paths::path_buffer_pool::get(); + let mode: bun_sys::Mode = options.mode.unwrap_or(node::fs::DEFAULT_PERMISSION); + match bun_sys::File::open( + path.slice_z(&mut buf), + bun_sys::O::CREAT | bun_sys::O::TRUNC, + mode, + ) { + bun_sys::Result::Err(e) => { + *err = e; + break 'err; + } + bun_sys::Result::Ok(f) => { + let _ = f.close(); // close error is non-actionable + return Ok(JSPromise::resolved_promise_value( + cx.global(), + JSValue::js_number(0.0), + )); } } } @@ -4322,6 +4059,7 @@ pub(crate) fn write_file_with_source_destination( source_blob: &mut Blob, destination_blob: &mut Blob, options: &WriteFileOptions, + opened_destination: Option, ) -> JsResult { let destination_store = destination_blob .store @@ -4349,77 +4087,35 @@ pub(crate) fn write_file_with_source_destination( // The borrowed views below are +0 on the store ref; // `WriteFile::create` takes its own ref. - #[cfg(windows)] - { - let promise = JSPromise::create(cx.global()); - let promise_value = promise.as_value(cx.global()); - promise_value.ensure_still_alive(); - // SAFETY: write_file_promise was just produced by heap::alloc above; sole owner. - unsafe { - (*write_file_promise) - .promise - .set(cx.global(), promise_value) - }; - match write_file_mod::WriteFileWindows::create( - cx.vm().event_loop(), - cx.context(), - destination_blob.borrowed_view(), - source_blob.borrowed_view(), - write_file_promise, - WriteFilePromise::run, - options.mkdirp_if_not_exists.unwrap_or(true), - ) { - Err(write_file_mod::WriteFileWindowsError::WriteFileWindowsDeinitialized) => {} - Err(write_file_mod::WriteFileWindowsError::Js(err)) => return Err(err), - Ok(_) => {} - } - return Ok(promise_value); - } - - #[cfg(not(windows))] - { - let file_copier = write_file_mod::WriteFile::create( - destination_blob.borrowed_view(), - source_blob.borrowed_view(), - options.mkdirp_if_not_exists.unwrap_or(true), - ) - .expect("unreachable"); - // SAFETY: `write_file_promise` was just produced by heap::alloc above; sole owner. - let mut write_file_promise = unsafe { bun_core::heap::take(write_file_promise) }; - // Defer promise creation until we're just about to schedule the task. - write_file_promise.promise = jsc::JSPromiseStrong::init(cx.global()); - let promise_value = write_file_promise.promise.value(); - promise_value.ensure_still_alive(); - write_file_mod::WriteFile::schedule(file_copier, write_file_promise, cx); - return Ok(promise_value); - } + let mut file_copier = write_file_mod::WriteFile::create( + destination_blob.borrowed_view(), + source_blob.borrowed_view(), + options.mkdirp_if_not_exists.unwrap_or(true), + ) + .expect("unreachable"); + if let Some(opened) = opened_destination { + file_copier.opened_fd = opened.into_fd(); + } + // SAFETY: `write_file_promise` was just produced by heap::alloc above; sole owner. + let mut write_file_promise = unsafe { bun_core::heap::take(write_file_promise) }; + // Defer promise creation until we're just about to schedule the task. + write_file_promise.promise = jsc::JSPromiseStrong::init(cx.global()); + let promise_value = write_file_promise.promise.value(); + promise_value.ensure_still_alive(); + write_file_mod::WriteFile::schedule(file_copier, write_file_promise, cx); + return Ok(promise_value); } // If this is file <> file, we can just copy the file else if destination_type == store::DataTag::File && source_type == store::DataTag::File { - #[cfg(windows)] - { - return Ok(copy_file::CopyFileWindows::init( - destination_store, - source_store, - cx.vm().event_loop_shared(), - cx.context(), - options.mkdirp_if_not_exists.unwrap_or(true), - destination_blob.size.get(), - options.mode, - )); - } - #[cfg(not(windows))] - { - return Ok(copy_file::CopyFile::create( - destination_store, - source_store, - destination_blob.offset.get(), - destination_blob.size.get(), - cx, - options.mkdirp_if_not_exists.unwrap_or(true), - options.mode, - )); - } + return Ok(copy_file::CopyFile::create( + destination_store, + source_store, + destination_blob.offset.get(), + destination_blob.size.get(), + cx, + options.mkdirp_if_not_exists.unwrap_or(true), + options.mode, + )); } else if destination_type == store::DataTag::File && source_type == store::DataTag::S3 { let s3 = source_store.data.as_s3(); if let Some(stream) = ReadableStream::from_js( @@ -4670,10 +4366,10 @@ pub(crate) fn write_file_internal( } // If you're doing Bun.write(), try to go fast by writing short input on the main thread. - // This is a heuristic, but it's a good one. - // - // except if you're on Windows. Windows I/O is slower. Let's not even try. - #[cfg(not(windows))] + // This is a heuristic, but it's a good one. It is also what keeps writes that are not awaited + // in the order they were made: the ones that go to the work pool have no order among them. + // A destination the fast path opened and then left to the work pool. + let mut opened_destination: Option = None; { let mut needs_async = false; let fast_path_ok = matches!(*path_or_blob, PathOrBlob::Path(_)) @@ -4683,69 +4379,43 @@ pub(crate) fn write_file_internal( && matches!(b.store().expect("infallible: store present").data, store::Data::File(ref f) if f.mode != 0 && bun_core::kind_from_mode(f.mode) == bun_core::FileKind::File)))); if fast_path_ok { - if data.is_string() { - let len = data.get_length(cx.global())?; - if len < 256 * 1024 { - let str = data.to_bun_string(cx.global())?; - let pathlike: &PathOrFileDescriptor = match &*path_or_blob { - PathOrBlob::Path(p) => p, - PathOrBlob::Blob(b) => { - &b.store() - .expect("infallible: store present") - .data - .as_file() - .pathlike - } - }; - let result = if matches!(pathlike, PathOrFileDescriptor::Path(_)) { - write_string_to_file_fast::( - cx.global(), - pathlike, - &str, - &mut needs_async, - ) - } else { - write_string_to_file_fast::( - cx.global(), - pathlike, - &str, - &mut needs_async, - ) - }; - if !needs_async { - return Ok(result); - } + let str; + let utf8; + let buffer_view; + let small: Option<&[u8]> = if data.is_string() { + if data.get_length(cx.global())? < 256 * 1024 { + str = data.to_bun_string(cx.global())?; + utf8 = str.to_utf8(); + Some(utf8.slice()) + } else { + None } - } else if let Some(buffer_view) = data.as_array_buffer(cx.global()) { - if buffer_view.byte_len < 256 * 1024 { - let pathlike: &PathOrFileDescriptor = match &*path_or_blob { - PathOrBlob::Path(p) => p, - PathOrBlob::Blob(b) => { - &b.store() - .expect("infallible: store present") - .data - .as_file() - .pathlike - } - }; - let result = if matches!(pathlike, PathOrFileDescriptor::Path(_)) { - write_bytes_to_file_fast::( - cx.global(), - pathlike, - buffer_view.byte_slice(), - &mut needs_async, - ) - } else { - write_bytes_to_file_fast::( - cx.global(), - pathlike, - buffer_view.byte_slice(), - &mut needs_async, - ) - }; - if !needs_async { - return Ok(result); + } else if let Some(view) = data.as_array_buffer(cx.global()) { + buffer_view = view; + (buffer_view.byte_len < 256 * 1024).then(|| buffer_view.byte_slice()) + } else { + None + }; + if let Some(bytes) = small { + let pathlike: &PathOrFileDescriptor = match &*path_or_blob { + PathOrBlob::Path(p) => p, + PathOrBlob::Blob(b) => { + &b.store() + .expect("infallible: store present") + .data + .as_file() + .pathlike } + }; + let result = write_bytes_to_file_fast( + cx.global(), + pathlike, + bytes, + &mut needs_async, + &mut opened_destination, + ); + if !needs_async { + return Ok(result); } } } @@ -5011,7 +4681,13 @@ pub(crate) fn write_file_internal( // RefPtr clone+drop keeps the destination store alive across the call. let _dest_hold = destination_store; - write_file_with_source_destination(cx, &mut *source_blob, &mut destination_blob, &options) + write_file_with_source_destination( + cx, + &mut *source_blob, + &mut destination_blob, + &options, + opened_destination, + ) } fn validate_writable_blob(global_this: &JSGlobalObject, blob: &Blob) -> JsResult<()> { @@ -5135,109 +4811,59 @@ pub(crate) fn write_file(global_this: &JSGlobalObject, callframe: &CallFrame) -> const WRITE_PERMISSIONS: bun_sys::Mode = 0o664; +/// How the fast path opens a path. POSIX does not truncate on open: the file is +/// cut to what was written afterwards, which is cheaper there. A Windows open +/// that does not truncate also opens a directory, and the write is what fails; +/// truncating on open refuses it, like the write that goes to the work pool. #[cfg(not(windows))] -fn write_string_to_file_fast( - global_this: &JSGlobalObject, - pathlike: &PathOrFileDescriptor, - str: &BunString, - needs_async: &mut bool, -) -> JSValue { - let fd: Fd = if !NEEDS_OPEN { - pathlike.fd() - } else { - let mut file_path = bun_paths::path_buffer_pool::get(); - match bun_sys::open( - pathlike.path().slice_z(&mut file_path), - // we deliberately don't use O_TRUNC here - // it's a perf optimization - bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::NONBLOCK, - WRITE_PERMISSIONS, - ) { - bun_sys::Result::Ok(result) => result, - bun_sys::Result::Err(err) => { - if err.get_errno() == bun_sys::E::ENOENT { - *needs_async = true; - return JSValue::ZERO; - } - return JSPromise::rejected_promise( - global_this, - err.with_path(pathlike.path().slice()).to_js(global_this), - ) - .to_js(); - } - } - }; - - // Declared before the truncate guard so it drops *after* it (close runs last). - let _close = NEEDS_OPEN.then(|| bun_sys::CloseOnDrop::new(fd)); - - // scopeguard's closure captures borrows at construction, conflicting - // with later `written += ...` / `truncate = false`. Route through `Cell` - // so the guard and the loop body share `&Cell<_>` (no mutable-borrow conflict). - let truncate = core::cell::Cell::new(NEEDS_OPEN || str.is_empty()); - let written = core::cell::Cell::new(0usize); - - // we only truncate if it's a path - // if it's a file descriptor, we assume they want manual control over that behavior - scopeguard::defer! { - if truncate.get() { - let _ = bun_sys::ftruncate(fd, i64::try_from(written.get()).expect("int cast")); - } - } - - if !str.is_empty() { - let decoded = str.to_utf8(); - let mut remain = decoded.slice(); - while !remain.is_empty() { - match bun_sys::write(fd, remain) { - bun_sys::Result::Ok(res) => { - written.set(written.get() + res); - remain = &remain[res..]; - if res == 0 { - break; - } - } - bun_sys::Result::Err(err) => { - truncate.set(false); - if err.get_errno() == bun_sys::E::EAGAIN { - *needs_async = true; - return JSValue::ZERO; - } - let err_js = if !NEEDS_OPEN { - err.to_js(global_this) - } else { - err.with_path(pathlike.path().slice()).to_js(global_this) - }; - return JSPromise::rejected_promise(global_this, err_js).to_js(); - } - } - } - } - - JSPromise::resolved_promise_value(global_this, JSValue::js_number(written.get() as f64)) +const FAST_WRITE_OPEN_FLAGS: i32 = bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::NONBLOCK; +#[cfg(windows)] +const FAST_WRITE_OPEN_FLAGS: i32 = bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::TRUNC; +/// Whether a path the fast path opened is cut to what was written afterwards. +const FAST_WRITE_TRUNCATES_AFTER: bool = cfg!(not(windows)); + +/// Whether a `write` to `fd` on the calling thread is over once the call +/// returns and cannot wait on this process: a disk file, or the process's own +/// stdout or stderr, which `console.log` writes the same way. Any other pipe +/// may be read by this very thread, and a handle opened for overlapped I/O +/// returns before the transfer is done. +#[cfg(windows)] +fn windows_write_returns_promptly(fd: Fd) -> bool { + use bun_sys::windows::fs; + let own_stdio = fd.native() == Fd::stdout().native() || fd.native() == Fd::stderr().native(); + (own_stdio || fs::is_disk_file(fd)) && fs::is_synchronous(fd) == Some(true) } -#[cfg(not(windows))] -fn write_bytes_to_file_fast( +fn write_bytes_to_file_fast( global_this: &JSGlobalObject, pathlike: &PathOrFileDescriptor, bytes: &[u8], needs_async: &mut bool, + opened_destination: &mut Option, ) -> JSValue { - let fd: Fd = if !NEEDS_OPEN { + #[cfg(not(windows))] + let _ = opened_destination; + let needs_open = matches!(pathlike, PathOrFileDescriptor::Path(_)); + let fd: Fd = if !needs_open { + if bun_jsc::FdUse::Uses(pathlike.fd()).is_behind_jobs(global_this.bun_vm()) { + *needs_async = true; + return JSValue::ZERO; + } + #[cfg(windows)] + if !windows_write_returns_promptly(pathlike.fd()) { + *needs_async = true; + return JSValue::ZERO; + } pathlike.fd() } else { let mut file_path = bun_paths::path_buffer_pool::get(); - let flags = if cfg!(not(windows)) { - bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::NONBLOCK + // Writing nothing is a truncation, which names its file as a read does. + let path = if bytes.is_empty() { + pathlike.path().slice_z(&mut file_path) } else { - bun_sys::O::WRONLY | bun_sys::O::CREAT + pathlike.path().slice_z_as_written(&mut file_path) }; - match bun_sys::open( - pathlike.path().slice_z(&mut file_path), - flags, - WRITE_PERMISSIONS, - ) { + match bun_sys::open(path, FAST_WRITE_OPEN_FLAGS, WRITE_PERMISSIONS) { bun_sys::Result::Ok(result) => result, bun_sys::Result::Err(err) => { if err.get_errno() == bun_sys::E::ENOENT { @@ -5253,11 +4879,19 @@ fn write_bytes_to_file_fast( } }; - // TODO: on windows this is always synchronous - - let truncate = NEEDS_OPEN || bytes.is_empty(); + let truncate = if needs_open { + FAST_WRITE_TRUNCATES_AFTER + } else { + bytes.is_empty() + }; let mut written: usize = 0; - let _close = NEEDS_OPEN.then(|| bun_sys::CloseOnDrop::new(fd)); + let _close = needs_open.then(|| bun_sys::CloseOnDrop::new(fd)); + #[cfg(windows)] + if needs_open && !bytes.is_empty() && !bun_sys::windows::fs::is_disk_file(fd) { + *opened_destination = _close; + *needs_async = true; + return JSValue::ZERO; + } let mut remain = bytes; while !remain.is_empty() { @@ -5274,7 +4908,7 @@ fn write_bytes_to_file_fast( *needs_async = true; return JSValue::ZERO; } - let err_js = if !NEEDS_OPEN { + let err_js = if !needs_open { err.to_js(global_this) } else { err.with_path(pathlike.path().slice()).to_js(global_this) @@ -5750,19 +5384,11 @@ pub(crate) unsafe extern "C" fn Blob__fromMmapWithType( } } -/// `stat.{st_mtime, st_mtime_nsec}` → JS epoch ms. `bun_sys::Stat` is -/// `libc::stat` on POSIX (fields) and `uv_stat_t` on Windows (`mtim` timespec); -/// cfg-split here so the call sites stay shared. +/// The modification time of `stat` in JS epoch ms. #[inline] fn stat_to_js_mtime(stat: &bun_sys::Stat) -> jsc::JSTimeType { - #[cfg(not(windows))] - { - jsc::to_js_time(stat.st_mtime as isize, stat.st_mtime_nsec as isize) - } - #[cfg(windows)] - { - jsc::to_js_time(stat.mtim.sec as isize, stat.mtim.nsec as isize) - } + let mtime = bun_sys::stat_mtime(stat); + jsc::to_js_time(mtime.sec as isize, mtime.nsec as isize) } /// Window clamp shared by the `resolve_size`/`resolved_size` arms: only an @@ -6437,6 +6063,8 @@ pub(crate) trait FileOpener: Sized { /// Override if you need different open flags; defaults to RDONLY. const OPEN_FLAGS: i32 = bun_sys::O::RDONLY; const OPENER_FLAGS: i32 = bun_sys::O::NONBLOCK | bun_sys::O::CLOEXEC; + /// See [`PathLikeExt::slice_z_as_written`]. + const AS_WRITTEN: bool = false; fn opened_fd(&self) -> Fd; fn set_opened_fd(&mut self, fd: Fd); @@ -6448,7 +6076,6 @@ pub(crate) trait FileOpener: Sized { /// `CopyFile`) override this to call [`mkdir_if_not_exists`]; everyone else /// (e.g. `ReadFile`) keeps the default `Retry::No`, so the open path falls /// straight through to the error branch. - #[cfg(not(windows))] fn try_mkdirp( &mut self, _err: bun_sys::Error, @@ -6457,17 +6084,6 @@ pub(crate) trait FileOpener: Sized { ) -> Retry { Retry::No } - #[cfg(windows)] - fn loop_(&self) -> *mut bun_libuv_sys::uv_loop_t; - #[cfg(windows)] - fn req(&mut self) -> &mut bun_libuv_sys::uv_fs_t; - /// Stash/retrieve the open completion callback across the libuv async hop. - /// Rust can't const-generic over fn - /// pointers, so the implementor stores it on `self` (e.g. next to `req`). - #[cfg(windows)] - fn set_open_callback(&mut self, cb: fn(&mut Self, Fd)); - #[cfg(windows)] - fn open_callback(&self) -> fn(&mut Self, Fd); fn get_fd_by_opening(&mut self, callback: fn(&mut Self, Fd)) { let mut buf = bun_paths::path_buffer_pool::get(); @@ -6475,126 +6091,46 @@ pub(crate) trait FileOpener: Sized { PathOrFileDescriptor::Path(p) => p.clone(), PathOrFileDescriptor::Fd(_) => unreachable!(), }; - let path = path_string.slice_z(&mut buf); + let path = if Self::AS_WRITTEN { + path_string.slice_z_as_written(&mut buf) + } else { + path_string.slice_z(&mut buf) + }; - #[cfg(windows)] - { - use bun_sys::ReturnCodeExt as _; - // Monomorphic libuv completion thunk — recovers `*mut Self` from - // `req.data`. - extern "C" fn wrapped_callback(req: *mut bun_libuv_sys::uv_fs_t) { - use bun_sys::ReturnCodeExt as _; - // SAFETY: `req.data` was set to `self as *mut Self` below before - // `uv_fs_open` was queued; libuv guarantees `req` is valid here. - let self_: &mut S = unsafe { bun_ptr::callback_ctx::((*req).data) }; - { - // SAFETY: req points into self_.req(); cleanup before reuse. - scopeguard::defer! { unsafe { bun_libuv_sys::uv_fs_req_cleanup(req); } } - // SAFETY: req is the live uv_fs_t from the open request. - let result = unsafe { (*req).result }; - if let Some(err_enum) = result.errno() { - let path_string_2 = match self_.pathlike() { - PathOrFileDescriptor::Path(p) => p.clone(), - PathOrFileDescriptor::Fd(_) => unreachable!(), - }; - self_.set_errno(bun_errno::from_errno(err_enum as i32).into()); - self_.set_system_error( - bun_sys::Error::from_code(err_enum, bun_sys::Tag::open) - .with_path(path_string_2.slice()) - .to_system_error() - .into(), - ); - self_.set_opened_fd(bun_sys::Fd::INVALID); - } else { - self_.set_opened_fd(Fd::from_uv(result.to_fd())); - } + loop { + match bun_sys::open( + path, + Self::OPEN_FLAGS | Self::OPENER_FLAGS, + crate::node::fs::DEFAULT_PERMISSION, + ) { + bun_sys::Result::Ok(fd) => { + self.set_opened_fd(fd); + break; } - let cb = self_.open_callback(); - cb(self_, self_.opened_fd()); - } - - self.set_open_callback(callback); - let loop_ = self.loop_(); - let self_ptr: *mut Self = core::ptr::from_mut(self); - // Derive `req` THROUGH `self_ptr` rather than via a fresh `self.req()` - // reborrow. Under Stacked Borrows, a direct `self.req()` here would - // create a sibling `&mut` that pops `self_ptr`'s tag, making the - // later deref in `wrapped_callback` (via `req.data`) UB. Going - // through the raw pointer keeps the reborrow as a child of - // `self_ptr`, so its provenance survives until the callback fires. - // SAFETY: `self_ptr` was just derived from a live `&mut self`. - let req = unsafe { (*self_ptr).req() }; - // Stash `self` on the request BEFORE dispatch. libuv never touches - // `req.data`, so pre-setting is safe; doing it after `uv_fs_open` - // is a UAF when the call fails synchronously and `callback` frees - // `self` (ReadFileUV::on_finish → finalize → heap::take). - req.data = self_ptr.cast(); - // SAFETY: loop_/req are live for the duration of the async open; - // req.data is consumed by `wrapped_callback::` above. - let rc = unsafe { - bun_libuv_sys::uv_fs_open( - loop_, - req, - path.as_ptr(), - Self::OPEN_FLAGS | Self::OPENER_FLAGS, - node::fs::DEFAULT_PERMISSION as i32, - Some(wrapped_callback::), - ) - }; - if let Some(errno) = rc.errno() { - self.set_errno(bun_errno::from_errno(errno as i32).into()); - self.set_system_error( - bun_sys::Error::from_code(errno, bun_sys::Tag::open) - .with_path(path_string.slice()) - .to_system_error() - .into(), - ); - self.set_opened_fd(bun_sys::Fd::INVALID); - // `callback` may free `self` (see comment above) — must be the - // last thing we touch on this path. - callback(self, bun_sys::Fd::INVALID); - return; - } - return; - } - - #[cfg(not(windows))] - { - loop { - match bun_sys::open( - path, - Self::OPEN_FLAGS | Self::OPENER_FLAGS, - crate::node::fs::DEFAULT_PERMISSION, - ) { - bun_sys::Result::Ok(fd) => { - self.set_opened_fd(fd); - break; - } - bun_sys::Result::Err(err) => { - if err.get_errno() == bun_sys::E::ENOENT { - match self.try_mkdirp(err.clone(), path, path_string.slice()) { - Retry::Continue => continue, - Retry::Fail => { - // `mkdir_if_not_exists` already populated - // `errno`/`system_error` on the impl. - self.set_opened_fd(Fd::INVALID); - break; - } - Retry::No => {} + bun_sys::Result::Err(err) => { + if err.get_errno() == bun_sys::E::ENOENT { + match self.try_mkdirp(err.clone(), path, path_string.slice()) { + Retry::Continue => continue, + Retry::Fail => { + // `mkdir_if_not_exists` already populated + // `errno`/`system_error` on the impl. + self.set_opened_fd(Fd::INVALID); + break; } + Retry::No => {} } - self.set_errno(bun_errno::from_errno(err.errno as i32).into()); - self.set_system_error(jsc::SysErrorJsc::to_system_error( - &err.with_path(path_string.slice()), - )); - self.set_opened_fd(Fd::INVALID); - break; } + self.set_errno(bun_errno::from_errno(err.errno as i32).into()); + self.set_system_error(jsc::SysErrorJsc::to_system_error( + &err.with_path(path_string.slice()), + )); + self.set_opened_fd(Fd::INVALID); + break; } } - - callback(self, self.opened_fd()); } + + callback(self, self.opened_fd()); } fn get_fd(&mut self, callback: fn(&mut Self, Fd)) { @@ -6617,6 +6153,32 @@ pub(crate) trait FileOpener: Sized { #[cfg(not(windows))] pub(crate) use io_parking::IoParking; +/// What a job that reads `file` tells [`Job::schedule_on_fds`](bun_jsc::Job::schedule_on_fds). +pub(crate) fn reads_fd_of(file: &PathOrFileDescriptor<'_>) -> Option { + match file { + PathOrFileDescriptor::Fd(fd) => Some(bun_jsc::FdUse::Uses(*fd)), + PathOrFileDescriptor::Path(_) => None, + } +} + +/// [`reads_fd_of`] for a job that writes to `file`. +pub(crate) fn writes_fd_of(file: &PathOrFileDescriptor<'_>) -> Option { + match file { + PathOrFileDescriptor::Fd(fd) => Some(bun_jsc::FdUse::Appends(*fd)), + PathOrFileDescriptor::Path(_) => None, + } +} + +/// [`JobContext::waits`](bun_jsc::JobContext::waits) for a job that reads or +/// writes `file`. What a path names is not known before it is opened. +#[cfg(windows)] +pub(crate) fn waits_on(file: &PathOrFileDescriptor<'_>) -> bool { + match file { + PathOrFileDescriptor::Fd(fd) => !bun_sys::windows::fs::is_disk_file(*fd), + PathOrFileDescriptor::Path(_) => false, + } +} + // TODO: move to bun_sys? pub(crate) trait FileCloser: Sized { const IO_TAG: bun_io::Tag; @@ -6628,8 +6190,6 @@ pub(crate) trait FileCloser: Sized { fn io_request(&mut self) -> Option<&mut bun_io::Request>; fn io_poll(&mut self) -> &mut bun_io::Poll; fn task(&mut self) -> &mut bun_jsc::WorkPoolTask; - #[cfg(windows)] - fn loop_(&self) -> *mut bun_libuv_sys::uv_loop_t; /// Intrusive backref: Rust `offset_of!` cannot name /// fields on a trait `Self`, so each concrete impl supplies its own @@ -6681,13 +6241,8 @@ pub(crate) trait FileCloser: Sized { && self.opened_fd() != Fd::INVALID && self.opened_fd().stdio_tag().is_none() { - #[cfg(windows)] - bun_io::Closer::close(self.opened_fd(), self.loop_()); - #[cfg(not(windows))] - { - use bun_sys::FdExt as _; - let _ = self.opened_fd().close_allowing_bad_file_descriptor(None); - } + use bun_sys::FdExt as _; + let _ = self.opened_fd().close_allowing_bad_file_descriptor(None); self.set_opened_fd(Fd::INVALID); } @@ -6726,11 +6281,6 @@ macro_rules! impl_file_closer { fn task(&mut self) -> &mut ::bun_jsc::WorkPoolTask { &mut self.task } - #[cfg(windows)] - fn loop_(&self) -> *mut ::bun_libuv_sys::uv_loop_t { - unreachable!() - } - fn schedule_close(request: &mut ::bun_io::Request) -> ::bun_io::Action<'_> { use ::bun_io::IntrusiveIoRequest as _; // SAFETY: `request` is `&mut self.io_request` (intrusive); recover parent. diff --git a/src/runtime/webcore/ByteBlobLoader.rs b/src/runtime/webcore/ByteBlobLoader.rs index 7d42235301bc..df686c146398 100644 --- a/src/runtime/webcore/ByteBlobLoader.rs +++ b/src/runtime/webcore/ByteBlobLoader.rs @@ -7,7 +7,7 @@ use crate::webcore::blob::{self, Blob, BlobExt as _, Store}; use crate::webcore::readable_stream; use crate::webcore::streams; -pub struct ByteBlobLoader { +pub(crate) struct ByteBlobLoader { pub offset: blob::SizeType, // LIFETIMES.tsv: SHARED — ref() on setup, deref() in clearData pub(crate) store: Option>, @@ -38,7 +38,6 @@ impl Default for ByteBlobLoader { pub(crate) type Source = readable_stream::NewSource; impl readable_stream::SourceContext for ByteBlobLoader { - const NAME: &'static str = "Blob"; // setRefUnrefFn = null const SUPPORTS_REF: bool = false; crate::source_context_codegen!(js_BlobInternalReadableStreamSource); diff --git a/src/runtime/webcore/ByteStream.rs b/src/runtime/webcore/ByteStream.rs index 019038c5c01f..c0f84dd46bda 100644 --- a/src/runtime/webcore/ByteStream.rs +++ b/src/runtime/webcore/ByteStream.rs @@ -202,7 +202,6 @@ impl Drop for PinnedBytes { } impl readable_stream::SourceContext for ByteStream { - const NAME: &'static str = "Bytes"; // setRefUnrefFn = null const SUPPORTS_REF: bool = false; crate::source_context_codegen!(js_BytesInternalReadableStreamSource); diff --git a/src/runtime/webcore/FileReader.rs b/src/runtime/webcore/FileReader.rs index c22703418fec..bab0b914c66c 100644 --- a/src/runtime/webcore/FileReader.rs +++ b/src/runtime/webcore/FileReader.rs @@ -6,10 +6,12 @@ use bun_collections::VecExt; use bun_io as aio; #[cfg(not(windows))] use bun_io::FileType; -use bun_io::{BufferedReader, Chunk, ReadState}; +use bun_io::{BufferedReader, Chunk, ReadState, ReaderFlags}; use bun_jsc::JsCell; use bun_ptr::{AsCtxPtr, RefPtr}; -use bun_sys::{self as sys, Fd, FdExt}; +#[cfg(unix)] +use bun_sys::FdExt; +use bun_sys::{self as sys, Fd}; use crate::webcore::SinkHandle; use crate::webcore::blob; @@ -105,6 +107,8 @@ pub(crate) struct OpenedFileBlob { pub(crate) fd: Fd, pub(crate) pollable: bool, pub(crate) nonblocking: bool, + /// `fd` is a duplicate of the caller's. + pub(crate) duplicate: bool, #[cfg(not(windows))] pub(crate) file_type: FileType, } @@ -115,6 +119,7 @@ impl Default for OpenedFileBlob { fd: Fd::INVALID, pollable: false, nonblocking: true, + duplicate: false, #[cfg(not(windows))] file_type: FileType::File, } @@ -158,6 +163,7 @@ impl Lazy { Ok(fd) => fd, Err(err) => return Err(err.with_fd(*pl_fd)), }; + this.duplicate = true; #[cfg(unix)] { @@ -169,7 +175,7 @@ impl Lazy { } } - fd.make_lib_uv_owned_for_syscall(sys::Tag::dup, sys::ErrorCase::CloseOnFail)? + fd } } PathOrFileDescriptor::Path(path) => { @@ -212,7 +218,7 @@ impl Lazy { let mode = stat.st_mode as _; if sys::S::ISDIR(mode) { - aio::Closer::close(fd, ()); + aio::Closer::close(fd); return Err(sys::Error::from_code(sys::Errno::EISDIR, sys::Tag::fstat)); } @@ -268,12 +274,7 @@ bun_io::impl_buffered_reader_parent! { on_reader_done = |this| (&*this).on_reader_done(); on_reader_error = |this, err| (&*this).on_reader_error(err); loop_ = |this| { - let ev = (&*this).event_loop.get(); - // The event loop is a libuv - // `uv_loop_t*` on Windows. `.cast()` reconciles the impl-declared - // `bun_uws_sys::Loop` nominal with `bun_io::Loop` (= `uv::Loop`). - #[cfg(windows)] { ev.uv_loop().cast() } - #[cfg(not(windows))] { ev.r#loop() } + (&*this).event_loop.get().r#loop() }; event_loop = |this| (&*this).event_loop.get().as_event_loop_ctx(); // A read delivers to `on_read_chunk` consumers (JS, or a native sink such @@ -338,22 +339,13 @@ impl FileReader { { file_type = opened.file_type; } - #[cfg(unix)] - { - use bun_io::pipe_reader::PosixFlags; - self.reader() - .flags - .set(PosixFlags::NONBLOCKING, opened.nonblocking); - self.reader().flags.set(PosixFlags::POLLABLE, pollable); - } - #[cfg(windows)] - { - use bun_io::pipe_reader::WindowsFlags; - self.reader() - .flags - .set(WindowsFlags::NONBLOCKING, opened.nonblocking); - self.reader().flags.set(WindowsFlags::POLLABLE, pollable); - } + self.reader() + .flags + .set(ReaderFlags::NONBLOCKING, opened.nonblocking); + self.reader().flags.set(ReaderFlags::POLLABLE, pollable); + self.reader() + .flags + .set(ReaderFlags::SHARES_POSITION, opened.duplicate); } } } @@ -385,8 +377,8 @@ impl FileReader { // POSIX non-pollable regular file every read is synchronous // (`read_file` → `sys::pread`), so there is no such callback — // holding the Strong there would root an abandoned reader forever - // and leak its fd. Windows file reads are async via libuv even for - // regular files, so the ref is always taken there. + // and leak its fd. On Windows every read, a regular file's included, + // completes through the loop, so the ref is always taken there. #[cfg(unix)] let need_io_ref = pollable; #[cfg(windows)] @@ -412,49 +404,27 @@ impl FileReader { } return streams::Start::Err(e); } - } else { - #[cfg(unix)] - { - use bun_io::pipe_reader::PosixFlags; - if !self.started.get() - && !self.waiting_for_on_reader_done.get() - && self.reader().flags.contains(PosixFlags::POLLABLE) - && !self.reader().is_done() - { - self.waiting_for_on_reader_done.set(true); - // SAFETY: see `parent()`. - unsafe { (*self.parent()).increment_count() }; - } - } - #[cfg(windows)] - { - // Non-lazy fromPipe path (Bun.spawn stdout/stderr): hold a - // ref across the pending uv_read_start so the source is not - // finalized while IOCP has a read queued on it. - if !self.started.get() - && !self.waiting_for_on_reader_done.get() - && self.reader().source.is_some() - && !self.reader().is_done() - { - self.waiting_for_on_reader_done.set(true); - // SAFETY: see `parent()`. - unsafe { (*self.parent()).increment_count() }; - } - } + } else if !self.started.get() + && !self.waiting_for_on_reader_done.get() + && self.reader().flags.contains(ReaderFlags::POLLABLE) + && !self.reader().is_done() + { + self.waiting_for_on_reader_done.set(true); + // SAFETY: see `parent()`. + unsafe { (*self.parent()).increment_count() }; } #[cfg(unix)] { - use bun_io::pipe_reader::PosixFlags; if file_type == FileType::Socket { - self.reader().flags.insert(PosixFlags::SOCKET); + self.reader().flags.insert(ReaderFlags::SOCKET); } let r = self.reader(); if let Some(poll) = r.handle.get_poll() { // `bun_io::FilePoll` is an opaque vtable wrapper; flag // mutation goes through `set_flag(FilePollFlag)`. - if file_type == FileType::Socket || r.flags.contains(PosixFlags::SOCKET) { + if file_type == FileType::Socket || r.flags.contains(ReaderFlags::SOCKET) { poll.set_flag(bun_io::FilePollFlag::Socket); } else { // if it's a TTY, we report it as a fifo @@ -462,7 +432,7 @@ impl FileReader { poll.set_flag(bun_io::FilePollFlag::Fifo); } - if r.flags.contains(PosixFlags::NONBLOCKING) { + if r.flags.contains(ReaderFlags::NONBLOCKING) { poll.set_flag(bun_io::FilePollFlag::Nonblocking); } } @@ -477,19 +447,13 @@ impl FileReader { self.buffered.replace(Vec::new()), )); } - } else { - #[cfg(unix)] - { - use bun_io::pipe_reader::PosixFlags; - if !was_lazy && self.reader().flags.contains(PosixFlags::POLLABLE) { - // A from_pipe() reader may arrive with IS_PAUSED set (lazy - // subprocess stdio); clear it so read() does not no-op. - self.reader().unpause(); - // SAFETY: the reader cell is live for `self`'s lifetime; `read` is - // the raw re-entrancy-safe entry (its dispatch runs user JS). - unsafe { IOReader::read(self.reader.get()) }; - } - } + } else if !was_lazy && self.reader().flags.contains(ReaderFlags::POLLABLE) { + // A from_pipe() reader may arrive with IS_PAUSED set (lazy + // subprocess stdio); clear it so read() does not no-op. + self.reader().unpause(); + // SAFETY: the reader cell is live for `self`'s lifetime; `read` is + // the raw re-entrancy-safe entry (its dispatch runs user JS). + unsafe { IOReader::read(self.reader.get()) }; } streams::Start::Ready @@ -892,7 +856,7 @@ impl FileReader { return out; } - if self.reader().has_pending_read() { + if self.reader().buffer_is_awaiting_read() { return Vec::::default(); } @@ -1019,20 +983,6 @@ impl FileReader { } } - pub(crate) fn set_raw_mode(&self, _flag: bool) -> sys::Result<()> { - #[cfg(not(windows))] - { - panic!( - "FileReader.setRawMode must not be called on {}", - std::env::consts::OS - ); - } - #[cfg(windows)] - { - self.reader().set_raw_mode(_flag) - } - } - pub(crate) fn set_flowing(&self, flag: bool) { bun_core::scoped_log!( FileReader, @@ -1098,7 +1048,6 @@ impl Drop for SourcePin { bun_core::impl_field_parent! { FileReader => Source.context; pub fn raw parent; pub fn shared parent_const; } impl readable_stream::SourceContext for FileReader { - const NAME: &'static str = "File"; const SUPPORTS_REF: bool = true; crate::source_context_codegen!(js_FileInternalReadableStreamSource); // R-2: trait sigs are still `&mut self` (shared with ByteBlobLoader/ @@ -1133,9 +1082,6 @@ impl readable_stream::SourceContext for FileReader { fn memory_cost_fn(&self) -> usize { Self::memory_cost(self) } - fn set_raw_mode(&mut self, flag: bool) -> Option> { - Some(Self::set_raw_mode(self, flag)) - } fn set_flowing(&mut self, flag: bool) { Self::set_flowing(self, flag) } diff --git a/src/runtime/webcore/FileSink.rs b/src/runtime/webcore/FileSink.rs index cd1d3348604b..c4ad9bf227a8 100644 --- a/src/runtime/webcore/FileSink.rs +++ b/src/runtime/webcore/FileSink.rs @@ -1,8 +1,6 @@ use core::cell::Cell; use core::sync::atomic::{AtomicI32, Ordering}; -#[cfg(windows)] -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; use bun_io::{self, WriteResult, WriteStatus}; use bun_jsc::JsCell; use bun_ptr::RefPtr; @@ -12,10 +10,6 @@ use crate::api::bun::process::Status as SpawnStatus; use crate::webcore::jsc::{CallFrame, EventLoopHandle, JSGlobalObject, JSValue, JsResult}; use crate::webcore::readable_stream::{self, ReadableStream}; use crate::webcore::{self, AutoFlusher, PathOrFileDescriptor, streams}; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; -#[cfg(windows)] -use bun_sys::windows::libuv::UvHandle as _; bun_core::declare_scope!(FileSink, visible); @@ -29,7 +23,7 @@ bun_core::declare_scope!(FileSink, visible); // canonical `*mut FileSink` instead of any receiver — see the `borrow = ptr` // note on the `impl_streaming_writer_parent!` invocation below. #[derive(bun_ptr::CellRefCounted)] -pub struct FileSink { +pub(crate) struct FileSink { ref_count: Cell, pub(crate) writer: JsCell, pub(crate) event_loop_handle: EventLoopHandle, @@ -44,12 +38,15 @@ pub struct FileSink { // TODO: these fields are duplicated on writer() // we should not duplicate these fields... + #[cfg(unix)] pub(crate) pollable: Cell, + #[cfg(unix)] pub(crate) nonblocking: Cell, pub(crate) force_sync: Cell, #[cfg(unix)] pub(crate) is_tty: Cell, + #[cfg(unix)] pub(crate) is_socket: Cell, pub(crate) fd: Cell, @@ -99,20 +96,10 @@ pub(crate) mod testing_apis { // (acronym splitter treats `AP|Is` as two words); alias so both resolve. pub(crate) use testing_apis as testing_ap_is; -/// `bun_sys` does not yet export -/// an isPollable helper, so re-derive it locally from `S_IFMT`. Windows always -/// returns `false`. +#[cfg(unix)] fn is_pollable(mode: sys::Mode) -> bool { - #[cfg(windows)] - { - let _ = mode; - false - } - #[cfg(unix)] - { - let fmt = mode & (libc::S_IFMT as sys::Mode); - fmt == (libc::S_IFIFO as sys::Mode) || fmt == (libc::S_IFSOCK as sys::Mode) - } + let fmt = mode & (libc::S_IFMT as sys::Mode); + fmt == (libc::S_IFIFO as sys::Mode) || fmt == (libc::S_IFSOCK as sys::Mode) } /// Streaming-writer vtable wiring: the @@ -155,7 +142,6 @@ bun_io::impl_streaming_writer_parent! { on_close = on_close, event_loop = |this| (*this).io_evtloop(), uws_loop = |this| (*this).event_loop_handle.r#loop(), - uv_loop = |this| (*this).event_loop_handle.uv_loop(), ref_ = |this| (&*this).ref_(), deref = |this| FileSink::deref(this), } @@ -223,57 +209,12 @@ pub(crate) extern "C" fn Bun__ForceFileSinkToBeSynchronousForProcessObjectStdio( // wrapped `*FileSink`. let this: &FileSink = unsafe { &(*this_ptr).sink }; - #[cfg(not(windows))] - { - this.force_sync.set(true); - // SAFETY(JsCell): single-field write; does not call into JS. - this.writer.with_mut(|w| w.force_sync = true); - if this.fd.get() != Fd::INVALID { - let _ = sys::update_nonblocking(this.fd.get(), false); - } - } - #[cfg(windows)] - { - // SAFETY(JsCell): closure does not call into JS — pure libuv FFI. - let did_set_blocking = this.writer.with_mut(|w| { - if let Some(source) = w.source.as_mut() { - match source { - bun_io::Source::Pipe(pipe) => { - // SAFETY: `pipe` is a live `Box` owned by `writer.source`; - // `uv_pipe_t` is `#[repr(C)]` with `uv_stream_t` as its first field - // (libuv handle subtyping), so the pointer cast is valid. - let rc = unsafe { - uv::uv_stream_set_blocking( - (&mut **pipe) as *mut uv::Pipe as *mut uv::uv_stream_t, - 1, - ) - }; - if rc == uv::ReturnCode::ZERO { - return true; - } - } - bun_io::Source::Tty(tty) => { - // SAFETY: `tty` is a live `BackRef` (heap or static stdin tty); - // `Tty` (via its first field, `uv::uv_tty_t`) embeds `uv_stream_t` as - // its first member, so the cast is the libuv handle-subtype downcast. - let rc = unsafe { - uv::uv_stream_set_blocking(tty.as_ptr().cast::(), 1) - }; - if rc == uv::ReturnCode::ZERO { - return true; - } - } - _ => {} - } - } - false - }); - if did_set_blocking { - return; - } - - // Fallback to WriteFile() if it fails. - this.force_sync.set(true); + this.force_sync.set(true); + // SAFETY(JsCell): single-field write; does not call into JS. + this.writer.with_mut(|w| w.force_sync = true); + #[cfg(unix)] + if this.fd.get() != Fd::INVALID { + let _ = sys::update_nonblocking(this.fd.get(), false); } } @@ -377,8 +318,6 @@ impl FileSink { (*this).written.set((*this).written.get() + amount); - // TODO: on windows done means ended (no pending data on the buffer) on unix we can still have pending data on the buffer - // we should unify the behaviors to simplify this let has_pending_data = (*this).writer.get().has_pending_data(); // Only keep the event loop ref'd while there's a pending write in progress. // If there's no pending write, no need to keep the event loop ref'd. @@ -490,7 +429,7 @@ impl FileSink { /// # Safety /// `this` must be the canonical live `*mut FileSink` (see /// [`on_attached_process_exit`](Self::on_attached_process_exit)). - pub unsafe fn on_ready(this: *mut FileSink) { + pub(crate) unsafe fn on_ready(this: *mut FileSink) { bun_core::scoped_log!(FileSink, "onReady()"); // SAFETY: caller contract — `this` is live; only `source` is reborrowed. unsafe { @@ -522,7 +461,7 @@ impl FileSink { /// # Safety /// `this` must be the canonical live `*mut FileSink` (see /// [`on_attached_process_exit`](Self::on_attached_process_exit)). May free `this`. - pub unsafe fn on_close(this: *mut FileSink) { + pub(crate) unsafe fn on_close(this: *mut FileSink) { bun_core::scoped_log!(FileSink, "onClose()"); // SAFETY: caller contract — `this` is live with write+dealloc provenance. unsafe { @@ -554,14 +493,8 @@ impl FileSink { } } - /// `writer.end()`; `on_close` follows and may free `self`, except (Windows) for an fd the writer - /// does not own, which is never closed: settle a piped stream here then. + /// `writer.end()`; `on_close` follows and may free `self`. fn end_writer(&self) { - #[cfg(windows)] - if !self.writer.get().owns_fd { - self.settle_stream_done(); - self.release_pipe(); - } self.writer.with_mut(|w| w.end()); } @@ -640,33 +573,63 @@ impl FileSink { } } + pub(crate) fn create(event_loop_: impl Into, fd: Fd) -> RefPtr { + Self::init(fd, event_loop_) + } + + /// What the writer is started on: a HANDLE of its own for a path or an fd, + /// the fd itself for a standard handle. #[cfg(windows)] - pub(crate) fn create_with_pipe( - event_loop_: impl Into, - pipe: *mut uv::Pipe, - ) -> RefPtr { - let evtloop: EventLoopHandle = event_loop_.into(); - - // SAFETY: `pipe` is a live `*mut uv::Pipe` provided by the caller. - // `UvHandle::fd()` returns the raw `uv_os_fd_t` (HANDLE on Windows); - // INVALID_HANDLE_VALUE maps to `Fd::INVALID`, anything else is - // tagged as a system handle. - let fd = match unsafe { (*pipe).fd() } { - h if h == uv::INVALID_HANDLE_VALUE => Fd::INVALID, - h => Fd::from_system(h), - }; - let this = RefPtr::new(FileSink::new(evtloop, fd)); - // SAFETY: `this` was just allocated above and is the sole reference. - unsafe { - (*this.as_ptr()).writer.get_mut().set_pipe(pipe); - (*this.as_ptr()).writer.get_mut().set_parent(this.as_ptr()); + fn open_input(&self, options: &Options) -> sys::Result { + match &options.input_path { + // A standard handle is passed as is: the writer's source leaves it open, and + // `Pipe::open_foreign` caches what it learned by which standard stream it is. + PathOrFileDescriptor::Fd(fd) if fd.stdio_tag().is_some() => Ok(*fd), + PathOrFileDescriptor::Fd(fd) => sys::dup(*fd), + PathOrFileDescriptor::Path(path) => { + use crate::node::types::PathLikeExt as _; + let mut buf = bun_paths::path_buffer_pool::get(); + let path = crate::node::PathLike::borrowed(path.slice()); + sys::open(path.slice_z(&mut buf), options.flags(), options.mode) + .map_err(|err| err.with_path(path.slice())) + } } - this } - #[cfg(not(windows))] - pub(crate) fn create(event_loop_: impl Into, fd: Fd) -> RefPtr { - Self::init(fd, event_loop_) + /// An fd of the writer's own for what `options.input_path` names, and + /// what kind of fd it is. + #[cfg(unix)] + fn open_input(&self, options: &Options) -> sys::Result { + let io_path = match &options.input_path { + PathOrFileDescriptor::Fd(fd) => bun_io::PathOrFileDescriptor::Fd(*fd), + PathOrFileDescriptor::Path(slice) => bun_io::PathOrFileDescriptor::Path(slice.slice()), + }; + let mut is_tty = false; + let mut pollable = self.pollable.get(); + let mut is_socket = self.is_socket.get(); + let mut nonblocking = self.nonblocking.get(); + let result = bun_io::open_for_writing( + Fd::cwd(), + &io_path, + options.flags(), + options.mode, + &mut pollable, + &mut is_socket, + self.force_sync.get(), + &mut nonblocking, + &mut is_tty, + |is_tty: &mut bool| *is_tty = true, + is_pollable, + ); + self.pollable.set(pollable); + self.is_socket.set(is_socket); + self.nonblocking.set(nonblocking); + if is_tty { + // TTY writes are synchronous, as in Node. + self.is_tty.set(true); + self.force_sync.set(true); + } + result } pub(crate) fn setup( @@ -679,74 +642,29 @@ impl FileSink { return sys::Result::Ok(()); } - // reshaped for borrowck — split into a local capture and apply after. - // R-2: out-params for `bun_io::open_for_writing` are local then `Cell::set`. - let mut is_tty_out = false; - let mut pollable_out = self.pollable.get(); - let mut is_socket_out = self.is_socket.get(); - let mut nonblocking_out = self.nonblocking.get(); - // `OpenForWritingInput` is impl'd for - // `bun_io::PathOrFileDescriptor`, not `webcore::PathOrFileDescriptor`; - // bridge by-value here. The borrowed slice is valid for the duration of - // `open_for_writing` (the call only needs it for `openat_a`). - let io_path = match &options.input_path { - PathOrFileDescriptor::Fd(fd) => bun_io::PathOrFileDescriptor::Fd(*fd), - PathOrFileDescriptor::Path(slice) => bun_io::PathOrFileDescriptor::Path(slice.slice()), - }; - let open = |pollable_out: &mut bool, - is_socket_out: &mut bool, - nonblocking_out: &mut bool, - is_tty_out: &mut bool| { - bun_io::open_for_writing( - Fd::cwd(), - &io_path, - options.flags(), - options.mode, - pollable_out, - is_socket_out, - self.force_sync.get(), - nonblocking_out, - is_tty_out, - |_tty: &mut bool| { - #[cfg(unix)] - { - *_tty = true; - } - }, - is_pollable, + // Writes to the process's own stdout and stderr are synchronous on + // Windows whatever they are connected to. Node documents files and pipes that way: + // https://nodejs.org/api/process.html#a-note-on-process-io + #[cfg(windows)] + if let PathOrFileDescriptor::Fd(fd) = &options.input_path + && matches!( + fd.stdio_tag(), + Some(bun_sys::Stdio::StdOut | bun_sys::Stdio::StdErr) ) - }; - let mut result = open( - &mut pollable_out, - &mut is_socket_out, - &mut nonblocking_out, - &mut is_tty_out, - ); - if options.mkdirp { - if let (sys::Result::Err(err), bun_io::PathOrFileDescriptor::Path(path)) = - (&result, &io_path) - { - if err.get_errno() == sys::E::ENOENT { - result = match webcore::blob::mkdirp_parent(path) { - Ok(()) => open( - &mut pollable_out, - &mut is_socket_out, - &mut nonblocking_out, - &mut is_tty_out, - ), - Err(err) => Err(err), - }; - } - } - } - self.pollable.set(pollable_out); - self.is_socket.set(is_socket_out); - self.nonblocking.set(nonblocking_out); - #[cfg(unix)] - if is_tty_out { - // TTY writes are synchronous, as in Node. - self.is_tty.set(true); + { self.force_sync.set(true); + } + + let mut result = self.open_input(options); + if options.mkdirp + && let (sys::Result::Err(err), PathOrFileDescriptor::Path(path)) = + (&result, &options.input_path) + && err.get_errno() == sys::E::ENOENT + { + result = webcore::blob::mkdirp_parent(path.slice(), false) + .and_then(|()| self.open_input(options)); + } + if self.force_sync.get() { // SAFETY(JsCell): single-field write; does not call into JS. self.writer.with_mut(|w| w.force_sync = true); } @@ -762,29 +680,14 @@ impl FileSink { self.close_with_graph(context); } + // On Windows "pollable" is an overlapped pipe end Bun created, which + // nothing opened here is. #[cfg(windows)] - { - if self.force_sync.get() { - // SAFETY(JsCell): `start_sync` is pure I/O setup; no JS. - match self - .writer - .with_mut(|w| w.start_sync(fd, self.pollable.get())) - { - sys::Result::Err(err) => { - fd.close(); - return sys::Result::Err(err); - } - sys::Result::Ok(()) => { - self.writer - .with_mut(|w| w.update_ref(self.io_evtloop(), false)); - } - } - return sys::Result::Ok(()); - } - } - + let pollable = false; + #[cfg(unix)] + let pollable = self.pollable.get(); // SAFETY(JsCell): `start` is pure I/O setup; no JS. - match self.writer.with_mut(|w| w.start(fd, self.pollable.get())) { + match self.writer.with_mut(|w| w.start(fd, pollable)) { sys::Result::Err(err) => { fd.close(); return sys::Result::Err(err); @@ -999,7 +902,7 @@ impl FileSink { } } - pub fn flush(&self) -> sys::Result<()> { + pub(crate) fn flush(&self) -> sys::Result<()> { sys::Result::Ok(()) } @@ -1043,6 +946,21 @@ impl FileSink { return sys::Result::Err(err); } }; + // A flush settles when what was written before it has been written out. + // `to_result` reports an in-flight Windows write as accepted, which is + // right for a `write()` and would let this return with the bytes still + // on their way; `on_write` settles the pending promise. + if cfg!(windows) && matches!(rc, WriteResult::Pending(_)) { + if !self.must_be_kept_alive_until_eof.get() { + self.must_be_kept_alive_until_eof.set(true); + self.ref_(); + } + self.pending.with_mut(|p| { + p.consumed += flushed; + p.result = streams::Writable::Owned(p.consumed); + }); + return sys::Result::Ok(streams::Writable::Pending(self.pending.as_ptr()).to_js(cx)); + } // A flush takes no new chunk from the caller; a pending one reports the // bytes it pushed out. It only reaches here when no write is pending. match self.to_result(rc, flushed) { @@ -1163,7 +1081,7 @@ impl FileSink { ) } - pub fn write(&self, data: &streams::Result) -> streams::Writable { + pub(crate) fn write(&self, data: &streams::Result) -> streams::Writable { if self.done.get() { return streams::Writable::Done; } @@ -1327,7 +1245,7 @@ impl FileSink { } } - pub fn to_js(&mut self, global_this: &JSGlobalObject) -> JSValue { + pub(crate) fn to_js(&mut self, global_this: &JSGlobalObject) -> JSValue { // Wrapper's +1; balanced by `finalize` → `deref()`. self.ref_(); JSSink::create_object(global_this, self, 0) @@ -1452,9 +1370,7 @@ impl FileSink { } pub(crate) fn update_ref(&self, value: bool) { - // `with_mut`: the Windows `BaseWindowsPipeWriter` impls take `&mut self` - // (the posix `PosixStreamingWriter` impls are `&self`); `with_mut` - // covers both. No JS re-entry — pure libuv ref/unref. + // No JS re-entry — pure loop ref/unref. self.writer.with_mut(|w| { if value { w.enable_keeping_process_alive(self.io_evtloop()); @@ -1541,7 +1457,7 @@ impl FileSink { { match self.fd.get().decode_windows() { bun_sys::fd::DecodeWindows::Windows(_) => -1, // TODO: - bun_sys::fd::DecodeWindows::Uv(num) => num, + bun_sys::fd::DecodeWindows::Crt(num) => num, } } #[cfg(not(windows))] @@ -1597,8 +1513,8 @@ impl FileSink { self.must_be_kept_alive_until_eof.set(true); self.ref_(); } - // A Windows uv_write is always async: Pending with an empty - // outgoing buffer is not backpressure, so keep the source flowing. + // A Windows write always completes through the loop: Pending with an + // empty outgoing buffer is not backpressure, so keep the source flowing. if !self.writer.get().is_backed_up() { return streams::Writable::Owned(accepted); } @@ -1639,11 +1555,14 @@ impl FileSink { started: Cell::new(false), must_be_kept_alive_until_eof: Cell::new(false), source_pending_pull: Cell::new(false), + #[cfg(unix)] pollable: Cell::new(false), + #[cfg(unix)] nonblocking: Cell::new(false), force_sync: Cell::new(false), #[cfg(unix)] is_tty: Cell::new(false), + #[cfg(unix)] is_socket: Cell::new(false), fd: Cell::new(fd), auto_flusher: JsCell::new(AutoFlusher::default()), @@ -1668,12 +1587,6 @@ bun_jsc::impl_abort_handle_owner!(FileSink, abort_handle, |this, _cause| { unsafe { let _guard = RefPtr::init_ref(this); (*this).done.set(true); - #[cfg(windows)] - if !(*this).writer.get().owns_fd { - // Never closed, so `on_close` does not follow: settle a piped stream here. - (*this).settle_stream_done(); - (*this).release_pipe(); - } (*this).writer.with_mut(|w| w.close()); FileSink::run_pending(this); FileSink::clear_keep_alive_ref(this); @@ -1802,7 +1715,7 @@ fn on_reject_stream(global_this: &JSGlobalObject, callframe: &CallFrame) -> JsRe impl FileSink { /// `Bun.write(file, stream)`: the byte-count promise `on_close` settles, or an `Error` value. - pub fn pipe_stream( + pub(crate) fn pipe_stream( &mut self, stream: &mut ReadableStream, global_this: &JSGlobalObject, @@ -1862,7 +1775,7 @@ impl FileSink { } } - pub fn assign_to_stream( + pub(crate) fn assign_to_stream( &mut self, stream: &mut ReadableStream, global_this: &JSGlobalObject, diff --git a/src/runtime/webcore/ReadableStream.rs b/src/runtime/webcore/ReadableStream.rs index 8202acb9af8e..f1934b416d9b 100644 --- a/src/runtime/webcore/ReadableStream.rs +++ b/src/runtime/webcore/ReadableStream.rs @@ -13,13 +13,13 @@ use crate::webcore::streams; use crate::webcore::{self, Blob, ByteBlobLoader, ByteStream, FileReader}; #[derive(Copy, Clone)] -pub struct ReadableStream { +pub(crate) struct ReadableStream { pub value: JSValue, pub ptr: Source, } /// Outcome of [`ReadableStream::wire_native_sink`]. -pub enum NativeWireResult { +pub(crate) enum NativeWireResult { /// The sink was installed on the native source; the source will call /// `SinkHandle::end` itself when it's done. Wired, @@ -163,7 +163,7 @@ unsafe extern "C" { // ─── ReadableStream methods ────────────────────────────────────────────────── impl ReadableStream { - pub fn tee( + pub(crate) fn tee( &self, global_this: &JSGlobalObject, ) -> JsResult> { @@ -185,7 +185,7 @@ impl ReadableStream { } /// Re-read this stream's tag (its native source may have changed hands). Pure, like `from_js_direct`. - pub fn reload_tag(&mut self) { + pub(crate) fn reload_tag(&mut self) { *self = ReadableStream::from_js_direct(self.value).unwrap_or(ReadableStream { ptr: Source::Invalid, value: JSValue::ZERO, @@ -193,7 +193,10 @@ impl ReadableStream { } /// Lift the whole payload out of an unread stream. On success the stream is spent (closed, disturbed, locked). - pub fn to_any_blob(&mut self, global_this: &JSGlobalObject) -> Option { + pub(crate) fn to_any_blob( + &mut self, + global_this: &JSGlobalObject, + ) -> Option { if self.is_disturbed(global_this) || self.is_locked(global_this) { return None; } @@ -244,7 +247,7 @@ impl ReadableStream { Some(blob) } - pub fn done(&self) { + pub(crate) fn done(&self) { // done is called when we are done consuming the stream // cancel actually mark the stream source as done // this will resolve any pending promises to done: true @@ -261,7 +264,7 @@ impl ReadableStream { /// Cancel the stream (an `AbortError` reason) and mark its native source done. The source's own /// cancel failure is the cancel promise's (handled) rejection; `Err` is anything thrown synchronously. - pub fn cancel(&self, global_this: &JSGlobalObject) -> JsResult<()> { + pub(crate) fn cancel(&self, global_this: &JSGlobalObject) -> JsResult<()> { let result = bun_jsc::cpp::ReadableStream__cancel(self.value, global_this); self.done(); result @@ -271,7 +274,7 @@ impl ReadableStream { /// cancel algorithm (the spec's ReadableStreamCancel). Unlike `cancel()`, /// this does not synthesize a DOMException — fetch() uses it to surface /// `AbortSignal.reason` to the request body's cancel callback. - pub fn cancel_with_reason( + pub(crate) fn cancel_with_reason( &self, global_this: &JSGlobalObject, reason: JSValue, @@ -282,7 +285,7 @@ impl ReadableStream { result } - pub fn abort(&self, global_this: &JSGlobalObject) -> JsResult<()> { + pub(crate) fn abort(&self, global_this: &JSGlobalObject) -> JsResult<()> { // for now we are just calling cancel should be fine self.cancel(global_this) } @@ -308,7 +311,7 @@ impl ReadableStream { /// fast-paths after wiring a `SinkHandle` directly so `.locked`, /// `.getReader()`, and body-mixin disturbed checks behave as they would /// after `readStreamIntoSink` acquires a reader. - pub fn lock_native(&self, global_object: &JSGlobalObject) { + pub(crate) fn lock_native(&self, global_object: &JSGlobalObject) { ReadableStream__lockNative(self.value, global_object); } @@ -318,7 +321,7 @@ impl ReadableStream { /// the source's `sinkOwner` slot is pointed at `owner_cell` (`owner` /// belongs to the producer side). See [`NativeWireResult`] for caller /// obligations. - pub fn wire_native_sink( + pub(crate) fn wire_native_sink( &self, global: &JSGlobalObject, sink: webcore::SinkHandle, @@ -405,28 +408,28 @@ impl ReadableStream { NativeWireResult::NotNative } - pub fn is_disturbed(&self, global_object: &JSGlobalObject) -> bool { + pub(crate) fn is_disturbed(&self, global_object: &JSGlobalObject) -> bool { is_disturbed_value(self.value, global_object) } - pub fn is_locked(&self, global_object: &JSGlobalObject) -> bool { + pub(crate) fn is_locked(&self, global_object: &JSGlobalObject) -> bool { // SAFETY: FFI call; value is a valid ReadableStream JSValue. ReadableStream__isLocked(self.value, global_object) } /// Fetch's "body is unusable" (). - pub fn is_disturbed_or_locked(&self, global_object: &JSGlobalObject) -> bool { + pub(crate) fn is_disturbed_or_locked(&self, global_object: &JSGlobalObject) -> bool { self.is_disturbed(global_object) || self.is_locked(global_object) } /// A pure `dynamicDowncast` type test: no tagging, no conversion. - pub fn is_readable_stream(value: JSValue) -> bool { + pub(crate) fn is_readable_stream(value: JSValue) -> bool { ReadableStream__is(value) } /// As [`from_js`](Self::from_js), but only matches a value that already is a `ReadableStream` /// (no async-iterable conversion): pure — no script, no exception, no trap poll. - pub fn from_js_direct(value: JSValue) -> Option { + pub(crate) fn from_js_direct(value: JSValue) -> Option { let mut ptr: *mut c_void = core::ptr::null_mut(); let tag = ReadableStreamTag__taggedStream(value, &mut ptr); Self::from_tag(tag, value, ptr) @@ -457,7 +460,7 @@ impl ReadableStream { } } - pub fn from_js( + pub(crate) fn from_js( value: JSValue, global_this: &JSGlobalObject, ) -> JsResult> { @@ -471,7 +474,7 @@ impl ReadableStream { Ok(Self::from_tag(tag, out, ptr)) } - pub fn from_native(global_this: &JSGlobalObject, native: JSValue) -> JsResult { + pub(crate) fn from_native(global_this: &JSGlobalObject, native: JSValue) -> JsResult { bun_jsc::from_js_host_call(global_this, || { ZigGlobalObject__createNativeReadableStream(global_this, native) }) @@ -519,7 +522,7 @@ impl ReadableStream { Self::from_blob_copy_ref(cx, &blob, recommended_chunk_size) } - pub fn from_blob_copy_ref( + pub(crate) fn from_blob_copy_ref( cx: &bun_jsc::JsThread<'_>, blob: &Blob, recommended_chunk_size: webcore::blob::SizeType, @@ -576,7 +579,7 @@ impl ReadableStream { } } - pub fn from_pipe

( + pub(crate) fn from_pipe

( cx: &bun_jsc::JsThread<'_>, _parent: P, buffered_reader: &mut bun_io::BufferedReader, @@ -611,7 +614,7 @@ impl ReadableStream { } /// A stream that delivers `bytes`, then errors with `err`. - pub fn from_bytes_then_error( + pub(crate) fn from_bytes_then_error( cx: &bun_jsc::JsThread<'_>, bytes: Vec, err: syscall::Error, @@ -633,7 +636,7 @@ impl ReadableStream { source.to_readable_stream(cx) } - pub fn empty(global_this: &JSGlobalObject) -> JsResult { + pub(crate) fn empty(global_this: &JSGlobalObject) -> JsResult { bun_jsc::from_js_host_call(global_this, || { // SAFETY: FFI call into JSC bindings; global_this is a valid &JSGlobalObject. ReadableStream__empty(global_this) @@ -641,7 +644,7 @@ impl ReadableStream { } /// A locked stand-in for the stream of a body that was read to its end: closed and disturbed. - pub fn used(global_this: &JSGlobalObject) -> JsResult { + pub(crate) fn used(global_this: &JSGlobalObject) -> JsResult { bun_jsc::from_js_host_call(global_this, || { // SAFETY: FFI call into JSC bindings; global_this is a valid &JSGlobalObject. ReadableStream__used(global_this, true) @@ -649,13 +652,13 @@ impl ReadableStream { } /// A locked stand-in for the stream of a body a consumer is still reading: it stays readable. - pub fn in_use(global_this: &JSGlobalObject) -> JsResult { + pub(crate) fn in_use(global_this: &JSGlobalObject) -> JsResult { bun_jsc::from_js_host_call(global_this, || ReadableStream__used(global_this, false)) } /// A stream already in the `errored` state, so every read rejects with /// `reason` instead of closing cleanly. - pub fn errored(global_this: &JSGlobalObject, reason: JSValue) -> JsResult { + pub(crate) fn errored(global_this: &JSGlobalObject, reason: JSValue) -> JsResult { bun_jsc::from_js_host_call(global_this, || { // SAFETY: FFI call into JSC bindings; global_this is a valid &JSGlobalObject. ReadableStream__errored(global_this, reason) @@ -712,7 +715,7 @@ bun_core::assert_ffi_discr!( // Clone/Copy: bitwise OK — variant pointers are non-owning handles to // JSC-managed loader objects (lifetime governed by the stream/JS heap). #[derive(Copy, Clone)] -pub enum Source { +pub(crate) enum Source { Invalid, /// ReadableStreamDefaultController or ReadableByteStreamController JavaScript, @@ -740,7 +743,7 @@ impl Source { /// Centralises the per-site raw-pointer deref so call sites are /// unsafe-free; the one audited deref lives in [`bun_ptr::BackRef::get`]. #[inline] - pub fn bytes(self) -> Option> { + pub(crate) fn bytes(self) -> Option> { match self { Source::Bytes(p) => Some(bun_ptr::BackRef::from( NonNull::new(p).expect("Source::Bytes payload is non-null"), @@ -757,7 +760,7 @@ impl Source { /// touched through this borrow is `Cell`/`JsCell`-backed, so re-entrant JS /// that re-derives a fresh `&FileReader` from `m_ctx` aliases shared-only. #[inline] - pub fn file(self) -> Option> { + pub(crate) fn file(self) -> Option> { match self { Source::File(p) => Some(bun_ptr::BackRef::from( NonNull::new(p).expect("Source::File payload is non-null"), @@ -774,8 +777,6 @@ impl Source { /// Per-context configuration and callbacks for `NewSource`. pub trait SourceContext: Sized { - /// `name_` — used to look up `jsc.Codegen.JS{NAME}InternalReadableStreamSource`. - const NAME: &'static str; /// `setRefUnrefFn != null` const SUPPORTS_REF: bool = false; @@ -835,19 +836,12 @@ pub trait SourceContext: Sized { None } - /// Returns `None` if the context type does not support raw mode. - /// The `None` default is only reachable if codegen wires `setRawMode` for - /// a context that does not implement it (see `set_raw_mode_from_js`). - fn set_raw_mode(&mut self, _flag: bool) -> Option> { - None - } - /// Default no-op. fn set_flowing(&mut self, _flag: bool) {} } // Hand-wired JSC class (the `#[bun_jsc::JsClass]` derive cannot be used on a -// type generic over `C`): codegen name is "JS{C::NAME}InternalReadableStreamSource". +// type generic over `C`): codegen name is "JS{File,Bytes,Blob}InternalReadableStreamSource". // The toJS/fromJS/fromJSDirect aliases are wired // manually below; cached-property accessors (pendingPromiseSetCached, // closeAdapter{Get,Set}Cached) are emitted by the .classes.ts generator. @@ -1294,20 +1288,6 @@ impl NewSource { self.to_readable_stream_with(cx, ReadableStream::from_native_text) } - pub fn set_raw_mode_from_js( - this: &mut Self, - global: &JSGlobalObject, - call_frame: &CallFrame, - ) -> JsResult { - let flag = call_frame.argument(0); - debug_assert!(flag.is_boolean()); - match this.context.set_raw_mode(flag == JSValue::TRUE) { - Some(Ok(())) => Ok(JSValue::UNDEFINED), - Some(Err(e)) => Ok(e.to_js(global)), - None => unreachable!("setRawMode is not implemented on {}", C::NAME), - } - } - pub fn set_flowing_from_js( this: &mut Self, _global: &JSGlobalObject, diff --git a/src/runtime/webcore/blob/Store.rs b/src/runtime/webcore/blob/Store.rs index 33a6aaa89aba..402e67bda84b 100644 --- a/src/runtime/webcore/blob/Store.rs +++ b/src/runtime/webcore/blob/Store.rs @@ -220,19 +220,12 @@ impl StoreExt for Store { impl FileExt for File { fn unlink(&self, cx: &bun_jsc::JsThread<'_>) -> JsResult { match &self.pathlike { - PathOrFileDescriptor::Path(path_like) => { - // The `*Binding` arg is unused in `AsyncFSTask::create`. - let binding = node_fs::Binding::default(); - // SAFETY: `bun_vm()` returns the live per-global VM pointer; the - // task is created on the JS thread that owns it. - Ok(node_fs::async_::Unlink::create( - cx, - &binding, - node_fs::args::Unlink::owned(path_like.slice().to_vec()), - cx.vm().as_mut(), - None, - )) - } + PathOrFileDescriptor::Path(path_like) => Ok(node_fs::async_::Unlink::create( + cx, + node_fs::args::Unlink::of_bun_file(path_like.slice().to_vec()), + cx.vm().as_mut(), + None, + )), PathOrFileDescriptor::Fd(_) => Ok(JSPromise::resolved_promise_value( cx.global(), // `JSGlobalObject::create_invalid_args` lives in the still-gated diff --git a/src/runtime/webcore/blob/copy_file.rs b/src/runtime/webcore/blob/copy_file.rs index e41579856cdd..fffe77383ef4 100644 --- a/src/runtime/webcore/blob/copy_file.rs +++ b/src/runtime/webcore/blob/copy_file.rs @@ -2,39 +2,21 @@ use crate::node::fs as node_fs; use crate::node::types::PathLikeExt as _; -#[cfg(not(windows))] -use crate::webcore::blob::{self, MkdirpTarget, Retry, store}; -use crate::webcore::blob::{MAX_SIZE, SizeType, Store}; +use crate::webcore::blob::{self, Retry}; +use crate::webcore::blob::{MAX_SIZE, MkdirpTarget, SizeType, Store, store}; use crate::webcore::node_types::PathOrFileDescriptor; -#[cfg(windows)] -use bun_io as aio; -#[cfg(not(windows))] -use bun_jsc::JSGlobalObject; -use bun_jsc::{self as jsc, JSPromise, JSValue}; +use bun_jsc::{self as jsc, JSGlobalObject, JSPromise, JSValue}; use bun_ptr::RefPtr; -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(not(windows))] use bun_sys::Stat; -#[cfg(not(windows))] -use bun_sys::SystemError; -#[cfg(windows)] -use bun_sys::windows::libuv; -use bun_sys::{self, Fd, FdExt, Mode}; -#[cfg(windows)] -use bun_sys_jsc::ErrorJsc as _; +use bun_sys::{self, Fd, FdExt, Mode, SystemError}; #[cfg(any(target_os = "linux", target_os = "android"))] use core::ffi::c_int; -#[cfg(windows)] -use core::ffi::c_void; -#[cfg(not(windows))] use core::marker::ConstParamTy; // ─────────────────────────────────────────────────────────────────────────── -// CopyFile (POSIX, blocking off-thread) +// CopyFile (blocking off-thread) // ─────────────────────────────────────────────────────────────────────────── -#[cfg(not(windows))] pub(crate) struct CopyFile { pub(crate) destination_file_store: store::File, pub(crate) source_file_store: store::File, @@ -56,7 +38,6 @@ pub(crate) struct CopyFile { pub(crate) destination_mode: Option, } -#[cfg(not(windows))] impl MkdirpTarget for CopyFile { fn mkdirp_if_not_exists(&self) -> bool { self.mkdirp_if_not_exists @@ -70,17 +51,25 @@ impl MkdirpTarget for CopyFile { } // SAFETY: file stores/paths and blob store refs (atomic counts); nothing thread-affine. -#[cfg(not(windows))] unsafe impl Send for CopyFile {} -#[cfg(not(windows))] impl jsc::JobContext for CopyFile { type OffThread = Self; type Js = jsc::JSPromiseStrong; + #[cfg(windows)] + fn waits(this: &Self) -> bool { + super::waits_on(&this.source_file_store.pathlike) + || super::waits_on(&this.destination_file_store.pathlike) + } fn run(this: &mut Self, done: bun_jsc::Completion) -> Option> { this.run_async(); Some(done) } + fn closed(this: &mut Self) { + this.system_error = Some( + bun_sys::Error::from_code(bun_sys::E::EBADF, bun_sys::Tag::copyfile).to_system_error(), + ); + } fn then( mut this: Self, mut promise: jsc::JSPromiseStrong, @@ -90,7 +79,6 @@ impl jsc::JobContext for CopyFile { } } -#[cfg(not(windows))] impl CopyFile { /// Schedule the copy on the work pool; returns its promise. pub(crate) fn create( @@ -119,7 +107,11 @@ impl CopyFile { }; let promise = jsc::JSPromiseStrong::init(cx.global()); let value = promise.value(); - jsc::Job::::schedule(cx, copy, promise); + let fd_uses = [ + super::reads_fd_of(©.source_file_store.pathlike), + super::writes_fd_of(©.destination_file_store.pathlike), + ]; + jsc::Job::::schedule_on_fds(cx, copy, promise, fd_uses); value } @@ -179,15 +171,15 @@ impl CopyFile { // This ensures mode is applied even when overwriting existing files, since // open()'s mode argument only affects newly created files. // On macOS clonefile path, chmod is called separately after clonefile. - if let Some(mode) = self.destination_mode { - if self.destination_fd != Fd::INVALID && self.system_error.is_none() { - match bun_sys::fchmod(self.destination_fd, mode) { - bun_sys::Result::Err(err) => { - self.system_error = Some(err.to_system_error()); - } - bun_sys::Result::Ok(()) => {} - } - } + // The mode is for a destination opened here by path, not for a + // descriptor the caller passed. + if let Some(mode) = self.destination_mode + && close_input + && self.system_error.is_none() + && let Err(err) = bun_sys::fchmod(self.destination_fd, mode) + { + let path = self.destination_file_store.pathlike.path().slice(); + self.system_error = Some(err.with_path(path).to_system_error()); } if close_input && close_output { @@ -227,18 +219,7 @@ impl CopyFile { OPEN_SOURCE_FLAGS, 0, ) { - bun_sys::Result::Ok(result) => { - match result.make_lib_uv_owned_for_syscall( - bun_sys::Tag::open, - bun_sys::ErrorCase::CloseOnFail, - ) { - bun_sys::Result::Ok(result_fd) => result_fd, - bun_sys::Result::Err(errno) => { - self.system_error = Some(errno.to_system_error()); - return Err(bun_errno::from_errno(errno.errno as i32).into()); - } - } - } + bun_sys::Result::Ok(result) => result, bun_sys::Result::Err(errno) => { self.system_error = Some(errno.to_system_error()); return Err(bun_errno::from_errno(errno.errno as i32).into()); @@ -261,18 +242,7 @@ impl CopyFile { let dest: &bun_core::ZStr = bun_core::ZStr::from_buf(&path_buf1[..], dest_len); let mode = self.destination_mode.unwrap_or(node_fs::DEFAULT_PERMISSION); match bun_sys::open(dest, OPEN_DESTINATION_FLAGS, mode) { - bun_sys::Result::Ok(result) => { - match result.make_lib_uv_owned_for_syscall( - bun_sys::Tag::open, - bun_sys::ErrorCase::CloseOnFail, - ) { - bun_sys::Result::Ok(result_fd) => self.destination_fd = result_fd, - bun_sys::Result::Err(errno) => { - self.system_error = Some(errno.to_system_error()); - return Err(bun_errno::from_errno(errno.errno as i32).into()); - } - } - } + bun_sys::Result::Ok(result) => self.destination_fd = result, bun_sys::Result::Err(errno) => { match blob::mkdir_if_not_exists(self, &errno, dest, dest.as_bytes()) { Retry::Continue => continue, @@ -503,7 +473,7 @@ impl CopyFile { Ok(()) } - #[cfg(any(target_os = "macos", target_os = "freebsd"))] + #[cfg(any(target_os = "macos", target_os = "freebsd", windows))] fn do_read_write_loop_capped(&mut self, cap: SizeType) -> Result<(), crate::Error> { let mut total: u64 = 0; match read_write_loop_capped(self.source_fd, self.destination_fd, cap, &mut total) { @@ -614,6 +584,32 @@ impl CopyFile { } pub(crate) fn run_async(&mut self) { + // `BUN_FEATURE_FLAG_DISABLE_UV_FS_COPYFILE` skips `CopyFileW`, so that + // the handle-to-handle loop below can be tested with two paths. + #[cfg(windows)] + if !bun_core::env_var::feature_flag::BUN_FEATURE_FLAG_DISABLE_UV_FS_COPYFILE + .get() + .unwrap_or(false) + { + match copy_by_path( + &self.destination_file_store, + &self.source_file_store, + &mut self.mkdirp_if_not_exists, + self.max_length, + self.destination_mode, + ) { + CopyByPath::Copied(len) => { + self.read_len = len; + return; + } + CopyByPath::Failed(err) => { + self.system_error = Some(err); + return; + } + CopyByPath::Unavailable => {} + } + } + #[cfg(target_os = "macos")] let mut stat_: Option = None; #[cfg(not(target_os = "macos"))] @@ -754,13 +750,6 @@ impl CopyFile { debug_assert!(self.destination_fd.is_valid()); debug_assert!(self.source_fd.is_valid()); - if matches!( - self.destination_file_store.pathlike, - PathOrFileDescriptor::Fd(_) - ) { - // nothing to do for the Fd case - } - let stat: Stat = match stat_ { Some(s) => s, None => match bun_sys::fstat(self.source_fd) { @@ -936,18 +925,200 @@ impl CopyFile { return; } - #[cfg(not(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - )))] + #[cfg(windows)] { - compile_error!("TODO: implement copyfile"); + let _ = self.do_read_write_loop_capped(self.max_length); + self.do_close(); } } } +#[cfg(windows)] +enum CopyByPath { + /// The number of bytes now in the destination. + Copied(SizeType), + /// A side has no path (a pipe, a console, the NUL device): copy through + /// handles instead. + Unavailable, + Failed(SystemError), +} + +/// The NUL-terminated path `CopyFileW` can name `pathlike` by, if it has one. +#[cfg(windows)] +fn copyable_path<'a>( + pathlike: &'a PathOrFileDescriptor<'static>, + buf: &'a mut bun_paths::PathBuffer, +) -> bun_sys::Result> { + match pathlike { + PathOrFileDescriptor::Path(path) => Ok(Some(path.slice_z(buf))), + PathOrFileDescriptor::Fd(fd) => match bun_sys::File::borrow(fd).kind()? { + bun_sys::FileKind::Directory => Err(bun_sys::Error::from_code( + bun_sys::E::EISDIR, + bun_sys::Tag::open, + )), + bun_sys::FileKind::CharacterDevice => Ok(None), + _ => { + let Ok(len) = bun_sys::get_fd_path(*fd, buf).map(|path| path.len()) else { + return Ok(None); + }; + buf[len] = 0; + // SAFETY: buf[len] == 0 written above. + Ok(Some(bun_core::ZStr::from_buf(&buf[..], len))) + } + }, + } +} + +#[cfg(windows)] +fn error_with_pathlike( + err: bun_sys::Error, + pathlike: &PathOrFileDescriptor<'static>, +) -> SystemError { + match pathlike { + PathOrFileDescriptor::Path(path) => err.with_path(path.slice()), + PathOrFileDescriptor::Fd(fd) => err.with_fd(*fd), + } + .to_system_error() +} + +/// Copies with `CopyFileW` when both sides have a path. +#[cfg(windows)] +fn copy_by_path( + destination: &store::File, + source: &store::File, + mkdirp_if_not_exists: &mut bool, + max_length: SizeType, + destination_mode: Option, +) -> CopyByPath { + use bun_sys::E; + use bun_sys::windows as w; + + let mut dest_buf = bun_paths::path_buffer_pool::get(); + let mut source_buf = bun_paths::path_buffer_pool::get(); + let dest_path = match copyable_path(&destination.pathlike, &mut dest_buf) { + Ok(Some(path)) => path, + Ok(None) => return CopyByPath::Unavailable, + Err(err) => return CopyByPath::Failed(err.to_system_error()), + }; + let source_path = match copyable_path(&source.pathlike, &mut source_buf) { + Ok(Some(path)) => path, + Ok(None) => return CopyByPath::Unavailable, + Err(err) => return CopyByPath::Failed(err.to_system_error()), + }; + let dest_w = match w::fs::WPath::new(dest_path.as_bytes()) { + Ok(path) => path, + Err(err) => { + let err = bun_sys::Error::from_win32(err, bun_sys::Tag::copyfile); + return CopyByPath::Failed(error_with_pathlike(err, &destination.pathlike)); + } + }; + let source_w = match w::fs::WPath::new(source_path.as_bytes()) { + Ok(path) => path, + Err(err) => { + let err = bun_sys::Error::from_win32(err, bun_sys::Tag::copyfile); + return CopyByPath::Failed(error_with_pathlike(err, &source.pathlike)); + } + }; + + loop { + // SAFETY: both paths are NUL-terminated. + if unsafe { w::CopyFileW(source_w.as_ptr(), dest_w.as_ptr(), 0) } != 0 { + break; + } + let err = bun_sys::Error::from_win32(w::Win32Error::get(), bun_sys::Tag::copyfile); + match err.get_errno() { + // Both sides are the same file: nothing to copy, as in libuv's + // uv_fs_copyfile. + E::EBUSY => { + if let (Ok(a), Ok(b)) = (bun_sys::stat(source_path), bun_sys::stat(dest_path)) { + if a.st_dev == b.st_dev && a.st_ino == b.st_ino { + break; + } + } + } + // Either the source or the destination's directory is missing. + E::ENOENT => { + let source_missing = matches!(source.pathlike, PathOrFileDescriptor::Path(_)) + && matches!( + bun_sys::access(source_path, 0), + Err(e) if e.get_errno() == E::ENOENT + ); + if source_missing { + return CopyByPath::Failed(error_with_pathlike(err, &source.pathlike)); + } + if *mkdirp_if_not_exists { + *mkdirp_if_not_exists = false; + match blob::mkdirp_parent(dest_path.as_bytes(), false) { + Ok(()) => continue, + Err(mkdir_err) => { + return CopyByPath::Failed(error_with_pathlike( + mkdir_err, + &destination.pathlike, + )); + } + } + } + } + // A source that is a directory is refused this way. + E::EPERM => { + if matches!(bun_sys::stat(source_path), Ok(stat) if bun_sys::S::ISDIR(stat.st_mode as u32)) + { + return CopyByPath::Failed(error_with_pathlike( + bun_sys::Error::from_code(E::EISDIR, bun_sys::Tag::copyfile), + &source.pathlike, + )); + } + } + _ => {} + } + // A source that does not open for reading is the side that failed. + // SAFETY: the path is NUL-terminated; no security attributes or template. + let source_handle = unsafe { + w::CreateFileW( + source_w.as_ptr(), + w::GENERIC_READ, + w::FILE_SHARE_READ | w::FILE_SHARE_WRITE | w::FILE_SHARE_DELETE, + core::ptr::null_mut(), + w::OPEN_EXISTING, + 0, + core::ptr::null_mut(), + ) + }; + if source_handle == w::INVALID_HANDLE_VALUE { + return CopyByPath::Failed(error_with_pathlike(err, &source.pathlike)); + } + // SAFETY: a handle this function opened. + unsafe { w::CloseHandle(source_handle) }; + return CopyByPath::Failed(error_with_pathlike(err, &destination.pathlike)); + } + + let size = match &destination.pathlike { + PathOrFileDescriptor::Path(_) => bun_sys::stat(dest_path), + PathOrFileDescriptor::Fd(fd) => bun_sys::fstat(*fd), + }; + let mut copied = match size { + Ok(stat) => stat.size() as SizeType, + Err(err) => return CopyByPath::Failed(err.to_system_error()), + }; + if max_length != MAX_SIZE && copied > max_length { + let _ = node_fs::NodeFS::default().truncate( + &node_fs::args::Truncate { + path: destination.pathlike.clone(), + len: max_length as u64, + flags: 0, + }, + node_fs::Flavor::Sync, + ); + copied = max_length; + } + if let (Some(mode), PathOrFileDescriptor::Path(_)) = (destination_mode, &destination.pathlike) { + if let Err(err) = bun_sys::chmod(dest_path, mode) { + return CopyByPath::Failed(error_with_pathlike(err, &destination.pathlike)); + } + } + CopyByPath::Copied(copied) +} + #[cfg(any(target_os = "linux", target_os = "android"))] fn read_write_fallback( src_fd: Fd, @@ -974,7 +1145,6 @@ fn read_write_fallback( } #[inline(never)] // 64 KB stack buffer -#[cfg(not(windows))] fn read_write_loop_capped( src_fd: Fd, dest_fd: Fd, @@ -1015,16 +1185,12 @@ fn read_write_loop_capped( // exactly what it owns; the `RefPtr`s release just their Store refcounts on // drop. No explicit `Drop` impl is needed. -// Kept local until bun_sys exports these; values match crate::node::fs. -#[cfg(not(windows))] +// Same values as in `node_fs.rs`. const PREALLOCATE_SUPPORTED: bool = cfg!(any(target_os = "linux", target_os = "android")); -#[cfg(not(windows))] const PREALLOCATE_LENGTH: SizeType = 2048 * 1024; -#[cfg(not(windows))] const OPEN_DESTINATION_FLAGS: i32 = bun_sys::O::CLOEXEC | bun_sys::O::CREAT | bun_sys::O::WRONLY | bun_sys::O::TRUNC; -#[cfg(not(windows))] const OPEN_SOURCE_FLAGS: i32 = bun_sys::O::CLOEXEC | bun_sys::O::RDONLY; #[derive(ConstParamTy, PartialEq, Eq, Clone, Copy)] @@ -1037,6 +1203,7 @@ pub(crate) enum TryWith { #[cfg(any(target_os = "linux", target_os = "android"))] impl TryWith { + #[cfg(any(target_os = "linux", target_os = "android"))] pub(crate) const fn tag(self) -> bun_sys::Tag { match self { TryWith::Sendfile => bun_sys::Tag::sendfile, @@ -1046,927 +1213,17 @@ impl TryWith { } } -// ─────────────────────────────────────────────────────────────────────────── -// CopyFileWindows (libuv async) -// ─────────────────────────────────────────────────────────────────────────── - -#[cfg(windows)] -pub(crate) struct CopyFileWindows<'a> { - pub(crate) destination_file_store: RefPtr, - pub(crate) source_file_store: RefPtr, - - pub(crate) io_request: libuv::fs_t, - pub(crate) promise: jsc::JSPromiseStrong, - pub(crate) mkdirp_if_not_exists: bool, - pub(crate) destination_mode: Option, - // per LIFETIMES.tsv: JSC_BORROW → &jsc::EventLoop - // TODO(refactor): lifetime — heap-allocated and re-entered from libuv callbacks; - // likely should be *const jsc::EventLoop. - pub(crate) event_loop: &'a jsc::event_loop::EventLoop, - /// The context of the script that asked for the copy. - pub(crate) context: jsc::ContextId, - - pub(crate) size: SizeType, - - /// Bytes written, stored for use after async chmod completes - pub(crate) written_bytes: usize, - - /// For mkdirp - pub(crate) err: Option, - - /// When we are unable to get the original file path, we do a read-write loop that uses libuv. - pub(crate) read_write_loop: ReadWriteLoop, -} - -#[cfg(windows)] -pub(crate) struct ReadWriteLoop { - pub(crate) source_fd: Fd, - pub(crate) must_close_source_fd: bool, - pub(crate) destination_fd: Fd, - pub(crate) must_close_destination_fd: bool, - pub(crate) written: usize, - pub(crate) read_buf: Vec, - pub(crate) uv_buf: libuv::uv_buf_t, -} - -#[cfg(windows)] -impl Default for ReadWriteLoop { - fn default() -> Self { - Self { - source_fd: Fd::INVALID, - must_close_source_fd: false, - destination_fd: Fd::INVALID, - must_close_destination_fd: false, - written: 0, - read_buf: Vec::new(), - uv_buf: libuv::uv_buf_t { - len: 0, - base: core::ptr::null_mut(), - }, - } - } -} - -// `ReadWriteLoop` is a subobject of `CopyFileWindows`, so passing both as -// `&mut` would be aliasing UB. These are hoisted onto `CopyFileWindows` so the -// borrow checker can see `self.read_write_loop` / `self.io_request` / `self.event_loop` -// as disjoint field accesses through a single `&mut self`. -#[cfg(windows)] -impl<'a> CopyFileWindows<'a> { - fn read_write_loop_start(&mut self) -> bun_sys::Result<()> { - self.read_write_loop.read_buf.reserve_exact(64 * 1024); - - self.read_write_loop_read() - } - - fn read_write_loop_read(&mut self) -> bun_sys::Result<()> { - self.read_write_loop.read_buf.clear(); - // reshaped for borrowck — use the full capacity slice. - let cap = self.read_write_loop.read_buf.capacity(); - self.read_write_loop.uv_buf = libuv::uv_buf_t { - len: cap as libuv::ULONG, - base: self.read_write_loop.read_buf.as_mut_ptr(), - }; - let source_fd = self.read_write_loop.source_fd; - let loop_ = self.event_loop.uv_loop(); - - // This io_request is used for both reading and writing. - // For now, we don't start reading the next chunk until - // we've finished writing all the previous chunks. - self.io_request.data = core::ptr::from_mut(self).cast::(); - - // SAFETY: FFI — `loop_` is the live VM uv loop, `io_request` is a zeroed/cleaned - // `fs_t` owned by `self`, `uv_buf` points into `read_buf`'s capacity, and - // `on_read` is a valid `uv_fs_cb`. - let rc = unsafe { - libuv::uv_fs_read( - loop_, - &mut self.io_request, - source_fd.uv(), - core::ptr::from_mut(&mut self.read_write_loop.uv_buf), - 1, - -1, - Some(on_read), - ) - }; - - if let Some(err) = rc.to_error(bun_sys::Tag::read) { - return bun_sys::Result::Err(err); - } - - bun_sys::Result::Ok(()) - } -} - -#[cfg(windows)] -impl ReadWriteLoop { - pub(crate) fn close(&mut self) { - if self.must_close_source_fd { - match self.source_fd.make_libuv_owned() { - Ok(fd) => { - aio::Closer::close(fd, aio::Loop::get()); - } - Err(_) => { - self.source_fd.close(); - } - } - self.must_close_source_fd = false; - self.source_fd = Fd::INVALID; - } - - if self.must_close_destination_fd { - match self.destination_fd.make_libuv_owned() { - Ok(fd) => { - aio::Closer::close(fd, aio::Loop::get()); - } - Err(_) => { - self.destination_fd.close(); - } - } - self.must_close_destination_fd = false; - self.destination_fd = Fd::INVALID; - } - - self.read_buf = Vec::new(); // clearAndFree() - } -} - -#[cfg(windows)] -extern "C" fn on_read(req: *mut libuv::fs_t) { - // SAFETY: `req->data` was set to `core::ptr::from_mut(self)` (whole-struct - // provenance) before scheduling. Recover the parent from `data` rather than - // `from_field_ptr!(.., io_request, req)`: the `req` pointer libuv hands back was - // produced from a `&mut self.io_request` reborrow whose provenance covers only the - // `io_request` field, so `container_of`-style subtraction would yield a - // `*mut CopyFileWindows` with out-of-bounds provenance (UB under Stacked/Tree - // Borrows). After forming `this`, access the request via `this.io_request` — never - // through `(*req)`, which would alias the live `&mut`. - let this: &mut CopyFileWindows = unsafe { &mut *(*req).data.cast::() }; - debug_assert!(core::ptr::addr_of_mut!(this.io_request) == req); - - let source_fd = this.read_write_loop.source_fd; - let destination_fd = this.read_write_loop.destination_fd; - // reshaped for borrowck — `read_buf.items` is `Vec` len-slice. - let read_buf = &mut this.read_write_loop.read_buf; - - let event_loop = this.event_loop; - - let rc = this.io_request.result; - - bun_sys::syslog!( - "uv_fs_read({}, {}) = {}", - source_fd, - read_buf.len(), - rc.int() - ); - if let Some(err) = rc.to_error(bun_sys::Tag::read) { - this.err = Some(err); - this.on_read_write_loop_complete(); - return; - } - - let n = usize::try_from(rc.int()).expect("int cast"); - // SAFETY: libuv wrote `n` bytes into the buffer's capacity. - unsafe { read_buf.set_len(n) }; - this.read_write_loop.uv_buf = libuv::uv_buf_t::init(read_buf.as_slice()); - - if rc.int() == 0 { - // Handle EOF. We can't read any more. - this.on_read_write_loop_complete(); - return; - } - - // Re-use the fs request. - this.io_request.deinit(); - // SAFETY: FFI — `io_request` was just cleaned via `deinit()`, `uv_buf` points into - // `read_buf` (len set above), and `on_write` is a valid `uv_fs_cb`. - let rc2 = unsafe { - libuv::uv_fs_write( - event_loop.uv_loop(), - &mut this.io_request, - destination_fd.uv(), - core::ptr::from_mut(&mut this.read_write_loop.uv_buf), - 1, - -1, - Some(on_write), - ) - }; - this.io_request.data = core::ptr::from_mut(this).cast::(); - - if let Some(err) = rc2.to_error(bun_sys::Tag::write) { - this.err = Some(err); - this.on_read_write_loop_complete(); - return; - } -} - -#[cfg(windows)] -extern "C" fn on_write(req: *mut libuv::fs_t) { - // SAFETY: see `on_read` — recover from `req->data` (whole-struct provenance), - // not `from_field_ptr!`; then access the request only via `this.io_request`. - let this: &mut CopyFileWindows = unsafe { &mut *(*req).data.cast::() }; - debug_assert!(core::ptr::addr_of_mut!(this.io_request) == req); - let buf_len = this.read_write_loop.read_buf.len(); - - let destination_fd = this.read_write_loop.destination_fd; - - let rc = this.io_request.result; - - bun_sys::syslog!( - "uv_fs_write({}, {}) = {}", - destination_fd, - buf_len, - rc.int() - ); - - if let Some(err) = rc.to_error(bun_sys::Tag::write) { - this.err = Some(err); - this.on_read_write_loop_complete(); - return; - } - - let wrote: u32 = u32::try_from(rc.int()).expect("int cast"); - - this.read_write_loop.written += wrote as usize; - - if (wrote as usize) < buf_len { - if wrote == 0 { - // Handle EOF. We can't write any more. - this.on_read_write_loop_complete(); - return; - } - - // Re-use the fs request. - this.io_request.deinit(); - this.io_request.data = core::ptr::from_mut(this).cast::(); - - let prev = this.read_write_loop.uv_buf.slice(); - this.read_write_loop.uv_buf = libuv::uv_buf_t::init(&prev[wrote as usize..]); - // SAFETY: FFI — `io_request` was just cleaned via `deinit()`, `uv_buf` is a tail - // slice of the previous write buffer (still backed by `read_buf`), and - // `on_write` is a valid `uv_fs_cb`. - let rc2 = unsafe { - libuv::uv_fs_write( - this.event_loop.uv_loop(), - &mut this.io_request, - destination_fd.uv(), - core::ptr::from_mut(&mut this.read_write_loop.uv_buf), - 1, - -1, - Some(on_write), - ) - }; - - if let Some(err) = rc2.to_error(bun_sys::Tag::write) { - this.err = Some(err); - this.on_read_write_loop_complete(); - return; - } - - return; - } - - this.io_request.deinit(); - match this.read_write_loop_read() { - bun_sys::Result::Err(err) => { - this.err = Some(err); - this.on_read_write_loop_complete(); - } - bun_sys::Result::Ok(()) => {} - } -} - -#[cfg(windows)] -impl<'a> CopyFileWindows<'a> { - pub(crate) fn on_read_write_loop_complete(&mut self) { - self.event_loop.unref_keep_alive(); - - if let Some(err) = self.err.take() { - self.throw(err); - return; - } - - let written = self.read_write_loop.written; - self.on_complete(written); - } - - pub(crate) fn new(init: CopyFileWindows<'a>) -> Box> { - Box::new(init) - } - - pub(crate) fn init( - destination_file_store: RefPtr, - source_file_store: RefPtr, - event_loop: &'a jsc::event_loop::EventLoop, - context: &jsc::ScriptExecutionContext, - mkdirp_if_not_exists: bool, - size_: SizeType, - destination_mode: Option, - ) -> JSValue { - // destination_file_store.ref() / source_file_store.ref() — Arc clone - let global = event_loop.global_ref(); - let result = bun_core::heap::into_raw(CopyFileWindows::new(CopyFileWindows { - destination_file_store, - source_file_store, - promise: jsc::JSPromiseStrong::init(global), - // SAFETY: all-zero is a valid libuv::fs_t - io_request: bun_core::ffi::zeroed::(), - event_loop, - context: context.id(), - mkdirp_if_not_exists, - destination_mode, - size: size_, - written_bytes: 0, - err: None, - read_write_loop: ReadWriteLoop::default(), - })); - // SAFETY: result was just allocated above - let result_ref = unsafe { &mut *result }; - let promise = result_ref.promise.value(); - - // On error, this function might free the CopyFileWindows struct. - // So we can no longer reference it beyond this point. - result_ref.copyfile(); - - promise - } - - fn prepare_pathlike( - pathlike: &mut PathOrFileDescriptor, - must_close: &mut bool, - is_reading: bool, - ) -> bun_sys::Result { - if let PathOrFileDescriptor::Path(path) = pathlike { - let fd = match bun_sys::openat_windows_a( - Fd::INVALID, - path.slice(), - if is_reading { - bun_sys::O::RDONLY - } else { - bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::TRUNC - }, - 0, - ) { - bun_sys::Result::Ok(result) => match result.make_libuv_owned() { - Ok(fd) => fd, - Err(_) => { - result.close(); - return bun_sys::Result::Err(bun_sys::Error { - errno: bun_sys::SystemErrno::EMFILE as u16, - syscall: bun_sys::Tag::open, - path: path.slice().into(), - ..Default::default() - }); - } - }, - bun_sys::Result::Err(err) => { - return bun_sys::Result::Err(err); - } - }; - *must_close = true; - bun_sys::Result::Ok(fd) - } else { - // We assume that this is already a uv-casted file descriptor. - bun_sys::Result::Ok(pathlike.fd()) - } - } - - fn prepare_read_write_loop(&mut self) { - // Open the destination first, so that if we need to call - // mkdirp(), we don't spend extra time opening the file handle for - // the source. - self.read_write_loop.destination_fd = match Self::prepare_pathlike( - &mut Store::data_mut(&self.destination_file_store) - .as_file_mut() - .pathlike, - &mut self.read_write_loop.must_close_destination_fd, - false, - ) { - bun_sys::Result::Ok(fd) => fd, - bun_sys::Result::Err(err) => { - if self.mkdirp_if_not_exists && err.get_errno() == bun_sys::E::ENOENT { - self.mkdirp(); - return; - } - - self.throw(err); - return; - } - }; - - self.read_write_loop.source_fd = match Self::prepare_pathlike( - &mut Store::data_mut(&self.source_file_store) - .as_file_mut() - .pathlike, - &mut self.read_write_loop.must_close_source_fd, - true, - ) { - bun_sys::Result::Ok(fd) => fd, - bun_sys::Result::Err(err) => { - self.throw(err); - return; - } - }; - - match self.read_write_loop_start() { - bun_sys::Result::Err(err) => { - self.throw(err); - } - bun_sys::Result::Ok(()) => { - self.event_loop.ref_keep_alive(); - } - } - } - - fn copyfile(&mut self) { - // This is for making it easier for us to test this code path - if bun_core::env_var::feature_flag::BUN_FEATURE_FLAG_DISABLE_UV_FS_COPYFILE - .get() - .unwrap_or(false) - { - self.prepare_read_write_loop(); - return; - } - - let mut pathbuf1 = bun_paths::path_buffer_pool::get(); - let mut pathbuf2 = bun_paths::path_buffer_pool::get(); - // capture the raw `self` pointer before borrowing the file - // stores. `slice_z` ties the returned `&ZStr` lifetime to `&self`, so - // `new_path`/`old_path` keep `self.{destination,source}_file_store` - // borrowed across the `uv_fs_copyfile` call below; taking - // `core::ptr::from_mut(self)` there would require an exclusive reborrow - // of all of `*self` and conflict. The pointer is only stored in - // `io_request.data` for the libuv callback to recover `self`. - let this_ptr: *mut c_void = core::ptr::from_mut(self).cast::(); - let destination_file_store = &mut self.destination_file_store.data.as_file(); - let source_file_store = &mut self.source_file_store.data.as_file(); - - let new_path: &bun_core::ZStr = 'brk: { - match &destination_file_store.pathlike { - PathOrFileDescriptor::Path(_) => { - break 'brk destination_file_store - .pathlike - .path() - .slice_z(&mut pathbuf1); - } - PathOrFileDescriptor::Fd(fd) => { - let fd = *fd; - match bun_sys::File::borrow(&fd).kind() { - bun_sys::Result::Err(err) => { - self.throw(err); - return; - } - bun_sys::Result::Ok(kind) => match kind { - bun_sys::FileKind::Directory => { - self.throw(bun_sys::Error::from_code( - bun_sys::E::EISDIR, - bun_sys::Tag::open, - )); - return; - } - bun_sys::FileKind::CharacterDevice => { - self.prepare_read_write_loop(); - return; - } - _ => { - let out = match bun_sys::get_fd_path(fd, &mut pathbuf1) { - Ok(out) => out, - Err(_) => { - // This case can happen when either: - // - NUL device - // - Pipe. `cat foo.txt | bun bar.ts` - self.prepare_read_write_loop(); - return; - } - }; - let len = out.len(); - pathbuf1[len] = 0; - // SAFETY: pathbuf1[len] == 0 written above - break 'brk bun_core::ZStr::from_buf(&pathbuf1[..], len); - } - }, - } - } - } - }; - let old_path: &bun_core::ZStr = 'brk: { - match &source_file_store.pathlike { - PathOrFileDescriptor::Path(_) => { - break 'brk source_file_store.pathlike.path().slice_z(&mut pathbuf2); - } - PathOrFileDescriptor::Fd(fd) => { - let fd = *fd; - match bun_sys::File::borrow(&fd).kind() { - bun_sys::Result::Err(err) => { - self.throw(err); - return; - } - bun_sys::Result::Ok(kind) => match kind { - bun_sys::FileKind::Directory => { - self.throw(bun_sys::Error::from_code( - bun_sys::E::EISDIR, - bun_sys::Tag::open, - )); - return; - } - bun_sys::FileKind::CharacterDevice => { - self.prepare_read_write_loop(); - return; - } - _ => { - let out = match bun_sys::get_fd_path(fd, &mut pathbuf2) { - Ok(out) => out, - Err(_) => { - // This case can happen when either: - // - NUL device - // - Pipe. `cat foo.txt | bun bar.ts` - self.prepare_read_write_loop(); - return; - } - }; - let len = out.len(); - pathbuf2[len] = 0; - // SAFETY: pathbuf2[len] == 0 written above - break 'brk bun_core::ZStr::from_buf(&pathbuf2[..], len); - } - }, - } - } - } - }; - let loop_ = self.event_loop.uv_loop(); - self.io_request.data = this_ptr; - - // SAFETY: FFI — `loop_` is the live VM uv loop, `io_request` is owned by `self`, - // `old_path`/`new_path` are NUL-terminated (from `slice_z`/`ZStr`), and - // `on_copy_file` is a valid `uv_fs_cb`. - let rc = unsafe { - libuv::uv_fs_copyfile( - loop_, - &mut self.io_request, - old_path.as_ptr(), - new_path.as_ptr(), - 0, - Some(on_copy_file), - ) - }; - - if let Some(mut err) = rc.to_error(bun_sys::Tag::copyfile) { - // https://github.com/oven-sh/bun/issues/6336 - if err.get_errno() == bun_sys::E::EPERM { - err = bun_sys::Error::from_code(bun_sys::E::ENOENT, bun_sys::Tag::copyfile); - } - self.throw(err.with_path(old_path.as_bytes())); - return; - } - self.event_loop.ref_keep_alive(); - } - - pub(crate) fn throw(&mut self, err: bun_sys::Error) { - let _context = jsc::virtual_machine::VirtualMachine::get().enter_context(self.context); - let global_this = self.event_loop.global_ref(); - // `swap()` returns a `&mut JSPromise` into a GC-owned cell (not into - // `self`), but its lifetime is elided to `&mut self`. Decay to a raw pointer so - // borrowck doesn't tie it to `self` across `destroy` below. - let promise = JSPromise::opaque_mut(self.promise.swap()); - let err_instance = err.to_js_with_async_stack(global_this, promise); - - // SAFETY: VM-owned event loop is valid for the process lifetime; `enter_scope` - // calls enter() now and exit() on drop. - let _guard = unsafe { - jsc::event_loop::EventLoop::enter_scope(self.event_loop as *const _ as *mut _) - }; - // SAFETY: self was heap-allocated in init(); destroy reclaims and drops it. self is not accessed afterward. - unsafe { Self::destroy(core::ptr::from_mut(self)) }; - // `promise` points to a GC-owned `JSPromise` cell, not into `self`; valid after `destroy`. - // This libuv completion is the landing frame for what settling leaves. - crate::dispatch::fold(promise.reject(global_this, err_instance)); - } - - pub(crate) fn on_complete(&mut self, written_actual: usize) { - let mut written = written_actual; - if written > usize::try_from(self.size).expect("int cast") && self.size != MAX_SIZE { - self.truncate(); - written = usize::try_from(self.size).expect("int cast"); - } - - // Apply destination mode if specified (async) - if let Some(mode) = self.destination_mode { - if matches!( - self.destination_file_store.data.as_file().pathlike, - PathOrFileDescriptor::Path(_) - ) { - self.written_bytes = written; - let mut pathbuf = bun_paths::path_buffer_pool::get(); - // Borrowck: `slice_z` ties the returned `&ZStr` to - // `&self.destination_file_store`, which would conflict with the - // `core::ptr::from_mut(self)` below. Capture the raw C pointer now — - // it points either into the stack-local `pathbuf` or into the - // Arc-held `destination_file_store` path bytes, both of which outlive - // the `uv_fs_chmod` call (libuv `strdup`s the path internally). - let path_ptr = self - .destination_file_store - .data - .as_file() - .pathlike - .path() - .slice_z(&mut pathbuf) - .as_ptr(); - let loop_ = self.event_loop.uv_loop(); - self.io_request.deinit(); - // SAFETY: all-zero is a valid libuv::fs_t - self.io_request = bun_core::ffi::zeroed::(); - self.io_request.data = core::ptr::from_mut(self).cast::(); - - // SAFETY: FFI — `loop_` is the live VM uv loop, `io_request` was just zeroed, - // `path_ptr` is NUL-terminated (from `slice_z`) and live for this call, - // and `on_chmod` is a valid `uv_fs_cb`. - let rc = unsafe { - libuv::uv_fs_chmod( - loop_, - &mut self.io_request, - path_ptr, - i32::try_from(mode).expect("int cast"), - Some(on_chmod), - ) - }; - - // chmod failed to start - reject the promise to report the error. - if let Some(mut err) = rc.to_error(bun_sys::Tag::chmod) { - let destination = &self.destination_file_store.data.as_file(); - if let PathOrFileDescriptor::Path(p) = &destination.pathlike { - err = err.with_path(p.slice()); - } - self.throw(err); - return; - } - self.event_loop.ref_keep_alive(); - return; - } - } - - self.resolve_promise(written); - } - - fn resolve_promise(&mut self, written: usize) { - let _context = jsc::virtual_machine::VirtualMachine::get().enter_context(self.context); - let global_this = self.event_loop.global_ref(); - // see `throw` — re-type the GC cell via the ZST opaque deref so it - // outlives `destroy(self)` for borrowck. - let promise = JSPromise::opaque_mut(self.promise.swap()); - // SAFETY: VM-owned event loop is valid for the process lifetime; `enter_scope` - // calls enter() now and exit() on drop. - let _guard = unsafe { - jsc::event_loop::EventLoop::enter_scope(self.event_loop as *const _ as *mut _) - }; - - // SAFETY: self was heap-allocated in init(); destroy reclaims and drops it. self is not accessed afterward. - unsafe { Self::destroy(core::ptr::from_mut(self)) }; - // `promise` points to a GC-owned `JSPromise` cell, not into `self`; valid after `destroy`. - // As in `throw`: folded at this libuv completion. - crate::dispatch::fold( - promise.resolve(global_this, JSValue::js_number_from_uint64(written as u64)), - ); - } - - #[cold] - fn truncate(&mut self) { - // TODO: optimize this - - let mut node_fs_ = node_fs::NodeFS::default(); - let _ = node_fs_.truncate( - &node_fs::args::Truncate { - path: self.destination_file_store.data.as_file().pathlike.clone(), - len: u64::try_from(self.size).expect("int cast"), - flags: 0, - }, - node_fs::Flavor::Sync, - ); - } - - /// SAFETY: `this` must have been produced by `heap::alloc` in `init()` and - /// not yet destroyed. After this call `this` is dangling. - pub(crate) unsafe fn destroy(this: *mut Self) { - // SAFETY: caller contract — `this` is a live `heap::alloc`-ed pointer. - unsafe { - (*this).read_write_loop.close(); - // destination_file_store.deref() / source_file_store.deref() — Arc Drop on Box drop - // promise.deinit() — handled by JscStrong's Drop on Box drop - (*this).io_request.deinit(); - drop(bun_core::heap::take(this)); - } - } - - fn mkdirp(&mut self) { - bun_sys::syslog!("mkdirp"); - self.mkdirp_if_not_exists = false; - // Borrowck: compute the raw path slice pointer up-front so the - // immutable borrow of `self.destination_file_store` ends before we take - // `core::ptr::from_mut(self)` for `completion_ctx` below. - let path: *const [u8] = { - let destination = &self.destination_file_store.data.as_file(); - if !matches!(destination.pathlike, PathOrFileDescriptor::Path(_)) { - self.throw(bun_sys::Error { - errno: bun_sys::SystemErrno::EINVAL as u16, - syscall: bun_sys::Tag::mkdir, - ..Default::default() - }); - return; - } - let path_slice = destination.pathlike.path().slice(); - // BORROW: not owned — `destination_file_store` (and thus its path) is held in - // `self`, which outlives the workpool task (completion runs `copyfile`/`throw` - // on `self` before any `destroy`). - bun_paths::dirname(path_slice) - // this shouldn't happen - .unwrap_or(path_slice) as *const [u8] - }; - - self.event_loop.ref_keep_alive(); - node_fs::async_::AsyncMkdirp::schedule(node_fs::async_::AsyncMkdirp { - completion: on_mkdirp_complete_concurrent, - completion_ctx: core::ptr::from_mut(self).cast::<()>(), - path, - ticket: jsc::VirtualMachine::VirtualMachine::get().ticket(), - task: Default::default(), - }); - } - - fn on_mkdirp_complete(&mut self) { - self.event_loop.unref_keep_alive(); - - if let Some(err) = self.err.take() { - // `bun_sys::Error.path` is an owned `Box<[u8]>` and is dropped with - // `err` inside `throw`. - self.throw(err); - return; - } - - self.copyfile(); - } -} - -#[cfg(windows)] -extern "C" fn on_copy_file(req: *mut libuv::fs_t) { - // SAFETY: see `on_read` — recover from `req->data` (whole-struct provenance), - // not `from_field_ptr!`; then access the request only via `this.io_request`. - let this: &mut CopyFileWindows = unsafe { &mut *(*req).data.cast::() }; - debug_assert!(core::ptr::addr_of_mut!(this.io_request) == req); - - let event_loop = this.event_loop; - event_loop.unref_keep_alive(); - let rc = this.io_request.result; - - bun_sys::syslog!("uv_fs_copyfile() = {}", rc); - if let Some(errno) = rc.errno() { - // ENOENT from uv_fs_copyfile can mean either the source file or the - // destination directory is missing. Disambiguate so a missing source - // rejects directly instead of entering the mkdirp+retry path. Only an - // ENOENT from the probe counts as "missing"; any other error leaves - // the mkdirp+retry path available. - let source_missing = errno == bun_sys::E::ENOENT - && match &this.source_file_store.data.as_file().pathlike { - PathOrFileDescriptor::Path(p) => { - let mut buf = bun_paths::path_buffer_pool::get(); - matches!( - bun_sys::access(p.slice_z(&mut buf), 0), - bun_sys::Result::Err(e) if e.get_errno() == bun_sys::E::ENOENT - ) - } - PathOrFileDescriptor::Fd(_) => false, - }; - - if this.mkdirp_if_not_exists && errno == bun_sys::E::ENOENT && !source_missing { - this.io_request.deinit(); - this.mkdirp(); - return; - } - - let mut err = bun_sys::Error::from_code( - // https://github.com/oven-sh/bun/issues/6336 - if errno == bun_sys::E::EPERM { - bun_sys::E::ENOENT - } else { - errno - }, - bun_sys::Tag::copyfile, - ); - let store = if source_missing { - &this.source_file_store - } else { - &this.destination_file_store - }; - match &store.data.as_file().pathlike { - PathOrFileDescriptor::Path(p) => { - err = err.with_path(p.slice()); - } - PathOrFileDescriptor::Fd(fd) => { - err = err.with_fd(*fd); - } - } - - this.throw(err); - return; - } - - // uv_fs_copyfile leaves `statbuf` empty. - let size = match &this.destination_file_store.data.as_file().pathlike { - PathOrFileDescriptor::Path(p) => { - let mut buf = bun_paths::path_buffer_pool::get(); - bun_sys::stat(p.slice_z(&mut buf)).map(|stat| stat.size()) - } - PathOrFileDescriptor::Fd(fd) => bun_sys::fstat(*fd).map(|stat| stat.size()), - }; - let size = match size { - Ok(size) => size, - Err(err) => { - this.throw(err); - return; - } - }; - this.on_complete(usize::try_from(size).expect("int cast")); -} - -#[cfg(windows)] -extern "C" fn on_chmod(req: *mut libuv::fs_t) { - // SAFETY: see `on_read` — recover from `req->data` (whole-struct provenance), - // not `from_field_ptr!`; then access the request only via `this.io_request`. - let this: &mut CopyFileWindows = unsafe { &mut *(*req).data.cast::() }; - debug_assert!(core::ptr::addr_of_mut!(this.io_request) == req); - - let event_loop = this.event_loop; - event_loop.unref_keep_alive(); - - let rc = this.io_request.result; - if let Some(mut err) = rc.to_error(bun_sys::Tag::chmod) { - let destination = &this.destination_file_store.data.as_file(); - if let PathOrFileDescriptor::Path(p) = &destination.pathlike { - err = err.with_path(p.slice()); - } - this.throw(err); - return; - } - - this.resolve_promise(this.written_bytes); -} - -#[cfg(windows)] -fn on_mkdirp_complete_concurrent(ctx: *mut (), err_: bun_sys::Maybe<()>, ticket: &jsc::Ticket) { - bun_sys::syslog!("mkdirp complete"); - // SAFETY: `ctx` is the `*mut CopyFileWindows` stored in `AsyncMkdirp.completion_ctx` - // by `mkdirp` above; sole owner on this concurrent path. - let this = unsafe { bun_ptr::callback_ctx::(ctx.cast()) }; - debug_assert!(this.err.is_none()); - this.err = match err_ { - bun_sys::Result::Err(e) => Some(e), - bun_sys::Result::Ok(()) => None, - }; - ticket.post(jsc::ConcurrentTask::create_from( - std::ptr::from_mut(this).cast::>(), - )); -} - -/// `mkdirp` finished on the work pool: the hop back to the JS thread. Same pointer as the copy, its -/// own tag. -#[cfg(windows)] -#[repr(transparent)] -pub(crate) struct CopyFileWindowsMkdirp<'a>(CopyFileWindows<'a>); - -#[cfg(windows)] -impl bun_event_loop::Taskable for CopyFileWindowsMkdirp<'_> { - const TAG: bun_event_loop::TaskTag = bun_event_loop::task_tag::CopyFileWindowsMkdirp; - /// Frees nothing: the copy is not this task's. - unsafe fn release_unrun(_: *mut Self) {} - /// Enters no context. - unsafe fn context(_: *const Self) -> bun_event_loop::ContextId { - bun_event_loop::ContextId::NONE - } -} - -#[cfg(windows)] -impl CopyFileWindowsMkdirp<'_> { - /// # Safety - /// `this` is the live `CopyFileWindows` `on_mkdirp_complete_concurrent` posted; - /// `on_mkdirp_complete` may free it via `throw`. - pub(crate) unsafe fn run(this: *mut Self) { - // SAFETY: fn contract. - unsafe { (*this).0.on_mkdirp_complete() }; - } -} - // ─────────────────────────────────────────────────────────────────────────── // IOWhich + module-level constants // ─────────────────────────────────────────────────────────────────────────── #[derive(ConstParamTy, PartialEq, Eq, Clone, Copy)] -#[cfg(not(windows))] pub(crate) enum IOWhich { Source, Destination, Both, } -#[cfg(not(windows))] fn unsupported_directory_error() -> SystemError { SystemError { errno: bun_sys::SystemErrno::EISDIR as i32, diff --git a/src/runtime/webcore/blob/read_file.rs b/src/runtime/webcore/blob/read_file.rs index d6ed99f1f2e2..1fcab70fd472 100644 --- a/src/runtime/webcore/blob/read_file.rs +++ b/src/runtime/webcore/blob/read_file.rs @@ -1,52 +1,30 @@ use core::ffi::c_void; use core::marker::PhantomData; -#[cfg(windows)] -use core::mem::MaybeUninit; -use core::sync::atomic::AtomicU8; -#[cfg(not(windows))] -use core::sync::atomic::Ordering; +use core::sync::atomic::{AtomicU8, Ordering}; use crate::Error; use crate::webcore::Lifetime; -#[cfg(not(windows))] use crate::webcore::blob::ClosingState; -#[cfg(windows)] -use crate::webcore::blob::store::Bytes as ByteStore; use crate::webcore::blob::store::{Data, File as FileStore}; use crate::webcore::blob::{Blob, FileCloser, FileOpener, MAX_SIZE, SizeType, Store}; use crate::webcore::node_types::PathOrFileDescriptor; -#[cfg(windows)] -use bun_collections::ByteVecExt as _; use bun_core::String as BunString; use bun_io as io; #[cfg(not(windows))] use bun_io::FileAction; -#[cfg(windows)] -// `bun_jsc::EventLoop` is the *module*; the struct is one level deeper. -use bun_jsc::event_loop::EventLoop; use bun_jsc::{ self as jsc, AnyPromise, JSGlobalObject, JSPromiseStrong, JSValue, JsResult, SystemError, }; use bun_ptr::RefPtr; -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(not(windows))] use bun_sys::Stat; -#[cfg(windows)] -use bun_sys::windows::libuv; use bun_sys::{self, Fd}; use bun_threading::{IntrusiveWorkTask as _, WorkPool, WorkPoolTask}; bun_output::declare_scope!(WriteFile, hidden); -bun_output::declare_scope!(ReadFile, hidden); macro_rules! bloblog { ($($t:tt)*) => { bun_output::scoped_log!(WriteFile, $($t)*) }; } -#[cfg(windows)] -macro_rules! log { - ($($t:tt)*) => { bun_output::scoped_log!(ReadFile, $($t)*) }; -} // ────────────────────────────────────────────────────────────────────────── // NewReadFileHandler @@ -81,7 +59,7 @@ impl<'a, F: ReadFileToJs> NewReadFileHandler<'a, F> { } /// A typed receiver for a file read's bytes. [`ReadFileCompletionFns::of`] erases it to the -/// `(ctx, run, cancel)` a `ReadFile` job carries as its JS side (or a `ReadFileUV` as a field): the +/// `(ctx, run, cancel)` a `ReadFile` job carries as its JS side: the /// shims call `C::run` / `C::cancel` directly and `ctx` is the raw `*mut C`, no extra heap wrapper. pub(crate) trait ReadFileCompletion { /// # Safety @@ -153,7 +131,7 @@ type ReadFileOnReadFileCallback = fn(ctx: *mut c_void, bytes: ReadFileResultType /// The read never completed; do with `ctx` what its owner needs (free it, or tell it). type ReadFileOnCancelCallback = fn(ctx: *mut c_void); -/// What a `ReadFile`/`ReadFileUV` does with the bytes (or the lack of them): `run` on completion, +/// What a `ReadFile` does with the bytes (or the lack of them): `run` on completion, /// `cancel` if it is dropped before completing. Exactly one of the two is invoked, once, on the JS /// thread — the ctx typically owns a promise and a Blob, so this is the job's JS side. pub(crate) struct ReadFileCompletionFns { @@ -193,7 +171,7 @@ impl Drop for ReadFileCompletionFns { } // SAFETY: the ctx holds JS-thread state (promise, Blob); it is only ever completed or cancelled on -// the JS thread — as a Job's `Js` side, or inside the JS-thread-only ReadFileUV. +// the JS thread, as a Job's `Js` side. unsafe impl bun_jsc::job::JsAffine for ReadFileCompletionFns {} pub(crate) struct ReadFileRead { @@ -232,11 +210,23 @@ impl bun_jsc::JobContext for ReadFile { /// Where the bytes go: completed by `then`, or cancelled (its `Drop`) when the job comes /// back to a VM that is no longer running script and is released unrun. type Js = ReadFileCompletionFns; + #[cfg(windows)] + fn waits(this: &Self) -> bool { + super::waits_on(&this.file_store.pathlike) + } fn run(this: &mut Self, done: bun_jsc::Completion) -> Option> { // Starts the read; finishes from the io loop via the token. this.run(done); None } + fn closed(this: &mut Self) { + let mut err = bun_sys::Error::from_code(bun_sys::E::EBADF, bun_sys::Tag::read); + if let PathOrFileDescriptor::Fd(fd) = &this.file_store.pathlike { + err = err.with_fd(*fd); + } + this.errno = Some(bun_errno::from_errno(err.errno as i32).into()); + this.system_error = Some(err.to_system_error().into()); + } fn then( this: Self, completion: ReadFileCompletionFns, @@ -259,7 +249,6 @@ impl bun_jsc::JobContext for ReadFile { } } -#[cfg(not(windows))] impl ReadFile { /// JS thread: hand a prepared `ReadFile` and what to do with its bytes to the work pool (the /// job is its one heap allocation). @@ -268,7 +257,8 @@ impl ReadFile { completion: ReadFileCompletionFns, cx: &bun_jsc::JsThread<'_>, ) { - bun_jsc::Job::::schedule(cx, this, completion); + let fd_use = super::reads_fd_of(&this.file_store.pathlike); + bun_jsc::Job::::schedule_on_fd(cx, this, completion, fd_use); } } @@ -279,30 +269,25 @@ impl ReadFile { pub(crate) struct ReadFile { pub(crate) file_store: FileStore, pub(crate) store: Option>, - #[cfg(not(windows))] pub offset: SizeType, - #[cfg(not(windows))] pub(crate) max_length: SizeType, - #[cfg(not(windows))] pub(crate) total_size: SizeType, pub(crate) opened_fd: Fd, - #[cfg(not(windows))] pub(crate) read_off: SizeType, - #[cfg(not(windows))] pub(crate) read_eof: bool, - #[cfg(not(windows))] pub(crate) size: SizeType, pub(crate) buffer: Vec, pub task: WorkPoolTask, pub(crate) system_error: Option, pub(crate) errno: Option, - #[cfg(not(windows))] pub(crate) io_task: Option, pub(crate) io_poll: io::Poll, pub(crate) io_request: io::Request, #[cfg(not(windows))] pub(crate) io_parking: super::IoParking, - #[cfg(not(windows))] + /// Not a regular file: its reported size cannot be trusted. On POSIX a read + /// may also have to wait for readiness on the io thread; a Windows file + /// handle has no readiness, so there the pool thread's read blocks. pub(crate) could_block: bool, pub(crate) close_after_io: bool, pub(crate) state: AtomicU8, // ClosingState @@ -328,44 +313,19 @@ impl FileOpener for ReadFile { fn pathlike(&self) -> &PathOrFileDescriptor<'static> { &self.file_store.pathlike } - #[cfg(windows)] - fn loop_(&self) -> *mut bun_libuv_sys::uv_loop_t { - unreachable!("ReadFile is POSIX-only; see ReadFileUV") - } - #[cfg(windows)] - fn req(&mut self) -> &mut bun_libuv_sys::uv_fs_t { - unreachable!("ReadFile is POSIX-only; see ReadFileUV") - } - #[cfg(windows)] - fn set_open_callback(&mut self, _cb: fn(&mut Self, Fd)) { - unreachable!() - } - #[cfg(windows)] - fn open_callback(&self) -> fn(&mut Self, Fd) { - unreachable!() - } } crate::webcore::blob::impl_file_closer!(ReadFile); impl ReadFile { pub(crate) fn update(&mut self) { - #[cfg(windows)] - { - return; // why - } - #[cfg(not(windows))] - { - if self.state.load(Ordering::Relaxed) == ClosingState::Closing as u8 { - self.on_finish(); - } else { - self.do_read_loop(); - } + if self.state.load(Ordering::Relaxed) == ClosingState::Closing as u8 { + self.on_finish(); + } else { + self.do_read_loop(); } } - // Not for Windows; Windows callers use ReadFileUV. - #[cfg(not(windows))] pub(crate) fn create( store: RefPtr, off: SizeType, @@ -393,9 +353,13 @@ impl ReadFile { io_poll: io::Poll::default(), io_request: io::Request { next: bun_threading::Link::new(), + #[cfg(not(windows))] callback: Self::on_request_readable, + #[cfg(windows)] + callback: ::schedule_close, scheduled: false, }, + #[cfg(not(windows))] io_parking: super::IoParking::new(), could_block: false, close_after_io: false, @@ -515,7 +479,6 @@ impl ReadFile { /// `max_length - read_off`. Returns `(use_stack, target)` so the caller /// keys its `extend_from_slice`/`commit_spare` decision off the same /// branch taken here. - #[cfg(not(windows))] fn remaining_buffer<'a>( buffer: &'a mut Vec, stack_buffer: &'a mut [u8], @@ -540,17 +503,32 @@ impl ReadFile { } /// Never touches `self.buffer`; the caller moves it out for the duration. - #[cfg(not(windows))] pub(crate) fn do_read( &mut self, buf: &mut [u8], read_len: &mut usize, retry: &mut bool, ) -> bool { + // A Windows read never asks to wait for readiness. + #[cfg(windows)] + let _ = &retry; let result: bun_sys::Result = 'brk: { if bun_sys::S::ISSOCK(self.file_store.mode) { break 'brk bun_sys::recv_non_block(self.opened_fd, buf); } + // NOTE: on Windows a `Bun.file(fd)` of a regular file is read at a + // position and the descriptor's own position stays where it was, + // so reading it twice gives the content twice. Elsewhere `read` + // moves the position. TODO: find out how much relies on either + // before making them one. + #[cfg(windows)] + if self.reads_at_position() { + break 'brk bun_sys::pread( + self.opened_fd, + buf, + (self.offset + self.read_off) as i64, + ); + } break 'brk bun_sys::read(self.opened_fd, buf); }; @@ -562,6 +540,7 @@ impl ReadFile { } Err(err) => { match err.get_errno() { + #[cfg(not(windows))] e if e == io::RETRY => { if !self.could_block { // regular files cannot use epoll. @@ -644,30 +623,19 @@ impl ReadFile { } fn run_async(&mut self, task: ReadFileTask) { - #[cfg(windows)] - { - // Windows reads go through ReadFileUV, never the pool. - let _ = task; - unreachable!("ReadFile on the work pool (Windows uses ReadFileUV)"); - } - #[cfg(not(windows))] - { - self.io_task = Some(task); + self.io_task = Some(task); - if self.file_store.pathlike.is_fd() { - self.opened_fd = self.file_store.pathlike.fd(); - } - - self.get_fd(Self::run_async_with_fd); + if self.file_store.pathlike.is_fd() { + self.opened_fd = self.file_store.pathlike.fd(); } + + self.get_fd(Self::run_async_with_fd); } - #[cfg(not(windows))] pub(crate) fn is_allowed_to_close(&self) -> bool { self.file_store.pathlike.is_path() } - #[cfg(not(windows))] fn on_finish(&mut self) { let close_after_io = self.close_after_io; self.size = self.buffer.len() as SizeType; @@ -687,7 +655,6 @@ impl ReadFile { } } - #[cfg(not(windows))] fn resolve_size_and_last_modified(&mut self, fd: Fd) { let stat: Stat = match bun_sys::fstat(fd) { Ok(result) => result, @@ -700,8 +667,7 @@ impl ReadFile { if let Some(store) = &self.store { if let Data::File(file) = Store::data_mut(store) { - let mtime = bun_sys::PosixStat::init(&stat).mtime(); - file.last_modified = jsc::to_js_time(mtime.sec as isize, mtime.nsec as isize); + file.last_modified = super::stat_to_js_mtime(&stat); } } @@ -733,6 +699,10 @@ impl ReadFile { self.size = self.max_length.min(4096); } + #[cfg(windows)] + if self.reads_at_position() { + return; + } if self.offset > 0 { // We DO support offset in Bun.file() // we ignore errors because it should continue to work even if its a pipe @@ -740,7 +710,12 @@ impl ReadFile { } } - #[cfg(not(windows))] + /// A regular file the caller gave as a descriptor. See `do_read`. + #[cfg(windows)] + fn reads_at_position(&self) -> bool { + !self.could_block && !self.file_store.pathlike.is_path() + } + fn run_async_with_fd(&mut self, fd: Fd) { if self.errno.is_some() { self.on_finish(); @@ -790,6 +765,7 @@ impl ReadFile { // // If we immediately call read(), it will block until stdin is // readable. + #[cfg(not(windows))] if self.could_block { if bun_core::is_readable(fd) == bun_core::Pollable::NotReady { self.wait_for_readable(); @@ -809,7 +785,6 @@ impl ReadFile { this.update(); } - #[cfg(not(windows))] fn do_read_loop(&mut self) { // we hold a 64 KB stack buffer incase the amount of data to // be read is greater than the reported amount @@ -850,6 +825,8 @@ impl ReadFile { // SAFETY: read() wrote `read_amount` initialized bytes into spare capacity. unsafe { bun_core::vec::commit_spare(&mut buffer, read_amount) }; } + // The next read asks for what is left of `max_length`. + self.read_off += read_amount as SizeType; // - If they DID set a max length, we should stop // reading after that. // @@ -876,16 +853,17 @@ impl ReadFile { // // If we immediately call read(), it will block until stdin is // readable. + #[cfg(not(windows))] if retry || (self.could_block - // If we received EOF, we can skip the poll() system - // call. We already know it's done. - && !self.read_eof) + // If we received EOF, we can skip the poll() system + // call. We already know it's done. + && !self.read_eof) { if self.could_block - // If we received EOF, we can skip the poll() system - // call. We already know it's done. - && !self.read_eof + // If we received EOF, we can skip the poll() system + // call. We already know it's done. + && !self.read_eof { match bun_core::is_readable(self.opened_fd) { bun_core::Pollable::NotReady => {} @@ -919,507 +897,3 @@ impl ReadFile { self.on_finish(); } } - -// ────────────────────────────────────────────────────────────────────────── -// ReadFileUV (Windows) -// ────────────────────────────────────────────────────────────────────────── - -#[cfg(windows)] -pub(crate) struct ReadFileUV<'a> { - pub(crate) loop_: *mut libuv::uv_loop_t, - pub(crate) event_loop: &'a EventLoop, - pub(crate) file_store: FileStore, - pub(crate) byte_store: ByteStore, - pub(crate) store: RefPtr, - pub offset: SizeType, - pub(crate) max_length: SizeType, - pub(crate) total_size: SizeType, - pub(crate) opened_fd: Fd, - pub(crate) read_len: SizeType, - pub(crate) read_off: SizeType, - pub(crate) read_eof: bool, - pub(crate) size: SizeType, - pub(crate) buffer: Vec, - pub(crate) system_error: Option, - pub(crate) errno: Option, - /// `Some` until the read completes; a `ReadFileUV` dropped before that cancels it. - pub(crate) completion: Option, - pub(crate) is_regular_file: bool, - /// The context of the script that asked for the read. - pub(crate) context: jsc::ContextId, - - pub(crate) req: libuv::fs_t, - /// Stash for the open completion callback across the libuv async hop. - open_callback: fn(&mut Self, Fd), -} - -#[cfg(windows)] -impl<'a> FileOpener for ReadFileUV<'a> { - fn opened_fd(&self) -> Fd { - self.opened_fd - } - fn set_opened_fd(&mut self, fd: Fd) { - self.opened_fd = fd; - } - fn set_errno(&mut self, e: crate::Error) { - self.errno = Some(e); - } - fn set_system_error(&mut self, e: jsc::SystemError) { - self.system_error = Some(e); - } - fn pathlike(&self) -> &PathOrFileDescriptor<'static> { - &self.file_store.pathlike - } - fn loop_(&self) -> *mut bun_libuv_sys::uv_loop_t { - self.loop_ - } - fn req(&mut self) -> &mut bun_libuv_sys::uv_fs_t { - &mut self.req - } - fn set_open_callback(&mut self, cb: fn(&mut Self, Fd)) { - self.open_callback = cb; - } - fn open_callback(&self) -> fn(&mut Self, Fd) { - self.open_callback - } -} - -#[cfg(windows)] -impl<'a> FileCloser for ReadFileUV<'a> { - const IO_TAG: bun_io::Tag = bun_io::Tag::ReadFile; - fn opened_fd(&self) -> Fd { - self.opened_fd - } - fn set_opened_fd(&mut self, fd: Fd) { - self.opened_fd = fd; - } - fn loop_(&self) -> *mut bun_libuv_sys::uv_loop_t { - self.loop_ - } - - // `ReadFileUV` has no `io_request` field (its libuv request field is - // `req`), so `do_close` falls straight to the close-fd branch and - // none of the methods below are ever reached — these are genuinely dead - // code paths. - fn close_after_io(&self) -> bool { - false - } - fn state(&self) -> &AtomicU8 { - unreachable!("@hasField(ReadFileUV, \"io_request\") == false") - } - fn io_request(&mut self) -> Option<&mut bun_io::Request> { - None - } - fn io_poll(&mut self) -> &mut bun_io::Poll { - unreachable!("@hasField(ReadFileUV, \"io_request\") == false") - } - fn task(&mut self) -> &mut bun_jsc::WorkPoolTask { - unreachable!("@hasField(ReadFileUV, \"io_request\") == false") - } - fn schedule_close(_: &mut bun_io::Request) -> bun_io::Action<'_> { - unreachable!("@hasField(ReadFileUV, \"io_request\") == false") - } - fn on_close_io_request(_: *mut bun_jsc::WorkPoolTask) { - unreachable!("@hasField(ReadFileUV, \"io_request\") == false") - } -} - -#[cfg(windows)] -impl<'a> ReadFileUV<'a> { - /// Typed entry: `C` supplies run/cancel for the erased completion. - pub(crate) fn start( - event_loop: *mut EventLoop, - context: &bun_jsc::ScriptExecutionContext, - store: RefPtr, - off: SizeType, - max_len: SizeType, - handler: *mut C, - ) { - Self::start_with_ctx( - event_loop, - context, - store, - off, - max_len, - ReadFileCompletionFns::of(handler), - ) - } - - /// Raw entry — caller already has the type-erased `(fn, *anyopaque)` pair - /// Shares the body with `start`. - pub(crate) fn start_with_ctx( - event_loop: *mut EventLoop, - context: &bun_jsc::ScriptExecutionContext, - store: RefPtr, - off: SizeType, - max_len: SizeType, - completion: ReadFileCompletionFns, - ) { - log!("ReadFileUV.start"); - // SAFETY: `event_loop` is the per-thread `EventLoop` singleton owned by - // the VM (`global.bun_vm().event_loop()`); it strictly outlives this - // async op, which additionally holds a keep-alive on it below. - let event_loop: &'a EventLoop = unsafe { &*event_loop }; - let file_store = store.data.as_file().clone(); - let this = Box::new(ReadFileUV { - // Projected through the helper to avoid materializing a - // `&VirtualMachine`. - loop_: event_loop.uv_loop().cast(), - event_loop, - file_store, - byte_store: ByteStore::default(), - store, // store.ref() — Arc clone owned here - offset: off, - max_length: max_len, - total_size: MAX_SIZE, - opened_fd: Fd::INVALID, - read_len: 0, - read_off: 0, - read_eof: false, - size: 0, - buffer: Vec::new(), - system_error: None, - errno: None, - completion: Some(completion), - is_regular_file: false, - context: context.id(), - req: bun_core::ffi::zeroed(), - open_callback: Self::on_file_open, - }); - // Keep the event loop alive while the async operation is pending - event_loop.ref_keep_alive(); - let this_ptr: *mut ReadFileUV = bun_core::heap::into_raw(this); - // SAFETY: this_ptr is freshly boxed and uniquely owned by the async op. - unsafe { (*this_ptr).get_fd(Self::on_file_open) }; - // ownership now lives with the libuv request chain until finalize(). - } - - pub(crate) fn finalize(this: *mut Self) { - log!("ReadFileUV.finalize"); - // SAFETY: `this` was heap-allocated in start(); we reclaim ownership here. - let mut this_box = unsafe { bun_core::heap::take(this) }; - let event_loop = this_box.event_loop; - - let completion = this_box - .completion - .take() - .expect("a ReadFileUV completes once"); - let _context = jsc::virtual_machine::VirtualMachine::get().enter_context(this_box.context); - - let result = if let Some(err) = this_box.system_error.take() { - ReadFileResultType::Err(err) - } else { - // Move byte_store out so dropping `this_box` below does not free the - // buffer we hand to the callback. Normalize to `Box<[u8]>` so the - // `is_temporary` consumer (Body.rs / Blob.rs) can soundly reclaim - // via `heap::take` — handing out `(ptr, len)` from a ByteStore - // whose `cap > len` would be a layout-mismatched dealloc. - let boxed = core::mem::take(&mut this_box.byte_store).into_boxed_slice(); - ReadFileResultType::Result(ReadFileRead { - buf: bun_core::heap::into_raw(boxed), - }) - }; - - // The completion must run BEFORE the cleanup below (store deref / req.deinit / - // box drop / event_loop.unref) — it may inspect store. A libuv callback returns void: an - // exception the JS side left pending is reported here (a termination just stands down). - crate::dispatch::fold(completion.complete(result)); - - this_box.req.deinit(); - drop(this_box); - // Release the event loop reference now that we're done - event_loop.unref_keep_alive(); - log!("ReadFileUV.finalize destroy"); - } - - pub(crate) fn is_allowed_to_close(&self) -> bool { - self.file_store.pathlike.is_path() - } - - fn on_finish(&mut self) { - log!("ReadFileUV.onFinish"); - let fd = self.opened_fd; - let needs_close = fd != Fd::INVALID; - - self.size = self.read_len.max(self.size); - self.total_size = self.total_size.max(self.size); - - if needs_close { - if self.do_close(self.is_allowed_to_close()) { - // we have to wait for the close to finish - return; - } - } - - Self::finalize(core::ptr::from_mut(self)); - } - - pub(crate) fn on_file_open(&mut self, opened_fd: Fd) { - log!("ReadFileUV.onFileOpen"); - if self.errno.is_some() { - self.on_finish(); - return; - } - - self.req.deinit(); - self.req.data = core::ptr::from_mut(self).cast::(); - - // SAFETY: FFI — `loop_` is the live VM uv loop, `self.req` is a freshly - // deinit'd `fs_t` owned by `self`, `opened_fd.uv()` is the just-opened fd, - // and `on_file_initial_stat` is a valid `uv_fs_cb` that recovers `self` - // from `req.data` (set above). - let rc = unsafe { - libuv::uv_fs_fstat( - self.loop_, - &mut self.req, - opened_fd.uv(), - Some(Self::on_file_initial_stat), - ) - }; - if let Some(errno) = rc.errno() { - self.errno = Some(bun_errno::from_errno(errno as i32).into()); - self.system_error = Some( - bun_sys::Error::from_code(errno, bun_sys::Tag::fstat) - .to_system_error() - .into(), - ); - self.on_finish(); - return; - } - - self.req.data = core::ptr::from_mut(self).cast::(); - } - - extern "C" fn on_file_initial_stat(req: *mut libuv::fs_t) { - log!("ReadFileUV.onFileInitialStat"); - // SAFETY: req.data was set to *mut Self in on_file_open(). - let this: &mut ReadFileUV = unsafe { bun_ptr::callback_ctx::((*req).data) }; - - // `req` aliases `this.req`; once `&mut ReadFileUV` exists, going through the - // raw `req` pointer would violate Stacked Borrows. Read via `this.req` instead. - if let Some(errno) = this.req.result.errno() { - this.errno = Some(bun_errno::from_errno(errno as i32).into()); - this.system_error = Some( - bun_sys::Error::from_code(errno, bun_sys::Tag::fstat) - .to_system_error() - .into(), - ); - this.on_finish(); - return; - } - - let stat = this.req.statbuf; - - // keep in sync with resolveSizeAndLastModified - if let Data::File(file) = Store::data_mut(&this.store) { - // `uv_timespec_t` fields are `c_long` (i32 on Windows); widen to the - // platform-width `isize` `to_js_time` expects. - file.last_modified = - jsc::to_js_time(stat.mtime().sec as isize, stat.mtime().nsec as isize); - } - - if bun_sys::S::ISDIR(u32::try_from(stat.mode()).expect("int cast")) { - this.errno = Some(crate::Error::Sys(bun_errno::SystemErrno::EISDIR)); - this.system_error = Some(SystemError { - code: BunString::static_("EISDIR").into(), - path: if this.file_store.pathlike.is_path() { - BunString::clone_utf8(this.file_store.pathlike.path().slice()) - } else { - BunString::EMPTY - } - .into(), - message: BunString::static_("Directories cannot be read like files").into(), - syscall: BunString::static_("read").into(), - ..Default::default() - }); - this.on_finish(); - return; - } - // `uv_stat_t::st_size` is `u64` (never negative); clamp to MAX_SIZE - // without a signed detour so a hypothetical >i64::MAX value isn't - // wrapped to negative and then floored to 0. - this.total_size = stat.size().min(MAX_SIZE as u64) as SizeType; - this.is_regular_file = bun_sys::is_regular_file(stat.mode() as bun_sys::Mode); - - log!("is_regular_file: {}", this.is_regular_file); - - if stat.size() > 0 && this.is_regular_file { - this.size = this.total_size.min(this.max_length); - } else if stat.size() == 0 && !this.is_regular_file { - // read up to 4k at a time if they didn't explicitly set a size and - // we're reading from something that's not a regular file. - this.size = this.max_length.min(4096); - } - - if this.offset > 0 { - // We DO support offset in Bun.file() - match bun_sys::set_file_offset(this.opened_fd, this.offset) { - // we ignore errors because it should continue to work even if its a pipe - Err(_) | Ok(_) => {} - } - } - - // Special files might report a size of > 0, and be wrong. - // so we should check specifically that its a regular file before trusting the size. - if this.size == 0 && this.is_regular_file { - // buffer is empty here, - // so move it (Vec) into the owning ByteStore rather than borrow. - this.byte_store = ByteStore::init(core::mem::take(&mut this.buffer)); - this.on_finish(); - return; - } - // Out of memory we can't read more than 4GB at a time (ULONG) on Windows - if this.size as usize > bun_sys::windows::ULONG::MAX as usize { - this.errno = Some(bun_errno::from_errno(bun_sys::E::NOMEM as i32).into()); - this.system_error = Some( - bun_sys::Error::from_code(bun_sys::E::NOMEM, bun_sys::Tag::read) - .to_system_error() - .into(), - ); - this.on_finish(); - return; - } - // add an extra 16 bytes to the buffer to avoid having to resize it for trailing extra data - let want = - ((this.size as usize).saturating_add(16)).min(bun_sys::windows::ULONG::MAX as usize); - if this.buffer.try_reserve_exact(want).is_err() { - this.errno = Some(crate::Error::Alloc(bun_alloc::AllocError)); - this.system_error = Some( - bun_sys::Error::from_code(bun_sys::E::NOMEM, bun_sys::Tag::read) - .to_system_error() - .into(), - ); - this.on_finish(); - return; - } - this.read_len = 0; - this.read_off = 0; - - this.req.deinit(); - - this.queue_read(); - } - - fn remaining_buffer(&mut self) -> &mut [MaybeUninit] { - // libuv writes into spare capacity before any read; callers only need - // ptr/len, so expose the spare slice directly instead of materialising - // a `&mut [u8]` over uninitialized bytes. - let limit = (self.max_length.saturating_sub(self.read_off)) as usize; - let spare = self.buffer.spare_capacity_mut(); - let take = spare.len().min(limit); - &mut spare[..take] - } - - pub(crate) fn queue_read(&mut self) { - // if not a regular file, buffer capacity is arbitrary, and running out doesn't mean we're - // at the end of the file - if (!self.remaining_buffer().is_empty() || !self.is_regular_file) - && self.errno.is_none() - && !self.read_eof - { - log!( - "ReadFileUV.queueRead - this.remainingBuffer().len = {}", - self.remaining_buffer().len() - ); - - if !self.is_regular_file { - // non-regular files have variable sizes, so we always ensure - // theres at least 4096 bytes of free space. there has already - // been an initial allocation done for us - if self.buffer.try_reserve(4096).is_err() { - self.errno = Some(crate::Error::Alloc(bun_alloc::AllocError)); - self.system_error = Some( - bun_sys::Error::from_code(bun_sys::E::NOMEM, bun_sys::Tag::read) - .to_system_error() - .into(), - ); - self.on_finish(); - return; - } - } - - let buf = self.remaining_buffer(); - // Construct uv_buf_t directly from `as_mut_ptr()` so the stored - // `base` carries write provenance — `uv_buf_t::init` takes `&[u8]` - // and would implicitly reborrow `buf` as shared, yielding a - // SharedReadOnly-tagged pointer that libuv then *writes* through - // (uv_fs_read fills this buffer), which is UB under Stacked Borrows. - let mut bufs: [libuv::uv_buf_t; 1] = [libuv::uv_buf_t { - len: buf.len() as libuv::ULONG, - base: buf.as_mut_ptr().cast::(), - }]; - self.req.assert_cleaned_up(); - // SAFETY: FFI — `loop_` is the live VM uv loop, `self.req` is a - // cleaned-up `fs_t` owned by `self`, `bufs` points at a stack uv_buf - // wrapping `self.buffer`'s spare capacity (libuv copies the iovec - // descriptor before returning), `opened_fd.uv()` is the open fd, and - // `on_read` is a valid `uv_fs_cb` that recovers `self` from `req.data`. - let res = unsafe { - libuv::uv_fs_read( - self.loop_, - &mut self.req, - self.opened_fd.uv(), - bufs.as_mut_ptr(), - bufs.len() as u32, - i64::try_from(self.offset + self.read_off).expect("int cast"), - Some(Self::on_read), - ) - }; - self.req.data = core::ptr::from_mut(self).cast::(); - if let Some(errno) = res.errno() { - self.errno = Some(bun_errno::from_errno(errno as i32).into()); - self.system_error = Some( - bun_sys::Error::from_code(errno, bun_sys::Tag::read) - .to_system_error() - .into(), - ); - self.on_finish(); - } - } else { - log!("ReadFileUV.queueRead done"); - - // We are done reading. - let owned = core::mem::take(&mut self.buffer).into_boxed_slice(); - self.byte_store = ByteStore::init_owned(owned); - self.on_finish(); - } - } - - pub(crate) extern "C" fn on_read(req: *mut libuv::fs_t) { - // SAFETY: req.data was set to *mut Self in queue_read(). - let this: &mut ReadFileUV = unsafe { bun_ptr::callback_ctx::((*req).data) }; - - // `req` aliases `this.req`; once `&mut ReadFileUV` exists, going through the - // raw `req` pointer would violate Stacked Borrows. Read via `this.req` instead. - let result = this.req.result; - - if let Some(errno) = result.errno() { - this.errno = Some(bun_errno::from_errno(errno as i32).into()); - this.system_error = Some( - bun_sys::Error::from_code(errno, bun_sys::Tag::read) - .to_system_error() - .into(), - ); - this.on_finish(); - return; - } - - if result.int() == 0 { - // We are done reading. - let owned = core::mem::take(&mut this.buffer).into_boxed_slice(); - this.byte_store = ByteStore::init_owned(owned); - this.on_finish(); - return; - } - - this.read_off += SizeType::try_from(result.int()).expect("int cast"); - // SAFETY: libuv wrote result.int() bytes into remaining_buffer()'s spare slice. - unsafe { - this.buffer - .uv_commit(usize::try_from(result.int()).expect("int cast")) - }; - - this.req.deinit(); - this.queue_read(); - } -} diff --git a/src/runtime/webcore/blob/write_file.rs b/src/runtime/webcore/blob/write_file.rs index 1b0cf6612a57..94aa0b8546d1 100644 --- a/src/runtime/webcore/blob/write_file.rs +++ b/src/runtime/webcore/blob/write_file.rs @@ -1,8 +1,6 @@ use core::ffi::c_void; use core::ptr::NonNull; -use core::sync::atomic::AtomicU8; -#[cfg(not(windows))] -use core::sync::atomic::Ordering; +use core::sync::atomic::{AtomicU8, Ordering}; use crate::Error; use bun_io as io; @@ -13,9 +11,10 @@ use bun_jsc::{self as jsc, JSGlobalObject, JSPromise, JSValue, SystemError}; use bun_sys::{self as sys, Fd}; use bun_threading::{IntrusiveWorkTask as _, WorkPool, WorkPoolTask}; -use crate::webcore::blob::{self, Blob, FileOpener, SizeType}; -#[cfg(not(windows))] -use crate::webcore::blob::{ClosingState, FileCloser, MkdirpTarget, Retry, mkdir_if_not_exists}; +use crate::webcore::blob::{ + self, Blob, ClosingState, FileCloser, FileOpener, MkdirpTarget, Retry, SizeType, + mkdir_if_not_exists, +}; use crate::webcore::body; bun_output::declare_scope!(WriteFile, hidden); @@ -25,10 +24,10 @@ bun_output::declare_scope!(WriteFile, hidden); // callbacks below (`WriteFileOnWriteFileCallback`), never across FFI, so the // layout is unconstrained. /// One `write()` attempt on the pool thread. -#[cfg(not(windows))] pub(crate) enum WriteStep { Wrote(usize), /// A pipe/socket is full: park on the io loop. + #[cfg(not(windows))] WouldBlock, /// `errno`/`system_error` are set. Failed, @@ -56,11 +55,19 @@ impl bun_jsc::JobContext for WriteFile { /// Whom the write is reported to. (Dropped with the job when that is released unrun: the /// promise then stays pending.) type Js = Box; + const OWED_AT_EXIT: bool = cfg!(windows); + #[cfg(windows)] + fn waits(this: &Self) -> bool { + super::waits_on(this.pathlike()) + } fn run(this: &mut Self, done: bun_jsc::Completion) -> Option> { // Starts the write; finishes from the io loop via the token. this.run(done); None } + fn closed(this: &mut Self) { + this.fail(&sys::Error::from_code(sys::E::EBADF, sys::Tag::write)); + } fn then( this: Self, promise: Box, @@ -84,26 +91,24 @@ impl bun_jsc::JobContext for WriteFile { impl WriteFile { /// JS thread: hand a prepared `WriteFile` to the work pool (the job is /// its one heap allocation). - #[cfg(not(windows))] pub(crate) fn schedule( this: WriteFile, promise: Box, cx: &bun_jsc::JsThread<'_>, ) { - bun_jsc::Job::::schedule(cx, this, promise); + let fd_use = super::writes_fd_of(this.pathlike()); + bun_jsc::Job::::schedule_on_fd(cx, this, promise, fd_use); } } pub(crate) struct WriteFile { pub(crate) file_blob: Blob, - #[cfg(not(windows))] pub(crate) bytes_blob: Blob, pub(crate) opened_fd: Fd, pub(crate) system_error: Option, pub(crate) errno: Option, pub task: WorkPoolTask, - #[cfg(not(windows))] pub(crate) io_task: Option, pub(crate) io_poll: io::Poll, pub(crate) io_request: io::Request, @@ -113,10 +118,12 @@ pub(crate) struct WriteFile { pub(crate) total_written: usize, + /// POSIX: the destination is a pipe/socket/tty whose writes can need to + /// wait for readiness on the io thread. A Windows file handle has no + /// readiness; a write there blocks the pool thread instead. #[cfg(not(windows))] pub(crate) could_block: bool, pub(crate) close_after_io: bool, - #[cfg(not(windows))] pub(crate) mkdirp_if_not_exists: bool, } @@ -128,8 +135,12 @@ bun_io::intrusive_io_request!(WriteFile, io_request); // ────────────────────────────────────────────────────────────────────────── impl FileOpener for WriteFile { - const OPEN_FLAGS: i32 = - bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::TRUNC | bun_sys::O::NONBLOCK; + const OPEN_FLAGS: i32 = bun_sys::O::WRONLY + | bun_sys::O::CREAT + | bun_sys::O::TRUNC + | bun_sys::O::NONBLOCK + | bun_sys::O::SEQUENTIAL; + const AS_WRITTEN: bool = true; fn opened_fd(&self) -> Fd { self.opened_fd @@ -154,7 +165,6 @@ impl FileOpener for WriteFile { .as_file() .pathlike } - #[cfg(not(windows))] fn try_mkdirp( &mut self, err: bun_sys::Error, @@ -163,25 +173,8 @@ impl FileOpener for WriteFile { ) -> Retry { mkdir_if_not_exists(self, &err, path, display_path) } - #[cfg(windows)] - fn loop_(&self) -> *mut bun_libuv_sys::uv_loop_t { - unreachable!("WriteFile is POSIX-only; see WriteFileWindows") - } - #[cfg(windows)] - fn req(&mut self) -> &mut bun_libuv_sys::uv_fs_t { - unreachable!("WriteFile is POSIX-only") - } - #[cfg(windows)] - fn set_open_callback(&mut self, _cb: fn(&mut Self, Fd)) { - unreachable!() - } - #[cfg(windows)] - fn open_callback(&self) -> fn(&mut Self, Fd) { - unreachable!() - } } -#[cfg(not(windows))] impl MkdirpTarget for WriteFile { fn mkdirp_if_not_exists(&self) -> bool { self.mkdirp_if_not_exists @@ -206,6 +199,17 @@ impl WriteFile { #[cfg(not(windows))] pub(crate) const IO_TAG: io::Tag = io::Tag::WriteFile; + /// Record `err` as why the write failed. The error names the destination + /// the way the caller did: by path, or by the fd it passed. + fn fail(&mut self, err: &sys::Error) { + self.errno = Some(bun_errno::from_errno(err.errno as i32).into()); + let err = match self.pathlike() { + PathOrFileDescriptor::Path(path) => err.with_path(path.slice()), + PathOrFileDescriptor::Fd(fd) => err.with_fd(*fd), + }; + self.system_error = Some(err.to_system_error().into()); + } + pub(crate) fn on_ready(&mut self) { bun_output::scoped_log!(WriteFile, "WriteFile.onReady()"); #[cfg(not(windows))] @@ -227,8 +231,7 @@ impl WriteFile { if !this.io_parking.fire() { return; } - this.errno = Some(bun_errno::from_errno(err.errno as i32).into()); - this.system_error = Some(err.to_system_error().into()); + this.fail(err); this.task = WorkPoolTask { node: Default::default(), callback: Self::do_write_loop_task, @@ -263,9 +266,7 @@ impl WriteFile { /// See `ReadFile::fail_cancelled`. #[cfg(not(windows))] fn fail_cancelled(&mut self) { - let err = sys::Error::from_code(sys::E::ECANCELED, sys::Tag::write); - self.errno = Some(bun_errno::from_errno(err.errno as i32).into()); - self.system_error = Some(err.to_system_error().into()); + self.fail(&sys::Error::from_code(sys::E::ECANCELED, sys::Tag::write)); self.state .store(ClosingState::Closing as u8, Ordering::SeqCst); } @@ -284,7 +285,6 @@ impl WriteFile { io::IoRequestLoop::schedule(&mut self.io_request); } - #[cfg(not(windows))] pub(crate) fn create( file_blob: Blob, bytes_blob: Blob, @@ -302,10 +302,15 @@ impl WriteFile { }, io_task: None, io_poll: io::Poll::default(), + #[cfg(not(windows))] io_request: io::Request::new(Self::on_request_writable), + #[cfg(windows)] + io_request: io::Request::new(::schedule_close), + #[cfg(not(windows))] io_parking: super::IoParking::new(), state: AtomicU8::new(ClosingState::Running as u8), total_written: 0, + #[cfg(not(windows))] could_block: false, close_after_io: false, mkdirp_if_not_exists, @@ -315,7 +320,6 @@ impl WriteFile { // reshaped for borrowck — take (off, len) here and re-derive the slice // internally so callers don't hold a borrow of self across the &mut self call. - #[cfg(not(windows))] pub(crate) fn do_write(&mut self, off: usize, len: usize) -> WriteStep { let fd = self.opened_fd; debug_assert!(fd != Fd::INVALID); @@ -333,11 +337,12 @@ impl WriteFile { } // regular files cannot use epoll. // this is fine on kqueue, but not on epoll. + #[cfg(not(windows))] Err(err) if err.get_errno() == io::RETRY && !self.could_block => continue, + #[cfg(not(windows))] Err(err) if err.get_errno() == io::RETRY => return WriteStep::WouldBlock, Err(err) => { - self.errno = Some(bun_errno::from_errno(err.errno as i32).into()); - self.system_error = Some(err.to_system_error().into()); + self.fail(&err); return WriteStep::Failed; } } @@ -364,25 +369,14 @@ impl WriteFile { } pub(crate) fn run(&mut self, task: WriteFileTask) { - #[cfg(windows)] - { - // Windows writes go through WriteFileWindows, never the pool. - let _ = task; - unreachable!("WriteFile on the work pool (Windows uses WriteFileWindows)"); - } - #[cfg(not(windows))] - { - self.io_task = Some(task); - self.run_async(); - } + self.io_task = Some(task); + self.run_async(); } - #[cfg(not(windows))] fn run_async(&mut self) { self.get_fd(Self::run_with_fd); } - #[cfg(not(windows))] pub(crate) fn is_allowed_to_close(&self) -> bool { self.file_blob .store @@ -395,7 +389,6 @@ impl WriteFile { .is_path() } - #[cfg(not(windows))] fn on_finish(&mut self) { bun_output::scoped_log!(WriteFile, "WriteFile.onFinish()"); @@ -410,43 +403,55 @@ impl WriteFile { } } - #[cfg(not(windows))] fn run_with_fd(&mut self, fd_: Fd) { if fd_ == Fd::INVALID || self.errno.is_some() { self.on_finish(); return; } + #[cfg(not(windows))] let fd = self.opened_fd; - self.could_block = 'brk: { - if let Some(store) = self.file_blob.store.get().as_ref() { - if let blob::store::Data::File(file) = &store.data { - if file.pathlike.is_fd() { - // If seekable was set, then so was mode - if file.seekable.is_some() { - // This is mostly to handle pipes which were passsed to the process somehow - // such as stderr, stdout. Bun.stdin and Bun.stderr will automatically set `mode` for us. - break 'brk !bun_sys::is_regular_file(file.mode); + // A path can name a pipe, whose reader takes as long as it likes. + #[cfg(windows)] + if self.pathlike().is_path() + && !sys::windows::fs::is_disk_file(fd_) + && let Some(task) = &self.io_task + { + task.held_up_at_exit(); + } + + #[cfg(not(windows))] + { + self.could_block = 'brk: { + if let Some(store) = self.file_blob.store.get().as_ref() { + if let blob::store::Data::File(file) = &store.data { + if file.pathlike.is_fd() { + // If seekable was set, then so was mode + if file.seekable.is_some() { + // This is mostly to handle pipes which were passsed to the process somehow + // such as stderr, stdout. Bun.stdin and Bun.stderr will automatically set `mode` for us. + break 'brk !bun_sys::is_regular_file(file.mode); + } } } } - } - // We opened the file descriptor with O_NONBLOCK, so we - // shouldn't have to worry about blocking reads/writes - // - // We do not call fstat() because that is very expensive. - false - }; + // We opened the file descriptor with O_NONBLOCK, so we + // shouldn't have to worry about blocking reads/writes + // + // We do not call fstat() because that is very expensive. + false + }; - // We have never supported offset in Bun.write(). - // and properly adding support means we need to also support it - // with splice, sendfile, and the other cases. + // We have never supported offset in Bun.write(). + // and properly adding support means we need to also support it + // with splice, sendfile, and the other cases. - if self.could_block && bun_core::is_writable(fd) == bun_core::Pollable::NotReady { - self.wait_for_writable(); - return; + if self.could_block && bun_core::is_writable(fd) == bun_core::Pollable::NotReady { + self.wait_for_writable(); + return; + } } #[cfg(any(target_os = "linux", target_os = "android"))] @@ -487,16 +492,6 @@ impl WriteFile { } fn do_write_loop(&mut self) { - #[cfg(windows)] - { - return; // why - } - #[cfg(not(windows))] - self.do_write_loop_posix(); - } - - #[cfg(not(windows))] - fn do_write_loop_posix(&mut self) { while self.state.load(Ordering::Relaxed) == ClosingState::Running as u8 { let remain_full = self.bytes_blob.shared_view(); // reshaped for borrowck — capture len/offset before mut borrow @@ -506,11 +501,13 @@ impl WriteFile { if remain_len > 0 && self.errno.is_none() { let wrote = match self.do_write(off, remain_len) { WriteStep::Wrote(n) => n, + #[cfg(not(windows))] WriteStep::WouldBlock => return self.wait_for_writable(), WriteStep::Failed => return self.on_finish(), }; // Do not immediately attempt to write again if it's not a regular file. + #[cfg(not(windows))] if self.could_block && bun_core::is_writable(self.opened_fd) == bun_core::Pollable::NotReady { @@ -534,674 +531,6 @@ impl WriteFile { } } -// ────────────────────────────────────────────────────────────────────────── -// WriteFileWindows -// -// libuv-backed write path used by `Blob.writeFileInternal` on Windows. The -// whole impl is `#[cfg(windows)]`-gated because `bun_sys::windows::libuv` -// (and the libuv `fs_t`/`uv_buf_t` types) only exist when targeting Windows. -// ────────────────────────────────────────────────────────────────────────── - -#[cfg(windows)] -pub(crate) use self::windows_impl::{ - WriteFileWindows, WriteFileWindowsError, WriteFileWindowsMkdirp, -}; - -#[cfg(windows)] -mod windows_impl { - use super::*; - use core::ptr::null_mut; - - use bun_io::{self as aio, IntrusiveUvFs as _, KeepAlive}; - // `bun_jsc::EventLoop` is a *module* (namespace re-export); the struct - // lives one level deeper. - use bun_jsc::{ConcurrentTask, event_loop::EventLoop}; - use bun_sys::ReturnCodeExt as _; - use bun_sys::windows::libuv as uv; - - pub(crate) struct WriteFileWindows { - pub(crate) io_request: uv::fs_t, - pub(crate) file_blob: Blob, - pub(crate) bytes_blob: Blob, - pub(crate) on_complete_callback: WriteFileOnWriteFileCallback, - pub(crate) on_complete_ctx: *mut c_void, - /// The context of the script that asked for the write. - pub(crate) context: bun_jsc::ContextId, - pub(crate) mkdirp_if_not_exists: bool, - pub(crate) uv_bufs: [uv::uv_buf_t; 1], - - pub(crate) fd: uv::uv_file, - pub(crate) err: Option, - pub(crate) total_written: usize, - pub(crate) event_loop: *mut EventLoop, - pub poll_ref: KeepAlive, - - pub(crate) owned_fd: bool, - } - - bun_io::intrusive_uv_fs!(WriteFileWindows, io_request); - - #[derive(thiserror::Error, Debug)] - pub(crate) enum WriteFileWindowsError { - #[error("WriteFileWindowsDeinitialized")] - WriteFileWindowsDeinitialized, - /// Delivering the result entered JS (settled the promise) and an exception is pending. - #[error("JSError")] - Js(jsc::JsError), - } - - impl From for WriteFileWindowsError { - fn from(err: jsc::JsError) -> Self { - WriteFileWindowsError::Js(err) - } - } - - /// `mkdirp` finished on the work pool: the hop back to the JS thread. Same pointer as the - /// write, its own tag. - #[repr(transparent)] - pub(crate) struct WriteFileWindowsMkdirp(WriteFileWindows); - - impl bun_event_loop::Taskable for WriteFileWindowsMkdirp { - const TAG: bun_event_loop::TaskTag = bun_event_loop::task_tag::WriteFileWindowsMkdirp; - /// Frees nothing: the write is not this task's. - unsafe fn release_unrun(_: *mut Self) {} - /// Enters no context. - unsafe fn context(_: *const Self) -> bun_event_loop::ContextId { - bun_event_loop::ContextId::NONE - } - } - - impl WriteFileWindowsMkdirp { - /// # Safety - /// `this` is the live `WriteFileWindows` `on_mkdirp_complete_concurrent` posted; - /// `on_mkdirp_complete` may free it. - pub(crate) unsafe fn run(this: *mut Self) { - // SAFETY: fn contract. - unsafe { WriteFileWindows::on_mkdirp_complete(this.cast::()) }; - } - } - - impl WriteFileWindows { - pub(crate) fn create_with_ctx( - file_blob: Blob, - bytes_blob: Blob, - event_loop: *mut EventLoop, - script_context: &bun_jsc::ScriptExecutionContext, - on_write_file_context: *mut c_void, - on_complete_callback: WriteFileOnWriteFileCallback, - mkdirp_if_not_exists: bool, - ) -> Result<*mut WriteFileWindows, WriteFileWindowsError> { - let mkdirp = mkdirp_if_not_exists - && file_blob - .store - .get() - .as_ref() - .unwrap() - .data - .as_file() - .pathlike - .is_path(); - let write_file = Self::new(WriteFileWindows { - file_blob, - bytes_blob, - on_complete_ctx: on_write_file_context, - on_complete_callback, - context: script_context.id(), - mkdirp_if_not_exists: mkdirp, - io_request: bun_core::ffi::zeroed::(), - uv_bufs: [uv::uv_buf_t { - base: null_mut(), - len: 0, - }], - event_loop, - fd: -1, - err: None, - total_written: 0, - poll_ref: KeepAlive::default(), - owned_fd: false, - }); - // SAFETY: just allocated, sole owner until returned. - // `open`/`do_write_loop` may free `*write_file` on the `Err` path, - // so we operate through the raw `write_file` pointer rather than - // holding a `&mut` across those calls (Stacked Borrows: a `&mut` - // local would dangle once `deinit` reclaims the Box). - unsafe { - (*write_file).io_request.loop_ = (*event_loop).uv_loop(); - (*write_file).io_request.data = write_file.cast::(); - - match &(*write_file) - .file_blob - .store - .get() - .as_ref() - .unwrap() - .data - .as_file() - .pathlike - { - PathOrFileDescriptor::Path(_) => { - Self::open(write_file)?; - } - PathOrFileDescriptor::Fd(fd) => { - (*write_file).fd = 'brk: { - // `EventLoop.virtual_machine` is `Option>`; - // `RareData::std{out,err,in}_store` is type-erased - // `Option>` — compare on raw pointer identity. - if let Some(vm) = (*event_loop).virtual_machine { - if let Some(rare) = (*vm.as_ptr()).rare_data.as_ref() { - let store_ptr = (*write_file) - .file_blob - .store - .get() - .as_ref() - .unwrap() - .as_ptr() - .cast::(); - if rare.stdout_store.map(|p| p.as_ptr()) == Some(store_ptr) { - break 'brk 1; - } else if rare.stderr_store.map(|p| p.as_ptr()) - == Some(store_ptr) - { - break 'brk 2; - } else if rare.stdin_store.map(|p| p.as_ptr()) - == Some(store_ptr) - { - break 'brk 0; - } - } - } - - // The file stored descriptor is not stdin, stdout, or stderr. - fd.uv() - }; - - Self::do_write_loop(write_file, (*write_file).loop_())?; - } - } - - (*write_file) - .poll_ref - .ref_(jsc::VirtualMachineRef::event_loop_ctx( - (*(*write_file).event_loop) - .virtual_machine - .unwrap() - .as_ptr(), - )); - } - Ok(write_file) - } - - #[inline] - pub(crate) fn loop_(&self) -> *mut uv::Loop { - // SAFETY: event_loop is the VM-owned EventLoop with process lifetime. - unsafe { (*self.event_loop).uv_loop() } - } - - /// # Safety - /// `this` must point to a live `WriteFileWindows` allocated via [`Self::new`]. - /// On `Err` return, `*this` has been freed (via [`Self::throw`] → [`Self::deinit`]) - /// and must not be accessed again. - pub(crate) unsafe fn open(this: *mut Self) -> Result<(), WriteFileWindowsError> { - // SAFETY: caller contract — `this` is live. - unsafe { (*this).io_request.data = this.cast::() }; - // SAFETY: caller contract — `this` is live; the borrow is released - // before any path that may free `*this`. - let path = unsafe { &(*this).file_blob } - .store - .get() - .as_ref() - .unwrap() - .data - .as_file() - .pathlike - .path() - .slice(); - let posix_path = match sys::to_posix_path(path) { - Ok(p) => p, - Err(_) => { - // SAFETY: caller contract — `this` is live; `throw` consumes it. - return Err(unsafe { - Self::throw( - this, - sys::Error { - errno: sys::E::NAMETOOLONG as _, - syscall: sys::Tag::open, - ..Default::default() - }, - ) - }); - } - }; - // SAFETY: (*this).io_request is a valid uv_fs_t embedded in a Box-allocated WriteFileWindows; - // (*this).loop_() is the VM's libuv loop which outlives this request; posix_path is NUL-terminated. - let rc = unsafe { - uv::uv_fs_open( - (*this).loop_(), - &mut (*this).io_request, - posix_path.as_ptr(), - uv::O::CREAT - | uv::O::WRONLY - | uv::O::NOCTTY - | uv::O::NONBLOCK - | uv::O::SEQUENTIAL - | uv::O::TRUNC, - 0o644, - Some(Self::on_open), - ) - }; - - // libuv always returns 0 when a callback is specified - if let Some(err) = rc.to_error(sys::Tag::open) { - debug_assert!(err.get_errno() != sys::E::NOENT); - // SAFETY: caller contract — `this` is live; `throw` consumes it. - return Err(unsafe { Self::throw(this, err.with_path(path)) }); - } else { - // SAFETY: caller contract — `this` is live on the Ok path. - unsafe { (*this).owned_fd = true }; - } - Ok(()) - } - - pub(crate) extern "C" fn on_open(req: *mut uv::fs_t) { - // SAFETY: req points to WriteFileWindows.io_request. Kept as a raw - // pointer (NOT `&mut`) because the paths below may free `*this` - // (`throw`/`do_write_loop` → `deinit`), and a `&mut` argument/local - // would be invalidated by that deallocation (Stacked Borrows). - let this: *mut WriteFileWindows = unsafe { WriteFileWindows::from_uv_fs(req) }; - debug_assert!(core::ptr::eq( - this, - // SAFETY: req == &(*this).io_request; data was set to `this` in create_with_ctx/open. - unsafe { (*req).data }.cast::() - )); - // SAFETY: `this` is live (libuv invokes us with the req we registered). - let rc = unsafe { (*this).io_request.result }; - #[cfg(debug_assertions)] - bun_output::scoped_log!( - WriteFile, - "onOpen({}) = {}", - bstr::BStr::new( - // SAFETY: `this` is live. - unsafe { &(*this).file_blob } - .store - .get() - .as_ref() - .unwrap() - .data - .as_file() - .pathlike - .path() - .slice() - ), - rc - ); - - if let Some(err) = rc.errno() { - // SAFETY: `this` is live. - if err == sys::E::NOENT && unsafe { (*this).mkdirp_if_not_exists } { - // cleanup the request so we can reuse it later. - // SAFETY: req points to (*this).io_request (valid uv_fs_t); libuv permits cleanup - // between uses to reuse the same req struct. - unsafe { (*req).deinit() }; - - // attempt to create the directory on another thread - // SAFETY: `this` is live; `mkdirp` does not free `*this`. - unsafe { (*this).mkdirp() }; - return; - } - - // SAFETY: `this` is live; borrow released before `throw` consumes `*this`. - let path = unsafe { &(*this).file_blob } - .store - .get() - .as_ref() - .unwrap() - .data - .as_file() - .pathlike - .path() - .slice() - .into(); - // SAFETY: `this` is live; `throw` consumes it. - match unsafe { - Self::throw( - this, - sys::Error { - errno: err as _, - path, - syscall: sys::Tag::open, - ..Default::default() - }, - ) - } { - WriteFileWindowsError::WriteFileWindowsDeinitialized => {} - WriteFileWindowsError::Js(err) => crate::dispatch::fold(Err(err)), - } - return; - } - - // SAFETY: `this` is live. - unsafe { (*this).fd = i32::try_from(rc.int()).expect("int cast") }; - - // the loop must be copied - // SAFETY: `this` is live; on `Err`, `*this` has been freed and is not accessed again. - if let Err(e) = unsafe { Self::do_write_loop(this, (*this).loop_()) } { - match e { - WriteFileWindowsError::WriteFileWindowsDeinitialized => {} - WriteFileWindowsError::Js(err) => crate::dispatch::fold(Err(err)), - } - } - } - - fn mkdirp(&mut self) { - bun_output::scoped_log!(WriteFile, "mkdirp"); - self.mkdirp_if_not_exists = false; - - // Compute the raw self pointer first so the immutable borrow of - // `path` (into `self.file_blob.store`) does not conflict with the - // `&mut self` reborrow needed by `from_mut`. - let ctx = core::ptr::from_mut(self).cast::<()>(); - let path = self - .file_blob - .store - .get() - .as_ref() - .unwrap() - .data - .as_file() - .pathlike - .path() - .slice(); - crate::node::fs::async_::AsyncMkdirp::schedule(crate::node::fs::async_::AsyncMkdirp { - completion: Self::on_mkdirp_complete_concurrent, - completion_ctx: ctx, - // BORROW: AsyncMkdirp.path is `*const [u8]` (not owned); `path` - // points into `self.file_blob.store`, which outlives the mkdirp - // task (it's released only in `deinit()`). - path: bun_core::dirname(path) - // this shouldn't happen - .unwrap_or(path) as *const [u8], - ticket: bun_jsc::virtual_machine::VirtualMachine::get().ticket(), - task: Default::default(), - }); - } - - /// # Safety - /// `this` must point to a live `WriteFileWindows` allocated via [`Self::new`]. - /// `*this` may be freed by the time this returns (via `throw`/`open` → `deinit`). - unsafe fn on_mkdirp_complete(this: *mut Self) { - // SAFETY: caller contract — `this` is live. - let err = unsafe { (*this).err.take() }; - if let Some(err_) = err { - // `sys::Error.path` is an owned `Box<[u8]>` freed by its Drop; - // no explicit free needed. - // SAFETY: caller contract — `this` is live; `throw` consumes it. - match unsafe { Self::throw(this, err_) } { - WriteFileWindowsError::WriteFileWindowsDeinitialized => {} - WriteFileWindowsError::Js(err) => crate::dispatch::fold(Err(err)), - } - return; - } - - // SAFETY: caller contract — `this` is live; on `Err`, `*this` has been freed. - if let Err(e) = unsafe { Self::open(this) } { - match e { - WriteFileWindowsError::WriteFileWindowsDeinitialized => {} - WriteFileWindowsError::Js(err) => crate::dispatch::fold(Err(err)), - } - } - } - - fn on_mkdirp_complete_concurrent( - ctx: *mut (), - err_: bun_sys::Result<()>, - ticket: &bun_jsc::Ticket, - ) { - // SAFETY: `ctx` is the `*mut Self` stored in `AsyncMkdirp.completion_ctx` - // by `mkdirp` above; sole owner on this concurrent path. - let this = unsafe { bun_ptr::callback_ctx::(ctx.cast()) }; - bun_output::scoped_log!(WriteFile, "mkdirp complete"); - debug_assert!(this.err.is_none()); - this.err = match err_ { - bun_sys::Result::Err(e) => Some(e), - bun_sys::Result::Ok(()) => None, - }; - ticket.post(ConcurrentTask::create_from( - std::ptr::from_mut(this).cast::(), - )); - } - - extern "C" fn on_write_complete(req: *mut uv::fs_t) { - // SAFETY: req points to WriteFileWindows.io_request. Kept as a raw - // pointer (NOT `&mut`) because the paths below may free `*this` - // (`throw`/`do_write_loop` → `deinit`), and a `&mut` would be - // invalidated by that deallocation (Stacked Borrows). - let this: *mut WriteFileWindows = unsafe { WriteFileWindows::from_uv_fs(req) }; - debug_assert!(core::ptr::eq( - this, - // SAFETY: req == &(*this).io_request; data was set to `this` in do_write_loop. - unsafe { (*req).data }.cast::() - )); - // SAFETY: `this` is live (libuv invokes us with the req we registered). - let rc = unsafe { (*this).io_request.result }; - if let Some(err) = rc.to_error(sys::Tag::write) { - // SAFETY: `this` is live; `throw` consumes it. - match unsafe { Self::throw(this, err) } { - WriteFileWindowsError::WriteFileWindowsDeinitialized => {} - WriteFileWindowsError::Js(err) => crate::dispatch::fold(Err(err)), - } - return; - } - - // SAFETY: `this` is live. - unsafe { (*this).total_written += usize::try_from(rc.int()).expect("int cast") }; - // SAFETY: `this` is live; on `Err`, `*this` has been freed and is not accessed again. - if let Err(e) = unsafe { Self::do_write_loop(this, (*this).loop_()) } { - match e { - WriteFileWindowsError::WriteFileWindowsDeinitialized => {} - WriteFileWindowsError::Js(err) => crate::dispatch::fold(Err(err)), - } - } - } - - /// # Safety - /// `this` must point to a live `WriteFileWindows` allocated via [`Self::new`]. - /// On return, `*this` has been freed and must not be accessed again. - pub(crate) unsafe fn on_finish(this: *mut Self) -> WriteFileWindowsError { - // SAFETY: VM-owned EventLoop lives for process lifetime; the guard - // forms short-lived `&mut` only at the enter/exit call sites (see - // EventLoopEnterGuard docs) so it does not alias `*this`. - let _exit = unsafe { jsc::event_loop::EventLoop::enter_scope((*this).event_loop) }; - - // We don't need to enqueue task since this is already in a task. - // SAFETY: caller contract — `this` is live; consumed here. - unsafe { Self::run_from_js_thread(this) } - } - - /// # Safety - /// `this` must point to a live `WriteFileWindows` allocated via [`Self::new`]. - /// On return, `*this` has been freed and must not be accessed again. - pub(crate) unsafe fn run_from_js_thread(this: *mut Self) -> WriteFileWindowsError { - // SAFETY: caller contract — `this` is live; copy out everything we - // need before `deinit` frees the allocation. - let (cb, cb_ctx, context) = unsafe { - ( - (*this).on_complete_callback, - (*this).on_complete_ctx, - (*this).context, - ) - }; - let _context = bun_jsc::virtual_machine::VirtualMachine::get().enter_context(context); - - // SAFETY: caller contract — `this` is live. - if let Some(err) = unsafe { (*this).to_system_error() } { - // SAFETY: caller contract — `this` is live; consumed here. - unsafe { Self::deinit(this) }; - if let Err(e) = cb(cb_ctx, WriteFileResultType::Err(Box::new(err))) { - return e.into(); - } - } else { - // SAFETY: caller contract — `this` is live. - let wrote = unsafe { (*this).total_written }; - // SAFETY: caller contract — `this` is live; consumed here. - unsafe { Self::deinit(this) }; - if let Err(e) = cb(cb_ctx, WriteFileResultType::Result(wrote as SizeType)) { - return e.into(); - } - } - - WriteFileWindowsError::WriteFileWindowsDeinitialized - } - - /// # Safety - /// `this` must point to a live `WriteFileWindows` allocated via [`Self::new`]. - /// On return, `*this` has been freed and must not be accessed again. - pub(crate) unsafe fn throw(this: *mut Self, err: sys::Error) -> WriteFileWindowsError { - // SAFETY: caller contract — `this` is live. - unsafe { - debug_assert!((*this).err.is_none()); - (*this).err = Some(err); - Self::on_finish(this) - } - } - - pub(crate) fn to_system_error(&self) -> Option { - if let Some(err) = &self.err { - let mut sys_err = err.clone(); - sys_err = match &self - .file_blob - .store - .get() - .as_ref() - .unwrap() - .data - .as_file() - .pathlike - { - PathOrFileDescriptor::Path(path) => sys_err.with_path(path.slice()), - PathOrFileDescriptor::Fd(fd) => sys_err.with_fd(*fd), - }; - - return Some(sys_err.to_system_error().into()); - } - None - } - - /// # Safety - /// `this` must point to a live `WriteFileWindows` allocated via [`Self::new`]. - /// On `Err` return, `*this` has been freed (via `on_finish`/`throw` → `deinit`) - /// and must not be accessed again. On `Ok`, `*this` remains live. - pub(crate) unsafe fn do_write_loop( - this: *mut Self, - uv_loop: *mut uv::Loop, - ) -> Result<(), WriteFileWindowsError> { - // SAFETY: caller contract — `this` is live. - let remain_full = unsafe { (*this).bytes_blob.shared_view() }; - // SAFETY: caller contract — `this` is live. - let off = unsafe { (*this).total_written }.min(remain_full.len()); - let remain = &remain_full[off..]; - - // SAFETY: caller contract — `this` is live. - if remain.is_empty() || unsafe { (*this).err.is_some() } { - // SAFETY: caller contract — `this` is live; consumed here. - return Err(unsafe { Self::on_finish(this) }); - } - - // SAFETY: caller contract — `this` is live. - unsafe { - (*this).uv_bufs[0].base = remain.as_ptr().cast_mut(); - (*this).uv_bufs[0].len = remain.len() as u32; - } - - // SAFETY: (*this).io_request is a valid uv_fs_t embedded in this Box-allocated struct; - // cleanup is safe to call between uses of the same req. - unsafe { uv::uv_fs_req_cleanup(&mut (*this).io_request) }; - // SAFETY: uv_loop is the VM's libuv loop (outlives `*this`); io_request/uv_bufs are - // embedded in `*this` which stays alive until on_write_complete fires; fd is open. - let rc = unsafe { - uv::uv_fs_write( - uv_loop, - &mut (*this).io_request, - (*this).fd, - (*this).uv_bufs.as_mut_ptr(), - 1, - -1, - Some(Self::on_write_complete), - ) - }; - // SAFETY: caller contract — `this` is live. - unsafe { (*this).io_request.data = this.cast::() }; - if rc.int() == 0 { - // EINPROGRESS - return Ok(()); - } - - if let Some(err) = rc.to_error(sys::Tag::write) { - // SAFETY: caller contract — `this` is live; consumed here. - return Err(unsafe { Self::throw(this, err) }); - } - - if rc.int() != 0 { - bun_core::Output::panic(format_args!( - "unexpected return code from uv_fs_write: {}", - rc.int() - )); - } - Ok(()) - } - - pub(crate) fn new(init: WriteFileWindows) -> *mut WriteFileWindows { - bun_core::heap::into_raw(Box::new(init)) - } - - /// # Safety - /// `this` must be the unique live pointer to a `WriteFileWindows` - /// allocated via [`Self::new`]. Consumes the allocation; `*this` is - /// freed and must not be accessed after this returns. - /// - /// Takes a raw pointer (not `&mut self`) because reclaiming the `Box` - /// while a `&mut self` argument is on the stack is a Stacked Borrows - /// protector violation (deallocating memory a protected reference - /// points into is UB even if the reference is never used again). - pub(crate) unsafe fn deinit(this: *mut Self) { - // SAFETY: caller contract — `this` is live. - unsafe { - let fd = (*this).fd; - if fd > 0 && (*this).owned_fd { - aio::Closer::close(Fd::from_uv(fd), (*this).io_request.loop_); - } - (*this).poll_ref.disable(); - // (*this).io_request is a valid uv_fs_t embedded in this struct; uv_fs_req_cleanup - // is safe on a zeroed or previously-used req. - uv::uv_fs_req_cleanup(&mut (*this).io_request); - // `this` was allocated via Self::new (heap::into_raw); reclaim and drop here. - drop(bun_core::heap::take(this)); - } - } - - pub(crate) fn create( - event_loop: *mut EventLoop, - script_context: &bun_jsc::ScriptExecutionContext, - file_blob: Blob, - bytes_blob: Blob, - context: *mut C, - callback: WriteFileOnWriteFileCallback, - mkdirp_if_not_exists: bool, - ) -> Result<*mut WriteFileWindows, WriteFileWindowsError> { - // see `WriteFile::create` — caller supplies an erased - // `*mut c_void` callback directly; `context` is just `.cast()`ed. - WriteFileWindows::create_with_ctx( - file_blob, - bytes_blob, - event_loop, - script_context, - context.cast::(), - callback, - mkdirp_if_not_exists, - ) - } - } -} - // ────────────────────────────────────────────────────────────────────────── pub(crate) struct WriteFilePromise { @@ -1322,6 +651,7 @@ impl WriteFileWaitFromLockedValueTask { mkdirp_if_not_exists: Some(this.mkdirp_if_not_exists), ..Default::default() }, + None, ) { Ok(p) => p, Err(err) => { diff --git a/src/runtime/webcore/fetch.rs b/src/runtime/webcore/fetch.rs index b11678aada77..930185cda30a 100644 --- a/src/runtime/webcore/fetch.rs +++ b/src/runtime/webcore/fetch.rs @@ -1562,7 +1562,7 @@ fn fetch_impl( } else { bun_sys::O::RDONLY | bun_sys::O::NOCTTY }; - bun_sys::open(zpath, flags, 0) + bun_sys::open(zpath, flags, 0).map_err(|err| err.with_path(path.slice())) } } }; diff --git a/src/runtime/webcore/s3/client.rs b/src/runtime/webcore/s3/client.rs index fb26347f2d3a..350a812aaa51 100644 --- a/src/runtime/webcore/s3/client.rs +++ b/src/runtime/webcore/s3/client.rs @@ -488,10 +488,6 @@ pub(crate) fn writable_stream( } // `credentials` ref adopted by value — moved into the MultiPartUpload below. - // JSC_BORROW: `global_this` outlives the task (it owns the VM/heap that owns the JS - // objects which keep the task alive); stored via `GlobalRef` in the heap-allocated - // MultiPartUpload. - let global_static = GlobalRef::from(cx.global()); let task_ptr: *mut MultiPartUpload = bun_core::heap::into_raw(Box::new(MultiPartUpload { root: Cell::new(None), queue: JsCell::new(None), @@ -511,7 +507,6 @@ pub(crate) fn writable_stream( // outlives every MultiPartUpload (the VM owns the heap that owns the JS objects // keeping this task alive). Dereference to `&'static` for storage. vm: VirtualMachine::get(), - global_this: global_static, buffered: JsCell::new(StreamBuffer::default()), uploaded_bytes: Cell::new(0), path: Box::<[u8]>::from(path), @@ -930,7 +925,6 @@ pub(crate) fn upload_stream( // SAFETY (JSC_BORROW): VirtualMachine::get() returns the live per-thread VM; it // outlives every MultiPartUpload. Dereference to `&'static` for storage. vm: VirtualMachine::get(), - global_this: global_static, buffered: JsCell::new(StreamBuffer::default()), uploaded_bytes: Cell::new(0), path: Box::<[u8]>::from(path), diff --git a/src/runtime/webcore/s3/multipart.rs b/src/runtime/webcore/s3/multipart.rs index 3fbc3be70b2a..331d61f63531 100644 --- a/src/runtime/webcore/s3/multipart.rs +++ b/src/runtime/webcore/s3/multipart.rs @@ -100,8 +100,8 @@ use bun_collections::IntegerBitSet; use bun_core::{declare_scope, scoped_log}; use bun_io::KeepAlive; use bun_io::StreamBuffer; +use bun_jsc::JsCell; use bun_jsc::virtual_machine::VirtualMachine; -use bun_jsc::{GlobalRef, JsCell}; use bun_ptr::RefPtr; use bun_s3_signing::acl::ACL; use bun_s3_signing::credentials::S3Credentials; @@ -122,7 +122,7 @@ use bun_collections::index_sort; declare_scope!(S3MultiPartUpload, hidden); #[derive(bun_ptr::CellRefCounted)] -pub struct MultiPartUpload { +pub(crate) struct MultiPartUpload { pub(crate) root: Cell>>, pub(crate) queue: JsCell>>, pub(crate) available: Cell>, @@ -143,8 +143,6 @@ pub struct MultiPartUpload { /// The context of the script that started the upload: its requests are that script's. pub(crate) context: bun_jsc::ContextId, pub(crate) vm: &'static VirtualMachine, - // JSC_BORROW per LIFETIMES.tsv row 1886 — rust_type `&JSGlobalObject` used verbatim - pub global_this: GlobalRef, pub(crate) buffered: JsCell, /// Bytes accepted by `write*` (after encoding): what a streamed `Bun.write`/`writer.end()` diff --git a/src/runtime/webcore/s3/simple_request.rs b/src/runtime/webcore/s3/simple_request.rs index 8c7369111576..176b4170648d 100644 --- a/src/runtime/webcore/s3/simple_request.rs +++ b/src/runtime/webcore/s3/simple_request.rs @@ -56,7 +56,7 @@ pub(crate) enum S3DownloadResult<'a> { Failure(S3Error<'a>), } -pub enum S3UploadResult<'a> { +pub(crate) enum S3UploadResult<'a> { Success, /// failure error is not owned and need to be copied if used after this callback Failure(S3Error<'a>), diff --git a/src/runtime/webcore/streams.rs b/src/runtime/webcore/streams.rs index 63f86eed71cd..b2421db3b8de 100644 --- a/src/runtime/webcore/streams.rs +++ b/src/runtime/webcore/streams.rs @@ -436,8 +436,8 @@ impl WritablePending { global, context, } => { - // A sink settles a write from its own completion (a libuv write callback on - // Windows), not from a queued task: for the script that is writing. + // A sink settles a write from its own completion, not from a queued task: for + // the script that is writing. let entered = global.bun_vm().enter_context(context); Writable::fulfill_promise( core::mem::replace(&mut self.result, Writable::Done), @@ -995,7 +995,6 @@ pub enum SourceHandle { /// The `'static` bound erases the `&JSGlobalObject` borrow carried in /// `Subprocess<'a>`; the pointed-at allocation outlives this handle. Subprocess(BackRef>), - ShellWritable(BackRef), FetchResponseBody(BackRef), ServerRequestBody(crate::server::AnyRequestContext), S3DownloadBody(BackRef), @@ -1038,8 +1037,6 @@ impl SourceHandle { SourceHandle::ByteStream(p) => p.on_close(err), SourceHandle::FileReader(p) => p.on_close(err), SourceHandle::Subprocess(p) => p.on_close(err), - // SAFETY: live backref; cleared before the pointee is freed. - SourceHandle::ShellWritable(mut p) => unsafe { p.get_mut() }.on_close(err), SourceHandle::FetchResponseBody(p) => p.on_stream_cancelled(), SourceHandle::S3DownloadBody(p) => p.on_stream_cancelled(), SourceHandle::ServerRequestBody(_) => {} @@ -1104,7 +1101,7 @@ impl SourceHandle { p.on_cancel(); } // Remaining variants leave `on_ready` at the trait default (no-op). - SourceHandle::Subprocess(_) | SourceHandle::ShellWritable(_) => {} + SourceHandle::Subprocess(_) => {} } } @@ -1121,7 +1118,6 @@ impl SourceHandle { | SourceHandle::ByteStream(_) | SourceHandle::FileReader(_) | SourceHandle::Subprocess(_) - | SourceHandle::ShellWritable(_) | SourceHandle::HTMLRewriter(_) | SourceHandle::TestingCancelOnDrain(_) => {} } @@ -1138,7 +1134,6 @@ impl SourceHandle { | SourceHandle::ByteStream(_) | SourceHandle::FileReader(_) | SourceHandle::Subprocess(_) - | SourceHandle::ShellWritable(_) | SourceHandle::HTMLRewriter(_) | SourceHandle::TestingCancelOnDrain(_) => {} } @@ -1164,7 +1159,7 @@ pub(crate) enum HTTPServerWritableState { /// `HTTPSResponseSink`) wraps this; the response itself is dispatched at /// runtime through `uws::AnyResponse`, so one instantiation serves HTTP/1.1, /// HTTP/2 and HTTP/3 alike. -pub struct HTTPServerWritable { +pub(crate) struct HTTPServerWritable { pub(crate) res: Option, pub(crate) buffer: Vec, pub(crate) pooled_buffer: Option>, @@ -1246,7 +1241,7 @@ impl HTTPServerWritable { /// registration / pending-flush creation) and the VM-owned global outlives /// this sink (JSC_BORROW). Never `None` once initialized. #[inline] - pub fn global_this(&self) -> &JSGlobalObject { + pub(crate) fn global_this(&self) -> &JSGlobalObject { self.global_this .as_ref() .expect("HTTPServerWritable.global_this used before init") @@ -1768,7 +1763,7 @@ impl HTTPServerWritable { bun_sys::Result::Ok(self.park_pending_flush(cx.global())) } - pub fn flush(&mut self) -> bun_sys::Result<()> { + pub(crate) fn flush(&mut self) -> bun_sys::Result<()> { bun_core::scoped_log!(HTTPServerWritableLog, "flush()"); self.unregister_auto_flusher(); @@ -1784,7 +1779,7 @@ impl HTTPServerWritable { bun_sys::Result::Ok(()) } - pub fn write(&mut self, data: &StreamResult) -> Writable { + pub(crate) fn write(&mut self, data: &StreamResult) -> Writable { if self.is_done() || self.requested_end { return Writable::Owned(0); } @@ -2107,7 +2102,7 @@ impl HTTPServerWritable { /// This can be called _many_ times for the same instance /// so it must zero out state instead of make it - pub fn finalize(&mut self) { + pub(crate) fn finalize(&mut self) { bun_core::scoped_log!(HTTPServerWritableLog, "finalize()"); if !self.is_done() { self.unregister_auto_flusher(); @@ -2256,7 +2251,7 @@ pub(crate) type HTTPResponseSink = HTTPServerWritable; // NetworkSink // ────────────────────────────────────────────────────────────────────────── -pub struct NetworkSink { +pub(crate) struct NetworkSink { /// The sink's ref on the upload, released in `detach_writable`. pub task: Option>, pub(crate) source: SourceHandle, @@ -2308,7 +2303,7 @@ impl NetworkSink { /// Invariant: `global_this` is set at construction and the VM-owned global /// outlives this sink (JSC_BORROW). Never `None` once set. #[inline] - pub fn global_this(&self) -> &JSGlobalObject { + pub(crate) fn global_this(&self) -> &JSGlobalObject { self.global_this .as_ref() .expect("NetworkSink.global_this used before init") @@ -2334,7 +2329,7 @@ impl NetworkSink { bun_sys::Result::Ok(()) } - pub fn finalize(&mut self) { + pub(crate) fn finalize(&mut self) { self.detach_writable(); } @@ -2405,7 +2400,7 @@ impl NetworkSink { source.ready(None, None); } - pub fn flush(&mut self) -> bun_sys::Result<()> { + pub(crate) fn flush(&mut self) -> bun_sys::Result<()> { bun_sys::Result::Ok(()) } @@ -2457,7 +2452,7 @@ impl NetworkSink { Writable::Pending(core::ptr::from_mut(&mut self.pending)) } - pub fn write(&mut self, data: &StreamResult) -> Writable { + pub(crate) fn write(&mut self, data: &StreamResult) -> Writable { if self.ended { return Writable::Owned(0); } @@ -2659,7 +2654,7 @@ impl NetworkSink { bun_sys::Result::Ok(JSValue::js_number(0.0)) } - pub fn to_js(&mut self, global_this: &JSGlobalObject) -> JSValue { + pub(crate) fn to_js(&mut self, global_this: &JSGlobalObject) -> JSValue { NetworkSinkJSSink::create_object(global_this, self, 0) } diff --git a/src/runtime/webview/ChromeBackend.cpp b/src/runtime/webview/ChromeBackend.cpp index adea04d47792..acef0bf7499b 100644 --- a/src/runtime/webview/ChromeBackend.cpp +++ b/src/runtime/webview/ChromeBackend.cpp @@ -507,7 +507,7 @@ bool Transport::ensureConnected(Zig::GlobalObject* zig, const WTF::String& wsUrl void Transport::writeRaw(const char* data, size_t len) { #if OS(WINDOWS) - // libuv queues the chunks in order; m_txQueue/onWritable are unused. + // The pipe writer queues the chunks in order; m_txQueue/onWritable are unused. if (!m_dead) Bun__Chrome__writePipe(data, len); #else if (m_dead || !m_readSock) return; diff --git a/src/runtime/webview/ChromeBackend.h b/src/runtime/webview/ChromeBackend.h index 2cbc972c9d96..113909bcb78e 100644 --- a/src/runtime/webview/ChromeBackend.h +++ b/src/runtime/webview/ChromeBackend.h @@ -317,7 +317,7 @@ for (;;) { // writes go through the same fd via us_socket_write. Socketpair not two // pipes because usockets' bsd_recv calls recv() which fails ENOTSOCK on a // pipe — the error was misread as EOF and onClose fired before any data. -// Windows has no inheritable sockets: ChromeProcess.rs drives two libuv pipes instead and everything from onData on is shared. +// Windows has no inheritable sockets: ChromeProcess.rs drives two pipes instead and everything from onData on is shared. // // pending maps CDP id → {methodTag, slot selector, weak view}. Promises // live in the WriteBarrier slots on JSWebView (visitChildren marks them); diff --git a/src/runtime/webview/ChromeProcess.rs b/src/runtime/webview/ChromeProcess.rs index 0d3f1a729325..5e154d3703ea 100644 --- a/src/runtime/webview/ChromeProcess.rs +++ b/src/runtime/webview/ChromeProcess.rs @@ -16,7 +16,7 @@ //! here: recv(readFd) returned -1 → loop treated as close → onClose fired //! before any data); socketpair gives us a proper socket for the read path //! and the write path can share it. -//! Windows (no inheritable sockets): two uv_pipe()s driven here instead, see [`PipeEvent`] and `Bun__Chrome__writePipe`. +//! Windows (no inheritable sockets): two pipes driven here instead, see [`PipeEvent`] and `Bun__Chrome__writePipe`. use core::ffi::{CStr, c_char}; use core::ptr; @@ -28,22 +28,17 @@ use std::io::Write as _; #[cfg(any(target_os = "linux", target_os = "android"))] use bun_core::ZStr; use bun_core::{self, ZBox, env_var, getenv_z, strings, zstr}; +#[cfg(windows)] +use bun_io::windows::{Pipe, PipeOrigin, ReadEvent, Refusal}; use bun_jsc::JSGlobalObject; use bun_jsc::virtual_machine::VirtualMachine; -#[cfg(windows)] -use bun_libuv_sys::{UvHandle as _, UvStream as _}; use bun_output::{declare_scope, scoped_log}; use bun_paths::{self, path_buffer_pool, platform, resolve_path}; -#[cfg(unix)] use bun_spawn::SpawnResultExt as _; use bun_spawn::{ self, EventLoopHandle, Process, ProcessExit, ProcessExitKind, ProcessHandle, SpawnOptions, Status, Stdio, }; -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; use bun_sys::{self, Fd, FdExt as _}; use bun_which::which; @@ -59,14 +54,13 @@ pub(crate) struct ChromeProcess { generation: u32, } -/// Our ends of the two pipes (boxed, null once closed) and the read scratch. +/// Our ends of the two pipes; `None` once closed. #[cfg(windows)] struct WindowsPipes { /// Child reads the other end as fd 3. - cmd: *mut uv::Pipe, + cmd: Option, /// Child writes the other end as fd 4. - reply: *mut uv::Pipe, - read_buf: Box<[u8]>, + reply: Option, } // PORTING.md §Global mutable state: JS-thread-only singleton ptr → AtomicPtr. @@ -178,7 +172,7 @@ bun_spawn::link_impl_ProcessExit! { } impl ChromeProcess { - /// Safety: `this` is the pointer published in INSTANCE (freed here); `process` is the exit callback's own argument, which carries the `&mut Process` already live in its frame (as in `SyncWindowsProcess::on_process_exit`). + /// Safety: `this` is the pointer published in INSTANCE (freed here); `process` is the exit callback's own argument, which carries the `&mut Process` already live in its frame. unsafe fn on_exit(this: *mut ChromeProcess, process: *mut Process, status: &Status) { scoped_log!(Chrome, "chrome exited: {}", status); // A retired Chrome was already unpublished by `Bun__Chrome__retire`. @@ -590,11 +584,6 @@ fn spawn( }, extra_fds: endpoints.child_fds(), // dup2'd to child fd 3 and 4, in order argv0: Some(chrome.as_ptr()), - #[cfg(windows)] - windows: bun_spawn::WindowsOptions { - loop_: event_loop, - ..Default::default() - }, ..SpawnOptions::default() }; @@ -602,8 +591,6 @@ fn spawn( // argv[0] non-null; valid for this call. let spawned = unsafe { bun_spawn::spawn_process(&opts, argv.as_ptr(), env.as_ptr().cast()) }??; - #[cfg(windows)] - let mut spawned = spawned; // Keeping our copies of the child's ends would mask Chrome's death (no EOF). endpoints.close_child_ends(); @@ -715,78 +702,74 @@ impl Drop for Endpoints { // --- Windows transport: two pipes ----------------------------------------- -#[cfg(windows)] -const READ_BUF_SIZE: usize = 64 * 1024; - #[cfg(windows)] struct Endpoints { /// The child's ends; `None` once closed. cmd_child: Option, reply_child: Option, - /// Our ends; null once handed over. - cmd: *mut uv::Pipe, - reply: *mut uv::Pipe, + /// Our ends; `None` once handed over. + cmd: Option, + reply: Option, } #[cfg(windows)] impl Endpoints { fn create(event_loop: EventLoopHandle) -> crate::Result { - // uv_pipe() returns [read end, write end]; UV_NONBLOCK_PIPE (overlapped) goes on our ends only. - let mut cmd_fds: [uv::uv_file; 2] = [0; 2]; - // SAFETY: FFI; `cmd_fds` is the out-array uv_pipe fills. - unsafe { uv::uv_pipe(&raw mut cmd_fds, 0, uv::UV_NONBLOCK_PIPE as i32) } - .to_result(bun_sys::Tag::uv_pipe)?; + use bun_spawn_sys::windows::stdio::ChildPipe; let mut endpoints = Endpoints { - cmd_child: Some(Fd::from_uv(cmd_fds[0])), + cmd_child: None, reply_child: None, - cmd: ptr::null_mut(), - reply: ptr::null_mut(), + cmd: None, + reply: None, }; - let cmd_parent = Fd::from_uv(cmd_fds[1]); - if let Err(err) = endpoints.wrap(event_loop, cmd_parent, |e, pipe| e.cmd = pipe) { - cmd_parent.close(); - return Err(err); - } - - let mut reply_fds: [uv::uv_file; 2] = [0; 2]; - // SAFETY: FFI; `reply_fds` is the out-array uv_pipe fills. - unsafe { uv::uv_pipe(&raw mut reply_fds, uv::UV_NONBLOCK_PIPE as i32, 0) } - .to_result(bun_sys::Tag::uv_pipe)?; - endpoints.reply_child = Some(Fd::from_uv(reply_fds[1])); - let reply_parent = Fd::from_uv(reply_fds[0]); - if let Err(err) = endpoints.wrap(event_loop, reply_parent, |e, pipe| e.reply = pipe) { - reply_parent.close(); - return Err(err); - } + let (child, ours) = Self::pipe( + event_loop, + ChildPipe { + readable: true, + writable: false, + overlapped: false, + }, + )?; + endpoints.cmd_child = Some(child); + endpoints.cmd = Some(ours); + let (child, ours) = Self::pipe( + event_loop, + ChildPipe { + readable: false, + writable: true, + overlapped: false, + }, + )?; + endpoints.reply_child = Some(child); + endpoints.reply = Some(ours); Ok(endpoints) } - /// On success libuv owns `fd`; on failure the caller still does. - fn wrap( - &mut self, + /// A pipe whose overlapped end is ours, driven by the loop, and whose + /// other end is a synchronous one for Chrome, which reads and writes its + /// fds with blocking calls. + fn pipe( event_loop: EventLoopHandle, - fd: Fd, - store: impl FnOnce(&mut Endpoints, *mut uv::Pipe), - ) -> crate::Result<()> { - let pipe: *mut uv::Pipe = bun_core::heap::into_raw(bun_core::boxed_zeroed::()); - // SAFETY: `pipe` is a live Box; `close_and_destroy` frees it in any state. - let result = unsafe { - (*pipe) - .init(event_loop.uv_loop(), false) - .to_result(bun_sys::Tag::uv_pipe) - .and_then(|()| (*pipe).open(fd.uv()).to_result(bun_sys::Tag::open)) - }; - if let Err(err) = result { - // SAFETY: see above. - unsafe { uv::Pipe::close_and_destroy(pipe) }; - return Err(err.into()); + child: bun_spawn_sys::windows::stdio::ChildPipe, + ) -> crate::Result<(Fd, Pipe)> { + use bun_spawn_sys::windows::{stdio, win32}; + let pair = stdio::create_pipe_pair(child) + .map_err(|code| win32::sys_error(code, bun_sys::Tag::pipe))?; + let child = Fd::from_system(pair.child); + let ours = Fd::from_system(pair.parent); + match Pipe::open(event_loop.r#loop(), ours, PipeOrigin::Created, true) { + Ok(pipe) => { + // Pending commands keep the loop alive (Transport::updateKeepAlive), + // not the pipes. + pipe.unref(); + Ok((child, pipe)) + } + Err(err) => { + ours.close(); + child.close(); + Err(err.into()) + } } - // Pending commands keep the loop alive (Transport::updateKeepAlive), not - // the pipes. - // SAFETY: `pipe` is initialized. - unsafe { (*pipe).unref() }; - store(self, pipe); - Ok(()) } fn child_fds(&self) -> Box<[Stdio]> { @@ -809,11 +792,9 @@ impl Endpoints { /// Moves our ends into a [`ChromeProcess`], starts reading replies, and publishes it. fn attach(mut self, process: ProcessHandle) -> crate::Result { let pipes = WindowsPipes { - cmd: core::mem::replace(&mut self.cmd, ptr::null_mut()), - reply: core::mem::replace(&mut self.reply, ptr::null_mut()), - read_buf: vec![0u8; READ_BUF_SIZE].into_boxed_slice(), + cmd: self.cmd.take(), + reply: self.reply.take(), }; - let reply = pipes.reply; let generation = GENERATION.load(Ordering::Relaxed).wrapping_add(1); let self_ptr = bun_core::heap::into_raw(Box::new(ChromeProcess { process, @@ -826,12 +807,15 @@ impl Endpoints { // Unlike POSIX the exit can't be delivered before we return (it comes // through this thread's loop), so the exit handler is installed after. - // SAFETY: `reply` and `process` are owned by `*self_ptr`, which - // outlives the reads (`WindowsPipes::close` stops them first). + // SAFETY: the reply pipe and `process` are owned by `*self_ptr`, which + // outlives the reads (`WindowsPipes::close` ends them first). let started = unsafe { - (*reply) - .read_start_ctx::(self_ptr) - .to_result(bun_sys::Tag::listen) + (*self_ptr) + .pipes + .reply + .as_mut() + .expect("endpoints live") + .read_start(self_ptr, ChromeProcess::on_reply) .and_then(|()| (*process).watch()) }; if let Err(err) = started { @@ -862,59 +846,54 @@ impl Endpoints { impl Drop for Endpoints { fn drop(&mut self) { self.close_child_ends(); - for pipe in [self.cmd, self.reply] { - if !pipe.is_null() { - // SAFETY: still ours; `attach` nulls the fields it takes over. - unsafe { uv::Pipe::close_and_destroy(pipe) }; - } - } } } #[cfg(windows)] impl WindowsPipes { - /// Idempotent; in-flight writes complete with UV_ECANCELED and free themselves. + /// Idempotent. Writes still in flight are cancelled and report to nobody. fn close(&mut self) { - let reply = core::mem::replace(&mut self.reply, ptr::null_mut()); - if !reply.is_null() { - // SAFETY: the live Box from `attach`; ownership ends here. - unsafe { - (*reply).read_stop(); - uv::Pipe::close_and_destroy(reply); - } - } - let cmd = core::mem::replace(&mut self.cmd, ptr::null_mut()); - if !cmd.is_null() { - // SAFETY: as above. - unsafe { uv::Pipe::close_and_destroy(cmd) }; - } + self.reply = None; + self.cmd = None; } } #[cfg(windows)] -impl uv::StreamReader for ChromeProcess { - fn on_read_alloc(this: &mut Self, _suggested_size: usize) -> &mut [u8] { - &mut this.pipes.read_buf - } - - fn on_read_error(this: &mut Self, err: core::ffi::c_int) { - scoped_log!( - Chrome, - "reply pipe closed: {:?}", - bun_sys::windows::translate_uv_error_to_e(err) - ); - PipeEvent::Closed.post(this.generation); +impl ChromeProcess { + /// # Safety + /// `this` is the `ChromeProcess` that owns the reply pipe. + unsafe fn on_reply(this: *mut ChromeProcess, event: ReadEvent<'_>) { + // SAFETY: caller contract. + let generation = unsafe { (*this).generation }; + match event { + ReadEvent::Data(bytes) => { + scoped_log!(Chrome, "read {} bytes", bytes.len()); + PipeEvent::Data(Box::from(bytes.as_slice())).post(generation); + } + ReadEvent::Eof | ReadEvent::EndOfWrite => { + scoped_log!(Chrome, "reply pipe closed"); + PipeEvent::Closed.post(generation); + } + ReadEvent::Err(err) => { + scoped_log!(Chrome, "reply pipe closed: {}", err); + PipeEvent::Closed.post(generation); + } + } } - unsafe fn on_read(this: *mut Self, data: &[u8]) { - scoped_log!(Chrome, "read {} bytes", data.len()); - // SAFETY: `this` is live for the duration of the callback. - let generation = unsafe { (*this).generation }; - PipeEvent::Data(Box::from(data)).post(generation); + /// # Safety + /// `this` is the `ChromeProcess` that owns the command pipe; a pipe only + /// reports while its owner has it open. + unsafe fn on_command_written(this: *mut ChromeProcess, result: bun_sys::Result) { + if let Err(err) = result { + scoped_log!(Chrome, "command pipe write failed: {}", err); + // SAFETY: caller contract. + PipeEvent::Closed.post(unsafe { (*this).generation }); + } } } -/// What the reply pipe produced, handed to C++ from an event-loop task rather than from the libuv read callback. +/// What the reply pipe produced, handed to C++ from an event-loop task rather than from the pipe's read callback. #[cfg(windows)] enum PipeEvent { Data(Box<[u8]>), @@ -931,7 +910,7 @@ pub(crate) struct QueuedEvent { #[cfg(windows)] impl PipeEvent { fn post(self, generation: u32) { - // Not dispatched from the read callback: C++ runs JS that may spin a nested event loop (bun:test does), and libuv re-arms the read only after the callback returns. + // Not dispatched from the read callback: C++ runs JS that may spin a nested event loop (bun:test does), and the pipe issues its next read only after the callback returns. VirtualMachine::get() .as_mut() .enqueue_task(bun_jsc::Task::from_boxed(Box::new(QueuedEvent { @@ -979,55 +958,6 @@ impl QueuedEvent { } } -/// One in-flight `uv_write` and the copy of the chunk `buf` points into. -#[cfg(windows)] -struct WriteReq { - req: uv::uv_write_t, - buf: uv::uv_buf_t, - bytes: Box<[u8]>, - generation: u32, -} - -#[cfg(windows)] -impl WriteReq { - /// Safety: `pipe` is the live command pipe of the Chrome with `generation`. - unsafe fn submit(pipe: *mut uv::Pipe, chunk: &[u8], generation: u32) -> bool { - let mut req = Box::new(WriteReq { - req: bun_core::ffi::zeroed::(), - buf: uv::uv_buf_t::init(b""), // re-init below, once `bytes` has stopped moving - bytes: Box::from(chunk), - generation, - }); - req.buf = uv::uv_buf_t::init(&req.bytes); - let req = bun_core::heap::into_raw(req); - // SAFETY: caller contract; `req` stays put until `on_write` reclaims it. - let rc = unsafe { - (*req) - .req - .write((*pipe).as_stream(), &(*req).buf, req, WriteReq::on_write) - }; - if let Some(err) = rc.to_error(bun_sys::Tag::write) { - scoped_log!(Chrome, "uv_write failed: {}", err); - // SAFETY: libuv did not take `req`, so the callback will not run. - unsafe { bun_core::heap::destroy(req) }; - return false; - } - true - } - - fn on_write(this: *mut WriteReq, status: uv::ReturnCode) { - // SAFETY: the Box leaked by `submit`; libuv hands it back exactly once. - let req = unsafe { bun_core::heap::take(this) }; - if let Some(err) = status.to_error(bun_sys::Tag::write) { - scoped_log!(Chrome, "command pipe write failed: {}", err); - // ECANCELED is `WindowsPipes::close` draining the queue; the death is already being reported. - if status.int() != uv::UV_ECANCELED { - PipeEvent::Closed.post(req.generation); - } - } - } -} - /// Transport::writeRaw on Windows. A write that fails surfaces as a Closed event, like any other loss of the transport. Safety: `data` points to `len` readable bytes. #[cfg(windows)] #[unsafe(no_mangle)] @@ -1036,22 +966,30 @@ unsafe extern "C" fn Bun__Chrome__writePipe(data: *const u8, len: usize) { if instance.is_null() { return; // Chrome already exited; the Exited event is on its way to C++ } - // SAFETY: INSTANCE is live until `on_exit` clears it; raw field reads, so - // nothing aliases the `&mut` the read callbacks form. - let (pipe, generation) = unsafe { ((*instance).pipes.cmd, (*instance).generation) }; - debug_assert!( - !pipe.is_null(), - "pipes are only closed after INSTANCE is cleared" - ); scoped_log!(Chrome, "write {} bytes", len); // SAFETY: caller contract. let bytes = unsafe { bun_core::ffi::slice(data, len) }; - for chunk in bytes.chunks(u32::MAX as usize) { - // SAFETY: `pipe` belongs to the published instance read above. - if !unsafe { WriteReq::submit(pipe, chunk, generation) } { - PipeEvent::Closed.post(generation); - return; + // SAFETY: INSTANCE is live until `on_exit` clears it, and no pipe callback + // (the only other code that touches it) is on the stack: they post tasks + // instead of calling into C++. + let chrome = unsafe { &mut *instance }; + let generation = chrome.generation; + let submitted = match chrome.pipes.cmd.as_mut() { + Some(pipe) => pipe + .write_owned( + bytes.to_vec(), + Refusal::Returned, + instance, + ChromeProcess::on_command_written, + ) + .is_ok(), + None => { + debug_assert!(false, "pipes are only closed after INSTANCE is cleared"); + false } + }; + if !submitted { + PipeEvent::Closed.post(generation); } } diff --git a/src/runtime/webview/WebKitBackend.cpp b/src/runtime/webview/WebKitBackend.cpp index d3d075c7ab2f..51ab2ceaed69 100644 --- a/src/runtime/webview/WebKitBackend.cpp +++ b/src/runtime/webview/WebKitBackend.cpp @@ -115,10 +115,9 @@ static constexpr us_socket_vtable_t s_hostVTable = { .on_handshake = nullptr, }; -// us_socket_ref/unref are no-ops on kqueue, and us_poll_start_rc doesn't -// touch loop.active. Track our own ref against view count. A view with -// pending ops keeps itself alive via visitChildren → promise → reaction -// → closure → view, so "any views" covers "any pending". +// us_poll_start_rc doesn't touch loop.active. Track our own ref against +// view count. A view with pending ops keeps itself alive via visitChildren +// → promise → reaction → closure → view, so "any views" covers "any pending". void HostClient::updateKeepAlive() { bool want = !viewsById.empty(); diff --git a/src/shell_parser/parse.rs b/src/shell_parser/parse.rs index 7a29d7d3b5a4..8e5b8381f6f9 100644 --- a/src/shell_parser/parse.rs +++ b/src/shell_parser/parse.rs @@ -497,33 +497,8 @@ pub mod ast { pub fn to_flags(self) -> i32 { // `bun_shell_parser` is sys-tier-free so it cannot depend on - // `bun_sys::O`; mirror those constants here. On POSIX `bun.O.*` is - // `libc::O_*`. On Windows `bun.O.*` is the *Linux-shaped octal* - // values — NOT MSVCRT `_O_*` — because - // `bun_sys::open` → `sys_uv::open` → `uv::O::from_bun_o` bit-tests - // against those exact values. Using `libc::O_CREAT` (0x100) / - // `libc::O_APPEND` (0x8) on Windows silently dropped CREAT/APPEND - // through `from_bun_o`, so `> file` failed to create the target. - #[cfg(not(windows))] - const O_RDONLY: i32 = libc::O_RDONLY; - #[cfg(not(windows))] - const O_WRONLY: i32 = libc::O_WRONLY; - #[cfg(not(windows))] - const O_CREAT: i32 = libc::O_CREAT; - #[cfg(not(windows))] - const O_TRUNC: i32 = libc::O_TRUNC; - #[cfg(not(windows))] - const O_APPEND: i32 = libc::O_APPEND; - #[cfg(windows)] - const O_RDONLY: i32 = 0o0; - #[cfg(windows)] - const O_WRONLY: i32 = 0o1; - #[cfg(windows)] - const O_CREAT: i32 = 0o100; - #[cfg(windows)] - const O_TRUNC: i32 = 0o1000; - #[cfg(windows)] - const O_APPEND: i32 = 0o2000; + // `bun_sys::O`, whose values these are. + use libc::{O_APPEND, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY}; let read_write_flags: i32 = if self.stdin() { O_RDONLY diff --git a/src/spawn/Cargo.toml b/src/spawn/Cargo.toml index 588f1e18d15f..fc8bd9ec01ce 100644 --- a/src/spawn/Cargo.toml +++ b/src/spawn/Cargo.toml @@ -13,10 +13,8 @@ workspace = true thiserror.workspace = true bun_alloc.workspace = true bun_errno.workspace = true -bun_opaque.workspace = true libc.workspace = true scopeguard.workspace = true -bun_analytics.workspace = true bun_dispatch.workspace = true bun_core.workspace = true bun_crash_handler.workspace = true @@ -27,3 +25,6 @@ bun_ptr.workspace = true bun_spawn_sys.workspace = true bun_sys.workspace = true bun_threading.workspace = true + +[target.'cfg(windows)'.dependencies] +bun_uws_sys.workspace = true diff --git a/src/spawn/ctrl_c.rs b/src/spawn/ctrl_c.rs index 2161124fd435..01cc2845bacd 100644 --- a/src/spawn/ctrl_c.rs +++ b/src/spawn/ctrl_c.rs @@ -97,7 +97,7 @@ pub fn child_died_of_it(status: &Status) -> bool { #[cfg(unix)] return status.signal_code() == Some(bun_core::SignalCode::SIGINT); #[cfg(windows)] - return matches!(status, Status::Exited(e) if e.raw == bun_sys::windows::STATUS_CONTROL_C_EXIT); + return matches!(status, Status::Exited(e) if e.is_ctrl_c_exit()); } /// End this process the way a child killed by Ctrl+C ended. diff --git a/src/spawn/lib.rs b/src/spawn/lib.rs index 70eda17189d9..c35d623b9f53 100644 --- a/src/spawn/lib.rs +++ b/src/spawn/lib.rs @@ -4,9 +4,9 @@ //! `bun_runtime` (cycle: `bun_runtime → bun_install`/`bun_jsc`). //! //! LAYERING: this crate **owns** the spawn implementation (not just data -//! shapes). `Process`, `Poller`, `WaiterThread`, `spawn_process`, and -//! `sync::spawn` were MOVED DOWN here from `bun_runtime::api::bun::process`; -//! `bun_runtime` re-exports them. The only non-leaf dependencies are +//! shapes): `Process`, `Poller`, `WaiterThread`, `spawn_process`, and +//! `sync::spawn`. `bun_runtime::api::bun::process` re-exports them. The only +//! non-leaf dependencies are //! `bun_io` (`FilePoll`/`KeepAlive`/`EventLoopCtx`), `bun_ptr` //! (`ThreadSafeRefCount`), `bun_io` (`BufferedWriter`), `bun_event_loop`, //! `bun_threading`, and `bun_crash_handler` — none of which depend back on @@ -43,7 +43,7 @@ pub use bun_spawn_sys::{Argv, CStrPtr, Envp, ffi}; pub use bun_spawn_sys::RusageFields; pub use process::{ Dup2, Exited, ExtraPipe, PidT, Poller, Process, ProcessHandle, Rusage, SpawnEnv, SpawnOptions, - SpawnProcessResult, SpawnResultExt, Status, StdioKind, spawn_process, spawn_process_cstr, + SpawnResult, SpawnResultExt, Status, Stdio, StdioKind, spawn_process, spawn_process_cstr, }; // Variant types live in `bun_runtime`/`bun_install`; each provides its body @@ -63,7 +63,6 @@ bun_dispatch::link_interface! { CronRemove, ChromeProcess, HostProcess, - SyncWindows, ] { fn on_process_exit(process: &mut Process, status: Status, rusage: &Rusage); } @@ -72,57 +71,22 @@ bun_dispatch::link_interface! { /// `None` = no handler set (the default for `Process::exit_handler`). pub type ProcessExitHandler = Option; -// In-crate `link_impl_*!` calls must be textually after the `link_interface!` -// that emits the macro (`#[macro_export]` is path-addressable from *other* -// crates only; same-crate use is textual-scope). POSIX `spawn_sync` waits -// inline and never installs a handler, so the `SyncWindows` arm is genuinely -// unreachable there — but every variant needs a body or the link fails. +/// The force-waiter-thread flag; only Linux honours it. +pub use bun_spawn_sys::waiter_thread_flag; #[cfg(windows)] -link_impl_ProcessExit! { - SyncWindows for process::sync::SyncWindowsProcess => |this| { - on_process_exit(process, status, rusage) => - process::sync::SyncWindowsProcess::on_process_exit(this, process, status, &*rusage), - } -} -#[cfg(not(windows))] -link_impl_ProcessExit! { - SyncWindows for process::SyncProcessPosix => |_this| { - on_process_exit(_process, _status, _rusage) => - unreachable!("SyncWindows exit handler is Windows-only"), - } -} +pub use process::WindowsOptions; #[cfg(unix)] -pub use process::{PosixSpawnOptions, PosixSpawnResult, PosixStdio as Stdio, WaitPidResult}; -#[cfg(unix)] -pub type SpawnResult = process::PosixSpawnResult; - -#[cfg(windows)] -pub use process::{ - WindowsOptions, WindowsSpawnOptions, WindowsSpawnResult, WindowsStdio as Stdio, - WindowsStdioResult as SpawnedStdio, -}; -#[cfg(windows)] -pub type SpawnResult = process::WindowsSpawnResult; -#[cfg(windows)] -pub mod windows { - /// `bun.windows.libuv.Pipe` raw pointer payload of `Stdio::Buffer` / - /// `Stdio::Ipc`. Erased so this crate stays libuv-agnostic at the type - /// surface; `bun_runtime` casts it back on consumption. - pub type UvPipePtr = *mut bun_sys::windows::libuv::Pipe; -} +pub use process::{WaitPidResult, WaiterThread}; /// Blocking (synchronous) spawn helpers. pub mod sync { - #[cfg(windows)] - pub use crate::process::WindowsOptions; pub use crate::process::sync::{Options, Result, SyncStdio as Stdio, spawn, spawn_with_argv}; } // ────────────────────────────────────────────────────────────────────────── -// `bun.jsc.Subprocess` cross-tier shapes — `Source`, `StdioResult`, -// `StaticPipeWriter

`. +// `bun.jsc.Subprocess` cross-tier shapes — `Source`, `StaticPipeWriter

`. // -// MOVE_DOWN from `bun_runtime::api::bun::subprocess`: `bun_install:: +// They live below `bun_runtime::api::bun::subprocess` because `bun_install:: // security_scanner` constructs a `StaticPipeWriter` to // stream a JSON blob to the scanner's stdin. The `Source` enum here carries a // `Box` arm (§Dispatch cold path — vtable travels with the @@ -130,24 +94,9 @@ pub mod sync { // crate naming `bun_jsc`/`bun_runtime`. // ────────────────────────────────────────────────────────────────────────── pub mod subprocess { - #[cfg(not(windows))] - use bun_sys::Fd; - pub use crate::process::StdioKind; pub use crate::static_pipe_writer::{StaticPipeWriter, StaticPipeWriterProcess}; - /// On POSIX this is `Option`; on Windows it is the `WindowsStdioResult` union. - #[cfg(not(windows))] - pub type StdioResult = Option; - #[cfg(windows)] - pub type StdioResult = crate::process::WindowsStdioResult; - - #[cfg(not(windows))] - #[inline] - pub fn stdio_result_from_fd(fd: Fd) -> StdioResult { - Some(fd) - } - /// The in-memory payload that a /// `StaticPipeWriter` drains into the child's stdin/extra-fd. /// diff --git a/src/spawn/process.rs b/src/spawn/process.rs index 1de6cc81d2c4..32fb455b62be 100644 --- a/src/spawn/process.rs +++ b/src/spawn/process.rs @@ -1,28 +1,25 @@ -#[cfg(any(windows, target_os = "macos"))] +#[cfg(target_os = "macos")] use core::ffi::c_void; use core::ffi::{c_char, c_int}; #[cfg(unix)] use core::sync::atomic::AtomicU32; +#[cfg(unix)] use core::sync::atomic::Ordering; -// (std::sync::Arc removed — Process is intrusively ref-counted via -// bun_ptr::ThreadSafeRefCount; see SyncWindowsProcess below.) #[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))] use bun_core::Global; +#[cfg(unix)] use bun_core::Output; use bun_event_loop::EventLoopHandle; #[cfg(unix)] -use bun_io::ParentDeathWatchdog; +use bun_io::FilePoll; +use bun_io::KeepAlive; #[cfg(unix)] -use bun_io::{FilePoll, KeepAlive}; +use bun_io::ParentDeathWatchdog; use bun_ptr::RefPtr; -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; use bun_sys::{self, Fd, Maybe}; #[cfg(windows)] -use uv::{UvHandle as _, UvStream as _}; +use bun_uws_sys::iocp; // posix_spawn(2) wrappers — owned by the `bun_spawn_sys` leaf crate. #[cfg(unix)] @@ -32,10 +29,8 @@ use bun_spawn_sys::posix_spawn::posix_spawn; #[cfg(unix)] pub use posix_spawn::WaitPidResult; -/// Low-level fd / memfd helpers historically grouped here as `spawn_sys`. -/// MOVE_DOWN: real impls now live in `bun_sys` (lower crate); re-export so -/// higher-tier callers (`bun_runtime::api::bun_spawn::stdio`, `Terminal`) -/// keep their `bun_spawn::process::spawn_sys::*` import path. +/// The fd / memfd helpers of `bun_sys` that spawning uses, under the path +/// `bun_runtime::api::bun_spawn::stdio` and `Terminal` import them from. pub mod spawn_sys { // memfd is Linux; FD_CLOEXEC is POSIX (Win32 handles are non-inheritable unless asked). #[cfg(unix)] @@ -47,17 +42,16 @@ pub mod spawn_sys { bun_core::declare_scope!(PROCESS, visible); // ─── Re-exports from `bun_spawn_sys` ───────────────────────────────────────── -// The raw OS spawn layer (option/result structs, `Rusage`, `spawn_process_posix`) -// moved into the leaf `bun_spawn_sys` crate so it has no event-loop dependency. -// Re-export here so existing `bun_spawn::process::*` paths keep resolving. +// The raw OS spawn layer (option/result structs, `Rusage`, `spawn_process_posix`, +// `spawn_process_windows`) lives in the leaf `bun_spawn_sys` crate so it has no +// event-loop dependency. Re-export here so `bun_spawn::process::*` paths resolve. #[cfg(any(target_os = "linux", target_os = "android"))] pub use bun_spawn_sys::PidFdType; -pub use bun_spawn_sys::spawn_process::rusage_zeroed; #[cfg(windows)] -pub use bun_spawn_sys::uv_getrusage; +pub use bun_spawn_sys::WindowsOptions; +pub use bun_spawn_sys::spawn_process::rusage_zeroed; pub use bun_spawn_sys::{ - Argv, CStrPtr, Dup2, Envp, ExtraPipe, PidT, PosixSpawnOptions, PosixSpawnResult, PosixStdio, - Rusage, StdioKind, + Argv, CStrPtr, Dup2, Envp, ExtraPipe, PidT, Rusage, SpawnOptions, SpawnResult, Stdio, StdioKind, }; /// Whether the process-exit poll should be registered one-shot. @@ -89,13 +83,6 @@ const PROCESS_POLL_ONE_SHOT: bool = !cfg!(any(target_os = "linux", target_os = " pub use crate::{ProcessExit, ProcessExitHandler, ProcessExitKind}; -// `opaque_ffi!` emits an inherent `impl` that doesn't carry inner `#[cfg]` -// attrs, so gate the whole macro invocation rather than the struct alone. -#[cfg(not(windows))] -bun_opaque::opaque_ffi! { - pub struct SyncProcessPosix; -} - #[inline] fn call_exit_handler( h: &ProcessExitHandler, @@ -110,16 +97,21 @@ fn call_exit_handler( h.on_process_exit(process, status, rusage); } -// bun.ptr.ThreadSafeRefCount → intrusive (FFI-crossing: *mut Process recovered -// via `container_of` in on_exit_uv / on_close_uv). Per PORTING.md §Pointers, -// keep the embedded count; the derive emits `ThreadSafeRefCounted` + -// `AnyRefCounted`. Default `destructor` (`heap::take`) applies — `Drop` below -// handles `poller.deinit()`. +// The count is intrusive and atomic: a raw `*mut Process` travels through the +// event loop's poll/wait callbacks and the waiter thread. The derive's default +// destructor frees the box (`heap::take`), which runs `Drop` below. #[derive(bun_ptr::ThreadSafeRefCounted)] pub struct Process { pub pid: PidT, #[cfg(any(target_os = "linux", target_os = "android"))] pub(crate) pidfd: PidFdType, + /// Open until `close()`; while it is, the pid cannot be reused. + #[cfg(windows)] + pub(crate) process_handle: bun_sys::windows::HANDLE, + /// The signal `kill()` ended the process with. Windows has no notion of + /// one, so this is what makes the exit report as signaled. + #[cfg(windows)] + pub(crate) exit_signal: u8, pub status: Status, pub poller: Poller, pub(crate) ref_count: bun_ptr::ThreadSafeRefCount, @@ -132,10 +124,10 @@ pub struct Process { } impl Drop for Process { - /// The allocation itself is freed by the `heap::take` in `destructor` - /// above; this `Drop` body covers the `poller.deinit()` call. fn drop(&mut self) { self.poller.deinit(); + #[cfg(windows)] + self.close_process_handle(); } } @@ -241,7 +233,7 @@ impl Process { /// the destructor `Box::from_raw`-drops the allocation, and a `&mut self` /// argument carries a Stacked-Borrows protector for the call's full /// duration — freeing while it's live is UB even though we never touch - /// `self` afterwards. Same rationale as [`Process::has_exited`] (:215). + /// `self` afterwards. /// /// # Safety /// `this` must point at a live `Process` with refcount ≥ 1. @@ -255,7 +247,7 @@ impl Process { /// for FilePoll/KeepAlive calls; reconstitutes the aio-level ctx here. #[inline] fn event_loop_ctx(&self) -> bun_io::EventLoopCtx { - event_loop_handle_to_ctx(self.event_loop) + self.event_loop.as_event_loop_ctx() } } @@ -264,27 +256,31 @@ pub fn event_loop_handle_to_ctx(handle: EventLoopHandle) -> bun_io::EventLoopCtx handle.as_event_loop_ctx() } -// ─── posix_spawn / FilePoll / uv-backed Process methods ────────────────────── +// ─── spawn-result / exit-watch Process methods ─────────────────────────────── impl Process { - #[cfg(unix)] - pub(crate) fn init_posix( - posix: &PosixSpawnResult, - event_loop: EventLoopHandle, - ) -> *mut Process { + /// Heap-allocates the `Process` for a spawned child with its initial ref. + pub(crate) fn init(spawned: &mut SpawnResult, event_loop: EventLoopHandle) -> *mut Process { + #[cfg(unix)] let status = 'brk: { - if posix.has_exited { + if spawned.has_exited { let mut rusage = rusage_zeroed(); - let waitpid_result = posix_spawn::wait4(posix.pid, 0, Some(&mut rusage)); - break 'brk Status::from(posix.pid, &waitpid_result).unwrap_or(Status::Running); + let waitpid_result = posix_spawn::wait4(spawned.pid, 0, Some(&mut rusage)); + break 'brk Status::from(spawned.pid, &waitpid_result).unwrap_or(Status::Running); } Status::Running }; - // bun.new → heap::alloc (pointer crosses FFI / intrusive refcount) + #[cfg(windows)] + let status = Status::Running; bun_core::heap::into_raw(Box::new(Process { ref_count: bun_ptr::ThreadSafeRefCount::init(), - pid: posix.pid, + pid: spawned.pid, #[cfg(any(target_os = "linux", target_os = "android"))] - pidfd: posix.pidfd.unwrap_or(0), + pidfd: spawned.pidfd.unwrap_or(0), + #[cfg(windows)] + process_handle: spawned.process_handle.take(), + #[cfg(windows)] + exit_signal: 0, + #[cfg(unix)] js_poster: event_loop.js_poster(), event_loop, poller: Poller::Detached, @@ -293,10 +289,32 @@ impl Process { })) } - // has_exited / has_killed live in the always-on impl above. + /// The process handle; invalid once the process has exited or `close()` ran. + #[cfg(windows)] + #[inline] + pub fn process_handle(&self) -> bun_sys::windows::HANDLE { + self.process_handle + } + + /// Resource usage so far of a process that has not exited yet. + #[cfg(windows)] + pub fn rusage(&self) -> Option { + if self.process_handle == bun_sys::windows::INVALID_HANDLE_VALUE { + return None; + } + Some(bun_spawn_sys::process_rusage(self.process_handle)) + } + + #[cfg(windows)] + fn close_process_handle(&mut self) { + if self.process_handle != bun_sys::windows::INVALID_HANDLE_VALUE { + // SAFETY: the handle is ours and nothing waits on it any more. + unsafe { bun_spawn_sys::windows::win32::CloseHandle(self.process_handle) }; + self.process_handle = bun_sys::windows::INVALID_HANDLE_VALUE; + } + } pub fn on_exit(&mut self, status: Status, rusage: &Rusage) { - // ProcessExitHandler is Copy (owner ptr + &'static vtable), so mirror let exit_handler = self.exit_handler; self.status = status.clone(); if self.has_exited() { @@ -305,29 +323,55 @@ impl Process { call_exit_handler(&exit_handler, self, status, rusage); } - #[cfg(unix)] - pub(crate) fn wait_posix(&mut self, sync_: bool) { - let mut rusage = rusage_zeroed(); - let waitpid_result = posix_spawn::wait4( - self.pid, - if sync_ { 0 } else { libc::WNOHANG as u32 }, - Some(&mut rusage), - ); - self.on_wait_pid(&waitpid_result, &rusage); + /// The process handle is signalled: report how the process ended. + #[cfg(windows)] + fn on_process_signaled(&mut self) { + use bun_spawn_sys::windows::win32; + let rusage = bun_spawn_sys::process_rusage(self.process_handle); + let mut exit_code: win32::DWORD = 0; + let status = if win32::GetExitCodeProcess(self.process_handle, &mut exit_code) == 0 { + Status::Err(win32::last_error(bun_sys::Tag::waitpid)) + } else if self.exit_signal != 0 { + Status::Signaled(self.exit_signal) + } else { + Status::Exited(Exited { + code: exit_code, + signal: 0, + }) + }; + bun_core::scoped_log!(PROCESS, "Process.onExit({}) {}", self.pid, status); + self.on_exit(status, &rusage); } pub fn wait(&mut self, sync_: bool) { #[cfg(unix)] - self.wait_posix(sync_); + { + let mut rusage = rusage_zeroed(); + let waitpid_result = posix_spawn::wait4( + self.pid, + if sync_ { 0 } else { libc::WNOHANG as u32 }, + Some(&mut rusage), + ); + self.on_wait_pid(&waitpid_result, &rusage); + } #[cfg(windows)] - let _ = sync_; + { + use bun_spawn_sys::windows::win32; + if self.has_exited() || self.process_handle == bun_sys::windows::INVALID_HANDLE_VALUE { + return; + } + let timeout = if sync_ { win32::INFINITE } else { 0 }; + if win32::WaitForSingleObject(self.process_handle, timeout) == win32::WAIT_OBJECT_0 { + self.on_process_signaled(); + } + } } /// # Safety /// `this` carries the +1 ref taken when the waiter-thread task was queued. /// `RefPtr::from_raw` releases it on return — which may free `this` — so /// this takes `*mut Self`, not `&mut self` (a `&mut` argument's - /// Stacked-Borrows protector outliving the allocation is UB; see :215). + /// Stacked-Borrows protector outliving the allocation is UB). #[cfg(unix)] pub(crate) unsafe fn on_wait_pid_from_waiter_thread( this: *mut Self, @@ -339,7 +383,7 @@ impl Process { // SAFETY: `_guard` keeps `this` live; `&mut` scoped to the poller unref. unsafe { if let Poller::WaiterThread(waiter) = &mut (*this).poller { - let ctx = event_loop_handle_to_ctx((*this).event_loop); + let ctx = (*this).event_loop.as_event_loop_ctx(); waiter.unref(ctx); (*this).poller = Poller::Detached; } @@ -424,9 +468,63 @@ impl Process { pub fn watch(&mut self) -> bun_sys::Result<()> { #[cfg(windows)] { - if let Poller::Uv(p) = &mut self.poller { - p.ref_(); + let ctx = self.event_loop_ctx(); + if let Poller::Wait(_, keep_alive) = &mut self.poller { + keep_alive.ref_(ctx); + return Ok(()); + } + if self.process_handle == bun_sys::windows::INVALID_HANDLE_VALUE { + return Err(bun_sys::Error::from_code( + bun_sys::E::ESRCH, + bun_sys::Tag::waitpid, + )); } + + let loop_ = self.event_loop.loop_(); + // SAFETY: `loop_` is the live loop of this thread. + let wait = unsafe { iocp::us_iocp_wait_create(loop_) }; + if wait.is_null() { + return Err(bun_sys::Error::from_code( + bun_sys::E::ENOMEM, + bun_sys::Tag::waitpid, + )); + } + let exit_wait = bun_core::heap::into_raw(Box::new(ExitWait { + op: iocp::Op::new(ExitWait::on_packet), + wait, + process: std::ptr::from_mut::(self), + loop_, + prev: core::ptr::null_mut(), + next: core::ptr::null_mut(), + })); + // SAFETY: `exit_wait` was just allocated and stays at this address + // until its packet is dequeued or the wait is stopped. An already + // signalled handle queues the packet all the same. + if unsafe { + iocp::us_iocp_wait_start(wait, self.process_handle, &raw mut (*exit_wait).op) + } != 0 + { + // SAFETY: the wait never started; nothing else refers to either. + unsafe { + iocp::us_iocp_wait_free(wait); + drop(bun_core::heap::take(exit_wait)); + } + return Err(bun_sys::Error::from_code( + bun_sys::E::EINVAL, + bun_sys::Tag::waitpid, + )); + } + + // SAFETY: `exit_wait` is live, at its final address and not listed. + unsafe { ExitWait::link(exit_wait) }; + let mut keep_alive = KeepAlive::default(); + keep_alive.ref_(ctx); + self.poller = Poller::Wait( + core::ptr::NonNull::new(exit_wait).expect("heap::into_raw is non-null"), + keep_alive, + ); + // Owned by `exit_wait` for as long as its `process` is set. + self.ref_(); return Ok(()); } @@ -529,94 +627,10 @@ impl Process { } } - #[cfg(windows)] - extern "C" fn on_exit_uv(process: *mut uv::uv_process_t, exit_status: i64, term_signal: c_int) { - // A Rust default-repr `enum` has no - // stable variant-payload offset, so the back-pointer is stored in - // `uv_process_t.data` (set in `spawn_process_windows` immediately - // after the handle is zeroed). - // - // Read everything needed from `*process` BEFORE creating - // `this: &mut Process`. The handle is the inline `Poller::Uv` field, - // so once `this` exclusively borrows the whole `Process`, any later - // `&mut *process` (or raw read via `process`) overlaps that borrow - // and pops `this`'s Unique tag under Stacked Borrows — the - // subsequent `this.close()` (which touches `self.poller`) would then - // use an invalidated tag. - // SAFETY: libuv passes the live handle; only reads its POD fields. - let rusage = uv_getrusage(unsafe { &mut *process }); - // SAFETY: raw read of POD `pid` field on the live handle. - let _pid = unsafe { (*process).pid }; - // SAFETY: `data` was set to the owning `*mut Process` before - // `uv_spawn`; libuv never overwrites it. `process` is not - // dereferenced again after this point. - let this: &mut Process = unsafe { bun_ptr::callback_ctx::((*process).data) }; - let exit_code: u8 = if exit_status >= 0 { - (exit_status as u64) as u8 - } else { - 0 - }; - let signal_code: Option = u8::try_from(term_signal).ok().filter(|&signal| signal != 0); - - bun_sys::windows::libuv::log!( - "Process.onExit({}) code: {}, signal: {:?}", - _pid, - exit_code, - signal_code - ); - - if let Some(sig) = signal_code { - this.close(); - this.on_exit(Status::Signaled(sig), &rusage); - } else if exit_status >= 0 { - // The check is on the signed libuv `exit_status`, so a negative - // `-UV_E*` reaches the Err arm. - this.close(); - this.on_exit( - Status::Exited(Exited { - code: exit_code, - signal: 0, - raw: exit_status as u32, - }), - &rusage, - ); - } else { - this.on_exit( - Status::Err(bun_sys::Error::from_code( - bun_sys::windows::translate_uv_error_to_e(exit_status as c_int), - bun_sys::Tag::waitpid, - )), - &rusage, - ); - } - } - - #[cfg(windows)] - extern "C" fn on_close_uv(uv_handle: *mut uv::uv_process_t) { - // SAFETY: read POD `pid` first — `uv_handle` points at the inline - // `Poller::Uv` payload inside `*this` (see `on_exit_uv`). - let _pid = unsafe { (*uv_handle).pid }; - // SAFETY: `*mut Process` back-pointer stashed in `data` at spawn. Stay - // raw — `RefPtr::drop` may free the allocation, so never bind a - // `&mut Process` whose tag would have to outlive that. - let this: *mut Process = unsafe { (*uv_handle).data.cast() }; - // SAFETY: adopts the +1 ref taken at `uv_spawn`. - let _guard = unsafe { RefPtr::from_raw(this) }; - bun_sys::windows::libuv::log!("Process.onClose({})", _pid); - // SAFETY: `_guard` keeps `this` live for this block. - unsafe { - if matches!((*this).poller, Poller::Uv(_)) { - (*this).poller = Poller::Detached; - } - } - } - pub fn close(&mut self) { #[cfg(unix)] { let mut stranded_watch_ref = false; - // Route the `Fd` arm through the centralized `fd_poll_mut()` - // accessor instead of open-coding `(*poll.as_ptr()).deinit()`. if let Some(poll) = self.poller.fd_poll_mut() { stranded_watch_ref = poll.is_registered(); poll.deinit(); @@ -631,21 +645,29 @@ impl Process { } #[cfg(windows)] { - // Hoist the libuv handle state into locals so the `&self.poller` - // borrow ends before we need `&mut self` for `ref_()` / - // `self.poller = …`. No raw-pointer round-trip needed. - let (closed, closing) = match &self.poller { - Poller::Uv(process) => (process.is_closed(), process.is_closing()), - _ => return, - }; - if closed { - self.poller = Poller::Detached; - } else if !closing { - self.ref_(); - if let Poller::Uv(process) = &mut self.poller { - process.close(Self::on_close_uv); + if let Poller::Wait(exit_wait, mut keep_alive) = + core::mem::replace(&mut self.poller, Poller::Detached) + { + keep_alive.unref(self.event_loop.as_event_loop_ctx()); + let exit_wait = exit_wait.as_ptr(); + // SAFETY: `exit_wait` is live while the poller holds it. Once the + // wait is stopped nothing refers to it; otherwise its packet is + // already on its way and it stays allocated for `on_packet`, + // which frees it. + unsafe { + ExitWait::unlink(exit_wait); + if iocp::us_iocp_wait_stop((*exit_wait).wait) != 0 { + iocp::us_iocp_wait_free((*exit_wait).wait); + drop(bun_core::heap::take(exit_wait)); + } else { + (*exit_wait).process = core::ptr::null_mut(); + } } + // SAFETY: the watch's ref; callers hold their own +1, so this + // never drops to zero. + unsafe { Self::deref(std::ptr::from_mut(self)) }; } + self.close_process_handle(); } #[cfg(any(target_os = "linux", target_os = "android"))] @@ -683,10 +705,7 @@ impl Process { // before `watch_or_reap()` installs the poller; the first // `recv_non_block` returns EAGAIN (yes hasn't written yet) so the // maxBuffer overflow fires from the event-loop poll - // tick *after* the Fd poller is armed. Do not widen this match to - // mask spawn-maxbuf.test.ts — the async-path hang there has a - // different root cause (poller is already Fd when `on_max_buffer` - // fires, so this arm is unreachable on that path). + // tick *after* the Fd poller is armed. match &self.poller { Poller::WaiterThread(_) | Poller::Fd(_) => { // All by-value `pid_t`/`c_int`; the kernel validates pid/ @@ -711,20 +730,25 @@ impl Process { } #[cfg(windows)] { - match &mut self.poller { - Poller::Uv(handle) => { - if let Some(err) = handle - .kill(c_int::from(signal)) - .to_error(bun_sys::Tag::kill) - { - // if the process was already killed don't throw - if err.errno != bun_sys::E::ESRCH as u16 { - return Err(err); - } - } - return Ok(()); - } - _ => {} + if self.has_exited() || self.process_handle == bun_sys::windows::INVALID_HANDLE_VALUE { + return Ok(()); + } + use bun_spawn_sys::windows::kill::{SIGKILL, kill}; + let mut signal = signal; + let mut result = kill(self.process_handle, c_int::from(signal)); + // A signal Windows cannot send still ends a child, as in Node: a + // `killSignal` is all a timeout or an abort has to end it with. + if matches!(&result, Err(err) if err.get_errno() == bun_sys::E::ENOSYS) { + signal = SIGKILL as u8; + result = kill(self.process_handle, SIGKILL); + } + match result { + // Signal 0 only probes: it ends nothing, so it is not how the process ended. + Ok(()) if signal == 0 => {} + Ok(()) => self.exit_signal = signal, + // if the process was already killed don't throw + Err(err) if err.get_errno() == bun_sys::E::ESRCH => {} + Err(err) => return Err(err), } } @@ -746,13 +770,23 @@ pub enum Status { #[derive(Clone, Copy, Default)] pub struct Exited { - pub code: u8, + /// At most 255 on POSIX. On Windows any `GetExitCodeProcess` DWORD, among + /// them the NTSTATUS of a crash (0xC0000409). + pub code: u32, /// The platform's signal number, or `0` for none; see `Status::signal`. pub signal: u8, - /// Untruncated `GetExitCodeProcess` DWORD; `code` is its low byte. - /// NTSTATUS crash codes only survive here (0xC0000409 → `code` 9). - #[cfg(windows)] - pub raw: u32, +} + +impl Exited { + /// Ended by the default Ctrl+C handler (`STATUS_CONTROL_C_EXIT`). Never on + /// POSIX, where that is a `SIGINT` death. + #[inline] + pub fn is_ctrl_c_exit(self) -> bool { + #[cfg(windows)] + return self.code == bun_sys::windows::STATUS_CONTROL_C_EXIT; + #[cfg(not(windows))] + false + } } impl Status { @@ -762,7 +796,7 @@ impl Status { #[cfg(unix)] pub(crate) fn from(pid: PidT, waitpid_result: &Maybe) -> Option { - let mut exit_code: Option = None; + let mut exit_code: Option = None; let mut signal: Option = None; match waitpid_result { @@ -778,7 +812,7 @@ impl Status { let status = result.status as c_int; if libc::WIFEXITED(status) { - exit_code = Some(libc::WEXITSTATUS(status) as u8); + exit_code = Some(libc::WEXITSTATUS(status) as u32); // True if the process terminated due to receipt of a signal. } @@ -839,36 +873,42 @@ impl core::fmt::Display for Status { } } -#[cfg(unix)] -pub enum PollerPosix { +pub enum Poller { /// Hive-allocated `bun_io::FilePoll` slot. Pointer (not `Box`) because the /// poll lives in `Store`; freed via `FilePoll::deinit`, /// never via Rust `drop`. + #[cfg(unix)] Fd(core::ptr::NonNull), + #[cfg(unix)] WaiterThread(KeepAlive), + /// Watching the process handle through the loop's completion port. + #[cfg(windows)] + Wait(core::ptr::NonNull, KeepAlive), Detached, } -#[cfg(unix)] -impl PollerPosix { +impl Poller { /// NOT `impl Drop`: this enum is reassigned freely (`self.poller = /// Poller::Detached`, `Poller::WaiterThread(..)`, etc.) and `close()` /// already performs the same teardown explicitly before reassigning. A /// `Drop` impl would double-free the hive slot on those reassignments. - /// Called only from `Process` drop. + /// Called only from `Process` drop. A Windows watch holds a ref on the + /// `Process`, so none is left by then. pub(crate) fn deinit(&mut self) { - // Route the `Fd` arm through the centralized `fd_poll_mut()` accessor - // instead of open-coding the `NonNull` deref here. + #[cfg(unix)] if let Some(poll) = self.fd_poll_mut() { poll.deinit(); - } else if let PollerPosix::WaiterThread(w) = self { + } else if let Poller::WaiterThread(w) = self { w.disable(); } + #[cfg(windows)] + debug_assert!(matches!(self, Poller::Detached)); } + #[cfg(unix)] fn into_fd(self) -> Option> { match self { - PollerPosix::Fd(f) => Some(f), + Poller::Fd(f) => Some(f), _ => None, } } @@ -880,6 +920,7 @@ impl PollerPosix { /// is the `NonNull` inside this enum, so `&mut self` ⇒ the returned /// `&mut FilePoll` is the only live reference to the slot /// (event-loop-thread exclusive). + #[cfg(unix)] #[inline] fn fd_poll_mut(&mut self) -> Option<&mut FilePoll> { match self { @@ -887,66 +928,167 @@ impl PollerPosix { // only via `deinit` (which consumes the variant). `&mut self` ⇒ // exclusive access to the unique handle ⇒ exclusive access to // the hive slot. - PollerPosix::Fd(poll) => Some(unsafe { poll.as_mut() }), + Poller::Fd(poll) => Some(unsafe { poll.as_mut() }), + _ => None, + } + } + + /// The `KeepAlive` a poller without a `FilePoll` carries. + fn keep_alive_mut(&mut self) -> Option<&mut KeepAlive> { + match self { + #[cfg(unix)] + Poller::WaiterThread(keep_alive) => Some(keep_alive), + #[cfg(windows)] + Poller::Wait(_, keep_alive) => Some(keep_alive), _ => None, } } pub(crate) fn enable_keeping_event_loop_alive(&mut self, ctx: bun_io::EventLoopCtx) { + #[cfg(unix)] if let Some(poll) = self.fd_poll_mut() { poll.enable_keeping_process_alive(ctx); - } else if let PollerPosix::WaiterThread(waiter) = self { - waiter.ref_(ctx); + return; + } + if let Some(keep_alive) = self.keep_alive_mut() { + keep_alive.ref_(ctx); } } pub(crate) fn disable_keeping_event_loop_alive(&mut self, ctx: bun_io::EventLoopCtx) { + #[cfg(unix)] if let Some(poll) = self.fd_poll_mut() { poll.disable_keeping_process_alive(ctx); - } else if let PollerPosix::WaiterThread(waiter) = self { - waiter.unref(ctx); + return; + } + if let Some(keep_alive) = self.keep_alive_mut() { + keep_alive.unref(ctx); } } } -#[cfg(unix)] -pub type Poller = PollerPosix; +/// The op a process-exit wait completes with. Heap-allocated on its own: the +/// loop owns it from `us_iocp_wait_start` until its packet is dequeued or +/// `us_iocp_wait_stop` reports the wait removed, whatever happens to the +/// `Process` meanwhile. +#[cfg(windows)] +#[repr(C)] +pub struct ExitWait { + op: iocp::Op, + wait: *mut iocp::Wait, + /// Holds one ref while set; null once the `Process` closed the watch with + /// the packet already in flight. + process: *mut Process, + loop_: *mut bun_uws_sys::Loop, + /// [`EXIT_WAITS`] links; listed exactly while `process` is set. + prev: *mut ExitWait, + next: *mut ExitWait, +} + #[cfg(windows)] -pub type Poller = PollerWindows; +thread_local! { + /// The exit waits of this thread that still report to a `Process`, so a + /// loop that is about to be freed can end them ([`close_all_for_loop`]). + static EXIT_WAITS: core::cell::Cell<*mut ExitWait> = + const { core::cell::Cell::new(core::ptr::null_mut()) }; +} +/// `us_loop_free` starts with this: what is still open on `loop_` caches its +/// pointer and completes through it, so each is closed, which cancels its +/// operations, and the loop then collects those before it goes. #[cfg(windows)] -pub enum PollerWindows { - Uv(uv::uv_process_t), - Detached, +#[unsafe(no_mangle)] +pub extern "C" fn Bun__closeAllForLoop(loop_: *mut bun_uws_sys::Loop) { + bun_io::windows::close_all_for_loop(loop_); + close_all_for_loop(loop_); } +/// Stop watching every process whose exit wait is on `loop_`, which this +/// thread is about to free (a Worker's): [`Process::close`] for each. Their +/// exit handlers never run and the children are left running, as when a POSIX +/// loop goes away with process polls still registered. #[cfg(windows)] -impl PollerWindows { - /// Not `Drop` — see `PollerPosix::deinit`. - pub(crate) fn deinit(&mut self) { - if let PollerWindows::Uv(p) = self { - debug_assert!(p.is_closed()); +fn close_all_for_loop(loop_: *mut bun_uws_sys::Loop) { + let mut cursor = EXIT_WAITS.get(); + while !cursor.is_null() { + // SAFETY: listed waits are live and their `process` is set; `close` + // unlinks `cursor` and nothing else, so the next pointer is read + // first. The extra ref keeps the `Process` allocated across `close` + // releasing the watch's. + unsafe { + let next = (*cursor).next; + if (*cursor).loop_ == loop_ { + let process = (*cursor).process; + (*process).ref_(); + (*process).close(); + Process::deref(process); + } + cursor = next; } } +} - pub(crate) fn enable_keeping_event_loop_alive(&mut self, _event_loop: bun_io::EventLoopCtx) { - match self { - PollerWindows::Uv(process) => { - process.ref_(); +#[cfg(windows)] +impl ExitWait { + /// # Safety + /// `this` is live, at its final address and not listed. + unsafe fn link(this: *mut ExitWait) { + let head = EXIT_WAITS.get(); + // SAFETY: caller contract; `head` is a listed (live) wait or null. + unsafe { + (*this).next = head; + if !head.is_null() { + (*head).prev = this; } - _ => {} } + EXIT_WAITS.set(this); } - pub(crate) fn disable_keeping_event_loop_alive(&mut self, _event_loop: bun_io::EventLoopCtx) { - // uv_unref() drops this handle from loop->active_handles. us_loop_pump - // forces a non-blocking uv_run iteration regardless, so the - // wait-thread's IOCP exit packet is still dequeued and on_exit_uv fires. - match self { - PollerWindows::Uv(p) => { - p.unref(); + /// # Safety + /// `this` is live and listed, on the thread that listed it. + unsafe fn unlink(this: *mut ExitWait) { + // SAFETY: caller contract; neighbours are listed (live) waits. + unsafe { + let (prev, next) = ((*this).prev, (*this).next); + if prev.is_null() { + EXIT_WAITS.set(next); + } else { + (*prev).next = next; + } + if !next.is_null() { + (*next).prev = prev; } - _ => {} + } + } + + unsafe extern "C" fn on_packet(_loop: *mut bun_uws_sys::Loop, op: *mut iocp::Op) { + // SAFETY: `op` is the first field of the `ExitWait` allocated in + // `Process::watch`; with its packet dequeued the loop is done with it. + let process = unsafe { + let this = op.cast::(); + let process = (*this).process; + if !process.is_null() { + ExitWait::unlink(this); + } + iocp::us_iocp_wait_free((*this).wait); + drop(bun_core::heap::take(this)); + process + }; + if process.is_null() { + return; + } + // SAFETY: adopts the ref `watch()` took. Stay raw — dropping the guard + // may free the allocation. + let _guard = unsafe { RefPtr::from_raw(process) }; + // SAFETY: `_guard` keeps `process` live; `&mut` scoped to each statement + // (`on_process_signaled` can run the exit handler). + unsafe { + if let Poller::Wait(_, mut keep_alive) = + core::mem::replace(&mut (*process).poller, Poller::Detached) + { + keep_alive.unref((*process).event_loop.as_event_loop_ctx()); + } + (*process).on_process_signaled(); } } } @@ -1304,11 +1446,6 @@ pub mod waiter_thread_posix { } impl WaiterThreadPosix { - #[inline] - pub fn set_should_use_waiter_thread() { - bun_spawn_sys::waiter_thread_flag::set(); - } - #[inline] pub(crate) fn should_use_waiter_thread() -> bool { bun_spawn_sys::waiter_thread_flag::get() @@ -1348,7 +1485,10 @@ pub mod waiter_thread_posix { let act = libc::sigaction { sa_sigaction: wakeup as *const () as usize, sa_mask: current_mask, - sa_flags: libc::SA_NOCLDSTOP, + // The handler only writes to the eventfd: a system call it + // lands in, on whichever thread, carries on instead of + // failing with EINTR. + sa_flags: libc::SA_NOCLDSTOP | libc::SA_RESTART, sa_restorer: None, }; libc::sigaction(libc::SIGCHLD, &raw const act, core::ptr::null_mut()); @@ -1437,268 +1577,9 @@ pub mod waiter_thread_posix { } } -// (PosixSpawnOptions / StdioKind / Dup2 / PosixStdio moved to bun_spawn_sys — -// re-exported above. Windows option/result types stay here: they embed -// `*mut Process` / `EventLoopHandle` and so cannot live in the leaf -sys crate.) - -#[cfg(windows)] -pub struct WindowsSpawnResult { - // Raw intrusive pointer. `Process` is intrusively - // ref-counted via `bun_ptr::ThreadSafeRefCount` and recovered via - // `uv_process_t.data` in the libuv callbacks; allocation is `heap::alloc` - // and destruction is `heap::take` (see `ThreadSafeRefCounted::destructor`). - pub(crate) process: Option<*mut Process>, - pub stdin: WindowsStdioResult, - pub stdout: WindowsStdioResult, - pub stderr: WindowsStdioResult, - pub extra_pipes: Vec, -} - -#[cfg(windows)] -impl Default for WindowsSpawnResult { - fn default() -> Self { - Self { - process: None, - stdin: WindowsStdioResult::Unavailable, - stdout: WindowsStdioResult::Unavailable, - stderr: WindowsStdioResult::Unavailable, - extra_pipes: Vec::new(), - } - } -} - -#[cfg(windows)] -pub enum WindowsStdioResult { - /// inherit, ignore, path, pipe - Unavailable, - Buffer(Box), - BufferFd(Fd), - /// A stdio slot at index >= 3 whose value `Subprocess.stdio` has exposed: - /// a duplicate of the pipe's HANDLE that the caller owns and closes - /// (`net.connect({ fd })` adopts it). The `Buffer` it came from is closed - /// when the slot is downgraded, so nothing here closes this handle. The - /// counterpart of the POSIX `ExtraPipe::UnownedFd`. - UnownedFd(Fd), -} - -#[cfg(windows)] -impl Default for WindowsStdioResult { - fn default() -> Self { - Self::Unavailable - } -} - -#[cfg(windows)] -impl WindowsStdioResult { - /// Mirrors `Option::::take()` on the POSIX `SpawnedStdio` so callers - /// (`shell::subproc`, `bun_spawn` JS bindings) can pull the handle out by - /// value without per-platform `mem::replace` at every call site. - #[inline] - pub fn take(&mut self) -> Self { - core::mem::take(self) - } -} - -#[cfg(windows)] -impl Drop for WindowsSpawnResult { - fn drop(&mut self) { - // Any `Buffer(Box)` still held here was `uv_pipe_init`'d in - // `spawn_process_windows` and is linked into the loop's handle queue; - // auto-dropping the Box would deallocate a live handle and the next - // `uv_run` would walk freed memory. Route un-consumed pipes through - // `uv_close` (free in the close callback). Slots already consumed via - // `.take()` are `Unavailable` and skip this. - // - // `WindowsStdioResult` itself deliberately has no `Drop` so callers - // can keep destructuring `Buffer(pipe)` by value; the container is - // the ownership boundary. - for slot in [&mut self.stdin, &mut self.stdout, &mut self.stderr] { - if let WindowsStdioResult::Buffer(pipe) = core::mem::take(slot) { - // SAFETY: `pipe` is the Box-allocated `uv::Pipe` from - // `create_zeroed_pipe`; `close_and_destroy` reclaims it via - // `Box::from_raw` in the close callback (or immediately if - // never init'd / `loop_ == null`). - unsafe { uv::Pipe::close_and_destroy(Box::into_raw(pipe)) }; - } - } - for slot in self.extra_pipes.drain(..) { - if let WindowsStdioResult::Buffer(pipe) = slot { - // SAFETY: see above. - unsafe { uv::Pipe::close_and_destroy(Box::into_raw(pipe)) }; - } - } - } -} - -#[cfg(windows)] -impl WindowsSpawnResult { - pub fn to_process(&mut self, _event_loop: impl Sized) -> RefPtr { - // SAFETY: the live heap `Process` allocated by `spawn_process_windows` - // with its initial ref. - unsafe { RefPtr::from_raw(self.process.take().unwrap()) } - } - - /// [`to_process`](Self::to_process) as the exit-handler owner's handle. - pub fn to_process_handle(&mut self, event_loop: impl Sized) -> ProcessHandle { - ProcessHandle(self.to_process(event_loop)) - } -} - -#[cfg(windows)] -pub struct WindowsSpawnOptions { - pub stdin: WindowsStdio, - pub stdout: WindowsStdio, - pub stderr: WindowsStdio, - pub ipc: Option, - pub extra_fds: Box<[WindowsStdio]>, - pub cwd: Box<[u8]>, - pub detached: bool, - /// `uv_process_options_t.uid` + `UV_PROCESS_SETUID`; libuv returns - /// `UV_ENOTSUP` on Windows, exactly like Node. - pub uid: Option, - /// `uv_process_options_t.gid` + `UV_PROCESS_SETGID`; libuv returns - /// `UV_ENOTSUP` on Windows, exactly like Node. - pub gid: Option, - pub windows: WindowsOptions, - pub argv0: Option<*const c_char>, - pub stream: bool, - pub use_execve_on_macos: bool, - pub can_block_entire_thread_to_reduce_cpu_usage_in_fast_path: bool, - /// Linux-only; placeholder for struct compatibility. - pub linux_pdeathsig: Option, - /// POSIX-only; placeholder for struct compatibility. - pub new_process_group: bool, - /// POSIX-only PTY slave fd; void placeholder on Windows. - pub pty_slave_fd: (), - /// Windows ConPTY handle. When set, the child is attached to the - /// pseudoconsole and stdin/stdout/stderr are provided by ConPTY. - pub pseudoconsole: Option, -} - -#[cfg(windows)] -impl Default for WindowsSpawnOptions { - fn default() -> Self { - Self { - stdin: WindowsStdio::Inherit, - stdout: WindowsStdio::Inherit, - stderr: WindowsStdio::Inherit, - ipc: None, - extra_fds: Box::new([]), - cwd: Box::new([]), - detached: false, - uid: None, - gid: None, - windows: WindowsOptions::default(), - argv0: None, - stream: true, - use_execve_on_macos: false, - can_block_entire_thread_to_reduce_cpu_usage_in_fast_path: false, - linux_pdeathsig: None, - new_process_group: false, - pty_slave_fd: (), - pseudoconsole: None, - } - } -} - -#[cfg(windows)] -#[derive(Clone, Copy)] -pub struct WindowsOptions { - pub verbatim_arguments: bool, - pub hide_window: bool, - pub loop_: EventLoopHandle, -} - -#[cfg(windows)] -impl Default for WindowsOptions { - fn default() -> Self { - Self { - verbatim_arguments: false, - hide_window: true, - // Every `bun.spawnSync` call site sets `loop_` explicitly. A - // zeroed handle here keeps `..Default::default()` usable - // for the other fields. `spawn_process_windows` (the sole consumer) - // asserts non-null at the read site so a forgotten `loop_` panics - // with a pointed message instead of segfaulting at the `.uv_loop` - // field offset. - // SAFETY: `EventLoopHandle` is a `Copy` enum of raw pointers; the - // all-zero bit pattern is discriminant 0 with a null payload — - // valid representation, never dereferenced before assignment. - loop_: unsafe { bun_core::ffi::zeroed_unchecked() }, - } - } -} - -#[cfg(windows)] -pub enum WindowsStdio { - Path(Box<[u8]>), - Inherit, - Ignore, - /// FFI-owned `uv::Pipe` (allocated via `heap::alloc` in - /// `create_zeroed_pipe`). Stored as a raw pointer so `spawn_process_windows` - /// can transfer sole ownership into `WindowsStdioResult::Buffer` via - /// `heap::take` without double-freeing when `WindowsSpawnOptions` drops. - Buffer(*mut uv::Pipe), - /// See `Buffer` — same FFI ownership model. - Ipc(*mut uv::Pipe), - Pipe(Fd), - Dup2(Dup2), -} - -#[cfg(windows)] -impl WindowsStdio { - /// Explicit destructor. - /// - /// **Not** `Drop`: `spawn_process_windows` takes `&WindowsSpawnOptions` - /// (immutable borrow) and transfers sole ownership of the `Buffer`/`Ipc` - /// pipe into `WindowsStdioResult::Buffer` via `heap::take`. An auto-Drop - /// here would then double-free the same `*mut uv::Pipe` when the borrowed - /// `WindowsSpawnOptions` (or the `to_spawn_options` temporary) goes out of - /// scope. Callers invoke - /// `deinit()` only on the *error* path where ownership was never taken. - pub fn deinit(&mut self) { - match self { - WindowsStdio::Buffer(pipe) | WindowsStdio::Ipc(pipe) => { - if !pipe.is_null() { - // SAFETY: non-null heap allocation from create_zeroed_pipe. - unsafe { uv::Pipe::close_and_destroy(*pipe) }; - *pipe = core::ptr::null_mut(); - } - } - _ => {} - } - } -} - -// WindowsSpawnOptions: no Drop. `WindowsStdio` holds FFI-owned `*mut uv::Pipe` -// whose ownership is transferred to `WindowsStdioResult` on success; callers -// must invoke `WindowsSpawnOptions::deinit` explicitly on the error path. -#[cfg(windows)] -impl WindowsSpawnOptions { - /// Explicit destructor. Closes and frees the heap-allocated `uv::Pipe` - /// handles for `Buffer`/`Ipc` stdio. - /// - /// **Not** `Drop`: on the *success* path `spawn_process_windows` transfers - /// sole ownership of each pipe into `WindowsStdioResult::Buffer` via - /// `heap::take`, leaving the raw pointers in `self` stale. An auto-Drop - /// would then double-free. Callers invoke this only on the *error* path - /// where ownership was never taken — failing to do so leaks `uv_pipe_t` - /// handles on the spawn-sync loop, which makes `uv_loop_close` return - /// `EBUSY` and trips `assert(err == 0)` in `uv_loop_delete` (uv-common.c). - pub fn deinit(&mut self) { - self.stdin.deinit(); - self.stdout.deinit(); - self.stderr.deinit(); - for stdio in self.extra_fds.iter_mut() { - stdio.deinit(); - } - } -} - -/// Event-loop-aware extension on the raw [`PosixSpawnResult`] from +/// Event-loop-aware extension on the raw [`SpawnResult`] from /// `bun_spawn_sys`. The result type itself lives in the leaf `-sys` crate (no -/// `Process`/`EventLoopHandle` dependency); `to_process` is added here as a -/// trait method so callers keep the `.to_process(loop_, sync)` spelling. +/// `Process`/`EventLoopHandle` dependency), so `to_process` is a trait method. pub trait SpawnResultExt: Sized { fn to_process(self, event_loop: EventLoopHandle) -> RefPtr; @@ -1708,29 +1589,13 @@ pub trait SpawnResultExt: Sized { } } -#[cfg(unix)] -impl SpawnResultExt for PosixSpawnResult { - fn to_process(self, event_loop: EventLoopHandle) -> RefPtr { - // SAFETY: `init_posix` heap-allocates the `Process` with its initial ref. - unsafe { RefPtr::from_raw(Process::init_posix(&self, event_loop)) } +impl SpawnResultExt for SpawnResult { + fn to_process(mut self, event_loop: EventLoopHandle) -> RefPtr { + // SAFETY: `init` heap-allocates the `Process` with its initial ref. + unsafe { RefPtr::from_raw(Process::init(&mut self, event_loop)) } } } -#[cfg(unix)] -pub type SpawnOptions = PosixSpawnOptions; -#[cfg(windows)] -pub type SpawnOptions = WindowsSpawnOptions; - -#[cfg(unix)] -pub type Stdio = PosixStdio; -#[cfg(windows)] -pub type Stdio = WindowsStdio; - -#[cfg(unix)] -pub type SpawnProcessResult = PosixSpawnResult; -#[cfg(windows)] -pub type SpawnProcessResult = WindowsSpawnResult; - // ─── spawn_process bodies + sync runner ────────────────────────────────────── mod spawn_process_body { @@ -1739,6 +1604,8 @@ mod spawn_process_body { #[cfg(unix)] pub use bun_spawn_sys::spawn_process_posix; + #[cfg(windows)] + use bun_spawn_sys::spawn_process_windows; /// RAII fd owner — closes the wrapped [`Fd`] on drop iff it is valid. /// Used by `sync::spawn_posix` (no-orphans kqueue, ppid pidfd). @@ -1774,21 +1641,22 @@ mod spawn_process_body { /// `argv` must point to a null-terminated array of NUL-terminated C /// strings with at least one non-null element; `envp` must point to a /// null-terminated array of NUL-terminated C strings, or be null on - /// Windows (libuv then inherits the parent environment). Both must remain - /// valid for the duration of the call. + /// Windows (the child then inherits this process's environment). Both must + /// remain valid for the duration of the call. pub unsafe fn spawn_process( options: &SpawnOptions, argv: Argv, // [*:null]?[*:0]const u8 envp: Envp, - ) -> Result, crate::Error> { + ) -> Result, crate::Error> { #[cfg(unix)] { // SAFETY: forwarded from this function's safety contract. unsafe { spawn_process_posix(options, argv, envp) }.map_err(Into::into) } - #[cfg(not(unix))] + #[cfg(windows)] { - spawn_process_windows(options, argv, envp) + // SAFETY: forwarded from this function's safety contract. + unsafe { spawn_process_windows(options, argv, envp) }.map_err(Into::into) } } @@ -1807,7 +1675,7 @@ mod spawn_process_body { options: &SpawnOptions, argv: &[&core::ffi::CStr], env: SpawnEnv<'_>, - ) -> Result, crate::Error> { + ) -> Result, crate::Error> { assert!(!argv.is_empty(), "spawn_process_cstr: argv[0] is required"); let argv: Vec = argv .iter() @@ -1816,7 +1684,6 @@ mod spawn_process_body { .collect(); let env_block: Vec; let envp: Envp = match env { - // libuv: a null `env` inherits the parent's environment. #[cfg(windows)] SpawnEnv::Inherit => core::ptr::null(), #[cfg(unix)] @@ -1836,465 +1703,8 @@ mod spawn_process_body { unsafe { spawn_process(options, argv.as_ptr(), envp) } } - #[cfg(windows)] - pub(crate) fn spawn_process_windows( - options: &WindowsSpawnOptions, - argv: *const *const c_char, - envp: *const *const c_char, - ) -> Result, crate::Error> { - bun_analytics::features::spawn.fetch_add(1, Ordering::Relaxed); - - // SAFETY: all-zero is a valid uv_process_options_t - let mut uv_process_options: uv::uv_process_options_t = - unsafe { bun_core::ffi::zeroed_unchecked() }; - - uv_process_options.args = argv; - uv_process_options.env = envp; - // SAFETY: argv is null-terminated, argv[0] is non-null - uv_process_options.file = options.argv0.unwrap_or_else(|| unsafe { *argv }); - uv_process_options.exit_cb = Some(Process::on_exit_uv); - // `WindowsOptions::default()` leaves `loop_` zeroed (every - // `bun.spawnSync` call site sets it explicitly). A zeroed - // `EventLoopHandle` is discriminant 0 (`Js`) with a null inner pointer, - // so `platform_event_loop()` returns null and the deref below segfaults - // at the `.uv_loop` field offset. Catch that with a clear panic instead - // of an opaque exit-code-3. Release-build assert: this is the contract - // boundary, not a debug aid. - assert!( - !options.windows.loop_.platform_event_loop().is_null(), - "spawn_process_windows: WindowsSpawnOptions.windows.loop_ was not set. \ - WindowsOptions::default() leaves it zeroed (Zig spec: `= undefined`); \ - every caller must populate it — see src/CLAUDE.md §Spawning Subprocesses \ - (`.loop = jsc.EventLoopHandle.init(jsc.MiniEventLoop.initGlobal(...))`)." - ); - // Non-null verified above; `EventLoopHandle::uv_loop` is the centralized - // accessor for the set-once `.uv_loop` field of the `uws::WindowsLoop`. - let loop_ = options.windows.loop_.uv_loop(); - - let cwd = match bun_sys::to_posix_path(&options.cwd) { - Ok(p) => p, - Err(e) => return Err(crate::Error::Sys(e)), - }; - - // `git_diff_internal` reaches here - // with `Options::default()` → `cwd = ""`. libuv treats a - // non-NULL `cwd` as explicit and hands `L""` to `CreateProcessW`, which - // fails with `ERROR_DIRECTORY` → `UV_ENOENT`. Map empty to NULL so libuv - // inherits the parent cwd. - uv_process_options.cwd = if options.cwd.is_empty() { - core::ptr::null() - } else { - cwd.as_ptr().cast::() - }; - - let mut uv_files_to_close: Vec = Vec::new(); - - if let Some(hpcon) = options.pseudoconsole { - uv_process_options.pseudoconsole = hpcon; - } - - if options.windows.hide_window { - uv_process_options.flags |= uv::UV_PROCESS_WINDOWS_HIDE; - } - - if options.windows.verbatim_arguments { - uv_process_options.flags |= uv::UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS; - } - - if options.detached { - uv_process_options.flags |= uv::UV_PROCESS_DETACHED; - } - - // libuv's uv_spawn rejects UV_PROCESS_SETUID/SETGID with UV_ENOTSUP on - // Windows — the same error Node reports for uid/gid there. - if let Some(uid) = options.uid { - uv_process_options.uid = uid as uv::uv_uid_t; - uv_process_options.flags |= uv::UV_PROCESS_SETUID; - } - if let Some(gid) = options.gid { - uv_process_options.gid = gid as uv::uv_gid_t; - uv_process_options.flags |= uv::UV_PROCESS_SETGID; - } - - let mut stdio_containers: Vec = - Vec::with_capacity(3 + options.extra_fds.len()); - // SAFETY: all-zero is valid uv_stdio_container_t - stdio_containers.resize_with(3 + options.extra_fds.len(), || unsafe { - bun_core::ffi::zeroed_unchecked() - }); - - let stdio_options: [&WindowsStdio; 3] = [&options.stdin, &options.stdout, &options.stderr]; - - // On Windows it seems don't have a dup2 equivalent with pipes - // So we need to use file descriptors. - // We can create a pipe with `uv_pipe(fds, 0, 0)` and get a read fd and write fd. - // We give the write fd to stdout/stderr - // And use the read fd to read from the output. - let mut dup_fds: [uv::uv_file; 2] = [-1, -1]; - let mut dup_src: Option = None; - let mut dup_tgt: Option = None; - - for fd_i in 0..3usize { - let pipe_flags = uv::UV_CREATE_PIPE | uv::UV_READABLE_PIPE | uv::UV_WRITABLE_PIPE; - let stdio: &mut uv::uv_stdio_container_t = &mut stdio_containers[fd_i]; - let flag: c_int = if fd_i == 0 { - uv::O::RDONLY - } else { - uv::O::WRONLY - }; - - let mut treat_as_dup: bool = false; - - if fd_i == 1 && matches!(stdio_options[2], WindowsStdio::Dup2(_)) { - treat_as_dup = true; - dup_tgt = Some(u32::try_from(fd_i).expect("int cast")); - } else if fd_i == 2 && matches!(stdio_options[1], WindowsStdio::Dup2(_)) { - treat_as_dup = true; - dup_tgt = Some(u32::try_from(fd_i).expect("int cast")); - } else { - match stdio_options[fd_i] { - WindowsStdio::Dup2(_) => { - treat_as_dup = true; - dup_src = Some(u32::try_from(fd_i).expect("int cast")); - } - WindowsStdio::Inherit => { - stdio.flags = uv::UV_INHERIT_FD; - stdio.data.fd = uv::uv_file::try_from(fd_i).expect("int cast"); - } - WindowsStdio::Ipc(_) => { - // ipc option inside stdin, stderr or stdout is not supported. - // Don't free the pipe here — the caller owns it and will - // clean it up via WindowsSpawnOptions Drop. - stdio.flags = uv::UV_IGNORE; - } - WindowsStdio::Ignore => { - stdio.flags = uv::UV_IGNORE; - } - WindowsStdio::Path(path) => { - let mut req = uv::fs_t::uninitialized(); - // Cleanup is via manual calls at each exit path, so `?` here - // would leak any fds opened by earlier loop iterations. - let path_z = match bun_sys::to_posix_path(path) { - Ok(p) => p, - Err(e) => { - cleanup_uv_files(&uv_files_to_close, loop_); - return Err(crate::Error::Sys(e)); - } - }; - // SAFETY: `req` is a fresh `fs_t`, `loop_` is the live uv - // loop, `path_z` is NUL-terminated and outlives the call - // (sync — no callback). - let rc = unsafe { - uv::uv_fs_open( - loop_, - &mut req, - path_z.as_ptr(), - flag | uv::O::CREAT, - 0o644, - None, - ) - }; - req.deinit(); - if let Some(err) = rc.to_error(bun_sys::Tag::open) { - cleanup_uv_files(&uv_files_to_close, loop_); - return Ok(Err(err)); - } - stdio.flags = uv::UV_INHERIT_FD; - let fd = rc.int(); - uv_files_to_close.push(fd); - stdio.data.fd = fd; - } - WindowsStdio::Buffer(my_pipe) => { - // SAFETY: `my_pipe` is a non-null heap allocation from - // create_zeroed_pipe (heap::alloc). - if let Some(err) = unsafe { (&mut **my_pipe).init(loop_, false) } - .to_error(bun_sys::Tag::uv_pipe) - { - cleanup_uv_files(&uv_files_to_close, loop_); - return Ok(Err(err)); - } - stdio.flags = pipe_flags; - stdio.data.stream = (*my_pipe).cast::(); - } - WindowsStdio::Pipe(fd) => { - stdio.flags = uv::UV_INHERIT_FD; - stdio.data.fd = fd.uv(); - } - } - } - - if treat_as_dup { - if fd_i == 1 { - // SAFETY: `dup_fds` is a 2-element out-array; libuv writes both. - if let Some(err) = - unsafe { uv::uv_pipe(&mut dup_fds, 0, 0) }.to_error(bun_sys::Tag::pipe) - { - cleanup_uv_files(&uv_files_to_close, loop_); - return Ok(Err(err)); - } - } - stdio.flags = uv::UV_INHERIT_FD; - stdio.data.fd = dup_fds[1]; - } - } - - for (i, ipc) in options.extra_fds.iter().enumerate() { - let stdio: &mut uv::uv_stdio_container_t = &mut stdio_containers[3 + i]; - let flag: c_int = uv::O::RDWR; - - match ipc { - WindowsStdio::Dup2(_) => panic!("TODO dup2 extra fd"), - WindowsStdio::Inherit => { - stdio.flags = uv::UV_INHERIT_FD; - stdio.data.fd = uv::uv_file::try_from(3 + i).expect("int cast"); - } - WindowsStdio::Ignore => { - stdio.flags = uv::UV_IGNORE; - } - WindowsStdio::Path(path) => { - let mut req = uv::fs_t::uninitialized(); - // See stdio loop above: manual cleanup on every exit path. - let path_z = match bun_sys::to_posix_path(path) { - Ok(p) => p, - Err(e) => { - cleanup_uv_files(&uv_files_to_close, loop_); - return Err(crate::Error::Sys(e)); - } - }; - // SAFETY: `req` is a fresh `fs_t`, `loop_` is the live uv loop, - // `path_z` is NUL-terminated and outlives the call (sync). - let rc = unsafe { - uv::uv_fs_open( - loop_, - &mut req, - path_z.as_ptr(), - flag | uv::O::CREAT, - 0o644, - None, - ) - }; - req.deinit(); - if let Some(err) = rc.to_error(bun_sys::Tag::open) { - cleanup_uv_files(&uv_files_to_close, loop_); - return Ok(Err(err)); - } - stdio.flags = uv::UV_INHERIT_FD; - let fd = rc.int(); - uv_files_to_close.push(fd); - stdio.data.fd = fd; - } - WindowsStdio::Ipc(my_pipe) => { - // SAFETY: non-null heap allocation from create_zeroed_pipe. - if let Some(err) = unsafe { (&mut **my_pipe).init(loop_, true) } - .to_error(bun_sys::Tag::uv_pipe) - { - cleanup_uv_files(&uv_files_to_close, loop_); - return Ok(Err(err)); - } - stdio.flags = uv::UV_CREATE_PIPE - | uv::UV_WRITABLE_PIPE - | uv::UV_READABLE_PIPE - | uv::UV_OVERLAPPED_PIPE; - stdio.data.stream = (*my_pipe).cast::(); - } - WindowsStdio::Buffer(my_pipe) => { - // SAFETY: non-null heap allocation from create_zeroed_pipe. - if let Some(err) = unsafe { (&mut **my_pipe).init(loop_, false) } - .to_error(bun_sys::Tag::uv_pipe) - { - cleanup_uv_files(&uv_files_to_close, loop_); - return Ok(Err(err)); - } - stdio.flags = uv::UV_CREATE_PIPE - | uv::UV_WRITABLE_PIPE - | uv::UV_READABLE_PIPE - | uv::UV_OVERLAPPED_PIPE; - stdio.data.stream = (*my_pipe).cast::(); - } - WindowsStdio::Pipe(fd) => { - stdio.flags = uv::UV_INHERIT_FD; - stdio.data.fd = fd.uv(); - } - } - } - - uv_process_options.stdio = stdio_containers.as_mut_ptr(); - uv_process_options.stdio_count = c_int::try_from(stdio_containers.len()).expect("int cast"); - uv_process_options.exit_cb = Some(Process::on_exit_uv); - - let process = bun_core::heap::into_raw(Box::new(Process { - ref_count: bun_ptr::ThreadSafeRefCount::init(), - event_loop: options.windows.loop_, - pid: 0, - status: Status::Running, - poller: Poller::Detached, - exit_handler: ProcessExitHandler::default(), - })); - - // SAFETY: process is freshly allocated - unsafe { - // SAFETY: all-zero is valid uv::Process - (*process).poller = Poller::Uv(bun_core::ffi::zeroed_unchecked()); - // Back-pointer for `on_exit_uv` / `on_close_uv`. - // Every libuv handle starts with `data: *mut c_void`. - let Poller::Uv(ref mut uv_proc) = (*process).poller else { - unreachable!() - }; - uv_proc.data = process.cast::(); - } - - let cleanup_dup = |failed: bool| { - if dup_src.is_some() { - debug_assert!(dup_src.is_some() && dup_tgt.is_some()); - } - if failed && dup_fds[0] != -1 { - Fd::from_uv(dup_fds[0]).close(); - } - if dup_fds[1] != -1 { - Fd::from_uv(dup_fds[1]).close(); - } - }; - - // SAFETY: process.poller was just set to Uv variant - let spawn_err = unsafe { - let Poller::Uv(ref mut uv_proc) = (*process).poller else { - unreachable!() - }; - uv_proc - .spawn(loop_, &mut uv_process_options) - .to_error(bun_sys::Tag::uv_spawn) - }; - if let Some(err) = spawn_err { - cleanup_dup(true); - cleanup_uv_files(&uv_files_to_close, loop_); - // SAFETY: process is valid - unsafe { - (*process).close(); - Process::deref(process); - } - return Ok(Err(err)); - } - // The process handle is open on this thread's loop until `close()`; a - // thread teardown closes it through us (the child keeps running, as with - // Node's ProcessWrap), so no exit callback can fire after the VM is gone. - unsafe fn stop_for_vm_teardown(p: *mut c_void) { - // SAFETY: recorded for this live Process; the handle leaves the list - // when `close()` issues its uv_close. - unsafe { (*p.cast::()).close() }; - } - // SAFETY: `process` is live; poller was just set to the spawned Uv handle. - unsafe { - let Poller::Uv(ref mut uv_proc) = (*process).poller else { - unreachable!() - }; - uv::open_handles::set_owner( - core::ptr::from_mut(uv_proc).cast(), - process.cast(), - Some(stop_for_vm_teardown), - ); - } - - // SAFETY: process is valid, poller is Uv - unsafe { - let Poller::Uv(ref uv_proc) = (*process).poller else { - unreachable!() - }; - (*process).pid = uv_proc.pid; - // Function pointers compared by address (`as usize`): direct - // `fn == fn` is unreliable across codegen units and triggers - // `unpredictable_function_pointer_comparisons`. - debug_assert_eq!( - uv_proc.exit_cb.map(|cb| cb as usize), - Some( - Process::on_exit_uv as unsafe extern "C" fn(*mut uv::uv_process_t, i64, c_int) - as usize, - ), - ); - } - - // No FRU `..Default::default()` here: `WindowsSpawnResult` impls `Drop`, - // so functional-record-update would have to move fields out of the - // temporary default — E0509. Spell the defaults out instead. - let mut result = WindowsSpawnResult { - // Intrusive raw pointer; refcount lives inside `Process` (see field comment). - process: Some(process), - stdin: WindowsStdioResult::Unavailable, - stdout: WindowsStdioResult::Unavailable, - stderr: WindowsStdioResult::Unavailable, - extra_pipes: Vec::with_capacity(options.extra_fds.len()), - }; - - for i in 0..3usize { - let stdio = &stdio_containers[i]; - let result_stdio: &mut WindowsStdioResult = match i { - 0 => &mut result.stdin, - 1 => &mut result.stdout, - 2 => &mut result.stderr, - _ => unreachable!(), - }; - - if dup_src == Some(u32::try_from(i).expect("int cast")) { - *result_stdio = WindowsStdioResult::Unavailable; - } else if dup_tgt == Some(u32::try_from(i).expect("int cast")) { - *result_stdio = WindowsStdioResult::BufferFd(Fd::from_uv(dup_fds[0])); - } else { - match stdio_options[i] { - WindowsStdio::Buffer(_) => { - // SAFETY: stdio.data.stream is the same `*mut uv::Pipe` - // produced by `heap::alloc` in create_zeroed_pipe and - // stored in `options.{stdin,stdout,stderr}`. `WindowsStdio` - // has no `Drop` (deinit is explicit), so - // reconstructing the Box here is the *sole* ownership - // transfer — the borrowed `options` dropping later is a - // no-op on the raw pointer. - *result_stdio = WindowsStdioResult::Buffer(unsafe { - bun_core::heap::take(stdio.data.stream.cast::()) - }); - } - _ => { - *result_stdio = WindowsStdioResult::Unavailable; - } - } - } - } - - for (i, input) in options.extra_fds.iter().enumerate() { - match input { - WindowsStdio::Ipc(_) | WindowsStdio::Buffer(_) => { - // SAFETY: sole ownership transfer of the heap-allocated - // uv::Pipe; `WindowsStdio` has no Drop (explicit `deinit`). - result.extra_pipes.push(WindowsStdioResult::Buffer(unsafe { - bun_core::heap::take(stdio_containers[3 + i].data.stream.cast::()) - })); - } - _ => { - result.extra_pipes.push(WindowsStdioResult::Unavailable); - } - } - } - - cleanup_dup(false); - cleanup_uv_files(&uv_files_to_close, loop_); - Ok(Ok(result)) - } - - #[cfg(windows)] - fn cleanup_uv_files(files: &[uv::uv_file], loop_: *mut uv::uv_loop_t) { - for &fd in files { - bun_io::Closer::close(Fd::from_uv(fd), loop_); - } - } - pub mod sync { use super::*; - // `Options.windows` is `WindowsOptions` on Windows; surface it under the - // `…::process::sync` path. A `pub use super::…` - // re-export trips E0365 here because the `use super::*` glob has already - // bound the name privately and rustc treats the explicit re-export as - // re-exporting that private binding; a type alias sidesteps the conflict. - #[cfg(windows)] - pub type WindowsOptions = super::WindowsOptions; - pub struct Options { pub stdin: SyncStdio, pub stdout: SyncStdio, @@ -2322,46 +1732,15 @@ mod spawn_process_body { } impl SyncStdio { - pub(crate) fn to_stdio(self) -> SpawnOptionsStdio { + pub(crate) fn to_stdio(self) -> Stdio { match self { - SyncStdio::Inherit => SpawnOptionsStdio::inherit(), - SyncStdio::Ignore => SpawnOptionsStdio::ignore(), - SyncStdio::Buffer => { - #[cfg(windows)] - { - SpawnOptionsStdio::buffer(bun_core::heap::into_raw(Box::new( - bun_core::ffi::zeroed::(), - ))) - } - #[cfg(not(windows))] - { - SpawnOptionsStdio::buffer() - } - } + SyncStdio::Inherit => Stdio::Inherit, + SyncStdio::Ignore => Stdio::Ignore, + SyncStdio::Buffer => Stdio::Buffer, } } } - // Helper alias: SpawnOptions::Stdio differs by platform - #[cfg(unix)] - pub(crate) type SpawnOptionsStdio = PosixStdio; - #[cfg(windows)] - pub(crate) type SpawnOptionsStdio = WindowsStdio; - - // PosixStdio constructor helpers live in `bun_spawn_sys` (inherent impl). - #[cfg(windows)] - impl WindowsStdio { - fn inherit() -> Self { - WindowsStdio::Inherit - } - fn ignore() -> Self { - WindowsStdio::Ignore - } - fn buffer(p: *mut uv::Pipe) -> Self { - WindowsStdio::Buffer(p) - } - } - impl Default for Options { fn default() -> Self { Self { @@ -2395,7 +1774,7 @@ mod spawn_process_body { argv0: self.argv0, new_process_group, #[cfg(windows)] - windows: self.windows.clone(), + windows: self.windows, ..Default::default() } } @@ -2413,411 +1792,256 @@ mod spawn_process_body { } } + /// One output pipe of a synchronously spawned child, drained with + /// overlapped reads so both pipes and the process can be waited on from + /// one thread. The handle is never associated with a completion port; + /// completion is signalled through `event`. #[cfg(windows)] - pub(crate) struct SyncWindowsPipeReader { - pub(crate) chunks: Vec>, - /// Buffer handed to libuv by `on_alloc`; reclaimed (truncated) by - /// `on_read`. Prevents the per-read leak that copying `data` into a - /// fresh Box would cause. - pending_alloc: Option>, - pub(crate) pipe: Box, - pub(crate) err: bun_sys::E, - pub(crate) context: *mut SyncWindowsProcess, - pub(crate) on_done_callback: - fn(&mut SyncWindowsProcess, OutFd, Vec>, bun_sys::E), - pub(crate) tag: OutFd, + struct PipeDrain { + handle: bun_sys::windows::HANDLE, + event: bun_sys::windows::HANDLE, + /// Boxed: the kernel writes to it while a read is pending. + overlapped: Box, + /// The pending read targets this buffer's spare capacity, so it is + /// not touched until the read completes. + bytes: Vec, + pending: bool, } #[cfg(windows)] - impl SyncWindowsPipeReader { - pub(crate) fn new(v: SyncWindowsPipeReader) -> Box { - Box::new(v) - } - - fn on_alloc(this: &mut SyncWindowsPipeReader, suggested_size: usize) -> &mut [u8] { - // Stash the allocation so `on_read` can reclaim it without copying. - // If a previous alloc was never consumed (nread == 0 / EAGAIN), - // dropping the old Box here frees it — no leak. - let buf = this - .pending_alloc - .insert(vec![0u8; suggested_size].into_boxed_slice()); - &mut buf[..] - } - - fn on_read(this: &mut SyncWindowsPipeReader, data: &[u8]) { - // `data` *is* the buffer - // `on_alloc` returned, sliced to `nread`. Reclaim that allocation - // (debug-assert it's the same pointer), truncate to the read - // length, and push — no copy, no leak. - let buf = this - .pending_alloc - .take() - .expect("on_read without preceding on_alloc"); - debug_assert_eq!(buf.as_ptr(), data.as_ptr()); - debug_assert!(data.len() <= buf.len()); - let mut v = Vec::from(buf); - v.truncate(data.len()); - this.chunks.push(v.into_boxed_slice()); - } - - fn on_error(this: &mut SyncWindowsPipeReader, err: bun_sys::E) { - this.err = err; - this.pipe.close(Self::on_close); - } - - // ── libuv C trampolines ────────────────────────────────────────── - // There is exactly - // one call site, so we hand-write the wrapper pair against - // `SyncWindowsPipeReader` and call `on_alloc` / `on_read` / `on_error` - // *directly* — no runtime fn-ptr stash. - unsafe extern "C" fn uv_alloc_cb( - req: *mut uv::uv_handle_t, - suggested_size: usize, - buffer: *mut uv::uv_buf_t, - ) { - // SAFETY: `req.data` was set to `*mut Self` in `start()`. - let this: &mut SyncWindowsPipeReader = - unsafe { &mut *((*req).data as *mut SyncWindowsPipeReader) }; - let buf = Self::on_alloc(this, suggested_size); - // SAFETY: `buffer` is a libuv-owned out-parameter. Do NOT route - // through `uv_buf_t::init(&[u8])` — that reborrows the `&mut [u8]` - // as shared, so `as_ptr().cast_mut()` yields a SharedReadOnly tag - // and libuv's subsequent write into `base[..nread]` is - // Stacked-Borrows UB. Construct from `as_mut_ptr()` directly so the - // raw pointer carries write provenance. - unsafe { - *buffer = uv::uv_buf_t { - len: buf.len() as uv::ULONG, - base: buf.as_mut_ptr(), - }; - } - } - unsafe extern "C" fn uv_read_cb( - req: *mut uv::uv_stream_t, - nreads: uv::ReturnCodeI64, - buffer: *const uv::uv_buf_t, - ) { - // SAFETY: `req.data` was set to `*mut Self` in `start()`. - let this: &mut SyncWindowsPipeReader = - unsafe { &mut *((*req).data as *mut SyncWindowsPipeReader) }; - let nreads = nreads.int(); - if nreads == 0 { - return; - } // EAGAIN / EWOULDBLOCK - if nreads < 0 { - this.pipe.read_stop(); - let e = bun_sys::windows::translate_uv_error_to_e(nreads as core::ffi::c_int); - Self::on_error(this, e); - } else { - // SAFETY: libuv guarantees `base[..nreads]` is the slice we - // returned from `uv_alloc_cb`, filled to `nreads` bytes. - let data = unsafe { - core::slice::from_raw_parts((*buffer).base.cast::(), nreads as usize) - }; - Self::on_read(this, data); - } - } - - extern "C" fn on_close(pipe: *mut uv::Pipe) { - // SAFETY: pipe.data was set to *mut Self in start() - let this: *mut SyncWindowsPipeReader = - unsafe { (*pipe).get_data::() }; - assert!( - !this.is_null(), - "Expected SyncWindowsPipeReader to have data" - ); - // SAFETY: this was heap-allocated in start(); libuv is done - // with the handle once the close callback fires, so reclaim - // ownership here. - let mut this = unsafe { bun_core::heap::take(this) }; - let context = this.context; - // Move ownership of the chunk allocations out *before* dropping - // `this`, otherwise the callback would observe freed buffers. - // The chunk allocations survive to be freed later by - // `flatten_owned_chunks`. - let chunks: Vec> = core::mem::take(&mut this.chunks); - let err = if this.err == bun_sys::E::CANCELED { - bun_sys::E::SUCCESS - } else { - this.err + impl PipeDrain { + const CHUNK: usize = 64 * 1024; + + fn new(fd: Option) -> Maybe { + use bun_spawn_sys::windows::win32; + let mut this = Self { + handle: fd.map_or(win32::INVALID_HANDLE_VALUE, |fd| fd.native()), + event: core::ptr::null_mut(), + // SAFETY: all-zero is a valid OVERLAPPED. + overlapped: Box::new(unsafe { bun_core::ffi::zeroed_unchecked() }), + bytes: Vec::new(), + pending: false, }; - let tag = this.tag; - let on_done_callback = this.on_done_callback; - drop(this); - // SAFETY: `context` is the live `SyncWindowsProcess` that owns - // this reader (set in `spawn_windows_with_pipes`); the callback - // is non-reentrant field writes only. - on_done_callback(unsafe { &mut *context }, tag, chunks, err); - } - - pub(crate) fn start(self: Box) -> Maybe<()> { - // Single-pointer ownership: `heap::alloc` is the *only* root for - // this allocation. Every subsequent access (including the libuv - // callbacks and the `heap::take` in `on_close`) goes through - // this pointer, so no Stacked Borrows tag is invalidated by an - // interleaved Box deref. - let this: *mut SyncWindowsPipeReader = bun_core::heap::into_raw(self); - // SAFETY: just allocated; sole owner. - unsafe { - (*this).pipe.set_data(this.cast()); - (*this).pipe.ref_(); - if let Some(err) = (*this) - .pipe - .read_start(Some(Self::uv_alloc_cb), Some(Self::uv_read_cb)) - .to_error(bun_sys::Tag::listen) - { - // Intentionally leak `this`. The boxed `uv::Pipe` was already - // `uv_pipe_init`'d by the spawn path and is linked into the - // loop's handle queue; freeing it here without `uv_close()` - // would leave a dangling `uv_handle_t`. The sole caller - // `Output::panic`s on error, so the leak is bounded. - return Err(err); + if this.is_open() { + // SAFETY: no attributes, no name; manual-reset, as overlapped I/O requires. + this.event = unsafe { + win32::CreateEventW(core::ptr::null_mut(), 1, 0, core::ptr::null()) + }; + if this.event.is_null() { + return Err(win32::last_error(bun_sys::Tag::read)); } + this.overlapped.hEvent = this.event; } - Ok(()) + Ok(this) } - } - - #[cfg(windows)] - #[derive(Clone, Copy, PartialEq, Eq)] - pub enum OutFd { - Stdout, - Stderr, - } - #[cfg(windows)] - impl OutFd { - #[inline] - pub(crate) fn as_str(self) -> &'static str { - match self { - OutFd::Stdout => "stdout", - OutFd::Stderr => "stderr", - } + fn is_open(&self) -> bool { + self.handle != bun_sys::windows::INVALID_HANDLE_VALUE } - } - #[cfg(windows)] - pub struct SyncWindowsProcess { - pub(crate) stderr: Vec>, - pub(crate) stdout: Vec>, - pub(crate) err: bun_sys::E, - pub(crate) waiting_count: u8, - /// Intrusive-refcounted. Allocated via - /// `heap::alloc` in `to_process`; freed when the embedded - /// `ThreadSafeRefCount` hits zero. Stored raw — `Arc` would - /// give only `*const` provenance and make `&mut *` writes UB. - pub(crate) process: *mut Process, - pub(crate) status: Option, - } + fn close(&mut self) { + // SAFETY: the parent end of the pipe is ours. + unsafe { bun_spawn_sys::windows::win32::CloseHandle(self.handle) }; + self.handle = bun_sys::windows::INVALID_HANDLE_VALUE; + } - #[cfg(windows)] - impl SyncWindowsProcess { - pub(crate) fn new(v: SyncWindowsProcess) -> Box { - Box::new(v) - } - - /// `process` is the *same* `*mut Process` that was threaded through - /// `Process::on_exit_uv` → `Process::on_exit` → `ProcessExitHandler::call` - /// (which holds a protector-guarded `&mut Process` in its frame). - /// Re-deriving a `&mut Process` from the independent `self.process` - /// root would pop that protected tag under Stacked Borrows, so we - /// take the already-live pointer instead. - pub fn on_process_exit( - this: *mut SyncWindowsProcess, - process: *mut Process, - status: Status, - _: &Rusage, - ) { - // SAFETY: `this` is the heap::alloc root from spawn_windows_with_pipes; - // single-threaded uv loop, no overlapping borrow of SyncWindowsProcess. - unsafe { - (*this).status = Some(status); - (*this).waiting_count -= 1; + /// `ok` and `bytes_read` are what `ReadFile`/`GetOverlappedResult` + /// answered for the read into the spare capacity. Returns whether the + /// pipe is still open. + fn finish_read(&mut self, ok: bool, bytes_read: u32) -> Maybe { + use bun_spawn_sys::windows::win32; + if ok { + // SAFETY: the kernel wrote `bytes_read` bytes into the spare capacity. + unsafe { bun_core::vec::commit_spare(&mut self.bytes, bytes_read as usize) }; + return Ok(true); } - // SAFETY: `process` carries the provenance of the `&mut Process` - // already live in `ProcessExitHandler::call`; mutating through it - // re-uses that tag instead of conflicting with it. - unsafe { - (*process).detach(); - Process::deref(process); + match win32::GetLastError() { + // Every write end is closed: end of file. + win32::ERROR_BROKEN_PIPE => { + self.close(); + Ok(false) + } + code => Err(win32::sys_error(code, bun_sys::Tag::read)), } } - pub(crate) fn on_reader_done( - &mut self, - tag: OutFd, - chunks: Vec>, - err: bun_sys::E, - ) { - match tag { - OutFd::Stderr => self.stderr = chunks, - OutFd::Stdout => self.stdout = chunks, - } - if err != bun_sys::E::SUCCESS { - self.err = err; + /// Reads until a read is left pending or the pipe ends. + fn read(&mut self) -> Maybe<()> { + use bun_spawn_sys::windows::win32; + while self.is_open() { + if self.bytes.try_reserve(Self::CHUNK).is_err() { + return Err(bun_sys::Error::from_code( + bun_sys::E::ENOMEM, + bun_sys::Tag::read, + )); + } + // SAFETY: only used as the destination of the read below. + let spare = unsafe { bun_core::vec::spare_bytes_mut(&mut self.bytes) }; + let mut bytes_read: u32 = 0; + // SAFETY: `spare` and `overlapped` stay valid and unmoved until + // the read completes (see `Drop`). + let ok = unsafe { + win32::ReadFile( + self.handle, + spare.as_mut_ptr(), + spare.len().min(u32::MAX as usize) as u32, + &mut bytes_read, + (&raw mut *self.overlapped).cast(), + ) + } != 0; + if !ok && win32::GetLastError() == win32::ERROR_IO_PENDING { + self.pending = true; + return Ok(()); + } + self.finish_read(ok, bytes_read)?; } - self.waiting_count -= 1; + Ok(()) + } + + /// `event` is signalled: take the pending read's result and read on. + fn on_event(&mut self) -> Maybe<()> { + use bun_spawn_sys::windows::win32; + let mut bytes_read: u32 = 0; + // SAFETY: `overlapped` is the pending read's; it has completed. + let ok = unsafe { + win32::GetOverlappedResult( + self.handle, + &raw mut *self.overlapped, + &mut bytes_read, + 0, + ) + } != 0; + self.pending = false; + self.finish_read(ok, bytes_read)?; + self.read() } } #[cfg(windows)] - fn flatten_owned_chunks(chunks: Vec>) -> Vec { - let mut total_size: usize = 0; - for chunk in &chunks { - total_size += chunk.len(); - } - let mut result = Vec::with_capacity(total_size); - for chunk in chunks { - result.extend_from_slice(&chunk); + impl Drop for PipeDrain { + fn drop(&mut self) { + use bun_spawn_sys::windows::win32; + // SAFETY: the handles are ours. A pending read owns `overlapped` + // and the buffer until it has completed, cancelled or not. + unsafe { + if self.pending { + let mut bytes_read: u32 = 0; + win32::CancelIoEx(self.handle, &raw mut *self.overlapped); + win32::GetOverlappedResult( + self.handle, + &raw mut *self.overlapped, + &mut bytes_read, + 1, + ); + } + if self.is_open() { + win32::CloseHandle(self.handle); + } + if !self.event.is_null() { + win32::CloseHandle(self.event); + } + } } - result } #[cfg(windows)] - fn spawn_windows_without_pipes( + fn spawn_windows( options: &Options, argv: *const *const c_char, envp: *const *const c_char, ) -> core::result::Result, crate::Error> { - let loop_ = options.windows.loop_.platform_event_loop(); - // All stdio inherited: the child is the foreground program on our console. - let _child = crate::ctrl_c::Child::enter(); - let mut spawned = - match spawn_process_windows(&options.to_spawn_options(false), argv, envp)? { - Err(err) => return Ok(Err(err)), - Ok(proces) => proces, - }; + use bun_spawn_sys::windows::win32; - // Sole owner during sync spawn; detached and released on return, - // by which time the loop has drained and no uv callback holds a - // competing `&mut Process`. - let process_handle = spawned.to_process_handle(()); - let process: *mut Process = process_handle.as_ptr(); - // SAFETY: just allocated; no other borrow live yet. - unsafe { - (*process).enable_keeping_event_loop_alive(); - } + // With no stdio captured the child is the foreground program on our console. + let _foreground = (options.stdin != SyncStdio::Buffer + && options.stdout != SyncStdio::Buffer + && options.stderr != SyncStdio::Buffer) + .then(crate::ctrl_c::Child::enter); - // SAFETY: read-only field access between uv ticks; the uv exit - // callback's `&mut Process` does not overlap this `&Process`. - while !unsafe { (*process).has_exited() } { - // SAFETY: `loop_` is the live `uws::WindowsLoop*` from - // `EventLoopHandle::platform_event_loop`. - unsafe { (*loop_).run() }; - } + // SAFETY: caller-built argv/envp; see `spawn_with_argv`. + let spawned = match unsafe { + spawn_process_windows(&options.to_spawn_options(false), argv, envp) + }? { + Err(err) => return Ok(Err(err)), + Ok(spawned) => spawned, + }; - Ok(Ok(Result { - // SAFETY: process has exited; no further mutation. - status: unsafe { (*process).status.clone() }, - stdout: Vec::new(), - stderr: Vec::new(), - })) - } + let process = spawned.process_handle.get(); - #[cfg(windows)] - fn spawn_windows_with_pipes( - options: &Options, - argv: *const *const c_char, - envp: *const *const c_char, - ) -> core::result::Result, crate::Error> { - let loop_: EventLoopHandle = options.windows.loop_; - let mut spawned = - match spawn_process_windows(&options.to_spawn_options(false), argv, envp)? { - Err(err) => return Ok(Err(err)), - Ok(process) => process, - }; - // Single-pointer ownership: the - // `heap::alloc` result is the *only* root for this allocation. Every - // field access below — including those inside uv callbacks fired from - // `tick()` — goes through `this_ptr`, so no Box auto-deref ever - // reasserts a Unique tag and pops the callbacks' tags under Stacked - // Borrows. - let this_ptr: *mut SyncWindowsProcess = - bun_core::heap::into_raw(SyncWindowsProcess::new(SyncWindowsProcess { - process: spawned.to_process(()).into_raw(), - stderr: Vec::new(), - stdout: Vec::new(), - err: bun_sys::E::SUCCESS, - waiting_count: 1, - status: None, - })); - // SAFETY: `(*this_ptr).process` was just produced by `to_process` (sole - // owner, mutable provenance from heap::alloc). - let _frame_ref = unsafe { RefPtr::init_ref((*this_ptr).process) }; - unsafe { - let p = &mut *(*this_ptr).process; - // SAFETY: `this_ptr` is the live `SyncWindowsProcess` on the - // caller's stack; `p` is owned by it and dropped before return. - p.set_exit_handler(ProcessExit::new(ProcessExitKind::SyncWindows, this_ptr)); - p.enable_keeping_event_loop_alive(); - } - - for (tag, stdio) in [ - (OutFd::Stdout, &mut spawned.stdout), - (OutFd::Stderr, &mut spawned.stderr), - ] { - // Move ownership of the `Box` out of `spawned` by - // resetting the slot to `Unavailable`; otherwise `spawned`'s - // auto-Drop at scope end would double-free the pipe already freed - // via `SyncWindowsPipeReader::on_close`. - let taken = core::mem::replace(stdio, WindowsStdioResult::Unavailable); - if let WindowsStdioResult::Buffer(pipe) = taken { - let reader = SyncWindowsPipeReader::new(SyncWindowsPipeReader { - context: this_ptr, - tag, - pipe, - chunks: Vec::new(), - pending_alloc: None, - err: bun_sys::E::SUCCESS, - on_done_callback: SyncWindowsProcess::on_reader_done, - }); - // SAFETY: sole owner via `this_ptr`; no uv callback has fired yet. - unsafe { - (*this_ptr).waiting_count += 1; - } - // `start` consumes the Box and transfers ownership to libuv - // via pipe.data (heap::alloc inside). - match reader.start() { - Err(err) => { - // SAFETY: sync spawn — `(*this_ptr).process` is the only - // handle and no uv callback has fired yet. - unsafe { - let _ = (*(*this_ptr).process).kill(1); - } - Output::panic(format_args!( - "Unexpected error starting {} pipe reader\n{}", - tag.as_str(), - err - )); + // Nothing is written to the child's stdin: end it. + if let Some(stdin) = spawned.stdin { + stdin.close(); + } + + let kill_child = || { + let _ = + bun_spawn_sys::windows::kill(process, bun_spawn_sys::windows::kill::SIGKILL); + }; + let mut drains = match ( + PipeDrain::new(spawned.stdout), + PipeDrain::new(spawned.stderr), + ) { + (Ok(stdout), Ok(stderr)) => [stdout, stderr], + (Err(err), _) | (_, Err(err)) => { + kill_child(); + return Ok(Err(err)); + } + }; + let drained: Maybe<()> = (|| { + for drain in &mut drains { + drain.read()?; + } + loop { + let mut events = [core::ptr::null_mut(); 2]; + let mut owners = [0usize; 2]; + let mut count = 0usize; + for (i, drain) in drains.iter().enumerate() { + if drain.pending { + events[count] = drain.event; + owners[count] = i; + count += 1; } - Ok(()) => {} } + if count == 0 { + return Ok(()); + } + // SAFETY: `events[..count]` are live event handles. + let signalled = unsafe { + win32::WaitForMultipleObjects( + count as u32, + events.as_ptr(), + 0, + win32::INFINITE, + ) + } as usize; + if signalled >= count { + return Err(win32::last_error(bun_sys::Tag::poll)); + } + drains[owners[signalled]].on_event()?; } + })(); + if let Err(err) = drained { + kill_child(); + return Ok(Err(err)); } - // SAFETY: read-only field access between uv ticks; callbacks fired - // inside `tick()` write through the same `this_ptr` root. - while unsafe { (*this_ptr).waiting_count } > 0 { - // SAFETY: `loop_` wraps a live `uws::WindowsLoop*`. - unsafe { (*loop_.platform_event_loop()).tick() }; + if win32::WaitForSingleObject(process, win32::INFINITE) != win32::WAIT_OBJECT_0 { + return Ok(Err(win32::last_error(bun_sys::Tag::waitpid))); + } + let mut exit_code: u32 = 0; + if win32::GetExitCodeProcess(process, &mut exit_code) == 0 { + return Ok(Err(win32::last_error(bun_sys::Tag::waitpid))); } - // SAFETY: loop drained (waiting_count == 0); no further uv callback - // will touch `this_ptr`. - let result = unsafe { - Result { - status: (*this_ptr) - .status - .take() - .expect("Expected Process to have exited when waiting_count == 0"), - stdout: flatten_owned_chunks(core::mem::take(&mut (*this_ptr).stdout)), - stderr: flatten_owned_chunks(core::mem::take(&mut (*this_ptr).stderr)), - } - }; - // SAFETY: reclaim the SyncWindowsProcess allocation; `_frame_ref` - // releases the process after. - drop(unsafe { bun_core::heap::take(this_ptr) }); - Ok(Ok(result)) + let [stdout, stderr] = &mut drains; + Ok(Ok(Result { + status: Status::Exited(Exited { + code: exit_code, + signal: 0, + }), + stdout: core::mem::take(&mut stdout.bytes), + stderr: core::mem::take(&mut stderr.bytes), + })) } pub fn spawn_with_argv( @@ -2826,15 +2050,7 @@ mod spawn_process_body { envp: *const *const c_char, ) -> core::result::Result, crate::Error> { #[cfg(windows)] - { - if options.stdin != SyncStdio::Buffer - && options.stderr != SyncStdio::Buffer - && options.stdout != SyncStdio::Buffer - { - return spawn_windows_without_pipes(options, argv, envp); - } - return spawn_windows_with_pipes(options, argv, envp); - } + return spawn_windows(options, argv, envp); #[cfg(unix)] spawn_posix(options, argv, envp) @@ -2843,7 +2059,11 @@ mod spawn_process_body { pub fn spawn(options: &Options) -> core::result::Result, crate::Error> { // SAFETY: `bun_sys::environ_ptr` returns the live, NULL-terminated C // `environ` array when no envp override is provided. + #[cfg(unix)] let envp: *const *const c_char = options.envp.unwrap_or_else(bun_sys::environ_ptr); + // A null block makes the child inherit this process's environment. + #[cfg(windows)] + let envp: *const *const c_char = options.envp.unwrap_or(core::ptr::null()); let argv = &options.argv; let mut string_builder = bun_core::StringBuilder::default(); for arg in argv { @@ -3357,7 +2577,7 @@ mod spawn_process_body { fn cleanup_spawn_posix( out: &mut [Vec; 2], out_fds: [Fd; 2], - process: &PosixSpawnResult, + process: &SpawnResult, success: bool, ) { // If we're going to return an error, diff --git a/src/spawn/static_pipe_writer.rs b/src/spawn/static_pipe_writer.rs index 68db15028e23..f485d010fb47 100644 --- a/src/spawn/static_pipe_writer.rs +++ b/src/spawn/static_pipe_writer.rs @@ -1,14 +1,12 @@ use core::mem::size_of; use bun_event_loop::EventLoopHandle; -#[cfg(windows)] -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; use bun_io::{BufferedWriter, WriteStatus}; use bun_ptr::{RawSlice, RefCount, RefPtr}; -use bun_sys; +use bun_sys::{self, Fd, FdExt as _}; use crate::process::StdioKind; -use crate::subprocess::{Source, StdioResult}; +use crate::subprocess::Source; bun_output::declare_scope!(StaticPipeWriter, hidden); @@ -16,7 +14,7 @@ bun_output::declare_scope!(StaticPipeWriter, hidden); /// /// This trait lets the /// generic `BufferedWriter>` field satisfy its -/// `PosixBufferedWriterParent`/`WindowsBufferedWriterParent` bound for all `P`. +/// parent bound for all `P`. /// /// Method takes `*mut Self` (not `&mut self`) because the writer is a field of /// the process — materializing `&mut P` while `&mut writer` is live would alias. @@ -42,7 +40,7 @@ pub struct StaticPipeWriter { /// Intrusive refcount; `ref`/`deref` provided via `bun_ptr::RefCount`. pub(crate) ref_count: RefCount, pub(crate) writer: IOWriter

, - pub(crate) stdio_result: StdioResult, + pub(crate) stdio_result: Option, pub source: Source, /// BACKREF: parent process is notified on close; never owned/destroyed here. pub(crate) process: *mut P, @@ -80,7 +78,6 @@ bun_io::impl_buffered_writer_parent! { // lifetime parameter. get_buffer = |this| &*(*this).buffer.as_ptr(), event_loop = |this| (*this).io_evtloop(), - uv_loop = |this| (*this).event_loop.uv_loop(), ref_ = |this| RefCount::::ref_(this), deref = |this| RefCount::::deref(this), } @@ -109,11 +106,10 @@ impl StaticPipeWriter

{ pub fn create( event_loop: EventLoopHandle, subprocess: *mut P, - result: StdioResult, + result: Option, source: Source, ) -> RefPtr { - #[allow(unused_mut)] - let mut boxed = Box::new(Self { + let boxed = Box::new(Self { ref_count: RefCount::init(), writer: IOWriter::

::default(), stdio_result: result, @@ -123,28 +119,6 @@ impl StaticPipeWriter

{ started: false, buffer: RawSlice::EMPTY, }); - #[cfg(windows)] - { - // On Windows `StdioResult` is the `WindowsStdioResult` union and - // the caller invariant is that the `Buffer` arm is set. Enforce - // that here: any other arm is a logic bug, not a silent no-op. - // Ownership of the boxed `uv::Pipe` transfers into the writer's - // `Source::Pipe`, so we move it out (replacing with `Unavailable`) - // and `heap::alloc` it (set_pipe re-wraps via `heap::take`). - use crate::process::WindowsStdioResult; - match core::mem::replace(&mut boxed.stdio_result, WindowsStdioResult::Unavailable) { - WindowsStdioResult::Buffer(pipe) => { - // SAFETY: `pipe` is a Box-allocated `uv::Pipe`; `set_pipe` - // takes ownership via `heap::take`. - unsafe { boxed.writer.set_pipe(bun_core::heap::into_raw(pipe)) }; - } - WindowsStdioResult::BufferFd(_) - | WindowsStdioResult::UnownedFd(_) - | WindowsStdioResult::Unavailable => { - unreachable!("StaticPipeWriter stdin requires WindowsStdioResult::Buffer"); - } - } - } let this = bun_core::heap::into_raw(boxed); // SAFETY: `this` was just leaked above; borrow scoped to registering // the parent backref. @@ -164,50 +138,31 @@ impl StaticPipeWriter

{ unsafe { RefCount::::ref_(std::ptr::from_mut::(self)) }; // Self-borrow into `self.source` — see `buffer` field invariant. self.buffer = RawSlice::new(self.source.slice()); - #[cfg(windows)] - { - let r = self.writer.start_with_current_pipe(); - self.started = r.is_ok(); - if r.is_err() { - // start() failed: `started` stays false so no release site - // fires — release start()'s `+1` here. - // SAFETY: `self` is the live `Self` we ref'd at the top of - // `start()`; the caller's `RefPtr` keeps it alive and - // `started` is false so no other site re-derefs. + let fd = self.stdio_result.unwrap(); + match self.writer.start(fd, true) { + bun_sys::Result::Err(err) => { + // The writer did not take `fd`; nothing else closes it. + self.stdio_result = None; + fd.close(); + // start() failed: `started` stays false so no release + // site fires — release start()'s `+1` here. + // SAFETY: `self` is the live `Self` we ref'd at the top + // of `start()`; the caller's `RefPtr` keeps it alive + // and `started` is false so no other site re-derefs. unsafe { RefCount::::deref(std::ptr::from_mut::(self)) }; + bun_sys::Result::Err(err) } - return r; - } - #[cfg(not(windows))] - { - use bun_sys::FdExt as _; - // On POSIX `StdioResult` is an `Option`. - let fd = self.stdio_result.unwrap(); - match self.writer.start(fd, true) { - bun_sys::Result::Err(err) => { - // The writer did not take `fd`; nothing else closes it. - self.stdio_result = None; - fd.close(); - // start() failed: `started` stays false so no release - // site fires — release start()'s `+1` here. - // SAFETY: `self` is the live `Self` we ref'd at the top - // of `start()`; the caller's `RefPtr` keeps it alive - // and `started` is false so no other site re-derefs. - unsafe { RefCount::::deref(std::ptr::from_mut::(self)) }; - bun_sys::Result::Err(err) - } - bun_sys::Result::Ok(()) => { - self.started = true; - #[cfg(unix)] - { - // `handle` is `PollOrFd` (enum); flag mutation goes - // through the FilePoll vtable shim. - if let Some(poll) = self.writer.handle.get_poll() { - poll.set_flag(bun_io::FilePollFlag::Socket); - } + bun_sys::Result::Ok(()) => { + self.started = true; + #[cfg(unix)] + { + // `handle` is `PollOrFd` (enum); flag mutation goes + // through the FilePoll vtable shim. + if let Some(poll) = self.writer.handle.get_poll() { + poll.set_flag(bun_io::FilePollFlag::Socket); } - bun_sys::Result::Ok(()) } + bun_sys::Result::Ok(()) } } } @@ -280,9 +235,9 @@ impl StaticPipeWriter

{ // for the lifetime of this writer (the process owns/outlives its stdio writers). unsafe { P::on_close_io(self.process, StdioKind::Stdin) }; if release_start_ref { - // SAFETY: token taken above. On POSIX this frees `self`: it is the - // last use here, and the writer's `close()` frames below do nothing - // after this callback. On Windows the in-flight write's ref outlives it. + // SAFETY: token taken above. This may free `self`: it is the last use + // here, and the writer's `close()` frames below do nothing after this + // callback (on Windows an in-flight write holds a ref of its own). unsafe { RefCount::::deref(std::ptr::from_mut::(self)) }; } } diff --git a/src/spawn_sys/Cargo.toml b/src/spawn_sys/Cargo.toml index 03e02092fc0b..ebf3eeaeada7 100644 --- a/src/spawn_sys/Cargo.toml +++ b/src/spawn_sys/Cargo.toml @@ -19,6 +19,3 @@ bun_sys.workspace = true bun_windows_sys.workspace = true # tier-0 no_std leaf, unconditional like bun_sys/bun_core: the un-cfg'd IoCounters alias in spawn_process.rs must resolve on non-Windows enumset.workspace = true strum.workspace = true - -[target.'cfg(windows)'.dependencies] -bun_libuv_sys.workspace = true diff --git a/src/spawn_sys/lib.rs b/src/spawn_sys/lib.rs index ec584d419ceb..47744ce91cbb 100644 --- a/src/spawn_sys/lib.rs +++ b/src/spawn_sys/lib.rs @@ -1,23 +1,23 @@ -//! `bun_spawn_sys` — raw OS process-spawn layer split out of `bun_spawn`. +//! `bun_spawn_sys` — the raw OS process-spawn layer under `bun_spawn`. //! -//! This crate owns everything that talks directly to the kernel/libuv to -//! create a child process and read its exit status, with **no** event-loop +//! This crate owns everything that talks directly to the kernel to create a +//! child process and read its exit status, with **no** event-loop //! integration: //! //! - `posix_spawn(2)` libc wrappers (`Actions`/`Attr`/`spawn_z`/`wait4`) //! - the `posix_spawn_bun` repr(C) request structs and FFI decl //! - `spawn_process_posix` (fd plumbing + `posix_spawn` call) -//! - `PosixSpawnOptions`/`PosixStdio`/`PosixSpawnResult`/`ExtraPipe`/ +//! - `spawn_process_windows` (`CreateProcessW`: image search, command line, +//! environment block, stdio handles and pipes, job) and `windows::kill` +//! - `SpawnOptions`/`Stdio`/`SpawnResult`/`ExtraPipe`/ //! `StdioKind`/`Dup2`/`Rusage` //! - signal-forwarding / no-orphans `extern "C"` decls //! //! Dependencies are deliberately leaf-only: `libc`, `bun_sys`, `bun_core`, -//! `bun_analytics`, and (Windows-only) `bun_libuv_sys`. There is **no** -//! `bun_event_loop`/`bun_io`/`bun_io`/`bun_threading` dependency — `Process`, +//! `bun_windows_sys` and `bun_analytics`. There is **no** +//! `bun_event_loop`/`bun_io`/`bun_threading` dependency — `Process`, //! `Poller`, `WaiterThread`, and the `sync` runner stay in `bun_spawn` and //! depend on this crate. -//! -//! See `docs/SPAWN_SYS_PROPOSAL.md` for the full crate-graph rationale. use core::ffi::c_char; @@ -34,10 +34,13 @@ pub use error::{Error, Result}; pub mod posix_spawn; /// `spawn_process_posix` + option/result structs + `Rusage`. -/// Split out of `src/spawn/process.rs`. #[path = "spawn_process.rs"] pub mod spawn_process; +/// `spawn_process_windows`, `kill`, and their parts. +#[cfg(windows)] +pub mod windows; + // ────────────────────────────────────────────────────────────────────────── // Canonical FFI type aliases for nullable C-string pointers (`*const c_char`) // @@ -75,9 +78,8 @@ const _: () = assert!( ); // ────────────────────────────────────────────────────────────────────────── -// Signal-forwarding / no-orphans FFI surface — moved down from -// `bun_spawn::process::sync` so the decls live next to `posix_spawn_bun`. -// `bun_spawn::sync` consumes these via `bun_spawn_sys::ffi::*`. +// Signal-forwarding / no-orphans FFI surface, declared next to +// `posix_spawn_bun`; `bun_spawn::sync` consumes it as `bun_spawn_sys::ffi::*`. // ────────────────────────────────────────────────────────────────────────── pub mod ffi { use core::ffi::c_int; @@ -114,7 +116,7 @@ pub mod ffi { // ────────────────────────────────────────────────────────────────────────── // Waiter-thread fallback flag — owned here so `spawn_process_posix` / -// `PosixSpawnResult::pifd_from_pid` can flip it without depending on +// `SpawnResult::pifd_from_pid` can flip it without depending on // `bun_threading`. `bun_spawn::process::WaiterThread` reads/writes through these. // ────────────────────────────────────────────────────────────────────────── pub mod waiter_thread_flag { @@ -139,7 +141,7 @@ pub mod waiter_thread_flag { // ────────────────────────────────────────────────────────────────────────── // `PR_SET_PDEATHSIG` default — `spawn_process_posix` consults this when -// `PosixSpawnOptions::linux_pdeathsig` is `None`. Storage lives here (lowest +// `SpawnOptions::linux_pdeathsig` is `None`. Storage lives here (lowest // tier that reads it); `bun_io::ParentDeathWatchdog::enable()` flips it on // from the main thread. `PR_SET_PDEATHSIG` is *thread*-scoped in the kernel, // so the default only applies when spawning from the same thread that armed @@ -187,11 +189,13 @@ pub mod pdeathsig { #[cfg(any(target_os = "linux", target_os = "android"))] pub use spawn_process::PidFdType; +#[cfg(windows)] +pub use spawn_process::process_rusage; #[cfg(unix)] pub use spawn_process::spawn_process_posix; -#[cfg(windows)] -pub use spawn_process::uv_getrusage; pub use spawn_process::{ - Dup2, ExtraPipe, PidT, PosixSpawnOptions, PosixSpawnResult, PosixStdio, Rusage, RusageFields, - StdioKind, rusage_zeroed, + Dup2, ExtraPipe, PidT, Rusage, RusageFields, SpawnOptions, SpawnResult, Stdio, StdioKind, + rusage_zeroed, }; +#[cfg(windows)] +pub use windows::{WindowsOptions, spawn_process_windows}; diff --git a/src/spawn_sys/posix_spawn.rs b/src/spawn_sys/posix_spawn.rs index 5a210eea3d7b..750cbe835cbe 100644 --- a/src/spawn_sys/posix_spawn.rs +++ b/src/spawn_sys/posix_spawn.rs @@ -34,10 +34,8 @@ mod darwin_spawn_np { } } -// `bun_sys::posix` currently exposes only `mode_t`/`S`/`E`/`errno()` (the -// MOVE_DOWN stub from `bun_errno`). Shim the remainder locally so this file -// is self-contained; delete in favour of `bun_sys::posix::*` once that module -// widens. +// `bun_sys::posix` exposes only `mode_t`/`S`/`E`/`errno()`; the rest of what +// this file needs is shimmed locally. use self::posix_compat::{Errno, errno, to_posix_path}; #[cfg(target_os = "macos")] use self::posix_compat::{errno_from_posix_spawn, mode_t}; @@ -90,10 +88,6 @@ mod posix_compat { } } -// MOVE_DOWN: this file was `src/runtime/api/bun/spawn.rs`; the `stdio` -// submodule (which depends on the JSC-tier `Subprocess`) stays in -// `bun_runtime::api::bun_spawn` and is not declared here. - pub mod bun_spawn { use super::*; use crate::Error; diff --git a/src/spawn_sys/spawn_process.rs b/src/spawn_sys/spawn_process.rs index d1cc73ec0371..080d0f23c9ab 100644 --- a/src/spawn_sys/spawn_process.rs +++ b/src/spawn_sys/spawn_process.rs @@ -1,6 +1,6 @@ -//! Raw `spawn_process_posix` + option/result types — split out of -//! `bun_spawn::process` so the fd/action plumbing has no event-loop -//! dependency. `Process`/`Poller`/`WaiterThread`/`sync` stay in `bun_spawn`. +//! Raw `spawn_process_posix` + option/result types (`spawn_process_windows` +//! is in `windows/`): the fd/action plumbing, with no event-loop dependency. +//! `Process`/`Poller`/`WaiterThread`/`sync` are in `bun_spawn`. #[cfg(any(target_os = "linux", target_os = "android"))] use core::ffi::CStr; @@ -31,7 +31,7 @@ use crate::{Argv, Envp}; #[cfg(unix)] pub type PidT = libc::pid_t; #[cfg(windows)] -pub type PidT = bun_libuv_sys::uv_pid_t; +pub type PidT = core::ffi::c_int; #[cfg(unix)] pub type FdT = libc::c_int; @@ -45,10 +45,8 @@ pub type PidFdType = FdT; // One Bun-level type per target. On every Unix this is `libc::rusage` — // the libc crate defines `rusage` on FreeBSD with // the standard `ru_*` field names, so no hand-rolled FreeBSD struct is -// needed. On Windows it is the value-typed `WinRusage` below (NOT -// `uv_rusage_t` — that is vendor FFI ABI in `bun_libuv_sys` and stays -// untouched; our Windows path fills `WinRusage` directly from Win32, not -// via libuv). +// needed. On Windows it is the value-typed `WinRusage` below, filled from +// the process handle. // ────────────────────────────────────────────────────────────────────────── #[derive(Default, Clone, Copy)] @@ -70,20 +68,10 @@ pub struct WinRusage { pub type IoCounters = bun_windows_sys::IO_COUNTERS; +/// Resource usage of the process behind `process`, running or exited. #[cfg(windows)] -unsafe extern "system" { - // IoCounters = bun_windows_sys::IO_COUNTERS (alias resolves, no change) - fn GetProcessIoCounters( - handle: bun_sys::windows::HANDLE, - counters: *mut IoCounters, - ) -> core::ffi::c_int; -} - -#[cfg(windows)] -pub fn uv_getrusage(process: &mut bun_libuv_sys::uv_process_t) -> WinRusage { - use core::ffi::c_void; +pub fn process_rusage(process: bun_sys::windows::HANDLE) -> WinRusage { let mut usage_info = Rusage::default(); - let process_pid: *mut c_void = process.process_handle; type WinTime = bun_sys::windows::FILETIME; // SAFETY: all-zero is a valid FILETIME (POD C struct) let mut starttime: WinTime = unsafe { bun_core::ffi::zeroed_unchecked() }; @@ -97,7 +85,7 @@ pub fn uv_getrusage(process: &mut bun_libuv_sys::uv_process_t) -> WinRusage { // SAFETY: FFI call with valid out-pointers if unsafe { bun_sys::windows::GetProcessTimes( - process_pid, + process, &mut starttime, &mut exittime, &mut kerneltime, @@ -126,11 +114,11 @@ pub fn uv_getrusage(process: &mut bun_libuv_sys::uv_process_t) -> WinRusage { } let mut counters = IoCounters::default(); // SAFETY: FFI call with valid out-pointer - let _ = unsafe { GetProcessIoCounters(process_pid, &mut counters) }; + let _ = unsafe { crate::windows::win32::GetProcessIoCounters(process, &mut counters) }; usage_info.inblock = counters.ReadOperationCount; usage_info.oublock = counters.WriteOperationCount; - let Ok(memory) = bun_sys::windows::GetProcessMemoryInfo(process_pid) else { + let Ok(memory) = bun_sys::windows::GetProcessMemoryInfo(process) else { return usage_info; }; usage_info.maxrss = memory.PeakWorkingSetSize as u64; @@ -304,21 +292,27 @@ impl RusageFields for WinRusage { // Spawn option / result types // ────────────────────────────────────────────────────────────────────────── -pub struct PosixSpawnOptions { - pub stdin: PosixStdio, - pub stdout: PosixStdio, - pub stderr: PosixStdio, +pub struct SpawnOptions { + pub stdin: Stdio, + pub stdout: Stdio, + pub stderr: Stdio, + /// POSIX: an fd of this process the child keeps as is (the IPC channel a + /// parent gave this process, passed on). pub ipc: Option, - pub extra_fds: Box<[PosixStdio]>, + pub extra_fds: Box<[Stdio]>, pub cwd: Box<[u8]>, pub detached: bool, /// Run the child as this user id (`setuid` after fork, like libuv/Node). pub uid: Option, /// Run the child as this group id (`setgid` after fork, like libuv/Node). pub gid: Option, + #[cfg(windows)] + pub windows: crate::windows::WindowsOptions, pub argv0: Option<*const c_char>, + /// Linux: when false, a `Buffer` stdout/stderr may be a memfd instead of a + /// pipe. pub stream: bool, - pub sync: bool, + /// Linux: the caller blocks in `wait4` for this child, so no pidfd is opened. pub can_block_entire_thread_to_reduce_cpu_usage_in_fast_path: bool, /// Apple Extension: If this bit is set, rather /// than returning to the caller, posix_spawn(2) @@ -337,7 +331,13 @@ pub struct PosixSpawnOptions { /// Not exposed to JS yet. pub new_process_group: bool, /// PTY slave fd for controlling terminal setup (-1 if not using PTY). + #[cfg(not(windows))] pub pty_slave_fd: i32, + /// Windows ConPTY handle. When set, the child is attached to the + /// pseudoconsole, which provides its stdin/stdout/stderr; the stdio + /// options are not used. + #[cfg(windows)] + pub pseudoconsole: Option, /// Linux only. When non-null, the child sets PR_SET_PDEATHSIG to this /// signal between vfork and exec in posix_spawn_bun, so the kernel kills /// it when the spawning thread dies. When null, defaults to SIGKILL if @@ -349,40 +349,36 @@ pub struct PosixSpawnOptions { pub cgroup_fd: Option, } -impl Default for PosixSpawnOptions { +impl Default for SpawnOptions { fn default() -> Self { Self { - stdin: PosixStdio::Inherit, - stdout: PosixStdio::Inherit, - stderr: PosixStdio::Inherit, + stdin: Stdio::Inherit, + stdout: Stdio::Inherit, + stderr: Stdio::Inherit, ipc: None, extra_fds: Box::default(), cwd: Box::default(), detached: false, uid: None, gid: None, + #[cfg(windows)] + windows: Default::default(), argv0: None, stream: true, - sync: false, can_block_entire_thread_to_reduce_cpu_usage_in_fast_path: false, use_execve_on_macos: false, no_sigpipe: true, new_process_group: false, + #[cfg(not(windows))] pty_slave_fd: -1, + #[cfg(windows)] + pseudoconsole: None, linux_pdeathsig: None, cgroup_fd: None, } } } -impl PosixSpawnOptions { - /// No-op. - /// Exists for cfg-parity with `WindowsSpawnOptions::deinit`, which closes - /// heap-allocated `uv::Pipe` handles on the spawn error path. - #[inline] - pub fn deinit(&mut self) {} -} - /// `bun.jsc.Subprocess.StdioKind` — defined here (not in `subprocess`) to keep /// the spawn-sys layer leaf. Re-exported up through `bun_spawn::process` → /// `bun_runtime::api::{bun_process, JscSubprocess}` → `shell::subproc`. @@ -416,48 +412,68 @@ pub struct Dup2 { pub to: StdioKind, } -pub enum PosixStdio { +pub enum Stdio { Path(Box<[u8]>), Inherit, Ignore, Buffer, - /// Like `Buffer` at indices >= 3 (creates a socketpair) but the parent - /// end is returned as `ExtraPipe::UnownedFd`: the caller owns and closes - /// it. Only valid in `extra_fds`. + /// Like `Buffer` at indices >= 3 (a socketpair; on Windows a duplex pipe) + /// but the caller takes ownership of the parent end once it has read it + /// from the result. Only valid in `extra_fds`. SocketFd, Ipc, Pipe(Fd), - // TODO: remove this entry, it doesn't seem to be used Dup2(Dup2), } -impl PosixStdio { - // Constructor helpers — keep parity with `WindowsStdio::buffer(*mut Pipe)` - // so `sync::SyncStdio::to_stdio` can spell both platforms uniformly. - #[inline] - pub fn inherit() -> Self { - PosixStdio::Inherit +/// A process handle, closed when dropped. +#[cfg(windows)] +pub struct OwnedProcessHandle(bun_sys::windows::HANDLE); + +#[cfg(windows)] +impl OwnedProcessHandle { + pub fn new(handle: bun_sys::windows::HANDLE) -> Self { + Self(handle) } - #[inline] - pub fn ignore() -> Self { - PosixStdio::Ignore + + pub fn get(&self) -> bun_sys::windows::HANDLE { + self.0 } - #[inline] - pub fn buffer() -> Self { - PosixStdio::Buffer + + /// The handle, now the caller's to close. + pub fn take(&mut self) -> bun_sys::windows::HANDLE { + core::mem::replace(&mut self.0, bun_sys::windows::INVALID_HANDLE_VALUE) + } +} + +#[cfg(windows)] +impl Default for OwnedProcessHandle { + fn default() -> Self { + Self(bun_sys::windows::INVALID_HANDLE_VALUE) + } +} + +#[cfg(windows)] +impl Drop for OwnedProcessHandle { + fn drop(&mut self) { + if self.0 != bun_sys::windows::INVALID_HANDLE_VALUE { + // SAFETY: a process handle this value owns. + unsafe { crate::windows::win32::CloseHandle(self.0) }; + } } } #[derive(Default)] -pub struct PosixSpawnResult { +pub struct SpawnResult { pub pid: PidT, #[cfg(any(target_os = "linux", target_os = "android"))] pub pidfd: Option, + /// `to_process` moves it into the `Process`. + #[cfg(windows)] + pub process_handle: OwnedProcessHandle, pub stdin: Option, pub stdout: Option, pub stderr: Option, - #[cfg(not(windows))] - pub(crate) ipc: Option, pub extra_pipes: Vec, pub memfds: [bool; 3], // ESRCH can happen when requesting the pidfd @@ -466,7 +482,7 @@ pub struct PosixSpawnResult { /// Entry in `extra_pipes` for a stdio slot at index >= 3. pub enum ExtraPipe { - /// We created this fd (e.g. socketpair for `"pipe"`); `finalizeStreams` + /// We created this fd (e.g. socketpair for `"pipe"`); `finalize_streams` /// closes it. Downgraded to `UnownedFd` once `.stdio` is read (the caller /// then owns the raw number and is responsible for closing it). OwnedFd(Fd), @@ -487,7 +503,7 @@ impl ExtraPipe { } } -impl PosixSpawnResult { +impl SpawnResult { #[cfg(any(target_os = "linux", target_os = "android"))] fn pidfd_flags_for_linux() -> u32 { // PIDFD_NONBLOCK is only supported on kernel 5.10+ (the EINVAL retry @@ -625,10 +641,10 @@ impl Drop for PosixSpawnFdGuard { /// for the duration of the call. #[cfg(unix)] pub unsafe fn spawn_process_posix( - options: &PosixSpawnOptions, + options: &SpawnOptions, argv: Argv, envp: Envp, -) -> crate::Result> { +) -> crate::Result> { bun_analytics::features::spawn.fetch_add(1, Ordering::Relaxed); let mut actions = PosixSpawnActions::init()?; @@ -654,9 +670,9 @@ pub unsafe fn spawn_process_posix( if options.use_execve_on_macos { f |= POSIX_SPAWN_SETEXEC; - if matches!(options.stdin, PosixStdio::Buffer) - || matches!(options.stdout, PosixStdio::Buffer) - || matches!(options.stderr, PosixStdio::Buffer) + if matches!(options.stdin, Stdio::Buffer) + || matches!(options.stdout, Stdio::Buffer) + || matches!(options.stderr, Stdio::Buffer) { Output::panic(format_args!( "Internal error: stdin, stdout, and stderr cannot be buffered when use_execve_on_macos is true", @@ -707,7 +723,7 @@ pub unsafe fn spawn_process_posix( if !options.cwd.is_empty() { actions.chdir(&options.cwd)?; } - let mut spawned = PosixSpawnResult::default(); + let mut spawned = SpawnResult::default(); let mut extra_fds: Vec = Vec::new(); // Cleanup is owned by an RAII guard so every `?` // (and every explicit `return Ok(Err(..))`) runs it. See PosixSpawnFdGuard. @@ -731,13 +747,12 @@ pub unsafe fn spawn_process_posix( if let Some(ipc) = options.ipc { actions.inherit(ipc)?; - spawned.ipc = Some(ipc); } - let stdio_options: [&PosixStdio; 3] = [&options.stdin, &options.stdout, &options.stderr]; + let stdio_options: [&Stdio; 3] = [&options.stdin, &options.stdout, &options.stderr]; // Probed before this function creates any fd: a socketpair end can land on a closed slot number and make that slot look open. - let inherits_closed_fd = |stdio: &PosixStdio, slot: usize| { - matches!(stdio, PosixStdio::Inherit) + let inherits_closed_fd = |stdio: &Stdio, slot: usize| { + matches!(stdio, Stdio::Inherit) && bun_sys::get_fcntl_flags(Fd::from_native(slot as FdT)).is_err() }; let closed_stdio: [bool; 3] = core::array::from_fn(|i| inherits_closed_fd(stdio_options[i], i)); @@ -770,7 +785,7 @@ pub unsafe fn spawn_process_posix( }) as u32; match stdio_options[i] { - PosixStdio::Dup2(dup2) => { + Stdio::Dup2(dup2) => { // This is a hack to get around the ordering of the spawn actions. // If stdout is set so that it redirects to stderr, the order of actions will be like this: // 0. dup2(stderr, stdout) - this makes stdout point to stderr @@ -784,7 +799,7 @@ pub unsafe fn spawn_process_posix( actions.dup2(dup2.to.to_fd(), dup2.out.to_fd())?; } } - PosixStdio::Inherit => { + Stdio::Inherit => { // A closed slot would inherit whatever fd is created later at that number (e.g. the ipc socketpair); libuv gives it /dev/null. if closed_stdio[i] { actions.open_z(fileno, c"/dev/null", flag | bun_sys::O::CREAT as u32, 0o664)?; @@ -794,13 +809,13 @@ pub unsafe fn spawn_process_posix( actions.inherit(fileno)?; } } - PosixStdio::Ipc | PosixStdio::Ignore => { + Stdio::Ipc | Stdio::Ignore => { actions.open_z(fileno, c"/dev/null", flag | bun_sys::O::CREAT as u32, 0o664)?; } - PosixStdio::Path(path) => { + Stdio::Path(path) => { actions.open(fileno, path, flag | bun_sys::O::CREAT as u32, 0o664)?; } - PosixStdio::Buffer => { + Stdio::Buffer => { #[cfg(any(target_os = "linux", target_os = "android"))] 'use_memfd: { if !options.stream && i > 0 && bun_sys::can_use_memfd() { @@ -899,10 +914,8 @@ pub unsafe fn spawn_process_posix( cleanup.to_close_at_end.push(fds[1]); cleanup.to_close_on_error.push(fds[0]); - if !options.sync { - if let Err(e) = bun_sys::set_nonblocking(fds[0]) { - return Ok(Err(e)); - } + if let Err(e) = bun_sys::set_nonblocking(fds[0]) { + return Ok(Err(e)); } let src = match cleanup.source_above_slots(max_slot, fds[1]) { @@ -916,7 +929,7 @@ pub unsafe fn spawn_process_posix( set_spawned_stdio(&mut spawned, i, fds[0]); } - PosixStdio::Pipe(fd) => { + Stdio::Pipe(fd) => { let _ = bun_sys::update_nonblocking(*fd, false); let src = match cleanup.source_above_slots(max_slot, *fd) { Ok(src) => src, @@ -925,7 +938,7 @@ pub unsafe fn spawn_process_posix( actions.dup2(src, fileno)?; set_spawned_stdio(&mut spawned, i, *fd); } - PosixStdio::SocketFd => { + Stdio::SocketFd => { // Rejected at i < 3 in Stdio::extract(); unreachable here. unreachable!("SocketFd at stdin/stdout/stderr"); } @@ -933,7 +946,7 @@ pub unsafe fn spawn_process_posix( } if dup_stdout_to_stderr { - if let PosixStdio::Dup2(d) = stdio_options[1] { + if let Stdio::Dup2(d) = stdio_options[1] { actions.dup2(d.to.to_fd(), d.out.to_fd())?; } } @@ -942,19 +955,19 @@ pub unsafe fn spawn_process_posix( let fileno = Fd::from_native(FdT::try_from(3 + i).unwrap()); match ipc { - PosixStdio::Dup2(_) => panic!("TODO dup2 extra fd"), - PosixStdio::Inherit => { + Stdio::Dup2(_) => panic!("TODO dup2 extra fd"), + Stdio::Inherit => { actions.inherit(fileno)?; extra_fds.push(ExtraPipe::Unavailable); } - PosixStdio::Ignore => { + Stdio::Ignore => { // Node leaves "ignore" at an extra slot closed in the child; // only fds 0-2 need a /dev/null placeholder. Close explicitly: // the post-exec close-range floor only covers higher fds. actions.close(fileno)?; extra_fds.push(ExtraPipe::Unavailable); } - PosixStdio::Path(path) => { + Stdio::Path(path) => { actions.open( fileno, path, @@ -963,7 +976,7 @@ pub unsafe fn spawn_process_posix( )?; extra_fds.push(ExtraPipe::Unavailable); } - PosixStdio::Ipc | PosixStdio::Buffer | PosixStdio::SocketFd => { + Stdio::Ipc | Stdio::Buffer | Stdio::SocketFd => { // Only the parent's end goes nonblocking: a child that is not bun or node does a plain write(2) on its end, and libuv hands out a blocking one too. let fds: [Fd; 2] = match bun_sys::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, false) { @@ -974,10 +987,8 @@ pub unsafe fn spawn_process_posix( cleanup.to_close_at_end.push(fds[1]); cleanup.to_close_on_error.push(fds[0]); - if !options.sync { - if let Err(e) = bun_sys::set_nonblocking(fds[0]) { - return Ok(Err(e)); - } + if let Err(e) = bun_sys::set_nonblocking(fds[0]) { + return Ok(Err(e)); } let src = match cleanup.source_above_slots(max_slot, fds[1]) { @@ -997,7 +1008,7 @@ pub unsafe fn spawn_process_posix( // js_bun_spawn_bindings.rs after all fallible init succeeds. extra_fds.push(ExtraPipe::OwnedFd(fds[0])); } - PosixStdio::Pipe(fd) => { + Stdio::Pipe(fd) => { let src = match cleanup.source_above_slots(max_slot, *fd) { Ok(src) => src, Err(e) => return Ok(Err(e)), @@ -1005,7 +1016,7 @@ pub unsafe fn spawn_process_posix( actions.dup2(src, fileno)?; // The fd was supplied by the caller (a number in the stdio array) and is // not owned by us. Record it so `stdio[N]` returns the caller's fd, but - // mark it unowned so finalizeStreams leaves it open. + // mark it unowned so `finalize_streams` leaves it open. extra_fds.push(ExtraPipe::UnownedFd(*fd)); } } @@ -1059,7 +1070,7 @@ pub unsafe fn spawn_process_posix( } #[cfg(unix)] -fn set_spawned_stdio(spawned: &mut PosixSpawnResult, i: usize, fd: Fd) { +fn set_spawned_stdio(spawned: &mut SpawnResult, i: usize, fd: Fd) { match i { 0 => spawned.stdin = Some(fd), 1 => spawned.stdout = Some(fd), diff --git a/src/spawn_sys/windows/args.rs b/src/spawn_sys/windows/args.rs new file mode 100644 index 000000000000..ad5c00898aac --- /dev/null +++ b/src/spawn_sys/windows/args.rs @@ -0,0 +1,67 @@ +//! The child's command line. + +/// Appends `s` (WTF-8) as UTF-16. An unpaired surrogate is kept; an invalid +/// byte becomes U+FFFD. +pub fn push_wtf8(out: &mut Vec, s: &[u8]) { + let start = out.len(); + // Every UTF-16 unit consumes at least one input byte. + out.resize(start + s.len(), 0); + let written = bun_core::strings::convert_utf8_to_utf16_in_buffer(&mut out[start..], s).len(); + out.truncate(start + written); +} + +/// Appends `arg` quoted so that `CommandLineToArgvW` and the MSVCRT/UCRT +/// `argv` parser give it back unchanged: quotes only around an empty argument +/// or one containing a space, a tab or `"`; `"` becomes `\"`; a run of `n` +/// backslashes is doubled only where a `"` follows it (the embedded one or the +/// closing one). +fn quote_arg(out: &mut Vec, arg: &[u16]) { + const QUOTE: u16 = b'"' as u16; + const BACKSLASH: u16 = b'\\' as u16; + + let needs_quotes = arg.is_empty() + || arg + .iter() + .any(|&c| c == b' ' as u16 || c == b'\t' as u16 || c == QUOTE); + if !needs_quotes { + out.extend_from_slice(arg); + return; + } + + out.push(QUOTE); + let mut backslashes = 0usize; + for &c in arg { + if c == BACKSLASH { + backslashes += 1; + } else { + if c == QUOTE { + out.extend(core::iter::repeat_n(BACKSLASH, backslashes + 1)); + } + backslashes = 0; + } + out.push(c); + } + out.extend(core::iter::repeat_n(BACKSLASH, backslashes)); + out.push(QUOTE); +} + +/// The NUL-terminated command line for `args` (WTF-8), joined with single +/// spaces. `verbatim` copies every argument as is. +pub fn make_command_line<'a>(args: impl Iterator, verbatim: bool) -> Vec { + let mut out: Vec = Vec::new(); + let mut scratch: Vec = Vec::new(); + for (i, arg) in args.enumerate() { + if i != 0 { + out.push(b' ' as u16); + } + if verbatim { + push_wtf8(&mut out, arg); + } else { + scratch.clear(); + push_wtf8(&mut scratch, arg); + quote_arg(&mut out, &scratch); + } + } + out.push(0); + out +} diff --git a/src/spawn_sys/windows/env.rs b/src/spawn_sys/windows/env.rs new file mode 100644 index 000000000000..622e56e70e60 --- /dev/null +++ b/src/spawn_sys/windows/env.rs @@ -0,0 +1,180 @@ +//! The child's environment block: UTF-16 `NAME=value\0` strings sorted by name +//! the way Windows sorts them, terminated by an empty string. + +use core::cmp::Ordering; + +use bun_core::wstr; + +/// Variables a child gets from this process when its environment does not +/// define them: Winsock and the legacy CryptoAPI fail to initialize without +/// `SYSTEMROOT`, several APIs read `TEMP`, and Cygwin-based programs need the +/// rest. NUL-terminated, sorted. +static REQUIRED_VARS: [&[u16]; 11] = [ + wstr!("HOMEDRIVE"), + wstr!("HOMEPATH"), + wstr!("LOGONSERVER"), + wstr!("PATH"), + wstr!("SYSTEMDRIVE"), + wstr!("SYSTEMROOT"), + wstr!("TEMP"), + wstr!("USERDOMAIN"), + wstr!("USERNAME"), + wstr!("USERPROFILE"), + wstr!("WINDIR"), +]; + +/// `PATH`, NUL-terminated. +pub static PATH: &[u16] = wstr!("PATH"); + +struct Entry { + start: usize, + /// Length of the name: up to the first `=`, so `=C:=C:\dir` has an empty name. + name_len: usize, + end: usize, +} + +/// Builds the block from `entries` (WTF-8 `NAME=value`). +/// +/// Entries without `=` are dropped (`CreateProcessW` rejects the whole block +/// otherwise). Of entries whose names compare equal the last one is kept, as +/// the last assignment to a variable is: a program is given the first one it +/// finds, and `{ ...process.env, PATH }` comes after the `Path` it replaces. +pub fn make_env_block<'a>(entries: impl Iterator) -> Vec { + let mut strings: Vec = Vec::new(); + let mut vars: Vec = Vec::new(); + for entry in entries { + if !bun_core::strings::contains_char(entry, b'=') { + continue; + } + let start = strings.len(); + super::args::push_wtf8(&mut strings, entry); + let end = strings.len(); + let name_len = strings[start..end] + .iter() + .position(|&c| c == b'=' as u16) + .unwrap_or(end - start); + vars.push(Entry { + start, + name_len, + end, + }); + } + + let name = |v: &Entry| &strings[v.start..v.start + v.name_len]; + vars.sort_by(|a, b| compare_names(name(a), name(b))); + // `=C:` and `=D:` are different variables with an empty name each. + let is_replaced_by = |v: &Entry, next: &Entry| { + v.name_len > 0 && compare_names(name(v), name(next)) == Ordering::Equal + }; + let vars: Vec<&Entry> = vars + .iter() + .enumerate() + .filter(|&(i, v)| !vars.get(i + 1).is_some_and(|next| is_replaced_by(v, next))) + .map(|(_, v)| v) + .collect(); + let mut block: Vec = Vec::with_capacity(strings.len() + vars.len() + 2); + let mut next_var = 0usize; + let mut next_required = 0usize; + while next_var < vars.len() || next_required < REQUIRED_VARS.len() { + let order = if next_required >= REQUIRED_VARS.len() { + Ordering::Greater + } else if next_var >= vars.len() { + Ordering::Less + } else { + let required = REQUIRED_VARS[next_required]; + compare_names(&required[..required.len() - 1], name(vars[next_var])) + }; + if order == Ordering::Less { + let required = REQUIRED_VARS[next_required]; + let rollback = block.len(); + block.extend_from_slice(&required[..required.len() - 1]); + block.push(b'=' as u16); + if parent_value(required, &mut block) { + block.push(0); + } else { + block.truncate(rollback); + } + next_required += 1; + } else { + let var = vars[next_var]; + block.extend_from_slice(&strings[var.start..var.end]); + block.push(0); + next_var += 1; + if order == Ordering::Equal { + next_required += 1; + } + } + } + // "A Unicode environment block is terminated by four zero bytes" + // (`CreateProcessW`): the last string's and the block's, or both the + // block's when it has no strings. + if block.is_empty() { + block.push(0); + } + block.push(0); + block +} + +/// The value of the `PATH=` entry (any case) of a block. +pub fn find_path(block: &[u16]) -> Option<&[u16]> { + let mut rest = block; + while !rest.is_empty() && rest[0] != 0 { + let len = bun_core::strings::index_of_any16(rest, &[0]).unwrap_or(rest.len()); + let entry = &rest[..len]; + if entry.len() >= 5 + && (entry[0] == b'P' as u16 || entry[0] == b'p' as u16) + && (entry[1] == b'A' as u16 || entry[1] == b'a' as u16) + && (entry[2] == b'T' as u16 || entry[2] == b't' as u16) + && (entry[3] == b'H' as u16 || entry[3] == b'h' as u16) + && entry[4] == b'=' as u16 + { + return Some(&entry[5..]); + } + rest = rest.get(len + 1..).unwrap_or(&[]); + } + None +} + +/// Name order of an environment block: `CompareStringOrdinal` ignoring case, +/// the same order the kernel keeps a process's block in (it compares +/// upper-cased, so `_` sorts after the letters, unlike `_wcsicmp`). +fn compare_names(a: &[u16], b: &[u16]) -> Ordering { + use super::win32; + // Names here are slices of one spawn's arguments, far below `c_int::MAX`. + // SAFETY: both pointers are valid for the given lengths. + let r = unsafe { + win32::CompareStringOrdinal(a.as_ptr(), a.len() as i32, b.as_ptr(), b.len() as i32, 1) + }; + r.cmp(&win32::CSTR_EQUAL) +} + +/// Appends this process's value of `name` (NUL-terminated); false when unset. +pub fn parent_value(name: &[u16], out: &mut Vec) -> bool { + use super::win32; + debug_assert_eq!(name.last(), Some(&0)); + let start = out.len(); + let mut capacity: usize = 256; + loop { + out.resize(start + capacity, 0); + win32::SetLastError(0); + // SAFETY: `name` is NUL-terminated; `out[start..]` holds `capacity` units. + let n = unsafe { + win32::GetEnvironmentVariableW( + name.as_ptr(), + out[start..].as_mut_ptr(), + capacity as win32::DWORD, + ) + } as usize; + if n == 0 { + out.truncate(start); + // 0 is also the length of a variable set to the empty string. + return win32::GetLastError() == 0; + } + if n < capacity { + out.truncate(start + n); + return true; + } + // Too small: `n` is the size needed, including the terminator. + capacity = n; + } +} diff --git a/src/spawn_sys/windows/job.rs b/src/spawn_sys/windows/job.rs new file mode 100644 index 000000000000..d56b0eee50d6 --- /dev/null +++ b/src/spawn_sys/windows/job.rs @@ -0,0 +1,102 @@ +//! The job that ends children with this process. + +use core::ptr; +use std::sync::OnceLock; + +use super::win32::{self, DWORD, HANDLE}; + +struct Job(Result); +// SAFETY: a job handle may be used from any thread. +unsafe impl Send for Job {} +// SAFETY: see above. +unsafe impl Sync for Job {} + +static GLOBAL_JOB: OnceLock = OnceLock::new(); + +/// The process-wide job every child that is not detached is created in. +/// +/// The handle is not inheritable and never closed, so this process ending, for +/// any reason, is what closes the job, and `KILL_ON_JOB_CLOSE` then ends the +/// children in it. `SILENT_BREAKAWAY_OK` keeps *their* children out of the +/// job: only processes put in explicitly are members, so children stay free to +/// use jobs themselves on systems without nested jobs and to start detached +/// processes. `DIE_ON_UNHANDLED_EXCEPTION` suppresses the crash dialog for +/// members. +/// +/// Null when no child of this process can be a member. +pub fn global_job() -> Result { + GLOBAL_JOB.get_or_init(|| Job(create())).0 +} + +fn create() -> Result { + // SAFETY: null attributes (not inheritable) and no name. + let job = unsafe { win32::CreateJobObjectW(ptr::null_mut(), ptr::null()) }; + if job.is_null() { + return Err(win32::GetLastError()); + } + let close = |err: DWORD| { + // SAFETY: `job` is a handle this function owns. + unsafe { win32::CloseHandle(job) }; + err + }; + + let mut info: win32::JOBOBJECT_EXTENDED_LIMIT_INFORMATION = bun_core::ffi::zeroed(); + info.BasicLimitInformation.LimitFlags = win32::JOB_OBJECT_LIMIT_BREAKAWAY_OK + | win32::JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK + | win32::JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION + | win32::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + // SAFETY: `info` is the structure `JobObjectExtendedLimitInformation` takes. + if unsafe { + win32::SetInformationJobObject( + job, + win32::JobObjectExtendedLimitInformation, + ptr::from_mut(&mut info).cast(), + size_of::() as DWORD, + ) + } == 0 + { + return Err(close(win32::GetLastError())); + } + + // If the first process put in a job is a Windows Store program, every + // later use of the handle fails with ERROR_INVALID_PARAMETER, seemingly + // because a job is tied to the session of its first member. Joining it + // ourselves first ties it to ours. + // SAFETY: both handles are valid. + if unsafe { win32::AssignProcessToJobObject(job, win32::GetCurrentProcess()) } == 0 { + let err = win32::GetLastError(); + if err != win32::ERROR_ACCESS_DENIED { + return Err(close(err)); + } + // The jobs this process is in do not let this one nest below them. A + // child that is born in all of them is refused the same way. + if children_stay_in_our_jobs() { + close(err); + return Ok(ptr::null_mut()); + } + } + + Ok(job) +} + +/// Whether a child is born in every job this process is in. It is unless the +/// innermost of them lets children break away silently; how far up such a +/// child leaves cannot be asked, so whether it may join is only known by +/// creating it. `CREATE_BREAKAWAY_FROM_JOB` is never passed, so +/// `BREAKAWAY_OK` changes nothing. +fn children_stay_in_our_jobs() -> bool { + let mut info: win32::JOBOBJECT_EXTENDED_LIMIT_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: a null job is the innermost job of the calling process; `info` + // is the structure `JobObjectExtendedLimitInformation` fills. + let queried = unsafe { + win32::QueryInformationJobObject( + ptr::null_mut(), + win32::JobObjectExtendedLimitInformation, + ptr::from_mut(&mut info).cast(), + size_of::() as DWORD, + ptr::null_mut(), + ) + } != 0; + queried + && info.BasicLimitInformation.LimitFlags & win32::JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK == 0 +} diff --git a/src/spawn_sys/windows/kill.rs b/src/spawn_sys/windows/kill.rs new file mode 100644 index 000000000000..71b2a5dca1f9 --- /dev/null +++ b/src/spawn_sys/windows/kill.rs @@ -0,0 +1,318 @@ +//! Signals, as far as Windows has them. + +use core::ffi::{c_int, c_void}; +use core::ptr; + +use bun_sys::{E, Error, Tag}; + +use bun_windows_sys::{ + CREATE_NEW, CreateDirectoryW, FILE_ATTRIBUTE_NORMAL, GUID, HKEY, HKEY_LOCAL_MACHINE, + RegGetValueW, +}; + +use super::win32::{self, BOOL, DWORD, HANDLE, INVALID_HANDLE_VALUE}; + +pub const SIGINT: c_int = 2; +pub const SIGQUIT: c_int = 3; +pub const SIGKILL: c_int = 9; +pub const SIGTERM: c_int = 15; +/// One past the highest signal number (`SIGWINCH`, 28). +pub const NSIG: c_int = 29; + +/// Sends `signum` to the process behind `process`. +/// +/// `SIGTERM`, `SIGKILL`, `SIGINT` and `SIGQUIT` end it with exit code 1 +/// (`SIGQUIT` after writing a dump where the system collects dumps); 0 only +/// probes (`ESRCH` once it has exited); any other signal is `ENOSYS`, and a +/// number that is not a signal `EINVAL`. +pub fn kill(process: HANDLE, signum: c_int) -> Result<(), Error> { + if !(0..NSIG).contains(&signum) { + return Err(Error::from_code(E::EINVAL, Tag::kill)); + } + + match signum { + SIGQUIT | SIGTERM | SIGKILL | SIGINT => { + if signum == SIGQUIT { + write_minidump(process); + } + if win32::TerminateProcess(process, 1) != 0 { + return Ok(()); + } + let err = win32::GetLastError(); + // TerminateProcess on a process that has exited fails with + // ERROR_ACCESS_DENIED. + if err == win32::ERROR_ACCESS_DENIED { + let mut status: DWORD = 0; + if win32::GetExitCodeProcess(process, &mut status) != 0 + && status != win32::STILL_ACTIVE + { + return Err(Error::from_code(E::ESRCH, Tag::kill)); + } + // 259 is also a legal exit code, so ask the handle. It may + // not be signalled yet for a process that only just exited, + // which then reports EPERM instead of ESRCH. + if win32::WaitForSingleObject(process, 0) == win32::WAIT_OBJECT_0 { + return Err(Error::from_code(E::ESRCH, Tag::kill)); + } + } + Err(win32::sys_error(err, Tag::kill)) + } + 0 => { + let mut status: DWORD = 0; + if win32::GetExitCodeProcess(process, &mut status) == 0 { + return Err(win32::last_error(Tag::kill)); + } + if status != win32::STILL_ACTIVE { + return Err(Error::from_code(E::ESRCH, Tag::kill)); + } + match win32::WaitForSingleObject(process, 0) { + win32::WAIT_OBJECT_0 => Err(Error::from_code(E::ESRCH, Tag::kill)), + win32::WAIT_TIMEOUT => Ok(()), + win32::WAIT_FAILED => Err(win32::last_error(Tag::kill)), + _ => Err(Error::from_code(E::EUNKNOWN, Tag::kill)), + } + } + _ => Err(Error::from_code(E::ENOSYS, Tag::kill)), + } +} + +/// [`kill`] by process id. Pid 0 is this process, not a process group; a pid +/// that names no process is `ESRCH`. +pub fn kill_pid(pid: c_int, signum: c_int) -> Result<(), Error> { + if pid == 0 { + return kill(win32::GetCurrentProcess(), signum); + } + // SAFETY: by-value arguments only. + let process = unsafe { + win32::OpenProcess( + win32::PROCESS_TERMINATE | win32::PROCESS_QUERY_INFORMATION | win32::SYNCHRONIZE, + 0, + pid as DWORD, + ) + }; + if process.is_null() { + return Err(match win32::GetLastError() { + win32::ERROR_INVALID_PARAMETER => Error::from_code(E::ESRCH, Tag::kill), + err => win32::sys_error(err, Tag::kill), + }); + } + let result = kill(process, signum); + // SAFETY: `process` is a handle this function owns. + unsafe { win32::CloseHandle(process) }; + result +} + +const KEY_QUERY_VALUE: DWORD = 0x0001; +const RRF_RT_ANY: DWORD = 0x0000_FFFF; +const FILE_DISPOSITION_INFO_CLASS: c_int = 4; +const MINIDUMP_WITH_FULL_MEMORY: DWORD = 0x0000_0002; +const MINIDUMP_IGNORE_INACCESSIBLE_MEMORY: DWORD = 0x0002_0000; +const MINIDUMP_WITH_AVX_XSTATE_CONTEXT: DWORD = 0x0020_0000; +/// Makes Wine include ELF modules in a dump. +const SYMOPT_WINE_WITH_NATIVE_MODULES: DWORD = 0x4000_0000; + +const FOLDERID_LOCAL_APP_DATA: GUID = GUID { + Data1: 0xf1b3_2785, + Data2: 0x6fba, + Data3: 0x4fcf, + Data4: [0x9d, 0x55, 0x7b, 0x8e, 0x7f, 0x15, 0x70, 0x91], +}; + +unsafe extern "system" { + fn RegOpenKeyExW( + hKey: HKEY, + lpSubKey: *const u16, + ulOptions: DWORD, + samDesired: DWORD, + phkResult: *mut HKEY, + ) -> i32; + fn RegCloseKey(hKey: HKEY) -> i32; + fn SHGetKnownFolderPath( + rfid: *const GUID, + dwFlags: DWORD, + hToken: HANDLE, + ppszPath: *mut *mut u16, + ) -> i32; + fn CoTaskMemFree(pv: *mut c_void); + fn K32GetModuleBaseNameW( + hProcess: HANDLE, + hModule: *mut c_void, + lpBaseName: *mut u16, + nSize: DWORD, + ) -> DWORD; + fn SetFileInformationByHandle( + hFile: HANDLE, + FileInformationClass: c_int, + lpFileInformation: *const c_void, + dwBufferSize: DWORD, + ) -> BOOL; + fn SymGetOptions() -> DWORD; + fn SymSetOptions(SymOptions: DWORD) -> DWORD; + fn MiniDumpWriteDump( + hProcess: HANDLE, + ProcessId: DWORD, + hFile: HANDLE, + DumpType: DWORD, + ExceptionParam: *const c_void, + UserStreamParam: *const c_void, + CallbackParam: *const c_void, + ) -> BOOL; +} + +fn until_nul(s: &[u16]) -> &[u16] { + &s[..bun_core::strings::index_of_any16(s, &[0]).unwrap_or(s.len())] +} + +/// Writes a full-memory dump of `process` as `..dmp`, if the +/// machine is set up to collect user-mode dumps (the `LocalDumps` key of +/// Windows Error Reporting exists): into its `DumpFolder`, by default +/// `%LOCALAPPDATA%\CrashDumps`, which is created when missing, as WER does. +/// Every failure is ignored; the caller ends the process either way. +fn write_minidump(process: HANDLE) { + let mut key: HKEY = ptr::null_mut(); + let subkey = bun_core::wstr!(r"SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps"); + // SAFETY: `subkey` is NUL-terminated; `key` is a valid out-pointer. + if unsafe { + RegOpenKeyExW( + HKEY_LOCAL_MACHINE, + subkey.as_ptr(), + 0, + KEY_QUERY_VALUE, + &mut key, + ) + } != 0 + { + return; + } + + let mut basename = [0u16; win32::MAX_PATH]; + // SAFETY: `basename` holds MAX_PATH units. + unsafe { + K32GetModuleBaseNameW( + process, + ptr::null_mut(), + basename.as_mut_ptr(), + win32::MAX_PATH as DWORD, + ) + }; + let pid = win32::GetProcessId(process); + + let mut folder_buf = [0u16; win32::MAX_PATH]; + let mut folder_bytes = size_of_val(&folder_buf) as DWORD; + let mut value_type: DWORD = 0; + let value_name = bun_core::wstr!("DumpFolder"); + // SAFETY: `folder_buf` holds `folder_bytes` bytes; the other pointers are + // valid for the call. + let status = unsafe { + RegGetValueW( + key, + ptr::null(), + value_name.as_ptr(), + RRF_RT_ANY, + &mut value_type, + folder_buf.as_mut_ptr().cast(), + &mut folder_bytes, + ) + }; + // SAFETY: `key` was opened above. + unsafe { RegCloseKey(key) }; + + let mut dump_name: Vec = Vec::with_capacity(win32::MAX_PATH); + if status == 0 { + dump_name.extend_from_slice(until_nul(&folder_buf)); + } else { + let mut local_app_data: *mut u16 = ptr::null_mut(); + // SAFETY: `local_app_data` is a valid out-pointer; the string it + // receives is NUL-terminated and freed below. + unsafe { + let found = SHGetKnownFolderPath( + &FOLDERID_LOCAL_APP_DATA, + 0, + ptr::null_mut(), + &mut local_app_data, + ) >= 0 + && !local_app_data.is_null(); + if found { + let mut len = 0usize; + while *local_app_data.add(len) != 0 { + len += 1; + } + dump_name.extend_from_slice(core::slice::from_raw_parts(local_app_data, len)); + } + CoTaskMemFree(local_app_data.cast()); + if !found { + return; + } + } + dump_name.extend(r"\CrashDumps".encode_utf16()); + } + + dump_name.push(0); + // SAFETY: `dump_name` is NUL-terminated. + unsafe { CreateDirectoryW(dump_name.as_ptr(), ptr::null_mut()) }; + dump_name.pop(); + + dump_name.push(b'\\' as u16); + dump_name.extend_from_slice(until_nul(&basename)); + dump_name.extend(format!(".{pid}.dmp").encode_utf16()); + dump_name.push(0); + if dump_name.len() > win32::MAX_PATH { + return; + } + + // SAFETY: `dump_name` is NUL-terminated. + let file = unsafe { + win32::CreateFileW( + dump_name.as_ptr(), + // DELETE is what lets the disposition below be set. + win32::GENERIC_WRITE | bun_windows_sys::externs::DELETE, + 0, + ptr::null_mut(), + CREATE_NEW, + FILE_ATTRIBUTE_NORMAL, + ptr::null_mut(), + ) + }; + if file == INVALID_HANDLE_VALUE { + return; + } + + // Delete-on-close until the dump is complete, so a failed one leaves no file. + let set_delete_on_close = |delete: bool| { + // FILE_DISPOSITION_INFO: one BOOLEAN. + let info = u8::from(delete); + // SAFETY: `info` is valid for the one byte passed. + unsafe { + SetFileInformationByHandle( + file, + FILE_DISPOSITION_INFO_CLASS, + ptr::from_ref(&info).cast(), + size_of::() as DWORD, + ) + }; + }; + set_delete_on_close(true); + + // SAFETY: `process` and `file` are valid handles; the optional parameters + // are null. + unsafe { + let sym_options = SymGetOptions(); + SymSetOptions(sym_options | SYMOPT_WINE_WITH_NATIVE_MODULES); + if MiniDumpWriteDump( + process, + pid, + file, + MINIDUMP_WITH_FULL_MEMORY + | MINIDUMP_IGNORE_INACCESSIBLE_MEMORY + | MINIDUMP_WITH_AVX_XSTATE_CONTEXT, + ptr::null(), + ptr::null(), + ptr::null(), + ) != 0 + { + set_delete_on_close(false); + } + SymSetOptions(sym_options); + win32::CloseHandle(file); + } +} diff --git a/src/spawn_sys/windows/mod.rs b/src/spawn_sys/windows/mod.rs new file mode 100644 index 000000000000..4314028ad703 --- /dev/null +++ b/src/spawn_sys/windows/mod.rs @@ -0,0 +1,669 @@ +//! Process creation on Windows: `CreateProcessW`. + +use core::ffi::c_void; +use core::ptr; +use core::sync::atomic::Ordering; + +use bun_core::DecodeWindows; +use bun_sys::{E, Fd, Tag}; + +use crate::spawn_process::{ExtraPipe, SpawnOptions, SpawnResult, Stdio}; +use crate::{Argv, Envp}; + +pub mod args; +pub mod env; +pub mod job; +pub mod kill; +pub mod search_path; +pub mod stdio; +pub mod win32; + +pub use kill::{kill, kill_pid}; + +use stdio::{ChildFd, ChildPipe}; +use win32::{DWORD, HANDLE, INVALID_HANDLE_VALUE}; + +/// Windows-only spawn options. +#[derive(Clone, Copy)] +pub struct WindowsOptions { + /// Join the arguments with spaces as they are instead of quoting them. + pub verbatim_arguments: bool, + /// No console window and a hidden main window for the child. + pub hide_window: bool, + /// Give the child an overlapped end of the pipe for stdin/stdout/stderr + /// (`Stdio::Buffer`). Only for a child known to do overlapped I/O on its + /// std handles. Pipes at fd 3 and above always have an overlapped child end. + pub overlapped_stdio: [bool; 3], +} + +impl Default for WindowsOptions { + fn default() -> Self { + Self { + verbatim_arguments: false, + hide_window: true, + overlapped_stdio: [false; 3], + } + } +} + +/// Held while this process has inheritable handles that are meant for one +/// child, or for none. +/// +/// A child is created with `bInheritHandles` and gets every handle of this +/// process that is inheritable at that moment, as with libuv and so Node: a +/// handle the program marked inheritable (an addon, `bun:ffi`, one this process +/// was itself started with) reaches the child by value. So a spawn makes its +/// child's handles inheritable only while it holds this, and a call that makes +/// inheritable handles of its own (`CreatePseudoConsole`) runs under it too. +/// Otherwise another thread's child would hold them as well, and a pipe does +/// not reach EOF until every holder of its other end is gone. +// TODO: name exactly the child's handles with +// PROC_THREAD_ATTRIBUTE_HANDLE_LIST once dropping the others is known not to +// break too much; this lock goes with it. +pub struct InheritableHandles { + _guard: bun_core::MutexGuard<'static, ()>, +} + +impl InheritableHandles { + pub fn lock() -> Self { + static LOCK: bun_core::Mutex<()> = bun_core::Mutex::new(()); + Self { + _guard: LOCK.lock(), + } + } +} + +/// The handles the child inherits, and the parent ends of the pipes made for it. +/// Every fd of the child has a handle of its own: its C runtime closes them one +/// by one, and a value in two slots would be closed twice. +struct ChildStdio { + fds: Vec, + /// Child-side handles this spawn made: every handle in `fds`. Inheritable + /// only inside [`ChildStdio::create_process`], and closed once the child + /// has its copies (or was not created); a pipe only reports EOF when no + /// writer is left. + to_close: Vec, + parent_ends: Vec, + keep_parent_ends: bool, +} + +impl Drop for ChildStdio { + fn drop(&mut self) { + self.close_child_handles(); + if !self.keep_parent_ends { + for &handle in &self.parent_ends { + // SAFETY: see above. + unsafe { win32::CloseHandle(handle) }; + } + } + } +} + +impl ChildStdio { + /// Runs `create` (`CreateProcessW`) with the child's handles inheritable + /// when it `inherits` handles. `Ok(false)`: `create` failed with a code + /// `may_retry` accepts, and the handles are as they were. They are closed + /// on every other way out. + fn create_process( + &mut self, + inherits: bool, + create: &mut dyn FnMut() -> Result<(), DWORD>, + may_retry: impl Fn(DWORD) -> bool, + ) -> Result { + let _inheritable = inherits.then(InheritableHandles::lock); + let set_all = |handles: &[HANDLE], inheritable: bool| { + handles + .iter() + .try_for_each(|&handle| stdio::set_inheritable(handle, inheritable)) + }; + let result = set_all(&self.to_close, true).and_then(|()| match create() { + Ok(()) => Ok(true), + Err(code) if may_retry(code) => set_all(&self.to_close, false).map(|()| false), + Err(code) => Err(code), + }); + if !matches!(result, Ok(false)) { + self.close_child_handles(); + } + result + } + + fn close_child_handles(&mut self) { + for handle in self.to_close.drain(..) { + // SAFETY: a handle this spawn created and still owns. + unsafe { win32::CloseHandle(handle) }; + } + } + + fn nul(&mut self, writable: bool) -> Result { + let handle = stdio::open_nul(writable)?; + self.to_close.push(handle); + Ok(ChildFd { + handle, + crt_flags: stdio::FOPEN | stdio::FDEV, + }) + } + + /// Another fd for what `fd` is open on (`2>&1`): a second handle to the + /// same file object, so the two share its position. + fn alias(&mut self, fd: ChildFd) -> Result { + if fd.handle == INVALID_HANDLE_VALUE { + return Ok(fd); + } + let handle = stdio::duplicate(fd.handle)?; + self.to_close.push(handle); + Ok(ChildFd { + handle, + crt_flags: fd.crt_flags, + }) + } + + fn inherit(&mut self, source: HANDLE) -> Result { + let handle = stdio::duplicate(source)?; + self.to_close.push(handle); + Ok(ChildFd { + handle, + crt_flags: stdio::crt_flags_for(handle)?, + }) + } + + fn pipe(&mut self, child: ChildPipe) -> Result<(ChildFd, Fd), DWORD> { + let pair = stdio::create_pipe_pair(child)?; + self.to_close.push(pair.child); + self.parent_ends.push(pair.parent); + Ok(( + ChildFd { + handle: pair.child, + crt_flags: stdio::FOPEN | stdio::FPIPE, + }, + Fd::from_system(pair.parent), + )) + } +} + +enum Slot { + Fd(ChildFd), + /// The parent end of a pipe made for this slot. + Piped(ChildFd, Fd), + /// A handle the caller supplied. + Supplied(ChildFd, Fd), +} + +fn make_slot( + stdio: &mut ChildStdio, + option: &Stdio, + index: usize, + options: &SpawnOptions, +) -> Result { + // `uv_spawn` is the `syscall` name a `Bun.spawn` error carries on Windows. + let spawn_error = |code: DWORD| win32::sys_error(code, Tag::uv_spawn); + let duplex = |overlapped: bool| ChildPipe { + readable: true, + writable: true, + overlapped, + }; + match option { + Stdio::Inherit => { + let source = Fd::from_crt(index as i32).native(); + match stdio.inherit(source) { + Ok(fd) => Ok(Slot::Fd(fd)), + // A process without a console has no std handles to pass on. + Err(win32::ERROR_INVALID_HANDLE) if index <= 2 => Ok(Slot::Fd(ChildFd::CLOSED)), + Err(code) => Err(spawn_error(code)), + } + } + Stdio::Ignore => { + if index <= 2 { + stdio.nul(index != 0).map(Slot::Fd).map_err(spawn_error) + } else { + Ok(Slot::Fd(ChildFd::CLOSED)) + } + } + Stdio::Ipc if index <= 2 => stdio.nul(index != 0).map(Slot::Fd).map_err(spawn_error), + Stdio::Ipc => stdio + .pipe(duplex(true)) + .map(|(fd, parent)| Slot::Piped(fd, parent)) + .map_err(spawn_error), + Stdio::Buffer | Stdio::SocketFd => { + let overlapped = index > 2 || options.windows.overlapped_stdio[index]; + stdio + .pipe(duplex(overlapped)) + .map(|(fd, parent)| Slot::Piped(fd, parent)) + .map_err(spawn_error) + } + Stdio::Path(path) => { + let access = match index { + 0 => bun_sys::O::RDONLY, + 1 | 2 => bun_sys::O::WRONLY, + _ => bun_sys::O::RDWR, + }; + // `Bun.file(path)` names a file by Win32's rules wherever it is + // opened (`NUL` is the device, not a file of that name), so here too. + let mut name = Vec::with_capacity(path.len() + 1); + name.extend_from_slice(path); + name.push(0); + let name = bun_core::ZStr::from_buf(&name, path.len()); + let handle = bun_sys::open(name, access | bun_sys::O::CREAT, 0o664)?.native(); + stdio.to_close.push(handle); + Ok(Slot::Fd(ChildFd { + handle, + crt_flags: stdio::crt_flags_for(handle).map_err(spawn_error)?, + })) + } + Stdio::Pipe(fd) => match stdio.inherit(fd.native()) { + Ok(child) => Ok(Slot::Supplied(child, *fd)), + Err(win32::ERROR_INVALID_HANDLE) + if matches!(fd.decode_windows(), DecodeWindows::Crt(0..=2)) => + { + Ok(Slot::Fd(ChildFd::CLOSED)) + } + Err(code) => Err(spawn_error(code)), + }, + Stdio::Dup2(_) => unreachable!("resolved by the caller"), + } +} + +/// Owns a `PROC_THREAD_ATTRIBUTE_LIST`. The values it points at are borrowed +/// until `CreateProcessW` returns. +struct AttributeList { + // Pointer-aligned backing storage. + buf: Vec, +} + +impl AttributeList { + fn new(count: DWORD) -> Result { + let mut size: usize = 0; + // SAFETY: the sizing call; it fails with ERROR_INSUFFICIENT_BUFFER by design. + unsafe { win32::InitializeProcThreadAttributeList(ptr::null_mut(), count, 0, &mut size) }; + let mut buf = vec![0usize; size.div_ceil(size_of::())]; + // SAFETY: `buf` holds at least `size` bytes. + if unsafe { + win32::InitializeProcThreadAttributeList(buf.as_mut_ptr().cast(), count, 0, &mut size) + } == 0 + { + return Err(win32::GetLastError()); + } + Ok(Self { buf }) + } + + /// # Safety + /// `value` must stay valid, unmoved, until this list is dropped. + unsafe fn set( + &mut self, + attribute: usize, + value: *const c_void, + size: usize, + ) -> Result<(), DWORD> { + // SAFETY: the list was initialized in `new`; `value` per the caller. + if unsafe { + win32::UpdateProcThreadAttribute( + self.buf.as_mut_ptr().cast(), + 0, + attribute, + value, + size, + ptr::null_mut(), + ptr::null_mut(), + ) + } == 0 + { + return Err(win32::GetLastError()); + } + Ok(()) + } + + fn as_mut_ptr(&mut self) -> *mut u8 { + self.buf.as_mut_ptr().cast() + } +} + +impl Drop for AttributeList { + fn drop(&mut self) { + // SAFETY: initialized in `new`. + unsafe { win32::DeleteProcThreadAttributeList(self.buf.as_mut_ptr().cast()) }; + } +} + +fn c_str_bytes<'a>(s: *const core::ffi::c_char) -> &'a [u8] { + // SAFETY: callers pass NUL-terminated strings (`spawn_process_windows` contract). + unsafe { core::ffi::CStr::from_ptr(s) }.to_bytes() +} + +/// # Safety +/// `list` is null or points to a null-terminated array of NUL-terminated strings. +unsafe fn c_str_array<'a>(list: *const *const core::ffi::c_char) -> impl Iterator { + let mut i = 0usize; + core::iter::from_fn(move || { + if list.is_null() { + return None; + } + // SAFETY: the array is null-terminated and `i` has not passed the terminator. + let s = unsafe { *list.add(i) }; + if s.is_null() { + return None; + } + i += 1; + Some(c_str_bytes(s)) + }) +} + +fn current_directory() -> Result, DWORD> { + let mut buf: Vec = vec![0; win32::MAX_PATH]; + loop { + // SAFETY: `buf` holds `buf.len()` units. + let n = + unsafe { win32::GetCurrentDirectoryW(buf.len() as DWORD, buf.as_mut_ptr()) } as usize; + if n == 0 { + return Err(win32::GetLastError()); + } + if n < buf.len() { + buf.truncate(n); + return Ok(buf); + } + buf.resize(n, 0); + } +} + +/// Starts the program `argv` names. +/// +/// `options.argv0`, else `argv[0]`, names the image; it is looked up with the +/// rules in [`search_path`] against the child's directory and `PATH`. A null +/// `envp` gives the child this process's environment. Pipes are created here; +/// their parent ends come back in the result as overlapped, non-inheritable +/// handles that are not associated with any completion port. +/// +/// # Safety +/// `argv` must point to a null-terminated array of NUL-terminated C strings +/// with at least one element; `envp` must be null or point to such an array. +/// Both must remain valid for the duration of the call. +pub unsafe fn spawn_process_windows( + options: &SpawnOptions, + argv: Argv, + envp: Envp, +) -> crate::Result> { + bun_analytics::features::spawn.fetch_add(1, Ordering::Relaxed); + // SAFETY: forwarded from this function's contract. + Ok(unsafe { spawn(options, argv, envp) }) +} + +unsafe fn spawn(options: &SpawnOptions, argv: Argv, envp: Envp) -> bun_sys::Result { + let spawn_error = |code: DWORD| win32::sys_error(code, Tag::uv_spawn); + + if options.uid.is_some() || options.gid.is_some() { + return Err(bun_sys::Error::from_code(E::ENOTSUP, Tag::uv_spawn)); + } + + // SAFETY: `argv` has at least one element (caller contract). + let file = c_str_bytes(options.argv0.unwrap_or_else(|| unsafe { *argv })); + let mut application: Vec = Vec::new(); + args::push_wtf8(&mut application, file); + + // SAFETY: caller contract. + let mut command_line = args::make_command_line( + unsafe { c_str_array(argv) }, + options.windows.verbatim_arguments, + ); + + let mut env_block: Option> = if envp.is_null() { + None + } else { + // SAFETY: caller contract. + Some(env::make_env_block(unsafe { c_str_array(envp) })) + }; + + // NUL-terminated when present. + let mut cwd: Option> = None; + if !options.cwd.is_empty() { + let mut wide: Vec = Vec::new(); + args::push_wtf8(&mut wide, &options.cwd); + wide.push(0); + // lpCurrentDirectory is limited to MAX_PATH; the short form may fit. + if wide.len() > win32::MAX_PATH { + let capacity = wide.len(); + // SAFETY: in-place conversion; `wide` holds `capacity` units and is NUL-terminated. + let n = unsafe { + win32::GetShortPathNameW(wide.as_ptr(), wide.as_mut_ptr(), capacity as DWORD) + } as usize; + if n == 0 { + return Err(win32::last_error(Tag::uv_spawn)); + } + if n < capacity { + wide.truncate(n + 1); + } + } + cwd = Some(wide); + } + + let application_path = { + let inherited_cwd; + let search_cwd: &[u16] = match &cwd { + Some(cwd) => &cwd[..cwd.len() - 1], + None if search_path::needs_cwd(&application) => { + inherited_cwd = current_directory().map_err(spawn_error)?; + &inherited_cwd + } + None => &[], + }; + let mut parent_path: Vec = Vec::new(); + let path: &[u16] = if search_path::needs_path(&application) { + match env_block.as_deref().and_then(env::find_path) { + Some(path) => path, + None => { + env::parent_value(env::PATH, &mut parent_path); + &parent_path + } + } + } else { + &[] + }; + search_path::search_path(&application, search_cwd, path) + }; + let Some(application_path) = application_path else { + return Err(spawn_error(win32::ERROR_FILE_NOT_FOUND)); + }; + + let stdio_count = 3 + options.extra_fds.len(); + if stdio_count > stdio::MAX_STDIO { + return Err(spawn_error(win32::ERROR_NOT_SUPPORTED)); + } + + let mut result = SpawnResult::default(); + let mut child_stdio = ChildStdio { + fds: Vec::new(), + to_close: Vec::new(), + parent_ends: Vec::new(), + keep_parent_ends: false, + }; + // A child on a pseudoconsole gets its std handles from it and nothing else. + let use_stdio = options.pseudoconsole.is_none(); + let mut any_inherited = false; + if use_stdio { + child_stdio.fds = vec![ChildFd::CLOSED; stdio_count]; + let std_options: [&Stdio; 3] = [&options.stdin, &options.stdout, &options.stderr]; + let mut parent_fds: [Option; 3] = [None; 3]; + let mut inherited = [false; 3]; + for (i, &option) in std_options.iter().enumerate() { + if matches!(option, Stdio::Dup2(_)) { + continue; + } + inherited[i] = matches!(option, Stdio::Inherit | Stdio::Path(_) | Stdio::Pipe(_)); + match make_slot(&mut child_stdio, option, i, options)? { + Slot::Fd(fd) => child_stdio.fds[i] = fd, + Slot::Piped(fd, parent) | Slot::Supplied(fd, parent) => { + child_stdio.fds[i] = fd; + parent_fds[i] = Some(parent); + } + } + } + for (i, &option) in std_options.iter().enumerate() { + if let Stdio::Dup2(dup2) = option { + let to = dup2.to as usize; + let target = child_stdio.fds[to]; + child_stdio.fds[i] = child_stdio.alias(target).map_err(spawn_error)?; + inherited[i] = inherited[to]; + } + } + any_inherited = inherited.contains(&true); + [result.stdin, result.stdout, result.stderr] = parent_fds; + + result.extra_pipes.reserve_exact(options.extra_fds.len()); + for (i, option) in options.extra_fds.iter().enumerate() { + if matches!(option, Stdio::Dup2(_)) { + panic!("TODO dup2 extra fd"); + } + any_inherited |= matches!(option, Stdio::Inherit | Stdio::Path(_) | Stdio::Pipe(_)); + result + .extra_pipes + .push(match make_slot(&mut child_stdio, option, 3 + i, options)? { + Slot::Fd(fd) => { + child_stdio.fds[3 + i] = fd; + ExtraPipe::Unavailable + } + Slot::Piped(fd, parent) => { + child_stdio.fds[3 + i] = fd; + ExtraPipe::OwnedFd(parent) + } + // The caller has its own number for a handle it supplied. + Slot::Supplied(fd, _) => { + child_stdio.fds[3 + i] = fd; + ExtraPipe::Unavailable + } + }); + } + } else { + result + .extra_pipes + .resize_with(options.extra_fds.len(), || ExtraPipe::Unavailable); + } + + let mut crt_block = stdio::make_crt_block(&child_stdio.fds); + + // SAFETY: all-zero is a valid STARTUPINFOEXW. + let mut startup: win32::STARTUPINFOEXW = unsafe { core::mem::zeroed() }; + // Always STARTF_USESTDHANDLES: without it the system duplicates this + // process's own std handles into the child, which would also override a + // pseudoconsole. + startup.StartupInfo.dwFlags = win32::STARTF_USESTDHANDLES | win32::STARTF_USESHOWWINDOW; + if use_stdio { + startup.StartupInfo.cbReserved2 = crt_block.len() as u16; + startup.StartupInfo.lpReserved2 = crt_block.as_mut_ptr(); + startup.StartupInfo.hStdInput = child_stdio.fds[0].handle; + startup.StartupInfo.hStdOutput = child_stdio.fds[1].handle; + startup.StartupInfo.hStdError = child_stdio.fds[2].handle; + } + startup.StartupInfo.wShowWindow = if options.windows.hide_window { + win32::SW_HIDE + } else { + win32::SW_SHOWDEFAULT + }; + + let mut process_flags = win32::CREATE_UNICODE_ENVIRONMENT; + // CREATE_NO_WINDOW puts the child on a console of its own, where console + // handles inherited from this process do not work. + if use_stdio && options.windows.hide_window && !any_inherited { + process_flags |= win32::CREATE_NO_WINDOW; + } + if options.detached { + // Not CREATE_BREAKAWAY_FROM_JOB: it fails the call under a job that + // does not allow breakaway, and the job children are put in lets + // their own children break away silently anyway. + process_flags |= win32::DETACHED_PROCESS | win32::CREATE_NEW_PROCESS_GROUP; + } + + let mut job: HANDLE = if options.detached { + ptr::null_mut() + } else { + job::global_job().map_err(spawn_error)? + }; + + let mut info: win32::PROCESS_INFORMATION = bun_core::ffi::zeroed(); + loop { + let attribute_count = + DWORD::from(!job.is_null()) + DWORD::from(options.pseudoconsole.is_some()); + let mut attributes = if attribute_count == 0 { + None + } else { + Some(AttributeList::new(attribute_count).map_err(spawn_error)?) + }; + if let Some(attributes) = &mut attributes { + // SAFETY: `job` and the pseudoconsole outlive `attributes`. + unsafe { + if !job.is_null() { + attributes + .set( + win32::PROC_THREAD_ATTRIBUTE_JOB_LIST, + ptr::from_ref(&job).cast(), + size_of::(), + ) + .map_err(spawn_error)?; + } + if let Some(pseudoconsole) = options.pseudoconsole { + attributes + .set( + win32::PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, + pseudoconsole.cast_const(), + size_of::(), + ) + .map_err(spawn_error)?; + } + } + } + let extended = match &mut attributes { + Some(attributes) => { + startup.StartupInfo.cb = size_of::() as DWORD; + startup.lpAttributeList = attributes.as_mut_ptr(); + win32::EXTENDED_STARTUPINFO_PRESENT + } + None => { + startup.StartupInfo.cb = size_of::() as DWORD; + startup.lpAttributeList = ptr::null_mut(); + 0 + } + }; + + let mut create = || { + // SAFETY: every pointer is valid for the call; the strings are NUL-terminated. + let created = unsafe { + win32::CreateProcessW( + application_path.as_ptr(), + command_line.as_mut_ptr(), + ptr::null_mut(), + ptr::null_mut(), + // A pseudoconsole's child takes its std handles from it. + i32::from(use_stdio), + process_flags | extended, + env_block + .as_mut() + .map_or(ptr::null_mut(), |block| block.as_mut_ptr().cast()), + cwd.as_ref().map_or(ptr::null(), |cwd| cwd.as_ptr()), + ptr::from_mut(&mut startup).cast(), + &mut info, + ) + }; + if created != 0 { + Ok(()) + } else { + Err(win32::GetLastError()) + } + }; + // A child born in a job that the job cannot nest below is refused + // with ERROR_ACCESS_DENIED. Such a parent must still be able to + // spawn: go without ending the child with us. + let has_job = !job.is_null(); + let without_job = move |code: DWORD| has_job && code == win32::ERROR_ACCESS_DENIED; + if child_stdio + .create_process(use_stdio, &mut create, without_job) + .map_err(spawn_error)? + { + break; + } + job = ptr::null_mut(); + } + + // SAFETY: the thread handle is ours and unused. + unsafe { win32::CloseHandle(info.hThread) }; + + child_stdio.keep_parent_ends = true; + result.pid = info.dwProcessId as crate::PidT; + result.process_handle = crate::spawn_process::OwnedProcessHandle::new(info.hProcess); + Ok(result) +} diff --git a/src/spawn_sys/windows/search_path.rs b/src/spawn_sys/windows/search_path.rs new file mode 100644 index 000000000000..e513b2367f1d --- /dev/null +++ b/src/spawn_sys/windows/search_path.rs @@ -0,0 +1,224 @@ +//! Finding the image to run, with cmd.exe's rules minus `PATHEXT`: +//! `CreateProcessW` can only start `.com` and `.exe` images, so only those are +//! tried. `CreateProcessW`'s own search is unusable: it resolves against this +//! process's directory and `PATH` rather than the child's. +//! +//! - A name with a directory part (relative or absolute) is not looked up in +//! `PATH`. +//! - A bare name is looked up in the child's directory first (unless +//! `NoDefaultCurrentDirectoryInExePath` is in effect), then in every `PATH` +//! entry. Entries may be relative (to the child's directory), may or may not +//! end in a separator, may be quoted with `"` or `'`, and are not trimmed. +//! - In each directory the exact name is tried first if it has an extension, +//! then the name with `.com` appended, then with `.exe` appended. +//! - A directory never matches; an unreadable file does. +//! - UNC paths work in both the name and `PATH`. + +use super::win32; + +const BACKSLASH: u16 = b'\\' as u16; +const SLASH: u16 = b'/' as u16; +const COLON: u16 = b':' as u16; +const DOT: u16 = b'.' as u16; +const SEMICOLON: u16 = b';' as u16; + +#[inline] +fn is_slash(c: u16) -> bool { + c == BACKSLASH || c == SLASH +} + +#[inline] +fn is_quote(c: u16) -> bool { + c == b'"' as u16 || c == b'\'' as u16 +} + +/// How much of the child's directory `dir` is resolved against. +enum CwdUse { + None, + DriveOnly, + Full, + /// `D:rel`: the whole directory when it is on drive `D:`, else nothing; + /// the `D:` prefix of `dir` is dropped in the first case. + IfSameDrive, +} + +fn cwd_use(dir: &[u16]) -> CwdUse { + if dir.len() > 2 && is_slash(dir[0]) && is_slash(dir[1]) { + CwdUse::None + } else if !dir.is_empty() && is_slash(dir[0]) { + CwdUse::DriveOnly + } else if dir.len() >= 2 && dir[1] == COLON && (dir.len() < 3 || !is_slash(dir[2])) { + CwdUse::IfSameDrive + } else if dir.len() > 2 && dir[1] == COLON { + CwdUse::None + } else { + CwdUse::Full + } +} + +/// Where the file name starts in `file`: after the last `\`, `/` or `:`. +fn file_name_start(file: &[u16]) -> usize { + file.iter() + .rposition(|&c| is_slash(c) || c == COLON) + .map_or(0, |i| i + 1) +} + +/// Whether [`search_path`] reads the child's directory for `file`. False only +/// for a name whose directory part is absolute with a drive, or UNC. +pub fn needs_cwd(file: &[u16]) -> bool { + let name_start = file_name_start(file); + name_start == 0 || !matches!(cwd_use(&file[..name_start]), CwdUse::None) +} + +/// Whether [`search_path`] reads `PATH` for `file`. +pub fn needs_path(file: &[u16]) -> bool { + file_name_start(file) == 0 +} + +fn ascii_eq_ignore_case(a: u16, b: u16) -> bool { + let lower = |c: u16| { + if (b'A' as u16..=b'Z' as u16).contains(&c) { + c + 32 + } else { + c + } + }; + lower(a) == lower(b) +} + +/// Whether `path` (NUL-terminated) names something that is not a directory. +fn exists(path: &[u16]) -> bool { + // SAFETY: `path` is NUL-terminated. + let attributes = unsafe { win32::GetFileAttributesW(path.as_ptr()) }; + attributes != win32::INVALID_FILE_ATTRIBUTES + && attributes & win32::FILE_ATTRIBUTE_DIRECTORY == 0 +} + +fn join_test(dir: &[u16], name: &[u16], ext: &[u16], cwd: &[u16]) -> Option> { + let (cwd, dir) = match cwd_use(dir) { + CwdUse::None => (&cwd[..0], dir), + CwdUse::DriveOnly => (&cwd[..cwd.len().min(2)], dir), + CwdUse::Full => (cwd, dir), + CwdUse::IfSameDrive => { + if cwd.len() < 2 + || !ascii_eq_ignore_case(cwd[0], dir[0]) + || !ascii_eq_ignore_case(cwd[1], dir[1]) + { + (&cwd[..0], dir) + } else { + (cwd, &dir[2..]) + } + } + }; + + let ends_in_separator = |s: &[u16]| s.last().is_some_and(|&c| is_slash(c) || c == COLON); + + let mut result: Vec = + Vec::with_capacity(cwd.len() + 1 + dir.len() + 1 + name.len() + 1 + ext.len() + 1); + result.extend_from_slice(cwd); + if !cwd.is_empty() && !ends_in_separator(cwd) { + result.push(BACKSLASH); + } + result.extend_from_slice(dir); + if !dir.is_empty() && !ends_in_separator(dir) { + result.push(BACKSLASH); + } + result.extend_from_slice(name); + if !ext.is_empty() { + if name.last().is_some_and(|&c| c != DOT) { + result.push(DOT); + } + result.extend_from_slice(ext); + } + result.push(0); + + if exists(&result) { Some(result) } else { None } +} + +fn walk_ext(dir: &[u16], name: &[u16], cwd: &[u16], name_has_ext: bool) -> Option> { + const COM: [u16; 3] = [b'c' as u16, b'o' as u16, b'm' as u16]; + const EXE: [u16; 3] = [b'e' as u16, b'x' as u16, b'e' as u16]; + if name_has_ext { + if let Some(found) = join_test(dir, name, &[], cwd) { + return Some(found); + } + } + if let Some(found) = join_test(dir, name, &COM, cwd) { + return Some(found); + } + join_test(dir, name, &EXE, cwd) +} + +/// The NUL-terminated path of the image `file` names, or `None`. +/// +/// `cwd` is the child's directory and `path` its `PATH` (neither +/// NUL-terminated); see [`needs_cwd`] / [`needs_path`] for when they are read. +pub fn search_path(file: &[u16], cwd: &[u16], path: &[u16]) -> Option> { + if file.is_empty() || file == [DOT] { + return None; + } + + let name_start = file_name_start(file); + let name = &file[name_start..]; + // An extension is a `.` in the file name that is not its last character. + let name_has_ext = name + .iter() + .position(|&c| c == DOT) + .is_some_and(|dot| dot + 1 < name.len()); + + if name_start != 0 { + return walk_ext(&file[..name_start], name, cwd, name_has_ext); + } + + // SAFETY: an empty NUL-terminated string. + let cwd_first = unsafe { win32::NeedCurrentDirectoryForExePathW([0u16].as_ptr()) } != 0; + if cwd_first { + if let Some(found) = walk_ext(&[], file, cwd, name_has_ext) { + return Some(found); + } + } + + let mut dir_end = 0usize; + loop { + if dir_end >= path.len() { + return None; + } + // Step over the separator the previous entry ended at. + if dir_end != 0 || path[0] == SEMICOLON { + dir_end += 1; + } + let dir_start = dir_end; + + // A `;` inside quotes does not end the entry. + if dir_start < path.len() && is_quote(path[dir_start]) { + let quote = path[dir_start]; + dir_end = path[dir_start + 1..] + .iter() + .position(|&c| c == quote) + .map_or(path.len(), |i| dir_start + 1 + i); + } + dir_end = path[dir_end.min(path.len())..] + .iter() + .position(|&c| c == SEMICOLON) + .map_or(path.len(), |i| dir_end + i); + + if dir_end == dir_start { + continue; + } + + let mut dir = &path[dir_start..dir_end]; + if is_quote(dir[0]) { + dir = &dir[1..]; + if dir.is_empty() { + continue; + } + } + if is_quote(dir[dir.len() - 1]) { + dir = &dir[..dir.len() - 1]; + } + + if let Some(found) = walk_ext(dir, file, cwd, name_has_ext) { + return Some(found); + } + } +} diff --git a/src/spawn_sys/windows/stdio.rs b/src/spawn_sys/windows/stdio.rs new file mode 100644 index 000000000000..05c4840ecfa5 --- /dev/null +++ b/src/spawn_sys/windows/stdio.rs @@ -0,0 +1,260 @@ +//! The child's stdio: the handles it inherits, the pipes to it, and the C +//! runtime's description of them. + +use core::ptr; + +use super::win32::{self, DWORD, HANDLE, INVALID_HANDLE_VALUE}; + +/// Flags of the C runtime's fd table (`lowio`), as far as a parent sets them. +pub const FOPEN: u8 = 0x01; +pub const FPIPE: u8 = 0x08; +pub const FDEV: u8 = 0x40; + +/// The CRT's limit is 8192 and `cbReserved2` would allow 7281; this is libuv's +/// cap, kept. +pub const MAX_STDIO: usize = 255; + +/// One fd of the child: the handle it inherits (`INVALID_HANDLE_VALUE` for a +/// closed fd) and its CRT flags (0 for a closed fd). +#[derive(Clone, Copy)] +pub struct ChildFd { + pub handle: HANDLE, + pub crt_flags: u8, +} + +impl ChildFd { + pub const CLOSED: ChildFd = ChildFd { + handle: INVALID_HANDLE_VALUE, + crt_flags: 0, + }; +} + +/// `STARTUPINFOW.lpReserved2`: how the MSVCRT/UCRT startup code learns the +/// child's fds, the only way fds above 2 reach it. Packed, so the handle array +/// is unaligned: +/// +/// ```text +/// i32 count +/// u8 crt_flags[count] +/// HANDLE os_handle[count] +/// ``` +/// +/// The CRT trusts `count` without checking it against `cbReserved2`, so the +/// block must be exactly this long. +pub fn make_crt_block(fds: &[ChildFd]) -> Vec { + debug_assert!(fds.len() <= MAX_STDIO); + let mut block = Vec::with_capacity(4 + fds.len() * (1 + size_of::())); + block.extend_from_slice(&(fds.len() as i32).to_ne_bytes()); + block.extend(fds.iter().map(|fd| fd.crt_flags)); + for fd in fds { + block.extend_from_slice(&(fd.handle as usize).to_ne_bytes()); + } + block +} + +/// CRT flags for a handle of unknown kind. `FDEV` makes the child's `isatty()` +/// true, so it is only for character devices. +pub fn crt_flags_for(handle: HANDLE) -> Result { + match win32::GetFileType(handle) { + win32::FILE_TYPE_DISK => Ok(FOPEN), + win32::FILE_TYPE_PIPE => Ok(FOPEN | FPIPE), + win32::FILE_TYPE_CHAR | win32::FILE_TYPE_REMOTE => Ok(FOPEN | FDEV), + _ => match win32::GetLastError() { + 0 => Ok(FOPEN | FDEV), + err => Err(err), + }, + } +} + +/// A duplicate of `handle`, not inheritable. +pub fn duplicate(handle: HANDLE) -> Result { + // `_get_osfhandle` answers -2 for a stdio fd with no stream attached, and + // `DuplicateHandle` would happily duplicate that pseudo handle. + if handle == INVALID_HANDLE_VALUE || handle.is_null() || handle as isize == -2 { + return Err(win32::ERROR_INVALID_HANDLE); + } + let mut dup: HANDLE = INVALID_HANDLE_VALUE; + let process = win32::GetCurrentProcess(); + // SAFETY: `dup` is a valid out-pointer. + if unsafe { + win32::DuplicateHandle( + process, + handle, + process, + &mut dup, + 0, + 0, + win32::DUPLICATE_SAME_ACCESS, + ) + } == 0 + { + return Err(win32::GetLastError()); + } + Ok(dup) +} + +/// Whether a child created with `bInheritHandles` gets `handle`. Only a spawn +/// holding [`InheritableHandles`](super::InheritableHandles) may set it. +pub fn set_inheritable(handle: HANDLE, inheritable: bool) -> Result<(), DWORD> { + let flags = if inheritable { + win32::HANDLE_FLAG_INHERIT + } else { + 0 + }; + if win32::SetHandleInformation(handle, win32::HANDLE_FLAG_INHERIT, flags) == 0 { + return Err(win32::GetLastError()); + } + Ok(()) +} + +/// A handle to `NUL`. A child started without one of fds 0-2 can misbehave, so +/// an ignored one gets this instead of nothing. +pub fn open_nul(writable: bool) -> Result { + const NUL: [u16; 4] = [b'N' as u16, b'U' as u16, b'L' as u16, 0]; + let access = if writable { + win32::FILE_GENERIC_WRITE | win32::FILE_READ_ATTRIBUTES + } else { + win32::FILE_GENERIC_READ + }; + // SAFETY: `NUL` is NUL-terminated. + let handle = unsafe { + win32::CreateFileW( + NUL.as_ptr(), + access, + win32::FILE_SHARE_READ | win32::FILE_SHARE_WRITE, + ptr::null_mut(), + win32::OPEN_EXISTING, + 0, + ptr::null_mut(), + ) + }; + if handle == INVALID_HANDLE_VALUE { + return Err(win32::GetLastError()); + } + Ok(handle) +} + +/// How the child uses its end of a pipe. +#[derive(Clone, Copy)] +pub struct ChildPipe { + pub readable: bool, + pub writable: bool, + /// The child's end is synchronous unless this is set: programs doing plain + /// `ReadFile`/`_read` on an overlapped handle lose data or crash as soon as + /// two of their threads use it at once. + pub overlapped: bool, +} + +/// Neither end is inheritable: a child inherits every inheritable handle of +/// this process, so an end is made inheritable only by the spawn it is for, +/// while that holds [`InheritableHandles`](super::InheritableHandles). +pub struct PipePair { + /// Overlapped, not associated with a completion port. + pub parent: HANDLE, + pub child: HANDLE, +} + +/// A connected named-pipe pair for one stdio slot. +pub fn create_pipe_pair(child: ChildPipe) -> Result { + use std::io::Write as _; + + // The parent end always gets read access: it carries the + // FILE_READ_ATTRIBUTES needed to query the pipe's state when writing. + let mut server_access = win32::PIPE_ACCESS_INBOUND + | win32::FILE_FLAG_OVERLAPPED + | win32::FILE_FLAG_FIRST_PIPE_INSTANCE + | win32::WRITE_DAC; + if child.readable { + server_access |= win32::PIPE_ACCESS_OUTBOUND; + } + let client_access = win32::WRITE_DAC + | if child.readable { + win32::GENERIC_READ + } else { + win32::FILE_READ_ATTRIBUTES + } + | if child.writable { + win32::GENERIC_WRITE + } else { + win32::FILE_WRITE_ATTRIBUTES + }; + + let pid = win32::GetCurrentProcessId(); + // An AppContainer may only create pipes under `\pipe\LOCAL\`. + let local = if bun_sys::windows::is_app_container() { + "LOCAL\\" + } else { + "" + }; + + let mut name_w = [0u16; 64]; + let server = loop { + let serial = bun_sys::windows::fs::next_pipe_serial(); + let mut name = [0u8; 63]; + let len = { + let mut cursor = &mut name[..]; + let _ = write!(cursor, "\\\\?\\pipe\\{local}bun\\{pid}-{serial}"); + 63 - cursor.len() + }; + for (w, &b) in name_w.iter_mut().zip(&name[..len]) { + *w = u16::from(b); + } + name_w[len] = 0; + + // SAFETY: `name_w` is NUL-terminated. + let server = unsafe { + win32::CreateNamedPipeW( + name_w.as_ptr(), + server_access, + win32::PIPE_TYPE_BYTE + | win32::PIPE_READMODE_BYTE + | win32::PIPE_WAIT + | win32::PIPE_REJECT_REMOTE_CLIENTS, + 1, + 65536, + 65536, + 0, + ptr::null_mut(), + ) + }; + if server != INVALID_HANDLE_VALUE { + break server; + } + match win32::GetLastError() { + // The name is taken: FILE_FLAG_FIRST_PIPE_INSTANCE refuses to join + // someone else's pipe. Try the next one. + win32::ERROR_PIPE_BUSY | win32::ERROR_ACCESS_DENIED => {} + err => return Err(err), + } + }; + + // SAFETY: `name_w` is NUL-terminated. + let client = unsafe { + win32::CreateFileW( + name_w.as_ptr(), + client_access, + 0, + ptr::null_mut(), + win32::OPEN_EXISTING, + win32::SECURITY_SQOS_PRESENT + | win32::SECURITY_ANONYMOUS + | if child.overlapped { + win32::FILE_FLAG_OVERLAPPED + } else { + 0 + }, + ptr::null_mut(), + ) + }; + if client == INVALID_HANDLE_VALUE { + let err = win32::GetLastError(); + // SAFETY: `server` is a handle this function owns. + unsafe { win32::CloseHandle(server) }; + return Err(err); + } + + Ok(PipePair { + parent: server, + child: client, + }) +} diff --git a/src/spawn_sys/windows/win32.rs b/src/spawn_sys/windows/win32.rs new file mode 100644 index 000000000000..15e99d9a9cfc --- /dev/null +++ b/src/spawn_sys/windows/win32.rs @@ -0,0 +1,98 @@ +//! Win32 declarations process creation needs. + +#![allow( + non_snake_case, + non_camel_case_types, + non_upper_case_globals, + clippy::upper_case_acronyms +)] + +pub use bun_windows_sys::externs::kernel32::{ + CreateNamedPipeW, CreateProcessW, DuplicateHandle, GetCurrentProcess, GetExitCodeProcess, + GetLastError, ReadFile, SetLastError, +}; +pub use bun_windows_sys::externs::{ + AssignProcessToJobObject, BOOL, CREATE_UNICODE_ENVIRONMENT, CSTR_EQUAL, CancelIoEx, + CloseHandle, CompareStringOrdinal, CreateEventW, CreateFileW, CreateJobObjectW, + DUPLICATE_SAME_ACCESS, DWORD, EXTENDED_STARTUPINFO_PRESENT, FILE_ATTRIBUTE_DIRECTORY, + FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, FILE_GENERIC_READ, FILE_GENERIC_WRITE, + FILE_READ_ATTRIBUTES, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_TYPE_CHAR, FILE_TYPE_DISK, + FILE_TYPE_PIPE, FILE_TYPE_REMOTE, FILE_WRITE_ATTRIBUTES, FILETIME, GENERIC_READ, GENERIC_WRITE, + GetCurrentDirectoryW, GetCurrentProcessId, GetEnvironmentVariableW, GetFileAttributesW, + GetFileType, GetShortPathNameW, HANDLE, HANDLE_FLAG_INHERIT, HPCON, INFINITE, + INVALID_FILE_ATTRIBUTES, INVALID_HANDLE_VALUE, InitializeProcThreadAttributeList, + JOB_OBJECT_LIMIT_BREAKAWAY_OK, JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION, + JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK, + JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation, MAX_PATH, + OPEN_EXISTING, OVERLAPPED, OpenProcess, PIPE_ACCESS_INBOUND, PIPE_ACCESS_OUTBOUND, + PIPE_READMODE_BYTE, PIPE_REJECT_REMOTE_CLIENTS, PIPE_TYPE_BYTE, PIPE_WAIT, + PROC_THREAD_ATTRIBUTE_JOB_LIST, PROCESS_INFORMATION, STARTF_USESTDHANDLES, STARTUPINFOEXW, + STARTUPINFOW, SYNCHRONIZE, SetHandleInformation, SetInformationJobObject, TerminateProcess, + UpdateProcThreadAttribute, WAIT_FAILED, WAIT_OBJECT_0, WRITE_DAC, WaitForMultipleObjects, + WaitForSingleObject, Win32Error, +}; + +pub const SECURITY_SQOS_PRESENT: DWORD = 0x0010_0000; +pub const SECURITY_ANONYMOUS: DWORD = 0; + +pub const STARTF_USESHOWWINDOW: DWORD = 0x0000_0001; +pub const SW_HIDE: u16 = 0; +pub const SW_SHOWDEFAULT: u16 = 10; + +pub const CREATE_NEW_PROCESS_GROUP: DWORD = 0x0000_0200; +pub const DETACHED_PROCESS: DWORD = 0x0000_0008; +pub const CREATE_NO_WINDOW: DWORD = 0x0800_0000; + +pub const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE: usize = 0x0002_0016; + +pub const PROCESS_TERMINATE: DWORD = 0x0001; +pub const PROCESS_QUERY_INFORMATION: DWORD = 0x0400; +pub const STILL_ACTIVE: DWORD = 259; +pub const WAIT_TIMEOUT: DWORD = 258; + +pub const ERROR_FILE_NOT_FOUND: DWORD = Win32Error::FILE_NOT_FOUND.0 as DWORD; +pub const ERROR_ACCESS_DENIED: DWORD = Win32Error::ACCESS_DENIED.0 as DWORD; +pub const ERROR_INVALID_HANDLE: DWORD = Win32Error::INVALID_HANDLE.0 as DWORD; +pub const ERROR_NOT_SUPPORTED: DWORD = Win32Error::NOT_SUPPORTED.0 as DWORD; +pub const ERROR_INVALID_PARAMETER: DWORD = Win32Error::INVALID_PARAMETER.0 as DWORD; +pub const ERROR_BROKEN_PIPE: DWORD = Win32Error::BROKEN_PIPE.0 as DWORD; +pub const ERROR_PIPE_BUSY: DWORD = Win32Error::PIPE_BUSY.0 as DWORD; +pub const ERROR_IO_PENDING: DWORD = Win32Error::IO_PENDING.0 as DWORD; + +unsafe extern "system" { + pub safe fn GetProcessId(Process: HANDLE) -> DWORD; + pub fn DeleteProcThreadAttributeList(lpAttributeList: *mut u8); + pub fn NeedCurrentDirectoryForExePathW(ExeName: *const u16) -> BOOL; + pub fn GetOverlappedResult( + hFile: HANDLE, + lpOverlapped: *mut OVERLAPPED, + lpNumberOfBytesTransferred: *mut DWORD, + bWait: BOOL, + ) -> BOOL; + pub fn GetProcessIoCounters( + hProcess: HANDLE, + lpIoCounters: *mut bun_windows_sys::IO_COUNTERS, + ) -> BOOL; + pub fn QueryInformationJobObject( + hJob: HANDLE, + JobObjectInformationClass: DWORD, + lpJobObjectInformation: *mut core::ffi::c_void, + cbJobObjectInformationLength: DWORD, + lpReturnLength: *mut DWORD, + ) -> BOOL; +} + +/// The calling thread's last error as a [`Win32Error`]-mapped `bun_sys::Error`. +#[inline] +pub fn last_error(tag: bun_sys::Tag) -> bun_sys::Error { + sys_error(GetLastError(), tag) +} + +/// A Win32 error code as a `bun_sys::Error`; a code without an errno is `EUNKNOWN`. +#[inline] +pub fn sys_error(code: DWORD, tag: bun_sys::Tag) -> bun_sys::Error { + match u16::try_from(code) { + Ok(code) => bun_sys::Error::from_win32(Win32Error::from_raw(code), tag), + Err(_) => bun_sys::Error::from_code(bun_sys::E::EUNKNOWN, tag), + } +} diff --git a/src/standalone_graph/StandaloneModuleGraph.rs b/src/standalone_graph/StandaloneModuleGraph.rs index b57b6888776c..6e8c53a3c405 100644 --- a/src/standalone_graph/StandaloneModuleGraph.rs +++ b/src/standalone_graph/StandaloneModuleGraph.rs @@ -747,7 +747,7 @@ impl File { pub fn stat(&self) -> Stat { let mut result: Stat = bun_core::ffi::zeroed(); result.st_size = self.utf8_contents().len() as _; - // `Stat` is `libc::stat` (POSIX) / `uv_stat_t` (Windows, `st_mode: u64`). + // `Stat` is `libc::stat` (POSIX) / `windows::fs::Stat` (Windows, `st_mode: u64`). result.st_mode = (libc::S_IFREG | 0o644) as _; result } diff --git a/src/symbols.def b/src/symbols.def index b98134a859f4..1b729f80482d 100644 --- a/src/symbols.def +++ b/src/symbols.def @@ -1,440 +1,329 @@ -; To update this list, use: +; uv_* is the list in src/symbols.txt (`exported` in src/jsc/bindings/libuv/generate_uv_stubs_constants.ts), +; defined by src/jsc/bindings/uv-stubs.c and src/jsc/bindings/uv-polyfills.c. ; -; dumpbin /symbols .\src\deps\libuv.lib | Where-Object { $_.Contains('| uv_') } | foreach-object { (($_ -split "\|")[1] -split " ")[1] } | Where-Object { $_ -match 'uv_[a-z]' } | ForEach-Object { " ${_}" } > out.txt; dumpbin /symbols .\build\CMakeFiles\bun-zig.o | Where-Object { $_.Contains('| napi_') } | foreach-object { (($_ -split "\|")[1] -split " ")[1] } | ForEach-Object { " ${_}" } >> out.txt -; -> out.txt +; napi_* can be listed from a local build with: +; +; dumpbin /symbols | Where-Object { $_.Contains('| napi_') } | foreach-object { (($_ -split "\|")[1] -split " ")[1] } | ForEach-Object { " ${_}" } > out.txt ; ; the above will include extra symbols that do not exist, remember ; to double check a local build EXPORTS - uv_fs_poll_init - uv_fs_poll_start - uv_fs_poll_stop - uv_fs_poll_getpath - uv_is_active - uv_now - uv_timer_init - uv_fs_stat - uv_fs_req_cleanup - uv_close - uv_timer_start - uv_inet_ntop - uv_inet_pton - uv_random + uv_accept + uv_async_init + uv_async_send + uv_available_parallelism + uv_backend_fd + uv_backend_timeout + uv_barrier_destroy uv_barrier_init uv_barrier_wait - uv_barrier_destroy - uv_mutex_init - uv_cond_init - uv_mutex_destroy - uv_mutex_lock - uv_cond_wait - uv_cond_broadcast - uv_mutex_unlock - uv_cond_destroy - uv_queue_work + uv_buf_init uv_cancel - uv_mutex_lock - uv_cond_signal - uv_mutex_unlock - uv_thread_join - uv_mutex_destroy + uv_chdir + uv_check_init + uv_check_start + uv_check_stop + uv_clock_gettime + uv_close + uv_cond_broadcast uv_cond_destroy - uv_once uv_cond_init - uv_mutex_init - uv_sem_init - uv_thread_create_ex - uv_sem_wait - uv_sem_destroy - uv_async_send - uv_sem_post + uv_cond_signal + uv_cond_timedwait uv_cond_wait - uv_timer_init - uv_timer_start - uv_timer_stop - uv_timer_again - uv_timer_set_repeat - uv_timer_get_repeat - uv_timer_get_due_in - uv_replace_allocator - uv_os_free_passwd - uv_os_free_group - uv_handle_size - uv_req_size - uv_loop_size - uv_buf_init - uv_err_name_r - uv_err_name - uv_strerror_r - uv_strerror - uv_ip4_addr - uv_ip6_addr - uv_ip4_name - uv_ip6_name - uv_ip_name - uv_tcp_bind - uv_udp_init_ex - uv_udp_init - uv_udp_bind - uv_tcp_connect - uv_udp_connect - uv_udp_send - uv_udp_try_send - uv_udp_recv_start - uv_udp_recv_stop - uv_walk - uv_print_all_handles - uv_print_active_handles - uv_ref - uv_unref - uv_has_ref - uv_stop - uv_now - uv_recv_buffer_size - uv_send_buffer_size - uv_fs_event_getpath - uv_fs_scandir_next - uv_loop_configure - uv_default_loop - uv_loop_new - uv_loop_close - uv_loop_delete - uv_read_start - uv_os_free_environ - uv_free_cpu_info - uv_library_shutdown - uv_metrics_info - uv_metrics_idle_time - uv_inet_pton - uv_inet_ntop - uv_udp_getpeername - uv_loop_init - uv_hrtime - uv_mutex_lock - uv_mutex_unlock - uv_handle_type_name - uv_handle_get_type - uv_handle_get_data - uv_handle_get_loop - uv_handle_set_data - uv_req_type_name - uv_req_get_type - uv_req_get_data - uv_req_set_data - uv_stream_get_write_queue_size - uv_udp_get_send_queue_size - uv_udp_get_send_queue_count - uv_process_get_pid - uv_fs_get_type - uv_fs_get_result - uv_fs_get_ptr - uv_fs_get_path - uv_fs_get_statbuf - uv_loop_get_data - uv_loop_set_data - uv_version - uv_version_string - uv_async_init - uv_async_send - uv_fatal_error - uv_loop_init - uv_update_time - uv_backend_fd - uv_loop_fork - uv_loop_alive - uv_backend_timeout - uv_run - uv_fileno + uv_cpu_info uv_cpumask_size - uv_mutex_lock - uv_mutex_unlock - uv_once - uv_mutex_init - uv_async_init - uv_mutex_destroy - uv_translate_sys_error - uv_fatal_error - uv_is_closing - uv_dlopen + uv_cwd + uv_default_loop + uv_disable_stdio_inheritance uv_dlclose - uv_dlsym uv_dlerror - uv_fatal_error - uv_translate_sys_error - uv_fs_req_cleanup - uv_fs_open + uv_dlopen + uv_dlsym + uv_err_name + uv_err_name_r + uv_exepath + uv_fileno + uv_free_cpu_info + uv_free_interface_addresses + uv_freeaddrinfo + uv_fs_access + uv_fs_chmod + uv_fs_chown uv_fs_close - uv_fs_read - uv_fs_write - uv_fs_unlink + uv_fs_closedir + uv_fs_copyfile + uv_fs_event_getpath + uv_fs_event_init + uv_fs_event_start + uv_fs_event_stop + uv_fs_fchmod + uv_fs_fchown + uv_fs_fdatasync + uv_fs_fstat + uv_fs_fsync + uv_fs_ftruncate + uv_fs_futime + uv_fs_get_path + uv_fs_get_ptr + uv_fs_get_result + uv_fs_get_statbuf + uv_fs_get_system_error + uv_fs_get_type + uv_fs_lchown + uv_fs_link + uv_fs_lstat + uv_fs_lutime uv_fs_mkdir uv_fs_mkdtemp uv_fs_mkstemp - uv_fs_rmdir - uv_fs_scandir + uv_fs_open uv_fs_opendir + uv_fs_poll_getpath + uv_fs_poll_init + uv_fs_poll_start + uv_fs_poll_stop + uv_fs_read uv_fs_readdir - uv_fs_closedir - uv_fs_link - uv_fs_symlink uv_fs_readlink uv_fs_realpath - uv_fs_chown - uv_fs_fchown - uv_fs_lchown - uv_fs_stat - uv_fs_lstat - uv_fs_fstat uv_fs_rename - uv_fs_fsync - uv_fs_fdatasync - uv_fs_ftruncate - uv_fs_copyfile + uv_fs_req_cleanup + uv_fs_rmdir + uv_fs_scandir + uv_fs_scandir_next uv_fs_sendfile - uv_fs_access - uv_fs_chmod - uv_fs_fchmod - uv_fs_utime - uv_fs_futime - uv_fs_lutime + uv_fs_stat uv_fs_statfs - uv_fs_get_system_error - uv_mutex_init - uv_fatal_error - uv_translate_sys_error - uv_mutex_lock - uv_mutex_unlock - uv_guess_handle - uv_fs_event_init - uv_fs_event_start - uv_fs_event_stop - uv_translate_sys_error - uv_fatal_error - uv_directory_watcher_buffer_size - uv_freeaddrinfo + uv_fs_symlink + uv_fs_unlink + uv_fs_utime + uv_fs_write + uv_get_available_memory + uv_get_constrained_memory + uv_get_free_memory + uv_get_osfhandle + uv_get_process_title + uv_get_total_memory uv_getaddrinfo - uv_if_indextoname - uv_if_indextoiid - uv_translate_sys_error uv_getnameinfo - uv_translate_sys_error + uv_getrusage + uv_getrusage_thread + uv_gettimeofday uv_guess_handle - uv_is_active - uv_close - uv_is_closing - uv_get_osfhandle - uv_open_osfhandle - uv_check_stop - uv_idle_stop - uv_prepare_stop - uv_timer_stop - uv_prepare_init - uv_prepare_start - uv_prepare_stop - uv_check_init - uv_check_start - uv_check_stop + uv_handle_get_data + uv_handle_get_loop + uv_handle_get_type + uv_handle_set_data + uv_handle_size + uv_handle_type_name + uv_has_ref + uv_hrtime uv_idle_init uv_idle_start uv_idle_stop - uv_pipe_init + uv_if_indextoiid + uv_if_indextoname + uv_inet_ntop + uv_inet_pton + uv_interface_addresses + uv_ip4_addr + uv_ip4_name + uv_ip6_addr + uv_ip6_name + uv_ip_name + uv_is_active + uv_is_closing + uv_is_readable + uv_is_writable + uv_key_create + uv_key_delete + uv_key_get + uv_key_set + uv_kill + uv_library_shutdown + uv_listen + uv_loadavg + uv_loop_alive + uv_loop_close + uv_loop_configure + uv_loop_delete + uv_loop_fork + uv_loop_get_data + uv_loop_init + uv_loop_new + uv_loop_set_data + uv_loop_size + uv_metrics_idle_time + uv_metrics_info + uv_mutex_destroy + uv_mutex_init + uv_mutex_init_recursive + uv_mutex_lock + uv_mutex_trylock + uv_mutex_unlock + uv_now + uv_once + uv_open_osfhandle + uv_os_environ + uv_os_free_environ + uv_os_free_group + uv_os_free_passwd + uv_os_get_group + uv_os_get_passwd + uv_os_get_passwd2 + uv_os_getenv + uv_os_gethostname + uv_os_getpid + uv_os_getppid + uv_os_getpriority + uv_os_homedir + uv_os_setenv + uv_os_setpriority + uv_os_tmpdir + uv_os_uname + uv_os_unsetenv uv_pipe - uv_pipe_pending_instances uv_pipe_bind uv_pipe_bind2 + uv_pipe_chmod uv_pipe_connect uv_pipe_connect2 + uv_pipe_getpeername + uv_pipe_getsockname + uv_pipe_init uv_pipe_open uv_pipe_pending_count - uv_pipe_getsockname - uv_pipe_getpeername + uv_pipe_pending_instances uv_pipe_pending_type - uv_pipe_chmod - uv_fatal_error - uv_translate_sys_error - uv_close - uv_timer_start - uv_buf_init - uv_timer_stop + uv_poll_init + uv_poll_init_socket + uv_poll_start + uv_poll_stop + uv_prepare_init + uv_prepare_start + uv_prepare_stop + uv_print_active_handles + uv_print_all_handles + uv_process_get_pid + uv_process_kill + uv_queue_work + uv_random + uv_read_start uv_read_stop - uv_timer_init - uv_unref - uv_once - uv_thread_create - uv_thread_create_ex - uv_thread_setaffinity - uv_thread_getaffinity - uv_thread_getcpu - uv_thread_self - uv_key_get - uv_key_set - uv_thread_join - uv_thread_equal - uv_mutex_init - uv_mutex_init_recursive - uv_mutex_destroy - uv_mutex_lock - uv_mutex_trylock - uv_mutex_unlock - uv_rwlock_init + uv_recv_buffer_size + uv_ref + uv_replace_allocator + uv_req_get_data + uv_req_get_type + uv_req_set_data + uv_req_size + uv_req_type_name + uv_resident_set_memory + uv_run uv_rwlock_destroy + uv_rwlock_init uv_rwlock_rdlock - uv_rwlock_tryrdlock uv_rwlock_rdunlock - uv_rwlock_wrlock + uv_rwlock_tryrdlock uv_rwlock_trywrlock + uv_rwlock_wrlock uv_rwlock_wrunlock - uv_sem_init uv_sem_destroy + uv_sem_init uv_sem_post - uv_sem_wait uv_sem_trywait - uv_cond_init - uv_cond_destroy - uv_cond_signal - uv_cond_broadcast - uv_cond_wait - uv_cond_timedwait - uv_key_create - uv_key_delete - uv_fatal_error - uv_cpumask_size - uv_translate_sys_error - uv_poll_init - uv_poll_init_socket - uv_poll_start - uv_poll_stop - uv_translate_sys_error - uv_once - uv_fatal_error - uv_spawn - uv_process_kill - uv_kill - uv_fatal_error - uv_translate_sys_error - uv_once - uv_disable_stdio_inheritance + uv_sem_wait + uv_send_buffer_size + uv_set_process_title + uv_setup_args + uv_shutdown uv_signal_init - uv_signal_stop uv_signal_start uv_signal_start_oneshot - uv_fatal_error - uv_listen - uv_accept - uv_read_stop - uv_write - uv_write2 - uv_try_write - uv_try_write2 - uv_shutdown - uv_is_readable - uv_is_writable + uv_signal_stop + uv_sleep + uv_socketpair + uv_spawn + uv_stop + uv_stream_get_write_queue_size uv_stream_set_blocking - uv_translate_sys_error - uv_tcp_init_ex - uv_tcp_init + uv_strerror + uv_strerror_r + uv_tcp_bind uv_tcp_close_reset - uv_tcp_getsockname + uv_tcp_connect uv_tcp_getpeername - uv_tcp_nodelay + uv_tcp_getsockname + uv_tcp_init + uv_tcp_init_ex uv_tcp_keepalive - uv_tcp_simultaneous_accepts + uv_tcp_nodelay uv_tcp_open - uv_socketpair + uv_tcp_simultaneous_accepts + uv_thread_create + uv_thread_create_ex + uv_thread_detach + uv_thread_equal + uv_thread_getaffinity + uv_thread_getcpu + uv_thread_getname + uv_thread_getpriority + uv_thread_join + uv_thread_self + uv_thread_setaffinity + uv_thread_setname + uv_thread_setpriority + uv_timer_again + uv_timer_get_due_in + uv_timer_get_repeat + uv_timer_init + uv_timer_set_repeat + uv_timer_start + uv_timer_stop uv_translate_sys_error - uv_tcp_non_ifs_lsp_ipv6 - uv_tcp_non_ifs_lsp_ipv4 - uv_close - uv_addr_ip4_any_ - uv_fatal_error - uv_buf_init - uv_read_stop - uv_addr_ip6_any_ - uv_simultaneous_server_accepts - uv_tty_init - uv_tty_set_mode + uv_try_write + uv_try_write2 + uv_tty_get_vterm_state uv_tty_get_winsize - uv_process_tty_read_raw_req - uv_process_tty_read_line_req + uv_tty_init uv_tty_reset_mode + uv_tty_set_mode uv_tty_set_vterm_state - uv_tty_get_vterm_state - uv_sem_init - uv_mutex_init - uv_sem_wait - uv_sem_post - uv_translate_sys_error - uv_buf_init - uv_mutex_lock - uv_mutex_unlock - uv_fatal_error + uv_udp_bind + uv_udp_connect + uv_udp_get_send_queue_count + uv_udp_get_send_queue_size uv_udp_getpeername uv_udp_getsockname - uv_udp_using_recvmmsg + uv_udp_init + uv_udp_init_ex + uv_udp_open + uv_udp_recv_start + uv_udp_recv_stop + uv_udp_send + uv_udp_set_broadcast uv_udp_set_membership - uv_udp_set_source_membership uv_udp_set_multicast_interface - uv_udp_set_broadcast - uv_udp_open - uv_udp_set_ttl - uv_udp_set_multicast_ttl uv_udp_set_multicast_loop - uv_translate_sys_error - uv_udp_recv_stop - uv_addr_ip4_any_ - uv_buf_init - uv_addr_ip6_any_ - uv_ip6_addr - uv_ip4_addr - uv_inet_pton - uv_exepath - uv_cwd - uv_chdir - uv_loadavg - uv_get_free_memory - uv_get_total_memory - uv_get_constrained_memory - uv_get_available_memory - uv_os_getpid - uv_os_getppid - uv_setup_args - uv_set_process_title - uv_get_process_title - uv_clock_gettime - uv_hrtime - uv_resident_set_memory + uv_udp_set_multicast_ttl + uv_udp_set_source_membership + uv_udp_set_ttl + uv_udp_try_send + uv_udp_try_send2 + uv_udp_using_recvmmsg + uv_unref + uv_update_time uv_uptime - uv_available_parallelism - uv_cpu_info - uv_interface_addresses - uv_free_interface_addresses - uv_getrusage - uv_os_homedir - uv_os_getenv - uv_os_get_passwd - uv_os_tmpdir - uv_os_get_passwd2 - uv_os_get_group - uv_os_environ - uv_os_setenv - uv_os_unsetenv - uv_os_gethostname - uv_os_getpriority - uv_os_setpriority - uv_os_uname - uv_gettimeofday - uv_sleep - uv_fatal_error - uv_translate_sys_error - uv_os_free_passwd - uv_fatal_error - uv_addr_ip4_any_ - uv_ip4_addr - uv_addr_ip6_any_ - uv_ip6_addr - uv_tcp_non_ifs_lsp_ipv4 - uv_tcp_non_ifs_lsp_ipv6 - uv_fatal_error + uv_utf16_length_as_wtf8 + uv_utf16_to_wtf8 + uv_version + uv_version_string + uv_walk + uv_write + uv_write2 + uv_wtf8_length_as_utf16 + uv_wtf8_to_utf16 napi_get_undefined napi_get_null napi_get_boolean diff --git a/src/sys/Cargo.toml b/src/sys/Cargo.toml index 13333f01e240..e918ace0da18 100644 --- a/src/sys/Cargo.toml +++ b/src/sys/Cargo.toml @@ -32,6 +32,3 @@ thiserror.workspace = true # kernel via inline asm, skipping glibc's PLT + errno-TLS + cancellation check. # Android: same kernel ABI; rustix's linux_raw backend supports it directly. rustix = { workspace = true } - -[target.'cfg(windows)'.dependencies] -bun_libuv_sys.workspace = true diff --git a/src/sys/Error.rs b/src/sys/Error.rs index 07ef7e676ac8..89bedb9b709b 100644 --- a/src/sys/Error.rs +++ b/src/sys/Error.rs @@ -59,15 +59,6 @@ impl IntoErrnoInt for E { self as Int } } -// On POSIX `E` is a `type` alias for `SystemErrno` (same type → duplicate impl); -// on Windows they are distinct enums, so the second impl is required. -#[cfg(windows)] -impl IntoErrnoInt for SystemErrno { - #[inline] - fn into_errno_int(self) -> Int { - self as Int - } -} impl Error { /// `Error::new(errno, tag)` — dispatches via `IntoErrnoInt` so a single @@ -102,14 +93,16 @@ impl Error { pub fn from_code_int(errno: c_int, syscall_tag: Tag) -> Error { debug_assert!((0..=c_int::from(u16::MAX)).contains(&errno)); Error { + // The kernel's number, also one the table does not declare (FUSE, + // drivers): JS reports it as `err.errno`, as Node does. errno: errno as Int, syscall: syscall_tag, ..Default::default() } } - /// `self.errno` is an `E`/`SystemErrno` discriminant on every platform (not - /// a Win32 or libuv code); one the enum does not declare is `EUNKNOWN`. + /// `self.errno` is a host errno on every platform (not a Win32 code or a + /// `UV_E*` number); one the enum does not declare is `EUNKNOWN`. #[inline] pub fn get_errno(&self) -> E { E::from_raw(self.errno) @@ -176,20 +169,6 @@ impl Error { } } - /// Unlike `with_path`/`with_path_dest` (which reset `fd`), this only - /// overlays `dest`. - #[cfg(windows)] - #[inline] - pub(crate) fn with_dest(&self, dest: &[u8]) -> Error { - Error { - errno: self.errno, - syscall: self.syscall, - fd: self.fd, - path: self.path.clone(), - dest: Box::from(dest), - } - } - #[inline] pub fn with_path_dest(&self, path: &[u8], dest: &[u8]) -> Error { Error { @@ -236,10 +215,13 @@ impl Error { self.resolve_system_errno().unwrap_or(SystemErrno::EIO) } - /// 1. Convert libuv errno values into libc ones. - /// 2. Get the tag name as a string for printing. + /// The errno and its name (e.g. `"ENOENT"`) for printing. A number the + /// table does not declare prints as `EUNKNOWN`, as `get_errno` has it. pub fn get_error_code_tag_name(&self) -> Option<(&'static str, SystemErrno)> { - let e = self.resolve_system_errno()?; + if self.errno == 0 { + return None; + } + let e = SystemErrno::from_repr(self.errno).unwrap_or(SystemErrno::EUNKNOWN); // strum::IntoStaticStr — variant name (e.g., "ENOENT"). Some((<&'static str>::from(e), e)) } @@ -258,6 +240,16 @@ impl Error { Some(coreutils_error_map::COREUTILS_ERROR_MAP[system_errno].as_bytes()) } + /// `err.errno` as Node has it: libuv's number. On POSIX that is the negated + /// host errno; on Windows the errno's `UV_E*` value. + pub fn uv_errno(&self) -> c_int { + #[cfg(windows)] + if let Some(uv) = bun_errno::e_to_uv(self.errno) { + return uv; + } + c_int::from(self.errno).wrapping_neg() + } + /// Shared scaffolding for [`to_shell_system_error`] and [`to_system_error`]. /// Fills `errno`/`syscall`/`code`/`path`/`dest`/`fd`, leaves `message` empty, /// and returns the looked-up `(code, label)` so each caller can build its own @@ -266,17 +258,8 @@ impl Error { &self, map: &enum_map::EnumMap, ) -> (SystemError, Option<(&'static str, &'static str)>) { - // Node reports libuv's codes in `err.errno` on every platform. On POSIX - // that is just the negated host errno; on Windows the discriminant maps - // back to its `UV_E*` value. - #[cfg(windows)] - let js_errno = crate::windows::libuv::e_discriminant_to_uv(self.errno) - .unwrap_or_else(|| c_int::from(self.errno).wrapping_neg()); - #[cfg(not(windows))] - let js_errno = c_int::from(self.errno).wrapping_neg(); - let mut err = SystemError { - errno: js_errno, + errno: self.uv_errno(), syscall: BunString::static_(<&'static str>::from(self.syscall).as_bytes()), ..Default::default() }; @@ -298,12 +281,12 @@ impl Error { if let Some(valid) = fd_unwrap_valid(self.fd) { // When the FD is a windows handle, there is no sane way to report this. #[cfg(windows)] - if valid.kind() == crate::FdKind::Uv { - err.fd = Some(valid.uv()); + if valid.kind() == crate::FdKind::Crt { + err.fd = Some(valid.crt()); } #[cfg(not(windows))] { - err.fd = Some(valid.uv()); + err.fd = Some(valid.crt()); } } @@ -444,38 +427,3 @@ impl bun_core::output::ErrName for &Error { (**self).as_sys_err_info() } } - -// ────────────────────────────────────────────────────────────────────────── -// `ReturnCodeExt` — `ReturnCode::to_error(tag) -> Option` lives here (not -// in `bun_libuv_sys`) because `Error`/`Tag` are higher-tier types. -// ────────────────────────────────────────────────────────────────────────── -#[cfg(windows)] -pub trait ReturnCodeExt: Sized { - /// `Some(errno)` when the return code is negative; `None` on success. - fn errno(self) -> Option; - #[inline] - fn to_error(self, syscall_tag: Tag) -> Option { - self.errno().map(|e| Error::from_code(e, syscall_tag)) - } - #[inline] - fn to_result(self, syscall_tag: Tag) -> crate::Result<()> { - match self.to_error(syscall_tag) { - Some(e) => Err(e), - None => Ok(()), - } - } -} -#[cfg(windows)] -impl ReturnCodeExt for crate::windows::libuv::ReturnCode { - #[inline] - fn errno(self) -> Option { - (self.int() < 0).then(|| crate::windows::translate_uv_error_to_e(self.int())) - } -} -#[cfg(windows)] -impl ReturnCodeExt for crate::windows::libuv::ReturnCodeI64 { - #[inline] - fn errno(self) -> Option { - (self.int() < 0).then(|| crate::windows::translate_uv_error_to_e(self.int() as c_int)) - } -} diff --git a/src/sys/PosixStat.rs b/src/sys/PosixStat.rs index 8abe19a4333b..1cebb21a40c1 100644 --- a/src/sys/PosixStat.rs +++ b/src/sys/PosixStat.rs @@ -84,10 +84,7 @@ pub fn stat_atime(s: &Stat) -> Timespec { } #[cfg(windows)] { - Timespec { - sec: s.atim.sec as i64, - nsec: s.atim.nsec as i64, - } + s.atim } } #[inline] @@ -101,10 +98,7 @@ pub fn stat_mtime(s: &Stat) -> Timespec { } #[cfg(windows)] { - Timespec { - sec: s.mtim.sec as i64, - nsec: s.mtim.nsec as i64, - } + s.mtim } } #[inline] @@ -118,24 +112,18 @@ pub fn stat_ctime(s: &Stat) -> Timespec { } #[cfg(windows)] { - Timespec { - sec: s.ctim.sec as i64, - nsec: s.ctim.nsec as i64, - } + s.ctim } } #[inline] pub fn stat_birthtime(s: &Stat) -> Timespec { // Linux `struct stat` has no birthtime (only `statx` does), so it gets the // ctime arm like libuv's `uv__to_stat`; everything else reads the real - // birthtime. Windows `Stat` is `uv_stat_t` and libuv fills `birthtim` from - // NTFS CreationTime, so it must NOT fall into the ctime arm. + // birthtime. Windows `Stat` fills `birthtim` from NTFS CreationTime, so it + // must NOT fall into the ctime arm. #[cfg(windows)] { - Timespec { - sec: s.birthtim.sec as i64, - nsec: s.birthtim.nsec as i64, - } + s.birthtim } #[cfg(any( target_os = "macos", @@ -194,8 +182,7 @@ impl PosixStat { } #[cfg(windows)] { - // Windows `Stat` is libuv `uv_stat_t` — `st_*`-named u64 fields - // (matches uv.h; see libuv.rs `uv_stat_t`). + // Windows `Stat` fields are already `u64`. PosixStat { dev: stat_.st_dev, ino: stat_.st_ino, diff --git a/src/sys/dir.rs b/src/sys/dir.rs index 0a77bc7f765e..c0245f60088e 100644 --- a/src/sys/dir.rs +++ b/src/sys/dir.rs @@ -126,7 +126,12 @@ impl Dir { name: Vec, parent_dir: Fd, iter: dir_iterator::WrappedIterator, + /// How often the directory was found not empty after all of it was deleted. + rescans: u8, } + // Where deletes lack POSIX semantics (FAT, exFAT), a file that is open + // elsewhere stays listed until it is closed. + const MAX_RESCANS: u8 = 50; // Ensure every still-open iterator dir is closed on early return. let mut stack = scopeguard::guard(Vec::::with_capacity(16), |mut s| { for item in s.drain(..) { @@ -137,6 +142,7 @@ impl Dir { name: sub_path.to_vec(), parent_dir: self.fd, iter: dir_iterator::iterate(initial), + rescans: 0, }); 'process_stack: while let Some(top) = stack.last_mut() { @@ -172,6 +178,7 @@ impl Dir { name: entry.name.slice_u8().to_vec(), parent_dir: parent, iter: dir_iterator::iterate(new_dir), + rescans: 0, }); continue 'process_stack; } else { @@ -201,6 +208,7 @@ impl Dir { let dir_fd = top.iter.dir(); let parent_dir = top.parent_dir; let name = core::mem::take(&mut top.name); + let rescans = top.rescans; // Pop before closing so the cleanup guard doesn't double-close on // an error from `unlinkat_a`. stack.pop(); @@ -211,7 +219,7 @@ impl Dir { Ok(()) => {} Err(e) => match e.get_errno() { E::ENOENT => {} - E::ENOTEMPTY => need_to_retry = true, + E::ENOTEMPTY if rescans < MAX_RESCANS => need_to_retry = true, _ => return Err(e), }, } @@ -245,6 +253,7 @@ impl Dir { name, parent_dir, iter: dir_iterator::iterate(new_dir), + rescans: rescans + 1, }); continue 'process_stack; } @@ -341,11 +350,10 @@ impl Dir { } } - /// `symlinkat(target, self.fd, link)`. The - /// `is_directory` flag is a no-op on POSIX; - /// on Windows it selects junction vs. file-symlink and - /// callers route through `sys_uv::symlink_uv` instead. - pub fn sym_link(&self, target: &[u8], link_name: &[u8], _is_directory: bool) -> Maybe<()> { + /// `symlinkat(target, self.fd, link)`. On Windows this always makes a file + /// symlink; callers that need a directory symlink or junction use + /// `symlink_dir` / `junction`. + pub fn sym_link(&self, target: &[u8], link_name: &[u8]) -> Maybe<()> { let mut tbuf = bun_paths::path_buffer_pool::get(); let tlen = target.len().min(tbuf.0.len() - 1); tbuf.0[..tlen].copy_from_slice(&target[..tlen]); diff --git a/src/sys/fd.rs b/src/sys/fd.rs index add0523b5926..f1f4297f0072 100644 --- a/src/sys/fd.rs +++ b/src/sys/fd.rs @@ -1,7 +1,5 @@ -#[cfg(any(target_os = "macos", windows))] +#[cfg(target_os = "macos")] use core::ffi::c_int; -#[cfg(windows)] -use core::ffi::c_void; // `Fd` (the packed handle struct + pure-data accessors) is canonical in // bun_core. This file adds the syscall-touching surface as an extension trait. @@ -16,18 +14,6 @@ use crate as sys; bun_core::define_scoped_log!(log, SYS, visible); -#[derive(Copy, Clone, Eq, PartialEq)] -pub enum ErrorCase { - CloseOnFail, - LeakFdOnFail, -} - -#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq, strum::IntoStaticStr)] -pub enum MakeLibUvOwnedError { - #[error("SystemFdQuotaExceeded")] - SystemFdQuotaExceeded, -} - // ────────────────────────────────────────────────────────────────────────── // FdExt — syscall-touching methods on `bun_core::Fd`. // @@ -56,13 +42,10 @@ pub trait FdExt: Copy + Sized { /// Use fd API to implement `node:fs` close: stdio must actually close and /// EBADF must surface to the caller. Consider fd the raw close method. fn close_allowing_standard_io(self, return_address: Option) -> Option; - /// Assumes given a valid file descriptor. If error, the handle has not been closed. - fn make_lib_uv_owned(self) -> Result; - fn make_lib_uv_owned_for_syscall( - self, - syscall_tag: sys::Tag, - error_case: ErrorCase, - ) -> sys::Result; + /// Give a HANDLE a slot in the C runtime's fd table, for an fd that JS + /// gets to see. `EMFILE` from `open` when the table is full, and the HANDLE + /// is closed then. + fn make_crt_owned_for_syscall(self) -> sys::Result; fn make_path_u8(self, subpath: &[u8]) -> sys::Maybe<()>; fn delete_tree(self, subpath: &[u8]) -> sys::Maybe<()>; } @@ -145,8 +128,7 @@ impl FdExt for Fd { } #[cfg(windows)] { - use sys::ReturnCodeExt as _; - use sys::windows::{NTSTATUS, libuv as uv}; + use sys::windows::NTSTATUS; match self.decode_windows() { // It decodes to ntdll's handle; `close(AT_FDCWD)` is EBADF too. _ if self == Fd::cwd() => Some(sys::Error { @@ -155,31 +137,24 @@ impl FdExt for Fd { fd: self, ..Default::default() }), - DecodeWindows::Uv(file_number) => { - let mut req = uv::fs_t::uninitialized(); - // SAFETY: synchronous libuv fs call (cb = None); req lives on the - // stack for the duration of the call. - let rc = unsafe { - uv::uv_fs_close(uv::Loop::get(), &mut req, file_number, None) - }; - // fs_t has no Drop impl, so cleanup - // must be explicit (uv_fs_req_cleanup). - req.deinit(); - rc.to_error(sys::Tag::close).map(|e| e.with_fd(self)) - } - DecodeWindows::Windows(handle) => { - unsafe extern "system" { - // safe: by-value `HANDLE` only; bad/stale handle → - // `STATUS_INVALID_HANDLE`, never UB (mirrors POSIX - // `close(fd)` → `EBADF`, which is `safe fn` in - // `safe_libc`). - safe fn NtClose(Handle: bun_windows_sys::HANDLE) -> NTSTATUS; - } - match NtClose(handle) { - NTSTATUS::SUCCESS => None, - rc => Some(sys::Error::new(rc, sys::Tag::CloseHandle).with_fd(self)), + // `_close` also closes the HANDLE the CRT fd owns. The + // CRT's fds 0-2 stay open (and report success), as in Node. + DecodeWindows::Crt(file_number) => { + if file_number > 2 && bun_core::fd::crt_close(file_number) == -1 { + Some(sys::Error { + errno: sys::E::EBADF as _, + syscall: sys::Tag::close, + fd: self, + ..Default::default() + }) + } else { + None } } + DecodeWindows::Windows(handle) => match bun_windows_sys::NtClose(handle) { + NTSTATUS::SUCCESS => None, + rc => Some(sys::Error::new(rc, sys::Tag::CloseHandle).with_fd(self)), + }, } } }; @@ -214,47 +189,24 @@ impl FdExt for Fd { result } - fn make_lib_uv_owned(self) -> Result { - debug_assert!(self.is_valid()); + fn make_crt_owned_for_syscall(self) -> sys::Result { #[cfg(not(windows))] { Ok(self) } #[cfg(windows)] { + debug_assert!(self.is_valid()); match self.kind() { + FdKind::Crt => Ok(self), FdKind::System => { - let n = uv_open_osfhandle(self.native())?; - Ok(Fd::from_uv(n)) - } - FdKind::Uv => Ok(self), - } - } - } - - fn make_lib_uv_owned_for_syscall( - self, - syscall_tag: sys::Tag, - error_case: ErrorCase, - ) -> sys::Result { - #[cfg(not(windows))] - { - let _ = (syscall_tag, error_case); - Ok(self) - } - #[cfg(windows)] - { - match self.make_lib_uv_owned() { - Ok(fd) => Ok(fd), - Err(MakeLibUvOwnedError::SystemFdQuotaExceeded) => { - if matches!(error_case, ErrorCase::CloseOnFail) { - self.close(); + let n = bun_core::fd::crt_open_osfhandle(self.native()); + debug_assert!(n >= -1); + if n != -1 { + return Ok(Fd::from_crt(n)); } - Err(sys::Error { - errno: sys::E::EMFILE as _, - syscall: syscall_tag, - ..Default::default() - }) + self.close(); + Err(sys::Error::from_code(sys::E::EMFILE, sys::Tag::open)) } } } @@ -271,9 +223,8 @@ impl FdExt for Fd { } } -// `fromJS` / `fromJSValidated` / `toJS` / `toJSWithoutMakingLibUVOwned` are -// `*_jsc` aliases — deleted per PORTING.md; they live as extension-trait -// methods in `bun_sys_jsc`. +// `from_js` / `from_js_validated` / `to_js` / `to_js_without_making_crt_owned` +// are extension-trait methods in `bun_sys_jsc`. // There are deliberately no `std::fs::File`/`Dir` conversion helpers // (std::fs is banned). Callers use @@ -293,7 +244,7 @@ impl FdExt for Fd { // bun.sys.File. // ────────────────────────────────────────────────────────────────────────── -// Platform helpers (Windows libuv / macOS close_nocancel). +// Platform helpers (macOS close_nocancel). // ────────────────────────────────────────────────────────────────────────── #[cfg(target_os = "macos")] unsafe extern "C" { @@ -303,16 +254,6 @@ unsafe extern "C" { safe fn close_nocancel(fd: c_int) -> c_int; } -#[cfg(windows)] -fn uv_open_osfhandle(in_: *mut c_void) -> Result { - let out = bun_core::fd::uv_open_osfhandle(in_); - debug_assert!(out >= -1); - if out == -1 { - return Err(MakeLibUvOwnedError::SystemFdQuotaExceeded); - } - Ok(out) -} - // fd → path bodies moved down to `bun_core::fd_path_raw[_w]` (libc/kernel32- // only; PORTING.md "move storage down"). `bun_sys` keeps the richer // `get_fd_path[_w]` returning `Maybe<&mut [u8/u16]>` for callers that want diff --git a/src/sys/file.rs b/src/sys/file.rs index 8d29752deee2..091e9b67e021 100644 --- a/src/sys/file.rs +++ b/src/sys/file.rs @@ -263,9 +263,7 @@ impl File { pub fn get_pos(&self) -> Maybe { lseek(self.handle, 0, libc::SEEK_CUR).map(|p| p as u64) } - /// The file's size in bytes. - /// On Windows that's `GetFileSizeEx` directly on the HANDLE (NOT via - /// libuv `fstat`, which would require a uv-kind fd). + /// The file's size in bytes (`GetFileSizeEx` on Windows). pub fn get_end_pos(&self) -> Maybe { get_file_size(self.handle).map(|n| n as usize) } diff --git a/src/sys/lib.rs b/src/sys/lib.rs index c89037345c8d..ac39134f256b 100644 --- a/src/sys/lib.rs +++ b/src/sys/lib.rs @@ -13,15 +13,11 @@ // `bun_core` so any external `bun_sys::bun_core::…` paths continue to resolve. #[cfg(windows)] pub extern crate bun_core as bun_str; -#[cfg(windows)] -pub extern crate bun_libuv_sys; pub mod fd; -pub use fd::{ErrorCase, FdExt, MakeLibUvOwnedError, RawFd}; +pub use fd::{FdExt, RawFd}; #[path = "Error.rs"] mod error; pub use error::Error; -#[cfg(windows)] -pub use error::ReturnCodeExt; impl From for bun_errno::SystemErrno { #[inline] fn from(e: Error) -> Self { @@ -51,15 +47,6 @@ impl SystemError { /// (`Error::to_system_error` stores `errno` negated to match Node.) #[inline] pub fn get_errno(&self) -> E { - // On Windows `self.errno` is a libuv code (e.g. UV_EBUSY = -4082); - // canonicalize to the small `E` discriminant so Rust-side callers that - // compare against `E::BUSY`/`E::BADF` keep matching. - #[cfg(windows)] - if let Some(d) = crate::windows::libuv::uv_err_to_e_discriminant(self.errno) { - if let Some(e) = E::try_from_raw(d) { - return e; - } - } e_from_negated(self.errno) } } @@ -888,9 +875,7 @@ pub fn getcwd_alloc() -> Maybe { Ok(bun_core::ZBox::from_bytes(&buf[..len])) } -/// `getcwd` returning a NUL-terminated -/// borrow into `buf`. POSIX `getcwd(3)` already NUL-terminates; on Windows -/// the libuv path does too. +/// `getcwd` returning a NUL-terminated borrow into `buf`. pub fn getcwd_z(buf: &mut bun_paths::PathBuffer) -> Maybe<&ZStr> { let len = getcwd(&mut buf[..])?; debug_assert!(len < buf.len()); @@ -1115,33 +1100,17 @@ pub mod O { pub const RDONLY: i32 = libc::O_RDONLY; pub const WRONLY: i32 = libc::O_WRONLY; pub const RDWR: i32 = libc::O_RDWR; - // On Windows the `O.*` constants are fixed octal, Linux-shaped values, - // NOT MSVCRT `_O_*`. `uv::O::from_bun_o` bit-tests - // against these exact values, so re-using `libc::O_CREAT` (0x100) etc. - // silently dropped CREAT/EXCL/APPEND on Windows. - #[cfg(unix)] pub const CREAT: i32 = libc::O_CREAT; - #[cfg(unix)] pub const TRUNC: i32 = libc::O_TRUNC; - #[cfg(unix)] pub const APPEND: i32 = libc::O_APPEND; - #[cfg(unix)] pub const EXCL: i32 = libc::O_EXCL; - #[cfg(windows)] - pub const CREAT: i32 = 0o100; - #[cfg(windows)] - pub const EXCL: i32 = 0o200; - #[cfg(windows)] - pub const TRUNC: i32 = 0o1000; - #[cfg(windows)] - pub const APPEND: i32 = 0o2000; #[cfg(unix)] pub const NONBLOCK: i32 = libc::O_NONBLOCK; #[cfg(unix)] pub const CLOEXEC: i32 = libc::O_CLOEXEC; - // Windows libc has no `O_NONBLOCK`/`O_CLOEXEC`; libuv ignores them. Values - // chosen to round-trip through `uv::O::from_bun_o` without colliding with - // the `_O_*` flags MSVCRT defines. + // Windows libc has no `O_NONBLOCK`/`O_CLOEXEC`; `open` ignores them. These + // and the other values MSVCRT lacks are not those of an `_O_*` flag `open` + // reads (`windows::O::from_js`). #[cfg(windows)] pub const NONBLOCK: i32 = 0o4000; #[cfg(windows)] @@ -1190,6 +1159,12 @@ pub mod O { pub const NOCTTY: i32 = 0; #[cfg(windows)] pub const ACCMODE: i32 = 3; + /// Windows: the file is read or written front to back, which the cache + /// manager reads ahead and unmaps behind for. Nothing to say elsewhere. + #[cfg(windows)] + pub const SEQUENTIAL: i32 = libc::O_SEQUENTIAL; + #[cfg(not(windows))] + pub const SEQUENTIAL: i32 = 0; #[cfg(target_os = "macos")] pub const SYMLINK: i32 = libc::O_SYMLINK; #[cfg(not(target_os = "macos"))] @@ -1215,13 +1190,12 @@ pub use dir::*; #[cfg(unix)] pub type Stat = libc::stat; -/// On Windows `bun.Stat` is libuv's `uv_stat_t`. #[cfg(windows)] -pub type Stat = bun_libuv_sys::uv_stat_t; +pub use windows::fs::Stat; // ────────────────────────────────────────────────────────────────────────── // Syscall surface — real posix libc FFI. Windows path lives in -// `windows_impl` (NT/kernel32/libuv triad) below. +// `windows_impl` below. // ────────────────────────────────────────────────────────────────────────── use bun_core::ZStr; @@ -1234,15 +1208,6 @@ pub fn last_errno() -> i32 { bun_core::ffi::errno() } -/// Copy `path` into a NUL-terminated buffer. -/// Returns `ENAMETOOLONG` if `path` contains an interior NUL. -#[inline] -pub fn to_posix_path( - path: &[u8], -) -> core::result::Result { - std::ffi::CString::new(path).map_err(|_| bun_errno::SystemErrno::ENAMETOOLONG) -} - #[inline] #[cfg(not(windows))] fn err_with(tag: Tag) -> Error { @@ -1261,25 +1226,27 @@ impl Tag { pub const access: Tag = Tag(2); pub const connect: Tag = Tag(3); pub const chmod: Tag = Tag(4); + #[cfg(not(windows))] pub(crate) const chown: Tag = Tag(5); pub const clonefile: Tag = Tag(6); pub const close: Tag = Tag(8); pub const copy_file_range: Tag = Tag(9); pub const copyfile: Tag = Tag(10); - pub(crate) const fchmod: Tag = Tag(11); + pub const fchmod: Tag = Tag(11); pub const fchmodat: Tag = Tag(12); - pub(crate) const fchown: Tag = Tag(13); + pub const fchown: Tag = Tag(13); pub(crate) const fcntl: Tag = Tag(14); pub const fdatasync: Tag = Tag(15); pub const fstat: Tag = Tag(16); pub const fstatat: Tag = Tag(17); pub const fsync: Tag = Tag(18); - pub(crate) const ftruncate: Tag = Tag(19); + pub const ftruncate: Tag = Tag(19); #[cfg(not(windows))] pub(crate) const futimens: Tag = Tag(20); pub const getdents64: Tag = Tag(21); pub const getdirentries64: Tag = Tag(22); pub const lchmod: Tag = Tag(23); + #[cfg(not(windows))] pub(crate) const lchown: Tag = Tag(24); pub const link: Tag = Tag(25); pub(crate) const lseek: Tag = Tag(26); @@ -1292,7 +1259,9 @@ impl Tag { pub const mmap: Tag = Tag(33); pub(crate) const munmap: Tag = Tag(34); pub const open: Tag = Tag(35); + #[cfg(not(windows))] pub(crate) const pread: Tag = Tag(36); + #[cfg(not(windows))] pub(crate) const pwrite: Tag = Tag(37); pub const read: Tag = Tag(38); pub const readlink: Tag = Tag(39); @@ -1336,17 +1305,14 @@ impl Tag { #[cfg(not(windows))] pub(crate) const preadv: Tag = Tag(77); pub const ioctl_ficlone: Tag = Tag(78); - pub const accept: Tag = Tag(79); + // 79 is `accept`, 81 `connect2` and 84 `try_write` in `name()`; nothing reports them. pub const bind2: Tag = Tag(80); - pub const connect2: Tag = Tag(81); pub const listen: Tag = Tag(82); pub const pipe: Tag = Tag(83); - pub const try_write: Tag = Tag(84); pub const socketpair: Tag = Tag(85); pub const setsockopt: Tag = Tag(86); pub const rm: Tag = Tag(88); pub const uv_spawn: Tag = Tag(89); - pub const uv_pipe: Tag = Tag(90); pub const uv_tty_set_mode: Tag = Tag(91); pub const uv_os_homedir: Tag = Tag(93); pub const WriteFile: Tag = Tag(94); @@ -1357,8 +1323,7 @@ impl Tag { pub(crate) const GetFinalPathNameByHandle: Tag = Tag(97); #[cfg(windows)] pub(crate) const CloseHandle: Tag = Tag(98); - #[cfg(windows)] - pub(crate) const SetFilePointerEx: Tag = Tag(99); + // 99 is `SetFilePointerEx` in `name()`; nothing reports it. pub const SetEndOfFile: Tag = Tag(100); // ── later additions — appended above the frozen range so existing // discriminants never shift. @@ -2301,11 +2266,7 @@ mod posix_impl { SUPPORTS_STATX_ON_LINUX.store(false, Ordering::Relaxed); return statx_fallback(fd, path, flags); } - return Err(Error { - errno: raw_errno as _, - syscall, - ..Default::default() - }); + return Err(Error::from_code_int(raw_errno, syscall)); } // SAFETY: rc == 0 ⇒ kernel populated the buffer. @@ -3009,7 +2970,7 @@ mod posix_impl { check!(safe_libc::fchdir(fd.native()), Tag::fchdir); Ok(()) } - pub fn umask(mode: Mode) -> Mode { + pub(crate) fn libc_umask(mode: Mode) -> Mode { // `Mode` is normalized to u32 across platforms; libc::mode_t is u16 on // Darwin/FreeBSD and u32 on Linux — cast at the boundary. safe_libc::umask(mode as libc::mode_t) as Mode @@ -3471,6 +3432,59 @@ pub use posix_impl::*; // impls in posix_impl/windows_impl were uncached duplicates. pub use bun_alloc::page_size; +/// The process umask. libc can only read it by setting it, which a file +/// created on another thread at that moment sees, so it is kept here: +/// [`init_umask`] reads it once, [`umask`] is the only thing that sets it. +static UMASK: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0); +static UMASK_SET_LOCK: bun_core::Mutex<()> = bun_core::Mutex::new(()); +#[cfg(debug_assertions)] +static UMASK_WAS_READ: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false); + +/// Call once, before the process has a second thread. +pub fn init_umask() { + let mask = libc_umask(0); + libc_umask(mask); + UMASK.store(mask, core::sync::atomic::Ordering::Relaxed); + #[cfg(debug_assertions)] + UMASK_WAS_READ.store(true, core::sync::atomic::Ordering::Relaxed); +} + +#[inline] +pub fn get_umask() -> Mode { + #[cfg(debug_assertions)] + debug_assert!( + UMASK_WAS_READ.load(core::sync::atomic::Ordering::Relaxed), + "init_umask() has not run" + ); + UMASK.load(core::sync::atomic::Ordering::Relaxed) +} + +/// `umask(2)`: sets the process umask and returns the one it replaces. +pub fn umask(mode: Mode) -> Mode { + #[cfg(debug_assertions)] + debug_assert!( + UMASK_WAS_READ.load(core::sync::atomic::Ordering::Relaxed), + "init_umask() has not run" + ); + let _guard = UMASK_SET_LOCK.lock(); + libc_umask(mode); + // Setting it again returns the bits of `mode` libc kept. + let kept = libc_umask(mode); + UMASK.swap(kept, core::sync::atomic::Ordering::Relaxed) +} + +/// `process.umask()`. +#[unsafe(no_mangle)] +pub(crate) extern "C" fn Bun__getUmask() -> u32 { + get_umask() +} + +/// `process.umask(mask)`. Returns the umask it replaces. +#[unsafe(no_mangle)] +pub(crate) extern "C" fn Bun__setUmask(mask: u32) -> u32 { + umask(mask) +} + /// `bun.jsc.Node.TimeLike` — `timespec` shape, decoupled from JSC (T6). /// futimens/utimens take this; the JSC binding constructs it from /// JS Date/number. T1 owns the data shape. @@ -3490,386 +3504,51 @@ impl TimeLike { } } } +/// `TimeLike::nsec` value meaning "the current time". #[cfg(unix)] pub const UTIME_NOW: i64 = libc::UTIME_NOW; #[cfg(windows)] pub const UTIME_NOW: i64 = -1; - +/// `TimeLike::nsec` value meaning "leave this timestamp unchanged". +#[cfg(unix)] +pub const UTIME_OMIT: i64 = libc::UTIME_OMIT; #[cfg(windows)] -#[path = "sys_uv.rs"] -pub mod sys_uv; - -/// On non-Windows, `sys_uv` is just an alias for the regular syscall surface so -/// callers (e.g. `pack_command`) can write `bun_sys::sys_uv::fstat(fd)` portably. -#[cfg(not(windows))] -pub mod sys_uv { - pub use super::{ - close, fstat, lstat, mkdir, open, pread, pwrite, read, rename, stat, unlink, write, - }; -} +pub const UTIME_OMIT: i64 = -2; #[cfg(windows)] mod windows_impl { - // NT/kernel32/libuv triad. The libuv-backed ops - // delegate to `crate::sys_uv`; the rest call NT/kernel32 directly. + // Every fd-taking op accepts both kinds of `Fd`: `fd.native()` resolves a + // CRT fd to the HANDLE it owns. use super::windows as w; - use super::windows::libuv as uv; use super::*; - // ── libuv-backed ───────────────────────────────────────────────────── - pub fn open(path: &ZStr, flags: i32, mode: Mode) -> Maybe { - sys_uv::open(path, flags, mode) - } + pub use w::fs::{ + chmod, chown, fchmod, fchown, fdatasync, fstat, fsync, ftruncate, futimens, isatty, + junction, lchown, link, lstat, lutimens, mkdir, mkdtemp, open, pipe, pread, pwrite, read, + readlink, realpath, rename, stat, symlink, symlink_dir, unlink, utimens, write, + }; + pub fn close(fd: Fd) -> Maybe<()> { - match sys_uv::close(fd) { + match fd.close_allowing_bad_file_descriptor(None) { Some(e) => Err(e), None => Ok(()), } } - pub fn read(fd: Fd, buf: &mut [u8]) -> Maybe { - // libuv for uv-kind fds, kernel32 `ReadFile` otherwise. The libuv path - // requires a CRT fd via `fd.uv()`, which PANICS for HANDLE-backed - // (`FdKind::System`) Fds — i.e. anything from `openat()`/NtCreateFile. - if fd.kind() == FdKind::Uv { - return sys_uv::read(fd, buf); - } - let adjusted_len = buf.len().min(MAX_COUNT) as w::DWORD; - // Stdin callers route through this function (via - // `File::stdin().read_to_end_into` / `output_sink().read`), so the - // BROKEN_PIPE/HANDLE_EOF → 0 (EOF) mapping and the OPERATION_ABORTED - // retry live here. - loop { - let mut amount_read: w::DWORD = 0; - // SAFETY: FFI; `fd.cast()` is a valid HANDLE, buf valid for `adjusted_len`. - let rc = unsafe { - w::kernel32::ReadFile( - fd.native(), - buf.as_mut_ptr(), - adjusted_len, - &mut amount_read, - core::ptr::null_mut(), - ) - }; - if rc == 0 { - let er = w::Win32Error::get(); - match er { - w::Win32Error::BROKEN_PIPE | w::Win32Error::HANDLE_EOF => return Ok(0), - w::Win32Error::OPERATION_ABORTED => continue, - _ => return Err(Error::from_win32(er, Tag::read).with_fd(fd)), - } - } - return Ok(amount_read as usize); - } - } - pub fn write(fd: Fd, buf: &[u8]) -> Maybe { - // kernel32 `WriteFile` directly - // (NOT via libuv — sys_uv::write → fd.uv() panics for HANDLE-backed - // Fds). Also remaps `ERROR_ACCESS_DENIED → EBADF` (a write to a - // read-only-opened HANDLE yields ACCESS_DENIED, which POSIX surfaces - // as EBADF "fd not open for writing"). - debug_assert!(!buf.is_empty()); - let adjusted_len = buf.len().min(MAX_COUNT) as w::DWORD; - let mut bytes_written: w::DWORD = 0; - // SAFETY: FFI; `fd.cast()` is a valid HANDLE, buf valid for `adjusted_len`. - let rc = unsafe { - w::kernel32::WriteFile( - fd.native(), - buf.as_ptr(), - adjusted_len, - &mut bytes_written, - core::ptr::null_mut(), - ) - }; - if rc == 0 { - let er = w::Win32Error::get(); - let err = if er == w::Win32Error::ACCESS_DENIED { - Error::from_code(E::EBADF, Tag::write) - } else { - Error::from_win32(er, Tag::write) - }; - return Err(err.with_fd(fd)); - } - Ok(bytes_written as usize) - } - pub fn pread(fd: Fd, buf: &mut [u8], off: i64) -> Maybe { - // Positioned-I/O lowering: - // libuv path for uv-kind fds, kernel32 ReadFile+OVERLAPPED for system - // (HANDLE) fds. - if fd.kind() == FdKind::Uv { - return sys_uv::pread(fd, buf, off); - } - let off = off as u64; - let adjusted_len = buf.len().min(MAX_COUNT) as w::DWORD; - loop { - let mut overlapped = w::OVERLAPPED { - Internal: 0, - InternalHigh: 0, - Offset: off as w::DWORD, - OffsetHigh: (off >> 32) as w::DWORD, - hEvent: core::ptr::null_mut(), - }; - let mut amount_read: w::DWORD = 0; - // SAFETY: FFI; `fd.cast()` is a valid HANDLE for the System-kind - // arm, `buf` valid for `adjusted_len`, `overlapped` lives for the - // synchronous call (handle was not opened FILE_FLAG_OVERLAPPED). - let rc = unsafe { - w::kernel32::ReadFile( - fd.native(), - buf.as_mut_ptr(), - adjusted_len, - &mut amount_read, - core::ptr::from_mut(&mut overlapped).cast(), - ) - }; - if rc == 0 { - let er = w::Win32Error::get(); - match er { - // BROKEN_PIPE/HANDLE_EOF map to EOF (0 bytes read). - w::Win32Error::BROKEN_PIPE | w::Win32Error::HANDLE_EOF => return Ok(0), - w::Win32Error::OPERATION_ABORTED => continue, - _ => return Err(Error::from_win32(er, Tag::pread).with_fd(fd)), - } - } - return Ok(amount_read as usize); - } - } - pub(crate) fn pwrite(fd: Fd, buf: &[u8], off: i64) -> Maybe { - // Same lowering as `pread`: kernel32 WriteFile with an - // `OVERLAPPED.Offset` for HANDLE-kind fds. - if fd.kind() == FdKind::Uv { - return sys_uv::pwrite(fd, buf, off); - } - let off = off as u64; - let adjusted_len = buf.len().min(MAX_COUNT) as w::DWORD; - let mut overlapped = w::OVERLAPPED { - Internal: 0, - InternalHigh: 0, - Offset: off as w::DWORD, - OffsetHigh: (off >> 32) as w::DWORD, - hEvent: core::ptr::null_mut(), - }; - let mut bytes_written: w::DWORD = 0; - // SAFETY: FFI; `fd.cast()` is a valid HANDLE for the System-kind arm, - // `buf` valid for `adjusted_len`, `overlapped` lives for the - // synchronous call (handle was not opened FILE_FLAG_OVERLAPPED). - let rc = unsafe { - w::kernel32::WriteFile( - fd.native(), - buf.as_ptr(), - adjusted_len, - &mut bytes_written, - core::ptr::from_mut(&mut overlapped).cast(), - ) - }; - if rc == 0 { - let er = w::Win32Error::get(); - let err = if er == w::Win32Error::ACCESS_DENIED { - Error::from_code(E::EBADF, Tag::pwrite) - } else { - Error::from_win32(er, Tag::pwrite) - }; - return Err(err.with_fd(fd)); - } - Ok(bytes_written as usize) - } - pub fn stat(path: &ZStr) -> Maybe { - sys_uv::stat(path) - } - pub fn fstat(fd: Fd) -> Maybe { - if fd.kind() == FdKind::Uv { - return sys_uv::fstat(fd); - } - // HANDLE-backed (`FdKind::System`, e.g. `openat()` results): stat the - // HANDLE directly instead of allocating a throwaway CRT fd via - // `_open_osfhandle` (which cannot be `_close`d without also closing - // the caller's HANDLE, so it leaked a CRT slot per call). - fstat_handle(fd) - } - /// Port of libuv's `fs__fstat_handle` + `fs__stat_handle` + - /// `fs__stat_assign_statbuf` (`src/win/fs.c`). Fills a `uv_stat_t` from a - /// raw HANDLE without touching the CRT fd table. - fn fstat_handle(fd: Fd) -> Maybe { - use bun_core::S; - let handle = fd.native(); - let nt_err = |rc: w::NTSTATUS| Error::new(rc, Tag::fstat).with_fd(fd); - let mut st: Stat = bun_core::ffi::zeroed(); - - // Dispatch on handle type; pipes and consoles get a synthetic stat. - let file_type = w::GetFileType(handle); - if file_type == w::FILE_TYPE_PIPE { - st.st_mode = S::IFIFO as u64; - st.st_nlink = 1; - st.st_rdev = (w::FILE_DEVICE_NAMED_PIPE as u64) << 16; - st.st_ino = handle as usize as u64; - return Ok(st); - } - if file_type == w::FILE_TYPE_CHAR { - let mut mode: w::DWORD = 0; - // SAFETY: FFI; `handle` is a valid HANDLE, `mode` valid for write. - if unsafe { w::kernel32::GetConsoleMode(handle, &mut mode) } != 0 { - st.st_mode = S::IFCHR as u64; - st.st_nlink = 1; - st.st_rdev = (w::FILE_DEVICE_CONSOLE as u64) << 16; - st.st_ino = handle as usize as u64; - return Ok(st); - } - // Non-console char device (NUL, COM1, ...): fall through to the - // disk path, which special-cases `FILE_DEVICE_NULL`. - } else if file_type != w::FILE_TYPE_DISK { - return Err(Error::new(E::EBADF, Tag::fstat).with_fd(fd)); - } - - let mut io: w::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); - - let mut device_info: w::FILE_FS_DEVICE_INFORMATION = bun_core::ffi::zeroed(); - // SAFETY: FFI; `handle` valid, output buffers valid for write. - let rc = unsafe { - w::ntdll::NtQueryVolumeInformationFile( - handle, - &mut io, - core::ptr::from_mut(&mut device_info).cast(), - core::mem::size_of::() as u32, - w::FS_INFORMATION_CLASS::FileFsDeviceInformation, - ) - }; - if w::NT_ERROR(rc) { - return Err(nt_err(rc)); - } - if device_info.DeviceType == w::FILE_DEVICE_NULL { - st.st_mode = (S::IFCHR | S::IRUSR | S::IWUSR) as u64; - st.st_mode |= (st.st_mode & 0o700) >> 3 | (st.st_mode & 0o700) >> 6; - st.st_nlink = 1; - st.st_blksize = 4096; - st.st_rdev = (w::FILE_DEVICE_NULL as u64) << 16; - return Ok(st); - } - - let mut file_info: w::FILE_ALL_INFORMATION = bun_core::ffi::zeroed(); - // SAFETY: FFI; `handle` valid, output buffers valid for write. - // STATUS_BUFFER_OVERFLOW (variable-length name truncated) is expected - // and is a warning, not an error; `NT_ERROR` excludes it. - let rc = unsafe { - w::ntdll::NtQueryInformationFile( - handle, - &mut io, - core::ptr::from_mut(&mut file_info).cast(), - core::mem::size_of::() as u32, - w::FILE_INFORMATION_CLASS::FileAllInformation, - ) - }; - if w::NT_ERROR(rc) { - return Err(nt_err(rc)); - } - - let mut volume_info: w::FILE_FS_VOLUME_INFORMATION = bun_core::ffi::zeroed(); - // SAFETY: FFI; `handle` valid, output buffers valid for write. - let rc = unsafe { - w::ntdll::NtQueryVolumeInformationFile( - handle, - &mut io, - core::ptr::from_mut(&mut volume_info).cast(), - core::mem::size_of::() as u32, - w::FS_INFORMATION_CLASS::FileFsVolumeInformation, - ) - }; - if rc == w::NTSTATUS::NOT_IMPLEMENTED { - st.st_dev = 0; - } else if w::NT_ERROR(rc) { - return Err(nt_err(rc)); - } else { - st.st_dev = volume_info.VolumeSerialNumber as u64; - } - - // libuv's `S_IFLNK` arm is gated on `do_lstat`, which is always 0 on - // the fstat path, so reparse points fall through to DIR-or-REG. - let attrs = file_info.BasicInformation.FileAttributes; - if attrs & w::FILE_ATTRIBUTE_DIRECTORY != 0 { - st.st_mode = S::IFDIR as u64; - st.st_size = 0; - } else { - st.st_mode = S::IFREG as u64; - st.st_size = file_info.StandardInformation.EndOfFile as u64; - } - if attrs & w::FILE_ATTRIBUTE_READONLY != 0 { - st.st_mode |= S::IRUSR as u64; - } else { - st.st_mode |= (S::IRUSR | S::IWUSR) as u64; - } - st.st_mode |= (st.st_mode & 0o700) >> 3 | (st.st_mode & 0o700) >> 6; - - st.atim = w::filetime_to_timespec(file_info.BasicInformation.LastAccessTime); - st.mtim = w::filetime_to_timespec(file_info.BasicInformation.LastWriteTime); - st.ctim = w::filetime_to_timespec(file_info.BasicInformation.ChangeTime); - st.birthtim = w::filetime_to_timespec(file_info.BasicInformation.CreationTime); - st.st_ino = file_info.InternalInformation.IndexNumber as u64; - st.st_nlink = file_info.StandardInformation.NumberOfLinks as u64; - st.st_blocks = (file_info.StandardInformation.AllocationSize as u64) >> 9; - st.st_blksize = 4096; - Ok(st) - } - pub fn lstat(path: &ZStr) -> Maybe { - sys_uv::lstat(path) - } - pub fn mkdir(path: &ZStr, mode: Mode) -> Maybe<()> { - sys_uv::mkdir(path, mode) - } - pub fn unlink(path: &ZStr) -> Maybe<()> { - sys_uv::unlink(path) - } - pub fn rename(from: &ZStr, to: &ZStr) -> Maybe<()> { - sys_uv::rename(from, to) - } - pub fn symlink(target: &ZStr, link: &ZStr) -> Maybe<()> { - sys_uv::symlink_uv(target, link, 0) - } - pub fn readlink(path: &ZStr, buf: &mut [u8]) -> Maybe { - sys_uv::readlink(path, buf).map(|s| s.len()) - } - pub fn fchmod(fd: Fd, mode: Mode) -> Maybe<()> { - sys_uv::fchmod(fd, mode) - } - pub fn ftruncate(fd: Fd, len: i64) -> Maybe<()> { - // Calls `NtSetInformationFile(.., - // FileEndOfFileInformation)` directly on the HANDLE (NOT via libuv — - // sys_uv::ftruncate requires a CRT fd via `fd.uv()`, which fails for - // HANDLE-backed `Fd`s that have no uv mapping). - let mut io: w::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); - let mut eof = bun_windows_sys::FILE_END_OF_FILE_INFORMATION { EndOfFile: len }; - // SAFETY: FFI; fd is a valid HANDLE, eof/io valid for the call. - let rc = unsafe { - w::ntdll::NtSetInformationFile( - fd.native(), - &mut io, - core::ptr::from_mut(&mut eof).cast::(), - core::mem::size_of::() as u32, - w::FILE_INFORMATION_CLASS::FileEndOfFileInformation, - ) - }; - if rc != bun_windows_sys::NTSTATUS::SUCCESS { - return Err(Error::new(rc, Tag::ftruncate).with_fd(fd)); - } - Ok(()) - } - - // ── kernel32 / ntdll arms ──────────────────────────────────────────── pub fn openat(dir: impl AsFd, path: &ZStr, flags: i32, mode: Mode) -> Maybe { let dir = dir.as_fd(); - // Route through the NtCreateFile path - // (normalize → `open_file_at_windows_nt_path`) so the result is a - // HANDLE-backed `Fd` and `O::DIRECTORY`/`O::NOFOLLOW`/`O::PATH` are - // honoured. Do NOT fall back to libuv `open()` here — that returns a - // CRT-fd-backed `Fd` and ignores the directory/nofollow flags. + // The NtCreateFile path (normalize → `open_file_at_windows_nt_path`) + // honours `O::DIRECTORY`/`O::NOFOLLOW`/`O::PATH`, which `open()` ignores. super::openat_windows_a(dir, path.as_bytes(), flags, mode) } /// `DuplicateHandle` with `bInheritHandle = FALSE`, the equivalent of the - /// POSIX arm's `F_DUPFD_CLOEXEC`: libuv spawns children with + /// POSIX arm's `F_DUPFD_CLOEXEC`: children are spawned with /// `bInheritHandles = TRUE`, so an inheritable duplicate would leak into - /// every process spawned while it is open. Stdio handed to a child is - /// duplicated again (inheritable) by libuv itself, so nothing needs it. + /// every process spawned while it is open. pub fn dup(fd: Fd) -> Maybe { // DuplicateHandle on the underlying HANDLE. let process = w::kernel32::GetCurrentProcess(); let mut target: w::HANDLE = core::ptr::null_mut(); + // SAFETY: FFI; `target` is a live local. let out = unsafe { w::kernel32::DuplicateHandle( process, @@ -3889,6 +3568,7 @@ mod windows_impl { pub fn getcwd(buf: &mut [u8]) -> Maybe { // GetCurrentDirectoryW + WTF16→UTF8. let mut wbuf = bun_paths::w_path_buffer_pool::get(); + // SAFETY: FFI; `wbuf` is valid for `wbuf.len()` units. let len = unsafe { w::kernel32::GetCurrentDirectoryW(wbuf.len() as u32, wbuf.as_mut_ptr()) }; if len == 0 { @@ -3904,12 +3584,32 @@ mod windows_impl { let utf8 = bun_paths::string_paths::from_w_path(buf, &wbuf[..len as usize]); Ok(utf8.len()) } + /// `path` as the name a native call relative to `dir` takes. To + /// `NtCreateFile` `.` and `..` are names like any other, so a path that + /// has one is resolved first, the way [`openat`] resolves every path. + pub(super) fn nt_path_at<'a>( + dir: Fd, + path: &ZStr, + buf: &'a mut [u16], + ) -> Maybe<&'a bun_core::WStr> { + let path = path.as_bytes(); + // To `NtCreateFile` no name at all is `dir` itself. + if path.is_empty() { + return Err(Error::from_code(E::ENOENT, Tag::open)); + } + if !bun_core::strings::split_any(path, b"/\\").any(|name| name == b"." || name == b"..") { + return Ok(bun_paths::string_paths::to_nt_path(buf, path)); + } + let mut wide = bun_paths::w_path_buffer_pool::get(); + let wide = super::convert_path_u8_to_u16(&mut wide.0[..], path)?; + super::normalize_path_windows(dir, wide, buf) + } pub fn mkdirat(dir: impl AsFd, path: &ZStr, _mode: Mode) -> Maybe<()> { let dir = dir.as_fd(); // Open with `op = OnlyCreate`, then close the resulting handle on // success. let mut wbuf = bun_paths::w_path_buffer_pool::get(); - let wpath = bun_paths::string_paths::to_nt_path(&mut wbuf, path.as_bytes()); + let wpath = nt_path_at(dir, path, &mut wbuf.0[..])?; let made = super::open_dir_at_windows_nt_path( dir, wpath, @@ -3928,8 +3628,8 @@ mod windows_impl { let to_dir = to_dir.as_fd(); let mut wf = bun_paths::w_path_buffer_pool::get(); let mut wt = bun_paths::w_path_buffer_pool::get(); - let from_w = bun_paths::string_paths::to_nt_path(&mut wf, from.as_bytes()); - let to_w = bun_paths::string_paths::to_nt_path(&mut wt, to.as_bytes()); + let from_w = nt_path_at(from_dir, from, &mut wf.0[..])?; + let to_w = nt_path_at(to_dir, to, &mut wt.0[..])?; super::windows::rename_at_w(from_dir, from_w, to_dir, to_w, true) } pub(crate) fn renameat2( @@ -3945,12 +3645,8 @@ mod windows_impl { renameat(from_dir, from, to_dir, to) } pub fn unlinkat_with_flags(dir: Fd, path: &ZStr, flags: i32) -> Maybe<()> { - // Convert to NT path and call `DeleteFileBun`; - // `remove_dir = flags & AT_REMOVEDIR != 0`. - // AT_REMOVEDIR on Windows = 0x200. - const AT_REMOVEDIR: i32 = 0x200; let mut wbuf = bun_paths::w_path_buffer_pool::get(); - let wpath = bun_paths::string_paths::to_nt_path(&mut wbuf, path.as_bytes()); + let wpath = nt_path_at(dir, path, &mut wbuf.0[..])?; super::windows::DeleteFileBun( wpath, super::windows::DeleteFileOptions { @@ -4095,7 +3791,6 @@ mod windows_impl { } pub fn symlinkat(target: &ZStr, dirfd: impl AsFd, dest: &ZStr) -> Maybe<()> { let dirfd = dirfd.as_fd(); - // Resolve `dest` against `dirfd`, then symlink via libuv. let mut db = bun_paths::path_buffer_pool::get(); let d = super::get_fd_path(dirfd, &mut db)?; let mut dj = bun_paths::path_buffer_pool::get(); @@ -4103,7 +3798,7 @@ mod windows_impl { &mut dj.0, &[d, dest.as_bytes()], ); - sys_uv::symlink_uv(target, d_abs, 0) + symlink(target, d_abs) } pub fn readlinkat(fd: impl AsFd, path: &ZStr, buf: &mut [u8]) -> Maybe { let fd = fd.as_fd(); @@ -4145,8 +3840,11 @@ mod windows_impl { ) { return Err(Error::new(E::ENAMETOOLONG, Tag::access).with_path(path.as_bytes())); } - let mut wbuf = bun_paths::w_path_buffer_pool::get(); - let wpath = bun_paths::string_paths::to_kernel32_path(&mut wbuf, path.as_bytes()); + let wpath = match w::fs::WPath::new(path.as_bytes()) { + Ok(wpath) => wpath, + Err(e) => return Err(Error::from_win32(e, Tag::access).with_path(path.as_bytes())), + }; + // SAFETY: a `WPath` is NUL-terminated. let attrs = unsafe { w::kernel32::GetFileAttributesW(wpath.as_ptr()) }; if attrs == w::INVALID_FILE_ATTRIBUTES { return Err( @@ -4172,30 +3870,6 @@ mod windows_impl { Err(_) => Ok(false), } } - pub fn utimens(path: &ZStr, atime: TimeLike, mtime: TimeLike) -> Maybe<()> { - let a = atime.sec as f64 + atime.nsec as f64 / 1e9; - let m = mtime.sec as f64 + mtime.nsec as f64 / 1e9; - let mut req = uv::fs_t::uninitialized(); - let rc = unsafe { - uv::uv_fs_utime( - core::ptr::null_mut(), - &mut req, - path.as_ptr().cast::<_>(), - a, - m, - None, - ) - }; - // uv_fs_utime runs fs__capture_path which - // uv__malloc's the WTF-16 path into the req even for sync (cb=NULL) - // calls; only uv_fs_req_cleanup frees it. fs_t has no Drop impl, so - // call it explicitly before any return. - req.deinit(); - if let Some(err) = rc.to_error(Tag::utime) { - return Err(err.with_path(path.as_bytes())); - } - Ok(()) - } pub fn exists_z(path: &ZStr) -> bool { // GetFileAttributesW != INVALID. access(path, 0).is_ok() @@ -4226,6 +3900,7 @@ mod windows_impl { pub fn get_file_size(fd: Fd) -> Maybe { // GetFileSizeEx. let mut size: i64 = 0; + // SAFETY: FFI; `size` is a live local. let ok = unsafe { w::kernel32::GetFileSizeEx(fd.native() as w::HANDLE, &mut size) }; if ok == 0 { return Err(Error::from_win32(w::Win32Error::get(), Tag::fstat).with_fd(fd)); @@ -4234,37 +3909,10 @@ mod windows_impl { // negative LARGE_INTEGER never becomes ~18 EB after the i64→u64 cast. Ok(size.max(0) as u64) } - pub fn realpath<'a>(path: &ZStr, buf: &'a mut bun_core::PathBuffer) -> Maybe<&'a [u8]> { - // sys_uv.rs:216 — open + GetFinalPathNameByHandle (uv_fs_realpath edge cases). - let fd = open(path, O::RDONLY, 0)?; - let r = super::get_fd_path(fd, buf); - let _ = close(fd); - // get_fd_path yields `&mut [u8]`; coerce to shared. - r.map(|s| &*s) - } - pub fn pipe() -> Maybe<[Fd; 2]> { - // uv_pipe(fds, 0, 0). - let mut fds: [uv::uv_file; 2] = [-1, -1]; - let rc = unsafe { uv::uv_pipe(&mut fds, 0, 0) }; - if let Some(err) = rc.to_error(Tag::pipe) { - return Err(err); - } - Ok([Fd::from_uv(fds[0]), Fd::from_uv(fds[1])]) - } - pub fn isatty(fd: Fd) -> bool { - // `uv_guess_handle` takes a `uv_file` (CRT int fd); `fd.uv()` PANICS - // for HANDLE-backed (`FdKind::System`) Fds that are not stdio. Branch - // on the fd kind: uv-backed → libuv probe, HANDLE-backed → - // `GetFileType == FILE_TYPE_CHAR` (the canonical Win32 tty test, what - // `_isatty()` ultimately calls). - match fd.kind() { - FdKind::Uv => uv::uv_guess_handle(fd.uv()) == uv::UV_TTY, - FdKind::System => w::GetFileType(fd.native()) == w::FILE_TYPE_CHAR, - } - } pub fn lseek(fd: Fd, offset: i64, whence: i32) -> Maybe { // SetFilePointerEx. let mut new: i64 = 0; + // SAFETY: FFI; `new` is a live local. let ok = unsafe { w::SetFilePointerEx(fd.native() as w::HANDLE, offset, &mut new, whence as u32) }; @@ -4290,12 +3938,8 @@ mod windows_impl { // as the drive root, not the drive's saved cwd. let mut wbuf = bun_paths::w_path_buffer_pool::get(); let wpath = bun_paths::string_paths::to_w_dir_path(&mut wbuf, path.as_bytes()); - if unsafe { w::SetCurrentDirectoryW(wpath.as_ptr()) } == 0 { - return Err( - Error::from_win32(w::Win32Error::get(), Tag::chdir).with_path(path.as_bytes()) - ); - } - Ok(()) + crate::windows::fs::set_current_directory(wpath) + .map_err(|e| Error::from_win32(e, Tag::chdir).with_path(path.as_bytes())) } pub fn fchdir(fd: Fd) -> Maybe<()> { let mut buf = bun_paths::path_buffer_pool::get(); @@ -4306,7 +3950,7 @@ mod windows_impl { // SAFETY: NUL-terminated above. chdir(ZStr::from_buf(&zb.0[..], p.len())) } - pub fn umask(mode: Mode) -> Mode { + pub(crate) fn libc_umask(mode: Mode) -> Mode { unsafe extern "C" { safe fn _umask(m: core::ffi::c_int) -> core::ffi::c_int; } @@ -4318,6 +3962,7 @@ mod windows_impl { // before the `usize → i32` cast — otherwise ≥2 GiB buffers wrap to a // negative length and Winsock fails with WSAEFAULT. let len = buf.len().min(i32::MAX as usize) as i32; + // SAFETY: FFI; `buf` is writable for `len` bytes. let rc = unsafe { w::ws2_32::recv(fd.native() as _, buf.as_mut_ptr().cast::<_>(), len, flags) }; if rc < 0 { @@ -4329,6 +3974,7 @@ mod windows_impl { // Winsock `send`. Clamp to `i32::MAX` so the // `usize → i32` cast can't wrap to a negative length on huge buffers. let len = buf.len().min(i32::MAX as usize) as i32; + // SAFETY: FFI; `buf` is readable for `len` bytes. let rc = unsafe { w::ws2_32::send(fd.native() as _, buf.as_ptr().cast::<_>(), len, flags) }; if rc < 0 { return Err(Error::from_win32(w::Win32Error::get(), Tag::send).with_fd(fd)); @@ -4393,28 +4039,23 @@ fn read_fill_vec( // ────────────────────────────────────────────────────────────────────────── // `bun.PlatformIOVecConst` / `bun.platformIOVecConstCreate` — POSIX // `iovec_const` (= `struct iovec` with the writev contract that `base` is -// not written through). On Windows this aliases `uv_buf_t`; -// that arm lives in `windows_impl` below. -// Layout matches `libc::iovec` (`{ *void, usize }`) so a `&[PlatformIoVecConst]` -// can be passed straight to `pwritev(2)`. +// not written through). The layout is that of `PlatformIoVec` +// (`{ *void, usize }`): on POSIX a `&[PlatformIoVecConst]` can be passed +// straight to `pwritev(2)`. // ────────────────────────────────────────────────────────────────────────── -#[cfg(unix)] #[repr(C)] #[derive(Clone, Copy)] pub struct PlatformIoVecConst { pub(crate) base: *const u8, pub len: usize, } -// SAFETY: `{ *const u8, usize }` — `(null, 0)` is a valid empty iovec (S021). -#[cfg(unix)] +// SAFETY: `{ *const u8, usize }` — `(null, 0)` is a valid empty iovec. unsafe impl bun_core::ffi::Zeroable for PlatformIoVecConst {} -#[cfg(unix)] const _: () = assert!( - core::mem::size_of::() == core::mem::size_of::() - && core::mem::align_of::() == core::mem::align_of::() + core::mem::size_of::() == core::mem::size_of::() + && core::mem::align_of::() == core::mem::align_of::() ); -#[cfg(unix)] #[inline] pub fn platform_iovec_const_create(buf: &[u8]) -> PlatformIoVecConst { PlatformIoVecConst { @@ -4483,84 +4124,41 @@ pub fn pwritev(fd: Fd, vecs: &[PlatformIoVecConst], offset: i64) -> Maybe } #[cfg(windows)] { - // `PlatformIoVecConst` is layout-identical to `uv_buf_t` on Windows - // (asserted below), so the slice forwards as-is. - sys_uv::pwritev(fd, vecs, offset) + windows::fs::pwritev(fd, vecs, offset) } } // ────────────────────────────────────────────────────────────────────────── -// `bun.PlatformIOVec` — mutable iovec (`{ *void, usize }` on POSIX, -// `uv_buf_t` on Windows). Layout-compatible with `libc::iovec` so a -// `&[PlatformIoVec]` can be passed straight to `readv(2)`/`writev(2)`. +// `bun.PlatformIOVec` — mutable iovec. On POSIX it is `libc::iovec`, so a +// `&[PlatformIoVec]` can be passed straight to `readv(2)`/`writev(2)`. On +// Windows it is a struct of the same shape. // ────────────────────────────────────────────────────────────────────────── #[cfg(unix)] pub type PlatformIoVec = libc::iovec; #[cfg(windows)] -pub type PlatformIoVec = bun_libuv_sys::uv_buf_t; -// Both casings of `PlatformIOVec` / `PlatformIOVecConst` are provided so -// existing call sites (`sys_uv.rs`) compile without churn. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct PlatformIoVec { + pub iov_base: *mut core::ffi::c_void, + pub iov_len: usize, +} +// SAFETY: `{ *mut c_void, usize }` — `(null, 0)` is a valid empty buffer. +#[cfg(windows)] +unsafe impl bun_core::ffi::Zeroable for PlatformIoVec {} pub use PlatformIoVec as PlatformIOVec; pub use PlatformIoVecConst as PlatformIOVecConst; #[inline] pub fn platform_iovec_create(buf: &mut [u8]) -> PlatformIoVec { - #[cfg(unix)] - { - PlatformIoVec { - iov_base: buf.as_mut_ptr().cast(), - iov_len: buf.len(), - } - } - #[cfg(windows)] - { - // `uv_buf_t` on Windows is `{ ULONG len; char* base; }` — order-swapped vs - // POSIX iovec. - PlatformIoVec { - len: buf.len() as bun_libuv_sys::ULONG, - base: buf.as_mut_ptr(), - } + PlatformIoVec { + iov_base: buf.as_mut_ptr().cast(), + iov_len: buf.len(), } } #[inline] pub const fn platform_iovec_len(iov: &PlatformIoVec) -> usize { - #[cfg(unix)] - { - iov.iov_len - } - #[cfg(windows)] - { - iov.len as usize - } -} - -/// Windows `PlatformIOVecConst` — same `uv_buf_t` layout (libuv has no -/// const-buf type), with `base` typed `*const u8` so callers can build it -/// from `&[u8]` without casts. -#[cfg(windows)] -#[repr(C)] -#[derive(Clone, Copy)] -pub struct PlatformIoVecConst { - pub len: bun_libuv_sys::ULONG, - pub(crate) base: *const u8, -} -// SAFETY: `{ ULONG, *const u8 }` — `(0, null)` is a valid empty `uv_buf_t` (S021). -#[cfg(windows)] -unsafe impl bun_core::ffi::Zeroable for PlatformIoVecConst {} -#[cfg(windows)] -const _: () = assert!( - core::mem::size_of::() == core::mem::size_of::() - && core::mem::align_of::() - == core::mem::align_of::() -); -#[cfg(windows)] -#[inline] -pub fn platform_iovec_const_create(buf: &[u8]) -> PlatformIoVecConst { - PlatformIoVecConst { - len: buf.len() as bun_libuv_sys::ULONG, - base: buf.as_ptr(), - } + iov.iov_len } /// `bun.sys.writev` — gather-write. Retries on EINTR @@ -4604,6 +4202,14 @@ pub fn writev(fd: Fd, vecs: &[PlatformIoVec]) -> Maybe { } } +#[cfg(windows)] +pub fn writev(fd: Fd, vecs: &[PlatformIoVec]) -> Maybe { + // SAFETY: `PlatformIoVec` and `PlatformIoVecConst` are layout-identical + // (asserted above); the cast keeps the slice's (ptr, len). + let vecs = unsafe { &*(core::ptr::from_ref(vecs) as *const [PlatformIoVecConst]) }; + windows::fs::pwritev(fd, vecs, -1) +} + /// `bun.sys.readv` — scatter-read. Retries on EINTR /// (macOS uses `readv$NOCANCEL`). #[cfg(unix)] @@ -4649,6 +4255,11 @@ pub fn readv(fd: Fd, vecs: &[PlatformIoVec]) -> Maybe { } } +#[cfg(windows)] +pub fn readv(fd: Fd, vecs: &[PlatformIoVec]) -> Maybe { + windows::fs::preadv(fd, vecs, -1) +} + /// `bun.sys.preadv` — scatter-read at `position`. Retries on EINTR /// (macOS uses `preadv$NOCANCEL`). #[cfg(unix)] @@ -4705,16 +4316,20 @@ pub fn preadv(fd: Fd, vecs: &[PlatformIoVec], position: i64) -> Maybe { } } +#[cfg(windows)] +pub fn preadv(fd: Fd, vecs: &[PlatformIoVec], position: i64) -> Maybe { + windows::fs::preadv(fd, vecs, position) +} + // ────────────────────────────────────────────────────────────────────────── // `bun.StatFS` / `bun.sys.statfs`. // On POSIX `bun.StatFS` aliases `struct statfs` (Linux/macOS/FreeBSD); on -// Windows it is `uv_statfs_t` populated from `GetDiskFreeSpace` (handled in -// `sys_uv`). +// Windows it is populated from `FileFsFullSizeInformation`. // ────────────────────────────────────────────────────────────────────────── #[cfg(unix)] pub type StatFS = libc::statfs; -#[cfg(not(unix))] -pub type StatFS = self::windows::libuv::uv_statfs_t; +#[cfg(windows)] +pub use windows::fs::StatFS; /// `bun.sys.statfs` — query filesystem stats for `path`. Retries on EINTR. /// @@ -4753,6 +4368,11 @@ pub fn statfs(path: &ZStr) -> Maybe { } } +#[cfg(windows)] +pub fn statfs(path: &ZStr) -> Maybe { + windows::fs::statfs(path) +} + /// `bun.timespec` — re-exported from `bun_core` so `PosixStat.rs` can spell /// `crate::Timespec` (matching the `bun.timespec` namespacing). pub use bun_core::Timespec; @@ -4806,8 +4426,7 @@ impl std::io::Write for FileWriter { // ────────────────────────────────────────────────────────────────────────── // Additional surface unblocked for dependents. -// Symbols are real posix wrappers; Windows arms route -// through the libuv/kernel32 layer in `windows_impl` above. +// Symbols are real posix wrappers; Windows arms live in `windows_impl` above. // ────────────────────────────────────────────────────────────────────────── /// `bun.sys.Error.Int` — backing integer for `errno`. @@ -5980,13 +5599,12 @@ pub fn dlopen(filename: &ZStr, flags: i32) -> Option<*mut c_void> { // the `W` entry point like every other Windows path in this crate. let mut wbuf = bun_paths::w_path_buffer_pool::get(); let wpath = bun_paths::string_paths::to_w_path(&mut wbuf, filename.as_bytes()); - // Match libuv `uv_dlopen` (and Bun's own `process.dlopen`): request - // altered search so dependent DLLs resolve next to the loaded module. + // Like Node's (and Bun's own) `process.dlopen`: request altered + // search so dependent DLLs resolve next to the loaded module. // MSDN documents that flag as undefined for relative paths, so only // set it when absolute; bare names keep the standard search order. - const LOAD_WITH_ALTERED_SEARCH_PATH: u32 = 0x0000_0008; let dw_flags = if bun_paths::is_absolute_windows(filename.as_bytes()) { - LOAD_WITH_ALTERED_SEARCH_PATH + bun_windows_sys::LOAD_WITH_ALTERED_SEARCH_PATH } else { 0 }; @@ -6161,9 +5779,7 @@ pub fn open_dir_no_renaming_or_deleting_windows(dir: Fd, path: &[u8]) -> Maybe Option<(usize, bool)> { /// `normalizePathWindows` — convert a (possibly relative) path into an NT /// object name for `NtCreateFile` against `dir_fd`: absolute inputs become -/// `\??\C:\…`, dirfd-relative inputs resolve to absolute `\Device\…` names. +/// `\??\C:\…`, a single name (other than `.` and `..`) stays as it is for +/// `RootDirectory = dir_fd`, other dirfd-relative inputs resolve to absolute +/// `\Device\…` names. #[cfg(windows)] pub fn normalize_path_windows<'a>( dir_fd: Fd, @@ -6293,6 +5909,11 @@ pub fn normalize_path_windows<'a>( /// output Win32-consumable: absolute inputs lose `\??\`, dotted or /// multi-component relatives become `\\?\GLOBALROOT\Device\…`, and bare names /// pass through verbatim (Win32 resolves those against the cwd). +/// +/// Names reach `NtCreateFile` as written: `aux.js`, `nul`, `name.` and `name ` +/// are files, as they are for Node, which prefixes every path with `\\?\`. +/// None of Win32's DOS-device or trailing dot/space rules are applied, apart +/// from the absolute `…\nul` below. #[cfg(windows)] pub fn normalize_path_windows_opts<'a>( dir_fd: Fd, @@ -6302,6 +5923,10 @@ pub fn normalize_path_windows_opts<'a>( ) -> Maybe<&'a bun_core::WStr> { use bun_core::WStr; let too_long = || Error::from_code(E::ENAMETOOLONG, Tag::open); + // To `NtCreateFile` no name at all is `dir_fd` itself. + if path.is_empty() { + return Err(Error::from_code(E::ENOENT, Tag::open)); + } let mut path = path; if bun_paths::is_absolute_windows_wtf16(path) { @@ -6331,15 +5956,24 @@ pub fn normalize_path_windows_opts<'a>( } use bun_paths::is_sep_any_t as is_sep; if is_sep(path[1]) && is_sep(path[3]) { - // (b) `\\.\…` device path → preserve verbatim so `\\.\pipe\foo` - // is not collapsed to `\pipe\foo` by the normalizer. - if path[2] == b'.' as u16 { + let names_a_drive = path.len() >= 6 + && bun_paths::resolve_path::is_drive_letter_t::(path[4]) + && path[5] == b':' as u16; + // (b) `\\.\…` device path → the rest verbatim so `\\.\pipe\foo` + // is not collapsed to `\pipe\foo` by the normalizer. For + // `NtCreateFile` the prefix is spelled `\??\`. + if path[2] == b'.' as u16 && !names_a_drive { if path.len() >= buf.len() { return Err(too_long()); } + let (second, third) = if opts.add_nt_prefix { + (b'?', b'?') + } else { + (b'\\', b'.') + }; buf[0] = b'\\' as u16; - buf[1] = b'\\' as u16; - buf[2] = b'.' as u16; + buf[1] = second as u16; + buf[2] = third as u16; buf[3] = b'\\' as u16; let rest = &path[4..]; buf[4..4 + rest.len()].copy_from_slice(rest); @@ -6347,8 +5981,9 @@ pub fn normalize_path_windows_opts<'a>( return Ok(WStr::from_buf(&buf[..], path.len())); } // (c) `\??\…` / `\\?\…` already prefixed → strip the 4-u16 - // prefix before re-normalizing to avoid a double `\??\`. - if path[2] == b'?' as u16 { + // prefix before re-normalizing to avoid a double `\??\`. So is + // `\\.\C:\…`, which Win32 resolves like any other path. + if path[2] == b'?' as u16 || path[2] == b'.' as u16 { path = &path[4..]; } } @@ -6398,8 +6033,7 @@ pub fn normalize_path_windows_opts<'a>( return Ok(unsafe { WStr::from_raw(norm.as_ptr(), len) }); } - // Strip a leading drive letter (`C:`) on the relative part; the bypass - // below still copies the original `path` verbatim. + // A drive-relative path (`C:name`) is taken as relative to `dir_fd`. let rel = if path.len() >= 2 && bun_paths::resolve_path::is_drive_letter_t::(path[0]) && path[1] == b':' as u16 @@ -6409,14 +6043,18 @@ pub fn normalize_path_windows_opts<'a>( path }; - // Routing = any separator or `.`; clamp relevance = `..` resolving above - // the dirfd. One pass via the shared classifier. + // Clamp relevance = `..` resolving above the dirfd. One pass via the + // shared classifier. let facts = bun_paths::classify_rel_t(rel, bun_paths::PathFormat::Windows); - let saw_sep_or_dot = facts.has_sep || facts.has_dot; - - // Relative path with no separators or `.` can be passed straight through - // to `NtCreateFile` against `RootDirectory`. - if !saw_sep_or_dot { + const DOT: u16 = b'.' as u16; + let is_single_name = !facts.has_sep && !matches!(rel, [DOT] | [DOT, DOT]); + + // A single name other than `.` and `..` can be passed straight through to + // `NtCreateFile` against `RootDirectory`. Win32 output does that for + // dotless names only (see `NormalizePathWindowsOpts`). Not with a drive + // letter in front: to the kernel `C:name` is the stream `name` of a file `C`. + let has_drive = rel.len() != path.len(); + if is_single_name && !has_drive && (opts.add_nt_prefix || !facts.has_dot) { if path.len() >= buf.len() { return Err(too_long()); } @@ -6455,7 +6093,7 @@ pub fn normalize_path_windows_opts<'a>( Err(windows::GetFinalPathNameByHandleError::NameTooLong) => return Err(too_long()), // `E.BADFD` (errno 77 'file descriptor in bad state'), // not `EBADF` (9). - Err(_) => return Err(Error::from_code(E::BADFD, Tag::open)), + Err(_) => return Err(Error::from_code(E::EBADFD, Tag::open)), }; // The volume boundary is only needed when `..` resolves above the dirfd: @@ -6478,7 +6116,7 @@ pub fn normalize_path_windows_opts<'a>( ) { Ok(p) => p, Err(windows::GetFinalPathNameByHandleError::NameTooLong) => return Err(too_long()), - Err(_) => return Err(Error::from_code(E::BADFD, Tag::open)), + Err(_) => return Err(Error::from_code(E::EBADFD, Tag::open)), }; // A mis-placed boundary would resolve `..` to the wrong file; fail // loud when the two names don't compose (e.g. a rename race). @@ -6487,7 +6125,7 @@ pub fn normalize_path_windows_opts<'a>( share_rooted = rooted; len } - None => return Err(Error::from_code(E::BADFD, Tag::open)), + None => return Err(Error::from_code(E::EBADFD, Tag::open)), } }; // The `\Device\…` name of a volume-root handle ends in `\`; keep the @@ -6566,34 +6204,6 @@ pub fn normalize_path_windows_opts<'a>( Ok(WStr::from_buf(&buf[..], g + prefix_len + sub_len)) } -/// Open a `\\.\…` device path via kernel32 `CreateFileW` -/// (NtCreateFile cannot open device paths). -#[cfg(windows)] -fn open_windows_device_path( - path: &bun_core::WStr, - desired_access: u32, - creation_disposition: u32, - flags_and_attributes: u32, -) -> Maybe { - use bun_windows_sys::externs as w; - // SAFETY: path is NUL-terminated UTF-16. - let rc = unsafe { - w::CreateFileW( - path.as_ptr(), - desired_access, - FILE_SHARE, - core::ptr::null_mut(), - creation_disposition, - flags_and_attributes, - core::ptr::null_mut(), - ) - }; - if rc == bun_windows_sys::INVALID_HANDLE_VALUE { - return Err(Error::from_win32(windows::Win32Error::get(), Tag::open)); - } - Ok(Fd::from_system(rc)) -} - /// Absolute NT object names our producers emit: `\??\…` or `\Device\…`. Only /// these get `RootDirectory = null`; any other rooted string stays /// dirfd-relative so `NtCreateFile` rejects it (`OBJECT_PATH_SYNTAX_BAD`). @@ -6637,30 +6247,7 @@ pub(crate) fn open_dir_at_windows_nt_path( 0 }; - // NtCreateFile seems to not function on device paths. Since it is - // absolute, it can just use CreateFileW. let p = path.as_slice(); - if p.len() >= 4 - && p[0] == b'\\' as u16 - && p[1] == b'\\' as u16 - && p[2] == b'.' as u16 - && p[3] == b'\\' as u16 - { - return open_windows_device_path( - path, - flags, - if options.op != WindowsOpenDirOp::OnlyOpen { - w::FILE_OPEN_IF - } else { - w::FILE_OPEN - }, - w::FILE_DIRECTORY_FILE - | w::FILE_SYNCHRONOUS_IO_NONALERT - | windows::FILE_OPEN_FOR_BACKUP_INTENT - | open_reparse, - ); - } - let path_len_bytes = (p.len() * 2) as u16; let mut nt_name = w::UNICODE_STRING { Length: path_len_bytes, @@ -6692,7 +6279,7 @@ pub(crate) fn open_dir_at_windows_nt_path( &mut io, core::ptr::null_mut(), 0, - FILE_SHARE, + SHARE_ALL, match options.op { WindowsOpenDirOp::OnlyOpen => w::FILE_OPEN, WindowsOpenDirOp::OnlyCreate => w::FILE_CREATE, @@ -6706,6 +6293,11 @@ pub(crate) fn open_dir_at_windows_nt_path( 0, ) }; + // `ERROR_DIRECTORY`, which this status becomes below, is ENOENT in the + // Win32 table: that is what `CreateProcessW` reports for a missing cwd. + if rc == w::NTSTATUS::NOT_A_DIRECTORY { + return Err(Error::from_code(E::ENOTDIR, Tag::open)); + } // Not `Error::new(rc)`: the curated NTSTATUS table maps // `OBJECT_NAME_INVALID` to EINVAL, and `open` of a bad name is ENOENT // (libuv/Node), which `RtlNtStatusToDosError` gives. @@ -6766,7 +6358,7 @@ pub(crate) fn open_file_at_windows_nt_path( &mut io, core::ptr::null_mut(), attributes, - options.sharing_mode, + SHARE_ALL, options.disposition, options.options, core::ptr::null_mut(), @@ -6776,7 +6368,9 @@ pub(crate) fn open_file_at_windows_nt_path( if rc == w::NTSTATUS::ACCESS_DENIED && attributes == w::FILE_ATTRIBUTE_NORMAL - && (options.access_mask & (w::GENERIC_READ | w::GENERIC_WRITE)) == w::GENERIC_WRITE + && (options.access_mask & (w::GENERIC_WRITE | w::FILE_WRITE_DATA | w::FILE_APPEND_DATA)) + != 0 + && (options.access_mask & (w::GENERIC_READ | w::FILE_READ_DATA)) == 0 { // > If CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, // > CreateFile fails and sets the last error to ERROR_ACCESS_DENIED @@ -6793,25 +6387,18 @@ pub(crate) fn open_file_at_windows_nt_path( continue; } + // `RtlNtStatusToDosError` turns this one into ERROR_ACCESS_DENIED. + if rc == w::NTSTATUS::FILE_IS_A_DIRECTORY { + // To `O_EXCL` a directory is a name that exists, like any other. + let errno = if options.disposition == w::FILE_CREATE { + E::EEXIST + } else { + E::EISDIR + }; + return Err(Error::from_code(errno, Tag::open)); + } return match windows::Win32Error::from_nt_status(rc) { - windows::Win32Error::SUCCESS => { - if (options.access_mask & w::FILE_APPEND_DATA) != 0 { - // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfilepointerex - // SAFETY: FFI; result is a valid handle. - if unsafe { w::SetFilePointerEx(result, 0, core::ptr::null_mut(), w::FILE_END) } - == 0 - { - // NtCreateFile succeeded — close the live HANDLE before - // bailing so this error path doesn't leak it. - // SAFETY: FFI; `result` is the just-created handle. - unsafe { - w::CloseHandle(result); - } - return Err(Error::from_code(E::EUNKNOWN, Tag::SetFilePointerEx)); - } - } - Ok(Fd::from_system(result)) - } + windows::Win32Error::SUCCESS => Ok(Fd::from_system(result)), // See `open_dir_at_windows_nt_path`: Rtl mapping, not the curated table. code => Err(Error::from_win32(code, Tag::open)), }; @@ -6831,7 +6418,9 @@ fn convert_path_u8_to_u16<'a>(buf: &'a mut [u16], path: &[u8]) -> Maybe<&'a mut { return Err(Error::from_code(E::ENAMETOOLONG, Tag::open)); } - Ok(bun_core::convert_utf8_to_utf16_in_buffer(buf, path)) + // Bytes that are no name: `ERROR_INVALID_NAME`, which is `ENOENT`. + bun_core::strings::try_convert_wtf8_to_utf16_in_buffer(buf, path) + .ok_or_else(|| Error::from_code(E::ENOENT, Tag::open)) } #[cfg(windows)] @@ -6866,8 +6455,7 @@ pub fn open_file_at_windows(dir_fd: Fd, path: &[u16], opts: NtCreateFileOptions) open_file_at_windows_nt_path(dir_fd, norm, opts) } -/// POSIX-flag → NtCreateFile -/// translation. +/// `open(2)` over `NtCreateFile`. `flags` are `bun_sys::O` values. #[cfg(windows)] fn openat_windows_impl(dir: Fd, norm: &bun_core::WStr, flags: i32, perm: Mode) -> Maybe { use bun_windows_sys::externs as w; @@ -6885,53 +6473,47 @@ fn openat_windows_impl(dir: Fd, norm: &bun_core::WStr, flags: i32, perm: Mode) - ); } - let nonblock = (flags & O::NONBLOCK) != 0; - - // Matches libuv fs__open: O_RDONLY asks for read access only. GENERIC_WRITE - // already includes FILE_WRITE_ATTRIBUTES for the write-mode branches; the - // fs.futimes path goes through uv_fs_futime which ReOpenFiles for it. - let mut access_mask: u32 = w::READ_CONTROL | w::SYNCHRONIZE; - if (flags & O::RDWR) != 0 { - access_mask |= w::GENERIC_READ | w::GENERIC_WRITE; - } else if (flags & O::WRONLY) != 0 { - access_mask |= w::GENERIC_WRITE; - } else { - access_mask |= w::GENERIC_READ; - } - // O_APPEND is orthogonal to the access mode, so it cannot be another arm of - // the chain above: `a+` is O_RDWR|O_APPEND and would otherwise never get - // FILE_APPEND_DATA, which is what the post-open seek to FILE_END keys off. - if (flags & O::APPEND) != 0 { - access_mask |= w::GENERIC_WRITE | w::FILE_APPEND_DATA; - } - - // Create disposition is derived from O_CREAT/O_EXCL/O_TRUNC alone; the - // read/write access mode only affects `access_mask` above. - let creat = (flags & O::CREAT) != 0; - let excl = (flags & O::EXCL) != 0; - let truncate = (flags & O::TRUNC) != 0; - let disposition: u32 = match (creat, excl, truncate) { - (true, true, _) => w::FILE_CREATE, - (true, false, true) => w::FILE_OVERWRITE_IF, - (true, false, false) => w::FILE_OPEN_IF, - (false, _, true) => w::FILE_OVERWRITE, - (false, _, false) => w::FILE_OPEN, + use windows::fs::Disposition; + // `CreateOptions` bits of `NtCreateFile` (`wdm.h`). + const FILE_WRITE_THROUGH: u32 = 0x0000_0002; + const FILE_SEQUENTIAL_ONLY: u32 = 0x0000_0004; + const FILE_NO_INTERMEDIATE_BUFFERING: u32 = 0x0000_0008; + + let request = windows::fs::OpenRequest::new(flags, perm) + .map_err(|errno| Error::from_code(errno, Tag::open))?; + // FILE_READ_ATTRIBUTES is what `fstat` needs; `CreateFileW` adds it to + // every open, `NtCreateFile` does not. + let access_mask: u32 = request.access | w::FILE_READ_ATTRIBUTES; + let disposition: u32 = match request.disposition { + Disposition::Open => w::FILE_OPEN, + Disposition::OpenOrCreate => w::FILE_OPEN_IF, + Disposition::CreateNew => w::FILE_CREATE, + Disposition::Truncate => w::FILE_OVERWRITE, + Disposition::CreateOrTruncate => w::FILE_OVERWRITE_IF, }; - let blocking_flag: u32 = if !nonblock { - w::FILE_SYNCHRONOUS_IO_NONALERT - } else { - 0 - }; - let follow = (flags & O::NOFOLLOW) == 0; - let opts: u32 = if follow { - blocking_flag - } else { - blocking_flag | w::FILE_OPEN_REPARSE_POINT - }; + // Always a synchronous handle, whatever `O::NONBLOCK` says: `read`/`write` + // pass no OVERLAPPED, which an asynchronous file handle rejects. + let mut opts: u32 = w::FILE_SYNCHRONOUS_IO_NONALERT; + if request.no_buffering { + opts |= FILE_NO_INTERMEDIATE_BUFFERING; + } + if request.write_through { + opts |= FILE_WRITE_THROUGH; + } + if request.sequential { + opts |= FILE_SEQUENTIAL_ONLY; + } + if (flags & O::NOFOLLOW) != 0 { + opts |= w::FILE_OPEN_REPARSE_POINT; + } + // A directory opens for reading, as with `open(2)`; for writing it is EISDIR. + if (flags & (O::WRONLY | O::RDWR | O::APPEND)) != 0 { + opts |= w::FILE_NON_DIRECTORY_FILE; + } let mut attributes: u32 = w::FILE_ATTRIBUTE_NORMAL; - if (flags & O::CREAT) != 0 && (perm & 0x80) == 0 && perm != 0 { + if request.read_only { attributes |= w::FILE_ATTRIBUTE_READONLY; } @@ -6967,7 +6549,7 @@ pub fn openat_windows_a(dir: impl AsFd, path: &[u8], flags: i32, perm: Mode) -> let wide = convert_path_u8_to_u16(&mut wbuf.0[..], path)?; let mut buf2 = bun_paths::w_path_buffer_pool::get(); let norm = normalize_path_windows(dir, wide, &mut buf2.0[..])?; - openat_windows_impl(dir, norm, flags, perm) + openat_windows_impl(dir, norm, flags, perm).map_err(|e| e.with_path(path)) } // ── existence checks ── @@ -6989,11 +6571,15 @@ pub struct WindowsFileAttributes { /// `INVALID_FILE_ATTRIBUTES`. #[cfg(windows)] pub fn get_file_attributes(path: &ZStr) -> Option { + let wpath = windows::fs::WPath::new(path.as_bytes()).ok()?; + get_file_attributes_w(wpath.as_wstr()) +} + +/// [`get_file_attributes`] of a path that is spelled for Win32 already. +#[cfg(windows)] +pub fn get_file_attributes_w(wpath: &bun_core::WStr) -> Option { use bun_windows_sys::externs as w; - let mut wbuf = bun_paths::w_path_buffer_pool::get(); - let wpath = bun_paths::string_paths::to_kernel32_path(&mut wbuf.0[..], path.as_bytes()); - // Win32 API does file path normalization, so we do not need the valid path assertion here. - // SAFETY: `wpath` is NUL-terminated UTF-16 produced by `to_kernel32_path`. + // SAFETY: a `WStr` is NUL-terminated. let dword = unsafe { w::GetFileAttributesW(wpath.as_ptr()) }; if dword == windows::INVALID_FILE_ATTRIBUTES { return None; @@ -7079,6 +6665,10 @@ pub enum ExistsAtType { #[cfg(windows)] fn exists_at_type_nt(dir: Fd, mut path: &[u16]) -> Maybe { use bun_windows_sys::externs as w; + // To the kernel no name at all is `dir` itself. + if path.is_empty() { + return Err(Error::from_code(E::ENOENT, Tag::access)); + } // Trim leading `.\` — NtQueryAttributesFile expects relative paths // without it. if path.len() > 2 && path[0] == b'.' as u16 && path[1] == b'\\' as u16 { @@ -7140,7 +6730,8 @@ pub fn exists_at_type(dir: Fd, sub: &ZStr) -> Maybe { // `NtQueryAttributesFile` against an OBJECT_ATTRIBUTES // built from the (optionally NT-prefixed) wide path. let mut wbuf = bun_paths::w_path_buffer_pool::get(); - let path = bun_paths::string_paths::to_nt_path(&mut wbuf.0[..], sub.as_bytes()).as_slice(); + let at = if dir.is_valid() { dir } else { Fd::cwd() }; + let path = windows_impl::nt_path_at(at, sub, &mut wbuf.0[..])?.as_slice(); exists_at_type_nt(dir, path) } } @@ -7528,7 +7119,7 @@ pub fn get_fd_path_w(fd: Fd, out: &mut [u16]) -> Maybe<&mut [u16]> { use crate::windows::GetFinalPathNameByHandleError as GE; Error::from_code( match e { - GE::FileNotFound => E::ENOENT, + GE::Failed(_) => E::ENOENT, GE::NameTooLong => E::ENAMETOOLONG, }, Tag::GetFinalPathNameByHandle, @@ -7851,18 +7442,14 @@ pub mod posix { pub const INET6_ADDRSTRLEN: usize = 46; // ── sockaddr family ── + #[cfg(windows)] + pub use bun_windows_sys::ws2_32::{sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage}; #[cfg(unix)] pub use libc::{sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage}; - // Route through `bun_libuv_sys` (not `bun_windows_sys::ws2_32`) so types - // returned by libuv APIs (`uv_interface_address_t`, `uv_udp_*`) are the - // *same* nominal type callers see via `bun_sys::posix::sockaddr_*` — Rust - // doesn't structurally unify two identical-layout `#[repr(C)]` structs. - #[cfg(windows)] - pub use bun_libuv_sys::{sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage}; // ── access(2) mode bits ── - // POSIX-standard values; libuv re-uses the same numbers on Windows - // (`uv/win.h`), so these are target-invariant. + // POSIX-standard values; Node uses the same numbers on Windows, so these + // are target-invariant. pub const F_OK: c_int = 0; pub const R_OK: c_int = 4; pub const W_OK: c_int = 2; @@ -8054,11 +7641,12 @@ pub mod net { } #[cfg(windows)] mod sock { - // Same nominal types as `bun_sys::posix::sockaddr*` (see comment there) + // Same nominal types as `bun_sys::posix::sockaddr*` // so `Address::init_posix` accepts pointers callers cast through that // path. AF_* values come from ws2def.h. - pub(super) use bun_libuv_sys::{sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage}; - pub(super) use bun_windows_sys::ws2_32::{AF_INET, AF_INET6}; + pub(super) use bun_windows_sys::ws2_32::{ + AF_INET, AF_INET6, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, + }; } use sock::*; @@ -8068,8 +7656,8 @@ pub mod net { // against this shape stay target-agnostic. // // Layout-identical to the C-named ground truth re-exported at - // `crate::posix::sockaddr_in[6]` (= `libc` on Unix, `ws2_32` via - // `bun_libuv_sys` on Windows) — asserted below. Kept as a *distinct* + // `crate::posix::sockaddr_in[6]` (= `libc` on Unix, `ws2_32` on + // Windows) — asserted below. Kept as a *distinct* // nominal type because the C structs use `sin_*`/`sin6_*` names AND nest // `in_addr`/`in6_addr`; Rust won't structurally unify. // @@ -8286,9 +7874,8 @@ pub mod net { impl fmt::Display for Address { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if let Some(v4) = self.as_in4() { - // `sin_addr` is `in_addr { s_addr: u32 }` on POSIX/ws2_32 but - // `[u8; 4]` in `bun_libuv_sys::sockaddr_in`; reinterpret as - // raw octets so both shapes resolve. + // `sin_addr` is `in_addr { s_addr: u32 }` in network byte + // order; read it as raw octets. // SAFETY: `sin_addr` is 4 bytes of POD on every target. let octets: [u8; 4] = unsafe { *core::ptr::addr_of!(v4.sin_addr).cast::<[u8; 4]>() }; @@ -8503,6 +8090,12 @@ impl CloseOnDrop { pub fn new(fd: Fd) -> Self { Self(fd) } + + /// The fd, which is the caller's to close from here on. + #[inline] + pub fn into_fd(self) -> Fd { + core::mem::ManuallyDrop::new(self).0 + } } impl Drop for CloseOnDrop { #[inline] @@ -8580,75 +8173,28 @@ impl WindowsSymlinkOptions { // ────────────────────────────────────────────────────────────────────────── #[cfg(windows)] mod win_symlink_impl { - use super::{E, Error, Maybe, Tag, WindowsSymlinkOptions, ZStr, sys_uv, windows}; + use super::{E, Error, Maybe, Tag, WindowsSymlinkOptions, ZStr, windows}; use bun_core::WStr; - use core::sync::atomic::{AtomicU32, Ordering}; - - // `CreateSymbolicLinkW` `dwFlags` bits (winbase.h). Not currently exported - // by `bun_windows_sys`; spell them locally so we don't widen the leaf - // crate's surface for two constants. - const SYMBOLIC_LINK_FLAG_DIRECTORY: u32 = 0x1; - const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: u32 = 0x2; - - /// `WindowsSymlinkOptions.symlink_flags` — process-global, starts - /// with `ALLOW_UNPRIVILEGED_CREATE` and is cleared on `INVALID_PARAMETER` - /// (older Windows). - /// - /// The global only carries `ALLOW_UNPRIVILEGED_CREATE` (cleared on - /// `INVALID_PARAMETER`); `DIRECTORY` is OR'd into a *local* on each call. - /// Stickying `DIRECTORY` into the global would make a later - /// `directory=false` call still pass `SYMBOLIC_LINK_FLAG_DIRECTORY`, so - /// `CreateSymbolicLinkW` would create a broken directory symlink for a - /// file target. - static SYMLINK_FLAGS: AtomicU32 = AtomicU32::new(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); - - impl WindowsSymlinkOptions { - #[inline] - fn flags(self) -> u32 { - let mut f = SYMLINK_FLAGS.load(Ordering::Relaxed); - if self.directory { - f |= SYMBOLIC_LINK_FLAG_DIRECTORY; - } - f - } - #[inline] - fn denied() { - SYMLINK_FLAGS.store(0, Ordering::Relaxed); - } - } /// `symlinkW`. `dest` is the link path, `target` is - /// the path the link points to. Retries once with `flags = 0` if the - /// kernel rejects `ALLOW_UNPRIVILEGED_CREATE` (`INVALID_PARAMETER`). + /// the path the link points to. pub fn symlink_w(dest: &WStr, target: &WStr, options: WindowsSymlinkOptions) -> Maybe<()> { - loop { - let flags = options.flags(); - // SAFETY: both inputs are NUL-terminated wide strings. - let rc = unsafe { windows::CreateSymbolicLinkW(dest.as_ptr(), target.as_ptr(), flags) }; - if rc == 0 { - let win_err = windows::Win32Error::get(); - if win_err == windows::Win32Error::INVALID_PARAMETER - && (flags & SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) != 0 - { - WindowsSymlinkOptions::denied(); - continue; - } + windows::fs::create_symbolic_link(dest.as_ptr(), target.as_ptr(), options.directory) + .map_err(|win_err| { let err = Error::from_win32(win_err, Tag::symlink); // Only ENOENT/EEXIST keep `has_failed_to_create_symlink` // unset; every other failure flips the sticky bit so // `symlinkOrJunction` falls through to junctions next time. - if !matches!(err.get_errno(), E::NOENT | E::EXIST) { + if !matches!(err.get_errno(), E::ENOENT | E::EEXIST) { WindowsSymlinkOptions::set_has_failed_to_create_symlink(true); } - return Err(err); - } - return Ok(()); - } + err + }) } /// `symlinkOrJunction`. Tries `CreateSymbolicLinkW` /// (directory flavour) first; on failure other than ENOENT/EEXIST falls - /// back to a libuv junction. `abs_fallback_junction_target = None` says + /// back to a junction. `abs_fallback_junction_target = None` says /// `target` is already absolute and reusable for the junction. pub fn symlink_or_junction( dest: &ZStr, @@ -8675,17 +8221,13 @@ mod win_symlink_impl { Err(err) => match err.get_errno() { // EEXIST/ENOENT: surface the symlink error; junctions // would hit the same condition. - E::EXIST | E::NOENT => return Err(err), + E::EEXIST | E::ENOENT => return Err(err), // anything else: fall through to junction. _ => {} }, } } - sys_uv::symlink_uv( - abs_fallback_junction_target.unwrap_or(target), - dest, - bun_libuv_sys::UV_FS_SYMLINK_JUNCTION, - ) + windows::fs::junction(abs_fallback_junction_target.unwrap_or(target), dest) } /// `unlinkW` — `DeleteFileW` with errno mapping. @@ -8721,11 +8263,18 @@ pub fn link_w(src: &bun_core::WStr, dest: &bun_core::WStr) -> Maybe<()> { Ok(()) } -/// `rmdir` — `rmdirat(FD.cwd(), to)`. Exposed on all -/// platforms (POSIX `unlinkat(.., AT_REMOVEDIR)`; Windows `DeleteFileBun`). +/// `rmdir(2)`. On Windows a path that is not a directory is `ENOENT`, as in +/// Node. #[inline] pub fn rmdir(to: &ZStr) -> Maybe<()> { - rmdirat(Fd::cwd(), to) + #[cfg(windows)] + { + windows::fs::rmdir(to) + } + #[cfg(not(windows))] + { + rmdirat(Fd::cwd(), to) + } } /// Type-style alias so callers can write `bun_sys::MakePath::make_path::(..)` @@ -9427,7 +8976,7 @@ mod normalize_path_windows_tests { w::CreateFileW( wp.as_ptr(), w::GENERIC_READ, - FILE_SHARE, + SHARE_ALL, core::ptr::null_mut(), w::OPEN_EXISTING, w::FILE_FLAG_BACKUP_SEMANTICS, @@ -9633,7 +9182,7 @@ mod normalize_path_windows_tests { // debug_assertions, these ARE the no-panic proof; the invalid stream // spelling is NtCreateFile's to reject at open time. assert_eq!(normalize(*dir, ":\\x"), format!("{base}\\:\\x")); - assert_eq!(normalize(*dir, ":a.b"), format!("{base}\\:a.b")); + assert_eq!(normalize(*dir, ":a.b"), ":a.b"); assert_eq!(normalize(*dir, ".\\:\\x"), format!("{base}\\:\\x")); assert_eq!(normalize(*dir, "a\\:\\x"), format!("{base}\\a\\:\\x")); // `..` collapse promoting `:` toward the front of the output. @@ -9666,18 +9215,20 @@ mod normalize_path_windows_tests { } #[test] - fn dot_in_name_resolves_under_base() { + fn dot_in_name_passes_through() { let _g = crate::file::tests::FD_TEST_LOCK.lock(); let tree = TempTree::new("nt_norm_dotname"); let dir = scopeguard::guard(open_dir_handle(&tree.0), |fd| { let _ = close(fd); }); - // Dot without separator routes to fd resolution (not the bare - // passthrough) and needs no `..` clamp. - assert_eq!( - normalize(*dir, "a.b"), - format!("{}\\a.b", normalize(*dir, ".")) - ); + // A dot inside a single name is part of the name: `NtCreateFile` + // resolves it against `RootDirectory`. + for name in ["a.b", ".a", "a.", "...", "a..b"] { + assert_eq!(normalize(*dir, name), name); + } + // Win32 output resolves a dotted name against the handle. + let base = normalize_opts(*dir, ".", false); + assert_eq!(normalize_opts(*dir, "a.b", false), format!("{base}\\a.b")); } #[test] @@ -9909,28 +9460,24 @@ mod normalize_path_windows_tests { // query fails deterministically (no closed-handle recycling races). // The compose/None-query failure arms are not constructible from a // real handle; `clamp_prefix_len_pairs` covers them at the unit level. - let nul_path = wide("\\\\.\\NUL\0"); let nul = scopeguard::guard( - open_windows_device_path( - bun_core::WStr::from_buf(&nul_path[..], nul_path.len() - 1), - w::GENERIC_READ, - w::OPEN_EXISTING, - 0, - ) - .expect("open NUL"), + open(bun_core::zstr!("\\\\.\\NUL"), O::RDONLY, 0).expect("open NUL"), |fd| { let _ = close(fd); }, ); - assert_eq!(normalize_err(*nul, ".\\x").get_errno(), E::BADFD); + assert_eq!(normalize_err(*nul, ".\\x").get_errno(), E::EBADFD); } #[test] - fn drive_qualified_bare_name_passes_through() { + fn drive_qualified_bare_name_is_resolved() { let _g = crate::file::tests::FD_TEST_LOCK.lock(); - // The bypass copies the ORIGINAL path (drive prefix included); only - // the post-strip remainder decides the routing. - assert_eq!(normalize(Fd::INVALID, "C:foo"), "C:foo"); + // Passed through, `C:foo` would be the stream `foo` of a file `C`. + let resolved = normalize(Fd::INVALID, "C:foo"); + assert!( + resolved.ends_with("\\foo") && !resolved.contains(':'), + "{resolved}" + ); } #[test] diff --git a/src/sys/libuv_error_map.rs b/src/sys/libuv_error_map.rs index be899d07ed5a..f873e6922788 100644 --- a/src/sys/libuv_error_map.rs +++ b/src/sys/libuv_error_map.rs @@ -16,8 +16,7 @@ pub(crate) static LIBUV_ERROR_MAP: EnumMap = build_li const fn build_libuv_error_map() -> EnumMap { // Indexing relies on `SystemErrno`'s dense `0..N` discriminants matching the `enum_map::Enum` // declaration-order index for every variant referenced below (true on all four targets — see - // `bun_errno::*_errno`). The Windows `UV_*` tail variants are never written here; their slots - // keep the "unknown error" fill. + // `bun_errno::*_errno`). let mut arr: [&str; ::LENGTH] = ["unknown error"; ::LENGTH]; diff --git a/src/sys/sys_uv.rs b/src/sys/sys_uv.rs deleted file mode 100644 index 9cf9c4ef936b..000000000000 --- a/src/sys/sys_uv.rs +++ /dev/null @@ -1,862 +0,0 @@ -//! bun.sys.sys_uv is a polyfill of bun.sys but with libuv. -//! TODO: Probably should merge this into bun.sys itself with isWindows checks -#![cfg(windows)] - -use core::ffi::{c_char, c_int, c_uint}; - -use bstr::BStr; - -use bun_core::ZStr; - -use crate::ReturnCodeExt; -use crate::Tag; -use crate::windows::libuv as uv; -use crate::{E, Fd, FdExt, Mode, PlatformIOVec, PlatformIOVecConst, Stat, StatFS}; - -type Result = crate::Result; - -bun_core::define_scoped_log!(log, crate::fd::SYS); - -pub use crate::Error; - -// libuv dont support openat (https://github.com/libuv/libuv/issues/4167) -pub use crate::access; -pub use crate::get_fd_path; -pub use crate::openat; -// Re-export the bun.sys versions of these (libuv has no -// equivalent or `bun.sys` already routes through Win32 directly). -pub use crate::lseek; -pub use crate::symlink; -pub use crate::unlinkat; -pub use crate::unlinkat_with_flags; - -// Note: `req = undefined; req.deinit()` has a safety-check in a debug build - -/// RAII owner for a synchronous `uv_fs_t` request. -/// -/// `uv_fs_t` becomes **self-referential** after `uv_fs_read`/`uv_fs_write` with -/// `nbufs <= 4`: libuv points `req->fs.info.bufs` at the inline -/// `req->fs.info.bufsml[4]` array (vendor/libuv/src/win/fs.c:3291). If the -/// struct is bitwise-moved before `uv_fs_req_cleanup`, the cleanup check -/// `if (bufs != bufsml) uv__free(bufs);` (fs.c:3237) sees the *old* stack -/// address ≠ the *new* `bufsml` slot and frees a stack pointer — heap UB. -/// -/// `scopeguard::guard(fs_t, |mut r| r.deinit())` triggers exactly that move -/// (its `Drop` `ManuallyDrop::take`s the value into the closure arg), so we -/// instead give the request a real `Drop` impl: Rust calls `Drop::drop` *in -/// place* at the original address, so `bufs == bufsml` still holds and cleanup -/// is sound. Do **not** move an `FsReq` after passing it to libuv. -#[repr(transparent)] -struct FsReq(uv::fs_t); - -impl FsReq { - #[inline] - fn new() -> Self { - Self(uv::fs_t::uninitialized()) - } -} - -impl Drop for FsReq { - #[inline] - fn drop(&mut self) { - self.0.deinit(); - } -} - -impl core::ops::Deref for FsReq { - type Target = uv::fs_t; - #[inline] - fn deref(&self) -> &uv::fs_t { - &self.0 - } -} - -impl core::ops::DerefMut for FsReq { - #[inline] - fn deref_mut(&mut self) -> &mut uv::fs_t { - &mut self.0 - } -} - -pub fn open(file_path: &ZStr, c_flags: i32, perm_: Mode) -> Result { - // libuv heap-allocates the WCHAR path copy - // (`fs__capture_path`) and only `uv_fs_req_cleanup` frees it. `FsReq`'s - // `Drop` runs cleanup in place on every return path (see `FsReq` doc for - // why a by-value scopeguard is unsound here). - let mut req = FsReq::new(); - - let flags = uv::O::from_bun_o(c_flags); - - let mut perm = perm_; - if perm == 0 { - // Set a sensible default, otherwise on windows the file will be unusable - perm = 0o644; - } - - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { - uv::uv_fs_open( - uv::Loop::get(), - &mut *req, - file_path.as_ptr(), - flags, - perm as c_int, - None, - ) - }; - log!( - "uv open({}, {}, {}) = {}", - BStr::new(file_path.as_bytes()), - flags, - perm, - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::open).with_path(file_path.as_bytes())) - } else { - Result::Ok(Fd::from_uv(req.result.to_fd())) - } -} - -pub fn mkdir(file_path: &ZStr, flags: Mode) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { - uv::uv_fs_mkdir( - uv::Loop::get(), - &mut *req, - file_path.as_ptr(), - flags as c_int, - None, - ) - }; - - log!( - "uv mkdir({}, {}) = {}", - BStr::new(file_path.as_bytes()), - flags, - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::mkdir).with_path(file_path.as_bytes())) - } else { - Result::Ok(()) - } -} - -pub fn chmod(file_path: &ZStr, flags: Mode) -> Result<()> { - let mut req = FsReq::new(); - - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { - uv::uv_fs_chmod( - uv::Loop::get(), - &mut *req, - file_path.as_ptr(), - flags as c_int, - None, - ) - }; - - log!( - "uv chmod({}, {}) = {}", - BStr::new(file_path.as_bytes()), - flags, - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::chmod).with_path(file_path.as_bytes())) - } else { - Result::Ok(()) - } -} - -pub fn fchmod(fd: Fd, flags: Mode) -> Result<()> { - let uv_fd = fd.uv(); - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_fchmod(uv::Loop::get(), &mut *req, uv_fd, flags as c_int, None) }; - - log!("uv fchmod({}, {}) = {}", uv_fd, flags, rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::fchmod).with_fd(fd)) - } else { - Result::Ok(()) - } -} - -pub fn statfs(file_path: &ZStr) -> Result { - // `uv_fs_statfs` heap-allocates the - // `uv_statfs_t` result into `req.ptr` (plus the WCHAR path copy); only - // `uv_fs_req_cleanup` frees them. Guard so both success and error paths - // free. - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_statfs(uv::Loop::get(), &mut *req, file_path.as_ptr(), None) }; - - log!( - "uv statfs({}) = {}", - BStr::new(file_path.as_bytes()), - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::statfs).with_path(file_path.as_bytes())) - } else { - // On Windows `StatFS == uv_statfs_t`, so the libuv result *is* the - // public type. - // SAFETY: libuv guarantees `req.ptr` points to a valid `uv_statfs_t` - // on success; the pointer carries no alignment guarantee for `StatFS`, - // so read it by value via `read_unaligned`. The value is copied out - // *before* `FsReq::drop` runs `uv_fs_req_cleanup` and frees the - // backing allocation. - let p = unsafe { req.ptr_as::() }; - Result::Ok(unsafe { core::ptr::read_unaligned(p) }) - } -} - -pub fn chown(file_path: &ZStr, uid: uv::uv_uid_t, gid: uv::uv_uid_t) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { - uv::uv_fs_chown( - uv::Loop::get(), - &mut *req, - file_path.as_ptr(), - uid, - gid, - None, - ) - }; - - log!( - "uv chown({}, {}, {}) = {}", - BStr::new(file_path.as_bytes()), - uid, - gid, - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::chown).with_path(file_path.as_bytes())) - } else { - Result::Ok(()) - } -} - -pub fn lchown(file_path: &ZStr, uid: uv::uv_uid_t, gid: uv::uv_uid_t) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { - uv::uv_fs_lchown( - uv::Loop::get(), - &mut *req, - file_path.as_ptr(), - uid, - gid, - None, - ) - }; - - log!( - "uv lchown({}, {}, {}) = {}", - BStr::new(file_path.as_bytes()), - uid, - gid, - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::lchown).with_path(file_path.as_bytes())) - } else { - Result::Ok(()) - } -} - -pub fn fchown(fd: Fd, uid: uv::uv_uid_t, gid: uv::uv_uid_t) -> Result<()> { - let uv_fd = fd.uv(); - - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_fchown(uv::Loop::get(), &mut *req, uv_fd, uid, gid, None) }; - - log!("uv chown({}, {}, {}) = {}", uv_fd, uid, gid, rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::fchown).with_fd(fd)) - } else { - Result::Ok(()) - } -} - -pub fn rmdir(file_path: &ZStr) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_rmdir(uv::Loop::get(), &mut *req, file_path.as_ptr(), None) }; - - log!( - "uv rmdir({}) = {}", - BStr::new(file_path.as_bytes()), - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::rmdir).with_path(file_path.as_bytes())) - } else { - Result::Ok(()) - } -} - -pub fn unlink(file_path: &ZStr) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_unlink(uv::Loop::get(), &mut *req, file_path.as_ptr(), None) }; - - log!( - "uv unlink({}) = {}", - BStr::new(file_path.as_bytes()), - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::unlink).with_path(file_path.as_bytes())) - } else { - Result::Ok(()) - } -} - -pub(crate) fn readlink<'a>(file_path: &ZStr, buf: &'a mut [u8]) -> Result<&'a mut ZStr> { - // `uv_fs_readlink` heap-allocates the target - // string into `req.ptr` (plus the WCHAR path copy); only `uv_fs_req_cleanup` - // frees them. The guard covers all four return paths below; the bytes are - // copied into `buf` *before* the guard runs. - let mut req = FsReq::new(); - // Edge cases: http://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_realpath - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_readlink(uv::Loop::get(), &mut *req, file_path.as_ptr(), None) }; - - if let Some(errno) = rc.errno() { - log!( - "uv readlink({}) = {}, [err]", - BStr::new(file_path.as_bytes()), - rc.int() - ); - return Result::Err(Error::new(errno, Tag::readlink).with_path(file_path.as_bytes())); - } else { - // Seems like `rc` does not contain the size? - debug_assert!(rc.int() == 0); - let result_ptr: *mut c_char = unsafe { req.ptr_as::() } as *mut c_char; - let Some(result_ptr) = (!result_ptr.is_null()).then_some(result_ptr) else { - return Result::Err( - Error::new(E::NOENT, Tag::readlink).with_path(file_path.as_bytes()), - ); - }; - // SAFETY: libuv guarantees req.ptr is a NUL-terminated string on success. - let slice = unsafe { bun_core::ffi::cstr(result_ptr) }.to_bytes(); - // Reserve one byte for the NUL sentinel below. When slice.len == buf.len - // there is no room for it and buf[slice.len] = 0 would be out of bounds. - if slice.len() >= buf.len() { - log!( - "uv readlink({}) = {}, {} TRUNCATED", - BStr::new(file_path.as_bytes()), - rc.int(), - BStr::new(slice) - ); - return Result::Err( - Error::new(E::NAMETOOLONG, Tag::readlink).with_path(file_path.as_bytes()), - ); - } - log!( - "uv readlink({}) = {}, {}", - BStr::new(file_path.as_bytes()), - rc.int(), - BStr::new(slice) - ); - let len = slice.len(); - buf[0..len].copy_from_slice(slice); - buf[len] = 0; - // SAFETY: buf[len] == 0 written above; buf[0..len] is valid. - return Result::Ok(unsafe { ZStr::from_raw_mut(buf.as_mut_ptr(), len) }); - } -} - -pub fn rename(from: &ZStr, to: &ZStr) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = - unsafe { uv::uv_fs_rename(uv::Loop::get(), &mut *req, from.as_ptr(), to.as_ptr(), None) }; - - log!( - "uv rename({}, {}) = {}", - BStr::new(from.as_bytes()), - BStr::new(to.as_bytes()), - rc.int() - ); - if let Some(errno) = rc.errno() { - // which one goes in the .path field? - Result::Err(Error::new(errno, Tag::rename)) - } else { - Result::Ok(()) - } -} - -pub fn link(from: &ZStr, to: &ZStr) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = - unsafe { uv::uv_fs_link(uv::Loop::get(), &mut *req, from.as_ptr(), to.as_ptr(), None) }; - - log!( - "uv link({}, {}) = {}", - BStr::new(from.as_bytes()), - BStr::new(to.as_bytes()), - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err( - Error::new(errno, Tag::link) - .with_path(from.as_bytes()) - .with_dest(to.as_bytes()), - ) - } else { - Result::Ok(()) - } -} - -pub fn symlink_uv(target: &ZStr, new_path: &ZStr, flags: c_int) -> Result<()> { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { - uv::uv_fs_symlink( - uv::Loop::get(), - &mut *req, - target.as_ptr(), - new_path.as_ptr(), - flags, - None, - ) - }; - - log!( - "uv symlink({}, {}) = {}", - BStr::new(target.as_bytes()), - BStr::new(new_path.as_bytes()), - rc.int() - ); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::symlink)) - } else { - Result::Ok(()) - } -} - -pub fn ftruncate(fd: Fd, size: i64) -> Result<()> { - // Accept `i64` directly so this matches the cross-platform `bun_sys::ftruncate` - // signature and callers don't need a per-platform `as isize` cast. - let uv_fd = fd.uv(); - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_ftruncate(uv::Loop::get(), &mut *req, uv_fd, size, None) }; - - log!("uv ftruncate({}, {}) = {}", uv_fd, size, rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::ftruncate).with_fd(fd)) - } else { - Result::Ok(()) - } -} - -pub fn fstat(fd: Fd) -> Result { - let uv_fd = fd.uv(); - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_fstat(uv::Loop::get(), &mut *req, uv_fd, None) }; - - log!("uv fstat({}) = {}", uv_fd, rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::fstat).with_fd(fd)) - } else { - // `statbuf` is inline in `fs_t` (not heap), copied out before deinit. - Result::Ok(req.statbuf) - } -} - -pub fn fdatasync(fd: Fd) -> Result<()> { - let uv_fd = fd.uv(); - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_fdatasync(uv::Loop::get(), &mut *req, uv_fd, None) }; - - log!("uv fdatasync({}) = {}", uv_fd, rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::fdatasync).with_fd(fd)) - } else { - Result::Ok(()) - } -} - -pub fn fsync(fd: Fd) -> Result<()> { - let uv_fd = fd.uv(); - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_fsync(uv::Loop::get(), &mut *req, uv_fd, None) }; - - log!("uv fsync({}) = {}", uv_fd, rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::fsync).with_fd(fd)) - } else { - Result::Ok(()) - } -} - -pub fn stat(path: &ZStr) -> Result { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_stat(uv::Loop::get(), &mut *req, path.as_ptr(), None) }; - - log!("uv stat({}) = {}", BStr::new(path.as_bytes()), rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::stat).with_path(path.as_bytes())) - } else { - // `statbuf` is inline in `fs_t` (not heap), copied out before deinit. - Result::Ok(req.statbuf) - } -} - -pub fn lstat(path: &ZStr) -> Result { - let mut req = FsReq::new(); - // SAFETY: synchronous libuv fs call; req lives on the stack for the duration. - let rc = unsafe { uv::uv_fs_lstat(uv::Loop::get(), &mut *req, path.as_ptr(), None) }; - - log!("uv lstat({}) = {}", BStr::new(path.as_bytes()), rc.int()); - if let Some(errno) = rc.errno() { - Result::Err(Error::new(errno, Tag::lstat).with_path(path.as_bytes())) - } else { - // `statbuf` is inline in `fs_t` (not heap), copied out before deinit. - Result::Ok(req.statbuf) - } -} - -pub(crate) fn close(fd: Fd) -> Option { - fd.close_allowing_bad_file_descriptor(None) -} - -/// Maximum number of iovec buffers that can be passed to uv_fs_read/uv_fs_write. -/// libuv uses c_uint for nbufs, so we must not exceed its maximum value. -const MAX_IOVEC_COUNT: usize = c_uint::MAX as usize; - -/// Maximum size of a single buffer in uv_buf_t. -/// libuv uses ULONG (u32) for the buffer length on Windows. -const MAX_BUF_LEN: usize = u32::MAX as usize; - -/// Returns the total byte capacity of a slice of iovec buffers. -fn sum_bufs_len(bufs: &[PlatformIOVec]) -> usize { - let mut total: usize = 0; - for buf in bufs { - total += buf.len as usize; - } - total -} - -pub fn preadv(fd: Fd, bufs: &[PlatformIOVec], position: i64) -> Result { - let uv_fd = fd.uv(); - const _: () = assert!( - core::mem::size_of::() == core::mem::size_of::() - && core::mem::align_of::() == core::mem::align_of::() - ); - - let debug_timer = bun_core::Output::DebugTimer::start(); - - let mut total_read: usize = 0; - let mut remaining_bufs = bufs; - let mut current_position = position; - - while !remaining_bufs.is_empty() { - let chunk_len = remaining_bufs.len().min(MAX_IOVEC_COUNT); - let chunk_bufs = &remaining_bufs[0..chunk_len]; - - // `uv_fs_read` heap-allocates - // `req->fs.info.bufs` when `nbufs > 4` and self-points it at - // `req->fs.info.bufsml` when `nbufs <= 4`; `FsReq::drop` runs - // `uv_fs_req_cleanup` in place every iteration (early-return included). - let mut req = FsReq::new(); - - // The int return value of uv_fs_read truncates req.result (ssize_t) and - // wraps negative when bytes read > INT_MAX, so use req.result directly. - // SAFETY: synchronous libuv fs call; req and chunk_bufs live on the stack. - let _ = unsafe { - uv::uv_fs_read( - uv::Loop::get(), - &mut *req, - uv_fd, - chunk_bufs.as_ptr(), - c_uint::try_from(chunk_len).expect("int cast"), - current_position, - None, - ) - }; - - let chunk_capacity = sum_bufs_len(chunk_bufs); - - if cfg!(debug_assertions) { - log!( - "uv read({}, {} total bytes) = {} ({})", - uv_fd, - chunk_capacity, - req.result.int(), - debug_timer - ); - } - - if let Some(e) = req.result.errno() { - return Result::Err(Error::new(e, Tag::read).with_fd(fd)); - } - - let bytes_read: usize = usize::try_from(req.result.int()).expect("int cast"); - total_read += bytes_read; - - // If we read less than requested, we're done (EOF or partial read) - if bytes_read == 0 || bytes_read < chunk_capacity { - break; - } - - remaining_bufs = &remaining_bufs[chunk_len..]; - - // Update position for the next chunk (if position tracking is enabled) - if current_position >= 0 { - current_position += i64::try_from(bytes_read).expect("int cast"); - } - } - - Result::Ok(total_read) -} - -pub fn pwritev(fd: Fd, bufs: &[PlatformIOVecConst], position: i64) -> Result { - let uv_fd = fd.uv(); - const _: () = assert!( - core::mem::size_of::() == core::mem::size_of::() - && core::mem::align_of::() == core::mem::align_of::() - ); - - let debug_timer = bun_core::Output::DebugTimer::start(); - - let mut total_written: usize = 0; - let mut remaining_bufs = bufs; - let mut current_position = position; - - while !remaining_bufs.is_empty() { - let chunk_len = remaining_bufs.len().min(MAX_IOVEC_COUNT); - let chunk_bufs = &remaining_bufs[0..chunk_len]; - - // `uv_fs_write` heap-allocates - // `req->fs.info.bufs` when `nbufs > 4` and self-points it at - // `req->fs.info.bufsml` when `nbufs <= 4`; `FsReq::drop` runs - // `uv_fs_req_cleanup` in place every iteration (early-return included). - let mut req = FsReq::new(); - - // The int return value of uv_fs_write truncates req.result (ssize_t) and - // wraps negative when bytes written > INT_MAX, so use req.result directly. - // SAFETY: synchronous libuv fs call; req and chunk_bufs live on the stack. - let _ = unsafe { - uv::uv_fs_write( - uv::Loop::get(), - &mut *req, - uv_fd, - chunk_bufs.as_ptr().cast(), - c_uint::try_from(chunk_len).expect("int cast"), - current_position, - None, - ) - }; - - // `sum_bufs_len` expects `&[PlatformIOVec]`; rather than repr-punning the - // const slice through `from_raw_parts`, sum the `.len` fields directly. - let chunk_capacity: usize = chunk_bufs.iter().map(|b| b.len as usize).sum(); - - if cfg!(debug_assertions) { - log!( - "uv write({}, {} total bytes) = {} ({})", - uv_fd, - chunk_capacity, - req.result.int(), - debug_timer - ); - } - - if let Some(e) = req.result.errno() { - return Result::Err(Error::new(e, Tag::write).with_fd(fd)); - } - - let bytes_written: usize = usize::try_from(req.result.int()).expect("int cast"); - total_written += bytes_written; - - // If we wrote less than requested, we're done (partial write) - if bytes_written == 0 || bytes_written < chunk_capacity { - break; - } - - remaining_bufs = &remaining_bufs[chunk_len..]; - - // Update position for the next chunk (if position tracking is enabled) - if current_position >= 0 { - current_position += i64::try_from(bytes_written).expect("int cast"); - } - } - - Result::Ok(total_written) -} - -#[inline] -pub fn readv(fd: Fd, bufs: &[PlatformIOVec]) -> Result { - preadv(fd, bufs, -1) -} - -pub fn pread(fd: Fd, buf: &mut [u8], position: i64) -> Result { - // If buffer fits in a single uv_buf_t, use the simple path - if buf.len() <= MAX_BUF_LEN { - let bufs: [PlatformIOVec; 1] = [crate::platform_iovec_create(buf)]; - return preadv(fd, &bufs, position); - } - - // Buffer is too large, need to chunk it - let mut total_read: usize = 0; - let mut remaining = buf; - let mut current_position = position; - - while !remaining.is_empty() { - let chunk_len = remaining.len().min(MAX_BUF_LEN); - let bufs: [PlatformIOVec; 1] = [crate::platform_iovec_create(&mut remaining[0..chunk_len])]; - - match preadv(fd, &bufs, current_position) { - Result::Err(err) => return Result::Err(err), - Result::Ok(bytes_read) => { - total_read += bytes_read; - - if bytes_read == 0 || bytes_read < chunk_len { - break; - } - - remaining = &mut remaining[chunk_len..]; - if current_position >= 0 { - current_position += i64::try_from(bytes_read).expect("int cast"); - } - } - } - } - - Result::Ok(total_read) -} - -pub fn read(fd: Fd, buf: &mut [u8]) -> Result { - // If buffer fits in a single uv_buf_t, use the simple path - if buf.len() <= MAX_BUF_LEN { - let bufs: [PlatformIOVec; 1] = [crate::platform_iovec_create(buf)]; - return readv(fd, &bufs); - } - - // Buffer is too large, need to chunk it - let mut total_read: usize = 0; - let mut remaining = buf; - - while !remaining.is_empty() { - let chunk_len = remaining.len().min(MAX_BUF_LEN); - let bufs: [PlatformIOVec; 1] = [crate::platform_iovec_create(&mut remaining[0..chunk_len])]; - - match readv(fd, &bufs) { - Result::Err(err) => return Result::Err(err), - Result::Ok(bytes_read) => { - total_read += bytes_read; - - if bytes_read == 0 || bytes_read < chunk_len { - break; - } - - remaining = &mut remaining[chunk_len..]; - } - } - } - - Result::Ok(total_read) -} - -#[inline] -pub fn writev(fd: Fd, bufs: &[PlatformIOVec]) -> Result { - // SAFETY: `PlatformIOVec` (= `uv_buf_t`) and `PlatformIOVecConst` are - // layout-identical on Windows (size/align asserted in lib.rs); the - // fat-pointer cast preserves the original slice's (ptr, len) metadata - // exactly instead of re-deriving it. - let const_bufs = unsafe { &*(bufs as *const [PlatformIOVec] as *const [PlatformIOVecConst]) }; - pwritev(fd, const_bufs, -1) -} - -pub fn pwrite(fd: Fd, buf: &[u8], position: i64) -> Result { - // If buffer fits in a single uv_buf_t, use the simple path - if buf.len() <= MAX_BUF_LEN { - let bufs: [PlatformIOVecConst; 1] = [crate::platform_iovec_const_create(buf)]; - return pwritev(fd, &bufs, position); - } - - // Buffer is too large, need to chunk it - let mut total_written: usize = 0; - let mut remaining = buf; - let mut current_position = position; - - while !remaining.is_empty() { - let chunk_len = remaining.len().min(MAX_BUF_LEN); - let bufs: [PlatformIOVecConst; 1] = - [crate::platform_iovec_const_create(&remaining[0..chunk_len])]; - - match pwritev(fd, &bufs, current_position) { - Result::Err(err) => return Result::Err(err), - Result::Ok(bytes_written) => { - total_written += bytes_written; - - if bytes_written == 0 || bytes_written < chunk_len { - break; - } - - remaining = &remaining[chunk_len..]; - if current_position >= 0 { - current_position += i64::try_from(bytes_written).expect("int cast"); - } - } - } - } - - Result::Ok(total_written) -} - -pub fn write(fd: Fd, buf: &[u8]) -> Result { - // If buffer fits in a single uv_buf_t, use the simple path - if buf.len() <= MAX_BUF_LEN { - let bufs: [PlatformIOVecConst; 1] = [crate::platform_iovec_const_create(buf)]; - return writev_const(fd, &bufs); - } - - // Buffer is too large, need to chunk it - let mut total_written: usize = 0; - let mut remaining = buf; - - while !remaining.is_empty() { - let chunk_len = remaining.len().min(MAX_BUF_LEN); - let bufs: [PlatformIOVecConst; 1] = - [crate::platform_iovec_const_create(&remaining[0..chunk_len])]; - - match writev_const(fd, &bufs) { - Result::Err(err) => return Result::Err(err), - Result::Ok(bytes_written) => { - total_written += bytes_written; - - if bytes_written == 0 || bytes_written < chunk_len { - break; - } - - remaining = &remaining[chunk_len..]; - } - } - } - - Result::Ok(total_written) -} - -// `write()` builds a `[1]PlatformIOVecConst`, but `writev` takes -// `&[PlatformIOVec]`; route through pwritev directly with position = -1. -// TODO(refactor): unify the iovec types on Windows. -#[inline] -fn writev_const(fd: Fd, bufs: &[PlatformIOVecConst]) -> Result { - pwritev(fd, bufs, -1) -} diff --git a/src/sys/tmp.rs b/src/sys/tmp.rs index bc72fd36ecdb..6fb4f90b0b4a 100644 --- a/src/sys/tmp.rs +++ b/src/sys/tmp.rs @@ -1,6 +1,6 @@ use bun_core::ZStr; -use crate::{ErrorCase, Fd, FdExt, Mode, O, Tag}; +use crate::{Fd, Mode, O}; // To be used with files // not folders! @@ -26,8 +26,7 @@ impl<'a> Tmpfile<'a> { tmpfilename, O::CREAT | O::EXCL | O::CLOEXEC | O::WRONLY, perm, - )? - .make_lib_uv_owned_for_syscall(Tag::open, ErrorCase::CloseOnFail)?; + )?; Ok(Tmpfile { destination_dir, diff --git a/src/sys/windows/O.rs b/src/sys/windows/O.rs new file mode 100644 index 000000000000..f1e2c02d6de1 --- /dev/null +++ b/src/sys/windows/O.rs @@ -0,0 +1,31 @@ +//! The numeric `O_*` values Node exposes as `fs.constants` on Windows: MSVC's +//! `_O_*`, which are `bun_sys::O`'s, plus the `UV_FS_O_*` extensions. + +pub use crate::O::{APPEND, CREAT, EXCL, RDONLY, RDWR, SEQUENTIAL, TRUNC, WRONLY}; + +pub const FILEMAP: i32 = 0x2000_0000; +pub const DIRECT: i32 = 0x0200_0000; +pub const DSYNC: i32 = 0x0400_0000; +pub const SYNC: i32 = 0x0800_0000; + +/// The bits `open` reads. +const READ: i32 = + WRONLY | RDWR | CREAT | EXCL | TRUNC | APPEND | SEQUENTIAL | FILEMAP | DIRECT | DSYNC | SYNC; + +const _: () = { + use crate::O::{CLOEXEC, DIRECTORY, NOATIME, NOFOLLOW, NONBLOCK, PATH}; + assert!(READ & (CLOEXEC | DIRECTORY | NOATIME | NOFOLLOW | NONBLOCK | PATH) == 0); +}; + +/// A numeric `flags` from JS as `bun_sys::O` values. The other `_O_*` bits are +/// dropped: `_O_WTEXT` and its neighbours have the numbers of `O::DIRECTORY`, +/// `O::NOFOLLOW` and `O::NOATIME`, which MSVC lacks. `SYNC` wins over `DSYNC`; +/// `open` refuses the two together. +pub fn from_js(flags: i32) -> i32 { + let flags = flags & READ; + if flags & SYNC != 0 { + flags & !DSYNC + } else { + flags + } +} diff --git a/src/sys/windows/fs.rs b/src/sys/windows/fs.rs new file mode 100644 index 000000000000..37bf7b742684 --- /dev/null +++ b/src/sys/windows/fs.rs @@ -0,0 +1,2494 @@ +//! Native Win32/NT implementations of `bun_sys`'s file operations on Windows. +//! +//! What callers can observe — `CreateFileW` parameters, how `st_mode` is +//! synthesised, which Win32 error becomes which errno, which reparse points +//! count as symlinks — follows libuv's `src/win/fs.c`, because that is what +//! `node:fs` exposes on Windows. + +use core::ffi::c_void; +use core::ops::Range; +use core::ptr; +use core::sync::atomic::{AtomicUsize, Ordering}; + +use bun_core::{S, Timespec, ZStr}; +use bun_paths::{is_drive_letter_t, is_sep_any_t}; +use bun_windows_sys as win32; + +use super::{ + EPOCH_DIFFERENCE_100NS, HANDLE, INVALID_HANDLE_VALUE, NTSTATUS, O, Win32Error, + Win32ErrorExt as _, +}; +use crate::{E, Error, Fd, Maybe, Mode, PlatformIoVec, PlatformIoVecConst, Tag, TimeLike}; + +type Win32Result = core::result::Result; + +pub(crate) const SHARE_ALL: u32 = + win32::FILE_SHARE_READ | win32::FILE_SHARE_WRITE | win32::FILE_SHARE_DELETE; + +// ────────────────────────────────────────────────────────────────────────── +// Types +// ────────────────────────────────────────────────────────────────────────── + +/// `bun_sys::Stat` on Windows. Every numeric field is a `u64` so the values +/// reach JS `Stats` without per-field width handling. +#[repr(C)] +#[derive(Clone, Copy, Debug, Default)] +pub struct Stat { + pub st_dev: u64, + pub st_mode: u64, + pub st_nlink: u64, + pub st_uid: u64, + pub st_gid: u64, + pub st_rdev: u64, + pub st_ino: u64, + pub st_size: u64, + pub st_blksize: u64, + pub st_blocks: u64, + pub atim: Timespec, + pub mtim: Timespec, + pub ctim: Timespec, + pub birthtim: Timespec, +} +impl Stat { + #[inline] + pub fn size(&self) -> u64 { + self.st_size + } +} +// SAFETY: integers only; all-zero is a valid value. +unsafe impl bun_core::ffi::Zeroable for Stat {} + +/// `bun_sys::StatFS` on Windows. +#[repr(C)] +#[derive(Clone, Copy, Debug, Default)] +pub struct StatFS { + pub f_type: u64, + pub f_bsize: u64, + pub f_blocks: u64, + pub f_bfree: u64, + pub f_bavail: u64, + pub f_files: u64, + pub f_ffree: u64, +} +// SAFETY: integers only; all-zero is a valid value. +unsafe impl bun_core::ffi::Zeroable for StatFS {} + +const TICKS_PER_SEC: i64 = 10_000_000; + +fn filetime_to_timespec(filetime: i64) -> Timespec { + // Split before moving the epoch: the subtraction cannot overflow then, + // whatever the volume has stored. + let sec = filetime.div_euclid(TICKS_PER_SEC) - EPOCH_DIFFERENCE_100NS / TICKS_PER_SEC; + let nsec = filetime.rem_euclid(TICKS_PER_SEC) * 100; + Timespec { sec, nsec } +} + +// ────────────────────────────────────────────────────────────────────────── +// Helpers +// ────────────────────────────────────────────────────────────────────────── + +/// The error for a write that failed with `code`: a closed pipe is `EPIPE`. +/// (Everywhere else `ERROR_BROKEN_PIPE` is `EOF` and `ERROR_NO_DATA` is `EAGAIN`.) +fn write_error(code: Win32Error, tag: Tag) -> Error { + match code { + Win32Error::BROKEN_PIPE | Win32Error::NO_DATA => Error::from_code(E::EPIPE, tag), + // Writing through a handle that was not opened for writing. + Win32Error::ACCESS_DENIED => Error::from_code(E::EBADF, tag), + _ => Error::from_win32(code, tag), + } +} + +/// The error for a read that failed with `code`, or `None` when the failure +/// means end-of-file. +fn read_error(code: Win32Error, tag: Tag) -> Option { + match code { + Win32Error::BROKEN_PIPE | Win32Error::HANDLE_EOF => None, + // Reading through a handle that was not opened for reading. + Win32Error::ACCESS_DENIED => Some(Error::from_code(E::EBADF, tag)), + _ => Some(Error::from_win32(code, tag)), + } +} + +fn bad_fd(fd: Fd, tag: Tag) -> Error { + Error { + errno: E::EBADF as _, + syscall: tag, + fd, + ..Default::default() + } +} + +/// The HANDLE behind either kind of `fd`, or `EBADF`. +fn handle_of(fd: Fd, tag: Tag) -> Maybe { + let handle = fd.native(); + if handle == INVALID_HANDLE_VALUE { + return Err(bad_fd(fd, tag)); + } + Ok(handle) +} + +struct OwnedHandle(HANDLE); +impl Drop for OwnedHandle { + fn drop(&mut self) { + // SAFETY: `self.0` is a live handle this value owns. + unsafe { win32::CloseHandle(self.0) }; + } +} + +fn create_file( + path: *const u16, + access: u32, + share: u32, + disposition: u32, + flags_and_attributes: u32, +) -> Win32Result { + // SAFETY: `path` is a NUL-terminated wide string that outlives the call. + let handle = unsafe { + win32::CreateFileW( + path, + access, + share, + ptr::null_mut(), + disposition, + flags_and_attributes, + ptr::null_mut(), + ) + }; + if handle == INVALID_HANDLE_VALUE { + return Err(Win32Error::get()); + } + Ok(OwnedHandle(handle)) +} + +/// Paths at least this long (the `CreateDirectoryW` limit, the smallest of the +/// `MAX_PATH`-derived limits) are made absolute and `\\?\`-prefixed: the +/// `longPathAware` manifest has no effect unless the machine-wide +/// `LongPathsEnabled` policy is also on. +const LONG_PATH_THRESHOLD: usize = 248; + +/// `\\?\…` and `\\.\…`. +fn is_device_path(path: &[u16]) -> bool { + path.len() >= 4 + && is_sep_any_t(path[0]) + && is_sep_any_t(path[1]) + && (path[2] == b'?' as u16 || path[2] == b'.' as u16) + && is_sep_any_t(path[3]) +} + +/// Whether Win32 holds `path` to `MAX_PATH`. A relative path counts with the +/// directory Win32 resolves it against. +fn exceeds_max_path(path: &[u16]) -> bool { + if is_device_path(path) { + return false; + } + if path.len() >= LONG_PATH_THRESHOLD { + return true; + } + let is_absolute = path.len() >= 3 && path[1] == b':' as u16 && is_sep_any_t(path[2]) + || path.len() >= 2 && is_sep_any_t(path[0]) && is_sep_any_t(path[1]); + if is_absolute { + return false; + } + path.len() + current_directory_len() >= LONG_PATH_THRESHOLD +} + +/// The length of the current directory, NUL included; 0 until it is asked for. +/// Asking takes the process-wide PEB lock, so it is asked once per directory +/// rather than once per relative path. +static CURRENT_DIRECTORY_LEN: AtomicUsize = AtomicUsize::new(0); +static CURRENT_DIRECTORY_LOCK: bun_core::Mutex<()> = bun_core::Mutex::new(()); + +fn current_directory_len() -> usize { + let len = CURRENT_DIRECTORY_LEN.load(Ordering::Relaxed); + if len != 0 { + return len; + } + let _guard = CURRENT_DIRECTORY_LOCK.lock(); + read_current_directory_len() +} + +/// Caller holds `CURRENT_DIRECTORY_LOCK`. +fn read_current_directory_len() -> usize { + // SAFETY: a zero-length query writes nothing and returns the length of + // the current directory, NUL included. + let len = + unsafe { bun_windows_sys::externs::GetCurrentDirectoryW(0, ptr::null_mut()) } as usize; + CURRENT_DIRECTORY_LEN.store(len, Ordering::Relaxed); + len +} + +/// `SetCurrentDirectoryW`. The only call in the process that changes the +/// current directory, which is what keeps `CURRENT_DIRECTORY_LEN` true. +pub(crate) fn set_current_directory(path: &bun_core::WStr) -> Win32Result<()> { + let _guard = CURRENT_DIRECTORY_LOCK.lock(); + // SAFETY: `path` is NUL-terminated. + if unsafe { bun_windows_sys::externs::SetCurrentDirectoryW(path.as_ptr()) } == 0 { + return Err(Win32Error::get()); + } + read_current_directory_len(); + Ok(()) +} + +/// Writes the NUL-terminated `path` into `full`, resolved by +/// `GetFullPathNameW` and `\\?\`-prefixed. Returns where in `full` it is. +fn write_long_path(path: *const u16, full: &mut [u16]) -> Win32Result> { + // Room in front of the resolved path for `\\?\UNC\`. + const RESERVE: usize = 8; + let capacity = full.len() - RESERVE; + // SAFETY: the caller passes a NUL-terminated `path`; `full[RESERVE..]` is + // writable for `capacity` units. + let n = unsafe { + win32::GetFullPathNameW( + path, + capacity as u32, + full[RESERVE..].as_mut_ptr(), + ptr::null_mut(), + ) + } as usize; + if n == 0 { + return Err(Win32Error::get()); + } + if n >= capacity { + return Err(Win32Error::FILENAME_EXCED_RANGE); + } + let resolved = &full[RESERVE..RESERVE + n]; + Ok(if is_device_path(resolved) { + // A device name resolved to `\\.\NAME`. + RESERVE..RESERVE + n + } else if n >= 2 && is_sep_any_t(resolved[0]) && is_sep_any_t(resolved[1]) { + // `\\server\share\…` → `\\?\UNC\server\share\…` + let start = RESERVE + 1 - 7; + for (dst, src) in full[start..start + 7].iter_mut().zip(b"\\\\?\\UNC") { + *dst = u16::from(*src); + } + start..RESERVE + n + } else { + let start = RESERVE - 4; + full[start..RESERVE].copy_from_slice(&super::LONG_PATH_PREFIX); + start..RESERVE + n + }) +} + +/// For a wide path that goes to a Win32 call without a [`WPath`]: puts the +/// NUL-terminated `buf[..len]` into the form Win32 takes past `MAX_PATH` when +/// it needs that, and returns its length. +fn lengthen_path_in_place(buf: &mut [u16], len: usize) -> Win32Result { + if !exceeds_max_path(&buf[..len]) { + return Ok(len); + } + let mut full = bun_paths::w_path_buffer_pool::get(); + let long = write_long_path(buf.as_ptr(), &mut full[..])?; + let len = long.len(); + if len >= buf.len() { + return Err(Win32Error::FILENAME_EXCED_RANGE); + } + buf[..len].copy_from_slice(&full[long]); + buf[len] = 0; + Ok(len) +} + +/// `path` as the NUL-terminated wide string a kernel32 call takes, in `buf`: +/// the form Win32 takes past `MAX_PATH` when it needs that. Returns its length. +/// An absolute `path` gets a `\\?\` prefix and names its file literally, so the +/// caller has resolved its `.` and `..`. +pub fn kernel32_path(buf: &mut [u16], path: &[u8]) -> Win32Result { + let len = bun_paths::string_paths::try_to_kernel32_path(buf, path) + .ok_or(Win32Error::INVALID_NAME)? + .len(); + lengthen_path_in_place(buf, len) +} + +/// A NUL-terminated UTF-16 path for a Win32 call. +/// +/// Short paths are passed through as given, so Win32 resolves relative paths, +/// drive-relative paths, `.`/`..` and device names (`NUL`, `CON`) as usual. +pub struct WPath { + buf: bun_paths::w_path_buffer_pool::Guard, + start: usize, + len: usize, +} + +impl WPath { + pub fn new(path: &[u8]) -> Win32Result { + let wpath = WPath::converted(path)?; + if !exceeds_max_path(wpath.units()) { + return Ok(wpath); + } + wpath.into_long() + } + + /// The directory `path` names where it leads a longer path, absolute and + /// `\\?\`-prefixed, so a call names it (and each level above it) literally. + /// Win32 names a segment differently by position: a leading segment loses + /// one trailing period and keeps its spaces, the last segment loses every + /// trailing period and space. + pub fn directory(path: &[u8]) -> Win32Result { + let mut wpath = WPath::converted(path)?; + if !is_device_path(wpath.units()) { + let len = wpath.len; + // `X:` is the current directory of drive X; `X:\` is its root. + let is_drive = len == 2 && wpath.buf[1] == b':' as u16; + if len > 0 && !is_drive && !is_sep_any_t(wpath.buf[len - 1]) { + wpath.buf[len] = b'\\' as u16; + wpath.buf[len + 1] = 0; + wpath.len = len + 1; + } + wpath = wpath.into_long()?; + } + let units = wpath.units(); + let mut len = units.len(); + while len > 1 && is_sep_any_t(units[len - 1]) && units[len - 2] != b':' as u16 { + len -= 1; + } + wpath.truncate(len); + Ok(wpath) + } + + /// `path` as the NT object name (`\??\…`) that Win32 turns it into, for a + /// native call. + fn nt(path: &[u8]) -> Win32Result { + let mut wpath = WPath::converted(path)?; + // Win32 takes a `\\?\` path as it is, and resolves any other. + if !(is_device_path(wpath.units()) && wpath.units()[2] == b'?' as u16) { + wpath = wpath.into_long()?; + } + // `\\?\` and `\\.\` are both `\??\`. + let start = wpath.start; + wpath.buf[start..start + 4].copy_from_slice(&super::NT_OBJECT_PREFIX); + Ok(wpath) + } + + /// `path` in UTF-16 as it was written, with `/` turned into `\`. + fn converted(path: &[u8]) -> Win32Result { + use bun_paths::string_paths; + if !string_paths::fits_in_wide_path_buffer(string_paths::without_nt_prefix(path)) { + return Err(Win32Error::FILENAME_EXCED_RANGE); + } + let mut buf = bun_paths::w_path_buffer_pool::get(); + // Bytes that are no name fail as they do in libuv and Node (`ENOENT`). + let len = string_paths::try_to_w_path(&mut buf[..], path) + .ok_or(Win32Error::INVALID_NAME)? + .len(); + + // `\??\` is the NT spelling of `\\?\`; Win32 only documents the latter. + if len >= 4 && buf[..4] == super::NT_OBJECT_PREFIX { + buf[1] = b'\\' as u16; + } + Ok(WPath { buf, start: 0, len }) + } + + /// Resolved by `GetFullPathNameW` and `\\?\`-prefixed. + fn into_long(self) -> Win32Result { + let mut full = bun_paths::w_path_buffer_pool::get(); + let long = write_long_path(self.as_ptr(), &mut full[..])?; + Ok(WPath { + buf: full, + start: long.start, + len: long.len(), + }) + } + + /// Conversion only: the units are what the caller wrote, with `/` turned + /// into `\`. For strings that are stored rather than opened (a symlink's + /// target), where making the path absolute would change its meaning. + fn verbatim(path: &[u8]) -> Win32Result { + if !bun_paths::string_paths::fits_in_wide_path_buffer(path) { + return Err(Win32Error::FILENAME_EXCED_RANGE); + } + let mut buf = bun_paths::w_path_buffer_pool::get(); + let len = bun_paths::string_paths::try_to_w_path(&mut buf[..], path) + .ok_or(Win32Error::INVALID_NAME)? + .len(); + Ok(WPath { buf, start: 0, len }) + } + + #[inline] + pub fn as_ptr(&self) -> *const u16 { + self.buf[self.start..].as_ptr() + } + + #[inline] + pub fn as_wstr(&self) -> &bun_core::WStr { + bun_core::WStr::from_buf(&self.buf[self.start..], self.len) + } + + #[inline] + fn units(&self) -> &[u16] { + &self.buf[self.start..self.start + self.len] + } + + /// The units followed by the terminating NUL. + #[inline] + fn units_with_nul_mut(&mut self) -> &mut [u16] { + &mut self.buf[self.start..self.start + self.len + 1] + } + + fn truncate(&mut self, len: usize) { + debug_assert!(len <= self.len); + self.len = len; + self.buf[self.start + len] = 0; + } +} + +// ────────────────────────────────────────────────────────────────────────── +// open +// ────────────────────────────────────────────────────────────────────────── + +/// What [`OpenRequest`] does with a file that exists or does not. +#[derive(Clone, Copy, PartialEq, Eq)] +pub(crate) enum Disposition { + Open, + OpenOrCreate, + CreateNew, + Truncate, + CreateOrTruncate, +} + +/// What the flags and mode of `open(2)` ask of Windows, by the rules of +/// libuv's `fs__open`. [`open`] spells it for `CreateFileW`, `openat` for +/// `NtCreateFile`. +pub(crate) struct OpenRequest { + /// File-specific rights, no `GENERIC_*` bits. `O_APPEND` is + /// `FILE_APPEND_DATA` without `FILE_WRITE_DATA`: the kernel then puts every + /// write at end-of-file atomically, whatever offset it is given. + pub access: u32, + pub disposition: Disposition, + /// A file this open creates is read-only. + pub read_only: bool, + pub no_buffering: bool, + pub write_through: bool, + pub sequential: bool, +} + +impl OpenRequest { + /// `O::FILEMAP` (Node's `UV_FS_O_FILEMAP`) requests I/O through a file + /// mapping. File contents are the same without it, so it is accepted and + /// ignored, as Node documents for every other OS. + pub(crate) fn new(flags: i32, mode: Mode) -> core::result::Result { + let mut access = match flags & (O::RDONLY | O::WRONLY | O::RDWR) { + O::RDONLY => win32::FILE_GENERIC_READ, + O::WRONLY => win32::FILE_GENERIC_WRITE, + O::RDWR => win32::FILE_GENERIC_READ | win32::FILE_GENERIC_WRITE, + _ => return Err(E::EINVAL), + }; + + if flags & O::APPEND != 0 { + access &= !win32::FILE_WRITE_DATA; + access |= win32::FILE_APPEND_DATA; + } + + const CREAT_EXCL: i32 = O::CREAT | O::EXCL; + const CREAT_TRUNC_EXCL: i32 = O::CREAT | O::TRUNC | O::EXCL; + const TRUNC_EXCL: i32 = O::TRUNC | O::EXCL; + const CREAT_TRUNC: i32 = O::CREAT | O::TRUNC; + let disposition = match flags & (O::CREAT | O::EXCL | O::TRUNC) { + 0 | O::EXCL => Disposition::Open, + O::CREAT => Disposition::OpenOrCreate, + CREAT_EXCL | CREAT_TRUNC_EXCL => Disposition::CreateNew, + O::TRUNC | TRUNC_EXCL => Disposition::Truncate, + CREAT_TRUNC => Disposition::CreateOrTruncate, + _ => return Err(E::EINVAL), + }; + + // A zero mode would create the file read-only. + let mode = if mode == 0 { 0o644 } else { mode }; + let read_only = flags & O::CREAT != 0 && (mode & !crate::get_umask()) & S::IWUSR == 0; + + let no_buffering = flags & O::DIRECT != 0; + if no_buffering { + // FILE_APPEND_DATA (part of FILE_GENERIC_WRITE) cannot be combined + // with unbuffered I/O. FILE_WRITE_DATA also permits appends, so + // drop it when both are present; append-only + direct is invalid. + if access & win32::FILE_APPEND_DATA != 0 { + if access & win32::FILE_WRITE_DATA != 0 { + access &= !win32::FILE_APPEND_DATA; + } else { + return Err(E::EINVAL); + } + } + } + + let write_through = match flags & (O::DSYNC | O::SYNC) { + 0 => false, + O::DSYNC | O::SYNC => true, + _ => return Err(E::EINVAL), + }; + + Ok(OpenRequest { + access, + disposition, + read_only, + no_buffering, + write_through, + sequential: flags & O::SEQUENTIAL != 0, + }) + } +} + +/// `open(2)` over `CreateFileW`. `flags` are `bun_sys::O` values. The result +/// is a HANDLE-kind `Fd`. +pub fn open(path: &ZStr, flags: i32, mode: Mode) -> Maybe { + let result = open_impl(path.as_bytes(), flags, mode); + crate::syslog!( + "open({}, {:#o}, {:#o}) = {:?}", + bstr::BStr::new(path.as_bytes()), + flags, + mode, + result.as_ref().map(|h| *h as usize) + ); + match result { + Ok(handle) => Ok(Fd::from_system(handle)), + Err(errno) => Err(Error::from_code(errno, Tag::open).with_path(path.as_bytes())), + } +} + +fn open_impl(path: &[u8], flags: i32, mode: Mode) -> core::result::Result { + let request = OpenRequest::new(flags, mode)?; + let disposition = match request.disposition { + Disposition::Open => win32::OPEN_EXISTING, + Disposition::OpenOrCreate => win32::OPEN_ALWAYS, + Disposition::CreateNew => win32::CREATE_NEW, + Disposition::Truncate => win32::TRUNCATE_EXISTING, + Disposition::CreateOrTruncate => win32::CREATE_ALWAYS, + }; + + // FILE_FLAG_BACKUP_SEMANTICS makes it possible to open a directory. + let mut attributes = win32::FILE_ATTRIBUTE_NORMAL | win32::FILE_FLAG_BACKUP_SEMANTICS; + if request.read_only { + attributes |= win32::FILE_ATTRIBUTE_READONLY; + } + if request.no_buffering { + attributes |= win32::FILE_FLAG_NO_BUFFERING; + } + if request.write_through { + attributes |= win32::FILE_FLAG_WRITE_THROUGH; + } + if request.sequential { + attributes |= win32::FILE_FLAG_SEQUENTIAL_SCAN; + } + + let wpath = WPath::new(path).map_err(|e| e.to_e())?; + // All sharing modes, to match UNIX semantics: in particular the file can + // be deleted or renamed while it is open. + match create_file( + wpath.as_ptr(), + request.access, + SHARE_ALL, + disposition, + attributes, + ) { + Ok(handle) => { + let raw = handle.0; + core::mem::forget(handle); + Ok(raw) + } + // With O_CREAT and no O_EXCL this means the path is a directory. + Err(Win32Error::FILE_EXISTS) if flags & O::CREAT != 0 && flags & O::EXCL == 0 => { + Err(E::EISDIR) + } + Err(e) => Err(e.to_e()), + } +} + +// ────────────────────────────────────────────────────────────────────────── +// read / write +// ────────────────────────────────────────────────────────────────────────── + +pub fn read(fd: Fd, buf: &mut [u8]) -> Maybe { + read_at(fd, buf, None) +} + +pub fn write(fd: Fd, buf: &[u8]) -> Maybe { + write_at(fd, buf, None) +} + +/// Whether `fd` is a file on a disk, where a write is over once the file +/// system has taken the bytes. A write to anything else (a pipe, a console, a +/// serial port) waits for as long as whatever is at its other end likes. +pub fn is_disk_file(fd: Fd) -> bool { + super::GetFileType(fd.native()) == super::FILE_TYPE_DISK +} + +/// Whether `fd` is a synchronous file object: [`read`] and [`write`] on it +/// return when the transfer is done. On one opened with +/// `FILE_FLAG_OVERLAPPED` they do not. `None` when the query fails. +/// +/// The query takes a synchronous file object's lock, which I/O another thread +/// or process has in flight on it holds, as the read or write would. +pub fn is_synchronous(fd: Fd) -> Option { + const FILE_SYNCHRONOUS_IO_ALERT: u32 = 0x0000_0010; + let mut mode: u32 = 0; + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + // SAFETY: FFI; the class writes one `ULONG` into `mode`. + let status = unsafe { + win32::ntdll::NtQueryInformationFile( + fd.native(), + &mut io, + ptr::from_mut(&mut mode).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileModeInformation, + ) + }; + (status == win32::NTSTATUS::SUCCESS) + .then_some(mode & (FILE_SYNCHRONOUS_IO_ALERT | win32::FILE_SYNCHRONOUS_IO_NONALERT) != 0) +} + +/// The `OVERLAPPED` of a synchronous `ReadFile`/`WriteFile` at `off`. +fn overlapped_at(off: u64) -> win32::OVERLAPPED { + win32::OVERLAPPED { + Internal: 0, + InternalHigh: 0, + Offset: off as u32, + OffsetHigh: (off >> 32) as u32, + hEvent: core::ptr::null_mut(), + } +} + +/// A positioned `ReadFile`/`WriteFile` on a synchronous handle moves its +/// file pointer too. `fs.read(fd, .., position)` must leave it alone, so it +/// is put back for the fds JS can see (CRT fds). HANDLE-kind callers never +/// mix positioned and sequential I/O on one handle and skip the two calls, +/// but for a duplicate of such an fd ([`pread_keeping_position`]). +struct RestoreFilePointer { + handle: HANDLE, + saved: Option, +} + +impl RestoreFilePointer { + fn new(fd: Fd) -> Self { + Self::new_if(fd, fd.kind() == crate::FdKind::Crt) + } + + fn new_if(fd: Fd, restore: bool) -> Self { + const FILE_POSITION_INFORMATION: win32::FILE_INFORMATION_CLASS = + win32::FILE_INFORMATION_CLASS::FilePositionInformation; + let handle = fd.native(); + let mut saved = None; + if restore { + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut current: i64 = 0; + // One system call; `SetFilePointerEx(FILE_CURRENT)` is this + // query followed by a set. + // SAFETY: FFI; `handle` is the fd's HANDLE, and the class + // writes one `LARGE_INTEGER` into `current`. + let status = unsafe { + win32::ntdll::NtQueryInformationFile( + handle, + &mut io, + core::ptr::from_mut(&mut current).cast(), + core::mem::size_of::() as u32, + FILE_POSITION_INFORMATION, + ) + }; + if win32::NT_SUCCESS(status) { + saved = Some(current); + } + } + Self { handle, saved } + } +} + +impl Drop for RestoreFilePointer { + fn drop(&mut self) { + if let Some(position) = self.saved { + // SAFETY: FFI; `handle` outlives the guard. + unsafe { + win32::SetFilePointerEx( + self.handle, + position, + core::ptr::null_mut(), + win32::FILE_BEGIN, + ) + }; + } + } +} + +/// A negative `off` reads at the file pointer. +pub fn pread(fd: Fd, buf: &mut [u8], off: i64) -> Maybe { + if off < 0 { + return read(fd, buf); + } + let _restore = RestoreFilePointer::new(fd); + read_at(fd, buf, Some(off as u64)) +} + +/// [`pread`] of a HANDLE that shares its file pointer with an fd JS can see: a +/// duplicate is the same file object. +pub fn pread_keeping_position(fd: Fd, buf: &mut [u8], off: u64) -> Maybe { + let _restore = RestoreFilePointer::new_if(fd, true); + read_at(fd, buf, Some(off)) +} + +/// `ReadFile` at `at`, or at the file pointer. A positioned read runs +/// under a [`RestoreFilePointer`] the caller holds. +fn read_at(fd: Fd, buf: &mut [u8], at: Option) -> Maybe { + let adjusted_len = buf.len().min(crate::MAX_COUNT) as u32; + // Stdin callers route through this function (via + // `File::stdin().read_to_end_into` / `output_sink().read`), so the + // OPERATION_ABORTED retry lives here. + loop { + let mut overlapped = at.map(overlapped_at); + let mut amount_read: u32 = 0; + // SAFETY: FFI; `buf` valid for `adjusted_len`, `overlapped` lives + // for the synchronous call (handle was not opened + // FILE_FLAG_OVERLAPPED). + let rc = unsafe { + win32::kernel32::ReadFile( + fd.native(), + buf.as_mut_ptr(), + adjusted_len, + &mut amount_read, + overlapped + .as_mut() + .map_or(core::ptr::null_mut(), |o| core::ptr::from_mut(o).cast()), + ) + }; + if rc == 0 { + let er = Win32Error::get(); + if er == Win32Error::OPERATION_ABORTED { + continue; + } + return match read_error(er, Tag::read) { + Some(err) => Err(err.with_fd(fd)), + None => Ok(0), + }; + } + return Ok(amount_read as usize); + } +} + +/// A negative `off` writes at the file pointer; see `pread`. +pub fn pwrite(fd: Fd, buf: &[u8], off: i64) -> Maybe { + if off < 0 { + return write(fd, buf); + } + let _restore = RestoreFilePointer::new(fd); + write_at(fd, buf, Some(off as u64)) +} + +/// `WriteFile` at `at`, or at the file pointer. A positioned write runs +/// under a [`RestoreFilePointer`] the caller holds. +fn write_at(fd: Fd, buf: &[u8], at: Option) -> Maybe { + let adjusted_len = buf.len().min(crate::MAX_COUNT) as u32; + let mut overlapped = at.map(overlapped_at); + let mut bytes_written: u32 = 0; + // SAFETY: FFI; `buf` valid for `adjusted_len`, `overlapped` lives for + // the synchronous call (handle was not opened FILE_FLAG_OVERLAPPED). + let rc = unsafe { + win32::kernel32::WriteFile( + fd.native(), + buf.as_ptr(), + adjusted_len, + &mut bytes_written, + overlapped + .as_mut() + .map_or(core::ptr::null_mut(), |o| core::ptr::from_mut(o).cast()), + ) + }; + if rc == 0 { + return Err(write_error(Win32Error::get(), Tag::write).with_fd(fd)); + } + Ok(bytes_written as usize) +} + +/// One `ReadFile` per buffer. `position < 0` reads at the file pointer. +/// A short read goes on to the next buffer, as libuv's `fs__read` does (on a +/// pipe that waits for more); a read of nothing ends it. An error after some +/// bytes were read reports those bytes instead. +pub fn preadv(fd: Fd, bufs: &[PlatformIoVec], position: i64) -> Maybe { + let _restore = (position >= 0).then(|| RestoreFilePointer::new(fd)); + let mut total: usize = 0; + for buf in bufs { + if buf.iov_len == 0 { + continue; + } + // SAFETY: a `PlatformIoVec` describes a writable buffer of `iov_len` + // bytes that the caller keeps alive for the call. + let slice: &mut [u8] = + unsafe { core::slice::from_raw_parts_mut(buf.iov_base.cast(), buf.iov_len) }; + let mut filled: usize = 0; + let ended = loop { + let at = (position >= 0).then(|| position as u64 + total as u64); + match read_at(fd, &mut slice[filled..], at) { + Ok(n) => { + total += n; + filled += n; + // `read_at` asks for at most `MAX_COUNT` bytes: a read of + // exactly that many goes on in the same buffer. + if n == crate::MAX_COUNT && filled < slice.len() { + continue; + } + break n == 0; + } + Err(_) if total > 0 => break true, + Err(e) => return Err(e), + } + }; + if ended { + break; + } + } + Ok(total) +} + +/// One `WriteFile` per buffer. `position < 0` writes at the file pointer. +pub fn pwritev(fd: Fd, bufs: &[PlatformIoVecConst], position: i64) -> Maybe { + let _restore = (position >= 0).then(|| RestoreFilePointer::new(fd)); + let mut total: usize = 0; + for buf in bufs { + if buf.len == 0 { + continue; + } + // SAFETY: a `PlatformIoVecConst` describes a readable buffer of `len` + // bytes that the caller keeps alive for the call. + let slice = unsafe { core::slice::from_raw_parts(buf.base, buf.len) }; + let at = (position >= 0).then(|| position as u64 + total as u64); + match write_at(fd, slice, at) { + Ok(n) => { + total += n; + if n < slice.len() { + break; + } + } + Err(_) if total > 0 => break, + Err(e) => return Err(e), + } + } + Ok(total) +} + +// ────────────────────────────────────────────────────────────────────────── +// stat / lstat / fstat +// ────────────────────────────────────────────────────────────────────────── + +pub fn stat(path: &ZStr) -> Maybe { + let result = stat_path(path.as_bytes(), false); + crate::syslog!( + "stat({}) = {:?}", + bstr::BStr::new(path.as_bytes()), + result.as_ref().err() + ); + result.map_err(|e| Error::from_win32(e, Tag::stat).with_path(path.as_bytes())) +} + +pub fn lstat(path: &ZStr) -> Maybe { + let result = stat_path(path.as_bytes(), true); + crate::syslog!( + "lstat({}) = {:?}", + bstr::BStr::new(path.as_bytes()), + result.as_ref().err() + ); + result.map_err(|e| Error::from_win32(e, Tag::lstat).with_path(path.as_bytes())) +} + +pub fn fstat(fd: Fd) -> Maybe { + let handle = handle_of(fd, Tag::fstat)?; + let result = fstat_handle(handle); + crate::syslog!("fstat({}) = {:?}", fd, result.as_ref().err()); + result.map_err(|e| Error::from_win32(e, Tag::fstat).with_fd(fd)) +} + +fn fstat_handle(handle: HANDLE) -> Win32Result { + // Pipes and consoles have nothing to query; set what can be known. + let synthetic = |mode: u32, device_type: u32| Stat { + st_mode: u64::from(mode), + st_nlink: 1, + st_rdev: u64::from(device_type) << 16, + st_ino: handle as usize as u64, + ..Default::default() + }; + match super::GetFileType(handle) { + super::FILE_TYPE_CHAR if is_console(handle) => { + Ok(synthetic(S::IFCHR, win32::FILE_DEVICE_CONSOLE)) + } + // A character device that is not a console (NUL, COM1) is statted + // like a disk file; `stat_handle` special-cases NUL. + super::FILE_TYPE_CHAR => stat_handle(handle, false), + // `GetFileType` has read the device type: it is not NUL's. + super::FILE_TYPE_DISK => stat_file_handle(handle, false), + super::FILE_TYPE_PIPE => Ok(synthetic(S::IFIFO, win32::FILE_DEVICE_NAMED_PIPE)), + _ => Err(Win32Error::INVALID_HANDLE), + } +} + +/// Whether `handle` is a console (input or screen buffer). +pub fn is_console(handle: HANDLE) -> bool { + let mut mode: u32 = 0; + win32::GetConsoleMode(handle, &mut mode) != 0 +} + +fn stat_path(path: &[u8], do_lstat: bool) -> Win32Result { + let mut wpath = WPath::new(path)?; + + // Strip one trailing slash, unless it follows a drive colon (`C:\`). + let units = wpath.units(); + let len = units.len(); + if len > 1 && units[len - 2] != b':' as u16 && is_sep_any_t(units[len - 1]) { + wpath.truncate(len - 1); + } + + match stat_wpath(&mut wpath, do_lstat) { + // A reparse point that is not a symlink is an ordinary file. + Err(Win32Error::SYMLINK_NOT_SUPPORTED | Win32Error::NOT_A_REPARSE_POINT) if do_lstat => { + stat_wpath(&mut wpath, false) + } + result => result, + } +} + +fn stat_wpath(wpath: &mut WPath, do_lstat: bool) -> Win32Result { + match stat_by_name(wpath, do_lstat) { + ByName::Done(stat) => return Ok(stat), + ByName::Failed(e) => return Err(e), + ByName::NeedsHandle => {} + } + + let mut flags = win32::FILE_FLAG_BACKUP_SEMANTICS; + if do_lstat { + flags |= win32::FILE_FLAG_OPEN_REPARSE_POINT; + } + match create_file( + wpath.as_ptr(), + win32::FILE_READ_ATTRIBUTES, + SHARE_ALL, + win32::OPEN_EXISTING, + flags, + ) { + Ok(handle) => stat_handle(handle.0, do_lstat), + // Not even FILE_READ_ATTRIBUTES is granted (`C:\pagefile.sys`, a + // file another process holds exclusively): the parent directory's + // listing still has most of the fields. + Err(e @ (Win32Error::ACCESS_DENIED | Win32Error::SHARING_VIOLATION)) => { + stat_from_directory_listing(wpath, do_lstat, e) + } + Err(e) => Err(e), + } +} + +enum ByName { + Done(Stat), + Failed(Win32Error), + NeedsHandle, +} + +/// `GetFileInformationByName` (Windows 11 24H2+) answers a stat without +/// opening a handle. +fn stat_by_name(wpath: &WPath, do_lstat: bool) -> ByName { + static GET_FILE_INFORMATION_BY_NAME: std::sync::OnceLock< + Option, + > = std::sync::OnceLock::new(); + let Some(get_file_information_by_name) = *GET_FILE_INFORMATION_BY_NAME.get_or_init(|| { + const MODULE: &[u16] = bun_core::w!("api-ms-win-core-file-l2-1-4.dll\0"); + // SAFETY: NUL-terminated literals; a null module handle is checked + // before use. The export has the `GetFileInformationByNameFn` signature. + unsafe { + let module = win32::kernel32::GetModuleHandleW(MODULE.as_ptr()); + if module.is_null() { + return None; + } + let proc = win32::GetProcAddress(module, c"GetFileInformationByName".as_ptr()); + if proc.is_null() { + return None; + } + Some(core::mem::transmute::< + *mut c_void, + win32::GetFileInformationByNameFn, + >(proc)) + } + }) else { + return ByName::NeedsHandle; + }; + + let mut info: win32::FILE_STAT_BASIC_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `wpath` is NUL-terminated; `info` is writable for its size. + let ok = unsafe { + get_file_information_by_name( + wpath.as_ptr(), + win32::FileStatBasicByNameInfo, + ptr::from_mut(&mut info).cast(), + core::mem::size_of::() as u32, + ) + }; + if ok == 0 { + return match Win32Error::get() { + // Opening a handle would fail the same way. + e @ (Win32Error::FILE_NOT_FOUND + | Win32Error::PATH_NOT_FOUND + | Win32Error::NOT_READY + | Win32Error::BAD_NET_NAME) => ByName::Failed(e), + _ => ByName::NeedsHandle, + }; + } + // Following a link, or measuring its target for `st_size`, needs a handle. + if info.FileAttributes & win32::FILE_ATTRIBUTE_REPARSE_POINT != 0 { + return ByName::NeedsHandle; + } + if info.DeviceType == win32::FILE_DEVICE_NULL { + return ByName::Done(null_device_stat()); + } + ByName::Done(stat_from_info(&info, do_lstat)) +} + +fn stat_handle(handle: HANDLE, do_lstat: bool) -> Win32Result { + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + + let mut device_info: win32::FILE_FS_DEVICE_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `handle` is live; the out-buffers are writable for their size. + let status = unsafe { + win32::ntdll::NtQueryVolumeInformationFile( + handle, + &mut io, + ptr::from_mut(&mut device_info).cast(), + core::mem::size_of::() as u32, + win32::FS_INFORMATION_CLASS::FileFsDeviceInformation, + ) + }; + if win32::NT_ERROR(status) { + return Err(Win32Error::from_ntstatus(status)); + } + if device_info.DeviceType == win32::FILE_DEVICE_NULL { + return Ok(null_device_stat()); + } + stat_file_handle(handle, do_lstat) +} + +/// [`stat_handle`] for a handle that is known not to be the null device's. +fn stat_file_handle(handle: HANDLE, do_lstat: bool) -> Win32Result { + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut file_info: win32::FILE_ALL_INFORMATION = bun_core::ffi::zeroed(); + // The file name does not fit the fixed-size struct, so the expected + // status is the STATUS_BUFFER_OVERFLOW warning, which NT_ERROR excludes. + // SAFETY: `handle` is live; the out-buffers are writable for their size. + let status = unsafe { + win32::ntdll::NtQueryInformationFile( + handle, + &mut io, + ptr::from_mut(&mut file_info).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileAllInformation, + ) + }; + if win32::NT_ERROR(status) { + return Err(Win32Error::from_ntstatus(status)); + } + + let volume_serial = volume_serial_number(handle)?; + + let attributes = file_info.BasicInformation.FileAttributes; + let end_of_file = if do_lstat && attributes & win32::FILE_ATTRIBUTE_REPARSE_POINT != 0 { + // `st_size` of a symlink is the length of its target. When the + // reparse point is not a symlink this fails and the caller retries + // without `do_lstat`. + let mut reparse = ReparseBuffer::new(); + bun_core::strings::element_length_utf16_into_utf8(reparse.read_link_target(handle)?) as i64 + } else { + file_info.StandardInformation.EndOfFile + }; + + Ok(stat_from_info( + &win32::FILE_STAT_BASIC_INFORMATION { + FileId: file_info.InternalInformation.IndexNumber, + CreationTime: file_info.BasicInformation.CreationTime, + LastAccessTime: file_info.BasicInformation.LastAccessTime, + LastWriteTime: file_info.BasicInformation.LastWriteTime, + ChangeTime: file_info.BasicInformation.ChangeTime, + AllocationSize: file_info.StandardInformation.AllocationSize, + EndOfFile: end_of_file, + FileAttributes: attributes, + NumberOfLinks: file_info.StandardInformation.NumberOfLinks, + VolumeSerialNumber: i64::from(volume_serial), + ..bun_core::ffi::zeroed() + }, + do_lstat, + )) +} + +/// 0 for a filesystem driver that does not implement the query. +fn volume_serial_number(handle: HANDLE) -> Win32Result { + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut volume_info: win32::FILE_FS_VOLUME_INFORMATION = bun_core::ffi::zeroed(); + // The volume label does not fit the fixed-size struct; see `stat_file_handle`. + // SAFETY: `handle` is live; the out-buffers are writable for their size. + let status = unsafe { + win32::ntdll::NtQueryVolumeInformationFile( + handle, + &mut io, + ptr::from_mut(&mut volume_info).cast(), + core::mem::size_of::() as u32, + win32::FS_INFORMATION_CLASS::FileFsVolumeInformation, + ) + }; + if status == NTSTATUS::NOT_IMPLEMENTED { + return Ok(0); + } + if win32::NT_ERROR(status) { + return Err(Win32Error::from_ntstatus(status)); + } + Ok(volume_info.VolumeSerialNumber) +} + +fn null_device_stat() -> Stat { + Stat { + st_mode: u64::from(S::IFCHR | 0o666), + st_nlink: 1, + st_blksize: 4096, + st_rdev: u64::from(win32::FILE_DEVICE_NULL) << 16, + ..Default::default() + } +} + +fn stat_from_info(info: &win32::FILE_STAT_BASIC_INFORMATION, do_lstat: bool) -> Stat { + let attributes = info.FileAttributes; + // Symlinks and junctions are the only reparse points treated as links, + // and only by lstat; callers have already established that this one is. + let (file_type, size) = if do_lstat && attributes & win32::FILE_ATTRIBUTE_REPARSE_POINT != 0 { + (S::IFLNK, info.EndOfFile as u64) + } else if attributes & win32::FILE_ATTRIBUTE_DIRECTORY != 0 { + (S::IFDIR, 0) + } else { + (S::IFREG, info.EndOfFile as u64) + }; + // The read-only attribute is the only permission there is to report; + // no execute bits, directories included. + let permissions = if attributes & win32::FILE_ATTRIBUTE_READONLY != 0 { + 0o444 + } else { + 0o666 + }; + Stat { + st_dev: u64::from(info.VolumeSerialNumber as u32), + st_mode: u64::from(file_type | permissions), + st_nlink: u64::from(info.NumberOfLinks), + st_ino: info.FileId as u64, + st_size: size, + // Querying the real sector size would cost another syscall. + st_blksize: 4096, + st_blocks: (info.AllocationSize as u64) >> 9, + atim: filetime_to_timespec(info.LastAccessTime), + mtim: filetime_to_timespec(info.LastWriteTime), + ctim: filetime_to_timespec(info.ChangeTime), + birthtim: filetime_to_timespec(info.CreationTime), + ..Default::default() + } +} + +/// Stat `wpath` from its parent directory's listing. `open_error` is why the +/// file itself could not be opened; it is returned when the listing cannot +/// answer either (a link that would have to be followed). +fn stat_from_directory_listing( + wpath: &mut WPath, + do_lstat: bool, + open_error: Win32Error, +) -> Win32Result { + let path = wpath.units_with_nul_mut(); + let len = path.len() - 1; + + // Find where the last component starts. + let mut split = len; + let mut includes_name = false; + while split > 0 && !is_sep_any_t(path[split - 1]) && path[split - 1] != b':' as u16 { + if path[split - 1] != b'.' as u16 { + includes_name = true; + } + split -= 1; + } + + const DOT: [u16; 2] = [b'.' as u16, 0]; + let mut root = [0u16; 8]; + // The separator that was overwritten with a NUL to terminate the + // directory part in place. + let mut restore: Option<(usize, u16)> = None; + let dir: *const u16 = if split == 0 && includes_name { + // A bare relative name. + DOT.as_ptr() + } else if split > 0 && is_sep_any_t(path[split - 1]) { + if !includes_name { + // `dir\`, `dir\..`: the whole path is the directory. + split = len; + path.as_ptr() + } else if split == 1 { + // `\name`: the separator alone is the current drive's root. + root[0] = path[0]; + root.as_ptr() + } else if (3..=7).contains(&split) && path[split - 2] == b':' as u16 { + // `X:\name`, `\\?\X:\name`: the root keeps its backslash. `X:` is + // the drive's current directory and `\\?\X:` the volume device. + root[..split].copy_from_slice(&path[..split]); + root.as_ptr() + } else { + restore = Some((split - 1, path[split - 1])); + path[split - 1] = 0; + path.as_ptr() + } + } else { + // `..`, `C:` + split = len; + path.as_ptr() + }; + + let result = (|| { + let name = &path[split..len]; + // These are wildcards to NtQueryDirectoryFile. + const WILDCARDS: [u16; 5] = [ + b'*' as u16, + b'?' as u16, + b'>' as u16, + b'<' as u16, + b'"' as u16, + ]; + if bun_core::strings::index_of_any16(name, &WILDCARDS).is_some() { + return Err(Win32Error::INVALID_NAME); + } + + let dir_handle = create_file( + dir, + win32::FILE_LIST_DIRECTORY, + SHARE_ALL, + win32::OPEN_EXISTING, + win32::FILE_FLAG_BACKUP_SEMANTICS, + )?; + + let Ok(name_bytes) = u16::try_from(name.len() * 2) else { + return Err(Win32Error::INVALID_PARAMETER); + }; + let mut file_mask = win32::UNICODE_STRING { + Length: name_bytes, + MaximumLength: name_bytes, + Buffer: name.as_ptr().cast_mut(), + }; + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut dir_info: win32::FILE_ID_FULL_DIR_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `dir_handle` is live; `dir_info` is writable for its size; + // `file_mask` is only read. + let status = unsafe { + win32::ntdll::NtQueryDirectoryFile( + dir_handle.0, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + &mut io, + ptr::from_mut(&mut dir_info).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileIdFullDirectoryInformation, + 1, + &mut file_mask, + 1, + ) + }; + // STATUS_BUFFER_OVERFLOW is success: only the file name did not fit. + if !win32::NT_SUCCESS(status) && status != NTSTATUS::BUFFER_OVERFLOW { + return Err(if status == NTSTATUS::NO_MORE_FILES { + Win32Error::PATH_NOT_FOUND + } else { + Win32Error::from_ntstatus(status) + }); + } + + let is_reparse_point = dir_info.FileAttributes & win32::FILE_ATTRIBUTE_REPARSE_POINT != 0; + if is_reparse_point && !do_lstat { + // Following the link needs the handle that could not be opened. + return Err(open_error); + } + + let volume_serial = volume_serial_number(dir_handle.0)?; + + Ok(stat_from_info( + &win32::FILE_STAT_BASIC_INFORMATION { + FileId: dir_info.FileId, + CreationTime: dir_info.CreationTime, + LastAccessTime: dir_info.LastAccessTime, + LastWriteTime: dir_info.LastWriteTime, + ChangeTime: dir_info.ChangeTime, + // A link's target length needs the handle too; report 0. + AllocationSize: if is_reparse_point { + 0 + } else { + dir_info.AllocationSize + }, + EndOfFile: if is_reparse_point { + 0 + } else { + dir_info.EndOfFile + }, + FileAttributes: dir_info.FileAttributes, + // Not part of a directory listing. + NumberOfLinks: 1, + VolumeSerialNumber: i64::from(volume_serial), + ..bun_core::ffi::zeroed() + }, + do_lstat, + )) + })(); + + if let Some((index, separator)) = restore { + path[index] = separator; + } + result +} + +// ────────────────────────────────────────────────────────────────────────── +// Reparse points +// ────────────────────────────────────────────────────────────────────────── + +/// Aligned storage for a `REPARSE_DATA_BUFFER` (the first +/// `MAXIMUM_REPARSE_DATA_BUFFER_SIZE` bytes of a pooled wide path buffer): +/// +/// ```text +/// 0 u32 ReparseTag +/// 4 u16 ReparseDataLength +/// 6 u16 Reserved +/// 8 symlink / mount point: u16 SubstituteNameOffset, SubstituteNameLength, +/// PrintNameOffset, PrintNameLength (bytes, relative +/// to PathBuffer) +/// 16 symlink: u32 Flags, then PathBuffer at 20 +/// 16 mount point: PathBuffer +/// 8 app exec link: u32 StringCount, then NUL-separated strings at 12 +/// ``` +struct ReparseBuffer(bun_paths::w_path_buffer_pool::Guard); + +const _: () = assert!(bun_paths::PATH_MAX_WIDE >= win32::MAXIMUM_REPARSE_DATA_BUFFER_SIZE / 2); + +impl ReparseBuffer { + fn new() -> Self { + Self(bun_paths::w_path_buffer_pool::get()) + } + + /// The path a symlink-like reparse point on `handle` points to, as the + /// caller of `readlink` should see it. `ERROR_SYMLINK_NOT_SUPPORTED` for + /// a reparse point that is not treated as a symlink. + fn read_link_target(&mut self, handle: HANDLE) -> Win32Result<&[u16]> { + let mut bytes: u32 = 0; + // SAFETY: `handle` is live; `self.0` is writable for its size. + let ok = unsafe { + win32::DeviceIoControl( + handle, + win32::FSCTL_GET_REPARSE_POINT, + ptr::null_mut(), + 0, + self.0.as_mut_ptr().cast(), + win32::MAXIMUM_REPARSE_DATA_BUFFER_SIZE as u32, + &mut bytes, + ptr::null_mut(), + ) + }; + if ok == 0 { + return Err(Win32Error::get()); + } + + const NOT_A_SYMLINK: Win32Error = Win32Error::SYMLINK_NOT_SUPPORTED; + let units = &mut self.0[..bytes as usize / 2]; + if units.len() < 8 { + return Err(NOT_A_SYMLINK); + } + let tag = u32::from(units[0]) | u32::from(units[1]) << 16; + let is_drive_path = |t: &[u16]| { + t.len() >= 2 + && is_drive_letter_t(t[0]) + && t[1] == b':' as u16 + && (t.len() == 2 || t[2] == b'\\' as u16) + }; + let has_nt_prefix = |t: &[u16]| t.len() >= 4 && t[..4] == super::NT_OBJECT_PREFIX; + + match tag { + win32::IO_REPARSE_TAG_SYMLINK | win32::IO_REPARSE_TAG_MOUNT_POINT => { + let path_buffer = if tag == win32::IO_REPARSE_TAG_SYMLINK { + 10 + } else { + 8 + }; + let start = path_buffer + usize::from(units[4]) / 2; + let end = start + usize::from(units[5]) / 2; + if end > units.len() { + return Err(Win32Error::INVALID_REPARSE_DATA); + } + let target = &mut units[start..end]; + + if tag == win32::IO_REPARSE_TAG_MOUNT_POINT { + // Only `\??\X:\…` junctions are links. A volume mount + // point (`\??\Volume{guid}\`) is not, as in Node. + if !(has_nt_prefix(target) && is_drive_path(&target[4..])) { + return Err(NOT_A_SYMLINK); + } + return Ok(&target[4..]); + } + + // Undo the NT namespacing CreateSymbolicLinkW applies to + // absolute targets. Any other `\??\` form was written by the + // link's creator and is returned as is. + if has_nt_prefix(target) { + if is_drive_path(&target[4..]) { + return Ok(&target[4..]); + } + let rest = &target[4..]; + if rest.len() >= 4 + && matches!(u8::try_from(rest[0]), Ok(b'U' | b'u')) + && matches!(u8::try_from(rest[1]), Ok(b'N' | b'n')) + && matches!(u8::try_from(rest[2]), Ok(b'C' | b'c')) + && rest[3] == b'\\' as u16 + { + // `\??\UNC\server\share` → `\\server\share` + target[6] = b'\\' as u16; + return Ok(&target[6..]); + } + } + Ok(target) + } + win32::IO_REPARSE_TAG_APPEXECLINK => { + // The target executable is the third NUL-separated string. + let string_count = u32::from(units[4]) | u32::from(units[5]) << 16; + if string_count < 3 { + return Err(NOT_A_SYMLINK); + } + let mut rest: &[u16] = &units[6..]; + for _ in 0..2 { + match bun_core::strings::index_of_any16(rest, &[0]) { + Some(len) if len > 0 => rest = &rest[len + 1..], + _ => return Err(NOT_A_SYMLINK), + } + } + let target = match bun_core::strings::index_of_any16(rest, &[0]) { + Some(len) => &rest[..len], + None => rest, + }; + if !(target.len() >= 3 && is_drive_path(target) && target[2] == b'\\' as u16) { + return Err(NOT_A_SYMLINK); + } + Ok(target) + } + _ => Err(NOT_A_SYMLINK), + } + } +} + +/// Writes the link target into `buf`, NUL-terminated; returns its length. +pub fn readlink(path: &ZStr, buf: &mut [u8]) -> Maybe { + let result = readlink_impl(path.as_bytes(), buf); + crate::syslog!( + "readlink({}) = {:?}", + bstr::BStr::new(path.as_bytes()), + result.as_ref().map(|&len| bstr::BStr::new(&buf[..len])) + ); + result.map_err(|errno| Error::from_code(errno, Tag::readlink).with_path(path.as_bytes())) +} + +fn readlink_impl(path: &[u8], buf: &mut [u8]) -> core::result::Result { + let win32_err = |e: Win32Error| match e { + Win32Error::NOT_A_REPARSE_POINT => E::EINVAL, + e => e.to_e(), + }; + let wpath = WPath::new(path).map_err(win32_err)?; + let handle = create_file( + wpath.as_ptr(), + 0, + 0, + win32::OPEN_EXISTING, + win32::FILE_FLAG_OPEN_REPARSE_POINT | win32::FILE_FLAG_BACKUP_SEMANTICS, + ) + .map_err(win32_err)?; + let mut reparse = ReparseBuffer::new(); + let target = reparse.read_link_target(handle.0).map_err(win32_err)?; + + // One byte is reserved for the NUL. + let Some(capacity) = buf.len().checked_sub(1) else { + return Err(E::ENAMETOOLONG); + }; + let encoded = bun_core::strings::copy_utf16_into_wtf8(&mut buf[..capacity], target); + if (encoded.read as usize) < target.len() { + return Err(E::ENAMETOOLONG); + } + let written = encoded.written as usize; + buf[written] = 0; + Ok(written) +} + +// ────────────────────────────────────────────────────────────────────────── +// unlink / rmdir / mkdir / rename / link +// ────────────────────────────────────────────────────────────────────────── + +pub fn unlink(path: &ZStr) -> Maybe<()> { + let result = unlink_or_rmdir(path.as_bytes(), false); + crate::syslog!( + "unlink({}) = {:?}", + bstr::BStr::new(path.as_bytes()), + result + ); + result.map_err(|errno| Error::from_code(errno, Tag::unlink).with_path(path.as_bytes())) +} + +pub fn rmdir(path: &ZStr) -> Maybe<()> { + let result = unlink_or_rmdir(path.as_bytes(), true); + crate::syslog!("rmdir({}) = {:?}", bstr::BStr::new(path.as_bytes()), result); + result.map_err(|errno| Error::from_code(errno, Tag::rmdir).with_path(path.as_bytes())) +} + +/// `FileBasicInformation.FileAttributes` of `handle`. +fn file_attributes(handle: HANDLE) -> Win32Result { + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut info: win32::FILE_BASIC_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `handle` is live; `info` is writable for its size. + let status = unsafe { + win32::ntdll::NtQueryInformationFile( + handle, + &mut io, + ptr::from_mut(&mut info).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileBasicInformation, + ) + }; + if !win32::NT_SUCCESS(status) { + return Err(Win32Error::from_ntstatus(status)); + } + Ok(info.FileAttributes) +} + +/// Opens the directory `path` for deletion, never following a link. The open +/// itself refuses what is not a directory (`ERROR_DIRECTORY`, which is +/// `ENOENT`: what Node on Windows reports for `rmdir(file)`, not `ENOTDIR`), +/// which `CreateFileW` has no flag for. +fn open_directory_for_delete(path: &[u8]) -> Win32Result { + // `CreateOptions` and `OBJECT_ATTRIBUTES.Attributes` bits (`wdm.h`). + const FILE_OPEN_FOR_BACKUP_INTENT: u32 = 0x0000_4000; + const OBJ_CASE_INSENSITIVE: u32 = 0x0000_0040; + + let wpath = WPath::nt(path)?; + let Ok(byte_len) = u16::try_from(wpath.len * 2) else { + return Err(Win32Error::FILENAME_EXCED_RANGE); + }; + let mut name = win32::UNICODE_STRING { + Length: byte_len, + MaximumLength: byte_len, + Buffer: wpath.as_ptr().cast_mut(), + }; + let mut attributes = win32::OBJECT_ATTRIBUTES { + Length: core::mem::size_of::() as u32, + RootDirectory: ptr::null_mut(), + ObjectName: &mut name, + Attributes: OBJ_CASE_INSENSITIVE, + SecurityDescriptor: ptr::null_mut(), + SecurityQualityOfService: ptr::null_mut(), + }; + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut handle: HANDLE = ptr::null_mut(); + // SAFETY: every pointer is to a live local; `name.Buffer` is only read. + let status = unsafe { + win32::ntdll::NtCreateFile( + &mut handle, + win32::FILE_READ_ATTRIBUTES | win32::DELETE | win32::SYNCHRONIZE, + &mut attributes, + &mut io, + ptr::null_mut(), + 0, + SHARE_ALL, + win32::FILE_OPEN, + win32::FILE_DIRECTORY_FILE + | win32::FILE_OPEN_REPARSE_POINT + | FILE_OPEN_FOR_BACKUP_INTENT + | win32::FILE_SYNCHRONOUS_IO_NONALERT, + ptr::null_mut(), + 0, + ) + }; + if !win32::NT_SUCCESS(status) { + return Err(Win32Error::from_ntstatus(status)); + } + Ok(OwnedHandle(handle)) +} + +/// A second handle to what `handle` is open on. `ReOpenFile` is this for a +/// file; it refuses a directory. +fn reopen(handle: HANDLE, access: u32) -> core::result::Result { + const FILE_OPEN_FOR_BACKUP_INTENT: u32 = 0x0000_4000; + let mut name = win32::UNICODE_STRING { + Length: 0, + MaximumLength: 0, + Buffer: ptr::null_mut(), + }; + let mut attributes = win32::OBJECT_ATTRIBUTES { + Length: core::mem::size_of::() as u32, + RootDirectory: handle, + ObjectName: &mut name, + Attributes: 0, + SecurityDescriptor: ptr::null_mut(), + SecurityQualityOfService: ptr::null_mut(), + }; + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut reopened: HANDLE = ptr::null_mut(); + // SAFETY: every pointer is to a live local; `handle` is live. + let status = unsafe { + win32::ntdll::NtCreateFile( + &mut reopened, + access | win32::SYNCHRONIZE, + &mut attributes, + &mut io, + ptr::null_mut(), + 0, + SHARE_ALL, + win32::FILE_OPEN, + win32::FILE_OPEN_REPARSE_POINT + | FILE_OPEN_FOR_BACKUP_INTENT + | win32::FILE_SYNCHRONOUS_IO_NONALERT, + ptr::null_mut(), + 0, + ) + }; + if !win32::NT_SUCCESS(status) { + return Err(status); + } + Ok(OwnedHandle(reopened)) +} + +/// Deletes the file or directory `handle` is open on with `DELETE` access. +pub(crate) fn delete_by_handle(handle: HANDLE) -> core::result::Result<(), win32::NTSTATUS> { + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + // POSIX delete: the name disappears at once even while other handles are + // open, and a read-only file needs no attribute change. + let mut disposition_ex = win32::FILE_DISPOSITION_INFORMATION_EX { + Flags: win32::FILE_DISPOSITION_DELETE + | win32::FILE_DISPOSITION_POSIX_SEMANTICS + | win32::FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE, + }; + // SAFETY: `handle` is live; the info struct matches the class. + let status = unsafe { + win32::ntdll::NtSetInformationFile( + handle, + &mut io, + ptr::from_mut(&mut disposition_ex).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileDispositionInformationEx, + ) + }; + if win32::NT_SUCCESS(status) { + return Ok(()); + } + // The errors libuv takes to mean that the file system (FAT, exFAT, some + // redirectors) has no POSIX delete. + if !matches!( + Win32Error::from_ntstatus(status), + Win32Error::NOT_SUPPORTED | Win32Error::INVALID_PARAMETER | Win32Error::INVALID_FUNCTION + ) { + return Err(status); + } + + let mut classic_delete = || { + let mut disposition = win32::FILE_DISPOSITION_INFORMATION { DeleteFile: 1 }; + // SAFETY: `handle` is live; the info struct matches the class. + unsafe { + win32::ntdll::NtSetInformationFile( + handle, + &mut io, + ptr::from_mut(&mut disposition).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileDispositionInformation, + ) + } + }; + let status = classic_delete(); + if win32::NT_SUCCESS(status) { + return Ok(()); + } + if status != win32::NTSTATUS::CANNOT_DELETE { + return Err(status); + } + + // What a classic delete says of something read-only, among others. + let attributes_handle = reopen( + handle, + win32::FILE_READ_ATTRIBUTES | win32::FILE_WRITE_ATTRIBUTES, + )?; + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut basic: win32::FILE_BASIC_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `attributes_handle` is live; `basic` is writable for its size. + let queried = unsafe { + win32::ntdll::NtQueryInformationFile( + attributes_handle.0, + &mut io, + ptr::from_mut(&mut basic).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileBasicInformation, + ) + }; + if !win32::NT_SUCCESS(queried) || basic.FileAttributes & win32::FILE_ATTRIBUTE_READONLY == 0 { + return Err(status); + } + let found = basic.FileAttributes; + let mut set_attributes = |attributes: u32| { + // Zeroed times are left as they are; zeroed attributes would be too. + let mut basic: win32::FILE_BASIC_INFORMATION = bun_core::ffi::zeroed(); + basic.FileAttributes = if attributes == 0 { + win32::FILE_ATTRIBUTE_NORMAL + } else { + attributes + }; + // SAFETY: `attributes_handle` is live; the info struct matches the class. + unsafe { + win32::ntdll::NtSetInformationFile( + attributes_handle.0, + &mut io, + ptr::from_mut(&mut basic).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileBasicInformation, + ) + } + }; + let cleared = set_attributes(found & !win32::FILE_ATTRIBUTE_READONLY); + if !win32::NT_SUCCESS(cleared) { + return Err(cleared); + } + let status = classic_delete(); + if win32::NT_SUCCESS(status) { + return Ok(()); + } + // It stays (a directory that is not empty), so it stays as it was found. + let _ = set_attributes(found); + Err(status) +} + +fn unlink_or_rmdir(path: &[u8], is_rmdir: bool) -> core::result::Result<(), E> { + let to_e = |e: Win32Error| e.to_e(); + let handle = if is_rmdir { + open_directory_for_delete(path).map_err(to_e)? + } else { + let wpath = WPath::new(path).map_err(to_e)?; + // Never follows a link: the link itself is what gets removed. + let handle = create_file( + wpath.as_ptr(), + win32::FILE_READ_ATTRIBUTES | win32::DELETE, + SHARE_ALL, + win32::OPEN_EXISTING, + win32::FILE_FLAG_OPEN_REPARSE_POINT | win32::FILE_FLAG_BACKUP_SEMANTICS, + ) + .map_err(to_e)?; + let attributes = file_attributes(handle.0).map_err(to_e)?; + if attributes & win32::FILE_ATTRIBUTE_DIRECTORY != 0 { + // POSIX wants EPERM for unlink(directory). A directory symlink or + // junction is a link, though, and unlink removes those. + if attributes & win32::FILE_ATTRIBUTE_REPARSE_POINT == 0 { + return Err(Win32Error::ACCESS_DENIED.to_e()); + } + let mut reparse = ReparseBuffer::new(); + if let Err(e) = reparse.read_link_target(handle.0) { + return Err(match e { + Win32Error::SYMLINK_NOT_SUPPORTED => Win32Error::ACCESS_DENIED.to_e(), + e => e.to_e(), + }); + } + } + handle + }; + delete_by_handle(handle.0).map_err(|status| Win32Error::from_ntstatus(status).to_e()) +} + +/// `mode` is ignored: a Windows directory has no permission bits to set. +pub fn mkdir(path: &ZStr, _mode: Mode) -> Maybe<()> { + // libuv's mapping of what `CreateDirectoryW` reports; a path that does + // not convert is `ENOENT` as for every other call. + let result = match WPath::new(path.as_bytes()) { + Err(e) => Err(e.to_e()), + // SAFETY: `wpath` is NUL-terminated. + Ok(wpath) if unsafe { win32::CreateDirectoryW(wpath.as_ptr(), ptr::null_mut()) } == 0 => { + Err(match Win32Error::get() { + Win32Error::INVALID_NAME | Win32Error::DIRECTORY => E::EINVAL, + e => e.to_e(), + }) + } + Ok(_) => Ok(()), + }; + crate::syslog!("mkdir({}) = {:?}", bstr::BStr::new(path.as_bytes()), result); + result.map_err(|errno| Error::from_code(errno, Tag::mkdir).with_path(path.as_bytes())) +} + +/// `mkdtemp(3)`: replaces the trailing `XXXXXX` of `template` in place and +/// creates that directory. +pub fn mkdtemp(template: &mut [u8]) -> Maybe<()> { + const CHARS: &[u8; 62] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + const NUM_X: usize = 6; + // The CRT's `TMP_MAX`. + const TRIES: u32 = i32::MAX as u32; + + let len = template.len(); + if len < NUM_X || &template[len - NUM_X..] != b"XXXXXX" { + return Err(Error::from_code(E::EINVAL, Tag::mkdtemp).with_path(template)); + } + let mut wpath = match WPath::new(template) { + Ok(wpath) => wpath, + Err(e) => return Err(Error::from_win32(e, Tag::mkdtemp).with_path(template)), + }; + let wide_len = wpath.len; + // A template that does not convert comes back empty. + if wide_len < NUM_X { + return Err(Error::from_code(E::EINVAL, Tag::mkdtemp).with_path(template)); + } + let mut last = Win32Error::ALREADY_EXISTS; + for _ in 0..TRIES { + // Not `fast_random`: every thread's generator starts from one seed. + let mut v = [0u8; 8]; + bun_core::os_entropy(&mut v); + let mut v = u64::from_ne_bytes(v); + let mut name = [0u8; NUM_X]; + for c in &mut name { + *c = CHARS[(v % CHARS.len() as u64) as usize]; + v /= CHARS.len() as u64; + } + let units = wpath.units_with_nul_mut(); + for (dst, src) in units[wide_len - NUM_X..wide_len].iter_mut().zip(name) { + *dst = u16::from(src); + } + // SAFETY: `wpath` is NUL-terminated. + if unsafe { win32::CreateDirectoryW(wpath.as_ptr(), ptr::null_mut()) } != 0 { + template[len - NUM_X..].copy_from_slice(&name); + return Ok(()); + } + last = Win32Error::get(); + if last != Win32Error::ALREADY_EXISTS { + break; + } + } + Err(Error::from_win32(last, Tag::mkdtemp).with_path(template)) +} + +/// `realpath(3)` over `GetFinalPathNameByHandleW`. The file is opened with no +/// access rights, so neither its ACL's read permission nor another open's +/// share mode can make this fail. +pub fn realpath<'a>(path: &ZStr, buf: &'a mut bun_paths::PathBuffer) -> Maybe<&'a [u8]> { + let to_error = |e: Win32Error| Error::from_win32(e, Tag::realpath).with_path(path.as_bytes()); + let wpath = WPath::new(path.as_bytes()).map_err(to_error)?; + let handle = create_file( + wpath.as_ptr(), + 0, + 0, + win32::OPEN_EXISTING, + win32::FILE_ATTRIBUTE_NORMAL | win32::FILE_FLAG_BACKUP_SEMANTICS, + ) + .map_err(to_error)?; + let mut wide = bun_paths::w_path_buffer_pool::get(); + let resolved = super::GetFinalPathNameByHandle(handle.0, Default::default(), &mut wide[..]) + .map_err(|e| match e { + super::GetFinalPathNameByHandleError::NameTooLong => { + Error::from_code(E::ENAMETOOLONG, Tag::realpath).with_path(path.as_bytes()) + } + // A volume that cannot answer is `EISDIR`, as in Node. + super::GetFinalPathNameByHandleError::Failed(e) => to_error(e), + })?; + let resolved = bun_core::strings::trim_prefix_comptime(resolved, &super::LONG_PATH_PREFIX); + let last = buf.0.len() - 1; + let len = + bun_core::strings::copy_utf16_into_wtf8(&mut buf.0[..last], resolved).written as usize; + buf.0[len] = 0; + Ok(&buf.0[..len]) +} + +/// `MoveFileExW(MOVEFILE_REPLACE_EXISTING)`: no cross-volume copy, and a +/// destination that is open elsewhere fails with `EPERM`/`EBUSY` (the errors +/// graceful-fs and friends retry on). +pub fn rename(from: &ZStr, to: &ZStr) -> Maybe<()> { + let result = (|| { + let from = WPath::new(from.as_bytes())?; + let to = WPath::new(to.as_bytes())?; + // SAFETY: both paths are NUL-terminated. + if unsafe { + super::kernel32::MoveFileExW( + from.as_ptr(), + to.as_ptr(), + super::MOVEFILE_REPLACE_EXISTING, + ) + } == 0 + { + return Err(Win32Error::get()); + } + Ok(()) + })(); + crate::syslog!( + "rename({}, {}) = {:?}", + bstr::BStr::new(from.as_bytes()), + bstr::BStr::new(to.as_bytes()), + result + ); + result.map_err(|e| Error::from_win32(e, Tag::rename)) +} + +/// Hard link `to` → existing file `from`. +pub fn link(from: &ZStr, to: &ZStr) -> Maybe<()> { + let result = (|| { + let existing = WPath::new(from.as_bytes())?; + let new = WPath::new(to.as_bytes())?; + if super::CreateHardLinkW(new.as_ptr(), existing.as_ptr(), None) == 0 { + return Err(Win32Error::get()); + } + Ok(()) + })(); + result + .map_err(|e| Error::from_win32(e, Tag::link).with_path_dest(from.as_bytes(), to.as_bytes())) +} + +// ────────────────────────────────────────────────────────────────────────── +// symlink / junction +// ────────────────────────────────────────────────────────────────────────── + +/// `CreateSymbolicLinkW`. The caller says whether the target is a directory; +/// Windows does not find out for itself. +pub(crate) fn create_symbolic_link( + link: *const u16, + target: *const u16, + directory: bool, +) -> Win32Result<()> { + let flags = win32::SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE + | if directory { + win32::SYMBOLIC_LINK_FLAG_DIRECTORY + } else { + 0 + }; + // SAFETY: the caller passes NUL-terminated wide strings. + if unsafe { win32::CreateSymbolicLinkW(link, target, flags) } == 0 { + return Err(Win32Error::get()); + } + Ok(()) +} + +fn symlink_impl(target: &ZStr, link: &ZStr, directory: bool) -> Maybe<()> { + let result = (|| { + let target = WPath::verbatim(target.as_bytes())?; + let link = WPath::new(link.as_bytes())?; + create_symbolic_link(link.as_ptr(), target.as_ptr(), directory) + })(); + crate::syslog!( + "symlink({}, {}) = {:?}", + bstr::BStr::new(target.as_bytes()), + bstr::BStr::new(link.as_bytes()), + result + ); + result.map_err(|e| Error::from_win32(e, Tag::symlink)) +} + +/// A symlink to a file. +pub fn symlink(target: &ZStr, link: &ZStr) -> Maybe<()> { + symlink_impl(target, link, false) +} + +/// A symlink to a directory. +pub fn symlink_dir(target: &ZStr, link: &ZStr) -> Maybe<()> { + symlink_impl(target, link, true) +} + +/// A junction at `link` pointing to the directory `target`, which must be an +/// absolute drive path (optionally `\\?\`-prefixed). Needs no privilege. +pub fn junction(target: &ZStr, link: &ZStr) -> Maybe<()> { + let result = junction_impl(target.as_bytes(), link.as_bytes()); + crate::syslog!( + "junction({}, {}) = {:?}", + bstr::BStr::new(target.as_bytes()), + bstr::BStr::new(link.as_bytes()), + result + ); + result.map_err(|errno| Error::from_code(errno, Tag::symlink)) +} + +fn junction_impl(target: &[u8], link: &[u8]) -> core::result::Result<(), E> { + let to_e = |e: Win32Error| e.to_e(); + let target = WPath::verbatim(target).map_err(to_e)?; + let target = target.units(); + + let is_long_path = target.len() >= 4 && target[..4] == super::LONG_PATH_PREFIX; + let is_absolute = is_long_path + || (target.len() >= 3 + && is_drive_letter_t(target[0]) + && target[1] == b':' as u16 + && is_sep_any_t(target[2])); + if !is_absolute { + return Err(E::EINVAL); + } + let target = if is_long_path { &target[4..] } else { target }; + + // `REPARSE_DATA_BUFFER` header (8 bytes), mount point header (8 bytes), + // then `\??\` + target + NUL (substitute name) and target + `\` + NUL + // (print name). See `ReparseBuffer` for the layout. + const HEADER_UNITS: usize = 8; + let mut buffer = vec![0u16; HEADER_UNITS + 4 + 2 * (target.len() + 2)]; + let mut at = HEADER_UNITS; + + // Appends `target` with each run of slashes collapsed into one `\`; + // returns whether it ended in a slash that has not been written yet. + let append_target = |buffer: &mut [u16], at: &mut usize| { + let mut pending_slash = false; + for &c in target { + if is_sep_any_t(c) { + pending_slash = true; + continue; + } + if pending_slash { + buffer[*at] = b'\\' as u16; + *at += 1; + pending_slash = false; + } + buffer[*at] = c; + *at += 1; + } + pending_slash + }; + + let substitute_start = at; + buffer[at..at + 4].copy_from_slice(&super::NT_OBJECT_PREFIX); + at += 4; + if append_target(&mut buffer, &mut at) { + buffer[at] = b'\\' as u16; + at += 1; + } + let substitute_len = at - substitute_start; + at += 1; // NUL + + let print_start = at; + let trailing_slash = append_target(&mut buffer, &mut at); + // A bare `X:` is printed as the root, `X:\`. + if trailing_slash || at - print_start == 2 { + buffer[at] = b'\\' as u16; + at += 1; + } + let print_len = at - print_start; + at += 1; // NUL + + let used_bytes = at * 2; + buffer[0] = win32::IO_REPARSE_TAG_MOUNT_POINT as u16; + buffer[1] = (win32::IO_REPARSE_TAG_MOUNT_POINT >> 16) as u16; + let Ok(data_len) = u16::try_from(used_bytes - 8) else { + return Err(E::ENAMETOOLONG); + }; + buffer[2] = data_len; + buffer[4] = ((substitute_start - HEADER_UNITS) * 2) as u16; + buffer[5] = (substitute_len * 2) as u16; + buffer[6] = ((print_start - HEADER_UNITS) * 2) as u16; + buffer[7] = (print_len * 2) as u16; + + let link = WPath::new(link).map_err(to_e)?; + // SAFETY: `link` is NUL-terminated. + if unsafe { win32::CreateDirectoryW(link.as_ptr(), ptr::null_mut()) } == 0 { + return Err(Win32Error::get().to_e()); + } + let set_reparse_point = || { + let handle = create_file( + link.as_ptr(), + win32::GENERIC_WRITE, + 0, + win32::OPEN_EXISTING, + win32::FILE_FLAG_BACKUP_SEMANTICS | win32::FILE_FLAG_OPEN_REPARSE_POINT, + )?; + let mut bytes: u32 = 0; + // SAFETY: `handle` is live; `buffer` is readable for `used_bytes`. + let ok = unsafe { + win32::DeviceIoControl( + handle.0, + win32::FSCTL_SET_REPARSE_POINT, + buffer.as_ptr().cast_mut().cast(), + used_bytes as u32, + ptr::null_mut(), + 0, + &mut bytes, + ptr::null_mut(), + ) + }; + if ok == 0 { + return Err(Win32Error::get()); + } + Ok(()) + }; + set_reparse_point().map_err(|e| { + // SAFETY: `link` is NUL-terminated. Undo the directory made above. + unsafe { win32::RemoveDirectoryW(link.as_ptr()) }; + e.to_e() + }) +} + +// ────────────────────────────────────────────────────────────────────────── +// chmod / fchmod / chown +// ────────────────────────────────────────────────────────────────────────── + +/// The owner-write bit decides the read-only attribute; nothing else in +/// `mode` has a Windows equivalent. +pub fn chmod(path: &ZStr, mode: Mode) -> Maybe<()> { + let result = (|| { + let wpath = WPath::new(path.as_bytes())?; + // SAFETY: `wpath` is NUL-terminated. + let attributes = unsafe { win32::GetFileAttributesW(wpath.as_ptr()) }; + if attributes == super::INVALID_FILE_ATTRIBUTES { + return Err(Win32Error::get()); + } + let attributes = if mode & S::IWUSR != 0 { + attributes & !win32::FILE_ATTRIBUTE_READONLY + } else { + attributes | win32::FILE_ATTRIBUTE_READONLY + }; + // SAFETY: `wpath` is NUL-terminated. + if unsafe { win32::SetFileAttributesW(wpath.as_ptr(), attributes) } == 0 { + return Err(Win32Error::get()); + } + Ok(()) + })(); + crate::syslog!( + "chmod({}, {:#o}) = {:?}", + bstr::BStr::new(path.as_bytes()), + mode, + result + ); + result.map_err(|e| Error::from_win32(e, Tag::chmod).with_path(path.as_bytes())) +} + +pub fn fchmod(fd: Fd, mode: Mode) -> Maybe<()> { + let handle = handle_of(fd, Tag::fchmod)?; + let result = fchmod_handle(handle, mode); + crate::syslog!("fchmod({}, {:#o}) = {:?}", fd, mode, result); + result.map_err(|e| Error::from_win32(e, Tag::fchmod).with_fd(fd)) +} + +fn fchmod_handle(handle: HANDLE, mode: Mode) -> Win32Result<()> { + // The caller's handle may lack FILE_WRITE_ATTRIBUTES. + // SAFETY: `handle` is live. + let reopened = unsafe { win32::ReOpenFile(handle, win32::FILE_WRITE_ATTRIBUTES, 0, 0) }; + if reopened == INVALID_HANDLE_VALUE { + return Err(Win32Error::get()); + } + let reopened = OwnedHandle(reopened); + + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut info: win32::FILE_BASIC_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `reopened` is live; `info` is writable for its size. + let status = unsafe { + win32::ntdll::NtQueryInformationFile( + reopened.0, + &mut io, + ptr::from_mut(&mut info).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileBasicInformation, + ) + }; + if !win32::NT_SUCCESS(status) { + return Err(Win32Error::from_ntstatus(status)); + } + + if mode & S::IWUSR != 0 { + info.FileAttributes &= !win32::FILE_ATTRIBUTE_READONLY; + } else { + info.FileAttributes |= win32::FILE_ATTRIBUTE_READONLY; + } + if info.FileAttributes == 0 { + // 0 means "leave unchanged". + info.FileAttributes = win32::FILE_ATTRIBUTE_NORMAL; + } + // SAFETY: `reopened` is live; the info struct matches the class. + let status = unsafe { + win32::ntdll::NtSetInformationFile( + reopened.0, + &mut io, + ptr::from_mut(&mut info).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileBasicInformation, + ) + }; + if !win32::NT_SUCCESS(status) { + return Err(Win32Error::from_ntstatus(status)); + } + Ok(()) +} + +/// Windows files have no POSIX owner: succeeds without doing anything, as in +/// Node. +pub fn chown(_path: &ZStr, _uid: u32, _gid: u32) -> Maybe<()> { + Ok(()) +} + +/// See [`chown`]. +pub fn lchown(_path: &ZStr, _uid: u32, _gid: u32) -> Maybe<()> { + Ok(()) +} + +/// See [`chown`]. +pub fn fchown(_fd: Fd, _uid: u32, _gid: u32) -> Maybe<()> { + Ok(()) +} + +// ────────────────────────────────────────────────────────────────────────── +// statfs +// ────────────────────────────────────────────────────────────────────────── + +pub fn statfs(path: &ZStr) -> Maybe { + let result = statfs_impl(path.as_bytes()); + crate::syslog!( + "statfs({}) = {:?}", + bstr::BStr::new(path.as_bytes()), + result.as_ref().err() + ); + result.map_err(|e| Error::from_win32(e, Tag::statfs).with_path(path.as_bytes())) +} + +fn statfs_impl(path: &[u8]) -> Win32Result { + let wpath = WPath::new(path)?; + let handle = create_file( + wpath.as_ptr(), + win32::FILE_READ_ATTRIBUTES, + SHARE_ALL, + win32::OPEN_EXISTING, + win32::FILE_FLAG_BACKUP_SEMANTICS, + )?; + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut info: win32::FILE_FS_FULL_SIZE_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `handle` is live; `info` is writable for its size. + let status = unsafe { + win32::ntdll::NtQueryVolumeInformationFile( + handle.0, + &mut io, + ptr::from_mut(&mut info).cast(), + core::mem::size_of::() as u32, + win32::FS_INFORMATION_CLASS::FileFsFullSizeInformation, + ) + }; + if win32::NT_ERROR(status) { + return Err(Win32Error::from_ntstatus(status)); + } + Ok(StatFS { + f_type: 0, + f_bsize: u64::from(info.SectorsPerAllocationUnit) * u64::from(info.BytesPerSector), + f_blocks: info.TotalAllocationUnits as u64, + f_bfree: info.ActualAvailableAllocationUnits as u64, + f_bavail: info.CallerAvailableAllocationUnits as u64, + f_files: 0, + f_ffree: 0, + }) +} + +// ────────────────────────────────────────────────────────────────────────── +// utimes +// ────────────────────────────────────────────────────────────────────────── + +/// `None` leaves the timestamp unchanged. A time outside what a FILETIME can +/// hold is `ERROR_INVALID_PARAMETER`, the error `SetFileTime` has for one. +fn time_like_to_filetime( + time: TimeLike, + now: &mut Option, +) -> Win32Result> { + if time.nsec == crate::UTIME_OMIT { + return Ok(None); + } + if time.nsec == crate::UTIME_NOW { + return Ok(Some(*now.get_or_insert_with(|| { + let mut ft = win32::FILETIME { + dwLowDateTime: 0, + dwHighDateTime: 0, + }; + // SAFETY: `ft` is a valid out-pointer. + unsafe { win32::GetSystemTimeAsFileTime(&mut ft) }; + ft + }))); + } + // Negative values are not times to `SetFileTime`: -1 and -2 switch the + // handle's own timestamp updates off and on, the rest are rejected. + let ticks = time + .sec + .checked_mul(TICKS_PER_SEC) + .and_then(|ticks| ticks.checked_add(time.nsec / 100)) + .and_then(|ticks| ticks.checked_add(EPOCH_DIFFERENCE_100NS)) + .and_then(|ticks| u64::try_from(ticks).ok()) + .ok_or(Win32Error::INVALID_PARAMETER)?; + Ok(Some(win32::FILETIME { + dwLowDateTime: ticks as u32, + dwHighDateTime: (ticks >> 32) as u32, + })) +} + +/// Sets the access and modification times; the creation time is left alone. +fn utime_handle(handle: HANDLE, atime: TimeLike, mtime: TimeLike) -> Win32Result<()> { + let mut now = None; + let atime = time_like_to_filetime(atime, &mut now)?; + let mtime = time_like_to_filetime(mtime, &mut now)?; + // SAFETY: `handle` is live; each pointer is null or a valid FILETIME. + if unsafe { + win32::SetFileTime( + handle, + ptr::null(), + atime.as_ref().map_or(ptr::null(), ptr::from_ref), + mtime.as_ref().map_or(ptr::null(), ptr::from_ref), + ) + } == 0 + { + return Err(Win32Error::get()); + } + Ok(()) +} + +fn utime_path(path: &[u8], atime: TimeLike, mtime: TimeLike, no_follow: bool) -> Win32Result<()> { + let wpath = WPath::new(path)?; + let mut flags = win32::FILE_FLAG_BACKUP_SEMANTICS; + if no_follow { + flags |= win32::FILE_FLAG_OPEN_REPARSE_POINT; + } + let handle = create_file( + wpath.as_ptr(), + win32::FILE_WRITE_ATTRIBUTES, + SHARE_ALL, + win32::OPEN_EXISTING, + flags, + )?; + utime_handle(handle.0, atime, mtime) +} + +pub fn utimens(path: &ZStr, atime: TimeLike, mtime: TimeLike) -> Maybe<()> { + utime_path(path.as_bytes(), atime, mtime, false) + .map_err(|e| Error::from_win32(e, Tag::utime).with_path(path.as_bytes())) +} + +/// Like [`utimens`], but sets the times of a symlink instead of its target. +pub fn lutimens(path: &ZStr, atime: TimeLike, mtime: TimeLike) -> Maybe<()> { + utime_path(path.as_bytes(), atime, mtime, true) + .map_err(|e| Error::from_win32(e, Tag::lutime).with_path(path.as_bytes())) +} + +pub fn futimens(fd: Fd, atime: TimeLike, mtime: TimeLike) -> Maybe<()> { + let handle = handle_of(fd, Tag::futime)?; + utime_handle(handle, atime, mtime).map_err(|e| Error::from_win32(e, Tag::futime).with_fd(fd)) +} + +// ────────────────────────────────────────────────────────────────────────── +// fsync / ftruncate / isatty / pipe +// ────────────────────────────────────────────────────────────────────────── + +fn flush(fd: Fd, tag: Tag) -> Maybe<()> { + let handle = handle_of(fd, tag)?; + // SAFETY: `handle` is a HANDLE value; a stale one fails with + // ERROR_INVALID_HANDLE. + if unsafe { win32::kernel32::FlushFileBuffers(handle) } == 0 { + return Err(Error::from_win32(Win32Error::get(), tag).with_fd(fd)); + } + Ok(()) +} + +pub fn fsync(fd: Fd) -> Maybe<()> { + flush(fd, Tag::fsync) +} + +/// Windows has no data-only flush; same as [`fsync`]. +pub fn fdatasync(fd: Fd) -> Maybe<()> { + flush(fd, Tag::fdatasync) +} + +/// Does not move the file pointer. +pub fn ftruncate(fd: Fd, len: i64) -> Maybe<()> { + let handle = handle_of(fd, Tag::ftruncate)?; + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut eof = win32::FILE_END_OF_FILE_INFORMATION { EndOfFile: len }; + // SAFETY: `handle` is a HANDLE value; the info struct matches the class. + let status = unsafe { + win32::ntdll::NtSetInformationFile( + handle, + &mut io, + ptr::from_mut(&mut eof).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileEndOfFileInformation, + ) + }; + if !win32::NT_SUCCESS(status) { + return Err( + Error::from_win32(Win32Error::from_ntstatus(status), Tag::ftruncate).with_fd(fd), + ); + } + Ok(()) +} + +pub fn isatty(fd: Fd) -> bool { + let handle = fd.native(); + handle != INVALID_HANDLE_VALUE + && super::GetFileType(handle) == super::FILE_TYPE_CHAR + && is_console(handle) +} + +/// A pipe as `[read end, write end]`: HANDLE-kind, byte-mode, blocking +/// (not overlapped) and non-inheritable. +pub fn pipe() -> Maybe<[Fd; 2]> { + pipe_impl().map_err(|e| Error::from_win32(e, Tag::pipe)) +} + +/// The `` of the next `bun\-` pipe name. One sequence for every +/// pipe this process names, so that two creators cannot pick the same. +pub fn next_pipe_serial() -> u64 { + use core::sync::atomic::AtomicU64; + static SERIAL: AtomicU64 = AtomicU64::new(0); + SERIAL.fetch_add(1, Ordering::Relaxed) +} + +fn pipe_impl() -> Win32Result<[Fd; 2]> { + use std::io::Write as _; + + let mut wide = [0u16; 97]; + let read_end = loop { + // An AppContainer process may only create pipes under `LOCAL\`. + let mut name = [0u8; 96]; + let name_len = { + let mut cursor = std::io::Cursor::new(&mut name[..]); + let _ = write!( + cursor, + "\\\\?\\pipe\\{}bun\\{}-{}", + if super::is_app_container() { + "LOCAL\\" + } else { + "" + }, + super::GetCurrentProcessId(), + next_pipe_serial(), + ); + cursor.position() as usize + }; + for (dst, src) in wide.iter_mut().zip(&name[..name_len]) { + *dst = u16::from(*src); + } + wide[name_len] = 0; + + // The read end is the server so that both ends get FILE_READ_ATTRIBUTES. + // SAFETY: `wide` is NUL-terminated. + let read_end = unsafe { + win32::kernel32::CreateNamedPipeW( + wide.as_ptr(), + win32::PIPE_ACCESS_INBOUND + | win32::WRITE_DAC + | win32::FILE_FLAG_FIRST_PIPE_INSTANCE, + win32::PIPE_TYPE_BYTE + | win32::PIPE_READMODE_BYTE + | win32::PIPE_WAIT + | win32::PIPE_REJECT_REMOTE_CLIENTS, + 1, + 65536, + 65536, + 0, + ptr::null_mut(), + ) + }; + if read_end != INVALID_HANDLE_VALUE { + break read_end; + } + match Win32Error::get() { + // The name is somebody else's: FILE_FLAG_FIRST_PIPE_INSTANCE + // refuses to join their pipe. Try the next one. + Win32Error::PIPE_BUSY | Win32Error::ACCESS_DENIED => {} + err => return Err(err), + } + }; + let read_end = OwnedHandle(read_end); + + let write_end = create_file( + wide.as_ptr(), + win32::GENERIC_WRITE | win32::FILE_READ_ATTRIBUTES | win32::WRITE_DAC, + 0, + win32::OPEN_EXISTING, + 0, + )?; + + // Both ends exist, so this does not block. + // SAFETY: `read_end` is live; a null OVERLAPPED is valid for a + // synchronous handle. + if unsafe { win32::ConnectNamedPipe(read_end.0, ptr::null_mut()) } == 0 { + let error = Win32Error::get(); + if error != Win32Error::PIPE_CONNECTED { + return Err(error); + } + } + + let fds = [Fd::from_system(read_end.0), Fd::from_system(write_end.0)]; + core::mem::forget(read_end); + core::mem::forget(write_end); + Ok(fds) +} diff --git a/src/sys/windows/mod.rs b/src/sys/windows/mod.rs index d2eac80990ea..39863710fd24 100644 --- a/src/sys/windows/mod.rs +++ b/src/sys/windows/mod.rs @@ -22,10 +22,9 @@ pub use bun_windows_sys::kernel32::GetLastError; pub use bun_windows_sys::ntdll; pub use bun_windows_sys::ws2_32; -/// Re-exports the tier-0 `bun_windows_sys::kernel32` -/// surface and layers the additional externs higher-tier crates reach for -/// (`ReadDirectoryChangesW`, IOCP, SRW locks, `CreateProcessW`, …). Declared -/// locally so adding an extern doesn't require touching `bun_windows_sys`. +/// Re-exports the tier-0 `bun_windows_sys::kernel32` surface and declares the +/// kernel32 externs only this crate's dependents name +/// (`ReadDirectoryChangesW`, IOCP, SRW locks, …). pub mod kernel32 { use super::{ BOOL, CONDITION_VARIABLE, DWORD, FileNotifyChangeFilter, HANDLE, LPCWSTR, LPOVERLAPPED, @@ -38,8 +37,6 @@ pub mod kernel32 { #[link(name = "kernel32")] unsafe extern "system" { - // safe: by-value DWORD write to the TEB; cannot fault. - pub safe fn SetLastError(dwErrCode: DWORD); // ── IOCP / async directory watching ── // safe: all args are by-value opaques (`HANDLE`/`ULONG_PTR`/`DWORD`); // a bad handle yields NULL + GetLastError, no UB. @@ -67,10 +64,6 @@ pub mod kernel32 { lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE, ) -> BOOL; - // safe: by-value `HANDLE` + `DWORD`; a bad handle yields - // `WAIT_FAILED` + GetLastError, no UB. - pub safe fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD; - // ── file moves ── pub fn MoveFileExW( lpExistingFileName: LPCWSTR, @@ -86,26 +79,18 @@ pub mod kernel32 { dwMilliseconds: DWORD, Flags: ULONG, ) -> BOOL; - - /// No preconditions; reads the calling thread's ID. - pub safe fn GetCurrentThreadId() -> DWORD; } } pub use bun_windows_sys::BOOL; pub use bun_windows_sys::BOOLEAN; pub use bun_windows_sys::CHAR; -pub use bun_windows_sys::DWORD; -pub use bun_windows_sys::LPVOID; -pub use bun_windows_sys::MAX_PATH; -pub use bun_windows_sys::PATH_MAX_WIDE; -pub use bun_windows_sys::WORD; -/// `PVOID` (winnt.h) — alias of `LPVOID`. Keep the alias so existing -/// callers (`bun_shim_impl`) don't need rewriting. -pub type PVOID = LPVOID; pub use bun_windows_sys::COORD; +pub use bun_windows_sys::DWORD; +pub use bun_windows_sys::ENABLE_VIRTUAL_TERMINAL_PROCESSING; pub use bun_windows_sys::FALSE; pub use bun_windows_sys::FILE_BEGIN; +pub use bun_windows_sys::FILE_CURRENT; pub use bun_windows_sys::FILE_END; pub use bun_windows_sys::FILE_OPEN; pub use bun_windows_sys::INVALID_HANDLE_VALUE; @@ -114,20 +99,22 @@ pub use bun_windows_sys::LPCSTR; pub use bun_windows_sys::LPCVOID; pub use bun_windows_sys::LPCWSTR; pub use bun_windows_sys::LPSTR; +pub use bun_windows_sys::LPVOID; pub use bun_windows_sys::LPWSTR; +pub use bun_windows_sys::MAX_PATH; pub use bun_windows_sys::NT_ERROR; pub use bun_windows_sys::NT_SUCCESS; pub use bun_windows_sys::NTSTATUS; +pub use bun_windows_sys::PATH_MAX_WIDE; pub use bun_windows_sys::PWSTR; pub use bun_windows_sys::TRUE; pub use bun_windows_sys::UINT; pub use bun_windows_sys::ULONG; pub use bun_windows_sys::UNICODE_STRING; pub use bun_windows_sys::WCHAR; +pub use bun_windows_sys::WORD; /// `STARTF_USESTDHANDLES` (winbase.h). pub use bun_windows_sys::externs::STARTF_USESTDHANDLES; -/// `ENABLE_VIRTUAL_TERMINAL_PROCESSING` (consoleapi.h). -pub const ENABLE_VIRTUAL_TERMINAL_PROCESSING: DWORD = 0x0004; pub const MOVEFILE_COPY_ALLOWED: DWORD = 0x2; pub const MOVEFILE_REPLACE_EXISTING: DWORD = 0x1; pub const MOVEFILE_WRITE_THROUGH: DWORD = 0x8; @@ -172,7 +159,9 @@ pub use bun_windows_sys::{ PIPE_TYPE_BYTE, PIPE_WAIT, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE, SYMBOLIC_LINK_FLAG_DIRECTORY, }; -pub use bun_windows_sys::{FILE_READ_ATTRIBUTES, FILE_READ_DATA, FILE_READ_EA, FILE_TRAVERSE}; +pub use bun_windows_sys::{ + FILE_GENERIC_READ, FILE_READ_ATTRIBUTES, FILE_READ_DATA, FILE_READ_EA, FILE_TRAVERSE, +}; // Stdio handle helpers (live in `bun_core::windows_sys` so the no-dep core can // resolve PEB stdio at startup; re-export here for the `sys::windows::*` path). pub use bun_core::windows_sys::{ @@ -192,25 +181,7 @@ pub const fn from_sys_time(nt_time: i64) -> i128 { (nt_time as i128 - EPOCH_DIFFERENCE_100NS as i128) * 100 } -/// Convert a 64-bit Windows `FILETIME` (100-ns ticks since 1601-01-01 UTC) -/// into a libuv `uv_timespec_t` (seconds + nanoseconds since the Unix epoch). -/// Matches libuv's `uv__filetime_to_timespec`. -#[inline] -pub(crate) fn filetime_to_timespec(filetime: i64) -> bun_libuv_sys::uv_timespec_t { - let t = filetime - EPOCH_DIFFERENCE_100NS; - let mut sec = t / 10_000_000; - let mut nsec = (t - sec * 10_000_000) * 100; - if nsec < 0 { - sec -= 1; - nsec += 1_000_000_000; - } - bun_libuv_sys::uv_timespec_t { - sec: sec as _, - nsec: nsec as _, - } -} - -pub const INVALID_FILE_ATTRIBUTES: u32 = u32::MAX; +pub use bun_windows_sys::INVALID_FILE_ATTRIBUTES; pub const NT_OBJECT_PREFIX: [u16; 4] = [b'\\' as u16, b'?' as u16, b'?' as u16, b'\\' as u16]; pub(crate) const LONG_PATH_PREFIX: [u16; 4] = @@ -230,11 +201,9 @@ pub use bun_windows_sys::HMODULE; // `bun_crash_handler`, `bun_threading`, `bun_install`. // ────────────────────────────────────────────────────────────────────────── +pub use bun_windows_sys::HRESULT; +pub use bun_windows_sys::INFINITE; pub use bun_windows_sys::ULONG_PTR; -/// `HRESULT` — 32-bit signed result code. -pub type HRESULT = i32; -/// `WaitForSingleObject` infinite timeout sentinel. -pub const INFINITE: DWORD = 0xFFFF_FFFF; // ── SRWLOCK / CONDITION_VARIABLE (`bun_threading` windows arm) ──────────── // Win32 defines both as `struct { PVOID Ptr; }`; static-init is all-zero @@ -278,8 +247,8 @@ pub const fn HRESULT_CODE(hr: HRESULT) -> HRESULT { hr & 0xFFFF } -// `NtCreateFile` access masks / create-options not yet in `bun_windows_sys`. -pub const FILE_LIST_DIRECTORY: ULONG = 0x0001; +pub use bun_windows_sys::FILE_LIST_DIRECTORY; +/// `NtCreateFile` CreateOptions. pub const FILE_OPEN_FOR_BACKUP_INTENT: ULONG = 0x0000_4000; // `ReadDirectoryChangesW` action codes (`winnt.h`). @@ -359,42 +328,26 @@ pub use bun_windows_sys::externs::FILE_FLAG_BACKUP_SEMANTICS; pub use bun_windows_sys::externs::GetFileInformationByHandle; pub use bun_windows_sys::externs::OPEN_EXISTING; -unsafe extern "system" { - // safe: `HANDLE` is a by-value opaque; bad handle → FILE_TYPE_UNKNOWN + - // GetLastError, no UB. - #[link_name = "GetFileType"] - safe fn GetFileType_raw(hFile: HANDLE) -> DWORD; -} - pub fn GetFileType(hFile: HANDLE) -> DWORD { - let rc = GetFileType_raw(hFile); + let rc = externs::GetFileType(hFile); // `syslog!` self-gates on `env::IS_DEBUG` (see lib.rs); no extra feature // flag needed (there is no `debug_logs` feature in bun_sys). - bun_sys::syslog!("GetFileType({}) = {}", Fd::from_system(hFile), rc); + // Not as an `Fd`: `INVALID_HANDLE_VALUE`, which a closed fd has, is none. + bun_sys::syslog!("GetFileType({:p}) = {}", hFile, rc); rc } /// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfiletype#return-value -pub(crate) const FILE_TYPE_UNKNOWN: DWORD = 0x0000; -pub(crate) const FILE_TYPE_DISK: DWORD = 0x0001; -pub const FILE_TYPE_CHAR: DWORD = 0x0002; -pub const FILE_TYPE_PIPE: DWORD = 0x0003; -pub(crate) const FILE_TYPE_REMOTE: DWORD = 0x8000; - -pub use SetCurrentDirectoryW as SetCurrentDirectory; -/// Each process has a single current directory made up of two parts: -/// -/// - A disk designator that is either a drive letter followed by a colon, or a server name and share name (\\servername\sharename) -/// - A directory on the disk designator -/// -/// The current directory is shared by all threads of the process: If one thread changes the current directory, it affects all threads in the process. Multithreaded applications and shared library code should avoid calling the SetCurrentDirectory function due to the risk of affecting relative path calculations being performed by other threads. Conversely, multithreaded applications and shared library code should avoid using relative paths so that they are unaffected by changes to the current directory performed by other threads. -/// -/// Note that the current directory for a process is locked while the process is executing. This will prevent the directory from being deleted, moved, or renamed. -pub use bun_windows_sys::externs::SetCurrentDirectoryW; +pub use bun_windows_sys::{FILE_TYPE_CHAR, FILE_TYPE_PIPE}; +pub(crate) use bun_windows_sys::{FILE_TYPE_DISK, FILE_TYPE_REMOTE, FILE_TYPE_UNKNOWN}; pub use bun_windows_sys::externs::RtlNtStatusToDosError; pub use bun_windows_sys::externs::SaferiIsExecutableFileType; +pub use bun_windows_sys::externs::{ + CSTR_EQUAL, FILE_FLAG_FIRST_PIPE_INSTANCE, GUID, HKEY, HKEY_LOCAL_MACHINE, OSVERSIONINFOW, + OpenProcessToken, RegGetValueW, RtlGetVersion, WAIT_OBJECT_0, +}; /// Codes from . /// Canonical newtype lives in `bun_windows_sys` (tier-0); re-exported here so @@ -402,7 +355,7 @@ pub use bun_windows_sys::externs::SaferiIsExecutableFileType; /// type. pub use bun_windows_sys::Win32Error; -/// `to_system_errno()` / `to_e()` — extension trait from `bun_errno` (the +/// `to_e()` — extension trait from `bun_errno` (the /// `SystemErrno` mapping table is a higher-tier concern than the tier-0 /// newtype). pub use bun_errno::Win32ErrorExt; @@ -411,10 +364,12 @@ pub use bun_errno::Win32ErrorExt; /// with `Error::from_win32(Win32Error::get(), tag)` instead. #[inline] pub fn last_system_errno() -> SystemErrno { - Win32Error::get().to_system_errno() + Win32Error::get().to_e() } -pub use bun_libuv_sys as libuv; +#[allow(non_snake_case)] +pub mod O; +pub mod fs; /// True when the process token is a Windows AppContainer (lowbox) token. /// Cached for the process lifetime; the token's AppContainer bit is immutable. @@ -448,7 +403,7 @@ pub fn is_app_container() -> bool { }) } -pub use bun_errno::translate_uv_error_to_e; +pub use bun_errno::uv_to_e; pub use bun_windows_sys::externs::GetProcAddress; @@ -526,7 +481,7 @@ pub fn translate_nt_status_to_errno(err: NTSTATUS) -> E { OBJECT_NAME_INVALID => bun_core::debug_warn!( "Received OBJECT_NAME_INVALID, indicates a file path conversion issue.", ), - t if e == E::UNKNOWN => bun_core::debug_warn!( + t if e == E::EUNKNOWN => bun_core::debug_warn!( "Called translateNTStatusToErrno with {:?} which does not have a mapping to errno.", t ), @@ -541,20 +496,11 @@ pub use bun_windows_sys::externs::GetHostNameW; /// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw pub use bun_windows_sys::externs::GetTempPathW; -/// `GetCurrentProcessId` (processthreadsapi.h) — current PID. Safe wrapper: -/// the underlying call has no preconditions and never fails. -#[inline] -pub fn GetCurrentProcessId() -> DWORD { - unsafe extern "system" { - // No preconditions; reads thread-local kernel state. - safe fn GetCurrentProcessId() -> DWORD; - } - GetCurrentProcessId() -} +pub use bun_windows_sys::GetCurrentProcessId; pub use bun_windows_sys::{PEB, RTL_USER_PROCESS_PARAMETERS, TEB, teb}; -pub use bun_windows_sys::externs::CreateJobObjectA; +pub use bun_windows_sys::externs::CreateJobObjectW; pub use bun_windows_sys::externs::AssignProcessToJobObject; @@ -598,8 +544,6 @@ pub use bun_windows_sys::{ MENU_EVENT_RECORD, MOUSE_EVENT_RECORD, WINDOW_BUFFER_SIZE_EVENT, }; -// Bun__UVSignalHandle__{init,close}: see src/runtime/node/uv_signal_handle_windows.rs - /// Is not the actual UID of the user, but just a hash of username. pub fn user_unique_id() -> u32 { // https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/165836c1-89d7-4abb-840d-80cf2510aa3e @@ -634,8 +578,9 @@ pub use bun_windows_sys::externs::CreateDirectoryExW; #[derive(thiserror::Error, strum::IntoStaticStr, Debug)] pub enum GetFinalPathNameByHandleError { - #[error("FileNotFound")] - FileNotFound, + /// What `GetFinalPathNameByHandleW` failed with. + #[error("{0:?}")] + Failed(Win32Error), #[error("NameTooLong")] NameTooLong, } @@ -750,7 +695,9 @@ fn lowbox_dos_name_fallback( "GetFinalPathNameByHandleW({:p}) = denied (no NT name)", hFile ); - return Err(GetFinalPathNameByHandleError::FileNotFound); + return Err(GetFinalPathNameByHandleError::Failed( + Win32Error::ACCESS_DENIED, + )); }; let nt = &nt_buf.0[..nt_len]; let Some((device, letter)) = system_volume_device() else { @@ -758,7 +705,9 @@ fn lowbox_dos_name_fallback( "GetFinalPathNameByHandleW({:p}) = denied (system volume unresolved)", hFile ); - return Err(GetFinalPathNameByHandleError::FileNotFound); + return Err(GetFinalPathNameByHandleError::Failed( + Win32Error::ACCESS_DENIED, + )); }; if !(nt.len() > device.len() && nt[..device.len()] == device[..] @@ -769,7 +718,9 @@ fn lowbox_dos_name_fallback( hFile, bun_core::fmt::utf16(nt) ); - return Err(GetFinalPathNameByHandleError::FileNotFound); + return Err(GetFinalPathNameByHandleError::Failed( + Win32Error::ACCESS_DENIED, + )); } let rest = &nt[device.len()..]; let total = 2 + rest.len(); @@ -861,18 +812,18 @@ pub(crate) fn GetFinalPathNameByHandle( }; if return_length == 0 { - let err = GetLastError(); + let err = Win32Error::get(); bun_sys::syslog!("GetFinalPathNameByHandleW({:p}) = {:?}", hFile, err); // An AppContainer (lowbox) token is denied the mount-manager lookup // behind the DOS volume-name translation while the NT form still // works; rebuild `X:\…` from the NT name (system volume only). if fmt.volume_name == win32::VolumeName::Dos - && err == u32::from(Win32Error::ACCESS_DENIED.0) + && err == Win32Error::ACCESS_DENIED && is_app_container() { return lowbox_dos_name_fallback(hFile, out_buffer); } - return Err(GetFinalPathNameByHandleError::FileNotFound); + return Err(GetFinalPathNameByHandleError::Failed(err)); } if (return_length as usize) >= out_buffer.len() { @@ -939,7 +890,8 @@ pub fn get_module_name_w(module: HMODULE, buf: &mut [u16]) -> Option<&[u16]> { u32::try_from(buf.len()).expect("int cast"), ) }; - if rc == 0 { + // A path that did not fit is cut off and reported as `buf.len()` units. + if rc == 0 || rc as usize >= buf.len() { return None; } Some(&buf[0..(rc as usize)]) @@ -947,12 +899,10 @@ pub fn get_module_name_w(module: HMODULE, buf: &mut [u16]) -> Option<&[u16]> { pub use bun_windows_sys::externs::GetThreadDescription; -pub const ENABLE_ECHO_INPUT: DWORD = 0x004; -pub const ENABLE_LINE_INPUT: DWORD = 0x002; -pub const ENABLE_PROCESSED_INPUT: DWORD = 0x001; -pub const ENABLE_VIRTUAL_TERMINAL_INPUT: DWORD = 0x200; -pub const ENABLE_WRAP_AT_EOL_OUTPUT: DWORD = 0x0002; -pub const ENABLE_PROCESSED_OUTPUT: DWORD = 0x0001; +pub use bun_windows_sys::{ + ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, ENABLE_PROCESSED_OUTPUT, + ENABLE_VIRTUAL_TERMINAL_INPUT, ENABLE_WRAP_AT_EOL_OUTPUT, +}; pub use bun_windows_sys::externs::GetConsoleCP; pub use bun_windows_sys::externs::GetConsoleOutputCP; @@ -973,10 +923,6 @@ impl Default for DeleteFileOptions { } } -const FILE_DISPOSITION_DELETE: ULONG = 0x00000001; -const FILE_DISPOSITION_POSIX_SEMANTICS: ULONG = 0x00000002; -const FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE: ULONG = 0x00000010; - // Copy-paste of the standard library function except without unreachable. pub fn DeleteFileBun(sub_path_w: &[u16], options: DeleteFileOptions) -> bun_sys::Result<()> { let create_options_flags: ULONG = if options.remove_dir { @@ -990,7 +936,7 @@ pub fn DeleteFileBun(sub_path_w: &[u16], options: DeleteFileOptions) -> bun_sys: // NtCreateFile's caller gets a recoverable error rather than an abort. let path_len_bytes = match u16::try_from(sub_path_w.len() * 2) { Ok(n) => n, - Err(_) => return bun_sys::Result::errno(E::NAMETOOLONG, bun_sys::Tag::open), + Err(_) => return bun_sys::Result::errno(E::ENAMETOOLONG, bun_sys::Tag::open), }; let mut nt_name = UNICODE_STRING { Length: path_len_bytes, @@ -1022,7 +968,7 @@ pub fn DeleteFileBun(sub_path_w: &[u16], options: DeleteFileOptions) -> bun_sys: let mut io: IO_STATUS_BLOCK = bun_core::ffi::zeroed(); let mut tmp_handle: HANDLE = ptr::null_mut(); // SAFETY: all out-params are valid - let mut rc = unsafe { + let rc = unsafe { ntdll::NtCreateFile( &mut tmp_handle, windows::SYNCHRONIZE | windows::DELETE, @@ -1059,75 +1005,23 @@ pub fn DeleteFileBun(sub_path_w: &[u16], options: DeleteFileOptions) -> bun_sys: let _ = externs::CloseHandle(h); }); - // FileDispositionInformationEx (and therefore FILE_DISPOSITION_POSIX_SEMANTICS and FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE) - // are only supported on NTFS filesystems, so the version check on its own is only a partial solution. To support non-NTFS filesystems - // like FAT32, we need to fallback to FileDispositionInformation if the usage of FileDispositionInformationEx gives - // us INVALID_PARAMETER. - // The same reasoning for win10_rs5 as in os.renameatW() applies (FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE requires >= win10_rs5). - let mut need_fallback = true; - // Deletion with posix semantics if the filesystem supports it. - let mut info = windows::FILE_DISPOSITION_INFORMATION_EX { - Flags: FILE_DISPOSITION_DELETE - | FILE_DISPOSITION_POSIX_SEMANTICS - | FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE, - }; - - // SAFETY: tmp_handle and io are valid - rc = unsafe { - ntdll::NtSetInformationFile( - tmp_handle, - &mut io, - core::ptr::from_mut(&mut info).cast::(), - size_of::() as u32, - windows::FileInformationClass::FileDispositionInformationEx, - ) - }; + let deleted = fs::delete_by_handle(tmp_handle); bun_sys::syslog!( "NtSetInformationFile({}, DELETE) = {:?}", bun_core::fmt::fmt_path_u16(sub_path_w, Default::default()), - rc + deleted ); - match rc { - x if x == windows::ntstatus::SUCCESS => return bun_sys::Result::success(), - // INVALID_PARAMETER here means that the filesystem does not support FileDispositionInformationEx - x if x == windows::ntstatus::INVALID_PARAMETER => {} - // For all other statuses, fall down to the switch below to handle them. - _ => need_fallback = false, - } - if need_fallback { - // Deletion with file pending semantics, which requires waiting or moving - // files to get them removed (from here). - let mut file_dispo = windows::FILE_DISPOSITION_INFORMATION { - DeleteFile: TRUE as BOOLEAN, - }; - - // SAFETY: tmp_handle and io are valid - rc = unsafe { - ntdll::NtSetInformationFile( - tmp_handle, - &mut io, - core::ptr::from_mut(&mut file_dispo).cast::(), - size_of::() as u32, - windows::FileInformationClass::FileDispositionInformation, - ) - }; - bun_sys::syslog!( - "NtSetInformationFile({}, DELETE) = {:?}", - bun_core::fmt::fmt_path_u16(sub_path_w, Default::default()), - rc - ); - } - // Another handle already set the delete disposition; the file is on its - // way out, which is what the caller asked for. Checked here so it covers - // both the FileDispositionInformationEx result and the legacy fallback. - if rc == windows::ntstatus::DELETE_PENDING || rc == windows::ntstatus::FILE_DELETED { - return bun_sys::Result::success(); - } - if let Some(err) = bun_sys::Result::<()>::errno_sys(rc, bun_sys::Tag::NtSetInformationFile) { - return err; + match deleted { + Ok(()) => bun_sys::Result::success(), + // Another handle already set the delete disposition; the file is on + // its way out, which is what the caller asked for. + Err(rc) + if rc == windows::ntstatus::DELETE_PENDING || rc == windows::ntstatus::FILE_DELETED => + { + bun_sys::Result::success() + } + Err(rc) => bun_sys::Result::errno(rc, bun_sys::Tag::NtSetInformationFile), } - - bun_sys::Result::success() } pub const EXCEPTION_CONTINUE_EXECUTION: i32 = -1; @@ -1200,27 +1094,12 @@ pub struct EXCEPTION_POINTERS { /// `OnceLock` so the per-call allocation goes away after the first /// call. pub fn detect_runtime_version() -> &'static str { - #[repr(C)] - struct OSVERSIONINFOW { - dwOSVersionInfoSize: u32, - dwMajorVersion: u32, - dwMinorVersion: u32, - dwBuildNumber: u32, - dwPlatformId: u32, - szCSDVersion: [u16; 128], - } - unsafe extern "system" { - // safe: out-param is `&mut OSVERSIONINFOW` (non-null, valid for write); - // ntdll only writes the struct and returns NTSTATUS — no preconditions. - safe fn RtlGetVersion(info: &mut OSVERSIONINFOW) -> i32; - } + use bun_windows_sys::{OSVERSIONINFOW, RtlGetVersion}; static CACHE: std::sync::OnceLock = std::sync::OnceLock::new(); - // SAFETY: `#[repr(C)]` POD — five u32 + a `[u16; 128]` array. - unsafe impl bun_core::ffi::Zeroable for OSVERSIONINFOW {} CACHE.get_or_init(|| { let mut info: OSVERSIONINFOW = bun_core::ffi::zeroed(); info.dwOSVersionInfoSize = core::mem::size_of::() as u32; - if RtlGetVersion(&mut info) != 0 { + if RtlGetVersion(&mut info) != NTSTATUS::SUCCESS { return std::string::String::from("unknown"); } std::format!("{}.{}", info.dwMajorVersion, info.dwBuildNumber) @@ -1233,8 +1112,8 @@ pub use bun_windows_sys::externs::UpdateProcThreadAttribute; pub use bun_windows_sys::externs::IsProcessInJob; -pub(crate) const EXTENDED_STARTUPINFO_PRESENT: DWORD = 0x80000; -pub(crate) const PROC_THREAD_ATTRIBUTE_JOB_LIST: DWORD = 0x2000D; +pub use bun_windows_sys::CREATE_UNICODE_ENVIRONMENT; +pub(crate) use bun_windows_sys::{EXTENDED_STARTUPINFO_PRESENT, PROC_THREAD_ATTRIBUTE_JOB_LIST}; /// Handle to a Windows pseudoconsole (ConPTY). pub use bun_windows_sys::externs::HPCON; @@ -1245,15 +1124,16 @@ pub use bun_windows_sys::externs::ResizePseudoConsole; pub use bun_windows_sys::externs::ClosePseudoConsole; -pub const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: DWORD = 0x2000; -pub(crate) const JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION: DWORD = 0x400; -pub(crate) const JOB_OBJECT_LIMIT_BREAKAWAY_OK: DWORD = 0x800; -pub(crate) const JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK: DWORD = 0x00001000; +pub use bun_windows_sys::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; +pub(crate) use bun_windows_sys::{ + JOB_OBJECT_LIMIT_BREAKAWAY_OK, JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION, + JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK, +}; /// `LimitFlags` for a kill-on-close Job whose members are added by /// inheritance (`--no-orphans`, the parallel-test coordinator). Omits /// `SILENT_BREAKAWAY_OK` on purpose: that would make every child escape the -/// Job. libuv's global job sets it because libuv assigns each child itself. +/// Job. pub const JOB_LIMIT_FLAGS_KILL_TREE_ON_CLOSE: DWORD = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | JOB_OBJECT_LIMIT_BREAKAWAY_OK; @@ -1316,8 +1196,6 @@ pub mod rescle { description: Option<&[u8]>, copyright: Option<&[u8]>, ) -> Result<(), RescleError> { - const _: () = assert!(cfg!(windows)); - // Validate version string format if provided if let Some(v) = version { // Empty version string is invalid @@ -1468,8 +1346,8 @@ pub fn update_stdio_mode_flags( ) -> Result { let fd = i.fd(); let mut original_mode: DWORD = 0; - if kernel32_2::GetConsoleMode(fd.native(), &mut original_mode) == 0 - || kernel32_2::SetConsoleMode(fd.native(), (original_mode | opts.set) & !opts.unset) == 0 + if externs::GetConsoleMode(fd.native(), &mut original_mode) == 0 + || externs::SetConsoleMode(fd.native(), (original_mode | opts.set) & !opts.unset) == 0 { return Err(last_system_errno()); } @@ -1498,7 +1376,7 @@ impl Drop for StdinModeGuard { #[inline] fn drop(&mut self) { if let Some(mode) = self.original { - let _ = kernel32_2::SetConsoleMode(bun_sys::Stdio::StdIn.fd().native(), mode); + let _ = externs::SetConsoleMode(bun_sys::Stdio::StdIn.fd().native(), mode); } } } @@ -1516,21 +1394,16 @@ pub fn is_watcher_child() -> bool { let mut buf: [u16; 1] = [0]; // SAFETY: buf valid for 1 element unsafe { - kernel32_2::GetEnvironmentVariableW(WATCHER_CHILD_ENV_Z.as_ptr(), buf.as_mut_ptr(), 1) > 0 + externs::GetEnvironmentVariableW(WATCHER_CHILD_ENV_Z.as_ptr(), buf.as_mut_ptr(), 1) > 0 } } pub fn become_watcher_manager() -> ! { // this process will be the parent of the child process that actually runs the script let mut procinfo: PROCESS_INFORMATION = bun_core::ffi::zeroed(); - unsafe extern "C" { - // safe: no args; C++ shim mutates process-global stdio inheritance - // flags — no preconditions. - safe fn windows_enable_stdio_inheritance(); - } windows_enable_stdio_inheritance(); // SAFETY: null args allowed - let job = unsafe { externs::CreateJobObjectA(ptr::null_mut(), ptr::null()) }; + let job = unsafe { externs::CreateJobObjectW(ptr::null_mut(), ptr::null()) }; if job.is_null() { // Print the Win32 error name, not the raw DWORD. let err = Win32Error::get(); @@ -1576,8 +1449,6 @@ pub fn become_watcher_manager() -> ! { } bun_core::Output::panic(format_args!("Failed to spawn process: {}\n", err)); } - // `kernel32::WaitForSingleObject` is the local `safe fn` re-decl - // (by-value `HANDLE`/`DWORD` only); check `WAIT_FAILED` inline. if kernel32::WaitForSingleObject(procinfo.hProcess, win32::INFINITE) == externs::WAIT_FAILED { let err = Win32Error::get(); @@ -1587,17 +1458,17 @@ pub fn become_watcher_manager() -> ! { )); } let mut exit_code: DWORD = 0; - if kernel32_2::GetExitCodeProcess(procinfo.hProcess, &mut exit_code) == 0 { + if externs::GetExitCodeProcess(procinfo.hProcess, &mut exit_code) == 0 { // Capture before NtClose — closing the handle may overwrite the // thread's last-error. let err = Win32Error::get(); - let _ = kernel32_2::NtClose(procinfo.hProcess); + let _ = ntdll::NtClose(procinfo.hProcess); bun_core::Output::panic(format_args!( "Failed to get exit code of child process: {:?}\n", err )); } - let _ = kernel32_2::NtClose(procinfo.hProcess); + let _ = ntdll::NtClose(procinfo.hProcess); // magic exit code to indicate that the child process should be re-spawned if exit_code == WATCHER_RELOAD_EXIT { @@ -1631,7 +1502,7 @@ pub(crate) fn spawn_watcher_child( externs::UpdateProcThreadAttribute( p.as_mut_ptr(), 0, - PROC_THREAD_ATTRIBUTE_JOB_LIST as usize, + PROC_THREAD_ATTRIBUTE_JOB_LIST, core::ptr::from_mut(&mut job_local).cast::(), size_of::(), ptr::null_mut(), @@ -1642,8 +1513,6 @@ pub(crate) fn spawn_watcher_child( return Err(bun_errno::SystemErrno::EIO); } - // The win32 layer exposes these as DWORD constants — assemble the raw mask. - const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400; let flags: DWORD = CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT; let image_path = exe_path_w(); @@ -1738,9 +1607,9 @@ pub(crate) fn spawn_watcher_child( return Err(bun_errno::SystemErrno::EIO); } let mut is_in_job: BOOL = 0; - let _ = kernel32_2::IsProcessInJob(procinfo.hProcess, job, &mut is_in_job); + let _ = externs::IsProcessInJob(procinfo.hProcess, job, &mut is_in_job); debug_assert!(is_in_job != 0); - let _ = kernel32_2::NtClose(procinfo.hThread); + let _ = ntdll::NtClose(procinfo.hThread); Ok(()) } @@ -1759,10 +1628,13 @@ pub(crate) extern "C" fn Bun__LoadLibraryBunString(str_: &bun_core::String) -> * kernel32::SetLastError(DWORD::from(Win32Error::FILENAME_EXCED_RANGE.int())); return ptr::null_mut(); } - const LOAD_WITH_ALTERED_SEARCH_PATH: DWORD = 0x00000008; // SAFETY: buf NUL-terminated by `encode_into_utf16_buf_z`. unsafe { - kernel32::LoadLibraryExW(buf.as_ptr(), ptr::null_mut(), LOAD_WITH_ALTERED_SEARCH_PATH) + kernel32::LoadLibraryExW( + buf.as_ptr(), + ptr::null_mut(), + win32::LOAD_WITH_ALTERED_SEARCH_PATH, + ) } } @@ -1805,7 +1677,7 @@ pub fn move_opened_file_at( let struct_len = size_of::() - 1 + new_file_name.len() * 2; if struct_len > STRUCT_BUF_LEN { - return bun_sys::Result::errno(E::NAMETOOLONG, bun_sys::Tag::NtSetInformationFile); + return bun_sys::Result::errno(E::ENAMETOOLONG, bun_sys::Tag::NtSetInformationFile); } // SAFETY: AlignedBuf is #[repr(align(8))] which matches FILE_RENAME_INFORMATION_EX alignment. @@ -1848,16 +1720,46 @@ pub fn move_opened_file_at( new_file_name.len(), ); } - // SAFETY: src_fd valid; rename_info has struct_len initialized bytes - let rc = unsafe { - ntdll::NtSetInformationFile( - src_fd.native(), - &mut io_status_block, - rename_info.cast::(), - u32::try_from(struct_len).expect("int cast"), // already checked for error.NameTooLong - win32::FileInformationClass::FileRenameInformationEx, - ) + let rename_info_bytes = rename_info.cast::(); + let mut set_rename_information = |class| { + // SAFETY: src_fd valid; rename_info has struct_len initialized bytes + unsafe { + ntdll::NtSetInformationFile( + src_fd.native(), + &mut io_status_block, + rename_info_bytes, + u32::try_from(struct_len).expect("int cast"), // already checked for error.NameTooLong + class, + ) + } }; + let mut rc = set_rename_information(win32::FileInformationClass::FileRenameInformationEx); + // What a file system without POSIX rename (FAT, exFAT, some redirectors) + // says. `FILE_RENAME_INFORMATION` is the same struct with a BOOLEAN where + // the flags are. + if matches!( + Win32Error::from_ntstatus(rc), + Win32Error::NOT_SUPPORTED | Win32Error::INVALID_PARAMETER | Win32Error::INVALID_FUNCTION + ) { + // Unlike the POSIX one, this rename lets a directory replace a file. + let mut io: win32::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + let mut standard = MaybeUninit::::zeroed(); + // SAFETY: src_fd valid; `standard` is writable for its size, and all + // zeroes is a value of it. The class takes no particular access. + let is_directory = unsafe { + ntdll::NtQueryInformationFile( + src_fd.native(), + &mut io, + standard.as_mut_ptr().cast::(), + size_of::() as u32, + win32::FileInformationClass::FileStandardInformation, + ) == win32::ntstatus::SUCCESS + && standard.assume_init().Directory != 0 + }; + // SAFETY: `rename_info` was initialized above. + unsafe { (*rename_info).Flags = ULONG::from(replace_if_exists && !is_directory) }; + rc = set_rename_information(win32::FileInformationClass::FileRenameInformation); + } bun_sys::syslog!( "moveOpenedFileAt({} ->> {} '{}', {}) = {}", src_fd, @@ -1895,40 +1797,17 @@ pub(crate) fn rename_at_w( new_path_w: &[u16], replace_if_exists: bool, ) -> bun_sys::Result<()> { - let src_fd = 'brk: { - match bun_sys::open_file_at_windows( - old_dir_fd, - old_path_w, - bun_sys::NtCreateFileOptions { - access_mask: win32::SYNCHRONIZE - | win32::GENERIC_WRITE - | win32::DELETE - | win32::FILE_TRAVERSE, - disposition: win32::FILE_OPEN, - options: win32::FILE_SYNCHRONOUS_IO_NONALERT | win32::FILE_OPEN_REPARSE_POINT, - ..Default::default() - }, - ) { - bun_sys::Result::Err(_) => { - // retry, wtihout FILE_TRAVERSE flag - match bun_sys::open_file_at_windows( - old_dir_fd, - old_path_w, - bun_sys::NtCreateFileOptions { - access_mask: win32::SYNCHRONIZE | win32::GENERIC_WRITE | win32::DELETE, - disposition: win32::FILE_OPEN, - options: win32::FILE_SYNCHRONOUS_IO_NONALERT - | win32::FILE_OPEN_REPARSE_POINT, - ..Default::default() - }, - ) { - bun_sys::Result::Err(err2) => return bun_sys::Result::Err(err2), - bun_sys::Result::Ok(fd) => break 'brk fd, - } - } - bun_sys::Result::Ok(fd) => break 'brk fd, - } - }; + // What a rename takes. Write access is refused for a read-only file. + let src_fd = bun_sys::open_file_at_windows( + old_dir_fd, + old_path_w, + bun_sys::NtCreateFileOptions { + access_mask: win32::SYNCHRONIZE | win32::DELETE, + disposition: win32::FILE_OPEN, + options: win32::FILE_SYNCHRONOUS_IO_NONALERT | win32::FILE_OPEN_REPARSE_POINT, + ..Default::default() + }, + )?; let _close = bun_sys::CloseOnDrop::new(src_fd); move_opened_file_at(src_fd, new_dir_fd, new_path_w, replace_if_exists) @@ -1940,30 +1819,6 @@ mod kernel32_2 { /// No preconditions; allocates and returns the env block (or null). pub(super) safe fn GetEnvironmentStringsW() -> LPWSTR; pub(super) fn FreeEnvironmentStringsW(penv: LPWSTR) -> BOOL; - pub(super) fn GetEnvironmentVariableW( - lpName: LPCWSTR, - lpBuffer: *mut WCHAR, - nSize: DWORD, - ) -> DWORD; - // safe: by-value `HANDLE`/`DWORD` only; bad handle → BOOL 0 + - // GetLastError, never UB. Out-param is `&mut DWORD` (non-null, valid - // for write). Local `safe fn` re-decls so in-crate callers drop the - // per-site `unsafe { }`; the `bun_windows_sys::externs` raw decls stay - // re-exported for out-of-crate callers. - pub(super) safe fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: &mut DWORD) -> BOOL; - pub(super) safe fn SetConsoleMode(hConsoleHandle: HANDLE, dwMode: DWORD) -> BOOL; - pub(super) safe fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: &mut DWORD) -> BOOL; - // safe: by-value `HANDLE`×2; out-param is `&mut BOOL` (non-null, valid - // for write). Bad handle → BOOL 0 + GetLastError, never UB. - pub(super) safe fn IsProcessInJob( - hProcess: HANDLE, - hJob: HANDLE, - result: &mut BOOL, - ) -> BOOL; - // safe: by-value `HANDLE` only; bad/stale handle → - // `STATUS_INVALID_HANDLE`, never UB (mirrors POSIX `close(fd)` → - // `EBADF`, which is `safe fn` in `safe_libc`). - pub(super) safe fn NtClose(Handle: HANDLE) -> NTSTATUS; } } @@ -1997,7 +1852,7 @@ pub fn GetEnvironmentVariableW( nSize: DWORD, ) -> Result { // SAFETY: caller provides valid buffer - let rc = unsafe { kernel32_2::GetEnvironmentVariableW(lpName, lpBuffer, nSize) }; + let rc = unsafe { externs::GetEnvironmentVariableW(lpName, lpBuffer, nSize) }; if rc == 0 { match Win32Error::get() { @@ -2032,7 +1887,7 @@ pub fn getenv_w(name: &[u16]) -> Option> { loop { // SAFETY: name and buf are valid for the call's duration. let n = unsafe { - kernel32_2::GetEnvironmentVariableW(name.as_ptr(), buf.as_mut_ptr(), buf.len() as DWORD) + externs::GetEnvironmentVariableW(name.as_ptr(), buf.as_mut_ptr(), buf.len() as DWORD) }; if n == 0 { return None; @@ -2045,10 +1900,6 @@ pub fn getenv_w(name: &[u16]) -> Option> { } } -// `bun.windows.libuv` — re-exported as `pub use bun_libuv_sys as libuv` above. -// The duplicate inline `pub mod libuv { ... }` that lived here caused E0260 and -// has been removed; its items belong in `bun_libuv_sys`. - bun_core::declare_scope!(windowsUserUniqueId, visible); // SetFilePointerEx referenced via the `pub use` at the top of this module. @@ -2066,20 +1917,20 @@ mod tests { /// may read as success. #[test] fn to_e_never_success() { - assert_eq!(Win32Error::FILE_NOT_FOUND.to_e(), E::NOENT); - assert_eq!(UNMAPPED.to_e(), E::UNKNOWN); - assert_eq!(Win32Error::SUCCESS.to_e(), E::UNKNOWN); + assert_eq!(Win32Error::FILE_NOT_FOUND.to_e(), E::ENOENT); + assert_eq!(UNMAPPED.to_e(), E::EUNKNOWN); + assert_eq!(Win32Error::SUCCESS.to_e(), E::EUNKNOWN); // `HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)` unwraps; other HRESULTs do not. - assert_eq!(Win32Error::from_u32(0x8007_0005).to_e(), E::PERM); - assert_eq!(Win32Error::from_u32(0x8000_4005).to_e(), E::UNKNOWN); + assert_eq!(Win32Error::from_u32(0x8007_0005).to_e(), E::EPERM); + assert_eq!(Win32Error::from_u32(0x8000_4005).to_e(), E::EUNKNOWN); assert_eq!(Win32Error::from_u32(5), Win32Error::ACCESS_DENIED); assert_eq!( Error::from_win32(UNMAPPED, Tag::open).get_errno(), - E::UNKNOWN + E::EUNKNOWN ); assert_eq!( Error::from_win32(Win32Error::ACCESS_DENIED, Tag::open).get_errno(), - E::PERM + E::EPERM ); } diff --git a/src/sys_jsc/error_jsc.rs b/src/sys_jsc/error_jsc.rs index 323dc8ee0e89..74953c73bbed 100644 --- a/src/sys_jsc/error_jsc.rs +++ b/src/sys_jsc/error_jsc.rs @@ -74,7 +74,7 @@ pub mod TestingAPIs { } } - /// Exposes libuv -> `bun.sys.E` translation so tests can feed out-of-range + /// Exposes the `UV_E*` -> `E` translation so tests can feed out-of-range /// negative values and verify it does not panic. Windows-only. #[bun_jsc::host_fn] pub fn translate_uv_error_to_e( @@ -94,7 +94,7 @@ pub mod TestingAPIs { #[cfg(windows)] { let code: core::ffi::c_int = arguments[0].to_int32(); - let result = bun_sys::windows::translate_uv_error_to_e(code); + let result = bun_sys::windows::uv_to_e(code).unwrap_or(bun_sys::E::EUNKNOWN); return bun_string_jsc::create_utf8_for_js( global, <&'static str>::from(result).as_bytes(), diff --git a/src/sys_jsc/fd_jsc.rs b/src/sys_jsc/fd_jsc.rs index cd2f6faecd63..e536088f5024 100644 --- a/src/sys_jsc/fd_jsc.rs +++ b/src/sys_jsc/fd_jsc.rs @@ -6,7 +6,7 @@ use core::ffi::c_int; use bun_sys::FdKind; use bun_sys::{Fd, FdExt}; -use crate::{JSGlobalObject, JSValue, JsResult, RangeErrorOptions}; +use crate::{JSGlobalObject, JSValue, JsResult, RangeErrorOptions, SystemErrorJsc as _}; /// Extension trait wiring `to_js` / `from_js` onto `bun_sys::Fd`; /// the `*_jsc` crate provides them as trait methods. @@ -14,7 +14,7 @@ pub trait FdJsc: Sized { fn from_js(value: JSValue) -> Option; fn from_js_validated(value: JSValue, global: &JSGlobalObject) -> JsResult>; fn to_js(self, global: &JSGlobalObject) -> JSValue; - fn to_js_without_making_lib_uv_owned(self) -> JSValue; + fn to_js_without_making_crt_owned(self) -> JSValue; } impl FdJsc for Fd { @@ -28,12 +28,10 @@ impl FdJsc for Fd { return None; } let fd: i32 = i32::try_from(fd64).expect("int cast"); - // On Windows, JS-visible fds are libuv/CRT fds (see `to_js`). libuv fd - // 0/1/2 already map to stdio, so there is no need to substitute the - // cached `.system` HANDLE here — doing so forces every `sys_uv` call to - // round-trip through `Fd::uv()`'s stdio-handle comparison, which panics - // if the process std handle was swapped after startup. - Some(Fd::from_uv(fd)) + // On Windows, JS-visible fds are CRT fds (see `to_js`). CRT fd 0/1/2 + // already map to stdio, so there is no need to substitute the cached + // `.system` HANDLE here. + Some(Fd::from_crt(fd)) } // If a non-number is given, returns null. @@ -70,7 +68,7 @@ impl FdJsc for Fd { let fd: c_int = c_int::try_from(int).expect("int cast"); // See `from_js` above for why stdio fds are not remapped to the cached // `.system` HANDLE on Windows. - Ok(Some(Fd::from_uv(fd))) + Ok(Some(Fd::from_crt(fd))) } /// After calling, the input file descriptor is no longer valid and must not be used. @@ -79,29 +77,22 @@ impl FdJsc for Fd { if !self.is_valid() { return JSValue::js_number_from_int32(-1); } - let uv_owned_fd = match self.make_lib_uv_owned() { + let crt_owned_fd = match self.make_crt_owned_for_syscall() { Ok(fd) => fd, - Err(_) => { - self.close(); - let err_instance = (bun_jsc::SystemError { - message: bun_core::String::static_("EMFILE, too many open files"), - code: bun_core::String::static_("EMFILE"), - ..Default::default() - }) - .to_error_instance(global); - let _ = global.vm().throw_error(global, err_instance); + Err(err) => { + let _ = global.throw_value(err.to_system_error().to_error_instance(global)); return JSValue::ZERO; } }; - JSValue::js_number_from_int32(uv_owned_fd.uv()) + JSValue::js_number_from_int32(crt_owned_fd.crt()) } - /// Convert an FD to a JavaScript number without transferring ownership to libuv. - /// Unlike to_js(), this does not call make_lib_uv_owned() on Windows, so the caller + /// Convert an FD to a JavaScript number without transferring ownership to the C runtime. + /// Unlike to_js(), this does not give the HANDLE a C runtime fd on Windows, so the caller /// retains ownership and must close the FD themselves. /// Returns -1 for invalid file descriptors. - /// On Windows: returns Uint64 for system handles, Int32 for uv file descriptors. - fn to_js_without_making_lib_uv_owned(self) -> JSValue { + /// On Windows: returns Uint64 for system handles, Int32 for CRT file descriptors. + fn to_js_without_making_crt_owned(self) -> JSValue { if !self.is_valid() { return JSValue::js_number_from_int32(-1); } @@ -109,7 +100,7 @@ impl FdJsc for Fd { { return match self.kind() { FdKind::System => JSValue::js_number_from_uint64(self.native() as u64), - FdKind::Uv => JSValue::js_number_from_int32(self.uv()), + FdKind::Crt => JSValue::js_number_from_int32(self.crt()), }; } #[cfg(not(windows))] diff --git a/src/threading/lib.rs b/src/threading/lib.rs index 358de89b7e67..b3af867af395 100644 --- a/src/threading/lib.rs +++ b/src/threading/lib.rs @@ -30,7 +30,7 @@ pub use condition::{Condition, Condvar}; /// `Futex` re-exported as a capitalized module alias so callers can write /// `Futex::wait`, `Futex::wake`, `Futex::Deadline`. pub use futex as Futex; -pub use guarded::Guarded; +pub use guarded::{Guarded, GuardedLock}; pub use mutex::{Mutex, MutexGuard}; pub use reset_event::ResetEvent; pub use rwlock::RwLock; diff --git a/src/threading/work_pool.rs b/src/threading/work_pool.rs index 60f4a0c98fe6..b6cb95498fea 100644 --- a/src/threading/work_pool.rs +++ b/src/threading/work_pool.rs @@ -1,3 +1,4 @@ +use core::sync::atomic::{AtomicU32, Ordering}; use std::sync::OnceLock; use crate::ThreadPool; @@ -131,6 +132,15 @@ macro_rules! owned_task { } static POOL: OnceLock = OnceLock::new(); +static WAITING_POOL: OnceLock = OnceLock::new(); +/// [`WorkPool::owe_write`]. +static WRITES_OWED: AtomicU32 = AtomicU32::new(0); +/// [`WorkPool::write_held_up`]. +static WRITES_HELD_UP: AtomicU32 = AtomicU32::new(0); +/// How long an exit goes on waiting for writes that are held up while none of +/// them ends. It covers the time until a thread, which may have to be created, +/// first runs: hundreds of milliseconds where other processes want the CPUs. +const HELD_UP_GRACE_NS: u64 = 1_000 * 1_000_000; #[cold] fn create() -> ThreadPool { @@ -140,6 +150,16 @@ fn create() -> ThreadPool { }) } +#[cold] +fn create_waiting() -> ThreadPool { + ThreadPool::init(crate::thread_pool::Config { + // Its threads mostly sleep, so a machine with few cores gets as many + // as libuv's pool has. + max_threads: u32::from(bun_core::get_thread_count()).max(4), + stack_size: crate::thread_pool::DEFAULT_THREAD_STACK_SIZE, + }) +} + impl WorkPool { #[inline] pub fn get() -> &'static ThreadPool { @@ -150,6 +170,70 @@ impl WorkPool { Self::get().schedule(Batch::from(task)); } + /// [`schedule`](Self::schedule) for a task that may wait, for as long as + /// that takes, on something outside the process: a name server, the other + /// end of a pipe. Those have threads of their own, so that they cannot + /// take every thread from the tasks that compute or read a disk. + pub fn schedule_wait(task: *mut Task) { + WAITING_POOL + .get_or_init(create_waiting) + .schedule(Batch::from(task)); + } + + /// A task that is about to be scheduled writes to a disk what its caller + /// takes for written. A process that exits at once would end before a + /// thread has got to it (the first task of a pool waits for its thread to + /// be created), so the exit waits: [`wait_for_writes`](Self::wait_for_writes). + pub fn owe_write() { + WRITES_OWED.fetch_add(1, Ordering::Relaxed); + } + + /// The task has written, or will not. + pub fn write_settled() { + if WRITES_OWED.fetch_sub(1, Ordering::Release) == 1 { + crate::Futex::wake(&WRITES_OWED, u32::MAX); + } + } + + /// A write that is owed goes to something other than a disk (a pipe), whose + /// other end takes it when it likes, or never. One that is being read is + /// over in no time, so the exit waits for it too, but not for long. + pub fn write_held_up() { + // Counted by one of the two at any time. + WRITES_HELD_UP.fetch_add(1, Ordering::AcqRel); + Self::write_settled(); + } + + /// As [`write_settled`](Self::write_settled), after + /// [`write_held_up`](Self::write_held_up). + pub fn held_up_write_settled() { + WRITES_HELD_UP.fetch_sub(1, Ordering::AcqRel); + crate::Futex::wake(&WRITES_HELD_UP, u32::MAX); + } + + pub fn wait_for_writes() { + loop { + match WRITES_OWED.load(Ordering::Acquire) { + 0 => {} + owed => { + crate::Futex::wait_forever(&WRITES_OWED, owed); + continue; + } + } + match WRITES_HELD_UP.load(Ordering::Acquire) { + // One that ends owes what was written behind it first. + 0 if WRITES_OWED.load(Ordering::Acquire) == 0 => return, + 0 => {} + held_up => { + if crate::Futex::wait(&WRITES_HELD_UP, held_up, Some(HELD_UP_GRACE_NS)).is_err() + { + return; + } + } + } + } + } + /// Schedule a heap-allocated task by value. The pool takes ownership of /// the `Box`; [`OwnedTask::run`] receives it back on a worker thread. /// Replaces the open-coded `Box::into_raw` + `&raw mut (*p).task` + @@ -175,7 +259,11 @@ impl WorkPool { Self::schedule_owned(Box::new(task)); } - pub fn go(context: C, function: fn(C)) -> Result<(), bun_alloc::AllocError> { + /// `function(context)` on [`schedule_wait`](Self::schedule_wait)'s threads. + pub fn go_wait( + context: C, + function: fn(C), + ) -> Result<(), bun_alloc::AllocError> { // PERF: `function` is stored as a runtime field rather than // monomorphized into the callback — profile if it shows up on a hot path. #[repr(C)] @@ -204,7 +292,7 @@ impl WorkPool { function, })); // SAFETY: task_ is a valid Box-allocated TaskType; .task is its first field. - Self::schedule(unsafe { &raw mut (*task_).task }); + Self::schedule_wait(unsafe { &raw mut (*task_).task }); Ok(()) } } diff --git a/src/tsconfig.json b/src/tsconfig.json index 4d2b492972f9..cd70b9245c06 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -13,14 +13,6 @@ "include": ["**/*.ts", "**/*.tsx", "../packages/bun-types/index.d.ts"], // js and runtime/bake are projects of their own, with settings that only apply there. The `bun init` and // `bun create` templates are users' projects: each has its own tsconfig and dependencies that are not installed - // here (`cli` is a link to `runtime/cli`). The libuv stub generator needs tree-sitter, which is not installed. - "exclude": [ - "js", - "runtime/bake", - "runtime/cli/init", - "runtime/cli/create", - "cli/init", - "cli/create", - "jsc/bindings/libuv" - ] + // here (`cli` is a link to `runtime/cli`). + "exclude": ["js", "runtime/bake", "runtime/cli/init", "runtime/cli/create", "cli/init", "cli/create"] } diff --git a/src/uws/lib.rs b/src/uws/lib.rs index 31ee23f5b94f..b19eb8d16db8 100644 --- a/src/uws/lib.rs +++ b/src/uws/lib.rs @@ -17,8 +17,6 @@ use bun_core::ZStr; // `SocketAddress`) stay defined here; `bun_uws_sys::socket` has lifetime- // bearing variants of the same names that are not yet reconciled. -#[cfg(windows)] -pub use bun_uws_sys::Timer; pub use bun_uws_sys::{ AnyWebSocket, BodyReaderMixin, ConnectingSocket, NewApp, RawWebSocket, Request, WebSocketBehavior, us_socket_stream_buffer_t, us_socket_t, uws_res, @@ -1515,7 +1513,7 @@ pub mod ssl_wrapper { // Loop / InternalLoopData // ═══════════════════════════════════════════════════════════════════════════ // Mirrors `struct us_internal_loop_data_t` (packages/bun-usockets/src/internal/ -// loop_data.h) and `struct us_loop_t` (epoll_kqueue.h / libuv.h). Re-exported +// loop_data.h) and `struct us_loop_t` (epoll_kqueue.h / iocp.h). Re-exported // from bun_uws_sys so `bun_uws::Loop` and `bun_uws_sys::Loop` are the same // type (bun_io's EventLoopCtxVTable is typed against the uws_sys version). pub use bun_uws_sys::loop_::{LoopHandler, us_wakeup_loop}; diff --git a/src/uws_sys/Cargo.toml b/src/uws_sys/Cargo.toml index ed21a02507c9..bc93a43fd724 100644 --- a/src/uws_sys/Cargo.toml +++ b/src/uws_sys/Cargo.toml @@ -25,6 +25,5 @@ bun_http_types.workspace = true [target.'cfg(windows)'.dependencies] bun_windows_sys.workspace = true -bun_libuv_sys.workspace = true bun_paths.workspace = true # no cycle: bun_paths -> {bun_core, bun_alloc}; neither depends on bun_uws_sys diff --git a/src/uws_sys/InternalLoopData.rs b/src/uws_sys/InternalLoopData.rs index 80cf50a7ed03..0a87f5fd7815 100644 --- a/src/uws_sys/InternalLoopData.rs +++ b/src/uws_sys/InternalLoopData.rs @@ -1,7 +1,5 @@ use core::ffi::{c_char, c_int, c_void}; -#[cfg(windows)] -use crate::Timer; use crate::{ConnectingSocket, Loop, SocketGroup, udp, us_socket_t}; /// Layout placeholder for the `mutex` field of `us_internal_loop_data_t`. @@ -24,9 +22,6 @@ bun_opaque::opaque_ffi! { #[repr(C)] pub struct InternalLoopData { - #[cfg(windows)] - pub sweep_timer: *mut Timer, - #[cfg(not(windows))] pub sweep_next_tick_ns: i64, pub sweep_timer_count: i32, pub wakeup_async: *mut us_internal_async, @@ -35,8 +30,6 @@ pub struct InternalLoopData { pub quic_next_tick_us: i64, /// `us_nq_driver_s *` — node:quic's loop driver list. pub(crate) nq_head: *mut c_void, - #[cfg(windows)] - pub quic_timer: *mut Timer, pub iterator: *mut SocketGroup, pub recv_buf: *mut u8, pub send_buf: *mut u8, diff --git a/src/uws_sys/Loop.rs b/src/uws_sys/Loop.rs index 42ebb296a441..68e316ce7afc 100644 --- a/src/uws_sys/Loop.rs +++ b/src/uws_sys/Loop.rs @@ -1,30 +1,29 @@ -use core::ffi::{c_int, c_uint, c_void}; +use core::ffi::{c_int, c_uint}; use core::ptr::NonNull; use crate::InternalLoopData; use crate::Timespec; -#[cfg(windows)] -use bun_libuv_sys as uv; - bun_core::declare_scope!(Loop, visible); -/// A `now_ns` the caller has no reading to share for. The JS park hook takes its own only if -/// it reaches the idle sweep, so passing this costs nothing on the paths that never park. +/// A `now_ns` the caller has no reading to share for: the tick takes its own where it needs one. pub const NOW_NS_UNKNOWN: u64 = 0; -// ───────────────────────────── PosixLoop ───────────────────────────── - // Mirrors C `struct us_loop_t` (packages/bun-usockets/src/internal/eventing/ -// epoll_kqueue.h). The C struct has `alignas(LIBUS_EXT_ALIGNMENT /* 16 */)` on -// both `data` and `ready_polls`; Rust cannot align individual fields, so the +// epoll_kqueue.h, and iocp.h on Windows, which keeps the same leading fields +// in the same order). The C struct has `alignas(LIBUS_EXT_ALIGNMENT /* 16 */)` +// on both `data` and `ready_polls`; Rust cannot align individual fields, so the // struct head gets `#[repr(C, align(16))]` and `ready_polls` is preceded by a // zero-sized align(16) field that forces the same offset rounding the C // `alignas` performs (`libc::epoll_event` is `packed`/align(1) on x86-64 // Linux, so the element type alone would not pad). Layout is verified by the // static assertions below the struct. +// +// The loop is allocated and freed by C and only ever used behind a pointer. +// On Windows the mirror stops after the fields Rust reads; the C struct goes +// on (AFD helper handles, the wait timer, the dequeued completion packets). #[repr(C, align(16))] -pub struct PosixLoop { +pub struct Loop { pub internal_loop_data: InternalLoopData, /// Number of non-fallthrough polls in the loop @@ -37,20 +36,36 @@ pub struct PosixLoop { pub(crate) current_ready_poll: i32, /// Loop's own file descriptor + #[cfg(not(windows))] pub fd: i32, + /// The loop's I/O completion port + #[cfg(windows)] + pub iocp: *mut core::ffi::c_void, + /// Number of polls owned by Bun pub active: u32, - /// Incremented atomically by wakeup(), swapped to 0 before epoll/kqueue. + /// Incremented atomically by wakeup(), swapped to 0 before the loop waits. /// If non-zero, the event loop will return immediately so we can skip the GC safepoint. + #[cfg(not(windows))] pub pending_wakeups: u32, + /// Readiness of the Bun-owned socket poll being dispatched. + #[cfg(windows)] + current_ready_events: c_int, + #[cfg(windows)] + current_ready_error: c_int, + #[cfg(windows)] + current_ready_eof: c_int, + /// Forces `ready_polls` to the next 16-byte boundary, matching the C /// `alignas(LIBUS_EXT_ALIGNMENT)` on `us_loop_t::ready_polls`. + #[cfg(not(windows))] _ready_polls_align: ReadyPollsAlign, /// The list of ready polls + #[cfg(not(windows))] pub(crate) ready_polls: [EventType; 1024], } @@ -58,6 +73,7 @@ pub struct PosixLoop { /// The zero-length array member keeps `improper_ctypes` satisfied (a /// field-less struct is rejected in `extern` signatures) without changing /// size (still 0) or alignment. +#[cfg(not(windows))] #[repr(C, align(16))] struct ReadyPollsAlign { _unused: [u8; 0], @@ -69,21 +85,20 @@ struct ReadyPollsAlign { #[cfg(not(windows))] const _: () = { use core::mem::{align_of, offset_of, size_of}; - assert!(align_of::() == 16); - assert!(offset_of!(PosixLoop, num_polls) == size_of::()); - assert!(offset_of!(PosixLoop, num_ready_polls) == offset_of!(PosixLoop, num_polls) + 4); - assert!(offset_of!(PosixLoop, current_ready_poll) == offset_of!(PosixLoop, num_polls) + 8); - assert!(offset_of!(PosixLoop, fd) == offset_of!(PosixLoop, num_polls) + 12); - assert!(offset_of!(PosixLoop, active) == offset_of!(PosixLoop, num_polls) + 16); - assert!(offset_of!(PosixLoop, pending_wakeups) == offset_of!(PosixLoop, num_polls) + 20); + assert!(align_of::() == 16); + assert!(offset_of!(Loop, num_polls) == size_of::()); + assert!(offset_of!(Loop, num_ready_polls) == offset_of!(Loop, num_polls) + 4); + assert!(offset_of!(Loop, current_ready_poll) == offset_of!(Loop, num_polls) + 8); + assert!(offset_of!(Loop, fd) == offset_of!(Loop, num_polls) + 12); + assert!(offset_of!(Loop, active) == offset_of!(Loop, num_polls) + 16); + assert!(offset_of!(Loop, pending_wakeups) == offset_of!(Loop, num_polls) + 20); assert!( - offset_of!(PosixLoop, ready_polls) - == (offset_of!(PosixLoop, pending_wakeups) + 4).next_multiple_of(16) + offset_of!(Loop, ready_polls) + == (offset_of!(Loop, pending_wakeups) + 4).next_multiple_of(16) ); assert!( - size_of::() - == (offset_of!(PosixLoop, ready_polls) + 1024 * size_of::()) - .next_multiple_of(16) + size_of::() + == (offset_of!(Loop, ready_polls) + 1024 * size_of::()).next_multiple_of(16) ); }; @@ -97,9 +112,24 @@ pub type EventType = libc::kevent64_s; // so ready_polls is `struct kevent[1024]` there. #[cfg(target_os = "freebsd")] pub type EventType = libc::kevent; -// TODO: +/// What a Bun-owned socket poll is told when it is dispatched: `LIBUS_SOCKET_*` +/// readiness bits, whether the poll failed, and whether the peer sent FIN. #[cfg(windows)] -pub type EventType = *mut c_void; +#[derive(Clone, Copy)] +pub struct EventType { + pub events: c_int, + pub error: bool, + pub eof: bool, +} + +#[cfg(windows)] +const _: () = { + use core::mem::offset_of; + // `HANDLE iocp` is pointer-aligned, so it sits 4 bytes later than `int fd`. + assert!(offset_of!(Loop, iocp) == offset_of!(Loop, num_polls) + 16); + assert!(offset_of!(Loop, active) == offset_of!(Loop, num_polls) + 24); + assert!(offset_of!(Loop, current_ready_events) == offset_of!(Loop, num_polls) + 28); +}; /// Loop handler trait with optional `pre`/`post` hooks. Implementors override /// `PRE`/`POST` if they have them. @@ -109,12 +139,7 @@ pub trait LoopHandler { const POST: Option = None; } -// `impl PosixLoop` is posix-only: every method calls into `c::*` whose -// signatures are typed `*mut Loop`, and on Windows `Loop = WindowsLoop` so -// `&mut PosixLoop` does not coerce. Windows callers go through the -// `impl WindowsLoop` block below (same surface, different routing). -#[cfg(not(windows))] -impl PosixLoop { +impl Loop { pub fn update_date(&mut self) { // SAFETY: self is a valid loop pointer unsafe { c::uws_loop_date_header_timer_update(self) }; @@ -132,12 +157,23 @@ impl PosixLoop { /// / `kevent64_s` / `kevent` — all `Copy` in `libc`), so the by-value /// return is a stack copy the caller may borrow across re-entrant handler /// dispatch without aliasing the loop. + #[cfg(not(windows))] #[inline] pub fn current_ready_event(&self) -> EventType { let idx = usize::try_from(self.current_ready_poll).expect("int cast"); self.ready_polls[idx] } + #[cfg(windows)] + #[inline] + pub fn current_ready_event(&self) -> EventType { + EventType { + events: self.current_ready_events, + error: self.current_ready_error != 0, + eof: self.current_ready_eof != 0, + } + } + pub fn inc(&mut self) { bun_core::scoped_log!(Loop, "inc {} + 1 = {}", self.num_polls, self.num_polls + 1); self.num_polls += 1; @@ -230,12 +266,10 @@ impl PosixLoop { unsafe { c::us_quic_loop_flush_if_pending(self) }; } - /// `None` if epoll/kqueue cannot be created (EMFILE). + /// `None` if the loop's kernel objects cannot be created (e.g. EMFILE). pub fn create() -> Option> { - // SAFETY: us_create_loop allocates and returns a new loop; null hint is valid - let p = unsafe { - c::us_create_loop(core::ptr::null_mut(), Some(H::WAKEUP), H::PRE, H::POST, 0) - }; + // SAFETY: us_create_loop allocates and returns a new loop + let p = unsafe { c::us_create_loop(Some(H::WAKEUP), H::PRE, H::POST, 0) }; NonNull::new(p) } @@ -255,9 +289,10 @@ impl PosixLoop { unsafe { c::us_loop_run_bun_tick(self, &raw const timespec, NOW_NS_UNKNOWN) }; } - /// `now_ns` is the CLOCK_MONOTONIC reading the caller took to pick `timespec` (see - /// `timer::All::get_timeout`), reused by the JS park hook's idle-sweep rate limit rather - /// than read again. `NOW_NS_UNKNOWN` if the caller has none to share. + /// `now_ns` is the monotonic-clock reading the caller took to pick `timespec` (see + /// `timer::All::get_timeout`), reused by the tick's idle-sweep rate limit rather + /// than read again; on Windows `timespec` also counts from it when the wait + /// timer is armed. `NOW_NS_UNKNOWN` if the caller has none to share. pub fn tick_with_timeout(&mut self, timespec: Option<&Timespec>, now_ns: u64) { // SAFETY: self is a valid loop pointer unsafe { @@ -298,188 +333,12 @@ impl PosixLoop { /// # Safety /// `this` must have been returned by `us_create_loop`/`uws_get_loop` and not /// yet freed. - pub unsafe fn destroy(this: *mut PosixLoop) { + pub unsafe fn destroy(this: *mut Loop) { // SAFETY: `this` was returned by us_create_loop/uws_get_loop and not yet freed unsafe { c::us_loop_free(this) }; } } -// ───────────────────────────── WindowsLoop ───────────────────────────── - -#[cfg(windows)] -#[repr(C, align(16))] -pub struct WindowsLoop { - pub internal_loop_data: InternalLoopData, - - pub uv_loop: *mut uv::Loop, - pub is_default: c_int, - pub pre: *mut uv::uv_prepare_t, - pub check: *mut uv::uv_check_t, - pub idle_sweep_timer: *mut crate::Timer, -} - -#[cfg(windows)] -impl WindowsLoop { - pub fn should_enable_date_header_timer(&self) -> bool { - self.internal_loop_data.should_enable_date_header_timer() - } - - pub fn get() -> *mut WindowsLoop { - // SAFETY: uv::Loop::get() returns the libuv default loop; uws wraps it - unsafe { c::uws_get_loop_with_native(uv::Loop::get() as *mut c_void) } - } - - pub fn iteration_number(&self) -> u64 { - self.internal_loop_data.iteration_nr - } - - /// Shared borrow of the backing libuv loop. - /// - /// `uv_loop` is a back-reference set once by C `us_create_loop` and never - /// reassigned for the `WindowsLoop`'s lifetime, so projecting `&uv::Loop` - /// from `&self` is sound. Consolidates the `unsafe { (*self.uv_loop).… }` - /// pattern (one `unsafe`, N safe callers). - #[inline] - pub fn uv(&self) -> &uv::Loop { - // SAFETY: `uv_loop` is non-null after `us_create_loop` and remains - // valid for the entire lifetime of `*self`; `&self` bounds the - // returned borrow so it cannot outlive the wrapper. - unsafe { &*self.uv_loop } - } - - /// Exclusive borrow of the backing libuv loop. Used only for the - /// `active_handles` bookkeeping field (Bun-private; libuv itself only - /// reads it inside `uv__loop_alive`). `&mut self` provides exclusivity - /// over the wrapper; the `uv_loop_t` is the per-thread singleton so no - /// other Rust `&mut` to it is live on this thread. - #[inline] - fn uv_mut(&mut self) -> &mut uv::Loop { - // SAFETY: see `uv()` for liveness; `&mut self` is the sole Rust - // borrow path to the wrapper, and the only mutation performed via - // this accessor is the `active_handles` counter. - unsafe { &mut *self.uv_loop } - } - - pub fn add_active(&mut self, val: u32) { - self.uv_mut().add_active(val); - } - - pub fn sub_active(&mut self, val: u32) { - self.uv_mut().sub_active(val); - } - - pub fn is_active(&self) -> bool { - self.uv().is_active() - } - - pub fn wakeup(&mut self) { - // SAFETY: self is a valid loop pointer - unsafe { c::us_wakeup_loop(self) }; - } - - #[inline] - pub fn wake(&mut self) { - self.wakeup(); - } - - /// Signature matches the POSIX impl so callers need no `cfg`. `now_ns` is unused here: on - /// Windows the park hook is driven from `us_loop_run` (libuv.c), which reads libuv's - /// already-refreshed clock via `uv_now` rather than taking one of its own. - pub fn tick_with_timeout(&mut self, _: Option<&Timespec>, _now_ns: u64) { - // SAFETY: self is a valid loop pointer - unsafe { c::us_loop_run(self) }; - } - - pub fn tick_without_idle(&mut self) { - // SAFETY: self is a valid loop pointer - unsafe { c::us_loop_pump(self) }; - } - - pub fn drain_quic_if_necessary(&mut self) { - if !self.internal_loop_data.nq_head.is_null() { - // Full pass with close dispatch deferred to the next loop point. - // SAFETY: self is a valid loop pointer - unsafe { c::us_nq_loop_drain(self) }; - } - if self.internal_loop_data.quic_head.is_null() { - return; - } - // SAFETY: self is a valid loop pointer - unsafe { c::us_quic_loop_flush_if_pending(self) }; - } - - /// `None` if `uv_loop_init` fails (handle exhaustion). - pub fn create() -> Option> { - // SAFETY: us_create_loop allocates and returns a new loop; null hint is valid - let p = unsafe { - c::us_create_loop(core::ptr::null_mut(), Some(H::WAKEUP), H::PRE, H::POST, 0) - }; - NonNull::new(p) - } - - pub fn run(&mut self) { - // SAFETY: self is a valid loop pointer - unsafe { c::us_loop_run(self) }; - } - - // TODO: remove these two aliases - #[inline] - pub fn tick(&mut self) { - self.run(); - } - #[inline] - pub fn wait(&mut self) { - self.run(); - } - - pub fn inc(&mut self) { - self.uv_mut().inc(); - } - - pub fn dec(&mut self) { - self.uv_mut().dec(); - } - - #[inline] - pub fn ref_(&mut self) { - self.inc(); - } - #[inline] - pub fn unref(&mut self) { - self.dec(); - } - - pub fn drain_closed_sockets(&mut self) { - // SAFETY: self is a valid loop pointer - unsafe { c::us_internal_free_closed_sockets(self) }; - } - - pub fn close_all_groups(&mut self) -> bool { - // SAFETY: self is a valid loop pointer - unsafe { c::us_loop_close_all_groups(self) != 0 } - } - - pub fn update_date(&mut self) { - // SAFETY: self is a valid loop pointer - unsafe { c::uws_loop_date_header_timer_update(self) }; - } - - /// # Safety - /// `this` must have been returned by `us_create_loop`/`uws_get_loop_with_native` - /// and not yet freed. - pub unsafe fn destroy(this: *mut WindowsLoop) { - // SAFETY: `this` was returned by us_create_loop/uws_get_loop_with_native and not yet freed - unsafe { c::us_loop_free(this) }; - } -} - -// ───────────────────────────── Loop alias ───────────────────────────── - -#[cfg(windows)] -pub type Loop = WindowsLoop; -#[cfg(not(windows))] -pub type Loop = PosixLoop; - // ───────────────────────────── extern "C" ───────────────────────────── type LoopCb = unsafe extern "C" fn(*mut Loop); @@ -487,15 +346,14 @@ type LoopCb = unsafe extern "C" fn(*mut Loop); mod c { use super::*; - // `Loop` (= `PosixLoop`/`WindowsLoop`) is a sized `#[repr(C)]` mirror of the + // `Loop` is a sized `#[repr(C)]` mirror of the // C struct (NOT an opaque ZST with `UnsafeCell`), so the safe-fn-with-`&mut` // pattern does not apply: `&mut Loop` at the FFI boundary would emit LLVM - // `noalias` over real fields, and the reentrant callees (`us_loop_run`, + // `noalias` over real fields, and the reentrant callees (`us_loop_run_bun_tick`, // `us_loop_close_all_groups`, …) dispatch Rust callbacks that touch the same // loop via `Loop::get()`. Keep all loop-taking decls as raw `*mut Loop`. unsafe extern "C" { pub(super) fn us_create_loop( - hint: *mut c_void, wakeup_cb: Option, pre_cb: Option, post_cb: Option, @@ -504,43 +362,28 @@ mod c { pub(super) fn us_loop_free(loop_: *mut Loop); pub(super) fn us_quic_loop_flush_if_pending(loop_: *mut Loop); pub(super) fn us_nq_loop_drain(loop_: *mut Loop); - pub fn us_loop_run(loop_: *mut Loop); - #[cfg(windows)] - pub(super) fn us_loop_pump(loop_: *mut Loop); pub fn us_wakeup_loop(loop_: *mut Loop); - #[cfg(not(windows))] - pub(super) fn us_loop_run_bun_tick( - loop_: *mut Loop, - timeout_ms: *const Timespec, - now_ns: u64, - ); + pub(super) fn us_loop_run_bun_tick(loop_: *mut Loop, timeout: *const Timespec, now_ns: u64); pub(super) fn us_internal_free_closed_sockets(loop_: *mut Loop); pub(super) fn us_loop_close_all_groups(loop_: *mut Loop) -> c_int; - #[cfg(not(windows))] pub(super) safe fn uws_get_loop() -> *mut Loop; - #[cfg(windows)] - pub(super) fn uws_get_loop_with_native(native: *mut c_void) -> *mut WindowsLoop; pub(super) fn uws_loop_date_header_timer_update(loop_: *mut Loop); } } -// Re-exported raw externs for cross-thread callers (e.g. bun_http's -// `HTTPThread::wakeup`, bun_io's `WindowsWaker`) that hold only a `*mut Loop` -// and MUST NOT form a `&mut Loop` via `Loop::wakeup`/`Loop::run` — see the -// noalias warning on `mod c` above. `us_loop_run` is included because the -// event-loop thread parks inside it while worker threads call -// `us_wakeup_loop` concurrently; routing either through a `&mut self` -// receiver would create two live `&mut Loop` to the same singleton (UB). -pub use c::{us_loop_run, us_wakeup_loop}; +// Raw extern for cross-thread callers (e.g. bun_http's `HTTPThread::wakeup`) +// that hold only a `*mut Loop` and must not form a `&mut Loop` via +// `Loop::wakeup` while the loop's own thread holds one: see the noalias note +// on `mod c`. +pub use c::us_wakeup_loop; unsafe extern "C" { // safe: no args; frees this thread's lazily-created uws loop if it exists. safe fn bun_free_loop_at_thread_exit(); } -/// Frees this thread's uws loop (its socket groups, timers and — where uSockets -/// created it — the native loop). Call when a thread that ran a uws loop (a -/// Worker) exits, after everything registered on the loop is gone. On Windows -/// the loop borrows the thread's libuv loop; close that afterwards. +/// Frees this thread's uws loop (its socket groups and the native loop). Call +/// when a thread that ran a uws loop (a Worker) exits, after everything +/// registered on the loop is gone. pub fn free_thread_loop() { bun_free_loop_at_thread_exit() } diff --git a/src/uws_sys/Response.rs b/src/uws_sys/Response.rs index 453eae7c61eb..907e3c42a937 100644 --- a/src/uws_sys/Response.rs +++ b/src/uws_sys/Response.rs @@ -365,7 +365,7 @@ impl Response { { // on windows uSockets exposes SOCKET (uintptr-sized) as a pointer // value; tag kind=system via `from_system` (masks bit 63) so - // `INVALID_SOCKET` (~0) doesn't decode as kind=uv. + // `INVALID_SOCKET` (~0) doesn't decode as kind=crt. return Fd::from_system( c::uws_res_get_native_handle(Self::ssl_flag(), self.as_raw()) as *mut core::ffi::c_void, diff --git a/src/uws_sys/SocketContext.rs b/src/uws_sys/SocketContext.rs index 18f5223a9266..12db417fcfee 100644 --- a/src/uws_sys/SocketContext.rs +++ b/src/uws_sys/SocketContext.rs @@ -37,12 +37,12 @@ fn stat_for_digest(path: &bun_core::ZStr) -> Option<[i64; 3]> { fn stat_for_digest(path: &bun_core::ZStr) -> Option<[i64; 3]> { use bun_windows_sys as fs; use bun_windows_sys::FILETIME; - // Spec parity: `bun.sys.stat` on Windows is libuv `uv_fs_stat`, which opens - // via `CreateFileW` *without* `FILE_FLAG_OPEN_REPARSE_POINT` and therefore + // Parity with `bun_sys::stat`, which on Windows opens via + // `CreateFileW` *without* `FILE_FLAG_OPEN_REPARSE_POINT` and therefore // follows symlinks to the target. `GetFileAttributesExW` does NOT follow // reparse points — it would return the link's own mtime/size and miss an - // in-place cert rotation behind a symlink (stale SSL_CTX served). Match - // libuv: open query-only, `GetFileInformationByHandle`, close. + // in-place cert rotation behind a symlink (stale SSL_CTX served). So: + // open query-only, `GetFileInformationByHandle`, close. // // `bun_core::to_w_path_normalized` lives above this crate, so widen // inline: UTF-8→UTF-16LE (≤ input.len() code units), normalize `/`→`\`, @@ -81,11 +81,10 @@ fn stat_for_digest(path: &bun_core::ZStr) -> Option<[i64; 3]> { let ft: FILETIME = data.ftLastWriteTime; // FILETIME = 100ns ticks since 1601-01-01. Feed raw ticks split as // `[sec_field, nsec_field, size]` — the digest only needs *some* - // deterministic encoding of mtime, not the libuv POSIX-epoch split the - // deleted `__bun_uws_stat_file` produced. The SSL-context cache keyed on - // this digest is in-memory process-lifetime only (no on-disk - // persistence), so cross-version byte-compat of the key is - // irrelevant; only stability *within* a process matters. + // deterministic encoding of mtime, not a POSIX-epoch split. The + // SSL-context cache keyed on this digest is in-memory process-lifetime + // only (no on-disk persistence), so cross-version byte-compat of the key + // is irrelevant; only stability *within* a process matters. let ticks = (u64::from(ft.dwHighDateTime) << 32) | u64::from(ft.dwLowDateTime); let size = (u64::from(data.nFileSizeHigh) << 32) | u64::from(data.nFileSizeLow); Some([ diff --git a/src/uws_sys/Timer.rs b/src/uws_sys/Timer.rs deleted file mode 100644 index c6c8aed41875..000000000000 --- a/src/uws_sys/Timer.rs +++ /dev/null @@ -1,67 +0,0 @@ -use core::ffi::{c_uint, c_void}; -use core::mem::size_of; -use core::ptr::NonNull; - -use crate::Loop; - -bun_core::declare_scope!(uws, visible); - -// Windows (libuv) only. Use `JSC.EventLoopTimer` everywhere else. -bun_opaque::opaque_ffi! { pub struct Timer; } - -impl Timer { - pub fn create(loop_: &mut Loop, _ptr: T) -> NonNull { - // SAFETY: `loop_` is a valid loop pointer. - let t = unsafe { - us_create_timer( - loop_, - 0, - c_uint::try_from(size_of::()).expect("int cast"), - ) - }; - NonNull::new(t).unwrap_or_else(|| { - panic!( - "us_create_timer: returned null: {}", - std::io::Error::last_os_error().raw_os_error().unwrap_or(0) - ) - }) - } - - pub fn set( - &mut self, - ptr: T, - cb: Option, - ms: i32, - repeat_ms: i32, - ) { - // SAFETY: ext storage was allocated with size_of::() in create(); - // caller guarantees T matches. - unsafe { - us_timer_set(self, cb, ms, repeat_ms); - let value_ptr = us_timer_ext(self); - (value_ptr.cast::()).write(ptr); - } - } - - pub unsafe fn close(this: *mut Self) { - bun_core::scoped_log!(uws, "Timer.deinit()"); - // SAFETY: `this` is a live timer handle; us_timer_close frees it. - unsafe { us_timer_close(this, FALLTHROUGH as i32) }; - } -} - -unsafe extern "C" { - pub(crate) fn us_create_timer( - loop_: *mut Loop, - fallthrough: i32, - ext_size: c_uint, - ) -> *mut Timer; - pub(crate) safe fn us_timer_ext(timer: &mut Timer) -> *mut *mut c_void; - pub(crate) fn us_timer_close(timer: *mut Timer, fallthrough: i32); - pub(crate) safe fn us_timer_set( - timer: &mut Timer, - cb: Option, - ms: i32, - repeat_ms: i32, - ); -} diff --git a/src/uws_sys/iocp.rs b/src/uws_sys/iocp.rs new file mode 100644 index 000000000000..18a04c59582e --- /dev/null +++ b/src/uws_sys/iocp.rs @@ -0,0 +1,151 @@ +//! The Windows loop's completion port, for I/O that Bun owns (pipes, the +//! console, process exit). Mirrors `packages/bun-usockets/src/internal/ +//! eventing/iocp.h`. + +use core::ffi::{c_int, c_void}; + +pub use bun_windows_sys::{HANDLE, OVERLAPPED}; + +use crate::Loop; + +pub type CompleteFn = unsafe extern "C" fn(*mut Loop, *mut Op); + +/// `struct us_iocp_op`: anything that completes through the loop's port. +/// +/// It is the first field of the struct that owns it, and its address is what +/// the kernel is given as the `OVERLAPPED` (or as the wait packet's context), +/// so the dequeued packet leads back to it. The owner must stay allocated, at +/// the same address, until `complete` has run for every packet it has in +/// flight: cancelling the I/O or closing the handle only asks for the +/// completion, it does not wait for it. +/// +/// For overlapped I/O the NTSTATUS is `overlapped.Internal` and the byte count +/// is `overlapped.InternalHigh`. +#[repr(C)] +pub struct Op { + pub overlapped: OVERLAPPED, + pub complete: CompleteFn, + /// The loop's, while the op is on its way through `us_iocp_op_ready`. + next_ready: *mut Op, +} + +impl Op { + pub const fn new(complete: CompleteFn) -> Self { + Self { + overlapped: OVERLAPPED { + Internal: 0, + InternalHigh: 0, + Offset: 0, + OffsetHigh: 0, + hEvent: core::ptr::null_mut(), + }, + complete, + next_ready: core::ptr::null_mut(), + } + } + + /// Before the OVERLAPPED goes to the kernel again: no result, offset 0. + /// `event` is null for an operation that completes through the port. + #[inline] + pub fn reset(&mut self, event: HANDLE) { + self.overlapped = OVERLAPPED { + Internal: 0, + InternalHigh: 0, + Offset: 0, + OffsetHigh: 0, + hEvent: event, + }; + } + + /// The NTSTATUS of a completed overlapped operation. + #[inline] + pub fn status(&self) -> i32 { + self.overlapped.Internal as i32 + } + + /// The byte count of a completed overlapped operation. + #[inline] + pub fn bytes_transferred(&self) -> usize { + self.overlapped.InternalHigh + } +} + +bun_opaque::opaque_ffi! { + /// `struct us_iocp_wait`: delivers an [`Op`] once when a handle is signalled. + pub struct Wait; + /// `struct us_internal_afd_poll`: socket readiness for a poll Bun owns. + pub struct SocketPoll; +} + +/// `struct us_iocp_starved`: something that could not start waiting for what +/// it serves. While linked, `retry` runs at the start of every tick, before +/// the tick looks at the port, and the loop keeps ticking for it. `retry` may +/// unlink its own node and must not run an owner's callback. +#[repr(C)] +pub struct Starved { + pub retry: unsafe extern "C" fn(*mut Starved), + prev: *mut Starved, + next: *mut Starved, +} + +impl Starved { + pub const fn new(retry: unsafe extern "C" fn(*mut Starved)) -> Self { + Self { + retry, + prev: core::ptr::null_mut(), + next: core::ptr::null_mut(), + } + } +} + +pub const SOCKET_READABLE: c_int = 1; +pub const SOCKET_WRITABLE: c_int = 2; + +unsafe extern "C" { + /// The loop's completion port. Associate only handles Bun created: the + /// association belongs to the file object, so it is shared with every other + /// process holding the same handle and cannot be undone while I/O is pending. + pub fn us_loop_iocp(loop_: *mut Loop) -> HANDLE; + + /// Call once the kernel has accepted an op that will post a packet (the + /// call returned pending, or succeeded on a handle without + /// `FILE_SKIP_COMPLETION_PORT_ON_SUCCESS`). The loop balances it when the + /// packet is dequeued, and does not close the port while any are out. + pub fn us_iocp_op_submitted(loop_: *mut Loop); + /// The packet of a submitted op will not come: a thread that cannot be + /// stopped was left to finish the op by itself, and frees it. + pub fn us_iocp_op_abandoned(loop_: *mut Loop); + /// `op`'s `complete` runs from the loop's next tick, before that tick + /// takes packets from the port. Loop thread only; counted like a packet. + pub fn us_iocp_op_ready(loop_: *mut Loop, op: *mut Op); + + /// `starved` is not linked, and stays at its address until unlinked. Loop + /// thread only. + pub fn us_iocp_starved_link(loop_: *mut Loop, starved: *mut Starved); + /// `starved` is linked. Loop thread only. + pub fn us_iocp_starved_unlink(loop_: *mut Loop, starved: *mut Starved); + + pub fn us_iocp_wait_create(loop_: *mut Loop) -> *mut Wait; + /// 0, or -1 if the wait could not be registered. + pub fn us_iocp_wait_start(wait: *mut Wait, handle: HANDLE, op: *mut Op) -> c_int; + /// Nonzero if the wait was removed before it fired: `complete` will not + /// run. Zero if its packet was already dequeued or is about to be. + pub fn us_iocp_wait_stop(wait: *mut Wait) -> c_int; + pub fn us_iocp_wait_free(wait: *mut Wait); + + /// Readiness for a socket; `owner` is the tagged pointer handed back + /// through `Bun__internal_dispatch_ready_poll`, with the readiness in + /// `Loop::current_ready_event`. Null (with the Winsock error set) on failure. + pub fn us_iocp_poll_socket( + loop_: *mut Loop, + owner: *mut c_void, + socket: usize, + events: c_int, + ) -> *mut SocketPoll; + pub fn us_iocp_poll_socket_change(loop_: *mut Loop, poll: *mut SocketPoll, events: c_int); + /// The poll is released once the kernel is done with it; `owner` is not used again. + pub fn us_iocp_poll_socket_stop(loop_: *mut Loop, poll: *mut SocketPoll); + + /// `WSAStartup`, once. Anything that reaches ws2_32 without going through uSockets calls it first. + pub safe fn us_internal_winsock_ensure(); +} diff --git a/src/uws_sys/lib.rs b/src/uws_sys/lib.rs index 4deb071c8632..9f86f7a9ed9f 100644 --- a/src/uws_sys/lib.rs +++ b/src/uws_sys/lib.rs @@ -416,6 +416,8 @@ pub mod h2; pub mod h3; #[path = "InternalLoopData.rs"] pub mod internal_loop_data; +#[cfg(windows)] +pub mod iocp; #[path = "ListenSocket.rs"] pub mod listen_socket; #[path = "Loop.rs"] @@ -434,10 +436,6 @@ pub mod socket_group; pub mod socket_kind; #[path = "thunk.rs"] pub mod thunk; -// libuv only — use `bun_event_loop::EventLoopTimer` elsewhere. -#[cfg(windows)] -#[path = "Timer.rs"] -pub mod timer; #[path = "udp.rs"] pub mod udp; #[path = "us_socket_t.rs"] @@ -466,7 +464,7 @@ pub mod fault_inject { /// `us_internal_init_loop_ssl_data`. pub const SSL_LOOP_BUFFER: c_int = 10; /// Not a syscall: poll registration in `us_poll_start_rc` - /// (`uv_poll_init_socket` on Windows, `EPOLL_CTL_ADD` / `kevent` on + /// (`afd_poll_create` on Windows, `EPOLL_CTL_ADD` / `kevent` on /// epoll/kqueue). pub const POLL_START: c_int = 11; /// Not a syscall: the JS `Buffer` allocated for a TLS session/keylog @@ -501,23 +499,18 @@ pub use socket::{ // ───────────────────────────── re-exports ──────────────────────────────────── -pub use internal_loop_data::InternalLoopData; -#[cfg(windows)] -pub use loop_::WindowsLoop; -pub use loop_::{Loop, NOW_NS_UNKNOWN}; -pub use socket_kind::SocketKind; -#[cfg(windows)] -pub use timer::Timer; - pub use body_reader_mixin::BodyReaderMixin; pub use connecting_socket::ConnectingSocket; +pub use internal_loop_data::InternalLoopData; pub use listen_socket::ListenSocket; +pub use loop_::{Loop, NOW_NS_UNKNOWN}; pub use request::{AnyRequest, Request}; pub use response::c::uws_res; pub use response::{AnyResponse, SocketAddress, WebSocketUpgradeContext}; pub use socket_context::BunSocketContextOptions; pub use socket_group::ConnectResult; pub use socket_group::SocketGroup; +pub use socket_kind::SocketKind; pub use us_socket::{CloseCode, QueuedInput, UsIoVec, us_socket_stream_buffer_t, us_socket_t}; pub use web_socket::{AnyWebSocket, RawWebSocket, WebSocketBehavior}; diff --git a/src/uws_sys/libuwsockets.cpp b/src/uws_sys/libuwsockets.cpp index d9dc129a943d..6c5d2c7b545b 100644 --- a/src/uws_sys/libuwsockets.cpp +++ b/src/uws_sys/libuwsockets.cpp @@ -355,20 +355,6 @@ extern "C" } } - void uws_app_run(int ssl, uws_app_t *app) - { - if (ssl) - { - uWS::SSLApp *uwsApp = (uWS::SSLApp *)app; - uwsApp->run(); - } - else - { - uWS::App *uwsApp = (uWS::App *)app; - uwsApp->run(); - } - } - void uws_app_close(int ssl, uws_app_t *app) { if (ssl) @@ -1501,11 +1487,6 @@ size_t uws_req_get_header(uws_req_t *res, const char *lower_case_header, { return (struct us_loop_t *)uWS::Loop::get(); } - struct us_loop_t *uws_get_loop_with_native(void *existing_native_loop) - { - return (struct us_loop_t *)uWS::Loop::get(existing_native_loop); - } - void uws_res_uncork(int ssl, uws_res_r res) { if (ssl) @@ -1795,8 +1776,7 @@ __attribute__((callback (corker, ctx))) } } - // A thread that ran a uws loop (a Worker) is exiting; free its loop. On Windows the loop sits on - // the thread's libuv loop, which the caller closes after this returns. + // A thread that ran a uws loop (a Worker) is exiting; free its loop. extern "C" void bun_free_loop_at_thread_exit() { uWS::Loop::freeLoopAtThreadExit(); } diff --git a/src/uws_sys/socket.rs b/src/uws_sys/socket.rs index f9faf3445070..1fddad4b4093 100644 --- a/src/uws_sys/socket.rs +++ b/src/uws_sys/socket.rs @@ -389,7 +389,7 @@ impl NewSocketHandler { } /// Write `data` and pass `file_descriptor` over the socket via SCM_RIGHTS. - /// POSIX-only — Windows IPC fd passing goes through libuv pipes instead. + /// POSIX-only — Windows IPC runs over a named pipe, not a socket. /// /// LAYERING: takes the raw POSIX fd (`c_int`) rather than `bun_sys::Fd` — /// `bun_uws_sys` sits below `bun_sys`; callers extract `.native()` at the diff --git a/src/uws_sys/us_socket_t.rs b/src/uws_sys/us_socket_t.rs index db3284f2564f..c44e4dc14a9f 100644 --- a/src/uws_sys/us_socket_t.rs +++ b/src/uws_sys/us_socket_t.rs @@ -438,7 +438,7 @@ impl us_socket_t { let raw = c::us_socket_get_fd(self); // LIBUS_SOCKET_DESCRIPTOR is `c_int` on POSIX, `SOCKET` (`usize`) on // Windows. Tag kind=system explicitly — `from_native` would store raw - // bits verbatim and mis-tag `INVALID_SOCKET` (~0) as kind=uv. + // bits verbatim and mis-tag `INVALID_SOCKET` (~0) as kind=crt. #[cfg(windows)] { Fd::from_system(raw as *mut core::ffi::c_void) diff --git a/src/windows_sys/externs.rs b/src/windows_sys/externs.rs index b84cbfd3a9bf..cda3abeaead9 100644 --- a/src/windows_sys/externs.rs +++ b/src/windows_sys/externs.rs @@ -1,6 +1,6 @@ //! Raw Win32 extern fn declarations + tier-0 Win32 typedefs. -//! `bun_sys::windows` re-exports FROM here (see the layering doc). This crate is a tier-0 leaf: it depends on nothing above -//! `libuv_sys`. +//! `bun_sys::windows` re-exports FROM here (see the layering doc). This crate is a tier-0 leaf with no workspace +//! dependencies. //! //! `#[link(name = "...")]` on every `extern` block is wrapped in //! `#[cfg_attr(windows, ...)]`. This crate is depended on unconditionally (not @@ -22,7 +22,6 @@ pub type DWORD = c_ulong; type DWORD_PTR = usize; pub type UINT = c_uint; pub type ULONG = c_ulong; -pub type LONG = c_long; pub type LARGE_INTEGER = i64; pub type WCHAR = u16; pub type CHAR = c_char; @@ -78,11 +77,7 @@ pub struct FILETIME { } // ────────────────────────────────────────────────────────────────────────── -// Win32 POD structs shared by `bun_libuv_sys` (uv/win.h embeds) and -// `bun_sys::windows`. Single source of truth. -// All derive Clone+Copy: libuv embeds them in `uv_req_s`/`uv_tty_s`/ -// `uv_fs_s` which themselves derive Copy, so non-Copy here would break -// the derive chain. +// Win32 POD structs. Single source of truth. // ────────────────────────────────────────────────────────────────────────── /// `OVERLAPPED` (`minwinbase.h`) — 32 bytes / align 8 on x64. @@ -96,19 +91,6 @@ pub struct OVERLAPPED { pub hEvent: HANDLE, } -/// `RTL_CRITICAL_SECTION` (`winnt.h`) — 40 bytes / align 8 on x64. -/// libuv aliases this as `uv_mutex_t`. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct CRITICAL_SECTION { - pub DebugInfo: *mut c_void, - pub LockCount: LONG, - pub RecursionCount: LONG, - pub OwningThread: HANDLE, - pub LockSemaphore: HANDLE, - pub SpinCount: ULONG_PTR, -} - /// `WIN32_FIND_DATAW` (`minwinbase.h`) — 592 bytes / align 4. #[repr(C)] #[derive(Clone, Copy)] @@ -182,14 +164,13 @@ pub struct INPUT_RECORD { pub Event: INPUT_RECORD_Event, } -// Layout pins: a typo in any of the above is a silent ABI break across the -// libuv embed boundary; assert the authoritative Windows-x64 sizes. Gated on +// Layout pins: a typo in any of the above is a silent ABI break; assert the +// authoritative Windows-x64 sizes. Gated on // `windows` (not just pointer width) because `DWORD = c_ulong` is 4 bytes // under LLP64 but 8 under LP64, so the sizes differ on a Linux cross-check. #[cfg(all(windows, target_pointer_width = "64"))] const _: () = { assert!(core::mem::size_of::() == 32); - assert!(core::mem::size_of::() == 40); assert!(core::mem::size_of::() == 592); assert!(core::mem::size_of::() == 20); }; @@ -227,9 +208,6 @@ pub struct WIN32_FILE_ATTRIBUTE_DATA { pub nFileSizeLow: DWORD, } -/// `GET_FILEEX_INFO_LEVELS` — enum(u32) selecting `GetFileAttributesExW` payload. -pub(crate) type GET_FILEEX_INFO_LEVELS = u32; - /// `FILE_INFO_BY_HANDLE_CLASS` (`winbase.h`), as a bare `u32`. pub type FILE_INFO_BY_HANDLE_CLASS = u32; @@ -311,17 +289,67 @@ pub const GENERIC_WRITE: ACCESS_MASK = 0x4000_0000; // File-specific access rights (`winnt.h`). pub const FILE_READ_DATA: ACCESS_MASK = 0x0001; pub const FILE_LIST_DIRECTORY: ACCESS_MASK = 0x0001; +pub const FILE_WRITE_DATA: ACCESS_MASK = 0x0002; pub const FILE_APPEND_DATA: ACCESS_MASK = 0x0004; pub const FILE_READ_EA: ACCESS_MASK = 0x0008; +pub(crate) const FILE_WRITE_EA: ACCESS_MASK = 0x0010; pub const FILE_TRAVERSE: ACCESS_MASK = 0x0020; pub const FILE_READ_ATTRIBUTES: ACCESS_MASK = 0x0080; +pub const FILE_WRITE_ATTRIBUTES: ACCESS_MASK = 0x0100; +pub const WRITE_DAC: ACCESS_MASK = 0x0004_0000; +pub const FILE_GENERIC_READ: ACCESS_MASK = + READ_CONTROL | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE; +pub const FILE_GENERIC_WRITE: ACCESS_MASK = READ_CONTROL + | FILE_WRITE_DATA + | FILE_WRITE_ATTRIBUTES + | FILE_WRITE_EA + | FILE_APPEND_DATA + | SYNCHRONIZE; // `CreateFileW` dwCreationDisposition (`winbase.h`). +pub const CREATE_NEW: DWORD = 1; +pub const CREATE_ALWAYS: DWORD = 2; pub const OPEN_EXISTING: DWORD = 3; +pub const OPEN_ALWAYS: DWORD = 4; +pub const TRUNCATE_EXISTING: DWORD = 5; // `CreateFileW` dwFlagsAndAttributes (`winbase.h`). +pub const FILE_FLAG_FIRST_PIPE_INSTANCE: DWORD = 0x0008_0000; +pub const FILE_FLAG_OPEN_REPARSE_POINT: DWORD = 0x0020_0000; pub const FILE_FLAG_BACKUP_SEMANTICS: DWORD = 0x0200_0000; +pub const FILE_FLAG_SEQUENTIAL_SCAN: DWORD = 0x0800_0000; +pub const FILE_FLAG_NO_BUFFERING: DWORD = 0x2000_0000; pub const FILE_FLAG_OVERLAPPED: DWORD = 0x4000_0000; +pub const FILE_FLAG_WRITE_THROUGH: DWORD = 0x8000_0000; + +// `SetFilePointerEx` dwMoveMethod. +pub const FILE_CURRENT: DWORD = 1; + +// `SetHandleInformation` dwMask. +pub const HANDLE_FLAG_INHERIT: DWORD = 0x0000_0001; + +// Reparse tags and FSCTLs (`winnt.h` / `winioctl.h`). +pub const IO_REPARSE_TAG_MOUNT_POINT: DWORD = 0xA000_0003; +pub const IO_REPARSE_TAG_SYMLINK: DWORD = 0xA000_000C; +pub const IO_REPARSE_TAG_APPEXECLINK: DWORD = 0x8000_001B; +pub const FSCTL_SET_REPARSE_POINT: DWORD = 0x0009_00A4; +pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x0009_00A8; +pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024; + +/// `FSCTL_PIPE_PEEK` (`ntifs.h`): what `PeekNamedPipe` sends. The output is a +/// [`FILE_PIPE_PEEK_BUFFER`] followed by the peeked bytes; with no room for +/// all of those the call returns `STATUS_BUFFER_OVERFLOW` and the fixed fields +/// are valid. +pub const FSCTL_PIPE_PEEK: DWORD = 0x0011_400C; + +/// The fixed part of `FILE_PIPE_PEEK_BUFFER` (`ntifs.h`). +#[repr(C)] +pub struct FILE_PIPE_PEEK_BUFFER { + pub NamedPipeState: ULONG, + pub ReadDataAvailable: ULONG, + pub NumberOfMessages: ULONG, + pub MessageLength: ULONG, +} // Reparse tags (`winnt.h`). `IsReparseTagNameSurrogate` == bit 29: the reparse // point names another filesystem entity (symlink, mount point). Non-surrogate @@ -337,6 +365,7 @@ pub const PIPE_ACCESS_OUTBOUND: DWORD = 0x0000_0002; pub const PIPE_TYPE_BYTE: DWORD = 0x0000_0000; pub const PIPE_READMODE_BYTE: DWORD = 0x0000_0000; pub const PIPE_WAIT: DWORD = 0x0000_0000; +pub const PIPE_REJECT_REMOTE_CLIENTS: DWORD = 0x0000_0008; /// `CreateSymbolicLinkW` dwFlags (`winbase.h`). pub const SYMBOLIC_LINK_FLAG_DIRECTORY: DWORD = 0x1; @@ -370,6 +399,63 @@ pub struct FILE_DIRECTORY_INFORMATION { pub FileName: [WCHAR; 1], } +/// `FILE_ID_FULL_DIR_INFORMATION` (`ntifs.h`) — `NtQueryDirectoryFile` record +/// carrying the 64-bit file id. `FileName` is a flexible array. +#[repr(C)] +pub struct FILE_ID_FULL_DIR_INFORMATION { + pub NextEntryOffset: ULONG, + pub FileIndex: ULONG, + pub CreationTime: LARGE_INTEGER, + pub LastAccessTime: LARGE_INTEGER, + pub LastWriteTime: LARGE_INTEGER, + pub ChangeTime: LARGE_INTEGER, + pub EndOfFile: LARGE_INTEGER, + pub AllocationSize: LARGE_INTEGER, + pub FileAttributes: ULONG, + pub FileNameLength: ULONG, + pub EaSize: ULONG, + pub FileId: LARGE_INTEGER, + pub FileName: [WCHAR; 1], +} + +/// `FILE_STAT_BASIC_INFORMATION` (`winbase.h`, Windows 11 24H2 SDK) — output +/// of `GetFileInformationByName(.., FileStatBasicByNameInfo)`. +#[repr(C)] +pub struct FILE_STAT_BASIC_INFORMATION { + pub FileId: LARGE_INTEGER, + pub CreationTime: LARGE_INTEGER, + pub LastAccessTime: LARGE_INTEGER, + pub LastWriteTime: LARGE_INTEGER, + pub ChangeTime: LARGE_INTEGER, + pub AllocationSize: LARGE_INTEGER, + pub EndOfFile: LARGE_INTEGER, + pub FileAttributes: ULONG, + pub ReparseTag: ULONG, + pub NumberOfLinks: ULONG, + pub DeviceType: ULONG, + pub DeviceCharacteristics: ULONG, + pub Reserved: ULONG, + pub VolumeSerialNumber: LARGE_INTEGER, + pub FileId128: [u8; 16], +} +/// `FILE_INFO_BY_NAME_CLASS::FileStatBasicByNameInfo`. +pub const FileStatBasicByNameInfo: c_int = 3; +/// `GetFileInformationByName` (Windows 11 24H2+; resolve with `GetProcAddress`). +pub type GetFileInformationByNameFn = unsafe extern "system" fn( + FileName: LPCWSTR, + FileInformationClass: c_int, + FileInfoBuffer: *mut c_void, + FileInfoBufferSize: ULONG, +) -> BOOL; + +#[cfg(windows)] +const _: () = { + assert!(core::mem::size_of::() == 104); + assert!(core::mem::offset_of!(FILE_STAT_BASIC_INFORMATION, VolumeSerialNumber) == 80); + assert!(core::mem::offset_of!(FILE_ID_FULL_DIR_INFORMATION, FileId) == 72); + assert!(core::mem::offset_of!(FILE_ID_FULL_DIR_INFORMATION, FileName) == 80); +}; + /// `FILE_INFORMATION_CLASS` (`wdm.h`) — selector for `NtQuery*` / /// `NtSetInformationFile`. Newtype-over-u32 so unmapped values round-trip. #[repr(transparent)] @@ -378,9 +464,15 @@ pub struct FILE_INFORMATION_CLASS(pub u32); impl FILE_INFORMATION_CLASS { pub const FileDirectoryInformation: Self = Self(1); pub const FileBasicInformation: Self = Self(4); + pub const FileStandardInformation: Self = Self(5); pub const FileDispositionInformation: Self = Self(13); + #[cfg(windows)] + pub const FilePositionInformation: Self = Self(14); + #[cfg(windows)] + pub const FileModeInformation: Self = Self(16); pub const FileAllInformation: Self = Self(18); pub const FileEndOfFileInformation: Self = Self(20); + pub const FileIdFullDirectoryInformation: Self = Self(38); pub const FileDispositionInformationEx: Self = Self(64); } @@ -393,6 +485,7 @@ pub struct FS_INFORMATION_CLASS(pub u32); impl FS_INFORMATION_CLASS { pub const FileFsVolumeInformation: Self = Self(1); pub const FileFsDeviceInformation: Self = Self(4); + pub const FileFsFullSizeInformation: Self = Self(7); } /// `FILE_STANDARD_INFORMATION` (`wdm.h`). @@ -437,6 +530,17 @@ pub struct FILE_FS_DEVICE_INFORMATION { pub Characteristics: ULONG, } +/// `FILE_FS_FULL_SIZE_INFORMATION` (`ntifs.h`). +#[repr(C)] +pub struct FILE_FS_FULL_SIZE_INFORMATION { + pub TotalAllocationUnits: LARGE_INTEGER, + /// What the caller's quota leaves of `ActualAvailableAllocationUnits`. + pub CallerAvailableAllocationUnits: LARGE_INTEGER, + pub ActualAvailableAllocationUnits: LARGE_INTEGER, + pub SectorsPerAllocationUnit: ULONG, + pub BytesPerSector: ULONG, +} + /// `FILE_FS_VOLUME_INFORMATION` (`ntifs.h`). `VolumeLabel` is variable-length; /// with a fixed-size buffer the call returns `STATUS_BUFFER_OVERFLOW` and the /// fixed fields are still populated. @@ -460,6 +564,7 @@ const _: () = { assert!(core::mem::offset_of!(FILE_ALL_INFORMATION, CurrentByteOffset) == 80); assert!(core::mem::offset_of!(FILE_ALL_INFORMATION, FileNameLength) == 96); assert!(core::mem::size_of::() == 8); + assert!(core::mem::size_of::() == 32); assert!(core::mem::size_of::() == 24); assert!(core::mem::offset_of!(FILE_FS_VOLUME_INFORMATION, VolumeSerialNumber) == 8); }; @@ -502,6 +607,9 @@ pub struct FILE_RENAME_INFORMATION_EX { } // `FILE_DISPOSITION_INFORMATION_EX.Flags` bits (winnt.h). +pub const FILE_DISPOSITION_DELETE: ULONG = 0x0000_0001; +pub const FILE_DISPOSITION_POSIX_SEMANTICS: ULONG = 0x0000_0002; +pub const FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE: ULONG = 0x0000_0010; // `FILE_RENAME_INFORMATION_EX.Flags` bits (winnt.h). pub const FILE_RENAME_REPLACE_IF_EXISTS: ULONG = 0x0000_0001; @@ -530,6 +638,7 @@ pub struct GetFinalPathNameByHandleFormat { } impl FILE_INFORMATION_CLASS { + pub const FileRenameInformation: Self = Self(10); pub const FileRenameInformationEx: Self = Self(65); } @@ -738,7 +847,23 @@ pub mod ntdll { Length: ULONG, FsInformationClass: FS_INFORMATION_CLASS, ) -> NTSTATUS; - pub fn NtClose(Handle: HANDLE) -> NTSTATUS; + /// `NtFsControlFile` (`ntifs.h`). On a handle opened for overlapped + /// I/O it can return `STATUS_PENDING`, and then only `Event` (or the + /// completion port) says when `IoStatusBlock` and the output are valid. + pub fn NtFsControlFile( + FileHandle: HANDLE, + Event: HANDLE, + ApcRoutine: *mut c_void, + ApcContext: *mut c_void, + IoStatusBlock: *mut IO_STATUS_BLOCK, + FsControlCode: ULONG, + InputBuffer: *mut c_void, + InputBufferLength: ULONG, + OutputBuffer: *mut c_void, + OutputBufferLength: ULONG, + ) -> NTSTATUS; + /// A bad handle is `STATUS_INVALID_HANDLE`. + pub safe fn NtClose(Handle: HANDLE) -> NTSTATUS; // ── futex (`WaitOnAddress`) — used by `bun_threading::Futex` ── // Linked from ntdll instead of `API-MS-Win-Core-Synch-l1-2-0.dll` @@ -756,7 +881,7 @@ pub mod ntdll { /// `RtlExitUserProcess` (ntdll). The Win32 `ExitProcess` forwards to /// this; the freestanding `bun_shim_impl` calls it directly to avoid /// linking kernel32 in the standalone PE. - pub fn RtlExitUserProcess(ExitStatus: u32) -> !; + pub safe fn RtlExitUserProcess(ExitStatus: u32) -> !; /// `NtQueryInformationProcess` (`winternl.h`). With /// `ProcessBasicInformation` (0), fills a [`PROCESS_BASIC_INFORMATION`]. @@ -800,10 +925,6 @@ pub mod user32 {} /// `advapi32` namespace (subset placeholder; fill in as needed). pub mod advapi32 {} -// `bun.windows.libuv` is exposed from the higher-tier `bun_sys::windows` -// module, NOT here — `bun_windows_sys` is the leaf Win32 externs crate and -// must not depend on `bun_libuv_sys` (would invert the tier ordering). - // ────────────────────────────────────────────────────────────────────────── // kernel32 namespace (subset). // ────────────────────────────────────────────────────────────────────────── @@ -879,7 +1000,8 @@ pub mod kernel32 { lpOverlapped: *mut c_void, ) -> BOOL; pub fn LoadLibraryExW(lpLibFileName: LPCWSTR, hFile: HANDLE, dwFlags: DWORD) -> HMODULE; - pub fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: *mut DWORD) -> BOOL; + /// A bad handle fails with `ERROR_INVALID_HANDLE`. + pub safe fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: &mut DWORD) -> BOOL; /// `FlushFileBuffers` — fsync(2)-equivalent for HANDLE-backed files. pub fn FlushFileBuffers(hFile: HANDLE) -> BOOL; /// `CreateProcessW` (`processthreadsapi.h`). @@ -911,23 +1033,26 @@ pub mod kernel32 { lpConsoleScreenBufferInfo: *mut CONSOLE_SCREEN_BUFFER_INFO, ) -> BOOL; /// `FillConsoleOutputAttribute` (`wincon.h`). - pub fn FillConsoleOutputAttribute( + pub safe fn FillConsoleOutputAttribute( hConsoleOutput: HANDLE, wAttribute: WORD, nLength: DWORD, dwWriteCoord: COORD, - lpNumberOfAttrsWritten: *mut DWORD, + lpNumberOfAttrsWritten: &mut DWORD, ) -> BOOL; /// `FillConsoleOutputCharacterW` (`wincon.h`). - pub fn FillConsoleOutputCharacterW( + pub safe fn FillConsoleOutputCharacterW( hConsoleOutput: HANDLE, cCharacter: WCHAR, nLength: DWORD, dwWriteCoord: COORD, - lpNumberOfCharsWritten: *mut DWORD, + lpNumberOfCharsWritten: &mut DWORD, ) -> BOOL; /// `SetConsoleCursorPosition` (`wincon.h`). - pub fn SetConsoleCursorPosition(hConsoleOutput: HANDLE, dwCursorPosition: COORD) -> BOOL; + pub safe fn SetConsoleCursorPosition( + hConsoleOutput: HANDLE, + dwCursorPosition: COORD, + ) -> BOOL; /// `ExitThread` (`processthreadsapi.h`). No preconditions; terminates /// the calling thread. pub safe fn ExitThread(dwExitCode: DWORD) -> !; @@ -949,16 +1074,24 @@ pub mod kernel32 { ) -> *mut c_void; /// `SetUnhandledExceptionFilter` (`errhandlingapi.h`). Runs after all /// frame-based (SEH) handlers have declined. - pub fn SetUnhandledExceptionFilter( + pub safe fn SetUnhandledExceptionFilter( lpTopLevelExceptionFilter: Option i32>, ) -> Option i32>; /// `GetModuleHandleW` (`libloaderapi.h`). `lpModuleName == null` returns /// the base address of the calling process's executable image. pub fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE; /// `RemoveVectoredExceptionHandler` (`errhandlingapi.h`). - pub fn RemoveVectoredExceptionHandler(Handle: *mut c_void) -> u32; + /// `Handle` is the cookie `AddVectoredExceptionHandler` returned; a stale + /// one is rejected with 0. + pub safe fn RemoveVectoredExceptionHandler(Handle: *mut c_void) -> u32; } // Re-export externs declared at the crate root so `kernel32::Foo` resolves. + #[cfg(windows)] + pub use super::{ + CancelIoEx, CompareStringOrdinal, CreateEventW, GetCurrentProcessId, GetCurrentThreadId, + GetEnvironmentVariableW, GetFileType, GetShortPathNameW, GetTickCount64, SetEvent, + TerminateProcess, WaitForMultipleObjects, WaitForSingleObject, + }; pub use super::{ CreateFileW, GetCurrentDirectoryW, GetFileAttributesW, GetSystemDirectoryW, GetSystemInfo, SYSTEM_INFO, SetCurrentDirectoryW, SetFilePointerEx, @@ -1008,6 +1141,7 @@ impl NTSTATUS { /// `NtSetInformationFile(FileDispositionInformation)`. pub const CANNOT_DELETE: NTSTATUS = NTSTATUS(0xC000_0121); pub const NOT_IMPLEMENTED: NTSTATUS = NTSTATUS(0xC000_0002); + pub const BUFFER_OVERFLOW: NTSTATUS = NTSTATUS(0x8000_0005); pub const NO_MORE_FILES: NTSTATUS = NTSTATUS(0x8000_0006); pub const NO_SUCH_FILE: NTSTATUS = NTSTATUS(0xC000_000F); /// `STATUS_TIMEOUT` — returned by `NtWaitForSingleObject` / @@ -1180,7 +1314,9 @@ impl Win32Error { pub const LOCK_VIOLATION: Win32Error = Win32Error(33); pub const HANDLE_EOF: Win32Error = Win32Error(38); pub const HANDLE_DISK_FULL: Win32Error = Win32Error(39); + pub const NOT_READY: Win32Error = Win32Error(21); pub const NOT_SUPPORTED: Win32Error = Win32Error(50); + pub const BAD_NET_NAME: Win32Error = Win32Error(67); pub const NETNAME_DELETED: Win32Error = Win32Error(64); pub const FILE_EXISTS: Win32Error = Win32Error(80); pub const CANNOT_MAKE: Win32Error = Win32Error(82); @@ -1206,11 +1342,14 @@ impl Win32Error { pub const PIPE_BUSY: Win32Error = Win32Error(231); pub const NO_DATA: Win32Error = Win32Error(232); pub const PIPE_NOT_CONNECTED: Win32Error = Win32Error(233); + pub const PIPE_CONNECTED: Win32Error = Win32Error(535); pub const DIRECTORY: Win32Error = Win32Error(267); pub const EA_TABLE_FULL: Win32Error = Win32Error(277); pub const DELETE_PENDING: Win32Error = Win32Error(303); pub const ELEVATION_REQUIRED: Win32Error = Win32Error(740); pub const OPERATION_ABORTED: Win32Error = Win32Error(995); + #[cfg(windows)] + pub const IO_PENDING: Win32Error = Win32Error(997); pub const NOACCESS: Win32Error = Win32Error(998); pub const INVALID_FLAGS: Win32Error = Win32Error(1004); pub const END_OF_MEDIA: Win32Error = Win32Error(1100); @@ -1240,6 +1379,7 @@ impl Win32Error { pub const CANT_RESOLVE_FILENAME: Win32Error = Win32Error(1921); pub const NOT_CONNECTED: Win32Error = Win32Error(2250); pub const IO_REISSUE_AS_CACHED: Win32Error = Win32Error(3950); + pub const NOT_A_REPARSE_POINT: Win32Error = Win32Error(4390); pub const INVALID_REPARSE_DATA: Win32Error = Win32Error(4392); // — WSA pseudo-variants — @@ -1269,7 +1409,13 @@ impl Win32Error { pub const WSAETIMEDOUT: Win32Error = Win32Error(10060); pub const WSAECONNREFUSED: Win32Error = Win32Error(10061); pub const WSAEHOSTUNREACH: Win32Error = Win32Error(10065); + #[cfg(windows)] + pub const WSATYPE_NOT_FOUND: Win32Error = Win32Error(10109); pub const WSAHOST_NOT_FOUND: Win32Error = Win32Error(11001); + #[cfg(windows)] + pub const WSATRY_AGAIN: Win32Error = Win32Error(11002); + #[cfg(windows)] + pub const WSANO_RECOVERY: Win32Error = Win32Error(11003); pub const WSANO_DATA: Win32Error = Win32Error(11004); pub const WSA_QOS_RESERVED_PETYPE: Win32Error = Win32Error(11031); @@ -1318,7 +1464,7 @@ impl Win32Error { // that directly (T0 must not depend on T1). } -pub type LPDWORD = *mut DWORD; +pub(crate) type LPDWORD = *mut DWORD; pub type HPCON = *mut c_void; #[cfg_attr(windows, link(name = "kernel32"))] @@ -1426,9 +1572,9 @@ unsafe extern "system" { lpBuffer: LPCWSTR, // [out] ) -> DWORD; - pub fn CreateJobObjectA( + pub fn CreateJobObjectW( lpJobAttributes: *mut c_void, // [in, optional] - lpName: LPCSTR, // [in, optional] + lpName: LPCWSTR, // [in, optional] ) -> HANDLE; pub fn AssignProcessToJobObject( @@ -1760,9 +1906,10 @@ unsafe extern "system" { /// No preconditions; returns 0 on failure. pub safe fn SetConsoleOutputCP(wCodePageID: UINT) -> BOOL; - pub fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: *mut DWORD) -> BOOL; + /// A handle that is not a console's fails with `ERROR_INVALID_HANDLE`. + pub safe fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: &mut DWORD) -> BOOL; - pub fn SetConsoleMode(hConsoleHandle: HANDLE, dwMode: DWORD) -> BOOL; + pub safe fn SetConsoleMode(hConsoleHandle: HANDLE, dwMode: DWORD) -> BOOL; pub fn InitializeProcThreadAttributeList( lpAttributeList: *mut u8, @@ -1781,7 +1928,7 @@ unsafe extern "system" { lpReturnSize: *mut usize, // [in, optional] ) -> BOOL; - pub fn IsProcessInJob(process: HANDLE, job: HANDLE, result: *mut BOOL) -> BOOL; + pub safe fn IsProcessInJob(process: HANDLE, job: HANDLE, result: &mut BOOL) -> BOOL; pub fn CreatePseudoConsole( size: COORD, @@ -1844,13 +1991,208 @@ unsafe extern "system" { dwFlags: ULONG, ) -> BOOL; - pub fn GetFileAttributesExW( + pub fn SetFileAttributesW(lpFileName: LPCWSTR, dwFileAttributes: DWORD) -> BOOL; + + pub fn RemoveDirectoryW(lpPathName: LPCWSTR) -> BOOL; + + /// Returns the required size (including the NUL) when `lpBuffer` is too + /// small, else the length written (excluding the NUL); 0 on failure. + pub fn GetFullPathNameW( lpFileName: LPCWSTR, - fInfoLevelId: GET_FILEEX_INFO_LEVELS, - lpFileInformation: LPVOID, + nBufferLength: DWORD, + lpBuffer: LPWSTR, + lpFilePart: *mut LPWSTR, + ) -> DWORD; + + pub fn SetFileTime( + hFile: HANDLE, + lpCreationTime: *const FILETIME, + lpLastAccessTime: *const FILETIME, + lpLastWriteTime: *const FILETIME, ) -> BOOL; + + pub fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: *mut FILETIME); + + /// A new handle to the file `hOriginalFile` has open, with its own access, + /// share mode and flags; `INVALID_HANDLE_VALUE` on failure. + pub fn ReOpenFile( + hOriginalFile: HANDLE, + dwDesiredAccess: DWORD, + dwShareMode: DWORD, + dwFlagsAndAttributes: DWORD, + ) -> HANDLE; + + pub fn DeviceIoControl( + hDevice: HANDLE, + dwIoControlCode: DWORD, + lpInBuffer: LPVOID, + nInBufferSize: DWORD, + lpOutBuffer: LPVOID, + nOutBufferSize: DWORD, + lpBytesReturned: *mut DWORD, + lpOverlapped: *mut OVERLAPPED, + ) -> BOOL; + + pub fn ConnectNamedPipe(hNamedPipe: HANDLE, lpOverlapped: *mut OVERLAPPED) -> BOOL; + + /// No pointer arguments; a bad handle fails with `ERROR_INVALID_HANDLE`. + pub safe fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) -> BOOL; + + pub fn GetStartupInfoW(lpStartupInfo: *mut STARTUPINFOW); } unsafe extern "C" { - pub fn windows_enable_stdio_inheritance(); + /// `c-bindings.cpp`: sets `HANDLE_FLAG_INHERIT` on the three standard handles. + pub safe fn windows_enable_stdio_inheritance(); +} + +// ────────────────────────────────────────────────────────────────────────── +// Declarations only Windows code names. Behind `cfg(windows)` so a lint run +// on another target does not report them as unused. +// ────────────────────────────────────────────────────────────────────────── +#[cfg(windows)] +pub use windows_only::*; + +#[cfg(windows)] +mod windows_only { + use super::*; + + type LONG = c_long; + + /// `GetFileAttributesW` failure value. + pub const INVALID_FILE_ATTRIBUTES: DWORD = DWORD::MAX; + + // `GetFileType` results (`winbase.h`). + pub const FILE_TYPE_UNKNOWN: DWORD = 0x0000; + pub const FILE_TYPE_DISK: DWORD = 0x0001; + pub const FILE_TYPE_CHAR: DWORD = 0x0002; + pub const FILE_TYPE_PIPE: DWORD = 0x0003; + pub const FILE_TYPE_REMOTE: DWORD = 0x8000; + + // `GetConsoleMode` / `SetConsoleMode` flags (`consoleapi.h`): input handles. + pub const ENABLE_PROCESSED_INPUT: DWORD = 0x0001; + pub const ENABLE_LINE_INPUT: DWORD = 0x0002; + pub const ENABLE_ECHO_INPUT: DWORD = 0x0004; + pub const ENABLE_VIRTUAL_TERMINAL_INPUT: DWORD = 0x0200; + // Screen-buffer handles. + pub const ENABLE_PROCESSED_OUTPUT: DWORD = 0x0001; + pub const ENABLE_WRAP_AT_EOL_OUTPUT: DWORD = 0x0002; + pub const ENABLE_VIRTUAL_TERMINAL_PROCESSING: DWORD = 0x0004; + + /// `INPUT_RECORD::EventType` (`wincon.h`). + pub const KEY_EVENT: WORD = 0x0001; + /// `KEY_EVENT_RECORD::dwControlKeyState` (`wincon.h`). + pub const LEFT_CTRL_PRESSED: DWORD = 0x0008; + + // `CreateProcessW` dwCreationFlags (`winbase.h`). + pub const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x0000_0400; + pub const EXTENDED_STARTUPINFO_PRESENT: DWORD = 0x0008_0000; + + /// `UpdateProcThreadAttribute` Attribute (`winbase.h`). + pub const PROC_THREAD_ATTRIBUTE_JOB_LIST: usize = 0x0002_000D; + + // `JOBOBJECT_BASIC_LIMIT_INFORMATION::LimitFlags` (`winnt.h`). + pub const JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION: DWORD = 0x0000_0400; + pub const JOB_OBJECT_LIMIT_BREAKAWAY_OK: DWORD = 0x0000_0800; + pub const JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK: DWORD = 0x0000_1000; + pub const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: DWORD = 0x0000_2000; + + /// `WaitForSingleObject` / `WaitForMultipleObjects`: the first handle is signalled. + pub const WAIT_OBJECT_0: DWORD = 0; + + /// `CompareStringOrdinal` result. + pub const CSTR_EQUAL: c_int = 2; + + /// `LoadLibraryExW` dwFlags. + pub const LOAD_WITH_ALTERED_SEARCH_PATH: DWORD = 0x0000_0008; + + /// Registry key handle (`winreg.h`). + pub type HKEY = *mut c_void; + /// `(HKEY)(ULONG_PTR)((LONG)0x80000002)`: a predefined handle (the `LONG` + /// sign-extends), not a pointer. + pub const HKEY_LOCAL_MACHINE: HKEY = 0x8000_0002_u32 as i32 as isize as HKEY; + + /// `GUID` (`guiddef.h`). + #[repr(C)] + #[derive(Clone, Copy, PartialEq, Eq)] + pub struct GUID { + pub Data1: u32, + pub Data2: u16, + pub Data3: u16, + pub Data4: [u8; 8], + } + + /// `RTL_OSVERSIONINFOW` (`winnt.h`). + #[repr(C)] + pub struct OSVERSIONINFOW { + pub dwOSVersionInfoSize: DWORD, + pub dwMajorVersion: DWORD, + pub dwMinorVersion: DWORD, + pub dwBuildNumber: DWORD, + pub dwPlatformId: DWORD, + pub szCSDVersion: [WCHAR; 128], + } + + #[cfg_attr(windows, link(name = "kernel32"))] + unsafe extern "system" { + /// A bad handle is `WAIT_FAILED`. + pub safe fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD; + pub fn WaitForMultipleObjects( + nCount: DWORD, + lpHandles: *const HANDLE, + bWaitAll: BOOL, + dwMilliseconds: DWORD, + ) -> DWORD; + pub fn CreateEventW( + lpEventAttributes: *mut SECURITY_ATTRIBUTES, + bManualReset: BOOL, + bInitialState: BOOL, + lpName: LPCWSTR, + ) -> HANDLE; + pub fn SetEvent(hEvent: HANDLE) -> BOOL; + pub fn CancelIoEx(hFile: HANDLE, lpOverlapped: *mut OVERLAPPED) -> BOOL; + /// No preconditions. + pub safe fn GetCurrentThreadId() -> DWORD; + /// No preconditions. + pub safe fn GetCurrentProcessId() -> DWORD; + /// No preconditions. + pub safe fn GetTickCount64() -> u64; + /// A bad handle is `FILE_TYPE_UNKNOWN` with the last error set. + pub safe fn GetFileType(hFile: HANDLE) -> DWORD; + /// A bad handle fails with `ERROR_INVALID_HANDLE`. + pub safe fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) -> BOOL; + pub fn GetEnvironmentVariableW(lpName: LPCWSTR, lpBuffer: LPWSTR, nSize: DWORD) -> DWORD; + pub fn GetShortPathNameW( + lpszLongPath: LPCWSTR, + lpszShortPath: LPWSTR, + cchBuffer: DWORD, + ) -> DWORD; + pub fn CompareStringOrdinal( + lpString1: *const WCHAR, + cchCount1: c_int, + lpString2: *const WCHAR, + cchCount2: c_int, + bIgnoreCase: BOOL, + ) -> c_int; + } + + #[cfg_attr(windows, link(name = "advapi32"))] + unsafe extern "system" { + /// Returns a Win32 error code, `ERROR_SUCCESS` on success. + pub fn RegGetValueW( + hkey: HKEY, + lpSubKey: LPCWSTR, + lpValue: LPCWSTR, + dwFlags: DWORD, + pdwType: *mut DWORD, + pvData: *mut c_void, + pcbData: *mut DWORD, + ) -> LONG; + } + + #[cfg_attr(windows, link(name = "ntdll"))] + unsafe extern "system" { + /// Fills `lpVersionInformation`, whose `dwOSVersionInfoSize` the caller set. + pub safe fn RtlGetVersion(lpVersionInformation: &mut OSVERSIONINFOW) -> NTSTATUS; + } } diff --git a/src/windows_sys/lib.rs b/src/windows_sys/lib.rs index 371be082ce24..5748f94211ef 100644 --- a/src/windows_sys/lib.rs +++ b/src/windows_sys/lib.rs @@ -9,10 +9,6 @@ pub mod externs; // `bun_sys::windows`'s `pub use bun_windows_sys::Foo;` re-exports resolve. pub use externs::*; -// `bun.windows.libuv` is NOT re-exported here — this is the bottom-tier Win32 -// externs crate and must stay leaf. The `bun.windows.libuv` alias lives in the -// higher-tier `bun_sys::windows` module (`pub use bun_libuv_sys as libuv`). - /// `NTSTATUS` value namespace (`ntstatus.h`). The `NTSTATUS` newtype carries /// these as associated consts, but `bun_sys::windows` glob-imports them as /// bare match patterns (`use bun_windows_sys::ntstatus::*`); associated consts @@ -21,7 +17,6 @@ pub mod ntstatus { use super::externs::NTSTATUS; pub const SUCCESS: NTSTATUS = NTSTATUS::SUCCESS; pub const ACCESS_DENIED: NTSTATUS = NTSTATUS::ACCESS_DENIED; - pub const INVALID_PARAMETER: NTSTATUS = NTSTATUS::INVALID_PARAMETER; pub const OBJECT_NAME_INVALID: NTSTATUS = NTSTATUS::OBJECT_NAME_INVALID; pub const FILE_DELETED: NTSTATUS = NTSTATUS::FILE_DELETED; pub const DELETE_PENDING: NTSTATUS = NTSTATUS::DELETE_PENDING; diff --git a/test/bake/bake-harness.ts b/test/bake/bake-harness.ts index df3335261457..e9f4f52fc799 100644 --- a/test/bake/bake-harness.ts +++ b/test/bake/bake-harness.ts @@ -1084,10 +1084,11 @@ export class Client extends EventEmitter { /** * Node on Windows sometimes aborts inside its own teardown after the exit the * harness requested (a `uv_async_send` on a closing handle). The fixture never - * exits with 3 or 9 itself, so that abort is a clean exit. + * exits with 3 or STATUS_STACK_BUFFER_OVERRUN (what `abort()` ends a process + * with) itself, so that abort is a clean exit. */ async #abortedInTeardown(exitWasRequested: boolean): Promise { - if (!isWindows || !exitWasRequested || (this.exitCode !== 3 && this.exitCode !== 9)) return false; + if (!isWindows || !exitWasRequested || (this.exitCode !== 3 && this.exitCode !== 0xc0000409)) return false; // The assertion is on stderr; let both pipes drain before reading the lines. if (this.output.closes < 2) await EventEmitter.once(this.output, "close"); return this.output.lines.some(line => line.includes("UV_HANDLE_CLOSING")); diff --git a/test/bake/dev-and-prod.test.ts b/test/bake/dev-and-prod.test.ts index 26c769669420..d3b4bc742106 100644 --- a/test/bake/dev-and-prod.test.ts +++ b/test/bake/dev-and-prod.test.ts @@ -264,12 +264,11 @@ devTest("hmr handles rapid consecutive edits", { // // Writing IDENTICAL content N times forces same-sourceMapId duplicates // on every platform. Use synchronous writeFileSync so truncate + write - // + close happen back-to-back on the calling thread; the previous - // `Bun.write` here is two separate async libuv ops on Windows with a - // JS-thread round-trip in between, giving the bundler a multi-ms window - // to read 0 bytes. writeFileSync shrinks that window to microseconds — - // usually enough, but the residual race is why allowUnlimitedReloads is - // set above. + // + close happen back-to-back on the calling thread; on Windows + // `Bun.write` runs them as a work-pool job and settles through the + // event loop. writeFileSync keeps the window in which the bundler can + // read 0 bytes to microseconds — usually enough, but the residual race + // is why allowUnlimitedReloads is set above. const target = dev.join("index.ts"); const rapidContent = hmrSelfAcceptingModule("render rapid"); for (let i = 0; i < 10; i++) { diff --git a/test/bake/fixtures/deinitialization/test.ts b/test/bake/fixtures/deinitialization/test.ts index 5addb57ce4c4..3cd947ad336e 100644 --- a/test/bake/fixtures/deinitialization/test.ts +++ b/test/bake/fixtures/deinitialization/test.ts @@ -148,7 +148,7 @@ beforeAll(async () => { }); test("baseline: stopped server wrapper collects", () => { - // libuv platforms may materialize both Debug and non-Debug prototypes. + // The bound allows prototypes only: one, or the Debug and the non-Debug one. expect(serverWrapperBaseline).toBeLessThanOrEqual(2); }); diff --git a/test/cli/install/bun-run.test.ts b/test/cli/install/bun-run.test.ts index 25675aca221d..0b1651073918 100644 --- a/test/cli/install/bun-run.test.ts +++ b/test/cli/install/bun-run.test.ts @@ -1088,7 +1088,7 @@ describe.concurrent("bun run", () => { // BUN-3MAQ: guards the user-visible contract that a >32,767-u16 environment // block reaches a .bunx child intact. CreateProcessW with // CREATE_UNICODE_ENVIRONMENT has no documented block-size limit. This does - // not assert which spawn path (fast .bunx shim vs. libuv fallback) was + // not assert which spawn path (fast .bunx shim vs. spawn fallback) was // taken, since both are correct; it fails only if the child loses env data // or the process crashes. POSIX hits E2BIG first, so Windows-only. it.if(isWindows)( diff --git a/test/cli/run/filter-workspace.test.ts b/test/cli/run/filter-workspace.test.ts index 96a8ac82abe4..14be1d83a6e9 100644 --- a/test/cli/run/filter-workspace.test.ts +++ b/test/cli/run/filter-workspace.test.ts @@ -1,7 +1,9 @@ import { spawnSync } from "bun"; import { describe, expect, test } from "bun:test"; import { bunEnv, bunExe, isLinux, isWindows, tempDir, tempDirWithFiles } from "harness"; -import { existsSync, symlinkSync } from "node:fs"; +import { spawn as nodeSpawn } from "node:child_process"; +import { once } from "node:events"; +import { existsSync, readFileSync, symlinkSync } from "node:fs"; import { setTimeout as sleep } from "node:timers/promises"; import { constants } from "os"; import { join } from "path"; @@ -1031,9 +1033,9 @@ describe("selectors", () => { // #20319: on Windows, `bun --filter` / `bun run --parallel` spawn each script // as `bun exec "