This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 420
Fix exceptions and dynamic casts across dll boundaries #2828
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c4aa69
test dynamic cast of class accross dll boundary
John-Colvin 8ad8855
fix dynamic casts of classes passed across dll boundaries
John-Colvin a03e651
fix catching derived exceptions across dll boundaries
John-Colvin afdd25d
check for null
John-Colvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class C : Exception | ||
{ | ||
this() { super(""); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
version (DLL) | ||
{ | ||
version (Windows) | ||
{ | ||
import core.sys.windows.dll; | ||
mixin SimpleDllMain; | ||
} | ||
|
||
pragma(mangle, "foo") | ||
export Object foo(Object o) | ||
{ | ||
import classdef : C; | ||
|
||
assert(cast(C) o); | ||
return new C; | ||
} | ||
|
||
pragma(mangle, "bar") | ||
export void bar(void function() f) | ||
{ | ||
import core.stdc.stdio : fopen, fclose; | ||
import classdef : C; | ||
bool caught; | ||
try | ||
f(); | ||
catch (C e) | ||
caught = true; | ||
assert(caught); | ||
|
||
// verify we've actually got to the end, because for some reason we can | ||
// end up exiting with code 0 when throwing an exception | ||
fclose(fopen("dynamiccast_endbar", "w")); | ||
throw new C; | ||
} | ||
} | ||
else | ||
{ | ||
T getFunc(T)(const(char)* sym, string thisExePath) | ||
{ | ||
import core.runtime : Runtime; | ||
|
||
version (Windows) | ||
{ | ||
import core.sys.windows.winbase : GetProcAddress; | ||
return cast(T) Runtime.loadLibrary("dynamiccast.dll") | ||
.GetProcAddress(sym); | ||
} | ||
else version (Posix) | ||
{ | ||
import core.sys.posix.dlfcn : dlsym; | ||
import core.stdc.string : strrchr; | ||
|
||
auto name = thisExePath ~ '\0'; | ||
const pathlen = strrchr(name.ptr, '/') - name.ptr + 1; | ||
name = name[0 .. pathlen] ~ "dynamiccast.so"; | ||
return cast(T) Runtime.loadLibrary(name) | ||
.dlsym(sym); | ||
} | ||
else static assert(0); | ||
} | ||
|
||
void main(string[] args) | ||
{ | ||
import classdef : C; | ||
import core.stdc.stdio : fopen, fclose, remove; | ||
|
||
remove("dynamiccast_endmain"); | ||
remove("dynamiccast_endbar"); | ||
|
||
C c = new C; | ||
|
||
auto o = getFunc!(Object function(Object))("foo", args[0])(c); | ||
assert(cast(C) o); | ||
|
||
bool caught; | ||
try | ||
getFunc!(void function(void function()))("bar", args[0])( | ||
{ throw new C; }); | ||
catch (C e) | ||
caught = true; | ||
assert(caught); | ||
|
||
// verify we've actually got to the end, because for some reason we can | ||
// end up exiting with code 0 when throwing an exception | ||
fclose(fopen("dynamiccast_endmain", "w")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be sensible to restrict the name check to
version (Shared)
, i.e., a shared druntime (.{so,dll}
). I think it's reasonable to expect a process with multiple D binaries to share a single druntime; that's not an option with DMD on Windows, I know (so could be enabled forversion (DigitalMars) version (Windows)
in general).It just wouldn't slow down dynamic casting when sticking with a static druntime.