#!/bin/bash
# Reproduce: ANGLE-GL backend integer-renderbuffer robust-init bypass.
#
# The bug lives in ANGLE's GL backend, so --use-angle=gl is required (Vulkan /
# Metal / D3D backends use different robust-init code).  Observable disclosure
# additionally requires a hardware GPU whose driver hands out un-zeroed VRAM
# and follows the GL spec by leaving integer color attachments untouched on
# glClear() (Mali, Adreno, Apple GL, various desktop drivers).

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

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

timeout 30 "$CHROME" \
  --no-sandbox --no-first-run --no-default-browser-check \
  --disable-gpu-sandbox --ignore-gpu-blocklist \
  --use-gl=angle --use-angle=gl \
  --enable-logging=stderr --v=0 \
  --user-data-dir="$PROFILE" \
  --headless=new \
  "http://127.0.0.1:$PORT/poc.html" >"$LOG" 2>&1 || true

grep -E 'POC-TRACE|\[POC\]' "$LOG"
echo
echo "Full log: $LOG"
