Alexey Samsonov | 603c4be | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- sanitizer_libc.cc -------------------------------------------------===// |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is shared between AddressSanitizer and ThreadSanitizer |
Kostya Serebryany | 16e0075 | 2012-05-31 13:42:53 +0000 | [diff] [blame] | 11 | // run-time libraries. See sanitizer_libc.h for details. |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Alexey Samsonov | 1f3c2fe | 2013-05-29 09:15:39 +0000 | [diff] [blame] | 13 | #include "sanitizer_allocator_internal.h" |
Alexey Samsonov | f7667cc | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 14 | #include "sanitizer_common.h" |
Kostya Serebryany | 16e0075 | 2012-05-31 13:42:53 +0000 | [diff] [blame] | 15 | #include "sanitizer_libc.h" |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 16 | |
| 17 | namespace __sanitizer { |
| 18 | |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 19 | s64 internal_atoll(const char *nptr) { |
| 20 | return internal_simple_strtoll(nptr, (char**)0, 10); |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 23 | void *internal_memchr(const void *s, int c, uptr n) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 24 | const char *t = (const char *)s; |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 25 | for (uptr i = 0; i < n; ++i, ++t) |
| 26 | if (*t == c) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 27 | return reinterpret_cast<void *>(const_cast<char *>(t)); |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 28 | return 0; |
| 29 | } |
| 30 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 31 | void *internal_memrchr(const void *s, int c, uptr n) { |
| 32 | const char *t = (const char *)s; |
| 33 | void *res = nullptr; |
| 34 | for (uptr i = 0; i < n; ++i, ++t) { |
| 35 | if (*t == c) res = reinterpret_cast<void *>(const_cast<char *>(t)); |
| 36 | } |
| 37 | return res; |
| 38 | } |
| 39 | |
Alexey Samsonov | 327c1c1 | 2012-06-14 14:04:54 +0000 | [diff] [blame] | 40 | int internal_memcmp(const void* s1, const void* s2, uptr n) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 41 | const char *t1 = (const char *)s1; |
| 42 | const char *t2 = (const char *)s2; |
Alexey Samsonov | 327c1c1 | 2012-06-14 14:04:54 +0000 | [diff] [blame] | 43 | for (uptr i = 0; i < n; ++i, ++t1, ++t2) |
| 44 | if (*t1 != *t2) |
| 45 | return *t1 < *t2 ? -1 : 1; |
| 46 | return 0; |
| 47 | } |
| 48 | |
Alexey Samsonov | f7667cc | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 49 | void *internal_memcpy(void *dest, const void *src, uptr n) { |
| 50 | char *d = (char*)dest; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 51 | const char *s = (const char *)src; |
Alexey Samsonov | f7667cc | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 52 | for (uptr i = 0; i < n; ++i) |
| 53 | d[i] = s[i]; |
| 54 | return dest; |
| 55 | } |
| 56 | |
Alexander Potapenko | f1673e6 | 2012-10-15 15:34:41 +0000 | [diff] [blame] | 57 | void *internal_memmove(void *dest, const void *src, uptr n) { |
| 58 | char *d = (char*)dest; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 59 | const char *s = (const char *)src; |
Alexander Potapenko | 71a8273 | 2012-10-26 13:47:36 +0000 | [diff] [blame] | 60 | sptr i, signed_n = (sptr)n; |
| 61 | CHECK_GE(signed_n, 0); |
Alexander Potapenko | f1673e6 | 2012-10-15 15:34:41 +0000 | [diff] [blame] | 62 | if (d < s) { |
Alexander Potapenko | 71a8273 | 2012-10-26 13:47:36 +0000 | [diff] [blame] | 63 | for (i = 0; i < signed_n; ++i) |
Alexander Potapenko | f1673e6 | 2012-10-15 15:34:41 +0000 | [diff] [blame] | 64 | d[i] = s[i]; |
| 65 | } else { |
Alexander Potapenko | 71a8273 | 2012-10-26 13:47:36 +0000 | [diff] [blame] | 66 | if (d > s && signed_n > 0) |
| 67 | for (i = signed_n - 1; i >= 0 ; --i) { |
Kostya Serebryany | 5759d92 | 2012-10-16 04:50:32 +0000 | [diff] [blame] | 68 | d[i] = s[i]; |
Alexander Potapenko | f1673e6 | 2012-10-15 15:34:41 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | return dest; |
| 72 | } |
| 73 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 74 | // Semi-fast bzero for 16-aligned data. Still far from peak performance. |
| 75 | void internal_bzero_aligned16(void *s, uptr n) { |
| 76 | struct S16 { u64 a, b; } ALIGNED(16); |
| 77 | CHECK_EQ((reinterpret_cast<uptr>(s) | n) & 15, 0); |
| 78 | for (S16 *p = reinterpret_cast<S16*>(s), *end = p + n / 16; p < end; p++) { |
| 79 | p->a = p->b = 0; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 80 | SanitizerBreakOptimization(0); // Make sure this does not become memset. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Alexey Samsonov | 4fac148 | 2012-06-08 14:11:12 +0000 | [diff] [blame] | 84 | void *internal_memset(void* s, int c, uptr n) { |
| 85 | // The next line prevents Clang from making a call to memset() instead of the |
| 86 | // loop below. |
| 87 | // FIXME: building the runtime with -ffreestanding is a better idea. However |
| 88 | // there currently are linktime problems due to PR12396. |
| 89 | char volatile *t = (char*)s; |
| 90 | for (uptr i = 0; i < n; ++i, ++t) { |
| 91 | *t = c; |
| 92 | } |
| 93 | return s; |
| 94 | } |
| 95 | |
Alexey Samsonov | 2511347 | 2012-08-21 09:26:26 +0000 | [diff] [blame] | 96 | uptr internal_strcspn(const char *s, const char *reject) { |
| 97 | uptr i; |
| 98 | for (i = 0; s[i]; i++) { |
| 99 | if (internal_strchr(reject, s[i]) != 0) |
| 100 | return i; |
| 101 | } |
| 102 | return i; |
| 103 | } |
| 104 | |
Alexey Samsonov | f7667cc | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 105 | char* internal_strdup(const char *s) { |
| 106 | uptr len = internal_strlen(s); |
| 107 | char *s2 = (char*)InternalAlloc(len + 1); |
| 108 | internal_memcpy(s2, s, len); |
| 109 | s2[len] = 0; |
| 110 | return s2; |
| 111 | } |
| 112 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 113 | char* internal_strndup(const char *s, uptr n) { |
| 114 | uptr len = internal_strnlen(s, n); |
| 115 | char *s2 = (char*)InternalAlloc(len + 1); |
| 116 | internal_memcpy(s2, s, len); |
| 117 | s2[len] = 0; |
| 118 | return s2; |
| 119 | } |
| 120 | |
Alexey Samsonov | c0d78c1 | 2012-06-04 13:27:49 +0000 | [diff] [blame] | 121 | int internal_strcmp(const char *s1, const char *s2) { |
| 122 | while (true) { |
| 123 | unsigned c1 = *s1; |
| 124 | unsigned c2 = *s2; |
| 125 | if (c1 != c2) return (c1 < c2) ? -1 : 1; |
| 126 | if (c1 == 0) break; |
| 127 | s1++; |
| 128 | s2++; |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
Alexey Samsonov | 8cd0df7 | 2012-06-18 14:34:59 +0000 | [diff] [blame] | 133 | int internal_strncmp(const char *s1, const char *s2, uptr n) { |
| 134 | for (uptr i = 0; i < n; i++) { |
| 135 | unsigned c1 = *s1; |
| 136 | unsigned c2 = *s2; |
| 137 | if (c1 != c2) return (c1 < c2) ? -1 : 1; |
| 138 | if (c1 == 0) break; |
| 139 | s1++; |
| 140 | s2++; |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
Alexey Samsonov | 88207ab | 2012-06-15 12:24:07 +0000 | [diff] [blame] | 145 | char* internal_strchr(const char *s, int c) { |
| 146 | while (true) { |
| 147 | if (*s == (char)c) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 148 | return const_cast<char *>(s); |
Alexey Samsonov | 88207ab | 2012-06-15 12:24:07 +0000 | [diff] [blame] | 149 | if (*s == 0) |
| 150 | return 0; |
| 151 | s++; |
| 152 | } |
| 153 | } |
| 154 | |
Alexey Samsonov | 72870db | 2013-09-03 13:09:28 +0000 | [diff] [blame] | 155 | char *internal_strchrnul(const char *s, int c) { |
| 156 | char *res = internal_strchr(s, c); |
| 157 | if (!res) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 158 | res = const_cast<char *>(s) + internal_strlen(s); |
Alexey Samsonov | 72870db | 2013-09-03 13:09:28 +0000 | [diff] [blame] | 159 | return res; |
| 160 | } |
| 161 | |
Alexey Samsonov | 4fac148 | 2012-06-08 14:11:12 +0000 | [diff] [blame] | 162 | char *internal_strrchr(const char *s, int c) { |
| 163 | const char *res = 0; |
| 164 | for (uptr i = 0; s[i]; i++) { |
| 165 | if (s[i] == c) res = s + i; |
| 166 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 167 | return const_cast<char *>(res); |
Alexey Samsonov | 4fac148 | 2012-06-08 14:11:12 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 170 | uptr internal_strlen(const char *s) { |
| 171 | uptr i = 0; |
| 172 | while (s[i]) i++; |
| 173 | return i; |
| 174 | } |
| 175 | |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 176 | char *internal_strncat(char *dst, const char *src, uptr n) { |
| 177 | uptr len = internal_strlen(dst); |
| 178 | uptr i; |
| 179 | for (i = 0; i < n && src[i]; i++) |
| 180 | dst[len + i] = src[i]; |
| 181 | dst[len + i] = 0; |
| 182 | return dst; |
| 183 | } |
| 184 | |
Alexey Samsonov | 3836ff2 | 2012-06-04 10:30:16 +0000 | [diff] [blame] | 185 | char *internal_strncpy(char *dst, const char *src, uptr n) { |
| 186 | uptr i; |
| 187 | for (i = 0; i < n && src[i]; i++) |
| 188 | dst[i] = src[i]; |
Will Dietz | 9843298 | 2013-08-30 19:53:55 +0000 | [diff] [blame] | 189 | internal_memset(dst + i, '\0', n - i); |
Alexey Samsonov | 3836ff2 | 2012-06-04 10:30:16 +0000 | [diff] [blame] | 190 | return dst; |
| 191 | } |
| 192 | |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 193 | uptr internal_strnlen(const char *s, uptr maxlen) { |
| 194 | uptr i = 0; |
| 195 | while (i < maxlen && s[i]) i++; |
| 196 | return i; |
| 197 | } |
| 198 | |
| 199 | char *internal_strstr(const char *haystack, const char *needle) { |
| 200 | // This is O(N^2), but we are not using it in hot places. |
| 201 | uptr len1 = internal_strlen(haystack); |
| 202 | uptr len2 = internal_strlen(needle); |
| 203 | if (len1 < len2) return 0; |
| 204 | for (uptr pos = 0; pos <= len1 - len2; pos++) { |
| 205 | if (internal_memcmp(haystack + pos, needle, len2) == 0) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 206 | return const_cast<char *>(haystack) + pos; |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | s64 internal_simple_strtoll(const char *nptr, char **endptr, int base) { |
Kostya Serebryany | 6985085 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 212 | CHECK_EQ(base, 10); |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 213 | while (IsSpace(*nptr)) nptr++; |
| 214 | int sgn = 1; |
| 215 | u64 res = 0; |
| 216 | bool have_digits = false; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 217 | char *old_nptr = const_cast<char *>(nptr); |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 218 | if (*nptr == '+') { |
| 219 | sgn = 1; |
| 220 | nptr++; |
| 221 | } else if (*nptr == '-') { |
| 222 | sgn = -1; |
| 223 | nptr++; |
| 224 | } |
| 225 | while (IsDigit(*nptr)) { |
| 226 | res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX; |
| 227 | int digit = ((*nptr) - '0'); |
| 228 | res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX; |
| 229 | have_digits = true; |
| 230 | nptr++; |
| 231 | } |
| 232 | if (endptr != 0) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 233 | *endptr = (have_digits) ? const_cast<char *>(nptr) : old_nptr; |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 234 | } |
| 235 | if (sgn > 0) { |
| 236 | return (s64)(Min((u64)INT64_MAX, res)); |
| 237 | } else { |
| 238 | return (res > INT64_MAX) ? INT64_MIN : ((s64)res * -1); |
| 239 | } |
| 240 | } |
| 241 | |
Kostya Serebryany | eb28093 | 2012-12-28 15:24:16 +0000 | [diff] [blame] | 242 | bool mem_is_zero(const char *beg, uptr size) { |
Dmitry Vyukov | 8ceeec4 | 2013-02-04 08:07:45 +0000 | [diff] [blame] | 243 | CHECK_LE(size, 1ULL << FIRST_32_SECOND_64(30, 40)); // Sanity check. |
Kostya Serebryany | eb28093 | 2012-12-28 15:24:16 +0000 | [diff] [blame] | 244 | const char *end = beg + size; |
| 245 | uptr *aligned_beg = (uptr *)RoundUpTo((uptr)beg, sizeof(uptr)); |
| 246 | uptr *aligned_end = (uptr *)RoundDownTo((uptr)end, sizeof(uptr)); |
| 247 | uptr all = 0; |
| 248 | // Prologue. |
| 249 | for (const char *mem = beg; mem < (char*)aligned_beg && mem < end; mem++) |
| 250 | all |= *mem; |
| 251 | // Aligned loop. |
| 252 | for (; aligned_beg < aligned_end; aligned_beg++) |
| 253 | all |= *aligned_beg; |
| 254 | // Epilogue. |
| 255 | if ((char*)aligned_end >= beg) |
| 256 | for (const char *mem = (char*)aligned_end; mem < end; mem++) |
| 257 | all |= *mem; |
| 258 | return all == 0; |
| 259 | } |
| 260 | |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 261 | } // namespace __sanitizer |