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
11void handler(int sig, siginfo_t *info, void *uctx) {
12 __msan_check_mem_is_initialized(x: uctx, size: 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(x: xstate, size: sizeof(*xstate));
23 }
24 }
25#endif
26}
27
28__attribute__((noinline)) void poison_stack() {
29 char buf[64 << 10];
30 printf(format: "buf: %p-%p\n", buf, buf + sizeof(buf));
31}
32
33int main(int argc, char **argv) {
34 struct sigaction act = {};
35 act.sa_sigaction = handler;
36 act.sa_flags = SA_SIGINFO;
37 sigaction(SIGPROF, act: &act, oact: 0);
38 poison_stack();
39 pthread_kill(threadid: pthread_self(), SIGPROF);
40 return 0;
41}
42
43// CHECK-NOT: WARNING: MemorySanitizer:
44

source code of compiler-rt/test/msan/Linux/signal_mcontext.cpp