Skip to content

feat(obs-util): port util/cf-tokenizer.c to Rust (#6) - #49

Merged
zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:rust/cf-tokenizer-port
Oct 10, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:rust/cf-tokenizer-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)

Status (2026-10-10): rebased by @zackees onto master as a linear history (thanks!). The cf-tokenizer.c extraction merged via #42, and the base and vec2 conflicts are resolved. The port now matches master's C fixes #54 (cf_literal_to_str stops at the closing quote) and #60 (an empty second strref compares as 1). Ready for review.

Overlap with #68: this PR also carries the lexer.c port (from the closed #41), which #68 ports too. Whichever lands second should rebase onto the other's lexer core.

Ports libobs/util/cf-tokenizer.c (the C-family lexer extracted from cf-lexer.c in #42) to Rust: cf_literal_to_str, cf_lexer_init, cf_lexer_free, cf_lexer_lex. The preprocessor in cf-lexer.c stays in C and keeps consuming the same token arrays.

Commits

  1. lexer.c RED, then GREEN (from feat(obs-util): port util/lexer.c to Rust (#6) #41).
  2. RED: the shim, the cf_token/cf_lexer mirrors, the oracle, the guard, rust-exports.txt and the tests, against a todo!() core. Tier 1 fails 0/14, Tier 3 aborts on the first call, and the layout tests pass.
  3. GREEN: the safe core. Tier 1 passes 14/14, layout 2/2, and Tier 3 5/5.
  4. Fix (@zackees): adopts libobs/util: Drop the closing quote in cf_literal_to_str #54 and libobs/util: Order empty strrefs consistently in strref compares #60 in the Rust cores and their Tier 1 expectations.

Design

  • Safe core. cf_lex(text) -> CfLexed { reformatted, tokens, offset, unexpected_eof } returns tokens as offsets (str into reformatted, unmerged into the input). It reuses the safe lexer::get_base_token and strref_cmp from feat(obs-util): port util/lexer.c to Rust (#6) #41.
  • Shim. It allocates exactly what C does through bmalloc: file, the base lexer's bstrdup text, reformatted (strlen + 1), and the token darray, one push at a time so capacity growth matches. It then turns the offsets into the pointers that cf_preprocess and the shader/effect parsers walk.
  • Oracle independence. oracle/cf_tokenizer.c also renames the lexer.c functions it calls, so it runs on the C lexer oracle, not on the Rust lexer port.

Quirks kept (characterized, not endorsed)

  • A string token's unmerged_str is one byte too long: the opening quote is counted twice.
  • \x takes as many hex digits as strtoul accepts, then skips exactly 2. Octal escapes start parsing at their second digit and skip 3. Unknown escapes write nothing.
  • strtoul semantics are emulated: leading whitespace, sign, 0x prefix, and overflow → ULONG_MAX → 0xff at the platform's unsigned long width. That is 32-bit on Windows and 64-bit elsewhere, so \x123456789 gives 0xff on Windows and 0x89 on Linux, as in C. The core takes the width as a parameter, and both widths are unit-tested.

Intentional differences (C over-reads)

  • Trailing splice. Text ending in a line splice (\ + newline) inside a comment or string makes C read one byte past the NUL of the bstrdup'd text, a heap over-read in today's code. The port reads NUL there. Parity inputs never end in a splice.
  • Escapes past the NUL. In cf_literal_to_str, \x/octal escapes can skip past the literal's NUL and C keeps reading. The port reads NUL. Parity pads literals with NULs so the oracle is deterministic.
  • NULL literal. cf_literal_to_str(NULL, …) returns NULL; C dereferences it.

These are listed in rust/README.md and in the parity file header.

Tier 3 and mutation check

Parity compares, field by field:

  • the return value, file, unexpected_eof, and the base lexer offset;
  • reformatted up to write_offset;
  • every token: type, lex back-pointer, and str/unmerged_str as offsets;
  • darray num and capacity.

It does this for fresh lexes and for re-lexing a used lexer, on generated C-like sources (splices, comments, strings, #include <…>, numbers like .5/1.5e3, underscores, CR/LF pairs, high bytes) plus arbitrary bytes. cf_literal_to_str is compared over the whole bzalloc buffer for literals full of escapes, with any count. Seventeen mutations, all caught, were run before the rebase. The 16 still meaningful are listed below; the 17th "fixed" the closing-quote leak, which #54 has since made the real C behavior:

Mutation Caught
no _ → alpha reclassification ✅
string unmerged_str off-by-one "fixed" ✅
escapes always allowed (even after #include) ✅
<…> never a string ✅
#include not required at line start ✅
unexpected_eof never cleared ✅
*/ star tracking broken ✅
line comments ignore splices ✅
.5 not a number ✅
digits break on . ✅
tabs not merged with spaces ✅
\x skips 3 ✅
octal parsed from the first digit ✅
strtoul overflow wraps instead of saturating ✅
strtoul skips no whitespace ✅
CRLF splices skip one byte ✅

Verification

On Windows 11 (MSVC):

  • soldr cargo test --workspace --locked passes (107 tests). Tier 3 runs the shim on this platform.
  • ./lint passes.
  • Clippy -D warnings is clean for x86_64/aarch64 Linux and aarch64 macOS.

Since the rebase, @zackees ran the local gate on Linux (lint, rust-test, Tier 2 OFF/ON, exports identical) and stamped it.

Not verified locally (by me)

  • Tier 2 OFF/ON: test_cf_lexer runs unchanged on the Rust tokenizer with ON, and its preprocessor and parser cases exercise the C preprocessor on Rust-built token arrays.
  • The exported-symbol diffs, and the local gate stamp.

There is no Docker or WSL on this machine.

🤖 Generated with Claude Code

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Maintainer update: rebased onto master as a linear history. Dropped the cf-tokenizer.c extraction commit (merged via #42) and the two merge commits; merged export-list/oracle build.rs/oracle lib.rs entries with #61 (base) and vec2. Added one fix commit: master's #60 (strref compare returns 1 when only the second strref is empty) and #54 (cf_literal_to_str stops at the closing quote) changed the C the ports characterized, so the Rust ports and their Tier 1 expectations now match; parity tests were failing before. Local gate green (lint, rust-test, Tier 2 OFF/ON, exports IDENTICAL 1752) and stamped. Note: #68 also ports lexer.c and will overlap with this. Still a draft for you to mark ready.

@zackees
zackees force-pushed the rust/cf-tokenizer-port branch from 4b1e887 to c879c12 Compare October 10, 2026 01:02
@b7216309-jpg
b7216309-jpg marked this pull request as ready for review October 10, 2026 01:04
@b7216309-jpg
b7216309-jpg force-pushed the rust/cf-tokenizer-port branch from c879c12 to f40241a Compare October 10, 2026 07:33
@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Rebased onto current master (#66 and #74 landed), still linear. Pushed with --force-with-lease against your c879c12, and your commits are kept with the same content.

Changes besides the rebase:

  • macOS strref_cmp_equal_prefixes failure (Rust 1, C -1, after 28 passing cases): a bug in my test. as_strrefs(&Some((x.clone(), x.len()))) built the strrefs from a temporary that is freed at the end of the let, so both calls read freed memory, which another test thread could reuse in between. The new commit 1736968 binds the segment to a local. I could not run macOS here, so this CI run is the confirmation.
  • Duplicate dstr_catf / bmemdup at link time: feat(libobs): port file-serializer to Rust #66 makes the oracle build the real util/dstr.c, and its file_serializer_host.c defines bmemdup. The lexer RED commit no longer adds oracle/test_dstr.c or a second bmemdup in test_bmem.c; the lexer oracle uses those copies. The last commit drops the test_dstr.c notes from testing-policy.md and the oracle docs.

Locally on Windows: soldr cargo test --workspace (37 binaries) and ./lint pass. The two GREEN commits still fail one parity test each against current master, because #54 and #60 changed the C after them. Your fix commit brings the port in line with that C.

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto master 56f6067 (resolved an rust-exports.txt conflict with obs_nal_find_startcode); kept the #54/#60 adaptations. RED commits verified: cf_tokenizer 0/14 and lexer 0/16 pass at the todo!() commits; at head all obs-util tests pass. Local gate (lint, rust-test, tier2-linux) passed and stamped this commit. The earlier Windows failure was an MSBuild file-lock flake. Note: #68 merged into grind/meta-4-c7d3, not master, so the lexer port here still overlaps it.

b7216309-jpg and others added 3 commits October 10, 2026 12:10
Adds the C ABI shim for cf_literal_to_str, cf_lexer_init,
cf_lexer_free and cf_lexer_lex (libobs/util/cf-tokenizer.c), 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 cf_tokenizer fails 0/14 and
--test cf_tokenizer_parity aborts on the first call; the layout test
passes.

The oracle renames the lexer.c functions it calls to their oracle_
names, so it runs on the C lexer oracle rather than on the Rust lexer
port.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of the C-family lexer and cf_literal_to_str.
GREEN for Tier 1 (14/14), the layout tests (2/2) and Tier 3 (5/5).

cf_lex returns the reformatted text and tokens as offsets; the shim
allocates file, the base lexer text, reformatted and the token darray
through bmalloc exactly as C does and turns the offsets into the
pointers the C preprocessor and parsers walk.

The text is read as if NUL bytes followed it. That matches C wherever C
stays in bounds; where C reads past the terminator (a comment or string
ending in a line splice at the end of the text, or \x/octal escapes
skipping past a literal's NUL) the port reads NUL. A NULL
cf_literal_to_str literal returns NULL, where C dereferences it.

\x and octal escapes keep C's strtoul semantics, including the overflow
to 0xff at the width of unsigned long (32 bits on Windows, 64 on
Linux and macOS).

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master fixed two C quirks the lexer/cf-tokenizer ports had characterized:
strref_cmp_strref/strref_cmpi_strref now return 1 when only the second
strref is empty (obs-rust#60), and cf_literal_to_str stops at the closing quote
instead of leaking it after an escape (obs-rust#54). Port both and update the
Tier 1 expectations; the parity tests against the C oracle now pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=b9700f40fdf2c959859b23729ac355aac18f8611 secs=94 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791659811,"gate":"rust/all/fmt","host":"linux-x86_64","key":"80b41cabc2b99754af82351b24464077a0f2c3b5e8e0435931a367feff4ba543","lane":"lint","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":21,"stamp":"e78152623289d105c70f3f12be5fbaa3","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"}
Ci-Attestation: {"at":1791659811,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"80b41cabc2b99754af82351b24464077a0f2c3b5e8e0435931a367feff4ba543","lane":"lint","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":21,"stamp":"b968c0775d5f1beddcf661bf919941da","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"}
Ci-Attestation: {"at":1791659811,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"caf8e4acd1f26e9c504c4828ce9275052454638ab4735c03c11aba9d7e9b72f2","lane":"rust-test","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":21,"stamp":"709942d4b526e2d335967e90ca1a2d42","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"}
Ci-Attestation: {"at":1791659811,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"127862c3824d34ae1b11b28ac0aa24185302d5538923458e438655efe47de3ef","lane":"tier2-linux","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":52,"stamp":"cb6ddd2624a4bf11e4728568e684301c","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"}
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto master after #59 landed #68's lexer port. I dropped this PR's own lexer port (its RED and GREEN commits and the lexer-notes doc commit) and adapted cf_tokenizer to master's lexer API (scan_base_token with IgnoreWhitespace::Parse, and strref_cmp now takes Options). Three commits remain: RED, port, fix-adaptation. RED: cf_tokenizer fails 14/14 at 83d0f78. GREEN: workspace clippy is clean and all tests pass, including cf_tokenizer parity against the C oracle. The local gate passes and is stamped. Both checks were run with soldr's compile cache disabled, because it was replaying stale diagnostics.

@zackees
zackees force-pushed the rust/cf-tokenizer-port branch from cd8a67e to fbec99f Compare October 10, 2026 19:17
@zackees
zackees merged commit f531382 into obs-rust:master Oct 10, 2026
22 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