extra_fds[0] = new_map(); if (!ASSERT_GE(extra_fds[0], 0, "new_map")) goto cleanup;
extra_fds[1] = new_map(); if (!ASSERT_GE(extra_fds[1], 0, "new_map")) goto cleanup;
prog_fd = load_test_prog(extra_fds, 2); if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD")) goto cleanup;
nr_map_ids = ARRAY_SIZE(map_ids); if (!check_expected_map_ids(prog_fd, 3, map_ids, &nr_map_ids)) goto cleanup;
/* maps should still exist when original file descriptors are closed */
Close(extra_fds[0]);
Close(extra_fds[1]); if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map_ids[0] should exist")) goto cleanup; if (!ASSERT_EQ(map_exists(map_ids[1]), true, "map_ids[1] should exist")) goto cleanup;
/* some fds might be invalid, so ignore return codes */
cleanup:
Close(extra_fds[1]);
Close(extra_fds[0]);
Close(prog_fd);
}
/* maps should still exist when original file descriptors are closed */
Close(extra_fds[0]);
Close(extra_fds[1]); if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map should exist")) goto cleanup; if (!ASSERT_EQ(map_exists(map_ids[1]), true, "map should exist")) goto cleanup;
/* some fds might be invalid, so ignore return codes */
cleanup:
Close(extra_fds[1]);
Close(extra_fds[0]);
Close(prog_fd);
}
extra_fds[0] = new_map(); if (!ASSERT_GE(extra_fds[0], 0, "new_map")) goto cleanup;
prog_fd = __load_test_prog(extra_fds[0], extra_fds, 1); if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD")) goto cleanup;
nr_map_ids = ARRAY_SIZE(map_ids); if (!check_expected_map_ids(prog_fd, 1, map_ids, &nr_map_ids)) goto cleanup;
/* map should still exist when original file descriptor is closed */
Close(extra_fds[0]); if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map should exist")) goto cleanup;
/* some fds might be invalid, so ignore return codes */
cleanup:
Close(extra_fds[0]);
Close(prog_fd);
}
memset(&info, 0, info_len);
err = bpf_btf_get_info_by_fd(btf_fd, &info, &info_len); if (err) return err; if (id)
*id = info.id; return0;
}
/* *Checkthatfd_arrayoperatesproperlyforbtfs.Namely,tocheckthat *passingabtffdinfd_arrayincreasesitsreferencecount,dothe *following: *1)Createanewbtf,it'sreferencedonlybyafiledescriptor,sorefcnt=1 *2)LoadaBPFprogwithfd_array[0]=btf_fd;nowbtf'srefcnt=2 *3)Closethebtf_fd,nowrefcnt=1 *WaitandcheckthatBTFstillexists.
*/ staticvoid check_fd_array_cnt__referenced_btfs(void)
{ int extra_fds[1] = { -1 }; int prog_fd = -1;
__u32 btf_id; int tries; int err;
extra_fds[0] = new_btf(); if (!ASSERT_GE(extra_fds[0], 0, "new_btf")) goto cleanup;
prog_fd = load_test_prog(extra_fds, 1); if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD")) goto cleanup;
/* btf should still exist when original file descriptor is closed */
err = get_btf_id_by_fd(extra_fds[0], &btf_id); if (!ASSERT_EQ(err, 0, "get_btf_id_by_fd")) goto cleanup;
Close(extra_fds[0]);
if (!ASSERT_GE(kern_sync_rcu(), 0, "kern_sync_rcu 1")) goto cleanup;
if (!ASSERT_EQ(btf_exists(btf_id), true, "btf should exist")) goto cleanup;
Close(prog_fd);
/* The program is freed by a workqueue, so no reliable
* way to sync, so just wait a bit (max ~1 second). */ for (tries = 100; tries >= 0; tries--) {
usleep(1000);
if (!btf_exists(btf_id)) break;
if (tries) continue;
PRINT_FAIL("btf should have been freed");
}
/* some fds might be invalid, so ignore return codes */
cleanup:
Close(extra_fds[0]);
Close(prog_fd);
}
/* *Testthataprogramwithtrashinfd_arraycan'tbeloaded: *onlymapandBTFfiledescriptorsshouldbeaccepted.
*/ staticvoid check_fd_array_cnt__fd_array_with_trash(void)
{ int extra_fds[3] = { -1, -1, -1 }; int prog_fd = -1;
extra_fds[0] = new_map(); if (!ASSERT_GE(extra_fds[0], 0, "new_map")) goto cleanup;
extra_fds[1] = new_btf(); if (!ASSERT_GE(extra_fds[1], 0, "new_btf")) goto cleanup;
/* trash 1: not a file descriptor */
extra_fds[2] = 0xbeef;
prog_fd = load_test_prog(extra_fds, 3); if (!ASSERT_EQ(prog_fd, -EBADF, "prog should have been rejected with -EBADF")) goto cleanup;
/* trash 2: not a map or btf */
extra_fds[2] = socket(AF_INET, SOCK_STREAM, 0); if (!ASSERT_GE(extra_fds[2], 0, "socket")) goto cleanup;
prog_fd = load_test_prog(extra_fds, 3); if (!ASSERT_EQ(prog_fd, -EINVAL, "prog should have been rejected with -EINVAL")) goto cleanup;
/* Validate that the prog is ok if trash is removed */
Close(extra_fds[2]);
extra_fds[2] = new_btf(); if (!ASSERT_GE(extra_fds[2], 0, "new_btf")) goto cleanup;
prog_fd = load_test_prog(extra_fds, 3); if (!ASSERT_GE(prog_fd, 0, "prog should have been loaded")) goto cleanup;
/* some fds might be invalid, so ignore return codes */
cleanup:
Close(extra_fds[2]);
Close(extra_fds[1]);
Close(extra_fds[0]);
}
/* *Testthataprogramwithtoobigfd_arraycan'tbeloaded.
*/ staticvoid check_fd_array_cnt__fd_array_too_big(void)
{ int extra_fds[65]; int prog_fd = -1; int i;
for (i = 0; i < 65; i++) {
extra_fds[i] = new_map(); if (!ASSERT_GE(extra_fds[i], 0, "new_map")) goto cleanup_fds;
}
prog_fd = load_test_prog(extra_fds, 65);
ASSERT_EQ(prog_fd, -E2BIG, "prog should have been rejected with -E2BIG");
cleanup_fds: while (i > 0)
Close(extra_fds[--i]);
}
void test_fd_array_cnt(void)
{ if (test__start_subtest("no-fd-array"))
check_fd_array_cnt__no_fd_array();
if (test__start_subtest("fd-array-ok"))
check_fd_array_cnt__fd_array_ok();
if (test__start_subtest("fd-array-dup-input"))
check_fd_array_cnt__duplicated_maps();
if (test__start_subtest("fd-array-ref-maps-in-array"))
check_fd_array_cnt__referenced_maps_in_fd_array();
if (test__start_subtest("fd-array-ref-btfs"))
check_fd_array_cnt__referenced_btfs();
if (test__start_subtest("fd-array-trash-input"))
check_fd_array_cnt__fd_array_with_trash();
if (test__start_subtest("fd-array-2big"))
check_fd_array_cnt__fd_array_too_big();
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.29 Sekunden
(vorverarbeitet am 2026-10-11)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.