44 lines · cpp
1// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -O1 %s -o %t && %run %t 2>&1 | FileCheck %s2 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(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 layout17 // 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#endif26}27 28__attribute__((noinline)) void poison_stack() {29 char buf[64 << 10];30 printf("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, 0);38 poison_stack();39 pthread_kill(pthread_self(), SIGPROF);40 return 0;41}42 43// CHECK-NOT: WARNING: MemorySanitizer:44