annotate test/thunk/test_thunk.c @ 203:3999750fae28

- allocwx changes from last commit mentioned in Changelog and adapted testcode
author Tassilo Philipp
date Tue, 21 Mar 2017 02:31:55 +0100
parents ff0c8ee2bbc7
children f5577f6bf97a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /*
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 Package: dyncall
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 Library: test
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 File: test/thunk/test_thunk.c
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 Description:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 License:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 Copyright (c) 2011-2015 Daniel Adler <dadler@uni-goettingen.de>,
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 Tassilo Philipp <tphilipp@potion-studios.com>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 Permission to use, copy, modify, and distribute this software for any
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 purpose with or without fee is hereby granted, provided that the above
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 copyright notice and this permission notice appear in all copies.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 #include "../../dyncall/dyncall.h"
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 #include "../../dyncallback/dyncall_thunk.h"
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 #include "../../dyncallback/dyncall_alloc_wx.h"
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 #include "../common/platformInit.h"
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 /**
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 ** test: thunks with several allocation modes
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 ** such as on stack, on heap and explicit wx
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 **
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 **/
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 #include <assert.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
ff0c8ee2bbc7 - testcode: missing include fixed
cslag
parents: 155
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 void my_entry(const char* text)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 {
155
bbefb8b8e74c - sparc32 thunk code
cslag
parents: 32
diff changeset
55 printf("%s: %d\n", text, strcmp(text, "wx") == 0 || strcmp(text, "stack") == 0 || strcmp(text, "heap") == 0); /* @@@ */
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 typedef void (printfun)(const char*);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 void test_stack()
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 DCThunk t;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 printfun* fp;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 dcbInitThunk(&t, &my_entry);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 #include <stdlib.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 void test_heap()
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 printfun* fp;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 DCThunk* p = (DCThunk*)malloc(sizeof(DCThunk));
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 if(!p) {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 printf("0\n");
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 return;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 dcbInitThunk(p, &my_entry);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 free(p);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 void test_wx()
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 DCThunk* p;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 printfun* fp;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 int err = dcAllocWX(sizeof(DCThunk), (void**)&p);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 if(err || !p) {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 printf("0\n");
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 return;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 dcbInitThunk(p, &my_entry);
203
3999750fae28 - allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents: 176
diff changeset
101 err = dcInitExecWX((void*)p, sizeof(DCThunk));
3999750fae28 - allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents: 176
diff changeset
102 if(err) {
3999750fae28 - allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents: 176
diff changeset
103 dcFreeWX((void*)p, sizeof(DCThunk));
3999750fae28 - allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents: 176
diff changeset
104 printf("0\n");
3999750fae28 - allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents: 176
diff changeset
105 return;
3999750fae28 - allocwx changes from last commit mentioned in Changelog and adapted testcode
Tassilo Philipp
parents: 176
diff changeset
106 }
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 fp = (printfun*)p;
32
f533c5ad1b6e - added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents: 0
diff changeset
108 if(setjmp(jbuf) != 0)
f533c5ad1b6e - added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents: 0
diff changeset
109 printf("sigsegv\n");
f533c5ad1b6e - added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents: 0
diff changeset
110 else
f533c5ad1b6e - added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents: 0
diff changeset
111 fp("wx");
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 dcFreeWX((void*)p, sizeof(DCThunk));
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 int main()
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 dcTest_initPlatform();
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118
32
f533c5ad1b6e - added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents: 0
diff changeset
119 signal(SIGSEGV, segv_handler);
f533c5ad1b6e - added some exception handling to thunk test, to catch w^x tests, etc.
cslag
parents: 0
diff changeset
120
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121 printf("Allocating ...\n");
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 printf("... W^X memory: ");
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 test_wx();
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 printf("... heap memory: ");
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 test_heap();
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 printf("... stack memory: ");
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 test_stack();
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 dcTest_deInitPlatform();
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 return 0;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133