examples/union-cc/e2e.sh
#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-only
# Heimdall is a defensive sandbox. The end-to-end test of cargo mimir + cargo heimdall on
# union-cc (a Rust program with C from `cc` in build.rs), every link real:
#
# 1. cargo mimir make --with-c the REAL cargo-mimir-dump, c-mimir-dump and mimir-exec:
# the C half captured and merged, the possible set written
# 2. heimdall profile probe/gen the measured profile of the corpus (the `c` run only)
# 3. heimdall profile union measured + possible set = the union filter
# 4. heimdall profile enforce the program runs under the union filter (KILL on a miss)
# 7. cargo heimdall union the one command: its possible part is the FULL graph (Rust
# + the C half + the binary half): `rare-c`'s getpriority,
# made only in C, is in it without any measurement, and
# `--rust-only` (the Rust graph alone) lacks it and says so
#
# Every check names the exact call or file it wants; the first that fails ends the test RED
# (exit 1) with the reason. A negative control proves the filter is real: the `rare` path,
# which the corpus never runs, dies (SIGSYS) under the measured profile alone and runs under
# the union.
#
# ./e2e.sh [out dir] (default: a new temp dir; it must not exist)
#
# Needs on PATH: cargo-mimir, heimdall and cargo-heimdall. One-file cargo-mimir (nornir-mimir-bin 0.7 on) runs
# every Mímir role itself and needs mimir-driver (and the nightly it is pinned to) beside it;
# an older cargo-mimir needs cargo-mimir-dump (+ mimir-driver), mimir-exec and c-mimir-dump
# (+ its c-mimir-* links, LLVM 23.1.5) on PATH too. The tool image (ci/tools.Containerfile)
# has them all. Leaves the source tree as it was.
set -euo pipefail
HERE=$(cd "$(dirname "$0")" && pwd)
OUT=${1:-$(mktemp -u "${TMPDIR:-/tmp}/union-cc-e2e.XXXXXX")}
[ ! -e "$OUT" ] || { echo "e2e: $OUT exists" >&2; exit 2; }
mkdir -p "$OUT"
OUT=$(cd "$OUT" && pwd)
ARCH=$(uname -m)
LIBC=glibc
step() { echo; echo "== e2e: $*"; }
red() { echo "e2e RED: $*" >&2; exit 1; }
# has <file> <jq-free python check>: the JSON checks run in python3 (in the image).
pyjson() { python3 -I -c "$1" "${@:2}"; }
command -v cargo-mimir > /dev/null || red "cargo-mimir is not on PATH"
command -v cargo-heimdall > /dev/null || red "cargo-heimdall is not on PATH"
if cargo-mimir __role c-mimir-dump --version > /dev/null 2>&1; then
need="heimdall python3"
[ -x "$(dirname "$(command -v cargo-mimir)")/mimir-driver" ] \
|| red "mimir-driver is not beside $(command -v cargo-mimir)"
else
need="cargo-mimir-dump mimir-exec c-mimir-dump heimdall python3"
fi
for t in $need; do
command -v "$t" > /dev/null || red "$t is not on PATH"
done
# The program's main, as Mímir names the instance (`<package>[bin=<crate>]@<version>::main`).
ROOT='union-cc[bin=union_cc]@0.1.0::main'
step "1. cargo mimir make --with-c --root $ROOT"
# A copy of the crate: the build, the capture and the probe write nothing into the source tree.
cp -a "$HERE" "$OUT/src"
rm -rf "$OUT/src/target"
(cd "$OUT/src" && cargo mimir make --with-c --out "$OUT/make" --root "$ROOT") \
|| red "cargo mimir make --with-c failed (exit $?)"
R=$OUT/make/receipt.json
[ -s "$R" ] || red "no receipt $R"
# The program is the one make reports: the receipt's `programs[].elf` (the path cargo gave
# for the artifact, and its sha256). No path is guessed from the target dir: with a scope
# make builds the toolchain's crates from source for `--target <host>`, so cargo puts the
# program under target/<host>/debug, and the instance pass leaves a second file of the
# same name under target/mimir-exec.
pyjson '
import hashlib, json, sys
progs = [p for p in json.load(open(sys.argv[1]))["programs"] if p["name"] == "union-cc"]
assert len(progs) == 1, f"the receipt names the program union-cc {len(progs)} times"
elf = progs[0]["elf"]
path, want = elf["path"], elf["sha256"]
have = hashlib.sha256(open(path, "rb").read()).hexdigest()
assert have == want, f"{path}: sha256 {have}, the receipt says {want}"
print(path)
' "$R" > "$OUT/program-path" || red "receipt: the program make built: $(cat "$OUT/program-path")"
BIN=$(cat "$OUT/program-path")
[ -x "$BIN" ] || red "make built no $BIN"
# The C half: captured, read into c-source, and union_cc_parent in the program's graph with
# its C source (parent.c, a `c` moniker): the binary graph alone has the symbol, never the file.
pyjson '
import json, sys
r = json.load(open(sys.argv[1]))
half, c = r["c_half"], r["c_capture"]
assert half == "captured", f"c_half = {half!r}, wanted captured"
read_exit = c["read_exit"]
assert read_exit == 0, f"c-mimir-dump read exit {read_exit}"
assert c["translation_units"] >= 1, "no C translation unit captured"
progs = r["exec"]["programs"]
assert [p["program"] for p in progs] == ["union-cc"], progs
assert progs[0]["exit"] == 0 and progs[0]["possible"], f"no possible set: {progs[0]}"
print(progs[0]["possible"]["path"])
' "$R" > "$OUT/possible-path" || red "receipt: $(cat "$OUT/possible-path")"
POSSIBLE=$(cat "$OUT/possible-path")
[ -s "$POSSIBLE" ] || red "no possible set $POSSIBLE"
pyjson '
import json, sys
fns = [f for f in json.load(open(sys.argv[1]))["functions"] if f["name"] == "union_cc_parent"]
assert len(fns) == 1, f"union_cc_parent in the graph {len(fns)} times"
file, moniker = fns[0]["source_file"], fns[0]["source_moniker"] or ""
assert file == "parent.c", f"union_cc_parent source_file {file!r}: the C half is not merged"
assert moniker.startswith("c "), f"union_cc_parent moniker {moniker!r}"
' "$OUT/make/graph/union-cc/report.json" || red "the C half is not in the graph of union-cc"
step "2. the possible set names the rare path's calls"
pyjson '
import json, sys
calls = set(json.load(open(sys.argv[1]))["syscalls"])
missing = [c for c in ("mkdir", "rmdir") if c not in calls]
assert not missing, f"possible set lacks {missing}; it has {sorted(calls)}"
' "$POSSIBLE" || red "possible set"
step "3. heimdall profile probe + gen (the corpus: the c run)"
cp -a "$HERE/profiles" "$OUT/profiles"
# --binary: the probe measures the program make reported (the workload's own `[binary]`
# line is where a plain `cargo build` puts it).
heimdall profile probe union-cc --profiles "$OUT/profiles" --libc $LIBC --binary "$BIN" \
|| red "heimdall profile probe failed"
heimdall profile gen union-cc --profiles "$OUT/profiles" --libc $LIBC || red "heimdall profile gen failed"
MEASURED=$OUT/profiles/union-cc/$ARCH-$LIBC.json
[ -s "$MEASURED" ] || red "no measured profile $MEASURED"
pyjson '
import json, sys
calls = {r["syscall"] for r in json.load(open(sys.argv[1]))["rules"]}
assert "getppid" in calls, f"the measured profile lacks getppid (the C call); it has {sorted(calls)}"
extra = [c for c in ("mkdir", "rmdir", "getpriority") if c in calls]
assert not extra, f"the measured profile already has {extra}: the control proves nothing"
' "$MEASURED" || red "measured profile"
step "4. heimdall profile union"
heimdall profile union --measured "$MEASURED" --possible "$POSSIBLE" --out "$OUT/union" \
|| red "heimdall profile union failed"
UNION=$OUT/union/$ARCH-$LIBC.json
pyjson '
import json, sys
calls = {r["syscall"] for r in json.load(open(sys.argv[1]))["rules"]}
missing = [c for c in ("getppid", "mkdir", "rmdir") if c not in calls]
assert not missing, f"the union lacks {missing}"
' "$UNION" || red "union filter"
for f in "$OUT/union/$ARCH-$LIBC.rs" "$OUT/union/$ARCH-$LIBC.oci.json"; do
[ -s "$f" ] || red "union wrote no $f"
done
step "5. the program runs under the union filter"
out=$(heimdall profile enforce --profile "$UNION" -- "$BIN" c) || red "union-cc c under the union: exit $?"
case "$out" in "union-cc: parent "*) ;; *) red "union-cc c printed '$out'";; esac
heimdall profile enforce --profile "$UNION" -- "$BIN" rare "$OUT/rare-dir" \
|| red "union-cc rare under the union: exit $? (the graph's calls are not enough)"
[ ! -e "$OUT/rare-dir" ] || red "union-cc rare left $OUT/rare-dir"
step "6. negative control: the rare path under the measured profile alone dies"
set +e
heimdall profile enforce --profile "$MEASURED" -- "$BIN" rare "$OUT/rare-dir-2"
rc=$?
set -e
[ "$rc" = 159 ] || red "union-cc rare under the measured profile: exit $rc, wanted 159 (SIGSYS)"
step "7. cargo heimdall union: the possible part is the full graph (Rust + C half + binary half)"
cargo heimdall union union-cc --bin union-cc --libc $LIBC --graph "$OUT/full-graph" \
--out "$OUT/full-union" --workspace "$OUT/src" --profiles "$OUT/profiles" \
2> "$OUT/full-union.log" || { cat "$OUT/full-union.log" >&2; red "cargo heimdall union failed"; }
FULL=$OUT/full-union/$ARCH-$LIBC.json
# graph src names the set by Mímir's program name (`_` for `-`)
FULL_POSSIBLE=$(ls "$OUT"/full-graph/union[-_]cc.full.possible.json 2> /dev/null | head -1)
[ -s "$FULL_POSSIBLE" ] || red "graph src wrote no full possible set in $OUT/full-graph"
pyjson '
import json, sys
half = json.load(open(sys.argv[1])).get("c_half")
assert half == "captured", f"graph src c_half = {half!r}, wanted captured"
have = sorted(json.load(open(sys.argv[2]))["syscalls"])
assert "getpriority" in have, f"the full possible set lacks getpriority (C only); it has {have}"
calls = {r["syscall"] for r in json.load(open(sys.argv[3]))["rules"]}
missing = [c for c in ("getppid", "mkdir", "rmdir", "getpriority") if c not in calls]
assert not missing, f"the full union lacks {missing}"
' "$OUT/full-graph/graph-src.json" "$FULL_POSSIBLE" "$FULL" \
|| red "the full union"
out=$(heimdall profile enforce --profile "$FULL" -- "$BIN" rare-c) || red "union-cc rare-c under the full union: exit $?"
case "$out" in "union-cc: priority "*) ;; *) red "union-cc rare-c printed '$out'";; esac
step "8. control: --rust-only lacks the C-only call and says so"
cargo heimdall union union-cc --bin union-cc --libc $LIBC --graph "$OUT/rust-graph" \
--out "$OUT/rust-union" --workspace "$OUT/src" --profiles "$OUT/profiles" --rust-only \
2> "$OUT/rust-union.log" || { cat "$OUT/rust-union.log" >&2; red "cargo heimdall union --rust-only failed"; }
grep -q -- "--rust-only" "$OUT/rust-union.log" || red "--rust-only printed no note"
pyjson '
import json, sys
calls = {r["syscall"] for r in json.load(open(sys.argv[1]))["rules"]}
assert "getpriority" not in calls, "the Rust graph alone has getpriority: the control proves nothing"
' "$OUT/rust-union/$ARCH-$LIBC.json" || red "--rust-only union"
set +e
heimdall profile enforce --profile "$OUT/rust-union/$ARCH-$LIBC.json" -- "$BIN" rare-c
rc=$?
set -e
[ "$rc" = 159 ] || red "union-cc rare-c under the Rust-only union: exit $rc, wanted 159 (SIGSYS)"
echo
echo "e2e GREEN: make --with-c -> probe -> gen -> union -> enforce; cargo heimdall union full vs --rust-only ($OUT)"