feat(libkernel): accept MADV_PROTECT advisory hint in madvise #4582

Merged
jagrat04 merged 1 commit from upstream-pr/4582 into main 2026-10-13 07:44:19 +00:00
jagrat04 commented 2026-10-12 16:09:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jagrat04 on 2026-10-12T16:09:36Z.


What

Accepts MADV_PROTECT (10) as a valid advisory hint in madvise in libkernel. In FreeBSD, MADV_PROTECT protects the process from OOM termination. Like the other madvise hints (0-9), user-mode emulation accepts it as a no-op hint without terminating the process via NotImplemented_nid_no_patch. Updates unit tests in GuestMemory.cpp and documents Technical Debt.

Tested

ools/check_conventions.py --base origin/main

Guest unit tests in core/libs/tests/GuestMemory.cpp

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832014081 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4582). Originally posted by [jagrat04](https://github.com/jagrat04) on 2026-10-12T16:09:36Z. --- ### What Accepts MADV_PROTECT (10) as a valid advisory hint in madvise in libkernel. In FreeBSD, MADV_PROTECT protects the process from OOM termination. Like the other madvise hints (0-9), user-mode emulation accepts it as a no-op hint without terminating the process via NotImplemented_nid_no_patch. Updates unit tests in GuestMemory.cpp and documents Technical Debt. ### Tested ools/check_conventions.py --base origin/main Guest unit tests in core/libs/tests/GuestMemory.cpp ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current main; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation .md files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-12 16:10:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T16:10:38Z.


Progress report (43e3a70 → 59bd0c1)

📈 System libraries: 87.89% (+0.02%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel madvise_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128049959 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4582#issuecomment-6128049959). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T16:10:38Z. --- <!-- progress-report --> ### Progress report (43e3a70 → 59bd0c1) 📈 **System libraries**: 87.89% (+0.02%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `madvise_nid_postfix` | </details>
adriadam10 commented 2026-10-12 21:02:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-12T21:02:38Z.

Review: CHANGES_REQUESTED


Thanks for the PR. Before this can go in:

  • madvise with MADV_PROTECT is not an address-range hint in FreeBSD: kern_madvise (sys/vm/vm_mmap.c) handles it before the address check, ignores addr/len, and calls kern_procctl(..., PROC_SPROTECT, PPROT_SET) on the calling process. Here PosixMemory.cpp:229 still validates the range for advice 10, so madvise(0, 0xFFFFFFFFFFFFF000, 10) returns EINVAL where FreeBSD does not look at it. Please make advice 10 skip the range check, or keep throwing.
  • PROC_SPROTECT needs privilege (PRIV_VM_MADV_PROTECT) and fails otherwise, so returning 0 unconditionally is a guess, not the exact result. If you keep it as a stub, say which result it follows and why in the TechnicalDebt line (docs/dev/debt/libkernel.md:146), and cite vm_mmap.c.
  • core/libs/tests/GuestMemory.cpp:1969 only adds advice 10 with a valid range. Add a row with an invalid range for advice 10, so the test pins the behaviour chosen above.
  • The description names no title or issue that calls madvise(MADV_PROTECT). Without one, the previous explicit throw is the exact-or-throw choice; please add the title/PPSA (or log) that hits it.

Feel free to push the changes and ping me.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5495914427 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4582#pullrequestreview-5495914427). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-12T21:02:38Z. **Review:** CHANGES_REQUESTED --- Thanks for the PR. Before this can go in: - `madvise` with `MADV_PROTECT` is not an address-range hint in FreeBSD: `kern_madvise` (`sys/vm/vm_mmap.c`) handles it before the address check, ignores `addr`/`len`, and calls `kern_procctl(..., PROC_SPROTECT, PPROT_SET)` on the calling process. Here `PosixMemory.cpp:229` still validates the range for advice 10, so `madvise(0, 0xFFFFFFFFFFFFF000, 10)` returns `EINVAL` where FreeBSD does not look at it. Please make advice 10 skip the range check, or keep throwing. - `PROC_SPROTECT` needs privilege (`PRIV_VM_MADV_PROTECT`) and fails otherwise, so returning 0 unconditionally is a guess, not the exact result. If you keep it as a stub, say which result it follows and why in the TechnicalDebt line (`docs/dev/debt/libkernel.md:146`), and cite `vm_mmap.c`. - `core/libs/tests/GuestMemory.cpp:1969` only adds advice 10 with a valid range. Add a row with an invalid range for advice 10, so the test pins the behaviour chosen above. - The description names no title or issue that calls `madvise(MADV_PROTECT)`. Without one, the previous explicit throw is the exact-or-throw choice; please add the title/PPSA (or log) that hits it. Feel free to push the changes and ping me.
jagrat04 commented 2026-10-12 22:54:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jagrat04 on 2026-10-12T22:54:02Z.


Updated as requested:

  • Rebased cleanly on origin/main.
  • madvise skips the address/length validation for GuestAdviceProtect (10), matching FreeBSD 11 kern_madvise (sys/vm/vm_mmap.c) handling before address checks.
  • Documented in docs/dev/debt/libkernel.md citing sys/vm/vm_mmap.c and noting the assumption of caller privilege (PRIV_VM_MADV_PROTECT).
  • Added tests in GuestMemory.cpp checking invalid ranges with advice 10.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134171984 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4582#issuecomment-6134171984). Originally posted by [jagrat04](https://github.com/jagrat04) on 2026-10-12T22:54:02Z. --- Updated as requested: - Rebased cleanly on origin/main. - madvise skips the address/length validation for GuestAdviceProtect (10), matching FreeBSD 11 kern_madvise (sys/vm/vm_mmap.c) handling before address checks. - Documented in docs/dev/debt/libkernel.md citing sys/vm/vm_mmap.c and noting the assumption of caller privilege (PRIV_VM_MADV_PROTECT). - Added tests in GuestMemory.cpp checking invalid ranges with advice 10.
nicolasfalesy commented 2026-10-13 07:36:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T07:36:14Z.


Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto main 7e181fcc3 together with #4725, #4719, #4585 and #4582 (no conflicts): guest_memory passes. With this PR's source change reverted (test kept), it fails on Linux, so the check guards the change on Linux too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140682950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4582#issuecomment-6140682950). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T07:36:14Z. --- Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto `main` 7e181fcc3 together with #4725, #4719, #4585 and #4582 (no conflicts): `guest_memory` passes. With this PR's source change reverted (test kept), it fails on Linux, so the check guards the change on Linux too.
adriadam10 commented 2026-10-13 07:44:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T07:44:15Z.

Review: APPROVED


Checked against FreeBSD kern_madvise: advice 10 now returns before the range validation, the debt entry documents the privilege assumption, and the new guest_memory rows fail with the source change reverted. Build and ctest green locally (also confirmed on Linux by @nicolasfalesy); merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5500310799 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4582#pullrequestreview-5500310799). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T07:44:15Z. **Review:** APPROVED --- Checked against FreeBSD `kern_madvise`: advice 10 now returns before the range validation, the debt entry documents the privilege assumption, and the new `guest_memory` rows fail with the source change reverted. Build and ctest green locally (also confirmed on Linux by @nicolasfalesy); merging.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4582
No description provided.