Add Embedded Swift Cxx exception personality

Users frequently run into a missing runtime symbol for Cxx exceptions
(`_swift_exceptionPersonality`) when mixing Embedded Swift and Cxx with
exceptions enabled. This leads to a confusing an hard to debug linker
error. This commit adds an implementation of this function to the
Embedded Swift runtime which simply fatal errors if a cxx exception is
caught in a Swift frame.

Issue: rdar://164423867
Issue: #85490
This commit is contained in:
Rauhul Varma
2025-11-13 09:59:46 -08:00
parent a1b41acf8d
commit ce02872adf
2 changed files with 34 additions and 1 deletions

View File

@@ -432,7 +432,7 @@ func isValidPointerForNativeRetain(object: Builtin.RawPointer) -> Bool {
#if _pointerBitWidth(_64)
if unsafe (objectBits & HeapObject.immortalObjectPointerBit) != 0 { return false }
#endif
return true
}
@@ -750,3 +750,19 @@ func _embeddedReportFatalErrorInFile(prefix: StaticString, message: UnsafeBuffer
if message.count > 0 { print(": ", terminator: "") }
unsafe print(message)
}
// CXX Exception Personality
public typealias _Unwind_Action = CInt
public typealias _Unwind_Reason_Code = CInt
@c @used
public func _swift_exceptionPersonality(
version: CInt,
actions: _Unwind_Action,
exceptionClass: UInt64,
exceptionObject: UnsafeMutableRawPointer,
context: UnsafeMutableRawPointer
) -> _Unwind_Reason_Code {
fatalError("C++ exception handling detected but the Embedded Swift runtime does not support exceptions")
}

View File

@@ -0,0 +1,17 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -enable-experimental-feature Embedded -O -c -o %t/main.o -cxx-interoperability-mode=default
// RUN: %target-clang %target-clang-resource-dir-opt %t/main.o -o %t/a.out -dead_strip
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: swift_feature_Embedded
@_expose(Cxx)
func f1() {
print("OK!")
}
f1()
// CHECK: OK!