www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16400] New: naked variadic C function emits broken prologue

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

          Issue ID: 16400
           Summary: naked variadic C function emits broken prologue
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: cauterite gmail.com

extern(C) void f(int, ...) {
    asm {naked; ret;};
};

void main() {
    assert(*(cast(ubyte*) &f) == 0xc3); // fails
    f(0); // corrupts the stack
};

--------------------------

The generated instructions in `f` are:
    lea     eax, [ebp+0C];
    mov     [ebp-4], eax;
    ret;
which is obviously wrong, since it's supposed to be a naked function, and it
has no business reading the previous stack frame's EBP.

Note that if you give `f` fixed arity `extern(C) void f(int)` there is no
problem.

--
Aug 17 2016