Re: [PATCH 3/5] selftests/sgx: Ensure correct secinfo struct alignment in test enclave.

From: Jo Van Bulck
Date: Mon Aug 07 2023 - 05:21:26 EST


On 03.08.23 06:00, Huang, Kai wrote:> We already have __aligned. Can you provide more information in what
circumstances that __aligned isn't enough to guarantee the alignment?

We have a lot of kernel code which has __aligned but doesn't have volatile.

Thank you. I also dug deeper into this and the proper fix is indeed not to make the variable volatile.

The real problem is that the inline assembly does not have the "memory" clobber to tell the compiler that the "assembly code performs memory reads or writes to items other than those listed in the input and output operands (for example, accessing the memory pointed to by one of the input parameters)" [1].

I checked that, depending on the optimizations and compiler (gcc vs clang), the compiler may indeed reorder the write to secinfo.flags to _after_ the inline asm block. Declaring secinfo as volatile fixed that, but the proper fix is of course to properly include a "memory" clobber for the asm block.

I'll include a fix to include a "memory" clobber for the inline asm block in the next patch revision.

[1] https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers-1