Coverage Report

Created: 2026-02-14 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/tools/djxl_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
#include <jxl/decode_cxx.h>
9
#include <jxl/memory_manager.h>
10
#include <jxl/thread_parallel_runner.h>
11
#include <jxl/thread_parallel_runner_cxx.h>
12
#include <jxl/types.h>
13
14
#include <algorithm>
15
#include <cstdint>
16
#include <cstdio>
17
#include <cstdlib>
18
#include <cstring>
19
#include <hwy/targets.h>
20
#include <map>
21
#include <memory>
22
#include <mutex>
23
#include <random>
24
#include <vector>
25
26
#include "lib/jxl/base/common.h"
27
#include "lib/jxl/base/compiler_specific.h"
28
#include "lib/jxl/fuzztest.h"
29
#include "tools/tracking_memory_manager.h"
30
31
namespace {
32
33
// Externally visible value to ensure pixels are used in the fuzzer.
34
int external_code = 0;
35
36
constexpr const size_t kStreamingTargetNumberOfChunks = 128;
37
38
using ::jpegxl::tools::kGiB;
39
using ::jpegxl::tools::TrackingMemoryManager;
40
41
20.5k
void CheckImpl(bool ok, const char* conndition, const char* file, int line) {
42
20.5k
  if (!ok) {
43
0
    fprintf(stderr, "Check(%s) failed at %s:%d\n", conndition, file, line);
44
0
    JXL_CRASH();
45
0
  }
46
20.5k
}
47
20.5k
#define Check(OK) CheckImpl((OK), #OK, __FILE__, __LINE__)
48
49
// Options for the fuzzing
50
struct FuzzSpec {
51
  JxlDataType output_type;
52
  JxlEndianness output_endianness;
53
  size_t output_align;
54
  bool get_alpha;
55
  bool get_grayscale;
56
  bool use_streaming;
57
  bool jpeg_to_pixels;  // decode to pixels even if it is JPEG-reconstructible
58
  // Whether to use the callback mechanism for the output image or not.
59
  bool use_callback;
60
  bool keep_orientation;
61
  bool decode_boxes;
62
  bool coalescing;
63
  // Used for random variation of chunk sizes, extra channels, ... to get
64
  uint32_t random_seed;
65
};
66
67
template <typename It>
68
228k
void Consume(const It& begin, const It& end) {
69
3.43G
  for (auto it = begin; it < end; ++it) {
70
3.43G
    if (*it == 0) {
71
1.47G
      external_code ^= ~0;
72
1.95G
    } else {
73
1.95G
      external_code ^= *it;
74
1.95G
    }
75
3.43G
  }
76
228k
}
djxl_fuzzer.cc:void (anonymous namespace)::Consume<unsigned char const*>(unsigned char const* const&, unsigned char const* const&)
Line
Count
Source
68
90.0k
void Consume(const It& begin, const It& end) {
69
6.99M
  for (auto it = begin; it < end; ++it) {
70
6.90M
    if (*it == 0) {
71
6.52M
      external_code ^= ~0;
72
6.52M
    } else {
73
380k
      external_code ^= *it;
74
380k
    }
75
6.90M
  }
76
90.0k
}
djxl_fuzzer.cc:void (anonymous namespace)::Consume<std::__1::__wrap_iter<char const*> >(std::__1::__wrap_iter<char const*> const&, std::__1::__wrap_iter<char const*> const&)
Line
Count
Source
68
73.7k
void Consume(const It& begin, const It& end) {
69
253k
  for (auto it = begin; it < end; ++it) {
70
179k
    if (*it == 0) {
71
107k
      external_code ^= ~0;
72
107k
    } else {
73
72.2k
      external_code ^= *it;
74
72.2k
    }
75
179k
  }
76
73.7k
}
djxl_fuzzer.cc:void (anonymous namespace)::Consume<std::__1::__wrap_iter<unsigned char const*> >(std::__1::__wrap_iter<unsigned char const*> const&, std::__1::__wrap_iter<unsigned char const*> const&)
Line
Count
Source
68
49.9k
void Consume(const It& begin, const It& end) {
69
3.27G
  for (auto it = begin; it < end; ++it) {
70
3.27G
    if (*it == 0) {
71
1.47G
      external_code ^= ~0;
72
1.80G
    } else {
73
1.80G
      external_code ^= *it;
74
1.80G
    }
75
3.27G
  }
76
49.9k
}
djxl_fuzzer.cc:void (anonymous namespace)::Consume<std::__1::__wrap_iter<unsigned char*> >(std::__1::__wrap_iter<unsigned char*> const&, std::__1::__wrap_iter<unsigned char*> const&)
Line
Count
Source
68
14.5k
void Consume(const It& begin, const It& end) {
69
156M
  for (auto it = begin; it < end; ++it) {
70
155M
    if (*it == 0) {
71
37.1k
      external_code ^= ~0;
72
155M
    } else {
73
155M
      external_code ^= *it;
74
155M
    }
75
155M
  }
76
14.5k
}
77
78
template <typename T>
79
90.0k
void Consume(const T& entry) {
80
90.0k
  const uint8_t* begin = reinterpret_cast<const uint8_t*>(&entry);
81
90.0k
  Consume(begin, begin + sizeof(T));
82
90.0k
}
djxl_fuzzer.cc:void (anonymous namespace)::Consume<JxlBasicInfo>(JxlBasicInfo const&)
Line
Count
Source
79
16.3k
void Consume(const T& entry) {
80
16.3k
  const uint8_t* begin = reinterpret_cast<const uint8_t*>(&entry);
81
16.3k
  Consume(begin, begin + sizeof(T));
82
16.3k
}
djxl_fuzzer.cc:void (anonymous namespace)::Consume<JxlExtraChannelInfo>(JxlExtraChannelInfo const&)
Line
Count
Source
79
45.8k
void Consume(const T& entry) {
80
45.8k
  const uint8_t* begin = reinterpret_cast<const uint8_t*>(&entry);
81
45.8k
  Consume(begin, begin + sizeof(T));
82
45.8k
}
djxl_fuzzer.cc:void (anonymous namespace)::Consume<JxlFrameHeader>(JxlFrameHeader const&)
Line
Count
Source
79
27.9k
void Consume(const T& entry) {
80
27.9k
  const uint8_t* begin = reinterpret_cast<const uint8_t*>(&entry);
81
27.9k
  Consume(begin, begin + sizeof(T));
82
27.9k
}
83
84
// use_streaming: if true, decodes the data in small chunks, if false, decodes
85
// it in one shot.
86
bool DecodeJpegXl(const uint8_t* jxl, size_t size,
87
                  JxlMemoryManager* memory_manager, size_t max_pixels,
88
                  size_t max_total_pixels, const FuzzSpec& spec,
89
                  std::vector<uint8_t>* pixels, std::vector<uint8_t>* jpeg,
90
                  size_t* xsize, size_t* ysize,
91
20.5k
                  std::vector<uint8_t>* icc_profile) {
92
20.5k
  size_t total_pixels = 0;
93
  // Multi-threaded parallel runner. Limit to max 2 threads since the fuzzer
94
  // itself is already multithreaded.
95
20.5k
  size_t num_threads =
96
20.5k
      std::min<size_t>(2, JxlThreadParallelRunnerDefaultNumWorkerThreads());
97
20.5k
  auto runner = JxlThreadParallelRunnerMake(memory_manager, num_threads);
98
99
20.5k
  auto mt = jxl::make_unique<std::mt19937>(spec.random_seed);
100
20.5k
  std::exponential_distribution<> dis_streaming(kStreamingTargetNumberOfChunks);
101
102
20.5k
  auto dec = JxlDecoderMake(memory_manager);
103
20.5k
  if (JXL_DEC_SUCCESS !=
104
20.5k
      JxlDecoderSubscribeEvents(
105
20.5k
          dec.get(), JXL_DEC_BASIC_INFO | JXL_DEC_COLOR_ENCODING |
106
20.5k
                         JXL_DEC_PREVIEW_IMAGE | JXL_DEC_FRAME |
107
20.5k
                         JXL_DEC_FULL_IMAGE | JXL_DEC_JPEG_RECONSTRUCTION |
108
20.5k
                         JXL_DEC_BOX)) {
109
0
    return false;
110
0
  }
111
20.5k
  if (JXL_DEC_SUCCESS != JxlDecoderSetParallelRunner(dec.get(),
112
20.5k
                                                     JxlThreadParallelRunner,
113
20.5k
                                                     runner.get())) {
114
0
    return false;
115
0
  }
116
20.5k
  if (JXL_DEC_SUCCESS != JxlDecoderSetKeepOrientation(
117
20.5k
                             dec.get(), TO_JXL_BOOL(spec.keep_orientation))) {
118
0
    abort();
119
0
  }
120
20.5k
  if (JXL_DEC_SUCCESS !=
121
20.5k
      JxlDecoderSetCoalescing(dec.get(), TO_JXL_BOOL(spec.coalescing))) {
122
0
    abort();
123
0
  }
124
20.5k
  JxlBasicInfo info;
125
20.5k
  uint32_t channels = (spec.get_grayscale ? 1 : 3) + (spec.get_alpha ? 1 : 0);
126
20.5k
  JxlPixelFormat format = {channels, spec.output_type, spec.output_endianness,
127
20.5k
                           spec.output_align};
128
129
20.5k
  if (!spec.use_streaming) {
130
    // Set all input at once
131
10.3k
    JxlDecoderSetInput(dec.get(), jxl, size);
132
10.3k
    JxlDecoderCloseInput(dec.get());
133
10.3k
  }
134
135
20.5k
  bool seen_basic_info = false;
136
20.5k
  bool seen_color_encoding = false;
137
20.5k
  bool seen_preview = false;
138
20.5k
  bool seen_need_image_out = false;
139
20.5k
  bool seen_full_image = false;
140
20.5k
  bool seen_frame = false;
141
20.5k
  uint32_t num_frames = 0;
142
20.5k
  bool seen_jpeg_reconstruction = false;
143
20.5k
  bool seen_jpeg_need_more_output = false;
144
  // If streaming and seen around half the input, test flushing
145
20.5k
  bool tested_flush = false;
146
147
  // Size made available for the streaming input, emulating a subset of the
148
  // full input size.
149
20.5k
  size_t streaming_size = 0;
150
20.5k
  size_t leftover = size;
151
20.5k
  size_t preview_xsize = 0;
152
20.5k
  size_t preview_ysize = 0;
153
20.5k
  bool want_preview = false;
154
20.5k
  std::vector<uint8_t> preview_pixels;
155
156
20.5k
  std::vector<uint8_t> extra_channel_pixels;
157
158
20.5k
  struct CalledRow {
159
20.5k
    std::vector<int> delta;
160
    // Use the pixel values.
161
20.5k
    uint32_t value = 0;
162
20.5k
  };
163
164
  // Callback function used when decoding with use_callback.
165
20.5k
  struct DecodeCallbackData {
166
20.5k
    JxlBasicInfo info;
167
20.5k
    size_t xsize = 0;
168
20.5k
    size_t ysize = 0;
169
20.5k
    std::unique_ptr<std::mutex[]> called_rows_mutex;
170
    // For each row stores the segments of the row being called. For each row
171
    // the sum of all the int values in the map up to [i] (inclusive) tell how
172
    // many times a callback included the pixel i of that row.
173
20.5k
    std::vector<CalledRow> called_rows;
174
20.5k
  };
175
20.5k
  DecodeCallbackData decode_callback_data;
176
20.5k
  auto decode_callback = +[](void* opaque, size_t x, size_t y,
177
37.0M
                             size_t num_pixels, const void* pixels) {
178
37.0M
    DecodeCallbackData* data = static_cast<DecodeCallbackData*>(opaque);
179
37.0M
    if (num_pixels > data->xsize) abort();
180
37.0M
    if (x + num_pixels > data->xsize) abort();
181
37.0M
    if (y >= data->ysize) abort();
182
37.0M
    if (num_pixels && !pixels) abort();
183
    // Keep track of the segments being called by the callback.
184
37.0M
    {
185
37.0M
      const std::lock_guard<std::mutex> lock(data->called_rows_mutex.get()[y]);
186
37.0M
      auto& called_row = data->called_rows[y];
187
37.0M
      called_row.delta[x]++;
188
37.0M
      called_row.delta[x + num_pixels]--;
189
37.0M
      called_row.value += *static_cast<const uint8_t*>(pixels);
190
37.0M
    }
191
37.0M
  };
192
193
20.5k
  JxlExtraChannelInfo extra_channel_info;
194
195
20.5k
  std::vector<uint8_t> box_buffer;
196
197
20.5k
  if (spec.decode_boxes &&
198
9.85k
      JXL_DEC_SUCCESS != JxlDecoderSetDecompressBoxes(dec.get(), JXL_TRUE)) {
199
    // error ignored, can still fuzz if it doesn't brotli-decompress brob boxes.
200
0
  }
201
202
733k
  for (;;) {
203
733k
    JxlDecoderStatus status = JxlDecoderProcessInput(dec.get());
204
733k
    if (status == JXL_DEC_ERROR) {
205
19.4k
      return false;
206
714k
    } else if (status == JXL_DEC_NEED_MORE_INPUT) {
207
555k
      if (spec.use_streaming) {
208
555k
        size_t remaining = JxlDecoderReleaseInput(dec.get());
209
        // move any remaining bytes to the front if necessary
210
555k
        size_t used = streaming_size - remaining;
211
555k
        jxl += used;
212
555k
        leftover -= used;
213
555k
        streaming_size -= used;
214
555k
        size_t chunk_size = std::max<size_t>(
215
555k
            1, size * std::min<double>(1.0, dis_streaming(*mt)));
216
555k
        size_t add_size =
217
555k
            std::min<size_t>(chunk_size, leftover - streaming_size);
218
555k
        if (add_size == 0) {
219
          // End of the streaming data reached
220
7
          return false;
221
7
        }
222
555k
        streaming_size += add_size;
223
555k
        if (JXL_DEC_SUCCESS !=
224
555k
            JxlDecoderSetInput(dec.get(), jxl, streaming_size)) {
225
0
          return false;
226
0
        }
227
555k
        if (leftover == streaming_size) {
228
          // All possible input bytes given
229
8.35k
          JxlDecoderCloseInput(dec.get());
230
8.35k
        }
231
232
555k
        if (!tested_flush && seen_frame) {
233
          // Test flush max once to avoid too slow fuzzer run
234
1.90k
          tested_flush = true;
235
1.90k
          JxlDecoderFlushImage(dec.get());
236
1.90k
        }
237
555k
      } else {
238
0
        return false;
239
0
      }
240
555k
    } else if (status == JXL_DEC_JPEG_NEED_MORE_OUTPUT) {
241
310
      if (want_preview) abort();  // expected preview before frame
242
310
      if (spec.jpeg_to_pixels) abort();
243
310
      if (!seen_jpeg_reconstruction) abort();
244
310
      seen_jpeg_need_more_output = true;
245
310
      size_t used_jpeg_output =
246
310
          jpeg->size() - JxlDecoderReleaseJPEGBuffer(dec.get());
247
310
      jpeg->resize(std::max<size_t>(4096, jpeg->size() * 2));
248
310
      uint8_t* jpeg_buffer = jpeg->data() + used_jpeg_output;
249
310
      size_t jpeg_buffer_size = jpeg->size() - used_jpeg_output;
250
251
310
      if (JXL_DEC_SUCCESS !=
252
310
          JxlDecoderSetJPEGBuffer(dec.get(), jpeg_buffer, jpeg_buffer_size)) {
253
0
        return false;
254
0
      }
255
158k
    } else if (status == JXL_DEC_BASIC_INFO) {
256
16.3k
      if (seen_basic_info) abort();  // already seen basic info
257
16.3k
      seen_basic_info = true;
258
259
16.3k
      memset(&info, 0, sizeof(info));
260
16.3k
      if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo(dec.get(), &info)) {
261
0
        return false;
262
0
      }
263
16.3k
      Consume(info);
264
265
16.3k
      *xsize = info.xsize;
266
16.3k
      *ysize = info.ysize;
267
16.3k
      decode_callback_data.info = info;
268
16.3k
      size_t num_pixels = *xsize * *ysize;
269
      // num_pixels overflow
270
16.3k
      if (*xsize != 0 && num_pixels / *xsize != *ysize) return false;
271
      // limit max memory of this fuzzer test
272
16.3k
      if (num_pixels > max_pixels) return false;
273
274
16.2k
      if (info.have_preview) {
275
903
        want_preview = true;
276
903
        preview_xsize = info.preview.xsize;
277
903
        preview_ysize = info.preview.ysize;
278
903
        size_t preview_num_pixels = preview_xsize * preview_ysize;
279
        // num_pixels overflow
280
903
        if (preview_xsize != 0 &&
281
903
            preview_num_pixels / preview_xsize != preview_ysize) {
282
0
          return false;
283
0
        }
284
        // limit max memory of this fuzzer test
285
903
        if (preview_num_pixels > max_pixels) return false;
286
903
      }
287
288
62.0k
      for (size_t ec = 0; ec < info.num_extra_channels; ++ec) {
289
45.8k
        memset(&extra_channel_info, 0, sizeof(extra_channel_info));
290
45.8k
        if (JXL_DEC_SUCCESS !=
291
45.8k
            JxlDecoderGetExtraChannelInfo(dec.get(), ec, &extra_channel_info)) {
292
0
          abort();
293
0
        }
294
45.8k
        Consume(extra_channel_info);
295
45.8k
        std::vector<char> ec_name(extra_channel_info.name_length + 1);
296
45.8k
        if (JXL_DEC_SUCCESS != JxlDecoderGetExtraChannelName(dec.get(), ec,
297
45.8k
                                                             ec_name.data(),
298
45.8k
                                                             ec_name.size())) {
299
0
          abort();
300
0
        }
301
45.8k
        Consume(ec_name.cbegin(), ec_name.cend());
302
45.8k
      }
303
142k
    } else if (status == JXL_DEC_COLOR_ENCODING) {
304
14.5k
      if (!seen_basic_info) abort();     // expected basic info first
305
14.5k
      if (seen_color_encoding) abort();  // already seen color encoding
306
14.5k
      seen_color_encoding = true;
307
308
      // Get the ICC color profile of the pixel data
309
14.5k
      size_t icc_size;
310
14.5k
      if (JXL_DEC_SUCCESS !=
311
14.5k
          JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
312
14.5k
                                      &icc_size)) {
313
2
        return false;
314
2
      }
315
14.5k
      icc_profile->resize(icc_size);
316
14.5k
      if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(
317
14.5k
                                 dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
318
14.5k
                                 icc_profile->data(), icc_profile->size())) {
319
0
        return false;
320
0
      }
321
14.5k
      if (want_preview) {
322
882
        size_t preview_size;
323
882
        if (JXL_DEC_SUCCESS !=
324
882
            JxlDecoderPreviewOutBufferSize(dec.get(), &format, &preview_size)) {
325
23
          return false;
326
23
        }
327
859
        preview_pixels.resize(preview_size);
328
859
        if (JXL_DEC_SUCCESS != JxlDecoderSetPreviewOutBuffer(
329
859
                                   dec.get(), &format, preview_pixels.data(),
330
859
                                   preview_pixels.size())) {
331
0
          abort();
332
0
        }
333
859
      }
334
127k
    } else if (status == JXL_DEC_PREVIEW_IMAGE) {
335
      // TODO(eustas): test JXL_DEC_NEED_PREVIEW_OUT_BUFFER
336
584
      if (seen_preview) abort();
337
584
      if (!want_preview) abort();
338
584
      if (!seen_color_encoding) abort();
339
584
      want_preview = false;
340
584
      seen_preview = true;
341
584
      Consume(preview_pixels.cbegin(), preview_pixels.cend());
342
127k
    } else if (status == JXL_DEC_FRAME ||
343
99.2k
               status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
344
46.1k
      if (want_preview) abort();          // expected preview before frame
345
46.1k
      if (!seen_color_encoding) abort();  // expected color encoding first
346
46.1k
      if (status == JXL_DEC_FRAME) {
347
27.9k
        if (seen_frame) abort();  // already seen JXL_DEC_FRAME
348
27.9k
        seen_frame = true;
349
27.9k
        JxlFrameHeader frame_header;
350
27.9k
        memset(&frame_header, 0, sizeof(frame_header));
351
27.9k
        if (JXL_DEC_SUCCESS !=
352
27.9k
            JxlDecoderGetFrameHeader(dec.get(), &frame_header)) {
353
0
          abort();
354
0
        }
355
27.9k
        decode_callback_data.xsize = frame_header.layer_info.xsize;
356
27.9k
        decode_callback_data.ysize = frame_header.layer_info.ysize;
357
27.9k
        size_t num_pixels = static_cast<size_t>(frame_header.layer_info.xsize) *
358
27.9k
                            frame_header.layer_info.ysize;
359
27.9k
        if (num_pixels > max_pixels) return false;
360
27.9k
        if (total_pixels > max_total_pixels) return false;
361
27.9k
        total_pixels += num_pixels;
362
27.9k
        if (!spec.coalescing) {
363
26.5k
          decode_callback_data.called_rows.clear();
364
26.5k
        }
365
27.9k
        decode_callback_data.called_rows_mutex =
366
27.9k
            jxl::make_unique<std::mutex[]>(decode_callback_data.ysize);
367
27.9k
        decode_callback_data.called_rows.resize(decode_callback_data.ysize);
368
10.3M
        for (size_t y = 0; y < decode_callback_data.ysize; ++y) {
369
10.3M
          decode_callback_data.called_rows[y].delta.clear();
370
10.3M
          decode_callback_data.called_rows[y].delta.resize(
371
10.3M
              decode_callback_data.xsize + 1);
372
10.3M
        }
373
27.9k
        Consume(frame_header);
374
27.9k
        std::vector<char> frame_name(frame_header.name_length + 1);
375
27.9k
        if (JXL_DEC_SUCCESS != JxlDecoderGetFrameName(dec.get(),
376
27.9k
                                                      frame_name.data(),
377
27.9k
                                                      frame_name.size())) {
378
0
          abort();
379
0
        }
380
27.9k
        Consume(frame_name.cbegin(), frame_name.cend());
381
        // When not testing streaming, test that JXL_DEC_NEED_IMAGE_OUT_BUFFER
382
        // occurs instead, so do not set buffer now.
383
27.9k
        if (!spec.use_streaming) continue;
384
27.9k
      }
385
27.6k
      if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
386
        // expected JXL_DEC_FRAME instead
387
18.1k
        if (!seen_frame) abort();
388
        // already should have set buffer if streaming
389
18.1k
        if (spec.use_streaming) abort();
390
        // already seen need image out
391
18.1k
        if (seen_need_image_out) abort();
392
18.1k
        seen_need_image_out = true;
393
18.1k
      }
394
395
27.6k
      if (info.num_extra_channels > 0) {
396
6.34k
        std::uniform_int_distribution<> dis(0, info.num_extra_channels);
397
6.34k
        size_t ec_index = dis(*mt);
398
        // There is also a probability no extra channel is chosen
399
6.34k
        if (ec_index < info.num_extra_channels) {
400
          // TODO(eustas): that looks suspicious to me
401
3.70k
          size_t last_ec_index = info.num_extra_channels - 1;
402
3.70k
          size_t ec_size;
403
3.70k
          if (JXL_DEC_SUCCESS !=
404
3.70k
              JxlDecoderExtraChannelBufferSize(dec.get(), &format, &ec_size,
405
3.70k
                                               last_ec_index)) {
406
11
            return false;
407
11
          }
408
3.69k
          extra_channel_pixels.resize(ec_size);
409
3.69k
          if (JXL_DEC_SUCCESS !=
410
3.69k
              JxlDecoderSetExtraChannelBuffer(dec.get(), &format,
411
3.69k
                                              extra_channel_pixels.data(),
412
3.69k
                                              ec_size, last_ec_index)) {
413
0
            return false;
414
0
          }
415
3.69k
        }
416
6.34k
      }
417
418
27.5k
      if (spec.use_callback) {
419
8.14k
        if (JXL_DEC_SUCCESS !=
420
8.14k
            JxlDecoderSetImageOutCallback(dec.get(), &format, decode_callback,
421
8.14k
                                          &decode_callback_data)) {
422
27
          return false;
423
27
        }
424
19.4k
      } else {
425
        // Use the pixels output buffer.
426
19.4k
        size_t buffer_size;
427
19.4k
        if (JXL_DEC_SUCCESS !=
428
19.4k
            JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size)) {
429
59
          return false;
430
59
        }
431
19.3k
        pixels->resize(buffer_size);
432
19.3k
        void* pixels_buffer = static_cast<void*>(pixels->data());
433
19.3k
        size_t pixels_buffer_size = pixels->size();
434
19.3k
        if (JXL_DEC_SUCCESS !=
435
19.3k
            JxlDecoderSetImageOutBuffer(dec.get(), &format, pixels_buffer,
436
19.3k
                                        pixels_buffer_size)) {
437
0
          return false;
438
0
        }
439
19.3k
      }
440
81.1k
    } else if (status == JXL_DEC_JPEG_RECONSTRUCTION) {
441
      // Do not check preview precedence here, since this event only declares
442
      // that JPEG is going to be decoded; though, when first byte of JPEG
443
      // arrives (JXL_DEC_JPEG_NEED_MORE_OUTPUT) it is certain that preview
444
      // should have been produced already.
445
425
      if (seen_jpeg_reconstruction) abort();
446
425
      seen_jpeg_reconstruction = true;
447
425
      if (!spec.jpeg_to_pixels) {
448
        // Make sure buffer is allocated, but current size is too small to
449
        // contain valid JPEG.
450
421
        jpeg->resize(1);
451
421
        uint8_t* jpeg_buffer = jpeg->data();
452
421
        size_t jpeg_buffer_size = jpeg->size();
453
421
        if (JXL_DEC_SUCCESS !=
454
421
            JxlDecoderSetJPEGBuffer(dec.get(), jpeg_buffer, jpeg_buffer_size)) {
455
0
          return false;
456
0
        }
457
421
      }
458
80.6k
    } else if (status == JXL_DEC_FULL_IMAGE) {
459
24.6k
      if (want_preview) abort();  // expected preview before frame
460
24.6k
      if (!spec.jpeg_to_pixels && seen_jpeg_reconstruction) {
461
108
        if (!seen_jpeg_need_more_output) abort();
462
108
        jpeg->resize(jpeg->size() - JxlDecoderReleaseJPEGBuffer(dec.get()));
463
24.5k
      } else {
464
        // expected need image out or frame first
465
24.5k
        if (!seen_need_image_out && !seen_frame) abort();
466
24.5k
      }
467
468
24.6k
      seen_full_image = true;  // there may be multiple if animated
469
470
      // There may be a next animation frame so expect those again:
471
24.6k
      seen_need_image_out = false;
472
24.6k
      seen_frame = false;
473
24.6k
      num_frames++;
474
475
      // "Use" all the pixels; MSAN needs a conditional to count as usage.
476
24.6k
      Consume(pixels->cbegin(), pixels->cend());
477
24.6k
      Consume(jpeg->cbegin(), jpeg->cend());
478
479
      // When not coalescing, check that the whole (possibly cropped) frame was
480
      // sent
481
24.6k
      if (seen_need_image_out && spec.use_callback && spec.coalescing) {
482
        // Check that the callback sent all the pixels
483
0
        for (uint32_t y = 0; y < decode_callback_data.ysize; y++) {
484
0
          const auto& deltas = decode_callback_data.called_rows[y].delta;
485
0
          if (deltas[0] != 1) abort();
486
0
          if (deltas[decode_callback_data.xsize] != -1) abort();
487
0
          for (size_t i = 1; i < decode_callback_data.xsize; ++i) {
488
0
            if (deltas[i] != 0) abort();
489
0
          }
490
0
        }
491
0
      }
492
      // Nothing to do. Do not yet return. If the image is an animation, more
493
      // full frames may be decoded. This example only keeps the last one.
494
56.0k
    } else if (status == JXL_DEC_SUCCESS) {
495
938
      if (!seen_full_image) abort();  // expected full image before finishing
496
497
      // When decoding we may not get seen_need_image_out unless we were
498
      // decoding the image to pixels.
499
938
      if (seen_need_image_out && spec.use_callback && spec.coalescing) {
500
        // Check that the callback sent all the pixels
501
0
        for (uint32_t y = 0; y < decode_callback_data.ysize; y++) {
502
0
          const auto& deltas = decode_callback_data.called_rows[y].delta;
503
0
          if (deltas[0] != static_cast<int>(num_frames)) abort();
504
0
          if (deltas[decode_callback_data.xsize] != -1) abort();
505
0
          for (size_t i = 1; i < decode_callback_data.xsize; ++i) {
506
0
            if (deltas[i] != 0) abort();
507
0
          }
508
0
        }
509
0
      }
510
511
      // All decoding successfully finished.
512
      // It's not required to call JxlDecoderReleaseInput(dec.get()) here since
513
      // the decoder will be destroyed.
514
938
      return true;
515
55.0k
    } else if (status == JXL_DEC_BOX) {
516
49.1k
      if (spec.decode_boxes) {
517
17.5k
        if (!box_buffer.empty()) {
518
14.8k
          size_t remaining = JxlDecoderReleaseBoxBuffer(dec.get());
519
14.8k
          size_t box_size = box_buffer.size() - remaining;
520
14.8k
          if (box_size != 0) {
521
14.5k
            Consume(box_buffer.begin(), box_buffer.begin() + box_size);
522
14.5k
            box_buffer.clear();
523
14.5k
          }
524
14.8k
        }
525
17.5k
        box_buffer.resize(64);
526
17.5k
        JxlDecoderSetBoxBuffer(dec.get(), box_buffer.data(), box_buffer.size());
527
17.5k
      }
528
49.1k
    } else if (status == JXL_DEC_BOX_NEED_MORE_OUTPUT) {
529
5.89k
      if (!spec.decode_boxes) {
530
0
        abort();  // Not expected when not setting output buffer
531
0
      }
532
5.89k
      size_t remaining = JxlDecoderReleaseBoxBuffer(dec.get());
533
5.89k
      size_t box_size = box_buffer.size() - remaining;
534
5.89k
      box_buffer.resize(box_buffer.size() * 2);
535
5.89k
      JxlDecoderSetBoxBuffer(dec.get(), box_buffer.data() + box_size,
536
5.89k
                             box_buffer.size() - box_size);
537
5.89k
    } else {
538
0
      return false;
539
0
    }
540
733k
  }
541
20.5k
}
542
543
20.5k
int DoTestOneInput(const uint8_t* data, size_t size) {
544
20.5k
  if (size < 4) return 0;
545
20.5k
  uint32_t flags = 0;
546
20.5k
  size_t used_flag_bits = 0;
547
20.5k
  memcpy(&flags, data + size - 4, 4);
548
20.5k
  size -= 4;
549
550
246k
  const auto getFlag = [&flags, &used_flag_bits](size_t max_value) {
551
246k
    size_t limit = 1;
552
657k
    while (limit <= max_value) {
553
410k
      limit <<= 1;
554
410k
      used_flag_bits++;
555
410k
      if (used_flag_bits > 32) abort();
556
410k
    }
557
246k
    uint32_t result = flags % limit;
558
246k
    flags /= limit;
559
246k
    return result % (max_value + 1);
560
246k
  };
561
164k
  const auto getBoolFlag = [&getFlag]() -> bool {
562
164k
    return static_cast<bool>(getFlag(1));
563
164k
  };
564
565
20.5k
  FuzzSpec spec;
566
  // Allows some different possible variations in the chunk sizes of the
567
  // streaming case
568
20.5k
  spec.random_seed = flags ^ size;
569
20.5k
  spec.get_alpha = getBoolFlag();
570
20.5k
  spec.get_grayscale = getBoolFlag();
571
20.5k
  spec.use_streaming = getBoolFlag();
572
20.5k
  spec.jpeg_to_pixels = getBoolFlag();
573
20.5k
  spec.use_callback = getBoolFlag();
574
20.5k
  spec.keep_orientation = getBoolFlag();
575
20.5k
  spec.coalescing = getBoolFlag();
576
20.5k
  spec.output_type = static_cast<JxlDataType>(getFlag(JXL_TYPE_FLOAT16));
577
20.5k
  spec.output_endianness = static_cast<JxlEndianness>(getFlag(JXL_BIG_ENDIAN));
578
20.5k
  spec.output_align = getFlag(16);
579
20.5k
  spec.decode_boxes = getBoolFlag();
580
581
20.5k
  std::vector<uint8_t> pixels;
582
20.5k
  std::vector<uint8_t> jpeg;
583
20.5k
  std::vector<uint8_t> icc;
584
20.5k
  size_t xsize;
585
20.5k
  size_t ysize;
586
20.5k
  size_t max_pixels = 1 << 21;
587
20.5k
  size_t max_total_pixels = 5 * max_pixels;
588
589
20.5k
  TrackingMemoryManager memory_manager{/* cap */ 1 * kGiB,
590
20.5k
                                       /* total_cap */ 5 * kGiB};
591
20.5k
  const auto targets = hwy::SupportedAndGeneratedTargets();
592
20.5k
  hwy::SetSupportedTargetsForTest(targets[getFlag(targets.size() - 1)]);
593
20.5k
  DecodeJpegXl(data, size, memory_manager.get(), max_pixels, max_total_pixels,
594
20.5k
               spec, &pixels, &jpeg, &xsize, &ysize, &icc);
595
20.5k
  hwy::SetSupportedTargetsForTest(0);
596
20.5k
  Check(memory_manager.Reset());
597
598
20.5k
  return 0;
599
20.5k
}
600
601
}  // namespace
602
603
63.5k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
604
63.5k
  return DoTestOneInput(data, size);
605
63.5k
}
606
607
0
void TestOneInput(const std::vector<uint8_t>& data) {
608
0
  DoTestOneInput(data.data(), data.size());
609
0
}
610
611
FUZZ_TEST(DjxlFuzzTest, TestOneInput);