home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume27 / efence / part01 / eftest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-01  |  3.4 KB  |  205 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <setjmp.h>
  6. #include <signal.h>
  7. #include "efence.h"
  8.  
  9. /*
  10.  * Electric Fence confidence tests.
  11.  * Make sure all of the various functions of Electric Fence work correctly.
  12.  */
  13.  
  14. #ifndef    PAGE_PROTECTION_VIOLATED_SIGNAL
  15. #define    PAGE_PROTECTION_VIOLATED_SIGNAL    SIGSEGV
  16. #endif
  17.  
  18. struct diagnostic {
  19.     int        (*test)(void);
  20.     int        expectedStatus;
  21.     const char *    explanation;
  22. };
  23.  
  24. extern int    EF_PROTECT_BELOW;
  25. extern int    EF_ALIGNMENT;
  26.  
  27. static jmp_buf    env;
  28.  
  29. /*
  30.  * There is still too little standardization of the arguments and return
  31.  * type of signal handler functions.
  32.  */
  33. static
  34. void
  35. segmentationFaultHandler(
  36. int signalNumber
  37. #if ( defined(_AIX) )
  38. , ...
  39. #endif
  40. )
  41.  {
  42.     signal(PAGE_PROTECTION_VIOLATED_SIGNAL, SIG_DFL);
  43.     longjmp(env, 1);
  44. }
  45.  
  46. static int
  47. gotSegmentationFault(int (*test)(void))
  48. {
  49.     if ( setjmp(env) == 0 ) {
  50.         int            status;
  51.  
  52.         signal(PAGE_PROTECTION_VIOLATED_SIGNAL
  53.         ,segmentationFaultHandler);
  54.         status = (*test)();
  55.         signal(PAGE_PROTECTION_VIOLATED_SIGNAL, SIG_DFL);
  56.         return status;
  57.     }
  58.     else
  59.         return 1;
  60. }
  61.  
  62. static char *    allocation;
  63. /* c is global so that assignments to it won't be optimized out. */
  64. char    c;
  65.  
  66. static int
  67. allocateMemory(void)
  68. {
  69.     allocation = (char *)malloc(1);
  70.  
  71.     if ( allocation != 0 )
  72.         return 0;
  73.     else
  74.         return 1;
  75. }
  76.  
  77. static int
  78. freeMemory(void)
  79. {
  80.     free(allocation);
  81.     return 0;
  82. }
  83.  
  84. static int
  85. protectBelow(void)
  86. {
  87.     EF_PROTECT_BELOW = 1;
  88.     return 0;
  89. }
  90.  
  91. static int
  92. read0(void)
  93. {
  94.     c = *allocation;
  95.  
  96.     return 0;
  97. }
  98.  
  99. static int
  100. write0(void)
  101. {
  102.     *allocation = 1;
  103.  
  104.     return 0;
  105. }
  106.  
  107. static int
  108. read1(void)
  109. {
  110.     c = allocation[1];
  111.  
  112.     return 0;
  113. }
  114.  
  115. static int
  116. readMinus1(void)
  117. {
  118.     c = allocation[-1];
  119.     return 0;
  120. }
  121.  
  122. static struct diagnostic diagnostics[] = {
  123.     {
  124.         allocateMemory, 0,
  125.         "Allocation 1: This test allocates a single byte of memory."
  126.     },
  127.     {
  128.         read0, 0,
  129.         "Read valid memory 1: This test reads the allocated memory."
  130.     },
  131.     {
  132.         write0, 0,
  133.         "Write valid memory 1: This test writes the allocated memory."
  134.     },
  135.     {
  136.         read1, 1,
  137.         "Read overrun: This test reads beyond the end of the buffer."
  138.     },
  139.     {
  140.         freeMemory, 0,
  141.         "Free memory: This test frees the allocated memory."
  142.     },
  143.     {
  144.         protectBelow, 0,
  145.         "Protect below: This sets Electric Fence to protect\n"
  146.         "the lower boundary of a malloc buffer, rather than the\n"
  147.         "upper boundary."
  148.     },
  149.     {
  150.         allocateMemory, 0,
  151.         "Allocation 2: This allocates memory with the lower boundary"
  152.         " protected."
  153.     },
  154.     {
  155.         read0, 0,
  156.         "Read valid memory 2: This test reads the allocated memory."
  157.     },
  158.     {
  159.         write0, 0,
  160.         "Write valid memory 2: This test writes the allocated memory."
  161.     },
  162.     {
  163.         readMinus1, 1,
  164.         "Read underrun: This test reads before the beginning of the"
  165.         " buffer."
  166.     },
  167.     {
  168.         0, 0, 0
  169.     }
  170. };
  171.  
  172. static const char    failedTest[]
  173.  = "Electric Fence confidence test failed.\n";
  174.  
  175. static const char    newline = '\n';
  176.  
  177. int
  178. main(int argc, char * * argv)
  179. {
  180.     static const struct diagnostic *    diag = diagnostics;
  181.     
  182.  
  183.     EF_PROTECT_BELOW = 0;
  184.     EF_ALIGNMENT = 0;
  185.  
  186.     while ( diag->explanation != 0 ) {
  187.         int    status = gotSegmentationFault(diag->test);
  188.  
  189.         if ( status != diag->expectedStatus ) {
  190.             /*
  191.              * Don't use stdio to print here, because stdio
  192.              * uses malloc() and we've just proven that malloc()
  193.              * is broken. Also, use _exit() instead of exit(),
  194.              * because _exit() doesn't flush stdio.
  195.              */
  196.             write(2, failedTest, sizeof(failedTest) - 1);
  197.             write(2, diag->explanation, strlen(diag->explanation));
  198.             write(2, &newline, 1);
  199.             _exit(-1);
  200.         }
  201.         diag++;
  202.     }
  203.     return 0;
  204. }
  205.