• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_common.h"
14 #include "sanitizer_libc.h"
15 
16 namespace __sanitizer {
17 
internal_atoll(const char * nptr)18 s64 internal_atoll(const char *nptr) {
19   return internal_simple_strtoll(nptr, (char**)0, 10);
20 }
21 
internal_memchr(const void * s,int c,uptr n)22 void *internal_memchr(const void *s, int c, uptr n) {
23   const char* t = (char*)s;
24   for (uptr i = 0; i < n; ++i, ++t)
25     if (*t == c)
26       return (void*)t;
27   return 0;
28 }
29 
internal_memcmp(const void * s1,const void * s2,uptr n)30 int internal_memcmp(const void* s1, const void* s2, uptr n) {
31   const char* t1 = (char*)s1;
32   const char* t2 = (char*)s2;
33   for (uptr i = 0; i < n; ++i, ++t1, ++t2)
34     if (*t1 != *t2)
35       return *t1 < *t2 ? -1 : 1;
36   return 0;
37 }
38 
internal_memcpy(void * dest,const void * src,uptr n)39 void *internal_memcpy(void *dest, const void *src, uptr n) {
40   char *d = (char*)dest;
41   char *s = (char*)src;
42   for (uptr i = 0; i < n; ++i)
43     d[i] = s[i];
44   return dest;
45 }
46 
internal_memset(void * s,int c,uptr n)47 void *internal_memset(void* s, int c, uptr n) {
48   // The next line prevents Clang from making a call to memset() instead of the
49   // loop below.
50   // FIXME: building the runtime with -ffreestanding is a better idea. However
51   // there currently are linktime problems due to PR12396.
52   char volatile *t = (char*)s;
53   for (uptr i = 0; i < n; ++i, ++t) {
54     *t = c;
55   }
56   return s;
57 }
58 
internal_strcspn(const char * s,const char * reject)59 uptr internal_strcspn(const char *s, const char *reject) {
60   uptr i;
61   for (i = 0; s[i]; i++) {
62     if (internal_strchr(reject, s[i]) != 0)
63       return i;
64   }
65   return i;
66 }
67 
internal_strdup(const char * s)68 char* internal_strdup(const char *s) {
69   uptr len = internal_strlen(s);
70   char *s2 = (char*)InternalAlloc(len + 1);
71   internal_memcpy(s2, s, len);
72   s2[len] = 0;
73   return s2;
74 }
75 
internal_strcmp(const char * s1,const char * s2)76 int internal_strcmp(const char *s1, const char *s2) {
77   while (true) {
78     unsigned c1 = *s1;
79     unsigned c2 = *s2;
80     if (c1 != c2) return (c1 < c2) ? -1 : 1;
81     if (c1 == 0) break;
82     s1++;
83     s2++;
84   }
85   return 0;
86 }
87 
internal_strncmp(const char * s1,const char * s2,uptr n)88 int internal_strncmp(const char *s1, const char *s2, uptr n) {
89   for (uptr i = 0; i < n; i++) {
90     unsigned c1 = *s1;
91     unsigned c2 = *s2;
92     if (c1 != c2) return (c1 < c2) ? -1 : 1;
93     if (c1 == 0) break;
94     s1++;
95     s2++;
96   }
97   return 0;
98 }
99 
internal_strchr(const char * s,int c)100 char* internal_strchr(const char *s, int c) {
101   while (true) {
102     if (*s == (char)c)
103       return (char*)s;
104     if (*s == 0)
105       return 0;
106     s++;
107   }
108 }
109 
internal_strrchr(const char * s,int c)110 char *internal_strrchr(const char *s, int c) {
111   const char *res = 0;
112   for (uptr i = 0; s[i]; i++) {
113     if (s[i] == c) res = s + i;
114   }
115   return (char*)res;
116 }
117 
internal_strlen(const char * s)118 uptr internal_strlen(const char *s) {
119   uptr i = 0;
120   while (s[i]) i++;
121   return i;
122 }
123 
internal_strncat(char * dst,const char * src,uptr n)124 char *internal_strncat(char *dst, const char *src, uptr n) {
125   uptr len = internal_strlen(dst);
126   uptr i;
127   for (i = 0; i < n && src[i]; i++)
128     dst[len + i] = src[i];
129   dst[len + i] = 0;
130   return dst;
131 }
132 
internal_strncpy(char * dst,const char * src,uptr n)133 char *internal_strncpy(char *dst, const char *src, uptr n) {
134   uptr i;
135   for (i = 0; i < n && src[i]; i++)
136     dst[i] = src[i];
137   for (; i < n; i++)
138     dst[i] = '\0';
139   return dst;
140 }
141 
internal_strnlen(const char * s,uptr maxlen)142 uptr internal_strnlen(const char *s, uptr maxlen) {
143   uptr i = 0;
144   while (i < maxlen && s[i]) i++;
145   return i;
146 }
147 
internal_strstr(const char * haystack,const char * needle)148 char *internal_strstr(const char *haystack, const char *needle) {
149   // This is O(N^2), but we are not using it in hot places.
150   uptr len1 = internal_strlen(haystack);
151   uptr len2 = internal_strlen(needle);
152   if (len1 < len2) return 0;
153   for (uptr pos = 0; pos <= len1 - len2; pos++) {
154     if (internal_memcmp(haystack + pos, needle, len2) == 0)
155       return (char*)haystack + pos;
156   }
157   return 0;
158 }
159 
internal_simple_strtoll(const char * nptr,char ** endptr,int base)160 s64 internal_simple_strtoll(const char *nptr, char **endptr, int base) {
161   CHECK_EQ(base, 10);
162   while (IsSpace(*nptr)) nptr++;
163   int sgn = 1;
164   u64 res = 0;
165   bool have_digits = false;
166   char *old_nptr = (char*)nptr;
167   if (*nptr == '+') {
168     sgn = 1;
169     nptr++;
170   } else if (*nptr == '-') {
171     sgn = -1;
172     nptr++;
173   }
174   while (IsDigit(*nptr)) {
175     res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX;
176     int digit = ((*nptr) - '0');
177     res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX;
178     have_digits = true;
179     nptr++;
180   }
181   if (endptr != 0) {
182     *endptr = (have_digits) ? (char*)nptr : old_nptr;
183   }
184   if (sgn > 0) {
185     return (s64)(Min((u64)INT64_MAX, res));
186   } else {
187     return (res > INT64_MAX) ? INT64_MIN : ((s64)res * -1);
188   }
189 }
190 
191 }  // namespace __sanitizer
192