Skip to content

Commit b94936c

Browse files
HoyeonRheeKernel Patches Daemon
authored andcommitted
libbpf: add compile-time OOB warning to bpf_tail_call_static
Add 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. 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. Signed-off-by: Hoyeon Lee <[email protected]>
1 parent af15c58 commit b94936c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tools/lib/bpf/bpf_helpers.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
173173
:: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
174174
: "r0", "r1", "r2", "r3", "r4", "r5");
175175
}
176+
177+
#if __has_attribute(diagnose_if)
178+
static __always_inline void __bpf_tail_call_warn(int oob)
179+
__attribute__((diagnose_if(oob, "bpf_tail_call: slot >= max_entries", "warning")));
180+
181+
#define BPF_MAP_ENTRIES(m) \
182+
((__u32)(sizeof(*(m)->max_entries) / sizeof(**(m)->max_entries)))
183+
184+
#ifndef bpf_tail_call_static
185+
#define bpf_tail_call_static(ctx, map, slot) \
186+
({ \
187+
/* wrapped to avoid double evaluation. */ \
188+
const __u32 __slot = (slot); \
189+
__bpf_tail_call_warn(__slot >= BPF_MAP_ENTRIES(map)); \
190+
/* Avoid re-expand & invoke original as (bpf_tail_call_static)(..) */ \
191+
(bpf_tail_call_static)(ctx, map, __slot); \
192+
})
193+
#endif /* bpf_tail_call_static */
194+
#endif
195+
176196
#endif
177197
#endif
178198

0 commit comments

Comments
 (0)