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 char *__strncpy_chk(char *, const char *, size_t, size_t); 49 __END_DECLS 50 51 #if __SSP_FORTIFY_LEVEL > 0 52 53 #define __ssp_bos_check3_typed_var(fun, dsttype, dsrvar, dst, srctype, srcvar, \ 54 src, lenvar, len) __extension__ ({ \ 55 srctype srcvar = (src); \ 56 dsttype dstvar = (dst); \ 57 size_t lenvar = (len); \ 58 ((__ssp_bos0(dstvar) != (size_t)-1) ? \ 59 __builtin___ ## fun ## _chk(dstvar, srcvar, lenvar, \ 60 __ssp_bos0(dstvar)) : \ 61 __ ## fun ## _ichk(dstvar, srcvar, lenvar)); \ 62 }) 63 64 #define __ssp_bos_check3_typed(fun, dsttype, dst, srctype, src, len) \ 65 __ssp_bos_check3_typed_var(fun, dsttype, __ssp_var(dstv), dst, \ 66 srctype, __ssp_var(srcv), src, __ssp_var(lenv), len) 67 68 #define __ssp_bos_check3(fun, dst, src, len) \ 69 __ssp_bos_check3_typed_var(fun, void *, __ssp_var(dstv), dst, \ 70 const void *, __ssp_var(srcv), src, __ssp_var(lenv), len) 71 72 #define __ssp_bos_check2_var(fun, dstvar, dst, srcvar, src) __extension__ ({ \ 73 const void *srcvar = (src); \ 74 void *dstvar = (dst); \ 75 ((__ssp_bos0(dstvar) != (size_t)-1) ? \ 76 __builtin___ ## fun ## _chk(dstvar, srcvar, \ 77 __ssp_bos0(dstvar)) : \ 78 __ ## fun ## _ichk(dstvar, srcvar)); \ 79 }) 80 81 #define __ssp_bos_check2(fun, dst, src) \ 82 __ssp_bos_check2_var(fun, __ssp_var(dstv), dst, __ssp_var(srcv), src) 83 84 #define __ssp_bos_icheck3_restrict(fun, type1, type2) \ 85 static __inline type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \ 86 static __inline __attribute__((__always_inline__)) type1 \ 87 __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \ 88 return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ 89 } 90 91 #define __ssp_bos_icheck3(fun, type1, type2) \ 92 static __inline type1 __ ## fun ## _ichk(type1, type2, size_t); \ 93 static __inline __attribute__((__always_inline__)) type1 \ 94 __ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \ 95 return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ 96 } 97 98 #define __ssp_bos_icheck2_restrict(fun, type1, type2) \ 99 static __inline type1 __ ## fun ## _ichk(type1, type2); \ 100 static __inline __attribute__((__always_inline__)) type1 \ 101 __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \ 102 return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \ 103 } 104 105 __BEGIN_DECLS 106 __ssp_bos_icheck3_restrict(memcpy, void *, const void *) 107 __ssp_bos_icheck3(memmove, void *, const void *) 108 __ssp_bos_icheck3(memset, void *, int) 109 __ssp_bos_icheck2_restrict(stpcpy, char *, const char *) 110 __ssp_bos_icheck3_restrict(stpncpy, char *, const char *) 111 __ssp_bos_icheck2_restrict(strcpy, char *, const char *) 112 __ssp_bos_icheck2_restrict(strcat, char *, const char *) 113 __ssp_bos_icheck3_restrict(strncpy, char *, const char *) 114 __ssp_bos_icheck3_restrict(strncat, char *, const char *) 115 __END_DECLS 116 117 #define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len) 118 #define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len) 119 #define memset(dst, val, len) \ 120 __ssp_bos_check3_typed(memset, void *, dst, int, val, len) 121 #define stpcpy(dst, src) __ssp_bos_check2(stpcpy, dst, src) 122 #define stpncpy(dst, src, len) __ssp_bos_check3(stpncpy, dst, src, len) 123 #define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src) 124 #define strcat(dst, src) __ssp_bos_check2(strcat, dst, src) 125 #define strncpy(dst, src, len) __ssp_bos_check3(strncpy, dst, src, len) 126 #define strncat(dst, src, len) __ssp_bos_check3(strncat, dst, src, len) 127 128 #endif /* __SSP_FORTIFY_LEVEL > 0 */ 129 #endif /* _SSP_STRING_H_ */ 130