# AFL++ Rust overlay generated by scripts/build_afl_rust.py
FROM rust@sha256:f2c004e3d2d8b547e8cf7ffbc29205385f4e71d23e53e4e383065db5e9f822cd AS build
RUN apt-get update -qq && apt-get install -y --no-install-recommends clang llvm-dev g++ make python3 \
    && rm -rf /var/lib/apt/lists/*
RUN cargo install cargo-afl --version 0.18.2 --locked
WORKDIR /work
COPY fuzzing/afl-native/sql/rust/databend_common_ast/Cargo.toml fuzzing/afl-native/sql/rust/databend_common_ast/Cargo.lock /work/
COPY projects/sql/rust/databend_common_ast/src /work/src
COPY fuzzing/afl-native/sql/rust/databend_common_ast/afl_fuzzer.rs /work/src/bin/afl_fuzzer.rs
COPY fuzzing/afl-native/sql/rust/databend_common_ast/fixed_getrandom.c /work/
RUN cargo fetch --locked --target aarch64-unknown-linux-gnu
RUN cargo afl config --build --force \
    && cargo afl build --locked --target aarch64-unknown-linux-gnu --release --bin afl_fuzzer \
    && cp target/aarch64-unknown-linux-gnu/release/afl_fuzzer /work/afl_fuzzer \
    && cc -O2 -shared -fPIC -o /work/fixed_getrandom.so /work/fixed_getrandom.c
FROM ubuntu@sha256:065aa8626841f16057b78b027b464cd59c4fac36c204c72dcbf48d66f5fa8a96
RUN apt-get update -qq && apt-get install -y --no-install-recommends afl++=4.33c-1.1ubuntu1 libstdc++6 \
    && rm -rf /var/lib/apt/lists/*
COPY --from=build /work/afl_fuzzer /work/afl_fuzzer
COPY --from=build /work/fixed_getrandom.so /work/fixed_getrandom.so
ENV AFL_PRELOAD=/work/fixed_getrandom.so
ENTRYPOINT ["/work/afl_fuzzer"]
