xref: /xnu-12377.41.6/tests/exc_guard_helper_test_unexpected.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2024 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions /*
30*bbb1b6f9SApple OSS Distributions  * exc_guard_helper_test_unexpected.c
31*bbb1b6f9SApple OSS Distributions  *
32*bbb1b6f9SApple OSS Distributions  * Test the testing helper functions in exc_guard_helper.h.
33*bbb1b6f9SApple OSS Distributions  * The exception handler used by block_raise_exc_guard_of_type()
34*bbb1b6f9SApple OSS Distributions  * should allow other exceptions to continue to a crash.
35*bbb1b6f9SApple OSS Distributions  */
36*bbb1b6f9SApple OSS Distributions 
37*bbb1b6f9SApple OSS Distributions #include "test_utils.h"
38*bbb1b6f9SApple OSS Distributions #include "exc_guard_helper.h"
39*bbb1b6f9SApple OSS Distributions 
40*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
41*bbb1b6f9SApple OSS Distributions #include <sys/wait.h>
42*bbb1b6f9SApple OSS Distributions #include <sys/types.h>
43*bbb1b6f9SApple OSS Distributions #include <sys/sysctl.h>
44*bbb1b6f9SApple OSS Distributions #include <mach/mach.h>
45*bbb1b6f9SApple OSS Distributions #include <mach/mach_vm.h>
46*bbb1b6f9SApple OSS Distributions #include <mach/task_info.h>
47*bbb1b6f9SApple OSS Distributions 
48*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
49*bbb1b6f9SApple OSS Distributions 	T_META_NAMESPACE("xnu"),
50*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
51*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("vm"),
52*bbb1b6f9SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
53*bbb1b6f9SApple OSS Distributions 	T_META_ALL_VALID_ARCHS(true),
54*bbb1b6f9SApple OSS Distributions 
55*bbb1b6f9SApple OSS Distributions 	T_META_IGNORECRASHES(".*exc_guard_helper_test_unexpected.*")
56*bbb1b6f9SApple OSS Distributions 	);
57*bbb1b6f9SApple OSS Distributions 
58*bbb1b6f9SApple OSS Distributions T_DECL(exc_guard_helper_test_unexpected_exc_guard,
59*bbb1b6f9SApple OSS Distributions     "provoke one guard exception type while exc_guard_helper is expecting another")
60*bbb1b6f9SApple OSS Distributions {
61*bbb1b6f9SApple OSS Distributions 	if (process_is_translated()) {
62*bbb1b6f9SApple OSS Distributions 		T_SKIP("VM guard exceptions not supported on Rosetta (rdar://142438840)");
63*bbb1b6f9SApple OSS Distributions 	}
64*bbb1b6f9SApple OSS Distributions 
65*bbb1b6f9SApple OSS Distributions 	pid_t child_pid;
66*bbb1b6f9SApple OSS Distributions 
67*bbb1b6f9SApple OSS Distributions 	if ((child_pid = fork())) {
68*bbb1b6f9SApple OSS Distributions 		/* parent */
69*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "fork");
70*bbb1b6f9SApple OSS Distributions 
71*bbb1b6f9SApple OSS Distributions 		int status;
72*bbb1b6f9SApple OSS Distributions 		pid_t waited_pid;
73*bbb1b6f9SApple OSS Distributions 
74*bbb1b6f9SApple OSS Distributions 		waited_pid = waitpid(child_pid, &status, 0);
75*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(waited_pid, "waitpid");
76*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(waited_pid, child_pid, "waitpid");
77*bbb1b6f9SApple OSS Distributions 
78*bbb1b6f9SApple OSS Distributions 		T_ASSERT_TRUE(WIFSIGNALED(status), "child should have crashed");
79*bbb1b6f9SApple OSS Distributions 		T_ASSERT_EQ(WTERMSIG(status), SIGKILL, "child should have crashed with SIGKILL");
80*bbb1b6f9SApple OSS Distributions 	} else {
81*bbb1b6f9SApple OSS Distributions 		/* child */
82*bbb1b6f9SApple OSS Distributions 		kern_return_t kr;
83*bbb1b6f9SApple OSS Distributions 		task_exc_guard_behavior_t behavior;
84*bbb1b6f9SApple OSS Distributions 		exc_guard_helper_info_t exc_info;
85*bbb1b6f9SApple OSS Distributions 		mach_port_t port;
86*bbb1b6f9SApple OSS Distributions 
87*bbb1b6f9SApple OSS Distributions 		exc_guard_helper_init();
88*bbb1b6f9SApple OSS Distributions 
89*bbb1b6f9SApple OSS Distributions 		/*
90*bbb1b6f9SApple OSS Distributions 		 * set GUARD_TYPE_MACH_PORT to be enabled and fatal.
91*bbb1b6f9SApple OSS Distributions 		 * This child process is expected to crash.
92*bbb1b6f9SApple OSS Distributions 		 */
93*bbb1b6f9SApple OSS Distributions 		kr = task_get_exc_guard_behavior(mach_task_self(), &behavior);
94*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "get old behavior");
95*bbb1b6f9SApple OSS Distributions 		behavior &= ~TASK_EXC_GUARD_MP_ALL;
96*bbb1b6f9SApple OSS Distributions 		behavior |= TASK_EXC_GUARD_MP_DELIVER | TASK_EXC_GUARD_MP_FATAL;
97*bbb1b6f9SApple OSS Distributions 		kr = task_set_exc_guard_behavior(mach_task_self(), behavior);
98*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "set fatal mach port behavior");
99*bbb1b6f9SApple OSS Distributions 
100*bbb1b6f9SApple OSS Distributions 		kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
101*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "new port");
102*bbb1b6f9SApple OSS Distributions 		kr = mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
103*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "make send");
104*bbb1b6f9SApple OSS Distributions 
105*bbb1b6f9SApple OSS Distributions 		/* provoke GUARD_TYPE_MACH_PORT while listening for GUARD_TYPE_VIRT_MEMORY */
106*bbb1b6f9SApple OSS Distributions 
107*bbb1b6f9SApple OSS Distributions 		if (block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY, &exc_info, ^{
108*bbb1b6f9SApple OSS Distributions 			kern_return_t kr;
109*bbb1b6f9SApple OSS Distributions 			T_LOG("CHILD EXPECTED TO CRASH after this guard exception");
110*bbb1b6f9SApple OSS Distributions 			kr = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, INT32_MAX);
111*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_ERROR(kr, KERN_INVALID_VALUE, "add too many send rights");
112*bbb1b6f9SApple OSS Distributions 		})) {
113*bbb1b6f9SApple OSS Distributions 			T_FAIL("Mach port guard exception unexpectedly caught by VM guard exception handler");
114*bbb1b6f9SApple OSS Distributions 		}
115*bbb1b6f9SApple OSS Distributions 
116*bbb1b6f9SApple OSS Distributions 		T_FAIL("expected Mach port guard exception to kill the process");
117*bbb1b6f9SApple OSS Distributions 	}
118*bbb1b6f9SApple OSS Distributions }
119