Home | History | Annotate | Download | only in sanitizer_common
      1 //===-- sanitizer_libc.cc -------------------------------------------------===//
      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
     11 // run-time libraries. See sanitizer_libc.h for details.
     12 //===----------------------------------------------------------------------===//
     13 #include "sanitizer_allocator_internal.h"
     14 #include "sanitizer_common.h"
     15 #include "sanitizer_libc.h"
     16 
     17 namespace __sanitizer {
     18 
     19 s64 internal_atoll(const char *nptr) {
     20   return internal_simple_strtoll(nptr, (char**)0, 10);
     21 }
     22 
     23 void *internal_memchr(const void *s, int c, uptr n) {
     24   const char *t = (const char *)s;
     25   for (uptr i = 0; i < n; ++i, ++t)
     26     if (*t == c)
     27       return reinterpret_cast<void *>(const_cast<char *>(t));
     28   return 0;
     29 }
     30 
     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 
     40 int internal_memcmp(const void* s1, const void* s2, uptr n) {
     41   const char *t1 = (const char *)s1;
     42   const char *t2 = (const char *)s2;
     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 
     49 void *internal_memcpy(void *dest, const void *src, uptr n) {
     50   char *d = (char*)dest;
     51   const char *s = (const char *)src;
     52   for (uptr i = 0; i < n; ++i)
     53     d[i] = s[i];
     54   return dest;
     55 }
     56 
     57 void *internal_memmove(void *dest, const void *src, uptr n) {
     58   char *d = (char*)dest;
     59   const char *s = (const char *)src;
     60   sptr i, signed_n = (sptr)n;
     61   CHECK_GE(signed_n, 0);
     62   if (d < s) {
     63     for (i = 0; i < signed_n; ++i)
     64       d[i] = s[i];
     65   } else {
     66     if (d > s && signed_n > 0)
     67       for (i = signed_n - 1; i >= 0 ; --i) {
     68         d[i] = s[i];
     69       }
     70   }
     71   return dest;
     72 }
     73 
     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;
     80     SanitizerBreakOptimization(0);  // Make sure this does not become memset.
     81   }
     82 }
     83 
     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 
     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 
    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 
    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 
    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 
    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 
    145 char* internal_strchr(const char *s, int c) {
    146   while (true) {
    147     if (*s == (char)c)
    148       return const_cast<char *>(s);
    149     if (*s == 0)
    150       return 0;
    151     s++;
    152   }
    153 }
    154 
    155 char *internal_strchrnul(const char *s, int c) {
    156   char *res = internal_strchr(s, c);
    157   if (!res)
    158     res = const_cast<char *>(s) + internal_strlen(s);
    159   return res;
    160 }
    161 
    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   }
    167   return const_cast<char *>(res);
    168 }
    169 
    170 uptr internal_strlen(const char *s) {
    171   uptr i = 0;
    172   while (s[i]) i++;
    173   return i;
    174 }
    175 
    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 
    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];
    189   internal_memset(dst + i, '\0', n - i);
    190   return dst;
    191 }
    192 
    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)
    206       return const_cast<char *>(haystack) + pos;
    207   }
    208   return 0;
    209 }
    210 
    211 s64 internal_simple_strtoll(const char *nptr, char **endptr, int base) {
    212   CHECK_EQ(base, 10);
    213   while (IsSpace(*nptr)) nptr++;
    214   int sgn = 1;
    215   u64 res = 0;
    216   bool have_digits = false;
    217   char *old_nptr = const_cast<char *>(nptr);
    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) {
    233     *endptr = (have_digits) ? const_cast<char *>(nptr) : old_nptr;
    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 
    242 bool mem_is_zero(const char *beg, uptr size) {
    243   CHECK_LE(size, 1ULL << FIRST_32_SECOND_64(30, 40));  // Sanity check.
    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 
    261 }  // namespace __sanitizer
    262