Repository navigation
Closed
Labels
enhancementNew feature or request
Description
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 >= lenreturn 0;read_bits(n > 8)keeps the low 8 bits;bits <= 0returns 0;r16is 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 readsreader.posdirectly formemcpy(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):
posisuint8_t. Withlen = 300, after 256r8()callsposis0and the 257thr8()returnsdata[0]instead ofdata[256](compiled and run againstlibobs/util/bitstream.c). Withlen ≤ 255there is no wrap: reads stop atpos == lenand 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)
- Workspace: root
Cargo.toml(members = ["rust/*"], resolver 2),rust-toolchain.toml(stable + rustfmt + clippy),rust/README.md. rust/obs-util/src/bitstream.rs— safe coreBitstreamReader<'a> { buf: &'a [u8], pos: usize, sub_pos: u8 }withnew,read_bit,read_bits(u32) -> u8,r8,r16.rust/obs-util/src/ffi/bitstream.rs—#[repr(C)] struct bitstream_readermatching the header exactly, plus#[no_mangle] extern "C"versions of the four exported functions that delegate to the core and write backposasu8.rust/obs-c-oracle/— dev-only crate;build.rscompilesoracle/bitstream.c, which#definesbitstream_reader_* → oracle_bitstream_reader_*and#includeslibobs/util/bitstream.c. Also exposessizeof/offsetofvalues measured from the real header.- CMake:
ENABLE_RUST_LIBOBSoption (defaultOFF); whenON,util/bitstream.cis 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 intest_bitstream.c, plus edge cases:read_bits(9..=16)truncation,bits == 0, a 300-byte buffer through the safe core) committed againsttodo!()stubs;soldr cargo test -p obs-utilfails — output shown in the PR. - Tier 1, GREEN: with the real core,
soldr cargo test -p obs-utilpasses. - Tier 2, layout:
tests/bitstream_layout.rsassertssize_of,align_of, and each field offset of the#[repr(C)]struct equal the values from the C header viaobs-c-oracle. - Tier 2, C test:
test/cmocka/test_bitstream.cis unmodified and passes withENABLE_RUST_LIBOBS=OFFand=ON.bitstream.his unmodified. - Tier 3:
tests/bitstream_parity.rsproptest drives random buffers (0..=1024 bytes, including > 255) and random read sequences through bothoracle_*and the Rustextern "C"functions; outputs andpos/subPosstate are identical. -
soldr cargo clippy --all-targets -- -D warningsandsoldr cargo fmt --checkclean;unsafeonly insrc/ffi/andobs-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 posand its wraparound; the safe Rust core usesusize. 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 nameobs-utilmirrorslibobs/util/. ENABLE_RUST_LIBOBSdefaultOFFuntil 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
2024assumed. - Windows: the Rust staticlib's symbols must be exported from
obs.dll(C uses__declspec(dllexport)viaEXPORT). A.deffile or#[export_name]+ linker args may be needed; resolve during step 5.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Type
Projects
- StatusShow more project fieldsDone
Activity
[-]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)[/+]zackees commentedon Oct 8, 2026
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 earlierusizeplan 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.zackees commentedon Oct 8, 2026
Correction after a deeper look at the
uint8_t poswraparound:libobs/util/bitstream.cand ran it: with a 300-byte buffer, after 256r8()callspos == 0, and the nextr8()returnsdata[0](0xAA) instead ofdata[256](0x55). Withlen <= 255there is no wrap.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.reader.posdirectly, so the field layout is used by callers, not just declared.The issue body is updated to match.
test: Add failing tests for Wayland global hotkeys through the portal