[Dlang-internal] Dlang structured exception handling on Windows

Adam D. Ruppe destructionator at gmail.com
Mon Mar 19 19:09:01 UTC 2018


On Monday, 19 March 2018 at 18:39:30 UTC, KytoDragon wrote:
> What exactly causes this behaviour? (e.q. which part of 
> druntime/phobos/dmd/ldc2)

Look in druntime src/rt/dmain2.d for "trapExceptions". It is set 
to true except "IsDebuggerPresent". That catches everything.

You can't reset it to false since it makes a copy of the global 
var before any user code is run! And the command line switch to 
disable it that I'm sure I saw the PR for apparently never got 
in, since I can't find that code in live. So we have to hack 
around it.

> Is there a way to disable that?
> Has anybody an other solution?

Behold:

---
import core.runtime;
import std.stdio;
extern(C) int _d_run_main(int argc, char** argv) {
	Runtime.initialize();
	scope(exit) Runtime.terminate();

	writeln("here");

	int* n = null;
	*n = 1;
	writeln("there");
	return 0;
}

// a dummy to trick the compiler into actually linking in the 
runtime
void main() {}
---


_d_run_main is an internal function that does a bunch of setup 
work. This defines our own to override the one in dmain2 and 
duplicates a bit of its work (though not all, there may be some 
things that are broken with this!)

With this overridden we can get some code running without the 
exception catch wrapper. Works on 32 bit windows at least.


Ideally, we'd just get the commandline/envvar switch to 
disable... and the PR stalled for ages then the author moved on 
https://github.com/dlang/druntime/pull/1538 we just should revive 
that and get it up ASAP. Much better solution than the filthy 
hack above.


More information about the Dlang-internal mailing list