7 #include "vtv_malloc.h"
8 #include "../../../include/vtv-change-permission.h"
10 volatile static int signal_count
= 0;
12 sigjmp_buf before_segv
;
14 unsigned int vtv_debug
= 0;
17 handler(int sig
, siginfo_t
*si
, void *unused
)
20 /* You are not supposed to longjmp out of a signal handler but it seems
21 to work for this test case and it simplifies it */
22 siglongjmp(before_segv
, 1);
25 /* Try to modify the memory pointed by "s" but dont actually change the values.
26 Assumes and verifies the memory to be modified is mprotected */
27 void mempoke(void * s
, size_t n
)
29 volatile char * p
= (char *)s
;
33 ret
= sigsetjmp(before_segv
, 1);
37 assert(ret
== 1 && signal_count
== 1);
39 ret
= sigsetjmp(before_segv
, 1);
43 assert(ret
== 1 && signal_count
== 2);
51 /* Set up handler for SIGSEGV. */
53 sa
.sa_flags
= SA_SIGINFO
;
54 sigemptyset(&sa
.sa_mask
);
55 sa
.sa_sigaction
= handler
;
56 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1)
59 /* Make the 'bookkeeping' vars read-write. */
60 __VLTChangePermission (__VLTP_READ_WRITE
);
65 /* Verify not writable after unprotect */
66 __vtv_malloc_unprotect();
67 ptr
= (char *)__vtv_malloc(size
);
68 memset(ptr
, 'a', size
);
69 __vtv_malloc_protect();
73 /* verify not-writable after protect, unprotect */
74 __vtv_malloc_unprotect();
75 ptr
= (char *)__vtv_malloc(size
);
76 memset(ptr
, 'a', size
);
77 __vtv_malloc_protect();
78 __vtv_malloc_unprotect();
79 memset(ptr
, 'a', size
);
80 assert(ptr
[size
- 1] == 'a');
81 __vtv_malloc_protect();
82 assert(ptr
[size
- 1] == 'a');
86 /* Allocate a bunch of small objects.
87 Make sure the alignment is correct.
88 Verify data has not been corrupted.
89 Make sure the data cannot modified */
92 for (s
= 3; s
< 28; s
+= 3)
100 __vtv_malloc_unprotect();
101 for (i
= 0; i
< ITERS
; i
++)
103 ptr
= (char *)__vtv_malloc(size
);
104 assert(((long)ptr
& VTV_ALIGNMENT_MASK
) == 0);
105 memset(ptr
, (i
& 127), size
);
106 assert(ptr
[size
- 1] == (i
& 127));
109 __vtv_malloc_protect();
111 for (i
= 0; i
< ITERS
; i
++)
112 mempoke(ptrs
[i
], size
);
114 __vtv_malloc_unprotect();
115 for (i
= 0; i
< ITERS
; i
++)
117 __vtv_malloc_protect();
122 /* Allocate a bunch of medium size objects.
123 Make sure the alignment is correct.
124 Verify data has not been corrupted.
125 Try to modify the data to verify everything gets unprotected */
128 for (s
= 501; s
< 2500; s
+= 91)
136 __vtv_malloc_unprotect();
137 for (i
= 0; i
< ITERS2
; i
++)
140 ptr
= (char *)__vtv_malloc(size
);
141 assert(((long)ptr
& VTV_ALIGNMENT_MASK
) == 0);
142 memset(ptr
, i
& 127, size
);
143 assert(ptr
[size
- 1] == i
& 127);
146 __vtv_malloc_protect();
148 for (i
= 0; i
< ITERS2
; i
++)
149 mempoke(ptrs
[i
], size
);
151 __vtv_malloc_unprotect();
152 for (i
= 0; i
< ITERS2
; i
++)
154 __vtv_malloc_protect();
159 /* Allocate a bunch of medium size objects. Make sure the alignment is correct */
162 for (s
= 3001; s
< 15000; s
+= 307)
170 __vtv_malloc_unprotect();
171 for (i
= 0; i
< ITERS3
; i
++)
173 ptr
= (char *)__vtv_malloc(size
);
174 assert(((long)ptr
& VTV_ALIGNMENT_MASK
) == 0);
175 memset(ptr
, i
& 127, size
);
176 assert(ptr
[size
- 1] == i
& 127);
179 __vtv_malloc_protect();
181 for (i
= 0; i
< ITERS3
; i
++)
182 mempoke(ptrs
[i
], size
);
184 __vtv_malloc_unprotect();
185 for (i
= 0; i
< ITERS3
; i
++)
187 __vtv_malloc_protect();