4 * Copyright Red Hat Inc., 2019
7 * Alexander Bulekov <alxndr@bu.edu>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
17 #include "qemu/units.h"
18 #include "qapi/error.h"
20 #include "tests/qtest/libqtest.h"
23 * A libfuzzer fuzzing target
25 * The QEMU fuzzing binary is built with all available targets, each
26 * with a unique @name that can be specified on the command-line to
27 * select which target should run.
29 * A target must implement ->fuzz() to process a random input. If QEMU
30 * crashes in ->fuzz() then libfuzzer will record a failure.
32 * Fuzzing targets are registered with fuzz_add_target():
34 * static const FuzzTarget fuzz_target = {
35 * .name = "my-device-fifo",
36 * .description = "Fuzz the FIFO buffer registers of my-device",
40 * static void register_fuzz_target(void)
42 * fuzz_add_target(&fuzz_target);
44 * fuzz_target_init(register_fuzz_target);
46 typedef struct FuzzTarget
{
47 const char *name
; /* target identifier (passed to --fuzz-target=)*/
48 const char *description
; /* help text */
52 * Returns the arguments that are passed to qemu/system init(). Freed by
55 GString
*(*get_init_cmdline
)(struct FuzzTarget
*);
58 * will run once, prior to running qemu/system init.
59 * eg: set up shared-memory for communication with the child-process
62 void(*pre_vm_init
)(void);
65 * will run once, after QEMU has been initialized, prior to the fuzz-loop.
66 * eg: detect the memory map
69 void(*pre_fuzz
)(QTestState
*);
72 * accepts and executes an input from libfuzzer. this is repeatedly
73 * executed during the fuzzing loop. Its should handle setup, input
74 * execution and cleanup.
77 void(*fuzz
)(QTestState
*, const unsigned char *, size_t);
80 * The fuzzer can specify a "Custom Crossover" function for combining two
81 * inputs from the corpus. This function is sometimes called by libfuzzer
82 * when mutating inputs.
84 * data1: location of first input
85 * size1: length of first input
86 * data1: location of second input
87 * size1: length of second input
88 * out: where to place the resulting, mutated input
89 * max_out_size: the maximum length of the input that can be placed in out
90 * seed: the seed that should be used to make mutations deterministic, when
93 * See libfuzzer's LLVMFuzzerCustomCrossOver API for more info.
97 size_t(*crossover
)(const uint8_t *data1
, size_t size1
,
98 const uint8_t *data2
, size_t size2
,
99 uint8_t *out
, size_t max_out_size
,
105 void flush_events(QTestState
*);
106 void fuzz_reset(QTestState
*);
108 /* Use the QTest ASCII protocol or call address_space API directly?*/
109 void fuzz_qtest_set_serialize(bool option
);
112 * makes a copy of *target and adds it to the target-list.
113 * i.e. fine to set up target on the caller's stack
115 void fuzz_add_target(const FuzzTarget
*target
);
117 size_t LLVMFuzzerCustomCrossOver(const uint8_t *data1
, size_t size1
,
118 const uint8_t *data2
, size_t size2
,
119 uint8_t *out
, size_t max_out_size
,
121 int LLVMFuzzerTestOneInput(const unsigned char *Data
, size_t Size
);
122 int LLVMFuzzerInitialize(int *argc
, char ***argv
, char ***envp
);