12 * Electric Fence confidence tests.
13 * Make sure all of the various functions of Electric Fence work correctly.
16 #ifndef PAGE_PROTECTION_VIOLATED_SIGNAL
17 #define PAGE_PROTECTION_VIOLATED_SIGNAL SIGSEGV
23 const char * explanation
;
26 extern int EF_PROTECT_BELOW
;
27 extern int EF_ALIGNMENT
;
32 * There is still too little standardization of the arguments and return
33 * type of signal handler functions.
37 segmentationFaultHandler(
44 signal(PAGE_PROTECTION_VIOLATED_SIGNAL
, SIG_DFL
);
49 gotSegmentationFault(int (*test
)(void))
51 if ( setjmp(env
) == 0 ) {
54 signal(PAGE_PROTECTION_VIOLATED_SIGNAL
55 ,segmentationFaultHandler
);
57 signal(PAGE_PROTECTION_VIOLATED_SIGNAL
, SIG_DFL
);
64 static char * allocation
;
65 /* c is global so that assignments to it won't be optimized out. */
72 * If ef_number can't hold all of the bits of a void *, have the user
73 * add -DUSE_ LONG_LONG to the compiler flags so that ef_number will be
74 * declared as "unsigned long long" instead of "unsigned long".
76 return ( sizeof(ef_number
) < sizeof(void *) );
82 allocation
= (char *)malloc(1);
84 if ( allocation
!= 0 )
100 EF_PROTECT_BELOW
= 1;
135 static struct diagnostic diagnostics
[] = {
138 "Please add -DLONG_LONG to the compiler flags and recompile."
142 "Allocation 1: This test allocates a single byte of memory."
146 "Read valid memory 1: This test reads the allocated memory."
150 "Write valid memory 1: This test writes the allocated memory."
154 "Read overrun: This test reads beyond the end of the buffer."
158 "Free memory: This test frees the allocated memory."
162 "Protect below: This sets Electric Fence to protect\n"
163 "the lower boundary of a malloc buffer, rather than the\n"
168 "Allocation 2: This allocates memory with the lower boundary"
173 "Read valid memory 2: This test reads the allocated memory."
177 "Write valid memory 2: This test writes the allocated memory."
181 "Read underrun: This test reads before the beginning of the"
189 static const char failedTest
[]
190 = "Electric Fence confidence test failed.\n";
192 static const char newline
= '\n';
195 main(int argc
, char * * argv
)
197 static const struct diagnostic
* diag
= diagnostics
;
200 EF_PROTECT_BELOW
= 0;
203 while ( diag
->explanation
!= 0 ) {
204 int status
= gotSegmentationFault(diag
->test
);
206 if ( status
!= diag
->expectedStatus
) {
208 * Don't use stdio to print here, because stdio
209 * uses malloc() and we've just proven that malloc()
210 * is broken. Also, use _exit() instead of exit(),
211 * because _exit() doesn't flush stdio.
213 write(2, failedTest
, sizeof(failedTest
) - 1);
214 write(2, diag
->explanation
, strlen(diag
->explanation
));
215 write(2, &newline
, 1);