Skip to content

obs-util: Port util/dstr-libc.c to Rust - #1859

Closed
rdoupe wants to merge 5 commits into
obs-rust:masterfrom
rdoupe:port/dstr-libc
Closed

rdoupe wants to merge 5 commits into
obs-rust:masterfrom
rdoupe:port/dstr-libc

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1858

What and why

Unit 2 of #1846. This ports libobs/util/dstr-libc.c.h, the last part of dstr that was still C (#43 ported dstr.c), to rust/obs-util. The hybrid libobs drops the C file and links the Rust shims in its place. The C reference keeps compiling it. #1846 asks for one PR per unit, so this PR has the characterization, RED, GREEN, parity and docs as separate commits.

  • Safe core (src/dstr_libc.rs): compare_by() (the loop of wstrcmpi, wstrcmp_n and wstrcmpi_n), wstrstri(), wcsdepad() and map_case(), the dstr_to_upper / dstr_to_lower loop.
    • It is generic over the wchar_t unit: i32 on Linux and macOS, u16 on Windows. Units compare by that type, as in C.
    • The bounded compares read no further than C reads.
    • wcsdepad keeps the C's one memmove of the string and its NUL, and its unit-by-unit NULs over the trailing padding.
  • Shims (src/ffi/dstr_libc.rs): the same 11 EXPORTed symbols and C signatures: wstrcmpi, wstrcmp_n, wstrcmpi_n, wstrstri, wcsdepad, dstr_from_mbs, dstr_to_mbs, dstr_to_wcs, dstr_from_wcs, dstr_to_upper and dstr_to_lower.
    • The case fold and the case map are the C library's towupper and towlower, passed into the core. Results follow the C library and the current LC_CTYPE locale exactly as in C, one unit to one unit (U+00DF stays U+00DF, never "SS").
    • The conversions call what C calls, in the C's order: os_mbs_to_utf8_ptr and os_utf8_to_wcs_ptr (still C, unit 1 of meta: 10 libobs units to move from C to Rust next #1846) and wchar_to_utf8 (the Rust utf8 port). So array, len and capacity match, including capacity 0 after dstr_from_mbs.
  • The printf family stays C. Stable Rust cannot define a C variadic function or take a va_list. dstr_printf, dstr_catf, dstr_vprintf and dstr_vcatf are copied unchanged into util/dstr-variadic.c.h, which joins the hybrid-only group libobs.rust-variadic. pipe-variadic.c.h and base-variadic.c.h do the same. Tier 3 checks the copy against the original.
  • Build:
    • dstr-libc.c.h moves into libobs.ported-util (C reference only), next to dstr.c.h. The libobs.util-libc group held only this file and is removed.
    • Windows exports go through rust-exports.d/obs-util-dstr_libc.txt.
    • ci/test_ported_sources.py checks that the C object is excluded on Linux, Windows and macOS.
    • Public headers are unchanged. The exported-symbol count is unchanged at 1766, identical across OBS_LIBOBS_BACKEND=C, libobs-c-reference and the hybrid.
  • Tests:
    • Characterization: 15 new cases in the existing test_dstr, with no new executable.
    • Tier 1: tests/strings/dstr_libc.rs.
    • Tier 3: tests/strings/dstr_libc_parity.rs. Both are modules of the existing obs-util harness.
    • Under cargo test, oracle/platform_host.c now forwards os_mbs_to_utf8_ptr and os_utf8_to_wcs_ptr by name on Unix too, as it already did on Windows. Unit 1 will replace those forwarders when it ports them.

Behaviour of the C that is kept, characterized and not endorsed:

  • dstr_to_upper and dstr_to_lower free a string that is not valid UTF-8, and truncate at an embedded NUL.
  • dstr_to_mbs converts multibyte to UTF-8.
  • dstr_from_mbs leaves capacity 0.
  • A printf result whose first byte is NUL frees the dstr. The glibc vsnprintf failure path sizes the buffer to 4096.

No intentional differences.

Tier 3 runs on the same random inputs and compares return values, returned strings, and the array, bytes, len and capacity of every dstr:

  • wide strings of any unit value (negative units, surrogates, past U+10FFFF, embedded NULs) with n of 0, 1, random and SIZE_MAX;
  • wcsdepad over the whole buffer;
  • the conversions and case maps on arbitrary bytes (invalid UTF-8, embedded NULs, 4095-4097 bytes) from every starting dstr shape, and chains of them;
  • all of it again in a UTF-8 LC_CTYPE, in a re-exec'd child;
  • the printf copy against the original, including the glibc failure path.

Mutation check (not committed): 13 mutations, each caught by Tier 3:

  • 6 in the core: a compare one unit short, '\r' not padding, wstrstri starting at 1 or skipping the terminator, map_case skipping a unit, wcsdepad keeping a pad;
  • 4 in the shims: towlower for towupper, no free in dstr_from_mbs, dstr_from_wcs resizing to len + 2, dstr_to_wcs passing length 0;
  • 3 in the C copy: either error path assuming 4094 bytes, dstr_vcatf keeping an empty result.

port_comparison, new pairs (record.py, 5 paired passes, x86_64 Linux, GCC, --cpu 7, alignment not pinned), Rust / C median (pass range):

Pair Rust C Rust / C (pass range)
dstr/wstrcmpi/128 1198.3 ns 1254.2 ns 0.955 (0.881-1.086)
dstr/wstrcmpi/4096 30641.0 ns 35329.5 ns 0.867 (0.607-1.060)
dstr/wstrstri/128 1282.1 ns 1720.3 ns 0.745 (0.703-1.705)
dstr/wstrstri/4096 37462.9 ns 48118.2 ns 0.779 (0.734-0.786)
dstr/to_upper/128 2534.5 ns 1611.5 ns 1.573 (1.222-1.761)
dstr/to_upper/4096 75314.6 ns 51396.8 ns 1.465 (1.058-2.204)
dstr/from_wcs/128 872.8 ns 496.8 ns 1.757 (0.944-2.051)
dstr/from_wcs/4096 28530.9 ns 14932.9 ns 1.911 (1.439-2.105)

The host was under load from another build (load average about 24) during the run, so the ranges are wide; the printf_control control pair (C on both sides) ranged 0.52-2.22. Rerun on a quiet machine before quoting the ratios.

The compare and search are at or below C. to_upper and from_wcs are over the 1.10 bound, and the cost is not in this unit: both rebuild the dstr through wchar_to_utf8, which is the existing Rust utf8 port on the Rust side and C on the oracle side. Measured alone (a throwaway pair, not committed), Rust wchar_to_utf8 is about 3x the C at 128 characters (1.02 µs vs 0.32 µs) and about 1.9x at 4096 (44.5 µs vs 23.8 µs). That is a follow-up for the utf8 port, not part of this PR.

RED → GREEN

  • RED commit: e735faf. soldr cargo test -p obs-util --lib tests::strings::dstr_libc fails 13 of 13 with "not yet implemented". The characterization commit before it (0e74c36) passes test_dstr 35/35 on OBS_LIBOBS_BACKEND=C, libobs-c-reference and the hybrid.
  • GREEN: 244d1cb (port), 7b163ba (Tier 3), 3278bc6 (docs and benches), on 6f34983.
    • test_dstr 35/35 against OBS_LIBOBS_BACKEND=C, libobs-c-reference and the hybrid.
    • Full ctest on the hybrid tree (on the previous base, a901385; after the rebase the affected tests, exports and obs-util were rerun): 381/381 with libobs-c-reference (OFF) and 381/381 with the hybrid (ON). The only skips are open_crashes_without_obs_core (expected) and test_hex_float_values_ignore_comma_locale (no comma-decimal locale on the host).
    • soldr cargo test -p obs-util --lib: 791 passed.
    • Exported symbols: 1766, identical across the three libraries.
    • local_gate.py --lane lint and --lane cross pass. ci/test_build_groups.py, ci/test_ported_sources.py and ci/test_oracle_standins.py pass.
  • Real app (UI, protocol output or logging changes): not applicable, no user-visible change.

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)

Adds characterization cases for libobs/util/dstr-libc.c.h to the
existing test_dstr executable, before its port. They pin what the C
does today and pass against the C reference and the hybrid libobs,
where the file is still C.

- wstrcmpi, wstrcmp_n, wstrcmpi_n: NULL is the empty string, n == 0
  compares nothing, case folding compares both sides upper-cased.
- wstrstri: the first case-insensitive match, an empty find matches at
  the start, NULL on either side is NULL.
- wcsdepad: only ' ', '\t', '\n' and '\r' are padding; the leading
  padding is moved out with the NUL, the trailing padding is overwritten
  with NULs one by one and the old tail past them stays.
- dstr_printf, dstr_catf, dstr_vprintf, dstr_vcatf: formats, the exact
  capacity on a fresh dstr and the dstr_ensure_capacity growth, an empty
  result frees, a first byte of NUL frees, an embedded NUL later is
  counted, 4095, 4096 and 100000-byte results. On glibc, the
  vsnprintf failure path (%ls of a non-ASCII character in the "C"
  locale): 4096 bytes and the strlen length.
- dstr_from_mbs, dstr_to_mbs, dstr_to_wcs, dstr_from_wcs: NULL and
  empty, capacity 0 after dstr_from_mbs, dstr_to_mbs is a multibyte to
  UTF-8 conversion that stops at a NUL, dstr_to_wcs converts len bytes
  (an embedded NUL too), invalid UTF-8 gives L"", a surrogate frees.
- dstr_to_upper and dstr_to_lower: an embedded NUL truncates, invalid
  UTF-8 frees the string, 10000 bytes, and in a UTF-8 LC_CTYPE (skipped
  where none exists) the towupper and towlower mapping of the locale,
  one code point at a time (U+00DF stays). On glibc, the "C" locale
  leaves U+00E9 alone and mbstowcs of a high byte gives "".

Odd results are marked "characterized, not endorsed". Cases that need a
UTF-8 wchar_t or glibc are guarded with #ifndef _WIN32 or #ifdef
__GLIBC__, as test_dstr_case already is.

Refs obs-rust#1858
RED for the port of libobs/util/dstr-libc.c.h to rust/obs-util. The
safe core (src/dstr_libc.rs) is a stub (todo!()):

- compare_by(), the compare loop of wstrcmpi, wstrcmp_n and wstrcmpi_n:
  each step compares the folded units, stops when either raw unit is
  NUL, or after n units;
- wstrstri(), the first position (up to and including the terminator)
  where find matches with the fold;
- wcsdepad(), on the string and its NUL;
- map_case(), the dstr_to_upper / dstr_to_lower loop up to the first
  NUL.

They are generic over the wchar_t unit (i32 on Linux and macOS, u16 on
Windows). The fold and the case map are parameters: C calls towupper
and towlower, whose results depend on the C library and the locale, and
the shims will pass those functions.

tests/strings/dstr_libc.rs ports the safe-core assertions of the new
test_dstr cases 1:1, under the same names: test_wstrcmpi,
test_wstrcmp_n, test_wstrcmpi_n, test_wstrstri, test_wcsdepad, and the
case loop of test_dstr_case, test_dstr_case_edges and test_dstr_locale,
with an ASCII fold for the "C" locale and a Latin-1 one for a UTF-8
locale. Edge cases: UTF-16 units, signed units (a negative unit sorts
below NUL), the reads of the compare stop where C stops, a fold that
maps to NUL matches at the terminator, padding in the middle stays.

The tests are a module of the existing obs-util library harness; no new
test binary.

soldr cargo test -p obs-util --lib tests::strings::dstr_libc fails 13 of
13 with "not yet implemented".

Refs obs-rust#1858
Ports libobs/util/dstr-libc.c.h to rust/obs-util, the last part of
dstr.c that was still C (obs-rust#43 ported the rest). The hybrid libobs drops
the C file and links the Rust shims in its place; the C reference keeps
compiling it.

Safe core (src/dstr_libc.rs, generic over the wchar_t unit, i32 on
Linux and macOS, u16 on Windows): compare_by(), the loop of wstrcmpi,
wstrcmp_n and wstrcmpi_n; wstrstri(); wcsdepad(), with the C's one
memmove of the string and its NUL and its NULs over the trailing
padding; and map_case(), the dstr_to_upper / dstr_to_lower loop.

Shims (src/ffi/dstr_libc.rs), the same 11 exported symbols and C
signatures: wstrcmpi, wstrcmp_n, wstrcmpi_n, wstrstri, wcsdepad,
dstr_from_mbs, dstr_to_mbs, dstr_to_wcs, dstr_from_wcs, dstr_to_upper,
dstr_to_lower. The case fold and the case map are the C library's
towupper and towlower, so the results follow the C library and the
current LC_CTYPE locale as C does (one unit to one unit, no "SS" for
U+00DF). The conversions call what C calls in the C's order:
os_mbs_to_utf8_ptr and os_utf8_to_wcs_ptr (util/platform.c, still C)
and wchar_to_utf8 (the Rust utf8 port). dstr_from_mbs still leaves
capacity 0, and dstr_from_wcs resizes as dstr_resize does, so array,
len and capacity match C. A NULL wide string reads as the empty one,
and the bounded compares read no further than C.

dstr_printf, dstr_catf, dstr_vprintf and dstr_vcatf stay C: stable Rust
cannot define a C variadic function or take a va_list. They are copied
unchanged into util/dstr-variadic.c.h, which joins the hybrid-only
group libobs.rust-variadic, as pipe-variadic.c.h does for
os_process_args_add_argf.

Build: dstr-libc.c.h moves into libobs.ported-util (LIBOBS_PORTED_SOURCES,
C reference only) next to dstr.c.h, and the libobs.util-libc group,
which held only it, is removed. The shims are exported on Windows
through rust-exports.d/obs-util-dstr_libc.txt. ffi/dstr.rs shares its
ensure_capacity and free with the new module. ci/test_ported_sources.py
checks that the C object is excluded from the production build on
Linux, Windows and macOS.

soldr cargo test -p obs-util --lib: 784 passed, 13 of them the Tier 1
cases of the RED commit.

Refs obs-rust#1858
Tier 3 for the dstr-libc port: tests/strings/dstr_libc_parity.rs runs
the Rust shims and the original dstr-libc.c (oracle/dstr_libc.c, the
oracle_* names) on the same random inputs and compares return values,
returned strings, and the array, bytes through the terminator, len and
capacity of every dstr.

- wstrcmpi, wstrcmp_n and wstrcmpi_n on wide strings of any unit value
  (negative ones where wchar_t is signed, surrogates, values past
  U+10FFFF, embedded NULs), with NULL, prefixes, and n of 0, 1, random
  and SIZE_MAX; wstrstri, also with a find taken from the string; and
  wcsdepad, over the whole buffer including the units past the new NUL.
- The conversions and the case maps from every starting shape
  (unallocated, spare capacity, the capacity-0 buffer dstr_from_mbs
  leaves) on arbitrary bytes, including invalid UTF-8, embedded NULs
  and 4095 to 4097 bytes, and chains of them on one dstr.
- utf8_locale_matches_c repeats all of it with LC_CTYPE set to a UTF-8
  locale, where towupper, towlower and mbstowcs map non-ASCII text. It
  re-execs the harness for itself (setlocale is process-wide).
- printf_copy_matches_original checks that util/dstr-variadic.c, the
  copy of the printf family the hybrid compiles (oracle/dstr_variadic.c,
  variadic_* names), leaves the original's dstr: formats, empty and
  NUL-first results, growth, 5000-byte output, and on glibc the
  vsnprintf failure path of both functions.

No intentional differences and no excluded inputs.

The Rust shims call os_mbs_to_utf8_ptr and os_utf8_to_wcs_ptr by their
libobs names, so oracle/platform_host.c now forwards those two on Unix
too, as it already did on Windows. ci/test_oracle_standins.py lists them
as forwarders on every host.

Mutation check (not committed; each alone fails the file): compare_by
stopping one unit early, no '\r' padding, wstrstri starting at 1 or not
trying the terminator, map_case skipping a unit, wcsdepad keeping the
last pad, towlower for towupper, dstr_from_mbs without its free,
dstr_from_wcs resizing to len + 2, dstr_to_wcs passing length 0, and in
the C copy, either error path assuming 4094 bytes or dstr_vcatf keeping
an empty result.

soldr cargo test -p obs-util --lib: 791 passed.

Refs obs-rust#1858
rust/README.md gets the dstr-libc.c.h row: the 11 swapped symbols and
their export fragment, the printf family that stays C in
util/dstr-variadic.c, the C library's towupper and towlower as the case
fold and map, the conversions called in the C's order, the C behaviour
that is kept, and no intentional differences. The dstr.c row points to
it.

port_comparison pairs each new shim with the C oracle on ASCII text with
a U+00E9 every 8 characters, at 128 and 4096: dstr/to_upper and
dstr/from_wcs (a dstr rebuilt from a fresh copy each time), and
dstr/wstrcmpi (equal strings in different case) and dstr/wstrstri (the
only match at the end). Each pair checks the results match before it
measures. release-criteria.md lists them.

Fixes obs-rust#1858
@zackees

zackees commented Oct 23, 2026

Copy link
Copy Markdown
Member

Coordinator note: two claims cover the same unit. #1855 (@zackees, claimed 2026-10-23 02:08 UTC) and #1858 / PR #1859 (@rdoupe, claimed 02:34 UTC) are both "unit 2 of #1846", the util/dstr-libc.c port. Under coordination.md the earlier claim (#1855) has priority. #1859 already has a full port up, though. Please agree between you which one continues, and close the other issue as a duplicate, so no CI or review time goes to two ports.

@rdoupe

rdoupe commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator Author

My mistake: I checked for claims before starting but not again before filing, and #1855 came first. Closing this in favour of #1855 / #1857. If anything here is useful to #1857, feel free to take it: the Tier 3 parity also runs the conversions and case maps under a UTF-8 LC_CTYPE in a re-exec'd child; wcsdepad is compared over the whole buffer including what lies past the new terminator; and the oracle's platform_host.c now forwards os_mbs_to_utf8_ptr/os_utf8_to_wcs_ptr on Unix too.

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 util/dstr-libc.c to obs-util

2 participants