Skip to content

fix(libc): copy mktime's and timelocal's struct tm through checked guest copies - #22929

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/libc-mktime-guest-copies
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/libc-mktime-guest-copies

Conversation

@milescoviello

@milescoviello milescoviello commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What

mktime (libc_mktime and its mktime alias) read the caller's struct tm through the guest pointer and wrote the normalized result back through it, on both the Windows/macOS path and the common one. timelocal stored tm_isdst = -1 through it before calling mktime. A null, unmapped or read-only struct tm crashed the host.

Changes:

  • mktime copies the struct tm in with one GuestCopyIn_nid_no_patch and writes the normalized result with one GuestCopyOut_nid_no_patch on each of its two paths.
  • timelocal writes its tm_isdst = -1 with GuestCopyOut_nid_no_patch.
  • A struct tm that is null or can't be read or written returns -1 with errno set to EFAULT, as time in the same file does for an output it can't write. FreeBSD dereferences the pointer, so this is a host-safety fallback.

Results for valid pointers are unchanged.

Tested

Windows 11 VM (GeForce GT 1030, WinLibs GCC 16.2.0 POSIX UCRT, Ninja, Release), which takes the Windows path of mktime:

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

  • guest_mktime passes, 3 runs, on c69358e916 (this PR's head).
  • Negative control: the same tree with main's source file ends guest_mktime in SIGSEGV at address 0x38.
  • kiwe147's batch run of c69358e916, applied with other PRs onto main fd4aeed483 (one combined tree, not this PR alone), Windows 11, WinLibs GCC 15.2.0 r7, Release, no validation layer: guest_mktime pass on an NVIDIA GeForce RTX 3080 (driver 621.24).

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

…est copies

mktime read the caller's struct tm and wrote the normalized result
through the guest pointer, and timelocal stored tm_isdst through it, so
a null, unmapped or read-only struct tm crashed the host. The struct is
now copied in with one GuestCopyIn_nid_no_patch, and the result, like
timelocal's tm_isdst = -1, is written with GuestCopyOut_nid_no_patch. A
struct tm that is null or can't be read or written returns -1 with errno
set to EFAULT, as time does for an output it can't write.

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 c69358e916:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_mktime_tests built cleanly.
  • CTest (guest_mktime):
    1/1 Test #1088: guest_mktime .....................   Passed    0.09 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Time.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1088: guest_mktime .....................***Exception: SegFault  0.77 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libc/src/Time.cpp libc_mktime_nid_postfix correctly guards against nullptr and unmapped guest memory using GuestCopyIn_nid_no_patch and sets errno to EFAULT on failure
    • Verified both the #if defined(_WIN32) || defined(APPLE) and Linux fallback return paths write back normalized GuestTm using GuestCopyOut_nid_no_patch with EFAULT on failure
    • Verified timelocal_nid_postfix writes tm_isdst = -1 via GuestCopyOut_nid_no_patch before forwarding to libc_mktime_nid_postfix, properly returning -1 and setting errno to EFAULT if the write fails
    • Verified docs/dev/debt/changes/fix-libc-mktime-guest-copies.md follows TechnicalDebt fragment conventions and AGENTS.md rules
    • Verified test coverage in core/libs/tests/GuestMktime.cpp exercises nullptr, unmapped address 0x10, and subsequent valid normalization for libc_mktime, mktime, and timelocal

Looks Good To Me

This was referenced Nov 21, 2026
@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head c69358e916, applied with git apply on main fd4aeed483 together with the other 26 PRs of one batch (#24498, #24665, #17297, #21499, #21726, #22534, #12900, #17843, #22051, #22075, #23087, #23206, #23222, #23227, #13147, #17482, #21935, #17477, #18272, #22858, #23028, #23295, #23916, #24179, #24706, #12619). 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_mktime (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 (6525 tests) on the RTX 3080 has 8 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. Traced to other PRs of the batch, each by reverting it and testing again: #18272 (guest_audio_out), #12900 (guest_user_service_initial_user_bounds); none is in this PR's area. Failing tests I could not trace to a single PR (reverting the likely candidates did not clear them): guest_content_search. 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)

@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @kiwe147 for including this one in the batch. The guest_mktime test passing is a useful Windows data point, and I added it to the description, noted as a combined batch tree rather than this PR alone.

@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 checked guest copies protect mktime and timelocal from inaccessible struct tm pointers, and the reviewed changes show no verified blocking defect.

What I ran

Reviewed head c69358e9163b6aff40cb653ed65b8d793a7a6400. I read the prepared description, commit summary, manifest, diff, and all three changed files, then inspected the changed implementation and searched existing guest-copy usage. I did not run commands or tests; reported test results below are author-reported.

Results

Check Result Note
Changed-file review passed All three changed files read
Guest-copy call sites passed Existing uses found
Test execution not run Runs described by author only

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently run the Windows or Linux tests, reproduce the negative controls, or validate the combined-tree merge claim. No files were excluded: all files listed in the manifest were included and read.

To get this to a good state

Nothing is required.

@oneandonlydean
oneandonlydean merged commit b464859 into boykopovar:main Nov 27, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants