2 * Test code for VMState
4 * Copyright (c) 2013 Red Hat Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "migration/migration.h"
29 #include "migration/vmstate.h"
30 #include "migration/qemu-file-types.h"
31 #include "../migration/qemu-file.h"
32 #include "../migration/qemu-file-channel.h"
33 #include "qemu/coroutine.h"
34 #include "io/channel-file.h"
36 static char temp_file
[] = "/tmp/vmst.test.XXXXXX";
40 /* Duplicate temp_fd and seek to the beginning of the file */
41 static QEMUFile
*open_test_file(bool write
)
43 int fd
= dup(temp_fd
);
47 lseek(fd
, 0, SEEK_SET
);
49 g_assert_cmpint(ftruncate(fd
, 0), ==, 0);
51 ioc
= QIO_CHANNEL(qio_channel_file_new_fd(fd
));
53 f
= qemu_fopen_channel_output(ioc
);
55 f
= qemu_fopen_channel_input(ioc
);
57 object_unref(OBJECT(ioc
));
61 #define SUCCESS(val) \
62 g_assert_cmpint((val), ==, 0)
64 #define FAILURE(val) \
65 g_assert_cmpint((val), !=, 0)
67 static void save_vmstate(const VMStateDescription
*desc
, void *obj
)
69 QEMUFile
*f
= open_test_file(true);
71 /* Save file with vmstate */
72 vmstate_save_state(f
, desc
, obj
, NULL
);
73 qemu_put_byte(f
, QEMU_VM_EOF
);
74 g_assert(!qemu_file_get_error(f
));
78 static void save_buffer(const uint8_t *buf
, size_t buf_size
)
80 QEMUFile
*fsave
= open_test_file(true);
81 qemu_put_buffer(fsave
, buf
, buf_size
);
85 static void compare_vmstate(const uint8_t *wire
, size_t size
)
87 QEMUFile
*f
= open_test_file(false);
90 /* read back as binary */
92 g_assert_cmpint(qemu_get_buffer(f
, result
, sizeof(result
)), ==,
94 g_assert(!qemu_file_get_error(f
));
96 /* Compare that what is on the file is the same that what we
97 expected to be there */
98 SUCCESS(memcmp(result
, wire
, sizeof(result
)));
102 g_assert_cmpint(qemu_file_get_error(f
), ==, -EIO
);
107 static int load_vmstate_one(const VMStateDescription
*desc
, void *obj
,
108 int version
, const uint8_t *wire
, size_t size
)
113 f
= open_test_file(true);
114 qemu_put_buffer(f
, wire
, size
);
117 f
= open_test_file(false);
118 ret
= vmstate_load_state(f
, desc
, obj
, version
);
120 g_assert(qemu_file_get_error(f
));
122 g_assert(!qemu_file_get_error(f
));
129 static int load_vmstate(const VMStateDescription
*desc
,
130 void *obj
, void *obj_clone
,
131 void (*obj_copy
)(void *, void*),
132 int version
, const uint8_t *wire
, size_t size
)
134 /* We test with zero size */
135 obj_copy(obj_clone
, obj
);
136 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, 0));
138 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
139 * able to test in the middle */
143 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
144 obj_copy(obj
, obj_clone
);
145 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
- 2));
147 /* Test with size/2, first half of real state */
148 obj_copy(obj
, obj_clone
);
149 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
/2));
151 /* Test with size/2, second half of real state */
152 obj_copy(obj
, obj_clone
);
153 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
+ (size
/2), size
/2));
156 obj_copy(obj
, obj_clone
);
157 return load_vmstate_one(desc
, obj
, version
, wire
, size
);
160 /* Test struct that we are going to use for our tests */
162 typedef struct TestSimple
{
169 int16_t i16_1
, i16_2
;
170 int32_t i32_1
, i32_2
;
171 int64_t i64_1
, i64_2
;
174 /* Object instantiation, we are going to use it in more than one test */
176 TestSimple obj_simple
= {
193 /* Description of the values. If you add a primitive type
194 you are expected to add a test here */
196 static const VMStateDescription vmstate_simple_primitive
= {
197 .name
= "simple/primitive",
199 .minimum_version_id
= 1,
200 .fields
= (VMStateField
[]) {
201 VMSTATE_BOOL(b_1
, TestSimple
),
202 VMSTATE_BOOL(b_2
, TestSimple
),
203 VMSTATE_UINT8(u8_1
, TestSimple
),
204 VMSTATE_UINT16(u16_1
, TestSimple
),
205 VMSTATE_UINT32(u32_1
, TestSimple
),
206 VMSTATE_UINT64(u64_1
, TestSimple
),
207 VMSTATE_INT8(i8_1
, TestSimple
),
208 VMSTATE_INT8(i8_2
, TestSimple
),
209 VMSTATE_INT16(i16_1
, TestSimple
),
210 VMSTATE_INT16(i16_2
, TestSimple
),
211 VMSTATE_INT32(i32_1
, TestSimple
),
212 VMSTATE_INT32(i32_2
, TestSimple
),
213 VMSTATE_INT64(i64_1
, TestSimple
),
214 VMSTATE_INT64(i64_2
, TestSimple
),
215 VMSTATE_END_OF_LIST()
219 /* It describes what goes through the wire. Our tests are basically:
222 - save a struct a vmstate to a file
223 - read that file back (binary read, no vmstate)
224 - compare it with what we expect to be on the wire
226 - save to the file what we expect to be on the wire
227 - read struct back with vmstate in a different
228 - compare back with the original struct
231 uint8_t wire_simple_primitive
[] = {
235 /* u16_1 */ 0x02, 0x00,
236 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
237 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
240 /* i16_1 */ 0x02, 0x00,
241 /* i16_2 */ 0xfe, 0x0,
242 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
243 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
244 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
245 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
246 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
249 static void obj_simple_copy(void *target
, void *source
)
251 memcpy(target
, source
, sizeof(TestSimple
));
254 static void test_simple_primitive(void)
256 TestSimple obj
, obj_clone
;
258 memset(&obj
, 0, sizeof(obj
));
259 save_vmstate(&vmstate_simple_primitive
, &obj_simple
);
261 compare_vmstate(wire_simple_primitive
, sizeof(wire_simple_primitive
));
263 SUCCESS(load_vmstate(&vmstate_simple_primitive
, &obj
, &obj_clone
,
264 obj_simple_copy
, 1, wire_simple_primitive
,
265 sizeof(wire_simple_primitive
)));
267 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
285 typedef struct TestStruct
{
291 static const VMStateDescription vmstate_versioned
= {
292 .name
= "test/versioned",
294 .minimum_version_id
= 1,
295 .fields
= (VMStateField
[]) {
296 VMSTATE_UINT32(a
, TestStruct
),
297 VMSTATE_UINT32_V(b
, TestStruct
, 2), /* Versioned field in the middle, so
298 * we catch bugs more easily.
300 VMSTATE_UINT32(c
, TestStruct
),
301 VMSTATE_UINT64(d
, TestStruct
),
302 VMSTATE_UINT32_V(e
, TestStruct
, 2),
303 VMSTATE_UINT64_V(f
, TestStruct
, 2),
304 VMSTATE_END_OF_LIST()
308 static void test_load_v1(void)
313 0, 0, 0, 0, 0, 0, 0, 40, /* d */
314 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
316 save_buffer(buf
, sizeof(buf
));
318 QEMUFile
*loading
= open_test_file(false);
319 TestStruct obj
= { .b
= 200, .e
= 500, .f
= 600 };
320 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 1);
321 g_assert(!qemu_file_get_error(loading
));
322 g_assert_cmpint(obj
.a
, ==, 10);
323 g_assert_cmpint(obj
.b
, ==, 200);
324 g_assert_cmpint(obj
.c
, ==, 30);
325 g_assert_cmpint(obj
.d
, ==, 40);
326 g_assert_cmpint(obj
.e
, ==, 500);
327 g_assert_cmpint(obj
.f
, ==, 600);
328 qemu_fclose(loading
);
331 static void test_load_v2(void)
337 0, 0, 0, 0, 0, 0, 0, 40, /* d */
339 0, 0, 0, 0, 0, 0, 0, 60, /* f */
340 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
342 save_buffer(buf
, sizeof(buf
));
344 QEMUFile
*loading
= open_test_file(false);
346 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 2);
347 g_assert_cmpint(obj
.a
, ==, 10);
348 g_assert_cmpint(obj
.b
, ==, 20);
349 g_assert_cmpint(obj
.c
, ==, 30);
350 g_assert_cmpint(obj
.d
, ==, 40);
351 g_assert_cmpint(obj
.e
, ==, 50);
352 g_assert_cmpint(obj
.f
, ==, 60);
353 qemu_fclose(loading
);
356 static bool test_skip(void *opaque
, int version_id
)
358 TestStruct
*t
= (TestStruct
*)opaque
;
362 static const VMStateDescription vmstate_skipping
= {
365 .minimum_version_id
= 1,
366 .fields
= (VMStateField
[]) {
367 VMSTATE_UINT32(a
, TestStruct
),
368 VMSTATE_UINT32(b
, TestStruct
),
369 VMSTATE_UINT32_TEST(c
, TestStruct
, test_skip
),
370 VMSTATE_UINT64(d
, TestStruct
),
371 VMSTATE_UINT32_TEST(e
, TestStruct
, test_skip
),
372 VMSTATE_UINT64_V(f
, TestStruct
, 2),
373 VMSTATE_END_OF_LIST()
378 static void test_save_noskip(void)
380 QEMUFile
*fsave
= open_test_file(true);
381 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
383 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
384 g_assert(!qemu_file_get_error(fsave
));
386 uint8_t expected
[] = {
390 0, 0, 0, 0, 0, 0, 0, 4, /* d */
392 0, 0, 0, 0, 0, 0, 0, 6, /* f */
396 compare_vmstate(expected
, sizeof(expected
));
399 static void test_save_skip(void)
401 QEMUFile
*fsave
= open_test_file(true);
402 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
404 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
405 g_assert(!qemu_file_get_error(fsave
));
407 uint8_t expected
[] = {
410 0, 0, 0, 0, 0, 0, 0, 4, /* d */
411 0, 0, 0, 0, 0, 0, 0, 6, /* f */
415 compare_vmstate(expected
, sizeof(expected
));
418 static void test_load_noskip(void)
424 0, 0, 0, 0, 0, 0, 0, 40, /* d */
426 0, 0, 0, 0, 0, 0, 0, 60, /* f */
427 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
429 save_buffer(buf
, sizeof(buf
));
431 QEMUFile
*loading
= open_test_file(false);
432 TestStruct obj
= { .skip_c_e
= false };
433 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
434 g_assert(!qemu_file_get_error(loading
));
435 g_assert_cmpint(obj
.a
, ==, 10);
436 g_assert_cmpint(obj
.b
, ==, 20);
437 g_assert_cmpint(obj
.c
, ==, 30);
438 g_assert_cmpint(obj
.d
, ==, 40);
439 g_assert_cmpint(obj
.e
, ==, 50);
440 g_assert_cmpint(obj
.f
, ==, 60);
441 qemu_fclose(loading
);
444 static void test_load_skip(void)
449 0, 0, 0, 0, 0, 0, 0, 40, /* d */
450 0, 0, 0, 0, 0, 0, 0, 60, /* f */
451 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
453 save_buffer(buf
, sizeof(buf
));
455 QEMUFile
*loading
= open_test_file(false);
456 TestStruct obj
= { .skip_c_e
= true, .c
= 300, .e
= 500 };
457 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
458 g_assert(!qemu_file_get_error(loading
));
459 g_assert_cmpint(obj
.a
, ==, 10);
460 g_assert_cmpint(obj
.b
, ==, 20);
461 g_assert_cmpint(obj
.c
, ==, 300);
462 g_assert_cmpint(obj
.d
, ==, 40);
463 g_assert_cmpint(obj
.e
, ==, 500);
464 g_assert_cmpint(obj
.f
, ==, 60);
465 qemu_fclose(loading
);
472 const VMStateDescription vmsd_tst
= {
475 .minimum_version_id
= 1,
476 .fields
= (VMStateField
[]) {
477 VMSTATE_INT32(i
, TestStructTriv
),
478 VMSTATE_END_OF_LIST()
482 /* test array migration */
487 TestStructTriv
*ar
[AR_SIZE
];
488 } TestArrayOfPtrToStuct
;
490 const VMStateDescription vmsd_arps
= {
493 .minimum_version_id
= 1,
494 .fields
= (VMStateField
[]) {
495 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar
, TestArrayOfPtrToStuct
,
496 AR_SIZE
, 0, vmsd_tst
, TestStructTriv
),
497 VMSTATE_END_OF_LIST()
501 static uint8_t wire_arr_ptr_no0
[] = {
502 0x00, 0x00, 0x00, 0x00,
503 0x00, 0x00, 0x00, 0x01,
504 0x00, 0x00, 0x00, 0x02,
505 0x00, 0x00, 0x00, 0x03,
509 static void test_arr_ptr_str_no0_save(void)
511 TestStructTriv ar
[AR_SIZE
] = {{.i
= 0}, {.i
= 1}, {.i
= 2}, {.i
= 3} };
512 TestArrayOfPtrToStuct sample
= {.ar
= {&ar
[0], &ar
[1], &ar
[2], &ar
[3]} };
514 save_vmstate(&vmsd_arps
, &sample
);
515 compare_vmstate(wire_arr_ptr_no0
, sizeof(wire_arr_ptr_no0
));
518 static void test_arr_ptr_str_no0_load(void)
520 TestStructTriv ar_gt
[AR_SIZE
] = {{.i
= 0}, {.i
= 1}, {.i
= 2}, {.i
= 3} };
521 TestStructTriv ar
[AR_SIZE
] = {};
522 TestArrayOfPtrToStuct obj
= {.ar
= {&ar
[0], &ar
[1], &ar
[2], &ar
[3]} };
525 save_buffer(wire_arr_ptr_no0
, sizeof(wire_arr_ptr_no0
));
526 SUCCESS(load_vmstate_one(&vmsd_arps
, &obj
, 1,
527 wire_arr_ptr_no0
, sizeof(wire_arr_ptr_no0
)));
528 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
529 /* compare the target array ar with the ground truth array ar_gt */
530 g_assert_cmpint(ar_gt
[idx
].i
, ==, ar
[idx
].i
);
534 static uint8_t wire_arr_ptr_0
[] = {
535 0x00, 0x00, 0x00, 0x00,
537 0x00, 0x00, 0x00, 0x02,
538 0x00, 0x00, 0x00, 0x03,
542 static void test_arr_ptr_str_0_save(void)
544 TestStructTriv ar
[AR_SIZE
] = {{.i
= 0}, {.i
= 1}, {.i
= 2}, {.i
= 3} };
545 TestArrayOfPtrToStuct sample
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
547 save_vmstate(&vmsd_arps
, &sample
);
548 compare_vmstate(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
551 static void test_arr_ptr_str_0_load(void)
553 TestStructTriv ar_gt
[AR_SIZE
] = {{.i
= 0}, {.i
= 0}, {.i
= 2}, {.i
= 3} };
554 TestStructTriv ar
[AR_SIZE
] = {};
555 TestArrayOfPtrToStuct obj
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
558 save_buffer(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
559 SUCCESS(load_vmstate_one(&vmsd_arps
, &obj
, 1,
560 wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
)));
561 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
562 /* compare the target array ar with the ground truth array ar_gt */
563 g_assert_cmpint(ar_gt
[idx
].i
, ==, ar
[idx
].i
);
565 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
567 g_assert_cmpint((uintptr_t)(obj
.ar
[idx
]), ==, 0);
569 g_assert_cmpint((uintptr_t)(obj
.ar
[idx
]), !=, 0);
574 typedef struct TestArrayOfPtrToInt
{
575 int32_t *ar
[AR_SIZE
];
576 } TestArrayOfPtrToInt
;
578 const VMStateDescription vmsd_arpp
= {
581 .minimum_version_id
= 1,
582 .fields
= (VMStateField
[]) {
583 VMSTATE_ARRAY_OF_POINTER(ar
, TestArrayOfPtrToInt
,
584 AR_SIZE
, 0, vmstate_info_int32
, int32_t*),
585 VMSTATE_END_OF_LIST()
589 static void test_arr_ptr_prim_0_save(void)
591 int32_t ar
[AR_SIZE
] = {0 , 1, 2, 3};
592 TestArrayOfPtrToInt sample
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
594 save_vmstate(&vmsd_arpp
, &sample
);
595 compare_vmstate(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
598 static void test_arr_ptr_prim_0_load(void)
600 int32_t ar_gt
[AR_SIZE
] = {0, 1, 2, 3};
601 int32_t ar
[AR_SIZE
] = {3 , 42, 1, 0};
602 TestArrayOfPtrToInt obj
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
605 save_buffer(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
606 SUCCESS(load_vmstate_one(&vmsd_arpp
, &obj
, 1,
607 wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
)));
608 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
609 /* compare the target array ar with the ground truth array ar_gt */
611 g_assert_cmpint(42, ==, ar
[idx
]);
613 g_assert_cmpint(ar_gt
[idx
], ==, ar
[idx
]);
618 /* test QTAILQ migration */
619 typedef struct TestQtailqElement TestQtailqElement
;
621 struct TestQtailqElement
{
624 QTAILQ_ENTRY(TestQtailqElement
) next
;
627 typedef struct TestQtailq
{
629 QTAILQ_HEAD(TestQtailqHead
, TestQtailqElement
) q
;
633 static const VMStateDescription vmstate_q_element
= {
634 .name
= "test/queue-element",
636 .minimum_version_id
= 1,
637 .fields
= (VMStateField
[]) {
638 VMSTATE_BOOL(b
, TestQtailqElement
),
639 VMSTATE_UINT8(u8
, TestQtailqElement
),
640 VMSTATE_END_OF_LIST()
644 static const VMStateDescription vmstate_q
= {
645 .name
= "test/queue",
647 .minimum_version_id
= 1,
648 .fields
= (VMStateField
[]) {
649 VMSTATE_INT16(i16
, TestQtailq
),
650 VMSTATE_QTAILQ_V(q
, TestQtailq
, 1, vmstate_q_element
, TestQtailqElement
,
652 VMSTATE_INT32(i32
, TestQtailq
),
653 VMSTATE_END_OF_LIST()
659 /* start of element 0 of q */ 0x01,
662 /* start of element 1 of q */ 0x01,
666 /* i32 */ 0x00, 0x01, 0x11, 0x70,
667 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
670 static void test_save_q(void)
677 TestQtailqElement obj_qe1
= {
682 TestQtailqElement obj_qe2
= {
687 QTAILQ_INIT(&obj_q
.q
);
688 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe1
, next
);
689 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe2
, next
);
691 save_vmstate(&vmstate_q
, &obj_q
);
692 compare_vmstate(wire_q
, sizeof(wire_q
));
695 static void test_load_q(void)
702 TestQtailqElement obj_qe1
= {
707 TestQtailqElement obj_qe2
= {
712 QTAILQ_INIT(&obj_q
.q
);
713 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe1
, next
);
714 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe2
, next
);
716 QEMUFile
*fsave
= open_test_file(true);
718 qemu_put_buffer(fsave
, wire_q
, sizeof(wire_q
));
719 g_assert(!qemu_file_get_error(fsave
));
722 QEMUFile
*fload
= open_test_file(false);
726 vmstate_load_state(fload
, &vmstate_q
, &tgt
, 1);
727 char eof
= qemu_get_byte(fload
);
728 g_assert(!qemu_file_get_error(fload
));
729 g_assert_cmpint(tgt
.i16
, ==, obj_q
.i16
);
730 g_assert_cmpint(tgt
.i32
, ==, obj_q
.i32
);
731 g_assert_cmpint(eof
, ==, QEMU_VM_EOF
);
733 TestQtailqElement
*qele_from
= QTAILQ_FIRST(&obj_q
.q
);
734 TestQtailqElement
*qlast_from
= QTAILQ_LAST(&obj_q
.q
, TestQtailqHead
);
735 TestQtailqElement
*qele_to
= QTAILQ_FIRST(&tgt
.q
);
736 TestQtailqElement
*qlast_to
= QTAILQ_LAST(&tgt
.q
, TestQtailqHead
);
739 g_assert_cmpint(qele_to
->b
, ==, qele_from
->b
);
740 g_assert_cmpint(qele_to
->u8
, ==, qele_from
->u8
);
741 if ((qele_from
== qlast_from
) || (qele_to
== qlast_to
)) {
744 qele_from
= QTAILQ_NEXT(qele_from
, next
);
745 qele_to
= QTAILQ_NEXT(qele_to
, next
);
748 g_assert_cmpint((uintptr_t) qele_from
, ==, (uintptr_t) qlast_from
);
749 g_assert_cmpint((uintptr_t) qele_to
, ==, (uintptr_t) qlast_to
);
752 TestQtailqElement
*qele
;
753 while (!QTAILQ_EMPTY(&tgt
.q
)) {
754 qele
= QTAILQ_LAST(&tgt
.q
, TestQtailqHead
);
755 QTAILQ_REMOVE(&tgt
.q
, qele
, next
);
762 typedef struct TmpTestStruct
{
767 static void tmp_child_pre_save(void *opaque
)
769 struct TmpTestStruct
*tts
= opaque
;
771 tts
->diff
= tts
->parent
->b
- tts
->parent
->a
;
774 static int tmp_child_post_load(void *opaque
, int version_id
)
776 struct TmpTestStruct
*tts
= opaque
;
778 tts
->parent
->b
= tts
->parent
->a
+ tts
->diff
;
783 static const VMStateDescription vmstate_tmp_back_to_parent
= {
784 .name
= "test/tmp_child_parent",
785 .fields
= (VMStateField
[]) {
786 VMSTATE_UINT64(f
, TestStruct
),
787 VMSTATE_END_OF_LIST()
791 static const VMStateDescription vmstate_tmp_child
= {
792 .name
= "test/tmp_child",
793 .pre_save
= tmp_child_pre_save
,
794 .post_load
= tmp_child_post_load
,
795 .fields
= (VMStateField
[]) {
796 VMSTATE_INT64(diff
, TmpTestStruct
),
797 VMSTATE_STRUCT_POINTER(parent
, TmpTestStruct
,
798 vmstate_tmp_back_to_parent
, TestStruct
),
799 VMSTATE_END_OF_LIST()
803 static const VMStateDescription vmstate_with_tmp
= {
804 .name
= "test/with_tmp",
806 .fields
= (VMStateField
[]) {
807 VMSTATE_UINT32(a
, TestStruct
),
808 VMSTATE_UINT64(d
, TestStruct
),
809 VMSTATE_WITH_TMP(TestStruct
, TmpTestStruct
, vmstate_tmp_child
),
810 VMSTATE_END_OF_LIST()
814 static void obj_tmp_copy(void *target
, void *source
)
816 memcpy(target
, source
, sizeof(TestStruct
));
819 static void test_tmp_struct(void)
821 TestStruct obj
, obj_clone
;
823 uint8_t const wire_with_tmp
[] = {
824 /* u32 a */ 0x00, 0x00, 0x00, 0x02,
825 /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
826 /* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
827 /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
828 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
831 memset(&obj
, 0, sizeof(obj
));
836 save_vmstate(&vmstate_with_tmp
, &obj
);
838 compare_vmstate(wire_with_tmp
, sizeof(wire_with_tmp
));
840 memset(&obj
, 0, sizeof(obj
));
841 SUCCESS(load_vmstate(&vmstate_with_tmp
, &obj
, &obj_clone
,
842 obj_tmp_copy
, 1, wire_with_tmp
,
843 sizeof(wire_with_tmp
)));
844 g_assert_cmpint(obj
.a
, ==, 2); /* From top level vmsd */
845 g_assert_cmpint(obj
.b
, ==, 4); /* from the post_load */
846 g_assert_cmpint(obj
.d
, ==, 1); /* From top level vmsd */
847 g_assert_cmpint(obj
.f
, ==, 8); /* From the child->parent */
850 int main(int argc
, char **argv
)
852 temp_fd
= mkstemp(temp_file
);
854 module_call_init(MODULE_INIT_QOM
);
856 g_test_init(&argc
, &argv
, NULL
);
857 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive
);
858 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1
);
859 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2
);
860 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip
);
861 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip
);
862 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip
);
863 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip
);
864 g_test_add_func("/vmstate/array/ptr/str/no0/save",
865 test_arr_ptr_str_no0_save
);
866 g_test_add_func("/vmstate/array/ptr/str/no0/load",
867 test_arr_ptr_str_no0_load
);
868 g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save
);
869 g_test_add_func("/vmstate/array/ptr/str/0/load",
870 test_arr_ptr_str_0_load
);
871 g_test_add_func("/vmstate/array/ptr/prim/0/save",
872 test_arr_ptr_prim_0_save
);
873 g_test_add_func("/vmstate/array/ptr/prim/0/load",
874 test_arr_ptr_prim_0_load
);
875 g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q
);
876 g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q
);
877 g_test_add_func("/vmstate/tmp_struct", test_tmp_struct
);