Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on IR Generation in LLVM DenseMap, but it happens only in some configurations (Patch included) #70345

Open
bc-lee opened this issue Dec 9, 2023 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@bc-lee
Copy link
Contributor

bc-lee commented Dec 9, 2023

Description

I'm currently compiling Swift in Fedora, and I encountered a crash during IR generation, but it occurs only in some configurations.

Steps to reproduce

  1. Create a docker container with fedora 39 and start it
  2. Install dependencies
  3. Clone swift release/5.9 branch
  4. Build

Detailed build scripts are here

Expected result

Build succeeds, though some tests may fail

Actual result

In some cases, build fails.

Cases that do not crash

  • (x86_64) Fedora 39 with Fedora 39's Clang (17.0.6) link
  • (x86_64) Fedora 39 with Clang 17.0.6 built from source link
  • (aarch64) Fedora 39 with Clang included in Swift 5.8.1 link

Cases that crash

  • (aarch64) Fedora 39 with Fedora 39's Clang (17.0.6) link, link2
  • (aarch64) Fedora 39 with Clang 17.0.6 built from source link

The crash stack trace is as follows:

swift-frontend: /builddir/build/BUILD/swift-source/llvm-project/llvm/include/llvm/ADT/DenseMap.h:408: void llvm::DenseMapBase<llvm::DenseMap<swift::irgen::LinkEntity, llvm::Function *>, swift::irgen::LinkEntity, llvm::Function *, llvm::DenseMapInfo<swift::irgen::LinkEntity>, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function *>>::moveFromOldBuckets(BucketT *, BucketT *) [DerivedT = llvm::DenseMap<swift::irgen::LinkEntity, llvm::Function *>, KeyT = swift::irgen::LinkEntity, ValueT = llvm::Function *, KeyInfoT = llvm::DenseMapInfo<swift::irgen::LinkEntity>, BucketT = llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function *>]: Assertion `!FoundVal && "Key already in new map?"' failed.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend -frontend -c /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Algorithms/Split.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Algorithms/StartsWith.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Algorithms/Trim.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Consumers/CollectionConsumer.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Consumers/FixedPatternConsumer.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Consumers/ManyConsumer.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Consumers/PredicateConsumer.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Matching/FirstMatch.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Matching/MatchResult.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Matching/Matches.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Matching/MatchingCollectionConsumer.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Matching/MatchingCollectionSearcher.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Searchers/CollectionSearcher.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Searchers/ConsumerSearcher.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Searchers/NaivePatternSearcher.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Searchers/PatternOrEmpty.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Searchers/PredicateSearcher.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Algorithms/Searchers/ZSearcher.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/ByteCodeGen.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Capture.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Compiler.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/ConsumerInterface.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Backtracking.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Consume.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Engine.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/InstPayload.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Instruction.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/MEBuilder.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/MEBuiltins.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/MECapture.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/MEProgram.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/MEQuantify.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Metrics.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Processor.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Registers.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Structuralize.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Tracing.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Executor.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/MatchingOptions.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/PrintAsPattern.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Regex/ASTConversion.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Regex/AnyRegexOutput.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Regex/Core.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Regex/CustomComponents.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Regex/DSLTree.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Regex/Match.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Regex/Options.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/ASCII.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/CaseConversion.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/CharacterProps.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/Comparison.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/Decoding.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/Encodings.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/Formatting.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/NFC.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/NecessaryEvils.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/NumberParsing.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/ScalarProps.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/Transcoding.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/UCD.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/Validation.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Unicode/WordBreaking.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/ASTBuilder.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/AsciiBitset.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/Misc.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/Protocols.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/RegexFactory.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/Traced.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/TypeVerification.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/TypedIndex.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Utility/TypedInt.swift /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/_CharacterClassModel.swift -supplementary-output-file-map /tmp/supplementaryOutputs-e5a39b -target aarch64-unknown-linux-gnu -Xllvm -aarch64-use-tbi -disable-objc-interop -sdk / -I /builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/./lib/swift/linux -color-diagnostics -warn-implicit-overrides -enable-library-evolution -module-cache-path /builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/./module-cache -module-link-name swift_StringProcessing -resource-dir /builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/./lib/swift -static -swift-version 5 -tools-directory /builddir/build/BUILD/swift-source/build/buildbot_linux/llvm-linux-aarch64/./bin -O -library-level api -D SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY -D SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED -D SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING -D SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING -D SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION -D SWIFT_RUNTIME_OS_VERSIONING -D SWIFT_STDLIB_ENABLE_UNICODE_DATA -D SWIFT_STDLIB_ENABLE_VECTOR_TYPES -D SWIFT_STDLIB_HAS_COMMANDLINE -D SWIFT_STDLIB_HAS_STDIN -D SWIFT_STDLIB_HAS_ENVIRON -D SWIFT_THREADING_LINUX -D SWIFT_ENABLE_REFLECTION -require-explicit-availability=ignore -enforce-exclusivity=unchecked -disable-autolinking-runtime-compatibility-concurrency -disable-objc-interop -public-autolink-library swift_RegexParser -enable-ossa-modules -enable-lexical-lifetimes=false -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -prespecialize-generic-metadata -define-availability "SwiftStdlib 9999:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999" -define-availability "SwiftStdlib 5.0:macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2" -define-availability "SwiftStdlib 5.1:macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0" -define-availability "SwiftStdlib 5.2:macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4" -define-availability "SwiftStdlib 5.3:macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0" -define-availability "SwiftStdlib 5.4:macOS 11.3, iOS 14.5, watchOS 7.4, tvOS 14.5" -define-availability "SwiftStdlib 5.5:macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0" -define-availability "SwiftStdlib 5.6:macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4" -define-availability "SwiftStdlib 5.7:macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0" -define-availability "SwiftStdlib 5.8:macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4" -define-availability "SwiftStdlib 5.9:macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0" -define-availability "SwiftStdlib 5.10:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999" -target-min-inlining-version min -plugin-path /builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/lib/swift/host/plugins -plugin-path /builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/local/lib/swift/host/plugins -Xcc -DSWIFT_STDLIB_HAS_ENVIRON -parse-as-library -module-name _StringProcessing -o /builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/stdlib/public/StringProcessing//LINUX/aarch64/_StringProcessing.o -runtime-compatibility-version none -disable-autolinking-runtime-compatibility-dynamic-replacements
1.	Swift version 5.9 (swift-5.9-RELEASE)
2.	Compiling with the current language version
3.	While evaluating request IRGenRequest(IR Generation for module _StringProcessing)
4.	While emitting metadata for 'OpCode' (at /builddir/build/BUILD/swift-source/swift-experimental-string-processing/Sources/_StringProcessing/Engine/Instruction.swift:25:3)
 #0 0x0000000006227760 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x6227760)
 #1 0x00000000062257d8 llvm::sys::RunSignalHandlers() (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x62257d8)
 #2 0x0000000006227b78 SignalHandler(int) Signals.cpp:0:0
 #3 0x0000ffff82aee7fc (linux-vdso.so.1+0x7fc)
 #4 0x0000ffff81e92280 __pthread_kill_implementation (/lib64/libc.so.6+0x92280)
 #5 0x0000ffff81e45800 gsignal (/lib64/libc.so.6+0x45800)
 #6 0x0000ffff81e30288 abort (/lib64/libc.so.6+0x30288)
 #7 0x0000ffff81e3e56c __assert_fail_base (/lib64/libc.so.6+0x3e56c)
 #8 0x0000ffff81e3e5e0 __assert_perror_fail (/lib64/libc.so.6+0x3e5e0)
 #9 0x0000000000a52368 llvm::DenseMapBase<llvm::DenseMap<swift::irgen::LinkEntity, llvm::Function*, llvm::DenseMapInfo<swift::irgen::LinkEntity, void>, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>>, swift::irgen::LinkEntity, llvm::Function*, llvm::DenseMapInfo<swift::irgen::LinkEntity, void>, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>>::moveFromOldBuckets(llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>*, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>*) DLangDemangle.cpp:0:0
#10 0x0000000000a521f4 llvm::DenseMap<swift::irgen::LinkEntity, llvm::Function*, llvm::DenseMapInfo<swift::irgen::LinkEntity, void>, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>>::grow(unsigned int) DLangDemangle.cpp:0:0
#11 0x0000000000a52148 llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>* llvm::DenseMapBase<llvm::DenseMap<swift::irgen::LinkEntity, llvm::Function*, llvm::DenseMapInfo<swift::irgen::LinkEntity, void>, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>>, swift::irgen::LinkEntity, llvm::Function*, llvm::DenseMapInfo<swift::irgen::LinkEntity, void>, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>>::InsertIntoBucketImpl<swift::irgen::LinkEntity>(swift::irgen::LinkEntity const&, swift::irgen::LinkEntity const&, llvm::detail::DenseMapPair<swift::irgen::LinkEntity, llvm::Function*>*) DLangDemangle.cpp:0:0
#12 0x0000000000a49a88 swift::irgen::IRGenModule::getAddrOfValueWitness(swift::CanType, swift::irgen::ValueWitness, swift::ForDefinition_t) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0xa49a88)
#13 0x0000000000b3d4d8 addValueWitness(swift::irgen::IRGenModule&, swift::irgen::ConstantStructBuilder&, swift::irgen::ValueWitness, swift::irgen::FixedPacking, swift::CanType, swift::SILType, swift::irgen::TypeInfo const&, llvm::Optional<(anonymous namespace)::BoundGenericTypeCharacteristics>) GenValueWitness.cpp:0:0
#14 0x0000000000b3cc70 addValueWitnessesForAbstractType(swift::irgen::IRGenModule&, swift::irgen::ConstantStructBuilder&, swift::CanType, bool&) GenValueWitness.cpp:0:0
#15 0x0000000000b3c894 swift::irgen::emitValueWitnessTable(swift::irgen::IRGenModule&, swift::CanType, bool, bool) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0xb3c894)
#16 0x0000000000ab9620 swift::irgen::emitEnumMetadata(swift::irgen::IRGenModule&, swift::EnumDecl*) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0xab9620)
#17 0x0000000000a390bc swift::irgen::IRGenerator::emitLazyDefinitions() (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0xa390bc)
#18 0x0000000000908c90 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x908c90)
#19 0x000000000095ba7c swift::GeneratedModule swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::integer_sequence<unsigned long, 0ul>) const DLangDemangle.cpp:0:0
#20 0x0000000000917c6c llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) DLangDemangle.cpp:0:0
#21 0x0000000000909bb4 swift::performIRGeneration(swift::ModuleDecl*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::unique_ptr<swift::SILModule, std::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, llvm::GlobalVariable**) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x909bb4)
#22 0x00000000005ce0b8 generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::unique_ptr<swift::SILModule, std::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>) FrontendTool.cpp:0:0
#23 0x00000000005ca3fc performCompileStepsPostSILGen(swift::CompilerInstance&, std::unique_ptr<swift::SILModule, std::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) FrontendTool.cpp:0:0
#24 0x00000000005c979c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x5c979c)
#25 0x00000000005db6c4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) FrontendTool.cpp:0:0
#26 0x00000000005cbb8c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) FrontendTool.cpp:0:0
#27 0x00000000005cad78 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x5cad78)
#28 0x000000000044d6a0 swift::mainEntry(int, char const**) (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x44d6a0)
#29 0x0000ffff81e309dc __libc_start_call_main (/lib64/libc.so.6+0x309dc)
#30 0x0000ffff81e30ab0 __libc_start_main@GLIBC_2.17 (/lib64/libc.so.6+0x30ab0)
#31 0x000000000044c530 _start (/builddir/build/BUILD/swift-source/build/buildbot_linux/swift-linux-aarch64/bin/swift-frontend+0x44c530)

Patch

The crash occurred in LLVM DenseMap, and I found that it was caused by the following code.

https://github.com/apple/swift/blob/9d033372ac16ac72aadfadc4d1ad4ef2462babe0/lib/IRGen/GenDecl.cpp#L5451

/// Returns the address of a value-witness function.
llvm::Function *IRGenModule::getAddrOfValueWitness(CanType abstractType,
                                                   ValueWitness index,
                                                ForDefinition_t forDefinition) {
  LinkEntity entity = LinkEntity::forValueWitness(abstractType, index);

  llvm::Function *&entry = GlobalFuncs[entity];
  if (entry) {
    if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
    return entry;
  }

  auto signature = getValueWitnessSignature(index);
  LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
  entry = createFunction(*this, link, signature);
  return entry;
}

From what I understand, the GlobalFuncs is a DenseMap that maps LinkEntity to llvm::Function*. operator [] finds the value for the key, and if it doesn't exist, it creates a new value and returns it.
It seems that this code is logically correct, but somehow it crashes. I think this is because the entity is not unique, but I'm not sure.

Instead of using operator [], I changed the code to use find and insert as follows.

/// Returns the address of a value-witness function.
llvm::Function *IRGenModule::getAddrOfValueWitness(CanType abstractType,
                                                   ValueWitness index,
                                                ForDefinition_t forDefinition) {
  LinkEntity entity = LinkEntity::forValueWitness(abstractType, index);

  auto found = GlobalFuncs.find(entity);
  llvm::Function *entry = nullptr;
  if (found != GlobalFuncs.end()) {
    entry = found->second;
    if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
    return entry;
  }

  auto signature = getValueWitnessSignature(index);
  LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
  entry = createFunction(*this, link, signature);
  GlobalFuncs.insert({entity, entry});
  return entry;
}

(Full patch is here. It might need adjustments to apply to the main branch.)

With this patch, the crash is resolved, and the build succeeds.

  • (aarch64) Fedora 39 with Fedora 39's Clang (17.0.6) link
  • (aarch64) Fedora 39 with Clang 17.0.6 built from source link
@bc-lee bc-lee added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Dec 9, 2023
@melizasw
Copy link

I am seeing this crash on Debian Unstable and Testing when building Swift 5.8.1 with Clang 18 and 19 on Arm64 (Did not test with Clang 17). This was not an issue with Clang 16 which was the default compiler at the time that I built the previous package version 5.7.3.

This patch provided by @bc-lee has been added to the Debian distribution package and will have to stay there until this bug is closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants