xref: /xnu-8792.41.9/osfmk/libsa/string.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions  *
15*5c2921b0SApple OSS Distributions  * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions  *
18*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions  * limitations under the License.
25*5c2921b0SApple OSS Distributions  *
26*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions /*
29*5c2921b0SApple OSS Distributions  * NOTICE: This file was modified by McAfee Research in 2004 to introduce
30*5c2921b0SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
31*5c2921b0SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
32*5c2921b0SApple OSS Distributions  * Version 2.0.
33*5c2921b0SApple OSS Distributions  */
34*5c2921b0SApple OSS Distributions /*
35*5c2921b0SApple OSS Distributions  * HISTORY
36*5c2921b0SApple OSS Distributions  * @OSF_COPYRIGHT@
37*5c2921b0SApple OSS Distributions  */
38*5c2921b0SApple OSS Distributions #ifndef _STRING_H_
39*5c2921b0SApple OSS Distributions #define _STRING_H_      1
40*5c2921b0SApple OSS Distributions 
41*5c2921b0SApple OSS Distributions #ifdef MACH_KERNEL_PRIVATE
42*5c2921b0SApple OSS Distributions #include <types.h>
43*5c2921b0SApple OSS Distributions #else
44*5c2921b0SApple OSS Distributions #include <sys/types.h>
45*5c2921b0SApple OSS Distributions #endif
46*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
47*5c2921b0SApple OSS Distributions 
48*5c2921b0SApple OSS Distributions #ifdef __cplusplus
49*5c2921b0SApple OSS Distributions extern "C" {
50*5c2921b0SApple OSS Distributions #endif
51*5c2921b0SApple OSS Distributions 
52*5c2921b0SApple OSS Distributions #ifndef NULL
53*5c2921b0SApple OSS Distributions #if defined (__cplusplus)
54*5c2921b0SApple OSS Distributions #if __cplusplus >= 201103L
55*5c2921b0SApple OSS Distributions #define NULL nullptr
56*5c2921b0SApple OSS Distributions #else
57*5c2921b0SApple OSS Distributions #define NULL 0
58*5c2921b0SApple OSS Distributions #endif
59*5c2921b0SApple OSS Distributions #else
60*5c2921b0SApple OSS Distributions #define NULL ((void *)0)
61*5c2921b0SApple OSS Distributions #endif
62*5c2921b0SApple OSS Distributions #endif
63*5c2921b0SApple OSS Distributions 
64*5c2921b0SApple OSS Distributions extern void     *memcpy(void *dst __sized_by(n), const void *src __sized_by(n), size_t n);
65*5c2921b0SApple OSS Distributions extern int      memcmp(const void *s1 __sized_by(n), const void *s2 __sized_by(n), size_t n) __stateful_pure;
66*5c2921b0SApple OSS Distributions extern void     *memmove(void *dst __sized_by(n), const void *src __sized_by(n), size_t n);
67*5c2921b0SApple OSS Distributions extern void     *memset(void *s __sized_by(n), int, size_t n);
68*5c2921b0SApple OSS Distributions extern int      memset_s(void *s __sized_by(smax), size_t smax, int c, size_t n);
69*5c2921b0SApple OSS Distributions 
70*5c2921b0SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
71*5c2921b0SApple OSS Distributions /* memcmp_zero_ptr_aligned() checks string s of n bytes contains all zeros.
72*5c2921b0SApple OSS Distributions  * Address and size of the string s must be pointer-aligned.
73*5c2921b0SApple OSS Distributions  * Return 0 if true, 1 otherwise. Also return 0 if n is 0.
74*5c2921b0SApple OSS Distributions  */
75*5c2921b0SApple OSS Distributions extern unsigned long memcmp_zero_ptr_aligned(const void *s __sized_by(n), size_t n) __stateful_pure;
76*5c2921b0SApple OSS Distributions #endif
77*5c2921b0SApple OSS Distributions 
78*5c2921b0SApple OSS Distributions extern size_t   strlen(const char *) __stateful_pure;
79*5c2921b0SApple OSS Distributions extern size_t   strnlen(const char *, size_t) __stateful_pure;
80*5c2921b0SApple OSS Distributions 
81*5c2921b0SApple OSS Distributions /* strcpy() and strncpy() are deprecated. Please use strlcpy() instead. */
82*5c2921b0SApple OSS Distributions __kpi_deprecated_arm64_macos_unavailable
83*5c2921b0SApple OSS Distributions extern char     *strcpy(char *, const char *) __deprecated;
84*5c2921b0SApple OSS Distributions 
85*5c2921b0SApple OSS Distributions __kpi_deprecated_arm64_macos_unavailable
86*5c2921b0SApple OSS Distributions extern char     *strncpy(char *, const char *, size_t);
87*5c2921b0SApple OSS Distributions 
88*5c2921b0SApple OSS Distributions /* strcat() and strncat() are deprecated. Please use strlcat() instead. */
89*5c2921b0SApple OSS Distributions __kpi_deprecated_arm64_macos_unavailable
90*5c2921b0SApple OSS Distributions extern char     *strcat(char *, const char *) __deprecated;
91*5c2921b0SApple OSS Distributions 
92*5c2921b0SApple OSS Distributions __kpi_deprecated_arm64_macos_unavailable
93*5c2921b0SApple OSS Distributions extern char     *strncat(char *, const char *, size_t);
94*5c2921b0SApple OSS Distributions 
95*5c2921b0SApple OSS Distributions extern int      strcmp(const char *, const char *) __stateful_pure;
96*5c2921b0SApple OSS Distributions extern int      strncmp(const char *, const char *, size_t) __stateful_pure;
97*5c2921b0SApple OSS Distributions 
98*5c2921b0SApple OSS Distributions extern size_t   strlcpy(char *, const char *, size_t);
99*5c2921b0SApple OSS Distributions extern size_t   strlcat(char *, const char *, size_t);
100*5c2921b0SApple OSS Distributions 
101*5c2921b0SApple OSS Distributions extern int      strcasecmp(const char *s1, const char *s2) __stateful_pure;
102*5c2921b0SApple OSS Distributions extern int      strncasecmp(const char *s1, const char *s2, size_t n) __stateful_pure;
103*5c2921b0SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
104*5c2921b0SApple OSS Distributions extern const char     *strnstr(const char *s, const char *find, size_t slen) __stateful_pure;
105*5c2921b0SApple OSS Distributions #else
106*5c2921b0SApple OSS Distributions extern char     *strnstr(const char *s, const char *find, size_t slen) __stateful_pure;
107*5c2921b0SApple OSS Distributions #endif
108*5c2921b0SApple OSS Distributions extern char     *strchr(const char *s, int c) __stateful_pure;
109*5c2921b0SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
110*5c2921b0SApple OSS Distributions extern char     *strrchr(const char *s, int c) __stateful_pure;
111*5c2921b0SApple OSS Distributions #endif
112*5c2921b0SApple OSS Distributions #if XNU_PLATFORM_MacOSX
113*5c2921b0SApple OSS Distributions #ifndef KERNEL_PRIVATE
114*5c2921b0SApple OSS Distributions extern char     *STRDUP(const char *, int);
115*5c2921b0SApple OSS Distributions #endif
116*5c2921b0SApple OSS Distributions #endif /* XNU_PLATFORM_MacOSX */
117*5c2921b0SApple OSS Distributions extern int      strprefix(const char *s1, const char *s2) __stateful_pure;
118*5c2921b0SApple OSS Distributions 
119*5c2921b0SApple OSS Distributions extern int      bcmp(const void *s1 __sized_by(n), const void *s2 __sized_by(n), size_t n) __stateful_pure;
120*5c2921b0SApple OSS Distributions extern void     bcopy(const void *src __sized_by(n), void *dst __sized_by(n), size_t n);
121*5c2921b0SApple OSS Distributions extern void     bzero(void *s __sized_by(n), size_t n);
122*5c2921b0SApple OSS Distributions extern int      timingsafe_bcmp(const void *b1 __sized_by(n), const void *b2 __sized_by(n), size_t n);
123*5c2921b0SApple OSS Distributions 
124*5c2921b0SApple OSS Distributions #ifdef PRIVATE
125*5c2921b0SApple OSS Distributions #include <san/memintrinsics.h>
126*5c2921b0SApple OSS Distributions #endif
127*5c2921b0SApple OSS Distributions 
128*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin_dynamic_object_size)
129*5c2921b0SApple OSS Distributions #define XNU_BOS __builtin_dynamic_object_size
130*5c2921b0SApple OSS Distributions #else
131*5c2921b0SApple OSS Distributions #define XNU_BOS __builtin_object_size
132*5c2921b0SApple OSS Distributions #endif
133*5c2921b0SApple OSS Distributions 
134*5c2921b0SApple OSS Distributions /* __nochk_ functions for opting out of type 1 bounds checking */
135*5c2921b0SApple OSS Distributions __attribute__((always_inline)) static inline void *
__nochk_memcpy(void * dest __sized_by (len),const void * src __sized_by (len),size_t len)136*5c2921b0SApple OSS Distributions __nochk_memcpy(void *dest __sized_by(len), const void *src __sized_by(len), size_t len)
137*5c2921b0SApple OSS Distributions {
138*5c2921b0SApple OSS Distributions 	return __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, 0));
139*5c2921b0SApple OSS Distributions }
140*5c2921b0SApple OSS Distributions __attribute__((always_inline)) static inline void *
__nochk_memmove(void * dest __sized_by (len),const void * src __sized_by (len),size_t len)141*5c2921b0SApple OSS Distributions __nochk_memmove(void *dest __sized_by(len), const void *src __sized_by(len), size_t len)
142*5c2921b0SApple OSS Distributions {
143*5c2921b0SApple OSS Distributions 	return __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
144*5c2921b0SApple OSS Distributions }
145*5c2921b0SApple OSS Distributions __attribute__((always_inline)) static inline void
__nochk_bcopy(const void * src __sized_by (len),void * dest __sized_by (len),size_t len)146*5c2921b0SApple OSS Distributions __nochk_bcopy(const void *src __sized_by(len), void *dest __sized_by(len), size_t len)
147*5c2921b0SApple OSS Distributions {
148*5c2921b0SApple OSS Distributions 	__builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
149*5c2921b0SApple OSS Distributions }
150*5c2921b0SApple OSS Distributions 
151*5c2921b0SApple OSS Distributions #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_13
152*5c2921b0SApple OSS Distributions /* older deployment target */
153*5c2921b0SApple OSS Distributions #elif defined(KASAN) || (defined (_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 0)
154*5c2921b0SApple OSS Distributions /* _FORTIFY_SOURCE disabled */
155*5c2921b0SApple OSS Distributions #else /* _chk macros */
156*5c2921b0SApple OSS Distributions 
157*5c2921b0SApple OSS Distributions #if defined XNU_KERNEL_PRIVATE || defined(_FORTIFY_SOURCE_STRICT)
158*5c2921b0SApple OSS Distributions /* Stricter checking is optional for kexts. When type is set to 1, __builtin_object_size
159*5c2921b0SApple OSS Distributions  * returns the size of the closest surrounding sub-object, which would detect copying past
160*5c2921b0SApple OSS Distributions  * the end of a struct member. */
161*5c2921b0SApple OSS Distributions #define BOS_COPY_TYPE 1
162*5c2921b0SApple OSS Distributions #else
163*5c2921b0SApple OSS Distributions #define BOS_COPY_TYPE 0
164*5c2921b0SApple OSS Distributions #endif
165*5c2921b0SApple OSS Distributions 
166*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___memcpy_chk)
167*5c2921b0SApple OSS Distributions #define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
168*5c2921b0SApple OSS Distributions #endif
169*5c2921b0SApple OSS Distributions 
170*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___memmove_chk)
171*5c2921b0SApple OSS Distributions #define memmove(dest, src, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
172*5c2921b0SApple OSS Distributions #endif
173*5c2921b0SApple OSS Distributions 
174*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___strncpy_chk)
175*5c2921b0SApple OSS Distributions #define strncpy(dest, src, len) __builtin___strncpy_chk(dest, src, len, XNU_BOS(dest, 1))
176*5c2921b0SApple OSS Distributions #endif
177*5c2921b0SApple OSS Distributions 
178*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___strncat_chk)
179*5c2921b0SApple OSS Distributions #define strncat(dest, src, len) __builtin___strncat_chk(dest, src, len, XNU_BOS(dest, 1))
180*5c2921b0SApple OSS Distributions #endif
181*5c2921b0SApple OSS Distributions 
182*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___strlcat_chk)
183*5c2921b0SApple OSS Distributions #define strlcat(dest, src, len) __builtin___strlcat_chk(dest, src, len, XNU_BOS(dest, 1))
184*5c2921b0SApple OSS Distributions #endif
185*5c2921b0SApple OSS Distributions 
186*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___strlcpy_chk)
187*5c2921b0SApple OSS Distributions #define strlcpy(dest, src, len) __builtin___strlcpy_chk(dest, src, len, XNU_BOS(dest, 1))
188*5c2921b0SApple OSS Distributions #endif
189*5c2921b0SApple OSS Distributions 
190*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___strcpy_chk)
191*5c2921b0SApple OSS Distributions #define strcpy(dest, src, len) __builtin___strcpy_chk(dest, src, XNU_BOS(dest, 1))
192*5c2921b0SApple OSS Distributions #endif
193*5c2921b0SApple OSS Distributions 
194*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___strcat_chk)
195*5c2921b0SApple OSS Distributions #define strcat(dest, src) __builtin___strcat_chk(dest, src, XNU_BOS(dest, 1))
196*5c2921b0SApple OSS Distributions #endif
197*5c2921b0SApple OSS Distributions 
198*5c2921b0SApple OSS Distributions #if __has_builtin(__builtin___memmove_chk)
199*5c2921b0SApple OSS Distributions #define bcopy(src, dest, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
200*5c2921b0SApple OSS Distributions #endif
201*5c2921b0SApple OSS Distributions 
202*5c2921b0SApple OSS Distributions #if KERNEL_PRIVATE
203*5c2921b0SApple OSS Distributions /*
204*5c2921b0SApple OSS Distributions  * those allow small memsets/bzeros to be reasoned about by the compiler
205*5c2921b0SApple OSS Distributions  * despite using -fno-builtin
206*5c2921b0SApple OSS Distributions  */
207*5c2921b0SApple OSS Distributions #ifndef memset
208*5c2921b0SApple OSS Distributions #define memset(p, c, len) __builtin_memset(p, c, len)
209*5c2921b0SApple OSS Distributions #endif
210*5c2921b0SApple OSS Distributions 
211*5c2921b0SApple OSS Distributions #ifndef bzero
212*5c2921b0SApple OSS Distributions #define bzero(p, len)     __builtin_bzero(p, len)
213*5c2921b0SApple OSS Distributions #endif
214*5c2921b0SApple OSS Distributions #endif /* KERNEL_PRIVATE */
215*5c2921b0SApple OSS Distributions 
216*5c2921b0SApple OSS Distributions #endif /* _chk macros */
217*5c2921b0SApple OSS Distributions #ifdef __cplusplus
218*5c2921b0SApple OSS Distributions }
219*5c2921b0SApple OSS Distributions #endif
220*5c2921b0SApple OSS Distributions 
221*5c2921b0SApple OSS Distributions #endif  /* _STRING_H_ */
222