Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename ASAN_EVAL to ASAN_ONLY, and kill ASAN_ASSERT.
[simgrid.git] / src / kernel / context / ContextUnix.cpp
index 4e739c1..b4af204 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -6,9 +6,10 @@
 /* \file UContext.cpp Context switching with ucontexts from System V        */
 
 #include "ContextUnix.hpp"
+#include "context_private.hpp"
 
 #include "mc/mc.h"
-#include "src/mc/mc_ignore.h"
+#include "src/mc/mc_ignore.hpp"
 #include "src/simix/ActorImpl.hpp"
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
@@ -21,24 +22,6 @@ constexpr int CTX_ADDR_LEN = 2;
 static_assert(sizeof(simgrid::kernel::context::UContext*) <= CTX_ADDR_LEN * sizeof(int),
               "Ucontexts are not supported on this arch yet");
 
-// The name of this function is currently hardcoded in the code (as string).
-// Do not change it without fixing those references as well.
-static void smx_ctx_sysv_wrapper(int i1, int i2)
-{
-  // Rebuild the Context* pointer from the integers:
-  int ctx_addr[CTX_ADDR_LEN] = {i1, i2};
-  simgrid::kernel::context::UContext* context;
-  memcpy(&context, ctx_addr, sizeof context);
-
-  try {
-    (*context)();
-    context->Context::stop();
-  } catch (simgrid::kernel::context::Context::StopRequest const&) {
-    XBT_DEBUG("Caught a StopRequest");
-  }
-  context->suspend();
-}
-
 namespace simgrid {
 namespace kernel {
 namespace context {
@@ -103,7 +86,12 @@ UContext::UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_fun
     this->uc_.uc_link = nullptr;
     this->uc_.uc_stack.ss_sp   = sg_makecontext_stack_addr(this->stack_);
     this->uc_.uc_stack.ss_size = sg_makecontext_stack_size(smx_context_usable_stack_size);
-    UContext::make_ctx(&this->uc_, UContext::wrapper, this);
+#if PTH_STACKGROWTH == -1
+    ASAN_ONLY(this->asan_stack_ = static_cast<char*>(this->stack_) + smx_context_usable_stack_size);
+#else
+    ASAN_ONLY(this->asan_stack_ = this->stack_);
+#endif
+    UContext::make_ctx(&this->uc_, UContext::smx_ctx_sysv_wrapper, this);
   } else {
     if (process != nullptr && maestro_context_ == nullptr)
       maestro_context_ = this;
@@ -121,9 +109,24 @@ UContext::~UContext()
   SIMIX_context_stack_delete(this->stack_);
 }
 
-void UContext::wrapper(int i1, int i2)
+// The name of this function is currently hardcoded in the code (as string).
+// Do not change it without fixing those references as well.
+void UContext::smx_ctx_sysv_wrapper(int i1, int i2)
 {
-  smx_ctx_sysv_wrapper(i1, i2);
+  // Rebuild the Context* pointer from the integers:
+  int ctx_addr[CTX_ADDR_LEN] = {i1, i2};
+  simgrid::kernel::context::UContext* context;
+  memcpy(&context, ctx_addr, sizeof context);
+
+  ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
+  try {
+    (*context)();
+    context->Context::stop();
+  } catch (simgrid::kernel::context::Context::StopRequest const&) {
+    XBT_DEBUG("Caught a StopRequest");
+  }
+  ASAN_ONLY(context->asan_stop_ = true);
+  context->suspend();
 }
 
 /** A better makecontext
@@ -138,6 +141,15 @@ void UContext::make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg)
   makecontext(ucp, (void (*)())func, 2, ctx_addr[0], ctx_addr[1]);
 }
 
+inline void UContext::swap(UContext* from, UContext* to)
+{
+  void* fake_stack = nullptr;
+  ASAN_ONLY(to->asan_ctx_ = from);
+  ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
+  swapcontext(&from->uc_, &to->uc_);
+  ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
+}
+
 void UContext::stop()
 {
   Context::stop();