llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_libc.cc

93 lines
2.2 KiB
C++

//===-- sanitizer_libc.cc -------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is shared between AddressSanitizer and ThreadSanitizer
// run-time libraries. See sanitizer_libc.h for details.
//===----------------------------------------------------------------------===//
#include "sanitizer_common.h"
#include "sanitizer_libc.h"
namespace __sanitizer {
void MiniLibcStub() {
}
void *internal_memchr(const void *s, int c, uptr n) {
const char* t = (char*)s;
for (uptr i = 0; i < n; ++i, ++t)
if (*t == c)
return (void*)t;
return 0;
}
void *internal_memcpy(void *dest, const void *src, uptr n) {
char *d = (char*)dest;
char *s = (char*)src;
for (uptr i = 0; i < n; ++i)
d[i] = s[i];
return dest;
}
void *internal_memset(void* s, int c, uptr n) {
// The next line prevents Clang from making a call to memset() instead of the
// loop below.
// FIXME: building the runtime with -ffreestanding is a better idea. However
// there currently are linktime problems due to PR12396.
char volatile *t = (char*)s;
for (uptr i = 0; i < n; ++i, ++t) {
*t = c;
}
return s;
}
char* internal_strdup(const char *s) {
uptr len = internal_strlen(s);
char *s2 = (char*)InternalAlloc(len + 1);
internal_memcpy(s2, s, len);
s2[len] = 0;
return s2;
}
int internal_strcmp(const char *s1, const char *s2) {
while (true) {
unsigned c1 = *s1;
unsigned c2 = *s2;
if (c1 != c2) return (c1 < c2) ? -1 : 1;
if (c1 == 0) break;
s1++;
s2++;
}
return 0;
}
char *internal_strrchr(const char *s, int c) {
const char *res = 0;
for (uptr i = 0; s[i]; i++) {
if (s[i] == c) res = s + i;
}
return (char*)res;
}
uptr internal_strlen(const char *s) {
uptr i = 0;
while (s[i]) i++;
return i;
}
char *internal_strncpy(char *dst, const char *src, uptr n) {
uptr i;
for (i = 0; i < n && src[i]; i++)
dst[i] = src[i];
for (; i < n; i++)
dst[i] = '\0';
return dst;
}
} // namespace __sanitizer