Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug two more leaks
[simgrid.git] / src / mc / remote / RemoteClient.hpp
1 /* Copyright (c) 2008-2017. 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 #ifndef SIMGRID_MC_PROCESS_H
7 #define SIMGRID_MC_PROCESS_H
8
9 #include <cstddef>
10 #include <cstdint>
11
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 #include <vector>
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
26 #include "src/mc/remote/Channel.hpp"
27 #include "src/mc/remote/RemotePtr.hpp"
28
29 #include "src/simix/popping_private.h"
30 #include "src/simix/smx_private.h"
31 #include <simgrid/simix.h>
32
33 #include "src/xbt/memory_map.hpp"
34
35 #include "src/mc/AddressSpace.hpp"
36 #include "src/mc/ObjectInformation.hpp"
37 #include "src/mc/mc_base.h"
38 #include "src/mc/mc_forward.hpp"
39 #include "src/mc/remote/mc_protocol.h"
40
41 namespace simgrid {
42 namespace mc {
43
44 class ActorInformation {
45 public:
46   /** MCed address of the process */
47   RemotePtr<simgrid::simix::ActorImpl> address = nullptr;
48   Remote<simgrid::simix::ActorImpl> copy;
49
50   /** Hostname (owned by `mc_modelchecker->hostnames`) */
51   const char* hostname = nullptr;
52   std::string name;
53
54   void clear()
55   {
56     name.clear();
57     address  = nullptr;
58     hostname = nullptr;
59   }
60 };
61
62 struct IgnoredRegion {
63   std::uint64_t addr;
64   std::size_t size;
65 };
66
67 struct IgnoredHeapRegion {
68   int block;
69   int fragment;
70   void* address;
71   std::size_t size;
72 };
73
74 /** The Model-Checked process, seen from the MCer perspective
75  *
76  *  This class is mixing a lot of different responsibilities and is tied
77  *  to SIMIX. It should probably be split into different classes.
78  *
79  *  Responsibilities:
80  *
81  *  - reading from the process memory (`AddressSpace`);
82  *  - accessing the system state of the process (heap, …);
83  *  - storing the SIMIX state of the process;
84  *  - privatization;
85  *  - communication with the model-checked process;
86  *  - stack unwinding;
87  *  - etc.
88  */
89 class RemoteClient final : public AddressSpace {
90 private:
91   // Those flags are used to track down which cached information
92   // is still up to date and which information needs to be updated.
93   static constexpr int cache_none            = 0;
94   static constexpr int cache_heap            = 1;
95   static constexpr int cache_malloc          = 2;
96   static constexpr int cache_simix_processes = 4;
97
98 public:
99   RemoteClient(pid_t pid, int sockfd);
100   ~RemoteClient();
101   void init();
102
103   RemoteClient(RemoteClient const&) = delete;
104   RemoteClient(RemoteClient&&)      = delete;
105   RemoteClient& operator=(RemoteClient const&) = delete;
106   RemoteClient& operator=(RemoteClient&&) = delete;
107
108   // Read memory:
109   const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
110                          ReadOptions options = ReadOptions::none()) const override;
111
112   void read_variable(const char* name, void* target, size_t size) const;
113   template <class T> void read_variable(const char* name, T* target) const
114   {
115     read_variable(name, target, sizeof(*target));
116   }
117   template <class T> Remote<T> read_variable(const char* name) const
118   {
119     Remote<T> res;
120     read_variable(name, res.getBuffer(), sizeof(T));
121     return res;
122   }
123
124   std::string read_string(RemotePtr<char> address) const;
125   std::string read_string(RemotePtr<char> address, std::size_t len) const
126   {
127     return AddressSpace::read_string(address, len);
128   }
129
130   // Write memory:
131   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
132   void clear_bytes(RemotePtr<void> address, size_t len);
133
134   // Debug information:
135   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
136   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
137   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
138   simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
139   simgrid::mc::Variable* find_variable(const char* name) const;
140
141   // Heap access:
142   xbt_mheap_t get_heap()
143   {
144     if (not(this->cache_flags_ & RemoteClient::cache_heap))
145       this->refresh_heap();
146     return this->heap.get();
147   }
148   const malloc_info* get_malloc_info()
149   {
150     if (not(this->cache_flags_ & RemoteClient::cache_malloc))
151       this->refresh_malloc_info();
152     return this->heap_info.data();
153   }
154
155   void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
156
157   Channel const& getChannel() const { return channel_; }
158   Channel& getChannel() { return channel_; }
159
160   std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
161   void ignore_region(std::uint64_t address, std::size_t size);
162
163   pid_t pid() const { return pid_; }
164
165   bool in_maestro_stack(RemotePtr<void> p) const
166   {
167     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
168   }
169
170   bool running() const { return running_; }
171
172   void terminate() { running_ = false; }
173
174   bool privatized(ObjectInformation const& info) const { return privatized_ && info.executable(); }
175   bool privatized() const { return privatized_; }
176   void privatized(bool privatized) { privatized_ = privatized; }
177
178   void ignore_global_variable(const char* name)
179   {
180     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
181       info->remove_global_variable(name);
182   }
183
184   std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
185   std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
186
187   std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
188   void ignore_heap(IgnoredHeapRegion const& region);
189   void unignore_heap(void* address, size_t size);
190
191   void ignore_local_variable(const char* var_name, const char* frame_name);
192   std::vector<simgrid::mc::ActorInformation>& actors();
193   std::vector<simgrid::mc::ActorInformation>& dead_actors();
194
195   /** Get a local description of a remote SIMIX actor */
196   simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> actor)
197   {
198     xbt_assert(mc_model_checker != nullptr);
199     if (not actor)
200       return nullptr;
201     this->refresh_simix();
202     for (auto& actor_info : this->smx_actors_infos)
203       if (actor_info.address == actor)
204         return &actor_info;
205     for (auto& actor_info : this->smx_dead_actors_infos)
206       if (actor_info.address == actor)
207         return &actor_info;
208     return nullptr;
209   }
210
211   /** Get a local copy of the SIMIX actor structure */
212   simgrid::simix::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> process)
213   {
214     simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
215     if (actor_info)
216       return actor_info->copy.getBuffer();
217     else
218       return nullptr;
219   }
220
221   void dumpStack();
222
223 private:
224   void init_memory_map_info();
225   void refresh_heap();
226   void refresh_malloc_info();
227   void refresh_simix();
228
229 private:
230   pid_t pid_ = -1;
231   Channel channel_;
232   bool running_ = false;
233   std::vector<simgrid::xbt::VmMap> memory_map_;
234   RemotePtr<void> maestro_stack_start_;
235   RemotePtr<void> maestro_stack_end_;
236   int memory_file = -1;
237   std::vector<IgnoredRegion> ignored_regions_;
238   bool privatized_ = false;
239   std::vector<s_stack_region_t> stack_areas_;
240   std::vector<IgnoredHeapRegion> ignored_heap_;
241
242 public: // object info
243   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
244   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
245   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
246   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
247
248 public: // Copies of MCed SMX data structures
249         /** Copy of `simix_global->process_list`
250          *
251          *  See mc_smx.c.
252          */
253   std::vector<ActorInformation> smx_actors_infos;
254
255   /** Copy of `simix_global->process_to_destroy`
256    *
257    *  See mc_smx.c.
258    */
259   std::vector<ActorInformation> smx_dead_actors_infos;
260
261 private:
262   /** State of the cache (which variables are up to date) */
263   int cache_flags_ = RemoteClient::cache_none;
264
265 public:
266   /** Address of the heap structure in the MCed process. */
267   void* heap_address;
268
269   /** Copy of the heap structure of the process
270    *
271    *  This is refreshed with the `MC_process_refresh` call.
272    *  This is not used if the process is the current one:
273    *  use `get_heap_info()` in order to use it.
274    */
275   std::unique_ptr<s_xbt_mheap_t> heap;
276
277   /** Copy of the allocation info structure
278    *
279    *  This is refreshed with the `MC_process_refresh` call.
280    *  This is not used if the process is the current one:
281    *  use `get_malloc_info()` in order to use it.
282    */
283   std::vector<malloc_info> heap_info;
284
285 public: // Libunwind-data
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 address-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 #endif