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 static inline __attribute__((always_inline))
29 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region)
31 size_t pageno = mc_page_number(region->start_addr, (void*) addr);
32 size_t snapshot_pageno = region->page_numbers[pageno];
33 const void* snapshot_page = mc_page_store_get_page(mc_model_checker->pages, snapshot_pageno);
34 return (char*) snapshot_page + mc_page_offset((void*) addr);
37 /** \brief Translate a pointer from process address space to snapshot address space
39 * The address space contains snapshot of the main/application memory:
40 * this function finds the address in a given snaphot for a given
41 * real/application address.
43 * For read only memory regions and other regions which are not int the
44 * snapshot, the address is not changed.
46 * \param addr Application address
47 * \param snapshot The snapshot of interest (if NULL no translation is done)
48 * \return Translated address in the snapshot address space
50 static inline __attribute__((always_inline))
51 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot)
54 // If not in a process state/clone:
56 return (uintptr_t *) addr;
59 mc_mem_region_t region = mc_get_snapshot_region((void*) addr, snapshot);
61 xbt_assert(mc_region_contain(region, (void*) addr), "Trying to read out of the region boundary.");
68 else if (region->data) {
69 uintptr_t offset = addr - (uintptr_t) region->start_addr;
70 return (void *) ((uintptr_t) region->data + offset);
74 else if (region->page_numbers) {
75 return mc_translate_address_region(addr, region);
79 xbt_die("No data for this memory region");
83 /** @brief Read memory from a snapshot region broken across fragmented pages
85 * @param addr Process (non-snapshot) address of the data
86 * @param region Snapshot memory region where the data is located
87 * @param target Buffer to store the value
88 * @param size Size of the data to read in bytes
89 * @return Pointer where the data is located (target buffer of original location)
91 static void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
93 void* end = (char*) addr + size - 1;
94 size_t page_end = mc_page_number(NULL, end);
98 while (mc_page_number(NULL, addr) != page_end) {
99 void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
100 void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
101 size_t readable = (char*) next_page - (char*) addr;
102 memcpy(dest, snapshot_addr, readable);
103 addr = (char*) addr + readable;
104 dest = (char*) dest + readable;
109 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
110 memcpy(dest, snapshot_addr, size);
115 /** @brief Read memory from a snapshot region
117 * @param addr Process (non-snapshot) address of the data
118 * @param region Snapshot memory region where the data is located
119 * @param target Buffer to store the value
120 * @param size Size of the data to read in bytes
121 * @return Pointer where the data is located (target buffer of original location)
123 void* mc_snapshot_read_region(void* addr, mc_mem_region_t region, void* target, size_t size)
125 uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start_addr;
127 xbt_assert(addr >= region->start_addr && (char*) addr+size < (char*)region->start_addr+region->size,
128 "Trying to read out of the region boundary.");
130 // Linear memory region:
132 return (void*) ((uintptr_t) region->data + offset);
135 // Fragmented memory region:
136 else if (region->page_numbers) {
137 void* end = (char*) addr + size - 1;
138 if( mc_same_page(addr, end) ) {
139 // The memory is contained in a single page:
140 return mc_translate_address_region((uintptr_t) addr, region);
142 // The memory spans several pages:
143 return mc_snapshot_read_fragmented(addr, region, target, size);
148 xbt_die("No data available for this region");
152 /** @brief Read memory from a snapshot
154 * @param addr Process (non-snapshot) address of the data
155 * @param snapshot Snapshot (or NULL is no snapshot)
156 * @param target Buffer to store the value
157 * @param size Size of the data to read in bytes
158 * @return Pointer where the data is located (target buffer of original location)
160 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size)
163 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot);
164 return mc_snapshot_read_region(addr, region, target, size);
170 /** Compare memory between snapshots (with known regions)
172 * @param addr1 Address in the first snapshot
173 * @param snapshot2 Region of the address in the first snapshot
174 * @param addr2 Address in the second snapshot
175 * @param snapshot2 Region of the address in the second snapshot
176 * @return same as memcmp
178 int mc_snapshot_region_memcp(
179 void* addr1, mc_mem_region_t region1,
180 void* addr2, mc_mem_region_t region2, size_t size)
182 // Using alloca() for large allocations may trigger stack overflow:
183 // use malloc if the buffer is too big.
185 bool stack_alloc = size < 64;
186 void* buffer = stack_alloc ? alloca(2*size) : malloc(2*size);
187 void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer, size);
188 void* buffer2 = mc_snapshot_read_region(addr2, region2, (char*) buffer + size, size);
190 if (buffer1 == buffer2) {
193 res = memcmp(buffer1, buffer2, size);
201 /** Compare memory between snapshots
203 * @param addr1 Address in the first snapshot
204 * @param snapshot1 First snapshot
205 * @param addr2 Address in the second snapshot
206 * @param snapshot2 Second snapshot
207 * @return same as memcmp
209 int mc_snapshot_memcp(
210 void* addr1, mc_snapshot_t snapshot1,
211 void* addr2, mc_snapshot_t snapshot2, size_t size)
213 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1);
214 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2);
215 return mc_snapshot_region_memcp(addr1, region1, addr2, region2, size);