|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Runtime-isolation contract for the memlab harness (#1696, follow-up to #1691). |
| 5 | +# |
| 6 | +# scripts/memlab.sh starts the product over stdio to attribute retained memory |
| 7 | +# and then removes its work directory. It used to give the run a private |
| 8 | +# CBM_CACHE_DIR only, but only CBM_RUNTIME_DIR moves the daemon rendezvous |
| 9 | +# (docs/CONFIGURATION.md), so the profiled process joined the operator's |
| 10 | +# account daemon — refused with a cache-root conflict when one was live, or |
| 11 | +# left as the account daemon with its cache deleted underneath it otherwise. |
| 12 | +# |
| 13 | +# memlab launches the product through its Python driver, and the driver hands |
| 14 | +# the product the environment it inherited (memlab-drive.py passes no env= to |
| 15 | +# Popen). Recording the environment at the driver boundary therefore observes |
| 16 | +# exactly what the product receives, on every host — including Windows, whose |
| 17 | +# native Python cannot exec a shell fixture — so a python3 shim stands in for |
| 18 | +# the driver and no product process is started at all. |
| 19 | + |
| 20 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 21 | +WORKDIR="$(mktemp -d)" |
| 22 | +trap 'rm -rf "$WORKDIR"' EXIT |
| 23 | + |
| 24 | +fail() { |
| 25 | + echo "FAIL: $*" >&2 |
| 26 | + exit 1 |
| 27 | +} |
| 28 | + |
| 29 | +normalize_path() { |
| 30 | + local path=${1%$'\r'} |
| 31 | + if command -v cygpath >/dev/null 2>&1; then |
| 32 | + cygpath -u "$path" 2>/dev/null && return 0 |
| 33 | + fi |
| 34 | + printf '%s\n' "${path//\\//}" |
| 35 | +} |
| 36 | + |
| 37 | +if grep -q 'env=' "$ROOT/scripts/memlab-drive.py"; then |
| 38 | + fail "memlab-drive.py no longer passes the harness environment through unchanged" |
| 39 | +fi |
| 40 | + |
| 41 | +SHIM_DIR="$WORKDIR/bin" |
| 42 | +mkdir -p "$SHIM_DIR" |
| 43 | +cat > "$SHIM_DIR/python3" <<'EOF' |
| 44 | +#!/usr/bin/env bash |
| 45 | +printf '%s\t%s\n' "${CBM_CACHE_DIR-}" "${CBM_RUNTIME_DIR-}" >> "$CBM_MEMLAB_ENV_PROBE" |
| 46 | +echo "served=0 failed=0" |
| 47 | +exit 1 |
| 48 | +EOF |
| 49 | +DUMMY_BINARY="$WORKDIR/dummy-binary" |
| 50 | +printf '#!/usr/bin/env bash\nexit 0\n' > "$DUMMY_BINARY" |
| 51 | +chmod +x "$SHIM_DIR/python3" "$DUMMY_BINARY" |
| 52 | + |
| 53 | +CALLER_CACHE="$WORKDIR/caller-cache" |
| 54 | +CALLER_RUNTIME="$WORKDIR/caller-runtime" |
| 55 | +ENV_LOG="$WORKDIR/environment.log" |
| 56 | +mkdir -p "$CALLER_CACHE" "$CALLER_RUNTIME" "$WORKDIR/cwd" |
| 57 | + |
| 58 | +# memlab writes its profile and log into $PWD, hence the cwd change. It is |
| 59 | +# tracked without an executable bit, so run it through bash. |
| 60 | +( |
| 61 | + cd "$WORKDIR/cwd" |
| 62 | + PATH="$SHIM_DIR:$PATH" \ |
| 63 | + CBM_CACHE_DIR="$CALLER_CACHE" \ |
| 64 | + CBM_RUNTIME_DIR="$CALLER_RUNTIME" \ |
| 65 | + CBM_MEMLAB_ENV_PROBE="$ENV_LOG" \ |
| 66 | + bash "$ROOT/scripts/memlab.sh" "$DUMMY_BINARY" 1 probe > "$WORKDIR/memlab.out" 2>&1 || true |
| 67 | +) |
| 68 | + |
| 69 | +[[ -s "$ENV_LOG" ]] || fail "memlab did not reach its driver" |
| 70 | + |
| 71 | +CALLER_CACHE_NORMALIZED=$(normalize_path "$CALLER_CACHE") |
| 72 | +CALLER_RUNTIME_NORMALIZED=$(normalize_path "$CALLER_RUNTIME") |
| 73 | +private_root="" |
| 74 | +while IFS=$'\t' read -r child_cache_raw child_runtime_raw; do |
| 75 | + child_cache=$(normalize_path "$child_cache_raw") |
| 76 | + child_runtime=$(normalize_path "$child_runtime_raw") |
| 77 | + if [[ -z "$child_runtime" || "$child_runtime" == "$CALLER_RUNTIME_NORMALIZED" ]]; then |
| 78 | + fail "memlab exposed the caller CBM_RUNTIME_DIR to the product" |
| 79 | + fi |
| 80 | + if [[ -z "$child_cache" || "$child_cache" == "$CALLER_CACHE_NORMALIZED" ]]; then |
| 81 | + fail "memlab exposed the caller CBM_CACHE_DIR to the product" |
| 82 | + fi |
| 83 | + if [[ "${child_runtime%/*}" != "${child_cache%/*}" || |
| 84 | + "${child_runtime##*/}" != "runtime" || "${child_cache##*/}" != "cache" ]]; then |
| 85 | + fail "memlab runtime/cache were not isolated beneath one private root" |
| 86 | + fi |
| 87 | + private_root="${child_runtime%/*}" |
| 88 | +done < "$ENV_LOG" |
| 89 | + |
| 90 | +[[ ! -e "$private_root" ]] || fail "memlab left its private root behind: $private_root" |
| 91 | + |
| 92 | +echo "PASS: memlab harness isolates its daemon runtime and cache from the caller" |
0 commit comments