1*fdd8201dSApple OSS Distributions #include <darwintest.h>
2*fdd8201dSApple OSS Distributions #include <mach/host_priv.h>
3*fdd8201dSApple OSS Distributions #include <mach/mach.h>
4*fdd8201dSApple OSS Distributions #include <mach/mach_types.h>
5*fdd8201dSApple OSS Distributions #include <mach/processor_set.h>
6*fdd8201dSApple OSS Distributions #include <mach/task.h>
7*fdd8201dSApple OSS Distributions #include <sys/sysctl.h>
8*fdd8201dSApple OSS Distributions #include <unistd.h>
9*fdd8201dSApple OSS Distributions #include <mach-o/dyld.h>
10*fdd8201dSApple OSS Distributions #include <mach-o/dyld_images.h>
11*fdd8201dSApple OSS Distributions #include <sys/types.h>
12*fdd8201dSApple OSS Distributions #include <sys/wait.h>
13*fdd8201dSApple OSS Distributions #include <stdlib.h>
14*fdd8201dSApple OSS Distributions
15*fdd8201dSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
16*fdd8201dSApple OSS Distributions
17*fdd8201dSApple OSS Distributions static void
do_child(int * pipefd)18*fdd8201dSApple OSS Distributions do_child(int *pipefd)
19*fdd8201dSApple OSS Distributions {
20*fdd8201dSApple OSS Distributions int exit = 0;
21*fdd8201dSApple OSS Distributions
22*fdd8201dSApple OSS Distributions close(pipefd[1]);
23*fdd8201dSApple OSS Distributions read(pipefd[0], &exit, sizeof(int));
24*fdd8201dSApple OSS Distributions T_QUIET; T_EXPECT_EQ_INT(exit, 1, "exit");
25*fdd8201dSApple OSS Distributions close(pipefd[0]);
26*fdd8201dSApple OSS Distributions }
27*fdd8201dSApple OSS Distributions
28*fdd8201dSApple OSS Distributions T_DECL(task_info_28439149, "ensure that task_info has the correct permission",
29*fdd8201dSApple OSS Distributions T_META_CHECK_LEAKS(false), T_META_ASROOT(true))
30*fdd8201dSApple OSS Distributions {
31*fdd8201dSApple OSS Distributions int pipefd[2];
32*fdd8201dSApple OSS Distributions
33*fdd8201dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(pipe(pipefd), "pipe");
34*fdd8201dSApple OSS Distributions
35*fdd8201dSApple OSS Distributions int pid = fork();
36*fdd8201dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
37*fdd8201dSApple OSS Distributions
38*fdd8201dSApple OSS Distributions if (pid == 0) {
39*fdd8201dSApple OSS Distributions do_child(pipefd);
40*fdd8201dSApple OSS Distributions return;
41*fdd8201dSApple OSS Distributions }
42*fdd8201dSApple OSS Distributions
43*fdd8201dSApple OSS Distributions close(pipefd[0]);
44*fdd8201dSApple OSS Distributions
45*fdd8201dSApple OSS Distributions int exit;
46*fdd8201dSApple OSS Distributions mach_msg_type_number_t count;
47*fdd8201dSApple OSS Distributions struct task_basic_info_64 ti;
48*fdd8201dSApple OSS Distributions task_dyld_info_data_t di;
49*fdd8201dSApple OSS Distributions
50*fdd8201dSApple OSS Distributions task_t self = mach_task_self();
51*fdd8201dSApple OSS Distributions task_t other_name;
52*fdd8201dSApple OSS Distributions task_t other;
53*fdd8201dSApple OSS Distributions int ret;
54*fdd8201dSApple OSS Distributions
55*fdd8201dSApple OSS Distributions T_EXPECT_MACH_SUCCESS(task_for_pid(self, pid, &other), NULL);
56*fdd8201dSApple OSS Distributions T_EXPECT_MACH_SUCCESS(task_name_for_pid(self, pid, &other_name), NULL);
57*fdd8201dSApple OSS Distributions
58*fdd8201dSApple OSS Distributions count = TASK_BASIC_INFO_64_COUNT;
59*fdd8201dSApple OSS Distributions T_EXPECT_MACH_SUCCESS(task_info(self, TASK_BASIC_INFO_64, (task_info_t)&ti,
60*fdd8201dSApple OSS Distributions &count), "task_info(self, TASK_BASIC_INFO_64 ...)");
61*fdd8201dSApple OSS Distributions count = TASK_BASIC_INFO_64_COUNT;
62*fdd8201dSApple OSS Distributions T_EXPECT_MACH_SUCCESS(task_info(other, TASK_BASIC_INFO_64, (task_info_t)&ti,
63*fdd8201dSApple OSS Distributions &count), "task_info(other_name, TASK_BASIC_INFO_64 ...)");
64*fdd8201dSApple OSS Distributions count = TASK_BASIC_INFO_64_COUNT;
65*fdd8201dSApple OSS Distributions T_EXPECT_MACH_SUCCESS(task_info(other_name, TASK_BASIC_INFO_64, (task_info_t)&ti,
66*fdd8201dSApple OSS Distributions &count), "task_info(other_name, TASK_BASIC_INFO_64 ...)");
67*fdd8201dSApple OSS Distributions
68*fdd8201dSApple OSS Distributions
69*fdd8201dSApple OSS Distributions count = TASK_DYLD_INFO_COUNT;
70*fdd8201dSApple OSS Distributions T_EXPECT_MACH_SUCCESS(task_info(self, TASK_DYLD_INFO, (task_info_t)&di,
71*fdd8201dSApple OSS Distributions &count), "task_info(self, TASK_DYLD_INFO ...)");
72*fdd8201dSApple OSS Distributions count = TASK_DYLD_INFO_COUNT;
73*fdd8201dSApple OSS Distributions T_EXPECT_MACH_SUCCESS(task_info(other, TASK_DYLD_INFO, (task_info_t)&di,
74*fdd8201dSApple OSS Distributions &count), "task_info(other_name, TASK_DYLD_INFO ...)");
75*fdd8201dSApple OSS Distributions count = TASK_DYLD_INFO_COUNT;
76*fdd8201dSApple OSS Distributions ret = task_info(other_name, TASK_DYLD_INFO, (task_info_t)&di, &count);
77*fdd8201dSApple OSS Distributions T_EXPECT_EQ_INT(ret, KERN_INVALID_ARGUMENT, "task info TASK_DYLD_INFO should fail with mach_port_name");
78*fdd8201dSApple OSS Distributions
79*fdd8201dSApple OSS Distributions exit = 1;
80*fdd8201dSApple OSS Distributions write(pipefd[1], &exit, sizeof(int));
81*fdd8201dSApple OSS Distributions close(pipefd[1]);
82*fdd8201dSApple OSS Distributions
83*fdd8201dSApple OSS Distributions wait(NULL);
84*fdd8201dSApple OSS Distributions }
85