George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 33 | void* __memchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23); |
| 34 | void* __memrchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23); |
| 35 | char* __stpncpy_chk2(char*, const char*, size_t, size_t, size_t) __INTRODUCED_IN(21); |
| 36 | char* __strncpy_chk2(char*, const char*, size_t, size_t, size_t) __INTRODUCED_IN(21); |
| 37 | size_t __strlcpy_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(17); |
| 38 | size_t __strlcat_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(17); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 39 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 40 | #if defined(__BIONIC_FORTIFY) |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 41 | extern void* __memrchr_real(const void*, int, size_t) __RENAME(memrchr); |
| 42 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 43 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
| 44 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 45 | void* memcpy(void* const dst __pass_object_size0, const void* src, size_t copy_amount) |
| 46 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame^] | 47 | __clang_error_if(__bos_unevaluated_lt(__bos0(dst), copy_amount), |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 48 | "'memcpy' called with size bigger than buffer") { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 49 | return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst)); |
| 50 | } |
| 51 | |
| 52 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 53 | void* memmove(void* const dst __pass_object_size0, const void* src, size_t len) |
| 54 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame^] | 55 | __clang_error_if(__bos_unevaluated_lt(__bos0(dst), len), |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 56 | "'memmove' called with size bigger than buffer") { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 57 | 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 IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 63 | char* stpcpy(char* const dst __pass_object_size, const char* src) |
| 64 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame^] | 65 | __clang_error_if(__bos_unevaluated_leq(__bos(dst), __builtin_strlen(src)), |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 66 | "'stpcpy' called with string bigger than buffer") { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 67 | 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 IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 73 | char* strcpy(char* const dst __pass_object_size, const char* src) |
| 74 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame^] | 75 | __clang_error_if(__bos_unevaluated_leq(__bos(dst), __builtin_strlen(src)), |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 76 | "'strcpy' called with string bigger than buffer") { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 77 | return __builtin___strcpy_chk(dst, src, __bos(dst)); |
| 78 | } |
| 79 | |
| 80 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 81 | char* strcat(char* const dst __pass_object_size, const char* src) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 82 | return __builtin___strcat_chk(dst, src, __bos(dst)); |
| 83 | } |
| 84 | |
| 85 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 86 | char* strncat(char* const dst __pass_object_size, const char* src, size_t n) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 87 | return __builtin___strncat_chk(dst, src, n, __bos(dst)); |
| 88 | } |
| 89 | |
| 90 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 91 | void* memset(void* const s __pass_object_size0, int c, size_t n) |
| 92 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame^] | 93 | __clang_error_if(__bos_unevaluated_lt(__bos0(s), n), |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 94 | "'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 IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 97 | return __builtin___memset_chk(s, c, n, __bos0(s)); |
| 98 | } |
| 99 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
| 100 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 101 | #if __ANDROID_API__ >= __ANDROID_API_M__ |
| 102 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 103 | void* memchr(const void* const s __pass_object_size, int c, size_t n) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 104 | 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 Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 114 | void* __memrchr_fortify(const void* const __pass_object_size s, int c, size_t n) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 115 | size_t bos = __bos(s); |
| 116 | |
| 117 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 118 | return __memrchr_real(s, c, n); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 119 | } |
| 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 Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 127 | char* stpncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 128 | __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 Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 141 | char* strncpy(char* const dst __pass_object_size, const char* const src __pass_object_size, size_t n) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 142 | __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 IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 157 | size_t strlcpy(char* const dst __pass_object_size, const char* src, size_t size) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 158 | 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 IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 168 | size_t strlcat(char* const dst __pass_object_size, const char* src, size_t size) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 169 | 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 Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 185 | size_t strlen(const char* const s __pass_object_size) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 186 | __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 IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 192 | size_t strlen(const char* const s __pass_object_size0) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 193 | size_t bos = __bos0(s); |
| 194 | |
| 195 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 196 | return __builtin_strlen(s); |
| 197 | } |
| 198 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 199 | 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 Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 205 | char* strchr(const char* const s __pass_object_size, int c) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 206 | 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 Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 216 | char* strrchr(const char* const s __pass_object_size, int c) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 217 | 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 Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 227 | #if __ANDROID_API__ >= __ANDROID_API_M__ |
| 228 | #if defined(__cplusplus) |
| 229 | extern "C++" { |
| 230 | __BIONIC_FORTIFY_INLINE |
| 231 | void* 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 |
| 236 | const 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 |
| 242 | void* 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 IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 248 | #endif /* defined(__BIONIC_FORTIFY) */ |