Initialize allocators when thread local is empty
@@ -200,7 +200,6 @@ OE_EXPORT volatile uint64_t _tdata_align = 1;
OE_EXPORT volatile uint64_t _tbss_size = 0;
OE_EXPORT volatile uint64_t _tbss_align = 1;
-// Number of thread-local relocations.
static volatile bool _thread_locals_relocated = false;
// TODO: Make this flexible in case more than one page of thread local storage
@@ -314,7 +313,7 @@ oe_result_t oe_thread_local_init(oe_sgx_td_t* td)
// Copy the template
oe_memcpy_s(tls_start, _tdata_size, tdata, _tdata_size);
- // Perform thread-local relocations.
+ // Perform thread-local relocations, only run once.
if (!_thread_locals_relocated)
{
// Note: For an enclave, thread-local relocations always set the
@@ -346,17 +345,6 @@ oe_result_t oe_thread_local_init(oe_sgx_td_t* td)
_thread_locals_relocated = true;
}
-
- {
- static bool _allocator_initialized = false;
- bool initialized = _allocator_initialized;
- OE_ATOMIC_MEMORY_BARRIER_ACQUIRE();
- if (!initialized)
- {
- /* Initialize the allocator */
- OE_ATOMIC_MEMORY_BARRIER_RELEASE();
- _allocator_initialized = true;
- }
}
// To properly initialize the allocator, oe_allocator_init must first be
@@ -373,9 +361,10 @@ oe_result_t oe_thread_local_init(oe_sgx_td_t* td)
// called. This results in incorrect order of the allocator callbacks.
// Therefore, we call oe_allocator_init here (via oe_once)
// and then call oe_allocator_thread_init.
+ // Note that we need to initialize the allocator even when thread-local data
+ // is empty (i.e., tls_start is NULL when tdata and tbss are zero).
_initialize_allocator();
oe_allocator_thread_init();
- }
result = OE_OK;
done:
@@ -426,11 +415,11 @@ oe_result_t oe_thread_local_cleanup(oe_sgx_td_t* td)
/* Clear tls section if it exists */
uint8_t* fs = _get_fs_from_td(td);
uint8_t* tls_start = _get_thread_local_data_start(td);
- if (tls_start)
- {
+ // Invoke the cleanup function even when the thread-local data is empty
+ // (i.e., tls_start is NULL when tdata and tbss are zero).
oe_allocator_thread_cleanup();
+ if (tls_start)
oe_memset_s(tls_start, (uint64_t)(fs - tls_start), 0, 0);
- }
return OE_OK;
}
