Logo AND Algorithmique Numérique Distribuée

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