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