From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (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 5DA70149C6F for ; Fri, 5 Sep 2025 04:01:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757044867; cv=none; b=p91K0GgMbqm3rDnKlsh4b5lt48ZSPecpFYVH2LfnfDL9lALmvoL5BYuv29H3GlHA1t9PoAEEodgF3cBtj4luckCkZ5rnXvKnrhJaJyuO3ZfYioFJzWEl8Qv0ggCiQSgpEH0BQ7LDBHLb0oVqZrDk03GD8LWIIl2Qwd81i+3LCwI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757044867; c=relaxed/simple; bh=rLaqlph0538ajH36AIEbPc/SPTncMLcESZo0Bgf6XgU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=UBkX7N7RN922KHMHLJ1oXtVAwYIxUZCsm0796EuJ6WXXSGn9qFAshz8hcnCJWolC06YKawBM+MGrXa9+67udt6gcamXMs/CCbyeojkd2C9Gqq1Z31EGnfsaUiE6bT5NQJjLBCDL+TO0frn+DqcwB/cIkoW7t3ByuTr8zxXNIGuU= 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=r4/2gz2F; arc=none smtp.client-ip=95.215.58.182 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="r4/2gz2F" Message-ID: <6b52caf8-51f0-45ed-8f59-11f24dd2861e@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1757044852; 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=xU4WiHHqDbAwz19vt8XJQJ1JAgeIb0EHNdRyJYIcNnw=; b=r4/2gz2FDcv4CZMR9YY/B2P6q/X5hJ1+gOJZfwzKQAE/DHa/BQd7plMzDAumvl6o9piLwI vz5dSpNo1DHizUdQaJIXDY9mppn/bavc1UjkTm77TBq+TIcMDxqx/3clpOMtC3Eou/N8G7 JTN/ZOuBtsYTNxZBtK0zXZO2mr5Sil8= Date: Thu, 4 Sep 2025 21:00:41 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [RFC bpf-next 1/1] libbpf: add compile-time OOB warning to bpf_tail_call_static Content-Language: en-GB To: Hoyeon Lee Cc: 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: <20250905025314.245650-1-hoyeon.lee@suse.com> <20250905025314.245650-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: <20250905025314.245650-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 7:53 PM, Hoyeon Lee wrote: > 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 > --- > tools/lib/bpf/bpf_helpers.h | 20 ++++++++++++++++++++ > 1 file changed, 20 insertions(+) > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index 80c028540656..0d9551bb90c0 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -173,6 +173,26 @@ 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 > + > #endif > #endif I got compilation failures with llvm21. In file included from progs/bpf_flow.c:19: /home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:178:29: error: function '__bpf_tail_call_warn' has internal linkage but is not defined [-Werror,-Wundefined-internal] 178 | static __always_inline void __bpf_tail_call_warn(int oob) | CLNG-BPF [test_progs] btf_type_tag_percpu.bpf.o ^ progs/bpf_flow.c:122:3: note: used here 122 | bpf_tail_call_static(skb, &jmp_table, IP); | ^ /home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:189:2: note: expanded from macro 'bpf_tail_call_static' 189 | __bpf_tail_c CLNG-BPF [test_progs] btf_type_tag_user.bpf.o all_warn(__slot >= BPF_MAP_ENTRIES(map)); CLNG-BPF [test_progs] cb_refs.bpf.o \ | ^ > > -- > 2.51.0