Skip to contentSkip to conversation

selftests: bpf: support building selftests with clang and fix warnings

PATCH Applied Stanislav Fomichev 2 participants 6 messages Apr 2, 2019

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover selftests: bpf: support building selftests with clang and fix warnings

This series contains small fixes to make bpf selftests compile cleanly with clangs. All of them are not real problems, but it's nice to have an option to use clang for the tests themselves.

Stanislav Fomichev (4):
selftests: bpf: tests.h should depend on .c files, not the output
selftests: bpf: fix -Wformat-security warning for
flow_dissector_load.c
selftests: bpf: fix -Wformat-invalid-specifier for bpf_obj_id.c
selftests: bpf: remove duplicate .flags initialization in ctx_skb.c

 tools/testing/selftests/bpf/Makefile                | 4 ++--
 tools/testing/selftests/bpf/flow_dissector_load.c   | 2 +-
 tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c | 8 ++++----
 tools/testing/selftests/bpf/verifier/ctx_skb.c      | 1 -
 4 files changed, 7 insertions(+), 8 deletions(-)
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#1 lore raw

PATCH 1/4 selftests: bpf: tests.h should depend on .c files, not the output

This makes sure we don't put headers as input files when doing compilation, because clang complains about the following:

clang-9: error: cannot specify -o when generating multiple output files ../lib.mk:152: recipe for target 'xxx/tools/testing/selftests/bpf/test_verifier' failed
make: *** [xxx/tools/testing/selftests/bpf/test_verifier] Error 1
make: *** Waiting for unfinished jobs....
clang-9: error: cannot specify -o when generating multiple output files ../lib.mk:152: recipe for target 'xxx/tools/testing/selftests/bpf/test_progs' failed

---
 tools/testing/selftests/bpf/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +2 -2
index 2aed37ea61a4..e7ee6a242146 100644
@@ -206,7 +206,7 @@ ifeq ($(DWARF2BTF),y)
 endif
 
 PROG_TESTS_H := $(OUTPUT)/prog_tests/tests.h
-$(OUTPUT)/test_progs: $(PROG_TESTS_H)
+test_progs.c: $(PROG_TESTS_H)
 $(OUTPUT)/test_progs: CFLAGS += $(TEST_PROGS_CFLAGS)
 $(OUTPUT)/test_progs: prog_tests/*.c
 
@@ -229,7 +229,7 @@ $(PROG_TESTS_H): $(PROG_TESTS_DIR) $(PROG_TESTS_FILES)
 		 ) > $(PROG_TESTS_H))
 
 VERIFIER_TESTS_H := $(OUTPUT)/verifier/tests.h
-$(OUTPUT)/test_verifier: $(VERIFIER_TESTS_H)
+test_verifier.c: $(VERIFIER_TESTS_H)
 $(OUTPUT)/test_verifier: CFLAGS += $(TEST_VERIFIER_CFLAGS)
 
 VERIFIER_TESTS_DIR = $(OUTPUT)/verifier
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#2 lore raw

PATCH 2/4 selftests: bpf: fix -Wformat-security warning for flow_dissector_load.c

In reply to Stanislav Fomichev

flow_dissector_load.c:55:19: warning: format string is not a string literal (potentially insecure)

      [-Wformat-security]
                error(1, errno, command);
                                ^~~~~~~
flow_dissector_load.c:55:19: note: treat the string as an argument to avoid this
                error(1, errno, command);
                                ^
                                "%s",

1 warning generated.

---
 tools/testing/selftests/bpf/flow_dissector_load.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/flow_dissector_load.c +1 -1
index 77cafa66d048..7136ab9ffa73 100644
@@ -52,7 +52,7 @@ static void detach_program(void)
 	sprintf(command, "rm -r %s", cfg_pin_path);
 	ret = system(command);
 	if (ret)
-		error(1, errno, command);
+		error(1, errno, "%s", command);
 }
 
 static void parse_opts(int argc, char **argv)
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#3 lore raw

PATCH 3/4 selftests: bpf: fix -Wformat-invalid-specifier for bpf_obj_id.c

In reply to Stanislav Fomichev

Use standard C99 %zu for sizeof, not GCC's custom %Zu:
bpf_obj_id.c:76:48: warning: invalid conversion specifier 'Z'

---
 tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c +4 -4
index a64f7a02139c..cb827383db4d 100644
@@ -73,7 +73,7 @@ void test_bpf_obj_id(void)
 			  info_len != sizeof(struct bpf_map_info) ||
 			  strcmp((char *)map_infos[i].name, expected_map_name),
 			  "get-map-info(fd)",
-			  "err %d errno %d type %d(%d) info_len %u(%Zu) key_size %u value_size %u max_entries %u map_flags %X name %s(%s)\n",
+			  "err %d errno %d type %d(%d) info_len %u(%zu) key_size %u value_size %u max_entries %u map_flags %X name %s(%s)\n",
 			  err, errno,
 			  map_infos[i].type, BPF_MAP_TYPE_ARRAY,
 			  info_len, sizeof(struct bpf_map_info),
@@ -117,7 +117,7 @@ void test_bpf_obj_id(void)
 			  *(int *)(long)prog_infos[i].map_ids != map_infos[i].id ||
 			  strcmp((char *)prog_infos[i].name, expected_prog_name),
 			  "get-prog-info(fd)",
-			  "err %d errno %d i %d type %d(%d) info_len %u(%Zu) jit_enabled %d jited_prog_len %u xlated_prog_len %u jited_prog %d xlated_prog %d load_time %lu(%lu) uid %u(%u) nr_map_ids %u(%u) map_id %u(%u) name %s(%s)\n",
+			  "err %d errno %d i %d type %d(%d) info_len %u(%zu) jit_enabled %d jited_prog_len %u xlated_prog_len %u jited_prog %d xlated_prog %d load_time %lu(%lu) uid %u(%u) nr_map_ids %u(%u) map_id %u(%u) name %s(%s)\n",
 			  err, errno, i,
 			  prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
 			  info_len, sizeof(struct bpf_prog_info),
@@ -185,7 +185,7 @@ void test_bpf_obj_id(void)
 		      memcmp(&prog_info, &prog_infos[i], info_len) ||
 		      *(int *)(long)prog_info.map_ids != saved_map_id,
 		      "get-prog-info(next_id->fd)",
-		      "err %d errno %d info_len %u(%Zu) memcmp %d map_id %u(%u)\n",
+		      "err %d errno %d info_len %u(%zu) memcmp %d map_id %u(%u)\n",
 		      err, errno, info_len, sizeof(struct bpf_prog_info),
 		      memcmp(&prog_info, &prog_infos[i], info_len),
 		      *(int *)(long)prog_info.map_ids, saved_map_id);
@@ -231,7 +231,7 @@ void test_bpf_obj_id(void)
 		      memcmp(&map_info, &map_infos[i], info_len) ||
 		      array_value != array_magic_value,
 		      "check get-map-info(next_id->fd)",
-		      "err %d errno %d info_len %u(%Zu) memcmp %d array_value %llu(%llu)\n",
+		      "err %d errno %d info_len %u(%zu) memcmp %d array_value %llu(%llu)\n",
 		      err, errno, info_len, sizeof(struct bpf_map_info),
 		      memcmp(&map_info, &map_infos[i], info_len),
 		      array_value, array_magic_value);
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#4 lore raw

PATCH 4/4 selftests: bpf: remove duplicate .flags initialization in ctx_skb.c

In reply to Stanislav Fomichev

verifier/ctx_skb.c:708:11: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
 tools/testing/selftests/bpf/verifier/ctx_skb.c | 1 -
 1 file changed, 1 deletion(-)
tools/testing/selftests/bpf/verifier/ctx_skb.c +0 -1
index c660deb582f1..b0fda2877119 100644
@@ -705,7 +705,6 @@
 	.errstr = "invalid bpf_context access",
 	.result = REJECT,
 	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
-	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"check cb access: half, wrong type",
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
maintainer Applied
#5 lore raw
In reply to Stanislav Fomichev
Quoted 11 lines · Stanislav Fomichev

This series contains small fixes to make bpf selftests compile cleanly with clangs. All of them are not real problems, but it's nice to have an option to use clang for the tests themselves.

Stanislav Fomichev (4):
selftests: bpf: tests.h should depend on .c files, not the output
selftests: bpf: fix -Wformat-security warning for
flow_dissector_load.c
selftests: bpf: fix -Wformat-invalid-specifier for bpf_obj_id.c
selftests: bpf: remove duplicate .flags initialization in ctx_skb.c

Quoted 6 lines
 tools/testing/selftests/bpf/Makefile                | 4 ++--
 tools/testing/selftests/bpf/flow_dissector_load.c   | 2 +-
 tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c | 8 ++++----
 tools/testing/selftests/bpf/verifier/ctx_skb.c      | 1 -
 4 files changed, 7 insertions(+), 8 deletions(-)

Applied, thanks!