Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 7 additions & 9 deletions crates/xcl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ fn direct_sources(dir: &Path, out: &mut Vec<PathBuf>) {
}
}

fn add(h: &mut u64, bytes: &[u8]) {
for &byte in bytes {
*h ^= byte as u64;
*h = h.wrapping_mul(0x100_0000_01b3);
}
}
// FNV-1a over the sources, each CRLF taken as LF (`source_hash.rs`, tested with the crate)
#[path = "src/source_hash.rs"]
mod source_hash;
use source_hash::{add, add_source};

fn main() {
// xcl.exe without high-entropy ASLR: the title's memory then lies under 2^40, which a
Expand All @@ -69,7 +67,7 @@ fn main() {
println!("cargo:rerun-if-changed={relative}");
add(&mut h, relative.as_bytes());
add(&mut h, &[0]);
add(&mut h, &std::fs::read(root.join(relative)).expect("read shader translator source"));
add_source(&mut h, &std::fs::read(root.join(relative)).expect("read shader translator source"));
add(&mut h, &[0]);
}
println!("cargo:rustc-env=XCL_TRANSLATOR_SOURCE_ID={h:016x}");
Expand All @@ -78,7 +76,7 @@ fn main() {
println!("cargo:rerun-if-changed={relative}");
add(&mut p, relative.as_bytes());
add(&mut p, &[0]);
add(&mut p, &std::fs::read(root.join(relative)).expect("read per-lane source"));
add_source(&mut p, &std::fs::read(root.join(relative)).expect("read per-lane source"));
add(&mut p, &[0]);
}
println!("cargo:rustc-env=XCL_PER_LANE_SOURCE_ID={p:016x}");
Expand All @@ -91,7 +89,7 @@ fn main() {
for path in &sources {
add(&mut d, path.strip_prefix(root).unwrap_or(path).to_string_lossy().replace('\\', "/").as_bytes());
add(&mut d, &[0]);
add(&mut d, &std::fs::read(path).expect("read direct DXIL source"));
add_source(&mut d, &std::fs::read(path).expect("read direct DXIL source"));
add(&mut d, &[0]);
}
println!("cargo:rustc-env=XCL_DIRECT_DXIL_SOURCE_ID={d:016x}");
Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub mod resolver;
pub mod rpf;
mod rpf_probe;
pub mod shaders;
#[cfg(test)]
mod source_hash;
pub mod state;
pub mod stubs;
pub mod tls;
Expand Down
54 changes: 54 additions & 0 deletions crates/xcl/src/source_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! The hash `build.rs` gives the translators' sources (`XCL_TRANSLATOR_SOURCE_ID`,
//! `XCL_PER_LANE_SOURCE_ID`, `XCL_DIRECT_DXIL_SOURCE_ID`), part of the translated cache's identity
//! (`recomp::pso`). `build.rs` includes this file (`#[path]`), so its tests run with the crate's.

/// FNV-1a 64 of `bytes`, continuing `h`.
pub fn add(h: &mut u64, bytes: &[u8]) {
for &byte in bytes {
*h ^= byte as u64;
*h = h.wrapping_mul(0x100_0000_01b3);
}
}

/// `add` of a source file's text with each CRLF taken as LF. rustc reads both the same, but a
/// checkout with `core.autocrlf` has CRLF and one without has LF: hashed as bytes, the same commit
/// got two identities, and a build from a tree where one file was rewritten with LF a third, so
/// none of them found the others' translations.
pub fn add_source(h: &mut u64, text: &[u8]) {
let mut rest = text;
while let Some(at) = rest.windows(2).position(|w| w == b"\r\n") {
add(h, &rest[..at]);
rest = &rest[at + 1..];
}
add(h, rest);
}

#[cfg(test)]
mod tests {
use super::{add, add_source};

fn hash(f: fn(&mut u64, &[u8]), text: &[u8]) -> u64 {
let mut h = 0xcbf2_9ce4_8422_2325u64;
f(&mut h, text);
h
}

#[test]
fn a_source_hashes_the_same_with_crlf_and_lf_line_endings() {
let lf = b"fn a() {\n b();\n}\n";
let crlf = b"fn a() {\r\n b();\r\n}\r\n";
assert_eq!(hash(add_source, crlf), hash(add_source, lf));
// an LF file keeps the identity it had before
assert_eq!(hash(add_source, lf), hash(add, lf));
}

#[test]
fn other_carriage_returns_and_other_text_still_count() {
let lf = b"let s = \"a\";\n";
assert_ne!(hash(add_source, b"let s = \"a\r\";\n"), hash(add_source, lf));
assert_ne!(hash(add_source, b"let s = \"b\";\n"), hash(add_source, lf));
assert_ne!(hash(add_source, b"a\r\r\nb"), hash(add_source, b"a\nb"));
// only the CR right before an LF goes
assert_eq!(hash(add_source, b"a\r\r\nb"), hash(add, b"a\r\nb"));
}
}
Loading