From 78df3a5c6b82d4962173ce25f6459281f0f5e0f9 Mon Sep 17 00:00:00 2001 From: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:24:46 +0530 Subject: [PATCH] loader: extend the SIB index register with REX.X, not REX.R (#63) The interpreter used REX.R to extend the SIB index field and ignored REX.X entirely, so the two paths disagreed with the native DBT translator, which already extends the index with REX.X. Two defects are fixed in the SIB branch of the MOV r/m effective-address computation: - The index register is now extended by REX.X. REX.R extends the ModRM reg field and must not affect the index. - A SIB index field of 100b selects r12 when REX.X is set, and means "no index register" only when REX.X is clear. REX.X is decoded from the REX prefix into the typed Prefixes struct. Add regression tests covering an extended index register including r12, an independent REX.R and REX.X combination, both prefix bits set together, and the no-index case. Every encoding was produced by LLVM 23.1.6 llvm-mc and the expected addresses were derived from the Intel SDM SIB rules. The three REX.X tests fail against the unfixed interpreter and pass with the fix; the no-index test passes in both cases. Closes #63 Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com> --- crates/loader/src/cpu.rs | 11 +- crates/loader/tests/bootstrap.rs | 175 ++++++++++++++++++++++++++++++- 2 files changed, 183 insertions(+), 3 deletions(-) diff --git a/crates/loader/src/cpu.rs b/crates/loader/src/cpu.rs index 3a58846..2943d96 100644 --- a/crates/loader/src/cpu.rs +++ b/crates/loader/src/cpu.rs @@ -64,6 +64,8 @@ struct Prefixes { rex_w: bool, /// REX.R: extends the ModRM `reg` field to R8-R15. rex_r: bool, + /// REX.X: extends the SIB `index` field to R8-R15. + rex_x: bool, /// REX.B: extends the ModRM `rm` field to R8-R15. rex_b: bool, /// The GS segment override prefix (0x65) is present. @@ -453,6 +455,7 @@ impl<'a> Interpreter<'a> { if (byte & 0xF0) == 0x40 { prefixes.rex_w = (byte & 0x08) != 0; prefixes.rex_r = (byte & 0x04) != 0; + prefixes.rex_x = (byte & 0x02) != 0; prefixes.rex_b = (byte & 0x01) != 0; byte = self.fetch_u8()?; } @@ -762,7 +765,11 @@ impl<'a> Interpreter<'a> { // SIB byte follows ModR/M let sib = self.fetch_u8()?; let scale_shift = (sib >> 6) & 3; - let index_raw = ((sib >> 3) & 7) + if prefixes.rex_r { 8 } else { 0 }; // SIB.index can use REX.X; here index_raw + // REX.X extends the SIB index field; REX.R extends the ModRM reg + // field and must not affect the index. An index field of 100b + // selects r12 when REX.X is set, and means "no index" otherwise. + let index_field = (sib >> 3) & 7; + let index_raw = index_field + if prefixes.rex_x { 8 } else { 0 }; let base_raw = (sib & 7) + if prefixes.rex_b { 8 } else { 0 }; let base_v = if mode == 0 && (sib & 7) == 5 { @@ -772,7 +779,7 @@ impl<'a> Interpreter<'a> { self.cpu.get(Register::from_index(base_raw)) }; - let index_v = if (sib >> 3) & 7 == 4 { + let index_v = if index_field == 4 && !prefixes.rex_x { // index == 4 means no index register (ESP/RSP) 0u64 } else { diff --git a/crates/loader/tests/bootstrap.rs b/crates/loader/tests/bootstrap.rs index 9176b32..2573c70 100644 --- a/crates/loader/tests/bootstrap.rs +++ b/crates/loader/tests/bootstrap.rs @@ -3,7 +3,7 @@ //! End-to-end integration tests for Xenora X12: PE32+ loading & x86-64 execution bootstrap. use xenora_loader::cpu::{CpuState, ExitReason, Interpreter, Register}; -use xenora_loader::memory::{MemoryError, MemoryPermissions}; +use xenora_loader::memory::{GuestMemory, MemoryError, MemoryPermissions}; use xenora_loader::pe::{ IMAGE_FILE_MACHINE_AMD64, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ, PE32_PLUS_MAGIC, ParsedPe, }; @@ -289,3 +289,176 @@ fn test_sib_addressing_mode() { assert_eq!(result.exit_reason, ExitReason::Halt); assert_eq!(result.final_state.get(Register::Rdx), 0x12345678_9abcdef0); } + +/// Base address of the read-write data region used by the SIB tests. +const DATA_BASE: u64 = 0x6000_0000; +/// Base address of the read-write stack region used by the SIB tests. +const STACK_BASE: u64 = 0x5000_0000; + +/// Run `code` on the interpreter with a writable stack and a writable data +/// region, and return the final state, the exit reason and the guest memory. +/// +/// `DATA_BASE` is mapped read-write so a test can observe where a store +/// landed. `STACK_BASE` holds the stack pointer. +fn run_with_regions(code: &[u8]) -> (CpuState, ExitReason, GuestMemory) { + let pe_bytes = build_synthetic_pe32_plus(code); + let parsed = ParsedPe::parse(&pe_bytes).expect("parse"); + let mut loaded = parsed.load_into_memory(&pe_bytes).expect("load"); + + loaded + .memory + .map_region(STACK_BASE, 0x10000, MemoryPermissions::READ_WRITE) + .expect("stack mapping should succeed"); + loaded + .memory + .map_region(DATA_BASE, 0x10000, MemoryPermissions::READ_WRITE) + .expect("data mapping should succeed"); + + let cpu = CpuState::new(loaded.entry_point, STACK_BASE + 0x8000); + let mut interp = Interpreter::new(cpu, &mut loaded.memory, 100); + let result = interp.run(); + (result.final_state, result.exit_reason, loaded.memory) +} + +/// `mov rax, DATA_BASE` (48 B8 ). +fn mov_rax_data_base() -> Vec { + let mut code = vec![0x48, 0xB8]; + code.extend_from_slice(&DATA_BASE.to_le_bytes()); + code +} + +/// `mov rbx, 0x12345678_9abcdef0` (48 BB ). +fn mov_rbx_value() -> Vec { + let mut code = vec![0x48, 0xBB]; + code.extend_from_slice(&0x1234_5678_9abc_def0u64.to_le_bytes()); + code +} + +/// A SIB index register is extended by REX.X, not by REX.R. +/// +/// The encoding 4A 89 1C E0 is `mov [rax + r12*8], rbx`. Its REX prefix sets +/// REX.X, so the SIB index field 100b selects r12 and the store lands at +/// `rax + r12*8`. REX.R is clear in this prefix, so the ModRM `reg` field is +/// rbx. +#[test] +fn test_rex_x_extends_the_sib_index_register() { + // mov rax, DATA_BASE (48 b8 ) + // mov r12, 4 (41 bc 04 00 00 00) + // mov rbx, 0x12345678_9abcdef0 (48 bb ) + // mov [rax + r12*8], rbx (4a 89 1c e0) + // hlt (f4) + let mut code = mov_rax_data_base(); + code.extend_from_slice(&[0x41, 0xBC, 0x04, 0x00, 0x00, 0x00]); // mov r12d, 4 + code.extend_from_slice(&mov_rbx_value()); + code.extend_from_slice(&[0x4A, 0x89, 0x1C, 0xE0]); // mov [rax + r12*8], rbx + code.push(0xF4); // hlt + + let (_state, reason, memory) = run_with_regions(&code); + assert_eq!(reason, ExitReason::Halt); + // r12 holds 4, so the store lands at DATA_BASE + 4*8. + assert_eq!( + memory.read_u64(DATA_BASE + 32).expect("indexed slot"), + 0x1234_5678_9abc_def0 + ); + // The unindexed slot at DATA_BASE must be untouched. + assert_eq!(memory.read_u64(DATA_BASE).expect("base slot"), 0); +} + +/// REX.R and REX.X extend independent fields in the same instruction. +/// +/// The encoding 4C 89 1C C8 is `mov [rax + rcx*8], r11`. Its REX prefix sets +/// REX.R, so the ModRM `reg` field 011 selects r11. It does not set REX.X, so +/// the SIB index field 001 selects rcx and must not be extended to r9. +#[test] +fn test_rex_r_does_not_extend_the_sib_index_register() { + // mov rax, DATA_BASE (48 b8 ) + // mov r11, 0x11223344_55667788 (49 bb ) + // mov ecx, 4 (b9 04 00 00 00) + // mov r9, 8 (41 b9 08 00 00 00) + // mov [rax + rcx*8], r11 (4c 89 1c c8) + // hlt (f4) + let mut code = mov_rax_data_base(); + let mut r11 = vec![0x49, 0xBB]; + r11.extend_from_slice(&0x1122_3344_5566_7788u64.to_le_bytes()); + code.extend_from_slice(&r11); + code.extend_from_slice(&[0xB9, 0x04, 0x00, 0x00, 0x00]); // mov ecx, 4 + code.extend_from_slice(&[0x41, 0xB9, 0x08, 0x00, 0x00, 0x00]); // mov r9d, 8 + code.extend_from_slice(&[0x4C, 0x89, 0x1C, 0xC8]); // mov [rax + rcx*8], r11 + code.push(0xF4); // hlt + + let (_state, reason, memory) = run_with_regions(&code); + assert_eq!(reason, ExitReason::Halt); + // rcx holds 4, so the store lands at DATA_BASE + 4*8. A wrongly extended + // index would have selected r9 and landed at DATA_BASE + 8*8. + assert_eq!( + memory.read_u64(DATA_BASE + 32).expect("indexed slot"), + 0x1122_3344_5566_7788 + ); + assert_eq!(memory.read_u64(DATA_BASE + 64).expect("r9 slot"), 0); +} + +/// REX.R and REX.X set together extend their own fields independently. +/// +/// The encoding 4E 89 1C E0 is `mov [rax + r12*8], r11`. Its REX prefix sets +/// both REX.R and REX.X: the ModRM `reg` field 011 becomes r11 and the SIB +/// index field 100b becomes r12. +#[test] +fn test_rex_r_and_rex_x_set_together_extend_independent_fields() { + // mov rax, DATA_BASE (48 b8 ) + // mov r11, 0x11223344_55667788 (49 bb ) + // mov r12, 4 (41 bc 04 00 00 00) + // mov [rax + r12*8], r11 (4e 89 1c e0) + // hlt (f4) + let mut code = mov_rax_data_base(); + let mut r11 = vec![0x49, 0xBB]; + r11.extend_from_slice(&0x1122_3344_5566_7788u64.to_le_bytes()); + code.extend_from_slice(&r11); + code.extend_from_slice(&[0x41, 0xBC, 0x04, 0x00, 0x00, 0x00]); // mov r12d, 4 + code.extend_from_slice(&[0x4E, 0x89, 0x1C, 0xE0]); // mov [rax + r12*8], r11 + code.push(0xF4); // hlt + + let (_state, reason, memory) = run_with_regions(&code); + assert_eq!(reason, ExitReason::Halt); + assert_eq!( + memory.read_u64(DATA_BASE + 32).expect("indexed slot"), + 0x1122_3344_5566_7788 + ); + assert_eq!(memory.read_u64(DATA_BASE).expect("base slot"), 0); +} + +/// Without REX.X, a SIB index field of 100b means "no index register" and the +/// address is the base alone. +/// +/// The encoding 48 89 1C 24 is `mov [rsp], rbx`: the SIB index field is 100b +/// and REX.X is clear, so no index register contributes to the address. +#[test] +fn test_sib_index_field_100b_without_rex_x_means_no_index() { + // mov rbx, 0x12345678_9abcdef0 (48 bb ) + // mov r12, 0x1000 (41 bc 00 10 00 00) + // mov [rsp], rbx (48 89 1c 24) + // hlt (f4) + let mut code = mov_rbx_value(); + code.extend_from_slice(&[0x41, 0xBC, 0x00, 0x10, 0x00, 0x00]); // mov r12d, 0x1000 + code.extend_from_slice(&[0x48, 0x89, 0x1C, 0x24]); // mov [rsp], rbx + code.push(0xF4); // hlt + + let pe_bytes = build_synthetic_pe32_plus(&code); + let parsed = ParsedPe::parse(&pe_bytes).expect("parse"); + let mut loaded = parsed.load_into_memory(&pe_bytes).expect("load"); + loaded + .memory + .map_region(STACK_BASE, 0x10000, MemoryPermissions::READ_WRITE) + .expect("stack mapping should succeed"); + + let cpu = CpuState::new(loaded.entry_point, STACK_BASE + 0x8000); + let mut interp = Interpreter::new(cpu, &mut loaded.memory, 100); + let result = interp.run(); + assert_eq!(result.exit_reason, ExitReason::Halt); + + // The index is absent, so the address is the base (rsp) alone. + let stack_top = STACK_BASE + 0x8000; + assert_eq!( + loaded.memory.read_u64(stack_top).expect("base slot"), + 0x1234_5678_9abc_def0 + ); +}