Exit functions are guest code run through callback tasks in reverse
registration order. Table storage lives on the default guest heap.
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
The guest sees a configurable simulated machine, kept separate from the
detected host hardware. Sleeping goes through the injectable clock.
Move the PE fixture harness into a shared test support module.
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Add callback tasks to the import dispatcher. A task runs guest callbacks
on a reserved stack frame, checks the nonvolatile registers after each,
and returns to the original caller. Use it for qsort, qsort_s, bsearch,
bsearch_s, _initterm, _initterm_e, InitOnceExecuteOnce and FlsFree
cleanup callbacks.
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Pin MXCSR for a flushed exact tiny result, PE for unmasked overflow and
underflow, and run the sweeps with each exception unmasked alone. Require a
refused form to stop at its exact instruction with all state unchanged,
assert the cached DBT run hits the cache, and check the fallback counter for
every straight-line case. Add a faulting zero-count memory shift.
The tighter checks expose one more defect, kept as an ignored test: an exact
unmasked overflow sets PE as well as OE.
Correct the chronology: 700 table cases were written before the CPU source
was read, and the other cases and the sweeps afterwards. The message of
1c5c320 says otherwise.
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
Ownership is keyed by guest TEB. Operations that would block another
guest thread stop with the runtime-limit fault because no guest
scheduler exists. Also stop the import generator from overriding the
semaphore handlers with stubs, and check every generator standard
service against the loader registry.
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Add 727 cases and definition-based sweeps for the guest CPU, derived from
the Intel SDM (revision 093) before the CPU code was read. The interpreter
and the DBT run the same cases.
588 cases are verified, 133 use forms both engines refuse, and four ignored
tests state open defects: stale DBT blocks after the guest rewrites code,
the address of a page-crossing fault, and the fault class of a
non-canonical address and of an instruction over 15 bytes.
Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
BSF, BSR and BT were decoded and executed by the interpreter, but every
occurrence of them in a translated block fell back to the shared semantics,
because `is_native` did not list them. The static probe therefore counted
them among the instructions the ARM64 DBT does not support.
Lower them to ARM64:
- BSF writes the index of the lowest set bit of the source and BSR the index
of the highest set bit. The lowest set bit is the highest set bit of the
reversed value, so BSF is `RBIT` followed by `CLZ`, and BSR is 63 minus the
leading zero count.
- A zero source sets the zero flag and leaves the destination undefined, so
the destination keeps its value. The interpreter and the independent
reference apply the same rule, and the reference marks the destination as
undefined so it is not compared.
- BT sets the carry flag from the tested bit, with the bit index masked to
the operand width. The bit string is not modified.
Only the `0F A3` form of BT is lowered. The `0F BA` group form takes an
immediate index and may write memory, so it stays on the shared semantics and
is not advertised as natively supported.
The new encodings of RBIT, CLZ and LSLV were cross-checked against LLVM 23.1.6
`llvm-mc -show-encoding`.
The BSF, BSR and BT fixtures in `tests/x86/integer.txt` already cover the
16-bit, 32-bit and 64-bit forms with register and memory sources, and the
fixture test asserts that a native form never reaches the shared-semantics
helper, so those fixtures now exercise the new native path against the
independent reference. A new test pins the zero source, the highest bit, the
operand widths, the flag results and the masked bit index.
Closes#196
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
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>