Merge pull request #347 from Pouare514/fix/kernel-nooverwrite-win

fix(libkernel): support NoOverwrite mappings on Windows
This commit is contained in:
Dean
2026-10-05 10:11:34 +13:00
committed by GitHub
2 changed files with 55 additions and 0 deletions
@@ -397,6 +397,32 @@ void* MapPlaced(void* addr, size_t len, int prot, int flags, size_t alignment) {
}
if ((flags & GuestMapFixed) != 0) {
ValidateRange(addr, len, alignment);
#ifdef _WIN32
if ((flags & GuestMapNoOverwrite) != 0) {
const auto start = reinterpret_cast<std::uintptr_t>(addr);
auto cursor = start;
while (cursor - start < len) {
MEMORY_BASIC_INFORMATION info{};
if (VirtualQuery(reinterpret_cast<LPCVOID>(cursor), &info, sizeof(info)) != sizeof(info))
throw std::system_error(EINVAL, std::generic_category(), "No-overwrite range query failed");
if (info.State == MEM_FREE) {
cursor = reinterpret_cast<std::uintptr_t>(info.BaseAddress) + info.RegionSize;
continue;
}
const auto regionEnd = reinterpret_cast<std::uintptr_t>(info.BaseAddress) + info.RegionSize;
if (regionEnd <= cursor) throw std::system_error(EINVAL, std::generic_category(), "No-overwrite range query failed");
const auto usedEnd = std::min(regionEnd, start + len);
const auto usedBegin = std::max(reinterpret_cast<std::uintptr_t>(info.BaseAddress), start);
if (info.State != MEM_RESERVE || !GuestArena::GuestArenaContains_nid_postfix(reinterpret_cast<void*>(usedBegin), usedEnd - usedBegin)) {
char busy[160];
std::snprintf(busy, sizeof(busy), "No-overwrite range %p+0x%zx is occupied (state=0x%lx prot=0x%lx)", addr, len,
(unsigned long)info.State, (unsigned long)info.Protect);
throw std::system_error(EEXIST, std::generic_category(), busy);
}
cursor = usedEnd;
}
}
#endif
#if defined(__linux__)
const int placement = (flags & GuestMapNoOverwrite) != 0 ? MAP_FIXED_NOREPLACE : MAP_FIXED;
#else
+29
View File
@@ -27,6 +27,9 @@
#include <sys/mman.h>
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
extern "C" {
void* APS5_VABI mmap_nid_postfix(void*, std::size_t, int, int, int, std::int64_t) noexcept;
@@ -179,6 +182,29 @@ static void CheckFixedVirtualReservation() {
Require(sceKernelMunmap(pooled, page * 2) == 0);
}
#ifdef _WIN32
static void CheckNoOverwriteRejectsHostOccupiedMapping() {
constexpr std::size_t page = 0x4000;
void* reservation = nullptr;
Require(sceKernelReserveVirtualRange(&reservation, page * 4, 0, 0) == 0);
Require(sceKernelMunmap(reservation, page * 4) == 0);
void* target = static_cast<unsigned char*>(reservation) + page;
GuestArena::GuestArenaCommit_nid_postfix(target, page, PAGE_READWRITE, page);
std::int64_t phys = 0;
Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page, 0, 0, &phys) == 0);
void* fixed = target;
bool rejected = false;
try {
rejected = sceKernelMapDirectMemory(&fixed, page, 3, 0x90, phys, 0) != 0;
} catch (const std::exception&) {
rejected = true;
}
Require(rejected);
GuestArena::GuestArenaReset_nid_postfix(target, page);
Require(sceKernelReleaseDirectMemory(phys, page) == 0);
}
#endif
static void CheckSharedDirectMemoryLifecycle() {
constexpr std::size_t page = 0x4000;
std::int64_t phys = 0;
@@ -562,6 +588,9 @@ int main() {
CheckFixedVirtualReservation();
CheckSharedDirectMemoryLifecycle();
CheckHeapAfterMappingReuse();
#ifdef _WIN32
CheckNoOverwriteRejectsHostOccupiedMapping();
#endif
CheckSharedWriteTracking();
CheckReadsIntoSharedWriteTracking();
#if defined(__linux__)