fix(libs): find the executable directory on macOS #4014

Merged
ahir061 merged 1 commit from upstream-pr/4014 into main 2026-10-12 12:56:48 +00:00
ahir061 commented 2026-10-11 21:16:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-11T21:16:25Z.


What

macOS has no /proc, so std::filesystem::read_symlink("/proc/self/exe") throws (filesystem_error, No such file or directory). libSceFont's ExecutableDirectory() and libSceNpEntitlementAccess's EntitlementsPath() use it on every non-Windows host, so finding the system fonts and the entitlements file threw on macOS.

Both now have an __APPLE__ branch: the path from _NSGetExecutablePath (size queried first), resolved with std::filesystem::canonical. That gives the same real folder /proc/self/exe gives on Linux, also when the executable is launched through a symlink or a relative path. A failure throws the message each file already uses on Windows. Linux and Windows are unchanged, and the ANYPS5_ENTITLEMENTS override still comes first.

Not changed here: ShaderDiskCache.cpp also reads /proc/self/exe, but with an error_code, so on macOS the shader and pipeline disk caches are silently disabled instead of throwing (unless ANYPS5_SHADER_CACHE_DIR is set). ModuleInfo.cpp, GuestAllocations.cpp and the GuestImageAnonymous test also read it. Those are separate topics.

Tested

  • MacBook Air M3(Apple silicon), macOS 26.0.1, AppleClang 17.0.0, x86_64 under Rosetta:
    • read_symlink("/proc/self/exe") throws filesystem_error (No such file or directory), the failure this fixes.
    • A standalone program with the same lookup code prints the real executable folder when launched by full path, as ./program from its folder, and through a symlink in another folder (it resolves to the target's folder).
    • Both changed files compile for x86_64 macOS (clang++ -std=c++20 -arch x86_64 -fsyntax-only). The only warnings are the existing -Wreturn-type-c-linkage ones from General.hpp.
  • tools/check_conventions.py --base upstream/main passes.
  • Not run: the two libraries and their tests, since the full build does not complete on macOS yet; Linux and Windows builds (their code paths are unchanged).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4822413019 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4014). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-11T21:16:25Z. --- ### What macOS has no `/proc`, so `std::filesystem::read_symlink("/proc/self/exe")` throws (`filesystem_error`, No such file or directory). libSceFont's `ExecutableDirectory()` and libSceNpEntitlementAccess's `EntitlementsPath()` use it on every non-Windows host, so finding the system fonts and the entitlements file threw on macOS. Both now have an `__APPLE__` branch: the path from `_NSGetExecutablePath` (size queried first), resolved with `std::filesystem::canonical`. That gives the same real folder `/proc/self/exe` gives on Linux, also when the executable is launched through a symlink or a relative path. A failure throws the message each file already uses on Windows. Linux and Windows are unchanged, and the `ANYPS5_ENTITLEMENTS` override still comes first. Not changed here: `ShaderDiskCache.cpp` also reads `/proc/self/exe`, but with an `error_code`, so on macOS the shader and pipeline disk caches are silently disabled instead of throwing (unless `ANYPS5_SHADER_CACHE_DIR` is set). `ModuleInfo.cpp`, `GuestAllocations.cpp` and the `GuestImageAnonymous` test also read it. Those are separate topics. ### Tested - MacBook Air M3(Apple silicon), macOS 26.0.1, AppleClang 17.0.0, x86_64 under Rosetta: - `read_symlink("/proc/self/exe")` throws `filesystem_error` (No such file or directory), the failure this fixes. - A standalone program with the same lookup code prints the real executable folder when launched by full path, as `./program` from its folder, and through a symlink in another folder (it resolves to the target's folder). - Both changed files compile for x86_64 macOS (`clang++ -std=c++20 -arch x86_64 -fsyntax-only`). The only warnings are the existing `-Wreturn-type-c-linkage` ones from `General.hpp`. - `tools/check_conventions.py --base upstream/main` passes. - Not run: the two libraries and their tests, since the full build does not complete on macOS yet; Linux and Windows builds (their code paths are unchanged). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Dextroid17 commented 2026-10-12 01:37:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:37:44Z.


The fix is correct and the API usage is right. Both read_symlink("/proc/self/exe") call sites genuinely had no macOS branch before this, and the new __APPLE__ path queries _NSGetExecutablePath's required buffer size first rather than guessing a fixed size — which matters, since the real API only fills the buffer when it's already large enough and otherwise just reports the needed size, so a fixed-size guess could have silently truncated a long path. Running the result through std::filesystem::canonical is the right call too, since Apple's own documentation for this API says the returned path "may be a symbolic link and not the real file" — canonical resolution is what actually makes this equivalent to what /proc/self/exe gives on Linux. The ANYPS5_ENTITLEMENTS override still runs first, and the Linux/Windows branches are untouched (purely additive diff).

No overlap with #4019 (reviewed earlier tonight) — that PR only touches ShaderDiskCache.cpp, which this PR's own body explicitly and correctly scopes out as a separate topic. Worth flagging instead: the real file-level conflicts here are with two other open PRs — #3891 and #4156 — which the repo's own overlap bot already lists as touching these same lines in SystemFont.cpp and Export.cpp. The checklist's "no other open PR implements the same functions" doesn't hold against those two; worth a look at whether they're duplicate attempts at the same fix or something narrower.

One edge case worth a mention, not a blocker: canonical resolves a relative launch path against the process's current working directory, so if the process chdir()s after launch, the result could diverge from what /proc/self/exe gives on Linux (which stays correct regardless of cwd changes). Untested in the PR, low-risk for this project's actual launch patterns.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116214364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4014#issuecomment-6116214364). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T01:37:44Z. --- The fix is correct and the API usage is right. Both `read_symlink("/proc/self/exe")` call sites genuinely had no macOS branch before this, and the new `__APPLE__` path queries `_NSGetExecutablePath`'s required buffer size first rather than guessing a fixed size — which matters, since the real API only fills the buffer when it's already large enough and otherwise just reports the needed size, so a fixed-size guess could have silently truncated a long path. Running the result through `std::filesystem::canonical` is the right call too, since Apple's own documentation for this API says the returned path "may be a symbolic link and not the real file" — canonical resolution is what actually makes this equivalent to what `/proc/self/exe` gives on Linux. The `ANYPS5_ENTITLEMENTS` override still runs first, and the Linux/Windows branches are untouched (purely additive diff). No overlap with #4019 (reviewed earlier tonight) — that PR only touches `ShaderDiskCache.cpp`, which this PR's own body explicitly and correctly scopes out as a separate topic. Worth flagging instead: the real file-level conflicts here are with two *other* open PRs — #3891 and #4156 — which the repo's own overlap bot already lists as touching these same lines in `SystemFont.cpp` and `Export.cpp`. The checklist's "no other open PR implements the same functions" doesn't hold against those two; worth a look at whether they're duplicate attempts at the same fix or something narrower. One edge case worth a mention, not a blocker: `canonical` resolves a relative launch path against the process's *current* working directory, so if the process `chdir()`s after launch, the result could diverge from what `/proc/self/exe` gives on Linux (which stays correct regardless of cwd changes). Untested in the PR, low-risk for this project's actual launch patterns. Written with AI assistance.
ahir061 commented 2026-10-12 07:20:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-12T07:20:13Z.


Thanks for the review.

Overlap: you're right, and I missed it. #3891 (a draft adding the native ARM64 AOT path, 210 files) includes the same lookup for these two call sites and for ShaderDiskCache.cpp, through a shared MacOsExecutablePath() helper in libc, and #4156 is stacked on it. This PR and #4019 are that fix on its own, so they can land independently of the AOT work. @yabanana, if one of these merges first, the matching hunks can drop out of #3891 on its next rebase; if maintainers would rather have the shared helper, I'm happy to switch to it or close these in favour of #3891.

chdir: in the tests above, _NSGetExecutablePath already returned an absolute path in every launch mode, including ./program and ./symlink from their folders (for example /private/tmp/macos_exedir/real/exedir), so canonical did not depend on the working directory there. Apple doesn't document that the path is always absolute, so the concern holds in principle.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6119620294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4014#issuecomment-6119620294). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-12T07:20:13Z. --- Thanks for the review. Overlap: you're right, and I missed it. #3891 (a draft adding the native ARM64 AOT path, 210 files) includes the same lookup for these two call sites and for ShaderDiskCache.cpp, through a shared `MacOsExecutablePath()` helper in libc, and #4156 is stacked on it. This PR and #4019 are that fix on its own, so they can land independently of the AOT work. @yabanana, if one of these merges first, the matching hunks can drop out of #3891 on its next rebase; if maintainers would rather have the shared helper, I'm happy to switch to it or close these in favour of #3891. chdir: in the tests above, `_NSGetExecutablePath` already returned an absolute path in every launch mode, including `./program` and `./symlink` from their folders (for example `/private/tmp/macos_exedir/real/exedir`), so `canonical` did not depend on the working directory there. Apple doesn't document that the path is always absolute, so the concern holds in principle.
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!4014
No description provided.