* c-parser.c (c_parser_omp_requires): Call sorry_at on requires
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_libc.cc
blob94fa69b6a2da260b13f5ef3f1fbe1ec8c354649d
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 //===----------------------------------------------------------------------===//
12 #include "sanitizer_allocator_internal.h"
13 #include "sanitizer_common.h"
14 #include "sanitizer_libc.h"
16 namespace __sanitizer {
18 s64 internal_atoll(const char *nptr) {
19 return internal_simple_strtoll(nptr, nullptr, 10);
22 void *internal_memchr(const void *s, int c, uptr n) {
23 const char *t = (const char *)s;
24 for (uptr i = 0; i < n; ++i, ++t)
25 if (*t == c)
26 return reinterpret_cast<void *>(const_cast<char *>(t));
27 return nullptr;
30 void *internal_memrchr(const void *s, int c, uptr n) {
31 const char *t = (const char *)s;
32 void *res = nullptr;
33 for (uptr i = 0; i < n; ++i, ++t) {
34 if (*t == c) res = reinterpret_cast<void *>(const_cast<char *>(t));
36 return res;
39 int internal_memcmp(const void* s1, const void* s2, uptr n) {
40 const char *t1 = (const char *)s1;
41 const char *t2 = (const char *)s2;
42 for (uptr i = 0; i < n; ++i, ++t1, ++t2)
43 if (*t1 != *t2)
44 return *t1 < *t2 ? -1 : 1;
45 return 0;
48 void *internal_memcpy(void *dest, const void *src, uptr n) {
49 char *d = (char*)dest;
50 const char *s = (const char *)src;
51 for (uptr i = 0; i < n; ++i)
52 d[i] = s[i];
53 return dest;
56 void *internal_memmove(void *dest, const void *src, uptr n) {
57 char *d = (char*)dest;
58 const char *s = (const char *)src;
59 sptr i, signed_n = (sptr)n;
60 CHECK_GE(signed_n, 0);
61 if (d < s) {
62 for (i = 0; i < signed_n; ++i)
63 d[i] = s[i];
64 } else {
65 if (d > s && signed_n > 0)
66 for (i = signed_n - 1; i >= 0 ; --i) {
67 d[i] = s[i];
70 return dest;
73 void *internal_memset(void* s, int c, uptr n) {
74 // Optimize for the most performance-critical case:
75 if ((reinterpret_cast<uptr>(s) % 16) == 0 && (n % 16) == 0) {
76 u64 *p = reinterpret_cast<u64*>(s);
77 u64 *e = p + n / 8;
78 u64 v = c;
79 v |= v << 8;
80 v |= v << 16;
81 v |= v << 32;
82 for (; p < e; p += 2)
83 p[0] = p[1] = v;
84 return s;
86 // The next line prevents Clang from making a call to memset() instead of the
87 // loop below.
88 // FIXME: building the runtime with -ffreestanding is a better idea. However
89 // there currently are linktime problems due to PR12396.
90 char volatile *t = (char*)s;
91 for (uptr i = 0; i < n; ++i, ++t) {
92 *t = c;
94 return s;
97 uptr internal_strcspn(const char *s, const char *reject) {
98 uptr i;
99 for (i = 0; s[i]; i++) {
100 if (internal_strchr(reject, s[i]))
101 return i;
103 return i;
106 char* internal_strdup(const char *s) {
107 uptr len = internal_strlen(s);
108 char *s2 = (char*)InternalAlloc(len + 1);
109 internal_memcpy(s2, s, len);
110 s2[len] = 0;
111 return s2;
114 int internal_strcmp(const char *s1, const char *s2) {
115 while (true) {
116 unsigned c1 = *s1;
117 unsigned c2 = *s2;
118 if (c1 != c2) return (c1 < c2) ? -1 : 1;
119 if (c1 == 0) break;
120 s1++;
121 s2++;
123 return 0;
126 int internal_strncmp(const char *s1, const char *s2, uptr n) {
127 for (uptr i = 0; i < n; i++) {
128 unsigned c1 = *s1;
129 unsigned c2 = *s2;
130 if (c1 != c2) return (c1 < c2) ? -1 : 1;
131 if (c1 == 0) break;
132 s1++;
133 s2++;
135 return 0;
138 char* internal_strchr(const char *s, int c) {
139 while (true) {
140 if (*s == (char)c)
141 return const_cast<char *>(s);
142 if (*s == 0)
143 return nullptr;
144 s++;
148 char *internal_strchrnul(const char *s, int c) {
149 char *res = internal_strchr(s, c);
150 if (!res)
151 res = const_cast<char *>(s) + internal_strlen(s);
152 return res;
155 char *internal_strrchr(const char *s, int c) {
156 const char *res = nullptr;
157 for (uptr i = 0; s[i]; i++) {
158 if (s[i] == c) res = s + i;
160 return const_cast<char *>(res);
163 uptr internal_strlen(const char *s) {
164 uptr i = 0;
165 while (s[i]) i++;
166 return i;
169 uptr internal_strlcat(char *dst, const char *src, uptr maxlen) {
170 const uptr srclen = internal_strlen(src);
171 const uptr dstlen = internal_strnlen(dst, maxlen);
172 if (dstlen == maxlen) return maxlen + srclen;
173 if (srclen < maxlen - dstlen) {
174 internal_memmove(dst + dstlen, src, srclen + 1);
175 } else {
176 internal_memmove(dst + dstlen, src, maxlen - dstlen - 1);
177 dst[maxlen - 1] = '\0';
179 return dstlen + srclen;
182 char *internal_strncat(char *dst, const char *src, uptr n) {
183 uptr len = internal_strlen(dst);
184 uptr i;
185 for (i = 0; i < n && src[i]; i++)
186 dst[len + i] = src[i];
187 dst[len + i] = 0;
188 return dst;
191 uptr internal_strlcpy(char *dst, const char *src, uptr maxlen) {
192 const uptr srclen = internal_strlen(src);
193 if (srclen < maxlen) {
194 internal_memmove(dst, src, srclen + 1);
195 } else if (maxlen != 0) {
196 internal_memmove(dst, src, maxlen - 1);
197 dst[maxlen - 1] = '\0';
199 return srclen;
202 char *internal_strncpy(char *dst, const char *src, uptr n) {
203 uptr i;
204 for (i = 0; i < n && src[i]; i++)
205 dst[i] = src[i];
206 internal_memset(dst + i, '\0', n - i);
207 return dst;
210 uptr internal_strnlen(const char *s, uptr maxlen) {
211 uptr i = 0;
212 while (i < maxlen && s[i]) i++;
213 return i;
216 char *internal_strstr(const char *haystack, const char *needle) {
217 // This is O(N^2), but we are not using it in hot places.
218 uptr len1 = internal_strlen(haystack);
219 uptr len2 = internal_strlen(needle);
220 if (len1 < len2) return nullptr;
221 for (uptr pos = 0; pos <= len1 - len2; pos++) {
222 if (internal_memcmp(haystack + pos, needle, len2) == 0)
223 return const_cast<char *>(haystack) + pos;
225 return nullptr;
228 s64 internal_simple_strtoll(const char *nptr, const char **endptr, int base) {
229 CHECK_EQ(base, 10);
230 while (IsSpace(*nptr)) nptr++;
231 int sgn = 1;
232 u64 res = 0;
233 bool have_digits = false;
234 char *old_nptr = const_cast<char *>(nptr);
235 if (*nptr == '+') {
236 sgn = 1;
237 nptr++;
238 } else if (*nptr == '-') {
239 sgn = -1;
240 nptr++;
242 while (IsDigit(*nptr)) {
243 res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX;
244 int digit = ((*nptr) - '0');
245 res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX;
246 have_digits = true;
247 nptr++;
249 if (endptr) {
250 *endptr = (have_digits) ? const_cast<char *>(nptr) : old_nptr;
252 if (sgn > 0) {
253 return (s64)(Min((u64)INT64_MAX, res));
254 } else {
255 return (res > INT64_MAX) ? INT64_MIN : ((s64)res * -1);
259 bool mem_is_zero(const char *beg, uptr size) {
260 CHECK_LE(size, 1ULL << FIRST_32_SECOND_64(30, 40)); // Sanity check.
261 const char *end = beg + size;
262 uptr *aligned_beg = (uptr *)RoundUpTo((uptr)beg, sizeof(uptr));
263 uptr *aligned_end = (uptr *)RoundDownTo((uptr)end, sizeof(uptr));
264 uptr all = 0;
265 // Prologue.
266 for (const char *mem = beg; mem < (char*)aligned_beg && mem < end; mem++)
267 all |= *mem;
268 // Aligned loop.
269 for (; aligned_beg < aligned_end; aligned_beg++)
270 all |= *aligned_beg;
271 // Epilogue.
272 if ((char*)aligned_end >= beg)
273 for (const char *mem = (char*)aligned_end; mem < end; mem++)
274 all |= *mem;
275 return all == 0;
278 } // namespace __sanitizer