Home | History | Annotate | Download | only in Linux
      1 // RUN: %clangxx_asan -O0 %s -o %t && %t
      2 // RUN: %clangxx_asan -DPOSITIVE -O0 %s -o %t && not %t 2>&1 | FileCheck %s
      3 
      4 #include <assert.h>
      5 #include <stdio.h>
      6 #include <sys/ptrace.h>
      7 #include <sys/types.h>
      8 #include <sys/user.h>
      9 #include <sys/wait.h>
     10 #include <unistd.h>
     11 
     12 int main(void) {
     13   pid_t pid;
     14   pid = fork();
     15   if (pid == 0) { // child
     16     ptrace(PTRACE_TRACEME, 0, NULL, NULL);
     17     execl("/bin/true", "true", NULL);
     18   } else {
     19     wait(NULL);
     20     user_regs_struct regs;
     21     int res;
     22     user_regs_struct * volatile pregs = &regs;
     23 #ifdef POSITIVE
     24     ++pregs;
     25 #endif
     26     res = ptrace(PTRACE_GETREGS, pid, NULL, pregs);
     27     // CHECK: AddressSanitizer: stack-buffer-overflow
     28     // CHECK: {{.*ptrace.cc:}}[[@LINE-2]]
     29     assert(!res);
     30 #if __WORDSIZE == 64
     31     printf("%zx\n", regs.rip);
     32 #else
     33     printf("%lx\n", regs.eip);
     34 #endif
     35 
     36     user_fpregs_struct fpregs;
     37     res = ptrace(PTRACE_GETFPREGS, pid, NULL, &fpregs);
     38     assert(!res);
     39     printf("%lx\n", (unsigned long)fpregs.cwd);
     40 
     41 #if __WORDSIZE == 32
     42     user_fpxregs_struct fpxregs;
     43     res = ptrace(PTRACE_GETFPXREGS, pid, NULL, &fpxregs);
     44     assert(!res);
     45     printf("%lx\n", (unsigned long)fpxregs.mxcsr);
     46 #endif
     47 
     48     ptrace(PTRACE_CONT, pid, NULL, NULL);
     49     wait(NULL);
     50   }
     51   return 0;
     52 }
     53