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/
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 solve in more
11 # cases with more judicious use of log calls with EXIF_LOG_CODE_NO_MEMORY
16 if [ "$1" = "-v" ] ; then
20 if [ x"$FAILMALLOC_PATH" = x ]; then
21 echo libfailmalloc is not available
27 if [ -e .libs/lt-test-value ]; then
28 # If libtool is in use, the normal "binary" is actually a shell script which
29 # would be interfered with by libfailmalloc. Instead, use the special lt-
30 # binary which should work properly.
31 BINARY_PREFIX=".libs/lt-"
34 # Usage: failmalloc_binary_test #iterations binary <optional arguments>
35 # FIXME: auto-determine #iterations by comparing the output of each run
36 # with the output of a normal run, and exiting when that happens.
37 failmalloc_binary_test () {
38 binary="$BINARY_PREFIX$2"
42 echo Checking "$binary" for "$iterations" iterations
43 for n in $(seq "$iterations"); do
44 test "$VERBOSE" = 1 && { echo "$n"; set -x; }
45 FAILMALLOC_INTERVAL="$n" LD_PRELOAD="$FAILMALLOC_PATH" "$binary" "$@" >/dev/null
47 test "$VERBOSE" = 1 && set +x;
48 if test "$s" -ge 128; then
49 # Such status codes only happen due to termination due to a signal
51 echo "Abnormal binary exit status $s at malloc #$n on $binary"
58 # The number of iterations is determined empirically to be about twice as
59 # high as the maximum number of mallocs performed by the test program in order
60 # to avoid lowering code coverage in the case of future code changes that cause
63 failmalloc_binary_test 500 test-value
64 failmalloc_binary_test 300 test-mem
65 for f in $SRCDIR/testdata/*jpg; do
66 echo "Testing `basename "$f"`"
67 failmalloc_binary_test 500 test-parse "$f"
68 # N.B., test-parse --swap-byte-order doesn't test any new paths