loader: raise #GP for instructions longer than 15 bytes (#384)

The shared decoder already stopped before fetching a 16th byte, but it
reported the instruction as unsupported, so both engines exited with an
illegal instruction. It now returns DecodeError::TooLong, which maps to
ExitReason::GeneralProtection (Intel SDM Vol. 3A, 7.15). The DBT retries a
block that fails this way one instruction at a time, as it already does for
illegal instructions and fetch faults, so earlier instructions in the block
complete and the fault reports the overlong instruction's RIP.

The known answer recorded as a defect by #155 is enabled, with three more
cases, and two page-end tests check that no 16th byte is fetched.

Signed-off-by: Bartosz Szczepański <bartosz.szczepanski@hotmail.com>
This commit is contained in:
Bartosz Szczepański committed 2026-11-24 23:16:49 +01:00
1 parent 3b47c21bb0
commit 0d6cef6c4e
6 files changed
+87 -23

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
- 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.
+2
View File
@@ -470,6 +470,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,
}
}
}
+3 -1
View File
@@ -127,7 +127,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);
+7 -11
View File
@@ -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
@@ -200,24 +201,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)
}
@@ -256,12 +257,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;
+16 -5
View File
@@ -2613,11 +2613,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"
@@ -2688,7 +2700,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),
];
@@ -3529,9 +3541,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]
+58 -6
View File
@@ -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));
}
}