1 /*
2 * Copyright (c) 2000-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 #include "std_safe.h"
30 #include "dt_proxy.h"
31 #include "unit_test_utils.h"
32
33 #include <mach/arm/vm_types.h> // for vm_map_offset_t
34 #include <mach/mach_types.h>
35 #include <kern/assert.h>
36 #include <kern/debug.h>
37
38 // This file is linked to the same .dylib as the XNU code static library
39 // to provide some utilities that XNU code relies on when building for unit-test
40
41 // This symbol normally comes from lastkernelconstructor.o but this object is not linked to libkernel since it is
42 // a zero length symbol, which fails to prelink with ld -r
43 // It is defined here again to resolve the external.
44 // It is used for debugging and in kext related functions which are not needed by the tester
45 void* last_kernel_symbol = NULL;
46
47 // In normal XNU build this is a global of type mach_header_64 that the linker adds. The user-space linker
48 // doesn't add it so it's added here to resolve the external. It's not needed by the tester. see <mach-o/ldsyms.h>
49 int _mh_execute_header;
50
51 // called from fake_init, setup proxies for darwintest asserts
52 struct dt_proxy_callbacks *dt_proxy = NULL;
53 void
set_dt_proxy_attached(struct dt_proxy_callbacks * p)54 set_dt_proxy_attached(struct dt_proxy_callbacks *p)
55 {
56 dt_proxy = p;
57 }
58 struct dt_proxy_callbacks *
get_dt_proxy_attached(void)59 get_dt_proxy_attached(void)
60 {
61 return dt_proxy;
62 }
63
64 // check if panic/assert were expected by the test using T_ASSERT_PANIC
65 struct ut_expected_panic_s ut_expected_panic;
66
67 void
ut_check_expected_panic(const char * panic_str)68 ut_check_expected_panic(const char* panic_str)
69 {
70 if (!ut_expected_panic.expect_panic) {
71 return;
72 }
73 ut_expected_panic.expect_panic = false;
74 if (ut_expected_panic.str_contains != NULL) {
75 if (strstr(panic_str, ut_expected_panic.str_contains) == NULL) {
76 PT_LOG_FMTSTR("Panic with unexpected panic-string, expected: `%s`", ut_expected_panic.str_contains);
77 return;
78 }
79 }
80 PT_LOG("Panic was expected");
81 longjmp(ut_expected_panic.jb, 1);
82 }
83
84 // This function is called on an assert instead of invoking a brk instruction which would trap the kernel
85 __attribute__((noreturn)) void
ut_assert_trap(int code,long a,long b,long c)86 ut_assert_trap(int code, long a, long b, long c)
87 {
88 struct kernel_panic_reason pr = {};
89 if (code == MACH_ASSERT_TRAP_CODE) {
90 panic_assert_format(pr.buf, sizeof(pr.buf), (struct mach_assert_hdr *)a, b, c);
91 PT_LOG_OR_RAW_FMTSTR("%s", pr.buf);
92 } else {
93 snprintf(pr.buf, sizeof(pr.buf), "%x", code);
94 PT_LOG_OR_RAW_FMTSTR("Unknown assert code %s", pr.buf);
95 }
96
97 ut_check_expected_panic(pr.buf); // may not return
98 PT_FAIL("Unexpected assert fail, exiting");
99 abort();
100 }
101
102 // This function can be called from the tested code to force a context switch when using fibers
103 // See the mock implementation
104 void
ut_fibers_ctxswitch(void)105 ut_fibers_ctxswitch(void)
106 {
107 }
108
109 // This function can be called from the tested code to force a context switch to a specific fiber
110 // See the mock implementation
111 void
ut_fibers_ctxswitch_to(int fiber_id)112 ut_fibers_ctxswitch_to(int fiber_id)
113 {
114 }
115
116 // This function can be called from the tested code to get the current fiber id when using fibers, -1 otherwise
117 // See the mock implementation
118 int
ut_fibers_current_id(void)119 ut_fibers_current_id(void)
120 {
121 return -1;
122 }
123
124 static void
fail_not_mocked()125 fail_not_mocked()
126 {
127 PT_FAIL("This function should never be called since it is mocked by the mocks dylib");
128 }
129
130 // This function is changed from being a macro. It needs to have an implementation
131 // in the code attached to XNU so that it can be mocked
132 __mockable void
lock_disable_preemption_for_thread(thread_t t)133 lock_disable_preemption_for_thread(thread_t t)
134 {
135 fail_not_mocked();
136 }
137
138 __mockable __attribute__((const)) thread_t
current_thread_fast(void)139 current_thread_fast(void)
140 {
141 fail_not_mocked();
142 return NULL;
143 }
144
145
146
147 extern vm_map_t vm_map_create_external(
148 pmap_t pmap,
149 vm_map_offset_t min,
150 vm_map_offset_t max,
151 boolean_t pageable);
152
153 // this function alias is done during linking of XNU, which doesn't happen when building the library
154 vm_map_t
vm_map_create(pmap_t pmap,vm_map_offset_t min,vm_map_offset_t max,boolean_t pageable)155 vm_map_create(
156 pmap_t pmap,
157 vm_map_offset_t min,
158 vm_map_offset_t max,
159 boolean_t pageable)
160 {
161 return vm_map_create_external(pmap, min, max, pageable);
162 }
163