feat(libSceJsc): answer the JSString null and length cases as JavaScriptCore does #12442
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12442"
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 simnJS on 2026-10-28T13:42:12Z.
What
Six
libSceJscJavaScriptCore string exports were implemented onmain, but a few of their inputs still went throughNotImplemented_nid_no_patch. JavaScriptCore's own source defines those inputs, both in the WebKit commit the debt file already pins (f274ef4c03) and in Sony's PS5 WebKit for firmware 11.x (a GitHub copy of Sony's source release for firmware 11.00 to 11.60). They now answer as that source does:JSStringCreateWithUTF8CString(nullptr)returns a new null string, as it does for malformed UTF-8. WebKit takes the null pointer as an empty span (StringCommon.h),StringImpl::createreturns null for it (StringImpl.cpp), and the function then falls back toOpaqueJSString::create()(JSStringRef.cpp). Sony's 11.x version tests the pointer itself (JSStringRef.cpp).JSStringCreateWithUTF8CStringorJSStringIsEqualToUTF8CStringfollowsStringImpl::create(std::span<const char8_t>)(StringImpl.cpp):INT32_MAXbytes throwsstd::length_errorbefore decoding, where WebKit'sRELEASE_ASSERT(isValidLength<char8_t>)fires;INT32_MAX;StringImplcreation callsCRASH().The shared decoder checks both bounds with
GuestJSStringValidLength<Unit>(Api.hpp), which mirrorsisValidLength(StringImpl.h).JSStringCreateWithCharactersuses the same helper.JSStringIsEqualToUTF8CString(string, nullptr)comparesstringwith that null string (JSStringRef.cpp). It is true only for a null string, never for an empty string or a null handle (OpaqueJSString.cpp).JSStringCreateWithCharacters(nullptr, count)returns a null string for anycount. WTF'sStringconstructor keeps a null pointer as a null string without reading it (WTFString.cpp; Sony 11.x: WTFString.cpp).std::length_errorwhere WebKit callsCRASH()(StringImpl.h, L1273-L1281).(UINT32_MAX - sizeof(StringImpl)) / 2with the 24-byte header of a 64-bit build: a 4-byte count, a 4-byte length, an 8-byte data pointer and 4-byte hash and flags, padded to 8 (StringImpl.h).mainthrew aboveINT32_MAXinstead.JSStringRetain,JSStringReleaseandJSStringGetMaximumUTF8CStringSizedereference the handle without a check (JSStringRef.cpp; Sony 11.x: L68-L97), so the console faults on a null handle. They throwstd::invalid_argumentfor it, asmainalready does where the console faults, for example insigsuspendandfeupdateenv.ThreadSafeRefCountedis (ThreadSafeRefCounted.h; Sony 11.x: L55-L100). A release deletes the string only when it takes the count from 1 to 0.mainused a 64-bit counter and threw when it was full.JSStringGetMaximumUTF8CStringSizecomputeslength() * 3 + 1in 32-bit unsigned arithmetic, as WebKit'sunsigned length()does, so it wraps from 1431655765 units on.The six names move from todo to done in
tools/progress.py(libSceJsc 4 → 10 of 160). The debt entries keep what is still open: the console's JavaScriptCore version is unverified, and the null-handle and length faults throw. Sony's 11.x source has one difference, recorded there: it keeps the low 32 bits of a length of 2^32 or more (OpaqueJSString.h), which throws here as in pinned WebKit. Not measured on a console.Tested
Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release,
BUILD_TESTING=ON, onmain750cf6cf48. No title run.guest_jsc_string(extended): passes, 40 runs on this head (ctest --repeat until-fail:20twice). No other test links libSceJsc.SIZE_MAX: a null string (length 0, null characters pointer) equal to the null UTF-8 string;INT32_MAX,INT32_MAX + 1,UINT32_MAX, 2^32 andSIZE_MAX:std::length_errorbefore any read, since the test's pointer is unreadable;JSStringCreateWithUTF8CString(nullptr): length 0, a null characters pointer, maximum size 1 and an output of"". It equals malformed UTF-8 and null characters, but not the empty strings of either constructor;JSStringIsEqualToUTF8CStringwith a nullconst char*, against null, malformed, empty and non-empty strings and a null handle, and a null handle against"";std::invalid_argument;UINT32_MAXwraps it to 0, and the next release takes it back toUINT32_MAXwithout deleting the string;GuestJSStringValidLengthat and around both limits:char8_taccepts up toINT32_MAXand refusesINT32_MAX + 1, 2^32 andSIZE_MAX;char16_taccepts 2147483635 and refuses 2147483636,INT32_MAXand above.main'sExport.cppandApi.hppput back,guest_jsc_stringfails at its first new case.mainreads 2147483636 units from the test's unreadable pointer (access violation at0x1, exception0xc0000005), where WebKit crashes before reading.public_headersandpublic_headers_compile_*: 65/65 pass.2b10aa4211:build.ymlin full mode (run): no failures out of 2837 tests (851 skipped), includingguest_jsc_string.guest_jsc_stringandpublic_headerspass. Neither failure involves libSceJsc:conventions_ci_gatecomes from the fork's own workflow file, which was fixed on the fork after this run started;precise_waitis a timing test that fails on the fork'smaintoo.python tools/check_conventions.py --base origin/mainwith this title and body: all checks pass.python tools/pr_overlap.py --branch feat/jsc-string-edge-answers: no overlap with open pull requests.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 MarcZX10 on 2026-10-28T13:50:17Z.
Review: COMMENTED
I reviewed the exact head
422de87a44and manually compiled and ranGuestJscString.cppwith the PRExport.cppunder GCC 12/C++20; the test executable passed. The minimal harness supplied only the unrelated no-patch export handler needed to link this standalone test, so this is not a full project build. The added null/empty, malformed UTF-8, surrogate, buffer-boundary, and ownership cases align with the implementation and cited WebKit behavior; I found no defect in the reviewed paths. Hosted convention/eligibility checks were still queued at my last refresh, and CMake configuration here is blocked by the missing SDL2 source/XRandR support.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T13:58:32Z.
Review: COMMENTED
Rechecked the rebased/current head
50c8d4cd8fafter the force-push. Its source/test tree is unchanged from the head I previously compiled and ran; the GCC 12/C++20 standaloneGuestJscStringharness passed, with the previously noted limitation that a temporary handler was needed only for unrelated no-patch exports. The new hosted checks are queued. The earlier code review therefore still applies to this exact tree; this is a COMMENTED follow-up, not an approval.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T14:30:00Z.
Review: CHANGES_REQUESTED
One remaining length-path mismatch:
Decode(const char* text)appends decoded UTF-16 units until the NUL terminator without enforcing JavaScriptCore’s maximum source/string length. The newly addedMaximumLengthguard is used byJSStringCreateWithCharacters, butJSStringCreateWithUTF8CString(andJSStringIsEqualToUTF8CString) can still accept and allocate from a C string longer than the pinned WebKit limit. In the cited WebKit revision,StringImpl::create(std::span<const char8_t>)appliesisValidLength<char8_t>and the resulting string construction also enforcesMaxLength(StringImpl.h,isValidLength); the UTF-8 decoder here has no equivalent check and may grow beyond it if a sufficiently large input is available. Please enforce the corresponding input/decoded-length bound before growing the vector (and cover the boundary with a helper-level test or otherwise document why this path is intentionally different). I have not allocated a multi-gigabyte string locally, so this is a source-based edge-case finding, not a claimed runtime reproduction. All applicable hosted checks and the exact-headGuestJscStringtest were green in the prior review; this is the remaining issue preventing approval.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T15:29:26Z.
Review: APPROVED
Reviewed exact head
2b10aa4211019c51de352517a43198cb3956e41a. The previously missing UTF-8 path now checks the byte length before decoding and checks the decoded UTF-16 length for non-ASCII strings; ASCII is bounded by the byte-length limit. The shared helper matches the documented 32-bit byte-size and signed-length constraints, and its tests cover both accepted and rejected boundaries without allocating multi-gigabyte inputs. The author reports 40 focused Windows runs, negative controls, and 65/65 public-header checks; hosted Windows, conventions and policy checks pass. My independent diff-check and merge-tree check pass. The earlier review finding is resolved; no blocker remains.