feat(libSceNKWebKit): implement bmalloc iso heap and TLS layout bookkeeping #22561

Merged
RiverWhy merged 5 commits from upstream-pr/22561 into main 2026-11-19 01:20:46 +00:00
RiverWhy commented 2026-11-17 20:50:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by RiverWhy on 2026-11-17T20:50:50Z.


What

Implements 13 bmalloc Iso heap bookkeeping exports that all throw on main today:

  • IsoHeapImplBase (7): the Mutex& constructor, D0, didCommit, didDecommit, isNowFreeable, isNoLongerFreeable and addToAllIsoHeaps.
  • AllIsoHeaps (2): add and head.
  • IsoTLSEntry (3): the size_t constructor, D0 and extent.
  • IsoTLSLayout (1): add.

This fills in the construction and destruction that the existing bmalloc class allocation operators debt entry leaves open. It uses the field layout that FieldGetters.cpp already reads: footprint at 0x20, freeableMemory at 0x28, and IsoTLSEntry offset and size at 0x10 and 0x14.

Source: pinned IsoHeapImpl.h/.cpp, IsoHeapImplInlines.h, IsoTLSEntry.h/.cpp, IsoTLSLayout.cpp and AllIsoHeaps.cpp (PS5 11.x). The pinned tree defaults to libpas on PlayStation, but the module exports these non-libpas symbols, so this follows the !BUSE(LIBPAS) code, as the existing bmalloc entries do.

Layouts were measured by compiling those files with libpas off:

  • IsoHeapImplBase, 112 bytes. The constructor writes lock, then zeroes m_next, the slow-path time, both counters and the eight 6-byte packed shared cells. It sets m_availableShared = 0xFF, the allocation mode to Init and the inline-directory flag to true, as the measured constructor does. The vtable is [D1, D0, scavenge].
  • IsoTLSEntry, 24 bytes. The constructor sets m_next = nullptr, m_offset = UINT_MAX and m_size to the low 32 bits of the size. extent adds the two unsigned fields before widening, so it wraps as upstream does. The vtable is [D1, D0, construct, move, destruct, scavenge].
  • Pure virtual slots throw std::logic_error. D0 frees through the classes' MAKE_BMALLOCED operator delete exports already on main.

Other details:

  • IsoTLSLayout::add places each entry at its tail's extent rounded up to 8 bytes. Its RELEASE_BASSERTs (entry already linked, offset reaching UINT_MAX, head without tail or tail without head) throw std::logic_error instead of crashing.
  • addToAllIsoHeaps adds to a host AllIsoHeaps, because the module's per-process storage isn't exported. The add/head locks and the IsoTLSLayout adding lock are host mutexes.
  • A null object or argument throws before anything is written.
  • Not included: scavengeNow and finishScavenging decommit guest pages through madvise and call into IsoDirectory, so they still throw.

Debt: a fragment, docs/dev/debt/changes/feat-nkwebkit-bmalloc-iso-heap-bookkeeping.md, adds the entry and removes the 13 inventory rows.

Overlap: pr_overlap.py finds no open PR with these exports. It couldn't compare 19 NKWebKit PRs that conflict with main (#13515, #14138, #14164, #14259, #14860, #15184, #16510, #17695, #17771, #17793, #17805, #17922, #17927, #17929, #17933, #18395, #20319, #21957, #21958); I checked their diffs by hand and none mention these classes. The only reported conflict is the shared last include line of libSceNKWebKit.hpp (#18797, #18805, #20327, #21422, #21432, #21979, #22449–#22456, #22480–#22482, #22555–#22557).

Tested

Linux Mint 22.3. A shared driver runs random sequences on 4 heaps and up to 96 entries:

  • commit, decommit and freeable counter updates with small, page-sized, 64-bit random and near-wrap amounts;
  • adding heaps to an AllIsoHeaps and walking the chain;
  • creating entries with sizes up to and past 2^32;
  • adding entries to an IsoTLSLayout, and reading offsets and extents.

It hashes the constructed objects' bytes (outside the vtable and lock pointer), every counter, offset, extent and list order. The hash is compared with the one from the real 11.x IsoHeapImpl/IsoTLSEntry/IsoTLSLayout/AllIsoHeaps code, compiled with libpas off and running the same driver.

  • New nkwebkit_bmallocisoheap test: 16 sequences of 1,000 operations match. As a one-off, 300 sequences of 10,000 also matched.
  • Hand checks:
    • the vtable slots and deleting destructors;
    • each IsoTLSLayout::add assertion;
    • addToAllIsoHeaps on the host list;
    • null refusals;
    • the existing footprint, freeableMemory, size and offset getters reading the constructed objects.
  • Negative control: widening before adding in extent fails the transcript check.
  • All 120 NKWebKit tests pass.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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:5364785212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22561). Originally posted by [RiverWhy](https://github.com/RiverWhy) on 2026-11-17T20:50:50Z. --- ### What Implements 13 bmalloc Iso heap bookkeeping exports that all throw on `main` today: - **IsoHeapImplBase (7):** the `Mutex&` constructor, `D0`, `didCommit`, `didDecommit`, `isNowFreeable`, `isNoLongerFreeable` and `addToAllIsoHeaps`. - **AllIsoHeaps (2):** `add` and `head`. - **IsoTLSEntry (3):** the `size_t` constructor, `D0` and `extent`. - **IsoTLSLayout (1):** `add`. This fills in the construction and destruction that the existing [bmalloc class allocation operators](https://github.com/boykopovar/AnyPS5/blob/main/core/libs/prx/libSceNKWebKit/src/Bmalloc.cpp) debt entry leaves open. It uses the field layout that `FieldGetters.cpp` already reads: `footprint` at 0x20, `freeableMemory` at 0x28, and `IsoTLSEntry` offset and size at 0x10 and 0x14. Source: pinned [IsoHeapImpl.h](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/IsoHeapImpl.h)/[.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/IsoHeapImpl.cpp), [IsoHeapImplInlines.h](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/IsoHeapImplInlines.h#L219-L247), [IsoTLSEntry.h](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/IsoTLSEntry.h)/[.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/IsoTLSEntry.cpp), [IsoTLSLayout.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/IsoTLSLayout.cpp) and [AllIsoHeaps.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/bmalloc/bmalloc/AllIsoHeaps.cpp) (PS5 11.x). The pinned tree defaults to libpas on PlayStation, but the module exports these non-libpas symbols, so this follows the `!BUSE(LIBPAS)` code, as the existing bmalloc entries do. Layouts were measured by compiling those files with libpas off: - **IsoHeapImplBase, 112 bytes.** The constructor writes `lock`, then zeroes `m_next`, the slow-path time, both counters and the eight 6-byte packed shared cells. It sets `m_availableShared = 0xFF`, the allocation mode to `Init` and the inline-directory flag to true, as the measured constructor does. The vtable is `[D1, D0, scavenge]`. - **IsoTLSEntry, 24 bytes.** The constructor sets `m_next = nullptr`, `m_offset = UINT_MAX` and `m_size` to the low 32 bits of the size. `extent` adds the two `unsigned` fields before widening, so it wraps as upstream does. The vtable is `[D1, D0, construct, move, destruct, scavenge]`. - Pure virtual slots throw `std::logic_error`. `D0` frees through the classes' `MAKE_BMALLOCED` `operator delete` exports already on `main`. Other details: - `IsoTLSLayout::add` places each entry at its tail's extent rounded up to 8 bytes. Its `RELEASE_BASSERT`s (entry already linked, offset reaching `UINT_MAX`, head without tail or tail without head) throw `std::logic_error` instead of crashing. - `addToAllIsoHeaps` adds to a host `AllIsoHeaps`, because the module's per-process storage isn't exported. The `add`/`head` locks and the `IsoTLSLayout` adding lock are host mutexes. - A null object or argument throws before anything is written. - Not included: `scavengeNow` and `finishScavenging` decommit guest pages through `madvise` and call into `IsoDirectory`, so they still throw. Debt: a fragment, `docs/dev/debt/changes/feat-nkwebkit-bmalloc-iso-heap-bookkeeping.md`, adds the entry and removes the 13 inventory rows. Overlap: `pr_overlap.py` finds no open PR with these exports. It couldn't compare 19 NKWebKit PRs that conflict with `main` (#13515, #14138, #14164, #14259, #14860, #15184, #16510, #17695, #17771, #17793, #17805, #17922, #17927, #17929, #17933, #18395, #20319, #21957, #21958); I checked their diffs by hand and none mention these classes. The only reported conflict is the shared last include line of `libSceNKWebKit.hpp` (#18797, #18805, #20327, #21422, #21432, #21979, #22449–#22456, #22480–#22482, #22555–#22557). ### Tested Linux Mint 22.3. A shared driver runs random sequences on 4 heaps and up to 96 entries: - commit, decommit and freeable counter updates with small, page-sized, 64-bit random and near-wrap amounts; - adding heaps to an `AllIsoHeaps` and walking the chain; - creating entries with sizes up to and past 2^32; - adding entries to an `IsoTLSLayout`, and reading offsets and extents. It hashes the constructed objects' bytes (outside the vtable and lock pointer), every counter, offset, extent and list order. The hash is compared with the one from the real 11.x `IsoHeapImpl`/`IsoTLSEntry`/`IsoTLSLayout`/`AllIsoHeaps` code, compiled with libpas off and running the same driver. - New `nkwebkit_bmallocisoheap` test: 16 sequences of 1,000 operations match. As a one-off, 300 sequences of 10,000 also matched. - Hand checks: - the vtable slots and deleting destructors; - each `IsoTLSLayout::add` assertion; - `addToAllIsoHeaps` on the host list; - null refusals; - the existing `footprint`, `freeableMemory`, `size` and `offset` getters reading the constructed objects. - Negative control: widening before adding in `extent` fails the transcript check. - All 120 NKWebKit tests pass. ### Checklist - [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) and the licence notice of code adapted from upstream (see AGENTS.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-17 20:54:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T20:54:30Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: nkwebkit_bmallocisoheap_tests built cleanly.
  • CTest (nkwebkit_bmallocisoheap):
    1/1 Test #4427: nkwebkit_bmallocisoheap ..........   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceNKWebKit/Export.cpp, core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp, core/libs/prx/libSceNKWebKit/libSceNKWebKit.hpp, core/libs/prx/libSceNKWebKit/src/BmallocIsoHeap.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified struct layouts and static_asserts in BmallocIsoHeapExports.hpp against upstream bmalloc 64-bit definitions (IsoHeapImplBase is 112 bytes, IsoTLSEntry is 24 bytes, IsoTLSLayout is 16 bytes, AllIsoHeaps is 8 bytes)
    • Verified null pointer handling in Checked() helper throws std::invalid_argument for all public entrypoints in BmallocIsoHeap.cpp
    • Verified thread safety in AllIsoHeaps add/head (protected by AllIsoHeapsMutex()) and IsoTLSLayout add (protected by addingMutex)
    • Verified pointer offset alignment calculation (extent + EntryAlignment - 1) / EntryAlignment * EntryAlignment and UINT_MAX bounds checks in IsoTLSLayout::add
    • Verified deleting destructors and vtable stub entries matching upstream WebKit bmalloc virtual calling conventions and freeing via bmalloc nw/dl exports
    • Verified compliance with AGENTS.md rules including technical debt fragment in docs/dev/debt/changes/ and removal of NotImplemented_nid_no_patch stubs

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5946453241 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22561#pullrequestreview-5946453241). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T20:54:30Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `e333f6f4ea`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `nkwebkit_bmallocisoheap_tests` built cleanly. - **CTest** (`nkwebkit_bmallocisoheap`): ``` 1/1 Test #4427: nkwebkit_bmallocisoheap .......... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceNKWebKit/Export.cpp`, `core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp`, `core/libs/prx/libSceNKWebKit/libSceNKWebKit.hpp`, `core/libs/prx/libSceNKWebKit/src/BmallocIsoHeap.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified struct layouts and static_asserts in BmallocIsoHeapExports.hpp against upstream bmalloc 64-bit definitions (IsoHeapImplBase is 112 bytes, IsoTLSEntry is 24 bytes, IsoTLSLayout is 16 bytes, AllIsoHeaps is 8 bytes) - Verified null pointer handling in Checked() helper throws std::invalid_argument for all public entrypoints in BmallocIsoHeap.cpp - Verified thread safety in AllIsoHeaps add/head (protected by AllIsoHeapsMutex()) and IsoTLSLayout add (protected by addingMutex) - Verified pointer offset alignment calculation (extent + EntryAlignment - 1) / EntryAlignment * EntryAlignment and UINT_MAX bounds checks in IsoTLSLayout::add - Verified deleting destructors and vtable stub entries matching upstream WebKit bmalloc virtual calling conventions and freeing via bmalloc nw/dl exports - Verified compliance with AGENTS.md rules including technical debt fragment in docs/dev/debt/changes/ and removal of NotImplemented_nid_no_patch stubs Looks Good To Me
PrivacyOnion commented 2026-11-17 23:55:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T23:55:26Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 4dc35bba30:

  • Conventions: tools/check_conventions.py passed.
  • Build: nkwebkit_bmallocisoheap_tests built cleanly.
  • CTest (nkwebkit_bmallocisoheap):
    1/1 Test #4455: nkwebkit_bmallocisoheap ..........   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceNKWebKit/Export.cpp, core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp, core/libs/prx/libSceNKWebKit/libSceNKWebKit.hpp, core/libs/prx/libSceNKWebKit/src/BmallocIsoHeap.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified IsoHeapImplBase (112 bytes) and IsoTLSEntry (24 bytes) ABI layouts, field offsets, and static_asserts against 64-bit WebKit bmalloc in BmallocIsoHeapExports.hpp.
    • Verified Checked() null pointer validation and error throwing behavior across all exports in BmallocIsoHeap.cpp.
    • Verified thread safety in AllIsoHeaps::add/head using AllIsoHeapsMutex and IsoTLSLayout::add using addingMutex with proper ReleaseAssert bounds and memory fence checks.
    • Verified 64-bit counter operations (didCommit, didDecommit, isNowFreeable, isNoLongerFreeable) and extent/offset calculation logic.
    • Verified compliance with AGENTS.md conventions, removal of NotImplemented_nid_no_patch stubs, and technical debt documentation in docs/dev/debt/changes/.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5948635569 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22561#pullrequestreview-5948635569). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T23:55:26Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `4dc35bba30`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `nkwebkit_bmallocisoheap_tests` built cleanly. - **CTest** (`nkwebkit_bmallocisoheap`): ``` 1/1 Test #4455: nkwebkit_bmallocisoheap .......... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceNKWebKit/Export.cpp`, `core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp`, `core/libs/prx/libSceNKWebKit/libSceNKWebKit.hpp`, `core/libs/prx/libSceNKWebKit/src/BmallocIsoHeap.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified IsoHeapImplBase (112 bytes) and IsoTLSEntry (24 bytes) ABI layouts, field offsets, and static_asserts against 64-bit WebKit bmalloc in BmallocIsoHeapExports.hpp. - Verified Checked() null pointer validation and error throwing behavior across all exports in BmallocIsoHeap.cpp. - Verified thread safety in AllIsoHeaps::add/head using AllIsoHeapsMutex and IsoTLSLayout::add using addingMutex with proper ReleaseAssert bounds and memory fence checks. - Verified 64-bit counter operations (didCommit, didDecommit, isNowFreeable, isNoLongerFreeable) and extent/offset calculation logic. - Verified compliance with AGENTS.md conventions, removal of NotImplemented_nid_no_patch stubs, and technical debt documentation in docs/dev/debt/changes/. 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!22561
No description provided.