Skip to content

engine: fault guest reads on the requested access size instead of always 8 bytes - #2

Open
bluetoe-99 wants to merge 2 commits into
feature/tier1-ldr-prepost-indexfrom
feature/engine-sized-guest-read
Open

bluetoe-99 wants to merge 2 commits into
feature/tier1-ldr-prepost-indexfrom
feature/engine-sized-guest-read

Conversation

@bluetoe-99

@bluetoe-99 bluetoe-99 commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Problem

guest_read always reads 8 bytes and faults unless max_readable_bytes >= 8, so a byte load at the last readable byte of a region faults even though the architecture only asked for 1 byte. The reference engine faults at the access's own granularity, not at 8 bytes. This is the remaining baseline fuzzer error class reported in the upstream LDR pre/post PR (397FFC00 and the FF C00 family at every width).

This PR is stacked on feature/tier1-ldr-prepost-index: the JIT stub changed here sits inside that branch's translate_ldr hunks. This PR's diff is the lane only (+94/-11); it will be retargeted upstream once that PR merges.

Change

The Tier 1 LDR lowering passes the instruction's access width as a third helper argument: 1 << bits[31:30] (0 byte, 1 halfword, 2 word, 3 doubleword). That formula covers the whole immediate and register-offset LDR group, including LDRB/LDRH/LDRSB/LDRSH/LDRSW, since they all route through the single translate_ldr stub. The engine faults only when the requested size exceeds max_readable_bytes and copies exactly that many bytes into a zero-initialized uint64_t.

  • include/backend/bal_cpu.h: one typedef (read_size parameter). The JIT stub is the only other consumer of the function pointer.
  • src/bal_engine.c: range guard (1..8), size-based fault check, sized memcpy.
  • src/backend/x86/bal_x86_tier1_compiler.c: BAL_X86_ABI_ARG3 (R8 on Windows, RDX on SysV) plus one MOV_REGISTER_IMMEDIATE before the call. The third argument is caller-saved and already clobbered by the existing call, so no allocator impact.
  • The JIT-side truncation/zero-extension masks are kept on purpose. They are redundant now, and removing them would be cosmetic.

Fault behavior is strictly monotone: max_readable < size with size <= 8 is a subset of the old max_readable < 8, so this change can only remove faults, never add one.

Fuzz evidence (full 60,389-seed corpus, identical order and worker state on both sides)

To compare against main without its 6-error early stop, a diagnostic copy of de07fcaa was built with the cap raised (scratch build, not committed).

build passed unique failed seeds errors
main de07fcaa (cap raised) 1054 149 18
this branch (stacked on the LDR pre/post PR) 1154 104 0
  • New fails: 0. Every failed seed on the branch also fails on main in the same run context (the 104 are a subset of the 149).
  • New errors: 0. The branch has no errors at all. All 18 main errors are gone: 12 sized-read fixes across the 39x/79x FF C00 families, and 6 became oracle skips because of the harness order in the LDR pre/post PR.
  • 45 seeds moved fail to pass, 63 seeds improved overall, nothing regressed anywhere in the corpus.

The extra failed seeds this branch does show (78 beyond the known 26 sign-extension ones) were never executed by any previous run because main stops at 6 errors. They are pre-existing flag/ADC-family mismatches, shown by the same-context full-corpus comparison above.

Unit tests

20 tests in test_bal_tier1_ldr (16 from the LDR pre/post PR plus 4 new): a 64-bit read ending flush with the region end still succeeds, a byte read at the last byte succeeds where a full-width read would fault, and halfword and 64-bit reads crossing the end still fault with BAL_ERROR_MEMORY_FAULT. Full suite: ctest 15/15 green, Clang 18 Debug, generated decoder tables byte-identical to main.

@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-ldr-prepost-index branch from 4e49723 to 4900c88 Compare October 29, 2026 15:50
@bluetoe-99
bluetoe-99 force-pushed the feature/engine-sized-guest-read branch from 3a5a81a to 646707a Compare October 29, 2026 15:50
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-ldr-prepost-index branch from 4900c88 to 877a93a Compare October 29, 2026 19:21
@bluetoe-99
bluetoe-99 force-pushed the feature/engine-sized-guest-read branch from 646707a to c057245 Compare October 29, 2026 19:21
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-ldr-prepost-index branch from 877a93a to 38a2bc3 Compare October 29, 2026 19:46
@bluetoe-99
bluetoe-99 force-pushed the feature/engine-sized-guest-read branch from c057245 to f6f6822 Compare October 29, 2026 19:46
The tier1 LDR lowering now passes the instruction's access width
(bits 31-30 as a power of two: 1/2/4/8) as the third helper argument.
guest_read faults only when the actual access is unreadable, matching
the reference engine's per-access fault granularity. Reads that
previously faulted spuriously near region ends (e.g. an LDRB at the
last byte) now execute correctly.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
Four differential unit tests: a 64-bit read ending flush with the
region end still succeeds, a byte read at the last byte succeeds where
a full-width read would fault, and halfword and 64-bit reads crossing
the end still fault with BAL_ERROR_MEMORY_FAULT.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
@bluetoe-99
bluetoe-99 force-pushed the feature/engine-sized-guest-read branch from f6f6822 to 45d9d99 Compare October 29, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant