public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108799] New: Improper deprecation diagnostic for rsp clobber
@ 2023-02-15 10:28 andrew.cooper3 at citrix dot com
  2023-02-15 17:55 ` [Bug middle-end/108799] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: andrew.cooper3 at citrix dot com @ 2023-02-15 10:28 UTC (permalink / raw)
  To: gcc-bugs

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+uv@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?

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-01 13:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-15 10:28 [Bug c/108799] New: Improper deprecation diagnostic for rsp clobber andrew.cooper3 at citrix dot com
2023-02-15 17:55 ` [Bug middle-end/108799] " pinskia at gcc dot gnu.org
2023-02-15 18:03 ` andrew.cooper3 at citrix dot com
2023-04-01 13:32 ` pskocik at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).