| 1 | /* |
| 2 | * Copyright (c) 2017, 2023 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_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. Please obtain a copy of the License at |
| 10 | * http://www.opensource.apple.com/apsl/ and read it before using this |
| 11 | * file. |
| 12 | * |
| 13 | * The Original Code and all software distributed under the License are |
| 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 18 | * Please see the License for the specific language governing rights and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | * @APPLE_LICENSE_HEADER_END@ |
| 22 | */ |
| 23 | |
| 24 | #ifndef __STRINGS_H_ |
| 25 | # error "Never use <secure/_strings.h> directly; include <strings.h> instead." |
| 26 | #endif |
| 27 | |
| 28 | #ifndef _SECURE__STRINGS_H_ |
| 29 | #define _SECURE__STRINGS_H_ |
| 30 | |
| 31 | #include <sys/cdefs.h> |
| 32 | #include <Availability.h> |
| 33 | #include <secure/_common.h> |
| 34 | |
| 35 | #if _USE_FORTIFY_LEVEL > 0 |
| 36 | |
| 37 | /* bcopy and bzero */ |
| 38 | |
| 39 | /* Removed in Issue 7 */ |
| 40 | #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L |
| 41 | |
| 42 | #ifdef __LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES |
| 43 | |
| 44 | static inline void |
| 45 | __bcopy_ptrcheck(const void *_LIBC_SIZE(__n) __src, void *const _LIBC_SIZE(__n) __darwin_pass_obsz0 __dst, size_t __n) { |
| 46 | 	memmove(__dst, __src, __n); |
| 47 | } |
| 48 | |
| 49 | static inline void |
| 50 | __bzero_ptrcheck(void *const _LIBC_SIZE(__n) __darwin_pass_obsz0 __dst, size_t __n) { |
| 51 | 	memset(__dst, 0, __n); |
| 52 | } |
| 53 | |
| 54 | #define __bcopy_chk_func __bcopy_ptrcheck |
| 55 | #define __bzero_chk_func __bzero_ptrcheck |
| 56 | |
| 57 | #else |
| 58 | |
| 59 | #ifndef __has_builtin |
| 60 | #define __undef__has_builtin |
| 61 | #define __has_builtin(x) defined(__GNUC__) |
| 62 | #endif |
| 63 | |
| 64 | #if __has_builtin(__builtin___memmove_chk) |
| 65 | #define __bcopy_chk_func(src, dst, ...) \ |
| 66 | 	__builtin___memmove_chk(dst, src, __VA_ARGS__, __darwin_obsz0 (dst)) |
| 67 | #endif |
| 68 | |
| 69 | #if __has_builtin(__builtin___memset_chk) |
| 70 | #define __bzero_chk_func(dst, ...) \ |
| 71 | 	__builtin___memset_chk(dst, 0, __VA_ARGS__, __darwin_obsz0 (dst)) |
| 72 | #endif |
| 73 | |
| 74 | #ifdef __undef__has_builtin |
| 75 | #undef __undef__has_builtin |
| 76 | #undef __has_builtin |
| 77 | #endif |
| 78 | |
| 79 | #endif |
| 80 | |
| 81 | #ifdef __bcopy_chk_func |
| 82 | #undef bcopy |
| 83 | /* void	bcopy(const void *src, void *dst, size_t len) */ |
| 84 | #define bcopy(...) __bcopy_chk_func (__VA_ARGS__) |
| 85 | #endif |
| 86 | |
| 87 | #ifdef __bzero_chk_func |
| 88 | #undef bzero |
| 89 | /* void	bzero(void *s, size_t n) */ |
| 90 | #define bzero(...) __bzero_chk_func (__VA_ARGS__) |
| 91 | #endif |
| 92 | |
| 93 | #endif |
| 94 | |
| 95 | #endif /* _USE_FORTIFY_LEVEL > 0 */ |
| 96 | #endif /* _SECURE__STRINGS_H_ */ |