[phobos] Silent failure of std.container unittests

Walter Bright walter at digitalmars.com
Tue Jul 13 21:36:20 PDT 2010



Andrei Alexandrescu wrote:
>
> I'm not sure where the discussion leaves us, but at the end of the day 
> we're in the following situation:
>
> 1. I just svn up'ed a minute ago and built dmd. It shows version 2.048.
>
> 2. This program:
>
> unittest
> {
>     int x = printf("inside unittest\n");
>     assert(x == 100000);
> }
>
> void main(string args[])
> {
>     writeln("inside main");
> }
>
> prints this:
>
> inside unittest
> test(7): unittest failure
> inside main
>
> and exits with error code zero.
>
> Please advise.
>

I just tried it (on Windows and Ubuntu) and it does not print "inside 
main". Also, it needs an "import std.stdio;"

I suggest verifying that druntime/src/core/runtime.d contains the 
following code:

extern (C) __gshared unittest_errors = false;

extern (C) bool runModuleUnitTests()
{
    static if( __traits( compiles, backtrace ) )
    {
        static extern (C) void unittestSegvHandler( int signum, 
siginfo_t* info, void* ptr )
        {
            static enum MAXFRAMES = 128;
            void*[MAXFRAMES]  callstack;
            int               numframes;

            numframes = backtrace( callstack, MAXFRAMES );
            backtrace_symbols_fd( callstack, numframes, 2 );
        }

        sigaction_t action = void;
        sigaction_t oldseg = void;
        sigaction_t oldbus = void;

        (cast(byte*) &action)[0 .. action.sizeof] = 0;
        sigfillset( &action.sa_mask ); // block other signals
        action.sa_flags = SA_SIGINFO | SA_RESETHAND;
        action.sa_sigaction = &unittestSegvHandler;
        sigaction( SIGSEGV, &action, &oldseg );
        sigaction( SIGBUS, &action, &oldbus );
        scope( exit )
        {
            sigaction( SIGSEGV, &oldseg, null );
            sigaction( SIGBUS, &oldbus, null );
        }
    }

    if( Runtime.sm_moduleUnitTester is null )
    {
        foreach( m; ModuleInfo )
        {
            if( m )
            {
                auto fp = m.unitTest;

                if( fp )
                    fp();
            }
        }
        return !unittest_errors;
    }
    return Runtime.sm_moduleUnitTester();
}

and druntime/src/core/exception.d contains the following code:

extern (C) extern __gshared bool unittest_errors;

extern (C) void onUnittestErrorMsg( string file, size_t line, string msg )
{
    static char[] intToString( char[] buf, uint val )
    {
        assert( buf.length > 9 );
        auto p = buf.ptr + buf.length;

        do
        {
            *--p = cast(char)(val % 10 + '0');
        } while( val /= 10 );

        return buf[p - buf.ptr .. $];
    }

    static struct Console
    {
        Console opCall( in char[] val )
        {
            version( Windows )
            {
                uint count = void;
                WriteFile( GetStdHandle( 0xfffffff5 ), val.ptr, 
val.length, &count, null );
            }
            else version( Posix )
            {
                write( 2, val.ptr, val.length );
            }
            return this;
        }


        Console opCall( uint val )
        {
            char[10] tmp = void;
            return opCall( intToString( tmp, val ) );
        }
    }

    static __gshared Console console;

    unittest_errors = true;
    console( file )( "(" )( line )( "): " )( msg )( "\n" );
}

If they do, then make sure you really did delete the druntime lib and 
rebuild it and you're not linking with an old one.



More information about the phobos mailing list