#!/bin/sh
# PCP QA Test No. 2114
#
# Check for memory leaks on the error paths of the derived metrics parser,
# and in the teardown of a derived metric holding a compiled regex.
#
# Companion test qa/2113 checks the diagnostics from the same expressions.
#
# Note the expressions are deliberately bundled into one config file, and
# the last one is deliberately harmless.  The parser keeps its most recent
# allocations in file-scope statics (np, specmeta) and in bison's
# derive_lval, so the last thing a failed parse leaks is still reachable
# at exit and valgrind reports it as "still reachable", not "definitely
# lost".  It only becomes definitely lost once a later parse overwrites
# those pointers ... so every expression here needs another one after it,
# and one expression per run would prove nothing.
#
# Copyright (c) 2026 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_valgrind

status=0	# success is the default!
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

# For this test, don't load any global derived metric configs by default.
# So the PCP_DERIVED_CONFIG environment variable is set to an empty string.
PCP_DERIVED_CONFIG=""; export PCP_DERIVED_CONFIG

_filter()
{
    sed \
	-e '/pmResult/s/ .* numpmid/ ... numpmid/' \
    # end
}

# real QA test starts here

cat <<'End-of-File' >$tmp.derived.conf
# syntax errors with a partial expression on the parser stack
e.rescale.a = rescale(disk.dev.read,
e.matchinst.a = matchinst(/100/
e.matchinst.b = matchinst(/100/,
e.matchinst.c = matchinst(/100/, disk.dev.read
e.matchinst.d = matchinst(/100/, (disk.dev.read + 1
e.quest.a = disk.dev.read ? disk.dev.write :
e.instname.a = disk.dev.read[
# mkconst() errors, before and after the comma
e.mkconst.a = mkconst(123)
e.mkconst.b = mkconst(123 type=32)
e.mkconst.c = mkconst(123, bogus=1)
e.mkconst.d = mkconst(123.456)
e.mkconst.e = mkconst(123.456 type=float)
e.mkconst.f = mkconst(123.456, bogus=1)
# semantic errors that abort the parse
e.units.a = rescale(disk.dev.read, "no such unit")
e.units.b = mkconst(123, units="no such unit")
e.type.a = mkconst(123.456, type=32)
e.type.b = mkconst(123, type="no such type")
e.sem.a = mkconst(123, semantics="no such semantics")
e.indom.a = mkconst(123, indom="123.456foo")
e.anon.a = anon(PM_TYPE_BOGUS)
e.regex.a = matchinst(/10[/, disk.dev.read)
# novalue() errors
e.novalue.a = 1 ? disk.dev.read : novalue(
e.novalue.b = 1 ? disk.dev.read : novalue(type="no such type")
e.novalue.c = 1 ? disk.dev.read : novalue(bogus=1)
# state left behind by a failed parse
e.stale.a = mkconst(123, meta=disk.dev.read, type="no such type")
e.stale.b = mkconst(123, units=Kbyte, type="no such type")
e.stale.c = mkconst(123, meta=disk.dev.read, meta=disk.dev.read_bytes, type=u32)
# ... and something that parses, so the above are no longer reachable
e.ok = mkconst(42, type=u32)
End-of-File

PCP_DERIVED_CONFIG=$tmp.derived.conf _run_valgrind --save-output pminfo -a archives/20041125 e.ok
echo "=== expressions that failed to parse ==="
grep -c 'syntax error' $tmp.err
echo "=== pminfo stdout ==="
cat $tmp.out

# matchinst() compiles a regex, and only the registered[] copy of the
# expression tree owns it ... bind_expr() shallow copies the regex_t into
# the per-context copy it builds for a global derived metric.  So the
# regex must be regfree()d when the registered[] tree goes away, and must
# _not_ be regfree()d when a bound copy goes away.
#
# Note the metric has to be unregistered.  Without that the regex is
# still reachable from registered[] at exit and valgrind says nothing.
#
# Fetching is left to qa/2115, which covers the derived metric fetch
# path.
#
echo
echo "=== global derived metric with a regex, unregistered ==="
PCP_DERIVED_CONFIG="" _run_valgrind src/unregister \
    -r e.mi='matchinst(/dev/,disk.dev.read)' -a archives/20041125 -u e.mi e.mi \
| _filter

echo
echo "=== per-context derived metric with a regex, unregistered ==="
PCP_DERIVED_CONFIG="" _run_valgrind src/unregister \
    -a archives/20041125 -q e.mi='matchinst(/dev/,disk.dev.read)' -u e.mi e.mi \
| _filter
