Commit Graph
113 Commits
Author SHA1 Message Date
Adeeb RahmanandCursor 17fa47f058 Add CRT onexit tables, atexit and _cexit
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>
2026-11-24 09:52:50 -05:00
Adeeb RahmanandCursor 8d7f7fadb4 Add guest system information, calendar time and sleeping
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>
2026-11-24 09:48:16 -05:00
Adeeb RahmanandCursor 1eae910ce1 Let services call guest code and continue after it returns
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>
2026-11-24 09:41:39 -05:00
Adeeb RahmanandCursor ac6f9daff3 Add guest mutexes, locks, one-time init, SLists and TLS slots
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>
2026-11-24 09:33:07 -05:00
Adeeb Rahman 105bf8a65c Add local fixture profiles and selection notifications
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:58:40 -05:00
Adeeb Rahman 9da73f8ad8 Expose fixture save quota usage and platform capabilities
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:57:03 -05:00
Adeeb Rahman 9241630f6c Add quota-checked save slots for original guest fixtures
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:56:19 -05:00
Adeeb Rahman 542684612d Add process-local platform lifecycle identity and settings
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:55:17 -05:00
Adeeb Rahman f9a7d99638 Drain terminated manual GDK queues with canceled callbacks
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:54:10 -05:00
Adeeb Rahman 32e41e3890 Schedule delayed GDK callbacks on manual ports
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:52:34 -05:00
Adeeb Rahman bf56047db9 Wait for manual GDK callbacks with bounded dispatch deadlines
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:51:29 -05:00
Adeeb Rahman fa2e89cc06 Dispatch queued GDK callbacks through translated guest execution
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:50:31 -05:00
Adeeb Rahman b8fae106af Queue bounded GDK guest callbacks on shared ports
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:48:46 -05:00
Adeeb Rahman 4b370efa12 Expose shared ports for manual GDK composite queues
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:47:43 -05:00
Adeeb Rahman f6c87de433 Add manual GDK task queue handle ownership
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:46:45 -05:00
Adeeb Rahman 0b6ab43ad4 Apply task deadlines to cooperative background work
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:45:32 -05:00
Adeeb Rahman aa93665b97 Allow cooperative cancellation of running runtime tasks
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:44:37 -05:00
Adeeb Rahman 565c144a39 Run runtime tasks on a managed background worker
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:43:38 -05:00
Adeeb Rahman ed33b57c8d Exercise manual asynchronous tasks through original guest services
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:42:27 -05:00
Adeeb Rahman 9e2c7b6e0b Add bounded manual work and completion ports for runtime tasks
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:41:17 -05:00
Adeeb Rahman 88e7dad8b2 Verify UDP polling wakes on a concurrent send
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:40:20 -05:00
Adeeb Rahman 7ff88bc3f4 Support bounded positive timeouts in guest UDP polling
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:39:27 -05:00
Adeeb Rahman 0109aea9ea Report synthetic UDP socket type and datagram limit
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:38:28 -05:00
Adeeb Rahman ce2a753d2f Expose guest UDP local and peer addresses
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:37:34 -05:00
Adeeb Rahman 342fcbd17d Format guest IPv4 addresses through Winsock inet_ntop
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:36:38 -05:00
Adeeb Rahman 7a91e11742 Report immediate Winsock UDP readiness through WSAPoll
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:35:53 -05:00
Adeeb Rahman e3cd65cb79 Build guest UDP endpoints through Winsock address conversion
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:34:40 -05:00
Adeeb Rahman 198242e5e4 Deliver bounded UDP datagrams inside guest loopback
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:32:34 -05:00
Adeeb Rahman aacd160626 Add bounded guest Winsock startup and UDP socket lifecycle
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:30:55 -05:00
Adeeb Rahman 73764899c2 Isolate Win32 last-error state by guest thread
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:27:46 -05:00
Adeeb Rahman 51269bb714 Isolate CRT errno by guest thread within the address space
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:25:33 -05:00
Adeeb Rahman c1e6646bf2 Use typed HRESULT results at the guest graphics boundary
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:24:10 -05:00
Adeeb Rahman 2367414be5 Preserve typed import identities in probe and sync reports
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:20:01 -05:00
Adeeb Rahman 1d0b0d1547 Generate documented ordinal aliases for typed runtime services
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:18:16 -05:00
Adeeb Rahman fbb487384f Resolve registered ordinals through guest module handles
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:16:53 -05:00
Adeeb Rahman ac13c59b7d Track built-in guest module references and revoke released exports
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
2026-11-24 08:15:06 -05:00
Adeeb Rahman 017b303e79 Merge remote-tracking branch 'origin/main' into feat/system-runtime-expansion
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>

# Conflicts:
#	CHANGELOG.md
#	crates/import-gen/src/codegen.rs
#	crates/loader/src/imports.rs
#	crates/loader/src/services.rs
2026-11-24 08:07:05 -05:00
Ranveer Soni c7351c846d Merge pull request #291 from Swastickc/task/289-segment-tests
loader: restore and complete FS/GS segment-override tests (#289)
2026-11-24 18:13:09 +05:30
Swastick Chowdhury 8cae9772b6 loader: restore and complete FS/GS segment-override tests (#289)
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>
2026-11-24 18:08:32 +05:30
jelly013 13c259027b Merge upstream main (#286, #287) into CPU/runtime continuation
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.
2026-11-24 13:35:58 +01:00
jelly013 42825b223b Merge upstream main (#280, #281, #282, #283) into CPU/runtime continuation
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>
2026-11-24 13:33:18 +01:00
jelly013 280cd492fe runtime: one import resolver and typed service adapters
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>
2026-11-24 13:29:40 +01:00
Swastick Chowdhury c4d0b6c000 loader: stop the static probe panicking on truncated PE names (#270)
`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>
2026-11-24 17:57:26 +05:30
Ranveer Soni 3bdc43d3ad Merge pull request #281 from Swastickc/task/66-movsx-movzx
loader: implement MOVZX and MOVSX in the interpreter (#66)
2026-11-24 17:47:21 +05:30
Ranveer Soni b49ec43ddc Merge branch 'main' into task/72-segment-override 2026-11-24 17:46:11 +05:30
jelly013 4f4d977807 cpu: add exact SSE and SSE2 floating point
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>
2026-11-24 13:11:07 +01:00
Swastick Chowdhury f6dfe58695 loader: implement MOVZX and MOVSX in the interpreter (#66)
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>
2026-11-24 17:37:02 +05:30
Swastick Chowdhury b12aa502a2 loader: add the FS and GS segment base to RIP-relative operands in the DBT (#72)
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>
2026-11-24 17:36:38 +05:30
Swastick Chowdhury 20780eab24 loader: clear the upper half of a 32-bit MOVSX destination (#65)
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>
2026-11-24 17:35:49 +05:30
jelly013 dce37dab18 cpu: add SSE2 state, moves and packed integer forms
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>
2026-11-24 12:53:35 +01:00