i386: Improve CET shadow stack instruction modeling for correctness
Correctly models memory and flag effects of Intel CET shadow stack instructions to prevent incorrect optimizations.
This commit corrects the modeling of Intel Control-flow Enforcement Technology (CET) shadow stack instructions on i386. Instructions like rstorssp, wrss, wruss, and clrRbsy previously described their memory operands incorrectly, allowing optimizations such as Common Subexpression Elimination (CSE) to use stale memory values. Additionally, the clrRbsy instruction’s clobbering of flags was not declared, enabling incorrect reuse of flags. The changes accurately describe memory operand effects and declare flag clobbers, ensuring compiler passes operate on correct program semantics.
In Details
This commit refines the machine description patterns for several Intel CET (Control-flow Enforcement Technology) shadow stack instructions on the i386 architecture. The patterns for rstorssp, wrss, wruss, and clrRbsy previously used UNSPEC_VOLATILE for memory operands, which incorrectly declared them as inputs without modification. This allowed passes like CSE to potentially reuse stale memory values. The fix changes the memory operand description to accurately reflect whether it's read/modified (+m) or just set (=m). Furthermore, clrRbsy was found to clobber FLAGS_REG, whic…
- CET
- Control-flow Enforcement Technology. A set of hardware features designed to mitigate control-flow hijacking attacks, such as Return-Oriented Programming (ROP).
- Shadow Stack
- A hardware-supported duplicate of the call stack that stores return addresses. It is used by CET to detect and prevent stack-based attacks.
- i386
- The Intel 80386 microprocessor architecture, a 32-bit extension of the earlier x86 architecture. GCC supports code generation for this architecture.
- rstorssp
- Restore Shadow Stack Pointer. An instruction related to CET that restores the shadow stack pointer.
- wrss
- Write Shadow Stack. An instruction related to CET that writes to the shadow stack.
- wruss
- Write User Shadow Stack. An instruction related to CET that writes to the user-level shadow stack.
- clrRbsy
- Clear Busy. An instruction related to CET that clears a busy status, possibly on the shadow stack or related structures.
- CSE
- Common Subexpression Elimination. An optimization pass that identifies and replaces redundant computations of the same expression with a single computation.
- UNSPEC_VOLATILE
- A GCC internal construct representing an operation whose side effects must not be optimized away and whose result cannot be assumed to be preserved across certain operations. It's often used for intrinsic functions or special machine instructions.
- FLAGS_REG
- The processor's flags register (e.g., EFLAGS on x86), which stores status bits like zero, carry, and overflow resulting from arithmetic operations.