kernel: return released guest memory to the host #1269

Open
github-bot wants to merge 2 commits from github-bot/gh-1269 into main AGit
Owner

@⁠msfvn on GitHub · 2026-10-09 02:25 UTC · original
msfvn:release-guest-memory → main · +96 −1 · 3 files · 2 commits · open

Problem

Direct and flexible memory share one memfd. ReleaseDirectMemory and flexible Unmap only update the emulator's bookkeeping, so the host keeps (or swaps) every page the game has ever written. FlexibleMemory::Map also memsets the whole new range, committing pages the game may never touch. Astro Bot streams through gigabytes of direct memory while loading; on a machine with 8 GB of RAM the host ran out of memory during the first loading screen.

Change

  • GuestBackingStore::DiscardRange punches the range out of the memfd (fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)); called for released direct memory and unmapped flexible memory.
  • ZeroRange (used when mapping flexible memory) punches a hole instead of memset, falling back to memset where unsupported (Windows, macOS keep their current behavior).
    Both read back as zeros, as before.

Testing

  • New ReleasedMemoryReturnsToHost case in virtual_memory_allocation_tests (Linux): checks that released direct memory and unmapped flexible memory leave the backing (via the memfd's st_blocks) and that mapping flexible memory does not commit it. It fails without this change; all 63 cases pass with it.
  • Astro Bot on 7.5 GB RAM: the first loading screen got ~40 s further before memory ran out.

AI involvement: written with Claude Code (AI assistant) at my request and reviewed by a C++ developer I know; tested on my machine (RTX 3050 Laptop 4 GB, CachyOS) as described above, including playing Astro Bot on this build. Happy to adjust anything you point out.

**@⁠msfvn** on GitHub · 2026-10-09 02:25 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1269) `msfvn:release-guest-memory` → `main` · +96 −1 · 3 files · 2 commits · open ### Problem Direct and flexible memory share one memfd. `ReleaseDirectMemory` and flexible `Unmap` only update the emulator's bookkeeping, so the host keeps (or swaps) every page the game has ever written. `FlexibleMemory::Map` also `memset`s the whole new range, committing pages the game may never touch. Astro Bot streams through gigabytes of direct memory while loading; on a machine with 8 GB of RAM the host ran out of memory during the first loading screen. ### Change - `GuestBackingStore::DiscardRange` punches the range out of the memfd (`fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)`); called for released direct memory and unmapped flexible memory. - `ZeroRange` (used when mapping flexible memory) punches a hole instead of `memset`, falling back to `memset` where unsupported (Windows, macOS keep their current behavior). Both read back as zeros, as before. ### Testing - New `ReleasedMemoryReturnsToHost` case in `virtual_memory_allocation_tests` (Linux): checks that released direct memory and unmapped flexible memory leave the backing (via the memfd's `st_blocks`) and that mapping flexible memory does not commit it. It fails without this change; all 63 cases pass with it. - Astro Bot on 7.5 GB RAM: the first loading screen got ~40 s further before memory ran out. **AI involvement:** written with Claude Code (AI assistant) at my request and reviewed by a C++ developer I know; tested on my machine (RTX 3050 Laptop 4 GB, CachyOS) as described above, including playing Astro Bot on this build. Happy to adjust anything you point out.
Direct and flexible memory live in one memfd. Releasing direct memory or
unmapping flexible memory only updated the emulator's bookkeeping, so the
host kept (or swapped) every page the game had ever written; mapping
flexible memory also memset the whole range, committing pages the game may
never touch. Astro Bot streams through gigabytes of direct memory while
loading, and on an 8 GB machine the host ran out of memory in the first
loading screen.

Punch the released ranges out of the memfd, and zero newly mapped flexible
memory the same way, falling back to memset where that is unsupported.
Both read back as zeros, as before. On Linux the loading screen now gets
about 40 seconds further before memory runs out on that machine.

A new test checks that released direct memory and unmapped flexible
memory leave the backing, and that mapping flexible memory does not
commit it; it fails without this change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Use the error-code overload of directory_iterator, so a missing
/proc/self/fd makes GuestBackingKiB return 0 and the checks report a
normal test failure instead of an uncaught exception.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:45 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-09 02:29 UTC · original · edited 2026-10-11 01:22 UTC

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 960ae3bf-a6fd-47c7-b0b1-8ff8228a2956


📥 Commits

Reviewing files that changed from the base of the PR and between 9e925ee152 and 266eba7276.



📒 Files selected for processing (1)
  • tests/VirtualMemoryAllocationTests.cpp


🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/VirtualMemoryAllocationTests.cpp


Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.




📝 Walkthrough
📝 Walkthrough

Walkthrough

GuestBackingStore now attempts to punch holes in the backing file when it zeroes ranges. It falls back to memset if hole punching fails. Direct-memory release and flexible-memory unmapping now request backing-range discards. Linux tests measure backing-file allocation after release and check flexible-memory mapping behavior.



Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 266eb

The change returns released guest memory to the host on Linux, with a fallback where hole punching is unsupported. No actionable merge-blocking risk is evident from the supplied context.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description clearly explains the host-memory issue, the backing-store changes, and the Linux testing performed.
Title check Passed The title clearly and concisely summarizes the main change: returning released guest memory to the host.

  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-09 02:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1269#issuecomment-6073038440) · edited 2026-10-11 01:22 UTC <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1269?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary><strong>ℹ️ Recent review info</strong></summary> <dl> <dd> <dl> <dd> <details> <summary><strong>⚙️ Run configuration</strong></summary> <dl> <dd> <dl> <dd> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `960ae3bf-a6fd-47c7-b0b1-8ff8228a2956` <hr> </dd> </dl> <hr> </dd> </dl> </details> <details> <summary><strong>📥 Commits</strong></summary> <dl> <dd> <dl> <dd> Reviewing files that changed from the base of the PR and between 9e925ee152cf5fe79acf583af0c9ba235910f66a and 266eba72766b4141eb9e358a600231a90deea959. <hr> </dd> </dl> <hr> </dd> </dl> </details> <details> <summary><strong>📒 Files selected for processing (1)</strong></summary> <dl> <dd> <dl> <dd> * `tests/VirtualMemoryAllocationTests.cpp` <hr> </dd> </dl> <hr> </dd> </dl> </details> <details> <summary><strong>🚧 Files skipped from review as they are similar to previous changes (1)</strong></summary> <dl> <dd> <dl> <dd> * tests/VirtualMemoryAllocationTests.cpp <hr> </dd> </dl> <hr> </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review. <hr> </dd> </dl> <hr> </dd> </dl> </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> ## Walkthrough `GuestBackingStore` now attempts to punch holes in the backing file when it zeroes ranges. It falls back to `memset` if hole punching fails. Direct-memory release and flexible-memory unmapping now request backing-range discards. Linux tests measure backing-file allocation after release and check flexible-memory mapping behavior. <hr> </dd> </dl> </details> <hr> </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"266eba72766b4141eb9e358a600231a90deea959" --> **Change:** Bug fix <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** **⚪ Minimal** · up to `266eb` <!-- final_review_risk_coverage:{"sourceCommitId":"266eba72766b4141eb9e358a600231a90deea959","coveredCommitId":"266eba72766b4141eb9e358a600231a90deea959","kind":"reviewed"} --> The change returns released guest memory to the host on Linux, with a fallback where hole punching is unsupported. No actionable merge-blocking risk is evident from the supplied context. <!-- final_review_risk_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks | <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v1.svg" width="14" height="17" align="middle" alt="Passed" title="Passed"></picture></sup> 4 | <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v1.svg" width="14" height="17" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong>✅ Passed checks (4 passed)</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------ | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description clearly explains the host-memory issue, the backing-store changes, and the Linux testing performed. | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely summarizes the main change: returning released guest memory to the host. | </dd> </dl> </details> <hr> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1269)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) <hr> </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
@ -1493,0 +1509,4 @@
}
}
return 0;
}
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-09 02:29 UTC · original · edited 2026-10-11 02:00 UTC

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '1490,1560p' tests/VirtualMemoryAllocationTests.cpp
rg -n 'RunTest|GuestBackingKiB|KytyDirectMemory' tests/VirtualMemoryAllocationTests.cpp src/kernel/memoryAddressSpace.inc

Repository: KytyPS5/KytyPS5

Length of output: 11663


🏁 Script executed:

set -eu
printf '%s\n' '--- RunTest and nearby test entrypoint ---'
sed -n '270,330p' tests/VirtualMemoryAllocationTests.cpp
sed -n '4435,4600p' tests/VirtualMemoryAllocationTests.cpp
printf '%s\n' '--- includes and Linux setup references ---'
sed -n '1,140p' tests/VirtualMemoryAllocationTests.cpp
rg -n -F --glob '*.{cpp,h,hpp,inc,md,txt,yml,yaml}' -- 'KytyDirectMemory' .
printf '%s\n' '--- backing fd declaration and lifecycle ---'
sed -n '1,125p' src/kernel/memoryAddressSpace.inc
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'm_fd' src/kernel | head -80
printf '%s\n' '--- test build/run environment references ---'
rg -n -i --glob '*.{md,txt,cmake,cpp,h,hpp,yml,yaml,sh}' -- 'VirtualMemoryAllocationTests|/proc/self/fd|procfs|Linux' tests .github cmake CMakeLists.txt 2>/dev/null | head -240

Repository: KytyPS5/KytyPS5

Length of output: 26136


🏁 Script executed:

set -eu
sed -n '285,325p' tests/VirtualMemoryAllocationTests.cpp
sed -n '4470,4590p' tests/VirtualMemoryAllocationTests.cpp
rg -n -F --glob '*.{cpp,h,hpp,inc,md,txt,cmake,yml,yaml,sh}' -- 'KytyDirectMemory' .
rg -n -i --glob '*.{cpp,h,hpp,inc,md,txt,cmake,yml,yaml,sh}' -- 'proc/self/fd|procfs|VirtualMemoryAllocationTests' .
sed -n '1,115p' src/kernel/memoryAddressSpace.inc

Repository: KytyPS5/KytyPS5

Length of output: 11528


🏁 Script executed:

set -eu
printf '%s\n' '--- GuestBackingStore declarations and uses ---'
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'GuestBackingStore' src tests
printf '%s\n' '--- surrounding owners and construction sites ---'
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'make_unique<GuestBackingStore>|unique_ptr<GuestBackingStore>|GuestBackingStore(' src
printf '%s\n' '--- memory address-space owner declarations ---'
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'GuestAddressSpace' src/kernel

Repository: KytyPS5/KytyPS5

Length of output: 2157


🏁 Script executed:

set -eu
printf '%s\n' '--- backing construction and owner ---'
sed -n '690,735p' src/kernel/memoryAddressSpace.inc
sed -n '1410,1455p' src/kernel/memoryAddressSpace.inc
printf '%s\n' '--- address-space type and instance references ---'
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'class GuestAddressSpace|GuestAddressSpace::|new GuestAddressSpace|GuestAddressSpace ' src/kernel
printf '%s\n' '--- local test backing lifetime ---'
sed -n '3560,3650p' src/kernel/memory.cpp

Repository: KytyPS5/KytyPS5

Length of output: 2746


🏁 Script executed:

set -u
printf '%s\n' '--- GuestAddressSpace construction sites ---'
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'new GuestAddressSpace' src tests; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ]
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'GuestAddressSpace(' src tests; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ]
printf '%s\n' '--- likely address-space owner declarations ---'
rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'm_guest_address_space|g_guest_address_space|GuestAddressSpace*' src/kernel; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ]
printf '%s\n' '--- local GuestBackingStore test scope ---'
sed -n '3585,3645p' src/kernel/memory.cpp

Repository: KytyPS5/KytyPS5

Length of output: 2633


Handle iterator-construction errors in GuestBackingKiB.

When /proc/self/fd cannot be opened, std::filesystem::directory_iterator("/proc/self/fd") throws std::filesystem::filesystem_error. RunTest catches only TestFailure, so this exception can terminate the test process. Pass an error code to the iterator constructor. The helper will then return zero, and the existing checks will report a handled test failure.

Suggested fix
-	for (const auto& entry: std::filesystem::directory_iterator("/proc/self/fd")) {
-		std::error_code ec;
+	std::error_code ec;
+	for (const auto& entry: std::filesystem::directory_iterator("/proc/self/fd", ec)) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

#if defined(__linux__)
// Host memory the guest backing holds, in KiB, resident or swapped: the backing is a memfd.
uint64_t GuestBackingKiB() {
	std::error_code ec;
	for (const auto& entry: std::filesystem::directory_iterator("/proc/self/fd", ec)) {
		const auto      target = std::filesystem::read_symlink(entry.path(), ec);
		if (ec || target.string().find("KytyDirectMemory") == std::string::npos) {
			continue;
		}
		struct stat info {};
		if (::stat(entry.path().c_str(), &info) == 0) {
			return static_cast<uint64_t>(info.st_blocks) * 512 / 1024;
		}
	}
	return 0;
}

✅ Addressed in commit 266eba7

**@⁠coderabbitai[bot]** on GitHub · 2026-10-09 02:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1269#discussion_r4226062554) · edited 2026-10-11 02:00 UTC **🩺 Stability & Availability** | **🟡 Minor** | **⚡ Quick win** <details> <summary>🔎 Supported by static analysis</summary> 🏁 Script executed: ```bash sed -n '1490,1560p' tests/VirtualMemoryAllocationTests.cpp rg -n 'RunTest|GuestBackingKiB|KytyDirectMemory' tests/VirtualMemoryAllocationTests.cpp src/kernel/memoryAddressSpace.inc ``` Repository: KytyPS5/KytyPS5 Length of output: 11663 --- 🏁 Script executed: ```bash set -eu printf '%s\n' '--- RunTest and nearby test entrypoint ---' sed -n '270,330p' tests/VirtualMemoryAllocationTests.cpp sed -n '4435,4600p' tests/VirtualMemoryAllocationTests.cpp printf '%s\n' '--- includes and Linux setup references ---' sed -n '1,140p' tests/VirtualMemoryAllocationTests.cpp rg -n -F --glob '*.{cpp,h,hpp,inc,md,txt,yml,yaml}' -- 'KytyDirectMemory' . printf '%s\n' '--- backing fd declaration and lifecycle ---' sed -n '1,125p' src/kernel/memoryAddressSpace.inc rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'm_fd' src/kernel | head -80 printf '%s\n' '--- test build/run environment references ---' rg -n -i --glob '*.{md,txt,cmake,cpp,h,hpp,yml,yaml,sh}' -- 'VirtualMemoryAllocationTests|/proc/self/fd|procfs|Linux' tests .github cmake CMakeLists.txt 2>/dev/null | head -240 ``` Repository: KytyPS5/KytyPS5 Length of output: 26136 --- 🏁 Script executed: ```bash set -eu sed -n '285,325p' tests/VirtualMemoryAllocationTests.cpp sed -n '4470,4590p' tests/VirtualMemoryAllocationTests.cpp rg -n -F --glob '*.{cpp,h,hpp,inc,md,txt,cmake,yml,yaml,sh}' -- 'KytyDirectMemory' . rg -n -i --glob '*.{cpp,h,hpp,inc,md,txt,cmake,yml,yaml,sh}' -- 'proc/self/fd|procfs|VirtualMemoryAllocationTests' . sed -n '1,115p' src/kernel/memoryAddressSpace.inc ``` Repository: KytyPS5/KytyPS5 Length of output: 11528 --- 🏁 Script executed: ```bash set -eu printf '%s\n' '--- GuestBackingStore declarations and uses ---' rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'GuestBackingStore' src tests printf '%s\n' '--- surrounding owners and construction sites ---' rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'make_unique<GuestBackingStore>|unique_ptr<GuestBackingStore>|GuestBackingStore(' src printf '%s\n' '--- memory address-space owner declarations ---' rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'GuestAddressSpace' src/kernel ``` Repository: KytyPS5/KytyPS5 Length of output: 2157 --- 🏁 Script executed: ```bash set -eu printf '%s\n' '--- backing construction and owner ---' sed -n '690,735p' src/kernel/memoryAddressSpace.inc sed -n '1410,1455p' src/kernel/memoryAddressSpace.inc printf '%s\n' '--- address-space type and instance references ---' rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'class GuestAddressSpace|GuestAddressSpace::|new GuestAddressSpace|GuestAddressSpace ' src/kernel printf '%s\n' '--- local test backing lifetime ---' sed -n '3560,3650p' src/kernel/memory.cpp ``` Repository: KytyPS5/KytyPS5 Length of output: 2746 --- 🏁 Script executed: ```bash set -u printf '%s\n' '--- GuestAddressSpace construction sites ---' rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'new GuestAddressSpace' src tests; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ] rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'GuestAddressSpace(' src tests; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ] printf '%s\n' '--- likely address-space owner declarations ---' rg -n -F --glob '*.{cpp,h,hpp,inc}' -- 'm_guest_address_space|g_guest_address_space|GuestAddressSpace*' src/kernel; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ] printf '%s\n' '--- local GuestBackingStore test scope ---' sed -n '3585,3645p' src/kernel/memory.cpp ``` Repository: KytyPS5/KytyPS5 Length of output: 2633 --- </details> **Handle iterator-construction errors in `GuestBackingKiB`.** When `/proc/self/fd` cannot be opened, `std::filesystem::directory_iterator("/proc/self/fd")` throws `std::filesystem::filesystem_error`. `RunTest` catches only `TestFailure`, so this exception can terminate the test process. Pass an error code to the iterator constructor. The helper will then return zero, and the existing checks will report a handled test failure. <details> <summary>Suggested fix</summary> ```diff - for (const auto& entry: std::filesystem::directory_iterator("/proc/self/fd")) { - std::error_code ec; + std::error_code ec; + for (const auto& entry: std::filesystem::directory_iterator("/proc/self/fd", ec)) { ``` </details> <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion #if defined(__linux__) // Host memory the guest backing holds, in KiB, resident or swapped: the backing is a memfd. uint64_t GuestBackingKiB() { std::error_code ec; for (const auto& entry: std::filesystem::directory_iterator("/proc/self/fd", ec)) { const auto target = std::filesystem::read_symlink(entry.path(), ec); if (ec || target.string().find("KytyDirectMemory") == std::string::npos) { continue; } struct stat info {}; if (::stat(entry.path().c_str(), &info) == 0) { return static_cast<uint64_t>(info.st_blocks) * 512 / 1024; } } return 0; } ``` </details> <!-- suggestion_end --> <!-- fingerprinting:phantom:medusa:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:29edb3a0eb37fac2662373c5 --> <!-- This is an auto-generated reply by CodeRabbit --> ✅ Addressed in commit 266eba7
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-09 02:29 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ea210989-1f0f-41ed-a51f-aa4a525eb994
📥 Commits

Reviewing files that changed from the base of the PR and between 7b9997baea and 9e925ee152.

📒 Files selected for processing (3)
  • src/kernel/memory.cpp
  • src/kernel/memoryAddressSpace.inc
  • tests/VirtualMemoryAllocationTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-09 02:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1269#pullrequestreview-5465057716) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `ea210989-1f0f-41ed-a51f-aa4a525eb994` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 7b9997baea0d385e9e8fc2bd3abeb02110bc4148 and 9e925ee152cf5fe79acf583af0c9ba235910f66a. </details> <details> <summary>📒 Files selected for processing (3)</summary> * `src/kernel/memory.cpp` * `src/kernel/memoryAddressSpace.inc` * `tests/VirtualMemoryAllocationTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=1f6e51eb-0022-4b82-82a1-510d7701b465 attempt=f445e953-6a68-4fef-9715-e6dc1f84ef80 batch=1/1 -->
@ -1493,0 +1509,4 @@
}
}
return 0;
}
Author
Owner

@⁠msfvn on GitHub · 2026-10-11 02:00 UTC · original

Fixed in 266eba72: the helper uses the error_code overload of directory_iterator, so a missing /proc/self/fd returns 0 and the checks report a normal failure.

**@⁠msfvn** on GitHub · 2026-10-11 02:00 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1269#discussion_r4239926955) Fixed in 266eba72: the helper uses the `error_code` overload of `directory_iterator`, so a missing `/proc/self/fd` returns 0 and the checks report a normal failure.
@ -1493,0 +1509,4 @@
}
}
return 0;
}
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-11 02:01 UTC · original

@msfvn Thanks for the fix. The error_code overload handles a missing /proc/self/fd without a filesystem_error, so GuestBackingKiB() returns 0.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

✅ Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-11 02:01 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1269#discussion_r4239927641) `@msfvn` Thanks for the fix. The `error_code` overload handles a missing `/proc/self/fd` without a `filesystem_error`, so `GuestBackingKiB()` returns 0. --- <!-- This is an auto-generated comment: tweet message by coderabbit.ai --> If you found this review helpful, would you consider giving us a shout-out on [X](https://twitter.com/intent/tweet?related=CodeRabbitAI&text=Use%20%60std%3A%3Aerror_code%60%20when%20iterating%20%60%2Fproc%2Fself%2Ffd%60%3A%20missing%20procfs%20now%20yields%20a%20handled%20test%20failure%20instead%20of%20crashing%20the%20test%20process.%20Try%20%40coderabbitai%20for%20practical%20code%20reviews!%20%F0%9F%9B%A0%EF%B8%8F&url=https%3A%2F%2Fgithub.com%2FKytyPS5%2FKytyPS5%2Fpull%2F1269%23discussion_r4226062554)? Thank you for using CodeRabbit! <!-- end of auto-generated comment: tweet message by coderabbit.ai --> ✅ Review thread resolved. _You are interacting with an AI system._ <!-- This is an auto-generated reply by CodeRabbit -->
This pull request has changes conflicting with the target branch.
  • src/kernel/memory.cpp
  • src/kernel/memoryAddressSpace.inc
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1269/head:github-bot/gh-1269'
git switch 'github-bot/gh-1269'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1269'
git switch 'github-bot/gh-1269'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1269'
git switch 'github-bot/gh-1269'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1269'
git switch 'main'
git merge --squash 'github-bot/gh-1269'
git switch 'main'
git merge --ff-only 'github-bot/gh-1269'
git switch 'main'
git merge 'github-bot/gh-1269'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.