• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* x86 variant of the amd64-solaris/context_gpr.c test. */
2 
3 #include <assert.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/regset.h>
9 #include <sys/syscall.h>
10 #include <sys/ucontext.h>
11 
12 static siginfo_t si;
13 static ucontext_t uc;
14 /* x0 is always zero, but is visible to Valgrind as uninitialised. */
15 static int x0;
16 
sighandler(int sig,siginfo_t * sip,void * arg)17 static void sighandler(int sig, siginfo_t *sip, void *arg)
18 {
19    ucontext_t *ucp = (ucontext_t *) arg;
20 
21    si = *sip;
22    uc = *ucp;
23 
24    ucp->uc_mcontext.gregs[ECX] = x0;
25 }
26 
main(void)27 int main(void)
28 {
29    struct sigaction sa;
30    pid_t pid;
31    int eax, ebx, ecx, edx, esi, edi;
32    int y0;
33 
34    /* Uninitialised, but we know px[0] is 0x0. */
35    int *px = malloc(sizeof(*px));
36    x0 = px[0];
37 
38    /* Uninitialised, but we know py[0] is 0x0. */
39    int *py = malloc(sizeof(*py));
40    y0 = py[0];
41 
42    sa.sa_sigaction = sighandler;
43    sa.sa_flags = SA_SIGINFO;
44    if (sigfillset(&sa.sa_mask)) {
45       perror("sigfillset");
46       return 1;
47    }
48    if (sigaction(SIGUSR1, &sa, NULL)) {
49       perror("sigaction");
50       return 1;
51    }
52 
53    pid = getpid();
54 
55    __asm__ __volatile__(
56       /* Set values in general purpose registers. */
57       "movl   %[y0], %%ebx\n"
58       "movl   $0xf1, %%ecx\n"
59       "movl   $0xf2, %%edx\n"
60       "movl   $0xf3, %%esi\n"
61       "movl   $0xf4, %%edi\n"
62 
63       /* Prepare syscall parameters. */
64       "pushl  %[sig]\n"
65       "pushl  %[pid]\n"
66       "pushl  $0xdeadbeef\n"
67       "movl   %[scall], %%eax\n"
68 
69       /* Trigger the signal handler. */
70       "int    $0x91\n"
71       "addl   $12, %%esp\n"
72       : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx), "=S" (esi),
73         "=D" (edi)
74       : [scall] "i" (SYS_kill), [pid] "a" (pid), [sig] "i" (SIGUSR1),
75         [y0] "m" (y0)
76       : "cc", "memory");
77 
78    printf("Values in the signal handler:\n");
79    printf("  eax=%#x, edx=%#x, esi=%#x, edi=%#x\n",
80           uc.uc_mcontext.gregs[EAX], uc.uc_mcontext.gregs[EDX],
81           uc.uc_mcontext.gregs[ESI], uc.uc_mcontext.gregs[EDI]);
82    /* Check that ebx contains an uninitialised value (origin is py[0]). */
83    if (uc.uc_mcontext.gregs[EBX])
84       assert(0);
85 
86    printf("Values after the return from the signal handler:\n");
87    printf("  eax=%#x, edx=%#x, esi=%#x, edi=%#x\n", eax, edx, esi, edi);
88    /* Check that ebx and ecx contain uninitialised values (origin is py[0]
89       and px[0], respectively). */
90    if (ebx || ecx)
91       assert(0);
92 
93    return 0;
94 }
95 
96