#!/bin/bash

# Copyright (c) 2019-2026 Red Hat.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your
# option) any later version.
#
# It is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


# Test that a warning is issued for instrumented code

TEST_NAME=instrumentation
. $srcdir/common.sh

PLUGIN_OPTS="-fplugin-arg-annobin-no-attach"
OPTS="-c -O2 -D_FORTIFY_SOURCE=2 -fPIE -Wall -fstack-protector-strong -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -p"

start_test

EXE=instrumentation-test.exe

$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -g $OPTS $srcdir/hello_hard.c
$GCC -pie hello_hard.o -o $EXE -Wl,--defsym,big_stack=0 -Wl,-z,now
if [ $? != 0 ];
then
    echo "$TEST_NAME: SKIP: Unable to compile test source code"
    end_test
    exit $EXIT_TEST_SKIPPED
fi

A_OUT=instrumentation-test.out
$ANNOCHECK --suppress-version-warnings --skip-all --test-instrumentation $EXE > $A_OUT

grep -q -e "MAYB: test: instrumentation" $A_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not FAIL for executable compiled with instrumentation"
    echo " $TEST_NAME: annocheck output:"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

end_test
