Move pdf_jpx_fuzzer decoding logic into pdf_jpx_fuzzer_common Move the JPX decoding logic out of pdf_jpx_fuzzer.cc into a new pdf_jpx_fuzzer_common source set, so it can be shared with an upcoming FuzzTest version of the fuzzer. pdf_jpx_fuzzer.cc now only parses the decoder parameters from the fuzzer input and calls DecodeJpxForFuzzing(). No functional change intended. Bug: 581810614 Change-Id: I574d19eb0c685f541b2291df1287facb7b9f30e0 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/164230 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/testing/fuzzers/BUILD.gn b/testing/fuzzers/BUILD.gn index ec350de..7e8749f 100644 --- a/testing/fuzzers/BUILD.gn +++ b/testing/fuzzers/BUILD.gn
@@ -190,6 +190,24 @@ include_dirs = [ "../.." ] } +source_set("pdf_jpx_fuzzer_common"){ + testonly = !is_component_build + sources = [ + "pdf_jpx_fuzzer_common.cc", + "pdf_jpx_fuzzer_common.h", + ] + configs += [ ":fuzzer_config" ] + public_deps = [ + "../../core/fxcodec", + "../../core/fxcrt", + ] + deps = [ + ":fuzzer_utils", + "../../core/fpdfapi/page", + "../../core/fxge", + ] +} + template("pdfium_fuzzer") { is_public = defined(invoker.public_fuzzer) && invoker.public_fuzzer if (is_public) { @@ -568,10 +586,7 @@ pdfium_fuzzer("pdf_jpx_fuzzer") { sources = [ "pdf_jpx_fuzzer.cc" ] deps = [ - "../../core/fpdfapi/page", - "../../core/fxcodec", - "../../core/fxcrt", - "../../core/fxge", + ":pdf_jpx_fuzzer_common", ] }
diff --git a/testing/fuzzers/pdf_jpx_fuzzer.cc b/testing/fuzzers/pdf_jpx_fuzzer.cc index 2800f7e..8ce75a7 100644 --- a/testing/fuzzers/pdf_jpx_fuzzer.cc +++ b/testing/fuzzers/pdf_jpx_fuzzer.cc
@@ -3,28 +3,10 @@ // found in the LICENSE file. #include <cstdint> -#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" -#include "core/fxge/dib/fx_dib.h" - -namespace { - -const uint32_t kMaxJPXFuzzSize = 100 * 1024 * 1024; // 100 MB - -bool CheckImageSize(const CJPX_Decoder::JpxImageInfo& image_info) { - static constexpr uint32_t kMemLimitBytes = 1024 * 1024 * 1024; // 1 GB. - FX_SAFE_UINT32 mem = image_info.width; - mem *= image_info.height; - mem *= image_info.channels; - return mem.IsValid() && mem.ValueOrDie() <= kMemLimitBytes; -} - -} // namespace +#include "core/fxcrt/span.h" +#include "testing/fuzzers/pdf_jpx_fuzzer_common.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { if (size < 3) { @@ -39,59 +21,7 @@ uint8_t resolution_levels_to_skip = pre[1]; bool strict_mode = !!pre[2]; - std::unique_ptr<CJPX_Decoder> decoder = CJPX_Decoder::Create( - span, color_space_option, resolution_levels_to_skip, strict_mode); - if (!decoder) { - return 0; - } - - // A call to StartDecode could be too expensive if image size is very big, so - // check size before calling StartDecode(). - CJPX_Decoder::JpxImageInfo image_info = decoder->GetInfo(); - if (!CheckImageSize(image_info)) { - return 0; - } - - if (!decoder->StartDecode()) { - return 0; - } - - // StartDecode() could change image size, so check again. - image_info = decoder->GetInfo(); - if (!CheckImageSize(image_info)) { - return 0; - } - - // 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, - conversion.format())) { - return 0; - } - - if (bitmap->GetHeight() <= 0 || - kMaxJPXFuzzSize / bitmap->GetPitch() < - static_cast<uint32_t>(bitmap->GetHeight())) { - return 0; - } - - decoder->Decode(bitmap->GetWritableBuffer(), bitmap->GetPitch(), - conversion.swap_rgb(), components); - + DecodeJpxForFuzzing(span, color_space_option, resolution_levels_to_skip, + strict_mode); return 0; }
diff --git a/testing/fuzzers/pdf_jpx_fuzzer_common.cc b/testing/fuzzers/pdf_jpx_fuzzer_common.cc new file mode 100644 index 0000000..780eb8b --- /dev/null +++ b/testing/fuzzers/pdf_jpx_fuzzer_common.cc
@@ -0,0 +1,87 @@ +// Copyright 2026 The PDFium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/fuzzers/pdf_jpx_fuzzer_common.h" + +#include <memory> + +#include "core/fpdfapi/page/cpdf_colorspace.h" +#include "core/fpdfapi/page/jpx_decode_conversion.h" +#include "core/fxcrt/fx_safe_types.h" +#include "core/fxcrt/retain_ptr.h" +#include "core/fxge/dib/cfx_dibitmap.h" +#include "core/fxge/dib/fx_dib.h" + +namespace { + +constexpr uint32_t kMaxJPXFuzzSize = 100 * 1024 * 1024; // 100 MB + +bool CheckImageSize(const CJPX_Decoder::JpxImageInfo& image_info) { + static constexpr uint32_t kMemLimitBytes = 1024 * 1024 * 1024; // 1 GB. + FX_SAFE_UINT32 mem = image_info.width; + mem *= image_info.height; + mem *= image_info.channels; + return mem.IsValid() && mem.ValueOrDie() <= kMemLimitBytes; +} + +} // namespace + +void DecodeJpxForFuzzing(pdfium::span<const uint8_t> data, + CJPX_Decoder::ColorSpaceOption color_space_option, + uint8_t resolution_levels_to_skip, + bool strict_mode) { + std::unique_ptr<CJPX_Decoder> decoder = CJPX_Decoder::Create( + data, color_space_option, resolution_levels_to_skip, strict_mode); + if (!decoder) { + return; + } + + // A call to StartDecode could be too expensive if image size is very big, so + // check size before calling StartDecode(). + CJPX_Decoder::JpxImageInfo image_info = decoder->GetInfo(); + if (!CheckImageSize(image_info)) { + return; + } + + if (!decoder->StartDecode()) { + return; + } + + // StartDecode() could change image size, so check again. + image_info = decoder->GetInfo(); + if (!CheckImageSize(image_info)) { + return; + } + + // 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; + } + + const auto& conversion = maybe_conversion.value(); + int components = conversion.jpx_components_count().value_or(0); + if (components <= 0) { + return; + } + + image_info.width = conversion.width(); + + auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>(); + if (!bitmap->Create(image_info.width, image_info.height, + conversion.format())) { + return; + } + + if (bitmap->GetHeight() <= 0 || + kMaxJPXFuzzSize / bitmap->GetPitch() < + static_cast<uint32_t>(bitmap->GetHeight())) { + return; + } + + decoder->Decode(bitmap->GetWritableBuffer(), bitmap->GetPitch(), + conversion.swap_rgb(), components); +}
diff --git a/testing/fuzzers/pdf_jpx_fuzzer_common.h b/testing/fuzzers/pdf_jpx_fuzzer_common.h new file mode 100644 index 0000000..4b317ff --- /dev/null +++ b/testing/fuzzers/pdf_jpx_fuzzer_common.h
@@ -0,0 +1,20 @@ +// Copyright 2026 The PDFium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef TESTING_FUZZERS_PDF_JPX_FUZZER_UTILS_H_ +#define TESTING_FUZZERS_PDF_JPX_FUZZER_UTILS_H_ + +#include <stdint.h> + +#include "core/fxcodec/jpx/cjpx_decoder.h" +#include "core/fxcrt/span.h" + +// Decodes `data` as a JPEG 2000 image with the given decoder parameters, +// bailing out early on images that are too large to decode in a fuzzer. +void DecodeJpxForFuzzing(pdfium::span<const uint8_t> data, + CJPX_Decoder::ColorSpaceOption color_space_option, + uint8_t resolution_levels_to_skip, + bool strict_mode); + +#endif // TESTING_FUZZERS_PDF_JPX_FUZZER_UTILS_H_