1 //===-- sanitizer_libc.cc -------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
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
)
26 return reinterpret_cast<void *>(const_cast<char *>(t
));
30 void *internal_memrchr(const void *s
, int c
, uptr n
) {
31 const char *t
= (const char *)s
;
33 for (uptr i
= 0; i
< n
; ++i
, ++t
) {
34 if (*t
== c
) res
= reinterpret_cast<void *>(const_cast<char *>(t
));
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
)
44 return *t1
< *t2
? -1 : 1;
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
)
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);
62 for (i
= 0; i
< signed_n
; ++i
)
65 if (d
> s
&& signed_n
> 0)
66 for (i
= signed_n
- 1; i
>= 0 ; --i
) {
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
);
86 // The next line prevents Clang from making a call to memset() instead of the
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
) {
97 uptr
internal_strcspn(const char *s
, const char *reject
) {
99 for (i
= 0; s
[i
]; i
++) {
100 if (internal_strchr(reject
, s
[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
);
114 int internal_strcmp(const char *s1
, const char *s2
) {
118 if (c1
!= c2
) return (c1
< c2
) ? -1 : 1;
126 int internal_strncmp(const char *s1
, const char *s2
, uptr n
) {
127 for (uptr i
= 0; i
< n
; i
++) {
130 if (c1
!= c2
) return (c1
< c2
) ? -1 : 1;
138 char* internal_strchr(const char *s
, int c
) {
141 return const_cast<char *>(s
);
148 char *internal_strchrnul(const char *s
, int c
) {
149 char *res
= internal_strchr(s
, c
);
151 res
= const_cast<char *>(s
) + internal_strlen(s
);
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
) {
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);
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
);
185 for (i
= 0; i
< n
&& src
[i
]; i
++)
186 dst
[len
+ i
] = src
[i
];
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';
202 char *internal_strncpy(char *dst
, const char *src
, uptr n
) {
204 for (i
= 0; i
< n
&& src
[i
]; i
++)
206 internal_memset(dst
+ i
, '\0', n
- i
);
210 uptr
internal_strnlen(const char *s
, uptr maxlen
) {
212 while (i
< maxlen
&& s
[i
]) 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
;
228 s64
internal_simple_strtoll(const char *nptr
, const char **endptr
, int base
) {
230 while (IsSpace(*nptr
)) nptr
++;
233 bool have_digits
= false;
234 char *old_nptr
= const_cast<char *>(nptr
);
238 } else if (*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
;
250 *endptr
= (have_digits
) ? const_cast<char *>(nptr
) : old_nptr
;
253 return (s64
)(Min((u64
)INT64_MAX
, res
));
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
));
266 for (const char *mem
= beg
; mem
< (char*)aligned_beg
&& mem
< end
; mem
++)
269 for (; aligned_beg
< aligned_end
; aligned_beg
++)
272 if ((char*)aligned_end
>= beg
)
273 for (const char *mem
= (char*)aligned_end
; mem
< end
; mem
++)
278 } // namespace __sanitizer