diff --git a/sdk/pthread/pthread.cpp b/sdk/pthread/pthread.cpp index 5be9060ec..c7298347d 100644 --- a/sdk/pthread/pthread.cpp +++ b/sdk/pthread/pthread.cpp @@ -89,7 +89,10 @@ __thread pthread_info pthread_info_tls = {NULL, NULL, {0, 0, 0, 0, 0, 0, 0, 0}, bool _pthread_enabled(void) { - pthread_info_tls.m_local_storage = NULL; + if(is_tcs_binding_mode() == false) + { + pthread_info_tls.m_local_storage = NULL; + } pthread_info_tls.m_pthread = NULL; pthread_info_tls.m_state = SGX_SUCCESS; memset((char*)&pthread_info_tls.m_mark, 0x00, sizeof(jmp_buf)); diff --git a/sdk/pthread/pthread_tls.cpp b/sdk/pthread/pthread_tls.cpp index fcdc14063..2a0596b06 100644 --- a/sdk/pthread/pthread_tls.cpp +++ b/sdk/pthread/pthread_tls.cpp @@ -25,6 +25,7 @@ #include "internal/arch.h" #include "internal/thread_data.h" #include "trts_internal.h" +#include "trts_util.h" #define PTHREAD_DESTRUCTOR_ITERATIONS 4 #define PTHREAD_KEYS_MAX 256 @@ -173,7 +174,13 @@ void _pthread_tls_destructors(void) struct sgx_pthread_storage *rs; int i; - if(NULL == pthread_info_tls.m_pthread) + // For a child thread created inside enclave, we need to clear the TLS when + // the thread exits. + // In other cases, we need to make decision based on the tcs policy: + // 1) Unbinding mode, we need to clear the TLS before the root ecall exits. + // 2) Binding mode, we need to persist the TLS across multiple ECALLs. In this + // case, the destructor should not be called. + if(NULL == pthread_info_tls.m_pthread && true == is_tcs_binding_mode()) //do nothing return; diff --git a/sdk/trts/trts_util.cpp b/sdk/trts/trts_util.cpp index 196bcad08..14a97df93 100644 --- a/sdk/trts/trts_util.cpp +++ b/sdk/trts/trts_util.cpp @@ -213,3 +213,8 @@ bool is_pkru_enabled() return true; return false; } + +bool is_tcs_binding_mode() +{ + return g_global_data.thread_policy == 0 ? true : false; +} diff --git a/sdk/trts/trts_util.h b/sdk/trts/trts_util.h index 88ef7ed92..196986bd8 100644 --- a/sdk/trts/trts_util.h +++ b/sdk/trts/trts_util.h @@ -60,6 +60,8 @@ bool is_utility_thread(); size_t get_max_tcs_num(); bool is_pkru_enabled(); +bool is_tcs_binding_mode(); + #ifdef __cplusplus } #endif