Skip to content

feat(obs-util): port utf8_to_wchar/wchar_to_utf8 (#6) - #40

Closed
b7216309-jpg wants to merge 4 commits into
obs-rust:masterfrom
b7216309-jpg:rust/utf8-port
Closed

b7216309-jpg wants to merge 4 commits into
obs-rust:masterfrom
b7216309-jpg:rust/utf8-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)

Ports the non-Windows libobs/util/utf8.c (utf8_to_wchar, wchar_to_utf8) to Rust. This is step 2 of the Phase 3 port order, after crc32.

Commits

  1. RED: the shim, two oracle wrappers, the ENABLE_RUST_LIBOBS guard, and Tier 1 and Tier 3 tests against a todo!() core. Tier 1 fails 0/19 and Tier 3 fails 0/5.
  2. GREEN: the safe core. Tier 1 passes 19/19 and Tier 3 passes 5/5.
  3. test: a stronger parity generator, added after the mutation check (see below).

Decisions (also recorded in #6)

  • Non-Windows only. On Windows, utf8.c wraps MultiByteToWideChar and WideCharToMultiByte on 16-bit wchar_t. The guard keeps the C file on Windows ($<OR:$<NOT:ENABLE_RUST_LIBOBS>,OS_WINDOWS>), and ffi::utf8 is cfg(not(windows)). This follows the policy's "per-OS files are ported per OS" rule. test_utf8 is already NOT MSVC.
  • wchar_t signedness. It is int on x86 Linux and macOS and unsigned int on Arm Linux. Only wchar_to_utf8 observes it: a negative value is an error, while the same bits as an unsigned value encode as six bytes with bit 31 dropped. The core is generic over a WChar trait implemented for i32 and u32, following the Position pattern in bitstream. The shim uses libc::wchar_t. libc is a new non-Windows dependency and was already in Cargo.lock.
  • Two oracles. oracle/utf8_s32.c and utf8_u32.c #undef _WIN32 and #define wchar_t as int32_t or uint32_t before including the unmodified util/utf8.c. Parity therefore covers both signedness variants on every OS, including Windows dev machines.
  • Internal symbols. libobs builds with hidden visibility and utf8.c has no EXPORT, so with ENABLE_RUST_LIBOBS=ON the #[no_mangle] shims would leak into the dynamic symbol table. They are named as local: in rust-exports.map and in rust-unexports-macos.txt, and they are not in rust-exports.txt. export_list.rs now requires every shim to be either exported or hidden (not both), and every hidden shim to also be in the macOS list. The policy doc has a bullet for this.
  • Tier 2. test_utf8 compiles utf8.c into the test executable, as Phase 3: characterize leaf utilities and port crc32 (#6) #34 set up. It therefore still characterizes the C source and does not exercise the swap. The swap is exercised by dstr.c and platform.c inside libobs with ON. The shim itself is covered by utf8_parity::shim (NULL input, insize 0, the platform wchar_t).

Intentional differences

None. Characterized and kept as-is: overlong forms and code points above U+10FFFF are accepted. Counting mode (out == NULL) skips the surrogate and BOM checks. A dropped character stays in the output buffer past the returned count. UTF8_IGNORE_ERROR also disables the forbidden-octet check. On error, partial output is left in the buffer. Parity compares the whole output buffer (sentinel-filled), not just the return value.

Mutation check

I applied nine mutations to the core. The first parity generator (random bytes, edge bytes, valid strings) missed one: forbidding 0xf6 instead of 0xf5. Without UTF8_IGNORE_ERROR the first invalid byte makes both sides return 0, and random bytes rarely form a complete sequence. Commit 3 adds lead bytes with 0–6 continuation bytes mixed into valid text. Parity now catches all of the following:

Mutation Caught
forbid 0xf6 instead of 0xf5 ✅
0xc0 not forbidden ✅
six-byte lead keeps bit 31 ✅
output advances past a dropped surrogate ✅
BOM skipped without the flag ✅
encoder space check off by one ✅
truncated sequence allowed ✅
five-byte lead mask 0x07 equivalent (bit 2 is always 0 in 0xf8–0xfb)

Verification

On Windows 11 (MSVC):

  • soldr cargo test --workspace --locked: 80 passed.
  • ./lint passes.
  • Parity soak with 300k cases per property (release build) passes.
  • cargo clippy -p obs-util -p libobs-rust --lib -D warnings is clean for x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu and aarch64-apple-darwin. This type-checks the non-Windows shim under both signedness variants.

Not verified locally

  • The non-Windows shim tests (utf8_parity::shim) and Tier 2 OFF/ON, including the exported-symbol diff from rust/tools/linux-validate/run.sh. This machine has no Docker and no WSL, so they first run in CI. I'd especially like the rust-linux-tier2 and macOS rust-tier2 symbol diffs to confirm that the local: and -unexported_symbols_list entries keep the export sets identical.
  • The local gate stamp, for the same reason.

🤖 Generated with Claude Code

b7216309-jpg and others added 3 commits October 9, 2026 13:05
Adds the utf8_to_wchar/wchar_to_utf8 shim, two oracle wrappers, the
ENABLE_RUST_LIBOBS guard and Tier 1/Tier 3 tests. The safe core bodies
are todo!(), so soldr cargo test -p obs-util --test utf8 fails 0/19
and --test utf8_parity fails 0/5.

Only the non-Windows implementation is ported. On Windows utf8.c wraps
MultiByteToWideChar/WideCharToMultiByte on 16-bit wchar_t, so the guard
keeps the C file there and the shim is cfg(not(windows)).

The core is generic over the wchar_t signedness (int on x86 Linux and
macOS, unsigned int on Arm Linux), which only wchar_to_utf8 observes.
The oracle compiles the non-Windows branch on every platform, once with
wchar_t as int32_t and once as uint32_t, so parity covers both.

libobs builds with hidden visibility and utf8.c has no EXPORT, so the
shims are named as local in rust-exports.map and in
rust-unexports-macos.txt rather than listed in rust-exports.txt.
export_list.rs now checks that every shim is exported or hidden.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of the non-Windows libobs/util/utf8.c. GREEN
for the Tier 1 (19/19) and Tier 3 (5/5) utf8 tests.

The decoder writes each decoded character before the surrogate and BOM
checks and only advances past it when it is kept, so a dropped
character stays in the buffer past the returned count, as in C. The
encoder drops bit 31 of six-byte sequences, which only an unsigned
wchar_t reaches, as the C byte shuffling does.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Purely random bytes almost never form a lead byte followed by enough
continuation bytes, and without UTF8_IGNORE_ERROR the first invalid
byte makes C and Rust both return 0. A mutation that forbade 0xf6
instead of 0xf5 therefore passed the parity test.

Add inputs built from lead bytes with 0-6 continuation bytes, mixed
into otherwise valid text. With them the parity test catches every
non-equivalent mutation tried: wrong forbidden octets, the six-byte
lead mask, advancing past a dropped surrogate, BOM skipping without
the flag, and off-by-one space and truncation checks.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	rust/obs-c-oracle/build.rs
#	rust/obs-c-oracle/src/lib.rs
#	rust/obs-util/tests/export_list.rs
@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