diff --git a/runtime/macos_host/build.sh b/runtime/macos_host/build.sh index 53b98eb090..e0a9924dd2 100755 --- a/runtime/macos_host/build.sh +++ b/runtime/macos_host/build.sh @@ -35,6 +35,10 @@ compile_guest /MD /c "$FIX/tiny_game.cpp" /Fo"$OUT/tiny_game.obj" "$LLD/lld-link" /nologo /winsysroot:"$XWIN_DIR" "$OUT/tiny_game.obj" "$OUT/xgameruntime.lib" /out:"$OUT/tiny_game.exe" compile_guest /MD /c guest/eh_probe.cpp /Fo"$OUT/eh_probe.obj" "$LLD/lld-link" /nologo /winsysroot:"$XWIN_DIR" "$OUT/eh_probe.obj" /out:"$OUT/eh_probe.exe" +compile_guest /MD /c guest/eh_dll.cpp /Fo"$OUT/eh_dll.obj" +"$LLD/lld-link" /nologo /dll /winsysroot:"$XWIN_DIR" "$OUT/eh_dll.obj" /out:"$OUT/eh_dll.dll" +compile_guest /MD /c guest/eh_module_probe.cpp /Fo"$OUT/eh_module_probe.obj" # run it: exit code 0 +"$LLD/lld-link" /nologo /winsysroot:"$XWIN_DIR" "$OUT/eh_module_probe.obj" /out:"$OUT/eh_module_probe.exe" compile_guest /MT /c guest/eh_probe.cpp /Fo"$OUT/eh_probe_mt.obj" # static CRT: throws go through RaiseException "$LLD/lld-link" /nologo /winsysroot:"$XWIN_DIR" "$OUT/eh_probe_mt.obj" /out:"$OUT/eh_probe_mt.exe" printf 'LIBRARY vulkan-1.dll\nEXPORTS\n vkGetInstanceProcAddr\n' > "$OUT/vulkan-1.def" diff --git a/runtime/macos_host/guest/eh_dll.cpp b/runtime/macos_host/guest/eh_dll.cpp new file mode 100644 index 0000000000..363ab3cd85 --- /dev/null +++ b/runtime/macos_host/guest/eh_dll.cpp @@ -0,0 +1,27 @@ +// The DLL eh_module_probe loads: C++ exceptions thrown in a module other than the host's main image, as a game +// mapped by gta6rt's loader throws them. +#include + +static int g_dtors = 0; +struct Guard { + ~Guard() { ++g_dtors; } +}; +__declspec(noinline) static void deep(int n) { + Guard g; + if (n == 0) throw std::runtime_error("dll"); + deep(n - 1); +} + +extern "C" __declspec(dllexport) int catch_inside() { // thrown and caught in this DLL: 0 + try { + deep(2); + } catch (const std::exception& e) { + return g_dtors == 3 && e.what()[0] == 'd' ? 0 : 1; + } + return 2; +} +extern "C" __declspec(dllexport) void throw_out(int v) { // caught by the caller, in another module + Guard g; + throw v; +} +extern "C" __declspec(dllexport) int dtors() { return g_dtors; } diff --git a/runtime/macos_host/guest/eh_module_probe.cpp b/runtime/macos_host/guest/eh_module_probe.cpp new file mode 100644 index 0000000000..43142f6dbc --- /dev/null +++ b/runtime/macos_host/guest/eh_module_probe.cpp @@ -0,0 +1,20 @@ +// C++ exceptions in a DLL the guest loads (eh_dll.dll): thrown and caught there, and thrown there and caught here. +// Exit code 0, or the number of the first check that failed. +#include + +int main() { + HMODULE dll = LoadLibraryA("eh_dll.dll"); + if (!dll) return 1; + auto catch_inside = reinterpret_cast(GetProcAddress(dll, "catch_inside")); + auto throw_out = reinterpret_cast(GetProcAddress(dll, "throw_out")); // C++ type: may throw + auto dtors = reinterpret_cast(GetProcAddress(dll, "dtors")); + if (catch_inside() != 0) return 2; + int got = 0; + try { + throw_out(9); + } catch (int v) { + got = v; + } + if (got != 9 || dtors() != 4) return 3; + return 0; +}