From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E86531D73F for ; Fri, 5 Sep 2025 15:55:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757087720; cv=none; b=Iv80Ez8gL7e9z1Z1J7mrlNm+0qpPQctTaRS0HOy5+7wZyVmYn9wb8KBFHAvtHIsmuFbNmf0QTtZYRmJVNQRb+MOyZ7w5IVqMGC1d6rgDUH3iSmMSjvBszRXIlgDPLzBiYCrqdHn3q94FwZK/Z9Pfl/MHX7oaVgxGqYrRR/hjdwc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757087720; c=relaxed/simple; bh=VK5L9ZQl5SjCnEBuOT7vVdJruIk2fkLuhS+o/xPYcMw=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=PdUgFLcPC1KrHy+gc6U3LzUKDXxzqz27LEFHZWZ7FoYgUjWK6oPsRuo/QqQLvc7twztnBri3xQ6lEoPMoaGxR85Z48xj4pYckcx8Ex4uoPoYCQRuicrOqjcGrsdni8RT+Y2RQGDrp57cdsaP/UvQ/7i4ymCL3fvI+Q0V2qJZyM0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Cu/U+6Ps; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Cu/U+6Ps" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1757087706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5SkhDx01VH5ULkTtGZmBjnS7ayJVpHz3ZygQmDSZ2dE=; b=Cu/U+6PsLuRwZnWv3E3Wzw7kScSJJHds5FQyisY7hZ2cFZhr1wFebmDS00NgIUvDISC20c jqLR1yuKPB/o4rskbDHmRuCW9PQ1W88xlz4xNKj8JykWG/iTU2llI7HZIojAN8dn8KU1kW vgZ9r3FJPxIJrxzu629/XpqNrIiGBRU= Date: Fri, 5 Sep 2025 08:54:45 -0700 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [RFC bpf-next v2 1/1] libbpf: add compile-time OOB warning to bpf_tail_call_static Content-Language: en-GB To: Hoyeon Lee Cc: netdev@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , 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" References: <20250905051814.291254-1-hoyeon.lee@suse.com> <20250905051814.291254-2-hoyeon.lee@suse.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Yonghong Song In-Reply-To: <20250905051814.291254-2-hoyeon.lee@suse.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 9/4/25 10:18 PM, Hoyeon Lee wrote: > Add a compile-time check to bpf_tail_call_static() to warn when a > constant slot(index) >= 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 calls are currently silent no-ops at runtime, so > emitting a compile-time warning helps detect logic errors earlier. > This is currently limited to Clang (due to diagnose_if) and only for > constant indices, but should still catch the common cases. > > Signed-off-by: Hoyeon Lee > --- > Changes in V2: > - add function definition for __bpf_tail_call_warn for compile error > > tools/lib/bpf/bpf_helpers.h | 21 +++++++++++++++++++++ > 1 file changed, 21 insertions(+) > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index 80c028540656..98bc1536c497 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -173,6 +173,27 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot) > :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot) > : "r0", "r1", "r2", "r3", "r4", "r5"); > } > + > +#if __has_attribute(diagnose_if) > +static __always_inline void __bpf_tail_call_warn(int oob) > + __attribute__((diagnose_if(oob, "bpf_tail_call: slot >= max_entries", > + "warning"))) {}; > + > +#define BPF_MAP_ENTRIES(m) \ > + ((__u32)(sizeof(*(m)->max_entries) / sizeof(**(m)->max_entries))) > + > +#ifndef bpf_tail_call_static > +#define bpf_tail_call_static(ctx, map, slot) \ > +({ \ > + /* wrapped to avoid double evaluation. */ \ > + const __u32 __slot = (slot); \ > + __bpf_tail_call_warn(__slot >= BPF_MAP_ENTRIES(map)); \ > + /* Avoid re-expand & invoke original as (bpf_tail_call_static)(..) */ \ > + (bpf_tail_call_static)(ctx, map, __slot); \ > +}) > +#endif /* bpf_tail_call_static */ > +#endif I got the following error with llvm21. progs/tailcall_bpf2bpf3.c:20:3: error: bpf_tail_call: slot >= max_entries [-Werror,-Wuser-defined-warnings] 20 | bpf_tail_call_static(skb, &jmp_table,progs/tailcall_bpf2bpf2.c:17:3 10); | : ^ /home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:190:53: note: expanded from macro 'bpf_tail_call_static' 190 | __bpf_tail_call_warn(__slot >= BPF_MAP_ENTRIES(map)); \ | ^ /home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:179:17: note: from 'diagnose_if' attribute on '__bpf_tail_call_warn': 179 | __attribute__((diagnose_if(oob, "bpf_tail_call: slot >= max_entries", | ^ ~~~ error: bpf_tail_call: slot >= max_entries [-Werror,-Wuser-defined-warnings] 17 | bpf_tail_call_static(skb, &jmp_table, 1); | ^ /home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:190:53: note: expanded from macro 'bpf_tail_call_static' 190 | __bpf_tail_call_warn(__slot >= BPF_MAP_ENTRIES(map)); \ | ^ /home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:179:17: note: from 'diagnose_if' attribute on '__bpf_tail_call_warn': 179 | __attribute__((diagnose_if(oob, "bpf_tail_call: slot >= max_entries", | ^ ~~~ CLNG-BPF [test_progs] tailcall_poke.bpf.o 1 error generated. make: *** [Makefile:733: /home/yhs/work/bpf-next/tools/testing/selftests/bpf/tailcall_bpf2bpf3.bpf.o] Error 1 > + > #endif > #endif > > -- > 2.51.0