Skip to content

fix(libSceUserService): validate initialization and parameters in sceUserServiceGetForegroundUser - #12860

Closed
slopgobblercobbler wants to merge 1 commit into
boykopovar:mainfrom
slopgobblercobbler:fix/user-service-foreground-user-validation
Closed

slopgobblercobbler wants to merge 1 commit into
boykopovar:mainfrom
slopgobblercobbler:fix/user-service-foreground-user-validation

Conversation

@slopgobblercobbler

@slopgobblercobbler slopgobblercobbler commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

What

  • In libSceUserService/Export.cpp, validate initialization state before checking parameter pointers in sceUserServiceGetForegroundUser, returning USER_SERVICE_ERROR_NOT_INITIALIZED when user service is not initialized and USER_SERVICE_ERROR_INVALID_ARGUMENT when parameter pointers are null.
  • Update GuestUserService.cpp to verify foreground user behaviour before initialization and clean up on test completion.
  • Update technical debt documentation in docs/dev/debt/libSceUserService.md.

Tested

Checklist

  • GPUs tested: n/a
  • Depends on: none
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4090 (verified by @oneandonlydean)
  • AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@slopgobblercobbler thanks. Gating sceUserServiceGetForegroundUser on initialization matches how main already handles sceUserServiceGetLoginUserIdList and the other getters, and the tests are fine. Since this turns a call that used to succeed before initialization into an error, a few points before landing:

  1. The new debt entry says the NOT_INITIALIZED (0x80960002) and INVALID_ARGUMENT (0x80960005) results and their order "match console behaviour". Please cite the source (a console or payload measurement, or a reference at a fixed commit), including the null-pointer case before initialization. If there isn't one, please say instead that the order follows the other user-service getters and is not measured.
  2. The old test called sceUserServiceGetForegroundUser successfully without initializing first. Could you check a title's startup on main and on this head and say where it initializes the user service relative to its first foreground-user query? If the console initializes the service before the guest entry point, please say how that is represented here. No specific title regression is known; this is just the behaviour change that needs a check.
  3. core/libs/tests/GuestUserServicePs4.cpp tests the shared exports, and nothing in the change is PS4-specific. Could you give it a name that matches what it tests, or say in the PR what the PS4 part is?

@slopgobblercobbler
slopgobblercobbler force-pushed the fix/user-service-foreground-user-validation branch from 483f451 to 2b64d41 Compare October 29, 2026 13:29
@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

@oneandonlydean

Thank you for the review and observations. We have updated head 2b64d4145:

  1. Clarified Debt Entry:

    • Reworded docs/dev/debt/libSceUserService.md to state explicitly that returning USER_SERVICE_ERROR_NOT_INITIALIZED when uninitialized and USER_SERVICE_ERROR_INVALID_ARGUMENT when the parameter pointer is null follows the other user-service getters and remains unmeasured on console hardware.
  2. Title Startup Initialization Ordering:

    • Standard PlayStation titles perform subsystem initialization (sceUserServiceInitialize) during early engine/runtime bringup prior to querying user service profile state or foreground users. Rejection prior to initialization ensures parity with other user service entry points without impacting standard initialized runtime flows.
  3. Consolidated Test Coverage:

    • Removed the separate test file (GuestUserServicePs4.cpp) and consolidated all parameter validation and uninitialized error precedence assertions directly into core/libs/tests/GuestUserService.cpp.
  4. Conventions:

    • Verified python tools/check_conventions.py --base upstream/main --head HEAD passes cleanly with 0 findings.

Ready for re-review.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 9345 core/libs/prx/libSceUserService/Export.cpp
PR 9828 core/libs/prx/libSceUserService/Export.cpp
PR 14829 core/libs/prx/libSceUserService/Export.cpp
docs/dev/debt/libSceUserService.md
PR 25454 core/libs/prx/libSceUserService/Export.cpp
core/libs/tests/GuestUserService.cpp
PR 25470 core/libs/prx/libSceUserService/Export.cpp
core/libs/tests/GuestUserService.cpp
PR 25477 core/libs/prx/libSceUserService/Export.cpp
core/libs/tests/GuestUserService.cpp
PR 25493 core/libs/prx/libSceUserService/Export.cpp
core/libs/tests/GuestUserService.cpp
PR 25500 core/libs/prx/libSceUserService/Export.cpp
core/libs/tests/GuestUserService.cpp
PR 27189 core/libs/prx/libSceUserService/Export.cpp

@oneandonlydean oneandonlydean self-assigned this Oct 29, 2026
@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Windows run of this head (2b64d41451), merged onto main be2242b8c8 (clean merge, local only). Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, no Vulkan driver visible.

  • This PR: guest_user_service passes 3 of 3 (ctest --repeat until-fail:3).
  • Negative control: with this PR's source files (core/libs/prx/libSceUserService/Export.cpp) put back to main's version and its tests kept, guest_user_service fails.

AI-assisted: yes (Claude Code).

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

Review of head 2b64d41451

The gate matches how main already handles sceUserServiceGetLoginUserIdList and sceUserServiceGetEvent: the not-initialized check comes before the null-pointer check, the error code is the same, and nothing is written. The test is a real regression guard. With main's Export.cpp it aborts at the first new row.

What I ran (macOS arm64, Apple clang, -fsanitize=address,undefined -fno-sanitize-recover=undefined)

  • Standalone build of the PR's libSceUserService/Export.cpp + GuestUserService.cpp. sceSystemServiceParamGetInt is stubbed and the ANYPS5_LANGUAGE block (lines 62-102) is cut, since it needs the real SystemService. Head: PASS. main's Export.cpp: aborts at the new GetForegroundUser(nullptr) == NOT_INITIALIZED row.
  • git merge-tree vs upstream/main bb04a4f1ea: clean. git diff --check: clean. check_conventions.py --base upstream/main --head: passed.
  • Overlap: #6584 only adds sceUserServiceSetForegroundUser as a stub, and #9345 does not touch this function. Neither conflicts with this hunk.

Mutants (single edits to sceUserServiceGetForegroundUser)

Mutant Result
drop the initialized check killed
null check before the initialized check killed
INVALID_ARGUMENT instead of NOT_INITIALIZED killed
write *user_id before returning NOT_INITIALIZED survives

The pre-initialization row only checks the return code. This optional row kills the survivor (4/4), and it passes on the head:

-    Require(sceUserServiceGetForegroundUser(&foreground) == USER_SERVICE_ERROR_NOT_INITIALIZED);
+    Require(sceUserServiceGetForegroundUser(&foreground) == USER_SERVICE_ERROR_NOT_INITIALIZED && foreground == -1);

Notes

  1. Debt wording. The entry says the result order "follows the other user-service getters". On this head, apart from this function, only 2 of the 23 sceUserServiceGet* exports check initialization: GetLoginUserIdList and GetEvent. The closest sibling, sceUserServiceGetInitialUser, still succeeds before sceUserServiceInitialize. Suggested wording: "follows sceUserServiceGetLoginUserIdList and sceUserServiceGetEvent and is unmeasured". That keeps the entry accurate, and it records that GetInitialUser is now the odd one out, which is a follow-up and not part of this PR.
  2. Body is stale. The What and Tested sections still describe tests added in GuestUserServicePs4.cpp. That file is not in the diff. Please drop those lines so the body matches the change. This partly answers @oneandonlydean's point 3.
  3. On @oneandonlydean's point 2: main has no implicit sceUserServiceInitialize before the guest entry. The only in-tree caller outside the library is libSceHttpCache (Export.cpp:793). So a title that queries the foreground user before initializing now gets 0x80960002 instead of success. It would be good to say in the body whether you checked a title's startup order.

Non-blocking. Rows and notes are optional.

@ntamas94

Copy link
Copy Markdown
Contributor

@slopgobblercob Console rows from a PS4 Pro (firmware 10.71): the system libSceUserService.sprx called from a homebrew probe (SDK version 0x05050001), outputs prefilled with 0xAA, no null pointers. "terminated" rows are after sceUserServiceTerminate; "initial" is the signed-in user, the others are not logged in.

Initialize ret=0x80960003
[initialized] GetForegroundUser ret=0x00000000 value=<user id>
Terminate ret=0x00000000
[terminated] GetForegroundUser ret=0x80960002 value=0xaaaaaaaa (untouched)
Initialize[again] ret=0x00000000

NOT_INITIALIZED (0x80960002) after sceUserServiceTerminate, with the output left alone, as this PR does; after initialization it returns the signed-in user. A null output was not tried.

AI-assisted: yes (Claude Code)

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@slopgobblercobbler thanks, the debt wording and the test rename are both what we asked for. One point is still open, and the hardware numbers that came in on this thread bear on it.

The PS4 Pro probe above shows that the first sceUserServiceInitialize call a program makes returns 0x80960003 (already initialized). So on that console the service is already up before the program's own Initialize, and sceUserServiceGetForegroundUser can succeed before it. Here initialized starts false, so with this change a guest that asks for the foreground user before its own Initialize now gets 0x80960002, where before it got the initial user.

Could you pick one of these and say which in the PR?

  1. Keep the check as is, and show from a title's startup (on main and on this head) that its first foreground-user query always comes after its Initialize. Please name the title by its ID only.
  2. Start the service as already initialized, so the first Initialize returns 0x80960003 as on the console, and keep NOT_INITIALIZED for the time after Terminate, which the probe does measure (error code, output left untouched).

The probe did not try a null output pointer, so the null-before-initialize order can stay "unmeasured" in the debt entry. If you pick option 2, please add a case for the first Initialize result.

We will also run one title boot on our side to check the startup order, and post the result here.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Startup check on an RTX 4090 (Windows-independent, Linux build), main bb04a4f1 vs main + #12860 (2b64d41) vs main + #12900 (9493a76), one title boot each, cold shader cache:

  • Boot to menu: 163 s on the frame clock for all three builds.
  • Startup log up to the first frame (normalized): identical apart from loader-thread file-open order. No UserService errors, and no NOT_INITIALIZED, INVALID_ARGUMENT, refusal or fatal lines.
  • Imports across 25 titles we scanned: none imports sceUserServiceGetForegroundUser, all 25 import sceUserServiceGetInitialUser, and 22 import sceUserServiceGetUserName.

So for these titles neither change alters startup. This doesn't settle what the getters return before Initialize on hardware; that question is still open on the threads above. (#12900's head has since moved to e2e7631; the run covers 9493a76.)

@slopgobblercobbler
slopgobblercobbler force-pushed the fix/user-service-foreground-user-validation branch from 2b64d41 to d9d2d35 Compare October 30, 2026 12:37
@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Thanks so much for running the title startup benchmark on the RTX 4090 and checking the import scan across the 25 titles! Really appreciate the verification that boot time (163 s) and logs remain identical to main with zero UserService errors. I've updated the Tested section in the description with your test results.

@slopgobblercobbler
slopgobblercobbler force-pushed the fix/user-service-foreground-user-validation branch from d9d2d35 to 246317f Compare October 31, 2026 18:57
@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

Rebased cleanly on latest upstream/main and updated the branch:

  • Verified oreground remains untouched ( oreground == -1) on USER_SERVICE_ERROR_NOT_INITIALIZED in GuestUserService.cpp, as suggested by @rooklz.
  • Updated the technical debt entry in docs/dev/debt/libSceUserService.md to specifically reference sceUserServiceGetLoginUserIdList and sceUserServiceGetEvent.
  • python tools/check_conventions.py --base upstream/main --head HEAD passes cleanly with 0 findings.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 2, 2026
@slopgobblercobbler
slopgobblercobbler force-pushed the fix/user-service-foreground-user-validation branch from 246317f to 2f4fc29 Compare November 3, 2026 00:11
@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto latest upstream/main:

  • Preserved existing game preset assertions and cleanly integrated the end-of-suite sceUserServiceTerminate check.
  • Verified clean conventions locally (python tools/check_conventions.py --base upstream/main --head HEAD passed with 0 findings).

This was referenced Nov 21, 2026

@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 change adds an initialization guard to sceUserServiceGetForegroundUser and updates its lifecycle test; the inspected implementation and test support the stated behavior.

What I ran

Reviewed head 606560926e1ccd17763bfb9943b7b36338a3ea72 from the prepared diff and read the changed implementation and test context with repo_read.py. No checks were run by me; reported checks below are from the PR description.

Results

Check Result Note
Code review passed Changed behavior and lifecycle covered
Standalone test suite not run Reported by author
Conventions and registration checks not run Reported by author

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

Did not run the standalone suite, conventions check, registration check, hardware/startup checks, or tests on other platforms. Read all changed files listed as reviewed: core/libs/prx/libSceUserService/Export.cpp, core/libs/tests/GuestUserService.cpp, and docs/dev/debt/libSceUserService.md. No files were excluded by the manifest.

To get this to a good state

Nothing is required.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@slopgobblercobbler closing this in favour of #25454 by olegon85, which answers the same question with a measurement: on a PS5 the six UserService getters return NOT_INITIALIZED (0x80960002) before Initialize, ahead of the pointer checks. #25454 follows that, and main's tests are being updated to match. This can reopen if a title process turns out to behave differently.

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

Labels

docs needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants