[Sanitizer] Use DEFINE_REAL macro in TSan runtime to call libc implementations of functions. Move strchr to sanitizer_libc.
llvm-svn: 158517
This commit is contained in:
parent
f11b42e396
commit
3efd6fc26c
@ -232,16 +232,6 @@ uptr internal_strnlen(const char *s, uptr maxlen) {
|
||||
return i;
|
||||
}
|
||||
|
||||
char* internal_strchr(const char *s, int c) {
|
||||
while (true) {
|
||||
if (*s == (char)c)
|
||||
return (char*)s;
|
||||
if (*s == 0)
|
||||
return 0;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
char *internal_strstr(const char *haystack, const char *needle) {
|
||||
// This is O(N^2), but we are not using it in hot places.
|
||||
uptr len1 = internal_strlen(haystack);
|
||||
|
||||
@ -33,7 +33,6 @@ namespace __asan {
|
||||
// __asan::internal_X() is the implementation of X() for use in RTL.
|
||||
s64 internal_atoll(const char *nptr);
|
||||
uptr internal_strnlen(const char *s, uptr maxlen);
|
||||
char* internal_strchr(const char *s, int c);
|
||||
char *internal_strstr(const char *haystack, const char *needle);
|
||||
char *internal_strncat(char *dst, const char *src, uptr n);
|
||||
// Works only for base=10 and doesn't set errno.
|
||||
|
||||
@ -75,6 +75,16 @@ int internal_strcmp(const char *s1, const char *s2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* internal_strchr(const char *s, int c) {
|
||||
while (true) {
|
||||
if (*s == (char)c)
|
||||
return (char*)s;
|
||||
if (*s == 0)
|
||||
return 0;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
char *internal_strrchr(const char *s, int c) {
|
||||
const char *res = 0;
|
||||
for (uptr i = 0; s[i]; i++) {
|
||||
|
||||
@ -29,6 +29,7 @@ int internal_memcmp(const void* s1, const void* s2, uptr n);
|
||||
void *internal_memcpy(void *dest, const void *src, uptr n);
|
||||
// Should not be used in performance-critical places.
|
||||
void *internal_memset(void *s, int c, uptr n);
|
||||
char* internal_strchr(const char *s, int c);
|
||||
int internal_strcmp(const char *s1, const char *s2);
|
||||
char *internal_strdup(const char *s);
|
||||
uptr internal_strlen(const char *s);
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#ifndef TSAN_DEFS_H
|
||||
#define TSAN_DEFS_H
|
||||
|
||||
#include "interception/interception.h"
|
||||
#include "sanitizer_common/sanitizer_internal_defs.h"
|
||||
#include "sanitizer_common/sanitizer_libc.h"
|
||||
#include "tsan_stat.h"
|
||||
@ -134,13 +135,6 @@ T RoundUp(T p, int align) {
|
||||
return (T)(((u64)p + align - 1) & ~(align - 1));
|
||||
}
|
||||
|
||||
void real_memset(void *ptr, int c, uptr size);
|
||||
void real_memcpy(void *dst, const void *src, uptr size);
|
||||
int internal_strncmp(const char *s1, const char *s2, uptr size);
|
||||
void internal_strcpy(char *s1, const char *s2);
|
||||
const char *internal_strstr(const char *where, const char *what);
|
||||
const char *internal_strchr(const char *where, char what);
|
||||
|
||||
struct MD5Hash {
|
||||
u64 hash[2];
|
||||
bool operator==(const MD5Hash &other) const {
|
||||
@ -160,4 +154,9 @@ class StackTrace;
|
||||
|
||||
} // namespace __tsan
|
||||
|
||||
DECLARE_REAL(void*, memset, void *ptr, int v, uptr size);
|
||||
DECLARE_REAL(void*, memcpy, void *dst, const void *src, uptr size);
|
||||
DECLARE_REAL(int, strncmp, const char *s1, const char *s2, uptr n);
|
||||
DECLARE_REAL(const char*, strstr, const char *s1, const char *s2);
|
||||
|
||||
#endif // TSAN_DEFS_H
|
||||
|
||||
@ -32,7 +32,7 @@ void WEAK OverrideFlags(Flags *f) {
|
||||
}
|
||||
|
||||
void InitializeFlags(Flags *f, const char *env) {
|
||||
real_memset(f, 0, sizeof(*f));
|
||||
REAL(memset)(f, 0, sizeof(*f));
|
||||
|
||||
// Default values.
|
||||
f->enable_annotations = true;
|
||||
@ -78,7 +78,7 @@ static const char *GetFlagValue(const char *env, const char *name,
|
||||
const char **end) {
|
||||
if (env == 0)
|
||||
return *end = 0;
|
||||
const char *pos = internal_strstr(env, name);
|
||||
const char *pos = REAL(strstr)(env, name);
|
||||
if (pos == 0)
|
||||
return *end = 0;
|
||||
pos += internal_strlen(name);
|
||||
@ -139,7 +139,7 @@ static void Flag(const char *env, const char **flag, const char *name) {
|
||||
return;
|
||||
int len = end - val;
|
||||
char *f = (char*)internal_alloc(MBlockFlag, len + 1);
|
||||
real_memcpy(f, val, len);
|
||||
REAL(memcpy)(f, val, len);
|
||||
f[len] = 0;
|
||||
*flag = f;
|
||||
}
|
||||
|
||||
@ -287,7 +287,7 @@ TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) {
|
||||
{
|
||||
SCOPED_INTERCEPTOR_RAW(calloc, size, n);
|
||||
p = user_alloc(thr, pc, n * size);
|
||||
real_memset(p, 0, n * size);
|
||||
REAL(memset)(p, 0, n * size);
|
||||
}
|
||||
invoke_malloc_hook(p, n * size);
|
||||
return p;
|
||||
@ -1283,7 +1283,7 @@ TSAN_INTERCEPTOR(int, sigaction, int sig, sigaction_t *act, sigaction_t *old) {
|
||||
TSAN_INTERCEPTOR(sighandler_t, signal, int sig, sighandler_t h) {
|
||||
sigaction_t act = {};
|
||||
act.sa_handler = h;
|
||||
real_memset(&act.sa_mask, -1, sizeof(act.sa_mask));
|
||||
REAL(memset)(&act.sa_mask, -1, sizeof(act.sa_mask));
|
||||
act.sa_flags = 0;
|
||||
sigaction_t old = {};
|
||||
int res = sigaction(sig, &act, &old);
|
||||
@ -1521,30 +1521,6 @@ void InitializeInterceptors() {
|
||||
}
|
||||
}
|
||||
|
||||
void real_memset(void *ptr, int c, uptr size) {
|
||||
REAL(memset)(ptr, c, size);
|
||||
}
|
||||
|
||||
void real_memcpy(void *dst, const void *src, uptr size) {
|
||||
REAL(memcpy)(dst, src, size);
|
||||
}
|
||||
|
||||
int internal_strncmp(const char *s1, const char *s2, uptr size) {
|
||||
return REAL(strncmp)(s1, s2, size);
|
||||
}
|
||||
|
||||
void internal_strcpy(char *s1, const char *s2) {
|
||||
REAL(strcpy)(s1, s2); // NOLINT
|
||||
}
|
||||
|
||||
const char *internal_strstr(const char *where, const char *what) {
|
||||
return REAL(strstr)(where, what);
|
||||
}
|
||||
|
||||
const char *internal_strchr(const char *where, char what) {
|
||||
return (const char*)REAL(strchr)((void*)where, what);
|
||||
}
|
||||
|
||||
void internal_start_thread(void(*func)(void *arg), void *arg) {
|
||||
void *th;
|
||||
REAL(pthread_create)(&th, 0, (void*(*)(void *arg))func, arg);
|
||||
|
||||
@ -166,11 +166,11 @@ void MD5_Update(MD5_CTX *ctx, void *data, ulong_t size) {
|
||||
free = 64 - used;
|
||||
|
||||
if (size < free) {
|
||||
real_memcpy(&ctx->buffer[used], data, size);
|
||||
REAL(memcpy)(&ctx->buffer[used], data, size);
|
||||
return;
|
||||
}
|
||||
|
||||
real_memcpy(&ctx->buffer[used], data, free);
|
||||
REAL(memcpy)(&ctx->buffer[used], data, free);
|
||||
data = (unsigned char *)data + free;
|
||||
size -= free;
|
||||
body(ctx, ctx->buffer, 64);
|
||||
@ -181,7 +181,7 @@ void MD5_Update(MD5_CTX *ctx, void *data, ulong_t size) {
|
||||
size &= 0x3f;
|
||||
}
|
||||
|
||||
real_memcpy(ctx->buffer, data, size);
|
||||
REAL(memcpy)(ctx->buffer, data, size);
|
||||
}
|
||||
|
||||
void MD5_Final(unsigned char *result, MD5_CTX *ctx) {
|
||||
@ -194,13 +194,13 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx) {
|
||||
free = 64 - used;
|
||||
|
||||
if (free < 8) {
|
||||
real_memset(&ctx->buffer[used], 0, free);
|
||||
REAL(memset)(&ctx->buffer[used], 0, free);
|
||||
body(ctx, ctx->buffer, 64);
|
||||
used = 0;
|
||||
free = 64;
|
||||
}
|
||||
|
||||
real_memset(&ctx->buffer[used], 0, free - 8);
|
||||
REAL(memset)(&ctx->buffer[used], 0, free - 8);
|
||||
|
||||
ctx->lo <<= 3;
|
||||
ctx->buffer[56] = ctx->lo;
|
||||
@ -231,7 +231,7 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx) {
|
||||
result[14] = ctx->d >> 16;
|
||||
result[15] = ctx->d >> 24;
|
||||
|
||||
real_memset(ctx, 0, sizeof(*ctx));
|
||||
REAL(memset)(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
MD5Hash md5_hash(const void *data, uptr size) {
|
||||
|
||||
@ -221,7 +221,7 @@ void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
|
||||
end += read;
|
||||
}
|
||||
end[0] = 0;
|
||||
end = (char*)internal_strstr(buf, "[stack]");
|
||||
end = (char*)REAL(strstr)(buf, "[stack]");
|
||||
if (end == 0) {
|
||||
TsanPrintf("Can't find [stack] in /proc/self/maps\n");
|
||||
Die();
|
||||
|
||||
@ -50,9 +50,9 @@ static void StackStripMain(ReportStack *stack) {
|
||||
const char *path_prefix = flags()->strip_path_prefix;
|
||||
uptr path_prefix_len = internal_strlen(path_prefix);
|
||||
for (ReportStack *ent = stack; ent; ent = ent->next) {
|
||||
if (ent->func && 0 == internal_strncmp(ent->func, prefix, prefix_len))
|
||||
if (ent->func && 0 == REAL(strncmp)(ent->func, prefix, prefix_len))
|
||||
ent->func += prefix_len;
|
||||
if (ent->file && 0 == internal_strncmp(ent->file, path_prefix,
|
||||
if (ent->file && 0 == REAL(strncmp)(ent->file, path_prefix,
|
||||
path_prefix_len))
|
||||
ent->file += path_prefix_len;
|
||||
if (ent->file && ent->file[0] == '.' && ent->file[1] == '/')
|
||||
|
||||
@ -212,7 +212,7 @@ void ThreadFinish(ThreadState *thr) {
|
||||
// Save from info about the thread.
|
||||
tctx->dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
|
||||
ThreadDeadInfo();
|
||||
real_memcpy(&tctx->dead_info->trace.events[0],
|
||||
REAL(memcpy)(&tctx->dead_info->trace.events[0],
|
||||
&thr->trace.events[0], sizeof(thr->trace.events));
|
||||
for (int i = 0; i < kTraceParts; i++) {
|
||||
tctx->dead_info->trace.headers[i].stack0.CopyFrom(
|
||||
|
||||
@ -68,7 +68,7 @@ bool SuppressionMatch(char *templ, const char *str) {
|
||||
tpos = (char*)internal_strchr(templ, '*');
|
||||
if (tpos != 0)
|
||||
tpos[0] = 0;
|
||||
spos = internal_strstr(str, templ);
|
||||
spos = REAL(strstr)(str, templ);
|
||||
str = spos + internal_strlen(templ);
|
||||
templ = tpos;
|
||||
if (tpos)
|
||||
@ -93,18 +93,18 @@ Suppression *SuppressionParse(const char* supp) {
|
||||
while (line != end2 && (end2[-1] == ' ' || end2[-1] == '\t'))
|
||||
end2--;
|
||||
SuppressionType stype;
|
||||
if (0 == internal_strncmp(line, "race:", sizeof("race:") - 1)) {
|
||||
if (0 == REAL(strncmp)(line, "race:", sizeof("race:") - 1)) {
|
||||
stype = SuppressionRace;
|
||||
line += sizeof("race:") - 1;
|
||||
} else if (0 == internal_strncmp(line, "thread:",
|
||||
} else if (0 == REAL(strncmp)(line, "thread:",
|
||||
sizeof("thread:") - 1)) {
|
||||
stype = SuppressionThread;
|
||||
line += sizeof("thread:") - 1;
|
||||
} else if (0 == internal_strncmp(line, "mutex:",
|
||||
} else if (0 == REAL(strncmp)(line, "mutex:",
|
||||
sizeof("mutex:") - 1)) {
|
||||
stype = SuppressionMutex;
|
||||
line += sizeof("mutex:") - 1;
|
||||
} else if (0 == internal_strncmp(line, "signal:",
|
||||
} else if (0 == REAL(strncmp)(line, "signal:",
|
||||
sizeof("signal:") - 1)) {
|
||||
stype = SuppressionSignal;
|
||||
line += sizeof("signal:") - 1;
|
||||
@ -118,7 +118,7 @@ Suppression *SuppressionParse(const char* supp) {
|
||||
head = s;
|
||||
s->type = stype;
|
||||
s->templ = (char*)internal_alloc(MBlockSuppression, end2 - line + 1);
|
||||
real_memcpy(s->templ, line, end2 - line);
|
||||
REAL(memcpy)(s->templ, line, end2 - line);
|
||||
s->templ[end2 - line] = 0;
|
||||
}
|
||||
if (end[0] == 0)
|
||||
|
||||
@ -147,7 +147,7 @@ static SectionDesc *GetSectionDesc(uptr addr) {
|
||||
static ReportStack *NewFrame(uptr addr) {
|
||||
ReportStack *ent = (ReportStack*)internal_alloc(MBlockReportStack,
|
||||
sizeof(ReportStack));
|
||||
real_memset(ent, 0, sizeof(*ent));
|
||||
REAL(memset)(ent, 0, sizeof(*ent));
|
||||
ent->pc = addr;
|
||||
return ent;
|
||||
}
|
||||
@ -179,12 +179,12 @@ ReportStack *SymbolizeCode(uptr addr) {
|
||||
char *pos = (char*)internal_strchr(func, '\n');
|
||||
if (pos && func[0] != '?') {
|
||||
res->func = (char*)internal_alloc(MBlockReportStack, pos - func + 1);
|
||||
real_memcpy(res->func, func, pos - func);
|
||||
REAL(memcpy)(res->func, func, pos - func);
|
||||
res->func[pos - func] = 0;
|
||||
char *pos2 = (char*)internal_strchr(pos, ':');
|
||||
if (pos2) {
|
||||
res->file = (char*)internal_alloc(MBlockReportStack, pos2 - pos - 1 + 1);
|
||||
real_memcpy(res->file, pos + 1, pos2 - pos - 1);
|
||||
REAL(memcpy)(res->file, pos + 1, pos2 - pos - 1);
|
||||
res->file[pos2 - pos - 1] = 0;
|
||||
res->line = atoi(pos2 + 1);
|
||||
}
|
||||
@ -215,14 +215,14 @@ ReportStack *SymbolizeData(uptr addr) {
|
||||
symb[0].module = 0;
|
||||
symb[0].offset = addr;
|
||||
symb[0].name = alloc->Alloc<char>(pos - tmp + 1);
|
||||
real_memcpy(symb[0].name, tmp, pos - tmp);
|
||||
REAL(memcpy)(symb[0].name, tmp, pos - tmp);
|
||||
symb[0].name[pos - tmp] = 0;
|
||||
symb[0].file = 0;
|
||||
symb[0].line = 0;
|
||||
char *pos2 = strchr(pos, ':');
|
||||
if (pos2) {
|
||||
symb[0].file = alloc->Alloc<char>(pos2 - pos - 1 + 1);
|
||||
real_memcpy(symb[0].file, pos + 1, pos2 - pos - 1);
|
||||
REAL(memcpy)(symb[0].file, pos + 1, pos2 - pos - 1);
|
||||
symb[0].file[pos2 - pos - 1] = 0;
|
||||
symb[0].line = atoi(pos2 + 1);
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ void StackTrace::Init(const uptr *pcs, uptr cnt) {
|
||||
return;
|
||||
n_ = cnt;
|
||||
s_ = (uptr*)internal_alloc(MBlockStackTrace, cnt * sizeof(s_[0]));
|
||||
real_memcpy(s_, pcs, cnt * sizeof(s_[0]));
|
||||
REAL(memcpy)(s_, pcs, cnt * sizeof(s_[0]));
|
||||
}
|
||||
|
||||
void StackTrace::ObtainCurrent(ThreadState *thr, uptr toppc) {
|
||||
|
||||
@ -94,7 +94,7 @@ class Vector {
|
||||
cap = size;
|
||||
T *p = (T*)internal_alloc(typ_, cap * sizeof(T));
|
||||
if (cap0) {
|
||||
real_memcpy(p, begin_, cap0 * sizeof(T));
|
||||
REAL(memcpy)(p, begin_, cap0 * sizeof(T));
|
||||
internal_free(begin_);
|
||||
}
|
||||
begin_ = p;
|
||||
|
||||
@ -79,7 +79,7 @@ TEST(Platform, FileOps) {
|
||||
EXPECT_EQ(len1, internal_read(fd, buf, len1));
|
||||
EXPECT_EQ(0, internal_memcmp(buf, str1, len1));
|
||||
EXPECT_EQ((char)0, buf[len1 + 1]);
|
||||
real_memset(buf, 0, len1);
|
||||
REAL(memset)(buf, 0, len1);
|
||||
EXPECT_EQ(len2, internal_read(fd, buf, len2));
|
||||
EXPECT_EQ(0, internal_memcmp(buf, str2, len2));
|
||||
internal_close(fd);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user