Skip to contentSkip to conversation

Fix few compiler warnings in selftests and libbpf

PATCH Applied Andrii Nakryiko 4 participants 7 messages Jul 5, 2022 – Jul 6, 2022

Subsystems (5)
Kselftest BPF BPF selftests libbpf +1 more subsystems
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

cover Fix few compiler warnings in selftests and libbpf

Few small patches fixing compiler warning issues detected by Coverity or by building selftests in -O2 mode.

Andrii Nakryiko (3):
selftests/bpf: fix bogus uninitialized variable warning selftests/bpf: fix few more compiler warnings
libbpf: remove unnecessary usdt_rel_ip assignments

 tools/lib/bpf/usdt.c                                       | 6 ++----
 tools/testing/selftests/bpf/network_helpers.c              | 2 +-
 tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 4 ++--
 tools/testing/selftests/bpf/prog_tests/usdt.c              | 2 +-
 tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c      | 2 +-
 5 files changed, 7 insertions(+), 9 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/3 selftests/bpf: fix bogus uninitialized variable warning

When compiling selftests/bpf in optimized mode (-O2), GCC erroneously complains about uninitialized token variable:

In file included from network_helpers.c:22:
network_helpers.c: In function ‘open_netns’:
test_progs.h:355:22: error: ‘token’ may be used uninitialized [-Werror=maybe-uninitialized]

    355 |         int ___err = libbpf_get_error(___res);                          \
        |                      ^~~~~~~~~~~~~~~~~~~~~~~~
  network_helpers.c:440:14: note: in expansion of macro ‘ASSERT_OK_PTR’
    440 |         if (!ASSERT_OK_PTR(token, "malloc token"))
        |              ^~~~~~~~~~~~~
  In file included from /data/users/andriin/linux/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:21,
                   from bpf_util.h:9,
                   from network_helpers.c:20:
  /data/users/andriin/linux/tools/testing/selftests/bpf/tools/include/bpf/libbpf_legacy.h:113:17: note: by argument 1 of type ‘const void *’ to ‘libbpf_get_error’ declared here
    113 | LIBBPF_API long libbpf_get_error(const void *ptr);
        |                 ^~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make: *** [Makefile:522: /data/users/andriin/linux/tools/testing/selftests/bpf/network_helpers.o] Error 1

This is completely bogus becuase libbpf_get_error() doesn't dereference pointer, but the only easy way to silence this is to allocate initialized memory with calloc().

---
 tools/testing/selftests/bpf/network_helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/network_helpers.c +1 -1
index 59cf81ec55af..bec15558fd93 100644
@@ -436,7 +436,7 @@ struct nstoken *open_netns(const char *name)
 	int err;
 	struct nstoken *token;
 
-	token = malloc(sizeof(struct nstoken));
+	token = calloc(1, sizeof(struct nstoken));
 	if (!ASSERT_OK_PTR(token, "malloc token"))
 		return NULL;
 
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/3 selftests/bpf: fix few more compiler warnings

In reply to Andrii Nakryiko

When compiling with -O2, GCC detects few problems with selftests/bpf, so fix all of them. Two are real issues (uninitialized err and nums out-of-bounds access), but two other uninitialized variables warnings are due to GCC not being able to prove that variables are indeed initialized under conditions under which they are used.

Fix all 4 cases, though.

---
 tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 4 ++--
 tools/testing/selftests/bpf/prog_tests/usdt.c              | 2 +-
 tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c      | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +2 -2
index 586dc52d6fb9..a8cb8a96ddaf 100644
@@ -329,7 +329,7 @@ static int get_syms(char ***symsp, size_t *cntp)
 	struct hashmap *map;
 	char buf[256];
 	FILE *f;
-	int err;
+	int err = 0;
 
 	/*
 	 * The available_filter_functions contains many duplicates,
@@ -404,7 +404,7 @@ static void test_bench_attach(void)
 	double attach_delta, detach_delta;
 	struct bpf_link *link = NULL;
 	char **syms = NULL;
-	size_t cnt, i;
+	size_t cnt = 0, i;
 
 	if (!ASSERT_OK(get_syms(&syms, &cnt), "get_syms"))
 		return;
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 5f733d50b0d7..9ad9da0f215e 100644
@@ -12,7 +12,7 @@ int lets_test_this(int);
 
 static volatile int idx = 2;
 static volatile __u64 bla = 0xFEDCBA9876543210ULL;
-static volatile short nums[] = {-1, -2, -3, };
+static volatile short nums[] = {-1, -2, -3, -4};
 
 static volatile struct {
 	int x;
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c +1 -1
index fb77a123fe89..874a846e298c 100644
@@ -63,7 +63,7 @@ static bool expect_str(char *buf, size_t size, const char *str, const char *name
 static void test_synproxy(bool xdp)
 {
 	int server_fd = -1, client_fd = -1, accept_fd = -1;
-	char *prog_id, *prog_id_end;
+	char *prog_id = NULL, *prog_id_end;
 	struct nstoken *ns = NULL;
 	FILE *ctrl_file = NULL;
 	char buf[CMD_OUT_BUF_SIZE];
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH 3/3 libbpf: remove unnecessary usdt_rel_ip assignments

In reply to Andrii Nakryiko

Coverity detected that usdt_rel_ip is unconditionally overwritten anyways, so there is no need to unnecessarily initialize it with unused value. Clean this up.

---
 tools/lib/bpf/usdt.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/lib/bpf/usdt.c +2 -4
index 5159207cbfd9..d18e37982344 100644
@@ -652,11 +652,9 @@ static int collect_usdt_targets(struct usdt_manager *man, Elf *elf, const char *
 		 *
 		 *   [0] https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation
 		 */
-		usdt_rel_ip = usdt_abs_ip = note.loc_addr;
-		if (base_addr) {
+		usdt_abs_ip = note.loc_addr;
+		if (base_addr)
 			usdt_abs_ip += base_addr - note.base_addr;
-			usdt_rel_ip += base_addr - note.base_addr;
-		}
 
 		/* When attaching uprobes (which is what USDTs basically are)
 		 * kernel expects file offset to be specified, not a relative
Signature · 1 line
2.30.2
Acked-by on the v1 series
#4 lore raw
In reply to Andrii Nakryiko
Quoted 8 lines · Andrii Nakryiko

Few small patches fixing compiler warning issues detected by Coverity or by building selftests in -O2 mode.

Andrii Nakryiko (3):
selftests/bpf: fix bogus uninitialized variable warning selftests/bpf: fix few more compiler warnings
libbpf: remove unnecessary usdt_rel_ip assignments

Quoted 7 lines
  tools/lib/bpf/usdt.c                                       | 6 ++----
  tools/testing/selftests/bpf/network_helpers.c              | 2 +-
  tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 4 ++--
  tools/testing/selftests/bpf/prog_tests/usdt.c              | 2 +-
  tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c      | 2 +-
  5 files changed, 7 insertions(+), 9 deletions(-)

Ack for the whole series:

Acked-by on the v1 series
#5 lore raw
In reply to Andrii Nakryiko
Quoted 8 lines · Andrii Nakryiko

Few small patches fixing compiler warning issues detected by Coverity or by building selftests in -O2 mode.

Andrii Nakryiko (3):
selftests/bpf: fix bogus uninitialized variable warning selftests/bpf: fix few more compiler warnings
libbpf: remove unnecessary usdt_rel_ip assignments

Quoted 6 lines
 tools/lib/bpf/usdt.c                                       | 6 ++----
 tools/testing/selftests/bpf/network_helpers.c              | 2 +-
 tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 4 ++--
 tools/testing/selftests/bpf/prog_tests/usdt.c              | 2 +-
 tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c      | 2 +-
 5 files changed, 7 insertions(+), 9 deletions(-)

jirka

#6 lore raw
In reply to Andrii Nakryiko
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 9 lines · On Tue, 5 Jul 2022 15:48:15 -0700 you wrote:

Few small patches fixing compiler warning issues detected by Coverity or by building selftests in -O2 mode.

Andrii Nakryiko (3):
selftests/bpf: fix bogus uninitialized variable warning selftests/bpf: fix few more compiler warnings
libbpf: remove unnecessary usdt_rel_ip assignments

[...]

Here is the summary with links:
- [bpf-next,1/3] selftests/bpf: fix bogus uninitialized variable warning

    https://git.kernel.org/bpf/bpf-next/c/645d5d3bc001
  - [bpf-next,2/3] selftests/bpf: fix few more compiler warnings
    https://git.kernel.org/bpf/bpf-next/c/c46a12200114
  - [bpf-next,3/3] libbpf: remove unnecessary usdt_rel_ip assignments
    https://git.kernel.org/bpf/bpf-next/c/7c8121af1bfe

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html