Skip to content

obs-util: Port the window setting's string codec - #3386

Merged
zackees merged 7 commits into
masterfrom
fix/rust-port-the-window-setting-s-s-422
Nov 1, 2026
Merged

zackees merged 7 commits into
masterfrom
fix/rust-port-the-window-setting-s-s-422

Conversation

@rdoupe

@rdoupe rdoupe commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3380

What and why

Window capture and game capture on Windows save the chosen window as one string, title:class:exe, with each part escaped (# as #22, then : as #3A). When a source loads, ms_build_window_strings() splits that string and decodes each part. This PR ports that codec to obs_util::window_match, the module that already holds the window helpers' match decisions (#3156). Nothing changes in behaviour.

  1. Move (libobs: Move the window setting's string codec): the codec's core moves out of libobs/util/windows/window-helpers.c.h into libobs/util/windows/window-match.c.h (group libobs.ported-window-match), as three hidden functions declared in window-match.h: ms_window_match_encode() (snprintf-style), ms_window_match_decode() (in place) and ms_window_match_split() (the first three fields as offsets and lengths). window-helpers.c keeps every allocation and dstr_* call, add_window()'s description and join, and insert_preserved_val() and ms_check_window_property_setting() with their property-list calls. ms_build_window_strings() stays EXPORT, and its outputs stay exactly as they were: all three NULL for a NULL string or fewer than three fields, and NULL for an empty field (dstr_ncopy(), like the dstr_copy("") it replaces).
  2. Characterization (test: Characterize the window string codec): test_window_match_steps gets five new cases. They cover the encode and its truncation, the decode order (#2222 is #22, #223A is #3A, and lower-case #3a stays as it is), the split of "", ":", "a:b", "::", ":::" and "a:b:c:d", and a round trip of 169 title/class/exe triples.
  3. RED (obs-util: Test the window string codec): Tier 1 tests against todo!() stubs. The shims and their rust-hidden.d names come in this commit too, so the hybrid Windows libobs links at every commit.
  4. GREEN (obs-util: Port the window string codec): encode(), encoded_len(), encode_into(), decode() and split() in obs_util::window_match, on bytes, reading each string up to its first NUL as the C does.
  5. Tier 3 (obs-util: Compare the window string codec with C): the oracle, shims and core are compared over NULL, 19 hand-picked strings, every string up to 5 bytes over #:23Aa, 0xC3 and 0x01, and proptests, including the round trip of the window list's string through the shims. Each encode is checked at every buffer size. Each decode compares the whole buffer, including the bytes after the new NUL. Each split compares all six outputs. A window_match/codec/32 pair is added to the port_comparison bench.
  6. Docs (docs: Document the window string codec port): the rust/README.md row and the module docs. naming.md already maps libobs/util/ to obs-util.

The move was checked in a scratch harness. It extracts origin/master's encode_dstr(), decode_str(), ms_build_window_strings() and add_window()'s string building verbatim, and runs them next to this branch's versions over the real C dstr, on 299,614 strings (NULL, every string up to 6 bytes over #:23Aax and 0xC3, and 20 hand-picked ones). It ran under GCC and Clang, at -O0 and -O2, with and without ASan/UBSan: the logs (3,894,989 lines) were identical and nothing leaked. Of 23 planted mutants it caught 21. The other two are equivalent: one writes the last byte, which the NUL then overwrites, and the other drops a NULL-string return that the split already makes.

No intentional difference. Mutation check: the Tier 3 tests catch all 10 mutants of the Rust core (each escape changed, the decode passes swapped, a match that needs a fourth byte or restarts inside itself, no NUL after a pass, a write past the room, a split that accepts two fields, a field start off by one, and the measured length left unescaped).

  1. Tier 2 against Rust (test: Run the window match steps against Rust): test_window_match_steps_rust builds the unchanged C test against libobs_rust-static, so it runs the Rust shims of all eight window match functions. obs-util's archive member also needs libuuid, GIO and two log helpers that libobs hides, so the test links the libraries and compiles its own util/base-variadic.c, the same way test_effect_param_types_steps_rust compiles its adapters. It runs on Linux only: on macOS and Windows that archive member pulls in other host code, and no PR lane builds this test there to check the link. Tier 3 compares the same shims with the C on every host.

No upstream issue.

RED → GREEN

  • RED commit: 1d0242a. soldr cargo test -p obs-util --lib -- platform::window_match:: fails 7 tests with not yet implemented: steps_encode, steps_encode_truncates, steps_decode, steps_split, steps_round_trip, edge_codec_against_model and edge_prop_codec. The other 22 pass. On RED, local_gate.py --test-diff origin/master failed only those 7 (5966 passed, 6 skipped), and --lane lint passed.
  • GREEN: Tier 1 passes 29/29 and Tier 3 passes 16/16 (soldr cargo test -p obs-util --lib -- platform::window_match). local_gate.py --test-diff origin/master ran 5978 tests: 5978 passed, 6 skipped (the same 6 skip on RED). --lane lint and --lane cross pass, and so does soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets -- -D warnings. Tier 2: test_window_match_steps passes 20/20 against the C build, hybrid libobs and libobs-c-reference (rebuilt for the run). test_window_match_steps_rust passes 20/20 against the Rust shims. A planted Rust mutant (# escaped as #23) makes it fail encode, encode_truncates and round_trip. The exported symbol sets are identical on the C, hybrid and c-ref builds (1,767 each), and the three new shims are local (t) in hybrid libobs. The moved C and the new test compile clean with clang-cl /W4 /WX for MSVC, and window-helpers.c.h compiles with clang-cl and mingw. port_comparison passes its allocation check. Runs were on cfb8d69, rebased onto master 93b3ec2: ./lint, --lane lint, --lane cross, MSVC clippy and the bench pass. ./unittest's Rust tiers ran 6056 tests: 6056 passed, 6 skipped. Its Docker Tier 2 lane can't reach Docker on this host, so Tier 2 ran natively with ctest, as above.
  • Test cost: new tests: longest 0.43 s at the full level and 0.15 s reduced (debug build), peak RSS 13.6 MB over 25 runs, no threads of their own.
  • Real app (UI, protocol output or logging changes): not applicable. This is a port with no change in behaviour.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (none)

@rdoupe
rdoupe force-pushed the fix/rust-port-the-window-setting-s-s-422 branch from 103b165 to b0f6462 Compare November 1, 2026 09:53
@obs-rust-project-manager obs-rust-project-manager Bot moved this to In progress in obs-rust Multiview Nov 1, 2026
Behaviour-neutral extraction before the Rust port. Window and game
capture save the chosen window as "title:class:exe", each part escaped.
The codec's core moves from libobs/util/windows/window-helpers.c.h to
libobs/util/windows/window-match.c.h, as three hidden functions declared
without EXPORT in window-match.h:

- ms_window_match_encode(): encode_dstr()'s two dstr_replace() passes,
  '#' to "#22", then ':' to "#3A", as one pass over the bytes (the
  second pass never met a ':' the first wrote), snprintf-style: it
  returns the escaped length and writes only when out is not NULL;
- ms_window_match_decode(): decode_str()'s two passes in place, "#3A"
  to ':', then "#22" to '#', each left to right and never into what it
  wrote ("#2222" stays "#22"); it returns the new length;
- ms_window_match_split(): strlist_split(str, ':', true)'s first three
  fields as offsets and lengths, false with fewer than three.

window-helpers.c keeps every allocation: encode_dstr() sizes the
string, bmalloc()s it, fills it and moves it into the dstr only when
the length changed, so an empty dstr keeps its NULL array as
dstr_replace() did. decode_str() copies a field with dstr_ncopy(),
which gives NULL for an empty field as dstr_copy("") did, and decodes
it. ms_build_window_strings() keeps its NULL outputs, its NULL-string
return and its three decodes in order; the strlist allocation goes.
add_window()'s description and join are unchanged.

Checked in a scratch harness (not committed). It extracts
origin/master's encode_dstr(), decode_str(), ms_build_window_strings()
and add_window()'s string building verbatim, and this branch's with the
moved file, over the real C dstr and bmem. It runs 299,614 strings
(NULL, every string up to 6 bytes over "#:23Aax" and 0xC3, and 20
picked ones such as "#2222", "##3A22", "x#3a", "a:b:c:d" and high
bytes) through the split, the encode of a dstr, the window list's
description and join, and the split and decode of that join; an empty
dstr and empty join parts too. Each output and each dstr length is
logged. GCC and Clang, -O0 and -O2, with and without ASan and UBSan
without recovery: identical logs of 3,894,989 lines and no leaks.

23 planted mutants; the harness kills 21: each escape or its order
changed or dropped, the NUL left out, a decode pass at the end of the
string missed, a pass resuming inside its match, the passes swapped or
the second dropped, lower-case "#3a" decoded, too few fields accepted,
a field one byte long, the split from the last ':', and in the callers
the empty-dstr shortcut, dstr_ncopy() replaced by a copy that keeps
"", the decode dropped, the fields swapped or one too long, and an
output left unset. The two it cannot see are equivalent: a write of
the last byte, which the NUL then overwrites, and the NULL-string
return, which the split also makes.

Refs #3380.
test_window_match_steps pins the moved codec of the saved window
string before the port: the encode of NULL, plain, '#', ':' and high
bytes, its snprintf-style length and truncation; the decode of each
escape, "#3A" before "#22" with each pass once ("#2222" is "#22",
"#223A" is "#3A", "##3A22" is "#:22"), lower-case "#3a" left alone and
NULL; the split of NULL, "", ":", "a:b", "::", ":::", "a:b:c:d" and
empty fields, with all six outputs 0 when it fails; and the round trip
of add_window()'s join through the split and decode for 169 triples of
titles, classes and exes holding '#', ':' and both.

Refs #3380.
Tier 1 for the codec of the saved window string, against todo!()
stubs: encode(), encoded_len(), encode_into(), decode() and split() in
obs_util::window_match. The five new cases of test_window_match_steps
are ported 1:1 as steps_encode, steps_encode_truncates, steps_decode,
steps_split and steps_round_trip. New edge cases compare the core with
a model of origin/master's C (encode_dstr() and decode_str() as two
dstr_replace() passes each, the split as strlist_split(str, ':',
true)): every string up to 5 bytes over "#:23Aa", NUL and 0xC3, and a
proptest over strings rich in '#', ':', '2', '3' and 'A', with the
window list's join round-tripped.

The three shims in src/ffi/window_match.rs come with them, hidden
(rust-hidden.d), so the hybrid libobs on Windows links at every
commit; the oracle renames the moved functions to oracle_* so they
link next to the shims.

Refs #3380.
The codec of the saved window string in obs_util::window_match:
encode() and encoded_len() map '#' to "#22" and ':' to "#3A" byte by
byte, which is what encode_dstr()'s two passes do (the second never
meets a ':' the first wrote); encode_into() is the snprintf-style
ms_window_match_encode(); decode() runs the two passes of
ms_window_match_decode() in place, "#3A" then "#22", each left to
right with a NUL after its result; split() gives the first three
':'-separated fields, empty ones kept. Each reads its input up to the
first NUL, as the C does, and works on bytes, not UTF-8. Nothing here
can panic or allocate except encode().

The shims, already in place, now reach a real core. Tier 1 passes
(29 of 29).

Refs #3380.
Tier 3 for the codec: oracle (the moved window-match.c.h), shims and
core over NULL, 19 picked strings, and every string up to 5 bytes over
"#:23Aa", 0xC3 and 0x01 (all up to 3 bytes at every level, a share of
the longer ones at the reduced level). Each encode runs with a NULL
buffer and at every size from 0 to two past the escaped length, and
the whole buffer is compared; each decode compares the whole buffer,
the bytes past the new NUL included; each split compares all six
outputs, prefilled. Proptests run strings rich in '#', ':', '2', '3'
and 'A', and the window list's title:class:exe round trip through the
shims. No intentional difference.

Mutation check: 10 mutants of the core, each killed by these tests
(each escape changed, the decode passes swapped, a match needing a
fourth byte or resuming inside itself, no NUL after a pass, a write
past the room, two fields accepted, a field start off by one, and the
measured length unescaped).

The port_comparison bench gets a window_match/codec/32 pair: each
window's saved string built, split and decoded in stack buffers.

Refs #3380.
The rust/README.md row of window-match.c.h and the window_match module
docs name the codec of the saved window string: what it encodes and
decodes, in which order, what window-helpers.c keeps, and that it
works on bytes. naming.md already maps libobs/util/ to obs-util, and
obs-util's description and lib.rs list no module detail.

Fixes #3380.
test_window_match_steps_rust builds the unchanged
test_window_match_steps.c against libobs_rust-static, so the C test
runs the Rust shims of all eight window match functions, the codec
included, not only the C it compiles in. The functions are hidden in
libobs, so the executable takes them from the production staticlib.
obs-util's archive member also needs libuuid, GIO, glib and libobs's
hidden base_default_log_handler() and base_log_literal(): the test
links the first three and compiles its own util/base-variadic.c, as
test_effect_param_types_steps_rust does with its adapters.

Hybrid backend and Linux only. On macOS and Windows obs-util's member
pulls in other host code, and no PR lane builds this test there to
check that link; the Tier 3 parity tests compare the same shims with
the C on every host.

Checked: 20 of 20 pass, and a planted Rust mutant ('#' escaped as
"#23") fails encode, encode_truncates and round_trip.

Refs #3380.
@rdoupe
rdoupe force-pushed the fix/rust-port-the-window-setting-s-s-422 branch from b0f6462 to cfb8d69 Compare November 1, 2026 10:35
@rdoupe
rdoupe marked this pull request as ready for review November 1, 2026 11:01
@rdoupe
rdoupe requested a review from zackees as a code owner November 1, 2026 11:01
@zackees
zackees added this pull request to the merge queue Nov 1, 2026
Merged via the queue into master with commit b447b30 Nov 1, 2026
41 checks passed
@zackees
zackees deleted the fix/rust-port-the-window-setting-s-s-422 branch November 1, 2026 11:21
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Done in obs-rust Multiview Nov 1, 2026
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.

rust: Port the window setting's string codec

2 participants