#include #include #include #include #include #include #define P6_NOP1 ".byte 0x90\n" #define P6_NOP2 ".byte 0x66,0x90\n" #define P6_NOP3 ".byte 0x0f,0x1f,0x00\n" #define P6_NOP4 ".byte 0x0f,0x1f,0x40,0\n" #define P6_NOP5 ".byte 0x0f,0x1f,0x44,0x00,0\n" #define P6_NOP6 ".byte 0x66,0x0f,0x1f,0x44,0x00,0\n" #define P6_NOP7 ".byte 0x0f,0x1f,0x80,0,0,0,0\n" #define P6_NOP8 ".byte 0x0f,0x1f,0x84,0x00,0,0,0,0\n" static sigjmp_buf bail_buf; static void sigill(int sig) { (void)sig; siglongjmp(bail_buf, 1); } static int do_test(int n) { struct sigaction old_sa, sa; int err; memset(&sa, 0, sizeof sa); sa.sa_handler = sigill; sigaction(SIGILL, &sa, &old_sa); err = sigsetjmp(bail_buf, 1); if (!err) { switch (n) { case -1: asm volatile("ud2a"); /* Test the mechanism */ break; case 0: asm volatile(""); break; case 1: asm volatile(P6_NOP1); break; case 2: asm volatile(P6_NOP2); break; case 3: asm volatile(P6_NOP3); break; case 4: asm volatile(P6_NOP4); break; case 5: asm volatile(P6_NOP5); break; case 6: asm volatile(P6_NOP6); break; case 7: asm volatile(P6_NOP7); break; case 8: asm volatile(P6_NOP8); break; default: abort(); } } sigaction(SIGILL, &old_sa, NULL); return err; } int main(void) { int i; int test, err = 0; if (!do_test(-1)) { printf("Trap mechanism broken!\n"); return 2; } for (i = 0; i <= 8; i++) { test = do_test(i); err |= test; printf("Test %d: %s\n", i, test ? "err" : "ok"); } return err; }