blob: 80eb43e6f091f6b3e1c4b81c90ac90cf83550fc9 [file] [log] [blame]
George Burgess IVb97049c2017-07-24 15:05:05 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef _STRING_H
30#error "Never include this file directly; instead, include <string.h>"
31#endif
32
Elliott Hughes3f66e742017-08-01 13:24:40 -070033void* __memchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
34void* __memrchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
35char* __stpncpy_chk2(char*, const char*, size_t, size_t, size_t) __INTRODUCED_IN(21);
36char* __strncpy_chk2(char*, const char*, size_t, size_t, size_t) __INTRODUCED_IN(21);
37size_t __strlcpy_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(17);
38size_t __strlcat_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(17);
George Burgess IVb97049c2017-07-24 15:05:05 -070039
George Burgess IVb97049c2017-07-24 15:05:05 -070040#if defined(__BIONIC_FORTIFY)
Elliott Hughesdf9a4892017-08-23 14:34:03 -070041extern void* __memrchr_real(const void*, int, size_t) __RENAME(memrchr);
42
George Burgess IVb97049c2017-07-24 15:05:05 -070043#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
44__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070045void* memcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount)
46 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070047 __clang_error_if(__bos_unevaluated_lt(__bos0(dst), copy_amount),
George Burgess IVb6300462017-07-31 21:29:42 -070048 "'memcpy' called with size bigger than buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -070049 return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst));
50}
51
52__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070053void* memmove(void* const dst __pass_object_size0, const void* src, size_t len)
54 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070055 __clang_error_if(__bos_unevaluated_lt(__bos0(dst), len),
George Burgess IVb6300462017-07-31 21:29:42 -070056 "'memmove' called with size bigger than buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -070057 return __builtin___memmove_chk(dst, src, len, __bos0(dst));
58}
59#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
60
61#if __ANDROID_API__ >= __ANDROID_API_L__
62__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070063char* stpcpy(char* const dst __pass_object_size, const char* src)
64 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070065 __clang_error_if(__bos_unevaluated_leq(__bos(dst), __builtin_strlen(src)),
George Burgess IVb6300462017-07-31 21:29:42 -070066 "'stpcpy' called with string bigger than buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -070067 return __builtin___stpcpy_chk(dst, src, __bos(dst));
68}
69#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
70
71#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
72__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070073char* strcpy(char* const dst __pass_object_size, const char* src)
74 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070075 __clang_error_if(__bos_unevaluated_leq(__bos(dst), __builtin_strlen(src)),
George Burgess IVb6300462017-07-31 21:29:42 -070076 "'strcpy' called with string bigger than buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -070077 return __builtin___strcpy_chk(dst, src, __bos(dst));
78}
79
80__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -070081char* strcat(char* const dst __pass_object_size, const char* src) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070082 return __builtin___strcat_chk(dst, src, __bos(dst));
83}
84
85__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -070086char* strncat(char* const dst __pass_object_size, const char* src, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070087 return __builtin___strncat_chk(dst, src, n, __bos(dst));
88}
89
90__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070091void* memset(void* const s __pass_object_size0, int c, size_t n)
92 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070093 __clang_error_if(__bos_unevaluated_lt(__bos0(s), n),
George Burgess IVb6300462017-07-31 21:29:42 -070094 "'memset' called with size bigger than buffer")
95 /* If you're a user who wants this warning to go away: use `(&memset)(foo, bar, baz)`. */
96 __clang_warning_if(c && !n, "'memset' will set 0 bytes; maybe the arguments got flipped?") {
George Burgess IVb97049c2017-07-24 15:05:05 -070097 return __builtin___memset_chk(s, c, n, __bos0(s));
98}
99#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
100
George Burgess IVb97049c2017-07-24 15:05:05 -0700101#if __ANDROID_API__ >= __ANDROID_API_M__
102__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700103void* memchr(const void* const s __pass_object_size, int c, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700104 size_t bos = __bos(s);
105
106 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
107 return __builtin_memchr(s, c, n);
108 }
109
110 return __memchr_chk(s, c, n, bos);
111}
112
113__BIONIC_FORTIFY_INLINE
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700114void* __memrchr_fortify(const void* const __pass_object_size s, int c, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700115 size_t bos = __bos(s);
116
117 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700118 return __memrchr_real(s, c, n);
George Burgess IVb97049c2017-07-24 15:05:05 -0700119 }
120
121 return __memrchr_chk(s, c, n, bos);
122}
123#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
124
125#if __ANDROID_API__ >= __ANDROID_API_L__
126__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700127char* stpncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
George Burgess IVb97049c2017-07-24 15:05:05 -0700128 __overloadable {
129 size_t bos_dst = __bos(dst);
130 size_t bos_src = __bos(src);
131
132 /* Ignore dst size checks; they're handled in strncpy_chk */
133 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
134 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
135 }
136
137 return __stpncpy_chk2(dst, src, n, bos_dst, bos_src);
138}
139
140__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700141char* strncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
George Burgess IVb97049c2017-07-24 15:05:05 -0700142 __overloadable {
143 size_t bos_dst = __bos(dst);
144 size_t bos_src = __bos(src);
145
146 /* Ignore dst size checks; they're handled in strncpy_chk */
147 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
148 return __builtin___strncpy_chk(dst, src, n, bos_dst);
149 }
150
151 return __strncpy_chk2(dst, src, n, bos_dst, bos_src);
152}
153#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
154
155#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
156__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700157size_t strlcpy(char* const dst __pass_object_size, const char* src, size_t size) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700158 size_t bos = __bos(dst);
159
160 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
161 return __call_bypassing_fortify(strlcpy)(dst, src, size);
162 }
163
164 return __strlcpy_chk(dst, src, size, bos);
165}
166
167__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700168size_t strlcat(char* const dst __pass_object_size, const char* src, size_t size) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700169 size_t bos = __bos(dst);
170
171 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
172 return __call_bypassing_fortify(strlcat)(dst, src, size);
173 }
174
175 return __strlcat_chk(dst, src, size, bos);
176}
177
178/*
179 * If we can evaluate the size of s at compile-time, just call __builtin_strlen
180 * on it directly. This makes it way easier for compilers to fold things like
181 * strlen("Foo") into a constant, as users would expect. -1ULL is chosen simply
182 * because it's large.
183 */
184__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700185size_t strlen(const char* const s __pass_object_size)
George Burgess IVb97049c2017-07-24 15:05:05 -0700186 __overloadable __enable_if(__builtin_strlen(s) != -1ULL,
187 "enabled if s is a known good string.") {
188 return __builtin_strlen(s);
189}
190
191__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700192size_t strlen(const char* const s __pass_object_size0) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700193 size_t bos = __bos0(s);
194
195 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
196 return __builtin_strlen(s);
197 }
198
George Burgess IVb97049c2017-07-24 15:05:05 -0700199 return __strlen_chk(s, bos);
200}
201#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
202
203#if __ANDROID_API__ >= __ANDROID_API_J_MR2__
204__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700205char* strchr(const char* const s __pass_object_size, int c) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700206 size_t bos = __bos(s);
207
208 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
209 return __builtin_strchr(s, c);
210 }
211
212 return __strchr_chk(s, c, bos);
213}
214
215__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700216char* strrchr(const char* const s __pass_object_size, int c) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700217 size_t bos = __bos(s);
218
219 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
220 return __builtin_strrchr(s, c);
221 }
222
223 return __strrchr_chk(s, c, bos);
224}
225#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR2__ */
226
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700227#if __ANDROID_API__ >= __ANDROID_API_M__
228#if defined(__cplusplus)
229extern "C++" {
230__BIONIC_FORTIFY_INLINE
231void* memrchr(void* const __pass_object_size s, int c, size_t n) {
232 return __memrchr_fortify(s, c, n);
233}
234
235__BIONIC_FORTIFY_INLINE
236const void* memrchr(const void* const __pass_object_size s, int c, size_t n) {
237 return __memrchr_fortify(s, c, n);
238}
239}
240#else
241__BIONIC_FORTIFY_INLINE
242void* memrchr(const void* const __pass_object_size s, int c, size_t n) __overloadable {
243 return __memrchr_fortify(s, c, n);
244}
245#endif
246#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
247
George Burgess IVb97049c2017-07-24 15:05:05 -0700248#endif /* defined(__BIONIC_FORTIFY) */