6 #include "vtv_malloc.h"
7 #include "../../../include/vtv-change-permission.h"
9 unsigned int vtv_debug
= 0;
12 handler(int sig
, siginfo_t
*si
, void *unused
)
14 printf("Got SIGSEGV at address: 0x%lx\n",
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
++)
33 /* Set up handler for SIGSEGV. In this test case, we should never hit any SIGSEGV */
35 sa
.sa_flags
= SA_SIGINFO
;
36 sigemptyset(&sa
.sa_mask
);
37 sa
.sa_sigaction
= handler
;
38 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1)
41 /* Make the 'bookkeeping' vars read-write. */
42 __VLTChangePermission (__VLTP_READ_WRITE
);
47 /* Verify simple allocation and deallocation */
48 __vtv_malloc_unprotect();
49 ptr
= (char *)__vtv_malloc(size
);
50 __vtv_malloc_protect();
53 /* Verify writable after unprotect */
54 __vtv_malloc_unprotect();
55 ptr
= (char *)__vtv_malloc(size
);
56 memset(ptr
, 'a', size
);
57 __vtv_malloc_protect();
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');
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');
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 */
86 for (s
= 3; s
< 28; s
+= 3)
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));
103 __vtv_malloc_protect();
105 __vtv_malloc_unprotect();
106 for (i
= 0; i
< ITERS
; i
++)
108 if (memchk(ptrs
[i
], i
& 127, size
) != 0)
110 memset(ptrs
[i
], (i
+ 1) & 127, size
);
111 if (memchk(ptrs
[i
], (i
+ 1) & 127, size
) != 0)
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 */
126 for (s
= 501; s
< 2500; s
+= 91)
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);
144 __vtv_malloc_protect();
146 __vtv_malloc_unprotect();
147 for (i
= 0; i
< ITERS2
; i
++)
149 if (memchk(ptrs
[i
], i
& 127, size
) != 0)
151 memset(ptrs
[i
], (i
+ 1) & 127, size
);
152 if (memchk(ptrs
[i
], (i
+ 1) & 127, size
) != 0)
156 __vtv_malloc_protect();
161 /* Allocate a bunch of medium size objects. Make sure the alignment is correct */
164 for (s
= 3001; s
< 15000; s
+= 307)
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);
181 __vtv_malloc_protect();
183 __vtv_malloc_unprotect();
184 for (i
= 0; i
< ITERS3
; i
++)
186 if (memchk(ptrs
[i
], i
& 127, size
) != 0)
188 memset(ptrs
[i
], (i
+ 1) & 127, size
);
189 if (memchk(ptrs
[i
], (i
+ 1) & 127, size
) != 0)
193 __vtv_malloc_protect();