162306a36Sopenharmony_ci#!/bin/sh
262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0+
362306a36Sopenharmony_ci#
462306a36Sopenharmony_ci# Given a .litmus test and the corresponding litmus output file, check
562306a36Sopenharmony_ci# the .litmus.out file against the "Result:" comment to judge whether the
662306a36Sopenharmony_ci# test ran correctly.  If the --hw argument is omitted, check against the
762306a36Sopenharmony_ci# LKMM output, which is assumed to be in file.litmus.out. If either a
862306a36Sopenharmony_ci# "DATARACE" marker in the "Result:" comment or a "Flag data-race" marker
962306a36Sopenharmony_ci# in the LKMM output is present, the other must also be as well, at least
1062306a36Sopenharmony_ci# for litmus tests having a "Result:" comment. In this case, a failure of
1162306a36Sopenharmony_ci# the Always/Sometimes/Never portion of the "Result:" prediction will be
1262306a36Sopenharmony_ci# noted, but forgiven.
1362306a36Sopenharmony_ci#
1462306a36Sopenharmony_ci# If the --hw argument is provided, this is assumed to be a hardware
1562306a36Sopenharmony_ci# test, and the output is assumed to be in file.litmus.HW.out, where
1662306a36Sopenharmony_ci# "HW" is the --hw argument. In addition, non-Sometimes verification
1762306a36Sopenharmony_ci# results will be noted, but forgiven.  Furthermore, if there is no
1862306a36Sopenharmony_ci# "Result:" comment but there is an LKMM .litmus.out file, the observation
1962306a36Sopenharmony_ci# in that file will be used to judge the assembly-language verification.
2062306a36Sopenharmony_ci#
2162306a36Sopenharmony_ci# Usage:
2262306a36Sopenharmony_ci#	judgelitmus.sh file.litmus
2362306a36Sopenharmony_ci#
2462306a36Sopenharmony_ci# Run this in the directory containing the memory model, specifying the
2562306a36Sopenharmony_ci# pathname of the litmus test to check.
2662306a36Sopenharmony_ci#
2762306a36Sopenharmony_ci# Copyright IBM Corporation, 2018
2862306a36Sopenharmony_ci#
2962306a36Sopenharmony_ci# Author: Paul E. McKenney <paulmck@linux.ibm.com>
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cilitmus=$1
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciif test -f "$litmus" -a -r "$litmus"
3462306a36Sopenharmony_cithen
3562306a36Sopenharmony_ci	:
3662306a36Sopenharmony_cielse
3762306a36Sopenharmony_ci	echo ' --- ' error: \"$litmus\" is not a readable file
3862306a36Sopenharmony_ci	exit 255
3962306a36Sopenharmony_cifi
4062306a36Sopenharmony_ciif test -z "$LKMM_HW_MAP_FILE"
4162306a36Sopenharmony_cithen
4262306a36Sopenharmony_ci	litmusout=$litmus.out
4362306a36Sopenharmony_ci	lkmmout=
4462306a36Sopenharmony_cielse
4562306a36Sopenharmony_ci	litmusout="`echo $litmus |
4662306a36Sopenharmony_ci		sed -e 's/\.litmus$/.litmus.'${LKMM_HW_MAP_FILE}'/'`.out"
4762306a36Sopenharmony_ci	lkmmout=$litmus.out
4862306a36Sopenharmony_cifi
4962306a36Sopenharmony_ciif test -f "$LKMM_DESTDIR/$litmusout" -a -r "$LKMM_DESTDIR/$litmusout"
5062306a36Sopenharmony_cithen
5162306a36Sopenharmony_ci	:
5262306a36Sopenharmony_cielse
5362306a36Sopenharmony_ci	echo ' --- ' error: \"$LKMM_DESTDIR/$litmusout is not a readable file
5462306a36Sopenharmony_ci	exit 255
5562306a36Sopenharmony_cifi
5662306a36Sopenharmony_ciif grep -q '^Flag data-race$' "$LKMM_DESTDIR/$litmusout"
5762306a36Sopenharmony_cithen
5862306a36Sopenharmony_ci	datarace_modeled=1
5962306a36Sopenharmony_cifi
6062306a36Sopenharmony_ciif grep -q '^[( ]\* Result: ' $litmus
6162306a36Sopenharmony_cithen
6262306a36Sopenharmony_ci	outcome=`grep -m 1 '^[( ]\* Result: ' $litmus | awk '{ print $3 }'`
6362306a36Sopenharmony_ci	if grep -m1 '^[( ]\* Result: .* DATARACE' $litmus
6462306a36Sopenharmony_ci	then
6562306a36Sopenharmony_ci		datarace_predicted=1
6662306a36Sopenharmony_ci	fi
6762306a36Sopenharmony_ci	if test -n "$datarace_predicted" -a -z "$datarace_modeled" -a -z "$LKMM_HW_MAP_FILE"
6862306a36Sopenharmony_ci	then
6962306a36Sopenharmony_ci		echo '!!! Predicted data race not modeled' $litmus
7062306a36Sopenharmony_ci		exit 252
7162306a36Sopenharmony_ci	elif test -z "$datarace_predicted" -a -n "$datarace_modeled"
7262306a36Sopenharmony_ci	then
7362306a36Sopenharmony_ci		# Note that hardware models currently don't model data races
7462306a36Sopenharmony_ci		echo '!!! Unexpected data race modeled' $litmus
7562306a36Sopenharmony_ci		exit 253
7662306a36Sopenharmony_ci	fi
7762306a36Sopenharmony_cielif test -n "$LKMM_HW_MAP_FILE" && grep -q '^Observation' $LKMM_DESTDIR/$lkmmout > /dev/null 2>&1
7862306a36Sopenharmony_cithen
7962306a36Sopenharmony_ci	outcome=`grep -m 1 '^Observation ' $LKMM_DESTDIR/$lkmmout | awk '{ print $3 }'`
8062306a36Sopenharmony_cielse
8162306a36Sopenharmony_ci	outcome=specified
8262306a36Sopenharmony_cifi
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cigrep '^Observation' $LKMM_DESTDIR/$litmusout
8562306a36Sopenharmony_ciif grep -q '^Observation' $LKMM_DESTDIR/$litmusout
8662306a36Sopenharmony_cithen
8762306a36Sopenharmony_ci	:
8862306a36Sopenharmony_cielif grep ': Unknown macro ' $LKMM_DESTDIR/$litmusout
8962306a36Sopenharmony_cithen
9062306a36Sopenharmony_ci	badname=`grep ': Unknown macro ' $LKMM_DESTDIR/$litmusout |
9162306a36Sopenharmony_ci		sed -e 's/^.*: Unknown macro //' |
9262306a36Sopenharmony_ci		sed -e 's/ (User error).*$//'`
9362306a36Sopenharmony_ci	badmsg=' !!! Current LKMM version does not know "'$badname'"'" $litmus"
9462306a36Sopenharmony_ci	echo $badmsg
9562306a36Sopenharmony_ci	if ! grep -q '!!!' $LKMM_DESTDIR/$litmusout
9662306a36Sopenharmony_ci	then
9762306a36Sopenharmony_ci		echo ' !!! '$badmsg >> $LKMM_DESTDIR/$litmusout 2>&1
9862306a36Sopenharmony_ci	fi
9962306a36Sopenharmony_ci	exit 254
10062306a36Sopenharmony_cielif grep '^Command exited with non-zero status 124' $LKMM_DESTDIR/$litmusout
10162306a36Sopenharmony_cithen
10262306a36Sopenharmony_ci	echo ' !!! Timeout' $litmus
10362306a36Sopenharmony_ci	if ! grep -q '!!!' $LKMM_DESTDIR/$litmusout
10462306a36Sopenharmony_ci	then
10562306a36Sopenharmony_ci		echo ' !!! Timeout' >> $LKMM_DESTDIR/$litmusout 2>&1
10662306a36Sopenharmony_ci	fi
10762306a36Sopenharmony_ci	exit 124
10862306a36Sopenharmony_cielse
10962306a36Sopenharmony_ci	echo ' !!! Verification error' $litmus
11062306a36Sopenharmony_ci	if ! grep -q '!!!' $LKMM_DESTDIR/$litmusout
11162306a36Sopenharmony_ci	then
11262306a36Sopenharmony_ci		echo ' !!! Verification error' >> $LKMM_DESTDIR/$litmusout 2>&1
11362306a36Sopenharmony_ci	fi
11462306a36Sopenharmony_ci	exit 255
11562306a36Sopenharmony_cifi
11662306a36Sopenharmony_ciif test "$outcome" = DEADLOCK
11762306a36Sopenharmony_cithen
11862306a36Sopenharmony_ci	if grep '^Observation' $LKMM_DESTDIR/$litmusout | grep -q 'Never 0 0$'
11962306a36Sopenharmony_ci	then
12062306a36Sopenharmony_ci		ret=0
12162306a36Sopenharmony_ci	else
12262306a36Sopenharmony_ci		echo " !!! Unexpected non-$outcome verification" $litmus
12362306a36Sopenharmony_ci		if ! grep -q '!!!' $LKMM_DESTDIR/$litmusout
12462306a36Sopenharmony_ci		then
12562306a36Sopenharmony_ci			echo " !!! Unexpected non-$outcome verification" >> $LKMM_DESTDIR/$litmusout 2>&1
12662306a36Sopenharmony_ci		fi
12762306a36Sopenharmony_ci		ret=1
12862306a36Sopenharmony_ci	fi
12962306a36Sopenharmony_cielif grep '^Observation' $LKMM_DESTDIR/$litmusout | grep -q 'Never 0 0$'
13062306a36Sopenharmony_cithen
13162306a36Sopenharmony_ci	echo " !!! Unexpected non-$outcome deadlock" $litmus
13262306a36Sopenharmony_ci	if ! grep -q '!!!' $LKMM_DESTDIR/$litmusout
13362306a36Sopenharmony_ci	then
13462306a36Sopenharmony_ci		echo " !!! Unexpected non-$outcome deadlock" $litmus >> $LKMM_DESTDIR/$litmusout 2>&1
13562306a36Sopenharmony_ci	fi
13662306a36Sopenharmony_ci	ret=1
13762306a36Sopenharmony_cielif grep '^Observation' $LKMM_DESTDIR/$litmusout | grep -q $outcome || test "$outcome" = Maybe
13862306a36Sopenharmony_cithen
13962306a36Sopenharmony_ci	ret=0
14062306a36Sopenharmony_cielse
14162306a36Sopenharmony_ci	if test \( -n "$LKMM_HW_MAP_FILE" -a "$outcome" = Sometimes \) -o -n "$datarace_modeled"
14262306a36Sopenharmony_ci	then
14362306a36Sopenharmony_ci		flag="--- Forgiven"
14462306a36Sopenharmony_ci		ret=0
14562306a36Sopenharmony_ci	else
14662306a36Sopenharmony_ci		flag="!!! Unexpected"
14762306a36Sopenharmony_ci		ret=1
14862306a36Sopenharmony_ci	fi
14962306a36Sopenharmony_ci	echo " $flag non-$outcome verification" $litmus
15062306a36Sopenharmony_ci	if ! grep -qe "$flag" $LKMM_DESTDIR/$litmusout
15162306a36Sopenharmony_ci	then
15262306a36Sopenharmony_ci		echo " $flag non-$outcome verification" >> $LKMM_DESTDIR/$litmusout 2>&1
15362306a36Sopenharmony_ci	fi
15462306a36Sopenharmony_cifi
15562306a36Sopenharmony_citail -2 $LKMM_DESTDIR/$litmusout | head -1
15662306a36Sopenharmony_ciexit $ret
157