fix(libSceNKWebKit): clamp IntRect scale and AffineTransform int rounding #15043
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15043"
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 MarcZX10 on 2026-11-02T11:50:34Z.
What
IntRect::scalenow converts each scaled field with a truncating conversion that returnsINT_MINfor values outside the int range and for NaN, matching the firmware'svcvttps2dqbehaviour (reviewer disassembly of libSceNKWebKit 13.42). The float product is still computed in float, as the firmware does. This removes the undefined behaviour of the oldstatic_cast<int>.AffineTransform::mapSizeandmapPointforIntSizeandIntPointare back to the plainstatic_cast<int>(std::lround(...))ofmain. The clamp added earlier in this PR is reverted, because the firmware'smapPointwraps the low 32 bits of thelroundresult.mapSizewas not disassembled; it is reverted too, pending a check.Commits: the fixes for the two functions, and the test update (
IntRect::scaleoverflow now expectsINT_MIN; themapPoint/mapSizetest was removed).Tested
run_tests.sh, host kernel 6.1), targetnkwebkit_webcoregeometry_tests, ctestnkwebkit_webcoregeometry: Passed (HEADf459b8f214).tools/check_conventions.py --base <merge-base>andgit diff --check: clean.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 BunnyFl on 2026-11-02T11:57:31Z.
Review: DISMISSED
Thanks for chasing the UB. The firmware's own code, though, gives different values from the clamping, so I'm requesting changes.
Firmware 13.42's
libSceNKWebKit.sprx(system_ex/common_ex/lib, sha256e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409), read to the return, from disassembly only, not measured on a console:So on the console:
IntRect::scalegivesINT_MIN(0x80000000) for any field whose float product is out of range, positive or negative, and for NaN. It never givesINT_MAX. To remove the UB and match, convert out-of-range or NaN products tostd::numeric_limits<int>::min()rather thanClampToInteger.mapPointwraps: the 64-bitlroundresult is truncated to its low 32 bits, so translating by1.0e10gives1410065408, notINT_MAX.main'sstatic_cast<int>(std::lround(x))already does exactly that, and since C++20 that narrowing is defined as modulo 2³², so there's no UB to fix. The only unspecified case islroundoverflowinglong(|x| ≥ 2⁶³). If you want that pinned, x86-64'scvtsd2sireturns0x8000000000000000, whose low 32 bits are 0. That needs0x2adaf60's implementation checked to be sure, which I haven't done.mapSize(n1m5ZaCMtpk,0x1ea350) I didn't disassemble, but with the samelroundit presumably wraps the same way. Worth checking before changing it.INT_MINfor an overflowingscaleand the wrapped values formapPoint/mapSize.A side note, out of scope: the module computes
x2/y2withvfmadd231sd, soa*x + c*yis fused. Matching that bit-exactly would needstd::fma. Not asking for it here.I didn't build this branch; the requested values come from the module.
Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-06T17:38:42Z.
Review: APPROVED
@MarcZX10 thanks, re-checked at
f459b8f214, and my change request is resolved.IntRect::scalenow givesINT_MINfor out-of-range and NaN products, asvcvttps2dqdoes, andmapPointkeepsmain's wrappinglround.mapSize, which you left open. Firmware 13.42system_ex/common_ex/lib/libSceNKWebKit.sprx(sha256e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409),n1m5ZaCMtpkat0x1ea350, read to the return, from disassembly only, not measured on a console:The two call targets are PLT stubs:
0x2ad8a60jumps through GOT slot0x334f560, whose import isYFoOw5GkkK0(hypot), and0x2adaf60through0x33507e0, whose import isJ3XuGS-cC0Q(lround). SomapSizewraps exactly likemapPoint.main'smapSize(WebCoreTransform.cpp:304-312) computeswidth * xScale()withxScale()asstd::hypot(m[0], m[1])(:243-245), thenstatic_cast<int>(std::lround(...)), which matches step for step. Reverting it was right. I didn't read the module'shypotitself, so bit-exact agreement with the host'sstd::hypotisn't checked.What I ran: Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0, Release, this head merged onto current
main(365b808edf):nkwebkit_webcoregeometrypasses (1 of 1).main'sWebCoreGeometry.cpprestored, the new test also passes (1 of 1). On this toolchainmain's plainstatic_cast<int>(float)happens to giveINT_MINfor these inputs too (the plain build in the next bullet below shows it), so the test can't tell the two apart here.scaleinto a standalone file and built it with-O2 -fsanitize=float-cast-overflow -fsanitize-trap=all. With the test's inputs,main's version traps (exit 132) and yours prints-2147483648 -2147483648 -2147483648 30(exit 0). A plain-O2build gives the same numbers for both. That shows the change removes the UB the original PR was about, and keeps the value the firmware produces.tools/check_conventions.py --base upstream/main --headon this head: all checks pass.tools/pr_overlap.py --branch: no overlap with open pull requests.Non-blocking: since the test passes on
mainwith GCC (the restored-mainrun above), it pins the firmware value rather than catching a regression. A UBSan CI job would catch it, but that's outside this PR. The fused multiply-add inmapPointstays out of scope, as you said.AI-assisted: yes (Claude Code)