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