mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Merge pull request #274 from MacosPorters/archive/pull/386/head
loader: raise #GP for instructions longer than 15 bytes (#384)
This commit is contained in:
6 files changed
+87
-23
No files matched your search
@@ -34,6 +34,7 @@ remains unsupported.
|
||||
|
||||
### 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.
|
||||
- An instruction longer than 15 bytes now stops both engines with a general-protection fault instead of an illegal-instruction exit (#384; Intel SDM Vol. 3A, 7.15). The decoder still stops before fetching a 16th byte, and the DBT translates the faulting instruction on its own so the instructions before it in a block complete first. The `defect_overlong_instruction` known answer from #155 is now verified, with three more cases.
|
||||
- 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.
|
||||
|
||||
|
||||
@@ -483,6 +483,8 @@ impl From<DecodeError<MemoryError>> for ExitReason {
|
||||
match error {
|
||||
DecodeError::Fetch(fault) => Self::MemoryFault(fault),
|
||||
DecodeError::Unsupported(byte) => Self::IllegalInstruction(byte),
|
||||
// Intel SDM Vol. 3A, 7.15: exceeding the 15-byte limit is #GP(0).
|
||||
DecodeError::TooLong => Self::GeneralProtection,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,7 +184,9 @@ impl DbtEngine {
|
||||
if block_limit > 1
|
||||
&& matches!(
|
||||
block,
|
||||
Err(ExitReason::IllegalInstruction(_) | ExitReason::MemoryFault(_))
|
||||
Err(ExitReason::IllegalInstruction(_)
|
||||
| ExitReason::MemoryFault(_)
|
||||
| ExitReason::GeneralProtection)
|
||||
)
|
||||
{
|
||||
block = self.get_or_compile_block(cpu.rip, memory, 1);
|
||||
|
||||
@@ -14,8 +14,9 @@ use crate::x86_table::{
|
||||
/// Architectural x86-64 instruction length limit, in bytes.
|
||||
///
|
||||
/// An instruction may not exceed 15 bytes (Intel SDM Vol. 2A, "Instruction
|
||||
/// Length"). Sequences longer than this are not decodable and are rejected as
|
||||
/// illegal instructions.
|
||||
/// Length"). Decoding stops before a 16th byte would be fetched and reports
|
||||
/// [`DecodeError::TooLong`], a general-protection fault (Intel SDM Vol. 3A,
|
||||
/// 7.15).
|
||||
pub const MAX_INSTRUCTION_LEN: usize = 15;
|
||||
|
||||
/// Number of leading legacy segment prefix bytes (0x64 FS, 0x65 GS) at the
|
||||
@@ -204,24 +205,24 @@ pub enum DecodeError<E> {
|
||||
Fetch(E),
|
||||
/// No declared form matches; carries the byte that decided it.
|
||||
Unsupported(u8),
|
||||
/// The instruction would be longer than [`MAX_INSTRUCTION_LEN`] bytes.
|
||||
TooLong,
|
||||
}
|
||||
|
||||
struct Cursor<F> {
|
||||
fetch: F,
|
||||
rip: u64,
|
||||
len: usize,
|
||||
last: u8,
|
||||
}
|
||||
|
||||
impl<E, F: FnMut(u64) -> Result<u8, E>> Cursor<F> {
|
||||
fn u8(&mut self) -> Result<u8, DecodeError<E>> {
|
||||
if self.len == MAX_INSTRUCTION_LEN {
|
||||
return Err(DecodeError::Unsupported(self.last));
|
||||
return Err(DecodeError::TooLong);
|
||||
}
|
||||
let byte =
|
||||
(self.fetch)(self.rip.wrapping_add(self.len as u64)).map_err(DecodeError::Fetch)?;
|
||||
self.len += 1;
|
||||
self.last = byte;
|
||||
Ok(byte)
|
||||
}
|
||||
|
||||
@@ -260,12 +261,7 @@ pub fn decode<E>(
|
||||
rip: u64,
|
||||
fetch: impl FnMut(u64) -> Result<u8, E>,
|
||||
) -> Result<Insn, DecodeError<E>> {
|
||||
let mut cursor = Cursor {
|
||||
fetch,
|
||||
rip,
|
||||
len: 0,
|
||||
last: 0,
|
||||
};
|
||||
let mut cursor = Cursor { fetch, rip, len: 0 };
|
||||
let mut operand_size = false;
|
||||
let mut rep = false;
|
||||
let mut repne = false;
|
||||
|
||||
@@ -2814,11 +2814,23 @@ dpps_mask: x1=d:3f800000,40000000,40400000,40800000 x2=d:3f800000,3f800000,3f800
|
||||
movddup_movsldup_movshdup: x2=d:7f800001,2,3,4 => x1=d:7f800001,2,7f800001,2 x3=d:7f800001,7f800001,3,3 x4=d:2,2,4,4 f=*
|
||||
";
|
||||
|
||||
const DEFECT_OVERLONG_INSTRUCTION: &str = r"
|
||||
const INSTRUCTION_LENGTH: &str = r"
|
||||
@ .byte 0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x01,0xd8
|
||||
!666666666666666666666666666601d8
|
||||
SDM 3A 7.15 Interrupt 13 (instruction length over 15 bytes)
|
||||
sixteen_byte_redundant_operand_size_prefixes: => ! GP
|
||||
@ .byte 0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x05,0x34,0x12
|
||||
666666666666666666666666053412
|
||||
SDM 2A 2.2.1 (15-byte limit); 2A ADD
|
||||
fifteen_bytes_ending_in_the_immediate_execute: rax=ffffffffffff0001 => rax=ffffffffffff1235 f=cPazso
|
||||
@ .byte 0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x05,0x34,0x12
|
||||
!66666666666666666666666666053412
|
||||
SDM 3A 7.15 Interrupt 13 (instruction length over 15 bytes)
|
||||
sixteenth_byte_in_the_immediate_is_gp: rax=1 => ! GP
|
||||
@ mov eax, 1; .byte 0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x05,0x34,0x12
|
||||
b801000000 !66666666666666666666666666053412
|
||||
SDM 3A 7.15 Interrupt 13 (instruction length over 15 bytes)
|
||||
overlong_after_an_instruction_retires_it_first: rax=ffffffffffffffff => rax=1 ! GP
|
||||
";
|
||||
|
||||
const PAGE_CROSSING: &str = r"
|
||||
@@ -2889,7 +2901,7 @@ const GROUPS: &[(&str, &str)] = &[
|
||||
("fp_packed_and_sticky", FP_PACKED_AND_STICKY),
|
||||
("fp_mxcsr_and_unmasked", FP_MXCSR_AND_UNMASKED),
|
||||
("fp_beyond_sse2", FP_BEYOND_SSE2),
|
||||
("defect_overlong_instruction", DEFECT_OVERLONG_INSTRUCTION),
|
||||
("instruction_length", INSTRUCTION_LENGTH),
|
||||
("exact_unmasked_overflow", EXACT_UNMASKED_OVERFLOW),
|
||||
("page_crossing", PAGE_CROSSING),
|
||||
];
|
||||
@@ -3726,9 +3738,8 @@ fn fp_beyond_sse2() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "known defect (#155): an instruction over 15 bytes is illegal, not #GP"]
|
||||
fn defect_overlong_instruction() {
|
||||
check_group(DEFECT_OVERLONG_INSTRUCTION);
|
||||
fn instruction_length() {
|
||||
check_group(INSTRUCTION_LENGTH);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -178,7 +178,7 @@ fn interpreter_rejects_overlong_segment_prefix_run() {
|
||||
.unwrap();
|
||||
let cpu = CpuState::new(ENTRY, 0);
|
||||
let result = Interpreter::new(cpu, &mut memory, 8).run();
|
||||
assert_eq!(result.exit_reason, ExitReason::IllegalInstruction(0x65));
|
||||
assert_eq!(result.exit_reason, ExitReason::GeneralProtection);
|
||||
assert_eq!(result.instructions_executed, 0);
|
||||
assert_eq!(result.final_state.rip, ENTRY);
|
||||
}
|
||||
@@ -195,9 +195,61 @@ fn dbt_rejects_overlong_segment_prefix_run() {
|
||||
let result = DbtEngine::new(1024 * 1024)
|
||||
.expect("JIT memory allocation")
|
||||
.run(&mut cpu, &mut memory, 8);
|
||||
assert!(
|
||||
matches!(result.exit_reason, ExitReason::IllegalInstruction(_)),
|
||||
"expected IllegalInstruction, got {:?}",
|
||||
result.exit_reason
|
||||
);
|
||||
assert_eq!(result.exit_reason, ExitReason::GeneralProtection);
|
||||
assert_eq!(result.instructions_executed, 0);
|
||||
assert_eq!(cpu.rip, ENTRY);
|
||||
}
|
||||
|
||||
/// Runs `prefixes` bytes of 0x66 that end exactly at the end of a mapped page,
|
||||
/// with nothing mapped after it, on both engines.
|
||||
fn run_prefixes_at_page_end(prefixes: usize) -> [(ExitReason, u64, u64); 2] {
|
||||
const PAGE: u64 = 0x2000;
|
||||
const PAGE_LEN: usize = 0x1000;
|
||||
let mut data = vec![0x90; PAGE_LEN];
|
||||
data[PAGE_LEN - prefixes..].fill(0x66);
|
||||
let entry = PAGE + (PAGE_LEN - prefixes) as u64;
|
||||
let mut memory = GuestMemory::new();
|
||||
memory
|
||||
.map_region_with_data(PAGE, PAGE_LEN, MemoryPermissions::READ_EXECUTE, &data)
|
||||
.unwrap();
|
||||
|
||||
let interpreted = Interpreter::new(CpuState::new(entry, 0), &mut memory, 8).run();
|
||||
let mut cpu = CpuState::new(entry, 0);
|
||||
let translated = DbtEngine::new(1024 * 1024)
|
||||
.expect("JIT memory allocation")
|
||||
.run(&mut cpu, &mut memory, 8);
|
||||
[
|
||||
(
|
||||
interpreted.exit_reason,
|
||||
interpreted.final_state.rip,
|
||||
interpreted.instructions_executed,
|
||||
),
|
||||
(
|
||||
translated.exit_reason,
|
||||
cpu.rip,
|
||||
translated.instructions_executed,
|
||||
),
|
||||
]
|
||||
.map(|(exit, rip, count)| (exit, rip.wrapping_sub(entry), count as u64))
|
||||
}
|
||||
|
||||
/// Fifteen prefixes are already too long whatever follows, so the 16th byte is
|
||||
/// never fetched: #GP, not a fetch fault on the unmapped page after them
|
||||
/// (Intel SDM Vol. 3A, 7.15).
|
||||
#[test]
|
||||
fn overlong_instruction_is_gp_without_fetching_the_sixteenth_byte() {
|
||||
for (exit, rip_offset, count) in run_prefixes_at_page_end(MAX_INSTRUCTION_LEN) {
|
||||
assert_eq!(exit, ExitReason::GeneralProtection);
|
||||
assert_eq!((rip_offset, count), (0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/// Control for the test above: fourteen prefixes leave room for an opcode, so
|
||||
/// the fetch of the 15th byte is what fails.
|
||||
#[test]
|
||||
fn fourteen_prefixes_at_a_page_end_fault_on_the_fetch() {
|
||||
for (exit, rip_offset, count) in run_prefixes_at_page_end(MAX_INSTRUCTION_LEN - 1) {
|
||||
assert!(matches!(exit, ExitReason::MemoryFault(_)), "{exit:?}");
|
||||
assert_eq!((rip_offset, count), (0, 0));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user