Skip to content

We need to refactor unsafe around code boundaries like ffi #297

Closed

Description

@zackees

Goal

Keep unsafe at the code boundaries (C ABI shims in src/ffi, documented OS calls) so the safe core of each port crate cannot grow new unsafe unnoticed. Follow-up to #290.

Current state (origin/master)

Crate Lint Allowed exceptions
obs-util #![deny(unsafe_code)] mod ffi; pipe.rs:151 (Win32 handles); utf8.rs:14 (MultiByteToWideChar/WideCharToMultiByte)
obs-codec #![deny(unsafe_code)] mod ffi
obs-graphics #![deny(unsafe_code)] mod ffi
obs-media-io #![deny(unsafe_code)] mod ffi; mod audio_io (module-wide allow, 58 unsafe tokens in audio_io.rs)
obs-c-oracle none ~48 unsafe tokens; test-only differential oracle calling the original C, so FFI is its whole purpose
libobs-rust, obs-stream-rust, tools none no unsafe today

Remaining work

  • rust/obs-media-io: move audio_io.rs unsafe out of the safe core into src/ffi #294: move audio_io.rs unsafe into src/ffi and drop the module-level #[allow(unsafe_code)] in obs-media-io/src/lib.rs (RED: remove the allow, build fails; GREEN: refactor).
  • Add #![deny(unsafe_code)] (or forbid) to libobs-rust and obs-stream-rust now, while they hold zero unsafe, so they start clean.
  • Decide on obs-c-oracle: either leave it exempt (document why in its lib.rs) or require // SAFETY: comments via clippy::undocumented_unsafe_blocks.
  • Optional: enable clippy::undocumented_unsafe_blocks for the ffi modules workspace-wide so every remaining block carries a // SAFETY: note.

Done when

No module-wide allow(unsafe_code) remains outside ffi and the two documented Win32 boundaries, and every port crate has the lint.

Activity

  1. zackees commented on Oct 12, 2026

    @zackees
    MemberAuthor

    Status: #290 is merged. obs-util, obs-codec, obs-graphics and obs-media-io now #![deny(unsafe_code)] and allow it back only on mod ffi and two documented Win32 boundaries (pipe.rs, utf8.rs). A new unsafe in a safe core is now a build error.

    One exception remains: mod audio_io in obs-media-io carries a module-level #[allow(unsafe_code)], because the audio-io port (#289) landed with 54 unsafe sites in its safe core just before the lint. Moving them behind src/ffi is tracked in #294, which has a RED/GREEN plan (drop the allow so the build fails with 54 errors, then refactor). Once #294 lands, this issue can close.

  2. zackees commented on Oct 12, 2026

    @zackees
    MemberAuthor

    Proposal: adopt the platform-boundary pattern from soldr / zccache / fbuild

    Those three repos solve the same problem with one platform module (or crate) plus a custom Dylint that keeps platform code inside it:

    • soldr: crates/soldr-platform holds one cfg_select! in lib.rs and the platform_win / platform_linux / platform_macos trees. dylints/ban_platform_cfg_outside_boundary runs pre-expansion, so code that is cfg'd out for the current target is checked too. It rejects #[cfg(windows|unix|target_os…)], #[cfg_attr] and cfg!() everywhere else, and bans references to the concrete trees from outside the crate. Sibling lints (ban_raw_process_creation, ban_raw_network_access, …) then expect raw OS APIs only inside that crate. A site opts out with #[cfg_attr(dylint_lib = "...", allow(...))]. The lints are wired in through [workspace.metadata.dylint] libraries = [{ path = "dylints/*" }].
    • zccache: dylints/enforce_platform_boundary allows host cfg, std::os::{windows,unix}, libc and windows_sys only in three named platform.rs files. It has no baseline or allowlist.
    • fbuild: has the same enforce_platform_boundary lint plus about 25 other ban_* lints.

    Why this matters here

    #![deny(unsafe_code)] from #290 is a normal rustc lint, so it only checks code that compiles for the target being built. obs-util/src/pipe.rs:150-153 and obs-util/src/utf8.rs:13-14 are #[cfg(windows)], so a Linux lint lane never compiles or checks them. Platform-specific unsafe can sit in the safe core today as long as it is behind a host cfg. soldr's lint docs make the same point: the pre-expansion boundary lint is what lets a Linux-only lint lane stand in for every platform.

    Host cfgs in non-ffi production code on master today:

    File Sites
    obs-util/src/pipe.rs 7 (19, 150, 153, 159, 169, 174, 207)
    obs-util/src/bmem.rs 2
    obs-util/src/config_file.rs 2
    obs-util/src/file_serializer.rs 2
    obs-util/src/task.rs 2
    obs-util/src/threading.rs 1
    obs-util/src/utf8.rs 1
    obs-media-io/src/audio_io.rs 3 (239, 1136, plus the module-level #[cfg(unix)] port)

    Suggested plan

    1. Add a platform module per crate (obs-util/src/platform/{mod,unix,windows}.rs), or a shared obs-platform crate. It holds the only cfg_select! and exposes one safe API to the core.
    2. Add rust/dylints/ban_platform_cfg_outside_boundary, ported from soldr. Allowed paths: */src/platform/** and */src/ffi/**, since the C ABI shims also need host cfgs such as ffi/native_sem.rs. Tests are scanned the same way; a test needing a host cfg uses the cfg_attr(dylint_lib…) opt-out.
    3. Optionally add a second lint, ban_unsafe_outside_boundary. It would also run pre-expansion and reject unsafe outside ffi and platform, which covers what deny(unsafe_code) misses on cfg'd-out code. The two Win32 allow(unsafe_code) sites then move into platform/windows.rs.
    4. Hook cargo dylint --all into ./lint and local-gate.toml with the nightly pinned the way soldr does. That needs a nightly toolchain next to rust-toolchain.toml, which the testing policy should note.

    RED/GREEN: land the lint first with the sites above failing (RED), then move each site behind platform (GREEN), one crate per PR. #294 (audio_io) can follow the same shape.

  3. zackees commented on Oct 12, 2026

    @zackees
    MemberAuthor

    Remaining checklist: #413 (audio_io engine into src/ffi, closes #294) and #414 (forbid unsafe_code in libobs-rust and obs-stream-rust; obs-c-oracle exemption documented). The platform boundary Dylint landed in #326. Once both merge, this issue can close.

  4. zackees commented on Oct 13, 2026

    @zackees
    MemberAuthor

    Done: #326 (platform-boundary Dylint), #413 (audio_io engine into src/ffi, closed #294), #414 (forbid unsafe_code in libobs-rust and obs-studio-rust; obs-c-oracle exemption documented). All merged.

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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions