[ASan] Add interceptor for swapcontext to fight with false positives in some of its use cases.

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@168508 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_posix.cc b/lib/asan/asan_posix.cc
index ceaf120..699af68 100644
--- a/lib/asan/asan_posix.cc
+++ b/lib/asan/asan_posix.cc
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <sys/time.h>
 #include <sys/resource.h>
+#include <ucontext.h>
 #include <unistd.h>
 
 static const uptr kAltStackSize = SIGSTKSZ * 4;  // SIGSTKSZ is not enough.
@@ -95,6 +96,17 @@
   MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
 }
 
+void ClearShadowMemoryForContext(void *context) {
+  ucontext_t *ucp = (ucontext_t*)context;
+  uptr sp = (uptr)ucp->uc_stack.ss_sp;
+  uptr size = ucp->uc_stack.ss_size;
+  // Align to page size.
+  uptr bottom = sp & ~(kPageSize - 1);
+  size += sp - bottom;
+  size = RoundUpTo(size, kPageSize);
+  PoisonShadow(bottom, size, 0);
+}
+
 // ---------------------- TSD ---------------- {{{1
 
 static pthread_key_t tsd_key;