fix(libc): realpath resolves symbolic links like FreeBSD #11269

Merged
Gruzzberry merged 1 commit from upstream-pr/11269 into main 2026-10-27 08:22:10 +00:00
Gruzzberry commented 2026-10-25 23:14:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-25T23:14:30Z.


What

realpath never looked at symbolic links: on Windows it returned the link's own name (/dir/link -> /dir/link), succeeded for a dangling link, and could not tell a loop. FreeBSD 11.4's realpath1 lstats each component and, for a link, replaces the component with the readlink text, then keeps resolving. This change follows that loop:

  • a link to a file or directory resolves to the target (link/inner.txt through a directory link gives sub/inner.txt), a chain of links resolves to the end, and .. after a link is taken against the target;
  • a dangling link fails with ENOENT and leaves the missing target path in resolved; a loop, or more than 33 links in one resolution (symlinks++ > MAXSYMLINKS), fails with ELOOP;
  • a path below a link to a file (file-link/x, file-link/) fails with ENOTDIR;
  • a link text of 1024 bytes or more, or a result that would reach PATH_MAX, fails with ENAMETOOLONG, and an empty link text with ENOENT, as in the source.
    A link whose target is absolute (a host path, which a guest symlink cannot create either) is not expanded: it keeps the previous behaviour, because there is no guest path to resolve it to, and no later component of that resolution is expanded either (the guest path still names the link, so a relative .. in a link beneath it would otherwise resolve against the wrong directory; this is also what main does, so abs-link/../file-link stays /file-link). Only Windows symbolic links (reparse tag IO_REPARSE_TAG_SYMLINK) are expanded; other reparse points are treated as before. On other hosts std::filesystem::read_symlink is used. Nothing else changes, including the backslash separators and the getcwd start.

Known limits: the expansion works on the guest path, so a link made on the host whose text climbs with .. above a path alias root (or an alias whose host directory is itself a relative link) is resolved against the guest tree rather than the host one, and can fail with ENOENT where main returned the unexpanded name. Links created by the guest cannot do this, since the guest symlink refuses .. and absolute targets.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, Developer Mode on, on main b90d009d9f (upstream has since moved on without touching this code).

  • docs/dev/debt/libc.md: the realpath entry now says relative links are expanded and that absolute-target links, anything below them, junctions and host links that climb above a path alias root are not.
  • guest_realpath: new checks next to the existing ones for a file link, a directory link (alone, with a child, with .., with a trailing /), a chain, a relative link containing .., a dangling link (ENOENT and the trouble path), a loop (ELOOP), a path below a file link (ENOTDIR), and a chain of 33 links (resolves) against 34 (ELOOP), and a directory link with an absolute target (its children, including a relative link, resolve as before). The block is skipped, without failing, when links cannot be created, and it now also compiles for other hosts, where the links are made with std::filesystem::create_symlink (not run by me; an equivalent port of these checks was run on Linux by wachi35676 and passed with this change). On main the test fails; with this change it passes, three runs out of three.
  • Full suite: the only failures are pr_overlap_encoding, agc_host_import_cut_lifetime and agc_driver_depth_plane_partial_store, which fail on main in this environment too; agc_import_settle_initial_dcc timed out under ctest -j 12 and passes alone three times out of three.
  • python tools/check_conventions.py --base upstream/main: passes. tools/pr_overlap.py: no overlap with open pull requests.
  • Not tested by me: Linux and macOS (the read_symlink branch), a console, a title run. readlink and readlinkat were probed as well (short buffer, non-link EINVAL, null buffer) and the existing tests already cover them, so they are unchanged.

Overlaps

None reported by tools/pr_overlap.py.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none; merge order: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009664035 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11269). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T23:14:30Z. --- ### What `realpath` never looked at symbolic links: on Windows it returned the link's own name (`/dir/link -> /dir/link`), succeeded for a dangling link, and could not tell a loop. FreeBSD 11.4's [`realpath1`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdlib/realpath.c) `lstat`s each component and, for a link, replaces the component with the `readlink` text, then keeps resolving. This change follows that loop: - a link to a file or directory resolves to the target (`link/inner.txt` through a directory link gives `sub/inner.txt`), a chain of links resolves to the end, and `..` after a link is taken against the target; - a dangling link fails with `ENOENT` and leaves the missing target path in `resolved`; a loop, or more than 33 links in one resolution (`symlinks++ > MAXSYMLINKS`), fails with `ELOOP`; - a path below a link to a file (`file-link/x`, `file-link/`) fails with `ENOTDIR`; - a link text of 1024 bytes or more, or a result that would reach `PATH_MAX`, fails with `ENAMETOOLONG`, and an empty link text with `ENOENT`, as in the source. A link whose target is absolute (a host path, which a guest `symlink` cannot create either) is not expanded: it keeps the previous behaviour, because there is no guest path to resolve it to, and no later component of that resolution is expanded either (the guest path still names the link, so a relative `..` in a link beneath it would otherwise resolve against the wrong directory; this is also what `main` does, so `abs-link/../file-link` stays `/file-link`). Only Windows symbolic links (reparse tag `IO_REPARSE_TAG_SYMLINK`) are expanded; other reparse points are treated as before. On other hosts `std::filesystem::read_symlink` is used. Nothing else changes, including the backslash separators and the `getcwd` start. Known limits: the expansion works on the guest path, so a link made on the host whose text climbs with `..` above a path alias root (or an alias whose host directory is itself a relative link) is resolved against the guest tree rather than the host one, and can fail with `ENOENT` where `main` returned the unexpanded name. Links created by the guest cannot do this, since the guest `symlink` refuses `..` and absolute targets. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, Developer Mode on, on `main` b90d009d9f (upstream has since moved on without touching this code). - `docs/dev/debt/libc.md`: the `realpath` entry now says relative links are expanded and that absolute-target links, anything below them, junctions and host links that climb above a path alias root are not. - `guest_realpath`: new checks next to the existing ones for a file link, a directory link (alone, with a child, with `..`, with a trailing `/`), a chain, a relative link containing `..`, a dangling link (`ENOENT` and the trouble path), a loop (`ELOOP`), a path below a file link (`ENOTDIR`), and a chain of 33 links (resolves) against 34 (`ELOOP`), and a directory link with an absolute target (its children, including a relative link, resolve as before). The block is skipped, without failing, when links cannot be created, and it now also compiles for other hosts, where the links are made with `std::filesystem::create_symlink` (not run by me; an equivalent port of these checks was run on Linux by wachi35676 and passed with this change). On `main` the test fails; with this change it passes, three runs out of three. - Full suite: the only failures are `pr_overlap_encoding`, `agc_host_import_cut_lifetime` and `agc_driver_depth_plane_partial_store`, which fail on `main` in this environment too; `agc_import_settle_initial_dcc` timed out under `ctest -j 12` and passes alone three times out of three. - `python tools/check_conventions.py --base upstream/main`: passes. `tools/pr_overlap.py`: no overlap with open pull requests. - Not tested by me: Linux and macOS (the `read_symlink` branch), a console, a title run. `readlink` and `readlinkat` were probed as well (short buffer, non-link `EINVAL`, null buffer) and the existing tests already cover them, so they are unchanged. ### Overlaps None reported by `tools/pr_overlap.py`. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none; merge order: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 23:26:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:26:02Z.

Review: CHANGES_REQUESTED


Re-reviewed exact head eb1e38f442e7cf7e8753d5ff89b10dbec3ed0a56. The relative-link expansion and Windows regressions cover useful cases, and the hosted conventions, policy, relinker matrix and ASan checks now pass. One repository documentation gap remains: ReadGuestLink() explicitly throws for absolute symlink targets, but docs/dev/debt/libc.md is unchanged and still says symlinks are not expanded at all. That entry no longer describes the implementation or its remaining limitation. Please update it to distinguish supported relative links from unsupported absolute targets (and add a focused regression for the unsupported path, if that is the intended boundary), or implement absolute guest-target resolution. I could not run the changed code locally: the test additions are Windows-only, and this checkout has no configured build directory. Full product jobs are skipped by path policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645868047 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11269#pullrequestreview-5645868047). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:26:02Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `eb1e38f442e7cf7e8753d5ff89b10dbec3ed0a56`. The relative-link expansion and Windows regressions cover useful cases, and the hosted conventions, policy, relinker matrix and ASan checks now pass. One repository documentation gap remains: `ReadGuestLink()` explicitly throws for absolute symlink targets, but `docs/dev/debt/libc.md` is unchanged and still says symlinks are not expanded at all. That entry no longer describes the implementation or its remaining limitation. Please update it to distinguish supported relative links from unsupported absolute targets (and add a focused regression for the unsupported path, if that is the intended boundary), or implement absolute guest-target resolution. I could not run the changed code locally: the test additions are Windows-only, and this checkout has no configured build directory. Full product jobs are skipped by path policy.
MarcZX10 commented 2026-10-25 23:33:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:33:04Z.

Review: CHANGES_REQUESTED


Re-reviewed exact head eb6040b530ff07a6a5a14f47d5879bd97125b7dd. The PR description now explains the absolute-target boundary, but the repository docs/dev/debt/libc.md entry is unchanged and still says symlinks are never expanded. It no longer describes this implementation, which now resolves relative links and preserves the old behavior for absolute host targets. Please update that debt entry to reflect the supported relative-link behavior and the absolute-target limitation; the repository rules require known gaps to be recorded there. Hosted conventions/policy checks pass; cross-platform relinker and macOS checks are still running, full product jobs are skipped, and I could not run this Windows-only test change locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645903768 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11269#pullrequestreview-5645903768). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:33:04Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `eb6040b530ff07a6a5a14f47d5879bd97125b7dd`. The PR description now explains the absolute-target boundary, but the repository `docs/dev/debt/libc.md` entry is unchanged and still says symlinks are never expanded. It no longer describes this implementation, which now resolves relative links and preserves the old behavior for absolute host targets. Please update that debt entry to reflect the supported relative-link behavior and the absolute-target limitation; the repository rules require known gaps to be recorded there. Hosted conventions/policy checks pass; cross-platform relinker and macOS checks are still running, full product jobs are skipped, and I could not run this Windows-only test change locally.
MarcZX10 commented 2026-10-25 23:40:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:40:37Z.

Review: CHANGES_REQUESTED


Re-reviewed exact head 481ae30a4c5d99ee355cf858f424d00bfdd443a3. The Windows tests now cover relative file/directory links, chains, .., dangling links, loops, and the 33/34-link boundary. Two issues remain before I can approve:

  1. The required debt entry in docs/dev/debt/libc.md is unchanged and still says host symlinks are followed for existence/type but never expanded. This implementation now expands relative links, so the entry no longer describes realpath or its remaining limits. Please update it to state what relative and absolute targets do and the host-link boundary.
  2. The new tests are entirely under _WIN32, but ReadGuestLink has a separate std::filesystem::read_symlink implementation for Linux/macOS and the PR says those hosts were not tested. Please add focused coverage for that branch (at least relative resolution, a dangling link and a loop), or explain why those hosts are outside the supported behavior.

The body documents the absolute-target and host-root limitations, but those notes do not replace the repository debt entry. The policy, conventions and product-build checks I can see do not exercise the changed libc behavior; the PR reports Windows tests only. I have not independently run the Windows suite.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645947553 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11269#pullrequestreview-5645947553). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:40:37Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `481ae30a4c5d99ee355cf858f424d00bfdd443a3`. The Windows tests now cover relative file/directory links, chains, `..`, dangling links, loops, and the 33/34-link boundary. Two issues remain before I can approve: 1. The required debt entry in `docs/dev/debt/libc.md` is unchanged and still says host symlinks are followed for existence/type but never expanded. This implementation now expands relative links, so the entry no longer describes `realpath` or its remaining limits. Please update it to state what relative and absolute targets do and the host-link boundary. 2. The new tests are entirely under `_WIN32`, but `ReadGuestLink` has a separate `std::filesystem::read_symlink` implementation for Linux/macOS and the PR says those hosts were not tested. Please add focused coverage for that branch (at least relative resolution, a dangling link and a loop), or explain why those hosts are outside the supported behavior. The body documents the absolute-target and host-root limitations, but those notes do not replace the repository debt entry. The policy, conventions and product-build checks I can see do not exercise the changed libc behavior; the PR reports Windows tests only. I have not independently run the Windows suite.
MarcZX10 commented 2026-10-26 06:35:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T06:35:17Z.

Review: APPROVED


Re-reviewed exact head 4ccb760d12ecb78bdbe3e02b2eb0e8cf4438a4e7. The debt entry now describes the supported relative-link behavior and remaining absolute-target/host-link limits, and the added regression block now covers both Windows and POSIX symlink paths. The independent Windows and Linux focused runs report guest_realpath passing 3/3; the current hosted conventions, policy, relinker, register-context, write-watch and ASan checks pass. Product Linux/Windows jobs are skipped by path policy. I found no remaining blocking issue in the reviewed diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5648199665 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11269#pullrequestreview-5648199665). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T06:35:17Z. **Review:** APPROVED --- Re-reviewed exact head `4ccb760d12ecb78bdbe3e02b2eb0e8cf4438a4e7`. The debt entry now describes the supported relative-link behavior and remaining absolute-target/host-link limits, and the added regression block now covers both Windows and POSIX symlink paths. The independent Windows and Linux focused runs report `guest_realpath` passing 3/3; the current hosted conventions, policy, relinker, register-context, write-watch and ASan checks pass. Product Linux/Windows jobs are skipped by path policy. I found no remaining blocking issue in the reviewed diff.
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!11269
No description provided.