unit_tests/valgrind_tests.sh
3dcc2d78
 #!/bin/sh
b8edbb88
 # 
 # We don't look for 'still reachable' blocks, since clamd fork()s after loading
 # the DB. The parent exits without freeing the memory (but they are freed
 # anyway due to the exit).
 # To test for DB load leaks, we issue a RELOAD command, which should cause
 # leaks to be reported by valgrind if there are any.
 # 
 
7d081def
 test "x$VG" = "x1" || { echo "*** valgrind tests skipped by default, use 'make check VG=1' to activate"; exit 77; }
3dcc2d78
 VALGRIND=`which ${VALGRIND-valgrind}`
 test -n "$VALGRIND" || { echo "*** valgrind not found, skipping test"; exit 77; }
 test -x "$VALGRIND" || { echo "*** valgrind not executable, skipping test"; exit 77; }
 
b8edbb88
 parse_valgrindlog()
 {
 	if test ! -f $1; then
e9e40c70
 		echo "*** Logfile $1 not found. Valgrind failed to run?"
b8edbb88
 	fi
3c5f686d
 	NRUNS=`grep -a "ERROR SUMMARY" $1 | wc -l`
 	if test $NRUNS -eq `grep -a "ERROR SUMMARY: 0 errors" $1 | wc -l` && test `grep -a "FATAL:" $1|wc -l ` -eq 0; then
b8edbb88
 		if test "$1" = "valgrind-race.log" || 
 			test $NRUNS -eq `grep "no leaks are possible" $1 | wc -l` ||
aa12dc2c
 			test `grep "lost:" $1 | grep -v " 0 bytes" | wc -l` -eq 0; then 
0ef9c881
 			if test -z "$GENSUPP"; then
3c5f686d
 			        cp $1 /tmp
0ef9c881
 				rm -f $1;
 			fi
b8edbb88
 			return
 		else
 			echo "*** Valgrind test FAILED, memory LEAKS detected ***"
aa12dc2c
 			grep "lost:" $1 | grep -v " 0 bytes"
b8edbb88
 		fi
 	else
 		if test "$1" = "valgrind-race.log" ; then
 			echo "*** Valgrind test FAILED, DATA RACES detected ****"
 		else
 			echo "*** Valgrind test FAILED, memory ERRORS detected ****"
 		fi
 		grep "ERROR SUMMARY" $1 | grep -v "0 errors"
 		sed -rn '
 			/^[=0-9]+ +at/ {
 				# save current line in hold buffer
 				x
 				# print hold buffer
 				p
1642ffba
 				# get original line back and print
b8edbb88
 				x
1642ffba
 				p
b8edbb88
 			}
 			# store it in hold buffer
 			h
1642ffba
 		' <$1 | grep -Ev "Thread.+was created" | grep -v "Open"
3dcc2d78
 	fi
b8edbb88
 	echo "***"
964a1e73
 	echo "*** Please submit $1 to https://bugzilla.clamav.net"
b8edbb88
 	echo "***"
 }
 
0ef9c881
 if test "X$VALGRIND_GENSUPP" = "X1"; then
 	GENSUPP="--gen-suppressions=all"
 else
 	GENSUPP=
 fi
 
3c5f686d
 VALGRIND_COMMON_FLAGS="-v --trace-children=yes --suppressions=$abs_srcdir/valgrind.supp --error-exitcode=123 $GENSUPP"
0ef9c881
 VALGRIND_FLAGS="$VALGRIND_COMMON_FLAGS --track-fds=yes --leak-check=full"
 VALGRIND_FLAGS_RACE="$VALGRIND_COMMON_FLAGS --tool=helgrind"
f011ae3e
 export CK_DEFAULT_TIMEOUT=40
e9e40c70
 echo "--- Starting check_clamav under valgrind/memcheck"
b8edbb88
 rm -f valgrind-check.log valgrind-clamd.log valgrind-race.log
3c5f686d
 CK_FORK=no ../libtool --mode=execute $VALGRIND $VALGRIND_FLAGS --log-file=valgrind-check.log ./check_clamav &
f45111e5
 pid1=$!
 
e9e40c70
 echo "--- Starting clamd under valgrind/memcheck"
3c5f686d
 CLAMD_WRAPPER="$VALGRIND $VALGRIND_FLAGS --log-file=`pwd`/valgrind-clamd.log" $abs_srcdir/check_clamd.sh &
f45111e5
 pid2=$!
b8edbb88
 
e9e40c70
 echo "--- Starting clamd under valgrind/helgrind"
3c5f686d
 CLAMD_TEST_UNIQ1=3 CLAMD_TEST_UNIQ2=4 CLAMD_WRAPPER="$VALGRIND $VALGRIND_FLAGS_RACE --log-file=`pwd`/valgrind-race.log" $abs_srcdir/check_clamd.sh &
f45111e5
 pid3=$!
e9e40c70
 
3c5f686d
 errors=0
 wait $pid1
 if test $? -ne 0; then
     # don't rm logs
     GENSUPP=1
     errors=1
 fi
 wait $pid2
 if test $? -ne 0; then
     GENSUPP=1
     errors=1
 fi
 wait $pid3
 if test $? -ne 0; then
     GENSUPP=1
     errors=1
 fi
e9e40c70
 parse_valgrindlog valgrind-check.log
 parse_valgrindlog valgrind-clamd.log
b8edbb88
 parse_valgrindlog valgrind-race.log
 
eb0e3c98
 if test -f valgrind-check.log || test -f valgrind-race.log || test -f valgrind-clamd.log; then
b8edbb88
 	exit 1;
3dcc2d78
 fi
3c5f686d
 exit $errors