Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuzztest clean-ups #2529

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/workflows/ci-fuzztest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ jobs:
codec-dav1d: "LOCAL"
libxml2: "LOCAL"
libyuv: ${{ matrix.libyuv }}
- name: Build fuzztest
if: steps.setup.outputs.ext-cache-hit != 'true'
working-directory: ./ext
run: bash -e fuzztest.cmd

- name: Prepare libavif (cmake)
run: >
Expand All @@ -52,7 +48,6 @@ jobs:
-DAVIF_ENABLE_EXPERIMENTAL_YCGCO_R=ON
-DAVIF_ENABLE_EXPERIMENTAL_MINI=ON
-DAVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM=ON
-DAVIF_ENABLE_FUZZTEST=ON
-DAVIF_LOCAL_FUZZTEST=ON
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
-DAVIF_ENABLE_WERROR=ON
Expand Down
7 changes: 2 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ option(
"Build tests that compare encoding outputs to golden files. Needs AVIF_BUILD_APPS=ON and AVIF_BUILD_TESTS=ON, and depends on MP4box which can be built with ext/mp4box.sh"
OFF
)
option(AVIF_ENABLE_FUZZTEST "Build avif fuzztest targets. Requires Google FuzzTest. Has no effect unless AVIF_BUILD_TESTS is ON."
OFF
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering why you didn't use the AVIF_FUZZTEST=SYSTEM/LOCAL/OFF convention for FuzzTest.

option(
AVIF_LOCAL_FUZZTEST
"Build the Google FuzzTest framework by providing your own copy of the repo in ext/fuzztest (see Local Builds in README). CMake must be at least 3.25."
Expand Down Expand Up @@ -570,7 +567,7 @@ if(NOT AVIF_CODEC_AOM_ENABLED
message(WARNING "libavif: No decoding library is enabled.")
endif()

if(AVIF_LIB_USE_CXX OR AVIF_BUILD_APPS OR (AVIF_BUILD_TESTS AND (AVIF_ENABLE_FUZZTEST OR AVIF_GTEST)))
if(AVIF_LIB_USE_CXX OR AVIF_BUILD_APPS OR (AVIF_BUILD_TESTS AND (AVIF_LOCAL_FUZZTEST OR AVIF_GTEST)))
enable_language(CXX)
if(AVIF_ENABLE_NODISCARD)
# [[nodiscard]] requires C++17.
Expand Down Expand Up @@ -647,7 +644,7 @@ if(NOT SKIP_INSTALL_ALL)
include(GNUInstallDirs)
endif()

if(AVIF_BUILD_APPS OR (AVIF_BUILD_TESTS AND (AVIF_ENABLE_FUZZTEST OR AVIF_GTEST)))
if(AVIF_BUILD_APPS OR (AVIF_BUILD_TESTS AND (AVIF_LOCAL_FUZZTEST OR AVIF_GTEST)))
if(AVIF_ZLIBPNG STREQUAL "OFF")
message(FATAL_ERROR "libavif: AVIF_ZLIBPNG cannot be OFF when AVIF_BUILD_APPS or AVIF_BUILD_TESTS is ON")
elseif(AVIF_ZLIBPNG STREQUAL "SYSTEM")
Expand Down
32 changes: 32 additions & 0 deletions cmake/Modules/LocalFuzztest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
set(AVIF_LOCAL_FUZZTEST_TAG "078ea0871cc96d3a69bad406577f176a4fa14ae9")

set(FUZZTEST_SOURCE_DIR "${AVIF_SOURCE_DIR}/ext/fuzztest")

if(EXISTS "${FUZZTEST_SOURCE_DIR}")
message(STATUS "libavif(AVIF_LOCAL_FUZZTEST=ON): folder found at ${FUZZTEST_SOURCE_DIR}")
set(FUZZTEST_BINARY_DIR "${FUZZTEST_SOURCE_DIR}/build.libavif")
else()
message(STATUS "libavif(AVIF_LIBYUV=LOCAL): compiled library not found at ${LIB_FILENAME}; using FetchContent")

message(CHECK_START "libavif(AVIF_LOCAL_FUZZTEST=ON): configuring fuzztest")

set(FUZZTEST_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/fuzztest-src")
set(FUZZTEST_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/fuzztest-build")
FetchContent_Declare(
fuzztest
GIT_REPOSITORY "https://github.com/google/fuzztest.git"
BINARY_DIR "${FUZZTEST_BINARY_DIR}"
GIT_TAG "${AVIF_LOCAL_FUZZTEST_TAG}"
# Fixes for https://github.com/google/fuzztest/issues/1124
PATCH_COMMAND
sed -i.bak -e "s/-fsanitize=address//g" cmake/FuzzTestFlagSetup.cmake && sed -i.bak -e "s/-DADDRESS_SANITIZER//g"
cmake/FuzzTestFlagSetup.cmake &&
# Fixes for https://github.com/google/fuzztest/issues/1125
sed -i.bak -e "s/if (IsEnginePlaceholderInput(data))/if (data.size() == 0)/" fuzztest/internal/compatibility_mode.cc
&& sed -i.bak -e "s/set(GTEST_HAS_ABSL ON)/set(GTEST_HAS_ABSL OFF)/" cmake/BuildDependencies.cmake
)

message(CHECK_PASS "complete")
endif()

avif_fetchcontent_populate_cmake(fuzztest)
4 changes: 2 additions & 2 deletions ext/fuzztest.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ git checkout 078ea0871cc96d3a69bad406577f176a4fa14ae9
sed -i 's/-fsanitize=address//g' ./cmake/FuzzTestFlagSetup.cmake
sed -i 's/-DADDRESS_SANITIZER//g' ./cmake/FuzzTestFlagSetup.cmake
: # Fixes for https://github.com/google/fuzztest/issues/1125
sed -i 's/if (IsEnginePlaceholderInput(data)) return;/if (data.size() == 0) return;/g' ./fuzztest/internal/compatibility_mode.cc
sed -i 's/set(GTEST_HAS_ABSL ON)/set(GTEST_HAS_ABSL OFF)/g' ./cmake/BuildDependencies.cmake
sed -i 's/if (IsEnginePlaceholderInput(data))/if (data.size() == 0)/' ./fuzztest/internal/compatibility_mode.cc
sed -i 's/set(GTEST_HAS_ABSL ON)/set(GTEST_HAS_ABSL OFF)/' ./cmake/BuildDependencies.cmake

: # fuzztest is built by the main CMake project through add_subdirectory as recommended at:
: # https://github.com/google/fuzztest/blob/main/doc/quickstart-cmake.md
47 changes: 20 additions & 27 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ foreach(AVIFYUV_MODE limited rgb) # Modes drift and premultiply take more than 2
add_test(NAME avifyuv_${AVIFYUV_MODE} COMMAND avifyuv -m ${AVIFYUV_MODE})
endforeach()

if(AVIF_ENABLE_FUZZTEST OR AVIF_GTEST OR AVIF_BUILD_APPS)
if(AVIF_LOCAL_FUZZTEST OR AVIF_GTEST OR AVIF_BUILD_APPS)
add_library(aviftest_helpers OBJECT gtest/aviftest_helpers.cc)
target_link_libraries(aviftest_helpers PUBLIC avif_apps avif)
target_link_libraries(aviftest_helpers PRIVATE avif_enable_warnings)
Expand Down Expand Up @@ -169,42 +169,35 @@ endif()
################################################################################
# Experimental FuzzTest support (Linux only)

if(AVIF_ENABLE_FUZZTEST)
if(AVIF_LOCAL_FUZZTEST)
# Adds a fuzztest from file TEST_NAME.cc located in the gtest folder. Extra arguments
# are considered as extra source files.
macro(add_avif_fuzztest TEST_NAME)
add_executable(${TEST_NAME} gtest/${TEST_NAME}.cc ${ARGN})
# FuzzTest bundles GoogleTest so no need to link to gtest librairies.
target_link_libraries(${TEST_NAME} PRIVATE avif_fuzztest_helpers aviftest_helpers_internal avif_enable_warnings)
target_link_libraries(${TEST_NAME} PRIVATE avif_fuzztest_helpers aviftest_helpers_internal)
link_fuzztest(${TEST_NAME})
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME} --stack_limit_kb=512)
set_property(TEST ${TEST_NAME} PROPERTY ENVIRONMENT "TEST_DATA_DIRS=${CMAKE_CURRENT_SOURCE_DIR}/data/")
endmacro()

if(AVIF_LOCAL_FUZZTEST)
# Run ext/fuzztest.cmd first.
# Recommended top-level CMake options:
# -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DAVIF_CODEC_DAV1D=ON -DAVIF_ENABLE_WERROR=OFF
# Reproducing a failure can be done by setting the environment variable
# FUZZTEST_REPLAY=/path/to/repro_file.test
# and running one of the targets below.
# See https://github.com/google/fuzztest/blob/main/doc/quickstart-cmake.md
# Note: There are compiler warnings in the FuzzTest headers. Add the
# ext/fuzztest subdirectory with the SYSTEM directory property set to
# true so that warnings in its headers are suppressed.
if(CMAKE_VERSION VERSION_LESS 3.25.0)
message(FATAL_ERROR "CMake must be at least 3.25 to pass the SYSTEM argument to add_subdirectory(), bailing out")
endif()
# Add the fuzztest project. Note this may add some tests which may not be built because of EXCLUDE_FROM_ALL and will
# therefore fail. They can be ignored by adding them to CTestCustom.cmake
# See https://gitlab.kitware.com/cmake/cmake/-/issues/20212
add_subdirectory(${AVIF_SOURCE_DIR}/ext/fuzztest ${AVIF_SOURCE_DIR}/ext/fuzztest/build.libavif EXCLUDE_FROM_ALL SYSTEM)
else()
message(
FATAL_ERROR
"fuzztest: Installed FuzzTest is not supported, please set AVIF_LOCAL_FUZZTEST=ON or AVIF_ENABLE_FUZZTEST=OFF"
)
# Recommended top-level CMake options:
# -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DAVIF_CODEC_DAV1D=ON -DAVIF_ENABLE_WERROR=OFF
# Reproducing a failure can be done by setting the environment variable
# FUZZTEST_REPLAY=/path/to/repro_file.test
# and running one of the targets below.
# See https://github.com/google/fuzztest/blob/main/doc/quickstart-cmake.md
# Note: There are compiler warnings in the FuzzTest headers. Add the
# ext/fuzztest subdirectory with the SYSTEM directory property set to
# true so that warnings in its headers are suppressed.
if(CMAKE_VERSION VERSION_LESS 3.25.0)
message(FATAL_ERROR "CMake must be at least 3.25 to pass the SYSTEM argument to add_subdirectory(), bailing out")
endif()
# Add the fuzztest project. Note this may add some tests which may not be built because of EXCLUDE_FROM_ALL and will
# therefore fail. They can be ignored by adding them to CTestCustom.cmake
# See https://gitlab.kitware.com/cmake/cmake/-/issues/20212
include(LocalFuzztest)

fuzztest_setup_fuzzing_flags()

# Create a library with avif_fuzztest_helpers.cc to compile it only once.
Expand All @@ -220,7 +213,7 @@ if(AVIF_ENABLE_FUZZTEST)
add_avif_fuzztest(avif_fuzztest_read_image)
add_avif_fuzztest(avif_fuzztest_yuvrgb)
else()
message(STATUS "FuzzTest targets are disabled because AVIF_ENABLE_FUZZTEST is OFF.")
message(STATUS "FuzzTest targets are disabled because AVIF_LOCAL_FUZZTEST is OFF.")
endif()

################################################################################
Expand Down
2 changes: 1 addition & 1 deletion tests/CTestCustom.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(CTEST_CUSTOM_TESTS_IGNORE
# Ignore failing tests brought by `add_subdirectory(${AVIF_SOURCE_DIR}/ext/fuzztest)` when AVIF_ENABLE_FUZZTEST is ON
# Ignore failing tests brought by `add_subdirectory(${AVIF_SOURCE_DIR}/ext/fuzztest)` when AVIF_LOCAL_FUZZTEST is ON
antlr4_tests_NOT_BUILT
)
2 changes: 1 addition & 1 deletion tests/oss-fuzz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This document provides links and knowledge about fuzzing libavif on oss-fuzz.
You need to build with the following CMake flags:

```sh
-DAVIF_CODEC_AOM=LOCAL -DAVIF_CODEC_AOM_DECODE=ON -DAVIF_CODEC_AOM_ENCODE=ON -DAVIF_CODEC_DAV1D=LOCAL -DAVIF_LIBYUV=LOCAL -DAVIF_LIBSHARPYUV=LOCAL -DAVIF_BUILD_TESTS=ON -DAVIF_ENABLE_GTEST=ON -DAVIF_GTEST=LOCAL -DAVIF_LOCAL_FUZZTEST=ON -DAVIF_ENABLE_FUZZTEST=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DAVIF_ENABLE_WERROR=OFF
-DAVIF_CODEC_AOM=LOCAL -DAVIF_CODEC_AOM_DECODE=ON -DAVIF_CODEC_AOM_ENCODE=ON -DAVIF_CODEC_DAV1D=LOCAL -DAVIF_LIBYUV=LOCAL -DAVIF_LIBSHARPYUV=LOCAL -DAVIF_BUILD_TESTS=ON -DAVIF_ENABLE_GTEST=ON -DAVIF_GTEST=LOCAL -DAVIF_LOCAL_FUZZTEST=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DAVIF_ENABLE_WERROR=OFF
```

You can then run your tests as normal:
Expand Down
2 changes: 1 addition & 1 deletion tests/oss-fuzz/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ cd build
EXTRA_CMAKE_FLAGS=""
if [[ "$FUZZING_ENGINE" == "libfuzzer" ]]; then
CXXFLAGS="${CXXFLAGS} -DFUZZTEST_COMPATIBILITY_MODE"
EXTRA_CMAKE_FLAGS="${EXTRA_CMAKE_FLAGS} -DAVIF_ENABLE_FUZZTEST=ON -DFUZZTEST_COMPATIBILITY_MODE=libfuzzer"
EXTRA_CMAKE_FLAGS="${EXTRA_CMAKE_FLAGS} -DFUZZTEST_COMPATIBILITY_MODE=libfuzzer"
fi
cmake .. -G Ninja -DBUILD_SHARED_LIBS=OFF -DAVIF_CODEC_AOM=LOCAL -DAVIF_CODEC_DAV1D=LOCAL \
-DAVIF_CODEC_AOM_DECODE=ON -DAVIF_CODEC_AOM_ENCODE=ON \
Expand Down
Loading