Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extern(C++) cast from interface to child class returns wrong pointer value #19486

Open
dlangBugzillaToGithub opened this issue Sep 26, 2018 · 1 comment
Labels
Arch:x86_64 Issues specific to x86_64 Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code OS:Linux P2 Severity:critical

Comments

@dlangBugzillaToGithub
Copy link

Atila Neves (@atilaneves) reported this on 2018-09-26T11:50:29Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=19267

CC List

  • Suleyman Sahmi (سليمان السهمي)

Description

The code at the end with extern(C++) prints:

fooImpl @ 0x7fa6503c1000
foo     @ 0x7fa6503c1008

foo     @ 0x7fa6503c1008
fooImpl @ 0x7fa6503c1008


If the interface and class are extern(D) it (correctly) prints:

fooImpl @ 0x7fa6503c1000
foo     @ 0x7fa6503c1008

foo     @ 0x7fa6503c1008
fooImpl @ 0x7fa6503c1000


Casting from the class to the interface works in both cases and adds 8 bytes to the pointer. Casting back from interface to the class only works for extern(D) and fails for extern(C++): there's no way to get the original pointer back except for manually subtracting 8 bytes.

Code to reproduce:



import core.stdc.stdio: printf;

extern(C++) interface Foo {
    int foo();
}

extern(C++) class FooImpl: Foo {
    override int foo() { return 42; }
}

void main() {
    auto fooImpl = new FooImpl;
    printf("fooImpl @ %p
", fooImpl);

    Foo foo = fooImpl;
    printf("foo     @ %p
", foo);

    printf("
");
    takingFoo(foo);
}


void takingFoo(Foo foo) {
    printf("foo     @ %p
", foo);
    FooImpl fooImpl = cast(FooImpl) foo;  // oops
    printf("fooImpl @ %p
", fooImpl);

}
@dlangBugzillaToGithub
Copy link
Author

sahmi.soulaimane commented on 2019-05-22T06:08:15Z

This would require dynamic casting which means druntime must know about cpp rtti.

@thewilsonator thewilsonator added the Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code label Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arch:x86_64 Issues specific to x86_64 Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code OS:Linux P2 Severity:critical
Projects
None yet
Development

No branches or pull requests

2 participants