feat(libSceJsc): implement WTF dataFile, setDataFile and FilePrintStream #21958

Merged
frankea merged 5 commits from upstream-pr/21958 into main 2026-11-18 10:18:52 +00:00
frankea commented 2026-11-16 23:04:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-11-16T23:04:44Z.


What

Implements the WTF DataLog sink and FilePrintStream, six exports that were NotImplemented_nid_no_patch stubs: WTF::dataFile() and the FilePrintStream D0/D1/D2 destructors in libSceNKWebKit, and WTF::FilePrintStream::open and WTF::setDataFile(std::unique_ptr<PrintStream>&&) in libSceJsc.

The behaviour follows the PlayStation branch of DataLog.cpp L56-L179 at PS5-WebKit-11.x 80f77e94c63 (initializeLogFileOnce, initializeLogFile, both setDataFile overloads and dataFile), with FilePrintStream.cpp, LockedPrintStream.cpp and the PrintStream defaults in PrintStream.cpp. FilePrintStream.h/.cpp are identical to WebKit c55364d53f97.

  • dataFile() runs the one-time initialisation under std::call_once and returns the stream. When nothing was installed, the default sink is a FilePrintStream over the guest libc stdout in Borrow mode, placed in static storage (L151-L152). setvbuf(_IONBF) is skipped because the file is stdout (L158-L161). The stream is wrapped in a LockedPrintStream (L166).
  • setDataFile(unique_ptr&&) takes ownership of the caller's stream and leaves the unique_ptr empty. As upstream's RELEASE_ASSERT (L171) requires, it is accepted only when no stream is installed or the installed one is the static locked stream. A second install throws instead of crashing, and the caller keeps ownership.
  • FilePrintStream::open calls guest fopen and returns the new stream through the hidden unique_ptr result, or an empty unique_ptr when fopen fails.
  • The FilePrintStream destructors (D1, D2) fclose the file unless the mode is Borrow. D0 then frees the object.
  • The guest object layout is vptr, FILE*, then the AdoptionMode enum (24 bytes, pinned with static_assert). The repo builds its own vtables, as libwebrtc does (Vtable + 2): D1, D0, vprintf, flush, begin, end. FilePrintStream::vprintf is guest vfprintf and flush is guest fflush. begin returns the stream and end does nothing. LockedPrintStream holds a recursive lock for vprintf and flush; begin takes it and returns the target, and end releases it. Both of its destructors throw, because the only locked stream is the static default one, which upstream never destroys. The slots are D1 at 0, D0 at 1, vprintf 2, flush 3, begin 4 and end 5. Of the open PRs that build WTF stream vtables, #20404 uses the same slot layout, and #19839 uses the same slots 2-5.

setDataFile and open are libSceJsc exports, while dataFile and the destructors are libSceNKWebKit exports. libSceNKWebKit already links libSceJsc, so libSceJsc owns the single stream pointer and the vtables (src/WtfDataLog.cpp). The libSceNKWebKit exports call into it through WtfDataFile_nid_no_patch, WtfFilePrintStreamDestroy_nid_no_patch and WtfFilePrintStreamDelete_nid_no_patch.

Gaps, recorded in docs/dev/debt/libSceJsc.md through the fragment:

  • WebKit allocates PrintStream objects with fastMalloc and frees them with fastFree (WTF_MAKE_FAST_ALLOCATED). Those live in libSceNKWebKit, which libSceJsc cannot import. open therefore uses the guest operator new and D0 the guest operator delete. The console allocator is unmeasured.
  • The vtables have no RTTI.
  • The lock is a host std::recursive_mutex rather than RecursiveLockAdapter<WordLock>.
  • Destroying the static locked stream (D0 or D1) throws. Upstream never destroys it, and its destructor would free static storage through std::unique_ptr.
  • dataFile() throws when setDataFile replaced the default stream with a null one. Upstream would return a reference through a null pointer.
  • Nothing was compared with a console module.

Not in this PR: WTF::setDataFile(const char*) (libSceNKWebKit) still throws. Also, main's dataLogF/dataLogFV (WtfAssert.cpp) still write to host stderr, while on PlayStation they print through dataFile(), which is stdout. #19828 (WTF accumulated logs and dataLogFString) notes the same difference and does not implement any of these six functions. Routing dataLogF* through dataFile() can follow in a separate PR once one of the two lands.

Tested

macOS 27 on Apple silicon, x86_64 Release build under Rosetta (Ninja, /usr/bin/c++, BUILD_TESTING=ON). C files are built with -D_FORTIFY_SOURCE=0, because on current main the LibreSSL strlcpy.c and strlcat.c from #17961 do not compile against the macOS SDK's fortified strlcpy/strlcat macros.

  • New guest test executable GuestWtfDataLog.cpp, run as three ctest entries because dataFile() initialises once per process: guest_wtf_data_log, guest_wtf_data_log_install_first and guest_wtf_data_log_install_null. All three pass, and pass 10 of 10 with ctest --repeat until-fail:10. The test registers a host-backed application heap that records frees, and states slot indexes and Adopt = 0 / Borrow = 1 as literal numbers. It checks:
    • The first dataFile() output reaches the host stdout (captured with dup2): guest_wtf_data_log: default sink 1\n.
    • dataFile() returns one stream. Its target borrows guest stdout, and recursive begin/end work on one thread. A second thread's flush waits while begin holds the lock.
    • Slots 0 and 1 of the static locked stream throw and leave it installed with its target.
    • open + vprintf (%s=%d;%.1f) + flush writes the file. D0, through the export and through vtable slot 1, frees the object and closes the FILE (fcntl(F_GETFD) on its descriptor fails with EBADF), flushing unflushed output.
    • D1 and D2 through the exports, and D1 through vtable slot 0, fclose an adopted file and keep the object.
    • All five destructor paths leave a borrowed FILE open.
    • A failed fopen gives an empty unique_ptr.
    • setDataFile after the default sink, and setDataFile before the first dataFile(), take ownership; dataFile() then returns the installed stream and its output reaches the file. A second setDataFile throws and leaves ownership with the caller.
    • A null stream installed before the first dataFile() gives the default sink; a null stream installed over the default sink makes dataFile() throw.
    • Mutations caught: dropping the operator delete in D0, dropping the fclose, and pointing the default sink at stderr each fail the test.
  • After merging main 2e48261498a and rebuilding, the three guest_wtf_data_log tests pass again. Control: on origin/main 1ac29cd4105, a program that calls each of the six exports gets not implemented from every one.
  • ctest -R "jsc|nkwebkit|webkit|wtf|public_headers|progress": ctest reports 100% of 217 tests passed, including public_headers and its 64 compile shards. Of those 217, ctest lists 4 GPU tests as skipped on this machine: agc_driver_submit_progress, agc_driver_prepared_shaders_abi_deferred_background_progress, agc_submit_progress and agc_submit_progress_no_timeline.
  • tools/check_conventions.py --base origin/main passes. tools/debt_fold.py --check reports only the existing docs-nkwebkit-drop-readded-count-lines.md failure from main.
  • tools/progress.py --module (main 2e48261498 -> this branch): libSceNKWebKit 3240 -> 3244 of 11268, libSceJsc 83 -> 85 of 737.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5346830904 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21958). Originally posted by [frankea](https://github.com/frankea) on 2026-11-16T23:04:44Z. --- ### What Implements the WTF `DataLog` sink and `FilePrintStream`, six exports that were `NotImplemented_nid_no_patch` stubs: `WTF::dataFile()` and the `FilePrintStream` D0/D1/D2 destructors in libSceNKWebKit, and `WTF::FilePrintStream::open` and `WTF::setDataFile(std::unique_ptr<PrintStream>&&)` in libSceJsc. The behaviour follows the PlayStation branch of [DataLog.cpp L56-L179](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/DataLog.cpp#L56-L179) at PS5-WebKit-11.x `80f77e94c63` (`initializeLogFileOnce`, `initializeLogFile`, both `setDataFile` overloads and `dataFile`), with [FilePrintStream.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/FilePrintStream.cpp), [LockedPrintStream.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/LockedPrintStream.cpp) and the `PrintStream` defaults in [PrintStream.cpp](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/PrintStream.cpp#L58-L69). FilePrintStream.h/.cpp are identical to WebKit `c55364d53f97`. - `dataFile()` runs the one-time initialisation under `std::call_once` and returns the stream. When nothing was installed, the default sink is a `FilePrintStream` over the guest libc `stdout` in `Borrow` mode, placed in static storage (L151-L152). `setvbuf(_IONBF)` is skipped because the file is `stdout` (L158-L161). The stream is wrapped in a `LockedPrintStream` (L166). - `setDataFile(unique_ptr&&)` takes ownership of the caller's stream and leaves the `unique_ptr` empty. As upstream's `RELEASE_ASSERT` (L171) requires, it is accepted only when no stream is installed or the installed one is the static locked stream. A second install throws instead of crashing, and the caller keeps ownership. - `FilePrintStream::open` calls guest `fopen` and returns the new stream through the hidden `unique_ptr` result, or an empty `unique_ptr` when `fopen` fails. - The `FilePrintStream` destructors (D1, D2) `fclose` the file unless the mode is `Borrow`. D0 then frees the object. - The guest object layout is vptr, `FILE*`, then the `AdoptionMode` enum (24 bytes, pinned with `static_assert`). The repo builds its own vtables, as libwebrtc does (`Vtable + 2`): D1, D0, `vprintf`, `flush`, `begin`, `end`. `FilePrintStream::vprintf` is guest `vfprintf` and `flush` is guest `fflush`. `begin` returns the stream and `end` does nothing. `LockedPrintStream` holds a recursive lock for `vprintf` and `flush`; `begin` takes it and returns the target, and `end` releases it. Both of its destructors throw, because the only locked stream is the static default one, which upstream never destroys. The slots are D1 at 0, D0 at 1, `vprintf` 2, `flush` 3, `begin` 4 and `end` 5. Of the open PRs that build WTF stream vtables, #20404 uses the same slot layout, and #19839 uses the same slots 2-5. `setDataFile` and `open` are libSceJsc exports, while `dataFile` and the destructors are libSceNKWebKit exports. libSceNKWebKit already links libSceJsc, so libSceJsc owns the single stream pointer and the vtables (`src/WtfDataLog.cpp`). The libSceNKWebKit exports call into it through `WtfDataFile_nid_no_patch`, `WtfFilePrintStreamDestroy_nid_no_patch` and `WtfFilePrintStreamDelete_nid_no_patch`. Gaps, recorded in `docs/dev/debt/libSceJsc.md` through the fragment: - WebKit allocates `PrintStream` objects with `fastMalloc` and frees them with `fastFree` (`WTF_MAKE_FAST_ALLOCATED`). Those live in libSceNKWebKit, which libSceJsc cannot import. `open` therefore uses the guest `operator new` and D0 the guest `operator delete`. The console allocator is unmeasured. - The vtables have no RTTI. - The lock is a host `std::recursive_mutex` rather than `RecursiveLockAdapter<WordLock>`. - Destroying the static locked stream (D0 or D1) throws. Upstream never destroys it, and its destructor would free static storage through `std::unique_ptr`. - `dataFile()` throws when `setDataFile` replaced the default stream with a null one. Upstream would return a reference through a null pointer. - Nothing was compared with a console module. Not in this PR: `WTF::setDataFile(const char*)` (libSceNKWebKit) still throws. Also, main's `dataLogF`/`dataLogFV` (`WtfAssert.cpp`) still write to host `stderr`, while on PlayStation they print through `dataFile()`, which is `stdout`. #19828 (WTF accumulated logs and `dataLogFString`) notes the same difference and does not implement any of these six functions. Routing `dataLogF*` through `dataFile()` can follow in a separate PR once one of the two lands. ### Tested macOS 27 on Apple silicon, x86_64 Release build under Rosetta (Ninja, `/usr/bin/c++`, `BUILD_TESTING=ON`). C files are built with `-D_FORTIFY_SOURCE=0`, because on current `main` the LibreSSL `strlcpy.c` and `strlcat.c` from #17961 do not compile against the macOS SDK's fortified `strlcpy`/`strlcat` macros. - New guest test executable `GuestWtfDataLog.cpp`, run as three ctest entries because `dataFile()` initialises once per process: `guest_wtf_data_log`, `guest_wtf_data_log_install_first` and `guest_wtf_data_log_install_null`. All three pass, and pass 10 of 10 with `ctest --repeat until-fail:10`. The test registers a host-backed application heap that records frees, and states slot indexes and `Adopt = 0` / `Borrow = 1` as literal numbers. It checks: - The first `dataFile()` output reaches the host `stdout` (captured with `dup2`): `guest_wtf_data_log: default sink 1\n`. - `dataFile()` returns one stream. Its target borrows guest `stdout`, and recursive `begin`/`end` work on one thread. A second thread's `flush` waits while `begin` holds the lock. - Slots 0 and 1 of the static locked stream throw and leave it installed with its target. - `open` + `vprintf` (`%s=%d;%.1f`) + `flush` writes the file. D0, through the export and through vtable slot 1, frees the object and closes the `FILE` (`fcntl(F_GETFD)` on its descriptor fails with `EBADF`), flushing unflushed output. - D1 and D2 through the exports, and D1 through vtable slot 0, `fclose` an adopted file and keep the object. - All five destructor paths leave a borrowed `FILE` open. - A failed `fopen` gives an empty `unique_ptr`. - `setDataFile` after the default sink, and `setDataFile` before the first `dataFile()`, take ownership; `dataFile()` then returns the installed stream and its output reaches the file. A second `setDataFile` throws and leaves ownership with the caller. - A null stream installed before the first `dataFile()` gives the default sink; a null stream installed over the default sink makes `dataFile()` throw. - Mutations caught: dropping the `operator delete` in D0, dropping the `fclose`, and pointing the default sink at `stderr` each fail the test. - After merging `main` `2e48261498a` and rebuilding, the three `guest_wtf_data_log` tests pass again. Control: on `origin/main` `1ac29cd4105`, a program that calls each of the six exports gets `not implemented` from every one. - `ctest -R "jsc|nkwebkit|webkit|wtf|public_headers|progress"`: ctest reports 100% of 217 tests passed, including `public_headers` and its 64 compile shards. Of those 217, ctest lists 4 GPU tests as skipped on this machine: `agc_driver_submit_progress`, `agc_driver_prepared_shaders_abi_deferred_background_progress`, `agc_submit_progress` and `agc_submit_progress_no_timeline`. - `tools/check_conventions.py --base origin/main` passes. `tools/debt_fold.py --check` reports only the existing `docs-nkwebkit-drop-readded-count-lines.md` failure from `main`. - `tools/progress.py --module` (`main` 2e48261498a -> this branch): libSceNKWebKit 3240 -> 3244 of 11268, libSceJsc 83 -> 85 of 737. ### 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) 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 (Claude Code)
PrivacyOnion commented 2026-11-16 23:19:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T23:19:25Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_wtf_data_log_tests built cleanly.
  • CTest (guest_wtf_data_log, guest_wtf_data_log_install_first, guest_wtf_data_log_install_null):
    1/3 Test #690: guest_wtf_data_log_install_first ...   Passed    0.01 sec
    2/3 Test #691: guest_wtf_data_log_install_null ....   Passed    0.01 sec
    3/3 Test #689: guest_wtf_data_log .................   Passed    0.12 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libSceJsc/Export.cpp, core/libs/prx/libSceJsc/include/InventoryExports.hpp, core/libs/prx/libSceJsc/libSceJsc.hpp, core/libs/prx/libSceNKWebKit/Export.cpp, core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp, core/libs/prx/libSceNKWebKit/libSceNKWebKit.hpp, core/libs/prx/libSceJsc/src/WtfDataLog.cpp, core/libs/prx/libSceNKWebKit/src/WtfDataFile.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified WTF::setDataFile takes ownership of the unique_ptr and rejects subsequent assignments with std::logic_error if a non-default stream is already installed
    • Verified WTF::FilePrintStream::open handles file open failure, memory allocation failure with cleanup, and returns pointer to result slot
    • Verified FilePrintStream adoptionMode behavior: Borrow does not close stream->file, Adopt closes it via fclose_nid_postfix
    • Verified vtable layout, method slots (CompleteDestructor=0, DeletingDestructor=1, Vprintf=2, Flush=3, Begin=4, End=5), and calling conventions (APS5_VABI)
    • Verified thread-safety in LockedPrintStream via std::recursive_mutex and std::call_once initialization in WtfDataFile_nid_no_patch
    • Verified AGENTS.md compliance: tech debt fragment added, no forbidden comments, and proper exports/headers

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5929644388 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21958#pullrequestreview-5929644388). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T23:19:25Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `b0052d1807`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_wtf_data_log_tests` built cleanly. - **CTest** (`guest_wtf_data_log`, `guest_wtf_data_log_install_first`, `guest_wtf_data_log_install_null`): ``` 1/3 Test #690: guest_wtf_data_log_install_first ... Passed 0.01 sec 2/3 Test #691: guest_wtf_data_log_install_null .... Passed 0.01 sec 3/3 Test #689: guest_wtf_data_log ................. Passed 0.12 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libSceJsc/Export.cpp`, `core/libs/prx/libSceJsc/include/InventoryExports.hpp`, `core/libs/prx/libSceJsc/libSceJsc.hpp`, `core/libs/prx/libSceNKWebKit/Export.cpp`, `core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp`, `core/libs/prx/libSceNKWebKit/libSceNKWebKit.hpp`, `core/libs/prx/libSceJsc/src/WtfDataLog.cpp`, `core/libs/prx/libSceNKWebKit/src/WtfDataFile.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified WTF::setDataFile takes ownership of the unique_ptr and rejects subsequent assignments with std::logic_error if a non-default stream is already installed - Verified WTF::FilePrintStream::open handles file open failure, memory allocation failure with cleanup, and returns pointer to result slot - Verified FilePrintStream adoptionMode behavior: Borrow does not close stream->file, Adopt closes it via fclose_nid_postfix - Verified vtable layout, method slots (CompleteDestructor=0, DeletingDestructor=1, Vprintf=2, Flush=3, Begin=4, End=5), and calling conventions (APS5_VABI) - Verified thread-safety in LockedPrintStream via std::recursive_mutex and std::call_once initialization in WtfDataFile_nid_no_patch - Verified AGENTS.md compliance: tech debt fragment added, no forbidden comments, and proper exports/headers 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!21958
No description provided.