Open jpsim opened 1 year ago
Yeah, I can reproduce this locally. This crash happens when we are emitting debug info which the Swift Package does by default. If you need a quick work around, I verified that disabling debug info in the Swift package fixes the issue, however, we'll obviously still want to fix this bug properly.
ok so I think I can bypass it by compiling without emitting debug info, but then I get the following linker errors:
ld: warning: Could not find or use auto-linked library 'swiftCxxStdlib'
ld: warning: Could not find or use auto-linked library 'swiftCxx'
ld: warning: Could not find or use auto-linked library 'swiftstd'
Undefined symbols for architecture arm64:
"std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)>::~__func()", referenced from:
vtable for std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)> in libswift-cxx-interop-demo.lo(main.swift.o)
"std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)>::~__func()", referenced from:
vtable for std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)> in libswift-cxx-interop-demo.lo(main.swift.o)
ld: symbol(s) not found for architecture arm64
Should I file a separate issue to track this?
That's OK. We are tracking this. That's what's blocking the PR. Basically, we need to tell clang that we are actually using these symbols. We've found a couple edge cases here, hopefully this is one of the last ones. To work around this, you can manually tell clang that those symbols are required by explicitly referencing them in a .cpp
file. For example, adding a .cpp
file with something along these lines should fix the first linker error:
using Func = std::function<int(int)>;
int add(int a) { }
void test() {
Func test = add;
test(42);
}
Then you can do something similar for the second one (wrt blocks).
Hmm, still seeing linker failures with this:
// CxxTest.hpp
#ifndef CxxTest_hpp
#define CxxTest_hpp
#include <functional>
using Func = std::function<int(int)>;
inline int invokeWith42(Func fn) { return fn(42); }
void logCXXGreeting();
#endif
// CxxTest.cpp
#include "CxxTest.hpp"
#include <iostream>
void logCXXGreeting() {
std::cout << "Hello from C++!\n";
}
int add(int a) { return a; }
void test() {
Func test = add;
test(42);
}
// main.swift
import CxxTest
import CxxStdlib
logCXXGreeting()
print(invokeWith42(Func { $0 + 1 })) // as a block
let cClosure: @convention(c) (Int) -> Int = { $0 + 1 }
print(invokeWith42(Func(cClosure))) // as a c function pointer
ld: warning: Could not find or use auto-linked library 'swiftCxxStdlib'
ld: warning: Could not find or use auto-linked library 'swiftCxx'
ld: warning: Could not find or use auto-linked library 'swiftstd'
Undefined symbols for architecture arm64:
"std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)>::~__func()", referenced from:
vtable for std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)> in libswift-cxx-interop-demo.lo(main.swift.o)
"std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)>::~__func()", referenced from:
vtable for std::__1::__function::__func<long (long) block_pointer __strong, std::__1::allocator<std::__1::allocator>, int (int)> in libswift-cxx-interop-demo.lo(main.swift.o)
ld: symbol(s) not found for architecture arm64
I wanted to see if this had improved since my initial report, so I tried the initial example with the latest Swift toolchain on macOS and now getting these errors:
/Users/jp/src/swift-63944/Sources/swift-63944/main.swift:3:20: error: cannot convert value of type 'Int32' to expected argument type 'Func' (aka 'std.__1.function<>')
print(invokeWith42(Func { $0 + 1 })) // as a block
^
Assertion failed: (Val && "isa<> used on a null pointer"), function doit, file Casting.h, line 109.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0. Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-10-30-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/jp/src/swift-63944/Sources/swift-63944/main.swift -target arm64-apple-macos10.13 -Xllvm -aarch64-use-tbi -enable-objc-interop -cxx-interoperability-mode=default -sdk /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Products/Debug -I /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Products/Debug/PackageFrameworks -F /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Products/Debug -F /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Users/jp/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -external-plugin-path /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -serialize-debugging-options -package-name swift_63944 -const-gather-protocols-file /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/Objects-normal/arm64/swift-63944_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/jp/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/jp/src/swift-63944 -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-10-30-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/GeneratedModuleMaps/swift63944CXX.modulemap -Xcc -I/Users/jp/src/swift-63944/Sources/swift63944CXX/include -Xcc -I/Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Products/Debug/include -Xcc -I/Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/DerivedSources-normal/arm64 -Xcc -I/Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/DerivedSources/arm64 -Xcc -I/Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name swift_63944 -disable-clang-spi -target-sdk-version 14.0 -target-sdk-name macosx14.0 -external-plugin-path /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-10-30-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-10-30-a.xctoolchain/usr/local/lib/swift/host/plugins -emit-module-doc-path /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/Objects-normal/arm64/swift_63944.swiftdoc -emit-module-source-info-path /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/Objects-normal/arm64/swift_63944.swiftsourceinfo -serialize-diagnostics-path /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/Objects-normal/arm64/swift-63944-master-emit-module.dia -emit-dependencies-path /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/Objects-normal/arm64/swift-63944-master-emit-module.d -o /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/Objects-normal/arm64/swift_63944.swiftmodule -emit-abi-descriptor-path /Users/jp/Library/Developer/Xcode/DerivedData/swift-63944-bwuaupwfpjjjqqacevhstopbaolb/Build/Intermediates.noindex/swift-63944.build/Debug/swift-63944.build/Objects-normal/arm64/swift_63944.abi.json
1. Apple Swift version 5.11-dev (LLVM e22c96610989bdb, Swift bd372c2d6861ce3)
2. Compiling with the current language version
3. While evaluating request TypeCheckSourceFileRequest(source_file "/Users/jp/src/swift-63944/Sources/swift-63944/main.swift")
4. While type-checking statement at [/Users/jp/src/swift-63944/Sources/swift-63944/main.swift:3:1 - line:3:36] RangeText="print(invokeWith42(Func { $0 + 1 })"
5. While type-checking expression at [/Users/jp/src/swift-63944/Sources/swift-63944/main.swift:3:1 - line:3:36] RangeText="print(invokeWith42(Func { $0 + 1 })"
6. While type-checking-target starting at /Users/jp/src/swift-63944/Sources/swift-63944/main.swift:3:1
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0 swift-frontend 0x0000000107708e08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1 swift-frontend 0x0000000107707564 llvm::sys::RunSignalHandlers() + 112
2 swift-frontend 0x0000000107709458 SignalHandler(int) + 304
3 libsystem_platform.dylib 0x00000001874dea24 _sigtramp + 56
4 libsystem_pthread.dylib 0x00000001874afc28 pthread_kill + 288
5 libsystem_c.dylib 0x00000001873bdae8 abort + 180
6 libsystem_c.dylib 0x00000001873bce44 err + 0
7 swift-frontend 0x0000000107abef1c swift::constraints::Solution::getFunctionArgApplyInfo(swift::constraints::ConstraintLocator*) const (.cold.5) + 0
8 swift-frontend 0x00000001033607a4 swift::constraints::Solution::getFunctionArgApplyInfo(swift::constraints::ConstraintLocator*) const + 536
9 swift-frontend 0x00000001032e3bf0 swift::constraints::AllowArgumentMismatch::diagnose(swift::constraints::Solution const&, bool) const + 68
10 swift-frontend 0x000000010321c930 swift::constraints::ConstraintSystem::applySolutionFixes(swift::constraints::Solution const&) + 1572
11 swift-frontend 0x000000010321d070 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 96
12 swift-frontend 0x000000010342f554 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 396
13 swift-frontend 0x000000010342f370 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 184
14 swift-frontend 0x000000010342f230 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 92
15 swift-frontend 0x000000010350e040 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 272
16 swift-frontend 0x00000001035111d8 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 328
17 swift-frontend 0x000000010350f7dc bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
18 swift-frontend 0x000000010350f864 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 52
19 swift-frontend 0x0000000103548fa0 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 216
20 swift-frontend 0x000000010354b330 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 388
21 swift-frontend 0x000000010354b0c0 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 84
22 swift-frontend 0x0000000103548de0 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 44
23 swift-frontend 0x000000010259210c bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_7>(long, swift::SourceFile&) + 16
24 swift-frontend 0x000000010258d84c swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 160
25 swift-frontend 0x000000010258d78c swift::CompilerInstance::performSema() + 76
26 swift-frontend 0x00000001023adfc4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
27 swift-frontend 0x00000001023a16bc performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 704
28 swift-frontend 0x00000001023a0670 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2628
29 swift-frontend 0x00000001021b2448 swift::mainEntry(int, char const**) + 2192
30 dyld 0x0000000187157f28 start + 2236
The most relevant bit appears to be this assertion failure:
Assertion failed: (Val && "isa<> used on a null pointer"), function doit, file Casting.h, line 109
Appears to be one of these: https://github.com/apple/swift/blob/51580e3d5508ece210530ef99666f3776b85142c/stdlib/include/llvm/Support/Casting.h#L81-L115
Description
When building Swift code with a recent toolchain that uses an
std::function
with-enable-experimental-cxx-interop
, the compiler crashes, possibly running out of memory.Steps to reproduce
C++:
Swift:
Full project:
swift-cxx-interop-demo.zip
Full logs:
Expected behavior
Should compile successfully.
Environment
Additional context
https://github.com/apple/swift/pull/63893