2 # Copyright (c) 2005, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 # Author: Maxim Lifantsev
34 # Run the heap checker unittest in a mode where it is supposed to crash and
35 # return an error if it doesn't.
37 # We expect BINDIR to be set in the environment.
38 # If not, we set it to some reasonable value.
41 if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
42 echo "USAGE: $0 [unittest dir]"
43 echo " By default, unittest_dir=$BINDIR"
47 EXE
="${1:-$BINDIR}/heap-checker_unittest"
48 TMPDIR
="/tmp/heap_check_death_info"
51 # You need perl to run pprof, so I assume it's installed
53 $timeout=$ARGV[0]; shift;
54 $retval = 255; # the default retval, for the case where we timed out
55 eval { # need to run in an eval-block to trigger during system()
56 local $SIG{ALRM} = sub { die "alarm\n" }; # \n is required!
58 $retval = system(@ARGV);
59 # Make retval bash-style: exit status, or 128+n if terminated by signal n
60 $retval = ($retval & 127) ? (128 + $retval) : ($retval >> 8);
63 exit $retval; # return system()-retval, or 255 if system() never returned
67 # $1: timeout for alarm;
68 # $2: regexp of expected exit code(s);
69 # $3: regexp to match a line in the output;
70 # $4: regexp to not match a line in the output;
71 # $5+ args to pass to $EXE
73 # Note: make sure these varnames don't conflict with any vars outside Test()!
80 unexpected_regexp
="$1"
83 echo -n "Testing $EXE with $@ ... "
84 output
="$TMPDIR/output"
85 ALARM
$timeout env
"$@" $EXE > "$output" 2>&1
87 ec_ok
=`expr "$actual_ec" : "$expected_ec$" >/dev/null || echo false`
88 matches_ok
=`test -z "$expected_regexp" || \
89 grep "$expected_regexp" "$output" >/dev/null 2>&1 || echo false`
90 negmatches_ok
=`test -z "$unexpected_regexp" || \
91 ! grep "$unexpected_regexp" "$output" >/dev/null 2>&1 || echo false`
92 if $ec_ok && $matches_ok && $negmatches_ok; then
96 # If we get here, we failed. Now we just need to report why
98 if [ $actual_ec -eq 255 ]; then # 255 == SIGTERM due to $ALARM
99 echo "Test was taking unexpectedly long time to run and so we aborted it."
100 echo "Try the test case manually or raise the timeout from $timeout"
101 echo "to distinguish test slowness from a real problem."
104 echo "Wrong exit code: expected: '$expected_ec'; actual: $actual_ec"
106 echo "Output did not match '$expected_regexp'"
108 echo "Output unexpectedly matched '$unexpected_regexp'"
110 echo "Output from failed run:"
114 return 1 # 1: failure
117 TMPDIR
=/tmp
/heap_check_death_info
118 rm -rf $TMPDIR ||
exit 1
119 mkdir
$TMPDIR ||
exit 2
121 export HEAPCHECK
=strict
# default mode
123 # These invocations should pass (0 == PASS):
125 # This tests that turning leak-checker off dynamically works fine
126 Test
120 0 "^PASS$" "" HEAPCHECK
="" ||
exit 1
128 # This disables threads so we can cause leaks reliably and test finding them
129 Test
120 0 "^PASS$" "" HEAP_CHECKER_TEST_NO_THREADS
=1 ||
exit 2
131 # Test that --test_cancel_global_check works
132 Test
20 0 "Canceling .* whole-program .* leak check$" "" \
133 HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECKER_TEST_TEST_CANCEL_GLOBAL_CHECK
=1 ||
exit 3
134 Test
20 0 "Canceling .* whole-program .* leak check$" "" \
135 HEAP_CHECKER_TEST_TEST_LOOP_LEAK
=1 HEAP_CHECKER_TEST_TEST_CANCEL_GLOBAL_CHECK
=1 ||
exit 4
137 # Test that very early log messages are present and controllable:
138 EARLY_MSG
="Starting tracking the heap$"
140 Test
60 0 "$EARLY_MSG" "" \
141 HEAPCHECK
="" HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECKER_TEST_NO_THREADS
=1 \
142 PERFTOOLS_VERBOSE
=10 ||
exit 5
143 Test
60 0 "MemoryRegionMap Init$" "" \
144 HEAPCHECK
="" HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECKER_TEST_NO_THREADS
=1 \
145 PERFTOOLS_VERBOSE
=11 ||
exit 6
146 Test
60 0 "" "$EARLY_MSG" \
147 HEAPCHECK
="" HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECKER_TEST_NO_THREADS
=1 \
148 PERFTOOLS_VERBOSE
=-11 ||
exit 7
150 # These invocations should fail with very high probability,
151 # rather than return 0 or hang (1 == exit(1), 134 == abort(), 139 = SIGSEGV):
153 Test
60 1 "Exiting .* because of .* leaks$" "" \
154 HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECKER_TEST_NO_THREADS
=1 ||
exit 8
155 Test
60 1 "Exiting .* because of .* leaks$" "" \
156 HEAP_CHECKER_TEST_TEST_LOOP_LEAK
=1 HEAP_CHECKER_TEST_NO_THREADS
=1 ||
exit 9
158 # Test that we produce a reasonable textual leak report.
159 Test
60 1 "MakeALeak" "" \
160 HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECK_TEST_NO_THREADS
=1 \
163 # Test that very early log messages are present and controllable:
164 Test
60 1 "Starting tracking the heap$" "" \
165 HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECKER_TEST_NO_THREADS
=1 PERFTOOLS_VERBOSE
=10 \
167 Test
60 1 "" "Starting tracking the heap" \
168 HEAP_CHECKER_TEST_TEST_LEAK
=1 HEAP_CHECKER_TEST_NO_THREADS
=1 PERFTOOLS_VERBOSE
=-10 \
171 cd / # so we're not in TMPDIR when we delete it