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

            Bug ID: 108799
           Summary: Improper deprecation diagnostic for rsp clobber
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrew.cooper3 at citrix dot com
  Target Milestone: ---

Originally from LKML. 
https://lore.kernel.org/lkml/y9lfmq%2fr1%2fpep...@biznet-home.integral.gnuweeb.org/

Slightly modified example: https://godbolt.org/z/xx76nEvKM

Given:
  static void clobber_redzone_buggy(void)
  {
      register unsigned long rsp asm("rsp");
      unsigned long fl;

      asm volatile ("pushf; popq %[fl]"
                    : [fl] "=r" (fl)
                       , "+r" (rsp)
                    :
                    : //"rsp"
      );
  }

  static void set_red_zone(long *mem, long val)
  {
          __asm__ volatile ("movq %[val], %[mem]"
                            : [mem] "=m" (*mem)
                            : [val] "r" (val));
  }

  static long get_red_zone(long *mem)
  {
          long ret;

          __asm__ volatile ("movq %[in], %[out]"
                            : [out] "=r" (ret)
                            : [in] "m" (*mem));
          return ret;
  }

  long a_leaf_func_with_red_zone(void)
  {
          long x;

          set_red_zone(&x, 1);
          clobber_redzone_buggy();
          /* The correct retval is 1 */
          return get_red_zone(&x);
  }

gcc generates:

  a_leaf_func_with_red_zone:
          movl    $1, %eax
          movq    %rax, -8(%rsp)
          pushf
          popq    %rax
          movq    -8(%rsp), %rax
          ret

which is buggy because the asm clobbers the same redzone slot as `x` occupies.

Swapping the "+r"(rsp) constraint for an explicit "rsp" clobber generates:

  a_leaf_func_with_red_zone:
          pushq   %rbp
          movl    $1, %eax
          movq    %rsp, %rbp
          subq    $16, %rsp
          movq    %rax, -8(%rbp)
          pushf
          popq    %rax
          movq    -8(%rbp), %rax
          leave
          ret

which seems to do the right thing.  It sets up a full stack frame and avoids
using the redzone.

However, doing so yields:

  warning: listing the stack pointer register 'rsp' in a clobber list is
deprecated [-Wdeprecated]
  note: the value of the stack pointer after an 'asm' statement must be the
same as it was before the statement

The note is incorrect.  For ABIs with a redzone, the requirement is stricter
than simply preserving the value of the stack pointer.

The warning suggests that there ought to be a different way to express "this
clobbers the redzone", but there doesn't appear to be any other way.  If this
is the case, why is it deprecated?

Reply via email to