Coverage Report

Created: 2024-10-11 15:22

/src/wuffs/fuzz/c/std/png_fuzzer.c
Line
Count
Source (jump to first uncovered line)
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 png_fuzzer.c
25
./a.out ../../../test/data/*.png
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__ADLER32
62
#define WUFFS_CONFIG__MODULE__BASE
63
#define WUFFS_CONFIG__MODULE__CRC32
64
#define WUFFS_CONFIG__MODULE__DEFLATE
65
#define WUFFS_CONFIG__MODULE__PNG
66
#define WUFFS_CONFIG__MODULE__ZLIB
67
68
// If building this program in an environment that doesn't easily accommodate
69
// relative includes, you can use the script/inline-c-relative-includes.go
70
// program to generate a stand-alone C file.
71
#include "../../../release/c/wuffs-unsupported-snapshot.c"
72
#include "../fuzzlib/fuzzlib.c"
73
#include "../fuzzlib/fuzzlib_image_decoder.c"
74
75
const char*  //
76
15.9k
fuzz(wuffs_base__io_buffer* src, uint64_t hash) {
77
15.9k
  wuffs_png__decoder dec;
78
15.9k
  wuffs_base__status status = wuffs_png__decoder__initialize(
79
15.9k
      &dec, sizeof dec, WUFFS_VERSION,
80
15.9k
      (hash & 1) ? WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED : 0);
81
15.9k
  hash = wuffs_base__u64__rotate_right(hash, 1);
82
15.9k
  if (!wuffs_base__status__is_ok(&status)) {
83
0
    return wuffs_base__status__message(&status);
84
0
  }
85
15.9k
  return fuzz_image_decoder(
86
15.9k
      src, hash,
87
15.9k
      wuffs_png__decoder__upcast_as__wuffs_base__image_decoder(&dec));
88
15.9k
}