Repository navigation
Fix native EH in external shared libs on macOS - #130693
Conversation
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)
|
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. |
There was a problem hiding this comment.
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, andcorerun. - Restrict
nethostexports 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. |
|
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
}
]
} |
There was a problem hiding this comment.
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:
corerunretains its only externally-consumed export,GetCurrentClrDetails(used bysrc/coreclr/hosts/coreshim/CoreShim.cpp), and the leading underscore is the correct Mach-O C symbol mangling for theextern "C"export. The explicit single-symbol export list effectively overrides the pre-existing-rdynamic/CLR_CMAKE_KEEP_NATIVE_SYMBOLSon Apple, which is the intent.- For
apphost, theadd_link_optionscall precedesadd_subdirectory(standalone), so the directory-scoped option correctly propagates to theapphosttarget actually defined instandalone/CMakeLists.txt(subtle but correct per CMake directory-property inheritance). nethost(which must keepget_hostfxr_pathper itsExports.def) andsinglefilehost(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
|
/ba-g build analysis is stuck |
|
/backport to release/10.0 |
|
Started backporting to |
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]>
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