Skip to content

rust: Deny unsafe_code in the port crates - #290

Merged
zackees merged 4 commits into
obs-rust:masterfrom
MCPVOT:infra/unsafe-code-lint
Oct 12, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
MCPVOT:infra/unsafe-code-lint

Conversation

@MCPVOT

@MCPVOT MCPVOT commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Refs #143

What and why

The testing policy says a safe core contains no unsafe, and nothing enforced
it: src/ffi/ is the only place the policy allows, but the lint that would
catch a stray block was off. Issue #143 lists that enforcement as unclaimed,
with rust/obs-util/src/config_file.rs:949 as the example.

This denies unsafe_code for all four port crates (obs-util, obs-codec,
obs-graphics, obs-media-io) and allows it back only where the C boundary
is:

  • every src/ffi/ module, which is what the policy permits;
  • obs_util::pipe::windows and obs_util::utf8::windows, the two Windows
    platform modules that call Win32 directly. The pipe port (rust: Port util/pipe-windows.c to obs-util #259) documents
    that choice: a shim may not contain logic, so the handle calls stay in the
    platform module and they are unsafe by nature.

Both Windows modules carry their own #[allow(unsafe_code)] with the reason,
so the exception is visible where the module is declared.

Cleaning up the two safe cores that failed the lint turned up one real defect:

  • config_file's get_string read the value back through the address that
    config_get_string returns, after the lock had been dropped. A writer that
    replaces the value in between frees the buffer, so the safe Rust API could
    read freed memory. It copies the bytes with the lock held now. The bytes
    returned are unchanged, so there is no observable difference from C.
  • profiler's safe core held the blog declaration, a bmalloc adoption and
    the box drop. The shim owns them now: profile_end reports the two
    LOG_ERROR lines profiler.c prints through a callback the shim implements
    (ProfileLog), which is the split the pipe port already uses for a failed
    CreateProcessW. Tier 1 gains a case for those two lines; cmocka cannot
    observe them, because blog's output is not capturable there.

docs/rust-port/testing-policy.md now records the invariant and the two
allowed exceptions.

RED → GREEN

  • RED commit: 569a531 (rust: Deny unsafe_code in the port crates (RED)).
    With the denial and nothing else, cargo check --workspace fails with 10
    E0133 errors: config_file.rs:949, and profiler.rs:17, 105, 111, 115, 117, 137, 142, 495, 504.
  • GREEN: f2c21a9 (plus dcb1837 for the policy wording).
    • cargo check --workspace clean
    • cargo clippy --workspace --all-targets -- -D warnings exit 0
    • cargo fmt --all --check exit 0
    • cargo test --workspace: 70 of 71 test binaries pass

Honest state of the local run

I could not run the repo's gate on this machine, so this head carries no
attestation and is not stamped. The reasons, so you can judge the evidence:

  • the local toolchain is 1.97.1 (windows-gnullvm) against the pinned 1.98.1,
    so the tests ran as cargo test --workspace --ignore-rust-version;
  • no soldr and no Docker here, so the Tier 2 lane did not run at all: CI's
    tier2-linux and rust-tier2 are the first check of the C side. Nothing in
    this diff touches C, exported symbols or a header;
  • one test binary, buffered_file_serializer_parity, aborts with
    STATUS_ACCESS_VIOLATION in defaults_match_oracle. I re-ran it with this
    branch stashed, i.e. on unmodified master, and it aborts the same way, so it
    is not from this change. It looks windows-gnullvm-specific (CI runs MSVC); I
    can file it separately with the raw output if that is useful.

Two local shims were needed to build the oracle on a windows-gnu host at all,
and neither is in this diff: three MSVC intrinsics LLVM-MinGW's intrin.h
does not declare, and the pthread implementation, because
obs-c-oracle/build.rs picks pthread by compiler rather than by target. If you
want, I can send that one as its own small PR (keyed on CARGO_CFG_WINDOWS
plus the GCC/clang deps/w32-pthreads path instead of is_like_msvc()).

Checklist

  • Base is master
  • I claimed the issue, and no open PR touches this
  • RED, then GREEN commits
  • Local gate passed and the pushed head is stamped - see above: no soldr,
    no Docker, and the MSRV is one behind, so CI's jobs are the first full check
  • Intentional differences from C: none. config_file returns the same
    bytes, and profiler keeps the same blog calls with the same arguments in
    the same order.

@MCPVOT
MCPVOT requested a review from zackees as a code owner October 12, 2026 01:29
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

thank you for handling that

i'll follow up on the boundary seam later

The testing policy says the safe cores contain no `unsafe`, and nothing
enforced it. Deny the lint for the whole of every port crate and allow it
back only where the C ABI lives: the `ffi` modules, plus the two Windows
platform modules that call Win32 directly (`pipe::windows`,
`utf8::windows`), which the pipe port documents as deliberate.

With this commit alone the crates do not build: the lint fires on the two
safe cores that still hold `unsafe`.

Refs obs-rust#143
The `unsafe_code` denial now passes, with the two offending safe cores
cleaned up:

- config_file: `get_string` returned the value through the
  `config_get_string` address after the lock was dropped, so a writer that
  replaced the value in between freed the buffer the pointer came from.
  Copy the bytes with the lock held instead. Same bytes, no observable
  difference.
- profiler: the `blog` declaration, the `bmalloc` adoption and the box
  drop move into the shim. `profile_end` reports what `profiler.c` logs
  through a callback the shim implements, the split the pipe port already
  uses for a failed `CreateProcessW`.

Tier 1 gains a case for those two log lines, which cmocka cannot observe.

`cargo test --workspace`: 70 of 71 test binaries pass. The exception,
`buffered_file_serializer_parity`, aborts with STATUS_ACCESS_VIOLATION in
`defaults_match_oracle` on this machine and does so on unmodified master
too (verified by stashing this change first), so it is not from here.

Refs obs-rust#143
The policy already said the safe cores have no `unsafe`; say which lint
enforces it and where `allow` is written back.

Refs obs-rust#143
The audio-io port (obs-rust#289) merged with 54 unsafe sites in its safe core,
which the new deny(unsafe_code) rejects. Allow it on the module with a
pointer to obs-rust#294, which moves that code into src/ffi; every other module
stays under the lint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=63a892fb6ff84969dfc38d23be2f2b9923ce553a secs=114 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791769092,"gate":"rust/all/fmt","host":"linux-x86_64","key":"1ab3f600ca0050bca59f435d6ac45dde91181ac092d7b4c93a504c64ca7415ca","lane":"lint","parents":["2260b8992d1aeb76f94aacdc152fe1667d64b494"],"secs":14,"stamp":"5ad062e4a22509f1f532eeea1e502669","tree":"63a892fb6ff84969dfc38d23be2f2b9923ce553a","v":1,"via":"run"}
Ci-Attestation: {"at":1791769092,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"1ab3f600ca0050bca59f435d6ac45dde91181ac092d7b4c93a504c64ca7415ca","lane":"lint","parents":["2260b8992d1aeb76f94aacdc152fe1667d64b494"],"secs":14,"stamp":"6f07834adae2cd0f49955dab4643f20d","tree":"63a892fb6ff84969dfc38d23be2f2b9923ce553a","v":1,"via":"run"}
Ci-Attestation: {"at":1791769092,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"313badc9388c6f37a20dfac00209e8950e9008b086a82482e43137ddafc03b89","lane":"rust-test","parents":["2260b8992d1aeb76f94aacdc152fe1667d64b494"],"secs":47,"stamp":"27ba81a320fbccbcb6b31e042abfe257","tree":"63a892fb6ff84969dfc38d23be2f2b9923ce553a","v":1,"via":"run"}
Ci-Attestation: {"at":1791769092,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"217841ec98a6c6f2d928fc014925c0eec19795d829d8080fc907b2c7dd9bcf77","lane":"tier2-linux","parents":["2260b8992d1aeb76f94aacdc152fe1667d64b494"],"secs":53,"stamp":"d8ca05bee2805bf43c4f3bc60f9f5239","tree":"63a892fb6ff84969dfc38d23be2f2b9923ce553a","v":1,"via":"run"}
@zackees
zackees force-pushed the infra/unsafe-code-lint branch from dcb1837 to 4b3eaab Compare October 12, 2026 01:38
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The new head is 4b3eaab84.

  • RED (569a5315a): cargo build of the four port crates fails with 7 unsafe_code errors.
  • GREEN: clippy --all-targets -D warnings is clean, and every test suite in obs-util, obs-codec, obs-graphics and obs-media-io passes. ProfileName::from_static still takes &'static CStr.

One addition after the rebase: the audio-io port (#289) merged just before this and has 54 unsafe sites in its safe core (rust/obs-media-io/src/audio_io.rs, the raw-pointer API and audio-thread cells), so the new lint rejected master. Moving those into src/ffi is too big a refactor to fold in here. I added one commit that puts a module-level #[allow(unsafe_code)] on mod audio_io, citing #294, which tracks doing it properly with its own RED and GREEN. Every other module stays under the lint.

Local gate: lint, rust-test and tier2-linux all passed. Thanks for picking up this part of #143. Merging once CI is green.

@MCPVOT

MCPVOT commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks - the boundary seam is yours to call, and I will not push over a stamped head. My local rebase is parked on a scratch branch (local-rebase-backup); the PR branch here now matches 4b3eaab84 exactly, and I re-fetched before doing anything else.

Two exact numbers for the record, because three different figures are now in play (7 in your note, 10 in my RED commit message, 64 in reality after the rebase):

  • On the original base (cb6cd121b), the RED commit alone reports 10 E0133 errors, all in obs-util: config_file.rs:949, plus profiler.rs:17, 105, 111, 115, 117, 137, 142, 495, 504.
  • Those 10 split as 7 usage of an unsafe block, 1 usage of an unsafe extern block, 1 declaration of an unsafe function and 1 implementation of an unsafe method. If cargo build over the four crates reported 7, that is most likely the seven plain blocks; the other three sites render as different error kinds.
  • After your rebase onto media-io: Port audio-io to Rust #289's merge, that same RED commit (34ff6b888) alone reports 64 - the 10 above plus 54 in rust/obs-media-io/src/audio_io.rs. Re-running it is how I checked that your module-level allow in 4b3eaab84 is scoped and not a blanket exemption: with it the count drops back to exactly 10, and audio_io contributes 0. Nothing needs changing; I am only correcting the record my own commit message left stale.

I also ran your head on this Windows host (rustc 1.97.1, windows-gnullvm, with the two local oracle shims): fmt --all --check exit 0, clippy --workspace --all-targets -D warnings exit 0, and 74 of 75 test binaries pass. The exception is the buffered_file_serializer_parity STATUS_ACCESS_VIOLATION in the PR description, which reproduces on unmodified master here and is unrelated to this change.

On #294: the plan you wrote is the one I would have proposed, and I fixed the boundary list from the port's own unsafe extern "C" block while reading it - os_gettime_ns, os_set_thread_name, profile_store_name, obs_get_profiler_name_store, os_sleepto_ns_fast, profile_start, profile_end, profile_reenable_thread, the resampler create/destroy pair, and the input callback invocation. One caveat if you want me to take it: that module is #[cfg(unix)], so on this host it does not compile at all and I could not run a single one of its tests - I would be leaning entirely on CI's Linux lanes for RED and GREEN. If that is acceptable, say so and I will claim it; otherwise it is better with someone who can run it locally.

Still unclaimed in #143 and locally verifiable from Windows: the clippy::undocumented_unsafe_blocks sweep over src/ffi/ (about 40 modules). I can take that as its own PR, split per crate, if you want it.

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