Decide whether pe_map_release should reject a pointer pe_map_image did not return #163

Closed
opened 2026-11-21 17:50:34 +00:00 by raulmoracode · 4 comments
raulmoracode commented 2026-11-21 17:50:34 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by raulmoracode on 2026-11-21T17:50:34Z.


Question

Should pe_map_release reject a pointer that pe_map_image did not return, and should a second release of the same image be rejected instead of reaching VirtualFree?

Today pe_map_release dereferences DOS and NT headers directly from the argument and then calls VirtualFree on it. There is no registry of live images, no ownership tag and no double-release guard. The header read is only valid for a base returned by pe_map_image, which is stated in a comment but not enforced.

Evidence so far

  • tools/pe_map.cpp, pe_map_release: Image{base, 0}.headers() reads e_lfanew from the argument before any check, then reads OptionalHeader.SizeOfImage and the TLS and .pdata directories.
  • pe_map_image allocates with VirtualAlloc(nullptr, SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) and returns the base without recording it anywhere.
  • The existing tests release each base exactly once, so neither a foreign pointer nor a double release is covered.
  • docs/experiments.md describes the mapped-image lifecycle for callbacks but does not state what happens for a pointer the mapper did not return.

Two possible directions

  1. Registry. Keep a process-wide set of live bases behind a mutex. pe_map_release returns early (or sets a named status) when the pointer is not in the set, and removes it on success. This makes double release and foreign pointers safe and testable, at the cost of shared state.
  2. Contract. Document that the pointer must come from pe_map_image and only guard against an uncommitted or non-image page with VirtualQuery, without tracking ownership. Cheaper, but a double release can still reach VirtualFree.

Either way the behaviour needs to be written down before a test asserts it.

Completion condition

A maintainer picks one direction, and a source-built test proves the chosen behaviour:

  • foreign pointer: no crash, defined result;
  • double release: rejected or defined;
  • a valid image still releases and unregisters its .pdata table.

Required environment

x64 Windows, MSVC, CMake/Ninja, Python 3.11 or newer with pefile. No game files.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532352361 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/164). Originally posted by [raulmoracode](https://github.com/raulmoracode) on 2026-11-21T17:50:34Z. --- ## Question Should `pe_map_release` reject a pointer that `pe_map_image` did not return, and should a second release of the same image be rejected instead of reaching `VirtualFree`? Today `pe_map_release` dereferences DOS and NT headers directly from the argument and then calls `VirtualFree` on it. There is no registry of live images, no ownership tag and no double-release guard. The header read is only valid for a base returned by `pe_map_image`, which is stated in a comment but not enforced. ## Evidence so far - `tools/pe_map.cpp`, `pe_map_release`: `Image{base, 0}.headers()` reads `e_lfanew` from the argument before any check, then reads `OptionalHeader.SizeOfImage` and the TLS and `.pdata` directories. - `pe_map_image` allocates with `VirtualAlloc(nullptr, SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)` and returns the base without recording it anywhere. - The existing tests release each base exactly once, so neither a foreign pointer nor a double release is covered. - `docs/experiments.md` describes the mapped-image lifecycle for callbacks but does not state what happens for a pointer the mapper did not return. ## Two possible directions 1. **Registry.** Keep a process-wide set of live bases behind a mutex. `pe_map_release` returns early (or sets a named status) when the pointer is not in the set, and removes it on success. This makes double release and foreign pointers safe and testable, at the cost of shared state. 2. **Contract.** Document that the pointer must come from `pe_map_image` and only guard against an uncommitted or non-image page with `VirtualQuery`, without tracking ownership. Cheaper, but a double release can still reach `VirtualFree`. Either way the behaviour needs to be written down before a test asserts it. ## Completion condition A maintainer picks one direction, and a source-built test proves the chosen behaviour: - foreign pointer: no crash, defined result; - double release: rejected or defined; - a valid image still releases and unregisters its `.pdata` table. ## Required environment x64 Windows, MSVC, CMake/Ninja, Python 3.11 or newer with `pefile`. No game files. _AI-assisted._
R0Y00 commented 2026-11-24 06:47:45 +00:00 (Migrated from github.com)
Author
Contributor

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


Decision requested from the lead developer. Should callers be required to pass a currently live base returned by pe_map_image exactly once, or should the API guarantee a defined result for foreign and already-released pointers? Please specify the observable result, the existing null behavior and what callers can rely on if the same address is reused. This determines the ownership tracking and the regression tests. Leaving this issue open pending that decision.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067028724 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/164#issuecomment-7067028724). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:08:14Z. --- Decision requested from the lead developer. Should callers be required to pass a currently live base returned by `pe_map_image` exactly once, or should the API guarantee a defined result for foreign and already-released pointers? Please specify the observable result, the existing null behavior and what callers can rely on if the same address is reused. This determines the ownership tracking and the regression tests. Leaving this issue open pending that decision.
R0Y00 commented 2026-11-24 06:47:45 +00:00 (Migrated from github.com)
Author
Contributor

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


Related implementation: #139 at d18809a5 changes pe_map_release from void to int, returns 0 when the address is absent from its live-image registry, and can defer process detach and unmapping until the final notification reference drops. Please decide the return contract and whether successful release can return before detach finishes. The earlier rebase and lifetime test requests on #139 have been addressed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067045384 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/164#issuecomment-7067045384). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:09:33Z. --- Related implementation: #139 at `d18809a5` changes `pe_map_release` from `void` to `int`, returns 0 when the address is absent from its live-image registry, and can defer process detach and unmapping until the final notification reference drops. Please decide the return contract and whether successful release can return before detach finishes. The earlier rebase and lifetime test requests on #139 have been addressed.
R0Y00 commented 2026-11-24 06:47:52 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:05:08Z.


Decision recorded: use the live-base registry. A successful release returns 1 upon unregistering. Unrecognized bases return 0 without being dereferenced. The final in-flight notification owns deferred cleanup. This issue stays open for the implementation and Windows lifetime/TLS/SEH evidence in #139. Full contract and acceptance criteria

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067748739 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/164#issuecomment-7067748739). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T19:05:08Z. --- Decision recorded: use the live-base registry. A successful release returns 1 upon unregistering. Unrecognized bases return 0 without being dereferenced. The final in-flight notification owns deferred cleanup. This issue stays open for the implementation and Windows lifetime/TLS/SEH evidence in #139. [Full contract and acceptance criteria](https://github.com/R0Y00/Xbox2Windows/pull/139#issuecomment-7067743646)
R0Y00 commented 2026-11-24 06:47:58 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T20:26:15Z.


Implemented in #139 (da2bdd12). The Windows lifetime fixture covers foreign, interior and repeated releases, one winner on concurrent release, and unwind-table removal after the last in-flight notification.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068751575 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/164#issuecomment-7068751575). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T20:26:15Z. --- Implemented in #139 (da2bdd12). The Windows lifetime fixture covers foreign, interior and repeated releases, one winner on concurrent release, and unwind-table removal after the last in-flight notification.
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#163
No description provided.