diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 5544274..8cf16b3 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -81,7 +81,9 @@ fn main() -> ExitCode { } let Some(pe) = pe_path else { - eprintln!("usage: xenora probe --pe [--mode ] [--report ]"); + eprintln!( + "usage: xenora probe --pe [--mode ] [--report ]" + ); return ExitCode::from(2); }; diff --git a/crates/loader/Cargo.toml b/crates/loader/Cargo.toml index b79e1a9..39e5ca4 100644 --- a/crates/loader/Cargo.toml +++ b/crates/loader/Cargo.toml @@ -9,6 +9,10 @@ rust-version.workspace = true [lints] workspace = true +[features] +# Runs the tests that need a native Metal device (scripts/test-gpu.sh); CI's virtual GPU lacks Apple family 7. +gpu-tests = [] + [dependencies] libc = "0.2" diff --git a/crates/loader/src/pe.rs b/crates/loader/src/pe.rs index 34383fc..bb90018 100644 --- a/crates/loader/src/pe.rs +++ b/crates/loader/src/pe.rs @@ -83,6 +83,7 @@ pub struct LoadedImage { } // Windows PE Characteristic Constants +pub const IMAGE_SCN_CNT_CODE: u32 = 0x00000020; pub const IMAGE_SCN_MEM_EXECUTE: u32 = 0x20000000; pub const IMAGE_SCN_MEM_READ: u32 = 0x40000000; pub const IMAGE_SCN_MEM_WRITE: u32 = 0x80000000; diff --git a/crates/loader/src/probe.rs b/crates/loader/src/probe.rs index 29cc27e..e6158fd 100644 --- a/crates/loader/src/probe.rs +++ b/crates/loader/src/probe.rs @@ -77,7 +77,7 @@ pub struct InstructionRecord { pub struct ProbeReport { pub file_size: usize, pub sha256: String, - pub pe: ParsedPe, + pub parsed: ParsedPe, pub relocations: RelocationCensus, pub tls: TlsCensus, pub imports: Vec, @@ -159,8 +159,11 @@ pub fn probe_pe_bytes(bytes: &[u8], config: &ProbeConfig) -> Result Result= 128 { - u32::from_le_bytes(bytes[opt_offset + 120..opt_offset + 124].try_into().unwrap()) + u32::from_le_bytes( + bytes[opt_offset + 120..opt_offset + 124] + .try_into() + .unwrap(), + ) } else { 0 }; let reloc_dir_rva = if size_of_optional_header >= 160 { - u32::from_le_bytes(bytes[opt_offset + 152..opt_offset + 156].try_into().unwrap()) + u32::from_le_bytes( + bytes[opt_offset + 152..opt_offset + 156] + .try_into() + .unwrap(), + ) } else { 0 }; let reloc_dir_size = if size_of_optional_header >= 160 { - u32::from_le_bytes(bytes[opt_offset + 156..opt_offset + 160].try_into().unwrap()) + u32::from_le_bytes( + bytes[opt_offset + 156..opt_offset + 160] + .try_into() + .unwrap(), + ) } else { 0 }; let tls_dir_rva = if size_of_optional_header >= 192 { - u32::from_le_bytes(bytes[opt_offset + 184..opt_offset + 188].try_into().unwrap()) + u32::from_le_bytes( + bytes[opt_offset + 184..opt_offset + 188] + .try_into() + .unwrap(), + ) } else { 0 }; @@ -263,56 +282,66 @@ pub fn probe_pe_bytes(bytes: &[u8], config: &ProbeConfig) -> Result 0 { - if let Some(reloc_off) = rva_to_offset(reloc_dir_rva) { - let mut read_bytes = 0usize; - let max_read = std::cmp::min(reloc_dir_size as usize, bytes.len().saturating_sub(reloc_off)); - while read_bytes + 8 <= max_read { - let block_start = reloc_off + read_bytes; - let block_sz = u32::from_le_bytes(bytes[block_start + 4..block_start + 8].try_into().unwrap()) as usize; - if block_sz < 8 || read_bytes + block_sz > max_read { - break; - } - let num_entries = (block_sz - 8) / 2; - for j in 0..num_entries { - let e_off = block_start + 8 + j * 2; - let val = u16::from_le_bytes(bytes[e_off..e_off + 2].try_into().unwrap()); - let rtype = (val >> 12) as u8; - *relocations.type_counts.entry(rtype).or_insert(0) += 1; - relocations.total_entries += 1; - } - read_bytes += block_sz; + if reloc_dir_rva != 0 + && reloc_dir_size > 0 + && let Some(reloc_off) = rva_to_offset(reloc_dir_rva) + { + let mut read_bytes = 0usize; + let max_read = std::cmp::min( + reloc_dir_size as usize, + bytes.len().saturating_sub(reloc_off), + ); + while read_bytes + 8 <= max_read { + let block_start = reloc_off + read_bytes; + let block_sz = + u32::from_le_bytes(bytes[block_start + 4..block_start + 8].try_into().unwrap()) + as usize; + if block_sz < 8 || read_bytes + block_sz > max_read { + break; } + let num_entries = (block_sz - 8) / 2; + for j in 0..num_entries { + let e_off = block_start + 8 + j * 2; + let val = u16::from_le_bytes(bytes[e_off..e_off + 2].try_into().unwrap()); + let rtype = (val >> 12) as u8; + *relocations.type_counts.entry(rtype).or_insert(0) += 1; + relocations.total_entries += 1; + } + read_bytes += block_sz; } } // 2. TLS Census let mut tls = TlsCensus::default(); - if tls_dir_rva != 0 { - if let Some(tls_off) = rva_to_offset(tls_dir_rva) { - if tls_off + 40 <= bytes.len() { - tls.raw_data_start_va = u64::from_le_bytes(bytes[tls_off..tls_off + 8].try_into().unwrap()); - tls.raw_data_end_va = u64::from_le_bytes(bytes[tls_off + 8..tls_off + 16].try_into().unwrap()); - tls.index_address = u64::from_le_bytes(bytes[tls_off + 16..tls_off + 24].try_into().unwrap()); - tls.callbacks_address = u64::from_le_bytes(bytes[tls_off + 24..tls_off + 32].try_into().unwrap()); - tls.zero_fill_size = u32::from_le_bytes(bytes[tls_off + 32..tls_off + 36].try_into().unwrap()); - tls.characteristics = u32::from_le_bytes(bytes[tls_off + 36..tls_off + 40].try_into().unwrap()); + if tls_dir_rva != 0 + && let Some(tls_off) = rva_to_offset(tls_dir_rva) + && tls_off + 40 <= bytes.len() + { + tls.raw_data_start_va = u64::from_le_bytes(bytes[tls_off..tls_off + 8].try_into().unwrap()); + tls.raw_data_end_va = + u64::from_le_bytes(bytes[tls_off + 8..tls_off + 16].try_into().unwrap()); + tls.index_address = + u64::from_le_bytes(bytes[tls_off + 16..tls_off + 24].try_into().unwrap()); + tls.callbacks_address = + u64::from_le_bytes(bytes[tls_off + 24..tls_off + 32].try_into().unwrap()); + tls.zero_fill_size = + u32::from_le_bytes(bytes[tls_off + 32..tls_off + 36].try_into().unwrap()); + tls.characteristics = + u32::from_le_bytes(bytes[tls_off + 36..tls_off + 40].try_into().unwrap()); - if tls.callbacks_address >= image_base { - let cb_rva = (tls.callbacks_address - image_base) as u32; - if let Some(cb_off) = rva_to_offset(cb_rva) { - let mut curr = cb_off; - while curr + 8 <= bytes.len() { - let target = u64::from_le_bytes(bytes[curr..curr + 8].try_into().unwrap()); - if target == 0 { - break; - } - tls.callback_targets.push(target); - curr += 8; - if tls.callback_targets.len() >= 64 { - break; - } - } + if tls.callbacks_address >= image_base { + let cb_rva = (tls.callbacks_address - image_base) as u32; + if let Some(cb_off) = rva_to_offset(cb_rva) { + let mut curr = cb_off; + while curr + 8 <= bytes.len() { + let target = u64::from_le_bytes(bytes[curr..curr + 8].try_into().unwrap()); + if target == 0 { + break; + } + tls.callback_targets.push(target); + curr += 8; + if tls.callback_targets.len() >= 64 { + break; } } } @@ -321,66 +350,68 @@ pub fn probe_pe_bytes(bytes: &[u8], config: &ProbeConfig) -> Result Result Result Result Result Result, O: AsRef>( - pe_path: P, +/// Probe PE bytes with `config` and generate artifacts in the output directory. +pub fn run_probe_and_export>( + bytes: &[u8], output_dir: O, + config: &ProbeConfig, ) -> Result> { - let bytes = fs::read(pe_path)?; - let report = probe_pe_bytes(&bytes, &ProbeConfig::default())?; + let report = probe_pe_bytes(bytes, config)?; let out = output_dir.as_ref(); fs::create_dir_all(out)?; // 1. imports.csv - let mut imports_csv = String::from("module,symbol_or_ordinal,iat_rva,category,exists_in_xenora,implemented_with_tests,would_be_required_for_loader_binding,notes\n"); + let mut imports_csv = String::from( + "module,symbol_or_ordinal,iat_rva,category,exists_in_xenora,implemented_with_tests,would_be_required_for_loader_binding,notes\n", + ); for imp in &report.imports { let _ = writeln!( imports_csv, @@ -676,7 +719,13 @@ pub fn run_probe_and_export, O: AsRef>( let _ = writeln!( cpu_csv, "0x{:08x},0x{:016x},\"{}\",\"{}\",{},{},\"{}\"", - insn.rva, insn.va, hex_bytes, insn.mnemonic, insn.is_supported_dbt, insn.is_supported_interp, insn.notes + insn.rva, + insn.va, + hex_bytes, + insn.mnemonic, + insn.is_supported_dbt, + insn.is_supported_interp, + insn.notes ); } fs::write(out.join("cpu_coverage.csv"), cpu_csv)?; @@ -702,13 +751,18 @@ pub fn run_probe_and_export, O: AsRef>( "#, report.sha256, report.file_size, - report.pe.image_base, - report.pe.entry_point_rva, - report.pe.size_of_image, - report.pe.sections.len(), + report.parsed.image_base, + report.parsed.entry_point_rva, + report.parsed.size_of_image, + report.parsed.sections.len(), report.imports.len(), report.relocations.total_entries, - report.relocations.type_counts.get(&10).copied().unwrap_or(0), + report + .relocations + .type_counts + .get(&10) + .copied() + .unwrap_or(0), report.tls.callback_targets.len(), report .first_unsupported_dbt_insn @@ -724,14 +778,26 @@ pub fn run_probe_and_export, O: AsRef>( let _ = writeln!(md, "## Executable Metadata"); let _ = writeln!(md, "- **SHA-256**: `{}`", report.sha256); let _ = writeln!(md, "- **File Size**: `{}` bytes", report.file_size); - let _ = writeln!(md, "- **Image Base**: `0x{:016x}`", report.pe.image_base); - let _ = writeln!(md, "- **Entry Point RVA**: `0x{:08x}`", report.pe.entry_point_rva); - let _ = writeln!(md, "- **Size of Image**: `0x{:08x}`", report.pe.size_of_image); + let _ = writeln!( + md, + "- **Image Base**: `0x{:016x}`", + report.parsed.image_base + ); + let _ = writeln!( + md, + "- **Entry Point RVA**: `0x{:08x}`", + report.parsed.entry_point_rva + ); + let _ = writeln!( + md, + "- **Size of Image**: `0x{:08x}`", + report.parsed.size_of_image + ); let _ = writeln!(md, "- **Section Alignment**: `4096` bytes"); let _ = writeln!(md, "- **File Alignment**: `512` bytes\n"); let _ = writeln!(md, "## Sections Census (10)"); - for s in &report.pe.sections { + for s in &report.parsed.sections { let _ = writeln!( md, "- `{:<8}` RVA: `0x{:08x}` VirtualSize: `0x{:08x}` RawSize: `0x{:08x}` Perms: [{}{}{}]", @@ -746,7 +812,11 @@ pub fn run_probe_and_export, O: AsRef>( } let _ = writeln!(md, "\n## Base Relocations"); - let _ = writeln!(md, "- **Total Entries**: `{}`", report.relocations.total_entries); + let _ = writeln!( + md, + "- **Total Entries**: `{}`", + report.relocations.total_entries + ); for (t, count) in &report.relocations.type_counts { let name = match t { 0 => "ABSOLUTE (padding)", @@ -757,18 +827,41 @@ pub fn run_probe_and_export, O: AsRef>( } let _ = writeln!(md, "\n## TLS Metadata"); - let _ = writeln!(md, "- **RawData**: `0x{:016x}`..`0x{:016x}`", report.tls.raw_data_start_va, report.tls.raw_data_end_va); - let _ = writeln!(md, "- **Index Address**: `0x{:016x}`", report.tls.index_address); - let _ = writeln!(md, "- **Callbacks Array**: `0x{:016x}`", report.tls.callbacks_address); - let _ = writeln!(md, "- **Declared Callbacks ({})**:", report.tls.callback_targets.len()); + let _ = writeln!( + md, + "- **RawData**: `0x{:016x}`..`0x{:016x}`", + report.tls.raw_data_start_va, report.tls.raw_data_end_va + ); + let _ = writeln!( + md, + "- **Index Address**: `0x{:016x}`", + report.tls.index_address + ); + let _ = writeln!( + md, + "- **Callbacks Array**: `0x{:016x}`", + report.tls.callbacks_address + ); + let _ = writeln!( + md, + "- **Declared Callbacks ({})**:", + report.tls.callback_targets.len() + ); for cb in &report.tls.callback_targets { let _ = writeln!(md, " - `0x{:016x}`", cb); } let _ = writeln!(md, "\n## Import Statistics"); let _ = writeln!(md, "- **Total Imported DLLs**: `68`"); - let _ = writeln!(md, "- **Total Imported Thunks**: `{}`", report.imports.len()); - let _ = writeln!(md, "- **Supported / Implemented in Xenora**: `0` (Commercial runtime requires GDK / Scarlett emulation)\n"); + let _ = writeln!( + md, + "- **Total Imported Thunks**: `{}`", + report.imports.len() + ); + let _ = writeln!( + md, + "- **Supported / Implemented in Xenora**: `0` (Commercial runtime requires GDK / Scarlett emulation)\n" + ); let _ = writeln!(md, "## First Static DBT Blocker"); if let Some(insn) = &report.first_unsupported_dbt_insn { @@ -778,7 +871,11 @@ pub fn run_probe_and_export, O: AsRef>( .map(|b| format!("{b:02x}")) .collect::>() .join(" "); - let _ = writeln!(md, "- **RVA**: `0x{:08x}` (VA: `0x{:016x}`)", insn.rva, insn.va); + let _ = writeln!( + md, + "- **RVA**: `0x{:08x}` (VA: `0x{:016x}`)", + insn.rva, insn.va + ); let _ = writeln!(md, "- **Bytes**: `{}`", hex_bytes); let _ = writeln!(md, "- **Mnemonic**: `{}`", insn.mnemonic); let _ = writeln!(md, "- **Reason**: `{}`", insn.notes); diff --git a/crates/loader/tests/dbt_render.rs b/crates/loader/tests/dbt_render.rs index 774cace..6932e13 100644 --- a/crates/loader/tests/dbt_render.rs +++ b/crates/loader/tests/dbt_render.rs @@ -182,6 +182,10 @@ fn test_native_arm64_dbt_subroutine_calls_and_stack() { } #[test] +#[cfg_attr( + not(feature = "gpu-tests"), + ignore = "needs a native Metal device with Apple family 7; run scripts/test-gpu.sh" +)] fn test_guest_dbt_first_rendered_frame_with_metal() { // Synthetic PE32+ guest program: // Initializes vertex data pointer and shader address in guest registers, diff --git a/crates/loader/tests/probe_static.rs b/crates/loader/tests/probe_static.rs index 06b18b1..35174fd 100644 --- a/crates/loader/tests/probe_static.rs +++ b/crates/loader/tests/probe_static.rs @@ -79,7 +79,8 @@ fn test_probe_artifact_export_creates_all_files() { let _ = std::fs::remove_dir_all(&temp_dir); let config = ProbeConfig::default(); - let report = run_probe_and_export(&pe_bytes, &temp_dir, &config).expect("export should succeed"); + let report = + run_probe_and_export(&pe_bytes, &temp_dir, &config).expect("export should succeed"); assert!(temp_dir.join("candidate_static_report.md").exists()); assert!(temp_dir.join("candidate_static_report.json").exists()); diff --git a/scripts/test-gpu.sh b/scripts/test-gpu.sh index 3275d30..5bc8797 100755 --- a/scripts/test-gpu.sh +++ b/scripts/test-gpu.sh @@ -12,3 +12,7 @@ export CARGO_BUILD_JOBS=${CARGO_BUILD_JOBS:-4} cargo test --locked -p xenora-metal --features gpu-tests --test native_compute --no-run timeout 30s cargo test --locked -p xenora-metal --features gpu-tests --test native_compute -- \ --nocapture --test-threads=1 + +cargo test --locked -p xenora-loader --features gpu-tests --test dbt_render --no-run +timeout 30s cargo test --locked -p xenora-loader --features gpu-tests --test dbt_render -- \ + --nocapture --test-threads=1