From 4d5ffcd9873203868fc1e60262ad8c4fb3099b23 Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Wed, 8 Mar 2017 22:30:49 -0500 Subject: [PATCH 1/5] =?UTF-8?q?Windows:=20Don=E2=80=99t=20export=20=5FInit?= =?UTF-8?q?=5Fthread=5Fepoch=20to=20=5Ftls=5Findex.=20This=20can=20lead=20?= =?UTF-8?q?to=20corruption=20of=20the=20owning=20process=E2=80=99=20static?= =?UTF-8?q?=20variables.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interpreter/cling/tools/driver/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interpreter/cling/tools/driver/CMakeLists.txt b/interpreter/cling/tools/driver/CMakeLists.txt index 2a0d7004328b6..741db3224813b 100644 --- a/interpreter/cling/tools/driver/CMakeLists.txt +++ b/interpreter/cling/tools/driver/CMakeLists.txt @@ -52,8 +52,8 @@ if(MSVC) ) # Compiler added symbols for static variables. NOT for VStudio < 2015 - set(cling_exports ${cling_exports} _Init_thread_abort _Init_thread_epoch - _Init_thread_footer _Init_thread_header _tls_index + set(cling_exports ${cling_exports} _Init_thread_abort + _Init_thread_footer _Init_thread_header ) # new/delete variants needed when linking to static msvc runtime (esp. Debug) From 525d0d7835c5c80e604ac1d2a9290e6138466f3c Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Wed, 8 Mar 2017 17:15:47 -0500 Subject: [PATCH 2/5] Add EmuTLS.cpp. --- interpreter/cling/lib/Utils/EmuTLS.cpp | 189 +++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 interpreter/cling/lib/Utils/EmuTLS.cpp diff --git a/interpreter/cling/lib/Utils/EmuTLS.cpp b/interpreter/cling/lib/Utils/EmuTLS.cpp new file mode 100644 index 0000000000000..eccbf53366e3b --- /dev/null +++ b/interpreter/cling/lib/Utils/EmuTLS.cpp @@ -0,0 +1,189 @@ +/* ===---------- 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 +#include +#include +#include + +#include "int_lib.h" +#include "int_util.h" + +/* 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 + +/* 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 unsigned int gcc_word __attribute__((mode(word))); +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; + +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 = 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 +} + +/* 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. */ + typedef unsigned int gcc_pointer __attribute__((mode(pointer))); + 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; +} + +static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER; + +static size_t emutls_num_object = 0; /* number of allocated TLS objects */ + +typedef struct emutls_address_array { + uintptr_t size; /* number of elements in the 'data' array */ + void* data[]; +} emutls_address_array; + +static pthread_key_t emutls_pthread_key; + +static void emutls_key_destructor(void* ptr) { + emutls_address_array* array = (emutls_address_array*)ptr; + uintptr_t i; + for (i = 0; i < array->size; ++i) { + if (array->data[i]) + emutls_memalign_free(array->data[i]); + } + free(ptr); +} + +static void emutls_init(void) { + if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0) + abort(); +} + +/* 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) { + static pthread_once_t once = PTHREAD_ONCE_INIT; + pthread_once(&once, emutls_init); + pthread_mutex_lock(&emutls_mutex); + index = control->object.index; + if (!index) { + index = ++emutls_num_object; + __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE); + } + pthread_mutex_unlock(&emutls_mutex); + } + 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; + pthread_setspecific(emutls_pthread_key, (void*)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 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 = pthread_getspecific(emutls_pthread_key); + if (array == NULL) { + uintptr_t new_size = emutls_new_data_array_size(index); + array = malloc(new_size * sizeof(void *) + sizeof(emutls_address_array)); + 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 = realloc(array, new_size * sizeof(void *) + sizeof(emutls_address_array)); + 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 - 1] == NULL) + array->data[index - 1] = emutls_allocate_object(control); + return array->data[index - 1]; +} From 6f2bd2a670c8c62359b0c0115b4cddefcb146e94 Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Wed, 8 Mar 2017 22:07:54 -0500 Subject: [PATCH 3/5] Implement __emutls_get_address on Windows. --- interpreter/cling/lib/Utils/EmuTLS.cpp | 280 +++++++++++++++++++------ 1 file changed, 220 insertions(+), 60 deletions(-) diff --git a/interpreter/cling/lib/Utils/EmuTLS.cpp b/interpreter/cling/lib/Utils/EmuTLS.cpp index eccbf53366e3b..c884e90406653 100644 --- a/interpreter/cling/lib/Utils/EmuTLS.cpp +++ b/interpreter/cling/lib/Utils/EmuTLS.cpp @@ -7,7 +7,7 @@ * * ===----------------------------------------------------------------------=== */ -#include + #include #include #include @@ -15,6 +15,23 @@ #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 + +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. */ @@ -22,26 +39,6 @@ #define EMUTLS_USE_POSIX_MEMALIGN 0 #endif -/* 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 unsigned int gcc_word __attribute__((mode(word))); -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; - static __inline void *emutls_memalign_alloc(size_t align, size_t size) { void *base; #if EMUTLS_USE_POSIX_MEMALIGN @@ -50,7 +47,7 @@ static __inline void *emutls_memalign_alloc(size_t align, size_t size) { #else #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void*)) char* object; - if ((object = malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL) + if ((object = (char*)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL) abort(); base = (void*)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES)) & ~(uintptr_t)(align - 1)); @@ -69,10 +66,192 @@ static __inline void emutls_memalign_free(void *base) { #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 +#include +#include +#include +#include + +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; +} + +enum { __ATOMIC_ACQUIRE, __ATOMIC_RELEASE }; + +static __inline uintptr_t __atomic_load_n(void *ptr, unsigned type) { + assert(type == __ATOMIC_ACQUIRE); + return (uintptr_t) _load_be_u64(ptr); +} + +static __inline void __atomic_store_n(void *ptr, uintptr_t val, unsigned type) { + assert(type == __ATOMIC_RELEASE); + _store_be_u64(ptr, val); +} +#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. */ - typedef unsigned int gcc_pointer __attribute__((mode(pointer))); COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer)); COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*)); @@ -93,45 +272,19 @@ static __inline void *emutls_allocate_object(__emutls_control *control) { return base; } -static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER; - -static size_t emutls_num_object = 0; /* number of allocated TLS objects */ - -typedef struct emutls_address_array { - uintptr_t size; /* number of elements in the 'data' array */ - void* data[]; -} emutls_address_array; - -static pthread_key_t emutls_pthread_key; - -static void emutls_key_destructor(void* ptr) { - emutls_address_array* array = (emutls_address_array*)ptr; - uintptr_t i; - for (i = 0; i < array->size; ++i) { - if (array->data[i]) - emutls_memalign_free(array->data[i]); - } - free(ptr); -} - -static void emutls_init(void) { - if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0) - abort(); -} /* 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) { - static pthread_once_t once = PTHREAD_ONCE_INIT; - pthread_once(&once, emutls_init); - pthread_mutex_lock(&emutls_mutex); + emutls_init_once(); + emutls_lock(); index = control->object.index; if (!index) { index = ++emutls_num_object; __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE); } - pthread_mutex_unlock(&emutls_mutex); + emutls_unlock(); } return index; } @@ -142,7 +295,7 @@ static __inline void emutls_check_array_set_size(emutls_address_array *array, if (array == NULL) abort(); array->size = size; - pthread_setspecific(emutls_pthread_key, (void*)array); + emutls_setspecific(array); } /* Returns the new 'data' array size, number of elements, @@ -156,22 +309,29 @@ static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) { 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 = pthread_getspecific(emutls_pthread_key); + emutls_address_array* array = emutls_getspecific(); if (array == NULL) { uintptr_t new_size = emutls_new_data_array_size(index); - array = malloc(new_size * sizeof(void *) + sizeof(emutls_address_array)); + 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 = realloc(array, new_size * sizeof(void *) + sizeof(emutls_address_array)); + array = (emutls_address_array*) realloc(array, emutls_asize(new_size)); if (array) memset(array->data + orig_size, 0, (new_size - orig_size) * sizeof(void*)); @@ -182,8 +342,8 @@ emutls_get_address_array(uintptr_t index) { 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 - 1] == NULL) - array->data[index - 1] = emutls_allocate_object(control); - return array->data[index - 1]; + 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]; } From eff7edf7bf358bf33171f41c45f5d9958d4adda7 Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Wed, 8 Mar 2017 22:09:41 -0500 Subject: [PATCH 4/5] Enable emulated TLS and add test. --- .../lib/Interpreter/IncrementalExecutor.cpp | 3 ++ interpreter/cling/lib/Utils/CMakeLists.txt | 1 + interpreter/cling/lib/Utils/EmuTLS.cpp | 17 ++++-- .../cling/test/CodeGeneration/TLSVars.C | 54 +++++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 interpreter/cling/test/CodeGeneration/TLSVars.C diff --git a/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp b/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp index 1b1b2c79b77d3..1519e34ff6c0a 100644 --- a/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp +++ b/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp @@ -97,6 +97,9 @@ IncrementalExecutor::IncrementalExecutor(clang::DiagnosticsEngine& diags, m_AtExitFuncs.reserve(256); std::unique_ptr TM(CreateHostTargetMachine(CI)); + + TM->Options.EmulatedTLS = 1; + m_BackendPasses.reset(new BackendPasses(CI.getCodeGenOpts(), CI.getTargetOpts(), CI.getLangOpts(), diff --git a/interpreter/cling/lib/Utils/CMakeLists.txt b/interpreter/cling/lib/Utils/CMakeLists.txt index 79674223454e9..c26f1d10662ee 100644 --- a/interpreter/cling/lib/Utils/CMakeLists.txt +++ b/interpreter/cling/lib/Utils/CMakeLists.txt @@ -25,6 +25,7 @@ endif() add_cling_library(clingUtils OBJECT AST.cpp Diagnostics.cpp + EmuTLS.cpp ParserStateRAII.cpp Output.cpp Paths.cpp diff --git a/interpreter/cling/lib/Utils/EmuTLS.cpp b/interpreter/cling/lib/Utils/EmuTLS.cpp index c884e90406653..69dcc8bb35b7f 100644 --- a/interpreter/cling/lib/Utils/EmuTLS.cpp +++ b/interpreter/cling/lib/Utils/EmuTLS.cpp @@ -5,15 +5,24 @@ * This file is dual licensed under the MIT and the University of Illinois Open * Source Licenses. See LICENSE.TXT for details. * + * Taken from compiler-rt/lib/builtins/emutls.c with additions for Windows + * * ===----------------------------------------------------------------------=== */ +// When compiling with GCC, use its version of __emutls_get_address + +#if defined(__clang__) || defined(_WIN32) + +#include "cling/Utils/Casting.h" + #include #include #include -#include "int_lib.h" -#include "int_util.h" +#ifndef COMPILE_TIME_ASSERT +#define COMPILE_TIME_ASSERT(X) static_assert(X, "Error") +#endif typedef struct emutls_address_array { uintptr_t size; /* number of elements in the 'data' array */ @@ -340,10 +349,12 @@ emutls_get_address_array(uintptr_t index) { return array; } -void* __emutls_get_address(__emutls_control* control) { +extern "C" 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]; } + +#endif diff --git a/interpreter/cling/test/CodeGeneration/TLSVars.C b/interpreter/cling/test/CodeGeneration/TLSVars.C new file mode 100644 index 0000000000000..1728d1e4ffa87 --- /dev/null +++ b/interpreter/cling/test/CodeGeneration/TLSVars.C @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// CLING - the C++ LLVM-based InterpreterG :) +// +// This file is dual-licensed: you can choose to license it under the University +// of Illinois Open Source License or the GNU Lesser General Public License. See +// LICENSE.TXT for details. +//------------------------------------------------------------------------------ + +// RUN: cat %s | %cling -Xclang -verify | FileCheck %s + +// Test whether the TLS data is properly handled + +#include +#include +#include +#include +#include + +thread_local unsigned int TLSCounter = 1; +std::mutex WriteMutex; + +static void WriteValue(const char* Name, unsigned Val) { + std::lock_guard Lock(WriteMutex); + printf("TLSCounter for '%s' : %u\n", Name, Val); +} + +void TLSIncrement(const char* Name) { + ++TLSCounter; + if (const int Incr = atoi(Name)) + TLSCounter += Incr; + WriteValue(Name, TLSCounter); +} + +static const char* Name[] = { + "A", + "B", + "10", + "5", + 0, +}; +for (unsigned i = 0; Name[i]; ++i) { + std::thread(TLSIncrement, Name[i]).join(); +} +WriteValue("main", TLSCounter); + + +// CHECK: TLSCounter for 'A' : 2 +// CHECK: TLSCounter for 'B' : 2 +// CHECK: TLSCounter for '10' : 12 +// CHECK: TLSCounter for '5' : 7 +// CHECK: TLSCounter for 'main' : 1 + +// expected-no-diagnostics +.q From a96d2b3fc9c973b9003dcb09f2f88bf444a1434d Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Wed, 8 Mar 2017 22:29:38 -0500 Subject: [PATCH 5/5] =?UTF-8?q?Windows:=20Return=20current=20thread?= =?UTF-8?q?=E2=80=99s=20=5FInit=5Fthread=5Fepoch=20from=20=5F=5Femutls=5Fg?= =?UTF-8?q?et=5Faddress.=20Fixes=20function=20level=20static=20variables.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cling/lib/Interpreter/Interpreter.cpp | 12 ++++++ interpreter/cling/lib/Utils/EmuTLS.cpp | 8 ++++ .../cling/test/CodeGeneration/TLSVars.C | 40 +++++++++++++++---- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/interpreter/cling/lib/Interpreter/Interpreter.cpp b/interpreter/cling/lib/Interpreter/Interpreter.cpp index 50965d71565e6..e1fbd276964f2 100644 --- a/interpreter/cling/lib/Interpreter/Interpreter.cpp +++ b/interpreter/cling/lib/Interpreter/Interpreter.cpp @@ -61,6 +61,8 @@ using namespace clang; +extern "C" void* __emutls_get_address(struct __emutls_control*); + namespace { // Forward cxa_atexit for global d'tors. @@ -267,6 +269,16 @@ namespace cling { } } } +#ifdef LLVM_ON_WIN32 + // FIXME: Using emulated TLS LLVM doesn't respect external TLS data. + // By passing itself as the argument to __emutls_get_address, it can + // return a pointer to the current thread's _Init_thread_epoch. + // This obviously handles only one case, and would need to be rethought + // to properly support extern __declspec(thread), though hopefully that + // construct is dubious enough to never be used . + m_Executor->addSymbol("__emutls_v._Init_thread_epoch", + utils::FunctionToVoidPtr(&__emutls_get_address), true); +#endif } // Disable suggestions for ROOT diff --git a/interpreter/cling/lib/Utils/EmuTLS.cpp b/interpreter/cling/lib/Utils/EmuTLS.cpp index 69dcc8bb35b7f..011d57630465e 100644 --- a/interpreter/cling/lib/Utils/EmuTLS.cpp +++ b/interpreter/cling/lib/Utils/EmuTLS.cpp @@ -350,6 +350,14 @@ emutls_get_address_array(uintptr_t index) { } extern "C" void* __emutls_get_address(__emutls_control* control) { +#ifdef _WIN32 + // FIXME: Using emulated TLS LLVM doesn't respect external TLS data. + // To get arround this Interpreter::Initialize binds + // "__emutls_v._Init_thread_epoch" to &__emutls_get_address to mark that + // the current thread's _Init_thread_epoch should be returned. + if (reinterpret_cast(control) == cling::utils::FunctionToVoidPtr(&__emutls_get_address)) + return &_Init_thread_epoch; +#endif uintptr_t index = emutls_get_index(control); emutls_address_array* array = emutls_get_address_array(index--); if (array->data[index] == NULL) diff --git a/interpreter/cling/test/CodeGeneration/TLSVars.C b/interpreter/cling/test/CodeGeneration/TLSVars.C index 1728d1e4ffa87..ed43a6a1a4025 100644 --- a/interpreter/cling/test/CodeGeneration/TLSVars.C +++ b/interpreter/cling/test/CodeGeneration/TLSVars.C @@ -6,7 +6,7 @@ // LICENSE.TXT for details. //------------------------------------------------------------------------------ -// RUN: cat %s | %cling -Xclang -verify | FileCheck %s +// RUN: cat %s | %cling -Xclang -verify 2>&1 | FileCheck %s // Test whether the TLS data is properly handled @@ -17,10 +17,10 @@ #include thread_local unsigned int TLSCounter = 1; -std::mutex WriteMutex; +// std::mutex WriteMutex; static void WriteValue(const char* Name, unsigned Val) { - std::lock_guard Lock(WriteMutex); + // std::lock_guard Lock(WriteMutex); printf("TLSCounter for '%s' : %u\n", Name, Val); } @@ -45,10 +45,36 @@ WriteValue("main", TLSCounter); // CHECK: TLSCounter for 'A' : 2 -// CHECK: TLSCounter for 'B' : 2 -// CHECK: TLSCounter for '10' : 12 -// CHECK: TLSCounter for '5' : 7 -// CHECK: TLSCounter for 'main' : 1 +// CHECK-NEXT: TLSCounter for 'B' : 2 +// CHECK-NEXT: TLSCounter for '10' : 12 +// CHECK-NEXT: TLSCounter for '5' : 7 +// CHECK-NEXT: TLSCounter for 'main' : 1 + +// Below is predominately for Windows +struct TERD { + const char *Name; + TERD(const char *N) : Name(N) { printf("TERD::TERD::%s\n", Name); } + ~TERD() { printf("TERD::~TERD::%s\n", Name); } +}; +static TERD& inst01() { + static TERD st01("inst01"); + return st01; +} +static TERD& inst02() { + static TERD st02("inst02"); + return st02; +} + +inst01(); +// CHECK-NEXT: TERD::TERD::inst01 +std::thread(&inst01).join(); + +std::thread(&inst02).join(); +// CHECK-NEXT: TERD::TERD::inst02 +inst02(); // expected-no-diagnostics .q + +// CHECK-NEXT: TERD::~TERD::inst02 +// CHECK-NEXT: TERD::~TERD::inst01