Compare commits

...
Author SHA1 Message Date
Vedant Dadhich 9a29bd9c37 fix(loader): raise GP for non-canonical data addresses
Check canonical 48-bit address ranges in shared interpreter accesses, 16-byte SIMD paths, and every native DBT memory helper. Enable and expand the architectural known-answer group across operand widths, vector accesses, segment-base wrapping, boundary crossings, and valid canonical endpoints.

Signed-off-by: Vedant Dadhich <vedantdadhich21@gmail.com>
2026-11-25 03:21:14 +05:30
6 changed files with 144 additions and 12 deletions

No files matched your search

+1
View File
@@ -25,6 +25,7 @@ remains unsupported.
- Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits.
### Fixed
- Non-canonical 64-bit guest data addresses now raise `#GP` instead of a memory fault in both the interpreter and native DBT, including accesses that cross the canonical-address boundary. The synthetic architectural test is enabled again.
- Made the native semantic-review timeout in `scripts/test-gpu.sh` configurable with `XENORA_SEMANTIC_TIMEOUT` and raised its default from 120 to 300 seconds, so cold Apple Silicon runs do not fail while the expanded Metal known-answer suite is still progressing.
- Added a Python 3 fallback for `timeout` in `scripts/test-gpu.sh`, so the native Metal suite runs on stock macOS without GNU coreutils while preserving bounded execution and the usual timeout exit status.
+1 -1
View File
@@ -167,7 +167,7 @@ pub enum ExitReason {
StackUnderflow,
/// Division by zero or a quotient too large for its destination (#DE).
DivideError,
/// A misaligned 16-byte operand or a reserved MXCSR bit (#GP).
/// A non-canonical data address, misaligned 16-byte operand or reserved MXCSR bit (#GP).
GeneralProtection,
/// An unmasked SSE floating-point exception (#XM); MXCSR holds its flags.
SimdException,
+38
View File
@@ -1234,11 +1234,22 @@ struct BlockContext {
const BLOCK_CTX_FAULTED_OFFSET: u32 = 8;
const _: () = assert!(std::mem::offset_of!(BlockContext, faulted) == 8);
fn check_guest_address(ctx: *mut BlockContext, address: u64, size: u8) -> bool {
if x86_exec::canonical_access(address, size) {
return true;
}
fault(ctx, ExitReason::GeneralProtection);
false
}
extern "C" fn write_u64_helper(ctx: *mut BlockContext, addr: u64, val: u64) {
unsafe {
if (*ctx).exit_reason.is_some() {
return;
}
if !check_guest_address(ctx, addr, 8) {
return;
}
let mem = &mut *(*ctx).memory_ptr;
if let Err(e) = mem.write_u64(addr, val) {
(*ctx).faulted = 1;
@@ -1252,6 +1263,9 @@ extern "C" fn read_u64_helper(ctx: *mut BlockContext, addr: u64) -> u64 {
if (*ctx).exit_reason.is_some() {
return 0;
}
if !check_guest_address(ctx, addr, 8) {
return 0;
}
let mem = &*(*ctx).memory_ptr;
match mem.read_u64(addr) {
Ok(v) => v,
@@ -1295,6 +1309,9 @@ fn fault(ctx: *mut BlockContext, reason: ExitReason) {
/// Read 16 bytes at any alignment; returned in X0 (low) and X1 (high).
extern "C" fn read_u128_helper(ctx: *mut BlockContext, addr: u64) -> u128 {
if !check_guest_address(ctx, addr, 16) {
return 0;
}
let memory = unsafe { &*(*ctx).memory_ptr };
let mut bytes = [0; 16];
if let Err(error) = memory.read_bytes(addr, &mut bytes) {
@@ -1313,6 +1330,9 @@ extern "C" fn read_aligned_u128_helper(ctx: *mut BlockContext, addr: u64) -> u12
/// Write 16 bytes at any alignment; `value` arrives in X2 (low) and X3 (high).
extern "C" fn write_u128_helper(ctx: *mut BlockContext, addr: u64, value: u128) {
if !check_guest_address(ctx, addr, 16) {
return;
}
let memory = unsafe { &mut *(*ctx).memory_ptr };
if let Err(error) = memory.write_bytes(addr, &value.to_le_bytes()) {
fault(ctx, ExitReason::MemoryFault(error));
@@ -1360,6 +1380,9 @@ extern "C" fn write_u16_helper(ctx: *mut BlockContext, addr: u64, val: u64) {
if (*ctx).exit_reason.is_some() {
return;
}
if !check_guest_address(ctx, addr, 2) {
return;
}
let mem = &mut *(*ctx).memory_ptr;
if let Err(e) = mem.write_u16(addr, val as u16) {
(*ctx).faulted = 1;
@@ -1373,6 +1396,9 @@ extern "C" fn write_u32_helper(ctx: *mut BlockContext, addr: u64, val: u64) {
if (*ctx).exit_reason.is_some() {
return;
}
if !check_guest_address(ctx, addr, 4) {
return;
}
let mem = &mut *(*ctx).memory_ptr;
if let Err(e) = mem.write_u32(addr, val as u32) {
(*ctx).faulted = 1;
@@ -1386,6 +1412,9 @@ extern "C" fn read_u32_helper(ctx: *mut BlockContext, addr: u64) -> u64 {
if (*ctx).exit_reason.is_some() {
return 0;
}
if !check_guest_address(ctx, addr, 4) {
return 0;
}
let mem = &*(*ctx).memory_ptr;
match mem.read_u32(addr) {
Ok(v) => u64::from(v),
@@ -1403,6 +1432,9 @@ extern "C" fn write_u8_helper(ctx: *mut BlockContext, addr: u64, val: u64) {
if (*ctx).exit_reason.is_some() {
return;
}
if !check_guest_address(ctx, addr, 1) {
return;
}
let mem = &mut *(*ctx).memory_ptr;
if let Err(e) = mem.write_u8(addr, val as u8) {
(*ctx).faulted = 1;
@@ -1416,6 +1448,9 @@ extern "C" fn read_u8_helper(ctx: *mut BlockContext, addr: u64) -> u64 {
if (*ctx).exit_reason.is_some() {
return 0;
}
if !check_guest_address(ctx, addr, 1) {
return 0;
}
let mem = &*(*ctx).memory_ptr;
match mem.read_u8(addr) {
Ok(v) => v as u64,
@@ -1433,6 +1468,9 @@ extern "C" fn read_u16_helper(ctx: *mut BlockContext, addr: u64) -> u64 {
if (*ctx).exit_reason.is_some() {
return 0;
}
if !check_guest_address(ctx, addr, 2) {
return 0;
}
let mem = &*(*ctx).memory_ptr;
match mem.read_u16(addr) {
Ok(v) => v as u64,
+26
View File
@@ -49,6 +49,26 @@ pub fn address(cpu: &CpuState, operand: &MemOperand) -> u64 {
base.wrapping_add(offset(cpu, operand))
}
/// Whether a 64-bit guest address is canonical under the x86-64 48-bit
/// virtual-address rule (Intel SDM Vol. 1, section 3.3.7.1).
pub(crate) fn is_canonical(address: u64) -> bool {
matches!(address >> 47, 0 | 0x1_ffff)
}
/// Whether every byte of a data access has a canonical address. An overflowing
/// range is left to [`GuestMemory`] so it keeps its existing overflow fault.
pub(crate) fn canonical_access(address: u64, size: u8) -> bool {
if !is_canonical(address) {
return false;
}
if size <= 1 {
return true;
}
address
.checked_add(u64::from(size) - 1)
.is_none_or(is_canonical)
}
/// Effective address without a segment base, as LEA computes it.
fn offset(cpu: &CpuState, operand: &MemOperand) -> u64 {
let base = operand.base.map_or(0, |index| cpu.gpr[usize::from(index)]);
@@ -78,6 +98,9 @@ pub(crate) fn write_reg(cpu: &mut CpuState, reg: RegOperand, size: u8, value: u6
}
pub(crate) fn read_mem(memory: &GuestMemory, address: u64, size: u8) -> Result<u64, ExitReason> {
if !canonical_access(address, size) {
return Err(ExitReason::GeneralProtection);
}
match size {
1 => memory.read_u8(address).map(u64::from),
2 => memory.read_u16(address).map(u64::from),
@@ -93,6 +116,9 @@ pub(crate) fn write_mem(
size: u8,
value: u64,
) -> Result<(), ExitReason> {
if !canonical_access(address, size) {
return Err(ExitReason::GeneralProtection);
}
match size {
1 => memory.write_u8(address, value as u8),
2 => memory.write_u16(address, value as u16),
+9 -1
View File
@@ -8,7 +8,9 @@
use crate::cpu::{CpuState, ExitReason, Mxcsr};
use crate::memory::GuestMemory;
use crate::x86_decode::{Encoding, Insn, RegOperand, RmOperand};
use crate::x86_exec::{address, read_mem, read_reg, rm_operand, write_mem, write_reg};
use crate::x86_exec::{
address, canonical_access, read_mem, read_reg, rm_operand, write_mem, write_reg,
};
use crate::x86_float;
use crate::x86_table::{FORMS, Op};
@@ -216,6 +218,9 @@ fn read_vector(memory: &GuestMemory, address: u64, size: u8) -> Result<u128, Exi
if size < 16 {
return read_mem(memory, address, size).map(u128::from);
}
if !canonical_access(address, size) {
return Err(ExitReason::GeneralProtection);
}
let mut bytes = [0; 16];
memory
.read_bytes(address, &mut bytes)
@@ -232,6 +237,9 @@ fn write_vector(
if size < 16 {
return write_mem(memory, address, size, value as u64);
}
if !canonical_access(address, size) {
return Err(ExitReason::GeneralProtection);
}
memory
.write_bytes(address, &value.to_le_bytes())
.map_err(ExitReason::MemoryFault)
+69 -10
View File
@@ -1547,15 +1547,75 @@ store8_crossing_into_unmapped_page_writes_nothing: rdi=U rax=1122334455667788 =>
movups_load_crossing_into_unmapped: rsi=U => ! PFR U
";
const DEFECT_NONCANONICAL_ADDRESS: &str = r"
const NONCANONICAL_ADDRESS: &str = r"
@ movzx eax, byte ptr [rsi]
!0fb606
SDM 1 3.3.7.1; 2A MOVZX
noncanonical_byte_load_faults: rsi=0000800000000000 => ! GP
@ mov ax, [rsi]
!668b06
SDM 1 3.3.7.1; 2A MOV
noncanonical_word_load_faults: rsi=0000800000000000 => ! GP
@ mov eax, [rsi]
!8b06
SDM 1 3.3.7.1; 2A MOV
noncanonical_dword_load_faults: rsi=0000800000000000 => ! GP
@ mov rax, [rsi]
!488b06
SDM 1 3.3.7.1; 3A 7.15 Interrupt 13
noncanonical_load_faults: rsi=100000000000000 => ! GP
@ mov [rdi], rax
SDM 1 3.3.7.1; 2A MOV
noncanonical_qword_load_faults: rsi=0000800000000000 => ! GP
@ mov byte ptr [rdi], al
!8807
SDM 1 3.3.7.1; 2A MOV
noncanonical_byte_store_faults: rdi=ffff7fffffffffff => ! GP
@ mov word ptr [rdi], ax
!668907
SDM 1 3.3.7.1; 2A MOV
noncanonical_word_store_faults: rdi=ffff7fffffffffff => ! GP
@ mov dword ptr [rdi], eax
!8907
SDM 1 3.3.7.1; 2A MOV
noncanonical_dword_store_faults: rdi=ffff7fffffffffff => ! GP
@ mov qword ptr [rdi], rax
!488907
SDM 1 3.3.7.1; 3A 7.15 Interrupt 13
noncanonical_store_faults: rdi=7fffffffffffffff => ! GP
SDM 1 3.3.7.1; 2A MOV
noncanonical_qword_store_faults: rdi=ffff7fffffffffff => ! GP
@ mov rax, [rsi]
!488b06
SDM 1 3.3.7.1; 2A MOV
qword_crossing_into_lower_hole_faults: rsi=00007fffffffffff => ! GP
qword_ending_at_last_lower_canonical_byte_is_unmapped: rsi=00007ffffffffff8 => ! PFR 00007ffffffffff8
@ movzx eax, byte ptr [rsi]
!0fb606
SDM 1 3.3.7.1; 2A MOVZX
last_lower_canonical_byte_is_unmapped: rsi=00007fffffffffff => ! PFR 00007fffffffffff
first_upper_canonical_byte_is_unmapped: rsi=ffff800000000000 => ! PFR ffff800000000000
@ movups xmm0, [rsi]
!0f1006
SDM 1 3.3.7.1; 2B MOVUPS
noncanonical_vector_load_faults: rsi=0000800000000000 => ! GP
vector_crossing_into_lower_hole_faults: rsi=00007fffffffffff => ! GP
vector_ending_at_last_lower_canonical_byte_is_unmapped: rsi=00007ffffffffff0 => ! PFR 00007ffffffffff0
@ movups [rdi], xmm0
!0f1107
SDM 1 3.3.7.1; 2B MOVUPS
noncanonical_vector_store_faults: rdi=ffff7fffffffffff => ! GP
vector_store_at_first_upper_canonical_byte_is_unmapped: rdi=ffff800000000000 => ! PFR ffff800000000000
@ movdqu xmm0, [rsi]
!f30f6f06
SDM 1 3.3.7.1; 2B MOVDQU
movdqu_crossing_into_lower_hole_faults: rsi=00007fffffffffff => ! GP
movdqu_at_first_upper_canonical_byte_is_unmapped: rsi=ffff800000000000 => ! PFR ffff800000000000
@ movdqu [rdi], xmm0
!f30f7f07
SDM 1 3.3.7.1; 2B MOVDQU
movdqu_store_crossing_into_lower_hole_faults: rdi=00007fffffffffff => ! GP
@ mov rax, gs:[rsi]
!65488b06
SDM 1 3.3.7.1, 3A 4.2; 2A MOV
wrapped_segment_address_is_checked: gsbase=0000800000000000 rsi=ffffffffffffffff => ! GP
noncanonical_segment_base_faults: gsbase=0000800000000000 rsi=0 => ! GP
canonical_upper_segment_base_is_unmapped: gsbase=ffff800000000000 rsi=0 => ! PFR ffff800000000000
";
const PACKED_INTEGER_SSE2: &str = r"
@@ -2676,7 +2736,7 @@ const GROUPS: &[(&str, &str)] = &[
("fences", FENCES),
("memory_faults", MEMORY_FAULTS),
("straddling_fault_address", STRADDLING_FAULT_ADDRESS),
("defect_noncanonical_address", DEFECT_NONCANONICAL_ADDRESS),
("noncanonical_address", NONCANONICAL_ADDRESS),
("packed_integer_sse2", PACKED_INTEGER_SSE2),
("packed_integer_beyond_sse2", PACKED_INTEGER_BEYOND_SSE2),
("fp_rounding", FP_ROUNDING),
@@ -3468,9 +3528,8 @@ fn straddling_fault_address() {
}
#[test]
#[ignore = "known defect (#155): a non-canonical address is a memory fault, not #GP"]
fn defect_noncanonical_address() {
check_group(DEFECT_NONCANONICAL_ADDRESS);
fn noncanonical_address() {
check_group(NONCANONICAL_ADDRESS);
}
#[test]