[msan] Unpoison errno in common interceptors.


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@193343 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/msan/lit_tests/errno.cc b/lib/msan/lit_tests/errno.cc
new file mode 100644
index 0000000..af27ad0
--- /dev/null
+++ b/lib/msan/lit_tests/errno.cc
@@ -0,0 +1,17 @@
+// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int main()
+{
+  int x;
+  int *volatile p = &x;
+  errno = *p;
+  int res = read(-1, 0, 0);
+  assert(res == -1);
+  if (errno) printf("errno %d\n", errno);
+  return 0;
+}
diff --git a/lib/msan/msan_interceptors.cc b/lib/msan/msan_interceptors.cc
index 849949d..aea2ef6 100644
--- a/lib/msan/msan_interceptors.cc
+++ b/lib/msan/msan_interceptors.cc
@@ -1172,6 +1172,8 @@
 
 }  // namespace __msan
 
+extern "C" int *__errno_location(void);
+
 // A version of CHECK_UNPOISED using a saved scope value. Used in common
 // interceptors.
 #define CHECK_UNPOISONED_CTX(ctx, x, n)                         \
@@ -1188,12 +1190,13 @@
   CHECK_UNPOISONED_CTX(ctx, ptr, size)
 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, ptr, size) \
   __msan_unpoison(ptr, size)
-#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)              \
-  if (msan_init_is_running) return REAL(func)(__VA_ARGS__);   \
-  MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
-  ctx = (void *)&msan_ctx;                                    \
-  (void)ctx;                                                  \
-  InterceptorScope interceptor_scope;                         \
+#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)                  \
+  if (msan_init_is_running) return REAL(func)(__VA_ARGS__);       \
+  MSanInterceptorContext msan_ctx = {IsInInterceptorScope()};     \
+  ctx = (void *)&msan_ctx;                                        \
+  (void)ctx;                                                      \
+  InterceptorScope interceptor_scope;                             \
+  __msan_unpoison(__errno_location(), sizeof(int)); /* NOLINT */  \
   ENSURE_MSAN_INITED();
 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
   do {                                         \