eush77 / clang.mix

Multi-stage compiler-assisted specializer generator built on LLVM
Other
5 stars 0 forks source link

Segmentation fault if compiling in Debug mode #1

Open merge34 opened 4 years ago

merge34 commented 4 years ago

Hello. At the moment I am working on a project about the partial evaluation of CUDA code and I've noticed a possible problem in Clang.Mix. At the beginning I thought it compatibility problem with CUDA, but later I found out that it is much more general.

Scenario

  1. Write some C code with Mix attributes.
  2. Compile it with the following bash command: clang -S -O1 -emit-llvm test.c -g (pay attention to -g)

Problem If we follow the described scenario, we will get Segmentation fault error.

Example Consider the following minimal code example:

void foo(int bar) __stage(1)
{ 
}

__attribute__((mix(foo)))
void *mix_foo(void *context, int bar);

According to LLVM.mix FOSDEM presentation, the code should be valid. But if we compile it with -g (aka Debug mode), we will get Segmentation fault error.

Full error message ``` Stack dump: 0. Program arguments: /usr/local/bin/clang-8 -cc1 -triple x86_64-unknown-linux-gnu -S -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -coverage-notes-file /tmp/tests/llvm_mix_algo/benchmark/test.gcno -resource-dir /usr/local/lib/clang/8.0.0 -internal-isystem /usr/local/include -internal-isystem /usr/local/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /tmp/tests/llvm_mix_algo/benchmark -ferror-limit 19 -fmessage-length 238 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o test.s -x c test.c -faddrsig 1. parser at end of file 2. Per-module optimization passes 3. Running pass 'Multi-Stage Compilation' on module 'test.c'. #0 0x0000559c3ac802ca llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/usr/local/bin/clang-8+0x23002ca) #1 0x0000559c3ac7e834 llvm::sys::RunSignalHandlers() (/usr/local/bin/clang-8+0x22fe834) #2 0x0000559c3ac7e972 SignalHandler(int) (/usr/local/bin/clang-8+0x22fe972) #3 0x00007f657dbb8890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890) #4 0x0000559c3b6df918 llvm::mix::StagedIRBuilder >::stageMetadata(llvm::Metadata*) (/usr/local/bin/clang-8+0x2d5f918) #5 0x0000559c3b6dfdaa llvm::mix::StagedIRBuilder >::stageMDNode(llvm::MDNode*) (/usr/local/bin/clang-8+0x2d5fdaa) #6 0x0000559c3b6dfbbd llvm::mix::StagedIRBuilder >::stageMetadata(llvm::Metadata*) (/usr/local/bin/clang-8+0x2d5fbbd) #7 0x0000559c3b6dfdaa llvm::mix::StagedIRBuilder >::stageMDNode(llvm::MDNode*) (/usr/local/bin/clang-8+0x2d5fdaa) #8 0x0000559c3b6dfbbd llvm::mix::StagedIRBuilder >::stageMetadata(llvm::Metadata*) (/usr/local/bin/clang-8+0x2d5fbbd) #9 0x0000559c3b6dfdaa llvm::mix::StagedIRBuilder >::stageMDNode(llvm::MDNode*) (/usr/local/bin/clang-8+0x2d5fdaa) #10 0x0000559c3b6dfbbd llvm::mix::StagedIRBuilder >::stageMetadata(llvm::Metadata*) (/usr/local/bin/clang-8+0x2d5fbbd) #11 0x0000559c3b6decb9 llvm::mix::StagedIRBuilder >::stage(llvm::Value*) (/usr/local/bin/clang-8+0x2d5ecb9) #12 0x0000559c3b6e17e6 llvm::mix::detail::StagedInstructionBuilder >::visitCallInst(llvm::CallInst&) (/usr/local/bin/clang-8+0x2d617e6) #13 0x0000559c3b6ddfed llvm::InstVisitor >, void>::visit(llvm::Instruction&) (/usr/local/bin/clang-8+0x2d5dfed) #14 0x0000559c3b6de055 llvm::mix::detail::StagedInstructionBuilder >::stage(llvm::Instruction*) (/usr/local/bin/clang-8+0x2d5e055) #15 0x0000559c3b6de2e2 llvm::mix::StagedIRBuilder >::stageInstruction(llvm::Instruction*) (/usr/local/bin/clang-8+0x2d5e2e2) #16 0x0000559c3b6dec7d llvm::mix::StagedIRBuilder >::stage(llvm::Value*) (/usr/local/bin/clang-8+0x2d5ec7d) #17 0x0000559c3b6e6103 (anonymous namespace)::Mix::visitMixIntrinsicInst(llvm::IntrinsicInst&) (/usr/local/bin/clang-8+0x2d66103) #18 0x0000559c3b6e6d16 (anonymous namespace)::Mix::runOnModule(llvm::Module&) (/usr/local/bin/clang-8+0x2d66d16) #19 0x0000559c3a6c4c49 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/usr/local/bin/clang-8+0x1d44c49) #20 0x0000559c3ae86291 (anonymous namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction, std::unique_ptr >) (/usr/local/bin/clang-8+0x2506291) #21 0x0000559c3ae88e7c clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*, clang::BackendAction, clang::BackendOptions const&, std::unique_ptr >) (/usr/local/bin/clang-8+0x2508e7c) #22 0x0000559c3b68d26e clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/usr/local/bin/clang-8+0x2d0d26e) #23 0x0000559c3c0b48d9 clang::ParseAST(clang::Sema&, bool, bool) (/usr/local/bin/clang-8+0x37348d9) #24 0x0000559c3b68c0b7 clang::CodeGenAction::ExecuteAction() (/usr/local/bin/clang-8+0x2d0c0b7) #25 0x0000559c3b304aa6 clang::FrontendAction::Execute() (/usr/local/bin/clang-8+0x2984aa6) #26 0x0000559c3b2c949c clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/local/bin/clang-8+0x294949c) #27 0x0000559c3b3c20f3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/local/bin/clang-8+0x2a420f3) #28 0x0000559c393c4218 cc1_main(llvm::ArrayRef, char const*, void*) (/usr/local/bin/clang-8+0xa44218) #29 0x0000559c3932347e main (/usr/local/bin/clang-8+0x9a347e) #30 0x00007f657cc93b97 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b97) #31 0x0000559c393c146a _start (/usr/local/bin/clang-8+0xa4146a) clang-8: error: unable to execute command: Segmentation fault (core dumped) clang-8: error: clang frontend command failed due to signal (use -v to see invocation) clang version 8.0.0 Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/local/bin clang-8: note: diagnostic msg: PLEASE submit a bug report to and include the crash backtrace, preprocessed source, and associated run script. clang-8: note: diagnostic msg: ******************** PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang-8: note: diagnostic msg: /tmp/test-ae6a88.c clang-8: note: diagnostic msg: /tmp/test-ae6a88.sh clang-8: note: diagnostic msg: ******************** ```
test-ae6a88.c ``` # 1 "" # 1 "test.c" void foo(int bar) __stage(1) { } __attribute__((mix(foo))) void *mix_foo(void *context, int bar); ```
test-ae6a88.sh ``` # Crash reproducer for clang version 8.0.0 # Driver args: "-S" "test.c" "-g" # Original command: "/usr/local/bin/clang-8" "-cc1" "-triple" "x86_64-unknown-linux-gnu" "-S" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "test.c" "-mrelocation-model" "static" "-mthread-model" "posix" "-mdisable-fp-elim" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-fuse-init-array" "-target-cpu" "x86-64" "-dwarf-column-info" "-debug-info-kind=limited" "-dwarf-version=4" "-debugger-tuning=gdb" "-coverage-notes-file" "/tmp/tests/llvm_mix_algo/benchmark/test.gcno" "-resource-dir" "/usr/local/lib/clang/8.0.0" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/usr/local/lib/clang/8.0.0/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-fdebug-compilation-dir" "/tmp/tests/llvm_mix_algo/benchmark" "-ferror-limit" "19" "-fmessage-length" "238" "-fobjc-runtime=gcc" "-fdiagnostics-show-option" "-fcolor-diagnostics" "-o" "test.s" "-x" "c" "test.c" "-faddrsig" "/usr/local/bin/clang-8" "-cc1" "-triple" "x86_64-unknown-linux-gnu" "-S" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "test.c" "-mrelocation-model" "static" "-mthread-model" "posix" "-mdisable-fp-elim" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-fuse-init-array" "-target-cpu" "x86-64" "-dwarf-column-info" "-debug-info-kind=limited" "-dwarf-version=4" "-debugger-tuning=gdb" "-coverage-notes-file" "/tmp/tests/llvm_mix_algo/benchmark/test.gcno" "-ferror-limit" "19" "-fmessage-length" "238" "-fobjc-runtime=gcc" "-fdiagnostics-show-option" "-fcolor-diagnostics" "-x" "c" "test-ae6a88.c" "-faddrsig" ```

Environment description

eush77 commented 4 years ago

Hi,

The limitation that you have discovered was there since the beginning: -g does not work with llvm.mix because I have not added the necessary support for debug metadata.

If I run your example under the Debug build of clang (and btw I do recommend having the debug build lying around for investigating issues like this), it prints the following message before spitting out the stack dump:

Unhandled metadata kind
UNREACHABLE executed at /path/to/llvm/lib/Transforms/Mix/StagedIRBuilder.h:639!

which points to this line in the source code. The metadata in question is !DILocalVariable.

I opened the corresponding issue in llvm.mix repository: eush77/llvm.mix#1