Skip to content

[BPF] __bpf_trap declaration emitted even if not called #165696

@nikic

Description

@nikic

For this IR:

target triple = "bpf"
    
define i32 @test(i8 %x) {  
entry:
  %0 = and i8 %x, 3
  switch i8 %0, label %default.unreachable4 [
    i8 0, label %return
    i8 1, label %sw.bb1
    i8 2, label %sw.bb2
    i8 3, label %sw.bb3
  ]

sw.bb1:                                           ; preds = %entry
  br label %return

sw.bb2:                                           ; preds = %entry
  br label %return

sw.bb3:                                           ; preds = %entry
  br label %return

default.unreachable4:                             ; preds = %entry
  unreachable

return:                                           ; preds = %entry, %sw.bb3, %sw.bb2, %sw.bb1
  %retval.0 = phi i32 [ 12, %sw.bb1 ], [ 43, %sw.bb2 ], [ 54, %sw.bb3 ], [ 32, %entry ]
  ret i32 %retval.0
}

llc produces:

	.file	"<stdin>"
	.text
	.globl	test                            # -- Begin function test
	.p2align	3
	.type	test,@function
test:                                   # @test
	.cfi_startproc
# %bb.0:                                # %entry
	w1 &= 3
	if w1 s> 1 goto LBB0_3
# %bb.1:                                # %entry
	w0 = 32
	if w1 == 0 goto LBB0_6
# %bb.2:                                # %entry
	w0 = 12
	goto LBB0_6
LBB0_3:                                 # %entry
	if w1 == 2 goto LBB0_5
# %bb.4:                                # %entry
	w0 = 54
	goto LBB0_6
LBB0_5:                                 # %sw.bb2
	w0 = 43
LBB0_6:                                 # %return
	exit
.Lfunc_end0:
	.size	test, .Lfunc_end0-test
	.cfi_endproc
                                        # -- End function
	.weak	__bpf_trap

Note that it declares .weak __bpf_trap, even though the function is never called. This results in a libbpf: extern weak function __bpf_trap is unsupported error on kernels that don't have support for __bpf_trap. On kernels that do have it, there is no error because there is no call.

The reason is that the default.unreachable4 gets lowered to a call to __bpf_trap originally and then gets eliminated as an unused block.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions