pe: start the pe_run_package entry as the main image #1078

Merged
mumba17 merged 2 commits from upstream-pr/1085 into main 2026-11-24 01:11:27 +00:00
mumba17 commented 2026-11-24 00:32:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-24T00:32:57Z.


Change

tools/pe_run_package.py started the entry EXE with pe_map_run. An EXE built with /GA reads its thread-locals through TLS slot 0 without loading _tls_index, so under the runner it read and wrote whatever block the host Python process keeps in slot 0. #988 added pe_map_run_main for this case, but nothing in tools/ called it.

  • The runner now starts the entry with pe_map_run_main. Per #988, an image without a TLS template simply starts, so other entries behave as before.
  • The entry's host CreateThread imports (kernel32.dll, kernelbase.dll or an api-ms-win-core-processthreads-* contract) are bound to pe_map_main_create_thread, so the threads it starts are enrolled. Bundled DLLs keep their own imports, the same scope #988 documents for other thread routes.
  • A mapper without pe_map_run_main is refused with the entry EXE requires a mapper with pe_map_run_main.
  • No change to preflight, plan(), provider resolution or release order.

Test

tests/cpu/package_main_image_check.py, registered as cpu_package_main_image, reuses #988's /GA EXE cpu_exe_tls_slot0 and its expected strings:

  • Precondition: the fixture has a TLS directory and imports kernel32!CreateThread.
  • Windows launch: LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0, exit 0.
  • Runner without --system kernel32.dll: refused before mapping, with no guest log.
  • Runner with --system kernel32.dll: the same guest log, exit code 0 and {"image_order": ["cpu_exe_tls_slot0.exe"], "entry_executed": true, "exit_code": 0}.

Negative controls (local mutations, not committed):

Mutation Result
start with pe_map_run (current main) guest log m1=n- m:n--- a2=n- a:n--- a3=n- b2=n- b:n--- b3=n- X=8, which is #988's PLAIN; the check fails
keep pe_map_run_main, do not bind CreateThread m1=yy m:yyyy a:n--- b:n--- X=2: the main thread passes and both workers fail; the check fails

Verification

Environment: Windows 11 Pro build 26300, MSVC 19.44.35207.1 x64, Python 3.13.1 (out/pe-env), branch on 110e186.

ctest --preset dev --no-tests=error -R '^cpu_package_main_image$' -V
ctest --preset dev --no-tests=error -R 'cpu_package_main_image|cpu_native_exe_tls_slot0|cpu_native_startup|cpu_package_ordinal|cpu_package_shim_lifetime|xgameruntime_preflight|pe_package|import_forms|static_exe_intake|cpu_native_acphal_hal'
  • The related set passes 11/11 in dev and 11/11 in release, including every existing pe_run_package.py user and the three slot-0 tests from #988.
  • pe_package_preflight_check.py, pe_package_ordinal_plan_check.py, docs_links_check.py, bidi_controls_check.py and git diff --check are clean.
  • Not run: the full CTest suite and Windows CI for this branch.

Local observation (no image or log attached): I ran the main executable from the #870 checkpoints through the runner with the two shims and System32 providers. With the previous start, it crashed right after its memory setup on a write whose index register held ASCII text, where the Windows loader carried on. With this change it reaches the same next boundary as the Windows loader: the executable's runtime services initialize and the first call into a missing module follows. That made the gap visible. The fixture above is the evidence.

docs/experiments.md records the start mode next to the other runner options.

Refs #870, #988.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6571837287 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1085). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-24T00:32:57Z. --- ## Change `tools/pe_run_package.py` started the entry EXE with `pe_map_run`. An EXE built with `/GA` reads its thread-locals through TLS slot 0 without loading `_tls_index`, so under the runner it read and wrote whatever block the host Python process keeps in slot 0. #988 added `pe_map_run_main` for this case, but nothing in `tools/` called it. - The runner now starts the entry with `pe_map_run_main`. Per #988, an image without a TLS template simply starts, so other entries behave as before. - The entry's host `CreateThread` imports (`kernel32.dll`, `kernelbase.dll` or an `api-ms-win-core-processthreads-*` contract) are bound to `pe_map_main_create_thread`, so the threads it starts are enrolled. Bundled DLLs keep their own imports, the same scope #988 documents for other thread routes. - A mapper without `pe_map_run_main` is refused with `the entry EXE requires a mapper with pe_map_run_main`. - No change to preflight, `plan()`, provider resolution or release order. ## Test `tests/cpu/package_main_image_check.py`, registered as `cpu_package_main_image`, reuses #988's `/GA` EXE `cpu_exe_tls_slot0` and its expected strings: - **Precondition:** the fixture has a TLS directory and imports `kernel32!CreateThread`. - **Windows launch:** `LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0`, exit 0. - **Runner without `--system kernel32.dll`:** refused before mapping, with no guest log. - **Runner with `--system kernel32.dll`:** the same guest log, exit code 0 and `{"image_order": ["cpu_exe_tls_slot0.exe"], "entry_executed": true, "exit_code": 0}`. Negative controls (local mutations, not committed): | Mutation | Result | | --- | --- | | start with `pe_map_run` (current `main`) | guest log `m1=n- m:n--- a2=n- a:n--- a3=n- b2=n- b:n--- b3=n- X=8`, which is #988's `PLAIN`; the check fails | | keep `pe_map_run_main`, do not bind `CreateThread` | `m1=yy m:yyyy a:n--- b:n--- X=2`: the main thread passes and both workers fail; the check fails | ## Verification Environment: Windows 11 Pro build 26300, MSVC 19.44.35207.1 x64, Python 3.13.1 (`out/pe-env`), branch on `110e186`. ```powershell ctest --preset dev --no-tests=error -R '^cpu_package_main_image$' -V ctest --preset dev --no-tests=error -R 'cpu_package_main_image|cpu_native_exe_tls_slot0|cpu_native_startup|cpu_package_ordinal|cpu_package_shim_lifetime|xgameruntime_preflight|pe_package|import_forms|static_exe_intake|cpu_native_acphal_hal' ``` - The related set passes 11/11 in `dev` and 11/11 in `release`, including every existing `pe_run_package.py` user and the three slot-0 tests from #988. - `pe_package_preflight_check.py`, `pe_package_ordinal_plan_check.py`, `docs_links_check.py`, `bidi_controls_check.py` and `git diff --check` are clean. - Not run: the full CTest suite and Windows CI for this branch. **Local observation (no image or log attached):** I ran the main executable from the #870 checkpoints through the runner with the two shims and System32 providers. With the previous start, it crashed right after its memory setup on a write whose index register held ASCII text, where the Windows loader carried on. With this change it reaches the same next boundary as the Windows loader: the executable's runtime services initialize and the first call into a missing module follows. That made the gap visible. The fixture above is the evidence. `docs/experiments.md` records the start mode next to the other runner options. Refs #870, #988. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:46:14 +00:00
R0Y00 commented 2026-11-24 06:49:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:45:07Z.


I pushed a narrow provider-precedence fix. The ordinary System32 route still enrolls both workers and matches the native /GA fixture. The prior candidate silently replaced CreateThread even when the caller supplied that DLL through --shim. The new control sets both options and verifies the explicitly selected provider stays in effect. The main thread owns slot 0 and both unwrapped workers give their expected refusal markers. Windows Release cpu_package_main_image passed, including refusal before mapping with neither provider configured.
Candidate: 0cfb44ceb9

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7118394182 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1085#issuecomment-7118394182). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:45:07Z. --- I pushed a narrow provider-precedence fix. The ordinary System32 route still enrolls both workers and matches the native /GA fixture. The prior candidate silently replaced CreateThread even when the caller supplied that DLL through --shim. The new control sets both options and verifies the explicitly selected provider stays in effect. The main thread owns slot 0 and both unwrapped workers give their expected refusal markers. Windows Release cpu_package_main_image passed, including refusal before mapping with neither provider configured. Candidate: 0cfb44ceb9944ef15d2d62baca6966d5bd467bdd
Sign in to join this conversation.
No reviewers
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!1078
No description provided.