diff --git a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp index c9f06f7af..6e8fc15b4 100644 --- a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp @@ -228,11 +228,11 @@ public: section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, static_cast(size >> 32), static_cast(size), nullptr); if (!section) throw std::system_error(static_cast(GetLastError()), std::system_category(), "create direct memory backing"); #else - file = std::tmpfile(); - if (!file) throw std::system_error(errno, std::generic_category(), "create direct memory backing"); - if (ftruncate(fileno(file), static_cast(bytes)) != 0) { + file = memfd_create("direct memory", MFD_CLOEXEC); + if (file < 0) throw std::system_error(errno, std::generic_category(), "create direct memory backing"); + if (ftruncate(file, static_cast(bytes)) != 0) { const int error = errno; - std::fclose(file); + ::close(file); throw std::system_error(error, std::generic_category(), "size direct memory backing"); } #endif @@ -244,7 +244,7 @@ public: #ifdef _WIN32 CloseHandle(section); #else - std::fclose(file); + ::close(file); #endif } @@ -255,7 +255,7 @@ public: #ifdef _WIN32 GuestArena::GuestArenaMap_nid_postfix(reinterpret_cast(address), bytes, section, offset, WinProtFromPosix(protection)); #else - if (::mmap(reinterpret_cast(address), bytes, protection, MAP_SHARED | MAP_FIXED, fileno(file), static_cast(offset)) == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map direct memory backing"); + if (::mmap(reinterpret_cast(address), bytes, protection, MAP_SHARED | MAP_FIXED, file, static_cast(offset)) == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map direct memory backing"); #endif } @@ -264,7 +264,7 @@ private: #ifdef _WIN32 HANDLE section = nullptr; #else - std::FILE* file = nullptr; + int file = -1; #endif }; diff --git a/core/libs/tests/GuestMemory.cpp b/core/libs/tests/GuestMemory.cpp index 1d88de07c..94fd6eb02 100644 --- a/core/libs/tests/GuestMemory.cpp +++ b/core/libs/tests/GuestMemory.cpp @@ -16,6 +16,8 @@ #include #include #if defined(__linux__) +#include +#include #include #include #endif @@ -373,6 +375,30 @@ static void CheckWriteWatch() { Require(munmap(raw, spanned + tableSpan) == 0); } +static std::string BackingOf(const void* address) { + std::ifstream maps("/proc/self/maps"); + std::string line; + while (std::getline(maps, line)) { + if (std::strtoull(line.c_str(), nullptr, 16) != reinterpret_cast(address)) continue; + const auto path = line.find('/'); + return path == std::string::npos ? std::string() : line.substr(path); + } + return {}; +} + +static void CheckDirectMemoryBackingNeedsNoFilesystem() { + constexpr std::size_t page = 0x4000; + std::int64_t phys = 0; + Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page, 0, 0, &phys) == 0); + void* mapped = nullptr; + Require(sceKernelMapDirectMemory(&mapped, page, 3, 0, phys, 0) == 0); + const auto backing = BackingOf(mapped); + if (backing.rfind("/memfd:", 0) != 0) std::fprintf(stderr, "direct memory backing: %s\n", backing.c_str()); + Require(backing.rfind("/memfd:", 0) == 0); + Require(sceKernelMunmap(mapped, page) == 0); + Require(sceKernelReleaseDirectMemory(phys, page) == 0); +} + static void CheckDirectMemoryWriteWatch() { if (!GuestWriteWatch::GuestWriteWatchAvailable_nid_postfix()) { std::puts("write watch unavailable: direct memory not tested"); @@ -434,6 +460,7 @@ int main() { #if defined(__linux__) CheckWriteWatch(); CheckDirectMemoryWriteWatch(); + CheckDirectMemoryBackingNeedsNoFilesystem(); #endif constexpr std::size_t page = 0x4000; const auto failed = reinterpret_cast(static_cast(-1));