MacOS: Do not leak MAP_JIT on utils::memory_decommit

This commit is contained in:
Elad 2026-05-09 22:02:21 +03:00
parent 4ef86989ae
commit a91234ce44
4 changed files with 14 additions and 9 deletions

View File

@ -316,10 +316,10 @@ void jit_runtime::finalize() noexcept
#endif
// Reset JIT memory
#ifdef CAN_OVERCOMMIT
utils::memory_reset(get_jit_memory(), 0x80000000);
utils::memory_reset(get_jit_memory(), 0x80000000, true);
utils::memory_protect(get_jit_memory(), 0x40000000, utils::protection::wx);
#else
utils::memory_decommit(get_jit_memory(), 0x80000000);
utils::memory_decommit(get_jit_memory(), 0x80000000, true);
#endif
s_code_pos = 0;

View File

@ -218,7 +218,7 @@ struct MemoryManager1 : llvm::RTDyldMemoryManager
// utils::memory_decommit(m_code_mems, how_much(code_ptr));
// utils::memory_decommit(m_data_ro_mems, how_much(data_ro_ptr));
// utils::memory_decommit(m_data_rw_mems, how_much(data_rw_ptr));
utils::memory_decommit(m_code_mems, c_max_size * 3);
utils::memory_decommit(m_code_mems, c_max_size * 3, true);
}
llvm::JITSymbol findSymbol(const std::string& name) override

View File

@ -44,10 +44,15 @@ namespace utils
void memory_commit(void* pointer, usz size, protection prot = protection::rw);
// Decommit all memory committed via commit_page_memory.
void memory_decommit(void* pointer, usz size);
void memory_decommit(void* pointer, usz size, bool can_be_jit = false);
// Decommit all memory and commit it again.
void memory_reset(void* pointer, usz size, protection prot = protection::rw);
void memory_reset(void* pointer, usz size, protection prot = protection::rw, bool can_be_jit = false);
inline void memory_reset(void* pointer, usz size, bool can_be_jit = false)
{
return memory_reset(pointer, size, protection::rw, can_be_jit);
}
// Free memory after reserved by memory_reserve, should specify original size
void memory_release(void* pointer, usz size);

View File

@ -335,7 +335,7 @@ namespace utils
#endif
}
void memory_decommit(void* pointer, usz size)
void memory_decommit(void* pointer, usz size, [[maybe_unused]] bool can_be_jit)
{
if (!size)
{
@ -352,7 +352,7 @@ namespace utils
// The Xcode manpage says the pointer is a hint and the OS will try to map at the hint location
// so this isn't completely undefined behavior.
ensure(::munmap(pointer, size) != -1);
ensure(::mmap(pointer, size, PROT_NONE, MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0) == pointer);
ensure(::mmap(pointer, size, PROT_NONE, MAP_ANON | MAP_PRIVATE | (can_be_jit ? MAP_JIT : 0), -1, 0) == pointer);
#else
ensure(::mmap(pointer, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE | c_map_noreserve, -1, 0) != reinterpret_cast<void*>(uptr{umax}));
#endif
@ -368,7 +368,7 @@ namespace utils
#endif
}
void memory_reset(void* pointer, usz size, protection prot)
void memory_reset(void* pointer, usz size, protection prot, [[maybe_unused]] bool can_be_jit)
{
if (!size)
{
@ -382,7 +382,7 @@ namespace utils
const u64 ptr64 = reinterpret_cast<u64>(pointer);
#if defined(__APPLE__) && defined(ARCH_ARM64)
ensure(::munmap(pointer, size) != -1);
ensure(::mmap(pointer, size, +prot, MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0) == pointer);
ensure(::mmap(pointer, size, +prot, MAP_ANON | MAP_PRIVATE | (can_be_jit ? MAP_JIT : 0), -1, 0) == pointer);
#else
ensure(::mmap(pointer, size, +prot, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0) != reinterpret_cast<void*>(uptr{umax}));
#endif