llvm / llvm-project

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

Crash when compiling ebpf program #102121

Open amitschendel opened 2 months ago

amitschendel commented 2 months ago
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

SEC("xdp")
int trigger_crash(struct xdp_md *ctx)
{
    unsigned int key = 0;
    for (int i = 0; i < 100; i++) {
        __sync_fetch_and_add(&key, 0);
    }
    return XDP_PASS;
}

char _license[] SEC("license") = "GPL";
clang -O2 -target bpf -c -g ntp.bpf.c -o program.o
ntp.bpf.c:96:9: error: unsupported atomic operation, please use 64 bit version
   96 |         __sync_fetch_and_add(&key, 0);
      |         ^
fatal error: error in backend: Cannot select: 0x603f94fd5ce0: i64,ch = AtomicLoadOr<(load store seq_cst (s32) on %ir.2)> 0x603f94f21380, FrameIndex:i64<0>, Constant:i64<0>, ntp.bpf.c:96:9
  0x603f94fd55e0: i64 = FrameIndex<0>
  0x603f94fd5c70: i64 = Constant<0>
In function: trigger_crash
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: clang -O2 -target bpf -c -g ntp.bpf.c -o program.o
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'Function Pass Manager' on module 'ntp.bpf.c'.
4.      Running pass 'BPF DAG->DAG Pattern Instruction Selection' on function '@trigger_crash'
 #0 0x00007826985a667f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xda667f)
 #1 0x00007826985a47b9 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xda47b9)
 #2 0x00007826984f02b3 (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xcf02b3)
 #3 0x00007826984f0262 (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xcf0262)
 #4 0x00007826985a0f30 llvm::sys::Process::Exit(int, bool) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xda0f30)
 #5 0x0000603f939a5f1b (/usr/lib/llvm-18/bin/clang+0x14f1b)
 #6 0x00007826984fe2dc llvm::report_fatal_error(llvm::Twine const&, bool) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xcfe2dc)
 #7 0x0000782698e2f453 (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x162f453)
 #8 0x0000782698e2e8e6 llvm::SelectionDAGISel::SelectCodeCommon(llvm::SDNode*, unsigned char const*, unsigned int) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x162e8e6)
 #9 0x000078269ab5d618 (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x335d618)
#10 0x0000782698e25c8f llvm::SelectionDAGISel::DoInstructionSelection() (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x1625c8f)
#11 0x0000782698e25300 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x1625300)
#12 0x0000782698e2444e llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x162444e)
#13 0x0000782698e224a4 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x16224a4)
#14 0x0000782698982639 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0x1182639)
#15 0x00007826986f7992 llvm::FPPassManager::runOnFunction(llvm::Function&) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xef7992)
#16 0x00007826986fd514 llvm::FPPassManager::runOnModule(llvm::Module&) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xefd514)
#17 0x00007826986f80bf llvm::legacy::PassManagerImpl::run(llvm::Module&) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xef80bf)
#18 0x00007826a0a17940 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>>, clang::BackendConsumer*) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x1c17940)
#19 0x00007826a0da0017 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x1fa0017)
#20 0x000078269f997a06 clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0xb97a06)
#21 0x00007826a1806bec clang::FrontendAction::Execute() (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x2a06bec)
#22 0x00007826a1783674 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x2983674)
#23 0x00007826a1882bfd clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x2a82bfd)
#24 0x0000603f939a5a2e cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-18/bin/clang+0x14a2e)
#25 0x0000603f939a2e94 (/usr/lib/llvm-18/bin/clang+0x11e94)
#26 0x00007826a1433f42 (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x2633f42)
#27 0x00007826984f0237 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xcf0237)
#28 0x00007826a1433807 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x2633807)
#29 0x00007826a13fbae8 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x25fbae8)
#30 0x00007826a13fbd4f clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x25fbd4f)
#31 0x00007826a14181f0 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18.1+0x26181f0)
#32 0x0000603f939a27ec clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm-18/bin/clang+0x117ec)
#33 0x0000603f939af973 main (/usr/lib/llvm-18/bin/clang+0x1e973)
#34 0x000078269702a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#35 0x000078269702a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#36 0x0000603f9399f855 _start (/usr/lib/llvm-18/bin/clang+0xe855)
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Ubuntu clang version 18.1.3 (1)
Target: bpf
Thread model: posix
InstalledDir: /usr/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/ntp-539709.c
clang: note: diagnostic msg: /tmp/ntp-539709.sh
clang: note: diagnostic msg: 

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

artifacts.zip

yonghong-song commented 3 weeks ago

This can be explained in IR: for __sync_fetch_and_add(&key, 0), the related IR is %0 = atomicrmw or ptr %key, i32 0 seq_cst, align 4 unfortunately 32bit 'atomicrmw or' is only supported at -mcpu=v3, so you got an error above.

If you have __sync_fetch_and_add(&key, 1), the IR will be %0 = atomicrmw add ptr %key, i32 1 seq_cst, align 4 everything will be okay.