[llvm] [StackProtector] Clear out stack protector slot (PR #65461)

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 6 13:20:47 PDT 2023


rnk wrote:

Regarding XOR, LLVM already implements this for Windows, see `X86TargetLowering::emitStackGuardXorFP` (I can't link to it on GitHub because X86ISelLowering is 2.2MB (!) but that's another matter...), and I don't think anything stops us from reusing that logic on other platforms.

Regarding `__llvm_stack_protector_check`, it is an idea to optimize code size potentially at some performance cost, since it requires an extra call. In the godbolt example we generate this code sequence:
```
.... # regular code
  movq %fs:40, %rax
  cmpq 16(%rsp), %rax
  jne .LBB0_2
  # Normal epilogue
  addq $24, %rsp
  retq
  # extra BB to abort
.LBB0_2:
  callq __stack_chk_fail at PLT
```

The idea is that we could make the code more compact by loading the stack cookie into a register parameter, and then calling a helper that does the comparison, so we'd get this instead:
```
... # regular code
  mov 16(%rsp), %rdi
  callq __llvm_stack_protector_check
  # Normal epilogue
  addq $24, %rsp
  retq
...
__llvm_stack_protector_check:
  movq %fs:40, %rax
  cmpq %rax, %rdi
  jne .Labort
  retq
.Labort:
  subq $4, %rsp  # maintain 16 byte stack alignment
  callq __stack_chk_fail at PLT
```

https://github.com/llvm/llvm-project/pull/65461


More information about the llvm-commits mailing list