diff --git a/crates/xcl/build.rs b/crates/xcl/build.rs new file mode 100644 index 0000000000..eff042ad67 --- /dev/null +++ b/crates/xcl/build.rs @@ -0,0 +1,14 @@ +//! xcl.exe is linked without high-entropy ASLR. With it, Windows places a process's bottom-up +//! allocations (heaps, stacks, `VirtualAlloc(NULL)`) around 1–2.5 TiB, above 2^40; the title's +//! memory lands there too, and a retail game's XboxBvhBuilder refuses an acceleration structure +//! there ("DestAccelerationStructureData can not have more than 40b of address set", #585). +//! Without it, the same allocations start near 2 GiB, as console addresses do. + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + match std::env::var("CARGO_CFG_TARGET_ENV").as_deref() { + Ok("msvc") => println!("cargo:rustc-link-arg-bin=xcl=/HIGHENTROPYVA:NO"), + Ok("gnu") => println!("cargo:rustc-link-arg-bin=xcl=-Wl,--disable-high-entropy-va"), + _ => {} + } +} diff --git a/crates/xcl/tests/low_va.rs b/crates/xcl/tests/low_va.rs new file mode 100644 index 0000000000..fffcb54e52 --- /dev/null +++ b/crates/xcl/tests/low_va.rs @@ -0,0 +1,16 @@ +//! xcl.exe opts out of high-entropy ASLR, so the title's memory stays below 2^40 (#585). + +/// IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA +const HIGH_ENTROPY_VA: u16 = 0x20; + +#[test] +fn xcl_exe_is_linked_without_high_entropy_va() { + let image = std::fs::read(env!("CARGO_BIN_EXE_xcl")).unwrap(); + let pe = u32::from_le_bytes(image[0x3c..0x40].try_into().unwrap()) as usize; + assert_eq!(&image[pe..pe + 4], b"PE\0\0"); + let optional = pe + 24; + assert_eq!(u16::from_le_bytes(image[optional..optional + 2].try_into().unwrap()), 0x20b, "PE32+"); + // DllCharacteristics is at +70 of the PE32+ optional header + let flags = u16::from_le_bytes(image[optional + 70..optional + 72].try_into().unwrap()); + assert_eq!(flags & HIGH_ENTROPY_VA, 0, "DllCharacteristics {flags:#x}: high-entropy VA is on"); +}