1 // RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 
3 #include <pthread.h>
4 #include <sanitizer/msan_interface.h>
5 #include <signal.h>
6 #include <stdio.h>
7 #include <ucontext.h>
8 #include <unistd.h>
9 
10 void handler(int sig, siginfo_t *info, void *uctx) {
11   __msan_check_mem_is_initialized(uctx, sizeof(ucontext_t));
12 #if defined(__x86_64__)
13   auto *mctx = &static_cast<ucontext_t *>(uctx)->uc_mcontext;
14   if (auto *fpregs = mctx->fpregs) {
15     if (fpregs->__glibc_reserved1[12] == FP_XSTATE_MAGIC1) {
16       auto *xstate = reinterpret_cast<_xstate *>(mctx->fpregs);
17       __msan_check_mem_is_initialized(xstate, sizeof(*xstate));
18     }
19   }
20 #endif
21 }
22 
23 __attribute__((noinline)) void poison_stack() {
24   char buf[64 << 10];
25   printf("buf: %p-%p\n", buf, buf + sizeof(buf));
26 }
27 
28 int main(int argc, char **argv) {
29   struct sigaction act = {};
30   act.sa_sigaction = handler;
31   act.sa_flags = SA_SIGINFO;
32   sigaction(SIGPROF, &act, 0);
33   poison_stack();
34   pthread_kill(pthread_self(), SIGPROF);
35   return 0;
36 }
37 
38 // CHECK-NOT: WARNING: MemorySanitizer:
39