https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87220

            Bug ID: 87220
           Summary: -fstack-check produces inefficient and wrong tests
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugdal at aerifal dot cx
  Target Milestone: ---

Given the test case:

void bar(void *);
int foo()
{
    char a[10];
    bar(&a);
}

-fstack-check produces the code (x86_64; similar for 32-bit):

foo:
        subq    $4152, %rsp
        orq     $0, (%rsp)
        addq    $4128, %rsp
        leaq    6(%rsp), %rdi
        call    bar
        addq    $24, %rsp
        ret

This test:
1. Unnecessarily touches an extra page of stack that's not used, wasting
memory.
2. Skips over a whole page, potentially clobbering (data race) data on the
other side of a guard page.
3. Is completely unnecessary; since less than a page of stack is needed, only a
probe of the final desired stack pointer (after the third instruction) is
needed, and it's performed implicitly by the call.

I would expect at least:

foo:
        subq    $24, %rsp
        orq     $0, (%rsp)
        leaq    6(%rsp), %rdi
        call    bar
        addq    $24, %rsp
        ret

or ideally:

foo:
        subq    $24, %rsp
        leaq    6(%rsp), %rdi
        call    bar
        addq    $24, %rsp
        ret

The excessive stack usage, unsafety (jumping the guard page), and size and
potential performance hit from unnecessary checks seem to make -fstack-check
unsuitable for its intended purpose at this time.

Reply via email to