Mercurial > pub > dyncall > dyncall
annotate test/thunk/test_thunk.c @ 176:ff0c8ee2bbc7
- testcode: missing include fixed
author | cslag |
---|---|
date | Thu, 09 Feb 2017 16:04:51 +0100 |
parents | bbefb8b8e74c |
children | 3999750fae28 |
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> |
176 | 43 #include <string.h> |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
44 |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
45 jmp_buf jbuf; |
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 |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
48 void segv_handler(int sig) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
49 { |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
50 longjmp(jbuf, 1); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
51 } |
0 | 52 |
53 void my_entry(const char* text) | |
54 { | |
155 | 55 printf("%s: %d\n", text, strcmp(text, "wx") == 0 || strcmp(text, "stack") == 0 || strcmp(text, "heap") == 0); /* @@@ */ |
0 | 56 } |
57 | |
58 typedef void (printfun)(const char*); | |
59 | |
60 void test_stack() | |
61 { | |
62 DCThunk t; | |
63 printfun* fp; | |
64 dcbInitThunk(&t, &my_entry); | |
65 fp = (printfun*)&t; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
66 if(setjmp(jbuf) != 0) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
67 printf("sigsegv\n"); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
68 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
69 fp("stack"); |
0 | 70 } |
71 | |
72 #include <stdlib.h> | |
73 | |
74 void test_heap() | |
75 { | |
76 printfun* fp; | |
77 DCThunk* p = (DCThunk*)malloc(sizeof(DCThunk)); | |
78 if(!p) { | |
79 printf("0\n"); | |
80 return; | |
81 } | |
82 dcbInitThunk(p, &my_entry); | |
83 fp = (printfun*)p; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
84 if(setjmp(jbuf) != 0) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
85 printf("sigsegv\n"); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
86 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
87 fp("heap"); |
0 | 88 free(p); |
89 } | |
90 | |
91 void test_wx() | |
92 { | |
93 DCThunk* p; | |
94 printfun* fp; | |
95 int err = dcAllocWX(sizeof(DCThunk), (void**)&p); | |
96 if(err || !p) { | |
97 printf("0\n"); | |
98 return; | |
99 } | |
100 dcbInitThunk(p, &my_entry); | |
101 fp = (printfun*)p; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
102 if(setjmp(jbuf) != 0) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
103 printf("sigsegv\n"); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
104 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
105 fp("wx"); |
0 | 106 dcFreeWX((void*)p, sizeof(DCThunk)); |
107 } | |
108 | |
109 int main() | |
110 { | |
111 dcTest_initPlatform(); | |
112 | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
113 signal(SIGSEGV, segv_handler); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
114 |
0 | 115 printf("Allocating ...\n"); |
116 printf("... W^X memory: "); | |
117 test_wx(); | |
118 printf("... heap memory: "); | |
119 test_heap(); | |
120 printf("... stack memory: "); | |
121 test_stack(); | |
122 | |
123 dcTest_deInitPlatform(); | |
124 | |
125 return 0; | |
126 } | |
127 |