From 627406e033de5a49272171956961e90713cc0661 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 9 Jan 2024 18:25:25 +0900 Subject: [PATCH 1/4] posix: enable MAP_32BIT for macOS --- core/shared/platform/common/posix/posix_memmap.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index 583557bb85..505fa7bf0c 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -77,10 +77,8 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) map_prot |= PROT_EXEC; #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) -#ifndef __APPLE__ if (flags & MMAP_MAP_32BIT) map_flags |= MAP_32BIT; -#endif #endif if (flags & MMAP_MAP_FIXED) From ab7951e6f88874e6cb78f26fb2b616f25629ee16 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 9 Jan 2024 18:26:27 +0900 Subject: [PATCH 2/4] config_common.cmake: reduce the size of pagezero for macOS to make a room for MAP_32BIT mmap. --- build-scripts/config_common.cmake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 927c2e7c00..c6df1c8ce4 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -451,3 +451,7 @@ if (WAMR_BUILD_LINUX_PERF EQUAL 1) add_definitions (-DWASM_ENABLE_LINUX_PERF=1) message (" Enable linux perf support") endif () + +if (APPLE) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-pagezero_size,0x4000") +endif () From 59ffad47c66a73260987e77958b1759be9f757e7 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 9 Jan 2024 18:31:53 +0900 Subject: [PATCH 3/4] posix: fall back to non-MAP_32BIT mmap for macOS On macOS, by default, the first 4GB is occupied by the pagezero. While it can be controlled with link time options, as we are an library, we usually don't have a control on how to link an executable. --- core/shared/platform/common/posix/posix_memmap.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index 505fa7bf0c..128f16e220 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -84,6 +84,12 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) if (flags & MMAP_MAP_FIXED) map_flags |= MAP_FIXED; +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) +#if defined(__APPLE__) +retry_without_map_32bit: +#endif +#endif + #if defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64) /* As AOT relocation in RISCV64 may require that the code/data mapped * is in range 0 to 2GB, we try to map the memory with hint address @@ -141,6 +147,14 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) } if (addr == MAP_FAILED) { +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) +#if defined(__APPLE__) + if ((map_flags & MAP_32BIT) != 0) { + map_flags &= ~MAP_32BIT; + goto retry_without_map_32bit; + } +#endif +#endif #if BH_ENABLE_TRACE_MMAP != 0 os_printf("mmap failed\n"); #endif From f8853fdf324dd32d297d12310930d7dd4ac06712 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 10 Jan 2024 12:13:03 +0900 Subject: [PATCH 4/4] add a comment --- build-scripts/config_common.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index c6df1c8ce4..ca4935e3a8 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -453,5 +453,7 @@ if (WAMR_BUILD_LINUX_PERF EQUAL 1) endif () if (APPLE) + # On recent macOS versions, by default, the size of page zero is 4GB. + # Shrink it to make MAP_32BIT mmap can work. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-pagezero_size,0x4000") endif ()