memory: refactor #1941

Manually merged
github merged 3 commits from gh-pr/1941/head into gh-pr/1941/base 2026-11-26 10:09:49 +00:00
Owner

@⁠Nmzik on GitHub · 2026-10-27 19:25 UTC · original
KytyPS5:memory-refactor → main · +1442 −1247 · 6 files · 3 commits · merged 2026-10-27 20:14 UTC

No description provided.

**@⁠Nmzik** on GitHub · 2026-10-27 19:25 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1941) `KytyPS5:memory-refactor` → `main` · +1442 −1247 · 6 files · 3 commits · merged 2026-10-27 20:14 UTC *No description provided.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:46 +00:00
github manually merged commit d0027b4b31 into gh-pr/1941/base 2026-11-26 10:09:49 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-27 19:33 UTC · original · edited 2026-10-27 19:50 UTC

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 57c4b93ca8 and d0027b4b31.

Files selected for processing: 1
  • src/kernel/memory.cpp

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

Walkthrough

Physical, flexible, and pooled memory mappings now use ordered interval maps. Unmap and release operations support partial ranges and restore mappings and metadata when operations fail. Flexible backing tracks its high-water mark and clears previously exposed backing when reused. Pool decommit validates the full span and retains reservations. ASan shadow tracking records covered guest bytes. Tests add coverage for fragmented ranges, rollback, pool operations, backing zeroing, and ASan shadow lifetime.

Suggested reviewers: brandostrong, claxten10

Priority: Normal

Change: Refactor

Merge Risk: Low · up to d0027

A failed direct-memory release may leave a read-only mapping writable. This is a bounded risk worth fixing or explicitly accepting before merge.

Pre-merge checks: Passed 2 · Failed 1 · Inconclusive 2

Failed Failed checks: 1 warning · 2 inconclusive

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within survi… Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check Inconclusive The title identifies the memory area and indicates a refactor, but it does not state the primary change, such as interval-map tracking or mapping ownership changes. Replace the title with a specific summary, such as "memory: refactor mapping and backing ownership".
Description check Inconclusive No pull request description was provided, so the intent, scope, and validation of the memory refactor are not documented. Add a description that summarizes the mapping and backing changes and reports the relevant test results.
Passed Passed checks: 2 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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 72 functions across 4 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · 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-27 19:33 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1941#issuecomment-6451651323) · edited 2026-10-27 19:50 UTC <!-- coderabbit-ai-generated:v1 --> <!-- 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/1941?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><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="16" height="16" align="absmiddle" alt=""></picture> Recent review info</strong></summary> <dl> <dd> <!-- review_info_content --> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</strong></summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [memory-refactor](<https://github.com/KytyPS5/KytyPS5/tree/memory-refactor>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/d0027b4b31f008465a425f4673577828d73e2a7b/.coderabbit.yaml>) - **Commit**: [d0027b4](<https://github.com/KytyPS5/KytyPS5/commit/d0027b4b31f008465a425f4673577828d73e2a7b>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `5347c7cd-01b8-4fac-9bb8-802ed58c54d7` </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</strong></summary> <dl> <dd> Reviewing files that changed from the base of the PR and between 57c4b93ca8932447383e1fe787f36051bf9729c6 and d0027b4b31f008465a425f4673577828d73e2a7b. </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 1</strong></summary> <dl> <dd> * `src/kernel/memory.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review. </dd> </dl> </details> <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> Physical, flexible, and pooled memory mappings now use ordered interval maps. Unmap and release operations support partial ranges and restore mappings and metadata when operations fail. Flexible backing tracks its high-water mark and clears previously exposed backing when reused. Pool decommit validates the full span and retains reservations. ASan shadow tracking records covered guest bytes. Tests add coverage for fragmented ranges, rollback, pool operations, backing zeroing, and ASan shadow lifetime. **Suggested reviewers:** `brandostrong`, `claxten10` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <strong>Priority: <picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/priority-normal-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Normal</strong> <!-- change_assessment_commit:"d0027b4b31f008465a425f4673577828d73e2a7b" --> **Change:** Refactor <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-low-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Low</strong> · up to <code>d0027</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🔵 Low** · up to `d0027` --> <!-- final_review_risk_coverage:{"sourceCommitId":"d0027b4b31f008465a425f4673577828d73e2a7b","coveredCommitId":"d0027b4b31f008465a425f4673577828d73e2a7b","kind":"reviewed"} --> A failed direct-memory release may leave a read-only mapping writable. This is a bounded risk worth fixing or explicitly accepting before merge. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- 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-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 2 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-inconclusive-14-v2.svg" width="14" height="14" align="middle" alt="Inconclusive" title="Inconclusive"></picture></sup> 2</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning · 2 inconclusive</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :--------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within survi… | Write docstrings for the functions missing them to satisfy the coverage threshold. | | Title check | ![Inconclusive](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-inconclusive.svg) | The title identifies the memory area and indicates a refactor, but it does not state the primary change, such as interval-map tracking or mapping ownership changes. | Replace the title with a specific summary, such as "memory: refactor mapping and backing ownership". | | Description check | ![Inconclusive](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-inconclusive.svg) | No pull request description was provided, so the intent, scope, and validation of the memory refactor are not documented. | Add a description that summarizes the mapping and backing changes and reports the relevant test results. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 2 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. | </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **Explanation** Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 72 functions across 4 files. **Resolution** Write docstrings for the functions missing them to satisfy the coverage threshold. </dd> </dl> </details> </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">Autofix</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=1941)! 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) </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
@ -2860,0 +2305,4 @@
VirtualMemory::Mode mode = VirtualMemory::Mode::NoAccess;
EXIT_IF(!DecodeMemoryProtection(it->protection, &mode));
EXIT_IF(!g_guest_address_space->MapBacking(it->start, it->size, it->offset, mode));
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-27 19:33 UTC · original

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

rg -n 'MapBacking|ProtectViews|MapFixed|restore_owner_aliases' src/kernel/memory.cpp src/kernel/memoryAddressSpace.inc

Repository: KytyPS5/KytyPS5

Length of output: 2540


Script executed:

set -eu
printf '%s\n' '--- src/kernel/memoryAddressSpace.inc: mapping and rollback ---'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '240,390p'
printf '%s\n' '--- src/kernel/memoryAddressSpace.inc: unmap rollback ---'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '420,485p'
printf '%s\n' '--- src/kernel/memoryAddressSpace.inc: MapBacking and Protect ---'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '790,875p'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1015,1070p'
printf '%s\n' '--- src/kernel/memory.cpp: direct release rollback ---'
nl -ba src/kernel/memory.cpp | sed -n '2265,2345p'
printf '%s\n' '--- src/kernel/memory.cpp: direct mapping rollback ---'
nl -ba src/kernel/memory.cpp | sed -n '2405,2465p'
printf '%s\n' '--- src/kernel/memory.cpp: existing failed-unmap protection path ---'
nl -ba src/kernel/memory.cpp | sed -n '2725,2785p'

Repository: KytyPS5/KytyPS5

Length of output: 23936


Script executed:

set -eu
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '800,865p'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '340,380p'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1035,1065p'
nl -ba src/kernel/memory.cpp | sed -n '2290,2340p'
nl -ba src/kernel/memory.cpp | sed -n '2425,2460p'
nl -ba src/kernel/memory.cpp | sed -n '2740,2775p'

Repository: KytyPS5/KytyPS5

Length of output: 10796


Script executed:

set -eu
printf '%s\n' '--- KernelMprotect and protection metadata ---'
rg -n -F --glob 'src/kernel/memory.cpp' -e 'KernelMprotect' -e 'Mprotect' -e 'g_virtual_ranges->Protect' -e 'g_guest_address_space->Protect' src/kernel/memory.cpp
printf '%s\n' '--- protection call sites and failed-unmap recovery ---'
nl -ba src/kernel/memory.cpp | sed -n '2520,2615p'
nl -ba src/kernel/memory.cpp | sed -n '2820,2915p'
printf '%s\n' '--- VirtualRanges protection declaration/implementation ---'
rg -n -F --glob 'src/kernel/*' -e 'bool Protect' -e 'Protect(' src/kernel/memory.cpp src/kernel/memoryAddressSpace.inc

Repository: KytyPS5/KytyPS5

Length of output: 11496


Script executed:

set -eu
printf '%s\n' '--- mprotect and protection helpers ---'
nl -ba src/kernel/memory.cpp | sed -n '3235,3370p'
printf '%s\n' '--- backing map entry declaration and protection implementation ---'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1,90p'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '305,340p'
nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1036,1063p'
printf '%s\n' '--- all direct MapBacking rollback contexts ---'
nl -ba src/kernel/memory.cpp | sed -n '1935,1995p'
nl -ba src/kernel/memory.cpp | sed -n '2428,2455p'
nl -ba src/kernel/memory.cpp | sed -n '2750,2778p'

Repository: KytyPS5/KytyPS5

Length of output: 19268


Restore and retain read-only protection on every direct-view rollback.

KernelMprotect applies host protection and then updates VirtualRanges. A failed release can rebuild a read-only alias through MapBacking, which converts it to ReadWrite or ExecuteReadWrite. GuestBackingStore::Unmap can also rebuild a partially unmapped alias with that writable stored mode.

The all-alias Protect pass is correct for the immediate rollback. Apply it to restore_mappings and restore_chunks as well. Also update the stored backing-view mode when Protect changes a mapping. Otherwise, a later failed Unmap can widen the protection again.

This is a functional-correctness issue because host protection can differ from the recorded guest protection.

🛡️ Immediate rollback fix
--- "a/src/kernel/memory.cpp"
+++ "b/src/kernel/memory.cpp"
@@ -2301,14 +2301,19 @@
 			MapGpuRange(alias.start, alias.size);
 		}
 	};
-	auto restore_owner_aliases = [](std::span<const VirtualRanges::Range> aliases) {
+	auto restore_owner_aliases = [&mapped_aliases](std::span<const VirtualRanges::Range> aliases) {
 		for (auto it = aliases.rbegin(); it != aliases.rend(); ++it) {
 			VirtualMemory::Mode mode = VirtualMemory::Mode::NoAccess;
 
 			EXIT_IF(!DecodeMemoryProtection(it->protection, &mode));
 			EXIT_IF(!g_guest_address_space->MapBacking(it->start, it->size, it->offset, mode));
 		}
+		for (const auto& alias: mapped_aliases) {
+			VirtualMemory::Mode mode = VirtualMemory::Mode::NoAccess;
+			EXIT_IF(!DecodeMemoryProtection(alias.protection, &mode));
+			EXIT_IF(!g_guest_address_space->Protect(alias.start, alias.size, mode));
+		}
 	};
 
 	size_t owner_unmapped = 0;
 	for (const auto& alias: mapped_aliases) {
**@⁠coderabbitai[bot]** on GitHub · 2026-10-27 19:33 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1941#discussion_r4393235099) <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor** · **Functional Correctness** · **Quick win** <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/review-details-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/review-details.svg" width="16" height="16" align="absmiddle" alt=""></picture> Supported by static analysis</summary> <dl> <dd> Script executed: ```bash rg -n 'MapBacking|ProtectViews|MapFixed|restore_owner_aliases' src/kernel/memory.cpp src/kernel/memoryAddressSpace.inc ``` Repository: KytyPS5/KytyPS5 Length of output: 2540 --- Script executed: ```bash set -eu printf '%s\n' '--- src/kernel/memoryAddressSpace.inc: mapping and rollback ---' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '240,390p' printf '%s\n' '--- src/kernel/memoryAddressSpace.inc: unmap rollback ---' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '420,485p' printf '%s\n' '--- src/kernel/memoryAddressSpace.inc: MapBacking and Protect ---' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '790,875p' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1015,1070p' printf '%s\n' '--- src/kernel/memory.cpp: direct release rollback ---' nl -ba src/kernel/memory.cpp | sed -n '2265,2345p' printf '%s\n' '--- src/kernel/memory.cpp: direct mapping rollback ---' nl -ba src/kernel/memory.cpp | sed -n '2405,2465p' printf '%s\n' '--- src/kernel/memory.cpp: existing failed-unmap protection path ---' nl -ba src/kernel/memory.cpp | sed -n '2725,2785p' ``` Repository: KytyPS5/KytyPS5 Length of output: 23936 --- Script executed: ```bash set -eu nl -ba src/kernel/memoryAddressSpace.inc | sed -n '800,865p' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '340,380p' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1035,1065p' nl -ba src/kernel/memory.cpp | sed -n '2290,2340p' nl -ba src/kernel/memory.cpp | sed -n '2425,2460p' nl -ba src/kernel/memory.cpp | sed -n '2740,2775p' ``` Repository: KytyPS5/KytyPS5 Length of output: 10796 --- Script executed: ```bash set -eu printf '%s\n' '--- KernelMprotect and protection metadata ---' rg -n -F --glob 'src/kernel/memory.cpp' -e 'KernelMprotect' -e 'Mprotect' -e 'g_virtual_ranges->Protect' -e 'g_guest_address_space->Protect' src/kernel/memory.cpp printf '%s\n' '--- protection call sites and failed-unmap recovery ---' nl -ba src/kernel/memory.cpp | sed -n '2520,2615p' nl -ba src/kernel/memory.cpp | sed -n '2820,2915p' printf '%s\n' '--- VirtualRanges protection declaration/implementation ---' rg -n -F --glob 'src/kernel/*' -e 'bool Protect' -e 'Protect(' src/kernel/memory.cpp src/kernel/memoryAddressSpace.inc ``` Repository: KytyPS5/KytyPS5 Length of output: 11496 --- Script executed: ```bash set -eu printf '%s\n' '--- mprotect and protection helpers ---' nl -ba src/kernel/memory.cpp | sed -n '3235,3370p' printf '%s\n' '--- backing map entry declaration and protection implementation ---' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1,90p' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '305,340p' nl -ba src/kernel/memoryAddressSpace.inc | sed -n '1036,1063p' printf '%s\n' '--- all direct MapBacking rollback contexts ---' nl -ba src/kernel/memory.cpp | sed -n '1935,1995p' nl -ba src/kernel/memory.cpp | sed -n '2428,2455p' nl -ba src/kernel/memory.cpp | sed -n '2750,2778p' ``` Repository: KytyPS5/KytyPS5 Length of output: 19268 --- </dd> </dl> </details> **Restore and retain read-only protection on every direct-view rollback.** `KernelMprotect` applies host protection and then updates `VirtualRanges`. A failed release can rebuild a read-only alias through `MapBacking`, which converts it to `ReadWrite` or `ExecuteReadWrite`. `GuestBackingStore::Unmap` can also rebuild a partially unmapped alias with that writable stored mode. The all-alias `Protect` pass is correct for the immediate rollback. Apply it to `restore_mappings` and `restore_chunks` as well. Also update the stored backing-view mode when `Protect` changes a mapping. Otherwise, a later failed `Unmap` can widen the protection again. This is a functional-correctness issue because host protection can differ from the recorded guest protection. <details> <summary>🛡️ Immediate rollback fix</summary> <dl> <dd> ```diff --- "a/src/kernel/memory.cpp" +++ "b/src/kernel/memory.cpp" @@ -2301,14 +2301,19 @@ MapGpuRange(alias.start, alias.size); } }; - auto restore_owner_aliases = [](std::span<const VirtualRanges::Range> aliases) { + auto restore_owner_aliases = [&mapped_aliases](std::span<const VirtualRanges::Range> aliases) { for (auto it = aliases.rbegin(); it != aliases.rend(); ++it) { VirtualMemory::Mode mode = VirtualMemory::Mode::NoAccess; EXIT_IF(!DecodeMemoryProtection(it->protection, &mode)); EXIT_IF(!g_guest_address_space->MapBacking(it->start, it->size, it->offset, mode)); } + for (const auto& alias: mapped_aliases) { + VirtualMemory::Mode mode = VirtualMemory::Mode::NoAccess; + EXIT_IF(!DecodeMemoryProtection(alias.protection, &mode)); + EXIT_IF(!g_guest_address_space->Protect(alias.start, alias.size, mode)); + } }; size_t owner_unmapped = 0; for (const auto& alias: mapped_aliases) { ``` </dd> </dl> </details> <!-- fingerprinting:phantom:medusa:beignet55 --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:6a774e17ef9b0cc28549b02a --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-27 19:33 UTC · original · reviewed

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 0685bffb2f and fc918fcde3.

Files selected for processing: 5
  • src/kernel/memory.cpp
  • src/kernel/memory.h
  • src/kernel/memoryAddressSpace.inc
  • src/libs/libDbgAsan.cpp
  • 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-27 19:33 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1941#pullrequestreview-5673124357) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** --- <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="16" height="16" align="absmiddle" alt=""></picture> Review info</summary> <dl> <dd> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [memory-refactor](<https://github.com/KytyPS5/KytyPS5/tree/memory-refactor>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/fc918fcde303828b879e6b3da4eb0a9b8089997b/.coderabbit.yaml>) - **Commit**: [fc918fc](<https://github.com/KytyPS5/KytyPS5/commit/fc918fcde303828b879e6b3da4eb0a9b8089997b>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `25ea51c8-a5a9-4b9d-93e6-18bb3ecb5389` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary> <dl> <dd> Reviewing files that changed from the base of the PR and between 0685bffb2f060bf470aa8528a405ebbe17130076 and fc918fcde303828b879e6b3da4eb0a9b8089997b. </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 5</summary> <dl> <dd> * `src/kernel/memory.cpp` * `src/kernel/memory.h` * `src/kernel/memoryAddressSpace.inc` * `src/libs/libDbgAsan.cpp` * `tests/VirtualMemoryAllocationTests.cpp` </dd> </dl> </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. </dd> </dl> </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=d9b214da-0fe8-484a-8d1b-4fac88a47dfc attempt=d1cc43ff-2ab1-49a5-9af5-e3e27fd26085 batch=1/1 -->
This discussion has been locked. Commenting is limited to contributors.
No description provided.