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.h"
31 #include "../migration/qemu-file-channel.h"
32 #include "qemu/coroutine.h"
33 #include "io/channel-file.h"
35 static char temp_file
[] = "/tmp/vmst.test.XXXXXX";
39 /* Duplicate temp_fd and seek to the beginning of the file */
40 static QEMUFile
*open_test_file(bool write
)
42 int fd
= dup(temp_fd
);
46 lseek(fd
, 0, SEEK_SET
);
48 g_assert_cmpint(ftruncate(fd
, 0), ==, 0);
50 ioc
= QIO_CHANNEL(qio_channel_file_new_fd(fd
));
52 f
= qemu_fopen_channel_output(ioc
);
54 f
= qemu_fopen_channel_input(ioc
);
56 object_unref(OBJECT(ioc
));
60 #define SUCCESS(val) \
61 g_assert_cmpint((val), ==, 0)
63 #define FAILURE(val) \
64 g_assert_cmpint((val), !=, 0)
66 static void save_vmstate(const VMStateDescription
*desc
, void *obj
)
68 QEMUFile
*f
= open_test_file(true);
70 /* Save file with vmstate */
71 vmstate_save_state(f
, desc
, obj
, NULL
);
72 qemu_put_byte(f
, QEMU_VM_EOF
);
73 g_assert(!qemu_file_get_error(f
));
77 static void save_buffer(const uint8_t *buf
, size_t buf_size
)
79 QEMUFile
*fsave
= open_test_file(true);
80 qemu_put_buffer(fsave
, buf
, buf_size
);
84 static void compare_vmstate(const uint8_t *wire
, size_t size
)
86 QEMUFile
*f
= open_test_file(false);
89 /* read back as binary */
91 g_assert_cmpint(qemu_get_buffer(f
, result
, sizeof(result
)), ==,
93 g_assert(!qemu_file_get_error(f
));
95 /* Compare that what is on the file is the same that what we
96 expected to be there */
97 SUCCESS(memcmp(result
, wire
, sizeof(result
)));
101 g_assert_cmpint(qemu_file_get_error(f
), ==, -EIO
);
106 static int load_vmstate_one(const VMStateDescription
*desc
, void *obj
,
107 int version
, const uint8_t *wire
, size_t size
)
112 f
= open_test_file(true);
113 qemu_put_buffer(f
, wire
, size
);
116 f
= open_test_file(false);
117 ret
= vmstate_load_state(f
, desc
, obj
, version
);
119 g_assert(qemu_file_get_error(f
));
121 g_assert(!qemu_file_get_error(f
));
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
)
133 /* We test with zero size */
134 obj_copy(obj_clone
, obj
);
135 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, 0));
137 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
138 * able to test in the middle */
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));
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));
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));
155 obj_copy(obj
, obj_clone
);
156 return load_vmstate_one(desc
, obj
, version
, wire
, size
);
159 /* Test struct that we are going to use for our tests */
161 typedef struct TestSimple
{
168 int16_t i16_1
, i16_2
;
169 int32_t i32_1
, i32_2
;
170 int64_t i64_1
, i64_2
;
173 /* Object instantiation, we are going to use it in more than one test */
175 TestSimple obj_simple
= {
192 /* Description of the values. If you add a primitive type
193 you are expected to add a test here */
195 static const VMStateDescription vmstate_simple_primitive
= {
196 .name
= "simple/primitive",
198 .minimum_version_id
= 1,
199 .fields
= (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()
218 /* It describes what goes through the wire. Our tests are basically:
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
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
230 uint8_t wire_simple_primitive
[] = {
234 /* u16_1 */ 0x02, 0x00,
235 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
236 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
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 */
248 static void obj_simple_copy(void *target
, void *source
)
250 memcpy(target
, source
, sizeof(TestSimple
));
253 static void test_simple_primitive(void)
255 TestSimple obj
, obj_clone
;
257 memset(&obj
, 0, sizeof(obj
));
258 save_vmstate(&vmstate_simple_primitive
, &obj_simple
);
260 compare_vmstate(wire_simple_primitive
, sizeof(wire_simple_primitive
));
262 SUCCESS(load_vmstate(&vmstate_simple_primitive
, &obj
, &obj_clone
,
263 obj_simple_copy
, 1, wire_simple_primitive
,
264 sizeof(wire_simple_primitive
)));
266 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
284 typedef struct TestStruct
{
290 static const VMStateDescription vmstate_versioned
= {
291 .name
= "test/versioned",
293 .minimum_version_id
= 1,
294 .fields
= (VMStateField
[]) {
295 VMSTATE_UINT32(a
, TestStruct
),
296 VMSTATE_UINT32_V(b
, TestStruct
, 2), /* Versioned field in the middle, so
297 * we catch bugs more easily.
299 VMSTATE_UINT32(c
, TestStruct
),
300 VMSTATE_UINT64(d
, TestStruct
),
301 VMSTATE_UINT32_V(e
, TestStruct
, 2),
302 VMSTATE_UINT64_V(f
, TestStruct
, 2),
303 VMSTATE_END_OF_LIST()
307 static void test_load_v1(void)
312 0, 0, 0, 0, 0, 0, 0, 40, /* d */
313 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
315 save_buffer(buf
, sizeof(buf
));
317 QEMUFile
*loading
= open_test_file(false);
318 TestStruct obj
= { .b
= 200, .e
= 500, .f
= 600 };
319 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 1);
320 g_assert(!qemu_file_get_error(loading
));
321 g_assert_cmpint(obj
.a
, ==, 10);
322 g_assert_cmpint(obj
.b
, ==, 200);
323 g_assert_cmpint(obj
.c
, ==, 30);
324 g_assert_cmpint(obj
.d
, ==, 40);
325 g_assert_cmpint(obj
.e
, ==, 500);
326 g_assert_cmpint(obj
.f
, ==, 600);
327 qemu_fclose(loading
);
330 static void test_load_v2(void)
336 0, 0, 0, 0, 0, 0, 0, 40, /* d */
338 0, 0, 0, 0, 0, 0, 0, 60, /* f */
339 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
341 save_buffer(buf
, sizeof(buf
));
343 QEMUFile
*loading
= open_test_file(false);
345 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 2);
346 g_assert_cmpint(obj
.a
, ==, 10);
347 g_assert_cmpint(obj
.b
, ==, 20);
348 g_assert_cmpint(obj
.c
, ==, 30);
349 g_assert_cmpint(obj
.d
, ==, 40);
350 g_assert_cmpint(obj
.e
, ==, 50);
351 g_assert_cmpint(obj
.f
, ==, 60);
352 qemu_fclose(loading
);
355 static bool test_skip(void *opaque
, int version_id
)
357 TestStruct
*t
= (TestStruct
*)opaque
;
361 static const VMStateDescription vmstate_skipping
= {
364 .minimum_version_id
= 1,
365 .fields
= (VMStateField
[]) {
366 VMSTATE_UINT32(a
, TestStruct
),
367 VMSTATE_UINT32(b
, TestStruct
),
368 VMSTATE_UINT32_TEST(c
, TestStruct
, test_skip
),
369 VMSTATE_UINT64(d
, TestStruct
),
370 VMSTATE_UINT32_TEST(e
, TestStruct
, test_skip
),
371 VMSTATE_UINT64_V(f
, TestStruct
, 2),
372 VMSTATE_END_OF_LIST()
377 static void test_save_noskip(void)
379 QEMUFile
*fsave
= open_test_file(true);
380 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
382 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
383 g_assert(!qemu_file_get_error(fsave
));
385 uint8_t expected
[] = {
389 0, 0, 0, 0, 0, 0, 0, 4, /* d */
391 0, 0, 0, 0, 0, 0, 0, 6, /* f */
395 compare_vmstate(expected
, sizeof(expected
));
398 static void test_save_skip(void)
400 QEMUFile
*fsave
= open_test_file(true);
401 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
403 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
404 g_assert(!qemu_file_get_error(fsave
));
406 uint8_t expected
[] = {
409 0, 0, 0, 0, 0, 0, 0, 4, /* d */
410 0, 0, 0, 0, 0, 0, 0, 6, /* f */
414 compare_vmstate(expected
, sizeof(expected
));
417 static void test_load_noskip(void)
423 0, 0, 0, 0, 0, 0, 0, 40, /* d */
425 0, 0, 0, 0, 0, 0, 0, 60, /* f */
426 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
428 save_buffer(buf
, sizeof(buf
));
430 QEMUFile
*loading
= open_test_file(false);
431 TestStruct obj
= { .skip_c_e
= false };
432 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
433 g_assert(!qemu_file_get_error(loading
));
434 g_assert_cmpint(obj
.a
, ==, 10);
435 g_assert_cmpint(obj
.b
, ==, 20);
436 g_assert_cmpint(obj
.c
, ==, 30);
437 g_assert_cmpint(obj
.d
, ==, 40);
438 g_assert_cmpint(obj
.e
, ==, 50);
439 g_assert_cmpint(obj
.f
, ==, 60);
440 qemu_fclose(loading
);
443 static void test_load_skip(void)
448 0, 0, 0, 0, 0, 0, 0, 40, /* d */
449 0, 0, 0, 0, 0, 0, 0, 60, /* f */
450 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
452 save_buffer(buf
, sizeof(buf
));
454 QEMUFile
*loading
= open_test_file(false);
455 TestStruct obj
= { .skip_c_e
= true, .c
= 300, .e
= 500 };
456 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
457 g_assert(!qemu_file_get_error(loading
));
458 g_assert_cmpint(obj
.a
, ==, 10);
459 g_assert_cmpint(obj
.b
, ==, 20);
460 g_assert_cmpint(obj
.c
, ==, 300);
461 g_assert_cmpint(obj
.d
, ==, 40);
462 g_assert_cmpint(obj
.e
, ==, 500);
463 g_assert_cmpint(obj
.f
, ==, 60);
464 qemu_fclose(loading
);
471 const VMStateDescription vmsd_tst
= {
474 .minimum_version_id
= 1,
475 .fields
= (VMStateField
[]) {
476 VMSTATE_INT32(i
, TestStructTriv
),
477 VMSTATE_END_OF_LIST()
481 /* test array migration */
486 TestStructTriv
*ar
[AR_SIZE
];
487 } TestArrayOfPtrToStuct
;
489 const VMStateDescription vmsd_arps
= {
492 .minimum_version_id
= 1,
493 .fields
= (VMStateField
[]) {
494 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar
, TestArrayOfPtrToStuct
,
495 AR_SIZE
, 0, vmsd_tst
, TestStructTriv
),
496 VMSTATE_END_OF_LIST()
500 static uint8_t wire_arr_ptr_no0
[] = {
501 0x00, 0x00, 0x00, 0x00,
502 0x00, 0x00, 0x00, 0x01,
503 0x00, 0x00, 0x00, 0x02,
504 0x00, 0x00, 0x00, 0x03,
508 static void test_arr_ptr_str_no0_save(void)
510 TestStructTriv ar
[AR_SIZE
] = {{.i
= 0}, {.i
= 1}, {.i
= 2}, {.i
= 3} };
511 TestArrayOfPtrToStuct sample
= {.ar
= {&ar
[0], &ar
[1], &ar
[2], &ar
[3]} };
513 save_vmstate(&vmsd_arps
, &sample
);
514 compare_vmstate(wire_arr_ptr_no0
, sizeof(wire_arr_ptr_no0
));
517 static void test_arr_ptr_str_no0_load(void)
519 TestStructTriv ar_gt
[AR_SIZE
] = {{.i
= 0}, {.i
= 1}, {.i
= 2}, {.i
= 3} };
520 TestStructTriv ar
[AR_SIZE
] = {};
521 TestArrayOfPtrToStuct obj
= {.ar
= {&ar
[0], &ar
[1], &ar
[2], &ar
[3]} };
524 save_buffer(wire_arr_ptr_no0
, sizeof(wire_arr_ptr_no0
));
525 SUCCESS(load_vmstate_one(&vmsd_arps
, &obj
, 1,
526 wire_arr_ptr_no0
, sizeof(wire_arr_ptr_no0
)));
527 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
528 /* compare the target array ar with the ground truth array ar_gt */
529 g_assert_cmpint(ar_gt
[idx
].i
, ==, ar
[idx
].i
);
533 static uint8_t wire_arr_ptr_0
[] = {
534 0x00, 0x00, 0x00, 0x00,
536 0x00, 0x00, 0x00, 0x02,
537 0x00, 0x00, 0x00, 0x03,
541 static void test_arr_ptr_str_0_save(void)
543 TestStructTriv ar
[AR_SIZE
] = {{.i
= 0}, {.i
= 1}, {.i
= 2}, {.i
= 3} };
544 TestArrayOfPtrToStuct sample
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
546 save_vmstate(&vmsd_arps
, &sample
);
547 compare_vmstate(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
550 static void test_arr_ptr_str_0_load(void)
552 TestStructTriv ar_gt
[AR_SIZE
] = {{.i
= 0}, {.i
= 0}, {.i
= 2}, {.i
= 3} };
553 TestStructTriv ar
[AR_SIZE
] = {};
554 TestArrayOfPtrToStuct obj
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
557 save_buffer(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
558 SUCCESS(load_vmstate_one(&vmsd_arps
, &obj
, 1,
559 wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
)));
560 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
561 /* compare the target array ar with the ground truth array ar_gt */
562 g_assert_cmpint(ar_gt
[idx
].i
, ==, ar
[idx
].i
);
564 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
566 g_assert_cmpint((uintptr_t)(obj
.ar
[idx
]), ==, 0);
568 g_assert_cmpint((uintptr_t)(obj
.ar
[idx
]), !=, 0);
573 typedef struct TestArrayOfPtrToInt
{
574 int32_t *ar
[AR_SIZE
];
575 } TestArrayOfPtrToInt
;
577 const VMStateDescription vmsd_arpp
= {
580 .minimum_version_id
= 1,
581 .fields
= (VMStateField
[]) {
582 VMSTATE_ARRAY_OF_POINTER(ar
, TestArrayOfPtrToInt
,
583 AR_SIZE
, 0, vmstate_info_int32
, int32_t*),
584 VMSTATE_END_OF_LIST()
588 static void test_arr_ptr_prim_0_save(void)
590 int32_t ar
[AR_SIZE
] = {0 , 1, 2, 3};
591 TestArrayOfPtrToInt sample
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
593 save_vmstate(&vmsd_arpp
, &sample
);
594 compare_vmstate(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
597 static void test_arr_ptr_prim_0_load(void)
599 int32_t ar_gt
[AR_SIZE
] = {0, 1, 2, 3};
600 int32_t ar
[AR_SIZE
] = {3 , 42, 1, 0};
601 TestArrayOfPtrToInt obj
= {.ar
= {&ar
[0], NULL
, &ar
[2], &ar
[3]} };
604 save_buffer(wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
));
605 SUCCESS(load_vmstate_one(&vmsd_arpp
, &obj
, 1,
606 wire_arr_ptr_0
, sizeof(wire_arr_ptr_0
)));
607 for (idx
= 0; idx
< AR_SIZE
; ++idx
) {
608 /* compare the target array ar with the ground truth array ar_gt */
610 g_assert_cmpint(42, ==, ar
[idx
]);
612 g_assert_cmpint(ar_gt
[idx
], ==, ar
[idx
]);
617 /* test QTAILQ migration */
618 typedef struct TestQtailqElement TestQtailqElement
;
620 struct TestQtailqElement
{
623 QTAILQ_ENTRY(TestQtailqElement
) next
;
626 typedef struct TestQtailq
{
628 QTAILQ_HEAD(TestQtailqHead
, TestQtailqElement
) q
;
632 static const VMStateDescription vmstate_q_element
= {
633 .name
= "test/queue-element",
635 .minimum_version_id
= 1,
636 .fields
= (VMStateField
[]) {
637 VMSTATE_BOOL(b
, TestQtailqElement
),
638 VMSTATE_UINT8(u8
, TestQtailqElement
),
639 VMSTATE_END_OF_LIST()
643 static const VMStateDescription vmstate_q
= {
644 .name
= "test/queue",
646 .minimum_version_id
= 1,
647 .fields
= (VMStateField
[]) {
648 VMSTATE_INT16(i16
, TestQtailq
),
649 VMSTATE_QTAILQ_V(q
, TestQtailq
, 1, vmstate_q_element
, TestQtailqElement
,
651 VMSTATE_INT32(i32
, TestQtailq
),
652 VMSTATE_END_OF_LIST()
658 /* start of element 0 of q */ 0x01,
661 /* start of element 1 of q */ 0x01,
665 /* i32 */ 0x00, 0x01, 0x11, 0x70,
666 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
669 static void test_save_q(void)
676 TestQtailqElement obj_qe1
= {
681 TestQtailqElement obj_qe2
= {
686 QTAILQ_INIT(&obj_q
.q
);
687 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe1
, next
);
688 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe2
, next
);
690 save_vmstate(&vmstate_q
, &obj_q
);
691 compare_vmstate(wire_q
, sizeof(wire_q
));
694 static void test_load_q(void)
701 TestQtailqElement obj_qe1
= {
706 TestQtailqElement obj_qe2
= {
711 QTAILQ_INIT(&obj_q
.q
);
712 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe1
, next
);
713 QTAILQ_INSERT_TAIL(&obj_q
.q
, &obj_qe2
, next
);
715 QEMUFile
*fsave
= open_test_file(true);
717 qemu_put_buffer(fsave
, wire_q
, sizeof(wire_q
));
718 g_assert(!qemu_file_get_error(fsave
));
721 QEMUFile
*fload
= open_test_file(false);
725 vmstate_load_state(fload
, &vmstate_q
, &tgt
, 1);
726 char eof
= qemu_get_byte(fload
);
727 g_assert(!qemu_file_get_error(fload
));
728 g_assert_cmpint(tgt
.i16
, ==, obj_q
.i16
);
729 g_assert_cmpint(tgt
.i32
, ==, obj_q
.i32
);
730 g_assert_cmpint(eof
, ==, QEMU_VM_EOF
);
732 TestQtailqElement
*qele_from
= QTAILQ_FIRST(&obj_q
.q
);
733 TestQtailqElement
*qlast_from
= QTAILQ_LAST(&obj_q
.q
, TestQtailqHead
);
734 TestQtailqElement
*qele_to
= QTAILQ_FIRST(&tgt
.q
);
735 TestQtailqElement
*qlast_to
= QTAILQ_LAST(&tgt
.q
, TestQtailqHead
);
738 g_assert_cmpint(qele_to
->b
, ==, qele_from
->b
);
739 g_assert_cmpint(qele_to
->u8
, ==, qele_from
->u8
);
740 if ((qele_from
== qlast_from
) || (qele_to
== qlast_to
)) {
743 qele_from
= QTAILQ_NEXT(qele_from
, next
);
744 qele_to
= QTAILQ_NEXT(qele_to
, next
);
747 g_assert_cmpint((uintptr_t) qele_from
, ==, (uintptr_t) qlast_from
);
748 g_assert_cmpint((uintptr_t) qele_to
, ==, (uintptr_t) qlast_to
);
751 TestQtailqElement
*qele
;
752 while (!QTAILQ_EMPTY(&tgt
.q
)) {
753 qele
= QTAILQ_LAST(&tgt
.q
, TestQtailqHead
);
754 QTAILQ_REMOVE(&tgt
.q
, qele
, next
);
761 typedef struct TmpTestStruct
{
766 static void tmp_child_pre_save(void *opaque
)
768 struct TmpTestStruct
*tts
= opaque
;
770 tts
->diff
= tts
->parent
->b
- tts
->parent
->a
;
773 static int tmp_child_post_load(void *opaque
, int version_id
)
775 struct TmpTestStruct
*tts
= opaque
;
777 tts
->parent
->b
= tts
->parent
->a
+ tts
->diff
;
782 static const VMStateDescription vmstate_tmp_back_to_parent
= {
783 .name
= "test/tmp_child_parent",
784 .fields
= (VMStateField
[]) {
785 VMSTATE_UINT64(f
, TestStruct
),
786 VMSTATE_END_OF_LIST()
790 static const VMStateDescription vmstate_tmp_child
= {
791 .name
= "test/tmp_child",
792 .pre_save
= tmp_child_pre_save
,
793 .post_load
= tmp_child_post_load
,
794 .fields
= (VMStateField
[]) {
795 VMSTATE_INT64(diff
, TmpTestStruct
),
796 VMSTATE_STRUCT_POINTER(parent
, TmpTestStruct
,
797 vmstate_tmp_back_to_parent
, TestStruct
),
798 VMSTATE_END_OF_LIST()
802 static const VMStateDescription vmstate_with_tmp
= {
803 .name
= "test/with_tmp",
805 .fields
= (VMStateField
[]) {
806 VMSTATE_UINT32(a
, TestStruct
),
807 VMSTATE_UINT64(d
, TestStruct
),
808 VMSTATE_WITH_TMP(TestStruct
, TmpTestStruct
, vmstate_tmp_child
),
809 VMSTATE_END_OF_LIST()
813 static void obj_tmp_copy(void *target
, void *source
)
815 memcpy(target
, source
, sizeof(TestStruct
));
818 static void test_tmp_struct(void)
820 TestStruct obj
, obj_clone
;
822 uint8_t const wire_with_tmp
[] = {
823 /* u32 a */ 0x00, 0x00, 0x00, 0x02,
824 /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
825 /* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
826 /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
827 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
830 memset(&obj
, 0, sizeof(obj
));
835 save_vmstate(&vmstate_with_tmp
, &obj
);
837 compare_vmstate(wire_with_tmp
, sizeof(wire_with_tmp
));
839 memset(&obj
, 0, sizeof(obj
));
840 SUCCESS(load_vmstate(&vmstate_with_tmp
, &obj
, &obj_clone
,
841 obj_tmp_copy
, 1, wire_with_tmp
,
842 sizeof(wire_with_tmp
)));
843 g_assert_cmpint(obj
.a
, ==, 2); /* From top level vmsd */
844 g_assert_cmpint(obj
.b
, ==, 4); /* from the post_load */
845 g_assert_cmpint(obj
.d
, ==, 1); /* From top level vmsd */
846 g_assert_cmpint(obj
.f
, ==, 8); /* From the child->parent */
849 int main(int argc
, char **argv
)
851 temp_fd
= mkstemp(temp_file
);
853 module_call_init(MODULE_INIT_QOM
);
855 g_test_init(&argc
, &argv
, NULL
);
856 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive
);
857 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1
);
858 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2
);
859 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip
);
860 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip
);
861 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip
);
862 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip
);
863 g_test_add_func("/vmstate/array/ptr/str/no0/save",
864 test_arr_ptr_str_no0_save
);
865 g_test_add_func("/vmstate/array/ptr/str/no0/load",
866 test_arr_ptr_str_no0_load
);
867 g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save
);
868 g_test_add_func("/vmstate/array/ptr/str/0/load",
869 test_arr_ptr_str_0_load
);
870 g_test_add_func("/vmstate/array/ptr/prim/0/save",
871 test_arr_ptr_prim_0_save
);
872 g_test_add_func("/vmstate/array/ptr/prim/0/load",
873 test_arr_ptr_prim_0_load
);
874 g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q
);
875 g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q
);
876 g_test_add_func("/vmstate/tmp_struct", test_tmp_struct
);