]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-vmstate.c
Merge remote-tracking branch 'ehabkost/tags/numa-pull-request' into staging
[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 "migration/qemu-file.h"
31 #include "../migration/qemu-file-channel.h"
32 #include "qemu/coroutine.h"
33 #include "io/channel-file.h"
34
35 static char temp_file[] = "/tmp/vmst.test.XXXXXX";
36 static int temp_fd;
37
38
39 /* Duplicate temp_fd and seek to the beginning of the file */
40 static QEMUFile *open_test_file(bool write)
41 {
42 int fd = dup(temp_fd);
43 QIOChannel *ioc;
44 QEMUFile *f;
45
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_fopen_channel_output(ioc);
53 } else {
54 f = qemu_fopen_channel_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 vmstate_save_state(f, desc, obj, NULL);
72 qemu_put_byte(f, QEMU_VM_EOF);
73 g_assert(!qemu_file_get_error(f));
74 qemu_fclose(f);
75 }
76
77 static void save_buffer(const uint8_t *buf, size_t buf_size)
78 {
79 QEMUFile *fsave = open_test_file(true);
80 qemu_put_buffer(fsave, buf, buf_size);
81 qemu_fclose(fsave);
82 }
83
84 static void compare_vmstate(const uint8_t *wire, size_t size)
85 {
86 QEMUFile *f = open_test_file(false);
87 uint8_t result[size];
88
89 /* read back as binary */
90
91 g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
92 sizeof(result));
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, sizeof(result)));
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 = (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 TestStruct {
285 uint32_t a, b, c, e;
286 uint64_t d, f;
287 bool skip_c_e;
288 } TestStruct;
289
290 static const VMStateDescription vmstate_versioned = {
291 .name = "test/versioned",
292 .version_id = 2,
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.
298 */
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()
304 }
305 };
306
307 static void test_load_v1(void)
308 {
309 uint8_t buf[] = {
310 0, 0, 0, 10, /* a */
311 0, 0, 0, 30, /* c */
312 0, 0, 0, 0, 0, 0, 0, 40, /* d */
313 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
314 };
315 save_buffer(buf, sizeof(buf));
316
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);
328 }
329
330 static void test_load_v2(void)
331 {
332 uint8_t buf[] = {
333 0, 0, 0, 10, /* a */
334 0, 0, 0, 20, /* b */
335 0, 0, 0, 30, /* c */
336 0, 0, 0, 0, 0, 0, 0, 40, /* d */
337 0, 0, 0, 50, /* e */
338 0, 0, 0, 0, 0, 0, 0, 60, /* f */
339 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
340 };
341 save_buffer(buf, sizeof(buf));
342
343 QEMUFile *loading = open_test_file(false);
344 TestStruct obj;
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);
353 }
354
355 static bool test_skip(void *opaque, int version_id)
356 {
357 TestStruct *t = (TestStruct *)opaque;
358 return !t->skip_c_e;
359 }
360
361 static const VMStateDescription vmstate_skipping = {
362 .name = "test/skip",
363 .version_id = 2,
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()
373 }
374 };
375
376
377 static void test_save_noskip(void)
378 {
379 QEMUFile *fsave = open_test_file(true);
380 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
381 .skip_c_e = false };
382 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
383 g_assert(!qemu_file_get_error(fsave));
384
385 uint8_t expected[] = {
386 0, 0, 0, 1, /* a */
387 0, 0, 0, 2, /* b */
388 0, 0, 0, 3, /* c */
389 0, 0, 0, 0, 0, 0, 0, 4, /* d */
390 0, 0, 0, 5, /* e */
391 0, 0, 0, 0, 0, 0, 0, 6, /* f */
392 };
393
394 qemu_fclose(fsave);
395 compare_vmstate(expected, sizeof(expected));
396 }
397
398 static void test_save_skip(void)
399 {
400 QEMUFile *fsave = open_test_file(true);
401 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
402 .skip_c_e = true };
403 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
404 g_assert(!qemu_file_get_error(fsave));
405
406 uint8_t expected[] = {
407 0, 0, 0, 1, /* a */
408 0, 0, 0, 2, /* b */
409 0, 0, 0, 0, 0, 0, 0, 4, /* d */
410 0, 0, 0, 0, 0, 0, 0, 6, /* f */
411 };
412
413 qemu_fclose(fsave);
414 compare_vmstate(expected, sizeof(expected));
415 }
416
417 static void test_load_noskip(void)
418 {
419 uint8_t buf[] = {
420 0, 0, 0, 10, /* a */
421 0, 0, 0, 20, /* b */
422 0, 0, 0, 30, /* c */
423 0, 0, 0, 0, 0, 0, 0, 40, /* d */
424 0, 0, 0, 50, /* e */
425 0, 0, 0, 0, 0, 0, 0, 60, /* f */
426 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
427 };
428 save_buffer(buf, sizeof(buf));
429
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);
441 }
442
443 static void test_load_skip(void)
444 {
445 uint8_t buf[] = {
446 0, 0, 0, 10, /* a */
447 0, 0, 0, 20, /* b */
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 */
451 };
452 save_buffer(buf, sizeof(buf));
453
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);
465 }
466
467 typedef struct {
468 int32_t i;
469 } TestStructTriv;
470
471 const VMStateDescription vmsd_tst = {
472 .name = "test/tst",
473 .version_id = 1,
474 .minimum_version_id = 1,
475 .fields = (VMStateField[]) {
476 VMSTATE_INT32(i, TestStructTriv),
477 VMSTATE_END_OF_LIST()
478 }
479 };
480
481 /* test array migration */
482
483 #define AR_SIZE 4
484
485 typedef struct {
486 TestStructTriv *ar[AR_SIZE];
487 } TestArrayOfPtrToStuct;
488
489 const VMStateDescription vmsd_arps = {
490 .name = "test/arps",
491 .version_id = 1,
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()
497 }
498 };
499
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,
505 QEMU_VM_EOF
506 };
507
508 static void test_arr_ptr_str_no0_save(void)
509 {
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]} };
512
513 save_vmstate(&vmsd_arps, &sample);
514 compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
515 }
516
517 static void test_arr_ptr_str_no0_load(void)
518 {
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]} };
522 int idx;
523
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);
530 }
531 }
532
533 static uint8_t wire_arr_ptr_0[] = {
534 0x00, 0x00, 0x00, 0x00,
535 VMS_NULLPTR_MARKER,
536 0x00, 0x00, 0x00, 0x02,
537 0x00, 0x00, 0x00, 0x03,
538 QEMU_VM_EOF
539 };
540
541 static void test_arr_ptr_str_0_save(void)
542 {
543 TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
544 TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
545
546 save_vmstate(&vmsd_arps, &sample);
547 compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
548 }
549
550 static void test_arr_ptr_str_0_load(void)
551 {
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]} };
555 int idx;
556
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);
563 }
564 for (idx = 0; idx < AR_SIZE; ++idx) {
565 if (idx == 1) {
566 g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0);
567 } else {
568 g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0);
569 }
570 }
571 }
572
573 typedef struct TestArrayOfPtrToInt {
574 int32_t *ar[AR_SIZE];
575 } TestArrayOfPtrToInt;
576
577 const VMStateDescription vmsd_arpp = {
578 .name = "test/arps",
579 .version_id = 1,
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()
585 }
586 };
587
588 static void test_arr_ptr_prim_0_save(void)
589 {
590 int32_t ar[AR_SIZE] = {0 , 1, 2, 3};
591 TestArrayOfPtrToInt sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
592
593 save_vmstate(&vmsd_arpp, &sample);
594 compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
595 }
596
597 static void test_arr_ptr_prim_0_load(void)
598 {
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]} };
602 int idx;
603
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 */
609 if (idx == 1) {
610 g_assert_cmpint(42, ==, ar[idx]);
611 } else {
612 g_assert_cmpint(ar_gt[idx], ==, ar[idx]);
613 }
614 }
615 }
616
617 /* test QTAILQ migration */
618 typedef struct TestQtailqElement TestQtailqElement;
619
620 struct TestQtailqElement {
621 bool b;
622 uint8_t u8;
623 QTAILQ_ENTRY(TestQtailqElement) next;
624 };
625
626 typedef struct TestQtailq {
627 int16_t i16;
628 QTAILQ_HEAD(TestQtailqHead, TestQtailqElement) q;
629 int32_t i32;
630 } TestQtailq;
631
632 static const VMStateDescription vmstate_q_element = {
633 .name = "test/queue-element",
634 .version_id = 1,
635 .minimum_version_id = 1,
636 .fields = (VMStateField[]) {
637 VMSTATE_BOOL(b, TestQtailqElement),
638 VMSTATE_UINT8(u8, TestQtailqElement),
639 VMSTATE_END_OF_LIST()
640 },
641 };
642
643 static const VMStateDescription vmstate_q = {
644 .name = "test/queue",
645 .version_id = 1,
646 .minimum_version_id = 1,
647 .fields = (VMStateField[]) {
648 VMSTATE_INT16(i16, TestQtailq),
649 VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
650 next),
651 VMSTATE_INT32(i32, TestQtailq),
652 VMSTATE_END_OF_LIST()
653 }
654 };
655
656 uint8_t wire_q[] = {
657 /* i16 */ 0xfe, 0x0,
658 /* start of element 0 of q */ 0x01,
659 /* .b */ 0x01,
660 /* .u8 */ 0x82,
661 /* start of element 1 of q */ 0x01,
662 /* b */ 0x00,
663 /* u8 */ 0x41,
664 /* end of q */ 0x00,
665 /* i32 */ 0x00, 0x01, 0x11, 0x70,
666 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
667 };
668
669 static void test_save_q(void)
670 {
671 TestQtailq obj_q = {
672 .i16 = -512,
673 .i32 = 70000,
674 };
675
676 TestQtailqElement obj_qe1 = {
677 .b = true,
678 .u8 = 130,
679 };
680
681 TestQtailqElement obj_qe2 = {
682 .b = false,
683 .u8 = 65,
684 };
685
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);
689
690 save_vmstate(&vmstate_q, &obj_q);
691 compare_vmstate(wire_q, sizeof(wire_q));
692 }
693
694 static void test_load_q(void)
695 {
696 TestQtailq obj_q = {
697 .i16 = -512,
698 .i32 = 70000,
699 };
700
701 TestQtailqElement obj_qe1 = {
702 .b = true,
703 .u8 = 130,
704 };
705
706 TestQtailqElement obj_qe2 = {
707 .b = false,
708 .u8 = 65,
709 };
710
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);
714
715 QEMUFile *fsave = open_test_file(true);
716
717 qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
718 g_assert(!qemu_file_get_error(fsave));
719 qemu_fclose(fsave);
720
721 QEMUFile *fload = open_test_file(false);
722 TestQtailq tgt;
723
724 QTAILQ_INIT(&tgt.q);
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);
731
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);
736
737 while (1) {
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)) {
741 break;
742 }
743 qele_from = QTAILQ_NEXT(qele_from, next);
744 qele_to = QTAILQ_NEXT(qele_to, next);
745 }
746
747 g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
748 g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
749
750 /* clean up */
751 TestQtailqElement *qele;
752 while (!QTAILQ_EMPTY(&tgt.q)) {
753 qele = QTAILQ_LAST(&tgt.q, TestQtailqHead);
754 QTAILQ_REMOVE(&tgt.q, qele, next);
755 free(qele);
756 qele = NULL;
757 }
758 qemu_fclose(fload);
759 }
760
761 typedef struct TmpTestStruct {
762 TestStruct *parent;
763 int64_t diff;
764 } TmpTestStruct;
765
766 static void tmp_child_pre_save(void *opaque)
767 {
768 struct TmpTestStruct *tts = opaque;
769
770 tts->diff = tts->parent->b - tts->parent->a;
771 }
772
773 static int tmp_child_post_load(void *opaque, int version_id)
774 {
775 struct TmpTestStruct *tts = opaque;
776
777 tts->parent->b = tts->parent->a + tts->diff;
778
779 return 0;
780 }
781
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()
787 }
788 };
789
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()
799 }
800 };
801
802 static const VMStateDescription vmstate_with_tmp = {
803 .name = "test/with_tmp",
804 .version_id = 1,
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()
810 }
811 };
812
813 static void obj_tmp_copy(void *target, void *source)
814 {
815 memcpy(target, source, sizeof(TestStruct));
816 }
817
818 static void test_tmp_struct(void)
819 {
820 TestStruct obj, obj_clone;
821
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 */
828 };
829
830 memset(&obj, 0, sizeof(obj));
831 obj.a = 2;
832 obj.b = 4;
833 obj.d = 1;
834 obj.f = 8;
835 save_vmstate(&vmstate_with_tmp, &obj);
836
837 compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
838
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 */
847 }
848
849 int main(int argc, char **argv)
850 {
851 temp_fd = mkstemp(temp_file);
852
853 module_call_init(MODULE_INIT_QOM);
854
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);
877 g_test_run();
878
879 close(temp_fd);
880 unlink(temp_file);
881
882 return 0;
883 }