D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13147 - Wrong codegen for thisptr in naked extern (C++) methods
Summary: Wrong codegen for thisptr in naked extern (C++) methods
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-17 02:55 UTC by Mithun Hunsur
Modified: 2016-10-01 11:46 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mithun Hunsur 2014-07-17 02:55:27 UTC
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 Mithun Hunsur 2014-07-17 04:04:10 UTC
Tested with DMD 32-bit 2.065.
Comment 2 yebblies 2014-07-17 10:54:24 UTC
I suspect the correct solution is for the invariant calls to not be implicitly inserted for naked functions.
Comment 3 Walter Bright 2015-08-23 02:08:19 UTC
(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 safety0ff.bugz 2016-05-03 01:33:46 UTC
(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 github-bugzilla 2016-05-06 23:49:02 UTC
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
Comment 6 github-bugzilla 2016-10-01 11:46:44 UTC
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