fix Windows fixed placeholder reservations below allocation granularity #101

Closed
github-bot wants to merge 4 commits from gh-pr/101/head into gh-pr/101/base
Owner

@⁠Digote on GitHub · 2026-07-22 17:30 UTC · original
fix/windows-subgranularity-placeholder → main · +455 −29 · 4 files · 4 commits · closed without merging

What changed

  • Fix Windows fixed placeholder reservations when a PS5 16 KiB mapping starts at an address that is not aligned to the Windows allocation granularity.
  • Reserve the enclosing allocation-granularity placeholder container, split it, release the unused neighboring ranges, and retain an exact placeholder for the requested range.
  • Add FixedSub64KDirectMapUsesExactAddress, covering fixed reservation, exact direct-memory mapping, shared-backing aliasing, unmap, placeholder restoration, and release of the neighboring host ranges.

Why

UFC 5 fails early in #100 while mapping EAOSGlobal at 0xfbffffc000 with a size of 0x4000.

VirtualAlloc2 requires a non-null fixed base address to be aligned to the system allocation granularity. The affected address is a valid 16 KiB PS5-page address but is offset within a 64 KiB Windows allocation-granularity range.

The fix reserves an aligned placeholder container and uses placeholder splitting so that MapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER) receives a placeholder whose base address and size exactly match the requested mapping.

Documentation

Validation

  • Built on Windows with the project CMake/Ninja configuration:
    cmake --build Build\windows --target kyty_emulator virtual_memory_allocation_tests
  • Ran virtual_memory_allocation_tests: 27/27 cases passed.
  • git diff --check passed.
  • Ran clang-format validation for src/kernel/memoryAddressSpace.inc.
  • Reproduced UFC 5 (PPSA03541): the original placeholder-reserve-failed / fatal error no longer occurs. EAOSGlobal now maps at 0xfbffffc000 with shared = yes.

Scope

This partially addresses #100. The new UFC 5 log proceeds far beyond the original memory failure but later stops on an unrelated unsupported storage texture renderer error, so this PR should not close #100.

AI assistance disclosure

AI assistance was used for investigation, implementation support, test design, and documentation research. I have reviewed the final diff and validation results and remain responsible for this contribution.

**@⁠Digote** on GitHub · 2026-07-22 17:30 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101) `fix/windows-subgranularity-placeholder` → `main` · +455 −29 · 4 files · 4 commits · closed without merging ## What changed - Fix Windows fixed placeholder reservations when a PS5 16 KiB mapping starts at an address that is not aligned to the Windows allocation granularity. - Reserve the enclosing allocation-granularity placeholder container, split it, release the unused neighboring ranges, and retain an exact placeholder for the requested range. - Add `FixedSub64KDirectMapUsesExactAddress`, covering fixed reservation, exact direct-memory mapping, shared-backing aliasing, unmap, placeholder restoration, and release of the neighboring host ranges. ## Why UFC 5 fails early in #100 while mapping `EAOSGlobal` at `0xfbffffc000` with a size of `0x4000`. `VirtualAlloc2` requires a non-null fixed base address to be aligned to the system allocation granularity. The affected address is a valid 16 KiB PS5-page address but is offset within a 64 KiB Windows allocation-granularity range. The fix reserves an aligned placeholder container and uses placeholder splitting so that `MapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER)` receives a placeholder whose base address and size exactly match the requested mapping. ## Documentation - https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc2 - https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile3 - https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree ## Validation - Built on Windows with the project CMake/Ninja configuration: `cmake --build Build\windows --target kyty_emulator virtual_memory_allocation_tests` - Ran `virtual_memory_allocation_tests`: 27/27 cases passed. - `git diff --check` passed. - Ran clang-format validation for `src/kernel/memoryAddressSpace.inc`. - Reproduced UFC 5 (`PPSA03541`): the original `placeholder-reserve-failed` / fatal error no longer occurs. `EAOSGlobal` now maps at `0xfbffffc000` with `shared = yes`. ## Scope This partially addresses #100. The new UFC 5 log proceeds far beyond the original memory failure but later stops on an unrelated `unsupported storage texture` renderer error, so this PR should not close #100. ## AI assistance disclosure AI assistance was used for investigation, implementation support, test design, and documentation research. I have reviewed the final diff and validation results and remain responsible for this contribution.
Track allocation-granularity containers created for fixed sub-64K reservations and release their retained placeholder siblings when the last virtual-range owner is removed.

Extend the regression test to verify every sibling becomes MEM_FREE and that the full container can be reused at the same address.
Reuse tracked allocation-granularity placeholder containers and reserve only missing containers when a fixed guest range crosses their boundaries.

Add a regression test for a non-overlapping cross-container reservation whose first container is already tracked.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:10:24 +00:00
github-bot closed this pull request 2026-11-26 09:10:24 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-07-22 17:35 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ebe84caa72

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-07-22 17:35 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#pullrequestreview-4757040567) · **reviewed** ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `ebe84caa72` <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". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback". </details>
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-07-22 17:35 UTC · original
On src/kernel/memoryAddressSpace.inc line 1161 (outdated):

@@ -1119,20 +1119,47 @@ public:
 		if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) {
 			return false;
 		}
+		if (UINT64_MAX - vaddr < size) {
+			return false;
+		}
 		{
 			std::lock_guard lock(m_mutex);
 			if (FindContaining(vaddr, size) != m_free.end()) {
 				return true;
 			}
 		}
-		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size,
-		                          MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0);
-		if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) {
+
+		// VirtualAlloc2 requires a fixed base address to use the host allocation
+		// granularity. Reserve the enclosing container, then keep only the
+		// requested PS5-page range as a free placeholder for MapViewOfFile3.
+		const auto container_start = vaddr & ~(m_granularity - 1u);
+		if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) {
+			return false;
+		}
+		const auto container_end  = AlignUp(vaddr + size, m_granularity);
+		const auto container_size = container_end - container_start;
+		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start),
+		                          container_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER,
+		                          PAGE_NOACCESS, nullptr, 0);
+		if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != container_start) {
 			if (ptr != nullptr) {
 				VirtualFree(ptr, 0, MEM_RELEASE);
 			}
 			return false;
 		}
+		AddFree(container_start, container_size);
+		if (!Consume(vaddr, size)) {
+			return false;
+		}
+
+		const auto left_size  = vaddr - container_start;
+		const auto right_size = container_end - (vaddr + size);
+		if (left_size != 0) {
+			ReleaseFree(container_start, left_size);
+		}
+		if (right_size != 0) {
+			ReleaseFree(vaddr + size, right_size);

P2 Badge Retain sibling placeholders for later 16 KiB reserves

On Windows, after reserving one fixed 16 KiB placeholder inside a 64 KiB allocation-granularity block, these calls release the other pages in that block. If the guest then reserves or maps another 16 KiB page in the same block before unmapping the first, ReserveFixed can no longer recreate a placeholder for it: the enclosing VirtualAlloc2 conflicts with the existing placeholder, and the fallback ReserveFixedHostRange/VirtualAlloc path is rounded to the same occupied container. This makes valid adjacent PS5-page fixed reservations fail with ENOMEM; keep the sibling placeholders tracked or otherwise handle partially occupied containers.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-07-22 17:35 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#discussion_r3632414167) On `src/kernel/memoryAddressSpace.inc` line 1161 (outdated): ```diff @@ -1119,20 +1119,47 @@ public: if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) { return false; } + if (UINT64_MAX - vaddr < size) { + return false; + } { std::lock_guard lock(m_mutex); if (FindContaining(vaddr, size) != m_free.end()) { return true; } } - auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size, - MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0); - if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) { + + // VirtualAlloc2 requires a fixed base address to use the host allocation + // granularity. Reserve the enclosing container, then keep only the + // requested PS5-page range as a free placeholder for MapViewOfFile3. + const auto container_start = vaddr & ~(m_granularity - 1u); + if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) { + return false; + } + const auto container_end = AlignUp(vaddr + size, m_granularity); + const auto container_size = container_end - container_start; + auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start), + container_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, + PAGE_NOACCESS, nullptr, 0); + if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != container_start) { if (ptr != nullptr) { VirtualFree(ptr, 0, MEM_RELEASE); } return false; } + AddFree(container_start, container_size); + if (!Consume(vaddr, size)) { + return false; + } + + const auto left_size = vaddr - container_start; + const auto right_size = container_end - (vaddr + size); + if (left_size != 0) { + ReleaseFree(container_start, left_size); + } + if (right_size != 0) { + ReleaseFree(vaddr + size, right_size); ``` **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Retain sibling placeholders for later 16 KiB reserves** On Windows, after reserving one fixed 16 KiB placeholder inside a 64 KiB allocation-granularity block, these calls release the other pages in that block. If the guest then reserves or maps another 16 KiB page in the same block before unmapping the first, `ReserveFixed` can no longer recreate a placeholder for it: the enclosing `VirtualAlloc2` conflicts with the existing placeholder, and the fallback `ReserveFixedHostRange`/`VirtualAlloc` path is rounded to the same occupied container. This makes valid adjacent PS5-page fixed reservations fail with ENOMEM; keep the sibling placeholders tracked or otherwise handle partially occupied containers. Useful? React with 👍 / 👎.
Author
Owner

@⁠Digote on GitHub · 2026-07-22 18:15 UTC · original
On src/kernel/memoryAddressSpace.inc line 1161 (outdated):

@@ -1119,20 +1119,47 @@ public:
 		if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) {
 			return false;
 		}
+		if (UINT64_MAX - vaddr < size) {
+			return false;
+		}
 		{
 			std::lock_guard lock(m_mutex);
 			if (FindContaining(vaddr, size) != m_free.end()) {
 				return true;
 			}
 		}
-		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size,
-		                          MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0);
-		if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) {
+
+		// VirtualAlloc2 requires a fixed base address to use the host allocation
+		// granularity. Reserve the enclosing container, then keep only the
+		// requested PS5-page range as a free placeholder for MapViewOfFile3.
+		const auto container_start = vaddr & ~(m_granularity - 1u);
+		if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) {
+			return false;
+		}
+		const auto container_end  = AlignUp(vaddr + size, m_granularity);
+		const auto container_size = container_end - container_start;
+		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start),
+		                          container_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER,
+		                          PAGE_NOACCESS, nullptr, 0);
+		if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != container_start) {
 			if (ptr != nullptr) {
 				VirtualFree(ptr, 0, MEM_RELEASE);
 			}
 			return false;
 		}
+		AddFree(container_start, container_size);
+		if (!Consume(vaddr, size)) {
+			return false;
+		}
+
+		const auto left_size  = vaddr - container_start;
+		const auto right_size = container_end - (vaddr + size);
+		if (left_size != 0) {
+			ReleaseFree(container_start, left_size);
+		}
+		if (right_size != 0) {
+			ReleaseFree(vaddr + size, right_size);

Thanks, good catch! 👍

I fixed this in a5637b3.

The old code was releasing the neighboring placeholder ranges too early. Now it keeps the full 64 KiB placeholder reserved, so another adjacent 16 KiB reservation can still use the same block.

I also added a regression test for that adjacent 16 KiB case. All Windows virtual-memory tests are passing (27/27).

**@⁠Digote** on GitHub · 2026-07-22 18:15 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#discussion_r3632697253) On `src/kernel/memoryAddressSpace.inc` line 1161 (outdated): ```diff @@ -1119,20 +1119,47 @@ public: if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) { return false; } + if (UINT64_MAX - vaddr < size) { + return false; + } { std::lock_guard lock(m_mutex); if (FindContaining(vaddr, size) != m_free.end()) { return true; } } - auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size, - MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0); - if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) { + + // VirtualAlloc2 requires a fixed base address to use the host allocation + // granularity. Reserve the enclosing container, then keep only the + // requested PS5-page range as a free placeholder for MapViewOfFile3. + const auto container_start = vaddr & ~(m_granularity - 1u); + if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) { + return false; + } + const auto container_end = AlignUp(vaddr + size, m_granularity); + const auto container_size = container_end - container_start; + auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start), + container_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, + PAGE_NOACCESS, nullptr, 0); + if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != container_start) { if (ptr != nullptr) { VirtualFree(ptr, 0, MEM_RELEASE); } return false; } + AddFree(container_start, container_size); + if (!Consume(vaddr, size)) { + return false; + } + + const auto left_size = vaddr - container_start; + const auto right_size = container_end - (vaddr + size); + if (left_size != 0) { + ReleaseFree(container_start, left_size); + } + if (right_size != 0) { + ReleaseFree(vaddr + size, right_size); ``` Thanks, good catch! 👍 I fixed this in a5637b3. The old code was releasing the neighboring placeholder ranges too early. Now it keeps the full 64 KiB placeholder reserved, so another adjacent 16 KiB reservation can still use the same block. I also added a regression test for that adjacent 16 KiB case. All Windows virtual-memory tests are passing (27/27).
Author
Owner

@⁠Nmzik on GitHub · 2026-07-29 06:12 UTC · original · reviewed

The adjacent-page fix addresses the earlier review finding, but there is one remaining non-blocking resource-lifetime issue in the retained sibling placeholders. CI could not be inspected because the workflow stopped before creating any jobs.

**@⁠Nmzik** on GitHub · 2026-07-29 06:12 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#pullrequestreview-4804606928) · **reviewed** The adjacent-page fix addresses the earlier review finding, but there is one remaining non-blocking resource-lifetime issue in the retained sibling placeholders. CI could not be inspected because the workflow stopped before creating any jobs.
Author
Owner

@⁠Nmzik on GitHub · 2026-07-29 06:12 UTC · original
On src/kernel/memoryAddressSpace.inc line 1150 (outdated):

@@ -1119,21 +1119,35 @@ public:
 		if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) {
 			return false;
 		}
+		if (UINT64_MAX - vaddr < size) {
+			return false;
+		}
 		{
 			std::lock_guard lock(m_mutex);
 			if (FindContaining(vaddr, size) != m_free.end()) {
 				return true;
 			}
 		}
-		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size,
-		                          MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0);
-		if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) {
+
+		// VirtualAlloc2 requires a fixed base address to use the host allocation
+		// granularity. Reserve and retain the enclosing container as placeholders;
+		// Consume() will isolate the exact PS5-page range for MapViewOfFile3.
+		const auto container_start = vaddr & ~(m_granularity - 1u);
+		if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) {
+			return false;
+		}
+		const auto container_end  = AlignUp(vaddr + size, m_granularity);
+		const auto container_size = container_end - container_start;
+		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start),
+		                          container_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER,
+		                          PAGE_NOACCESS, nullptr, 0);
+		if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != container_start) {
 			if (ptr != nullptr) {
 				VirtualFree(ptr, 0, MEM_RELEASE);
 			}
 			return false;
 		}
-		AddFree(vaddr, size);
+		AddFree(container_start, container_size);

This retains the entire aligned container in m_free, but the virtual-range layer records only the requested 16 KiB range. When that range is later released, ReleaseFree(vaddr, size) frees only those 16 KiB; the unowned sibling placeholders remain reserved and tracked for the rest of the process. Repeating isolated sub-granularity reservations therefore leaks up to 48 KiB of host address space per 16 KiB guest range and can block unrelated host reservations in those gaps. Please release the enclosing container once none of its subranges are owned, and extend the test to verify all sibling addresses become unmapped after the requested ranges are released.

**@⁠Nmzik** on GitHub · 2026-07-29 06:12 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#discussion_r3671487341) On `src/kernel/memoryAddressSpace.inc` line 1150 (outdated): ```diff @@ -1119,21 +1119,35 @@ public: if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) { return false; } + if (UINT64_MAX - vaddr < size) { + return false; + } { std::lock_guard lock(m_mutex); if (FindContaining(vaddr, size) != m_free.end()) { return true; } } - auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size, - MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0); - if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) { + + // VirtualAlloc2 requires a fixed base address to use the host allocation + // granularity. Reserve and retain the enclosing container as placeholders; + // Consume() will isolate the exact PS5-page range for MapViewOfFile3. + const auto container_start = vaddr & ~(m_granularity - 1u); + if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) { + return false; + } + const auto container_end = AlignUp(vaddr + size, m_granularity); + const auto container_size = container_end - container_start; + auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start), + container_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, + PAGE_NOACCESS, nullptr, 0); + if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != container_start) { if (ptr != nullptr) { VirtualFree(ptr, 0, MEM_RELEASE); } return false; } - AddFree(vaddr, size); + AddFree(container_start, container_size); ``` This retains the entire aligned container in `m_free`, but the virtual-range layer records only the requested 16 KiB range. When that range is later released, `ReleaseFree(vaddr, size)` frees only those 16 KiB; the unowned sibling placeholders remain reserved and tracked for the rest of the process. Repeating isolated sub-granularity reservations therefore leaks up to 48 KiB of host address space per 16 KiB guest range and can block unrelated host reservations in those gaps. Please release the enclosing container once none of its subranges are owned, and extend the test to verify all sibling addresses become unmapped after the requested ranges are released.
Author
Owner

@⁠Digote on GitHub · 2026-07-29 13:29 UTC · original

Thanks for the detailed explanation. I've addressed the container lifetime issue in 399dc7f.

Fixed allocation-granularity containers are now tracked separately from their individual guest ranges. When a range gets released, we check the virtual-range ownership of each 64 KiB container:

  • If another guest subrange still occupies the container, all sibling placeholders are kept.
  • Once the last owner is removed, every retained placeholder fragment in the container is released and the container gets pulled from tracking.

Same cleanup applies to the unmap and rollback paths too, so a failed mapping can't leave an unowned container behind.

I extended FixedSub64KDirectMapUsesExactAddress to check that:

  • releasing the first 16 KiB range keeps its placeholder while the adjacent range is still owned
  • after releasing the final owner, all four 16 KiB addresses in the 64 KiB container report MEM_FREE
  • the full 64 KiB container can be reserved again at the same address and released cleanly

Validation:

  • cmake --build Build\windows --target virtual_memory_allocation_tests
  • virtual_memory_allocation_tests: 27/27 passing
  • git diff --check clean
  • clang-format clean

Also reproduced UFC 5 again — EAOSGlobal still maps fine at 0xfbffffc000 with shared backing, no placeholder reservation or container-release errors.

**@⁠Digote** on GitHub · 2026-07-29 13:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#issuecomment-5118416758) Thanks for the detailed explanation. I've addressed the container lifetime issue in 399dc7f. Fixed allocation-granularity containers are now tracked separately from their individual guest ranges. When a range gets released, we check the virtual-range ownership of each 64 KiB container: - If another guest subrange still occupies the container, all sibling placeholders are kept. - Once the last owner is removed, every retained placeholder fragment in the container is released and the container gets pulled from tracking. Same cleanup applies to the unmap and rollback paths too, so a failed mapping can't leave an unowned container behind. I extended FixedSub64KDirectMapUsesExactAddress to check that: - releasing the first 16 KiB range keeps its placeholder while the adjacent range is still owned - after releasing the final owner, all four 16 KiB addresses in the 64 KiB container report MEM_FREE - the full 64 KiB container can be reserved again at the same address and released cleanly Validation: - cmake --build Build\windows --target virtual_memory_allocation_tests - virtual_memory_allocation_tests: 27/27 passing - git diff --check clean - clang-format clean Also reproduced UFC 5 again — EAOSGlobal still maps fine at 0xfbffffc000 with shared backing, no placeholder reservation or container-release errors.
Author
Owner

@⁠Nmzik on GitHub · 2026-07-29 14:07 UTC · original · requested changes

The new container-lifetime cleanup addresses the previously reported sibling-placeholder leak, but valid fixed reservations can still fail when they cross from an existing tracked 64 KiB container into an unreserved one. CI could not be inspected because the workflow stopped before creating any jobs.

**@⁠Nmzik** on GitHub · 2026-07-29 14:07 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#pullrequestreview-4809176579) · **requested changes** The new container-lifetime cleanup addresses the previously reported sibling-placeholder leak, but valid fixed reservations can still fail when they cross from an existing tracked 64 KiB container into an unreserved one. CI could not be inspected because the workflow stopped before creating any jobs.
Author
Owner

@⁠Nmzik on GitHub · 2026-07-29 14:07 UTC · original
On src/kernel/memoryAddressSpace.inc line 1141 (outdated):

@@ -1119,21 +1119,44 @@ public:
 		if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) {
 			return false;
 		}
+		if (UINT64_MAX - vaddr < size) {
+			return false;
+		}
 		{
 			std::lock_guard lock(m_mutex);
 			if (FindContaining(vaddr, size) != m_free.end()) {
 				return true;
 			}
 		}
-		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size,
-		                          MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0);
-		if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) {
+
+		// VirtualAlloc2 requires a fixed base address to use the host allocation
+		// granularity. Reserve and retain the enclosing container as placeholders;
+		// Consume() will isolate the exact PS5-page range for MapViewOfFile3.
+		const auto container_start = vaddr & ~(m_granularity - 1u);
+		if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) {
+			return false;
+		}
+		const auto container_end  = AlignUp(vaddr + size, m_granularity);
+		const auto container_size = container_end - container_start;
+		auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start),

ReserveFixed returns early only when one m_free entry contains the entire request. For example, after reserving [base, base + 0x4000), a valid non-overlapping reserve of [base + 0x4000, base + 0x14000) spans the remainder of that tracked 64 KiB container and the first page of the next container. FindContaining fails, then this call tries to reserve both aligned containers at once; Windows rejects it because the first container is already reserved, so the guest request incorrectly returns ENOMEM. Please combine existing tracked placeholder containers with reservations for only the missing containers, and add a regression covering a cross-container request with the first container already present.

**@⁠Nmzik** on GitHub · 2026-07-29 14:07 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#discussion_r3675026095) On `src/kernel/memoryAddressSpace.inc` line 1141 (outdated): ```diff @@ -1119,21 +1119,44 @@ public: if (vaddr == 0 || size == 0 || !IsPageAligned(vaddr) || !IsPageAligned(size)) { return false; } + if (UINT64_MAX - vaddr < size) { + return false; + } { std::lock_guard lock(m_mutex); if (FindContaining(vaddr, size) != m_free.end()) { return true; } } - auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(vaddr), size, - MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0); - if (ptr == nullptr || reinterpret_cast<uint64_t>(ptr) != vaddr) { + + // VirtualAlloc2 requires a fixed base address to use the host allocation + // granularity. Reserve and retain the enclosing container as placeholders; + // Consume() will isolate the exact PS5-page range for MapViewOfFile3. + const auto container_start = vaddr & ~(m_granularity - 1u); + if (UINT64_MAX - (m_granularity - 1u) < vaddr + size) { + return false; + } + const auto container_end = AlignUp(vaddr + size, m_granularity); + const auto container_size = container_end - container_start; + auto* ptr = VirtualAlloc2(GetCurrentProcess(), reinterpret_cast<void*>(container_start), ``` `ReserveFixed` returns early only when one `m_free` entry contains the entire request. For example, after reserving `[base, base + 0x4000)`, a valid non-overlapping reserve of `[base + 0x4000, base + 0x14000)` spans the remainder of that tracked 64 KiB container and the first page of the next container. `FindContaining` fails, then this call tries to reserve both aligned containers at once; Windows rejects it because the first container is already reserved, so the guest request incorrectly returns `ENOMEM`. Please combine existing tracked placeholder containers with reservations for only the missing containers, and add a regression covering a cross-container request with the first container already present.
Author
Owner

@⁠Digote on GitHub · 2026-07-29 15:02 UTC · original

Thanks, fixed in 2c41030. ReserveFixed now reuses tracked 64 KiB containers, reserves only missing ones, and rolls back newly created containers on failure. I added the exact cross-container regression case; all 28 Windows virtual-memory tests pass, and UFC 5 still maps EAOSGlobal correctly without placeholder errors.

**@⁠Digote** on GitHub · 2026-07-29 15:02 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#issuecomment-5119607601) Thanks, fixed in 2c41030. ReserveFixed now reuses tracked 64 KiB containers, reserves only missing ones, and rolls back newly created containers on failure. I added the exact cross-container regression case; all 28 Windows virtual-memory tests pass, and UFC 5 still maps EAOSGlobal correctly without placeholder errors.
Author
Owner

@⁠Digote on GitHub · 2026-07-31 02:33 UTC · original

It looks like the UFC 5 issue has already been resolved with #135
Closing.

**@⁠Digote** on GitHub · 2026-07-31 02:33 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#issuecomment-5138586111) It looks like the UFC 5 issue has already been resolved with https://git.gtahub.st/github/KytyPS5--KytyPS5/pulls/135 Closing.
Author
Owner

@⁠Nmzik on GitHub · 2026-07-31 03:59 UTC · original

@⁠Digote Didnt expect that change to fix UFC5 memory allocation bug. Thanks for reporting

**@⁠Nmzik** on GitHub · 2026-07-31 03:59 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#issuecomment-5139102406) @⁠Digote Didnt expect that change to fix UFC5 memory allocation bug. Thanks for reporting
Author
Owner

@⁠Digote on GitHub · 2026-07-31 11:14 UTC · original

@⁠Nmzik test github.com/KytyPS5/KytyPS5@d8a4c83cc7
Teste

**@⁠Digote** on GitHub · 2026-07-31 11:14 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/101#issuecomment-5142252432) @⁠Nmzik test https://github.com/KytyPS5/KytyPS5/commit/d8a4c83cc7d343b1c7a2702084f3c282fbd00cd4 <img width="1731" height="791" alt="Teste" src="https://github.com/user-attachments/assets/d039d1c8-7ff9-4be3-95e5-c7be01338e8c" />

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.