Make pdf_jpx_fuzzer more realistic Currently, pdf_jpx_fuzzer tests CJPX_Decoder::Decode() with some bespoke code to determine the bitmap format and the corresponding image component count. Replace this code with JpxDecodeConversion, to better align the fuzzer with the logic in the production CJPX_Decoder::Decode() caller. As a result: 1) This potentially can avoid some fuzzing failures that may not be possible with production code. 2) This adds support for fuzzing CJPX_Decoder::Decode() with varying `swap_rgb` values. Currently, this argument is hard-coded to false. Bug: 428771937 Change-Id: Iadbc858024e5cc46d20ed429b0716cb734845c8b Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/135550 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/testing/fuzzers/pdf_jpx_fuzzer.cc b/testing/fuzzers/pdf_jpx_fuzzer.cc index 30d0de1..9ed6374 100644 --- a/testing/fuzzers/pdf_jpx_fuzzer.cc +++ b/testing/fuzzers/pdf_jpx_fuzzer.cc
@@ -6,6 +6,7 @@ #include <memory> #include "core/fpdfapi/page/cpdf_colorspace.h" +#include "core/fpdfapi/page/jpx_decode_conversion.h" #include "core/fxcodec/jpx/cjpx_decoder.h" #include "core/fxcrt/fx_safe_types.h" #include "core/fxge/dib/cfx_dibitmap.h" @@ -62,19 +63,25 @@ return 0; } - FXDIB_Format format; - if (image_info.channels == 1) { - format = FXDIB_Format::k8bppRgb; - } else if (image_info.channels <= 3) { - format = FXDIB_Format::kBgr; - } else if (image_info.channels == 4) { - format = FXDIB_Format::kBgrx; - } else { - image_info.width = (image_info.width * image_info.channels + 2) / 3; - format = FXDIB_Format::kBgr; + // TODO(thestig): Add colorspace support. + RetainPtr<CPDF_ColorSpace> color_space; + auto maybe_conversion = + JpxDecodeConversion::Create(image_info, color_space.Get()); + if (!maybe_conversion.has_value()) { + return 0; } + + const auto& conversion = maybe_conversion.value(); + int components = conversion.jpx_components_count().value_or(0); + if (components <= 0) { + return 0; + } + + image_info.width = conversion.width(); + auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>(); - if (!bitmap->Create(image_info.width, image_info.height, format)) { + if (!bitmap->Create(image_info.width, image_info.height, + conversion.format())) { return 0; } @@ -85,7 +92,7 @@ } decoder->Decode(bitmap->GetWritableBuffer(), bitmap->GetPitch(), - /*swap_rgb=*/false, GetCompsFromFormat(format)); + conversion.swap_rgb(), components); return 0; }