[all-commits] [llvm/llvm-project] 9d51c8: [BPF] Add exception handling support with .bpf_cle...

4ast via All-commits all-commits at lists.llvm.org
Thu Apr 16 14:12:53 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 9d51c891b7191914952fa783ffd05acb96dafe2d
      https://github.com/llvm/llvm-project/commit/9d51c891b7191914952fa783ffd05acb96dafe2d
  Author: 4ast <alexei.starovoitov at gmail.com>
  Date:   2026-04-16 (Thu, 16 Apr 2026)

  Changed paths:
    M llvm/lib/Target/BPF/BPFAsmPrinter.cpp
    M llvm/lib/Target/BPF/BPFAsmPrinter.h
    M llvm/lib/Target/BPF/BPFISelLowering.h
    A llvm/test/CodeGen/BPF/cleanup-reject-typed-catch.ll
    A llvm/test/CodeGen/BPF/cleanup-section.ll

  Log Message:
  -----------
  [BPF] Add exception handling support with .bpf_cleanup section (#192164)

Add support for invoke/landingpad/resume instructions in the BPF backend
so that Rust programs compiled with panic=unwind can run cleanup code
(Drop implementations) when bpf_throw fires.

Changes:

1. BPFISelLowering: Define exception pointer and selector registers
(both R0) so SelectionDAG can lower landingpad instructions.

2. BPFAsmPrinter::emitFunctionBodyEnd: Emit a .bpf_cleanup section with
a flat table of (begin, end, landing_pad) triples using
R_BPF_64_NODYLD32 relocations.

The .bpf_cleanup section layout (12 bytes per entry):

  u32 begin         // start of the invoke region
  u32 end           // end of the invoke region
  u32 landing_pad   // address of the cleanup block

The invoke region [begin, end) includes argument setup instructions
before the call. The runtime checks begin <= PC < end to find the
matching landing pad.

Landing pad blocks survive optimization because invoke maintains CFG
edges to them throughout codegen, same as every other backend. The
standard .gcc_except_table and .eh_frame are also emitted by the
existing DwarfCFIException handler; libbpf will ignore them.

In runtime:
- bpf_throw() is called (from panic handler)
- Kernel walks the BPF call stack with arch_bpf_stack_walk()
- For each frame, look up current PC in .bpf_cleanup table
- If match found: redirect execution to the cleanup function . cleanup
function runs Drop impls (bpf_free, rcu_read_unlock, etc.) . calls
_Unwind_Resume() which is patched to just 'ret' by the verifier .
bpf_throw() pops frame goes to next
- If no match: go to next frame

Signed-off-by: Alexei Starovoitov <ast at kernel.org>
Co-authored-by: Alexei Starovoitov <ast at kernel.org>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list