Coverage Report

Created: 2026-11-12 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wuffs/fuzz/c/std/json_fuzzer.cc
Line
Count
Source
1
// Copyright 2020 The Wuffs Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6
// option. This file may not be copied, modified, or distributed
7
// except according to those terms.
8
//
9
// SPDX-License-Identifier: Apache-2.0 OR MIT
10
11
// ----------------
12
13
// Silence the nested slash-star warning for the next comment's command line.
14
#pragma clang diagnostic push
15
#pragma clang diagnostic ignored "-Wcomment"
16
17
/*
18
This fuzzer (the fuzz function) is typically run indirectly, by a framework
19
such as https://github.com/google/oss-fuzz calling LLVMFuzzerTestOneInput.
20
21
When working on the fuzz implementation, or as a coherence check, defining
22
WUFFS_CONFIG__FUZZLIB_MAIN will let you manually run fuzz over a set of files:
23
24
gcc -DWUFFS_CONFIG__FUZZLIB_MAIN json_fuzzer.c
25
./a.out ../../../test/data/*.json
26
rm -f ./a.out
27
28
It should print "PASS", amongst other information, and exit(0).
29
*/
30
31
#pragma clang diagnostic pop
32
33
// Wuffs ships as a "single file C library" or "header file library" as per
34
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
35
//
36
// To use that single file as a "foo.c"-like implementation, instead of a
37
// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
38
// compiling it.
39
#define WUFFS_IMPLEMENTATION
40
41
#if defined(WUFFS_CONFIG__FUZZLIB_MAIN)
42
// Defining the WUFFS_CONFIG__STATIC_FUNCTIONS macro is optional, but when
43
// combined with WUFFS_IMPLEMENTATION, it demonstrates making all of Wuffs'
44
// functions have static storage.
45
//
46
// This can help the compiler ignore or discard unused code, which can produce
47
// faster compiles and smaller binaries. Other motivations are discussed in the
48
// "ALLOW STATIC IMPLEMENTATION" section of
49
// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt
50
#define WUFFS_CONFIG__STATIC_FUNCTIONS
51
#endif  // defined(WUFFS_CONFIG__FUZZLIB_MAIN)
52
53
// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
54
// release/c/etc.c choose which parts of Wuffs to build. That file contains the
55
// entire Wuffs standard library, implementing a variety of codecs and file
56
// formats. Without this macro definition, an optimizing compiler or linker may
57
// very well discard Wuffs code for unused codecs, but listing the Wuffs
58
// modules we use makes that process explicit. Preprocessing means that such
59
// code simply isn't compiled.
60
#define WUFFS_CONFIG__MODULES
61
#define WUFFS_CONFIG__MODULE__AUX__BASE
62
#define WUFFS_CONFIG__MODULE__AUX__JSON
63
#define WUFFS_CONFIG__MODULE__BASE
64
#define WUFFS_CONFIG__MODULE__JSON
65
66
// If building this program in an environment that doesn't easily accommodate
67
// relative includes, you can use the script/inline-c-relative-includes.go
68
// program to generate a stand-alone C file.
69
#include "../../../release/c/wuffs-unsupported-snapshot.c"
70
#include "../fuzzlib/fuzzlib.c"
71
72
12.2k
#define TOK_BUFFER_ARRAY_SIZE 4096
73
328k
#define STACK_SIZE (WUFFS_JSON__DECODER_DEPTH_MAX_INCL + 1)
74
75
// Wuffs allows either statically or dynamically allocated work buffers. This
76
// program exercises static allocation.
77
#define WORK_BUFFER_ARRAY_SIZE \
78
3.47M
  WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
79
#if WORK_BUFFER_ARRAY_SIZE > 0
80
uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
81
#else
82
// Not all C/C++ compilers support 0-length arrays.
83
uint8_t g_work_buffer_array[1];
84
#endif
85
86
// Each stack element is 1 byte. The low 7 bits denote the container:
87
//  - 0x01 means no container: we are at the top level.
88
//  - 0x02 means a [] list.
89
//  - 0x04 means a {} dictionary.
90
//
91
// The high 0x80 bit holds the even/odd-ness of the number of elements in that
92
// container. A valid dictionary contains key-value pairs and should therefore
93
// contain an even number of elements.
94
typedef uint8_t stack_element;
95
96
const char*  //
97
fuzz_one_token(wuffs_base__token t,
98
               wuffs_base__token prev_token,
99
               wuffs_base__io_buffer* src,
100
               size_t* ti,
101
               stack_element* stack,
102
6.37M
               size_t* depth) {
103
6.37M
  uint64_t len = wuffs_base__token__length(&t);
104
6.37M
  if (len > 0xFFFF) {
105
0
    return "fuzz: internal error: length too long (vs 0xFFFF)";
106
6.37M
  } else if (len > (src->meta.wi - *ti)) {
107
0
    return "fuzz: internal error: length too long (vs wi - ti)";
108
0
  }
109
6.37M
  *ti += len;
110
111
6.37M
  if ((wuffs_base__token__value_extension(&t) >= 0) &&
112
0
      !wuffs_base__token__continued(&prev_token)) {
113
0
    return "fuzz: internal error: extended token not after continued token";
114
0
  }
115
116
6.37M
  int64_t vbc = wuffs_base__token__value_base_category(&t);
117
6.37M
  uint64_t vbd = wuffs_base__token__value_base_detail(&t);
118
119
6.37M
  switch (vbc) {
120
588k
    case WUFFS_BASE__TOKEN__VBC__STRUCTURE: {
121
588k
      bool from_consistent = false;
122
588k
      if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE) {
123
1.98k
        from_consistent = stack[*depth] & 0x01;
124
586k
      } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) {
125
580k
        from_consistent = stack[*depth] & 0x02;
126
580k
      } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT) {
127
5.45k
        from_consistent = stack[*depth] & 0x04;
128
5.45k
      }
129
588k
      if (!from_consistent) {
130
0
        return "fuzz: internal error: inconsistent VBD__STRUCTURE__FROM_ETC";
131
0
      }
132
133
588k
      if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {
134
328k
        (*depth)++;
135
328k
        if ((*depth >= STACK_SIZE) || (*depth == 0)) {
136
0
          return "fuzz: internal error: depth too large";
137
0
        }
138
139
328k
        if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE) {
140
0
          return "fuzz: internal error: push to the 'none' container";
141
328k
        } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) {
142
323k
          stack[*depth] = 0x02;
143
323k
        } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT) {
144
5.39k
          stack[*depth] = 0x04;
145
5.39k
        } else {
146
0
          return "fuzz: internal error: unrecognized VBD__STRUCTURE__TO_ETC";
147
0
        }
148
149
328k
      } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP) {
150
259k
        if ((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT) &&
151
3.22k
            (0 != (0x80 & stack[*depth]))) {
152
0
          return "fuzz: internal error: dictionary had an incomplete key/value "
153
0
                 "pair";
154
0
        }
155
156
259k
        if (*depth <= 0) {
157
0
          return "fuzz: internal error: depth too small";
158
0
        }
159
259k
        (*depth)--;
160
161
259k
        bool to_consistent = false;
162
259k
        if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE) {
163
77
          to_consistent = stack[*depth] & 0x01;
164
259k
        } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) {
165
258k
          to_consistent = stack[*depth] & 0x02;
166
258k
        } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT) {
167
599
          to_consistent = stack[*depth] & 0x04;
168
599
        }
169
259k
        if (!to_consistent) {
170
0
          return "fuzz: internal error: inconsistent VBD__STRUCTURE__TO_ETC";
171
0
        }
172
173
259k
      } else {
174
0
        return "fuzz: internal error: unrecognized VBC__STRUCTURE";
175
0
      }
176
588k
      break;
177
588k
    }
178
179
588k
    case WUFFS_BASE__TOKEN__VBC__STRING: {
180
155k
      if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {
181
123k
        wuffs_base__slice_u8 s =
182
123k
            wuffs_base__make_slice_u8(src->data.ptr + *ti - len, len);
183
123k
        if ((vbd & WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_UTF_8) &&
184
123k
            (s.len != wuffs_base__utf_8__longest_valid_prefix(s.ptr, s.len))) {
185
0
          return "fuzz: internal error: invalid UTF-8";
186
0
        }
187
123k
        if ((vbd & WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_ASCII) &&
188
0
            (s.len != wuffs_base__ascii__longest_valid_prefix(s.ptr, s.len))) {
189
0
          return "fuzz: internal error: invalid ASCII";
190
0
        }
191
123k
      }
192
155k
      break;
193
155k
    }
194
195
4.17M
    case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: {
196
4.17M
      if ((WUFFS_BASE__UNICODE_SURROGATE__MIN_INCL <= vbd) &&
197
4.15M
          (vbd <= WUFFS_BASE__UNICODE_SURROGATE__MAX_INCL)) {
198
0
        return "fuzz: internal error: invalid Unicode surrogate";
199
4.17M
      } else if (WUFFS_BASE__UNICODE_CODE_POINT__MAX_INCL < vbd) {
200
0
        return "fuzz: internal error: invalid Unicode code point";
201
0
      }
202
4.17M
      break;
203
4.17M
    }
204
205
4.17M
    default:
206
1.45M
      break;
207
6.37M
  }
208
209
  // After a complete JSON value, update the parity (even/odd count) of the
210
  // container.
211
6.37M
  if (!wuffs_base__token__continued(&t) &&
212
1.93M
      (vbc != WUFFS_BASE__TOKEN__VBC__FILLER) &&
213
1.01M
      ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) ||
214
690k
       (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP))) {
215
690k
    stack[*depth] ^= 0x80;
216
690k
  }
217
218
6.37M
  return NULL;
219
6.37M
}
220
221
uint64_t  //
222
12.0k
buffer_limit(uint64_t hash, uint64_t min, uint64_t max) {
223
12.0k
  hash &= 0x3F;
224
12.0k
  uint64_t n;
225
12.0k
  if (hash < 0x20) {
226
6.11k
    n = min + hash;
227
6.11k
  } else {
228
5.95k
    n = max - (0x3F - hash);
229
5.95k
  }
230
12.0k
  if (n < min) {
231
0
    return min;
232
12.0k
  } else if (n > max) {
233
0
    return max;
234
0
  }
235
12.0k
  return n;
236
12.0k
}
237
238
uint32_t g_quirks[] = {
239
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_A,
240
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_CAPITAL_U,
241
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_E,
242
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_QUESTION_MARK,
243
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE,
244
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_V,
245
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_CODE_POINTS,
246
    WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_ZERO,
247
    WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK,
248
    WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE,
249
    WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA,
250
    WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS,
251
    WUFFS_JSON__QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR,
252
    WUFFS_JSON__QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK,
253
    WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER,
254
    WUFFS_JSON__QUIRK_JSON_POINTER_ALLOW_TILDE_N_TILDE_R_TILDE_T,
255
    WUFFS_JSON__QUIRK_REPLACE_INVALID_UNICODE,
256
    0,
257
};
258
259
6.03k
void set_quirks(wuffs_json__decoder* dec, uint64_t hash) {
260
108k
  for (uint32_t i = 0; g_quirks[i]; i++) {
261
102k
    uint64_t bit = 1 << (i & 63);
262
102k
    if (hash & bit) {
263
50.6k
      wuffs_json__decoder__set_quirk(dec, g_quirks[i], 1);
264
50.6k
    }
265
102k
  }
266
6.03k
}
267
268
const char*  //
269
6.03k
fuzz_complex(wuffs_base__io_buffer* full_src, uint64_t hash) {
270
6.03k
  uint64_t tok_limit = buffer_limit(
271
6.03k
      hash & 0x3F, WUFFS_JSON__DECODER_DST_TOKEN_BUFFER_LENGTH_MIN_INCL,
272
6.03k
      TOK_BUFFER_ARRAY_SIZE);
273
6.03k
  hash = wuffs_base__u64__rotate_right(hash, 6);
274
275
6.03k
  uint64_t src_limit = buffer_limit(
276
6.03k
      hash & 0x3F, WUFFS_JSON__DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL, 4096);
277
6.03k
  hash = wuffs_base__u64__rotate_right(hash, 6);
278
279
  // ----
280
281
6.03k
  wuffs_json__decoder dec;
282
6.03k
  wuffs_base__status status = wuffs_json__decoder__initialize(
283
6.03k
      &dec, sizeof dec, WUFFS_VERSION,
284
6.03k
      WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED);
285
6.03k
  if (!wuffs_base__status__is_ok(&status)) {
286
0
    return wuffs_base__status__message(&status);
287
0
  }
288
6.03k
  set_quirks(&dec, hash);
289
290
6.03k
  wuffs_base__token tok_array[TOK_BUFFER_ARRAY_SIZE];
291
6.03k
  wuffs_base__token_buffer tok = ((wuffs_base__token_buffer){
292
6.03k
      .data = ((wuffs_base__slice_token){
293
6.03k
          .ptr = tok_array,
294
6.03k
          .len = (size_t)((tok_limit < TOK_BUFFER_ARRAY_SIZE)
295
6.03k
                              ? tok_limit
296
6.03k
                              : TOK_BUFFER_ARRAY_SIZE),
297
6.03k
      }),
298
6.03k
  });
299
300
6.03k
  wuffs_base__token prev_token = wuffs_base__make_token(0);
301
6.03k
  uint32_t no_progress_count = 0;
302
303
6.03k
  stack_element stack[STACK_SIZE];
304
6.03k
  stack[0] = 0x01;  // We start in the 'none' container.
305
6.03k
  size_t depth = 0;
306
307
  // ----
308
309
3.47M
  while (true) {  // Outer loop.
310
3.47M
    wuffs_base__io_buffer src = make_limited_reader(*full_src, src_limit);
311
312
3.47M
    size_t old_tok_wi = tok.meta.wi;
313
3.47M
    size_t old_tok_ri = tok.meta.ri;
314
3.47M
    size_t old_src_wi = src.meta.wi;
315
3.47M
    size_t old_src_ri = src.meta.ri;
316
3.47M
    size_t ti = old_src_ri;
317
318
3.47M
    status = wuffs_json__decoder__decode_tokens(
319
3.47M
        &dec, &tok, &src,
320
3.47M
        wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE));
321
3.47M
    if ((tok.data.len < tok.meta.wi) ||  //
322
3.47M
        (tok.meta.wi < tok.meta.ri) ||   //
323
3.47M
        (tok.meta.ri != old_tok_ri)) {
324
0
      return "fuzz: internal error: inconsistent tok indexes";
325
3.47M
    } else if ((src.data.len < src.meta.wi) ||  //
326
3.47M
               (src.meta.wi < src.meta.ri) ||   //
327
3.47M
               (src.meta.wi != old_src_wi)) {
328
0
      return "fuzz: internal error: inconsistent src indexes";
329
0
    }
330
3.47M
    full_src->meta.ri += src.meta.ri - old_src_ri;
331
332
3.47M
    if ((tok.meta.wi > old_tok_wi) || (src.meta.ri > old_src_ri) ||
333
2.18M
        !wuffs_base__status__is_suspension(&status)) {
334
1.29M
      no_progress_count = 0;
335
2.17M
    } else if (no_progress_count < 999) {
336
2.17M
      no_progress_count++;
337
2.17M
    } else if (!full_src->meta.closed &&
338
2.16k
               (status.repr == wuffs_base__suspension__short_read)) {
339
2.16k
      return wuffs_base__status__message(&status);
340
2.16k
    } else {
341
0
      return "fuzz: internal error: no progress";
342
0
    }
343
344
    // ----
345
346
9.84M
    while (tok.meta.ri < tok.meta.wi) {  // Inner loop.
347
6.37M
      wuffs_base__token t = tok.data.ptr[tok.meta.ri++];
348
6.37M
      const char* z =
349
6.37M
          fuzz_one_token(t, prev_token, &src, &ti, &stack[0], &depth);
350
6.37M
      if (z != NULL) {
351
0
        return z;
352
0
      }
353
6.37M
      prev_token = t;
354
6.37M
    }  // Inner loop.
355
356
    // ----
357
358
    // Check that, starting from old_src_ri, summing the token lengths brings
359
    // us to the new src.meta.ri.
360
3.47M
    if (ti != src.meta.ri) {
361
0
      return "fuzz: internal error: ti != ri";
362
0
    }
363
364
3.47M
    if (status.repr == NULL) {
365
1.27k
      break;
366
367
3.47M
    } else if (status.repr == wuffs_base__suspension__short_read) {
368
2.38M
      if (src.meta.closed) {
369
0
        return "fuzz: internal error: short read on a closed io_reader";
370
0
      }
371
      // We don't compact full_src as it may be mmap'ed read-only.
372
2.38M
      continue;
373
374
2.38M
    } else if (status.repr == wuffs_base__suspension__short_write) {
375
1.08M
      wuffs_base__token_buffer__compact(&tok);
376
1.08M
      continue;
377
1.08M
    }
378
379
2.59k
    return wuffs_base__status__message(&status);
380
3.47M
  }  // Outer loop.
381
382
  // ----
383
384
1.27k
  if (depth != 0) {
385
0
    return "fuzz: internal error: decoded OK but final depth was not zero";
386
1.27k
  } else if (wuffs_base__token__continued(&prev_token)) {
387
0
    return "fuzz: internal error: decoded OK but final token was continued";
388
0
  }
389
1.27k
  return NULL;
390
1.27k
}
391
392
const char*  //
393
96
fuzz_simple(wuffs_base__io_buffer* full_src) {
394
96
  wuffs_json__decoder dec;
395
96
  wuffs_base__status status =
396
96
      wuffs_json__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION, 0);
397
96
  if (!wuffs_base__status__is_ok(&status)) {
398
0
    return wuffs_base__status__message(&status);
399
0
  }
400
401
96
  wuffs_base__token tok_array[TOK_BUFFER_ARRAY_SIZE];
402
96
  wuffs_base__token_buffer tok = ((wuffs_base__token_buffer){
403
96
      .data = ((wuffs_base__slice_token){
404
96
          .ptr = tok_array,
405
96
          .len = TOK_BUFFER_ARRAY_SIZE,
406
96
      }),
407
96
  });
408
409
291
  while (true) {
410
291
    status = wuffs_json__decoder__decode_tokens(
411
291
        &dec, &tok, full_src,
412
291
        wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE));
413
291
    if (status.repr == NULL) {
414
9
      break;
415
416
282
    } else if (status.repr == wuffs_base__suspension__short_write) {
417
195
      tok.meta.ri = tok.meta.wi;
418
195
      wuffs_base__token_buffer__compact(&tok);
419
195
      continue;
420
195
    }
421
422
87
    return wuffs_base__status__message(&status);
423
291
  }
424
425
9
  return NULL;
426
96
}
427
428
#if defined(__cplusplus)
429
#include <vector>
430
431
class Callbacks : public wuffs_aux::DecodeJsonCallbacks {
432
 public:
433
6.12k
  Callbacks() : m_depth(0) {}
434
435
2.80k
  std::string AppendNull() override { return ""; }
436
437
3.95k
  std::string AppendBool(bool val) override { return ""; }
438
439
390k
  std::string AppendI64(int64_t val) override { return ""; }
440
441
72.2k
  std::string AppendF64(double val) override { return ""; }
442
443
5.14k
  std::string AppendTextString(std::string&& val) override { return ""; }
444
445
129k
  std::string Push(uint32_t flags) override {
446
129k
    m_depth++;
447
129k
    return "";
448
129k
  }
449
450
87.1k
  std::string Pop(uint32_t flags) override {
451
87.1k
    m_depth--;
452
87.1k
    if (m_depth < 0) {
453
0
      fprintf(stderr, "negative depth\n");
454
0
      intentional_segfault();
455
0
    }
456
87.1k
    return "";
457
87.1k
  }
458
459
  void Done(wuffs_aux::DecodeJsonResult& result,
460
            wuffs_aux::sync_io::Input& input,
461
6.12k
            wuffs_aux::IOBuffer& buffer) override {
462
6.12k
    if (result.error_message.empty()) {
463
2.02k
      if (m_depth != 0) {
464
0
        fprintf(stderr, "no error message but final depth is non-zero\n");
465
0
        intentional_segfault();
466
0
      }
467
4.10k
    } else if (result.error_message.find("internal error:") !=
468
4.10k
               std::string::npos) {
469
0
      fprintf(stderr, "internal errors shouldn't occur: \"%s\"\n",
470
0
              result.error_message.c_str());
471
0
      intentional_segfault();
472
0
    }
473
6.12k
  }
474
475
 private:
476
  int64_t m_depth;
477
};
478
479
void  //
480
6.12k
fuzz_cpp(const uint8_t* in_ptr, size_t in_len, uint64_t hash) {
481
6.12k
  static const char* json_pointers[16] = {
482
6.12k
      "",           //
483
6.12k
      "",           //
484
6.12k
      "",           //
485
6.12k
      "",           //
486
6.12k
      "",           //
487
6.12k
      "",           //
488
6.12k
      "",           //
489
6.12k
      "",           //
490
6.12k
      "",           //
491
6.12k
      "",           //
492
6.12k
      "/",          //
493
6.12k
      "/2/3/4/5",   //
494
6.12k
      "/k0",        //
495
6.12k
      "/k0/1",      //
496
6.12k
      "/x/y",       //
497
6.12k
      "/~0/~1/~n",  //
498
6.12k
  };
499
6.12k
  const char* json_pointer = json_pointers[hash & 15];
500
6.12k
  hash = wuffs_base__u64__rotate_right(hash, 4);
501
502
6.12k
  std::vector<wuffs_aux::QuirkKeyValuePair> quirks;
503
110k
  for (uint32_t i = 0; g_quirks[i]; i++) {
504
104k
    uint64_t bit = 1 << (i & 63);
505
104k
    if (hash & bit) {
506
51.7k
      quirks.push_back({g_quirks[i], 1});
507
51.7k
    }
508
104k
  }
509
510
6.12k
  Callbacks callbacks;
511
6.12k
  wuffs_aux::sync_io::MemoryInput input(in_ptr, in_len);
512
6.12k
  wuffs_aux::DecodeJson(
513
6.12k
      callbacks, input,
514
6.12k
      wuffs_aux::DecodeJsonArgQuirks(quirks.data(), quirks.size()),
515
6.12k
      wuffs_aux::DecodeJsonArgJsonPointer(json_pointer));
516
6.12k
}
517
#endif  // defined(__cplusplus)
518
519
const char*  //
520
6.12k
fuzz(wuffs_base__io_buffer* full_src, uint64_t hash) {
521
6.12k
#if defined(__cplusplus)
522
6.12k
  fuzz_cpp(full_src->reader_pointer(), full_src->reader_length(),
523
6.12k
           wuffs_base__u64__rotate_right(hash, 32));
524
6.12k
#endif  // defined(__cplusplus)
525
526
  // Send 99.6% of inputs to fuzz_complex and the remainder to fuzz_simple. The
527
  // 0xA5 constant is arbitrary but non-zero. If the hash function maps the
528
  // empty input to 0, this still sends the empty input to fuzz_complex.
529
  //
530
  // The fuzz_simple implementation shows how easy decoding with Wuffs is when
531
  // all you want is to run LLVMFuzzerTestOneInput's built-in (Wuffs API
532
  // independent) checks (e.g. the ASan address sanitizer) and you don't really
533
  // care what the output is, just that it doesn't crash.
534
  //
535
  // The fuzz_complex implementation adds many more Wuffs API specific checks
536
  // (e.g. that the sum of the tokens' lengths do not exceed the input length).
537
6.12k
  if ((hash & 0xFF) != 0xA5) {
538
6.03k
    return fuzz_complex(full_src, wuffs_base__u64__rotate_right(hash, 8));
539
6.03k
  }
540
96
  return fuzz_simple(full_src);
541
6.12k
}