Path: blob/master/libs/compiler-rt/lib/builtins/emutls.c
12346 views
/* ===---------- emutls.c - Implements __emutls_get_address ---------------===1*2* The LLVM Compiler Infrastructure3*4* This file is dual licensed under the MIT and the University of Illinois Open5* Source Licenses. See LICENSE.TXT for details.6*7* ===----------------------------------------------------------------------===8*/9#include <stdint.h>10#include <stdlib.h>11#include <string.h>1213#include "int_lib.h"14#include "int_util.h"1516#ifdef __BIONIC__17/* There are 4 pthread key cleanup rounds on Bionic. Delay emutls deallocation18to round 2. We need to delay deallocation because:19- Android versions older than M lack __cxa_thread_atexit_impl, so apps20use a pthread key destructor to call C++ destructors.21- Apps might use __thread/thread_local variables in pthread destructors.22We can't wait until the final two rounds, because jemalloc needs two rounds23after the final malloc/free call to free its thread-specific data (see24https://reviews.llvm.org/D46978#1107507). */25#define EMUTLS_SKIP_DESTRUCTOR_ROUNDS 126#else27#define EMUTLS_SKIP_DESTRUCTOR_ROUNDS 028#endif2930typedef struct emutls_address_array {31uintptr_t skip_destructor_rounds;32uintptr_t size; /* number of elements in the 'data' array */33void* data[];34} emutls_address_array;3536static void emutls_shutdown(emutls_address_array *array);3738#ifndef _WIN323940#include <pthread.h>4142static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;43static pthread_key_t emutls_pthread_key;4445typedef unsigned int gcc_word __attribute__((mode(word)));46typedef unsigned int gcc_pointer __attribute__((mode(pointer)));4748/* Default is not to use posix_memalign, so systems like Android49* can use thread local data without heavier POSIX memory allocators.50*/51#ifndef EMUTLS_USE_POSIX_MEMALIGN52#define EMUTLS_USE_POSIX_MEMALIGN 053#endif5455static __inline void *emutls_memalign_alloc(size_t align, size_t size) {56void *base;57#if EMUTLS_USE_POSIX_MEMALIGN58if (posix_memalign(&base, align, size) != 0)59abort();60#else61#define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void*))62char* object;63if ((object = (char*)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)64abort();65base = (void*)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES))66& ~(uintptr_t)(align - 1));6768((void**)base)[-1] = object;69#endif70return base;71}7273static __inline void emutls_memalign_free(void *base) {74#if EMUTLS_USE_POSIX_MEMALIGN75free(base);76#else77/* The mallocated address is in ((void**)base)[-1] */78free(((void**)base)[-1]);79#endif80}8182static __inline void emutls_setspecific(emutls_address_array *value) {83pthread_setspecific(emutls_pthread_key, (void*) value);84}8586static __inline emutls_address_array* emutls_getspecific() {87return (emutls_address_array*) pthread_getspecific(emutls_pthread_key);88}8990static void emutls_key_destructor(void* ptr) {91emutls_address_array *array = (emutls_address_array*)ptr;92if (array->skip_destructor_rounds > 0) {93/* emutls is deallocated using a pthread key destructor. These94* destructors are called in several rounds to accommodate destructor95* functions that (re)initialize key values with pthread_setspecific.96* Delay the emutls deallocation to accommodate other end-of-thread97* cleanup tasks like calling thread_local destructors (e.g. the98* __cxa_thread_atexit fallback in libc++abi).99*/100array->skip_destructor_rounds--;101emutls_setspecific(array);102} else {103emutls_shutdown(array);104free(ptr);105}106}107108static __inline void emutls_init(void) {109if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)110abort();111}112113static __inline void emutls_init_once(void) {114static pthread_once_t once = PTHREAD_ONCE_INIT;115pthread_once(&once, emutls_init);116}117118static __inline void emutls_lock() {119pthread_mutex_lock(&emutls_mutex);120}121122static __inline void emutls_unlock() {123pthread_mutex_unlock(&emutls_mutex);124}125126#else /* _WIN32 */127128#include <windows.h>129#include <malloc.h>130#include <stdio.h>131#include <assert.h>132133static LPCRITICAL_SECTION emutls_mutex;134static DWORD emutls_tls_index = TLS_OUT_OF_INDEXES;135136typedef uintptr_t gcc_word;137typedef void * gcc_pointer;138139static void win_error(DWORD last_err, const char *hint) {140char *buffer = NULL;141if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |142FORMAT_MESSAGE_FROM_SYSTEM |143FORMAT_MESSAGE_MAX_WIDTH_MASK,144NULL, last_err, 0, (LPSTR)&buffer, 1, NULL)) {145fprintf(stderr, "Windows error: %s\n", buffer);146} else {147fprintf(stderr, "Unkown Windows error: %s\n", hint);148}149LocalFree(buffer);150}151152static __inline void win_abort(DWORD last_err, const char *hint) {153win_error(last_err, hint);154abort();155}156157static __inline void *emutls_memalign_alloc(size_t align, size_t size) {158void *base = _aligned_malloc(size, align);159if (!base)160win_abort(GetLastError(), "_aligned_malloc");161return base;162}163164static __inline void emutls_memalign_free(void *base) {165_aligned_free(base);166}167168static void emutls_exit(void) {169if (emutls_mutex) {170DeleteCriticalSection(emutls_mutex);171_aligned_free(emutls_mutex);172emutls_mutex = NULL;173}174if (emutls_tls_index != TLS_OUT_OF_INDEXES) {175emutls_shutdown((emutls_address_array*)TlsGetValue(emutls_tls_index));176TlsFree(emutls_tls_index);177emutls_tls_index = TLS_OUT_OF_INDEXES;178}179}180181#pragma warning (push)182#pragma warning (disable : 4100)183static BOOL CALLBACK emutls_init(PINIT_ONCE p0, PVOID p1, PVOID *p2) {184emutls_mutex = (LPCRITICAL_SECTION)_aligned_malloc(sizeof(CRITICAL_SECTION), 16);185if (!emutls_mutex) {186win_error(GetLastError(), "_aligned_malloc");187return FALSE;188}189InitializeCriticalSection(emutls_mutex);190191emutls_tls_index = TlsAlloc();192if (emutls_tls_index == TLS_OUT_OF_INDEXES) {193emutls_exit();194win_error(GetLastError(), "TlsAlloc");195return FALSE;196}197atexit(&emutls_exit);198return TRUE;199}200201static __inline void emutls_init_once(void) {202static INIT_ONCE once;203InitOnceExecuteOnce(&once, emutls_init, NULL, NULL);204}205206static __inline void emutls_lock() {207EnterCriticalSection(emutls_mutex);208}209210static __inline void emutls_unlock() {211LeaveCriticalSection(emutls_mutex);212}213214static __inline void emutls_setspecific(emutls_address_array *value) {215if (TlsSetValue(emutls_tls_index, (LPVOID) value) == 0)216win_abort(GetLastError(), "TlsSetValue");217}218219static __inline emutls_address_array* emutls_getspecific() {220LPVOID value = TlsGetValue(emutls_tls_index);221if (value == NULL) {222const DWORD err = GetLastError();223if (err != ERROR_SUCCESS)224win_abort(err, "TlsGetValue");225}226return (emutls_address_array*) value;227}228229/* Provide atomic load/store functions for emutls_get_index if built with MSVC.230*/231#if !defined(__ATOMIC_RELEASE)232#include <intrin.h>233234enum { __ATOMIC_ACQUIRE = 2, __ATOMIC_RELEASE = 3 };235236static __inline uintptr_t __atomic_load_n(void *ptr, unsigned type) {237assert(type == __ATOMIC_ACQUIRE);238// These return the previous value - but since we do an OR with 0,239// it's equivalent to a plain load.240#ifdef _WIN64241return InterlockedOr64(ptr, 0);242#else243return InterlockedOr(ptr, 0);244#endif245}246247static __inline void __atomic_store_n(void *ptr, uintptr_t val, unsigned type) {248assert(type == __ATOMIC_RELEASE);249InterlockedExchangePointer((void *volatile *)ptr, (void *)val);250}251252#endif /* __ATOMIC_RELEASE */253254#pragma warning (pop)255256#endif /* _WIN32 */257258static size_t emutls_num_object = 0; /* number of allocated TLS objects */259260/* Free the allocated TLS data261*/262static void emutls_shutdown(emutls_address_array *array) {263if (array) {264uintptr_t i;265for (i = 0; i < array->size; ++i) {266if (array->data[i])267emutls_memalign_free(array->data[i]);268}269}270}271272/* For every TLS variable xyz,273* there is one __emutls_control variable named __emutls_v.xyz.274* If xyz has non-zero initial value, __emutls_v.xyz's "value"275* will point to __emutls_t.xyz, which has the initial value.276*/277typedef struct __emutls_control {278/* Must use gcc_word here, instead of size_t, to match GCC. When279gcc_word is larger than size_t, the upper extra bits are all280zeros. We can use variables of size_t to operate on size and281align. */282gcc_word size; /* size of the object in bytes */283gcc_word align; /* alignment of the object in bytes */284union {285uintptr_t index; /* data[index-1] is the object address */286void* address; /* object address, when in single thread env */287} object;288void* value; /* null or non-zero initial value for the object */289} __emutls_control;290291/* Emulated TLS objects are always allocated at run-time. */292static __inline void *emutls_allocate_object(__emutls_control *control) {293/* Use standard C types, check with gcc's emutls.o. */294COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));295COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*));296297size_t size = control->size;298size_t align = control->align;299void* base;300if (align < sizeof(void*))301align = sizeof(void*);302/* Make sure that align is power of 2. */303if ((align & (align - 1)) != 0)304abort();305306base = emutls_memalign_alloc(align, size);307if (control->value)308memcpy(base, control->value, size);309else310memset(base, 0, size);311return base;312}313314315/* Returns control->object.index; set index if not allocated yet. */316static __inline uintptr_t emutls_get_index(__emutls_control *control) {317uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);318if (!index) {319emutls_init_once();320emutls_lock();321index = control->object.index;322if (!index) {323index = ++emutls_num_object;324__atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);325}326emutls_unlock();327}328return index;329}330331/* Updates newly allocated thread local emutls_address_array. */332static __inline void emutls_check_array_set_size(emutls_address_array *array,333uintptr_t size) {334if (array == NULL)335abort();336array->size = size;337emutls_setspecific(array);338}339340/* Returns the new 'data' array size, number of elements,341* which must be no smaller than the given index.342*/343static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) {344/* Need to allocate emutls_address_array with extra slots345* to store the header.346* Round up the emutls_address_array size to multiple of 16.347*/348uintptr_t header_words = sizeof(emutls_address_array) / sizeof(void *);349return ((index + header_words + 15) & ~((uintptr_t)15)) - header_words;350}351352/* Returns the size in bytes required for an emutls_address_array with353* N number of elements for data field.354*/355static __inline uintptr_t emutls_asize(uintptr_t N) {356return N * sizeof(void *) + sizeof(emutls_address_array);357}358359/* Returns the thread local emutls_address_array.360* Extends its size if necessary to hold address at index.361*/362static __inline emutls_address_array *363emutls_get_address_array(uintptr_t index) {364emutls_address_array* array = emutls_getspecific();365if (array == NULL) {366uintptr_t new_size = emutls_new_data_array_size(index);367array = (emutls_address_array*) malloc(emutls_asize(new_size));368if (array) {369memset(array->data, 0, new_size * sizeof(void*));370array->skip_destructor_rounds = EMUTLS_SKIP_DESTRUCTOR_ROUNDS;371}372emutls_check_array_set_size(array, new_size);373} else if (index > array->size) {374uintptr_t orig_size = array->size;375uintptr_t new_size = emutls_new_data_array_size(index);376array = (emutls_address_array*) realloc(array, emutls_asize(new_size));377if (array)378memset(array->data + orig_size, 0,379(new_size - orig_size) * sizeof(void*));380emutls_check_array_set_size(array, new_size);381}382return array;383}384385void* __emutls_get_address(void* control) {386uintptr_t index = emutls_get_index(control);387emutls_address_array* array = emutls_get_address_array(index--);388if (array->data[index] == NULL)389array->data[index] = emutls_allocate_object(control);390return array->data[index];391}392393394