xref: /openbmc/qemu/tests/unit/test-vmstate.c (revision 2bfd3c48)
1 /*
2  *  Test code for VMState
3  *
4  *  Copyright (c) 2013 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 
27 #include "migration/vmstate.h"
28 #include "migration/qemu-file-types.h"
29 #include "../migration/qemu-file.h"
30 #include "../migration/savevm.h"
31 #include "qemu/module.h"
32 #include "io/channel-file.h"
33 
34 static int temp_fd;
35 
36 
37 /* Duplicate temp_fd and seek to the beginning of the file */
38 static QEMUFile *open_test_file(bool write)
39 {
40     int fd;
41     QIOChannel *ioc;
42     QEMUFile *f;
43 
44     fd = dup(temp_fd);
45     g_assert(fd >= 0);
46     lseek(fd, 0, SEEK_SET);
47     if (write) {
48         g_assert_cmpint(ftruncate(fd, 0), ==, 0);
49     }
50     ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
51     if (write) {
52         f = qemu_file_new_output(ioc);
53     } else {
54         f = qemu_file_new_input(ioc);
55     }
56     object_unref(OBJECT(ioc));
57     return f;
58 }
59 
60 #define SUCCESS(val) \
61     g_assert_cmpint((val), ==, 0)
62 
63 #define FAILURE(val) \
64     g_assert_cmpint((val), !=, 0)
65 
66 static void save_vmstate(const VMStateDescription *desc, void *obj)
67 {
68     QEMUFile *f = open_test_file(true);
69 
70     /* Save file with vmstate */
71     int ret = vmstate_save_state(f, desc, obj, NULL);
72     g_assert(!ret);
73     qemu_put_byte(f, QEMU_VM_EOF);
74     g_assert(!qemu_file_get_error(f));
75     qemu_fclose(f);
76 }
77 
78 static void save_buffer(const uint8_t *buf, size_t buf_size)
79 {
80     QEMUFile *fsave = open_test_file(true);
81     qemu_put_buffer(fsave, buf, buf_size);
82     qemu_fclose(fsave);
83 }
84 
85 static void compare_vmstate(const uint8_t *wire, size_t size)
86 {
87     QEMUFile *f = open_test_file(false);
88     g_autofree uint8_t *result = g_malloc(size);
89 
90     /* read back as binary */
91 
92     g_assert_cmpint(qemu_get_buffer(f, result, size), ==, size);
93     g_assert(!qemu_file_get_error(f));
94 
95     /* Compare that what is on the file is the same that what we
96        expected to be there */
97     SUCCESS(memcmp(result, wire, size));
98 
99     /* Must reach EOF */
100     qemu_get_byte(f);
101     g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
102 
103     qemu_fclose(f);
104 }
105 
106 static int load_vmstate_one(const VMStateDescription *desc, void *obj,
107                             int version, const uint8_t *wire, size_t size)
108 {
109     QEMUFile *f;
110     int ret;
111 
112     f = open_test_file(true);
113     qemu_put_buffer(f, wire, size);
114     qemu_fclose(f);
115 
116     f = open_test_file(false);
117     ret = vmstate_load_state(f, desc, obj, version);
118     if (ret) {
119         g_assert(qemu_file_get_error(f));
120     } else{
121         g_assert(!qemu_file_get_error(f));
122     }
123     qemu_fclose(f);
124     return ret;
125 }
126 
127 
128 static int load_vmstate(const VMStateDescription *desc,
129                         void *obj, void *obj_clone,
130                         void (*obj_copy)(void *, void*),
131                         int version, const uint8_t *wire, size_t size)
132 {
133     /* We test with zero size */
134     obj_copy(obj_clone, obj);
135     FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
136 
137     /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
138      * able to test in the middle */
139 
140     if (size > 3) {
141 
142         /* We test with size - 2. We can't test size - 1 due to EOF tricks */
143         obj_copy(obj, obj_clone);
144         FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
145 
146         /* Test with size/2, first half of real state */
147         obj_copy(obj, obj_clone);
148         FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
149 
150         /* Test with size/2, second half of real state */
151         obj_copy(obj, obj_clone);
152         FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
153 
154     }
155     obj_copy(obj, obj_clone);
156     return load_vmstate_one(desc, obj, version, wire, size);
157 }
158 
159 /* Test struct that we are going to use for our tests */
160 
161 typedef struct TestSimple {
162     bool     b_1,   b_2;
163     uint8_t  u8_1;
164     uint16_t u16_1;
165     uint32_t u32_1;
166     uint64_t u64_1;
167     int8_t   i8_1,  i8_2;
168     int16_t  i16_1, i16_2;
169     int32_t  i32_1, i32_2;
170     int64_t  i64_1, i64_2;
171 } TestSimple;
172 
173 /* Object instantiation, we are going to use it in more than one test */
174 
175 TestSimple obj_simple = {
176     .b_1 = true,
177     .b_2 = false,
178     .u8_1 = 130,
179     .u16_1 = 512,
180     .u32_1 = 70000,
181     .u64_1 = 12121212,
182     .i8_1 = 65,
183     .i8_2 = -65,
184     .i16_1 = 512,
185     .i16_2 = -512,
186     .i32_1 = 70000,
187     .i32_2 = -70000,
188     .i64_1 = 12121212,
189     .i64_2 = -12121212,
190 };
191 
192 /* Description of the values.  If you add a primitive type
193    you are expected to add a test here */
194 
195 static const VMStateDescription vmstate_simple_primitive = {
196     .name = "simple/primitive",
197     .version_id = 1,
198     .minimum_version_id = 1,
199     .fields = (const VMStateField[]) {
200         VMSTATE_BOOL(b_1, TestSimple),
201         VMSTATE_BOOL(b_2, TestSimple),
202         VMSTATE_UINT8(u8_1, TestSimple),
203         VMSTATE_UINT16(u16_1, TestSimple),
204         VMSTATE_UINT32(u32_1, TestSimple),
205         VMSTATE_UINT64(u64_1, TestSimple),
206         VMSTATE_INT8(i8_1, TestSimple),
207         VMSTATE_INT8(i8_2, TestSimple),
208         VMSTATE_INT16(i16_1, TestSimple),
209         VMSTATE_INT16(i16_2, TestSimple),
210         VMSTATE_INT32(i32_1, TestSimple),
211         VMSTATE_INT32(i32_2, TestSimple),
212         VMSTATE_INT64(i64_1, TestSimple),
213         VMSTATE_INT64(i64_2, TestSimple),
214         VMSTATE_END_OF_LIST()
215     }
216 };
217 
218 /* It describes what goes through the wire.  Our tests are basically:
219 
220    * save test
221      - save a struct a vmstate to a file
222      - read that file back (binary read, no vmstate)
223      - compare it with what we expect to be on the wire
224    * load test
225      - save to the file what we expect to be on the wire
226      - read struct back with vmstate in a different
227      - compare back with the original struct
228 */
229 
230 uint8_t wire_simple_primitive[] = {
231     /* b_1 */   0x01,
232     /* b_2 */   0x00,
233     /* u8_1 */  0x82,
234     /* u16_1 */ 0x02, 0x00,
235     /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
236     /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
237     /* i8_1 */  0x41,
238     /* i8_2 */  0xbf,
239     /* i16_1 */ 0x02, 0x00,
240     /* i16_2 */ 0xfe, 0x0,
241     /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
242     /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
243     /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
244     /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
245     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
246 };
247 
248 static void obj_simple_copy(void *target, void *source)
249 {
250     memcpy(target, source, sizeof(TestSimple));
251 }
252 
253 static void test_simple_primitive(void)
254 {
255     TestSimple obj, obj_clone;
256 
257     memset(&obj, 0, sizeof(obj));
258     save_vmstate(&vmstate_simple_primitive, &obj_simple);
259 
260     compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
261 
262     SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
263                          obj_simple_copy, 1, wire_simple_primitive,
264                          sizeof(wire_simple_primitive)));
265 
266 #define FIELD_EQUAL(name)   g_assert_cmpint(obj.name, ==, obj_simple.name)
267 
268     FIELD_EQUAL(b_1);
269     FIELD_EQUAL(b_2);
270     FIELD_EQUAL(u8_1);
271     FIELD_EQUAL(u16_1);
272     FIELD_EQUAL(u32_1);
273     FIELD_EQUAL(u64_1);
274     FIELD_EQUAL(i8_1);
275     FIELD_EQUAL(i8_2);
276     FIELD_EQUAL(i16_1);
277     FIELD_EQUAL(i16_2);
278     FIELD_EQUAL(i32_1);
279     FIELD_EQUAL(i32_2);
280     FIELD_EQUAL(i64_1);
281     FIELD_EQUAL(i64_2);
282 }
283 
284 typedef struct TestSimpleArray {
285     uint16_t u16_1[3];
286 } TestSimpleArray;
287 
288 /* Object instantiation, we are going to use it in more than one test */
289 
290 TestSimpleArray obj_simple_arr = {
291     .u16_1 = { 0x42, 0x43, 0x44 },
292 };
293 
294 /* Description of the values.  If you add a primitive type
295    you are expected to add a test here */
296 
297 static const VMStateDescription vmstate_simple_arr = {
298     .name = "simple/array",
299     .version_id = 1,
300     .minimum_version_id = 1,
301     .fields = (const VMStateField[]) {
302         VMSTATE_UINT16_ARRAY(u16_1, TestSimpleArray, 3),
303         VMSTATE_END_OF_LIST()
304     }
305 };
306 
307 uint8_t wire_simple_arr[] = {
308     /* u16_1 */ 0x00, 0x42,
309     /* u16_1 */ 0x00, 0x43,
310     /* u16_1 */ 0x00, 0x44,
311     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
312 };
313 
314 static void obj_simple_arr_copy(void *target, void *source)
315 {
316     memcpy(target, source, sizeof(TestSimpleArray));
317 }
318 
319 static void test_simple_array(void)
320 {
321     TestSimpleArray obj, obj_clone;
322 
323     memset(&obj, 0, sizeof(obj));
324     save_vmstate(&vmstate_simple_arr, &obj_simple_arr);
325 
326     compare_vmstate(wire_simple_arr, sizeof(wire_simple_arr));
327 
328     SUCCESS(load_vmstate(&vmstate_simple_arr, &obj, &obj_clone,
329                          obj_simple_arr_copy, 1, wire_simple_arr,
330                          sizeof(wire_simple_arr)));
331 }
332 
333 typedef struct TestStruct {
334     uint32_t a, b, c, e;
335     uint64_t d, f;
336     bool skip_c_e;
337 } TestStruct;
338 
339 static const VMStateDescription vmstate_versioned = {
340     .name = "test/versioned",
341     .version_id = 2,
342     .minimum_version_id = 1,
343     .fields = (const VMStateField[]) {
344         VMSTATE_UINT32(a, TestStruct),
345         VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
346                                              * we catch bugs more easily.
347                                              */
348         VMSTATE_UINT32(c, TestStruct),
349         VMSTATE_UINT64(d, TestStruct),
350         VMSTATE_UINT32_V(e, TestStruct, 2),
351         VMSTATE_UINT64_V(f, TestStruct, 2),
352         VMSTATE_END_OF_LIST()
353     }
354 };
355 
356 static void test_load_v1(void)
357 {
358     uint8_t buf[] = {
359         0, 0, 0, 10,             /* a */
360         0, 0, 0, 30,             /* c */
361         0, 0, 0, 0, 0, 0, 0, 40, /* d */
362         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
363     };
364     save_buffer(buf, sizeof(buf));
365 
366     QEMUFile *loading = open_test_file(false);
367     TestStruct obj = { .b = 200, .e = 500, .f = 600 };
368     vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
369     g_assert(!qemu_file_get_error(loading));
370     g_assert_cmpint(obj.a, ==, 10);
371     g_assert_cmpint(obj.b, ==, 200);
372     g_assert_cmpint(obj.c, ==, 30);
373     g_assert_cmpint(obj.d, ==, 40);
374     g_assert_cmpint(obj.e, ==, 500);
375     g_assert_cmpint(obj.f, ==, 600);
376     qemu_fclose(loading);
377 }
378 
379 static void test_load_v2(void)
380 {
381     uint8_t buf[] = {
382         0, 0, 0, 10,             /* a */
383         0, 0, 0, 20,             /* b */
384         0, 0, 0, 30,             /* c */
385         0, 0, 0, 0, 0, 0, 0, 40, /* d */
386         0, 0, 0, 50,             /* e */
387         0, 0, 0, 0, 0, 0, 0, 60, /* f */
388         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
389     };
390     save_buffer(buf, sizeof(buf));
391 
392     QEMUFile *loading = open_test_file(false);
393     TestStruct obj;
394     vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
395     g_assert_cmpint(obj.a, ==, 10);
396     g_assert_cmpint(obj.b, ==, 20);
397     g_assert_cmpint(obj.c, ==, 30);
398     g_assert_cmpint(obj.d, ==, 40);
399     g_assert_cmpint(obj.e, ==, 50);
400     g_assert_cmpint(obj.f, ==, 60);
401     qemu_fclose(loading);
402 }
403 
404 static bool test_skip(void *opaque, int version_id)
405 {
406     TestStruct *t = (TestStruct *)opaque;
407     return !t->skip_c_e;
408 }
409 
410 static const VMStateDescription vmstate_skipping = {
411     .name = "test/skip",
412     .version_id = 2,
413     .minimum_version_id = 1,
414     .fields = (const VMStateField[]) {
415         VMSTATE_UINT32(a, TestStruct),
416         VMSTATE_UINT32(b, TestStruct),
417         VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
418         VMSTATE_UINT64(d, TestStruct),
419         VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
420         VMSTATE_UINT64_V(f, TestStruct, 2),
421         VMSTATE_END_OF_LIST()
422     }
423 };
424 
425 
426 static void test_save_noskip(void)
427 {
428     QEMUFile *fsave = open_test_file(true);
429     TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
430                        .skip_c_e = false };
431     int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
432     g_assert(!ret);
433     g_assert(!qemu_file_get_error(fsave));
434 
435     uint8_t expected[] = {
436         0, 0, 0, 1,             /* a */
437         0, 0, 0, 2,             /* b */
438         0, 0, 0, 3,             /* c */
439         0, 0, 0, 0, 0, 0, 0, 4, /* d */
440         0, 0, 0, 5,             /* e */
441         0, 0, 0, 0, 0, 0, 0, 6, /* f */
442     };
443 
444     qemu_fclose(fsave);
445     compare_vmstate(expected, sizeof(expected));
446 }
447 
448 static void test_save_skip(void)
449 {
450     QEMUFile *fsave = open_test_file(true);
451     TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
452                        .skip_c_e = true };
453     int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
454     g_assert(!ret);
455     g_assert(!qemu_file_get_error(fsave));
456 
457     uint8_t expected[] = {
458         0, 0, 0, 1,             /* a */
459         0, 0, 0, 2,             /* b */
460         0, 0, 0, 0, 0, 0, 0, 4, /* d */
461         0, 0, 0, 0, 0, 0, 0, 6, /* f */
462     };
463 
464     qemu_fclose(fsave);
465     compare_vmstate(expected, sizeof(expected));
466 }
467 
468 static void test_load_noskip(void)
469 {
470     uint8_t buf[] = {
471         0, 0, 0, 10,             /* a */
472         0, 0, 0, 20,             /* b */
473         0, 0, 0, 30,             /* c */
474         0, 0, 0, 0, 0, 0, 0, 40, /* d */
475         0, 0, 0, 50,             /* e */
476         0, 0, 0, 0, 0, 0, 0, 60, /* f */
477         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
478     };
479     save_buffer(buf, sizeof(buf));
480 
481     QEMUFile *loading = open_test_file(false);
482     TestStruct obj = { .skip_c_e = false };
483     vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
484     g_assert(!qemu_file_get_error(loading));
485     g_assert_cmpint(obj.a, ==, 10);
486     g_assert_cmpint(obj.b, ==, 20);
487     g_assert_cmpint(obj.c, ==, 30);
488     g_assert_cmpint(obj.d, ==, 40);
489     g_assert_cmpint(obj.e, ==, 50);
490     g_assert_cmpint(obj.f, ==, 60);
491     qemu_fclose(loading);
492 }
493 
494 static void test_load_skip(void)
495 {
496     uint8_t buf[] = {
497         0, 0, 0, 10,             /* a */
498         0, 0, 0, 20,             /* b */
499         0, 0, 0, 0, 0, 0, 0, 40, /* d */
500         0, 0, 0, 0, 0, 0, 0, 60, /* f */
501         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
502     };
503     save_buffer(buf, sizeof(buf));
504 
505     QEMUFile *loading = open_test_file(false);
506     TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
507     vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
508     g_assert(!qemu_file_get_error(loading));
509     g_assert_cmpint(obj.a, ==, 10);
510     g_assert_cmpint(obj.b, ==, 20);
511     g_assert_cmpint(obj.c, ==, 300);
512     g_assert_cmpint(obj.d, ==, 40);
513     g_assert_cmpint(obj.e, ==, 500);
514     g_assert_cmpint(obj.f, ==, 60);
515     qemu_fclose(loading);
516 }
517 
518 typedef struct {
519     int32_t i;
520 } TestStructTriv;
521 
522 const VMStateDescription vmsd_tst = {
523     .name = "test/tst",
524     .version_id = 1,
525     .minimum_version_id = 1,
526     .fields = (const VMStateField[]) {
527         VMSTATE_INT32(i, TestStructTriv),
528         VMSTATE_END_OF_LIST()
529     }
530 };
531 
532 /* test array migration */
533 
534 #define AR_SIZE 4
535 
536 typedef struct {
537     TestStructTriv *ar[AR_SIZE];
538 } TestArrayOfPtrToStuct;
539 
540 const VMStateDescription vmsd_arps = {
541     .name = "test/arps",
542     .version_id = 1,
543     .minimum_version_id = 1,
544     .fields = (const VMStateField[]) {
545         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
546                 AR_SIZE, 0, vmsd_tst, TestStructTriv),
547         VMSTATE_END_OF_LIST()
548     }
549 };
550 
551 static uint8_t wire_arr_ptr_no0[] = {
552     0x00, 0x00, 0x00, 0x00,
553     0x00, 0x00, 0x00, 0x01,
554     0x00, 0x00, 0x00, 0x02,
555     0x00, 0x00, 0x00, 0x03,
556     QEMU_VM_EOF
557 };
558 
559 static void test_arr_ptr_str_no0_save(void)
560 {
561     TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
562     TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
563 
564     save_vmstate(&vmsd_arps, &sample);
565     compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
566 }
567 
568 static void test_arr_ptr_str_no0_load(void)
569 {
570     TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
571     TestStructTriv ar[AR_SIZE] = {};
572     TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
573     int idx;
574 
575     save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
576     SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
577                           wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)));
578     for (idx = 0; idx < AR_SIZE; ++idx) {
579         /* compare the target array ar with the ground truth array ar_gt */
580         g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
581     }
582 }
583 
584 static uint8_t wire_arr_ptr_0[] = {
585     0x00, 0x00, 0x00, 0x00,
586     VMS_NULLPTR_MARKER,
587     0x00, 0x00, 0x00, 0x02,
588     0x00, 0x00, 0x00, 0x03,
589     QEMU_VM_EOF
590 };
591 
592 static void test_arr_ptr_str_0_save(void)
593 {
594     TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
595     TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
596 
597     save_vmstate(&vmsd_arps, &sample);
598     compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
599 }
600 
601 static void test_arr_ptr_str_0_load(void)
602 {
603     TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 0}, {.i = 2}, {.i = 3} };
604     TestStructTriv ar[AR_SIZE] = {};
605     TestArrayOfPtrToStuct obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
606     int idx;
607 
608     save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
609     SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
610                           wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
611     for (idx = 0; idx < AR_SIZE; ++idx) {
612         /* compare the target array ar with the ground truth array ar_gt */
613         g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
614     }
615     for (idx = 0; idx < AR_SIZE; ++idx) {
616         if (idx == 1) {
617             g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0);
618         } else {
619             g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0);
620         }
621     }
622 }
623 
624 typedef struct TestArrayOfPtrToInt {
625     int32_t *ar[AR_SIZE];
626 } TestArrayOfPtrToInt;
627 
628 const VMStateDescription vmsd_arpp = {
629     .name = "test/arps",
630     .version_id = 1,
631     .minimum_version_id = 1,
632     .fields = (const VMStateField[]) {
633         VMSTATE_ARRAY_OF_POINTER(ar, TestArrayOfPtrToInt,
634                 AR_SIZE, 0, vmstate_info_int32, int32_t*),
635         VMSTATE_END_OF_LIST()
636     }
637 };
638 
639 static void test_arr_ptr_prim_0_save(void)
640 {
641     int32_t ar[AR_SIZE] = {0 , 1, 2, 3};
642     TestArrayOfPtrToInt  sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
643 
644     save_vmstate(&vmsd_arpp, &sample);
645     compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
646 }
647 
648 static void test_arr_ptr_prim_0_load(void)
649 {
650     int32_t ar_gt[AR_SIZE] = {0, 1, 2, 3};
651     int32_t ar[AR_SIZE] = {3 , 42, 1, 0};
652     TestArrayOfPtrToInt obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
653     int idx;
654 
655     save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
656     SUCCESS(load_vmstate_one(&vmsd_arpp, &obj, 1,
657                           wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
658     for (idx = 0; idx < AR_SIZE; ++idx) {
659         /* compare the target array ar with the ground truth array ar_gt */
660         if (idx == 1) {
661             g_assert_cmpint(42, ==, ar[idx]);
662         } else {
663             g_assert_cmpint(ar_gt[idx], ==, ar[idx]);
664         }
665     }
666 }
667 
668 /* test QTAILQ migration */
669 typedef struct TestQtailqElement TestQtailqElement;
670 
671 struct TestQtailqElement {
672     bool     b;
673     uint8_t  u8;
674     QTAILQ_ENTRY(TestQtailqElement) next;
675 };
676 
677 typedef struct TestQtailq {
678     int16_t  i16;
679     QTAILQ_HEAD(, TestQtailqElement) q;
680     int32_t  i32;
681 } TestQtailq;
682 
683 static const VMStateDescription vmstate_q_element = {
684     .name = "test/queue-element",
685     .version_id = 1,
686     .minimum_version_id = 1,
687     .fields = (const VMStateField[]) {
688         VMSTATE_BOOL(b, TestQtailqElement),
689         VMSTATE_UINT8(u8, TestQtailqElement),
690         VMSTATE_END_OF_LIST()
691     },
692 };
693 
694 static const VMStateDescription vmstate_q = {
695     .name = "test/queue",
696     .version_id = 1,
697     .minimum_version_id = 1,
698     .fields = (const VMStateField[]) {
699         VMSTATE_INT16(i16, TestQtailq),
700         VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
701                          next),
702         VMSTATE_INT32(i32, TestQtailq),
703         VMSTATE_END_OF_LIST()
704     }
705 };
706 
707 uint8_t wire_q[] = {
708     /* i16 */                     0xfe, 0x0,
709     /* start of element 0 of q */ 0x01,
710     /* .b  */                     0x01,
711     /* .u8 */                     0x82,
712     /* start of element 1 of q */ 0x01,
713     /* b */                       0x00,
714     /* u8 */                      0x41,
715     /* end of q */                0x00,
716     /* i32 */                     0x00, 0x01, 0x11, 0x70,
717     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
718 };
719 
720 static void test_save_q(void)
721 {
722     TestQtailq obj_q = {
723         .i16 = -512,
724         .i32 = 70000,
725     };
726 
727     TestQtailqElement obj_qe1 = {
728         .b = true,
729         .u8 = 130,
730     };
731 
732     TestQtailqElement obj_qe2 = {
733         .b = false,
734         .u8 = 65,
735     };
736 
737     QTAILQ_INIT(&obj_q.q);
738     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
739     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
740 
741     save_vmstate(&vmstate_q, &obj_q);
742     compare_vmstate(wire_q, sizeof(wire_q));
743 }
744 
745 static void test_load_q(void)
746 {
747     TestQtailq obj_q = {
748         .i16 = -512,
749         .i32 = 70000,
750     };
751 
752     TestQtailqElement obj_qe1 = {
753         .b = true,
754         .u8 = 130,
755     };
756 
757     TestQtailqElement obj_qe2 = {
758         .b = false,
759         .u8 = 65,
760     };
761 
762     QTAILQ_INIT(&obj_q.q);
763     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
764     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
765 
766     QEMUFile *fsave = open_test_file(true);
767 
768     qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
769     g_assert(!qemu_file_get_error(fsave));
770     qemu_fclose(fsave);
771 
772     QEMUFile *fload = open_test_file(false);
773     TestQtailq tgt;
774 
775     QTAILQ_INIT(&tgt.q);
776     vmstate_load_state(fload, &vmstate_q, &tgt, 1);
777     char eof = qemu_get_byte(fload);
778     g_assert(!qemu_file_get_error(fload));
779     g_assert_cmpint(tgt.i16, ==, obj_q.i16);
780     g_assert_cmpint(tgt.i32, ==, obj_q.i32);
781     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
782 
783     TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q);
784     TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q);
785     TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q);
786     TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q);
787 
788     while (1) {
789         g_assert_cmpint(qele_to->b, ==, qele_from->b);
790         g_assert_cmpint(qele_to->u8, ==, qele_from->u8);
791         if ((qele_from == qlast_from) || (qele_to == qlast_to)) {
792             break;
793         }
794         qele_from = QTAILQ_NEXT(qele_from, next);
795         qele_to = QTAILQ_NEXT(qele_to, next);
796     }
797 
798     g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
799     g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
800 
801     /* clean up */
802     TestQtailqElement *qele;
803     while (!QTAILQ_EMPTY(&tgt.q)) {
804         qele = QTAILQ_LAST(&tgt.q);
805         QTAILQ_REMOVE(&tgt.q, qele, next);
806         free(qele);
807         qele = NULL;
808     }
809     qemu_fclose(fload);
810 }
811 
812 /* interval (key) */
813 typedef struct TestGTreeInterval {
814     uint64_t low;
815     uint64_t high;
816 } TestGTreeInterval;
817 
818 #define VMSTATE_INTERVAL                               \
819 {                                                      \
820     .name = "interval",                                \
821     .version_id = 1,                                   \
822     .minimum_version_id = 1,                           \
823     .fields = (const VMStateField[]) {                 \
824         VMSTATE_UINT64(low, TestGTreeInterval),        \
825         VMSTATE_UINT64(high, TestGTreeInterval),       \
826         VMSTATE_END_OF_LIST()                          \
827     }                                                  \
828 }
829 
830 /* mapping (value) */
831 typedef struct TestGTreeMapping {
832     uint64_t phys_addr;
833     uint32_t flags;
834 } TestGTreeMapping;
835 
836 #define VMSTATE_MAPPING                               \
837 {                                                     \
838     .name = "mapping",                                \
839     .version_id = 1,                                  \
840     .minimum_version_id = 1,                          \
841     .fields = (const VMStateField[]) {                \
842         VMSTATE_UINT64(phys_addr, TestGTreeMapping),  \
843         VMSTATE_UINT32(flags, TestGTreeMapping),      \
844         VMSTATE_END_OF_LIST()                         \
845     },                                                \
846 }
847 
848 static const VMStateDescription vmstate_interval_mapping[2] = {
849     VMSTATE_MAPPING,   /* value */
850     VMSTATE_INTERVAL   /* key   */
851 };
852 
853 typedef struct TestGTreeDomain {
854     int32_t  id;
855     GTree    *mappings;
856 } TestGTreeDomain;
857 
858 typedef struct TestGTreeIOMMU {
859     int32_t  id;
860     GTree    *domains;
861 } TestGTreeIOMMU;
862 
863 /* Interval comparison function */
864 static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
865 {
866     TestGTreeInterval *inta = (TestGTreeInterval *)a;
867     TestGTreeInterval *intb = (TestGTreeInterval *)b;
868 
869     if (inta->high < intb->low) {
870         return -1;
871     } else if (intb->high < inta->low) {
872         return 1;
873     } else {
874         return 0;
875     }
876 }
877 
878 /* ID comparison function */
879 static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
880 {
881     guint ua = GPOINTER_TO_UINT(a);
882     guint ub = GPOINTER_TO_UINT(b);
883     return (ua > ub) - (ua < ub);
884 }
885 
886 static void destroy_domain(gpointer data)
887 {
888     TestGTreeDomain *domain = (TestGTreeDomain *)data;
889 
890     g_tree_destroy(domain->mappings);
891     g_free(domain);
892 }
893 
894 static int domain_preload(void *opaque)
895 {
896     TestGTreeDomain *domain = opaque;
897 
898     domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
899                                        NULL, g_free, g_free);
900     return 0;
901 }
902 
903 static int iommu_preload(void *opaque)
904 {
905     TestGTreeIOMMU *iommu = opaque;
906 
907     iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp,
908                                      NULL, NULL, destroy_domain);
909     return 0;
910 }
911 
912 static const VMStateDescription vmstate_domain = {
913     .name = "domain",
914     .version_id = 1,
915     .minimum_version_id = 1,
916     .pre_load = domain_preload,
917     .fields = (const VMStateField[]) {
918         VMSTATE_INT32(id, TestGTreeDomain),
919         VMSTATE_GTREE_V(mappings, TestGTreeDomain, 1,
920                         vmstate_interval_mapping,
921                         TestGTreeInterval, TestGTreeMapping),
922         VMSTATE_END_OF_LIST()
923     }
924 };
925 
926 /* test QLIST Migration */
927 
928 typedef struct TestQListElement {
929     uint32_t  id;
930     QLIST_ENTRY(TestQListElement) next;
931 } TestQListElement;
932 
933 typedef struct TestQListContainer {
934     uint32_t  id;
935     QLIST_HEAD(, TestQListElement) list;
936 } TestQListContainer;
937 
938 static const VMStateDescription vmstate_qlist_element = {
939     .name = "test/queue list",
940     .version_id = 1,
941     .minimum_version_id = 1,
942     .fields = (const VMStateField[]) {
943         VMSTATE_UINT32(id, TestQListElement),
944         VMSTATE_END_OF_LIST()
945     }
946 };
947 
948 static const VMStateDescription vmstate_iommu = {
949     .name = "iommu",
950     .version_id = 1,
951     .minimum_version_id = 1,
952     .pre_load = iommu_preload,
953     .fields = (const VMStateField[]) {
954         VMSTATE_INT32(id, TestGTreeIOMMU),
955         VMSTATE_GTREE_DIRECT_KEY_V(domains, TestGTreeIOMMU, 1,
956                                    &vmstate_domain, TestGTreeDomain),
957         VMSTATE_END_OF_LIST()
958     }
959 };
960 
961 static const VMStateDescription vmstate_container = {
962     .name = "test/container/qlist",
963     .version_id = 1,
964     .minimum_version_id = 1,
965     .fields = (const VMStateField[]) {
966         VMSTATE_UINT32(id, TestQListContainer),
967         VMSTATE_QLIST_V(list, TestQListContainer, 1, vmstate_qlist_element,
968                         TestQListElement, next),
969         VMSTATE_END_OF_LIST()
970     }
971 };
972 
973 uint8_t first_domain_dump[] = {
974     /* id */
975     0x00, 0x0, 0x0, 0x6,
976     0x00, 0x0, 0x0, 0x2, /* 2 mappings */
977     0x1, /* start of a */
978     /* a */
979     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
980     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
981     /* map_a */
982     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
983     0x00, 0x00, 0x00, 0x01,
984     0x1, /* start of b */
985     /* b */
986     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
987     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
988     /* map_b */
989     0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
990     0x00, 0x00, 0x00, 0x02,
991     0x0, /* end of gtree */
992     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
993 };
994 
995 static TestGTreeDomain *create_first_domain(void)
996 {
997     TestGTreeDomain *domain;
998     TestGTreeMapping *map_a, *map_b;
999     TestGTreeInterval *a, *b;
1000 
1001     domain = g_new0(TestGTreeDomain, 1);
1002     domain->id = 6;
1003 
1004     a = g_new0(TestGTreeInterval, 1);
1005     a->low = 0x1000;
1006     a->high = 0x1FFF;
1007 
1008     b = g_new0(TestGTreeInterval, 1);
1009     b->low = 0x4000;
1010     b->high = 0x4FFF;
1011 
1012     map_a = g_new0(TestGTreeMapping, 1);
1013     map_a->phys_addr = 0xa000;
1014     map_a->flags = 1;
1015 
1016     map_b = g_new0(TestGTreeMapping, 1);
1017     map_b->phys_addr = 0xe0000;
1018     map_b->flags = 2;
1019 
1020     domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp, NULL,
1021                                         (GDestroyNotify)g_free,
1022                                         (GDestroyNotify)g_free);
1023     g_tree_insert(domain->mappings, a, map_a);
1024     g_tree_insert(domain->mappings, b, map_b);
1025     return domain;
1026 }
1027 
1028 static void test_gtree_save_domain(void)
1029 {
1030     TestGTreeDomain *first_domain = create_first_domain();
1031 
1032     save_vmstate(&vmstate_domain, first_domain);
1033     compare_vmstate(first_domain_dump, sizeof(first_domain_dump));
1034     destroy_domain(first_domain);
1035 }
1036 
1037 struct match_node_data {
1038     GTree *tree;
1039     gpointer key;
1040     gpointer value;
1041 };
1042 
1043 struct tree_cmp_data {
1044     GTree *tree1;
1045     GTree *tree2;
1046     GTraverseFunc match_node;
1047 };
1048 
1049 static gboolean match_interval_mapping_node(gpointer key,
1050                                             gpointer value, gpointer data)
1051 {
1052     TestGTreeMapping *map_a, *map_b;
1053     TestGTreeInterval *a, *b;
1054     struct match_node_data *d = (struct match_node_data *)data;
1055     a = (TestGTreeInterval *)key;
1056     b = (TestGTreeInterval *)d->key;
1057 
1058     map_a = (TestGTreeMapping *)value;
1059     map_b = (TestGTreeMapping *)d->value;
1060 
1061     assert(a->low == b->low);
1062     assert(a->high == b->high);
1063     assert(map_a->phys_addr == map_b->phys_addr);
1064     assert(map_a->flags == map_b->flags);
1065     g_tree_remove(d->tree, key);
1066     return true;
1067 }
1068 
1069 static gboolean diff_tree(gpointer key, gpointer value, gpointer data)
1070 {
1071     struct tree_cmp_data *tp = (struct tree_cmp_data *)data;
1072     struct match_node_data d = {tp->tree2, key, value};
1073 
1074     g_tree_foreach(tp->tree2, tp->match_node, &d);
1075     return false;
1076 }
1077 
1078 static void compare_trees(GTree *tree1, GTree *tree2,
1079                           GTraverseFunc function)
1080 {
1081     struct tree_cmp_data tp = {tree1, tree2, function};
1082 
1083     assert(g_tree_nnodes(tree1) == g_tree_nnodes(tree2));
1084     g_tree_foreach(tree1, diff_tree, &tp);
1085     g_tree_destroy(g_tree_ref(tree1));
1086 }
1087 
1088 static void diff_domain(TestGTreeDomain *d1, TestGTreeDomain *d2)
1089 {
1090     assert(d1->id == d2->id);
1091     compare_trees(d1->mappings, d2->mappings, match_interval_mapping_node);
1092 }
1093 
1094 static gboolean match_domain_node(gpointer key, gpointer value, gpointer data)
1095 {
1096     uint64_t id1, id2;
1097     TestGTreeDomain *d1, *d2;
1098     struct match_node_data *d = (struct match_node_data *)data;
1099 
1100     id1 = (uint64_t)(uintptr_t)key;
1101     id2 = (uint64_t)(uintptr_t)d->key;
1102     d1 = (TestGTreeDomain *)value;
1103     d2 = (TestGTreeDomain *)d->value;
1104     assert(id1 == id2);
1105     diff_domain(d1, d2);
1106     g_tree_remove(d->tree, key);
1107     return true;
1108 }
1109 
1110 static void diff_iommu(TestGTreeIOMMU *iommu1, TestGTreeIOMMU *iommu2)
1111 {
1112     assert(iommu1->id == iommu2->id);
1113     compare_trees(iommu1->domains, iommu2->domains, match_domain_node);
1114 }
1115 
1116 static void test_gtree_load_domain(void)
1117 {
1118     TestGTreeDomain *dest_domain = g_new0(TestGTreeDomain, 1);
1119     TestGTreeDomain *orig_domain = create_first_domain();
1120     QEMUFile *fload, *fsave;
1121     char eof;
1122 
1123     fsave = open_test_file(true);
1124     qemu_put_buffer(fsave, first_domain_dump, sizeof(first_domain_dump));
1125     g_assert(!qemu_file_get_error(fsave));
1126     qemu_fclose(fsave);
1127 
1128     fload = open_test_file(false);
1129 
1130     vmstate_load_state(fload, &vmstate_domain, dest_domain, 1);
1131     eof = qemu_get_byte(fload);
1132     g_assert(!qemu_file_get_error(fload));
1133     g_assert_cmpint(orig_domain->id, ==, dest_domain->id);
1134     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
1135 
1136     diff_domain(orig_domain, dest_domain);
1137     destroy_domain(orig_domain);
1138     destroy_domain(dest_domain);
1139     qemu_fclose(fload);
1140 }
1141 
1142 uint8_t iommu_dump[] = {
1143     /* iommu id */
1144     0x00, 0x0, 0x0, 0x7,
1145     0x00, 0x0, 0x0, 0x2, /* 2 domains */
1146     0x1,/* start of domain 5 */
1147         0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x5, /* key = 5 */
1148         0x00, 0x0, 0x0, 0x5, /* domain1 id */
1149         0x00, 0x0, 0x0, 0x1, /* 1 mapping */
1150         0x1, /* start of mappings */
1151             /* c */
1152             0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1153             0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF,
1154             /* map_c */
1155             0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00,
1156             0x00, 0x0, 0x0, 0x3,
1157             0x0, /* end of domain1 mappings*/
1158     0x1,/* start of domain 6 */
1159         0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x6, /* key = 6 */
1160         0x00, 0x0, 0x0, 0x6, /* domain6 id */
1161             0x00, 0x0, 0x0, 0x2, /* 2 mappings */
1162             0x1, /* start of a */
1163             /* a */
1164             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
1165             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
1166             /* map_a */
1167             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
1168             0x00, 0x00, 0x00, 0x01,
1169             0x1, /* start of b */
1170             /* b */
1171             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
1172             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
1173             /* map_b */
1174             0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
1175             0x00, 0x00, 0x00, 0x02,
1176             0x0, /* end of domain6 mappings*/
1177     0x0, /* end of domains */
1178     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
1179 };
1180 
1181 static TestGTreeIOMMU *create_iommu(void)
1182 {
1183     TestGTreeIOMMU *iommu = g_new0(TestGTreeIOMMU, 1);
1184     TestGTreeDomain *first_domain = create_first_domain();
1185     TestGTreeDomain *second_domain;
1186     TestGTreeMapping *map_c;
1187     TestGTreeInterval *c;
1188 
1189     iommu->id = 7;
1190     iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp, NULL,
1191                                      NULL,
1192                                      destroy_domain);
1193 
1194     second_domain = g_new0(TestGTreeDomain, 1);
1195     second_domain->id = 5;
1196     second_domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
1197                                               NULL,
1198                                               (GDestroyNotify)g_free,
1199                                               (GDestroyNotify)g_free);
1200 
1201     g_tree_insert(iommu->domains, GUINT_TO_POINTER(6), first_domain);
1202     g_tree_insert(iommu->domains, (gpointer)0x0000000000000005, second_domain);
1203 
1204     c = g_new0(TestGTreeInterval, 1);
1205     c->low = 0x1000000;
1206     c->high = 0x1FFFFFF;
1207 
1208     map_c = g_new0(TestGTreeMapping, 1);
1209     map_c->phys_addr = 0xF000000;
1210     map_c->flags = 0x3;
1211 
1212     g_tree_insert(second_domain->mappings, c, map_c);
1213     return iommu;
1214 }
1215 
1216 static void destroy_iommu(TestGTreeIOMMU *iommu)
1217 {
1218     g_tree_destroy(iommu->domains);
1219     g_free(iommu);
1220 }
1221 
1222 static void test_gtree_save_iommu(void)
1223 {
1224     TestGTreeIOMMU *iommu = create_iommu();
1225 
1226     save_vmstate(&vmstate_iommu, iommu);
1227     compare_vmstate(iommu_dump, sizeof(iommu_dump));
1228     destroy_iommu(iommu);
1229 }
1230 
1231 static void test_gtree_load_iommu(void)
1232 {
1233     TestGTreeIOMMU *dest_iommu = g_new0(TestGTreeIOMMU, 1);
1234     TestGTreeIOMMU *orig_iommu = create_iommu();
1235     QEMUFile *fsave, *fload;
1236     char eof;
1237 
1238     fsave = open_test_file(true);
1239     qemu_put_buffer(fsave, iommu_dump, sizeof(iommu_dump));
1240     g_assert(!qemu_file_get_error(fsave));
1241     qemu_fclose(fsave);
1242 
1243     fload = open_test_file(false);
1244     vmstate_load_state(fload, &vmstate_iommu, dest_iommu, 1);
1245     eof = qemu_get_byte(fload);
1246     g_assert(!qemu_file_get_error(fload));
1247     g_assert_cmpint(orig_iommu->id, ==, dest_iommu->id);
1248     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
1249 
1250     diff_iommu(orig_iommu, dest_iommu);
1251     destroy_iommu(orig_iommu);
1252     destroy_iommu(dest_iommu);
1253     qemu_fclose(fload);
1254 }
1255 
1256 static uint8_t qlist_dump[] = {
1257     0x00, 0x00, 0x00, 0x01, /* container id */
1258     0x1, /* start of a */
1259     0x00, 0x00, 0x00, 0x0a,
1260     0x1, /* start of b */
1261     0x00, 0x00, 0x0b, 0x00,
1262     0x1, /* start of c */
1263     0x00, 0x0c, 0x00, 0x00,
1264     0x1, /* start of d */
1265     0x0d, 0x00, 0x00, 0x00,
1266     0x0, /* end of list */
1267     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
1268 };
1269 
1270 static TestQListContainer *alloc_container(void)
1271 {
1272     TestQListElement *a = g_new(TestQListElement, 1);
1273     TestQListElement *b = g_new(TestQListElement, 1);
1274     TestQListElement *c = g_new(TestQListElement, 1);
1275     TestQListElement *d = g_new(TestQListElement, 1);
1276     TestQListContainer *container = g_new(TestQListContainer, 1);
1277 
1278     a->id = 0x0a;
1279     b->id = 0x0b00;
1280     c->id = 0xc0000;
1281     d->id = 0xd000000;
1282     container->id = 1;
1283 
1284     QLIST_INIT(&container->list);
1285     QLIST_INSERT_HEAD(&container->list, d, next);
1286     QLIST_INSERT_HEAD(&container->list, c, next);
1287     QLIST_INSERT_HEAD(&container->list, b, next);
1288     QLIST_INSERT_HEAD(&container->list, a, next);
1289     return container;
1290 }
1291 
1292 static void free_container(TestQListContainer *container)
1293 {
1294     TestQListElement *iter, *tmp;
1295 
1296     QLIST_FOREACH_SAFE(iter, &container->list, next, tmp) {
1297         QLIST_REMOVE(iter, next);
1298         g_free(iter);
1299     }
1300     g_free(container);
1301 }
1302 
1303 static void compare_containers(TestQListContainer *c1, TestQListContainer *c2)
1304 {
1305     TestQListElement *first_item_c1, *first_item_c2;
1306 
1307     while (!QLIST_EMPTY(&c1->list)) {
1308         first_item_c1 = QLIST_FIRST(&c1->list);
1309         first_item_c2 = QLIST_FIRST(&c2->list);
1310         assert(first_item_c2);
1311         assert(first_item_c1->id == first_item_c2->id);
1312         QLIST_REMOVE(first_item_c1, next);
1313         QLIST_REMOVE(first_item_c2, next);
1314         g_free(first_item_c1);
1315         g_free(first_item_c2);
1316     }
1317     assert(QLIST_EMPTY(&c2->list));
1318 }
1319 
1320 /*
1321  * Check the prev & next fields are correct by doing list
1322  * manipulations on the container. We will do that for both
1323  * the source and the destination containers
1324  */
1325 static void manipulate_container(TestQListContainer *c)
1326 {
1327      TestQListElement *prev = NULL, *iter = QLIST_FIRST(&c->list);
1328      TestQListElement *elem;
1329 
1330      elem = g_new(TestQListElement, 1);
1331      elem->id = 0x12;
1332      QLIST_INSERT_AFTER(iter, elem, next);
1333 
1334      elem = g_new(TestQListElement, 1);
1335      elem->id = 0x13;
1336      QLIST_INSERT_HEAD(&c->list, elem, next);
1337 
1338      while (iter) {
1339         prev = iter;
1340         iter = QLIST_NEXT(iter, next);
1341      }
1342 
1343      elem = g_new(TestQListElement, 1);
1344      elem->id = 0x14;
1345      QLIST_INSERT_BEFORE(prev, elem, next);
1346 
1347      elem = g_new(TestQListElement, 1);
1348      elem->id = 0x15;
1349      QLIST_INSERT_AFTER(prev, elem, next);
1350 
1351      QLIST_REMOVE(prev, next);
1352      g_free(prev);
1353 }
1354 
1355 static void test_save_qlist(void)
1356 {
1357     TestQListContainer *container = alloc_container();
1358 
1359     save_vmstate(&vmstate_container, container);
1360     compare_vmstate(qlist_dump, sizeof(qlist_dump));
1361     free_container(container);
1362 }
1363 
1364 static void test_load_qlist(void)
1365 {
1366     QEMUFile *fsave, *fload;
1367     TestQListContainer *orig_container = alloc_container();
1368     TestQListContainer *dest_container = g_new0(TestQListContainer, 1);
1369     char eof;
1370 
1371     QLIST_INIT(&dest_container->list);
1372 
1373     fsave = open_test_file(true);
1374     qemu_put_buffer(fsave, qlist_dump, sizeof(qlist_dump));
1375     g_assert(!qemu_file_get_error(fsave));
1376     qemu_fclose(fsave);
1377 
1378     fload = open_test_file(false);
1379     vmstate_load_state(fload, &vmstate_container, dest_container, 1);
1380     eof = qemu_get_byte(fload);
1381     g_assert(!qemu_file_get_error(fload));
1382     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
1383     manipulate_container(orig_container);
1384     manipulate_container(dest_container);
1385     compare_containers(orig_container, dest_container);
1386     free_container(orig_container);
1387     free_container(dest_container);
1388     qemu_fclose(fload);
1389 }
1390 
1391 typedef struct TmpTestStruct {
1392     TestStruct *parent;
1393     int64_t diff;
1394 } TmpTestStruct;
1395 
1396 static int tmp_child_pre_save(void *opaque)
1397 {
1398     struct TmpTestStruct *tts = opaque;
1399 
1400     tts->diff = tts->parent->b - tts->parent->a;
1401 
1402     return 0;
1403 }
1404 
1405 static int tmp_child_post_load(void *opaque, int version_id)
1406 {
1407     struct TmpTestStruct *tts = opaque;
1408 
1409     tts->parent->b = tts->parent->a + tts->diff;
1410 
1411     return 0;
1412 }
1413 
1414 static const VMStateDescription vmstate_tmp_back_to_parent = {
1415     .name = "test/tmp_child_parent",
1416     .fields = (const VMStateField[]) {
1417         VMSTATE_UINT64(f, TestStruct),
1418         VMSTATE_END_OF_LIST()
1419     }
1420 };
1421 
1422 static const VMStateDescription vmstate_tmp_child = {
1423     .name = "test/tmp_child",
1424     .pre_save = tmp_child_pre_save,
1425     .post_load = tmp_child_post_load,
1426     .fields = (const VMStateField[]) {
1427         VMSTATE_INT64(diff, TmpTestStruct),
1428         VMSTATE_STRUCT_POINTER(parent, TmpTestStruct,
1429                                vmstate_tmp_back_to_parent, TestStruct),
1430         VMSTATE_END_OF_LIST()
1431     }
1432 };
1433 
1434 static const VMStateDescription vmstate_with_tmp = {
1435     .name = "test/with_tmp",
1436     .version_id = 1,
1437     .fields = (const VMStateField[]) {
1438         VMSTATE_UINT32(a, TestStruct),
1439         VMSTATE_UINT64(d, TestStruct),
1440         VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child),
1441         VMSTATE_END_OF_LIST()
1442     }
1443 };
1444 
1445 static void obj_tmp_copy(void *target, void *source)
1446 {
1447     memcpy(target, source, sizeof(TestStruct));
1448 }
1449 
1450 static void test_tmp_struct(void)
1451 {
1452     TestStruct obj, obj_clone;
1453 
1454     uint8_t const wire_with_tmp[] = {
1455         /* u32 a */ 0x00, 0x00, 0x00, 0x02,
1456         /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1457         /* diff  */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
1458         /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
1459         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
1460     };
1461 
1462     memset(&obj, 0, sizeof(obj));
1463     obj.a = 2;
1464     obj.b = 4;
1465     obj.d = 1;
1466     obj.f = 8;
1467     save_vmstate(&vmstate_with_tmp, &obj);
1468 
1469     compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
1470 
1471     memset(&obj, 0, sizeof(obj));
1472     SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone,
1473                          obj_tmp_copy, 1, wire_with_tmp,
1474                          sizeof(wire_with_tmp)));
1475     g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */
1476     g_assert_cmpint(obj.b, ==, 4); /* from the post_load */
1477     g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */
1478     g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
1479 }
1480 
1481 int main(int argc, char **argv)
1482 {
1483     g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
1484                                                  g_get_tmp_dir());
1485     temp_fd = mkstemp(temp_file);
1486     g_assert(temp_fd >= 0);
1487 
1488     module_call_init(MODULE_INIT_QOM);
1489 
1490     g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1491 
1492     g_test_init(&argc, &argv, NULL);
1493     g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
1494     g_test_add_func("/vmstate/simple/array", test_simple_array);
1495     g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
1496     g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
1497     g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
1498     g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
1499     g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
1500     g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
1501     g_test_add_func("/vmstate/array/ptr/str/no0/save",
1502                     test_arr_ptr_str_no0_save);
1503     g_test_add_func("/vmstate/array/ptr/str/no0/load",
1504                     test_arr_ptr_str_no0_load);
1505     g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save);
1506     g_test_add_func("/vmstate/array/ptr/str/0/load",
1507                     test_arr_ptr_str_0_load);
1508     g_test_add_func("/vmstate/array/ptr/prim/0/save",
1509                     test_arr_ptr_prim_0_save);
1510     g_test_add_func("/vmstate/array/ptr/prim/0/load",
1511                     test_arr_ptr_prim_0_load);
1512     g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q);
1513     g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q);
1514     g_test_add_func("/vmstate/gtree/save/savedomain", test_gtree_save_domain);
1515     g_test_add_func("/vmstate/gtree/load/loaddomain", test_gtree_load_domain);
1516     g_test_add_func("/vmstate/gtree/save/saveiommu", test_gtree_save_iommu);
1517     g_test_add_func("/vmstate/gtree/load/loadiommu", test_gtree_load_iommu);
1518     g_test_add_func("/vmstate/qlist/save/saveqlist", test_save_qlist);
1519     g_test_add_func("/vmstate/qlist/load/loadqlist", test_load_qlist);
1520     g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
1521     g_test_run();
1522 
1523     close(temp_fd);
1524     unlink(temp_file);
1525 
1526     return 0;
1527 }
1528