File: context_rflags.c

package info (click to toggle)
valgrind 1%3A3.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 170,080 kB
  • sloc: ansic: 782,981; exp: 26,134; xml: 22,780; asm: 14,072; cpp: 7,903; makefile: 6,768; perl: 6,097; sh: 5,669; javascript: 981; awk: 148
file content (68 lines) | stat: -rw-r--r-- 1,577 bytes parent folder | download | duplicates (4)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Test if rflags values are correctly propagated in and out of a signal
   handler.  Note that we actually test only the propagation of the overflow
   and sign flags. */

#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/regset.h>
#include <sys/syscall.h>
#include <sys/ucontext.h>

#define OBIT(rflags) (!!((rflags) & (1 << 11)))
#define SBIT(rflags) (!!((rflags) & (1 << 7)))

static siginfo_t si;
static ucontext_t uc;

static void sighandler(int sig, siginfo_t *sip, void *arg)
{
   si = *sip;
   uc = *((ucontext_t *) arg);
}

int main(void)
{
   struct sigaction sa;
   pid_t pid;
   long rflags;

   sa.sa_sigaction = sighandler;
   sa.sa_flags = SA_SIGINFO;
   if (sigfillset(&sa.sa_mask)) {
      perror("sigfillset");
      return 1;
   }
   if (sigaction(SIGUSR1, &sa, NULL)) {
      perror("sigaction");
      return 1;
   }

   pid = getpid();

   __asm__ __volatile__(
      /* Set overflow and sign flags. */
      "movl   $1, %%edx\n"
      "addl   $0x7fffffff, %%edx\n"

      /* Trigger the signal handler. */
      "syscall\n"
      "pushfq\n"
      "popq   %%rdx\n"
      : "=d" (rflags)
      : "a" (SYS_kill), "D" (pid), "S" (SIGUSR1)
      : "cc", "memory");

   printf("Values in the signal handler:\n");
   printf("  overflow=%d, sign=%d\n",
          OBIT(uc.uc_mcontext.gregs[REG_RFL]),
          SBIT(uc.uc_mcontext.gregs[REG_RFL]));

   printf("Values after the return from the signal handler:\n");
   printf("  overflow=%d, sign=%d\n", OBIT(rflags), SBIT(rflags));

   return 0;
}