xref: /minix/include/ssp/string.h (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: string.h,v 1.13 2014/11/29 13:23:48 pooka Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 2006 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras  * by Christos Zoulas.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras #ifndef _SSP_STRING_H_
322fe8fb19SBen Gras #define _SSP_STRING_H_
332fe8fb19SBen Gras 
34*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
352fe8fb19SBen Gras #include <ssp/ssp.h>
362fe8fb19SBen Gras 
37f14fb602SLionel Sambuc __BEGIN_DECLS
38f14fb602SLionel Sambuc void *__memcpy_chk(void *, const void *, size_t, size_t);
39f14fb602SLionel Sambuc void *__memmove_chk(void *, void *, size_t, size_t);
40f14fb602SLionel Sambuc void *__memset_chk(void *, int, size_t, size_t);
4184d9c625SLionel Sambuc char *__stpcpy_chk(char *, const char *, size_t);
42f14fb602SLionel Sambuc char *__strcat_chk(char *, const char *, size_t);
43f14fb602SLionel Sambuc char *__strcpy_chk(char *, const char *, size_t);
44f14fb602SLionel Sambuc char *__strncat_chk(char *, const char *, size_t, size_t);
45f14fb602SLionel Sambuc char *__strncpy_chk(char *, const char *, size_t, size_t);
46f14fb602SLionel Sambuc __END_DECLS
47f14fb602SLionel Sambuc 
482fe8fb19SBen Gras #if __SSP_FORTIFY_LEVEL > 0
492fe8fb19SBen Gras 
502fe8fb19SBen Gras #define __ssp_bos_check3(fun, dst, src, len) \
512fe8fb19SBen Gras     ((__ssp_bos0(dst) != (size_t)-1) ? \
522fe8fb19SBen Gras     __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)) : \
532fe8fb19SBen Gras     __ ## fun ## _ichk(dst, src, len))
542fe8fb19SBen Gras 
552fe8fb19SBen Gras #define __ssp_bos_check2(fun, dst, src) \
562fe8fb19SBen Gras     ((__ssp_bos0(dst) != (size_t)-1) ? \
572fe8fb19SBen Gras     __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)) : \
582fe8fb19SBen Gras     __ ## fun ## _ichk(dst, src))
592fe8fb19SBen Gras 
602fe8fb19SBen Gras #define __ssp_bos_icheck3_restrict(fun, type1, type2) \
612fe8fb19SBen Gras static __inline type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \
622fe8fb19SBen Gras static __inline __attribute__((__always_inline__)) type1 \
632fe8fb19SBen Gras __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \
642fe8fb19SBen Gras 	return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
652fe8fb19SBen Gras }
662fe8fb19SBen Gras 
672fe8fb19SBen Gras #define __ssp_bos_icheck3(fun, type1, type2) \
682fe8fb19SBen Gras static __inline type1 __ ## fun ## _ichk(type1, type2, size_t); \
692fe8fb19SBen Gras static __inline __attribute__((__always_inline__)) type1 \
702fe8fb19SBen Gras __ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \
712fe8fb19SBen Gras 	return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
722fe8fb19SBen Gras }
732fe8fb19SBen Gras 
742fe8fb19SBen Gras #define __ssp_bos_icheck2_restrict(fun, type1, type2) \
752fe8fb19SBen Gras static __inline type1 __ ## fun ## _ichk(type1, type2); \
762fe8fb19SBen Gras static __inline __attribute__((__always_inline__)) type1 \
772fe8fb19SBen Gras __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \
782fe8fb19SBen Gras 	return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \
792fe8fb19SBen Gras }
802fe8fb19SBen Gras 
812fe8fb19SBen Gras __BEGIN_DECLS
822fe8fb19SBen Gras __ssp_bos_icheck3_restrict(memcpy, void *, const void *)
832fe8fb19SBen Gras __ssp_bos_icheck3(memmove, void *, const void *)
842fe8fb19SBen Gras __ssp_bos_icheck3(memset, void *, int)
8584d9c625SLionel Sambuc __ssp_bos_icheck2_restrict(stpcpy, char *, const char *)
86*0a6a1f1dSLionel Sambuc #if __GNUC_PREREQ__(4,8) || defined(__clang__)
87*0a6a1f1dSLionel Sambuc __ssp_bos_icheck3_restrict(stpncpy, char *, const char *)
88*0a6a1f1dSLionel Sambuc #endif
892fe8fb19SBen Gras __ssp_bos_icheck2_restrict(strcpy, char *, const char *)
902fe8fb19SBen Gras __ssp_bos_icheck2_restrict(strcat, char *, const char *)
912fe8fb19SBen Gras __ssp_bos_icheck3_restrict(strncpy, char *, const char *)
922fe8fb19SBen Gras __ssp_bos_icheck3_restrict(strncat, char *, const char *)
932fe8fb19SBen Gras __END_DECLS
942fe8fb19SBen Gras 
952fe8fb19SBen Gras #define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len)
962fe8fb19SBen Gras #define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len)
972fe8fb19SBen Gras #define memset(dst, val, len) __ssp_bos_check3(memset, dst, val, len)
9884d9c625SLionel Sambuc #define stpcpy(dst, src) __ssp_bos_check2(stpcpy, dst, src)
99*0a6a1f1dSLionel Sambuc #if __GNUC_PREREQ__(4,8) || defined(__clang__)
100*0a6a1f1dSLionel Sambuc #define stpncpy(dst, src, len) __ssp_bos_check3(stpncpy, dst, src, len)
101*0a6a1f1dSLionel Sambuc #endif
1022fe8fb19SBen Gras #define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src)
1032fe8fb19SBen Gras #define strcat(dst, src) __ssp_bos_check2(strcat, dst, src)
1042fe8fb19SBen Gras #define strncpy(dst, src, len) __ssp_bos_check3(strncpy, dst, src, len)
1052fe8fb19SBen Gras #define strncat(dst, src, len) __ssp_bos_check3(strncat, dst, src, len)
1062fe8fb19SBen Gras 
1072fe8fb19SBen Gras #endif /* __SSP_FORTIFY_LEVEL > 0 */
1082fe8fb19SBen Gras #endif /* _SSP_STRING_H_ */
109