From a7fef27922134ad96cd755af34f832828a1ab1d1 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Tue, 15 Sep 2026 12:04:28 -0700 Subject: [PATCH 01/85] Remove libuv on Windows and drive the event loop from an I/O completion port Bun no longer links libuv. On Windows the event loop is a uSockets backend (packages/bun-usockets/src/eventing/iocp.c) built on a completion port: sockets are polled through AFD, listening sockets accept with overlapped AcceptEx, timers use a high-resolution waitable timer, and process exit, events and console input arrive through wait completion packets. Pipes, the console and files (src/io/windows) use overlapped I/O on the same port. A pipe handle that cannot do overlapped I/O (an inherited synchronous stdin) is read on a thread of its own. Process spawning (src/spawn_sys/windows) calls CreateProcessW directly, with job objects and pseudoconsoles. node:fs, fs.watch, dns, os, tty and signal handling call Win32/NT directly. For N-API modules Windows now behaves like POSIX: the same small set of uv_* functions is polyfilled and every other uv_* export aborts with a message. --- .claude/docs/landing-prs.md | 2 +- .claude/skills/rust-system-calls/SKILL.md | 2 +- CLAUDE.md | 2 +- Cargo.lock | 26 +- Cargo.toml | 2 - LICENSE.md | 2 +- docs/project/license.mdx | 2 +- docs/runtime/child-process.mdx | 2 +- mordant-baseline.toml | 1 - package.json | 2 +- packages/bun-types/bun.d.ts | 6 +- packages/bun-usockets/src/bsd.c | 23 +- packages/bun-usockets/src/context.c | 33 +- packages/bun-usockets/src/crypto/openssl.c | 5 +- .../bun-usockets/src/eventing/epoll_kqueue.c | 20 +- packages/bun-usockets/src/eventing/iocp.c | 1696 +++++++++ packages/bun-usockets/src/eventing/libuv.c | 560 --- .../bun-usockets/src/internal/eventing/iocp.h | 143 + .../src/internal/eventing/libuv.h | 52 - .../bun-usockets/src/internal/fault_inject.h | 4 +- packages/bun-usockets/src/internal/internal.h | 32 +- .../bun-usockets/src/internal/loop_data.h | 15 +- .../src/internal/networking/bsd.h | 2 + packages/bun-usockets/src/libusockets.h | 39 +- packages/bun-usockets/src/loop.c | 103 +- packages/bun-usockets/src/quic.c | 30 +- packages/bun-usockets/src/socket.c | 34 +- packages/bun-uws/src/HttpContext.h | 32 +- packages/bun-uws/src/Loop.h | 20 +- .../win-poll-abort-with-disconnect.patch | 51 - .../win-poll-rearm-before-callback.patch | 36 - scripts/build/bun.ts | 2 +- scripts/build/deps/README.md | 6 +- scripts/build/deps/index.ts | 3 - scripts/build/deps/libuv.ts | 93 - scripts/build/deps/mimalloc.ts | 2 +- scripts/build/deps/nodejs-headers.ts | 5 +- scripts/build/fetch-cli.ts | 2 +- scripts/build/flags.ts | 6 +- scripts/build/source.ts | 27 +- scripts/glob-sources.ts | 4 +- scripts/orderfile/functrace-windows.c | 2 +- scripts/run-clang-format.sh | 1 - src/bun_core/Global.rs | 2 +- src/bun_core/heap.rs | 5 +- src/bun_core/lib.rs | 16 +- src/bun_core/output.rs | 3 +- src/bun_core/util.rs | 143 +- src/bun_core/windows_sys.rs | 58 +- src/bundler/BundleThread.rs | 25 +- src/cares_sys/Cargo.toml | 3 +- src/cares_sys/c_ares.rs | 53 +- src/cares_sys/lib.rs | 8 +- src/collections/hive_array.rs | 2 +- src/collections/vec_ext.rs | 33 +- src/crash_handler/lib.rs | 4 +- src/dns/lib.rs | 7 +- src/errno/Cargo.toml | 1 - src/errno/darwin_errno.rs | 8 +- src/errno/freebsd_errno.rs | 12 +- src/errno/lib.rs | 8 +- src/errno/linux_errno.rs | 4 +- src/errno/uv_codes.rs | 293 ++ src/errno/windows_errno.rs | 37 +- src/event_loop/AnyEventLoop.rs | 24 - src/event_loop/ConcurrentTask.rs | 8 - src/event_loop/README.md | 25 +- src/event_loop/SpawnSyncEventLoop.rs | 229 +- src/event_loop/lib.rs | 4 +- src/install/Cargo.toml | 3 + src/install/PackageInstall.rs | 33 +- src/install/PackageManager.rs | 6 +- src/install/PackageManager/patchPackage.rs | 3 +- .../PackageManager/security_scanner.rs | 208 +- src/install/error.rs | 2 +- src/install/extract_tarball.rs | 8 +- src/install/git_runner.rs | 78 +- src/install/isolated_install/Symlinker.rs | 2 +- src/install/lifecycle_script_runner.rs | 174 +- src/install/windows-shim/Cargo.toml | 2 +- src/install/windows-shim/bun_shim_impl.rs | 2 +- src/install/windows-shim/main.rs | 2 +- src/io/Cargo.toml | 3 + src/io/MaxBuf.rs | 11 +- src/io/PipeReader.rs | 923 ++--- src/io/PipeWriter.rs | 1302 +++---- src/io/keep_alive.rs | 14 +- src/io/lib.rs | 346 +- src/io/openForWriting.rs | 8 +- src/io/pipes.rs | 23 +- src/io/posix_event_loop.rs | 228 +- src/io/source.rs | 604 +--- src/io/windows/README.md | 88 + src/io/windows/file.rs | 415 +++ src/io/windows/ipc_frame.rs | 225 ++ src/io/windows/mod.rs | 310 ++ src/io/windows/pipe.rs | 2312 ++++++++++++ src/io/windows/pipe_server.rs | 396 +++ src/io/windows/sys.rs | 137 + src/io/windows/tty.rs | 1101 ++++++ src/io/windows/tty_input.rs | 705 ++++ src/io/windows/tty_output.rs | 1047 ++++++ src/io/windows_event_loop.rs | 254 -- src/js/internal-for-testing.ts | 2 +- src/js/node/child_process.ts | 2 +- src/js/node/tty.ts | 8 +- src/jsc/Debugger.rs | 92 +- src/jsc/PosixSignalHandle.rs | 82 +- src/jsc/RuntimeTranspilerCache.rs | 12 +- src/jsc/VirtualMachine.rs | 142 +- src/jsc/array_buffer.rs | 2 +- src/jsc/bindings/BunJSCEventLoop.cpp | 52 +- src/jsc/bindings/BunProcess.cpp | 172 +- src/jsc/bindings/BunProcess.h | 5 + .../BunProcessReportObjectWindows.cpp | 27 +- src/jsc/bindings/BunWindowsProcess.cpp | 150 + src/jsc/bindings/BunWindowsProcess.h | 43 + src/jsc/bindings/BunWindowsSignals.h | 25 + src/jsc/bindings/BunWinternl.h | 8 + src/jsc/bindings/OsBinding.cpp | 247 +- src/jsc/bindings/OsBinding.h | 48 + src/jsc/bindings/ProcessBindingConstants.cpp | 12 +- src/jsc/bindings/ProcessBindingTTYWrap.cpp | 74 +- src/jsc/bindings/ProcessBindingUV.cpp | 25 +- src/jsc/bindings/ProcessBindingUV.h | 3 + src/jsc/bindings/ZigGlobalObject.h | 11 - src/jsc/bindings/c-bindings.cpp | 66 +- src/jsc/bindings/helpers.cpp | 8 +- src/jsc/bindings/libuv/README.md | 9 +- ...uv_posix_stubs.ts => generate_uv_stubs.ts} | 44 +- ...ants.ts => generate_uv_stubs_constants.ts} | 40 +- src/jsc/bindings/libuv/uv.h | 5 +- src/jsc/bindings/libuv/uv/tree.h | 521 +++ src/jsc/bindings/libuv/uv/win.h | 698 ++++ src/jsc/bindings/root.h | 8 + ...lyfills-darwin.c => uv-polyfills-darwin.c} | 2 +- ...polyfills-linux.c => uv-polyfills-linux.c} | 2 +- ...polyfills-posix.c => uv-polyfills-posix.c} | 2 +- src/jsc/bindings/uv-polyfills-windows.c | 81 + .../{uv-posix-polyfills.c => uv-polyfills.c} | 24 +- .../{uv-posix-polyfills.h => uv-polyfills.h} | 11 +- .../bindings/{uv-posix-stubs.c => uv-stubs.c} | 4 +- src/jsc/bindings/v8/node.cpp | 6 +- src/jsc/bindings/vm/Semaphore.cpp | 15 +- src/jsc/bindings/vm/Semaphore.h | 8 +- src/jsc/bindings/webcore/JSWorker.cpp | 10 +- .../bindings/workaround-missing-symbols.cpp | 5 +- src/jsc/bindings/wtf-bindings.cpp | 29 +- src/jsc/error.rs | 4 +- src/jsc/event_loop.rs | 132 +- src/jsc/lib.rs | 3 - src/jsc/modules/NodeConstantsModule.h | 11 + src/jsc/modules/NodeTTYModule.cpp | 16 +- src/jsc/modules/NodeTTYModule.h | 4 - src/jsc/rare_data.rs | 6 +- src/jsc/virtual_machine_exports.rs | 9 - src/libarchive/error.rs | 4 +- src/libarchive/lib.rs | 2 +- src/libuv_sys/Cargo.toml | 19 - src/libuv_sys/lib.rs | 47 - src/libuv_sys/libuv.rs | 3132 ----------------- src/libuv_sys/open_handles.rs | 228 -- src/patch/Cargo.toml | 1 - src/patch/lib.rs | 24 - src/patch_jsc/testing.rs | 6 +- src/ptr/ref_count.rs | 2 +- src/runtime/Cargo.toml | 2 +- src/runtime/JSONLineBuffer.rs | 14 - src/runtime/api.rs | 2 +- src/runtime/api/BunObject.rs | 4 +- src/runtime/api/bun/Terminal.rs | 72 +- src/runtime/api/bun/js_bun_spawn_bindings.rs | 211 +- src/runtime/api/bun/spawn/stdio.rs | 72 +- src/runtime/api/bun/subprocess.rs | 180 +- src/runtime/api/bun/subprocess/Readable.rs | 57 +- .../bun/subprocess/SubprocessPipeReader.rs | 123 +- src/runtime/api/bun/subprocess/Writable.rs | 95 +- src/runtime/api/cron.rs | 88 +- src/runtime/bake/DevServer.rs | 4 +- src/runtime/bin_entry/mod.rs | 15 - src/runtime/cli/bunx_command.rs | 23 - .../cli/create/SourceFileProjectGenerator.rs | 21 - src/runtime/cli/create_command.rs | 46 - src/runtime/cli/filter_run.rs | 75 +- src/runtime/cli/link_command.rs | 8 +- src/runtime/cli/multi_run.rs | 96 +- src/runtime/cli/open.rs | 15 - src/runtime/cli/pack_command.rs | 22 +- src/runtime/cli/pm_version_command.rs | 21 - src/runtime/cli/run_command.rs | 36 +- src/runtime/cli/test/ChangedFilesFilter.rs | 11 - src/runtime/cli/test/parallel/Channel.rs | 367 +- src/runtime/cli/test/parallel/Coordinator.rs | 10 +- src/runtime/cli/test/parallel/Worker.rs | 234 +- src/runtime/cli/test/parallel/runner.rs | 4 +- src/runtime/cli/upgrade_command.rs | 19 - src/runtime/dispatch.rs | 93 +- src/runtime/dns_jsc/dns.rs | 682 +--- src/runtime/dns_jsc/mod.rs | 4 +- src/runtime/image/Image.rs | 11 +- src/runtime/ipc.rs | 665 ++-- src/runtime/ipc_host.rs | 16 +- src/runtime/jsc_hooks.rs | 118 +- src/runtime/napi/napi_body.rs | 19 +- src/runtime/node.rs | 28 +- src/runtime/node/Stat.rs | 12 +- src/runtime/node/StatFS.rs | 6 +- src/runtime/node/node_cluster_binding.rs | 12 +- src/runtime/node/node_fs.rs | 1028 +----- src/runtime/node/node_fs_binding.rs | 7 +- src/runtime/node/node_fs_watcher.rs | 271 +- src/runtime/node/node_os.rs | 378 +- src/runtime/node/node_process.rs | 16 +- src/runtime/node/node_util_binding.rs | 10 +- src/runtime/node/path_watcher.rs | 834 ++++- src/runtime/node/time_like.rs | 52 +- src/runtime/node/types.rs | 69 +- src/runtime/node/uv_signal_handle_windows.rs | 72 - src/runtime/node/win_watcher.rs | 550 --- src/runtime/node/windows_signal.rs | 175 + src/runtime/server/DirectoryRoute.rs | 15 +- src/runtime/server/FileResponseStream.rs | 33 +- src/runtime/server/FileRoute.rs | 23 +- src/runtime/server/mod.rs | 4 +- src/runtime/shell/Builtin.rs | 37 +- src/runtime/shell/IO.rs | 13 +- src/runtime/shell/IOReader.rs | 130 +- src/runtime/shell/IOWriter.rs | 204 +- src/runtime/shell/builtin/cat.rs | 190 +- src/runtime/shell/builtin/touch.rs | 3 +- src/runtime/shell/interpreter.rs | 79 +- src/runtime/shell/states/Cmd.rs | 4 +- src/runtime/shell/states/Pipeline.rs | 4 +- src/runtime/shell/subproc.rs | 482 +-- src/runtime/shell/util.rs | 2 +- src/runtime/socket/Handlers.rs | 2 +- src/runtime/socket/Listener.rs | 315 +- src/runtime/socket/SSLConfig.rs | 4 +- src/runtime/socket/WindowsNamedPipe.rs | 648 +--- src/runtime/socket/WindowsNamedPipeContext.rs | 130 +- src/runtime/socket/socket_body.rs | 10 +- src/runtime/socket/udp_socket.rs | 2 +- src/runtime/socket/uws_handlers.rs | 12 +- src/runtime/timer/mod.rs | 180 - src/runtime/webcore/Blob.rs | 679 +--- src/runtime/webcore/FileReader.rs | 43 +- src/runtime/webcore/FileSink.rs | 142 +- src/runtime/webcore/blob/copy_file.rs | 1641 +++------ src/runtime/webcore/blob/read_file.rs | 791 +---- src/runtime/webcore/blob/write_file.rs | 758 +--- src/runtime/webview/ChromeBackend.cpp | 2 +- src/runtime/webview/ChromeBackend.h | 2 +- src/runtime/webview/ChromeProcess.rs | 303 +- src/runtime/webview/WebKitBackend.cpp | 7 +- src/shell_parser/parse.rs | 2 +- src/spawn/Cargo.toml | 5 +- src/spawn/lib.rs | 59 +- src/spawn/process.rs | 1883 +++------- src/spawn/static_pipe_writer.rs | 98 +- src/spawn_sys/Cargo.toml | 3 - src/spawn_sys/lib.rs | 28 +- src/spawn_sys/posix_spawn.rs | 8 +- src/spawn_sys/spawn_process.rs | 183 +- src/spawn_sys/windows/args.rs | 128 + src/spawn_sys/windows/env.rs | 284 ++ src/spawn_sys/windows/job.rs | 73 + src/spawn_sys/windows/kill.rs | 340 ++ src/spawn_sys/windows/mod.rs | 624 ++++ src/spawn_sys/windows/search_path.rs | 409 +++ src/spawn_sys/windows/stdio.rs | 339 ++ src/spawn_sys/windows/win32.rs | 143 + src/standalone_graph/StandaloneModuleGraph.rs | 2 +- src/symbols.def | 631 ++-- src/sys/Cargo.toml | 3 - src/sys/Error.rs | 60 +- src/sys/PosixStat.rs | 7 +- src/sys/dir.rs | 9 +- src/sys/fd.rs | 69 +- src/sys/file.rs | 4 +- src/sys/lib.rs | 692 ++-- src/sys/sys_uv.rs | 862 ----- src/sys/tmp.rs | 2 +- src/sys/windows/O.rs | 131 + src/sys/windows/fs.rs | 2095 +++++++++++ src/sys/windows/mod.rs | 44 +- src/sys_jsc/fd_jsc.rs | 28 +- src/uws/lib.rs | 4 +- src/uws_sys/Cargo.toml | 1 - src/uws_sys/InternalLoopData.rs | 7 - src/uws_sys/Loop.rs | 292 +- src/uws_sys/Response.rs | 2 +- src/uws_sys/SocketContext.rs | 17 +- src/uws_sys/Timer.rs | 67 - src/uws_sys/iocp.rs | 122 + src/uws_sys/lib.rs | 19 +- src/uws_sys/libuwsockets.cpp | 8 +- src/uws_sys/socket.rs | 2 +- src/uws_sys/us_socket_t.rs | 2 +- src/windows_sys/externs.rs | 185 +- src/windows_sys/lib.rs | 4 - test/bake/dev-and-prod.test.ts | 11 +- test/bake/fixtures/deinitialization/test.ts | 2 +- test/cli/install/bun-run.test.ts | 2 +- test/cli/run/filter-workspace.test.ts | 4 +- test/cli/run/no-orphans.test.ts | 12 +- test/cli/test/parallel.test.ts | 6 +- test/expectations.txt | 2 +- .../source-lints/dead-code-escape-limits.json | 4 +- .../source-lints/fn-long-mut-reborrow.test.ts | 2 +- .../vm-thread-door.inventory.json | 5 +- test/js/bun/http/bun-serve-file.test.ts | 6 +- test/js/bun/http/bun-server.test.ts | 4 +- test/js/bun/io/bun-write.test.js | 46 + .../bun/net/named-pipe-listen-error.test.ts | 4 +- .../bun/net/socket-reentrant-tick-fixture.ts | 95 + test/js/bun/net/socket-retention.test.ts | 2 +- test/js/bun/net/socket-syscall-fault.test.ts | 4 +- test/js/bun/net/socket.test.ts | 111 +- test/js/bun/shell/bunshell.test.ts | 263 +- test/js/bun/shell/file-io.test.ts | 2 +- .../spawn/spawn-concurrent-workers-fixture.ts | 83 + test/js/bun/spawn/spawn-many-teardown.test.ts | 4 +- .../spawn/spawn-pipe-read-error-leak.test.ts | 10 +- .../bun/spawn/spawn-pipe-start-error.test.ts | 8 +- .../spawn/spawn-stdin-pipe-fd-leak.test.ts | 2 +- .../spawn/spawn-stdin-readable-stream.test.ts | 7 +- .../spawn-stdout-filereader-gc-uaf.test.ts | 6 +- .../spawn/spawn-stdout-iterate-leak.test.ts | 2 +- .../spawn/spawn-sync-loop-init-fail.test.ts | 6 +- test/js/bun/spawn/spawn.test.ts | 174 +- test/js/bun/spawn/spawnSync.test.ts | 5 +- .../spawnsync-no-microtask-drain.test.ts | 2 +- .../terminal/terminal-platform-gaps.test.ts | 65 +- test/js/bun/terminal/terminal-spawn.test.ts | 20 +- test/js/bun/util/filesink.test.ts | 8 +- .../bun/webview/webview-chrome-pipe.test.ts | 4 +- test/js/bun/windows/appcontainer.test.ts | 4 +- .../node/child_process/child_process.test.ts | 46 +- test/js/node/cluster.test.ts | 61 + .../fs/fs-writeSync-stdio-windows.test.ts | 6 +- test/js/node/fs/fs.test.ts | 224 +- .../fs/translate-uv-error-windows.test.ts | 4 +- test/js/node/http/node-http-connect.test.ts | 4 +- test/js/node/net/net-syscall-fault.test.ts | 2 +- test/js/node/net/node-net-server.test.ts | 2 +- test/js/node/net/node-net.test.ts | 185 +- .../process/process-signal-windows.test.ts | 16 +- test/js/node/process/process-stdin.test.ts | 189 + test/js/node/tls/ssl-ctx-cache.test.ts | 4 +- test/js/node/watch/fs.watch.test.ts | 148 +- test/js/web/streams/streams.test.js | 4 +- .../websocket-proxy-close-reentrancy.test.ts | 7 +- test/napi/uv-stub-stuff/good_plugin.c | 5 +- test/napi/uv-stub-stuff/plugin.c | 1 - test/napi/uv-stub-stuff/uv_impl.c | 16 +- test/napi/uv.test.ts | 11 +- test/napi/uv_stub.test.ts | 10 +- test/regression/issue/29169.test.ts | 2 +- 358 files changed, 24468 insertions(+), 23039 deletions(-) create mode 100644 packages/bun-usockets/src/eventing/iocp.c delete mode 100644 packages/bun-usockets/src/eventing/libuv.c create mode 100644 packages/bun-usockets/src/internal/eventing/iocp.h delete mode 100644 packages/bun-usockets/src/internal/eventing/libuv.h delete mode 100644 patches/libuv/win-poll-abort-with-disconnect.patch delete mode 100644 patches/libuv/win-poll-rearm-before-callback.patch delete mode 100644 scripts/build/deps/libuv.ts create mode 100644 src/errno/uv_codes.rs create mode 100644 src/io/windows/README.md create mode 100644 src/io/windows/file.rs create mode 100644 src/io/windows/ipc_frame.rs create mode 100644 src/io/windows/mod.rs create mode 100644 src/io/windows/pipe.rs create mode 100644 src/io/windows/pipe_server.rs create mode 100644 src/io/windows/sys.rs create mode 100644 src/io/windows/tty.rs create mode 100644 src/io/windows/tty_input.rs create mode 100644 src/io/windows/tty_output.rs delete mode 100644 src/io/windows_event_loop.rs create mode 100644 src/jsc/bindings/BunWindowsProcess.cpp create mode 100644 src/jsc/bindings/BunWindowsProcess.h create mode 100644 src/jsc/bindings/BunWindowsSignals.h create mode 100644 src/jsc/bindings/BunWinternl.h create mode 100644 src/jsc/bindings/OsBinding.h rename src/jsc/bindings/libuv/{generate_uv_posix_stubs.ts => generate_uv_stubs.ts} (87%) rename src/jsc/bindings/libuv/{generate_uv_posix_stubs_constants.ts => generate_uv_stubs_constants.ts} (91%) create mode 100644 src/jsc/bindings/libuv/uv/tree.h create mode 100644 src/jsc/bindings/libuv/uv/win.h rename src/jsc/bindings/{uv-posix-polyfills-darwin.c => uv-polyfills-darwin.c} (93%) rename src/jsc/bindings/{uv-posix-polyfills-linux.c => uv-polyfills-linux.c} (97%) rename src/jsc/bindings/{uv-posix-polyfills-posix.c => uv-polyfills-posix.c} (88%) create mode 100644 src/jsc/bindings/uv-polyfills-windows.c rename src/jsc/bindings/{uv-posix-polyfills.c => uv-polyfills.c} (91%) rename src/jsc/bindings/{uv-posix-polyfills.h => uv-polyfills.h} (81%) rename src/jsc/bindings/{uv-posix-stubs.c => uv-stubs.c} (99%) delete mode 100644 src/libuv_sys/Cargo.toml delete mode 100644 src/libuv_sys/lib.rs delete mode 100644 src/libuv_sys/libuv.rs delete mode 100644 src/libuv_sys/open_handles.rs delete mode 100644 src/runtime/node/uv_signal_handle_windows.rs delete mode 100644 src/runtime/node/win_watcher.rs create mode 100644 src/runtime/node/windows_signal.rs create mode 100644 src/spawn_sys/windows/args.rs create mode 100644 src/spawn_sys/windows/env.rs create mode 100644 src/spawn_sys/windows/job.rs create mode 100644 src/spawn_sys/windows/kill.rs create mode 100644 src/spawn_sys/windows/mod.rs create mode 100644 src/spawn_sys/windows/search_path.rs create mode 100644 src/spawn_sys/windows/stdio.rs create mode 100644 src/spawn_sys/windows/win32.rs delete mode 100644 src/sys/sys_uv.rs create mode 100644 src/sys/windows/O.rs create mode 100644 src/sys/windows/fs.rs delete mode 100644 src/uws_sys/Timer.rs create mode 100644 src/uws_sys/iocp.rs create mode 100644 test/js/bun/net/socket-reentrant-tick-fixture.ts create mode 100644 test/js/bun/spawn/spawn-concurrent-workers-fixture.ts 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/CLAUDE.md b/CLAUDE.md index b93d857ed53d..e908753e6245 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 20012ce3d0cf..b5ecf1e22d40 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", @@ -959,17 +961,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" @@ -1064,7 +1055,6 @@ dependencies = [ "bun_collections", "bun_core", "bun_errno", - "bun_event_loop", "bun_paths", "bun_spawn", "bun_sys", @@ -1268,7 +1258,6 @@ dependencies = [ "bun_jsc", "bun_libarchive", "bun_libdeflate_sys", - "bun_libuv_sys", "bun_lsquic_sys", "bun_md", "bun_opaque", @@ -1293,6 +1282,7 @@ dependencies = [ "bun_sourcemap", "bun_sourcemap_jsc", "bun_spawn", + "bun_spawn_sys", "bun_sql_jsc", "bun_standalone_graph", "bun_sys", @@ -1449,19 +1439,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", @@ -1474,7 +1463,6 @@ dependencies = [ "bstr", "bun_analytics", "bun_core", - "bun_libuv_sys", "bun_sys", "bun_windows_sys", "enumset", @@ -1571,7 +1559,6 @@ dependencies = [ "bun_alloc", "bun_core", "bun_errno", - "bun_libuv_sys", "bun_opaque", "bun_paths", "bun_windows_sys", @@ -1667,7 +1654,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 012f23a486ab..e7308a99c55d 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", @@ -411,7 +410,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 cb154882a4cd..1b1a01345059 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 can fire late under ConPTY** unless the child is reading stdin in raw mode. ConPTY reports a resize only to a raw-mode reader. Otherwise Bun checks the console size when its event loop next wakes. `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. --- diff --git a/mordant-baseline.toml b/mordant-baseline.toml index 18c3769d0737..3a0bb1116dce 100644 --- a/mordant-baseline.toml +++ b/mordant-baseline.toml @@ -143,7 +143,6 @@ "unused_pub:src/jsc/lib.rs" = 1 "unused_pub:src/jsc/webcore_types.rs" = 2 "unused_pub:src/libarchive/lib.rs" = 1 -"unused_pub:src/libuv_sys/lib.rs" = 5 "unused_pub:src/opaque/lib.rs" = 2 "unused_pub:src/parsers/yaml.rs" = 2 "unused_pub:src/paths/Path.rs" = 2 diff --git a/package.json b/package.json index 783f992dc90b..33f979b72909 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "build:local": "bun scripts/build.ts --profile=debug-local --build-dir=build/debug-local", "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 07398ce81d0a..0b60ce6a074e 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. */ @@ -7467,8 +7467,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. diff --git a/packages/bun-usockets/src/bsd.c b/packages/bun-usockets/src/bsd.c index ac261e35a81a..37ccb9974525 100644 --- a/packages/bun-usockets/src/bsd.c +++ b/packages/bun-usockets/src/bsd.c @@ -57,6 +57,7 @@ static void init_debug_logging() { #include #include #else /* _WIN32 */ +#include #include #endif @@ -65,10 +66,22 @@ 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; + return WSAStartup(MAKEWORD(2, 2), &wsa_data) == 0; +} + +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 @@ -326,7 +339,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); @@ -338,7 +351,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); diff --git a/packages/bun-usockets/src/context.c b/packages/bun-usockets/src/context.c index ad7bf0d44ad6..5e3a743dc6b0 100644 --- a/packages/bun-usockets/src/context.c +++ b/packages/bun-usockets/src/context.c @@ -391,7 +391,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. */ @@ -430,7 +430,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; @@ -457,7 +457,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); @@ -652,11 +652,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); @@ -732,7 +728,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) { @@ -745,11 +741,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 @@ -776,23 +768,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: { - 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 cb5d8706a08a..a5b1a4c82318 100644 --- a/packages/bun-usockets/src/crypto/openssl.c +++ b/packages/bun-usockets/src/crypto/openssl.c @@ -2371,8 +2371,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; } } @@ -2413,7 +2412,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 e034afab340e..66268fac5587 100644 --- a/packages/bun-usockets/src/eventing/epoll_kqueue.c +++ b/packages/bun-usockets/src/eventing/epoll_kqueue.c @@ -406,7 +406,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:1387,1590 and kqueue.c:253,451). */ 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) { @@ -470,7 +470,7 @@ void us_loop_run(struct us_loop_t *loop) { } } -extern unsigned int Bun__JSC_onBeforeWait(void * _Nonnull jsc_vm, uint64_t now_ns); +extern void Bun__JSC_onBeforeWait(void * _Nonnull jsc_vm); void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec* timeout, uint64_t now_ns) { if (loop->num_polls == 0) @@ -501,11 +501,8 @@ 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. */ if (will_idle_inside_event_loop && loop->data.jsc_vm) - (void) Bun__JSC_onBeforeWait(loop->data.jsc_vm, now_ns); + Bun__JSC_onBeforeWait(loop->data.jsc_vm); /* 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 @@ -514,6 +511,9 @@ void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec* timeout * 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) { + /* `now_ns` is the reading the JS side took to pick `timeout` + * (timer::All::get_timeout); 0 if it had none to share. Nothing + * measures a deadline against it. */ 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(); @@ -776,6 +776,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); diff --git a/packages/bun-usockets/src/eventing/iocp.c b/packages/bun-usockets/src/eventing/iocp.c new file mode 100644 index 000000000000..11afec8bde8b --- /dev/null +++ b/packages/bun-usockets/src/eventing/iocp.c @@ -0,0 +1,1696 @@ +/* + * 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. */ + +void Bun__internal_dispatch_ready_poll(void *loop, void *poll); +extern void Bun__JSC_onBeforeWait(void *_Nonnull jsc_vm); + +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)) + +#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_INVALID_HANDLE +#define STATUS_INVALID_HANDLE ((NTSTATUS) 0xC0000008L) +#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; +}; + +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; +}; + +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); +} + +/* 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; + for (int depth = 0; depth < 8; depth++) { + 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 = depth == 0; + return INVALID_SOCKET; + } + if (WSAIoctl(socket, SIO_BSP_HANDLE_POLL, NULL, 0, &base, sizeof(base), &bytes, NULL, NULL) == SOCKET_ERROR || base == socket) { + return INVALID_SOCKET; + } + socket = base; + } + return INVALID_SOCKET; +} + +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, OVERLAPPED_ENTRY *entry); +static int slow_poll_submit(struct us_internal_afd_poll *poll); +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); + +/* 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 (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(status == STATUS_INVALID_HANDLE ? WSAENOTSOCK : WSAENOBUFS); + 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; +} + +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; + /* 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 a second one on the same + * socket from this thread would steal its events: 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; + 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, OVERLAPPED_ENTRY *entry) { + (void) entry; + struct us_internal_afd_poll *poll = (struct us_internal_afd_poll *) op; + poll->state = AFD_POLL_STATE_IDLE; + + if (!poll->owner) { + afd_poll_free(poll); + return; + } + + NTSTATUS status = poll->iosb.Status; + if (status == STATUS_CANCELLED) { + afd_poll_queue_update(poll); + loop->afd_saw_cancelled = 1; + 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_dispatch(loop, poll, 1, 0, 0); + } + } +} + +/* select() fallback for sockets whose provider chain does not end at AFD. */ + +static void slow_poll_complete(struct us_loop_t *loop, struct us_iocp_op *op, OVERLAPPED_ENTRY *entry) { + (void) entry; + 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 stale = req->interest != poll->interest; + us_free(req); + + poll->slow_requests--; + if (!poll->owner) { + if (!poll->slow_requests && !poll->queued_for_update) { + afd_poll_free(poll); + } + return; + } + 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) || FD_ISSET(req->socket, &efds)) req->result_events |= LIBUS_SOCKET_WRITABLE; + } + /* The completion frees `req`. */ + HANDLE port = req->port; + PostQueuedCompletionStatus(port, 0, 0, &req->op.overlapped); + CloseHandle(port); + return 0; +} + +static int slow_poll_submit(struct us_internal_afd_poll *poll) { + /* 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; + } + 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; + 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; + } + } + 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->loop->iocp, 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); + } + us_free(wait); +} + +/* 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) { + (void) hint; + 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; +} + +/* Runs what is left of the dequeued batch. A dequeued packet exists nowhere + * else, and a callback can re-enter the loop: the entry is copied and the + * index moved past it first, so that the nested tick finishes this batch + * before its own wait overwrites it. */ +/* 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); +} + +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. */ + if (!entry.lpOverlapped) { + if (entry.lpCompletionKey == HRTIMER_COMPLETION_KEY) { + loop->hrtimer_deadline_ns = 0; + } + continue; + } + InterlockedDecrement((volatile LONG *) &loop->pending_ops); + struct us_iocp_op *op = (struct us_iocp_op *) entry.lpOverlapped; + op->complete(loop, op, &entry); + } +} + +static void us_internal_iocp_dequeue(struct us_loop_t *loop, DWORD timeout_ms) { + ULONG count = 0; + if (!GetQueuedCompletionStatusEx(loop->iocp, loop->ready_polls, LIBUS_MAX_READY_POLLS, &count, timeout_ms, FALSE)) { + count = 0; + } + loop->num_ready_polls = (int) count; + loop->current_ready_poll = 0; +} + +/* What has to happen before a wait, outside of it: see us_internal_iocp_wait. */ +static void us_internal_before_wait(struct us_loop_t *loop, long long timeout_ns) { + if (timeout_ns != 0) { + /* Not at startup: this loads powrprof.dll, and only a wait that blocks needs it. */ + InitOnceExecuteOnce(&resume_once, us_internal_resume_register, NULL, NULL); + } + afd_flush_updates(loop); +} + +/* 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) { + /* `now_ns` is the reading the caller took to pick `timeout_ns`, so for one + * pending timer this is the same value tick after tick. */ + const uint64_t due_ns = now_ns ? now_ns + (uint64_t) timeout_ns : 0; + if (due_ns && 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 { + /* Cancel, set, associate, in this order: a packet from the previous + * expiry is removed before the timer can fire again. */ + pNtCancelWaitCompletionPacket(loop->hrtimer_packet, TRUE); + LARGE_INTEGER due; + due.QuadPart = -((timeout_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; + timeout_ms = INFINITE; + } else { + loop->hrtimer_deadline_ns = 0; + timeout_ms = (DWORD) ((timeout_ns + 999999) / 1000000); + } + } + } 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; +} + +/* If the batch was full, more packets are likely queued already: take them + * before running pre/post again. Capped like the epoll/kqueue backend. + * + * Likewise after a poll cancelled to widen its mask came back: the flush + * submits the wider one, and what it was widened for (a resumed socket's + * queued data) completes it at once. Taking that now reports it in the same + * iteration an epoll_ctl would have. */ +static void us_internal_drain_ready_polls(struct us_loop_t *loop) { + int drain_count = 48; + while ((loop->num_ready_polls == LIBUS_MAX_READY_POLLS || loop->afd_saw_cancelled) && --drain_count != 0 && loop->num_polls > 0) { + loop->afd_saw_cancelled = 0; + afd_flush_updates(loop); + us_internal_iocp_dequeue(loop, 0); + if (loop->num_ready_polls == 0) { + break; + } + us_internal_dispatch_ready_polls(loop); + } +} + +/* 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(struct us_loop_t *loop) { + /* While we have non-fallthrough polls we shouldn't fall through */ + while (loop->num_polls) { + loop->data.tick_depth++; + us_internal_loop_pre(loop); + + us_internal_dispatch_ready_polls(loop); + long long timeout_ns = us_internal_clamp_to_sweep(loop, -1); + us_internal_before_wait(loop, timeout_ns); + us_internal_iocp_wait(loop, timeout_ns, 0); + + us_internal_dispatch_ready_polls(loop); + us_internal_drain_ready_polls(loop); + us_internal_sweep_if_due(loop); + + us_internal_loop_post(loop); + loop->data.tick_depth--; + } +} + +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) + return; + + loop->data.tick_depth++; + + us_internal_loop_pre(loop); + + /* Only a tick entered from a completion callback has anything left here. */ + us_internal_dispatch_ready_polls(loop); + + 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); + + const unsigned int had_wakeups = (unsigned int) InterlockedExchange((volatile LONG *) &loop->pending_wakeups, 0); + if (had_wakeups != 0) { + timeout_ns = 0; + } + const int will_idle_inside_event_loop = timeout_ns != 0; + if (will_idle_inside_event_loop && loop->data.jsc_vm) + Bun__JSC_onBeforeWait(loop->data.jsc_vm); + + /* After the finalizers above, which stop polls, and before the hand-off below: this frees. */ + us_internal_before_wait(loop, timeout_ns); + + /* 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. Only on a tick that really parks: the hand-off wakes the scavenger thread (a + * syscall), and a tick that does not block takes the heaps back before it could sweep. + * With no scavenger to hand off to, fall back to sweeping inline, rate-limited. */ + const int handed_off = will_idle_inside_event_loop && mi_on_thread_idle_start(); + if (!handed_off && will_idle_inside_event_loop) { + /* `now_ns` is the reading the JS side took to pick `timeout`; 0 if it had none to share. */ + 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(); + } + } + + us_internal_iocp_wait(loop, timeout_ns, now_ns); + + /* Before anything can allocate again. */ + if (handed_off) + mi_on_thread_idle_end(); + + us_internal_dispatch_ready_polls(loop); + us_internal_drain_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) { + /* 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 instead of holding the drain for its full duration. */ + 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); + + us_internal_loop_data_free(loop); + + if (loop->hrtimer) { + pNtCancelWaitCompletionPacket(loop->hrtimer_packet, TRUE); + CloseHandle(loop->hrtimer_packet); + CloseHandle(loop->hrtimer); + } + + /* Every op still out there was cancelled by its owner, but its memory + * belongs to the kernel until its packet is dequeued. Bounded: whatever + * has not come back by then stays allocated rather than being freed under + * the kernel. */ + for (int rounds = 0; rounds < 64; rounds++) { + afd_flush_updates(loop); + if (loop->pending_ops == 0) { + break; + } + us_internal_iocp_dequeue(loop, 16); + 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); +} + +/* Poll */ + +/* ── Accepting ─────────────────────────────────────────────────────────────── + * A listening socket is not polled for readiness. 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_ACCEPTS_PER_LISTENER 1 +#define US_ACCEPT_ADDRESS_LENGTH ((DWORD) (sizeof(struct sockaddr_storage) + 16)) + +struct us_internal_accept_slot { + struct us_iocp_op op; + struct us_internal_acceptor *acceptor; + /* Handed to the outstanding AcceptEx, or accepted and not taken yet. */ + SOCKET socket; + /* Completion by event, see us_internal_acceptor::by_event. */ + HANDLE event; + struct us_iocp_wait *wait; + unsigned char in_flight; + unsigned char accepted; + /* The packet in flight is a posted one: AcceptEx could not be started. */ + unsigned char retry; + char addresses[2 * US_ACCEPT_ADDRESS_LENGTH]; +}; + +/* The kernel owns a slot's OVERLAPPED and buffer until its packet is dequeued, + * so this is freed only once the listener stopped and no slot is in flight. */ +struct us_internal_acceptor { + 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; + /* The socket's file object is associated with another completion port + * (another loop or process accepts on it too), and a file object has only + * one. Each AcceptEx then carries an event with its low bit set, which + * queues nothing to that port, and a wait on the event delivers it here. */ + unsigned char by_event; + unsigned char dispatching; + unsigned int in_flight; + struct us_internal_acceptor *next; + struct us_internal_acceptor *prev; + struct us_internal_accept_slot slots[US_ACCEPTS_PER_LISTENER]; +}; + +static void acceptor_maybe_free(struct us_internal_acceptor *a) { + if (a->owner || a->in_flight || a->dispatching) { + return; + } + for (int i = 0; i < US_ACCEPTS_PER_LISTENER; i++) { + struct us_internal_accept_slot *slot = &a->slots[i]; + if (slot->socket != INVALID_SOCKET) { + closesocket(slot->socket); + } + if (slot->wait) { + us_iocp_wait_free(slot->wait); + } + if (slot->event) { + CloseHandle(slot->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); +} + +/* Returns 0 once a packet for the slot is on its way. */ +static int accept_slot_start(struct us_internal_accept_slot *slot) { + struct us_internal_acceptor *a = slot->acceptor; + slot->socket = WSASocketW(a->family, a->type, a->protocol, NULL, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); + if (slot->socket == INVALID_SOCKET) { + return -1; + } + memset(&slot->op.overlapped, 0, sizeof(slot->op.overlapped)); + if (a->by_event) { + ResetEvent(slot->event); + slot->op.overlapped.hEvent = (HANDLE) ((ULONG_PTR) slot->event | 1); + } + DWORD bytes; + if (!a->accept_ex(a->listener, slot->socket, slot->addresses, 0, US_ACCEPT_ADDRESS_LENGTH, US_ACCEPT_ADDRESS_LENGTH, &bytes, &slot->op.overlapped) && + WSAGetLastError() != ERROR_IO_PENDING) { + int err = WSAGetLastError(); + closesocket(slot->socket); + slot->socket = INVALID_SOCKET; + WSASetLastError(err); + return -1; + } + if (!a->by_event) { + /* Also when it succeeded at once: the packet is queued all the same. */ + us_iocp_op_submitted(a->loop); + } else if (us_iocp_wait_start(slot->wait, slot->event, &slot->op) != 0) { + /* Nothing will announce the completion, so collect it here. */ + CancelIoEx((HANDLE) a->listener, &slot->op.overlapped); + WaitForSingleObject(slot->event, INFINITE); + closesocket(slot->socket); + slot->socket = INVALID_SOCKET; + WSASetLastError(WSAENOBUFS); + return -1; + } + slot->in_flight = 1; + a->in_flight++; + return 0; +} + +/* Keep the listener accepting: if AcceptEx cannot be started now (out of + * sockets or memory), try again from the loop rather than never. */ +static void accept_slot_arm(struct us_internal_accept_slot *slot) { + struct us_internal_acceptor *a = slot->acceptor; + /* A connection that was reset while it waited fails the call that would have taken it. */ + for (int attempt = 0; attempt < 8; attempt++) { + if (accept_slot_start(slot) == 0) { + return; + } + if (WSAGetLastError() != WSAECONNRESET) { + break; + } + } + /* Not a shortage: the socket is no longer one that can accept. */ + if (WSAGetLastError() == WSAENOTSOCK || WSAGetLastError() == WSAEINVAL) { + return; + } + if (PostQueuedCompletionStatus(a->loop->iocp, 0, 0, &slot->op.overlapped)) { + us_iocp_op_submitted(a->loop); + slot->retry = 1; + slot->in_flight = 1; + a->in_flight++; + } +} + +static void accept_slot_complete(struct us_loop_t *loop, struct us_iocp_op *op, OVERLAPPED_ENTRY *entry) { + (void) loop; + (void) entry; + struct us_internal_accept_slot *slot = (struct us_internal_accept_slot *) op; + struct us_internal_acceptor *a = slot->acceptor; + slot->in_flight = 0; + a->in_flight--; + + const int retry = slot->retry; + slot->retry = 0; + if (!a->owner || a->loop->closing) { + acceptor_maybe_free(a); + return; + } + if (retry) { + accept_slot_arm(slot); + return; + } + + /* The status AcceptEx left in the OVERLAPPED; an error is the connection's + * (it was reset before it could be accepted), not the listener's. */ + int ok = NT_SUCCESS((NTSTATUS) slot->op.overlapped.Internal); + if (ok) { + SOCKET listener = a->listener; + ok = setsockopt(slot->socket, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (const char *) &listener, sizeof(listener)) == 0; + } + if (!ok) { + closesocket(slot->socket); + slot->socket = INVALID_SOCKET; + accept_slot_arm(slot); + return; + } + + slot->accepted = 1; + a->dispatching = 1; + us_internal_dispatch_ready_poll(a->owner, 0, 0, LIBUS_SOCKET_READABLE); + a->dispatching = 0; + if (slot->accepted) { + /* The listener was closed before it took the connection. */ + slot->accepted = 0; + closesocket(slot->socket); + slot->socket = INVALID_SOCKET; + if (a->owner) { + accept_slot_arm(slot); + } + } + acceptor_maybe_free(a); +} + +static void acceptor_cancel(struct us_internal_acceptor *a) { + for (int i = 0; i < US_ACCEPTS_PER_LISTENER; i++) { + struct us_internal_accept_slot *slot = &a->slots[i]; + if (slot->in_flight && !slot->retry) { + CancelIoEx((HANDLE) a->listener, &slot->op.overlapped); + } + } +} + +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_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->by_event = CreateIoCompletionPort((HANDLE) listener, loop->iocp, 0, 0) == NULL; + a->next = loop->acceptors; + if (a->next) { + a->next->prev = a; + } + loop->acceptors = a; + + for (int i = 0; i < US_ACCEPTS_PER_LISTENER; i++) { + a->slots[i].op.complete = accept_slot_complete; + a->slots[i].acceptor = a; + a->slots[i].socket = INVALID_SOCKET; + } + int started = 1; + for (int i = 0; i < US_ACCEPTS_PER_LISTENER; i++) { + struct us_internal_accept_slot *slot = &a->slots[i]; + if (a->by_event) { + slot->event = CreateEventW(NULL, TRUE, FALSE, NULL); + slot->wait = slot->event ? us_iocp_wait_create(loop) : NULL; + if (!slot->wait) { + started = 0; + break; + } + } + if (accept_slot_start(slot) != 0) { + started = 0; + break; + } + } + 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 (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); + } + for (int i = 0; i < US_ACCEPTS_PER_LISTENER; i++) { + struct us_internal_accept_slot *slot = &a->slots[i]; + if (!slot->accepted) { + continue; + } + SOCKET accepted = slot->socket; + slot->accepted = 0; + slot->socket = INVALID_SOCKET; + + struct sockaddr *local = NULL; + struct sockaddr *remote = NULL; + int local_length = 0; + int remote_length = 0; + a->get_addresses(slot->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); + + /* The kernel can take the next connection while this one is handled. */ + accept_slot_arm(slot); + + ssize_t injected = 0; + int unused = 0; + if (US_FAULT_CHECK(US_FAULT_ACCEPT, accepted, injected, unused)) { + closesocket(accepted); + return LIBUS_SOCKET_ERROR; + } + (void) injected; + (void) unused; + return accepted; + } + return LIBUS_SOCKET_ERROR; +} + +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)); + +#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)) { + errno = (int) -injected; + return (int) injected; + } +#endif + + /* 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; +} + +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 (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)); + if (p->acceptor) { + if (events & LIBUS_SOCKET_READABLE) { + return 0; + } + acceptor_stop(p->acceptor); + p->acceptor = NULL; + return 0; + } + if (!p->afd) { + /* Taken out by us_poll_stop (a paused socket that hung up); this is the resume putting it back. */ + return us_poll_start_rc(p, loop, events); + } + 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->cb_expects_the_loop = 1; + cb->leave_poll_ready = 1; + 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, OVERLAPPED_ENTRY *entry) { + (void) loop; + (void) entry; + 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(cb->cb_expects_the_loop ? (struct us_internal_callback_t *) cb->loop : cb); +} + +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); + } + } +} + +int us_socket_get_error(struct us_socket_t *s) { + int error = 0; + int len = sizeof(error); + if (getsockopt(us_poll_fd((struct us_poll_t *) s), SOL_SOCKET, SO_ERROR, (char *) &error, &len) == SOCKET_ERROR) { + return LIBUS_ERR; + } + return error; +} + +#endif diff --git a/packages/bun-usockets/src/eventing/libuv.c b/packages/bun-usockets/src/eventing/libuv.c deleted file mode 100644 index 26e2b9e43f5a..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); - -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); - 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..1cac1a4395c9 --- /dev/null +++ b/packages/bun-usockets/src/internal/eventing/iocp.h @@ -0,0 +1,143 @@ +/* + * 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, OVERLAPPED_ENTRY *entry); +}; + +struct us_internal_afd_poll; +struct us_internal_afd_helper; +struct us_internal_acceptor; + +/* 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 dequeued this iteration */ + 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; + + /* Incremented atomically by wakeup(), swapped to 0 before waiting. + * If non-zero, the event loop will return immediately so we can skip the GC safepoint. */ + unsigned int pending_wakeups; + + /* 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; + /* A poll cancelled to widen its mask was dequeued and waits for the flush. */ + unsigned char afd_saw_cancelled; + /* 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; + + /* 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 the tick's `now_ns`; 0 while + * it is not armed or that is unknown. */ + uint64_t hrtimer_deadline_ns; + + alignas(LIBUS_EXT_ALIGNMENT) OVERLAPPED_ENTRY ready_polls[LIBUS_MAX_READY_POLLS]; +}; + +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); + +/* 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. `entry->Internal` is + * 0 on delivery. */ +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 4b4496bca19d..4e5a88c34981 100644 --- a/packages/bun-usockets/src/internal/fault_inject.h +++ b/packages/bun-usockets/src/internal/fault_inject.h @@ -43,8 +43,8 @@ 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 (afd_poll_create + * 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 3f7112b8f7da..948a16ca51f5 100644 --- a/packages/bun-usockets/src/internal/internal.h +++ b/packages/bun-usockets/src/internal/internal.h @@ -41,9 +41,9 @@ typedef SSIZE_T ssize_t; #include #endif -#if defined(LIBUS_USE_EPOLL) || defined(LIBUS_USE_KQUEUE) #define LIBUS_MAX_READY_POLLS 1024 +#if defined(LIBUS_USE_EPOLL) || defined(LIBUS_USE_KQUEUE) void us_internal_loop_update_pending_ready_polls(struct us_loop_t *loop, struct us_poll_t *old_poll, struct us_poll_t *new_poll, @@ -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 @@ -162,13 +162,12 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in 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 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); @@ -206,6 +205,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); @@ -427,8 +436,13 @@ struct us_internal_callback_t { 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 959a9110204c..0a44da3cf756 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 69cff5abd34d..92960cdddfb7 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 * @@ -595,9 +572,6 @@ void us_loop_run(us_loop_r loop) nonnull_fn_decl; * 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 */ @@ -729,15 +703,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 @@ -752,9 +723,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 4962be10a222..cca488c4dfdc 100644 --- a/packages/bun-usockets/src/loop.c +++ b/packages/bun-usockets/src/loop.c @@ -48,37 +48,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 +96,12 @@ void us_internal_sweep_if_due(struct us_loop_t *loop) { us_internal_timer_sweep(loop); } -#endif - /* -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 +114,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,17 +133,11 @@ 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 __atomic_fetch_add(&loop->pending_wakeups, 1, __ATOMIC_RELEASE); -#endif us_internal_async_wakeup(loop->data.wakeup_async); } @@ -396,12 +359,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++; @@ -435,22 +392,13 @@ 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); break; @@ -484,7 +432,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 */ @@ -550,7 +498,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; @@ -737,8 +685,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 @@ -774,38 +720,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; @@ -929,7 +843,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); @@ -1083,13 +997,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 6570280afdbf..9961f77dde22 100644 --- a/packages/bun-usockets/src/quic.c +++ b/packages/bun-usockets/src/quic.c @@ -150,12 +150,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 @@ -184,19 +178,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 @@ -607,13 +592,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. */ @@ -635,9 +615,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 9c8f1fa82671..e971f42f8a9c 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); } @@ -395,7 +387,7 @@ struct us_socket_t *us_socket_detach(struct us_socket_t *s) { } 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) +#ifdef _WIN32 return 0; #else if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0) { @@ -416,14 +408,14 @@ static void us_internal_rearm_writable(struct us_socket_t *s) { } /* 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 @@ -799,13 +791,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); @@ -834,9 +819,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); @@ -844,13 +827,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-uws/src/HttpContext.h b/packages/bun-uws/src/HttpContext.h index 22b93c4e64b3..757786ea90dd 100644 --- a/packages/bun-uws/src/HttpContext.h +++ b/packages/bun-uws/src/HttpContext.h @@ -301,9 +301,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 @@ -314,9 +311,6 @@ struct HttpContext { /* Do not accept any data while in shutdown state */ if (us_socket_is_shut_down((us_socket_t *) s)) { - /* Balance the us_socket_ref above — every other return path - * reaches the unref via returnedData. */ - us_socket_unref(s); return s; } @@ -335,17 +329,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) { @@ -364,7 +355,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); } } @@ -376,7 +366,6 @@ 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; } } @@ -661,11 +650,6 @@ 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); - } /* Flush anything the 'clientError' handler wrote (uncorking a * closed socket is a no-op). */ ((AsyncSocket *) s)->uncork(); @@ -688,9 +672,6 @@ struct HttpContext { auto returnedData = result.returnedData; /* 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); - /* node:http compat: a partial request head was left in the fallback * buffer by this read (either fresh bytes on an idle connection or a * pipelined request after the previous message completed) - its @@ -771,7 +752,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) @@ -812,7 +793,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) @@ -1103,10 +1084,6 @@ struct HttpContext { /* HTTP clients always send first (the request, or ClientHello for TLS), so defer * accept() until data arrives and dispatch the read immediately after accept. */ auto socket = us_socket_group_listen(&group, socketKind(), sslCtx, host, port, options | LIBUS_LISTEN_DEFER_ACCEPT, socketExtSize(), &error); - // we dont depend on libuv ref for keeping it alive - if (socket) { - us_socket_unref(&socket->s); - } return socket; } @@ -1114,11 +1091,6 @@ struct HttpContext { us_listen_socket_t *listen_unix(struct ssl_ctx_st *sslCtx, const char *path, size_t pathlen, int options) { int error = 0; auto* socket = us_socket_group_listen_unix(&group, socketKind(), sslCtx, path, pathlen, options, socketExtSize(), &error); - // we dont depend on libuv ref for keeping it alive - if (socket) { - us_socket_unref(&socket->s); - } - return socket; } }; diff --git a/packages/bun-uws/src/Loop.h b/packages/bun-uws/src/Loop.h index a8de89601228..379c89ec6e53 100644 --- a/packages/bun-uws/src/Loop.h +++ b/packages/bun-uws/src/Loop.h @@ -95,12 +95,9 @@ 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; loop->free(); @@ -118,25 +115,16 @@ 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(nullptr); + getLazyLoop().cleanMe = true; } 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(); 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 a08274c3b182..b163e1b0fddb 100644 --- a/scripts/build/bun.ts +++ b/scripts/build/bun.ts @@ -360,7 +360,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 458b3617e550..0f091763d2d7 100644 --- a/scripts/build/deps/README.md +++ b/scripts/build/deps/README.md @@ -49,7 +49,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: @@ -148,9 +148,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/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 cbfcdfacedcf..191058f232b6 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 ad8f798f3c73..679a9b08c4e2 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 headers (src/jsc/bindings/libuv), and the + * bundled headers conflict. */ import { resolve } from "node:path"; @@ -29,7 +30,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 headers. // 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 4fab27b554ad..974fd970be69 100644 --- a/scripts/build/flags.ts +++ b/scripts/build/flags.ts @@ -440,7 +440,6 @@ export const globalFlags: Flag[] = [ { // libuv stubs use C23 anonymous parameters flag: "-Wno-c23-extensions", - when: c => c.unix, desc: "Allow C23 extensions (libuv stubs use anonymous parameters)", }, @@ -1684,10 +1683,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")); } + // libuv's headers: the UV_E* numbers, and the types of the uv_* stubs and polyfills. + 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 27e39ce2b9a9..fc69f3ceeb75 100644 --- a/scripts/build/source.ts +++ b/scripts/build/source.ts @@ -143,7 +143,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,8 +255,7 @@ export interface DirectBuild { /** * 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 + * 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). @@ -266,7 +265,6 @@ export interface DirectBuild { export interface ForbidUndefined { symbols: readonly string[]; - except?: readonly string[]; } /** @@ -479,7 +477,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; @@ -1734,10 +1732,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, @@ -1747,17 +1743,8 @@ function emitForbidUndefined( objects: string[], buildDir: string, ): string[] { - const { symbols, except = [] } = spec.forbidUndefined!; + const { symbols } = 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`); if (cfg.nm === undefined) return []; mkdirSync(buildDir, { recursive: true }); @@ -1765,7 +1752,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/glob-sources.ts b/scripts/glob-sources.ts index c9ba7599100c..a442690c5e85 100644 --- a/scripts/glob-sources.ts +++ b/scripts/glob-sources.ts @@ -121,8 +121,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 409c657f475e..0caeb841cb8d 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 3ac4e52ace7d..926713d9d799 100644 --- a/src/bun_core/Global.rs +++ b/src/bun_core/Global.rs @@ -339,7 +339,7 @@ impl SignalCode { #[cfg(not(unix))] { // Windows numbering: CRT plus libuv's synthetic SIGHUP/SIGQUIT/SIGKILL/ - // SIGWINCH (vendor/libuv/include/uv/win.h). The enum discriminants are Linux numbers + // SIGWINCH (libuv's include/uv/win.h). The enum discriminants are Linux numbers // and must not leak here (SIGABRT is 22 on Windows, not 6). use SignalCode as S; match self { 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 aa42dd2f9f2d..aa26c47ea8b7 100644 --- a/src/bun_core/lib.rs +++ b/src/bun_core/lib.rs @@ -461,9 +461,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). /// @@ -695,7 +695,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 @@ -2502,7 +2502,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. /// @@ -2537,7 +2537,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. @@ -2694,6 +2694,10 @@ pub mod ffi { #[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 {} diff --git a/src/bun_core/output.rs b/src/bun_core/output.rs index 309a9c64533f..c0080f016ff7 100644 --- a/src/bun_core/output.rs +++ b/src/bun_core/output.rs @@ -542,7 +542,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); @@ -2569,7 +2569,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/util.rs b/src/bun_core/util.rs index 662242c10620..4c37c1f4c6e5 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, …) 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,21 +910,21 @@ 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 + /// C runtime file number. On POSIX this equals `native()`. On Windows, + /// when kind=crt this extracts the stored CRT fd; 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. + /// arbitrary HANDLE to a CRT fd makes closing impossible. The supplier + /// should call `make_crt_owned()` 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::Crt(v) => v, DecodeWindows::Windows(handle) => { // `.stdin()`/`.stdout()`/`.stderr()` hand out the cached // `WINDOWS_CACHED_STD{IN,OUT,ERR}` (snapshotted at startup), @@ -951,8 +951,8 @@ impl Fd { return 2; } panic!( - "Cast bun.FD.uv({}) makes closing impossible!\n\n\ - The supplier of fd FD should call 'FD.makeLibUVOwned',\n\ + "Fd::crt({}) on a HANDLE makes closing impossible!\n\n\ + The supplier of the fd should call `make_crt_owned()`,\n\ probably where open() was called.", self, ); @@ -1006,7 +1006,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 +1026,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 +1038,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 +1050,7 @@ impl Fd { if self.0 & FD_KIND_BIT == 0 { FdKind::System } else { - FdKind::Uv + FdKind::Crt } } @@ -1074,37 +1074,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 +1089,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 +1130,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 +1159,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)] @@ -1411,14 +1382,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 +1408,43 @@ 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 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)] + pub fn crt_get_osfhandle(fd: c_int) -> *mut c_void { + if fd < 0 { + return crate::windows_sys::INVALID_HANDLE_VALUE; + } + 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 { + _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; + } + _close(fd) } #[cfg(windows)] pub use crate::windows_sys::{STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE}; @@ -4431,8 +4425,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}; @@ -4979,8 +4972,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 cfca2cae090b..2a13eec9bf15 100644 --- a/src/bun_core/windows_sys.rs +++ b/src/bun_core/windows_sys.rs @@ -37,7 +37,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. // ────────────────────────────────────────────────────────────────────────── @@ -48,18 +48,60 @@ pub use bun_windows_sys::{ // SAFETY: nested `i16`/`u16` POD; all-zero is the documented pre-call state // for `GetConsoleScreenBufferInfo` out-params. Impl lives here (not in // `bun_windows_sys`) because the `Zeroable` trait is owned by `bun_core`. -#[cfg(windows)] unsafe impl crate::ffi::Zeroable for CONSOLE_SCREEN_BUFFER_INFO {} // kernel32 externs are owned by the tier-0 leaf `bun_windows_sys`; re-export // 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..61d510f93dd9 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) { @@ -1664,28 +1667,28 @@ impl Error { pub fn init_eai(rc: i32) -> Option { #[cfg(windows)] { - use bun_libuv_sys as libuv; + use bun_errno::uv_codes as uv; // https://github.com/nodejs/node/blob/2eff28fb7a93d3f672f80b582f664a7c701569fb/lib/internal/errors.js#L807-L815 - if rc == libuv::UV_EAI_NODATA || rc == libuv::UV_EAI_NONAME { + if rc == uv::UV_EAI_NODATA || rc == uv::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), + uv::UV_EAI_AGAIN => Some(Error::EAI_AGAIN), + uv::UV_EAI_ADDRFAMILY => Some(Error::EBADFAMILY), + uv::UV_EAI_BADFLAGS => Some(Error::EBADFLAGS), + uv::UV_EAI_BADHINTS => Some(Error::EBADHINTS), + uv::UV_EAI_CANCELED => Some(Error::ECANCELLED), + uv::UV_EAI_FAIL => Some(Error::ENOTFOUND), + uv::UV_EAI_FAMILY => Some(Error::EBADFAMILY), + uv::UV_EAI_MEMORY => Some(Error::ENOMEM), + uv::UV_EAI_NODATA => Some(Error::ENODATA), + uv::UV_EAI_NONAME => Some(Error::ENONAME), + uv::UV_EAI_OVERFLOW => Some(Error::ENOMEM), + uv::UV_EAI_PROTOCOL => Some(Error::EBADQUERY), + uv::UV_EAI_SERVICE => Some(Error::ESERVICE), + uv::UV_EAI_SOCKTYPE => Some(Error::ECONNREFUSED), _ => Some(Error::ENOTFOUND), // UV_ENOENT and non documented errors }; } @@ -1738,11 +1741,11 @@ impl Error { pub fn eai_raw_by_name(name: &[u8]) -> Option { #[cfg(windows)] { - use bun_libuv_sys as libuv; + use bun_errno::uv_codes as uv; 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(uv::UV_EAI_AGAIN), + b"EAI_FAIL" => Some(uv::UV_EAI_FAIL), + b"EAI_NONAME" => Some(uv::UV_EAI_NONAME), _ => None, } } diff --git a/src/cares_sys/lib.rs b/src/cares_sys/lib.rs index dcb707d8551a..61bf5e06ea39 100644 --- a/src/cares_sys/lib.rs +++ b/src/cares_sys/lib.rs @@ -9,10 +9,10 @@ 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*`; sin_addr is `in_addr{s_addr}`, + // 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_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 217ecd408f3f..128558d839cb 100644 --- a/src/crash_handler/lib.rs +++ b/src/crash_handler/lib.rs @@ -879,7 +879,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 is actively working on supporting all libuv functions, please see\nthis issue to track our progress:\n\nhttps://github.com/oven-sh/bun/issues/18546\n\n", true), bstr::BStr::new(name) ) .is_err() @@ -1109,7 +1109,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 is actively working on supporting all libuv functions, please see\nthis issue to track our progress:\n\nhttps://github.com/oven-sh/bun/issues/18546\n\n", true), bstr::BStr::new(name) ) .is_err() 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 9f09e9ac1790..3ac72f3c4e34 100644 --- a/src/errno/darwin_errno.rs +++ b/src/errno/darwin_errno.rs @@ -137,16 +137,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 9bc8377f090d..c3d4d30e9681 100644 --- a/src/errno/freebsd_errno.rs +++ b/src/errno/freebsd_errno.rs @@ -133,22 +133,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 7c5c0ec7a90f..c4b5c3f039a5 100644 --- a/src/errno/lib.rs +++ b/src/errno/lib.rs @@ -45,7 +45,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"), @@ -87,14 +87,14 @@ 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). // // `$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] @@ -176,6 +176,8 @@ macro_rules! __uv_e_rows { }; } +pub mod uv_codes; + #[cfg(target_os = "macos")] pub mod darwin_errno; #[cfg(target_os = "macos")] diff --git a/src/errno/linux_errno.rs b/src/errno/linux_errno.rs index 975829fc88ed..38765f4d4bc6 100644 --- a/src/errno/linux_errno.rs +++ b/src/errno/linux_errno.rs @@ -172,10 +172,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..d1b89dee59c5 --- /dev/null +++ b/src/errno/uv_codes.rs @@ -0,0 +1,293 @@ +//! `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_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; + +/// Map a negative `UV_E*` libuv error code to the stable `bun.sys.E` / +/// `bun_errno::E` discriminant (e.g. `UV_ENOENT (-4058)` → `2`). +/// +/// The integer discriminants are ABI-stable POSIX values plus a fixed +/// Bun-assigned tail (`UNKNOWN=134`..`FTYPE=137`). Unmapped codes return +/// `None`. +/// +/// Keep in sync with `E` (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 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, + }) +} diff --git a/src/errno/windows_errno.rs b/src/errno/windows_errno.rs index 44e099ec4ef1..b2a05da4048e 100644 --- a/src/errno/windows_errno.rs +++ b/src/errno/windows_errno.rs @@ -6,11 +6,11 @@ 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 @@ -18,7 +18,7 @@ use bun_libuv_sys as uv; // 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`.) +// lives in `uv_codes::uv_err_to_e_discriminant`.) // // Entry shape: // [UV_X => EX] — UV_X has a non-UV_ counterpart `SystemErrno::EX` @@ -647,7 +647,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 +660,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 +674,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 +710,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 +733,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,13 +748,27 @@ 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 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; + }; + let errno = Win32Error(code).to_system_errno(); + uv::e_discriminant_to_uv(errno as u16).unwrap_or(uv::UV_UNKNOWN) +} + /// 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) @@ -761,7 +776,7 @@ pub fn translate_uv_error_to_e(code: c_int) -> E { .unwrap_or(E::UNKNOWN) } -// Thin adapter over the canonical row table in `bun_libuv_sys::uv_err_to_e_discriminant`. +// Thin adapter over the canonical row table in `uv_codes::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))?; @@ -781,7 +796,7 @@ 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); diff --git a/src/event_loop/AnyEventLoop.rs b/src/event_loop/AnyEventLoop.rs index d237e2f42331..18f3b7440a44 100644 --- a/src/event_loop/AnyEventLoop.rs +++ b/src/event_loop/AnyEventLoop.rs @@ -190,13 +190,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. @@ -463,23 +456,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 245c9500ea3c..7cf0e7d60a61 100644 --- a/src/event_loop/ConcurrentTask.rs +++ b/src/event_loop/ConcurrentTask.rs @@ -69,14 +69,12 @@ pub mod task_tag { BundleV2PluginResolve, // bun.bundle_v2.Resolve (JS-thread hop) BundleV2PluginLoad, // bun.bundle_v2.Load (JS-thread hop) ShellYesTask, // shell.Interpreter.Builtin.Yes.YesTask - Close, CppTask, DuplexUpgradeContext, FetchTasklet, FetchTaskletDeinit, FetchTaskletPromiseSettle, FSWatchTask, - GetAddrInfoLibuvComplete, HotReloadTask, WatchReloadTask, JSBundleCompletionTask, @@ -88,13 +86,10 @@ pub mod task_tag { NativeBrotli, NativeZlib, NativeZstd, - Open, PollPendingModulesTask, PosixSignalTask, MemoryPressureTask, ProcessWaiterThreadTask, - Read, - Readv, FlushPendingFileSinkTask, RuntimeTranspilerStore, S3HttpDownloadStreamingTask, @@ -112,7 +107,6 @@ pub mod task_tag { ShellRmDirTask, ShellRmTask, ShellTouchTask, - StatFS, StatWatcherTimerUpdate, StatWatcherHop, AsyncCpTask, @@ -121,8 +115,6 @@ pub mod task_tag { ThreadSafeFunction, ValkeyDeferredClose, WindowsNamedPipeContext, - Write, - Writev, } } 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..153254fdbcd1 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, }); @@ -208,15 +186,6 @@ impl SpawnSyncEventLoop { } /// 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,57 +193,14 @@ 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); + #[cfg(windows)] + bun_io::windows::close_all_for_loop(self.uws_loop.as_ptr()); // SAFETY: uws_loop was returned by `us_create_loop` in `init` and not yet freed. unsafe { uws::Loop::destroy(self.uws_loop.as_ptr()) }; } @@ -284,52 +210,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 +229,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 +237,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 9a0be447363f..b206001180bc 100644 --- a/src/event_loop/lib.rs +++ b/src/event_loop/lib.rs @@ -9,9 +9,7 @@ pub mod ManagedTask; // ──────────────────────────────────────────────────────────────────────────── // 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/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 50043c09ed1e..d484ce1b26b9 100644 --- a/src/install/PackageInstall.rs +++ b/src/install/PackageInstall.rs @@ -2002,34 +2002,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 e2c97f58844d..76a60bc81ccc 100644 --- a/src/install/PackageManager.rs +++ b/src/install/PackageManager.rs @@ -2210,11 +2210,11 @@ pub fn init( // `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 + // with env/top_level_dir/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 + // without top_level_dir, and without going through + // `init_global`'s loop setup. The shell's IOWriter then opens // stdout/stderr against an under-initialised loop → 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 3e9ae91a974e..448928c214e5 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}; @@ -25,9 +20,8 @@ use bun_io::Loop as AsyncLoop; use bun_io::pipe_reader::PosixFlags; 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, @@ -1023,12 +1017,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 @@ -1056,26 +1044,26 @@ 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. + /// 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], - ipc_output_fds: [Fd; 2], - ) -> Result<(), Error> { + 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 @@ -1100,7 +1088,6 @@ impl<'a> SecurityScanSubprocess<'a> { ) }? .map_err(|e| e.to_zig_err())?; - // `defer spawned.extra_pipes.deinit()` — drops at scope exit. ipc_output_fds[1].close(); @@ -1109,84 +1096,47 @@ impl<'a> SecurityScanSubprocess<'a> { self.ipc_reader.flags.remove(PosixFlags::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(&mut 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 spawn_options = SpawnOptions { @@ -1195,10 +1145,6 @@ impl<'a> SecurityScanSubprocess<'a> { 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() }; @@ -1212,32 +1158,14 @@ impl<'a> SecurityScanSubprocess<'a> { ) }? .map_err(|e| e.to_zig_err())?; - // `defer spawned.extra_pipes.deinit()` — drops at scope exit. - 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(()) + // `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(); + + self.finish_spawn(&mut spawned, ipc_read_fd, json_write_fd) } /// Common post-spawn setup: start the fd 3 reader, attach the process, @@ -1246,13 +1174,7 @@ impl<'a> SecurityScanSubprocess<'a> { &mut self, spawned: &mut 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 @@ -1266,13 +1188,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). + // `to_process` consumes the `SpawnResult`; take ownership of it 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: *mut Process = process_handle.as_ptr(); @@ -1291,13 +1209,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(); @@ -1364,7 +1291,7 @@ impl<'a> SecurityScanSubprocess<'a> { } pub(crate) fn loop_(&mut self) -> *mut AsyncLoop { - self.manager.event_loop.native_loop() + self.manager.event_loop.loop_() } pub(crate) fn on_reader_done(&mut self) { @@ -1412,9 +1339,8 @@ impl<'a> SecurityScanSubprocess<'a> { // 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). + // Windows: the read end is overlapped, so it cannot be drained with + // a synchronous read. #[cfg(not(windows))] { let fd = self.ipc_reader.get_fd(); diff --git a/src/install/error.rs b/src/install/error.rs index 1b2e2e464587..e480676e1bd7 100644 --- a/src/install/error.rs +++ b/src/install/error.rs @@ -402,7 +402,7 @@ 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::MakeCrtOwned(_) => 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..2c6bb94ccc4b 100644 --- a/src/install/extract_tarball.rs +++ b/src/install/extract_tarball.rs @@ -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..f77176b89aa2 100644 --- a/src/install/git_runner.rs +++ b/src/install/git_runner.rs @@ -15,12 +15,9 @@ use bun_io::BufferedReader; use bun_io::{FilePollFlag, PosixFlags}; 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,37 +633,31 @@ 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); - reader.with_mut(|r| { - r.flags.insert(PosixFlags::NONBLOCKING | PosixFlags::SOCKET); - r.start(fd, true) - })?; + // POSIX: the parent end is a socketpair half. + #[cfg(unix)] + { + let _ = bun_sys::set_nonblocking(fd); + reader.with_mut(|r| r.flags.insert(PosixFlags::NONBLOCKING | PosixFlags::SOCKET)); + } + 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); + continue; + } + #[cfg(unix)] 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); - }); - this.remaining_fds.set(this.remaining_fds.get() + 1); - reader.with_mut(|r| r.start_with_current_pipe())?; - } debug_assert!(this.process.get().is_none()); let process = spawned.to_process_handle(this.event_loop); @@ -972,6 +930,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/isolated_install/Symlinker.rs b/src/install/isolated_install/Symlinker.rs index 52c90735abba..fcf709085211 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. + // Junctions made by Bun versions that used libuv 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 f415b55cf5e5..5a55f48d3aaf 100644 --- a/src/install/lifecycle_script_runner.rs +++ b/src/install/lifecycle_script_runner.rs @@ -14,18 +14,13 @@ use bun_io::heap as io_heap; use bun_io::{FilePollFlag, PosixFlags}; 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,9 +396,7 @@ 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 `PosixBufferedReader` for a fresh socket fd. #[cfg(unix)] fn reset_output_flags(output: &mut OutputReader, fd: Fd) { output @@ -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,47 @@ 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); - } + )??; + + 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 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); - } + (*this).remaining_fds += 1; + // POSIX: the parent end is a socketpair half. + #[cfg(unix)] + { + let _ = bun_sys::set_nonblocking(fd); + Self::reset_output_flags(&mut *output, fd); } - } - #[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()?; + 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); + 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()?; + #[cfg(unix)] + if let Some(poll) = (*output).handle.get_poll() { + poll.set_flag(FilePollFlag::Socket); } } @@ -1183,7 +1085,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 b2ac4d74b15b..4df9a52f5857 100644 --- a/src/install/windows-shim/Cargo.toml +++ b/src/install/windows-shim/Cargo.toml @@ -20,7 +20,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 4158df487603..2c99c8a1dec3 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 diff --git a/src/install/windows-shim/main.rs b/src/install/windows-shim/main.rs index 90f8d9c38250..61179ad33ba0 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 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..c24fbea56f87 100644 --- a/src/io/MaxBuf.rs +++ b/src/io/MaxBuf.rs @@ -168,14 +168,19 @@ impl MaxBuf { /// 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). 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 762e064ccd1b..6e0d48b40756 100644 --- a/src/io/PipeReader.rs +++ b/src/io/PipeReader.rs @@ -5,17 +5,9 @@ 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))] +// 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 @@ -25,18 +17,10 @@ use crate::max_buf::MaxBuf; use crate::pipes::{Chunk, FileType, PollOrFd, ReadState}; #[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 _; +use crate::windows::ReadEvent; -// All logging in this module goes through `bun.sys.syslog` (the `SYS` scope) -// or `libuv::log!`. +// All logging in this module goes through `bun.sys.syslog` (the `SYS` scope). // ────────────────────────────────────────────────────────────────────────── // BufferedReaderVTable @@ -986,45 +970,25 @@ impl Drop for PosixBufferedReader { // WindowsBufferedReader // ────────────────────────────────────────────────────────────────────────── +/// 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. #[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. + /// `None` before `start` and after `close`. pub source: Option, pub(crate) _offset: usize, limit: ReadLimit, pub _buffer: Vec, - // for compatibility with Linux - pub flags: WindowsFlags, + pub flags: PosixFlags, 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()) - } -} +const WINDOWS_READ_CHUNK: usize = 64 * 1024; #[cfg(windows)] impl WindowsBufferedReader { @@ -1038,7 +1002,7 @@ impl WindowsBufferedReader { _offset: 0, limit: ReadLimit::NONE, _buffer: Vec::new(), - flags: WindowsFlags::new(), + flags: PosixFlags::new(), maxbuf: None, vtable: BufferedReaderVTable::init::(), } @@ -1047,31 +1011,21 @@ impl WindowsBufferedReader { #[inline] pub fn is_done(&self) -> bool { self.flags.intersects( - WindowsFlags::IS_DONE - | WindowsFlags::RECEIVED_EOF - | WindowsFlags::CLOSED_WITHOUT_REPORTING, + PosixFlags::IS_DONE | PosixFlags::RECEIVED_EOF | PosixFlags::CLOSED_WITHOUT_REPORTING, ) } pub fn from(&mut self, other: &mut WindowsBufferedReader, 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(PosixFlags::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); @@ -1085,23 +1039,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(PosixFlags::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(PosixFlags::KEEP_ALIVE, value); if let Some(source) = self.source.as_mut() { if value { source.ref_(); @@ -1135,18 +1098,7 @@ impl WindowsBufferedReader { } pub fn has_pending_read(&self) -> bool { - if self.flags.contains(WindowsFlags::HAS_INFLIGHT_READ) { - return true; - } - let Some(source) = &self.source else { - return false; - }; - match source { - Source::File(file) | Source::SyncFile(file) => { - file.state != crate::source::FileState::Deinitialized - } - _ => false, - } + self.source.as_ref().is_some_and(Source::is_reading) } /// Charges `bytes_read` against the `maxBuffer` budget, returning `true` @@ -1159,15 +1111,12 @@ impl WindowsBufferedReader { MaxBuf::on_read_bytes(maxbuf, bytes_read as u64) } + /// Dispatches what is in `_buffer`. Returns `false` when the reader was + /// closed from inside the dispatch. fn on_read_chunk(&mut self, has_more: ReadState) -> bool { if has_more == ReadState::Eof { - self.flags.insert(WindowsFlags::RECEIVED_EOF); + self.flags.insert(PosixFlags::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; } @@ -1184,10 +1133,7 @@ impl WindowsBufferedReader { 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; } } @@ -1198,18 +1144,13 @@ impl WindowsBufferedReader { } fn finish(&mut self) { - self.flags.remove(WindowsFlags::HAS_INFLIGHT_READ); - self.flags.insert(WindowsFlags::IS_DONE); + self.flags.insert(PosixFlags::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(); } @@ -1226,103 +1167,77 @@ 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)); + /// 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(WINDOWS_READ_CHUNK)).max(1) + } + + /// With a byte budget, the size of each read depends on the one before it. + fn may_read_ahead(&self) -> bool { + self.limit.0.is_none() && self.maxbuf.is_none() + } + + /// On Windows `is_pollable` says where the HANDLE came from: `true` for an + /// overlapped pipe end Bun created (a child's stdio, a pseudoconsole pipe), + /// which is driven through the loop's completion port directly; `false` + /// for anything else, which is classified by `GetFileType` and, if it is a + /// pipe, treated as somebody else's. 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<()> { + debug_assert!(self.source.is_none()); + // The parent's loop, not the thread's: `spawnSync` reads on its own. + let loop_ = self.vtable.loop_(); + let close_fd = self.flags.contains(PosixFlags::CLOSE_HANDLE); + let source = if is_pollable { + Source::open_owned_pipe(loop_, fd, close_fd)? + } else { + Source::open(loop_, fd, close_fd)? + }; + self.flags.set(PosixFlags::POLLABLE, is_pollable); + self.start_with_source(source) + } + + /// Read from a source that is already open (an accepted or connected + /// named pipe). + pub fn start_with_source(&mut self, source: Source) -> sys::Result<()> { + debug_assert!(self.source.is_none()); + if !self.flags.contains(PosixFlags::KEEP_ALIVE) { + source.unref(); } - 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.source = Some(source); self.buffer().clear(); - self.flags.remove(WindowsFlags::IS_DONE); + self.flags.remove(PosixFlags::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 + // 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::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`). - #[cfg(windows)] - 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); + // 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(PosixFlags::IS_PAUSED) || self.limit.reached() { + return sys::Result::Ok(()); } + let result = self.start_reading(); + if result.is_err() { + self.release_source(); + } + result } - /// `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() }; - } - - pub fn start(&mut self, fd: Fd, _: bool) -> 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 - 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() + /// 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(); + } } 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.flags.insert(PosixFlags::USE_PREAD); self.start(fd, poll) } @@ -1342,442 +1257,140 @@ impl WindowsBufferedReader { source.set_raw_mode(value) } - #[cfg(windows)] - 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); + fn start_reading(&mut self) -> sys::Result<()> { + if self.flags.contains(PosixFlags::IS_DONE) || self.limit.reached() { + return sys::Result::Ok(()); } - } - - #[cfg(windows)] - 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; + let this: *mut Self = self; + let len = self.next_read_len(); + let read_ahead = self.may_read_ahead(); + let offset = self + .flags + .contains(PosixFlags::USE_PREAD) + .then_some(self._offset as u64); + match self.source.as_mut() { + None => sys::Result::Err(sys::Error::from_code(sys::E::BADF, sys::Tag::read)), + Some(Source::Pipe(pipe)) => { + pipe.set_read_size(len); + pipe.set_read_ahead(read_ahead); + pipe.read_start(this, Self::on_source_read) } - 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); + 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) } } } - /// Callback fired when a file read operation completes or is canceled. - /// Handles cleanup, cancellation, and normal read processing. - #[cfg(windows)] - 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; - } - - // 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; - } - - if this.flags.contains(WindowsFlags::IS_DONE) { - return; - } - - 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, - ); + /// # 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) => { + let len = data.len(); + // SAFETY: as above. + unsafe { + 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); } - - // 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); - } - } + } + // SAFETY: as above. + ReadEvent::Eof => 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(PosixFlags::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) }; + } } } - #[cfg(windows)] - 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() - { - 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)); + 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 }; - 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() } { - return sys::Result::Ok(()); - } - - // 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); + // 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); - 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); - } + if has_more == ReadState::Eof { + self.close(); + return; + } + if self.flags.contains(PosixFlags::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(); + let read_ahead = self.may_read_ahead(); + match self.source.as_mut() { + Some(Source::Pipe(pipe)) => { + pipe.set_read_size(len); + pipe.set_read_ahead(read_ahead); } - 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); + 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 stop_reading(&mut self) -> sys::Result<()> { - if self.flags.contains(WindowsFlags::IS_DONE) - || self.flags.contains(WindowsFlags::IS_PAUSED) - { + if self.flags.contains(PosixFlags::IS_DONE) || self.flags.contains(PosixFlags::IS_PAUSED) { return sys::Result::Ok(()); } - self.flags.insert(WindowsFlags::IS_PAUSED); - let Some(source) = self.source.as_mut() else { - return sys::Result::Ok(()); - }; - match source { - Source::File(file) | Source::SyncFile(file) => { - file.stop(); - } - Source::Pipe(_) | Source::Tty(_) => { - // SAFETY: stream handle is live (just matched a stream source). - unsafe { uv::uv_read_stop(source.to_stream()) }; + self.flags.insert(PosixFlags::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 => {} } 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)); - } - } - } - #[cfg(windows)] - 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); - } - } - #[cfg(windows)] - 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); - } - #[cfg(not(windows))] - _ => {} - } - // self.source already None via take(). + 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. + if self.source.take().is_some() { + self.flags.insert(PosixFlags::IS_PAUSED); if CALL_DONE { self.done(); } @@ -1785,117 +1398,19 @@ impl WindowsBufferedReader { } /// 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); - } - } + self.close_impl::(); self._buffer = Vec::new(); } - #[cfg(windows)] - 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) }); - } - - #[cfg(windows)] - 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) }; - 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(); - } - } - pub fn pause(&mut self) { let _ = self.stop_reading(); } @@ -1903,22 +1418,30 @@ impl WindowsBufferedReader { 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(PosixFlags::IS_PAUSED); + self.on_read(0, ReadState::Eof); return; } - let _ = self.start_reading(); + if !self.flags.contains(PosixFlags::IS_PAUSED) { + return; + } + self.flags.remove(PosixFlags::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) }; + } } /// # Safety /// `this` is the live reader. Raw for signature parity with the POSIX /// entry (callers dispatch through a `*mut`); the body only unpauses. 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 + // Nothing on Windows can be read without waiting for its completion. // 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. @@ -1929,32 +1452,10 @@ 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); - } - } } } diff --git a/src/io/PipeWriter.rs b/src/io/PipeWriter.rs index 859c676aa2f8..37f22a97e533 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}; @@ -1096,257 +1088,14 @@ 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), -#[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(); - } - } - - fn enable_keeping_process_alive(&mut self, event_loop: EventLoopHandle) { - self.update_ref(event_loop, true); - } - - fn disable_keeping_process_alive(&mut self, event_loop: EventLoopHandle) { - self.update_ref(event_loop, false); - } - - 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()) }; - } - } - - 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(); - } - } - } - - /// 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); - } - } - } - - 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()) - } -} - -#[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) }); -} - -#[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) }); - } -} - -/// Common parent requirements for Windows writers (event loop access + ref counting). +/// 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). @@ -1358,7 +1107,7 @@ extern "C" fn on_tty_close(handle: *mut uv::uv_tty_t) { pub trait WindowsWriterParent { /// # Safety /// `this` must point to a live `Self`. - unsafe fn loop_(this: *mut Self) -> *mut uv::Loop; + 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); @@ -1367,6 +1116,65 @@ pub trait WindowsWriterParent { unsafe fn deref(this: *mut Self); } +/// Open what `fd` is for writing; see `WindowsBufferedReader::start` for what +/// `is_pollable` means on Windows. +#[cfg(windows)] +fn open_source_for_writing( + parent: *mut Parent, + fd: Fd, + is_pollable: bool, + close_fd: bool, +) -> sys::Result { + // 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_(parent) }; + if is_pollable { + Source::open_owned_pipe(loop_, fd, close_fd) + } else { + Source::open(loop_, fd, close_fd) + } +} + +/// Hand `data` to `source`; `on_write(ctx, ..)` runs from the loop afterwards. +/// +/// # Safety +/// `data` and `ctx` stay valid until `on_write` has run. +#[cfg(windows)] +unsafe fn submit_write( + source: &mut Source, + data: &[u8], + 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, 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) }, + } +} + +/// Write all of `data` before returning. +#[cfg(windows)] +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 // ────────────────────────────────────────────────────────────────────────── @@ -1386,6 +1194,7 @@ pub trait WindowsBufferedWriterParent: WindowsWriterParent { /// # Safety /// `this` must point to a live `Self`. unsafe fn on_close(_this: *mut Self) {} + /// 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]; @@ -1398,14 +1207,10 @@ pub trait WindowsBufferedWriterParent: WindowsWriterParent { #[cfg(windows)] 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, } @@ -1414,72 +1219,19 @@ impl Default for WindowsBufferedWriter 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, } } } #[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. +// SAFETY: write completions 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 { @@ -1496,29 +1248,16 @@ 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`]. + /// Laundered-receiver variant of [`parent_on_error`](Self::parent_on_error): + /// 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. #[inline(always)] fn r_on_error(this: *mut Self, err: sys::Error) { let parent = Self::r(this).parent; @@ -1526,9 +1265,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; @@ -1538,34 +1276,111 @@ 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 get_fd(&self) -> Fd { + self.source.as_ref().map_or(Fd::INVALID, Source::get_fd) + } + + 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 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 `WindowsBufferedReader::start` 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()); + let source = open_source_for_writing(self.parent, fd, is_pollable, self.close_fd)?; + self.source = Some(source); + self.is_done = false; + self.write(); + sys::Result::Ok(()) + } + + /// `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 && Parent::HAS_ON_CLOSE { + // 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); + } + + /// # 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) { + // PORT_NOTES_PLAN R-2: `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, writing `self.is_done`. Launder so + // post-`on_write` reads see fresh state. + let this: *mut Self = core::hint::black_box(this); // 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); - return; - } + let written = match result { + // Closed with the write still out; `close()` told the parent already. + 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. @@ -1596,150 +1411,36 @@ impl WindowsBufferedWriter { } } - 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); + pub fn write(&mut self) { + // if we are already done or if we have some pending payload we just wait until next write + // 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; } - - 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); + // 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; } - - // on_write_complete handles the deref (and is itself laundered). - Self::r(this).on_write_complete(uv::ReturnCode::zero()); - } - - pub fn write(&mut self) { - let buffer = self.get_buffer_internal(); - // 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 { + let this: *mut Self = self; + let Some(source) = self.source.as_mut() else { return; - } - - // 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); + }; + let len = buffer.len(); + // 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. + match unsafe { submit_write(source, buffer, this, Self::on_write_result) } { + sys::Result::Err(err) => { 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. + sys::Result::Ok(()) => { + self.pending_payload_size = len; + // The matching deref is in `on_write_result`, which runs for + // every submitted write, cancelled ones included. // SAFETY: parent BACKREF valid; intrusive refcount bump. unsafe { Parent::ref_(self.parent()) }; } @@ -1765,6 +1466,13 @@ impl WindowsBufferedWriter { } } +#[cfg(windows)] +impl Drop for WindowsBufferedWriter { + fn drop(&mut self) { + self.close_source(false); + } +} + // ────────────────────────────────────────────────────────────────────────── // StreamBuffer // ────────────────────────────────────────────────────────────────────────── @@ -1935,18 +1643,14 @@ 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, // we preserve the last write result for simplicity pub(crate) last_write_result: WriteResult, @@ -1960,11 +1664,9 @@ 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(), last_write_result: WriteResult::Wrote(0), @@ -1973,56 +1675,6 @@ impl Default for WindowsStreamingWriter 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 @@ -2041,20 +1693,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 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. #[inline(always)] fn r_on_error(this: *mut Self, err: sys::Error) { let parent = Self::r(this).parent; @@ -2062,9 +1703,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; @@ -2072,12 +1710,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; @@ -2091,55 +1725,152 @@ impl WindowsStreamingWriter { mem::size_of::() + self.current_payload.memory_cost() + self.outgoing.memory_cost() } + pub fn get_fd(&self) -> Fd { + self.source.as_ref().map_or(Fd::INVALID, Source::get_fd) + } + pub fn has_pending_data(&self) -> bool { self.outgoing.is_not_empty() || self.current_payload.is_not_empty() } - /// 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() } - 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 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 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 `WindowsBufferedReader::start` 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()); + let source = open_source_for_writing(self.parent, fd, is_pollable, 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. + if let Source::Pipe(pipe) = &mut source { + pipe.adopt_write_buffer(mem::take(&mut self.current_payload.list)); + } + drop(source); + } + } + + pub fn close(&mut self) { + let report = !self.closed_without_reporting; + self.close_source(report); + } + + /// 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) { + // PORT_NOTES_PLAN R-2: `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); // 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); + let submitted = Self::r(this).current_payload.size(); + let written = match result { + // Closed with the write still out; `close()` told the parent already. + 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::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(); + return; + } + sys::Result::Ok(written) => written.min(submitted), + }; + + 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); - // `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(); + Self::r(this).send_current_payload(); return; } - - // success means that we send all the data inside current_payload - let written = Self::r(this).current_payload.size(); Self::r(this).current_payload.reset(); // if we dont have more outgoing data we report done in onWrite @@ -2186,204 +1917,88 @@ impl WindowsStreamingWriter { } } - 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); + /// Report a failure to start a write like a failed write. + fn fail_send(this: *mut Self, err: sys::Error) { + Self::r(this).last_write_result = WriteResult::Err(err.clone()); + Self::r_on_error(this, err); + core::hint::black_box(this); + // See `on_write_result`: the parent must get `on_close`. + Self::r(this).close(); + } - // 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)) }); - } + /// Hand `current_payload` to the source. + fn send_current_payload(&mut self) { + // PORT_NOTES_PLAN R-2: 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::PIPE, sys::Tag::pipe)); return; - } - - // 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 + }; + // 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. + let submitted = unsafe { + submit_write( + source, + (*this).current_payload.slice(), + this, + Self::on_write_result, + ) + }; + if let sys::Result::Err(err) = submitted { Self::r(this).current_payload.reset(); - Self::r_deref(this); - 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); + Self::fail_send(this, err); return; } - - // on_write_complete handles the deref (and is itself laundered). - Self::r(this).on_write_complete(uv::ReturnCode::zero()); + // The matching deref is in `on_write_result`, which runs for every + // submitted write, cancelled ones included. + // 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); } /// 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() { // 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; - } - } - // 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); + /// `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::PIPE, sys::Tag::pipe)); + }; + let result = match self.outgoing.write_or_fallback(buffer_u8, buffer_u16, kind) { + Err(_) => WriteResult::Err(sys::Error::oom()), + Ok(bytes) => match write_blocking(source, bytes) { + sys::Result::Err(err) => WriteResult::Err(err), + sys::Result::Ok(0) => WriteResult::Done(0), + sys::Result::Ok(wrote) => WriteResult::Wrote(wrote), + }, + }; + self.outgoing.reset(); + result } fn write_internal_u8(&mut self, buffer: &[u8], kind: WriteKind) -> WriteResult { @@ -2391,39 +2006,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(); @@ -2447,43 +2031,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(); @@ -2530,31 +2079,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 } } @@ -2587,7 +2120,9 @@ pub type StreamingWriter

= WindowsStreamingWriter

; // (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. +// (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 that triple once per parent. // // `borrow = mut` → bodies form `&mut *this` (unique access for the @@ -2612,8 +2147,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; } @@ -2637,7 +2170,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, ) => { @@ -2687,11 +2219,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) { @@ -2771,7 +2303,6 @@ 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, ) => { @@ -2815,11 +2346,12 @@ macro_rules! impl_buffered_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 $el_this = this; #[allow(unused_unsafe)] - unsafe { $uv } + let event_loop: $crate::EventLoopHandle = unsafe { $el }; + event_loop.loop_() } #[inline] unsafe fn ref_(this: *mut Self) { 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 4df690e14bf0..56b48fcf97b2 100644 --- a/src/io/lib.rs +++ b/src/io/lib.rs @@ -5,8 +5,9 @@ // ════════════════════════════════════════════════════════════════════════════ // 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` and +// the Windows*Reader/Writer impls 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 +29,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 +113,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 +124,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(); @@ -204,9 +200,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 +263,7 @@ 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 posix_event_loop::{FilePoll, Loop, Store}; pub use posix_event_loop::{AllocatorType, Owner, PollTag, get_vm_ctx, js_vm_ctx}; @@ -304,13 +271,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 +317,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 @@ -392,16 +353,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 +364,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 +377,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}; @@ -454,8 +397,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"] @@ -471,6 +412,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; @@ -660,9 +603,6 @@ pub use pipe_writer::{BufferedWriter, StreamBuffer, StreamingWriter, WriteResult #[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 open_for_writing_mod::{open_for_writing, open_for_writing_impl}; @@ -1284,45 +1224,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 @@ -1772,7 +1673,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}; @@ -1849,19 +1749,7 @@ impl FilePollRef { } #[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( @@ -1874,20 +1762,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 { @@ -1899,14 +1775,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 { @@ -2112,116 +1981,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; @@ -2229,9 +2055,7 @@ pub mod closer { } } - #[cfg(not(windows))] impl Closer { - /// `_compat`: for signature compatibility with the Windows version. pub fn close(fd: Fd, _compat: ()) { debug_assert!(fd.is_valid()); WorkPool::schedule_owned(Box::new(Closer { @@ -2243,70 +2067,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 ac898e686bea..bedf5d562609 100644 --- a/src/io/openForWriting.rs +++ b/src/io/openForWriting.rs @@ -173,9 +173,11 @@ 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 or taken from an fd + // never is: the writer classifies it on its own. + let _ = force_sync; + *pollable = false; return Ok(fd); } } diff --git a/src/io/pipes.rs b/src/io/pipes.rs index 5cc50a9079e6..07ee6638be06 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")] @@ -62,7 +61,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 +94,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 6ff1bb4dff18..ae42bb1dfb8e 100644 --- a/src/io/posix_event_loop.rs +++ b/src/io/posix_event_loop.rs @@ -2,34 +2,14 @@ use core::ffi::c_int; use core::ffi::c_void; use core::fmt; -#[cfg(unix)] use core::ptr; -#[cfg(not(windows))] +use bun_sys::syslog; 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; - /// Local `errno_sys` helper. `bun_sys` /// does not yet expose this helper on `Result`; once it does, drop this and /// call `sys::Result::<()>::errno_sys` directly. @@ -131,9 +111,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 // ────────────────────────────────────────────────────────────────────────── @@ -141,7 +118,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). @@ -244,7 +221,6 @@ pub struct Owner { } impl Owner { - #[cfg(not(windows))] pub(crate) const NULL: Owner = Owner { tag: PollTag::Null, ptr: core::ptr::null_mut(), @@ -258,7 +234,6 @@ impl Owner { self.ptr.is_null() } #[inline] - #[cfg(not(windows))] pub(crate) fn clear(&mut self) { *self = Self::NULL; } @@ -280,11 +255,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, @@ -299,9 +271,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; @@ -358,6 +333,22 @@ impl FilePoll { self.on_update(0); } + #[cfg(windows)] + pub(crate) fn on_socket_event( + &mut self, + loop_: *mut Loop, + event: bun_uws_sys::loop_::EventType, + ) { + self.update_flags(Flags::from_socket_event(event)); + // The loop re-arms a socket poll by itself; a one-shot owner hears + // nothing more until it registers again. + if self.flags.contains(Flags::OneShot) && !self.socket_poll.is_null() { + // SAFETY: `socket_poll` is this poll's live registration on `loop_`. + unsafe { bun_uws_sys::iocp::us_iocp_poll_socket_change(loop_, self.socket_poll, 0) }; + } + self.on_update(0); + } + pub fn is_readable(&mut self) -> bool { let readable = self.flags.contains(Flags::Readable); self.flags.remove(Flags::Readable); @@ -474,10 +465,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); } @@ -492,10 +480,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); } } @@ -523,6 +508,8 @@ impl FilePoll { .wrapping_add(1), #[cfg(not(all(target_os = "macos", debug_assertions)))] generation_number: 0, + #[cfg(windows)] + socket_poll: ptr::null_mut(), } } @@ -560,38 +547,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!( @@ -832,6 +788,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 { @@ -868,40 +872,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) @@ -914,6 +898,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) @@ -1138,6 +1123,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); @@ -1151,7 +1144,6 @@ impl FilePoll { } } -#[cfg(not(windows))] impl fmt::Display for FilePoll { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -1268,6 +1260,25 @@ 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 + } } #[allow(dead_code)] @@ -1294,21 +1305,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 { @@ -1412,18 +1419,15 @@ impl Store { // on a tuple). Since the union has exactly one variant, wrap the raw // `TaggedPtr` directly with the same tag scheme (`1024 - index`). #[derive(Copy, Clone)] -#[allow(dead_code)] pub(crate) struct Pollable { repr: bun_collections::TaggedPtr, } impl Pollable { /// Tag value for `FilePoll` (index 0 → `1024 - 0`). - #[allow(dead_code)] pub(crate) const FILE_POLL_TAG: u16 = 1024; #[inline] - #[allow(dead_code)] pub(crate) fn init(ptr: *const crate::FilePoll) -> Self { Self { repr: bun_collections::TaggedPtr::init(ptr, Self::FILE_POLL_TAG), @@ -1431,7 +1435,6 @@ impl Pollable { } #[inline] - #[allow(dead_code)] pub(crate) fn from(val: *mut c_void) -> Self { Self { repr: bun_collections::TaggedPtr::from(val), @@ -1439,33 +1442,21 @@ impl Pollable { } #[inline] - #[allow(dead_code)] pub(crate) fn tag(self) -> u16 { self.repr.data() } #[inline] - #[allow(dead_code)] pub(crate) fn as_file_poll(self) -> *mut crate::FilePoll { self.repr.get::() } #[inline] - #[allow(dead_code)] pub(crate) fn ptr(self) -> *mut c_void { self.repr.to() } } -// `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` @@ -1499,6 +1490,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(loop_, ev); } #[cfg(target_os = "macos")] @@ -1515,7 +1508,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..64d84acb8920 100644 --- a/src/io/source.rs +++ b/src/io/source.rs @@ -1,487 +1,136 @@ -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_uws_sys::Loop; -bun_core::declare_scope!(PipeSource, hidden); +use crate::windows::{self, File, Pipe, 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() } + /// Classify `fd` and take it over. A pipe opened this way is treated as + /// somebody else's (inherited stdio, an fd that came from JS): see + /// [`Pipe::open_foreign`]. `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, close_fd: bool) -> bun_sys::Result { + let handle = fd.native(); + let file_type = bun_sys::windows::GetFileType(handle); + bun_core::scoped_log!(PipeSource, "open(fd: {}, type: {})", fd, file_type); + match file_type { + bun_sys::windows::FILE_TYPE_PIPE => { + Pipe::open_foreign(loop_, fd, close_fd).map(Source::Pipe) + } + // `NUL` and serial ports are character devices too. + bun_sys::windows::FILE_TYPE_CHAR if windows::tty::is_console(handle) => { + Tty::open(loop_, fd, close_fd).map(Source::Tty) + } + FILE_TYPE_UNKNOWN => { + let err = bun_sys::windows::Win32Error::get(); + if err == bun_sys::windows::Win32Error::SUCCESS { + return File::open(loop_, fd, close_fd).map(Source::File); + } + Err(bun_sys::Error::from_win32(err, bun_sys::Tag::open).with_fd(fd)) + } + _ => File::open(loop_, fd, close_fd).map(Source::File), + } } - /// 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 over an overlapped pipe end that Bun created itself (a spawned + /// child's stdio, a pseudoconsole pipe): see [`Pipe::open_owned`]. + pub fn open_owned_pipe(loop_: *mut Loop, fd: Fd, close_fd: bool) -> bun_sys::Result { + bun_core::scoped_log!(PipeSource, "open_owned_pipe(fd: {})", fd); + Pipe::open_owned(loop_, fd, close_fd).map(Source::Pipe) } + /// Closed from under its owner by a loop teardown. 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, + Source::Tty(tty) => tty.is_closed(), + Source::File(_) => false, } } 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, + Source::Tty(tty) => tty.is_active(), + Source::File(_) => true, } } - pub(crate) fn to_stream(&mut self) -> *mut uv::uv_stream_t { + /// 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 { - // 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!(), + Source::Pipe(pipe) => pipe.fd(), + Source::Tty(tty) => tty.fd(), + Source::File(file) => file.fd(), } } - 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), - } - } - - pub fn set_data(&mut self, data: *mut c_void) { - 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, - ), - } - } - - /// 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); + /// Leave the `Fd` the source was opened with open when the source closes. + pub fn disown(&mut self) { + match self { + Source::Pipe(pipe) => pipe.disown(), + Source::Tty(tty) => tty.disown(), + Source::File(file) => file.disown(), } - - // 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)) - } + /// 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 callback is what releases the bytes it borrowed. + pub fn close(self) { + match self { + Source::Pipe(pipe) => pipe.close(), + Source::Tty(tty) => tty.close(), + Source::File(file) => drop(file), } } - /// Direct accessor for the `File`/`SyncFile` arm. - /// Panics on Pipe/Tty — callers gate on `matches!(.., File | SyncFile)`. - pub(crate) fn file(&self) -> &File { + pub(crate) fn is_reading(&self) -> bool { match self { - Source::SyncFile(file) | Source::File(file) => file, - _ => unreachable!("Source::file() on non-file source"), + Source::Pipe(pipe) => pipe.is_reading(), + Source::Tty(tty) => tty.is_reading(), + Source::File(file) => file.is_busy(), } } pub(crate) fn set_raw_mode(&mut self, value: bool) -> bun_sys::Result<()> { 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 { + Source::Tty(tty) => tty.set_mode(if value { + windows::tty::Mode::Raw + } else { + windows::tty::Mode::Normal + }), + _ => Err(bun_sys::Error { errno: bun_sys::E::NOTSUP as _, syscall: bun_sys::Tag::uv_tty_set_mode, fd: self.get_fd(), @@ -491,83 +140,22 @@ impl Source { } } -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()) }; - } - - // 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()) }) - } -} +const FILE_TYPE_UNKNOWN: u32 = 0; -/// 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..d5c3c0b233cc --- /dev/null +++ b/src/io/windows/README.md @@ -0,0 +1,88 @@ +# `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 `PeekNamedPipe` reports, for three reasons: 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; and a pending N-byte read +is charged against the writer's `WriteQuotaAvailable`, which has made +Cygwin/MSYS writers believe the pipe is full. + +While the owner handles a chunk, the thread takes the next one only if it was +already in the pipe when that chunk was read. Reading the next chunk during the +owner's callback is where the throughput comes from (waiting ahead without +taking gains nothing); in a bulk stream the pipe has more data after a read +more than 99% of the time. What arrives later stays in the pipe until the loop +asks, so `pause()` from a `data` handler leaves it for a child that inherits the +handle, as on POSIX. + +## 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. +- 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: ring read 6700 / 2456 / 588; + reader thread with one chunk of read-ahead 4342 / 2591 / 605; a pool work + item per chunk 2730 / 1579 / 494; libuv-style inline peek+read 4984 / 2310 / 581. + +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..fcac8770cbbe --- /dev/null +++ b/src/io/windows/file.rs @@ -0,0 +1,415 @@ +//! 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. + +use core::ffi::c_void; +use core::ptr::NonNull; +use core::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use std::sync::Arc; + +use bun_sys::{self as sys, E, Fd, Tag}; +use bun_threading::work_pool::{IntrusiveWorkTask as _, Task, WorkPool}; +use bun_uws_sys::Loop; +use bun_uws_sys::iocp::{Op, OverlappedEntry}; + +use super::pipe::ReadEvent; +use super::{Callback, Link, Port}; + +/// 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. +pub struct File { + inner: NonNull, +} + +const IDLE: u8 = 0; +/// On the work pool's queue: [`File::cancel`] can still win. +const QUEUED: u8 = 1; +const RUNNING: u8 = 2; +const CANCELED: u8 = 3; + +type ReadCallback = unsafe fn(*const (), *mut c_void, ReadEvent<'_>); + +/// Who hears about reads. Kept apart from the request so it can change while +/// the work pool holds the request. +#[derive(Clone, Copy)] +struct Reader { + ctx: *mut c_void, + f: *const (), + call: ReadCallback, +} + +enum Request { + None, + Read { + len: usize, + offset: Option, + }, + Write { + data: *const u8, + len: usize, + callback: Callback>, + }, +} + +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, + loop_: *mut Loop, + port: Arc, + state: AtomicU8, + /// The worker has handed the result to the port. + posted: AtomicBool, + request: Request, + reader: Option, + buf: 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. + pub fn open(loop_: *mut Loop, fd: Fd, close_fd: 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, + loop_, + port, + state: AtomicU8::new(IDLE), + posted: AtomicBool::new(false), + request: Request::None, + reader: None, + buf: 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 + // never looks at `close_fd`. + 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 } + } + + /// 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<'_>)) { + unsafe fn call(f: *const (), ctx: *mut c_void, event: ReadEvent<'_>) { + // SAFETY: `f` was erased from exactly this type below. + let f = + unsafe { core::mem::transmute::<*const (), unsafe fn(*mut T, ReadEvent<'_>)>(f) }; + // SAFETY: the owner keeps `ctx` valid while the read is out. + unsafe { f(ctx.cast::(), event) } + } + // SAFETY: `inner` is live while the owner's `File` is; the work pool + // never looks at `reader`. + unsafe { + (*self.inner.as_ptr()).reader = Some(Reader { + ctx: ctx.cast(), + f: on_read as *const (), + call: call::, + }); + } + } + + /// 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, in which case `on_write` still runs. + pub unsafe fn write( + &mut self, + data: &[u8], + ctx: *mut T, + on_write: unsafe fn(*mut T, sys::Result), + ) -> sys::Result<()> { + self.start(Request::Write { + data: data.as_ptr(), + len: data.len(), + callback: Callback::new(ctx, on_write), + }) + } + + 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).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, its completion is the only thing that touches it again. + unsafe { + (*this).owner_gone = true; + if (*this).state.load(Ordering::Acquire) == IDLE && !(*this).completing { + 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; + let _ = (*this).state.compare_exchange( + QUEUED, + CANCELED, + Ordering::AcqRel, + Ordering::Acquire, + ); + // A write that is running reads the owner's bytes, and the owner + // goes away with the loop: see it through. Its packet is then + // queued before the loop's teardown drain looks for it. + while (*this).state.load(Ordering::Acquire) == RUNNING + && !(*this).posted.load(Ordering::Acquire) + { + std::thread::yield_now(); + } + Link::remove(link); + } + } + + /// # 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 inner = unsafe { bun_core::heap::take(this) }; + if inner.close_fd { + crate::closer::Closer::close(inner.fd, ()); + } + } + + /// # Safety + /// `this` is live with a request in `QUEUED` (or `CANCELED`) state. + unsafe fn schedule(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + (*this).posted.store(false, Ordering::Release); + super::op_submitted((*this).loop_); + (*(*this).loop_).add_active(1); + WorkPool::schedule(&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; + // it stays allocated until the packet posted below is dequeued, and the + // loop thread touches only `state`, `reader` and `owner_gone` in the + // meantime. + unsafe { + let this = Inner::from_task_ptr(task); + let started = (*this) + .state + .compare_exchange(QUEUED, RUNNING, Ordering::AcqRel, Ordering::Acquire) + .is_ok(); + let fd = (*this).fd; + (*this).result = if !started { + Err(sys::Error::from_code(E::ECANCELED, (*this).request.tag()).with_fd(fd)) + } else { + 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) => 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, len, .. } => { + let data = core::slice::from_raw_parts(*data, *len); + 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), + } + } + } + }; + // Before the post: the loop thread may free `this` as soon as the + // packet is queued. + (*this).posted.store(true, Ordering::Release); + (*this).port.post(&raw mut (*this).op); + } + } + + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op, _entry: *mut OverlappedEntry) { + 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 { + super::op_dequeued(loop_); + (*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.call)(reader.f, reader.ctx, event); + } + } + Request::Write { callback, .. } => callback.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..346092dcff30 --- /dev/null +++ b/src/io/windows/ipc_frame.rs @@ -0,0 +1,225 @@ +//! 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; + +pub const FLAG_HAS_DATA: u32 = 0x01; +pub const FLAG_HAS_SOCKET_TRANSFER: u32 = 0x02; +pub 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. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +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], + is_connection: bool, + }, +} + +/// 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, + transfer_is_connection: bool, + 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, + transfer_is_connection: false, + 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, + is_connection: self.transfer_is_connection, + }); + } + 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.transfer_is_connection = flags & FLAG_TRANSFER_IS_CONNECTION != 0; + } + self.payload_remaining = data_length as usize; + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn collect(decoder: &mut Decoder, input: &[u8]) -> Result<(Vec, usize), InvalidFrame> { + let mut data = Vec::new(); + let mut sockets = 0; + decoder.feed(input, |event| match event { + Event::Data(bytes) => data.extend_from_slice(bytes), + Event::SocketTransfer { .. } => sockets += 1, + })?; + Ok((data, sockets)) + } + + #[test] + fn round_trips_data_split_anywhere() { + let mut wire = Vec::new(); + wire.extend_from_slice(&data_header(5)); + wire.extend_from_slice(b"hello"); + wire.extend_from_slice(&data_header(0)); + wire.extend_from_slice(&data_header(6)); + wire.extend_from_slice(b" world"); + for split in 0..wire.len() { + let mut decoder = Decoder::new(); + let (mut data, _) = collect(&mut decoder, &wire[..split]).unwrap(); + data.extend(collect(&mut decoder, &wire[split..]).unwrap().0); + assert_eq!(data, b"hello world"); + } + } + + #[test] + fn socket_transfer_precedes_its_data() { + let mut wire = Vec::new(); + let mut header = data_header(2); + header[0] = (FLAG_HAS_DATA | FLAG_HAS_SOCKET_TRANSFER | FLAG_TRANSFER_IS_CONNECTION) as u8; + wire.extend_from_slice(&header); + wire.extend_from_slice(&[7u8; SOCKET_TRANSFER_LEN]); + wire.extend_from_slice(b"ok"); + let mut decoder = Decoder::new(); + let mut order = Vec::new(); + decoder + .feed(&wire, |event| match event { + Event::Data(bytes) => order.push(bytes.len()), + Event::SocketTransfer { + info, + is_connection, + } => { + assert!(is_connection); + assert_eq!(info[0], 7); + order.push(usize::MAX); + } + }) + .unwrap(); + assert_eq!(order, [usize::MAX, 2]); + } + + #[test] + fn rejects_bad_headers() { + let mut unknown_flag = data_header(1); + unknown_flag[0] = 0x08; + let mut reserved = data_header(1); + reserved[12] = 1; + let mut connection_without_transfer = data_header(1); + connection_without_transfer[0] = (FLAG_HAS_DATA | FLAG_TRANSFER_IS_CONNECTION) as u8; + let mut length_without_data = data_header(1); + length_without_data[0] = 0; + for header in [ + unknown_flag, + reserved, + connection_without_transfer, + length_without_data, + ] { + assert_eq!(collect(&mut Decoder::new(), &header), Err(InvalidFrame),); + } + } +} diff --git a/src/io/windows/mod.rs b/src/io/windows/mod.rs new file mode 100644 index 000000000000..16b57b5adeaa --- /dev/null +++ b/src/io/windows/mod.rs @@ -0,0 +1,310 @@ +//! 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, ReadEvent}; +pub use pipe_server::PipeServer; +pub use tty::Tty; + +// ────────────────────────────────────────────────────────────────────────── +// Loop bookkeeping +// ────────────────────────────────────────────────────────────────────────── + +/// A packet that leads to `op` is now owed to `loop_`: the loop must keep +/// turning (`num_polls`) and must not close its port (`us_iocp_op_submitted`) +/// until it is dequeued. +/// +/// # Safety +/// `loop_` is the live loop of the calling thread. +#[inline] +pub(crate) unsafe fn op_submitted(loop_: *mut Loop) { + // SAFETY: caller contract. + unsafe { + (*loop_).inc(); + iocp::us_iocp_op_submitted(loop_); + } +} + +/// As [`op_submitted`] for a wait started with `us_iocp_wait_start`, which +/// does the port accounting itself. +/// +/// # Safety +/// `loop_` is the live loop of the calling thread. +#[inline] +pub(crate) unsafe fn wait_submitted(loop_: *mut Loop) { + // SAFETY: caller contract. + unsafe { (*loop_).inc() }; +} + +/// The packet accounted by [`op_submitted`] / [`wait_submitted`] was dequeued +/// (or its wait was removed before firing). +/// +/// # Safety +/// `loop_` is the live loop of the calling thread. +#[inline] +pub(crate) unsafe fn op_dequeued(loop_: *mut Loop) { + // SAFETY: caller contract. + unsafe { (*loop_).dec() }; +} + +/// Queue `op` on the loop's own thread so its `complete` runs from the loop +/// rather than re-entrantly. Returns `false` if the port refused the packet. +/// +/// # Safety +/// `loop_` is the live loop of the calling thread; `op` stays allocated until +/// its `complete` has run. +pub(crate) unsafe fn post_to_loop(loop_: *mut Loop, op: *mut iocp::Op) -> bool { + // SAFETY: caller contract; `Op` starts with the OVERLAPPED the packet carries. + unsafe { + if sys::PostQueuedCompletionStatus(iocp::us_loop_iocp(loop_), 0, 0, op.cast()) == 0 { + return false; + } + // Only this thread dequeues, so the packet is still queued. + op_submitted(loop_); + } + true +} + +/// 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. + /// + /// # 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 { + // The loop thread would wait for this completion forever. + panic!("PostQueuedCompletionStatus failed: {:?}", sys::last_error()); + } + } +} + +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()); + } + let mut dup: sys::HANDLE = core::ptr::null_mut(); + // SAFETY: caller contract; both process handles are the pseudo-handle. + let ok = unsafe { + sys::DuplicateHandle( + sys::GetCurrentProcess(), + iocp::us_loop_iocp(loop_), + sys::GetCurrentProcess(), + &raw mut dup, + 0, + 0, + sys::DUPLICATE_SAME_ACCESS, + ) + }; + if ok == 0 { + return None; + } + 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 and console still open on `loop_`. Call on +/// the loop's thread before freeing the loop; the cancelled operations are +/// collected by the loop's own teardown drain. +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; + } + } + PORTS.with_borrow_mut(|ports| ports.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) } + } +} + +/// UTF-8 → NUL-terminated UTF-16 for a Win32 `W` call. +pub(crate) fn to_wide_z(bytes: &[u8]) -> Vec { + match bun_core::handle_oom(bun_core::strings::to_utf16_alloc(bytes, false, true)) { + Some(wide) => wide, + // All ASCII: nothing was converted. + None => { + let mut wide: Vec = Vec::with_capacity(bytes.len() + 1); + wide.extend(bytes.iter().map(|b| u16::from(*b))); + wide.push(0); + wide + } + } +} + +/// 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..6e55a17641e2 --- /dev/null +++ b/src/io/windows/pipe.rs @@ -0,0 +1,2312 @@ +//! 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 somebody else created. Reads block on +//! the pipe's own reader thread ([`SyncReader`]) and writes on a helper +//! thread; each posts its result to the loop. +//! +//! A pending read is never cancelled except by closing: cancelling a buffered +//! pipe read can make the peer's `WriteFile` report success for bytes nobody +//! received. Pausing lets the read complete and holds what it produced. + +use core::ffi::c_void; +use core::ptr::{self, NonNull}; +use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, AtomicU32, Ordering}; +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, OverlappedEntry, Wait}; + +use super::sys as win; +use super::sys::{HANDLE, INVALID_HANDLE_VALUE, Win32Error}; +use super::{Callback, Link, Port}; + +bun_core::declare_scope!(WinPipe, hidden); + +/// Bytes asked of the kernel per read unless the owner says otherwise. +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; + +/// How long opening somebody else's pipe waits for the helper thread that asks +/// the kernel whether the HANDLE is synchronous. The query only stalls behind +/// another process's blocking I/O on the same file object, which a +/// synchronous file object alone allows, so running out of time means +/// "synchronous". +const MODE_PROBE_DEADLINE_MS: u32 = 100; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Mode { + Owned, + Event, + Sync, +} + +/// What a read produced. +pub enum ReadEvent<'a> { + /// Bytes from the pipe. 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, + /// Reading has stopped. + Err(sys::Error), +} + +type ReadCallback = unsafe fn(*const (), *mut c_void, ReadEvent<'_>); + +#[derive(Clone, Copy)] +struct Reader { + ctx: *mut c_void, + f: *const (), + call: ReadCallback, +} + +/// 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; + } +} + +#[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>, + + reader: Option, + read_op: *mut ReadOp, + read_size: usize, + /// `Sync` mode: data may leave the pipe before the owner has seen the chunk + /// in front of it. + read_ahead: bool, + sync_reader: Option, + + /// Accepted, not yet handed to the kernel (`Event` and `Sync` run 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, + write_lane: Lane, + /// Buffers of writes that outlived the owner who lent them. + adopted: Vec>, + 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(()) + } + + /// # 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, + Err(Win32Error), +} + +#[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, + /// A failure that produced no packet of its own, carried by a posted one. + posted_error: Option, + lane: Lane, + // `Sync` mode; the reader thread's from `request` until it posts the op: + max_len: u32, + read_ahead: bool, + sync_bytes: u32, + sync_error: u32, + /// `read_stop` took the wait out of the kernel: an aborted completion is + /// not an error. + stopped: AtomicBool, + /// `Event` mode: the operation out is the zero-byte read that waits for + /// data, not a read that takes it. + zero_wait: bool, +} + +type WriteResult = sys::Result; + +#[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). + owned: Vec, + callback: Option>, + posted_error: Option, + // `Sync` mode: + handle: HANDLE, + port: Option>, + sync_error: u32, +} + +// ────────────────────────────────────────────────────────────────────────── +// Opening +// ────────────────────────────────────────────────────────────────────────── + +impl Pipe { + /// Take over an overlapped pipe end that Bun created (a spawned child's + /// stdio, a pseudoconsole pipe, a connected client). The HANDLE is + /// associated with the loop's completion port, and closed with the pipe + /// when `close_fd` is set. On `Err` the caller still owns `fd`. + pub fn open_owned(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::create(loop_, handle, Some(Fd::from_system(handle)), Mode::Owned) + } + + /// Take over a pipe end somebody else created. Standard handles are + /// duplicated and the original is left alone; any other `fd` is closed + /// with the pipe when `close_fd` is set. On `Err` the caller still owns + /// `fd`. + pub fn open_foreign(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 mut dup: HANDLE = ptr::null_mut(); + // SAFETY: both process handles are the pseudo-handle; `original` + // is the caller's open HANDLE. + let ok = unsafe { + win::DuplicateHandle( + win::GetCurrentProcess(), + original, + win::GetCurrentProcess(), + &raw mut dup, + 0, + 0, + win::DUPLICATE_SAME_ACCESS, + ) + }; + if ok == 0 { + return Err(sys::Error::from_win32(win::last_error(), Tag::dup).with_fd(fd)); + } + (dup, Some(Fd::from_system(dup))) + } else { + (original, close_fd.then_some(fd)) + }; + let duplicated = handle != original; + + // A standard handle is the same file object for the life of the + // process, so what the probe found (and the pipe mode it set) holds + // 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 KNOWN_EVENT: u8 = 1; + const KNOWN_SYNC: u8 = 2; + let known = fd.stdio_tag().map(|tag| &STD_MODES[tag as usize]); + let mode = match known.map(|slot| slot.load(Ordering::Acquire)) { + Some(KNOWN_EVENT) => Mode::Event, + Some(KNOWN_SYNC) => Mode::Sync, + _ => match probe_mode(handle) { + Ok(mode) => { + if let Some(slot) = known { + let value = if mode == Mode::Sync { + KNOWN_SYNC + } else { + KNOWN_EVENT + }; + slot.store(value, Ordering::Release); + } + mode + } + Err(errno) => { + if handle != original { + // SAFETY: `handle` is the duplicate made above. + unsafe { win::CloseHandle(handle) }; + } + return Err(sys::Error::from_code(errno, Tag::open).with_fd(fd)); + } + }, + }; + let pipe = Self::create(loop_, handle, close_with, mode); + if duplicated { + // SAFETY: `inner` is live while `pipe` is. + unsafe { (*pipe.raw()).flags.insert(Flags::DUPLICATED) }; + } + Ok(pipe) + } + + 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, + reader: None, + read_op: ptr::null_mut(), + read_size: DEFAULT_READ_SIZE, + read_ahead: true, + sync_reader: 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(), + write_lane: Lane::NONE, + adopted: Vec::new(), + 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() + } +} + +/// Ask the kernel, off-thread and with a deadline, whether `handle` is a +/// synchronous file object, and put it in byte-read blocking mode on the way. +fn probe_mode(handle: HANDLE) -> Result { + struct Probe { + /// The probe's own duplicate: an abandoned probe finishes whenever the + /// other process's read does, long after the caller's HANDLE value may + /// have been closed and reused. + handle: HANDLE, + done: HANDLE, + /// 0 until the thread runs, `STARTED` while it asks, then `RESULT_*`. + result: AtomicU32, + } + // SAFETY: the raw handles are only used for thread-safe Win32 calls. + unsafe impl Send for Probe {} + // SAFETY: as above; the one shared field is atomic. + unsafe impl Sync for Probe {} + impl Drop for Probe { + fn drop(&mut self) { + // SAFETY: both were created by `probe_mode` and are closed once. + unsafe { + win::CloseHandle(self.done); + win::CloseHandle(self.handle); + } + } + } + const RESULT_OVERLAPPED: u32 = 1; + const RESULT_SYNC: u32 = 2; + const RESULT_NOT_A_PIPE: u32 = 3; + const RESULT_NOWAIT: u32 = 4; + const RESULT_FAILED: u32 = 5; + const STARTED: u32 = 6; + + unsafe extern "system" fn run(context: *mut c_void) -> u32 { + // SAFETY: `context` is the `Arc` leaked for this thread below. + let probe = unsafe { Arc::from_raw(context.cast::().cast_const()) }; + probe.result.store(STARTED, Ordering::Release); + let mut mode_info: u32 = 0; + let mut iosb: win::IO_STATUS_BLOCK = bun_core::ffi::zeroed(); + // SAFETY: out-params are live locals sized for FileModeInformation. + let status = unsafe { + win::NtQueryInformationFile( + probe.handle, + &raw mut iosb, + (&raw mut mode_info).cast(), + size_of::() as u32, + win::FILE_INFORMATION_CLASS(win::FILE_MODE_INFORMATION), + ) + }; + let mut result = if status != win::NTSTATUS::SUCCESS { + RESULT_FAILED + } else if mode_info & (win::FILE_SYNCHRONOUS_IO_ALERT | win::FILE_SYNCHRONOUS_IO_NONALERT) + != 0 + { + RESULT_SYNC + } else { + RESULT_OVERLAPPED + }; + + if result != RESULT_FAILED { + 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( + probe.handle, + &raw mut pipe_mode, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + if ok == 0 { + let err = win::last_error(); + if err == Win32Error::ACCESS_DENIED { + // The handle lacks FILE_WRITE_ATTRIBUTES; that is fine as + // long as the pipe already blocks. + let mut state: u32 = 0; + // SAFETY: `state` is a live local; the rest is optional. + let ok = unsafe { + win::GetNamedPipeHandleStateW( + probe.handle, + &raw mut state, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + 0, + ) + }; + if ok == 0 { + result = RESULT_FAILED; + } else if state & win::PIPE_NOWAIT != 0 { + result = RESULT_NOWAIT; + } + } else if err == Win32Error::INVALID_PARAMETER { + // FILE_TYPE_PIPE, yet not a pipe: a socket. + result = RESULT_NOT_A_PIPE; + } else { + result = RESULT_FAILED; + } + } + } + probe.result.store(result, Ordering::Release); + // SAFETY: `done` stays open until the last `Arc` drops. + unsafe { win::SetEvent(probe.done) }; + 0 + } + + let mut duplicate: HANDLE = ptr::null_mut(); + // SAFETY: plain Win32 call; `duplicate` is a live local. + let duplicated = unsafe { + win::DuplicateHandle( + win::GetCurrentProcess(), + handle, + win::GetCurrentProcess(), + &raw mut duplicate, + 0, + 0, + win::DUPLICATE_SAME_ACCESS, + ) + }; + if duplicated == 0 { + return Err(E::EBADF); + } + // SAFETY: plain Win32 call. + let done = unsafe { win::CreateEventW(ptr::null_mut(), 1, 0, ptr::null()) }; + if done.is_null() { + // SAFETY: `duplicate` was just created and is not shared yet. + unsafe { win::CloseHandle(duplicate) }; + return Err(E::ENOMEM); + } + let probe = Arc::new(Probe { + handle: duplicate, + done, + result: AtomicU32::new(0), + }); + let for_thread = Arc::into_raw(probe.clone()); + // SAFETY: `run` takes over the leaked `Arc`; everything in `Probe` is + // usable from another thread. + if !unsafe { super::queue_blocking_work(run, for_thread.cast_mut().cast()) } { + // SAFETY: the thread never started, so the leaked `Arc` is still ours. + drop(unsafe { Arc::from_raw(for_thread) }); + return Err(E::ENOMEM); + } + loop { + bun_sys::windows::kernel32::WaitForSingleObject(done, MODE_PROBE_DEADLINE_MS); + return match probe.result.load(Ordering::Acquire) { + // The thread pool has not run it yet: the handle is not what is slow. + 0 => continue, + RESULT_OVERLAPPED => Ok(Mode::Event), + RESULT_NOT_A_PIPE => Err(E::ENOTSOCK), + RESULT_NOWAIT => Err(E::EACCES), + RESULT_FAILED => Err(E::EBADF), + _ => Ok(Mode::Sync), + }; + } +} + +// ────────────────────────────────────────────────────────────────────────── +// 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) + } + + /// Closed by [`close_all_for_loop`](super::close_all_for_loop): every + /// operation fails with `EBADF` from here on. + pub fn is_closed(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { (*self.raw()).flags.contains(Flags::DETACHED) } + } + + /// `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 next reads ask of the kernel (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) }; + } + + /// Whether a synchronous HANDLE's reader thread may take the next chunk + /// from the pipe while the owner is still handling the one before it (the + /// default). An owner that must not take a byte past some count turns it + /// off, since its next read size depends on what this one produced. + pub fn set_read_ahead(&mut self, allowed: bool) { + // SAFETY: `inner` is live while the owner's `Pipe` is. + unsafe { (*self.raw()).read_ahead = allowed }; + } + + /// 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<()> { + unsafe fn call(f: *const (), ctx: *mut c_void, event: ReadEvent<'_>) { + // SAFETY: `f` was erased from exactly this type below. + let f = + unsafe { core::mem::transmute::<*const (), unsafe fn(*mut T, ReadEvent<'_>)>(f) }; + // SAFETY: the owner keeps `ctx` valid while reading (see `read_start`). + unsafe { f(ctx.cast::(), event) } + } + 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(Reader { + ctx: ctx.cast(), + f: on_read as *const (), + call: call::, + }); + 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. On a HANDLE Bun created, a read already handed to the + /// kernel is left to finish and what it produces is kept for the next + /// `read_start`. Any other HANDLE (inherited stdin) may be read by another + /// process next, so what is out on it is a zero-byte wait, and that is + /// taken back: nothing has left the pipe at that point, and cancelling a + /// zero-byte read costs the peer's write nothing. A chunk that a + /// synchronous HANDLE's reader thread took before this call (it works one + /// ahead, see [`SyncReader`]) 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; + let in_flight = !read.is_null() && (*read).state == ReadState::InFlight; + match (*this).mode { + Mode::Owned => {} + Mode::Sync => { + if in_flight { + (*read).stopped.store(true, Ordering::Release); + } + // Also with nothing in flight: the thread may be waiting + // for the chunk after the one being delivered. + if let Some(reader) = &(*this).sync_reader { + reader.disarm(); + } + } + Mode::Event => { + if in_flight && (*read).zero_wait { + (*read).stopped.store(true, Ordering::Release); + win::CancelIoEx((*this).handle, (&raw mut (*read).op).cast()); + } + } + } + Inner::update_keep_alive(this); + } + } + + /// 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. + /// + /// # 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)). + pub unsafe fn write( + &mut self, + data: &[u8], + 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(), + Some(Callback::new(ctx, on_write)), + ) + } + } + + /// As [`write`](Self::write), for bytes the operation should own: nothing + /// needs to outlive the call, and `on_write` is optional. + pub fn write_owned( + &mut self, + data: Vec, + ctx: *mut T, + on_write: Option, + ) -> sys::Result<()> { + // SAFETY: the operation owns `data`, whose heap buffer does not move. + unsafe { + Inner::write( + self.raw(), + data.as_ptr(), + data.len(), + data, + on_write.map(|f| Callback::new(ctx, f)), + ) + } + } + + /// Write all of `data` before returning, blocking the calling thread for + /// as long as the reader takes. Queues behind writes already in flight. + 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) } + } + + /// 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. + unsafe { Inner::adopt_write_buffer(self.raw(), 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 { + sys::Error::from_win32(err, Tag::read) +} + +fn write_error(err: Win32Error) -> sys::Error { + // A pipe whose reader is gone reports one of three codes depending on how + // it went; all of them are EPIPE to a writer. + if err == Win32Error::BROKEN_PIPE + || err == Win32Error::NO_DATA + || err == Win32Error::PIPE_NOT_CONNECTED + { + 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::PIPE_NOT_CONNECTED + || err == Win32Error::NO_DATA + || err == Win32Error::HANDLE_EOF +} + +impl Inner { + fn has_writes(&self) -> bool { + self.writes_in_flight > 0 || !self.write_head.is_null() + } + + #[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(), + posted_error: None, + lane: Lane::NONE, + max_len: 0, + read_ahead: false, + sync_bytes: 0, + sync_error: 0, + stopped: AtomicBool::new(false), + zero_wait: false, + })); + } + let op = (*this).read_op; + match (*op).state { + ReadState::InFlight | ReadState::Replaying | ReadState::Delivering => Ok(()), + ReadState::Held => { + // Back through the port so the owner hears of it from the + // loop, not from inside `read_start`. + if !super::post_to_loop((*this).link.loop_, &raw mut (*op).op) { + return Err(win::last_error()); + } + (*this).pending += 1; + (*op).state = ReadState::Replaying; + Ok(()) + } + ReadState::Idle => Self::submit_read(this, op), + } + } + } + + /// # Safety + /// `this` and its idle `op` are live; `this` is not closing. + /// One overlapped `ReadFile` of `len` bytes into `op.buf` on an `Event` + /// HANDLE. `Ok(true)`: it is pending and its completion arrives as a + /// packet. `Ok(false)`: it is over already; `posted_error` or the + /// OVERLAPPED says how. + /// + /// # Safety + /// `this` is live in `Event` mode, `op` is its read with nothing out, and + /// `op.buf` 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_error = None; + (*op).op.overlapped.internal = 0; + (*op).op.overlapped.internal_high = 0; + // Low bit set: the completion queues no packet on whatever port + // the file object may be associated with. + (*op).op.overlapped.event = ((*op).lane.event as usize | 1) as HANDLE; + let ok = win::ReadFile( + (*this).handle, + (*op).buf.as_mut_ptr(), + len, + ptr::null_mut(), + (&raw mut (*op).op).cast(), + ); + if ok == 0 && win::last_error() == win::IO_PENDING { + if iocp::us_iocp_wait_start((*op).lane.wait, (*op).lane.event, &raw mut (*op).op) + != 0 + { + // Nothing will announce the completion, so collect it here. + win::CancelIoEx((*this).handle, (&raw mut (*op).op).cast()); + bun_sys::windows::kernel32::WaitForSingleObject( + (*op).lane.event, + bun_sys::windows::INFINITE, + ); + return Ok(false); + } + super::wait_submitted(loop_); + return Ok(true); + } + if ok == 0 { + (*op).posted_error = Some(win::last_error()); + } + Ok(false) + } + } + + /// The zero-byte wait of an `Event` HANDLE completed: read what is there. + /// Returns as [`event_read`](Self::event_read). + /// + /// # Safety + /// As `event_read`. + unsafe fn event_fetch(this: *mut Inner, op: *mut ReadOp) -> Result { + // SAFETY: caller contract. + unsafe { + let mut available: u32 = 0; + if win::PeekNamedPipe( + (*this).handle, + ptr::null_mut(), + 0, + ptr::null_mut(), + &raw mut available, + ptr::null_mut(), + ) == 0 + { + (*op).posted_error = Some(win::last_error()); + return Ok(false); + } + if available == 0 { + // Another reader of the pipe got there first: wait again. + (*op).zero_wait = true; + return Self::event_read(this, op, 0); + } + let len = (available as usize).min((*op).buf.capacity()) as u32; + Self::event_read(this, op, len) + } + } + + 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_; + let want = (*this).read_size; + (*op).buf.clear(); + if (*op).buf.capacity() < want { + (*op).buf.reserve_exact(want); + } + let len = want.min((*op).buf.capacity()) as u32; + (*op).outcome = Outcome::None; + (*op).posted_error = None; + (*op).op.overlapped.internal = 0; + (*op).op.overlapped.internal_high = 0; + (*op).op.overlapped.offset = 0; + (*op).op.overlapped.offset_high = 0; + + match (*this).mode { + Mode::Owned => { + (*op).op.overlapped.event = ptr::null_mut(); + let ok = win::ReadFile( + (*this).handle, + (*op).buf.as_mut_ptr(), + len, + ptr::null_mut(), + (&raw mut (*op).op).cast(), + ); + if ok != 0 || win::last_error() == win::IO_PENDING { + // A read that finished at once still queues its packet. + super::op_submitted(loop_); + } else { + (*op).posted_error = Some(win::last_error()); + if !super::post_to_loop(loop_, &raw mut (*op).op) { + return Err(win::last_error()); + } + } + } + Mode::Event => { + // Wait with a zero-byte read and take the data when it + // completes: see `read_stop`. + (*op).zero_wait = true; + if !Self::event_read(this, op, 0)? + && !super::post_to_loop(loop_, &raw mut (*op).op) + { + return Err(win::last_error()); + } + } + Mode::Sync => { + if (*this).sync_reader.is_none() { + let Some(port) = Self::port(this) else { + return Err(win::last_error()); + }; + (*this).sync_reader = Some(SyncReader::start((*this).handle, port)?); + } + (*op).max_len = len; + (*op).read_ahead = (*this).read_ahead; + (*op).sync_bytes = 0; + (*op).sync_error = 0; + if let Some(reader) = &(*this).sync_reader { + reader.request(op); + } + super::op_submitted(loop_); + } + } + (*this).pending += 1; + (*op).state = ReadState::InFlight; + Ok(()) + } + } + + // ── writing ────────────────────────────────────────────────────────── + + /// # 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, + callback: Option>, + ) -> sys::Result<()> { + // SAFETY: caller contract. + unsafe { + if (*this).gone() { + return Err(sys::Error::from_code(E::EBADF, Tag::write)); + } + // A helper thread cannot be stopped mid-write, so what it writes + // from must not depend on the owner staying around. + let (data, owned) = if (*this).mode == Mode::Sync && owned.is_empty() && len > 0 { + let copy = core::slice::from_raw_parts(data, len).to_vec(); + (copy.as_ptr(), copy) + } else { + (data, owned) + }; + let op = bun_core::heap::into_raw(Box::new(WriteOp { + op: Op::new(WriteOp::complete), + pipe: this, + next: ptr::null_mut(), + data, + len, + done: 0, + chunk: 0, + owned, + callback, + posted_error: None, + handle: (*this).handle, + port: None, + sync_error: 0, + })); + if (*this).write_tail.is_null() { + (*this).write_head = op; + } else { + (*(*this).write_tail).next = op; + } + (*this).write_tail = op; + Self::update_keep_alive(this); + Self::pump_writes(this); + 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. + /// + /// # Safety + /// `this` is live and not closing. + unsafe fn pump_writes(this: *mut Inner) { + // 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) + { + return; + } + (*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; + } + Self::submit_write(this, op); + } + } + } + + /// Start (or continue) `op`. A failure to start travels through the port + /// like any other completion. + /// + /// # Safety + /// `this` and `op` are live; `op` is counted in `writes_in_flight` and + /// `pending`. + unsafe fn submit_write(this: *mut Inner, op: *mut WriteOp) { + // 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_error = None; + (*op).op.overlapped.internal = 0; + (*op).op.overlapped.internal_high = 0; + (*op).op.overlapped.offset = 0; + (*op).op.overlapped.offset_high = 0; + let data = (*op).data.add((*op).done); + + let failed: Option = match (*this).mode { + Mode::Owned => { + (*op).op.overlapped.event = ptr::null_mut(); + let ok = win::WriteFile( + (*this).handle, + data, + chunk, + ptr::null_mut(), + (&raw mut (*op).op).cast(), + ); + if ok != 0 || win::last_error() == win::IO_PENDING { + super::op_submitted(loop_); + return; + } + Some(win::last_error()) + } + Mode::Event => match (*this).write_lane.ensure(loop_) { + Err(err) => Some(err), + Ok(()) => { + let event = (*this).write_lane.event; + (*op).op.overlapped.event = (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() == win::IO_PENDING { + if iocp::us_iocp_wait_start( + (*this).write_lane.wait, + event, + &raw mut (*op).op, + ) == 0 + { + super::wait_submitted(loop_); + (*this).event_write = op; + return; + } + // Nothing will announce the completion: wait for + // it here (the reader decides how long that is). + bun_sys::windows::kernel32::WaitForSingleObject( + event, + bun_sys::windows::INFINITE, + ); + None + } else if ok == 0 { + Some(win::last_error()) + } else { + None + } + } + }, + Mode::Sync => match Self::port(this) { + None => Some(win::last_error()), + Some(port) => { + (*op).port = Some(port); + (*op).handle = (*this).handle; + if super::queue_blocking_work(WriteOp::sync_write_thread, op.cast()) { + super::op_submitted(loop_); + return; + } + let err = win::last_error(); + (*op).port = None; + Some(err) + } + }, + }; + (*op).posted_error = failed; + if !super::post_to_loop(loop_, &raw mut (*op).op) { + // The port is unusable; the operation can only be abandoned. + (*op).posted_error = Some(Win32Error::OPERATION_ABORTED); + WriteOp::finish(op); + } + } + } + + /// # 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)); + } + let mut written = 0usize; + while written < data.len() { + let chunk = (data.len() - written).min(MAX_WRITE_CHUNK) as u32; + let mut n: u32 = 0; + if (*this).mode == Mode::Sync { + if win::WriteFile( + (*this).handle, + data.as_ptr().add(written), + chunk, + &raw mut n, + ptr::null_mut(), + ) == 0 + { + return Err(write_error(win::last_error())); + } + } else { + if (*this).blocking_event.is_null() { + let event = win::CreateEventW(ptr::null_mut(), 1, 0, ptr::null()); + if event.is_null() { + return Err(write_error(win::last_error())); + } + (*this).blocking_event = event; + } + let mut overlapped: win::OVERLAPPED = bun_core::ffi::zeroed(); + // Low bit set: no packet for this one, the event is all. + overlapped.hEvent = ((*this).blocking_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() != win::IO_PENDING { + return Err(write_error(win::last_error())); + } + bun_sys::windows::kernel32::WaitForSingleObject( + (*this).blocking_event, + bun_sys::windows::INFINITE, + ); + } + let status = win::status_to_win32(overlapped.Internal as i32); + if status != Win32Error::SUCCESS { + return Err(write_error(status)); + } + n = overlapped.InternalHigh as u32; + } + if n == 0 { + break; + } + written += n as usize; + } + Ok(written) + } + } + + /// # Safety + /// `this` is live. + unsafe fn adopt_write_buffer(this: *mut Inner, buffer: Vec) { + // SAFETY: caller contract. The pipe is freed only after every + // operation has been collected, which is as long as any of them can + // look at `buffer`. + unsafe { (*this).adopted.push(buffer) }; + } + + // ── 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() { + reader.stop(); + } + 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()); + } + Mode::Sync => {} + }, + ReadState::Idle | ReadState::Held => { + (*this).read_op = ptr::null_mut(); + ReadOp::destroy(read); + } + ReadState::Replaying | ReadState::Delivering => {} + } + } + + 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()); + } + // Queued writes never reached the kernel; they complete as + // cancelled, in order, through the port. + 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_error = Some(Win32Error::OPERATION_ABORTED); + (*this).writes_in_flight += 1; + (*this).pending += 1; + if !super::post_to_loop(loop_, &raw mut (*op).op) { + WriteOp::finish(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()); + (*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, _entry: *mut OverlappedEntry) { + 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 { + super::op_dequeued(loop_); + let pipe = (*this).pipe; + (*pipe).pending -= 1; + if (*pipe).gone() { + (*pipe).read_op = ptr::null_mut(); + Self::destroy(this); + Inner::maybe_finish(pipe); + return; + } + + if (*this).state == ReadState::InFlight { + if core::mem::take(&mut (*this).zero_wait) + && (*this).posted_error.is_none() + && win::status_to_win32((*this).op.status()) == Win32Error::SUCCESS + { + if !(*pipe).flags.contains(Flags::READING) { + // Stopped since: what arrived stays in the pipe. + (*this).stopped.store(false, Ordering::Release); + (*this).state = ReadState::Idle; + return; + } + match Inner::event_fetch(pipe, this) { + Ok(true) => { + (*pipe).pending += 1; + return; + } + Ok(false) => {} + Err(err) => (*this).posted_error = Some(err), + } + } + (*this).outcome = Self::outcome(this, (*pipe).mode); + if (*this).stopped.swap(false, Ordering::AcqRel) + && matches!(&(*this).outcome, Outcome::Err(err) if *err == Win32Error::OPERATION_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; + } + let Some(reader) = (*pipe).reader else { + (*this).state = ReadState::Held; + return; + }; + + (*this).state = ReadState::Delivering; + (*pipe).pins += 1; + match core::mem::replace(&mut (*this).outcome, Outcome::None) { + Outcome::None => {} + Outcome::Data if (*this).buf.is_empty() => {} + Outcome::Data => { + (reader.call)(reader.f, reader.ctx, ReadEvent::Data(&mut (*this).buf)); + } + Outcome::Eof => { + (*pipe).flags.remove(Flags::READING); + (*pipe).flags.insert(Flags::READ_ENDED); + (reader.call)(reader.f, reader.ctx, ReadEvent::Eof); + } + Outcome::Err(err) => { + (*pipe).flags.remove(Flags::READING); + (*pipe).flags.insert(Flags::READ_ENDED); + (reader.call)(reader.f, reader.ctx, ReadEvent::Err(read_error(err))); + } + } + (*pipe).pins -= 1; + (*this).state = ReadState::Idle; + + if (*pipe).gone() { + (*pipe).read_op = ptr::null_mut(); + Self::destroy(this); + Inner::maybe_finish(pipe); + return; + } + Inner::update_keep_alive(pipe); + if (*pipe).flags.contains(Flags::READING) + && let Err(err) = Inner::pump_read(pipe) + { + (*this).outcome = Outcome::Err(err); + (*this).state = ReadState::Held; + let _ = Inner::pump_read(pipe); + } + } + } + + /// # Safety + /// `this` just completed; its buffer holds what the kernel wrote. + unsafe fn outcome(this: *mut ReadOp, mode: Mode) -> Outcome { + // SAFETY: caller contract. The kernel initialized the first `bytes` + // bytes of the buffer's capacity. + unsafe { + let (err, bytes) = if let Some(err) = (*this).posted_error { + (err, 0) + } else if mode == Mode::Sync { + ( + Win32Error::from_u32((*this).sync_error), + (*this).sync_bytes as usize, + ) + } else { + ( + win::status_to_win32((*this).op.status()), + (*this).op.bytes_transferred(), + ) + }; + // A message-mode pipe reports the part of a message that fit as + // MORE_DATA; the bytes are as good as any. + if err == Win32Error::SUCCESS || err == win::MORE_DATA { + debug_assert!(bytes <= (*this).buf.capacity()); + (*this).buf.set_len(bytes.min((*this).buf.capacity())); + return Outcome::Data; + } + if is_eof(err) { + return Outcome::Eof; + } + Outcome::Err(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, + 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; the thread takes it and posts it exactly once. + request: AtomicPtr, +} + +// 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); + } + } +} + +/// The loop's side of the one thread that reads a `Sync` pipe for as long as +/// the pipe is open. +/// +/// The thread waits in a zero-byte read, which consumes nothing and can be +/// interrupted without the peer losing anything, then takes what +/// `PeekNamedPipe` reports. While the owner handles a chunk, the thread takes +/// the next one if it was in the pipe already when that chunk was read, and +/// holds it until the loop asks. What arrives later stays in the pipe until the +/// loop asks, so an owner that stops reading from its callback leaves it to +/// whoever reads the HANDLE next. `disarm` stops anything more being taken. +struct SyncReader { + shared: Arc, + /// For `CancelSynchronousIo`. + thread: HANDLE, +} + +impl SyncReader { + fn start(pipe: HANDLE, port: Arc) -> Result { + use std::os::windows::io::IntoRawHandle; + let mut handle: HANDLE = ptr::null_mut(); + // SAFETY: plain Win32 calls; `pipe` is the caller's open HANDLE. + let wake = unsafe { + if win::DuplicateHandle( + win::GetCurrentProcess(), + pipe, + win::GetCurrentProcess(), + &raw mut handle, + 0, + 0, + win::DUPLICATE_SAME_ACCESS, + ) == 0 + { + return Err(win::last_error()); + } + let wake = win::CreateEventW(ptr::null_mut(), 0, 0, ptr::null()); + if wake.is_null() { + let err = win::last_error(); + win::CloseHandle(handle); + return Err(err); + } + wake + }; + let shared = Arc::new(SyncShared { + handle, + port, + wake, + lock: bun_threading::Mutex::new(), + waiting: AtomicBool::new(false), + armed: AtomicBool::new(false), + shutdown: AtomicBool::new(false), + request: AtomicPtr::new(ptr::null_mut()), + }); + 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) { + debug_assert!(self.shared.request.load(Ordering::Relaxed).is_null()); + 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`. A request that + /// is outstanding comes back aborted, or with what was already read. + fn disarm(&self) { + self.shared.armed.store(false, Ordering::Release); + self.interrupt(); + } + + /// The thread answers an outstanding request (as `disarm`) and exits. + fn stop(self) { + self.shared.shutdown.store(true, Ordering::Release); + self.shared.armed.store(false, Ordering::Release); + self.interrupt(); + // SAFETY: both HANDLEs are open; `thread` is this struct's to close. + unsafe { + win::SetEvent(self.shared.wake); + win::CloseHandle(self.thread); + } + } + + /// Get the thread out of its zero-byte read. `armed` or `shutdown` was + /// changed first: the thread checks them under `lock` before it blocks. + fn interrupt(&self) { + self.shared.lock.lock(); + // The cancel only takes once the thread is inside the call, and the + // thread leaves `waiting` only through this lock, so none lands late. + while self.shared.waiting.load(Ordering::Acquire) { + // SAFETY: `thread` is this struct's open thread HANDLE. + unsafe { win::CancelSynchronousIo(self.thread) }; + win::SwitchToThread(); + } + self.shared.lock.unlock(); + } +} + +impl SyncShared { + fn run(&self) { + // What the thread has taken from the pipe and nobody asked for yet: + // the bytes in `ahead` and how the read that made them went. + let mut ahead: Vec = Vec::new(); + let mut held: Option = None; + // Bytes that may be taken before the next request; 0 for none. + let mut ahead_len: u32 = 0; + loop { + if self.shutdown.load(Ordering::Acquire) { + break; + } + if let Some(taken) = held { + let op = self.request.swap(ptr::null_mut(), Ordering::AcqRel); + if op.is_null() { + self.park(); + continue; + } + held = None; + // SAFETY: `op` is this thread's until it is posted. + unsafe { + ahead_len = if taken.error == 0 && taken.more && (*op).read_ahead { + (*op).max_len + } else { + 0 + }; + core::mem::swap(&mut (*op).buf, &mut ahead); + self.answer(op, taken.error); + } + continue; + } + + 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() { + // SAFETY: a stored request is this thread's until it is posted. + held = self.read(&mut ahead, unsafe { (*op).max_len }, true); + continue; + } + if ahead_len != 0 { + // Without waiting: what was there when the last chunk was read + // is there now, unless another reader of the pipe took it. + held = self.read(&mut ahead, ahead_len, false); + ahead_len = 0; + if held.is_some() { + continue; + } + } + self.park(); + } + self.abort_request(); + } + + 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, error: u32) { + // SAFETY: caller contract; the loop reads these once it dequeues the packet. + unsafe { + (*op).sync_error = error; + (*op).sync_bytes = (*op).buf.len() as u32; + self.port.post(&raw mut (*op).op); + } + } + + fn abort_request(&self) { + let op = self.request.swap(ptr::null_mut(), Ordering::AcqRel); + if !op.is_null() { + // SAFETY: `op` was taken from `request`. + unsafe { + (*op).buf.clear(); + self.answer(op, Win32Error::OPERATION_ABORTED.int().into()); + } + } + } + + /// Take at most `want` bytes into `into`, after waiting for them if `wait`. + /// `None` when nothing was taken and nothing went wrong: the wait was + /// interrupted, the owner stopped as the data arrived, or the pipe is empty. + fn read(&self, into: &mut Vec, want: u32, wait: bool) -> Option { + if wait { + match self.wait_for_data() { + Some(Win32Error::SUCCESS) => {} + Some(err) => { + into.clear(); + return Some(Taken::failed(err)); + } + None => return None, + } + } + if !self.armed.load(Ordering::Acquire) { + return None; + } + into.clear(); + if into.capacity() < want as usize { + into.reserve_exact(want as usize); + } + let Ok(available) = self.available() else { + return Some(Taken::failed(win::last_error())); + }; + // Nothing there: another reader of the pipe got to it first. + if available == 0 { + return wait.then_some(Taken { + error: 0, + more: false, + }); + } + let mut n: u32 = 0; + // SAFETY: `handle` is open while `self` is; `into` has room for `want` + // bytes and the kernel initializes the `n` it reports. + unsafe { + if win::ReadFile( + self.handle, + into.as_mut_ptr(), + available.min(want), + &raw mut n, + ptr::null_mut(), + ) == 0 + { + return Some(Taken::failed(win::last_error())); + } + into.set_len(n as usize); + } + // A failure here is the next read's to report. + Some(Taken { + error: 0, + more: self.available().map_or(true, |left| left > 0), + }) + } + + fn available(&self) -> Result { + let mut available: u32 = 0; + // SAFETY: `handle` is open while `self` is. + let ok = unsafe { + win::PeekNamedPipe( + self.handle, + ptr::null_mut(), + 0, + ptr::null_mut(), + &raw mut available, + ptr::null_mut(), + ) + }; + if ok == 0 { Err(()) } else { Ok(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::interrupt`: passing through the lock waits out a + // cancel that is still spinning, before any other call can take it. + self.waiting.store(false, Ordering::Release); + self.lock.lock(); + self.lock.unlock(); + + (waited != Win32Error::OPERATION_ABORTED).then_some(waited) + } +} + +/// How a read by the reader thread went. +#[derive(Clone, Copy)] +struct Taken { + /// Win32 error; 0 with the bytes in the thread's buffer. + error: u32, + /// The pipe held more right after these bytes were taken. + more: bool, +} + +impl Taken { + fn failed(err: Win32Error) -> Taken { + Taken { + error: err.int().into(), + more: false, + } + } +} + +impl WriteOp { + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op, _entry: *mut OverlappedEntry) { + 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 { + super::op_dequeued(loop_); + let pipe = (*this).pipe; + if (*this).posted_error.is_none() { + let (err, bytes) = if (*pipe).mode == Mode::Sync { + ( + Win32Error::from_u32((*this).sync_error), + (*this).chunk as usize, + ) + } else { + ( + win::status_to_win32((*this).op.status()), + (*this).op.bytes_transferred(), + ) + }; + if err != Win32Error::SUCCESS { + (*this).posted_error = Some(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); + } + } + + /// # 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).posted_error { + Some(err) => Err(write_error(err)), + None => Ok((*this).done), + }; + let callback = (*this).callback.take(); + drop(bun_core::heap::take(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).pins += 1; + callback.invoke(result); + (*pipe).pins -= 1; + } + if (*pipe).gone() { + Inner::maybe_finish(pipe); + return; + } + Inner::pump_writes(pipe); + Inner::update_keep_alive(pipe); + } + } + + unsafe extern "system" fn sync_write_thread(context: *mut c_void) -> u32 { + let op = context.cast::(); + // SAFETY: `context` is the `WriteOp` submitted by `submit_write`; it + // owns its bytes and stays allocated until the packet posted below is + // dequeued. + unsafe { + let mut written = 0u32; + let data = (*op).data.add((*op).done); + while written < (*op).chunk { + let mut n: u32 = 0; + if win::WriteFile( + (*op).handle, + data.add(written as usize), + (*op).chunk - written, + &raw mut n, + ptr::null_mut(), + ) == 0 + { + (*op).sync_error = win::last_error().int().into(); + break; + } + written += n; + } + if let Some(port) = (*op).port.take() { + 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. +pub struct ConnectRequest { + abandoned: Arc, +} + +#[repr(C)] +struct ConnectOp { + op: Op, + loop_: *mut Loop, + port: Option>, + name: Vec, + handle: HANDLE, + error: u32, + abandoned: Arc, + callback: Callback>, +} + +/// How long a connecting client waits for a busy server to offer an instance. +const CONNECT_BUSY_WAIT_MS: u32 = 30_000; + +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 mut error = 0u32; + let handle = match open_client(&name) { + Ok(handle) => handle, + Err(err) => { + error = err.int().into(); + INVALID_HANDLE_VALUE + } + }; + let busy = error == u32::from(Win32Error::PIPE_BUSY.int()); + let abandoned = Arc::new(AtomicBool::new(false)); + let op = bun_core::heap::into_raw(Box::new(ConnectOp { + op: Op::new(ConnectOp::complete), + loop_, + port: None, + name, + handle, + error, + abandoned: abandoned.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 { + let started = if busy { + match super::port_for(loop_) { + Some(port) => { + (*op).port = Some(port); + let queued = + super::queue_blocking_work(ConnectOp::wait_for_instance, op.cast()); + if queued { + super::op_submitted(loop_); + } + queued + } + None => false, + } + } else { + // The outcome is known; it is still reported from the loop. + super::post_to_loop(loop_, &raw mut (*op).op) + }; + if !started { + let err = win::last_error(); + let op = bun_core::heap::take(op); + if op.handle != INVALID_HANDLE_VALUE { + win::CloseHandle(op.handle); + } + return Err(sys::Error::from_win32(err, Tag::connect)); + } + (*loop_).add_active(1); + Ok(ConnectRequest { abandoned }) + } + } +} + +impl Drop for ConnectRequest { + fn drop(&mut self) { + self.abandoned.store(true, Ordering::Release); + } +} + +/// 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 { + return Ok(handle); + } + last = win::last_error(); + // A one-way pipe refuses the duplex open with ACCESS_DENIED. + if last != Win32Error::ACCESS_DENIED { + break; + } + } + Err(last) +} + +impl ConnectOp { + /// Every instance was busy, so wait for one and try again. + unsafe extern "system" fn wait_for_instance(context: *mut c_void) -> u32 { + let op = context.cast::(); + // SAFETY: `context` is the `ConnectOp` queued by `connect`, which + // stays allocated until the packet posted below is dequeued. The loop + // thread only touches `abandoned` meanwhile. + unsafe { + (*op).error = Win32Error::PIPE_BUSY.int().into(); + while !(*op).abandoned.load(Ordering::Acquire) { + if win::WaitNamedPipeW((*op).name.as_ptr(), CONNECT_BUSY_WAIT_MS) == 0 { + (*op).error = win::last_error().int().into(); + break; + } + match open_client(&(*op).name) { + Ok(handle) => { + (*op).handle = handle; + (*op).error = 0; + break; + } + // Another client took the instance first. + Err(err) if err == Win32Error::PIPE_BUSY => { + win::SwitchToThread(); + } + Err(err) => { + (*op).error = err.int().into(); + break; + } + } + } + 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, _entry: *mut OverlappedEntry) { + // SAFETY: `op` is the first field of the `ConnectOp` this packet was + // posted for; the packet is what kept it allocated. + unsafe { + super::op_dequeued(loop_); + (*loop_).sub_active(1); + let this = bun_core::heap::take(op.cast::()); + let connected = this.handle != INVALID_HANDLE_VALUE && this.error == 0; + if this.abandoned.load(Ordering::Acquire) { + if connected { + win::CloseHandle(this.handle); + } + return; + } + let result = if connected { + let fd = Fd::from_system(this.handle); + Pipe::open_owned(loop_, fd, true).inspect_err(|_| { + win::CloseHandle(this.handle); + }) + } else { + Err(sys::Error::from_win32( + Win32Error::from_u32(this.error), + Tag::connect, + )) + }; + this.callback.invoke(result); + } + } +} diff --git a/src/io/windows/pipe_server.rs b/src/io/windows/pipe_server.rs new file mode 100644 index 000000000000..f90648de942c --- /dev/null +++ b/src/io/windows/pipe_server.rs @@ -0,0 +1,396 @@ +//! A named-pipe server: a fixed number of instances wait for clients, and each +//! one a client takes is replaced when it is accepted. + +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, OverlappedEntry}; + +use super::sys as win; +use super::sys::{HANDLE, INVALID_HANDLE_VALUE, Win32Error}; +use super::{Callback, Link, Pipe}; + +/// Instances kept waiting for clients unless the owner asks otherwise. A burst +/// of more clients than this finds the pipe busy and waits for the next one. +pub const DEFAULT_PENDING_INSTANCES: u32 = 4; + +const PIPE_BUFFER_SIZE: u32 = 65536; + +/// The owner's handle to a listening pipe. Dropping it stops listening without +/// telling anyone. +pub struct PipeServer { + inner: NonNull, +} + +#[repr(C)] +struct Inner { + link: Link, + name: Vec, + slots: Vec<*mut AcceptOp>, + /// Connected instances nobody has accepted yet. + connected: Vec, + on_connection: Callback<()>, + refd: bool, + keeping_alive: bool, + closing: bool, + detached: bool, + owner_gone: bool, + pending: u32, + pins: u32, +} + +#[repr(C)] +struct AcceptOp { + op: Op, + server: *mut Inner, + handle: HANDLE, + posted_error: Option, + in_flight: bool, +} + +impl PipeServer { + /// Create the pipe `name` and wait for clients on `pending_instances` + /// instances. `on_connection(ctx, ())` runs from the loop each time a + /// client has connected; [`accept`](Self::accept) hands it over. + /// + /// `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], + pending_instances: u32, + ctx: *mut T, + on_connection: unsafe fn(*mut T, ()), + ) -> 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 count = pending_instances.max(1) as usize; + let inner = bun_core::heap::into_raw(Box::new(Inner { + link: Link::new(loop_, Inner::shut), + name, + slots: Vec::with_capacity(count), + connected: Vec::new(), + on_connection: Callback::new(ctx, on_connection), + refd: true, + keeping_alive: false, + closing: false, + detached: 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..count { + 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_error: None, + in_flight: false, + })); + (*inner).slots.push(slot); + Inner::arm(inner, slot); + } + Inner::update_keep_alive(inner); + Ok(PipeServer { + inner: NonNull::new_unchecked(inner), + }) + } + } + + /// Take a connected client. `None` when none is waiting. + pub fn accept(&mut self) -> Option { + let this = self.inner.as_ptr(); + // SAFETY: `inner` is live while the owner's `PipeServer` is. + unsafe { + let handle = (*this).connected.pop()?; + Some(Pipe::from_associated((*this).link.loop_, handle)) + } + } + + /// Let listening keep the loop alive (the default). + pub fn ref_(&self) { + // SAFETY: `inner` is live while the owner's `PipeServer` is. + unsafe { + (*self.inner.as_ptr()).refd = true; + Inner::update_keep_alive(self.inner.as_ptr()); + } + } + + pub fn unref(&self) { + // SAFETY: `inner` is live while the owner's `PipeServer` is. + unsafe { + (*self.inner.as_ptr()).refd = false; + Inner::update_keep_alive(self.inner.as_ptr()); + } + } +} + +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 so the pipe's access control can be changed after the fact. + 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 { + /// # Safety + /// `this` is live. Must run on the loop's thread. + unsafe fn update_keep_alive(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + if (*this).detached { + return; + } + let wanted = !(*this).closing && (*this).refd; + if wanted == (*this).keeping_alive { + return; + } + (*this).keeping_alive = wanted; + if wanted { + (*(*this).link.loop_).add_active(1); + } else { + (*(*this).link.loop_).sub_active(1); + } + } + } + + /// Wait for a client on `slot`, creating its instance first if it has none. + /// + /// # 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_; + (*slot).posted_error = None; + (*slot).op.overlapped.internal = 0; + (*slot).op.overlapped.internal_high = 0; + if (*slot).handle == INVALID_HANDLE_VALUE { + match create_instance(loop_, &(*this).name, false) { + Ok(handle) => (*slot).handle = handle, + Err(err) => (*slot).posted_error = Some(err), + } + } + let mut queued = false; + if (*slot).posted_error.is_none() { + let ok = win::ConnectNamedPipe((*slot).handle, (&raw mut (*slot).op).cast()); + let err = if ok != 0 { + Win32Error::SUCCESS + } else { + win::last_error() + }; + // A call that succeeded at once queues its packet like one that pends. + if ok != 0 || err == win::IO_PENDING { + super::op_submitted(loop_); + queued = true; + } else if err != Win32Error::PIPE_CONNECTED { + // PIPE_CONNECTED is a client that got in between the + // instance's creation and this call; anything else failed. + (*slot).posted_error = Some(err); + } + } + // Outcomes that produce no packet travel through the port too, so + // the owner hears of every client from the loop. + if !queued && !super::post_to_loop(loop_, &raw mut (*slot).op) { + if (*slot).handle != INVALID_HANDLE_VALUE { + win::CloseHandle((*slot).handle); + (*slot).handle = INVALID_HANDLE_VALUE; + } + return; + } + (*slot).in_flight = true; + (*this).pending += 1; + } + } + + /// # 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_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).closing { + Self::stop(this); + (*this).closing = true; + } + Self::update_keep_alive(this); + (*this).detached = true; + Link::remove(link); + Self::maybe_finish(this); + } + } + + /// # Safety + /// `this` is live and not closing. + unsafe fn stop(this: *mut Inner) { + // SAFETY: caller contract. + unsafe { + for handle in (*this).connected.drain(..) { + win::CloseHandle(handle); + } + // 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; + } + for slot in (*this).slots.drain(..) { + if (*slot).handle != INVALID_HANDLE_VALUE { + win::CloseHandle((*slot).handle); + } + drop(bun_core::heap::take(slot)); + } + if !(*this).owner_gone { + return; + } + if !(*this).detached { + Self::update_keep_alive(this); + Link::remove(this.cast()); + } + drop(bun_core::heap::take(this)); + } + } +} + +impl AcceptOp { + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op, _entry: *mut OverlappedEntry) { + 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 { + super::op_dequeued(loop_); + let this = (*slot).server; + (*slot).in_flight = false; + (*this).pending -= 1; + if (*this).closing { + Inner::maybe_finish(this); + return; + } + let err = (*slot) + .posted_error + .take() + .unwrap_or_else(|| win::status_to_win32((*slot).op.status())); + if err != Win32Error::SUCCESS { + // Nothing the owner can act on: replace the instance. + if (*slot).handle != INVALID_HANDLE_VALUE { + win::CloseHandle((*slot).handle); + (*slot).handle = INVALID_HANDLE_VALUE; + } + // Out of instances: retrying at once would spin. + if err != Win32Error::PIPE_BUSY && err != Win32Error::NOT_ENOUGH_MEMORY { + Inner::arm(this, slot); + } + return; + } + + (*this).connected.push((*slot).handle); + (*slot).handle = INVALID_HANDLE_VALUE; + // A fresh instance waits for the next client before the owner + // hears of this one. So do the slots that ran out of resources + // earlier and were left idle rather than retried in a loop. + for index in 0..(&(*this).slots).len() { + let other = (&(*this).slots)[index]; + if !(*other).in_flight { + Inner::arm(this, other); + } + } + let on_connection = (*this).on_connection; + (*this).pins += 1; + on_connection.invoke(()); + (*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..4daf75a34140 --- /dev/null +++ b/src/io/windows/sys.rs @@ -0,0 +1,137 @@ +//! 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::kernel32::{ + CreateNamedPipeW, DuplicateHandle, GetCurrentProcess, ReadFile, WriteFile, +}; +pub(crate) use bun_windows_sys::ntdll::NtQueryInformationFile; +pub(crate) use bun_windows_sys::{ + BOOL, CloseHandle, ConnectNamedPipe, CreateFileW, DUPLICATE_SAME_ACCESS, DWORD, + FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, FILE_INFORMATION_CLASS, GENERIC_READ, + GENERIC_WRITE, GetConsoleMode, HANDLE, INPUT_RECORD, INVALID_HANDLE_VALUE, IO_STATUS_BLOCK, + NTSTATUS, OPEN_EXISTING, OVERLAPPED, PIPE_READMODE_BYTE, PIPE_TYPE_BYTE, PIPE_WAIT, + SetConsoleMode, WRITE_DAC, Win32Error, +}; + +pub(crate) const IO_PENDING: Win32Error = Win32Error(997); +pub(crate) const MORE_DATA: Win32Error = Win32Error(234); + +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 FILE_READ_ATTRIBUTES: DWORD = 0x0080; +pub(crate) const FILE_WRITE_ATTRIBUTES: DWORD = 0x0100; +pub(crate) const WT_EXECUTELONGFUNCTION: u32 = 0x0000_0010; + +/// A `FILE_INFORMATION_CLASS` value for `NtQueryInformationFile`. +pub(crate) const FILE_MODE_INFORMATION: u32 = 16; + +pub(crate) const FILE_SYNCHRONOUS_IO_ALERT: u32 = 0x0000_0010; +pub(crate) const FILE_SYNCHRONOUS_IO_NONALERT: u32 = 0x0000_0020; + +#[repr(C)] +pub(crate) struct CONSOLE_READCONSOLE_CONTROL { + pub nLength: u32, + pub nInitialChars: u32, + pub dwCtrlWakeupMask: u32, + pub dwControlKeyState: u32, +} + +pub(crate) const KEY_EVENT: u16 = 0x0001; +pub(crate) const FOCUS_EVENT: u16 = 0x0010; +pub(crate) const LEFT_CTRL_PRESSED: u32 = 0x0008; + +pub(crate) const ENABLE_PROCESSED_INPUT: DWORD = 0x0001; +pub(crate) const ENABLE_LINE_INPUT: DWORD = 0x0002; +pub(crate) const ENABLE_ECHO_INPUT: DWORD = 0x0004; +pub(crate) const ENABLE_WINDOW_INPUT: DWORD = 0x0008; +pub(crate) const ENABLE_VIRTUAL_TERMINAL_INPUT: DWORD = 0x0200; + +pub(crate) type LPTHREAD_START_ROUTINE = unsafe extern "system" fn(*mut c_void) -> DWORD; + +#[link(name = "kernel32")] +unsafe extern "system" { + pub(crate) fn CancelIoEx(hFile: HANDLE, lpOverlapped: *mut OVERLAPPED) -> BOOL; + pub(crate) fn CancelSynchronousIo(hThread: HANDLE) -> BOOL; + pub(crate) fn PostQueuedCompletionStatus( + CompletionPort: HANDLE, + dwNumberOfBytesTransferred: DWORD, + dwCompletionKey: usize, + lpOverlapped: *mut OVERLAPPED, + ) -> BOOL; + pub(crate) fn CreateEventW( + lpEventAttributes: *mut c_void, + bManualReset: BOOL, + bInitialState: BOOL, + lpName: *const u16, + ) -> HANDLE; + pub(crate) fn SetEvent(hEvent: HANDLE) -> BOOL; + pub(crate) fn WaitNamedPipeW(lpNamedPipeName: *const u16, nTimeOut: DWORD) -> BOOL; + pub(crate) fn GetNamedPipeClientProcessId(Pipe: HANDLE, ClientProcessId: *mut u32) -> BOOL; + pub(crate) fn GetNamedPipeServerProcessId(Pipe: HANDLE, ServerProcessId: *mut u32) -> BOOL; + pub(crate) fn PeekNamedPipe( + hNamedPipe: HANDLE, + lpBuffer: *mut c_void, + nBufferSize: DWORD, + lpBytesRead: *mut DWORD, + lpTotalBytesAvail: *mut DWORD, + lpBytesLeftThisMessage: *mut DWORD, + ) -> 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) safe fn SwitchToThread() -> 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() +} + +/// 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..77e4b89f895f --- /dev/null +++ b/src/io/windows/tty.rs @@ -0,0 +1,1101 @@ +//! 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. + +use core::ffi::c_void; +use core::ptr::{self, NonNull}; +use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, AtomicU32, Ordering}; +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, OverlappedEntry, 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}; + +/// 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); +static ORIGINAL_INPUT_HANDLE: AtomicPtr = AtomicPtr::new(ptr::null_mut()); +static RAW_WAITERS: AtomicU32 = AtomicU32::new(0); +static LINE_READERS: AtomicU32 = AtomicU32::new(0); +static ON_RESIZE: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut()); + +/// Ends a blocked line read without touching the screen or the typed text: +/// the console returns from `ReadConsoleW` when this control character is +/// entered (`dwCtrlWakeupMask`), with the character last in the buffer. +const WAKE_CHAR: u16 = 0x1d; +const WAKE_VIRTUAL_KEY: u16 = 0xDD; +const WAKE_SCAN_CODE: u16 = 0x1B; + +/// 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, + } +} + +/// 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); + { + // Console writes must not interleave with the mode change. + let _output = tty_output::lock_output(); + let mut previous: u32 = 0; + // SAFETY: `previous` is a live local. + if unsafe { win::GetConsoleMode(input, &raw mut previous) } == 0 { + return Err(fail(win::last_error())); + } + if ORIGINAL_INPUT_MODE + .compare_exchange(u32::MAX, previous, Ordering::AcqRel, Ordering::Acquire) + .is_ok() + { + ORIGINAL_INPUT_HANDLE.store(input, Ordering::Release); + bun_core::add_exit_callback(Bun__Windows__resetConsoleMode); + } + let flags = match mode { + Mode::Normal => { + win::ENABLE_ECHO_INPUT | win::ENABLE_LINE_INPUT | win::ENABLE_PROCESSED_INPUT + } + Mode::Raw => win::ENABLE_WINDOW_INPUT, + Mode::RawVt => win::ENABLE_WINDOW_INPUT | win::ENABLE_VIRTUAL_TERMINAL_INPUT, + }; + // SAFETY: plain Win32 calls on the caller's console handle. + unsafe { + if win::SetConsoleMode(input, flags) == 0 { + // A console without VT input: the key table does the translation. + if mode != Mode::RawVt || win::SetConsoleMode(input, win::ENABLE_WINDOW_INPUT) == 0 + { + return Err(fail(win::last_error())); + } + } + } + } + let was = INPUT_MODE.swap(mode as u8, Ordering::AcqRel); + if (was == Mode::Normal as u8) != (mode == Mode::Normal) { + wake_readers(input); + } + 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; + } + let handle = ORIGINAL_INPUT_HANDLE.load(Ordering::Acquire); + // SAFETY: plain Win32 calls. The handle the mode was first changed through + // may have been closed since (the call then fails harmlessly); the console + // itself is still reachable by name. + unsafe { + if win::SetConsoleMode(handle, original) != 0 { + return; + } + let console = 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(), + ); + if console != INVALID_HANDLE_VALUE { + win::SetConsoleMode(console, original); + win::CloseHandle(console); + } + } +} + +/// [`set_console_mode`] for C++, in libuv's terms: `mode` is a `uv_tty_mode_t` +/// (0 normal, 1 raw, 2 "IO", 3 raw with VT input) and the result is 0 or a +/// negative `UV_E*`. +#[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) => bun_errno::uv_codes::e_discriminant_to_uv(err.errno) + .unwrap_or_else(|| core::ffi::c_int::from(err.errno).wrapping_neg()), + } +} + +/// [`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, + ); +} + +/// Get every reader out of the mechanism that the mode change made wrong. +fn wake_readers(input: HANDLE) { + // SAFETY: an all-zero `INPUT_RECORD` is a valid (empty) record. + let mut records: [win::INPUT_RECORD; 2] = unsafe { bun_core::ffi::zeroed_unchecked() }; + let mut count = 0usize; + if RAW_WAITERS.load(Ordering::Acquire) > 0 { + // Any record signals the handle; this one means nothing to anybody. + records[count].EventType = win::FOCUS_EVENT; + count += 1; + } + if LINE_READERS.load(Ordering::Acquire) > 0 { + records[count] = wake_record(); + count += 1; + } + if count == 0 { + return; + } + let mut written: u32 = 0; + // SAFETY: `records[..count]` is initialized; `written` is a live local. + unsafe { win::WriteConsoleInputW(input, records.as_ptr(), count as u32, &raw mut written) }; +} + +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: WAKE_SCAN_CODE, + uChar: bun_windows_sys::KEY_EVENT_RECORD_uChar { + UnicodeChar: WAKE_CHAR, + }, + dwControlKeyState: win::LEFT_CTRL_PRESSED, + }; + record +} + +/// Whether `handle` is a console (input or screen buffer). +pub fn is_console(handle: HANDLE) -> bool { + let mut mode: u32 = 0; + // SAFETY: `mode` is a live local. + unsafe { win::GetConsoleMode(handle, &raw mut mode) != 0 } +} + +type ReadCallback = unsafe fn(*const (), *mut c_void, ReadEvent<'_>); + +#[derive(Clone, Copy)] +struct Reader { + ctx: *mut c_void, + f: *const (), + call: ReadCallback, +} + +/// 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, + raw_op: *mut RawOp, + line_op: *mut LineOp, + /// A line that arrived while the owner was not reading. + held: Vec, + held_error: Option, + + output: OutputState, + writes_in_flight: u32, + + pending: u32, + pins: u32, +} + +#[repr(C)] +struct RawOp { + op: Op, + tty: *mut Inner, + wait: *mut Wait, + armed: bool, + buf: Vec, +} + +#[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. + cancel: AtomicBool, + stop: AtomicBool, + /// Held to set `cancel`/`stop` and look at `in_call`, and by the reader to + /// look at them and set `in_call`: the wake key is only typed for a + /// `ReadConsoleW` that will take it out of the queue again. + wake_lock: bun_threading::Mutex, + in_call: 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, +} + +/// A packet that carries no I/O: a write's completion or held input. +#[repr(C)] +struct PostedOp { + op: Op, + tty: *mut Inner, + write: Option<(Callback>, sys::Result)>, +} + +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 mut dup: HANDLE = ptr::null_mut(); + // SAFETY: both process handles are the pseudo-handle; `original` + // is the caller's open HANDLE. + let ok = unsafe { + win::DuplicateHandle( + win::GetCurrentProcess(), + original, + win::GetCurrentProcess(), + &raw mut dup, + 0, + 0, + win::DUPLICATE_SAME_ACCESS, + ) + }; + if ok == 0 { + return Err(sys::Error::from_win32(win::last_error(), Tag::dup).with_fd(fd)); + } + (dup, Some(Fd::from_system(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(), + line_op: ptr::null_mut(), + held: Vec::new(), + held_error: None, + output: OutputState::new(), + writes_in_flight: 0, + pending: 0, + pins: 0, + })); + // SAFETY: `inner` is at its final address; `link` is its first field. + unsafe { + 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) + } + + /// An input handle (as opposed to a screen buffer). + pub fn is_readable(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { (*self.raw()).flags.contains(Flags::READABLE) } + } + + pub fn is_closed(&self) -> bool { + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { (*self.raw()).flags.contains(Flags::DETACHED) } + } + + 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.writes_in_flight > 0) + } + + /// 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()); + } + } + + /// Set the console's input mode through this (input) handle. + pub fn set_mode(&mut self, mode: Mode) -> sys::Result<()> { + if !self.is_readable() { + return Err(sys::Error::from_code(E::EINVAL, Tag::uv_tty_set_mode).with_fd(self.fd())); + } + set_console_mode(self.handle(), mode) + } + + /// Deliver console input to `on_read(ctx, ..)`, always from the loop. `ctx` + /// must stay valid until `read_stop`, an `Err` event, or the `Tty` is + /// closed or dropped. A console has no end of file. + pub fn read_start( + &mut self, + ctx: *mut T, + on_read: unsafe fn(*mut T, ReadEvent<'_>), + ) -> sys::Result<()> { + unsafe fn call(f: *const (), ctx: *mut c_void, event: ReadEvent<'_>) { + // SAFETY: `f` was erased from exactly this type below. + let f = + unsafe { core::mem::transmute::<*const (), unsafe fn(*mut T, ReadEvent<'_>)>(f) }; + // SAFETY: the owner keeps `ctx` valid while reading. + unsafe { f(ctx.cast::(), event) } + } + 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(Reader { + ctx: ctx.cast(), + f: on_read as *const (), + call: call::, + }); + 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`. + pub fn write( + &mut self, + data: &[u8], + ctx: *mut T, + on_write: unsafe fn(*mut T, sys::Result), + ) -> sys::Result<()> { + let this = self.raw(); + let result = self.try_write(data); + // SAFETY: `inner` is live while the owner's `Tty` is. + unsafe { + if (*this).flags.contains(Flags::DETACHED) { + return result.map(|_| ()); + } + let op = bun_core::heap::into_raw(Box::new(PostedOp { + op: Op::new(PostedOp::complete), + tty: this, + write: Some((Callback::new(ctx, on_write), result)), + })); + if !super::post_to_loop((*this).link.loop_, &raw mut (*op).op) { + drop(bun_core::heap::take(op)); + return Err(sys::Error::from_win32(win::last_error(), Tag::write)); + } + (*this).pending += 1; + (*this).writes_in_flight += 1; + Inner::update_keep_alive(this); + } + Ok(()) + } + + /// Write `data` to the screen buffer now. + pub fn try_write(&mut self, data: &[u8]) -> sys::Result { + let this = self.raw(); + // SAFETY: `inner` is live while the owner's `Tty` is; the console lock + // inside `write` 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 tty_output::write((*this).handle, &mut (*this).output, data) { + Ok(()) => Ok(data.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, false) }; + } +} + +impl Drop for Tty { + fn drop(&mut self) { + // SAFETY: `inner` is live until `Inner::close` decides otherwise. + unsafe { Inner::close(self.raw(), true) }; + } +} + +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.writes_in_flight > 0); + 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_; + 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 current_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() { + // From the loop, not from inside `read_start`. + let op = bun_core::heap::into_raw(Box::new(PostedOp { + op: Op::new(PostedOp::complete), + tty: this, + write: None, + })); + if !super::post_to_loop(loop_, &raw mut (*op).op) { + drop(bun_core::heap::take(op)); + return Err(win::last_error()); + } + (*this).pending += 1; + return Ok(()); + } + + if current_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), + wake_lock: bun_threading::Mutex::new(), + in_call: AtomicBool::new(false), + chars: Vec::new(), + carry: 0, + bytes: Vec::new(), + error: 0, + woken: false, + })); + } + 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); + LINE_READERS.fetch_add(1, Ordering::AcqRel); + if !super::queue_blocking_work(LineOp::read_thread, op.cast()) { + let err = win::last_error(); + LINE_READERS.fetch_sub(1, Ordering::AcqRel); + (*op).port = None; + return Err(err); + } + super::op_submitted(loop_); + (*op).in_flight = true; + } else { + if (*this).raw_op.is_null() { + let wait = iocp::us_iocp_wait_create(loop_); + if wait.is_null() { + return Err(Win32Error::NOT_ENOUGH_MEMORY); + } + (*this).raw_op = bun_core::heap::into_raw(Box::new(RawOp { + op: Op::new(RawOp::complete), + tty: this, + wait, + armed: false, + buf: Vec::new(), + })); + } + let op = (*this).raw_op; + if iocp::us_iocp_wait_start((*op).wait, (*this).handle, &raw mut (*op).op) != 0 { + return Err(win::last_error()); + } + super::wait_submitted(loop_); + RAW_WAITERS.fetch_add(1, Ordering::AcqRel); + (*op).armed = true; + } + (*this).pending += 1; + Ok(()) + } + } + + /// # Safety + /// `this` is live, pinned, reading and has a reader. + unsafe fn deliver(this: *mut Inner, event: ReadEvent<'_>) { + // SAFETY: caller contract. + unsafe { + if let Some(reader) = (*this).reader { + (reader.call)(reader.f, reader.ctx, event); + } + } + } + + /// After a read finished: report an arming failure like a read error. + /// + /// # 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); + (*this).pins += 1; + Self::deliver(this, ReadEvent::Err(sys::Error::from_win32(err, Tag::read))); + (*this).pins -= 1; + } + } + } + + /// # Safety + /// `this` is live; the owner's `Tty` is consumed or being dropped. + unsafe fn close(this: *mut Inner, silent: bool) { + // SAFETY: caller contract. + unsafe { + (*this).flags.insert(Flags::OWNER_GONE); + if silent { + (*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); + } + (*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 { + let raw = (*this).raw_op; + if !raw.is_null() && (*raw).armed && iocp::us_iocp_wait_stop((*raw).wait) != 0 { + // Removed before it fired: no packet will come for it. + (*raw).armed = false; + RAW_WAITERS.fetch_sub(1, Ordering::AcqRel); + super::op_dequeued((*this).link.loop_); + (*this).pending -= 1; + } + 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()); + } + if !(*this).raw_op.is_null() { + let raw = bun_core::heap::take((*this).raw_op); + iocp::us_iocp_wait_free(raw.wait); + } + if !(*this).line_op.is_null() { + drop(bun_core::heap::take((*this).line_op)); + } + drop(bun_core::heap::take(this)); + } + } +} + +impl RawOp { + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op, _entry: *mut OverlappedEntry) { + let raw = op.cast::(); + // SAFETY: `op` is the first field of the `RawOp` whose wait fired; the + // tty outlives its packets (`pending`). + unsafe { + super::op_dequeued(loop_); + RAW_WAITERS.fetch_sub(1, Ordering::AcqRel); + (*raw).armed = false; + let this = (*raw).tty; + (*this).pending -= 1; + if (*this).gone() { + Inner::maybe_finish(this); + 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; + } + + (*raw).buf.clear(); + let result = + tty_input::read_raw((*this).handle, &mut (*this).raw_state, &mut (*raw).buf); + (*this).pins += 1; + if matches!(&result, Ok(read) if read.resized) { + let listener = ON_RESIZE.load(Ordering::Acquire); + if !listener.is_null() { + core::mem::transmute::<*mut (), fn()>(listener)(); + } + } + if !(*raw).buf.is_empty() && (*this).flags.contains(Flags::READING) { + Inner::deliver(this, ReadEvent::Data(&mut (*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, + )), + ); + } + (*this).pins -= 1; + if (*this).gone() { + Inner::maybe_finish(this); + 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) { + // SAFETY: caller contract. + unsafe { + (*line).wake_lock.lock(); + why.store(true, Ordering::Release); + if (*line).in_call.load(Ordering::Acquire) { + let record = wake_record(); + let mut written: u32 = 0; + win::WriteConsoleInputW((*line).handle, &raw const record, 1, &raw mut written); + } + (*line).wake_lock.unlock(); + } + } + + 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` (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 { + (*op).wake_lock.lock(); + let wanted = + !(*op).cancel.load(Ordering::Acquire) && !(*op).stop.load(Ordering::Acquire); + (*op).in_call.store(wanted, Ordering::Release); + (*op).wake_lock.unlock(); + if !wanted { + (*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; + if win::ReadConsoleW( + (*op).handle, + chars.as_mut_ptr().cast(), + LINE_READ_CHARS as u32, + &raw mut read, + &raw mut control, + ) == 0 + { + (*op).in_call.store(false, Ordering::Release); + (*op).error = win::last_error().int().into(); + break; + } + (*op).in_call.store(false, Ordering::Release); + // The count covers the characters carried in through + // `nInitialChars` as well. + let total = (read as usize).min(LINE_READ_CHARS); + chars.set_len(total); + if total > 0 && chars[total - 1] == WAKE_CHAR { + kept = total - 1; + if (*op).cancel.load(Ordering::Acquire) + || (*op).stop.load(Ordering::Acquire) + || current_mode() != Mode::Normal + { + (*op).woken = true; + break; + } + // The user typed the wake key (or another reader's wake-up + // landed here): carry on editing the same line. + if kept + 1 >= LINE_READ_CHARS { + chars.set_len(kept); + break; + } + continue; + } + 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); + } + } + LINE_READERS.fetch_sub(1, Ordering::AcqRel); + 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, _entry: *mut OverlappedEntry) { + 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 { + super::op_dequeued(loop_); + (*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 { + (*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; + } + (*this).pins += 1; + 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))); + } + (*this).pins -= 1; + if (*this).gone() { + Self::maybe_finish(this); + return; + } + Self::update_keep_alive(this); + Self::rearm(this); + } + } +} + +impl PostedOp { + unsafe extern "C" fn complete(loop_: *mut Loop, op: *mut Op, _entry: *mut OverlappedEntry) { + // SAFETY: `op` is the first field of a `PostedOp` posted by this + // module; the tty outlives its packets (`pending`). + unsafe { + super::op_dequeued(loop_); + let posted = bun_core::heap::take(op.cast::()); + let this = posted.tty; + (*this).pending -= 1; + + if let Some((callback, result)) = posted.write { + (*this).writes_in_flight -= 1; + if !(*this).flags.contains(Flags::SILENT) { + (*this).pins += 1; + callback.invoke(result); + (*this).pins -= 1; + } + if (*this).gone() { + Inner::maybe_finish(this); + } else { + Inner::update_keep_alive(this); + } + return; + } + + // Held input on its way to a reader that resumed. + if (*this).gone() { + Inner::maybe_finish(this); + } else { + 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..61ea853c0501 --- /dev/null +++ b/src/io/windows/tty_input.rs @@ -0,0 +1,705 @@ +//! 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}; + +/// 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 } + } +} + +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, +} + +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 { ffi::GetNumberOfConsoleInputEvents(handle, &raw mut available) } == 0 { + return Err(GetLastError()); + } + + let mut resized = false; + 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) }; + resized |= translate_all(state, batch, out); + available = available.saturating_sub(read); + } + Ok(RawInputResult { resized }) +} + +fn translate_all(state: &mut RawInputState, records: &[INPUT_RECORD], out: &mut Vec) -> bool { + let mut resized = false; + for record in records { + resized |= translate(state, record, out); + } + resized +} + +/// Appends the bytes for one record to `out`; returns whether it was a resize record. +fn translate(state: &mut RawInputState, record: &INPUT_RECORD, out: &mut Vec) -> bool { + match record.EventType { + ffi::WINDOW_BUFFER_SIZE_EVENT => true, + ffi::KEY_EVENT => { + // SAFETY: `EventType == KEY_EVENT` selects the `KeyEvent` member. + translate_key(state, unsafe { &record.Event.KeyEvent }, out); + false + } + _ => false, + } +} + +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 continuation = |shift: u32| 0x80 | ((code_point >> shift) & 0x3F) as u8; + if code_point < 0x80 { + out.push(code_point as u8); + } else if code_point < 0x800 { + out.extend_from_slice(&[0xC0 | (code_point >> 6) as u8, continuation(0)]); + } else if code_point < 0x10000 { + out.extend_from_slice(&[ + 0xE0 | (code_point >> 12) as u8, + continuation(6), + continuation(0), + ]); + } else { + out.extend_from_slice(&[ + 0xF0 | (code_point >> 18) as u8, + continuation(12), + continuation(6), + continuation(0), + ]); + } +} + +/// 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}; + + // `INPUT_RECORD::EventType` + pub(super) const KEY_EVENT: WORD = 0x0001; + 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 LEFT_CTRL_PRESSED: DWORD = 0x0008; + 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 GetNumberOfConsoleInputEvents( + hConsoleInput: HANDLE, + lpNumberOfEvents: *mut DWORD, + ) -> BOOL; + pub(super) fn ReadConsoleInputW( + hConsoleInput: HANDLE, + lpBuffer: *mut INPUT_RECORD, + nLength: DWORD, + lpNumberOfEventsRead: *mut DWORD, + ) -> BOOL; + } +} + +#[cfg(test)] +mod tests { + use bun_windows_sys::{ + COORD, INPUT_RECORD_Event, KEY_EVENT_RECORD_uChar, WINDOW_BUFFER_SIZE_EVENT, + }; + + use super::*; + + const ALT_L: DWORD = ffi::LEFT_ALT_PRESSED; + const ALT_R: DWORD = ffi::RIGHT_ALT_PRESSED; + const CTRL_L: DWORD = ffi::LEFT_CTRL_PRESSED; + const CTRL_R: DWORD = ffi::RIGHT_CTRL_PRESSED; + const SHIFT: DWORD = ffi::SHIFT_PRESSED; + + fn key_record(down: bool, repeat: WORD, vk: WORD, unit: u16, modifiers: DWORD) -> INPUT_RECORD { + INPUT_RECORD { + EventType: ffi::KEY_EVENT, + Event: INPUT_RECORD_Event { + KeyEvent: KEY_EVENT_RECORD { + bKeyDown: down.into(), + wRepeatCount: repeat, + wVirtualKeyCode: vk, + wVirtualScanCode: 0, + uChar: KEY_EVENT_RECORD_uChar { UnicodeChar: unit }, + dwControlKeyState: modifiers, + }, + }, + } + } + + fn char_down(unit: u16, modifiers: DWORD) -> INPUT_RECORD { + key_record(true, 1, 0, unit, modifiers) + } + + fn fn_down(vk: WORD, modifiers: DWORD) -> INPUT_RECORD { + key_record(true, 1, vk, 0, modifiers) + } + + fn resize_record() -> INPUT_RECORD { + INPUT_RECORD { + EventType: ffi::WINDOW_BUFFER_SIZE_EVENT, + Event: INPUT_RECORD_Event { + WindowBufferSizeEvent: WINDOW_BUFFER_SIZE_EVENT { + dwSize: COORD { X: 120, Y: 30 }, + }, + }, + } + } + + fn other_record(event_type: WORD) -> INPUT_RECORD { + INPUT_RECORD { + EventType: event_type, + ..key_record(true, 1, ffi::VK_UP, u16::from(b'x'), 0) + } + } + + fn run(records: &[INPUT_RECORD]) -> (Vec, bool) { + let mut state = RawInputState::new(); + let mut out = Vec::new(); + let resized = translate_all(&mut state, records, &mut out); + (out, resized) + } + + fn bytes(records: &[INPUT_RECORD]) -> Vec { + run(records).0 + } + + #[test] + fn plain_characters() { + assert_eq!(bytes(&[char_down(u16::from(b'a'), 0)]), b"a"); + assert_eq!(bytes(&[char_down(0x0D, 0)]), b"\r"); + // Ctrl+C + assert_eq!(bytes(&[char_down(0x03, CTRL_L)]), b"\x03"); + assert_eq!(bytes(&[char_down(0xE9, 0)]), "é".as_bytes()); + assert_eq!(bytes(&[char_down(0x20AC, 0)]), "€".as_bytes()); + assert_eq!( + bytes(&[ + char_down(u16::from(b'h'), SHIFT), + char_down(u16::from(b'i'), 0) + ]), + b"hi" + ); + } + + #[test] + fn alt_prefixes_escape() { + assert_eq!(bytes(&[char_down(u16::from(b'x'), ALT_L)]), b"\x1bx"); + assert_eq!(bytes(&[char_down(u16::from(b'x'), ALT_R)]), b"\x1bx"); + assert_eq!(bytes(&[char_down(0xE9, ALT_L | SHIFT)]), "\x1bé".as_bytes()); + } + + #[test] + fn altgr_is_not_prefixed() { + assert_eq!(bytes(&[char_down(0x20AC, ALT_R | CTRL_L)]), "€".as_bytes()); + assert_eq!(bytes(&[char_down(u16::from(b'@'), ALT_L | CTRL_R)]), b"@"); + } + + #[test] + fn surrogate_pair_across_records() { + // U+1F600 + assert_eq!( + bytes(&[char_down(0xD83D, 0), char_down(0xDE00, 0)]), + "😀".as_bytes() + ); + assert_eq!( + bytes(&[char_down(0xD83D, ALT_L), char_down(0xDE00, ALT_L)]), + "\x1b😀".as_bytes() + ); + // Unrelated records between the halves do not disturb the pending unit. + assert_eq!( + bytes(&[ + char_down(0xD83D, 0), + key_record(false, 1, 0, 0xD83D, 0), + resize_record(), + char_down(0xDE00, 0), + ]), + "😀".as_bytes() + ); + } + + #[test] + fn surrogate_pair_across_calls() { + let mut state = RawInputState::new(); + let mut first = Vec::new(); + assert!(!translate(&mut state, &char_down(0xD83D, 0), &mut first)); + assert!(first.is_empty()); + + let mut second = Vec::new(); + assert!(!translate(&mut state, &char_down(0xDE00, 0), &mut second)); + assert_eq!(second, "😀".as_bytes()); + + // The pending unit was consumed. + let mut third = Vec::new(); + translate(&mut state, &char_down(u16::from(b'a'), 0), &mut third); + assert_eq!(third, b"a"); + } + + #[test] + fn lone_surrogates_are_wtf8() { + assert_eq!(bytes(&[char_down(0xDE00, 0)]), [0xED, 0xB8, 0x80]); + assert_eq!( + bytes(&[char_down(0xD83D, 0), char_down(u16::from(b'a'), 0)]), + [0xED, 0xA0, 0xBD, b'a'] + ); + // A second high surrogate replaces the pending one. + assert_eq!( + bytes(&[ + char_down(0xD83C, 0), + char_down(0xD83D, 0), + char_down(0xDE00, 0) + ]), + "😀".as_bytes() + ); + } + + #[test] + fn alt_key_up_with_character() { + assert_eq!( + bytes(&[key_record(false, 1, ffi::VK_MENU, 0xE9, 0)]), + "é".as_bytes() + ); + // The key-up carries no prefix even though Alt is still reported. + assert_eq!( + bytes(&[key_record(false, 1, ffi::VK_MENU, 0xE9, ALT_L)]), + "é".as_bytes() + ); + assert_eq!( + bytes(&[ + key_record(false, 1, ffi::VK_MENU, 0xD83D, 0), + key_record(false, 1, ffi::VK_MENU, 0xDE00, 0), + ]), + "😀".as_bytes() + ); + assert_eq!(bytes(&[key_record(false, 1, ffi::VK_MENU, 0, 0)]), b""); + } + + #[test] + fn key_up_is_ignored() { + assert_eq!( + bytes(&[key_record(false, 1, 0x41, u16::from(b'a'), 0)]), + b"" + ); + assert_eq!(bytes(&[key_record(false, 1, ffi::VK_UP, 0, 0)]), b""); + assert_eq!(bytes(&[key_record(false, 1, ffi::VK_F1, 0, SHIFT)]), b""); + } + + #[test] + fn arrow_keys() { + for (vk, letter) in [ + (ffi::VK_UP, 'A'), + (ffi::VK_DOWN, 'B'), + (ffi::VK_RIGHT, 'C'), + (ffi::VK_LEFT, 'D'), + ] { + let enhanced = ffi::ENHANCED_KEY; + assert_eq!( + bytes(&[fn_down(vk, enhanced)]), + format!("\x1b[{letter}").into_bytes() + ); + assert_eq!( + bytes(&[fn_down(vk, enhanced | SHIFT)]), + format!("\x1b[1;2{letter}").into_bytes() + ); + assert_eq!( + bytes(&[fn_down(vk, enhanced | CTRL_L)]), + format!("\x1b[1;5{letter}").into_bytes() + ); + assert_eq!( + bytes(&[fn_down(vk, enhanced | CTRL_R)]), + format!("\x1b[1;5{letter}").into_bytes() + ); + assert_eq!( + bytes(&[fn_down(vk, enhanced | SHIFT | CTRL_L)]), + format!("\x1b[1;6{letter}").into_bytes() + ); + } + } + + #[test] + fn alt_prefixes_function_keys() { + assert_eq!( + bytes(&[fn_down(ffi::VK_UP, ffi::ENHANCED_KEY | ALT_L)]), + b"\x1b\x1b[A" + ); + assert_eq!(bytes(&[fn_down(ffi::VK_F1, ALT_R)]), b"\x1b\x1b[[A"); + // Unlike a character key, Ctrl does not suppress the prefix. + assert_eq!( + bytes(&[fn_down(ffi::VK_DELETE, ffi::ENHANCED_KEY | ALT_L | CTRL_L)]), + b"\x1b\x1b[3;5~" + ); + } + + #[test] + fn navigation_and_numpad_keys() { + let rows: [(WORD, WORD, &str); 11] = [ + (ffi::VK_INSERT, ffi::VK_NUMPAD0, "[2~"), + (ffi::VK_END, ffi::VK_NUMPAD1, "[4~"), + (ffi::VK_DOWN, ffi::VK_NUMPAD2, "[B"), + (ffi::VK_NEXT, ffi::VK_NUMPAD3, "[6~"), + (ffi::VK_LEFT, ffi::VK_NUMPAD4, "[D"), + (ffi::VK_CLEAR, ffi::VK_NUMPAD5, "[G"), + (ffi::VK_RIGHT, ffi::VK_NUMPAD6, "[C"), + (ffi::VK_UP, ffi::VK_NUMPAD7, "[A"), + (ffi::VK_HOME, ffi::VK_NUMPAD8, "[1~"), + (ffi::VK_PRIOR, ffi::VK_NUMPAD9, "[5~"), + (ffi::VK_DELETE, ffi::VK_DECIMAL, "[3~"), + ]; + for (navigation, numpad, sequence) in rows { + let expected = format!("\x1b{sequence}").into_bytes(); + assert_eq!(bytes(&[fn_down(navigation, 0)]), expected); + assert_eq!(bytes(&[fn_down(numpad, 0)]), expected); + } + assert_eq!(bytes(&[fn_down(ffi::VK_HOME, SHIFT)]), b"\x1b[1;2~"); + assert_eq!(bytes(&[fn_down(ffi::VK_END, CTRL_L)]), b"\x1b[4;5~"); + assert_eq!( + bytes(&[fn_down(ffi::VK_PRIOR, SHIFT | CTRL_R)]), + b"\x1b[5;6~" + ); + assert_eq!( + bytes(&[fn_down(ffi::VK_CLEAR, SHIFT | CTRL_L)]), + b"\x1b[1;6G" + ); + } + + #[test] + fn function_keys() { + let rows: [(WORD, [&str; 4]); 12] = [ + (ffi::VK_F1, ["[[A", "[23~", "[11^", "[23^"]), + (ffi::VK_F2, ["[[B", "[24~", "[12^", "[24^"]), + (ffi::VK_F3, ["[[C", "[25~", "[13^", "[25^"]), + (ffi::VK_F4, ["[[D", "[26~", "[14^", "[26^"]), + (ffi::VK_F5, ["[[E", "[28~", "[15^", "[28^"]), + (ffi::VK_F6, ["[17~", "[29~", "[17^", "[29^"]), + (ffi::VK_F7, ["[18~", "[31~", "[18^", "[31^"]), + (ffi::VK_F8, ["[19~", "[32~", "[19^", "[32^"]), + (ffi::VK_F9, ["[20~", "[33~", "[20^", "[33^"]), + (ffi::VK_F10, ["[21~", "[34~", "[21^", "[34^"]), + (ffi::VK_F11, ["[23~", "[23$", "[23^", "[23@"]), + (ffi::VK_F12, ["[24~", "[24$", "[24^", "[24@"]), + ]; + for (vk, [normal, shift, ctrl, shift_ctrl]) in rows { + for (modifiers, sequence) in [ + (0, normal), + (SHIFT, shift), + (CTRL_L, ctrl), + (CTRL_R, ctrl), + (SHIFT | CTRL_L, shift_ctrl), + ] { + assert_eq!( + bytes(&[fn_down(vk, modifiers)]), + format!("\x1b{sequence}").into_bytes() + ); + } + } + } + + #[test] + fn unmapped_keys_are_dropped() { + // VK_SHIFT, VK_CONTROL, VK_MENU, VK_CAPITAL, VK_F13 + for vk in [0x10, 0x11, ffi::VK_MENU, 0x14, 0x7C] { + assert_eq!(bytes(&[fn_down(vk, 0)]), b""); + assert_eq!(bytes(&[fn_down(vk, ALT_L)]), b""); + } + } + + #[test] + fn repeat_count() { + assert_eq!( + bytes(&[key_record(true, 3, 0x41, u16::from(b'a'), 0)]), + b"aaa" + ); + assert_eq!( + bytes(&[key_record(true, 0, 0x41, u16::from(b'a'), 0)]), + b"a" + ); + assert_eq!( + bytes(&[key_record(true, 2, 0x41, u16::from(b'a'), ALT_L)]), + b"\x1ba\x1ba" + ); + assert_eq!( + bytes(&[key_record(true, 2, ffi::VK_LEFT, 0, ffi::ENHANCED_KEY)]), + b"\x1b[D\x1b[D" + ); + assert_eq!( + bytes(&[char_down(0xD83D, 0), key_record(true, 2, 0, 0xDE00, 0)]), + "😀😀".as_bytes() + ); + // Earlier output is not replayed. + assert_eq!( + bytes(&[ + char_down(u16::from(b'x'), 0), + key_record(true, 2, 0x41, u16::from(b'a'), 0) + ]), + b"xaa" + ); + } + + #[test] + fn numpad_composition_with_left_alt_is_ignored() { + let composing = [ + 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_NUMPAD1, + ffi::VK_NUMPAD2, + ffi::VK_NUMPAD3, + ffi::VK_NUMPAD4, + ffi::VK_NUMPAD5, + ffi::VK_NUMPAD6, + ffi::VK_NUMPAD7, + ffi::VK_NUMPAD8, + ffi::VK_NUMPAD9, + ]; + for vk in composing { + assert_eq!(bytes(&[fn_down(vk, ALT_L)]), b""); + // With NumLock on the digit itself is reported; it is part of the composition too. + assert_eq!( + bytes(&[key_record(true, 1, vk, u16::from(b'1'), ALT_L)]), + b"" + ); + assert_ne!(bytes(&[fn_down(vk, ALT_R)]), b""); + assert_ne!(bytes(&[fn_down(vk, ALT_L | ffi::ENHANCED_KEY)]), b""); + } + assert_eq!(bytes(&[fn_down(ffi::VK_DELETE, ALT_L)]), b"\x1b\x1b[3~"); + assert_eq!(bytes(&[fn_down(ffi::VK_DECIMAL, ALT_L)]), b"\x1b\x1b[3~"); + } + + #[test] + fn non_key_records() { + assert_eq!(run(&[resize_record()]), (Vec::new(), true)); + // MOUSE_EVENT, MENU_EVENT, FOCUS_EVENT + for event_type in [0x0002, 0x0008, 0x0010] { + assert_eq!(run(&[other_record(event_type)]), (Vec::new(), false)); + } + assert_eq!( + run(&[ + char_down(u16::from(b'a'), 0), + resize_record(), + other_record(0x0010), + char_down(u16::from(b'b'), 0), + ]), + (b"ab".to_vec(), true) + ); + } + + #[test] + fn read_raw_reports_the_os_error() { + let mut state = RawInputState::new(); + let mut out = Vec::new(); + const ERROR_INVALID_HANDLE: u32 = 6; + assert_eq!( + read_raw(core::ptr::null_mut(), &mut state, &mut out).map(|result| result.resized), + Err(ERROR_INVALID_HANDLE) + ); + assert!(out.is_empty()); + } +} diff --git a/src/io/windows/tty_output.rs b/src/io/windows/tty_output.rs new file mode 100644 index 000000000000..1f7a97635a4e --- /dev/null +++ b/src/io/windows/tty_output.rs @@ -0,0 +1,1047 @@ +//! Console output. Bytes are decoded as UTF-8 and 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, +} + +#[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, + /// `\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, + 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, `SetConsoleMode` on an input +/// handle, 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(()) +} + +/// 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> { + 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], + }; + writer.write(data); + 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(()) +} + +/// 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 { + loop { + let info = screen_buffer_info(handle)?; + let (start, count, attributes) = region(&info); + let mut written: DWORD = 0; + // SAFETY: `written` is a valid out-pointer; everything else is by value. + let filled = unsafe { + FillConsoleOutputCharacterW(handle, u16::from(b' '), count, start, &raw mut written) + != 0 + && FillConsoleOutputAttribute(handle, attributes, written, start, &raw mut written) + != 0 + }; + if filled { + return Ok(info); + } + let err = GetLastError(); + if err != ERROR_INVALID_PARAMETER { + return Err(err); + } + } +} + +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; + // SAFETY: `mode` is a valid out-pointer; a bad `handle` makes the calls fail. + let enabled = unsafe { + GetConsoleMode(handle, &raw 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 that is either relative to the cursor or + /// relative to the virtual window's top-left, clipped to the virtual window. + fn make_real_coord( + &mut self, + info: &CONSOLE_SCREEN_BUFFER_INFO, + x: i32, + x_relative: bool, + y: i32, + y_relative: bool, + ) -> COORD { + self.update_virtual_window(info); + + let mut y = if y_relative { + i32::from(info.dwCursorPosition.Y) + y + } else { + 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 = if x_relative { + i32::from(info.dwCursorPosition.X) + x + } else { + 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<'_> { + /// Non-shortest forms, surrogates and values past U+10FFFF are let through; sequences of up + /// to seven bytes are decoded. + 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 { + self.put_codepoint(self.state.utf8_codepoint); + } + 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)), + // A continuation byte, or 0xFF. + 1 | 8 => self.put_codepoint(REPLACEMENT_CHARACTER), + ones => { + self.state.utf8_codepoint = u32::from(c) & (0xFF >> (ones + 1)); + self.state.utf8_bytes_left = (ones - 1) as u8; + } + } + } + self.flush_text(); + } + + 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(0, true, -count, true)), + 'B' => self.emulate(|w| w.move_caret(0, true, count, true)), + 'C' => self.emulate(|w| w.move_caret(count, true, 0, true)), + 'D' => self.emulate(|w| w.move_caret(-count, true, 0, true)), + 'E' => self.emulate(|w| w.move_caret(0, false, count, true)), + 'F' => self.emulate(|w| w.move_caret(0, false, -count, true)), + 'G' => { + let x = self.state.coordinate_arg(0); + self.emulate(|w| w.move_caret(x, false, 0, true)); + } + 'H' | 'f' => { + let y = self.state.coordinate_arg(0); + let x = self.state.coordinate_arg(1); + self.emulate(|w| w.move_caret(x, false, y, false)); + } + '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: i32, + x_relative: bool, + y: i32, + y_relative: bool, + ) -> Result<(), DWORD> { + loop { + let info = screen_buffer_info(self.handle)?; + let position = self + .console + .make_real_coord(&info, x, x_relative, y, y_relative); + // SAFETY: by-value arguments only. + if unsafe { SetConsoleCursorPosition(self.handle, position) } != 0 { + return Ok(()); + } + let err = GetLastError(); + // The console may have been resized since `info` was read. + if err != ERROR_INVALID_PARAMETER { + return Err(err); + } + } + } + + /// `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)?; + // SAFETY: by-value arguments only. + if unsafe { 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, x1_relative) = (0, direction == 0); + // The far end is past any real width; `make_real_coord` clips it. + let (x2, x2_relative) = if direction == 1 { + (0, true) + } else { + (0xFFFF, false) + }; + let (y1, y1_relative, y2, y2_relative) = if entire_screen { + (x1, x1_relative, x2, x2_relative) + } else { + (0, true, 0, true) + }; + + let console = &mut *self.console; + blank_cells(self.handle, |info| { + let start = console.make_real_coord(info, x1, x1_relative, y1, y1_relative); + let end = console.make_real_coord(info, x2, x2_relative, y2, y2_relative); + 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(i32::from(position.X), false, i32::from(position.Y), false)?; + } + 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/internal-for-testing.ts b/src/js/internal-for-testing.ts index c06e72fdf5d7..da13bd3d4600 100644 --- a/src/js/internal-for-testing.ts +++ b/src/js/internal-for-testing.ts @@ -718,7 +718,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/node/child_process.ts b/src/js/node/child_process.ts index 29eee5b7b3f2..76402ecfb7ad 100644 --- a/src/js/node/child_process.ts +++ b/src/js/node/child_process.ts @@ -1368,7 +1368,7 @@ class ChildProcess extends EventEmitter { // 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". + // stdio is a named pipe, not a socket usockets adopts, so leave as "pipe". if (process.platform !== "win32") { for (let i = 3; i < bunStdio.length; i++) { if (bunStdio[i] === "pipe") bunStdio[i] = "socket-fd"; diff --git a/src/js/node/tty.ts b/src/js/node/tty.ts index cad1915116f7..fe14c554eff7 100644 --- a/src/js/node/tty.ts +++ b/src/js/node/tty.ts @@ -56,14 +56,14 @@ 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 windows, this goes through the stream handle itself, as the native + // reader owns the console handle the mode is set on. // // 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 + // Special case for stdin: the mode is set on the process's stdin + // handle, and its stream is constructed differently if (this.fd === 0) { const err = ttySetMode(flag); if (err) { diff --git a/src/jsc/Debugger.rs b/src/jsc/Debugger.rs index 02f850138b13..810b3e9a1a36 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()` / @@ -242,64 +241,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. @@ -329,37 +270,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/PosixSignalHandle.rs b/src/jsc/PosixSignalHandle.rs index 0a2042e0bf8c..2ff9ad273f97 100644 --- a/src/jsc/PosixSignalHandle.rs +++ b/src/jsc/PosixSignalHandle.rs @@ -1,6 +1,5 @@ use core::sync::atomic::{AtomicBool, AtomicU8, AtomicU32, Ordering}; -#[cfg(unix)] use crate::VirtualMachineRef as VirtualMachine; use crate::event_loop::EventLoop; use crate::{JSGlobalObject, Task}; @@ -12,26 +11,22 @@ const BUFFER_SIZE: usize = 8192; /// Signal numbers queued by signal handlers on any thread for the main loop. #[derive(Default)] pub struct PosixSignalHandle { - #[allow(dead_code)] ring: SignalRing, } impl PosixSignalHandle { // `pub const new = bun.TrivialNew(@This());` - #[allow(dead_code)] pub(crate) fn new(init: Self) -> Box { Box::new(init) } /// Returns `false` if the ring is full. The caller wakes the loop on `true`. - #[allow(dead_code)] pub(crate) fn enqueue(&self, signal: u8) -> bool { self.ring.enqueue(signal) } /// Drain as many signals as possible and enqueue them as tasks in the event loop. /// Called by the main thread, the ring's single consumer. - #[allow(dead_code)] pub(crate) fn drain(&self, event_loop: &mut EventLoop) { while let Some(signal) = self.ring.dequeue() { // `Task` is a plain `{ tag, ptr }` pair (no bitfield packing), so build it @@ -47,44 +42,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; @@ -213,16 +207,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..60d8ab87d524 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| { @@ -816,7 +812,7 @@ impl RuntimeTranspilerCache { 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() { + break 'brk match dfd.make_crt_owned() { Ok(f) => f, Err(e) => { dfd.close(); diff --git a/src/jsc/VirtualMachine.rs b/src/jsc/VirtualMachine.rs index 0ad57b8a1b8a..caf3a80c8912 100644 --- a/src/jsc/VirtualMachine.rs +++ b/src/jsc/VirtualMachine.rs @@ -178,9 +178,7 @@ pub struct VirtualMachine { pub runtime_state: *mut c_void, pub event_loop_handle: Option<*mut PlatformEventLoop>, /// 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>, @@ -935,8 +933,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) } @@ -1220,19 +1217,18 @@ impl VirtualMachine { } /// 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 + /// uws loop). `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. /// /// Same single-JS-thread soundness contract as [`Self::uws_loop_mut`] — - /// the `PlatformEventLoop` is a separate heap allocation (uws/uv-owned), + /// the `PlatformEventLoop` 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> { // 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 }) @@ -1242,7 +1238,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) @@ -1360,20 +1355,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) { @@ -1974,9 +1962,8 @@ 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 @@ -1984,10 +1971,11 @@ impl VirtualMachine { /// parked; 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 @@ -2012,42 +2000,23 @@ 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 + // 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. @@ -2068,7 +2037,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. @@ -2082,7 +2051,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 @@ -2108,19 +2077,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() } { @@ -2128,11 +2089,16 @@ impl VirtualMachine { } // SAFETY: this thread's loop; nothing ticks it any more. unsafe { (*vm.uws_loop()).internal_loop_data.jsc_vm = core::ptr::null_mut() }; + // What is still open on the loop caches its pointer and completes + // through it; each is cancelled and detached here, and freeing the + // loop collects the cancelled operations. + #[cfg(windows)] + { + bun_io::windows::close_all_for_loop(vm.uws_loop()); + bun_spawn::process::close_all_for_loop(vm.uws_loop()); + } 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 -------------------------------------------------- @@ -2143,13 +2109,12 @@ impl VirtualMachine { impl VirtualMachine { /// One stop-phase sweep: registered 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 @@ -2160,18 +2125,6 @@ impl VirtualMachine { // SAFETY: fn contract. result = result.and(unsafe { (hooks.stop_active_handles_for_vm_teardown)(this) }); } - // 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. @@ -2436,13 +2389,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 @@ -3682,20 +3628,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 { @@ -3790,7 +3722,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, }) } diff --git a/src/jsc/array_buffer.rs b/src/jsc/array_buffer.rs index 7faa3b00349b..bf433c4dc0a7 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 3afacf9bafc8..a9dc936b84f3 100644 --- a/src/jsc/bindings/BunJSCEventLoop.cpp +++ b/src/jsc/bindings/BunJSCEventLoop.cpp @@ -6,31 +6,14 @@ #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`. extern "C" std::atomic Bun__defaultRemainingRunsUntilSkipReleaseAccess; -// 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. -extern "C" unsigned int Bun__JSC_onBeforeWait(JSC::VM* _Nonnull vm, uint64_t nowNs) +extern "C" void Bun__JSC_onBeforeWait(JSC::VM* _Nonnull vm) { 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 @@ -74,20 +57,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) { @@ -95,30 +69,6 @@ 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 } } - return runAgainInMs; } diff --git a/src/jsc/bindings/BunProcess.cpp b/src/jsc/bindings/BunProcess.cpp index 4deedb2a8b8f..4a52b69402dc 100644 --- a/src/jsc/bindings/BunProcess.cpp +++ b/src/jsc/bindings/BunProcess.cpp @@ -83,7 +83,10 @@ #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. @@ -148,6 +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(); +#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); +#endif extern "C" int32_t bun_stdio_tty[3]; namespace Bun { @@ -291,18 +298,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", BUN_REPORTED_LIBUV_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); @@ -1039,13 +1041,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* signalToContextIdsMap = nullptr; static const NeverDestroyed* getSignalNames() { @@ -1096,7 +1093,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); @@ -1292,34 +1289,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) @@ -1542,12 +1511,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 @@ -1572,6 +1543,14 @@ extern "C" void Bun__installWatchModeSignalHandler(int signalNumber) watchModeStickySignal = signalNumber; installForwardSignalHandler(signalNumber); } +#else +// Windows has no signals to install a handler for. While a signal is watched, +// the console control handler claims its event (CTRL_C_EVENT is SIGINT, +// CTRL_BREAK_EVENT is SIGBREAK, CTRL_CLOSE_EVENT is SIGHUP) and a console +// resize raises SIGWINCH; both report through Bun__onPosixSignal, so from +// there on a signal takes the same path on every platform. +extern "C" void Bun__watchWindowsSignal(int signalNumber); +extern "C" void Bun__unwatchWindowsSignal(int signalNumber); #endif extern "C" void Bun__MemoryPressure__install(JSC::JSGlobalObject* global); @@ -1622,7 +1601,7 @@ static void onDidChangeListeners(EventEmitter& eventEmitter, const Identifier& e loadSignalNumberToNameMap(); if (!signalToContextIdsMap) { - signalToContextIdsMap = new HashMap(); + signalToContextIdsMap = new HashSet(); } if (auto signalNumber = signalNameToNumberMap->get(eventName.string())) { @@ -1645,28 +1624,16 @@ static void onDidChangeListeners(EventEmitter& eventEmitter, const Identifier& e if (isAdded) { if (!signalToContextIdsMap->contains(signalNumber)) { - SignalHandleValue signal_handle = { -#if OS(WINDOWS) - .handle = nullptr, -#endif - }; -#if !OS(WINDOWS) 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); + signalToContextIdsMap->add(signalNumber); } } else { - if (signalToContextIdsMap->find(signalNumber) != signalToContextIdsMap->end() && listenerCount == 0) { + if (signalToContextIdsMap->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 // "has JS listeners" source of truth that e.g. self-kill flush consults. @@ -1677,8 +1644,7 @@ 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); @@ -3132,7 +3098,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 @@ -3876,9 +3842,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 {}; } @@ -3927,9 +3892,8 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionCpuUsage, (JSC::JSGlobalObject * global return {}; } #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 {}; } @@ -3939,8 +3903,13 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionCpuUsage, (JSC::JSGlobalObject * global Structure* cpuUsageStructure = process->cpuUsageStructure(); +#if !OS(WINDOWS) 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; +#else + double user = cpuTimes.user; + double system = cpuTimes.system; +#endif if (callFrame->argumentCount() > 0) { JSValue comparatorValue = callFrame->argument(0); @@ -4048,9 +4017,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); @@ -4058,15 +4025,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; @@ -4168,7 +4136,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 @@ -4185,12 +4157,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; @@ -4667,6 +4637,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); @@ -4678,21 +4653,20 @@ 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]; + DWORD length = GetConsoleTitleW(buffer, static_cast(std::size(buffer))); + 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 @@ -4714,12 +4688,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 } @@ -4791,7 +4767,7 @@ 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 @@ -4807,7 +4783,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/BunProcess.h b/src/jsc/bindings/BunProcess.h index 58bd83242c42..756aa6c76091 100644 --- a/src/jsc/bindings/BunProcess.h +++ b/src/jsc/bindings/BunProcess.h @@ -6,6 +6,11 @@ #include "BunClientData.h" #include "JSEventEmitter.h" +// process.versions.uv: the libuv that Node.js REPORTED_NODEJS_VERSION bundles +// (include/node/uv/version.h in that release's headers tarball). Changes with +// NODEJS_VERSION in scripts/build/deps/nodejs-headers.ts. +#define BUN_REPORTED_LIBUV_VERSION "1.52.1" + namespace Zig { class GlobalObject; } diff --git a/src/jsc/bindings/BunProcessReportObjectWindows.cpp b/src/jsc/bindings/BunProcessReportObjectWindows.cpp index 08f51d753148..4302a6b62c46 100644 --- a/src/jsc/bindings/BunProcessReportObjectWindows.cpp +++ b/src/jsc/bindings/BunProcessReportObjectWindows.cpp @@ -28,7 +28,8 @@ #include "JavaScriptCore/StackFrame.h" #include "JavaScriptCore/Interpreter.h" #include "wtf/text/OrdinalNumber.h" -#include +#include "OsBinding.h" +#include #include #include #include @@ -102,7 +103,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(BUN_REPORTED_LIBUV_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 +157,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 +184,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, {}); @@ -198,20 +197,20 @@ JSValue constructReportObjectWindows(VM& vm, Zig::GlobalObject* globalObject, Pr char addr[INET6_ADDRSTRLEN]; if (interfaces[i].address.address4.sin_family == AF_INET) { - uv_inet_ntop(AF_INET, &interfaces[i].address.address4.sin_addr, addr, sizeof(addr)); + 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)); + 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, "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)); + 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)); + 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, "family"_s, jsString(vm, String::fromLatin1("IPv6"))); diff --git a/src/jsc/bindings/BunWindowsProcess.cpp b/src/jsc/bindings/BunWindowsProcess.cpp new file mode 100644 index 000000000000..7efd65ecb108 --- /dev/null +++ b/src/jsc/bindings/BunWindowsProcess.cpp @@ -0,0 +1,150 @@ +#include "root.h" + +#if OS(WINDOWS) + +#include "BunWindowsProcess.h" +#include +#include +#include "BunWinternl.h" +#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 { + +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..b1fda915d6cc --- /dev/null +++ b/src/jsc/bindings/BunWindowsSignals.h @@ -0,0 +1,25 @@ +#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(). +#define SIGHUP 1 +#define SIGQUIT 3 +#define SIGKILL 9 +#define SIGWINCH 28 + +#if defined(NSIG) && NSIG <= SIGWINCH +#undef NSIG +#endif +#ifndef NSIG +#define NSIG (SIGWINCH + 1) +#endif + +#endif // OS(WINDOWS) diff --git a/src/jsc/bindings/BunWinternl.h b/src/jsc/bindings/BunWinternl.h new file mode 100644 index 000000000000..3320790f5ceb --- /dev/null +++ b/src/jsc/bindings/BunWinternl.h @@ -0,0 +1,8 @@ +#pragma once + +// declares a global `STRING` and then names it unqualified, which +// is ambiguous with JSC::STRING once a `using namespace JSC` is in effect (any +// earlier file of a unified bundle). +#define STRING BunWinternlSTRING +#include +#undef STRING diff --git a/src/jsc/bindings/OsBinding.cpp b/src/jsc/bindings/OsBinding.cpp index a61dfb27e8a5..bdffaef17320 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,252 @@ 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 "BunWinternl.h" +#include + +#pragma comment(lib, "iphlpapi.lib") +#pragma comment(lib, "advapi32.lib") 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. +static size_t writeUTF8(const WCHAR* string, int length, char* out, size_t capacity) +{ + int written = length == 0 ? 0 : WideCharToMultiByte(CP_UTF8, 0, string, length, out, static_cast(capacity - 1), nullptr, nullptr); + out[written] = '\0'; + return static_cast(written) + 1; +} + +// What NtQuerySystemInformation(SystemProcessorPerformanceInformation) fills in. +// winternl.h declares the same struct with DpcTime and InterruptTime as "Reserved1". +struct ProcessorPerformanceInformation { + LARGE_INTEGER IdleTime; + LARGE_INTEGER KernelTime; + LARGE_INTEGER UserTime; + LARGE_INTEGER DpcTime; + LARGE_INTEGER InterruptTime; + ULONG InterruptCount; +}; + +// Port of libuv's uv_cpu_info() (src/win/util.c). +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(ProcessorPerformanceInformation); + 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(ProcessorPerformanceInformation)); + + 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; + + DWORD speed; + 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); + 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; + info.cpu_times.irq = performance[i].InterruptTime.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). +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; + ULONG prefixLength = unicast->OnLinkPrefixLength; + + 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..8da22ff8f6c7 --- /dev/null +++ b/src/jsc/bindings/OsBinding.h @@ -0,0 +1,48 @@ +#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); +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..d52e56b3ec30 100644 --- a/src/jsc/bindings/ProcessBindingConstants.cpp +++ b/src/jsc/bindings/ProcessBindingConstants.cpp @@ -38,8 +38,18 @@ #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 +#include "BunWindowsSignals.h" #else // OS(WINDOWS) #include diff --git a/src/jsc/bindings/ProcessBindingTTYWrap.cpp b/src/jsc/bindings/ProcessBindingTTYWrap.cpp index b4d2bd7809c1..bc4d4470ee7c 100644 --- a/src/jsc/bindings/ProcessBindingTTYWrap.cpp +++ b/src/jsc/bindings/ProcessBindingTTYWrap.cpp @@ -23,38 +23,14 @@ #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); #endif namespace Bun { @@ -139,22 +115,10 @@ class TTYWrapObject final : public JSC::JSDestructibleObject { static_cast(cell)->TTYWrapObject::~TTYWrapObject(); } - ~TTYWrapObject() - { -#if OS(WINDOWS) - if (handle) { - handle->close(); - } -#endif - } + ~TTYWrapObject() = default; 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 +126,6 @@ class TTYWrapObject final : public JSC::JSDestructibleObject { , fd(fd) { -#if OS(WINDOWS) - handle = nullptr; -#endif } void finishCreation(JSC::VM& vm) @@ -192,9 +153,7 @@ JSC_DEFINE_HOST_FUNCTION(jsTTYSetMode, (JSC::JSGlobalObject * globalObject, Call auto flag = callFrame->argument(0); bool raw = flag.asBoolean(); - Zig::GlobalObject* global = uncheckedDowncast(globalObject); - - return JSValue::encode(jsNumber(Source__setRawModeStdin(global->uvLoop(), raw))); + return JSValue::encode(jsNumber(Source__setRawModeStdin(raw))); #else auto& vm = JSC::getVM(globalObject); auto scope = DECLARE_THROW_SCOPE(vm); @@ -261,12 +220,9 @@ JSC_DEFINE_HOST_FUNCTION(TTYWrap_functionSetMode, if (modeInt == 0) { Bun__setCTRLHandler(1); } - - int err = uv_tty_set_mode(ttyWrap->handle->tty(), modeInt); -#else +#endif // 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 +413,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(reinterpret_cast(_get_osfhandle(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..b873d4b8bcd8 100644 --- a/src/jsc/bindings/ProcessBindingUV.cpp +++ b/src/jsc/bindings/ProcessBindingUV.cpp @@ -10,13 +10,13 @@ // 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): 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 silently produces +// CRT-style codes on Windows (-2 for ENOENT) that match neither node nor the +// errors bun's fs emits there. +#include #define BUN_UV_ERRNO_MAP(macro) \ macro(E2BIG, "argument list too long") \ @@ -118,11 +118,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 71be788a977a..ac6231e8a4df 100644 --- a/src/jsc/bindings/ZigGlobalObject.h +++ b/src/jsc/bindings/ZigGlobalObject.h @@ -90,11 +90,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; @@ -333,12 +328,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..2fb060f56851 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) @@ -686,8 +688,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 +805,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,26 +814,10 @@ 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); } } } 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 0bf99cee85d3..a0e1a9180aa2 100644 --- a/src/jsc/bindings/libuv/README.md +++ b/src/jsc/bindings/libuv/README.md @@ -1,7 +1,7 @@ # libuv copied headers -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. These are copied headers from libuv. `bun uv-stubs` uses them 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. The handful of functions Bun does implement (`uv-polyfills.c`) use them too, and `uv/errno.h` has libuv's error numbers, which are what `err.errno` is on Windows. libuv commit hash: bb706f5fe71827f667f0bce532e95ce0698a498d @@ -9,6 +9,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/symbol.txt` and `src/linker.lds` and `src/symbols.dyn` -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/symbols.dyn`, `src/linker.lds` and `src/linker-freebsd.lds` when they differ from the lists diff --git a/src/jsc/bindings/libuv/generate_uv_posix_stubs.ts b/src/jsc/bindings/libuv/generate_uv_stubs.ts similarity index 87% rename from src/jsc/bindings/libuv/generate_uv_posix_stubs.ts rename to src/jsc/bindings/libuv/generate_uv_stubs.ts index 43bcb3015108..85a17bd3c885 100644 --- a/src/jsc/bindings/libuv/generate_uv_posix_stubs.ts +++ b/src/jsc/bindings/libuv/generate_uv_stubs.ts @@ -1,5 +1,5 @@ import { join } from "path"; -import { symbols, test_skipped } from "./generate_uv_posix_stubs_constants"; +import { exported, symbols, test_skipped } from "./generate_uv_stubs_constants"; import Parser from "tree-sitter"; import C from "tree-sitter-c"; @@ -287,27 +287,55 @@ for (const symbol of symbols) { 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" +#include "uv-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); +await Bun.write(join(import.meta.dir, "../", "uv-stubs.c"), final_contents); if (Bun.which("clang-format")) { - await Bun.$`clang-format -i ${join(import.meta.dir, "../", "uv-posix-stubs.c")}`; + await Bun.$`clang-format -i ${join(import.meta.dir, "../", "uv-stubs.c")}`; +} + +/** + * 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("symbols.dyn", name => ` _${name};`); +await syncExportList("linker.lds", name => ` ${name};`); +await syncExportList("linker-freebsd.lds", name => ` ${name};`); + 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) { 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 91% 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..783b0a32c7e1 100644 --- a/src/jsc/bindings/libuv/generate_uv_posix_stubs_constants.ts +++ b/src/jsc/bindings/libuv/generate_uv_stubs_constants.ts @@ -12,6 +12,26 @@ export const test_skipped = [ "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 = [ "uv_accept", "uv_async_init", @@ -123,8 +143,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 +180,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 +191,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 +302,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 +339,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/symbols.dyn, src/linker.lds, src/linker-freebsd.lds. */ +export const exported = [...symbols, ...polyfills].sort(); diff --git a/src/jsc/bindings/libuv/uv.h b/src/jsc/bindings/libuv/uv.h index 3c3076397dfa..9eac5685a577 100644 --- a/src/jsc/bindings/libuv/uv.h +++ b/src/jsc/bindings/libuv/uv.h @@ -66,8 +66,11 @@ struct uv__queue { struct uv__queue* prev; }; -/* This directory is only on the include path for non-Windows builds; Windows links the real libuv. */ +#if defined(_WIN32) +#include "uv/win.h" +#else #include "uv/unix.h" +#endif /* Expand this list if necessary. */ #define UV_ERRNO_MAP(XX) \ diff --git a/src/jsc/bindings/libuv/uv/tree.h b/src/jsc/bindings/libuv/uv/tree.h new file mode 100644 index 000000000000..06bba084f386 --- /dev/null +++ b/src/jsc/bindings/libuv/uv/tree.h @@ -0,0 +1,521 @@ +/*- + * Copyright 2002 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef UV_TREE_H_ +#define UV_TREE_H_ + +#ifndef UV__UNUSED +# if __GNUC__ +# define UV__UNUSED __attribute__((unused)) +# else +# define UV__UNUSED +# endif +#endif + +/* + * This file defines data structures for red-black trees. + * A red-black tree is a binary search tree with the node color as an + * extra attribute. It fulfills a set of conditions: + * - every search path from the root to a leaf consists of the + * same number of black nodes, + * - each red node (except for the root) has a black parent, + * - each leaf node is black. + * + * Every operation on a red-black tree is bounded as O(lg n). + * The maximum height of a red-black tree is 2lg (n+1). + */ + +/* Macros that define a red-black tree */ +#define RB_HEAD(name, type) \ +struct name { \ + struct type *rbh_root; /* root of the tree */ \ +} + +#define RB_INITIALIZER(root) \ + { NULL } + +#define RB_INIT(root) do { \ + (root)->rbh_root = NULL; \ +} while (/*CONSTCOND*/ 0) + +#define RB_BLACK 0 +#define RB_RED 1 +#define RB_ENTRY(type) \ +struct { \ + struct type *rbe_left; /* left element */ \ + struct type *rbe_right; /* right element */ \ + struct type *rbe_parent; /* parent element */ \ + int rbe_color; /* node color */ \ +} + +#define RB_LEFT(elm, field) (elm)->field.rbe_left +#define RB_RIGHT(elm, field) (elm)->field.rbe_right +#define RB_PARENT(elm, field) (elm)->field.rbe_parent +#define RB_COLOR(elm, field) (elm)->field.rbe_color +#define RB_ROOT(head) (head)->rbh_root +#define RB_EMPTY(head) (RB_ROOT(head) == NULL) + +#define RB_SET(elm, parent, field) do { \ + RB_PARENT(elm, field) = parent; \ + RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ + RB_COLOR(elm, field) = RB_RED; \ +} while (/*CONSTCOND*/ 0) + +#define RB_SET_BLACKRED(black, red, field) do { \ + RB_COLOR(black, field) = RB_BLACK; \ + RB_COLOR(red, field) = RB_RED; \ +} while (/*CONSTCOND*/ 0) + +#ifndef RB_AUGMENT +#define RB_AUGMENT(x) do {} while (0) +#endif + +#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ + (tmp) = RB_RIGHT(elm, field); \ + if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \ + RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_LEFT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (/*CONSTCOND*/ 0) + +#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ + (tmp) = RB_LEFT(elm, field); \ + if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \ + RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_RIGHT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (/*CONSTCOND*/ 0) + +/* Generates prototypes and inline functions */ +#define RB_PROTOTYPE(name, type, field, cmp) \ + RB_PROTOTYPE_INTERNAL(name, type, field, cmp,) +#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \ + RB_PROTOTYPE_INTERNAL(name, type, field, cmp, UV__UNUSED static) +#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \ +attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \ +attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\ +attr struct type *name##_RB_REMOVE(struct name *, struct type *); \ +attr struct type *name##_RB_INSERT(struct name *, struct type *); \ +attr struct type *name##_RB_FIND(struct name *, struct type *); \ +attr struct type *name##_RB_NFIND(struct name *, struct type *); \ +attr struct type *name##_RB_NEXT(struct type *); \ +attr struct type *name##_RB_PREV(struct type *); \ +attr struct type *name##_RB_MINMAX(struct name *, int); \ + \ + +/* Main rb operation. + * Moves node close to the key of elm to top + */ +#define RB_GENERATE(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp,) +#define RB_GENERATE_STATIC(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp, UV__UNUSED static) +#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \ +attr void \ +name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ +{ \ + struct type *parent, *gparent, *tmp; \ + while ((parent = RB_PARENT(elm, field)) != NULL && \ + RB_COLOR(parent, field) == RB_RED) { \ + gparent = RB_PARENT(parent, field); \ + if (parent == RB_LEFT(gparent, field)) { \ + tmp = RB_RIGHT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field); \ + elm = gparent; \ + continue; \ + } \ + if (RB_RIGHT(parent, field) == elm) { \ + RB_ROTATE_LEFT(head, parent, tmp, field); \ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_RIGHT(head, gparent, tmp, field); \ + } else { \ + tmp = RB_LEFT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field); \ + elm = gparent; \ + continue; \ + } \ + if (RB_LEFT(parent, field) == elm) { \ + RB_ROTATE_RIGHT(head, parent, tmp, field); \ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_LEFT(head, gparent, tmp, field); \ + } \ + } \ + RB_COLOR(head->rbh_root, field) = RB_BLACK; \ +} \ + \ +attr void \ +name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, \ + struct type *elm) \ +{ \ + struct type *tmp; \ + while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ + elm != RB_ROOT(head)) { \ + if (RB_LEFT(parent, field) == elm) { \ + tmp = RB_RIGHT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_LEFT(head, parent, tmp, field); \ + tmp = RB_RIGHT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) && \ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) { \ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) { \ + struct type *oleft; \ + if ((oleft = RB_LEFT(tmp, field)) \ + != NULL) \ + RB_COLOR(oleft, field) = RB_BLACK; \ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_RIGHT(head, tmp, oleft, field); \ + tmp = RB_RIGHT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field); \ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_RIGHT(tmp, field)) \ + RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \ + RB_ROTATE_LEFT(head, parent, tmp, field); \ + elm = RB_ROOT(head); \ + break; \ + } \ + } else { \ + tmp = RB_LEFT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_RIGHT(head, parent, tmp, field); \ + tmp = RB_LEFT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) && \ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) { \ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) { \ + struct type *oright; \ + if ((oright = RB_RIGHT(tmp, field)) \ + != NULL) \ + RB_COLOR(oright, field) = RB_BLACK; \ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_LEFT(head, tmp, oright, field); \ + tmp = RB_LEFT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field); \ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_LEFT(tmp, field)) \ + RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \ + RB_ROTATE_RIGHT(head, parent, tmp, field); \ + elm = RB_ROOT(head); \ + break; \ + } \ + } \ + } \ + if (elm) \ + RB_COLOR(elm, field) = RB_BLACK; \ +} \ + \ +attr struct type * \ +name##_RB_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *child, *parent, *old = elm; \ + int color; \ + if (RB_LEFT(elm, field) == NULL) \ + child = RB_RIGHT(elm, field); \ + else if (RB_RIGHT(elm, field) == NULL) \ + child = RB_LEFT(elm, field); \ + else { \ + struct type *left; \ + elm = RB_RIGHT(elm, field); \ + while ((left = RB_LEFT(elm, field)) != NULL) \ + elm = left; \ + child = RB_RIGHT(elm, field); \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ + if (RB_PARENT(elm, field) == old) \ + parent = elm; \ + (elm)->field = (old)->field; \ + if (RB_PARENT(old, field)) { \ + if (RB_LEFT(RB_PARENT(old, field), field) == old) \ + RB_LEFT(RB_PARENT(old, field), field) = elm; \ + else \ + RB_RIGHT(RB_PARENT(old, field), field) = elm; \ + RB_AUGMENT(RB_PARENT(old, field)); \ + } else \ + RB_ROOT(head) = elm; \ + RB_PARENT(RB_LEFT(old, field), field) = elm; \ + if (RB_RIGHT(old, field)) \ + RB_PARENT(RB_RIGHT(old, field), field) = elm; \ + if (parent) { \ + left = parent; \ + do { \ + RB_AUGMENT(left); \ + } while ((left = RB_PARENT(left, field)) != NULL); \ + } \ + goto color; \ + } \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ +color: \ + if (color == RB_BLACK) \ + name##_RB_REMOVE_COLOR(head, parent, child); \ + return (old); \ +} \ + \ +/* Inserts a node into the RB tree */ \ +attr struct type * \ +name##_RB_INSERT(struct name *head, struct type *elm) \ +{ \ + struct type *tmp; \ + struct type *parent = NULL; \ + int comp = 0; \ + tmp = RB_ROOT(head); \ + while (tmp) { \ + parent = tmp; \ + comp = (cmp)(elm, parent); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + RB_SET(elm, parent, field); \ + if (parent != NULL) { \ + if (comp < 0) \ + RB_LEFT(parent, field) = elm; \ + else \ + RB_RIGHT(parent, field) = elm; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = elm; \ + name##_RB_INSERT_COLOR(head, elm); \ + return (NULL); \ +} \ + \ +/* Finds the node with the same key as elm */ \ +attr struct type * \ +name##_RB_FIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (NULL); \ +} \ + \ +/* Finds the first node greater than or equal to the search key */ \ +attr struct type * \ +name##_RB_NFIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *res = NULL; \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) { \ + res = tmp; \ + tmp = RB_LEFT(tmp, field); \ + } \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (res); \ +} \ + \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_NEXT(struct type *elm) \ +{ \ + if (RB_RIGHT(elm, field)) { \ + elm = RB_RIGHT(elm, field); \ + while (RB_LEFT(elm, field)) \ + elm = RB_LEFT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} \ + \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_PREV(struct type *elm) \ +{ \ + if (RB_LEFT(elm, field)) { \ + elm = RB_LEFT(elm, field); \ + while (RB_RIGHT(elm, field)) \ + elm = RB_RIGHT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} \ + \ +attr struct type * \ +name##_RB_MINMAX(struct name *head, int val) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *parent = NULL; \ + while (tmp) { \ + parent = tmp; \ + if (val < 0) \ + tmp = RB_LEFT(tmp, field); \ + else \ + tmp = RB_RIGHT(tmp, field); \ + } \ + return (parent); \ +} + +#define RB_NEGINF -1 +#define RB_INF 1 + +#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) +#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) +#define RB_FIND(name, x, y) name##_RB_FIND(x, y) +#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y) +#define RB_NEXT(name, x) name##_RB_NEXT(x) +#define RB_PREV(name, x) name##_RB_PREV(x) +#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) +#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) + +#define RB_FOREACH(x, name, head) \ + for ((x) = RB_MIN(name, head); \ + (x) != NULL; \ + (x) = name##_RB_NEXT(x)) + +#define RB_FOREACH_FROM(x, name, y) \ + for ((x) = (y); \ + ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \ + (x) = (y)) + +#define RB_FOREACH_SAFE(x, name, head, y) \ + for ((x) = RB_MIN(name, head); \ + ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \ + (x) = (y)) + +#define RB_FOREACH_REVERSE(x, name, head) \ + for ((x) = RB_MAX(name, head); \ + (x) != NULL; \ + (x) = name##_RB_PREV(x)) + +#define RB_FOREACH_REVERSE_FROM(x, name, y) \ + for ((x) = (y); \ + ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \ + (x) = (y)) + +#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \ + for ((x) = RB_MAX(name, head); \ + ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \ + (x) = (y)) + +#endif /* UV_TREE_H_ */ diff --git a/src/jsc/bindings/libuv/uv/win.h b/src/jsc/bindings/libuv/uv/win.h new file mode 100644 index 000000000000..7b4ebd4b7b2e --- /dev/null +++ b/src/jsc/bindings/libuv/uv/win.h @@ -0,0 +1,698 @@ +/* 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 _WIN32_WINNT +# define _WIN32_WINNT 0x0A00 +#endif + +#if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED) +typedef intptr_t ssize_t; +# define SSIZE_MAX INTPTR_MAX +# define _SSIZE_T_ +# define _SSIZE_T_DEFINED +#endif + +#include + +#ifndef LOCALE_INVARIANT +# define LOCALE_INVARIANT 0x007f +#endif + +#include +/* Disable the typedef in mstcpip.h of MinGW. */ +#define _TCP_INITIAL_RTO_PARAMETERS _TCP_INITIAL_RTO_PARAMETERS__AVOID +#define TCP_INITIAL_RTO_PARAMETERS TCP_INITIAL_RTO_PARAMETERS__AVOID +#define PTCP_INITIAL_RTO_PARAMETERS PTCP_INITIAL_RTO_PARAMETERS__AVOID +#include +#undef _TCP_INITIAL_RTO_PARAMETERS +#undef TCP_INITIAL_RTO_PARAMETERS +#undef PTCP_INITIAL_RTO_PARAMETERS +#include + +#include +#include +#include +#include +#include + +#include "uv/tree.h" +#include "uv/threadpool.h" + +#define MAX_PIPENAME_LEN 256 + +#ifndef S_IFLNK +# define S_IFLNK 0xA000 +#endif + +/* Define missing in Windows Kit Include\{VERSION}\ucrt\sys\stat.h */ +#if defined(_CRT_INTERNAL_NONSTDC_NAMES) && _CRT_INTERNAL_NONSTDC_NAMES && !defined(S_IFIFO) +# define S_IFIFO _S_IFIFO +#endif + +/* Additional signals supported by uv_signal and or uv_kill. The CRT defines + * the following signals already: + * + * #define SIGINT 2 + * #define SIGILL 4 + * #define SIGABRT_COMPAT 6 + * #define SIGFPE 8 + * #define SIGSEGV 11 + * #define SIGTERM 15 + * #define SIGBREAK 21 + * #define SIGABRT 22 + * + * The additional signals have values that are common on other Unix + * variants (Linux and Darwin) + */ +#define SIGHUP 1 +#define SIGQUIT 3 +#define SIGKILL 9 +#define SIGWINCH 28 + +/* Redefine NSIG to take SIGWINCH into consideration */ +#if defined(NSIG) && NSIG <= SIGWINCH +# undef NSIG +#endif +#ifndef NSIG +# define NSIG SIGWINCH + 1 +#endif + +/* The CRT defines SIGABRT_COMPAT as 6, which equals SIGABRT on many unix-like + * platforms. However MinGW doesn't define it, so we do. */ +#ifndef SIGABRT_COMPAT +# define SIGABRT_COMPAT 6 +#endif + +/* + * Guids and typedefs for winsock extension functions + * Mingw32 doesn't have these :-( + */ +#ifndef WSAID_ACCEPTEX +# define WSAID_ACCEPTEX \ + {0xb5367df1, 0xcbac, 0x11cf, \ + {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}} + +# define WSAID_CONNECTEX \ + {0x25a207b9, 0xddf3, 0x4660, \ + {0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e}} + +# define WSAID_GETACCEPTEXSOCKADDRS \ + {0xb5367df2, 0xcbac, 0x11cf, \ + {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}} + +# define WSAID_DISCONNECTEX \ + {0x7fda2e11, 0x8630, 0x436f, \ + {0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}} + +# define WSAID_TRANSMITFILE \ + {0xb5367df0, 0xcbac, 0x11cf, \ + {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}} + + typedef BOOL (PASCAL *LPFN_ACCEPTEX) + (SOCKET sListenSocket, + SOCKET sAcceptSocket, + PVOID lpOutputBuffer, + DWORD dwReceiveDataLength, + DWORD dwLocalAddressLength, + DWORD dwRemoteAddressLength, + LPDWORD lpdwBytesReceived, + LPOVERLAPPED lpOverlapped); + + typedef BOOL (PASCAL *LPFN_CONNECTEX) + (SOCKET s, + const struct sockaddr* name, + int namelen, + PVOID lpSendBuffer, + DWORD dwSendDataLength, + LPDWORD lpdwBytesSent, + LPOVERLAPPED lpOverlapped); + + typedef void (PASCAL *LPFN_GETACCEPTEXSOCKADDRS) + (PVOID lpOutputBuffer, + DWORD dwReceiveDataLength, + DWORD dwLocalAddressLength, + DWORD dwRemoteAddressLength, + LPSOCKADDR* LocalSockaddr, + LPINT LocalSockaddrLength, + LPSOCKADDR* RemoteSockaddr, + LPINT RemoteSockaddrLength); + + typedef BOOL (PASCAL *LPFN_DISCONNECTEX) + (SOCKET hSocket, + LPOVERLAPPED lpOverlapped, + DWORD dwFlags, + DWORD reserved); + + typedef BOOL (PASCAL *LPFN_TRANSMITFILE) + (SOCKET hSocket, + HANDLE hFile, + DWORD nNumberOfBytesToWrite, + DWORD nNumberOfBytesPerSend, + LPOVERLAPPED lpOverlapped, + LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers, + DWORD dwFlags); + + typedef PVOID RTL_SRWLOCK; + typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK; +#endif + +typedef int (WSAAPI* LPFN_WSARECV) + (SOCKET socket, + LPWSABUF buffers, + DWORD buffer_count, + LPDWORD bytes, + LPDWORD flags, + LPWSAOVERLAPPED overlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine); + +typedef int (WSAAPI* LPFN_WSARECVFROM) + (SOCKET socket, + LPWSABUF buffers, + DWORD buffer_count, + LPDWORD bytes, + LPDWORD flags, + struct sockaddr* addr, + LPINT addr_len, + LPWSAOVERLAPPED overlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine); + +#ifndef _NTDEF_ + typedef LONG NTSTATUS; + typedef NTSTATUS *PNTSTATUS; +#endif + +#ifndef RTL_CONDITION_VARIABLE_INIT + typedef PVOID CONDITION_VARIABLE, *PCONDITION_VARIABLE; +#endif + +typedef struct _AFD_POLL_HANDLE_INFO { + HANDLE Handle; + ULONG Events; + NTSTATUS Status; +} AFD_POLL_HANDLE_INFO, *PAFD_POLL_HANDLE_INFO; + +typedef struct _AFD_POLL_INFO { + LARGE_INTEGER Timeout; + ULONG NumberOfHandles; + ULONG Exclusive; + AFD_POLL_HANDLE_INFO Handles[1]; +} AFD_POLL_INFO, *PAFD_POLL_INFO; + +#define UV_MSAFD_PROVIDER_COUNT 4 + + +/** + * It should be possible to cast uv_buf_t[] to WSABUF[] + * see http://msdn.microsoft.com/en-us/library/ms741542(v=vs.85).aspx + */ +typedef struct uv_buf_t { + ULONG len; + char* base; +} uv_buf_t; + +typedef int uv_file; +typedef SOCKET uv_os_sock_t; +typedef HANDLE uv_os_fd_t; +typedef int uv_pid_t; + +typedef HANDLE uv_thread_t; + +typedef HANDLE uv_sem_t; + +typedef CRITICAL_SECTION uv_mutex_t; + +/* This condition variable implementation is based on the SetEvent solution + * (section 3.2) at http://www.cs.wustl.edu/~schmidt/win32-cv-1.html + * We could not use the SignalObjectAndWait solution (section 3.4) because + * it want the 2nd argument (type uv_mutex_t) of uv_cond_wait() and + * uv_cond_timedwait() to be HANDLEs, but we use CRITICAL_SECTIONs. + */ + +typedef union { + CONDITION_VARIABLE cond_var; + struct { + unsigned int waiters_count; + CRITICAL_SECTION waiters_count_lock; + HANDLE signal_event; + HANDLE broadcast_event; + } unused_; /* TODO: retained for ABI compatibility; remove me in v2.x. */ +} uv_cond_t; + +typedef struct { + SRWLOCK read_write_lock_; + /* TODO: retained for ABI compatibility; remove me in v2.x */ +#ifdef _WIN64 + unsigned char padding_[72]; +#else + unsigned char padding_[44]; +#endif +} uv_rwlock_t; + +typedef struct { + unsigned threshold; + unsigned in; + uv_mutex_t mutex; + /* TODO: in v2 make this a uv_cond_t, without unused_ */ + CONDITION_VARIABLE cond; + unsigned out; +} uv_barrier_t; + +typedef struct { + DWORD tls_index; +} uv_key_t; + +#define UV_ONCE_INIT { 0, { NULL } } + +typedef struct uv_once_s { + unsigned char unused; + INIT_ONCE init_once; +} uv_once_t; + +/* Platform-specific definitions for uv_spawn support. */ +typedef unsigned char uv_uid_t; +typedef unsigned char uv_gid_t; + +typedef struct uv__dirent_s { + int d_type; + char d_name[1]; +} uv__dirent_t; + +#define UV_DIR_PRIVATE_FIELDS \ + HANDLE dir_handle; \ + WIN32_FIND_DATAW find_data; \ + BOOL need_find_call; + +#define HAVE_DIRENT_TYPES +#define UV__DT_DIR UV_DIRENT_DIR +#define UV__DT_FILE UV_DIRENT_FILE +#define UV__DT_LINK UV_DIRENT_LINK +#define UV__DT_FIFO UV_DIRENT_FIFO +#define UV__DT_SOCKET UV_DIRENT_SOCKET +#define UV__DT_CHAR UV_DIRENT_CHAR +#define UV__DT_BLOCK UV_DIRENT_BLOCK + +/* Platform-specific definitions for uv_dlopen support. */ +#define UV_DYNAMIC FAR WINAPI +typedef struct { + HMODULE handle; + char* errmsg; +} uv_lib_t; + +#define UV_LOOP_PRIVATE_FIELDS \ + /* The loop's I/O completion port */ \ + HANDLE iocp; \ + /* The current time according to the event loop. in msecs. */ \ + uint64_t time; \ + /* Tail of a single-linked circular queue of pending reqs. If the queue */ \ + /* is empty, tail_ is NULL. If there is only one item, */ \ + /* tail_->next_req == tail_ */ \ + uv_req_t* pending_reqs_tail; \ + /* Head of a single-linked list of closed handles */ \ + uv_handle_t* endgame_handles; \ + /* TODO(bnoordhuis) Stop heap-allocating |timer_heap| in libuv v2.x. */ \ + void* timer_heap; \ + /* Lists of active loop (prepare / check / idle) watchers */ \ + uv_prepare_t* prepare_handles; \ + uv_check_t* check_handles; \ + uv_idle_t* idle_handles; \ + /* This pointer will refer to the prepare/check/idle handle whose */ \ + /* callback is scheduled to be called next. This is needed to allow */ \ + /* safe removal from one of the lists above while that list being */ \ + /* iterated over. */ \ + uv_prepare_t* next_prepare_handle; \ + uv_check_t* next_check_handle; \ + uv_idle_t* next_idle_handle; \ + /* This handle holds the peer sockets for the fast variant of uv_poll_t */ \ + SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT]; \ + /* No longer used. */ \ + unsigned int active_tcp_streams; \ + /* No longer used. */ \ + unsigned int active_udp_streams; \ + /* Counter to started timer */ \ + uint64_t timer_counter; \ + /* Threadpool */ \ + struct uv__queue wq; \ + uv_mutex_t wq_mutex; \ + uv_async_t wq_async; + +#define UV_REQ_TYPE_PRIVATE \ + /* TODO: remove the req suffix */ \ + UV_ACCEPT, \ + UV_FS_EVENT_REQ, \ + UV_POLL_REQ, \ + UV_PROCESS_EXIT, \ + UV_READ, \ + UV_UDP_RECV, \ + UV_WAKEUP, \ + UV_SIGNAL_REQ, + +#define UV_REQ_PRIVATE_FIELDS \ + union { \ + /* Used by I/O operations */ \ + struct { \ + OVERLAPPED overlapped; \ + size_t queued_bytes; \ + } io; \ + /* in v2, we can move these to the UV_CONNECT_PRIVATE_FIELDS */ \ + struct { \ + ULONG_PTR result; /* overlapped.Internal is reused to hold the result */\ + HANDLE pipeHandle; \ + DWORD duplex_flags; \ + WCHAR* name; \ + } connect; \ + } u; \ + struct uv_req_s* next_req; + +#define UV_WRITE_PRIVATE_FIELDS \ + int coalesced; \ + uv_buf_t write_buffer; \ + HANDLE event_handle; \ + HANDLE wait_handle; + +#define UV_CONNECT_PRIVATE_FIELDS \ + /* empty */ + +#define UV_SHUTDOWN_PRIVATE_FIELDS \ + /* empty */ + +#define UV_UDP_SEND_PRIVATE_FIELDS \ + /* empty */ + +#define UV_PRIVATE_REQ_TYPES \ + typedef struct uv_pipe_accept_s { \ + UV_REQ_FIELDS \ + HANDLE pipeHandle; \ + struct uv_pipe_accept_s* next_pending; \ + } uv_pipe_accept_t; \ + \ + typedef struct uv_tcp_accept_s { \ + UV_REQ_FIELDS \ + SOCKET accept_socket; \ + char accept_buffer[sizeof(struct sockaddr_storage) * 2 + 32]; \ + HANDLE event_handle; \ + HANDLE wait_handle; \ + struct uv_tcp_accept_s* next_pending; \ + } uv_tcp_accept_t; \ + \ + typedef struct uv_read_s { \ + UV_REQ_FIELDS \ + HANDLE event_handle; \ + HANDLE wait_handle; \ + } uv_read_t; + +#define uv_stream_connection_fields \ + unsigned int write_reqs_pending; \ + uv_shutdown_t* shutdown_req; + +#define uv_stream_server_fields \ + uv_connection_cb connection_cb; + +#define UV_STREAM_PRIVATE_FIELDS \ + unsigned int reqs_pending; \ + int activecnt; \ + uv_read_t read_req; \ + union { \ + struct { uv_stream_connection_fields } conn; \ + struct { uv_stream_server_fields } serv; \ + } stream; + +#define uv_tcp_server_fields \ + uv_tcp_accept_t* accept_reqs; \ + unsigned int processed_accepts; \ + uv_tcp_accept_t* pending_accepts; \ + LPFN_ACCEPTEX func_acceptex; + +#define uv_tcp_connection_fields \ + uv_buf_t read_buffer; \ + LPFN_CONNECTEX func_connectex; + +#define UV_TCP_PRIVATE_FIELDS \ + SOCKET socket; \ + int delayed_error; \ + union { \ + struct { uv_tcp_server_fields } serv; \ + struct { uv_tcp_connection_fields } conn; \ + } tcp; + +#define UV_UDP_PRIVATE_FIELDS \ + SOCKET socket; \ + unsigned int reqs_pending; \ + int activecnt; \ + uv_req_t recv_req; \ + uv_buf_t recv_buffer; \ + struct sockaddr_storage recv_from; \ + int recv_from_len; \ + uv_udp_recv_cb recv_cb; \ + uv_alloc_cb alloc_cb; \ + LPFN_WSARECV func_wsarecv; \ + LPFN_WSARECVFROM func_wsarecvfrom; + +#define uv_pipe_server_fields \ + int pending_instances; \ + uv_pipe_accept_t* accept_reqs; \ + uv_pipe_accept_t* pending_accepts; + +#define uv_pipe_connection_fields \ + uv_timer_t* eof_timer; \ + uv_write_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \ + DWORD ipc_remote_pid; \ + union { \ + uint32_t payload_remaining; \ + uint64_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \ + } ipc_data_frame; \ + struct uv__queue ipc_xfer_queue; \ + int ipc_xfer_queue_length; \ + uv_write_t* non_overlapped_writes_tail; \ + CRITICAL_SECTION readfile_thread_lock; \ + volatile HANDLE readfile_thread_handle; + +#define UV_PIPE_PRIVATE_FIELDS \ + HANDLE handle; \ + WCHAR* name; \ + union { \ + struct { uv_pipe_server_fields } serv; \ + struct { uv_pipe_connection_fields } conn; \ + } pipe; + +/* TODO: put the parser states in a union - TTY handles are always half-duplex + * so read-state can safely overlap write-state. */ +#define UV_TTY_PRIVATE_FIELDS \ + HANDLE handle; \ + union { \ + struct { \ + /* Used for readable TTY handles */ \ + union { \ + /* TODO: remove me in v2.x. */ \ + HANDLE unused_; \ + int mode; \ + } mode; \ + uv_buf_t read_line_buffer; \ + HANDLE read_raw_wait; \ + /* Fields used for translating win keystrokes into vt100 characters */ \ + char last_key[8]; \ + unsigned char last_key_offset; \ + unsigned char last_key_len; \ + WCHAR last_utf16_high_surrogate; \ + INPUT_RECORD last_input_record; \ + } rd; \ + struct { \ + /* Used for writable TTY handles */ \ + /* utf8-to-utf16 conversion state */ \ + unsigned int utf8_codepoint; \ + unsigned char utf8_bytes_left; \ + /* eol conversion state */ \ + unsigned char previous_eol; \ + /* ansi parser state */ \ + unsigned short ansi_parser_state; \ + unsigned char ansi_csi_argc; \ + unsigned short ansi_csi_argv[4]; \ + COORD saved_position; \ + WORD saved_attributes; \ + } wr; \ + } tty; + +#define UV_POLL_PRIVATE_FIELDS \ + SOCKET socket; \ + /* Used in fast mode */ \ + SOCKET peer_socket; \ + AFD_POLL_INFO afd_poll_info_1; \ + AFD_POLL_INFO afd_poll_info_2; \ + /* Used in fast and slow mode. */ \ + uv_req_t poll_req_1; \ + uv_req_t poll_req_2; \ + unsigned char submitted_events_1; \ + unsigned char submitted_events_2; \ + unsigned char mask_events_1; \ + unsigned char mask_events_2; \ + unsigned char events; + +#define UV_TIMER_PRIVATE_FIELDS \ + union { \ + void* heap[3]; \ + struct uv__queue queue; \ + } node; \ + int unused; \ + uint64_t timeout; \ + uint64_t repeat; \ + uint64_t start_id; \ + uv_timer_cb timer_cb; + +#define UV_ASYNC_PRIVATE_FIELDS \ + struct uv_req_s async_req; \ + uv_async_cb async_cb; \ + /* char to avoid alignment issues */ \ + char volatile async_sent; + +#define UV_PREPARE_PRIVATE_FIELDS \ + uv_prepare_t* prepare_prev; \ + uv_prepare_t* prepare_next; \ + uv_prepare_cb prepare_cb; + +#define UV_CHECK_PRIVATE_FIELDS \ + uv_check_t* check_prev; \ + uv_check_t* check_next; \ + uv_check_cb check_cb; + +#define UV_IDLE_PRIVATE_FIELDS \ + uv_idle_t* idle_prev; \ + uv_idle_t* idle_next; \ + uv_idle_cb idle_cb; + +#define UV_HANDLE_PRIVATE_FIELDS \ + uv_handle_t* endgame_next; \ + unsigned int flags; + +#define UV_GETADDRINFO_PRIVATE_FIELDS \ + struct uv__work work_req; \ + uv_getaddrinfo_cb getaddrinfo_cb; \ + void* alloc; \ + WCHAR* node; \ + WCHAR* service; \ + /* The addrinfoW field is used to store a pointer to the hints, and */ \ + /* later on to store the result of GetAddrInfoW. The final result will */ \ + /* be converted to struct addrinfo* and stored in the addrinfo field. */ \ + struct addrinfoW* addrinfow; \ + 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_process_exit_s { \ + UV_REQ_FIELDS \ + } exit_req; \ + void* unused; /* TODO: retained for ABI compat; remove this in v2.x. */ \ + int exit_signal; \ + HANDLE wait_handle; \ + HANDLE process_handle; \ + volatile char exit_cb_pending; + +#define UV_FS_PRIVATE_FIELDS \ + struct uv__work work_req; \ + int flags; \ + DWORD sys_errno_; \ + union { \ + /* TODO: remove me in 0.9. */ \ + WCHAR* pathw; \ + int fd; \ + } file; \ + union { \ + struct { \ + int mode; \ + WCHAR* new_pathw; \ + int file_flags; \ + int fd_out; \ + unsigned int nbufs; \ + uv_buf_t* bufs; \ + int64_t offset; \ + uv_buf_t bufsml[4]; \ + } info; \ + struct { \ + double atime; \ + double mtime; \ + } time; \ + } fs; + +#define UV_WORK_PRIVATE_FIELDS \ + struct uv__work work_req; + +#define UV_FS_EVENT_PRIVATE_FIELDS \ + struct uv_fs_event_req_s { \ + UV_REQ_FIELDS \ + } req; \ + HANDLE dir_handle; \ + int req_pending; \ + uv_fs_event_cb cb; \ + WCHAR* filew; \ + WCHAR* short_filew; \ + WCHAR* dirw; \ + char* buffer; + +#define UV_SIGNAL_PRIVATE_FIELDS \ + RB_ENTRY(uv_signal_s) tree_entry; \ + struct uv_req_s signal_req; \ + unsigned long pending_signum; + +#ifndef F_OK +#define F_OK 0 +#endif +#ifndef R_OK +#define R_OK 4 +#endif +#ifndef W_OK +#define W_OK 2 +#endif +#ifndef X_OK +#define X_OK 1 +#endif + +/* fs open() flags supported on this platform: */ +#define UV_FS_O_APPEND _O_APPEND +#define UV_FS_O_CREAT _O_CREAT +#define UV_FS_O_EXCL _O_EXCL +#define UV_FS_O_FILEMAP 0x20000000 +#define UV_FS_O_RANDOM _O_RANDOM +#define UV_FS_O_RDONLY _O_RDONLY +#define UV_FS_O_RDWR _O_RDWR +#define UV_FS_O_SEQUENTIAL _O_SEQUENTIAL +#define UV_FS_O_SHORT_LIVED _O_SHORT_LIVED +#define UV_FS_O_TEMPORARY _O_TEMPORARY +#define UV_FS_O_TRUNC _O_TRUNC +#define UV_FS_O_WRONLY _O_WRONLY + +/* fs open() flags supported on other platforms (or mapped on this platform): */ +#define UV_FS_O_DIRECT 0x02000000 /* FILE_FLAG_NO_BUFFERING */ +#define UV_FS_O_DIRECTORY 0 +#define UV_FS_O_DSYNC 0x04000000 /* FILE_FLAG_WRITE_THROUGH */ +#define UV_FS_O_EXLOCK 0x10000000 /* EXCLUSIVE SHARING MODE */ +#define UV_FS_O_NOATIME 0 +#define UV_FS_O_NOCTTY 0 +#define UV_FS_O_NOFOLLOW 0 +#define UV_FS_O_NONBLOCK 0 +#define UV_FS_O_SYMLINK 0 +#define UV_FS_O_SYNC 0x08000000 /* FILE_FLAG_WRITE_THROUGH */ 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 93% rename from src/jsc/bindings/uv-posix-polyfills-darwin.c rename to src/jsc/bindings/uv-polyfills-darwin.c index 7497ae74bdf9..5cfe86bafcfe 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 diff --git a/src/jsc/bindings/uv-posix-polyfills-linux.c b/src/jsc/bindings/uv-polyfills-linux.c similarity index 97% rename from src/jsc/bindings/uv-posix-polyfills-linux.c rename to src/jsc/bindings/uv-polyfills-linux.c index 690c4a8439be..fdf39500e4b4 100644 --- a/src/jsc/bindings/uv-posix-polyfills-linux.c +++ b/src/jsc/bindings/uv-polyfills-linux.c @@ -1,5 +1,5 @@ -#include "uv-posix-polyfills.h" +#include "uv-polyfills.h" #include #include #include diff --git a/src/jsc/bindings/uv-posix-polyfills-posix.c b/src/jsc/bindings/uv-polyfills-posix.c similarity index 88% rename from src/jsc/bindings/uv-posix-polyfills-posix.c rename to src/jsc/bindings/uv-polyfills-posix.c index afaaf396df04..ce05be99b833 100644 --- a/src/jsc/bindings/uv-posix-polyfills-posix.c +++ b/src/jsc/bindings/uv-polyfills-posix.c @@ -1,4 +1,4 @@ -#include "uv-posix-polyfills.h" +#include "uv-polyfills.h" #include #include diff --git a/src/jsc/bindings/uv-polyfills-windows.c b/src/jsc/bindings/uv-polyfills-windows.c new file mode 100644 index 000000000000..7695c7911b51 --- /dev/null +++ b/src/jsc/bindings/uv-polyfills-windows.c @@ -0,0 +1,81 @@ +#include "uv-polyfills.h" + +uv_pid_t uv_os_getpid() +{ + return GetCurrentProcessId(); +} + +int32_t Bun__getParentProcessId(void); + +uv_pid_t uv_os_getppid() +{ + 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) +{ + static LARGE_INTEGER frequency; + LARGE_INTEGER counter; + + // QueryPerformanceFrequency always writes the same value, so racing + // first calls are harmless. + if (frequency.QuadPart == 0) + 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; +} + +// Copy-pasted from libuv +UV_EXTERN void uv_mutex_destroy(uv_mutex_t* mutex) +{ + DeleteCriticalSection(mutex); +} + +// Copy-pasted from libuv +UV_EXTERN int uv_mutex_init(uv_mutex_t* mutex) +{ + InitializeCriticalSection(mutex); + return 0; +} + +// Copy-pasted from libuv +UV_EXTERN int uv_mutex_init_recursive(uv_mutex_t* mutex) +{ + return uv_mutex_init(mutex); +} + +// Copy-pasted from libuv +UV_EXTERN void uv_mutex_lock(uv_mutex_t* mutex) +{ + EnterCriticalSection(mutex); +} + +// Copy-pasted from libuv +UV_EXTERN int uv_mutex_trylock(uv_mutex_t* mutex) +{ + if (TryEnterCriticalSection(mutex)) + return 0; + return UV_EBUSY; +} + +// Copy-pasted from libuv +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 91% rename from src/jsc/bindings/uv-posix-polyfills.c rename to src/jsc/bindings/uv-polyfills.c index 19ae7e5040c8..9f23c21ef30a 100644 --- a/src/jsc/bindings/uv-posix-polyfills.c +++ b/src/jsc/bindings/uv-polyfills.c @@ -1,6 +1,13 @@ -#include "uv-posix-polyfills.h" +#include "uv-polyfills.h" -#if OS(LINUX) || OS(DARWIN) || OS(FREEBSD) +void __bun_throw_not_implemented(const char* symbol_name) +{ + CrashHandler__unsupportedUVFunction(symbol_name); +} + +#if OS(WINDOWS) +#include "uv-polyfills-windows.c" +#else #include #include @@ -14,23 +21,18 @@ #define UV__ERR(x) (x) #endif -void __bun_throw_not_implemented(const char* symbol_name) -{ - CrashHandler__unsupportedUVFunction(symbol_name); -} - // Internals uint64_t uv__hrtime(uv_clocktype_t type); #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() diff --git a/src/jsc/bindings/uv-posix-polyfills.h b/src/jsc/bindings/uv-polyfills.h similarity index 81% rename from src/jsc/bindings/uv-posix-polyfills.h rename to src/jsc/bindings/uv-polyfills.h index a2f6fd160007..72a4f272c6cb 100644 --- a/src/jsc/bindings/uv-posix-polyfills.h +++ b/src/jsc/bindings/uv-polyfills.h @@ -4,15 +4,18 @@ #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 +// 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); // libuv headers will use UV_EXTERN +#if OS(WINDOWS) +// Exported through src/symbols.def. +#define UV_EXTERN +#else #define UV_EXTERN __attribute__((visibility("default"))) __attribute__((used)) +#endif #include @@ -20,5 +23,3 @@ 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-stubs.c similarity index 99% rename from src/jsc/bindings/uv-posix-stubs.c rename to src/jsc/bindings/uv-stubs.c index 04868a91cf9e..5c92b0f453a0 100644 --- a/src/jsc/bindings/uv-posix-stubs.c +++ b/src/jsc/bindings/uv-stubs.c @@ -1,7 +1,6 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -#include "uv-posix-polyfills.h" +#include "uv-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"); @@ -2117,4 +2116,3 @@ UV_EXTERN void uv_wtf8_to_utf16(const char* wtf8, __bun_throw_not_implemented("uv_wtf8_to_utf16"); __builtin_unreachable(); } -#endif 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 b94283911342..ab572b4b1c32 100644 --- a/src/jsc/bindings/webcore/JSWorker.cpp +++ b/src/jsc/bindings/webcore/JSWorker.cpp @@ -24,7 +24,7 @@ #include "ActiveDOMObject.h" #include "BunCPUProfiler.h" #if OS(WINDOWS) -#include +#include "BunWindowsProcess.h" #else #include #if defined(__APPLE__) @@ -885,10 +885,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..7cdb10ad1018 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,9 +35,11 @@ extern "C" int stat64( return _stat64(_FileName, _Stat); } +extern "C" int Bun__Process__kill(int pid, int signum); + extern "C" int kill(int pid, int sig) { - return uv_kill(pid, sig); + return Bun__Process__kill(pid, sig); } #endif diff --git a/src/jsc/bindings/wtf-bindings.cpp b/src/jsc/bindings/wtf-bindings.cpp index 4c6344fe0050..f2225c625e5a 100644 --- a/src/jsc/bindings/wtf-bindings.cpp +++ b/src/jsc/bindings/wtf-bindings.cpp @@ -13,8 +13,21 @@ 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(); + +// Exported through src/symbols.def. +extern "C" int uv_tty_reset_mode(void) +{ + Bun__Windows__resetConsoleMode(); + return 0; +} #endif #if !OS(WINDOWS) @@ -216,11 +229,17 @@ 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; + BunTTYState state; + memcpy(&state, rawState, sizeof(state)); + if (state.mode == mode) + return 0; + int rc = Bun__Windows__setConsoleMode(reinterpret_cast(_get_osfhandle(fd)), mode); + if (rc == 0) { + state.mode = mode; + memcpy(rawState, &state, sizeof(state)); + } + return rc; #endif } diff --git a/src/jsc/error.rs b/src/jsc/error.rs index 27bb7a4045c6..d17118854feb 100644 --- a/src/jsc/error.rs +++ b/src/jsc/error.rs @@ -63,7 +63,7 @@ pub enum Error { #[error(transparent)] Resolver(#[from] bun_resolver::Error), #[error(transparent)] - MakeLibUvOwned(#[from] bun_sys::MakeLibUvOwnedError), + MakeCrtOwned(#[from] bun_sys::MakeCrtOwnedError), #[error(transparent)] Path(#[from] bun_paths::path_options::Error), #[error(transparent)] @@ -121,7 +121,7 @@ impl Error { Self::Alloc(_) => "OutOfMemory", Self::Core(e) => e.name(), Self::Resolver(e) => e.name(), - Self::MakeLibUvOwned(e) => <&'static str>::from(e), + Self::MakeCrtOwned(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 e63b3583a000..1b84d23f065e 100644 --- a/src/jsc/event_loop.rs +++ b/src/jsc/event_loop.rs @@ -82,9 +82,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 @@ -102,15 +99,12 @@ 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 /// `enqueue()` reads go through the single audited `BackRef::deref` /// instead of an open-coded `NonNull::as_ref` `unsafe` at each site. pub signal_handler: Option>, - #[cfg(not(unix))] - pub signal_handler: (), } impl Default for EventLoop { @@ -126,19 +120,13 @@ 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, - #[cfg(not(unix))] - signal_handler: (), } } } @@ -431,9 +419,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(); } @@ -588,15 +573,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(); @@ -676,34 +658,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")); } } @@ -714,19 +685,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(); @@ -1046,7 +1004,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() { @@ -1210,8 +1167,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() }; } } @@ -1236,7 +1193,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(); @@ -1244,37 +1200,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. @@ -1286,7 +1219,7 @@ 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. + // timerfd used to. // SAFETY: as above — the tick runs loop callbacks that reach the loop // themselves, so the exclusive borrow is scoped to this call only. unsafe { @@ -1379,12 +1312,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( @@ -1401,11 +1328,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/lib.rs b/src/jsc/lib.rs index c665a01cf4c8..4b75d4f6d933 100644 --- a/src/jsc/lib.rs +++ b/src/jsc/lib.rs @@ -1238,10 +1238,7 @@ pub use self::event_loop::{ MiniEventLoop, PosixSignalHandle, 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::array_buffer::JSTypedArrayBytesDeallocator; diff --git a/src/jsc/modules/NodeConstantsModule.h b/src/jsc/modules/NodeConstantsModule.h index b9cf38a3416b..e75e93bbf689 100644 --- a/src/jsc/modules/NodeConstantsModule.h +++ b/src/jsc/modules/NodeConstantsModule.h @@ -50,6 +50,17 @@ #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" #else #include #endif diff --git a/src/jsc/modules/NodeTTYModule.cpp b/src/jsc/modules/NodeTTYModule.cpp index 9cc7b04803b2..236542ae6761 100644 --- a/src/jsc/modules/NodeTTYModule.cpp +++ b/src/jsc/modules/NodeTTYModule.cpp @@ -2,6 +2,11 @@ #include "NodeTTYModule.h" +#if OS(WINDOWS) +#include +#include +#endif + using namespace JSC; namespace Zig { @@ -20,13 +25,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 = reinterpret_cast(_get_osfhandle(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 01cffb13e12a..102c69cb6c71 100644 --- a/src/jsc/rare_data.rs +++ b/src/jsc/rare_data.rs @@ -995,7 +995,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, @@ -1014,7 +1014,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, @@ -1033,7 +1033,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 74c1fb6ecbc3..7d4e531603a4 100644 --- a/src/jsc/virtual_machine_exports.rs +++ b/src/jsc/virtual_machine_exports.rs @@ -11,9 +11,6 @@ use bun_event_loop::ManagedTask::ManagedTask; 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` / @@ -187,12 +184,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/libarchive/error.rs b/src/libarchive/error.rs index cb0a621e96f7..4993f4ff62bf 100644 --- a/src/libarchive/error.rs +++ b/src/libarchive/error.rs @@ -7,7 +7,7 @@ pub enum Error { #[error(transparent)] Alloc(#[from] bun_alloc::AllocError), #[error(transparent)] - MakeLibUvOwned(#[from] bun_sys::MakeLibUvOwnedError), + MakeCrtOwned(#[from] bun_sys::MakeCrtOwnedError), #[error(transparent)] Paths(#[from] bun_paths::Error), } @@ -19,7 +19,7 @@ impl Error { Self::Fail => "Fail", Self::Sys(e) => <&'static str>::from(e), Self::Alloc(_) => "OutOfMemory", - Self::MakeLibUvOwned(e) => <&'static str>::from(e), + Self::MakeCrtOwned(e) => <&'static str>::from(e), Self::Paths(e) => e.name(), } } diff --git a/src/libarchive/lib.rs b/src/libarchive/lib.rs index 76a215a4767e..120639d61865 100644 --- a/src/libarchive/lib.rs +++ b/src/libarchive/lib.rs @@ -1745,7 +1745,7 @@ impl Archiver { let guard = scopeguard::guard(file_handle_native, |fd| { fd.close(); }); - let owned = (*guard).make_lib_uv_owned()?; + let owned = (*guard).make_crt_owned()?; scopeguard::ScopeGuard::into_inner(guard); owned }; 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 4a2dccc2a86f..000000000000 --- a/src/libuv_sys/lib.rs +++ /dev/null @@ -1,47 +0,0 @@ -#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)] -#![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 5aa91608cadb..a134d98c9be5 100644 --- a/src/patch/lib.rs +++ b/src/patch/lib.rs @@ -1626,7 +1626,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]] = &[ @@ -1679,22 +1678,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() }; @@ -1750,7 +1739,6 @@ 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][..]; @@ -1811,23 +1799,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/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/runtime/Cargo.toml b/src/runtime/Cargo.toml index 386634671a62..bfb05f733306 100644 --- a/src/runtime/Cargo.toml +++ b/src/runtime/Cargo.toml @@ -78,6 +78,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 @@ -98,7 +99,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 1cf21b53fddf..6251c3bd5cea 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 3a3c08eef699..51cba927e12c 100644 --- a/src/runtime/api.rs +++ b/src/runtime/api.rs @@ -81,7 +81,7 @@ pub 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 mod bun_process; diff --git a/src/runtime/api/BunObject.rs b/src/runtime/api/BunObject.rs index d35e8556611b..20786f594f1f 100644 --- a/src/runtime/api/BunObject.rs +++ b/src/runtime/api/BunObject.rs @@ -2827,7 +2827,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, @@ -2867,7 +2867,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 b2c035649a4d..ed2126fbe2bc 100644 --- a/src/runtime/api/bun/Terminal.rs +++ b/src/runtime/api/bun/Terminal.rs @@ -29,8 +29,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; @@ -117,8 +115,8 @@ pub 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>, @@ -640,8 +638,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() @@ -766,11 +764,11 @@ impl Terminal { } 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. + // reader so onReaderDone fires (releasing the reader ref) instead + // of hanging on an EOF that will never come. Leak hpcon: calling + // ClosePseudoConsole here would deadlock, because the pipe HANDLE + // stays open until the loop has collected the cancelled read. + // Conhost sees broken-pipe once it has. let flags = self.flags.get(); if flags.contains(Flags::READER_STARTED) && !flags.contains(Flags::READER_DONE) { self.reader.with_mut(|r| r.close()); @@ -1079,8 +1077,8 @@ struct PseudoConsole { /// 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). +/// both on error. The "server" end is what the loop's completion port drives +/// 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. @@ -1098,7 +1096,7 @@ fn create_overlapped_pipe_pair( 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). + // (as the pipes spawn creates for a child's stdio are). let local = if windows::is_app_container() { r"LOCAL\" } else { @@ -1255,34 +1253,12 @@ fn create_pty_windows(cols: u16, rows: u16) -> Result 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); - } - }; + // The server (overlapped) ends go to BufferedReader/StreamingWriter.start() + // as they are. + let read_fd = Fd::from_system(out_server.take().unwrap()); + let write_fd = Fd::from_system(in_server.take().unwrap()); let result_hpcon = hpcon.take().unwrap(); - let read_fd = scopeguard::ScopeGuard::into_inner(read_fd_guard); Ok(PtyResult { master: Fd::INVALID, @@ -1977,14 +1953,7 @@ impl Terminal { } fn loop_(&self) -> *mut AsyncLoop { - #[cfg(windows)] - { - self.event_loop_handle.uv_loop() - } - #[cfg(not(windows))] - { - self.event_loop_handle.r#loop().cast() - } + self.event_loop_handle.r#loop().cast() } pub(crate) fn finalize(&self) { @@ -2030,9 +1999,6 @@ 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() } unsafe fn event_loop(this: *mut Self) -> bun_io::EventLoopHandle { @@ -2072,9 +2038,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 diff --git a/src/runtime/api/bun/js_bun_spawn_bindings.rs b/src/runtime/api/bun/js_bun_spawn_bindings.rs index 589e23e6f28b..4e8f7685b744 100644 --- a/src/runtime/api/bun/js_bun_spawn_bindings.rs +++ b/src/runtime/api/bun/js_bun_spawn_bindings.rs @@ -15,17 +15,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 +56,6 @@ fn subprocess_ipc_owner(ptr: *mut SubprocessT<'_>) -> 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; @@ -374,6 +362,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 @@ -591,7 +581,14 @@ fn spawn_maybe_sync( let mut stdio_iter = stdio_val.array_iterator(global_this)?; let mut i: i32 = 0; while let Some(value) = stdio_iter.next()? { - Stdio::extract(&mut stdio[i as usize], global_this, i, value, is_sync)?; + Stdio::extract( + &mut stdio[i as usize], + global_this, + i, + value, + is_sync, + &mut overlapped_stdio[i as usize], + )?; if i == 2 { break; } @@ -603,7 +600,15 @@ 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, global_this, i, value, is_sync)?; + // A pipe at fd 3 and above is overlapped for the child either way. + Stdio::extract( + &mut new_item, + global_this, + i, + value, + is_sync, + &mut false, + )?; let opt = match new_item.as_spawn_option(i) { stdio::ResultT::Result(opt) => opt, @@ -611,11 +616,7 @@ fn spawn_maybe_sync( return Err(e.throw_js(global_this)); } }; - #[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); @@ -628,15 +629,36 @@ fn spawn_maybe_sync( } } else { if let Some(value) = args.get(global_this, "stdin")? { - Stdio::extract(&mut stdio[0], global_this, 0, value, is_sync)?; + Stdio::extract( + &mut stdio[0], + global_this, + 0, + value, + is_sync, + &mut overlapped_stdio[0], + )?; } if let Some(value) = args.get(global_this, "stderr")? { - Stdio::extract(&mut stdio[2], global_this, 2, value, is_sync)?; + Stdio::extract( + &mut stdio[2], + global_this, + 2, + value, + is_sync, + &mut overlapped_stdio[2], + )?; } if let Some(value) = args.get(global_this, "stdout")? { - Stdio::extract(&mut stdio[1], global_this, 1, value, is_sync)?; + Stdio::extract( + &mut stdio[1], + global_this, + 1, + value, + is_sync, + &mut overlapped_stdio[1], + )?; } } @@ -1050,11 +1072,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(global_this)); }; sync_loop.prepare(jsc_vm_ptr.cast()); @@ -1096,7 +1113,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). @@ -1127,9 +1144,8 @@ fn spawn_maybe_sync( // 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); } } @@ -1154,7 +1170,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()), @@ -1170,12 +1186,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`). @@ -1202,16 +1212,12 @@ fn spawn_maybe_sync( .throw_value(SystemError::from(systemerror).to_error_instance(global_this))); } Err(err) => { - // See EMFILE arm above. - spawn_options.deinit(); return Err( global_this.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 @@ -1252,7 +1258,7 @@ fn spawn_maybe_sync( // Use the isolated loop for spawnSync operations // - // Note: `PosixSpawnResult::to_process` consumes `self` but only reads + // Note: `SpawnResult::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 // explicit. @@ -1262,8 +1268,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 @@ -1323,8 +1328,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> = @@ -1344,17 +1347,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( @@ -1374,44 +1366,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); @@ -1517,7 +1483,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, ); @@ -1533,6 +1499,17 @@ fn spawn_maybe_sync( } } + #[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, + ))); + } + } + if let Some(ipc_data) = subprocess.ipc() { #[cfg(unix)] { @@ -1549,42 +1526,22 @@ fn spawn_maybe_sync( v[usize::try_from(ipc_channel).expect("int cast")] = ExtraPipe::Unavailable; }); } - #[cfg(not(unix))] + #[cfg(windows)] { - use crate::node::MaybeExt as _; - 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 Some(err) = - unsafe { IPC::SendQueue::windows_configure_server(ipc_data.as_ctx_ptr(), ipc_pipe) } - .as_err() - { + // 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. + if let Err(err) = unsafe { + IPC::SendQueue::open_pipe(ipc_data.as_ctx_ptr(), loop_handle.loop_(), ipc_fd, true) + } { let err_js = err.to_js(global_this); subprocess.deref(); return Err(global_this.throw_value(err_js)); } + // The channel owns the pipe end 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(global_this); } @@ -1737,7 +1694,6 @@ 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(global_this.throw_value(err.to_js(global_this))); @@ -1753,7 +1709,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 { @@ -2063,7 +2018,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/spawn/stdio.rs b/src/runtime/api/bun/spawn/stdio.rs index 787aec56fc7b..52d2ec39d8ae 100644 --- a/src/runtime/api/bun/spawn/stdio.rs +++ b/src/runtime/api/bun/spawn/stdio.rs @@ -1,8 +1,6 @@ #[cfg(any(target_os = "linux", target_os = "android"))] use bun_collections::VecExt; use bun_jsc::{self as jsc, JSGlobalObject, JSValue, JsResult}; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; use bun_sys::{self as sys, Fd, FdExt as _}; // `bun.jsc.WebCore` lives in this crate (not `bun_jsc`); alias so the body can @@ -15,13 +13,7 @@ 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; +pub(crate) type SpawnOptionsStdio = process::Stdio; // `bun.FD.Stdio` (the StdIn/StdOut/StdErr tag enum) is `bun_core::Stdio`, // re-exported through `bun_sys`. @@ -207,30 +199,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); @@ -278,25 +247,17 @@ impl Stdio { return ResultT::Err(ToSpawnOptsError::BlobUsedAsOut); } - buffer() + SpawnOptionsStdio::Buffer } Self::Dup2(d) => SpawnOptionsStdio::Dup2(ProcessDup2 { out: d.out, to: d.to, }), - Self::Capture(_) | Self::Pipe | Self::ReadableStream(_) => buffer(), - #[cfg(not(windows))] + Self::Capture(_) | Self::Pipe | Self::ReadableStream(_) => SpawnOptionsStdio::Buffer, 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::Ipc => SpawnOptionsStdio::Ipc, Self::Fd(fd) => SpawnOptionsStdio::Pipe(*fd), - #[cfg(not(windows))] Self::Memfd(fd) => SpawnOptionsStdio::Pipe(*fd), - #[cfg(windows)] - Self::Memfd(_) => panic!("This should never happen"), Self::Path(pathlike) => { SpawnOptionsStdio::Path(pathlike.slice().to_vec().into_boxed_slice()) } @@ -309,7 +270,6 @@ impl Stdio { pub(crate) fn is_piped(&self) -> bool { match self { Self::Capture(_) | Self::Blob(_) | Self::Pipe | Self::ReadableStream(_) => true, - Self::Ipc => cfg!(windows), _ => false, } } @@ -407,6 +367,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(()); @@ -425,8 +388,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(global.throw_invalid_arguments(format_args!( @@ -452,8 +418,8 @@ impl Stdio { } 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(global.throw_invalid_arguments(format_args!( "file descriptor must be a positive integer" @@ -677,15 +643,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 7a7d71701ba8..cc055ea2aa2d 100644 --- a/src/runtime/api/bun/subprocess.rs +++ b/src/runtime/api/bun/subprocess.rs @@ -12,7 +12,6 @@ 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}; @@ -21,17 +20,11 @@ use enumset::{EnumSet, EnumSetType}; // 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 mod resource_usage; @@ -78,9 +71,6 @@ pub mod js { /// Platform-dependent stdio result type. pub use bun_spawn::subprocess::StdioResult; -#[cfg(windows)] -type StdioPipeItem = StdioResult; -#[cfg(not(windows))] type StdioPipeItem = ExtraPipe; pub type StaticPipeWriter<'a> = NewStaticPipeWriter>; @@ -293,18 +283,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: StdioResult) { + if let Some(fd) = result { + debug_assert!(fd.is_valid()); + } } -pub(crate) use assert_stdio_result; impl Subprocess<'_> { #[bun_uws::uws_callback(thunk = "on_abort_signal_c")] @@ -366,18 +350,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); @@ -857,64 +834,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 { @@ -1259,26 +1197,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()); } @@ -1508,58 +1433,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 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. @@ -1591,8 +1473,8 @@ pub 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)] { diff --git a/src/runtime/api/bun/subprocess/Readable.rs b/src/runtime/api/bun/subprocess/Readable.rs index fc8d8eb81a4a..8473c0349482 100644 --- a/src/runtime/api/bun/subprocess/Readable.rs +++ b/src/runtime/api/bun/subprocess/Readable.rs @@ -92,7 +92,7 @@ impl Readable { max_size: Option>, _is_sync: bool, ) -> Readable { - super::assert_stdio_result!(result); + super::assert_stdio_result(result); let mut stdio = stdio; #[cfg(unix)] @@ -105,17 +105,7 @@ impl Readable { 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), Stdio::Memfd(_) => { // Ownership of the fd moves into the Readable; `Stdio`'s Drop would close it. let memfd = stdio.take_memfd().unwrap(); @@ -129,17 +119,7 @@ impl Readable { Readable::Ignore } } - 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)) } @@ -183,24 +163,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 67d2ce72d04d..d2dd3de065ac 100644 --- a/src/runtime/api/bun/subprocess/SubprocessPipeReader.rs +++ b/src/runtime/api/bun/subprocess/SubprocessPipeReader.rs @@ -6,12 +6,11 @@ use bun_io::BufferedReader; 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_jsc::event_loop::EventLoop; use bun_jsc::{JSGlobalObject, JSValue, JsResult}; use bun_ptr::{ParentRef, RefCount, RefPtr}; -use bun_sys; +use bun_sys::{self, FdExt as _}; use super::readable::Readable; use super::{StdioKind, StdioResult, Subprocess}; @@ -117,16 +116,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 { @@ -153,75 +142,46 @@ 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(PosixFlags::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); + 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; } - // 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 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); } } @@ -372,16 +332,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 d83a4325df22..b7fcd4668862 100644 --- a/src/runtime/api/bun/subprocess/Writable.rs +++ b/src/runtime/api/bun/subprocess/Writable.rs @@ -9,8 +9,6 @@ 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}; @@ -124,7 +122,7 @@ impl<'a> Writable<'a> { result: StdioResult, promise_for_stream: &mut JSValue, ) -> crate::Result> { - super::assert_stdio_result!(result); + super::assert_stdio_result(result); let global = event_loop.global_ref(); @@ -139,93 +137,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::Memfd(_) | 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) { @@ -233,7 +144,6 @@ impl<'a> Writable<'a> { } } - #[cfg(not(windows))] match stdio { Stdio::Dup2(_) => panic!("TODO dup2 stdio"), Stdio::Pipe | Stdio::ReadableStream(_) => { @@ -255,6 +165,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); @@ -315,7 +226,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 ae57dd6065f1..35381c263055 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) { @@ -2044,57 +2040,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()) @@ -2102,14 +2063,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)] { @@ -2146,34 +2104,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/bake/DevServer.rs b/src/runtime/bake/DevServer.rs index f26d5e23b697..c19b7d7f7c64 100644 --- a/src/runtime/bake/DevServer.rs +++ b/src/runtime/bake/DevServer.rs @@ -797,8 +797,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 098d8c73ceaa..eb82d4745bc2 100644 --- a/src/runtime/bin_entry/mod.rs +++ b/src/runtime/bin_entry/mod.rs @@ -231,19 +231,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 cb138cd59077..e86c2028100a 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 1abf11f77f11..e6bb2dc65175 100644 --- a/src/runtime/cli/create/SourceFileProjectGenerator.rs +++ b/src/runtime/cli/create/SourceFileProjectGenerator.rs @@ -190,13 +190,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, @@ -366,13 +359,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, @@ -428,13 +414,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 64529652c988..7811a90c94f4 100644 --- a/src/runtime/cli/create_command.rs +++ b/src/runtime/cli/create_command.rs @@ -113,17 +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,27 @@ 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 }; + #[cfg(unix)] + let _ = sys::set_nonblocking(fd); 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. + use bun_sys::FdExt as _; + fd.close(); + return Err(err.into()); + } } handle.process = Some(ProcessInfo { @@ -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() } } } @@ -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() }, diff --git a/src/runtime/cli/link_command.rs b/src/runtime/cli/link_command.rs index 37e454a1ce58..2257076535ac 100644 --- a/src/runtime/cli/link_command.rs +++ b/src/runtime/cli/link_command.rs @@ -180,11 +180,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 +197,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 5b8a75ebb39a..6b38ba2d62d3 100644 --- a/src/runtime/cli/multi_run.rs +++ b/src/runtime/cli/multi_run.rs @@ -19,10 +19,9 @@ 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, + self as spawn, Rusage, SpawnOptions, SpawnResult, Status, event_loop_handle_to_ctx, }; use bun_collections::index_sort; use bun_dotenv::Loader as DotEnvLoader; @@ -98,7 +97,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 +155,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 +177,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,57 +191,21 @@ 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)); + for (fd, reader) in [ + (stdout_fd, &mut self.stdout_reader.reader), + (stderr_fd, &mut self.stderr_reader.reader), + ] { + let Some(fd) = fd else { continue }; + #[cfg(unix)] + let _ = bun_sys::set_nonblocking(fd); + if let Err(err) = reader.start(fd, true) { + // A reader that fails to start (Windows only) has not taken the fd. + use bun_sys::FdExt as _; + fd.close(); + return Err(Error::from(err)); } } - #[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)?; - } - 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 { process, status: Status::Running, @@ -898,7 +852,8 @@ pub(crate) fn run(ctx: &mut Command::ContextData) -> Result 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() }, diff --git a/src/runtime/cli/open.rs b/src/runtime/cli/open.rs index 8c12a8c6154e..4f2255b9b320 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 c523a546df1d..51f0ce88ba52 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..739a335869b7 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, @@ -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/run_command.rs b/src/runtime/cli/run_command.rs index c0a303130aa4..5ea13ed05344 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) => { @@ -1578,7 +1567,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. @@ -2102,17 +2091,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, @@ -2777,12 +2755,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 @@ -2954,8 +2926,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 85e4d83e0bf7..2a44f12539c9 100644 --- a/src/runtime/cli/test/ChangedFilesFilter.rs +++ b/src/runtime/cli/test/ChangedFilesFilter.rs @@ -26,8 +26,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; @@ -591,15 +589,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 eff60a48b09f..6a39db35367d 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, ReadEvent}; 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 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,43 +149,23 @@ impl Channel { } } -// -- Windows (uv.Pipe) ------------------------------------------------------- +// -- Windows (pipe) ---------------------------------------------------------- #[cfg(windows)] +#[derive(Default)] pub 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). + /// socketpair end. Windows: an end of the pipe spawn made for fd 3; + /// `inherited` says it is the worker's (this process did not create it), + /// which decides how a pipe may be driven there. Takes `fd` either way. // 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 @@ -200,57 +174,61 @@ impl Channel { // promotion; the raw-pointer route sidesteps that lint while keeping both // call sites (which pass `&`/`&mut` and coerce) unchanged. /// `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, + vm: *const VirtualMachine, + 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, vm, fd, inherited) } - fn adopt_impl(&self, this: *mut Self, _vm: *const VirtualMachine, fd: Fd) -> bool { + fn adopt_impl( + &self, + this: *mut Self, + _vm: *const VirtualMachine, + 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 opened = if inherited { + Pipe::open_foreign(loop_, fd, true) + } else { + Pipe::open_owned(loop_, fd, 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 +245,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 +277,65 @@ 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, self.root.get(), Some(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::Err(_) => { + self_.backend.pipe.set(None); + self_.mark_done(); + } } } @@ -419,7 +345,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))] { @@ -434,7 +360,7 @@ impl Channel { } #[cfg(windows)] { - return !self.backend.inflight.get().is_empty(); + return self.backend.writing.get(); } #[cfg(not(windows))] { @@ -546,15 +472,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); @@ -648,68 +566,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 529f414881d3..8d343163002c 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); } } @@ -838,8 +838,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); @@ -903,7 +903,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; } diff --git a/src/runtime/cli/test/parallel/Worker.rs b/src/runtime/cli/test/parallel/Worker.rs index 3a1f27772e26..7d2475f100d6 100644 --- a/src/runtime/cli/test/parallel/Worker.rs +++ b/src/runtime/cli/test/parallel/Worker.rs @@ -5,16 +5,9 @@ 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; use super::channel::{Channel, ChannelOwner}; @@ -41,7 +34,7 @@ pub 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. @@ -117,171 +110,64 @@ impl Worker { this.err = WorkerPipe::new(self_ptr); }); - #[cfg(unix)] + // `.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(), + )), + ); { - // `.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); + let worker: &mut Worker = &mut **this; + for (fd, pipe) in [(stdout, &mut worker.out), (stderr, &mut worker.err)] { + let Some(fd) = fd else { continue }; + if pipe.reader.start(fd, true).is_err() { + // A reader that fails to start (Windows only) has not taken the fd. + use bun_sys::FdExt as _; + fd.close(); + return Err(crate::Error::PipeStartFailed); } - } 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"); - let _ = raw; - } - } - 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)) - } - .map_err(|_| crate::Error::PipeStartFailed)?; - } - 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) { + 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(), 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(); @@ -290,11 +176,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()); } } } @@ -436,7 +321,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 437e44caf391..3172d894b87e 100644 --- a/src/runtime/cli/test/parallel/runner.rs +++ b/src/runtime/cli/test/parallel/runner.rs @@ -466,7 +466,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 struct WorkerCommands { pub(crate) channel: Channel, /// Coordinator dispatches one `.run` and waits for `.file_done` before @@ -514,7 +514,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, vm, Fd::from_crt(3), true) { bun_core::pretty_errorln!("error: test worker failed to adopt IPC fd"); Global::exit(1); } diff --git a/src/runtime/cli/upgrade_command.rs b/src/runtime/cli/upgrade_command.rs index ac8bcf2d8c72..61b3957d92eb 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 @@ -844,8 +833,6 @@ impl UpgradeCommand { stdin: spawn_sync::SyncStdio::Inherit, stdout: spawn_sync::SyncStdio::Inherit, stderr: spawn_sync::SyncStdio::Inherit, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }) { Ok(Ok(r)) => r, @@ -947,8 +934,6 @@ impl UpgradeCommand { stderr: spawn_sync::SyncStdio::Inherit, stdout: spawn_sync::SyncStdio::Inherit, stdin: spawn_sync::SyncStdio::Inherit, - #[cfg(windows)] - windows: spawn_windows_options(), ..Default::default() }); let spawn_res = match spawn_res { @@ -992,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 @@ -1329,8 +1312,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 afae984518da..65dc23fbc20c 100644 --- a/src/runtime/dispatch.rs +++ b/src/runtime/dispatch.rs @@ -28,10 +28,6 @@ pub mod js2native; use bun_event_loop::ManagedTask::ManagedTask; 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::{ @@ -43,25 +39,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 { @@ -410,19 +380,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 { - $(task_tag::$tag => cast!(fs_async::$ty).run_from_js_thread()?,)* - // 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), @@ -589,7 +546,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 == 61, + task_tag::COUNT == 53, "dispatch::run_task / release_task_unrun arm count out of sync with bun_event_loop::task_tag", ); @@ -639,10 +596,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. @@ -653,6 +613,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`. @@ -664,6 +625,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| { @@ -680,6 +642,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 @@ -687,6 +650,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. @@ -694,10 +658,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)). @@ -710,15 +676,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. @@ -732,6 +703,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")] { @@ -743,8 +715,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` @@ -758,6 +732,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"), } } @@ -1294,39 +1270,12 @@ 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::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 578980ff4721..d66c9f865020 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; + /// `c_ares::Error::init_eai` reads `UV_EAI_*` codes on Windows. + pub(crate) const EAI_NONAME: core::ffi::c_int = bun_errno::uv_codes::UV_EAI_NONAME; } type SockaddrStorage = netc::sockaddr_storage; type AddrInfo = netc::addrinfo; @@ -99,7 +93,6 @@ fn js_event_loop_ctx() -> Async::EventLoopCtx { Async::posix_event_loop::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::*; @@ -177,141 +169,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(); - } - } - - 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, - ) -> 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); - 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, - 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 // ────────────────────────────────────────────────────────────────────────── @@ -924,7 +781,6 @@ pub mod get_addr_info_request { use super::*; /// The blocking `getaddrinfo` of one libc-backend lookup, run on the pool. - #[cfg(not(windows))] pub struct LibcLookup { pub(crate) backend: LibcBackend, } @@ -934,12 +790,9 @@ pub 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 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(); @@ -964,7 +817,6 @@ pub mod get_addr_info_request { } } - #[cfg(not(windows))] impl bun_jsc::JobContext for LibcLookup { type OffThread = Self; type Js = LibcRequest; @@ -1031,15 +883,15 @@ pub mod get_addr_info_request { } } - /// Non-Windows libc backend (worker-thread blocking getaddrinfo). - #[cfg(not(windows))] + /// The libc backend (worker-thread blocking getaddrinfo). pub enum LibcBackend { Success(GetAddrInfoResultList), + /// An `EAI_*` code; on Windows the `UV_EAI_*` spelling, which is what + /// `c_ares::Error::init_eai` reads there. Err(i32), Query(GetAddrInfo), } - #[cfg(not(windows))] impl LibcBackend { pub(crate) fn run(&mut self) { let LibcBackend::Query(query) = self else { @@ -1064,7 +916,15 @@ pub mod get_addr_info_request { // SAFETY: hostname[copied_len] == 0 let host = ZStr::from_buf(&hostname[..], copied_len); let debug_timer = Output::DebugTimer::start(); + #[cfg(windows)] + let err = windows_get_addr_info( + host.as_bytes(), + port_z.as_bytes(), + hints.as_ref(), + &raw mut addrinfo, + ); // SAFETY: FFI; all pointers valid for the call duration + #[cfg(not(windows))] let err = unsafe { libc::getaddrinfo( host.as_ptr().cast::(), @@ -1095,8 +955,16 @@ pub 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.cast()) + }; + // SAFETY: as above. + #[cfg(not(windows))] + unsafe { + bun_dns::freeaddrinfo(a) + } }); // SAFETY: addrinfo is non-null (checked above); freed by `_free` guard after copy. @@ -1104,19 +972,83 @@ pub mod get_addr_info_request { } } - /// Windows libc backend wraps a uv_getaddrinfo_t. #[cfg(windows)] - pub 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), returning the `UV_EAI_*` code libuv's + /// `uv__getaddrinfo_translate_error` gives for its result: those numbers are + /// the `errno` JS sees. #[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 { + use bun_errno::uv_codes as uv; + const WSAEINVAL: c_int = 10022; + const WSAEAFNOSUPPORT: c_int = 10047; + const WSAESOCKTNOSUPPORT: c_int = 10044; + const WSATYPE_NOT_FOUND: c_int = 10109; + const WSAHOST_NOT_FOUND: c_int = 11001; + const WSATRY_AGAIN: c_int = 11002; + const WSANO_RECOVERY: c_int = 11003; + const WSA_NOT_ENOUGH_MEMORY: c_int = 8; + + // SAFETY: no preconditions. + unsafe { 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 uv::UV_EAI_NONAME; + }; + host_buf[host_len] = 0; + // 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. + let err = unsafe { + GetAddrInfoW( + host_buf.as_ptr(), + service_buf.as_ptr(), + hints.map_or(ptr::null(), std::ptr::from_ref), + result, + ) + }; + match err { + 0 => 0, + WSATRY_AGAIN => uv::UV_EAI_AGAIN, + WSAEINVAL => uv::UV_EAI_BADFLAGS, + WSANO_RECOVERY => uv::UV_EAI_FAIL, + WSAEAFNOSUPPORT => uv::UV_EAI_FAMILY, + WSA_NOT_ENOUGH_MEMORY => uv::UV_EAI_MEMORY, + WSAHOST_NOT_FOUND => uv::UV_EAI_NONAME, + WSATYPE_NOT_FOUND => uv::UV_EAI_SERVICE, + WSAESOCKTNOSUPPORT => uv::UV_EAI_SOCKTYPE, + other => bun_errno::Bun__translateWin32ErrorToUV(other as u32), } } + pub enum Backend { CAres, #[cfg(target_os = "macos")] @@ -1132,13 +1064,6 @@ pub 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!(), - } - } } } @@ -1260,7 +1185,6 @@ impl GetAddrInfoRequest { } } - #[cfg(not(windows))] /// # Safety /// `this` must be the live heap `GetAddrInfoRequest` whose `run` already /// completed; consumed (freed) on every path. @@ -1357,59 +1281,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(); - #[cfg(windows)] - 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`. @@ -1886,7 +1757,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(self, promise: &mut JSPromiseStrong, global: &JSGlobalObject) { @@ -1941,32 +1812,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). /// @@ -2620,8 +2466,7 @@ pub mod internal { 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. + // ws2_32!freeaddrinfo. // SAFETY: `info` is non-null (checked above) and owned by getaddrinfo. unsafe { bun_dns::freeaddrinfo(info.cast()) }; Some(res) @@ -2671,7 +2516,7 @@ pub mod internal { #[cfg(windows)] 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; @@ -3671,12 +3516,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 @@ -3722,35 +3562,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 enum CacheHit { Inflight(*mut get_addr_info_request::PendingCacheKey), // BORROW_FIELD into resolver buffer @@ -4731,58 +4542,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 @@ -4791,13 +4551,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; @@ -4805,11 +4565,20 @@ impl Resolver { let _guard = self.ref_guard(); + // A poll that failed or hung up reports no direction. c-ares learns + // what happened from the socket itself, so it gets both (what Node's + // cares_wrap does); with neither it would only run its timeouts and + // keep a socket nothing polls any more. + 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 @@ -4825,139 +4594,75 @@ 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(); - // 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 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 !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; } - #[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); - } + 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 { + // 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); + } + + // 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); } } } @@ -5235,26 +4940,13 @@ impl Resolver { GetAddrInfoBackend::CAres => { self.c_ares_lookup_with_normalized_name(&query, global_this)? } - GetAddrInfoBackend::Libc => { - #[cfg(windows)] - { - lib_uv_backend::lookup(self, query, global_this)? - } - #[cfg(not(windows))] - { - lib_c::lookup(self, &query, global_this) - } - } + GetAddrInfoBackend::Libc => lib_c::lookup(self, &query, global_this), GetAddrInfoBackend::System => { #[cfg(target_os = "macos")] { dns_sd::lookup(self, &query, global_this) } - #[cfg(windows)] - { - lib_uv_backend::lookup(self, query, global_this)? - } - #[cfg(all(not(target_os = "macos"), not(windows)))] + #[cfg(not(target_os = "macos"))] { lib_c::lookup(self, &query, global_this) } diff --git a/src/runtime/dns_jsc/mod.rs b/src/runtime/dns_jsc/mod.rs index 30b2d4fb5898..20d7e7a99cb5 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 7f167d870b91..fcf2c711d041 100644 --- a/src/runtime/image/Image.rs +++ b/src/runtime/image/Image.rs @@ -1233,7 +1233,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 @@ -1545,15 +1545,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/ipc.rs b/src/runtime/ipc.rs index 4e63b88cf10b..ff5bcad53507 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, ReadEvent, 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 _}; use bun_uws; // `bun.cpp.*` — generated C++ dispatch shims for IPC handle (de)serialization @@ -837,40 +831,25 @@ 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 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 struct WindowsState { - pub(crate) is_server: bool, - /// 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), } -#[cfg(not(windows))] -#[derive(Default)] -pub struct WindowsState {} - #[derive(Copy, Clone, Eq, PartialEq)] enum CloseReason { Normal, @@ -922,7 +901,8 @@ pub struct SendQueue { pub(crate) write_in_progress: Cell, pub close_event_sent: Cell, - pub windows: JsCell, + #[cfg(windows)] + incoming_frames: JsCell, } #[derive(Copy, Clone)] @@ -991,7 +971,7 @@ pub enum SendQueueOwnerKind { } #[cfg(windows)] -pub type SocketType = *mut uv::Pipe; +pub type SocketType = Pipe; #[cfg(not(windows))] pub type SocketType = Socket; @@ -1045,7 +1025,8 @@ impl SendQueue { pending_after_close: Cell::new(false), write_in_progress: Cell::new(false), close_event_sent: Cell::new(false), - windows: JsCell::new(WindowsState::default()), + #[cfg(windows)] + incoming_frames: JsCell::new(IncomingFrames::default()), }); this.root.set(Some(this.as_non_null())); this @@ -1067,10 +1048,6 @@ impl SendQueue { } pub 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() } @@ -1082,43 +1059,26 @@ 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); + #[cfg(windows)] + { + let _ = reason; + // Closing the pipe cancels a write the kernel still holds, and the + // peer would lose a message this side already reported as sent. + 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); + return; } } - let _ = reason; // suppress unused on windows + #[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, + }); + } + // On Windows, replacing the `Open` value drops the pipe, which closes it. + self.socket_closed_notify(from != CloseFrom::Deinit); } fn socket_closed(&self) { @@ -1127,18 +1087,6 @@ impl SendQueue { 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); @@ -1210,38 +1158,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 fn close_socket_next_tick(&self, next_tick: bool) { log!("SendQueue#closeSocketNextTick"); if !self.socket_is_open() { @@ -1670,91 +1586,50 @@ impl SendQueue { } } - fn get_socket(&self) -> Option { - match *self.socket.get() { - SocketUnion::Open(s) => Some(s), - _ => None, - } - } - #[cfg(windows)] pub 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 _onWriteComplete immediately. on windows, it + /// calls _onWriteComplete 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, self.root_ptr(), Some(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..]; @@ -1769,179 +1644,142 @@ 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. `created_here` + /// says whose pipe end it is: the overlapped end spawn made for a child, or + /// 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 unsafe fn windows_configure_server( + pub unsafe fn open_pipe( this: *mut Self, - ipc_pipe: *mut uv::Pipe, + loop_: *mut bun_io::Loop, + fd: Fd, + created_here: bool, ) -> 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), - ); - self_.windows.with_mut(|w| w.is_server = true); - // 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 = if created_here { + Pipe::open_owned(loop_, fd, true)? + } else { + Pipe::open_foreign(loop_, fd, 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), - ); - self_.windows.with_mut(|w| w.is_server = false); - - // 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 => { + 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(..) { + match event { + FrameEvent::Data(range) if this.socket_is_open() => on_data2(this, &chunk[range]), + FrameEvent::Data(_) => {} + // 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(); - let _ = data; - // 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); } } @@ -2038,6 +1876,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)] { @@ -2050,7 +1936,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()) } } @@ -2115,10 +2001,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 { @@ -2145,9 +2028,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 { @@ -2196,10 +2076,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) @@ -2218,9 +2095,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); @@ -2477,101 +2351,6 @@ pub mod IPCHandlers { send_queue.close_socket(CloseReason::Failure, CloseFrom::User); } } - - #[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); - }); - - // 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), - } - } - } - } - } - } } #[track_caller] diff --git a/src/runtime/ipc_host.rs b/src/runtime/ipc_host.rs index 0d72ba74c0c8..d0e19b44f86b 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)] @@ -189,6 +192,7 @@ pub(crate) fn do_send( zig_handle = Some(Handle::init(fd, handle)); } } + #[cfg(windows)] crate::socket::listener::ListenerType::NamedPipe(_named_pipe) => {} crate::socket::listener::ListenerType::None => {} } @@ -373,8 +377,7 @@ fn Bun__Process__send(global: &JSGlobalObject, frame: &CallFrame) -> JsResult 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 ─────────────────────────────────── @@ -1099,20 +1091,20 @@ unsafe fn auto_tick(vm: *mut VirtualMachine) { unsafe { (*loop_).tick_without_idle() }; } } - - #[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()) }; + #[cfg(windows)] + // SAFETY: per fn contract — `vm` is the live per-thread VM. + if unsafe { (*vm).is_main_thread } { + crate::node::windows_signal::check_console_size_after_wake(); } - #[cfg(not(unix))] - let _ = state; + + // 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() }; @@ -1139,25 +1131,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(); @@ -1226,15 +1210,15 @@ unsafe fn auto_tick_active(vm: *mut VirtualMachine) { unsafe { (*loop_).tick_without_idle() }; } } - - #[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()) }; + #[cfg(windows)] + // SAFETY: per fn contract — `vm` is the live per-thread VM. + if unsafe { (*vm).is_main_thread } { + crate::node::windows_signal::check_console_size_after_wake(); } - #[cfg(not(unix))] - let _ = state; + + // 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() }; @@ -1545,7 +1529,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 +1644,6 @@ unsafe fn cancel_all_timers(vm: *mut VirtualMachine) { } } -/// `RuntimeHooks::close_timer_loop_handles_after_vm_destroyed`: teardown-only companion of -/// `cancel_all_timers` (which the `--isolate` swap also uses on a live VM). -/// -/// # Safety -/// `runtime_state()` is installed; JS thread; the JSC VM is already destroyed. -unsafe fn close_timer_loop_handles_after_vm_destroyed(_vm: *mut VirtualMachine) { - #[cfg(windows)] - { - let state = runtime_state(); - debug_assert!(!state.is_null()); - // SAFETY: live boxed per-thread RuntimeState (fn contract). - unsafe { (*state).timer.close_loop_handles_for_vm_teardown() }; - } -} - /// `RuntimeHooks::stop_active_handles_for_vm_teardown` — see [`stop_active_handles_for_vm_teardown`]. /// /// # Safety @@ -1713,8 +1681,6 @@ fn stop_dns_for_vm_teardown() -> 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 79aee0a7709a..bddc1fdb5c9b 100644 --- a/src/runtime/napi/napi_body.rs +++ b/src/runtime/napi/napi_body.rs @@ -2273,9 +2273,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)] @@ -2283,20 +2280,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() } diff --git a/src/runtime/node.rs b/src/runtime/node.rs index 492a6264ec05..8aafe3ad59a3 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. @@ -86,15 +86,6 @@ pub 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 mod path_watcher; -#[cfg(windows)] -#[path = "node/win_watcher.rs"] -pub 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 mod memory_pressure; #[path = "node/node_fs_binding.rs"] @@ -103,9 +94,11 @@ pub mod node_fs_binding; pub mod node_fs_stat_watcher; #[path = "node/node_fs_watcher.rs"] pub mod node_fs_watcher; +#[path = "node/path_watcher.rs"] +pub mod path_watcher; #[cfg(windows)] -#[path = "node/uv_signal_handle_windows.rs"] -pub mod uv_signal_handle_windows; +#[path = "node/windows_signal.rs"] +pub 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 @@ -160,15 +153,8 @@ pub mod zlib { // ─── submodule re-exports ───────────────────────────────────────────────── -#[cfg(unix)] -pub type uid_t = libc::uid_t; -#[cfg(not(unix))] -pub type uid_t = bun_sys::windows::libuv::uv_uid_t; - -#[cfg(unix)] -pub type gid_t = libc::gid_t; -#[cfg(not(unix))] -pub type gid_t = bun_sys::windows::libuv::uv_gid_t; +pub type uid_t = u32; +pub type gid_t = u32; /// Node.js expects the error to include contextual information /// - "syscall" diff --git a/src/runtime/node/Stat.rs b/src/runtime/node/Stat.rs index b2ce2375b57b..2c433b1bbca4 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 use bun_sys::PosixStat; /// Stats and BigIntStats classes from node:fs. `BIG` selects BigIntStats vs Stats. @@ -24,15 +23,8 @@ impl StatType { Self { value: *stat_ } } - /// Matches Node's `static_cast` of stat times: 32-bit wrap on win32, signed-preserving elsewhere. #[inline] fn timespec_parts(ts: StatTimespec) -> (i64, i64) { - #[cfg(windows)] - return ( - ((ts.sec as i32) as u32) as i64, - ((ts.nsec as i32) as u32) as i64, - ); - #[cfg(not(windows))] (ts.sec, ts.nsec) } diff --git a/src/runtime/node/StatFS.rs b/src/runtime/node/StatFS.rs index daf0254f27d8..32c9dc457a51 100644 --- a/src/runtime/node/StatFS.rs +++ b/src/runtime/node/StatFS.rs @@ -1,13 +1,9 @@ //! 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 +// `libc::statfs` on POSIX, `bun_sys::windows::fs::StatFS` on Windows. 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 { diff --git a/src/runtime/node/node_cluster_binding.rs b/src/runtime/node/node_cluster_binding.rs index 43052c1e6c7b..78d0504329cb 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 = { @@ -387,13 +387,9 @@ 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 - })); + return Ok(JSValue::js_number_from_int32( + bun_errno::Bun__translateWin32ErrorToUV(err as u32), + )); } 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 b36e7e2779e5..18b7c4abfea1 100644 --- a/src/runtime/node/node_fs.rs +++ b/src/runtime/node/node_fs.rs @@ -34,8 +34,6 @@ use bun_threading::work_pool::{IntrusiveWorkTask as _, Task as WorkPoolTask, Wor pub trait MaybeSysResultExt: Sized { fn get_errno(&self) -> E; #[cfg(not(windows))] - fn errno_sys(rc: Rc, syscall: sys::Tag) -> Option; - #[cfg(not(windows))] fn errno_sys_fd(rc: Rc, syscall: sys::Tag, fd: FD) -> Option; #[cfg(not(windows))] fn errno_sys_p( @@ -61,18 +59,6 @@ impl MaybeSysResultExt for Maybe { } #[cfg(not(windows))] #[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() - })), - } - } - #[cfg(not(windows))] - #[inline] fn errno_sys_fd(rc: Rc, syscall: sys::Tag, fd: FD) -> Option { match sys::get_errno(rc) { E::SUCCESS => None, @@ -122,17 +108,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. @@ -221,25 +196,15 @@ 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}, -}; +use bun_sys::windows; -// 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; // 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"))))] @@ -247,47 +212,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 // @@ -416,9 +340,8 @@ const CLONE_NOFOLLOW: u32 = 0x0001; 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). +/// `&ZStr`, so these are pure forwarders to the byte-path entry points. +/// Windows is handled by `#[cfg(windows)]` branches at the call sites. #[cfg(not(windows))] #[inline] fn mkdir_os_path(path: &OSPathSliceZ, mode: Mode) -> Maybe<()> { @@ -480,7 +403,7 @@ 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 { @@ -500,7 +423,7 @@ mod _async_tasks { AsyncFSTask, { NodeFSFunctionEnum::Chmod }>; pub(crate) type Chown = AsyncFSTask, { NodeFSFunctionEnum::Chown }>; - pub(crate) type Close = UVFSRequest; + pub(crate) type Close = AsyncFSTask; pub(crate) type CopyFile = AsyncFSTask, { NodeFSFunctionEnum::CopyFile }>; pub(crate) type Exists = @@ -532,15 +455,15 @@ mod _async_tasks { pub(crate) type Mkdtemp = AsyncFSTask, { NodeFSFunctionEnum::Mkdtemp }>; pub(crate) type Open = - UVFSRequest, { NodeFSFunctionEnum::Open }>; - pub(crate) type Read = UVFSRequest; + AsyncFSTask, { NodeFSFunctionEnum::Open }>; + pub(crate) type Read = AsyncFSTask; 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 Readv = AsyncFSTask; pub(crate) type Realpath = AsyncFSTask, { NodeFSFunctionEnum::Realpath }>; pub(crate) type RealpathNonNative = AsyncFSTask< @@ -564,427 +487,19 @@ mod _async_tasks { pub(crate) type Utimes = AsyncFSTask, { NodeFSFunctionEnum::Utimes }>; pub(crate) type Write = - UVFSRequest, { NodeFSFunctionEnum::Write }>; + AsyncFSTask, { NodeFSFunctionEnum::Write }>; pub(crate) type WriteFile = AsyncFSTask< ret::WriteFile, args::WriteFile<'static>, { NodeFSFunctionEnum::WriteFile }, >; pub(crate) type Writev = - UVFSRequest; + AsyncFSTask; pub(crate) type Statfs = - UVFSRequest, { NodeFSFunctionEnum::Statfs }>; + AsyncFSTask, { NodeFSFunctionEnum::Statfs }>; const _: () = assert!(ReadFile::HAVE_ABORT_SIGNAL); const _: () = assert!(WriteFile::HAVE_ABORT_SIGNAL); - - #[cfg(windows)] - /// Used internally. Not from JavaScript. - pub 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 type UVFSRequest = AsyncFSTask; - - #[cfg(windows)] - pub struct UVFSRequest { - pub(crate) promise: JSPromiseStrong, - 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, - } - - #[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() - } - - pub(crate) fn create( - global_object: &JSGlobalObject, - binding: &Binding, - task_args: ThreadIsolated, - vm: &mut VirtualMachine, - ) -> JSValue { - let task = Box::new(Self { - promise: JSPromiseStrong::init(global_object), - 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(global_object), - req: bun_core::ffi::zeroed(), - r#ref: KeepAlive::default(), - tracker: AsyncTaskTracker::init(vm), - }); - // Transfer ownership to libuv: the box outlives the async request and is - // reclaimed in `destroy()` (run_from_js_thread → scopeguard). `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()); - let _ = vm; - task.tracker.did_schedule(global_object); - - 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.promise.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.promise.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)); - } - - pub(crate) fn run_from_js_thread(&mut self) -> JsResult<()> { - // SAFETY: self was Box::leak'd in create(); destroy() runs exactly once on scope exit - let _deinit = - scopeguard::guard(core::ptr::from_mut(self), |p| unsafe { Self::destroy(p) }); - // Move `result` out so the `global_object()` `&self` borrow can coexist - // with consuming it below; the sentinel left behind is dropped in `destroy()`. - let result = core::mem::replace(&mut self.result, Err(sys::Error::default())); - let global_object = self.global_object(); - let success = matches!(result, Ok(_)); - let promise_value = self.promise.value(); - let promise = self.promise.get(); - let result = match result { - Err(err) => match err.to_js_with_async_stack(global_object, promise) { - Ok(v) => v, - Err(e) => { - return promise.reject(global_object, Err(e)); - } - }, - Ok(res) => match FsReturn::fs_to_js(res, global_object) { - Ok(v) => v, - Err(e) => { - return promise.reject(global_object, Err(e)); - } - }, - }; - promise_value.ensure_still_alive(); - - let _dispatch = self.tracker.dispatch(global_object); - - if success { - promise.resolve(global_object, result)?; - } else { - promise.reject(global_object, Ok(result))?; - } - Ok(()) - } - - /// 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) }; - // `bun_sys::Error` frees its path on Drop. - task.r#ref.unref(bun_io::js_vm_ctx()); - } } // ────────────────────────────────────────────────────────────────────────── @@ -1191,23 +706,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) } - } - } - /// 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 struct AsyncFSTask { @@ -2595,7 +2093,7 @@ mod _async_tasks { pub use _async_tasks::{ AsyncCpTask, AsyncFSTask, AsyncReaddirRecursiveTask, CpSingleTask, FsArgument, FsReturn, IntoResultListEntry, NewAsyncCpTask, ResultListEntry, ResultListEntryValue, ShellAsyncCpTask, - UVFSRequest, async_, + async_, }; // ────────────────────────────────────────────────────────────────────────── @@ -2663,8 +2161,7 @@ pub 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 struct FdVectorIo { pub(crate) fd: FD, pub(crate) buffers: VectorArrayBuffer, @@ -2742,7 +2239,7 @@ pub 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", @@ -2755,7 +2252,7 @@ pub 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", @@ -2780,7 +2277,7 @@ pub 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", @@ -2793,7 +2290,7 @@ pub 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", @@ -2805,18 +2302,9 @@ pub 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>; @@ -4512,18 +4000,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))] @@ -5092,24 +4568,20 @@ 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] { - 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() - }); - } - } - 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()); + // 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() + }; + let mut dest_buf = paths::path_buffer_pool::get(); + let Ok(src) = args.src.os_path_kernel32(&mut self.sync_error_buf) else { + return Err(too_long(&args.src)); + }; + let Ok(dest) = args.dest.os_path_kernel32(&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( @@ -5167,34 +4639,12 @@ 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); + Syscall::chown(path, args.uid, args.gid) } 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) { Err(err) => Err(err.with_path(args.path.slice())), Ok(_) => Ok(()), @@ -5247,21 +4697,7 @@ impl NodeFS { } 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(())) - } + Syscall::fsync(args.fd) } pub(crate) fn ftruncate(&mut self, args: &args::FTruncate, _: Flavor) -> Maybe { @@ -5269,30 +4705,7 @@ impl NodeFS { } 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 Syscall::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; @@ -5330,8 +4743,7 @@ 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 `Syscall::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) { Err(err) => Err(err.with_path(args.path.slice())), @@ -5725,23 +5137,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 Syscall::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))] @@ -5779,35 +5178,6 @@ impl NodeFS { } } - #[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)) - } - fn read_inner(&mut self, args: &args::Read) -> Maybe { debug_assert!(args.position.is_none()); // `ArrayBuffer` is a `Copy` descriptor over JSC-owned heap bytes; copy the @@ -5861,30 +5231,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 }); @@ -5915,34 +5261,6 @@ 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()); @@ -6779,7 +6097,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); @@ -6829,19 +6147,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(); @@ -7178,7 +6483,6 @@ impl NodeFS { } let mut buf = args.data.slice(); - #[cfg(not(windows))] let mut written: usize = 0; // Attempt to pre-allocate large files @@ -7229,10 +6533,7 @@ impl NodeFS { } Ok(amt) => { buf = &buf[amt..]; - #[cfg(not(windows))] - { - written += amt; - } + written += amt; if amt == 0 { break; } @@ -7250,14 +6551,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 _ = Syscall::ftruncate(fd, (written as u64 & ((1u64 << 63) - 1)) as i64); } if let Some(err) = write_err { @@ -7265,14 +6559,7 @@ impl NodeFS { } if args.flush { - #[cfg(windows)] - { - let _ = unsafe { windows::kernel32::FlushFileBuffers(fd.native()) }; - } - #[cfg(not(windows))] - { - let _ = Syscall::fsync(fd); - } + let _ = Syscall::fsync(fd); } Ok(()) @@ -7290,9 +6577,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, @@ -7347,38 +6631,14 @@ impl NodeFS { ) -> Maybe { #[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 mut outbuf = bun_paths::path_buffer_pool::get(); + let mut buf = + match Syscall::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]; } @@ -7598,8 +6858,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, @@ -7683,15 +6941,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 => Syscall::symlink(processed_target, new_path), + ResolvedLinkType::Dir => Syscall::symlink_dir(processed_target, new_path), + ResolvedLinkType::Junction => Syscall::junction(processed_target, new_path), + } { Err(err) => { Err(err.with_path_dest(args.target_path.slice(), args.new_path.slice())) } @@ -7764,21 +7019,10 @@ impl NodeFS { } 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), - }; + 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), } - // 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( @@ -7817,29 +7061,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( 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)), @@ -7848,29 +7073,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( 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)), @@ -8186,9 +7392,8 @@ impl NodeFS { #[cfg_attr(any(windows, target_os = "macos"), allow(dead_code))] 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) => { @@ -8866,36 +8071,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` @@ -8904,31 +8079,16 @@ impl NodeFS { pub struct Op; /// Per-`F` binding from `(R, A)` to its `NodeFS` method. Every -/// `AsyncFSTask` / `UVFSRequest` instantiation in +/// `AsyncFSTask` instantiation in /// `async_::*` has exactly one impl, so the `where Op<{F}>: NodeFSDispatch` /// bound is always satisfied at every monomorphised call site. pub 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 }> { @@ -8936,20 +8096,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) - } - )? } )+ }; @@ -8960,7 +8106,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; @@ -8977,26 +8123,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)] @@ -9408,7 +8554,7 @@ fn dt_delete_dir(parent: &sys::Dir, name: &[u8]) -> Result<(), E> { #[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. + let flags = 0x200; // AT_REMOVEDIR, as `bun_sys::unlinkat_with_flags` defines it on Windows. match Syscall::unlinkat_with_flags(parent.fd, z, flags) { Ok(()) => Ok(()), Err(e) => Err(e.get_errno()), @@ -9853,26 +8999,6 @@ pub 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 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 2c511397f5ca..77f8911214bd 100644 --- a/src/runtime/node/node_fs_binding.rs +++ b/src/runtime/node/node_fs_binding.rs @@ -53,10 +53,7 @@ where /// `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. +/// `create_task` is `async_::::create`. fn run_async( this: &Binding, global: &JSGlobalObject, @@ -383,7 +380,7 @@ pub(crate) fn string_to_flags_for_testing( // 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()); + let bits = bun_sys::windows::O::from_bun_o(flags.as_int()); #[cfg(not(windows))] let bits = flags.as_int(); Ok(JSValue::js_number_from_int32(bits)) diff --git a/src/runtime/node/node_fs_watcher.rs b/src/runtime/node/node_fs_watcher.rs index d421793a0b91..eb8b86eb262b 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; @@ -28,10 +26,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 @@ -47,12 +42,9 @@ pub 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, @@ -66,8 +58,7 @@ pub 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, @@ -84,15 +75,6 @@ pub 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. @@ -102,7 +84,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, @@ -127,16 +108,7 @@ impl FSWatcher { } } -#[cfg(windows)] -pub type FSWatchTask = FSWatchTaskWindows; -#[cfg(not(windows))] -pub 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 struct FSWatchTaskPosix { +pub 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. @@ -147,8 +119,7 @@ pub 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 @@ -163,14 +134,12 @@ impl Taskable for FSWatchTaskPosix { } } -#[cfg(not(windows))] pub 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. @@ -239,7 +208,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( @@ -285,9 +254,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 @@ -309,9 +277,6 @@ impl FSWatchTaskPosix { } } -#[cfg(windows)] -pub type EventPathString = StringOrBytesToDecode; -#[cfg(not(windows))] pub type EventPathString = Box<[u8]>; /// The kind of change a watcher backend reports for a path, before it becomes a JS event. @@ -342,8 +307,7 @@ pub 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. NoFilename(WatchEventKind), Abort, } @@ -367,159 +331,6 @@ unsafe extern "C" { safe fn Bun__domEventNameToJS(global: &JSGlobalObject, event_type: EventType) -> JSValue; } -#[cfg(windows)] -pub 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(); - } - } -} - -#[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, - } - } -} - -pub 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. -impl From<&[u8]> for StringOrBytesToDecode { - #[inline] - fn from(bytes: &[u8]) -> Self { - StringOrBytesToDecode::BytesToFree(Box::<[u8]>::from(bytes)) - } -} - -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`. /// @@ -536,8 +347,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 { @@ -562,57 +372,15 @@ 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)); - let _ = is_file; - } - - #[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) const ON_PATH_UPDATE: fn(Option<*mut c_void>, Event, bool) = Self::on_path_update; 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()); } } @@ -1073,7 +841,7 @@ impl FSWatcher { } 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`. @@ -1142,9 +910,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, @@ -1172,12 +938,6 @@ 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, @@ -1223,8 +983,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 207ec3f71c16..326d26893ae6 100644 --- a/src/runtime/node/node_os.rs +++ b/src/runtime/node/node_os.rs @@ -38,14 +38,162 @@ 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 only this file uses, and the C++ exports + /// (`OsBinding.cpp`) behind `os.cpus()` / `os.networkInterfaces()`. + #[cfg(windows)] + #[allow(non_snake_case, non_camel_case_types, clippy::upper_case_acronyms)] + mod win32 { + use bun_sys::windows::ws2_32::{sockaddr_in, sockaddr_in6}; + use bun_sys::windows::{BOOL, DWORD, HANDLE}; + use core::ffi::{c_char, c_int, c_void}; + + pub(super) const TOKEN_READ: DWORD = 0x0002_0008; + /// `(HKEY)(ULONG_PTR)((LONG)0x80000002)`: a predefined handle (the + /// `LONG` sign-extends), not a pointer. + pub(super) const HKEY_LOCAL_MACHINE: *mut c_void = + 0x8000_0002_u32 as i32 as isize as *mut c_void; + 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, + } + + #[repr(C)] + pub(super) struct OSVERSIONINFOW { + pub(super) dwOSVersionInfoSize: DWORD, + pub(super) dwMajorVersion: DWORD, + pub(super) dwMinorVersion: DWORD, + pub(super) dwBuildNumber: DWORD, + pub(super) dwPlatformId: DWORD, + pub(super) szCSDVersion: [u16; 128], + } + + #[link(name = "kernel32")] + unsafe extern "system" { + pub(super) fn GetEnvironmentVariableW( + name: *const u16, + buffer: *mut u16, + size: DWORD, + ) -> DWORD; + pub(super) safe fn GetTickCount64() -> u64; + 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; + } + #[link(name = "advapi32")] + unsafe extern "system" { + pub(super) fn OpenProcessToken( + process: HANDLE, + access: DWORD, + token: *mut HANDLE, + ) -> BOOL; + pub(super) fn RegGetValueW( + key: *mut c_void, + sub_key: *const u16, + value: *const u16, + flags: DWORD, + value_type: *mut DWORD, + data: *mut c_void, + data_size: *mut DWORD, + ) -> i32; + } + #[link(name = "ntdll")] + unsafe extern "system" { + pub(super) fn RtlGetVersion(info: *mut OSVERSIONINFOW) -> i32; + } + + /// `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: CpuTimes, + } + #[repr(C)] + pub(super) struct CpuTimes { + pub(super) user: u64, + pub(super) nice: u64, + pub(super) sys: u64, + pub(super) idle: u64, + pub(super) irq: u64, + } + + /// `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" { + /// Both 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, + ); + } + + /// `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], + }; + // SAFETY: `info` is a valid out-pointer with its size field set. + unsafe { RtlGetVersion(&mut info) }; + info + } + } + // ─── local shims for upstream API gaps (Phase D) ────────────────────────── /// Unified error for `cpus_impl_*` so `?` works on both `JsResult` and @@ -584,22 +732,22 @@ 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() { @@ -612,7 +760,7 @@ mod _impl { }; 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, @@ -645,7 +793,7 @@ mod _impl { #[cfg(not(windows))] errno: -(bun_sys::posix::E::ESRCH as c_int), #[cfg(windows)] - errno: libuv::UV_ESRCH, + errno: bun_errno::uv_codes::UV_ESRCH, syscall: BunString::static_("uv_os_getpriority"), ..Default::default() }; @@ -658,15 +806,10 @@ 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 match homedir_windows() { + Ok(home) => Ok(home), + Err(err) => Err(global.throw_value(err.to_js(global))), + }; } #[cfg(not(windows))] { @@ -757,12 +900,70 @@ 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)); + } + // `size` counts the terminating NUL. + Ok(BunString::clone_utf16( + &buf[..(size as usize).saturating_sub(1)], + )) + } + 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() }; + // SAFETY: idempotent Winsock init. + unsafe { 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); @@ -1143,10 +1344,10 @@ mod _impl { #[cfg(windows)] pub 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(), @@ -1159,8 +1360,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); @@ -1168,7 +1369,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 { @@ -1301,7 +1502,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 @@ -1338,17 +1539,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) @@ -1367,7 +1568,7 @@ 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); #[cfg(not(windows))] @@ -1418,28 +1619,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"))] { @@ -1512,6 +1715,12 @@ mod _impl { Ok(result) } + #[cfg(windows)] + pub(crate) fn version() -> JsResult { + Ok(version_windows()) + } + + #[cfg(not(windows))] pub(crate) fn version() -> JsResult { let mut name_buffer = [0u8; HOST_NAME_MAX]; @@ -1530,21 +1739,54 @@ 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)] + fn version_windows() -> BunString { + let info = win32::os_version(); + let mut version = [0u16; 256 + 1 + 128]; + let mut len: usize = 0; + + let mut size = (256 * 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[..256]).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[9] = 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(); + } + + BunString::clone_utf16(&version[..len]) } } // mod _impl pub use _impl::*; diff --git a/src/runtime/node/node_process.rs b/src/runtime/node/node_process.rs index 619f925b98d3..a9a0aa5e027c 100644 --- a/src/runtime/node/node_process.rs +++ b/src/runtime/node/node_process.rs @@ -99,6 +99,20 @@ 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) => bun_errno::uv_codes::e_discriminant_to_uv(err.errno) + .unwrap_or_else(|| core::ffi::c_int::from(err.errno).wrapping_neg()), + } +} + /// `--redirect-warnings=` value; `Dead` when unset. #[unsafe(no_mangle)] pub(crate) extern "C" fn Bun__Node__getRedirectWarnings() -> bun_core::String { @@ -180,7 +194,7 @@ mod _impl { // 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 a91afd43132e..d61fb78d9a58 100644 --- a/src/runtime/node/path_watcher.rs +++ b/src/runtime/node/path_watcher.rs @@ -1,4 +1,4 @@ -//! 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 @@ -8,14 +8,14 @@ //! lock-ordering workarounds, a WorkPool directory crawler, and a bolted-on FSEvents //! side-channel. //! -//! 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,17 +26,26 @@ use core::cell::{Cell, UnsafeCell}; use core::ffi::c_void; -#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows +))] use core::sync::atomic::{AtomicBool, Ordering}; #[cfg(any(target_os = "linux", target_os = "android"))] use bun_collections::HashMap; use bun_collections::{ArrayHashMap, StringArrayHashMap}; -#[cfg(not(windows))] use bun_core::ZBox; -#[cfg(any(target_os = "linux", target_os = "android"))] +#[cfg(any(target_os = "linux", target_os = "android", windows))] use bun_core::strings; -#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows +))] use bun_core::{Output, zstr}; use bun_core::{ZStr, handle_oom}; use bun_paths as path; @@ -44,11 +53,15 @@ 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(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows +))] use bun_sys::FdExt; use bun_sys::{self as sys, E, Fd, Tag}; use bun_threading::Mutex; -#[cfg(not(windows))] use bun_wyhash::hash; use bun_jsc::VirtualMachineRef as VirtualMachine; @@ -61,7 +74,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 @@ -97,15 +110,26 @@ 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(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows + ))] 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(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows + ))] running: AtomicBool, /// Monotonic kevent generation counter (FreeBSD). Bumped under `mutex`. @@ -133,9 +157,19 @@ 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(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows + ))] platform_fd: Cell::new(Fd::INVALID), - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows + ))] running: AtomicBool::new(true), #[cfg(target_os = "freebsd")] next_gen: Cell::new(1), @@ -191,22 +225,25 @@ pub struct PathWatcher { manager: Option<&'static PathWatcherManager>, /// Canonical absolute path (realpath of the user-supplied path). Owned. - #[cfg(not(windows))] path: ZBox, - #[cfg(not(windows))] recursive: bool, - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows + ))] 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). - #[cfg(not(windows))] + /// Per-platform per-watch state (inotify wds, kqueue fds, the + /// FSEventsWatcher, or the directory handle's change request). platform: PlatformWatch, } @@ -215,22 +252,17 @@ pub 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`. #[derive(Default)] pub(crate) struct ChangeEvent { - #[cfg(not(windows))] hash: Cell, - #[cfg(not(windows))] event_type: Cell, - #[cfg(not(windows))] timestamp: Cell, } -#[cfg(not(windows))] impl ChangeEvent { fn should_emit(&self, hash: u64, timestamp: i64, event_type: WatchEventKind) -> bool { let time_diff = timestamp - self.timestamp.get(); @@ -258,10 +290,9 @@ 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. - #[cfg(not(windows))] fn emit(&self, event_type: WatchEventKind, rel_path: &[u8], is_file: bool) { let timestamp = bun_core::time::milli_timestamp(); let h = hash(rel_path); @@ -288,10 +319,11 @@ impl PathWatcher { } } - /// 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)( @@ -302,7 +334,6 @@ impl PathWatcher { } } - #[cfg(not(windows))] fn emit_error(&self, err: &sys::Error, close: bool) { for &ctx in self.handlers.keys() { (FSWatcher::ON_PATH_UPDATE)( @@ -318,7 +349,6 @@ impl PathWatcher { /// Signals end-of-batch so `FSWatcher` can flush its queued events to the JS thread. /// Caller holds `manager.mutex`. - #[cfg(not(windows))] fn flush(&self) { for &ctx in self.handlers.keys() { FSWatcher::on_update_end(Some(ctx)); @@ -435,41 +465,8 @@ pub(crate) fn watch( 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); @@ -485,19 +482,21 @@ pub(crate) fn watch( return Ok(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), - #[cfg(not(windows))] path: ZBox::from_bytes(resolved.as_bytes()), - #[cfg(not(windows))] recursive, - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] + #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + windows + ))] is_file, handlers: ArrayHashMap::default(), - #[cfg(not(windows))] platform: PlatformWatch::default(), }); // SAFETY: watcher just allocated; we hold the only reference. @@ -577,6 +576,49 @@ 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); + let resolved: &ZStr = match sys::get_fd_path(probe_fd, 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. + ZStr::from_buf(&resolve_buf[..], len) + } + }; + Ok((resolved, is_file)) +} + // ──────────────────────────────────────────────────────────────────────────────── // Platform backends // ──────────────────────────────────────────────────────────────────────────────── @@ -659,6 +701,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"); @@ -1630,5 +1677,636 @@ 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)); + let resolved: &ZStr = match sys::get_fd_path(Fd::from_system(handle), 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. + ZStr::from_buf(&resolve_buf[..], len) + } + }; + Ok((resolved, is_file)) +} + +#[cfg(windows)] +mod windows_impl { + use super::*; + use bun_sys::windows as w; + use bun_sys::windows::{BOOL, DWORD, HANDLE, OVERLAPPED}; + + /// The largest buffer `ReadDirectoryChangesW` accepts for a directory on a + /// network share. + const BUFFER_SIZE: usize = 64 * 1024; + + /// `OVERLAPPED.Internal` of a request that ended because its directory + /// handle was closed. Like an overflow it completes successfully with zero + /// bytes. + const STATUS_NOTIFY_CLEANUP: usize = 0x0000_010B; + + #[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; + + #[link(name = "kernel32")] + unsafe extern "system" { + fn GetShortPathNameW(long_path: *const u16, short_path: *mut u16, len: DWORD) -> DWORD; + fn GetLongPathNameW(short_path: *const u16, long_path: *mut u16, len: DWORD) -> DWORD; + fn GetFileInformationByHandleEx( + file: HANDLE, + class: u32, + info: *mut c_void, + size: DWORD, + ) -> BOOL; + fn CompareStringOrdinal( + a: *const u16, + a_len: i32, + b: *const u16, + b_len: i32, + ignore_case: BOOL, + ) -> i32; + } + const CSTR_EQUAL: i32 = 2; + + /// 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. + buffer: [u8; 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) + } + + /// 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, + }; + + // A drive root keeps its separator (`C:` alone is the drive's + // current directory). + 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); + + // 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 is written in place before `assume_init` + // (`buffer` is an output buffer; zeroed so no byte is uninit). + unsafe { + core::ptr::addr_of_mut!((*p).overlapped).write(bun_core::ffi::zeroed()); + core::ptr::addr_of_mut!((*p).buffer).write_bytes(0, 1); + 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(); + let mut full_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[..], + &mut full_buf[..], + ); + watcher.flush(); + } + manager.mutex.unlock(); + } + } + } + + 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], + full_buf: &mut [u16], + ) { + // SAFETY: caller contract; the kernel is done with `buffer`. + let request = unsafe { &*this }; + match error { + None if bytes > 0 => { + request.emit_records(watcher, bytes, name_buf, long_buf, full_buf) + } + None => { + if request.overlapped.Internal == STATUS_NOTIFY_CLEANUP { + return; + } + watcher.emit_overflow(); + } + Some(error) => { + 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: libuv reports a rename + // of the directory's full path, then the error from the + // request that can no longer be issued. + watcher.emit(WatchEventKind::Rename, watcher.path.as_bytes(), false); + } + } + // SAFETY: caller contract; no request is outstanding. + if let Err(error) = unsafe { DirRequest::issue(this) } { + watcher.emit_error(&sys::Error::from_win32(error, Tag::watch), true); + } + } + + 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], + full_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; + } + let name: &[u16] = bun_core::cast_slice::( + &self.buffer[name_start..name_start + name_len], + ); + + 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. + None if action != w::FILE_ACTION_REMOVED + && action != w::FILE_ACTION_RENAMED_OLD_NAME => + { + Some(self.long_name(name, long_buf, full_buf).unwrap_or(name)) + } + None => Some(name), + }; + if let Some(reported) = reported { + let n = strings::copy_utf16_into_utf8(&mut name_buf[..], reported).written + as usize; + watcher.emit(event_type, &name_buf[..n], 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. + fn long_name<'a>( + &self, + name: &[u16], + long_buf: &'a mut [u16], + full_buf: &mut [u16], + ) -> Option<&'a [u16]> { + let dir_len = self.dir_path.len(); + let full_len = dir_len + 1 + name.len(); + if full_len + 1 > full_buf.len() { + return None; + } + full_buf[..dir_len].copy_from_slice(&self.dir_path); + full_buf[dir_len] = u16::from(b'\\'); + full_buf[dir_len + 1..full_len].copy_from_slice(name); + full_buf[full_len] = 0; + // SAFETY: `full_buf` is NUL-terminated; `long_buf` is writable for its length. + let long_len = unsafe { + GetLongPathNameW( + full_buf.as_ptr(), + long_buf.as_mut_ptr(), + long_buf.len() as DWORD, + ) + } as usize; + if long_len == 0 || long_len >= long_buf.len() { + return None; + } + // The directory part keeps its length: `dir_path` is already long-form. + let long = &long_buf[..long_len]; + if long.len() <= dir_len + 1 || !eql_ignore_case(&long[..dir_len], &self.dir_path) { + return None; + } + Some(&long[dir_len + 1..]) + } + } +} diff --git a/src/runtime/node/time_like.rs b/src/runtime/node/time_like.rs index c90148985274..604b18802426 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 type TimeLike = f64; -#[cfg(not(windows))] -pub type TimeLike = libc::timespec; +pub 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; @@ -49,9 +44,20 @@ pub fn from_js(global_object: &JSGlobalObject, value: JSValue) -> JsResult` evaluated as a double, +/// which at today's dates rounds to 16 ticks. `utimes(p, 1713037251.36)` reads +/// back as 1713037251360 ms only with that rounding. #[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; + // `as` saturates on overflow/NaN. + let ticks = (seconds * TICKS_PER_S as f64 + UNIX_EPOCH_TICKS as f64) as i64 - UNIX_EPOCH_TICKS; + TimeLike { + sec: ticks.div_euclid(TICKS_PER_S), + nsec: ticks.rem_euclid(TICKS_PER_S) * 100, + } } #[cfg(not(windows))] @@ -65,33 +71,26 @@ 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 -} - -#[cfg(not(windows))] fn from_now() -> TimeLike { // Permissions requirements // To set both file timestamps to the current time (i.e., times is @@ -112,11 +111,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 aedf9cf262d7..a01d4e9152c4 100644 --- a/src/runtime/node/types.rs +++ b/src/runtime/node/types.rs @@ -877,6 +877,49 @@ pub(crate) trait PathOrFdExt { Self: Sized; } +/// Win32 resolves a relative path against the current directory and then +/// applies `MAX_PATH`, which only the `\\?\` form lifts. For a relative `path` +/// that long once resolved, writes it resolved, normalized and prefixed into +/// `buf`, NUL-terminated, and returns where it starts and its length. +#[cfg(windows)] +fn long_relative_path(path: &[u8], buf: &mut PathBuffer) -> Option<(usize, usize)> { + // `CreateDirectoryW`'s limit, the smallest of the `MAX_PATH`-derived ones. + const THRESHOLD: usize = 248; + // Rooted, UNC and device paths, and `C:` forms, are not resolved against the cwd alone. + if path.is_empty() || bun_paths::is_sep_any(path[0]) || (path.len() >= 2 && path[1] == b':') { + return None; + } + // SAFETY: a zero-length query writes nothing and returns the length of + // the current directory, NUL included. + let cwd_len = + unsafe { bun_sys::windows::kernel32::GetCurrentDirectoryW(0, core::ptr::null_mut()) } + as usize; + if path.len() + cwd_len < THRESHOLD || !strings::fits_in_wide_path_buffer(path) { + return None; + } + let mut cwd_buf = bun_paths::path_buffer_pool::get(); + let cwd = bun_sys::getcwd_z(&mut cwd_buf).ok()?.as_bytes(); + // Room in front of the joined path for `\\?\UNC\`. + const RESERVE: usize = 8; + let joined_len = bun_paths::resolve_path::join_abs_string_buf_checked::< + bun_paths::platform::Windows, + >(cwd, &mut buf[RESERVE..MAX_PATH_BYTES - 1], &[path])? + .len(); + let is_unc = bun_paths::is_sep_any(buf[RESERVE]) && bun_paths::is_sep_any(buf[RESERVE + 1]); + let (start, len) = if is_unc { + // `\\server\share\…` → `\\?\UNC\server\share\…` + let start = RESERVE + 1 - 7; + buf[start..start + 7].copy_from_slice(b"\\\\?\\UNC"); + (start, joined_len - 1 + 7) + } else { + let start = RESERVE - 4; + buf[start..RESERVE].copy_from_slice(&bun_sys::windows::LONG_PATH_PREFIX_U8); + (start, joined_len + 4) + }; + buf[start + len] = 0; + Some((start, 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 @@ -934,6 +977,9 @@ impl PathLikeExt for PathLike<'_> { // at `buf[len]`. return ZStr::from_buf(&buf[..], len); } + } else if let Some((start, len)) = long_relative_path(sliced, buf) { + // SAFETY: `long_relative_path` wrote the NUL at `buf[start + len]`. + return ZStr::from_buf(&buf[start..], len); } } @@ -1063,6 +1109,11 @@ impl PathLikeExt for PathLike<'_> { let buf_u16 = unsafe { bun_core::bytes_as_slice_mut::(&mut buf[..]) }; return Ok(strings::to_kernel32_path(buf_u16, normal)); } + if let Some((start, len)) = long_relative_path(s, &mut 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[start..start + len])); + } // 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]). @@ -1538,12 +1589,12 @@ impl FileSystemFlags { 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 + // use the platform's native MSVC 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)))); + return Ok(Some(FileSystemFlags(bun_sys::windows::O::to_bun_o(flags)))); } #[cfg(not(windows))] { @@ -1711,10 +1762,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 e24dc56b438f..000000000000 --- a/src/runtime/node/uv_signal_handle_windows.rs +++ /dev/null @@ -1,72 +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. - -#[cfg(windows)] -use core::ffi::c_int; - -#[cfg(windows)] -use bun_jsc::JSGlobalObject; -#[cfg(windows)] -use bun_sys::windows::libuv; - -#[cfg(windows)] -#[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 -} - -#[cfg(windows)] -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). -#[cfg(windows)] -#[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 fa2842f6519c..000000000000 --- a/src/runtime/node/win_watcher.rs +++ /dev/null @@ -1,550 +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 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> { - #[cfg(not(windows))] - compile_error!("win_watcher should only be used on Windows"); - - // 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..be7c5bb7e122 --- /dev/null +++ b/src/runtime/node/windows_signal.rs @@ -0,0 +1,175 @@ +//! 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. + +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); + safe fn GetTickCount64() -> u64; +} + +/// Bit `n` is set while signal `n`, one a console control event stands for, +/// has a listener. +static WATCHED: AtomicU32 = AtomicU32::new(0); + +/// Runs on a thread the system creates for each event. While the event's +/// signal has no listener it is left to the next handler, and in the end to +/// the default one, which terminates the process. +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 { + 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 +} + +/// First `process.on()` listener for `signum` on the main thread. +#[unsafe(no_mangle)] +pub extern "C" fn Bun__watchWindowsSignal(signum: c_int) { + match signum { + SIGINT | SIGBREAK | SIGHUP => { + static INSTALL: std::sync::Once = std::sync::Once::new(); + INSTALL.call_once(|| { + let _ = win::SetConsoleCtrlHandler(Some(on_console_ctrl), win::TRUE); + }); + WATCHED.fetch_or(1 << signum, Ordering::Release); + } + SIGWINCH => watch_console_size(), + _ => {} + } +} + +/// Last listener for `signum` removed. +#[unsafe(no_mangle)] +pub 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: columns of the screen buffer in the +/// high half, rows of the window 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; + } + CONSOLE_OUTPUT.store(console, Ordering::Release); + Some(console) +} + +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; + } + 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: the main thread after a wake-up, 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); + } +} + +/// Whether SIGWINCH has a listener and there is a console to compare. +static CONSOLE_SIZE_WATCHED: AtomicBool = AtomicBool::new(false); +/// `GetTickCount64` of the last comparison made after a wake-up. +static CONSOLE_SIZE_CHECKED_MS: AtomicU64 = AtomicU64::new(0); + +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)); + CONSOLE_SIZE_WATCHED.store(true, Ordering::Release); +} + +fn unwatch_console_size() { + CONSOLE_SIZE_WATCHED.store(false, Ordering::Release); + bun_io::windows::tty::set_resize_listener(None); +} + +/// The console reports a resize only to whoever is reading its input in raw +/// mode. For everyone else the main thread compares the size when its loop has +/// woken for something, at most every `INTERVAL_MS`; an idle process is not +/// woken for it. +pub(crate) fn check_console_size_after_wake() { + const INTERVAL_MS: u64 = 100; + if !CONSOLE_SIZE_WATCHED.load(Ordering::Relaxed) { + return; + } + let now = GetTickCount64(); + if now.wrapping_sub(CONSOLE_SIZE_CHECKED_MS.load(Ordering::Relaxed)) < INTERVAL_MS { + return; + } + CONSOLE_SIZE_CHECKED_MS.store(now, Ordering::Relaxed); + raise_if_console_resized(); +} diff --git a/src/runtime/server/DirectoryRoute.rs b/src/runtime/server/DirectoryRoute.rs index 7edc61e19be7..b4800ccfa880 100644 --- a/src/runtime/server/DirectoryRoute.rs +++ b/src/runtime/server/DirectoryRoute.rs @@ -294,24 +294,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..0cad17d03146 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, PosixFlags as ReaderFlags, ReadState}; use bun_jsc::JsCell; use bun_ptr::RefPtr; use bun_sys::{self as sys, Fd}; @@ -223,10 +221,14 @@ impl FileResponseStream { return; } + // 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)) && opts.pollable; + // 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::POLLABLE, pollable); reader .flags .set(ReaderFlags::NONBLOCKING, opts.file_type != FileType::File); @@ -243,9 +245,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); @@ -602,22 +604,12 @@ impl FileResponseStream { } 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() - } + 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; @@ -648,9 +640,6 @@ 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(), ()); } } diff --git a/src/runtime/server/FileRoute.rs b/src/runtime/server/FileRoute.rs index 126b57911eee..76ed724e2e89 100644 --- a/src/runtime/server/FileRoute.rs +++ b/src/runtime/server/FileRoute.rs @@ -257,25 +257,7 @@ 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 Ok(fd) = fd_result else { + let Ok(fd) = bun_sys::open_a(path, open_flags, 0) else { req.set_yield(true); route.on_response_complete(resp); return; @@ -288,9 +270,6 @@ 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, ()); route.on_response_complete(resp); } diff --git a/src/runtime/server/mod.rs b/src/runtime/server/mod.rs index b96201e5dce9..82da94c0c117 100644 --- a/src/runtime/server/mod.rs +++ b/src/runtime/server/mod.rs @@ -1712,8 +1712,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 44220d935e84..6798b561812c 100644 --- a/src/runtime/shell/Builtin.rs +++ b/src/runtime/shell/Builtin.rs @@ -429,7 +429,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(); @@ -603,34 +602,7 @@ 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, } }; @@ -647,12 +619,15 @@ impl Builtin { // 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. + // takes the pollable path. let redirect_writer = IOWriter::init( redirfd, io_writer::Flags { - pollable: if cfg!(windows) { true } else { pollable }, + #[cfg(not(windows))] + pollable, + #[cfg(not(windows))] nonblock: is_nonblocking, + #[cfg(not(windows))] is_socket, ..Default::default() }, diff --git a/src/runtime/shell/IO.rs b/src/runtime/shell/IO.rs index f5254fde114a..74d6c06a3a08 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 110d85cab602..8580fa3be9d1 100644 --- a/src/runtime/shell/IOReader.rs +++ b/src/runtime/shell/IOReader.rs @@ -41,15 +41,12 @@ pub(crate) type ReaderImpl = bun_io::BufferedReader; struct State { fd: Fd, - buf: Vec, 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`. 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). @@ -120,27 +117,23 @@ impl IOReader { } pub(crate) fn init(fd: Fd, evtloop: EventLoopHandle) -> std::sync::Arc { + #[cfg_attr(windows, allow(unused_mut))] let mut reader = ReaderImpl::init::(); + // On Windows the reader closes what it reads from: a HANDLE of its + // own (see `start_reader`). #[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 this = std::sync::Arc::new_cyclic(|w| IOReader { reader: UnsafeCell::new(reader), state: UnsafeCell::new(State { fd, - buf: Vec::new(), readers: Readers::new(), raw_err: None, evtloop, - #[cfg(windows)] - is_reading: false, self_weak: std::sync::Weak::clone(w), read_guards: Vec::new(), interp: None, @@ -178,9 +171,7 @@ 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 @@ -194,46 +185,45 @@ impl IOReader { 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. pub(crate) fn start(&self) -> Yield { + let r = self.reader(); #[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_registered(), + bun_io::pipes::PollOrFd::Fd(_) => true, + }; + // A source exists from the first read until EOF. #[cfg(windows)] - { - 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() { + let need_start = r.source.is_none(); + if need_start { + let fd = self.state().fd; + if let Err(e) = Self::start_reader(r, fd) { self.on_reader_error(&e); } - Yield::suspended() } + Yield::suspended() + } + + #[cfg(not(windows))] + fn start_reader(r: &mut ReaderImpl, fd: Fd) -> sys::Result<()> { + 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. What `fd` is (a file, + /// a pipe of either kind, the console) is for the reader to find out. + #[cfg(windows)] + fn start_reader(r: &mut ReaderImpl, fd: Fd) -> sys::Result<()> { + use bun_sys::FdExt as _; + let own = sys::dup(fd)?; + let started = r.start(own, false); + if started.is_err() { + own.close(); + } + started } /// Only adds if not already present. @@ -259,7 +249,6 @@ impl IOReader { // `state()` accesses (and `run_yield`'s re-read of `interp`) see live // memory. 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 @@ -279,24 +268,10 @@ impl IOReader { } 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. - } + // 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. should_continue } @@ -304,7 +279,6 @@ impl IOReader { // `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. @@ -324,7 +298,6 @@ impl IOReader { // mid-loop and `run_yield`'s `state().interp` reads 0xdfdf poison. // 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; @@ -367,7 +340,7 @@ bun_io::impl_buffered_reader_parent! { 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()); @@ -385,23 +358,14 @@ impl Drop for IOReader { 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::(); - } - } + // 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). #[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); + if matches!(r.handle, bun_io::pipes::PollOrFd::Poll(_)) { + r.handle.close_impl(None, None::, false); } + let _ = sys::close(s.fd); } r.disable_keeping_process_alive(()); // `reader` Drop handles its own deinit. diff --git a/src/runtime/shell/IOWriter.rs b/src/runtime/shell/IOWriter.rs index ef004501d537..38b57e4c12c7 100644 --- a/src/runtime/shell/IOWriter.rs +++ b/src/runtime/shell/IOWriter.rs @@ -17,8 +17,6 @@ use core::cell::UnsafeCell; #[cfg(not(windows))] use core::ffi::c_void; -#[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,10 +94,15 @@ pub enum WriterTag { // Flags / Writer queue entry // ────────────────────────────────────────────────────────────────────────── +// What kind of fd this is decides how POSIX writes to it; on Windows the +// writer classifies the handle itself. #[derive(Clone, Copy, Default)] pub struct Flags { + #[cfg(not(windows))] pub(crate) pollable: bool, + #[cfg(not(windows))] pub(crate) nonblock: bool, + #[cfg(not(windows))] pub(crate) is_socket: bool, pub(crate) broken_pipe: bool, } @@ -185,7 +188,9 @@ struct State { fd: Fd, writers: Writers, buf: Vec, - /// quick hack to get windows working; ideally this should be removed. + /// The chunk a Windows write has in flight; empty when none is. The kernel + /// borrows those bytes until the write's completion is dequeued, and `buf` + /// reallocates whenever a child enqueues meanwhile. #[cfg(windows)] winbuf: Vec, writer_idx: usize, @@ -198,7 +203,6 @@ 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` @@ -256,14 +260,7 @@ impl IOWriter { pub(crate) fn init(fd: Fd, flags: Flags, evtloop: EventLoopHandle) -> 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, @@ -276,7 +273,6 @@ impl IOWriter { total_bytes_written: 0, err: None, evtloop, - is_writing: false, started: false, flags, self_weak: std::sync::Weak::clone(w), @@ -317,12 +313,6 @@ 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::(); @@ -340,7 +330,6 @@ impl IOWriter { /// `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 @@ -353,7 +342,13 @@ 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) { + // On Windows `true` claims an overlapped pipe end made for a spawned + // child, which no fd of the shell's is; the writer finds out what it has. + #[cfg(windows)] + let pollable = false; + #[cfg(not(windows))] + let pollable = s.flags.pollable; + if let Err(e) = s.writer.start(s.fd, pollable) { #[cfg(not(windows))] { // We get this if we pass in a file descriptor that is not @@ -383,41 +378,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; @@ -481,14 +443,9 @@ 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); - } + // Does nothing while a write is in flight; its completion + // continues with whatever was queued meanwhile. + s.writer.write(); return WriteOutcome::Suspended; } @@ -551,33 +508,26 @@ impl IOWriter { s.total_bytes_written >= s.buf.len() } - /// Only does things on windows. - #[inline] - fn set_writing(&self, writing: bool) { - #[cfg(windows)] - { - self.state().is_writing = writing; - } - let _ = writing; - } - // ── buffer slicing ────────────────────────────────────────────────── /// 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(); #[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(); + // The writer also asks when a write completes, before `on_write`. + // Skipping dead writers then would move `writer_idx` off the one + // the completed bytes belong to. + if self.state().winbuf.is_empty() { + let result = self.get_buffer_impl(); + self.state().winbuf.extend_from_slice(result); + } + // `state()` ties the borrow to `&self`, the lifetime the signature + // wants. + return self.state().winbuf.as_slice(); } #[cfg(not(windows))] - result + self.get_buffer_impl() } fn get_buffer_impl(&self) -> &[u8] { @@ -680,7 +630,6 @@ impl IOWriter { debug_assert!(s.writer_idx < s.writers.len()); } - scopeguard::defer! { self.set_writing(false); } self.skip_dead(); let idx = self.state().writer_idx; @@ -689,10 +638,7 @@ impl IOWriter { 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(self.state().fd, buf) { bun_io::WriteResult::Done(amt) | bun_io::WriteResult::Wrote(amt) => amt, bun_io::WriteResult::Pending(amt) => { // EAGAIN from a target that was classified non-pollable (a @@ -730,13 +676,11 @@ impl IOWriter { /// The `BufferedWriter.onWrite` hook. Runs on the event loop when the fd /// is writable. 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.winbuf.clear(); if s.writer_idx >= s.writers.len() { return; @@ -779,11 +723,6 @@ impl IOWriter { if !wrote_everything && s.writer_idx < s.writers.len() { #[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(); } #[cfg(not(windows))] @@ -829,7 +768,6 @@ impl IOWriter { /// 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); let s = self.state(); if err.get_errno() == E::EPIPE { s.flags.broken_pipe = true; @@ -851,15 +789,16 @@ impl IOWriter { s.writer_idx = 0; s.buf.clear(); s.writers.clear(); + #[cfg(windows)] + s.winbuf.clear(); pending } /// 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 + /// on the stack. On Windows a write that cannot be submitted is also + /// reported from under `write()`, a re-entry `write()` cannot turn into a /// `WriteOutcome::Failed`. fn on_error(&self, err: &sys::Error) { let _keepalive = self.keepalive(); @@ -908,9 +847,7 @@ impl IOWriter { 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). @@ -963,14 +900,9 @@ impl IOWriter { #[cfg(not(windows))] fn enqueue_file(&self, child: ChildPtr) -> Yield { - let s = self.state(); - if s.is_writing { - return Yield::suspended(); - } // 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.state().started = true; self.do_file_write(child) } @@ -1090,7 +1022,6 @@ 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), @@ -1098,23 +1029,21 @@ bun_io::impl_buffered_writer_parent! { } // ────────────────────────────────────────────────────────────────────────── -// drainBufferedData / tryWrite (POSIX file path) +// POSIX file path // ────────────────────────────────────────────────────────────────────────── +/// 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. #[cfg(not(windows))] -fn try_write_with_write_fn( - fd: Fd, - buf: &[u8], - write_fn: fn(Fd, &[u8]) -> sys::Maybe, -) -> bun_io::WriteResult { +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) => { @@ -1128,44 +1057,6 @@ 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 // ────────────────────────────────────────────────────────────────────────── @@ -1188,10 +1079,9 @@ impl Drop for IOWriter { .close_impl(None, None::, false); } } + // The source goes before the fd it was opened on. #[cfg(windows)] - { - s.writer.close(); - } + s.writer.close_without_reporting(); if s.fd != Fd::INVALID { let _ = sys::close(s.fd); } diff --git a/src/runtime/shell/builtin/cat.rs b/src/runtime/shell/builtin/cat.rs index f1b338640653..82ec8fa7f373 100644 --- a/src/runtime/shell/builtin/cat.rs +++ b/src/runtime/shell/builtin/cat.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use crate::shell::ExitCode; -use crate::shell::builtin::{Builtin, BuiltinIO, BuiltinInput, BuiltinState, IoKind, Kind}; +use crate::shell::builtin::{Builtin, BuiltinInput, BuiltinState, IoKind, Kind}; use crate::shell::interpreter::{ FlagParser, Interpreter, NodeId, ParseFlagResult, parse_flags, shell_openat, unsupported_flag, }; @@ -14,16 +14,30 @@ pub struct Cat { pub(crate) state: CatState, } +/// One input (stdin, or the current file argument) on its way to stdout. +#[derive(Default)] +pub struct Progress { + /// The reader reported EOF or an error. + in_done: bool, + chunks_queued: usize, + chunks_done: usize, + /// The reader's error; 0 after EOF. + errno: ExitCode, +} + +impl Progress { + /// A queued chunk calls back into this Cmd by `NodeId`, so the Cmd has to + /// stay until the last one has. + fn finished(&self) -> bool { + self.in_done && self.chunks_done >= self.chunks_queued + } +} + #[derive(Default)] pub 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, @@ -31,14 +45,30 @@ pub enum CatState { idx: usize, /// Per-file reader. reader: Option>, - chunks_queued: usize, - chunks_done: usize, - out_done: bool, - in_done: bool, + progress: Progress, }, WaitingWriteErr, } +impl CatState { + /// What follows a reader or writer completion. + fn step(&mut self) -> Step { + match self { + CatState::ExecStdin(progress) if progress.finished() => Step::Done(progress.errno), + CatState::ExecFilepathArgs { + reader, progress, .. + } if progress.finished() => { + if progress.errno == 0 { + return Step::Next; + } + *reader = None; + Step::Done(progress.errno) + } + _ => Step::Suspend, + } + } +} + /// Internal: what to do after dropping the &mut state borrow. pub(crate) enum Step { Suspend, @@ -76,21 +106,13 @@ impl Cat { 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, - } + CatState::ExecStdin(Progress::default()) } else { CatState::ExecFilepathArgs { args_start: filepath_start.unwrap(), idx: 0, reader: None, - chunks_queued: 0, - chunks_done: 0, - out_done: false, - in_done: false, + progress: Progress::default(), } }; @@ -138,10 +160,8 @@ impl Cat { // 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; + if let CatState::ExecStdin(progress) = &mut Self::state_mut(interp, cmd).state { + progress.in_done = true; } // Copy stdin bytes so the &mut on `stdout`/`write_no_io` // doesn't overlap a borrow of `stdin`. @@ -213,17 +233,11 @@ impl Cat { reader.set_interp(interp_ptr); if let CatState::ExecFilepathArgs { reader: slot, - chunks_done, - chunks_queued, - in_done, - out_done, + progress, .. } = &mut Self::state_mut(interp, cmd).state { - *chunks_done = 0; - *chunks_queued = 0; - *in_done = false; - *out_done = false; + *progress = Progress::default(); *slot = Some(Arc::clone(&reader)); } reader.add_reader(ReaderChildPtr { @@ -252,13 +266,8 @@ impl Cat { // 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); + CatState::ExecStdin(progress) => { + let was_done = core::mem::replace(&mut progress.in_done, true); if !was_done { if let BuiltinInput::Fd(r) = &Builtin::of(interp, cmd).stdin { r.remove_reader(rchild); @@ -276,39 +285,14 @@ impl Cat { return Builtin::done(interp, cmd, errno); } - 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) } @@ -322,10 +306,9 @@ impl Cat { *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, .. } => { + CatState::ExecStdin(progress) | CatState::ExecFilepathArgs { progress, .. } => { if let Some(safeguard) = stdout_needs_io { - *chunks_queued += 1; + progress.chunks_queued += 1; let child = ChildPtr::new(cmd, WriterTag::Builtin); return Builtin::of_mut(interp, cmd) .stdout @@ -344,62 +327,15 @@ impl Cat { err: Option, ) -> 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 - } - } - CatState::ExecFilepathArgs { - chunks_queued, - chunks_done, - in_done, - out_done, - reader, - .. - } => { - *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 - } + let state = &mut Self::state_mut(interp, cmd).state; + let step = match state { + CatState::ExecStdin(progress) | CatState::ExecFilepathArgs { progress, .. } => { + progress.in_done = true; + progress.errno = errno; + state.step() } CatState::WaitingWriteErr | CatState::Idle => Step::Suspend, }; - if cancel { - let wchild = ChildPtr::new(cmd, WriterTag::Builtin); - if let BuiltinIO::Fd(fd) = &Builtin::of(interp, cmd).stdout { - fd.writer.cancel_chunks(wchild); - } - } step.run(interp, cmd) } } diff --git a/src/runtime/shell/builtin/touch.rs b/src/runtime/shell/builtin/touch.rs index 8a5ef6b02c76..3a7b6f9591f4 100644 --- a/src/runtime/shell/builtin/touch.rs +++ b/src/runtime/shell/builtin/touch.rs @@ -280,8 +280,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/interpreter.rs b/src/runtime/shell/interpreter.rs index 1835df18b470..915e109659df 100644 --- a/src/runtime/shell/interpreter.rs +++ b/src/runtime/shell/interpreter.rs @@ -536,7 +536,7 @@ impl Interpreter { bun_sys::open(bun_core::ZStr::from_static(b"NUL\0"), bun_sys::O::RDONLY, 0) } } else { - shell_dup(Fd::stdin()) + bun_sys::dup(Fd::stdin()) }; let stdin_fd = match stdin_fd_res { Ok(fd) => fd, @@ -1167,7 +1167,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) ──────────────── @@ -1175,7 +1175,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, @@ -1189,6 +1189,7 @@ impl Interpreter { let stdout_writer = IOWriter::init( stdout_fd, crate::shell::io_writer::Flags { + #[cfg(not(windows))] pollable: is_pollable(stdout_fd), ..Default::default() }, @@ -1199,6 +1200,7 @@ impl Interpreter { let stderr_writer = IOWriter::init( stderr_fd, crate::shell::io_writer::Flags { + #[cfg(not(windows))] pollable: is_pollable(stderr_fd), ..Default::default() }, @@ -1532,9 +1534,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 { @@ -2243,30 +2243,23 @@ fn open_null_device() -> bun_sys::Result { /// 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. +/// (non-pollable → synchronous write path). +#[cfg(not(windows))] fn is_pollable(fd: Fd) -> bool { - #[cfg(windows)] - { - let _ = fd; - false - } - #[cfg(unix)] + 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 @@ -2302,23 +2295,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 @@ -2394,9 +2370,8 @@ 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, resolve via `shell_get_path` then `bun_sys::open`. pub(crate) fn shell_openat( dir: Fd, path: &bun_core::ZStr, @@ -2405,7 +2380,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(); @@ -2419,11 +2393,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, @@ -2434,13 +2404,10 @@ 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 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))] diff --git a/src/runtime/shell/states/Cmd.rs b/src/runtime/shell/states/Cmd.rs index 374c4731b8d7..5f585692c1be 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/Pipeline.rs b/src/runtime/shell/states/Pipeline.rs index 143286399a2e..2cc701b09572 100644 --- a/src/runtime/shell/states/Pipeline.rs +++ b/src/runtime/shell/states/Pipeline.rs @@ -204,8 +204,10 @@ impl Pipeline { let w = IOWriter::init( pipes[cmd_idx][1], io_writer::Flags { + #[cfg(not(windows))] pollable: true, - is_socket: cfg!(unix), + #[cfg(not(windows))] + is_socket: true, ..Default::default() }, evtloop, diff --git a/src/runtime/shell/subproc.rs b/src/runtime/shell/subproc.rs index 96a931236618..c2110c1a47cf 100644 --- a/src/runtime/shell/subproc.rs +++ b/src/runtime/shell/subproc.rs @@ -1,11 +1,8 @@ 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, SignalCodeExt, 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::io_writer::{self, IOWriter}; @@ -15,8 +12,6 @@ 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; @@ -117,9 +112,6 @@ pub use JscSubprocess::StdioKind; use crate::shell::ShellErr; -#[cfg(windows)] -pub type StdioResult = WindowsStdioResult; -#[cfg(not(windows))] pub type StdioResult = Option; bun_output::define_scoped_log!(log, SHELL_SUBPROC, visible); @@ -392,60 +384,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 @@ -508,7 +483,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] }; @@ -610,54 +584,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`. @@ -682,20 +636,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; @@ -704,15 +644,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())); } @@ -985,133 +916,64 @@ impl Writable { subprocess: *mut Subprocess, result: StdioResult, ) -> 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::Memfd(_) | 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), - ))) - } - 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"); - } + 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(fd) => Ok(Writable::Fd(*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"); } } } @@ -1196,9 +1058,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); } } @@ -1227,7 +1088,7 @@ impl Readable { _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 { @@ -1236,82 +1097,43 @@ 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(fd) => Readable::Fd(*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::Memfd(_) => 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(result.unwrap()), - // 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::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(fd) => Readable::Fd(*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::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, } } @@ -1476,7 +1298,6 @@ pub struct PipeReader { pub(crate) process: Option<*mut ShellSubprocess>, pub(crate) event_loop: EventLoopHandle, pub(crate) state: PipeReaderState, - #[cfg_attr(windows, allow(dead_code))] pub(crate) stdio_result: StdioResult, pub(crate) out_type: OutKind, pub(crate) captured_writer: CapturedWriter, @@ -1713,25 +1534,8 @@ impl PipeReader { captured_writer.dead = false; } - #[allow(unused_mut)] - let mut reader = IOReader::init::(); - #[cfg(not(windows))] + let reader = IOReader::init::(); let stdio_result = result; - #[cfg(windows)] - // With `Box` the pipe cannot be aliased, so ownership - // transfers to `reader.source` (`stdio_result` is never read again - // on Windows — `start()` goes through `start_with_current_pipe`). - let stdio_result = match result { - StdioResult::Buffer(buf) => { - reader.set_source(bun_io::Source::Pipe(buf)); - StdioResult::Unavailable - } - StdioResult::BufferFd(fd) => { - reader.set_source(bun_io::Source::File(bun_io::Source::open_file(fd))); - StdioResult::BufferFd(fd) - } - StdioResult::UnownedFd(_) | StdioResult::Unavailable => panic!("Shouldn't happen."), - }; // Allocate directly into the Arc so the address is stable BEFORE we // hand it to `reader.set_parent` / `container_of` consumers. @@ -1784,15 +1588,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), + 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 @@ -1831,8 +1634,7 @@ 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 @@ -2080,14 +1882,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. @@ -2125,20 +1920,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. @@ -2178,18 +1963,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 723c9c34efd3..fcdf6d981565 100644 --- a/src/runtime/shell/util.rs +++ b/src/runtime/shell/util.rs @@ -6,6 +6,6 @@ pub 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 cd98b92c48e5..ef246335d4ae 100644 --- a/src/runtime/socket/Handlers.rs +++ b/src/runtime/socket/Handlers.rs @@ -509,7 +509,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 3f9cb16194b4..0608d85fd000 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); @@ -102,11 +96,11 @@ pub struct Listener { pub 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] None, @@ -261,14 +255,11 @@ impl Listener { // TODO: server_name is not supported on named pipes, I belive its , lets wait for // someone to ask for it - // we need to add support for the backlog parameter on listen here we use the - // default value of nodejs match WindowsNamedPipeListeningContext::listen( global, &pipe_buf[..pipe_len], - 511, ssl_cfg_taken.as_ref(), - this, + this_ref, ) { Ok(named_pipe) => { this_ref.listener.set(ListenerType::NamedPipe( @@ -284,14 +275,17 @@ 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 { + if let ListenPipeError::Sys(sys_err) = &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 let Some((name, se)) = sys_err.get_error_code_tag_name() + && let Some(uv_errno) = + bun_errno::uv_codes::e_discriminant_to_uv(sys_err.errno) + { if se != bun_sys::SystemErrno::EUNKNOWN && (se as u16) < 3000 { let err = jsc::SystemError { - errno: *uv_errno, + errno: uv_errno, code: bun_core::String::static_(name).into(), message: bun_core::String::clone_utf8( format!( @@ -887,14 +881,9 @@ 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()) }; } - #[cfg(not(windows))] - ListenerType::NamedPipe(_) => {} ListenerType::None => {} } @@ -919,14 +908,9 @@ 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()) }; } - #[cfg(not(windows))] - ListenerType::NamedPipe(_) => {} ListenerType::None => {} } // `deinit` frees the allocation itself (`heap::take`); hand ownership @@ -1011,11 +995,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), } @@ -1099,10 +1083,10 @@ impl Listener { { 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); } } @@ -1168,7 +1152,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 @@ -1185,27 +1168,22 @@ impl Listener { None => false, }, 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 { @@ -1749,137 +1727,70 @@ fn normalize_pipe_name<'a>(pipe_name: &[u8], buffer: &'a mut [u8]) -> Option<&'a #[cfg(windows)] pub 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, - pub ctx: Option, // server reuses the same ctx + /// `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, + ctx: Option, // server reuses the same ctx } -#[cfg(not(windows))] -pub struct WindowsNamedPipeListeningContext { - _priv: (), -} - -/// `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, (): ()) { + // SAFETY: fn contract; the borrows end before any handler runs. + let (pipe, listener_ref, ssl_ctx) = unsafe { + let Some(pipe) = (*this).server.as_mut().and_then(|server| server.accept()) else { + return; + }; + (pipe, (*this).listener, (*this).ctx.clone()) + }; // `BackRef` deref — owner `Listener` outlives this context (see field doc). - let listener_ref = this_ref.listener.unwrap(); let listener: &Listener = listener_ref.get(); use crate::socket::windows_named_pipe_context::SocketType as PipeSocketType; - let socket: PipeSocketType = if this_ref.ctx.is_some() { + let socket: PipeSocketType = if ssl_ctx.is_some() { PipeSocketType::Tls(Listener::on_name_pipe_created::(listener)) } else { PipeSocketType::Tcp(Listener::on_name_pipe_created::(listener)) }; - let client = WindowsNamedPipeContext::create(&this_ref.global_this, socket); + let client = WindowsNamedPipeContext::create(&listener.handlers.global_object, 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, this_ref.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: 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(); @@ -1896,78 +1807,40 @@ 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, + bun_io::windows::pipe_server::DEFAULT_PENDING_INSTANCES, + 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) + + // The owning `Listener`'s `poll_ref` alone keeps the process alive, + // so that `server.unref()` lets it exit. + server.unref(); + // 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 3823142abe5f..94c2cc498e3b 100644 --- a/src/runtime/socket/SSLConfig.rs +++ b/src/runtime/socket/SSLConfig.rs @@ -298,8 +298,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/WindowsNamedPipe.rs b/src/runtime/socket/WindowsNamedPipe.rs index 427bd39f026e..39dc80a92b62 100644 --- a/src/runtime/socket/WindowsNamedPipe.rs +++ b/src/runtime/socket/WindowsNamedPipe.rs @@ -14,38 +14,25 @@ //! - 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}; -#[cfg(windows)] -use core::ptr::NonNull; use bun_boringssl_sys as boringssl; -#[cfg(windows)] -use bun_collections::ByteVecExt; use bun_core::timespec; -#[cfg(windows)] -use bun_io::pipe_writer::BaseWindowsPipeWriter as _; -use bun_io::{StreamingWriter, WriteStatus}; +use bun_io::windows::{ConnectRequest, Pipe, ReadEvent}; +use bun_io::{Source, StreamingWriter, WriteStatus}; use bun_jsc::JsCell; use bun_jsc::virtual_machine::VirtualMachine; -#[cfg(windows)] -use bun_libuv_sys::{UvHandle as _, UvStream as _}; -#[cfg(windows)] -use bun_sys::ReturnCodeExt as _; -#[cfg(windows)] -use bun_sys::windows::libuv as uv; use bun_sys::{self, Fd}; use bun_uws::us_bun_verify_error_t; use crate::socket::SSLConfig; use crate::socket::ssl_wrapper::{self, SSLWrapper}; -#[cfg(windows)] -use crate::timer::EventLoopTimerTag; -use crate::timer::{ElTimespec, EventLoopTimer, EventLoopTimerState}; +use crate::timer::{ElTimespec, EventLoopTimer, EventLoopTimerState, EventLoopTimerTag}; bun_output::declare_scope!(WindowsNamedPipe, visible); @@ -59,15 +46,9 @@ pub 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. - #[cfg(windows)] - pub(crate) pipe: Cell>>, // any duplex - #[cfg(not(windows))] - pub pipe: (), + /// 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. @@ -76,15 +57,11 @@ pub struct WindowsNamedPipe { /// (`bun_io::EventLoopHandle` wraps `*const EventLoopHandle`). pub event_loop_handle: bun_jsc::EventLoopHandle, + /// 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, - #[cfg(windows)] - pub(crate) connect_req: JsCell, - #[cfg(not(windows))] - pub connect_req: (), pub(crate) event_loop_timer: JsCell, pub(crate) current_timeout: Cell, @@ -99,16 +76,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; } } @@ -173,41 +143,6 @@ impl WindowsNamedPipe { Some(result) } - #[cfg(windows)] - #[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`). - #[cfg(windows)] - 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 @@ -236,26 +171,27 @@ impl WindowsNamedPipe { (self.handlers.on_writable)(self.handlers.ctx); } - #[cfg(windows)] - 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::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(); - 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) { @@ -271,19 +207,7 @@ impl WindowsNamedPipe { ); match status { - WriteStatus::Pending => {} - WriteStatus::Drained => { - // unref after sending all data - #[cfg(windows)] - 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::Pending | WriteStatus::Drained => {} WriteStatus::EndOfFile => { // we send FIN so we close after this self.close_writer(); @@ -315,20 +239,18 @@ impl WindowsNamedPipe { r } - #[cfg(windows)] - fn on_read_error(&self, err: bun_sys::E) { + fn on_read_end(&self) { + bun_output::scoped_log!(WindowsNamedPipe, "onReadEnd"); + let _keep_alive = self.keep_alive(); + // we received FIN but we dont allow half-closed connections right now + (self.handlers.on_end)(self.handlers.ctx); + self.close_writer(); + } + + fn on_read_error(&self, err: bun_sys::Error) { bun_output::scoped_log!(WindowsNamedPipe, "onReadError"); let _keep_alive = self.keep_alive(); - // `E::EOF` only exists in the Windows errno table (libuv UV_EOF mapping); - // this type is Windows-only at runtime so the comparison is gated. - #[cfg(windows)] - 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); - self.close_writer(); - return; - } - self.on_error(bun_sys::Error::from_code(err, bun_sys::Tag::read)); + self.on_error(err); self.close_writer(); } @@ -392,7 +314,6 @@ impl WindowsNamedPipe { unsafe { &*this }.internal_write(d) } - #[cfg(windows)] fn wrapper_handlers(&self) -> ssl_wrapper::Handlers<*mut WindowsNamedPipe> { ssl_wrapper::Handlers { ctx: self.root_ptr(), @@ -434,8 +355,6 @@ impl WindowsNamedPipe { return; } bun_output::scoped_log!(WindowsNamedPipe, "onClose"); - #[cfg(windows)] - 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); @@ -446,14 +365,6 @@ 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 - #[cfg(windows)] - 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 @@ -484,42 +395,29 @@ impl WindowsNamedPipe { self.call_write_or_end(Some(encoded_data), true); } + /// 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, + }) + } + + 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 fn resume_stream(&self) -> bool { - #[cfg(windows)] - { - 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() - } - #[cfg(not(windows))] - { - false - } + matches!(self.read_start(), Some(Ok(()))) } + /// 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 fn pause_stream(&self) -> bool { - #[cfg(windows)] - { - let Some(pipe) = self.uv_pipe() else { - return false; - }; - // SAFETY: live libuv handle alias; see `pipe`. - unsafe { (*pipe).read_stop() }; - true - } - #[cfg(not(windows))] - { - false - } + self.with_pipe(Pipe::read_stop).is_some() } #[bun_uws::uws_callback(export = "WindowsNamedPipe__flush")] @@ -550,31 +448,18 @@ impl WindowsNamedPipe { (self.handlers.on_timeout)(self.handlers.ctx); } - #[cfg(windows)] - pub(crate) fn from( - pipe: Box, - handlers: Handlers, - vm: &'static VirtualMachine, - ) -> WindowsNamedPipe { - // The whole fn is `#[cfg(windows)]`-gated so POSIX builds never see - // `uv::Pipe`. + pub(crate) fn init(handlers: Handlers, vm: &'static VirtualMachine) -> WindowsNamedPipe { WindowsNamedPipe { vm, event_loop_handle: bun_jsc::EventLoopHandle::init(vm.event_loop().cast::<()>()), - // 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, )), @@ -591,45 +476,31 @@ 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). - #[cfg(windows)] - 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); - } - - #[cfg(windows)] - 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 `on_error → close` has nothing to close and + /// reports no `on_close`; the owner (`handlers.on_close`) releases its + /// ref from there. + fn on_connect_error(&self, err: bun_sys::Error) { + self.on_error(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()); @@ -639,26 +510,22 @@ impl WindowsNamedPipe { } } self.flush(); - self.deref(); } - #[cfg(windows)] - 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<()> { - #[cfg(windows)] - 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, @@ -667,35 +534,9 @@ impl WindowsNamedPipe { } } } - #[cfg(windows)] - { - 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()); @@ -707,130 +548,57 @@ impl WindowsNamedPipe { bun_sys::Result::Ok(()) } - #[cfg(windows)] + /// Adopt a pipe end somebody else opened. `fd` is closed with the socket; + /// on `Err` it is still the caller's. pub(crate) fn open( &self, fd: Fd, 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 pipe = Pipe::open_foreign(self.vm.uws_loop(), fd, true)?; - self.r#ref(); - self.on_connect(uv::ReturnCode::ZERO); + let _keep_alive = self.keep_alive(); + self.on_connected(pipe); bun_sys::Result::Ok(()) } - #[cfg(windows)] + /// 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(()) } - #[cfg(not(windows))] - pub(crate) fn open( - &self, - _fd: Fd, - _ssl_options: Option, - _owned_ctx: Option, - ) -> bun_sys::Result<()> { - // Unreachable on POSIX — `WindowsNamedPipeContext` is aliased to `()` there; - // this stub exists only so the module type-checks across platforms. - unreachable!("WindowsNamedPipe::open is windows-only") - } - - #[cfg(not(windows))] - pub(crate) fn connect( - &self, - _path: &[u8], - _ssl_options: Option, - _owned_ctx: Option, - ) -> bun_sys::Result<()> { - // Unreachable on POSIX — see `open` above. - unreachable!("WindowsNamedPipe::connect is windows-only") - } - /// 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. - #[cfg(windows)] + /// Returns `None` when neither input requested TLS. fn init_tls_wrapper( &self, ssl_options: Option, @@ -867,54 +635,19 @@ impl WindowsNamedPipe { None } - pub(crate) fn start(&self, is_client: bool) -> bool { - self.update_flags(|f| f.set(Flags::IS_CLIENT, is_client)); - #[cfg(windows)] - { - let Some(pipe_nn) = self.pipe.get() else { - return false; - }; - // SAFETY: live libuv handle alias; see `pipe`. - unsafe { (*pipe_nn.as_ptr()).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| { - 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()) } - }); - 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 { - self.on_error(err); - return false; - } - true - } - #[cfg(not(windows))] - { - let _ = is_client; - false + 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.writer.with_mut(|w| { + w.set_parent(this); + w.start_with_source(Source::Pipe(pipe)); + }); + if let Some(bun_sys::Result::Err(err)) = self.read_start() { + self.on_error(err); + return false; } + true } pub(crate) fn is_tls(&self) -> bool { @@ -937,21 +670,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. - #[cfg(windows)] - 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). - let this = unsafe { &*this.cast::() }; - if this.flags.get().contains(Flags::PIPE_ADOPTED) { - this.close(); - } else { - this.discard_unadopted_pipe(); - } - } - + /// 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 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); }); @@ -978,12 +708,7 @@ impl WindowsNamedPipe { if let Some(wrapper) = self.wrapper_ref() { wrapper.shutdown_read(); } else { - #[cfg(windows)] - 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); } } @@ -1042,8 +767,8 @@ impl WindowsNamedPipe { } // reschedule the timer - // `EventLoopTimer.next` is the lower-tier `ElTimespec` stub; - // bridge from `bun_core::Timespec` until the lower tier switches. + // `EventLoopTimer.next` is the lower-tier `ElTimespec`; + // bridge from `bun_core::Timespec`. let next = timespec::ms_from_now(bun_core::TimespecMockMode::ForceRealTime, ms as i64); self.event_loop_timer.with_mut(|t| { t.next = ElTimespec { @@ -1065,38 +790,18 @@ 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 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. - #[cfg(windows)] - { - 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()); - } + // 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); } @@ -1106,27 +811,9 @@ 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. - #[cfg(windows)] - 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); } } @@ -1144,9 +831,8 @@ pub(crate) extern "C" fn WindowsNamedPipe__ssl(this: *const c_void) -> *mut bori } } -// Windows-only at runtime; the POSIX impl exists purely so the -// `StreamingWriter` field type-checks (poll_tag::NULL keeps the -// dispatch table from being silently wrong if a poll is ever created). +// This module is Windows-only; `poll_tag` and `event_loop` feed the macro's +// POSIX impl alone. bun_io::impl_streaming_writer_parent! { WindowsNamedPipe; poll_tag = bun_io::posix_event_loop::poll_tag::NULL, @@ -1157,46 +843,6 @@ bun_io::impl_streaming_writer_parent! { on_close = on_close, event_loop = |this| (*this).event_loop_handle.as_event_loop_ctx(), uws_loop = |this| (*this).vm.uws_loop(), - uv_loop = |this| (*this).vm.uv_loop(), ref_ = |this| (&*this).r#ref(), deref = |this| (&*this).deref(), } - -/// The three `stream.readStart` callbacks (alloc/error/read) baked into a -/// trait so the -/// `extern "C"` libuv trampoline is monomorphised over `WindowsNamedPipe`. -#[cfg(windows)] -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(); - let _ = data; - // 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 f29fc430a992..10880aff164a 100644 --- a/src/runtime/socket/WindowsNamedPipeContext.rs +++ b/src/runtime/socket/WindowsNamedPipeContext.rs @@ -8,12 +8,9 @@ use crate::socket::NewSocket; use crate::socket::SSLConfig; use crate::socket::windows_named_pipe::{Handlers as NamedPipeHandlers, WindowsNamedPipe}; 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, JSGlobalObject, SysErrorJsc}; -#[cfg(windows)] -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}; @@ -35,9 +32,8 @@ pub 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, @@ -90,18 +86,8 @@ pub enum SocketType { fn socket_from_named_pipe( pipe: *mut WindowsNamedPipe, ) -> uws::NewSocketHandler { - #[cfg(windows)] - { - uws::NewSocketHandler { - socket: uws::InternalSocket::Pipe(pipe.cast()), - } - } - #[cfg(not(windows))] - { - let _ = pipe; - uws::NewSocketHandler { - socket: uws::InternalSocket::Pipe, - } + uws::NewSocketHandler { + socket: uws::InternalSocket::Pipe(pipe.cast()), } } @@ -247,7 +233,7 @@ impl WindowsNamedPipeContext { /// # Safety /// `this` is a registered live context (see `create`). pub(crate) unsafe fn stop_for_vm_teardown(this: *mut Self) { - // SAFETY: fn contract; `close` re-enters `on_close`, which may free `this`. + // SAFETY: fn contract; the call re-enters `on_close`, which may free `this`. unsafe { (*ptr::addr_of_mut!((*this).named_pipe)).close() }; } @@ -316,7 +302,6 @@ impl WindowsNamedPipeContext { unsafe { Self::deref(this) }; } - #[cfg(windows)] /// # Safety /// `this` is the live queued pointer; the call may free it. pub(crate) unsafe fn run_event(this: *mut Self) { @@ -363,7 +348,6 @@ 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 @@ -391,57 +375,43 @@ impl WindowsNamedPipeContext { on_session: |p, d| Self::on_session(p.cast::(), d), on_keylog: |p, d| Self::on_keylog(p.cast::(), d), }; - #[cfg(not(windows))] - { - // On POSIX `crate::socket::WindowsNamedPipeContext` is aliased to `()` (see mod.rs) - // so no caller can reach `create()`. This arm exists only so the module - // type-checks; matches the sibling `WindowsNamedPipe::open`/`connect` POSIX arms. - let _ = (vm, this, handlers, socket); - unreachable!("WindowsNamedPipeContext::create is windows-only") + 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 { + ptr::write( + this, + WindowsNamedPipeContext { + ref_count: Cell::new(1), + socket, + named_pipe, + vm, + global_this, + task_event: EventState::None, + is_open: false, + }, + ); + (*ptr::addr_of_mut!((*this).named_pipe)) + .root + .set(ptr::addr_of_mut!((*this).named_pipe)); } - #[cfg(windows)] - { - let named_pipe = { - let pipe = Box::new(bun_core::ffi::zeroed::()); - WindowsNamedPipe::from(pipe, handlers, vm) - }; - // SAFETY: `this` is freshly allocated uninit storage exclusively owned here; we write - // every field exactly once before any read. - unsafe { - ptr::write( - this, - WindowsNamedPipeContext { - ref_count: Cell::new(1), - socket, - named_pipe, - vm, - global_this, - task_event: EventState::None, - is_open: false, - }, - ); - (*ptr::addr_of_mut!((*this).named_pipe)) - .root - .set(ptr::addr_of_mut!((*this).named_pipe)); - } - LIVE_COUNT.fetch_add(1, Ordering::Relaxed); + LIVE_COUNT.fetch_add(1, Ordering::Relaxed); - // Take a +1 intrusive ref so the wrapped JS socket outlives this context. - match_socket!(socket, |s: NewSocket| { - s.ref_(); - Ok(()) - }); + // Take a +1 intrusive ref so the wrapped JS socket outlives this context. + match_socket!(socket, |s: NewSocket| { + s.ref_(); + Ok(()) + }); - // A socket over a Windows named pipe is in no uSockets group: the VM's - // stop phase closes it through this owner (unregistered when freed). - // SAFETY: non-null, fully initialised above. - crate::jsc_hooks::ActiveHandle::WindowsNamedPipe(unsafe { - core::ptr::NonNull::new_unchecked(this) - }) - .register(); + // A socket over a Windows named pipe is in no uSockets group: the VM's + // stop phase closes it through this owner (unregistered when freed). + // SAFETY: non-null, fully initialised above. + crate::jsc_hooks::ActiveHandle::WindowsNamedPipe(unsafe { + core::ptr::NonNull::new_unchecked(this) + }) + .register(); - this - } + this } /// `owned_ctx` is moved into `named_pipe.open`. Prefer it over `ssl_config` so a @@ -484,24 +454,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) @@ -526,7 +483,6 @@ impl Drop for WindowsNamedPipeContext { } } -#[cfg(windows)] 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 diff --git a/src/runtime/socket/socket_body.rs b/src/runtime/socket/socket_body.rs index ea34077d56ba..40bf8425eca6 100644 --- a/src/runtime/socket/socket_body.rs +++ b/src/runtime/socket/socket_body.rs @@ -1221,8 +1221,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::uv_codes::e_discriminant_to_uv(errno_ as u16) + .unwrap_or(bun_errno::uv_codes::UV_ECONNREFUSED); SystemError { errno: -errno_, message: BunString::static_("Failed to connect"), @@ -3325,10 +3325,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 0912e04fd427..71032bc3bc09 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_handlers.rs b/src/runtime/socket/uws_handlers.rs index c0da5f699344..d810907f8bd4 100644 --- a/src/runtime/socket/uws_handlers.rs +++ b/src/runtime/socket/uws_handlers.rs @@ -146,15 +146,11 @@ where fold(T::on_end(this, wrap::(s))); } fn on_connect_error(ext: &mut Self::Ext, s: *mut us_socket_t, code: i32) { - // Close FIRST, then notify — same order `main`'s `configure()` - // trampoline used. The handler may re-enter `connectInner` + // Close FIRST, then notify. The handler may re-enter `connectInner` // synchronously (node:net `autoSelectFamily` falls back to the - // next address from inside the JS `connectError` callback); on - // Windows/libuv, starting the next attempt's `uv_poll_t` while - // this half-open one is still active and then closing it - // *afterwards* leaves the second poll never delivering - // writable/error → process hang (Win11-aarch64 - // double-connect.test, test-net-server-close). + // next address from inside the JS `connectError` callback), and + // the next attempt must not start while this half-open socket is + // still open (double-connect.test, test-net-server-close). // // Safe for TLS too: `us_internal_ssl_close` short-circuits // SEMI_SOCKET straight to `close_raw`, and `close_raw` skips diff --git a/src/runtime/timer/mod.rs b/src/runtime/timer/mod.rs index 97b1e578bffb..17d6b8db3b18 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(), @@ -667,126 +655,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() }; - } - #[allow(clippy::not_unsafe_ptr_arg_deref)] pub(crate) fn remove(&mut self, timer: *mut EventLoopTimer) { self.assert_js_thread(); @@ -862,10 +733,6 @@ impl All { (*timer).state = EventLoopTimerState::ACTIVE; } } - #[cfg(windows)] - if self.thread_id == std::thread::current().id() { - self.ensure_uv_timer(); - } } #[allow(clippy::not_unsafe_ptr_arg_deref)] @@ -955,13 +822,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; @@ -1107,43 +971,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 @@ -1158,25 +991,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/Blob.rs b/src/runtime/webcore/Blob.rs index d5574473c709..ebee781e059d 100644 --- a/src/runtime/webcore/Blob.rs +++ b/src/runtime/webcore/Blob.rs @@ -451,51 +451,29 @@ impl BlobExt for Blob { let handler = bun_core::heap::into_raw(Box::new(Handler::<'_, F>::new(self.dupe(), global))); - #[cfg(windows)] - { - // SAFETY: handler was just boxed; sole owner. - unsafe { (*handler).promise = jsc::JSPromiseStrong::init(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. - global.bun_vm().event_loop(), - self.store().expect("infallible: store present").clone(), - self.offset.get(), - self.size.get(), - handler, - ); - return promise_value; - } - - #[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(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), - global, - ); + 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(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), + global, + ); - 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 @@ -686,31 +664,17 @@ impl BlobExt for Blob { ctx: *mut C, global: &JSGlobalObject, ) { - #[cfg(windows)] - { - return read_file::ReadFileUV::start_with_ctx( - // SAFETY: `bun_vm()` returns the live VM for this global. - global.bun_vm().event_loop(), - 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), - global, - ); - } + 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), + global, + ); } fn get_content_type(&self) -> Option> { let ct = self.content_type_slice(); @@ -1048,7 +1012,7 @@ 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(uv_file) => { bun_core::write_pretty!( writer, ENABLE_ANSI_COLORS, @@ -1434,135 +1398,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( - global_this, - err.with_path(path).to_js(global_this), - ) - .to_js()); - } - } - }; - - let is_stdout_or_stderr = 'brk: { - if !matches!(pathlike, PathOrFileDescriptor::Fd(_)) { - break 'brk false; - } - - if let Some(rare) = global_this.bun_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::<()>(), + ), + ); - #[cfg(windows)] - 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(global_this, err.to_js(global_this)).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) { + return Ok( + JSPromise::rejected_promise(global_this, err.to_js(global_this)).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) { - return Ok( - JSPromise::rejected_promise(global_this, err.to_js(global_this)).to_js(), - ); - } - break 'brk_sink sink; } + sink }; // `pipe_stream` takes its own refs; init's +1 drops with `file_sink` on return. @@ -1670,126 +1539,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))); + 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) { - 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) { + 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, @@ -3501,7 +3290,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, @@ -4350,73 +4139,34 @@ 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(ctx); - let promise_value = promise.as_value(ctx); - promise_value.ensure_still_alive(); - // SAFETY: write_file_promise was just produced by heap::alloc above; sole owner. - unsafe { (*write_file_promise).promise.set(ctx, promise_value) }; - match write_file_mod::WriteFileWindows::create( - ctx.bun_vm().event_loop(), - 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(), - write_file_promise, - WriteFilePromise::run, - options.mkdirp_if_not_exists.unwrap_or(true), - ) - .expect("unreachable"); - // Defer promise creation until we're just about to schedule the task. - // SAFETY: write_file_promise was just produced by heap::alloc above; sole owner. - unsafe { (*write_file_promise).promise = jsc::JSPromiseStrong::init(ctx) }; - // SAFETY: same `write_file_promise` as above; still solely owned here. - let promise_value = unsafe { (*write_file_promise).promise.value() }; - promise_value.ensure_still_alive(); - write_file_mod::WriteFile::schedule(file_copier, ctx); - return Ok(promise_value); - } + let file_copier = write_file_mod::WriteFile::create( + destination_blob.borrowed_view(), + source_blob.borrowed_view(), + write_file_promise, + WriteFilePromise::run, + options.mkdirp_if_not_exists.unwrap_or(true), + ) + .expect("unreachable"); + // Defer promise creation until we're just about to schedule the task. + // SAFETY: write_file_promise was just produced by heap::alloc above; sole owner. + unsafe { (*write_file_promise).promise = jsc::JSPromiseStrong::init(ctx) }; + // SAFETY: same `write_file_promise` as above; still solely owned here. + let promise_value = unsafe { (*write_file_promise).promise.value() }; + promise_value.ensure_still_alive(); + write_file_mod::WriteFile::schedule(file_copier, ctx); + 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, - ctx.bun_vm().event_loop_shared(), - 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(), - ctx, - 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(), + ctx, + 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( @@ -5229,11 +4979,7 @@ fn write_bytes_to_file_fast( 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 - } else { - bun_sys::O::WRONLY | bun_sys::O::CREAT - }; + let flags = bun_sys::O::WRONLY | bun_sys::O::CREAT | bun_sys::O::NONBLOCK; match bun_sys::open( pathlike.path().slice_z(&mut file_path), flags, @@ -5241,7 +4987,6 @@ fn write_bytes_to_file_fast( ) { bun_sys::Result::Ok(result) => result, bun_sys::Result::Err(err) => { - #[cfg(not(windows))] if err.get_errno() == bun_sys::E::ENOENT { *_needs_async = true; return JSValue::ZERO; @@ -5255,8 +5000,6 @@ fn write_bytes_to_file_fast( } }; - // TODO: on windows this is always synchronous - let truncate = NEEDS_OPEN || bytes.is_empty(); let mut written: usize = 0; let _close = NEEDS_OPEN.then(|| bun_sys::CloseOnDrop::new(fd)); @@ -5272,7 +5015,6 @@ fn write_bytes_to_file_fast( } } bun_sys::Result::Err(err) => { - #[cfg(not(windows))] if err.get_errno() == bun_sys::E::EAGAIN { *_needs_async = true; return JSValue::ZERO; @@ -5288,12 +5030,6 @@ fn write_bytes_to_file_fast( } if truncate { - #[cfg(windows)] - // SAFETY: fd is a valid open handle on this code path; FFI call. - unsafe { - bun_sys::windows::kernel32::SetEndOfFile(fd.native()) - }; - #[cfg(not(windows))] let _ = bun_sys::ftruncate(fd, i64::try_from(written).expect("int cast")); } @@ -5763,7 +5499,7 @@ 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); +/// `libc::stat` on POSIX (fields) and has an `mtim` timespec on Windows; /// cfg-split here so the call sites stay shared. #[inline] fn stat_to_js_mtime(stat: &bun_sys::Stat) -> jsc::JSTimeType { @@ -6476,17 +6212,6 @@ pub 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(); @@ -6496,124 +6221,40 @@ pub trait FileOpener: Sized { }; let path = 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)) { @@ -6647,8 +6288,6 @@ pub 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 @@ -6700,13 +6339,8 @@ pub 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); } @@ -6745,11 +6379,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/FileReader.rs b/src/runtime/webcore/FileReader.rs index c1965e1a3a9a..3a2d367f08e6 100644 --- a/src/runtime/webcore/FileReader.rs +++ b/src/runtime/webcore/FileReader.rs @@ -9,7 +9,9 @@ use bun_io::FileType; use bun_io::{BufferedReader, Chunk, ReadState}; 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; @@ -168,7 +170,7 @@ impl Lazy { } } - fd.make_lib_uv_owned_for_syscall(sys::Tag::dup, sys::ErrorCase::CloseOnFail)? + fd } } PathOrFileDescriptor::Path(path) => { @@ -267,12 +269,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 @@ -337,7 +334,6 @@ impl FileReader { { file_type = opened.file_type; } - #[cfg(unix)] { use bun_io::pipe_reader::PosixFlags; self.reader() @@ -345,14 +341,6 @@ impl FileReader { .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); - } } } } @@ -384,8 +372,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,7 +400,6 @@ impl FileReader { return streams::Start::Err(e); } } else { - #[cfg(unix)] { use bun_io::pipe_reader::PosixFlags; if !self.started.get() @@ -425,21 +412,6 @@ impl FileReader { 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() }; - } - } } #[cfg(unix)] @@ -477,7 +449,6 @@ impl FileReader { )); } } else { - #[cfg(unix)] { use bun_io::pipe_reader::PosixFlags; if !was_lazy && self.reader().flags.contains(PosixFlags::POLLABLE) { diff --git a/src/runtime/webcore/FileSink.rs b/src/runtime/webcore/FileSink.rs index 13824fe458c4..7105c02c18e4 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); @@ -149,7 +143,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), } @@ -217,57 +210,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); } } @@ -522,14 +470,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()); } @@ -608,31 +550,6 @@ impl FileSink { } } - #[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()); - } - this - } - - #[cfg(not(windows))] pub(crate) fn create(event_loop_: impl Into, fd: Fd) -> RefPtr { Self::init(fd, event_loop_) } @@ -643,6 +560,19 @@ impl FileSink { return sys::Result::Ok(()); } + // Writes to the process's own stdout and stderr are synchronous on + // Windows whatever they are connected to (Node documents pipes and + // files that way; a console write is inline as well). + #[cfg(windows)] + if let PathOrFileDescriptor::Fd(fd) = &options.input_path + && matches!( + fd.stdio_tag(), + Some(bun_sys::Stdio::StdOut | bun_sys::Stdio::StdErr) + ) + { + self.force_sync.set(true); + } + // 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 force_sync_out = self.force_sync.get(); @@ -706,7 +636,6 @@ impl FileSink { self.pollable.set(pollable_out); self.is_socket.set(is_socket_out); self.nonblocking.set(nonblocking_out); - #[cfg(unix)] if force_sync_out { self.force_sync.set(true); // SAFETY(JsCell): single-field write; does not call into JS. @@ -720,27 +649,6 @@ impl FileSink { sys::Result::Ok(fd) => fd, }; - #[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(()); - } - } - // SAFETY(JsCell): `start` is pure I/O setup; no JS. match self.writer.with_mut(|w| w.start(fd, self.pollable.get())) { sys::Result::Err(err) => { @@ -1369,9 +1277,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()); @@ -1454,7 +1360,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))] @@ -1498,8 +1404,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); } diff --git a/src/runtime/webcore/blob/copy_file.rs b/src/runtime/webcore/blob/copy_file.rs index 0d5e8d8ffdc8..36223eea339b 100644 --- a/src/runtime/webcore/blob/copy_file.rs +++ b/src/runtime/webcore/blob/copy_file.rs @@ -2,35 +2,22 @@ use crate::node::fs as node_fs; use crate::node::types::PathLikeExt as _; -#[cfg(not(windows))] 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; 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(windows)] -use bun_sys::windows::libuv; use bun_sys::{self, Fd, FdExt, Mode, SystemError}; -#[cfg(windows)] -use bun_sys_jsc::ErrorJsc as _; #[cfg(any(target_os = "linux", target_os = "android"))] use core::ffi::c_int; -#[cfg(windows)] -use core::ffi::c_void; use core::marker::ConstParamTy; // ─────────────────────────────────────────────────────────────────────────── -// CopyFile (POSIX, blocking off-thread) +// CopyFile (blocking off-thread) // ─────────────────────────────────────────────────────────────────────────── pub struct CopyFile { - #[cfg(not(windows))] pub(crate) destination_file_store: store::File, pub(crate) source_file_store: store::File, // `RefPtr` is the thread-safe refcounted handle; @@ -39,11 +26,8 @@ pub struct CopyFile { pub(crate) store: Option>, pub(crate) source_store: Option>, pub offset: SizeType, - #[cfg(not(windows))] pub(crate) max_length: SizeType, - #[cfg(not(windows))] pub(crate) destination_fd: Fd, - #[cfg(not(windows))] pub(crate) source_fd: Fd, pub(crate) system_error: Option, @@ -51,7 +35,6 @@ pub struct CopyFile { pub(crate) read_len: SizeType, pub(crate) mkdirp_if_not_exists: bool, - #[cfg(not(windows))] pub(crate) destination_mode: Option, } @@ -88,7 +71,6 @@ impl jsc::JobContext for CopyFile { impl CopyFile { /// Schedule the copy on the work pool; returns its promise. - #[cfg(not(windows))] pub(crate) fn create( store: RefPtr, source_store: RefPtr, @@ -164,7 +146,6 @@ impl CopyFile { ) } - #[cfg(not(windows))] pub(crate) fn do_close(&mut self) { let close_input = !matches!( self.destination_file_store.pathlike, @@ -197,7 +178,6 @@ impl CopyFile { } } - #[cfg(not(windows))] pub(crate) fn do_close_file(&mut self) { match WHICH { IOWhich::Both => { @@ -213,7 +193,6 @@ impl CopyFile { } } - #[cfg(not(windows))] pub(crate) fn do_open_file(&mut self) -> Result<(), crate::Error> { let mut path_buf1 = bun_paths::path_buffer_pool::get(); // open source file first @@ -227,18 +206,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 +229,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 +460,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,345 +571,507 @@ impl CopyFile { } pub(crate) fn run_async(&mut self) { + // `BUN_FEATURE_FLAG_DISABLE_UV_FS_COPYFILE` makes the handle-to-handle + // loop below testable. #[cfg(windows)] + if !bun_core::env_var::feature_flag::BUN_FEATURE_FLAG_DISABLE_UV_FS_COPYFILE + .get() + .unwrap_or(false) { - return; // why + 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(not(windows))] - { - #[cfg(target_os = "macos")] - let mut stat_: Option = None; - #[cfg(not(target_os = "macos"))] - let stat_: Option = None; - if let PathOrFileDescriptor::Fd(fd) = &self.destination_file_store.pathlike { - self.destination_fd = *fd; - } + #[cfg(target_os = "macos")] + let mut stat_: Option = None; + #[cfg(not(target_os = "macos"))] + let stat_: Option = None; - if let PathOrFileDescriptor::Fd(fd) = &self.source_file_store.pathlike { - self.source_fd = *fd; - } + if let PathOrFileDescriptor::Fd(fd) = &self.destination_file_store.pathlike { + self.destination_fd = *fd; + } + + if let PathOrFileDescriptor::Fd(fd) = &self.source_file_store.pathlike { + self.source_fd = *fd; + } - // Do we need to open both files? - if self.destination_fd == Fd::INVALID && self.source_fd == Fd::INVALID { - // First, we attempt to clonefile() on macOS - // This is the fastest way to copy a file. - #[cfg(target_os = "macos")] + // Do we need to open both files? + if self.destination_fd == Fd::INVALID && self.source_fd == Fd::INVALID { + // First, we attempt to clonefile() on macOS + // This is the fastest way to copy a file. + #[cfg(target_os = "macos")] + { + if self.offset == 0 + && matches!( + self.source_file_store.pathlike, + PathOrFileDescriptor::Path(_) + ) + && matches!( + self.destination_file_store.pathlike, + PathOrFileDescriptor::Path(_) + ) { - if self.offset == 0 - && matches!( - self.source_file_store.pathlike, - PathOrFileDescriptor::Path(_) - ) - && matches!( - self.destination_file_store.pathlike, - PathOrFileDescriptor::Path(_) - ) - { - 'do_clonefile: { - let mut path_buf = bun_paths::path_buffer_pool::get(); - - // stat the output file, make sure it: - // 1. Exists - match bun_sys::stat( - self.source_file_store - .pathlike - .path() - .slice_z(&mut path_buf), - ) { - bun_sys::Result::Ok(result) => { - stat_ = Some(result); - - if bun_sys::S::ISDIR(result.st_mode as u32) { - self.system_error = Some(unsupported_directory_error()); - return; - } + 'do_clonefile: { + let mut path_buf = bun_paths::path_buffer_pool::get(); + + // stat the output file, make sure it: + // 1. Exists + match bun_sys::stat( + self.source_file_store + .pathlike + .path() + .slice_z(&mut path_buf), + ) { + bun_sys::Result::Ok(result) => { + stat_ = Some(result); - if !bun_sys::S::ISREG(result.st_mode as u32) { - break 'do_clonefile; - } - } - bun_sys::Result::Err(err) => { - // If we can't stat it, we also can't copy it. - self.system_error = Some(err.to_system_error()); + if bun_sys::S::ISDIR(result.st_mode as u32) { + self.system_error = Some(unsupported_directory_error()); return; } + + if !bun_sys::S::ISREG(result.st_mode as u32) { + break 'do_clonefile; + } } + bun_sys::Result::Err(err) => { + // If we can't stat it, we also can't copy it. + self.system_error = Some(err.to_system_error()); + return; + } + } - match self.do_clonefile() { - Ok(()) => { - let stat_size = stat_.unwrap().st_size; - if self.max_length != MAX_SIZE - && self.max_length - < SizeType::try_from(stat_size).expect("int cast") - { - // If this fails...well, there's not much we can do about it. - // SAFETY: NUL-terminated path in path_buf; libc truncate(2). - let _ = unsafe { - bun_sys::c::truncate( - self.destination_file_store - .pathlike - .path() - .slice_z(&mut path_buf) - .as_ptr(), - i64::try_from(self.max_length).expect("int cast"), - ) - }; - self.read_len = - SizeType::try_from(self.max_length).expect("int cast"); - } else { - self.read_len = - SizeType::try_from(stat_size).expect("int cast"); - } - // Apply destination mode if specified (clonefile copies source permissions) - if let Some(mode) = self.destination_mode { - match bun_sys::chmod( + match self.do_clonefile() { + Ok(()) => { + let stat_size = stat_.unwrap().st_size; + if self.max_length != MAX_SIZE + && self.max_length + < SizeType::try_from(stat_size).expect("int cast") + { + // If this fails...well, there's not much we can do about it. + // SAFETY: NUL-terminated path in path_buf; libc truncate(2). + let _ = unsafe { + bun_sys::c::truncate( self.destination_file_store .pathlike .path() - .slice_z(&mut path_buf), - mode, - ) { - bun_sys::Result::Err(err) => { - self.system_error = Some(err.to_system_error()); - return; - } - bun_sys::Result::Ok(()) => {} + .slice_z(&mut path_buf) + .as_ptr(), + i64::try_from(self.max_length).expect("int cast"), + ) + }; + self.read_len = + SizeType::try_from(self.max_length).expect("int cast"); + } else { + self.read_len = + SizeType::try_from(stat_size).expect("int cast"); + } + // Apply destination mode if specified (clonefile copies source permissions) + if let Some(mode) = self.destination_mode { + match bun_sys::chmod( + self.destination_file_store + .pathlike + .path() + .slice_z(&mut path_buf), + mode, + ) { + bun_sys::Result::Err(err) => { + self.system_error = Some(err.to_system_error()); + return; } + bun_sys::Result::Ok(()) => {} } - return; - } - Err(_) => { - // this may still fail, in which case we just continue trying with fcopyfile - // it can fail when the input file already exists - // or if the output is not a directory - // or if it's a network volume - self.system_error = None; } + return; + } + Err(_) => { + // this may still fail, in which case we just continue trying with fcopyfile + // it can fail when the input file already exists + // or if the output is not a directory + // or if it's a network volume + self.system_error = None; } } } } - - if self.do_open_file::<{ IOWhich::Both }>().is_err() { - return; - } - // Do we need to open only one file? - } else if self.destination_fd == Fd::INVALID { - self.source_fd = self.source_file_store.pathlike.fd(); - - if self.do_open_file::<{ IOWhich::Destination }>().is_err() { - return; - } - // Do we need to open only one file? - } else if self.source_fd == Fd::INVALID { - self.destination_fd = self.destination_file_store.pathlike.fd(); - - if self.do_open_file::<{ IOWhich::Source }>().is_err() { - return; - } } - if self.system_error.is_some() { + if self.do_open_file::<{ IOWhich::Both }>().is_err() { return; } + // Do we need to open only one file? + } else if self.destination_fd == Fd::INVALID { + self.source_fd = self.source_file_store.pathlike.fd(); - 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 + if self.do_open_file::<{ IOWhich::Destination }>().is_err() { + return; } + // Do we need to open only one file? + } else if self.source_fd == Fd::INVALID { + self.destination_fd = self.destination_file_store.pathlike.fd(); - let stat: Stat = match stat_ { - Some(s) => s, - None => match bun_sys::fstat(self.source_fd) { - bun_sys::Result::Ok(result) => result, - bun_sys::Result::Err(err) => { - self.do_close(); - self.system_error = Some(err.to_system_error()); - return; - } - }, - }; - - if bun_sys::S::ISDIR(stat.st_mode as _) { - self.system_error = Some(unsupported_directory_error()); - self.do_close(); + if self.do_open_file::<{ IOWhich::Source }>().is_err() { return; } + } - // BSD fstat on a pipe reports bytes currently buffered in st_size; - // only a regular-file st_size is a length. - if stat.st_size != 0 && bun_sys::S::ISREG(stat.st_mode as _) { - self.max_length = (SizeType::try_from(stat.st_size) - .expect("int cast") - .min(self.max_length)) - .max(self.offset) - - self.offset; - if self.max_length == 0 { + if self.system_error.is_some() { + return; + } + + 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) { + bun_sys::Result::Ok(result) => result, + bun_sys::Result::Err(err) => { self.do_close(); + self.system_error = Some(err.to_system_error()); return; } + }, + }; - if PREALLOCATE_SUPPORTED - && matches!( - self.destination_file_store.pathlike, - PathOrFileDescriptor::Path(_) - ) - && self.max_length > PREALLOCATE_LENGTH - && self.max_length != MAX_SIZE - { - let _ = bun_sys::preallocate_file( - self.destination_fd.native(), - 0, - self.max_length as i64, - ); - } + if bun_sys::S::ISDIR(stat.st_mode as _) { + self.system_error = Some(unsupported_directory_error()); + self.do_close(); + return; + } + + // BSD fstat on a pipe reports bytes currently buffered in st_size; + // only a regular-file st_size is a length. + if stat.st_size != 0 && bun_sys::S::ISREG(stat.st_mode as _) { + self.max_length = (SizeType::try_from(stat.st_size) + .expect("int cast") + .min(self.max_length)) + .max(self.offset) + - self.offset; + if self.max_length == 0 { + self.do_close(); + return; } - #[cfg(any(target_os = "linux", target_os = "android"))] + if PREALLOCATE_SUPPORTED + && matches!( + self.destination_file_store.pathlike, + PathOrFileDescriptor::Path(_) + ) + && self.max_length > PREALLOCATE_LENGTH + && self.max_length != MAX_SIZE { - // Bun.write(Bun.file("a"), Bun.file("b")) - if bun_sys::S::ISREG(stat.st_mode as _) - && (bun_sys::S::ISREG(self.destination_file_store.mode as _) - || self.destination_file_store.mode == 0) - { - if self.destination_file_store.is_atty.unwrap_or(false) { - let _ = self.do_copy_file_range::<{ TryWith::CopyFileRange }, true>(); - } else { - let _ = self.do_copy_file_range::<{ TryWith::CopyFileRange }, false>(); - } + let _ = bun_sys::preallocate_file( + self.destination_fd.native(), + 0, + self.max_length as i64, + ); + } + } - self.do_close(); - return; + #[cfg(any(target_os = "linux", target_os = "android"))] + { + // Bun.write(Bun.file("a"), Bun.file("b")) + if bun_sys::S::ISREG(stat.st_mode as _) + && (bun_sys::S::ISREG(self.destination_file_store.mode as _) + || self.destination_file_store.mode == 0) + { + if self.destination_file_store.is_atty.unwrap_or(false) { + let _ = self.do_copy_file_range::<{ TryWith::CopyFileRange }, true>(); + } else { + let _ = self.do_copy_file_range::<{ TryWith::CopyFileRange }, false>(); } - // $ bun run foo.js | bun run bar.js - if bun_sys::S::ISFIFO(stat.st_mode as _) - && bun_sys::S::ISFIFO(self.destination_file_store.mode as _) - { - if self.destination_file_store.is_atty.unwrap_or(false) { - let _ = self.do_copy_file_range::<{ TryWith::Splice }, true>(); - } else { - let _ = self.do_copy_file_range::<{ TryWith::Splice }, false>(); - } + self.do_close(); + return; + } - self.do_close(); - return; + // $ bun run foo.js | bun run bar.js + if bun_sys::S::ISFIFO(stat.st_mode as _) + && bun_sys::S::ISFIFO(self.destination_file_store.mode as _) + { + if self.destination_file_store.is_atty.unwrap_or(false) { + let _ = self.do_copy_file_range::<{ TryWith::Splice }, true>(); + } else { + let _ = self.do_copy_file_range::<{ TryWith::Splice }, false>(); } - if bun_sys::S::ISREG(stat.st_mode as _) - || bun_sys::S::ISCHR(stat.st_mode as _) - || bun_sys::S::ISSOCK(stat.st_mode as _) - { - if self.destination_file_store.is_atty.unwrap_or(false) { - let _ = self.do_copy_file_range::<{ TryWith::Sendfile }, true>(); - } else { - let _ = self.do_copy_file_range::<{ TryWith::Sendfile }, false>(); - } + self.do_close(); + return; + } - self.do_close(); - return; + if bun_sys::S::ISREG(stat.st_mode as _) + || bun_sys::S::ISCHR(stat.st_mode as _) + || bun_sys::S::ISSOCK(stat.st_mode as _) + { + if self.destination_file_store.is_atty.unwrap_or(false) { + let _ = self.do_copy_file_range::<{ TryWith::Sendfile }, true>(); + } else { + let _ = self.do_copy_file_range::<{ TryWith::Sendfile }, false>(); } - self.system_error = Some(unsupported_non_regular_file_error()); self.do_close(); return; } - #[cfg(target_os = "macos")] - { - // fcopyfile rewrites dest from offset 0 and the slice trim is - // ftruncate; both are only safe for a dest Bun opened O_TRUNC. - if matches!( - self.destination_file_store.pathlike, - PathOrFileDescriptor::Path(_) + self.system_error = Some(unsupported_non_regular_file_error()); + self.do_close(); + return; + } + + #[cfg(target_os = "macos")] + { + // fcopyfile rewrites dest from offset 0 and the slice trim is + // ftruncate; both are only safe for a dest Bun opened O_TRUNC. + if matches!( + self.destination_file_store.pathlike, + PathOrFileDescriptor::Path(_) + ) { + let copied = match self.do_fcopy_file_with_read_write_loop_fallback( + u64::try_from(stat.st_size).expect("int cast"), ) { - let copied = match self.do_fcopy_file_with_read_write_loop_fallback( - u64::try_from(stat.st_size).expect("int cast"), - ) { - Ok(copied) => copied, - Err(_) => { - self.do_close(); - return; - } - }; - if stat.st_size != 0 - && SizeType::try_from(stat.st_size).expect("int cast") > self.max_length - { - let _ = bun_sys::ftruncate( - self.destination_fd, - i64::try_from(self.max_length).expect("int cast"), - ); - self.read_len = copied.min(self.max_length as u64) as SizeType; - } else { - self.read_len = copied as SizeType; + Ok(copied) => copied, + Err(_) => { + self.do_close(); + return; } - } else if self.do_read_write_loop_capped(self.max_length).is_err() { - self.do_close(); - return; + }; + if stat.st_size != 0 + && SizeType::try_from(stat.st_size).expect("int cast") > self.max_length + { + let _ = bun_sys::ftruncate( + self.destination_fd, + i64::try_from(self.max_length).expect("int cast"), + ); + self.read_len = copied.min(self.max_length as u64) as SizeType; + } else { + self.read_len = copied as SizeType; } - + } else if self.do_read_write_loop_capped(self.max_length).is_err() { self.do_close(); return; } - #[cfg(target_os = "freebsd")] - { - if matches!( - self.destination_file_store.pathlike, - PathOrFileDescriptor::Path(_) + self.do_close(); + return; + } + + #[cfg(target_os = "freebsd")] + { + if matches!( + self.destination_file_store.pathlike, + PathOrFileDescriptor::Path(_) + ) { + let mut total_written: u64 = 0; + match node_fs::NodeFS::copy_file_using_read_write_loop( + bun_core::ZStr::EMPTY, + bun_core::ZStr::EMPTY, + self.source_fd, + self.destination_fd, + 0, + &mut total_written, ) { - let mut total_written: u64 = 0; - match node_fs::NodeFS::copy_file_using_read_write_loop( - bun_core::ZStr::EMPTY, - bun_core::ZStr::EMPTY, - self.source_fd, - self.destination_fd, - 0, - &mut total_written, - ) { - bun_sys::Result::Err(err) => { - self.system_error = Some(err.to_system_error()); - self.do_close(); - return; - } - bun_sys::Result::Ok(()) => {} - } - if stat.st_size != 0 - && SizeType::try_from(stat.st_size).expect("int cast") > self.max_length - { - let _ = bun_sys::ftruncate( - self.destination_fd, - i64::try_from(self.max_length).expect("int cast"), - ); - self.read_len = total_written.min(self.max_length as u64) as SizeType; - } else { - self.read_len = total_written as SizeType; + bun_sys::Result::Err(err) => { + self.system_error = Some(err.to_system_error()); + self.do_close(); + return; } - } else if self.do_read_write_loop_capped(self.max_length).is_err() { - self.do_close(); - return; + bun_sys::Result::Ok(()) => {} + } + if stat.st_size != 0 + && SizeType::try_from(stat.st_size).expect("int cast") > self.max_length + { + let _ = bun_sys::ftruncate( + self.destination_fd, + i64::try_from(self.max_length).expect("int cast"), + ); + self.read_len = total_written.min(self.max_length as u64) as SizeType; + } else { + self.read_len = total_written as SizeType; } + } else if self.do_read_write_loop_capped(self.max_length).is_err() { self.do_close(); return; } + self.do_close(); + return; + } - #[cfg(not(any( - target_os = "linux", - target_os = "android", - target_os = "macos", - target_os = "freebsd" - )))] - { - compile_error!("TODO: implement copyfile"); + #[cfg(windows)] + { + 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 mut dest_wbuf = bun_paths::w_path_buffer_pool::get(); + let mut source_wbuf = bun_paths::w_path_buffer_pool::get(); + let dest_w = + bun_paths::string_paths::to_kernel32_path(&mut dest_wbuf[..], dest_path.as_bytes()); + let source_w = + bun_paths::string_paths::to_kernel32_path(&mut source_wbuf[..], source_path.as_bytes()); + + loop { + // SAFETY: both paths are NUL-terminated. + if unsafe { w::CopyFileW(source_w.as_ptr(), dest_w.as_ptr(), 0) } != 0 { + break; + } + let mut 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()) { + Ok(()) => continue, + Err(mkdir_err) => return CopyByPath::Failed(mkdir_err.to_system_error()), + } + } } + // https://github.com/oven-sh/bun/issues/6336 + E::EPERM => err = bun_sys::Error::from_code(E::ENOENT, bun_sys::Tag::copyfile), + _ => {} + } + 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::Arguments::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"))] @@ -981,7 +1100,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, @@ -1023,15 +1141,11 @@ fn read_write_loop_capped( // drop. No explicit `Drop` impl is needed. // Kept local until bun_sys exports these; values match crate::node::fs. -#[cfg(not(windows))] 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)] @@ -1052,888 +1166,6 @@ impl TryWith { } } -// ─────────────────────────────────────────────────────────────────────────── -// CopyFileWindows (libuv async) -// ─────────────────────────────────────────────────────────────────────────── - -#[cfg(windows)] -pub 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, - - 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 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 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, - 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, - 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 fn throw(&mut self, err: bun_sys::Error) { - 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 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::Arguments::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, - }; - // callback signature to match `ManagedTask::new`'s `fn(*mut T) -> jsc::JsResult<()>`. - fn call_erased(this: *mut CopyFileWindows<'_>) -> bun_event_loop::JsResult<()> { - // SAFETY: `this` is the heap-allocated `CopyFileWindows` passed to - // `ManagedTask::new` below; `on_mkdirp_complete` may free it via `throw`, so we - // do not touch `this` afterward. - unsafe { (*this).on_mkdirp_complete() }; - Ok(()) - } - ticket.post(jsc::ConcurrentTask::create( - jsc::ManagedTask::ManagedTask::new::(this, call_erased), - )); -} - // ─────────────────────────────────────────────────────────────────────────── // IOWhich + module-level constants // ─────────────────────────────────────────────────────────────────────────── @@ -1945,7 +1177,6 @@ pub enum IOWhich { 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 788a13fe672a..5eb648e12d2c 100644 --- a/src/runtime/webcore/blob/read_file.rs +++ b/src/runtime/webcore/blob/read_file.rs @@ -1,53 +1,31 @@ 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; 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 @@ -82,7 +60,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 trait ReadFileCompletion { /// # Safety @@ -145,7 +123,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 struct ReadFileCompletionFns { @@ -185,7 +163,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 struct ReadFileRead { @@ -251,7 +229,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). @@ -272,28 +249,24 @@ pub struct ReadFile { pub(crate) file_store: FileStore, pub(crate) store: Option>, 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 @@ -319,44 +292,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, @@ -384,9 +332,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, @@ -506,7 +458,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], @@ -531,13 +482,15 @@ 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); @@ -553,6 +506,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. @@ -635,30 +589,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; @@ -678,7 +621,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, @@ -731,7 +673,6 @@ impl ReadFile { } } - #[cfg(not(windows))] fn run_async_with_fd(&mut self, fd: Fd) { if self.errno.is_some() { self.on_finish(); @@ -781,6 +722,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(); @@ -800,618 +742,113 @@ impl ReadFile { this.update(); } - #[cfg(not(windows))] fn do_read_loop(&mut self) { - #[cfg(not(windows))] - { - // we hold a 64 KB stack buffer incase the amount of data to - // be read is greater than the reported amount - // - // 64 KB is large, but since this is running in a thread - // with it's own stack, it should have sufficient space. - let mut stack_storage = bun_core::vec::UninitBuf::<{ 64 * 1024 }>::uninit(); - // SAFETY: only `do_read` writes into it and only `stack_buffer[..read_amount]` is read back. - let stack_buffer = unsafe { stack_storage.as_bytes_mut() }; - // `do_read` never touches `self.buffer`; move it out so the read - // target slice (which may point into its spare capacity) can be - // held as a safe `&mut [u8]` across the `&mut self` call. - let mut buffer = core::mem::take(&mut self.buffer); - while self.state.load(Ordering::Relaxed) == ClosingState::Running as u8 { - let (use_stack, buf) = Self::remaining_buffer( - &mut buffer, - stack_buffer, - self.max_length, - self.read_off, - ); - - if !buf.is_empty() && self.errno.is_none() && !self.read_eof { - let mut read_amount: usize = 0; - let mut retry = false; - let continue_reading = self.do_read(buf, &mut read_amount, &mut retry); - - // We might read into the stack buffer, so we need to copy it into the heap. - if use_stack { - // `do_read` initialized exactly `stack_buffer[..read_amount]` (0 on error/retry). - let read = &stack_buffer[..read_amount]; - if buffer.capacity() == 0 { - // We need to allocate a new buffer - // In this case, we want to use `ensureTotalCapacityPrecise` so that it's an exact amount - // We want to avoid over-allocating incase it's a large amount of data sent in a single chunk followed by a 0 byte chunk. - buffer.reserve_exact(read.len()); - } else { - buffer.reserve(read.len()); - } - buffer.extend_from_slice(read); + // we hold a 64 KB stack buffer incase the amount of data to + // be read is greater than the reported amount + // + // 64 KB is large, but since this is running in a thread + // with it's own stack, it should have sufficient space. + let mut stack_storage = bun_core::vec::UninitBuf::<{ 64 * 1024 }>::uninit(); + // SAFETY: only `do_read` writes into it and only `stack_buffer[..read_amount]` is read back. + let stack_buffer = unsafe { stack_storage.as_bytes_mut() }; + // `do_read` never touches `self.buffer`; move it out so the read + // target slice (which may point into its spare capacity) can be + // held as a safe `&mut [u8]` across the `&mut self` call. + let mut buffer = core::mem::take(&mut self.buffer); + while self.state.load(Ordering::Relaxed) == ClosingState::Running as u8 { + let (use_stack, buf) = + Self::remaining_buffer(&mut buffer, stack_buffer, self.max_length, self.read_off); + + if !buf.is_empty() && self.errno.is_none() && !self.read_eof { + let mut read_amount: usize = 0; + let mut retry = false; + let continue_reading = self.do_read(buf, &mut read_amount, &mut retry); + + // We might read into the stack buffer, so we need to copy it into the heap. + if use_stack { + // `do_read` initialized exactly `stack_buffer[..read_amount]` (0 on error/retry). + let read = &stack_buffer[..read_amount]; + if buffer.capacity() == 0 { + // We need to allocate a new buffer + // In this case, we want to use `ensureTotalCapacityPrecise` so that it's an exact amount + // We want to avoid over-allocating incase it's a large amount of data sent in a single chunk followed by a 0 byte chunk. + buffer.reserve_exact(read.len()); } else { - // record the amount of data read - // SAFETY: read() wrote `read_amount` initialized bytes into spare capacity. - unsafe { bun_core::vec::commit_spare(&mut buffer, read_amount) }; - } - // - If they DID set a max length, we should stop - // reading after that. - // - // - If they DID NOT set a max_length, then it will - // be Blob.max_size which is an impossibly large - // amount to read. - if !self.read_eof && buffer.len() >= self.max_length as usize { - break; + buffer.reserve(read.len()); } + buffer.extend_from_slice(read); + } else { + // record the amount of data read + // SAFETY: read() wrote `read_amount` initialized bytes into spare capacity. + unsafe { bun_core::vec::commit_spare(&mut buffer, read_amount) }; + } + // - If they DID set a max length, we should stop + // reading after that. + // + // - If they DID NOT set a max_length, then it will + // be Blob.max_size which is an impossibly large + // amount to read. + if !self.read_eof && buffer.len() >= self.max_length as usize { + break; + } - if !continue_reading { - // Stop reading, we errored - break; - } + if !continue_reading { + // Stop reading, we errored + break; + } - // If it's not a regular file, it might be something - // which would block on the next read. So we should - // avoid immediately reading again until the next time - // we're scheduled to read. - // - // An example of where this happens is stdin. - // - // await Bun.stdin.text(); - // - // If we immediately call read(), it will block until stdin is - // readable. - 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 it's not a regular file, it might be something + // which would block on the next read. So we should + // avoid immediately reading again until the next time + // we're scheduled to read. + // + // An example of where this happens is stdin. + // + // await Bun.stdin.text(); + // + // 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 self.could_block + // 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 - { - match bun_core::is_readable(self.opened_fd) { - bun_core::Pollable::NotReady => {} - bun_core::Pollable::Ready | bun_core::Pollable::Hup => continue, - } + match bun_core::is_readable(self.opened_fd) { + bun_core::Pollable::NotReady => {} + bun_core::Pollable::Ready | bun_core::Pollable::Hup => continue, } - self.read_eof = false; - self.buffer = buffer; - self.wait_for_readable(); - - return; } + self.read_eof = false; + self.buffer = buffer; + self.wait_for_readable(); - // There can be more to read - continue; - } - - // -- We are done reading. - break; - } - self.buffer = buffer; - - if self.system_error.is_some() { - self.buffer = Vec::new(); // clearAndFree - } - - // If we over-allocated by a lot, we should shrink the buffer to conserve memory. - if self.buffer.len() + 16_000 < self.buffer.capacity() { - self.buffer.shrink_to_fit(); - } - self.on_finish(); - } - } -} - -// ────────────────────────────────────────────────────────────────────────── -// ReadFileUV (Windows) -// ────────────────────────────────────────────────────────────────────────── - -#[cfg(windows)] -pub 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, - - 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, - store: RefPtr, - off: SizeType, - max_len: SizeType, - handler: *mut C, - ) { - Self::start_with_ctx( - event_loop, - 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, - 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, - 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(). - let _ = this_ptr; - } - - pub 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 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(); + // There can be more to read + continue; } - } 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(); + // -- We are done reading. + break; } - } + self.buffer = buffer; - 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 self.system_error.is_some() { + self.buffer = Vec::new(); // clearAndFree } - 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; + // If we over-allocated by a lot, we should shrink the buffer to conserve memory. + if self.buffer.len() + 16_000 < self.buffer.capacity() { + self.buffer.shrink_to_fit(); } - - 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(); + self.on_finish(); } } diff --git a/src/runtime/webcore/blob/write_file.rs b/src/runtime/webcore/blob/write_file.rs index 9de9be8c72bf..d79d13a05f19 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; @@ -14,10 +12,9 @@ use bun_sys::{self as sys, Fd}; use bun_threading::{IntrusiveWorkTask as _, WorkPool, WorkPoolTask}; use crate::webcore::blob::{ - self, Blob, FileOpener, MkdirpTarget, Retry, SizeType, mkdir_if_not_exists, + self, Blob, ClosingState, FileCloser, FileOpener, MkdirpTarget, Retry, SizeType, + mkdir_if_not_exists, }; -#[cfg(not(windows))] -use crate::webcore::blob::{ClosingState, FileCloser}; use crate::webcore::body; bun_output::declare_scope!(WriteFile, hidden); @@ -27,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, @@ -88,14 +85,12 @@ impl WriteFile { pub 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, @@ -107,6 +102,9 @@ pub struct WriteFile { pub(crate) on_complete_callback: WriteFileOnWriteFileCallback, 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, @@ -155,22 +153,6 @@ 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!() - } } impl MkdirpTarget for WriteFile { @@ -275,7 +257,6 @@ impl WriteFile { io::IoRequestLoop::schedule(&mut self.io_request); } - #[cfg(not(windows))] pub(crate) fn create_with_ctx( file_blob: Blob, bytes_blob: Blob, @@ -295,13 +276,17 @@ 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), on_complete_ctx: on_write_file_context, on_complete_callback, total_written: 0, + #[cfg(not(windows))] could_block: false, close_after_io: false, mkdirp_if_not_exists, @@ -309,7 +294,6 @@ impl WriteFile { Ok(write_file) } - #[cfg(not(windows))] pub(crate) fn create( file_blob: Blob, bytes_blob: Blob, @@ -331,7 +315,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); @@ -349,7 +332,9 @@ 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()); @@ -380,25 +365,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 @@ -411,7 +385,6 @@ impl WriteFile { .is_path() } - #[cfg(not(windows))] fn on_finish(&mut self) { bun_output::scoped_log!(WriteFile, "WriteFile.onFinish()"); @@ -426,43 +399,46 @@ 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); + #[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"))] @@ -503,16 +479,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 @@ -522,11 +488,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 { @@ -550,646 +518,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}; - -#[cfg(windows)] -mod windows_impl { - use super::*; - use core::ptr::null_mut; - - use bun_io::{self as aio, IntrusiveUvFs as _, KeepAlive}; - // `bun_jsc::EventLoop`/`ManagedTask` are *modules* (namespace - // re-exports); the structs live one level deeper. - use bun_jsc::{ConcurrentTask, ManagedTask::ManagedTask, 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, - 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) - } - } - - impl WriteFileWindows { - pub(crate) fn create_with_ctx( - file_blob: Blob, - bytes_blob: Blob, - event_loop: *mut EventLoop, - 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, - 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)), - } - } - } - - /// `ManagedTask`-shaped trampoline for [`on_mkdirp_complete`]: takes - /// `*mut Self` and returns the event-loop `jsc::JsResult<()>` (always `Ok`: the inner body - /// reports a delivery exception itself). - fn on_mkdirp_complete_task(this: *mut WriteFileWindows) -> bun_event_loop::JsResult<()> { - // SAFETY: `this` is the live Box-allocated `WriteFileWindows` whose - // pointer was stashed in `on_mkdirp_complete_concurrent` below; - // the JS thread is the sole accessor at this point. `*this` may be - // freed inside; not accessed afterward. - unsafe { Self::on_mkdirp_complete(this) }; - Ok(()) - } - - 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( - ManagedTask::new::(this, Self::on_mkdirp_complete_task), - )); - } - - 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) = unsafe { ((*this).on_complete_callback, (*this).on_complete_ctx) }; - - // 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, - 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, - context.cast::(), - callback, - mkdirp_if_not_exists, - ) - } - } -} - // ────────────────────────────────────────────────────────────────────────── pub struct WriteFilePromise { diff --git a/src/runtime/webview/ChromeBackend.cpp b/src/runtime/webview/ChromeBackend.cpp index 98d5482a215b..4112f727f3fc 100644 --- a/src/runtime/webview/ChromeBackend.cpp +++ b/src/runtime/webview/ChromeBackend.cpp @@ -503,7 +503,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 3bacdc7489ec..8273daaf827c 100644 --- a/src/runtime/webview/ChromeBackend.h +++ b/src/runtime/webview/ChromeBackend.h @@ -320,7 +320,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 3e8967d46bf6..fbadf9c8e82e 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, ReadEvent}; 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_owned(event_loop.r#loop(), ours, 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 => { + 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]>), @@ -935,7 +914,7 @@ impl PipeEvent { generation, event: self, })); - // 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::ManagedTask::ManagedTask::new_owned( @@ -970,55 +949,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)] @@ -1027,22 +957,29 @@ 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(), + instance, + Some(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 5c285b84c372..ee23d5f525e2 100644 --- a/src/shell_parser/parse.rs +++ b/src/shell_parser/parse.rs @@ -502,7 +502,7 @@ pub mod ast { // `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 + // `bun_sys::open` → `bun_sys::windows::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. 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/lib.rs b/src/spawn/lib.rs index d963cea5a19a..ebb42261b2fb 100644 --- a/src/spawn/lib.rs +++ b/src/spawn/lib.rs @@ -26,11 +26,7 @@ pub mod posix_spawn { pub mod bun_spawn { pub use crate::process; - pub use crate::process::{ - Process, SpawnOptions, SpawnProcessResult, Status, spawn_process, sync, - }; - #[cfg(windows)] - pub use crate::process::{WindowsSpawnOptions, WindowsSpawnResult}; + pub use crate::process::{Process, SpawnOptions, Status, spawn_process, sync}; pub use bun_spawn_sys::posix_spawn::bun_spawn::*; } } @@ -62,7 +58,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, SignalCodeExt, SpawnEnv, - SpawnOptions, SpawnProcessResult, SpawnResultExt, Status, StdioKind, WaiterThread, + SpawnOptions, SpawnResult, SpawnResultExt, Status, Stdio, StdioKind, WaiterThread, spawn_process, spawn_process_cstr, }; @@ -83,7 +79,6 @@ bun_dispatch::link_interface! { CronRemove, ChromeProcess, HostProcess, - SyncWindows, ] { fn on_process_exit(process: &mut Process, status: Status, rusage: &Rusage); } @@ -92,44 +87,10 @@ 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. -#[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"), - } -} -#[cfg(unix)] -pub use process::{PosixSpawnOptions, PosixSpawnResult, PosixStdio as Stdio, WaitPidResult}; #[cfg(unix)] -pub type SpawnResult = process::PosixSpawnResult; - +pub use process::WaitPidResult; #[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::WindowsOptions; /// Blocking (synchronous) spawn helpers. pub mod sync { @@ -150,23 +111,13 @@ 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))] + /// The parent's end of a child's stdio slot, when spawn made a pipe for it. 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 bb4f39e62940..9dd51447f519 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)] @@ -31,9 +28,6 @@ use bun_spawn_sys::posix_spawn::posix_spawn; /// is `u32` there; `Status::from` casts before matching. #[cfg(unix)] pub use posix_spawn::WaitPidResult; -#[cfg(windows)] -#[derive(Clone, Copy)] -pub struct 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 @@ -53,15 +47,15 @@ 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. -pub use bun_spawn_sys::spawn_process::rusage_zeroed; +// 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(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, PidFdType, PidT, PosixSpawnOptions, PosixSpawnResult, - PosixStdio, Rusage, StdioKind, + Argv, CStrPtr, Dup2, Envp, ExtraPipe, PidFdType, PidT, Rusage, SpawnOptions, SpawnResult, + Stdio, StdioKind, }; /// Whether the process-exit poll should be registered one-shot. @@ -93,13 +87,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, @@ -114,9 +101,9 @@ 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` + +// bun.ptr.ThreadSafeRefCount → intrusive (a raw `*mut Process` travels through +// the event loop's poll/wait callbacks and the waiter thread). Per PORTING.md +// §Pointers, keep the embedded count; the derive emits `ThreadSafeRefCounted` + // `AnyRefCounted`. Default `destructor` (`heap::take`) applies — `Drop` below // handles `poller.deinit()`. #[derive(bun_ptr::ThreadSafeRefCounted)] @@ -124,6 +111,13 @@ 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, @@ -140,6 +134,8 @@ impl Drop for Process { /// above; this `Drop` body covers the `poller.deinit()` call. fn drop(&mut self) { self.poller.deinit(); + #[cfg(windows)] + self.close_process_handle(); } } @@ -272,13 +268,10 @@ 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 { + pub(crate) fn init_posix(posix: &SpawnResult, event_loop: EventLoopHandle) -> *mut Process { let status = 'brk: { if posix.has_exited { let mut rusage = rusage_zeroed(); @@ -301,6 +294,45 @@ impl Process { })) } + #[cfg(windows)] + pub(crate) fn init_windows(spawned: &SpawnResult, event_loop: EventLoopHandle) -> *mut Process { + bun_core::heap::into_raw(Box::new(Process { + ref_count: bun_ptr::ThreadSafeRefCount::init(), + pid: spawned.pid, + process_handle: spawned.process_handle, + exit_signal: 0, + event_loop, + poller: Poller::Detached, + status: Status::Running, + exit_handler: ProcessExitHandler::default(), + })) + } + + /// 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; + } + } + // has_exited / has_killed / signal_code live in the always-on impl above. pub fn on_exit(&mut self, status: Status, rusage: &Rusage) { @@ -324,11 +356,47 @@ impl Process { 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; + // SAFETY: `exit_code` is a valid out-pointer. + let status = if unsafe { win32::GetExitCodeProcess(self.process_handle, &mut exit_code) } + == 0 + { + Status::Err(win32::last_error(bun_sys::Tag::waitpid)) + } else if self.exit_signal > 0 && self.exit_signal < bun_core::SignalCode::SIGSYS as u8 { + Status::Signaled(self.exit_signal) + } else { + Status::Exited(Exited { + code: exit_code as u8, + signal: 0, + raw: exit_code, + }) + }; + bun_core::scoped_log!(PROCESS, "Process.onExit({}) {}", self.pid, status); + self.on_exit(status, &rusage); + } + + #[cfg(windows)] + pub(crate) fn wait_windows(&mut self, sync_: bool) { + 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(); + } + } + pub fn wait(&mut self, sync_: bool) { #[cfg(unix)] self.wait_posix(sync_); #[cfg(windows)] - let _ = sync_; + self.wait_windows(sync_); } /// # Safety @@ -432,9 +500,57 @@ 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_) }; + 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(()); } @@ -537,93 +653,6 @@ 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 = - if term_signal > 0 && term_signal < bun_core::SignalCode::SIGSYS as c_int { - Some(term_signal as u8) - } else { - None - }; - - 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)] { @@ -644,21 +673,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(event_loop_handle_to_ctx(self.event_loop)); + 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"))] @@ -724,20 +761,14 @@ 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(()); + } + match bun_spawn_sys::windows::kill(self.process_handle, c_int::from(signal)) { + 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), } } @@ -947,39 +978,150 @@ pub type Poller = PollerPosix; #[cfg(windows)] pub type Poller = PollerWindows; +/// 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)] +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()) }; +} + +/// 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)] +pub 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; + } + } +} + +#[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); + } + + /// # 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, + _entry: *mut iocp::OverlappedEntry, + ) { + // 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(event_loop_handle_to_ctx((*process).event_loop)); + } + (*process).on_process_signaled(); + } + } +} + #[cfg(windows)] pub enum PollerWindows { - Uv(uv::uv_process_t), + /// Watching the process handle through the loop's completion port. + Wait(core::ptr::NonNull, KeepAlive), Detached, } #[cfg(windows)] impl PollerWindows { - /// Not `Drop` — see `PollerPosix::deinit`. + /// Not `Drop` — see `PollerPosix::deinit`. A watch holds a ref on the + /// `Process`, so none is left by the time it is dropped. pub(crate) fn deinit(&mut self) { - if let PollerWindows::Uv(p) = self { - debug_assert!(p.is_closed()); - } + debug_assert!(matches!(self, PollerWindows::Detached)); } - pub(crate) fn enable_keeping_event_loop_alive(&mut self, _event_loop: bun_io::EventLoopCtx) { - match self { - PollerWindows::Uv(process) => { - process.ref_(); - } - _ => {} + pub(crate) fn enable_keeping_event_loop_alive(&mut self, ctx: bun_io::EventLoopCtx) { + if let PollerWindows::Wait(_, keep_alive) = self { + keep_alive.ref_(ctx); } } - 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(); - } - _ => {} + pub(crate) fn disable_keeping_event_loop_alive(&mut self, ctx: bun_io::EventLoopCtx) { + if let PollerWindows::Wait(_, keep_alive) = self { + keep_alive.unref(ctx); } } } @@ -1487,265 +1629,7 @@ impl WaiterThread { pub fn set_should_use_waiter_thread() {} } -// (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. @@ -1758,29 +1642,17 @@ pub trait SpawnResultExt: Sized { } } -#[cfg(unix)] -impl SpawnResultExt for PosixSpawnResult { +impl SpawnResultExt for SpawnResult { 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)) } + #[cfg(unix)] + let process = Process::init_posix(&self, event_loop); + #[cfg(windows)] + let process = Process::init_windows(&self, event_loop); + // SAFETY: both heap-allocate the `Process` with its initial ref. + unsafe { RefPtr::from_raw(process) } } } -#[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 { @@ -1789,6 +1661,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). @@ -1824,21 +1698,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) } } @@ -1857,7 +1732,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() @@ -1866,7 +1741,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)] @@ -1886,455 +1760,6 @@ 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 @@ -2343,7 +1768,7 @@ mod spawn_process_body { // 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 type WindowsOptions = bun_spawn_sys::WindowsOptions; pub struct Options { pub stdin: SyncStdio, @@ -2362,8 +1787,6 @@ mod spawn_process_body { #[cfg(windows)] pub windows: WindowsOptions, - #[cfg(not(windows))] - pub windows: (), } #[derive(Clone, Copy, PartialEq, Eq)] @@ -2374,46 +1797,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 { @@ -2429,8 +1821,6 @@ mod spawn_process_body { argv0: None, #[cfg(windows)] windows: Default::default(), - #[cfg(not(windows))] - windows: (), } } } @@ -2449,9 +1839,7 @@ mod spawn_process_body { argv0: self.argv0, new_process_group, #[cfg(windows)] - windows: self.windows.clone(), - #[cfg(not(windows))] - windows: (), + windows: self.windows, ..Default::default() } } @@ -2469,411 +1857,264 @@ 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; + /// `result` is 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: 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, + }; - // 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() }; + struct ProcessHandle(bun_sys::windows::HANDLE); + impl Drop for ProcessHandle { + fn drop(&mut self) { + // SAFETY: the process handle of the result is ours. + unsafe { win32::CloseHandle(self.0) }; + } } + let process = ProcessHandle(spawned.process_handle); - Ok(Ok(Result { - // SAFETY: process has exited; no further mutation. - status: unsafe { (*process).status.clone() }, - stdout: Vec::new(), - stderr: Vec::new(), - })) - } + // Nothing is written to the child's stdin: end it. + if let Some(stdin) = spawned.stdin { + stdin.close(); + } - #[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 - )); + let kill_child = || { + let _ = + bun_spawn_sys::windows::kill(process.0, 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.0, win32::INFINITE) != win32::WAIT_OBJECT_0 { + return Ok(Err(win32::last_error(bun_sys::Tag::waitpid))); + } + let mut exit_code: u32 = 0; + // SAFETY: `exit_code` is a valid out-pointer. + if unsafe { win32::GetExitCodeProcess(process.0, &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 as u8, + signal: 0, + raw: exit_code, + }), + stdout: core::mem::take(&mut stdout.bytes), + stderr: core::mem::take(&mut stderr.bytes), + })) } pub fn spawn_with_argv( @@ -2882,15 +2123,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) @@ -2899,7 +2132,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 { @@ -3413,7 +2650,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..e2af1375a592 100644 --- a/src/spawn/static_pipe_writer.rs +++ b/src/spawn/static_pipe_writer.rs @@ -1,8 +1,6 @@ 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; @@ -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. @@ -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), } @@ -112,8 +109,7 @@ impl StaticPipeWriter

{ result: StdioResult, 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,32 @@ 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. + use bun_sys::FdExt as _; + 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 +236,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 5552599afeea..c97eb9ee7dd1 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 do — keeps the IoCounters alias valid on non-Windows so the flat re-export in lib.rs stays un-cfg'd 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 ef83467c37d3..e631dddfcb1b 100644 --- a/src/spawn_sys/lib.rs +++ b/src/spawn_sys/lib.rs @@ -1,18 +1,20 @@ //! `bun_spawn_sys` — raw OS process-spawn layer split out of `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_windows_sys` and `bun_analytics`. There is **no** //! `bun_event_loop`/`bun_io`/`bun_io`/`bun_threading` dependency — `Process`, //! `Poller`, `WaiterThread`, and the `sync` runner stay in `bun_spawn` and //! depend on this crate. @@ -37,6 +39,10 @@ pub mod posix_spawn; #[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`) // @@ -113,7 +119,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::WaiterThread` reads/writes through these. // ────────────────────────────────────────────────────────────────────────── pub mod waiter_thread_flag { @@ -138,7 +144,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 @@ -184,11 +190,13 @@ pub mod pdeathsig { // Public surface — flat re-exports so `bun_spawn` can `pub use bun_spawn_sys::*`. // ────────────────────────────────────────────────────────────────────────── +#[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, FdT, IoCounters, PidFdType, PidT, PosixSpawnOptions, PosixSpawnResult, - PosixStdio, Rusage, RusageFields, StdioKind, WinRusage, WinTimeval, rusage_zeroed, + Dup2, ExtraPipe, FdT, IoCounters, PidFdType, PidT, Rusage, RusageFields, SpawnOptions, + SpawnResult, Stdio, StdioKind, WinRusage, WinTimeval, 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 6555894ca834..19cb69f05270 100644 --- a/src/spawn_sys/posix_spawn.rs +++ b/src/spawn_sys/posix_spawn.rs @@ -135,8 +135,8 @@ pub mod bun_spawn { pub use bun_core::spawn_ffi::{Action, FileActionType}; // `Fd::native()` returns `*mut c_void` on Windows, which can't fill the - // `c_int` action slot. posix_spawn never runs on Windows (libuv handles - // spawn there), so trap instead of inventing a HANDLE→int cast. + // `c_int` action slot. posix_spawn never runs on Windows, so trap + // instead of inventing a HANDLE→int cast. #[cfg(unix)] #[inline(always)] fn fd_int(fd: Fd) -> fd_t { @@ -473,7 +473,7 @@ pub mod posix_spawn { #[cfg(unix)] pub(crate) type Attr = bun_spawn::Attr; // No not(unix) Actions/Attr aliases: Windows goes through - // `process.rs::spawn_process_windows` (libuv) and never reaches these. + // `windows::spawn_process_windows` and never reaches these. // The #[repr(C)] request mirrors + extern decl live in `bun_core::spawn_ffi` // (single source of truth for bun-spawn.cpp's `bun_spawn_request_t`). The @@ -805,7 +805,7 @@ pub mod posix_spawn { // Higher-tier re-exports (`Process`/`Status`/`spawn_process`/`sync`/ // `Windows*`) live in `bun_spawn::posix_spawn::bun_spawn`, which augments // this module — they need event-loop types this `-sys` crate cannot name. - pub use crate::spawn_process::{PosixSpawnResult, Rusage}; + pub use crate::spawn_process::{Rusage, SpawnResult}; } #[cfg(unix)] diff --git a/src/spawn_sys/spawn_process.rs b/src/spawn_sys/spawn_process.rs index 2ec26ae2edd2..b3b3ea6e3646 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/`) — split out of `bun_spawn::process` so the fd/action +//! plumbing has no event-loop dependency. `Process`/`Poller`/`WaiterThread`/`sync` stay 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; @@ -49,10 +49,8 @@ pub type PidFdType = (); // 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)] @@ -74,20 +72,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() }; @@ -101,7 +89,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, @@ -130,11 +118,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; @@ -308,19 +296,20 @@ 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, 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, - pub windows: (), + #[cfg(windows)] + pub windows: crate::windows::WindowsOptions, pub argv0: Option<*const c_char>, pub stream: bool, pub sync: bool, @@ -342,9 +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-only ConPTY handle; void placeholder on POSIX. - pub pseudoconsole: (), + /// 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 @@ -356,19 +349,20 @@ 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, - windows: (), + #[cfg(windows)] + windows: Default::default(), argv0: None, stream: true, sync: false, @@ -376,22 +370,16 @@ impl Default for PosixSpawnOptions { use_execve_on_macos: false, no_sigpipe: true, new_process_group: false, + #[cfg(not(windows))] pty_slave_fd: -1, - pseudoconsole: (), + #[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`. @@ -425,14 +413,14 @@ 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), @@ -440,38 +428,44 @@ pub enum PosixStdio { 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 - } - #[inline] - pub fn ignore() -> Self { - PosixStdio::Ignore - } - #[inline] - pub fn buffer() -> Self { - PosixStdio::Buffer - } -} - -#[derive(Default)] -pub struct PosixSpawnResult { +pub struct SpawnResult { pub pid: PidT, pub pidfd: Option, + /// The process; the result's owner closes it (`to_process` moves it into + /// the `Process`). + #[cfg(windows)] + pub process_handle: bun_sys::windows::HANDLE, 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 pub has_exited: bool, } +#[allow( + clippy::derivable_impls, + reason = "only derivable where the windows-gated `process_handle` is absent; its default is \ + `INVALID_HANDLE_VALUE`, not null" +)] +impl Default for SpawnResult { + fn default() -> Self { + Self { + pid: 0, + pidfd: None, + #[cfg(windows)] + process_handle: bun_sys::windows::INVALID_HANDLE_VALUE, + stdin: None, + stdout: None, + stderr: None, + extra_pipes: Vec::new(), + memfds: [false; 3], + has_exited: false, + } + } +} + /// Entry in `extra_pipes` for a stdio slot at index >= 3. pub enum ExtraPipe { /// We created this fd (e.g. socketpair for `"pipe"`); `finalizeStreams` @@ -495,7 +489,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 @@ -620,10 +614,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()?; @@ -649,9 +643,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", @@ -702,7 +696,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. @@ -718,10 +712,9 @@ 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]; // Reshaped for borrowck: we // index spawned.{stdin,stdout,stderr} via a helper closure. let mut dup_stdout_to_stderr: bool = false; @@ -740,7 +733,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 @@ -754,7 +747,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 bun_sys::get_fcntl_flags(fileno).is_err() { actions.open_z(fileno, c"/dev/null", flag | bun_sys::O::CREAT as u32, 0o664)?; @@ -762,13 +755,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() { @@ -876,11 +869,11 @@ pub unsafe fn spawn_process_posix( set_spawned_stdio(&mut spawned, i, fds[0]); } - PosixStdio::Pipe(fd) => { + Stdio::Pipe(fd) => { actions.dup2(*fd, 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"); } @@ -888,7 +881,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())?; } } @@ -897,19 +890,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, @@ -918,8 +911,8 @@ pub unsafe fn spawn_process_posix( )?; extra_fds.push(ExtraPipe::Unavailable); } - PosixStdio::Ipc | PosixStdio::Buffer | PosixStdio::SocketFd => { - let is_ipc = matches!(ipc, PosixStdio::Ipc); + Stdio::Ipc | Stdio::Buffer | Stdio::SocketFd => { + let is_ipc = matches!(ipc, Stdio::Ipc); let fds: [Fd; 2] = match bun_sys::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, is_ipc) { Ok(p) => p, @@ -948,7 +941,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) => { actions.dup2(*fd, 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 @@ -1006,7 +999,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..860da0f7e4a5 --- /dev/null +++ b/src/spawn_sys/windows/args.rs @@ -0,0 +1,128 @@ +//! 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). +pub 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 +} + +#[cfg(test)] +mod tests { + use super::*; + + fn w(s: &str) -> Vec { + s.encode_utf16().collect() + } + + fn quoted(s: &str) -> String { + let mut out = Vec::new(); + quote_arg(&mut out, &w(s)); + String::from_utf16(&out).unwrap() + } + + #[test] + fn quoting_matches_the_msvcrt_rules() { + assert_eq!(quoted(""), r#""""#); + assert_eq!(quoted("plain"), "plain"); + assert_eq!(quoted("hello world"), r#""hello world""#); + assert_eq!(quoted("tab\there"), "\"tab\there\""); + assert_eq!(quoted(r#"hello"world"#), r#""hello\"world""#); + assert_eq!(quoted(r#"hello""world"#), r#""hello\"\"world""#); + assert_eq!(quoted(r"hello\world"), r"hello\world"); + assert_eq!(quoted(r"hello\\world"), r"hello\\world"); + assert_eq!(quoted(r#"hello\"world"#), r#""hello\\\"world""#); + assert_eq!(quoted(r#"hello\\"world"#), r#""hello\\\\\"world""#); + assert_eq!(quoted(r"hello world\"), r#""hello world\\""#); + assert_eq!(quoted(r"a\b c"), r#""a\b c""#); + assert_eq!(quoted("new\nline"), "new\nline"); + } + + #[test] + fn command_line_joins_with_one_space_and_terminates() { + let args: [&[u8]; 4] = [b"C:\\Program Files\\x.exe", b"", b"a b", b"c"]; + let line = make_command_line(args.iter().copied(), false); + assert_eq!(line.last(), Some(&0)); + assert_eq!( + String::from_utf16(&line[..line.len() - 1]).unwrap(), + r#""C:\Program Files\x.exe" "" "a b" c"# + ); + } + + #[test] + fn verbatim_copies_arguments_untouched() { + let args: [&[u8]; 3] = [b"cmd.exe", b"/c", br#""echo "a b"""#]; + let line = make_command_line(args.iter().copied(), true); + assert_eq!( + String::from_utf16(&line[..line.len() - 1]).unwrap(), + r#"cmd.exe /c "echo "a b"""# + ); + } + + #[test] + fn lone_surrogates_survive() { + // U+D800 as WTF-8. + let mut out = Vec::new(); + push_wtf8(&mut out, &[b'a', 0xED, 0xA0, 0x80, b'b']); + assert_eq!(out, [b'a' as u16, 0xD800, b'b' as u16]); + } +} diff --git a/src/spawn_sys/windows/env.rs b/src/spawn_sys/windows/env.rs new file mode 100644 index 000000000000..987834652b1d --- /dev/null +++ b/src/spawn_sys/windows/env.rs @@ -0,0 +1,284 @@ +//! 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; + +const fn widen(s: &str) -> [u16; N] { + let bytes = s.as_bytes(); + let mut out = [0u16; N]; + let mut i = 0; + while i < bytes.len() { + out[i] = bytes[i] as u16; + i += 1; + } + out +} + +macro_rules! wide_z { + ($s:literal) => {{ + const W: [u16; $s.len() + 1] = widen($s); + &W + }}; +} + +/// 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. +pub static REQUIRED_VARS: [&[u16]; 11] = [ + wide_z!("HOMEDRIVE"), + wide_z!("HOMEPATH"), + wide_z!("LOGONSERVER"), + wide_z!("PATH"), + wide_z!("SYSTEMDRIVE"), + wide_z!("SYSTEMROOT"), + wide_z!("TEMP"), + wide_z!("USERDOMAIN"), + wide_z!("USERNAME"), + wide_z!("USERPROFILE"), + wide_z!("WINDIR"), +]; + +/// `PATH`, NUL-terminated. +pub static PATH: &[u16] = wide_z!("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). Entries whose names compare equal are all kept, in input order. +/// `compare_names` orders two names; `parent_value` appends this process's +/// value of a NUL-terminated name and returns whether it has one. +pub fn make_env_block<'a>( + entries: impl Iterator, + compare_names: impl Fn(&[u16], &[u16]) -> Ordering, + mut parent_value: impl FnMut(&[u16], &mut Vec) -> bool, +) -> 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))); + + let mut block: Vec = Vec::with_capacity(strings.len() + vars.len() + 1); + 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; + } + } + } + block.push(0); + block +} + +/// The value of the first `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 = rest.iter().position(|&c| c == 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`). +pub fn compare_names_ordinal(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; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn upper(c: u16) -> u16 { + if (b'a' as u16..=b'z' as u16).contains(&c) { + c - 32 + } else { + c + } + } + + fn compare(a: &[u16], b: &[u16]) -> Ordering { + a.iter().map(|&c| upper(c)).cmp(b.iter().map(|&c| upper(c))) + } + + fn block_strings(block: &[u16]) -> Vec { + assert_eq!(block.last(), Some(&0)); + block[..block.len() - 1] + .split(|&c| c == 0) + .filter(|s| !s.is_empty()) + .map(|s| String::from_utf16(s).unwrap()) + .collect() + } + + fn build(entries: &[&str], parent: &[(&str, &str)]) -> Vec { + let block = make_env_block( + entries.iter().map(|s| s.as_bytes()), + compare, + |name, out| { + let name = String::from_utf16(&name[..name.len() - 1]).unwrap(); + match parent.iter().find(|(k, _)| *k == name) { + Some((_, v)) => { + out.extend(v.encode_utf16()); + true + } + None => false, + } + }, + ); + assert!(block.ends_with(&[0])); + block_strings(&block) + } + + #[test] + fn sorts_case_insensitively_with_underscore_after_letters() { + assert_eq!( + build(&["foo_bar=1", "FOOBAR=2", "b=3", "A=4"], &[]), + ["A=4", "b=3", "FOOBAR=2", "foo_bar=1"] + ); + } + + #[test] + fn injects_missing_required_variables_in_order() { + assert_eq!( + build( + &["ZED=1", "Path=C:\\bin", "NOEQUALS", "AAA=0"], + &[ + ("SYSTEMROOT", "C:\\Windows"), + ("PATH", "ignored"), + ("TEMP", "C:\\T"), + ], + ), + [ + "AAA=0", + "Path=C:\\bin", + "SYSTEMROOT=C:\\Windows", + "TEMP=C:\\T", + "ZED=1", + ] + ); + } + + #[test] + fn keeps_duplicates_and_hidden_drive_entries() { + assert_eq!( + build(&["foo=1", "FOO=2", "=C:=C:\\dir", "EMPTY="], &[]), + ["=C:=C:\\dir", "EMPTY=", "foo=1", "FOO=2"] + ); + } + + #[test] + fn empty_environment_is_a_valid_block() { + let block = make_env_block(core::iter::empty(), compare, |_, _| false); + assert_eq!(block, [0]); + } + + #[test] + fn finds_path_in_any_case() { + let block: Vec = "A=1\0pAtH=C:\\x;D:\\y\0Z=2\0\0".encode_utf16().collect(); + assert_eq!( + find_path(&block).map(|p| String::from_utf16(p).unwrap()), + Some("C:\\x;D:\\y".to_string()) + ); + let none: Vec = "PATHS=1\0\0".encode_utf16().collect(); + assert_eq!(find_path(&none), None); + } +} diff --git a/src/spawn_sys/windows/job.rs b/src/spawn_sys/windows/job.rs new file mode 100644 index 000000000000..02a57faa1a0c --- /dev/null +++ b/src/spawn_sys/windows/job.rs @@ -0,0 +1,73 @@ +//! 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. +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 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 + { + let err = win32::GetLastError(); + // SAFETY: `job` is a handle this function owns. + unsafe { win32::CloseHandle(job) }; + return Err(err); + } + + // 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. ERROR_ACCESS_DENIED means this process + // is in a job that cannot nest, which is fine. + // SAFETY: both handles are valid. + if unsafe { win32::AssignProcessToJobObject(job, win32::GetCurrentProcess()) } == 0 { + let err = win32::GetLastError(); + if err != win32::ERROR_ACCESS_DENIED { + // SAFETY: `job` is a handle this function owns. + unsafe { win32::CloseHandle(job) }; + return Err(err); + } + } + + Ok(job) +} diff --git a/src/spawn_sys/windows/kill.rs b/src/spawn_sys/windows/kill.rs new file mode 100644 index 000000000000..85333130287f --- /dev/null +++ b/src/spawn_sys/windows/kill.rs @@ -0,0 +1,340 @@ +//! Signals, as far as Windows has them. + +use core::ffi::{c_int, c_void}; +use core::ptr; + +use bun_sys::{E, Error, Tag}; + +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; + // SAFETY: `status` is a valid out-pointer. + if unsafe { 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; + // SAFETY: `status` is a valid out-pointer. + if unsafe { 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 +} + +type HKEY = *mut c_void; +const HKEY_LOCAL_MACHINE: HKEY = 0x8000_0002_u32 as i32 as isize as HKEY; +const KEY_QUERY_VALUE: DWORD = 0x0001; +const RRF_RT_ANY: DWORD = 0x0000_FFFF; +const CREATE_NEW: DWORD = 1; +const FILE_ATTRIBUTE_NORMAL: DWORD = 0x80; +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; + +#[repr(C)] +struct GUID { + data1: u32, + data2: u16, + data3: u16, + data4: [u8; 8], +} + +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 RegGetValueW( + hkey: HKEY, + lpSubKey: *const u16, + lpValue: *const u16, + dwFlags: DWORD, + pdwType: *mut DWORD, + pvData: *mut c_void, + pcbData: *mut DWORD, + ) -> 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 CreateDirectoryW(lpPathName: *const u16, lpSecurityAttributes: *mut c_void) -> BOOL; + 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 wide_z(s: &str) -> Vec { + s.encode_utf16().chain(core::iter::once(0)).collect() +} + +fn until_nul(s: &[u16]) -> &[u16] { + &s[..s.iter().position(|&c| c == 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 = wide_z(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 = wide_z("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(), + win32::GENERIC_WRITE, + 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..fd6be6e75f1c --- /dev/null +++ b/src/spawn_sys/windows/mod.rs @@ -0,0 +1,624 @@ +//! Process creation on Windows: `CreateProcessW` with an explicit handle list. + +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], + } + } +} + +/// The handles the child inherits, and the parent ends of the pipes made for it. +struct ChildStdio { + fds: Vec, + /// Child-side handles this spawn made, 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, + nul_in: HANDLE, + nul_out: HANDLE, +} + +impl Drop for ChildStdio { + fn drop(&mut self) { + for &handle in &self.to_close { + // SAFETY: handles this spawn created and still owns. + unsafe { win32::CloseHandle(handle) }; + } + if !self.keep_parent_ends { + for &handle in &self.parent_ends { + // SAFETY: see above. + unsafe { win32::CloseHandle(handle) }; + } + } + } +} + +impl ChildStdio { + fn nul(&mut self, writable: bool) -> Result { + let slot = if writable { + &mut self.nul_out + } else { + &mut self.nul_in + }; + if *slot == INVALID_HANDLE_VALUE { + *slot = stdio::open_nul(writable)?; + let handle = *slot; + self.to_close.push(handle); + } + Ok(ChildFd { + handle: *slot, + crt_flags: stdio::FOPEN | stdio::FDEV, + }) + } + + fn inherit(&mut self, source: HANDLE) -> Result { + let handle = stdio::duplicate_inheritable(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 { + 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, + }; + let handle = bun_sys::open_a(path, access | bun_sys::O::CREAT, 0o664)?.native(); + stdio.to_close.push(handle); + if win32::SetHandleInformation( + handle, + win32::HANDLE_FLAG_INHERIT, + win32::HANDLE_FLAG_INHERIT, + ) == 0 + { + return Err(win32::last_error(Tag::uv_spawn)); + } + 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 file_exists(path: &[u16]) -> bool { + // SAFETY: `path` is NUL-terminated (`search_path` contract). + let attributes = unsafe { win32::GetFileAttributesW(path.as_ptr()) }; + attributes != win32::INVALID_FILE_ATTRIBUTES + && attributes & win32::FILE_ATTRIBUTE_DIRECTORY == 0 +} + +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) }, + env::compare_names_ordinal, + env::parent_value, + )) + }; + + // 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 mut cwd_first = false; + let path: &[u16] = if search_path::needs_path(&application) { + // SAFETY: an empty NUL-terminated string. + cwd_first = unsafe { win32::NeedCurrentDirectoryForExePathW([0u16].as_ptr()) } != 0; + 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, cwd_first, &mut file_exists) + }; + 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, + nul_in: INVALID_HANDLE_VALUE, + nul_out: INVALID_HANDLE_VALUE, + }; + // 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); + } + } + } + // `2>&1`: the fd shares the handle of the one it is redirected to. + for (i, &option) in std_options.iter().enumerate() { + if let Stdio::Dup2(dup2) = option { + let to = dup2.to as usize; + child_stdio.fds[i] = child_stdio.fds[to]; + 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) + } + Slot::Supplied(fd, parent) => { + child_stdio.fds[3 + i] = fd; + ExtraPipe::UnownedFd(parent) + } + }); + } + } else { + result + .extra_pipes + .resize_with(options.extra_fds.len(), || ExtraPipe::Unavailable); + } + + let mut crt_block = stdio::make_crt_block(&child_stdio.fds); + let handle_list = stdio::make_handle_list(&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, handle list or not, 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)? + }; + + // SAFETY: all-zero is a valid PROCESS_INFORMATION. + let mut info: win32::PROCESS_INFORMATION = unsafe { core::mem::zeroed() }; + loop { + let attribute_count = DWORD::from(!handle_list.is_empty()) + + 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: `handle_list`, `job` and the pseudoconsole outlive `attributes`. + unsafe { + if !handle_list.is_empty() { + attributes + .set( + win32::PROC_THREAD_ATTRIBUTE_HANDLE_LIST, + handle_list.as_ptr().cast(), + size_of_val(handle_list.as_slice()), + ) + .map_err(spawn_error)?; + } + 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 + } + }; + + // 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 handle list requires it, and restricts it to the list. + i32::from(!handle_list.is_empty()), + 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 { + break; + } + let code = win32::GetLastError(); + // A process in a job that cannot nest (before Windows 8, or an odd + // sandbox) cannot put its children in another job. Such a parent must + // still be able to spawn: go without ending the child with us. + if code == win32::ERROR_ACCESS_DENIED && !job.is_null() { + job = ptr::null_mut(); + continue; + } + return Err(spawn_error(code)); + } + + // 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 = 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..4573ea0e0a60 --- /dev/null +++ b/src/spawn_sys/windows/search_path.rs @@ -0,0 +1,409 @@ +//! 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`. + +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) +} + +fn join_test( + dir: &[u16], + name: &[u16], + ext: &[u16], + cwd: &[u16], + exists: &mut dyn FnMut(&[u16]) -> bool, +) -> 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, + exists: &mut dyn FnMut(&[u16]) -> 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, exists) { + return Some(found); + } + } + if let Some(found) = join_test(dir, name, &COM, cwd, exists) { + return Some(found); + } + join_test(dir, name, &EXE, cwd, exists) +} + +/// 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. +/// `cwd_first` is `NeedCurrentDirectoryForExePathW("")`. `exists` answers +/// whether a NUL-terminated path names something that is not a directory. +pub fn search_path( + file: &[u16], + cwd: &[u16], + path: &[u16], + cwd_first: bool, + exists: &mut dyn FnMut(&[u16]) -> bool, +) -> 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, exists); + } + + if cwd_first { + if let Some(found) = walk_ext(&[], file, cwd, name_has_ext, exists) { + 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, exists) { + return Some(found); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn w(s: &str) -> Vec { + s.encode_utf16().collect() + } + + /// Runs the search against a fixed set of existing files; returns the match + /// and every path probed, in order. + fn run( + file: &str, + cwd: &str, + path: &str, + cwd_first: bool, + files: &[&str], + ) -> (Option, Vec) { + let mut probed = Vec::new(); + let found = search_path(&w(file), &w(cwd), &w(path), cwd_first, &mut |p| { + assert_eq!(p.last(), Some(&0)); + let p = String::from_utf16(&p[..p.len() - 1]).unwrap(); + let hit = files.iter().any(|f| f.eq_ignore_ascii_case(&p)); + probed.push(p); + hit + }) + .map(|p| String::from_utf16(&p[..p.len() - 1]).unwrap()); + (found, probed) + } + + #[test] + fn empty_and_dot_never_match() { + assert_eq!(run("", "C:\\cwd", "C:\\bin", true, &[]).0, None); + assert_eq!( + run(".", "C:\\cwd", "C:\\bin", true, &["C:\\cwd\\..exe"]).0, + None + ); + } + + #[test] + fn bare_name_tries_cwd_then_path_with_com_before_exe() { + let (found, probed) = run( + "node", + "C:\\cwd", + "C:\\a;C:\\b\\", + true, + &["C:\\b\\node.exe"], + ); + assert_eq!(found.as_deref(), Some("C:\\b\\node.exe")); + assert_eq!( + probed, + [ + "C:\\cwd\\node.com", + "C:\\cwd\\node.exe", + "C:\\a\\node.com", + "C:\\a\\node.exe", + "C:\\b\\node.com", + "C:\\b\\node.exe", + ] + ); + } + + #[test] + fn cwd_is_skipped_when_the_system_says_so() { + let (found, probed) = run("x", "C:\\cwd", "C:\\a", false, &["C:\\cwd\\x.exe"]); + assert_eq!(found, None); + assert_eq!(probed, ["C:\\a\\x.com", "C:\\a\\x.exe"]); + } + + #[test] + fn a_name_with_an_extension_is_tried_exactly_first() { + let (found, probed) = run("run.cmd", "C:\\cwd", "", true, &["C:\\cwd\\run.cmd"]); + assert_eq!(found.as_deref(), Some("C:\\cwd\\run.cmd")); + assert_eq!(probed, ["C:\\cwd\\run.cmd"]); + + // A trailing dot is not an extension, and no second dot is added. + let (_, probed) = run("noext.", "C:\\cwd", "", true, &[]); + assert_eq!(probed, ["C:\\cwd\\noext.com", "C:\\cwd\\noext.exe"]); + } + + #[test] + fn a_name_with_a_directory_never_reads_path() { + let (found, probed) = run( + "sub/tool", + "C:\\cwd", + "C:\\a", + true, + &["C:\\a\\tool.exe", "C:\\cwd\\sub/tool.exe"], + ); + assert_eq!(found.as_deref(), Some("C:\\cwd\\sub/tool.exe")); + assert_eq!(probed, ["C:\\cwd\\sub/tool.com", "C:\\cwd\\sub/tool.exe"]); + assert!(!needs_path(&w("sub/tool"))); + assert!(needs_path(&w("tool"))); + } + + #[test] + fn cwd_rules_for_each_kind_of_directory() { + // Absolute with a drive, and UNC: cwd unused. + assert_eq!( + run("C:\\x\\a.exe", "D:\\cwd", "", true, &["C:\\x\\a.exe"]) + .0 + .as_deref(), + Some("C:\\x\\a.exe") + ); + assert!(!needs_cwd(&w("C:\\x\\a.exe"))); + assert_eq!( + run( + "\\\\srv\\share\\a.exe", + "D:\\cwd", + "", + true, + &["\\\\srv\\share\\a.exe"] + ) + .0 + .as_deref(), + Some("\\\\srv\\share\\a.exe") + ); + assert!(!needs_cwd(&w("\\\\srv\\share\\a.exe"))); + // Rooted without a drive: the drive of cwd. + assert_eq!( + run("\\x\\a.exe", "D:\\cwd", "", true, &["D:\\x\\a.exe"]) + .0 + .as_deref(), + Some("D:\\x\\a.exe") + ); + // Drive-relative: all of cwd on the same drive, none of it otherwise. + assert_eq!( + run("d:x\\a.exe", "D:\\cwd", "", true, &["D:\\cwd\\x\\a.exe"]) + .0 + .as_deref(), + Some("D:\\cwd\\x\\a.exe") + ); + assert_eq!( + run("E:x\\a.exe", "D:\\cwd", "", true, &["E:x\\a.exe"]) + .0 + .as_deref(), + Some("E:x\\a.exe") + ); + assert!(needs_cwd(&w("E:x\\a.exe"))); + assert!(needs_cwd(&w("a.exe"))); + } + + #[test] + fn path_entries_may_be_quoted_relative_or_empty() { + let (found, probed) = run( + "t", + "C:\\cwd", + ";;\"C:\\semi;colon\";rel;'C:\\q'", + false, + &["C:\\q\\t.exe"], + ); + assert_eq!(found.as_deref(), Some("C:\\q\\t.exe")); + assert_eq!( + probed, + [ + "C:\\semi;colon\\t.com", + "C:\\semi;colon\\t.exe", + "C:\\cwd\\rel\\t.com", + "C:\\cwd\\rel\\t.exe", + "C:\\q\\t.com", + "C:\\q\\t.exe", + ] + ); + } + + #[test] + fn a_lone_quote_entry_is_skipped() { + let (found, probed) = run("t", "C:\\cwd", "\";C:\\a", false, &["C:\\a\\t.exe"]); + // The unterminated quote swallows the rest of PATH. + assert_eq!(found, None); + assert_eq!(probed, ["C:\\cwd\\;C:\\a\\t.com", "C:\\cwd\\;C:\\a\\t.exe"]); + assert_eq!( + run("t", "C:\\cwd", "\"", false, &[]).1, + Vec::::new() + ); + } +} diff --git a/src/spawn_sys/windows/stdio.rs b/src/spawn_sys/windows/stdio.rs new file mode 100644 index 000000000000..912d0c9408bb --- /dev/null +++ b/src/spawn_sys/windows/stdio.rs @@ -0,0 +1,339 @@ +//! 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 the cap +/// children have been spawned with so far. +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 +} + +/// The handles to name in `PROC_THREAD_ATTRIBUTE_HANDLE_LIST`. The list may +/// not contain a handle twice, nor `INVALID_HANDLE_VALUE` (either fails +/// `CreateProcessW` with `ERROR_INVALID_PARAMETER`). +pub fn make_handle_list(fds: &[ChildFd]) -> Vec { + let mut list: Vec = Vec::with_capacity(fds.len()); + for fd in fds { + if fd.handle != INVALID_HANDLE_VALUE && !fd.handle.is_null() && !list.contains(&fd.handle) { + list.push(fd.handle); + } + } + list +} + +/// 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), + }, + } +} + +/// An inheritable duplicate of `handle`. +pub fn duplicate_inheritable(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, + 1, + win32::DUPLICATE_SAME_ACCESS, + ) + } == 0 + { + return Err(win32::GetLastError()); + } + Ok(dup) +} + +fn inheritable() -> win32::SECURITY_ATTRIBUTES { + win32::SECURITY_ATTRIBUTES { + nLength: size_of::() as DWORD, + lpSecurityDescriptor: ptr::null_mut(), + bInheritHandle: 1, + } +} + +/// An inheritable 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 + }; + let mut sa = inheritable(); + // SAFETY: `NUL` is NUL-terminated; `sa` outlives the call. + let handle = unsafe { + win32::CreateFileW( + NUL.as_ptr(), + access, + win32::FILE_SHARE_READ | win32::FILE_SHARE_WRITE, + &mut sa, + 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, +} + +pub struct PipePair { + /// Overlapped, not inheritable, not associated with a completion port. + pub parent: HANDLE, + /// Inheritable. + 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), + } + }; + + let mut sa = inheritable(); + // SAFETY: `name_w` is NUL-terminated; `sa` outlives the call. + let client = unsafe { + win32::CreateFileW( + name_w.as_ptr(), + client_access, + 0, + &mut sa, + 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, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn h(v: usize) -> HANDLE { + v as HANDLE + } + + #[test] + fn crt_block_layout_is_packed() { + let fds = [ + ChildFd { + handle: h(0x1111), + crt_flags: FOPEN | FDEV, + }, + ChildFd { + handle: h(0x2222), + crt_flags: FOPEN | FPIPE, + }, + ChildFd::CLOSED, + ChildFd { + handle: h(0x4444), + crt_flags: FOPEN, + }, + ]; + let block = make_crt_block(&fds); + let ptr_size = size_of::(); + assert_eq!(block.len(), 4 + 4 + 4 * ptr_size); + assert_eq!(block[..4], 4i32.to_ne_bytes()); + assert_eq!(block[4..8], [0x41, 0x09, 0x00, 0x01]); + let handle_at = |i: usize| { + let start = 8 + i * ptr_size; + usize::from_ne_bytes(block[start..start + ptr_size].try_into().unwrap()) + }; + assert_eq!(handle_at(0), 0x1111); + assert_eq!(handle_at(1), 0x2222); + assert_eq!(handle_at(2), usize::MAX); + assert_eq!(handle_at(3), 0x4444); + } + + #[test] + fn largest_block_fits_cb_reserved2() { + let fds = [ChildFd::CLOSED; MAX_STDIO]; + assert!(u16::try_from(make_crt_block(&fds).len()).is_ok()); + } + + #[test] + fn handle_list_has_no_duplicates_or_closed_slots() { + let fds = [ + ChildFd { + handle: h(8), + crt_flags: FOPEN, + }, + ChildFd { + handle: h(12), + crt_flags: FOPEN, + }, + ChildFd { + handle: h(12), + crt_flags: FOPEN, + }, + ChildFd::CLOSED, + ChildFd { + handle: ptr::null_mut(), + crt_flags: 0, + }, + ]; + assert_eq!(make_handle_list(&fds), [h(8), h(12)]); + assert!(make_handle_list(&[ChildFd::CLOSED; 3]).is_empty()); + } +} diff --git a/src/spawn_sys/windows/win32.rs b/src/spawn_sys/windows/win32.rs new file mode 100644 index 000000000000..b70066ada035 --- /dev/null +++ b/src/spawn_sys/windows/win32.rs @@ -0,0 +1,143 @@ +//! Win32 declarations process creation needs that `bun_windows_sys` does not +//! carry. + +#![allow(non_snake_case, non_camel_case_types, clippy::upper_case_acronyms)] + +use core::ffi::c_int; + +pub use bun_windows_sys::externs::kernel32::{ + CreateNamedPipeW, CreateProcessW, DuplicateHandle, GetCurrentProcess, GetExitCodeProcess, + GetLastError, ReadFile, +}; +pub use bun_windows_sys::externs::{ + AssignProcessToJobObject, BOOL, CloseHandle, CreateFileW, CreateJobObjectW, + DUPLICATE_SAME_ACCESS, DWORD, FILE_ATTRIBUTE_DIRECTORY, FILE_FLAG_OVERLAPPED, FILE_SHARE_READ, + FILE_SHARE_WRITE, FILETIME, GENERIC_READ, GENERIC_WRITE, GetCurrentDirectoryW, + GetFileAttributesW, GetProcessTimes, HANDLE, HANDLE_FLAG_INHERIT, HPCON, INFINITE, + INVALID_HANDLE_VALUE, InitializeProcThreadAttributeList, JOBOBJECT_EXTENDED_LIMIT_INFORMATION, + JobObjectExtendedLimitInformation, MAX_PATH, OPEN_EXISTING, OVERLAPPED, OpenProcess, + PIPE_ACCESS_INBOUND, PIPE_ACCESS_OUTBOUND, PROCESS_INFORMATION, SECURITY_ATTRIBUTES, + STARTF_USESTDHANDLES, STARTUPINFOEXW, STARTUPINFOW, SetHandleInformation, + SetInformationJobObject, UpdateProcThreadAttribute, WAIT_FAILED, Win32Error, +}; + +pub const INVALID_FILE_ATTRIBUTES: DWORD = DWORD::MAX; + +pub const FILE_TYPE_UNKNOWN: DWORD = 0; +pub const FILE_TYPE_DISK: DWORD = 1; +pub const FILE_TYPE_CHAR: DWORD = 2; +pub const FILE_TYPE_PIPE: DWORD = 3; +pub const FILE_TYPE_REMOTE: DWORD = 0x8000; + +pub const FILE_READ_ATTRIBUTES: DWORD = 0x0080; +pub const FILE_WRITE_ATTRIBUTES: DWORD = 0x0100; +pub const WRITE_DAC: DWORD = 0x0004_0000; +pub const FILE_GENERIC_READ: DWORD = 0x0012_0089; +pub const FILE_GENERIC_WRITE: DWORD = 0x0012_0116; + +pub const FILE_FLAG_FIRST_PIPE_INSTANCE: DWORD = 0x0008_0000; +pub const SECURITY_SQOS_PRESENT: DWORD = 0x0010_0000; +pub const SECURITY_ANONYMOUS: DWORD = 0; +pub const PIPE_TYPE_BYTE: DWORD = 0; +pub const PIPE_READMODE_BYTE: DWORD = 0; +pub const PIPE_WAIT: DWORD = 0; +pub const PIPE_REJECT_REMOTE_CLIENTS: DWORD = 0x0000_0008; + +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 CREATE_UNICODE_ENVIRONMENT: DWORD = 0x0000_0400; +pub const DETACHED_PROCESS: DWORD = 0x0000_0008; +pub const CREATE_NO_WINDOW: DWORD = 0x0800_0000; +pub const EXTENDED_STARTUPINFO_PRESENT: DWORD = 0x0008_0000; + +pub const PROC_THREAD_ATTRIBUTE_HANDLE_LIST: usize = 0x0002_0002; +pub const PROC_THREAD_ATTRIBUTE_JOB_LIST: usize = 0x0002_000D; +pub const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE: usize = 0x0002_0016; + +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; + +pub const PROCESS_TERMINATE: DWORD = 0x0001; +pub const PROCESS_QUERY_INFORMATION: DWORD = 0x0400; +pub const SYNCHRONIZE: DWORD = 0x0010_0000; +pub const STILL_ACTIVE: DWORD = 259; +pub const WAIT_OBJECT_0: DWORD = 0; +pub const WAIT_TIMEOUT: DWORD = 258; + +pub const CSTR_EQUAL: c_int = 2; + +pub const ERROR_FILE_NOT_FOUND: DWORD = 2; +pub const ERROR_ACCESS_DENIED: DWORD = 5; +pub const ERROR_INVALID_HANDLE: DWORD = 6; +pub const ERROR_NOT_SUPPORTED: DWORD = 50; +pub const ERROR_INVALID_PARAMETER: DWORD = 87; +pub const ERROR_BROKEN_PIPE: DWORD = 109; +pub const ERROR_PIPE_BUSY: DWORD = 231; +pub const ERROR_IO_PENDING: DWORD = 997; + +unsafe extern "system" { + pub safe fn TerminateProcess(hProcess: HANDLE, uExitCode: u32) -> BOOL; + pub safe fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD; + pub fn WaitForMultipleObjects( + nCount: DWORD, + lpHandles: *const HANDLE, + bWaitAll: BOOL, + dwMilliseconds: DWORD, + ) -> DWORD; + pub safe fn GetProcessId(Process: HANDLE) -> DWORD; + pub safe fn GetCurrentProcessId() -> DWORD; + pub safe fn GetFileType(hFile: HANDLE) -> DWORD; + pub safe fn SetLastError(dwErrCode: DWORD); + pub fn DeleteProcThreadAttributeList(lpAttributeList: *mut u8); + pub fn CompareStringOrdinal( + lpString1: *const u16, + cchCount1: c_int, + lpString2: *const u16, + cchCount2: c_int, + bIgnoreCase: BOOL, + ) -> c_int; + pub fn GetEnvironmentVariableW(lpName: *const u16, lpBuffer: *mut u16, nSize: DWORD) -> DWORD; + pub fn NeedCurrentDirectoryForExePathW(ExeName: *const u16) -> BOOL; + pub fn GetShortPathNameW( + lpszLongPath: *const u16, + lpszShortPath: *mut u16, + cchBuffer: DWORD, + ) -> DWORD; + pub fn CreateEventW( + lpEventAttributes: *mut SECURITY_ATTRIBUTES, + bManualReset: BOOL, + bInitialState: BOOL, + lpName: *const u16, + ) -> HANDLE; + pub fn GetOverlappedResult( + hFile: HANDLE, + lpOverlapped: *mut OVERLAPPED, + lpNumberOfBytesTransferred: *mut DWORD, + bWait: BOOL, + ) -> BOOL; + pub fn CancelIoEx(hFile: HANDLE, lpOverlapped: *mut OVERLAPPED) -> BOOL; + pub fn GetProcessIoCounters( + hProcess: HANDLE, + lpIoCounters: *mut bun_windows_sys::IO_COUNTERS, + ) -> 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 453a26890575..804da17b4f04 100644 --- a/src/standalone_graph/StandaloneModuleGraph.rs +++ b/src/standalone_graph/StandaloneModuleGraph.rs @@ -711,7 +711,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..d227c19e7f1e 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 (every UV_EXTERN function in src/jsc/bindings/libuv), +; 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..e91efedd65ae 100644 --- a/src/sys/Error.rs +++ b/src/sys/Error.rs @@ -176,20 +176,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,8 +222,7 @@ 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. pub fn get_error_code_tag_name(&self) -> Option<(&'static str, SystemErrno)> { let e = self.resolve_system_errno()?; // strum::IntoStaticStr — variant name (e.g., "ENOENT"). @@ -270,7 +255,7 @@ impl Error { // 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) + let js_errno = bun_errno::uv_codes::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(); @@ -298,12 +283,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 +429,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..92a1d7a0ec6c 100644 --- a/src/sys/PosixStat.rs +++ b/src/sys/PosixStat.rs @@ -128,8 +128,8 @@ pub fn stat_ctime(s: &Stat) -> Timespec { 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 { @@ -194,8 +194,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 41c9196add79..c80ad383a3f9 100644 --- a/src/sys/dir.rs +++ b/src/sys/dir.rs @@ -332,11 +332,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..6e50566c1974 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. @@ -23,7 +21,7 @@ pub enum ErrorCase { } #[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq, strum::IntoStaticStr)] -pub enum MakeLibUvOwnedError { +pub enum MakeCrtOwnedError { #[error("SystemFdQuotaExceeded")] SystemFdQuotaExceeded, } @@ -57,8 +55,8 @@ pub trait FdExt: Copy + Sized { /// 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( + fn make_crt_owned(self) -> Result; + fn make_crt_owned_for_syscall( self, syscall_tag: sys::Tag, error_case: ErrorCase, @@ -145,8 +143,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,17 +152,19 @@ 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)) + // `_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) => { unsafe extern "system" { @@ -214,7 +213,7 @@ impl FdExt for Fd { result } - fn make_lib_uv_owned(self) -> Result { + fn make_crt_owned(self) -> Result { debug_assert!(self.is_valid()); #[cfg(not(windows))] { @@ -224,15 +223,19 @@ impl FdExt for Fd { { match self.kind() { FdKind::System => { - let n = uv_open_osfhandle(self.native())?; - Ok(Fd::from_uv(n)) + let n = bun_core::fd::crt_open_osfhandle(self.native()); + debug_assert!(n >= -1); + if n == -1 { + return Err(MakeCrtOwnedError::SystemFdQuotaExceeded); + } + Ok(Fd::from_crt(n)) } - FdKind::Uv => Ok(self), + FdKind::Crt => Ok(self), } } } - fn make_lib_uv_owned_for_syscall( + fn make_crt_owned_for_syscall( self, syscall_tag: sys::Tag, error_case: ErrorCase, @@ -244,9 +247,9 @@ impl FdExt for Fd { } #[cfg(windows)] { - match self.make_lib_uv_owned() { + match self.make_crt_owned() { Ok(fd) => Ok(fd), - Err(MakeLibUvOwnedError::SystemFdQuotaExceeded) => { + Err(MakeCrtOwnedError::SystemFdQuotaExceeded) => { if matches!(error_case, ErrorCase::CloseOnFail) { self.close(); } @@ -271,7 +274,7 @@ impl FdExt for Fd { } } -// `fromJS` / `fromJSValidated` / `toJS` / `toJSWithoutMakingLibUVOwned` are +// `fromJS` / `fromJSValidated` / `toJS` / `toJSWithoutMakingCrtOwned` are // `*_jsc` aliases — deleted per PORTING.md; they live as extension-trait // methods in `bun_sys_jsc`. @@ -293,7 +296,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 +306,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 23baec9e0f42..60d4f0c9c5ac 100644 --- a/src/sys/file.rs +++ b/src/sys/file.rs @@ -264,9 +264,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 dddbb5965b97..801c10fe11c1 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::{ErrorCase, FdExt, MakeCrtOwnedError, 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,11 +47,11 @@ 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); + // On Windows `self.errno` is a `UV_E*` 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(d) = bun_errno::uv_codes::uv_err_to_e_discriminant(self.errno) { if let Some(e) = E::try_from_raw(d) { return e; } @@ -888,9 +884,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()); @@ -1148,7 +1142,7 @@ pub mod O { 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 + // NOT MSVCRT `_O_*`. `windows::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)] @@ -1171,9 +1165,9 @@ pub mod O { 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. Values + // chosen to round-trip through `windows::O::from_bun_o` without colliding + // with the `_O_*` flags MSVCRT defines. #[cfg(windows)] pub const NONBLOCK: i32 = 0o4000; #[cfg(windows)] @@ -1247,13 +1241,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, StatTimespec}; // ────────────────────────────────────────────────────────────────────────── // Syscall surface — real posix libc FFI. Windows path lives in -// `windows_impl` (NT/kernel32/libuv triad) below. +// `windows_impl` below. // ────────────────────────────────────────────────────────────────────────── use bun_core::ZStr; @@ -1293,6 +1286,7 @@ 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); #[cfg(not(target_os = "macos"))] @@ -1302,6 +1296,7 @@ impl Tag { pub const copyfile: Tag = Tag(10); pub(crate) const fchmod: Tag = Tag(11); pub const fchmodat: Tag = Tag(12); + #[cfg(not(windows))] pub(crate) const fchown: Tag = Tag(13); pub(crate) const fcntl: Tag = Tag(14); pub const fdatasync: Tag = Tag(15); @@ -1314,6 +1309,7 @@ impl Tag { 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); @@ -1326,7 +1322,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); @@ -1367,6 +1365,7 @@ impl Tag { #[cfg(not(windows))] pub(crate) const pwritev: Tag = Tag(75); pub const readv: Tag = Tag(76); + #[cfg(not(windows))] pub(crate) const preadv: Tag = Tag(77); pub const ioctl_ficlone: Tag = Tag(78); pub const accept: Tag = Tag(79); @@ -3502,49 +3501,37 @@ 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, readlink, realpath, + rename, stat, symlink, symlink_dir, unlink, utimens, + }; + 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 @@ -3552,7 +3539,7 @@ mod windows_impl { // retry live here. loop { let mut amount_read: w::DWORD = 0; - // SAFETY: FFI; `fd.cast()` is a valid HANDLE, buf valid for `adjusted_len`. + // SAFETY: FFI; buf valid for `adjusted_len`. let rc = unsafe { w::kernel32::ReadFile( fd.native(), @@ -3564,25 +3551,21 @@ mod windows_impl { }; 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)), + if er == w::Win32Error::OPERATION_ABORTED { + continue; } + return match w::fs::read_error(er, Tag::read) { + Some(err) => Err(err.with_fd(fd)), + None => Ok(0), + }; } 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`. + // SAFETY: FFI; buf valid for `adjusted_len`. let rc = unsafe { w::kernel32::WriteFile( fd.native(), @@ -3593,24 +3576,54 @@ mod windows_impl { ) }; 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)); + return Err(w::fs::write_error(w::Win32Error::get(), Tag::write).with_fd(fd)); } Ok(bytes_written as usize) } + /// A negative `off` reads at the file pointer. On a synchronous handle a + /// positioned read also leaves the file pointer after the bytes read. + /// A positioned `ReadFile`/`WriteFile` on a synchronous handle also moves + /// its file pointer. `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. + pub(crate) struct RestoreFilePointer { + handle: w::HANDLE, + saved: Option, + } + impl RestoreFilePointer { + pub(crate) fn new(fd: Fd) -> Self { + let handle = fd.native(); + let mut saved = None; + if fd.kind() == FdKind::Crt { + let mut current: i64 = 0; + // SAFETY: FFI; `handle` is the fd's HANDLE, `current` valid for write. + if unsafe { w::SetFilePointerEx(handle, 0, &mut current, w::FILE_CURRENT) } != 0 { + 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 { + w::SetFilePointerEx(self.handle, position, core::ptr::null_mut(), w::FILE_BEGIN) + }; + } + } + } + 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); + if off < 0 { + return read(fd, buf); } - let off = off as u64; + let _restore = RestoreFilePointer::new(fd); + pread_at(fd, buf, off as u64) + } + /// `pread` under a [`RestoreFilePointer`] the caller holds. + pub(crate) fn pread_at(fd: Fd, buf: &mut [u8], off: u64) -> Maybe { let adjusted_len = buf.len().min(MAX_COUNT) as w::DWORD; loop { let mut overlapped = w::OVERLAPPED { @@ -3621,9 +3634,9 @@ mod windows_impl { 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). + // SAFETY: FFI; `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(), @@ -3635,23 +3648,27 @@ mod windows_impl { }; 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)), + if er == w::Win32Error::OPERATION_ABORTED { + continue; } + return match w::fs::read_error(er, Tag::read) { + Some(err) => Err(err.with_fd(fd)), + None => Ok(0), + }; } 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); + /// 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 off = off as u64; + let _restore = RestoreFilePointer::new(fd); + pwrite_at(fd, buf, off as u64) + } + /// `pwrite` under a [`RestoreFilePointer`] the caller holds. + pub(crate) fn pwrite_at(fd: Fd, buf: &[u8], off: u64) -> Maybe { let adjusted_len = buf.len().min(MAX_COUNT) as w::DWORD; let mut overlapped = w::OVERLAPPED { Internal: 0, @@ -3661,9 +3678,8 @@ mod windows_impl { 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). + // SAFETY: FFI; `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(), @@ -3674,210 +3690,20 @@ mod windows_impl { ) }; 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)); + return Err(w::fs::write_error(w::Win32Error::get(), Tag::write).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(); @@ -4113,7 +3939,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(); @@ -4121,7 +3946,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(); @@ -4190,30 +4015,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() @@ -4252,34 +4053,6 @@ 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; @@ -4411,8 +4184,8 @@ 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. +// not written through). On Windows it has the layout of +// `PlatformIoVec`; that arm is below. // Layout matches `libc::iovec` (`{ *void, usize }`) so a `&[PlatformIoVecConst]` // can be passed straight to `pwritev(2)`. // ────────────────────────────────────────────────────────────────────────── @@ -4501,23 +4274,28 @@ 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 has the layout of `WSABUF`: `{ ULONG len; char* buf; }`, the +// fields in the opposite order. // ────────────────────────────────────────────────────────────────────────── #[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 len: windows::ULONG, + pub base: *mut u8, +} +// SAFETY: `{ ULONG, *mut u8 }` — `(0, null)` 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; @@ -4532,10 +4310,9 @@ pub fn platform_iovec_create(buf: &mut [u8]) -> PlatformIoVec { } #[cfg(windows)] { - // `uv_buf_t` on Windows is `{ ULONG len; char* base; }` — order-swapped vs - // POSIX iovec. + debug_assert!(buf.len() <= windows::ULONG::MAX as usize); PlatformIoVec { - len: buf.len() as bun_libuv_sys::ULONG, + len: buf.len() as windows::ULONG, base: buf.as_mut_ptr(), } } @@ -4553,30 +4330,29 @@ pub const fn platform_iovec_len(iov: &PlatformIoVec) -> 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. +/// Windows `PlatformIOVecConst` — the layout of `PlatformIoVec`, 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 len: windows::ULONG, pub(crate) base: *const u8, } -// SAFETY: `{ ULONG, *const u8 }` — `(0, null)` is a valid empty `uv_buf_t` (S021). +// SAFETY: `{ ULONG, *const u8 }` — `(0, null)` is a valid empty buffer (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::() + 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 { + debug_assert!(buf.len() <= windows::ULONG::MAX as usize); PlatformIoVecConst { - len: buf.len() as bun_libuv_sys::ULONG, + len: buf.len() as windows::ULONG, base: buf.as_ptr(), } } @@ -4623,11 +4399,13 @@ pub fn writev(fd: Fd, vecs: &[PlatformIoVec]) -> Maybe { return Ok(rc as usize); } } - #[cfg(not(unix))] + #[cfg(windows)] { - // TODO(windows): route through `uv_fs_write` with `uv_buf_t[]`. - let _ = (fd, vecs); - Err(Error::from_code_int(libc::ENOSYS, Tag::writev)) + // 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) } } @@ -4678,10 +4456,9 @@ pub fn readv(fd: Fd, vecs: &[PlatformIoVec]) -> Maybe { return Ok(rc as usize); } } - #[cfg(not(unix))] + #[cfg(windows)] { - let _ = (fd, vecs); - Err(Error::from_code_int(libc::ENOSYS, Tag::readv)) + windows::fs::preadv(fd, vecs, -1) } } @@ -4741,23 +4518,21 @@ pub fn preadv(fd: Fd, vecs: &[PlatformIoVec], position: i64) -> Maybe { return Ok(rc as usize); } } - #[cfg(not(unix))] + #[cfg(windows)] { - let _ = (fd, vecs, position); - Err(Error::from_code_int(libc::ENOSYS, Tag::preadv)) + 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 `GetDiskFreeSpaceW`. // ────────────────────────────────────────────────────────────────────────── #[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. /// @@ -4794,10 +4569,9 @@ pub fn statfs(path: &ZStr) -> Maybe { } return Ok(st); } - #[cfg(not(unix))] + #[cfg(windows)] { - let _ = path; - Err(Error::from_code(E::NOSYS, Tag::statfs)) + windows::fs::statfs(path) } } @@ -4854,8 +4628,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`. @@ -6025,8 +5798,8 @@ 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; @@ -6376,15 +6149,21 @@ 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. + // (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 { 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); @@ -6751,6 +6530,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. @@ -6838,6 +6622,10 @@ 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 { + return Err(Error::from_code(E::EISDIR, Tag::open)); + } return match windows::Win32Error::from_nt_status(rc) { windows::Win32Error::SUCCESS => { if (options.access_mask & w::FILE_APPEND_DATA) != 0 { @@ -6930,12 +6718,11 @@ 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; + // O_RDONLY asks for read access only, like `open()`. GENERIC_WRITE already + // includes FILE_WRITE_ATTRIBUTES for the write-mode branches. + // FILE_READ_ATTRIBUTES is what `fstat` needs; `CreateFileW` adds it to + // every open, `NtCreateFile` does not. + let mut access_mask: u32 = w::READ_CONTROL | w::SYNCHRONIZE | w::FILE_READ_ATTRIBUTES; if (flags & O::RDWR) != 0 { access_mask |= w::GENERIC_READ | w::GENERIC_WRITE; } else if (flags & O::WRONLY) != 0 { @@ -6963,17 +6750,16 @@ fn openat_windows_impl(dir: Fd, norm: &bun_core::WStr, flags: i32, perm: Mode) - (false, _, false) => w::FILE_OPEN, }; - 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 (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 { @@ -7012,7 +6798,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 ── @@ -7914,18 +7700,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; @@ -8120,8 +7902,9 @@ pub mod net { // Same nominal types as `bun_sys::posix::sockaddr*` (see comment there) // 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::*; @@ -8131,8 +7914,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. // @@ -8349,9 +8132,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]>() }; @@ -8643,59 +8425,14 @@ 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 @@ -8703,15 +8440,13 @@ mod win_symlink_impl { if !matches!(err.get_errno(), E::NOENT | E::EXIST) { 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, @@ -8744,11 +8479,7 @@ mod win_symlink_impl { }, } } - 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. @@ -8784,11 +8515,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::(..)` 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..fc8cbf0ff664 100644 --- a/src/sys/tmp.rs +++ b/src/sys/tmp.rs @@ -27,7 +27,7 @@ impl<'a> Tmpfile<'a> { O::CREAT | O::EXCL | O::CLOEXEC | O::WRONLY, perm, )? - .make_lib_uv_owned_for_syscall(Tag::open, ErrorCase::CloseOnFail)?; + .make_crt_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..14703c84c685 --- /dev/null +++ b/src/sys/windows/O.rs @@ -0,0 +1,131 @@ +//! The numeric `O_*` values Node exposes as `fs.constants` on Windows (MSVC +//! `_O_*` plus the `UV_FS_O_*` extensions), and the translation to and from +//! the POSIX-shaped `bun_sys::O` values Bun uses internally. + +pub const APPEND: i32 = 0x0008; +pub const CREAT: i32 = 0x0100; +pub const EXCL: i32 = 0x0400; +pub 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 const DIRECT: i32 = 0x0200_0000; +pub const DSYNC: i32 = 0x0400_0000; +pub 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 const NOFOLLOW: i32 = 0; +pub const NONBLOCK: i32 = 0; +pub const SYMLINK: i32 = 0; + +// `bun.O.*` — POSIX-shaped flag values Bun normalises to internally. +// +// Linux-style octal constants, not MSVC `libc::O_*` (CREAT=0x100, EXCL=0x400, +// APPEND=0x8). `crate::O` on Windows uses the same values for the flags it +// defines as non-zero; SYNC/DSYNC/DIRECT are zero there but are still +// recognised here when a caller passes the Linux value. +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 the Windows `fs.constants` values. +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; + } + // `open` rejects SYNC and DSYNC together (EINVAL). + // `bun.O.SYNC` (0o4010000) is a superset of `DSYNC` (0o10000), so check + // SYNC first to emit only `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 the Windows `fs.constants` values 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(windows_flags: i32) -> i32 { + let mut flags: i32 = 0; + if windows_flags & WRONLY != 0 { + flags |= bun_o::WRONLY; + } + if windows_flags & RDWR != 0 { + flags |= bun_o::RDWR; + } + if windows_flags & CREAT != 0 { + flags |= bun_o::CREAT; + } + if windows_flags & EXCL != 0 { + flags |= bun_o::EXCL; + } + if windows_flags & TRUNC != 0 { + flags |= bun_o::TRUNC; + } + if windows_flags & APPEND != 0 { + flags |= bun_o::APPEND; + } + if windows_flags & SYNC != 0 { + flags |= bun_o::SYNC; + } else if windows_flags & DSYNC != 0 { + flags |= bun_o::DSYNC; + } + if windows_flags & DIRECT != 0 { + flags |= bun_o::DIRECT; + } + if windows_flags & FILEMAP != 0 { + flags |= FILEMAP; + } + flags +} diff --git a/src/sys/windows/fs.rs b/src/sys/windows/fs.rs new file mode 100644 index 000000000000..92010c347d31 --- /dev/null +++ b/src/sys/windows/fs.rs @@ -0,0 +1,2095 @@ +//! 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::ptr; +use core::sync::atomic::{AtomicU32, Ordering}; + +use bun_core::{S, ZStr}; +use bun_windows_sys as win32; + +use super::{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; + +const SHARE_ALL: u32 = win32::FILE_SHARE_READ | win32::FILE_SHARE_WRITE | win32::FILE_SHARE_DELETE; + +// ────────────────────────────────────────────────────────────────────────── +// Types +// ────────────────────────────────────────────────────────────────────────── + +/// Seconds + nanoseconds since the Unix epoch. +#[repr(C)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct StatTimespec { + pub sec: i64, + pub nsec: i64, +} + +/// `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 st_flags: u64, + pub st_gen: u64, + pub atim: StatTimespec, + pub mtim: StatTimespec, + pub ctim: StatTimespec, + pub birthtim: StatTimespec, +} +impl Stat { + #[inline] + pub fn mtime(&self) -> StatTimespec { + self.mtim + } + #[inline] + pub fn mode(&self) -> u64 { + self.st_mode + } + #[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, + pub f_spare: [u64; 4], +} +// SAFETY: integers only; all-zero is a valid value. +unsafe impl bun_core::ffi::Zeroable for StatFS {} + +/// 100 ns ticks between 1601-01-01 (FILETIME epoch) and 1970-01-01. +const WIN_TO_UNIX_TICK_OFFSET: i64 = super::EPOCH_DIFFERENCE_100NS; +const TICKS_PER_SEC: i64 = 10_000_000; + +fn filetime_to_timespec(filetime: i64) -> StatTimespec { + let t = filetime - WIN_TO_UNIX_TICK_OFFSET; + let mut sec = t / TICKS_PER_SEC; + let mut nsec = (t % TICKS_PER_SEC) * 100; + if nsec < 0 { + sec -= 1; + nsec += 1_000_000_000; + } + StatTimespec { sec, nsec } +} + +// ────────────────────────────────────────────────────────────────────────── +// Helpers +// ────────────────────────────────────────────────────────────────────────── + +#[inline] +fn nt_error(status: NTSTATUS) -> Win32Error { + Win32Error::from_ntstatus(status) +} + +#[inline] +fn is_slash(c: u16) -> bool { + c == b'\\' as u16 || c == b'/' as u16 +} + +#[inline] +fn is_letter(c: u16) -> bool { + (c >= b'a' as u16 && c <= b'z' as u16) || (c >= b'A' as u16 && c <= b'Z' as u16) +} + +/// 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`.) +pub(crate) 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. +pub(crate) 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; + +/// 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. +struct WPath { + buf: bun_paths::w_path_buffer_pool::Guard, + start: usize, + len: usize, +} + +impl WPath { + fn new(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(); + let len = string_paths::to_w_path(&mut buf[..], path).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; + } + + let is_device_path = len >= 4 + && buf[0] == b'\\' as u16 + && buf[1] == b'\\' as u16 + && (buf[2] == b'?' as u16 || buf[2] == b'.' as u16) + && buf[3] == b'\\' as u16; + // A relative path counts with the directory Win32 resolves it against. + let is_absolute = len >= 3 && buf[1] == b':' as u16 && is_slash(buf[2]) + || len >= 2 && is_slash(buf[0]) && is_slash(buf[1]); + let resolved_len = if is_absolute || is_device_path { + len + } else { + // SAFETY: a zero-length query writes nothing and returns the + // length of the current directory, NUL included. + len + unsafe { bun_windows_sys::externs::GetCurrentDirectoryW(0, ptr::null_mut()) } + as usize + }; + if resolved_len < LONG_PATH_THRESHOLD || is_device_path { + return Ok(WPath { buf, start: 0, len }); + } + + // Room in front of the resolved path for `\\?\UNC\`. + const RESERVE: usize = 8; + let mut full = bun_paths::w_path_buffer_pool::get(); + let capacity = full.len() - RESERVE; + // SAFETY: `buf` is NUL-terminated; `full[RESERVE..]` is writable for + // `capacity` units. + let n = unsafe { + win32::GetFullPathNameW( + buf.as_ptr(), + 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]; + let (start, len) = if is_slash(resolved[0]) + && is_slash(resolved[1]) + && (resolved[2] == b'?' as u16 || resolved[2] == b'.' as u16) + { + // A device name resolved to `\\.\NAME`. + (RESERVE, n) + } else if is_slash(resolved[0]) && is_slash(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, n - 1 + 7) + } else { + let start = RESERVE - 4; + full[start..RESERVE].copy_from_slice(&super::LONG_PATH_PREFIX); + (start, n + 4) + }; + Ok(WPath { + buf: full, + start, + 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::to_w_path(&mut buf[..], path).len(); + Ok(WPath { buf, start: 0, len }) + } + + #[inline] + fn as_ptr(&self) -> *const u16 { + self.buf[self.start..].as_ptr() + } + + #[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 +// ────────────────────────────────────────────────────────────────────────── + +/// `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 { + // A zero mode would create the file read-only. + let mode = if mode == 0 { 0o644 } else { mode }; + let result = open_impl(path.as_bytes(), O::from_bun_o(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())), + } +} + +/// `flags` are `windows::O` values (the numbers `fs.constants` exposes). +fn open_impl(path: &[u8], flags: i32, mode: Mode) -> core::result::Result { + // `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. + 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 { + // Append-only access makes the kernel write at end-of-file atomically. + access &= !win32::FILE_WRITE_DATA; + access |= win32::FILE_APPEND_DATA; + } + + // All sharing modes, to match UNIX semantics: in particular the file can + // be deleted or renamed while it is open. + let share = if flags & O::EXLOCK != 0 { 0 } else { SHARE_ALL }; + + 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 => win32::OPEN_EXISTING, + O::CREAT => win32::OPEN_ALWAYS, + CREAT_EXCL | CREAT_TRUNC_EXCL => win32::CREATE_NEW, + O::TRUNC | TRUNC_EXCL => win32::TRUNCATE_EXISTING, + CREAT_TRUNC => win32::CREATE_ALWAYS, + _ => return Err(E::EINVAL), + }; + + let mut attributes = win32::FILE_ATTRIBUTE_NORMAL; + if flags & O::CREAT != 0 { + // The CRT only has a setter; read the umask by setting it twice. + let umask = crate::umask(0); + crate::umask(umask); + if (mode & !umask) & S::IWUSR == 0 { + attributes |= win32::FILE_ATTRIBUTE_READONLY; + } + } + + if flags & O::TEMPORARY != 0 { + attributes |= win32::FILE_FLAG_DELETE_ON_CLOSE | win32::FILE_ATTRIBUTE_TEMPORARY; + access |= win32::DELETE; + } + + if flags & O::SHORT_LIVED != 0 { + attributes |= win32::FILE_ATTRIBUTE_TEMPORARY; + } + + match flags & (O::SEQUENTIAL | O::RANDOM) { + 0 => {} + O::SEQUENTIAL => attributes |= win32::FILE_FLAG_SEQUENTIAL_SCAN, + O::RANDOM => attributes |= win32::FILE_FLAG_RANDOM_ACCESS, + _ => return Err(E::EINVAL), + } + + if flags & O::DIRECT != 0 { + // FILE_APPEND_DATA (part of FILE_GENERIC_WRITE) cannot be combined + // with FILE_FLAG_NO_BUFFERING. 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); + } + } + attributes |= win32::FILE_FLAG_NO_BUFFERING; + } + + match flags & (O::DSYNC | O::SYNC) { + 0 => {} + O::DSYNC | O::SYNC => attributes |= win32::FILE_FLAG_WRITE_THROUGH, + _ => return Err(E::EINVAL), + } + + // Makes it possible to open a directory. + attributes |= win32::FILE_FLAG_BACKUP_SEMANTICS; + + let wpath = WPath::new(path).map_err(|e| e.to_e())?; + match create_file(wpath.as_ptr(), access, share, 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()), + } +} + +// ────────────────────────────────────────────────────────────────────────── +// Vectored I/O +// ────────────────────────────────────────────────────────────────────────── + +/// One `ReadFile` per buffer. `position < 0` reads at the file pointer. +/// Stops at the first short read; 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(|| crate::RestoreFilePointer::new(fd)); + let mut total: usize = 0; + for buf in bufs { + if buf.len == 0 { + continue; + } + // SAFETY: a `PlatformIoVec` describes a writable buffer of `len` + // bytes that the caller keeps alive for the call. + let slice = unsafe { core::slice::from_raw_parts_mut(buf.base, buf.len as usize) }; + let result = if position < 0 { + crate::read(fd, slice) + } else { + crate::pread_at(fd, slice, position as u64 + total as u64) + }; + match result { + Ok(n) => { + total += n; + if n < slice.len() { + break; + } + } + Err(_) if total > 0 => break, + Err(e) => return Err(e), + } + } + 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(|| crate::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 as usize) }; + let result = if position < 0 { + crate::write(fd, slice) + } else { + crate::pwrite_at(fd, slice, position as u64 + total as u64) + }; + match result { + 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 | super::FILE_TYPE_DISK => stat_handle(handle, false), + super::FILE_TYPE_PIPE => Ok(synthetic(S::IFIFO, win32::FILE_DEVICE_NAMED_PIPE)), + _ => Err(Win32Error::INVALID_HANDLE), + } +} + +fn is_console(handle: HANDLE) -> bool { + let mut mode: u32 = 0; + // SAFETY: `mode` is a valid out-pointer; any handle value is acceptable. + unsafe { 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_slash(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(nt_error(status)); + } + if device_info.DeviceType == win32::FILE_DEVICE_NULL { + return Ok(null_device_stat()); + } + + 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: as above. + 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(nt_error(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, + ReparseTag: 0, + NumberOfLinks: file_info.StandardInformation.NumberOfLinks, + DeviceType: device_info.DeviceType, + DeviceCharacteristics: 0, + Reserved: 0, + VolumeSerialNumber: i64::from(volume_serial), + FileId128: [0; 16], + }, + 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_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(nt_error(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_slash(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_slash(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 { + nt_error(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)?; + + let mut device_info: win32::FILE_FS_DEVICE_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `dir_handle` is live; the out-buffers are writable. + let status = unsafe { + win32::ntdll::NtQueryVolumeInformationFile( + dir_handle.0, + &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(nt_error(status)); + } + + 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, + ReparseTag: 0, + // Not part of a directory listing. + NumberOfLinks: 1, + DeviceType: device_info.DeviceType, + DeviceCharacteristics: 0, + Reserved: 0, + VolumeSerialNumber: i64::from(volume_serial), + FileId128: [0; 16], + }, + do_lstat, + )) + })(); + + if let Some((index, separator)) = restore { + path[index] = separator; + } + result +} + +// ────────────────────────────────────────────────────────────────────────── +// Reparse points +// ────────────────────────────────────────────────────────────────────────── + +/// Aligned storage for a `REPARSE_DATA_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([u16; win32::MAXIMUM_REPARSE_DATA_BUFFER_SIZE / 2]); + +impl ReparseBuffer { + fn new() -> Self { + Self([0; win32::MAXIMUM_REPARSE_DATA_BUFFER_SIZE / 2]) + } + + /// 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_letter(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_utf8(&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())) +} + +fn unlink_or_rmdir(path: &[u8], is_rmdir: bool) -> core::result::Result<(), E> { + let to_e = |e: Win32Error| e.to_e(); + 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 mut info: win32::BY_HANDLE_FILE_INFORMATION = bun_core::ffi::zeroed(); + // SAFETY: `handle` is live; `info` is writable. + if unsafe { win32::GetFileInformationByHandle(handle.0, &mut info) } == 0 { + return Err(Win32Error::get().to_e()); + } + let attributes = info.dwFileAttributes; + let is_directory = attributes & win32::FILE_ATTRIBUTE_DIRECTORY != 0; + + if is_rmdir && !is_directory { + // What Node on Windows reports for `rmdir(file)`, not ENOTDIR. + return Err(E::ENOENT); + } + + if !is_rmdir && is_directory { + // 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(), + }); + } + } + + 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.0, + &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(()); + } + let error = nt_error(status); + // The filesystem (FAT, exFAT, most network redirectors) or the OS + // (before Windows 10 1709) does not support POSIX delete. + if !matches!( + error, + Win32Error::NOT_SUPPORTED | Win32Error::INVALID_PARAMETER | Win32Error::INVALID_FUNCTION + ) { + return Err(error.to_e()); + } + + if attributes & win32::FILE_ATTRIBUTE_READONLY != 0 { + // A classic delete refuses read-only files. The first handle was + // opened without FILE_WRITE_ATTRIBUTES because asking for it up front + // fails under Wine (https://bugs.winehq.org/show_bug.cgi?id=50771). + // SAFETY: `handle` is live. + let write_attributes = unsafe { + win32::ReOpenFile( + handle.0, + win32::FILE_WRITE_ATTRIBUTES, + SHARE_ALL, + win32::FILE_FLAG_OPEN_REPARSE_POINT | win32::FILE_FLAG_BACKUP_SEMANTICS, + ) + }; + if write_attributes == INVALID_HANDLE_VALUE { + return Err(Win32Error::get().to_e()); + } + let write_attributes = OwnedHandle(write_attributes); + let mut basic: win32::FILE_BASIC_INFORMATION = bun_core::ffi::zeroed(); + basic.FileAttributes = + (attributes & !win32::FILE_ATTRIBUTE_READONLY) | win32::FILE_ATTRIBUTE_ARCHIVE; + // SAFETY: `write_attributes` is live; the info struct matches the class. + let status = unsafe { + win32::ntdll::NtSetInformationFile( + write_attributes.0, + &mut io, + ptr::from_mut(&mut basic).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileBasicInformation, + ) + }; + if !win32::NT_SUCCESS(status) { + return Err(nt_error(status).to_e()); + } + } + + let mut disposition = win32::FILE_DISPOSITION_INFORMATION { DeleteFile: 1 }; + // SAFETY: `handle` is live; the info struct matches the class. + let status = unsafe { + win32::ntdll::NtSetInformationFile( + handle.0, + &mut io, + ptr::from_mut(&mut disposition).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileDispositionInformation, + ) + }; + if win32::NT_SUCCESS(status) { + Ok(()) + } else { + Err(nt_error(status).to_e()) + } +} + +/// `mode` is ignored: a Windows directory has no permission bits to set. +pub fn mkdir(path: &ZStr, _mode: Mode) -> Maybe<()> { + let result = WPath::new(path.as_bytes()) + .and_then(|wpath| { + // SAFETY: `wpath` is NUL-terminated. + if unsafe { win32::CreateDirectoryW(wpath.as_ptr(), ptr::null_mut()) } == 0 { + return Err(Win32Error::get()); + } + Ok(()) + }) + .map_err(|e| match e { + Win32Error::INVALID_NAME | Win32Error::DIRECTORY => E::EINVAL, + e => e.to_e(), + }); + 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 { + let mut v = bun_core::fast_random(); + 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| { + let error = match e { + super::GetFinalPathNameByHandleError::NameTooLong => { + Error::from_code(E::ENAMETOOLONG, Tag::realpath) + } + super::GetFinalPathNameByHandleError::FileNotFound => { + // The wrapper tried several forms of the query. Ask once + // more for the code itself: a volume that cannot answer + // is EISDIR to Node, not ENOENT. + // SAFETY: a zero-length buffer is valid; the call only sizes. + let sized = unsafe { + bun_windows_sys::externs::GetFinalPathNameByHandleW( + handle.0, + ptr::null_mut(), + 0, + 0, + ) + }; + if sized == 0 { + Error::from_win32(Win32Error::get(), Tag::realpath) + } else { + Error::from_code(E::ENOENT, Tag::realpath) + } + } + }; + error.with_path(path.as_bytes()) + })?; + let len = bun_paths::string_paths::from_w_path(&mut buf.0[..], resolved).len(); + 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 +// ────────────────────────────────────────────────────────────────────────── + +/// `SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE`, until the OS rejects it +/// (before Windows 10 1703 the flag is `ERROR_INVALID_PARAMETER`). +static UNPRIVILEGED_CREATE_FLAG: AtomicU32 = + AtomicU32::new(win32::SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); + +/// `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<()> { + loop { + let unprivileged = UNPRIVILEGED_CREATE_FLAG.load(Ordering::Relaxed); + let flags = unprivileged + | 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 Ok(()); + } + let error = Win32Error::get(); + if error == Win32Error::INVALID_PARAMETER && unprivileged != 0 { + UNPRIVILEGED_CREATE_FLAG.store(0, Ordering::Relaxed); + continue; + } + return Err(error); + } +} + +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_letter(target[0]) + && target[1] == b':' as u16 + && is_slash(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_slash(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(nt_error(status)); + } + + let mut set = |info: &mut win32::FILE_BASIC_INFORMATION| { + // SAFETY: `reopened` is live; the info struct matches the class. + let status = unsafe { + win32::ntdll::NtSetInformationFile( + reopened.0, + &mut io, + ptr::from_mut(info).cast(), + core::mem::size_of::() as u32, + win32::FILE_INFORMATION_CLASS::FileBasicInformation, + ) + }; + if win32::NT_SUCCESS(status) { + Ok(()) + } else { + Err(nt_error(status)) + } + }; + + // Toggling read-only does not take effect unless the archive attribute + // is set, so set it for the duration. + let clear_archive = info.FileAttributes & win32::FILE_ATTRIBUTE_ARCHIVE == 0; + if clear_archive { + info.FileAttributes |= win32::FILE_ATTRIBUTE_ARCHIVE; + set(&mut info)?; + } + + if mode & S::IWUSR != 0 { + info.FileAttributes &= !win32::FILE_ATTRIBUTE_READONLY; + } else { + info.FileAttributes |= win32::FILE_ATTRIBUTE_READONLY; + } + set(&mut info)?; + + if clear_archive { + info.FileAttributes &= !win32::FILE_ATTRIBUTE_ARCHIVE; + if info.FileAttributes == 0 { + // 0 means "leave unchanged". + info.FileAttributes = win32::FILE_ATTRIBUTE_NORMAL; + } + set(&mut info)?; + } + 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 mut sectors_per_cluster: u32 = 0; + let mut bytes_per_sector: u32 = 0; + let mut free_clusters: u32 = 0; + let mut total_clusters: u32 = 0; + let mut get_disk_free_space = |dir: *const u16| { + // SAFETY: `dir` is NUL-terminated; the out-pointers are valid. + if unsafe { + win32::GetDiskFreeSpaceW( + dir, + &mut sectors_per_cluster, + &mut bytes_per_sector, + &mut free_clusters, + &mut total_clusters, + ) + } == 0 + { + return Err(Win32Error::get()); + } + Ok(()) + }; + + match get_disk_free_space(wpath.as_ptr()) { + Ok(()) => {} + // `path` is a file; ask about the directory it is in. + Err(Win32Error::DIRECTORY) => { + let mut parent = bun_paths::w_path_buffer_pool::get(); + let mut file_part: *mut u16 = ptr::null_mut(); + // SAFETY: `wpath` is NUL-terminated; `parent` is writable for its + // length; `file_part` receives a pointer into `parent`. + let n = unsafe { + win32::GetFullPathNameW( + wpath.as_ptr(), + parent.len() as u32, + parent.as_mut_ptr(), + &mut file_part, + ) + } as usize; + if n == 0 || n >= parent.len() { + return Err(Win32Error::DIRECTORY); + } + if !file_part.is_null() { + // SAFETY: `file_part` points into `parent`, at or before its NUL. + unsafe { *file_part = 0 }; + } + get_disk_free_space(parent.as_ptr())?; + } + Err(e) => return Err(e), + } + + Ok(StatFS { + f_type: 0, + f_bsize: u64::from(bytes_per_sector) * u64::from(sectors_per_cluster), + f_blocks: u64::from(total_clusters), + f_bfree: u64::from(free_clusters), + f_bavail: u64::from(free_clusters), + f_files: 0, + f_ffree: 0, + f_spare: [0; 4], + }) +} + +// ────────────────────────────────────────────────────────────────────────── +// utimes +// ────────────────────────────────────────────────────────────────────────── + +/// `None` leaves the timestamp unchanged. +fn time_like_to_filetime( + time: TimeLike, + now: &mut Option, +) -> Option { + if time.nsec == crate::UTIME_OMIT { + return None; + } + if time.nsec == crate::UTIME_NOW { + return 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 + })); + } + let ticks = time + .sec + .saturating_mul(TICKS_PER_SEC) + .saturating_add(time.nsec / 100) + .saturating_add(WIN_TO_UNIX_TICK_OFFSET) as u64; + 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(nt_error(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 594bfda0a2cc..9285d75290a0 100644 --- a/src/sys/windows/mod.rs +++ b/src/sys/windows/mod.rs @@ -106,6 +106,7 @@ pub type PVOID = LPVOID; pub use bun_windows_sys::COORD; 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; @@ -192,24 +193,6 @@ 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 const NT_OBJECT_PREFIX: [u16; 4] = [b'\\' as u16, b'?' as u16, b'?' as u16, b'\\' as u16]; @@ -219,9 +202,7 @@ pub(crate) const LONG_PATH_PREFIX: [u16; 4] = pub(crate) const NT_OBJECT_PREFIX_U8: [u8; 4] = *b"\\??\\"; pub const LONG_PATH_PREFIX_U8: [u8; 4] = *b"\\\\?\\"; -#[cfg(windows)] pub use bun_paths::PathBuffer; -#[cfg(windows)] pub use bun_paths::WPathBuffer; pub use bun_windows_sys::HANDLE; @@ -416,7 +397,9 @@ pub fn last_system_errno() -> SystemErrno { Win32Error::get().to_system_errno() } -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. @@ -556,7 +539,7 @@ pub fn GetCurrentProcessId() -> DWORD { 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; @@ -600,8 +583,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 @@ -1255,7 +1236,7 @@ pub(crate) const JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK: DWORD = 0x00001000; /// `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; @@ -1318,8 +1299,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 @@ -1532,7 +1511,7 @@ pub fn become_watcher_manager() -> ! { } 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(); @@ -1753,11 +1732,6 @@ pub(crate) fn spawn_watcher_child( /// broke when I just used it. Not sure. ... but this works! #[unsafe(no_mangle)] pub(crate) extern "C" fn Bun__LoadLibraryBunString(str_: &bun_core::String) -> *mut c_void { - #[cfg(not(windows))] - { - compile_error!("unreachable"); - } - let mut buf = bun_paths::w_path_buffer_pool::get(); // The path is JS-supplied; over-length input must surface as the same // `null + GetLastError()` shape `LoadLibraryExW` itself would yield, not @@ -2052,10 +2026,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. diff --git a/src/sys_jsc/fd_jsc.rs b/src/sys_jsc/fd_jsc.rs index cd2f6faecd63..9caa2250e5bb 100644 --- a/src/sys_jsc/fd_jsc.rs +++ b/src/sys_jsc/fd_jsc.rs @@ -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,7 +77,7 @@ 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() { Ok(fd) => fd, Err(_) => { self.close(); @@ -93,15 +91,15 @@ impl FdJsc for Fd { 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 call make_crt_owned() 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 +107,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/uws/lib.rs b/src/uws/lib.rs index 9832300d1b48..b659e5dd771c 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, @@ -1300,7 +1298,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 5add2c03a003..86b9910c19ad 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 843061721f4b..30b679bdf29b 100644 --- a/src/uws_sys/Loop.rs +++ b/src/uws_sys/Loop.rs @@ -4,27 +4,27 @@ 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 +/// A `now_ns` the caller has no reading to share for. The tick takes its own only if /// it reaches the idle sweep, so passing this costs nothing on the paths that never park. 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 +37,35 @@ 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 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. 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,25 @@ 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, pending_wakeups) == offset_of!(Loop, num_polls) + 28); + assert!(offset_of!(Loop, current_ready_events) == offset_of!(Loop, num_polls) + 32); +}; /// Loop handler trait with optional `pre`/`post` hooks. Implementors override /// `PRE`/`POST` if they have them. @@ -109,12 +140,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 +158,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; @@ -256,7 +293,7 @@ impl PosixLoop { } /// `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 + /// `timer::All::get_timeout`), reused by the tick's idle-sweep rate limit rather /// than read again. `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 @@ -298,188 +335,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); @@ -488,7 +349,7 @@ 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`, @@ -506,10 +367,7 @@ mod c { 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, @@ -517,10 +375,7 @@ mod c { ); 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); } } @@ -538,10 +393,9 @@ unsafe extern "C" { 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 1c9e2e9dd96e..f5daf7a91629 100644 --- a/src/uws_sys/Response.rs +++ b/src/uws_sys/Response.rs @@ -340,7 +340,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 865596e96e4b..35366c3fa53e 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..a957c0d6f8b2 --- /dev/null +++ b/src/uws_sys/iocp.rs @@ -0,0 +1,122 @@ +//! 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`. +#![cfg(windows)] + +use core::ffi::{c_int, c_void}; + +use crate::Loop; + +pub type HANDLE = *mut c_void; + +/// `OVERLAPPED`. +#[repr(C)] +pub struct Overlapped { + pub internal: usize, + pub internal_high: usize, + pub offset: u32, + pub offset_high: u32, + pub event: HANDLE, +} + +/// `OVERLAPPED_ENTRY`: one dequeued completion packet. +#[repr(C)] +pub struct OverlappedEntry { + pub completion_key: usize, + pub overlapped: *mut Overlapped, + pub internal: usize, + pub bytes_transferred: u32, +} + +pub type CompleteFn = unsafe extern "C" fn(*mut Loop, *mut Op, *mut OverlappedEntry); + +/// `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.internal_high`. +#[repr(C)] +pub struct Op { + pub overlapped: Overlapped, + pub complete: Option, +} + +impl Op { + pub const fn new(complete: CompleteFn) -> Self { + Self { + overlapped: Overlapped { + internal: 0, + internal_high: 0, + offset: 0, + offset_high: 0, + event: core::ptr::null_mut(), + }, + complete: Some(complete), + } + } + + /// 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.internal_high + } +} + +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; +} + +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); + + 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 fn us_internal_winsock_ensure(); +} diff --git a/src/uws_sys/lib.rs b/src/uws_sys/lib.rs index ad9170bca813..f17d4acbd22a 100644 --- a/src/uws_sys/lib.rs +++ b/src/uws_sys/lib.rs @@ -390,6 +390,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"] @@ -408,10 +410,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"] @@ -440,7 +438,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 @@ -475,23 +473,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, PosixLoop}; -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 5c1e40665e62..9466daed6264 100644 --- a/src/uws_sys/libuwsockets.cpp +++ b/src/uws_sys/libuwsockets.cpp @@ -1506,11 +1506,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) @@ -1739,8 +1734,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 016e77932502..ce5ef85d716a 100644 --- a/src/uws_sys/socket.rs +++ b/src/uws_sys/socket.rs @@ -398,7 +398,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 896650e2488f..4b9c324452e1 100644 --- a/src/uws_sys/us_socket_t.rs +++ b/src/uws_sys/us_socket_t.rs @@ -442,7 +442,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..71c3f87efcc6 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 @@ -78,11 +78,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. @@ -97,7 +93,6 @@ pub struct OVERLAPPED { } /// `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 { @@ -182,8 +177,8 @@ 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"))] @@ -283,6 +278,7 @@ pub const FILE_SHARE_DELETE: ULONG = 0x0000_0004; pub const FILE_ATTRIBUTE_READONLY: DWORD = 0x0000_0001; pub const FILE_ATTRIBUTE_HIDDEN: DWORD = 0x0000_0002; pub const FILE_ATTRIBUTE_DIRECTORY: DWORD = 0x0000_0010; +pub const FILE_ATTRIBUTE_ARCHIVE: DWORD = 0x0000_0020; pub const FILE_ATTRIBUTE_NORMAL: DWORD = 0x0000_0080; pub const FILE_ATTRIBUTE_TEMPORARY: DWORD = 0x0000_0100; pub const FILE_ATTRIBUTE_REPARSE_POINT: DWORD = 0x0000_0400; @@ -311,17 +307,54 @@ 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 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_DELETE_ON_CLOSE: DWORD = 0x0400_0000; +pub const FILE_FLAG_SEQUENTIAL_SCAN: DWORD = 0x0800_0000; +pub const FILE_FLAG_RANDOM_ACCESS: DWORD = 0x1000_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; // Reparse tags (`winnt.h`). `IsReparseTagNameSurrogate` == bit 29: the reparse // point names another filesystem entity (symlink, mount point). Non-surrogate @@ -337,6 +370,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 +404,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)] @@ -381,6 +472,7 @@ impl FILE_INFORMATION_CLASS { pub const FileDispositionInformation: Self = Self(13); pub const FileAllInformation: Self = Self(18); pub const FileEndOfFileInformation: Self = Self(20); + pub const FileIdFullDirectoryInformation: Self = Self(38); pub const FileDispositionInformationEx: Self = Self(64); } @@ -502,6 +594,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; @@ -800,10 +895,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). // ────────────────────────────────────────────────────────────────────────── @@ -1008,6 +1099,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 +1272,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,6 +1300,7 @@ 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); @@ -1240,6 +1335,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 — @@ -1426,9 +1522,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( @@ -1849,6 +1945,63 @@ unsafe extern "system" { fInfoLevelId: GET_FILEEX_INFO_LEVELS, lpFileInformation: LPVOID, ) -> BOOL; + + 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, + nBufferLength: DWORD, + lpBuffer: LPWSTR, + lpFilePart: *mut LPWSTR, + ) -> DWORD; + + pub fn GetDiskFreeSpaceW( + lpRootPathName: LPCWSTR, + lpSectorsPerCluster: *mut DWORD, + lpBytesPerSector: *mut DWORD, + lpNumberOfFreeClusters: *mut DWORD, + lpTotalNumberOfClusters: *mut DWORD, + ) -> BOOL; + + 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" { diff --git a/src/windows_sys/lib.rs b/src/windows_sys/lib.rs index b2b811cc2c76..942a7013865c 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 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..42a8a59f4b05 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. + // Some platforms may materialize both Debug and non-Debug prototypes. 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 ffeb5be84746..faa90df089c5 100644 --- a/test/cli/run/filter-workspace.test.ts +++ b/test/cli/run/filter-workspace.test.ts @@ -1006,9 +1006,9 @@ describe("selectors", () => { // #20319: on Windows, `bun --filter` / `bun run --parallel` spawn each script // as `bun exec "