[gcc/testsuite]
[official-gcc.git] / libvtv / testsuite / libvtv.mempool.cc / mempool_positive.cc
blob5e60df9b686eeec6d056e338e848db1ceefccf70
1 #include <string.h>
2 #include <assert.h>
3 #include <signal.h>
4 #include <stdio.h>
6 #include "vtv_malloc.h"
7 #include "../../../include/vtv-change-permission.h"
9 unsigned int vtv_debug = 0;
11 static void
12 handler(int sig, siginfo_t *si, void *unused)
14 printf("Got SIGSEGV at address: 0x%lx\n",
15 (long) si->si_addr);
16 exit(1);
19 int memchk(const void * s, int c, size_t n)
21 const char * p = (const char *)s;
22 for (; p < ((char *)s + n); p++)
23 if (*p != c)
24 return 1;
25 return 0;
28 int main()
30 char * ptr;
31 int size;
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);
41 /* Make the 'bookkeeping' vars read-write. */
42 __VLTChangePermission (__VLTP_READ_WRITE);
43 __vtv_malloc_init();
45 size = 10;
47 /* Verify simple allocation and deallocation */
48 __vtv_malloc_unprotect();
49 ptr = (char *)__vtv_malloc(size);
50 __vtv_malloc_protect();
51 __vtv_free(ptr);
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);
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);
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);
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 */
85 int s;
86 for (s = 3; s < 28; s += 3)
88 size = s;
90 int i;
91 #define ITERS 1000
92 char * ptrs[ITERS];
94 __vtv_malloc_unprotect();
95 for (i = 0; i < ITERS; i++)
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;
103 __vtv_malloc_protect();
105 __vtv_malloc_unprotect();
106 for (i = 0; i < ITERS; i++)
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]);
115 __vtv_malloc_protect();
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 */
125 int s;
126 for (s = 501; s < 2500; s += 91)
128 size = s;
130 int i;
131 #define ITERS2 100
132 char * ptrs[ITERS2];
134 __vtv_malloc_unprotect();
135 for (i = 0; i < ITERS2; i++)
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;
144 __vtv_malloc_protect();
146 __vtv_malloc_unprotect();
147 for (i = 0; i < ITERS2; i++)
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]);
156 __vtv_malloc_protect();
161 /* Allocate a bunch of medium size objects. Make sure the alignment is correct */
163 int s;
164 for (s = 3001; s < 15000; s += 307)
166 size = s;
168 int i;
169 #define ITERS3 50
170 char * ptrs[ITERS3];
172 __vtv_malloc_unprotect();
173 for (i = 0; i < ITERS3; i++)
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;
181 __vtv_malloc_protect();
183 __vtv_malloc_unprotect();
184 for (i = 0; i < ITERS3; i++)
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]);
193 __vtv_malloc_protect();
198 return 0;