Skip to content

fix(libSceNKWebKit): read and write WTFGetBacktrace's frame count through checked guest copies - #22783

Closed
milescoviello wants to merge 1 commit into
boykopovar:mainfrom
milescoviello:fix/nkwebkit-wtf-backtrace-guest-copies
Closed

milescoviello wants to merge 1 commit into
boykopovar:mainfrom
milescoviello:fix/nkwebkit-wtf-backtrace-guest-copies

Conversation

@milescoviello

Copy link
Copy Markdown
Contributor

What

WTFGetBacktrace read the requested frame count through the caller's size pointer and wrote the captured count back through it. On Windows it also captured frames with CaptureStackBackTrace straight into the caller's stack buffer. An unmapped count or buffer crashed the host.

Changes:

  • The requested count is read with GuestCopyIn_nid_no_patch. A count that can't be read returns at once, as a null pointer or a non-positive count does.
  • On Windows, frames are captured only when stack is writable for the requested count. Otherwise no frames are captured and the count is 0, the value every other host already reports.
  • The captured count is written back with one GuestCopyOut_nid_no_patch.

Results for valid pointers are unchanged. WTFReportBacktrace passes its own host buffers and behaves as before.

Tested

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON:

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

…ough checked guest copies

WTFGetBacktrace read and wrote the caller's frame count through the
guest pointer, and on Windows captured frames straight into the
caller's buffer, so an unmapped count or buffer crashed the host. The
count is now read with GuestCopyIn_nid_no_patch, and the captured count
is written back with one GuestCopyOut_nid_no_patch. A count that can't
be read returns at once, as a null one does. On Windows, a buffer that
isn't writable for the requested count captures no frames and reports
0, the count the other hosts always report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: nkwebkit_wtf_tests built cleanly.
  • CTest (nkwebkit_wtf):
    1/1 Test #4581: nkwebkit_wtf .....................   Passed    0.14 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceNKWebKit/src/WtfAssert.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4581: nkwebkit_wtf .....................***Exception: SegFault  0.18 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked WtfAssert.cpp WTFGetBacktrace for null pointer checks and guest copy safety: stack and size pointers are checked before dereferencing, and count is read via GuestCopyIn_nid_no_patch
    • Checked boundary conditions: requested <= 0 returns immediately, preserving existing negative/zero frame count behavior
    • Checked Windows CaptureStackBackTrace integration: verifies GuestWritable_nid_no_patch for requested * sizeof(void*) before capturing frames directly into stack buffer
    • Checked output copying: captured count is written back through GuestCopyOut_nid_no_patch, safely handling unmapped or read-only size pointers
    • Checked AGENTS.md compliance: no disallowed comments introduced, technical debt fragment correctly placed in docs/dev/debt/changes/, and tests verify valid, invalid size, and invalid stack pointers

Looks Good To Me

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@milescoviello this fixes the same function and the same lines as #22780, which was opened a few minutes earlier and already has a review pass and a negative-control test on its head. Both make WTFGetBacktrace read the size and write the frames through checked guest copies, so only one can merge without a rebase. #22780 also clamps the Windows frame count to the USHORT maximum and has a dedicated test file for read-only pages, INT32_MAX, null pointers and errno. I am closing this one as a duplicate to keep a single fix. If you see a case #22780 does not cover, please comment on it there.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Agreed, thanks Dean. #22780 came first and covers more (the USHORT clamp and the read-only page cases). My duplicate check ran against an open-PR list from before it was opened. I'll refresh that list right before opening a PR from now on.

@github-actions

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 22780 core/libs/prx/libSceNKWebKit/src/WtfAssert.cpp L434, L436, L438-441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants