Skip to content

[clang-repl] Segfault on assignment when JIT compiling with -O1 or above #95581

Closed
@jeaye

Description

@jeaye

Hi folks. I've been working with @vgvassilev to identify an issue with Clang/LLVM 19. Currently running on Vassil's branch here: #94166

NOTE: This issue does not exist in LLVM 18 (1118c2e).

I have a minimal repro case shown below. It works with -O0, but crashes with -O1 or higher.

The C++ source

struct box {
  box() = default;
  box(int *const data) : data{data} {}

  int *data{};
};

box foo() {
  box ret;
  /* The assignment here causes the crash. Using direct initialization works.
   * With -O0, this compiles and works. With -O1 or above, it dies. */
  ret = new int{};
  return ret;
}

The repro steps

❯ ./build/bin/clang-repl -Xcc -O1
clang-repl> #include "foo.hpp"
Segmentation fault (core dumped)

The backtrace

#0  0x0000555558b9e025 in clang::CodeGen::CodeGenTypes::ConvertRecordDeclType(clang::RecordDecl const*) ()
#1  0x0000555558b9ef2b in clang::CodeGen::CodeGenTypes::getCGRecordLayout(clang::RecordDecl const*) ()
#2  0x0000555558b9ab87 in clang::CodeGen::CodeGenTBAA::CollectFields(unsigned long, clang::QualType, llvm::SmallVectorImpl<llvm::MDBuilder::TBAAStructField>&, bool) ()
#3  0x0000555558b9b358 in clang::CodeGen::CodeGenTBAA::getTBAAStructInfo(clang::QualType) ()
#4  0x00005555589a6853 in clang::CodeGen::CodeGenFunction::EmitAggregateCopy(clang::CodeGen::LValue, clang::CodeGen::LValue, clang::QualType, clang::CodeGen::AggValueSlot::Overlap_t, bool) ()
#5  0x00005555589bc204 in clang::CodeGen::CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr(clang::CallExpr const*, clang::CXXMethodDecl const*, clang::CodeGen::ReturnValueSlot, bool, clang::NestedNameSpecifier*, bool, clang::Expr const*) ()
#6  0x00005555589bd268 in clang::CodeGen::CodeGenFunction::EmitCXXOperatorMemberCallExpr(clang::CXXOperatorCallExpr const*, clang::CXXMethodDecl const*, clang::CodeGen::ReturnValueSlot) ()
#7  0x00005555589955bd in clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) ()
#8  0x0000555558995700 in clang::CodeGen::CodeGenFunction::EmitCallExprLValue(clang::CallExpr const*) ()
#9  0x000055555899597e in clang::CodeGen::CodeGenFunction::EmitLValueHelper(clang::Expr const*, clang::CodeGen::KnownNonNull_t) ()
#10 0x00005555589963d4 in clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*, clang::CodeGen::KnownNonNull_t) ()
#11 0x0000555558995d0d in clang::CodeGen::CodeGenFunction::EmitLValueHelper(clang::Expr const*, clang::CodeGen::KnownNonNull_t) ()
#12 0x0000555558994070 in clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) ()
#13 0x0000555558ab0d92 in clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) ()
#14 0x0000555558ab7ef9 in clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) ()
#15 0x0000555558b14cdc in clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) ()
#16 0x0000555558b2672f in clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) ()
#17 0x0000555558b6a44a in clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) ()
#18 0x0000555558b664bc in clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) ()
#19 0x0000555558b66d5b in clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) ()
#20 0x0000555558b6e1b7 in clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) [clone .part.0] ()
#21 0x0000555558896d81 in (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ()
#22 0x00005555588719e7 in clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) ()
#23 0x000055555834df3f in clang::IncrementalASTConsumer::HandleTopLevelDecl(clang::DeclGroupRef) ()
#24 0x0000555558351424 in clang::IncrementalParser::ParseOrWrapTopLevelDecl() ()
#25 0x000055555835225d in clang::IncrementalParser::Parse(llvm::StringRef) ()
#26 0x0000555558342568 in clang::Interpreter::ParseAndExecute(llvm::StringRef, clang::Value*) ()
#27 0x0000555556333495 in main ()

Environment details

  • OS: Ubuntu 24.04 (distrobox)
  • LLVM commit: 0b3fc78

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:codegenIR generation bugs: mangling, exceptions, etc.crashPrefer [crash-on-valid] or [crash-on-invalid]

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions