BPF List
 help / color / mirror / Atom feed
* [RFC bpf-next v2 0/1] libbpf: add compile-time OOB warning to bpf_tail_call_static
@ 2025-09-05  5:18 Hoyeon Lee
  2025-09-05  5:18 ` [RFC bpf-next v2 1/1] " Hoyeon Lee
  2025-09-05  8:42 ` [RFC bpf-next v2 0/1] " Shung-Hsi Yu
  0 siblings, 2 replies; 7+ messages in thread
From: Hoyeon Lee @ 2025-09-05  5:18 UTC (permalink / raw)
  Cc: netdev, Hoyeon Lee, Andrii Nakryiko, Eduard Zingerman,
	Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
	Hao Luo, Jiri Olsa, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, open list:BPF [LIBRARY] (libbpf),
	open list,
	open list:CLANG/LLVM BUILD SUPPORT:Keyword:b(?i:clang|llvm)b

This RFC adds a compile-time check to bpf_tail_call_static() to warn
when a constant slot(index) is >= map->max_entries. This uses a small
BPF_MAP_ENTRIES() macro together with Clang's diagnose_if attribute.

Clang front-end keeps the map type with a '(*max_entries)[N]' field,
so the expression

    sizeof(*(m)->max_entries) / sizeof(**(m)->max_entries)

is resolved to N entirely at compile time. This allows diagnose_if()
to emit a warning when a constant slot index is out of range.

Example:

    struct { /* BPF_MAP_TYPE_PROG_ARRAY = 3 */
        __uint(type, 3);             // int (*type)[3];
        __uint(max_entries, 100);    // int (*max_entries)[100];
        __type(key, __u32);          // typeof(__u32) *key;
        __type(value, __u32);        // typeof(__u32) *value;
    } progs SEC(".maps");

    bpf_tail_call_static(ctx, &progs, 111);

produces:

    bound.bpf.c:26:9: warning: bpf_tail_call: slot >= max_entries [-Wuser-defined-warnings]
       26 |         bpf_tail_call_static(ctx, &progs, 111);
          |         ^
    /usr/local/include/bpf/bpf_helpers.h:190:54: note: expanded from macro 'bpf_tail_call_static'
      190 |          __bpf_tail_call_warn(__slot >= BPF_MAP_ENTRIES(map));                  \
          |                                                             ^
    /usr/local/include/bpf/bpf_helpers.h:183:20: note: from 'diagnose_if' attribute on '__bpf_tail_call_warn':
      183 |     __attribute__((diagnose_if(oob, "bpf_tail_call: slot >= max_entries", "warning")));
          |                    ^           ~~~

Out-of-bounds tail call checkup is no-ops at runtime. Emitting a
compile-time warning can help developers detect mistakes earlier. The
check is currently limited to Clang (due to diagnose_if) and constant
indices, but should catch common errors.

---
Changes in V2:
- add function definition for __bpf_tail_call_warn for compile error

Hoyeon Lee (1):
  libbpf: add compile-time OOB warning to bpf_tail_call_static

 tools/lib/bpf/bpf_helpers.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

--
2.51.0

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-09-08  4:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-05  5:18 [RFC bpf-next v2 0/1] libbpf: add compile-time OOB warning to bpf_tail_call_static Hoyeon Lee
2025-09-05  5:18 ` [RFC bpf-next v2 1/1] " Hoyeon Lee
2025-09-05 15:54   ` Yonghong Song
2025-09-08  4:52     ` Hoyeon Lee
2025-09-05  8:42 ` [RFC bpf-next v2 0/1] " Shung-Hsi Yu
2025-09-05 18:57   ` Andrii Nakryiko
2025-09-08  4:56     ` Hoyeon Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox