]> git.ipfire.org Git - thirdparty/gcc.git/blob - libvtv/testsuite/libvtv.mempool.cc/mempool_positive.cc
Update libvtv testsuite so that most of the tests now run under
[thirdparty/gcc.git] / libvtv / testsuite / libvtv.mempool.cc / mempool_positive.cc
1 #include <string.h>
2 #include <assert.h>
3 #include <signal.h>
4 #include <stdio.h>
5
6 #include "vtv_malloc.h"
7 #include "../../../include/vtv-change-permission.h"
8
9 unsigned int vtv_debug = 0;
10
11 static void
12 handler(int sig, siginfo_t *si, void *unused)
13 {
14 printf("Got SIGSEGV at address: 0x%lx\n",
15 (long) si->si_addr);
16 exit(1);
17 }
18
19 int memchk(const void * s, int c, size_t n)
20 {
21 const char * p = (const char *)s;
22 for (; p < ((char *)s + n); p++)
23 if (*p != c)
24 return 1;
25 return 0;
26 }
27
28 int main()
29 {
30 char * ptr;
31 int size;
32
33 /* Set up handler for SIGSEGV. In this test case, we should never hit any SIGSEGV */
34 struct sigaction sa;
35 sa.sa_flags = SA_SIGINFO;
36 sigemptyset(&sa.sa_mask);
37 sa.sa_sigaction = handler;
38 if (sigaction(SIGSEGV, &sa, NULL) == -1)
39 assert(0);
40
41 /* Make the 'bookkeeping' vars read-write. */
42 __VLTChangePermission (__VLTP_READ_WRITE);
43 __vtv_malloc_init();
44
45 size = 10;
46
47 /* Verify simple allocation and deallocation */
48 __vtv_malloc_unprotect();
49 ptr = (char *)__vtv_malloc(size);
50 __vtv_malloc_protect();
51 __vtv_free(ptr);
52
53 /* Verify writable after unprotect */
54 __vtv_malloc_unprotect();
55 ptr = (char *)__vtv_malloc(size);
56 memset(ptr, 'a', size);
57 __vtv_malloc_protect();
58 __vtv_free(ptr);
59
60 /* verify readable after protect */
61 __vtv_malloc_unprotect();
62 ptr = (char *)__vtv_malloc(size);
63 memset(ptr, 'a', size);
64 __vtv_malloc_protect();
65 assert(ptr[size - 1] == 'a');
66 __vtv_free(ptr);
67
68 /* verify writable after protect, unprotect */
69 __vtv_malloc_unprotect();
70 ptr = (char *)__vtv_malloc(size);
71 memset(ptr, 'a', size);
72 __vtv_malloc_protect();
73 __vtv_malloc_unprotect();
74 memset(ptr, 'a', size);
75 assert(ptr[size - 1] == 'a');
76 __vtv_malloc_protect();
77 assert(ptr[size - 1] == 'a');
78 __vtv_free(ptr);
79
80 /* Allocate a bunch of small objects.
81 Make sure the alignment is correct.
82 Verify data has not been corrupted.
83 Try to modify the data to verify everything gets unprotected */
84 {
85 int s;
86 for (s = 3; s < 28; s += 3)
87 {
88 size = s;
89 {
90 int i;
91 #define ITERS 1000
92 char * ptrs[ITERS];
93
94 __vtv_malloc_unprotect();
95 for (i = 0; i < ITERS; i++)
96 {
97 ptr = (char *)__vtv_malloc(size);
98 assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
99 memset(ptr, (i & 127), size);
100 assert(ptr[size - 1] == (i & 127));
101 ptrs[i] = ptr;
102 }
103 __vtv_malloc_protect();
104
105 __vtv_malloc_unprotect();
106 for (i = 0; i < ITERS; i++)
107 {
108 if (memchk(ptrs[i], i & 127, size) != 0)
109 assert(0);
110 memset(ptrs[i], (i + 1) & 127, size);
111 if (memchk(ptrs[i], (i + 1) & 127, size) != 0)
112 assert(0);
113 __vtv_free(ptrs[i]);
114 }
115 __vtv_malloc_protect();
116 }
117 }
118 }
119
120 /* Allocate a bunch of medium size objects.
121 Make sure the alignment is correct.
122 Verify data has not been corrupted.
123 Try to modify the data to verify everything gets unprotected */
124 {
125 int s;
126 for (s = 501; s < 2500; s += 91)
127 {
128 size = s;
129 {
130 int i;
131 #define ITERS2 100
132 char * ptrs[ITERS2];
133
134 __vtv_malloc_unprotect();
135 for (i = 0; i < ITERS2; i++)
136 {
137
138 ptr = (char *)__vtv_malloc(size);
139 assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
140 memset(ptr, i & 127, size);
141 assert(ptr[size - 1] == i & 127);
142 ptrs[i] = ptr;
143 }
144 __vtv_malloc_protect();
145
146 __vtv_malloc_unprotect();
147 for (i = 0; i < ITERS2; i++)
148 {
149 if (memchk(ptrs[i], i & 127, size) != 0)
150 assert(0);
151 memset(ptrs[i], (i + 1) & 127, size);
152 if (memchk(ptrs[i], (i + 1) & 127, size) != 0)
153 assert(0);
154 __vtv_free(ptrs[i]);
155 }
156 __vtv_malloc_protect();
157 }
158 }
159 }
160
161 /* Allocate a bunch of medium size objects. Make sure the alignment is correct */
162 {
163 int s;
164 for (s = 3001; s < 15000; s += 307)
165 {
166 size = s;
167 {
168 int i;
169 #define ITERS3 50
170 char * ptrs[ITERS3];
171
172 __vtv_malloc_unprotect();
173 for (i = 0; i < ITERS3; i++)
174 {
175 ptr = (char *)__vtv_malloc(size);
176 assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
177 memset(ptr, i & 127, size);
178 assert(ptr[size - 1] == i & 127);
179 ptrs[i] = ptr;
180 }
181 __vtv_malloc_protect();
182
183 __vtv_malloc_unprotect();
184 for (i = 0; i < ITERS3; i++)
185 {
186 if (memchk(ptrs[i], i & 127, size) != 0)
187 assert(0);
188 memset(ptrs[i], (i + 1) & 127, size);
189 if (memchk(ptrs[i], (i + 1) & 127, size) != 0)
190 assert(0);
191 __vtv_free(ptrs[i]);
192 }
193 __vtv_malloc_protect();
194 }
195 }
196 }
197
198 return 0;
199 }