Skip to content

Commit 5aeebba

Browse files
authored
Merge pull request DeusData#2197 from astandrik/fix/1696-memlab-runtime-isolation
fix(test): isolate memlab harness daemon runtime
2 parents 6cd1900 + 823e720 commit 5aeebba

3 files changed

Lines changed: 118 additions & 5 deletions

File tree

‎scripts/memlab.sh‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ if [ ! -x "$BINARY" ]; then
2929
exit 2
3030
fi
3131

32+
# The profiled process must reach a daemon rendezvous and cache this run owns:
33+
# only CBM_RUNTIME_DIR moves the rendezvous, so a private CBM_CACHE_DIR alone
34+
# joined the operator's account daemon (#1691, #1696). The helper also stops
35+
# that daemon before its cache is removed and leaves the root for diagnosis
36+
# when it will not stop.
37+
# shellcheck source=test-runtime.sh
38+
source "$(dirname "$0")/test-runtime.sh"
39+
cbm_test_runtime_init || exit 1
40+
trap 'cbm_test_runtime_cleanup "$BINARY"' EXIT
41+
3242
WORK=$(mktemp -d 2>/dev/null || mktemp -d -t memlab)
3343

3444
# Native Windows: the server walks the full ancestor chain of both the binary
@@ -80,7 +90,12 @@ PROFILE_OUT="$PWD/memlab-${LABEL}.jsonl"
8090
RUN_LOG="$PWD/memlab-${LABEL}.log"
8191
rm -f "$PROFILE_OUT" "$RUN_LOG"
8292

83-
cleanup() { rm -rf "$WORK" 2>/dev/null || true; rm -rf "${WIN_ROOT:-}" 2>/dev/null || true; }
93+
cleanup() {
94+
# The helper probes with $BINARY, whose native-Windows copy lives in $WORK.
95+
cbm_test_runtime_cleanup "$BINARY"
96+
rm -rf "$WORK" 2>/dev/null || true
97+
rm -rf "${WIN_ROOT:-}" 2>/dev/null || true
98+
}
8499
trap cleanup EXIT
85100

86101
# A fixed corpus: same file count and content on every platform, so a
@@ -113,12 +128,15 @@ done
113128
echo "=== memlab: binary=$BINARY requests=$REQUESTS label=$LABEL ==="
114129
echo "corpus: $(find "$CORPUS" -name '*.py' | wc -l | tr -d ' ') files"
115130

116-
# The Windows binary needs a native path here; an msys /c/... path is not one.
117-
if command -v cygpath >/dev/null 2>&1 && ! command -v winepath >/dev/null 2>&1; then
131+
# The native Windows binary needs a native path here, under its stamped root;
132+
# an msys /c/... path is not one. Everywhere else the helper's owner-only cache
133+
# (already exported as CBM_CACHE_DIR) is the cache.
134+
if [ -n "${WIN_ROOT:-}" ]; then
118135
mkdir -p "$WORK/cache"
119136
export CBM_CACHE_DIR="$(cygpath -w "$WORK/cache")"
137+
CACHE_HOST="$WORK/cache"
120138
else
121-
export CBM_CACHE_DIR="$WORK/cache"
139+
CACHE_HOST="$CBM_TEST_CACHE_DIR_HOST"
122140
fi
123141
export CBM_MEMWASTE=1
124142
export CBM_MEMWASTE_OUT="$PROFILE_OUT"
@@ -149,7 +167,7 @@ python3 "$(dirname "$0")/memlab-drive.py" "$DRIVE_BINARY" "$DRIVE_CORPUS" "$REQU
149167
RC=$?
150168
# With CBM_CACHE_DIR set the process logs to its own file rather than stderr,
151169
# so fold that in or the census series is invisible.
152-
cat "$WORK"/cache/logs/*.log >> "$RUN_LOG" 2>/dev/null || true
170+
cat "$CACHE_HOST"/logs/*.log >> "$RUN_LOG" 2>/dev/null || true
153171
cat "$WORK/server-stderr.log" >> "$RUN_LOG" 2>/dev/null || true
154172
RESPONSES=$(sed -n "s/.*served=\\([0-9]*\\).*/\\1/p" "$WORK/drive.out" | head -1); RESPONSES=${RESPONSES:-0}
155173
CENSUS=$(grep -c "mem.census" "$RUN_LOG" 2>/dev/null | head -1); CENSUS=${CENSUS:-0}

‎scripts/test.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ bash "$ROOT/tests/test_makefile_ts_runtime_dependencies.sh"
230230
echo "=== Step 0g: security fuzz harness self-test ==="
231231
bash "$ROOT/tests/test_security_fuzz_harness.sh"
232232

233+
echo "=== Step 0g2: memlab harness runtime isolation contract (#1696) ==="
234+
bash "$ROOT/tests/test_memlab_runtime_isolation_contract.sh"
235+
233236
echo "=== Step 0h: smoke release-fixture contract ==="
234237
bash "$ROOT/tests/test_smoke_fixture_contract.sh"
235238

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)