Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions d3d12/xtask/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use anyhow::{Context, Result, bail};

// (The translator's one file is 570 KiB of source.)
const MAX_FILE_SIZE: usize = 640 * 1024;
/// A source file may be longer: the size check is about data hidden in a file that has no business
/// being large, and Rust source is ordinary code at any length. The content checks below still run.
const MAX_SOURCE_FILE_SIZE: usize = 8 * 1024 * 1024;
const MAX_ENCODED_RUN: usize = 512;

const ALLOWED_EXTENSIONS: &[&str] = &[
Expand All @@ -29,10 +32,16 @@ const ALLOWED_NAMES: &[&str] = &[
];

/// Paths explicitly exempted, with a reason. Keep this empty if at all possible.
const EXCEPTIONS: &[(&str, &str)] = &[(
"crates/leonida-formats/src/bcpack-tables.bin",
"the BCPack format's constant tables: nothing decodes without them and they cannot be derived (owner's decision, 2026-11-23; docs/asset-policy.md)",
)];
const EXCEPTIONS: &[(&str, &str)] = &[
(
"crates/leonida-formats/src/bcpack-tables.bin",
"the BCPack format's constant tables: nothing decodes without them and they cannot be derived (owner's decision, 2026-11-23; docs/asset-policy.md)",
),
(
"crates/leonida-gpu/src/indirect/amplify_dxil.rs",
"DXC's DXIL output for `amplify.hlsl`, written by `tools/make_amplify_dxil.py` and read as the amplification stage's code: it is a shader container, so `DXBC` in `FORBIDDEN_MAGIC` keeps it out of a `.bin`, and a byte array is the only form it can be built in. It is generated - regenerate it with that tool, never edit it by hand",
),
];

/// Signatures of formats that must never be committed.
const FORBIDDEN_MAGIC: &[(&[u8], &str)] = &[
Expand Down Expand Up @@ -155,9 +164,10 @@ pub fn check(path: &str, content: &[u8]) -> Vec<String> {
if !allowed {
problems.push("file type not on the allowlist".to_owned());
}
if content.len() > MAX_FILE_SIZE {
let size_limit = if ext.as_deref() == Some("rs") { MAX_SOURCE_FILE_SIZE } else { MAX_FILE_SIZE };
if content.len() > size_limit {
problems.push(format!(
"{} bytes exceeds the {MAX_FILE_SIZE}-byte limit",
"{} bytes exceeds the {size_limit}-byte limit",
content.len()
));
}
Expand Down Expand Up @@ -238,6 +248,18 @@ mod tests {
assert!(check(".githooks/pre-commit", b"#!/bin/sh\n").is_empty());
}

#[test]
fn a_source_file_past_the_data_limit_is_still_source() {
// Two files in this repository are longer than MAX_FILE_SIZE and are ordinary code.
let line = b"fn a() -> u32 { 1 }\n";
let long: Vec<u8> = line.iter().copied().cycle().take(640 * 1024 + 1).collect();
assert!(check("crates/x/src/host.rs", &long).is_empty());
// The limit still holds for a text file that has no reason to be that long, and the content
// checks are unchanged for either.
assert!(!check("notes.txt", &long).is_empty());
assert!(check("crates/x/src/lib.rs", &[0u8; 16]).iter().any(|p| p.contains("NUL")));
}

#[test]
fn rejects_game_like_files() {
assert!(!check("assets/tex.dds", b"DDS \x7c\0\0\0").is_empty());
Expand Down