Issue 173800
Summary Miscompile: Return value of no_caller_saved_registers function on x86-64 gets destroyed
Labels new issue
Assignees
Reporter Vogtinator
    With a simple function such as this:

```
__attribute__((no_caller_saved_registers)) __attribute__((target("general-regs-only")))
int getPerCPU()
{
	int ret;
	asm volatile("movl $0x1337, %[ret]" : [ret] "=r" (ret));
	return ret;
}
```

The return value is not actually returned, but the return value register has its original value.

```
> clang -c test.c && objdump -d test.o

test.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <getPerCPU>:
   0:   55                      push   %rbp
   1:   48 89 e5 mov    %rsp,%rbp
   4:   50                      push   %rax <--------- %rax saved on stack
   5:   b8 37 13 00 00          mov $0x1337,%eax
   a:   89 45 f4                mov    %eax,-0xc(%rbp)
   d: 8b 45 f4                mov    -0xc(%rbp),%eax
  10:   58 pop    %rax  <--------- previous %rax restored from stack
  11:   5d pop    %rbp
  12:   c3                      ret
```

The `__attribute__((target("general-regs-only")))` part is to make it compile with gcc as well, which emits correct code:

```
> gcc -c test.c && objdump -d test.o

test.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <getPerCPU>:
   0:   55 push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   b8 37 13 00 00          mov    $0x1337,%eax
   9:   89 45 fc                mov %eax,-0x4(%rbp)
   c:   8b 45 fc                mov    -0x4(%rbp),%eax
 f:   5d                      pop    %rbp
  10:   c3 ret
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to