Commit 103d002f authored by Hou Tao's avatar Hou Tao Committed by Martin KaFai Lau

selftests/bpf: Free the allocated resources after test case succeeds

Free the created fd or allocated bpf_object after test case succeeds,
else there will be resource leaks.

Spotted by using address sanitizer and checking the content of
/proc/$pid/fd directory.
Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
Link: https://lore.kernel.org/r/20220921070035.2016413-3-houtao@huaweicloud.comSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent f5eb23b9
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
...@@ -137,6 +138,7 @@ static void __test_map_lookup_and_update_batch(bool is_pcpu) ...@@ -137,6 +138,7 @@ static void __test_map_lookup_and_update_batch(bool is_pcpu)
free(keys); free(keys);
free(values); free(values);
free(visited); free(visited);
close(map_fd);
} }
static void array_map_batch_ops(void) static void array_map_batch_ops(void)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
...@@ -255,6 +256,7 @@ void __test_map_lookup_and_delete_batch(bool is_pcpu) ...@@ -255,6 +256,7 @@ void __test_map_lookup_and_delete_batch(bool is_pcpu)
free(visited); free(visited);
if (!is_pcpu) if (!is_pcpu)
free(values); free(values);
close(map_fd);
} }
void htab_map_batch_ops(void) void htab_map_batch_ops(void)
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
...@@ -150,4 +151,5 @@ void test_lpm_trie_map_batch_ops(void) ...@@ -150,4 +151,5 @@ void test_lpm_trie_map_batch_ops(void)
free(keys); free(keys);
free(values); free(values);
free(visited); free(visited);
close(map_fd);
} }
...@@ -659,13 +659,13 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -659,13 +659,13 @@ static void test_sockmap(unsigned int tasks, void *data)
{ {
struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break; struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break;
int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break; int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break;
struct bpf_object *parse_obj, *verdict_obj, *msg_obj;
int ports[] = {50200, 50201, 50202, 50204}; int ports[] = {50200, 50201, 50202, 50204};
int err, i, fd, udp, sfd[6] = {0xdeadbeef}; int err, i, fd, udp, sfd[6] = {0xdeadbeef};
u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0}; u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
int parse_prog, verdict_prog, msg_prog; int parse_prog, verdict_prog, msg_prog;
struct sockaddr_in addr; struct sockaddr_in addr;
int one = 1, s, sc, rc; int one = 1, s, sc, rc;
struct bpf_object *obj;
struct timeval to; struct timeval to;
__u32 key, value; __u32 key, value;
pid_t pid[tasks]; pid_t pid[tasks];
...@@ -761,6 +761,7 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -761,6 +761,7 @@ static void test_sockmap(unsigned int tasks, void *data)
i, udp); i, udp);
goto out_sockmap; goto out_sockmap;
} }
close(udp);
/* Test update without programs */ /* Test update without programs */
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
...@@ -823,27 +824,27 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -823,27 +824,27 @@ static void test_sockmap(unsigned int tasks, void *data)
/* Load SK_SKB program and Attach */ /* Load SK_SKB program and Attach */
err = bpf_prog_test_load(SOCKMAP_PARSE_PROG, err = bpf_prog_test_load(SOCKMAP_PARSE_PROG,
BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog); BPF_PROG_TYPE_SK_SKB, &parse_obj, &parse_prog);
if (err) { if (err) {
printf("Failed to load SK_SKB parse prog\n"); printf("Failed to load SK_SKB parse prog\n");
goto out_sockmap; goto out_sockmap;
} }
err = bpf_prog_test_load(SOCKMAP_TCP_MSG_PROG, err = bpf_prog_test_load(SOCKMAP_TCP_MSG_PROG,
BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog); BPF_PROG_TYPE_SK_MSG, &msg_obj, &msg_prog);
if (err) { if (err) {
printf("Failed to load SK_SKB msg prog\n"); printf("Failed to load SK_SKB msg prog\n");
goto out_sockmap; goto out_sockmap;
} }
err = bpf_prog_test_load(SOCKMAP_VERDICT_PROG, err = bpf_prog_test_load(SOCKMAP_VERDICT_PROG,
BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog); BPF_PROG_TYPE_SK_SKB, &verdict_obj, &verdict_prog);
if (err) { if (err) {
printf("Failed to load SK_SKB verdict prog\n"); printf("Failed to load SK_SKB verdict prog\n");
goto out_sockmap; goto out_sockmap;
} }
bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx"); bpf_map_rx = bpf_object__find_map_by_name(verdict_obj, "sock_map_rx");
if (!bpf_map_rx) { if (!bpf_map_rx) {
printf("Failed to load map rx from verdict prog\n"); printf("Failed to load map rx from verdict prog\n");
goto out_sockmap; goto out_sockmap;
...@@ -855,7 +856,7 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -855,7 +856,7 @@ static void test_sockmap(unsigned int tasks, void *data)
goto out_sockmap; goto out_sockmap;
} }
bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx"); bpf_map_tx = bpf_object__find_map_by_name(verdict_obj, "sock_map_tx");
if (!bpf_map_tx) { if (!bpf_map_tx) {
printf("Failed to load map tx from verdict prog\n"); printf("Failed to load map tx from verdict prog\n");
goto out_sockmap; goto out_sockmap;
...@@ -867,7 +868,7 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -867,7 +868,7 @@ static void test_sockmap(unsigned int tasks, void *data)
goto out_sockmap; goto out_sockmap;
} }
bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg"); bpf_map_msg = bpf_object__find_map_by_name(verdict_obj, "sock_map_msg");
if (!bpf_map_msg) { if (!bpf_map_msg) {
printf("Failed to load map msg from msg_verdict prog\n"); printf("Failed to load map msg from msg_verdict prog\n");
goto out_sockmap; goto out_sockmap;
...@@ -879,7 +880,7 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -879,7 +880,7 @@ static void test_sockmap(unsigned int tasks, void *data)
goto out_sockmap; goto out_sockmap;
} }
bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break"); bpf_map_break = bpf_object__find_map_by_name(verdict_obj, "sock_map_break");
if (!bpf_map_break) { if (!bpf_map_break) {
printf("Failed to load map tx from verdict prog\n"); printf("Failed to load map tx from verdict prog\n");
goto out_sockmap; goto out_sockmap;
...@@ -1125,7 +1126,9 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -1125,7 +1126,9 @@ static void test_sockmap(unsigned int tasks, void *data)
} }
close(fd); close(fd);
close(map_fd_rx); close(map_fd_rx);
bpf_object__close(obj); bpf_object__close(parse_obj);
bpf_object__close(msg_obj);
bpf_object__close(verdict_obj);
return; return;
out: out:
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++)
...@@ -1283,8 +1286,11 @@ static void test_map_in_map(void) ...@@ -1283,8 +1286,11 @@ static void test_map_in_map(void)
printf("Inner map mim.inner was not destroyed\n"); printf("Inner map mim.inner was not destroyed\n");
goto out_map_in_map; goto out_map_in_map;
} }
close(fd);
} }
bpf_object__close(obj);
return; return;
out_map_in_map: out_map_in_map:
......
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