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.
Tested with DMD 32-bit 2.065.
I suspect the correct solution is for the invariant calls to not be implicitly inserted for naked functions.
(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
(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.
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/940e48c1c2481f450fea9eb90a988e435ded84d0 fix Issue 13147 - Wrong codegen for thisptr in naked extern (C++) methods https://github.com/dlang/dmd/commit/b5a59760d06ad1e1b4af9bf41a268650992463ff Add struct test for bug 13147 fix and inline assembly check. https://github.com/dlang/dmd/commit/a0840035a8d92901b7f94961b2be5812bc9c9bfa Merge pull request #5729 from WalterWaldron/fix13147 Fix Issue 13147 - invariant code added to functions with naked inline assembly
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/940e48c1c2481f450fea9eb90a988e435ded84d0 fix Issue 13147 - Wrong codegen for thisptr in naked extern (C++) methods https://github.com/dlang/dmd/commit/b5a59760d06ad1e1b4af9bf41a268650992463ff Add struct test for bug 13147 fix and inline assembly check. https://github.com/dlang/dmd/commit/a0840035a8d92901b7f94961b2be5812bc9c9bfa Merge pull request #5729 from WalterWaldron/fix13147