Skip to content

ex_stack: report an unhandled exception instead of terminating in libc++abi - #1302

Merged
hosking merged 2 commits into
masterfrom
ex-stack-unhandled
Nov 22, 2026
Merged

hosking merged 2 commits into
masterfrom
ex-stack-unhandled

Conversation

@hosking

@hosking hosking commented Nov 22, 2026

Copy link
Copy Markdown
Member

Under ex_stack, a Modula-3 exception that no handler catches reaches std::terminate. The program then dies with

libc++abi: terminating due to uncaught exception of type _M3Exc

instead of the usual runtime-error report:

***
*** runtime error:
***    Unhandled exception: Main.a
***    file "../Main.m3", line 13
***

m3tests r001, r003, r004 and r005 fail on master for this reason on ARM64_DARWIN, and so probably on every ex_stack target. This has likely been the case since ex_stack moved to C++ EH in May, but I haven't bisected it.

Changes

  • ex_stack/RTExStackC.c installs a std::terminate handler. It recovers the escaping _M3Exc by re-throwing it, then passes its activation to RTException.ReportUnhandled. Anything else falls through to abort() as before.
  • RTException.ReportUnhandled (new) records the exception as un_except/un_arg, as DefaultBackstop does. It reports through Crash and does not re-raise, since a throw from inside terminate would recurse.
  • RTExStack.DumpStack prints the libunwind stack dump only under @M3stackdump or @M3debugex, as under ex_frame. Before, it printed one on every crash, and the reference outputs don't include it.

This is ported from the msir branch (f096d61), where it has been in use since July. There the handler is in each target's RTStackC.c. Since #1271 it belongs in the target-independent RTExStackC.c, so it appears once.

Testing (ARM64_DARWIN, C backend). Two concierge.py upgrade passes, the second self-hosted, then m3tests:

  • r001, r003, r004 and r005 now pass.
  • 12 errors in total, down from 16. The remaining failures are the same set as before: p047 p109 p126 p137 p201 p227 p268 p269 p270 p277 p280.

🤖 Generated with Claude Code

…c++abi

Under ex_stack a Modula-3 exception that no handler catches reaches
std::terminate, which prints "libc++abi: terminating due to uncaught
exception of type _M3Exc" and aborts, without the runtime-error report
("Unhandled exception: Main.a", file and line).  m3tests r001, r003,
r004 and r005 failed for it on ARM64_DARWIN.

- RTExStackC.c installs a std::terminate handler that recovers the
  escaping _M3Exc by re-throwing it and hands its activation to the new
  RTException.ReportUnhandled.
- ReportUnhandled records the exception as un_except/un_arg, as
  DefaultBackstop does, and reports it through Crash without re-raising
  (a throw from inside terminate would recurse).
- RTExStack.DumpStack prints the libunwind stack dump only under
  @M3stackdump or @M3debugex, as under ex_frame; it had printed it on
  every crash, which the reference outputs don't include.

Ported from the msir branch (f096d61), where the handler is in each
target's RTStackC.c; here it is in the target-independent RTExStackC.c.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
struct _M3TerminateInstaller {
_M3TerminateInstaller() { std::set_terminate(_m3_terminate_handler); }
};
_M3TerminateInstaller _m3_terminate_installer;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just be aware, that..hand waving vagueness, Visual C++ LTCG link time codegen somehow sometimes breaks static initializers.
I'm afraid I do not have details.
I just don't know when it breaks them.
I know some code bases avoid LTCG for this reason.
And other code bases move the static initializers to files without LTCG.
Specifically the two code bases are msvcrt.dll (not LTCG) and ucrtbase.dll (static initializers in separate files without LTCG).
Modula-3, you know, would have you put this in a module initializer, which it hooks up itself, via the entry point that Modula-3 perhaps demands control of.
I am surprised you did't use that mechanism, though I realize that would just then "bridge" over to C++.

A C++ static initializer is not dependable everywhere (e.g. under Visual
C++ link-time code generation), so RTExStackC.c now exports
RTStack__InstallTerminateHandler, and RTExStack's module body calls it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hosking

hosking commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Good point, thanks. A C++ static initializer was the convenient way to install the handler, but there's no need to depend on it. I've changed it to the Modula-3 way you suggest: RTExStackC.c now just exports RTStack__InstallTerminateHandler(), and RTExStack's module body calls it during initialization. That works the same on every toolchain, LTCG or not. The only window it leaves is an exception escaping before RTExStack has initialized, which would get the C++ runtime's plain terminate. That's early enough in m3core's startup that I'm happy with it. Pushed; m3tests is unchanged, and an unhandled exception still gets the usual report.

hosking pushed a commit that referenced this pull request Nov 22, 2026
…, LOOPHOLE fix

Brings in #1271 (ex_stack target-independent), #1294/#1299 (unused base
pointer), #1295, #1296 (_m3_caught local), #1297, #1298, the RTStackC
include fix and #1300 (LOOPHOLE between a float and a packed integer, with
m3test p308).

Conflicts:
- RTStackC.c (ARM64_DARWIN, AMD64_DARWIN, AMD64_LINUX): take master's;
  RTStack__ThrowM3Exc now lives in ex_stack/RTExStackC.c (#1271).  msir's
  terminate backstop moves there too, as in #1302 (text identical, so the
  merge of #1302 will be clean).
- RTExStack.m3: master's (no Has_walker assertion, #1271) plus msir's
  @M3stackdump switch.
- LoopholeExpr.m3: both import lists.
- m3tests m3makefile: msir's p300 to p304 and master's p305 to p308.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jaykrell

Copy link
Copy Markdown
Contributor

I think it was probably OK.
I mean, I really dislike my level of understanding here.
I've heard there are problems here.
I've seen a small amount of code workaround it, as I indicated, without fully explaining the situation.
It makes some very little bit of sense -- LTCG puts the compiler backend in the uncomfortable position of having to act like the linker in some ways, and it does a very good job of it, but occasionally you find small gaps.

There's other wierdness in the linking model through time.
Historically you got all of an obj's .data or none of it.
All of an obj's .rdata or none of it.
But now we have the compiler -Gw switch, like -Gy, that splits up the data and allows granular linking.
This also have affects in this area presumably.

or TRY/FINALLY handler. Only meaningful when Has_walker () = TRUE and the
M3C backend generates C++ try/catch for exception handling. *)

<*EXTERNAL "RTStack__InstallTerminateHandler" *>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once or twice in the past, without fully understanding the situation, I have had problems where I named the C function exactly like Modula-3, in this form.

For that reason I have taken to using a single underscore in C.

Again, like the other comment, this is not a fully confident statement, but suspicions and incomplete memories.

Maybe we should just follow your way and see what happens. :)

@hosking

hosking commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Thanks Jay. Moving the install into RTExStack's module body leaves the static-initializer question moot, LTCG or not. On the double underscore: RTStack__InstallTerminateHandler follows the naming that RTStack's other external functions already use (RTStack__ThrowM3Exc, RTStack__CurFrame and so on), and those link fine on every target CI builds. If you can recall a specific case where Module__Proc names caused trouble, I'd like to know; otherwise I'll keep the existing convention. CI is green on every job, so I plan to merge this.

🤖 Generated with Claude Code

@hosking
hosking merged commit a7faf44 into master Nov 22, 2026
11 checks passed
hosking pushed a commit that referenced this pull request Nov 23, 2026
Brings in #1302 (report an unhandled ex_stack exception), the RTStack
and RTMachine consolidation (#1312-#1319), #1307 (Copy_n's sub-byte
mask), #1309, #1323/#1324, #1331, #1332, #1334 and #1335.

Conflicts, all in favour of master's versions:
- RTException, RTExStack, RTExStackC.c: #1302 is the upstreamed form of
  msir's unhandled-exception report; master installs the terminate
  handler by an explicit call from RTExStack's body rather than a C++
  static initializer.
- runtime/common/m3makefile: RTStackCommon.c moved to separate
  Has_walker files (#1316).
- m3tests: keep master's p309 alongside msir's p303 and p304.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants