From 8e7feffef5e41a487cc322c68265c09cc0d7bdf2 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 16 Jan 2020 16:31:32 +0100 Subject: [PATCH] Move ASan related instructions around context swapping into SwappedContext. * make ASan related fields private * kill now unused src/kernel/contexts/context_private.h --- MANIFEST.in | 1 - src/kernel/context/ContextBoost.cpp | 7 +------ src/kernel/context/ContextBoost.hpp | 4 ++-- src/kernel/context/ContextRaw.cpp | 7 +------ src/kernel/context/ContextRaw.hpp | 4 ++-- src/kernel/context/ContextSwapped.cpp | 19 ++++++++++++++++++- src/kernel/context/ContextSwapped.hpp | 18 ++++++++++-------- src/kernel/context/ContextUnix.cpp | 8 +------- src/kernel/context/ContextUnix.hpp | 4 ++-- src/kernel/context/context_private.hpp | 23 ----------------------- tools/cmake/DefinePackages.cmake | 1 - 11 files changed, 37 insertions(+), 59 deletions(-) delete mode 100644 src/kernel/context/context_private.hpp diff --git a/MANIFEST.in b/MANIFEST.in index 3816a7f252..013c51230f 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2185,7 +2185,6 @@ include src/kernel/context/ContextThread.cpp include src/kernel/context/ContextThread.hpp include src/kernel/context/ContextUnix.cpp include src/kernel/context/ContextUnix.hpp -include src/kernel/context/context_private.hpp include src/kernel/future.cpp include src/kernel/lmm/fair_bottleneck.cpp include src/kernel/lmm/maxmin.cpp diff --git a/src/kernel/context/ContextBoost.cpp b/src/kernel/context/ContextBoost.cpp index 9e5e486466..9d275c2d3c 100644 --- a/src/kernel/context/ContextBoost.cpp +++ b/src/kernel/context/ContextBoost.cpp @@ -4,7 +4,6 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "ContextBoost.hpp" -#include "context_private.hpp" #include "simgrid/Exception.hpp" #include "src/simix/smx_private.hpp" @@ -49,19 +48,15 @@ void BoostContext::wrapper(BoostContext::arg_type arg) smx_ctx_wrapper(context); } -void BoostContext::swap_into(SwappedContext* to_) +void BoostContext::swap_into_for_real(SwappedContext* to_) { BoostContext* to = static_cast(to_); #if BOOST_VERSION < 106100 boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast(to)); #else BoostContext* ctx[2] = {this, to}; - ASAN_ONLY(void* fake_stack = nullptr); - ASAN_ONLY(to->asan_ctx_ = this); - ASAN_START_SWITCH(this->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_); boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx); this->verify_previous_context(static_cast(arg.data)[0]); - ASAN_FINISH_SWITCH(fake_stack, &this->asan_ctx_->asan_stack_, &this->asan_ctx_->asan_stack_size_); static_cast(arg.data)[0]->fc_ = arg.fctx; #endif } diff --git a/src/kernel/context/ContextBoost.hpp b/src/kernel/context/ContextBoost.hpp index 64456350d0..8284fc9835 100644 --- a/src/kernel/context/ContextBoost.hpp +++ b/src/kernel/context/ContextBoost.hpp @@ -34,8 +34,6 @@ class BoostContext : public SwappedContext { public: BoostContext(std::function&& code, actor::ActorImpl* actor, SwappedContextFactory* factory); - void swap_into(SwappedContext* to) override; - private: #if BOOST_VERSION < 106100 boost::context::fcontext_t fc_; @@ -46,6 +44,8 @@ private: #endif XBT_ATTRIB_NORETURN static void wrapper(arg_type arg); + + void swap_into_for_real(SwappedContext* to) override; }; class BoostContextFactory : public SwappedContextFactory { diff --git a/src/kernel/context/ContextRaw.cpp b/src/kernel/context/ContextRaw.cpp index 4ac3c90fc3..47fe3f057c 100644 --- a/src/kernel/context/ContextRaw.cpp +++ b/src/kernel/context/ContextRaw.cpp @@ -4,7 +4,6 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "ContextRaw.hpp" -#include "context_private.hpp" #include "mc/mc.h" #include "simgrid/Exception.hpp" #include "src/simix/smx_private.hpp" @@ -206,14 +205,10 @@ RawContext::RawContext(std::function&& code, actor::ActorImpl* actor, Sw } } -void RawContext::swap_into(SwappedContext* to_) +void RawContext::swap_into_for_real(SwappedContext* to_) { const RawContext* to = static_cast(to_); - ASAN_ONLY(void* fake_stack = nullptr); - ASAN_ONLY(to_->asan_ctx_ = this); - ASAN_START_SWITCH(this->asan_stop_ ? nullptr : &fake_stack, to_->asan_stack_, to_->asan_stack_size_); raw_swapcontext(&this->stack_top_, to->stack_top_); - ASAN_FINISH_SWITCH(fake_stack, &this->asan_ctx_->asan_stack_, &this->asan_ctx_->asan_stack_size_); } ContextFactory* raw_factory() diff --git a/src/kernel/context/ContextRaw.hpp b/src/kernel/context/ContextRaw.hpp index 7e364730c6..f9b30e04bd 100644 --- a/src/kernel/context/ContextRaw.hpp +++ b/src/kernel/context/ContextRaw.hpp @@ -28,11 +28,11 @@ class RawContext : public SwappedContext { public: RawContext(std::function&& code, actor::ActorImpl* actor, SwappedContextFactory* factory); - void swap_into(SwappedContext* to) override; - private: /** pointer to top the stack stack */ void* stack_top_ = nullptr; + + void swap_into_for_real(SwappedContext* to) override; }; class RawContextFactory : public SwappedContextFactory { diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index 8d7e071a63..e6e6a7bf00 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -7,7 +7,6 @@ #include "simgrid/modelchecker.h" #include "src/internal_config.h" #include "src/kernel/actor/ActorImpl.hpp" -#include "src/kernel/context/context_private.hpp" #include "src/simix/smx_private.hpp" #include "xbt/parmap.hpp" @@ -28,6 +27,9 @@ #if HAVE_VALGRIND_H #include #endif +#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT +#include +#endif XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context); @@ -158,6 +160,21 @@ void SwappedContext::stop() throw ForcefulKillException(); } +void SwappedContext::swap_into(SwappedContext* to) +{ +#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT + void* fake_stack = nullptr; + to->asan_ctx_ = this; + __sanitizer_start_switch_fiber(this->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_); +#endif + + swap_into_for_real(to); + +#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT + __sanitizer_finish_switch_fiber(fake_stack, &this->asan_ctx_->asan_stack_, &this->asan_ctx_->asan_stack_size_); +#endif +} + /** Maestro wants to run all ready actors */ void SwappedContextFactory::run_all() { diff --git a/src/kernel/context/ContextSwapped.hpp b/src/kernel/context/ContextSwapped.hpp index b83ae9fdaa..aac9841475 100644 --- a/src/kernel/context/ContextSwapped.hpp +++ b/src/kernel/context/ContextSwapped.hpp @@ -6,6 +6,7 @@ #ifndef SIMGRID_KERNEL_CONTEXT_SWAPPED_CONTEXT_HPP #define SIMGRID_KERNEL_CONTEXT_SWAPPED_CONTEXT_HPP +#include "src/internal_config.h" // HAVE_SANITIZER_* #include "src/kernel/context/Context.hpp" #include @@ -55,20 +56,13 @@ public: virtual void resume(); XBT_ATTRIB_NORETURN void stop() override; - virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses + void swap_into(SwappedContext* to); unsigned char* get_stack() const { return stack_; } // Return the address for the bottom of the stack. Depending on the stack direction it may be the lower or higher // address unsigned char* get_stack_bottom() const { return PTH_STACKGROWTH == -1 ? stack_ + smx_context_stack_size : stack_; } -#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT - const void* asan_stack_ = nullptr; - size_t asan_stack_size_ = 0; - SwappedContext* asan_ctx_ = nullptr; - bool asan_stop_ = false; -#endif - protected: // With ASan, after a context switch, check that the originating context is the expected one (see BoostContext) void verify_previous_context(const SwappedContext* context) const; @@ -82,6 +76,14 @@ private: #if HAVE_VALGRIND_H unsigned int valgrind_stack_id_; #endif +#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT + const void* asan_stack_ = nullptr; + size_t asan_stack_size_ = 0; + SwappedContext* asan_ctx_ = nullptr; + bool asan_stop_ = false; +#endif + + virtual void swap_into_for_real(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses }; inline void SwappedContext::verify_previous_context(XBT_ATTRIB_UNUSED const SwappedContext* context) const diff --git a/src/kernel/context/ContextUnix.cpp b/src/kernel/context/ContextUnix.cpp index 882dd743e2..0b6f7a8129 100644 --- a/src/kernel/context/ContextUnix.cpp +++ b/src/kernel/context/ContextUnix.cpp @@ -5,8 +5,6 @@ /* \file UContext.cpp Context switching with ucontexts from System V */ -#include "context_private.hpp" - #include "mc/mc.h" #include "simgrid/Exception.hpp" #include "src/kernel/actor/ActorImpl.hpp" @@ -73,14 +71,10 @@ UContext::UContext(std::function&& code, actor::ActorImpl* actor, Swappe } } -void UContext::swap_into(SwappedContext* to_) +void UContext::swap_into_for_real(SwappedContext* to_) { const UContext* to = static_cast(to_); - ASAN_ONLY(void* fake_stack = nullptr); - ASAN_ONLY(to_->asan_ctx_ = this); - ASAN_START_SWITCH(this->asan_stop_ ? nullptr : &fake_stack, to_->asan_stack_, to_->asan_stack_size_); swapcontext(&this->uc_, &to->uc_); - ASAN_FINISH_SWITCH(fake_stack, &this->asan_ctx_->asan_stack_, &this->asan_ctx_->asan_stack_size_); } diff --git a/src/kernel/context/ContextUnix.hpp b/src/kernel/context/ContextUnix.hpp index 8b20842f9a..c8a9ad2b71 100644 --- a/src/kernel/context/ContextUnix.hpp +++ b/src/kernel/context/ContextUnix.hpp @@ -27,10 +27,10 @@ class UContext : public SwappedContext { public: UContext(std::function&& code, actor::ActorImpl* actor, SwappedContextFactory* factory); - void swap_into(SwappedContext* to) override; - private: ucontext_t uc_; /* the ucontext that executes the code */ + + void swap_into_for_real(SwappedContext* to) override; }; class UContextFactory : public SwappedContextFactory { diff --git a/src/kernel/context/context_private.hpp b/src/kernel/context/context_private.hpp deleted file mode 100644 index 93aada64ec..0000000000 --- a/src/kernel/context/context_private.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (c) 2017-2020. 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. */ - -#ifndef SIMGRID_KERNEL_CONTEXT_PRIVATE_HPP -#define SIMGRID_KERNEL_CONTEXT_PRIVATE_HPP - -#include "src/internal_config.h" - -#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT -#include -#define ASAN_ONLY(expr) expr -#define ASAN_START_SWITCH(fake_stack_save, bottom, size) __sanitizer_start_switch_fiber(fake_stack_save, bottom, size) -#define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old) \ - __sanitizer_finish_switch_fiber(fake_stack_save, bottom_old, size_old) -#else -#define ASAN_ONLY(expr) (void)0 -#define ASAN_START_SWITCH(fake_stack_save, bottom, size) (void)0 -#define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old) (void)0 -#endif - -#endif diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 7b373ef410..ce4f338e2d 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -389,7 +389,6 @@ set(SIMIX_SRC src/kernel/future.cpp src/simix/libsmx.cpp src/simix/smx_context.cpp - src/kernel/context/context_private.hpp src/kernel/context/Context.cpp src/kernel/context/Context.hpp src/kernel/context/ContextRaw.cpp -- 2.20.1