#!/usr/bin/env bash
# Run the WebGL2 POC against Chrome built with the validating GLES2 command
# decoder enabled.
#
# Production target: Android, where the validating decoder ships and is the
# default on devices in the passthrough-decoder blocklist (Samsung Galaxy XR /
# Robin XR per the in-source list, plus any Finch-pushed entries).
#
# Desktop reproduction: add `enable_validating_command_decoder = true` to
# out/asan/args.gn, rebuild, and run with --use-cmd-decoder=validating.
set -eu

ARTIFACTS="$(cd "$(dirname "$0")" && pwd)"
CHROME="${CHROME:-/chromium/chromium/src/out/asan/chrome}"
PORT="${PORT:-8765}"
PROFILE="$(mktemp -d /tmp/chrome-poc-profile.XXXXXX)"

cleanup() {
  [[ -n "${SERVER_PID:-}" ]] && kill "$SERVER_PID" 2>/dev/null || true
  rm -rf "$PROFILE"
}
trap cleanup EXIT

cd "$ARTIFACTS"
python3 server.py "$PORT" >/dev/null 2>&1 &
SERVER_PID=$!
sleep 1

# --use-cmd-decoder=validating : select the vulnerable decoder.
# --use-angle=gl               : avoid SwANGLE (which forces passthrough);
#                                on Android use the native GLES driver instead.
# --ignore-gpu-blocklist       : llvmpipe is GPU-blocklisted for WebGL2; the
#                                affected Android devices are not.
# --enable-logging=stderr      : surface GPU-process LOG(ERROR) (the optional
#                                [POC] instrumentation) to stderr.
timeout 30 "$CHROME" \
  --no-sandbox --no-first-run --no-default-browser-check \
  --use-cmd-decoder=validating \
  --use-angle=gl \
  --ignore-gpu-blocklist \
  --enable-logging=stderr --v=0 \
  --user-data-dir="$PROFILE" \
  --headless=new \
  "http://127.0.0.1:$PORT/poc.html" 2>&1 \
  | grep -E '\[POC\]|CONSOLE'
