Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Boost contexts: inform ASan about context switches.
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-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 #include <cstdint>
7
8 #include <functional>
9 #include <utility>
10 #include <vector>
11
12 #include <boost/context/all.hpp>
13
14 #include <xbt/log.h>
15 #include <xbt/xbt_os_thread.h>
16
17 #include "src/simix/smx_private.h"
18 #include "src/internal_config.h"
19 #include "src/kernel/context/ContextBoost.hpp"
20
21 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
22 #include <sanitizer/asan_interface.h>
23 #define ASAN_EVAL(expr) (expr)
24 #define ASAN_START_SWITCH(fake_stack_save, bottom, size) __sanitizer_start_switch_fiber(fake_stack_save, bottom, size)
25 #define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old)                                                      \
26   __sanitizer_finish_switch_fiber(fake_stack_save, bottom_old, size_old)
27 #else
28 #define ASAN_EVAL(expr) (void)0
29 #define ASAN_START_SWITCH(fake_stack_save, bottom, size) (void)0
30 #define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old) (void)(fake_stack_save)
31 #endif
32
33 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
34
35 namespace simgrid {
36 namespace kernel {
37 namespace context {
38
39 class BoostSerialContext : public BoostContext {
40 public:
41   BoostSerialContext(std::function<void()> code,
42       void_pfn_smxprocess_t cleanup_func,
43       smx_actor_t process)
44     : BoostContext(std::move(code), cleanup_func, process) {}
45   void suspend() override;
46 };
47
48 #if HAVE_THREAD_CONTEXTS
49 class BoostParallelContext : public BoostContext {
50 public:
51   BoostParallelContext(std::function<void()> code,
52       void_pfn_smxprocess_t cleanup_func,
53       smx_actor_t process)
54     : BoostContext(std::move(code), cleanup_func, process) {}
55   void suspend() override;
56   void resume() override;
57 };
58 #endif
59
60 // BoostContextFactory
61
62 bool BoostContext::parallel_                             = false;
63 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
64 uintptr_t BoostContext::threads_working_                 = 0;
65 xbt_os_thread_key_t BoostContext::worker_id_key_;
66 unsigned long BoostContext::process_index_   = 0;
67 BoostContext* BoostContext::maestro_context_ = nullptr;
68 std::vector<BoostContext*> BoostContext::workers_context_;
69
70 BoostContextFactory::BoostContextFactory()
71   : ContextFactory("BoostContextFactory")
72 {
73   BoostContext::parallel_ = SIMIX_context_is_parallel();
74   if (BoostContext::parallel_) {
75 #if HAVE_THREAD_CONTEXTS
76     BoostContext::parmap_ = nullptr;
77     BoostContext::workers_context_.clear();
78     BoostContext::workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
79     BoostContext::maestro_context_ = nullptr;
80     xbt_os_thread_key_create(&BoostContext::worker_id_key_);
81 #else
82     xbt_die("No thread support for parallel context execution");
83 #endif
84   }
85 }
86
87 BoostContextFactory::~BoostContextFactory()
88 {
89 #if HAVE_THREAD_CONTEXTS
90   delete BoostContext::parmap_;
91   BoostContext::parmap_ = nullptr;
92   BoostContext::workers_context_.clear();
93 #endif
94 }
95
96 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
97                                                   smx_actor_t process)
98 {
99   BoostContext* context = nullptr;
100   if (BoostContext::parallel_)
101 #if HAVE_THREAD_CONTEXTS
102     context = this->new_context<BoostParallelContext>(std::move(code), cleanup_func, process);
103 #else
104     xbt_die("No support for parallel execution");
105 #endif
106   else
107     context = this->new_context<BoostSerialContext>(std::move(code), cleanup_func, process);
108   return context;
109 }
110
111 void BoostContextFactory::run_all()
112 {
113 #if HAVE_THREAD_CONTEXTS
114   if (BoostContext::parallel_) {
115     BoostContext::threads_working_ = 0;
116     if (not BoostContext::parmap_)
117       BoostContext::parmap_ =
118           new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
119     BoostContext::parmap_->apply(
120         [](smx_actor_t process) {
121           BoostContext* context = static_cast<BoostContext*>(process->context);
122           return context->resume();
123         },
124         simix_global->process_to_run);
125   } else
126 #endif
127   {
128     if (simix_global->process_to_run.empty())
129       return;
130     smx_actor_t first_process    = simix_global->process_to_run.front();
131     BoostContext::process_index_ = 1;
132     /* execute the first process */
133     static_cast<BoostContext*>(first_process->context)->resume();
134   }
135 }
136
137
138 // BoostContext
139
140 void BoostContext::smx_ctx_boost_wrapper(BoostContext::ctx_arg_type arg)
141 {
142 #if BOOST_VERSION < 106100
143   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
144 #else
145   ASAN_FINISH_SWITCH(nullptr, &static_cast<BoostContext**>(arg.data)[0]->asan_stack_,
146                      &static_cast<BoostContext**>(arg.data)[0]->asan_stack_size_);
147   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
148   BoostContext* context                         = static_cast<BoostContext**>(arg.data)[1];
149 #endif
150   try {
151     (*context)();
152     context->Context::stop();
153   } catch (StopRequest const&) {
154     XBT_DEBUG("Caught a StopRequest");
155   }
156   ASAN_EVAL(context->asan_stop_ = true);
157   context->suspend();
158 }
159
160 inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
161 {
162 #if BOOST_VERSION < 105600
163   boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
164 #elif BOOST_VERSION < 106100
165   boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
166 #else
167   BoostContext* ctx[2] = {from, to};
168   void* fake_stack;
169   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
170   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
171   ASAN_FINISH_SWITCH(fake_stack, &static_cast<BoostContext**>(arg.data)[0]->asan_stack_,
172                      &static_cast<BoostContext**>(arg.data)[0]->asan_stack_size_);
173   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
174 #endif
175 }
176
177 BoostContext::BoostContext(std::function<void()> code,
178     void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
179   : Context(std::move(code), cleanup_func, process)
180 {
181
182   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
183   if (has_code()) {
184     this->stack_ = SIMIX_context_stack_new();
185 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
186 // or higher address:
187 #if PTH_STACKGROWTH == -1
188     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
189 #else
190     void* stack = this->stack_;
191 #endif
192     ASAN_EVAL(this->asan_stack_ = stack);
193 #if BOOST_VERSION < 106100
194     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
195 #else
196     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
197 #endif
198   } else {
199 #if BOOST_VERSION < 105600
200     this->fc_ = new boost::context::fcontext_t();
201 #endif
202     if (BoostContext::maestro_context_ == nullptr)
203       BoostContext::maestro_context_ = this;
204   }
205 }
206
207 BoostContext::~BoostContext()
208 {
209 #if BOOST_VERSION < 105600
210   if (not this->stack_)
211     delete this->fc_;
212 #endif
213   if (this == maestro_context_)
214     maestro_context_ = nullptr;
215   SIMIX_context_stack_delete(this->stack_);
216 }
217
218 // BoostSerialContext
219
220 void BoostContext::stop()
221 {
222   Context::stop();
223   throw StopRequest();
224 }
225
226 void BoostContext::resume()
227 {
228   SIMIX_context_set_current(this);
229   smx_ctx_boost_jump_fcontext(maestro_context_, this);
230 }
231
232 void BoostSerialContext::suspend()
233 {
234   /* determine the next context */
235   BoostSerialContext* next_context;
236   unsigned long int i = process_index_;
237   process_index_++;
238
239   if (i < simix_global->process_to_run.size()) {
240     /* execute the next process */
241     XBT_DEBUG("Run next process");
242     next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
243   } else {
244     /* all processes were run, return to maestro */
245     XBT_DEBUG("No more process to run");
246     next_context = static_cast<BoostSerialContext*>(maestro_context_);
247   }
248   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
249   smx_ctx_boost_jump_fcontext(this, next_context);
250 }
251
252 // BoostParallelContext
253
254 #if HAVE_THREAD_CONTEXTS
255
256 void BoostParallelContext::suspend()
257 {
258   boost::optional<smx_actor_t> next_work = parmap_->next();
259   BoostParallelContext* next_context;
260   if (next_work) {
261     XBT_DEBUG("Run next process");
262     next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
263   } else {
264     XBT_DEBUG("No more processes to run");
265     uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
266     next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
267   }
268
269   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
270   smx_ctx_boost_jump_fcontext(this, next_context);
271 }
272
273 void BoostParallelContext::resume()
274 {
275   uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
276   xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
277
278   BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
279   workers_context_[worker_id] = worker_context;
280
281   SIMIX_context_set_current(this);
282   smx_ctx_boost_jump_fcontext(worker_context, this);
283 }
284
285 #endif
286
287 XBT_PRIVATE ContextFactory* boost_factory()
288 {
289   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
290   return new BoostContextFactory();
291 }
292
293 }}} // namespace