sanitizer_libc.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //===-- sanitizer_libc.cc -------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between AddressSanitizer and ThreadSanitizer
  9. // run-time libraries. See sanitizer_libc.h for details.
  10. //===----------------------------------------------------------------------===//
  11. #include "sanitizer_allocator_internal.h"
  12. #include "sanitizer_common.h"
  13. #include "sanitizer_libc.h"
  14. namespace __sanitizer {
  15. // Make the compiler think that something is going on there.
  16. static inline void break_optimization(void *arg) {
  17. #if _MSC_VER
  18. // FIXME: make sure this is actually enough.
  19. __asm;
  20. #else
  21. __asm__ __volatile__("" : : "r" (arg) : "memory");
  22. #endif
  23. }
  24. s64 internal_atoll(const char *nptr) {
  25. return internal_simple_strtoll(nptr, (char**)0, 10);
  26. }
  27. void *internal_memchr(const void *s, int c, uptr n) {
  28. const char* t = (char*)s;
  29. for (uptr i = 0; i < n; ++i, ++t)
  30. if (*t == c)
  31. return (void*)t;
  32. return 0;
  33. }
  34. int internal_memcmp(const void* s1, const void* s2, uptr n) {
  35. const char* t1 = (char*)s1;
  36. const char* t2 = (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. char *s = (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. char *s = (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. return dest;
  64. }
  65. // Semi-fast bzero for 16-aligned data. Still far from peak performance.
  66. void internal_bzero_aligned16(void *s, uptr n) {
  67. struct S16 { u64 a, b; } ALIGNED(16);
  68. CHECK_EQ((reinterpret_cast<uptr>(s) | n) & 15, 0);
  69. for (S16 *p = reinterpret_cast<S16*>(s), *end = p + n / 16; p < end; p++) {
  70. p->a = p->b = 0;
  71. break_optimization(0); // Make sure this does not become memset.
  72. }
  73. }
  74. void *internal_memset(void* s, int c, uptr n) {
  75. // The next line prevents Clang from making a call to memset() instead of the
  76. // loop below.
  77. // FIXME: building the runtime with -ffreestanding is a better idea. However
  78. // there currently are linktime problems due to PR12396.
  79. char volatile *t = (char*)s;
  80. for (uptr i = 0; i < n; ++i, ++t) {
  81. *t = c;
  82. }
  83. return s;
  84. }
  85. uptr internal_strcspn(const char *s, const char *reject) {
  86. uptr i;
  87. for (i = 0; s[i]; i++) {
  88. if (internal_strchr(reject, s[i]) != 0)
  89. return i;
  90. }
  91. return i;
  92. }
  93. char* internal_strdup(const char *s) {
  94. uptr len = internal_strlen(s);
  95. char *s2 = (char*)InternalAlloc(len + 1);
  96. internal_memcpy(s2, s, len);
  97. s2[len] = 0;
  98. return s2;
  99. }
  100. int internal_strcmp(const char *s1, const char *s2) {
  101. while (true) {
  102. unsigned c1 = *s1;
  103. unsigned c2 = *s2;
  104. if (c1 != c2) return (c1 < c2) ? -1 : 1;
  105. if (c1 == 0) break;
  106. s1++;
  107. s2++;
  108. }
  109. return 0;
  110. }
  111. int internal_strncmp(const char *s1, const char *s2, uptr n) {
  112. for (uptr i = 0; i < n; i++) {
  113. unsigned c1 = *s1;
  114. unsigned c2 = *s2;
  115. if (c1 != c2) return (c1 < c2) ? -1 : 1;
  116. if (c1 == 0) break;
  117. s1++;
  118. s2++;
  119. }
  120. return 0;
  121. }
  122. char* internal_strchr(const char *s, int c) {
  123. while (true) {
  124. if (*s == (char)c)
  125. return (char*)s;
  126. if (*s == 0)
  127. return 0;
  128. s++;
  129. }
  130. }
  131. char *internal_strchrnul(const char *s, int c) {
  132. char *res = internal_strchr(s, c);
  133. if (!res)
  134. res = (char*)s + internal_strlen(s);
  135. return res;
  136. }
  137. char *internal_strrchr(const char *s, int c) {
  138. const char *res = 0;
  139. for (uptr i = 0; s[i]; i++) {
  140. if (s[i] == c) res = s + i;
  141. }
  142. return (char*)res;
  143. }
  144. uptr internal_strlen(const char *s) {
  145. uptr i = 0;
  146. while (s[i]) i++;
  147. return i;
  148. }
  149. char *internal_strncat(char *dst, const char *src, uptr n) {
  150. uptr len = internal_strlen(dst);
  151. uptr i;
  152. for (i = 0; i < n && src[i]; i++)
  153. dst[len + i] = src[i];
  154. dst[len + i] = 0;
  155. return dst;
  156. }
  157. char *internal_strncpy(char *dst, const char *src, uptr n) {
  158. uptr i;
  159. for (i = 0; i < n && src[i]; i++)
  160. dst[i] = src[i];
  161. internal_memset(dst + i, '\0', n - i);
  162. return dst;
  163. }
  164. uptr internal_strnlen(const char *s, uptr maxlen) {
  165. uptr i = 0;
  166. while (i < maxlen && s[i]) i++;
  167. return i;
  168. }
  169. char *internal_strstr(const char *haystack, const char *needle) {
  170. // This is O(N^2), but we are not using it in hot places.
  171. uptr len1 = internal_strlen(haystack);
  172. uptr len2 = internal_strlen(needle);
  173. if (len1 < len2) return 0;
  174. for (uptr pos = 0; pos <= len1 - len2; pos++) {
  175. if (internal_memcmp(haystack + pos, needle, len2) == 0)
  176. return (char*)haystack + pos;
  177. }
  178. return 0;
  179. }
  180. s64 internal_simple_strtoll(const char *nptr, char **endptr, int base) {
  181. CHECK_EQ(base, 10);
  182. while (IsSpace(*nptr)) nptr++;
  183. int sgn = 1;
  184. u64 res = 0;
  185. bool have_digits = false;
  186. char *old_nptr = (char*)nptr;
  187. if (*nptr == '+') {
  188. sgn = 1;
  189. nptr++;
  190. } else if (*nptr == '-') {
  191. sgn = -1;
  192. nptr++;
  193. }
  194. while (IsDigit(*nptr)) {
  195. res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX;
  196. int digit = ((*nptr) - '0');
  197. res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX;
  198. have_digits = true;
  199. nptr++;
  200. }
  201. if (endptr != 0) {
  202. *endptr = (have_digits) ? (char*)nptr : old_nptr;
  203. }
  204. if (sgn > 0) {
  205. return (s64)(Min((u64)INT64_MAX, res));
  206. } else {
  207. return (res > INT64_MAX) ? INT64_MIN : ((s64)res * -1);
  208. }
  209. }
  210. bool mem_is_zero(const char *beg, uptr size) {
  211. CHECK_LE(size, 1ULL << FIRST_32_SECOND_64(30, 40)); // Sanity check.
  212. const char *end = beg + size;
  213. uptr *aligned_beg = (uptr *)RoundUpTo((uptr)beg, sizeof(uptr));
  214. uptr *aligned_end = (uptr *)RoundDownTo((uptr)end, sizeof(uptr));
  215. uptr all = 0;
  216. // Prologue.
  217. for (const char *mem = beg; mem < (char*)aligned_beg && mem < end; mem++)
  218. all |= *mem;
  219. // Aligned loop.
  220. for (; aligned_beg < aligned_end; aligned_beg++)
  221. all |= *aligned_beg;
  222. // Epilogue.
  223. if ((char*)aligned_end >= beg)
  224. for (const char *mem = (char*)aligned_end; mem < end; mem++)
  225. all |= *mem;
  226. return all == 0;
  227. }
  228. } // namespace __sanitizer