fix(libSceNKWebKit): clamp IntRect scale and AffineTransform int rounding #15043

Merged
MarcZX10 merged 5 commits from upstream-pr/15043 into main 2026-11-08 16:17:15 +00:00
MarcZX10 commented 2026-11-02 11:50:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T11:50:34Z.


What

  • IntRect::scale now converts each scaled field with a truncating conversion that returns INT_MIN for values outside the int range and for NaN, matching the firmware's vcvttps2dq behaviour (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 old static_cast<int>.
  • AffineTransform::mapSize and mapPoint for IntSize and IntPoint are back to the plain static_cast<int>(std::lround(...)) of main. The clamp added earlier in this PR is reverted, because the firmware's mapPoint wraps the low 32 bits of the lround result. mapSize was not disassembled; it is reverted too, pending a check.

Commits: the fixes for the two functions, and the test update (IntRect::scale overflow now expects INT_MIN; the mapPoint/mapSize test was removed).

Tested

  • Linux archive build (run_tests.sh, host kernel 6.1), target nkwebkit_webcoregeometry_tests, ctest nkwebkit_webcoregeometry: Passed (HEAD f459b8f214).
  • tools/check_conventions.py --base <merge-base> and git diff --check: clean.
  • Not run: negative control on the unfixed code; Windows, macOS and console builds; a console measurement.

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
  • 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:5119829233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15043). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T11:50:34Z. --- ### What - `IntRect::scale` now converts each scaled field with a truncating conversion that returns `INT_MIN` for values outside the int range and for NaN, matching the firmware's `vcvttps2dq` behaviour (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 old `static_cast<int>`. - `AffineTransform::mapSize` and `mapPoint` for `IntSize` and `IntPoint` are back to the plain `static_cast<int>(std::lround(...))` of `main`. The clamp added earlier in this PR is reverted, because the firmware's `mapPoint` wraps the low 32 bits of the `lround` result. `mapSize` was not disassembled; it is reverted too, pending a check. Commits: the fixes for the two functions, and the test update (`IntRect::scale` overflow now expects `INT_MIN`; the `mapPoint`/`mapSize` test was removed). ### Tested - Linux archive build (`run_tests.sh`, host kernel 6.1), target `nkwebkit_webcoregeometry_tests`, ctest `nkwebkit_webcoregeometry`: Passed (HEAD `f459b8f214`). - `tools/check_conventions.py --base <merge-base>` and `git diff --check`: clean. - Not run: negative control on the unfixed code; Windows, macOS and console builds; a console measurement. ### 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) - [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
BunnyFl commented 2026-11-02 11:57:31 +00:00 (Migrated from github.com)

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, sha256 e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409), read to the return, from disassembly only, not measured on a console:

0x35bde0 WebCore::IntRect::scale(float) (FEkLrSser8s)
  vcvtdq2ps xmm1, [rdi]           ; the four ints to float
  vbroadcastss xmm0, xmm0
  vmulps xmm0, xmm0, xmm1         ; product in float
  vcvttps2dq xmm0, xmm0           ; truncate: out of range or NaN -> 0x80000000
  vmovups [rdi], xmm0 ; ret

0x4d250 WebCore::AffineTransform::mapPoint(const IntPoint&) (MzgS-gyYwgY)
  vcvtsi2sd ... ; vmulsd ... ; vfmadd231sd ... ; vaddsd ...   ; x2, y2 (fused multiply-add)
  call 0x2adaf60 ; mov rbx, rax   ; lround(x2), 64-bit
  call 0x2adaf60                  ; lround(y2)
  shl rax, 32 ; mov ecx, ebx ; or rax, rcx ; ret   ; keeps the low 32 bits of each

So on the console:

  • IntRect::scale gives INT_MIN (0x80000000) for any field whose float product is out of range, positive or negative, and for NaN. It never gives INT_MAX. To remove the UB and match, convert out-of-range or NaN products to std::numeric_limits<int>::min() rather than ClampToInteger.
  • mapPoint wraps: the 64-bit lround result is truncated to its low 32 bits, so translating by 1.0e10 gives 1410065408, not INT_MAX. main's static_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 is lround overflowing long (|x| ≥ 2⁶³). If you want that pinned, x86-64's cvtsd2si returns 0x8000000000000000, whose low 32 bits are 0. That needs 0x2adaf60's implementation checked to be sure, which I haven't done.
  • mapSize (n1m5ZaCMtpk, 0x1ea350) I didn't disassemble, but with the same lround it presumably wraps the same way. Worth checking before changing it.
  • The tests would then expect INT_MIN for an overflowing scale and the wrapped values for mapPoint/mapSize.

A side note, out of scope: the module computes x2/y2 with vfmadd231sd, so a*x + c*y is fused. Matching that bit-exactly would need std::fma. Not asking for it here.

I didn't build this branch; the requested values come from the module.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5736176374 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15043#pullrequestreview-5736176374). Originally posted by [BunnyFl](https://github.com/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`, sha256 `e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409`), read to the return, from disassembly only, not measured on a console: ``` 0x35bde0 WebCore::IntRect::scale(float) (FEkLrSser8s) vcvtdq2ps xmm1, [rdi] ; the four ints to float vbroadcastss xmm0, xmm0 vmulps xmm0, xmm0, xmm1 ; product in float vcvttps2dq xmm0, xmm0 ; truncate: out of range or NaN -> 0x80000000 vmovups [rdi], xmm0 ; ret 0x4d250 WebCore::AffineTransform::mapPoint(const IntPoint&) (MzgS-gyYwgY) vcvtsi2sd ... ; vmulsd ... ; vfmadd231sd ... ; vaddsd ... ; x2, y2 (fused multiply-add) call 0x2adaf60 ; mov rbx, rax ; lround(x2), 64-bit call 0x2adaf60 ; lround(y2) shl rax, 32 ; mov ecx, ebx ; or rax, rcx ; ret ; keeps the low 32 bits of each ``` So on the console: - **`IntRect::scale`** gives `INT_MIN` (`0x80000000`) for any field whose float product is out of range, positive or negative, and for NaN. It never gives `INT_MAX`. To remove the UB and match, convert out-of-range or NaN products to `std::numeric_limits<int>::min()` rather than `ClampToInteger`. - **`mapPoint`** wraps: the 64-bit `lround` result is truncated to its low 32 bits, so translating by `1.0e10` gives `1410065408`, not `INT_MAX`. `main`'s `static_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 is `lround` overflowing `long` (|x| ≥ 2⁶³). If you want that pinned, x86-64's `cvtsd2si` returns `0x8000000000000000`, whose low 32 bits are 0. That needs `0x2adaf60`'s implementation checked to be sure, which I haven't done. - **`mapSize`** (`n1m5ZaCMtpk`, `0x1ea350`) I didn't disassemble, but with the same `lround` it presumably wraps the same way. Worth checking before changing it. - The tests would then expect `INT_MIN` for an overflowing `scale` and the wrapped values for `mapPoint`/`mapSize`. A side note, out of scope: the module computes `x2`/`y2` with `vfmadd231sd`, so `a*x + c*y` is fused. Matching that bit-exactly would need `std::fma`. Not asking for it here. I didn't build this branch; the requested values come from the module.
BunnyFl commented 2026-11-06 17:38:42 +00:00 (Migrated from github.com)

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::scale now gives INT_MIN for out-of-range and NaN products, as vcvttps2dq does, and mapPoint keeps main's wrapping lround.

mapSize, which you left open. Firmware 13.42 system_ex/common_ex/lib/libSceNKWebKit.sprx (sha256 e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409), n1m5ZaCMtpk at 0x1ea350, read to the return, from disassembly only, not measured on a console:

1ea35b: vcvtsi2sdl (%rsi),%xmm15,%xmm0     ; (double)size.width
1ea35f: vmovsd 0x8(%rdi),%xmm1             ; m[1]
1ea36f: vmovsd (%rdi),%xmm0                ; m[0]
1ea373: call 0x2ad8a60                     ; hypot(m[0], m[1])
1ea378: vmulsd -0x20(%rbp),%xmm0,%xmm0     ; * width
1ea388: vcvtsi2sdl 0x4(%rbx),%xmm15,%xmm0  ; (double)size.height
1ea392: vmovsd 0x10(%r14),%xmm0            ; m[2], with m[3] from 0x18(%r14)
1ea398: call 0x2ad8a60                     ; hypot(m[2], m[3])
1ea39d: vmulsd -0x18(%rbp),%xmm0,%xmm0     ; * height
1ea3ac: call 0x2adaf60                     ; lround(width2)
1ea3b9: call 0x2adaf60                     ; lround(height2)
1ea3be: shl $0x20,%rax ; mov %ebx,%ecx ; or %rcx,%rax ; ret   ; low 32 bits of each

The two call targets are PLT stubs: 0x2ad8a60 jumps through GOT slot 0x334f560, whose import is YFoOw5GkkK0 (hypot), and 0x2adaf60 through 0x33507e0, whose import is J3XuGS-cC0Q (lround). So mapSize wraps exactly like mapPoint. main's mapSize (WebCoreTransform.cpp:304-312) computes width * xScale() with xScale() as std::hypot(m[0], m[1]) (:243-245), then static_cast<int>(std::lround(...)), which matches step for step. Reverting it was right. I didn't read the module's hypot itself, so bit-exact agreement with the host's std::hypot isn'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_webcoregeometry passes (1 of 1).
  • With main's WebCoreGeometry.cpp restored, the new test also passes (1 of 1). On this toolchain main's plain static_cast<int>(float) happens to give INT_MIN for these inputs too (the plain build in the next bullet below shows it), so the test can't tell the two apart here.
  • So the negative control was a sanitizer instead. I copied both versions of scale into 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 -O2 build 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 --head on this head: all checks pass.
  • tools/pr_overlap.py --branch: no overlap with open pull requests.

Non-blocking: since the test passes on main with GCC (the restored-main run 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 in mapPoint stays out of scope, as you said.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5804421458 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15043#pullrequestreview-5804421458). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-06T17:38:42Z. **Review:** APPROVED --- @MarcZX10 thanks, re-checked at `f459b8f214`, and my change request is resolved. `IntRect::scale` now gives `INT_MIN` for out-of-range and NaN products, as `vcvttps2dq` does, and `mapPoint` keeps `main`'s wrapping `lround`. **`mapSize`, which you left open.** Firmware 13.42 `system_ex/common_ex/lib/libSceNKWebKit.sprx` (sha256 `e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409`), `n1m5ZaCMtpk` at `0x1ea350`, read to the return, from disassembly only, not measured on a console: ``` 1ea35b: vcvtsi2sdl (%rsi),%xmm15,%xmm0 ; (double)size.width 1ea35f: vmovsd 0x8(%rdi),%xmm1 ; m[1] 1ea36f: vmovsd (%rdi),%xmm0 ; m[0] 1ea373: call 0x2ad8a60 ; hypot(m[0], m[1]) 1ea378: vmulsd -0x20(%rbp),%xmm0,%xmm0 ; * width 1ea388: vcvtsi2sdl 0x4(%rbx),%xmm15,%xmm0 ; (double)size.height 1ea392: vmovsd 0x10(%r14),%xmm0 ; m[2], with m[3] from 0x18(%r14) 1ea398: call 0x2ad8a60 ; hypot(m[2], m[3]) 1ea39d: vmulsd -0x18(%rbp),%xmm0,%xmm0 ; * height 1ea3ac: call 0x2adaf60 ; lround(width2) 1ea3b9: call 0x2adaf60 ; lround(height2) 1ea3be: shl $0x20,%rax ; mov %ebx,%ecx ; or %rcx,%rax ; ret ; low 32 bits of each ``` The two call targets are PLT stubs: `0x2ad8a60` jumps through GOT slot `0x334f560`, whose import is `YFoOw5GkkK0` (`hypot`), and `0x2adaf60` through `0x33507e0`, whose import is `J3XuGS-cC0Q` (`lround`). So `mapSize` wraps exactly like `mapPoint`. `main`'s `mapSize` (`WebCoreTransform.cpp:304-312`) computes `width * xScale()` with `xScale()` as `std::hypot(m[0], m[1])` (`:243-245`), then `static_cast<int>(std::lround(...))`, which matches step for step. Reverting it was right. I didn't read the module's `hypot` itself, so bit-exact agreement with the host's `std::hypot` isn'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_webcoregeometry` passes (1 of 1). - With `main`'s `WebCoreGeometry.cpp` restored, the new test **also passes** (1 of 1). On this toolchain `main`'s plain `static_cast<int>(float)` happens to give `INT_MIN` for these inputs too (the plain build in the next bullet below shows it), so the test can't tell the two apart here. - **So the negative control was a sanitizer instead.** I copied both versions of `scale` into 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 `-O2` build 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 --head` on this head: all checks pass. - `tools/pr_overlap.py --branch`: no overlap with open pull requests. Non-blocking: since the test passes on `main` with GCC (the restored-`main` run 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 in `mapPoint` stays out of scope, as you said. AI-assisted: yes (Claude Code)
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!15043
No description provided.