1 /*
2 * Copyright (c) 2024 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * try_read_write_test_unexpected.c
31 *
32 * Test the testing helper functions in try_read_write.h.
33 * The exception handler used by try_read_byte/try_write_byte
34 * should allow other exceptions to continue to a crash.
35 */
36
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <darwintest.h>
40 #include <sys/wait.h>
41 #include <mach/mach.h>
42 #include <mach/mach_vm.h>
43
44 #include "try_read_write.h"
45
46 T_GLOBAL_META(
47 T_META_NAMESPACE("xnu"),
48 T_META_RADAR_COMPONENT_NAME("xnu"),
49 T_META_RADAR_COMPONENT_VERSION("vm"),
50 T_META_RUN_CONCURRENTLY(true),
51 T_META_ALL_VALID_ARCHS(true),
52
53 /* these tests are expected to crash */
54 T_META_IGNORECRASHES(".*try_read_write_test_unexpected.*")
55 );
56
57 static void
install_exception_handler(void)58 install_exception_handler(void)
59 {
60 kern_return_t kr;
61 bool result;
62
63 result = try_write_byte(0, 0, &kr);
64 T_QUIET; T_ASSERT_EQ(result, false, "try_write_byte to NULL");
65 T_QUIET; T_ASSERT_EQ(kr, KERN_INVALID_ADDRESS, "try_write_byte to NULL");
66 }
67
68 static void
69 test_crasher(void (^crashing_block)(void))
70 {
71 pid_t child_pid;
72 if ((child_pid = fork())) {
73 /* parent */
74 int status;
75 int err = waitpid(child_pid, &status, 0);
76 T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
77 T_EXPECT_TRUE(WIFSIGNALED(status), "parent: child process should crash");
78 } else {
79 /* child */
80 T_LOG("-- Calling try_write_byte() to install exception handlers --");
81 install_exception_handler();
82 T_LOG("-- The next exception should crash --");
83 crashing_block();
84 T_FAIL("child: process should have crashed");
85 }
86 }
87
88 static void __attribute__((noinline))
THIS_IS_EXPECTED_TO_CRASH_EXC_BAD_ACCESS(void)89 THIS_IS_EXPECTED_TO_CRASH_EXC_BAD_ACCESS(void)
90 {
91 *(volatile int *)0 = 1;
92 }
93
94 static void __attribute__((noinline))
THIS_IS_EXPECTED_TO_CRASH_BUILTIN_TRAP(void)95 THIS_IS_EXPECTED_TO_CRASH_BUILTIN_TRAP(void)
96 {
97 __builtin_trap();
98 }
99
100
101 T_DECL(try_read_write_unexpected_bad_access,
102 "test an unrelated EXC_BAD_ACCESS exception "
103 "with the try_read_write exception handler in place")
104 {
105 test_crasher(^{
106 /*
107 * Provoke EXC_BAD_ACCESS outside try_read_byte and try_write_byte.
108 * The try_read_write exception handler should catch and rethrow it.
109 */
110 THIS_IS_EXPECTED_TO_CRASH_EXC_BAD_ACCESS();
111 });
112 }
113
114
115 T_DECL(try_read_write_unexpected_trap,
116 "test an unrelated non-EXC_BAD_ACCESS exception "
117 "with the try_read_write exception handler in place")
118 {
119 test_crasher(^{
120 /*
121 * Provoke a non-EXC_BAD_ACCESS exception outside of try_read_byte and try_write_byte.
122 * The try_read_write exception handler should not catch it.
123 */
124 THIS_IS_EXPECTED_TO_CRASH_BUILTIN_TRAP();
125 });
126 }
127