libkernel: Windows lstat misreports symbolic links instead of throwing #2886

Closed
opened 2026-10-10 17:40:27 +00:00 by Amoenus · 0 comments
Amoenus commented 2026-10-10 17:40:27 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Amoenus on 2026-10-10T17:40:27Z.


Confirmed Windows regression

On unchanged main 57f96fd2b36563e8380c603a92b1ef0d9d3a6aa4, Windows lstat returns success with ordinary file/directory types for native symbolic links instead of reaching its explicit unsupported-link exception. Reproduced 5/5 runs. A fresh fetch still points main to that commit.

Windows 11 Enterprise 26200, WinLibs r7 MinGW-w64 GCC 15.2.0, Release, BUILD_TESTING=ON, Developer Mode enabled for creating links. No Linux or console claim.

The merged implementation in PR #1624 explicitly intended Windows symbolic links to throw. In DoLstat, its guard uses std::filesystem::is_symlink(std::filesystem::symlink_status(...)). On this toolchain, the native links report an ordinary file/directory type with no error. Native GetFileAttributesW includes FILE_ATTRIBUTE_REPARSE_POINT. The existing filesystem test exercises links only outside Windows.

Reproduction

Build full system libraries from the pinned main using the normal Windows instructions. Make a fixture under the probe's working directory with an ordinary target file and directory, then native relative links:

from pathlib import Path
import os

fixture = Path.cwd() / "lstat-fixture"
(fixture / "sub/deep").mkdir(parents=True)
(fixture / "sub/target.txt").write_text("target", encoding="utf-8")
os.symlink(r"sub\target.txt", fixture / "link.txt")
os.symlink(r"sub\deep", fixture / "linkdir", target_is_directory=True)
assert (fixture / "link.txt").read_text(encoding="utf-8") == "target"
assert (fixture / "linkdir").resolve() == (fixture / "sub/deep").resolve()

Use native backslashes in Windows reparse targets; native target access is verified above. The probe below dynamically calls the actual patched libkernel.prx export DRGXpDDh8Ng and uses the repository's FileStat and APS5_VABI definitions. Its ordinary-file control must succeed before the link checks. No implementation change or game-specific adapter is used.

Compile it from the source checkout with the same MinGW unwind setup used by the repository's runtime tests:

g++ -std=gnu++20 -O3 -fno-asynchronous-unwind-tables -I core/libs LstatSymlinkValidation-main.cpp -L build-review/core/libs/libs/implib -l libc -o LstatSymlinkValidation-main.exe
objcopy --rename-section .eh_frame=.ehfram LstatSymlinkValidation-main.exe

Run from the fixture's parent, with the absolute patched-library directory prepended to PATH, then pass that same directory and lstat-fixture as arguments. Using the repository's libc for unwinding matters when testing an exception-based fix.

#include "SceTypes.hpp"
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <stdexcept>
#include <string>
#include <windows.h>

using Lstat = int (APS5_VABI *)(const char*, FileStat*);
using Error = int* (APS5_VABI *)();
static Error guestError;

static bool Check(Lstat lstat, const std::string& path) {
    FileStat status;
    std::memset(&status, 0x5a, sizeof(status));
    try {
        const int result = lstat(path.c_str(), &status);
        std::printf("path=%s result=%d errno=%d mode=%06o size=%lld rejected=0\n", path.c_str(), result, guestError ? *guestError() : -1, status.st_mode, static_cast<long long>(status.st_size));
        return false;
    } catch (const std::runtime_error& error) {
        const bool exact = std::string(error.what()) == "lstat of a Windows symbolic link not implemented";
        bool unchanged = true;
        for (const auto value : std::string_view(reinterpret_cast<const char*>(&status), sizeof(status)))
            unchanged = unchanged && static_cast<unsigned char>(value) == 0x5a;
        std::printf("path=%s exception=%s unchanged=%d\n", path.c_str(), error.what(), unchanged);
        return exact && unchanged;
    }
}

int main(int argc, char** argv) {
    SetErrorMode(0x8003);
    if (argc != 3) return 20;
    const std::filesystem::path directory(argv[1]);
    if (!SetDllDirectoryW(directory.c_str())) return 21;
    const auto kernel = LoadLibraryW((directory / "libkernel.prx").c_str());
    if (kernel == nullptr) { std::printf("LoadLibrary error=%lu\n", GetLastError()); return 22; }
    const auto lstat = reinterpret_cast<Lstat>(GetProcAddress(kernel, "DRGXpDDh8Ng"));
    if (lstat == nullptr) return 23;
    const auto libc = LoadLibraryW((directory / "libc.prx").c_str());
    if (libc == nullptr) return 24;
    guestError = reinterpret_cast<Error>(GetProcAddress(libc, "9BcDykPmo1I"));
    std::printf("cwd=%s\n", std::filesystem::current_path().string().c_str());
    const std::string guest = std::string("/") + argv[2];
    FileStat ordinary{};
    std::printf("ordinary_result=%d\n", lstat((guest + "/sub/target.txt").c_str(), &ordinary));
    if ((ordinary.st_mode & 0170000) != 0100000) return 25;
    const bool file = Check(lstat, guest + "/link.txt");
    const bool directoryLink = Check(lstat, guest + "/linkdir");
    return file && directoryLink ? 0 : 1;
}

Observed

All five unchanged-main runs returned:

ordinary_result=0
.../link.txt result=0 mode=100666 size=0 rejected=0
.../linkdir  result=0 mode=040777 size=0 rejected=0

These are successful non-link classifications. The printed guest errno after success is not treated as an additional failure. A standalone host metadata probe also reports is_symlink=0, error 0, and native reparse-point attributes. This does not depend on realpath being implemented.

Suggested correction and local validation

A local diagnostic change checks GetFileAttributesW on Windows and throws when a successfully queried path has FILE_ATTRIBUTE_REPARSE_POINT. This rejects all Windows reparse points; it does not implement guest link metadata or host-to-guest target translation.

The expanded guest_filesystem test creates native file, directory, and dangling links, verifies the exact existing exception, and verifies that both guest errno and the entire output buffer remain untouched. Full system-library build passed; the test passed 5/5 runs, each below 0.1 s. Removing the guard made the expanded test fail; restoring it returned the test to 5/5. The same dynamic probe then received the exact exception for both links and verified untouched buffers. Conventions and test-registration checks passed. The Windows fixture requires Developer Mode or link creation privileges. No full-suite or Linux result is claimed.

This was discovered while validating startup gaps for PPSA99902, but no lstat call by that title has been observed. Startup still stops on unresolved imports before guest entry. Searches for existing lstat/symlink Windows issues and PRs found the merged implementation and related work, but no open report of this guard failure. No new PR is being created while intake is paused.

AI-assisted investigation and local changes: Codex. Original game files are unchanged.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5793719294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2886). Originally posted by [Amoenus](https://github.com/Amoenus) on 2026-10-10T17:40:27Z. --- ## Confirmed Windows regression On unchanged main `57f96fd2b36563e8380c603a92b1ef0d9d3a6aa4`, Windows `lstat` returns success with ordinary file/directory types for native symbolic links instead of reaching its explicit unsupported-link exception. Reproduced 5/5 runs. A fresh fetch still points main to that commit. Windows 11 Enterprise 26200, WinLibs r7 MinGW-w64 GCC 15.2.0, Release, BUILD_TESTING=ON, Developer Mode enabled for creating links. No Linux or console claim. The merged implementation in [PR #1624](https://github.com/boykopovar/AnyPS5/pull/1624) explicitly intended Windows symbolic links to throw. In [DoLstat](https://github.com/boykopovar/AnyPS5/blob/57f96fd2b36563e8380c603a92b1ef0d9d3a6aa4/core/libs/prx/libkernel/File/src/NativeStat.cpp#L46-L50), its guard uses `std::filesystem::is_symlink(std::filesystem::symlink_status(...))`. On this toolchain, the native links report an ordinary file/directory type with no error. Native `GetFileAttributesW` includes FILE_ATTRIBUTE_REPARSE_POINT. The existing filesystem test exercises links only outside Windows. ## Reproduction Build full system libraries from the pinned main using the normal Windows instructions. Make a fixture under the probe's working directory with an ordinary target file and directory, then native relative links: ```python from pathlib import Path import os fixture = Path.cwd() / "lstat-fixture" (fixture / "sub/deep").mkdir(parents=True) (fixture / "sub/target.txt").write_text("target", encoding="utf-8") os.symlink(r"sub\target.txt", fixture / "link.txt") os.symlink(r"sub\deep", fixture / "linkdir", target_is_directory=True) assert (fixture / "link.txt").read_text(encoding="utf-8") == "target" assert (fixture / "linkdir").resolve() == (fixture / "sub/deep").resolve() ``` Use native backslashes in Windows reparse targets; native target access is verified above. The probe below dynamically calls the actual patched `libkernel.prx` export DRGXpDDh8Ng and uses the repository's FileStat and APS5_VABI definitions. Its ordinary-file control must succeed before the link checks. No implementation change or game-specific adapter is used. Compile it from the source checkout with the same MinGW unwind setup used by the repository's runtime tests: ```text g++ -std=gnu++20 -O3 -fno-asynchronous-unwind-tables -I core/libs LstatSymlinkValidation-main.cpp -L build-review/core/libs/libs/implib -l libc -o LstatSymlinkValidation-main.exe objcopy --rename-section .eh_frame=.ehfram LstatSymlinkValidation-main.exe ``` Run from the fixture's parent, with the absolute patched-library directory prepended to PATH, then pass that same directory and `lstat-fixture` as arguments. Using the repository's libc for unwinding matters when testing an exception-based fix. ```cpp #include "SceTypes.hpp" #include <cstdio> #include <cstring> #include <filesystem> #include <stdexcept> #include <string> #include <windows.h> using Lstat = int (APS5_VABI *)(const char*, FileStat*); using Error = int* (APS5_VABI *)(); static Error guestError; static bool Check(Lstat lstat, const std::string& path) { FileStat status; std::memset(&status, 0x5a, sizeof(status)); try { const int result = lstat(path.c_str(), &status); std::printf("path=%s result=%d errno=%d mode=%06o size=%lld rejected=0\n", path.c_str(), result, guestError ? *guestError() : -1, status.st_mode, static_cast<long long>(status.st_size)); return false; } catch (const std::runtime_error& error) { const bool exact = std::string(error.what()) == "lstat of a Windows symbolic link not implemented"; bool unchanged = true; for (const auto value : std::string_view(reinterpret_cast<const char*>(&status), sizeof(status))) unchanged = unchanged && static_cast<unsigned char>(value) == 0x5a; std::printf("path=%s exception=%s unchanged=%d\n", path.c_str(), error.what(), unchanged); return exact && unchanged; } } int main(int argc, char** argv) { SetErrorMode(0x8003); if (argc != 3) return 20; const std::filesystem::path directory(argv[1]); if (!SetDllDirectoryW(directory.c_str())) return 21; const auto kernel = LoadLibraryW((directory / "libkernel.prx").c_str()); if (kernel == nullptr) { std::printf("LoadLibrary error=%lu\n", GetLastError()); return 22; } const auto lstat = reinterpret_cast<Lstat>(GetProcAddress(kernel, "DRGXpDDh8Ng")); if (lstat == nullptr) return 23; const auto libc = LoadLibraryW((directory / "libc.prx").c_str()); if (libc == nullptr) return 24; guestError = reinterpret_cast<Error>(GetProcAddress(libc, "9BcDykPmo1I")); std::printf("cwd=%s\n", std::filesystem::current_path().string().c_str()); const std::string guest = std::string("/") + argv[2]; FileStat ordinary{}; std::printf("ordinary_result=%d\n", lstat((guest + "/sub/target.txt").c_str(), &ordinary)); if ((ordinary.st_mode & 0170000) != 0100000) return 25; const bool file = Check(lstat, guest + "/link.txt"); const bool directoryLink = Check(lstat, guest + "/linkdir"); return file && directoryLink ? 0 : 1; } ``` ## Observed All five unchanged-main runs returned: ```text ordinary_result=0 .../link.txt result=0 mode=100666 size=0 rejected=0 .../linkdir result=0 mode=040777 size=0 rejected=0 ``` These are successful non-link classifications. The printed guest errno after success is not treated as an additional failure. A standalone host metadata probe also reports `is_symlink=0`, error 0, and native reparse-point attributes. This does not depend on realpath being implemented. ## Suggested correction and local validation A local diagnostic change checks GetFileAttributesW on Windows and throws when a successfully queried path has FILE_ATTRIBUTE_REPARSE_POINT. This rejects all Windows reparse points; it does not implement guest link metadata or host-to-guest target translation. The expanded `guest_filesystem` test creates native file, directory, and dangling links, verifies the exact existing exception, and verifies that both guest errno and the entire output buffer remain untouched. Full system-library build passed; the test passed 5/5 runs, each below 0.1 s. Removing the guard made the expanded test fail; restoring it returned the test to 5/5. The same dynamic probe then received the exact exception for both links and verified untouched buffers. Conventions and test-registration checks passed. The Windows fixture requires Developer Mode or link creation privileges. No full-suite or Linux result is claimed. This was discovered while validating startup gaps for PPSA99902, but no lstat call by that title has been observed. Startup still stops on unresolved imports before guest entry. Searches for existing lstat/symlink Windows issues and PRs found the merged implementation and related work, but no open report of this guard failure. No new PR is being created while intake is paused. AI-assisted investigation and local changes: Codex. Original game files are unchanged.
Sign in to join this conversation.
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#2886
No description provided.