Skip to content

rust: Enforce a platform boundary with a pre-expansion Dylint #323

Closed

Description

@zackees

Part of #297. Ports the platform-boundary pattern used by soldr (dylints/ban_platform_cfg_outside_boundary), zccache and fbuild (dylints/enforce_platform_boundary).

Problem

#![deny(unsafe_code)] (#290) and clippy only check code that compiles for the target being linted. Host-cfg'd code in the safe cores, such as obs-util/src/pipe.rs:150 and utf8.rs:13 (both #[cfg(windows)] + allow(unsafe_code)), is never checked by the Linux lint lane. Host selection is spread across about 20 sites in production sources:

File Host cfg / native refs
obs-util/src/pipe.rs (+ pipe/windows.rs) 7 cfg, std::os::unix, Win32
obs-util/src/bmem.rs 2 (CLong width)
obs-util/src/config_file.rs 2 (path bytes, Windows BOM) + std::os::unix
obs-util/src/file_serializer.rs 2 (path bytes) + std::os::unix
obs-util/src/task.rs 2 (native vs condvar sem)
obs-util/src/threading.rs 1 (Sem re-export)
obs-util/src/utf8.rs (+ utf8/windows.rs) 1, Win32
obs-media-io/src/audio_io.rs 2 (mod port gate) + 9 libc::pthread_*

Policy

Each port crate has one src/platform/ module. src/platform/mod.rs is the only host-selection site in the crate (cfg_select!). Its children (unix.rs, windows.rs, …) hold the native code, and the rest of the crate gets a platform-neutral API from it. The C ABI shims in src/ffi/ also stay allowed, because the exported symbol set itself differs per OS.

Everywhere else in rust/*/src/**, a pre-expansion Dylint rejects:

  • #[cfg], #[cfg_attr], #![cfg] and cfg!() using windows, unix, target_os, target_family, target_arch, target_env, target_pointer_width and similar host selectors;
  • std::os::{windows,unix,linux,macos}, libc::, windows_sys, windows::Win32.

Because the lint runs before cfg expansion, code compiled out for the host is checked too, so a single Linux lint lane covers every platform.

Out of scope: rust/obs-c-oracle (a test-only crate whose purpose is linking the original C), rust/tools, and integration tests under tests/. Tests stay as they are, mirroring the C tests' platform guards, and are not held to deny(unsafe_code).

Plan

  1. RED: add dylints/ban_platform_cfg_outside_boundary (cdylib, dylint_linting, nightly pinned in its own rust-toolchain.toml), adapted from soldr, with UI tests. Register it in the root Cargo.toml [workspace.metadata.dylint] and run soldr cargo dylint --all -- --workspace --all-targets in the ./lint lane. It fails on the sites above.
  2. GREEN, obs-util: add src/platform/{mod,unix,windows}.rs. Move into it the CLong widths, path_from_bytes, the Windows config BOM, the Sem selection, the pipe Unix/Windows halves and the utf8 Windows converter. The Win32 allow(unsafe_code) sites move to platform/windows, and lib.rs allows unsafe_code only on mod ffi and mod platform.
  3. GREEN, obs-media-io: add src/platform/{mod,unix}.rs. Move the recursive pthread_mutex_t (InputMutex) behind a platform RecursiveMutex. Gate the Unix-only audio_io::port with a capability macro defined in platform/mod.rs (with_audio_thread_port!), so the decision lives in the boundary. This removes the libc:: references from audio_io.rs; the remaining unsafe in the port is still rust/obs-media-io: move audio_io.rs unsafe out of the safe core into src/ffi #294.
  4. Docs: add the policy to docs/rust-port/testing-policy.md and AGENTS.md (where platform code goes, how to run the lint, how to opt out).

Acceptance

  • ./lint runs the dylint and passes.
  • Removing any moved site back into a safe core fails ./lint (shown by the RED commit).
  • ./unittest passes. Windows and macOS CI legs pass.

Activity

  1. zackees commented on Oct 12, 2026

    @zackees
    MemberAuthor

    Claiming: #323. Plan: 1) RED commit adds the Dylint + lint-lane wiring; 2) GREEN commits move obs-util then obs-media-io behind src/platform; 3) docs. One PR, opening today.

  2. added
    claimed
    on Oct 12, 2026
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

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions