fix(libSceNKWebKit): take WTF::ramSize from the guest sysconf #20100

Merged
MatteoSid merged 1 commit from upstream-pr/20100 into main 2026-11-14 16:45:03 +00:00
MatteoSid commented 2026-11-14 16:10:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MatteoSid on 2026-11-14T16:10:00Z.


What

WTF::ramSize() in libSceNKWebKit (_ZN3WTF7ramSizeEv, NID L+f444MZShU) returned the host's physical memory on every call. It used GlobalMemoryStatusEx on Windows and the host sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE) elsewhere, and threw when the host could not answer. The PS5 11.x WebKit tree does something else:

  • The x86_64 default turns USE_SYSTEM_MALLOC off (WebKitFeatures.cmake), and the PlayStation port links bmalloc statically (PlatformPlayStation.cmake).
  • So RAMSize.cpp returns bmalloc::api::availableMemory() and keeps it in a call_once static.
  • bmalloc's computeAvailableMemory takes the BOS(FREEBSD) branch, which __FreeBSD__ selects (BPlatform.h). It returns sysinfo's totalram * mem_unit, or availableMemoryGuess (512 MB) when sysinfo fails. The BOS(UNIX) branch below it multiplies sysconf(_SC_PHYS_PAGES) by sysconf(_SC_PAGE_SIZE) and falls back to the same guess when either returns -1. It never throws.

WebKit c55364d53f97 has the same BOS(FREEBSD) and BOS(UNIX) branches.

Changes:

  • ramSize() now asks guest libkernel sysconf(121) (_SC_PHYS_PAGES) and sysconf(47) (_SC_PAGESIZE), the queries #19993 and #20053 use for pageSize() and numberOfProcessorCores(). It multiplies them once, caches the result, and returns 512 MB when either query is -1, where it used to throw. libSceNKWebKit already links libkernel.
  • The code is in the new src/WtfRamSize.cpp. It starts with a Derived from line and the RAMSize.cpp and AvailableMemory.cpp notices copied verbatim, like WtfNumberOfCores.cpp.
  • WtfSystem.cpp only changes the export's body and declares the helper. Its includes stay as they are, because #19380 and #19823 edit the lines around them.
  • Debt: ramSize comes out of the "WTF host services" entry and gets its own entry next to WTF::numberOfProcessorCores:
    • sysinfo is not called. On the console, sysinfo comes from libScePosixForWebKit (9Vqxt-qBPB4), and its prototype is unknown (libScePosixForWebKit.md). So this PR follows the BOS(UNIX) branch of the same function, which gives the same value whenever sysinfo reports _SC_PHYS_PAGES pages of _SC_PAGESIZE bytes.
    • Host-reported memory. Guest sysconf(121) still reports the host's memory in 16 KiB pages, not the 139888 pages a PS5 payload read. The libkernel sysconf entry already records that gap.

Reachability: ramSize() takes no arguments and needs no object, so any module that imports it from libSceNKWebKit reaches it directly. In the pinned PS5 tree, a web view calls it on these paths:

Which console modules import this NID has not been checked against a dump.

Tested

Linux (Ubuntu 24.04 container, GCC 13.3), Release, -DBUILD_TESTING=ON, on head 498463a545 based on main 3e63c9f68f. Built libSceNKWebKit, the patched core/libs/libs/libSceNKWebKit.prx and every libSceNKWebKit test target. ctest -R 'nkwebkit|^public_headers$' passes 81 of 81.

nkwebkit_wtf drives the export through RamSizeSysconfForTest_nid_no_patch, which follows NumberOfProcessorCoresSysconfForTest_nid_no_patch from #20053. It replaces the sysconf query and drops the cached size, and nullptr restores guest sysconf_nid_postfix. The test checks:

  • a query that returns 139888 for 121 and 16384 for 47 gives 139888 * 16384 twice, with two queries;
  • a query that fails only for 121, only for 47, or for both gives 512 MiB, cached after the first call;
  • with the guest query restored, the size equals sysconf_nid_postfix(121) * sysconf_nid_postfix(47), is at least 1 GiB and is stable.

No test needs timing.

Negative controls on 498463a545, one at a time, in the Docker build. Each makes nkwebkit_wtf fail (no build failures), and the restored tree passes:

  • main's WtfSystem.cpp export body (host sysconf, throws on failure);
  • sysconf(58) instead of 121 for the pages;
  • sysconf(46) instead of 47 for the page size;
  • a 1024 MB guess instead of 512 MB;
  • only the pages checked for -1;
  • only the page size checked for -1;
  • no cache (g_ramSizeComputed never set);
  • a hook that keeps the cached size.

nm -D on the patched libSceNKWebKit.prx lists L+f444MZShU at the address of _ZN3WTF7ramSizeEv (0x26d0c0), and the hook keeps its _nid_no_patch name. tools/check_conventions.py --base origin/main --head HEAD passes, and git diff --check origin/main is clean.

Overlap: no open PR mentions ramSize, RAMSize or the NID. tools/pr_overlap.py --branch found no overlap; it could not compare 19 PRs (debt file, and tests/Wtf.cpp for #19103 and #19106), which I checked by hand:

  • none of them touches the "WTF host services", WTF::pageSize or WTF::numberOfProcessorCores entries, or ramSize;
  • #19103 and #19106 edit tests/Wtf.cpp at lines 6, 562-573, 1132 and 1424, far from TestSystem (line 308).

No title tested.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5313411545 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20100). Originally posted by [MatteoSid](https://github.com/MatteoSid) on 2026-11-14T16:10:00Z. --- ### What `WTF::ramSize()` in libSceNKWebKit (`_ZN3WTF7ramSizeEv`, NID `L+f444MZShU`) returned the host's physical memory on every call. It used `GlobalMemoryStatusEx` on Windows and the host `sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE)` elsewhere, and threw when the host could not answer. The PS5 11.x WebKit tree does something else: - The x86_64 default turns `USE_SYSTEM_MALLOC` off ([WebKitFeatures.cmake](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/cmake/WebKitFeatures.cmake#L91-L96)), and the PlayStation port links bmalloc statically ([PlatformPlayStation.cmake](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/PlatformPlayStation.cmake#L46-L47)). - So [RAMSize.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/RAMSize.cpp#L49-L83) returns `bmalloc::api::availableMemory()` and keeps it in a `call_once` static. - bmalloc's [`computeAvailableMemory`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/AvailableMemory.cpp#L152-L188) takes the `BOS(FREEBSD)` branch, which `__FreeBSD__` selects ([BPlatform.h](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/BPlatform.h#L51-L53)). It returns `sysinfo`'s `totalram * mem_unit`, or `availableMemoryGuess` (512 MB) when `sysinfo` fails. The `BOS(UNIX)` branch below it multiplies `sysconf(_SC_PHYS_PAGES)` by `sysconf(_SC_PAGE_SIZE)` and falls back to the same guess when either returns -1. It never throws. [WebKit c55364d53f97](https://github.com/WebKit/WebKit/blob/c55364d53f971f63a709976f168d932d558b8909/Source/bmalloc/bmalloc/AvailableMemory.cpp#L158-L196) has the same `BOS(FREEBSD)` and `BOS(UNIX)` branches. Changes: - `ramSize()` now asks guest libkernel `sysconf(121)` (`_SC_PHYS_PAGES`) and `sysconf(47)` (`_SC_PAGESIZE`), the queries #19993 and #20053 use for `pageSize()` and `numberOfProcessorCores()`. It multiplies them once, caches the result, and returns 512 MB when either query is -1, where it used to throw. libSceNKWebKit already links libkernel. - The code is in the new `src/WtfRamSize.cpp`. It starts with a `Derived from` line and the RAMSize.cpp and AvailableMemory.cpp notices copied verbatim, like `WtfNumberOfCores.cpp`. - `WtfSystem.cpp` only changes the export's body and declares the helper. Its includes stay as they are, because #19380 and #19823 edit the lines around them. - Debt: `ramSize` comes out of the "WTF host services" entry and gets its own entry next to `WTF::numberOfProcessorCores`: - **`sysinfo` is not called.** On the console, `sysinfo` comes from libScePosixForWebKit (`9Vqxt-qBPB4`), and its prototype is unknown ([libScePosixForWebKit.md](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/debt/libScePosixForWebKit.md)). So this PR follows the `BOS(UNIX)` branch of the same function, which gives the same value whenever `sysinfo` reports `_SC_PHYS_PAGES` pages of `_SC_PAGESIZE` bytes. - **Host-reported memory.** Guest `sysconf(121)` still reports the host's memory in 16 KiB pages, not the 139888 pages a PS5 payload read. The libkernel `sysconf` entry already records that gap. Reachability: `ramSize()` takes no arguments and needs no object, so any module that imports it from libSceNKWebKit reaches it directly. In the pinned PS5 tree, a web view calls it on these paths: - [`JSC::Heap::Heap`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/JavaScriptCore/heap/Heap.cpp#L289-L292) stores it in `m_ramSize` for every VM, including the one a web page's scripts run in. [`minHeapSize`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/JavaScriptCore/heap/Heap.cpp#L131-L160) and `proportionalHeapSize` scale the GC heap by it. Those functions, and the critical-memory threshold at [L423](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/JavaScriptCore/heap/Heap.cpp#L423), decide when the collector runs. - [`WebKit::calculateMemoryCacheSizes`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WebKit/Shared/CacheModel.cpp#L36-L38) sizes the memory cache and the back/forward cache from it. - [`MemoryPressureHandler::Configuration`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/MemoryPressureHandler.cpp#L322-L324) uses it for its base threshold, and the kill thresholds at [L107-L126](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/MemoryPressureHandler.cpp#L107-L126) read it too. The PlayStation port builds it with [`unix/MemoryPressureHandlerUnix.cpp`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/PlatformPlayStation.cmake#L23). - The WebAssembly memory limit, [`BufferMemoryManager::memoryLimit`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/JavaScriptCore/runtime/BufferMemoryHandle.h#L99-L104), is three times this value. Which console modules import this NID has not been checked against a dump. ### Tested Linux (Ubuntu 24.04 container, GCC 13.3), Release, `-DBUILD_TESTING=ON`, on head `498463a545` based on `main` `3e63c9f68f`. Built `libSceNKWebKit`, the patched `core/libs/libs/libSceNKWebKit.prx` and every libSceNKWebKit test target. `ctest -R 'nkwebkit|^public_headers$'` passes 81 of 81. `nkwebkit_wtf` drives the export through `RamSizeSysconfForTest_nid_no_patch`, which follows `NumberOfProcessorCoresSysconfForTest_nid_no_patch` from #20053. It replaces the sysconf query and drops the cached size, and `nullptr` restores guest `sysconf_nid_postfix`. The test checks: - a query that returns 139888 for 121 and 16384 for 47 gives 139888 * 16384 twice, with two queries; - a query that fails only for 121, only for 47, or for both gives 512 MiB, cached after the first call; - with the guest query restored, the size equals `sysconf_nid_postfix(121) * sysconf_nid_postfix(47)`, is at least 1 GiB and is stable. No test needs timing. Negative controls on `498463a545`, one at a time, in the Docker build. Each makes `nkwebkit_wtf` fail (no build failures), and the restored tree passes: - main's `WtfSystem.cpp` export body (host `sysconf`, throws on failure); - `sysconf(58)` instead of 121 for the pages; - `sysconf(46)` instead of 47 for the page size; - a 1024 MB guess instead of 512 MB; - only the pages checked for -1; - only the page size checked for -1; - no cache (`g_ramSizeComputed` never set); - a hook that keeps the cached size. `nm -D` on the patched `libSceNKWebKit.prx` lists `L+f444MZShU` at the address of `_ZN3WTF7ramSizeEv` (`0x26d0c0`), and the hook keeps its `_nid_no_patch` name. `tools/check_conventions.py --base origin/main --head HEAD` passes, and `git diff --check origin/main` is clean. Overlap: no open PR mentions `ramSize`, `RAMSize` or the NID. `tools/pr_overlap.py --branch` found no overlap; it could not compare 19 PRs (debt file, and `tests/Wtf.cpp` for #19103 and #19106), which I checked by hand: - none of them touches the "WTF host services", `WTF::pageSize` or `WTF::numberOfProcessorCores` entries, or `ramSize`; - #19103 and #19106 edit `tests/Wtf.cpp` at lines 6, 562-573, 1132 and 1424, far from `TestSystem` (line 308). No title tested. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-14 16:34:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-14T16:34:37Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 498463a545:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: nkwebkit_wtf_tests built cleanly.
  • CTest (nkwebkit_wtf):
    1/1 Test #4053: nkwebkit_wtf .....................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceNKWebKit/src/WtfSystem.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4053: nkwebkit_wtf .....................Subprocess aborted***Exception:   0.21 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libSceNKWebKit/src/WtfRamSize.cpp: Verified ComputeAvailableMemory correctly queries guest libkernel sysconf for ScPhysPages (121) and ScPageSize (47), safely falling back to AvailableMemoryGuess (512 MiB) if either returns -1.
    • core/libs/prx/libSceNKWebKit/src/WtfRamSize.cpp: Verified thread safety of NKWebKit::RamSize() and RamSizeSysconfForTest_nid_no_patch, confirming std::lock_guardstd::mutex protects g_ramSizeComputed, g_ramSize, and g_sysconf without race conditions or deadlocks.
    • core/libs/prx/libSceNKWebKit/src/WtfSystem.cpp: Verified export _ZN3WTF7ramSizeEv retains the required APS5_VABI calling convention and uint64_t return type while cleanly delegating to NKWebKit::RamSize().
    • core/libs/prx/libSceNKWebKit/src/WtfRamSize.cpp: Verified LP64 and LLP64 portability, ensuring calculations in size_t and int64_t avoid narrowing, signedness, or overflow issues across Linux and Windows.
    • core/libs/prx/libSceNKWebKit/tests/Wtf.cpp: Verified TestRamSize thoroughly covers successful computation, cached sysconf query count (remaining at 2 on repeated invocation), partial sysconf failures, total failure, and restoration of guest libkernel sysconf via nullptr.
    • docs/dev/debt/libSceNKWebKit.md: Verified technical debt was updated accurately to remove ramSize from the WTF host services entry and add a dedicated WTF::ramSize entry documenting the sysinfo and host-reported memory gaps.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5905851416 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20100#pullrequestreview-5905851416). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-14T16:34:37Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `498463a545`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `nkwebkit_wtf_tests` built cleanly. - **CTest** (`nkwebkit_wtf`): ``` 1/1 Test #4053: nkwebkit_wtf ..................... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceNKWebKit/src/WtfSystem.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4053: nkwebkit_wtf .....................Subprocess aborted***Exception: 0.21 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libSceNKWebKit/src/WtfRamSize.cpp: Verified ComputeAvailableMemory correctly queries guest libkernel sysconf for ScPhysPages (121) and ScPageSize (47), safely falling back to AvailableMemoryGuess (512 MiB) if either returns -1. - core/libs/prx/libSceNKWebKit/src/WtfRamSize.cpp: Verified thread safety of NKWebKit::RamSize() and RamSizeSysconfForTest_nid_no_patch, confirming std::lock_guard<std::mutex> protects g_ramSizeComputed, g_ramSize, and g_sysconf without race conditions or deadlocks. - core/libs/prx/libSceNKWebKit/src/WtfSystem.cpp: Verified export _ZN3WTF7ramSizeEv retains the required APS5_VABI calling convention and uint64_t return type while cleanly delegating to NKWebKit::RamSize(). - core/libs/prx/libSceNKWebKit/src/WtfRamSize.cpp: Verified LP64 and LLP64 portability, ensuring calculations in size_t and int64_t avoid narrowing, signedness, or overflow issues across Linux and Windows. - core/libs/prx/libSceNKWebKit/tests/Wtf.cpp: Verified TestRamSize thoroughly covers successful computation, cached sysconf query count (remaining at 2 on repeated invocation), partial sysconf failures, total failure, and restoration of guest libkernel sysconf via nullptr. - docs/dev/debt/libSceNKWebKit.md: Verified technical debt was updated accurately to remove ramSize from the WTF host services entry and add a dedicated WTF::ramSize entry documenting the sysinfo and host-reported memory gaps. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!20100
No description provided.