Skip to content

Commit ce02872

Browse files
committed
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
1 parent a1b41ac commit ce02872

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ func isValidPointerForNativeRetain(object: Builtin.RawPointer) -> Bool {
432432
#if _pointerBitWidth(_64)
433433
if unsafe (objectBits & HeapObject.immortalObjectPointerBit) != 0 { return false }
434434
#endif
435-
435+
436436
return true
437437
}
438438

@@ -750,3 +750,19 @@ func _embeddedReportFatalErrorInFile(prefix: StaticString, message: UnsafeBuffer
750750
if message.count > 0 { print(": ", terminator: "") }
751751
unsafe print(message)
752752
}
753+
754+
// CXX Exception Personality
755+
756+
public typealias _Unwind_Action = CInt
757+
public typealias _Unwind_Reason_Code = CInt
758+
759+
@c @used
760+
public func _swift_exceptionPersonality(
761+
version: CInt,
762+
actions: _Unwind_Action,
763+
exceptionClass: UInt64,
764+
exceptionObject: UnsafeMutableRawPointer,
765+
context: UnsafeMutableRawPointer
766+
) -> _Unwind_Reason_Code {
767+
fatalError("C++ exception handling detected but the Embedded Swift runtime does not support exceptions")
768+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -enable-experimental-feature Embedded -O -c -o %t/main.o -cxx-interoperability-mode=default
3+
// RUN: %target-clang %target-clang-resource-dir-opt %t/main.o -o %t/a.out -dead_strip
4+
// RUN: %target-run %t/a.out | %FileCheck %s
5+
6+
// REQUIRES: swift_in_compiler
7+
// REQUIRES: executable_test
8+
// REQUIRES: swift_feature_Embedded
9+
10+
@_expose(Cxx)
11+
func f1() {
12+
print("OK!")
13+
}
14+
15+
f1()
16+
17+
// CHECK: OK!

0 commit comments

Comments
 (0)