hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
 
#include "utils.h"
 
extern char __start___ex_table[];
extern char __stop___ex_table[];
 
#if defined(__powerpc64__)
#define UCONTEXT_NIA(UC)    (UC)->uc_mcontext.gp_regs[PT_NIP]
#elif defined(__powerpc__)
#define UCONTEXT_NIA(UC)    (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
#else
#error implement UCONTEXT_NIA
#endif
 
static void segv_handler(int signr, siginfo_t *info, void *ptr)
{
   ucontext_t *uc = (ucontext_t *)ptr;
   unsigned long addr = (unsigned long)info->si_addr;
   unsigned long *ip = &UCONTEXT_NIA(uc);
   unsigned long *ex_p = (unsigned long *)__start___ex_table;
 
   while (ex_p < (unsigned long *)__stop___ex_table) {
       unsigned long insn, fixup;
 
       insn = *ex_p++;
       fixup = *ex_p++;
 
       if (insn == *ip) {
           *ip = fixup;
           return;
       }
   }
 
   printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
   abort();
}
 
static void setup_segv_handler(void)
{
   struct sigaction action;
 
   memset(&action, 0, sizeof(action));
   action.sa_sigaction = segv_handler;
   action.sa_flags = SA_SIGINFO;
   sigaction(SIGSEGV, &action, NULL);
}
 
unsigned long COPY_LOOP(void *to, const void *from, unsigned long size);
unsigned long test_copy_tofrom_user_reference(void *to, const void *from, unsigned long size);
 
static int total_passed;
static int total_failed;
 
static void do_one_test(char *dstp, char *srcp, unsigned long len)
{
   unsigned long got, expected;
 
   got = COPY_LOOP(dstp, srcp, len);
   expected = test_copy_tofrom_user_reference(dstp, srcp, len);
 
   if (got != expected) {
       total_failed++;
       printf("FAIL from=%p to=%p len=%ld returned %ld, expected %ld\n",
              srcp, dstp, len, got, expected);
       //abort();
   } else
       total_passed++;
}
 
//#define MAX_LEN 512
#define MAX_LEN 16
 
int test_copy_exception(void)
{
   int page_size;
   static char *p, *q;
   unsigned long src, dst, len;
 
   page_size = getpagesize();
   p = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
 
   if (p == MAP_FAILED) {
       perror("mmap");
       exit(1);
   }
 
   memset(p, 0, page_size);
 
   setup_segv_handler();
 
   if (mprotect(p + page_size, page_size, PROT_NONE)) {
       perror("mprotect");
       exit(1);
   }
 
   q = p + page_size - MAX_LEN;
 
   for (src = 0; src < MAX_LEN; src++) {
       for (dst = 0; dst < MAX_LEN; dst++) {
           for (len = 0; len < MAX_LEN+1; len++) {
               // printf("from=%p to=%p len=%ld\n", q+dst, q+src, len);
               do_one_test(q+dst, q+src, len);
           }
       }
   }
 
   printf("Totals:\n");
   printf("  Pass: %d\n", total_passed);
   printf("  Fail: %d\n", total_failed);
 
   return 0;
}
 
int main(void)
{
   return test_harness(test_copy_exception, str(COPY_LOOP));
}