Skip to content

fix(libSceUsbd): return SCE_USBD_ERROR_INVALID_ARG from sceUsbdCancelTransfer for a null transfer - #21195

Open
roboterspinne wants to merge 1 commit into
boykopovar:mainfrom
roboterspinne:fix/libsceusbd-cancel-null-transfer
Open

roboterspinne wants to merge 1 commit into
boykopovar:mainfrom
roboterspinne:fix/libsceusbd-cancel-null-transfer

Conversation

@roboterspinne

@roboterspinne roboterspinne commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What

On main d4a1472 sceUsbdCancelTransfer (core/libs/prx/libSceUsbd/Export.cpp:486-493) throws std::runtime_error("sceUsbdCancelTransfer: null transfer") for a null transfer (:487), so a title that cancels a null transfer ends the host process (counter-check under Tested). The library follows FreeBSD 11.4 libusb (docs/dev/debt/libSceUsbd.md:5-6). There libusb_cancel_transfer begins with if (uxfer == NULL) return (LIBUSB_ERROR_INVALID_PARAM); (libusb10.c:1510-1511). LIBUSB_ERROR_INVALID_PARAM is -2, and ToGuestError (Export.cpp:75-79) maps it to 0x80240000 + 2 = 0x80240002, SCE_USBD_ERROR_INVALID_ARG (Export.cpp:27). The debt entry "libSceUsbd errors" (docs/dev/debt/libSceUsbd.md:6) states this rule: the null arguments FreeBSD libusb rejects return 0x80240002. It names only sceUsbdOpen and sceUsbdGetActiveConfigDescriptor as null throws, on a null device they dereference. The sibling sceUsbdSubmitTransfer already returns SCE_USBD_ERROR_INVALID_ARG for a null transfer (Export.cpp:670), as FreeBSD's libusb_submit_transfer does (libusb10.c:1447-1448), and core/libs/tests/GuestUsbd.cpp:572 asserts it. What the console returns is not measured; the debt entry "libSceUsbd transfers, kernel drivers and host access" (docs/dev/debt/libSceUsbd.md:7) records the error codes as unverified on PS5.

  • Rule. sceUsbdCancelTransfer(nullptr) returns SCE_USBD_ERROR_INVALID_ARG (0x80240002) instead of throwing. One line changes (Export.cpp:487). A non-null transfer that is not in flight still returns SCE_USBD_ERROR_NOT_FOUND (0x80240005, :491), and an in-flight transfer still goes through ToGuestError(libusb_cancel_transfer(...)) (:492). FreeBSD's uxfer->status = LIBUSB_TRANSFER_CANCELLED write for a transfer that is not pending (libusb10.c:1533-1536) is a separate topic and not part of this change.
  • Debt. No fragment. The entry "libSceUsbd errors" (docs/dev/debt/libSceUsbd.md:6) already states the rule this change follows, and no entry mentions a throw for a null transfer (tools/debt_fold.py --show libSceUsbd, pending fragments applied, has no match for "null transfer").
  • Tests. core/libs/tests/GuestUsbd.cpp, in main right after the sceUsbdSubmitTransfer(nullptr) check (:572): Require(sceUsbdCancelTransfer(nullptr) == invalidArgument, "null transfer cancelled"), with invalidArgument defined as 0x80240002 at :24. ctest guest_usbd; no other test name matches ^guest_usbd$, and no CMake change. main reaches the line without a USB device: after sceUsbdInit (:472) there is no return, argc branch or #if before it, and initialization keeps an empty device list when the host has no USB access (docs/dev/debt/libSceUsbd.md:7). The existing cancel checks (:359, :362, :384, :396, :571) pass a non-null transfer and are unchanged.
  • Linux. guest_usbd is registered under if(BUILD_TESTING) with no host condition (core/libs/prx/libSceUsbd/CMakeLists.txt:9-18); only its PATH setting is under WIN32, and libSceUsbd links the 3rdparty/libusb-cmake submodule on every host. Without the change the throw is the first statement of sceUsbdCancelTransfer, before any lock or libusb call, so it does not depend on the host. Nothing catches it and Require is not wrapped in Throws there, so the test terminates with the uncaught std::runtime_error on Linux and macOS as it does on Windows. Nothing was run on Linux here.
  • Overlap. pr_overlap.py --names sceUsbdCancelTransfer finds no open pull request that implements it. pr_overlap.py --branch reports no shared lines or exports with the open pull requests it can compare (main d4a1472); it cannot compare fix(libSceUsbd): retain devices during active USB operations #9803 (Dellz36), which does not merge with main. Checked by hand: fix(libSceUsbd): retain devices during active USB operations #9803 leaves sceUsbdCancelTransfer as its own base has it, and a trial merge of fix(libSceUsbd): retain devices during active USB operations #9803 with this branch conflicts in the same two files with the same number of regions (16 in Export.cpp, 11 in GuestUsbd.cpp) as a trial merge with main d4a1472.

Tested

Windows 11, WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON, build directory configured through its 8.3 short path, on d4a1472. Built with cmake --build <build> --parallel 4 --target guest_usbd_tests; each of the four builds below exited 0. Before every run each core/libs/libs/unpatched/*.prx that also exists in <build>/tests (libSceUsbd.prx, libc.prx, libkernel.prx) was copied there with cmake -E copy_if_different, and the md5sum of libSceUsbd.prx in both places matched each time. ctest --test-dir <build> -R '^guest_usbd$' --show-only=json-v1 lists one test, guest_usbd (tests/guest_usbd_tests.exe); -R 'usbd' lists no other test, and no other CMake file links libSceUsbd.

  • Counter-check on unchanged code (Export.cpp of d4a1472, new test line in place): ctest --test-dir <build> -R '^guest_usbd$' --output-on-failure fails with Exit code 0xc0000409; the output shows terminate called after throwing an instance of 'std::runtime_error' and what(): sceUsbdCancelTransfer: null transfer.
  • With the change: the same command passes 1 of 1.
  • Negative control at the end: with only the Export.cpp line put back to d4a1472 and guest_usbd_tests rebuilt, the test failed again with Exit code 0xc0000409 and what(): sceUsbdCancelTransfer: null transfer. With the change restored and rebuilt, it passed 1 of 1.
  • Not run here: Linux and macOS (see the Linux bullet above).
  • python tools/check_conventions.py --base upstream/main --head HEAD with this title and description passed.

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 (Claude Code)

…Transfer for a null transfer

sceUsbdCancelTransfer threw std::runtime_error for a null transfer, so
a title that cancelled one ended the process. FreeBSD 11.4
libusb_cancel_transfer returns LIBUSB_ERROR_INVALID_PARAM for it, which
ToGuestError maps to 0x80240002, and the libSceUsbd errors debt entry
states that rule. The call now returns SCE_USBD_ERROR_INVALID_ARG, as
sceUsbdSubmitTransfer already does; GuestUsbd.cpp checks it.
@github-actions github-actions Bot added the prx label Nov 16, 2026

@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 c69337c980:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_usbd_tests built cleanly.
  • CTest (guest_usbd):
    1/1 Test #4773: guest_usbd .......................   Passed    2.34 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceUsbd/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4773: guest_usbd .......................Subprocess aborted***Exception:   2.73 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libSceUsbd/Export.cpp:486-487 checks transfer == nullptr before acquiring state.mutex or accessing state.inFlight, returning SCE_USBD_ERROR_INVALID_ARG (0x80240002).
    • Confirmed error code mapping matches FreeBSD libusb_cancel_transfer returning LIBUSB_ERROR_INVALID_PARAM (-2), which maps via ToGuestError (Export.cpp:75-79) to 0x80240002 (SCE_USBD_ERROR_INVALID_ARG, defined at Export.cpp:27).
    • Checked concurrency and lock lifetimes in sceUsbdCancelTransfer: the early return happens prior to std::lock_guard lock(state.mutex), so no lock is held or leaked and no shared state is mutated.
    • Verified core/libs/tests/GuestUsbd.cpp:573 directly exercises sceUsbdCancelTransfer(nullptr) and asserts it returns invalidArgument (0x80240002) without leaving dirty state.
    • Checked compliance with AGENTS.md and docs/dev/debt/libSceUsbd.md: no code comments added, no title-specific special cases, and the behavior aligns with the documented libSceUsbd error rule for null arguments rejected by FreeBSD libusb.

Looks Good To Me

@MarcZX10 MarcZX10 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.

Reviewed — looks good

The null-transfer check now returns the established invalid-argument error, and the added test covers that behavior.

What I ran

Reviewed head c69337c980fd316e065ecf3455653e0b4f911c99. No commands or tests were run; the author reports Windows 11 test results in the PR description.

Results

Check Result Note
Changed code review passed Both changed files read
guest_usbd test not run Windows result reported by author
Linux and macOS not run Not verified here

Findings

Blocking

None.

Non-blocking

None.

Not verified

I read core/libs/prx/libSceUsbd/Export.cpp and core/libs/tests/GuestUsbd.cpp; both files listed as reviewed were included in the prepared review folder. I did not run tests or verify the author's reported results, Linux/macOS behavior, or console behavior.

To get this to a good state

Nothing is required.

This was referenced Nov 21, 2026
@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head c69337c980, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#17478, #21223, #24030, #24201, #14202, #18889, #19531, #21641, #21773, #22468, #22471, #23239, #23255, #23484, #23869, #23992, #24311, #18425, #21255, #22989, #23507, #23886, #14582, #17878, #19666, #19899, #20138, #21656, #21746). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_usbd (1 test, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6522 tests) on the RTX 3080 has 4 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked. The 2125 agc_/video tests on the iGPU have 2 failing: agc_driver_stencil_plane_memory, agc_driver_store_over_untracked (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I ran guest_usbd with the patch applied on Linux. The full fixture passed, including the regression check for the invalid-argument result from a null cancel-transfer request.

AI-assisted: yes.

@Plyxen Plyxen 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.

I read the diff at c69337c980. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at c1593e4e1dd together with other open PRs (so not an isolated build).

  • guest_usbd: pass on both.
  • Control (the same tree and tests, with the sources of this PR and of the other CPU PRs in the batch put back to main's): it fails with "sceUsbdCancelTransfer: null transfer".

Not checked: the values against a console.

AI-assisted: yes (Claude Code)

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants