Restore the six #280 regression tests that the merge resolving #280's
conflict with #283 removed. Add FS and GS RIP-relative loads and stores
with positive and negative displacements, a differential run comparing
the native DBT and interpreter registers and memory bytes, and
RIP-relative faults reporting the segment-adjusted address. Make the
existing differential test compare both paths' memory.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Keep the probe truncation test and the bounds check on section offsets from
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
#286, with imports on the shared parser.
Keep every upstream test and the new assembler forms. Keep the shared
decoder and lowering, which already cover the merged MOVZX, MOVSX and
segment fixes, and adopt upstream's rule that GS applies when FS and GS
overrides are combined. Add segment regressions for RIP-relative operands,
negative displacements and fault addresses in both engines.
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
Parse import directories with one bounded parser shared by the static probe
and the binder, and resolve imports through one registry lookup. Refuse
conflicting registrations, keep ordinals apart from names, match API sets
exactly, and bind only when every import resolves.
Generate typed Win64 adapters, metadata and a coverage report from a
declarative service registry. Pass floating-point arguments and results in
XMM registers, and stop the guest if a service changes a nonvolatile
register. Make unimplemented generated wrappers fail explicitly.
Verify 20 services by calling them from translated guest code through bound
import slots, and the parser on damaged tables.
Refs #201, #270
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
`probe_pe_bytes` panicked on a truncated PE instead of returning an error or
reporting the import as unreadable.
The cause was in `crates/loader/src/probe.rs`. `rva_to_offset` maps an RVA to
a file offset using the section header, so on a truncated file the computed
offset can point past the end of the buffer. The loop that finds the
terminating NUL then stopped at `bytes.len()`, leaving `no > end`, and the
slice `bytes[no..end]` panicked with "range start index 1632 out of range for
slice of length 1556".
Fix both name reads:
- `rva_to_offset` now returns `None` when the computed offset is at or past
the end of the file, because such an offset is not readable. This applies to
every caller, so the relocation, TLS, thunk and disassembly paths are
covered by the same rule.
- A new `read_c_string` helper reads a NUL-terminated string and returns
`None` when the string starts outside the buffer. It replaces the
hand-written scan in the DLL-name read and in the symbol-name read, which
also had an unchecked `ho + 2` for the two-byte import hint.
A name that cannot be read is reported as `UNKNOWN.dll` or `UNKNOWN_SYM`,
which is what the probe already did when the RVA did not resolve at all. The
probe stays read-only and still performs no guest execution.
Add a regression test that truncates the in-repo `fixtures/pe/trusted-runner.exe`
at every length from 1 to the file size and asserts that `probe_pe_bytes`
never panics. The reported range was 1,556 to 1,665 bytes; the sweep covers
the whole file, so it also covers the other name and thunk reads that the
report noted might share the pattern. The test fails against the unmodified
probe with the reported panic and passes with this change.
Closes#270
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Add the scalar and packed binary32 and binary64 arithmetic, compares and
conversions, computed exactly and rounded once in software. Follow MXCSR
rounding, sticky flags, denormals-are-zero, flush-to-zero, tininess after
rounding and x86 NaN selection. Stop on unmasked exceptions without a
result. Host floating point is not used, and no form is lowered to NEON.
Verify 6,824 vectors computed with exact fractions by an independent Python
reference in both engines, and round-to-nearest results against the host.
List the behaviour that is implemented but left unasserted.
Refs #127, #128
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
The native DBT translator implements the four extension opcodes MOVZX r32/r64,
r/m8 (0F B6), MOVZX r32/r64, r/m16 (0F B7), MOVSX r32/r64, r/m8 (0F BE) and
MOVSX r32/r64, r/m16 (0F BF), with both register and memory source operands.
The interpreter implemented none of them: its 0x0F handler covered only SYSCALL
(0F 05) and BSWAP (0F C8..CF), so any of the four opcodes exited with
IllegalInstruction and the two execution paths accepted different instruction
sets for the same bytes.
Implement them in the interpreter:
- The source width follows the opcode, so 0F B6 and 0F BE read one byte and
0F B7 and 0F BF read one word.
- The destination width follows REX.W. Without REX.W the destination is 32
bits and its upper half is cleared; with REX.W it is 64 bits.
- MOVZX zero-extends, so both destination widths hold the same value.
- MOVSX sign-extends to the destination width, so a 32-bit destination keeps
the sign in bits 31:0 with the upper half cleared, while a 64-bit
destination keeps the sign extension across all 64 bits.
The effective-address computation shared by the memory forms of MOV,
MOVZX and MOVSX is extracted into an `effective_address` method so both
instruction families use one implementation. That is a pure refactor: the
address computation itself is unchanged.
Add six regression tests in crates/loader/tests/bootstrap.rs covering the
register and the memory source forms independently, both destination widths,
and the sign-extension boundary values. Every encoding was produced by LLVM
23.1.6 llvm-mc -show-encoding and the expected values follow the Intel SDM
rules for MOVZX and MOVSX.
All six tests fail against the unmodified interpreter and pass with this
change.
A related defect in the native DBT translator was found while writing these
tests: its MOVSX paths sign-extend to 64 bits unconditionally, so a 32-bit
destination keeps the sign extension in bits 63:32 instead of having them
cleared. That belongs to #65, which permits the translator files, and is
reported there rather than fixed here.
Closes#66
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
The native DBT translator computed the RIP-relative address and passed it
straight to the memory helper, without adding the FS or GS segment base. The
non-RIP-relative path went through emit_effective_address, which does add it,
so the two paths in the same translator disagreed with each other, and the
interpreter in crates/loader/src/cpu.rs disagreed with both.
Per the Intel SDM Volume 3A, section 3.4.4, "When FS and GS segment overrides
are used in 64-bit mode, their respective base addresses are used in the
linear address calculation: (FS or GS).base + index + displacement", and the
AMD64 Architecture Programmer's Manual Volume 1, section 2.2.3.3, "Use of the
FS or GS prefix causes their respective segment bases to be added to the
effective address calculation." The base is added for every addressing form,
so the RIP-relative form needs it too.
The segment base is a runtime guest value, so it cannot be folded into the
translation-time immediate. The addition is factored into emit_segment_base,
which reads the base from the guest CPU state at run time, and is now called
from the RIP-relative MOV paths as well as from emit_effective_address.
Add six regression tests in the dbt test module:
- fs_segment_override_adds_the_fs_base_to_the_address: a segment-relative
store and load land at the FS base plus the displacement, not at the bare
displacement.
- gs_segment_override_adds_the_gs_base_to_the_address: the GS base is used,
and a distinct FS base is left untouched.
- rip_relative_segment_override_adds_the_segment_base: the RIP-relative form
adds the segment base. This test fails without the fix.
- segment_relative_access_to_an_unmapped_address_faults: a segment-relative
access outside the mapped regions faults and reports the segment-adjusted
address.
- interpreter_and_native_dbt_agree_on_segment_relative_addressing: the same
program runs through the interpreter and the native DBT and produces the same
state and the same memory writes.
- redundant_fs_and_gs_overrides_are_handled_the_same_by_both_paths: an
instruction carrying both an FS and a GS override is architecturally
undefined, so this pins the local rule that both paths prefer GS and that
they agree with each other.
Every encoding was produced by LLVM 23.1.6 llvm-mc -show-encoding, and the
expected addresses were derived from the SDM rule above.
Closes#72
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
The native DBT translator sign-extended to 64 bits unconditionally and stored
the full 64-bit result, so a 32-bit MOVSX destination kept the sign extension
in bits 63:32 instead of having them cleared.
Before, both MOVSX paths did this:
asm.sxtb_64(X9, X9); // or sxth_64
asm.str_uoff_64(X9, X19, (reg_idx as u32) * 8);
`sxtb_64` is `SBFM Xd, Xn, #0, #7`, which sign-extends the byte to 64 bits,
and `str_uoff_64` writes all 64 bits of the destination register.
That is correct for the REX.W form `movsbq rax, bl`, but the Intel SDM
requires a 32-bit result for the 32-bit destination form `movsbl eax, bl`. The
translator therefore left `rax = 0xFFFFFFFFFFFFFFFF` where the architecture
requires `rax = 0x00000000FFFFFFFF`.
Select the sign-extension width from REX.W:
- REX.W set: keep the 64-bit extension, which is already correct.
- REX.W clear: sign-extend to 32 bits, which also clears bits 63:32 of the
destination because the 32-bit form writes a W register.
This needs two new assembler instructions, `sxtb_32` and `sxth_32`, which are
the 32-bit forms of the existing `sxtb_64` and `sxth_64`:
sxtb_32: SBFM Wd, Wn, #0, #7 (0x13001c00)
sxth_32: SBFM Wd, Wn, #0, #15 (0x13003c00)
Both encodings were confirmed against LLVM 23.1.6 `llvm-mc -triple=aarch64
-show-encoding`.
The MOVZX paths are unchanged: a zero extension produces the same value at
both destination widths, because the 32-bit form already clears the upper half.
Add three regression tests in the dbt test module covering the register and
memory source forms and the byte and word sources, each at both destination
widths. Every encoding was produced by LLVM 23.1.6 llvm-mc -show-encoding and
the expected values follow the Intel SDM rules for MOVSX.
All three tests fail against the unmodified translator and pass with this
change.
Closes#65
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Add XMM0-XMM15 and MXCSR to the guest state and extend the registry with
mandatory prefixes, so the shared decoder covers the SSE and SSE2 moves and
packed integer operations. Reject opcodes where 0x66 could be both an
operand size and a mandatory prefix.
Translate operations with identical NEON lane semantics to ARM64; other
forms call the shared semantics and are counted. Raise a distinct
general-protection exit for misaligned 16-byte operands and reserved MXCSR
bits. Floating-point arithmetic is not implemented.
Verify 374 LLVM-assembled instructions against a byte-array reference that
reads only their assembly text, and mixed sequences between the engines.
Refs #124, #125, #126, #128
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
Add a declarative x86-64 instruction registry with a validating generator,
and decode through its tables in the static probe, the interpreter and the
native DBT. Move integer semantics into shared reviewed primitives and
extend them to 8-, 16-, 32- and 64-bit register and memory operands.
Store all six arithmetic flags in the guest state so they survive helper
calls and block boundaries, and correct unsigned branch conditions. Lower
common forms to ARM64; other forms call the shared semantics and are
counted. Report probe support from the decoder that executes.
Verify 985 LLVM-assembled instructions against a bit-level reference that
reads only their assembly text, and random sequences between the engines.
Refs #64, #65, #66, #68, #70, #71, #73, #118, #119, #120, #121, #122,
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
#123, #194, #195, #196
Implement MOVZX, MOVSX (0F B6/B7/BE/BF) and MOVSXD (63) for register and
checked memory operands in the interpreter and the native DBT, and report
their exact lengths in the static probe. Cover 16-, 32- and 64-bit
destinations, legacy high-byte registers and RIP-relative operands.
Fix the DBT SXTW encoding, 32-bit accesses that touched eight bytes,
RIP-relative byte and extension operands, and LEA with a segment prefix.
Make the prefix-length regression independent of an untracked file and
correct the campaign ledger.
Refs #65, #66, #67, #68, #69, #117
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
Add SXTW to the ARM64 assembler (encoding 0x93407D00 | Wn<<5 | Xd, verified
against llvm-mc 23.1.6) and a 0x63 arm to the DBT's compile_block that loads
the source word, emits SXTW into the destination register and stores it.
Sixth test executes the instruction through the native DBT.
Signed-off-by: Ranveer Soni <git@ranveersoni.me>
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>
Add the 0x63 arm to the software interpreter (issue #117): with REX.W it
sign-extends the 32-bit source into the destination (MOVSXD r64, r/m32);
without REX.W it writes the 32-bit destination register and zeroes the
upper half. REX.R and REX.B extend the destination and source registers.
Five interpreter known-answer tests cover negative and positive sign
extension, upper-half zeroing and register extension.
Signed-off-by: Ranveer Soni <git@ranveersoni.me>
Integrate the missing instruction-length correction into the upstream Prefixes
architecture (issue #69): the interpreter now tracks the instruction length in
execute_instruction and returns Err(IllegalInstruction) when the legacy
segment prefix run or the REX fetch would exceed the 15-byte architectural
limit, preserving the upstream Prefixes struct and interfaces. Regression
tests unchanged (x86_prefix_length.rs).
Signed-off-by: Ranveer Soni <git@ranveersoni.me>
Add crates/loader/src/x86_decode.rs with the shared architectural limit
MAX_INSTRUCTION_LEN (15 bytes) and segment_prefix_len. Bounded the legacy
segment prefix runs in the static probe, the software interpreter and the
native DBT so overlong prefix runs decode as illegal instructions instead of
consuming past the architectural limit, and the probe now rejects any decoded
record longer than 15 bytes. Regression tests cover the overlong prefix run,
prefix-count lengths, the minimized fuzz-artifact input, and interpreter and
DBT rejections (issue #69).
Signed-off-by: Ranveer Soni <git@ranveersoni.me>
Replace the #[allow(clippy::too_many_arguments)] on execute_mov_rm with a
Prefixes struct that carries rex_w, rex_r, rex_b, seg_gs and seg_fs. The
interpreter decodes the prefixes once per instruction and passes the struct to
the helpers that need them, so the lint is satisfied by construction instead
of suppressed.
Add a regression test module that pins the prefix and register-extension
behaviour of the interpreter: REX.R and REX.B selecting the expected ModRM
reg and rm registers, the two bits extending independent fields, a move
without REX.W staying 32-bit wide, and the FS prefix adding the FS base.
The test was run against unmodified main first and passed there, so the
refactor is shown to preserve semantics.
Closes#62
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Replace the saturating TEB address with checked arithmetic and reject a
stack below address zero, an overflowing TEB range, or a TEB overlapping
the stack before the image is parsed. Name the Win64 TEB field offsets
and add unit tests for layout validation and field placement.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Sum max(VirtualSize, SizeOfRawData) over all sections and reject images
above a 256 MiB loader budget with SectionMappingError(AllocationFailed)
before any host allocation. Adds pe_mapping_limits tests.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Route reads, writes, instruction fetches and loader bypass writes through
one resolver. Accesses may span adjacent mapped regions when each grants
the required permission; the whole range is checked before any byte is
copied, so failed accesses change nothing; address wraparound returns
Overflow. Adds memory_validation tests.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Rust 1.100 Clippy rejects chunks_exact with a constant size
(chunks_exact_to_as_chunks), which fails the pinned
cargo clippy -D warnings gate on main since #250. No behavior change.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Read the relocation directory only when NumberOfRvaAndSizes covers it,
reject odd block sizes and trailing partial block headers, keep DIR64
targets inside SizeOfImage, and check the directory end offset. Adds
relocation_validation tests for positive, negative and zero rebasing and
each malformed case.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Fix two parser panics found by the pe_parse fuzz target: a truncated
PE32+ optional header and unchecked RVA range arithmetic. Also require
the 112-byte fixed optional header, check section table and raw data
bounds, reject overlapping sections and require the entry point to lie in
an executable section. Adds pe_validation regression tests built from
original synthetic images.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Reject a failed, zero or non-power-of-two page size, and use checked
arithmetic for page rounding, emitted instruction length and code-cache
capacity. Adds unit tests for each overflow and boundary case.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
The second independent review asked for coverage of a nonzero vertex-buffer
offset and proof that zero-count draws never reach the backend, and for the
changelog to say which errors recording raises and which execution raises.
Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
The review found that a raw primitive-restart index could pass the native
range check with a negative base vertex, that the loader ignored instance
parameters and could wrap a guest address, and that coverage missed flat
multi-triangle draws, an unadjusted textured draw and 32-bit indices above
16 bits.
- Native and CPU reference reject the raw restart value whatever the base.
- The loader rejects index counts other than 0 or 3 first, more than one
instance and a nonzero start instance, and checks guest address
arithmetic through the last byte read.
- Three clean-room cases added (re-derived in a fresh session, which also
reproduced the first 13); native error checks now name each reason, and
the loader error test proves no rejected draw reaches the backend.
Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
Indexed draws had no contract: the loader had no index buffer command and
the Metal backend only drew non-indexed triangles. Add a bounded one:
- NativeCompute::render_indexed issues real drawIndexedPrimitives calls on
a 16- or 32-bit index buffer with a start index and signed base vertex,
optionally sampling a natively uploaded BC1 texture.
- The loader gains SetIndexBuffer (DXGI R16_UINT/R32_UINT only) and
DrawIndexedInstanced, which fetches one triangle through the index
buffer and base vertex and feeds the existing one-triangle backend.
- Both reject bad formats, offsets and counts, indices past the buffer and
any index + base vertex outside the vertex buffer before drawing.
- xenora-ref gains an index-fetch reference over the depth rasteriser.
Expected images and fetch lists were derived independently (clean room)
from public D3D12, Metal and BC1 documentation.
Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>