dumatest.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * DUMA - Red-Zone memory allocator.
  3. * Copyright (C) 2002-2008 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
  4. * Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
  5. * License: GNU GPL (GNU General Public License, see COPYING-GPL)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. *
  22. * FILE CONTENTS:
  23. * DUMA confidence tests.
  24. * Make sure all of the various functions of DUMA work correctly.
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #if !defined(WIN32) || defined(__CYGWIN__)
  30. #include <unistd.h>
  31. #else
  32. #include <io.h>
  33. #endif
  34. #include <setjmp.h>
  35. #include <signal.h>
  36. #include "../duma.h"
  37. #ifndef PAGE_PROTECTION_VIOLATED_SIGNAL
  38. /* changed default in code below to use two signals: SIGSEGV and SIGBUS */
  39. /* #define PAGE_PROTECTION_VIOLATED_SIGNAL SIGSEGV */
  40. #endif
  41. struct diagnostic
  42. {
  43. int (*test)(void); /* pointer to some test function returning int */
  44. int expectedStatus; /* expected return value/status */
  45. const char * explanation; /* explanation of that test */
  46. };
  47. #if !defined(WIN32) || defined(__CYGWIN__)
  48. static sigjmp_buf env;
  49. #else
  50. static jmp_buf env;
  51. #endif
  52. /*
  53. * There is still too little standardization of the arguments and return
  54. * type of signal handler functions.
  55. */
  56. static void
  57. segmentationFaultHandler(
  58. int signalNumber
  59. #if ( defined(_AIX) )
  60. , ...
  61. #endif
  62. )
  63. {
  64. (void)signalNumber;
  65. #if !defined(WIN32) || defined(__CYGWIN__)
  66. siglongjmp(env, 1);
  67. #else
  68. longjmp(env, 1);
  69. #endif
  70. }
  71. static int
  72. gotSegmentationFault(int (*test)(void))
  73. {
  74. #if !defined(WIN32) || defined(__CYGWIN__)
  75. sigset_t newmask, oldmask;
  76. int savemask;
  77. #endif
  78. #ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
  79. void (*oldhandler)(int) = SIG_ERR;
  80. #else
  81. void (*oldSIGSEGVhandler)(int) = SIG_ERR;
  82. #if !defined(WIN32) || defined(__CYGWIN__)
  83. void (*oldSIGBUShandler)(int) = SIG_ERR;
  84. #endif
  85. #endif
  86. int status;
  87. #if !defined(WIN32) || defined(__CYGWIN__)
  88. if ( 0 == sigsetjmp(env, savemask) )
  89. #else
  90. if ( 0 == setjmp(env) )
  91. #endif
  92. {
  93. #if !defined(WIN32) || defined(__CYGWIN__)
  94. /* unblock signal and save previous signal mask */
  95. sigemptyset(&newmask);
  96. #ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
  97. sigaddset(&newmask, PAGE_PROTECTION_VIOLATED_SIGNAL);
  98. #else
  99. sigaddset(&newmask, SIGSEGV);
  100. sigaddset(&newmask, SIGBUS);
  101. #endif
  102. sigprocmask(SIG_UNBLOCK, &newmask, &oldmask);
  103. #endif
  104. #ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
  105. oldhandler = signal(PAGE_PROTECTION_VIOLATED_SIGNAL, segmentationFaultHandler);
  106. #else
  107. oldSIGSEGVhandler = signal(SIGSEGV, segmentationFaultHandler);
  108. #if !defined(WIN32) || defined(__CYGWIN__)
  109. oldSIGBUShandler = signal(SIGBUS, segmentationFaultHandler);
  110. #endif
  111. #endif
  112. status = (*test)();
  113. }
  114. else
  115. status = 1;
  116. /* install previous signal handler */
  117. #ifdef PAGE_PROTECTION_VIOLATED_SIGNAL
  118. if (SIG_ERR != oldhandler)
  119. signal(PAGE_PROTECTION_VIOLATED_SIGNAL, oldhandler);
  120. #else
  121. if (SIG_ERR != oldSIGSEGVhandler)
  122. signal(SIGSEGV, oldSIGSEGVhandler);
  123. #if !defined(WIN32) || defined(__CYGWIN__)
  124. if (SIG_ERR != oldSIGBUShandler)
  125. signal(SIGBUS, oldSIGBUShandler);
  126. #endif
  127. #endif
  128. #if !defined(WIN32) || defined(__CYGWIN__)
  129. /* restore signal mask */
  130. sigprocmask(SIG_SETMASK, &oldmask, NULL);
  131. #endif
  132. return status;
  133. }
  134. static char * allocation = (char*)0;
  135. /* c is global so that assignments to it won't be optimized out. */
  136. char c;
  137. static int
  138. testSizes(void)
  139. {
  140. /*
  141. * If DUMA_ADDR can't hold all of the bits of a void *,
  142. * have the user call createconf.
  143. */
  144. size_t sd = sizeof(DUMA_ADDR);
  145. size_t sv = sizeof(void *);
  146. return ( sd < sv );
  147. }
  148. static int
  149. allocateMemory(void)
  150. {
  151. allocation = (char *)malloc(1);
  152. if ( allocation != (char*)0 )
  153. return 0;
  154. else
  155. return 1;
  156. }
  157. static int
  158. freeMemory(void)
  159. {
  160. free(allocation);
  161. allocation = (char*)0;
  162. return 0;
  163. }
  164. static int
  165. protectBelow(void)
  166. {
  167. DUMA_SET_PROTECT_BELOW(1);
  168. return 0;
  169. }
  170. static int
  171. protectAbove(void)
  172. {
  173. DUMA_SET_PROTECT_BELOW(0);
  174. return 0;
  175. }
  176. static int
  177. read0(void)
  178. {
  179. c = *allocation;
  180. return 0;
  181. }
  182. static int
  183. write0(void)
  184. {
  185. *allocation = 1;
  186. return 0;
  187. }
  188. static int
  189. read1(void)
  190. {
  191. c = allocation[1];
  192. return 0;
  193. }
  194. static int
  195. readMinus1(void)
  196. {
  197. c = allocation[-1];
  198. return 0;
  199. }
  200. static struct diagnostic diagnostics[] =
  201. {
  202. {
  203. testSizes, 0, "Please add -DLONG_LONG to the compiler flags and recompile."
  204. },
  205. #if 1
  206. {
  207. protectAbove, 0, "Protect above: This sets DUMA to protect\n"
  208. "the upper boundary of a malloc buffer, rather than the lower boundary."
  209. },
  210. {
  211. allocateMemory, 0, "Allocation 1: This test allocates a single byte of memory."
  212. },
  213. {
  214. read0, 0, "Read valid memory 1: This test reads the allocated memory."
  215. },
  216. {
  217. write0, 0, "Write valid memory 1: This test writes the allocated memory."
  218. },
  219. {
  220. readMinus1, 0, "Read underrun: This test reads before the beginning of the buffer."
  221. },
  222. {
  223. read1, 1, "Read overrun: This test reads beyond the end of the buffer."
  224. },
  225. {
  226. freeMemory, 0, "Free memory 1: This test frees the allocated memory."
  227. },
  228. #endif
  229. #if 1
  230. {
  231. protectBelow, 0, "Protect below: This sets DUMA to protect\n"
  232. "the lower boundary of a malloc buffer, rather than the upper boundary."
  233. },
  234. {
  235. allocateMemory, 0, "Allocation 2: This allocates memory with the lower boundary protected."
  236. },
  237. {
  238. read0, 0, "Read valid memory 2: This test reads the allocated memory."
  239. },
  240. {
  241. write0, 0, "Write valid memory 2: This test writes the allocated memory."
  242. },
  243. {
  244. readMinus1, 1, "Read underrun: This test reads before the beginning of the buffer."
  245. },
  246. {
  247. freeMemory, 0, "Free memory 2: This test frees the allocated memory."
  248. },
  249. #endif
  250. {
  251. 0, 0, 0
  252. }
  253. };
  254. static const char failedTest[] = "DUMA confidence test failed.\n";
  255. static const char newline = '\n';
  256. int
  257. main(int argc, char * * argv)
  258. {
  259. static const struct diagnostic * diag = diagnostics;
  260. int testno;
  261. (void)argc;
  262. (void)argv;
  263. #ifdef DUMA_EXPLICIT_INIT
  264. duma_init();
  265. #endif
  266. DUMA_SET_PROTECT_BELOW(0);
  267. DUMA_SET_ALIGNMENT(DUMA_MIN_ALIGNMENT);
  268. allocation = 0;
  269. for (testno=0; diag->explanation != 0; ++testno, ++diag)
  270. {
  271. int status;
  272. #if 0
  273. write(0, diag->explanation, strlen(diag->explanation));
  274. write(0, &newline, 1);
  275. #endif
  276. status = gotSegmentationFault(diag->test);
  277. if ( status != diag->expectedStatus )
  278. {
  279. /*
  280. * Don't use stdio to print here, because stdio
  281. * uses malloc() and we've just proven that malloc()
  282. * is broken. Also, use _exit() instead of exit(),
  283. * because _exit() doesn't flush stdio.
  284. */
  285. write(2, failedTest, sizeof(failedTest) - 1);
  286. write(2, diag->explanation, strlen(diag->explanation));
  287. write(2, &newline, 1);
  288. _exit(-1);
  289. }
  290. }
  291. /* avoid memory leak */
  292. if (allocation)
  293. freeMemory();
  294. #if 0
  295. {
  296. char * dynmemA;
  297. char * dynmemB;
  298. /* test for DUMA_CHECK_FREQ */
  299. printf("0\n");
  300. protectAbove();
  301. printf("1\n");
  302. dynmemA = (char*)malloc( 10 * sizeof(char) );
  303. printf("2\n");
  304. dynmemA[-1 ] = 0;
  305. printf("3\n");
  306. dynmemB = (char*)malloc( 11 * sizeof(char) );
  307. printf("4\n");
  308. free( dynmemB );
  309. printf("5\n");
  310. free( dynmemA );
  311. printf("6\n");
  312. }
  313. #endif
  314. return 0;
  315. }