sanitizer_libc.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //===-- sanitizer_libc.cpp ------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between AddressSanitizer and ThreadSanitizer
  10. // run-time libraries. See sanitizer_libc.h for details.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_allocator_internal.h"
  13. #include "sanitizer_common.h"
  14. #include "sanitizer_libc.h"
  15. namespace __sanitizer {
  16. s64 internal_atoll(const char *nptr) {
  17. return internal_simple_strtoll(nptr, nullptr, 10);
  18. }
  19. void *internal_memchr(const void *s, int c, uptr n) {
  20. const char *t = (const char *)s;
  21. for (uptr i = 0; i < n; ++i, ++t)
  22. if (*t == c)
  23. return reinterpret_cast<void *>(const_cast<char *>(t));
  24. return nullptr;
  25. }
  26. void *internal_memrchr(const void *s, int c, uptr n) {
  27. const char *t = (const char *)s;
  28. void *res = nullptr;
  29. for (uptr i = 0; i < n; ++i, ++t) {
  30. if (*t == c) res = reinterpret_cast<void *>(const_cast<char *>(t));
  31. }
  32. return res;
  33. }
  34. int internal_memcmp(const void* s1, const void* s2, uptr n) {
  35. const char *t1 = (const char *)s1;
  36. const char *t2 = (const char *)s2;
  37. for (uptr i = 0; i < n; ++i, ++t1, ++t2)
  38. if (*t1 != *t2)
  39. return *t1 < *t2 ? -1 : 1;
  40. return 0;
  41. }
  42. void *internal_memcpy(void *dest, const void *src, uptr n) {
  43. char *d = (char*)dest;
  44. const char *s = (const char *)src;
  45. for (uptr i = 0; i < n; ++i)
  46. d[i] = s[i];
  47. return dest;
  48. }
  49. void *internal_memmove(void *dest, const void *src, uptr n) {
  50. char *d = (char*)dest;
  51. const char *s = (const char *)src;
  52. sptr i, signed_n = (sptr)n;
  53. CHECK_GE(signed_n, 0);
  54. if (d < s) {
  55. for (i = 0; i < signed_n; ++i)
  56. d[i] = s[i];
  57. } else {
  58. if (d > s && signed_n > 0) {
  59. for (i = signed_n - 1; i >= 0; --i) {
  60. d[i] = s[i];
  61. }
  62. }
  63. }
  64. return dest;
  65. }
  66. void *internal_memset(void* s, int c, uptr n) {
  67. // Optimize for the most performance-critical case:
  68. if ((reinterpret_cast<uptr>(s) % 16) == 0 && (n % 16) == 0) {
  69. u64 *p = reinterpret_cast<u64*>(s);
  70. u64 *e = p + n / 8;
  71. u64 v = c;
  72. v |= v << 8;
  73. v |= v << 16;
  74. v |= v << 32;
  75. for (; p < e; p += 2)
  76. p[0] = p[1] = v;
  77. return s;
  78. }
  79. // The next line prevents Clang from making a call to memset() instead of the
  80. // loop below.
  81. // FIXME: building the runtime with -ffreestanding is a better idea. However
  82. // there currently are linktime problems due to PR12396.
  83. char volatile *t = (char*)s;
  84. for (uptr i = 0; i < n; ++i, ++t) {
  85. *t = c;
  86. }
  87. return s;
  88. }
  89. uptr internal_strcspn(const char *s, const char *reject) {
  90. uptr i;
  91. for (i = 0; s[i]; i++) {
  92. if (internal_strchr(reject, s[i]))
  93. return i;
  94. }
  95. return i;
  96. }
  97. char* internal_strdup(const char *s) {
  98. uptr len = internal_strlen(s);
  99. char *s2 = (char*)InternalAlloc(len + 1);
  100. internal_memcpy(s2, s, len);
  101. s2[len] = 0;
  102. return s2;
  103. }
  104. int internal_strcmp(const char *s1, const char *s2) {
  105. while (true) {
  106. unsigned c1 = *s1;
  107. unsigned c2 = *s2;
  108. if (c1 != c2) return (c1 < c2) ? -1 : 1;
  109. if (c1 == 0) break;
  110. s1++;
  111. s2++;
  112. }
  113. return 0;
  114. }
  115. int internal_strncmp(const char *s1, const char *s2, uptr n) {
  116. for (uptr i = 0; i < n; i++) {
  117. unsigned c1 = *s1;
  118. unsigned c2 = *s2;
  119. if (c1 != c2) return (c1 < c2) ? -1 : 1;
  120. if (c1 == 0) break;
  121. s1++;
  122. s2++;
  123. }
  124. return 0;
  125. }
  126. char* internal_strchr(const char *s, int c) {
  127. while (true) {
  128. if (*s == (char)c)
  129. return const_cast<char *>(s);
  130. if (*s == 0)
  131. return nullptr;
  132. s++;
  133. }
  134. }
  135. char *internal_strchrnul(const char *s, int c) {
  136. char *res = internal_strchr(s, c);
  137. if (!res)
  138. res = const_cast<char *>(s) + internal_strlen(s);
  139. return res;
  140. }
  141. char *internal_strrchr(const char *s, int c) {
  142. const char *res = nullptr;
  143. for (uptr i = 0; s[i]; i++) {
  144. if (s[i] == c) res = s + i;
  145. }
  146. return const_cast<char *>(res);
  147. }
  148. uptr internal_strlen(const char *s) {
  149. uptr i = 0;
  150. while (s[i]) i++;
  151. return i;
  152. }
  153. uptr internal_strlcat(char *dst, const char *src, uptr maxlen) {
  154. const uptr srclen = internal_strlen(src);
  155. const uptr dstlen = internal_strnlen(dst, maxlen);
  156. if (dstlen == maxlen) return maxlen + srclen;
  157. if (srclen < maxlen - dstlen) {
  158. internal_memmove(dst + dstlen, src, srclen + 1);
  159. } else {
  160. internal_memmove(dst + dstlen, src, maxlen - dstlen - 1);
  161. dst[maxlen - 1] = '\0';
  162. }
  163. return dstlen + srclen;
  164. }
  165. char *internal_strncat(char *dst, const char *src, uptr n) {
  166. uptr len = internal_strlen(dst);
  167. uptr i;
  168. for (i = 0; i < n && src[i]; i++)
  169. dst[len + i] = src[i];
  170. dst[len + i] = 0;
  171. return dst;
  172. }
  173. uptr internal_strlcpy(char *dst, const char *src, uptr maxlen) {
  174. const uptr srclen = internal_strlen(src);
  175. if (srclen < maxlen) {
  176. internal_memmove(dst, src, srclen + 1);
  177. } else if (maxlen != 0) {
  178. internal_memmove(dst, src, maxlen - 1);
  179. dst[maxlen - 1] = '\0';
  180. }
  181. return srclen;
  182. }
  183. char *internal_strncpy(char *dst, const char *src, uptr n) {
  184. uptr i;
  185. for (i = 0; i < n && src[i]; i++)
  186. dst[i] = src[i];
  187. internal_memset(dst + i, '\0', n - i);
  188. return dst;
  189. }
  190. uptr internal_strnlen(const char *s, uptr maxlen) {
  191. uptr i = 0;
  192. while (i < maxlen && s[i]) i++;
  193. return i;
  194. }
  195. char *internal_strstr(const char *haystack, const char *needle) {
  196. // This is O(N^2), but we are not using it in hot places.
  197. uptr len1 = internal_strlen(haystack);
  198. uptr len2 = internal_strlen(needle);
  199. if (len1 < len2) return nullptr;
  200. for (uptr pos = 0; pos <= len1 - len2; pos++) {
  201. if (internal_memcmp(haystack + pos, needle, len2) == 0)
  202. return const_cast<char *>(haystack) + pos;
  203. }
  204. return nullptr;
  205. }
  206. s64 internal_simple_strtoll(const char *nptr, const char **endptr, int base) {
  207. CHECK_EQ(base, 10);
  208. while (IsSpace(*nptr)) nptr++;
  209. int sgn = 1;
  210. u64 res = 0;
  211. bool have_digits = false;
  212. char *old_nptr = const_cast<char *>(nptr);
  213. if (*nptr == '+') {
  214. sgn = 1;
  215. nptr++;
  216. } else if (*nptr == '-') {
  217. sgn = -1;
  218. nptr++;
  219. }
  220. while (IsDigit(*nptr)) {
  221. res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX;
  222. int digit = ((*nptr) - '0');
  223. res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX;
  224. have_digits = true;
  225. nptr++;
  226. }
  227. if (endptr) {
  228. *endptr = (have_digits) ? const_cast<char *>(nptr) : old_nptr;
  229. }
  230. if (sgn > 0) {
  231. return (s64)(Min((u64)INT64_MAX, res));
  232. } else {
  233. return (res > INT64_MAX) ? INT64_MIN : ((s64)res * -1);
  234. }
  235. }
  236. uptr internal_wcslen(const wchar_t *s) {
  237. uptr i = 0;
  238. while (s[i]) i++;
  239. return i;
  240. }
  241. uptr internal_wcsnlen(const wchar_t *s, uptr maxlen) {
  242. uptr i = 0;
  243. while (i < maxlen && s[i]) i++;
  244. return i;
  245. }
  246. bool mem_is_zero(const char *beg, uptr size) {
  247. CHECK_LE(size, 1ULL << FIRST_32_SECOND_64(30, 40)); // Sanity check.
  248. const char *end = beg + size;
  249. uptr *aligned_beg = (uptr *)RoundUpTo((uptr)beg, sizeof(uptr));
  250. uptr *aligned_end = (uptr *)RoundDownTo((uptr)end, sizeof(uptr));
  251. uptr all = 0;
  252. // Prologue.
  253. for (const char *mem = beg; mem < (char*)aligned_beg && mem < end; mem++)
  254. all |= *mem;
  255. // Aligned loop.
  256. for (; aligned_beg < aligned_end; aligned_beg++)
  257. all |= *aligned_beg;
  258. // Epilogue.
  259. if ((char *)aligned_end >= beg) {
  260. for (const char *mem = (char *)aligned_end; mem < end; mem++) all |= *mem;
  261. }
  262. return all == 0;
  263. }
  264. } // namespace __sanitizer