Skip to content

Compiler crash (assertion) when using C++ interop and SWIFT_SHARED_REFERENCE #84557

@gmondada

Description

@gmondada

Description

When I define a Swift struct containing an optional reference to a C++ class, the compiler crashes.

Assertion failed: (getReferenceCounting() == ReferenceCounting::Custom && "only supported for custom ref-counting"), function strongCustomRetain, file ClassTypeInfo.h, line 105.

Reproduction

Let's define a simple C++ class imported in Swift as a reference type, thanks to SWIFT_SHARED_REFERENCE:

class vola_buf {
public:
    atomic_int _ref_count;
    
    vola_buf() : _ref_count(1) {}

    vola_buf(const vola_buf&) = delete;
    vola_buf(vola_buf&&) = delete;
    ~vola_buf() = default;

    vola_buf& operator=(const vola_buf&) = delete;
    const vola_buf& operator=(vola_buf&&) = delete;

} SWIFT_SHARED_REFERENCE(vola_buf_retain, vola_buf_release);

static inline void vola_buf_retain(struct vola_buf * _Nonnull me) {
    int count = atomic_fetch_add(&me->_ref_count, 1);
    assert(count > 0);
}

static inline void vola_buf_release(struct vola_buf * _Nonnull me) {
    int count = atomic_fetch_sub(&me->_ref_count, 1);
    assert(count > 0);
    if (count == 1)
        delete me;
}

Let's wrap the reference in a Swift struct:

public struct Buffer {
    private let buffer: vola_buf

    init(buffer: vola_buf) {
        self.buffer = buffer
    }
}

Now, as soon as I use an optional Buffer (aka Buffer?), the compiler crashes. Example:

struct CrashingType {
    let buffer: Buffer // this works
    let optionalBuffer: Buffer? // this makes the compiler crash
}

Stack dump

$ swiftly run swift test -Xcc -I/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/include
[1/1] Planning build
Building for debugging...
error: compile command failed due to signal 6 (use -v to see invocation)
Assertion failed: (getReferenceCounting() == ReferenceCounting::Custom && "only supported for custom ref-counting"), function strongCustomRetain, file ClassTypeInfo.h, line 105.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      Program arguments: /Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/gabriele/perso/prj/crash-demo/Tests/VolaTests/VolaTests.swift -emit-dependencies-path /Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/VolaTests.build/VolaTests.d -emit-reference-dependencies-path /Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/VolaTests.build/VolaTests.swiftdeps -serialize-diagnostics-path /Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/VolaTests.build/VolaTests.dia -target arm64-apple-macosx15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -cxx-interoperability-mode=default -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I /Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/Modules -I /Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/lib/swift/macosx/testing -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/PrivateFrameworks -color-diagnostics -Xcc -fcolor-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=5 -module-cache-path /Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 6 -Onone -D SWIFT_PACKAGE -D DEBUG -D SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE -enable-cross-import-overlays -empty-abi-descriptor -plugin-path /Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/lib/swift/host/plugins/testing -enable-anonymous-context-mangled-names -file-compilation-dir /Users/gabriele/perso/prj/crash-demo -Xcc -fmodule-map-file=/Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/VolaCpp.build/module.modulemap -Xcc -I -Xcc /Users/gabriele/perso/prj/crash-demo/Sources/VolaCpp/include -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/PrivateFrameworks -Xcc -fPIC -Xcc -g -Xcc -I/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/include -no-auto-bridging-header-chaining -module-name VolaTests -package-name crash_demo -in-process-plugin-server-path /Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/local/lib/swift/host/plugins -target-sdk-version 26.0 -target-sdk-name macosx26.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -parse-as-library -o /Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/VolaTests.build/VolaTests.swift.o -index-store-path /Users/gabriele/perso/prj/crash-demo/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.      Apple Swift version 6.2 (swift-6.2-RELEASE)
2.      Compiling with the current language version
3.      While evaluating request IRGenRequest(IR Generation for file "/Users/gabriele/perso/prj/crash-demo/Tests/VolaTests/VolaTests.swift")
4.      While emitting IR SIL function "@$s9VolaTests12CrashingTypeV14optionalBuffer0A00F0VSgvg".
 for getter for optionalBuffer (at /Users/gabriele/perso/prj/crash-demo/Tests/VolaTests/VolaTests.swift:7:9)
 #0 0x0000000105f84f18 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1059dcf18)
 #1 0x0000000105f83644 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1059db644)
 #2 0x0000000105f85560 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1059dd560)
 #3 0x0000000182a3d6a4 (/usr/lib/system/libsystem_platform.dylib+0x1804ad6a4)
 #4 0x0000000182a0388c (/usr/lib/system/libsystem_pthread.dylib+0x18047388c)
 #5 0x000000018290ca3c (/usr/lib/system/libsystem_c.dylib+0x18037ca3c)
 #6 0x000000018290bc70 (/usr/lib/system/libsystem_c.dylib+0x18037bc70)
 #7 0x0000000105f983cc (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1059f03cc)
 #8 0x0000000100bbfb68 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x100617b68)
 #9 0x0000000100c185b4 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1006705b4)
#10 0x0000000100c14ec8 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x10066cec8)
#11 0x0000000100d41f28 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x100799f28)
#12 0x0000000100d3d3e4 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1007953e4)
#13 0x0000000100d3ac54 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x100792c54)
#14 0x0000000100bd6fe8 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x10062efe8)
#15 0x0000000100ceb99c (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x10074399c)
#16 0x0000000100d39de4 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x100791de4)
#17 0x0000000100cf31b8 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x10074b1b8)
#18 0x0000000100ced86c (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x10074586c)
#19 0x0000000100840664 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x100298664)
#20 0x000000010083d1cc (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1002951cc)
#21 0x000000010083c604 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x100294604)
#22 0x0000000100848dc8 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1002a0dc8)
#23 0x000000010083e3fc (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1002963fc)
#24 0x000000010083dbb8 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x100295bb8)
#25 0x00000001005e16b4 (/Users/gabriele/Library/Developer/Toolchains/swift-6.2-RELEASE.xctoolchain/usr/bin/swift-frontend+0x1000396b4)
#26 0x0000000182662b98 

error: fatalError

Expected behavior

The compiler should eventually signal an error, but not crash

Environment

$ swiftly run swift --version
Apple Swift version 6.2 (swift-6.2-RELEASE)
Target: arm64-apple-macosx15.0
Build config: +assertions

Additional information

See demo project below

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.c++ interopFeature: Interoperability with C++crashBug: A crash, i.e., an abnormal termination of software

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions