fix(libkernel): read the process start before the clock in process time #1501
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1501"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-08T10:04:13Z.
What
sceKernelGetProcessTimeandsceKernelGetProcessTimeCountercomputedGetMonotonicNanos() - 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.guest_process_timeandguest_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::Initializestored the wrapped value and tilt correction stopped working), so #1076 now depends on this one.Tested
guest_process_timeandguest_process_time_counterpass. With theTime.cppchange reverted, both abort.guest_padpasses either way onmain.cteston Windows 11 (MinGW-w64 GCC 15.2.0, WinLibs r7) as part of #1076.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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
maine28fa01ctogether with #1501 and #1483 to share one build.guest_process_timeandguest_process_time_counter: 10 of 10 runs pass.Time.cppput back tomain, tests kept): both tests abort in 3 of 3 runs, so the race reproduces on Windows too.agc_driver_srgb8_color_target, an NVIDIA import flake (#1434).Tested with AI assistance (Claude Code).
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:
This will improve reliability of process time measurements. Ready to merge.
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.
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: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_itand 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.