[pr-refactor] SAFETY comments on the remaining unsafe blocks in darner-core and darner-loader #314

Open
ElGibbone wants to merge 2 commits from elgibbone/safety-comments-non-gpu into main AGit
First-time contributor

What it changes

  • The problem on main: read from the code only. Five unsafe blocks in darner-core and darner-loader had no // SAFETY: comment.

Adds the five missing // SAFETY: comments (darner-core folders.rs; darner-loader callhook.rs tests, console_insn.rs, heap_sites.rs) and turns on #![warn(clippy::undocumented_unsafe_blocks)] in darner-core and darner-loader, so a new unsafe block without one is reported. No code changes behaviour: console_insn.rs and heap_sites.rs only move a comment next to its block (one else if became else { if } for that).

The first version also put a copy of main's comment on the remove_var in darner-shader/src/emit/wave64_tests.rs. As lewis499 found, that comment claims every reader of DARNER_THREAD_TICKET holds ONE_AT_A_TIME, which is not true, so 8ed8039 takes it out again: this PR no longer touches darner-shader. The race itself is on main and has its own PR, #386.

How it was tested

  • Tested revision: 8ed8039
  • Commands and results (Windows 11 Home 26300, Ryzen 7 7800X3D, RTX 4070 Ti driver 621.47, Rust 1.98.1 MSVC):
    • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
    • cargo xtask policy: 555 files ok
    • cargo test --workspace --locked --no-fail-fast: darner-core 31 passed; darner-loader 92 passed, plus its integration tests (load_probe, title_dlls) passed; darner-hle, darner-formats, darner-game and the darner app passed.
    • darner-gpu: 1771 passed, 2 failed, 81 ignored (host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end, watch::tests::ask_once_by_res_splits_reservations_without_refusals); in an earlier run a third, watch::watch_pipe_epoch_tests::ask_once_delta_reask_direct_carves_refreshes_and_updates_page_epochs, failed instead. The watch_pipe_epoch test passes run alone (5 of 5 on this branch, 5 of 5 on main); I did not rerun the other two alone. They look like tests that fail under the load of the parallel run. This PR does not touch darner-gpu.
    • darner-shader: 573 of 574 finished before the run was stopped (oracle64::legacy_mul_forms_are_exact was still running after 15 minutes in a debug build). One failure, emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders ("DXIL validation failed: Not all elements of output SV_Depth were written"), fails the same way on main (f3840d7, 3 of 3); this PR no longer touches darner-shader. emit::wave64_tests passed.
  • Changes a default: no
  • Gates run on that revision: the two clippy gates and cargo xtask policy in full; cargo test --workspace with the gaps above.
  • Gameplay: not relevant (comments and a lint only).
  • Hardware dependence: none.
  • Coverage gaps: darner-shader's oracle64::legacy_mul_forms_are_exact and the crates and doc tests after darner-shader did not finish.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only). Not run in the game: comments and a lint only, no behaviour change.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #285 (a SAFETY comment in darner-gpu, a different file). #95 and #276 change darner-loader/src/lib.rs, #293 changes darner-core/src/folders.rs; they merge with this branch without a conflict (git merge-tree). #386 fixes the DARNER_THREAD_TICKET race that the review found.
  • Replaces or overlaps: none found
## What it changes - The problem on main: read from the code only. Five unsafe blocks in darner-core and darner-loader had no `// SAFETY:` comment. Adds the five missing `// SAFETY:` comments (darner-core folders.rs; darner-loader callhook.rs tests, console_insn.rs, heap_sites.rs) and turns on `#![warn(clippy::undocumented_unsafe_blocks)]` in darner-core and darner-loader, so a new unsafe block without one is reported. No code changes behaviour: console_insn.rs and heap_sites.rs only move a comment next to its block (one `else if` became `else { if }` for that). The first version also put a copy of `main`'s comment on the `remove_var` in `darner-shader/src/emit/wave64_tests.rs`. As lewis499 found, that comment claims every reader of `DARNER_THREAD_TICKET` holds `ONE_AT_A_TIME`, which is not true, so `8ed8039` takes it out again: this PR no longer touches darner-shader. The race itself is on `main` and has its own PR, #386. ## How it was tested - Tested revision: 8ed8039 - Commands and results (Windows 11 Home 26300, Ryzen 7 7800X3D, RTX 4070 Ti driver 621.47, Rust 1.98.1 MSVC): - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo xtask policy`: 555 files ok - `cargo test --workspace --locked --no-fail-fast`: darner-core 31 passed; darner-loader 92 passed, plus its integration tests (load_probe, title_dlls) passed; darner-hle, darner-formats, darner-game and the darner app passed. - darner-gpu: 1771 passed, 2 failed, 81 ignored (`host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end`, `watch::tests::ask_once_by_res_splits_reservations_without_refusals`); in an earlier run a third, `watch::watch_pipe_epoch_tests::ask_once_delta_reask_direct_carves_refreshes_and_updates_page_epochs`, failed instead. The watch_pipe_epoch test passes run alone (5 of 5 on this branch, 5 of 5 on `main`); I did not rerun the other two alone. They look like tests that fail under the load of the parallel run. This PR does not touch darner-gpu. - darner-shader: 573 of 574 finished before the run was stopped (`oracle64::legacy_mul_forms_are_exact` was still running after 15 minutes in a debug build). One failure, `emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders` ("DXIL validation failed: Not all elements of output SV_Depth were written"), fails the same way on `main` (`f3840d7`, 3 of 3); this PR no longer touches darner-shader. `emit::wave64_tests` passed. - Changes a default: no - Gates run on that revision: the two clippy gates and `cargo xtask policy` in full; `cargo test --workspace` with the gaps above. - Gameplay: not relevant (comments and a lint only). - Hardware dependence: none. - Coverage gaps: darner-shader's `oracle64::legacy_mul_forms_are_exact` and the crates and doc tests after darner-shader did not finish. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (docs, tests only). Not run in the game: comments and a lint only, no behaviour change. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #285 (a SAFETY comment in darner-gpu, a different file). #95 and #276 change darner-loader/src/lib.rs, #293 changes darner-core/src/folders.rs; they merge with this branch without a conflict (git merge-tree). #386 fixes the `DARNER_THREAD_TICKET` race that the review found. - Replaces or overlaps: none found
Adds the six missing // SAFETY: comments across darner-core, darner-loader and darner-shader tests, and enables clippy::undocumented_unsafe_blocks in darner-core and darner-loader while leaving darner-shader/src/lib.rs untouched so DARNER_TRANSLATOR does not change.
ElGibbone changed title from Document remaining unsafe blocks in core, loader and shader to [pr-refactor] SAFETY comments on the remaining unsafe blocks in darner-core, darner-loader and a shader test 2026-12-04 14:41:50 +00:00
First-time contributor

Independent Windows build, gate, and code-read review of 66df156078b6 (full head 66df156078b6f85ac153e3e9ed60755bfebc809f), based on main f3840d73d3504c30218debfbc8795b9225447317. Not run in-game.

Environment

  • Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (x86_64-pc-windows-msvc), MSVC 19.44.35230.

Measured results

  • Gate 1 (cargo clippy --workspace --all-targets --locked -- -D warnings): PASS.
  • Gate 2 (cargo clippy --workspace --all-targets --all-features --locked -- -D warnings): PASS.
  • cargo xtask policy: PASS (asset policy: 555 files ok).
  • cargo test --locked -p darner-core: PASS (31 passed; 0 failed; 0 ignored).
  • cargo test --locked -p darner-loader --lib: PASS (92 passed; 0 failed; 0 ignored).
  • cargo test --locked -p darner-shader --lib emit::wave64_tests::the_console_threads_of_a_two_dimensional_group_are_each_given_once: PASS (1 passed; 0 failed; 0 ignored; 573 filtered out).
  • cargo test --locked -p darner-shader --lib emit::wave64_tests: PASS (48 passed; 0 failed; 1 ignored; 525 filtered out).
  • Full workspace test gate (cargo test --workspace --locked) was not run; focused affected-crate tests were run instead.

Code-read audit

  • The added crate-level clippy::undocumented_unsafe_blocks lints are active in the intended core and loader crates, and both workspace Clippy gates pass. The folder setup comment matches apply's documented before-threads precondition (crates/darner-core/src/folders.rs:73-94); the loader comments were checked against the generated Win64 call/entry test routines, the owned line passed to zero_through_fault, and the protected code/table writes (crates/darner-loader/src/callhook.rs:289-326, console_insn.rs:651-667, heap_sites.rs:1606-1624).
  • Changes needed: The new environment-mutation safety comment says every test reading DARNER_THREAD_TICKET holds ONE_AT_A_TIME (crates/darner-shader/src/emit/wave64_tests.rs:1540-1546), but lanes_past_32_take_the_high_dword_of_a_mask_held_as_a_number lowers a 64-thread wave64 program at host width 32 without acquiring that lock (wave64_tests.rs:773-775). That lowering can reach ticket_on() when group > wave (crates/darner-shader/src/emit/lower.rs:1653-1656), so it can observe the temporary "0" set by lower_with(false) while the other test is compiling. The full wave64 test run passed here, but the stated synchronization invariant does not hold and the output remains schedule-dependent. Synchronize every translation path that can read the switch or replace the process-environment toggle with a test-only override.

Verdict: CHANGES NEEDED.

AI-assisted: yes (GitHub Copilot, Auto mode).

Independent Windows build, gate, and code-read review of `66df156078b6` (full head `66df156078b6f85ac153e3e9ed60755bfebc809f`), based on `main` `f3840d73d3504c30218debfbc8795b9225447317`. Not run in-game. ### Environment - Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (`x86_64-pc-windows-msvc`), MSVC 19.44.35230. ### Measured results - Gate 1 (`cargo clippy --workspace --all-targets --locked -- -D warnings`): **PASS**. - Gate 2 (`cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`): **PASS**. - `cargo xtask policy`: **PASS** (`asset policy: 555 files ok`). - `cargo test --locked -p darner-core`: **PASS** (`31 passed; 0 failed; 0 ignored`). - `cargo test --locked -p darner-loader --lib`: **PASS** (`92 passed; 0 failed; 0 ignored`). - `cargo test --locked -p darner-shader --lib emit::wave64_tests::the_console_threads_of_a_two_dimensional_group_are_each_given_once`: **PASS** (`1 passed; 0 failed; 0 ignored; 573 filtered out`). - `cargo test --locked -p darner-shader --lib emit::wave64_tests`: **PASS** (`48 passed; 0 failed; 1 ignored; 525 filtered out`). - Full workspace test gate (`cargo test --workspace --locked`) was not run; focused affected-crate tests were run instead. ### Code-read audit - The added crate-level `clippy::undocumented_unsafe_blocks` lints are active in the intended core and loader crates, and both workspace Clippy gates pass. The folder setup comment matches `apply`'s documented before-threads precondition (`crates/darner-core/src/folders.rs:73-94`); the loader comments were checked against the generated Win64 call/entry test routines, the owned line passed to `zero_through_fault`, and the protected code/table writes (`crates/darner-loader/src/callhook.rs:289-326`, `console_insn.rs:651-667`, `heap_sites.rs:1606-1624`). - **Changes needed:** The new environment-mutation safety comment says every test reading `DARNER_THREAD_TICKET` holds `ONE_AT_A_TIME` (`crates/darner-shader/src/emit/wave64_tests.rs:1540-1546`), but `lanes_past_32_take_the_high_dword_of_a_mask_held_as_a_number` lowers a 64-thread wave64 program at host width 32 without acquiring that lock (`wave64_tests.rs:773-775`). That lowering can reach `ticket_on()` when `group > wave` (`crates/darner-shader/src/emit/lower.rs:1653-1656`), so it can observe the temporary `"0"` set by `lower_with(false)` while the other test is compiling. The full wave64 test run passed here, but the stated synchronization invariant does not hold and the output remains schedule-dependent. Synchronize every translation path that can read the switch or replace the process-environment toggle with a test-only override. **Verdict: CHANGES NEEDED.** AI-assisted: yes (GitHub Copilot, Auto mode).
The comment it copied says every test that reads DARNER_THREAD_TICKET holds
ONE_AT_A_TIME, which is not true: folded_tests'
two_dimensional_groups_of_several_host_waves_give_every_console_thread_once
reaches ticket_on() without the lock. This pull request should not repeat that
claim; the race itself is left to a change of its own. The comment on set_var
above is main's and stays as it is.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
First-time contributor

Thanks @lewis499, you're right: the comment claimed an invariant that does not hold, so it is gone. Pushed 8ed8039, which drops the copied // SAFETY: line on remove_var in wave64_tests.rs. With it this PR no longer touches darner-shader at all; the comment on set_var just above is main's and stays as it is.

One correction on the reader: lanes_past_32_take_the_high_dword_of_a_mask_held_as_a_number lowers a wave64 program at host width 32, so lower takes the wave == 32 && x.native.wave64 branch (crates/darner-shader/src/emit/lower.rs:1641) and never calls ticket_on(). The test that does reach it without the lock is folded_tests::two_dimensional_groups_of_several_host_waves_give_every_console_thread_once (a wave32 program in a 32 x 2 group, lower.rs:1653-1656). That race is on main already and is not this PR's change, so it has its own PR: #386.

Windows results on 8ed8039: both clippy gates clean, cargo xtask policy 555 files ok, darner-core and darner-loader tests pass. The workspace run has failures in darner-gpu, which this PR does not touch (the one I reran alone passed 5 of 5) and one in darner-shader that fails the same way on main; the details are in the updated description.

Thanks @lewis499, you're right: the comment claimed an invariant that does not hold, so it is gone. Pushed `8ed8039`, which drops the copied `// SAFETY:` line on `remove_var` in `wave64_tests.rs`. With it this PR no longer touches darner-shader at all; the comment on `set_var` just above is `main`'s and stays as it is. One correction on the reader: `lanes_past_32_take_the_high_dword_of_a_mask_held_as_a_number` lowers a wave64 program at host width 32, so `lower` takes the `wave == 32 && x.native.wave64` branch (`crates/darner-shader/src/emit/lower.rs:1641`) and never calls `ticket_on()`. The test that does reach it without the lock is `folded_tests::two_dimensional_groups_of_several_host_waves_give_every_console_thread_once` (a wave32 program in a 32 x 2 group, `lower.rs:1653-1656`). That race is on `main` already and is not this PR's change, so it has its own PR: #386. Windows results on `8ed8039`: both clippy gates clean, `cargo xtask policy` 555 files ok, darner-core and darner-loader tests pass. The workspace run has failures in darner-gpu, which this PR does not touch (the one I reran alone passed 5 of 5) and one in darner-shader that fails the same way on `main`; the details are in the updated description.
ElGibbone changed title from [pr-refactor] SAFETY comments on the remaining unsafe blocks in darner-core, darner-loader and a shader test to [pr-refactor] SAFETY comments on the remaining unsafe blocks in darner-core and darner-loader 2026-12-04 18:42:27 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/314/head:elgibbone/safety-comments-non-gpu'
git switch 'elgibbone/safety-comments-non-gpu'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'elgibbone/safety-comments-non-gpu'
git switch 'elgibbone/safety-comments-non-gpu'
git rebase 'main'
git switch 'main'
git merge --ff-only 'elgibbone/safety-comments-non-gpu'
git switch 'elgibbone/safety-comments-non-gpu'
git rebase 'main'
git switch 'main'
git merge --no-ff 'elgibbone/safety-comments-non-gpu'
git switch 'main'
git merge --squash 'elgibbone/safety-comments-non-gpu'
git switch 'main'
git merge --ff-only 'elgibbone/safety-comments-non-gpu'
git switch 'main'
git merge 'elgibbone/safety-comments-non-gpu'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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
TrustNo1/Darner!314
No description provided.