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 #pragma once 30 31 // check that we're being compiled from the unit-tests makefile and that UT_MODULE was found 32 #if defined(UT_MODULE) && (UT_MODULE == -1) 33 #error "UT_MODULE not defined, did you forget to add a `#define UT_MODULE <module>` in your test?" 34 #endif 35 36 #include <stdint.h> 37 #include <stddef.h> 38 #include <stdarg.h> 39 #include <stdbool.h> 40 #include <mach/boolean.h> 41 42 // This file defines some function from libc that are used by the mocks. 43 // The testers are built with -nostdlibinc so the system headers folders (from the SDK) are not available when 44 // compiling these files. 45 // Having system headers folders available for the includes search would create conflicts when XNU code includes 46 // headers like string.h, sys/types.h etc' (which need to come from XNU). 47 // This is why it's not possible do add headers like <stdlib.h> here. 48 // Furthermore, even if we could have included system headers like stdlib.h, Having that and XNU headers in the same 49 // translation unit would have created conflicts with XNU-defined types which have the same name but a different type 50 // from the system includes definition. 51 // For example, every type in mach/mach_types.h is typedef'd as mach_port_t in the system headers. 52 53 // from stdlib.h 54 extern void *calloc(size_t count, size_t size); 55 extern void *aligned_alloc(size_t alignment, size_t size); 56 extern void free(void *ptr); 57 extern int rand(void); 58 extern void srand(unsigned seed); 59 extern __attribute__((noreturn)) void exit(int status); 60 extern __attribute__((noreturn)) void abort(void); 61 extern char * getenv(const char *name); 62 extern int atoi(const char *str); 63 64 extern void * calloc(size_t count, size_t size); 65 extern void free(void *ptr); 66 extern void * malloc(size_t size); 67 extern void *realloc(void *ptr, size_t size); 68 69 // from stdio.h 70 extern int vsnprintf(char * str, size_t size, const char * format, va_list ap); 71 extern int snprintf(char * str, size_t size, const char * format, ...); 72 73 // from string.h 74 extern void *memcpy(void *restrict dst, const void *restrict src, size_t n); 75 extern void *memset(void *b, int c, size_t len); 76 extern int strcmp(const char *s1, const char *s2); 77 extern char *strstr(const char *haystack, const char *needle); 78 extern char *strdup(const char *s1); 79 80 // from unistd.h 81 extern size_t write(int fildes, const void *buf, size_t nbyte); 82 #define STDOUT_FILENO 1 83 84 // from pthread.h 85 #if defined(__LP64__) 86 #define __PTHREAD_SIZE__ 8176 87 #define __PTHREAD_ATTR_SIZE__ 56 88 #define __PTHREAD_MUTEX_SIZE__ 56 89 #define __PTHREAD_MUTEXATTR_SIZE__ 8 90 #define __PTHREAD_COND_SIZE__ 40 91 #define __PTHREAD_CONDATTR_SIZE__ 8 92 #else // !__LP64__ 93 #define __PTHREAD_SIZE__ 4088 94 #define __PTHREAD_ATTR_SIZE__ 36 95 #define __PTHREAD_MUTEX_SIZE__ 40 96 #define __PTHREAD_MUTEXATTR_SIZE__ 8 97 #define __PTHREAD_COND_SIZE__ 24 98 #define __PTHREAD_CONDATTR_SIZE__ 4 99 #endif 100 #if defined(__arm__) || defined(__arm64__) 101 #define PTHREAD_STACK_MIN 16384 102 #else 103 #define PTHREAD_STACK_MIN 8192 104 #endif 105 106 struct _opaque_pthread_attr_t { 107 long __sig; 108 char __opaque[__PTHREAD_ATTR_SIZE__]; 109 }; 110 struct _opaque_pthread_t { 111 long __sig; 112 void *__cleanup_stack; 113 char __opaque[__PTHREAD_SIZE__]; 114 }; 115 struct _opaque_pthread_mutex_t { 116 long __sig; 117 char __opaque[__PTHREAD_MUTEX_SIZE__]; 118 }; 119 struct _opaque_pthread_mutexattr_t { 120 long __sig; 121 char __opaque[__PTHREAD_MUTEXATTR_SIZE__]; 122 }; 123 struct _opaque_pthread_cond_t { 124 long __sig; 125 char __opaque[__PTHREAD_COND_SIZE__]; 126 }; 127 struct _opaque_pthread_condattr_t { 128 long __sig; 129 char __opaque[__PTHREAD_CONDATTR_SIZE__]; 130 }; 131 typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t; 132 typedef struct _opaque_pthread_t *__darwin_pthread_t; 133 typedef unsigned long __darwin_pthread_key_t; 134 typedef struct _opaque_pthread_mutex_t __darwin_pthread_mutex_t; 135 typedef struct _opaque_pthread_mutexattr_t __darwin_pthread_mutexattr_t; 136 typedef struct _opaque_pthread_cond_t __darwin_pthread_cond_t; 137 typedef struct _opaque_pthread_condattr_t __darwin_pthread_condattr_t; 138 139 typedef __darwin_pthread_t pthread_t; 140 typedef __darwin_pthread_attr_t pthread_attr_t; 141 typedef __darwin_pthread_key_t pthread_key_t; 142 typedef __darwin_pthread_mutex_t pthread_mutex_t; 143 typedef __darwin_pthread_mutexattr_t pthread_mutexattr_t; 144 typedef __darwin_pthread_cond_t pthread_cond_t; 145 typedef __darwin_pthread_condattr_t pthread_condattr_t; 146 147 extern pthread_t pthread_self(void); 148 extern int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); 149 extern int pthread_join(pthread_t thread, void **value_ptr); 150 extern int pthread_setspecific(pthread_key_t key, const void *value); 151 extern void *pthread_getspecific(pthread_key_t key); 152 extern int pthread_key_create(pthread_key_t *key, void (*destructor)(void *)); 153 extern int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 154 extern int pthread_mutex_destroy(pthread_mutex_t *mutex); 155 extern int pthread_mutex_lock(pthread_mutex_t *mutex); 156 extern int pthread_mutex_trylock(pthread_mutex_t *mutex); 157 extern int pthread_mutex_unlock(pthread_mutex_t *mutex); 158 extern int pthread_mutexattr_init(pthread_mutexattr_t *attr); 159 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); 160 extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); 161 #define PTHREAD_MUTEX_RECURSIVE 2 162 extern int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); 163 extern int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *mutex); 164 extern int pthread_cond_signal(pthread_cond_t *cond); 165 extern int pthread_cond_broadcast(pthread_cond_t *cond); 166 extern int pthread_cond_destroy(pthread_cond_t *cond); 167 extern size_t pthread_get_stacksize_np(pthread_t); 168 extern void* pthread_get_stackaddr_np(pthread_t); 169 170 // errno.h 171 #define EBUSY 16 172 173 extern int * __error(void); 174 #define errno (*__error()) 175 176 // sysctl.h 177 extern int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen); 178 179 // from setjmp.h 180 #if defined(__x86_64__) 181 # define _JBLEN ((9 * 2) + 3 + 16) 182 #elif defined(__i386__) 183 # define _JBLEN (18) 184 #elif defined(__arm__) && !defined(__ARM_ARCH_7K__) 185 # define _JBLEN (10 + 16 + 2) 186 #elif defined(__arm64__) || defined(__ARM_ARCH_7K__) 187 # define _JBLEN ((14 + 8 + 2) * 2) 188 #else 189 # error Undefined platform for setjmp 190 #endif 191 192 typedef int jmp_buf[_JBLEN]; 193 194 extern int setjmp(jmp_buf); 195 extern void longjmp(jmp_buf, int) __attribute__((__noreturn__)); 196 197 // from time.h 198 #include <sys/time.h> 199 extern time_t time(time_t *tloc); 200