diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 907294a56..567a2d100 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -216,5 +216,16 @@ if(BUILD_TESTING) target_include_directories(guest_directory_entry_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_directory_entry_tests PRIVATE libkernel libc) add_test(NAME guest_directory_entries COMMAND guest_directory_entry_tests) + else() + add_test_executable(guest_directory_close_tests ${LIBS_INCLUDE_DIR}/tests/GuestDirectoryClose.cpp) + target_include_directories(guest_directory_close_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_directory_close_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_directory_close_tests) + add_test(NAME guest_directory_close COMMAND guest_directory_close_tests) + set_tests_properties(guest_directory_close PROPERTIES TIMEOUT 15 ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + if(MINGW) + target_compile_options(guest_directory_close_tests PRIVATE -fasynchronous-unwind-tables) + endif() endif() endif() diff --git a/core/libs/prx/libkernel/File/src/Stdio.cpp b/core/libs/prx/libkernel/File/src/Stdio.cpp index 75696033a..67c64ec64 100644 --- a/core/libs/prx/libkernel/File/src/Stdio.cpp +++ b/core/libs/prx/libkernel/File/src/Stdio.cpp @@ -148,6 +148,7 @@ int APS5_VABI chmod_nid_postfix(const char* path, int mode) { int APS5_VABI close_nid_postfix(int d) { if (d >= GuestSockets::FirstDescriptor) return GuestSockets::Close(d); #ifdef _WIN32 + File::ForgetDirectoryDescriptor(d); return _close(d); #else return ::close(d); diff --git a/core/libs/tests/GuestDirectoryClose.cpp b/core/libs/tests/GuestDirectoryClose.cpp new file mode 100644 index 000000000..b4ab7b0fc --- /dev/null +++ b/core/libs/tests/GuestDirectoryClose.cpp @@ -0,0 +1,66 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include "prx/libkernel/File/include/FileFlags.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI sceKernelOpen(const char*, int, std::uint16_t); +int APS5_VABI sceKernelGetdents(int, char*, int); +int APS5_VABI close_nid_postfix(int); +int APS5_VABI _close_nid_postfix(int); +} + +static void Check(bool value, int line) { + if (!value) { + std::fprintf(stderr, "Directory close check failed at line %d\n", line); + std::abort(); + } +} +#define Require(value) Check((value), __LINE__) + +static constexpr int ErrorEnotdir = static_cast(0x80020014u); + +static void VerifyCloseReuse(const std::filesystem::path& root, const char* scenario, + bool useUnderscoreClose) { + const auto directoryPath = root / scenario; + Require(std::filesystem::create_directory(directoryPath)); + const auto filePath = directoryPath / "entry.txt"; + { + std::ofstream file(filePath, std::ios::binary); + Require(file.is_open()); + file << "entry"; + Require(static_cast(file)); + } + + const int directory = sceKernelOpen(directoryPath.string().c_str(), + SCE_KERNEL_O_RDONLY | SCE_KERNEL_O_DIRECTORY, 0); + Require(directory >= 0); + Require((useUnderscoreClose ? _close_nid_postfix(directory) : close_nid_postfix(directory)) == 0); + + const int file = sceKernelOpen(filePath.string().c_str(), SCE_KERNEL_O_RDONLY, 0); + Require(file >= 0); + Require(file == directory); + + std::array entries{}; + Require(sceKernelGetdents(file, entries.data(), static_cast(entries.size())) == ErrorEnotdir); + Require((useUnderscoreClose ? _close_nid_postfix(file) : close_nid_postfix(file)) == 0); +} + +int main() { + const auto root = std::filesystem::temp_directory_path() / + ("anyps5-directory-close-" + + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + Require(std::filesystem::create_directory(root)); + + VerifyCloseReuse(root, "close", false); + VerifyCloseReuse(root, "underscore-close", true); + + std::filesystem::remove_all(root); +}