Skip to content

rust: Port util/pipe-windows.c to obs-util - #259

Merged
zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:port/pipe-windows
Oct 11, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:port/pipe-windows

Conversation

@b7216309-jpg

@b7216309-jpg b7216309-jpg commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #258. This is the pipe-windows.c item of #153; the POSIX half was #78. With it, the pipe port covers every OS.

Commits

  1. C characterization (test/cmocka/test_pipe.c). The process cases were POSIX-only; this adds Windows versions with cmd.exe children:

    • read and write mode, stderr
    • exit codes as (int)DWORD: 255 and 256 stay as they are, exit -1 is -1, and there is no char cast as in pipe-posix.c
    • a missing program and an empty command line
    • the type: only a leading r/w is accepted, unlike POSIX
    • NULLs
    • how os_process_pipe_create2 quotes arguments, read back through cmd /c echo, which prints its command line as given

    All pass against the current C.

  2. RED.

    • Oracle: oracle/pipe_windows.c compiles pipe-windows.c as oracle_* on Windows. build.rs skips *_windows.c elsewhere, as it does *_posix.c.
    • Shims: the process shims build on Windows over a Windows Pipe with the POSIX API. create_logged/create2_logged let the shim log a failed CreateProcessW through blog, like C.
    • Quoting: windows_command_line (create2's quoting) is platform-neutral, so the quoting is tested on every OS.
    • Wiring: pipe-windows.c is behind ENABLE_RUST_LIBOBS, and the process exports lose their unix marker. export_list.rs keeps the marker check for future per-OS ports but no longer expects a Unix-only shim.
  3. GREEN: obs_util::pipe::windows.

Why Win32 and not std::process::Command

Command differs from CreateProcessW(NULL, cmd_line, …, bInheritHandles=TRUE, CREATE_NO_WINDOW, …) in ways a caller can see:

  • it resolves the program itself, so the search order differs
  • it can't set STARTF_FORCEOFFFEEDBACK
  • it restricts which handles the child inherits

So the core calls the same Win32 functions in the same order. They're declared in the module against kernel32.lib, with size checks on the three structs, rather than taken from windows-sys. A first version used windows-sys, and its import records for DLLs that std also imports ended up twice in libobs_rust.lib, so the Rust-ON obs.dll link failed with LNK2005 __IMPORT_DESCRIPTOR_api-ms-win-core-synch-l1-2-0. The command line is converted like os_utf8_to_wcs_ptr on Windows: BOM dropped, MultiByteToWideChar(CP_UTF8, 0, …), so invalid UTF-8 becomes U+FFFD.

Intentional difference: when creating a pipe fails half-way, C leaks the handles it already made (both stderr ends, or the stdio pair if the second CreatePipe fails). The port closes them. Only the handle count can tell.

Tests

  • Tier 1 (tests/pipe.rs): the quoting (the cmocka case, runs of backslashes before a quote and at the end, empty and lone programs) on every OS. On Windows, the cmocka process cases, the logged error codes (2 for a missing program, 87 for an empty command line) and the BOM.
  • Tier 3 (tests/pipe_parity.rs, Windows), against the oracle:
    • a table of commands, including spawn failures, a BOM and invalid UTF-8
    • create2 cases
    • random arguments read back through cmd /c echo
    • random write payloads and read chunk sizes
    • a write after the child exited (sync via stderr EOF, no sleeps)
    • NULLs
  • Serialized spawning: children inherit every inheritable handle, as in C. Two tests spawning at once can hand each other's pipe ends to their children and keep them open, so the Windows process tests take a lock, as the cmocka cases also run one at a time.

Mutation check:

  • Dropping the doubling of backslashes before a closing quote fails the Tier 1 quoting tests, and on its own Tier 3 (process_create2_matches_oracle, create2_quoting_matches_oracle).
  • Casting the exit code through char fails pipe_exit_code_mirrors_test_pipe_exit_code.

Run locally on Windows (MSVC):

  • test_pipe.c against the real pipe-windows.c: 14/14
  • the same test_pipe.c linked against libobs_rust.lib: 14/14, with the CreateProcessW failed: 2/87 lines from blog
  • cargo test --workspace
  • clippy with -D warnings for Windows, x86_64 Linux and aarch64 macOS

aarch64 Linux shows only the existing ffi/dstr.rs casts. Windows CI is the real gate for the CMake side.

🤖 Generated with Claude Code

b7216309-jpg and others added 2 commits October 11, 2026 22:02
The process cases of test_pipe were POSIX-only. Add Windows versions
with cmd.exe children:

- read and write mode, stderr, and exit codes returned as (int)DWORD
  (255 and 256 stay, -1 is -1; there is no char cast as in pipe-posix.c)
- how os_process_pipe_create2 quotes arguments into one command line,
  read back through cmd's echo: empty args and args with a space or tab
  are quoted, quotes are escaped, and backslashes before a quote or the
  closing quote are doubled
- a missing program and an empty command line fail to spawn
- only a leading 'r' or 'w' is accepted as the type, unlike POSIX
- NULL arguments

read_all moves out of the POSIX-only block. All cases pass against the
current C.

Refs obs-rust#258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- oracle: pipe-windows.c compiled on Windows as oracle_* (build.rs now
  skips *_windows.c wrappers elsewhere, like *_posix.c)
- obs-util: the process shims are built on Windows too, over a Windows
  Pipe with the POSIX API, plus create_logged/create2_logged so the
  shim logs a failed CreateProcessW like C. windows_command_line, the
  create2 quoting, is platform-neutral. All of it is todo!() here.
- Tier 1 (tests/pipe.rs): the quoting on every OS, and the Windows
  cases of test_pipe with cmd.exe children
- Tier 3 (tests/pipe_parity.rs): commands, create2 (including random
  arguments read back through cmd's echo), chunked reads, writes, a
  write to an exited child and nulls against the oracle. Spawning is
  serialized: children inherit every inheritable handle, as in C.
- util/pipe-windows.c is left out of libobs with ENABLE_RUST_LIBOBS, and
  the process exports lose their `unix` marker

Refs obs-rust#258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b7216309-jpg
b7216309-jpg requested a review from zackees as a code owner October 11, 2026 20:03
The Windows half of the pipe port, in obs_util::pipe::windows. It calls
Win32 as the C does rather than std::process::Command, which resolves
the program itself (so the search order differs), cannot set
STARTF_FORCEOFFFEEDBACK and limits the handles a child inherits:

- CreatePipe with inheritable ends, our ends made private, then
  CreateProcessW with no application name, CREATE_NO_WINDOW and every
  inheritable handle inherited; the command line is converted like
  os_utf8_to_wcs_ptr (BOM dropped, invalid UTF-8 as U+FFFD)
- only a leading 'r' or 'w' is a type; one ReadFile/WriteFile per
  call with the length cast to DWORD; the exit code is (int)DWORD
- a failed CreateProcessW is logged by the shim through blog, with the
  error code and not the command line
- windows_command_line is create2's quoting

Intentional difference: C leaks the handles it already made when
creating a pipe fails half-way; they are closed here.

Mutation check: dropping the doubling of backslashes before a closing
quote fails the Tier 1 quoting tests and, on its own, Tier 3
(process_create2_matches_oracle and create2_quoting_matches_oracle);
casting the exit code through char fails
pipe_exit_code_mirrors_test_pipe_exit_code.

Closes obs-rust#258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=1d7a4a40811e727ab14814994b9beb8590952992 secs=94 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791749583,"gate":"rust/all/fmt","host":"linux-x86_64","key":"c3a46f924323191e2d7625457f29decef671038145e924d6ec011a5df3c33a29","lane":"lint","parents":["340f5d17a4c5669de1b98bdc148f020f02ac1361"],"secs":17,"stamp":"5560b1ab6fce619b9f21ab0bc74f4b8c","tree":"1d7a4a40811e727ab14814994b9beb8590952992","v":1,"via":"run"}
Ci-Attestation: {"at":1791749583,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"c3a46f924323191e2d7625457f29decef671038145e924d6ec011a5df3c33a29","lane":"lint","parents":["340f5d17a4c5669de1b98bdc148f020f02ac1361"],"secs":17,"stamp":"85996a0237e52f789a6703c174255a75","tree":"1d7a4a40811e727ab14814994b9beb8590952992","v":1,"via":"run"}
Ci-Attestation: {"at":1791749583,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"6001cd1e5f0967967f9b5f93e55ff2f3633af2a4b47e59dcf9d83beebfefda36","lane":"rust-test","parents":["340f5d17a4c5669de1b98bdc148f020f02ac1361"],"secs":27,"stamp":"fe48744c5b06f180730a270fb14c0f8f","tree":"1d7a4a40811e727ab14814994b9beb8590952992","v":1,"via":"run"}
Ci-Attestation: {"at":1791749583,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"0522de807392758f6c3e6a0289978640974392ba6079361c612e39def8dc6d30","lane":"tier2-linux","parents":["340f5d17a4c5669de1b98bdc148f020f02ac1361"],"secs":49,"stamp":"21380b44553346a5d921f6ced545ee9a","tree":"1d7a4a40811e727ab14814994b9beb8590952992","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Rebased onto current master, Linux-verified and stamped. The new head is 306ec7aba.

  • The test_pipe.c change only moves the #ifndef _WIN32 guards and the skip comment. The POSIX cases are unchanged, and the Windows cases are added under #else. That extends the characterization without weakening it, so it's fine.
  • Linux, where this port must not change anything: pipe 18 of 18, pipe_parity 9 of 9 and export_list 3 of 3 passed. Tier 2 passed 86 of 86 with ENABLE_RUST_LIBOBS OFF and 86 of 86 with it ON, and the local gate passed.
  • I can't run the Windows RED and GREEN here. On the new head, the Windows Rust Tests (parity against the C oracle) and the Windows Tier 2 cmocka run are the GREEN check. The RED commit's stubs are described in your PR.

Merging once CI, including both Windows jobs, is green.

@zackees
zackees merged commit 96a1f79 into obs-rust:master Oct 11, 2026
22 of 23 checks passed
@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Thanks for the rebase and the Linux run. The Windows Tier 2 failure was mine. windows-sys (through windows-link) put a second set of import records for api-ms-win-core-synch-l1-2-0 into libobs_rust.lib, and linking all of it into obs.dll failed with LNK2005 __IMPORT_DESCRIPTOR_…. I reproduced that locally by linking a DLL with /WHOLEARCHIVE:libobs_rust.lib.

The core now declares its ten kernel32 functions and three structs itself, like ffi::bmem does for malloc, with size asserts against the SDK. windows-sys is gone, so Cargo.toml and Cargo.lock match master again. The changes are folded into RED and GREEN, and I pushed with a lease on your 306ec7aba.

Locally on Windows: the /WHOLEARCHIVE DLL now links; test_pipe.c passes 14/14 against the rebuilt libobs_rust.lib; pipe passes 18/18, pipe_parity 10/10 and export_list 3/3; clippy -D warnings is clean.

@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Windows Rust Tests pass, but Rust Tier 2 windows fails to link with ENABLE_RUST_LIBOBS=ON (OFF passed 62 of 62):

libobs_rust.lib(api-ms-win-core-synch-l1-2-0.dll) : error LNK2005: __IMPORT_DESCRIPTOR_api-ms-win-core-synch-l1-2-0 already defined in libobs_rust.lib(api-ms-win-core-synch-l1-2-0.dll)
obs.dll : fatal error LNK1169: one or more multiply defined symbols found

Likely cause: windows-sys 0.61 links through windows-link, which uses raw-dylib and emits its own import descriptor for each DLL it calls into. Rust's std also imports api-ms-win-core-synch-l1-2-0 via raw-dylib. A normal static-lib link dedups the two, but libobs links libobs_rust with WHOLEARCHIVE (libobs/cmake/rust.cmake), which forces every member in, so both descriptors land in obs.dll. Rust Tests don't hit this because they link through rustc, not WHOLEARCHIVE.

Options, roughly in order of effort:

  1. Avoid the synch functions in api-ms-win-core-synch (WaitOnAddress and the like, plus anything windows-sys maps there). For example, take WaitForSingleObject and the pipe calls from your own #[link(name = "kernel32")] extern "system" declarations, so the only synch descriptor is std's.
  2. Use windows-sys 0.52, which links through windows-targets import libs (not raw-dylib), if that resolves the duplicate on your machine.
  3. Narrow WHOLEARCHIVE for Windows in rust.cmake. That's a bigger change and would need its own red/green.

I can't reproduce this on Linux, so I'll leave the fix to you, since you're on Windows. It's ready to merge as soon as the Windows Tier 2 job is green.

@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

This merged at 306ec7aba, a few minutes before I pushed the link fix to the branch, so master has the Windows Rust-ON LNK2005. The fix is #261. It's your option 1, re-applied on master: own kernel32 declarations, no windows-sys.

zackees pushed a commit that referenced this pull request Oct 11, 2026
#259 merged with the core taking its Win32 calls from windows-sys 0.61,
which links through windows-link's raw-dylib import records. std
imports api-ms-win-core-synch-l1-2-0 the same way, and libobs links
libobs_rust.lib with WHOLEARCHIVE, so obs.dll gets both records and the
Windows ENABLE_RUST_LIBOBS=ON build fails:

  libobs_rust.lib(api-ms-win-core-synch-l1-2-0.dll) : error LNK2005:
  __IMPORT_DESCRIPTOR_api-ms-win-core-synch-l1-2-0 already defined
  obs.dll : fatal error LNK1169

The module now declares its ten kernel32 functions and three structs
itself, against kernel32.lib like ffi::bmem declares malloc, with size
checks against the SDK, and obs-util no longer depends on windows-sys.
Behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

port: util/pipe-windows.c to obs-util

2 participants