From dcae918b849b3e0c05d3c2bd90bda0621fa4aed4 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 1 Feb 2021 09:49:12 -0800 Subject: [PATCH 1/4] always do ASLR in secure mode even in debug mode (issue #289) --- src/os.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/os.c b/src/os.c index e2c93d72..3b4b4b4d 100644 --- a/src/os.c +++ b/src/os.c @@ -472,7 +472,7 @@ static void* mi_os_get_aligned_hint(size_t try_alignment, size_t size) { uintptr_t hint = mi_atomic_add_acq_rel(&aligned_base, size); if (hint == 0 || hint > ((intptr_t)30<<40)) { // try to wrap around after 30TiB (area after 32TiB is used for huge OS pages) uintptr_t init = ((uintptr_t)4 << 40); // start at 4TiB area - #if (MI_SECURE>0 || MI_DEBUG==0) // security: randomize start of aligned allocations unless in debug mode + #if (MI_SECURE>0) // security: randomize start of aligned allocations uintptr_t r = _mi_heap_random_next(mi_get_default_heap()); init = init + (MI_SEGMENT_SIZE * ((r>>17) & 0xFFFFF)); // (randomly 20 bits)*4MiB == 0 to 4TiB #endif @@ -1013,7 +1013,7 @@ static uint8_t* mi_os_claim_huge_pages(size_t pages, size_t* total_size) { if (start == 0) { // Initialize the start address after the 32TiB area start = ((uintptr_t)32 << 40); // 32TiB virtual start address -#if (MI_SECURE>0 || MI_DEBUG==0) // security: randomize start of huge pages unless in debug mode +#if (MI_SECURE>0) // security: randomize start of huge pages uintptr_t r = _mi_heap_random_next(mi_get_default_heap()); start = start + ((uintptr_t)MI_HUGE_OS_PAGE_SIZE * ((r>>17) & 0x0FFF)); // (randomly 12bits)*1GiB == between 0 to 4TiB #endif From 0091a641a75542d8b940196b2a01a32e3acb8e9c Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 1 Feb 2021 09:55:18 -0800 Subject: [PATCH 2/4] undo previous commit dcae918 due to wrong logic (issue #289) --- src/os.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/os.c b/src/os.c index 3b4b4b4d..e2c93d72 100644 --- a/src/os.c +++ b/src/os.c @@ -472,7 +472,7 @@ static void* mi_os_get_aligned_hint(size_t try_alignment, size_t size) { uintptr_t hint = mi_atomic_add_acq_rel(&aligned_base, size); if (hint == 0 || hint > ((intptr_t)30<<40)) { // try to wrap around after 30TiB (area after 32TiB is used for huge OS pages) uintptr_t init = ((uintptr_t)4 << 40); // start at 4TiB area - #if (MI_SECURE>0) // security: randomize start of aligned allocations + #if (MI_SECURE>0 || MI_DEBUG==0) // security: randomize start of aligned allocations unless in debug mode uintptr_t r = _mi_heap_random_next(mi_get_default_heap()); init = init + (MI_SEGMENT_SIZE * ((r>>17) & 0xFFFFF)); // (randomly 20 bits)*4MiB == 0 to 4TiB #endif @@ -1013,7 +1013,7 @@ static uint8_t* mi_os_claim_huge_pages(size_t pages, size_t* total_size) { if (start == 0) { // Initialize the start address after the 32TiB area start = ((uintptr_t)32 << 40); // 32TiB virtual start address -#if (MI_SECURE>0) // security: randomize start of huge pages +#if (MI_SECURE>0 || MI_DEBUG==0) // security: randomize start of huge pages unless in debug mode uintptr_t r = _mi_heap_random_next(mi_get_default_heap()); start = start + ((uintptr_t)MI_HUGE_OS_PAGE_SIZE * ((r>>17) & 0x0FFF)); // (randomly 12bits)*1GiB == between 0 to 4TiB #endif From c426ab4ea2c0be6257409be2c35afda5c87c42f9 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 1 Feb 2021 15:41:41 -0800 Subject: [PATCH 3/4] add condition to avoid compilation error on vs2015 (#issue 353) --- src/heap.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/heap.c b/src/heap.c index a9799dde..275af755 100644 --- a/src/heap.c +++ b/src/heap.c @@ -359,7 +359,9 @@ static void mi_heap_absorb(mi_heap_t* heap, mi_heap_t* from) { // turns out to be ok as `_mi_heap_delayed_free` only visits the list and calls a // the regular `_mi_free_delayed_block` which is safe. _mi_heap_delayed_free(from); + #if !defined(_MSC_VER) || (_MSC_VER > 1900) // somehow the following line gives an error in VS2015, issue #353 mi_assert_internal(mi_atomic_load_ptr_relaxed(mi_block_t,&from->thread_delayed_free) == NULL); + #endif // and reset the `from` heap mi_heap_reset_pages(from); From a7c33a3b0eae509463bde8a492470472fad1ca39 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 1 Feb 2021 15:47:22 -0800 Subject: [PATCH 4/4] fix getting the unique thread id on the Apple M1, see issue #354. --- include/mimalloc-internal.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index e87732ac..cf4d80ea 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -707,7 +707,7 @@ static inline void* mi_tls_slot(size_t slot) mi_attr_noexcept { res = tcb[slot]; #elif defined(__aarch64__) void** tcb; UNUSED(ofs); -#if defined(__APPLE__) // issue #343 +#if defined(__APPLE__) // M1, issue #343 __asm__ volatile ("mrs %0, tpidrro_el0" : "=r" (tcb)); #else __asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tcb)); @@ -734,7 +734,7 @@ static inline void mi_tls_slot_set(size_t slot, void* value) mi_attr_noexcept { tcb[slot] = value; #elif defined(__aarch64__) void** tcb; UNUSED(ofs); -#if defined(__APPLE__) // issue #343 +#if defined(__APPLE__) // M1, issue #343 __asm__ volatile ("mrs %0, tpidrro_el0" : "=r" (tcb)); #else __asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tcb)); @@ -744,8 +744,13 @@ static inline void mi_tls_slot_set(size_t slot, void* value) mi_attr_noexcept { } static inline uintptr_t _mi_thread_id(void) mi_attr_noexcept { - // in all our targets, slot 0 is the pointer to the thread control block +#if defined(__aarch64__) && defined(__APPLE__) // M1 + // on macOS on the M1, slot 0 does not seem to work, so we fall back to portable C for now. See issue #354 + return (uintptr_t)&_mi_heap_default; +#else + // in all our other targets, slot 0 is the pointer to the thread control block return (uintptr_t)mi_tls_slot(0); +#endif } #else // otherwise use standard C