From 636afc654ad9bfbf950b54cae3c97c6922da489b Mon Sep 17 00:00:00 2001 From: Sebastian Ullrich Date: Tue, 13 Dec 2022 21:00:34 +0100 Subject: [PATCH] fix: avoid mapping .oleans in the way of the stack --- src/library/module.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/library/module.cpp b/src/library/module.cpp index ddbae3b1be..bb41bbe318 100644 --- a/src/library/module.cpp +++ b/src/library/module.cpp @@ -66,7 +66,9 @@ extern "C" LEAN_EXPORT object * lean_save_module_data(b_obj_arg fname, b_obj_arg size_t base_addr = name(mod, true).hash(); // x86-64 user space is currently limited to the lower 47 bits // https://en.wikipedia.org/wiki/X86-64#Virtual_address_space_details - base_addr = base_addr & ((1LL<<47) - 1); + // On Linux at least, the stack grows down from ~0x7fff... followed by shared libraries, so reserve + // a bit of space for them (0x7fff...-0x7f00... = 1TB) + base_addr = base_addr % 0x7f0000000000; // `mmap` addresses must be page-aligned. The default (non-huge) page size on x86-64 is 4KB. // `MapViewOfFileEx` addresses must be aligned to the "memory allocation granularity", which is 64KB. base_addr = base_addr & ~((1LL<<16) - 1);