1#!/bin/sh 2# Use Failmalloc to test behaviour in the face of out-of-memory conditions. 3# The test runs a binary multiple times while configuring Failmalloc to fail a 4# different malloc() call each time, while looking for abnormal program exits 5# due to segfaults. See https://www.nongnu.org/failmalloc/ 6# 7# Ideally, it would ensure that the test binary returns an error code on each 8# failure, but this often doesn't happen. This is a problem that should be 9# rectified, but the API doesn't allow returning an error code in many 10# functions that could encounter a problem. The issue could be solved in more 11# cases with more judicious use of log calls with EXIF_LOG_CODE_NO_MEMORY 12# codes. 13 14VERBOSE= 15if [ "$1" = "-v" ] ; then 16 VERBOSE=1 17fi 18 19if [ x"$FAILMALLOC_PATH" = x ]; then 20 echo "libfailmalloc is not available" 21 exit 77 22fi 23 24BINARY_PREFIX=./ 25if [ -e .libs/lt-test-value ]; then 26 # If libtool is in use, the normal "binary" is actually a shell script which 27 # would be interfered with by libfailmalloc. Instead, use the special lt- 28 # binary which should work properly. 29 BINARY_PREFIX=".libs/lt-" 30fi 31 32# Usage: failmalloc_binary_test #iterations binary <optional arguments> 33# FIXME: auto-determine #iterations by comparing the output of each run 34# with the output of a normal run, and exiting when that happens. 35failmalloc_binary_test () { 36 binary="$BINARY_PREFIX$2" 37 iterations="$1" 38 shift 39 shift 40 echo Checking "$binary" for "$iterations" iterations 41 for n in $(seq "$iterations"); do 42 test "$VERBOSE" = 1 && { echo "$n"; set -x; } 43 FAILMALLOC_INTERVAL="$n" LD_PRELOAD="$FAILMALLOC_PATH" "$binary" "$@" >/dev/null 44 s=$? 45 test "$VERBOSE" = 1 && set +x; 46 if test "$s" -ge 128; then 47 # Such status codes only happen due to termination due to a signal 48 # like SIGSEGV. 49 echo "Abnormal binary exit status $s at malloc #$n on $binary" 50 echo FAILURE 51 exit 1 52 fi 53 done 54} 55 56# The number of iterations is determined empirically to be about twice as 57# high as the maximum number of mallocs performed by the test program in order 58# to avoid lowering code coverage in the case of future code changes that cause 59# more allocations. 60 61failmalloc_binary_test 500 test-value 62failmalloc_binary_test 300 test-mem 63for f in ${srcdir}/testdata/*jpg; do 64 echo "Testing `basename "$f"`" 65 failmalloc_binary_test 500 test-parse$EXEEXT "$f" 66 # N.B., test-parse$EXEEXT --swap-byte-order doesn't test any new paths 67done 68 69echo PASSED 70