feat(libSceJsc): implement WTF dataFile, setDataFile and FilePrintStream #21958
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21958"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-11-16T23:04:44Z.
What
Implements the WTF
DataLogsink andFilePrintStream, six exports that wereNotImplemented_nid_no_patchstubs:WTF::dataFile()and theFilePrintStreamD0/D1/D2 destructors in libSceNKWebKit, andWTF::FilePrintStream::openandWTF::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, bothsetDataFileoverloads anddataFile), with FilePrintStream.cpp, LockedPrintStream.cpp and thePrintStreamdefaults in PrintStream.cpp. FilePrintStream.h/.cpp are identical to WebKitc55364d53f97.dataFile()runs the one-time initialisation understd::call_onceand returns the stream. When nothing was installed, the default sink is aFilePrintStreamover the guest libcstdoutinBorrowmode, placed in static storage (L151-L152).setvbuf(_IONBF)is skipped because the file isstdout(L158-L161). The stream is wrapped in aLockedPrintStream(L166).setDataFile(unique_ptr&&)takes ownership of the caller's stream and leaves theunique_ptrempty. As upstream'sRELEASE_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::opencalls guestfopenand returns the new stream through the hiddenunique_ptrresult, or an emptyunique_ptrwhenfopenfails.FilePrintStreamdestructors (D1, D2)fclosethe file unless the mode isBorrow. D0 then frees the object.FILE*, then theAdoptionModeenum (24 bytes, pinned withstatic_assert). The repo builds its own vtables, as libwebrtc does (Vtable + 2): D1, D0,vprintf,flush,begin,end.FilePrintStream::vprintfis guestvfprintfandflushis guestfflush.beginreturns the stream andenddoes nothing.LockedPrintStreamholds a recursive lock forvprintfandflush;begintakes it and returns the target, andendreleases 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,vprintf2,flush3,begin4 andend5. Of the open PRs that build WTF stream vtables, #20404 uses the same slot layout, and #19839 uses the same slots 2-5.setDataFileandopenare libSceJsc exports, whiledataFileand 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 throughWtfDataFile_nid_no_patch,WtfFilePrintStreamDestroy_nid_no_patchandWtfFilePrintStreamDelete_nid_no_patch.Gaps, recorded in
docs/dev/debt/libSceJsc.mdthrough the fragment:PrintStreamobjects withfastMallocand frees them withfastFree(WTF_MAKE_FAST_ALLOCATED). Those live in libSceNKWebKit, which libSceJsc cannot import.opentherefore uses the guestoperator newand D0 the guestoperator delete. The console allocator is unmeasured.std::recursive_mutexrather thanRecursiveLockAdapter<WordLock>.std::unique_ptr.dataFile()throws whensetDataFilereplaced the default stream with a null one. Upstream would return a reference through a null pointer.Not in this PR:
WTF::setDataFile(const char*)(libSceNKWebKit) still throws. Also, main'sdataLogF/dataLogFV(WtfAssert.cpp) still write to hoststderr, while on PlayStation they print throughdataFile(), which isstdout. #19828 (WTF accumulated logs anddataLogFString) notes the same difference and does not implement any of these six functions. RoutingdataLogF*throughdataFile()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 currentmainthe LibreSSLstrlcpy.candstrlcat.cfrom #17961 do not compile against the macOS SDK's fortifiedstrlcpy/strlcatmacros.GuestWtfDataLog.cpp, run as three ctest entries becausedataFile()initialises once per process:guest_wtf_data_log,guest_wtf_data_log_install_firstandguest_wtf_data_log_install_null. All three pass, and pass 10 of 10 withctest --repeat until-fail:10. The test registers a host-backed application heap that records frees, and states slot indexes andAdopt = 0/Borrow = 1as literal numbers. It checks:dataFile()output reaches the hoststdout(captured withdup2):guest_wtf_data_log: default sink 1\n.dataFile()returns one stream. Its target borrows gueststdout, and recursivebegin/endwork on one thread. A second thread'sflushwaits whilebeginholds the lock.open+vprintf(%s=%d;%.1f) +flushwrites the file. D0, through the export and through vtable slot 1, frees the object and closes theFILE(fcntl(F_GETFD)on its descriptor fails withEBADF), flushing unflushed output.fclosean adopted file and keep the object.FILEopen.fopengives an emptyunique_ptr.setDataFileafter the default sink, andsetDataFilebefore the firstdataFile(), take ownership;dataFile()then returns the installed stream and its output reaches the file. A secondsetDataFilethrows and leaves ownership with the caller.dataFile()gives the default sink; a null stream installed over the default sink makesdataFile()throw.operator deletein D0, dropping thefclose, and pointing the default sink atstderreach fail the test.main2e48261498aand rebuilding, the threeguest_wtf_data_logtests pass again. Control: onorigin/main1ac29cd4105, a program that calls each of the six exports getsnot implementedfrom every one.ctest -R "jsc|nkwebkit|webkit|wtf|public_headers|progress": ctest reports 100% of 217 tests passed, includingpublic_headersand 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_progressandagc_submit_progress_no_timeline.tools/check_conventions.py --base origin/mainpasses.tools/debt_fold.py --checkreports only the existingdocs-nkwebkit-drop-readded-count-lines.mdfailure frommain.tools/progress.py --module(main2e48261498-> this branch): libSceNKWebKit 3240 -> 3244 of 11268, libSceJsc 83 -> 85 of 737.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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:tools/check_conventions.pypassed.guest_wtf_data_log_testsbuilt cleanly.guest_wtf_data_log,guest_wtf_data_log_install_first,guest_wtf_data_log_install_null):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.cppreverted tomain, the new tests do not build/link without the change; restoring it makes them build and pass.Looks Good To Me