Skip to content

Commit

Permalink
Fix crash with runtime enabled on Windows, this one was due to Except…
Browse files Browse the repository at this point in the history
…ion traversing runtime boundaries.

`runtimeSection` now only support `nothrow` callees.
  • Loading branch information
Guillaume Piolat committed Sep 18, 2018
1 parent 9c019cc commit 41129bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
21 changes: 5 additions & 16 deletions core/dplug/core/runtime.d
Expand Up @@ -67,25 +67,14 @@ auto runtimeSection(F)(F functionOrDelegateThatCanBeGC) nothrow @nogc if (isCall
auto myGCDelegate = toDelegate(functionOrDelegateThatCanBeGC);
alias T = typeof(myGCDelegate);

static ReturnType!T internalFunc(T fun, Parameters!T params)
static ReturnType!T internalFunc(T fun, Parameters!T params) nothrow
{
import core.stdc.stdio;
ScopedRuntimeSection section;
section.enter();

try
{
section.enter();
}
catch(Exception e)
{
// runtime initialization failed
// this should never happen
assert(false);
}

// the nice thing here is that `nothrow` is inferred so GC created Exception
// will traverse the boundaries, albeit collected...
// TODO if fun can throw, convert the exception to manually handled Exception and strings
// Important: we only support `nothrow` runtime section.
// Supporting exception was creating spurious bugs, probably the excpeption being collected.
return fun(params);
}

Expand All @@ -96,7 +85,7 @@ auto runtimeSection(F)(F functionOrDelegateThatCanBeGC) nothrow @nogc if (isCall
typeof(myGCDelegate.ptr) ptr;
typeof(myGCDelegate.funcptr) funcptr;

ReturnType!T opCall(Parameters!T params) @nogc
ReturnType!T opCall(Parameters!T params) nothrow @nogc
{
T dg;
dg.funcptr = funcptr;
Expand Down
23 changes: 5 additions & 18 deletions tests/using-runtime/main.d
Expand Up @@ -102,19 +102,9 @@ nothrow:
assert(result == 1984);

// You can call stand-alone functions or methods
try
{
size_t len = runtimeSection(&myCarelessFunction)(false);
assert(len == 4000);
len = runtimeSection(&myCarelessFunction)(true);
}
catch(Exception e)
{
// for some reason the Exception allocated in the runtime section hasn't been
// collected, not sure why
// Anyway it's UB to read it and I'm not sure you can really catch it in the first place.
//printf("%s\n", e.msg.ptr);
}
// but they have to be `nothrow`.
size_t len = runtimeSection(&myCarelessFunction)();
assert(len == 4000);
}

override void processAudio(const(float*)[] inputs, float*[]outputs, int frames, TimeInfo info)
Expand Down Expand Up @@ -160,12 +150,9 @@ static void cleanupObject(ref HeapObject obj) nothrow
}
}

size_t myCarelessFunction(bool doThrow)
size_t myCarelessFunction() nothrow
{
auto A = new ubyte[4000];
if (doThrow)
throw new Exception("an error!");
else
return A.length;
return A.length;
}

0 comments on commit 41129bf

Please sign in to comment.