Debian Codegraph @

src/fuzz/fuzz.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdio.h>
5
6 #include "shared-forward.h"
7
8 #include "fileio.h"
9 #include "log.h"
10 #include "log-assert-critical.h"
11
12 /* The entry point into the fuzzer */
13 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
14
15 static inline FILE* data_to_file(const uint8_t *data, size_t size) {
16 if (size == 0)
17 return fopen("/dev/null", "re");
18 else
19 return fmemopen_unlocked((char*) data, size, "r");
20 }
21
22 /* Check if we are within the specified size range.
23 * The upper limit is ignored if FUZZ_USE_SIZE_LIMIT is unset.
24 */
25 static inline bool outside_size_range(size_t size, size_t lower, size_t upper) {
26 if (size < lower)
27 return true;
28 if (size > upper)
29 return FUZZ_USE_SIZE_LIMIT;
30 return false;
31 }
32
33 static inline void fuzz_setup_logging(void) {
34 /* We don't want to fill the logs and slow down stuff when running
35 * in a fuzzing mode, so disable most of the logging. */
36 log_set_assert_return_is_critical(true);
37 log_set_max_level(LOG_CRIT);
38 log_setup();
39 }
40
41 /* Force value to not be optimized away. */
42 #define DO_NOT_OPTIMIZE(value) ({ asm volatile("" : : "g"(value) : "memory"); })