Skip to content

feat(obs-util): port util/text-lookup.c to Rust (#6) - #52

Closed
b7216309-jpg wants to merge 8 commits into
obs-rust:masterfrom
b7216309-jpg:rust/text-lookup-port
Closed

b7216309-jpg wants to merge 8 commits into
obs-rust:masterfrom
b7216309-jpg:rust/text-lookup-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: builds on #51 (dstr port), which builds on #41 and #50; the oracle runs on the lexer and dstr oracles from those PRs. Until they land, the diff shows their commits too. Only the last two commits are new here.

Ports libobs/util/text-lookup.c (the locale string tables behind obs_module_text and the frontend's Str()) to Rust: text_lookup_create, text_lookup_add, text_lookup_destroy, text_lookup_getstr.

Commits (new in this PR)

  1. RED: the shim, the oracle (with two test-only stand-ins), the guard, rust-exports.txt and the tests, against a todo!() core. Tier 1 fails 0/12 and Tier 3 aborts.
  2. GREEN: the safe core. Tier 1 passes 12/12 and Tier 3 2/2.

Design

  • Opaque handle. struct text_lookup is private to text-lookup.c, so lookup_t * is a boxed Rust TextLookup. There is no #[repr(C)] mirror and no layout test.
  • Stable value pointers. Values are CStrings in a HashMap. Their heap buffers don't move when the map grows, so a pointer returned by text_lookup_getstr stays valid until that key is replaced or the lookup is destroyed, the same lifetime as with the uthash table.
  • File reading follows os_fread_utf8: the UTF-8 BOM is skipped; an empty or BOM-only file makes create/add fail; the text ends at the first NUL. Every CR then becomes a space (dstr_replace(&file_str, "\r", " ")).
  • Parser. It is a port of lookup_gettoken/lookup_getstringtoken/lookup_addfiledata on the safe base lexer from feat(obs-util): port util/lexer.c to Rust (#6) #41, quirks included (characterized, not endorsed):
    • whitespace is significant, so Key = value stores " ";
    • an empty quoted value (Key="") ends parsing of the whole file;
    • an unterminated string ending in an escaped quote loses that quote;
    • only the first = is skipped (B==c stores =);
    • a later duplicate replaces the earlier one.
  • Allocator. Entries live on the Rust heap, so bnum_allocs no longer counts them (C never frees them; text_lookup_destroy does).

A crash in today's C code

lookup_getstringtoken computes the value length as 1 + (temp - offset) - 1 - 1 when the closing quote check sees the opening quote. That happens for a quote immediately followed by a newline or the end of the file, e.g. a locale line Key=" . The size_t length wraps to SIZE_MAX, and bstrdup_n then tries to copy that much. One malformed line in any locale .ini crashes OBS. The port treats that string as empty, which ends parsing like any other empty value. The parity generator never emits a quote right before LF, NUL or EOF so the oracle doesn't crash. It's listed as an intentional difference in rust/README.md. Happy to send a separate C fix for master if wanted.

Other intentional differences: a NULL lookup, lookup_val or out returns false, where C dereferences them.

Oracle stand-ins (test-only)

  • oracle/test_platform_io.c: os_fopen/os_fread_utf8, copied from platform.c (with ftell and the dstr oracle's astrcmp_n). This avoids linking all of platform.
  • oracle/include/uthash.h: the four uthash macros text-lookup.c uses (HASH_FIND_STR, HASH_REPLACE_STR, HASH_DELETE, HASH_ITER) as a linked list. The system uthash.h is an external dependency that the Rust CI jobs don't install. Lookups give the same results whatever the hash, and the include dir is scoped to the text-lookup oracle build only.

The policy doc says to delete both when the oracle can link platform.c and a real uthash.

Tier 3 and mutation check

Random locale-like files (key=value lines, quotes, escapes, comments, CR/LF, =/==, BOMs, arbitrary bytes) are written to temp files and loaded by both. create must agree on success, and getstr on every candidate key (each word of the files, with and without quotes, plus whitespace and punctuation keys). This is checked before and after text_lookup_add of a second random file, over 1,024 file pairs.

Thirteen mutations: twelve caught, one equivalent.

  • Caught (12):
    • BOM not stripped
    • CR kept
    • no cut at NUL
    • = skipped twice
    • \" not unescaped
    • comment ending at space
    • # without push-back
    • escaped quote not escaped
    • closing quote kept
    • first duplicate wins
    • no skip to next line
    • strings stopping at spaces
  • Equivalent (1): reordering the escape replacements. All four are a backslash plus a distinct character, so their matches cannot interact.

Verification

On Windows 11 (MSVC):

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

Not verified locally

  • Tier 2 OFF/ON: test_text_lookup runs unchanged on the Rust symbols with ON, and every module's locale loading goes through them.
  • The exported-symbol diffs and the local gate stamp.

There is no Docker or WSL here.

🤖 Generated with Claude Code

b7216309-jpg and others added 8 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]>
Adds the C ABI shim for text_lookup_create, text_lookup_add,
text_lookup_destroy and text_lookup_getstr, the oracle wrapper, the
ENABLE_RUST_LIBOBS guard, rust-exports.txt entries, and Tier 1 and
Tier 3 tests. The safe core bodies are todo!(), so
soldr cargo test -p obs-util --test text_lookup fails 0/12 and
--test text_lookup_parity aborts on the first call. struct text_lookup
is opaque to C, so there is no layout test: a lookup_t is a boxed Rust
TextLookup.

The oracle runs on the lexer and dstr oracles. Two test-only stand-ins
let it build everywhere cargo test runs: oracle/test_platform_io.c for
os_fopen/os_fread_utf8 (util/platform.c) and oracle/include/uthash.h for
the system uthash, which the Rust CI jobs do not install.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of the locale string tables. GREEN for Tier 1
(12/12) and Tier 3 (2/2).

TextLookup owns a HashMap of CStrings: their buffers never move while
an entry lives, so a value pointer handed to C stays valid until its key
is replaced or the lookup is destroyed, as with the C hash table. File
reading follows os_fread_utf8 (BOM skipped; an empty or BOM-only file
fails; the text ends at the first NUL), then every CR becomes a space
and the C parser's quirks are kept.

Intentional differences: a quote right before a newline or the end of
the text ends parsing, where C's string length wraps around to SIZE_MAX
and bstrdup_n crashes; NULL lookup, lookup_val or out return false
where C dereferences them.

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