]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-vmstate.c
Merge remote-tracking branch 'remotes/dgilbert/tags/pull-migration-20170213a' into...
[mirror_qemu.git] / tests / test-vmstate.c
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 "qemu-common.h"
28 #include "migration/migration.h"
29 #include "migration/vmstate.h"
30 #include "qemu/coroutine.h"
31 #include "io/channel-file.h"
32
33 static char temp_file[] = "/tmp/vmst.test.XXXXXX";
34 static int temp_fd;
35
36 /* Fake yield_until_fd_readable() implementation so we don't have to pull the
37 * coroutine code as dependency.
38 */
39 void yield_until_fd_readable(int fd)
40 {
41 fd_set fds;
42 FD_ZERO(&fds);
43 FD_SET(fd, &fds);
44 select(fd + 1, &fds, NULL, NULL, NULL);
45 }
46
47
48 /* Duplicate temp_fd and seek to the beginning of the file */
49 static QEMUFile *open_test_file(bool write)
50 {
51 int fd = dup(temp_fd);
52 QIOChannel *ioc;
53 QEMUFile *f;
54
55 lseek(fd, 0, SEEK_SET);
56 if (write) {
57 g_assert_cmpint(ftruncate(fd, 0), ==, 0);
58 }
59 ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
60 if (write) {
61 f = qemu_fopen_channel_output(ioc);
62 } else {
63 f = qemu_fopen_channel_input(ioc);
64 }
65 object_unref(OBJECT(ioc));
66 return f;
67 }
68
69 #define SUCCESS(val) \
70 g_assert_cmpint((val), ==, 0)
71
72 #define FAILURE(val) \
73 g_assert_cmpint((val), !=, 0)
74
75 static void save_vmstate(const VMStateDescription *desc, void *obj)
76 {
77 QEMUFile *f = open_test_file(true);
78
79 /* Save file with vmstate */
80 vmstate_save_state(f, desc, obj, NULL);
81 qemu_put_byte(f, QEMU_VM_EOF);
82 g_assert(!qemu_file_get_error(f));
83 qemu_fclose(f);
84 }
85
86 static void save_buffer(const uint8_t *buf, size_t buf_size)
87 {
88 QEMUFile *fsave = open_test_file(true);
89 qemu_put_buffer(fsave, buf, buf_size);
90 qemu_fclose(fsave);
91 }
92
93 static void compare_vmstate(const uint8_t *wire, size_t size)
94 {
95 QEMUFile *f = open_test_file(false);
96 uint8_t result[size];
97
98 /* read back as binary */
99
100 g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
101 sizeof(result));
102 g_assert(!qemu_file_get_error(f));
103
104 /* Compare that what is on the file is the same that what we
105 expected to be there */
106 SUCCESS(memcmp(result, wire, sizeof(result)));
107
108 /* Must reach EOF */
109 qemu_get_byte(f);
110 g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
111
112 qemu_fclose(f);
113 }
114
115 static int load_vmstate_one(const VMStateDescription *desc, void *obj,
116 int version, const uint8_t *wire, size_t size)
117 {
118 QEMUFile *f;
119 int ret;
120
121 f = open_test_file(true);
122 qemu_put_buffer(f, wire, size);
123 qemu_fclose(f);
124
125 f = open_test_file(false);
126 ret = vmstate_load_state(f, desc, obj, version);
127 if (ret) {
128 g_assert(qemu_file_get_error(f));
129 } else{
130 g_assert(!qemu_file_get_error(f));
131 }
132 qemu_fclose(f);
133 return ret;
134 }
135
136
137 static int load_vmstate(const VMStateDescription *desc,
138 void *obj, void *obj_clone,
139 void (*obj_copy)(void *, void*),
140 int version, const uint8_t *wire, size_t size)
141 {
142 /* We test with zero size */
143 obj_copy(obj_clone, obj);
144 FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
145
146 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
147 * able to test in the middle */
148
149 if (size > 3) {
150
151 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
152 obj_copy(obj, obj_clone);
153 FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
154
155 /* Test with size/2, first half of real state */
156 obj_copy(obj, obj_clone);
157 FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
158
159 /* Test with size/2, second half of real state */
160 obj_copy(obj, obj_clone);
161 FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
162
163 }
164 obj_copy(obj, obj_clone);
165 return load_vmstate_one(desc, obj, version, wire, size);
166 }
167
168 /* Test struct that we are going to use for our tests */
169
170 typedef struct TestSimple {
171 bool b_1, b_2;
172 uint8_t u8_1;
173 uint16_t u16_1;
174 uint32_t u32_1;
175 uint64_t u64_1;
176 int8_t i8_1, i8_2;
177 int16_t i16_1, i16_2;
178 int32_t i32_1, i32_2;
179 int64_t i64_1, i64_2;
180 } TestSimple;
181
182 /* Object instantiation, we are going to use it in more than one test */
183
184 TestSimple obj_simple = {
185 .b_1 = true,
186 .b_2 = false,
187 .u8_1 = 130,
188 .u16_1 = 512,
189 .u32_1 = 70000,
190 .u64_1 = 12121212,
191 .i8_1 = 65,
192 .i8_2 = -65,
193 .i16_1 = 512,
194 .i16_2 = -512,
195 .i32_1 = 70000,
196 .i32_2 = -70000,
197 .i64_1 = 12121212,
198 .i64_2 = -12121212,
199 };
200
201 /* Description of the values. If you add a primitive type
202 you are expected to add a test here */
203
204 static const VMStateDescription vmstate_simple_primitive = {
205 .name = "simple/primitive",
206 .version_id = 1,
207 .minimum_version_id = 1,
208 .fields = (VMStateField[]) {
209 VMSTATE_BOOL(b_1, TestSimple),
210 VMSTATE_BOOL(b_2, TestSimple),
211 VMSTATE_UINT8(u8_1, TestSimple),
212 VMSTATE_UINT16(u16_1, TestSimple),
213 VMSTATE_UINT32(u32_1, TestSimple),
214 VMSTATE_UINT64(u64_1, TestSimple),
215 VMSTATE_INT8(i8_1, TestSimple),
216 VMSTATE_INT8(i8_2, TestSimple),
217 VMSTATE_INT16(i16_1, TestSimple),
218 VMSTATE_INT16(i16_2, TestSimple),
219 VMSTATE_INT32(i32_1, TestSimple),
220 VMSTATE_INT32(i32_2, TestSimple),
221 VMSTATE_INT64(i64_1, TestSimple),
222 VMSTATE_INT64(i64_2, TestSimple),
223 VMSTATE_END_OF_LIST()
224 }
225 };
226
227 /* It describes what goes through the wire. Our tests are basically:
228
229 * save test
230 - save a struct a vmstate to a file
231 - read that file back (binary read, no vmstate)
232 - compare it with what we expect to be on the wire
233 * load test
234 - save to the file what we expect to be on the wire
235 - read struct back with vmstate in a different
236 - compare back with the original struct
237 */
238
239 uint8_t wire_simple_primitive[] = {
240 /* b_1 */ 0x01,
241 /* b_2 */ 0x00,
242 /* u8_1 */ 0x82,
243 /* u16_1 */ 0x02, 0x00,
244 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
245 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
246 /* i8_1 */ 0x41,
247 /* i8_2 */ 0xbf,
248 /* i16_1 */ 0x02, 0x00,
249 /* i16_2 */ 0xfe, 0x0,
250 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
251 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
252 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
253 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
254 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
255 };
256
257 static void obj_simple_copy(void *target, void *source)
258 {
259 memcpy(target, source, sizeof(TestSimple));
260 }
261
262 static void test_simple_primitive(void)
263 {
264 TestSimple obj, obj_clone;
265
266 memset(&obj, 0, sizeof(obj));
267 save_vmstate(&vmstate_simple_primitive, &obj_simple);
268
269 compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
270
271 SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
272 obj_simple_copy, 1, wire_simple_primitive,
273 sizeof(wire_simple_primitive)));
274
275 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
276
277 FIELD_EQUAL(b_1);
278 FIELD_EQUAL(b_2);
279 FIELD_EQUAL(u8_1);
280 FIELD_EQUAL(u16_1);
281 FIELD_EQUAL(u32_1);
282 FIELD_EQUAL(u64_1);
283 FIELD_EQUAL(i8_1);
284 FIELD_EQUAL(i8_2);
285 FIELD_EQUAL(i16_1);
286 FIELD_EQUAL(i16_2);
287 FIELD_EQUAL(i32_1);
288 FIELD_EQUAL(i32_2);
289 FIELD_EQUAL(i64_1);
290 FIELD_EQUAL(i64_2);
291 }
292
293 typedef struct TestStruct {
294 uint32_t a, b, c, e;
295 uint64_t d, f;
296 bool skip_c_e;
297 } TestStruct;
298
299 static const VMStateDescription vmstate_versioned = {
300 .name = "test/versioned",
301 .version_id = 2,
302 .minimum_version_id = 1,
303 .fields = (VMStateField[]) {
304 VMSTATE_UINT32(a, TestStruct),
305 VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
306 * we catch bugs more easily.
307 */
308 VMSTATE_UINT32(c, TestStruct),
309 VMSTATE_UINT64(d, TestStruct),
310 VMSTATE_UINT32_V(e, TestStruct, 2),
311 VMSTATE_UINT64_V(f, TestStruct, 2),
312 VMSTATE_END_OF_LIST()
313 }
314 };
315
316 static void test_load_v1(void)
317 {
318 uint8_t buf[] = {
319 0, 0, 0, 10, /* a */
320 0, 0, 0, 30, /* c */
321 0, 0, 0, 0, 0, 0, 0, 40, /* d */
322 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
323 };
324 save_buffer(buf, sizeof(buf));
325
326 QEMUFile *loading = open_test_file(false);
327 TestStruct obj = { .b = 200, .e = 500, .f = 600 };
328 vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
329 g_assert(!qemu_file_get_error(loading));
330 g_assert_cmpint(obj.a, ==, 10);
331 g_assert_cmpint(obj.b, ==, 200);
332 g_assert_cmpint(obj.c, ==, 30);
333 g_assert_cmpint(obj.d, ==, 40);
334 g_assert_cmpint(obj.e, ==, 500);
335 g_assert_cmpint(obj.f, ==, 600);
336 qemu_fclose(loading);
337 }
338
339 static void test_load_v2(void)
340 {
341 uint8_t buf[] = {
342 0, 0, 0, 10, /* a */
343 0, 0, 0, 20, /* b */
344 0, 0, 0, 30, /* c */
345 0, 0, 0, 0, 0, 0, 0, 40, /* d */
346 0, 0, 0, 50, /* e */
347 0, 0, 0, 0, 0, 0, 0, 60, /* f */
348 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
349 };
350 save_buffer(buf, sizeof(buf));
351
352 QEMUFile *loading = open_test_file(false);
353 TestStruct obj;
354 vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
355 g_assert_cmpint(obj.a, ==, 10);
356 g_assert_cmpint(obj.b, ==, 20);
357 g_assert_cmpint(obj.c, ==, 30);
358 g_assert_cmpint(obj.d, ==, 40);
359 g_assert_cmpint(obj.e, ==, 50);
360 g_assert_cmpint(obj.f, ==, 60);
361 qemu_fclose(loading);
362 }
363
364 static bool test_skip(void *opaque, int version_id)
365 {
366 TestStruct *t = (TestStruct *)opaque;
367 return !t->skip_c_e;
368 }
369
370 static const VMStateDescription vmstate_skipping = {
371 .name = "test/skip",
372 .version_id = 2,
373 .minimum_version_id = 1,
374 .fields = (VMStateField[]) {
375 VMSTATE_UINT32(a, TestStruct),
376 VMSTATE_UINT32(b, TestStruct),
377 VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
378 VMSTATE_UINT64(d, TestStruct),
379 VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
380 VMSTATE_UINT64_V(f, TestStruct, 2),
381 VMSTATE_END_OF_LIST()
382 }
383 };
384
385
386 static void test_save_noskip(void)
387 {
388 QEMUFile *fsave = open_test_file(true);
389 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
390 .skip_c_e = false };
391 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
392 g_assert(!qemu_file_get_error(fsave));
393
394 uint8_t expected[] = {
395 0, 0, 0, 1, /* a */
396 0, 0, 0, 2, /* b */
397 0, 0, 0, 3, /* c */
398 0, 0, 0, 0, 0, 0, 0, 4, /* d */
399 0, 0, 0, 5, /* e */
400 0, 0, 0, 0, 0, 0, 0, 6, /* f */
401 };
402
403 qemu_fclose(fsave);
404 compare_vmstate(expected, sizeof(expected));
405 }
406
407 static void test_save_skip(void)
408 {
409 QEMUFile *fsave = open_test_file(true);
410 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
411 .skip_c_e = true };
412 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
413 g_assert(!qemu_file_get_error(fsave));
414
415 uint8_t expected[] = {
416 0, 0, 0, 1, /* a */
417 0, 0, 0, 2, /* b */
418 0, 0, 0, 0, 0, 0, 0, 4, /* d */
419 0, 0, 0, 0, 0, 0, 0, 6, /* f */
420 };
421
422 qemu_fclose(fsave);
423 compare_vmstate(expected, sizeof(expected));
424 }
425
426 static void test_load_noskip(void)
427 {
428 uint8_t buf[] = {
429 0, 0, 0, 10, /* a */
430 0, 0, 0, 20, /* b */
431 0, 0, 0, 30, /* c */
432 0, 0, 0, 0, 0, 0, 0, 40, /* d */
433 0, 0, 0, 50, /* e */
434 0, 0, 0, 0, 0, 0, 0, 60, /* f */
435 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
436 };
437 save_buffer(buf, sizeof(buf));
438
439 QEMUFile *loading = open_test_file(false);
440 TestStruct obj = { .skip_c_e = false };
441 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
442 g_assert(!qemu_file_get_error(loading));
443 g_assert_cmpint(obj.a, ==, 10);
444 g_assert_cmpint(obj.b, ==, 20);
445 g_assert_cmpint(obj.c, ==, 30);
446 g_assert_cmpint(obj.d, ==, 40);
447 g_assert_cmpint(obj.e, ==, 50);
448 g_assert_cmpint(obj.f, ==, 60);
449 qemu_fclose(loading);
450 }
451
452 static void test_load_skip(void)
453 {
454 uint8_t buf[] = {
455 0, 0, 0, 10, /* a */
456 0, 0, 0, 20, /* b */
457 0, 0, 0, 0, 0, 0, 0, 40, /* d */
458 0, 0, 0, 0, 0, 0, 0, 60, /* f */
459 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
460 };
461 save_buffer(buf, sizeof(buf));
462
463 QEMUFile *loading = open_test_file(false);
464 TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
465 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
466 g_assert(!qemu_file_get_error(loading));
467 g_assert_cmpint(obj.a, ==, 10);
468 g_assert_cmpint(obj.b, ==, 20);
469 g_assert_cmpint(obj.c, ==, 300);
470 g_assert_cmpint(obj.d, ==, 40);
471 g_assert_cmpint(obj.e, ==, 500);
472 g_assert_cmpint(obj.f, ==, 60);
473 qemu_fclose(loading);
474 }
475
476 typedef struct {
477 int32_t i;
478 } TestStructTriv;
479
480 const VMStateDescription vmsd_tst = {
481 .name = "test/tst",
482 .version_id = 1,
483 .minimum_version_id = 1,
484 .fields = (VMStateField[]) {
485 VMSTATE_INT32(i, TestStructTriv),
486 VMSTATE_END_OF_LIST()
487 }
488 };
489
490 #define AR_SIZE 4
491
492 typedef struct {
493 TestStructTriv *ar[AR_SIZE];
494 } TestArrayOfPtrToStuct;
495
496 const VMStateDescription vmsd_arps = {
497 .name = "test/arps",
498 .version_id = 1,
499 .minimum_version_id = 1,
500 .fields = (VMStateField[]) {
501 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
502 AR_SIZE, 0, vmsd_tst, TestStructTriv),
503 VMSTATE_END_OF_LIST()
504 }
505 };
506 static void test_arr_ptr_str_no0_save(void)
507 {
508 TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
509 TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
510 uint8_t wire_sample[] = {
511 0x00, 0x00, 0x00, 0x00,
512 0x00, 0x00, 0x00, 0x01,
513 0x00, 0x00, 0x00, 0x02,
514 0x00, 0x00, 0x00, 0x03,
515 QEMU_VM_EOF
516 };
517
518 save_vmstate(&vmsd_arps, &sample);
519 compare_vmstate(wire_sample, sizeof(wire_sample));
520 }
521
522 static void test_arr_ptr_str_no0_load(void)
523 {
524 TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
525 TestStructTriv ar[AR_SIZE] = {};
526 TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
527 int idx;
528 uint8_t wire_sample[] = {
529 0x00, 0x00, 0x00, 0x00,
530 0x00, 0x00, 0x00, 0x01,
531 0x00, 0x00, 0x00, 0x02,
532 0x00, 0x00, 0x00, 0x03,
533 QEMU_VM_EOF
534 };
535
536 save_buffer(wire_sample, sizeof(wire_sample));
537 SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
538 wire_sample, sizeof(wire_sample)));
539 for (idx = 0; idx < AR_SIZE; ++idx) {
540 /* compare the target array ar with the ground truth array ar_gt */
541 g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
542 }
543 }
544
545 /* test QTAILQ migration */
546 typedef struct TestQtailqElement TestQtailqElement;
547
548 struct TestQtailqElement {
549 bool b;
550 uint8_t u8;
551 QTAILQ_ENTRY(TestQtailqElement) next;
552 };
553
554 typedef struct TestQtailq {
555 int16_t i16;
556 QTAILQ_HEAD(TestQtailqHead, TestQtailqElement) q;
557 int32_t i32;
558 } TestQtailq;
559
560 static const VMStateDescription vmstate_q_element = {
561 .name = "test/queue-element",
562 .version_id = 1,
563 .minimum_version_id = 1,
564 .fields = (VMStateField[]) {
565 VMSTATE_BOOL(b, TestQtailqElement),
566 VMSTATE_UINT8(u8, TestQtailqElement),
567 VMSTATE_END_OF_LIST()
568 },
569 };
570
571 static const VMStateDescription vmstate_q = {
572 .name = "test/queue",
573 .version_id = 1,
574 .minimum_version_id = 1,
575 .fields = (VMStateField[]) {
576 VMSTATE_INT16(i16, TestQtailq),
577 VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
578 next),
579 VMSTATE_INT32(i32, TestQtailq),
580 VMSTATE_END_OF_LIST()
581 }
582 };
583
584 uint8_t wire_q[] = {
585 /* i16 */ 0xfe, 0x0,
586 /* start of element 0 of q */ 0x01,
587 /* .b */ 0x01,
588 /* .u8 */ 0x82,
589 /* start of element 1 of q */ 0x01,
590 /* b */ 0x00,
591 /* u8 */ 0x41,
592 /* end of q */ 0x00,
593 /* i32 */ 0x00, 0x01, 0x11, 0x70,
594 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
595 };
596
597 static void test_save_q(void)
598 {
599 TestQtailq obj_q = {
600 .i16 = -512,
601 .i32 = 70000,
602 };
603
604 TestQtailqElement obj_qe1 = {
605 .b = true,
606 .u8 = 130,
607 };
608
609 TestQtailqElement obj_qe2 = {
610 .b = false,
611 .u8 = 65,
612 };
613
614 QTAILQ_INIT(&obj_q.q);
615 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
616 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
617
618 save_vmstate(&vmstate_q, &obj_q);
619 compare_vmstate(wire_q, sizeof(wire_q));
620 }
621
622 static void test_load_q(void)
623 {
624 TestQtailq obj_q = {
625 .i16 = -512,
626 .i32 = 70000,
627 };
628
629 TestQtailqElement obj_qe1 = {
630 .b = true,
631 .u8 = 130,
632 };
633
634 TestQtailqElement obj_qe2 = {
635 .b = false,
636 .u8 = 65,
637 };
638
639 QTAILQ_INIT(&obj_q.q);
640 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
641 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
642
643 QEMUFile *fsave = open_test_file(true);
644
645 qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
646 g_assert(!qemu_file_get_error(fsave));
647 qemu_fclose(fsave);
648
649 QEMUFile *fload = open_test_file(false);
650 TestQtailq tgt;
651
652 QTAILQ_INIT(&tgt.q);
653 vmstate_load_state(fload, &vmstate_q, &tgt, 1);
654 char eof = qemu_get_byte(fload);
655 g_assert(!qemu_file_get_error(fload));
656 g_assert_cmpint(tgt.i16, ==, obj_q.i16);
657 g_assert_cmpint(tgt.i32, ==, obj_q.i32);
658 g_assert_cmpint(eof, ==, QEMU_VM_EOF);
659
660 TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q);
661 TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q, TestQtailqHead);
662 TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q);
663 TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q, TestQtailqHead);
664
665 while (1) {
666 g_assert_cmpint(qele_to->b, ==, qele_from->b);
667 g_assert_cmpint(qele_to->u8, ==, qele_from->u8);
668 if ((qele_from == qlast_from) || (qele_to == qlast_to)) {
669 break;
670 }
671 qele_from = QTAILQ_NEXT(qele_from, next);
672 qele_to = QTAILQ_NEXT(qele_to, next);
673 }
674
675 g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
676 g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
677
678 /* clean up */
679 TestQtailqElement *qele;
680 while (!QTAILQ_EMPTY(&tgt.q)) {
681 qele = QTAILQ_LAST(&tgt.q, TestQtailqHead);
682 QTAILQ_REMOVE(&tgt.q, qele, next);
683 free(qele);
684 qele = NULL;
685 }
686 qemu_fclose(fload);
687 }
688
689 typedef struct TmpTestStruct {
690 TestStruct *parent;
691 int64_t diff;
692 } TmpTestStruct;
693
694 static void tmp_child_pre_save(void *opaque)
695 {
696 struct TmpTestStruct *tts = opaque;
697
698 tts->diff = tts->parent->b - tts->parent->a;
699 }
700
701 static int tmp_child_post_load(void *opaque, int version_id)
702 {
703 struct TmpTestStruct *tts = opaque;
704
705 tts->parent->b = tts->parent->a + tts->diff;
706
707 return 0;
708 }
709
710 static const VMStateDescription vmstate_tmp_back_to_parent = {
711 .name = "test/tmp_child_parent",
712 .fields = (VMStateField[]) {
713 VMSTATE_UINT64(f, TestStruct),
714 VMSTATE_END_OF_LIST()
715 }
716 };
717
718 static const VMStateDescription vmstate_tmp_child = {
719 .name = "test/tmp_child",
720 .pre_save = tmp_child_pre_save,
721 .post_load = tmp_child_post_load,
722 .fields = (VMStateField[]) {
723 VMSTATE_INT64(diff, TmpTestStruct),
724 VMSTATE_STRUCT_POINTER(parent, TmpTestStruct,
725 vmstate_tmp_back_to_parent, TestStruct),
726 VMSTATE_END_OF_LIST()
727 }
728 };
729
730 static const VMStateDescription vmstate_with_tmp = {
731 .name = "test/with_tmp",
732 .version_id = 1,
733 .fields = (VMStateField[]) {
734 VMSTATE_UINT32(a, TestStruct),
735 VMSTATE_UINT64(d, TestStruct),
736 VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child),
737 VMSTATE_END_OF_LIST()
738 }
739 };
740
741 static void obj_tmp_copy(void *target, void *source)
742 {
743 memcpy(target, source, sizeof(TestStruct));
744 }
745
746 static void test_tmp_struct(void)
747 {
748 TestStruct obj, obj_clone;
749
750 uint8_t const wire_with_tmp[] = {
751 /* u32 a */ 0x00, 0x00, 0x00, 0x02,
752 /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
753 /* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
754 /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
755 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
756 };
757
758 memset(&obj, 0, sizeof(obj));
759 obj.a = 2;
760 obj.b = 4;
761 obj.d = 1;
762 obj.f = 8;
763 save_vmstate(&vmstate_with_tmp, &obj);
764
765 compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
766
767 memset(&obj, 0, sizeof(obj));
768 SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone,
769 obj_tmp_copy, 1, wire_with_tmp,
770 sizeof(wire_with_tmp)));
771 g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */
772 g_assert_cmpint(obj.b, ==, 4); /* from the post_load */
773 g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */
774 g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
775 }
776
777 int main(int argc, char **argv)
778 {
779 temp_fd = mkstemp(temp_file);
780
781 module_call_init(MODULE_INIT_QOM);
782
783 g_test_init(&argc, &argv, NULL);
784 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
785 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
786 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
787 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
788 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
789 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
790 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
791 g_test_add_func("/vmstate/array/ptr/str/no0/save",
792 test_arr_ptr_str_no0_save);
793 g_test_add_func("/vmstate/array/ptr/str/no0/load",
794 test_arr_ptr_str_no0_load);
795 g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q);
796 g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q);
797 g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
798 g_test_run();
799
800 close(temp_fd);
801 unlink(temp_file);
802
803 return 0;
804 }