xref: /qemu/tests/unit/test-vmstate.c (revision f853fa0714061989b14d7f85667a353b69e2d9b6)
12668b4bfSEduardo Habkost /*
22668b4bfSEduardo Habkost  *  Test code for VMState
32668b4bfSEduardo Habkost  *
42668b4bfSEduardo Habkost  *  Copyright (c) 2013 Red Hat Inc.
52668b4bfSEduardo Habkost  *
62668b4bfSEduardo Habkost  * Permission is hereby granted, free of charge, to any person obtaining a copy
72668b4bfSEduardo Habkost  * of this software and associated documentation files (the "Software"), to deal
82668b4bfSEduardo Habkost  * in the Software without restriction, including without limitation the rights
92668b4bfSEduardo Habkost  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
102668b4bfSEduardo Habkost  * copies of the Software, and to permit persons to whom the Software is
112668b4bfSEduardo Habkost  * furnished to do so, subject to the following conditions:
122668b4bfSEduardo Habkost  *
132668b4bfSEduardo Habkost  * The above copyright notice and this permission notice shall be included in
142668b4bfSEduardo Habkost  * all copies or substantial portions of the Software.
152668b4bfSEduardo Habkost  *
162668b4bfSEduardo Habkost  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
172668b4bfSEduardo Habkost  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
182668b4bfSEduardo Habkost  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
192668b4bfSEduardo Habkost  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
202668b4bfSEduardo Habkost  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
212668b4bfSEduardo Habkost  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
222668b4bfSEduardo Habkost  * THE SOFTWARE.
232668b4bfSEduardo Habkost  */
242668b4bfSEduardo Habkost 
25681c28a3SPeter Maydell #include "qemu/osdep.h"
262668b4bfSEduardo Habkost 
272668b4bfSEduardo Habkost #include "migration/vmstate.h"
2808a0aee1SJuan Quintela #include "migration/qemu-file-types.h"
2908a0aee1SJuan Quintela #include "../migration/qemu-file.h"
30c3d2e2e7SJuan Quintela #include "../migration/savevm.h"
310b8fa32fSMarkus Armbruster #include "qemu/module.h"
328925839fSDaniel P. Berrange #include "io/channel-file.h"
332668b4bfSEduardo Habkost 
34748bfb4eSStefan Weil static int temp_fd;
352668b4bfSEduardo Habkost 
369935bacaSDr. David Alan Gilbert 
372668b4bfSEduardo Habkost /* Duplicate temp_fd and seek to the beginning of the file */
open_test_file(bool write)38c6f6646cSJuan Quintela static QEMUFile *open_test_file(bool write)
392668b4bfSEduardo Habkost {
401c861885SPeter Maydell     int fd;
418925839fSDaniel P. Berrange     QIOChannel *ioc;
424ae3c0e2SMarc-André Lureau     QEMUFile *f;
434ae3c0e2SMarc-André Lureau 
441c861885SPeter Maydell     fd = dup(temp_fd);
451c861885SPeter Maydell     g_assert(fd >= 0);
462668b4bfSEduardo Habkost     lseek(fd, 0, SEEK_SET);
47c6f6646cSJuan Quintela     if (write) {
482668b4bfSEduardo Habkost         g_assert_cmpint(ftruncate(fd, 0), ==, 0);
492668b4bfSEduardo Habkost     }
508925839fSDaniel P. Berrange     ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
518925839fSDaniel P. Berrange     if (write) {
5277ef2dc1SDaniel P. Berrangé         f = qemu_file_new_output(ioc);
538925839fSDaniel P. Berrange     } else {
5477ef2dc1SDaniel P. Berrangé         f = qemu_file_new_input(ioc);
558925839fSDaniel P. Berrange     }
564ae3c0e2SMarc-André Lureau     object_unref(OBJECT(ioc));
574ae3c0e2SMarc-André Lureau     return f;
582668b4bfSEduardo Habkost }
592668b4bfSEduardo Habkost 
604ea7df4eSJuan Quintela #define SUCCESS(val) \
614ea7df4eSJuan Quintela     g_assert_cmpint((val), ==, 0)
624ea7df4eSJuan Quintela 
634ea7df4eSJuan Quintela #define FAILURE(val) \
644ea7df4eSJuan Quintela     g_assert_cmpint((val), !=, 0)
654ea7df4eSJuan Quintela 
save_vmstate(const VMStateDescription * desc,void * obj)664ea7df4eSJuan Quintela static void save_vmstate(const VMStateDescription *desc, void *obj)
674ea7df4eSJuan Quintela {
684ea7df4eSJuan Quintela     QEMUFile *f = open_test_file(true);
694ea7df4eSJuan Quintela 
704ea7df4eSJuan Quintela     /* Save file with vmstate */
712f168d07SDr. David Alan Gilbert     int ret = vmstate_save_state(f, desc, obj, NULL);
722f168d07SDr. David Alan Gilbert     g_assert(!ret);
734ea7df4eSJuan Quintela     qemu_put_byte(f, QEMU_VM_EOF);
744ea7df4eSJuan Quintela     g_assert(!qemu_file_get_error(f));
754ea7df4eSJuan Quintela     qemu_fclose(f);
764ea7df4eSJuan Quintela }
774ea7df4eSJuan Quintela 
save_buffer(const uint8_t * buf,size_t buf_size)786d57b4c0SHalil Pasic static void save_buffer(const uint8_t *buf, size_t buf_size)
796d57b4c0SHalil Pasic {
806d57b4c0SHalil Pasic     QEMUFile *fsave = open_test_file(true);
816d57b4c0SHalil Pasic     qemu_put_buffer(fsave, buf, buf_size);
826d57b4c0SHalil Pasic     qemu_fclose(fsave);
836d57b4c0SHalil Pasic }
846d57b4c0SHalil Pasic 
compare_vmstate(const uint8_t * wire,size_t size)855c379d90SDr. David Alan Gilbert static void compare_vmstate(const uint8_t *wire, size_t size)
864ea7df4eSJuan Quintela {
874ea7df4eSJuan Quintela     QEMUFile *f = open_test_file(false);
88972d325aSPhilippe Mathieu-Daudé     g_autofree uint8_t *result = g_malloc(size);
894ea7df4eSJuan Quintela 
904ea7df4eSJuan Quintela     /* read back as binary */
914ea7df4eSJuan Quintela 
92972d325aSPhilippe Mathieu-Daudé     g_assert_cmpint(qemu_get_buffer(f, result, size), ==, size);
934ea7df4eSJuan Quintela     g_assert(!qemu_file_get_error(f));
944ea7df4eSJuan Quintela 
954ea7df4eSJuan Quintela     /* Compare that what is on the file is the same that what we
964ea7df4eSJuan Quintela        expected to be there */
97972d325aSPhilippe Mathieu-Daudé     SUCCESS(memcmp(result, wire, size));
984ea7df4eSJuan Quintela 
994ea7df4eSJuan Quintela     /* Must reach EOF */
1004ea7df4eSJuan Quintela     qemu_get_byte(f);
1014ea7df4eSJuan Quintela     g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
1024ea7df4eSJuan Quintela 
1034ea7df4eSJuan Quintela     qemu_fclose(f);
1044ea7df4eSJuan Quintela }
1054ea7df4eSJuan Quintela 
load_vmstate_one(const VMStateDescription * desc,void * obj,int version,const uint8_t * wire,size_t size)1064ea7df4eSJuan Quintela static int load_vmstate_one(const VMStateDescription *desc, void *obj,
1075c379d90SDr. David Alan Gilbert                             int version, const uint8_t *wire, size_t size)
1084ea7df4eSJuan Quintela {
1094ea7df4eSJuan Quintela     QEMUFile *f;
1104ea7df4eSJuan Quintela     int ret;
1114ea7df4eSJuan Quintela 
1124ea7df4eSJuan Quintela     f = open_test_file(true);
1134ea7df4eSJuan Quintela     qemu_put_buffer(f, wire, size);
1144ea7df4eSJuan Quintela     qemu_fclose(f);
1154ea7df4eSJuan Quintela 
1164ea7df4eSJuan Quintela     f = open_test_file(false);
1174ea7df4eSJuan Quintela     ret = vmstate_load_state(f, desc, obj, version);
1184ea7df4eSJuan Quintela     if (ret) {
1194ea7df4eSJuan Quintela         g_assert(qemu_file_get_error(f));
1204ea7df4eSJuan Quintela     } else{
1214ea7df4eSJuan Quintela         g_assert(!qemu_file_get_error(f));
1224ea7df4eSJuan Quintela     }
1234ea7df4eSJuan Quintela     qemu_fclose(f);
1244ea7df4eSJuan Quintela     return ret;
1254ea7df4eSJuan Quintela }
1264ea7df4eSJuan Quintela 
1274ea7df4eSJuan Quintela 
load_vmstate(const VMStateDescription * desc,void * obj,void * obj_clone,void (* obj_copy)(void *,void *),int version,const uint8_t * wire,size_t size)1284ea7df4eSJuan Quintela static int load_vmstate(const VMStateDescription *desc,
1294ea7df4eSJuan Quintela                         void *obj, void *obj_clone,
1304ea7df4eSJuan Quintela                         void (*obj_copy)(void *, void*),
1315c379d90SDr. David Alan Gilbert                         int version, const uint8_t *wire, size_t size)
1324ea7df4eSJuan Quintela {
1334ea7df4eSJuan Quintela     /* We test with zero size */
1344ea7df4eSJuan Quintela     obj_copy(obj_clone, obj);
1354ea7df4eSJuan Quintela     FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
1364ea7df4eSJuan Quintela 
1374ea7df4eSJuan Quintela     /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
1384ea7df4eSJuan Quintela      * able to test in the middle */
1394ea7df4eSJuan Quintela 
1404ea7df4eSJuan Quintela     if (size > 3) {
1414ea7df4eSJuan Quintela 
1424ea7df4eSJuan Quintela         /* We test with size - 2. We can't test size - 1 due to EOF tricks */
1434ea7df4eSJuan Quintela         obj_copy(obj, obj_clone);
1444ea7df4eSJuan Quintela         FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
1454ea7df4eSJuan Quintela 
1464ea7df4eSJuan Quintela         /* Test with size/2, first half of real state */
1474ea7df4eSJuan Quintela         obj_copy(obj, obj_clone);
1484ea7df4eSJuan Quintela         FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
1494ea7df4eSJuan Quintela 
1504ea7df4eSJuan Quintela         /* Test with size/2, second half of real state */
1514ea7df4eSJuan Quintela         obj_copy(obj, obj_clone);
1524ea7df4eSJuan Quintela         FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
1534ea7df4eSJuan Quintela 
1544ea7df4eSJuan Quintela     }
1554ea7df4eSJuan Quintela     obj_copy(obj, obj_clone);
1564ea7df4eSJuan Quintela     return load_vmstate_one(desc, obj, version, wire, size);
1574ea7df4eSJuan Quintela }
1584ea7df4eSJuan Quintela 
1594ea7df4eSJuan Quintela /* Test struct that we are going to use for our tests */
1604ea7df4eSJuan Quintela 
1614ea7df4eSJuan Quintela typedef struct TestSimple {
1624ea7df4eSJuan Quintela     bool     b_1,   b_2;
1634ea7df4eSJuan Quintela     uint8_t  u8_1;
1644ea7df4eSJuan Quintela     uint16_t u16_1;
1654ea7df4eSJuan Quintela     uint32_t u32_1;
1664ea7df4eSJuan Quintela     uint64_t u64_1;
1674ea7df4eSJuan Quintela     int8_t   i8_1,  i8_2;
1684ea7df4eSJuan Quintela     int16_t  i16_1, i16_2;
1694ea7df4eSJuan Quintela     int32_t  i32_1, i32_2;
1704ea7df4eSJuan Quintela     int64_t  i64_1, i64_2;
1714ea7df4eSJuan Quintela } TestSimple;
1724ea7df4eSJuan Quintela 
1734ea7df4eSJuan Quintela /* Object instantiation, we are going to use it in more than one test */
1744ea7df4eSJuan Quintela 
1754ea7df4eSJuan Quintela TestSimple obj_simple = {
1764ea7df4eSJuan Quintela     .b_1 = true,
1774ea7df4eSJuan Quintela     .b_2 = false,
1784ea7df4eSJuan Quintela     .u8_1 = 130,
1794ea7df4eSJuan Quintela     .u16_1 = 512,
1804ea7df4eSJuan Quintela     .u32_1 = 70000,
1814ea7df4eSJuan Quintela     .u64_1 = 12121212,
1824ea7df4eSJuan Quintela     .i8_1 = 65,
1834ea7df4eSJuan Quintela     .i8_2 = -65,
1844ea7df4eSJuan Quintela     .i16_1 = 512,
1854ea7df4eSJuan Quintela     .i16_2 = -512,
1864ea7df4eSJuan Quintela     .i32_1 = 70000,
1874ea7df4eSJuan Quintela     .i32_2 = -70000,
1884ea7df4eSJuan Quintela     .i64_1 = 12121212,
1894ea7df4eSJuan Quintela     .i64_2 = -12121212,
1904ea7df4eSJuan Quintela };
1914ea7df4eSJuan Quintela 
1924ea7df4eSJuan Quintela /* Description of the values.  If you add a primitive type
1934ea7df4eSJuan Quintela    you are expected to add a test here */
1944ea7df4eSJuan Quintela 
1954ea7df4eSJuan Quintela static const VMStateDescription vmstate_simple_primitive = {
1964ea7df4eSJuan Quintela     .name = "simple/primitive",
1974ea7df4eSJuan Quintela     .version_id = 1,
1984ea7df4eSJuan Quintela     .minimum_version_id = 1,
199*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
2004ea7df4eSJuan Quintela         VMSTATE_BOOL(b_1, TestSimple),
2014ea7df4eSJuan Quintela         VMSTATE_BOOL(b_2, TestSimple),
2024ea7df4eSJuan Quintela         VMSTATE_UINT8(u8_1, TestSimple),
2034ea7df4eSJuan Quintela         VMSTATE_UINT16(u16_1, TestSimple),
2044ea7df4eSJuan Quintela         VMSTATE_UINT32(u32_1, TestSimple),
2054ea7df4eSJuan Quintela         VMSTATE_UINT64(u64_1, TestSimple),
2064ea7df4eSJuan Quintela         VMSTATE_INT8(i8_1, TestSimple),
2074ea7df4eSJuan Quintela         VMSTATE_INT8(i8_2, TestSimple),
2084ea7df4eSJuan Quintela         VMSTATE_INT16(i16_1, TestSimple),
2094ea7df4eSJuan Quintela         VMSTATE_INT16(i16_2, TestSimple),
2104ea7df4eSJuan Quintela         VMSTATE_INT32(i32_1, TestSimple),
2114ea7df4eSJuan Quintela         VMSTATE_INT32(i32_2, TestSimple),
2124ea7df4eSJuan Quintela         VMSTATE_INT64(i64_1, TestSimple),
2134ea7df4eSJuan Quintela         VMSTATE_INT64(i64_2, TestSimple),
2144ea7df4eSJuan Quintela         VMSTATE_END_OF_LIST()
2154ea7df4eSJuan Quintela     }
2164ea7df4eSJuan Quintela };
2174ea7df4eSJuan Quintela 
2184ea7df4eSJuan Quintela /* It describes what goes through the wire.  Our tests are basically:
2194ea7df4eSJuan Quintela 
2204ea7df4eSJuan Quintela    * save test
2214ea7df4eSJuan Quintela      - save a struct a vmstate to a file
2224ea7df4eSJuan Quintela      - read that file back (binary read, no vmstate)
2234ea7df4eSJuan Quintela      - compare it with what we expect to be on the wire
2244ea7df4eSJuan Quintela    * load test
2254ea7df4eSJuan Quintela      - save to the file what we expect to be on the wire
2264ea7df4eSJuan Quintela      - read struct back with vmstate in a different
2274ea7df4eSJuan Quintela      - compare back with the original struct
2284ea7df4eSJuan Quintela */
2294ea7df4eSJuan Quintela 
2304ea7df4eSJuan Quintela uint8_t wire_simple_primitive[] = {
2314ea7df4eSJuan Quintela     /* b_1 */   0x01,
2324ea7df4eSJuan Quintela     /* b_2 */   0x00,
2334ea7df4eSJuan Quintela     /* u8_1 */  0x82,
2344ea7df4eSJuan Quintela     /* u16_1 */ 0x02, 0x00,
2354ea7df4eSJuan Quintela     /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
2364ea7df4eSJuan Quintela     /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
2374ea7df4eSJuan Quintela     /* i8_1 */  0x41,
2384ea7df4eSJuan Quintela     /* i8_2 */  0xbf,
2394ea7df4eSJuan Quintela     /* i16_1 */ 0x02, 0x00,
2404ea7df4eSJuan Quintela     /* i16_2 */ 0xfe, 0x0,
2414ea7df4eSJuan Quintela     /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
2424ea7df4eSJuan Quintela     /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
2434ea7df4eSJuan Quintela     /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
2444ea7df4eSJuan Quintela     /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
2454ea7df4eSJuan Quintela     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
2464ea7df4eSJuan Quintela };
2474ea7df4eSJuan Quintela 
obj_simple_copy(void * target,void * source)2484ea7df4eSJuan Quintela static void obj_simple_copy(void *target, void *source)
2494ea7df4eSJuan Quintela {
2504ea7df4eSJuan Quintela     memcpy(target, source, sizeof(TestSimple));
2514ea7df4eSJuan Quintela }
2524ea7df4eSJuan Quintela 
test_simple_primitive(void)2534ea7df4eSJuan Quintela static void test_simple_primitive(void)
2544ea7df4eSJuan Quintela {
2554ea7df4eSJuan Quintela     TestSimple obj, obj_clone;
2564ea7df4eSJuan Quintela 
2574ea7df4eSJuan Quintela     memset(&obj, 0, sizeof(obj));
2584ea7df4eSJuan Quintela     save_vmstate(&vmstate_simple_primitive, &obj_simple);
2594ea7df4eSJuan Quintela 
2604ea7df4eSJuan Quintela     compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
2614ea7df4eSJuan Quintela 
2624ea7df4eSJuan Quintela     SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
2634ea7df4eSJuan Quintela                          obj_simple_copy, 1, wire_simple_primitive,
2644ea7df4eSJuan Quintela                          sizeof(wire_simple_primitive)));
2654ea7df4eSJuan Quintela 
2664ea7df4eSJuan Quintela #define FIELD_EQUAL(name)   g_assert_cmpint(obj.name, ==, obj_simple.name)
2674ea7df4eSJuan Quintela 
2684ea7df4eSJuan Quintela     FIELD_EQUAL(b_1);
2694ea7df4eSJuan Quintela     FIELD_EQUAL(b_2);
2704ea7df4eSJuan Quintela     FIELD_EQUAL(u8_1);
2714ea7df4eSJuan Quintela     FIELD_EQUAL(u16_1);
2724ea7df4eSJuan Quintela     FIELD_EQUAL(u32_1);
2734ea7df4eSJuan Quintela     FIELD_EQUAL(u64_1);
2744ea7df4eSJuan Quintela     FIELD_EQUAL(i8_1);
2754ea7df4eSJuan Quintela     FIELD_EQUAL(i8_2);
2764ea7df4eSJuan Quintela     FIELD_EQUAL(i16_1);
2774ea7df4eSJuan Quintela     FIELD_EQUAL(i16_2);
2784ea7df4eSJuan Quintela     FIELD_EQUAL(i32_1);
2794ea7df4eSJuan Quintela     FIELD_EQUAL(i32_2);
2804ea7df4eSJuan Quintela     FIELD_EQUAL(i64_1);
2814ea7df4eSJuan Quintela     FIELD_EQUAL(i64_2);
2824ea7df4eSJuan Quintela }
2834ea7df4eSJuan Quintela 
284b95d6588SMarc-André Lureau typedef struct TestSimpleArray {
285b95d6588SMarc-André Lureau     uint16_t u16_1[3];
286b95d6588SMarc-André Lureau } TestSimpleArray;
287b95d6588SMarc-André Lureau 
288b95d6588SMarc-André Lureau /* Object instantiation, we are going to use it in more than one test */
289b95d6588SMarc-André Lureau 
290b95d6588SMarc-André Lureau TestSimpleArray obj_simple_arr = {
291b95d6588SMarc-André Lureau     .u16_1 = { 0x42, 0x43, 0x44 },
292b95d6588SMarc-André Lureau };
293b95d6588SMarc-André Lureau 
294b95d6588SMarc-André Lureau /* Description of the values.  If you add a primitive type
295b95d6588SMarc-André Lureau    you are expected to add a test here */
296b95d6588SMarc-André Lureau 
297b95d6588SMarc-André Lureau static const VMStateDescription vmstate_simple_arr = {
298b95d6588SMarc-André Lureau     .name = "simple/array",
299b95d6588SMarc-André Lureau     .version_id = 1,
300b95d6588SMarc-André Lureau     .minimum_version_id = 1,
301*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
302b95d6588SMarc-André Lureau         VMSTATE_UINT16_ARRAY(u16_1, TestSimpleArray, 3),
303b95d6588SMarc-André Lureau         VMSTATE_END_OF_LIST()
304b95d6588SMarc-André Lureau     }
305b95d6588SMarc-André Lureau };
306b95d6588SMarc-André Lureau 
307b95d6588SMarc-André Lureau uint8_t wire_simple_arr[] = {
308b95d6588SMarc-André Lureau     /* u16_1 */ 0x00, 0x42,
309b95d6588SMarc-André Lureau     /* u16_1 */ 0x00, 0x43,
310b95d6588SMarc-André Lureau     /* u16_1 */ 0x00, 0x44,
311b95d6588SMarc-André Lureau     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
312b95d6588SMarc-André Lureau };
313b95d6588SMarc-André Lureau 
obj_simple_arr_copy(void * target,void * source)314b95d6588SMarc-André Lureau static void obj_simple_arr_copy(void *target, void *source)
315b95d6588SMarc-André Lureau {
316b95d6588SMarc-André Lureau     memcpy(target, source, sizeof(TestSimpleArray));
317b95d6588SMarc-André Lureau }
318b95d6588SMarc-André Lureau 
test_simple_array(void)319b95d6588SMarc-André Lureau static void test_simple_array(void)
320b95d6588SMarc-André Lureau {
321b95d6588SMarc-André Lureau     TestSimpleArray obj, obj_clone;
322b95d6588SMarc-André Lureau 
323b95d6588SMarc-André Lureau     memset(&obj, 0, sizeof(obj));
324b95d6588SMarc-André Lureau     save_vmstate(&vmstate_simple_arr, &obj_simple_arr);
325b95d6588SMarc-André Lureau 
326b95d6588SMarc-André Lureau     compare_vmstate(wire_simple_arr, sizeof(wire_simple_arr));
327b95d6588SMarc-André Lureau 
328b95d6588SMarc-André Lureau     SUCCESS(load_vmstate(&vmstate_simple_arr, &obj, &obj_clone,
329b95d6588SMarc-André Lureau                          obj_simple_arr_copy, 1, wire_simple_arr,
330b95d6588SMarc-André Lureau                          sizeof(wire_simple_arr)));
331b95d6588SMarc-André Lureau }
332b95d6588SMarc-André Lureau 
3334ea7df4eSJuan Quintela typedef struct TestStruct {
3342668b4bfSEduardo Habkost     uint32_t a, b, c, e;
3352668b4bfSEduardo Habkost     uint64_t d, f;
3362668b4bfSEduardo Habkost     bool skip_c_e;
3372668b4bfSEduardo Habkost } TestStruct;
3382668b4bfSEduardo Habkost 
3392668b4bfSEduardo Habkost static const VMStateDescription vmstate_versioned = {
3404ea7df4eSJuan Quintela     .name = "test/versioned",
3412668b4bfSEduardo Habkost     .version_id = 2,
3422668b4bfSEduardo Habkost     .minimum_version_id = 1,
343*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
3442668b4bfSEduardo Habkost         VMSTATE_UINT32(a, TestStruct),
3452668b4bfSEduardo Habkost         VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
3462668b4bfSEduardo Habkost                                              * we catch bugs more easily.
3472668b4bfSEduardo Habkost                                              */
3482668b4bfSEduardo Habkost         VMSTATE_UINT32(c, TestStruct),
3492668b4bfSEduardo Habkost         VMSTATE_UINT64(d, TestStruct),
3502668b4bfSEduardo Habkost         VMSTATE_UINT32_V(e, TestStruct, 2),
3512668b4bfSEduardo Habkost         VMSTATE_UINT64_V(f, TestStruct, 2),
3522668b4bfSEduardo Habkost         VMSTATE_END_OF_LIST()
3532668b4bfSEduardo Habkost     }
3542668b4bfSEduardo Habkost };
3552668b4bfSEduardo Habkost 
test_load_v1(void)3562668b4bfSEduardo Habkost static void test_load_v1(void)
3572668b4bfSEduardo Habkost {
3582668b4bfSEduardo Habkost     uint8_t buf[] = {
3592668b4bfSEduardo Habkost         0, 0, 0, 10,             /* a */
3602668b4bfSEduardo Habkost         0, 0, 0, 30,             /* c */
3612668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 40, /* d */
3622668b4bfSEduardo Habkost         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
3632668b4bfSEduardo Habkost     };
3646d57b4c0SHalil Pasic     save_buffer(buf, sizeof(buf));
3652668b4bfSEduardo Habkost 
366c6f6646cSJuan Quintela     QEMUFile *loading = open_test_file(false);
3672668b4bfSEduardo Habkost     TestStruct obj = { .b = 200, .e = 500, .f = 600 };
3682668b4bfSEduardo Habkost     vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
3692668b4bfSEduardo Habkost     g_assert(!qemu_file_get_error(loading));
3702668b4bfSEduardo Habkost     g_assert_cmpint(obj.a, ==, 10);
3712668b4bfSEduardo Habkost     g_assert_cmpint(obj.b, ==, 200);
3722668b4bfSEduardo Habkost     g_assert_cmpint(obj.c, ==, 30);
3732668b4bfSEduardo Habkost     g_assert_cmpint(obj.d, ==, 40);
3742668b4bfSEduardo Habkost     g_assert_cmpint(obj.e, ==, 500);
3752668b4bfSEduardo Habkost     g_assert_cmpint(obj.f, ==, 600);
3762668b4bfSEduardo Habkost     qemu_fclose(loading);
3772668b4bfSEduardo Habkost }
3782668b4bfSEduardo Habkost 
test_load_v2(void)3792668b4bfSEduardo Habkost static void test_load_v2(void)
3802668b4bfSEduardo Habkost {
3812668b4bfSEduardo Habkost     uint8_t buf[] = {
3822668b4bfSEduardo Habkost         0, 0, 0, 10,             /* a */
3832668b4bfSEduardo Habkost         0, 0, 0, 20,             /* b */
3842668b4bfSEduardo Habkost         0, 0, 0, 30,             /* c */
3852668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 40, /* d */
3862668b4bfSEduardo Habkost         0, 0, 0, 50,             /* e */
3872668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 60, /* f */
3882668b4bfSEduardo Habkost         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
3892668b4bfSEduardo Habkost     };
3906d57b4c0SHalil Pasic     save_buffer(buf, sizeof(buf));
3912668b4bfSEduardo Habkost 
392c6f6646cSJuan Quintela     QEMUFile *loading = open_test_file(false);
3932668b4bfSEduardo Habkost     TestStruct obj;
3942668b4bfSEduardo Habkost     vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
3952668b4bfSEduardo Habkost     g_assert_cmpint(obj.a, ==, 10);
3962668b4bfSEduardo Habkost     g_assert_cmpint(obj.b, ==, 20);
3972668b4bfSEduardo Habkost     g_assert_cmpint(obj.c, ==, 30);
3982668b4bfSEduardo Habkost     g_assert_cmpint(obj.d, ==, 40);
3992668b4bfSEduardo Habkost     g_assert_cmpint(obj.e, ==, 50);
4002668b4bfSEduardo Habkost     g_assert_cmpint(obj.f, ==, 60);
4012668b4bfSEduardo Habkost     qemu_fclose(loading);
4022668b4bfSEduardo Habkost }
4032668b4bfSEduardo Habkost 
test_skip(void * opaque,int version_id)4042668b4bfSEduardo Habkost static bool test_skip(void *opaque, int version_id)
4052668b4bfSEduardo Habkost {
4062668b4bfSEduardo Habkost     TestStruct *t = (TestStruct *)opaque;
4072668b4bfSEduardo Habkost     return !t->skip_c_e;
4082668b4bfSEduardo Habkost }
4092668b4bfSEduardo Habkost 
4102668b4bfSEduardo Habkost static const VMStateDescription vmstate_skipping = {
4114ea7df4eSJuan Quintela     .name = "test/skip",
4122668b4bfSEduardo Habkost     .version_id = 2,
4132668b4bfSEduardo Habkost     .minimum_version_id = 1,
414*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
4152668b4bfSEduardo Habkost         VMSTATE_UINT32(a, TestStruct),
4162668b4bfSEduardo Habkost         VMSTATE_UINT32(b, TestStruct),
4172668b4bfSEduardo Habkost         VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
4182668b4bfSEduardo Habkost         VMSTATE_UINT64(d, TestStruct),
4192668b4bfSEduardo Habkost         VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
4202668b4bfSEduardo Habkost         VMSTATE_UINT64_V(f, TestStruct, 2),
4212668b4bfSEduardo Habkost         VMSTATE_END_OF_LIST()
4222668b4bfSEduardo Habkost     }
4232668b4bfSEduardo Habkost };
4242668b4bfSEduardo Habkost 
4252668b4bfSEduardo Habkost 
test_save_noskip(void)4262668b4bfSEduardo Habkost static void test_save_noskip(void)
4272668b4bfSEduardo Habkost {
428a8ec4437SDaniel P. Berrange     QEMUFile *fsave = open_test_file(true);
4292668b4bfSEduardo Habkost     TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
4302668b4bfSEduardo Habkost                        .skip_c_e = false };
4312f168d07SDr. David Alan Gilbert     int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
4322f168d07SDr. David Alan Gilbert     g_assert(!ret);
4332668b4bfSEduardo Habkost     g_assert(!qemu_file_get_error(fsave));
4342668b4bfSEduardo Habkost 
4352668b4bfSEduardo Habkost     uint8_t expected[] = {
4362668b4bfSEduardo Habkost         0, 0, 0, 1,             /* a */
4372668b4bfSEduardo Habkost         0, 0, 0, 2,             /* b */
4382668b4bfSEduardo Habkost         0, 0, 0, 3,             /* c */
4392668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 4, /* d */
4402668b4bfSEduardo Habkost         0, 0, 0, 5,             /* e */
4412668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 6, /* f */
4422668b4bfSEduardo Habkost     };
443a8ec4437SDaniel P. Berrange 
4449935bacaSDr. David Alan Gilbert     qemu_fclose(fsave);
445a8ec4437SDaniel P. Berrange     compare_vmstate(expected, sizeof(expected));
4462668b4bfSEduardo Habkost }
4472668b4bfSEduardo Habkost 
test_save_skip(void)4482668b4bfSEduardo Habkost static void test_save_skip(void)
4492668b4bfSEduardo Habkost {
450a8ec4437SDaniel P. Berrange     QEMUFile *fsave = open_test_file(true);
4512668b4bfSEduardo Habkost     TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
4522668b4bfSEduardo Habkost                        .skip_c_e = true };
4532f168d07SDr. David Alan Gilbert     int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
4542f168d07SDr. David Alan Gilbert     g_assert(!ret);
4552668b4bfSEduardo Habkost     g_assert(!qemu_file_get_error(fsave));
4562668b4bfSEduardo Habkost 
4572668b4bfSEduardo Habkost     uint8_t expected[] = {
4582668b4bfSEduardo Habkost         0, 0, 0, 1,             /* a */
4592668b4bfSEduardo Habkost         0, 0, 0, 2,             /* b */
4602668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 4, /* d */
4612668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 6, /* f */
4622668b4bfSEduardo Habkost     };
4632668b4bfSEduardo Habkost 
4649935bacaSDr. David Alan Gilbert     qemu_fclose(fsave);
465a8ec4437SDaniel P. Berrange     compare_vmstate(expected, sizeof(expected));
4662668b4bfSEduardo Habkost }
4672668b4bfSEduardo Habkost 
test_load_noskip(void)4682668b4bfSEduardo Habkost static void test_load_noskip(void)
4692668b4bfSEduardo Habkost {
4702668b4bfSEduardo Habkost     uint8_t buf[] = {
4712668b4bfSEduardo Habkost         0, 0, 0, 10,             /* a */
4722668b4bfSEduardo Habkost         0, 0, 0, 20,             /* b */
4732668b4bfSEduardo Habkost         0, 0, 0, 30,             /* c */
4742668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 40, /* d */
4752668b4bfSEduardo Habkost         0, 0, 0, 50,             /* e */
4762668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 60, /* f */
4772668b4bfSEduardo Habkost         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
4782668b4bfSEduardo Habkost     };
4796d57b4c0SHalil Pasic     save_buffer(buf, sizeof(buf));
4802668b4bfSEduardo Habkost 
481a8ec4437SDaniel P. Berrange     QEMUFile *loading = open_test_file(false);
4822668b4bfSEduardo Habkost     TestStruct obj = { .skip_c_e = false };
4832668b4bfSEduardo Habkost     vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
4842668b4bfSEduardo Habkost     g_assert(!qemu_file_get_error(loading));
4852668b4bfSEduardo Habkost     g_assert_cmpint(obj.a, ==, 10);
4862668b4bfSEduardo Habkost     g_assert_cmpint(obj.b, ==, 20);
4872668b4bfSEduardo Habkost     g_assert_cmpint(obj.c, ==, 30);
4882668b4bfSEduardo Habkost     g_assert_cmpint(obj.d, ==, 40);
4892668b4bfSEduardo Habkost     g_assert_cmpint(obj.e, ==, 50);
4902668b4bfSEduardo Habkost     g_assert_cmpint(obj.f, ==, 60);
4912668b4bfSEduardo Habkost     qemu_fclose(loading);
4922668b4bfSEduardo Habkost }
4932668b4bfSEduardo Habkost 
test_load_skip(void)4942668b4bfSEduardo Habkost static void test_load_skip(void)
4952668b4bfSEduardo Habkost {
4962668b4bfSEduardo Habkost     uint8_t buf[] = {
4972668b4bfSEduardo Habkost         0, 0, 0, 10,             /* a */
4982668b4bfSEduardo Habkost         0, 0, 0, 20,             /* b */
4992668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 40, /* d */
5002668b4bfSEduardo Habkost         0, 0, 0, 0, 0, 0, 0, 60, /* f */
5012668b4bfSEduardo Habkost         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
5022668b4bfSEduardo Habkost     };
5036d57b4c0SHalil Pasic     save_buffer(buf, sizeof(buf));
5042668b4bfSEduardo Habkost 
505a8ec4437SDaniel P. Berrange     QEMUFile *loading = open_test_file(false);
5062668b4bfSEduardo Habkost     TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
5072668b4bfSEduardo Habkost     vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
5082668b4bfSEduardo Habkost     g_assert(!qemu_file_get_error(loading));
5092668b4bfSEduardo Habkost     g_assert_cmpint(obj.a, ==, 10);
5102668b4bfSEduardo Habkost     g_assert_cmpint(obj.b, ==, 20);
5112668b4bfSEduardo Habkost     g_assert_cmpint(obj.c, ==, 300);
5122668b4bfSEduardo Habkost     g_assert_cmpint(obj.d, ==, 40);
5132668b4bfSEduardo Habkost     g_assert_cmpint(obj.e, ==, 500);
5142668b4bfSEduardo Habkost     g_assert_cmpint(obj.f, ==, 60);
5152668b4bfSEduardo Habkost     qemu_fclose(loading);
5162668b4bfSEduardo Habkost }
5172668b4bfSEduardo Habkost 
5188cc49f03SHalil Pasic typedef struct {
5198cc49f03SHalil Pasic     int32_t i;
5208cc49f03SHalil Pasic } TestStructTriv;
5218cc49f03SHalil Pasic 
5228cc49f03SHalil Pasic const VMStateDescription vmsd_tst = {
5238cc49f03SHalil Pasic     .name = "test/tst",
5248cc49f03SHalil Pasic     .version_id = 1,
5258cc49f03SHalil Pasic     .minimum_version_id = 1,
526*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
5278cc49f03SHalil Pasic         VMSTATE_INT32(i, TestStructTriv),
5288cc49f03SHalil Pasic         VMSTATE_END_OF_LIST()
5298cc49f03SHalil Pasic     }
5308cc49f03SHalil Pasic };
5318cc49f03SHalil Pasic 
532cc958831SHalil Pasic /* test array migration */
533cc958831SHalil Pasic 
5348cc49f03SHalil Pasic #define AR_SIZE 4
5358cc49f03SHalil Pasic 
5368cc49f03SHalil Pasic typedef struct {
5378cc49f03SHalil Pasic     TestStructTriv *ar[AR_SIZE];
5388cc49f03SHalil Pasic } TestArrayOfPtrToStuct;
5398cc49f03SHalil Pasic 
5408cc49f03SHalil Pasic const VMStateDescription vmsd_arps = {
5418cc49f03SHalil Pasic     .name = "test/arps",
5428cc49f03SHalil Pasic     .version_id = 1,
5438cc49f03SHalil Pasic     .minimum_version_id = 1,
544*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
5458cc49f03SHalil Pasic         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
5468cc49f03SHalil Pasic                 AR_SIZE, 0, vmsd_tst, TestStructTriv),
5478cc49f03SHalil Pasic         VMSTATE_END_OF_LIST()
5488cc49f03SHalil Pasic     }
5498cc49f03SHalil Pasic };
550cc958831SHalil Pasic 
551cc958831SHalil Pasic static uint8_t wire_arr_ptr_no0[] = {
5528cc49f03SHalil Pasic     0x00, 0x00, 0x00, 0x00,
5538cc49f03SHalil Pasic     0x00, 0x00, 0x00, 0x01,
5548cc49f03SHalil Pasic     0x00, 0x00, 0x00, 0x02,
5558cc49f03SHalil Pasic     0x00, 0x00, 0x00, 0x03,
5568cc49f03SHalil Pasic     QEMU_VM_EOF
5578cc49f03SHalil Pasic };
5588cc49f03SHalil Pasic 
test_arr_ptr_str_no0_save(void)559cc958831SHalil Pasic static void test_arr_ptr_str_no0_save(void)
560cc958831SHalil Pasic {
561cc958831SHalil Pasic     TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
562cc958831SHalil Pasic     TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
563cc958831SHalil Pasic 
5648cc49f03SHalil Pasic     save_vmstate(&vmsd_arps, &sample);
565cc958831SHalil Pasic     compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
5668cc49f03SHalil Pasic }
5678cc49f03SHalil Pasic 
test_arr_ptr_str_no0_load(void)5688cc49f03SHalil Pasic static void test_arr_ptr_str_no0_load(void)
5698cc49f03SHalil Pasic {
5708cc49f03SHalil Pasic     TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
5718cc49f03SHalil Pasic     TestStructTriv ar[AR_SIZE] = {};
5728cc49f03SHalil Pasic     TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
5738cc49f03SHalil Pasic     int idx;
574cc958831SHalil Pasic 
575cc958831SHalil Pasic     save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
576cc958831SHalil Pasic     SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
577cc958831SHalil Pasic                           wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)));
578cc958831SHalil Pasic     for (idx = 0; idx < AR_SIZE; ++idx) {
579cc958831SHalil Pasic         /* compare the target array ar with the ground truth array ar_gt */
580cc958831SHalil Pasic         g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
581cc958831SHalil Pasic     }
582cc958831SHalil Pasic }
583cc958831SHalil Pasic 
584cc958831SHalil Pasic static uint8_t wire_arr_ptr_0[] = {
5858cc49f03SHalil Pasic     0x00, 0x00, 0x00, 0x00,
586cc958831SHalil Pasic     VMS_NULLPTR_MARKER,
5878cc49f03SHalil Pasic     0x00, 0x00, 0x00, 0x02,
5888cc49f03SHalil Pasic     0x00, 0x00, 0x00, 0x03,
5898cc49f03SHalil Pasic     QEMU_VM_EOF
5908cc49f03SHalil Pasic };
5918cc49f03SHalil Pasic 
test_arr_ptr_str_0_save(void)592cc958831SHalil Pasic static void test_arr_ptr_str_0_save(void)
593cc958831SHalil Pasic {
594cc958831SHalil Pasic     TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
595cc958831SHalil Pasic     TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
596cc958831SHalil Pasic 
597cc958831SHalil Pasic     save_vmstate(&vmsd_arps, &sample);
598cc958831SHalil Pasic     compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
599cc958831SHalil Pasic }
600cc958831SHalil Pasic 
test_arr_ptr_str_0_load(void)601cc958831SHalil Pasic static void test_arr_ptr_str_0_load(void)
602cc958831SHalil Pasic {
603cc958831SHalil Pasic     TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 0}, {.i = 2}, {.i = 3} };
604cc958831SHalil Pasic     TestStructTriv ar[AR_SIZE] = {};
605cc958831SHalil Pasic     TestArrayOfPtrToStuct obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
606cc958831SHalil Pasic     int idx;
607cc958831SHalil Pasic 
608cc958831SHalil Pasic     save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
6098cc49f03SHalil Pasic     SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
610cc958831SHalil Pasic                           wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
6118cc49f03SHalil Pasic     for (idx = 0; idx < AR_SIZE; ++idx) {
6128cc49f03SHalil Pasic         /* compare the target array ar with the ground truth array ar_gt */
6138cc49f03SHalil Pasic         g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
6148cc49f03SHalil Pasic     }
615cc958831SHalil Pasic     for (idx = 0; idx < AR_SIZE; ++idx) {
616cc958831SHalil Pasic         if (idx == 1) {
617cc958831SHalil Pasic             g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0);
618cc958831SHalil Pasic         } else {
619cc958831SHalil Pasic             g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0);
620cc958831SHalil Pasic         }
621cc958831SHalil Pasic     }
6228cc49f03SHalil Pasic }
6238cc49f03SHalil Pasic 
62443333099SHalil Pasic typedef struct TestArrayOfPtrToInt {
62543333099SHalil Pasic     int32_t *ar[AR_SIZE];
62643333099SHalil Pasic } TestArrayOfPtrToInt;
62743333099SHalil Pasic 
62843333099SHalil Pasic const VMStateDescription vmsd_arpp = {
62943333099SHalil Pasic     .name = "test/arps",
63043333099SHalil Pasic     .version_id = 1,
63143333099SHalil Pasic     .minimum_version_id = 1,
632*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
63343333099SHalil Pasic         VMSTATE_ARRAY_OF_POINTER(ar, TestArrayOfPtrToInt,
63443333099SHalil Pasic                 AR_SIZE, 0, vmstate_info_int32, int32_t*),
63543333099SHalil Pasic         VMSTATE_END_OF_LIST()
63643333099SHalil Pasic     }
63743333099SHalil Pasic };
63843333099SHalil Pasic 
test_arr_ptr_prim_0_save(void)63943333099SHalil Pasic static void test_arr_ptr_prim_0_save(void)
64043333099SHalil Pasic {
64143333099SHalil Pasic     int32_t ar[AR_SIZE] = {0 , 1, 2, 3};
64243333099SHalil Pasic     TestArrayOfPtrToInt  sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
64343333099SHalil Pasic 
64443333099SHalil Pasic     save_vmstate(&vmsd_arpp, &sample);
64543333099SHalil Pasic     compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
64643333099SHalil Pasic }
64743333099SHalil Pasic 
test_arr_ptr_prim_0_load(void)64843333099SHalil Pasic static void test_arr_ptr_prim_0_load(void)
64943333099SHalil Pasic {
65043333099SHalil Pasic     int32_t ar_gt[AR_SIZE] = {0, 1, 2, 3};
65143333099SHalil Pasic     int32_t ar[AR_SIZE] = {3 , 42, 1, 0};
65243333099SHalil Pasic     TestArrayOfPtrToInt obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
65343333099SHalil Pasic     int idx;
65443333099SHalil Pasic 
65543333099SHalil Pasic     save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
65643333099SHalil Pasic     SUCCESS(load_vmstate_one(&vmsd_arpp, &obj, 1,
65743333099SHalil Pasic                           wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
65843333099SHalil Pasic     for (idx = 0; idx < AR_SIZE; ++idx) {
65943333099SHalil Pasic         /* compare the target array ar with the ground truth array ar_gt */
66043333099SHalil Pasic         if (idx == 1) {
66143333099SHalil Pasic             g_assert_cmpint(42, ==, ar[idx]);
66243333099SHalil Pasic         } else {
66343333099SHalil Pasic             g_assert_cmpint(ar_gt[idx], ==, ar[idx]);
66443333099SHalil Pasic         }
66543333099SHalil Pasic     }
66643333099SHalil Pasic }
66743333099SHalil Pasic 
6687e99f22cSJianjun Duan /* test QTAILQ migration */
6697e99f22cSJianjun Duan typedef struct TestQtailqElement TestQtailqElement;
6707e99f22cSJianjun Duan 
6717e99f22cSJianjun Duan struct TestQtailqElement {
6727e99f22cSJianjun Duan     bool     b;
6737e99f22cSJianjun Duan     uint8_t  u8;
6747e99f22cSJianjun Duan     QTAILQ_ENTRY(TestQtailqElement) next;
6757e99f22cSJianjun Duan };
6767e99f22cSJianjun Duan 
6777e99f22cSJianjun Duan typedef struct TestQtailq {
6787e99f22cSJianjun Duan     int16_t  i16;
679eae3eb3eSPaolo Bonzini     QTAILQ_HEAD(, TestQtailqElement) q;
6807e99f22cSJianjun Duan     int32_t  i32;
6817e99f22cSJianjun Duan } TestQtailq;
6827e99f22cSJianjun Duan 
6837e99f22cSJianjun Duan static const VMStateDescription vmstate_q_element = {
6847e99f22cSJianjun Duan     .name = "test/queue-element",
6857e99f22cSJianjun Duan     .version_id = 1,
6867e99f22cSJianjun Duan     .minimum_version_id = 1,
687*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
6887e99f22cSJianjun Duan         VMSTATE_BOOL(b, TestQtailqElement),
6897e99f22cSJianjun Duan         VMSTATE_UINT8(u8, TestQtailqElement),
6907e99f22cSJianjun Duan         VMSTATE_END_OF_LIST()
6917e99f22cSJianjun Duan     },
6927e99f22cSJianjun Duan };
6937e99f22cSJianjun Duan 
6947e99f22cSJianjun Duan static const VMStateDescription vmstate_q = {
6957e99f22cSJianjun Duan     .name = "test/queue",
6967e99f22cSJianjun Duan     .version_id = 1,
6977e99f22cSJianjun Duan     .minimum_version_id = 1,
698*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
6997e99f22cSJianjun Duan         VMSTATE_INT16(i16, TestQtailq),
7007e99f22cSJianjun Duan         VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
7017e99f22cSJianjun Duan                          next),
7027e99f22cSJianjun Duan         VMSTATE_INT32(i32, TestQtailq),
7037e99f22cSJianjun Duan         VMSTATE_END_OF_LIST()
7047e99f22cSJianjun Duan     }
7057e99f22cSJianjun Duan };
7067e99f22cSJianjun Duan 
7077e99f22cSJianjun Duan uint8_t wire_q[] = {
7087e99f22cSJianjun Duan     /* i16 */                     0xfe, 0x0,
7097e99f22cSJianjun Duan     /* start of element 0 of q */ 0x01,
7107e99f22cSJianjun Duan     /* .b  */                     0x01,
7117e99f22cSJianjun Duan     /* .u8 */                     0x82,
7127e99f22cSJianjun Duan     /* start of element 1 of q */ 0x01,
7137e99f22cSJianjun Duan     /* b */                       0x00,
7147e99f22cSJianjun Duan     /* u8 */                      0x41,
7157e99f22cSJianjun Duan     /* end of q */                0x00,
7167e99f22cSJianjun Duan     /* i32 */                     0x00, 0x01, 0x11, 0x70,
7177e99f22cSJianjun Duan     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
7187e99f22cSJianjun Duan };
7197e99f22cSJianjun Duan 
test_save_q(void)7207e99f22cSJianjun Duan static void test_save_q(void)
7217e99f22cSJianjun Duan {
7227e99f22cSJianjun Duan     TestQtailq obj_q = {
7237e99f22cSJianjun Duan         .i16 = -512,
7247e99f22cSJianjun Duan         .i32 = 70000,
7257e99f22cSJianjun Duan     };
7267e99f22cSJianjun Duan 
7277e99f22cSJianjun Duan     TestQtailqElement obj_qe1 = {
7287e99f22cSJianjun Duan         .b = true,
7297e99f22cSJianjun Duan         .u8 = 130,
7307e99f22cSJianjun Duan     };
7317e99f22cSJianjun Duan 
7327e99f22cSJianjun Duan     TestQtailqElement obj_qe2 = {
7337e99f22cSJianjun Duan         .b = false,
7347e99f22cSJianjun Duan         .u8 = 65,
7357e99f22cSJianjun Duan     };
7367e99f22cSJianjun Duan 
7377e99f22cSJianjun Duan     QTAILQ_INIT(&obj_q.q);
7387e99f22cSJianjun Duan     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
7397e99f22cSJianjun Duan     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
7407e99f22cSJianjun Duan 
7417e99f22cSJianjun Duan     save_vmstate(&vmstate_q, &obj_q);
7427e99f22cSJianjun Duan     compare_vmstate(wire_q, sizeof(wire_q));
7437e99f22cSJianjun Duan }
7447e99f22cSJianjun Duan 
test_load_q(void)7457e99f22cSJianjun Duan static void test_load_q(void)
7467e99f22cSJianjun Duan {
7477e99f22cSJianjun Duan     TestQtailq obj_q = {
7487e99f22cSJianjun Duan         .i16 = -512,
7497e99f22cSJianjun Duan         .i32 = 70000,
7507e99f22cSJianjun Duan     };
7517e99f22cSJianjun Duan 
7527e99f22cSJianjun Duan     TestQtailqElement obj_qe1 = {
7537e99f22cSJianjun Duan         .b = true,
7547e99f22cSJianjun Duan         .u8 = 130,
7557e99f22cSJianjun Duan     };
7567e99f22cSJianjun Duan 
7577e99f22cSJianjun Duan     TestQtailqElement obj_qe2 = {
7587e99f22cSJianjun Duan         .b = false,
7597e99f22cSJianjun Duan         .u8 = 65,
7607e99f22cSJianjun Duan     };
7617e99f22cSJianjun Duan 
7627e99f22cSJianjun Duan     QTAILQ_INIT(&obj_q.q);
7637e99f22cSJianjun Duan     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
7647e99f22cSJianjun Duan     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
7657e99f22cSJianjun Duan 
7667e99f22cSJianjun Duan     QEMUFile *fsave = open_test_file(true);
7677e99f22cSJianjun Duan 
7687e99f22cSJianjun Duan     qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
7697e99f22cSJianjun Duan     g_assert(!qemu_file_get_error(fsave));
7707e99f22cSJianjun Duan     qemu_fclose(fsave);
7717e99f22cSJianjun Duan 
7727e99f22cSJianjun Duan     QEMUFile *fload = open_test_file(false);
7737e99f22cSJianjun Duan     TestQtailq tgt;
7747e99f22cSJianjun Duan 
7757e99f22cSJianjun Duan     QTAILQ_INIT(&tgt.q);
7767e99f22cSJianjun Duan     vmstate_load_state(fload, &vmstate_q, &tgt, 1);
7777e99f22cSJianjun Duan     char eof = qemu_get_byte(fload);
7787e99f22cSJianjun Duan     g_assert(!qemu_file_get_error(fload));
7797e99f22cSJianjun Duan     g_assert_cmpint(tgt.i16, ==, obj_q.i16);
7807e99f22cSJianjun Duan     g_assert_cmpint(tgt.i32, ==, obj_q.i32);
7817e99f22cSJianjun Duan     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
7827e99f22cSJianjun Duan 
7837e99f22cSJianjun Duan     TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q);
784eae3eb3eSPaolo Bonzini     TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q);
7857e99f22cSJianjun Duan     TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q);
786eae3eb3eSPaolo Bonzini     TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q);
7877e99f22cSJianjun Duan 
7887e99f22cSJianjun Duan     while (1) {
7897e99f22cSJianjun Duan         g_assert_cmpint(qele_to->b, ==, qele_from->b);
7907e99f22cSJianjun Duan         g_assert_cmpint(qele_to->u8, ==, qele_from->u8);
7917e99f22cSJianjun Duan         if ((qele_from == qlast_from) || (qele_to == qlast_to)) {
7927e99f22cSJianjun Duan             break;
7937e99f22cSJianjun Duan         }
7947e99f22cSJianjun Duan         qele_from = QTAILQ_NEXT(qele_from, next);
7957e99f22cSJianjun Duan         qele_to = QTAILQ_NEXT(qele_to, next);
7967e99f22cSJianjun Duan     }
7977e99f22cSJianjun Duan 
7987e99f22cSJianjun Duan     g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
7997e99f22cSJianjun Duan     g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
8007e99f22cSJianjun Duan 
8017e99f22cSJianjun Duan     /* clean up */
8027e99f22cSJianjun Duan     TestQtailqElement *qele;
8037e99f22cSJianjun Duan     while (!QTAILQ_EMPTY(&tgt.q)) {
804eae3eb3eSPaolo Bonzini         qele = QTAILQ_LAST(&tgt.q);
8057e99f22cSJianjun Duan         QTAILQ_REMOVE(&tgt.q, qele, next);
8067e99f22cSJianjun Duan         free(qele);
8077e99f22cSJianjun Duan         qele = NULL;
8087e99f22cSJianjun Duan     }
8097e99f22cSJianjun Duan     qemu_fclose(fload);
8107e99f22cSJianjun Duan }
8117e99f22cSJianjun Duan 
8129a85e4b8SEric Auger /* interval (key) */
8139a85e4b8SEric Auger typedef struct TestGTreeInterval {
8149a85e4b8SEric Auger     uint64_t low;
8159a85e4b8SEric Auger     uint64_t high;
8169a85e4b8SEric Auger } TestGTreeInterval;
8179a85e4b8SEric Auger 
8189a85e4b8SEric Auger #define VMSTATE_INTERVAL                               \
8199a85e4b8SEric Auger {                                                      \
8209a85e4b8SEric Auger     .name = "interval",                                \
8219a85e4b8SEric Auger     .version_id = 1,                                   \
8229a85e4b8SEric Auger     .minimum_version_id = 1,                           \
823*57c73988SRichard Henderson     .fields = (const VMStateField[]) {                 \
8249a85e4b8SEric Auger         VMSTATE_UINT64(low, TestGTreeInterval),        \
8259a85e4b8SEric Auger         VMSTATE_UINT64(high, TestGTreeInterval),       \
8269a85e4b8SEric Auger         VMSTATE_END_OF_LIST()                          \
8279a85e4b8SEric Auger     }                                                  \
8289a85e4b8SEric Auger }
8299a85e4b8SEric Auger 
8309a85e4b8SEric Auger /* mapping (value) */
8319a85e4b8SEric Auger typedef struct TestGTreeMapping {
8329a85e4b8SEric Auger     uint64_t phys_addr;
8339a85e4b8SEric Auger     uint32_t flags;
8349a85e4b8SEric Auger } TestGTreeMapping;
8359a85e4b8SEric Auger 
8369a85e4b8SEric Auger #define VMSTATE_MAPPING                               \
8379a85e4b8SEric Auger {                                                     \
8389a85e4b8SEric Auger     .name = "mapping",                                \
8399a85e4b8SEric Auger     .version_id = 1,                                  \
8409a85e4b8SEric Auger     .minimum_version_id = 1,                          \
841*57c73988SRichard Henderson     .fields = (const VMStateField[]) {                \
8429a85e4b8SEric Auger         VMSTATE_UINT64(phys_addr, TestGTreeMapping),  \
8439a85e4b8SEric Auger         VMSTATE_UINT32(flags, TestGTreeMapping),      \
8449a85e4b8SEric Auger         VMSTATE_END_OF_LIST()                         \
8459a85e4b8SEric Auger     },                                                \
8469a85e4b8SEric Auger }
8479a85e4b8SEric Auger 
8489a85e4b8SEric Auger static const VMStateDescription vmstate_interval_mapping[2] = {
8499a85e4b8SEric Auger     VMSTATE_MAPPING,   /* value */
8509a85e4b8SEric Auger     VMSTATE_INTERVAL   /* key   */
8519a85e4b8SEric Auger };
8529a85e4b8SEric Auger 
8539a85e4b8SEric Auger typedef struct TestGTreeDomain {
8549a85e4b8SEric Auger     int32_t  id;
8559a85e4b8SEric Auger     GTree    *mappings;
8569a85e4b8SEric Auger } TestGTreeDomain;
8579a85e4b8SEric Auger 
8589a85e4b8SEric Auger typedef struct TestGTreeIOMMU {
8599a85e4b8SEric Auger     int32_t  id;
8609a85e4b8SEric Auger     GTree    *domains;
8619a85e4b8SEric Auger } TestGTreeIOMMU;
8629a85e4b8SEric Auger 
8639a85e4b8SEric Auger /* Interval comparison function */
interval_cmp(gconstpointer a,gconstpointer b,gpointer user_data)8649a85e4b8SEric Auger static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
8659a85e4b8SEric Auger {
8669a85e4b8SEric Auger     TestGTreeInterval *inta = (TestGTreeInterval *)a;
8679a85e4b8SEric Auger     TestGTreeInterval *intb = (TestGTreeInterval *)b;
8689a85e4b8SEric Auger 
8699a85e4b8SEric Auger     if (inta->high < intb->low) {
8709a85e4b8SEric Auger         return -1;
8719a85e4b8SEric Auger     } else if (intb->high < inta->low) {
8729a85e4b8SEric Auger         return 1;
8739a85e4b8SEric Auger     } else {
8749a85e4b8SEric Auger         return 0;
8759a85e4b8SEric Auger     }
8769a85e4b8SEric Auger }
8779a85e4b8SEric Auger 
8789a85e4b8SEric Auger /* ID comparison function */
int_cmp(gconstpointer a,gconstpointer b,gpointer user_data)8799a85e4b8SEric Auger static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
8809a85e4b8SEric Auger {
88185c93c57SYonggang Luo     guint ua = GPOINTER_TO_UINT(a);
88285c93c57SYonggang Luo     guint ub = GPOINTER_TO_UINT(b);
8839a85e4b8SEric Auger     return (ua > ub) - (ua < ub);
8849a85e4b8SEric Auger }
8859a85e4b8SEric Auger 
destroy_domain(gpointer data)8869a85e4b8SEric Auger static void destroy_domain(gpointer data)
8879a85e4b8SEric Auger {
8889a85e4b8SEric Auger     TestGTreeDomain *domain = (TestGTreeDomain *)data;
8899a85e4b8SEric Auger 
8909a85e4b8SEric Auger     g_tree_destroy(domain->mappings);
8919a85e4b8SEric Auger     g_free(domain);
8929a85e4b8SEric Auger }
8939a85e4b8SEric Auger 
domain_preload(void * opaque)8949a85e4b8SEric Auger static int domain_preload(void *opaque)
8959a85e4b8SEric Auger {
8969a85e4b8SEric Auger     TestGTreeDomain *domain = opaque;
8979a85e4b8SEric Auger 
8989a85e4b8SEric Auger     domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
8999a85e4b8SEric Auger                                        NULL, g_free, g_free);
9009a85e4b8SEric Auger     return 0;
9019a85e4b8SEric Auger }
9029a85e4b8SEric Auger 
iommu_preload(void * opaque)9039a85e4b8SEric Auger static int iommu_preload(void *opaque)
9049a85e4b8SEric Auger {
9059a85e4b8SEric Auger     TestGTreeIOMMU *iommu = opaque;
9069a85e4b8SEric Auger 
9079a85e4b8SEric Auger     iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp,
9089a85e4b8SEric Auger                                      NULL, NULL, destroy_domain);
9099a85e4b8SEric Auger     return 0;
9109a85e4b8SEric Auger }
9119a85e4b8SEric Auger 
9129a85e4b8SEric Auger static const VMStateDescription vmstate_domain = {
9139a85e4b8SEric Auger     .name = "domain",
9149a85e4b8SEric Auger     .version_id = 1,
9159a85e4b8SEric Auger     .minimum_version_id = 1,
9169a85e4b8SEric Auger     .pre_load = domain_preload,
917*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
9189a85e4b8SEric Auger         VMSTATE_INT32(id, TestGTreeDomain),
9199a85e4b8SEric Auger         VMSTATE_GTREE_V(mappings, TestGTreeDomain, 1,
9209a85e4b8SEric Auger                         vmstate_interval_mapping,
9219a85e4b8SEric Auger                         TestGTreeInterval, TestGTreeMapping),
9229a85e4b8SEric Auger         VMSTATE_END_OF_LIST()
9239a85e4b8SEric Auger     }
9249a85e4b8SEric Auger };
9259a85e4b8SEric Auger 
9264746dbf8SEric Auger /* test QLIST Migration */
9274746dbf8SEric Auger 
9284746dbf8SEric Auger typedef struct TestQListElement {
9294746dbf8SEric Auger     uint32_t  id;
9304746dbf8SEric Auger     QLIST_ENTRY(TestQListElement) next;
9314746dbf8SEric Auger } TestQListElement;
9324746dbf8SEric Auger 
9334746dbf8SEric Auger typedef struct TestQListContainer {
9344746dbf8SEric Auger     uint32_t  id;
9354746dbf8SEric Auger     QLIST_HEAD(, TestQListElement) list;
9364746dbf8SEric Auger } TestQListContainer;
9374746dbf8SEric Auger 
9384746dbf8SEric Auger static const VMStateDescription vmstate_qlist_element = {
9394746dbf8SEric Auger     .name = "test/queue list",
9404746dbf8SEric Auger     .version_id = 1,
9414746dbf8SEric Auger     .minimum_version_id = 1,
942*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
9434746dbf8SEric Auger         VMSTATE_UINT32(id, TestQListElement),
9444746dbf8SEric Auger         VMSTATE_END_OF_LIST()
9454746dbf8SEric Auger     }
9464746dbf8SEric Auger };
9474746dbf8SEric Auger 
9489a85e4b8SEric Auger static const VMStateDescription vmstate_iommu = {
9499a85e4b8SEric Auger     .name = "iommu",
9509a85e4b8SEric Auger     .version_id = 1,
9519a85e4b8SEric Auger     .minimum_version_id = 1,
9529a85e4b8SEric Auger     .pre_load = iommu_preload,
953*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
9549a85e4b8SEric Auger         VMSTATE_INT32(id, TestGTreeIOMMU),
9559a85e4b8SEric Auger         VMSTATE_GTREE_DIRECT_KEY_V(domains, TestGTreeIOMMU, 1,
9569a85e4b8SEric Auger                                    &vmstate_domain, TestGTreeDomain),
9579a85e4b8SEric Auger         VMSTATE_END_OF_LIST()
9589a85e4b8SEric Auger     }
9599a85e4b8SEric Auger };
9609a85e4b8SEric Auger 
9614746dbf8SEric Auger static const VMStateDescription vmstate_container = {
9624746dbf8SEric Auger     .name = "test/container/qlist",
9634746dbf8SEric Auger     .version_id = 1,
9644746dbf8SEric Auger     .minimum_version_id = 1,
965*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
9664746dbf8SEric Auger         VMSTATE_UINT32(id, TestQListContainer),
9674746dbf8SEric Auger         VMSTATE_QLIST_V(list, TestQListContainer, 1, vmstate_qlist_element,
9684746dbf8SEric Auger                         TestQListElement, next),
9694746dbf8SEric Auger         VMSTATE_END_OF_LIST()
9704746dbf8SEric Auger     }
9714746dbf8SEric Auger };
9724746dbf8SEric Auger 
9739a85e4b8SEric Auger uint8_t first_domain_dump[] = {
9749a85e4b8SEric Auger     /* id */
9759a85e4b8SEric Auger     0x00, 0x0, 0x0, 0x6,
9769a85e4b8SEric Auger     0x00, 0x0, 0x0, 0x2, /* 2 mappings */
9779a85e4b8SEric Auger     0x1, /* start of a */
9789a85e4b8SEric Auger     /* a */
9799a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
9809a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
9819a85e4b8SEric Auger     /* map_a */
9829a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
9839a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x01,
9849a85e4b8SEric Auger     0x1, /* start of b */
9859a85e4b8SEric Auger     /* b */
9869a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
9879a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
9889a85e4b8SEric Auger     /* map_b */
9899a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
9909a85e4b8SEric Auger     0x00, 0x00, 0x00, 0x02,
9919a85e4b8SEric Auger     0x0, /* end of gtree */
9929a85e4b8SEric Auger     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
9939a85e4b8SEric Auger };
9949a85e4b8SEric Auger 
create_first_domain(void)9959a85e4b8SEric Auger static TestGTreeDomain *create_first_domain(void)
9969a85e4b8SEric Auger {
9979a85e4b8SEric Auger     TestGTreeDomain *domain;
9989a85e4b8SEric Auger     TestGTreeMapping *map_a, *map_b;
9999a85e4b8SEric Auger     TestGTreeInterval *a, *b;
10009a85e4b8SEric Auger 
1001b21e2380SMarkus Armbruster     domain = g_new0(TestGTreeDomain, 1);
10029a85e4b8SEric Auger     domain->id = 6;
10039a85e4b8SEric Auger 
1004b21e2380SMarkus Armbruster     a = g_new0(TestGTreeInterval, 1);
10059a85e4b8SEric Auger     a->low = 0x1000;
10069a85e4b8SEric Auger     a->high = 0x1FFF;
10079a85e4b8SEric Auger 
1008b21e2380SMarkus Armbruster     b = g_new0(TestGTreeInterval, 1);
10099a85e4b8SEric Auger     b->low = 0x4000;
10109a85e4b8SEric Auger     b->high = 0x4FFF;
10119a85e4b8SEric Auger 
1012b21e2380SMarkus Armbruster     map_a = g_new0(TestGTreeMapping, 1);
10139a85e4b8SEric Auger     map_a->phys_addr = 0xa000;
10149a85e4b8SEric Auger     map_a->flags = 1;
10159a85e4b8SEric Auger 
1016b21e2380SMarkus Armbruster     map_b = g_new0(TestGTreeMapping, 1);
10179a85e4b8SEric Auger     map_b->phys_addr = 0xe0000;
10189a85e4b8SEric Auger     map_b->flags = 2;
10199a85e4b8SEric Auger 
10209a85e4b8SEric Auger     domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp, NULL,
10219a85e4b8SEric Auger                                         (GDestroyNotify)g_free,
10229a85e4b8SEric Auger                                         (GDestroyNotify)g_free);
10239a85e4b8SEric Auger     g_tree_insert(domain->mappings, a, map_a);
10249a85e4b8SEric Auger     g_tree_insert(domain->mappings, b, map_b);
10259a85e4b8SEric Auger     return domain;
10269a85e4b8SEric Auger }
10279a85e4b8SEric Auger 
test_gtree_save_domain(void)10289a85e4b8SEric Auger static void test_gtree_save_domain(void)
10299a85e4b8SEric Auger {
10309a85e4b8SEric Auger     TestGTreeDomain *first_domain = create_first_domain();
10319a85e4b8SEric Auger 
10329a85e4b8SEric Auger     save_vmstate(&vmstate_domain, first_domain);
10339a85e4b8SEric Auger     compare_vmstate(first_domain_dump, sizeof(first_domain_dump));
10349a85e4b8SEric Auger     destroy_domain(first_domain);
10359a85e4b8SEric Auger }
10369a85e4b8SEric Auger 
10379a85e4b8SEric Auger struct match_node_data {
10389a85e4b8SEric Auger     GTree *tree;
10399a85e4b8SEric Auger     gpointer key;
10409a85e4b8SEric Auger     gpointer value;
10419a85e4b8SEric Auger };
10429a85e4b8SEric Auger 
10439a85e4b8SEric Auger struct tree_cmp_data {
10449a85e4b8SEric Auger     GTree *tree1;
10459a85e4b8SEric Auger     GTree *tree2;
10469a85e4b8SEric Auger     GTraverseFunc match_node;
10479a85e4b8SEric Auger };
10489a85e4b8SEric Auger 
match_interval_mapping_node(gpointer key,gpointer value,gpointer data)10499a85e4b8SEric Auger static gboolean match_interval_mapping_node(gpointer key,
10509a85e4b8SEric Auger                                             gpointer value, gpointer data)
10519a85e4b8SEric Auger {
10529a85e4b8SEric Auger     TestGTreeMapping *map_a, *map_b;
10539a85e4b8SEric Auger     TestGTreeInterval *a, *b;
10549a85e4b8SEric Auger     struct match_node_data *d = (struct match_node_data *)data;
10559a85e4b8SEric Auger     a = (TestGTreeInterval *)key;
10569a85e4b8SEric Auger     b = (TestGTreeInterval *)d->key;
10579a85e4b8SEric Auger 
10589a85e4b8SEric Auger     map_a = (TestGTreeMapping *)value;
10599a85e4b8SEric Auger     map_b = (TestGTreeMapping *)d->value;
10609a85e4b8SEric Auger 
10619a85e4b8SEric Auger     assert(a->low == b->low);
10629a85e4b8SEric Auger     assert(a->high == b->high);
10639a85e4b8SEric Auger     assert(map_a->phys_addr == map_b->phys_addr);
10649a85e4b8SEric Auger     assert(map_a->flags == map_b->flags);
10659a85e4b8SEric Auger     g_tree_remove(d->tree, key);
10669a85e4b8SEric Auger     return true;
10679a85e4b8SEric Auger }
10689a85e4b8SEric Auger 
diff_tree(gpointer key,gpointer value,gpointer data)10699a85e4b8SEric Auger static gboolean diff_tree(gpointer key, gpointer value, gpointer data)
10709a85e4b8SEric Auger {
10719a85e4b8SEric Auger     struct tree_cmp_data *tp = (struct tree_cmp_data *)data;
10729a85e4b8SEric Auger     struct match_node_data d = {tp->tree2, key, value};
10739a85e4b8SEric Auger 
10749a85e4b8SEric Auger     g_tree_foreach(tp->tree2, tp->match_node, &d);
10759a85e4b8SEric Auger     return false;
10769a85e4b8SEric Auger }
10779a85e4b8SEric Auger 
compare_trees(GTree * tree1,GTree * tree2,GTraverseFunc function)10789a85e4b8SEric Auger static void compare_trees(GTree *tree1, GTree *tree2,
10799a85e4b8SEric Auger                           GTraverseFunc function)
10809a85e4b8SEric Auger {
10819a85e4b8SEric Auger     struct tree_cmp_data tp = {tree1, tree2, function};
10829a85e4b8SEric Auger 
1083abe2c4bdSEric Auger     assert(g_tree_nnodes(tree1) == g_tree_nnodes(tree2));
10849a85e4b8SEric Auger     g_tree_foreach(tree1, diff_tree, &tp);
1085abe2c4bdSEric Auger     g_tree_destroy(g_tree_ref(tree1));
10869a85e4b8SEric Auger }
10879a85e4b8SEric Auger 
diff_domain(TestGTreeDomain * d1,TestGTreeDomain * d2)10889a85e4b8SEric Auger static void diff_domain(TestGTreeDomain *d1, TestGTreeDomain *d2)
10899a85e4b8SEric Auger {
10909a85e4b8SEric Auger     assert(d1->id == d2->id);
10919a85e4b8SEric Auger     compare_trees(d1->mappings, d2->mappings, match_interval_mapping_node);
10929a85e4b8SEric Auger }
10939a85e4b8SEric Auger 
match_domain_node(gpointer key,gpointer value,gpointer data)10949a85e4b8SEric Auger static gboolean match_domain_node(gpointer key, gpointer value, gpointer data)
10959a85e4b8SEric Auger {
10969a85e4b8SEric Auger     uint64_t id1, id2;
10979a85e4b8SEric Auger     TestGTreeDomain *d1, *d2;
10989a85e4b8SEric Auger     struct match_node_data *d = (struct match_node_data *)data;
10999a85e4b8SEric Auger 
11009a85e4b8SEric Auger     id1 = (uint64_t)(uintptr_t)key;
11019a85e4b8SEric Auger     id2 = (uint64_t)(uintptr_t)d->key;
11029a85e4b8SEric Auger     d1 = (TestGTreeDomain *)value;
11039a85e4b8SEric Auger     d2 = (TestGTreeDomain *)d->value;
11049a85e4b8SEric Auger     assert(id1 == id2);
11059a85e4b8SEric Auger     diff_domain(d1, d2);
11069a85e4b8SEric Auger     g_tree_remove(d->tree, key);
11079a85e4b8SEric Auger     return true;
11089a85e4b8SEric Auger }
11099a85e4b8SEric Auger 
diff_iommu(TestGTreeIOMMU * iommu1,TestGTreeIOMMU * iommu2)11109a85e4b8SEric Auger static void diff_iommu(TestGTreeIOMMU *iommu1, TestGTreeIOMMU *iommu2)
11119a85e4b8SEric Auger {
11129a85e4b8SEric Auger     assert(iommu1->id == iommu2->id);
11139a85e4b8SEric Auger     compare_trees(iommu1->domains, iommu2->domains, match_domain_node);
11149a85e4b8SEric Auger }
11159a85e4b8SEric Auger 
test_gtree_load_domain(void)11169a85e4b8SEric Auger static void test_gtree_load_domain(void)
11179a85e4b8SEric Auger {
1118b21e2380SMarkus Armbruster     TestGTreeDomain *dest_domain = g_new0(TestGTreeDomain, 1);
11199a85e4b8SEric Auger     TestGTreeDomain *orig_domain = create_first_domain();
11209a85e4b8SEric Auger     QEMUFile *fload, *fsave;
11219a85e4b8SEric Auger     char eof;
11229a85e4b8SEric Auger 
11239a85e4b8SEric Auger     fsave = open_test_file(true);
11249a85e4b8SEric Auger     qemu_put_buffer(fsave, first_domain_dump, sizeof(first_domain_dump));
11259a85e4b8SEric Auger     g_assert(!qemu_file_get_error(fsave));
11269a85e4b8SEric Auger     qemu_fclose(fsave);
11279a85e4b8SEric Auger 
11289a85e4b8SEric Auger     fload = open_test_file(false);
11299a85e4b8SEric Auger 
11309a85e4b8SEric Auger     vmstate_load_state(fload, &vmstate_domain, dest_domain, 1);
11319a85e4b8SEric Auger     eof = qemu_get_byte(fload);
11329a85e4b8SEric Auger     g_assert(!qemu_file_get_error(fload));
11339a85e4b8SEric Auger     g_assert_cmpint(orig_domain->id, ==, dest_domain->id);
11349a85e4b8SEric Auger     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
11359a85e4b8SEric Auger 
11369a85e4b8SEric Auger     diff_domain(orig_domain, dest_domain);
11379a85e4b8SEric Auger     destroy_domain(orig_domain);
11389a85e4b8SEric Auger     destroy_domain(dest_domain);
11399a85e4b8SEric Auger     qemu_fclose(fload);
11409a85e4b8SEric Auger }
11419a85e4b8SEric Auger 
11429a85e4b8SEric Auger uint8_t iommu_dump[] = {
11439a85e4b8SEric Auger     /* iommu id */
11449a85e4b8SEric Auger     0x00, 0x0, 0x0, 0x7,
11459a85e4b8SEric Auger     0x00, 0x0, 0x0, 0x2, /* 2 domains */
11469a85e4b8SEric Auger     0x1,/* start of domain 5 */
11479a85e4b8SEric Auger         0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x5, /* key = 5 */
11489a85e4b8SEric Auger         0x00, 0x0, 0x0, 0x5, /* domain1 id */
11499a85e4b8SEric Auger         0x00, 0x0, 0x0, 0x1, /* 1 mapping */
11509a85e4b8SEric Auger         0x1, /* start of mappings */
11519a85e4b8SEric Auger             /* c */
11529a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
11539a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF,
11549a85e4b8SEric Auger             /* map_c */
11559a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00,
11569a85e4b8SEric Auger             0x00, 0x0, 0x0, 0x3,
11579a85e4b8SEric Auger             0x0, /* end of domain1 mappings*/
11589a85e4b8SEric Auger     0x1,/* start of domain 6 */
11599a85e4b8SEric Auger         0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x6, /* key = 6 */
11609a85e4b8SEric Auger         0x00, 0x0, 0x0, 0x6, /* domain6 id */
11619a85e4b8SEric Auger             0x00, 0x0, 0x0, 0x2, /* 2 mappings */
11629a85e4b8SEric Auger             0x1, /* start of a */
11639a85e4b8SEric Auger             /* a */
11649a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
11659a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
11669a85e4b8SEric Auger             /* map_a */
11679a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
11689a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x01,
11699a85e4b8SEric Auger             0x1, /* start of b */
11709a85e4b8SEric Auger             /* b */
11719a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
11729a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
11739a85e4b8SEric Auger             /* map_b */
11749a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
11759a85e4b8SEric Auger             0x00, 0x00, 0x00, 0x02,
11769a85e4b8SEric Auger             0x0, /* end of domain6 mappings*/
11779a85e4b8SEric Auger     0x0, /* end of domains */
11789a85e4b8SEric Auger     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
11799a85e4b8SEric Auger };
11809a85e4b8SEric Auger 
create_iommu(void)11819a85e4b8SEric Auger static TestGTreeIOMMU *create_iommu(void)
11829a85e4b8SEric Auger {
1183b21e2380SMarkus Armbruster     TestGTreeIOMMU *iommu = g_new0(TestGTreeIOMMU, 1);
11849a85e4b8SEric Auger     TestGTreeDomain *first_domain = create_first_domain();
11859a85e4b8SEric Auger     TestGTreeDomain *second_domain;
11869a85e4b8SEric Auger     TestGTreeMapping *map_c;
11879a85e4b8SEric Auger     TestGTreeInterval *c;
11889a85e4b8SEric Auger 
11899a85e4b8SEric Auger     iommu->id = 7;
11909a85e4b8SEric Auger     iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp, NULL,
11919a85e4b8SEric Auger                                      NULL,
11929a85e4b8SEric Auger                                      destroy_domain);
11939a85e4b8SEric Auger 
1194b21e2380SMarkus Armbruster     second_domain = g_new0(TestGTreeDomain, 1);
11959a85e4b8SEric Auger     second_domain->id = 5;
11969a85e4b8SEric Auger     second_domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
11979a85e4b8SEric Auger                                               NULL,
11989a85e4b8SEric Auger                                               (GDestroyNotify)g_free,
11999a85e4b8SEric Auger                                               (GDestroyNotify)g_free);
12009a85e4b8SEric Auger 
12019a85e4b8SEric Auger     g_tree_insert(iommu->domains, GUINT_TO_POINTER(6), first_domain);
12029a85e4b8SEric Auger     g_tree_insert(iommu->domains, (gpointer)0x0000000000000005, second_domain);
12039a85e4b8SEric Auger 
1204b21e2380SMarkus Armbruster     c = g_new0(TestGTreeInterval, 1);
12059a85e4b8SEric Auger     c->low = 0x1000000;
12069a85e4b8SEric Auger     c->high = 0x1FFFFFF;
12079a85e4b8SEric Auger 
1208b21e2380SMarkus Armbruster     map_c = g_new0(TestGTreeMapping, 1);
12099a85e4b8SEric Auger     map_c->phys_addr = 0xF000000;
12109a85e4b8SEric Auger     map_c->flags = 0x3;
12119a85e4b8SEric Auger 
12129a85e4b8SEric Auger     g_tree_insert(second_domain->mappings, c, map_c);
12139a85e4b8SEric Auger     return iommu;
12149a85e4b8SEric Auger }
12159a85e4b8SEric Auger 
destroy_iommu(TestGTreeIOMMU * iommu)12169a85e4b8SEric Auger static void destroy_iommu(TestGTreeIOMMU *iommu)
12179a85e4b8SEric Auger {
12189a85e4b8SEric Auger     g_tree_destroy(iommu->domains);
12199a85e4b8SEric Auger     g_free(iommu);
12209a85e4b8SEric Auger }
12219a85e4b8SEric Auger 
test_gtree_save_iommu(void)12229a85e4b8SEric Auger static void test_gtree_save_iommu(void)
12239a85e4b8SEric Auger {
12249a85e4b8SEric Auger     TestGTreeIOMMU *iommu = create_iommu();
12259a85e4b8SEric Auger 
12269a85e4b8SEric Auger     save_vmstate(&vmstate_iommu, iommu);
12279a85e4b8SEric Auger     compare_vmstate(iommu_dump, sizeof(iommu_dump));
12289a85e4b8SEric Auger     destroy_iommu(iommu);
12299a85e4b8SEric Auger }
12309a85e4b8SEric Auger 
test_gtree_load_iommu(void)12319a85e4b8SEric Auger static void test_gtree_load_iommu(void)
12329a85e4b8SEric Auger {
1233b21e2380SMarkus Armbruster     TestGTreeIOMMU *dest_iommu = g_new0(TestGTreeIOMMU, 1);
12349a85e4b8SEric Auger     TestGTreeIOMMU *orig_iommu = create_iommu();
12359a85e4b8SEric Auger     QEMUFile *fsave, *fload;
12369a85e4b8SEric Auger     char eof;
12379a85e4b8SEric Auger 
12389a85e4b8SEric Auger     fsave = open_test_file(true);
12399a85e4b8SEric Auger     qemu_put_buffer(fsave, iommu_dump, sizeof(iommu_dump));
12409a85e4b8SEric Auger     g_assert(!qemu_file_get_error(fsave));
12419a85e4b8SEric Auger     qemu_fclose(fsave);
12429a85e4b8SEric Auger 
12439a85e4b8SEric Auger     fload = open_test_file(false);
12449a85e4b8SEric Auger     vmstate_load_state(fload, &vmstate_iommu, dest_iommu, 1);
12459a85e4b8SEric Auger     eof = qemu_get_byte(fload);
1246a6fbd637SChen Qun     g_assert(!qemu_file_get_error(fload));
12479a85e4b8SEric Auger     g_assert_cmpint(orig_iommu->id, ==, dest_iommu->id);
12489a85e4b8SEric Auger     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
12499a85e4b8SEric Auger 
12509a85e4b8SEric Auger     diff_iommu(orig_iommu, dest_iommu);
12519a85e4b8SEric Auger     destroy_iommu(orig_iommu);
12529a85e4b8SEric Auger     destroy_iommu(dest_iommu);
12539a85e4b8SEric Auger     qemu_fclose(fload);
12549a85e4b8SEric Auger }
12559a85e4b8SEric Auger 
12564746dbf8SEric Auger static uint8_t qlist_dump[] = {
12574746dbf8SEric Auger     0x00, 0x00, 0x00, 0x01, /* container id */
12584746dbf8SEric Auger     0x1, /* start of a */
12594746dbf8SEric Auger     0x00, 0x00, 0x00, 0x0a,
12604746dbf8SEric Auger     0x1, /* start of b */
12614746dbf8SEric Auger     0x00, 0x00, 0x0b, 0x00,
12624746dbf8SEric Auger     0x1, /* start of c */
12634746dbf8SEric Auger     0x00, 0x0c, 0x00, 0x00,
12644746dbf8SEric Auger     0x1, /* start of d */
12654746dbf8SEric Auger     0x0d, 0x00, 0x00, 0x00,
12664746dbf8SEric Auger     0x0, /* end of list */
12674746dbf8SEric Auger     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
12684746dbf8SEric Auger };
12694746dbf8SEric Auger 
alloc_container(void)12704746dbf8SEric Auger static TestQListContainer *alloc_container(void)
12714746dbf8SEric Auger {
1272b21e2380SMarkus Armbruster     TestQListElement *a = g_new(TestQListElement, 1);
1273b21e2380SMarkus Armbruster     TestQListElement *b = g_new(TestQListElement, 1);
1274b21e2380SMarkus Armbruster     TestQListElement *c = g_new(TestQListElement, 1);
1275b21e2380SMarkus Armbruster     TestQListElement *d = g_new(TestQListElement, 1);
1276b21e2380SMarkus Armbruster     TestQListContainer *container = g_new(TestQListContainer, 1);
12774746dbf8SEric Auger 
12784746dbf8SEric Auger     a->id = 0x0a;
12794746dbf8SEric Auger     b->id = 0x0b00;
12804746dbf8SEric Auger     c->id = 0xc0000;
12814746dbf8SEric Auger     d->id = 0xd000000;
12824746dbf8SEric Auger     container->id = 1;
12834746dbf8SEric Auger 
12844746dbf8SEric Auger     QLIST_INIT(&container->list);
12854746dbf8SEric Auger     QLIST_INSERT_HEAD(&container->list, d, next);
12864746dbf8SEric Auger     QLIST_INSERT_HEAD(&container->list, c, next);
12874746dbf8SEric Auger     QLIST_INSERT_HEAD(&container->list, b, next);
12884746dbf8SEric Auger     QLIST_INSERT_HEAD(&container->list, a, next);
12894746dbf8SEric Auger     return container;
12904746dbf8SEric Auger }
12914746dbf8SEric Auger 
free_container(TestQListContainer * container)12924746dbf8SEric Auger static void free_container(TestQListContainer *container)
12934746dbf8SEric Auger {
12944746dbf8SEric Auger     TestQListElement *iter, *tmp;
12954746dbf8SEric Auger 
12964746dbf8SEric Auger     QLIST_FOREACH_SAFE(iter, &container->list, next, tmp) {
12974746dbf8SEric Auger         QLIST_REMOVE(iter, next);
12984746dbf8SEric Auger         g_free(iter);
12994746dbf8SEric Auger     }
13004746dbf8SEric Auger     g_free(container);
13014746dbf8SEric Auger }
13024746dbf8SEric Auger 
compare_containers(TestQListContainer * c1,TestQListContainer * c2)13034746dbf8SEric Auger static void compare_containers(TestQListContainer *c1, TestQListContainer *c2)
13044746dbf8SEric Auger {
13054746dbf8SEric Auger     TestQListElement *first_item_c1, *first_item_c2;
13064746dbf8SEric Auger 
13074746dbf8SEric Auger     while (!QLIST_EMPTY(&c1->list)) {
13084746dbf8SEric Auger         first_item_c1 = QLIST_FIRST(&c1->list);
13094746dbf8SEric Auger         first_item_c2 = QLIST_FIRST(&c2->list);
13104746dbf8SEric Auger         assert(first_item_c2);
13114746dbf8SEric Auger         assert(first_item_c1->id == first_item_c2->id);
13124746dbf8SEric Auger         QLIST_REMOVE(first_item_c1, next);
13134746dbf8SEric Auger         QLIST_REMOVE(first_item_c2, next);
13144746dbf8SEric Auger         g_free(first_item_c1);
13154746dbf8SEric Auger         g_free(first_item_c2);
13164746dbf8SEric Auger     }
13174746dbf8SEric Auger     assert(QLIST_EMPTY(&c2->list));
13184746dbf8SEric Auger }
13194746dbf8SEric Auger 
13204746dbf8SEric Auger /*
13214746dbf8SEric Auger  * Check the prev & next fields are correct by doing list
13224746dbf8SEric Auger  * manipulations on the container. We will do that for both
13234746dbf8SEric Auger  * the source and the destination containers
13244746dbf8SEric Auger  */
manipulate_container(TestQListContainer * c)13254746dbf8SEric Auger static void manipulate_container(TestQListContainer *c)
13264746dbf8SEric Auger {
13274746dbf8SEric Auger      TestQListElement *prev = NULL, *iter = QLIST_FIRST(&c->list);
13284746dbf8SEric Auger      TestQListElement *elem;
13294746dbf8SEric Auger 
1330b21e2380SMarkus Armbruster      elem = g_new(TestQListElement, 1);
13314746dbf8SEric Auger      elem->id = 0x12;
13324746dbf8SEric Auger      QLIST_INSERT_AFTER(iter, elem, next);
13334746dbf8SEric Auger 
1334b21e2380SMarkus Armbruster      elem = g_new(TestQListElement, 1);
13354746dbf8SEric Auger      elem->id = 0x13;
13364746dbf8SEric Auger      QLIST_INSERT_HEAD(&c->list, elem, next);
13374746dbf8SEric Auger 
13384746dbf8SEric Auger      while (iter) {
13394746dbf8SEric Auger         prev = iter;
13404746dbf8SEric Auger         iter = QLIST_NEXT(iter, next);
13414746dbf8SEric Auger      }
13424746dbf8SEric Auger 
1343b21e2380SMarkus Armbruster      elem = g_new(TestQListElement, 1);
13444746dbf8SEric Auger      elem->id = 0x14;
13454746dbf8SEric Auger      QLIST_INSERT_BEFORE(prev, elem, next);
13464746dbf8SEric Auger 
1347b21e2380SMarkus Armbruster      elem = g_new(TestQListElement, 1);
13484746dbf8SEric Auger      elem->id = 0x15;
13494746dbf8SEric Auger      QLIST_INSERT_AFTER(prev, elem, next);
13504746dbf8SEric Auger 
13514746dbf8SEric Auger      QLIST_REMOVE(prev, next);
13524746dbf8SEric Auger      g_free(prev);
13534746dbf8SEric Auger }
13544746dbf8SEric Auger 
test_save_qlist(void)13554746dbf8SEric Auger static void test_save_qlist(void)
13564746dbf8SEric Auger {
13574746dbf8SEric Auger     TestQListContainer *container = alloc_container();
13584746dbf8SEric Auger 
13594746dbf8SEric Auger     save_vmstate(&vmstate_container, container);
13604746dbf8SEric Auger     compare_vmstate(qlist_dump, sizeof(qlist_dump));
13614746dbf8SEric Auger     free_container(container);
13624746dbf8SEric Auger }
13634746dbf8SEric Auger 
test_load_qlist(void)13644746dbf8SEric Auger static void test_load_qlist(void)
13654746dbf8SEric Auger {
13664746dbf8SEric Auger     QEMUFile *fsave, *fload;
13674746dbf8SEric Auger     TestQListContainer *orig_container = alloc_container();
1368b21e2380SMarkus Armbruster     TestQListContainer *dest_container = g_new0(TestQListContainer, 1);
13694746dbf8SEric Auger     char eof;
13704746dbf8SEric Auger 
13714746dbf8SEric Auger     QLIST_INIT(&dest_container->list);
13724746dbf8SEric Auger 
13734746dbf8SEric Auger     fsave = open_test_file(true);
13744746dbf8SEric Auger     qemu_put_buffer(fsave, qlist_dump, sizeof(qlist_dump));
13754746dbf8SEric Auger     g_assert(!qemu_file_get_error(fsave));
13764746dbf8SEric Auger     qemu_fclose(fsave);
13774746dbf8SEric Auger 
13784746dbf8SEric Auger     fload = open_test_file(false);
13794746dbf8SEric Auger     vmstate_load_state(fload, &vmstate_container, dest_container, 1);
13804746dbf8SEric Auger     eof = qemu_get_byte(fload);
13814746dbf8SEric Auger     g_assert(!qemu_file_get_error(fload));
13824746dbf8SEric Auger     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
13834746dbf8SEric Auger     manipulate_container(orig_container);
13844746dbf8SEric Auger     manipulate_container(dest_container);
13854746dbf8SEric Auger     compare_containers(orig_container, dest_container);
13864746dbf8SEric Auger     free_container(orig_container);
13874746dbf8SEric Auger     free_container(dest_container);
1388a6fbd637SChen Qun     qemu_fclose(fload);
13894746dbf8SEric Auger }
13904746dbf8SEric Auger 
13915c379d90SDr. David Alan Gilbert typedef struct TmpTestStruct {
13925c379d90SDr. David Alan Gilbert     TestStruct *parent;
13935c379d90SDr. David Alan Gilbert     int64_t diff;
13945c379d90SDr. David Alan Gilbert } TmpTestStruct;
13955c379d90SDr. David Alan Gilbert 
tmp_child_pre_save(void * opaque)139644b1ff31SDr. David Alan Gilbert static int tmp_child_pre_save(void *opaque)
13975c379d90SDr. David Alan Gilbert {
13985c379d90SDr. David Alan Gilbert     struct TmpTestStruct *tts = opaque;
13995c379d90SDr. David Alan Gilbert 
14005c379d90SDr. David Alan Gilbert     tts->diff = tts->parent->b - tts->parent->a;
140144b1ff31SDr. David Alan Gilbert 
140244b1ff31SDr. David Alan Gilbert     return 0;
14035c379d90SDr. David Alan Gilbert }
14045c379d90SDr. David Alan Gilbert 
tmp_child_post_load(void * opaque,int version_id)14055c379d90SDr. David Alan Gilbert static int tmp_child_post_load(void *opaque, int version_id)
14065c379d90SDr. David Alan Gilbert {
14075c379d90SDr. David Alan Gilbert     struct TmpTestStruct *tts = opaque;
14085c379d90SDr. David Alan Gilbert 
14095c379d90SDr. David Alan Gilbert     tts->parent->b = tts->parent->a + tts->diff;
14105c379d90SDr. David Alan Gilbert 
14115c379d90SDr. David Alan Gilbert     return 0;
14125c379d90SDr. David Alan Gilbert }
14135c379d90SDr. David Alan Gilbert 
14145c379d90SDr. David Alan Gilbert static const VMStateDescription vmstate_tmp_back_to_parent = {
14155c379d90SDr. David Alan Gilbert     .name = "test/tmp_child_parent",
1416*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
14175c379d90SDr. David Alan Gilbert         VMSTATE_UINT64(f, TestStruct),
14185c379d90SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
14195c379d90SDr. David Alan Gilbert     }
14205c379d90SDr. David Alan Gilbert };
14215c379d90SDr. David Alan Gilbert 
14225c379d90SDr. David Alan Gilbert static const VMStateDescription vmstate_tmp_child = {
14235c379d90SDr. David Alan Gilbert     .name = "test/tmp_child",
14245c379d90SDr. David Alan Gilbert     .pre_save = tmp_child_pre_save,
14255c379d90SDr. David Alan Gilbert     .post_load = tmp_child_post_load,
1426*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
14275c379d90SDr. David Alan Gilbert         VMSTATE_INT64(diff, TmpTestStruct),
14285c379d90SDr. David Alan Gilbert         VMSTATE_STRUCT_POINTER(parent, TmpTestStruct,
14295c379d90SDr. David Alan Gilbert                                vmstate_tmp_back_to_parent, TestStruct),
14305c379d90SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
14315c379d90SDr. David Alan Gilbert     }
14325c379d90SDr. David Alan Gilbert };
14335c379d90SDr. David Alan Gilbert 
14345c379d90SDr. David Alan Gilbert static const VMStateDescription vmstate_with_tmp = {
14355c379d90SDr. David Alan Gilbert     .name = "test/with_tmp",
14365c379d90SDr. David Alan Gilbert     .version_id = 1,
1437*57c73988SRichard Henderson     .fields = (const VMStateField[]) {
14385c379d90SDr. David Alan Gilbert         VMSTATE_UINT32(a, TestStruct),
14395c379d90SDr. David Alan Gilbert         VMSTATE_UINT64(d, TestStruct),
14405c379d90SDr. David Alan Gilbert         VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child),
14415c379d90SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
14425c379d90SDr. David Alan Gilbert     }
14435c379d90SDr. David Alan Gilbert };
14445c379d90SDr. David Alan Gilbert 
obj_tmp_copy(void * target,void * source)14455c379d90SDr. David Alan Gilbert static void obj_tmp_copy(void *target, void *source)
14465c379d90SDr. David Alan Gilbert {
14475c379d90SDr. David Alan Gilbert     memcpy(target, source, sizeof(TestStruct));
14485c379d90SDr. David Alan Gilbert }
14495c379d90SDr. David Alan Gilbert 
test_tmp_struct(void)14505c379d90SDr. David Alan Gilbert static void test_tmp_struct(void)
14515c379d90SDr. David Alan Gilbert {
14525c379d90SDr. David Alan Gilbert     TestStruct obj, obj_clone;
14535c379d90SDr. David Alan Gilbert 
14545c379d90SDr. David Alan Gilbert     uint8_t const wire_with_tmp[] = {
14555c379d90SDr. David Alan Gilbert         /* u32 a */ 0x00, 0x00, 0x00, 0x02,
14565c379d90SDr. David Alan Gilbert         /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
14575c379d90SDr. David Alan Gilbert         /* diff  */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
14585c379d90SDr. David Alan Gilbert         /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
14595c379d90SDr. David Alan Gilbert         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
14605c379d90SDr. David Alan Gilbert     };
14615c379d90SDr. David Alan Gilbert 
14625c379d90SDr. David Alan Gilbert     memset(&obj, 0, sizeof(obj));
14635c379d90SDr. David Alan Gilbert     obj.a = 2;
14645c379d90SDr. David Alan Gilbert     obj.b = 4;
14655c379d90SDr. David Alan Gilbert     obj.d = 1;
14665c379d90SDr. David Alan Gilbert     obj.f = 8;
14675c379d90SDr. David Alan Gilbert     save_vmstate(&vmstate_with_tmp, &obj);
14685c379d90SDr. David Alan Gilbert 
14695c379d90SDr. David Alan Gilbert     compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
14705c379d90SDr. David Alan Gilbert 
14715c379d90SDr. David Alan Gilbert     memset(&obj, 0, sizeof(obj));
14725c379d90SDr. David Alan Gilbert     SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone,
14735c379d90SDr. David Alan Gilbert                          obj_tmp_copy, 1, wire_with_tmp,
14745c379d90SDr. David Alan Gilbert                          sizeof(wire_with_tmp)));
14755c379d90SDr. David Alan Gilbert     g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */
14765c379d90SDr. David Alan Gilbert     g_assert_cmpint(obj.b, ==, 4); /* from the post_load */
14775c379d90SDr. David Alan Gilbert     g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */
14785c379d90SDr. David Alan Gilbert     g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
14795c379d90SDr. David Alan Gilbert }
14805c379d90SDr. David Alan Gilbert 
main(int argc,char ** argv)14812668b4bfSEduardo Habkost int main(int argc, char **argv)
14822668b4bfSEduardo Habkost {
148396c64746SYonggang Luo     g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
148496c64746SYonggang Luo                                                  g_get_tmp_dir());
14852668b4bfSEduardo Habkost     temp_fd = mkstemp(temp_file);
14861c861885SPeter Maydell     g_assert(temp_fd >= 0);
14872668b4bfSEduardo Habkost 
14888925839fSDaniel P. Berrange     module_call_init(MODULE_INIT_QOM);
14898925839fSDaniel P. Berrange 
1490e468ffdcSMarc-André Lureau     g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1491977a7204SDaniel P. Berrangé 
14922668b4bfSEduardo Habkost     g_test_init(&argc, &argv, NULL);
14934ea7df4eSJuan Quintela     g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
1494b95d6588SMarc-André Lureau     g_test_add_func("/vmstate/simple/array", test_simple_array);
14952668b4bfSEduardo Habkost     g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
14962668b4bfSEduardo Habkost     g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
14972668b4bfSEduardo Habkost     g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
14982668b4bfSEduardo Habkost     g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
14992668b4bfSEduardo Habkost     g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
15002668b4bfSEduardo Habkost     g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
15018cc49f03SHalil Pasic     g_test_add_func("/vmstate/array/ptr/str/no0/save",
15028cc49f03SHalil Pasic                     test_arr_ptr_str_no0_save);
15038cc49f03SHalil Pasic     g_test_add_func("/vmstate/array/ptr/str/no0/load",
15048cc49f03SHalil Pasic                     test_arr_ptr_str_no0_load);
1505cc958831SHalil Pasic     g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save);
1506cc958831SHalil Pasic     g_test_add_func("/vmstate/array/ptr/str/0/load",
1507cc958831SHalil Pasic                     test_arr_ptr_str_0_load);
150843333099SHalil Pasic     g_test_add_func("/vmstate/array/ptr/prim/0/save",
150943333099SHalil Pasic                     test_arr_ptr_prim_0_save);
151043333099SHalil Pasic     g_test_add_func("/vmstate/array/ptr/prim/0/load",
151143333099SHalil Pasic                     test_arr_ptr_prim_0_load);
15127e99f22cSJianjun Duan     g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q);
15137e99f22cSJianjun Duan     g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q);
15149a85e4b8SEric Auger     g_test_add_func("/vmstate/gtree/save/savedomain", test_gtree_save_domain);
15159a85e4b8SEric Auger     g_test_add_func("/vmstate/gtree/load/loaddomain", test_gtree_load_domain);
15169a85e4b8SEric Auger     g_test_add_func("/vmstate/gtree/save/saveiommu", test_gtree_save_iommu);
15179a85e4b8SEric Auger     g_test_add_func("/vmstate/gtree/load/loadiommu", test_gtree_load_iommu);
15184746dbf8SEric Auger     g_test_add_func("/vmstate/qlist/save/saveqlist", test_save_qlist);
15194746dbf8SEric Auger     g_test_add_func("/vmstate/qlist/load/loadqlist", test_load_qlist);
15205c379d90SDr. David Alan Gilbert     g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
15212668b4bfSEduardo Habkost     g_test_run();
15222668b4bfSEduardo Habkost 
15232668b4bfSEduardo Habkost     close(temp_fd);
15242668b4bfSEduardo Habkost     unlink(temp_file);
15252668b4bfSEduardo Habkost 
15262668b4bfSEduardo Habkost     return 0;
15272668b4bfSEduardo Habkost }
1528