mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Merge pull request #270 from MacosPorters/archive/pull/374/head
Fix ARM64 variable shift register encoding
This commit is contained in:
2 files changed
+50
-7
No files matched your search
@@ -240,6 +240,7 @@ remains unsupported.
|
||||
- Fixed `DuplicateHandle` and `GetThreadId` naming the main thread from other guest threads, `OwningThread` in critical sections always holding the main thread's ID, the callback-depth limit being shared by all guest threads, and pending one-time initializations being unbounded.
|
||||
- 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.
|
||||
- Fixed the native dependency audit reading Cargo JSON and inspection-tool output in the process locale encoding; it now reads and reports UTF-8 paths and diagnostics under an ASCII locale (#363).
|
||||
- Fixed a latent ARM64 `LSLV` encoding bug that forced bit 1 of the shift-count register field (part of #131). The dynamic binary translator uses `X10` in its only call, which already has that bit set. `RBIT`, `CLZ` and `LSLV` now panic on out-of-range register numbers before emitting an instruction. Encoding tests use words checked against LLVM's assembler.
|
||||
- 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 TLS callback metadata that was accepted or misread without an error (#78). A non-zero `AddressOfCallbacks` outside the image's sections, or 4 GiB or more above the image base, is now refused; it used to mean "no callbacks" or read the list from an unrelated place. The callback list must reach its null entry inside its section, where the section's zero-filled tail counts, and a list of more than 64 callbacks is refused where it used to be cut short. `execute_tls_callbacks` refuses a stack top that is not 16-byte aligned or cannot hold the entry frame, which used to overflow a subtraction, and a file cut inside the TLS data-directory entry no longer panics the parser. `TlsError` gains four variants for these cases. Twelve unit tests in `crates/loader/src/tls.rs` use original headers and code, and also check the argument registers, the stack pointer at entry and after return, callback order and the instruction budget. Expectations come from Microsoft's public PE Format and x64 calling convention pages; nothing ran on Windows, and independent review is pending.
|
||||
- Fixed the thread's TLS pointer naming the TLS data itself (#76). Compiled code loads the pointer from the thread environment block, indexes it by the module's TLS index to get the address of the data block, and then adds a variable's offset; the loader had mapped the data where the pointer array belongs, so the first eight bytes of the template were used as the block address. `setup_tls` now maps a pointer array at the thread's TLS base and the data block one page above it, and stores the block address in the slot of the index it assigns. `crates/loader/tests/tls_layout.rs` follows that access sequence through guest memory for a template with zero fill, a rebased image and a zero-fill-only directory. The data block of the fixture harness can now be at most 60 KiB, down from 64 KiB, before it would meet the thread environment block. Alignment requests above 4 KiB and more than one module's TLS are not handled. The access sequence follows Microsoft's public PE Format page; nothing ran on Windows, and independent review is pending.
|
||||
|
||||
@@ -307,23 +307,27 @@ impl Arm64Assembler {
|
||||
}
|
||||
|
||||
/// RBIT Xd, Xn: reverse the bit order of the source.
|
||||
/// Panics if a register number exceeds 31.
|
||||
pub fn rbit_64(&mut self, xd: u8, xn: u8) {
|
||||
self.emit(0xdac0_0000 | ((u32::from(xn) & 0x1f) << 5) | (u32::from(xd) & 0x1f));
|
||||
assert!(xd <= XZR && xn <= XZR, "RBIT register must be in 0..=31");
|
||||
self.emit(0xdac0_0000 | (u32::from(xn) << 5) | u32::from(xd));
|
||||
}
|
||||
|
||||
/// CLZ Xd, Xn: count the leading zero bits of the source.
|
||||
/// Panics if a register number exceeds 31.
|
||||
pub fn clz_64(&mut self, xd: u8, xn: u8) {
|
||||
self.emit(0xdac0_1000 | ((u32::from(xn) & 0x1f) << 5) | (u32::from(xd) & 0x1f));
|
||||
assert!(xd <= XZR && xn <= XZR, "CLZ register must be in 0..=31");
|
||||
self.emit(0xdac0_1000 | (u32::from(xn) << 5) | u32::from(xd));
|
||||
}
|
||||
|
||||
/// LSLV Xd, Xn, Xm: shift the source left by the amount in Xm.
|
||||
/// Panics if a register number exceeds 31.
|
||||
pub fn lslv_64(&mut self, xd: u8, xn: u8, xm: u8) {
|
||||
self.emit(
|
||||
0x9ac2_2000
|
||||
| ((u32::from(xm) & 0x1f) << 16)
|
||||
| ((u32::from(xn) & 0x1f) << 5)
|
||||
| (u32::from(xd) & 0x1f),
|
||||
assert!(
|
||||
xd <= XZR && xn <= XZR && xm <= XZR,
|
||||
"LSLV register must be in 0..=31"
|
||||
);
|
||||
self.emit(0x9ac0_2000 | (u32::from(xm) << 16) | (u32::from(xn) << 5) | u32::from(xd));
|
||||
}
|
||||
|
||||
/// LDR Qt, [Xn, #offset] (offset a multiple of 16)
|
||||
@@ -831,6 +835,44 @@ mod tests {
|
||||
assert_eq!(branch.instructions()[0], 0x14000005);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rbit_clz_lslv_encodings_match_llvm() {
|
||||
// A64 words from Apple Clang's integrated assembler and llvm-objdump.
|
||||
let mut asm = Arm64Assembler::new();
|
||||
asm.rbit_64(X10, X9); // rbit x10, x9
|
||||
asm.rbit_64(X0, XZR); // rbit x0, xzr
|
||||
asm.clz_64(X10, X10); // clz x10, x10
|
||||
asm.clz_64(XZR, X30); // clz xzr, x30
|
||||
asm.lslv_64(X11, X11, X10); // lslv x11, x11, x10
|
||||
asm.lslv_64(X0, X1, X0); // lslv x0, x1, x0
|
||||
asm.lslv_64(XZR, X30, XZR); // lslv xzr, x30, xzr
|
||||
assert_eq!(
|
||||
asm.instructions(),
|
||||
[
|
||||
0xdac0012a, 0xdac003e0, 0xdac0114a, 0xdac013df, 0x9aca216b, 0x9ac02020, 0x9adf23df,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rbit_clz_lslv_reject_out_of_range_registers() {
|
||||
let invalid_operands: [fn(&mut Arm64Assembler); 7] = [
|
||||
|asm| asm.rbit_64(32, X0),
|
||||
|asm| asm.rbit_64(X0, 32),
|
||||
|asm| asm.clz_64(32, X0),
|
||||
|asm| asm.clz_64(X0, 32),
|
||||
|asm| asm.lslv_64(32, X0, X0),
|
||||
|asm| asm.lslv_64(X0, 32, X0),
|
||||
|asm| asm.lslv_64(X0, X0, 32),
|
||||
];
|
||||
for (index, emit) in invalid_operands.into_iter().enumerate() {
|
||||
let mut asm = Arm64Assembler::new();
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| emit(&mut asm)));
|
||||
assert!(result.is_err(), "invalid operand case {index} was accepted");
|
||||
assert!(asm.instructions().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsigned_offset_stores_scale_by_access_size() {
|
||||
let mut asm = Arm64Assembler::new();
|
||||
|
||||
Reference in new issue
Block a user