AtlatestRepositorysigil-doc
1
#!/usr/bin/env bash2
# SD-P4 sabotage driver.3
#4
# Seven product-layer legs and one assertion-layer leg. For each: plant the defect5
# the arm exists to catch, prove the arm goes RED, restore, and prove the6
# restoration by CONTENT HASH rather than by the exit code of the restore.7
#8
# Two properties this driver asserts about ITSELF, because a sabotage run that9
# quietly did nothing is indistinguishable from a passing one:10
# * the edit must actually change the file (hash before != hash after), or the11
# leg is vacuous and the run aborts;12
# * the red must be an ASSERTION failure from a suite that compiled and ran --13
# the driver requires the test runner's own "N passed, M failed" line, so a14
# compile error cannot be mistaken for a demonstrated red.15
set -euo pipefail16
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"17
cd "$repo_dir"18
sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"19
export SIGIL_REDIRECTS="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"20
work=$(mktemp -d "$repo_dir/build/sd-p4-sabotage.XXXXXX")21
export XDG_CACHE_HOME="$work/cache"23
parser=src/sigil/doc/private/parser.sgl24
inspect=src/sigil/doc/inspect.sgl25
suite=test/test-passive-parse.sgl26
reader=test/test-reader.sgl28
pristine="$work/pristine"29
mkdir -p "$pristine"30
for f in "$parser" "$inspect" "$suite" "$reader"; do31
mkdir -p "$pristine/$(dirname "$f")"32
cp "$f" "$pristine/$f"33
done35
hash_of() { sha256sum "$1" | cut -d' ' -f1; }37
restore() {38
for f in "$parser" "$inspect" "$suite" "$reader"; do39
cp "$pristine/$f" "$f"40
test "$(hash_of "$f")" = "$(hash_of "$pristine/$f")" || {41
echo "RESTORE FAILED for $f" >&2; exit 1; }42
done43
}45
# apply <file> <old> <new> -- exact string replacement, aborting if it no-ops.46
apply() {47
local file="$1" old="$2" new="$3" before after48
before=$(hash_of "$file")49
python3 - "$file" "$old" "$new" <<'PY'50
import sys51
path, old, new = sys.argv[1], sys.argv[2], sys.argv[3]52
text = open(path).read()53
if text.count(old) != 1:54
sys.exit("sabotage target appears %d times in %s; expected exactly 1" % (text.count(old), path))55
open(path, "w").write(text.replace(old, new))56
PY57
after=$(hash_of "$file")58
test "$before" != "$after" || { echo "SABOTAGE DID NOT CHANGE $file" >&2; exit 1; }59
}61
# expect_suite_red <label> <suite> <log>62
expect_suite_red() {63
local label="$1" target="$2" log="$3"64
set +e65
setsid -w "$sigil" -L src -L test test "$target" >"$log" 2>&166
local status=$?67
set -e68
echo "--- $label: exit=$status"69
# A red must come from a suite that COMPILED AND RAN. Without this the leg70
# would accept a compile error as a demonstrated failure.71
rg -q '\d+ passed, \d+ failed' "$log" || {72
echo "$label: no runner summary -- the suite did not run, this is NOT a demonstrated red" >&273
tail -30 "$log" >&2; exit 1; }74
test "$status" -ne 0 || { echo "$label: SABOTAGE DID NOT TURN THE GATE RED" >&2; exit 1; }75
rg -o '\d+ passed, \d+ failed' "$log" | head -176
rg -o '^\s+name: .*' "$log" 2>/dev/null | head -5 || true77
}79
echo "=============================================================="80
echo "LEG 1 (product) -- the inspector evaluates author expressions."81
echo "The §0-named sabotage: author code runs during inspection."82
echo "=============================================================="83
apply "$inspect" \84
"expression: (if (and (not command?) (eq? head 'expr)) (caddr node) #f)" \85
"expression: (if (and (not command?) (eq? head 'expr)) (begin (eval (syntax->datum (caddr node))) (caddr node)) #f)"86
witness_log="$work/leg1-witness.log"87
set +e88
setsid -w "$sigil" -L src -L test eval -f test/sd-p4-witness-probe.sgl >"$witness_log" 2>&189
set -e90
if rg -q 'SDP4-WITNESS-' "$witness_log"; then91
echo "LEG 1 RED (correct): author markers appeared during passive inspection:"92
rg -o 'SDP4-WITNESS-[A-Z-]*' "$witness_log" | sort -u93
else94
echo "LEG 1 FAILED TO GO RED -- the witness cannot see execution" >&295
cat "$witness_log" >&2; exit 196
fi97
restore99
echo "=============================================================="100
echo "LEG 2 (product) -- a node's end span is off by one."101
echo "=============================================================="102
apply "$inspect" "(define (node-end node) (list-ref node 4))" \103
"(define (node-end node) (- (list-ref node 4) 1))"104
expect_suite_red "LEG 2" "$suite" "$work/leg2.log"105
restore107
echo "=============================================================="108
echo "LEG 3 (product) -- the location index counts SCALARS, not UTF-8"109
echo "bytes. The round-trip arm stays green under this; only the"110
echo "byte/scalar arm can see it. That is why they are separate arms."111
echo "=============================================================="112
apply "$parser" \113
" (cond ((<= n #x7f) 1) ((<= n #x7ff) 2) ((<= n #xffff) 3) (else 4))))" \114
" (cond ((<= n #x7f) 1) ((<= n #x7ff) 1) ((<= n #xffff) 1) (else 1))))"115
expect_suite_red "LEG 3" "$suite" "$work/leg3.log"116
restore118
echo "=============================================================="119
echo "LEG 4 (product) -- restore the swallowed delegated diagnostic."120
echo "=============================================================="121
apply "$parser" \122
" (if delegated-failure123
(string-append124
reason125
\"; while delimiting this argument group the standard reader reported: \"126
(exception-message delegated-failure)127
\" (a '(' named there is this parser's substitution for the opening '[')\")128
reason)))" \129
" reason))"130
expect_suite_red "LEG 4" "$suite" "$work/leg4.log"131
restore133
echo "=============================================================="134
echo "LEG 5 (product) -- reinstate MODULE-LEVEL parser state. The"135
echo "exact defect I3 names: a second parse observes the first."136
echo "=============================================================="137
apply "$parser" \138
" (define (parse-document-nodes source source-name header-end)139
(let* ((state (make-parser-state source source-name))" \140
" (define cached-parser-state #f)141
(define (parse-document-nodes source source-name header-end)142
(let* ((state (or cached-parser-state143
(let ((fresh (make-parser-state source source-name)))144
(set! cached-parser-state fresh)145
fresh)))"146
expect_suite_red "LEG 5" "$suite" "$work/leg5.log"147
# The grep arm must independently see the reinstated module-level assignment.148
rg -q '\(set! ' "$parser" && echo "LEG 5: gate ARM 03's set! probe also fires (correct)"149
restore151
echo "=============================================================="152
echo "LEG 6 (product) -- perturb the per-parse step count, so the"153
echo "assertion MOVED off the old module-level telemetry global is"154
echo "shown to be able to fail in its new location."155
echo "=============================================================="156
apply "$parser" "scalars: scalars byte-length: byte steps: visited)" \157
"scalars: scalars byte-length: byte steps: (+ visited 1))"158
expect_suite_red "LEG 6a (passive suite)" "$suite" "$work/leg6a.log"159
expect_suite_red "LEG 6b (re-pointed D1 assertion)" "$reader" "$work/leg6b.log"160
restore162
echo "=============================================================="163
echo "LEG 7 (assertion layer) -- invert the byte-span comparison in"164
echo "the harness itself, proving the harness notices the harness."165
echo "=============================================================="166
apply "$suite" \167
" (assert-equal (byte-offset source start) (srcloc-position loc))" \168
" (assert-equal (+ 1 (byte-offset source start)) (srcloc-position loc))"169
expect_suite_red "LEG 7" "$suite" "$work/leg7.log"170
restore172
echo "=============================================================="173
echo "LEG 8 (product) -- restore a MODULE-LEVEL SHARED LOCATION INDEX"174
echo "spelled WITHOUT set!, exactly as the adversarial review did to"175
echo "defeat the first version of these gates. Every other arm stayed"176
echo "green under this; the discriminating test must go red."177
echo "=============================================================="178
apply "$parser" \179
" (define (position-at state end) (vector-ref (parser-state-bytes state) end))180
(define (cursor-at state end)181
(list (vector-ref (parser-state-lines state) end)182
(vector-ref (parser-state-columns state) end)))" \183
" (define shared-index-cell (make-vector 1 #f))184
(define (shared-index state) (or (vector-ref shared-index-cell 0) state))185
(define (position-at state end) (vector-ref (parser-state-bytes (shared-index state)) end))186
(define (cursor-at state end)187
(list (vector-ref (parser-state-lines (shared-index state)) end)188
(vector-ref (parser-state-columns (shared-index state)) end)))"189
apply "$parser" \190
" (parser-state source: source source-name: source-name191
bytes: bytes lines: lines columns: columns192
scalars: scalars byte-length: byte steps: visited)" \193
" (let ((fresh (parser-state source: source source-name: source-name194
bytes: bytes lines: lines columns: columns195
scalars: scalars byte-length: byte steps: visited)))196
(vector-set! shared-index-cell 0 fresh)197
fresh)"198
# The `set!` grep is deliberately expected to stay BLIND here -- that is the199
# review's point, and the reason the behavioural test above had to exist.200
if rg -q '\(set!' "$parser"; then201
echo "LEG 8: unexpected -- this sabotage was supposed to contain no set!" >&2; exit 1202
fi203
echo "LEG 8: gate ARM 03's set! grep is BLIND to this defect (0 matches), as expected."204
expect_suite_red "LEG 8" "$suite" "$work/leg8.log"205
restore207
echo208
echo "All files restored; hashes verified against the pristine copies."209
for f in "$parser" "$inspect" "$suite" "$reader"; do210
printf '%s %s\n' "$(hash_of "$f")" "$f"211
done212
echo "SD_P4_SABOTAGE_COMPLETE"