Skip to content

Fix native EH in external shared libs on macOS - #130693

Merged
janvorli merged 2 commits into
dotnet:mainfrom
janvorli:fix-cpp-eh-interop-macos
Jul 27, 2026
Merged

janvorli merged 2 commits into
dotnet:mainfrom
janvorli:fix-cpp-eh-interop-macos

Conversation

@janvorli

@janvorli janvorli commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

There is an exception-handling issue on macOS when throwing and catching some standard C++ exceptions in a native C++ lib. The hosts (dotnet, apphost, corerun, nethost) get local copies of typeinfo for several of these exceptions and these copies are not hidden. When an external shared library is loaded, the linker resolves references to those typeinfo instances to the local copies in the host instead of the ones in the libc++. That results in failures matching these exception types in c++ catch.

The fix is to disable exporting symbols from these hosts (except for the _GetCurrentClrDetails from the nethost, which is needed)

Close #122445

There is an exception-handling issue on macOS when throwing and catching
some standard C++ exceptions in a native C++ lib. The hosts (dotnet,
apphost, corerun, nethost) get local copies of typeinfo for several
of these exceptions and these copies are not hidden. When an external
shared library is loaded, the linker resolves references to those typeinfo
instances to the local copies in the host instead of the ones in
the libc++. That results in failures matching these exception types
in c++ catch.

The fix is to disable exporting symbols from these hosts (except for the
_get_hostfxr_path from the nethost, which is needed)
@janvorli janvorli added this to the 11.0.0 milestone Jul 14, 2026
@janvorli
janvorli requested review from elinor-fung and jkotas July 14, 2026 15:02
@janvorli janvorli self-assigned this Jul 14, 2026
Copilot AI review requested due to automatic review settings July 14, 2026 15:02
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

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.

Pull request overview

This PR updates macOS linker behavior for several host executables/libraries to prevent them from exporting local C++ RTTI/typeinfo symbols that can be incorrectly bound by externally loaded shared libraries, breaking typed C++ exception catching.

Changes:

  • Add Apple-specific link options to suppress exported symbols in dotnet, apphost, and corerun.
  • Restrict nethost exports on Apple to only _get_hostfxr_path.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/native/corehost/nethost/CMakeLists.txt Adds an Apple-only export restriction so only _get_hostfxr_path is exported.
src/native/corehost/dotnet/CMakeLists.txt Adds an Apple-only option to disable exported symbols from the dotnet host executable.
src/native/corehost/apphost/CMakeLists.txt Adds an Apple-only option to disable exported symbols from the apphost executable.
src/coreclr/hosts/corerun/CMakeLists.txt Adds an Apple-only option to disable exported symbols from the corerun test host.

Copilot AI review requested due to automatic review settings July 15, 2026 16:53

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "3f98f4c41defc5d51c375422c5c727420f6a2ac7",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "08c6b6f03263877f650a0532e693506af42c9251",
  "last_reviewed_commit": "3f98f4c41defc5d51c375422c5c727420f6a2ac7",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "08c6b6f03263877f650a0532e693506af42c9251",
  "last_recorded_worker_run_id": "29684888136",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "3f98f4c41defc5d51c375422c5c727420f6a2ac7",
      "review_id": 4730670021
    }
  ]
}

@github-actions github-actions Bot left a comment

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.

Holistic Review

Motivation: On macOS, the executable hosts (corerun, apphost, dotnet) each link in local copies of typeinfo for standard C++ EH types such as std::bad_alloc and std::out_of_range. Because those symbols were exported, the dynamic linker resolved references from externally loaded shared libraries to the host's local copies rather than the libc++ ones, causing C++ catch type matching to fail for exceptions thrown and caught inside a native shared library. This addresses issue #122445.

Approach: Add an Apple-only linker option to each of the three host CMakeLists.txt files. apphost and dotnet use -no_exported_symbols to suppress all dynamic exports; corerun uses -exported_symbol,_GetCurrentClrDetails to keep only the single symbol that coreshim looks up via GetProcAddress. The changes are guarded by CLR_CMAKE_HOST_APPLE, so other platforms are unaffected.

Summary: The fix is minimal, correctly scoped, and matches the diagnosis. Points verified:

  • corerun retains its only externally-consumed export, GetCurrentClrDetails (used by src/coreclr/hosts/coreshim/CoreShim.cpp), and the leading underscore is the correct Mach-O C symbol mangling for the extern "C" export. The explicit single-symbol export list effectively overrides the pre-existing -rdynamic/CLR_CMAKE_KEEP_NATIVE_SYMBOLS on Apple, which is the intent.
  • For apphost, the add_link_options call precedes add_subdirectory(standalone), so the directory-scoped option correctly propagates to the apphost target actually defined in standalone/CMakeLists.txt (subtle but correct per CMake directory-property inheritance).
  • nethost (which must keep get_hostfxr_path per its Exports.def) and singlefilehost (which manages its own exports file) are intentionally not touched, consistent with the stated design.

No correctness, security, or portability concerns; the change is Apple-specific and low-risk. Verifying actual EH behavior depends on macOS CI, which cannot be exercised here, but the mechanism is sound. LGTM.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 58.6 AIC · ⌖ 9.8 AIC · ⊞ 10K

@janvorli

Copy link
Copy Markdown
Member Author

/ba-g build analysis is stuck

@janvorli
janvorli merged commit c6e9091 into dotnet:main Jul 27, 2026
178 checks passed
@janvorli

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 28, 2026
agocke pushed a commit that referenced this pull request Aug 11, 2026
Backport of #130693 to release/10.0

/cc @janvorli

## Customer Impact

- [x] Customer reported
- [ ] Found internally

There is an exception-handling issue on macOS when throwing and catching
some standard C++ exceptions in a native C++ lib. When customer pinvokes
into a shared library that uses C++ exception handling internally and
throws / catches standard c++ exceptions like `std::out_of_range` and
others, it can fail to catch those. Even though the exception stays
within the confines of the native code of the shared library.

The hosts (dotnet, apphost, corerun, nethost) get local copies of
typeinfo for several of these exceptions and these copies are not
hidden. When an external shared library is loaded, the linker resolves
references to those typeinfo instances to the local copies in the host
instead of the ones in the libc++. That results in failures matching
these exception types in c++ catch.

## Regression

- [ ] Yes
- [x] No

## Testing
Directed test shared by the customer, CI testing, local coreclr and
libraries tests.

## Risk
Low, it just prevents exporting symbols from hosts (except of
`_GetCurrentClrDetails` from corerun). Those symbols are not needed for
anything.

---------

Co-authored-by: Jan Vorlicek <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants