blob: beb5ff5b868f28d879d9c5697116432516d00a8b [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
Elliott Hughes95c6cd72019-12-20 13:26:14 -080043#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV36926f42019-09-15 16:57:00 -070044/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -070045__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070046void* memcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount)
George Burgess IV36926f42019-09-15 16:57:00 -070047 __overloadable {
George Burgess IV2f787652020-02-04 21:40:40 -080048 return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst));
George Burgess IVb97049c2017-07-24 15:05:05 -070049}
50
George Burgess IV36926f42019-09-15 16:57:00 -070051/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -070052__BIONIC_FORTIFY_INLINE
George Burgess IV36926f42019-09-15 16:57:00 -070053void* memmove(void* const dst __pass_object_size0, const void* src, size_t len) __overloadable {
George Burgess IV2f787652020-02-04 21:40:40 -080054 return __builtin___memmove_chk(dst, src, len, __bos0(dst));
George Burgess IVb97049c2017-07-24 15:05:05 -070055}
George Burgess IV8a0cdb12019-10-21 13:27:57 -070056#endif
George Burgess IVb97049c2017-07-24 15:05:05 -070057
George Burgess IV849c0b92019-06-10 16:22:09 -070058#if defined(__USE_GNU)
Elliott Hughes95c6cd72019-12-20 13:26:14 -080059#if __ANDROID_API__ >= 30
George Burgess IV849c0b92019-06-10 16:22:09 -070060__BIONIC_FORTIFY_INLINE
61void* mempcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount)
62 __overloadable
63 __clang_error_if(__bos_unevaluated_lt(__bos0(dst), copy_amount),
64 "'mempcpy' called with size bigger than buffer") {
George Burgess IV8a0cdb12019-10-21 13:27:57 -070065#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV849c0b92019-06-10 16:22:09 -070066 size_t bos_dst = __bos0(dst);
George Burgess IV8a0cdb12019-10-21 13:27:57 -070067 if (!__bos_trivially_ge(bos_dst, copy_amount)) {
68 return __builtin___mempcpy_chk(dst, src, copy_amount, bos_dst);
George Burgess IV849c0b92019-06-10 16:22:09 -070069 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -070070#endif
71 return __builtin_mempcpy(dst, src, copy_amount);
George Burgess IV849c0b92019-06-10 16:22:09 -070072}
Elliott Hughes95c6cd72019-12-20 13:26:14 -080073#endif /* __ANDROID_API__ >= 30 */
George Burgess IVfd1ff4b2019-09-18 17:29:55 -070074#endif /* __USE_GNU */
George Burgess IV849c0b92019-06-10 16:22:09 -070075
George Burgess IVb97049c2017-07-24 15:05:05 -070076__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070077char* stpcpy(char* const dst __pass_object_size, const char* src)
78 __overloadable
George Burgess IVc03d5962019-05-23 15:22:01 -070079 __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)),
George Burgess IVb6300462017-07-31 21:29:42 -070080 "'stpcpy' called with string bigger than buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -080081#if __ANDROID_API__ >= 21 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV2f787652020-02-04 21:40:40 -080082 return __builtin___stpcpy_chk(dst, src, __bos(dst));
83#else
George Burgess IVfd1ff4b2019-09-18 17:29:55 -070084 return __builtin_stpcpy(dst, src);
George Burgess IV2f787652020-02-04 21:40:40 -080085#endif
George Burgess IVfd1ff4b2019-09-18 17:29:55 -070086}
George Burgess IVb97049c2017-07-24 15:05:05 -070087
George Burgess IVb97049c2017-07-24 15:05:05 -070088__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -070089char* strcpy(char* const dst __pass_object_size, const char* src)
90 __overloadable
George Burgess IVc03d5962019-05-23 15:22:01 -070091 __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)),
George Burgess IVb6300462017-07-31 21:29:42 -070092 "'strcpy' called with string bigger than buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -080093#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV2f787652020-02-04 21:40:40 -080094 return __builtin___strcpy_chk(dst, src, __bos(dst));
95#else
George Burgess IVfd1ff4b2019-09-18 17:29:55 -070096 return __builtin_strcpy(dst, src);
George Burgess IV2f787652020-02-04 21:40:40 -080097#endif
George Burgess IVb97049c2017-07-24 15:05:05 -070098}
99
100__BIONIC_FORTIFY_INLINE
George Burgess IV77f99aa2019-06-06 14:14:52 -0700101char* strcat(char* const dst __pass_object_size, const char* src)
102 __overloadable
103 __clang_error_if(__bos_unevaluated_le(__bos(dst), __builtin_strlen(src)),
104 "'strcat' called with string bigger than buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800105#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700106 return __builtin___strcat_chk(dst, src, __bos(dst));
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700107#else
108 return __builtin_strcat(dst, src);
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700109#endif
George Burgess IVb97049c2017-07-24 15:05:05 -0700110}
111
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800112#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV36926f42019-09-15 16:57:00 -0700113/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700114__BIONIC_FORTIFY_INLINE
George Burgess IV36926f42019-09-15 16:57:00 -0700115char* strncat(char* const dst __pass_object_size, const char* src, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700116 return __builtin___strncat_chk(dst, src, n, __bos(dst));
117}
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700118#endif
George Burgess IVb97049c2017-07-24 15:05:05 -0700119
George Burgess IV36926f42019-09-15 16:57:00 -0700120/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700121__BIONIC_FORTIFY_INLINE
George Burgess IV36926f42019-09-15 16:57:00 -0700122void* memset(void* const s __pass_object_size0, int c, size_t n) __overloadable
George Burgess IVb6300462017-07-31 21:29:42 -0700123 /* If you're a user who wants this warning to go away: use `(&memset)(foo, bar, baz)`. */
124 __clang_warning_if(c && !n, "'memset' will set 0 bytes; maybe the arguments got flipped?") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800125#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV2f787652020-02-04 21:40:40 -0800126 return __builtin___memset_chk(s, c, n, __bos0(s));
127#else
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700128 return __builtin_memset(s, c, n);
George Burgess IV2f787652020-02-04 21:40:40 -0800129#endif
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700130}
George Burgess IVb97049c2017-07-24 15:05:05 -0700131
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800132#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700133__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700134void* memchr(const void* const s __pass_object_size, int c, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700135 size_t bos = __bos(s);
136
George Burgess IVc03d5962019-05-23 15:22:01 -0700137 if (__bos_trivially_ge(bos, n)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700138 return __builtin_memchr(s, c, n);
139 }
140
141 return __memchr_chk(s, c, n, bos);
142}
143
144__BIONIC_FORTIFY_INLINE
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700145void* __memrchr_fortify(const void* const __pass_object_size s, int c, size_t n) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700146 size_t bos = __bos(s);
147
George Burgess IVc03d5962019-05-23 15:22:01 -0700148 if (__bos_trivially_ge(bos, n)) {
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700149 return __memrchr_real(s, c, n);
George Burgess IVb97049c2017-07-24 15:05:05 -0700150 }
151
152 return __memrchr_chk(s, c, n, bos);
153}
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700154#endif
George Burgess IVb97049c2017-07-24 15:05:05 -0700155
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800156#if __ANDROID_API__ >= 21 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV36926f42019-09-15 16:57:00 -0700157/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700158__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700159char* stpncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
George Burgess IV36926f42019-09-15 16:57:00 -0700160 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700161 size_t bos_dst = __bos(dst);
162 size_t bos_src = __bos(src);
163
164 /* Ignore dst size checks; they're handled in strncpy_chk */
165 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
166 return __builtin___stpncpy_chk(dst, src, n, bos_dst);
167 }
168
169 return __stpncpy_chk2(dst, src, n, bos_dst, bos_src);
170}
171
George Burgess IV36926f42019-09-15 16:57:00 -0700172/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -0700173__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700174char* strncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n)
George Burgess IV36926f42019-09-15 16:57:00 -0700175 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -0700176 size_t bos_dst = __bos(dst);
177 size_t bos_src = __bos(src);
178
179 /* Ignore dst size checks; they're handled in strncpy_chk */
180 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
181 return __builtin___strncpy_chk(dst, src, n, bos_dst);
182 }
183
184 return __strncpy_chk2(dst, src, n, bos_dst, bos_src);
185}
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700186#endif
George Burgess IVb97049c2017-07-24 15:05:05 -0700187
George Burgess IVb97049c2017-07-24 15:05:05 -0700188__BIONIC_FORTIFY_INLINE
George Burgess IV77f99aa2019-06-06 14:14:52 -0700189size_t strlcpy(char* const dst __pass_object_size, const char* src, size_t size)
190 __overloadable
191 __clang_error_if(__bos_unevaluated_lt(__bos(dst), size),
192 "'strlcpy' called with size bigger than buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800193#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV2f787652020-02-04 21:40:40 -0800194 return __strlcpy_chk(dst, src, size, __bos(dst));
195#else
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700196 return __call_bypassing_fortify(strlcpy)(dst, src, size);
George Burgess IV2f787652020-02-04 21:40:40 -0800197#endif
George Burgess IVb97049c2017-07-24 15:05:05 -0700198}
199
200__BIONIC_FORTIFY_INLINE
George Burgess IV77f99aa2019-06-06 14:14:52 -0700201size_t strlcat(char* const dst __pass_object_size, const char* src, size_t size)
202 __overloadable
203 __clang_error_if(__bos_unevaluated_lt(__bos(dst), size),
204 "'strlcat' called with size bigger than buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800205#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV2f787652020-02-04 21:40:40 -0800206 return __strlcat_chk(dst, src, size, __bos(dst));
George Burgess IVd02e7b12020-02-13 11:44:20 -0800207#else
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700208 return __call_bypassing_fortify(strlcat)(dst, src, size);
George Burgess IVd02e7b12020-02-13 11:44:20 -0800209#endif
George Burgess IVb97049c2017-07-24 15:05:05 -0700210}
211
George Burgess IVd02e7b12020-02-13 11:44:20 -0800212#if __ANDROID_API__ >= 17 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700213__BIONIC_FORTIFY_INLINE
George Burgess IVb6300462017-07-31 21:29:42 -0700214size_t strlen(const char* const s __pass_object_size0) __overloadable {
George Burgess IVd02e7b12020-02-13 11:44:20 -0800215 return __strlen_chk(s, __bos0(s));
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700216}
George Burgess IVd02e7b12020-02-13 11:44:20 -0800217#endif
George Burgess IVb97049c2017-07-24 15:05:05 -0700218
George Burgess IVb97049c2017-07-24 15:05:05 -0700219__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700220char* strchr(const char* const s __pass_object_size, int c) __overloadable {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800221#if __ANDROID_API__ >= 18 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700222 size_t bos = __bos(s);
223
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700224 if (bos != __BIONIC_FORTIFY_UNKNOWN_SIZE) {
225 return __strchr_chk(s, c, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700226 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700227#endif
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700228 return __builtin_strchr(s, c);
George Burgess IVb97049c2017-07-24 15:05:05 -0700229}
230
231__BIONIC_FORTIFY_INLINE
Elliott Hughes3f66e742017-08-01 13:24:40 -0700232char* strrchr(const char* const s __pass_object_size, int c) __overloadable {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800233#if __ANDROID_API__ >= 18 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700234 size_t bos = __bos(s);
235
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700236 if (bos != __BIONIC_FORTIFY_UNKNOWN_SIZE) {
237 return __strrchr_chk(s, c, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700238 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700239#endif
George Burgess IVfd1ff4b2019-09-18 17:29:55 -0700240 return __builtin_strrchr(s, c);
241}
George Burgess IVb97049c2017-07-24 15:05:05 -0700242
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800243#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700244#if defined(__cplusplus)
245extern "C++" {
246__BIONIC_FORTIFY_INLINE
247void* memrchr(void* const __pass_object_size s, int c, size_t n) {
248 return __memrchr_fortify(s, c, n);
249}
250
251__BIONIC_FORTIFY_INLINE
252const void* memrchr(const void* const __pass_object_size s, int c, size_t n) {
253 return __memrchr_fortify(s, c, n);
254}
255}
256#else
257__BIONIC_FORTIFY_INLINE
258void* memrchr(const void* const __pass_object_size s, int c, size_t n) __overloadable {
259 return __memrchr_fortify(s, c, n);
260}
261#endif
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800262#endif /* __ANDROID_API__ >= 23 */
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700263
George Burgess IVb97049c2017-07-24 15:05:05 -0700264#endif /* defined(__BIONIC_FORTIFY) */