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