Repository navigation
Description
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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
jkotas commentedon Dec 11, 2025
cc @janvorli
janvorli commentedon Dec 11, 2025
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
However, I can repro it on the same machine using the official build that was created from the same state of sources.
janvorli commentedon Dec 11, 2025
The only difference w.r.t. official builds I can imagine is signing. But I don't see why signing would influence C++ EH.
jgiannuzzi commentedon Dec 15, 2025
Thanks for looking into this @janvorli! On the output you give above you did reproduce one of the exception catching issues (
ThrowLengthErrorCatchException- sostd::length_errorbeing thrown and not being caught asstd::exception). It is still really weird that the official build exhibits the issue forstd::out_of_rangebut your local release build doesn't!jkotas commentedon Dec 15, 2025
Does the official build use the same XCode version as your local build?
jgiannuzzi commentedon Dec 17, 2025
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.
janvorli commentedon Jan 2, 2026
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
janvorli commentedon Jan 2, 2026
It seems like there is no newer XCode for x64 macOS, let me try it on my arm64 one.
janvorli commentedon Jan 2, 2026
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*&) constIn 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 islibc++abi.dylibtypeinfo for std::exceptionand the other isdotnettypeinfo for std::length_errorIt means is trying to use typeinfo for
std::length_errortype from a wrong module for some reason. That explains why it cannot catch it.The
__cxxabiv1::__class_type_info::can_catchreturns false for this case.For a case that didn't fail e.g. the
ThrowLogicErrorCatchException, both arguments to thecan_catchrepresent 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.
janvorli commentedon Jan 3, 2026
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.
janvorli commentedon Jan 13, 2026
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-rttiis 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-rttifor the host or parts of the host that uses the std:: stuff that can throw.jkotas commentedon Jan 13, 2026
This sounds like a problem with symbol visibility. Should all std:: symbols in the host be private?
janvorli commentedon Jan 13, 2026
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-hiddentoo. 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.
jkotas commentedon Jan 13, 2026
We have switched to
std::bad_allocrecently 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
dotnet-policy-service commentedon Apr 15, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.
janvorli commentedon Jul 14, 2026
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
.
Fix native EH in external shared libs on macOS (#130693)