interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tools / x86efi / corpus_diff.sh

99 lines · 3605 bytes Raw Blame History
1#!/bin/sh
2# corpus_diff.sh NEW_OMC OLD_OMC OUT.tsv [asis|x86linux]
3# One TSV line per tracked .om file:
4# path verdict exit_new exit_old sha256_new sha256_old
5# verdict:
6# identical: both compilers exit 0 and write the same stdout bytes
7# same-refusal: both exit with the same status in 1..128 and write the same stderr
8# signal: either compiler was killed by a signal (exit 129 or above; 139 is SIGSEGV, which is how
9# a guest segfault under qemu-user reaches the shell). Never counted as a refusal; each such
10# line is also reported on stderr as CORPUS_DIFF_SIGNAL.
11# differ: any other pair
12# stdout is hashed even when a compiler refuses (the hash is of the bytes it wrote).
13# A compiler that is not a native executable for this machine is NOT_RUN (exit 3).
14# CORPUS_DIFF_ALLOW_QEMU=1 runs that pair anyway and records the host on stderr.
15# file(1) decides whether a compiler is native; without it the script is NOT_RUN (exit 3).
16# No em dash or en dash is used in this file.
17set -eu
18if [ "$#" -lt 3 ] || [ "$#" -gt 4 ]; then
19 echo "usage: corpus_diff.sh NEW_OMC OLD_OMC OUT.tsv [asis|x86linux]" >&2
20 exit 2
21fi
22new=$1
23old=$2
24out=$3
25mode=${4:-asis}
26case $mode in
27 asis|x86linux) ;;
28 *) echo "mode must be asis or x86linux" >&2; exit 2 ;;
29esac
30[ -f "$new" ] && [ -f "$old" ] || { echo "NOT_RUN missing compiler" >&2; exit 3; }
31command -v file > /dev/null 2>&1 || { echo "NOT_RUN file(1) not found; it is needed to check that both compilers are native executables" >&2; exit 3; }
32
33root=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
34cd "$root"
35
36native_ok() {
37 desc=$(file -b "$1")
38 case $(uname -m) in
39 aarch64|arm64) echo "$desc" | grep -q 'ARM aarch64' ;;
40 x86_64) echo "$desc" | grep -q 'x86-64' ;;
41 *) return 1 ;;
42 esac
43}
44if ! native_ok "$new" || ! native_ok "$old"; then
45 if [ "${CORPUS_DIFF_ALLOW_QEMU:-}" != 1 ]; then
46 echo "NOT_RUN compilers are not native executables for $(uname -m)" >&2
47 echo "NOT_RUN host $(uname -sr)" >&2
48 file -b "$new" >&2
49 file -b "$old" >&2
50 exit 3
51 fi
52 echo "CORPUS_DIFF_QEMU host=$(uname -m) $(uname -sr) mode=$mode" >&2
53fi
54
55first_line() {
56 awk '
57 /^[ \t]*$/ { next }
58 /^[ \t]*note([ \t]|$)/ { next }
59 { print; exit }
60 ' "$1"
61}
62
63tmp=$(mktemp -d "${TMPDIR:-/tmp}/corpus-diff.XXXXXX")
64trap 'rm -rf "$tmp"' EXIT
65: > "$out"
66n=0
67git ls-files '*.om' | while IFS= read -r path; do
68 src=$path
69 if [ "$mode" = x86linux ]; then
70 lead=$(first_line "$path")
71 case $lead in
72 "target "*) src=$path ;;
73 *) { printf '%s\n' 'target x86linux'; cat "$path"; } > "$tmp/src.om"; src=$tmp/src.om ;;
74 esac
75 fi
76 set +e
77 "$new" < "$src" > "$tmp/new.out" 2> "$tmp/new.err"
78 nr=$?
79 "$old" < "$src" > "$tmp/old.out" 2> "$tmp/old.err"
80 orc=$?
81 set -e
82 nh=$(sha256sum "$tmp/new.out" | cut -d ' ' -f 1)
83 oh=$(sha256sum "$tmp/old.out" | cut -d ' ' -f 1)
84 if [ "$nr" -gt 128 ] || [ "$orc" -gt 128 ]; then
85 verdict=signal
86 echo "CORPUS_DIFF_SIGNAL $path exit_new=$nr exit_old=$orc" >&2
87 elif [ "$nr" -eq 0 ] && [ "$orc" -eq 0 ] && cmp -s "$tmp/new.out" "$tmp/old.out"; then
88 verdict=identical
89 elif [ "$nr" -ne 0 ] && [ "$orc" -ne 0 ] && [ "$nr" -eq "$orc" ] && cmp -s "$tmp/new.err" "$tmp/old.err"; then
90 verdict=same-refusal
91 else
92 verdict=differ
93 fi
94 printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$path" "$verdict" "$nr" "$orc" "$nh" "$oh"
95 n=$((n + 1))
96done > "$out"
97echo "CORPUS_DIFF lines=$(wc -l < "$out") mode=$mode out=$out"
98sig=$(awk -F '\t' '$2 == "signal"' "$out" | wc -l)
99[ "$sig" -eq 0 ] || echo "CORPUS_DIFF_SIGNAL total=$sig (a compiler was killed by a signal; not a refusal)" >&2