mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

Segfault on Android when running provided demo 'mimalloc-test-api'

Open ldcsaa opened this issue 5 years ago • 8 comments

(version 1.6.x, 1.7.0, 2.0.0 has the same problem)

Compile demo with android-ndk-r21b:

@echo off

set CURPATH=%cd%
set NDK_HOME=G:\Microsoft\AndroidNDK64\android-ndk-r21b
cd %~dp0

cd mimalloc
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=./install -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=%NDK_HOME%\build\cmake\android.toolchain.cmake -DANDROID_ABI=x86 -DANDROID_NATIVE_API_LEVEL=21 ..
make -j24 VERBOSE=1
make install
cd %CURPATH%

When run demo, raises segfault exception:

03-29 17:20:01.187: D/WifiStateMachine(1425): --------- beginning of crash
03-29 17:20:36.517: A/libc(2127): Fatal signal 11 (SIGSEGV), code 1, fault addr 0xcf7fffec in tid 2127 (mimalloc-test-a)
03-29 17:20:36.517: A/libc(2127): [ 03-29 17:20:36.518  1185: 1185 W/         ]
03-29 17:20:36.517: A/libc(2127): debuggerd: handling request: pid=2127 uid=0 gid=0 tid=2127
03-29 17:20:36.568: A/DEBUG(2128): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-29 17:20:36.568: A/DEBUG(2128): Build fingerprint: 'google/android_x86/x86:7.1.2/N2G48C/4565141:/release-keys'
03-29 17:20:36.568: A/DEBUG(2128): Revision: '0'
03-29 17:20:36.568: A/DEBUG(2128): ABI: 'x86'
03-29 17:20:36.568: A/DEBUG(2128): pid: 2127, tid: 2127, name: mimalloc-test-a  >>> ./mimalloc-test-api <<<
03-29 17:20:36.568: A/DEBUG(2128): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xcf7fffec
03-29 17:20:36.568: A/DEBUG(2128):     eax 00000023  ebx 8aadff18  ecx 00000000  edx c7ffe9ac
03-29 17:20:36.568: A/DEBUG(2128):     esi 00000000  edi 00000021
03-29 17:20:36.568: A/DEBUG(2128):     xcs 00000073  xds 0000007b  xes 0000007b  xfs 00000000  xss 0000007b
03-29 17:20:36.568: A/DEBUG(2128):     eip 8aaad60a  ebp cf800028  esp cf7ffff0  flags 00010286
03-29 17:20:36.571: A/DEBUG(2128): backtrace:
03-29 17:20:36.571: A/DEBUG(2128):     #00 pc 0000360a  /data/local/tmp/mimalloc-test-api (calloc+10)
03-29 17:20:36.571: A/DEBUG(2128):     #01 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.571: A/DEBUG(2128):     #02 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.571: A/DEBUG(2128):     #03 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.571: A/DEBUG(2128):     #04 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.571: A/DEBUG(2128):     #05 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.571: A/DEBUG(2128):     #06 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.571: A/DEBUG(2128):     #07 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.571: A/DEBUG(2128):     #08 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.571: A/DEBUG(2128):     #09 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.571: A/DEBUG(2128):     #10 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.571: A/DEBUG(2128):     #11 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.571: A/DEBUG(2128):     #12 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.571: A/DEBUG(2128):     #13 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.571: A/DEBUG(2128):     #14 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.571: A/DEBUG(2128):     #15 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.571: A/DEBUG(2128):     #16 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.571: A/DEBUG(2128):     #17 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.571: A/DEBUG(2128):     #18 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.571: A/DEBUG(2128):     #19 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.571: A/DEBUG(2128):     #20 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #21 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #22 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #23 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #24 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #25 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #26 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #27 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #28 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #29 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #30 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #31 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #32 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #33 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #34 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #35 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #36 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #37 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #38 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #39 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #40 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #41 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #42 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #43 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #44 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #45 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #46 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #47 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #48 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #49 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #50 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #51 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #52 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #53 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #54 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #55 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #56 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #57 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #58 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #59 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #60 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)
03-29 17:20:36.572: A/DEBUG(2128):     #61 pc 00027acc  /data/local/tmp/mimalloc-test-api (__emutls_get_address+200)
03-29 17:20:36.572: A/DEBUG(2128):     #62 pc 00004536  /data/local/tmp/mimalloc-test-api (mi_get_default_heap+38)
03-29 17:20:36.572: A/DEBUG(2128):     #63 pc 0000362e  /data/local/tmp/mimalloc-test-api (calloc+46)

PS: When I build a mimalloc static library, and use it in another place, it raises segfault exception too.

ldcsaa avatar Mar 29 '21 09:03 ldcsaa

__emutls_get_address() appears to call calloc(), causing recursion.

This also happens on AArch64, by the way.

easyaspi314 avatar Apr 27 '21 16:04 easyaspi314

I wonder if this is fixed by #384 ? (should be in the latest dev or dev-slice)

daanx avatar Apr 28 '21 18:04 daanx

Just tested on Linux Ubuntu 20.04 on arm64 and everything seemed to work; I guess it is android specific and I do not have a test environment set up for android. I will try to look into it -- seems a similar problem as we used to have on macOS where the loader uses the allocator recursively when initializing thread local state. You may want to try to add

#define MI_TLS_RECURSE_GUARD 1

at the start of the include/mimalloc_internal.h file and see if that fixes the problem?

daanx avatar Apr 28 '21 18:04 daanx

Just tested on Linux Ubuntu 20.04 on arm64 and everything seemed to work; I guess it is android specific and I do not have a test environment set up for android. I will try to look into it -- seems a similar problem as we used to have on macOS where the loader uses the allocator recursively when initializing thread local state. You may want to try to add

#define MI_TLS_RECURSE_GUARD 1

at the start of the include/mimalloc_internal.h file and see if that fixes the problem?

I tested the dev and dev-slice branches and tried to add #define MI_TLS_RECURSE_GUARD 1 to include/mimalloc_internal.h, but the problem is not fixed.

yecate avatar Apr 30 '21 15:04 yecate

I have no way to test on Android -- I wonder if the crash is related to the threadid. mimalloc has been used on Android so I wonder if it is related to recent changes? Can you try in the dev_slice branch, to change include\mimalloc-internal around line 882:

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;
#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);

to

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;
#elif defined(__BIONIC__) && (defined(__arm__) || defined(__aarch64__))
  // on Android, slot 1 is the thread ID (pointer to pthread internal struct)
  return (uintptr_t)&_mi_heap_default;
  //  return (uintptr_t)mi_tls_slot(1);

and see if that works? (ah, also check if __BIONIC__ is defined in the first place. Should we use __ANDROID__ instead?)

daanx avatar May 01 '21 16:05 daanx

I have no way to test on Android -- I wonder if the crash is related to the threadid. mimalloc has been used on Android so I wonder if it is related to recent changes? Can you try in the dev_slice branch, to change include\mimalloc-internal around line 882:

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;
#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);

to

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;
#elif defined(__BIONIC__) && (defined(__arm__) || defined(__aarch64__))
  // on Android, slot 1 is the thread ID (pointer to pthread internal struct)
  return (uintptr_t)&_mi_heap_default;
  //  return (uintptr_t)mi_tls_slot(1);

and see if that works? (ah, also check if __BIONIC__ is defined in the first place. Should we use __ANDROID__ instead?)

payyy

yecate avatar May 05 '21 08:05 yecate

define macro MI_TLS_PTHREAD works well for me on android @yecate

phthegreat avatar Jul 02 '21 10:07 phthegreat

define macro MI_TLS_PTHREAD works well for me on android @yecate

thanks it works for me

yecate avatar Jul 10 '21 14:07 yecate