Skip to content
pi-c Artifact [2d614c4a41]
Login

Artifact [2d614c4a41]

Artifact 2d614c4a41ac34c936d81376eb92e09bf7f6c623f1f2eedd24b5fc7e0092cda8:


#!/bin/sh
# Current-context substrate proof only. Never runs the CLI against a user's DB.
# Exit 2 gates missing local inputs/tools; exit 1 is a failed proof/build.
set -eu
ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
SOURCE=${CONTEXT_SOURCE:-/Users/dmestas/projects/context}
OUT="$ROOT/build/qualification/context"
CC=${CC:-cc}
BOUNDED="$ROOT/build/qualification/run-bounded"
WARN='-std=c11 -Wall -Wextra -Wpedantic -Werror -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wvla -Wcast-qual'

gate() { printf 'GATED: context %s\n' "$*" >&2; exit 2; }
MODE=${1:-normal}
[ "$#" -le 1 ] || gate 'usage: sh tools/test-context.sh [normal|sanitize]'
case "$MODE" in
    normal) FLAGS='-O1 -g' ;;
    sanitize) FLAGS='-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=undefined' ;;
    *) gate 'usage: sh tools/test-context.sh [normal|sanitize]' ;;
esac
for tool in "$CC" make shasum cmp mktemp; do
    command -v "$tool" >/dev/null 2>&1 || gate "missing local tool: $tool"
done
[ -d "$SOURCE" ] || gate "source directory not found: $SOURCE"
SOURCE=$(CDPATH='' cd -- "$SOURCE" && pwd -P)
INPUTS='Makefile src/ctx.c src/page.html schema/index.sql schema/state.sql schema/ticket-table.sql vendor/libfossil/libfossil.c vendor/libfossil/libfossil.h vendor/libfossil/libfossil-config.h vendor/libfossil/sqlite3.c vendor/libfossil/sqlite3.h'
for input in $INPUTS; do
    [ -f "$SOURCE/$input" ] && [ ! -L "$SOURCE/$input" ] || gate "missing regular source input: $input"
done
mkdir -p "$OUT"
RUN=$(mktemp -d "$OUT/run.XXXXXX")
SNAPSHOT="$RUN/source"
PRIVATE=''
cleanup() {
    if [ -n "$PRIVATE" ]; then
        case "$PRIVATE" in "$RUN"/private.*) ;; *) exit 1 ;; esac
        [ ! -L "$PRIVATE" ] || exit 1
        rm -rf -- "$PRIVATE"
    fi
}
trap cleanup 0
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
printf '%s\n' "$RUN" > "$OUT/latest-run.txt"
printf '%s\n' "$SOURCE" > "$RUN/source-location.txt"
printf '%s\n' "$MODE" > "$RUN/mode.txt"
printf 'NOT RUN\n' > "$RUN/result.txt"
mkdir -p "$SNAPSHOT/src" "$SNAPSHOT/schema" "$SNAPSHOT/vendor/libfossil" "$RUN/probe"
# Copy only source/schema/build inputs: never reuse upstream build objects or DBs.
# Splitting is intentional: INPUTS is the fixed space-free allowlist above.
# shellcheck disable=SC2086
(cd "$SOURCE" && shasum -a 256 $INPUTS) > "$RUN/source.sha256"
for input in $INPUTS; do cp "$SOURCE/$input" "$SNAPSHOT/$input"; done
(cd "$SNAPSHOT" && shasum -a 256 -c "$RUN/source.sha256") > "$RUN/snapshot-check.log"
# shellcheck disable=SC2086
(cd "$SOURCE" && shasum -a 256 $INPUTS) > "$RUN/source-after.sha256"
cmp -s "$RUN/source.sha256" "$RUN/source-after.sha256" || gate 'source changed during snapshot'
cp "$ROOT/test/context/atomicity.c" "$ROOT/test/context/context-test.c" \
   "$ROOT/test/context/context-test.h" "$RUN/probe/"
(cd "$RUN/probe" && shasum -a 256 atomicity.c context-test.c context-test.h) > "$RUN/probe.sha256"
shasum -a 256 "$ROOT/tools/run-bounded.c" "$ROOT/tools/test-context.sh" > "$RUN/driver.sha256"
# Same small helper bootstrap used by the existing local qualification drivers.
if [ ! -x "$BOUNDED" ] || [ "$ROOT/tools/run-bounded.c" -nt "$BOUNDED" ]; then
    "$CC" -std=c11 -O2 "$ROOT/tools/run-bounded.c" -o "$BOUNDED"
fi
run() {
    seconds=$1
    shift
    printf '%s\n' '-- argv --' "$BOUNDED" "$seconds" '--' "$@" >> "$RUN/commands.log" || return "$?"
    "$BOUNDED" "$seconds" -- "$@"
}
run 10 "$CC" --version > "$RUN/compiler.txt" 2>&1
run 180 make -C "$SNAPSHOT" -j2 "CC=$CC" "CFLAGS=$FLAGS" \
    build/schema.h build/state_schema.h build/ticket_schema.h build/page.h \
    build/libfossil.o build/sqlite3.o > "$RUN/build.log" 2>&1 || {
    /bin/cat "$RUN/build.log" >&2; exit 1;
}
# Legacy CLI has unrelated unused static functions under CTX_NO_MAIN. Keep its
# existing -Wall/-Wextra policy, promote to errors, suppress only that warning.
# shellcheck disable=SC2086
run 60 "$CC" $FLAGS -Wall -Wextra -Werror -Wno-unused-function \
    -I"$SNAPSHOT/src" -I"$SNAPSHOT/build" -I"$SNAPSHOT/vendor/libfossil" \
    -I"$RUN/probe" -c "$RUN/probe/context-test.c" -o "$RUN/context-test.o" \
    >> "$RUN/build.log" 2>&1 || { /bin/cat "$RUN/build.log" >&2; exit 1; }
case $(uname -s) in Darwin) PLATFORM_LIB=-liconv ;; *) PLATFORM_LIB=-ldl ;; esac
build_probe() {
    binary=$1
    shift
    # The probe, including mutant builds, retains every strict warning.
    # shellcheck disable=SC2086
    run 60 "$CC" $WARN $FLAGS -I"$SNAPSHOT/vendor/libfossil" -I"$RUN/probe" \
        "$@" -c "$RUN/probe/atomicity.c" -o "$binary.o" || return "$?"
    # shellcheck disable=SC2086
    run 60 "$CC" $FLAGS "$binary.o" "$RUN/context-test.o" \
        "$SNAPSHOT/build/libfossil.o" "$SNAPSHOT/build/sqlite3.o" \
        -lz -lpthread -lm "$PLATFORM_LIB" -o "$binary"
}
build_probe "$RUN/atomicity" >> "$RUN/build.log" 2>&1 || {
    /bin/cat "$RUN/build.log" >&2; exit 1;
}
build_probe "$RUN/commit-mutant" -DCTX_PROBE_MUTANT_COMMIT_ROLLBACK \
    >> "$RUN/build.log" 2>&1 || { /bin/cat "$RUN/build.log" >&2; exit 1; }
shasum -a 256 "$RUN/atomicity" "$RUN/commit-mutant" "$BOUNDED" > "$RUN/binary.sha256"
run_probe() {
    PRIVATE=$(mktemp -d "$RUN/private.XXXXXX") || return "$?"
    mkdir "$PRIVATE/fossil" "$PRIVATE/config" "$PRIVATE/tmp" || return "$?"
    printf 'context-atomicity-probe\n' > "$PRIVATE/.context-probe-private" || return "$?"
    printf '%s\n' "$PRIVATE" >> "$RUN/private-paths.txt" || return "$?"
    # No inherited FOSSIL_HOME, XDG_CONFIG_HOME, provider config, or credentials.
    (cd "$PRIVATE" && run 40 env -i PATH=/usr/bin:/bin HOME="$PRIVATE" \
        CTX_DB="$PRIVATE/ctx.sqlite" USER=context-probe LOGNAME=context-probe \
        FOSSIL_HOME="$PRIVATE/fossil" XDG_CONFIG_HOME="$PRIVATE/config" \
        TMPDIR="$PRIVATE/tmp" ASAN_OPTIONS=detect_leaks=0:abort_on_error=1 \
        UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 "$1")
}
if run_probe "$RUN/atomicity" > "$RUN/probe.log" 2>&1; then
    cleanup
    PRIVATE=''
else
    status=$?
    printf 'FAIL: probe exit %s\n' "$status" > "$RUN/result.txt"
    /bin/cat "$RUN/probe.log" >&2
    printf 'context evidence: %s\n' "$RUN" >&2
    exit "$status"
fi
if run_probe "$RUN/commit-mutant" > "$RUN/mutant.log" 2>&1; then
    printf 'FAIL: rollback oracle accepted COMMIT mutant\n' >&2
    exit 1
else
    status=$?
    if [ "$status" -ne 1 ] || ! grep -F 'FAIL: technote body matches expected committed version' \
        "$RUN/mutant.log" >/dev/null; then
        /bin/cat "$RUN/mutant.log" >&2
        printf 'FAIL: mutant failed for an unexpected reason\n' >&2
        exit 1
    fi
    cleanup
    PRIVATE=''
fi
while IFS= read -r private_path; do
    [ ! -e "$private_path" ] && [ ! -L "$private_path" ] || {
        printf 'FAIL: private resources remain\n' >&2; exit 1;
    }
done < "$RUN/private-paths.txt"
printf 'PASS: rollback oracle rejects commit mutant\nPASS: private context resources removed\n' >> "$RUN/probe.log"
if [ "$MODE" = sanitize ]; then
    printf 'PASS: context ASan+UBSan (leak detection not claimed)\n' >> "$RUN/probe.log"
fi
printf 'PASS (storage substrate only)\n' > "$RUN/result.txt"
/bin/cat "$RUN/probe.log"
printf 'context evidence: %s\n' "$RUN"