public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list
@ 2022-03-06 11:43 570070308 at qq dot com
  2022-03-06 11:46 ` [Bug c/104805] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: 570070308 at qq dot com @ 2022-03-06 11:43 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104805
           Summary: [12.0] x86_64 Extended asm may use rbp register to
                    input/output even thougth "rbp" is in the clobber list
                    when "rsp" and "rbp" are both in the in the clobber
                    list
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 570070308 at qq dot com
  Target Milestone: ---

test.c:
```
void kkk()
{
    char a;
    __asm__ volatile (
            "%0"
            :"+m"(a)
            :
            : "rsp","rbp"
            );
}
```

assembly code:
```
#APP
# 4 "test.c" 1
    -9(%rbp)
# 0 "" 2
#NO_APP
```

can see that it use `-9(%rbp)` to represent `a`. Accouding to
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers,
the rbp should not be use to represent any operands because it is in the
clobber list.

compile log:
```
ig@ig-virtual-machine:~/temp$ gcc-12 -S test.c -O1 -v
Using built-in specs.
COLLECT_GCC=gcc-12
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
12-20220302-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-12
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-12-VlTCdr/gcc-12-12-20220302/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-VlTCdr/gcc-12-12-20220302/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.0.1 20220302 (experimental) [master r12-7448-g58394373a70]
(Ubuntu 12-20220302-1ubuntu1) 
COLLECT_GCC_OPTIONS='-S' '-O1' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/12/cc1 -quiet -v -imultiarch x86_64-linux-gnu
test.c -quiet -dumpbase test.c -dumpbase-ext .c -mtune=generic -march=x86-64
-O1 -version -o test.s -fasynchronous-unwind-tables -fstack-protector-strong
-Wformat -Wformat-security -fstack-clash-protection -fcf-protection
GNU C17 (Ubuntu 12-20220302-1ubuntu1) version 12.0.1 20220302 (experimental)
[master r12-7448-g58394373a70] (x86_64-linux-gnu)
        compiled by GNU C version 12.0.1 20220302 (experimental) [master
r12-7448-g58394373a70], GMP version 6.2.1, MPFR version 4.1.0, MPC version
1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/12/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/12/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C17 (Ubuntu 12-20220302-1ubuntu1) version 12.0.1 20220302 (experimental)
[master r12-7448-g58394373a70] (x86_64-linux-gnu)
        compiled by GNU C version 12.0.1 20220302 (experimental) [master
r12-7448-g58394373a70], GMP version 6.2.1, MPFR version 4.1.0, MPC version
1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 3929bb57dd80f5cc2d4f3202c349e2dc
test.c: In function ‘kkk’:
test.c:4:5: warning: listing the stack pointer register ‘rsp’ in a clobber list
is deprecated [-Wdeprecated]
    4 |     __asm__ volatile (
      |     ^~~~~~~
test.c:4:5: note: the value of the stack pointer after an ‘asm’ statement must
be the same as it was before the statement
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-S' '-O1' '-v' '-mtune=generic' '-march=x86-64'
ig@ig-virtual-machine:~/temp$
```

if compile with -O0, then gcc will give an error.

if don't put "rsp" and "rbp" both in the clobber list, the result will be
right.

gcc is installed by apt on ubuntu 22.04

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

* [Bug c/104805] [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list
  2022-03-06 11:43 [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list 570070308 at qq dot com
@ 2022-03-06 11:46 ` jakub at gcc dot gnu.org
  2022-03-06 11:51 ` 570070308 at qq dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-03-06 11:46 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Clobber of "rsp" makes no sense, you can't change the value of the stack
pointer in inline asm without restoring it back before the end of inline asm.

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

* [Bug c/104805] [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list
  2022-03-06 11:43 [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list 570070308 at qq dot com
  2022-03-06 11:46 ` [Bug c/104805] " jakub at gcc dot gnu.org
@ 2022-03-06 11:51 ` 570070308 at qq dot com
  2022-03-06 12:09 ` 570070308 at qq dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: 570070308 at qq dot com @ 2022-03-06 11:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from 。 <570070308 at qq dot com> ---
(In reply to Jakub Jelinek from comment #1)
> Clobber of "rsp" makes no sense, you can't change the value of the stack
> pointer in inline asm without restoring it back before the end of inline asm.

I know that changing rsp is dangerous, and the gcc will give a warning if you
use rsp. I have never changed rsp in my inline-asm coding, or put "rsp" in the
clobber list. I'm just for finding bug and make gcc better.

what about:

__asm__
(
"pushq %%rax\n\t"
"popq %%rax"
:
:
:"rsp"
);

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

* [Bug c/104805] [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list
  2022-03-06 11:43 [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list 570070308 at qq dot com
  2022-03-06 11:46 ` [Bug c/104805] " jakub at gcc dot gnu.org
  2022-03-06 11:51 ` 570070308 at qq dot com
@ 2022-03-06 12:09 ` 570070308 at qq dot com
  2022-03-06 13:03 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: 570070308 at qq dot com @ 2022-03-06 12:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from 。 <570070308 at qq dot com> ---
(In reply to Jakub Jelinek from comment #1)
> Clobber of "rsp" makes no sense, you can't change the value of the stack
> pointer in inline asm without restoring it back before the end of inline asm.

And, this bug is about "rbp" not "rsp". GCC uses "rbp" to represent input
operand even though the "rbp" is in the clobber list.

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

* [Bug c/104805] [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list
  2022-03-06 11:43 [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list 570070308 at qq dot com
                   ` (2 preceding siblings ...)
  2022-03-06 12:09 ` 570070308 at qq dot com
@ 2022-03-06 13:03 ` jakub at gcc dot gnu.org
  2022-03-06 13:22 ` 570070308 at qq dot com
  2022-03-06 19:47 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-03-06 13:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
rbp is hard frame pointer, so depending on whether the function needs a frame
pointer (at -O0 I think all functions do), the register isn't available for use
(and therefore for clobbering) in inline asm.
Only in functions where it isn't needed, it is not fixed then and can be used
for other purposes.

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

* [Bug c/104805] [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list
  2022-03-06 11:43 [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list 570070308 at qq dot com
                   ` (3 preceding siblings ...)
  2022-03-06 13:03 ` jakub at gcc dot gnu.org
@ 2022-03-06 13:22 ` 570070308 at qq dot com
  2022-03-06 19:47 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: 570070308 at qq dot com @ 2022-03-06 13:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from 。 <570070308 at qq dot com> ---
(In reply to Jakub Jelinek from comment #4)
> rbp is hard frame pointer, so depending on whether the function needs a
> frame pointer (at -O0 I think all functions do), the register isn't
> available for use (and therefore for clobbering) in inline asm.
> Only in functions where it isn't needed, it is not fixed then and can be
> used for other purposes.

So you explained why "rbp" cann't be in the clobber list with -O0, and may be
can be in the clobber list with -O1,-O2 or -O3(when the function don't needs a
frame pointer), I understand this now.

But when the "rbp" is in the clobber list successfully, it should not be used
to represent any input/output operands according to the doc because user may
change the %rbp and make the input/output operands wrong.

for example:
```
void kkk()
{
    char a;
    __asm__ volatile (
            "writing %%rbp\n\t"
            // %0 may point to error memory because %rbp change
            // for example -9(%rbp) represent char a
            "reading %0\n\t"
            "writing %0"
            :"+m"(a)
            :
            : "rsp","rbp"
            );
}
```

I have done a lot of experiments, if a register is list in the clobber list, it
will never appear to represent in the input/output operands, and the doc say so
too. Only "rbp" is an exception.

https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers
```
When the compiler selects which registers to use to represent input and output
operands, it does not use any of the clobbered registers. As a result,
clobbered registers are available for any use in the assembler code.
```

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

* [Bug c/104805] [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list
  2022-03-06 11:43 [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list 570070308 at qq dot com
                   ` (4 preceding siblings ...)
  2022-03-06 13:22 ` 570070308 at qq dot com
@ 2022-03-06 19:47 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-03-06 19:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
See
https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Optimize-Options.html#index-fomit-frame-pointer

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

end of thread, other threads:[~2022-03-06 19:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-06 11:43 [Bug c/104805] New: [12.0] x86_64 Extended asm may use rbp register to input/output even thougth "rbp" is in the clobber list when "rsp" and "rbp" are both in the in the clobber list 570070308 at qq dot com
2022-03-06 11:46 ` [Bug c/104805] " jakub at gcc dot gnu.org
2022-03-06 11:51 ` 570070308 at qq dot com
2022-03-06 12:09 ` 570070308 at qq dot com
2022-03-06 13:03 ` jakub at gcc dot gnu.org
2022-03-06 13:22 ` 570070308 at qq dot com
2022-03-06 19:47 ` pinskia at gcc dot gnu.org

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).