Skip to content
Open
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
14 changes: 10 additions & 4 deletions crates/xcl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ 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
// retail title's BVH builder requires of its GPU addresses (#585, PR #631)
// xcl.exe and integration tests without high-entropy ASLR: the title's memory then lies under
// 2^40, which a retail title's BVH builder requires of its GPU addresses (#585, PR #631, #2263)
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"),
Ok("msvc") => {
println!("cargo:rustc-link-arg-bin=xcl=/HIGHENTROPYVA:NO");
println!("cargo:rustc-link-arg-tests=/HIGHENTROPYVA:NO");
}
Ok("gnu") => {
println!("cargo:rustc-link-arg-bin=xcl=-Wl,--disable-high-entropy-va");
println!("cargo:rustc-link-arg-tests=-Wl,--disable-high-entropy-va");
}
_ => {}
}
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
Expand Down
38 changes: 31 additions & 7 deletions crates/xcl/tests/low_va.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
//! xcl.exe opts out of high-entropy ASLR, so the title's memory stays below 2^40 (#585).
//! `xcl.exe` and the crate's integration tests opt out of high-entropy ASLR, so the title's
//! memory stays below 2^40 (#585, #2263).

use std::path::Path;
use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE};

/// IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA
const HIGH_ENTROPY_VA: u16 = 0x20;
/// Upper bound of the low-VA address space required by console BVH addresses (#585).
const LOW_VA_CEILING: usize = 1usize << 40;

#[test]
fn xcl_exe_is_linked_without_high_entropy_va() {
let image = std::fs::read(env!("CARGO_BIN_EXE_xcl")).unwrap();
fn assert_no_high_entropy_va(path: &Path) {
let image = std::fs::read(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
let pe = u32::from_le_bytes(image[0x3c..0x40].try_into().unwrap()) as usize;
assert_eq!(&image[pe..pe + 4], b"PE\0\0");
assert_eq!(&image[pe..pe + 4], b"PE\0\0", "{}: not a PE image", path.display());
let optional = pe + 24;
assert_eq!(u16::from_le_bytes(image[optional..optional + 2].try_into().unwrap()), 0x20b, "PE32+");
assert_eq!(u16::from_le_bytes(image[optional..optional + 2].try_into().unwrap()), 0x20b, "{}: expected PE32+", path.display());
// 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");
assert_eq!(flags & HIGH_ENTROPY_VA, 0, "{}: DllCharacteristics {flags:#x}: high-entropy VA is on", path.display());
}

#[test]
fn xcl_exe_is_linked_without_high_entropy_va() {
assert_no_high_entropy_va(Path::new(env!("CARGO_BIN_EXE_xcl")));
}

#[test]
fn integration_tests_are_linked_without_high_entropy_va() {
let exe = std::env::current_exe().unwrap();
assert_no_high_entropy_va(&exe);
let addr = unsafe {
let p = VirtualAlloc(std::ptr::null(), 64 * 1024, MEM_RESERVE, PAGE_READWRITE);
assert!(!p.is_null(), "VirtualAlloc(NULL, 64 KiB, MEM_RESERVE) failed");
let addr = p as usize;
assert_ne!(VirtualFree(p, 0, MEM_RELEASE), 0, "VirtualFree({addr:#x}) failed");
addr
};
assert!(addr < LOW_VA_CEILING, "VirtualAlloc(NULL) landed at {addr:#x} (>= 2^40)");
}
Loading