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 "unit_test_utils.h"
30 #include "dt_proxy.h"
31 #include "sys/queue.h"
32 #include <string.h>
33
34 extern int backtrace(void **array, int size);
35 extern char **backtrace_symbols(void *const *array, int size);
36
37 #ifdef __BUILDING_WITH_SANITIZER__
38 extern void __sanitizer_symbolize_pc(void *pc, const char *fmt, char *out_buf, size_t out_buf_size);
39 #endif
40
41 extern int kernel_sysctlbyname(const char *, void *, size_t *, void *, size_t);
42
43 int64_t
run_sysctl_test(const char * t,int64_t value,int argc,char * const * argv)44 run_sysctl_test(const char *t, int64_t value, int argc, char* const* argv)
45 {
46 char name[1024];
47 int64_t result = 0;
48 size_t s = sizeof(value);
49 int rc;
50
51 snprintf(name, sizeof(name), "debug.test.%s", t);
52
53 bool run_real = (argc > 0 && strcmp(argv[0], "real_sysctl") == 0);
54 if (!run_real) {
55 rc = kernel_sysctlbyname(name, &result, &s, &value, s);
56 } else {
57 rc = sysctlbyname(name, &result, &s, &value, s);
58 }
59 PT_QUIET; PT_ASSERT_POSIX_ZERO(rc, "sysctlbyname()");
60 return result;
61 }
62
63 void *
checked_alloc_align(size_t size,size_t align)64 checked_alloc_align(size_t size, size_t align)
65 {
66 void *ptr = NULL;
67 if (align < sizeof(void *)) {
68 ptr = calloc(1, size);
69 PT_QUIET; PT_ASSERT_NOTNULL(ptr, "failed alloc");
70 } else {
71 ptr = aligned_alloc(align, size);
72 PT_QUIET; PT_ASSERT_NOTNULL(ptr, "failed alloc");
73 memset(ptr, 0, size);
74 }
75 return ptr;
76 }
77
78 struct backtrace_array *
collect_current_backtrace(void)79 collect_current_backtrace(void)
80 {
81 struct backtrace_array *bt = malloc(sizeof(struct backtrace_array));
82 bt->nptrs = backtrace(bt->buffer, 100);
83 return bt;
84 }
85
86 void
print_collected_backtrace(struct backtrace_array * bt)87 print_collected_backtrace(struct backtrace_array *bt)
88 {
89 #ifdef __BUILDING_WITH_SANITIZER__
90 // If compiled with any sanitizer, use __sanitizer_symbolize_pc as it gives much more info compared to backtrace_symbols
91 char description[1024];
92 for (int idx = 0; idx < bt->nptrs; idx++) {
93 __sanitizer_symbolize_pc(bt->buffer[idx], "%p %F %L", description,
94 sizeof(description));
95 raw_printf("%d\t%s\n", idx, description);
96 }
97 #else
98 char** strings = backtrace_symbols(bt->buffer, bt->nptrs);
99 PT_QUIET; PT_ASSERT_NOTNULL(strings, "backtrace_symbols");
100 for (int idx = 0; idx < bt->nptrs; idx++) {
101 raw_printf("%s\n", strings[idx]);
102 }
103 free(strings);
104 #endif
105 raw_printf("\n");
106 }
107
108 void
print_current_backtrace(void)109 print_current_backtrace(void)
110 {
111 struct backtrace_array bt;
112 bt.nptrs = backtrace(bt.buffer, 100);
113 print_collected_backtrace(&bt);
114 }
115