1 /* Test if x87 FP valus are correctly propagated in and out of a signal 2 handler and also check that the same applies for uninitialised values and 3 their origins. */ 4 5 #include <assert.h> 6 #include <signal.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 #include <sys/syscall.h> 11 #include <sys/ucontext.h> 12 13 static siginfo_t si; 14 static ucontext_t uc; 15 static float inhandler[8]; 16 17 static void sighandler(int sig, siginfo_t *sip, ucontext_t *ucp) 18 { 19 int i; 20 21 si = *sip; 22 uc = *ucp; 23 24 /* Reset the FP stack so it's possible to push other values onto it. (It 25 is fully filled in main() before triggering the signal handler). Note 26 that VEX also clears all FP values when the finit instruction is 27 executed. This provides another level of validation that the restore 28 code is correct. */ 29 __asm__ __volatile__( 30 "finit\n"); 31 32 /* Convert 80b values in mcontext to 32b values in the inhandler array. */ 33 for (i = 0; i < 8; i++) { 34 __asm__ __volatile__( 35 "fldt %[in]\n" 36 "fstps %[out]\n" 37 : [out] "=m" (inhandler[i]) 38 : [in] "m" (*(char*)&ucp->uc_mcontext.fpregs.fp_reg_set.fpchip_state.st[i])); 39 } 40 } 41 42 int main(void) 43 { 44 struct sigaction sa; 45 pid_t pid; 46 float out[8]; 47 float x0; 48 49 /* Uninitialised, but we know px[0] is 0x0. */ 50 float *px = malloc(sizeof(*px)); 51 x0 = px[0]; 52 53 sa.sa_handler = sighandler; 54 sa.sa_flags = SA_SIGINFO; 55 if (sigfillset(&sa.sa_mask)) { 56 perror("sigfillset"); 57 return 1; 58 } 59 if (sigaction(SIGUSR1, &sa, NULL)) { 60 perror("sigaction"); 61 return 1; 62 } 63 64 pid = getpid(); 65 66 __asm__ __volatile__( 67 /* Set values in the FP stack. */ 68 "flds %[x0]\n" 69 "fld1\n" 70 "flds %[x0]\n" 71 "fld1\n" 72 "flds %[x0]\n" 73 "fld1\n" 74 "flds %[x0]\n" 75 "fld1\n" 76 77 /* Trigger the signal handler. */ 78 "syscall\n" 79 "fstps 0x00 + %[out]\n" 80 "fstps 0x04 + %[out]\n" 81 "fstps 0x08 + %[out]\n" 82 "fstps 0x0c + %[out]\n" 83 "fstps 0x10 + %[out]\n" 84 "fstps 0x14 + %[out]\n" 85 "fstps 0x18 + %[out]\n" 86 "fstps 0x1c + %[out]\n" 87 : [out] "=m" (out[0]) 88 : "a" (SYS_kill), "D" (pid), "S" (SIGUSR1), [x0] "m" (x0) 89 : "rdx", "cc", "memory"); 90 91 printf("Values in the signal handler:\n"); 92 printf(" fp[0]=%f, fp[2]=%f, fp[4]=%f, fp[6]=%f\n", 93 inhandler[0], inhandler[2], inhandler[4], inhandler[6]); 94 /* Check that inhandler[1], inhandler[3], inhandler[5] and inhandler[7] 95 contain uninitialised values (origin is px[0]). */ 96 if (inhandler[1] || inhandler[3] || inhandler[5] || inhandler[7]) 97 assert(0); 98 99 printf("Values after the return from the signal handler:\n"); 100 printf(" fp[0]=%f, fp[2]=%f, fp[4]=%f, fp[6]=%f\n", 101 out[0], out[2], out[4], out[6]); 102 /* Check that out[1], out[3], out[5] and out[7] contain uninitialised 103 values (origin is px[0]). */ 104 if (out[1] || out[3] || out[5] || out[7]) 105 assert(0); 106 107 return 0; 108 } 109 110