// SPDX-License-Identifier: GPL-3.0-or-later //! Base relocation regression tests (#81). //! //! Images are built byte by byte from the PE/COFF layout: data directory 5 //! (Base Relocation Table) at optional-header offset 152, and blocks made of a //! 4-byte page RVA, a 4-byte block size and 2-byte entries whose top 4 bits //! are the type (0 = ABSOLUTE padding, 3 = HIGHLOW, 10 = DIR64). use xenora_loader::{GuestMemory, ParsedPe, RelocationError, apply_relocations}; const PREFERRED: u64 = 0x1_4000_0000; const RELOC_FILE: usize = 0x400; /// .text at RVA 0x1000 (file 0x200) holding two pointers, .reloc at RVA 0x2000 (file 0x400). /// `NumberOfRvaAndSizes` and the relocation table bytes are parameters. fn build(rva_and_sizes: u32, table: &[u8], dir_size: u32) -> Vec { let mut pe = vec![0u8; 0x600]; pe[0..2].copy_from_slice(b"MZ"); pe[0x3C..0x40].copy_from_slice(&0x40u32.to_le_bytes()); pe[0x40..0x44].copy_from_slice(b"PE\0\0"); let coff = 0x44; pe[coff..coff + 2].copy_from_slice(&0x8664u16.to_le_bytes()); pe[coff + 2..coff + 4].copy_from_slice(&2u16.to_le_bytes()); pe[coff + 16..coff + 18].copy_from_slice(&240u16.to_le_bytes()); let opt = coff + 20; pe[opt..opt + 2].copy_from_slice(&0x020Bu16.to_le_bytes()); pe[opt + 16..opt + 20].copy_from_slice(&0x1000u32.to_le_bytes()); pe[opt + 24..opt + 32].copy_from_slice(&PREFERRED.to_le_bytes()); pe[opt + 32..opt + 36].copy_from_slice(&0x1000u32.to_le_bytes()); pe[opt + 36..opt + 40].copy_from_slice(&0x200u32.to_le_bytes()); pe[opt + 56..opt + 60].copy_from_slice(&0x3000u32.to_le_bytes()); pe[opt + 60..opt + 64].copy_from_slice(&0x200u32.to_le_bytes()); pe[opt + 108..opt + 112].copy_from_slice(&rva_and_sizes.to_le_bytes()); pe[opt + 152..opt + 156].copy_from_slice(&0x2000u32.to_le_bytes()); pe[opt + 156..opt + 160].copy_from_slice(&dir_size.to_le_bytes()); let sections: [(&[u8], u32, u32, u32, u32); 2] = [ (b".text", 0x1000, 0x200, 0x200, 0x6000_0020), // .reloc fills the image up to SizeOfImage (0x3000). (b".reloc", 0x2000, 0x1000, RELOC_FILE as u32, 0x4200_0040), ]; for (i, (name, va, vsize, raw, flags)) in sections.into_iter().enumerate() { let h = opt + 240 + i * 40; pe[h..h + name.len()].copy_from_slice(name); pe[h + 8..h + 12].copy_from_slice(&vsize.to_le_bytes()); pe[h + 12..h + 16].copy_from_slice(&va.to_le_bytes()); pe[h + 16..h + 20].copy_from_slice(&0x200u32.to_le_bytes()); pe[h + 20..h + 24].copy_from_slice(&raw.to_le_bytes()); pe[h + 36..h + 40].copy_from_slice(&flags.to_le_bytes()); } // Two absolute pointers into the image, at RVAs 0x1010 and 0x1018. pe[0x210..0x218].copy_from_slice(&(PREFERRED + 0x1100).to_le_bytes()); pe[0x218..0x220].copy_from_slice(&(PREFERRED + 0x2000).to_le_bytes()); pe[RELOC_FILE..RELOC_FILE + table.len()].copy_from_slice(table); pe } fn block(page_rva: u32, entries: &[u16]) -> Vec { let mut b = page_rva.to_le_bytes().to_vec(); b.extend_from_slice(&(8 + 2 * entries.len() as u32).to_le_bytes()); for e in entries { b.extend_from_slice(&e.to_le_bytes()); } b } /// DIR64 at 0x1010 and 0x1018, plus one ABSOLUTE padding entry to keep 4-byte alignment. fn standard_table() -> Vec { block(0x1000, &[0xA010, 0xA018, 0x0000]) } fn load(bytes: &[u8], base: u64) -> (ParsedPe, GuestMemory) { let pe = ParsedPe::parse(bytes).expect("parse"); let mut memory = GuestMemory::new(); for s in &pe.sections { let raw = &bytes[s.raw_data_ptr as usize..(s.raw_data_ptr + s.raw_data_size) as usize]; memory .map_region_with_data( base + u64::from(s.virtual_address), 0x200, s.permissions, raw, ) .expect("map"); } (pe, memory) } fn relocate( bytes: &[u8], base: u64, ) -> ( Result, GuestMemory, ) { let (pe, mut memory) = load(bytes, base); let result = apply_relocations(&pe, bytes, base, &mut memory); (result, memory) } #[test] fn positive_rebase_adds_delta_to_each_dir64_target() { let table = standard_table(); let bytes = build(16, &table, table.len() as u32); let base = PREFERRED + 0x1_0000_0000; let (result, memory) = relocate(&bytes, base); let report = result.expect("relocate"); assert_eq!(report.delta, 0x1_0000_0000); assert_eq!(report.applied_dir64, 2); assert_eq!(report.skipped_absolute, 1); assert_eq!(report.total_entries, 3); assert_eq!( memory.read_u64_bypass(base + 0x1010).unwrap(), base + 0x1100 ); assert_eq!( memory.read_u64_bypass(base + 0x1018).unwrap(), base + 0x2000 ); } #[test] fn negative_rebase_subtracts_delta() { let table = standard_table(); let bytes = build(16, &table, table.len() as u32); let base = 0x1000_0000; let (result, memory) = relocate(&bytes, base); assert_eq!( result.expect("relocate").delta, base as i64 - PREFERRED as i64 ); assert_eq!( memory.read_u64_bypass(base + 0x1010).unwrap(), base + 0x1100 ); assert_eq!( memory.read_u64_bypass(base + 0x1018).unwrap(), base + 0x2000 ); } #[test] fn preferred_base_leaves_targets_unchanged() { let table = standard_table(); let bytes = build(16, &table, table.len() as u32); let (result, memory) = relocate(&bytes, PREFERRED); assert_eq!(result.expect("relocate").applied_dir64, 0); assert_eq!( memory.read_u64_bypass(PREFERRED + 0x1010).unwrap(), PREFERRED + 0x1100 ); } #[test] fn directory_beyond_declared_count_is_ignored() { // NumberOfRvaAndSizes = 5 means directory index 5 does not exist. let table = standard_table(); let bytes = build(5, &table, table.len() as u32); let base = PREFERRED + 0x1_0000_0000; let (result, memory) = relocate(&bytes, base); assert_eq!(result.expect("relocate").applied_dir64, 0); assert_eq!( memory.read_u64_bypass(base + 0x1010).unwrap(), PREFERRED + 0x1100 ); } #[test] fn unsupported_type_is_rejected() { let table = block(0x1000, &[0x3010, 0x0000]); // HIGHLOW is not a PE32+ fixup here let bytes = build(16, &table, table.len() as u32); let (result, _) = relocate(&bytes, PREFERRED + 0x1000); assert_eq!( result.unwrap_err(), RelocationError::UnsupportedRelocationType(3) ); } #[test] fn malformed_block_sizes_are_rejected() { let base = PREFERRED + 0x1000; for size in [0u32, 4, 7, 11] { let mut table = 0x1000u32.to_le_bytes().to_vec(); table.extend_from_slice(&size.to_le_bytes()); table.extend_from_slice(&[0; 8]); let bytes = build(16, &table, table.len() as u32); let (result, _) = relocate(&bytes, base); assert_eq!( result.unwrap_err(), RelocationError::MalformedBlockHeader { offset: RELOC_FILE }, "block size {size}" ); } } #[test] fn block_larger_than_directory_is_rejected() { let table = standard_table(); // 14 bytes let bytes = build(16, &table, 12); let (result, _) = relocate(&bytes, PREFERRED + 0x1000); assert_eq!( result.unwrap_err(), RelocationError::BlockSizeOutOfBounds { block_size: 14, max_size: 12 } ); } #[test] fn trailing_partial_block_header_is_rejected() { let mut table = block(0x1000, &[0xA010, 0x0000]); table.extend_from_slice(&[0; 4]); let bytes = build(16, &table, table.len() as u32); let (result, _) = relocate(&bytes, PREFERRED + 0x1000); assert_eq!( result.unwrap_err(), RelocationError::MalformedBlockHeader { offset: RELOC_FILE + 12 } ); } #[test] fn directory_past_end_of_file_is_rejected() { let table = standard_table(); let bytes = build(16, &table, 0x300); // .reloc raw data starts at 0x400 in a 0x600-byte file let (result, _) = relocate(&bytes, PREFERRED + 0x1000); assert_eq!( result.unwrap_err(), RelocationError::DirectoryOutOfBounds { rva: 0x2000, size: 0x300 } ); } #[test] fn target_outside_image_is_rejected_even_if_mapped() { // SizeOfImage is 0x3000; RVA 0x3000 is past the image but mapped by another region. let table = block(0x3000, &[0xA000, 0x0000]); let bytes = build(16, &table, table.len() as u32); let base = PREFERRED + 0x1000; let (pe, mut memory) = load(&bytes, base); let rw = pe.sections[1].permissions; memory .map_region(base + 0x3000, 0x1000, rw) .expect("adjacent mapping"); memory.write_u64_bypass(base + 0x3000, 0x55).unwrap(); let result = apply_relocations(&pe, &bytes, base, &mut memory); assert_eq!( result.unwrap_err(), RelocationError::TargetOutOfBounds { target_va: base + 0x3000 } ); assert_eq!(memory.read_u64_bypass(base + 0x3000).unwrap(), 0x55); } #[test] fn target_rva_at_top_of_address_space_is_rejected() { let table = block(0xFFFF_F000, &[0xAFFC, 0x0000]); let bytes = build(16, &table, table.len() as u32); let (result, _) = relocate(&bytes, PREFERRED + 0x1000); assert!(matches!( result, Err(RelocationError::TargetOutOfBounds { .. }) )); }