[Issue 1844] New: Problems using dynamic cast on x86-64 platform

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Feb 16 13:17:01 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1844

           Summary: Problems using dynamic cast on x86-64 platform
           Product: DGCC aka GDC
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P1
         Component: glue layer
        AssignedTo: dvdfrdmn at users.sf.net
        ReportedBy: svanleent at gmail.com


On the x86-64 platform, downcasting from an interface towards it's original
object, appears to throw up segmentation fault. This is a test which get's it
to go wrong (the version of GDC used was revision 199):

module test_2008_02_13;

import tango.io.Console;

public interface A {
        void foo();
}

public interface B {
        void bar();
}

public interface C : B {}

public interface D : A, C {
        void foobar();
}

public interface E : D {}
public interface F : E {}
public interface G : F {}

public class AB : G {
        void foo() {
                Cout("Foo").newline;
        }

        void bar() {
                Cout("Bar").newline;
        }

        void foobar() {
                Cout("FooBar").newline;
        }
}

/// Upcast Tests
void upcastTests(AB ab) {
        Cout("---Start Upcast Tests---").newline;

        if(cast(A)ab) {
                auto a = cast(A)ab;
                a.foo(); // Should print Foo
        }

        if(cast(B)ab) {
                auto b = cast(B)ab;
                b.bar(); // Should print Bar
        }

        if(cast(C)ab) {
                auto c = cast(C)ab;
                c.bar(); // Should print Bar
        }

        if(cast(D)ab) {
                auto d = cast(D)ab;
                d.foobar(); // Should print FooBar
        }

        if(cast(E)ab) {
                auto e = cast(E)ab;
                e.foobar(); // Should print FooBar
        }

        if(cast(F)ab) {
                auto f = cast(F)ab;
                f.foobar(); // Should print FooBar
        }

        if(cast(G)ab) {
                auto g = cast(G)ab;
                g.foobar(); // Should print FooBar
        }

        Cout("---End Upcast Tests---").newline;
}

/// Downcast Tests
void downcastTests(A a) {

        Cout("---Start Downcast Tests---").newline;

        a.foo(); // Should print Foo

        // Segfaults from here onwards

        if(cast(B)a) {
                auto b = cast(B)a;
                b.bar();
        }

        if(cast(C)a) {
                auto c = cast(C)a;
                c.bar();
        }

        if(cast(D)a) {
                auto d = cast(D)a;
                d.foobar();
        }

        if(cast(E)a) {
                auto e = cast(E)a;
                e.foobar();
        }

        if(cast(F)a) {
                auto f = cast(F)a;
                f.foobar();
        }

        if(cast(G)a) {
                auto g = cast(G)a;
                g.foobar();
        }

        if(cast(AB)a) {
                auto ab = cast(AB)a;
                ab.foobar();
        }

        Cout("---End Downcast Tests---").newline;
}

void main(char[][] args) {
        auto ab = new AB();
        ab.foo(); // prints Foo
        ab.bar(); // prints Bar
        upcastTests(ab);
        downcastTests(ab);

}


-- 



More information about the D.gnu mailing list