Skip to content

Commit 273369c

Browse files
rauhulDougGregor
authored andcommitted
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 (cherry picked from commit ce02872)
1 parent cb238d7 commit 273369c

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
@@ -271,7 +271,7 @@ func isValidPointerForNativeRetain(object: Builtin.RawPointer) -> Bool {
271271
#if _pointerBitWidth(_64)
272272
if unsafe (objectBits & HeapObject.immortalObjectPointerBit) != 0 { return false }
273273
#endif
274-
274+
275275
return true
276276
}
277277

@@ -576,3 +576,19 @@ func _embeddedReportFatalErrorInFile(prefix: StaticString, message: UnsafeBuffer
576576
if message.count > 0 { print(": ", terminator: "") }
577577
unsafe print(message)
578578
}
579+
580+
// CXX Exception Personality
581+
582+
public typealias _Unwind_Action = CInt
583+
public typealias _Unwind_Reason_Code = CInt
584+
585+
@c @used
586+
public func _swift_exceptionPersonality(
587+
version: CInt,
588+
actions: _Unwind_Action,
589+
exceptionClass: UInt64,
590+
exceptionObject: UnsafeMutableRawPointer,
591+
context: UnsafeMutableRawPointer
592+
) -> _Unwind_Reason_Code {
593+
fatalError("C++ exception handling detected but the Embedded Swift runtime does not support exceptions")
594+
}
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)