1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "mc_private.h"
11 #include "mc_page_store.h"
13 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot)
15 for (size_t i = 0; i != NB_REGIONS; ++i) {
16 mc_mem_region_t region = snapshot->regions[i];
17 void* start = region->start_addr;
18 void* end = (char*) start + region->size;
20 if (addr >= start && addr < end) {
28 /** @brief Read memory from a snapshot region broken across fragmented pages
30 * @param addr Process (non-snapshot) address of the data
31 * @param region Snapshot memory region where the data is located
32 * @param target Buffer to store the value
33 * @param size Size of the data to read in bytes
34 * @return Pointer where the data is located (target buffer of original location)
36 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
38 void* end = (char*) addr + size - 1;
39 size_t page_end = mc_page_number(NULL, end);
43 xbt_die("Missing destination buffer for fragmented memory access");
47 while (mc_page_number(NULL, addr) != page_end) {
48 void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
49 void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
50 size_t readable = (char*) next_page - (char*) addr;
51 memcpy(dest, snapshot_addr, readable);
52 addr = (char*) addr + readable;
53 dest = (char*) dest + readable;
58 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
59 memcpy(dest, snapshot_addr, size);
64 /** @brief Read memory from a snapshot
66 * @param addr Process (non-snapshot) address of the data
67 * @param snapshot Snapshot (or NULL is no snapshot)
68 * @param target Buffer to store the value
69 * @param size Size of the data to read in bytes
70 * @return Pointer where the data is located (target buffer of original location)
72 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size)
75 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot);
76 return mc_snapshot_read_region(addr, region, target, size);
82 /** Compare memory between snapshots (with known regions)
84 * @param addr1 Address in the first snapshot
85 * @param snapshot2 Region of the address in the first snapshot
86 * @param addr2 Address in the second snapshot
87 * @param snapshot2 Region of the address in the second snapshot
88 * @return same as memcmp
90 int mc_snapshot_region_memcmp(
91 void* addr1, mc_mem_region_t region1,
92 void* addr2, mc_mem_region_t region2, size_t size)
94 // Using alloca() for large allocations may trigger stack overflow:
95 // use malloc if the buffer is too big.
96 bool stack_alloc = size < 64;
97 void* buffer1a = (region1==NULL || region1->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
98 void* buffer2a = (region2==NULL || region2->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
99 void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer1a, size);
100 void* buffer2 = mc_snapshot_read_region(addr2, region2, buffer2a, size);
102 if (buffer1 == buffer2) {
105 res = memcmp(buffer1, buffer2, size);
114 /** Compare memory between snapshots
116 * @param addr1 Address in the first snapshot
117 * @param snapshot1 First snapshot
118 * @param addr2 Address in the second snapshot
119 * @param snapshot2 Second snapshot
120 * @return same as memcmp
122 int mc_snapshot_memcp(
123 void* addr1, mc_snapshot_t snapshot1,
124 void* addr2, mc_snapshot_t snapshot2, size_t size)
126 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1);
127 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2);
128 return mc_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
136 #include <sys/mman.h>
138 #include "mc/mc_private.h"
140 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
142 static inline void init_memory(void* mem, size_t size)
145 hash = ((hash << 5) + hash) + (uintptr_t) size;
146 hash = ((hash << 5) + hash) + size;
147 char* dest = (char*) mem;
148 for (int i=0; i!=size; ++i) {
149 hash = ((hash << 5) + hash) + size;
150 dest[i] = hash & 255;
154 XBT_TEST_UNIT("page_region", test_snapshot_page_read, "Test page snapshots")
156 xbt_test_add("Initialisation");
157 _sg_mc_soft_dirty = 0;
158 xbt_assert(xbt_pagesize == getpagesize());
159 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
160 mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
161 mc_model_checker->pages = mc_pages_store_new();
163 for(int n=1; n!=10; ++n) {
165 // Store region page(s):
166 size_t byte_size = n * xbt_pagesize;
167 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
168 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
170 // Init memory and take snapshots:
171 init_memory(source, byte_size);
172 mc_mem_region_t region0 = mc_region_new_sparse(0, source, byte_size, NULL);
173 for(int i=1; i<n; i+=2) {
174 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
176 mc_mem_region_t region = mc_region_new_sparse(0, source, byte_size, NULL);
178 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
179 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
181 xbt_test_add("Reading region data for %i page(s)", n);
182 for(int j=0; j!=100; ++j) {
183 size_t offset = rand() % byte_size;
184 size_t size = rand() % (byte_size - offset);
185 void* read = mc_snapshot_read_region((char*) source+offset, region, destination, size);
186 xbt_test_assert(!memcmp((char*) source+offset, read, size), "Mismatch in mc_snapshot_read_region()");
189 xbt_test_add("Compare region data for %i page(s)", n);
190 for(int j=0; j!=100; ++j) {
191 size_t offset = rand() % byte_size;
192 size_t size = rand() % (byte_size - offset);
193 xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
194 "Mismatch in mc_snapshot_region_memcmp()");
199 xbt_test_add("Read pointer for %i page(s)", n);
200 memcpy(source, &mc_model_checker, sizeof(void*));
201 mc_mem_region_t region2 = mc_region_new_sparse(0, source, byte_size, NULL);
202 xbt_test_assert(mc_snapshot_read_pointer_region(source, region2) == mc_model_checker,
203 "Mismtach in mc_snapshot_read_pointer_region()");
204 MC_region_destroy(region2);
207 MC_region_destroy(region);
208 MC_region_destroy(region0);
209 munmap(destination, byte_size);
210 munmap(source, byte_size);
213 mc_pages_store_delete(mc_model_checker->pages);
214 xbt_free(mc_model_checker);
215 mc_model_checker = NULL;
218 #endif /* SIMGRID_TEST */