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

            Bug ID: 86929
           Summary: `-fstack-protector` results in wrong code
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---
            Target: *-w64-mingw32

The following program:
```
void test(int n)
{
  char str[50];
}
int main(void)
{
  test(42);
}
```

, after being compiled with

```
x86_64-w64-mingw32-gcc -fstack-protector -O0 test.c
```

, results in assembly that references the FS segment register

```
test:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $96, %rsp
    .seh_stackalloc 96
    .seh_endprologue
    movl    %ecx, 16(%rbp)
    movq    %fs:0, %rax
```

, which causes crashes on x64, as on Windows this not the correct way to
reference native thread-local storage. At the moment, GCC should use emutls
instead.

Compiling this program with i686-w64-mingw32-gcc does not results in an
immediate segment fault, but the assembly code still references the GS segment
register, so it is still wrong.

Prior to GCC 8, stack protectors used static, non-thread-local pointers, which
were subject to data races, but didn't result in segment fault for
single-threaded programs.

Reply via email to