mirror of
https://github.com/torvalds/linux.git
synced 2026-09-22 04:34:03 +02:00
selftests/bpf: Add ASAN support for libarena selftests
Expand the arena library selftest infrastructure to support address sanitization. Add the compiler flags necessary to compile the library under ASAN when supported. Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com> Link: https://lore.kernel.org/r/20260426190338.4615-6-emil@etsalapatis.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
9ab78691eb
commit
cfc00618b9
|
|
@ -79,6 +79,12 @@ ifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),)
|
||||||
CLANG_CPUV4 := 1
|
CLANG_CPUV4 := 1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# Check whether clang supports BPF address sanitizer (requires LLVM 22+)
|
||||||
|
CLANG_HAS_ARENA_ASAN := $(shell echo 'int x;' | \
|
||||||
|
$(CLANG) --target=bpf -fsanitize=kernel-address \
|
||||||
|
-mllvm -asan-shadow-addr-space=1 \
|
||||||
|
-x c -c - -o /dev/null 2>/dev/null && echo 1)
|
||||||
|
|
||||||
# Order correspond to 'make run_tests' order
|
# Order correspond to 'make run_tests' order
|
||||||
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_progs \
|
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_progs \
|
||||||
test_sockmap \
|
test_sockmap \
|
||||||
|
|
@ -764,6 +770,14 @@ LIBARENA_SKEL := libarena/libarena.skel.h
|
||||||
$(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)
|
$(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)
|
||||||
+$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS)
|
+$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS)
|
||||||
|
|
||||||
|
ifneq ($(CLANG_HAS_ARENA_ASAN),)
|
||||||
|
LIBARENA_ASAN_SKEL := libarena/libarena_asan.skel.h
|
||||||
|
CFLAGS += -DHAS_BPF_ARENA_ASAN
|
||||||
|
|
||||||
|
$(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)
|
||||||
|
+$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS)
|
||||||
|
endif
|
||||||
|
|
||||||
# Define test_progs test runner.
|
# Define test_progs test runner.
|
||||||
TRUNNER_TESTS_DIR := prog_tests
|
TRUNNER_TESTS_DIR := prog_tests
|
||||||
TRUNNER_BPF_PROGS_DIR := progs
|
TRUNNER_BPF_PROGS_DIR := progs
|
||||||
|
|
@ -788,7 +802,9 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
|
||||||
flow_dissector_load.h \
|
flow_dissector_load.h \
|
||||||
ip_check_defrag_frags.h \
|
ip_check_defrag_frags.h \
|
||||||
bpftool_helpers.c \
|
bpftool_helpers.c \
|
||||||
usdt_1.c usdt_2.c
|
usdt_1.c usdt_2.c \
|
||||||
|
$(LIBARENA_SKEL) \
|
||||||
|
$(LIBARENA_ASAN_SKEL)
|
||||||
TRUNNER_LIB_SOURCES := find_bit.c
|
TRUNNER_LIB_SOURCES := find_bit.c
|
||||||
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
|
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
|
||||||
$(OUTPUT)/liburandom_read.so \
|
$(OUTPUT)/liburandom_read.so \
|
||||||
|
|
@ -959,3 +975,7 @@ override define INSTALL_RULE
|
||||||
endef
|
endef
|
||||||
|
|
||||||
libarena: $(LIBARENA_SKEL)
|
libarena: $(LIBARENA_SKEL)
|
||||||
|
|
||||||
|
ifneq ($(CLANG_HAS_ARENA_ASAN),)
|
||||||
|
libarena_asan: $(LIBARENA_ASAN_SKEL)
|
||||||
|
endif
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ LIBBPF_INCLUDE ?= $(INCLUDE_DIR)
|
||||||
# Scan src/ and selftests/ to generate the final binaries
|
# Scan src/ and selftests/ to generate the final binaries
|
||||||
LIBARENA_SOURCES = $(wildcard $(LIBARENA)/src/*.bpf.c) $(wildcard $(LIBARENA)/selftests/*.bpf.c)
|
LIBARENA_SOURCES = $(wildcard $(LIBARENA)/src/*.bpf.c) $(wildcard $(LIBARENA)/selftests/*.bpf.c)
|
||||||
LIBARENA_OBJECTS = $(notdir $(LIBARENA_SOURCES:.bpf.c=.bpf.o))
|
LIBARENA_OBJECTS = $(notdir $(LIBARENA_SOURCES:.bpf.c=.bpf.o))
|
||||||
|
LIBARENA_OBJECTS_ASAN = $(notdir $(LIBARENA_SOURCES:.bpf.c=_asan.bpf.o))
|
||||||
|
|
||||||
INCLUDES = -I$(LIBARENA)/include -I$(BPFDIR)
|
INCLUDES = -I$(LIBARENA)/include -I$(BPFDIR)
|
||||||
ifneq ($(INCLUDE_DIR),)
|
ifneq ($(INCLUDE_DIR),)
|
||||||
|
|
@ -39,6 +40,13 @@ ifneq ($(LIBBPF_INCLUDE),)
|
||||||
INCLUDES += -I$(LIBBPF_INCLUDE)
|
INCLUDES += -I$(LIBBPF_INCLUDE)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ASAN_FLAGS = -fsanitize=kernel-address -fno-stack-protector -fno-builtin
|
||||||
|
ASAN_FLAGS += -mllvm -asan-instrument-address-spaces=1 -mllvm -asan-shadow-addr-space=1
|
||||||
|
ASAN_FLAGS += -mllvm -asan-use-stack-safety=0 -mllvm -asan-stack=0
|
||||||
|
ASAN_FLAGS += -mllvm -asan-kernel=1
|
||||||
|
ASAN_FLAGS += -mllvm -asan-constructor-kind=none
|
||||||
|
ASAN_FLAGS += -mllvm -asan-destructor-kind=none
|
||||||
|
|
||||||
# ENABLE_ATOMICS_TESTS required because we use arena spinlocks
|
# ENABLE_ATOMICS_TESTS required because we use arena spinlocks
|
||||||
override BPF_CFLAGS += -DENABLE_ATOMICS_TESTS
|
override BPF_CFLAGS += -DENABLE_ATOMICS_TESTS
|
||||||
override BPF_CFLAGS += -O2 -g
|
override BPF_CFLAGS += -O2 -g
|
||||||
|
|
@ -53,17 +61,32 @@ CFLAGS += $(INCLUDES)
|
||||||
vpath %.bpf.c $(LIBARENA)/src $(LIBARENA)/selftests
|
vpath %.bpf.c $(LIBARENA)/src $(LIBARENA)/selftests
|
||||||
vpath %.c $(LIBARENA)/src $(LIBARENA)/selftests
|
vpath %.c $(LIBARENA)/src $(LIBARENA)/selftests
|
||||||
|
|
||||||
|
skeletons: libarena.skel.h libarena_asan.skel.h
|
||||||
|
.PHONY: skeletons
|
||||||
|
|
||||||
|
libarena_asan.skel.h: libarena_asan.bpf.o
|
||||||
|
$(call msg,GEN-SKEL,libarena,$@)
|
||||||
|
$(Q)$(BPFTOOL) gen skeleton $< name "libarena_asan" > $@
|
||||||
|
|
||||||
libarena.skel.h: libarena.bpf.o
|
libarena.skel.h: libarena.bpf.o
|
||||||
$(call msg,GEN-SKEL,libarena,$@)
|
$(call msg,GEN-SKEL,libarena,$@)
|
||||||
$(Q)$(BPFTOOL) gen skeleton $< name "libarena" > $@
|
$(Q)$(BPFTOOL) gen skeleton $< name "libarena" > $@
|
||||||
|
|
||||||
|
libarena_asan.bpf.o: $(LIBARENA_OBJECTS_ASAN)
|
||||||
|
$(call msg,GEN-OBJ,libarena,$@)
|
||||||
|
$(Q)$(BPFTOOL) gen object $@ $^
|
||||||
|
|
||||||
libarena.bpf.o: $(LIBARENA_OBJECTS)
|
libarena.bpf.o: $(LIBARENA_OBJECTS)
|
||||||
$(call msg,GEN-OBJ,libarena,$@)
|
$(call msg,GEN-OBJ,libarena,$@)
|
||||||
$(Q)$(BPFTOOL) gen object $@ $^
|
$(Q)$(BPFTOOL) gen object $@ $^
|
||||||
|
|
||||||
|
%_asan.bpf.o: %.bpf.c
|
||||||
|
$(call msg,CLNG-BPF,libarena,$@)
|
||||||
|
$(Q)$(CLANG) $(BPF_CFLAGS) $(ASAN_FLAGS) -DBPF_ARENA_ASAN $(BPF_TARGET_ENDIAN) -c $< -o $@
|
||||||
|
|
||||||
%.bpf.o: %.bpf.c
|
%.bpf.o: %.bpf.c
|
||||||
$(call msg,CLNG-BPF,libarena,$@)
|
$(call msg,CLNG-BPF,libarena,$@)
|
||||||
$(Q)$(CLANG) $(BPF_CFLAGS) $(BPF_TARGET_ENDIAN) -c $< -o $@
|
$(Q)$(CLANG) $(BPF_CFLAGS) $(BPF_TARGET_ENDIAN) -c $< -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(Q)rm -f *.skel.h *.bpf.o
|
$(Q)rm -f *.skel.h *.bpf.o *.linked*.o
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,11 @@ static inline bool libarena_is_test_prog(const char *name)
|
||||||
return strstr(name, "test_") == name;
|
return strstr(name, "test_") == name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline bool libarena_is_asan_test_prog(const char *name)
|
||||||
|
{
|
||||||
|
return strstr(name, "asan_test") == name;
|
||||||
|
}
|
||||||
|
|
||||||
static inline int libarena_run_prog_args(int prog_fd, void *args, size_t argsize)
|
static inline int libarena_run_prog_args(int prog_fd, void *args, size_t argsize)
|
||||||
{
|
{
|
||||||
LIBBPF_OPTS(bpf_test_run_opts, opts);
|
LIBBPF_OPTS(bpf_test_run_opts, opts);
|
||||||
|
|
@ -97,3 +102,31 @@ static inline int libarena_get_globals_pages(int arena_get_globals_fd,
|
||||||
free(vec);
|
free(vec);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int libarena_asan_init(int arena_asan_init_fd,
|
||||||
|
int asan_init_fd,
|
||||||
|
size_t arena_all_pages)
|
||||||
|
{
|
||||||
|
LIBBPF_OPTS(bpf_test_run_opts, opts);
|
||||||
|
struct asan_init_args args;
|
||||||
|
u64 globals_pages;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = libarena_get_globals_pages(arena_asan_init_fd,
|
||||||
|
arena_all_pages, &globals_pages);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
args = (struct asan_init_args){
|
||||||
|
.arena_all_pages = arena_all_pages,
|
||||||
|
.arena_globals_pages = globals_pages,
|
||||||
|
};
|
||||||
|
|
||||||
|
opts.ctx_in = &args;
|
||||||
|
opts.ctx_size_in = sizeof(args);
|
||||||
|
|
||||||
|
ret = bpf_prog_test_run_opts(asan_init_fd, &opts);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
return opts.retval;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
|
||||||
|
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define ST_PAGES 64
|
||||||
|
|
||||||
|
static inline void print_asan_map_state(void __arena *addr)
|
||||||
|
{
|
||||||
|
arena_stdout("%s:%d ASAN %p -> (val: %x gran: %x set: [%s])",
|
||||||
|
__func__, __LINE__, addr,
|
||||||
|
*(s8a *)(addr), ASAN_GRANULE(addr),
|
||||||
|
asan_shadow_set(addr) ? "yes" : "no");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Emit an error and force the current function to exit if the ASAN
|
||||||
|
* violation state is unexpected. Reset the violation state after.
|
||||||
|
*/
|
||||||
|
static inline int asan_validate_addr(bool cond, void __arena *addr)
|
||||||
|
{
|
||||||
|
if ((asan_violated != 0) == cond) {
|
||||||
|
asan_violated = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
arena_stdout("%s:%d ASAN asan_violated %lx", __func__, __LINE__,
|
||||||
|
(u64)asan_violated);
|
||||||
|
print_asan_map_state(addr);
|
||||||
|
|
||||||
|
asan_violated = 0;
|
||||||
|
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int asan_validate(void)
|
||||||
|
{
|
||||||
|
if (!asan_violated)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
arena_stdout("%s:%d Found ASAN violation at %lx", __func__, __LINE__,
|
||||||
|
asan_violated);
|
||||||
|
|
||||||
|
asan_violated = 0;
|
||||||
|
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct blob {
|
||||||
|
volatile u8 mem[59];
|
||||||
|
u8 oob;
|
||||||
|
};
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
|
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
|
||||||
#include <libarena/common.h>
|
#include <libarena/common.h>
|
||||||
|
|
||||||
|
#include <libarena/asan.h>
|
||||||
|
|
||||||
const volatile u32 zero = 0;
|
const volatile u32 zero = 0;
|
||||||
|
|
||||||
int arena_fls(__u64 word)
|
int arena_fls(__u64 word)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user