Mercurial > pub > dyncall > dyncall
annotate test/thunk/test_thunk.c @ 49:4388e27eadd7
merge
author | Daniel Adler |
---|---|
date | Sun, 20 Dec 2015 00:12:06 +0100 |
parents | f533c5ad1b6e |
children | bbefb8b8e74c |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: test | |
5 File: test/thunk/test_thunk.c | |
6 Description: | |
7 License: | |
8 | |
9 Copyright (c) 2011-2015 Daniel Adler <dadler@uni-goettingen.de>, | |
10 Tassilo Philipp <tphilipp@potion-studios.com> | |
11 | |
12 Permission to use, copy, modify, and distribute this software for any | |
13 purpose with or without fee is hereby granted, provided that the above | |
14 copyright notice and this permission notice appear in all copies. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
23 | |
24 */ | |
25 | |
26 #include "../../dyncall/dyncall.h" | |
27 #include "../../dyncallback/dyncall_thunk.h" | |
28 #include "../../dyncallback/dyncall_alloc_wx.h" | |
29 #include "../common/platformInit.h" | |
30 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ | |
31 | |
32 | |
33 /** | |
34 ** test: thunks with several allocation modes | |
35 ** such as on stack, on heap and explicit wx | |
36 ** | |
37 **/ | |
38 | |
39 #include <assert.h> | |
40 #include <errno.h> | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
41 #include <signal.h> |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
42 #include <setjmp.h> |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
43 |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
44 jmp_buf jbuf; |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
45 |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
46 |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
47 void segv_handler(int sig) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
48 { |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
49 longjmp(jbuf, 1); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
50 } |
0 | 51 |
52 void my_entry(const char* text) | |
53 { | |
54 printf("%s: 1\n", text); | |
55 } | |
56 | |
57 typedef void (printfun)(const char*); | |
58 | |
59 void test_stack() | |
60 { | |
61 DCThunk t; | |
62 printfun* fp; | |
63 dcbInitThunk(&t, &my_entry); | |
64 fp = (printfun*)&t; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
65 if(setjmp(jbuf) != 0) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
66 printf("sigsegv\n"); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
67 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
68 fp("stack"); |
0 | 69 } |
70 | |
71 #include <stdlib.h> | |
72 | |
73 void test_heap() | |
74 { | |
75 printfun* fp; | |
76 DCThunk* p = (DCThunk*)malloc(sizeof(DCThunk)); | |
77 if(!p) { | |
78 printf("0\n"); | |
79 return; | |
80 } | |
81 dcbInitThunk(p, &my_entry); | |
82 fp = (printfun*)p; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
83 if(setjmp(jbuf) != 0) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
84 printf("sigsegv\n"); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
85 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
86 fp("heap"); |
0 | 87 free(p); |
88 } | |
89 | |
90 void test_wx() | |
91 { | |
92 DCThunk* p; | |
93 printfun* fp; | |
94 int err = dcAllocWX(sizeof(DCThunk), (void**)&p); | |
95 if(err || !p) { | |
96 printf("0\n"); | |
97 return; | |
98 } | |
99 dcbInitThunk(p, &my_entry); | |
100 fp = (printfun*)p; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
101 if(setjmp(jbuf) != 0) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
102 printf("sigsegv\n"); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
103 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
104 fp("wx"); |
0 | 105 dcFreeWX((void*)p, sizeof(DCThunk)); |
106 } | |
107 | |
108 int main() | |
109 { | |
110 dcTest_initPlatform(); | |
111 | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
112 signal(SIGSEGV, segv_handler); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
113 |
0 | 114 printf("Allocating ...\n"); |
115 printf("... W^X memory: "); | |
116 test_wx(); | |
117 printf("... heap memory: "); | |
118 test_heap(); | |
119 printf("... stack memory: "); | |
120 test_stack(); | |
121 | |
122 dcTest_deInitPlatform(); | |
123 | |
124 return 0; | |
125 } | |
126 |