fix CVE-2019-9278
[platform/upstream/libexif.git] / test / check-failmalloc.sh
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 solve in more
11 # cases with more judicious use of log calls with EXIF_LOG_CODE_NO_MEMORY
12 # codes.
13 . ./check-vars.sh
14
15 VERBOSE=
16 if [ "$1" = "-v" ] ; then
17     VERBOSE=1
18 fi
19
20 if [ x"$FAILMALLOC_PATH" = x ]; then
21     echo libfailmalloc is not available
22     echo SKIPPING
23     exit
24 fi
25
26 BINARY_PREFIX=./
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-"
32 fi
33
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"
39   iterations="$1"
40   shift
41   shift
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
46       s=$?
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
50           # like SIGSEGV.
51           echo "Abnormal binary exit status $s at malloc #$n on $binary"
52           echo FAILURE
53           exit 1
54       fi
55   done
56 }
57
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
61 # more allocations.
62
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
69 done
70
71 echo PASSED