xref: /openbmc/qemu/tests/qtest/fuzz/qtest_wrappers.c (revision 8e49197c)
1 /*
2  * qtest function wrappers
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 
14 #include "qemu/osdep.h"
15 #include "hw/core/cpu.h"
16 #include "exec/ioport.h"
17 
18 #include "fuzz.h"
19 
20 static bool serialize = true;
21 
22 #define WRAP(RET_TYPE, NAME_AND_ARGS)\
23     RET_TYPE __wrap_##NAME_AND_ARGS;\
24     RET_TYPE __real_##NAME_AND_ARGS;
25 
WRAP(uint8_t,qtest_inb (QTestState * s,uint16_t addr))26 WRAP(uint8_t  , qtest_inb(QTestState *s, uint16_t addr))
27 WRAP(uint16_t , qtest_inw(QTestState *s, uint16_t addr))
28 WRAP(uint32_t , qtest_inl(QTestState *s, uint16_t addr))
29 WRAP(void     , qtest_outb(QTestState *s, uint16_t addr, uint8_t value))
30 WRAP(void     , qtest_outw(QTestState *s, uint16_t addr, uint16_t value))
31 WRAP(void     , qtest_outl(QTestState *s, uint16_t addr, uint32_t value))
32 WRAP(uint8_t  , qtest_readb(QTestState *s, uint64_t addr))
33 WRAP(uint16_t , qtest_readw(QTestState *s, uint64_t addr))
34 WRAP(uint32_t , qtest_readl(QTestState *s, uint64_t addr))
35 WRAP(uint64_t , qtest_readq(QTestState *s, uint64_t addr))
36 WRAP(void     , qtest_writeb(QTestState *s, uint64_t addr, uint8_t value))
37 WRAP(void     , qtest_writew(QTestState *s, uint64_t addr, uint16_t value))
38 WRAP(void     , qtest_writel(QTestState *s, uint64_t addr, uint32_t value))
39 WRAP(void     , qtest_writeq(QTestState *s, uint64_t addr, uint64_t value))
40 WRAP(void     , qtest_memread(QTestState *s, uint64_t addr,
41                               void *data, size_t size))
42 WRAP(void     , qtest_bufread(QTestState *s, uint64_t addr, void *data,
43                               size_t size))
44 WRAP(void     , qtest_memwrite(QTestState *s, uint64_t addr, const void *data,
45                                size_t size))
46 WRAP(void,      qtest_bufwrite(QTestState *s, uint64_t addr,
47                                const void *data, size_t size))
48 WRAP(void,      qtest_memset(QTestState *s, uint64_t addr,
49                              uint8_t patt, size_t size))
50 
51 
52 uint8_t __wrap_qtest_inb(QTestState *s, uint16_t addr)
53 {
54     if (!serialize) {
55         return cpu_inb(addr);
56     } else {
57         return __real_qtest_inb(s, addr);
58     }
59 }
60 
__wrap_qtest_inw(QTestState * s,uint16_t addr)61 uint16_t __wrap_qtest_inw(QTestState *s, uint16_t addr)
62 {
63     if (!serialize) {
64         return cpu_inw(addr);
65     } else {
66         return __real_qtest_inw(s, addr);
67     }
68 }
69 
__wrap_qtest_inl(QTestState * s,uint16_t addr)70 uint32_t __wrap_qtest_inl(QTestState *s, uint16_t addr)
71 {
72     if (!serialize) {
73         return cpu_inl(addr);
74     } else {
75         return __real_qtest_inl(s, addr);
76     }
77 }
78 
__wrap_qtest_outb(QTestState * s,uint16_t addr,uint8_t value)79 void __wrap_qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
80 {
81     if (!serialize) {
82         cpu_outb(addr, value);
83     } else {
84         __real_qtest_outb(s, addr, value);
85     }
86 }
87 
__wrap_qtest_outw(QTestState * s,uint16_t addr,uint16_t value)88 void __wrap_qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
89 {
90     if (!serialize) {
91         cpu_outw(addr, value);
92     } else {
93         __real_qtest_outw(s, addr, value);
94     }
95 }
96 
__wrap_qtest_outl(QTestState * s,uint16_t addr,uint32_t value)97 void __wrap_qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
98 {
99     if (!serialize) {
100         cpu_outl(addr, value);
101     } else {
102         __real_qtest_outl(s, addr, value);
103     }
104 }
105 
__wrap_qtest_readb(QTestState * s,uint64_t addr)106 uint8_t __wrap_qtest_readb(QTestState *s, uint64_t addr)
107 {
108     uint8_t value;
109     if (!serialize) {
110         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
111                             &value, 1);
112         return value;
113     } else {
114         return __real_qtest_readb(s, addr);
115     }
116 }
117 
__wrap_qtest_readw(QTestState * s,uint64_t addr)118 uint16_t __wrap_qtest_readw(QTestState *s, uint64_t addr)
119 {
120     uint16_t value;
121     if (!serialize) {
122         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
123                             &value, 2);
124         return value;
125     } else {
126         return __real_qtest_readw(s, addr);
127     }
128 }
129 
__wrap_qtest_readl(QTestState * s,uint64_t addr)130 uint32_t __wrap_qtest_readl(QTestState *s, uint64_t addr)
131 {
132     uint32_t value;
133     if (!serialize) {
134         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
135                             &value, 4);
136         return value;
137     } else {
138         return __real_qtest_readl(s, addr);
139     }
140 }
141 
__wrap_qtest_readq(QTestState * s,uint64_t addr)142 uint64_t __wrap_qtest_readq(QTestState *s, uint64_t addr)
143 {
144     uint64_t value;
145     if (!serialize) {
146         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
147                             &value, 8);
148         return value;
149     } else {
150         return __real_qtest_readq(s, addr);
151     }
152 }
153 
__wrap_qtest_writeb(QTestState * s,uint64_t addr,uint8_t value)154 void __wrap_qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
155 {
156     if (!serialize) {
157         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
158                             &value, 1);
159     } else {
160         __real_qtest_writeb(s, addr, value);
161     }
162 }
163 
__wrap_qtest_writew(QTestState * s,uint64_t addr,uint16_t value)164 void __wrap_qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
165 {
166     if (!serialize) {
167         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
168                             &value, 2);
169     } else {
170         __real_qtest_writew(s, addr, value);
171     }
172 }
173 
__wrap_qtest_writel(QTestState * s,uint64_t addr,uint32_t value)174 void __wrap_qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
175 {
176     if (!serialize) {
177         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
178                             &value, 4);
179     } else {
180         __real_qtest_writel(s, addr, value);
181     }
182 }
183 
__wrap_qtest_writeq(QTestState * s,uint64_t addr,uint64_t value)184 void __wrap_qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
185 {
186     if (!serialize) {
187         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
188                             &value, 8);
189     } else {
190         __real_qtest_writeq(s, addr, value);
191     }
192 }
193 
__wrap_qtest_memread(QTestState * s,uint64_t addr,void * data,size_t size)194 void __wrap_qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
195 {
196     if (!serialize) {
197         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
198                            size);
199     } else {
200         __real_qtest_memread(s, addr, data, size);
201     }
202 }
203 
__wrap_qtest_bufread(QTestState * s,uint64_t addr,void * data,size_t size)204 void __wrap_qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
205 {
206     if (!serialize) {
207         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
208                            size);
209     } else {
210         __real_qtest_bufread(s, addr, data, size);
211     }
212 }
213 
__wrap_qtest_memwrite(QTestState * s,uint64_t addr,const void * data,size_t size)214 void __wrap_qtest_memwrite(QTestState *s, uint64_t addr, const void *data,
215                            size_t size)
216 {
217     if (!serialize) {
218         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
219                             data, size);
220     } else {
221         __real_qtest_memwrite(s, addr, data, size);
222     }
223 }
224 
__wrap_qtest_bufwrite(QTestState * s,uint64_t addr,const void * data,size_t size)225 void __wrap_qtest_bufwrite(QTestState *s, uint64_t addr,
226                     const void *data, size_t size)
227 {
228     if (!serialize) {
229         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
230                             data, size);
231     } else {
232         __real_qtest_bufwrite(s, addr, data, size);
233     }
234 }
__wrap_qtest_memset(QTestState * s,uint64_t addr,uint8_t patt,size_t size)235 void __wrap_qtest_memset(QTestState *s, uint64_t addr,
236                          uint8_t patt, size_t size)
237 {
238     void *data;
239     if (!serialize) {
240         data = malloc(size);
241         memset(data, patt, size);
242         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
243                             data, size);
244     } else {
245         __real_qtest_memset(s, addr, patt, size);
246     }
247 }
248 
fuzz_qtest_set_serialize(bool option)249 void fuzz_qtest_set_serialize(bool option)
250 {
251     serialize = option;
252 }
253