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 *
__sized_by(sz)37 __sized_by(sz)
38 __nosan_memcpy(void *dst __sized_by(sz), const void *src __sized_by(sz), size_t sz)
39 {
40 return memcpy(dst, src, sz);
41 }
42 static inline void *
__sized_by(sz)43 __sized_by(sz)
44 __nosan_memset(void *src __sized_by(sz), int c, size_t sz)
45 {
46 return memset(src, c, sz);
47 }
48 static inline void *
__sized_by(sz)49 __sized_by(sz)
50 __nosan_memmove(void *src __sized_by(sz), const void *dst __sized_by(sz), size_t sz)
51 {
52 return memmove(src, dst, sz);
53 }
54 static inline int
__nosan_bcmp(const void * a __sized_by (sz),const void * b __sized_by (sz),size_t sz)55 __nosan_bcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz)
56 {
57 return bcmp(a, b, sz);
58 }
59 static inline void
__nosan_bcopy(const void * src __sized_by (sz),void * dst __sized_by (sz),size_t sz)60 __nosan_bcopy(const void *src __sized_by(sz), void *dst __sized_by(sz), size_t sz)
61 {
62 bcopy(src, dst, sz);
63 }
64 static inline int
__nosan_memcmp(const void * a __sized_by (sz),const void * b __sized_by (sz),size_t sz)65 __nosan_memcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz)
66 {
67 return memcmp(a, b, sz);
68 }
69 static inline void
__nosan_bzero(void * dst __sized_by (sz),size_t sz)70 __nosan_bzero(void *dst __sized_by(sz), size_t sz)
71 {
72 bzero(dst, sz);
73 }
74
75 static inline size_t
__nosan_strlcpy(char * __sized_by (sz)dst,const char * __null_terminated src,size_t sz)76 __nosan_strlcpy(char *__sized_by(sz)dst, const char *__null_terminated src, size_t sz)
77 {
78 return strlcpy(dst, src, sz);
79 }
80 static inline size_t
__nosan_strlcat(char * __sized_by (sz)dst,const char * __null_terminated src,size_t sz)81 __nosan_strlcat(char *__sized_by(sz)dst, const char *__null_terminated src, size_t sz)
82 {
83 return strlcat(dst, src, sz);
84 }
85 static inline size_t
__nosan_strnlen(const char * __counted_by (sz)src,size_t sz)86 __nosan_strnlen(const char *__counted_by(sz)src, size_t sz)
87 {
88 return strnlen(src, sz);
89 }
90 static inline size_t
__nosan_strlen(const char * __null_terminated src)91 __nosan_strlen(const char *__null_terminated src)
92 {
93 return strlen(src);
94 }
95 #if !__has_ptrcheck && !__has_include(<__xnu_libcxx_sentinel.h>)
96 static inline char *
__nosan_strncpy(char * dst,const char * src,size_t sz)97 __nosan_strncpy(char *dst, const char *src, size_t sz)
98 {
99 #ifdef __clang__
100 #pragma clang diagnostic push
101 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
102 #endif
103 return strncpy(dst, src, sz);
104 #ifdef __clang__
105 #pragma clang diagnostic pop
106 #endif
107 }
108 static inline char *
__nosan_strncat(char * dst,const char * src,size_t sz)109 __nosan_strncat(char *dst, const char *src, size_t sz)
110 {
111 #ifdef __clang__
112 #pragma clang diagnostic push
113 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
114 #endif
115 return strncat(dst, src, sz);
116 #ifdef __clang__
117 #pragma clang diagnostic pop
118 #endif
119 }
120 #endif /* !__has_ptrcheck && !__has_include(<__xnu_libcxx_sentinel.h>) */
121
122 #if KASAN
123 void *__sized_by(sz) __asan_memcpy(void *src __sized_by(sz), const void *dst __sized_by(sz), size_t sz);
124 void *__sized_by(sz) __asan_memset(void *src __sized_by(sz), int c, size_t sz);
125 void *__sized_by(sz) __asan_memmove(void *src __sized_by(sz), const void *dst __sized_by(sz), size_t sz);
126 void __asan_bcopy(const void *src __sized_by(sz), void *dst __sized_by(sz), size_t sz);
127 void __asan_bzero(void *dst __sized_by(sz), size_t sz);
128 int __asan_bcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz);
129 int __asan_memcmp(const void *a __sized_by(sz), const void *b __sized_by(sz), size_t sz);
130
131 size_t __asan_strlcpy(char *__sized_by(sz) dst, const char *__null_terminated src, size_t sz);
132 char *__asan_strncpy(char *dst, const char *src, size_t sz);
133 char *__asan_strncat(char *dst, const char *src, size_t sz);
134 size_t __asan_strlcat(char *__sized_by(sz) dst, const char *__null_terminated src, size_t sz);
135 size_t __asan_strnlen(const char *__null_terminated src, size_t sz);
136 size_t __asan_strlen(const char *__null_terminated src);
137
138 #define memcpy __asan_memcpy
139 #define memmove __asan_memmove
140 #define memset __asan_memset
141 #define bcopy __asan_bcopy
142 #define bzero __asan_bzero
143 #define bcmp __asan_bcmp
144 #define memcmp __asan_memcmp
145
146 #define strlcpy __asan_strlcpy
147 #define strncpy __asan_strncpy
148 #define strlcat __asan_strlcat
149 #define strncat __asan_strncat
150 // #define strnlen __asan_strnlen
151 // #define strlen __asan_strlen
152
153 #endif
154
155 #endif /* _SAN_MEMINTRINSICS_H_ */
156