[sanitizer] Intercept setlocale.


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@185416 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/msan/lit_tests/setlocale.cc b/lib/msan/lit_tests/setlocale.cc
new file mode 100644
index 0000000..a22b744
--- /dev/null
+++ b/lib/msan/lit_tests/setlocale.cc
@@ -0,0 +1,13 @@
+// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t
+
+#include <assert.h>
+#include <locale.h>
+#include <stdlib.h>
+
+int main(void) {
+  char *locale = setlocale (LC_ALL, "");
+  assert(locale);
+  if (locale[0])
+    exit(0);
+  return 0;
+}
diff --git a/lib/sanitizer_common/sanitizer_common_interceptors.inc b/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 6f76a4c..7d39e29 100644
--- a/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -1399,6 +1399,24 @@
 #define INIT_PTRACE
 #endif
 
+#if SANITIZER_INTERCEPT_SETLOCALE
+INTERCEPTOR(char *, setlocale, int category, char *locale) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
+  if (locale)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
+  char * res = REAL(setlocale)(category, locale);
+  if (res)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
+  return res;
+}
+
+#define INIT_SETLOCALE           \
+  INTERCEPT_FUNCTION(setlocale);
+#else
+#define INIT_SETLOCALE
+#endif
+
 
 #define SANITIZER_COMMON_INTERCEPTORS_INIT \
   INIT_STRCASECMP;                         \
@@ -1445,4 +1463,5 @@
   INIT_SYSINFO;                            \
   INIT_READDIR;                            \
   INIT_READDIR64;                          \
-  INIT_PTRACE;
+  INIT_PTRACE;                             \
+  INIT_SETLOCALE;
diff --git a/lib/sanitizer_common/sanitizer_platform_interceptors.h b/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 7ddde75..b749a62 100644
--- a/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -96,5 +96,6 @@
 # define SANITIZER_INTERCEPT_READDIR SI_NOT_WINDOWS
 # define SANITIZER_INTERCEPT_READDIR64 SI_LINUX_NOT_ANDROID
 # define SANITIZER_INTERCEPT_PTRACE SI_LINUX_NOT_ANDROID
+# define SANITIZER_INTERCEPT_SETLOCALE SI_NOT_WINDOWS
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H
diff --git a/lib/tsan/rtl/tsan_stat.cc b/lib/tsan/rtl/tsan_stat.cc
index f2925cb..01ca2d3 100644
--- a/lib/tsan/rtl/tsan_stat.cc
+++ b/lib/tsan/rtl/tsan_stat.cc
@@ -337,6 +337,7 @@
   name[StatInt_readdir_r]                = "  readdir_r                       ";
   name[StatInt_readdir64_r]              = "  readdir64_r                     ";
   name[StatInt_ptrace]                   = "  ptrace                          ";
+  name[StatInt_setlocale]                = "  setlocale                       ";
 
   name[StatAnnotation]                   = "Dynamic annotations               ";
   name[StatAnnotateHappensBefore]        = "  HappensBefore                   ";
diff --git a/lib/tsan/rtl/tsan_stat.h b/lib/tsan/rtl/tsan_stat.h
index cd83216..a67e457 100644
--- a/lib/tsan/rtl/tsan_stat.h
+++ b/lib/tsan/rtl/tsan_stat.h
@@ -332,6 +332,7 @@
   StatInt_readdir_r,
   StatInt_readdir64_r,
   StatInt_ptrace,
+  StatInt_setlocale,
 
   // Dynamic annotations.
   StatAnnotation,