Thread overview
setAssertHandler (druntime) segfaults
Jul 24, 2009
Lutger
Jul 24, 2009
Lutger
Jul 25, 2009
Lutger
July 24, 2009
There is a function setAssertHandler in druntime, but when I try to use it it segfaults. I'm not sure how it should be used, this is a complete example of what I try to do:

import std.stdio;
import core.exception;

void handleAssertion(string file, size_t line, string msg = null)
{
    writefln("assert in %s at line %s", file, line);
};

static this()
{
    setAssertHandler( &handleAssertion  );
}

unittest { assert(false); }

void main() {}


output:
assert in test at line 16
Segmentation fault

This is with dmd 2.031 on linux. Is this a bug, am I doing something wrong?


July 24, 2009
On Fri, Jul 24, 2009 at 4:37 PM, Lutger<lutger.blijdestijn@gmail.com> wrote:
> There is a function setAssertHandler in druntime, but when I try to use it it segfaults. I'm not sure how it should be used, this is a complete example of what I try to do:
>
> import std.stdio;
> import core.exception;
>
> void handleAssertion(string file, size_t line, string msg = null)
> {
>    writefln("assert in %s at line %s", file, line);
> };
>
> static this()
> {
>    setAssertHandler( &handleAssertion  );
> }
>
> unittest { assert(false); }
>
> void main() {}
>
>
> output:
> assert in test at line 16
> Segmentation fault
>
> This is with dmd 2.031 on linux. Is this a bug, am I doing something wrong?

Hm, it might - and I'm just taking a wild guess here - be that std.stdio hasn't yet been initialized when you do the writefln in your assertion handler.  But you really should try using a debugger to get a stacktrace.
July 24, 2009
Jarrett Billingsley wrote:

> On Fri, Jul 24, 2009 at 4:37 PM, Lutger<lutger.blijdestijn@gmail.com> wrote:
>> There is a function setAssertHandler in druntime, but when I try to use it it segfaults. I'm not sure how it should be used, this is a complete example of what I try to do:
>>
>> import std.stdio;
>> import core.exception;
>>
>> void handleAssertion(string file, size_t line, string msg = null)
>> {
>> writefln("assert in %s at line %s", file, line);
>> };
>>
>> static this()
>> {
>> setAssertHandler( &handleAssertion  );
>> }
>>
>> unittest { assert(false); }
>>
>> void main() {}
>>
>>
>> output:
>> assert in test at line 16
>> Segmentation fault
>>
>> This is with dmd 2.031 on linux. Is this a bug, am I doing something wrong?
> 
> Hm, it might - and I'm just taking a wild guess here - be that std.stdio hasn't yet been initialized when you do the writefln in your assertion handler.  But you really should try using a debugger to get a stacktrace.

Ok I tried. Funny thing, with -g enabled it doesn't segfault anymore. Without -g, the trace is not intelligible to me. Segfault also occurs when commenting out writefln.

This is useless I assume but anyway, this is what gdb gives me:

#0  0x0805292e in _TMP257 ()
#1  0x0000000e in ?? ()
#2  0xffffd02c in ?? ()
#3  0x08048f79 in _D4test11__unittest1FZv ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)


July 24, 2009
On Fri, Jul 24, 2009 at 5:46 PM, Lutger<lutger.blijdestijn@gmail.com> wrote:
> Jarrett Billingsley wrote:
>
>> On Fri, Jul 24, 2009 at 4:37 PM, Lutger<lutger.blijdestijn@gmail.com> wrote:
>>> There is a function setAssertHandler in druntime, but when I try to use it it segfaults. I'm not sure how it should be used, this is a complete example of what I try to do:
>>>
>>> import std.stdio;
>>> import core.exception;
>>>
>>> void handleAssertion(string file, size_t line, string msg = null)
>>> {
>>> writefln("assert in %s at line %s", file, line);
>>> };
>>>
>>> static this()
>>> {
>>> setAssertHandler( &handleAssertion  );
>>> }
>>>
>>> unittest { assert(false); }
>>>
>>> void main() {}
>>>
>>>
>>> output:
>>> assert in test at line 16
>>> Segmentation fault
>>>
>>> This is with dmd 2.031 on linux. Is this a bug, am I doing something wrong?
>>
>> Hm, it might - and I'm just taking a wild guess here - be that std.stdio hasn't yet been initialized when you do the writefln in your assertion handler.  But you really should try using a debugger to get a stacktrace.
>
> Ok I tried. Funny thing, with -g enabled it doesn't segfault anymore. Without -g, the trace is not intelligible to me. Segfault also occurs when commenting out writefln.
>
> This is useless I assume but anyway, this is what gdb gives me:
>
> #0  0x0805292e in _TMP257 ()
> #1  0x0000000e in ?? ()
> #2  0xffffd02c in ?? ()
> #3  0x08048f79 in _D4test11__unittest1FZv ()
> Backtrace stopped: previous frame inner to this frame (corrupt stack?)

I'm.. utterly at a loss.  It seems that exiting the assertion handler by doing anything other than throwing an exception causes the segfault.  And like you said, -g makes the problem disappear.  I have no idea what's going on.
July 25, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3208