From fc457760a85b29bf7ea6e3c39c980f170fa61c62 Mon Sep 17 00:00:00 2001 From: ps54all Date: Wed, 7 Oct 2026 16:00:55 +0200 Subject: [PATCH 1/4] feat(libkernel): export the POSIX rename Titles that ship their own libc in sce_module import rename from libkernel (NID NN01qLRhiqU), where it was missing, so PPSA12544 failed to start. It checks both paths like unlink and maps sceKernelRename's result to errno. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libkernel/File/src/Stdio.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/libs/prx/libkernel/File/src/Stdio.cpp b/core/libs/prx/libkernel/File/src/Stdio.cpp index 109f729e7..899183c94 100644 --- a/core/libs/prx/libkernel/File/src/Stdio.cpp +++ b/core/libs/prx/libkernel/File/src/Stdio.cpp @@ -643,6 +643,12 @@ int APS5_VABI sceKernelRename(const char* from, const char* to) { return 0; } +int APS5_VABI rename_nid_postfix(const char* from, const char* to) { + if (const int error = PathError(from)) return PosixFailure(error); + if (const int error = PathError(to)) return PosixFailure(error); + return PosixResult(sceKernelRename(from, to)); +} + int APS5_VABI sceKernelRmdir(const char* path) { if (path == nullptr) throw std::invalid_argument("sceKernelRmdir: path is null"); const auto native = ResolvePath_nid_no_patch(path); From 372e2d4267e2e2446fdedf8d0794bdc679047613 Mon Sep 17 00:00:00 2001 From: ps54all Date: Thu, 8 Oct 2026 09:34:31 +0200 Subject: [PATCH 2/4] test(libkernel): cover the libkernel rename export directly guest_filesystem_tests links libkernel and libc, both of which define rename_nid_postfix, so which one it binds to depends on the loader and the suite passes either way. Resolve the symbol from libkernel.prx explicitly so the export added for titles that ship their own libc.prx is actually tested. Co-Authored-By: Claude Sonnet 5.5 --- core/libs/prx/libkernel/CMakeLists.txt | 10 +++++ core/libs/tests/GuestKernelRename.cpp | 52 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 core/libs/tests/GuestKernelRename.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 2520ab477..509b247f0 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -141,6 +141,16 @@ if(BUILD_TESTING) set_tests_properties(guest_kernel_sync PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_kernel_rename_tests ${LIBS_INCLUDE_DIR}/tests/GuestKernelRename.cpp) + target_include_directories(guest_kernel_rename_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_kernel_rename_tests PRIVATE libkernel libc) + target_compile_definitions(guest_kernel_rename_tests PRIVATE KERNEL_PRX_PATH="$") + configure_windows_unwind(guest_kernel_rename_tests) + add_test(NAME guest_kernel_rename COMMAND guest_kernel_rename_tests) + if(WIN32) + set_tests_properties(guest_kernel_rename PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_aio_tests ${LIBS_INCLUDE_DIR}/tests/GuestAio.cpp) target_include_directories(guest_aio_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_aio_tests PRIVATE libkernel libc) diff --git a/core/libs/tests/GuestKernelRename.cpp b/core/libs/tests/GuestKernelRename.cpp new file mode 100644 index 000000000..e83582979 --- /dev/null +++ b/core/libs/tests/GuestKernelRename.cpp @@ -0,0 +1,52 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#else +#include +#endif +extern "C" { +int* APS5_VABI __error_nid_postfix(); +} +using RenameFunction = int (APS5_VABI*)(const char*, const char*); +static RenameFunction LoadKernelRename() { +#ifdef _WIN32 + const HMODULE module = LoadLibraryA(KERNEL_PRX_PATH); + return module ? reinterpret_cast(reinterpret_cast(GetProcAddress(module, "rename_nid_postfix"))) : nullptr; +#else + void* module = dlopen(KERNEL_PRX_PATH, RTLD_NOW | RTLD_LOCAL); + return module ? reinterpret_cast(dlsym(module, "rename_nid_postfix")) : nullptr; +#endif +} +static void Check(bool value, int line) { + if (!value) { + std::fprintf(stderr, "Kernel rename check failed at line %d (guest errno %d)\n", line, *__error_nid_postfix()); + std::abort(); + } +} +#define Require(value) Check((value), __LINE__) +int main() { + const RenameFunction rename_nid_postfix = LoadKernelRename(); + if (rename_nid_postfix == nullptr) { std::fputs("libkernel does not export rename_nid_postfix\n", stderr); return 1; } + const auto root = std::filesystem::temp_directory_path() / ("aps5-kernel-rename-" + + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + Require(std::filesystem::create_directory(root)); + const auto file = root / "file.txt"; + const auto renamed = root / "renamed.txt"; + { std::ofstream stream(file); stream << "retained"; } + Require(rename_nid_postfix(file.string().c_str(), renamed.string().c_str()) == 0); + Require(!std::filesystem::exists(file) && std::filesystem::exists(renamed)); + Require(rename_nid_postfix(renamed.string().c_str(), renamed.string().c_str()) == 0); + Require(rename_nid_postfix(file.string().c_str(), renamed.string().c_str()) == -1 && *__error_nid_postfix() == 2); + Require(rename_nid_postfix(nullptr, renamed.string().c_str()) == -1 && *__error_nid_postfix() == 14); + Require(rename_nid_postfix(renamed.string().c_str(), nullptr) == -1 && *__error_nid_postfix() == 14); + Require(rename_nid_postfix("", renamed.string().c_str()) == -1 && *__error_nid_postfix() == 2); + Require(rename_nid_postfix(renamed.string().c_str(), "") == -1 && *__error_nid_postfix() == 2); + std::filesystem::remove_all(root); + return 0; +} From 1f580c8ddeba4b39e60573464e6ed945a1b6dcfb Mon Sep 17 00:00:00 2001 From: ps54all Date: Thu, 8 Oct 2026 11:36:53 +0200 Subject: [PATCH 3/4] test(libkernel): use a relative directory in the rename test The guest VFS on Linux does not map absolute host paths, so the test failed at the first rename there. Match GuestFilesystem.cpp and build the directory relative to the working directory. Co-Authored-By: Claude Sonnet 5.5 --- core/libs/tests/GuestKernelRename.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/libs/tests/GuestKernelRename.cpp b/core/libs/tests/GuestKernelRename.cpp index e83582979..f6486e433 100644 --- a/core/libs/tests/GuestKernelRename.cpp +++ b/core/libs/tests/GuestKernelRename.cpp @@ -33,7 +33,7 @@ static void Check(bool value, int line) { int main() { const RenameFunction rename_nid_postfix = LoadKernelRename(); if (rename_nid_postfix == nullptr) { std::fputs("libkernel does not export rename_nid_postfix\n", stderr); return 1; } - const auto root = std::filesystem::temp_directory_path() / ("aps5-kernel-rename-" + + const auto root = std::filesystem::path("anyps5-kernel-rename-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); Require(std::filesystem::create_directory(root)); const auto file = root / "file.txt"; From 1bd0ef8c96ad76b9314b963850913f84e9c2006c Mon Sep 17 00:00:00 2001 From: ps54all Date: Thu, 8 Oct 2026 16:05:01 +0200 Subject: [PATCH 4/4] refactor(libkernel): share one rename implementation with libc The repo allows one implementation per export. Move libc's rename into libc/src/Rename.cpp, compile that file into both libc and libkernel, and drop the second copy from libkernel's Stdio.cpp. FilesystemError moves to a header so the shared source can use it. The test now checks that the symbol it resolves belongs to libkernel (module handle on Windows, dladdr on Linux) so it fails when the export is removed. Co-Authored-By: Claude Sonnet 5.5 --- core/libs/prx/libc/CMakeLists.txt | 1 + .../libs/prx/libc/include/FilesystemError.hpp | 25 +++++++++++ core/libs/prx/libc/src/Filesystem.cpp | 41 +------------------ core/libs/prx/libc/src/Rename.cpp | 23 +++++++++++ core/libs/prx/libkernel/CMakeLists.txt | 1 + core/libs/prx/libkernel/File/src/Stdio.cpp | 6 --- core/libs/tests/GuestKernelRename.cpp | 19 +++++++-- 7 files changed, 67 insertions(+), 49 deletions(-) create mode 100644 core/libs/prx/libc/include/FilesystemError.hpp create mode 100644 core/libs/prx/libc/src/Rename.cpp diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 30aaf71ba..77c51f38f 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -32,6 +32,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Environment.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Directory.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Filesystem.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/Rename.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Errors.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/CxxAbiSupport.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/exception/Exports.cpp diff --git a/core/libs/prx/libc/include/FilesystemError.hpp b/core/libs/prx/libc/include/FilesystemError.hpp new file mode 100644 index 000000000..e31bedd2a --- /dev/null +++ b/core/libs/prx/libc/include/FilesystemError.hpp @@ -0,0 +1,25 @@ +#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_FILESYSTEMERROR_HPP +#define CORE_LIBS_PRX_LIBC_INCLUDE_FILESYSTEMERROR_HPP + +#include + +inline int FilesystemError(const std::error_code& error) { + if (error == std::errc::no_such_file_or_directory) return 2; + if (error == std::errc::permission_denied) return 13; + if (error == std::errc::operation_not_permitted) return 1; + if (error == std::errc::not_a_directory) return 20; + if (error == std::errc::is_a_directory) return 21; + if (error == std::errc::directory_not_empty) return 66; + if (error == std::errc::device_or_resource_busy) return 16; + if (error == std::errc::read_only_file_system) return 30; + if (error == std::errc::filename_too_long) return 63; + if (error == std::errc::too_many_symbolic_link_levels) return 62; + if (error == std::errc::not_enough_memory) return 12; + if (error == std::errc::invalid_argument) return 22; + if (error == std::errc::cross_device_link) return 18; + if (error == std::errc::file_exists) return 17; + if (error == std::errc::no_space_on_device) return 28; + return 5; +} + +#endif diff --git a/core/libs/prx/libc/src/Filesystem.cpp b/core/libs/prx/libc/src/Filesystem.cpp index 2e2d2bd3b..caaa9b69a 100644 --- a/core/libs/prx/libc/src/Filesystem.cpp +++ b/core/libs/prx/libc/src/Filesystem.cpp @@ -1,3 +1,4 @@ +#include "prx/libc/include/FilesystemError.hpp" #include "prx/libc/include/General.hpp" #include #include @@ -10,27 +11,6 @@ #include #endif -namespace { -int FilesystemError(const std::error_code& error) { - if (error == std::errc::no_such_file_or_directory) return 2; - if (error == std::errc::permission_denied) return 13; - if (error == std::errc::operation_not_permitted) return 1; - if (error == std::errc::not_a_directory) return 20; - if (error == std::errc::is_a_directory) return 21; - if (error == std::errc::directory_not_empty) return 66; - if (error == std::errc::device_or_resource_busy) return 16; - if (error == std::errc::read_only_file_system) return 30; - if (error == std::errc::filename_too_long) return 63; - if (error == std::errc::too_many_symbolic_link_levels) return 62; - if (error == std::errc::not_enough_memory) return 12; - if (error == std::errc::invalid_argument) return 22; - if (error == std::errc::cross_device_link) return 18; - if (error == std::errc::file_exists) return 17; - if (error == std::errc::no_space_on_device) return 28; - return 5; -} -} - extern "C" int APS5_VABI access_nid_postfix(const char* path, int mode) { if (!path) { errno = 14; return -1; } if (mode < 0 || (mode & ~7)) { errno = 22; return -1; } @@ -90,25 +70,6 @@ extern "C" int APS5_VABI access_nid_postfix(const char* path, int mode) { } } -extern "C" int APS5_VABI rename_nid_postfix(const char* from, const char* to) { - if (!from || !to) { errno = 14; return -1; } - if (!*from || !*to) { errno = 2; return -1; } - try { - const auto source = ResolvePath_nid_no_patch(from); - const auto destination = ResolvePath_nid_no_patch(to); - std::error_code error; - std::filesystem::rename(source, destination, error); - if (error) { errno = FilesystemError(error); return -1; } - RecordWrittenPath_nid_no_patch(source); - RecordWrittenPath_nid_no_patch(destination); - return 0; - } catch (const std::bad_alloc&) { errno = 12; return -1; } - catch (const std::filesystem::filesystem_error& error) { - errno = FilesystemError(error.code()); - return -1; - } -} - extern "C" int APS5_VABI remove_nid_postfix(const char* path) { if (!path) { errno = 14; return -1; } if (!*path) { errno = 2; return -1; } diff --git a/core/libs/prx/libc/src/Rename.cpp b/core/libs/prx/libc/src/Rename.cpp new file mode 100644 index 000000000..3e5c5658c --- /dev/null +++ b/core/libs/prx/libc/src/Rename.cpp @@ -0,0 +1,23 @@ +#include "prx/libc/include/FilesystemError.hpp" +#include "prx/libc/include/General.hpp" +#include +#include + +extern "C" int APS5_VABI rename_nid_postfix(const char* from, const char* to) { + if (!from || !to) { errno = 14; return -1; } + if (!*from || !*to) { errno = 2; return -1; } + try { + const auto source = ResolvePath_nid_no_patch(from); + const auto destination = ResolvePath_nid_no_patch(to); + std::error_code error; + std::filesystem::rename(source, destination, error); + if (error) { errno = FilesystemError(error); return -1; } + RecordWrittenPath_nid_no_patch(source); + RecordWrittenPath_nid_no_patch(destination); + return 0; + } catch (const std::bad_alloc&) { errno = 12; return -1; } + catch (const std::filesystem::filesystem_error& error) { + errno = FilesystemError(error.code()); + return -1; + } +} diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 509b247f0..8679e4440 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/File/src/NativeStat.cpp ${CMAKE_CURRENT_SOURCE_DIR}/File/src/Open.cpp ${CMAKE_CURRENT_SOURCE_DIR}/File/src/Stdio.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../libc/src/Rename.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Semaphore/src/Semaphore.cpp ${CMAKE_CURRENT_SOURCE_DIR}/SyncOnAddress/src/SyncOnAddress.cpp ${CMAKE_CURRENT_SOURCE_DIR}/DirectMemory/Export.cpp diff --git a/core/libs/prx/libkernel/File/src/Stdio.cpp b/core/libs/prx/libkernel/File/src/Stdio.cpp index 899183c94..109f729e7 100644 --- a/core/libs/prx/libkernel/File/src/Stdio.cpp +++ b/core/libs/prx/libkernel/File/src/Stdio.cpp @@ -643,12 +643,6 @@ int APS5_VABI sceKernelRename(const char* from, const char* to) { return 0; } -int APS5_VABI rename_nid_postfix(const char* from, const char* to) { - if (const int error = PathError(from)) return PosixFailure(error); - if (const int error = PathError(to)) return PosixFailure(error); - return PosixResult(sceKernelRename(from, to)); -} - int APS5_VABI sceKernelRmdir(const char* path) { if (path == nullptr) throw std::invalid_argument("sceKernelRmdir: path is null"); const auto native = ResolvePath_nid_no_patch(path); diff --git a/core/libs/tests/GuestKernelRename.cpp b/core/libs/tests/GuestKernelRename.cpp index f6486e433..6b74a12f9 100644 --- a/core/libs/tests/GuestKernelRename.cpp +++ b/core/libs/tests/GuestKernelRename.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -17,10 +18,22 @@ using RenameFunction = int (APS5_VABI*)(const char*, const char*); static RenameFunction LoadKernelRename() { #ifdef _WIN32 const HMODULE module = LoadLibraryA(KERNEL_PRX_PATH); - return module ? reinterpret_cast(reinterpret_cast(GetProcAddress(module, "rename_nid_postfix"))) : nullptr; + if (module == nullptr) return nullptr; + const auto function = reinterpret_cast(reinterpret_cast(GetProcAddress(module, "rename_nid_postfix"))); + HMODULE owner = nullptr; + if (function == nullptr || !GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(function), &owner) || owner != module) return nullptr; + return function; #else void* module = dlopen(KERNEL_PRX_PATH, RTLD_NOW | RTLD_LOCAL); - return module ? reinterpret_cast(dlsym(module, "rename_nid_postfix")) : nullptr; + if (module == nullptr) return nullptr; + void* function = dlsym(module, "rename_nid_postfix"); + Dl_info owner{}; + void* self = dlsym(module, "sceKernelRename"); + Dl_info expected{}; + if (function == nullptr || self == nullptr || !dladdr(function, &owner) || !dladdr(self, &expected) || + owner.dli_fname == nullptr || expected.dli_fname == nullptr || std::strcmp(owner.dli_fname, expected.dli_fname) != 0) return nullptr; + return reinterpret_cast(function); #endif } static void Check(bool value, int line) { @@ -32,7 +45,7 @@ static void Check(bool value, int line) { #define Require(value) Check((value), __LINE__) int main() { const RenameFunction rename_nid_postfix = LoadKernelRename(); - if (rename_nid_postfix == nullptr) { std::fputs("libkernel does not export rename_nid_postfix\n", stderr); return 1; } + if (rename_nid_postfix == nullptr) { std::fputs("rename_nid_postfix is not defined by libkernel\n", stderr); return 1; } const auto root = std::filesystem::path("anyps5-kernel-rename-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); Require(std::filesystem::create_directory(root));