xref: /openbmc/qemu/tests/qtest/fuzz/qos_fuzz.c (revision 1f42e246)
1 /*
2  * QOS-assisted fuzzing helpers
3  *
4  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>
17  */
18 
19 #include "qemu/osdep.h"
20 #include "qemu/units.h"
21 #include "qapi/error.h"
22 #include "qemu-common.h"
23 #include "exec/memory.h"
24 #include "exec/address-spaces.h"
25 #include "sysemu/sysemu.h"
26 #include "qemu/main-loop.h"
27 
28 #include "tests/qtest/libqtest.h"
29 #include "tests/qtest/libqos/malloc.h"
30 #include "tests/qtest/libqos/qgraph.h"
31 #include "tests/qtest/libqos/qgraph_internal.h"
32 #include "tests/qtest/libqos/qos_external.h"
33 
34 #include "fuzz.h"
35 #include "qos_fuzz.h"
36 
37 #include "qapi/qapi-commands-machine.h"
38 #include "qapi/qapi-commands-qom.h"
39 
40 
41 void *fuzz_qos_obj;
42 QGuestAllocator *fuzz_qos_alloc;
43 
44 static const char *fuzz_target_name;
45 static char **fuzz_path_vec;
46 
47 static void qos_set_machines_devices_available(void)
48 {
49     MachineInfoList *mach_info;
50     ObjectTypeInfoList *type_info;
51 
52     mach_info = qmp_query_machines(&error_abort);
53     machines_apply_to_node(mach_info);
54     qapi_free_MachineInfoList(mach_info);
55 
56     type_info = qmp_qom_list_types(true, "device", true, true,
57                                    &error_abort);
58     types_apply_to_node(type_info);
59     qapi_free_ObjectTypeInfoList(type_info);
60 }
61 
62 static char **current_path;
63 
64 void *qos_allocate_objects(QTestState *qts, QGuestAllocator **p_alloc)
65 {
66     return allocate_objects(qts, current_path + 1, p_alloc);
67 }
68 
69 static GString *qos_build_main_args(void)
70 {
71     char **path = fuzz_path_vec;
72     QOSGraphNode *test_node;
73     GString *cmd_line = g_string_new(path[0]);
74     void *test_arg;
75 
76     if (!path) {
77         fprintf(stderr, "QOS Path not found\n");
78         abort();
79     }
80 
81     /* Before test */
82     current_path = path;
83     test_node = qos_graph_get_node(path[(g_strv_length(path) - 1)]);
84     test_arg = test_node->u.test.arg;
85     if (test_node->u.test.before) {
86         test_arg = test_node->u.test.before(cmd_line, test_arg);
87     }
88     /* Prepend the arguments that we need */
89     g_string_prepend(cmd_line,
90             TARGET_NAME " -display none -machine accel=qtest -m 64 ");
91     return cmd_line;
92 }
93 
94 /*
95  * This function is largely a copy of qos-test.c:walk_path. Since walk_path
96  * is itself a callback, its a little annoying to add another argument/layer of
97  * indirection
98  */
99 static void walk_path(QOSGraphNode *orig_path, int len)
100 {
101     QOSGraphNode *path;
102     QOSGraphEdge *edge;
103 
104     /*
105      * etype set to QEDGE_CONSUMED_BY so that machine can add to the command
106      * line
107      */
108     QOSEdgeType etype = QEDGE_CONSUMED_BY;
109 
110     /* twice QOS_PATH_MAX_ELEMENT_SIZE since each edge can have its arg */
111     char **path_vec = g_new0(char *, (QOS_PATH_MAX_ELEMENT_SIZE * 2));
112     int path_vec_size = 0;
113 
114     char *after_cmd, *before_cmd, *after_device;
115     GString *after_device_str = g_string_new("");
116     char *node_name = orig_path->name, *path_str;
117 
118     GString *cmd_line = g_string_new("");
119     GString *cmd_line2 = g_string_new("");
120 
121     path = qos_graph_get_node(node_name); /* root */
122     node_name = qos_graph_edge_get_dest(path->path_edge); /* machine name */
123 
124     path_vec[path_vec_size++] = node_name;
125     path_vec[path_vec_size++] = qos_get_machine_type(node_name);
126 
127     for (;;) {
128         path = qos_graph_get_node(node_name);
129         if (!path->path_edge) {
130             break;
131         }
132 
133         node_name = qos_graph_edge_get_dest(path->path_edge);
134 
135         /* append node command line + previous edge command line */
136         if (path->command_line && etype == QEDGE_CONSUMED_BY) {
137             g_string_append(cmd_line, path->command_line);
138             g_string_append(cmd_line, after_device_str->str);
139             g_string_truncate(after_device_str, 0);
140         }
141 
142         path_vec[path_vec_size++] = qos_graph_edge_get_name(path->path_edge);
143         /* detect if edge has command line args */
144         after_cmd = qos_graph_edge_get_after_cmd_line(path->path_edge);
145         after_device = qos_graph_edge_get_extra_device_opts(path->path_edge);
146         before_cmd = qos_graph_edge_get_before_cmd_line(path->path_edge);
147         edge = qos_graph_get_edge(path->name, node_name);
148         etype = qos_graph_edge_get_type(edge);
149 
150         if (before_cmd) {
151             g_string_append(cmd_line, before_cmd);
152         }
153         if (after_cmd) {
154             g_string_append(cmd_line2, after_cmd);
155         }
156         if (after_device) {
157             g_string_append(after_device_str, after_device);
158         }
159     }
160 
161     path_vec[path_vec_size++] = NULL;
162     g_string_append(cmd_line, after_device_str->str);
163     g_string_free(after_device_str, true);
164 
165     g_string_append(cmd_line, cmd_line2->str);
166     g_string_free(cmd_line2, true);
167 
168     /*
169      * here position 0 has <arch>/<machine>, position 1 has <machine>.
170      * The path must not have the <arch>, qtest_add_data_func adds it.
171      */
172     path_str = g_strjoinv("/", path_vec + 1);
173 
174     /* Check that this is the test we care about: */
175     char *test_name = strrchr(path_str, '/') + 1;
176     if (strcmp(test_name, fuzz_target_name) == 0) {
177         /*
178          * put arch/machine in position 1 so run_one_test can do its work
179          * and add the command line at position 0.
180          */
181         path_vec[1] = path_vec[0];
182         path_vec[0] = g_string_free(cmd_line, false);
183 
184         fuzz_path_vec = path_vec;
185     } else {
186         g_free(path_vec);
187     }
188 
189     g_free(path_str);
190 }
191 
192 static GString *qos_get_cmdline(FuzzTarget *t)
193 {
194     /*
195      * Set a global variable that we use to identify the qos_path for our
196      * fuzz_target
197      */
198     fuzz_target_name = t->name;
199     qos_set_machines_devices_available();
200     qos_graph_foreach_test_path(walk_path);
201     return qos_build_main_args();
202 }
203 
204 void fuzz_add_qos_target(
205         FuzzTarget *fuzz_opts,
206         const char *interface,
207         QOSGraphTestOptions *opts
208         )
209 {
210     qos_add_test(fuzz_opts->name, interface, NULL, opts);
211     fuzz_opts->get_init_cmdline = qos_get_cmdline;
212     fuzz_add_target(fuzz_opts);
213 }
214 
215 void qos_init_path(QTestState *s)
216 {
217     fuzz_qos_obj = qos_allocate_objects(s , &fuzz_qos_alloc);
218 }
219