-
Notifications
You must be signed in to change notification settings - Fork 16
Reducing a segmentation fault
It might not be obvious how to correctly reduce a segmentation fault with DustMite:
- Grepping for "Segmentation fault" in the program's output will not work, because that message is printed by the shell, not the program.
- The exit code that needs to be tested varies depending on how the
program is run. This is due to how the exit status is reported by
the
wait
function.
Shells will set the exit status variable ($?
) to 139
when the last
program exited due to a segmentation fault, so your script will need
to test against that. Note that when using dmd -run
, the exit status
will be 1, because dmd
does not use exec
. To illustrate:
$ echo 'void main() { *cast(int*)(null) = 0; }' > test.d
$ dmd test.d && ./test ; echo $?
Segmentation fault (core dumped)
139
$ rdmd test.d ; echo $?
Segmentation fault (core dumped)
139
$ dmd -run test.d ; echo $?
Error: program killed by signal 11
1
Thus, an example script to reduce a segmentation fault would be:
#!/bin/bash
# Compile the program first; fail if the program fails to compile.
dmd program.d || exit 1
# Now run the compiled program.
./program
# Test exit code.
if [ $? -eq 139 ]; then
# Program exited due to segmentation fault;
# report success to DustMite.
exit 0
else
# Program ran successfully or exited due to another error;
# report failure to DustMite.
exit 1
fi
Note that for a non-trivial program, it is very likely that DustMite will reduce the program to one which segfaults due to a different reason (such as by reducing away the initialization of a reference-type variable). See Detecting a specific segfault for more information, or Detecting a segfault in dmd itself for reducing segfaults in DMD.