/src/wuffs/fuzz/c/std/bmp_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 bmp_fuzzer.c  | 
25  |  | ./a.out ../../../test/data/*.bmp  | 
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__BASE  | 
62  |  | #define WUFFS_CONFIG__MODULE__BMP  | 
63  |  |  | 
64  |  | // If building this program in an environment that doesn't easily accommodate  | 
65  |  | // relative includes, you can use the script/inline-c-relative-includes.go  | 
66  |  | // program to generate a stand-alone C file.  | 
67  |  | #include "../../../release/c/wuffs-unsupported-snapshot.c"  | 
68  |  | #include "../fuzzlib/fuzzlib.c"  | 
69  |  | #include "../fuzzlib/fuzzlib_image_decoder.c"  | 
70  |  |  | 
71  |  | const char*  //  | 
72  | 15.9k  | fuzz(wuffs_base__io_buffer* src, uint64_t hash) { | 
73  | 15.9k  |   wuffs_bmp__decoder dec;  | 
74  | 15.9k  |   wuffs_base__status status = wuffs_bmp__decoder__initialize(  | 
75  | 15.9k  |       &dec, sizeof dec, WUFFS_VERSION,  | 
76  | 15.9k  |       (hash & 1) ? WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED : 0);  | 
77  | 15.9k  |   hash = wuffs_base__u64__rotate_right(hash, 1);  | 
78  | 15.9k  |   if (!wuffs_base__status__is_ok(&status)) { | 
79  | 0  |     return wuffs_base__status__message(&status);  | 
80  | 0  |   }  | 
81  | 15.9k  |   return fuzz_image_decoder(  | 
82  | 15.9k  |       src, hash,  | 
83  | 15.9k  |       wuffs_bmp__decoder__upcast_as__wuffs_base__image_decoder(&dec));  | 
84  | 15.9k  | }  |