Skip to content

Commit 64c123a

Browse files
committed
app/memcmp: improve rflags.tf simulation stepping
1 parent 27ff720 commit 64c123a

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

app/memcmp/main.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <sys/mman.h>
1616

1717
#define MAX_LEN 15
18-
#define DO_TIMER_STEP 1
18+
#define DO_TIMER_STEP 0
1919
#define DEBUG 0
2020
#define DBG_ENCL 1
2121
#if DO_TIMER_STEP
@@ -74,7 +74,7 @@ void aep_cb_func(void)
7474
* referencing the enclave code page about to be executed, so as to be able
7575
* to filter out "zero-step" results that won't set the accessed bit.
7676
*/
77-
if (ACCESSED(*pte_encl)) step_cnt++;
77+
if (do_irq && ACCESSED(*pte_encl)) step_cnt++;
7878
*pte_encl = MARK_NOT_ACCESSED( *pte_encl );
7979
*pte_trigger = MARK_NOT_ACCESSED(*pte_trigger);
8080

@@ -92,8 +92,6 @@ void aep_cb_func(void)
9292
*pmd_encl = MARK_NOT_ACCESSED( *pmd_encl );
9393
#if DO_TIMER_STEP
9494
apic_timer_irq( SGX_STEP_TIMER_INTERVAL );
95-
#else
96-
ENABLE_TF;
9795
#endif
9896
}
9997
}
@@ -117,6 +115,7 @@ void fault_handler(int signo, siginfo_t * si, void *ctx)
117115
#endif
118116
ASSERT(!mprotect(trigger_adrs, 4096, PROT_READ | PROT_WRITE));
119117
do_irq = 1;
118+
sgx_step_do_trap = 1;
120119
}
121120
else
122121
{
@@ -130,6 +129,10 @@ void fault_handler(int signo, siginfo_t * si, void *ctx)
130129
#if DEBUG
131130
//info("Caught single-step trap (RIP=%p)\n", si->si_addr);
132131
#endif
132+
133+
/* ensure RFLAGS.TF is clear to disable debug single-stepping */
134+
ucontext_t *uc = (ucontext_t *) ctx;
135+
uc->uc_mcontext.gregs[REG_EFL] &= ~0x100;
133136
break;
134137
#endif
135138

@@ -234,6 +237,7 @@ int main( int argc, char **argv )
234237
for (int j = 0; j < pwd_len; j++) pwd[j] = '*';
235238
pwd[pwd_len] = '\0';
236239
do_irq = 0; trigger_cnt = 0, step_cnt = 0, fault_cnt = 0;
240+
sgx_step_do_trap = 0;
237241
ASSERT(!mprotect(trigger_adrs, 4096, PROT_NONE ));
238242
SGX_ASSERT( memcmp_pwd(eid, &pwd_success, pwd) );
239243

@@ -261,6 +265,7 @@ int main( int argc, char **argv )
261265
{
262266
pwd[i] = j;
263267
do_irq = 0; trigger_cnt = 0, step_cnt = 0, fault_cnt = 0;
268+
sgx_step_do_trap = 0;
264269
ASSERT(!mprotect(trigger_adrs, 4096, PROT_NONE ));
265270
SGX_ASSERT( memcmp_pwd(eid, &pwd_success, pwd) );
266271

libsgxstep/aep_trampoline.S

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ sgx_step_aep_trampoline:
7979

8080
mov $3, %xax /* ERESUME leaf */
8181

82+
83+
/* optionally set RFLAGS.TF to enable simulated single-stepping for DEBUG enclaves */
84+
lea sgx_step_do_trap(%rip), %rdx
85+
test %rdx, %rdx
86+
je sgx_step_aep_eresume
87+
pushf
88+
orl $0x100, (%rsp)
89+
popf
90+
8291
.global sgx_step_aep_eresume
8392
sgx_step_aep_eresume:
8493
.byte 0x0f, 0x01, 0xd7 /* ENCLU */

libsgxstep/enclave.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern void sgx_step_aep_trampoline(void);
3838
aep_cb_t sgx_step_aep_cb = NULL;
3939
uint64_t nemesis_tsc_eresume = 0x0;
4040
int sgx_step_eresume_cnt = 0;
41+
int sgx_step_do_trap = 0;
4142

4243
extern int fd_step;
4344
struct sgx_step_enclave_info victim = {0};

libsgxstep/enclave.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct sgx_step_enclave_info
3636

3737
extern uint64_t nemesis_tsc_eresume, nemesis_tsc_aex;
3838
extern int sgx_step_eresume_cnt;
39+
extern int sgx_step_do_trap;
3940

4041
typedef void (*aep_cb_t)(void);
4142
void register_aep_cb(aep_cb_t cb);

0 commit comments

Comments
 (0)