Commit fd9c40c5 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'test_progs-asan'

Andrii Nakryiko says:

====================
Add necessary infra to build selftests with ASAN (or any other sanitizer). Fix
a bunch of found memory leaks and other memory access issues.

v1->v2:
  - don't add ASAN flavor, but allow extra flags for build (Alexei);
  - fix few more found issues, which somehow were missed first time.
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 3271e8f3 e4e8f4d0
...@@ -59,7 +59,14 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn, ...@@ -59,7 +59,14 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
void hashmap__clear(struct hashmap *map) void hashmap__clear(struct hashmap *map)
{ {
struct hashmap_entry *cur, *tmp;
int bkt;
hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
free(cur);
}
free(map->buckets); free(map->buckets);
map->buckets = NULL;
map->cap = map->cap_bits = map->sz = 0; map->cap = map->cap_bits = map->sz = 0;
} }
......
...@@ -6934,6 +6934,7 @@ int libbpf_find_vmlinux_btf_id(const char *name, ...@@ -6934,6 +6934,7 @@ int libbpf_find_vmlinux_btf_id(const char *name,
enum bpf_attach_type attach_type) enum bpf_attach_type attach_type)
{ {
struct btf *btf; struct btf *btf;
int err;
btf = libbpf_find_kernel_btf(); btf = libbpf_find_kernel_btf();
if (IS_ERR(btf)) { if (IS_ERR(btf)) {
...@@ -6941,7 +6942,9 @@ int libbpf_find_vmlinux_btf_id(const char *name, ...@@ -6941,7 +6942,9 @@ int libbpf_find_vmlinux_btf_id(const char *name,
return -EINVAL; return -EINVAL;
} }
return __find_vmlinux_btf_id(btf, name, attach_type); err = __find_vmlinux_btf_id(btf, name, attach_type);
btf__free(btf);
return err;
} }
static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
......
...@@ -30,8 +30,6 @@ test_tcpnotify_user ...@@ -30,8 +30,6 @@ test_tcpnotify_user
test_libbpf test_libbpf
test_tcp_check_syncookie_user test_tcp_check_syncookie_user
test_sysctl test_sysctl
test_hashmap
test_btf_dump
test_current_pid_tgid_new_ns test_current_pid_tgid_new_ns
xdping xdping
test_cpp test_cpp
...@@ -39,4 +37,4 @@ test_cpp ...@@ -39,4 +37,4 @@ test_cpp
/no_alu32 /no_alu32
/bpf_gcc /bpf_gcc
/tools /tools
/runqslower
...@@ -20,9 +20,10 @@ CLANG ?= clang ...@@ -20,9 +20,10 @@ CLANG ?= clang
LLC ?= llc LLC ?= llc
LLVM_OBJCOPY ?= llvm-objcopy LLVM_OBJCOPY ?= llvm-objcopy
BPF_GCC ?= $(shell command -v bpf-gcc;) BPF_GCC ?= $(shell command -v bpf-gcc;)
CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) -I$(CURDIR) \ SAN_CFLAGS ?=
-I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) -I$(TOOLSINCDIR) \ CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) $(SAN_CFLAGS) \
-I$(APIDIR) \ -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
-I$(TOOLSINCDIR) -I$(APIDIR) \
-Dbpf_prog_load=bpf_prog_test_load \ -Dbpf_prog_load=bpf_prog_test_load \
-Dbpf_load_program=bpf_test_load_program -Dbpf_load_program=bpf_test_load_program
LDLIBS += -lcap -lelf -lz -lrt -lpthread LDLIBS += -lcap -lelf -lz -lrt -lpthread
...@@ -32,7 +33,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test ...@@ -32,7 +33,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
test_cgroup_storage \ test_cgroup_storage \
test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \
test_progs-no_alu32 \ test_progs-no_alu32 \
test_current_pid_tgid_new_ns test_current_pid_tgid_new_ns
...@@ -324,7 +325,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ ...@@ -324,7 +325,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
$(TRUNNER_BPF_SKELS) \ $(TRUNNER_BPF_SKELS) \
$$(BPFOBJ) | $(TRUNNER_OUTPUT) $$(BPFOBJ) | $(TRUNNER_OUTPUT)
$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
cd $$(@D) && $$(CC) $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F) cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
%.c \ %.c \
......
...@@ -392,7 +392,7 @@ static struct core_reloc_test_case test_cases[] = { ...@@ -392,7 +392,7 @@ static struct core_reloc_test_case test_cases[] = {
.input = STRUCT_TO_CHAR_PTR(core_reloc_existence___minimal) { .input = STRUCT_TO_CHAR_PTR(core_reloc_existence___minimal) {
.a = 42, .a = 42,
}, },
.input_len = sizeof(struct core_reloc_existence), .input_len = sizeof(struct core_reloc_existence___minimal),
.output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) { .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
.a_exists = 1, .a_exists = 1,
.b_exists = 0, .b_exists = 0,
......
...@@ -80,9 +80,6 @@ void test_ns_current_pid_tgid(void) ...@@ -80,9 +80,6 @@ void test_ns_current_pid_tgid(void)
"User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid)) "User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid))
goto cleanup; goto cleanup;
cleanup: cleanup:
if (!link) { bpf_link__destroy(link);
bpf_link__destroy(link);
link = NULL;
}
bpf_object__close(obj); bpf_object__close(obj);
} }
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
#include <test_progs.h> #include <test_progs.h>
#include "bpf/libbpf_internal.h" #include "bpf/libbpf_internal.h"
/* AddressSanitizer sometimes crashes due to data dereference below, due to
* this being mmap()'ed memory. Disable instrumentation with
* no_sanitize_address attribute
*/
__attribute__((no_sanitize_address))
static void on_sample(void *ctx, int cpu, void *data, __u32 size) static void on_sample(void *ctx, int cpu, void *data, __u32 size)
{ {
int cpu_data = *(int *)data, duration = 0; int cpu_data = *(int *)data, duration = 0;
......
...@@ -351,6 +351,7 @@ int extract_build_id(char *build_id, size_t size) ...@@ -351,6 +351,7 @@ int extract_build_id(char *build_id, size_t size)
len = size; len = size;
memcpy(build_id, line, len); memcpy(build_id, line, len);
build_id[len] = '\0'; build_id[len] = '\0';
free(line);
return 0; return 0;
err: err:
fclose(fp); fclose(fp);
...@@ -420,6 +421,18 @@ static int libbpf_print_fn(enum libbpf_print_level level, ...@@ -420,6 +421,18 @@ static int libbpf_print_fn(enum libbpf_print_level level,
return 0; return 0;
} }
static void free_str_set(const struct str_set *set)
{
int i;
if (!set)
return;
for (i = 0; i < set->cnt; i++)
free((void *)set->strs[i]);
free(set->strs);
}
static int parse_str_list(const char *s, struct str_set *set) static int parse_str_list(const char *s, struct str_set *set)
{ {
char *input, *state = NULL, *next, **tmp, **strs = NULL; char *input, *state = NULL, *next, **tmp, **strs = NULL;
...@@ -756,11 +769,11 @@ int main(int argc, char **argv) ...@@ -756,11 +769,11 @@ int main(int argc, char **argv)
fprintf(stdout, "Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n", fprintf(stdout, "Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt); env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt);
free(env.test_selector.blacklist.strs); free_str_set(&env.test_selector.blacklist);
free(env.test_selector.whitelist.strs); free_str_set(&env.test_selector.whitelist);
free(env.test_selector.num_set); free(env.test_selector.num_set);
free(env.subtest_selector.blacklist.strs); free_str_set(&env.subtest_selector.blacklist);
free(env.subtest_selector.whitelist.strs); free_str_set(&env.subtest_selector.whitelist);
free(env.subtest_selector.num_set); free(env.subtest_selector.num_set);
return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS; return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment