Skip to content

Report a Haxe exception that escapes every catch before the process ends - #489

Open
dimensionscape wants to merge 1 commit into
mainfrom
fix/report-escaped-haxe-exceptions
Open

dimensionscape wants to merge 1 commit into
mainfrom
fix/report-escaped-haxe-exceptions

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

A Haxe exception that no C++ catch takes ends the process through std::terminate: one thrown in a callback the OS or a library calls, through a noexcept function or a destructor, from the HXCPP_CATCH_SEGV signal handler, or out of the function of a thread Haxe did not start. main's catch never sees it, and the C++ runtime says at most what type was thrown. GCC's prints terminate called after throwing an instance of 'Dynamic', LLVM's libc++abi: terminating due to uncaught exception of type Dynamic, and MSVC's nothing at all. A game built with MinGW crashed this way after an update, and the Haxe error could not be recovered without a debugger.

hx::Boot now installs a terminate handler (HaxeTerminate in src/hx/Debug.cpp). When std::current_exception() holds a Dynamic, it reports it as main's catch does, then calls the handler that was installed before it, and aborts. Anything else goes straight to the previous handler.

Called from hxcpp::__hxcpp_main
Called from Main::main Main.hx
Called from Main::child Main.hx
Called from Main::throwThroughNoexcept Main.hx
Error : thrown through a noexcept function
The exception escaped through native code, ending the process
  • The stack is the frames the exception left and then those it never left: with no catch to reach, it may not have unwound at all (MSVC calls terminate before unwinding, GCC unwinds up to a noexcept frame).
  • A program linked for the Windows subsystem also gets a message box, as HX_WIN_MAIN's catch shows. HX_WIN_MAIN is defined only for the main file, so the runtime reads the subsystem from the exe's header. Android also gets the log, under the Exception tag.
  • The exit code is unchanged, since the previous handler and abort still end the process.

The collector's state:

  • Reporting runs Haxe code, so the handler first calls SetTopOfStack(&top, true), which attaches a thread the collector did not know and leaves a GC-free zone, then copies the exception onto its stack before allocating. On a thread it had to attach, the stack of frames is not the exception's and is not printed.
  • On the thread running a collection, as in a finalizer, nothing can allocate, so it prints a report without reading the exception (new hx::IsCollectingThread).
  • A second thread that terminates while the first reports waits in a GC-free zone for the first to end the process. A terminate from inside the report aborts. Nothing in the handler throws: toString, the _hx_customStack read and the stack dump are each guarded with catch (...).

MSVC's runtime keeps a terminate handler for each thread (checked here: one set on the main thread does not run for an exception escaping a std::thread or a CreateThread thread), so with MSVC each thread also installs it as it attaches to the collector, in StackContext::onThreadAttach. A thread that never attached is not covered there. libstdc++ and libc++ keep one handler for the process.

Not covered by this: with MSVC and HXCPP_CATCH_SEGV (/EHa), a fault translated to a Haxe exception inside a noexcept function is not terminated at all; it reaches main's catch, as before.

Tests:

  • test/regression/EscapedHaxeException runs five cases in child processes and checks each exits with a failure and prints Error : <message>, the escape line, and the frame that threw where it has one: through a noexcept function, a thrown null, out of a std::thread that attached itself, out of one that attached and detached again (the exception kept by a static, its toString allocating), and from native code inside a GC-free zone. Skipped on Emscripten, which has no threads.
  • Ran locally on Windows (MSVC 18, Haxe 4.3.7): the new test passes on x64 and x86 and fails on main, where every case prints nothing. 18 related regression tests (exceptions, threads, GC-free zones, exit) pass through Run.hx, and test/haxe passes (2698 assertions). Also checked by hand with HXCPP_SINGLE_THREADED_APP and HXCPP_CATCH_SEGV.
  • Linux, macOS and MinGW are left to CI.

A Haxe exception that no C++ catch takes, as one thrown in a callback the
OS or a library calls, through a noexcept function or a destructor, from
the HXCPP_CATCH_SEGV handler, or out of a thread Haxe did not start, ends
the process through std::terminate. GCC's runtime then printed only
"terminate called after throwing an instance of 'Dynamic'", LLVM's said
the same in its own words, and MSVC's printed nothing, so the Haxe error
was lost without a debugger.

hx::Boot now installs a terminate handler that reports a Haxe exception as
main's catch does: the frames it left and those it never left, then
"Error : " and the message, a line saying it escaped through native code,
a message box in a program linked for the Windows subsystem, and the
Android log. It then calls the handler that was there before, and aborts.
It reads the exception on a thread the collector knows and outside a
GC-free zone, attaching the thread or leaving the zone first, and not on
the thread running a collection, which cannot allocate. A second thread
that terminates meanwhile waits in a GC-free zone for the first to end the
process, and a terminate from inside the report aborts.

MSVC's runtime keeps a terminate handler for each thread, so there each
thread installs it as it attaches to the collector.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant