You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
compiler-rt
cmake
docs
include
lib
BlocksRuntime
asan
builtins
Darwin-excludes
aarch64
arm
i386
macho_embedded
ppc
x86_64
CMakeLists.txt
README.txt
absvdi2.c
absvsi2.c
absvti2.c
adddf3.c
addsf3.c
addtf3.c
addvdi3.c
addvsi3.c
addvti3.c
apple_versioning.c
ashldi3.c
ashlti3.c
ashrdi3.c
ashrti3.c
assembly.h
atomic.c
atomic_flag_clear.c
atomic_flag_clear_explicit.c
atomic_flag_test_and_set.c
atomic_flag_test_and_set_explicit.c
atomic_signal_fence.c
atomic_thread_fence.c
bswapdi2.c
bswapsi2.c
clear_cache.c
clzdi2.c
clzsi2.c
clzti2.c
cmpdi2.c
cmpti2.c
comparedf2.c
comparesf2.c
comparetf2.c
cpu_model.c
ctzdi2.c
ctzsi2.c
ctzti2.c
divdc3.c
divdf3.c
divdi3.c
divmoddi4.c
divmodsi4.c
divsc3.c
divsf3.c
divsi3.c
divtc3.c
divtf3.c
divti3.c
divxc3.c
emutls.c
enable_execute_stack.c
eprintf.c
extenddftf2.c
extendhfsf2.c
extendsfdf2.c
extendsftf2.c
ffsdi2.c
ffssi2.c
ffsti2.c
fixdfdi.c
fixdfsi.c
fixdfti.c
fixsfdi.c
fixsfsi.c
fixsfti.c
fixtfdi.c
fixtfsi.c
fixtfti.c
fixunsdfdi.c
fixunsdfsi.c
fixunsdfti.c
fixunssfdi.c
fixunssfsi.c
fixunssfti.c
fixunstfdi.c
fixunstfsi.c
fixunstfti.c
fixunsxfdi.c
fixunsxfsi.c
fixunsxfti.c
fixxfdi.c
fixxfti.c
floatdidf.c
floatdisf.c
floatditf.c
floatdixf.c
floatsidf.c
floatsisf.c
floatsitf.c
floattidf.c
floattisf.c
floattitf.c
floattixf.c
floatundidf.c
floatundisf.c
floatunditf.c
floatundixf.c
floatunsidf.c
floatunsisf.c
floatunsitf.c
floatuntidf.c
floatuntisf.c
floatuntitf.c
floatuntixf.c
fp_add_impl.inc
fp_extend.h
fp_extend_impl.inc
fp_fixint_impl.inc
fp_fixuint_impl.inc
fp_lib.h
fp_mul_impl.inc
fp_trunc.h
fp_trunc_impl.inc
gcc_personality_v0.c
int_endianness.h
int_lib.h
int_math.h
int_types.h
int_util.c
int_util.h
lshrdi3.c
lshrti3.c
mingw_fixfloat.c
moddi3.c
modsi3.c
modti3.c
muldc3.c
muldf3.c
muldi3.c
mulodi4.c
mulosi4.c
muloti4.c
mulsc3.c
mulsf3.c
multc3.c
multf3.c
multi3.c
mulvdi3.c
mulvsi3.c
mulvti3.c
mulxc3.c
negdf2.c
negdi2.c
negsf2.c
negti2.c
negvdi2.c
negvsi2.c
negvti2.c
os_version_check.c
paritydi2.c
paritysi2.c
parityti2.c
popcountdi2.c
popcountsi2.c
popcountti2.c
powidf2.c
powisf2.c
powitf2.c
powixf2.c
subdf3.c
subsf3.c
subtf3.c
subvdi3.c
subvsi3.c
subvti3.c
trampoline_setup.c
truncdfhf2.c
truncdfsf2.c
truncsfhf2.c
trunctfdf2.c
trunctfsf2.c
ucmpdi2.c
ucmpti2.c
udivdi3.c
udivmoddi4.c
udivmodsi4.c
udivmodti4.c
udivsi3.c
udivti3.c
umoddi3.c
umodsi3.c
umodti3.c
unwind-ehabi-helpers.h
cfi
dfsan
esan
fuzzer
hwasan
interception
lsan
msan
profile
safestack
sanitizer_common
scudo
stats
tsan
ubsan
ubsan_minimal
xray
CMakeLists.txt
test
unittests
www
.arcconfig
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
CREDITS.TXT
LICENSE.TXT
README.txt
libcxx
libcxxabi
libunwind
lld
lldb
llvm
openmp
polly
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mk
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
362 lines
11 KiB
C
362 lines
11 KiB
C
![]() |
/* ===---------- emutls.c - Implements __emutls_get_address ---------------===
|
||
|
*
|
||
|
* The LLVM Compiler Infrastructure
|
||
|
*
|
||
|
* This file is dual licensed under the MIT and the University of Illinois Open
|
||
|
* Source Licenses. See LICENSE.TXT for details.
|
||
|
*
|
||
|
* ===----------------------------------------------------------------------===
|
||
|
*/
|
||
|
#include <stdint.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include "int_lib.h"
|
||
|
#include "int_util.h"
|
||
|
|
||
|
typedef struct emutls_address_array {
|
||
|
uintptr_t size; /* number of elements in the 'data' array */
|
||
|
void* data[];
|
||
|
} emutls_address_array;
|
||
|
|
||
|
static void emutls_shutdown(emutls_address_array *array);
|
||
|
|
||
|
#ifndef _WIN32
|
||
|
|
||
|
#include <pthread.h>
|
||
|
|
||
|
static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||
|
static pthread_key_t emutls_pthread_key;
|
||
|
|
||
|
typedef unsigned int gcc_word __attribute__((mode(word)));
|
||
|
typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
|
||
|
|
||
|
/* Default is not to use posix_memalign, so systems like Android
|
||
|
* can use thread local data without heavier POSIX memory allocators.
|
||
|
*/
|
||
|
#ifndef EMUTLS_USE_POSIX_MEMALIGN
|
||
|
#define EMUTLS_USE_POSIX_MEMALIGN 0
|
||
|
#endif
|
||
|
|
||
|
static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
|
||
|
void *base;
|
||
|
#if EMUTLS_USE_POSIX_MEMALIGN
|
||
|
if (posix_memalign(&base, align, size) != 0)
|
||
|
abort();
|
||
|
#else
|
||
|
#define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void*))
|
||
|
char* object;
|
||
|
if ((object = (char*)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
|
||
|
abort();
|
||
|
base = (void*)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES))
|
||
|
& ~(uintptr_t)(align - 1));
|
||
|
|
||
|
((void**)base)[-1] = object;
|
||
|
#endif
|
||
|
return base;
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_memalign_free(void *base) {
|
||
|
#if EMUTLS_USE_POSIX_MEMALIGN
|
||
|
free(base);
|
||
|
#else
|
||
|
/* The mallocated address is in ((void**)base)[-1] */
|
||
|
free(((void**)base)[-1]);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
static void emutls_key_destructor(void* ptr) {
|
||
|
emutls_shutdown((emutls_address_array*)ptr);
|
||
|
free(ptr);
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_init(void) {
|
||
|
if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
|
||
|
abort();
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_init_once(void) {
|
||
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
||
|
pthread_once(&once, emutls_init);
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_lock() {
|
||
|
pthread_mutex_lock(&emutls_mutex);
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_unlock() {
|
||
|
pthread_mutex_unlock(&emutls_mutex);
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_setspecific(emutls_address_array *value) {
|
||
|
pthread_setspecific(emutls_pthread_key, (void*) value);
|
||
|
}
|
||
|
|
||
|
static __inline emutls_address_array* emutls_getspecific() {
|
||
|
return (emutls_address_array*) pthread_getspecific(emutls_pthread_key);
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
|
||
|
#include <windows.h>
|
||
|
#include <malloc.h>
|
||
|
#include <stdio.h>
|
||
|
#include <assert.h>
|
||
|
|
||
|
static LPCRITICAL_SECTION emutls_mutex;
|
||
|
static DWORD emutls_tls_index = TLS_OUT_OF_INDEXES;
|
||
|
|
||
|
typedef uintptr_t gcc_word;
|
||
|
typedef void * gcc_pointer;
|
||
|
|
||
|
static void win_error(DWORD last_err, const char *hint) {
|
||
|
char *buffer = NULL;
|
||
|
if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
||
|
NULL, last_err, 0, (LPSTR)&buffer, 1, NULL)) {
|
||
|
fprintf(stderr, "Windows error: %s\n", buffer);
|
||
|
} else {
|
||
|
fprintf(stderr, "Unkown Windows error: %s\n", hint);
|
||
|
}
|
||
|
LocalFree(buffer);
|
||
|
}
|
||
|
|
||
|
static __inline void win_abort(DWORD last_err, const char *hint) {
|
||
|
win_error(last_err, hint);
|
||
|
abort();
|
||
|
}
|
||
|
|
||
|
static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
|
||
|
void *base = _aligned_malloc(size, align);
|
||
|
if (!base)
|
||
|
win_abort(GetLastError(), "_aligned_malloc");
|
||
|
return base;
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_memalign_free(void *base) {
|
||
|
_aligned_free(base);
|
||
|
}
|
||
|
|
||
|
static void emutls_exit(void) {
|
||
|
if (emutls_mutex) {
|
||
|
DeleteCriticalSection(emutls_mutex);
|
||
|
_aligned_free(emutls_mutex);
|
||
|
emutls_mutex = NULL;
|
||
|
}
|
||
|
if (emutls_tls_index != TLS_OUT_OF_INDEXES) {
|
||
|
emutls_shutdown((emutls_address_array*)TlsGetValue(emutls_tls_index));
|
||
|
TlsFree(emutls_tls_index);
|
||
|
emutls_tls_index = TLS_OUT_OF_INDEXES;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#pragma warning (push)
|
||
|
#pragma warning (disable : 4100)
|
||
|
static BOOL CALLBACK emutls_init(PINIT_ONCE p0, PVOID p1, PVOID *p2) {
|
||
|
emutls_mutex = (LPCRITICAL_SECTION)_aligned_malloc(sizeof(CRITICAL_SECTION), 16);
|
||
|
if (!emutls_mutex) {
|
||
|
win_error(GetLastError(), "_aligned_malloc");
|
||
|
return FALSE;
|
||
|
}
|
||
|
InitializeCriticalSection(emutls_mutex);
|
||
|
|
||
|
emutls_tls_index = TlsAlloc();
|
||
|
if (emutls_tls_index == TLS_OUT_OF_INDEXES) {
|
||
|
emutls_exit();
|
||
|
win_error(GetLastError(), "TlsAlloc");
|
||
|
return FALSE;
|
||
|
}
|
||
|
atexit(&emutls_exit);
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_init_once(void) {
|
||
|
static INIT_ONCE once;
|
||
|
InitOnceExecuteOnce(&once, emutls_init, NULL, NULL);
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_lock() {
|
||
|
EnterCriticalSection(emutls_mutex);
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_unlock() {
|
||
|
LeaveCriticalSection(emutls_mutex);
|
||
|
}
|
||
|
|
||
|
static __inline void emutls_setspecific(emutls_address_array *value) {
|
||
|
if (TlsSetValue(emutls_tls_index, (LPVOID) value) == 0)
|
||
|
win_abort(GetLastError(), "TlsSetValue");
|
||
|
}
|
||
|
|
||
|
static __inline emutls_address_array* emutls_getspecific() {
|
||
|
LPVOID value = TlsGetValue(emutls_tls_index);
|
||
|
if (value == NULL) {
|
||
|
const DWORD err = GetLastError();
|
||
|
if (err != ERROR_SUCCESS)
|
||
|
win_abort(err, "TlsGetValue");
|
||
|
}
|
||
|
return (emutls_address_array*) value;
|
||
|
}
|
||
|
|
||
|
/* Provide atomic load/store functions for emutls_get_index if built with MSVC.
|
||
|
*/
|
||
|
#if !defined(__ATOMIC_RELEASE)
|
||
|
#include <intrin.h>
|
||
|
|
||
|
enum { __ATOMIC_ACQUIRE = 2, __ATOMIC_RELEASE = 3 };
|
||
|
|
||
|
static __inline uintptr_t __atomic_load_n(void *ptr, unsigned type) {
|
||
|
assert(type == __ATOMIC_ACQUIRE);
|
||
|
// These return the previous value - but since we do an OR with 0,
|
||
|
// it's equivalent to a plain load.
|
||
|
#ifdef _WIN64
|
||
|
return InterlockedOr64(ptr, 0);
|
||
|
#else
|
||
|
return InterlockedOr(ptr, 0);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
static __inline void __atomic_store_n(void *ptr, uintptr_t val, unsigned type) {
|
||
|
assert(type == __ATOMIC_RELEASE);
|
||
|
InterlockedExchangePointer((void *volatile *)ptr, (void *)val);
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
#pragma warning (pop)
|
||
|
|
||
|
#endif
|
||
|
|
||
|
static size_t emutls_num_object = 0; /* number of allocated TLS objects */
|
||
|
|
||
|
/* Free the allocated TLS data
|
||
|
*/
|
||
|
static void emutls_shutdown(emutls_address_array *array) {
|
||
|
if (array) {
|
||
|
uintptr_t i;
|
||
|
for (i = 0; i < array->size; ++i) {
|
||
|
if (array->data[i])
|
||
|
emutls_memalign_free(array->data[i]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* For every TLS variable xyz,
|
||
|
* there is one __emutls_control variable named __emutls_v.xyz.
|
||
|
* If xyz has non-zero initial value, __emutls_v.xyz's "value"
|
||
|
* will point to __emutls_t.xyz, which has the initial value.
|
||
|
*/
|
||
|
typedef struct __emutls_control {
|
||
|
/* Must use gcc_word here, instead of size_t, to match GCC. When
|
||
|
gcc_word is larger than size_t, the upper extra bits are all
|
||
|
zeros. We can use variables of size_t to operate on size and
|
||
|
align. */
|
||
|
gcc_word size; /* size of the object in bytes */
|
||
|
gcc_word align; /* alignment of the object in bytes */
|
||
|
union {
|
||
|
uintptr_t index; /* data[index-1] is the object address */
|
||
|
void* address; /* object address, when in single thread env */
|
||
|
} object;
|
||
|
void* value; /* null or non-zero initial value for the object */
|
||
|
} __emutls_control;
|
||
|
|
||
|
/* Emulated TLS objects are always allocated at run-time. */
|
||
|
static __inline void *emutls_allocate_object(__emutls_control *control) {
|
||
|
/* Use standard C types, check with gcc's emutls.o. */
|
||
|
COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
|
||
|
COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*));
|
||
|
|
||
|
size_t size = control->size;
|
||
|
size_t align = control->align;
|
||
|
void* base;
|
||
|
if (align < sizeof(void*))
|
||
|
align = sizeof(void*);
|
||
|
/* Make sure that align is power of 2. */
|
||
|
if ((align & (align - 1)) != 0)
|
||
|
abort();
|
||
|
|
||
|
base = emutls_memalign_alloc(align, size);
|
||
|
if (control->value)
|
||
|
memcpy(base, control->value, size);
|
||
|
else
|
||
|
memset(base, 0, size);
|
||
|
return base;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* Returns control->object.index; set index if not allocated yet. */
|
||
|
static __inline uintptr_t emutls_get_index(__emutls_control *control) {
|
||
|
uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
|
||
|
if (!index) {
|
||
|
emutls_init_once();
|
||
|
emutls_lock();
|
||
|
index = control->object.index;
|
||
|
if (!index) {
|
||
|
index = ++emutls_num_object;
|
||
|
__atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
|
||
|
}
|
||
|
emutls_unlock();
|
||
|
}
|
||
|
return index;
|
||
|
}
|
||
|
|
||
|
/* Updates newly allocated thread local emutls_address_array. */
|
||
|
static __inline void emutls_check_array_set_size(emutls_address_array *array,
|
||
|
uintptr_t size) {
|
||
|
if (array == NULL)
|
||
|
abort();
|
||
|
array->size = size;
|
||
|
emutls_setspecific(array);
|
||
|
}
|
||
|
|
||
|
/* Returns the new 'data' array size, number of elements,
|
||
|
* which must be no smaller than the given index.
|
||
|
*/
|
||
|
static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) {
|
||
|
/* Need to allocate emutls_address_array with one extra slot
|
||
|
* to store the data array size.
|
||
|
* Round up the emutls_address_array size to multiple of 16.
|
||
|
*/
|
||
|
return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
|
||
|
}
|
||
|
|
||
|
/* Returns the size in bytes required for an emutls_address_array with
|
||
|
* N number of elements for data field.
|
||
|
*/
|
||
|
static __inline uintptr_t emutls_asize(uintptr_t N) {
|
||
|
return N * sizeof(void *) + sizeof(emutls_address_array);
|
||
|
}
|
||
|
|
||
|
/* Returns the thread local emutls_address_array.
|
||
|
* Extends its size if necessary to hold address at index.
|
||
|
*/
|
||
|
static __inline emutls_address_array *
|
||
|
emutls_get_address_array(uintptr_t index) {
|
||
|
emutls_address_array* array = emutls_getspecific();
|
||
|
if (array == NULL) {
|
||
|
uintptr_t new_size = emutls_new_data_array_size(index);
|
||
|
array = (emutls_address_array*) malloc(emutls_asize(new_size));
|
||
|
if (array)
|
||
|
memset(array->data, 0, new_size * sizeof(void*));
|
||
|
emutls_check_array_set_size(array, new_size);
|
||
|
} else if (index > array->size) {
|
||
|
uintptr_t orig_size = array->size;
|
||
|
uintptr_t new_size = emutls_new_data_array_size(index);
|
||
|
array = (emutls_address_array*) realloc(array, emutls_asize(new_size));
|
||
|
if (array)
|
||
|
memset(array->data + orig_size, 0,
|
||
|
(new_size - orig_size) * sizeof(void*));
|
||
|
emutls_check_array_set_size(array, new_size);
|
||
|
}
|
||
|
return array;
|
||
|
}
|
||
|
|
||
|
void* __emutls_get_address(__emutls_control* control) {
|
||
|
uintptr_t index = emutls_get_index(control);
|
||
|
emutls_address_array* array = emutls_get_address_array(index--);
|
||
|
if (array->data[index] == NULL)
|
||
|
array->data[index] = emutls_allocate_object(control);
|
||
|
return array->data[index];
|
||
|
}
|