docs(builder): unsafe is allowed where the builder calls the system itself, each block with its SAFETY #3250

Merged
HumanBean merged 2 commits from llun/Longshot:docs/builder-unsafe-rule into main 2026-12-05 17:05:08 +00:00
Member

Summary

The written rule for unsafe in the builder doesn't match main. docs/dev/standards.md says it is allowed only in
build-core/src/sys.rs's Windows module and game-files/src/disk.rs; docs/dev/reference/builder.md names three
modules; the lint's comment in Cargo.toml says the same. On main, 31 files under builder/crates have
#[allow(unsafe_code)]: the sandboxes and the game's file service, pads.rs (SDL3), switch2.rs (WinUSB and HID),
game_log.rs, crash_block.rs, the control channel, the network broker's sockets and others. 398 of the 410
unsafe blocks there have a // SAFETY: comment right above them; the other 12 sit under one comment that covers a
short run of plain calls.

The stale sentence blocks reviews of code that follows the existing pattern: BeanBot's L3 on
PR 3245 (the GameCube adapter driver, modelled on
switch2.rs's mod os) asks for the rule to be changed "through the appropriate decision" or the code moved into
sys.rs. This PR is that decision, for a maintainer to take or turn down.

It rewrites the rule as the tree keeps it: the workspace denies unsafe, but a module or function that calls the
system or a C library itself (Win32, POSIX, WinUSB, SDL3, Vulkan's loader) allows it, and each unsafe block has a
// SAFETY: comment saying why it holds (one may cover a short run of plain calls). Everything else is safe Rust.
builder.md lists the main places; Cargo.toml's comment points at standards.md.

Read: docs/dev/standards.md ("The builder is Rust"), then docs/dev/reference/builder.md.

Testing

Tested: docs and a comment only; nothing the game or the builder runs changes. mise run test -- --changed origin/main on Windows: 14 passed, 0 failed. The counts above come from git grep over origin/main's
builder/crates (7bc53f841).

Still to test:
None.

Measurements

Nothing to measure: documentation and a Cargo.toml comment only.

Risks

A maintainer may want a tighter rule than the tree has (for example a list of allowed modules kept in a check). Then
this PR is closed, and PR 3245's driver moves its calls to sys.rs instead. Nothing changes for players.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • Docs updated.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

## Summary The written rule for `unsafe` in the builder doesn't match `main`. `docs/dev/standards.md` says it is allowed only in `build-core/src/sys.rs`'s Windows module and `game-files/src/disk.rs`; `docs/dev/reference/builder.md` names three modules; the lint's comment in `Cargo.toml` says the same. On `main`, 31 files under `builder/crates` have `#[allow(unsafe_code)]`: the sandboxes and the game's file service, `pads.rs` (SDL3), `switch2.rs` (WinUSB and HID), `game_log.rs`, `crash_block.rs`, the control channel, the network broker's sockets and others. 398 of the 410 `unsafe` blocks there have a `// SAFETY:` comment right above them; the other 12 sit under one comment that covers a short run of plain calls. The stale sentence blocks reviews of code that follows the existing pattern: BeanBot's L3 on [PR 3245](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/3245) (the GameCube adapter driver, modelled on `switch2.rs`'s `mod os`) asks for the rule to be changed "through the appropriate decision" or the code moved into `sys.rs`. This PR is that decision, for a maintainer to take or turn down. It rewrites the rule as the tree keeps it: the workspace denies `unsafe`, but a module or function that calls the system or a C library itself (Win32, POSIX, WinUSB, SDL3, Vulkan's loader) allows it, and each `unsafe` block has a `// SAFETY:` comment saying why it holds (one may cover a short run of plain calls). Everything else is safe Rust. `builder.md` lists the main places; `Cargo.toml`'s comment points at `standards.md`. Read: `docs/dev/standards.md` ("The builder is Rust"), then `docs/dev/reference/builder.md`. ## Testing **Tested:** docs and a comment only; nothing the game or the builder runs changes. `mise run test -- --changed origin/main` on Windows: 14 passed, 0 failed. The counts above come from `git grep` over `origin/main`'s `builder/crates` (7bc53f841). **Still to test:** None. ## Measurements Nothing to measure: documentation and a `Cargo.toml` comment only. ## Risks A maintainer may want a tighter rule than the tree has (for example a list of allowed modules kept in a check). Then this PR is closed, and PR 3245's driver moves its calls to `sys.rs` instead. Nothing changes for players. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [x] `mise run test` passes (any failure that also fails on `main` is named). - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] Docs updated. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
docs(builder): unsafe is allowed where the builder calls the system itself, each block with its SAFETY
Some checks failed
PR description / Title and description (pull_request_target) Has been cancelled
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
3836ff36a8
The rule in standards.md (and builder.md, and Cargo.toml's lint comment) said
unsafe was allowed only in sys.rs's Windows module and disk.rs (builder.md: three
modules). main has 31 files with #[allow(unsafe_code)] (the sandboxes, the file
service, pads.rs's SDL3, switch2.rs's WinUSB, the game's log, the crash record,
the network broker), and 398 of 410 unsafe blocks have a SAFETY comment right
above them, the rest one above their short run of calls. The rule now says what
the tree does: a module or function that calls the system or a C library itself
allows it, each unsafe block saying why it holds; everything else is safe Rust.

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Member
@review
BeanBot left a comment

Recommendation: Approve. The rule better describes most existing builder code, but it still excludes legitimate existing unsafe operations. CI is also still running. Nothing blocking is left. Recommend approval; not approved on the forge.

The change broadens the documented unsafe policy and requires safety comments; it changes no executable behavior. The remaining gap can still lead reviewers to reject code following an existing builder pattern.

Should fix (1): not blocking, but worth doing before merging.

  • L1 docs/dev/standards.md:48: The new exception still excludes existing unsafe operations that do not themselves call the system or a C library. (more on the line) Do: Extend the rule and the corresponding reference and Cargo comment to cover necessary unsafe Rust operations at system-resource boundaries, with their safety invariants documented.

@fix can do L1.

Checks: Still running: 11 checks.

More: does it help, other systems, switches, what was read

Does it help: Not a perf change. The documented policy covers the listed system and C-library calls, but does not cover every existing unsafe operation.

Other systems: No game, builder, platform, or runtime behavior changes. The rule guides future builder reviews on Windows and Linux.

Switches: None.

Reviewed: 3836ff36a8 against 7bc53f8413: Read AGENTS.md, CONTRIBUTING.md, the pull request template, the diff and commit metadata, Cargo.toml, and docs/dev/standards.md. Skimmed docs/dev/reference/builder.md and searched the builder's unsafe allowances and representative call sites. docs/review-checklist.md and the adjacent Tooling checkout were absent. No recorded runs, translations, or generated files changed.; nothing run (a code review).

Checked and fine:

  • The commit records AI assistance in trailers.
  • The diff contains no game-derived files, generated outputs, API changes, or third-party changes.
  • The PR description identifies the policy choice and reports a Windows check run.

Testing: No commands from the project or tests were run, as requested. The author reports 14 checks passed on Windows; the listed CI checks remain running.

Overlaps and merge order: PR 3245 is referenced as a consumer of this policy; no overlapping PR was established from the available information.


Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.

**Recommendation: Approve.** The rule better describes most existing builder code, but it still excludes legitimate existing unsafe operations. CI is also still running. Nothing blocking is left. Recommend approval; not approved on the forge. The change broadens the documented unsafe policy and requires safety comments; it changes no executable behavior. The remaining gap can still lead reviewers to reject code following an existing builder pattern. **Should fix (1)**: not blocking, but worth doing before merging. - **L1** `docs/dev/standards.md:48`: The new exception still excludes existing unsafe operations that do not themselves call the system or a C library. (more on the line) **Do:** Extend the rule and the corresponding reference and Cargo comment to cover necessary unsafe Rust operations at system-resource boundaries, with their safety invariants documented. `@fix` can do L1. **Checks:** Still running: 11 checks. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Not a perf change. The documented policy covers the listed system and C-library calls, but does not cover every existing unsafe operation. **Other systems:** No game, builder, platform, or runtime behavior changes. The rule guides future builder reviews on Windows and Linux. **Switches:** None. **Reviewed:** 3836ff36a8e0 against 7bc53f841323: Read AGENTS.md, CONTRIBUTING.md, the pull request template, the diff and commit metadata, Cargo.toml, and docs/dev/standards.md. Skimmed docs/dev/reference/builder.md and searched the builder's unsafe allowances and representative call sites. docs/review-checklist.md and the adjacent Tooling checkout were absent. No recorded runs, translations, or generated files changed.; nothing run (a code review). **Checked and fine:** - The commit records AI assistance in trailers. - The diff contains no game-derived files, generated outputs, API changes, or third-party changes. - The PR description identifies the policy choice and reports a Windows check run. **Testing:** No commands from the project or tests were run, as requested. The author reports 14 checks passed on Windows; the listed CI checks remain running. **Overlaps and merge order:** PR 3245 is referenced as a consumer of this policy; no overlapping PR was established from the available information. </details> --- <sub>Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.</sub>
@ -48,3 +48,1 @@
- **The builder is Rust, with few dependencies.** The workspace in `builder/` denies `unsafe` (allowed only in
`build-core/src/sys.rs`'s Windows module and `game-files/src/disk.rs`'s system calls) and asks for docs on every
public item; clippy runs with warnings as errors, and rustfmt checks the layout (`tests/test_lifter.py`). A new crate
- **The builder is Rust, with few dependencies.** The workspace in `builder/` denies `unsafe`, but where code calls the
Member

L1 (should fix): The new exception still excludes existing unsafe operations that do not themselves call the system or a C library.

When: For example, build-driver's map_file calls the Rust memmap2 API, and its channel code uses BorrowedFd::borrow_raw; neither operation calls the system or a C library itself, so the new rule's 'Everything else is safe Rust' disallows these existing patterns.

Do: Extend the rule and the corresponding reference and Cargo comment to cover necessary unsafe Rust operations at system-resource boundaries, with their safety invariants documented.

**L1** (should fix): The new exception still excludes existing unsafe operations that do not themselves call the system or a C library. *When:* For example, build-driver's map_file calls the Rust memmap2 API, and its channel code uses BorrowedFd::borrow_raw; neither operation calls the system or a C library itself, so the new rule's 'Everything else is safe Rust' disallows these existing patterns. **Do:** Extend the rule and the corresponding reference and Cargo comment to cover necessary unsafe Rust operations at system-resource boundaries, with their safety invariants documented.
docs(builder): the rule covers Rust's own unsafe at the system's edge too
Some checks failed
PR description / Title and description (pull_request_target) Successful in 8s
CI / What the change needs (pull_request) Successful in 8s
CI / Third-party parts and guide screenshots (pull_request) Successful in 13s
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Successful in 15s
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Failing after 13m21s
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Failing after 1m18s
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Successful in 10m21s
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Successful in 10m9s
CI / Website build and checks (pull_request) Failing after 37s
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Failing after 47m22s
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Failing after 1h0m7s
0cb2985b4e
Review (BeanBot, L1): "calls the system or a C library itself" left out unsafe
operations main already has at the same boundary that call nothing themselves:
a raw handle or descriptor taken as owned or borrowed (BorrowedFd::borrow_raw,
from_raw_handle) and a file mapped into memory (memmap2). The rule now names them.

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Member

Pushed 0cb2985b4 for L1: the rule now covers Rust's own unsafe operations on what the system hands out (a raw handle or descriptor taken as owned or borrowed, a file mapped into memory), as build-driver's memmap2 mapping and channel's BorrowedFd::borrow_raw do, in standards.md, builder.md and Cargo.toml's comment. The changed checks pass (mise run test -- --changed origin/main).

Pushed 0cb2985b4 for L1: the rule now covers Rust's own unsafe operations on what the system hands out (a raw handle or descriptor taken as owned or borrowed, a file mapped into memory), as build-driver's memmap2 mapping and channel's BorrowedFd::borrow_raw do, in standards.md, builder.md and Cargo.toml's comment. The changed checks pass (mise run test -- --changed origin/main).
Author
Member
@review
BeanBot left a comment

Recommendation: Approve. The revised rule covers the existing raw-descriptor and file-mapping operations named by L1. It changes documentation and a manifest comment without changing player-facing behavior. Recommend approval; not approved on the forge.

The change aligns the builder’s written unsafe-code rule with its existing system-boundary practices. It clarifies where unsafe code is allowed and requires safety comments, without changing executable code.

Since the last review (on 3836ff36a8)
Resolved: L1 (The new exception still excludes existing unsafe operations that do not themselves call th…)

Checks: Still running: 11 checks.

More: does it help, other systems, switches, what was read

Does it help: Not a perf change. The revised text addresses the documented mismatch; no performance measurements are needed.

Other systems: The changed files do not alter Windows, Linux, the builder, or the game at runtime.

Switches: None.

Reviewed: 0cb2985b4e against 7bc53f8413: Read AGENTS.md, CONTRIBUTING.md, the pull request template, both diffs, commit list, changed passages, and the relevant file-mapping and descriptor-borrowing code. The rest of Cargo.toml, standards.md, and builder.md was not read in full. docs/review-checklist.md and ../Tooling/AGENTS.md were unavailable.; nothing run (a code review).

Checked and fine:

  • The exception retains the workspace unsafe-code denial and the requirement for SAFETY comments.
  • The fix does not remove the pull request’s intended documentation change.

Testing: No commands from the project were run. The author reports changed checks passing; CI is still running.

Overlaps and merge order: None found in the supplied context.


Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.

**Recommendation: Approve.** The revised rule covers the existing raw-descriptor and file-mapping operations named by L1. It changes documentation and a manifest comment without changing player-facing behavior. Recommend approval; not approved on the forge. The change aligns the builder’s written unsafe-code rule with its existing system-boundary practices. It clarifies where unsafe code is allowed and requires safety comments, without changing executable code. **Since the last review** (on 3836ff36a8e0) Resolved: L1 (The new exception still excludes existing unsafe operations that do not themselves call th…) **Checks:** Still running: 11 checks. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Not a perf change. The revised text addresses the documented mismatch; no performance measurements are needed. **Other systems:** The changed files do not alter Windows, Linux, the builder, or the game at runtime. **Switches:** None. **Reviewed:** 0cb2985b4ec0 against 7bc53f841323: Read AGENTS.md, CONTRIBUTING.md, the pull request template, both diffs, commit list, changed passages, and the relevant file-mapping and descriptor-borrowing code. The rest of Cargo.toml, standards.md, and builder.md was not read in full. docs/review-checklist.md and ../Tooling/AGENTS.md were unavailable.; nothing run (a code review). **Checked and fine:** - The exception retains the workspace unsafe-code denial and the requirement for SAFETY comments. - The fix does not remove the pull request’s intended documentation change. **Testing:** No commands from the project were run. The author reports changed checks passing; CI is still running. **Overlaps and merge order:** None found in the supplied context. </details> --- <sub>Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.</sub>
HumanBean deleted branch docs/builder-unsafe-rule 2026-12-05 17:05:08 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!3250
No description provided.