Bug 13331 – naked asm functions are broken when compiling with -profile
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-08-19T08:33:00Z
Last change time
2017-08-07T13:16:05Z
Keywords
iasm, wrong-code
Assigned to
nobody
Creator
maor
Comments
Comment #0 by maor — 2014-08-19T08:33:46Z
when defining the following function (and unit test):
private ulong /*RAX*/ exchangeAndAdd(ulong * counter /*RSI*/, ulong addition /*RDI*/)
{
asm
{
naked ;
mov RAX, RDI ;
lock ;
xadd [RSI], RAX ;
ret ;
}
}
unittest {
ulong a = 10;
ulong b = exchangeAndAdd(&a, 2);
assert(a==12);
assert(b==10);
}
void main() {}
running after compiling with `dmd -unittest ./test.d -of/tmp/test` works fine.
However, running after compiling with `dmd -unittest -profile ./test.d -of/tmp/test` crashes with a segmentation fault due to the profiling code added to the naked function, below is the assembly code produced, you can see that rdx,rsi & rdi are used by the injected profiling code without preserving them.
(gdb) disas _D4test14exchangeAndAddFPmmZm
Dump of assembler code for function _D4test14exchangeAndAddFPmmZm:
0x00000000004330c0 <+0>: mov 0x37da1(%rip),%rdx # 0x46ae68 <_TMP55+8>
0x00000000004330c7 <+7>: mov 0x37d92(%rip),%rdi # 0x46ae60 <_TMP55>
0x00000000004330ce <+14>: mov %rdx,%rsi
0x00000000004330d1 <+17>: callq 0x44ef88 <trace_pro>
0x00000000004330d6 <+22>: mov %rdi,%rax
0x00000000004330d9 <+25>: lock xadd %rax,(%rsi)
0x00000000004330de <+30>: retq
0x00000000004330df <+31>: sub $0x8,%rsp
0x00000000004330e3 <+35>: callq 0x4330ee <_D4test14exchangeAndAddFPmmZm+46>
0x00000000004330e8 <+40>: add $0x8,%rsp
0x00000000004330ec <+44>: jmp 0x4330f4
0x00000000004330ee <+46>: callq 0x44f22c <_c_trace_epi>
0x00000000004330f3 <+51>: retq
End of assembler dump.
Using dmd (DMD64 D Compiler v2.065) on linux (3.13.0-33-generic #58-Ubuntu SMP Tue Jul 29 16:45:05 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux)
Comment #1 by cauterite — 2016-08-14T14:38:50Z
minimal test case:
void main() {asm {naked; ret;}}
still broken with DMD 2.071.1 / 32-bit backend