loader: report the first inaccessible byte of a page-crossing guest fault (#175)

A guest access that starts in a mapped 4 KiB page and runs into an
unmapped or forbidden one reported the start of the access. It now
reports the lowest address the access may not touch, which is what the
Intel SDM page-fault cases from #155 expect. Which accesses succeed is
unchanged, and a failed access still reads and writes nothing.

- memory.rs: resolve() reports the failing byte for both
  UnmappedAddress and PermissionViolation.
- tests/memory_page_boundaries.rs: permissions, adjacent pages, address
  overflow and the fault address at every 4 KiB guest boundary across
  two host pages (16 KiB each on Apple Silicon).
- cpu_known_answers.rs: the three page-crossing fault cases are no
  longer an ignored defect; they pass on both engines.
- memory_validation.rs, generated_runtime_integration.rs: five
  expectations that pinned the start address now name the first
  inaccessible byte.

Signed-off-by: sahib453 <117431595+sahib453@users.noreply.github.com>
This commit is contained in:
sahib453 committed 2026-11-24 23:09:42 +05:30
1 parent 0fdb995892
commit 8b67479526
6 files changed
+297 -16

No files matched your search

+1
View File
@@ -187,6 +187,7 @@ remains unsupported.
native DBT.
### Fixed
- Fixed the address reported when a guest memory access faults part-way across a 4 KiB page boundary (#175). An access that starts in a mapped page and runs into an unmapped or forbidden one now reports the first byte it may not touch; it used to report the start of the access. An access whose first byte fails reports the same address as before, and a failed access still reads and writes nothing. Three Intel SDM page-fault cases in `cpu_known_answers.rs` moved from open defect to verified on both engines. `crates/loader/tests/memory_page_boundaries.rs` checks permissions, adjacent pages, address overflow and the fault address at every 4 KiB guest boundary across two host pages (16 KiB each on Apple Silicon). Guest memory is checked in software, so the host page size does not affect these results. Four #82 expectations and one CRT `memmove` expectation that pinned the start address were corrected. Not compared with x86 hardware; awaiting independent review.
- Made the static probe and the binder share one bounded import parser, after the truncated-name fix for #270. Truncated, unterminated, empty or non-ASCII names, reserved bits and endless tables are reported by the probe and refused by the binder.
- Fixed import binding writing some slots before finding an unresolved import, and service calls taking effect before an unreadable return address was noticed. A service that changes a nonvolatile register, including XMM6 to XMM15, now stops the guest (part of #201).
- Fixed older import-generator templates that reported success for unimplemented wrappers, used a nonexistent exit reason, and read floating-point arguments from integer registers.
+11 -2
View File
@@ -419,6 +419,12 @@ impl GuestMemory {
/// byte must be mapped and every region must grant `required` (`None` bypasses
/// permission checks for the loader). The whole range is checked before any byte
/// is copied, so a failed access never partially reads or writes.
///
/// A failed access reports the lowest address it may not touch, which is the
/// start of the access only when its first byte fails. An access that begins in
/// a mapped page and runs into an unmapped or forbidden one therefore reports
/// the first byte of that page, as an x86 page fault reports the linear address
/// that could not be translated or accessed.
fn resolve(
&self,
address: u64,
@@ -452,7 +458,10 @@ impl GuestMemory {
Some(_) => region.permissions.execute,
};
if let (false, Some(required)) = (permitted, required) {
return Err(MemoryError::PermissionViolation { address, required });
return Err(MemoryError::PermissionViolation {
address: cursor,
required,
});
}
let take = (end.min(region_end) - cursor) as usize;
pieces.push((index, (cursor - region.base) as usize, take));
@@ -461,7 +470,7 @@ impl GuestMemory {
return Ok(pieces);
}
}
Err(MemoryError::UnmappedAddress(address))
Err(MemoryError::UnmappedAddress(cursor))
}
fn read_checked(
+4 -8
View File
@@ -1493,7 +1493,7 @@ jump_to_unmapped_faults_at_target: rbx=U => rax=1 rip=U f=* ! PFX U
jump_to_data_page_faults_at_target: rbx=D+40 => rip=D+40 f=* ! PFX D+40
";
const DEFECT_STRADDLING_FAULT_ADDRESS: &str = r"
const STRADDLING_FAULT_ADDRESS: &str = r"
@ mov rcx, [rsi-4]
!488b4efc
SDM 3A 5.7 (the address with no translation); 3A 7.15 Interrupt 14
@@ -2632,10 +2632,7 @@ const GROUPS: &[(&str, &str)] = &[
("fs_gs", FS_GS),
("decoding", DECODING),
("memory_faults", MEMORY_FAULTS),
(
"defect_straddling_fault_address",
DEFECT_STRADDLING_FAULT_ADDRESS,
),
("straddling_fault_address", STRADDLING_FAULT_ADDRESS),
("defect_noncanonical_address", DEFECT_NONCANONICAL_ADDRESS),
("packed_integer_sse2", PACKED_INTEGER_SSE2),
("packed_integer_beyond_sse2", PACKED_INTEGER_BEYOND_SSE2),
@@ -3440,9 +3437,8 @@ fn memory_faults() {
}
#[test]
#[ignore = "known defect (#155): a page-crossing fault reports the start of the access"]
fn defect_straddling_fault_address() {
check_group(DEFECT_STRADDLING_FAULT_ADDRESS);
fn straddling_fault_address() {
check_group(STRADDLING_FAULT_ADDRESS);
}
#[test]
@@ -96,10 +96,11 @@ fn crt_handlers_validate_memory_boundaries() {
0x1008
)))
);
// The destination 0x1005..0x1009 is mapped up to 0x1008.
assert_eq!(
call("memmove", 0x1005, 0x1000, 4, &mut memory),
Err(ExitReason::MemoryFault(MemoryError::UnmappedAddress(
0x1005
0x1008
)))
);
let mut bytes = [0; 8];
@@ -0,0 +1,274 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! Guest 4 KiB page boundaries on a host with larger pages (#175).
//!
//! Apple Silicon uses 16 KiB host pages, so four guest pages share one host
//! page and host page protection cannot separate them. Guest memory is checked
//! in software, byte by byte, and is never mapped with host protections. These
//! tests lay 4 KiB guest pages across two spans of the detected host page size
//! and require the same results at every guest page boundary, whether or not it
//! is also a host page boundary.
//!
//! The expected fault address is the first byte, in address order, that the
//! access may not touch: the linear address with no translation or without the
//! needed right. This is the reading of the Intel SDM used by the page-fault
//! cases in `cpu_known_answers.rs` (Volume 3A, Sections 5.7 and 7.15, Interrupt
//! 14). Every expectation below is a literal address or a table written from
//! that rule, not a value read back from the memory model. Nothing here was
//! compared with x86 hardware.
use xenora_loader::services::sysinfo::HostCapabilities;
use xenora_loader::{GuestMemory, MemoryError, MemoryPermissions as P};
const GUEST_PAGE: u64 = 0x1000;
/// Aligned to every host page size up to 1 GiB.
const BASE: u64 = 0x4000_0000;
fn host_page() -> u64 {
let size = HostCapabilities::detect()
.page_size
.expect("the host reports a page size");
assert_eq!(size % GUEST_PAGE, 0, "host page size {size:#x}");
size
}
/// Guest page boundaries strictly inside two host pages starting at `BASE`.
/// On a 16 KiB host that is seven boundaries, one of them a host page boundary.
fn boundaries() -> Vec<u64> {
let pages = 2 * host_page() / GUEST_PAGE;
(1..pages).map(|page| BASE + page * GUEST_PAGE).collect()
}
fn unmapped(address: u64) -> MemoryError {
MemoryError::UnmappedAddress(address)
}
fn violation(address: u64, required: &'static str) -> MemoryError {
MemoryError::PermissionViolation { address, required }
}
/// The page below `boundary`, filled with 0x5a, and optionally the page above
/// it, filled with 0xa5.
fn pages_around(boundary: u64, below: P, above: Option<P>) -> GuestMemory {
let mut memory = GuestMemory::new();
memory
.map_region_with_data(boundary - GUEST_PAGE, 0x1000, below, &[0x5a; 0x1000])
.unwrap();
if let Some(above) = above {
memory
.map_region_with_data(boundary, 0x1000, above, &[0xa5; 0x1000])
.unwrap();
}
memory
}
/// The 16 bytes centred on `boundary`, read without permission checks.
fn bytes_around(memory: &GuestMemory, boundary: u64) -> (u64, u64) {
(
memory.read_u64_bypass(boundary - 8).unwrap(),
memory.read_u64_bypass(boundary).unwrap(),
)
}
const BELOW: u64 = 0x5a5a_5a5a_5a5a_5a5a;
const ABOVE: u64 = 0xa5a5_a5a5_a5a5_a5a5;
#[test]
fn apple_silicon_host_pages_hold_four_guest_pages() {
let size = host_page();
if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
assert_eq!(size, 0x4000);
assert_eq!(boundaries().len(), 7);
assert!(boundaries().contains(&(BASE + 0x4000)));
}
}
#[test]
fn each_guest_page_in_a_host_page_keeps_its_own_permissions() {
// (permissions, read allowed, write allowed, execute allowed)
let kinds = [
(P::READ_WRITE, true, true, false),
(P::READ_ONLY, true, false, false),
(P::NO_ACCESS, false, false, false),
(P::READ_EXECUTE, true, false, true),
];
let pages = 2 * host_page() / GUEST_PAGE;
let mut memory = GuestMemory::new();
for page in 0..pages {
let fill = [page as u8 + 1; 0x1000];
let kind = kinds[page as usize % kinds.len()].0;
memory
.map_region_with_data(BASE + page * GUEST_PAGE, 0x1000, kind, &fill)
.unwrap();
}
for page in 0..pages {
let (_, read, write, execute) = kinds[page as usize % kinds.len()];
let fill = page as u8 + 1;
let first = BASE + page * GUEST_PAGE;
for address in [first, first + 0xFFF] {
let expected = |allowed, required| {
if allowed {
Ok(fill)
} else {
Err(violation(address, required))
}
};
assert_eq!(memory.read_u8(address), expected(read, "read"));
assert_eq!(
memory.fetch_instruction_byte(address),
expected(execute, "execute")
);
assert_eq!(
memory.write_u8(address, fill),
expected(write, "write").map(|_| ())
);
}
}
assert_eq!(memory.read_u8(BASE - 1), Err(unmapped(BASE - 1)));
let end = BASE + pages * GUEST_PAGE;
assert_eq!(memory.read_u8(end), Err(unmapped(end)));
}
#[test]
fn access_across_adjacent_guest_pages_lands_in_both() {
for boundary in boundaries() {
let mut memory = pages_around(boundary, P::READ_WRITE, Some(P::READ_WRITE));
memory
.write_u64(boundary - 3, 0x8877_6655_4433_2211)
.unwrap();
assert_eq!(memory.read_u64(boundary - 3), Ok(0x8877_6655_4433_2211));
// Little endian: three bytes below the boundary, five above it.
assert_eq!(
bytes_around(&memory, boundary),
(0x3322_115a_5a5a_5a5a, 0xa5a5_a588_7766_5544),
"boundary {boundary:#x}"
);
}
}
#[test]
fn crossing_into_an_unmapped_page_faults_at_its_first_byte() {
for boundary in boundaries() {
let mut memory = pages_around(boundary, P::READ_WRITE, None);
let fault = Err(unmapped(boundary));
for back in 1..8u64 {
let start = boundary - back;
let context = format!("boundary {boundary:#x}, start -{back}");
assert_eq!(memory.read_u64(start).map(|_| ()), fault, "{context}");
assert_eq!(memory.write_u64(start, 0), fault, "{context}");
assert_eq!(memory.write_u64_bypass(start, 0), fault, "{context}");
assert_eq!(memory.read_u64_bypass(start).map(|_| ()), fault);
}
for (back, width) in [(1u64, 2usize), (3, 4), (15, 16), (1, 0x1001)] {
let mut buffer = vec![0u8; width];
assert_eq!(memory.read_bytes(boundary - back, &mut buffer), fault);
assert_eq!(memory.write_bytes(boundary - back, &buffer), fault);
}
// The failed stores wrote nothing below the boundary, and the last
// access that fits still succeeds.
assert_eq!(memory.read_u64(boundary - 8), Ok(BELOW));
// An access that does not touch the mapped page faults at its own
// first byte.
assert_eq!(memory.read_u64(boundary).map(|_| ()), fault);
assert_eq!(
memory.read_u64(boundary + 5).map(|_| ()),
Err(unmapped(boundary + 5))
);
}
}
#[test]
fn starting_in_an_unmapped_page_faults_at_the_access_start() {
for boundary in boundaries() {
// Only the page above the boundary exists.
let mut memory = GuestMemory::new();
memory
.map_region_with_data(boundary, 0x1000, P::READ_WRITE, &[0xa5; 0x1000])
.unwrap();
for back in 1..8u64 {
let start = boundary - back;
assert_eq!(memory.read_u64(start).map(|_| ()), Err(unmapped(start)));
assert_eq!(memory.write_u64(start, 0), Err(unmapped(start)));
}
assert_eq!(memory.read_u64(boundary), Ok(ABOVE));
}
}
#[test]
fn crossing_into_a_page_without_the_right_faults_at_its_first_byte() {
for boundary in boundaries() {
// A store into a read-only page.
let mut memory = pages_around(boundary, P::READ_WRITE, Some(P::READ_ONLY));
for back in 1..8u64 {
assert_eq!(
memory.write_u64(boundary - back, 0),
Err(violation(boundary, "write")),
"boundary {boundary:#x}, start -{back}"
);
assert!(memory.read_u64(boundary - back).is_ok());
}
assert_eq!(bytes_around(&memory, boundary), (BELOW, ABOVE));
// A load from a page with no access.
let memory = pages_around(boundary, P::READ_ONLY, Some(P::NO_ACCESS));
for back in 1..8u64 {
assert_eq!(
memory.read_u64(boundary - back).map(|_| ()),
Err(violation(boundary, "read")),
"boundary {boundary:#x}, start -{back}"
);
}
assert_eq!(memory.read_u64(boundary - 8), Ok(BELOW));
}
}
#[test]
fn the_lowest_failing_byte_decides_the_fault() {
for boundary in boundaries() {
// Forbidden first page, unmapped second page: the first byte fails.
let mut memory = pages_around(boundary, P::READ_ONLY, None);
assert_eq!(
memory.write_u64(boundary - 4, 0),
Err(violation(boundary - 4, "write"))
);
assert_eq!(
memory.read_u64(boundary - 4).map(|_| ()),
Err(unmapped(boundary))
);
// Writable, then read-only, then unmapped: a store over all three
// stops at the read-only page, a load at the unmapped one.
let mut memory = pages_around(boundary, P::READ_WRITE, Some(P::READ_ONLY));
let mut buffer = vec![0u8; 0x1008];
assert_eq!(
memory.write_bytes(boundary - 4, &buffer),
Err(violation(boundary, "write"))
);
assert_eq!(
memory.read_bytes(boundary - 4, &mut buffer),
Err(unmapped(boundary + GUEST_PAGE))
);
assert_eq!(bytes_around(&memory, boundary), (BELOW, ABOVE));
}
}
#[test]
fn the_highest_guest_page_boundaries_do_not_wrap() {
// The last page that can be mapped ends one page below 2^64.
let top = u64::MAX - 0xFFF; // 0xffff_ffff_ffff_f000
let mut memory = pages_around(top, P::READ_WRITE, None);
assert_eq!(
memory.map_region(top, 0x1000, P::READ_WRITE),
Err(MemoryError::Overflow)
);
assert_eq!(memory.read_u64(top - 8), Ok(BELOW));
assert_eq!(memory.read_u64(top - 4).map(|_| ()), Err(unmapped(top)));
assert_eq!(memory.write_u64(top - 4, 0), Err(unmapped(top)));
// An access whose end passes 2^64 is an overflow wherever it starts.
assert_eq!(memory.read_u64(u64::MAX - 6), Err(MemoryError::Overflow));
assert_eq!(
memory.write_u64(u64::MAX - 6, 0),
Err(MemoryError::Overflow)
);
assert_eq!(memory.read_u64(top - 8), Ok(BELOW));
}
+5 -5
View File
@@ -5,7 +5,7 @@
//! Expected behavior follows x86-64 data access rules: an access is valid when
//! every byte it touches is mapped with the required permission, whether or not
//! it is aligned or crosses into an adjacent mapping. A faulting access changes
//! nothing.
//! nothing and reports the lowest address it may not touch (#175).
use xenora_loader::{GuestMemory, MemoryError, MemoryPermissions as P};
@@ -56,7 +56,7 @@ fn region_end_is_exclusive() {
lone.map_region(A, 0x1000, P::READ_ONLY).unwrap();
assert_eq!(
lone.read_u32(A + 0xFFD),
Err(MemoryError::UnmappedAddress(A + 0xFFD))
Err(MemoryError::UnmappedAddress(A + 0x1000))
);
assert_eq!(
lone.read_u8(A - 1),
@@ -90,7 +90,7 @@ fn spanning_into_a_gap_fails_without_partial_write() {
m.map_region(A + 0x2000, 0x1000, P::READ_WRITE).unwrap();
assert_eq!(
m.write_u64(A + 0xFFC, u64::MAX),
Err(MemoryError::UnmappedAddress(A + 0xFFC))
Err(MemoryError::UnmappedAddress(A + 0x1000))
);
assert_eq!(m.read_u32(A + 0xFFC), Ok(0x1111_1111));
}
@@ -101,7 +101,7 @@ fn spanning_into_a_forbidden_region_fails_atomically() {
assert_eq!(
m.write_u64(A + 0xFFC, u64::MAX),
Err(MemoryError::PermissionViolation {
address: A + 0xFFC,
address: A + 0x1000,
required: "write"
})
);
@@ -123,7 +123,7 @@ fn loader_bypass_ignores_permissions_but_not_mapping() {
assert_eq!(m.read_u32(A), Ok(7));
assert_eq!(
m.write_u64_bypass(A + 0x1FFC, 0),
Err(MemoryError::UnmappedAddress(A + 0x1FFC))
Err(MemoryError::UnmappedAddress(A + 0x2000))
);
}