1#!/bin/sh 2# Copyright (c) 2005, Google Inc. 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 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 14# distribution. 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. 18# 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. 30 31# --- 32# Author: Maxim Lifantsev 33# 34# Run the heap checker unittest in a mode where it is supposed to crash and 35# return an error if it doesn't. 36 37# We expect BINDIR to be set in the environment. 38# If not, we set it to some reasonable value. 39BINDIR="${BINDIR:-.}" 40 41if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then 42 echo "USAGE: $0 [unittest dir]" 43 echo " By default, unittest_dir=$BINDIR" 44 exit 1 45fi 46 47EXE="${1:-$BINDIR}/heap-checker_unittest" 48TMPDIR="/tmp/heap_check_death_info" 49 50ALARM() { 51 # You need perl to run pprof, so I assume it's installed 52 perl -e ' 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! 57 alarm $timeout; 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); 61 alarm 0; 62 }; 63 exit $retval; # return system()-retval, or 255 if system() never returned 64' "$@" 65} 66 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 72Test() { 73 # Note: make sure these varnames don't conflict with any vars outside Test()! 74 timeout="$1" 75 shift 76 expected_ec="$1" 77 shift 78 expected_regexp="$1" 79 shift 80 unexpected_regexp="$1" 81 shift 82 83 echo -n "Testing $EXE with $@ ... " 84 output="$TMPDIR/output" 85 ALARM $timeout env "$@" $EXE > "$output" 2>&1 86 actual_ec=$? 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 93 echo "PASS" 94 return 0 # 0: success 95 fi 96 # If we get here, we failed. Now we just need to report why 97 echo "FAIL" 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." 102 else 103 $ec_ok || \ 104 echo "Wrong exit code: expected: '$expected_ec'; actual: $actual_ec" 105 $matches_ok || \ 106 echo "Output did not match '$expected_regexp'" 107 $negmatches_ok || \ 108 echo "Output unexpectedly matched '$unexpected_regexp'" 109 fi 110 echo "Output from failed run:" 111 echo "---" 112 cat "$output" 113 echo "---" 114 return 1 # 1: failure 115} 116 117TMPDIR=/tmp/heap_check_death_info 118rm -rf $TMPDIR || exit 1 119mkdir $TMPDIR || exit 2 120 121export HEAPCHECK=strict # default mode 122 123# These invocations should pass (0 == PASS): 124 125# This tests that turning leak-checker off dynamically works fine 126Test 120 0 "^PASS$" "" HEAPCHECK="" || exit 1 127 128# This disables threads so we can cause leaks reliably and test finding them 129Test 120 0 "^PASS$" "" HEAP_CHECKER_TEST_NO_THREADS=1 || exit 2 130 131# Test that --test_cancel_global_check works 132Test 20 0 "Canceling .* whole-program .* leak check$" "" \ 133 HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_TEST_CANCEL_GLOBAL_CHECK=1 || exit 3 134Test 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 136 137# Test that very early log messages are present and controllable: 138EARLY_MSG="Starting tracking the heap$" 139 140Test 60 0 "$EARLY_MSG" "" \ 141 HEAPCHECK="" HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 \ 142 PERFTOOLS_VERBOSE=10 || exit 5 143Test 60 0 "MemoryRegionMap Init$" "" \ 144 HEAPCHECK="" HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 \ 145 PERFTOOLS_VERBOSE=11 || exit 6 146Test 60 0 "" "$EARLY_MSG" \ 147 HEAPCHECK="" HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 \ 148 PERFTOOLS_VERBOSE=-11 || exit 7 149 150# These invocations should fail with very high probability, 151# rather than return 0 or hang (1 == exit(1), 134 == abort(), 139 = SIGSEGV): 152 153Test 60 1 "Exiting .* because of .* leaks$" "" \ 154 HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 || exit 8 155Test 60 1 "Exiting .* because of .* leaks$" "" \ 156 HEAP_CHECKER_TEST_TEST_LOOP_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 || exit 9 157 158# Test that we produce a reasonable textual leak report. 159Test 60 1 "MakeALeak" "" \ 160 HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECK_TEST_NO_THREADS=1 \ 161 || exit 10 162 163# Test that very early log messages are present and controllable: 164Test 60 1 "Starting tracking the heap$" "" \ 165 HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 PERFTOOLS_VERBOSE=10 \ 166 || exit 11 167Test 60 1 "" "Starting tracking the heap" \ 168 HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 PERFTOOLS_VERBOSE=-10 \ 169 || exit 12 170 171cd / # so we're not in TMPDIR when we delete it 172rm -rf $TMPDIR 173 174echo "PASS" 175 176exit 0 177