Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Testing framework shared between all the drivers #783

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
e689244
new: first scaffolding tests
Andreagit97 Nov 22, 2022
fd82e3e
update: adapt the code to run also syscalls tests
Andreagit97 Nov 22, 2022
643e4d1
fix: bind syscall
Andreagit97 Nov 22, 2022
7d16854
fix: bpf syscall
Andreagit97 Nov 23, 2022
a97df58
fix: close syscall
Andreagit97 Nov 23, 2022
67bb9dd
fix: fchdir syscall
Andreagit97 Nov 23, 2022
2413292
fix: fcntl syscall
Andreagit97 Nov 23, 2022
a9af14d
fix: copy_file_range syscall
Andreagit97 Nov 23, 2022
17b6682
fix: flock syscall
Andreagit97 Nov 23, 2022
d17edda
update: enable again the sys_exit tests
Andreagit97 Nov 23, 2022
79bbaff
fix: ioctl syscall
Andreagit97 Nov 23, 2022
8e0da9a
fix: mkdir syscall
Andreagit97 Nov 23, 2022
8b8f24b
fix: ptrace syscall
Andreagit97 Nov 24, 2022
9cd7148
fix: setns syscall
Andreagit97 Nov 24, 2022
59e9b3d
fix: setpgid syscall
Andreagit97 Nov 24, 2022
dbb077b
fix: shutdown syscall
Andreagit97 Nov 24, 2022
caab5a3
fix: recvfrom syscall
Andreagit97 Nov 24, 2022
4dfd14c
fix: recvmsg syscall
Andreagit97 Nov 24, 2022
8230e7c
fix: signalfd and signalfd4 tests
Andreagit97 Nov 24, 2022
e7d524e
fix: correct order in nwtwork syscalls modern bpf
Andreagit97 Nov 25, 2022
4a456cb
fix: manage empty params in network syscalls
Andreagit97 Nov 25, 2022
678ca4d
fix: unify accept logic between 3 drivers
Andreagit97 Nov 26, 2022
db6a995
fix: process syscalls (clone, fork)
Andreagit97 Nov 26, 2022
b9ad5d7
fix: copy_file_range exit event
Andreagit97 Nov 26, 2022
b8d8224
fix: fchmod, fchmodat syscalls
Andreagit97 Nov 26, 2022
465063b
fix: ioctl exit event
Andreagit97 Nov 27, 2022
b392ee9
fix: mkdirat exit event
Andreagit97 Nov 29, 2022
eea3f75
fix: open_by_handle_at exit event
Andreagit97 Nov 29, 2022
42e0ad3
fix: pipe, pipe2 exit events
Andreagit97 Nov 29, 2022
a66095f
fix: setsockopt exit event
Andreagit97 Dec 1, 2022
a3f3a9b
new: support auto inject bpf/kmod
Andreagit97 Dec 1, 2022
0e72ec6
fix: unlink exit event
Andreagit97 Dec 2, 2022
4b9d635
fix: quotactl exit event
Andreagit97 Dec 2, 2022
e2501b9
fix: recvfrom exit event
Andreagit97 Dec 3, 2022
549c357
fix: send empty params when recvmsg syscall fails
Andreagit97 Dec 3, 2022
41b65b8
tests: assert current params and skip test if the underling logic is …
Andreagit97 Dec 3, 2022
25020d7
fix: send `sendmsg` empty params when the syscall fails
Andreagit97 Dec 3, 2022
a6fa170
fix: socketpair exit test
Andreagit97 Dec 3, 2022
41d8adb
fix: support `PPM_EXE_WRITABLE` in tests
Andreagit97 Dec 3, 2022
7eee00c
tests: call the bpf exit event from the child
Andreagit97 Dec 3, 2022
e5e62eb
update: enable again `sched_fork` and `sched_exec` tests
Andreagit97 Dec 5, 2022
79012d1
tests: fix `sched_proc_fork` test
Andreagit97 Dec 5, 2022
4250180
fix: adapt tests after rebase
Andreagit97 Dec 6, 2022
6852247
fix: quotactl enter event
Andreagit97 Dec 6, 2022
cfdeb1d
tests: add a new test to test reading phase from buffer
Andreagit97 Dec 6, 2022
eac71cd
tests: add check for unknown engine
Andreagit97 Dec 6, 2022
8b4b545
cleanup: rename test folder
Andreagit97 Dec 6, 2022
c8471d0
chore: adapt cmake to run tests also without the modern bpf probe
Andreagit97 Dec 6, 2022
71f1aab
ci: support drivers tests in CI
Andreagit97 Dec 6, 2022
180482a
new: add an helper for pushing empty params
Andreagit97 Dec 7, 2022
088802a
fix: add a missing header for epoll_create1
Andreagit97 Dec 7, 2022
c1d40a1
fix: prlimit64 enter event
Andreagit97 Dec 7, 2022
57b416c
tests: fix BPF tests on amazon linux 4.14
Andreagit97 Dec 7, 2022
298c51f
new(ci): test drivers also on ubuntu 20.04
Andreagit97 Dec 8, 2022
4ede2bf
fix(ci): add a cmake option to compile out drivers tests
Andreagit97 Dec 8, 2022
c63a53d
doc: fix README and a comment
Andreagit97 Dec 9, 2022
14c1dc7
cleanup
Andreagit97 Dec 9, 2022
34dd4e8
fix: reintroduce cmake option after rebase
Andreagit97 Dec 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 68 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ jobs:
KERNELDIR=/lib/modules/$(ls /lib/modules)/build make -j4
make run-unit-tests

build-and-test-modern-bpf-x86:
name: build-and-test-modern-bpf-x86 😇 (bundled_deps)
# This job checks that we correctly run `scap-open` for all the 3 drivers.
test-scap-open-x86:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test to check all drivers correctly start (only x86, it would be amazing to test also other archs)

name: test-scap-open-x86 😆 (bundled_deps)
runs-on: ubuntu-22.04
needs: paths-filter
if: needs.paths-filter.outputs.driver_changed == 'true' || needs.paths-filter.outputs.libscap_changed == 'true'
Expand All @@ -177,31 +178,80 @@ jobs:
- name: Install deps ⛓️
run: |
sudo apt update
sudo apt install -y --no-install-recommends ca-certificates cmake build-essential clang-14 git pkg-config autoconf automake libtool libelf-dev libcap-dev
sudo apt install -y --no-install-recommends ca-certificates cmake build-essential clang-14 llvm-14 git pkg-config autoconf automake libtool libelf-dev libcap-dev linux-headers-$(uname -r)
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 90
sudo update-alternatives --install /usr/bin/llvm-strip llvm-strip /usr/bin/llvm-strip-14 90
sudo update-alternatives --install /usr/bin/llc llc /usr/bin/llc-14 90

- name: Build scap-open 🏗️
- name: Build scap-open and drivers 🏗️
run: |
mkdir -p build
cd build && cmake -DUSE_BUNDLED_DEPS=ON -DBUILD_LIBSCAP_MODERN_BPF=ON -DBUILD_MODERN_BPF_TEST=ON -DMODERN_BPF_DEBUG_MODE=ON -DBUILD_LIBSCAP_GVISOR=OFF ../
cd build && cmake -DUSE_BUNDLED_DEPS=ON -DBUILD_LIBSCAP_MODERN_BPF=ON -DBUILD_LIBSCAP_GVISOR=OFF -DBUILD_BPF=True -DCREATE_TEST_TARGETS=Off ../
make scap-open
make driver bpf

- name: Run scap-open 🏎️
- name: Run scap-open with modern bpf 🏎️
run: |
cd build
sudo ./libscap/examples/01-open/scap-open --modern_bpf --num_events 0

- name: Build bpf_test 🏗
- name: Run scap-open with bpf 🏎
run: |
cd build
make bpf_test
sudo ./libscap/examples/01-open/scap-open --bpf ./driver/bpf/probe.o --num_events 0

- name: Running tests 🧪
- name: Run scap-open with kmod 🏎️
run: |
cd build
sudo ./test/modern_bpf/bpf_test --verbose
sudo insmod ./driver/scap.ko
sudo ./libscap/examples/01-open/scap-open --kmod --num_events 0
sudo rmmod scap

build-modern-bpf-arm64:
name: build-modern-bpf-arm64 🙃 (system_deps)
# This job matrix run tests for all the 3 drivers on 2 machines: ubuntu-22.04, ubuntu 20-04
test-drivers-x86:
name: test-drivers-x86 😇 (bundled_deps)
runs-on: ubuntu-22.04
needs: paths-filter
if: needs.paths-filter.outputs.driver_changed == 'true' || needs.paths-filter.outputs.libscap_changed == 'true'
steps:

- name: Checkout Libs ⤵️
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install deps ⛓️
run: |
sudo apt update
sudo apt install -y --no-install-recommends ca-certificates cmake build-essential git pkg-config autoconf automake libelf-dev libcap-dev linux-headers-$(uname -r) clang-14 llvm-14 libtool
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 90
sudo update-alternatives --install /usr/bin/llvm-strip llvm-strip /usr/bin/llvm-strip-14 90
sudo update-alternatives --install /usr/bin/llc llc /usr/bin/llc-14 90

- name: Build drivers tests 🏗️
run: |
mkdir -p build
cd build && cmake -DUSE_BUNDLED_DEPS=ON -DENABLE_DRIVERS_TESTS=ON -DBUILD_LIBSCAP_MODERN_BPF=ON -DMODERN_BPF_DEBUG_MODE=ON -DBUILD_BPF=True -DBUILD_LIBSCAP_GVISOR=OFF ../
make drivers_test
make driver bpf

- name: Run drivers_test with modern bpf 🏎️
run: |
cd build
sudo ./test/drivers/drivers_test -m

- name: Run drivers_test with bpf 🏎️
run: |
cd build
sudo ./test/drivers/drivers_test -b

- name: Run drivers_test with kmod 🏎️
run: |
cd build
sudo ./test/drivers/drivers_test -k

build-only-test-drivers-arm64:
name: build-only-test-drivers-arm64 🙃 (system_deps)
runs-on: ubuntu-22.04
needs: paths-filter
if: needs.paths-filter.outputs.driver_changed == 'true' || needs.paths-filter.outputs.libscap_changed == 'true'
Expand All @@ -227,12 +277,11 @@ jobs:
git config --global --add safe.directory ${{ github.workspace }}
.github/install-deps.sh
mkdir -p build
cd build && cmake -DUSE_BUNDLED_DEPS=OFF -DBUILD_LIBSCAP_MODERN_BPF=ON -DBUILD_MODERN_BPF_TEST=ON -DMODERN_BPF_DEBUG_MODE=ON -DBUILD_LIBSCAP_GVISOR=OFF ../
make scap-open
make bpf_test
cd build && cmake -DUSE_BUNDLED_DEPS=OFF -DENABLE_DRIVERS_TESTS=ON -DBUILD_LIBSCAP_MODERN_BPF=ON -DMODERN_BPF_DEBUG_MODE=ON -DBUILD_LIBSCAP_GVISOR=OFF ../
make drivers_test

build-modern-bpf-s390x:
name: build-modern-bpf-s390x 😁 (system_deps)
build-only-test-drivers-s390x:
name: build-only-test-drivers-s390x 😁 (system_deps)
runs-on: ubuntu-22.04
needs: paths-filter
if: needs.paths-filter.outputs.driver_changed == 'true' || needs.paths-filter.outputs.libscap_changed == 'true'
Expand All @@ -258,9 +307,8 @@ jobs:
git config --global --add safe.directory ${{ github.workspace }}
.github/install-deps.sh
mkdir -p build
cd build && cmake -DUSE_BUNDLED_DEPS=OFF -DBUILD_LIBSCAP_MODERN_BPF=ON -DBUILD_MODERN_BPF_TEST=ON -DMODERN_BPF_DEBUG_MODE=ON -DBUILD_LIBSCAP_GVISOR=OFF ../
make scap-open
make bpf_test
cd build && cmake -DUSE_BUNDLED_DEPS=OFF -DENABLE_DRIVERS_TESTS=ON -DBUILD_LIBSCAP_MODERN_BPF=ON -DMODERN_BPF_DEBUG_MODE=ON -DBUILD_LIBSCAP_GVISOR=OFF ../
make drivers_test

build-libs-driverkit:
name: build-libs-driverkit
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ option(USE_BUNDLED_DEPS "Enable bundled dependencies instead of using the system
option(MINIMAL_BUILD "Produce a minimal build with only the essential features (no eBPF probe driver, no kubernetes, no mesos, no marathon and no container metadata)" OFF)
option(MUSL_OPTIMIZED_BUILD "Enable if you want a musl optimized build" OFF)
option(USE_BUNDLED_DRIVER "Use the driver/ subdirectory in the build process (only available in Linux)" ON)
option(ENABLE_DRIVERS_TESTS "Enable driver tests (bpf, kernel module, modern bpf)" OFF)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to disable it by default otherwise some CI jobs that use system deps will break since the gtest version is different


include(GNUInstallDirs)

Expand Down Expand Up @@ -94,4 +95,8 @@ if(CREATE_TEST_TARGETS AND NOT WIN32)
)

add_subdirectory(test/e2e)

if(ENABLE_DRIVERS_TESTS)
add_subdirectory(test/drivers)
endif()
endif()
57 changes: 46 additions & 11 deletions driver/bpf/filler_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ static __always_inline long bpf_fd_to_socktuple(struct filler_data *data,
struct socket *sock;
struct sock *sk;
long size = 0;
struct in6_addr in6 = {0};

sock = bpf_sockfd_lookup(data, fd);
if (!sock)
Expand Down Expand Up @@ -643,8 +644,14 @@ static __always_inline long bpf_fd_to_socktuple(struct filler_data *data,
struct sockaddr_in *usrsockaddr_in = (struct sockaddr_in *)usrsockaddr;

if (is_inbound) {
sip = usrsockaddr_in->sin_addr.s_addr;
sport = ntohs(usrsockaddr_in->sin_port);
/* To take inbound info we cannot use the `src_addr` obtained from the syscall
* it could be empty!
* From kernel 3.13 we can take both ipv4 and ipv6 info from here
* https://elixir.bootlin.com/linux/v3.13/source/include/net/sock.h#L164
*/
bpf_probe_read(&sip, sizeof(sip), &sk->__sk_common.skc_daddr);
bpf_probe_read(&sport, sizeof(sport), &sk->__sk_common.skc_dport);
sport = ntohs(sport);
dip = ((struct sockaddr_in *)sock_address)->sin_addr.s_addr;
dport = ntohs(((struct sockaddr_in *)sock_address)->sin_port);
} else {
Expand Down Expand Up @@ -699,8 +706,10 @@ static __always_inline long bpf_fd_to_socktuple(struct filler_data *data,
struct sockaddr_in6 *usrsockaddr_in6 = (struct sockaddr_in6 *)usrsockaddr;

if (is_inbound) {
sip6 = usrsockaddr_in6->sin6_addr.s6_addr;
sport = ntohs(usrsockaddr_in6->sin6_port);
bpf_probe_read(&in6, sizeof(in6), &sk->__sk_common.skc_v6_daddr);
sip6 = in6.in6_u.u6_addr8;
bpf_probe_read(&sport, sizeof(sport), &sk->__sk_common.skc_dport);
sport = ntohs(sport);
dip6 = ((struct sockaddr_in6 *)sock_address)->sin6_addr.s6_addr;
dport = ntohs(((struct sockaddr_in6 *)sock_address)->sin6_port);
} else {
Expand Down Expand Up @@ -941,15 +950,24 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
case PT_SOCKADDR:
case PT_SOCKTUPLE:
case PT_FDLIST:
if (!data->curarg_already_on_frame) {
bpf_printk("expected arg already on frame: evt_type %d, curarg %d, type %d\n",
data->state->tail_ctx.evt_type,
data->state->tail_ctx.curarg, type);
return PPM_FAILURE_BUG;
if(data->curarg_already_on_frame)
{
len = val_len;
break;
}
/* Cases in which we don't have the tuple and
* we want to send an empty param.
*/
else if(val==0)
{
len = 0;
break;
}
bpf_printk("expected arg already on frame: evt_type %d, curarg %d, type %d\n",
data->state->tail_ctx.evt_type,
data->state->tail_ctx.curarg, type);
return PPM_FAILURE_BUG;

len = val_len;
break;
case PT_FLAGS8:
case PT_ENUMFLAGS8:
case PT_UINT8:
Expand Down Expand Up @@ -1020,6 +1038,23 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
return PPM_SUCCESS;
}

static __always_inline int bpf_push_empty_param(struct filler_data *data)
{
/* This is not so necessary but just keep it for compliance with other helpers */
if (data->state->tail_ctx.curarg >= PPM_MAX_EVENT_PARAMS) {
bpf_printk("invalid curarg: %d\n", data->state->tail_ctx.curarg);
return PPM_FAILURE_BUG;
}

/* We push 0 in the length array */
fixup_evt_arg_len(data->buf, data->state->tail_ctx.curarg, 0);
data->curarg_already_on_frame = false;

/* We increment the current argument */
++data->state->tail_ctx.curarg;
return PPM_SUCCESS;
}

static __always_inline int bpf_val_to_ring(struct filler_data *data,
unsigned long val)
{
Expand Down
Loading