comparison test/thunk/test_thunk.c @ 32:f533c5ad1b6e

- added some exception handling to thunk test, to catch w^x tests, etc.
author cslag
date Fri, 27 Nov 2015 01:11:49 +0100
parents 3e629dc19168
children bbefb8b8e74c
comparison
equal deleted inserted replaced
31:6e7b1b7ad9d3 32:f533c5ad1b6e
36 ** 36 **
37 **/ 37 **/
38 38
39 #include <assert.h> 39 #include <assert.h>
40 #include <errno.h> 40 #include <errno.h>
41 #include <signal.h>
42 #include <setjmp.h>
43
44 jmp_buf jbuf;
45
46
47 void segv_handler(int sig)
48 {
49 longjmp(jbuf, 1);
50 }
41 51
42 void my_entry(const char* text) 52 void my_entry(const char* text)
43 { 53 {
44 printf("%s: 1\n", text); 54 printf("%s: 1\n", text);
45 } 55 }
50 { 60 {
51 DCThunk t; 61 DCThunk t;
52 printfun* fp; 62 printfun* fp;
53 dcbInitThunk(&t, &my_entry); 63 dcbInitThunk(&t, &my_entry);
54 fp = (printfun*)&t; 64 fp = (printfun*)&t;
55 fp("stack"); 65 if(setjmp(jbuf) != 0)
66 printf("sigsegv\n");
67 else
68 fp("stack");
56 } 69 }
57 70
58 #include <stdlib.h> 71 #include <stdlib.h>
59 72
60 void test_heap() 73 void test_heap()
65 printf("0\n"); 78 printf("0\n");
66 return; 79 return;
67 } 80 }
68 dcbInitThunk(p, &my_entry); 81 dcbInitThunk(p, &my_entry);
69 fp = (printfun*)p; 82 fp = (printfun*)p;
70 fp("heap"); 83 if(setjmp(jbuf) != 0)
84 printf("sigsegv\n");
85 else
86 fp("heap");
71 free(p); 87 free(p);
72 } 88 }
73 89
74 void test_wx() 90 void test_wx()
75 { 91 {
80 printf("0\n"); 96 printf("0\n");
81 return; 97 return;
82 } 98 }
83 dcbInitThunk(p, &my_entry); 99 dcbInitThunk(p, &my_entry);
84 fp = (printfun*)p; 100 fp = (printfun*)p;
85 fp("wx"); 101 if(setjmp(jbuf) != 0)
102 printf("sigsegv\n");
103 else
104 fp("wx");
86 dcFreeWX((void*)p, sizeof(DCThunk)); 105 dcFreeWX((void*)p, sizeof(DCThunk));
87 } 106 }
88 107
89 int main() 108 int main()
90 { 109 {
91 dcTest_initPlatform(); 110 dcTest_initPlatform();
111
112 signal(SIGSEGV, segv_handler);
92 113
93 printf("Allocating ...\n"); 114 printf("Allocating ...\n");
94 printf("... W^X memory: "); 115 printf("... W^X memory: ");
95 test_wx(); 116 test_wx();
96 printf("... heap memory: "); 117 printf("... heap memory: ");