#!/bin/bash
# Reproduce the ANGLE-Vulkan stageSelfAsSubresourceUpdates uninitialised-memory leak.
DIR="$(cd "$(dirname "$0")" && pwd)"
CHROME="${CHROME:-/chromium/chromium/src/out/asan/chrome}"
PORT="${PORT:-8123}"
PROFILE=$(mktemp -d /tmp/chrome-poc-profile.XXXXXX)
RAW=$(mktemp /tmp/chrome-poc-raw.XXXXXX)

python3 "$DIR/server.py" "$PORT" > /dev/null 2>&1 &
SRV=$!
trap 'kill $SRV 2>/dev/null; rm -rf "$PROFILE" "$RAW"' EXIT
sleep 1

# --use-angle=swiftshader selects the ANGLE Vulkan backend over SwiftShader's
# software Vulkan ICD. On Android and ChromeOS the Vulkan backend is the
# production default; on a headless Linux box this merely substitutes for the
# missing hardware ICD and does not change the bug-triggering ANGLE code path.
# On a machine with a real Vulkan driver use --use-angle=vulkan instead.
timeout 90 "$CHROME" \
  --no-sandbox --no-first-run --no-default-browser-check \
  --disable-gpu-sandbox --in-process-gpu \
  --use-gl=angle --use-angle=swiftshader --ignore-gpu-blocklist \
  --enable-logging=stderr --v=0 \
  --user-data-dir="$PROFILE" \
  --headless=new --virtual-time-budget=10000 \
  "$@" \
  "http://127.0.0.1:$PORT/poc.html" > "$RAW" 2>&1

grep -vE "dbus/|object_proxy|registration_request|dbind|account_consistency|vaapi_wrapper|idle_linux|dawn/|maxDynamic|Found no drivers|UPower|OAuth|ScreenSaver|gcm|inotify" \
  "$RAW" | tee "$DIR/chrome-output.log"
