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