aboutsummaryrefslogtreecommitdiff
path: root/lib/asan/lit_tests/TestCases/Linux/ptrace.cc
blob: 8831b81efa96d94699332456ab49ae80627778e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// RUN: %clangxx_asan -O0 %s -o %t && %t
// RUN: %clangxx_asan -DPOSITIVE -O0 %s -o %t && not %t 2>&1 | FileCheck %s

#include <assert.h>
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void) {
  pid_t pid;
  pid = fork();
  if (pid == 0) { // child
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);
    execl("/bin/true", "true", NULL);
  } else {
    wait(NULL);
    user_regs_struct regs;
    int res;
    user_regs_struct * volatile pregs = &regs;
#ifdef POSITIVE
    ++pregs;
#endif
    res = ptrace(PTRACE_GETREGS, pid, NULL, pregs);
    // CHECK: AddressSanitizer: stack-buffer-overflow
    // CHECK: {{.*ptrace.cc:}}[[@LINE-2]]
    assert(!res);
#if __WORDSIZE == 64
    printf("%zx\n", regs.rip);
#else
    printf("%lx\n", regs.eip);
#endif

    user_fpregs_struct fpregs;
    res = ptrace(PTRACE_GETFPREGS, pid, NULL, &fpregs);
    assert(!res);
    printf("%lx\n", (unsigned long)fpregs.cwd);

#if __WORDSIZE == 32
    user_fpxregs_struct fpxregs;
    res = ptrace(PTRACE_GETFPXREGS, pid, NULL, &fpxregs);
    assert(!res);
    printf("%lx\n", (unsigned long)fpxregs.mxcsr);
#endif

    ptrace(PTRACE_CONT, pid, NULL, NULL);
    wait(NULL);
  }
  return 0;
}