xref: /llvm-project/compiler-rt/test/msan/Linux/signal_mcontext.cpp (revision 9d1857f69f4ef00d9fd1b21660c20e00b993d06f)
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 <stdint.h>
7 #include <stdio.h>
8 #include <ucontext.h>
9 #include <unistd.h>
10 
handler(int sig,siginfo_t * info,void * uctx)11 void handler(int sig, siginfo_t *info, void *uctx) {
12   __msan_check_mem_is_initialized(uctx, sizeof(ucontext_t));
13 #if defined(__GLIBC__) && defined(__x86_64__)
14   auto *mctx = &static_cast<ucontext_t *>(uctx)->uc_mcontext;
15   if (auto *fpregs = mctx->fpregs) {
16     // The member names differ across header versions, but the actual layout
17     // is always the same.  So avoid using members, just use arithmetic.
18     const uint32_t *after_xmm =
19         reinterpret_cast<const uint32_t *>(fpregs + 1) - 24;
20     if (after_xmm[12] == FP_XSTATE_MAGIC1) {
21       auto *xstate = reinterpret_cast<_xstate *>(mctx->fpregs);
22       __msan_check_mem_is_initialized(xstate, sizeof(*xstate));
23     }
24   }
25 #endif
26 }
27 
poison_stack()28 __attribute__((noinline)) void poison_stack() {
29   char buf[64 << 10];
30   printf("buf: %p-%p\n", buf, buf + sizeof(buf));
31 }
32 
main(int argc,char ** argv)33 int main(int argc, char **argv) {
34   struct sigaction act = {};
35   act.sa_sigaction = handler;
36   act.sa_flags = SA_SIGINFO;
37   sigaction(SIGPROF, &act, 0);
38   poison_stack();
39   pthread_kill(pthread_self(), SIGPROF);
40   return 0;
41 }
42 
43 // CHECK-NOT: WARNING: MemorySanitizer:
44