Ubuntu

D programs segfault when vararg functions are used

Reported by Lars Tandle Kyllingstad on 2008-05-30
32
This bug affects 3 people
Affects Status Importance Assigned to Milestone
gdc-4.2 (Ubuntu)
Undecided
Unassigned

Bug Description

Binary package hint: gdc-4.2

Programs that are compiled with the Ubuntu build of GDC give a segmentation fault whenever vararg functions, such as writef and writefln are used. The following hello-world program, for instance, will crash:

import std.stdio;
void main() {
    writefln("Hello %s!", "world");
}

This program compiles without errors, and the segfault happens when running the program. I am running Ubuntu 8.04 with GDC build 0.25-4.2.3-2ubuntu2. This was never an issue for me with the 4.1 build.

This issue has been discussed on the official D forum, and it seems only the Ubuntu version has this problem:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=72162

I forgot to mention: I am using the AMD64 version of Hardy.

Jakob Bornecrantz (wallbraker) wrote :

Have the same thing, also AMD64. Its realy annoying since I can't use any of the vararg functions in the std library.

Knut Karevoll (gnonthgol) wrote :

This bug is Confirmed.
Any vararg functions fails with a segfault on x86-64.
This bug is CRITICAL as it renders GDC almost unusable on x86-64

Changed in gdc-4.2:
status: New → Confirmed

Bug confirmed:

oshawk@earth:/tmp$ cat test.d
import std.stdio;
 void main() {
     writefln("Hello %s!", "world");
 }

goshawk@earth:/tmp$ gdc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,d --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.1.3 --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --disable-libmudflap --enable-checking=release x86_64-linux-gnu
Thread model: posix
gcc version 4.1.3 20070831 (prerelease gdc 0.25, using dmd 1.021) (Ubuntu 0.25-4.1.2-16ubuntu1)
goshawk@earth:/tmp$ gdc test.d
goshawk@earth:/tmp$ ./a.out
Hello world!

goshawk@earth:/tmp$ gdc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,d --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --disable-libmudflap --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.2.3 20080225 (prerelease gdc 0.25 20071215, using dmd 1.022) (Ubuntu 0.25-4.2.3-2ubuntu2)
goshawk@earth:/tmp$ gdc test.d
goshawk@earth:/tmp$ ./a.out
Segmentation fault

Pallinger Péter (pp-publikus) wrote :

I can also confirm this bug.
A quick workaround is to use gcd-4.1.

Geir Hauge (geir-hauge) wrote :

Any program compiled with gdc seem to have memory leaks.

$ echo "void main(){}" > test.d ; gdc test.d
$ valgrind ./a.out
==24118== Memcheck, a memory error detector.
==24118== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==24118== Using LibVEX rev 1804, a library for dynamic binary translation.
==24118== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==24118== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
==24118== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==24118== For more details, rerun with: -v
==24118==
==24118==
==24118== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==24118== malloc/free: in use at exit: 8,248 bytes in 3 blocks.
==24118== malloc/free: 13 allocs, 10 frees, 41,652 bytes allocated.
==24118== For counts of detected errors, rerun with: -v
==24118== searching for pointers to 3 not-freed blocks.
==24118== checked 117,644 bytes.
==24118==
==24118== LEAK SUMMARY:
==24118== definitely lost: 8,200 bytes in 1 blocks.
==24118== possibly lost: 0 bytes in 0 blocks.
==24118== still reachable: 48 bytes in 2 blocks.
==24118== suppressed: 0 bytes in 0 blocks.
==24118== Rerun with --leak-check=full to see details of leaked memory.

I get the same results with gdc-4.1.

Knut Karevoll (gnonthgol) wrote :

I have looked into this bug and reduced it to this:
int main() {
    int x = 0;
    void test() {
        void test2() {
            x = 1; //Segfault because &x = 0x0
        }

        test2();
    }

    test();
    return 0;
}

Somehow gdc-2.4 comepiles the test function to
 movq -24(%rbp), %rax
 movq %rax, -16(%rbp)
 movq %rdi, -24(%rbp)
 movq -24(%rbp), %rax
When it should be (as compiled with gdc-4.1)
 movq %rdi, %rax
 movq %rax, -16(%rbp)

This makes %rax corect for test() but it sends the value -24(%rbp) to test2()

I hope someone with more tecnical insight into gdc can fix this bug
In the meantime you can use gdc-4.1 by default by running $sudo ln -s /usr/bin/gdc-4.1 /usr/bin/gdc

Serge Matveenko (lig) wrote :

Could confirm on Ubuntu 8.10 with gdc-4.2

$ LANG=C gdc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,d --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --disable-libmudflap --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.2.4 20080705 (prerelease gdc 0.25 20080312, using dmd 1.024) (Ubuntu 0.25-4.2.4-3.1)

Yeah, confirmed

goshawk@earth:~$ gdc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,d --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --disable-libmudflap --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.2.4 20080705 (prerelease gdc 0.25 20080312, using dmd 1.024) (Ubuntu 0.25-4.2.4-3.1)
goshawk@earth:~$ nano test.d
goshawk@earth:~$ gdc test.d
goshawk@earth:~$ ./a.out
Segmentation fault
goshawk@earth:~$

Confirmed for PowerPC (32bit)

The 3rd example of the "learning D" webpage with many writef/ln crashes.
Rewriting to import C libs printf is an ugly workaround.

deadalnix (deadalnix) wrote :

Yeah, this bug is 1 year and half old, occurs only on ubuntu, but is kindly repeted in each release !

Great job ! Just like the gb php lib, that is buggy since 7.04 (at least).

I just can't understand what's the goal.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers