Bug 13147 – Wrong codegen for thisptr in naked extern (C++) methods
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2014-07-17T02:55:00Z
Last change time
2016-10-01T11:46:44Z
Assigned to
nobody
Creator
me
Comments
Comment #0 by me — 2014-07-17T02:55:27Z
Naked extern (C++) methods are missing "mov [EBP-4], ECX", resulting in a failed invariant assertion ('null this'). This appears to be because the instruction to load [EBP-4] with ECX is treated as part of the stack frame. While it is expected that a stack frame is set up by the user in a naked function, the invariant is tested before my inline assembly preventing me from doing so.
This means that anything to do with 'this' in a naked extern (C++) fails to work, as D looks for 'this' in the untouched [EBP-4]. A test case is below:
class Test
{
extern (C++) Test test()
{
asm { naked; }
return this;
}
}
When the above function is disassembled, the mov [EBP-4], ECX instruction will be missing. When run, it will assert with "null this".
A related issue appears to be https://issues.dlang.org/show_bug.cgi?id=2350.
Comment #1 by me — 2014-07-17T04:04:10Z
Tested with DMD 32-bit 2.065.
Comment #2 by yebblies — 2014-07-17T10:54:24Z
I suspect the correct solution is for the invariant calls to not be implicitly inserted for naked functions.
Comment #3 by bugzilla — 2015-08-23T02:08:19Z
(In reply to yebblies from comment #2)
> I suspect the correct solution is for the invariant calls to not be
> implicitly inserted for naked functions.
And you'd be right.
https://github.com/D-Programming-Language/dmd/pull/4921
Comment #4 by safety0ff.bugz — 2016-05-03T01:33:46Z
(In reply to Walter Bright from comment #3)
>
> And you'd be right.
>
> https://github.com/D-Programming-Language/dmd/pull/4921
Does this fix the struct case as well?
struct S { void foo() { asm { naked; ret; } } }
I just hit this bug.
Comment #5 by github-bugzilla — 2016-05-06T23:49:02Z