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 *
malloc_type_malloc(size_t size,malloc_type_id_t type_id)77 malloc_type_malloc(size_t size, malloc_type_id_t type_id)
78 {
79 if (_libkernel_functions->version >= 5) {
80 if (_libkernel_functions->malloc_type_malloc) {
81 return _libkernel_functions->malloc_type_malloc(size, type_id);
82 }
83 }
84 return malloc(size);
85 }
86
87 __attribute__((visibility("hidden")))
88 void
malloc_type_free(void * ptr,malloc_type_id_t type_id)89 malloc_type_free(void *ptr, malloc_type_id_t type_id)
90 {
91 if (_libkernel_functions->version >= 5) {
92 if (_libkernel_functions->malloc_type_free) {
93 return _libkernel_functions->malloc_type_free(ptr, type_id);
94 }
95 }
96 return free(ptr);
97 }
98
99 __attribute__((visibility("hidden")))
100 void *
malloc_type_realloc(void * ptr,size_t size,malloc_type_id_t type_id)101 malloc_type_realloc(void *ptr, size_t size, malloc_type_id_t type_id)
102 {
103 if (_libkernel_functions->version >= 5) {
104 if (_libkernel_functions->malloc_type_realloc) {
105 return _libkernel_functions->malloc_type_realloc(ptr, size, type_id);
106 }
107 }
108 return realloc(ptr, size);
109 }
110
111 __attribute__((visibility("hidden")))
112 void *
malloc_type_reallocf(void * ptr,size_t size,malloc_type_id_t type_id)113 malloc_type_reallocf(void *ptr, size_t size, malloc_type_id_t type_id)
114 {
115 void *nptr = malloc_type_realloc(ptr, size, type_id);
116 if (!nptr && ptr) {
117 free(ptr);
118 }
119 return nptr;
120 }
121
122 __attribute__((visibility("hidden")))
123 void
_pthread_exit_if_canceled(int error)124 _pthread_exit_if_canceled(int error)
125 {
126 return _libkernel_functions->_pthread_exit_if_canceled(error);
127 }
128
129 __attribute__((visibility("hidden")))
130 void
_pthread_set_self(void * ptr)131 _pthread_set_self(void *ptr __attribute__((__unused__)))
132 {
133 }
134
135 __attribute__((visibility("hidden")))
136 void
_pthread_clear_qos_tsd(mach_port_t thread_port)137 _pthread_clear_qos_tsd(mach_port_t thread_port)
138 {
139 if (_libkernel_functions->version >= 3 &&
140 _libkernel_functions->pthread_clear_qos_tsd) {
141 return _libkernel_functions->pthread_clear_qos_tsd(thread_port);
142 }
143 }
144
145 __attribute__((visibility("hidden")))
146 int
pthread_current_stack_contains_np(const void * addr,size_t len)147 pthread_current_stack_contains_np(const void *addr, size_t len)
148 {
149 if (_libkernel_functions->version >= 4 &&
150 _libkernel_functions->pthread_current_stack_contains_np) {
151 return _libkernel_functions->pthread_current_stack_contains_np(addr, len);
152 }
153
154 return 0;
155 }
156
157 /*
158 * Upcalls to optimized libplatform string functions
159 */
160
161 static const struct _libkernel_string_functions
162 _libkernel_generic_string_functions = {
163 .bzero = _libkernel_bzero,
164 .memmove = _libkernel_memmove,
165 .memset = _libkernel_memset,
166 .strchr = _libkernel_strchr,
167 .strcmp = _libkernel_strcmp,
168 .strcpy = _libkernel_strcpy,
169 .strlcpy = _libkernel_strlcpy,
170 .strlen = _libkernel_strlen,
171 };
172 static _libkernel_string_functions_t _libkernel_string_functions =
173 &_libkernel_generic_string_functions;
174
175 kern_return_t
__libkernel_platform_init(_libkernel_string_functions_t fns)176 __libkernel_platform_init(_libkernel_string_functions_t fns)
177 {
178 _libkernel_string_functions = fns;
179 return KERN_SUCCESS;
180 }
181
182 __attribute__((visibility("hidden")))
183 void
bzero(void * s,size_t n)184 bzero(void *s, size_t n)
185 {
186 return _libkernel_string_functions->bzero(s, n);
187 }
188
189 __attribute__((visibility("hidden")))
190 void
__bzero(void * s,size_t n)191 __bzero(void *s, size_t n)
192 {
193 return _libkernel_string_functions->bzero(s, n);
194 }
195
196 __attribute__((visibility("hidden")))
197 void *
memchr(const void * s,int c,size_t n)198 memchr(const void *s, int c, size_t n)
199 {
200 return _libkernel_string_functions->memchr(s, c, n);
201 }
202
203 __attribute__((visibility("hidden")))
204 int
memcmp(const void * s1,const void * s2,size_t n)205 memcmp(const void *s1, const void *s2, size_t n)
206 {
207 return _libkernel_string_functions->memcmp(s1, s2, n);
208 }
209
210 __attribute__((visibility("hidden")))
211 void *
memmove(void * dst,const void * src,size_t n)212 memmove(void *dst, const void *src, size_t n)
213 {
214 return _libkernel_string_functions->memmove(dst, src, n);
215 }
216
217 __attribute__((visibility("hidden")))
218 void *
memcpy(void * dst,const void * src,size_t n)219 memcpy(void *dst, const void *src, size_t n)
220 {
221 return _libkernel_string_functions->memmove(dst, src, n);
222 }
223
224 __attribute__((visibility("hidden")))
225 void *
memccpy(void * __restrict dst,const void * __restrict src,int c,size_t n)226 memccpy(void *__restrict dst, const void *__restrict src, int c, size_t n)
227 {
228 return _libkernel_string_functions->memccpy(dst, src, c, n);
229 }
230
231 __attribute__((visibility("hidden")))
232 void *
memset(void * b,int c,size_t len)233 memset(void *b, int c, size_t len)
234 {
235 return _libkernel_string_functions->memset(b, c, len);
236 }
237
238 __attribute__((visibility("hidden")))
239 char *
strchr(const char * s,int c)240 strchr(const char *s, int c)
241 {
242 return _libkernel_string_functions->strchr(s, c);
243 }
244
245 __attribute__((visibility("hidden")))
246 char *
index(const char * s,int c)247 index(const char *s, int c)
248 {
249 return _libkernel_string_functions->strchr(s, c);
250 }
251
252 __attribute__((visibility("hidden")))
253 int
strcmp(const char * s1,const char * s2)254 strcmp(const char *s1, const char *s2)
255 {
256 return _libkernel_string_functions->strcmp(s1, s2);
257 }
258
259 __attribute__((visibility("hidden")))
260 char *
strcpy(char * restrict dst,const char * restrict src)261 strcpy(char * restrict dst, const char * restrict src)
262 {
263 return _libkernel_string_functions->strcpy(dst, src);
264 }
265
266 __attribute__((visibility("hidden")))
267 size_t
strlcat(char * restrict dst,const char * restrict src,size_t maxlen)268 strlcat(char * restrict dst, const char * restrict src, size_t maxlen)
269 {
270 return _libkernel_string_functions->strlcat(dst, src, maxlen);
271 }
272
273 __attribute__((visibility("hidden")))
274 size_t
strlcpy(char * restrict dst,const char * restrict src,size_t maxlen)275 strlcpy(char * restrict dst, const char * restrict src, size_t maxlen)
276 {
277 return _libkernel_string_functions->strlcpy(dst, src, maxlen);
278 }
279
280 __attribute__((visibility("hidden")))
281 size_t
strlen(const char * str)282 strlen(const char *str)
283 {
284 return _libkernel_string_functions->strlen(str);
285 }
286
287 __attribute__((visibility("hidden")))
288 int
strncmp(const char * s1,const char * s2,size_t n)289 strncmp(const char *s1, const char *s2, size_t n)
290 {
291 return _libkernel_string_functions->strncmp(s1, s2, n);
292 }
293
294 __attribute__((visibility("hidden")))
295 char *
strncpy(char * restrict dst,const char * restrict src,size_t maxlen)296 strncpy(char * restrict dst, const char * restrict src, size_t maxlen)
297 {
298 return _libkernel_string_functions->strncpy(dst, src, maxlen);
299 }
300
301 __attribute__((visibility("hidden")))
302 size_t
strnlen(const char * s,size_t maxlen)303 strnlen(const char *s, size_t maxlen)
304 {
305 return _libkernel_string_functions->strnlen(s, maxlen);
306 }
307
308 __attribute__((visibility("hidden")))
309 char *
strstr(const char * s,const char * find)310 strstr(const char *s, const char *find)
311 {
312 return _libkernel_string_functions->strstr(s, find);
313 }
314
315 /*
316 * mach/mach.h voucher_mach_msg API
317 */
318
319 static const struct _libkernel_voucher_functions
320 _libkernel_voucher_functions_empty;
321 static _libkernel_voucher_functions_t _libkernel_voucher_functions =
322 &_libkernel_voucher_functions_empty;
323
324 kern_return_t
__libkernel_voucher_init(_libkernel_voucher_functions_t fns)325 __libkernel_voucher_init(_libkernel_voucher_functions_t fns)
326 {
327 _libkernel_voucher_functions = fns;
328 return KERN_SUCCESS;
329 }
330
331 boolean_t
voucher_mach_msg_set(mach_msg_header_t * msg)332 voucher_mach_msg_set(mach_msg_header_t *msg)
333 {
334 if (_libkernel_voucher_functions->voucher_mach_msg_set) {
335 return _libkernel_voucher_functions->voucher_mach_msg_set(msg);
336 }
337 return FALSE;
338 }
339
340 void
voucher_mach_msg_clear(mach_msg_header_t * msg)341 voucher_mach_msg_clear(mach_msg_header_t *msg)
342 {
343 if (_libkernel_voucher_functions->voucher_mach_msg_clear) {
344 _libkernel_voucher_functions->voucher_mach_msg_clear(msg);
345 }
346 }
347
348 voucher_mach_msg_state_t
voucher_mach_msg_adopt(mach_msg_header_t * msg)349 voucher_mach_msg_adopt(mach_msg_header_t *msg)
350 {
351 if (_libkernel_voucher_functions->voucher_mach_msg_adopt) {
352 return _libkernel_voucher_functions->voucher_mach_msg_adopt(msg);
353 }
354 return VOUCHER_MACH_MSG_STATE_UNCHANGED;
355 }
356
357 void
voucher_mach_msg_revert(voucher_mach_msg_state_t state)358 voucher_mach_msg_revert(voucher_mach_msg_state_t state)
359 {
360 if (_libkernel_voucher_functions->voucher_mach_msg_revert) {
361 _libkernel_voucher_functions->voucher_mach_msg_revert(state);
362 }
363 }
364
365 mach_msg_size_t
voucher_mach_msg_fill_aux(mach_msg_aux_header_t * aux_hdr,mach_msg_size_t sz)366 voucher_mach_msg_fill_aux(mach_msg_aux_header_t *aux_hdr, mach_msg_size_t sz)
367 {
368 if (_libkernel_voucher_functions->version < 3) {
369 return 0;
370 }
371 if (_libkernel_voucher_functions->voucher_mach_msg_fill_aux) {
372 return _libkernel_voucher_functions->voucher_mach_msg_fill_aux(aux_hdr, sz);
373 }
374 return 0;
375 }
376
377 boolean_t
voucher_mach_msg_fill_aux_supported(void)378 voucher_mach_msg_fill_aux_supported(void)
379 {
380 if (_libkernel_voucher_functions->version < 3) {
381 return FALSE;
382 }
383 return NULL != _libkernel_voucher_functions->voucher_mach_msg_fill_aux;
384 }
385