1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <setjmp.h>
#include "vtv_malloc.h"
#include "../../../include/vtv-change-permission.h"
volatile static int signal_count = 0;
sigjmp_buf before_segv;
unsigned int vtv_debug = 0;
static void
handler(int sig, siginfo_t *si, void *unused)
{
signal_count++;
/* You are not supposed to longjmp out of a signal handler but it seems
to work for this test case and it simplifies it */
siglongjmp(before_segv, 1);
}
/* Try to modify the memory pointed by "s" but dont actually change the values.
Assumes and verifies the memory to be modified is mprotected */
void mempoke(void * s, size_t n)
{
volatile char * p = (char *)s;
int ret;
signal_count = 0;
ret = sigsetjmp(before_segv, 1);
if (ret == 0)
p[0] = p[0];
assert(ret == 1 && signal_count == 1);
ret = sigsetjmp(before_segv, 1);
if (ret == 0)
p[n - 1] = p[n - 1];
assert(ret == 1 && signal_count == 2);
}
int main()
{
char * ptr;
int size;
/* Set up handler for SIGSEGV. */
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGSEGV, &sa, NULL) == -1)
assert(0);
/* Make the 'bookkeeping' vars read-write. */
__VLTChangePermission (__VLTP_READ_WRITE);
__vtv_malloc_init();
size = 10;
/* Verify not writable after unprotect */
__vtv_malloc_unprotect();
ptr = (char *)__vtv_malloc(size);
memset(ptr, 'a', size);
__vtv_malloc_protect();
mempoke(ptr, size);
__vtv_free(ptr);
/* verify not-writable after protect, unprotect */
__vtv_malloc_unprotect();
ptr = (char *)__vtv_malloc(size);
memset(ptr, 'a', size);
__vtv_malloc_protect();
__vtv_malloc_unprotect();
memset(ptr, 'a', size);
assert(ptr[size - 1] == 'a');
__vtv_malloc_protect();
assert(ptr[size - 1] == 'a');
mempoke(ptr,size);
__vtv_free(ptr);
/* Allocate a bunch of small objects.
Make sure the alignment is correct.
Verify data has not been corrupted.
Make sure the data cannot modified */
{
int s;
for (s = 3; s < 28; s += 3)
{
size = s;
{
int i;
#define ITERS 1000
char * ptrs[ITERS];
__vtv_malloc_unprotect();
for (i = 0; i < ITERS; i++)
{
ptr = (char *)__vtv_malloc(size);
assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
memset(ptr, (i & 127), size);
assert(ptr[size - 1] == (i & 127));
ptrs[i] = ptr;
}
__vtv_malloc_protect();
for (i = 0; i < ITERS; i++)
mempoke(ptrs[i], size);
__vtv_malloc_unprotect();
for (i = 0; i < ITERS; i++)
__vtv_free(ptrs[i]);
__vtv_malloc_protect();
}
}
}
/* Allocate a bunch of medium size objects.
Make sure the alignment is correct.
Verify data has not been corrupted.
Try to modify the data to verify everything gets unprotected */
{
int s;
for (s = 501; s < 2500; s += 91)
{
size = s;
{
int i;
#define ITERS2 100
char * ptrs[ITERS2];
__vtv_malloc_unprotect();
for (i = 0; i < ITERS2; i++)
{
ptr = (char *)__vtv_malloc(size);
assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
memset(ptr, i & 127, size);
assert(ptr[size - 1] == i & 127);
ptrs[i] = ptr;
}
__vtv_malloc_protect();
for (i = 0; i < ITERS2; i++)
mempoke(ptrs[i], size);
__vtv_malloc_unprotect();
for (i = 0; i < ITERS2; i++)
__vtv_free(ptrs[i]);
__vtv_malloc_protect();
}
}
}
/* Allocate a bunch of medium size objects. Make sure the alignment is correct */
{
int s;
for (s = 3001; s < 15000; s += 307)
{
size = s;
{
int i;
#define ITERS3 50
char * ptrs[ITERS3];
__vtv_malloc_unprotect();
for (i = 0; i < ITERS3; i++)
{
ptr = (char *)__vtv_malloc(size);
assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0);
memset(ptr, i & 127, size);
assert(ptr[size - 1] == i & 127);
ptrs[i] = ptr;
}
__vtv_malloc_protect();
for (i = 0; i < ITERS3; i++)
mempoke(ptrs[i], size);
__vtv_malloc_unprotect();
for (i = 0; i < ITERS3; i++)
__vtv_free(ptrs[i]);
__vtv_malloc_protect();
}
}
}
return 0;
}
|