microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.53k stars 852 forks source link

Crash while running mimalloc-test-stress on Apple M1 Macbook Pro #380

Closed folowing closed 3 years ago

folowing commented 3 years ago

mimalloc version: 2.0.0 or dev-slice

Crash Reports

Process:               mimalloc-test-stress [67182]
Path:                  /private/tmp/*/mimalloc-test-stress
Identifier:            mimalloc-test-stress
Version:               0
Code Type:             ARM-64 (Native)
Parent Process:        zsh [65512]
Responsible:           iTerm2 [2374]
User ID:               501

Date/Time:             2021-03-27 22:42:36.701 +0800
OS Version:            macOS 11.2.3 (20D91)
Report Version:        12

Time Awake Since Boot: 560000 seconds
Time Since Wake:       4400 seconds

System Integrity Protection: enabled

Crashed Thread:        0

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
dyld: launch, running initializers
/usr/lib/libSystem.B.dylib
abort() called

Thread 0 Crashed:
0   libsystem_kernel.dylib          0x0000000186b0ccec __pthread_kill + 8
1   libsystem_pthread.dylib         0x0000000186b3dc24 pthread_kill + 292
2   libsystem_c.dylib               0x0000000186a85864 abort + 104
3   libdyld.dylib                   0x0000000186b5cd38 _tlv_bootstrap + 16
4   libmimalloc.2.0.dylib           0x0000000100bf2cd0 mi_process_init + 136
5   libmimalloc.2.0.dylib           0x0000000100bf2a58 mi_thread_init + 24
6   libmimalloc.2.0.dylib           0x0000000100becbbc _mi_malloc_generic + 280
7   libmimalloc.2.0.dylib           0x0000000100beeef8 _mi_heap_malloc_zero + 180
8   libkeymgr.dylib                 0x00000001901a2a7c dwarf2_unwind_dyld_add_image_hook + 40
9   dyld                            0x0000000100cfcd94 dyld::registerAddCallback(void (*)(mach_header const*, long)) + 400
10  libdyld.dylib                   0x0000000186b441a8 _dyld_register_func_for_add_image + 132
11  libkeymgr.dylib                 0x00000001901a2798 __keymgr_initializer + 32
12  libSystem.B.dylib               0x00000001901b37d0 libSystem_initializer + 196
13  dyld                            0x0000000100d1790c ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 868
14  dyld                            0x0000000100d17b94 ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 56
15  dyld                            0x0000000100d1184c ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 620
16  dyld                            0x0000000100d11794 ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 436
17  dyld                            0x0000000100d11794 ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 436
18  dyld                            0x0000000100d0f300 ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 192
19  dyld                            0x0000000100d0f3cc ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 96
20  dyld                            0x0000000100cfa84c dyld::initializeMainExecutable() + 220
21  dyld                            0x0000000100d00b98 dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 7388
22  dyld                            0x0000000100cf9258 dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 476
23  dyld                            0x0000000100cf9038 _dyld_start + 56

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x0000000000000000   x5: 0x0000000000000000   x6: 0x0000000000000000   x7: 0x0000000000000340
    x8: 0x00000000000005b9   x9: 0xbc9f1445971308a1  x10: 0xfffffffeff40d033  x11: 0x00000001f47a2858
   x12: 0x00000001f47a2858  x13: 0x000000000000003c  x14: 0x0000000000000881  x15: 0x000000008000001f
   x16: 0x0000000000000148  x17: 0x00000001ec783330  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000100dafd40  x21: 0x0000000000000303  x22: 0x0000000100dafe20  x23: 0x000000016f249760
   x24: 0x000000001f070018  x25: 0x0000000100d7c000  x26: 0x000000016f2498d8  x27: 0x000000016f249948
   x28: 0x000000016f24a580   fp: 0x000000016f2495d0   lr: 0x0000000186b3dc24
    sp: 0x000000016f2495b0   pc: 0x0000000186b0ccec cpsr: 0x40000000
   far: 0x0000000100c5bd40  esr: 0x56000080

Binary Images:
       0x100bb4000 -        0x100bb7fff +mimalloc-test-stress (0) <DEF65874-D429-333D-8B0C-59145207B452> /tmp/*/mimalloc-test-stress
       0x100be0000 -        0x100bf3fff +libmimalloc.2.0.dylib (0) <824109EA-08A4-39BC-92D7-EDB9F478680D> /tmp/*/libmimalloc.2.0.dylib
       0x100cf8000 -        0x100d73fff  dyld (832.7.3) <4AB185B3-DC20-3C03-A193-67C0E6C589D7> /usr/lib/dyld
       0x18689a000 -        0x18689bfff  libsystem_blocks.dylib (78) <9B6D4883-03E9-3785-851E-EA79FA64ADC1> /usr/lib/system/libsystem_blocks.dylib
       0x18689c000 -        0x1868d3fff  libxpc.dylib (2038.80.3) <BD0DFD42-0DC3-3F3D-9C04-5A2B3D93794D> /usr/lib/system/libxpc.dylib
       0x1868d4000 -        0x1868ebfff  libsystem_trace.dylib (1277.80.2) <4A466196-D2DD-367B-80AB-988F281EC3B8> /usr/lib/system/libsystem_trace.dylib
       0x1868ec000 -        0x186960fff  libcorecrypto.dylib (1000.80.5) <9BD8FED7-2A36-3602-A5A7-0CA87C03FB84> /usr/lib/system/libcorecrypto.dylib
       0x186961000 -        0x18698cfff  libsystem_malloc.dylib (317.40.8) <21120432-52C1-34E4-BF01-623722FA3A41> /usr/lib/system/libsystem_malloc.dylib
       0x18698d000 -        0x1869d0fff  libdispatch.dylib (1271.40.12) <F5BFBD55-EF70-3659-854D-9061325EB26D> /usr/lib/system/libdispatch.dylib
       0x1869d1000 -        0x186a09fff  libobjc.A.dylib (818.2) <B03625B0-501E-3AC1-8E16-08B621120EAD> /usr/lib/libobjc.A.dylib
       0x186a0a000 -        0x186a0cfff  libsystem_featureflags.dylib (28.60.1) <297CC4DD-AFA4-3BA3-B4E1-0DF47E49C21E> /usr/lib/system/libsystem_featureflags.dylib
       0x186a0d000 -        0x186a8dfff  libsystem_c.dylib (1439.40.11) <A7147E08-E7C0-3842-916E-F2270A689F47> /usr/lib/system/libsystem_c.dylib
       0x186a8e000 -        0x186ae9fff  libc++.1.dylib (904.4) <B139607F-1E80-3A8E-870D-0AC022069EA1> /usr/lib/libc++.1.dylib
       0x186aea000 -        0x186b03fff  libc++abi.dylib (904.4) <1DD3A1C9-D765-34FB-B8C1-87BF52CE49C0> /usr/lib/libc++abi.dylib
       0x186b04000 -        0x186b36fff  libsystem_kernel.dylib (7195.81.3) <55FCA547-4877-3075-8A08-FE1620BFC682> /usr/lib/system/libsystem_kernel.dylib
       0x186b37000 -        0x186b42fff  libsystem_pthread.dylib (454.80.2) <8E907E6C-C227-312E-944C-767093692AFF> /usr/lib/system/libsystem_pthread.dylib
       0x186b43000 -        0x186b81fff  libdyld.dylib (832.7.3) <EF759BF3-97FA-30EA-A1CA-EDECFEA726FE> /usr/lib/system/libdyld.dylib
       0x186b82000 -        0x186b88fff  libsystem_platform.dylib (254.80.2) <8633A39C-10A2-3B44-93F7-617AB09FF640> /usr/lib/system/libsystem_platform.dylib
       0x186b89000 -        0x186bb4fff  libsystem_info.dylib (542.40.3) <4CC96CFC-7198-3F26-8C8C-20FB010CDF98> /usr/lib/system/libsystem_info.dylib
       0x188f39000 -        0x188f43fff  libsystem_darwin.dylib (1439.40.11) <B790A863-2D74-300E-9698-A25B5602B32F> /usr/lib/system/libsystem_darwin.dylib
       0x189376000 -        0x189381fff  libsystem_notify.dylib (279.40.4) <A7B6BDA8-5371-352E-8A36-95D46C4B07F1> /usr/lib/system/libsystem_notify.dylib
       0x18b331000 -        0x18b340fff  libsystem_networkextension.dylib (1295.80.3) <B6BD1267-BE59-3E42-B2B5-2BF13F17D02D> /usr/lib/system/libsystem_networkextension.dylib
       0x18b398000 -        0x18b3affff  libsystem_asl.dylib (385) <31E28E59-1CDD-3B83-8BF0-56C675227FA2> /usr/lib/system/libsystem_asl.dylib
       0x18c920000 -        0x18c927fff  libsystem_symptoms.dylib (1431.40.36) <0657E539-C0CE-30F8-B630-FBAE36109542> /usr/lib/system/libsystem_symptoms.dylib
       0x18e395000 -        0x18e3a6fff  libsystem_containermanager.dylib (318.80.2) <0B742EA4-AFA2-36B6-AB4B-2F8ACA7211AA> /usr/lib/system/libsystem_containermanager.dylib
       0x18f018000 -        0x18f01cfff  libsystem_configuration.dylib (1109.60.2) <AEC5E654-A5B8-343E-80B7-27D5D0D856D9> /usr/lib/system/libsystem_configuration.dylib
       0x18f01d000 -        0x18f021fff  libsystem_sandbox.dylib (1441.60.4) <D7CDDE27-978E-3511-AE6F-296D901290B2> /usr/lib/system/libsystem_sandbox.dylib
       0x18f9dd000 -        0x18f9dffff  libquarantine.dylib (119.40.2) <66942A5C-57B3-3524-BA49-0F2DA4A584D1> /usr/lib/system/libquarantine.dylib
       0x18ff15000 -        0x18ff19fff  libsystem_coreservices.dylib (127) <A9BDB37A-3E00-3FD0-AE92-7ED6FB63DD3B> /usr/lib/system/libsystem_coreservices.dylib
       0x19011a000 -        0x190147fff  libsystem_m.dylib (3186.40.2) <8FADC1D2-0B61-34C7-9F1C-19D85CB07E16> /usr/lib/system/libsystem_m.dylib
       0x190149000 -        0x19014efff  libmacho.dylib (973.4) <8146EF75-0B83-3A8D-A1DA-1D49AD62DE88> /usr/lib/system/libmacho.dylib
       0x19016b000 -        0x190178fff  libcommonCrypto.dylib (60178.40.2) <49528AE3-1376-376C-BE61-31393A50850D> /usr/lib/system/libcommonCrypto.dylib
       0x190179000 -        0x190183fff  libunwind.dylib (200.10) <017E8A02-B5EA-36DB-BB73-9EDD72F1229A> /usr/lib/system/libunwind.dylib
       0x190184000 -        0x19018bfff  liboah.dylib (203.30) <46D9195A-40F2-3CD5-AE73-2F6EF52ECCEC> /usr/lib/liboah.dylib
       0x19018c000 -        0x190194fff  libcopyfile.dylib (173.40.2) <B123D3D2-7B17-3A85-AFC6-37D7AD1C5E78> /usr/lib/system/libcopyfile.dylib
       0x190195000 -        0x190198fff  libcompiler_rt.dylib (102.2) <EDB437BA-9775-3761-A646-58086BD12C4D> /usr/lib/system/libcompiler_rt.dylib
       0x190199000 -        0x19019bfff  libsystem_collections.dylib (1439.40.11) <B252D66F-979E-3ABA-83AC-0BDED154A9A7> /usr/lib/system/libsystem_collections.dylib
       0x19019c000 -        0x19019efff  libsystem_secinit.dylib (87.60.1) <F250D007-BE32-3C94-86DD-33598CF6D3E3> /usr/lib/system/libsystem_secinit.dylib
       0x19019f000 -        0x1901a1fff  libremovefile.dylib (49.40.3) <FE46F050-5062-3117-88B2-E727C8AFABA6> /usr/lib/system/libremovefile.dylib
       0x1901a2000 -        0x1901a2fff  libkeymgr.dylib (31) <1EE40BB9-C661-3FB2-8409-44E93D2820DA> /usr/lib/system/libkeymgr.dylib
       0x1901a3000 -        0x1901abfff  libsystem_dnssd.dylib (1310.80.1) <7F309D4C-1FE5-3C27-8403-9F305D7D8725> /usr/lib/system/libsystem_dnssd.dylib
       0x1901ac000 -        0x1901b1fff  libcache.dylib (83) <C8504CA2-F42B-312F-B79D-2B1C7E64FA4D> /usr/lib/system/libcache.dylib
       0x1901b2000 -        0x1901b3fff  libSystem.B.dylib (1292.60.1) <CF348B98-3432-3B5A-920B-2C7AFE89000D> /usr/lib/libSystem.B.dylib
       0x1935c6000 -        0x1935c6fff  liblaunch.dylib (2038.80.3) <327A64BF-8C2C-303D-91E4-70A168C5D87F> /usr/lib/system/liblaunch.dylib
       0x195a90000 -        0x195a90fff  libsystem_product_info_filter.dylib (8.40.1) <616C96CE-226D-3719-B37F-A3FCAA32591A> /usr/lib/system/libsystem_product_info_filter.dylib

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 667038
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=484.3M resident=0K(0%) swapped_out_or_unallocated=484.3M(100%)
Writable regions: Total=9936K written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=9936K(100%)

                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                   32K        1 
MALLOC                            1168K        7 
MALLOC guard page                   96K        4 
STACK GUARD                       56.0M        1 
Stack                             8176K        1 
__AUTH                              45K       12 
__AUTH_CONST                        75K       38 
__DATA                             738K       39 
__DATA_CONST                       209K       39 
__DATA_DIRTY                        57K       21 
__LINKEDIT                       479.8M        5 
__OBJC_CONST                        10K        5 
__OBJC_RO                         64.5M        1 
__OBJC_RW                         2544K        1 
__TEXT                            4708K       45 
shared memory                       16K        1 
===========                     =======  ======= 
TOTAL                            617.7M      221 
daanx commented 3 years ago

There were recently some commits related to the M1. Does this still happen with the latest dev-slice ? I guess I will need to get an M1 soon so I can test properly :-)

folowing commented 3 years ago

There were recently some commits related to the M1. Does this still happen with the latest dev-slice ? I guess I will need to get an M1 soon so I can test properly :-)

I pulled latest sources and checked it again just now and there is still a problem

daanx commented 3 years ago

:-( difficult to guess what goes wrong -- I need an M1 to properly debug/port mimalloc. Hmm, I'll look into getting a test M1 mac mini perhaps.

daanx commented 3 years ago

Looking at #343 it seems #xhochy was able to run mimalloc on the M1; -- I wonder what has changed? Perhaps a renaming from __MACH__ to __APPLE__ ?

jserv commented 3 years ago

I attempted to apply the following changes:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ca2ce3e..10c7db7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -362,7 +362,7 @@ if (MI_BUILD_TESTS)
   target_compile_definitions(mimalloc-test-stress PRIVATE ${mi_defines})
   target_compile_options(mimalloc-test-stress PRIVATE ${mi_cflags})
   target_include_directories(mimalloc-test-stress PRIVATE include)
-  target_link_libraries(mimalloc-test-stress PRIVATE mimalloc ${mi_libraries})
+  target_link_libraries(mimalloc-test-stress PRIVATE mimalloc-static ${mi_libraries})

   enable_testing()
   add_test(test_api, mimalloc-test-api)
diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h
index 449893b..47290a9 100644
--- a/include/mimalloc-internal.h
+++ b/include/mimalloc-internal.h
@@ -712,7 +712,8 @@ static inline void* mi_tls_slot(size_t slot) mi_attr_noexcept {
 #elif defined(__aarch64__)
   void** tcb; UNUSED(ofs);
 #if defined(__APPLE__) // M1, issue #343
-  __asm__ volatile ("mrs %0, tpidrro_el0" : "=r" (tcb));
+  __asm__ volatile ("mrs %0, tpidrro_el0\n"
+                    "bic %0, %0, #0x7\n" : "=r" (tcb));
 #else
   __asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tcb));
 #endif
@@ -739,7 +740,8 @@ static inline void mi_tls_slot_set(size_t slot, void* value) mi_attr_noexcept {
 #elif defined(__aarch64__)
   void** tcb; UNUSED(ofs);
 #if defined(__APPLE__) // M1, issue #343
-  __asm__ volatile ("mrs %0, tpidrro_el0" : "=r" (tcb));
+  __asm__ volatile ("mrs %0, tpidrro_el0\n"
+                    "bic %0, %0, #0x7\n" : "=r" (tcb));
 #else
   __asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tcb));
 #endif
@@ -749,8 +751,7 @@ 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 {
 #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;
+  return (uintptr_t)mi_tls_slot(0);
 #elif defined(__BIONIC__) && (defined(__arm__) || defined(__aarch64__))
   // on Android, slot 1 is the thread ID (pointer to pthread internal struct)
   return (uintptr_t)mi_tls_slot(1);

Then, the executable mimalloc-test-stress, linked to static libmimalloc.a, seems to work. However, I still failed to run the same program dynamically linked to libmimalloc.dylib yet.

daanx commented 3 years ago

I think this is fixed now in the latest releases (v1.7.2, and v2.0.2)