BetterC + Windows + setjmp longjmp

SrMordred patric.dexheimer at gmail.com
Tue Sep 18 19:45:18 UTC 2018


On Tuesday, 18 September 2018 at 03:09:18 UTC, Mike Parker wrote:
> On Tuesday, 18 September 2018 at 00:24:23 UTC, SrMordred wrote:
>
>> Yes, i'm using signal(SIGSEGV, sigfn_t func), it catches 
>> correctly, but end the execution after.
>>
>> I find the alternatives of setjmp/longjmp and sigaction, but 
>> none are avaliable on windows afaik.
>
> setjmp/longjmp are available on windows (image loaders using 
> libpng, lua, quake3, and lots of old C code make use of them):
>
> https://msdn.microsoft.com/en-us/library/xe7acxfb.aspx?f=255&MSPPError=-2147217396
> https://msdn.microsoft.com/en-us/library/3ye15wsy.aspx
>
> You can declare the functions in your own code and they should 
> link just fine. It would be helpful if you also submit a PR to 
> add them to DRuntime for Windows.
>
> As for sigaction, a quick search suggests it isn't available 
> (and it doesn't show up in MSDN):
>
> https://stackoverflow.com/questions/32389905/sigaction-and-porting-linux-code-to-windows

I think this may be going beyond my knowledge ;/

(Trying to follow setjmp.h, don´t have experience doing this)

enum _SIGSET_NWORDS = (1024 / (8 * (ulong.sizeof)));
struct __sigset_t
{
     ulong[_SIGSET_NWORDS] __val;
}

struct __jmp_buf_tag
{
     long[8] __jmpbuf;
     int __mask_was_saved;
     __sigset_t __saved_mask;
};

alias __jmp_buf_tag[1] jmp_buf;

extern (C) @nogc nothrow  int setjmp(ref jmp_buf) ;
extern (C) @nogc nothrow void longjmp(ref jmp_buf, int);

OK, things linked fine.
I´m trying to escape a sigfault with signal + set/longjmp:

jmp_buf buf;
extern(C) nothrow @nogc void h1(int code)  { longjmp(buf, 1); }

//main
signal(SIGSEGV, &h1);
auto x = setjmp(buf);
if(x == 1) ... (go elsewhere)
else
//forcing some sigfault

There are no compilation errors. But my program still just 
crashes after sigfault.

So maybe setjmp/longjmp are not working properly.
Or is not possible to escape a crash.
Or i completely misunderstood everything :P



More information about the Digitalmars-d-learn mailing list