sslab-gatech / opensgx

OpenSGX
Other
293 stars 80 forks source link

Compiler Optimization breaks ASM #36

Open sam1013 opened 8 years ago

sam1013 commented 8 years ago

Hi, when doing compiler optimization with -O2 or -O3, gcc wrongly optimizes enclu. Especially calls to EENTER, ERESUME, EEXIT might get corrupted. This is because gcc does not know about semantics of SGX instructions, which change control flow. Hence, gcc does not save caller's registers, which might get reused in the new context. --> Data corruption

The solution I used is to unify all enclu calls in a single function and mark this function noinline and returns_twice in the header file; example shown below.

sgx-user.h: extern void enclu(enclu_cmd_t leaf, uword_t rbx, uword_t rcx, uword_t rdx, out_regs_t* out_regs) __attribute__((noinline, returns_twice));