From 482e47f6444b299b45346230b5efc1c29fe6b345 Mon Sep 17 00:00:00 2001 From: Andrea Terzolo Date: Thu, 16 May 2024 14:32:07 +0200 Subject: [PATCH] cleanup(modern): avoid to use an array with just one element Signed-off-by: Andrea Terzolo Co-authored-by: Mauro Ezequiel Moltrasio --- .../events/syscall_dispatched_events/accept.bpf.c | 9 ++++----- .../events/syscall_dispatched_events/accept4.bpf.c | 9 ++++----- .../events/syscall_dispatched_events/bind.bpf.c | 7 +++---- .../events/syscall_dispatched_events/connect.bpf.c | 13 +++++++------ .../events/syscall_dispatched_events/recvmsg.bpf.c | 7 +++---- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept.bpf.c b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept.bpf.c index b0a4a3ef9b..8a5973895a 100644 --- a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept.bpf.c +++ b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept.bpf.c @@ -77,16 +77,15 @@ int BPF_PROG(accept_x, auxmap__store_socktuple_param(auxmap, (int32_t)ret, INBOUND, NULL); /* Collect parameters at the beginning to manage socketcalls */ - unsigned long args[1] = {0}; - extract__network_args(args, 1, regs); + unsigned long socket_fd = 0; + extract__network_args(&socket_fd, 1, regs); /* Perform some computations to get queue information. */ - /* If the syscall is successful the `sockfd` will be >= 0. We want + /* If the syscall is successful the `socket_fd` will be >= 0. We want * to extract information from the listening socket, not from the * new one. */ - int32_t sockfd = (int32_t)args[0]; - struct file *file = extract__file_struct_from_fd(sockfd); + struct file *file = extract__file_struct_from_fd((int32_t)socket_fd); struct socket *socket = get_sock_from_file(file); if(socket != NULL) { diff --git a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept4.bpf.c b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept4.bpf.c index 4b0f7c6369..0180a5652d 100644 --- a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept4.bpf.c +++ b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/accept4.bpf.c @@ -81,16 +81,15 @@ int BPF_PROG(accept4_x, auxmap__store_socktuple_param(auxmap, (int32_t)ret, INBOUND, NULL); /* Collect parameters at the beginning to manage socketcalls */ - unsigned long args[1] = {0}; - extract__network_args(args, 1, regs); + unsigned long socket_fd = 0; + extract__network_args(&socket_fd, 1, regs); /* Perform some computations to get queue information. */ - /* If the syscall is successful the `sockfd` will be >= 0. We want + /* If the syscall is successful the `socket_fd` will be >= 0. We want * to extract information from the listening socket, not from the * new one. */ - int32_t sockfd = (int32_t)args[0]; - struct file *file = extract__file_struct_from_fd(sockfd); + struct file *file = extract__file_struct_from_fd((int32_t)socket_fd); struct socket *socket = get_sock_from_file(file); if(socket != NULL) { diff --git a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/bind.bpf.c b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/bind.bpf.c index bd9b8ded8f..7e9736bea4 100644 --- a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/bind.bpf.c +++ b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/bind.bpf.c @@ -17,8 +17,8 @@ int BPF_PROG(bind_e, long id) { /* Collect parameters at the beginning to easily manage socketcalls */ - unsigned long args[1] = {0}; - extract__network_args(args, 1, regs); + unsigned long socket_fd = 0; + extract__network_args(&socket_fd, 1, regs); struct ringbuf_struct ringbuf; if(!ringbuf__reserve_space(&ringbuf, ctx, BIND_E_SIZE, PPME_SOCKET_BIND_E)) @@ -31,8 +31,7 @@ int BPF_PROG(bind_e, /*=============================== COLLECT PARAMETERS ===========================*/ /* Parameter 1: fd (type: PT_FD) */ - int32_t fd = (int32_t)args[0]; - ringbuf__store_s64(&ringbuf, (int64_t)fd); + ringbuf__store_s64(&ringbuf, (int64_t)(int32_t)socket_fd); /*=============================== COLLECT PARAMETERS ===========================*/ diff --git a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/connect.bpf.c b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/connect.bpf.c index fac6616890..bf9c49927d 100644 --- a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/connect.bpf.c +++ b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/connect.bpf.c @@ -65,19 +65,17 @@ int BPF_PROG(connect_x, /*=============================== COLLECT PARAMETERS ===========================*/ - unsigned long args[1] = {0}; - extract__network_args(args, 1, regs); + unsigned long socket_fd = 0; + extract__network_args(&socket_fd, 1, regs); /* Parameter 1: res (type: PT_ERRNO) */ auxmap__store_s64_param(auxmap, ret); - int32_t socket_fd = (int32_t)args[0]; - /* Parameter 2: tuple (type: PT_SOCKTUPLE) */ /* We need a valid sockfd to extract source data.*/ if(ret == 0 || ret == -EINPROGRESS) { - auxmap__store_socktuple_param(auxmap, socket_fd, OUTBOUND, NULL); + auxmap__store_socktuple_param(auxmap, (int32_t)socket_fd, OUTBOUND, NULL); } else { @@ -85,7 +83,10 @@ int BPF_PROG(connect_x, } /* Parameter 3: fd (type: PT_FD)*/ - auxmap__store_s64_param(auxmap, (int64_t)socket_fd); + /* We need the double cast to extract the first 4 bytes and then + * convert them to a signed integer on 64-bit + */ + auxmap__store_s64_param(auxmap, (int64_t)(int32_t)socket_fd); /*=============================== COLLECT PARAMETERS ===========================*/ diff --git a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/recvmsg.bpf.c b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/recvmsg.bpf.c index 869505ef5d..f0fbb9354a 100644 --- a/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/recvmsg.bpf.c +++ b/driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/recvmsg.bpf.c @@ -17,8 +17,8 @@ int BPF_PROG(recvmsg_e, long id) { /* Collect parameters at the beginning to manage socketcalls */ - unsigned long args[1] = {0}; - extract__network_args(args, 1, regs); + unsigned long socket_fd = 0; + extract__network_args(&socket_fd, 1, regs); struct ringbuf_struct ringbuf; if(!ringbuf__reserve_space(&ringbuf, ctx, RECVMSG_E_SIZE, PPME_SOCKET_RECVMSG_E)) @@ -31,8 +31,7 @@ int BPF_PROG(recvmsg_e, /*=============================== COLLECT PARAMETERS ===========================*/ /* Parameter 1: fd (type: PT_FD)*/ - int32_t fd = (int32_t)args[0]; - ringbuf__store_s64(&ringbuf, (int64_t)fd); + ringbuf__store_s64(&ringbuf, (int64_t)(int32_t)socket_fd); /*=============================== COLLECT PARAMETERS ===========================*/