Mercurial > pub > dyncall > dyncall
annotate test/thunk/test_thunk.c @ 383:268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
whenever available to enable handling stack errors (as handler uses same
stack); thanks for bringing this to our attention, Bernhard!
author | Tassilo Philipp |
---|---|
date | Wed, 20 Jan 2021 17:36:34 +0100 |
parents | fccbb45a2dae |
children | 2144287113df |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: test | |
5 File: test/thunk/test_thunk.c | |
6 Description: | |
7 License: | |
8 | |
371
451299d50c1a
- windows arm64 support (dyncall, dyncallback, cmake support for armasm64), thanks Bernhard Urban-Forster!
Tassilo Philipp
parents:
281
diff
changeset
|
9 Copyright (c) 2011-2020 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 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 |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
47 static int last_sig; |
32
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 void segv_handler(int sig) |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
50 { |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
51 last_sig = sig; |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
52 longjmp(jbuf, 1); |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
53 } |
0 | 54 |
55 void my_entry(const char* text) | |
56 { | |
155 | 57 printf("%s: %d\n", text, strcmp(text, "wx") == 0 || strcmp(text, "stack") == 0 || strcmp(text, "heap") == 0); /* @@@ */ |
0 | 58 } |
59 | |
60 typedef void (printfun)(const char*); | |
61 | |
62 void test_stack() | |
63 { | |
64 DCThunk t; | |
65 printfun* fp; | |
66 dcbInitThunk(&t, &my_entry); | |
67 fp = (printfun*)&t; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
68 if(setjmp(jbuf) != 0) |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
69 printf(last_sig == SIGSEGV ? "sigsegv\n" : "sigbus\n"); |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
70 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
71 fp("stack"); |
0 | 72 } |
73 | |
74 #include <stdlib.h> | |
75 | |
76 void test_heap() | |
77 { | |
78 printfun* fp; | |
79 DCThunk* p = (DCThunk*)malloc(sizeof(DCThunk)); | |
80 if(!p) { | |
81 printf("0\n"); | |
82 return; | |
83 } | |
84 dcbInitThunk(p, &my_entry); | |
85 fp = (printfun*)p; | |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
86 if(setjmp(jbuf) != 0) |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
87 printf(last_sig == SIGSEGV ? "sigsegv\n" : "sigbus\n"); |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
88 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
89 fp("heap"); |
0 | 90 free(p); |
91 } | |
92 | |
93 void test_wx() | |
94 { | |
95 DCThunk* p; | |
96 printfun* fp; | |
97 int err = dcAllocWX(sizeof(DCThunk), (void**)&p); | |
98 if(err || !p) { | |
99 printf("0\n"); | |
100 return; | |
101 } | |
102 dcbInitThunk(p, &my_entry); | |
203
3999750fae28
- allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents:
176
diff
changeset
|
103 err = dcInitExecWX((void*)p, sizeof(DCThunk)); |
3999750fae28
- allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents:
176
diff
changeset
|
104 if(err) { |
3999750fae28
- allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents:
176
diff
changeset
|
105 dcFreeWX((void*)p, sizeof(DCThunk)); |
3999750fae28
- allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents:
176
diff
changeset
|
106 printf("0\n"); |
3999750fae28
- allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents:
176
diff
changeset
|
107 return; |
3999750fae28
- allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents:
176
diff
changeset
|
108 } |
0 | 109 fp = (printfun*)p; |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
110 if(setjmp(jbuf) != 0) |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
111 printf(last_sig == SIGSEGV ? "sigsegv\n" : "sigbus\n"); |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
112 else |
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
113 fp("wx"); |
0 | 114 dcFreeWX((void*)p, sizeof(DCThunk)); |
115 } | |
116 | |
117 int main() | |
118 { | |
119 dcTest_initPlatform(); | |
120 | |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
121 /* handle sigsegv and sigbus (latter used on some platforms for some mem */ |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
122 /* access errors); use more complex setup if SA_ONSTACK is available */ |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
123 |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
124 #if defined(SA_ONSTACK) |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
125 /* notes: |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
126 - use sigaction(2) to pass SA_ONSTACK, to handle segfaults on stack (as |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
127 handler would use same stack, this needs to be requested explicitly) |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
128 - not using sigaltstack(2), as no need in our case |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
129 */ |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
130 struct sigaction sigAct; |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
131 sigfillset(&(sigAct.sa_mask)); |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
132 sigAct.sa_sigaction = segv_handler; |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
133 sigAct.sa_flags = SA_ONSTACK; |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
134 sigaction(SIGSEGV, &sigAct, NULL); |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
135 sigaction(SIGBUS, &sigAct, NULL); |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
136 #else |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
137 signal(SIGSEGV, segv_handler); |
381 | 138 #if !defined(DC_WINDOWS) |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
139 signal(SIGBUS, segv_handler); |
381 | 140 #endif |
383
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
141 #endif |
268378d66610
- thunk test: better signal handling, using sigaction(2) with SA_ONSTACK
Tassilo Philipp
parents:
381
diff
changeset
|
142 |
32
f533c5ad1b6e
- added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents:
0
diff
changeset
|
143 |
0 | 144 printf("Allocating ...\n"); |
145 printf("... W^X memory: "); | |
146 test_wx(); | |
147 printf("... heap memory: "); | |
148 test_heap(); | |
149 printf("... stack memory: "); | |
150 test_stack(); | |
151 | |
152 dcTest_deInitPlatform(); | |
153 | |
154 return 0; | |
155 } | |
156 |