Skip to content

Commit 823e720

Browse files
committed
fix(test): isolate memlab harness daemon runtime
scripts/memlab.sh gave the profiled run a private CBM_CACHE_DIR only, so the process joined the operator's account daemon and cleanup removed the cache without stopping any daemon. Source scripts/test-runtime.sh for a harness-owned runtime and cache, and stop the private daemon before the work directory is removed. Add tests/test_memlab_runtime_isolation_contract.sh, which fails before this change, and wire it into scripts/test.sh. Part of DeusData#1696. Signed-off-by: Anton Standrik <astandrik@yandex-team.ru>
1 parent 339b3f4 commit 823e720

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
@@ -24,6 +24,16 @@ if [ ! -x "$BINARY" ]; then
2424
exit 2
2525
fi
2626

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

2939
# Native Windows: the server walks the full ancestor chain of both the binary
@@ -75,7 +85,12 @@ PROFILE_OUT="$PWD/memlab-${LABEL}.jsonl"
7585
RUN_LOG="$PWD/memlab-${LABEL}.log"
7686
rm -f "$PROFILE_OUT" "$RUN_LOG"
7787

78-
cleanup() { rm -rf "$WORK" 2>/dev/null || true; rm -rf "${WIN_ROOT:-}" 2>/dev/null || true; }
88+
cleanup() {
89+
# The helper probes with $BINARY, whose native-Windows copy lives in $WORK.
90+
cbm_test_runtime_cleanup "$BINARY"
91+
rm -rf "$WORK" 2>/dev/null || true
92+
rm -rf "${WIN_ROOT:-}" 2>/dev/null || true
93+
}
7994
trap cleanup EXIT
8095

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

111-
# The Windows binary needs a native path here; an msys /c/... path is not one.
112-
if command -v cygpath >/dev/null 2>&1 && ! command -v winepath >/dev/null 2>&1; then
126+
# The native Windows binary needs a native path here, under its stamped root;
127+
# an msys /c/... path is not one. Everywhere else the helper's owner-only cache
128+
# (already exported as CBM_CACHE_DIR) is the cache.
129+
if [ -n "${WIN_ROOT:-}" ]; then
113130
mkdir -p "$WORK/cache"
114131
export CBM_CACHE_DIR="$(cygpath -w "$WORK/cache")"
132+
CACHE_HOST="$WORK/cache"
115133
else
116-
export CBM_CACHE_DIR="$WORK/cache"
134+
CACHE_HOST="$CBM_TEST_CACHE_DIR_HOST"
117135
fi
118136
export CBM_MEM_PROFILE=1
119137
export CBM_MEM_PROFILE_OUT="$PROFILE_OUT"
@@ -139,7 +157,7 @@ python3 "$(dirname "$0")/memlab-drive.py" "$DRIVE_BINARY" "$DRIVE_CORPUS" "$REQU
139157
RC=$?
140158
# With CBM_CACHE_DIR set the process logs to its own file rather than stderr,
141159
# so fold that in or the census series is invisible.
142-
cat "$WORK"/cache/logs/*.log >> "$RUN_LOG" 2>/dev/null || true
160+
cat "$CACHE_HOST"/logs/*.log >> "$RUN_LOG" 2>/dev/null || true
143161
cat "$WORK/server-stderr.log" >> "$RUN_LOG" 2>/dev/null || true
144162
RESPONSES=$(sed -n "s/.*served=\\([0-9]*\\).*/\\1/p" "$WORK/drive.out" | head -1); RESPONSES=${RESPONSES:-0}
145163
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
@@ -227,6 +227,9 @@ bash "$ROOT/tests/test_makefile_ts_runtime_dependencies.sh"
227227
echo "=== Step 0g: security fuzz harness self-test ==="
228228
bash "$ROOT/tests/test_security_fuzz_harness.sh"
229229

230+
echo "=== Step 0g2: memlab harness runtime isolation contract (#1696) ==="
231+
bash "$ROOT/tests/test_memlab_runtime_isolation_contract.sh"
232+
230233
echo "=== Step 0h: smoke release-fixture contract ==="
231234
bash "$ROOT/tests/test_smoke_fixture_contract.sh"
232235

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)