[Sanitizer] Use DEFINE_REAL macro in TSan runtime to call libc implementations of functions. Move strchr to sanitizer_libc.

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@158517 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/tsan/rtl/tsan_defs.h b/lib/tsan/rtl/tsan_defs.h
index 117b245..5127d5d 100644
--- a/lib/tsan/rtl/tsan_defs.h
+++ b/lib/tsan/rtl/tsan_defs.h
@@ -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 @@
   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 @@
 
 }  // 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
diff --git a/lib/tsan/rtl/tsan_flags.cc b/lib/tsan/rtl/tsan_flags.cc
index 2268d6a..1722ad1 100644
--- a/lib/tsan/rtl/tsan_flags.cc
+++ b/lib/tsan/rtl/tsan_flags.cc
@@ -32,7 +32,7 @@
 }
 
 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 @@
                                 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 @@
     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;
 }
diff --git a/lib/tsan/rtl/tsan_interceptors.cc b/lib/tsan/rtl/tsan_interceptors.cc
index e4d183a..41a1e80 100644
--- a/lib/tsan/rtl/tsan_interceptors.cc
+++ b/lib/tsan/rtl/tsan_interceptors.cc
@@ -287,7 +287,7 @@
   {
     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(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 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);
diff --git a/lib/tsan/rtl/tsan_md5.cc b/lib/tsan/rtl/tsan_md5.cc
index c2074c0..00e7b83 100644
--- a/lib/tsan/rtl/tsan_md5.cc
+++ b/lib/tsan/rtl/tsan_md5.cc
@@ -166,11 +166,11 @@
     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 @@
     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 @@
   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 @@
   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) {
diff --git a/lib/tsan/rtl/tsan_platform_linux.cc b/lib/tsan/rtl/tsan_platform_linux.cc
index 91d31e6..86ebbbd 100644
--- a/lib/tsan/rtl/tsan_platform_linux.cc
+++ b/lib/tsan/rtl/tsan_platform_linux.cc
@@ -221,7 +221,7 @@
       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();
diff --git a/lib/tsan/rtl/tsan_rtl_report.cc b/lib/tsan/rtl/tsan_rtl_report.cc
index c6832da..b6065d3 100644
--- a/lib/tsan/rtl/tsan_rtl_report.cc
+++ b/lib/tsan/rtl/tsan_rtl_report.cc
@@ -50,9 +50,9 @@
   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] == '/')
diff --git a/lib/tsan/rtl/tsan_rtl_thread.cc b/lib/tsan/rtl/tsan_rtl_thread.cc
index 7f6d69b..2e32252 100644
--- a/lib/tsan/rtl/tsan_rtl_thread.cc
+++ b/lib/tsan/rtl/tsan_rtl_thread.cc
@@ -212,7 +212,7 @@
   // 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(
diff --git a/lib/tsan/rtl/tsan_suppressions.cc b/lib/tsan/rtl/tsan_suppressions.cc
index ced6591..613ff03 100644
--- a/lib/tsan/rtl/tsan_suppressions.cc
+++ b/lib/tsan/rtl/tsan_suppressions.cc
@@ -68,7 +68,7 @@
     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 @@
       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 @@
       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)
diff --git a/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc b/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc
index 6a445c2..1cd7d28 100644
--- a/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc
+++ b/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc
@@ -147,7 +147,7 @@
 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 @@
   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 @@
       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);
       }
diff --git a/lib/tsan/rtl/tsan_sync.cc b/lib/tsan/rtl/tsan_sync.cc
index 36b316a..b8ee589 100644
--- a/lib/tsan/rtl/tsan_sync.cc
+++ b/lib/tsan/rtl/tsan_sync.cc
@@ -155,7 +155,7 @@
     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) {
diff --git a/lib/tsan/rtl/tsan_vector.h b/lib/tsan/rtl/tsan_vector.h
index 5b6998f..9852d77 100644
--- a/lib/tsan/rtl/tsan_vector.h
+++ b/lib/tsan/rtl/tsan_vector.h
@@ -94,7 +94,7 @@
       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;
diff --git a/lib/tsan/unit_tests/tsan_platform_test.cc b/lib/tsan/unit_tests/tsan_platform_test.cc
index 0d815f6..b9ff0d1 100644
--- a/lib/tsan/unit_tests/tsan_platform_test.cc
+++ b/lib/tsan/unit_tests/tsan_platform_test.cc
@@ -79,7 +79,7 @@
   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);