fix(libkernel): read the process start before the clock in process time #1501

Merged
TideOPL merged 1 commit from upstream-pr/1501 into main 2026-10-10 01:10:57 +00:00
TideOPL commented 2026-10-08 10:04:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-08T10:04:13Z.


What

sceKernelGetProcessTime and sceKernelGetProcessTimeCounter computed GetMonotonicNanos() - GetStartNanos() in one expression. The evaluation order of the two calls is unspecified, and GCC reads the clock first. GetStartNanos() captures the process start on its first call, so on the first call in a process the start was taken after the clock reading and the result wrapped to about 2^64.

  • libkernel: both functions now read the start into a local before reading the clock.
  • New tests guest_process_time and guest_process_time_counter: the first call in a fresh process must not wrap, and process time must not go backwards.

Split out of #1076 at a reviewer's request. That PR's pad queue exposed this bug (Pad::Initialize stored the wrapped value and tilt correction stopped working), so #1076 now depends on this one.

Tested

  • Ubuntu 24.04 (WSL), GCC 13.3, Release: guest_process_time and guest_process_time_counter pass. With the Time.cpp change reverted, both abort. guest_pad passes either way on main.
  • The same change and tests passed the full ctest on Windows 11 (MinGW-w64 GCC 15.2.0, WinLibs r7) as part of #1076.
  • No title or homebrew tested.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4785087954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1501). Originally posted by [TideOPL](https://github.com/TideOPL) on 2026-10-08T10:04:13Z. --- ### What `sceKernelGetProcessTime` and `sceKernelGetProcessTimeCounter` computed `GetMonotonicNanos() - GetStartNanos()` in one expression. The evaluation order of the two calls is unspecified, and GCC reads the clock first. `GetStartNanos()` captures the process start on its first call, so on the first call in a process the start was taken after the clock reading and the result wrapped to about 2^64. - **libkernel:** both functions now read the start into a local before reading the clock. - New tests `guest_process_time` and `guest_process_time_counter`: the first call in a fresh process must not wrap, and process time must not go backwards. Split out of #1076 at a reviewer's request. That PR's pad queue exposed this bug (`Pad::Initialize` stored the wrapped value and tilt correction stopped working), so #1076 now depends on this one. ### Tested - Ubuntu 24.04 (WSL), GCC 13.3, Release: `guest_process_time` and `guest_process_time_counter` pass. With the `Time.cpp` change reverted, both abort. `guest_pad` passes either way on `main`. - The same change and tests passed the full `ctest` on Windows 11 (MinGW-w64 GCC 15.2.0, WinLibs r7) as part of #1076. - No title or homebrew tested. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
popcoder565 commented 2026-10-08 10:35:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T10:35:24Z.


Tested on Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H, merged onto main e28fa01c together with #1501 and #1483 to share one build.

  • guest_process_time and guest_process_time_counter: 10 of 10 runs pass.
  • Mutation check (Time.cpp put back to main, tests kept): both tests abort in 3 of 3 runs, so the race reproduces on Windows too.
  • Full ctest on that build: 435 of 436 pass. The one failure is agc_driver_srgb8_color_target, an NVIDIA import flake (#1434).

Tested with AI assistance (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6057987982 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1501#issuecomment-6057987982). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-08T10:35:24Z. --- Tested on Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H, merged onto `main` e28fa01c together with #1501 and #1483 to share one build. - `guest_process_time` and `guest_process_time_counter`: 10 of 10 runs pass. - Mutation check (`Time.cpp` put back to `main`, tests kept): both tests abort in 3 of 3 runs, so the race reproduces on Windows too. - Full ctest on that build: 435 of 436 pass. The one failure is `agc_driver_srgb8_color_target`, an NVIDIA import flake (#1434). Tested with AI assistance (Claude Code).
klosixgaming-arch commented 2026-10-08 11:28:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:28:50Z.

Review: APPROVED


Good small fix for a potential race condition in process time functions. Caching the start time in a local variable ensures consistent results even if GetStartNanos() returns different values between calls.

Strengths:

  • Simple, focused fix for a real issue (race condition between two GetStartNanos() calls)
  • Good addition of tests for process time functions including wrap-around detection
  • Clean implementation with proper test coverage

This will improve reliability of process time measurements. Ready to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5455847119 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1501#pullrequestreview-5455847119). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-08T11:28:50Z. **Review:** APPROVED --- Good small fix for a potential race condition in process time functions. Caching the start time in a local variable ensures consistent results even if GetStartNanos() returns different values between calls. **Strengths:** - Simple, focused fix for a real issue (race condition between two GetStartNanos() calls) - Good addition of tests for process time functions including wrap-around detection - Clean implementation with proper test coverage This will improve reliability of process time measurements. Ready to merge.
Dextroid17 commented 2026-10-08 18:47:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T18:47:17Z.


Verified end to end on Linux, including the negative control, because this one is platform-independent.

  merge onto current main (2b297842)        no conflicts
  build                                     clean
  guest_process_time / guest_process_time_counter   Passed
  NEGATIVE CONTROL  only Time.cpp back to main's
      `return (GetMonotonicNanos() - GetStartNanos()) / 1000ULL;`, both new tests kept:
      0% tests passed, 2 tests failed out of 2
  restored                                  Passed

So the two tests gate the fix rather than passing beside it, which is what a change to an evaluation order needs.

The mechanism is exactly as the description says. GetStartNanos() captures a function-local static on its first call:

static std::uint64_t GetStartNanos() {
    static const std::uint64_t start = GetMonotonicNanos();
    return start;
}

so in the single expression the order of the two calls decides whether the start is captured before or after the clock is read. GCC evaluates the left operand first here, which is why the first call returned clock - start_taken_after_it and wrapped through unsigned underflow. Naming the start in a local removes the question for both counters, and the second function (sceKernelGetProcessTimeCounter) had the same shape.

No questions on this one.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6066755337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1501#issuecomment-6066755337). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T18:47:17Z. --- Verified end to end on Linux, including the negative control, because this one is platform-independent. ``` merge onto current main (2b297842) no conflicts build clean guest_process_time / guest_process_time_counter Passed NEGATIVE CONTROL only Time.cpp back to main's `return (GetMonotonicNanos() - GetStartNanos()) / 1000ULL;`, both new tests kept: 0% tests passed, 2 tests failed out of 2 restored Passed ``` So the two tests gate the fix rather than passing beside it, which is what a change to an evaluation order needs. The mechanism is exactly as the description says. `GetStartNanos()` captures a function-local static on its **first** call: ```cpp static std::uint64_t GetStartNanos() { static const std::uint64_t start = GetMonotonicNanos(); return start; } ``` so in the single expression the order of the two calls decides whether the start is captured before or after the clock is read. GCC evaluates the left operand first here, which is why the first call returned `clock - start_taken_after_it` and wrapped through unsigned underflow. Naming the start in a local removes the question for both counters, and the second function (`sceKernelGetProcessTimeCounter`) had the same shape. No questions on this one. Written with AI assistance.
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!1501
No description provided.