]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/darwintests/proc_core_name_24152432.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / tools / tests / darwintests / proc_core_name_24152432.c
CommitLineData
39037602
A
1#include <darwintest.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <sys/types.h>
5#include <sys/sysctl.h>
6#include <sys/resource.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdio.h>
10#include <TargetConditionals.h>
11#include <unistd.h>
12
13#define BUFFLEN 2048
14#define EVILLEN 19
15
16static const char corefile_ctl[] = "kern.corefile";
17static const char coredump_ctl[] = "kern.coredump";
18/* The default coredump location if the kern.coredump ctl is invalid */
19static const char default_dump_fmt[] = "/cores/core.%d";
20/* The coredump location when we set kern.coredump ctl to something valid */
21static const char valid_dump_fmt[] = "/cores/test-core.%d";
22
23/* /cores/core.%(null), then BORK immediately after. */
24static char evil[] = {'/', 'c', 'o', 'r', 'e', 's', '/', 'c', 'o', 'r', 'e', '.', '%', '\0', 'B', 'O', 'R', 'K', '\0'};
25/* A valid coredump location to test. */
26static char valid_dump_loc[] = "/cores/test-core.%P";
27
28static const struct rlimit lim_infty = {
29 RLIM_INFINITY,
30 RLIM_INFINITY
31};
32
33#if TARGET_OS_OSX
34static int fork_and_wait_for_segfault(void);
35
36static int fork_and_wait_for_segfault() {
37 int pid, ret;
38 pid = fork();
39 if (pid == 0) {
40 unsigned int *ptr = NULL; /* Cause a segfault so that we get a coredump */
41 *ptr = 0xdeadd00d;
42 T_FAIL("Expected segmentation fault on write to NULL pointer");
43 }
44 T_ASSERT_TRUE(pid != -1, "Checking fork success in parent");
45
46 ret = wait(NULL);
47 T_ASSERT_TRUE(ret != -1, "Waited for child to segfault and dump core");
48 return pid;
49}
50#endif
51
52T_DECL(
53 proc_core_name_24152432,
54 "Tests behavior of core dump when kern.corefile ends in %, e.g., /cores/core.%",
55 T_META("owner", "Core Kernel Team"),
56 T_META_ASROOT(YES))
57{
58#if TARGET_OS_OSX
59 int ret, pid;
60 int enable_core_dump = 1;
61 char buf[BUFFLEN];
62 memset(buf, 0, BUFFLEN);
63 size_t oldlen = BUFFLEN;
64
65 ret = sysctlbyname(coredump_ctl, buf, &oldlen, &enable_core_dump, sizeof(int));
66 T_ASSERT_POSIX_SUCCESS(ret, "sysctl: enable core dumps");
67 memset(buf, 0, BUFFLEN);
68 oldlen = BUFFLEN;
69
70 ret = setrlimit(RLIMIT_CORE, &lim_infty);
71 T_ASSERT_POSIX_SUCCESS(ret, "setrlimit: remove limit on maximum coredump size");
72
73 ret = sysctlbyname(corefile_ctl, buf, &oldlen, evil, EVILLEN);
74 T_ASSERT_POSIX_SUCCESS(ret, "sysctl: set bad core dump location, old value was %s", buf);
75 memset(buf, 0, BUFFLEN);
76 oldlen = BUFFLEN;
77
78 pid = fork_and_wait_for_segfault();
79
80 snprintf(buf, BUFFLEN, default_dump_fmt, pid);
81 ret = remove(buf);
82 T_ASSERT_TRUE(ret != -1, "Removing coredump file (should be in fallback location)");
83 memset(buf, 0, BUFFLEN);
84
85 ret = sysctlbyname(corefile_ctl, buf, &oldlen, valid_dump_loc, strlen(valid_dump_loc));
86 T_ASSERT_POSIX_SUCCESS(ret, "sysctl: set valid core dump location, old value was %s", buf);
87 memset(buf, 0, BUFFLEN);
88
89 pid = fork_and_wait_for_segfault();
90
91 snprintf(buf, BUFFLEN, valid_dump_fmt, pid);
92 ret = remove(buf);
93 T_ASSERT_TRUE(ret != -1, "Removing coredump file (should be in valid location)");
94#else
95 T_LOG("proc_core_name appears in OS X only, skipping test.");
96#endif
97 T_PASS("proc_core_name_24152432 PASSED");
98}