Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Save valgrind_stack_id in a private field.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 12 Mar 2019 13:31:16 +0000 (14:31 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 12 Mar 2019 15:41:48 +0000 (16:41 +0100)
Only register stacks when running on Valgrind to avoid false alarms from Asan.

src/kernel/context/ContextBoost.cpp
src/kernel/context/ContextRaw.cpp
src/kernel/context/ContextSwapped.cpp
src/kernel/context/ContextSwapped.hpp
src/kernel/context/ContextUnix.cpp
src/kernel/context/context_private.hpp

index ee99def..9717474 100644 (file)
@@ -31,14 +31,14 @@ BoostContext::BoostContext(std::function<void()>&& code, smx_actor_t actor, Swap
     /* We need to pass the bottom of the stack to make_fcontext,
        depending on the stack direction it may be the lower or higher address: */
 #if PTH_STACKGROWTH == -1
-    unsigned char* stack = get_stack() + smx_context_usable_stack_size;
+    unsigned char* stack = get_stack() + smx_context_stack_size;
 #else
     unsigned char* stack = get_stack();
 #endif
 #if BOOST_VERSION < 106100
-    this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
+    this->fc_ = boost::context::make_fcontext(stack, smx_context_stack_size, BoostContext::wrapper);
 #else
-    this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
+    this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_stack_size, BoostContext::wrapper);
 #endif
 
   } else {
index 520d834..328e8f5 100644 (file)
@@ -199,7 +199,7 @@ RawContext::RawContext(std::function<void()>&& code, smx_actor_t actor, SwappedC
     : SwappedContext(std::move(code), actor, factory)
 {
    if (has_code()) {
-     this->stack_top_ = raw_makecontext(get_stack(), smx_context_usable_stack_size, RawContext::wrapper, this);
+     this->stack_top_ = raw_makecontext(get_stack(), smx_context_stack_size, RawContext::wrapper, this);
    } else {
      if (MC_is_active())
        MC_ignore_heap(&stack_top_, sizeof(stack_top_));
index 8226815..3d7740f 100644 (file)
@@ -52,6 +52,7 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
     factory_->workers_context_[0] = this;
 
   if (has_code()) {
+    xbt_assert((smx_context_stack_size & 0xf) == 0, "smx_context_stack_size should be multiple of 16");
     if (smx_context_guard_size > 0 && not MC_is_active()) {
 
 #if !defined(PTH_STACKGROWTH) || (PTH_STACKGROWTH != -1)
@@ -97,13 +98,13 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
     }
 
 #if PTH_STACKGROWTH == -1
-    ASAN_ONLY(this->asan_stack_ = this->stack_ + smx_context_usable_stack_size);
+    ASAN_ONLY(this->asan_stack_ = this->stack_ + smx_context_stack_size);
 #else
     ASAN_ONLY(this->asan_stack_ = this->stack_);
 #endif
 #if HAVE_VALGRIND_H
-    unsigned int valgrind_stack_id = VALGRIND_STACK_REGISTER(this->stack_, this->stack_ + smx_context_stack_size);
-    memcpy(this->stack_ + smx_context_usable_stack_size, &valgrind_stack_id, sizeof valgrind_stack_id);
+    if (RUNNING_ON_VALGRIND)
+      this->valgrind_stack_id_ = VALGRIND_STACK_REGISTER(this->stack_, this->stack_ + smx_context_stack_size);
 #endif
   }
 }
@@ -114,9 +115,8 @@ SwappedContext::~SwappedContext()
     return;
 
 #if HAVE_VALGRIND_H
-  unsigned int valgrind_stack_id;
-  memcpy(&valgrind_stack_id, stack_ + smx_context_usable_stack_size, sizeof valgrind_stack_id);
-  VALGRIND_STACK_DEREGISTER(valgrind_stack_id);
+  if (RUNNING_ON_VALGRIND)
+    VALGRIND_STACK_DEREGISTER(valgrind_stack_id_);
 #endif
 
 #ifndef _WIN32
index ad888cd..c132b63 100644 (file)
@@ -62,6 +62,10 @@ public:
 private:
   unsigned char* stack_ = nullptr;       /* the thread stack */
   SwappedContextFactory* const factory_; // for sequential and parallel run_all()
+
+#if HAVE_VALGRIND_H
+  unsigned int valgrind_stack_id_;
+#endif
 };
 
 } // namespace context
index 70a87d2..77b952e 100644 (file)
@@ -72,7 +72,7 @@ UContext::UContext(std::function<void()>&& code, smx_actor_t actor, SwappedConte
     getcontext(&this->uc_);
     this->uc_.uc_link = nullptr;
     this->uc_.uc_stack.ss_sp   = sg_makecontext_stack_addr(get_stack());
-    this->uc_.uc_stack.ss_size = sg_makecontext_stack_size(smx_context_usable_stack_size);
+    this->uc_.uc_stack.ss_size = sg_makecontext_stack_size(smx_context_stack_size);
     // Makecontext expects integer arguments; we want to pass a pointer.
     // This context address is decomposed into a serie of integers, which are passed as arguments to makecontext.
 
@@ -83,7 +83,7 @@ UContext::UContext(std::function<void()>&& code, smx_actor_t actor, SwappedConte
 
 #if SIMGRID_HAVE_MC
     if (MC_is_active()) {
-      MC_register_stack_area(get_stack(), actor, &(this->uc_), smx_context_usable_stack_size);
+      MC_register_stack_area(get_stack(), actor, &(this->uc_), smx_context_stack_size);
     }
 #endif
   }
index 0bf2a78..8a47f56 100644 (file)
 #define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old) (void)0
 #endif
 
-/* We are using the bottom of the stack to save some information, like the
- * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
- * size for the stack. Round its value to a multiple of 16 (asan wants the stacks to be aligned this way). */
-#if HAVE_VALGRIND_H
-#define smx_context_usable_stack_size                                                                                  \
-  ((smx_context_stack_size - sizeof(unsigned int)) & ~0xf) /* for valgrind_stack_id */
-#else
-#define smx_context_usable_stack_size (smx_context_stack_size & ~0xf)
-#endif
-
 #endif