vgvassilev/clad

Segfault on anonymous struct

Closed this issue · 3 comments

clad seems to have problems when applied to a union with an anonymous struct.

I could reproduce it on a minimal example:

#include "clad/Differentiator/Differentiator.h"
#include <iostream>

typedef int Fint;

typedef union Findex {

    struct {Fint j, k, l;};
    Fint dim[3];
}
Findex;

void f(double* x, double* y, int size) {
  Findex p;

  for(p.j = 0; p.j < size; p.j += 1) {
    y[p.j] = 2.0 * x[p.j];
  }
}



int main() {

  auto f_dx = clad::gradient(f);
  f_dx.dump();
}

I called clad with:

clang++ -fplugin=$CLAD_DIR/lib/clad.so -I$CLAD_DIR/include findex_error.cpp -o findex_error.exe

The error output is:

error: no member named '' in 'Findex'
error: no member named '' in 'Findex'
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -dumpdir findex_error.exe- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name findex_error.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/msagebaum/Kaiserslautern/mtu/Programms/trace_suite -resource-dir /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/lib/clang/17 -I /home/msagebaum/Applications/clad/inst/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/backward -internal-isystem /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/14/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/home/msagebaum/Kaiserslautern/mtu/Programms/trace_suite -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -load /home/msagebaum/Applications/clad/inst/lib/clad.so -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/findex_error-35ebb2.o -x c++ findex_error.cpp
1.	<eof> parser at end of file
 #0 0x00000000032807cb llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0x32807cb)
 #1 0x000000000327dfeb SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f556f04fd00 __restore_rt (/lib64/libc.so.6+0x40d00)
 #3 0x00007f556e73f59e clang::Expr::getType() const /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/include/clang/AST/Expr.h:142:37
 #4 0x00007f556eb2c127 clad::utils::BuildMemberExpr(clang::Sema&, clang::Scope*, clang::Expr*, llvm::StringRef) /home/msagebaum/Applications/clad/lib/Differentiator/CladUtils.cpp:336:35
 #5 0x00007f556e8c3493 clad::ReverseModeVisitor::VisitMemberExpr(clang::MemberExpr const*) /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:3310:50
 #6 0x00007f556e8a9bb8 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/include/clang/AST/StmtNodes.inc:1255:160
 #7 0x00007f556e8a7d86 clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) /home/msagebaum/Applications/clad/include/clad/Differentiator/ReverseModeVisitor.h:127:7
 #8 0x00007f556e8becf7 clad::ReverseModeVisitor::VisitBinaryOperator(clang::BinaryOperator const*) /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:2506:20
 #9 0x00007f556e8aaa77 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::VisitBinAssign(clang::BinaryOperator const*) /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/include/clang/AST/StmtVisitor.h:138:218
#10 0x00007f556e8a83a3 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/include/clang/AST/StmtVisitor.h:72:157
#11 0x00007f556e8a7d86 clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) /home/msagebaum/Applications/clad/include/clad/Differentiator/ReverseModeVisitor.h:127:7
#12 0x00007f556e8c1f46 clad::ReverseModeVisitor::DifferentiateSingleStmt(clang::Stmt const*, clang::Expr*) /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:3056:9
#13 0x00007f556e8b5d5d clad::ReverseModeVisitor::VisitForStmt(clang::ForStmt const*) /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:1020:57
#14 0x00007f556e8a8898 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/include/clang/AST/StmtNodes.inc:109:151
#15 0x00007f556e8a7d86 clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) /home/msagebaum/Applications/clad/include/clad/Differentiator/ReverseModeVisitor.h:127:7
#16 0x00007f556e8c1f46 clad::ReverseModeVisitor::DifferentiateSingleStmt(clang::Stmt const*, clang::Expr*) /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:3056:9
#17 0x00007f556e8b3c06 clad::ReverseModeVisitor::VisitCompoundStmt(clang::CompoundStmt const*) /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:684:24
#18 0x00007f556e8a87f0 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/include/clang/AST/StmtNodes.inc:73:166
#19 0x00007f556e8a7d86 clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) /home/msagebaum/Applications/clad/include/clad/Differentiator/ReverseModeVisitor.h:127:7
#20 0x00007f556e8b29cd clad::ReverseModeVisitor::DifferentiateWithClad() /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:519:37
#21 0x00007f556e8b1590 clad::ReverseModeVisitor::Derive() /home/msagebaum/Applications/clad/lib/Differentiator/ReverseModeVisitor.cpp:333:30
#22 0x00007f556e73e5de clad::DerivativeBuilder::Derive(clad::DiffRequest const&) /home/msagebaum/Applications/clad/lib/Differentiator/DerivativeBuilder.cpp:418:24
#23 0x00007f556e713e9a clad::plugin::CladPlugin::ProcessDiffRequest(clad::DiffRequest&) /home/msagebaum/Applications/clad/tools/ClangPlugin.cpp:234:58
#24 0x00007f556e714a8b clad::plugin::CladPlugin::FinalizeTranslationUnit() /home/msagebaum/Applications/clad/tools/ClangPlugin.cpp:450:54
#25 0x00007f556e714b92 clad::plugin::CladPlugin::HandleTranslationUnit(clang::ASTContext&) /home/msagebaum/Applications/clad/tools/ClangPlugin.cpp:466:24
#26 0x0000000003cfb308 clang::MultiplexConsumer::HandleTranslationUnit(clang::ASTContext&) (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0x3cfb308)
#27 0x0000000005163489 clang::ParseAST(clang::Sema&, bool, bool) (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0x5163489)
#28 0x0000000003cbeb81 clang::FrontendAction::Execute() (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0x3cbeb81)
#29 0x0000000003c4a15b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0x3c4a15b)
#30 0x0000000003d7dbeb clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0x3d7dbeb)
#31 0x0000000000b0139d cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0xb0139d)
#32 0x0000000000af9f47 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#33 0x0000000000afd5fa clang_main(int, char**, llvm::ToolContext const&) (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0xafd5fa)
#34 0x00000000009ed831 main (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0x9ed831)
#35 0x00007f556f039088 __libc_start_call_main (/lib64/libc.so.6+0x2a088)
#36 0x00007f556f03914b __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x2a14b)
#37 0x0000000000af93a5 _start (/home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin/clang-17+0xaf93a5)
clang++: error: unable to execute command: Segmentation fault (core dumped)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 17.0.6
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/msagebaum/Applications/llvm-project-llvmorg-17.0.6/install/bin
clang++: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: /tmp/findex_error-a87395.cpp
clang++: note: diagnostic msg: /tmp/findex_error-a87395.sh
clang++: note: diagnostic msg: 

********************

Hello, thank you for reporting this bug.

It is addressed in PR #1152. Your test case has been added to verify the results.

If you have more such use cases in mind, don't hesitate to add them in this issue! We want to improve our test coverage.

Thanks for the fast response. The error is now away and I have to handle some linking issues from my applications. Thank you very much.

Glad to hear it (mostly) works! Let us know if you need anything else.