]> git.saurik.com Git - apple/xnu.git/blob - tests/ltable_exhaustion_test.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / tests / ltable_exhaustion_test.c
1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <mach/mach.h>
4 #include <unistd.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7
8 #define ITER 100
9
10 T_DECL(ltable_exhaustion_test,
11 "check if allocating not used ltable entries can panic the system",
12 T_META_ASROOT(true))
13 {
14 int n_ltable_entries, n_ltable_entries_after;
15 size_t len = sizeof(int);
16 int i;
17 mach_port_name_t portset;
18
19 /*
20 * Get how many ltable entries are allocated right now.
21 */
22 T_EXPECT_POSIX_SUCCESS(sysctlbyname("kern.n_ltable_entries", &n_ltable_entries, &len, NULL, 0), "kern.n_ltable_entries");
23
24 for (i = 0; i < ITER; i++) {
25 mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &portset);
26 }
27
28 /*
29 * Get how many ltable entries are allocated after the loop. Other processes in the system might have allocated entries,
30 * so don't expect the same value.
31 */
32 T_EXPECT_POSIX_SUCCESS(sysctlbyname("kern.n_ltable_entries", &n_ltable_entries_after, &len, NULL, 0), "kern.n_ltable_entries");
33
34 T_EXPECT_LE(n_ltable_entries_after, n_ltable_entries + ITER, "ltable before %d after %d iter %d", n_ltable_entries, n_ltable_entries_after, ITER);
35 }