apple / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. This fork is used to manage Apple’s stable releases of Clang as well as support the Swift project.
https://llvm.org
Other
1.09k stars 320 forks source link

Compiler crash while serializing `std::optional` #7661

Open kateinoigakukun opened 8 months ago

kateinoigakukun commented 8 months ago

Description

Clang thread crashes while loading C++ headers including std::optional in a little bit complex structure.

Steps to reproduce

Here is a minimum reproducible project. The include graph consists of A.h -> C.h B.h -> C.h and A and B contain use of std::optional. This happens only with macOS's libc++ but doesn't with libstdc++. (Not tested with libc++ on Linux)

check.zip

.
├── check.swift
└── include
    ├── A.h
    ├── B.h
    ├── C.h
    └── module.modulemap
// ---A.h
#ifndef A_H
#define A_H

#include "C.h"
#include <optional>

class A {
  std::optional<C> Field;
  A(C Field) : Field(Field) {}

};

#endif

// ---B.h
#ifndef B_H
#define B_H

#include "C.h"
#include <optional>

class B {
  std::optional<C> Field;
  B(std::optional<C> Field) : Field(Field) {}
};

#include "A.h"

#endif

// ---C.h
#ifndef C_H
#define C_H

class C {};

#endif
module A {
  header "A.h"
  requires cplusplus
  export *
}

module B {
  header "B.h"
  requires cplusplus
  export *
}

module C {
  header "C.h"
  requires cplusplus
  export *
}
$ /Users/katei/ghq/github.com/apple/build/buildbot_incremental/swift-macosx-arm64/bootstrapping0/bin/swift-frontend -frontend -c check.swift -sdk /Applications/Xcode/Xcode-15.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I include -enable-experimental-cxx-interop -Xcc -std=c++17 -o /dev/null
<unknown>:0: warning: the '-enable-experimental-cxx-interop' flag is deprecated; please pass '-cxx-interoperability-mode=' instead
<unknown>:0: note: Swift will maintain source compatibility for imported APIs based on the selected compatibility mode, so updating the Swift compiler will not change how APIs are imported
Assertion failed: (ConstructorNameSet.empty() && "Failed to find all of the visible " "constructors by walking all the " "lexical members of the context."), function GenerateNameLookupTable, file ASTWriter.cpp, line 4067.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      <eof> parser at end of file
1.      /Applications/Xcode/Xcode-15.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/include/c++/v1/optional:550:8: serializing 'std::__optional_copy_assign_base<C>'
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           0x00000001078ca7a8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001078c9bcc llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001078cade4 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018e085a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018e056cc0 pthread_kill + 288
5  libsystem_c.dylib        0x000000018df66a50 abort + 180
6  libsystem_c.dylib        0x000000018df65d6c err + 0
7  swift-frontend           0x0000000108529314 clang::ASTWriter::GenerateNameLookupTable(clang::DeclContext const*, llvm::SmallVectorImpl<char>&) (.cold.25) + 0
8  swift-frontend           0x00000001060c6e34 clang::ASTWriter::GenerateNameLookupTable(clang::DeclContext const*, llvm::SmallVectorImpl<char>&) + 5388
9  swift-frontend           0x00000001060c7304 clang::ASTWriter::WriteDeclContextVisibleBlock(clang::ASTContext&, clang::DeclContext*) + 348
10 swift-frontend           0x00000001060f3cb8 clang::ASTDeclWriter::Visit(clang::Decl*) + 292
11 swift-frontend           0x0000000106102628 clang::ASTWriter::WriteDecl(clang::ASTContext&, clang::Decl*) + 256
12 swift-frontend           0x00000001060d040c clang::ASTWriter::WriteASTCore(clang::Sema&, llvm::StringRef, clang::Module*) + 8736
13 swift-frontend           0x00000001060ce14c clang::ASTWriter::WriteAST(clang::Sema&, llvm::StringRef, clang::Module*, llvm::StringRef, bool, bool) + 556
14 swift-frontend           0x00000001061176e0 clang::PCHGenerator::HandleTranslationUnit(clang::ASTContext&) + 280
15 swift-frontend           0x0000000105db60c8 clang::MultiplexConsumer::HandleTranslationUnit(clang::ASTContext&) + 52
16 swift-frontend           0x0000000105f19a24 clang::ParseAST(clang::Sema&, bool, bool) + 720
17 swift-frontend           0x0000000105d87e0c clang::FrontendAction::Execute() + 100
18 swift-frontend           0x0000000105d221f0 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 808
19 swift-frontend           0x0000000105d2baec void llvm::function_ref<void ()>::callback_fn<compileModuleImpl(clang::CompilerInstance&, clang::SourceLocation, llvm::StringRef, clang::FrontendInputFile, llvm::StringRef, llvm::StringRef, llvm::function_ref<void (clang::CompilerInstance&)>, llvm::function_ref<void (clang::CompilerInstance&)>)::$_8>(long) + 128
20 swift-frontend           0x000000010781ea58 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 208
21 swift-frontend           0x000000010781ec20 RunSafelyOnThread_Dispatch(void*) + 52
22 swift-frontend           0x000000010781ec78 void* llvm::thread::ThreadProxy<std::__1::tuple<void (*)(void*), (anonymous namespace)::RunSafelyOnThreadInfo*>>(void*) + 24
23 libsystem_pthread.dylib  0x000000018e057034 _pthread_start + 136
24 libsystem_pthread.dylib  0x000000018e051e3c thread_start + 8

Expected behavior

Compilation succeeds without crash

Environment

egorzhdan commented 8 months ago

This is reproducible without using Swift. Moved the issue to Apple's LLVM fork.

clang -x objective-c++-header -index-store-path . -c check/include/B.h -MD -MT dependencies -o Prefix.pch.gch -isysroot {sdk path} -fmodules