Skip to content

Fix thread ID getter on Android ARM/AArch64#384

Merged
daanx merged 1 commit intomicrosoft:devfrom
kdrag0n:fix-android-thread-id
Apr 22, 2021
Merged

Fix thread ID getter on Android ARM/AArch64#384
daanx merged 1 commit intomicrosoft:devfrom
kdrag0n:fix-android-thread-id

Conversation

@kdrag0n
Copy link
Contributor

@kdrag0n kdrag0n commented Apr 7, 2021

Android's Bionic libc stores the thread ID in TLS slot 1 instead of 0 on 32-bit ARM and AArch64. Slot 0 contains a pointer to the ELF DTV (Dynamic Thread Vector) instead, which is constant for each loaded DSO.

Because mimalloc uses the thread ID to determine whether operations are thread-local or cross-thread (atomic), all threads having the same ID causes internal data structures to get corrupted quickly when multiple threads are using the allocator:

mimalloc: assertion failed: at "external/mimalloc/src/page.c":563, mi_page_extend_free
  assertion: "page->local_free == NULL"
mimalloc: assertion failed: at "external/mimalloc/src/page.c":74, mi_page_is_valid_init
  assertion: "page->used <= page->capacity"
mimalloc: assertion failed: at "external/mimalloc/src/page.c":100, mi_page_is_valid_init
  assertion: "page->used + free_count == page->capacity"
mimalloc: assertion failed: at "external/mimalloc/src/page.c":74, mi_page_is_valid_init
  assertion: "page->used <= page->capacity"

Add support for Android's alternate TLS layout to fix the crashes in multi-threaded use cases.

Fixes #376.

@kdrag0n kdrag0n changed the base branch from master to dev April 7, 2021 08:59
Android's Bionic libc stores the thread ID in TLS slot 1 instead of 0
on 32-bit ARM and AArch64. Slot 0 contains a pointer to the ELF DTV
(Dynamic Thread Vector) instead, which is constant for each loaded DSO.

Because mimalloc uses the thread ID to determine whether operations are
thread-local or cross-thread (atomic), all threads having the same ID
causes internal data structures to get corrupted quickly when multiple
threads are using the allocator:

mimalloc: assertion failed: at "external/mimalloc/src/page.c":563, mi_page_extend_free
  assertion: "page->local_free == NULL"
mimalloc: assertion failed: at "external/mimalloc/src/page.c":74, mi_page_is_valid_init
  assertion: "page->used <= page->capacity"
mimalloc: assertion failed: at "external/mimalloc/src/page.c":100, mi_page_is_valid_init
  assertion: "page->used + free_count == page->capacity"
mimalloc: assertion failed: at "external/mimalloc/src/page.c":74, mi_page_is_valid_init
  assertion: "page->used <= page->capacity"

Add support for Android's alternate TLS layout to fix the crashes in
multi-threaded use cases.

Fixes #376.
@daanx
Copy link
Collaborator

daanx commented Apr 22, 2021

@kdrag0n this is great! Thanks for figuring this out as I cannot test myself :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault on Android with multiple threads

2 participants