xref: /xnu-8020.121.3/san/memory/memintrinsics.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 2016-2020 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #ifndef _SAN_MEMINTRINSICS_H_
29 #define _SAN_MEMINTRINSICS_H_
30 
31 #include <sys/cdefs.h>
32 
33 /*
34  * Non-sanitized versions of memory intrinsics
35  */
36 static inline void *
__nosan_memcpy(void * dst __sized_by (sz),const void * src __sized_by (sz),size_t sz)37 __nosan_memcpy(void *dst __sized_by(sz), const void *src __sized_by(sz), size_t sz)
38 {
39 	return memcpy(dst, src, sz);
40 }
41 static inline void *
__nosan_memset(void * src __sized_by (sz),int c,size_t sz)42 __nosan_memset(void *src __sized_by(sz), int c, size_t sz)
43 {
44 	return memset(src, c, sz);
45 }
46 static inline void *
__nosan_memmove(void * src __sized_by (sz),const void * dst __sized_by (sz),size_t sz)47 __nosan_memmove(void *src __sized_by(sz), const void *dst __sized_by(sz), size_t sz)
48 {
49 	return memmove(src, dst, sz);
50 }
51 static inline int
__nosan_bcmp(const void * a __sized_by (sz),const void * b __sized_by (sz),size_t sz)52 __nosan_bcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz)
53 {
54 	return bcmp(a, b, sz);
55 }
56 static inline void
__nosan_bcopy(const void * src __sized_by (sz),void * dst __sized_by (sz),size_t sz)57 __nosan_bcopy(const void *src __sized_by(sz), void *dst __sized_by(sz), size_t sz)
58 {
59 	bcopy(src, dst, sz);
60 }
61 static inline int
__nosan_memcmp(const void * a __sized_by (sz),const void * b __sized_by (sz),size_t sz)62 __nosan_memcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz)
63 {
64 	return memcmp(a, b, sz);
65 }
66 static inline void
__nosan_bzero(void * dst __sized_by (sz),size_t sz)67 __nosan_bzero(void *dst __sized_by(sz), size_t sz)
68 {
69 	bzero(dst, sz);
70 }
71 
72 static inline size_t
__nosan_strlcpy(char * dst,const char * src,size_t sz)73 __nosan_strlcpy(char *dst, const char *src, size_t sz)
74 {
75 	return strlcpy(dst, src, sz);
76 }
77 static inline char  *
__nosan_strncpy(char * dst,const char * src,size_t sz)78 __nosan_strncpy(char *dst, const char *src, size_t sz)
79 {
80 #ifdef __clang__
81 #pragma clang diagnostic push
82 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
83 #endif
84 	return strncpy(dst, src, sz);
85 #ifdef __clang__
86 #pragma clang diagnostic pop
87 #endif
88 }
89 static inline size_t
__nosan_strlcat(char * dst,const char * src,size_t sz)90 __nosan_strlcat(char *dst, const char *src, size_t sz)
91 {
92 	return strlcat(dst, src, sz);
93 }
94 static inline char  *
__nosan_strncat(char * dst,const char * src,size_t sz)95 __nosan_strncat(char *dst, const char *src, size_t sz)
96 {
97 #ifdef __clang__
98 #pragma clang diagnostic push
99 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
100 #endif
101 	return strncat(dst, src, sz);
102 #ifdef __clang__
103 #pragma clang diagnostic pop
104 #endif
105 }
106 static inline size_t
__nosan_strnlen(const char * src,size_t sz)107 __nosan_strnlen(const char *src, size_t sz)
108 {
109 	return strnlen(src, sz);
110 }
111 static inline size_t
__nosan_strlen(const char * src)112 __nosan_strlen(const char *src)
113 {
114 	return strlen(src);
115 }
116 
117 #if KASAN
118 void *__asan_memcpy(void *src __sized_by(sz), const void *dst __sized_by(sz), size_t sz);
119 void *__asan_memset(void *src __sized_by(sz), int c, size_t sz);
120 void *__asan_memmove(void *src __sized_by(sz), const void *dst __sized_by(sz), size_t sz);
121 void  __asan_bcopy(const void *src __sized_by(sz), void *dst __sized_by(sz), size_t sz);
122 void  __asan_bzero(void *dst __sized_by(sz), size_t sz);
123 int   __asan_bcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz);
124 int   __asan_memcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz);
125 
126 size_t __asan_strlcpy(char *dst, const char *src, size_t sz);
127 char  *__asan_strncpy(char *dst, const char *src, size_t sz);
128 size_t __asan_strlcat(char *dst, const char *src, size_t sz);
129 char  *__asan_strncat(char *dst, const char *src, size_t sz);
130 size_t __asan_strnlen(const char *src, size_t sz);
131 size_t __asan_strlen(const char *src);
132 
133 #define memcpy    __asan_memcpy
134 #define memmove   __asan_memmove
135 #define memset    __asan_memset
136 #define bcopy     __asan_bcopy
137 #define bzero     __asan_bzero
138 #define bcmp      __asan_bcmp
139 #define memcmp    __asan_memcmp
140 
141 #define strlcpy   __asan_strlcpy
142 #define strncpy   __asan_strncpy
143 #define strlcat   __asan_strlcat
144 #define strncat   __asan_strncat
145 // #define strnlen   __asan_strnlen
146 // #define strlen    __asan_strlen
147 
148 #endif
149 
150 #endif /* _SAN_MEMINTRINSICS_H_ */
151