#!/bin/bash
# Build the standalone libyuv CopyPlane overflow POC by linking against the
# in-tree ASAN-built libyuv object files. This exercises the exact code that
# ships in Chrome.
set -e

CHROMIUM_SRC=${CHROMIUM_SRC:-/chromium/chromium/src}
OUT=${OUT:-$CHROMIUM_SRC/out/asan}
CLANG=$CHROMIUM_SRC/third_party/llvm-build/Release+Asserts/bin/clang++
LIBYUV_OBJ=$OUT/obj/third_party/libyuv/libyuv_internal

# Ensure the libyuv objects are up to date.
( cd "$CHROMIUM_SRC" && autoninja -C "$OUT" third_party/libyuv:libyuv_internal )

"$CLANG" \
  -fuse-ld=lld \
  -fsanitize=address -fno-omit-frame-pointer -g -O1 -std=c++17 \
  -I"$CHROMIUM_SRC/third_party/libyuv/include" \
  -DCHROMIUM \
  libyuv_copyplane_poc.cc \
  $(ls "$LIBYUV_OBJ"/*.o | grep -Ev 'jpeg|convert_to_') \
  -o libyuv_copyplane_poc

echo "[+] Built ./libyuv_copyplane_poc"
