Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move the stack as field of SafetyChecker and CommDetChecker
[simgrid.git] / src / mc / Process.hpp
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_MC_PROCESS_H
8 #define SIMGRID_MC_PROCESS_H
9
10 #include <cstdint>
11 #include <cstddef>
12
13 #include <type_traits>
14 #include <vector>
15 #include <memory>
16
17 #include <sys/types.h>
18
19 #include <simgrid_config.h>
20
21 #include <xbt/base.h>
22 #include <xbt/mmalloc.h>
23
24 #include "src/xbt/mmalloc/mmprivate.h"
25 #include "src/mc/Channel.hpp"
26
27 #include <simgrid/simix.h>
28 #include "src/simix/popping_private.h"
29 #include "src/simix/smx_private.h"
30
31 #include "src/xbt/memory_map.hpp"
32
33 #include "src/mc/mc_forward.hpp"
34 #include "src/mc/mc_base.h"
35 #include "src/mc/AddressSpace.hpp"
36 #include "src/mc/mc_protocol.h"
37 #include "src/mc/ObjectInformation.hpp"
38
39
40 namespace simgrid {
41 namespace mc {
42
43 class SimixProcessInformation {
44 public:
45   /** MCed address of the process */
46   void* address = nullptr;
47   union {
48     /** (Flat) Copy of the process data structure */
49     struct s_smx_process copy;
50   };
51   /** Hostname (owned by `mc_modelchecker->hostnames`) */
52   const char* hostname = nullptr;
53   std::string name;
54
55   void clear()
56   {
57     name.clear();
58     address = nullptr;
59     hostname = nullptr;
60   }
61 };
62
63 struct IgnoredRegion {
64   std::uint64_t addr;
65   std::size_t size;
66 };
67
68 struct IgnoredHeapRegion {
69   int block;
70   int fragment;
71   void *address;
72   std::size_t size;
73 };
74
75 /** Representation of a process
76  *
77  *  This class is mixing a lot of differents responsabilities and is tied
78  *  to SIMIX. It should probably split into different classes.
79  *
80  *  Responsabilities:
81  *
82  *  - reading from the process memory (`AddressSpace`);
83  *  - accessing the system state of the porcess (heap, …);
84  *  - storing the SIMIX state of the process;
85  *  - privatization;
86  *  - communication with the model-checked process;
87  *  - stack unwinding;
88  *  - etc.
89  */
90 class Process final : public AddressSpace {
91 private:
92   // Those flags are used to track down which cached information
93   // is still up to date and which information needs to be updated.
94   static constexpr int cache_none = 0;
95   static constexpr int cache_heap = 1;
96   static constexpr int cache_malloc = 2;
97   static constexpr int cache_simix_processes = 4;
98 public:
99   Process(pid_t pid, int sockfd);
100   ~Process();
101   void init();
102
103   Process(Process const&) = delete;
104   Process(Process &&) = delete;
105   Process& operator=(Process const&) = delete;
106   Process& operator=(Process &&) = delete;
107
108   // Read memory:
109   const void* read_bytes(void* buffer, std::size_t size,
110     RemotePtr<void> address, int process_index = ProcessIndexAny,
111     ReadOptions options = ReadOptions::none()) const override;
112   void read_variable(const char* name, void* target, size_t size) const;
113   template<class T>
114   T read_variable(const char *name) const
115   {
116     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
117     T res;
118     read_variable(name, &res, sizeof(T));
119     return res;
120   }
121   char* read_string(RemotePtr<void> address) const;
122
123   // Write memory:
124   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
125   void clear_bytes(RemotePtr<void> address, size_t len);
126
127   // Debug information:
128   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
129   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
130   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
131   simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
132   simgrid::mc::Variable* find_variable(const char* name) const;
133
134   // Heap access:
135   xbt_mheap_t get_heap()
136   {
137     if (!(this->cache_flags_ & Process::cache_heap))
138       this->refresh_heap();
139     return this->heap.get();
140   }
141   malloc_info* get_malloc_info()
142   {
143     if (!(this->cache_flags_ & Process::cache_malloc))
144       this->refresh_malloc_info();
145     return this->heap_info.data();
146   }
147   
148   void clear_cache()
149   {
150     this->cache_flags_ = Process::cache_none;
151   }
152
153   Channel const& getChannel() const { return channel_; }
154   Channel& getChannel() { return channel_; }
155
156   std::vector<IgnoredRegion> const& ignored_regions() const
157   {
158     return ignored_regions_;
159   }
160   void ignore_region(std::uint64_t address, std::size_t size);
161
162   pid_t pid() const { return pid_; }
163
164   bool in_maestro_stack(RemotePtr<void> p) const
165   {
166     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
167   }
168
169   bool running() const
170   {
171     return running_;
172   }
173
174   void terminate()
175   {
176     running_ = false;
177   }
178
179   void reset_soft_dirty();
180   void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
181
182   bool privatized(ObjectInformation const& info) const
183   {
184     return privatized_ && info.executable();
185   }
186   bool privatized() const
187   {
188     return privatized_;
189   }
190   void privatized(bool privatized) { privatized_ = privatized; }
191
192   void ignore_global_variable(const char* name)
193   {
194     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
195         this->object_infos)
196       info->remove_global_variable(name);
197   }
198
199   std::vector<s_stack_region_t>& stack_areas()
200   {
201     return stack_areas_;
202   }
203   std::vector<s_stack_region_t> const& stack_areas() const
204   {
205     return stack_areas_;
206   }
207
208   std::vector<IgnoredHeapRegion> const& ignored_heap() const
209   {
210     return ignored_heap_;
211   }
212   void ignore_heap(IgnoredHeapRegion const& region);
213   void unignore_heap(void *address, size_t size);
214
215   void ignore_local_variable(const char *var_name, const char *frame_name);
216   std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
217   std::vector<simgrid::mc::SimixProcessInformation>& old_simix_processes();
218
219   void dumpStack();
220
221 private:
222   void init_memory_map_info();
223   void refresh_heap();
224   void refresh_malloc_info();
225   void refresh_simix();
226
227 private:
228   pid_t pid_ = -1;
229   Channel channel_;
230   bool running_ = false;
231   std::vector<simgrid::xbt::VmMap> memory_map_;
232   RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
233   int memory_file = -1;
234   std::vector<IgnoredRegion> ignored_regions_;
235   int clear_refs_fd_ = -1;
236   int pagemap_fd_ = -1;
237   bool privatized_ = false;
238   std::vector<s_stack_region_t> stack_areas_;
239   std::vector<IgnoredHeapRegion> ignored_heap_;
240
241 public: // object info
242   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
243   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
244   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
245   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
246
247 public: // Copies of MCed SMX data structures
248   /** Copy of `simix_global->process_list`
249    *
250    *  See mc_smx.c.
251    */
252   std::vector<SimixProcessInformation> smx_process_infos;
253
254   /** Copy of `simix_global->process_to_destroy`
255    *
256    *  See mc_smx.c.
257    */
258   std::vector<SimixProcessInformation> smx_old_process_infos;
259
260 private:
261   /** State of the cache (which variables are up to date) */
262   int cache_flags_ = Process::cache_none;
263
264 public:
265   /** Address of the heap structure in the MCed process. */
266   void* heap_address;
267
268   /** Copy of the heap structure of the process
269    *
270    *  This is refreshed with the `MC_process_refresh` call.
271    *  This is not used if the process is the current one:
272    *  use `get_heap_info()` in order to use it.
273    */
274    std::unique_ptr<s_xbt_mheap_t> heap;
275
276   /** Copy of the allocation info structure
277    *
278    *  This is refreshed with the `MC_process_refresh` call.
279    *  This is not used if the process is the current one:
280    *  use `get_malloc_info()` in order to use it.
281    */
282   std::vector<malloc_info> heap_info;
283
284 public: // Libunwind-data
285
286   /** Full-featured MC-aware libunwind address space for the process
287    *
288    *  This address space is using a simgrid::mc::UnwindContext*
289    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
290    *  and unw_context_t).
291    */
292   unw_addr_space_t unw_addr_space;
293
294   /** Underlying libunwind addres-space
295    *
296    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
297    *  operations of the native MC address space is currently delegated
298    *  to this address space (either the local or a ptrace unwinder).
299    */
300   unw_addr_space_t unw_underlying_addr_space;
301
302   /** The corresponding context
303    */
304   void* unw_underlying_context;
305 };
306
307 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
308  */
309 XBT_PRIVATE int open_vm(pid_t pid, int flags);
310
311 }
312 }
313
314 #endif