llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.22k stars 11.65k forks source link

BPF backend crashes when returning 128-bit integers #63036

Open GabrielRavier opened 1 year ago

GabrielRavier commented 1 year ago

The following code:

__int128_t f()
{
    return 0;
}

currently makes trunk Clang crash when using the eBPF backend, with the following error message:

fatal error: error in backend: unable to allocate function return #1
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: /opt/compiler-explorer/clang-trunk/bin/clang++ -g -o /app/output.s -S -target bpf -fcolor-diagnostics -fno-crash-diagnostics -O3 <source>
1.  <eof> parser at end of file
2.  Code generation
3.  Running pass 'Function Pass Manager' on module '<source>'.
4.  Running pass 'BPF DAG->DAG Pattern Instruction Selection' on function '@_Z1fv'
 #0 0x000055b1e113db9f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3616b9f)
 #1 0x000055b1e113bbcc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3614bcc)
 #2 0x000055b1e108b597 llvm::CrashRecoveryContext::HandleExit(int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3564597)
 #3 0x000055b1e11337c2 llvm::sys::Process::Exit(int, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x360c7c2)
 #4 0x000055b1de9c0d86 (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe99d86)
 #5 0x000055b1e109458a llvm::report_fatal_error(llvm::Twine const&, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x356d58a)
 #6 0x000055b1e045d1bf llvm::CCState::AnalyzeReturn(llvm::SmallVectorImpl<llvm::ISD::OutputArg> const&, bool (*)(unsigned int, llvm::MVT, llvm::MVT, llvm::CCValAssign::LocInfo, llvm::ISD::ArgFlagsTy, llvm::CCState&)) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x29361bf)
 #7 0x000055b1df415b24 llvm::BPFTargetLowering::LowerReturn(llvm::SDValue, unsigned int, bool, llvm::SmallVectorImpl<llvm::ISD::OutputArg> const&, llvm::SmallVectorImpl<llvm::SDValue> const&, llvm::SDLoc const&, llvm::SelectionDAG&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x18eeb24)
 #8 0x000055b1e2100f80 llvm::SelectionDAGBuilder::visitRet(llvm::ReturnInst const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x45d9f80)
 #9 0x000055b1e21030f8 llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x45dc0f8)
#10 0x000055b1e21783de llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, bool&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x46513de)
#11 0x000055b1e217968f llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x465268f)
#12 0x000055b1e217be76 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (.part.0) SelectionDAGISel.cpp:0:0
#13 0x000055b1e05b0f10 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#14 0x000055b1e0acfc7a llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2fa8c7a)
#15 0x000055b1e0acfe09 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2fa8e09)
#16 0x000055b1e0ad09f0 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2fa99f0)
#17 0x000055b1e13ae4b0 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x38874b0)
#18 0x000055b1e22bc68c clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x479568c)
#19 0x000055b1e368c139 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5b65139)
#20 0x000055b1e22bb745 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4794745)
#21 0x000055b1e1b9fbb1 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4078bb1)
#22 0x000055b1e1b238b3 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ffc8b3)
#23 0x000055b1e1c7eae3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4157ae3)
#24 0x000055b1de9c230c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe9b30c)
#25 0x000055b1de9bda0f ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#26 0x000055b1e19802dd void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#27 0x000055b1e108b487 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3564487)
#28 0x000055b1e198051c clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#29 0x000055b1e1947f5e clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3e20f5e)
#30 0x000055b1e194892d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3e2192d)
#31 0x000055b1e1952ffc clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3e2bffc)
#32 0x000055b1de9bff0f clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe98f0f)
#33 0x000055b1de8bd255 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xd96255)
#34 0x00007f81a2001083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#35 0x000055b1de9b9cee _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe92cee)
clang++: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Compiler returned: 70

this appears to be caused by LLVM seemingly not knowing how to handle returning 128-bit objects.

inclyc commented 9 months ago

For now the frame pointer is read-only, and we can only return values via registers, which basically means you can only return 1 value within 64-bit R0 register.

Note that the linux kernel documentation explicitly states that BPF does not support multiple returning values.

https://docs.kernel.org/bpf/bpf_design_QA.html#id6

This issue should be consider as "won't fix" or just turn the backend crashing into some elegant error reporting.

GabrielRavier commented 9 months ago

I guessed it was probably an issue with BPF itself but I was reporting in large part because I'd assume anything that makes LLVM go "FATAL ERROR PLEASE REPORT THIS TO GITHUB" is a bug that should be fixed, even if just to make it so it doesn't direct the user to report to GitHub.

GabrielRavier commented 9 months ago

Also, does this imply there's a bug in GCC's BPF backend, given that it'll happily return an __int128_t by stuffing half the return value in r1 ?

inclyc commented 9 months ago

I guessed it was probably an issue with BPF itself but I was reporting in large part because I'd assume anything that makes LLVM go "FATAL ERROR PLEASE REPORT THIS TO GITHUB" is a bug that should be fixed, even if just to make it so it doesn't direct the user to report to GitHub.

Totally agree as I commented above,

turn the backend crashing into some elegant error reporting.