xref: /freebsd/include/ssp/string.h (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
1 /*	$NetBSD: string.h,v 1.14 2020/09/05 13:37:59 mrg Exp $	*/
2 
3 /*-
4  *
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * Copyright (c) 2006 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Christos Zoulas.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #ifndef _SSP_STRING_H_
35 #define _SSP_STRING_H_
36 
37 #include <ssp/ssp.h>
38 
39 __BEGIN_DECLS
40 void *__memcpy_chk(void *, const void *, size_t, size_t);
41 void *__memmove_chk(void *, const void *, size_t, size_t);
42 void *__memset_chk(void *, int, size_t, size_t);
43 char *__stpcpy_chk(char *, const char *, size_t);
44 char *__stpncpy_chk(char *, const char *, size_t, size_t);
45 char *__strcat_chk(char *, const char *, size_t);
46 char *__strcpy_chk(char *, const char *, size_t);
47 char *__strncat_chk(char *, const char *, size_t, size_t);
48 size_t __strlcat_chk(char *, const char *, size_t, size_t);
49 char *__strncpy_chk(char *, const char *, size_t, size_t);
50 size_t __strlcpy_chk(char *, const char *, size_t, size_t);
51 __END_DECLS
52 
53 #if __SSP_FORTIFY_LEVEL > 0
54 
55 #define __ssp_bos_check3_typed_var(fun, dsttype, dsrvar, dst, srctype, srcvar, \
56     src, lenvar, len) __extension__ ({				\
57     srctype srcvar = (src);				\
58     dsttype dstvar = (dst);				\
59     size_t lenvar = (len);				\
60     ((__ssp_bos0(dstvar) != (size_t)-1) ?		\
61     __builtin___ ## fun ## _chk(dstvar, srcvar, lenvar,	\
62         __ssp_bos0(dstvar)) :				\
63     __ ## fun ## _ichk(dstvar, srcvar, lenvar));	\
64 })
65 
66 #define __ssp_bos_check3_typed(fun, dsttype, dst, srctype, src, len)	\
67     __ssp_bos_check3_typed_var(fun, dsttype, __ssp_var(dstv), dst,	\
68         srctype, __ssp_var(srcv), src, __ssp_var(lenv), len)
69 
70 #define __ssp_bos_check3(fun, dst, src, len)		\
71     __ssp_bos_check3_typed_var(fun, void *, __ssp_var(dstv), dst,	\
72         const void *, __ssp_var(srcv), src, __ssp_var(lenv), len)
73 
74 #define __ssp_bos_check2_var(fun, dstvar, dst, srcvar, src) __extension__ ({ 	\
75     const void *srcvar = (src);				\
76     void *dstvar = (dst);				\
77     ((__ssp_bos0(dstvar) != (size_t)-1) ?		\
78     __builtin___ ## fun ## _chk(dstvar, srcvar,		\
79         __ssp_bos0(dstvar)) :				\
80     __ ## fun ## _ichk(dstvar, srcvar));		\
81 })
82 
83 #define __ssp_bos_check2(fun, dst, src)			\
84     __ssp_bos_check2_var(fun, __ssp_var(dstv), dst, __ssp_var(srcv), src)
85 
86 #define __ssp_bos_icheck3_restrict(fun, type1, type2) \
87 static __inline type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \
88 static __inline __attribute__((__always_inline__)) type1 \
89 __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \
90 	return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
91 }
92 
93 #define __ssp_bos_icheck3(fun, type1, type2) \
94 static __inline type1 __ ## fun ## _ichk(type1, type2, size_t); \
95 static __inline __attribute__((__always_inline__)) type1 \
96 __ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \
97 	return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
98 }
99 
100 #define __ssp_bos_icheck2_restrict(fun, type1, type2) \
101 static __inline type1 __ ## fun ## _ichk(type1, type2); \
102 static __inline __attribute__((__always_inline__)) type1 \
103 __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \
104 	return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \
105 }
106 
107 __BEGIN_DECLS
108 __ssp_bos_icheck3_restrict(memcpy, void *, const void *)
109 __ssp_bos_icheck3_restrict(mempcpy, void *, const void *)
110 __ssp_bos_icheck3(memmove, void *, const void *)
111 __ssp_bos_icheck3(memset, void *, int)
112 __ssp_bos_icheck2_restrict(stpcpy, char *, const char *)
113 __ssp_bos_icheck3_restrict(stpncpy, char *, const char *)
114 __ssp_bos_icheck2_restrict(strcpy, char *, const char *)
115 __ssp_bos_icheck2_restrict(strcat, char *, const char *)
116 __ssp_redirect0(int, strerror_r, (int __errnum, char *__buf, size_t __len),
117     (__errnum, __buf, __len));
118 __ssp_bos_icheck3_restrict(strncpy, char *, const char *)
119 __ssp_bos_icheck3_restrict(strncat, char *, const char *)
120 __END_DECLS
121 
122 #define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len)
123 #define mempcpy(dst, src, len) __ssp_bos_check3(mempcpy, dst, src, len)
124 #define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len)
125 #define memset(dst, val, len) \
126     __ssp_bos_check3_typed(memset, void *, dst, int, val, len)
127 #define stpcpy(dst, src) __ssp_bos_check2(stpcpy, dst, src)
128 #define stpncpy(dst, src, len) __ssp_bos_check3(stpncpy, dst, src, len)
129 #define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src)
130 #define strcat(dst, src) __ssp_bos_check2(strcat, dst, src)
131 #define strlcpy(dst, src, dstlen) \
132     __strlcpy_chk(dst, src, dstlen, __ssp_bos(dst))
133 #define strncpy(dst, src, len) __ssp_bos_check3(strncpy, dst, src, len)
134 #define strlcat(dst, src, dstlen) \
135     __strlcat_chk(dst, src, dstlen, __ssp_bos(dst))
136 #define strncat(dst, src, len) __ssp_bos_check3(strncat, dst, src, len)
137 
138 #endif /* __SSP_FORTIFY_LEVEL > 0 */
139 #endif /* _SSP_STRING_H_ */
140