From 82cd9d4cce8662dbc4b95b8f15d91b49a2d7aaf6 Mon Sep 17 00:00:00 2001 From: lzha101 Date: Tue, 18 May 2021 10:54:38 +0800 Subject: [PATCH] Fix simulation mode failure with tcmalloc. (#683) Signed-off-by: Zhang Lili --- sdk/selib/sgx_create_report.cpp | 14 +++++++++++--- sdk/simulation/tinst/rts_sim.h | 2 ++ sdk/trts/trts_xsave.cpp | 6 +++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/sdk/selib/sgx_create_report.cpp b/sdk/selib/sgx_create_report.cpp index 52c4b20da..e9d7e4122 100644 --- a/sdk/selib/sgx_create_report.cpp +++ b/sdk/selib/sgx_create_report.cpp @@ -44,6 +44,7 @@ #include "sgx_trts.h" #include "trts_inst.h" #include "se_cdefs.h" +#include "sgx_spinlock.h" // add a version to tservice. SGX_ACCESS_VERSION(tservice, 1) @@ -131,11 +132,18 @@ const sgx_report_t *sgx_self_report(void) .key_id = {0}, .mac = {0} }; + static sgx_spinlock_t report_lock = SGX_SPINLOCK_INITIALIZER; - // Below sgx_create_report() will be called only once during the enclave initialization, - // so there is no potential race conditional. if (0 == _report.body.attributes.flags) - sgx_create_report(nullptr, nullptr, &_report); + { + // sgx_create_report() only needs to be called once to get self report. + sgx_spin_lock(&report_lock); + if (0 == _report.body.attributes.flags) + { + sgx_create_report(nullptr, nullptr, &_report); + } + sgx_spin_unlock(&report_lock); + } return &_report; } diff --git a/sdk/simulation/tinst/rts_sim.h b/sdk/simulation/tinst/rts_sim.h index 31a6d16ca..55c79b824 100644 --- a/sdk/simulation/tinst/rts_sim.h +++ b/sdk/simulation/tinst/rts_sim.h @@ -67,6 +67,8 @@ typedef struct _global_data_sim_t uint64_t seed; /* to initialize the PRNG */ } global_data_sim_t; +extern global_data_sim_t g_global_data_sim; + #ifdef __cplusplus } #endif diff --git a/sdk/trts/trts_xsave.cpp b/sdk/trts/trts_xsave.cpp index fbdebd605..2806260b7 100644 --- a/sdk/trts/trts_xsave.cpp +++ b/sdk/trts/trts_xsave.cpp @@ -58,6 +58,7 @@ int g_xsave_enabled __attribute__((section(".nipd"))) = 0; // flag to in #ifdef SE_SIM uint32_t g_xsave_mask_high __attribute__((section(".nipd"))) = 0xFFFFFFFF; uint32_t g_xsave_mask_low __attribute__((section(".nipd"))) = 0xFFFFFFFF; +#include "rts_sim.h" #endif // EENTER will set xcr0 with secs.attr.xfrm, @@ -71,10 +72,13 @@ uint32_t g_xsave_mask_low __attribute__((section(".nipd"))) = 0xFFFFFFFF; SE_OPTIMIZE_OFF uint64_t get_xfeature_state() { +#ifndef SE_SIM auto *report = sgx_self_report(); g_xsave_enabled = (report->body.attributes.xfrm == SGX_XFRM_LEGACY) ? 0 : 1; uint64_t xfrm = report->body.attributes.xfrm; -#ifdef SE_SIM +#else + uint64_t xfrm = g_global_data_sim.secs_ptr->attributes.xfrm; + g_xsave_enabled = (xfrm == SGX_XFRM_LEGACY) ? 0 : 1; g_xsave_mask_high = (uint32_t)(xfrm >> 32); g_xsave_mask_low = (uint32_t)(xfrm & 0xFFFFFFFF); #endif