bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction
@ 2020-08-20  6:14 Andrii Nakryiko
  2020-08-20  6:14 ` [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow Andrii Nakryiko
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Andrii Nakryiko @ 2020-08-20  6:14 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Yonghong Song

BPF_CALL | BPF_JMP32 is explicitly not allowed by verifier for BPF helper
calls, so don't detect it as a valid call. Also drop the check on func_id
pointer, as it's currently always non-null.

Reported-by: Yonghong Song <yhs@fb.com>
Fixes: 109cea5a594f ("libbpf: Sanitize BPF program code for bpf_probe_read_{kernel, user}[_str]")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 77d420c02094..92ca4eb6ba2d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5844,14 +5844,12 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 
 static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
 {
-	__u8 class = BPF_CLASS(insn->code);
-
-	if ((class == BPF_JMP || class == BPF_JMP32) &&
+	if (BPF_CLASS(insn->code) == BPF_JMP &&
 	    BPF_OP(insn->code) == BPF_CALL &&
 	    BPF_SRC(insn->code) == BPF_K &&
-	    insn->src_reg == 0 && insn->dst_reg == 0) {
-		    if (func_id)
-			    *func_id = insn->imm;
+	    insn->src_reg == 0 &&
+	    insn->dst_reg == 0) {
+		    *func_id = insn->imm;
 		    return true;
 	}
 	return false;
-- 
2.24.1


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

* [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow
  2020-08-20  6:14 [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Andrii Nakryiko
@ 2020-08-20  6:14 ` Andrii Nakryiko
  2020-08-20 14:21   ` Yonghong Song
  2020-08-20  6:14 ` [PATCH bpf-next 3/4] selftests/bpf: fix two minor compilation warnings reported by GCC 4.9 Andrii Nakryiko
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Andrii Nakryiko @ 2020-08-20  6:14 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Yonghong Song

GCC compilers older than version 5 don't support __builtin_mul_overflow yet.
Given GCC 4.9 is the minimal supported compiler for building kernel and the
fact that libbpf is a dependency of resolve_btfids, which is dependency of
CONFIG_DEBUG_INFO_BTF=y, this needs to be handled. This patch fixes the issue
by falling back to slower detection of integer overflow in such cases.

Fixes: 029258d7b228 ("libbpf: Remove any use of reallocarray() in libbpf")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf_internal.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index 61dff515a2f0..4d1c366fca2c 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -10,6 +10,7 @@
 #define __LIBBPF_LIBBPF_INTERNAL_H
 
 #include <stdlib.h>
+#include <limits.h>
 
 /* make sure libbpf doesn't use kernel-only integer typedefs */
 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
@@ -77,6 +78,9 @@ do {				\
 #define pr_info(fmt, ...)	__pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
 #define pr_debug(fmt, ...)	__pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
 
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
 /*
  * Re-implement glibc's reallocarray() for libbpf internal-only use.
  * reallocarray(), unfortunately, is not available in all versions of glibc,
@@ -90,8 +94,14 @@ static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
 {
 	size_t total;
 
+#if __has_builtin(__builtin_mul_overflow)
 	if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
 		return NULL;
+#else
+	if (size == 0 || nmemb > ULONG_MAX / size)
+		return NULL;
+	total = nmemb * size;
+#endif
 	return realloc(ptr, total);
 }
 
-- 
2.24.1


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

* [PATCH bpf-next 3/4] selftests/bpf: fix two minor compilation warnings reported by GCC 4.9
  2020-08-20  6:14 [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Andrii Nakryiko
  2020-08-20  6:14 ` [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow Andrii Nakryiko
@ 2020-08-20  6:14 ` Andrii Nakryiko
  2020-08-20 14:21   ` Yonghong Song
  2020-08-20  6:14 ` [PATCH bpf-next 4/4] selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests Andrii Nakryiko
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Andrii Nakryiko @ 2020-08-20  6:14 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Yonghong Song

GCC 4.9 seems to be more strict in some regards. Fix two minor issue it
reported.

Fixes: 1c1052e0140a ("tools/testing/selftests/bpf: Add self-tests for new helper bpf_get_ns_current_pid_tgid.")
Fixes: 2d7824ffd25c ("selftests: bpf: Add test for sk_assign")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/sk_assign.c         | 3 ++-
 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
index 47fa04adc147..d43038d2b9e1 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
@@ -268,6 +268,7 @@ void test_sk_assign(void)
 	int server = -1;
 	int server_map;
 	int self_net;
+	int i;
 
 	self_net = open(NS_SELF, O_RDONLY);
 	if (CHECK_FAIL(self_net < 0)) {
@@ -286,7 +287,7 @@ void test_sk_assign(void)
 		goto cleanup;
 	}
 
-	for (int i = 0; i < ARRAY_SIZE(tests) && !READ_ONCE(stop); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests) && !READ_ONCE(stop); i++) {
 		struct test_sk_cfg *test = &tests[i];
 		const struct sockaddr *addr;
 		const int zero = 0;
diff --git a/tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c b/tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c
index ed253f252cd0..ec53b1ef90d2 100644
--- a/tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c
+++ b/tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c
@@ -156,4 +156,5 @@ int main(int argc, char **argv)
 			bpf_object__close(obj);
 		}
 	}
+	return 0;
 }
-- 
2.24.1


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

* [PATCH bpf-next 4/4] selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests
  2020-08-20  6:14 [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Andrii Nakryiko
  2020-08-20  6:14 ` [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow Andrii Nakryiko
  2020-08-20  6:14 ` [PATCH bpf-next 3/4] selftests/bpf: fix two minor compilation warnings reported by GCC 4.9 Andrii Nakryiko
@ 2020-08-20  6:14 ` Andrii Nakryiko
  2020-08-20 14:22   ` Yonghong Song
  2020-08-20 14:17 ` [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Yonghong Song
  2020-08-20 15:16 ` Daniel Borkmann
  4 siblings, 1 reply; 9+ messages in thread
From: Andrii Nakryiko @ 2020-08-20  6:14 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Yonghong Song,
	Alexei Starovoitov

Record which built-ins are optional and needed for some of recent BPF CO-RE
subtests. Document Clang diff that fixed corner-case issue with
__builtin_btf_type_id().

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/testing/selftests/bpf/README.rst        | 21 +++++++++++++++++++
 .../bpf/progs/test_core_reloc_type_id.c       |  4 +++-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/README.rst b/tools/testing/selftests/bpf/README.rst
index e885d351595f..66acfcf15ff2 100644
--- a/tools/testing/selftests/bpf/README.rst
+++ b/tools/testing/selftests/bpf/README.rst
@@ -43,3 +43,24 @@ This is due to a llvm BPF backend bug. The fix
   https://reviews.llvm.org/D78466
 has been pushed to llvm 10.x release branch and will be
 available in 10.0.1. The fix is available in llvm 11.0.0 trunk.
+
+BPF CO-RE-based tests and Clang version
+=======================================
+
+A set of selftests use BPF target-specific built-ins, which might require
+bleeding-edge Clang versions (Clang 12 nightly at this time).
+
+Few sub-tests of core_reloc test suit (part of test_progs test runner) require
+the following built-ins, listed with corresponding Clang diffs introducing
+them to Clang/LLVM. These sub-tests are going to be skipped if Clang is too
+old to support them, they shouldn't cause build failures or runtime test
+failures:
+
+  - __builtin_btf_type_id() ([0], [1], [2]);
+  - __builtin_preserve_type_info(), __builtin_preserve_enum_value() ([3], [4]).
+
+  [0] https://reviews.llvm.org/D74572
+  [1] https://reviews.llvm.org/D74668
+  [2] https://reviews.llvm.org/D85174
+  [3] https://reviews.llvm.org/D83878
+  [4] https://reviews.llvm.org/D83242
diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_type_id.c b/tools/testing/selftests/bpf/progs/test_core_reloc_type_id.c
index 23e6e6bf276c..22aba3f6e344 100644
--- a/tools/testing/selftests/bpf/progs/test_core_reloc_type_id.c
+++ b/tools/testing/selftests/bpf/progs/test_core_reloc_type_id.c
@@ -75,10 +75,12 @@ int test_core_type_id(void *ctx)
 {
 	/* We use __builtin_btf_type_id() in this tests, but up until the time
 	 * __builtin_preserve_type_info() was added it contained a bug that
-	 * would make this test fail. The bug was fixed with addition of
+	 * would make this test fail. The bug was fixed ([0]) with addition of
 	 * __builtin_preserve_type_info(), though, so that's what we are using
 	 * to detect whether this test has to be executed, however strange
 	 * that might look like.
+	 *
+	 *   [0] https://reviews.llvm.org/D85174
 	 */
 #if __has_builtin(__builtin_preserve_type_info)
 	struct core_reloc_type_id_output *out = (void *)&data.out;
-- 
2.24.1


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

* Re: [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction
  2020-08-20  6:14 [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Andrii Nakryiko
                   ` (2 preceding siblings ...)
  2020-08-20  6:14 ` [PATCH bpf-next 4/4] selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests Andrii Nakryiko
@ 2020-08-20 14:17 ` Yonghong Song
  2020-08-20 15:16 ` Daniel Borkmann
  4 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2020-08-20 14:17 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team



On 8/19/20 11:14 PM, Andrii Nakryiko wrote:
> BPF_CALL | BPF_JMP32 is explicitly not allowed by verifier for BPF helper
> calls, so don't detect it as a valid call. Also drop the check on func_id
> pointer, as it's currently always non-null.
> 
> Reported-by: Yonghong Song <yhs@fb.com>
> Fixes: 109cea5a594f ("libbpf: Sanitize BPF program code for bpf_probe_read_{kernel, user}[_str]")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Acked-by: Yonghong Song <yhs@fb.com>

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

* Re: [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow
  2020-08-20  6:14 ` [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow Andrii Nakryiko
@ 2020-08-20 14:21   ` Yonghong Song
  0 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2020-08-20 14:21 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team



On 8/19/20 11:14 PM, Andrii Nakryiko wrote:
> GCC compilers older than version 5 don't support __builtin_mul_overflow yet.
> Given GCC 4.9 is the minimal supported compiler for building kernel and the
> fact that libbpf is a dependency of resolve_btfids, which is dependency of
> CONFIG_DEBUG_INFO_BTF=y, this needs to be handled. This patch fixes the issue
> by falling back to slower detection of integer overflow in such cases.
> 
> Fixes: 029258d7b228 ("libbpf: Remove any use of reallocarray() in libbpf")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Acked-by: Yonghong Song <yhs@fb.com>

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

* Re: [PATCH bpf-next 3/4] selftests/bpf: fix two minor compilation warnings reported by GCC 4.9
  2020-08-20  6:14 ` [PATCH bpf-next 3/4] selftests/bpf: fix two minor compilation warnings reported by GCC 4.9 Andrii Nakryiko
@ 2020-08-20 14:21   ` Yonghong Song
  0 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2020-08-20 14:21 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team



On 8/19/20 11:14 PM, Andrii Nakryiko wrote:
> GCC 4.9 seems to be more strict in some regards. Fix two minor issue it
> reported.
> 
> Fixes: 1c1052e0140a ("tools/testing/selftests/bpf: Add self-tests for new helper bpf_get_ns_current_pid_tgid.")
> Fixes: 2d7824ffd25c ("selftests: bpf: Add test for sk_assign")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Acked-by: Yonghong Song <yhs@fb.com>

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

* Re: [PATCH bpf-next 4/4] selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests
  2020-08-20  6:14 ` [PATCH bpf-next 4/4] selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests Andrii Nakryiko
@ 2020-08-20 14:22   ` Yonghong Song
  0 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2020-08-20 14:22 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Alexei Starovoitov



On 8/19/20 11:14 PM, Andrii Nakryiko wrote:
> Record which built-ins are optional and needed for some of recent BPF CO-RE
> subtests. Document Clang diff that fixed corner-case issue with
> __builtin_btf_type_id().
> 
> Suggested-by: Alexei Starovoitov <ast@kernel.org>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Acked-by: Yonghong Song <yhs@fb.com>

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

* Re: [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction
  2020-08-20  6:14 [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Andrii Nakryiko
                   ` (3 preceding siblings ...)
  2020-08-20 14:17 ` [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Yonghong Song
@ 2020-08-20 15:16 ` Daniel Borkmann
  4 siblings, 0 replies; 9+ messages in thread
From: Daniel Borkmann @ 2020-08-20 15:16 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, ast
  Cc: andrii.nakryiko, kernel-team, Yonghong Song

On 8/20/20 8:14 AM, Andrii Nakryiko wrote:
> BPF_CALL | BPF_JMP32 is explicitly not allowed by verifier for BPF helper
> calls, so don't detect it as a valid call. Also drop the check on func_id
> pointer, as it's currently always non-null.
> 
> Reported-by: Yonghong Song <yhs@fb.com>
> Fixes: 109cea5a594f ("libbpf: Sanitize BPF program code for bpf_probe_read_{kernel, user}[_str]")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Series applied, thanks!

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

end of thread, other threads:[~2020-08-20 15:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-20  6:14 [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Andrii Nakryiko
2020-08-20  6:14 ` [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow Andrii Nakryiko
2020-08-20 14:21   ` Yonghong Song
2020-08-20  6:14 ` [PATCH bpf-next 3/4] selftests/bpf: fix two minor compilation warnings reported by GCC 4.9 Andrii Nakryiko
2020-08-20 14:21   ` Yonghong Song
2020-08-20  6:14 ` [PATCH bpf-next 4/4] selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests Andrii Nakryiko
2020-08-20 14:22   ` Yonghong Song
2020-08-20 14:17 ` [PATCH bpf-next 1/4] libbpf: fix detection of BPF helper call instruction Yonghong Song
2020-08-20 15:16 ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).