Issue 233668
Summary [BPF] optnone skips the exit after __bpf_trap at -O0
Labels new issue
Assignees
Reporter yqtian-se
    At `-O0`, a BPF function ending in `__builtin_trap()` is emitted with a call to
`__bpf_trap` but no following `exit` instruction.

This reproduces with an assertions-enabled LLVM 24.0.0git build at revision
`8a13e7b0bd9b3aa6fa5ae4861e37472192d8db4a` on x86-64 Linux, targeting
`bpfel`.

Save the following as `reproducer.c` and run:

```console
$ clang --target=bpfel -mcpu=v3 -O0 -g0 -S reproducer.c -o -
```

The complete source is:

```c
void f(void) {
 __builtin_trap();
}
```

The end of the generated function is:

```asm
call __bpf_trap
```

There is no `exit`. The result is the same with `-g`. At `-O1` and `-O2`, the
same function ends with:

```asm
call __bpf_trap
exit
```

Clang's `-O0` IR carries the `optnone` attribute. Removing only that attribute
from the same IR makes `llc -O0` add the `exit`. The legacy
`BPFMIPreEmitPeepholeLegacy::runOnMachineFunction()` returns early through
`skipFunction()`, so `addExitAfterUnreachable()` never runs.

This does not depend on debug information, and the program is still intended
to be rejected. However, the exit was added in #131731 so the kernel verifier
could reach the dedicated `unexpected __bpf_trap()` diagnostic. Without it,
the verifier can instead stop at the structural `last insn is not an exit or
jmp` check.

Simply removing `skipFunction()` from the whole peephole would also enable
unrelated optimizations for `optnone` functions. It may be preferable to run
only the required trap-exit finalization for skipped functions, or to place
that finalization in a mandatory pass.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to