From 51e7adcbee23ec05295a7709ad4cd2c070335752 Mon Sep 17 00:00:00 2001 From: Xphalnos Date: Mon, 13 Oct 2025 21:05:43 +0200 Subject: [PATCH 1/2] Replace free() by munmap() Signed-off-by: Xphalnos --- src/host/memory/arena.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/host/memory/arena.cpp b/src/host/memory/arena.cpp index 7c1d8e2..89e8bc1 100644 --- a/src/host/memory/arena.cpp +++ b/src/host/memory/arena.cpp @@ -35,7 +35,6 @@ arena_t arena_init(size_t capacity) }; return arena; } -// new more memsafe code (ownedbywuigi) (i give up on windows compatibility for now, will stick to the old unsafe code) void* arena_allocate(memory::arena_t* arena, const std::size_t size) { @@ -55,14 +54,24 @@ void arena_reset(memory::arena_t* arena) void arena_free(memory::arena_t* arena) { PVM_ASSERT(arena != nullptr); + +#ifdef WIN32 + const int free = VirtualFree(arena->data, 0, MEM_RELEASE); + + PVM_ASSERT(free != 0); + + if (free == 0) + PVM_ASSERT_MSG(false, "Failed to free arena memory"); +#else + const int free = munmap(arena->data, arena->capacity); + + PVM_ASSERT(free == 0); + + if (free == -1) + PVM_ASSERT_MSG(false, "Failed to free arena memory"); +#endif + arena->capacity = 0; arena->size = 0; - - // TODO(GloriousTaco:memory): Replace free with a memory safe alternative. -#ifdef WIN32 - VirtualFree(arena->data, 0, MEM_RELEASE); -#else - free(arena->data); -#endif } } // namespace pound::host::memory From 194b15b55680860303800897329193c9612c8221 Mon Sep 17 00:00:00 2001 From: Ronald Caesar Date: Fri, 17 Oct 2025 16:28:02 -0400 Subject: [PATCH 2/2] host: add assertions Munmap can fail if the length argument is 0, and the address being freed is not a multiple of the host's page size. Signed-off-by: Ronald Caesar --- src/host/memory/arena.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/host/memory/arena.cpp b/src/host/memory/arena.cpp index 89e8bc1..a0b74ec 100644 --- a/src/host/memory/arena.cpp +++ b/src/host/memory/arena.cpp @@ -5,6 +5,7 @@ #ifdef WIN32 #include #else +#include #include #endif @@ -53,22 +54,26 @@ void arena_reset(memory::arena_t* arena) } void arena_free(memory::arena_t* arena) { - PVM_ASSERT(arena != nullptr); + PVM_ASSERT(nullptr != arena); + PVM_ASSERT(nullptr != arena->data); #ifdef WIN32 - const int free = VirtualFree(arena->data, 0, MEM_RELEASE); - - PVM_ASSERT(free != 0); - - if (free == 0) + size_t size = 0; + const int return_val = VirtualFree(arena->data, size, MEM_RELEASE); + if (0 == return_val) + { PVM_ASSERT_MSG(false, "Failed to free arena memory"); + } #else - const int free = munmap(arena->data, arena->capacity); - - PVM_ASSERT(free == 0); - - if (free == -1) + long page_size = sysconf(_SC_PAGESIZE); + PVM_ASSERT(page_size > 0); + PVM_ASSERT(arena->capacity > 0); + PVM_ASSERT(0 == ((uintptr_t)arena->data % (size_t)page_size)); + int return_val = munmap(arena->data, arena->capacity); + if (-1 == return_val) + { PVM_ASSERT_MSG(false, "Failed to free arena memory"); + } #endif arena->capacity = 0;