xref: /xnu-8796.101.5/libsyscall/wrappers/_libc_funcptr.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1 /*
2  * Copyright (c) 2010-2014 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 "_libkernel_init.h"
30 #include "strings.h"
31 
32 extern _libkernel_functions_t _libkernel_functions;
33 extern void mig_os_release(void* ptr);
34 
35 __attribute__((visibility("hidden")))
36 void *
malloc(size_t size)37 malloc(size_t size)
38 {
39 	if (_libkernel_functions->malloc) {
40 		return _libkernel_functions->malloc(size);
41 	}
42 	return NULL;
43 }
44 
45 __attribute__((visibility("hidden")))
46 void
free(void * ptr)47 free(void *ptr)
48 {
49 	if (_libkernel_functions->free) {
50 		_libkernel_functions->free(ptr);
51 	}
52 }
53 
54 __attribute__((visibility("hidden")))
55 void *
realloc(void * ptr,size_t size)56 realloc(void *ptr, size_t size)
57 {
58 	if (_libkernel_functions->realloc) {
59 		return _libkernel_functions->realloc(ptr, size);
60 	}
61 	return NULL;
62 }
63 
64 __attribute__((visibility("hidden")))
65 void *
reallocf(void * ptr,size_t size)66 reallocf(void *ptr, size_t size)
67 {
68 	void *nptr = realloc(ptr, size);
69 	if (!nptr && ptr) {
70 		free(ptr);
71 	}
72 	return nptr;
73 }
74 
75 __attribute__((visibility("hidden")))
76 void
_pthread_exit_if_canceled(int error)77 _pthread_exit_if_canceled(int error)
78 {
79 	return _libkernel_functions->_pthread_exit_if_canceled(error);
80 }
81 
82 __attribute__((visibility("hidden")))
83 void
_pthread_set_self(void * ptr)84 _pthread_set_self(void *ptr __attribute__((__unused__)))
85 {
86 }
87 
88 __attribute__((visibility("hidden")))
89 void
_pthread_clear_qos_tsd(mach_port_t thread_port)90 _pthread_clear_qos_tsd(mach_port_t thread_port)
91 {
92 	if (_libkernel_functions->version >= 3 &&
93 	    _libkernel_functions->pthread_clear_qos_tsd) {
94 		return _libkernel_functions->pthread_clear_qos_tsd(thread_port);
95 	}
96 }
97 
98 __attribute__((visibility("hidden")))
99 int
pthread_current_stack_contains_np(const void * addr,size_t len)100 pthread_current_stack_contains_np(const void *addr, size_t len)
101 {
102 	if (_libkernel_functions->version >= 4 &&
103 	    _libkernel_functions->pthread_current_stack_contains_np) {
104 		return _libkernel_functions->pthread_current_stack_contains_np(addr, len);
105 	}
106 
107 	return 0;
108 }
109 
110 /*
111  * Upcalls to optimized libplatform string functions
112  */
113 
114 static const struct _libkernel_string_functions
115     _libkernel_generic_string_functions = {
116 	.bzero = _libkernel_bzero,
117 	.memmove = _libkernel_memmove,
118 	.memset = _libkernel_memset,
119 	.strchr = _libkernel_strchr,
120 	.strcmp = _libkernel_strcmp,
121 	.strcpy = _libkernel_strcpy,
122 	.strlcpy = _libkernel_strlcpy,
123 	.strlen = _libkernel_strlen,
124 };
125 static _libkernel_string_functions_t _libkernel_string_functions =
126     &_libkernel_generic_string_functions;
127 
128 kern_return_t
__libkernel_platform_init(_libkernel_string_functions_t fns)129 __libkernel_platform_init(_libkernel_string_functions_t fns)
130 {
131 	_libkernel_string_functions = fns;
132 	return KERN_SUCCESS;
133 }
134 
135 __attribute__((visibility("hidden")))
136 void
bzero(void * s,size_t n)137 bzero(void *s, size_t n)
138 {
139 	return _libkernel_string_functions->bzero(s, n);
140 }
141 
142 __attribute__((visibility("hidden")))
143 void
__bzero(void * s,size_t n)144 __bzero(void *s, size_t n)
145 {
146 	return _libkernel_string_functions->bzero(s, n);
147 }
148 
149 __attribute__((visibility("hidden")))
150 void *
memchr(const void * s,int c,size_t n)151 memchr(const void *s, int c, size_t n)
152 {
153 	return _libkernel_string_functions->memchr(s, c, n);
154 }
155 
156 __attribute__((visibility("hidden")))
157 int
memcmp(const void * s1,const void * s2,size_t n)158 memcmp(const void *s1, const void *s2, size_t n)
159 {
160 	return _libkernel_string_functions->memcmp(s1, s2, n);
161 }
162 
163 __attribute__((visibility("hidden")))
164 void *
memmove(void * dst,const void * src,size_t n)165 memmove(void *dst, const void *src, size_t n)
166 {
167 	return _libkernel_string_functions->memmove(dst, src, n);
168 }
169 
170 __attribute__((visibility("hidden")))
171 void *
memcpy(void * dst,const void * src,size_t n)172 memcpy(void *dst, const void *src, size_t n)
173 {
174 	return _libkernel_string_functions->memmove(dst, src, n);
175 }
176 
177 __attribute__((visibility("hidden")))
178 void *
memccpy(void * __restrict dst,const void * __restrict src,int c,size_t n)179 memccpy(void *__restrict dst, const void *__restrict src, int c, size_t n)
180 {
181 	return _libkernel_string_functions->memccpy(dst, src, c, n);
182 }
183 
184 __attribute__((visibility("hidden")))
185 void *
memset(void * b,int c,size_t len)186 memset(void *b, int c, size_t len)
187 {
188 	return _libkernel_string_functions->memset(b, c, len);
189 }
190 
191 __attribute__((visibility("hidden")))
192 char *
strchr(const char * s,int c)193 strchr(const char *s, int c)
194 {
195 	return _libkernel_string_functions->strchr(s, c);
196 }
197 
198 __attribute__((visibility("hidden")))
199 char *
index(const char * s,int c)200 index(const char *s, int c)
201 {
202 	return _libkernel_string_functions->strchr(s, c);
203 }
204 
205 __attribute__((visibility("hidden")))
206 int
strcmp(const char * s1,const char * s2)207 strcmp(const char *s1, const char *s2)
208 {
209 	return _libkernel_string_functions->strcmp(s1, s2);
210 }
211 
212 __attribute__((visibility("hidden")))
213 char *
strcpy(char * restrict dst,const char * restrict src)214 strcpy(char * restrict dst, const char * restrict src)
215 {
216 	return _libkernel_string_functions->strcpy(dst, src);
217 }
218 
219 __attribute__((visibility("hidden")))
220 size_t
strlcat(char * restrict dst,const char * restrict src,size_t maxlen)221 strlcat(char * restrict dst, const char * restrict src, size_t maxlen)
222 {
223 	return _libkernel_string_functions->strlcat(dst, src, maxlen);
224 }
225 
226 __attribute__((visibility("hidden")))
227 size_t
strlcpy(char * restrict dst,const char * restrict src,size_t maxlen)228 strlcpy(char * restrict dst, const char * restrict src, size_t maxlen)
229 {
230 	return _libkernel_string_functions->strlcpy(dst, src, maxlen);
231 }
232 
233 __attribute__((visibility("hidden")))
234 size_t
strlen(const char * str)235 strlen(const char *str)
236 {
237 	return _libkernel_string_functions->strlen(str);
238 }
239 
240 __attribute__((visibility("hidden")))
241 int
strncmp(const char * s1,const char * s2,size_t n)242 strncmp(const char *s1, const char *s2, size_t n)
243 {
244 	return _libkernel_string_functions->strncmp(s1, s2, n);
245 }
246 
247 __attribute__((visibility("hidden")))
248 char *
strncpy(char * restrict dst,const char * restrict src,size_t maxlen)249 strncpy(char * restrict dst, const char * restrict src, size_t maxlen)
250 {
251 	return _libkernel_string_functions->strncpy(dst, src, maxlen);
252 }
253 
254 __attribute__((visibility("hidden")))
255 size_t
strnlen(const char * s,size_t maxlen)256 strnlen(const char *s, size_t maxlen)
257 {
258 	return _libkernel_string_functions->strnlen(s, maxlen);
259 }
260 
261 __attribute__((visibility("hidden")))
262 char *
strstr(const char * s,const char * find)263 strstr(const char *s, const char *find)
264 {
265 	return _libkernel_string_functions->strstr(s, find);
266 }
267 
268 /*
269  * mach/mach.h voucher_mach_msg API
270  */
271 
272 static const struct _libkernel_voucher_functions
273     _libkernel_voucher_functions_empty;
274 static _libkernel_voucher_functions_t _libkernel_voucher_functions =
275     &_libkernel_voucher_functions_empty;
276 
277 kern_return_t
__libkernel_voucher_init(_libkernel_voucher_functions_t fns)278 __libkernel_voucher_init(_libkernel_voucher_functions_t fns)
279 {
280 	_libkernel_voucher_functions = fns;
281 	return KERN_SUCCESS;
282 }
283 
284 boolean_t
voucher_mach_msg_set(mach_msg_header_t * msg)285 voucher_mach_msg_set(mach_msg_header_t *msg)
286 {
287 	if (_libkernel_voucher_functions->voucher_mach_msg_set) {
288 		return _libkernel_voucher_functions->voucher_mach_msg_set(msg);
289 	}
290 	return FALSE;
291 }
292 
293 void
voucher_mach_msg_clear(mach_msg_header_t * msg)294 voucher_mach_msg_clear(mach_msg_header_t *msg)
295 {
296 	if (_libkernel_voucher_functions->voucher_mach_msg_clear) {
297 		_libkernel_voucher_functions->voucher_mach_msg_clear(msg);
298 	}
299 }
300 
301 voucher_mach_msg_state_t
voucher_mach_msg_adopt(mach_msg_header_t * msg)302 voucher_mach_msg_adopt(mach_msg_header_t *msg)
303 {
304 	if (_libkernel_voucher_functions->voucher_mach_msg_adopt) {
305 		return _libkernel_voucher_functions->voucher_mach_msg_adopt(msg);
306 	}
307 	return VOUCHER_MACH_MSG_STATE_UNCHANGED;
308 }
309 
310 void
voucher_mach_msg_revert(voucher_mach_msg_state_t state)311 voucher_mach_msg_revert(voucher_mach_msg_state_t state)
312 {
313 	if (_libkernel_voucher_functions->voucher_mach_msg_revert) {
314 		_libkernel_voucher_functions->voucher_mach_msg_revert(state);
315 	}
316 }
317 
318 mach_msg_size_t
voucher_mach_msg_fill_aux(mach_msg_aux_header_t * aux_hdr,mach_msg_size_t sz)319 voucher_mach_msg_fill_aux(mach_msg_aux_header_t *aux_hdr, mach_msg_size_t sz)
320 {
321 	if (_libkernel_voucher_functions->version < 3) {
322 		return 0;
323 	}
324 	if (_libkernel_voucher_functions->voucher_mach_msg_fill_aux) {
325 		return _libkernel_voucher_functions->voucher_mach_msg_fill_aux(aux_hdr, sz);
326 	}
327 	return 0;
328 }
329 
330 boolean_t
voucher_mach_msg_fill_aux_supported(void)331 voucher_mach_msg_fill_aux_supported(void)
332 {
333 	if (_libkernel_voucher_functions->version < 3) {
334 		return FALSE;
335 	}
336 	return NULL != _libkernel_voucher_functions->voucher_mach_msg_fill_aux;
337 }
338