From 1e0205a7db51b699ac31910622ff9b83cde4964d Mon Sep 17 00:00:00 2001 From: maryla-uc <91276487+maryla-uc@users.noreply.github.com> Date: Tue, 19 Sep 2023 14:00:15 +0200 Subject: [PATCH] Fuzztest: switch raw ptr to unique_ptr now that fuzztest supports it. (#1597) --- ext/fuzztest.cmd | 2 +- tests/CMakeLists.txt | 2 +- .../avif_fuzztest_enc_dec_experimental.cc | 11 +++--- tests/gtest/avif_fuzztest_helpers.cc | 19 ++++----- tests/gtest/avif_fuzztest_helpers.h | 39 +++++++------------ 5 files changed, 30 insertions(+), 43 deletions(-) diff --git a/ext/fuzztest.cmd b/ext/fuzztest.cmd index bd72b612e8..57f775614d 100755 --- a/ext/fuzztest.cmd +++ b/ext/fuzztest.cmd @@ -10,7 +10,7 @@ git clone https://github.com/google/fuzztest.git cd fuzztest : # There is no tagged release as of 2023/09/18. Pick the last commit. -git checkout 867d851afd3d77bbc0067d4007360a1d9e826a22 +git checkout b5500afbac873c884e1e021ea019ad943434df7d mkdir build.libavif cd build.libavif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 39c2aa50f0..044d1a3ac5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -260,7 +260,7 @@ if(AVIF_ENABLE_GTEST AND AVIF_ENABLE_FUZZTEST) if(AVIF_LOCAL_FUZZTEST) # Run ext/fuzztest.cmd first. # Recommended top-level CMake options: - # -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DAVIF_ENABLE_WERROR=OFF + # -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. diff --git a/tests/gtest/avif_fuzztest_enc_dec_experimental.cc b/tests/gtest/avif_fuzztest_enc_dec_experimental.cc index aac68fc1a3..8d0d1eb4ff 100644 --- a/tests/gtest/avif_fuzztest_enc_dec_experimental.cc +++ b/tests/gtest/avif_fuzztest_enc_dec_experimental.cc @@ -98,21 +98,20 @@ void EncodeDecodeValid(AvifImagePtr image, AvifEncoderPtr encoder, // hard to verify so do not check it. } -// Note that the images are passed as raw pointers because unique_ptr didn't -// seem to work. Similarly, avifGainMapMetadata is passed as a byte array +// Note that avifGainMapMetadata is passed as a byte array // because the C array fields in the struct seem to prevent fuzztest from // handling it natively. AvifImagePtr AddGainMapToImage( - avifImage* image, avifImage* gainMap, + AvifImagePtr image, AvifImagePtr gainMap, const std::array& metadata) { - image->gainMap.image = gainMap; + image->gainMap.image = gainMap.release(); std::memcpy(&image->gainMap.metadata, metadata.data(), metadata.size()); - return AvifImagePtr(image, avifImageDestroy); + return image; } inline auto ArbitraryAvifImageWithGainMap() { return fuzztest::Map( - AddGainMapToImage, ArbitraryAvifImageRawPtr(), ArbitraryAvifImageRawPtr(), + AddGainMapToImage, ArbitraryAvifImage(), ArbitraryAvifImage(), fuzztest::Arbitrary>()); } diff --git a/tests/gtest/avif_fuzztest_helpers.cc b/tests/gtest/avif_fuzztest_helpers.cc index bbd0d93023..fda8ae498f 100644 --- a/tests/gtest/avif_fuzztest_helpers.cc +++ b/tests/gtest/avif_fuzztest_helpers.cc @@ -43,21 +43,18 @@ AvifImagePtr CreateAvifImage(size_t width, size_t height, int depth, } // namespace -avifImage* CreateAvifImage8bRawPtr(size_t width, size_t height, - avifPixelFormat pixel_format, bool has_alpha, - const std::vector& samples) { +AvifImagePtr CreateAvifImage8b(size_t width, size_t height, + avifPixelFormat pixel_format, bool has_alpha, + const std::vector& samples) { return CreateAvifImage(width, height, 8, pixel_format, has_alpha, - samples.data()) - .release(); + samples.data()); } -avifImage* CreateAvifImage16bRawPtr(size_t width, size_t height, int depth, - avifPixelFormat pixel_format, - bool has_alpha, - const std::vector& samples) { +AvifImagePtr CreateAvifImage16b(size_t width, size_t height, int depth, + avifPixelFormat pixel_format, bool has_alpha, + const std::vector& samples) { return CreateAvifImage(width, height, depth, pixel_format, has_alpha, - reinterpret_cast(samples.data())) - .release(); + reinterpret_cast(samples.data())); } AvifEncoderPtr CreateAvifEncoder(avifCodecChoice codec_choice, int max_threads, diff --git a/tests/gtest/avif_fuzztest_helpers.h b/tests/gtest/avif_fuzztest_helpers.h index 933d23f5c6..e80b6d8e97 100644 --- a/tests/gtest/avif_fuzztest_helpers.h +++ b/tests/gtest/avif_fuzztest_helpers.h @@ -20,13 +20,12 @@ namespace testutil { // C++ wrapper for scoped memory management of C API objects. // Exposed for convenient fuzztest reproducer output. -avifImage* CreateAvifImage8bRawPtr(size_t width, size_t height, - avifPixelFormat pixel_format, bool has_alpha, - const std::vector& samples); -avifImage* CreateAvifImage16bRawPtr(size_t width, size_t height, int depth, - avifPixelFormat pixel_format, - bool has_alpha, - const std::vector& samples); +AvifImagePtr CreateAvifImage8b(size_t width, size_t height, + avifPixelFormat pixel_format, bool has_alpha, + const std::vector& samples); +AvifImagePtr CreateAvifImage16b(size_t width, size_t height, int depth, + avifPixelFormat pixel_format, bool has_alpha, + const std::vector& samples); AvifEncoderPtr CreateAvifEncoder(avifCodecChoice codec_choice, int max_threads, int min_quantizer, int max_quantizer, int min_quantizer_alpha, @@ -40,7 +39,6 @@ AvifDecoderPtr CreateAvifDecoder(avifCodecChoice codec_choice, int max_threads, uint32_t image_dimension_limit, uint32_t image_count_limit, avifStrictFlags strict_flags); -AvifImagePtr AvifImageToUniquePtr(avifImage* image); //------------------------------------------------------------------------------ // Custom fuzztest generators. @@ -62,14 +60,13 @@ inline auto ArbitraryPixelFormat() { } // avifImage generator type: Width, height, pixel format and 8-bit samples. -inline auto ArbitraryAvifImage8bRawPtr() { +inline auto ArbitraryAvifImage8b() { return fuzztest::FlatMap( [](size_t width, size_t height, avifPixelFormat pixel_format, bool has_alpha) { return fuzztest::Map( - CreateAvifImage8bRawPtr, fuzztest::Just(width), - fuzztest::Just(height), fuzztest::Just(pixel_format), - fuzztest::Just(has_alpha), + CreateAvifImage8b, fuzztest::Just(width), fuzztest::Just(height), + fuzztest::Just(pixel_format), fuzztest::Just(has_alpha), fuzztest::Arbitrary>().WithSize( GetNumSamples(width, height, pixel_format, has_alpha))); }, @@ -80,14 +77,14 @@ inline auto ArbitraryAvifImage8bRawPtr() { // avifImage generator type: Width, height, depth, pixel format and 16-bit // samples. -inline auto ArbitraryAvifImage16bRawPtr() { +inline auto ArbitraryAvifImage16b() { return fuzztest::FlatMap( [](size_t width, size_t height, int depth, avifPixelFormat pixel_format, bool has_alpha) { return fuzztest::Map( - CreateAvifImage16bRawPtr, fuzztest::Just(width), - fuzztest::Just(height), fuzztest::Just(depth), - fuzztest::Just(pixel_format), fuzztest::Just(has_alpha), + CreateAvifImage16b, fuzztest::Just(width), fuzztest::Just(height), + fuzztest::Just(depth), fuzztest::Just(pixel_format), + fuzztest::Just(has_alpha), fuzztest::ContainerOf>( fuzztest::InRange(0, (1 << depth) - 1)) .WithSize( @@ -99,15 +96,9 @@ inline auto ArbitraryAvifImage16bRawPtr() { fuzztest::Arbitrary()); } -// Generator for an arbitrary avifImage* (raw pointer). -inline auto ArbitraryAvifImageRawPtr() { - return fuzztest::OneOf(ArbitraryAvifImage8bRawPtr(), - ArbitraryAvifImage16bRawPtr()); -} - -// Generator for an arbitrary AvifImagePtr (unique pointer). +// Generator for an arbitrary AvifImagePtr. inline auto ArbitraryAvifImage() { - return fuzztest::Map(AvifImageToUniquePtr, ArbitraryAvifImageRawPtr()); + return fuzztest::OneOf(ArbitraryAvifImage8b(), ArbitraryAvifImage16b()); } // avifEncoder and avifDecoder generators