184604595SDaniel P. Berrangé /*
284604595SDaniel P. Berrangé  * QTest test cases for virtio balloon device
384604595SDaniel P. Berrangé  *
484604595SDaniel P. Berrangé  * Copyright (c) 2024 Gao Shiyuan <gaoshiyuan@baidu.com>
584604595SDaniel P. Berrangé  *
684604595SDaniel P. Berrangé  * SPDX-License-Identifier: GPL-2.0-or-later
784604595SDaniel P. Berrangé  */
884604595SDaniel P. Berrangé 
984604595SDaniel P. Berrangé #include "qemu/osdep.h"
1084604595SDaniel P. Berrangé #include "libqtest.h"
11*d65c890aSDaniel P. Berrangé #include "standard-headers/linux/virtio_balloon.h"
1284604595SDaniel P. Berrangé 
1384604595SDaniel P. Berrangé /*
1484604595SDaniel P. Berrangé  * https://gitlab.com/qemu-project/qemu/-/issues/2576
1584604595SDaniel P. Berrangé  * Used to trigger:
1684604595SDaniel P. Berrangé  *   virtio_address_space_lookup: Assertion `mrs.mr' failed.
1784604595SDaniel P. Berrangé  */
oss_fuzz_71649(void)1884604595SDaniel P. Berrangé static void oss_fuzz_71649(void)
1984604595SDaniel P. Berrangé {
2084604595SDaniel P. Berrangé     QTestState *s = qtest_init("-device virtio-balloon -machine q35"
2184604595SDaniel P. Berrangé                                " -nodefaults");
2284604595SDaniel P. Berrangé 
2384604595SDaniel P. Berrangé     qtest_outl(s, 0xcf8, 0x80000890);
2484604595SDaniel P. Berrangé     qtest_outl(s, 0xcfc, 0x2);
2584604595SDaniel P. Berrangé     qtest_outl(s, 0xcf8, 0x80000891);
2684604595SDaniel P. Berrangé     qtest_inl(s, 0xcfc);
2784604595SDaniel P. Berrangé     qtest_quit(s);
2884604595SDaniel P. Berrangé }
2984604595SDaniel P. Berrangé 
query_stats(void)30*d65c890aSDaniel P. Berrangé static void query_stats(void)
31*d65c890aSDaniel P. Berrangé {
32*d65c890aSDaniel P. Berrangé     QTestState *s = qtest_init("-device virtio-balloon,id=balloon"
33*d65c890aSDaniel P. Berrangé                                " -nodefaults");
34*d65c890aSDaniel P. Berrangé     QDict *ret = qtest_qmp_assert_success_ref(
35*d65c890aSDaniel P. Berrangé         s,
36*d65c890aSDaniel P. Berrangé         "{ 'execute': 'qom-get', 'arguments': "     \
37*d65c890aSDaniel P. Berrangé         "{ 'path': '/machine/peripheral/balloon', " \
38*d65c890aSDaniel P. Berrangé         "  'property': 'guest-stats' } }");
39*d65c890aSDaniel P. Berrangé     QDict *stats = qdict_get_qdict(ret, "stats");
40*d65c890aSDaniel P. Berrangé 
41*d65c890aSDaniel P. Berrangé     /* We expect 1 entry in the dict for each known kernel stat */
42*d65c890aSDaniel P. Berrangé     assert(qdict_size(stats) == VIRTIO_BALLOON_S_NR);
43*d65c890aSDaniel P. Berrangé 
44*d65c890aSDaniel P. Berrangé     qobject_unref(ret);
45*d65c890aSDaniel P. Berrangé     qtest_quit(s);
46*d65c890aSDaniel P. Berrangé }
47*d65c890aSDaniel P. Berrangé 
main(int argc,char ** argv)4884604595SDaniel P. Berrangé int main(int argc, char **argv)
4984604595SDaniel P. Berrangé {
5084604595SDaniel P. Berrangé     g_test_init(&argc, &argv, NULL);
5184604595SDaniel P. Berrangé 
5284604595SDaniel P. Berrangé     qtest_add_func("virtio-balloon/oss_fuzz_71649", oss_fuzz_71649);
53*d65c890aSDaniel P. Berrangé     qtest_add_func("virtio-balloon/query-stats", query_stats);
5484604595SDaniel P. Berrangé 
5584604595SDaniel P. Berrangé     return g_test_run();
5684604595SDaniel P. Berrangé }
5784604595SDaniel P. Berrangé 
58