#!/bin/bash set -euo pipefail cd "$(dirname "$0")/.." [[ $# -eq 0 ]] || { printf 'Usage: %s\n' "$0" >&2; exit 2; } process_status=0 pgrep -x Parcel > /dev/null || process_status=$? case "$process_status" in 0) printf 'Quit your running Parcel first; leaks only inspects the soak harness-owned app.\n' >&2; exit 2 ;; 1) ;; *) printf 'Could not inspect running Parcel processes.\n' >&2; exit "$process_status" ;; esac mkdir -p build/leaks work=$(mktemp -d "$PWD/build/leaks/run.XXXXXX") runner_pid= finish_runner() { local deadline=$((SECONDS + 130)) while kill -0 "$runner_pid" 2>/dev/null; do if (( SECONDS >= deadline )); then printf 'Harness failed to exit after continuation; terminating only the owned runner.\n' >&2 kill -TERM "$runner_pid" 2>/dev/null || true deadline=$((SECONDS + 5)) while kill -0 "$runner_pid" 2>/dev/null && (( SECONDS < deadline )); do sleep 0.5; done if kill -0 "$runner_pid" 2>/dev/null; then kill -KILL "$runner_pid" 2>/dev/null || true; fi wait "$runner_pid" || true return 2 fi sleep 0.5 done wait "$runner_pid" } cleanup() { # Release the harness on tool failure as well as success; the harness owns app cleanup. touch "$work/continue" if [[ -n $runner_pid ]]; then finish_runner || true; fi } trap cleanup EXIT wait_seconds=${PARCEL_LEAK_WAIT_SECONDS:-1800} [[ $wait_seconds =~ ^[0-9]+$ ]] || { printf 'PARCEL_LEAK_WAIT_SECONDS must be nonnegative seconds.\n' >&2; exit 2; } wait_seconds=$((10#$wait_seconds)) MallocStackLogging=1 PARCEL_SOAK=1 PARCEL_E2E_INSPECTABLE=1 \ PARCEL_E2E_FILTER='ParcelE2ETests.*SoakTests' \ PARCEL_LEAK_PID_FILE="$work/pid" PARCEL_LEAK_CONTINUE_FILE="$work/continue" \ Scripts/test-e2e.sh debug > "$work/soak.log" 2>&1 & runner_pid=$! started=$SECONDS while [[ ! -s $work/pid ]]; do if ! kill -0 "$runner_pid" 2>/dev/null; then status=0 wait "$runner_pid" || status=$? runner_pid= printf 'Soak exited before its leak handshake (status %s); inspect %s/soak.log.\n' "$status" "$work" >&2 exit 2 fi if (( SECONDS - started >= wait_seconds )); then printf 'Timed out waiting for the soak leak handshake; inspect %s/soak.log.\n' "$work" >&2 exit 2 fi sleep 0.5 done [[ ! -L $work/pid ]] || { printf 'Leak PID file must not be a symlink.\n' >&2; exit 2; } IFS= read -r pid < "$work/pid" [[ $pid =~ ^[1-9][0-9]*$ ]] || { printf 'Invalid harness-owned PID.\n' >&2; exit 2; } process=$(ps -p "$pid" -o comm=) expected="$(pwd -P)/build/Parcel.app/Contents/MacOS/Parcel" [[ $process == "$expected" ]] || { printf 'Handshake PID does not identify the harness bundle executable.\n' >&2; exit 2; } codesign -d --entitlements :- build/Parcel.app > "$work/entitlements.plist" 2> "$work/codesign.stderr" Scripts/parcel-dev.sh inspectable-entitlements "$work/entitlements.plist" || { printf 'Leak inspection requires the explicitly inspectable debug bundle.\n' >&2 exit 2 } status=0 # Report files intentionally belong to the invoking user, not root. # shellcheck disable=SC2024 sudo -n /usr/bin/leaks --outputGraph="$work/parcel.memgraph" "$pid" > "$work/leaks.txt" 2> "$work/leaks.stderr" || status=$? touch "$work/continue" soak_status=0 finish_runner || soak_status=$? runner_pid= if (( status != 0 )); then printf 'leaks failed or found leaks (status %s); inspect %s.\n' "$status" "$work" >&2 exit "$status" fi if (( soak_status != 0 )); then printf 'Soak failed (status %s); inspect %s/soak.log.\n' "$soak_status" "$work" >&2 exit "$soak_status" fi [[ -f $work/parcel.memgraph ]] || { printf 'leaks did not write its requested memory graph.\n' >&2; exit 2; } printf 'Harness-owned debug leak report: %s\n' "$work"