fix(libkernel): build the file, module and rtld calls on macOS hosts #10552

Merged
krishalivaibhav merged 20 commits from upstream-pr/10552 into main 2026-11-02 07:54:14 +00:00
krishalivaibhav commented 2026-10-24 09:43:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-24T09:43:15Z.


What

Builds libkernel on macOS hosts. Six files used Linux-only APIs and did not compile there; each now has a macOS path behind __APPLE__, and the Linux and Windows paths are unchanged:

  • File/src/NativeStat.cpp: st_atimespec/st_mtimespec/st_ctimespec, and a fstatat walk for guest paths that no longer fit PATH_MAX once the host root is prepended.
  • File/src/SharedMemory.cpp: an unlinked temp file instead of memfd_create; descriptors are recognised through F_GETPATH.
  • File/src/Stdio.cpp: statfs from sys/mount.h instead of sys/vfs.h, plus the directory-read and vector-IO differences of Darwin.
  • Module/src/DynamicLoader.cpp, Module/src/ModuleInfo.cpp, System/src/Rtld.cpp: dlopen/dladdr handles and Mach-O segment walks instead of link.h/dl_iterate_phdr.

A second commit makes File/src/DirectoryDescriptor.cpp resolve a descriptor's path through fcntl(F_GETPATH) on macOS, where /proc/self/fd does not exist (fdopendir returned ENOTDIR).

A third commit (rebased f023a5e691) gives guest_module_info a macOS expectation: eh_frame must match the image's __eh_frame section from getsectiondata, and eh_frame_hdr is 0 for the fixture, which is not a relinked image.

Extracted from iamthehimansh's feat/macos-port (#8658); Co-authored-by on both commits. The 22 explanatory // comments of the branch version were dropped to follow the no-comments rule.

Further commits:

  • ee34c291e1 (was 997f6cfd4, iamthehimansh): long-path stat prefixes are opened with O_SEARCH on macOS, so a directory without read permission can still be walked.
  • 559568f8d8 (was 458b30a05): sceKernelGetdirentries and HostWrite guard their guest outputs on macOS, with the S_ISDIR check the Linux path has.
  • 74cfbed70d: Rtld.cpp imageContaining returns nullptr on macOS for an address outside every image, as main does on Linux and Windows after #10243, instead of throwing.
  • 5200c2edd2: named shm_open and named shm_unlink call NotImplemented_nid_no_patch on macOS, as docs/dev/debt/libkernel.md says. The host name /anyps5-shm- plus 40 hex digits is 52 bytes against macOS PSHMNAMLEN 31, and /dev/shm does not exist there. Anonymous shared memory is unchanged.
  • a6d91c88dd: a debt entry for the macOS module info fields left at 0 (tls_*, init_proc_addr, fini_proc_addr, eh_frame_hdr_*) and for the non-empty Mach-O __eh_frame case the guest_module_info fixture does not cover.
  • e95186ba55: the macOS NativeIsPipe matches only FIFOs whose fstatfs fails, as pipe() descriptors do, and NativeFstatfs uses it, so a named FIFO is no longer a pipe.
  • fa9d3c8088: a zero-length readv, writev, preadv or pwritev on macOS checks the descriptor, then its access mode, then (for the positioned calls) that it is seekable, before returning 0. This is the FreeBSD 11.4 order; Linux checks seekability first, so a write-only pipe end gives ESPIPE there and EBADF here.
  • 787e08bb5c: an unlinked anyps5-shm- file counts as shared memory only in the temporary directory CreateAnonymousFile uses; the not-implemented traces name shm_open and shm_unlink.
  • 1490561626: debt entries for the macOS sceKernelGetdirentries (full re-read, O(n²) paging, index offsets) and for macOS opening every module with RTLD_GLOBAL.
  • f82257e8f9: dlclose on macOS unregisters a relinked image under the __APS5DATA,__meta load bias that dl_iterate_phdr reports and mprotect registers it under, not under its Mach-O header. The header and the load bias are found once when the module is opened, so ImageBase and its throw are gone.
  • f374a777a2: the macOS sceKernelGetdirentries reads an archived app directory through File::ReadDirectoryDescriptor, as the Linux copy does since 0a64d23bd; the HostWrite checks now come before fstat, as there.
  • f58b1c4523: the shared-memory check resolves the temporary directory once.
  • ce09c08f44: on macOS a lookup on the main program's handle (sceKernelDlsym with handle 0) goes through the default-scope path, because dlopen(NULL) there is equivalent to RTLD_DEFAULT; a module loaded without 0x100 is no longer found through it. DefaultScopeSymbol returns null for a hit in such a module, and the main program never counts as one.
  • 5afd6df8cc: sceKernelGetModuleInfoFromAddr on macOS reports eh_frame_hdr for a relinked image from the PT_GNU_EH_FRAME header in __APS5DATA,__meta, the source sceKernelGetModuleInfoForUnwind uses. FindImage requires the same 0x60-byte __meta minimum as dl_iterate_phdr.
  • 32d288095f: after a long-path split, leading slashes of the rest are dropped, so a // at the split point is not resolved from the host root.
  • 3dffebe9ec: removes the duplicate ftruncate and readv entries the rebase added, describes the handle-0 lookup in the module scope entry, adds the entries skipped or repeated when a directory changes between getdirentries calls to the macOS directory entry, and the eh_frame_hdr source to the module info entry.

Tested

Rebased onto main 809c7fa2df (one conflict, in the include block of DynamicLoader.cpp after #10627; resolved by keeping main's HostThreadLocal.hpp include), macOS x86_64 Release on Apple Silicon M2:

  • the seven changed libkernel files (DirectoryDescriptor, NativeStat, SharedMemory, Stdio, DynamicLoader, ModuleInfo, Rtld) compile with 0 errors;
  • Stdio.cpp, SharedMemory.cpp and DynamicLoader.cpp compile with 0 errors at ea0213b6ac, and all seven again at this head, with no warnings from their lines; a standalone x86_64 program with the new empty-vector, shared-memory-directory and pipe checks gives the expected result in 12 cases (EBADF, ESPIPE, 0; temporary directory matches, another directory does not; pipe() is a pipe, a named FIFO is not);
  • a standalone x86_64 program linked against the Rtld.cpp and SharedMemory.cpp objects (other symbols stubbed) checks that _sceKernelRtldThreadAtexitIncrement returns -1 and Decrement returns 0 for &local and nullptr, and that named shm_open and shm_unlink on a valid name reach NotImplemented_nid_no_patch. It prints ok.

Not run on the rebased head: the guest_* test programs. #10366 is merged, but on current main libkernel still does not build on macOS without #7631, #7804 and #4630 (Thread.cpp, Exception.cpp, DirectMemory.cpp), so guest_module_info, guest_filesystem, guest_dynamic_loader and the other tests below were not re-run.

Earlier results on the pre-rebase head (macOS: local integration of main (e7e3e9c19) + #10419 + #10366 + #10369 + #7631 + #7804 + #4630 + this PR + #10853): guest_module_info, guest_module_args, guest_libc_threads, guest_statvfs, guest_file_timestamps, guest_pthread_stack, guest_raise_exception, guest_fdopendir, guest_kernel_sys, guest_filesystem passed; guest_aio needs #10543 and guest_dynamic_loader needs #10627.

Linux: Ubuntu 24.04, g++ 13.3.0, x86-64 Release, on main 60465afd3 + the first commit: libkernel builds with no warnings from the six files. The later commits are __APPLE__-only.

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 under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none to compile; this merges before #12682, #12688 and #12700 (maintainer's call), which then keep both sides of two small Stdio.cpp hunks; the rest of libkernel on macOS needs #7631, #7804 and #4630
  • Fixes: none
  • Unblocks: #8658 split (libkernel on macOS)
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4994327175 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-10-24T09:43:15Z. --- ### What Builds `libkernel` on macOS hosts. Six files used Linux-only APIs and did not compile there; each now has a macOS path behind `__APPLE__`, and the Linux and Windows paths are unchanged: - `File/src/NativeStat.cpp`: `st_atimespec`/`st_mtimespec`/`st_ctimespec`, and a `fstatat` walk for guest paths that no longer fit `PATH_MAX` once the host root is prepended. - `File/src/SharedMemory.cpp`: an unlinked temp file instead of `memfd_create`; descriptors are recognised through `F_GETPATH`. - `File/src/Stdio.cpp`: `statfs` from `sys/mount.h` instead of `sys/vfs.h`, plus the directory-read and vector-IO differences of Darwin. - `Module/src/DynamicLoader.cpp`, `Module/src/ModuleInfo.cpp`, `System/src/Rtld.cpp`: `dlopen`/`dladdr` handles and Mach-O segment walks instead of `link.h`/`dl_iterate_phdr`. A second commit makes `File/src/DirectoryDescriptor.cpp` resolve a descriptor's path through `fcntl(F_GETPATH)` on macOS, where `/proc/self/fd` does not exist (`fdopendir` returned ENOTDIR). A third commit (rebased `f023a5e691`) gives `guest_module_info` a macOS expectation: `eh_frame` must match the image's `__eh_frame` section from `getsectiondata`, and `eh_frame_hdr` is 0 for the fixture, which is not a relinked image. Extracted from iamthehimansh's `feat/macos-port` (#8658); Co-authored-by on both commits. The 22 explanatory `//` comments of the branch version were dropped to follow the no-comments rule. Further commits: - `ee34c291e1` (was `997f6cfd4`, iamthehimansh): long-path stat prefixes are opened with `O_SEARCH` on macOS, so a directory without read permission can still be walked. - `559568f8d8` (was `458b30a05`): `sceKernelGetdirentries` and `HostWrite` guard their guest outputs on macOS, with the `S_ISDIR` check the Linux path has. - `74cfbed70d`: `Rtld.cpp` `imageContaining` returns `nullptr` on macOS for an address outside every image, as main does on Linux and Windows after #10243, instead of throwing. - `5200c2edd2`: named `shm_open` and named `shm_unlink` call `NotImplemented_nid_no_patch` on macOS, as `docs/dev/debt/libkernel.md` says. The host name `/anyps5-shm-` plus 40 hex digits is 52 bytes against macOS `PSHMNAMLEN` 31, and `/dev/shm` does not exist there. Anonymous shared memory is unchanged. - `a6d91c88dd`: a debt entry for the macOS module info fields left at 0 (`tls_*`, `init_proc_addr`, `fini_proc_addr`, `eh_frame_hdr_*`) and for the non-empty Mach-O `__eh_frame` case the `guest_module_info` fixture does not cover. - `e95186ba55`: the macOS `NativeIsPipe` matches only FIFOs whose `fstatfs` fails, as `pipe()` descriptors do, and `NativeFstatfs` uses it, so a named FIFO is no longer a pipe. - `fa9d3c8088`: a zero-length `readv`, `writev`, `preadv` or `pwritev` on macOS checks the descriptor, then its access mode, then (for the positioned calls) that it is seekable, before returning 0. This is the FreeBSD 11.4 order; Linux checks seekability first, so a write-only pipe end gives `ESPIPE` there and `EBADF` here. - `787e08bb5c`: an unlinked `anyps5-shm-` file counts as shared memory only in the temporary directory `CreateAnonymousFile` uses; the not-implemented traces name `shm_open` and `shm_unlink`. - `1490561626`: debt entries for the macOS `sceKernelGetdirentries` (full re-read, O(n²) paging, index offsets) and for macOS opening every module with `RTLD_GLOBAL`. - `f82257e8f9`: `dlclose` on macOS unregisters a relinked image under the `__APS5DATA,__meta` load bias that `dl_iterate_phdr` reports and `mprotect` registers it under, not under its Mach-O header. The header and the load bias are found once when the module is opened, so `ImageBase` and its throw are gone. - `f374a777a2`: the macOS `sceKernelGetdirentries` reads an archived app directory through `File::ReadDirectoryDescriptor`, as the Linux copy does since `0a64d23bd`; the `HostWrite` checks now come before `fstat`, as there. - `f58b1c4523`: the shared-memory check resolves the temporary directory once. - `ce09c08f44`: on macOS a lookup on the main program's handle (`sceKernelDlsym` with handle 0) goes through the default-scope path, because `dlopen(NULL)` there is equivalent to `RTLD_DEFAULT`; a module loaded without 0x100 is no longer found through it. `DefaultScopeSymbol` returns null for a hit in such a module, and the main program never counts as one. - `5afd6df8cc`: `sceKernelGetModuleInfoFromAddr` on macOS reports `eh_frame_hdr` for a relinked image from the `PT_GNU_EH_FRAME` header in `__APS5DATA,__meta`, the source `sceKernelGetModuleInfoForUnwind` uses. `FindImage` requires the same 0x60-byte `__meta` minimum as `dl_iterate_phdr`. - `32d288095f`: after a long-path split, leading slashes of the rest are dropped, so a `//` at the split point is not resolved from the host root. - `3dffebe9ec`: removes the duplicate `ftruncate` and `readv` entries the rebase added, describes the handle-0 lookup in the module scope entry, adds the entries skipped or repeated when a directory changes between `getdirentries` calls to the macOS directory entry, and the `eh_frame_hdr` source to the module info entry. ### Tested Rebased onto `main` 809c7fa2df (one conflict, in the include block of `DynamicLoader.cpp` after #10627; resolved by keeping main's `HostThreadLocal.hpp` include), macOS x86_64 Release on Apple Silicon M2: - the seven changed libkernel files (`DirectoryDescriptor`, `NativeStat`, `SharedMemory`, `Stdio`, `DynamicLoader`, `ModuleInfo`, `Rtld`) compile with 0 errors; - `Stdio.cpp`, `SharedMemory.cpp` and `DynamicLoader.cpp` compile with 0 errors at `ea0213b6ac`, and all seven again at this head, with no warnings from their lines; a standalone x86_64 program with the new empty-vector, shared-memory-directory and pipe checks gives the expected result in 12 cases (`EBADF`, `ESPIPE`, 0; temporary directory matches, another directory does not; `pipe()` is a pipe, a named FIFO is not); - a standalone x86_64 program linked against the `Rtld.cpp` and `SharedMemory.cpp` objects (other symbols stubbed) checks that `_sceKernelRtldThreadAtexitIncrement` returns -1 and `Decrement` returns 0 for `&local` and `nullptr`, and that named `shm_open` and `shm_unlink` on a valid name reach `NotImplemented_nid_no_patch`. It prints `ok`. Not run on the rebased head: the `guest_*` test programs. #10366 is merged, but on current `main` libkernel still does not build on macOS without #7631, #7804 and #4630 (`Thread.cpp`, `Exception.cpp`, `DirectMemory.cpp`), so `guest_module_info`, `guest_filesystem`, `guest_dynamic_loader` and the other tests below were not re-run. Earlier results on the pre-rebase head (macOS: local integration of `main` (e7e3e9c19) + #10419 + #10366 + #10369 + #7631 + #7804 + #4630 + this PR + #10853): `guest_module_info`, `guest_module_args`, `guest_libc_threads`, `guest_statvfs`, `guest_file_timestamps`, `guest_pthread_stack`, `guest_raise_exception`, `guest_fdopendir`, `guest_kernel_sys`, `guest_filesystem` passed; `guest_aio` needs #10543 and `guest_dynamic_loader` needs #10627. Linux: Ubuntu 24.04, g++ 13.3.0, x86-64 Release, on `main` 60465afd3 + the first commit: libkernel builds with no warnings from the six files. The later commits are `__APPLE__`-only. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none to compile; this merges before #12682, #12688 and #12700 (maintainer's call), which then keep both sides of two small `Stdio.cpp` hunks; the rest of libkernel on macOS needs #7631, #7804 and #4630 - [x] Fixes: none - [x] Unblocks: #8658 split (libkernel on macOS) - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-24 10:36:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T10:36:43Z.

Review: CHANGES_REQUESTED


The macOS AppendingWritev fallback has a correctness hole and overlaps directly with the helper in open PR #10543. Here it treats any descriptor with O_APPEND as an appendable regular file and calls writev even when the saved lseek position is negative. For example, an O_APPEND pipe/non-regular descriptor can be written successfully here although pwritev should fail with ESPIPE; if saving the offset fails, the code also performs the write without being able to restore the shared descriptor position. #10543’s WritevAtEnd checks fstat/S_ISREG, refuses the fallback when the offset cannot be saved, and covers the same pwrite/pwritev behavior. Please coordinate this shared path with #10543 (or make this PR depend on/reuse its helper) and preserve the native failure behavior for non-regular or non-seekable descriptors. A regression for an O_APPEND pipe and a regular-file offset-preservation case would cover the boundary.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637946357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5637946357). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T10:36:43Z. **Review:** CHANGES_REQUESTED --- The macOS `AppendingWritev` fallback has a correctness hole and overlaps directly with the helper in open PR #10543. Here it treats any descriptor with `O_APPEND` as an appendable regular file and calls `writev` even when the saved `lseek` position is negative. For example, an `O_APPEND` pipe/non-regular descriptor can be written successfully here although `pwritev` should fail with `ESPIPE`; if saving the offset fails, the code also performs the write without being able to restore the shared descriptor position. #10543’s `WritevAtEnd` checks `fstat`/`S_ISREG`, refuses the fallback when the offset cannot be saved, and covers the same pwrite/pwritev behavior. Please coordinate this shared path with #10543 (or make this PR depend on/reuse its helper) and preserve the native failure behavior for non-regular or non-seekable descriptors. A regression for an `O_APPEND` pipe and a regular-file offset-preservation case would cover the boundary.
imellag commented 2026-10-24 16:16:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-24T16:16:17Z.

Review: COMMENTED


Tested head 972a2c6525 on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2. Two points for the rebase.

Conflict in ModuleInfo.cpp. On main 3fcb5e4d76, NativeStat.cpp, SharedMemory.cpp, Stdio.cpp, DynamicLoader.cpp and Rtld.cpp compile with this PR, but ModuleInfo.cpp conflicts. #10230 rewrote sceKernelGetModuleInfoFromAddr:

  • it accepts flags 1 and 2;
  • it leaves st_size 0;
  • it builds a GuestImage on every host for the flags == 2 SystemImage check.

The macOS branch needs the same. Locally I used this, taking the image header from dladdr, which is what the Apple SystemImage reads:

#elif defined(__APPLE__)
    Dl_info symbol{};
    const bool found = Fill(static_cast<std::uintptr_t>(address), result) &&
        ::dladdr(reinterpret_cast<const void*>(address), &symbol) && symbol.dli_fbase != nullptr;
    const GuestImage image{nullptr, found ? reinterpret_cast<std::uintptr_t>(symbol.dli_fbase) : 0};

Test results. With that and the other open macOS build PRs (the same stack as in my review of #7631), guest_dynamic_loader, guest_module_args, guest_libc_threads, guest_statvfs and guest_file_timestamps pass.

guest_module_info aborts at GuestModuleInfo.cpp:68 (info.eh_frame_addr != 0). Apple ld gives the fixture only compact unwind information: otool -l libguest_module_fixture.dylib lists __text and __unwind_info, with no __eh_frame. So the Mach-O branch of Fill leaves the eh_frame fields 0. The eh_frame_hdr check on the next lines can't hold for Mach-O either.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638920967 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5638920967). Originally posted by [imellag](https://github.com/imellag) on 2026-10-24T16:16:17Z. **Review:** COMMENTED --- Tested head `972a2c6525` on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2. Two points for the rebase. **Conflict in `ModuleInfo.cpp`.** On `main` `3fcb5e4d76`, `NativeStat.cpp`, `SharedMemory.cpp`, `Stdio.cpp`, `DynamicLoader.cpp` and `Rtld.cpp` compile with this PR, but `ModuleInfo.cpp` conflicts. #10230 rewrote `sceKernelGetModuleInfoFromAddr`: - it accepts flags 1 and 2; - it leaves `st_size` 0; - it builds a `GuestImage` on every host for the `flags == 2` `SystemImage` check. The macOS branch needs the same. Locally I used this, taking the image header from `dladdr`, which is what the Apple `SystemImage` reads: ```cpp #elif defined(__APPLE__) Dl_info symbol{}; const bool found = Fill(static_cast<std::uintptr_t>(address), result) && ::dladdr(reinterpret_cast<const void*>(address), &symbol) && symbol.dli_fbase != nullptr; const GuestImage image{nullptr, found ? reinterpret_cast<std::uintptr_t>(symbol.dli_fbase) : 0}; ``` **Test results.** With that and the other open macOS build PRs (the same stack as in my review of #7631), `guest_dynamic_loader`, `guest_module_args`, `guest_libc_threads`, `guest_statvfs` and `guest_file_timestamps` pass. `guest_module_info` aborts at `GuestModuleInfo.cpp:68` (`info.eh_frame_addr != 0`). Apple ld gives the fixture only compact unwind information: `otool -l libguest_module_fixture.dylib` lists `__text` and `__unwind_info`, with no `__eh_frame`. So the Mach-O branch of `Fill` leaves the `eh_frame` fields 0. The `eh_frame_hdr` check on the next lines can't hold for Mach-O either. AI-assisted: yes
MarcZX10 commented 2026-10-24 19:51:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T19:51:50Z.

Review: DISMISSED


Re-reviewed exact head e60d9dfa3a911ebdc0847f229eebae1878e4e487. The earlier O_APPEND fallback concern is resolved by removing that path, and the F_GETPATH addition matches the macOS descriptor-path need. I cannot approve this head yet for two concrete reasons:

  • GitHub currently reports the branch as DIRTY against main; please rebase and rerun checks.
  • The existing cross-platform guest_module_info test still requires nonzero eh_frame and eh_frame_hdr for every non-Windows host. The reported macOS fixture has only __unwind_info, and the new Mach-O Fill path leaves both fields zero. The author’s own test note identifies this mismatch. Please add an explicit macOS expectation/fixture for these fields (or otherwise make the test pass with documented semantics) and run guest_module_info on macOS.

The hosted product jobs are skipped by path policy, so they do not cover this macOS behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639706799 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5639706799). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T19:51:50Z. **Review:** DISMISSED --- Re-reviewed exact head `e60d9dfa3a911ebdc0847f229eebae1878e4e487`. The earlier O_APPEND fallback concern is resolved by removing that path, and the `F_GETPATH` addition matches the macOS descriptor-path need. I cannot approve this head yet for two concrete reasons: - GitHub currently reports the branch as `DIRTY` against `main`; please rebase and rerun checks. - The existing cross-platform `guest_module_info` test still requires nonzero `eh_frame` and `eh_frame_hdr` for every non-Windows host. The reported macOS fixture has only `__unwind_info`, and the new Mach-O `Fill` path leaves both fields zero. The author’s own test note identifies this mismatch. Please add an explicit macOS expectation/fixture for these fields (or otherwise make the test pass with documented semantics) and run `guest_module_info` on macOS. The hosted product jobs are skipped by path policy, so they do not cover this macOS behavior.
maximilianfv commented 2026-10-26 06:51:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-26T06:51:58Z.

Review: CHANGES_REQUESTED


Re-review of 458b30a05. The two new commits look right and pass here, but I am requesting changes for two defects. The first appears once this head is merged with current main. The second is in the named shm_open path that this PR makes reachable on macOS. My 25 Oct report at 3206fbd27 said the second one was a problem in the series' test. That was wrong: it comes from this PR, as item 2 explains.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Applied at 458b30a05 to mugurc's macOS series (27e968974) merged with main at 2742004ed2, with other open macOS fixes, then toggled only this PR. As in my last report, git apply conflicts with the series' own macOS versions of these files. "With" therefore means the seven source files and GuestModuleInfo.cpp replaced by this head's versions. The series turns guest_module_info off on Apple, so I turned it back on for both states. Both states build with 0 errors, and the touched files give no warnings with this PR.

I ran 44 tests three times in each state, sequentially, and got the same result every run. The set is the one from my last report, plus guest_directory_entries, guest_underscore_entries, guest_path_name_length, guest_usb_storage, guest_kernel_app_archive_* and write_watch_*, which exercise the getdirentries and HostWrite changes:

guest_filesystem guest_kernel_sys_module_internal guest_module_info guest_shm_open (the series' copy)
Without this PR Fails (stopped at GuestFilesystem.cpp:1245 last time: getdents on a plain file must return EINVAL) Fails (stopped at :19 last time) module info check failed at line 34 Passed
With this PR Passed Passed Passed Aborts at GuestShmOpen.cpp:72, located with lldb: it expects a named shm_open to throw on macOS, and it no longer does. See item 2

The other 40 tests give the same result in both states. 34 pass both ways: the other guest_kernel_sys* tests, guest_libc_threads, guest_statvfs, guest_fdopendir, guest_file_timestamps, guest_pthread_stack, guest_raise_exception, guest_module_args, guest_dynamic_loader_variant_names, guest_directory_entries, guest_underscore_entries, guest_path_name_length, guest_usb_storage, guest_kernel_app_archive_archive/_plain and all write_watch_*. 6 fail both ways at the same point: guest_aio (:437, needs #10543), guest_dynamic_loader (:57, needs #10627), guest_append_writes (:79), guest_host_standard_error, guest_kernel_app_archive_mmap (mmap on a file read from app0.zar ... not implemented) and guest_refused_connect.

New commits: a standalone x86_64 check on this Mac agrees with 997f6cfd4. On a mode-0100 directory, openat(O_RDONLY | O_DIRECTORY) fails with EACCES, while openat(O_SEARCH) succeeds and fstatat through that descriptor works. O_SEARCH on a regular file fails with ENOTDIR. The guard block added in 458b30a05 matches the Linux sceKernelGetdirentries line for line.

Findings

  1. core/libs/prx/libkernel/System/src/Rtld.cpp:50: the macOS branch throws, but main now returns -1 or 0. #10243 (0ce4b82c2d, merged after this PR's base e7e3e9c19) made imageContaining return nullptr on Linux and Windows for an address outside every image. With that, _sceKernelRtldThreadAtexitIncrement returns -1 and Decrement returns 0, and main's GuestDynamicLoader.cpp:274-277 requires both for &local and nullptr. Git merges this head into current main without a conflict (git merge-tree), and the merged imageContaining keeps the __APPLE__ throw. On this Mac dladdr(&local) and dladdr(nullptr) both return 0, so after the merge both calls throw std::runtime_error out of an extern "C" export on macOS. Fix: rebase on current main and replace the throw in the __APPLE__ branch with return nullptr;. (I could not run this check end to end, because guest_dynamic_loader stops at :57 first without #10627.)

  2. core/libs/prx/libkernel/File/src/SharedMemory.cpp:612 and :1026: a valid named shm_open returns a made-up ENAMETOOLONG on macOS instead of throwing. Named objects now go through the shared non-Windows path. That path calls host ::shm_open("/anyps5-shm-" + 40 hex digits), a 52-byte name, and macOS limits POSIX shm names to PSHMNAMLEN (31). Checked here: shm_open with that name fails with errno 63, "File name too long", so every valid guest name gets error 63. HostShmLock is Linux-only, so macOS has no namespace lock. shm_unlink checks /dev/shm/... with faccessat, which does not exist on macOS, so it always returns ENOENT. Even with a short name, the Apple IsHostSharedMemoryDescriptor (:125-132) would not recognise a native POSIX shm descriptor. docs/dev/debt/libkernel.md:264 says "Named shm_open objects still throw on macOS". The series' GuestShmOpen.cpp:72 asserts that throw, and with this PR the assertion fails. main's GuestShmOpen.cpp tests only invalid names, so it does not catch this. Fix: under __APPLE__, call NotImplemented_nid_no_patch in OpenNamedFile and in the named shm_unlink, as the debt entry describes.

  3. core/libs/tests/GuestModuleInfo.cpp:63-64: on this fixture the new macOS expectation checks nothing. otool -l libguest_module_fixture.dylib here lists only __text and __unwind_info. getsectiondata therefore returns nullptr and 0, and the check reduces to 0 == 0 && 0 == 0. A regression that stops reporting __eh_frame would still pass. Either check an image that has an __eh_frame section and require frames != nullptr, or record in the debt file that the Mach-O fields are not covered. A debt entry is also needed for the other macOS Fill fields this PR leaves at 0 (tls_*, init_proc_addr, fini_proc_addr, eh_frame_hdr_*), as docs/dev/debt/libkernel.md does for Windows.

  4. PR body: What and Tested describe the three earlier commits. Please add 997f6cfd4 (O_SEARCH) and 458b30a05 (HostWrite guards and the S_ISDIR check), and the run that covers them.

Not checked here: Linux, Windows, and this PR together with #10543 or #10627.

AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5648365621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5648365621). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-26T06:51:58Z. **Review:** CHANGES_REQUESTED --- Re-review of `458b30a05`. The two new commits look right and pass here, but I am requesting changes for two defects. The first appears once this head is merged with current `main`. The second is in the named `shm_open` path that this PR makes reachable on macOS. My 25 Oct report at `3206fbd27` said the second one was a problem in the series' test. That was wrong: it comes from this PR, as item 2 explains. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) Applied at `458b30a05` to mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, with other open macOS fixes, then toggled only this PR. As in my last report, `git apply` conflicts with the series' own macOS versions of these files. "With" therefore means the seven source files and `GuestModuleInfo.cpp` replaced by this head's versions. The series turns `guest_module_info` off on Apple, so I turned it back on for both states. Both states build with 0 errors, and the touched files give no warnings with this PR. I ran 44 tests three times in each state, sequentially, and got the same result every run. The set is the one from my last report, plus `guest_directory_entries`, `guest_underscore_entries`, `guest_path_name_length`, `guest_usb_storage`, `guest_kernel_app_archive_*` and `write_watch_*`, which exercise the getdirentries and `HostWrite` changes: | | `guest_filesystem` | `guest_kernel_sys_module_internal` | `guest_module_info` | `guest_shm_open` (the series' copy) | |---|---|---|---|---| | Without this PR | Fails (stopped at `GuestFilesystem.cpp:1245` last time: getdents on a plain file must return EINVAL) | Fails (stopped at `:19` last time) | `module info check failed at line 34` | Passed | | With this PR | Passed | Passed | Passed | Aborts at `GuestShmOpen.cpp:72`, located with lldb: it expects a named `shm_open` to throw on macOS, and it no longer does. See item 2 | The other 40 tests give the same result in both states. 34 pass both ways: the other `guest_kernel_sys*` tests, `guest_libc_threads`, `guest_statvfs`, `guest_fdopendir`, `guest_file_timestamps`, `guest_pthread_stack`, `guest_raise_exception`, `guest_module_args`, `guest_dynamic_loader_variant_names`, `guest_directory_entries`, `guest_underscore_entries`, `guest_path_name_length`, `guest_usb_storage`, `guest_kernel_app_archive_archive`/`_plain` and all `write_watch_*`. 6 fail both ways at the same point: `guest_aio` (`:437`, needs #10543), `guest_dynamic_loader` (`:57`, needs #10627), `guest_append_writes` (`:79`), `guest_host_standard_error`, `guest_kernel_app_archive_mmap` (`mmap on a file read from app0.zar ... not implemented`) and `guest_refused_connect`. New commits: a standalone x86_64 check on this Mac agrees with `997f6cfd4`. On a mode-0100 directory, `openat(O_RDONLY | O_DIRECTORY)` fails with EACCES, while `openat(O_SEARCH)` succeeds and `fstatat` through that descriptor works. `O_SEARCH` on a regular file fails with ENOTDIR. The guard block added in `458b30a05` matches the Linux `sceKernelGetdirentries` line for line. ### Findings 1. **`core/libs/prx/libkernel/System/src/Rtld.cpp:50`: the macOS branch throws, but `main` now returns -1 or 0.** #10243 (`0ce4b82c2d`, merged after this PR's base `e7e3e9c19`) made `imageContaining` return `nullptr` on Linux and Windows for an address outside every image. With that, `_sceKernelRtldThreadAtexitIncrement` returns -1 and `Decrement` returns 0, and main's `GuestDynamicLoader.cpp:274-277` requires both for `&local` and `nullptr`. Git merges this head into current `main` without a conflict (`git merge-tree`), and the merged `imageContaining` keeps the `__APPLE__` `throw`. On this Mac `dladdr(&local)` and `dladdr(nullptr)` both return 0, so after the merge both calls throw `std::runtime_error` out of an `extern "C"` export on macOS. Fix: rebase on current `main` and replace the throw in the `__APPLE__` branch with `return nullptr;`. (I could not run this check end to end, because `guest_dynamic_loader` stops at `:57` first without #10627.) 2. **`core/libs/prx/libkernel/File/src/SharedMemory.cpp:612` and `:1026`: a valid named `shm_open` returns a made-up ENAMETOOLONG on macOS instead of throwing.** Named objects now go through the shared non-Windows path. That path calls host `::shm_open("/anyps5-shm-" + 40 hex digits)`, a 52-byte name, and macOS limits POSIX shm names to `PSHMNAMLEN` (31). Checked here: `shm_open` with that name fails with errno 63, "File name too long", so every valid guest name gets error 63. `HostShmLock` is Linux-only, so macOS has no namespace lock. `shm_unlink` checks `/dev/shm/...` with `faccessat`, which does not exist on macOS, so it always returns ENOENT. Even with a short name, the Apple `IsHostSharedMemoryDescriptor` (`:125-132`) would not recognise a native POSIX shm descriptor. `docs/dev/debt/libkernel.md:264` says "Named `shm_open` objects still throw on macOS". The series' `GuestShmOpen.cpp:72` asserts that throw, and with this PR the assertion fails. main's `GuestShmOpen.cpp` tests only invalid names, so it does not catch this. Fix: under `__APPLE__`, call `NotImplemented_nid_no_patch` in `OpenNamedFile` and in the named `shm_unlink`, as the debt entry describes. 3. **`core/libs/tests/GuestModuleInfo.cpp:63-64`: on this fixture the new macOS expectation checks nothing.** `otool -l libguest_module_fixture.dylib` here lists only `__text` and `__unwind_info`. `getsectiondata` therefore returns `nullptr` and 0, and the check reduces to `0 == 0 && 0 == 0`. A regression that stops reporting `__eh_frame` would still pass. Either check an image that has an `__eh_frame` section and require `frames != nullptr`, or record in the debt file that the Mach-O fields are not covered. A debt entry is also needed for the other macOS `Fill` fields this PR leaves at 0 (`tls_*`, `init_proc_addr`, `fini_proc_addr`, `eh_frame_hdr_*`), as `docs/dev/debt/libkernel.md` does for Windows. 4. **PR body:** `What` and `Tested` describe the three earlier commits. Please add `997f6cfd4` (`O_SEARCH`) and `458b30a05` (`HostWrite` guards and the `S_ISDIR` check), and the run that covers them. Not checked here: Linux, Windows, and this PR together with #10543 or #10627. AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).
maximilianfv commented 2026-10-26 08:52:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-26T08:52:23Z.

Review: CHANGES_REQUESTED


Re-review of 8d601f9ec. All four items from my review of 458b30a05 are fixed, and the A/B below shows no new failures. I am still requesting changes. A closer read of the __APPLE__ paths found three inputs that get a made-up result where Linux reports an error or throws, and one known gap with no debt entry (items 1 to 4). None of them comes from the three new commits. They were already there at 458b30a05, and I should have raised them then.

Earlier items

  1. Rtld.cpp macOS imageContaining: fixed (49c24af9a). On the "with" build here, lldb calls of _sceKernelRtldThreadAtexitIncrement_nid_postfix and Decrement return -1 and 0 for nullptr and for a stack address, as main's GuestDynamicLoader.cpp:274-277 requires.
  2. Named shm_open and shm_unlink: fixed (105e043a7). They throw after the name check, as docs/dev/debt/libkernel.md:272 says. The series' GuestShmOpen.cpp:72 assertion passes again.
  3. GuestModuleInfo.cpp on a fixture without __eh_frame: recorded in the debt file (8d601f9ec), together with the other macOS fields left at 0.
  4. PR body: it now lists every commit and how each was tested.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Applied at 8d601f9ec to mugurc's macOS series (27e968974) merged with main at 2742004ed2, with other open macOS fixes, then toggled only this PR. This head does not git apply there, because the series has its own macOS versions of these files. "With" therefore replaces the seven source files and GuestModuleInfo.cpp with 2742004ed2 plus this PR's changes (git merge-file, no conflicts). The exception is Rtld.cpp, which is this head's file and so includes #10243. The series turns guest_module_info off on Apple, so I turned it back on in both states. Both states build with 0 errors. The PR's lines add no warnings. DirectoryDescriptor.cpp:249 gives a -Wextern-c-compat warning whenever that file is rebuilt, but the warning is on main's code (struct PathTag {} inside an extern "C" function).

I ran the same 44 tests as last time, three times in each state, sequentially:

guest_filesystem guest_kernel_sys_module_internal guest_module_info guest_shm_open (the series' copy) 44 tests
Without this PR Fails Fails module info check failed at line 34 Passed 35, 36, 35 pass
With this PR Passed Passed Passed Passed 38, 38, 38 pass

The other five failures stop at the same point in both states:

  • guest_aio at :437 (needs #10543)
  • guest_dynamic_loader at :57 (needs #10627)
  • guest_append_writes at :79
  • guest_host_standard_error
  • guest_kernel_app_archive_mmap (mmap on a file read from app0.zar ... not implemented)

guest_refused_connect is flaky on this tree in both states. It passed 4 of 13 runs without this PR and 0 of 13 with it. lldb puts the failures without the PR at GuestRefusedConnect.cpp:81 and :88, and the failure with the PR at :81. Those lines are the libSceNet backlog-drain checks, which no file in this PR touches. It also failed in all 6 runs at the last head. The logs contain no Validation Error lines in either state.

Findings

  1. core/libs/prx/libkernel/File/src/Stdio.cpp:1602, :1621, :1638, :1655: a zero-length readv, writev, preadv or pwritev returns 0 without checking the descriptor.

    • On this Mac, host readv/writev with iovcnt 0 always fail with EINVAL, even for fd -1, so the PR returns 0 instead.
    • Effect: sceKernelReadv(-1, iov, 0) returns 0, and so does sceKernelWritev on a read-only descriptor. On Linux the same calls reach host readv/writev and fail with EBADF.
    • Fix: before returning 0, check the descriptor and its access mode with fcntl(d, F_GETFL). For preadv/pwritev, also check that it is seekable (lseek(d, 0, SEEK_CUR)), and map errno through SceErrorFromErrno.
  2. core/libs/prx/libkernel/File/src/SharedMemory.cpp:125-132: any unlinked file whose name starts with anyps5-shm-, in any directory, counts as shared memory.

    • F_GETPATH still returns the path after unlink. Checked here: for an open, unlinked file anyps5-shm-probe in the temporary directory, F_GETPATH returned its full path while st_nlink was 0.
    • Effect: a title that creates /download0/anyps5-shm-x, opens it and unlinks it gets the shared-memory paths for ftruncate, writev, pwritev and fchflags (EINVAL).
    • Linux only matches /dev/shm/anyps5-shm- and the memfd name.
    • Fix: also require the parent directory to be the one CreateAnonymousFile uses (std::filesystem::temp_directory_path(), compared after realpath, because F_GETPATH returns the path with symbolic links resolved).
  3. core/libs/prx/libkernel/File/src/Stdio.cpp:576-579 vs :592-596: on macOS NativeIsPipe is true for any FIFO, while NativeFstatfs treats only a FIFO whose fstatfs fails as a pipe.

    • On this Mac, fstatfs fails (EINVAL) on a pipe() descriptor and succeeds on a named FIFO.
    • Effect: fchflags on a named FIFO (:763) returns EINVAL on macOS. Linux's NativeIsPipe only matches pipefs, so the same call throws there.
    • Fix: make the macOS NativeIsPipe S_ISFIFO && fstatfs(...) != 0, and have NativeFstatfs call it, as the Linux branch does.
  4. core/libs/prx/libkernel/File/src/Stdio.cpp:1250-1310: the macOS sceKernelGetdirentries has no debt entry.

    • Every call seeks to 0 and reads the whole directory again through the undocumented __getdirentries64 (declared at :372).
    • The descriptor offset and *basep are entry indexes, not the host's directory cookies. A host readdir or lseek on the same descriptor therefore puts it at the wrong entry.
    • Paging through a large directory reads it O(n²) times.
    • Please add a docs/dev/debt/libkernel.md line that states this, like the Windows directory descriptor entry at :158.

Smaller points:

  • SharedMemory.cpp:617: NotImplemented_nid_no_patch(__func__) reports OpenNamedFile, not shm_open. Passing "shm_open" makes the trace name the export.
  • Question: Module/src/DynamicLoader.cpp:329 always loads with RTLD_GLOBAL and hides local modules afterwards in DefaultScopeSymbol. If RTLD_LOCAL cannot work on macOS, a debt line saying so would help the next reader. I have not tested this.

Not checked here: Linux, Windows, and this PR together with #10543 or #10627.

AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5649480357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5649480357). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-26T08:52:23Z. **Review:** CHANGES_REQUESTED --- Re-review of `8d601f9ec`. All four items from my review of `458b30a05` are fixed, and the A/B below shows no new failures. I am still requesting changes. A closer read of the `__APPLE__` paths found three inputs that get a made-up result where Linux reports an error or throws, and one known gap with no debt entry (items 1 to 4). None of them comes from the three new commits. They were already there at `458b30a05`, and I should have raised them then. ### Earlier items 1. `Rtld.cpp` macOS `imageContaining`: fixed (`49c24af9a`). On the "with" build here, lldb calls of `_sceKernelRtldThreadAtexitIncrement_nid_postfix` and `Decrement` return -1 and 0 for `nullptr` and for a stack address, as main's `GuestDynamicLoader.cpp:274-277` requires. 2. Named `shm_open` and `shm_unlink`: fixed (`105e043a7`). They throw after the name check, as `docs/dev/debt/libkernel.md:272` says. The series' `GuestShmOpen.cpp:72` assertion passes again. 3. `GuestModuleInfo.cpp` on a fixture without `__eh_frame`: recorded in the debt file (`8d601f9ec`), together with the other macOS fields left at 0. 4. PR body: it now lists every commit and how each was tested. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) Applied at `8d601f9ec` to mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, with other open macOS fixes, then toggled only this PR. This head does not `git apply` there, because the series has its own macOS versions of these files. "With" therefore replaces the seven source files and `GuestModuleInfo.cpp` with `2742004ed2` plus this PR's changes (`git merge-file`, no conflicts). The exception is `Rtld.cpp`, which is this head's file and so includes #10243. The series turns `guest_module_info` off on Apple, so I turned it back on in both states. Both states build with 0 errors. The PR's lines add no warnings. `DirectoryDescriptor.cpp:249` gives a `-Wextern-c-compat` warning whenever that file is rebuilt, but the warning is on main's code (`struct PathTag {}` inside an `extern "C"` function). I ran the same 44 tests as last time, three times in each state, sequentially: | | `guest_filesystem` | `guest_kernel_sys_module_internal` | `guest_module_info` | `guest_shm_open` (the series' copy) | 44 tests | |---|---|---|---|---|---| | Without this PR | Fails | Fails | `module info check failed at line 34` | Passed | 35, 36, 35 pass | | With this PR | Passed | Passed | Passed | Passed | 38, 38, 38 pass | The other five failures stop at the same point in both states: - `guest_aio` at `:437` (needs #10543) - `guest_dynamic_loader` at `:57` (needs #10627) - `guest_append_writes` at `:79` - `guest_host_standard_error` - `guest_kernel_app_archive_mmap` (`mmap on a file read from app0.zar ... not implemented`) `guest_refused_connect` is flaky on this tree in both states. It passed 4 of 13 runs without this PR and 0 of 13 with it. lldb puts the failures without the PR at `GuestRefusedConnect.cpp:81` and `:88`, and the failure with the PR at `:81`. Those lines are the libSceNet backlog-drain checks, which no file in this PR touches. It also failed in all 6 runs at the last head. The logs contain no `Validation Error` lines in either state. ### Findings 1. **`core/libs/prx/libkernel/File/src/Stdio.cpp:1602`, `:1621`, `:1638`, `:1655`: a zero-length `readv`, `writev`, `preadv` or `pwritev` returns 0 without checking the descriptor.** - On this Mac, host `readv`/`writev` with `iovcnt` 0 always fail with `EINVAL`, even for fd -1, so the PR returns 0 instead. - Effect: `sceKernelReadv(-1, iov, 0)` returns 0, and so does `sceKernelWritev` on a read-only descriptor. On Linux the same calls reach host `readv`/`writev` and fail with `EBADF`. - Fix: before returning 0, check the descriptor and its access mode with `fcntl(d, F_GETFL)`. For `preadv`/`pwritev`, also check that it is seekable (`lseek(d, 0, SEEK_CUR)`), and map `errno` through `SceErrorFromErrno`. 2. **`core/libs/prx/libkernel/File/src/SharedMemory.cpp:125-132`: any unlinked file whose name starts with `anyps5-shm-`, in any directory, counts as shared memory.** - `F_GETPATH` still returns the path after `unlink`. Checked here: for an open, unlinked file `anyps5-shm-probe` in the temporary directory, `F_GETPATH` returned its full path while `st_nlink` was 0. - Effect: a title that creates `/download0/anyps5-shm-x`, opens it and unlinks it gets the shared-memory paths for `ftruncate`, `writev`, `pwritev` and `fchflags` (`EINVAL`). - Linux only matches `/dev/shm/anyps5-shm-` and the memfd name. - Fix: also require the parent directory to be the one `CreateAnonymousFile` uses (`std::filesystem::temp_directory_path()`, compared after `realpath`, because `F_GETPATH` returns the path with symbolic links resolved). 3. **`core/libs/prx/libkernel/File/src/Stdio.cpp:576-579` vs `:592-596`: on macOS `NativeIsPipe` is true for any FIFO, while `NativeFstatfs` treats only a FIFO whose `fstatfs` fails as a pipe.** - On this Mac, `fstatfs` fails (`EINVAL`) on a `pipe()` descriptor and succeeds on a named FIFO. - Effect: `fchflags` on a named FIFO (`:763`) returns `EINVAL` on macOS. Linux's `NativeIsPipe` only matches pipefs, so the same call throws there. - Fix: make the macOS `NativeIsPipe` `S_ISFIFO && fstatfs(...) != 0`, and have `NativeFstatfs` call it, as the Linux branch does. 4. **`core/libs/prx/libkernel/File/src/Stdio.cpp:1250-1310`: the macOS `sceKernelGetdirentries` has no debt entry.** - Every call seeks to 0 and reads the whole directory again through the undocumented `__getdirentries64` (declared at `:372`). - The descriptor offset and `*basep` are entry indexes, not the host's directory cookies. A host `readdir` or `lseek` on the same descriptor therefore puts it at the wrong entry. - Paging through a large directory reads it O(n²) times. - Please add a `docs/dev/debt/libkernel.md` line that states this, like the Windows directory descriptor entry at `:158`. Smaller points: - `SharedMemory.cpp:617`: `NotImplemented_nid_no_patch(__func__)` reports `OpenNamedFile`, not `shm_open`. Passing `"shm_open"` makes the trace name the export. - Question: `Module/src/DynamicLoader.cpp:329` always loads with `RTLD_GLOBAL` and hides local modules afterwards in `DefaultScopeSymbol`. If `RTLD_LOCAL` cannot work on macOS, a debt line saying so would help the next reader. I have not tested this. Not checked here: Linux, Windows, and this PR together with #10543 or #10627. AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).
maximilianfv commented 2026-10-26 21:01:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-26T21:01:08Z.

Review: CHANGES_REQUESTED


Re-review of 6a689187b. The new commits fix all four items and both smaller points from my review of 8d601f9ec, and the A/B below shows no new failures. I am still requesting changes for three defects, items 1 to 3 below. None of them comes from the new commits. Items 1 and 2 were already there at 8d601f9ec, and item 3 comes from the rebase onto main.

I tested ea0213b6a, and you rebased while my runs were going. Between the two heads only the F_GETPATH commit changed: it is now the HostDescriptorTarget helper, which covers both of main's /proc/self/fd reads. Every other file this PR changes is the same on this tree at both heads (git merge-file of each head onto the tree's main gives identical files). I did not rebuild at 6a689187b, because the new DirectoryDescriptor.cpp needs main's archived-directory changes, which this tree does not have yet.

Earlier items

  1. Zero-length readv/writev/preadv/pwritev: fixed (f0bb0ae73). I called them through lldb on the "with" build:
    • fd -1, writev on a read-only descriptor and readv on a write-only one return EBADF (0x80020009).
    • preadv and pwritev on a pipe return ESPIPE (0x8002001d).
    • Valid descriptors return 0.
  2. anyps5-shm- outside the temporary directory: fixed (88b344f1c). The check uses the same temp_directory_path() as CreateAnonymousFile, and canonical resolves symbolic links the same way F_GETPATH does.
  3. Named FIFOs: fixed (f8bd9c3ae). NativeFstatfs behaves as before. The change a title can see is fchflags on a named FIFO, which now throws, as on Linux.
  4. sceKernelGetdirentries debt entry: added (6a689187b), with the RTLD_GLOBAL question and both trace names.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Applied at ea0213b6a to mugurc's macOS series (27e968974) merged with main at 2742004ed2, with other open macOS fixes, then toggled only this PR. As before, the head does not git apply there, so "with" is the file set from my last round plus git diff 8d601f9ec ea0213b6a. The series turns guest_module_info off on Apple, so I turned it back on in both states. Both states build with 0 errors, and the PR's lines add no warnings.

I ran the same 44 tests as last time, three times in each state, one run after another:

guest_filesystem guest_kernel_sys_module_internal guest_module_info guest_shm_open (the series' copy) 44 tests
Without this PR Fails Fails module info check failed at line 34 Passed 36, 36, 36 pass
With this PR Passed Passed Passed Passed 39, 39, 39 pass

The other five failures stop at the same point in both states:

  • guest_aio at :437 (needs #10543)
  • guest_dynamic_loader at :57 (needs #10627)
  • guest_append_writes at :79
  • guest_host_standard_error
  • guest_kernel_app_archive_mmap (mmap on a file read from app0.zar ... not implemented)

guest_refused_connect passed all six runs this time. No log has a Validation Error line.

Findings

  1. core/libs/prx/libkernel/Module/src/DynamicLoader.cpp:423: dlclose unregisters a relinked image under its Mach-O header, but mprotect registered it under its guest load bias.

    • mprotect registers the image under search.base = image->dlpi_addr (DirectMemory/DirectMemory.cpp:1285 and :1369).
    • The macOS dl_iterate_phdr sets dlpi_addr to the __APS5DATA,__meta load bias (libc/src/specifics/macos/GuestImages.cpp:33).
    • The relinker writes that load bias as imageBase + rva(first PT_LOAD) - p_vaddr (relinker/elfpatcher/src/macos/MacOsImage.cpp:511).
    • __TEXT takes the first LoadRva (0x10000) bytes, and GuestAllocations.cpp:336 requires the image's first loaded page to be its base. The key is therefore never the header.
    • ImageBase (:113) returns _dyld_get_image_header(index). UnregisterImage (GuestAllocations.cpp:343) finds no entry under that key and returns without doing anything.
    • Effect: when a title mprotects inside a .guest.prx and then unloads it, the image's pages stay in the allocation registry.
    • The guest_flexible_memory_budget fixture cannot catch this, because it writes dli_fbase as its load bias.
    • Fix: pass the dlpi_addr that dl_iterate_phdr reports for this image, as the macOS series' ImageBase does. Keep the header for the DefaultScopeSymbol comparison with dli_fbase.
    • I found this by reading the code and did not run it: no test here unloads a relinked image.
  2. DynamicLoader.cpp:134-145: DefaultScopeSymbol returns null when the first dlsym(RTLD_DEFAULT) hit is in a local module, even if a global module exports the same name.

    • Example: module A is loaded with flags 0 and exports foo, then module B is loaded with 0x100 and also exports foo. dyld returns A's foo first, the filter drops it, and the lookup returns null.
    • On Linux A is RTLD_LOCAL, so the same lookup returns B's foo.
    • Fix: when the first hit is hidden, try dlsym on each global module's handle in load order. Otherwise, state this limit in the new debt entry (docs/dev/debt/libkernel.md:145).
  3. core/libs/prx/libkernel/File/src/Stdio.cpp:1267: the macOS sceKernelGetdirentries does not have main's directory-descriptor branch.

    • 0a64d23bd (feat(libkernel): enumerate archived app directories) added if (File::IsDirectoryDescriptor(fd)) return File::ReadDirectoryDescriptor(fd, buf, nbytes, basep); to the non-Windows copy. After the rebase, that branch is only in the Linux copy (:1354).
    • On POSIX an archived app0 directory is opened as /dev/null (DirectoryDescriptor.cpp:246). On macOS, getdirentries on it therefore reaches fstat, sees a file that is not a directory, and returns EINVAL instead of the archive's entries.
    • Fix: add the same branch to the macOS copy, after the HostWrite checks.
    • I found this by reading the code: this tree does not have main's archived directories yet.

Smaller points:

  • SharedMemory.cpp:133-135 calls temp_directory_path and canonical (realpath) on every check of an unlinked anyps5-shm- file. A function-local static const would do this once.
  • Body, f0bb0ae73: "as host Linux does" is not quite right. Linux preadv checks that the descriptor is seekable first, so a write-only pipe end gives ESPIPE on Linux and EBADF here. The code's order (EBADF first) is the FreeBSD 11.4 order, so only the wording needs to change.
  • Question: ImageBase throws std::runtime_error (DynamicLoader.cpp:131) when no RTLD_NOLOAD probe returns the module's handle. dlclose_nid_postfix does not catch it, so the exception would leave an extern "C" export. I have not found an input that reaches the throw. If the fix for item 1 stores the base when the module is opened, ImageBase and its throw are no longer needed.

Not checked here: Linux, Windows, a build at 6a689187b, and this PR together with #10543 or #10627.

AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5658936236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5658936236). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-26T21:01:08Z. **Review:** CHANGES_REQUESTED --- Re-review of `6a689187b`. The new commits fix all four items and both smaller points from my review of `8d601f9ec`, and the A/B below shows no new failures. I am still requesting changes for three defects, items 1 to 3 below. None of them comes from the new commits. Items 1 and 2 were already there at `8d601f9ec`, and item 3 comes from the rebase onto `main`. I tested `ea0213b6a`, and you rebased while my runs were going. Between the two heads only the `F_GETPATH` commit changed: it is now the `HostDescriptorTarget` helper, which covers both of main's `/proc/self/fd` reads. Every other file this PR changes is the same on this tree at both heads (`git merge-file` of each head onto the tree's `main` gives identical files). I did not rebuild at `6a689187b`, because the new `DirectoryDescriptor.cpp` needs main's archived-directory changes, which this tree does not have yet. ### Earlier items 1. Zero-length `readv`/`writev`/`preadv`/`pwritev`: fixed (`f0bb0ae73`). I called them through lldb on the "with" build: - fd -1, `writev` on a read-only descriptor and `readv` on a write-only one return `EBADF` (`0x80020009`). - `preadv` and `pwritev` on a pipe return `ESPIPE` (`0x8002001d`). - Valid descriptors return 0. 2. `anyps5-shm-` outside the temporary directory: fixed (`88b344f1c`). The check uses the same `temp_directory_path()` as `CreateAnonymousFile`, and `canonical` resolves symbolic links the same way `F_GETPATH` does. 3. Named FIFOs: fixed (`f8bd9c3ae`). `NativeFstatfs` behaves as before. The change a title can see is `fchflags` on a named FIFO, which now throws, as on Linux. 4. `sceKernelGetdirentries` debt entry: added (`6a689187b`), with the `RTLD_GLOBAL` question and both trace names. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) Applied at `ea0213b6a` to mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, with other open macOS fixes, then toggled only this PR. As before, the head does not `git apply` there, so "with" is the file set from my last round plus `git diff 8d601f9ec ea0213b6a`. The series turns `guest_module_info` off on Apple, so I turned it back on in both states. Both states build with 0 errors, and the PR's lines add no warnings. I ran the same 44 tests as last time, three times in each state, one run after another: | | `guest_filesystem` | `guest_kernel_sys_module_internal` | `guest_module_info` | `guest_shm_open` (the series' copy) | 44 tests | |---|---|---|---|---|---| | Without this PR | Fails | Fails | `module info check failed at line 34` | Passed | 36, 36, 36 pass | | With this PR | Passed | Passed | Passed | Passed | 39, 39, 39 pass | The other five failures stop at the same point in both states: - `guest_aio` at `:437` (needs #10543) - `guest_dynamic_loader` at `:57` (needs #10627) - `guest_append_writes` at `:79` - `guest_host_standard_error` - `guest_kernel_app_archive_mmap` (`mmap on a file read from app0.zar ... not implemented`) `guest_refused_connect` passed all six runs this time. No log has a `Validation Error` line. ### Findings 1. **`core/libs/prx/libkernel/Module/src/DynamicLoader.cpp:423`: `dlclose` unregisters a relinked image under its Mach-O header, but `mprotect` registered it under its guest load bias.** - `mprotect` registers the image under `search.base = image->dlpi_addr` (`DirectMemory/DirectMemory.cpp:1285` and `:1369`). - The macOS `dl_iterate_phdr` sets `dlpi_addr` to the `__APS5DATA,__meta` load bias (`libc/src/specifics/macos/GuestImages.cpp:33`). - The relinker writes that load bias as `imageBase + rva(first PT_LOAD) - p_vaddr` (`relinker/elfpatcher/src/macos/MacOsImage.cpp:511`). - `__TEXT` takes the first `LoadRva` (0x10000) bytes, and `GuestAllocations.cpp:336` requires the image's first loaded page to be its base. The key is therefore never the header. - `ImageBase` (`:113`) returns `_dyld_get_image_header(index)`. `UnregisterImage` (`GuestAllocations.cpp:343`) finds no entry under that key and returns without doing anything. - Effect: when a title `mprotect`s inside a `.guest.prx` and then unloads it, the image's pages stay in the allocation registry. - The `guest_flexible_memory_budget` fixture cannot catch this, because it writes `dli_fbase` as its load bias. - Fix: pass the `dlpi_addr` that `dl_iterate_phdr` reports for this image, as the macOS series' `ImageBase` does. Keep the header for the `DefaultScopeSymbol` comparison with `dli_fbase`. - I found this by reading the code and did not run it: no test here unloads a relinked image. 2. **`DynamicLoader.cpp:134-145`: `DefaultScopeSymbol` returns null when the first `dlsym(RTLD_DEFAULT)` hit is in a local module, even if a global module exports the same name.** - Example: module A is loaded with flags 0 and exports `foo`, then module B is loaded with 0x100 and also exports `foo`. dyld returns A's `foo` first, the filter drops it, and the lookup returns null. - On Linux A is `RTLD_LOCAL`, so the same lookup returns B's `foo`. - Fix: when the first hit is hidden, try `dlsym` on each global module's handle in load order. Otherwise, state this limit in the new debt entry (`docs/dev/debt/libkernel.md:145`). 3. **`core/libs/prx/libkernel/File/src/Stdio.cpp:1267`: the macOS `sceKernelGetdirentries` does not have main's directory-descriptor branch.** - `0a64d23bd` (`feat(libkernel): enumerate archived app directories`) added `if (File::IsDirectoryDescriptor(fd)) return File::ReadDirectoryDescriptor(fd, buf, nbytes, basep);` to the non-Windows copy. After the rebase, that branch is only in the Linux copy (`:1354`). - On POSIX an archived app0 directory is opened as `/dev/null` (`DirectoryDescriptor.cpp:246`). On macOS, `getdirentries` on it therefore reaches `fstat`, sees a file that is not a directory, and returns `EINVAL` instead of the archive's entries. - Fix: add the same branch to the macOS copy, after the `HostWrite` checks. - I found this by reading the code: this tree does not have main's archived directories yet. Smaller points: - `SharedMemory.cpp:133-135` calls `temp_directory_path` and `canonical` (`realpath`) on every check of an unlinked `anyps5-shm-` file. A function-local `static const` would do this once. - Body, `f0bb0ae73`: "as host Linux does" is not quite right. Linux `preadv` checks that the descriptor is seekable first, so a write-only pipe end gives `ESPIPE` on Linux and `EBADF` here. The code's order (`EBADF` first) is the FreeBSD 11.4 order, so only the wording needs to change. - Question: `ImageBase` throws `std::runtime_error` (`DynamicLoader.cpp:131`) when no `RTLD_NOLOAD` probe returns the module's handle. `dlclose_nid_postfix` does not catch it, so the exception would leave an `extern "C"` export. I have not found an input that reaches the throw. If the fix for item 1 stores the base when the module is opened, `ImageBase` and its throw are no longer needed. Not checked here: Linux, Windows, a build at `6a689187b`, and this PR together with #10543 or #10627. AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).
maximilianfv commented 2026-10-27 13:26:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-27T13:26:45Z.

Review: DISMISSED


Re-review of 41dc0d130. The new commits fix findings 1 and 3 of my review of 6a689187b, the shared-memory point and the ImageBase question. The A/B below shows the same three tests passing that it showed last time, and no new failures. Finding 2 of my last review was my mistake, see below. I am still requesting changes for items 1 to 4: item 1 comes from the rebase, item 2 I confirmed with a probe, and items 3 and 4 I found by reading the code.

Earlier items

  1. dlclose key: fixed (b65f94a7a). FindImage reads the load bias from __APS5DATA,__meta (version 2, offset 0x08), the same field dl_iterate_phdr reports as dlpi_addr (libc/src/specifics/macos/GuestImages.cpp). That is the key mprotect registers images under. An image without that metadata keeps its Mach-O header as the key; UnregisterImage then does nothing, as before.
  2. DefaultScopeSymbol shadowing: this was my mistake. dlsym_nid_postfix (DynamicLoader.cpp:398-409) already runs FindSymbol on every module loaded with 0x100, for all four names, before it calls DefaultScopeSymbol. So in my example, B's foo was already found. I'm sorry for the extra work. See item 3 for what this means for the new fallback.
  3. Archived directories in the macOS sceKernelGetdirentries: fixed (355b6982a). The branch comes after the HostWrite checks, as in the Linux copy.
  • SharedMemory.cpp: the temporary directory is now resolved once (d130c8ee3).
  • The ImageBase throw is gone along with ImageBase.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

The PR no longer applies to my test tree. That tree is mugurc's macOS series (27e968974) merged with main at 2742004ed2, plus other open macOS fixes. On current main alone, the guest_* tests do not link on macOS, as your Tested section says. So "with" is the file set I tested at ea0213b6a plus the four new commits (git diff dc9d0ff80 41dc0d130 for DynamicLoader.cpp, Stdio.cpp and SharedMemory.cpp), which applied without conflicts. "Without" is the same tree without this PR. The tree turns guest_module_info off on Apple, so I turned it back on in both states. Both states build with 0 errors.

I ran the same 44 tests three times in each state, one run after another:

guest_filesystem guest_kernel_sys_module_internal guest_module_info 44 tests
Without this PR Fails Fails module info check failed at line 34 37, 37, 36 pass
With this PR Passed Passed Passed 39, 39, 39 pass

These fail the same way in both states:

  • guest_dynamic_loader stops at GuestDynamicLoader.cpp:57 (needs #10627).
  • guest_aio and guest_append_writes abort.
  • guest_kernel_app_archive_mmap stops at mmap on a file read from app0.zar ... not implemented.

guest_refused_connect failed in all three "with" runs and in one "without" run, so I ran it 20 more times in each state. It passed 6 of 20 times with this PR and 3 of 20 without. With a -O0 -g build, both states stop at the same place: GuestRefusedConnect.cpp:81, the first backlog-drain connect. It fails on this tree without the PR too, so it is not caused by this PR. No log has a Validation Error line.

Findings

  1. docs/dev/debt/libkernel.md:175-176: the rebase added a second copy of the ftruncate entry and of the readv entry.

    • :175 repeats :171 without its last sentence (PPSA04354).
    • :176 is an older version of :173, so the two readv entries now disagree.
    • On main (1764e016f8), the file has one of each.
    • Fix: delete lines 175 and 176.
  2. sceKernelDlsym(0, ...) finds symbols in modules loaded without 0x100, which contradicts the new debt entry at docs/dev/debt/libkernel.md:148.

    • sceKernelDlsym with handle 0 calls dlopen_nid_postfix(nullptr, 2) and then dlsym_nid_postfix on that handle (Module.cpp:173).
    • On macOS, dlopen(NULL) returns "a handle equivalent to RTLD_DEFAULT" (man 3 dlopen). FindSymbol → Symbol (DynamicLoader.cpp:100, :199) calls plain dlsym on that handle and never applies the HiddenImage filter. Every module is opened with RTLD_GLOBAL (:354), so this lookup sees all of them.
    • I checked this with a probe linked like guest_module_info_tests. It loads libguest_module_fixture.dylib with flags 2, then looks up GuestModuleAdd:
      • dlsym_nid_postfix(nullptr, ...) returns null.
      • sceKernelDlsym(0, ...) returns 0, with the module's address.
    • sceKernelLoadStartModule also loads with flags 2 (Module.cpp:292), so this affects every module a title starts.
    • On Linux the module is RTLD_LOCAL, so the same call should return ESRCH. I worked that out from the code and did not run it.
    • Fix: on macOS, send lookups on the main-program handle through the same filter (DefaultScopeSymbol), or open the main program with RTLD_FIRST and search the global modules after it. Then correct the debt entry.
  3. DynamicLoader.cpp:163-168: the new fallback loop in DefaultScopeSymbol cannot find anything.

    • globals is the set dlsym_nid_postfix has just searched (:398, :409). The loop's dlsym(module->native, symbolName) uses one of the four names FindSymbol already tried on each of those modules, and each of those lookups returned null.
    • So the loop only repeats lookups that failed. The sentences about it in the debt entry (:148, "then looks the name up..." and "that fallback searches each of those modules' dependencies...") and in the PR body describe a path that never changes the result.
    • Fix: drop the loop and keep return nullptr for a hidden image, or say which input reaches it. Then update the debt entry and the body.
  4. Stdio.cpp:1306-1332: the macOS sceKernelGetdirentries skips entries when the guest deletes entries between calls.

    • The descriptor offset is an entry index, and each call skips the first base entries of a fresh listing.
    • Example: a title reads a batch of k entries, unlinks them and reads again. The new listing has k fewer entries, so skipping k entries skips entries the title never saw. A recursive delete then leaves files behind, and rmdir fails with ENOTEMPTY. Creating entries between calls can return an entry twice.
    • On FreeBSD the offset is the file system's directory cookie, so this does not happen.
    • The debt entry at :174 covers host readdir and lseek on the same descriptor, but not this case.
    • Fix: resume from the entry after the last one returned, matched by d_ino and name, or by d_seekoff if APFS fills it in (I have not checked that it does). Otherwise, add this case to the entry.

Smaller points:

  • ModuleInfo.cpp:177-181: on macOS, sceKernelGetModuleInfoFromAddr reports eh_frame_hdr as 0, and the debt entry says so. But sceKernelGetModuleInfoForUnwind already reads PT_GNU_EH_FRAME for relinked images from the __meta program headers (Module.cpp, the macOS dl_iterate_phdr branch). The same source would fill eh_frame_hdr here. Then the body's "eh_frame_hdr is 0 (Mach-O has none)" would also be correct for relinked images.
  • NativeStat.cpp:85-101: if a // falls exactly at the split point (/ at index PATH_MAX - 1 and PATH_MAX), rest starts with /, so openat and fstatat resolve it from the host root. Stripping leading slashes from rest after each split would prevent that. I found this by reading the code.
  • FindImage (:116-138): an image with a __meta section of 0x10 to 0x5f bytes is accepted here, but dl_iterate_phdr skips it. Checking for at least 0x60 bytes, as GuestImages.cpp does, would keep the two in step.
  • Body:
    • #10543 and #7201 are now closed. #10543 was merged, so "left to #10543" and "guest_aio on macOS needs #10543" are out of date.
    • The fixture's --no-as-needed/nid_patcher_dynamic_strings guard is now on main, so c9b9dac5d no longer changes CMakeLists.txt.
    • d130c8ee3 is typed perf without trace_digest numbers; refactor would fit.

Not checked here: Linux, Windows, a build of the PR's own DirectoryDescriptor.cpp and Rtld.cpp at this head (my "with" set has their ea0213b6a versions), and this PR together with #10627.

AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5667774405 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5667774405). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-27T13:26:45Z. **Review:** DISMISSED --- Re-review of `41dc0d130`. The new commits fix findings 1 and 3 of my review of `6a689187b`, the shared-memory point and the `ImageBase` question. The A/B below shows the same three tests passing that it showed last time, and no new failures. Finding 2 of my last review was my mistake, see below. I am still requesting changes for items 1 to 4: item 1 comes from the rebase, item 2 I confirmed with a probe, and items 3 and 4 I found by reading the code. ### Earlier items 1. `dlclose` key: fixed (`b65f94a7a`). `FindImage` reads the load bias from `__APS5DATA,__meta` (version 2, offset 0x08), the same field `dl_iterate_phdr` reports as `dlpi_addr` (`libc/src/specifics/macos/GuestImages.cpp`). That is the key `mprotect` registers images under. An image without that metadata keeps its Mach-O header as the key; `UnregisterImage` then does nothing, as before. 2. `DefaultScopeSymbol` shadowing: this was my mistake. `dlsym_nid_postfix` (`DynamicLoader.cpp:398-409`) already runs `FindSymbol` on every module loaded with 0x100, for all four names, before it calls `DefaultScopeSymbol`. So in my example, B's `foo` was already found. I'm sorry for the extra work. See item 3 for what this means for the new fallback. 3. Archived directories in the macOS `sceKernelGetdirentries`: fixed (`355b6982a`). The branch comes after the `HostWrite` checks, as in the Linux copy. - `SharedMemory.cpp`: the temporary directory is now resolved once (`d130c8ee3`). - The `ImageBase` throw is gone along with `ImageBase`. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) The PR no longer applies to my test tree. That tree is mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, plus other open macOS fixes. On current `main` alone, the `guest_*` tests do not link on macOS, as your Tested section says. So "with" is the file set I tested at `ea0213b6a` plus the four new commits (`git diff dc9d0ff80 41dc0d130` for `DynamicLoader.cpp`, `Stdio.cpp` and `SharedMemory.cpp`), which applied without conflicts. "Without" is the same tree without this PR. The tree turns `guest_module_info` off on Apple, so I turned it back on in both states. Both states build with 0 errors. I ran the same 44 tests three times in each state, one run after another: | | `guest_filesystem` | `guest_kernel_sys_module_internal` | `guest_module_info` | 44 tests | |---|---|---|---|---| | Without this PR | Fails | Fails | `module info check failed at line 34` | 37, 37, 36 pass | | With this PR | Passed | Passed | Passed | 39, 39, 39 pass | These fail the same way in both states: - `guest_dynamic_loader` stops at `GuestDynamicLoader.cpp:57` (needs #10627). - `guest_aio` and `guest_append_writes` abort. - `guest_kernel_app_archive_mmap` stops at `mmap on a file read from app0.zar ... not implemented`. `guest_refused_connect` failed in all three "with" runs and in one "without" run, so I ran it 20 more times in each state. It passed 6 of 20 times with this PR and 3 of 20 without. With a `-O0 -g` build, both states stop at the same place: `GuestRefusedConnect.cpp:81`, the first backlog-drain connect. It fails on this tree without the PR too, so it is not caused by this PR. No log has a `Validation Error` line. ### Findings 1. **`docs/dev/debt/libkernel.md:175-176`: the rebase added a second copy of the `ftruncate` entry and of the `readv` entry.** - `:175` repeats `:171` without its last sentence (PPSA04354). - `:176` is an older version of `:173`, so the two `readv` entries now disagree. - On `main` (`1764e016f8`), the file has one of each. - Fix: delete lines 175 and 176. 2. **`sceKernelDlsym(0, ...)` finds symbols in modules loaded without 0x100, which contradicts the new debt entry at `docs/dev/debt/libkernel.md:148`.** - `sceKernelDlsym` with handle 0 calls `dlopen_nid_postfix(nullptr, 2)` and then `dlsym_nid_postfix` on that handle (`Module.cpp:173`). - On macOS, `dlopen(NULL)` returns "a handle equivalent to `RTLD_DEFAULT`" (`man 3 dlopen`). `FindSymbol` → `Symbol` (`DynamicLoader.cpp:100`, `:199`) calls plain `dlsym` on that handle and never applies the `HiddenImage` filter. Every module is opened with `RTLD_GLOBAL` (`:354`), so this lookup sees all of them. - I checked this with a probe linked like `guest_module_info_tests`. It loads `libguest_module_fixture.dylib` with flags 2, then looks up `GuestModuleAdd`: - `dlsym_nid_postfix(nullptr, ...)` returns null. - `sceKernelDlsym(0, ...)` returns 0, with the module's address. - `sceKernelLoadStartModule` also loads with flags 2 (`Module.cpp:292`), so this affects every module a title starts. - On Linux the module is `RTLD_LOCAL`, so the same call should return `ESRCH`. I worked that out from the code and did not run it. - Fix: on macOS, send lookups on the main-program handle through the same filter (`DefaultScopeSymbol`), or open the main program with `RTLD_FIRST` and search the global modules after it. Then correct the debt entry. 3. **`DynamicLoader.cpp:163-168`: the new fallback loop in `DefaultScopeSymbol` cannot find anything.** - `globals` is the set `dlsym_nid_postfix` has just searched (`:398`, `:409`). The loop's `dlsym(module->native, symbolName)` uses one of the four names `FindSymbol` already tried on each of those modules, and each of those lookups returned null. - So the loop only repeats lookups that failed. The sentences about it in the debt entry (`:148`, "then looks the name up..." and "that fallback searches each of those modules' dependencies...") and in the PR body describe a path that never changes the result. - Fix: drop the loop and keep `return nullptr` for a hidden image, or say which input reaches it. Then update the debt entry and the body. 4. **`Stdio.cpp:1306-1332`: the macOS `sceKernelGetdirentries` skips entries when the guest deletes entries between calls.** - The descriptor offset is an entry index, and each call skips the first `base` entries of a fresh listing. - Example: a title reads a batch of k entries, unlinks them and reads again. The new listing has k fewer entries, so skipping k entries skips entries the title never saw. A recursive delete then leaves files behind, and `rmdir` fails with `ENOTEMPTY`. Creating entries between calls can return an entry twice. - On FreeBSD the offset is the file system's directory cookie, so this does not happen. - The debt entry at `:174` covers host `readdir` and `lseek` on the same descriptor, but not this case. - Fix: resume from the entry after the last one returned, matched by `d_ino` and name, or by `d_seekoff` if APFS fills it in (I have not checked that it does). Otherwise, add this case to the entry. Smaller points: - `ModuleInfo.cpp:177-181`: on macOS, `sceKernelGetModuleInfoFromAddr` reports `eh_frame_hdr` as 0, and the debt entry says so. But `sceKernelGetModuleInfoForUnwind` already reads `PT_GNU_EH_FRAME` for relinked images from the `__meta` program headers (`Module.cpp`, the macOS `dl_iterate_phdr` branch). The same source would fill `eh_frame_hdr` here. Then the body's "eh_frame_hdr is 0 (Mach-O has none)" would also be correct for relinked images. - `NativeStat.cpp:85-101`: if a `//` falls exactly at the split point (`/` at index `PATH_MAX - 1` and `PATH_MAX`), `rest` starts with `/`, so `openat` and `fstatat` resolve it from the host root. Stripping leading slashes from `rest` after each split would prevent that. I found this by reading the code. - `FindImage` (`:116-138`): an image with a `__meta` section of 0x10 to 0x5f bytes is accepted here, but `dl_iterate_phdr` skips it. Checking for at least 0x60 bytes, as `GuestImages.cpp` does, would keep the two in step. - Body: - #10543 and #7201 are now closed. #10543 was merged, so "left to #10543" and "`guest_aio` on macOS needs #10543" are out of date. - The fixture's `--no-as-needed`/`nid_patcher_dynamic_strings` guard is now on `main`, so `c9b9dac5d` no longer changes `CMakeLists.txt`. - `d130c8ee3` is typed `perf` without `trace_digest` numbers; `refactor` would fit. Not checked here: Linux, Windows, a build of the PR's own `DirectoryDescriptor.cpp` and `Rtld.cpp` at this head (my "with" set has their `ea0213b6a` versions), and this PR together with #10627. AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).
MarcZX10 commented 2026-10-29 05:41:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:41:48Z.

Review: CHANGES_REQUESTED


Re-checked — changes requested

The dependency description is stale and the replacement branch has a real integration conflict. #11872 is closed and was replaced by the read work in #12682 (followed by #12688 and #12700). A pairwise merge preview of this PR’s current head with #12682 conflicts in core/libs/prx/libkernel/File/src/Stdio.cpp, which both branches change. My earlier comment asked for the order against the replacement IDs; the checklist still names only #11872.

What I ran

  • Checked exact head 2f75dd54e9240fed729e9be0a541d8f074fc31e1, current main 52c09d567d2fa453494dd720fcf53a98149468e4, and replacement PR #12682.
  • Ran git merge-tree --write-tree between the exact heads of #10552 and #12682, and inspected both Stdio.cpp diffs.
  • Checked the current PR description and the author’s latest integration update. No updated dependency or agreed order is recorded yet.

Results

Check Result
#10552 with current main Merge preview is clean
#10552 with #12682 Content conflict in core/libs/prx/libkernel/File/src/Stdio.cpp
Declared order Still refers to closed #11872; replacement order is not recorded
Product tests No combined test result for the conflicting heads is reported

Please coordinate the order with the affected PR author(s), update Depends on to the current open PRs as appropriate, and validate the combined Stdio.cpp behavior after integration. I am not requesting that either PR be closed or abandoned.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693357848 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10552#pullrequestreview-5693357848). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T05:41:48Z. **Review:** CHANGES_REQUESTED --- # Re-checked — changes requested The dependency description is stale and the replacement branch has a real integration conflict. #11872 is closed and was replaced by the read work in #12682 (followed by #12688 and #12700). A pairwise merge preview of this PR’s current head with #12682 conflicts in `core/libs/prx/libkernel/File/src/Stdio.cpp`, which both branches change. My earlier comment asked for the order against the replacement IDs; the checklist still names only #11872. ## What I ran - Checked exact head `2f75dd54e9240fed729e9be0a541d8f074fc31e1`, current `main` `52c09d567d2fa453494dd720fcf53a98149468e4`, and replacement PR #12682. - Ran `git merge-tree --write-tree` between the exact heads of #10552 and #12682, and inspected both `Stdio.cpp` diffs. - Checked the current PR description and the author’s latest integration update. No updated dependency or agreed order is recorded yet. ## Results | Check | Result | |---|---| | #10552 with current `main` | Merge preview is clean | | #10552 with #12682 | Content conflict in `core/libs/prx/libkernel/File/src/Stdio.cpp` | | Declared order | Still refers to closed #11872; replacement order is not recorded | | Product tests | No combined test result for the conflicting heads is reported | Please coordinate the order with the affected PR author(s), update `Depends on` to the current open PRs as appropriate, and validate the combined `Stdio.cpp` behavior after integration. I am not requesting that either PR be closed or abandoned.
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!10552
No description provided.