File: heap-checker-death_unittest.sh

package info (click to toggle)
google-perftools 2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,284 kB
  • sloc: cpp: 27,869; ansic: 9,534; sh: 4,799; perl: 4,116; makefile: 1,101; asm: 128
file content (176 lines) | stat: -rwxr-xr-x 6,656 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/sh
# Copyright (c) 2005, Google Inc.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# ---
# Author: Maxim Lifantsev
#
# Run the heap checker unittest in a mode where it is supposed to crash and
# return an error if it doesn't.

# We expect BINDIR to be set in the environment.
# If not, we set it to some reasonable value.
BINDIR="${BINDIR:-.}"

if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
  echo "USAGE: $0 [unittest dir]"
  echo "       By default, unittest_dir=$BINDIR"
  exit 1
fi

EXE="${1:-$BINDIR/heap-checker_unittest}"
TMPDIR="/tmp/heap_check_death_info"

ALARM() {
  # You need perl to run pprof, so I assume it's installed
  perl -e '
    $timeout=$ARGV[0]; shift;
    $retval = 255;   # the default retval, for the case where we timed out
    eval {           # need to run in an eval-block to trigger during system()
      local $SIG{ALRM} = sub { die "alarm\n" };  # \n is required!
      alarm $timeout;
      $retval = system(@ARGV);
      # Make retval bash-style: exit status, or 128+n if terminated by signal n
      $retval = ($retval & 127) ? (128 + $retval) : ($retval >> 8);
      alarm 0;
    };
    exit $retval;  # return system()-retval, or 255 if system() never returned
' "$@"
}

# $1: timeout for alarm;
# $2: regexp of expected exit code(s);
# $3: regexp to match a line in the output;
# $4: regexp to not match a line in the output;
# $5+ args to pass to $EXE
Test() {
  # Note: make sure these varnames don't conflict with any vars outside Test()!
  timeout="$1"
  shift
  expected_ec="$1"
  shift
  expected_regexp="$1"
  shift
  unexpected_regexp="$1"
  shift

  echo -n "Testing $EXE with $@ ... "
  output="$TMPDIR/output"
  ALARM $timeout env "$@" $EXE > "$output" 2>&1
  actual_ec=$?
  ec_ok=`expr "$actual_ec" : "$expected_ec$" >/dev/null || echo false`
  matches_ok=`test -z "$expected_regexp" || \
              grep "$expected_regexp" "$output" >/dev/null 2>&1 || echo false`
  negmatches_ok=`test -z "$unexpected_regexp" || \
                 ! grep "$unexpected_regexp" "$output" >/dev/null 2>&1 || echo false`
  if $ec_ok && $matches_ok && $negmatches_ok; then
    echo "PASS"
    return 0  # 0: success
  fi
  # If we get here, we failed.  Now we just need to report why
  echo "FAIL"
  if [ $actual_ec -eq 255 ]; then  # 255 == SIGTERM due to $ALARM
    echo "Test was taking unexpectedly long time to run and so we aborted it."
    echo "Try the test case manually or raise the timeout from $timeout"
    echo "to distinguish test slowness from a real problem."
  else
    $ec_ok || \
      echo "Wrong exit code: expected: '$expected_ec'; actual: $actual_ec"
    $matches_ok || \
      echo "Output did not match '$expected_regexp'"
    $negmatches_ok || \
      echo "Output unexpectedly matched '$unexpected_regexp'"
  fi
  echo "Output from failed run:"
  echo "---"
  cat "$output"
  echo "---"
  return 1  # 1: failure
}

TMPDIR=/tmp/heap_check_death_info
rm -rf $TMPDIR || exit 1
mkdir $TMPDIR || exit 2

export HEAPCHECK=strict       # default mode

# These invocations should pass (0 == PASS):

# This tests that turning leak-checker off dynamically works fine
Test 120 0 "^PASS$" "" HEAPCHECK="" || exit 1

# This disables threads so we can cause leaks reliably and test finding them
Test 120 0 "^PASS$" "" HEAP_CHECKER_TEST_NO_THREADS=1 || exit 2

# Test that --test_cancel_global_check works
Test 20 0 "Canceling .* whole-program .* leak check$" "" \
  HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_TEST_CANCEL_GLOBAL_CHECK=1 || exit 3
Test 20 0 "Canceling .* whole-program .* leak check$" "" \
  HEAP_CHECKER_TEST_TEST_LOOP_LEAK=1 HEAP_CHECKER_TEST_TEST_CANCEL_GLOBAL_CHECK=1 || exit 4

# Test that very early log messages are present and controllable:
EARLY_MSG="Starting tracking the heap$"

Test 60 0 "$EARLY_MSG" "" \
  HEAPCHECK="" HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 \
  PERFTOOLS_VERBOSE=10 || exit 5
Test 60 0 "MemoryRegionMap Init$" "" \
  HEAPCHECK="" HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 \
  PERFTOOLS_VERBOSE=11 || exit 6
Test 60 0 "" "$EARLY_MSG" \
  HEAPCHECK="" HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 \
  PERFTOOLS_VERBOSE=-11 || exit 7

# These invocations should fail with very high probability,
# rather than return 0 or hang (1 == exit(1), 134 == abort(), 139 = SIGSEGV):

Test 60 1 "Exiting .* because of .* leaks$" "" \
  HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 || exit 8
Test 60 1 "Exiting .* because of .* leaks$" "" \
  HEAP_CHECKER_TEST_TEST_LOOP_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 || exit 9

# Test that we produce a reasonable textual leak report.
Test 60 1 "MakeALeak" "" \
          HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 \
  || exit 10

# Test that very early log messages are present and controllable:
Test 60 1 "Starting tracking the heap$" "" \
  HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 PERFTOOLS_VERBOSE=10 \
  || exit 11
Test 60 1 "" "Starting tracking the heap" \
  HEAP_CHECKER_TEST_TEST_LEAK=1 HEAP_CHECKER_TEST_NO_THREADS=1 PERFTOOLS_VERBOSE=-10 \
  || exit 12

cd /    # so we're not in TMPDIR when we delete it
rm -rf $TMPDIR

echo "PASS"

exit 0