cpu: decisions needed before the jump-table translator handles calls, RIP-relative data and stack frames #312

Open
opened 2026-11-21 21:54:33 +00:00 by bosseurdinternet · 9 comments
bosseurdinternet commented 2026-11-21 21:54:33 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T21:54:33Z.


Problem

After #282, #285 and #309 (open), tools/cpu_switch_aot.py translates 651 of 1,119 functions of an MSVC 14.52 /O2 build of this repository's own C++ sources (plus one plain C++ file). What refuses the other 468 is mostly not an instruction: it is a transfer to other code, or an address.

First refusal Functions non-STL
direct call rel32 198 51
jmp qword ptr [rip+slot], a tail jump through the import table 77 12
call qword ptr [rip+slot] 39 36
direct jump to another function 37 11
SSE/AVX instruction 25 15
a RIP-relative address read as data, or returned 34 18
indirect call through a register or memory 10 2
RSP-based memory operand 6 4

Limits of these numbers: 831 of the 1,119 functions are MSVC STL instantiations; only the first refusal of each function is counted, so a function refused for a call usually also needs a stack frame; and this is desktop code, not a title.

Each of these needs a contract that the machine code of one function does not supply. I would rather ask than pick one and have several PRs built on it. Four questions follow, each with the option I would take.

Proposal

1. Imports (call or tail jump through an import slot). Translate only through an explicit binding, with cpu_call_aot.resolve_import and its --bind PROVIDER SYMBOL convention. The callee's code is not in the image, so the number of low RAX bytes it defines is unknown to the translator.

  • Proposed: the binding states that number (0, 1, 2 or 8). A tail-jump site contributes it to return_width as a RET site contributes its own; after a call, RAX has that many defined bytes.
  • Alternative: assume 8 for every bound import.

I am preparing the tail-jump case this way now. It is small and I will change it to match your answer.

2. Direct calls and direct jumps to other functions in the same image. Translate the callee by RVA into the same generated source, starting from the requested exports and following calls, with internal functions named by RVA. The caller is refused if any callee is refused. After a call, RAX has the callee's return_width defined bytes, and volatile registers are undefined.

  • Open point: recursion and call cycles. Proposed: allow them, since a C++ function may call itself, and compute return_width as a fixed point over the call graph starting from the lower bound.

3. RIP-relative data. Today a RIP-relative LEA result is a guest address and may only feed a jump table. Three models:

  • (a) Bound image base. The generated DLL is told where the original image is mapped (by Windows or by pe_map), and a RIP-relative operand becomes image_base + RVA. A RIP-relative LEA then yields a real address, so it can be stored, compared and returned. The translation then depends on the original image being mapped, which the existing differential tests already do.
  • (b) Per-symbol bindings, as ffx_version_query.lower() does with its globals.
  • (c) Copy the data sections into the generated DLL.

Proposed: (a). It needs no per-function input and keeps one copy of the data, shared with the native run it is compared against.

4. Stack frames. A non-leaf function saves nonvolatile registers, allocates with sub rsp, N and addresses [rsp+disp].

  • Proposed: saved registers become C++ locals, with the push/pop pairing checked on every path; the allocation becomes one local byte array per function, and [rsp+disp] an offset into it, so a slot's address is a real address that can be passed to a callee. Refused at first: a function whose RSP is not a constant offset from entry at every instruction, alloca, a callee that reads stack arguments (fifth and later), and any read of a slot that no path wrote.
  • Alternative: reuse the 16-word virtual frame of ffx_version_query.lower().

SSE/AVX is not part of this question; #314 covers a first case.

Alternatives

Keep the translator to leaf functions and count the rest as out of scope. That is a valid answer too: it would stop this line of PRs at integer mnemonics.

Verification

Each step would come as its own PR with a source-built MASM or C++ fixture, a differential check through the register probe, rejection controls printed by the test, and before and after counts on the corpus above with a check that no translated function is lost and that the generated C++ of functions already translated is unchanged. For calls, the corpus functions can also be executed beside their originals on generated inputs, as described in #282 and #285.

Checklist

  • I checked for an existing issue or pull request for this change.
  • I did not include game files, dumps, distribution links or credentials.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6534882715 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T21:54:33Z. --- ### Problem After #282, #285 and #309 (open), `tools/cpu_switch_aot.py` translates 651 of 1,119 functions of an MSVC 14.52 `/O2` build of this repository's own C++ sources (plus one plain C++ file). What refuses the other 468 is mostly not an instruction: it is a transfer to other code, or an address. | First refusal | Functions | non-STL | |---|---|---| | direct `call rel32` | 198 | 51 | | `jmp qword ptr [rip+slot]`, a tail jump through the import table | 77 | 12 | | `call qword ptr [rip+slot]` | 39 | 36 | | direct jump to another function | 37 | 11 | | SSE/AVX instruction | 25 | 15 | | a RIP-relative address read as data, or returned | 34 | 18 | | indirect call through a register or memory | 10 | 2 | | RSP-based memory operand | 6 | 4 | Limits of these numbers: 831 of the 1,119 functions are MSVC STL instantiations; only the first refusal of each function is counted, so a function refused for a call usually also needs a stack frame; and this is desktop code, not a title. Each of these needs a contract that the machine code of one function does not supply. I would rather ask than pick one and have several PRs built on it. Four questions follow, each with the option I would take. ### Proposal **1. Imports (call or tail jump through an import slot).** Translate only through an explicit binding, with `cpu_call_aot.resolve_import` and its `--bind PROVIDER SYMBOL` convention. The callee's code is not in the image, so the number of low RAX bytes it defines is unknown to the translator. - Proposed: the binding states that number (0, 1, 2 or 8). A tail-jump site contributes it to `return_width` as a `RET` site contributes its own; after a call, RAX has that many defined bytes. - Alternative: assume 8 for every bound import. I am preparing the tail-jump case this way now. It is small and I will change it to match your answer. **2. Direct calls and direct jumps to other functions in the same image.** Translate the callee by RVA into the same generated source, starting from the requested exports and following calls, with internal functions named by RVA. The caller is refused if any callee is refused. After a call, RAX has the callee's `return_width` defined bytes, and volatile registers are undefined. - Open point: recursion and call cycles. Proposed: allow them, since a C++ function may call itself, and compute `return_width` as a fixed point over the call graph starting from the lower bound. **3. RIP-relative data.** Today a RIP-relative `LEA` result is a guest address and may only feed a jump table. Three models: - (a) **Bound image base.** The generated DLL is told where the original image is mapped (by Windows or by `pe_map`), and a RIP-relative operand becomes `image_base + RVA`. A RIP-relative `LEA` then yields a real address, so it can be stored, compared and returned. The translation then depends on the original image being mapped, which the existing differential tests already do. - (b) **Per-symbol bindings**, as `ffx_version_query.lower()` does with its globals. - (c) **Copy the data sections** into the generated DLL. Proposed: (a). It needs no per-function input and keeps one copy of the data, shared with the native run it is compared against. **4. Stack frames.** A non-leaf function saves nonvolatile registers, allocates with `sub rsp, N` and addresses `[rsp+disp]`. - Proposed: saved registers become C++ locals, with the push/pop pairing checked on every path; the allocation becomes one local byte array per function, and `[rsp+disp]` an offset into it, so a slot's address is a real address that can be passed to a callee. Refused at first: a function whose RSP is not a constant offset from entry at every instruction, `alloca`, a callee that reads stack arguments (fifth and later), and any read of a slot that no path wrote. - Alternative: reuse the 16-word virtual frame of `ffx_version_query.lower()`. SSE/AVX is not part of this question; #314 covers a first case. ### Alternatives Keep the translator to leaf functions and count the rest as out of scope. That is a valid answer too: it would stop this line of PRs at integer mnemonics. ### Verification Each step would come as its own PR with a source-built MASM or C++ fixture, a differential check through the register probe, rejection controls printed by the test, and before and after counts on the corpus above with a check that no translated function is lost and that the generated C++ of functions already translated is unchanged. For calls, the corpus functions can also be executed beside their originals on generated inputs, as described in #282 and #285. ### Checklist - [x] I checked for an existing issue or pull request for this change. - [x] I did not include game files, dumps, distribution links or credentials. _AI-assisted (Claude Code)._
R0Y00 commented 2026-11-24 06:48:04 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T21:56:53Z.


Start with the IAT tail jump. Bind the module and slot explicitly by symbol name or ordinal, with the input ABI and the number of guaranteed low RAX bytes (0, 1, 2 or 8). Zero denotes a normal return with no guaranteed result bits. Take the conservative minimum across tail jumps and other reachable normal returns. For ordinary calls apply volatile-register and flag clobbers.

For internal edges, validate function-entry RVAs and emit each target once. Start with acyclic graphs and diagnose self- and mutual recursion. For RIP-relative data, accept an explicitly supplied live mapping matching the input image and a checked RVA. Preserve aliases and shared writes; compare writable data under identical initial state using separate mappings or restoration, and compare cross-mapping addresses by RVA. Keep the existing ABI available and limit pointer validity to the binding lifetime.

For frames, use a bounded byte array per invocation with checked size and alignment. Track stack offsets, save/restore and bytewise definition before reads on each path. Allow only known bounded pointer uses across calls; reject escapes, dynamic frames, unsupported stack arguments and unproved effects. Separate source-built fixtures are appropriate for each step. Report corpus figures for the measured desktop inputs.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069831364 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7069831364). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T21:56:53Z. --- Start with the IAT tail jump. Bind the module and slot explicitly by symbol name or ordinal, with the input ABI and the number of guaranteed low RAX bytes (0, 1, 2 or 8). Zero denotes a normal return with no guaranteed result bits. Take the conservative minimum across tail jumps and other reachable normal returns. For ordinary calls apply volatile-register and flag clobbers. For internal edges, validate function-entry RVAs and emit each target once. Start with acyclic graphs and diagnose self- and mutual recursion. For RIP-relative data, accept an explicitly supplied live mapping matching the input image and a checked RVA. Preserve aliases and shared writes; compare writable data under identical initial state using separate mappings or restoration, and compare cross-mapping addresses by RVA. Keep the existing ABI available and limit pointer validity to the binding lifetime. For frames, use a bounded byte array per invocation with checked size and alignment. Track stack offsets, save/restore and bytewise definition before reads on each path. Allow only known bounded pointer uses across calls; reject escapes, dynamic frames, unsupported stack arguments and unproved effects. Separate source-built fixtures are appropriate for each step. Report corpus figures for the measured desktop inputs.
grahamster2 commented 2026-11-24 06:48:12 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-21T23:37:42Z.


I'd like to take step 4 (stack frames) in tools/cpu_switch_aot.py, following the decision above. @bosseurdinternet, if you already have frames underway, say so and I'll verify or review yours instead.

First PR: call-free functions with a fixed frame. Calls stay refused until step 2 lands, so no frame pointer crosses a call yet.

  • RSP is tracked as a constant offset from entry RSP at every instruction. Paths that meet with different offsets are refused, as are sub rsp, reg and any other RSP write.
  • Accepted frame operations: push/pop of a register, sub rsp, imm and add rsp, imm, and [rsp+disp] loads and stores with no index. Every RET must see offset 0.
  • Storage: one alignas(16) byte array per invocation, sized at translation time to the deepest offset plus the caller's 32-byte home area. [rsp+disp] becomes a checked offset into it. The return-address slot and anything above the home area (stack arguments) are refused.
  • Bytewise definedness on each path: a frame read needs every byte it reads written on every incoming path. Home-area bytes start undefined.
  • Nonvolatile registers: a push of RBX, RBP, RSI, RDI or R12–R15 before any write saves the caller's value, and every RET must see that register restored by the matching pop. A nonvolatile write without such a save, or a read of a save slot other than its restore, is refused.
  • Refused for now: lea r, [rsp+d] or any other way a frame address reaches a register, RSP as an index, and enter/leave.

Verification: a source-built MASM fixture with push/pop of nonvolatiles, sub rsp spills and reloads, home-area spills of RCX and RDX, and a frame value that crosses a join. Differential checks through cpu_register_probe (return, trace, RBX, RSI) against the original, and printed rejection controls for an unbalanced pop, a read before write, mismatched offsets at a join, a return-address read, a stack-argument read, an RSP write from a register and a frame-address escape. I'll report before/after counts on the MSVC /O2 corpus at the top of this issue. @bosseurdinternet, could you share how you built and walked it, so the figures are comparable?

I'll base on current main and rebase after #343, #321 and #330, which also edit cpu_switch_aot.py. Environment: Windows 11 build 26200, MSVC 19.50, Python 3.12.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070998760 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7070998760). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-21T23:37:42Z. --- I'd like to take step 4 (stack frames) in `tools/cpu_switch_aot.py`, following the decision above. @bosseurdinternet, if you already have frames underway, say so and I'll verify or review yours instead. **First PR: call-free functions with a fixed frame.** Calls stay refused until step 2 lands, so no frame pointer crosses a call yet. - RSP is tracked as a constant offset from entry RSP at every instruction. Paths that meet with different offsets are refused, as are `sub rsp, reg` and any other RSP write. - Accepted frame operations: `push`/`pop` of a register, `sub rsp, imm` and `add rsp, imm`, and `[rsp+disp]` loads and stores with no index. Every RET must see offset 0. - Storage: one `alignas(16)` byte array per invocation, sized at translation time to the deepest offset plus the caller's 32-byte home area. `[rsp+disp]` becomes a checked offset into it. The return-address slot and anything above the home area (stack arguments) are refused. - Bytewise definedness on each path: a frame read needs every byte it reads written on every incoming path. Home-area bytes start undefined. - Nonvolatile registers: a `push` of RBX, RBP, RSI, RDI or R12–R15 before any write saves the caller's value, and every RET must see that register restored by the matching `pop`. A nonvolatile write without such a save, or a read of a save slot other than its restore, is refused. - Refused for now: `lea r, [rsp+d]` or any other way a frame address reaches a register, RSP as an index, and `enter`/`leave`. **Verification:** a source-built MASM fixture with push/pop of nonvolatiles, `sub rsp` spills and reloads, home-area spills of RCX and RDX, and a frame value that crosses a join. Differential checks through `cpu_register_probe` (return, trace, RBX, RSI) against the original, and printed rejection controls for an unbalanced pop, a read before write, mismatched offsets at a join, a return-address read, a stack-argument read, an RSP write from a register and a frame-address escape. I'll report before/after counts on the MSVC `/O2` corpus at the top of this issue. @bosseurdinternet, could you share how you built and walked it, so the figures are comparable? I'll base on current `main` and rebase after #343, #321 and #330, which also edit `cpu_switch_aot.py`. Environment: Windows 11 build 26200, MSVC 19.50, Python 3.12.
bosseurdinternet commented 2026-11-24 06:48:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T23:51:19Z.


Hey, thanks for the detailed plan! I've already implemented most of step 4 and am wrapping up the remaining tests and verification. I'm planning to open a PR within the next couple of hours.

It would probably make sense to review and build on that rather than duplicate the work. I'll share the PR here as soon as it's ready, along with the testing details and corpus results.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7071148293 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7071148293). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T23:51:19Z. --- Hey, thanks for the detailed plan! I've already implemented most of step 4 and am wrapping up the remaining tests and verification. I'm planning to open a PR within the next couple of hours. It would probably make sense to review and build on that rather than duplicate the work. I'll share the PR here as soon as it's ready, along with the testing details and corpus results.
grahamster2 commented 2026-11-24 06:48:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-21T23:53:31Z.


Thanks, @bosseurdinternet. I'll review and verify yours rather than open a second PR.

I had already built an independent version while waiting, so it can serve as a cross-check: cpu/switch-stack-frames on my fork (be4784f, not a PR). The fixture and controls don't depend on my translator, so once your PR is up I'll run them against your branch on Windows 11 build 26200 (MSVC 19.50) and report what each one does:

  • tests/cpu/frames.asm: five exports covering PUSH/POP of RBX and RSI used as scratch, qword/dword/word/byte stores combined by a wider reload, home-area spills of RCX and RDX with no allocation, the same slots written differently on two paths before a join, and a frame-resident counter across a loop back edge. Through cpu_register_probe, 545 inputs per export against a Python reference.
  • 15 same-length byte patches of that DLL, each expected to be refused: swapped and missing POPs, a read of the caller's RBX, a reload with its store removed, a store removed on one path only, a PUSH on one path, a save-slot read, [rsp], [rsp+28h] and [rsp-8] at entry, LEA and MOV copies of RSP, an unsaved write to RDI, mov rsp, rax and sub rsp, rcx.

Corpus method, in case it helps make figures comparable: a Release (/O2) build of the repo at 9bdbb20, every .pdata entry of the 81 non-_lifted images, deduplicated by function bytes, each translated by RVA with four arguments. That gave 1,406 unique functions. Two observations from it: three CRT-free fixtures (cpu_startup_a, cpu_startup_a_alt, cpu_life_sink) fail to link at /O2 with an unresolved memcpy, and once frames are accepted the most common new first refusal is a RIP-relative operand (28 functions), followed by XMM/YMM registers (8).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7071172394 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7071172394). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-21T23:53:31Z. --- Thanks, @bosseurdinternet. I'll review and verify yours rather than open a second PR. I had already built an independent version while waiting, so it can serve as a cross-check: [`cpu/switch-stack-frames`](https://github.com/grahamster2/Xbox2Windows/tree/cpu/switch-stack-frames) on my fork (`be4784f`, not a PR). The fixture and controls don't depend on my translator, so once your PR is up I'll run them against your branch on Windows 11 build 26200 (MSVC 19.50) and report what each one does: - [`tests/cpu/frames.asm`](https://github.com/grahamster2/Xbox2Windows/blob/cpu/switch-stack-frames/tests/cpu/frames.asm): five exports covering PUSH/POP of RBX and RSI used as scratch, qword/dword/word/byte stores combined by a wider reload, home-area spills of RCX and RDX with no allocation, the same slots written differently on two paths before a join, and a frame-resident counter across a loop back edge. Through `cpu_register_probe`, 545 inputs per export against a Python reference. - 15 same-length byte patches of that DLL, each expected to be refused: swapped and missing POPs, a read of the caller's RBX, a reload with its store removed, a store removed on one path only, a PUSH on one path, a save-slot read, `[rsp]`, `[rsp+28h]` and `[rsp-8]` at entry, LEA and MOV copies of RSP, an unsaved write to RDI, `mov rsp, rax` and `sub rsp, rcx`. Corpus method, in case it helps make figures comparable: a Release (`/O2`) build of the repo at `9bdbb20`, every `.pdata` entry of the 81 non-`_lifted` images, deduplicated by function bytes, each translated by RVA with four arguments. That gave 1,406 unique functions. Two observations from it: three CRT-free fixtures (`cpu_startup_a`, `cpu_startup_a_alt`, `cpu_life_sink`) fail to link at `/O2` with an unresolved `memcpy`, and once frames are accepted the most common new first refusal is a RIP-relative operand (28 functions), followed by XMM/YMM registers (8).
grahamster2 commented 2026-11-24 06:48:16 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:08:50Z.


@bosseurdinternet, I see #343 (step 1) and #416 (step 3), with frames coming from you as well. Is step 2, direct calls and direct jumps to other functions in the same image, also on your list?

If not, I'd like to take it, following the decision above: validate function-entry RVAs, emit each callee once, start with acyclic call graphs, diagnose self- and mutual recursion, and apply volatile-register and flag clobbers after a call. Every non-leaf callee needs a frame, so I'd build on your frames PR once it's up and prototype against my frames branch until then. If you already have it underway, say so and I'll review and verify it instead, as with frames.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072013275 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7072013275). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T01:08:50Z. --- @bosseurdinternet, I see #343 (step 1) and #416 (step 3), with frames coming from you as well. Is step 2, direct calls and direct jumps to other functions in the same image, also on your list? If not, I'd like to take it, following the decision above: validate function-entry RVAs, emit each callee once, start with acyclic call graphs, diagnose self- and mutual recursion, and apply volatile-register and flag clobbers after a call. Every non-leaf callee needs a frame, so I'd build on your frames PR once it's up and prototype against my frames branch until then. If you already have it underway, say so and I'll review and verify it instead, as with frames.
grahamster2 commented 2026-11-24 06:48:16 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:35:19Z.


I've opened step 2 (direct calls and tail jumps to other functions in the same image) as draft #425. It is stacked on #420 and I'll rebase once #420 lands. It follows the decision above: function-entry RVAs are validated, each callee is emitted once, self- and mutual recursion are refused, and volatile-register and flag clobbers apply after a call. The PR lists the points the decision left open for review, the main one being callees that have no .pdata entry. @bosseurdinternet, if you already have step 2 underway, say so and I'll close mine and review yours.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072299816 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7072299816). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T01:35:19Z. --- I've opened step 2 (direct calls and tail jumps to other functions in the same image) as draft #425. It is stacked on #420 and I'll rebase once #420 lands. It follows the decision above: function-entry RVAs are validated, each callee is emitted once, self- and mutual recursion are refused, and volatile-register and flag clobbers apply after a call. The PR lists the points the decision left open for review, the main one being callees that have no `.pdata` entry. @bosseurdinternet, if you already have step 2 underway, say so and I'll close mine and review yours.
grahamster2 commented 2026-11-24 06:48:19 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T02:50:52Z.


A design question for the next step after #425, before any code is written for it: frame addresses passed to callees.

With frames (#420) and direct calls (#425), the most common first refusal at /Od in @bosseurdinternet's corpus is a frame address that escapes or is passed to a callee (about 190 functions). In mine (/O2) it's 41. The typical shape is lea rcx, [rsp+20h]; call f where f writes or reads through RCX, i.e. an out-parameter or a small struct passed by pointer. The decision above says: "Allow only known bounded pointer uses across calls; reject escapes, dynamic frames, unsupported stack arguments and unproved effects."

Proposed reading, for you to accept, narrow or reject:

  1. A frame address may be passed only as a call argument (RCX, RDX, R8 or R9 at the call), as lea reg, [rsp+d] immediately feeding the call, never stored to memory, returned, or kept in a register after the call.
  2. The callee must be translated in the same run (#425's internal callees). Its accesses through that argument are proved, by the same definedness machinery, to stay inside a bounded window [0, n) from the pointer: constant offsets only, and no further escape (not stored, not passed on, not returned). n and the bytes the callee reads before writing and writes are recorded as the callee's effect summary.
  3. At the call site the caller's window [d, d+n) must lie inside its frame and not overlap the callee's home space or the return address. Bytes the callee reads must be defined in the caller. After the call, bytes the callee may write become written (defined if written on every callee path, otherwise undefined). Nothing else in the frame changes.
  4. In the generated C++, the argument is the address of the caller's frame array element, so the callee's load/store reach it directly. Pointer validity is limited to the call, matching "pointer validity to the binding lifetime".
  5. Refused: indexing through the pointer with a non-constant, pointer arithmetic that leaves [0, n), a pointer to the caller's home space or save slots, frame addresses passed to imports (unknown effects), and recursion through such calls.

Is that the boundary you want, or would you prefer something narrower first, e.g. read-only or write-only callee windows, or a single pointer argument per call? I'd do it as one PR stacked on #425 with fixtures for each rule and the usual controls.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073165358 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7073165358). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T02:50:52Z. --- A design question for the next step after #425, before any code is written for it: frame addresses passed to callees. With frames (#420) and direct calls (#425), the most common first refusal at `/Od` in @bosseurdinternet's corpus is a frame address that escapes or is passed to a callee (about 190 functions). In mine (`/O2`) it's 41. The typical shape is `lea rcx, [rsp+20h]; call f` where `f` writes or reads through RCX, i.e. an out-parameter or a small struct passed by pointer. The decision above says: "Allow only known bounded pointer uses across calls; reject escapes, dynamic frames, unsupported stack arguments and unproved effects." Proposed reading, for you to accept, narrow or reject: 1. A frame address may be passed only as a call argument (RCX, RDX, R8 or R9 at the call), as `lea reg, [rsp+d]` immediately feeding the call, never stored to memory, returned, or kept in a register after the call. 2. The callee must be translated in the same run (#425's internal callees). Its accesses through that argument are proved, by the same definedness machinery, to stay inside a bounded window `[0, n)` from the pointer: constant offsets only, and no further escape (not stored, not passed on, not returned). `n` and the bytes the callee reads before writing and writes are recorded as the callee's effect summary. 3. At the call site the caller's window `[d, d+n)` must lie inside its frame and not overlap the callee's home space or the return address. Bytes the callee reads must be defined in the caller. After the call, bytes the callee may write become written (defined if written on every callee path, otherwise undefined). Nothing else in the frame changes. 4. In the generated C++, the argument is the address of the caller's frame array element, so the callee's `load`/`store` reach it directly. Pointer validity is limited to the call, matching "pointer validity to the binding lifetime". 5. Refused: indexing through the pointer with a non-constant, pointer arithmetic that leaves `[0, n)`, a pointer to the caller's home space or save slots, frame addresses passed to imports (unknown effects), and recursion through such calls. Is that the boundary you want, or would you prefer something narrower first, e.g. read-only or write-only callee windows, or a single pointer argument per call? I'd do it as one PR stacked on #425 with fixtures for each rule and the usual controls. _AI-assisted (Claude Code)._
tedin7 commented 2026-11-24 06:50:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T02:23:48Z.


Claiming one narrow addition to tools/cpu_switch_aot.py: the inverted unsigned bound check, cmp reg, limit; jb/jbe dispatch_block; jmp default. JB and JBE enter the table, with limit and limit + 1 entries. It touches dispatch() and prove() only, not the ABI, frames, calls or data binding, so it does not overlap steps 1 to 4 or any open PR I could find.

Planned files: tools/cpu_switch_aot.py, a new source-built fixture tests/cpu/switch_inverted.asm with its check, CMakeLists.txt and docs/experiments.md. The check runs native, translated and exchanged-table builds against a Python reference and adds byte-patched rejection controls.

Environment: Linux, LLVM 19 (llvm-mc, lld-link), mingw-w64 g++ with Wine 10 for the native-versus-translated run. ML64 and MSVC are not available to me, so the MASM source and the /O2 build are only exercised by the Windows CI jobs.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7120096846 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7120096846). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T02:23:48Z. --- Claiming one narrow addition to `tools/cpu_switch_aot.py`: the inverted unsigned bound check, `cmp reg, limit; jb/jbe dispatch_block; jmp default`. JB and JBE enter the table, with `limit` and `limit + 1` entries. It touches `dispatch()` and `prove()` only, not the ABI, frames, calls or data binding, so it does not overlap steps 1 to 4 or any open PR I could find. Planned files: `tools/cpu_switch_aot.py`, a new source-built fixture `tests/cpu/switch_inverted.asm` with its check, `CMakeLists.txt` and `docs/experiments.md`. The check runs native, translated and exchanged-table builds against a Python reference and adds byte-patched rejection controls. Environment: Linux, LLVM 19 (`llvm-mc`, `lld-link`), mingw-w64 g++ with Wine 10 for the native-versus-translated run. ML64 and MSVC are not available to me, so the MASM source and the `/O2` build are only exercised by the Windows CI jobs. _AI-assisted (Claude Code)._
dicnunz commented 2026-11-25 06:01:35 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by dicnunz on 2026-11-25T05:11:27Z.


Claiming two stacked follow-ups to step 2 on tools/cpu_switch_aot.py, after the acyclic-call work already on main.

  1. Recursion through direct CALLs: allow a call to close a call cycle and settle return_width and argument demands by iterating to a fixed point, the method proposed in this issue. A tail jump that closes a cycle stays refused. Branch: https://github.com/dicnunz/Xbox2Windows/tree/cpu/switch-recursion (9e8d909)

  2. Stack arguments (5th to 12th) of internal callees: each caller must have written the bytes the callee reads. A tail jump to such a callee, and stack arguments of exported functions, stay refused. This branch contains the recursion commits, so it should land second: https://github.com/dicnunz/Xbox2Windows/tree/cpu/switch-stack-arguments (172c07f)

Both use new source-built MASM fixtures with differential checks and refusal controls. They were developed in Claude Code (Opus 5.5). On the fork's full Windows CI (windows-2025, Debug and /O2), cpu_aot_recursion and cpu_aot_stack_arguments passed. The one failing test, call_evidence_check, is unchanged by these branches and fails the same way on upstream main.

These do not overlap the remaining questions on this issue (frame addresses passed to callees, RIP-relative data, imports) or the open cpu: PRs I could find. They edit cpu_switch_aot.py, CMakeLists.txt and docs/experiments.md.

AI-assisted (Claude Code, Opus 5.5).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7153532893 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/317#issuecomment-7153532893). Originally posted by [dicnunz](https://github.com/dicnunz) on 2026-11-25T05:11:27Z. --- Claiming two stacked follow-ups to step 2 on `tools/cpu_switch_aot.py`, after the acyclic-call work already on main. 1. Recursion through direct `CALL`s: allow a call to close a call cycle and settle `return_width` and argument demands by iterating to a fixed point, the method proposed in this issue. A tail jump that closes a cycle stays refused. Branch: https://github.com/dicnunz/Xbox2Windows/tree/cpu/switch-recursion (`9e8d909`) 2. Stack arguments (5th to 12th) of internal callees: each caller must have written the bytes the callee reads. A tail jump to such a callee, and stack arguments of exported functions, stay refused. This branch contains the recursion commits, so it should land second: https://github.com/dicnunz/Xbox2Windows/tree/cpu/switch-stack-arguments (`172c07f`) Both use new source-built MASM fixtures with differential checks and refusal controls. They were developed in Claude Code (Opus 5.5). On the fork's full Windows CI (`windows-2025`, Debug and /O2), `cpu_aot_recursion` and `cpu_aot_stack_arguments` passed. The one failing test, `call_evidence_check`, is unchanged by these branches and fails the same way on upstream main. These do not overlap the remaining questions on this issue (frame addresses passed to callees, RIP-relative data, imports) or the open `cpu:` PRs I could find. They edit `cpu_switch_aot.py`, `CMakeLists.txt` and `docs/experiments.md`. _AI-assisted (Claude Code, Opus 5.5)._
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows#312
No description provided.