Commit only payload pages for ERA heap alignments above a page #181

Merged
Vonsek merged 1 commit from upstream-pr/182 into main 2026-11-21 19:59:00 +00:00
Vonsek commented 2026-11-21 18:07:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T18:07:07Z.


Change

era_heap::allocate passed every alignment exponent (bits 24–28, up to 31) to _aligned_malloc. The CRT over-allocates by the alignment and commits that padding as heap memory, so a 257-byte request with exponent 31 committed more than 2 GiB, and exponent 20 committed 1 MiB.

  • src/era_heap.cpp: alignments above the page size get their own reservation. The allocator reserves payload + alignment address space, releases it, and reserves and commits only the page-rounded payload at the aligned address inside it, retrying up to eight times if another thread takes the range. VirtualAlloc memory is already zero, so no memset touches it. Alignments up to one page keep the _aligned_malloc path unchanged.
  • release frees a block with VirtualFree(MEM_RELEASE) when it is aligned to the allocation granularity and VirtualQuery reports it as the AllocationBase. A CRT block cannot pass this test: _aligned_malloc stores its base pointer below the returned address, so the block never starts its own allocation. Other pointers go to _aligned_free as before.
  • tests/era_heap_check.cpp, CMakeLists.txt: new era_heap_large_alignment scenario. Exponents 13, 16, 17, 20 and 31 with payloads of 1, 257, one page, one page + 1 and three pages − 1 bytes. Each block must be aligned and zeroed. VirtualQuery must report AllocationBase == BaseAddress == block, MEM_COMMIT, PAGE_READWRITE, MEM_PRIVATE and RegionSize equal to the page-rounded payload. After release the address must be MEM_FREE.
  • docs/experiments.md: notes the deviation from SlimEra's XMemAllocDefault, which uses _aligned_malloc for all heap requests, and adds the test row. tools/pe_inventory.py: links XMemAlloc/XMemFree to the new test.

Attribute decoding is unchanged. The accepted attribute set, the 16-byte minimum alignment and the failure policy are the same as before.

Verification

  • Linux stand-in: src/era_heap.cpp and tests/era_heap_check.cpp compiled with clang++ 18 (-std=c++20 -Wall -Wextra, no warnings) against a windows.h/malloc.h stand-in mapping VirtualAlloc/VirtualFree/VirtualQuery onto mmap (4 KiB pages, 64 KiB granularity) and _aligned_malloc onto posix_memalign.
    • Before (main allocator, new test): fails at the first case, exponent=13 bytes=1 base_matches=0 state=1000.
    • After: large_alignment_cases=25 committed_bytes=163840 alignment_bytes=10743685120, PASS. allocation, failure, concurrency, caller_layout and reuse pass both before and after.
  • Windows: ctest --preset dev --no-tests=error -R 'era_heap|desktop_memory_query' -V at 3928f00 (rebased on main 1485f17, with the memory-type handling from #134 and the xmem shim; earlier runs at 7d0a713 and 9a09357 gave the same output) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100: passed in Debug (the dev preset) and in Release (cmake --preset dev -B out/build/rel -DCMAKE_BUILD_TYPE=Release, then ctest --test-dir out/build/rel). Both configurations print large_alignment_cases=25 committed_bytes=163840 alignment_bytes=10743685120 and reuse_cases=9 recycled_address=9. The full Debug ctest --preset dev --no-tests=error passed 120/120, with bvh_wave64_hardware, bvh_wave64_warp and wave_shuffle_warp skipped.

Evidence and follow-up

  • Exponent 31 reserves 2 GiB + one page of address space per call but commits one page. Whether Scarlett's default heap honors alignments this large, and what it commits for them, remains unknown.
  • The +alignment probe reservation is released before the aligned commit. VirtualAlloc2 with MEM_ADDRESS_REQUIREMENTS would avoid the retry, but it requires onecore.lib.
  • Rebased onto the memory-type handling now on main: the page-backed path applies only to heap requests (memory type 0); graphics types keep their VirtualAlloc path, and release_with_attributes routes heap blocks through release. era_heap_observed_words passes unchanged.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532531139 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/182). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T18:07:07Z. --- ## Change `era_heap::allocate` passed every alignment exponent (bits 24–28, up to 31) to `_aligned_malloc`. The CRT over-allocates by the alignment and commits that padding as heap memory, so a 257-byte request with exponent 31 committed more than 2 GiB, and exponent 20 committed 1 MiB. - `src/era_heap.cpp`: alignments above the page size get their own reservation. The allocator reserves `payload + alignment` address space, releases it, and reserves and commits only the page-rounded payload at the aligned address inside it, retrying up to eight times if another thread takes the range. `VirtualAlloc` memory is already zero, so no `memset` touches it. Alignments up to one page keep the `_aligned_malloc` path unchanged. - `release` frees a block with `VirtualFree(MEM_RELEASE)` when it is aligned to the allocation granularity and `VirtualQuery` reports it as the `AllocationBase`. A CRT block cannot pass this test: `_aligned_malloc` stores its base pointer below the returned address, so the block never starts its own allocation. Other pointers go to `_aligned_free` as before. - `tests/era_heap_check.cpp`, `CMakeLists.txt`: new `era_heap_large_alignment` scenario. Exponents 13, 16, 17, 20 and 31 with payloads of 1, 257, one page, one page + 1 and three pages − 1 bytes. Each block must be aligned and zeroed. `VirtualQuery` must report `AllocationBase == BaseAddress == block`, `MEM_COMMIT`, `PAGE_READWRITE`, `MEM_PRIVATE` and `RegionSize` equal to the page-rounded payload. After release the address must be `MEM_FREE`. - `docs/experiments.md`: notes the deviation from SlimEra's `XMemAllocDefault`, which uses `_aligned_malloc` for all heap requests, and adds the test row. `tools/pe_inventory.py`: links `XMemAlloc`/`XMemFree` to the new test. Attribute decoding is unchanged. The accepted attribute set, the 16-byte minimum alignment and the failure policy are the same as before. ## Verification - Linux stand-in: `src/era_heap.cpp` and `tests/era_heap_check.cpp` compiled with clang++ 18 (`-std=c++20 -Wall -Wextra`, no warnings) against a `windows.h`/`malloc.h` stand-in mapping `VirtualAlloc`/`VirtualFree`/`VirtualQuery` onto `mmap` (4 KiB pages, 64 KiB granularity) and `_aligned_malloc` onto `posix_memalign`. - Before (main allocator, new test): fails at the first case, `exponent=13 bytes=1 base_matches=0 state=1000`. - After: `large_alignment_cases=25 committed_bytes=163840 alignment_bytes=10743685120`, PASS. `allocation`, `failure`, `concurrency`, `caller_layout` and `reuse` pass both before and after. - Windows: `ctest --preset dev --no-tests=error -R 'era_heap|desktop_memory_query' -V` at 3928f00 (rebased on main 1485f17, with the memory-type handling from #134 and the xmem shim; earlier runs at 7d0a713 and 9a09357 gave the same output) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100: passed in Debug (the `dev` preset) and in Release (`cmake --preset dev -B out/build/rel -DCMAKE_BUILD_TYPE=Release`, then `ctest --test-dir out/build/rel`). Both configurations print `large_alignment_cases=25 committed_bytes=163840 alignment_bytes=10743685120` and `reuse_cases=9 recycled_address=9`. The full Debug `ctest --preset dev --no-tests=error` passed 120/120, with `bvh_wave64_hardware`, `bvh_wave64_warp` and `wave_shuffle_warp` skipped. ## Evidence and follow-up - Exponent 31 reserves 2 GiB + one page of address space per call but commits one page. Whether Scarlett's default heap honors alignments this large, and what it commits for them, remains unknown. - The `+alignment` probe reservation is released before the aligned commit. `VirtualAlloc2` with `MEM_ADDRESS_REQUIREMENTS` would avoid the retry, but it requires `onecore.lib`. - Rebased onto the memory-type handling now on main: the page-backed path applies only to heap requests (memory type 0); graphics types keep their `VirtualAlloc` path, and `release_with_attributes` routes heap blocks through `release`. `era_heap_observed_words` passes unchanged.
upstream-sync closed this pull request 2026-11-24 06:41:42 +00:00
R0Y00 commented 2026-11-24 06:47:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:16:29Z.


GitHub reports a conflict on 7d0a713c. Please reconcile the high-alignment path with the current ERA heap attribute decoder. Retain the Bink-sized heap and graphics allocation cases alongside the new payload commit test before marking ready.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067132844 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/182#issuecomment-7067132844). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:16:29Z. --- GitHub reports a conflict on `7d0a713c`. Please reconcile the high-alignment path with the current ERA heap attribute decoder. Retain the Bink-sized heap and graphics allocation cases alongside the new payload commit test before marking ready.
chatgpt-codex-connector[bot commented 2026-11-24 06:47:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T19:22:28Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T19:25:01.398372Z 3928f00 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067973483 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/182#issuecomment-7067973483). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T19:22:28Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"3928f00326e16b2957f2b2b6a1535aa70a9220b7","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-21T19:25:01.398372Z">2026-11-21T19:25:01.398372Z</relative-time> | `3928f00` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
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!181
No description provided.