1 /*
2  * QTest test cases for virtio balloon device
3  *
4  * Copyright (c) 2024 Gao Shiyuan <gaoshiyuan@baidu.com>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "libqtest.h"
11 #include "standard-headers/linux/virtio_balloon.h"
12 
13 /*
14  * https://gitlab.com/qemu-project/qemu/-/issues/2576
15  * Used to trigger:
16  *   virtio_address_space_lookup: Assertion `mrs.mr' failed.
17  */
18 static void oss_fuzz_71649(void)
19 {
20     QTestState *s = qtest_init("-device virtio-balloon -machine q35"
21                                " -nodefaults");
22 
23     qtest_outl(s, 0xcf8, 0x80000890);
24     qtest_outl(s, 0xcfc, 0x2);
25     qtest_outl(s, 0xcf8, 0x80000891);
26     qtest_inl(s, 0xcfc);
27     qtest_quit(s);
28 }
29 
30 static void query_stats(void)
31 {
32     QTestState *s = qtest_init("-device virtio-balloon,id=balloon"
33                                " -nodefaults");
34     QDict *ret = qtest_qmp_assert_success_ref(
35         s,
36         "{ 'execute': 'qom-get', 'arguments': "     \
37         "{ 'path': '/machine/peripheral/balloon', " \
38         "  'property': 'guest-stats' } }");
39     QDict *stats = qdict_get_qdict(ret, "stats");
40 
41     /* We expect 1 entry in the dict for each known kernel stat */
42     assert(qdict_size(stats) == VIRTIO_BALLOON_S_NR);
43 
44     qobject_unref(ret);
45     qtest_quit(s);
46 }
47 
48 int main(int argc, char **argv)
49 {
50     g_test_init(&argc, &argv, NULL);
51 
52     qtest_add_func("virtio-balloon/oss_fuzz_71649", oss_fuzz_71649);
53     qtest_add_func("virtio-balloon/query-stats", query_stats);
54 
55     return g_test_run();
56 }
57 
58