# AFL++ instruments the parser family under sabre/xml: pinned libxml2 2.15.4 plus PHP ext/xmlreader and ext/libxml.
# sabre/xml itself is interpreted PHP, so its feedback reaches AFL++ only through those native edges.
FROM --platform=linux/arm64 ubuntu@sha256:065aa8626841f16057b78b027b464cd59c4fac36c204c72dcbf48d66f5fa8a96 AS build
ENV DEBIAN_FRONTEND=noninteractive AFL_LLVM_INSTRUMENT=CLASSIC
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
    afl++=4.33c-1.1ubuntu1 ca-certificates clang curl llvm make pkg-config xz-utils \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /work
RUN curl --fail --location --retry 3 https://download.gnome.org/sources/libxml2/2.15/libxml2-2.15.4.tar.xz --output /work/libxml2-2.15.4.tar.xz \
    && echo '98087fd181d9070724f3fbc65c7377db03038eb92bd882374daff44940138821  /work/libxml2-2.15.4.tar.xz' | sha256sum --check \
    && curl --fail --location --retry 3 https://www.php.net/distributions/php-8.5.12.tar.xz --output /work/php-8.5.12.tar.xz \
    && echo '08bc2eba2a588654ade1357174aa646b761556c862dda0678345ee29ed481209  /work/php-8.5.12.tar.xz' | sha256sum --check \
    && mkdir /work/source && tar -xJf /work/libxml2-2.15.4.tar.xz -C /work/source && tar -xJf /work/php-8.5.12.tar.xz -C /work/source
WORKDIR /work/source/libxml2-2.15.4
RUN CC=afl-clang-fast CFLAGS='-O1 -g' \
    ./configure --prefix=/opt/libxml2 --disable-static --without-python --without-zlib --without-lzma --without-iconv --without-icu --without-http --without-debug \
    && make -j8 && make install
WORKDIR /work/source/php-8.5.12
RUN printf 'src:ext/xmlreader/php_xmlreader.c\nsrc:ext/libxml/libxml.c\n' > /work/afl-allowlist.txt
RUN AFL_LLVM_ALLOWLIST=/work/afl-allowlist.txt CC=afl-clang-fast CXX=afl-clang-fast++ CFLAGS='-O1 -g' PKG_CONFIG_PATH=/opt/libxml2/lib/pkgconfig \
    ./configure --prefix=/opt/php --enable-cli --disable-all --with-libxml --enable-xmlreader --without-pcre-jit \
    && AFL_LLVM_ALLOWLIST=/work/afl-allowlist.txt make -j8 && make install

FROM --platform=linux/arm64 ubuntu@sha256:065aa8626841f16057b78b027b464cd59c4fac36c204c72dcbf48d66f5fa8a96 AS sabre
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -qq && apt-get install -y --no-install-recommends ca-certificates curl unzip \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /work
RUN curl --fail --location --retry 3 https://codeload.github.com/sabre-io/xml/legacy.zip/bec83cbe2f4e1e1ca4254ed8d7caa7e32cc74b05 --output /work/xml.zip \
    && echo 'ff780dcb7389d1c8c2974d858140c6bef427b097e84ae1995ead20f1cf5b9fa3  /work/xml.zip' | sha256sum --check \
    && curl --fail --location --retry 3 https://codeload.github.com/sabre-io/uri/legacy.zip/a926c749dddfb289b8a9b5218d16ac06affdc631 --output /work/uri.zip \
    && echo '9adbe8ebafd256c74262e2d38347aa7044eabb0a8954f7ea6ec46d7cfc1b2a5e  /work/uri.zip' | sha256sum --check \
    && unzip -q /work/xml.zip -d /work/x && unzip -q /work/uri.zip -d /work/u \
    && mkdir -p /work/vendor/sabre && mv /work/x/sabre-io-xml-* /work/vendor/sabre/xml && mv /work/u/sabre-io-uri-* /work/vendor/sabre/uri

FROM --platform=linux/arm64 ubuntu@sha256:065aa8626841f16057b78b027b464cd59c4fac36c204c72dcbf48d66f5fa8a96
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -qq && apt-get download afl++=4.33c-1.1ubuntu1 \
    && echo '3a1e9c3421208d4065310e95dd65a6b3920762c115bef0c60133cd58a707a21a  afl++_4.33c-1.1ubuntu1_arm64.deb' | sha256sum --check \
    && apt-get install -y --no-install-recommends ./afl++_4.33c-1.1ubuntu1_arm64.deb \
    && rm -f afl++_4.33c-1.1ubuntu1_arm64.deb && rm -rf /var/lib/apt/lists/*
ENV LD_LIBRARY_PATH=/opt/libxml2/lib
WORKDIR /work
COPY --from=build /opt/libxml2 /opt/libxml2
COPY --from=build /opt/php /opt/php
COPY --from=sabre /work/vendor /work/vendor
COPY projects/xml/php/sabre_xml/autoload.php projects/xml/php/sabre_xml/fuzz-target.php projects/xml/php/sabre_xml/harness.php projects/xml/php/sabre_xml/summary.php /work/
ENTRYPOINT ["/opt/php/bin/php", "/work/fuzz-target.php"]
