#!/bin/bash
# Reproduce: Dawn GL CopyImageSubData blit fallback drops dst.z for cube
# destinations -> wrong faces written -> uninitialised GPU heap read.
#
# Production target: Android devices using WebGPU compatibility mode on the
# Dawn OpenGLES backend with a native ES 3.1 driver (e.g. Mali-G31/G51/G52,
# Adreno 5xx on Android 10-11). The flags below select the equivalent code
# path on a desktop Linux build for reproduction purposes only:
#
#   --use-webgpu-adapter=opengles
#       On Android this backend is selected automatically when Vulkan is
#       unavailable. On desktop Linux it must be selected explicitly.
#
#   --enable-dawn-features=gl_force_es_31_and_no_extensions
#       Pins the ANGLE context to exactly ES 3.1 (the production floor and the
#       version that takes the vulnerable blit fallback). Native ES 3.1 devices
#       hit this naturally.
#
#   --use-angle=swiftshader / --disable-dawn-features=adapter_blocklist
#       Headless environment has no GPU; use SwiftShader and skip the
#       CPU-adapter blocklist entry that exists only to hide software adapters
#       from web content. Real Android hardware adapters are not blocklisted.

set -e
cd "$(dirname "$0")"

CHROME="${CHROME:-/chromium/chromium/src/out/asan/chrome}"
PORT=8080

python3 server.py >server.log 2>&1 &
SERVER_PID=$!
trap 'kill $SERVER_PID 2>/dev/null' EXIT
sleep 1

rm -rf /tmp/chrome-poc-profile

timeout 30 "$CHROME" \
  --headless=new \
  --no-sandbox --disable-gpu-sandbox \
  --no-first-run --no-default-browser-check \
  --use-angle=swiftshader \
  --use-webgpu-adapter=opengles \
  --enable-dawn-features=gl_force_es_31_and_no_extensions \
  --disable-dawn-features=adapter_blocklist \
  --enable-logging=stderr --v=0 \
  --user-data-dir=/tmp/chrome-poc-profile \
  "http://127.0.0.1:${PORT}/poc.html" 2>&1 \
  | grep -E "CONSOLE|Dawn|dawn|opengl|ASAN|Sanitizer|ERROR.*gpu" \
  | grep -v "dbus\|inotify"

echo ""
echo "----- page log (server side) -----"
cat server.log
