sanitizer_libc.cc revision 327c1c17d9557ed8b197f732c8a070fd6ba821b5
1603c4be006d8c53905d736bf1f19a49f5ce98276Alexey Samsonov//===-- sanitizer_libc.cc -------------------------------------------------===//
2b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//
3b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//                     The LLVM Compiler Infrastructure
4b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//
5b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// This file is distributed under the University of Illinois Open Source
6b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// License. See LICENSE.TXT for details.
7b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//
8b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//===----------------------------------------------------------------------===//
9b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//
10b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// This file is shared between AddressSanitizer and ThreadSanitizer
1116e0075746b21ed866ec3be21ef0d1e46f0efed5Kostya Serebryany// run-time libraries. See sanitizer_libc.h for details.
12b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//===----------------------------------------------------------------------===//
13f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov#include "sanitizer_common.h"
1416e0075746b21ed866ec3be21ef0d1e46f0efed5Kostya Serebryany#include "sanitizer_libc.h"
15b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany
16b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryanynamespace __sanitizer {
17b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany
18b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryanyvoid MiniLibcStub() {
19b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany}
20b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany
211f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonovvoid *internal_memchr(const void *s, int c, uptr n) {
221f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  const char* t = (char*)s;
231f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  for (uptr i = 0; i < n; ++i, ++t)
241f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov    if (*t == c)
251f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov      return (void*)t;
261f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  return 0;
271f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov}
281f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov
29327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonovint internal_memcmp(const void* s1, const void* s2, uptr n) {
30327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov  const char* t1 = (char*)s1;
31327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov  const char* t2 = (char*)s2;
32327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov  for (uptr i = 0; i < n; ++i, ++t1, ++t2)
33327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov    if (*t1 != *t2)
34327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov      return *t1 < *t2 ? -1 : 1;
35327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov  return 0;
36327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov}
37327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonov
38f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonovvoid *internal_memcpy(void *dest, const void *src, uptr n) {
39f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  char *d = (char*)dest;
40f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  char *s = (char*)src;
41f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  for (uptr i = 0; i < n; ++i)
42f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov    d[i] = s[i];
43f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  return dest;
44f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov}
45f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov
464fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonovvoid *internal_memset(void* s, int c, uptr n) {
474fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  // The next line prevents Clang from making a call to memset() instead of the
484fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  // loop below.
494fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  // FIXME: building the runtime with -ffreestanding is a better idea. However
504fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  // there currently are linktime problems due to PR12396.
514fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  char volatile *t = (char*)s;
524fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  for (uptr i = 0; i < n; ++i, ++t) {
534fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov    *t = c;
544fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  }
554fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  return s;
564fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov}
574fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov
58f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonovchar* internal_strdup(const char *s) {
59f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  uptr len = internal_strlen(s);
60f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  char *s2 = (char*)InternalAlloc(len + 1);
61f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  internal_memcpy(s2, s, len);
62f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  s2[len] = 0;
63f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov  return s2;
64f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov}
65f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov
66c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonovint internal_strcmp(const char *s1, const char *s2) {
67c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov  while (true) {
68c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov    unsigned c1 = *s1;
69c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov    unsigned c2 = *s2;
70c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov    if (c1 != c2) return (c1 < c2) ? -1 : 1;
71c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov    if (c1 == 0) break;
72c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov    s1++;
73c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov    s2++;
74c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov  }
75c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov  return 0;
76c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov}
77c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov
784fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonovchar *internal_strrchr(const char *s, int c) {
794fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  const char *res = 0;
804fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  for (uptr i = 0; s[i]; i++) {
814fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov    if (s[i] == c) res = s + i;
824fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  }
834fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov  return (char*)res;
844fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov}
854fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov
86230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonovuptr internal_strlen(const char *s) {
87230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  uptr i = 0;
88230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  while (s[i]) i++;
89230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  return i;
90230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov}
91230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov
923836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonovchar *internal_strncpy(char *dst, const char *src, uptr n) {
933836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov  uptr i;
943836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov  for (i = 0; i < n && src[i]; i++)
953836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov    dst[i] = src[i];
963836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov  for (; i < n; i++)
973836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov    dst[i] = '\0';
983836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov  return dst;
993836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov}
1003836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov
101b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany}  // namespace __sanitizer
102