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

            Bug ID: 129156
           Summary: [16/17 Regression] x86 separate shrink-wrapping
                    allocates the frame without stack-clash probes when
                    the locals fit in a page but the register saves don't
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at gaul dot org
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

Created attachment 66341
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66341&action=edit
the test case

Testcase (reduced with cvise from gnulib's at-func.c as built in GNU tar):

void name(char *, int, char *);
long get(char *, int);
long f(int fd, char *file, int type) {
  if (fd < 0)
    return get(file, type);
  char buf[4096];
  name(buf, fd, file);
  get(file, type);
  return fd;
}

$ gcc -O2 -fstack-clash-protection -S t.c

f:
        subq    $4120, %rsp
        movq    %rbp, 4104(%rsp)
        movq    %rsi, %rbp
        testl   %edi, %edi
        js      .L5
        movq    %rbx, 4096(%rsp)
        movl    %edi, %ebx
        movq    %rsp, %rdi
        movq    %r12, 4112(%rsp)
        ...
        call    name

The whole 4120-byte frame is allocated by one subq with no probe. The only
stores into it are the register saves at the top; the next store below them
is the call's push of the return address, 4128 bytes below the caller's.
That exceeds the 4 KiB guard the probing assumes
(--param stack-clash-protection-guard-size=12).

With -fno-shrink-wrap-separate the same function is probed:

        pushq   %r12
        pushq   %rbp
        pushq   %rbx
        subq    $4096, %rsp
        orb     $0, (%rsp)

GCC 15.3, which does not shrink-wrap separately on x86, probes the same way.

Buffer sizes from 4072 to 4096 give the unprobed allocation (subq $4104 or
$4120). Above 4096 the registers are pushed again and the frame is probed.
The excess is the general-register save area: at most 8 bytes per
callee-saved register.

Run-time effect: the attached harness.c and switch.S call f on a stack whose
4 KiB PROT_NONE page lies just below f's register saves; name() does not
touch buf.

$ gcc -O2 -fstack-clash-protection -c -o f.o t.c
$ gcc -O0 -o h harness.c switch.S f.o && ./h

  gcc 16.2.1 -O2 -fstack-clash-protection:
    f returned 7: the frame stepped over the guard page
  gcc 16.2.1 -O2 -fstack-clash-protection -fno-shrink-wrap-separate:
    SIGSEGV: the frame touched the guard page
  gcc 15.3.0 -O2 -fstack-clash-protection -fno-stack-protector:
    SIGSEGV: the frame touched the guard page

glibc's default pthread guard is one page, so on thread stacks a call path
that leaves f's entry stack pointer in that 24-byte window skips the guard.

Cause, as I read gcc/config/i386/i386.cc on master (d2ad1dba8cb):

* ix86_get_separate_components declines separate wrapping when
  save_regs_using_push_pop (to_allocate) holds, which under
  -fstack-clash-protection means to_allocate > get_probe_interval ().
  But to_allocate there is stack_pointer_offset - sse_reg_save_offset,
  which leaves out the general-register save area.
  ix86_compute_frame_layout computes it the same way.
* For a function that is shrink-wrapped separately, ix86_expand_prologue
  takes the red-zone branch with crtl->shrink_wrapped_separate. It
  allocates the whole frame, register save area included, with one
  pro_epilogue_adjust_stack and sets m->fs.sp_offset to
  stack_pointer_offset, so the ix86_adjust_stack_and_probe call further
  down has nothing left to allocate. In the pro_and_epilogue dump, the
  first prologue expansion reports "Stack clash inline probes in
  prologue."; the second, after "The components we wrap separately are
  [sep 3 6]", reports "Stack clash no probe small stack adjustment in
  prologue."
* x86 separate shrink-wrapping came in with r16-1551-g2c30f828e45078. Until
  the PR120697 fix (r16-1608-g1f2e4058e57, 1f2e4058e57) the stack-clash
  paths in ix86_expand_prologue asserted !crtl->shrink_wrapped_separate;
  that fix removed the assertions, so this combination now compiles
  silently.

Gating separate wrapping on the whole allocation, register saves included,
or probing the combined allocation in the shrink-wrapped branch would close
it.

Seen in Fedora 45 packages built with GCC 16.2.1 and
-fstack-clash-protection (the flag is in each unit's DW_AT_producer):
tar (tar_acl_get_file_at, tar_acl_set_file_at, file_has_acl_at),
libsodium (ge25519_scalarmult, generate_addresses), libicuuc
(u_strToPunycode), libaom (model_skip_for_sb_y_large). Each allocates
0x1010-0x1020 bytes in one unprobed sub. Ubuntu 26.10's GCC 15 builds of
the same code have no such frames.

Possibly related, different mechanism: PR127334 (unprobed outgoing
aggregate argument block from push_block).

$ gcc -v
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap ... --enable-cet
  --with-tune=generic --with-tls=gnu2 --with-arch_32=i686
  --build=x86_64-redhat-linux --with-build-config=bootstrap-lto ...
gcc version 16.2.1 20261029 (Red Hat 16.2.1-3) (GCC)

Reply via email to