diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 84ba3bd15..c557a3363 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/Syslog.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/CxxAbiSupport.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 7992a68b6..b53d8a265 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -8,6 +8,7 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/File/src/Terminal.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 @@ -158,6 +159,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..6b74a12f9 --- /dev/null +++ b/core/libs/tests/GuestKernelRename.cpp @@ -0,0 +1,65 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#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); + 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); + 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) { + 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("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)); + 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; +}