/src/libjxl/tools/decode_basic_info_fuzzer.cc
Line | Count | Source |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #include <jxl/codestream_header.h> |
7 | | #include <jxl/decode.h> |
8 | | |
9 | | #include <cstddef> |
10 | | #include <cstdint> |
11 | | #include <cstdio> |
12 | | #include <vector> |
13 | | |
14 | | #include "lib/jxl/base/compiler_specific.h" |
15 | | #include "lib/jxl/fuzztest.h" |
16 | | #include "tools/tracking_memory_manager.h" |
17 | | |
18 | | namespace { |
19 | | |
20 | | using ::jpegxl::tools::kGiB; |
21 | | using ::jpegxl::tools::TrackingMemoryManager; |
22 | | |
23 | 7.70k | void CheckImpl(bool ok, const char* conndition, const char* file, int line) { |
24 | 7.70k | if (!ok) { |
25 | 0 | fprintf(stderr, "Check(%s) failed at %s:%d\n", conndition, file, line); |
26 | 0 | JXL_CRASH(); |
27 | 0 | } |
28 | 7.70k | } |
29 | 7.70k | #define Check(OK) CheckImpl((OK), #OK, __FILE__, __LINE__) |
30 | | |
31 | 7.70k | int DoTestOneInput(const uint8_t* data, size_t size) { |
32 | 7.70k | JxlDecoderStatus status; |
33 | 7.70k | TrackingMemoryManager memory_manager{/* cap */ 1 * kGiB, |
34 | 7.70k | /* total_cap */ 5 * kGiB}; |
35 | 7.70k | JxlDecoder* dec = JxlDecoderCreate(memory_manager.get()); |
36 | 7.70k | const auto finish = [&]() -> int { |
37 | 7.70k | JxlDecoderDestroy(dec); |
38 | 7.70k | Check(memory_manager.Reset()); |
39 | 7.70k | return 0; |
40 | 7.70k | }; |
41 | | |
42 | 7.70k | JxlDecoderSubscribeEvents(dec, JXL_DEC_BASIC_INFO | JXL_DEC_COLOR_ENCODING); |
43 | 7.70k | JxlDecoderSetInput(dec, data, size); |
44 | | |
45 | 7.70k | status = JxlDecoderProcessInput(dec); |
46 | | |
47 | 7.70k | if (status != JXL_DEC_BASIC_INFO) return finish(); |
48 | | |
49 | 5.22k | JxlBasicInfo info; |
50 | 5.22k | status = JxlDecoderGetBasicInfo(dec, &info); |
51 | 5.22k | bool have_basic_info = (status == JXL_DEC_SUCCESS); |
52 | | |
53 | 5.22k | if (have_basic_info) { |
54 | 5.22k | if (info.alpha_bits != 0) { |
55 | 25.9k | for (int i = 0; i < info.num_extra_channels; ++i) { |
56 | 25.7k | JxlExtraChannelInfo extra; |
57 | 25.7k | JxlDecoderGetExtraChannelInfo(dec, 0, &extra); |
58 | 25.7k | } |
59 | 237 | } |
60 | 5.22k | } |
61 | 5.22k | status = JxlDecoderProcessInput(dec); |
62 | | |
63 | 5.22k | if (status != JXL_DEC_COLOR_ENCODING) return finish(); |
64 | | |
65 | 322 | JxlDecoderGetColorAsEncodedProfile(dec, JXL_COLOR_PROFILE_TARGET_ORIGINAL, |
66 | 322 | nullptr); |
67 | 322 | size_t dec_profile_size; |
68 | 322 | JxlDecoderGetICCProfileSize(dec, JXL_COLOR_PROFILE_TARGET_ORIGINAL, |
69 | 322 | &dec_profile_size); |
70 | | |
71 | 322 | return finish(); |
72 | 5.22k | } |
73 | | |
74 | | } // namespace |
75 | | |
76 | 68.6k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
77 | 68.6k | return DoTestOneInput(data, size); |
78 | 68.6k | } |
79 | | |
80 | 0 | void TestOneInput(const std::vector<uint8_t>& data) { |
81 | 0 | DoTestOneInput(data.data(), data.size()); |
82 | 0 | } |
83 | | |
84 | | FUZZ_TEST(DecodeBasiInfoFuzzTest, TestOneInput); |