summaryrefslogtreecommitdiffhomepage
path: root/tests/zos/leak.sh
blob: e4167e2a844b1b9edc90c74091b50a9be674ffda (plain)
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
# z/OS: Language Environment's leak checker, as a pass or fail.
#
# HEAPCHK with a call depth and no per-call heap walk makes LE write the
# heap storage diagnostics report to a CEEDUMP at normal termination:
# every allocation still live at exit, with the traceback that made it.
# The total on its own says nothing, because a shell leaves working
# storage live at exit on purpose; a leak shows as the total growing
# with the work done.  So run one loop body at two sizes in one shell
# and compare.  The body takes in a substitution, a redirection, a
# forked command and a pipeline, so what it leaves behind is the
# shell's own storage on every one of those paths.
#
# Only the shell's report may be summed: a child inherits the dump
# target and writes one of its own, and 900 extra children would swamp
# the comparison.  The forked commands therefore turn heap checking off
# for themselves, and the main flow checks that one report came back.
#
# The limit is generous on purpose: the two runs pass a different
# iteration count on the command line, which moves the size of the
# environment copy by a few bytes.

command -v chtag >/dev/null 2>&1 || exit 77

n1=100
n2=1000

# Bytes live at exit across every report the run wrote.
live_bytes() {
    rm -rf "leak.$1"
    mkdir "leak.$1"
    (
        cd "leak.$1" || exit 1
        _CEE_RUNOPTS='HEAPCHK(ON,0,0,16,0)'
        export _CEE_RUNOPTS
        # The reports land here whatever the caller set.
        _CEE_DMPTARG=$PWD
        export _CEE_DMPTARG
        "$TSH" -c '
i=0
f() { local a=1; b=$((a + 2)); }
while [ "$i" -lt '"$1"' ]; do
    x=$(echo hi); y=${x#h}
    case $y in i) : ;; esac
    set -- a b c; shift
    f
    v=$((i * 3 + 1)); unset v
    : >redir.tmp
    w=$(_CEE_RUNOPTS="HEAPCHK(OFF)" /bin/true) 2>/dev/null
    echo hi | _CEE_RUNOPTS="HEAPCHK(OFF)" /bin/cat >/dev/null
    i=$((i + 1))
done' >/dev/null
    )
    awk '
        function h2d(s,  i, c, v, d) {
            d = 0; s = toupper(s)
            for (i = 1; i <= length(s); i++) {
                c = substr(s, i, 1)
                d = d * 16 + index("0123456789ABCDEF", c) - 1
            }
            return d
        }
        /Heap Storage Diagnostics/ { on = 1 }
        on && /^    [0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]  [0-9A-F]+  [0-9A-F]+ / {
            bytes += h2d($3)
        }
        END { print bytes + 0 }
    ' leak."$1"/CEEDUMP.*
}

a=$(live_bytes "$n1")
b=$(live_bytes "$n2")
grow=$((b - a))

for d in "leak.$n1" "leak.$n2"; do
    set -- "$d"/CEEDUMP.*
    { [ $# -eq 1 ] && [ -f "$1" ]; } || {
        echo "leak: $d: expected 1 report, found $#"
        exit 1
    }
done

# Only the comparison means anything: the environment copy differs by a
# byte or two between the runs and rounds to an 8-byte boundary, so the
# exact figure moves with whatever else is in the environment.  The
# number is printed only when it is the finding.
if [ "$grow" -gt 2048 ]; then
    echo "leak: grew by $grow bytes over $((n2 / n1))x the work"
    exit 1
fi
echo "leak: live storage did not grow with $((n2 / n1))x the work"