Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Finally kill the now empty ModelChecker class
[simgrid.git] / src / mc / sosp / Region.cpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/sosp/Region.hpp"
7 #include "src/mc/mc_config.hpp"
8 #include "src/mc/mc_forward.hpp"
9 #include "src/mc/sosp/RemoteProcessMemory.hpp"
10
11 #include <cstdlib>
12 #include <sys/mman.h>
13 #ifdef __FreeBSD__
14 #define MAP_POPULATE MAP_PREFAULT_READ
15 #endif
16
17 namespace simgrid::mc {
18
19 Region::Region(PageStore& store, RemoteProcessMemory& memory, RegionType region_type, void* start_addr, size_t size)
20     : region_type_(region_type), start_addr_(start_addr), size_(size)
21 {
22   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize - 1)) == 0, "Start address not at the beginning of a page");
23
24   chunks_ = ChunkedData(store, memory, RemotePtr<void>(start_addr), mmu::chunk_count(size));
25 }
26
27 /** @brief Restore a region from a snapshot
28  *
29  *  @param region     Target region
30  */
31 void Region::restore(RemoteProcessMemory& memory) const
32 {
33   xbt_assert(((start().address()) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
34   xbt_assert(simgrid::mc::mmu::chunk_count(size()) == get_chunks().page_count());
35
36   for (size_t i = 0; i != get_chunks().page_count(); ++i) {
37     auto* target_page       = (void*)simgrid::mc::mmu::join(i, (std::uintptr_t)(void*)start().address());
38     const void* source_page = get_chunks().page(i);
39     memory.write_bytes(source_page, xbt_pagesize, remote(target_page));
40   }
41 }
42
43 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, const simgrid::mc::Region* region)
44 {
45   auto [pageno, offset] = simgrid::mc::mmu::split(addr - region->start().address());
46   void* snapshot_page   = region->get_chunks().page(pageno);
47   return (char*)snapshot_page + offset;
48 }
49
50 void* Region::read(void* target, const void* addr, std::size_t size) const
51 {
52   xbt_assert(contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
53
54   // Last byte of the region:
55   const void* end_addr = (const char*)addr + size - 1;
56   if (simgrid::mc::mmu::same_chunk((std::uintptr_t)addr, (std::uintptr_t)end_addr)) {
57     // The memory is contained in a single page:
58     return mc_translate_address_region((uintptr_t)addr, this);
59   }
60   // Otherwise, the memory spans several pages. Let's copy it all into the provided buffer
61   xbt_assert(target != nullptr, "Missing destination buffer for fragmented memory access");
62
63   // TODO, we assume the chunks are aligned to natural chunk boundaries.
64   // We should remove this assumption.
65
66   // Page of the last byte of the memory area:
67   size_t page_end = simgrid::mc::mmu::split((std::uintptr_t)end_addr).first;
68
69   void* dest = target; // iterator in the buffer to where we should copy next
70
71   // Read each page:
72   while (simgrid::mc::mmu::split((std::uintptr_t)addr).first != page_end) {
73     const void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, this);
74     auto* next_page     = (void*)simgrid::mc::mmu::join(simgrid::mc::mmu::split((std::uintptr_t)addr).first + 1, 0);
75     size_t readable     = (char*)next_page - (const char*)addr;
76     memcpy(dest, snapshot_addr, readable);
77     addr = (const char*)addr + readable;
78     dest = (char*)dest + readable;
79     size -= readable;
80   }
81
82   // Read the end:
83   const void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, this);
84   memcpy(dest, snapshot_addr, size);
85
86   return target;
87 }
88
89 } // namespace simgrid::mc
90
91 /** Compare memory between snapshots (with known regions)
92  *
93  * @param addr1 Address in the first snapshot
94  * @param region1 Region of the address in the first snapshot
95  * @param addr2 Address in the second snapshot
96  * @param region2 Region of the address in the second snapshot
97  * @return same semantic as memcmp
98  */
99 int MC_snapshot_region_memcmp(const void* addr1, const simgrid::mc::Region* region1, const void* addr2,
100                               const simgrid::mc::Region* region2, size_t size)
101 {
102   // Using alloca() for large allocations may trigger stack overflow:
103   // use malloc if the buffer is too big.
104   bool stack_alloc    = size < 64;
105   void* buffer1a      = stack_alloc ? alloca(size) : ::operator new(size);
106   void* buffer2a      = stack_alloc ? alloca(size) : ::operator new(size);
107   const void* buffer1 = region1->read(buffer1a, addr1, size);
108   const void* buffer2 = region2->read(buffer2a, addr2, size);
109   int res;
110   if (buffer1 == buffer2)
111     res = 0;
112   else
113     res = memcmp(buffer1, buffer2, size);
114   if (not stack_alloc) {
115     ::operator delete(buffer1a);
116     ::operator delete(buffer2a);
117   }
118   return res;
119 }