[PATCH] D68720: Support -fstack-clash-protection for x86

serge via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 00:20:39 PDT 2019


serge-sans-paille added a subscriber: eli.friedman.
serge-sans-paille added a comment.

@efriedma  the free probe algorithm requires more testing, and I'd like to take into account memset and memcpy as free probes too. To showcase this algorithm, consider the following LLVM bitcode:

  define i32 @foo() local_unnamed_addr {
    %a = alloca i32, i64 2000, align 16
    %b = getelementptr inbounds i32, i32* %a, i64 1198
    store volatile i32 1, i32* %b
    %c = load volatile i32, i32* %a
    ret i32 %c
  }

when compiled with `llc` it outputs the following assembly:

  foo:                                    # @foo
      subq    $7880, %rsp             # imm = 0x1EC8
      movl    $1, 4664(%rsp)
      movl    -128(%rsp), %eax
      addq    $7880, %rsp             # imm = 0x1EC8
      retq

When `probe-stack` is set to `inline-asm` it outputs

  foo:                                    # @foo
  	subq	$4096, %rsp             # imm = 0x1000
  	movl	$1, 880(%rsp)
  	subq	$3784, %rsp             # imm = 0xEC8
  	movq	$0, (%rsp)
  	movl	-128(%rsp), %eax
  	addq	$7880, %rsp             # imm = 0x1EC8
  	retq

The stack allocation is split in two, but only one MOV is added, the first one is what I call a free probe. Turns out we could only use natural probes here, I need to implement that :-)

As a comparison, setting `probe-stack` to a random function name like `__probe_stack` outputs the following:

  foo:                                    # @foo
  	movl	$8008, %eax             # imm = 0x1F48
  	callq	__probe_stack
  	subq	%rax, %rsp
  	movl	$1, 4792(%rsp)
  	movl	(%rsp), %eax
  	addq	$8008, %rsp             # imm = 0x1F48
  	retq

which requires runtime support (to provide `__stack_probe`), and a function call overhead, while ideally just an extra `sub %rsp` would be needed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D68720/new/

https://reviews.llvm.org/D68720





More information about the llvm-commits mailing list