Skip to content

Commit fdb3b8b

Browse files
committed
Fix pthread tls bug.
Signed-off-by: Zhang Lili <lili.z.zhang@intel.com>
1 parent effae62 commit fdb3b8b

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

sdk/pthread/pthread.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ __thread pthread_info pthread_info_tls = {NULL, NULL, {0, 0, 0, 0, 0, 0, 0, 0},
8989

9090
bool _pthread_enabled(void)
9191
{
92-
pthread_info_tls.m_local_storage = NULL;
92+
if(is_tcs_binding_mode() == false)
93+
{
94+
pthread_info_tls.m_local_storage = NULL;
95+
}
9396
pthread_info_tls.m_pthread = NULL;
9497
pthread_info_tls.m_state = SGX_SUCCESS;
9598
memset((char*)&pthread_info_tls.m_mark, 0x00, sizeof(jmp_buf));

sdk/pthread/pthread_tls.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "internal/arch.h"
2626
#include "internal/thread_data.h"
2727
#include "trts_internal.h"
28+
#include "trts_util.h"
2829

2930
#define PTHREAD_DESTRUCTOR_ITERATIONS 4
3031
#define PTHREAD_KEYS_MAX 256
@@ -173,7 +174,13 @@ void _pthread_tls_destructors(void)
173174
struct sgx_pthread_storage *rs;
174175
int i;
175176

176-
if(NULL == pthread_info_tls.m_pthread)
177+
// For a child thread created inside enclave, we need to clear the TLS when
178+
// the thread exits.
179+
// In other cases, we need to make decision based on the tcs policy:
180+
// 1) Unbinding mode, we need to clear the TLS before the root ecall exits.
181+
// 2) Binding mode, we need to persist the TLS across multiple ECALLs. In this
182+
// case, the destructor should not be called.
183+
if(NULL == pthread_info_tls.m_pthread && true == is_tcs_binding_mode())
177184
//do nothing
178185
return;
179186

sdk/trts/trts_util.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,8 @@ bool is_pkru_enabled()
213213
return true;
214214
return false;
215215
}
216+
217+
bool is_tcs_binding_mode()
218+
{
219+
return g_global_data.thread_policy == 0 ? true : false;
220+
}

sdk/trts/trts_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ bool is_utility_thread();
6060
size_t get_max_tcs_num();
6161
bool is_pkru_enabled();
6262

63+
bool is_tcs_binding_mode();
64+
6365
#ifdef __cplusplus
6466
}
6567
#endif

0 commit comments

Comments
 (0)