Skip to content

Native C++ library fails to catch std::exception-derived types when called from .NET 10.0 on macOS Intel #122445

Closed

Description

@jgiannuzzi

Description

There is an exception-handling issue on macOS Intel when running a native C++ library that is invoked from .NET 10.0.

The failure happens entirely inside the native library, before any exception reaches managed code.
When the C++ code attempts to catch some exceptions derived from std::exception (e.g. std::out_of_range or std::length_error) using a typed catch, the handler is incorrectly bypassed, and the exception is caught only by a catch(...) block.

Example:

extern "C" {
int ThrowOutOfRangeCatchException() {
  try {
    throw std::out_of_range("Test exception");
  } catch (const std::exception &) {
    return 0;
  } catch (...) {
    return 1;
  }
}

The same native library behaves correctly when:

  • run directly from a native C++ test executable
  • invoked from .NET 9.0 on macOS Intel
  • when run on Linux or macOS Apple Silicon

This makes the issue specific to .NET 10.0 and macOS Intel (aka x64).

Reproduction Steps

A minimal reproducer exists at https://github.com/jgiannuzzi/dotnet-macos-intel-exception-handling-issue-reproducer with more information. A CI workflow has been setup to observe the issue and a script is provided to reproduce it locally (requires macOS on any architectures, as the issue can be reproduced on Apple Silicon via Rosetta 2).

Expected behavior

The native library should correctly catch exceptions using:

catch (const std::exception &)

regardless of whether it is invoked:

  • from native code
  • from .NET 9.0
  • from .NET 10.0
  • or from any other platform/runtime

Typed C++ catch blocks should not behave differently based on whether the caller is managed or native.

Actual behavior

When the native function is invoked specifically from .NET 10.0 on macOS Intel:

  • the catch (const std::exception &) block is never entered
  • the exception falls through to catch(...)
  • the function returns a status value indicating unexpected exception handling behavior
  • consequently, the managed test observes an incorrect status code – but the exception never reaches .NET, because the failure occurs entirely inside the native C++ function

No such behavior occurs on macOS ARM, Linux, or earlier .NET runtimes on the same machine.

Regression?

Yes. This issue was originally discovered in ParquetSharp when .NET 10.0.0 was released and started to be used to run our tests. The same piece of code has been working since at least .NET Framework 4.6 and .NET Core 3.0.

Known Workarounds

In the native C++ library, explicitly catching exceptions derived from std::exception that get bypassed (at least std::out_of_range and std::length_error have been identified) is know to work. See G-Research/ParquetSharp@6985e78 for an example.

Configuration

.NET Runtime: 10.0.0 or 10.0.1
OS: macOS (tested from 12.x to 26.x)
Architecture: Intel (x64) only - also via Rosetta 2 emulation

Other information

No response

Activity

  1. added
    untriaged
    on Dec 11, 2025
  2. added
    needs-area-label
    on Dec 11, 2025
  3. added
    area-ExceptionHandling-coreclr
    and removed
    needs-area-label
    on Dec 11, 2025
  4. jkotas commented on Dec 11, 2025

    @jkotas
    Member
  5. janvorli commented on Dec 11, 2025

    @janvorli
    Member

    It is strange. My local release build of both .NET 10 and .NET 11 doesn't have this problem. The output of the repro I get on my x64 mac is

    ThrowInvalidArgumentCatchException: 0
    ThrowLengthErrorCatchException: 1
    ThrowLengthErrorCatchLengthError: 0
    ThrowLogicErrorCatchException: 0
    ThrowOutOfRangeCatchException: 0
    ThrowOutOfRangeCatchOutOfRange: 0
    ThrowRangeErrorCatchException: 0
    ThrowRuntimeErrorCatchException: 0
    TryOutOfRangeCatchException: 0
    TryOutOfRangeCatchOutOfRange: 0
    

    However, I can repro it on the same machine using the official build that was created from the same state of sources.

  6. janvorli commented on Dec 11, 2025

    @janvorli
    Member

    The only difference w.r.t. official builds I can imagine is signing. But I don't see why signing would influence C++ EH.

  7. jgiannuzzi commented on Dec 15, 2025

    @jgiannuzzi
    ContributorAuthor

    Thanks for looking into this @janvorli! On the output you give above you did reproduce one of the exception catching issues (ThrowLengthErrorCatchException - so std::length_error being thrown and not being caught as std::exception). It is still really weird that the official build exhibits the issue for std::out_of_range but your local release build doesn't!

  8. jkotas commented on Dec 15, 2025

    @jkotas
    Member

    It is still really weird that the official build exhibits the issue for std::out_of_range but your local release build doesn't!

    Does the official build use the same XCode version as your local build?

  9. jgiannuzzi commented on Dec 17, 2025

    @jgiannuzzi
    ContributorAuthor

    Does the official build use the same XCode version as your local build?

    I'm assuming this question is directed at @janvorli as I have only encountered and tried to reproduce this issue with the official release builds.

  10. janvorli commented on Jan 2, 2026

    @janvorli
    Member

    Does the official build use the same XCode version as your local build?

    That's a good point. My local macOS x64 machine still has XCode 13.1, the .NET 10.0.0 official builds are built using 14.5

  11. janvorli commented on Jan 2, 2026

    @janvorli
    Member

    It seems like there is no newer XCode for x64 macOS, let me try it on my arm64 one.

  12. janvorli commented on Jan 2, 2026

    @janvorli
    Member

    I've built it on my M1 mac targeting the x64 and I can repro the problem with my build too.

    Debugging the macOS libc C++ EH code in assembler, as there is no source level code debugging possible, I've found the place where the exception type is matched for catch. It is in:
    libc++abi.dylib __cxxabiv1::__class_type_info::can_catch(__cxxabiv1::__shim_type_info const*, void*&) const
    In of one of the problematic cases, when the std::length_error is thrown and it is attempted to catch it as std::exception, I can see that the first argument is
    libc++abi.dylibtypeinfo for std::exceptionand the other isdotnettypeinfo for std::length_error

    It means is trying to use typeinfo for std::length_error type from a wrong module for some reason. That explains why it cannot catch it.

    The __cxxabiv1::__class_type_info::can_catch returns false for this case.

    For a case that didn't fail e.g. the ThrowLogicErrorCatchException, both arguments to the can_catch represent type infos from the same (libc++) module:
    libc++abi.dylibtypeinfo for std::exceptionandlibc++abi.dylibtypeinfo for std::logic_error.

    It seems that the exception types that fail are ones that are used in our host too.

  13. janvorli commented on Jan 3, 2026

    @janvorli
    Member

    I can also repro the problem when I build .NET 9 on my M1 machine, so it seems it is really related to the XCode / libc++ version.

  14. janvorli commented on Jan 13, 2026

    @janvorli
    Member

    I have finally found the culprit. We compile all .NET runtime code with -fno-rtti, as we don't want any RTTI info for the types we implement - it would be a waste of space. But there is a catch. The .NET host uses functions from libc++ that throw some std:: exceptions. In that case, if -fno-rtti is set , the compiler is forced to emit fallback RTTI objects into the executable. And when your shared library that also uses std:: exception is loaded, somehow the linker uses the fallback RTTI at some places and the libc++ RTTI at others, leading to the problem.

    I've verified that enabling RTTI for the whole runtime fixes the issue and there is no fallback RTTI for the std:: exceptions anymore. But as I've mentioned, we cannot do that globally. It looks like we will need to selectively remove the -fno-rtti for the host or parts of the host that uses the std:: stuff that can throw.

  15. jkotas commented on Jan 13, 2026

    @jkotas
    Member

    somehow the linker uses the fallback RTTI at some places and the libc++ RTTI at others, leading to the problem.

    This sounds like a problem with symbol visibility. Should all std:: symbols in the host be private?

  16. janvorli commented on Jan 13, 2026

    @janvorli
    Member

    That was my first theory, but it doesn't seem like visibility problem. We compile the host with all symbols visibility disabled. I've tried to add -fvisibility-inlines-hidden too. I am not aware of any other way to control the visibility. Here is an example what the symbols for one of the typeinfo looks like:

    Dumped by nm:
    00000000004cb410 (__DATA_CONST,__const) non-external (was a private external) typeinfo for std::exception
    00000000004472a0 (__TEXT,__const) non-external (was a private external) typeinfo name for std::exception
    Dumped by objdump:
    000000010000b0b3 w O __TEXT,__const .hidden typeinfo name for std::exception
    000000010000c2f0 w O __DATA_CONST,__const .hidden typeinfo for std::exception

    However, this is the same in .NET 9 and .NET 10. In official .NET 9 build, the problem doesn't occur, in .NET 10 it does. It occurs in my local .NET 9 build too, so I believe it is due to the recent xcode.

    I've asked copilot and it suggested the -fno-rtti being the reason for the symbols being present and it said that RTTI should be enabled for compiling files that use std:: exceptions (even indirectly by using libc++ methods that can throw them) so that the resulting binary uses the typeinfo from the libc++ and not its fallback copy.

  17. jkotas commented on Jan 13, 2026

    @jkotas
    Member

    RTTI should be enabled for compiling files that use std:: exceptions (even indirectly by using libc++ methods that can throw them)

    We have switched to std::bad_alloc recently everywhere (https://github.com/search?q=repo%3Adotnet%2Fruntime%20std%3A%3Abad_alloc&type=code) so that we can use standard operator new.

    Does that mean we need to compile with RTTI everywhere to make this work well?

    cc @jkoritzinsky

  18. dotnet-policy-service commented on Apr 15, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke
    See info in area-owners.md if you want to be subscribed.

  19. removed
    untriaged
    on Jul 10, 2026
  20. added this to the 11.0.0 milestone on Jul 10, 2026
  21. janvorli commented on Jul 14, 2026

    @janvorli
    Member

    I've been looking into it more and found another way to prevent the issue. Making the host export only selected symbols (just _main) via an exports file fixes the behavior too. The local typeinfo symbols are then marked as non-external in the host binary and the 3rd party dylib cannot bind to them
    .

  22. added a commit that references this issue on Jul 27, 2026
    c6e9091
  23. locked and limited conversation to collaborators on Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions