Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/tests/qtest/fuzz/i440fx_fuzz.c
Line
Count
Source
1
/*
2
 * I440FX Fuzzing Target
3
 *
4
 * Copyright Red Hat Inc., 2019
5
 *
6
 * Authors:
7
 *  Alexander Bulekov   <alxndr@bu.edu>
8
 *
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.
11
 */
12
13
#include "qemu/osdep.h"
14
15
#include "qemu/main-loop.h"
16
#include "tests/qtest/libqtest.h"
17
#include "tests/qtest/libqos/pci.h"
18
#include "tests/qtest/libqos/pci-pc.h"
19
#include "fuzz.h"
20
#include "qos_fuzz.h"
21
22
23
0
#define I440FX_PCI_HOST_BRIDGE_CFG 0xcf8
24
0
#define I440FX_PCI_HOST_BRIDGE_DATA 0xcfc
25
26
/*
27
 * the input to the fuzzing functions below is a buffer of random bytes. we
28
 * want to convert these bytes into a sequence of qtest or qos calls. to do
29
 * this we define some opcodes:
30
 */
31
enum action_id {
32
    WRITEB,
33
    WRITEW,
34
    WRITEL,
35
    READB,
36
    READW,
37
    READL,
38
    ACTION_MAX
39
};
40
41
static void ioport_fuzz_qtest(QTestState *s,
42
0
        const unsigned char *Data, size_t Size) {
43
    /*
44
     * loop over the Data, breaking it up into actions. each action has an
45
     * opcode, address offset and value
46
     */
47
0
    struct {
48
0
        uint8_t opcode;
49
0
        uint8_t addr;
50
0
        uint32_t value;
51
0
    } a;
52
53
0
    while (Size >= sizeof(a)) {
54
        /* make a copy of the action so we can normalize the values in-place */
55
0
        memcpy(&a, Data, sizeof(a));
56
        /* select between two i440fx Port IO addresses */
57
0
        uint16_t addr = a.addr % 2 ? I440FX_PCI_HOST_BRIDGE_CFG :
58
0
                                      I440FX_PCI_HOST_BRIDGE_DATA;
59
0
        switch (a.opcode % ACTION_MAX) {
60
0
        case WRITEB:
61
0
            qtest_outb(s, addr, (uint8_t)a.value);
62
0
            break;
63
0
        case WRITEW:
64
0
            qtest_outw(s, addr, (uint16_t)a.value);
65
0
            break;
66
0
        case WRITEL:
67
0
            qtest_outl(s, addr, (uint32_t)a.value);
68
0
            break;
69
0
        case READB:
70
0
            qtest_inb(s, addr);
71
0
            break;
72
0
        case READW:
73
0
            qtest_inw(s, addr);
74
0
            break;
75
0
        case READL:
76
0
            qtest_inl(s, addr);
77
0
            break;
78
0
        }
79
        /* Move to the next operation */
80
0
        Size -= sizeof(a);
81
0
        Data += sizeof(a);
82
0
    }
83
0
    flush_events(s);
84
0
}
85
86
static void i440fx_fuzz_qtest(QTestState *s,
87
                              const unsigned char *Data,
88
                              size_t Size)
89
0
{
90
0
    ioport_fuzz_qtest(s, Data, Size);
91
0
    fuzz_reset(s);
92
0
}
93
94
static void pciconfig_fuzz_qos(QTestState *s, QPCIBus *bus,
95
0
        const unsigned char *Data, size_t Size) {
96
    /*
97
     * Same as ioport_fuzz_qtest, but using QOS. devfn is incorporated into the
98
     * value written over Port IO
99
     */
100
0
    struct {
101
0
        uint8_t opcode;
102
0
        uint8_t offset;
103
0
        int devfn;
104
0
        uint32_t value;
105
0
    } a;
106
107
0
    while (Size >= sizeof(a)) {
108
0
        memcpy(&a, Data, sizeof(a));
109
0
        switch (a.opcode % ACTION_MAX) {
110
0
        case WRITEB:
111
0
            bus->config_writeb(bus, a.devfn, a.offset, (uint8_t)a.value);
112
0
            break;
113
0
        case WRITEW:
114
0
            bus->config_writew(bus, a.devfn, a.offset, (uint16_t)a.value);
115
0
            break;
116
0
        case WRITEL:
117
0
            bus->config_writel(bus, a.devfn, a.offset, (uint32_t)a.value);
118
0
            break;
119
0
        case READB:
120
0
            bus->config_readb(bus, a.devfn, a.offset);
121
0
            break;
122
0
        case READW:
123
0
            bus->config_readw(bus, a.devfn, a.offset);
124
0
            break;
125
0
        case READL:
126
0
            bus->config_readl(bus, a.devfn, a.offset);
127
0
            break;
128
0
        }
129
0
        Size -= sizeof(a);
130
0
        Data += sizeof(a);
131
0
    }
132
0
    flush_events(s);
133
0
}
134
135
static void i440fx_fuzz_qos(QTestState *s,
136
                            const unsigned char *Data,
137
                            size_t Size)
138
0
{
139
0
    static QPCIBus *bus;
140
141
0
    if (!bus) {
142
0
        bus = qpci_new_pc(s, fuzz_qos_alloc);
143
0
    }
144
145
0
    pciconfig_fuzz_qos(s, bus, Data, Size);
146
0
}
147
148
static const char *i440fx_qtest_argv = TARGET_NAME " -machine accel=qtest"
149
                                       " -m 0 -display none";
150
static GString *i440fx_argv(FuzzTarget *t)
151
0
{
152
0
    return g_string_new(i440fx_qtest_argv);
153
0
}
154
155
156
static void register_pci_fuzz_targets(void)
157
61
{
158
    /* Uses simple qtest commands and reboots to reset state */
159
61
    fuzz_add_target(&(FuzzTarget){
160
61
                .name = "i440fx-qtest-reboot-fuzz",
161
61
                .description = "Fuzz the i440fx using raw qtest commands and "
162
61
                               "rebooting after each run",
163
61
                .get_init_cmdline = i440fx_argv,
164
61
                .fuzz = i440fx_fuzz_qtest});
165
166
167
    /*
168
     * Uses libqos. Doesn't do anything to reset state. Note that if we were to
169
     * reboot after each run, we would also have to redo the qos-related
170
     * initialization (qos_init_path)
171
     */
172
61
    fuzz_add_qos_target(&(FuzzTarget){
173
61
                .name = "i440fx-qos-noreset-fuzz",
174
61
                .description = "Fuzz the i440fx using raw qtest commands and "
175
61
                               "rebooting after each run",
176
61
                .fuzz = i440fx_fuzz_qos,},
177
61
                "i440FX-pcihost",
178
61
                &(QOSGraphTestOptions){}
179
61
                );
180
61
}
181
182
fuzz_target_init(register_pci_fuzz_targets);