9 * DO NOT run this test file by itself.
10 * This test is meant to be invoked by control_port_options darwintest.
12 * If hard enforcement for pinned control port is on, pinned_test_main_thread_mod_ref-5 are
13 * expected to generate fatal EXC_GUARD.
15 * If hard enforcement for immovable control port is on, immovable_test_move_send_task_self-13 are
16 * expected to generate fatal EXC_GUARD.
18 * The type of exception raised (if any) is checked on control_port_options side.
20 #define MAX_TEST_NUM 13
23 attempt_send_immovable_port(mach_port_name_t port
, mach_msg_type_name_t disp
)
27 kr
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &server
);
30 kr
= mach_port_insert_right(mach_task_self(), server
, server
, MACH_MSG_TYPE_MAKE_SEND
);
34 mach_msg_header_t header
;
36 mach_msg_port_descriptor_t desc
;
39 msg
.header
.msgh_remote_port
= server
;
40 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
41 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0) | MACH_MSGH_BITS_COMPLEX
;
42 msg
.header
.msgh_size
= sizeof msg
;
44 msg
.body
.msgh_descriptor_count
= 1;
47 msg
.desc
.disposition
= disp
;
48 msg
.desc
.type
= MACH_MSG_PORT_DESCRIPTOR
;
50 return mach_msg_send(&msg
.header
);
54 pinned_test_main_thread_mod_ref()
56 printf("[Crasher]: Mod refs main thread's self port to 0\n");
57 mach_port_t thread_self
= mach_thread_self();
58 kern_return_t kr
= mach_port_mod_refs(mach_task_self(), thread_self
, MACH_PORT_RIGHT_SEND
, -2);
60 printf("[Crasher pinned_test_main_thread_mod_ref] mach_port_mod_refs returned %s \n.", mach_error_string(kr
));
66 printf("[Crasher]: Deallocate pthread_self\n");
67 mach_port_t th_self
= pthread_mach_thread_np(pthread_self());
68 kern_return_t kr
= mach_port_deallocate(mach_task_self(), th_self
);
70 printf("[Crasher pinned_test_pthread_dealloc] mach_port_deallocate returned %s \n.", mach_error_string(kr
));
75 pinned_test_pthread_dealloc()
77 printf("[Crasher]: Create a pthread and deallocate its self port\n");
79 int ret
= pthread_create(&thread
, NULL
, pthread_run
, NULL
);
81 ret
= pthread_join(thread
, NULL
);
86 pinned_test_task_self_dealloc()
88 printf("[Crasher]: Deallocate mach_task_self twice\n");
89 mach_port_t task_self
= mach_task_self();
90 kern_return_t kr
= mach_port_deallocate(task_self
, task_self
);
92 kr
= mach_port_deallocate(task_self
, task_self
);
94 printf("[Crasher pinned_test_task_self_dealloc] mach_port_deallocate returned %s \n.", mach_error_string(kr
));
98 pinned_test_task_self_mod_ref()
100 printf("[Crasher]: Mod refs mach_task_self() to 0\n");
101 kern_return_t kr
= mach_port_mod_refs(mach_task_self(), mach_task_self(), MACH_PORT_RIGHT_SEND
, -2);
103 printf("[Crasher pinned_test_task_self_mod_ref] mach_port_mod_refs returned %s \n.", mach_error_string(kr
));
107 pinned_test_task_threads_mod_ref()
109 printf("[Crasher]: task_threads should return pinned thread ports. Mod refs them to 0\n");
110 thread_array_t th_list
;
111 mach_msg_type_number_t th_cnt
;
113 mach_port_t th_kp
= mach_thread_self();
114 mach_port_deallocate(mach_task_self(), th_kp
);
116 kr
= task_threads(mach_task_self(), &th_list
, &th_cnt
);
117 mach_port_deallocate(mach_task_self(), th_list
[0]);
119 kr
= mach_port_mod_refs(mach_task_self(), th_list
[0], MACH_PORT_RIGHT_SEND
, -1);
121 printf("[Crasher pinned_test_task_threads_mod_ref] mach_port_mod_refs returned %s \n.", mach_error_string(kr
));
125 immovable_test_move_send_task_self()
128 printf("[Crasher]: Move send mach_task_self_\n");
129 kr
= attempt_send_immovable_port(mach_task_self(), MACH_MSG_TYPE_MOVE_SEND
);
131 printf("[Crasher immovable_test_move_send_task_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
135 immovable_test_copy_send_task_self()
138 printf("[Crasher]: Copy send mach_task_self_\n");
139 kr
= attempt_send_immovable_port(mach_task_self(), MACH_MSG_TYPE_COPY_SEND
);
141 printf("[Crasher immovable_test_copy_send_task_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
145 immovable_test_move_send_thread_self()
148 printf("[Crasher]: Move send main thread's self port\n");
149 kr
= attempt_send_immovable_port(mach_thread_self(), MACH_MSG_TYPE_MOVE_SEND
);
151 printf("[Crasher immovable_test_move_send_thread_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
155 immovable_test_copy_send_thread_self()
159 printf("[Crasher]: Copy send main thread's self port\n");
160 port
= mach_thread_self();
161 kr
= attempt_send_immovable_port(port
, MACH_MSG_TYPE_COPY_SEND
);
162 printf("[Crasher immovable_test_copy_send_thread_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
164 mach_port_deallocate(mach_task_self(), port
);
168 immovable_test_copy_send_task_read()
172 printf("[Crasher]: Copy send task read port\n");
173 kr
= task_get_special_port(mach_task_self(), TASK_READ_PORT
, &port
);
175 kr
= attempt_send_immovable_port(port
, MACH_MSG_TYPE_COPY_SEND
);
176 printf("[Crasher immovable_test_copy_send_task_read] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
178 mach_port_deallocate(mach_task_self(), port
);
182 immovable_test_copy_send_task_inspect()
186 printf("[Crasher]: Move send task inspect port\n");
187 kr
= task_get_special_port(mach_task_self(), TASK_INSPECT_PORT
, &port
);
189 kr
= attempt_send_immovable_port(port
, MACH_MSG_TYPE_MOVE_SEND
);
190 printf("[Crasher immovable_test_copy_send_task_inspect] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
194 immovable_test_move_send_thread_inspect()
198 mach_port_t th_port
= mach_thread_self();
200 printf("[Crasher]: Move send thread inspect port\n");
201 kr
= thread_get_special_port(th_port
, THREAD_INSPECT_PORT
, &port
);
203 kr
= attempt_send_immovable_port(port
, MACH_MSG_TYPE_MOVE_SEND
);
204 printf("[Crasher immovable_test_move_send_thread_inspect] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
206 mach_port_deallocate(mach_task_self(), th_port
);
210 immovable_test_copy_send_thread_read()
214 mach_port_t th_port
= mach_thread_self();
216 printf("[Crasher]: Copy send thread read port\n");
217 kr
= thread_get_special_port(th_port
, THREAD_READ_PORT
, &port
);
219 kr
= attempt_send_immovable_port(port
, MACH_MSG_TYPE_COPY_SEND
);
220 printf("[Crasher immovable_test_copy_send_thread_read] attempt_send_immovable_port returned %s \n.", mach_error_string(kr
));
222 mach_port_deallocate(mach_task_self(), port
);
223 mach_port_deallocate(mach_task_self(), th_port
);
227 main(int argc
, char *argv
[])
229 void (*tests
[MAX_TEST_NUM
])(void) = {
230 pinned_test_main_thread_mod_ref
,
231 pinned_test_pthread_dealloc
,
232 pinned_test_task_self_dealloc
,
233 pinned_test_task_self_mod_ref
,
234 pinned_test_task_threads_mod_ref
,
236 immovable_test_move_send_task_self
,
237 immovable_test_copy_send_task_self
,
238 immovable_test_move_send_thread_self
,
239 immovable_test_copy_send_thread_self
,
240 immovable_test_copy_send_task_read
,
241 immovable_test_copy_send_task_inspect
,
242 immovable_test_move_send_thread_inspect
,
243 immovable_test_copy_send_thread_read
,
245 printf("[Crasher]: My Pid: %d\n", getpid());
248 printf("[Crasher]: Specify a test to run.");
252 int test_num
= atoi(argv
[1]);
254 if (test_num
>= 0 && test_num
< MAX_TEST_NUM
) {
255 (*tests
[test_num
])();
257 printf("[Crasher]: Invalid test num. Exiting...\n");