]> git.saurik.com Git - apple/xnu.git/blame - tests/exception_tests.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / exception_tests.c
CommitLineData
c3c9b80d
A
1#include <darwintest.h>
2#include <pthread/private.h>
3#include <sys/sysctl.h>
4#include "exc_helpers.h"
5
6T_GLOBAL_META(
7 T_META_NAMESPACE("xnu.ipc"),
8 T_META_RUN_CONCURRENTLY(true));
9
10static size_t
11exc_immovable_handler(
12 mach_port_t task,
13 mach_port_t thread,
14 __unused exception_type_t type,
15 __unused mach_exception_data_t codes)
16{
17 T_EXPECT_EQ(task, mach_task_self(), "Received immovable task port");
18 T_EXPECT_EQ(thread, pthread_mach_thread_np(pthread_main_thread_np()),
19 "Received immovable thread port");
20 T_END;
21}
22
23T_DECL(exc_immovable, "Test that exceptions receive immovable ports")
24{
25 mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_ACCESS);
26 uint32_t opts = 0;
27 size_t size = sizeof(&opts);
28 mach_port_t mp;
29 kern_return_t kr;
30
31 T_LOG("Check if task_exc_guard exception has been enabled\n");
32 int ret = sysctlbyname("kern.ipc_control_port_options", &opts, &size, NULL, 0);
33 T_EXPECT_POSIX_SUCCESS(ret, "sysctlbyname(kern.ipc_control_port_options)");
34
35 if ((opts & 0x30) == 0) {
36 T_SKIP("immovable rights aren't enabled");
37 }
38
39 kr = task_get_special_port(mach_task_self(), TASK_KERNEL_PORT, &mp);
40 T_EXPECT_MACH_SUCCESS(kr, "task_get_special_port");
41 T_EXPECT_NE(mp, mach_task_self(), "should receive movable port");
42
43 /*
44 * do not deallocate the port we received on purpose to check
45 * that the exception will not coalesce with the movable port
46 * we have in our space now
47 */
48
49 run_exception_handler(exc_port, exc_immovable_handler);
50 *(void *volatile*)0 = 0;
51}