Commit 9d120b41 authored by Jiong Wang's avatar Jiong Wang Committed by Alexei Starovoitov
Browse files

selftests: bpf: enable hi32 randomization for all tests



The previous libbpf patch allows user to specify "prog_flags" to bpf
program load APIs. To enable high 32-bit randomization for a test, we need
to set BPF_F_TEST_RND_HI32 in "prog_flags".

To enable such randomization for all tests, we need to make sure all places
are passing BPF_F_TEST_RND_HI32. Changing them one by one is not
convenient, also, it would be better if a test could be switched to
"normal" running mode without code change.

Given the program load APIs used across bpf selftests are mostly:
  bpf_prog_load:      load from file
  bpf_load_program:   load from raw insns

A test_stub.c is implemented for bpf seltests, it offers two functions for
testing purpose:

  bpf_prog_test_load
  bpf_test_load_program

The are the same as "bpf_prog_load" and "bpf_load_program", except they
also set BPF_F_TEST_RND_HI32. Given *_xattr functions are the APIs to
customize any "prog_flags", it makes little sense to put these two
functions into libbpf.

Then, the following CFLAGS are passed to compilations for host programs:
  -Dbpf_prog_load=bpf_prog_test_load
  -Dbpf_load_program=bpf_test_load_program

They migrate the used load APIs to the test version, hence enable high
32-bit randomization for these tests without changing source code.

Besides all these, there are several testcases are using
"bpf_prog_load_attr" directly, their call sites are updated to pass
BPF_F_TEST_RND_HI32.

Signed-off-by: default avatarJiong Wang <jiong.wang@netronome.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent f3b55abb
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -15,7 +15,9 @@ LLC ?= llc
LLVM_OBJCOPY	?= llvm-objcopy
LLVM_READELF	?= llvm-readelf
BTF_PAHOLE	?= pahole
CFLAGS += -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) -I$(GENDIR) $(GENFLAGS) -I../../../include
CFLAGS += -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) -I$(GENDIR) $(GENFLAGS) -I../../../include \
	  -Dbpf_prog_load=bpf_prog_test_load \
	  -Dbpf_load_program=bpf_test_load_program
LDLIBS += -lcap -lelf -lrt -lpthread

# Order correspond to 'make run_tests' order
@@ -79,9 +81,9 @@ $(OUTPUT)/test_maps: map_tests/*.c

BPFOBJ := $(OUTPUT)/libbpf.a

$(TEST_GEN_PROGS): $(BPFOBJ)
$(TEST_GEN_PROGS): test_stub.o $(BPFOBJ)

$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/libbpf.a
$(TEST_GEN_PROGS_EXTENDED): test_stub.o $(OUTPUT)/libbpf.a

$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
@@ -177,7 +179,7 @@ $(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\
						$(ALU32_BUILD_DIR)/urandom_read
	$(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \
		-o $(ALU32_BUILD_DIR)/test_progs_32 \
		test_progs.c trace_helpers.c prog_tests/*.c \
		test_progs.c test_stub.c trace_helpers.c prog_tests/*.c \
		$(OUTPUT)/libbpf.a $(LDLIBS)

$(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H)
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ static int check_load(const char *file, enum bpf_prog_type type)
	attr.file = file;
	attr.prog_type = type;
	attr.log_level = 4;
	attr.prog_flags = BPF_F_TEST_RND_HI32;
	err = bpf_prog_load_xattr(&attr, &obj, &prog_fd);
	bpf_object__close(obj);
	if (err)
+1 −0
Original line number Diff line number Diff line
@@ -745,6 +745,7 @@ static int load_path(const struct sock_addr_test *test, const char *path)
	attr.file = path;
	attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
	attr.expected_attach_type = test->expected_attach_type;
	attr.prog_flags = BPF_F_TEST_RND_HI32;

	if (bpf_prog_load_xattr(&attr, &obj, &prog_fd)) {
		if (test->expected_result != LOAD_REJECT)
+1 −0
Original line number Diff line number Diff line
@@ -414,6 +414,7 @@ int main(int argc, char **argv)
	struct bpf_prog_load_attr attr = {
		.file = "test_sock_fields_kern.o",
		.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
		.prog_flags = BPF_F_TEST_RND_HI32,
	};
	int cgroup_fd, egress_fd, ingress_fd, err;
	struct bpf_program *ingress_prog;
+1 −0
Original line number Diff line number Diff line
@@ -148,6 +148,7 @@ static int run_test(int cgfd)
	memset(&attr, 0, sizeof(attr));
	attr.file = SOCKET_COOKIE_PROG;
	attr.prog_type = BPF_PROG_TYPE_UNSPEC;
	attr.prog_flags = BPF_F_TEST_RND_HI32;

	err = bpf_prog_load_xattr(&attr, &pobj, &prog_fd);
	if (err) {
Loading