Skip to content

feat(rust): port libobs/util/bitstream to Rust under the four-tier testing policy (first port) #1

Closed

Description

@zackees

Context

First step of the Rust conversion: port the smallest unit with an existing test, and establish the Cargo workspace, toolchain pin, C-oracle crate, and CMake hookup that every later port reuses.

This port MUST follow docs/rust-port/testing-policy.md exactly (added in #2). Goal: the public libobs C API/ABI stays compatible; internals are Rust.

The C unit tests live in test/cmocka/ (only built with ENABLE_UNIT_TESTS; no current workflow runs them):

Test LOC Code under test Dependencies
test_bitstream.c 36 libobs/util/bitstream.c (51 LOC) none
test_darray.c 31 util/darray.h (macro-generic array) bmalloc
test_serializer.c 36 util/serializer.h bmalloc, darray
test_os_path.c 53 util/platform.c os_get_path_extension platform layer

C behavior and ABI to preserve (libobs/util/bitstream.{c,h})

struct bitstream_reader { uint8_t pos; uint8_t subPos; uint8_t *buf; size_t len; };
EXPORT void     bitstream_reader_init(struct bitstream_reader *r, uint8_t *data, size_t len);
EXPORT uint8_t  bitstream_reader_read_bits(struct bitstream_reader *r, int bits);
EXPORT uint8_t  bitstream_reader_r8(struct bitstream_reader *r);
EXPORT uint16_t bitstream_reader_r16(struct bitstream_reader *r);
  • MSB-first; reads at pos >= len return 0; read_bits(n > 8) keeps the low 8 bits; bits <= 0 returns 0; r16 is big-endian.
  • The struct is caller-allocated and callers read its fields directly, so its layout is ABI and must be reproduced exactly. In-tree callers:
    • plugins/decklink/decklink-device-instance.cpp:259 — stack-allocates it, parses a CEA-708 CDP header (≤ ~14 bytes via the reader), then reads reader.pos directly for memcpy(outData, anc + reader.pos, cc_count * 3).
    • libobs/obs-avc.c:179 (get_sps_high_params) — stack-allocates it to parse an H.264 SPS (24 bits + four Exp-Golomb codes, ≤ ~36 bytes).
  • Latent C defect (verified): pos is uint8_t. With len = 300, after 256 r8() calls pos is 0 and the 257th r8() returns data[0] instead of data[256] (compiled and run against libobs/util/bitstream.c). With len ≤ 255 there is no wrap: reads stop at pos == len and return 0. Neither in-tree caller can trigger it: both read only the first few dozen bytes, whatever the buffer size. It's a latent trap for future callers, not a live bug. Because the layout is ABI, the C boundary keeps this behavior (see Decisions).

Proposal (per the policy's layout)

  1. Workspace: root Cargo.toml (members = ["rust/*"], resolver 2), rust-toolchain.toml (stable + rustfmt + clippy), rust/README.md.
  2. rust/obs-util/src/bitstream.rs — safe core BitstreamReader<'a> { buf: &'a [u8], pos: usize, sub_pos: u8 } with new, read_bit, read_bits(u32) -> u8, r8, r16.
  3. rust/obs-util/src/ffi/bitstream.rs — #[repr(C)] struct bitstream_reader matching the header exactly, plus #[no_mangle] extern "C" versions of the four exported functions that delegate to the core and write back pos as u8.
  4. rust/obs-c-oracle/ — dev-only crate; build.rs compiles oracle/bitstream.c, which #defines bitstream_reader_* → oracle_bitstream_reader_* and #includes libobs/util/bitstream.c. Also exposes sizeof/offsetof values measured from the real header.
  5. CMake: ENABLE_RUST_LIBOBS option (default OFF); when ON, util/bitstream.c is dropped from libobs and the Rust crate is linked in (Corrosion), with the four symbols exported from libobs.

Acceptance criteria

  • Tier 1, RED: rust/obs-util/tests/bitstream.rs (1:1 port of every assertion in test_bitstream.c, plus edge cases: read_bits(9..=16) truncation, bits == 0, a 300-byte buffer through the safe core) committed against todo!() stubs; soldr cargo test -p obs-util fails — output shown in the PR.
  • Tier 1, GREEN: with the real core, soldr cargo test -p obs-util passes.
  • Tier 2, layout: tests/bitstream_layout.rs asserts size_of, align_of, and each field offset of the #[repr(C)] struct equal the values from the C header via obs-c-oracle.
  • Tier 2, C test: test/cmocka/test_bitstream.c is unmodified and passes with ENABLE_RUST_LIBOBS=OFF and =ON. bitstream.h is unmodified.
  • Tier 3: tests/bitstream_parity.rs proptest drives random buffers (0..=1024 bytes, including > 255) and random read sequences through both oracle_* and the Rust extern "C" functions; outputs and pos/subPos state are identical.
  • soldr cargo clippy --all-targets -- -D warnings and soldr cargo fmt --check clean; unsafe only in src/ffi/ and obs-c-oracle, each with a // SAFETY: comment.
  • No new GitHub Actions workflow files.

Decisions

  • Unit chosen: bitstream — smallest dependency surface of the four cmocka tests.
  • Four-tier testing per the policy — Rust tests prove the core is correct, the unchanged C test proves compatibility, the oracle proptest proves identical behavior.
  • C boundary keeps u8 pos and its wraparound; the safe Rust core uses usize. Fixing the C behavior requires a header/ABI change, which the policy forbids inside a port — file separately if wanted. The Tier 3 proptest therefore matches the oracle exactly, with no excluded inputs.
  • Existing header is authoritative — no cbindgen-generated replacement header.
  • Workspace at repo root, crates under rust/; crate name obs-util mirrors libobs/util/.
  • ENABLE_RUST_LIBOBS default OFF until the port is accepted.
  • No CI wiring — adding a workflow file needs explicit sign-off; hooking into an existing workflow is a follow-up.
  • Priority: P2.

Open questions

  • Edition 2024 assumed.
  • Windows: the Rust staticlib's symbols must be exported from obs.dll (C uses __declspec(dllexport) via EXPORT). A .def file or #[export_name] + linker args may be needed; resolve during step 5.

Activity

  1. added
    enhancement
    on Oct 8, 2026
  2. changed the title [-]feat(rust): port test_bitstream + libobs/util/bitstream to Rust (first Rust unit test)[/-] [+]feat(rust): port libobs/util/bitstream to Rust under the four-tier testing policy (first port)[/+] on Oct 8, 2026
  3. zackees commented on Oct 8, 2026

    @zackees
    MemberAuthor

    Rescoped to follow the four-tier testing policy in #2. Key change: the API/ABI must stay compatible, so the C-facing struct keeps uint8_t pos (the earlier usize plan applies only to the safe Rust core), the unchanged C test must pass against the Rust implementation, and a proptest compares against the original C compiled as an oracle.

  4. zackees commented on Oct 8, 2026

    @zackees
    MemberAuthor

    Correction after a deeper look at the uint8_t pos wraparound:

    • The wrap is real. I compiled libobs/util/bitstream.c and ran it: with a 300-byte buffer, after 256 r8() calls pos == 0, and the next r8() returns data[0] (0xAA) instead of data[256] (0x55). With len <= 255 there is no wrap.
    • Neither in-tree caller can trigger it. My earlier reasoning ("CDP packets are normally < 256 bytes") was the wrong argument. The real reason is that callers only read a short prefix: decklink reads ≤ ~14 bytes through the reader, and libobs/obs-avc.c (a second caller I missed earlier) reads ≤ ~36 bytes of an SPS. So this is a latent defect, not a live bug.
    • This adds to the ABI constraint: decklink reads reader.pos directly, so the field layout is used by callers, not just declared.

    The issue body is updated to match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions