exc_validate.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <sys/mman.h>
  7. #include "utils.h"
  8. extern char __start___ex_table[];
  9. extern char __stop___ex_table[];
  10. #if defined(__powerpc64__)
  11. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
  12. #elif defined(__powerpc__)
  13. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
  14. #else
  15. #error implement UCONTEXT_NIA
  16. #endif
  17. static void segv_handler(int signr, siginfo_t *info, void *ptr)
  18. {
  19. ucontext_t *uc = (ucontext_t *)ptr;
  20. unsigned long addr = (unsigned long)info->si_addr;
  21. unsigned long *ip = &UCONTEXT_NIA(uc);
  22. unsigned long *ex_p = (unsigned long *)__start___ex_table;
  23. while (ex_p < (unsigned long *)__stop___ex_table) {
  24. unsigned long insn, fixup;
  25. insn = *ex_p++;
  26. fixup = *ex_p++;
  27. if (insn == *ip) {
  28. *ip = fixup;
  29. return;
  30. }
  31. }
  32. printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
  33. abort();
  34. }
  35. static void setup_segv_handler(void)
  36. {
  37. struct sigaction action;
  38. memset(&action, 0, sizeof(action));
  39. action.sa_sigaction = segv_handler;
  40. action.sa_flags = SA_SIGINFO;
  41. sigaction(SIGSEGV, &action, NULL);
  42. }
  43. unsigned long COPY_LOOP(void *to, const void *from, unsigned long size);
  44. unsigned long test_copy_tofrom_user_reference(void *to, const void *from, unsigned long size);
  45. static int total_passed;
  46. static int total_failed;
  47. static void do_one_test(char *dstp, char *srcp, unsigned long len)
  48. {
  49. unsigned long got, expected;
  50. got = COPY_LOOP(dstp, srcp, len);
  51. expected = test_copy_tofrom_user_reference(dstp, srcp, len);
  52. if (got != expected) {
  53. total_failed++;
  54. printf("FAIL from=%p to=%p len=%ld returned %ld, expected %ld\n",
  55. srcp, dstp, len, got, expected);
  56. //abort();
  57. } else
  58. total_passed++;
  59. }
  60. //#define MAX_LEN 512
  61. #define MAX_LEN 16
  62. int test_copy_exception(void)
  63. {
  64. int page_size;
  65. static char *p, *q;
  66. unsigned long src, dst, len;
  67. page_size = getpagesize();
  68. p = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
  69. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  70. if (p == MAP_FAILED) {
  71. perror("mmap");
  72. exit(1);
  73. }
  74. memset(p, 0, page_size);
  75. setup_segv_handler();
  76. if (mprotect(p + page_size, page_size, PROT_NONE)) {
  77. perror("mprotect");
  78. exit(1);
  79. }
  80. q = p + page_size - MAX_LEN;
  81. for (src = 0; src < MAX_LEN; src++) {
  82. for (dst = 0; dst < MAX_LEN; dst++) {
  83. for (len = 0; len < MAX_LEN+1; len++) {
  84. // printf("from=%p to=%p len=%ld\n", q+dst, q+src, len);
  85. do_one_test(q+dst, q+src, len);
  86. }
  87. }
  88. }
  89. printf("Totals:\n");
  90. printf(" Pass: %d\n", total_passed);
  91. printf(" Fail: %d\n", total_failed);
  92. return 0;
  93. }
  94. int main(void)
  95. {
  96. return test_harness(test_copy_exception, str(COPY_LOOP));
  97. }