File: dumatest.c

package info (click to toggle)
duma 2.5.21-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,460 kB
  • sloc: ansic: 5,512; cpp: 2,205; makefile: 441; javascript: 191; sh: 129
file content (366 lines) | stat: -rw-r--r-- 7,833 bytes parent folder | download | duplicates (2)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

/*
 * DUMA - Red-Zone memory allocator.
 * Copyright (C) 2002-2008 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
 * Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
 * License: GNU GPL (GNU General Public License, see COPYING-GPL)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 * FILE CONTENTS:
 * DUMA confidence tests.
 * Make sure all of the various functions of DUMA work correctly.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if !defined(WIN32) || defined(__CYGWIN__)
  #include <unistd.h>
#else
  #include <io.h>
#endif
#include <setjmp.h>
#include <signal.h>

#include "../duma.h"


#ifndef  PAGE_PROTECTION_VIOLATED_SIGNAL
/* changed default in code below to use two signals: SIGSEGV and SIGBUS */
/* #define  PAGE_PROTECTION_VIOLATED_SIGNAL  SIGSEGV */
#endif

struct diagnostic
{
  int           (*test)(void);      /* pointer to some test function returning int */
  int           expectedStatus;     /* expected return value/status */
  const char  * explanation;        /* explanation of that test */
};


#if !defined(WIN32) || defined(__CYGWIN__)
static sigjmp_buf  env;
#else
static jmp_buf  env;
#endif

/*
 * There is still too little standardization of the arguments and return
 * type of signal handler functions.
 */
static void
segmentationFaultHandler(
  int signalNumber
#if ( defined(_AIX) )
  , ...
#endif
)
{
  (void)signalNumber;
#if !defined(WIN32) || defined(__CYGWIN__)
  siglongjmp(env, 1);
#else
  longjmp(env, 1);
#endif
}


static int
gotSegmentationFault(int (*test)(void))
{
#if !defined(WIN32) || defined(__CYGWIN__)
  sigset_t newmask, oldmask;
  int savemask;
#endif
#ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
  void (*oldhandler)(int)         = SIG_ERR;
#else
  void (*oldSIGSEGVhandler)(int)  = SIG_ERR;

#if !defined(WIN32) || defined(__CYGWIN__)
  void (*oldSIGBUShandler)(int)   = SIG_ERR;
#endif
#endif
  int status;


#if !defined(WIN32) || defined(__CYGWIN__)
  if ( 0 == sigsetjmp(env, savemask) )
#else
  if ( 0 == setjmp(env) )
#endif
  {
#if !defined(WIN32) || defined(__CYGWIN__)
	/* unblock signal and save previous signal mask */
	sigemptyset(&newmask);
  #ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
	sigaddset(&newmask, PAGE_PROTECTION_VIOLATED_SIGNAL);
  #else
	sigaddset(&newmask, SIGSEGV);
	sigaddset(&newmask, SIGBUS);
  #endif
	sigprocmask(SIG_UNBLOCK, &newmask, &oldmask);
#endif

#ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
	oldhandler = signal(PAGE_PROTECTION_VIOLATED_SIGNAL, segmentationFaultHandler);
#else
	oldSIGSEGVhandler = signal(SIGSEGV, segmentationFaultHandler);
  #if !defined(WIN32) || defined(__CYGWIN__)
	oldSIGBUShandler  = signal(SIGBUS, segmentationFaultHandler);
  #endif
#endif

	status = (*test)();
  }
  else
	status = 1;

  /* install previous signal handler */

#ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
  if (SIG_ERR != oldhandler)
	signal(PAGE_PROTECTION_VIOLATED_SIGNAL, oldhandler);
#else
  if (SIG_ERR != oldSIGSEGVhandler)
	signal(SIGSEGV, oldSIGSEGVhandler);
#if !defined(WIN32) || defined(__CYGWIN__)
  if (SIG_ERR != oldSIGBUShandler)
	signal(SIGBUS, oldSIGBUShandler);
#endif
#endif

#if !defined(WIN32) || defined(__CYGWIN__)
  /* restore signal mask */
  sigprocmask(SIG_SETMASK, &oldmask, NULL);
#endif
  return status;
}


static char *  allocation = (char*)0;
/* c is global so that assignments to it won't be optimized out. */
char  c;

static int
testSizes(void)
{
  /*
   * If DUMA_ADDR can't hold all of the bits of a void *,
   * have the user call createconf.
   */
  size_t sd = sizeof(DUMA_ADDR);
  size_t sv = sizeof(void *);
  return ( sd < sv );
}

static int
allocateMemory(void)
{
  allocation = (char *)malloc(1);

  if ( allocation != (char*)0 )
	return 0;
  else
	return 1;
}

static int
freeMemory(void)
{
  free(allocation);
  allocation = (char*)0;
  return 0;
}

static int
protectBelow(void)
{
  DUMA_SET_PROTECT_BELOW(1);
  return 0;
}

static int
protectAbove(void)
{
  DUMA_SET_PROTECT_BELOW(0);
  return 0;
}

static int
read0(void)
{
  c = *allocation;
  return 0;
}

static int
write0(void)
{
  *allocation = 1;
  return 0;
}

static int
read1(void)
{
  c = allocation[1];
  return 0;
}

static int
readMinus1(void)
{
  c = allocation[-1];
  return 0;
}


static struct diagnostic diagnostics[] =
{
  {
	testSizes, 0,      "Please add -DLONG_LONG to the compiler flags and recompile."
  },
#if 1
  {
	protectAbove, 0,   "Protect above: This sets DUMA to protect\n"
					   "the upper boundary of a malloc buffer, rather than the lower boundary."
  },
  {
	allocateMemory, 0, "Allocation 1: This test allocates a single byte of memory."
  },
  {
	read0, 0,          "Read valid memory 1: This test reads the allocated memory."
  },
  {
	write0, 0,         "Write valid memory 1: This test writes the allocated memory."
  },
  {
	readMinus1, 0,     "Read underrun: This test reads before the beginning of the buffer."
  },
  {
	read1, 1,          "Read overrun: This test reads beyond the end of the buffer."
  },
  {
	freeMemory, 0,     "Free memory 1: This test frees the allocated memory."
  },
#endif
#if 1
  {
	protectBelow, 0,   "Protect below: This sets DUMA to protect\n"
					   "the lower boundary of a malloc buffer, rather than the upper boundary."
  },
  {
	allocateMemory, 0, "Allocation 2: This allocates memory with the lower boundary protected."
  },
  {
	read0, 0,          "Read valid memory 2: This test reads the allocated memory."
  },
  {
	write0, 0,         "Write valid memory 2: This test writes the allocated memory."
  },
  {
	readMinus1, 1,     "Read underrun: This test reads before the beginning of the buffer."
  },
  {
	freeMemory, 0,     "Free memory 2: This test frees the allocated memory."
  },
#endif
  {
	0, 0, 0
  }
};


static const char  failedTest[] = "DUMA confidence test failed.\n";
static const char  newline = '\n';


int
main(int argc, char * * argv)
{
  static const struct diagnostic *  diag = diagnostics;
  int testno;

  (void)argc;
  (void)argv;

#ifdef DUMA_EXPLICIT_INIT
  duma_init();
#endif

  DUMA_SET_PROTECT_BELOW(0);
  DUMA_SET_ALIGNMENT(DUMA_MIN_ALIGNMENT);

  allocation = 0;

  for (testno=0; diag->explanation != 0; ++testno, ++diag)
  {
	int status;

#if 0
	write(0, diag->explanation, strlen(diag->explanation));
	write(0, &newline, 1);
#endif

	status = gotSegmentationFault(diag->test);

	if ( status != diag->expectedStatus )
	{
	  /*
	   * Don't use stdio to print here, because stdio
	   * uses malloc() and we've just proven that malloc()
	   * is broken. Also, use _exit() instead of exit(),
	   * because _exit() doesn't flush stdio.
	   */
	  write(2, failedTest, sizeof(failedTest) - 1);
	  write(2, diag->explanation, strlen(diag->explanation));
	  write(2, &newline, 1);
	  _exit(-1);
	}
  }

  /* avoid memory leak */
  if (allocation)
	freeMemory();

#if 0
  {
	char * dynmemA;
	char * dynmemB;

	/* test for DUMA_CHECK_FREQ */
	printf("0\n");
	protectAbove();
	printf("1\n");
	dynmemA = (char*)malloc( 10 * sizeof(char) );
	printf("2\n");
	dynmemA[-1 ] = 0;
	printf("3\n");
	dynmemB = (char*)malloc( 11 * sizeof(char) );
	printf("4\n");
	free( dynmemB );
	printf("5\n");
	free( dynmemA );
	printf("6\n");
  }
#endif

  return 0;

}