Skip to content

feat(obs-util): port util/dstr.c to Rust (#6) - #51

Closed
b7216309-jpg wants to merge 6 commits into
obs-rust:masterfrom
b7216309-jpg:rust/dstr-port
Closed

b7216309-jpg wants to merge 6 commits into
obs-rust:masterfrom
b7216309-jpg:rust/dstr-port

Conversation

@b7216309-jpg

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

Copy link
Copy Markdown
Contributor

Refs #43 (Phase 3 tracking; #6 closed when #33 merged)

Stacked draft: depends on #41 (for the strref mirror) and #50 (the C-only dstr-libc.c extraction). Until those land, the diff also shows their commits. Only the last two commits are new here.

Ports the byte-string half of libobs/util/dstr.c to Rust: 26 EXPORT functions. These are astrcmpi, astrcmp_n, astrcmpi_n, astrstri, strdepad, strlist_split/strlist_free, and the dstr copy/ncopy/cat/ncat/insert/remove/replace/safe_printf/depad/left/mid/right family. The printf family, the wide-char compares and the conversions moved to dstr-libc.c in #50 and stay in C. Stable Rust can't define variadic functions, and the rest depend on the locale or libc.

Commits (new in this PR)

  1. RED: the shim, the struct dstr mirror, the oracle, the guard, rust-exports.txt and the tests, against a todo!() core. Tier 1 fails 0/24, Tier 3 aborts, and layout passes.
  2. GREEN: the safe core. Tier 1 passes 24/24, layout 1/1, and Tier 3 5/5.

Design

  • In-place editing without a copy per call. The dstr functions are generic over a DstrStore trait: the struct's state (array/len/capacity) plus the allocator calls C makes (brealloc, bmalloc for bmemdup, bfree). The shim implements it directly on the caller's struct dstr, so a loop of dstr_cat stays linear. VecDstr implements it for Rust callers and tests.
  • Capacity matches C exactly. The Rust core ports the dstr.h inline dstr_ensure_capacity growth rule (exact first allocation, then doubling). The inline helpers themselves stay in C under the header-inline exemption, and the layout test covers struct dstr.
  • Aliasing. Every source is copied before the destination changes. dstr_cat_dstr(d, d) is well-defined in C and gives the same result. Where C reads moved or freed memory (dstr_ncopy(d, d->array + 1, n) frees before copying, and dstr_insert_dstr(d, i, d) memcpys from a moved buffer), Rust reads the source as it was. This also keeps the shim free of &/&mut aliasing.

Intentional differences (C is undefined, crashes or hangs)

  • Aliased sources: a source inside the destination reads as it was before the call.
  • Missing bytes: reading past a source's end, or through a NULL source with a nonzero length, reads NUL (dstr_ncopy_dstr from an empty dstr, dstr_mid/dstr_left past the end).
  • Out of range: insert/insert_dstr/insert_ch with idx > len, remove with idx + count > len, and right with pos > len do nothing (C wraps size_t and moves memory).
  • Empty find: dstr_replace with a NULL find does nothing (C dereferences it); an empty find does nothing too (C loops forever, as test_dstr notes).
  • Extra write: dstr_insert_ch mid-string does not write C's extra byte past the buffer when capacity is exactly len + 2 (the out-of-bounds write test_dstr works around).
  • Split on NUL: strlist_split with split_ch == '\0' returns the whole string (C reads past the terminator).

Characterized and kept: dstr_remove with count == len frees whatever idx is; dstr_replace restarts the search after each replacement; dstr_ncat copies embedded NULs; compares use the platform's char signedness.

Tier 3: model-based parity

dstr_ops_match_c runs a random sequence of up to 24 operations (all 20 kinds, including self-sources, dstr_left in place, re-initialization, and NUL-bearing ncat bytes) on a Rust-shimmed dstr and an oracle dstr. After every step it checks that both agree on array being NULL, the C string, len and capacity. Arguments stay inside C's defined behavior, so the oracle is never asked to do something undefined. Separate properties cover the compares and astrstri, strdepad (in place, returning its argument) and strlist_split.

Mutation check. I ran 23 mutations. Every one that can change observable behavior is caught: by parity, or, for dstr_ncat's NUL-first check (which the shim already enforces before the core is reached), by Tier 1.

  • Caught (17):
    • growth ×3 instead of ×2
    • ncopy capacity +1
    • insert at len taking the middle path
    • insert_ch reserving len + 1
    • remove honoring idx on a full remove
    • replace not restarting after a shrink
    • replace growth not doubling
    • safe_printf skipping $4
    • depad keeping an empty string
    • right over-reserving
    • case-sensitive astrcmpi
    • astrcmp_n comparing n+1
    • split dropping empties
    • mid short by one
    • ncopy_dstr not freeing first
    • ncat_dstr not clamping
    • ncat dropping embedded NULs
  • Equivalent (4), no observable effect: copy freeing only a non-NULL array (the NULL-with-capacity state is unreachable); remove tail via memmove (it writes the same NUL); left ignoring same (the shim passes an empty source then); strdepad stopping at index 1 (buf[0] is never padding there).

The first generator version missed the ncat NUL-first case; the raw() generator was added for it.

Verification

On Windows 11 (MSVC):

  • soldr cargo test --workspace --locked passes (132 tests).
  • ./lint passes.
  • Clippy -D warnings is clean for x86_64/aarch64 Linux and aarch64 macOS.

Not verified locally

  • Tier 2 OFF/ON: test_dstr runs unchanged against the Rust symbols with ON, and every C user of dstr in libobs links against them.
  • The exported-symbol diffs and the local gate stamp.

There is no Docker or WSL on this machine.

🤖 Generated with Claude Code

b7216309-jpg and others added 6 commits October 9, 2026 13:26
Adds the C ABI shim for the ten EXPORT functions of
libobs/util/lexer.c, the #[repr(C)] mirrors of strref, base_token,
error_item, error_data and lexer, the oracle wrapper, the
ENABLE_RUST_LIBOBS guard, rust-exports.txt entries, and Tier 1, Tier 2
layout and Tier 3 tests. The safe core bodies are todo!(), so
soldr cargo test -p obs-util --test lexer fails 0/16 and
--test lexer_parity aborts on the first call; the layout test passes.

The lexer oracle needs dstr_catf and bmemdup. oracle/test_dstr.c is a
test-only dstr_catf (dstr_vcatf with plain brealloc growth), like
test_bmem.c for bmem, so the oracle does not pull in util/dstr.c and
its utf8.c dependency. bmemdup joins test_bmem.c.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of libobs/util/lexer.c. GREEN for Tier 1
(16/16), the layout tests (5/5) and Tier 3 (9/9).

Characters compare as the platform's C char (c_char), so bytes from
0x80 order as in C on both signed (x86) and unsigned (Arm Linux) char
targets. toupper is the C locale's, ASCII only.

Intentional difference: lexer_getstroffset leaves the outputs untouched
when lex->text is NULL, where C dereferences it.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	libobs/cmake/rust-exports.txt
#	rust/obs-c-oracle/build.rs
#	rust/obs-c-oracle/src/lib.rs
Behavior-neutral move, as the function-extraction policy requires.
These 15 functions move verbatim from util/dstr.c into the new
util/dstr-libc.c:

- the printf family (dstr_printf, dstr_catf, dstr_vprintf,
  dstr_vcatf), which takes C varargs and va_list;
- the wide-character compares (wstrcmpi, wstrcmp_n, wstrcmpi_n,
  wstrstri, wcsdepad), which depend on towupper and wchar_t width;
- the conversions (dstr_from_mbs, dstr_to_mbs, dstr_to_wcs,
  dstr_from_wcs, dstr_to_upper, dstr_to_lower), which go through
  platform.c and the locale.

wstrblank moves with them, and the static is_padding helper, which both
files use, is copied. No header, signature or behavior changes, and the
new file is always compiled.

What stays in dstr.c is pure byte-string logic that a Rust port can
replace under ENABLE_RUST_LIBOBS. Stable Rust cannot define variadic
functions, so the printf family has to stay in C.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds the C ABI shim for the 26 EXPORT functions left in
libobs/util/dstr.c after the dstr-libc.c split, the #[repr(C)] mirror
of struct dstr, the oracle wrapper, the ENABLE_RUST_LIBOBS guard,
rust-exports.txt entries, and Tier 1, layout and Tier 3 tests. The safe
core bodies are todo!(), so soldr cargo test -p obs-util --test dstr
fails 0/24 and --test dstr_parity aborts on the first call; the layout
test passes.

The core is generic over DstrStore, the dstr state plus the allocator
calls C makes on it; the shim implements it on the caller's struct dstr
with bmalloc/brealloc/bfree, so C and Rust keep sharing dstrs without a
copy per call. The test_dstr.c stand-in stays: dstr_catf now lives in
dstr-libc.c, which stays in C.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of the byte-string half of libobs/util/dstr.c.
GREEN for Tier 1 (24/24), the layout test (1/1) and Tier 3 (5/5).

Growth follows the dstr.h inline dstr_ensure_capacity, so capacity
matches C after every call. Sources are read before the destination
changes; where C reads past a source or through a NULL one, the port
reads NUL.

Intentional differences, where C is undefined, crashes or hangs: a
source inside the destination reads as it was before the call;
out-of-range insert/insert_dstr/insert_ch/remove/right arguments do
nothing; a NULL or empty dstr_replace find does nothing (C dereferences
NULL or loops forever); dstr_insert_ch does not write the extra byte C
writes past a full buffer; strlist_split on NUL returns the whole
string.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #68, which ports this (along with the rest of Phase 3) on top of the merged C fixes #53, #54 and #60. Thanks for picking up the decisions from here. The cf-tokenizer work (#42, #49) isn't covered by #68; I'll rebuild #49 on #68's lexer once it lands.

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.

1 participant