Skip to content

Linux: an untyped catch never catches a thrown number (aborts in std::terminate) #345

Closed

Description

@ASDAlexander77

On Linux, an untyped catch (e) (and catch (e: any)) never catches a thrown number. The program aborts instead:

function main() { try { throw 42; } catch (e) { print("caught"); } print("done."); }
terminate called after throwing an instance of 'int'
Aborted (core dumped)

Throwing a string or a class instance is caught correctly.

Cause

An untyped catch resolves its variable to AnyType, and MLIRRTTIHelperVCLinux::setType() maps that to _ZTIPv, the type descriptor for void*. Under the Itanium ABI a pointer-typed catch clause only matches pointer-typed exceptions, which is why strings and classes work. A thrown int or double matches nothing, the search walks past main, and std::terminate runs.

In C++ terms an untyped catch (e) is catch (...), which is a landing pad with a catch-all clause — a null typeinfo operand — not a void* clause.

The same bug existed on Windows and is fixed in #344, but that fix is specific to the Win32 RTTI tables and does not carry over.

Why it is still open

A catch-all clause alone is not enough. Making AnyType emit catch ptr null (attempted in ff01e3a on that PR, since reverted) did make a thrown number match, but it broke reading the caught value: 00try_catch.ts main4 does

try { throw <any>123; } catch (v: any) { t = <int>v; }
assert(t == 123);

and that failed on Linux in all six of its registrations, while Windows stayed green.

So matching and value binding have to be solved together: the catch-all needs the caught value bound from __cxa_begin_catch's result rather than from the clause's type. That needs a Linux machine to iterate on.

Current state

00throw_number_untyped_catch.ts is registered on Windows only, including its rc/none corpus variants, with a comment pointing here. It should be made cross-platform once this is fixed.

Reproduction, for whoever picks it up

Any Linux build:

tslang --emit=jit --no-default-lib -mm=rc tslang/test/tester/tests/00throw_number_untyped_catch.ts

Useful without a Linux machine: --emit=llvm -mtriple=x86_64-unknown-linux-gnu cross-emits the Linux IR on any host, which is enough to see the landing pad clauses (catch ptr @_ZTIPv today) but not to run them.

Relevant code: tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h (setType, and the separate overload setRTTIForType uses — the rethrow idiom catch (e) { throw e; } depends on the latter still declaring _ZTIPv), tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCLinux.h, and TryOpLowering in tslang/lib/TypeScript/LowerToLLVM.cpp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions