summaryrefslogtreecommitdiffstats
path: root/libvtv/testsuite/mempool_positive.c
blob: 511f50a1040bf3a00902c6a1555934968c20e25d (plain)
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
194
195
196
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <stdio.h>

#include "vtv_malloc.h"

bool vtv_debug = false;

static void
handler(int sig, siginfo_t *si, void *unused)
{
  printf("Got SIGSEGV at address: 0x%lx\n",
         (long) si->si_addr);
  exit(1);
}

int memchk(const void * s, int c, size_t n)
{
  const char * p = (const char *)s;
  for (; p < ((char *)s + n); p++)
    if (*p != c)
      return 1;
  return 0;
}

int main()
{
  char * ptr;
  int size;

  /* Set up handler for SIGSEGV. In this test case, we should never hit any 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);

  __vtv_malloc_init();

  size = 10;

  /* Verify simple allocation and deallocation */
  __vtv_malloc_unprotect();
  ptr = (char *)__vtv_malloc(size);
  __vtv_malloc_protect();
  __vtv_free(ptr);

  /* Verify writable after unprotect */
  __vtv_malloc_unprotect();
  ptr = (char *)__vtv_malloc(size);
  memset(ptr, 'a', size);
  __vtv_malloc_protect();
  __vtv_free(ptr);

  /* verify readable after protect */
  __vtv_malloc_unprotect();
  ptr = (char *)__vtv_malloc(size);
  memset(ptr, 'a', size);
  __vtv_malloc_protect();
  assert(ptr[size - 1] == 'a');
  __vtv_free(ptr);

  /* verify 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');
  __vtv_free(ptr);

  /* Allocate a bunch of small 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 = 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();

        __vtv_malloc_unprotect();
        for (i = 0; i < ITERS; i++)
        {
          if (memchk(ptrs[i], i & 127, size) != 0)
            assert(0);
          memset(ptrs[i], (i + 1) & 127, size);
          if (memchk(ptrs[i], (i + 1) & 127, size) != 0)
            assert(0);
          __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();

        __vtv_malloc_unprotect();
        for (i = 0; i < ITERS2; i++)
        {
          if (memchk(ptrs[i], i & 127, size) != 0)
            assert(0);
          memset(ptrs[i], (i + 1) & 127, size);
          if (memchk(ptrs[i], (i + 1) & 127, size) != 0)
            assert(0);
          __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();

        __vtv_malloc_unprotect();
        for (i = 0; i < ITERS3; i++)
        {
          if (memchk(ptrs[i], i & 127, size) != 0)
            assert(0);
          memset(ptrs[i], (i + 1) & 127, size);
          if (memchk(ptrs[i], (i + 1) & 127, size) != 0)
            assert(0);
          __vtv_free(ptrs[i]);
        }
        __vtv_malloc_protect();
      }
    }
  }

  return 0;
}
OpenPOWER on IntegriCloud