www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13331] New: naked asm functions are broken when compiling


          Issue ID: 13331
           Summary: naked asm functions are broken when compiling with
           Product: D
           Version: D2
          Hardware: x86
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: maor weka.io

when defining the following function (and unit test):

private ulong /*RAX*/ exchangeAndAdd(ulong * counter /*RSI*/, ulong addition
        naked                   ;
        mov  RAX, RDI           ;
        lock                    ;
        xadd [RSI], RAX         ;
        ret                     ;

unittest {
  ulong a = 10;
  ulong b = exchangeAndAdd(&a, 2);


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
   0x00000000004330c7 <+7>:    mov    0x37d92(%rip),%rdi        # 0x46ae60
   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
   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)

Aug 19 2014