www.digitalmars.com         C & C++   DMDScript  

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

https://issues.dlang.org/show_bug.cgi?id=13331

          Issue ID: 13331
           Summary: naked asm functions are broken when compiling with
                    -profile
           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
/*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:

<_TMP55+8>

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


Tue Jul 29 16:45:05 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux)

--
Aug 19 2014