Skip to content

util: Port the platform string conversions to obs-util - #1874

Merged
zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:rust/port-platform-conversions
Oct 23, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:rust/port-platform-conversions

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #262 (slice 1, conversions). Unit 1 of #1846.

What and why

#1610 moved os_utf8_to_wcs, os_wcs_to_utf8, os_mbs_to_wcs, os_wcs_to_mbs, their _ptr forms, os_utf8_to_mbs_ptr and os_mbs_to_utf8_ptr verbatim into libobs/util/platform-conversions.c and pinned them with the 18 cases of test_platform_conversions. This PR ports them to obs-util::platform_conversions, and hybrid libobs drops util/platform-conversions.c.

  • Safe core (rust/obs-util/src/platform_conversions.rs): the buffer handling of the C, step for step. A dst is a slice: None is the NULL-dst size query (which ignores dst_size), an empty one is left untouched, and otherwise the conversion gets dst_size - 1 elements and the NUL goes at its result. The UTF-8 conversions give 0 and an empty string for text that is invalid or does not fit; the mbs ones cut it to fit and treat (size_t)-1 as 0. The _ptr forms take their allocator as a closure (the shim's is bmalloc): one allocation of the size query plus one. os_utf8_to_mbs_ptr and os_mbs_to_utf8_ptr chain them as in C.
  • Same platform calls as C: utf8_to_wchar / wchar_to_utf8 (libobs's decoder off Windows, MultiByteToWideChar / WideCharToMultiByte on Windows) and the C library's mbstowcs / wcstombs in the current LC_CTYPE, through new platform::{utf8_to_wide, wide_to_utf8, mbs_to_wide, wide_to_mbs} on Unix and Windows.
  • Shim (src/ffi/platform_conversions.rs): the ten functions with their exported signatures; (str, len) and (dst, dst_size) become slices, NULL str returns 0 and sets *pstr to NULL in the _ptr forms. Exports in rust-exports.d/obs-util-platform_conversions.txt. util/platform-conversions.c moves into libobs.ported-platform; libobs.platform, now empty, is removed from source-sets.json. The C reference still compiles it.
  • platform_file: os_fread_mbs and os_quick_write_mbs_file now call the ported conversions instead of the private copies slice 3 kept in platform/{unix,windows}/file.rs, which are removed.
  • Oracle: the plain-named Windows forwarders in oracle/platform_host.c are removed, since the Rust shims own those names now. oracle/platform_windows.c calls the oracle_ conversions, so the oracle still runs the original C all the way down. So are the two Unix forwarders util: Port dstr-libc to obs-util #1857 added for os_utf8_to_wcs_ptr and os_mbs_to_utf8_ptr: the dstr-libc shims call those plain names, which now resolve to the obs-util shims in libobs and under cargo test alike. Their ci/test_oracle_standins.py entries are removed too.
  • Kept, characterized, not endorsed (test_platform_conversions is unchanged and passes): the mbs functions ignore len (so os_mbs_to_utf8_ptr does too); a dst too small for the UTF-8 conversions gives 0 and an empty string, where the mbs ones cut the text; the off-Windows size query accepts an encoded surrogate that the conversion then refuses; the decoder's overlong and above-U+10FFFF forms.
  • Intentional difference (in the parity test header, the core's module docs and the new rust/README.md row): C writes one element past the buffer; Rust doesn't. os_utf8_to_wcs in C passes out_len + 1 (dst_size) as the capacity to utf8_to_wchar, so text exactly dst_size wide characters long converts in full and dst[dst_size] = 0 is written one past the buffer. os_wcs_to_utf8 passes dst_size - 1 and does not have this. The Rust passes dst_size - 1 on both sides, so that text gives 0 and an empty string, as os_wcs_to_utf8 does for text exactly dst_size bytes long. The same capacity lets the C's failed conversion of longer text leave one more unit of partial text after the NUL, at dst[dst_size - 1]; both return 0 and an empty string there.
  • The wide string between the two steps of os_utf8_to_mbs_ptr / os_mbs_to_utf8_ptr is a Rust Vec, where the C bmallocs and frees it. bnum_allocs is the same after each call (checked in Tier 1 and Tier 3).

The exact-size case is not added to test_platform_conversions: the same binary runs against libobs-c-reference and the C backend, where the C writes past the buffer, so a Tier 2 case for the fixed behavior would fail there. It is pinned in Tier 1 and Tier 3.

RED → GREEN

  • RED commit: 8354e2a util: Test the platform string conversions against a Rust stub. Tier 1 (tests/platform/platform_conversions.rs) is a 1:1 port of the 18 cmocka cases, with the UTF-8 locale cases and the bnum_allocs checks in a child of the test binary, plus two new ones: the _ptr size-plus-one buffer, and utf8_to_wcs_exact_size_stays_in_dst. All 20 fail on todo!().
  • GREEN: 2cc8ac4 util: Port the platform string conversions to obs-util. Tier 1 20/20; soldr cargo test -p obs-util 791/791 at GREEN, 815/815 at the head (with Tier 3).
  • Tier 3: e5e6dc6 util: Compare the platform string conversions with the C (platform_conversions_parity.rs, 8 tests). The Rust shim and the C get the same input; results and every element of buffers two elements longer than dst_size are compared:
    • os_utf8_to_wcs over arbitrary bytes and pieces of valid and invalid UTF-8 (overlong, surrogate, above U+10FFFF, 5- and 6-byte, truncated, BOM, embedded NUL), every len, a NULL dst with a random dst_size, and every dst_size from 0 to two past the length the text needs;
    • os_wcs_to_utf8 over wide units of every range (surrogates and negative wchar_t included), the same way;
    • os_mbs_to_wcs / os_wcs_to_mbs in the C locale, and in a UTF-8 locale in a child process;
    • the _ptr forms, and their bnum_allocs on both allocators in a child.
    • The intentional difference is excluded exactly: for every dst_size of 2 or more, the C must match the original utf8_to_wchar into dst_size units plus the NUL, the Rust the same into dst_size - 1, and the two must agree wherever those agree. For the exact-size inputs the oracle gets a padded buffer, and the test checks that it wrote the NUL at dst[dst_size] and that the Rust wrote nothing past dst_size. Invalid UTF-8 stops libobs's decoder before the capacity matters, but on Windows MultiByteToWideChar turns it into U+FFFD, so there it can reach the capacity like valid text; the fixed case pins both.
  • Mutation check, each mutant reverted (not committed). Each one fails these tests:
    • os_wcs_to_utf8 capacity dst_size - 2: 4 parity tests;
    • os_utf8_to_wcs skips dst_size 2: 5;
    • os_mbs_to_wcs keeps (size_t)-1 as dst_size - 1: 2;
    • os_wcs_to_mbs size query gives 1 for (size_t)-1: 4;
    • os_mbs_to_utf8_ptr drops the last unit: 3;
    • os_utf8_to_wcs_ptr allocates without room for the NUL: 3;
    • os_wcs_to_utf8 leaves dst alone for 8 units or more: 4.
  • Docs and port_comparison: d410a79.
  • Local, two build trees (OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON), headless (private Xvfb, private D-Bus, fresh HOME):
Library test_platform_conversions Full ctest Exported symbols
C backend libobs 18/18 379/379 IDENTICAL (1766)
hybrid libobs 18/18 381/381 IDENTICAL (1766)
libobs-c-reference 18/18 381/381 IDENTICAL (1766)

After the rebase onto #1857: test_platform_conversions, test_dstr, test_dstr_libc, test_dstr_regressions, test_dstr_zero_len, test_platform and test_platform_file pass on all three libraries, exports still IDENTICAL (1766), and soldr cargo test -p obs-util is 815/815, dstr-libc parity included. The full ctest numbers above are from before that rebase.
No skips; a UTF-8 locale was installed, so the UTF-8 cases ran. In hybrid libobs, os_utf8_to_wcs and the others are the Rust ones; the production link has no libobs.platform or libobs.ported-platform object.

  • ci/local_gate.py --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, build groups, test-binary cap, ifdef ratchet): passed. --lane cross (Windows and macOS clippy/Dylints, and a link of every test binary for x86_64-pc-windows-msvc and aarch64-apple-darwin): passed. ci/test_build_groups.py, ci/test_ported_sources.py, ci/test_oracle_standins.py: passed.
  • port_comparison (record.py, 5 passes, --cpu 7, Linux x86_64, GCC 16.2, rustc 1.98.1; the machine was under load, so treat these as rough):
Benchmark Rust C Rust / C (run range)
conversions/utf8_to_wcs/64 355.0 ns 126.1 ns 2.816 (2.106-3.111)
conversions/utf8_to_wcs/4096 20952.0 ns 7659.5 ns 2.735 (2.500-2.783)
conversions/wcs_to_utf8/64 216.8 ns 140.3 ns 1.545 (1.315-1.634)
conversions/wcs_to_utf8/4096 6911.9 ns 3657.7 ns 1.890 (1.060-1.902)
conversions/utf8_to_wcs_ptr/64 271.9 ns 141.6 ns 1.920 (1.854-3.494)
conversions/utf8_to_mbs_ptr/ascii 335.4 ns 187.9 ns 1.785 (1.513-3.065)

The gap is in the obs-util utf8_to_wchar decoder that this port calls, not in the wrapper: utf8_to_wchar alone on the 4096-byte text takes 11.7 µs against the C's 3.9 µs. Speeding up that decoder is left for a follow-up.

  • Real app: not applicable (no user-visible change).

Windows/macOS: the platform/windows/file.rs primitives and the oracle change are compiled and linked by the cross lane but not run locally. PR CI's Windows and macOS lanes run them first.

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 flag changes)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

rust/obs-util/src/platform_conversions.rs holds the safe core for
libobs/util/platform-conversions.c with todo!() bodies: os_utf8_to_wcs,
os_wcs_to_utf8, os_mbs_to_wcs, os_wcs_to_mbs, their _ptr forms,
os_utf8_to_mbs_ptr and os_mbs_to_utf8_ptr. ffi/platform_conversions.rs
is the C ABI shim with the same exported signatures: it turns
(str, len) and (dst, dst_size) into slices and gives the _ptr forms a
bmalloc allocator. The conversions stay the platform's, as in C:
utf8_to_wchar / wchar_to_utf8 and the C library's mbstowcs / wcstombs,
through new platform::{utf8_to_wide, wide_to_utf8, mbs_to_wide,
wide_to_mbs} on Unix and Windows.

- Tier 1 (tests/platform/platform_conversions.rs): 1:1 port of the 18
  cases of test_platform_conversions.c. The UTF-8 locale cases and the
  bnum_allocs checks run in a child of the test binary. New cases: the
  _ptr forms' size-plus-one buffer, and text exactly dst_size wide
  characters long, which must give 0 and an empty string without a
  write past dst_size.
- ENABLE_RUST_LIBOBS=ON drops util/platform-conversions.c: it moves
  into libobs.ported-platform, and libobs.platform, now empty, is
  removed. Exports in rust-exports.d/obs-util-platform_conversions.txt.
- Oracle: the plain-named Windows forwarders in platform_host.c go,
  since the Rust shims now own those names, and oracle/platform_windows.c
  calls the oracle_ conversions instead.

All 20 Tier 1 cases fail on todo!(); the ones that reach the shim abort
the child when the panic crosses the extern "C" boundary.

Refs obs-rust#262
The safe core follows platform-conversions.c: a NULL dst is a size
query that ignores dst_size; an empty dst is left alone; otherwise the
conversion gets dst_size - 1 elements and the NUL goes at its result.
The UTF-8 conversions give 0 and an empty string for text that is
invalid or does not fit, the mbs ones cut it to fit and treat
(size_t)-1 as 0. The _ptr forms allocate the size query plus one, and
os_utf8_to_mbs_ptr / os_mbs_to_utf8_ptr chain them as in C, with the
wide string in between in a Vec rather than bmalloc'd.

Intentional difference: C's os_utf8_to_wcs gives utf8_to_wchar a
capacity of dst_size, not dst_size - 1, so text exactly dst_size wide
characters long converts in full and its NUL lands at dst[dst_size].
C writes one element past the buffer; Rust doesn't. The capacity here is
dst_size - 1, as os_wcs_to_utf8 has it, so that text gives 0 and an
empty string like any other text that does not fit.

platform_file's os_fread_mbs and os_quick_write_mbs_file now call the
ported os_mbs_to_utf8_ptr / os_utf8_to_mbs_ptr instead of the private
copies slice 3 kept in platform/{unix,windows}/file.rs, which go.

Tier 1 tests::platform::platform_conversions: 20/20. obs-util lib
harness: 791/791.

Refs obs-rust#262
oracle/platform.c already compiles platform-conversions.c as oracle_*;
src/platform_conversions.rs declares those functions for Rust.

platform_conversions_parity.rs calls the Rust shim and the C with the
same input and compares the results and every element of buffers that
are two elements longer than the dst_size both are given:
- os_utf8_to_wcs over arbitrary bytes and pieces of valid and invalid
  UTF-8 (overlong, surrogate, above U+10FFFF, 5- and 6-byte, truncated,
  BOM, embedded NUL), every len, a NULL dst with a random dst_size,
  and every dst_size from 0 to two past the length the text needs;
- os_wcs_to_utf8 over wide units of every range, surrogates and
  negative wchar_t included, the same way;
- os_mbs_to_wcs / os_wcs_to_mbs in the C locale, and in a UTF-8
  locale in a child of the test binary;
- the _ptr forms (results and strings), and their bnum_allocs on both
  allocators in a child.

The intentional difference is excluded exactly: for every dst_size of
2 or more, the C must match utf8_to_wchar (the original) into dst_size
units plus the NUL, the Rust the same into dst_size - 1, and the two
must agree wherever those agree. They differ for text exactly dst_size
long (the C returns dst_size and writes dst[dst_size]) and for longer
text whose partial conversion reaches dst[dst_size - 1] after the NUL
in C. The Rust writes nothing past dst_size for any input.

8/8 parity tests. Mutation check, each mutant reverted:
- os_wcs_to_utf8 capacity dst_size - 2: 4 tests fail;
- os_utf8_to_wcs skips dst_size 2: 5;
- os_mbs_to_wcs keeps (size_t)-1 as dst_size - 1: 2;
- os_wcs_to_mbs size query gives 1 for (size_t)-1: 4;
- os_mbs_to_utf8_ptr drops the last unit: 3;
- os_utf8_to_wcs_ptr allocates the size query without the NUL: 3;
- os_wcs_to_utf8 leaves dst alone for 8 units or more: 4.

Refs obs-rust#262
rust/README.md gets the platform-conversions.c row: the characterized
behavior kept, and the intentional difference. C writes one element
past the buffer; Rust doesn't. C's os_utf8_to_wcs gives utf8_to_wchar
a capacity of dst_size rather than dst_size - 1, so text exactly
dst_size wide characters long converts in full and its NUL is written
at dst[dst_size]; Rust gives dst_size - 1, as os_wcs_to_utf8 does, so
that text gives 0 and an empty string. The core's module docs say the
same.

port_comparison gets a conversions category (record.py knows it):
os_utf8_to_wcs and os_wcs_to_utf8 into a buffer at 64 and 4096 bytes
of mixed UTF-8, os_utf8_to_wcs_ptr at 64 (allocation and free
included), and os_utf8_to_mbs_ptr on an ASCII file name in the C
locale. record.py, 5 passes, --cpu 7, on a loaded machine (load
average about 24):

  utf8_to_wcs/64          355.0 ns   126.1 ns  2.82
  utf8_to_wcs/4096      20952.0 ns  7659.5 ns  2.74
  wcs_to_utf8/64          216.8 ns   140.3 ns  1.55
  wcs_to_utf8/4096       6911.9 ns  3657.7 ns  1.89
  utf8_to_wcs_ptr/64      271.9 ns   141.6 ns  1.92
  utf8_to_mbs_ptr/ascii   335.4 ns   187.9 ns  1.79

The gap is the obs-util utf8_to_wchar decoder this port calls, not the
wrapper: utf8_to_wchar alone on the 4096-byte text is 11.7 us against
the C's 3.9 us.

Refs obs-rust#262
@rdoupe
rdoupe force-pushed the rust/port-platform-conversions branch from e87958f to d410a79 Compare October 23, 2026 03:35
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
Merged via the queue into obs-rust:master with commit 58d1fc0 Oct 23, 2026
33 checks passed
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.

2 participants