Skip to content

Commit

Permalink
Fuzztest: switch raw ptr to unique_ptr now that fuzztest supports it. (
Browse files Browse the repository at this point in the history
  • Loading branch information
maryla-uc authored Sep 19, 2023
1 parent 40988c2 commit 1e0205a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 43 deletions.
2 changes: 1 addition & 1 deletion ext/fuzztest.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 5 additions & 6 deletions tests/gtest/avif_fuzztest_enc_dec_experimental.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t, sizeof(avifGainMapMetadata)>& 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<std::array<uint8_t, sizeof(avifGainMapMetadata)>>());
}

Expand Down
19 changes: 8 additions & 11 deletions tests/gtest/avif_fuzztest_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& samples) {
AvifImagePtr CreateAvifImage8b(size_t width, size_t height,
avifPixelFormat pixel_format, bool has_alpha,
const std::vector<uint8_t>& 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<uint16_t>& samples) {
AvifImagePtr CreateAvifImage16b(size_t width, size_t height, int depth,
avifPixelFormat pixel_format, bool has_alpha,
const std::vector<uint16_t>& samples) {
return CreateAvifImage(width, height, depth, pixel_format, has_alpha,
reinterpret_cast<const uint8_t*>(samples.data()))
.release();
reinterpret_cast<const uint8_t*>(samples.data()));
}

AvifEncoderPtr CreateAvifEncoder(avifCodecChoice codec_choice, int max_threads,
Expand Down
39 changes: 15 additions & 24 deletions tests/gtest/avif_fuzztest_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& samples);
avifImage* CreateAvifImage16bRawPtr(size_t width, size_t height, int depth,
avifPixelFormat pixel_format,
bool has_alpha,
const std::vector<uint16_t>& samples);
AvifImagePtr CreateAvifImage8b(size_t width, size_t height,
avifPixelFormat pixel_format, bool has_alpha,
const std::vector<uint8_t>& samples);
AvifImagePtr CreateAvifImage16b(size_t width, size_t height, int depth,
avifPixelFormat pixel_format, bool has_alpha,
const std::vector<uint16_t>& samples);
AvifEncoderPtr CreateAvifEncoder(avifCodecChoice codec_choice, int max_threads,
int min_quantizer, int max_quantizer,
int min_quantizer_alpha,
Expand All @@ -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.
Expand All @@ -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<std::vector<uint8_t>>().WithSize(
GetNumSamples(width, height, pixel_format, has_alpha)));
},
Expand All @@ -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<std::vector<uint16_t>>(
fuzztest::InRange<uint16_t>(0, (1 << depth) - 1))
.WithSize(
Expand All @@ -99,15 +96,9 @@ inline auto ArbitraryAvifImage16bRawPtr() {
fuzztest::Arbitrary<bool>());
}

// 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
Expand Down

0 comments on commit 1e0205a

Please sign in to comment.