# AFL++ overlay generated by scripts/build_afl_native.py
FROM ubuntu@sha256:065aa8626841f16057b78b027b464cd59c4fac36c204c72dcbf48d66f5fa8a96 AS build
RUN apt-get update -qq && apt-get install -y --no-install-recommends afl++=4.33c-1.1ubuntu1 python3 ca-certificates clang curl libclang-rt-21-dev llvm make python3 xz-utils \
 && rm -rf /var/lib/apt/lists/*
WORKDIR /work
RUN curl --fail --location --retry 3 'https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs10080/ghostpdl-10.08.0.tar.xz' -o /work/ghostscript.tar.xz \
 && echo '94943e3c4adb7115d132b3c28b913672f2e62e6a9d802f7e754e716b1634174a  /work/ghostscript.tar.xz' | sha256sum --check \
 && mkdir /work/ghostscript && tar -xJf /work/ghostscript.tar.xz -C /work/ghostscript --strip-components=1
RUN curl --fail --location --retry 3 'https://downloads.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.49/GraphicsMagick-1.3.49.tar.xz' -o /work/source.tar.xz \
 && echo '7efa070dc31116b4315061b39f84bc7181e8b060bf61214ec9af851131af9c81  /work/source.tar.xz' | sha256sum --check \
 && mkdir /work/source && tar -xJf /work/source.tar.xz -C /work/source --strip-components=1
COPY harness.c harness.h replay.c build.sh /work/
RUN python3 -c 'from pathlib import Path; p=Path("/work/build.sh"); s=p.read_text(); s=s.replace("clang++","AFL_CXX").replace("clang","afl-clang-fast").replace("AFL_CXX","afl-clang-fast++").replace(",undefined",",undefined -fno-sanitize=function"); p.write_text(s)' && chmod +x /work/build.sh && /work/build.sh \
 && { clang --version | head -1; llvm-cov --version | head -1; } > /work/toolchain.txt
FROM ubuntu@sha256:065aa8626841f16057b78b027b464cd59c4fac36c204c72dcbf48d66f5fa8a96
ENV LLVM_PROFILE_FILE=/dev/null
RUN apt-get update -qq && apt-get install -y --no-install-recommends afl++=4.33c-1.1ubuntu1 python3 llvm python3 \
 && rm -rf /var/lib/apt/lists/*
WORKDIR /work
COPY --from=build /work/replay /work/fuzzer /work/
COPY --from=build /work/ghostscript/sobin/libgs.so* /work/lib/
COPY --from=build /work/install/lib/GraphicsMagick-1.3.49/config /work/install/lib/GraphicsMagick-1.3.49/config
COPY --from=build /work/install/share/GraphicsMagick-1.3.49/config /work/install/share/GraphicsMagick-1.3.49/config
COPY --from=build /work/toolchain.txt /work/toolchain.txt
COPY run-control.sh /work/run-control.sh
ENV LD_LIBRARY_PATH=/work/lib
RUN chmod +x /work/run-control.sh
ENTRYPOINT ["/work/run-control.sh"]
