Skip to content
pi-c Artifact [5851e80872]
Login

Artifact [5851e80872]

Artifact 5851e8087295689f0c46a23446a3c441031a08230b3adeb61dc3a480ab1fa305:


#!/bin/sh
# S0 MC/DC qualification probe driver (build orchestration only).
#
# Compiles the declared probe with MC/DC instrumentation, runs it, merges the
# profile, exports llvm-cov JSON, and hands that evidence to the pure evaluator
# in tools/toolchain.py. The probe binary and profile artifacts are retained.
#
# The compiler and LLVM tools must come from one toolchain; on macOS they
# resolve through xcrun. Every child is bounded and reaped.
#
# Exit codes: 0 PASS, 1 FAIL (incomplete or invalid coverage), 2 GATED (a
# required tool or feature is unavailable).
usage() {
    cat >&2 <<'EOF'
usage: mcdc.sh [--out DIR] [--cc PATH] [--llvm-cov PATH] [--llvm-profdata PATH]
               [--probe FILE] [--define K=V]... [--expected-decisions N]
               [--timeout SECONDS]
EOF
    exit 2
}

ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
OUT="$ROOT/build/qualification/mcdc"
CC_ARG=""
LLVM_COV_ARG=""
LLVM_PROFDATA_ARG=""
PROBE="$ROOT/test/probes/mcdc.c"
EXPECTED=2
LIMIT=120

while [ $# -gt 0 ]; do
    case "$1" in
        --out) [ $# -ge 2 ] || usage; OUT=$2; shift 2 ;;
        --cc) [ $# -ge 2 ] || usage; CC_ARG=$2; shift 2 ;;
        --llvm-cov) [ $# -ge 2 ] || usage; LLVM_COV_ARG=$2; shift 2 ;;
        --llvm-profdata) [ $# -ge 2 ] || usage; LLVM_PROFDATA_ARG=$2; shift 2 ;;
        --probe) [ $# -ge 2 ] || usage; PROBE=$2; shift 2 ;;
        --define) [ $# -ge 2 ] || usage; DEFINES="$DEFINES -D$2"; shift 2 ;;
        --expected-decisions) [ $# -ge 2 ] || usage; EXPECTED=$2; shift 2 ;;
        --timeout) [ $# -ge 2 ] || usage; LIMIT=$2; shift 2 ;;
        -h|--help) usage ;;
        *) usage ;;
    esac
done

case "$EXPECTED" in ''|*[!0-9]*) usage ;; esac
[ "$EXPECTED" -ge 1 ] || usage
case "$LIMIT" in ''|*[!0-9]*) usage ;; esac
[ "$LIMIT" -ge 1 ] && [ "$LIMIT" -le 600 ] || usage
[ -f "$PROBE" ] || { echo "mcdc: probe not found: $PROBE" >&2; exit 2; }

# The shared helper owns the child's process group and deadline; it exits 124
# on timeout and 125 when it cannot isolate or arm, which we treat as GATED.
BOUNDED="$ROOT/build/qualification/run-bounded"
bounded() {
    limit=$1
    shift
    "$BOUNDED" "$limit" -- "$@"
}

ensure_bounded() {
    mkdir -p "$(dirname "$BOUNDED")"
    if [ -x "$BOUNDED" ] && [ "$ROOT/tools/run-bounded.c" -ot "$BOUNDED" ]; then
        return 0
    fi
    "${CC:-cc}" -std=c11 -O2 "$ROOT/tools/run-bounded.c" -o "$BOUNDED" >&2
}

resolve() { # resolve NAME OVERRIDE
    if [ -n "$2" ]; then
        [ -x "$2" ] && printf '%s\n' "$2"
        return
    fi
    if command -v "$1" >/dev/null 2>&1; then
        command -v "$1"
        return
    fi
    if [ "$(uname -s)" = Darwin ]; then
        xcrun --find "$1" 2>/dev/null
    fi
}

gate() { # gate REASON
    mkdir -p "$OUT"
    python3 -I "$ROOT/tools/toolchain.py" --out "$OUT" --gate "$1"
    exit 2
}

CC=$(resolve clang "$CC_ARG")
[ -n "$CC" ] || gate "compiler not found: ${CC_ARG:-clang}"
HELP_LOG=""
cleanup() {
    [ -n "$HELP_LOG" ] && rm -f "$HELP_LOG"
}
trap cleanup EXIT INT TERM HUP

mkdir -p "$OUT"
HELP_LOG=$(mktemp "${TMPDIR:-/tmp}/pi-c-mcdc-help.XXXXXX") || {
    echo "mcdc: cannot create a private temp file" >&2
    exit 2
}
"$CC" --help >"$HELP_LOG" 2>&1
if ! grep -q -- '-fcoverage-mcdc' "$HELP_LOG"; then
    gate "$CC does not expose -fcoverage-mcdc"
fi
rm -f "$HELP_LOG"
HELP_LOG=""

LLVM_COV=$(resolve llvm-cov "$LLVM_COV_ARG")
[ -n "$LLVM_COV" ] || gate "llvm-cov not found (PATH and xcrun)"
LLVM_PROFDATA=$(resolve llvm-profdata "$LLVM_PROFDATA_ARG")
[ -n "$LLVM_PROFDATA" ] || gate "llvm-profdata not found (PATH and xcrun)"

ensure_bounded || gate "cannot build the bounded-command helper"

mkdir -p "$OUT"
BIN="$OUT/probe"
PROFRAW="$OUT/probe.profraw"
PROFDATA="$OUT/probe.profdata"
EXPORT="$OUT/export.json"
CONTEXT="$OUT/context.txt"
rm -f "$PROFRAW" "$PROFDATA" "$EXPORT"

# shellcheck disable=SC2086
if ! bounded "$LIMIT" "$CC" -O0 -g $DEFINES \
        -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc \
        "$PROBE" -o "$BIN" >"$OUT/compile.log" 2>&1; then
    gate "probe did not compile with MC/DC instrumentation"
fi

if ! LLVM_PROFILE_FILE="$PROFRAW" bounded "$LIMIT" "$BIN" >"$OUT/probe.log" 2>&1; then
    python3 -I "$ROOT/tools/toolchain.py" --out "$OUT" --gate \
        "instrumented probe failed: $(tail -n 1 "$OUT/probe.log")"
    exit 1
fi
[ -f "$PROFRAW" ] || gate "no profile data emitted by the instrumented probe"

bounded "$LIMIT" "$LLVM_PROFDATA" merge -sparse "$PROFRAW" -o "$PROFDATA" \
    >"$OUT/merge.log" 2>&1 || gate "llvm-profdata merge failed"
bounded "$LIMIT" "$LLVM_COV" export "$BIN" -instr-profile="$PROFDATA" \
    >"$EXPORT" 2>"$OUT/export.log" || gate "llvm-cov export failed"
[ -s "$EXPORT" ] || gate "llvm-cov export produced no JSON"

{
    printf 'compiler=%s\n' "$CC"
    printf 'compiler_version=%s\n' "$("$CC" --version 2>/dev/null | head -n 1)"
    printf 'llvm_cov=%s\n' "$LLVM_COV"
    printf 'llvm_cov_version=%s\n' "$("$LLVM_COV" --version 2>/dev/null | head -n 1)"
    printf 'llvm_profdata=%s\n' "$LLVM_PROFDATA"
    printf 'probe_source=%s\n' "$PROBE"
    printf 'probe_binary=%s\n' "$BIN"
    printf 'profraw=%s\n' "$PROFRAW"
    printf 'profdata=%s\n' "$PROFDATA"
    printf 'defines=%s\n' "${DEFINES# }"
} > "$CONTEXT"

python3 -I "$ROOT/tools/toolchain.py" --export "$EXPORT" --out "$OUT" \
    --context "$CONTEXT" --expected-decisions "$EXPECTED"