Add a build gate for memory and unsafe rules, wired into CI #1456
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1466"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Krigsexe on 2026-11-24T16:32:42Z.
What is in this PR
A small standalone crate,
crates/xcl-gate, with zero dependencies, plus one CI step that runs itbefore
Build and test. Nothing else in the emulator changes: no emulator source file is touched.The gate scans every
.rsfile andCargo.tomland flags patterns called out by the Rustdocumentation and tooling (
std,clippy, Cargo,rustup). For each failing rule, it prints thereason, the source of the rule, how to fix it, and the offending lines.
unsafe-implunsafe implwithout a// SAFETY:commentunsafe-fn-docunsafe fnwithout a# Safetysectionmissing_safety_docunsafe-blockunsafe { }without a// SAFETY:commentundocumented_unsafe_blockspanic-sourceunwrap()/expect()(a poisoned lock panics every subsequent caller)unwrap_used,expect_usedmem-leakmem::forget,Box::leak,.leak()mem_forgetmem-uninitmem::uninitializedstddocumentationunbounded-allocationvec![0; n]/with_capacity(n)with an unbounded sizeVec::try_reservehardcoded-memoryconfig.rs--config-file)cargo-lock,cargo-profile,toolchain-channelCargo.lock, overridden dev profile checks, nightly or beta pinThe gate hardcodes no paths from this repository:
--config-fileand--skipare passed asarguments, so it can be used on any Rust codebase.
Gradual adoption without blocking existing code
scripts/gate-limits.txtrecords the current baseline. The build only fails when a count goesabove its recorded limit, so existing code is not blocked while new violations are caught.
Limits are lowered as findings are fixed, and never raised. Current baseline on
b7aa9f3:Before scanning, the gate fetches the remote and fast-forwards a clean checkout, failing if it
cannot so it never evaluates a stale tree.
--no-syncis available for offline runs.Preventing changes from editing the gate
On pull requests, CI restores
crates/xcl-gatefrom the target branch before running it. The gateitself rejects any PR that modifies the gate, the workflow, or
CODEOWNERS, or that raises alimit, adds a limit line, or deletes the limits file.
.github/CODEOWNERSassigns those paths to asingle owner.
The final safeguard is a GitHub repository setting that a file cannot enable on its own: Require
review from Code Owners must be turned on in branch protection for the default branch. Until then,
the first two layers act as a check rather than a hard lock.
Background
This gate acts as a guardrail against drift from AI-generated contributions, which now make up a
large share of incoming code. Generated code often looks plausible while skipping the unglamorous
rules: bounded allocations, avoiding panicking
unwraps, and documenting everyunsafe. The gateenforces a fixed, human-defined set of rules drawn from official Rust documentation, applied
uniformly regardless of who or what wrote the code.
Why this is worth merging
The repository currently has 726 open pull requests and 155 open issues. Reviewers cannot manually
audit every unchecked allocation,
unwrap, or undocumentedunsafeacross all of them. Yet thoseare the exact details that determine whether shaders compile reliably, whether memory stays under
control, and whether
unsaferemains contained and justified. Left unchecked, they accumulate andmake the codebase harder to trust with each merge.
With this gate in place, every open and future pull request is checked automatically. A PR that
introduces a violation fails CI immediately with the rule, the reason, the reference, and the exact
line. Contributors get feedback when they push rather than weeks later in review, and review time
goes toward moving the project forward instead of catching recurring debt. Because limits start at
today's counts and can only move down, nothing currently in the tree is blocked.
This is offered in good faith as a shared baseline to lighten review for maintainers and raise the
bar for everyone.
Tested
cargo build --releaseon Linux and Windows 10.0.26100 (Rust stable, MSVC BuildTools).
b7aa9f3(the counts above).0with those counts as limits; adding a single unboundedvec![0u8; n]makes it exit1with the rule, reason, source, fix, and line number.
0for thePR that introduces the gate and for a harmless later PR, and exits
1withgate-tamperfor alater PR that edits the gate or raises a limit.
Not tested
not distinguish test code from production code. Type-aware checks remain clippy's job
AI use
Used only to polish this PR description and readme; all code was written by @krigsexe and verify with double check by Opus 5.5.
Imported from ivall/xcl. Originally posted by Krigsexe on 2026-11-24T16:37:17Z.
Review: COMMENTED
Triple check done
Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T16:57:04Z.
Full-suite run, since this PR has no test report yet. Tested exact head
9dd0d16(on its baseb7aa9f3).Tested
cargo build --workspace, thenXCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 770 passed, 0 failed, 41 ignored. SKIPPED (no wave64):gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware.#[test]functions in the diff.XCL_ADAPTER=radeon),cargo test --workspace -- --test-threads=4: 770 passed, 0 failed, 41 ignored, no skips.The gate itself (
crates/xcl-gateis not a workspace member, so the suite above doesn't build or test it)cargo run --release --manifest-path crates/xcl-gate/Cargo.toml -- --root . --config-file crates/xcl/src/config.rs --skip crates/xcl-gate --no-sync --limits scripts/gate-limits.txt, which gave4238 finding(s),OK.mainis still at this PR's baseb7aa9f3, so that's also today's count.scripts/gate-limits.txtadds up to exactly 4238, so every rule sits at its limit with nothing to spare. The scan also counts#[cfg(test)]code. For example,config.rs:295, inside the test module, is listed underpanic-source.unwrap()in a test fails the build. I applied three other open PRs from today on top of this one (crates/diffs only) and ran the gate again. All three fail:panic-source: 2748 found, 2734 allowed,unsafe-block: 814 found, 812 allowedpanic-source: 2753 found, 2734 allowed,unsafe-block: 814 found, 812 allowedmem-leak: 37 found, 36 allowed(oneBox::leak),panic-source: 2756 found, 2734 allowedpanic-source(clippy'sunwrap_usedhasallow-unwrap-in-testsfor this), or adding some headroom, would stop most open PRs from failing CI once this merges.Not tested
AI use
Imported from ivall/xcl. Originally posted by Krigsexe on 2026-11-24T17:52:48Z.
Review: COMMENTED
@futurepro62
Thanks for testing the gate itself and running it against real open PRs, that's a very useful catch.
I agree on
panic-source:.unwrap()/.expect()in#[cfg(test)]/#[test]blocks andtests/files is idiomatic in Rust and shouldn't block test-only PRs. I've updatedrules.rsto skippanic-sourcein test code (similar to clippy'sallow-unwrap-in-tests) and regeneratedscripts/gate-limits.txtaccordingly.I intentionally kept
unsafe-blockandmem-leakactive everywhere, including tests, undocumentedunsafeorBox::leakshould still be justified or avoided.krigs
Imported from ivall/xcl. Originally posted by Krigsexe on 2026-11-24T18:11:35Z.
Review: COMMENTED
Final global check done with human review
upstream-sync referenced this pull request2026-11-24 19:06:45 +00:00
Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T18:17:25Z.
Retest at
57eb1af. Same command as CI (--no-sync --limits scripts/gate-limits.txt), Windows 11, Rust 1.100.0. Each PR was merged locally onto the #1466 head; nothing pushed.57eb1aff7b6286)84e7d21)8052b9c)mem-leak: 37 found, 36 allowedpanic-sourceand onunsafe-block(814 against 812). The first is the test-code exemption; the second is because the regeneratedunsafe-blocklimit is now 820.Box::leak, andmem-leakstays on in tests.AI use
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T18:53:03Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodifiedmain.)Thanks!
Imported from ivall/xcl. Originally posted by Krigsexe on 2026-11-25T16:21:59Z.
@ivall @throny @DavidM1911 @futurepro62
Hi guys !
Quick note on the
off-goallabel. I get that world loading is the priority right now, and I'm totally fine with this waiting if all CI/tooling PRs are held until then.I just wanted to explain why I opened this now rather than later. I know this gate is imperfect. It was built quickly with simple pattern matching and could definitely be cleaner with a proper lexer. The goal isn't cleanup for cleanup's sake.
With so many LLM-assisted PRs coming in, an agent's only goal is to make the build pass on the contributor's own machine. Without a CI check, agents keep hardcoding memory limits, adding unbounded allocations, or applying local workarounds that introduce new issues on other hardware. That ends up causing OOMs, slowing down shader compilation, and creating more brute-force fixes down the road.
Putting a baseline gate in CI creates a shared feedback loop: it aligns everyone's contributors and agents around the same project-wide rules, telling them right away what needs to be fixed or routed through config/runtime limits, without blocking any existing code.
Happy to leave this on hold until after world loading if that's the call. Just wanted to make the intent clear.
Just my engineering POV on pooling everyone's efforts.
Krigs
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T05:27:33Z.
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.Thanks!
— Devin
Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:32:32Z.
Nothing has happened on this PR for 3 days, so it is marked
staleand will be closed in 4 days. A push or a comment takes the mark off: rebase it ontomainif it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:51:42Z.
Triage (#1607) – head
57eb1af.github/CODEOWNERSassigns/.github/workflows/,/.github/CODEOWNERS, the gate and its limits to the PR author's account, not to the maintainer. With "Require review from Code Owners" on, as the README asks, every CI change would need that account's review.The new CI step runs the guard with
--protect .github/workflows --protect .github/CODEOWNERS, so any later PR that touches a workflow fails CI.scripts/gate-limits.txtis a snapshot ofb7aa9f3. Main now has about three times as manyunwrap/expectlines and more than twice as manyunsafe {blocks, so the gate would fail on main itself as soon as it is merged.The README points to an
install-in-repo.shthat is not in the PR.Recommendation: close (bad: hands CI ownership to the PR author and would fail every build on current main).
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.