Re: [PATCH bpf-next v5 0/3] bpf: Warn with __bpf_trap() kfunc maybe due to uninitialized variable

Eduard Zingerman
2025-05-26T21:49:36Z

Yonghong Song <yonghong.song@linux.dev> writes:

> Marc Suñé (Isovalent, part of Cisco) reported an issue where an
> uninitialized variable caused generating bpf prog binary code not
> working as expected. The reproducer is in [1] where the flags
> “-Wall -Werror” are enabled, but there is no warning as the compiler
> takes advantage of uninitialized variable to do aggressive optimization.
> Such optimization results in a verification log:
> last insn is not an exit or jmp
> User still needs to take quite some time to figure out what is
> the root cause.
>
> To give a better hint to user, __bpf_trap() kfunc is introduced
> in kernel and the compiler ([2]) will encode __bpf_trap()
> as needed. For example, compiler may generate 'unreachable' IR
> after do optimizaiton by taking advantage of uninitialized variable,
> and later bpf backend will translate such 'unreachable' IR to
> __bpf_trap() func in final binary. When kernel detects
> __bpf_trap(), it is able to issue much better verifier log, e.g.
> unexpected __bpf_trap() due to uninitialized variable?
>
> [1] https://github.com/msune/clang_bpf/blob/main/Makefile#L3
> [2] https://github.com/llvm/llvm-project/pull/131731

Combined kernel + llvm changes work as expected.

Tested-by: Eduard Zingerman <eddyz87@gmail.com> …