Skip to content

Commit

Permalink
Fix pthread tls bug.
Browse files Browse the repository at this point in the history
Signed-off-by: Zhang Lili <[email protected]>
  • Loading branch information
lzha101 committed Jun 17, 2022
1 parent effae62 commit fdb3b8b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion sdk/pthread/pthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
9 changes: 8 additions & 1 deletion sdk/pthread/pthread_tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions sdk/trts/trts_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions sdk/trts/trts_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fdb3b8b

Please sign in to comment.