1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "mtasker_context.hh"
#include <system_error>
#include <exception>
#include <cstring>
#include <cassert>
#include <signal.h>
#include <ucontext.h>
#ifdef PDNS_USE_VALGRIND
#include <valgrind/valgrind.h>
#endif /* PDNS_USE_VALGRIND */
#ifdef HAVE_FIBER_SANITIZER
__thread void* t_mainStack{nullptr};
__thread size_t t_mainStackSize{0};
#endif /* HAVE_FIBER_SANITIZER */
template <typename Message> static __attribute__((noinline, cold, noreturn))
void
throw_errno (Message&& msg) {
throw std::system_error
(errno, std::system_category(), std::forward<Message>(msg));
}
static inline
std::pair<int, int>
splitPointer (void* const ptr) noexcept {
static_assert (sizeof(int) == 4, "splitPointer() requires an 4 byte 'int'");
// In theory, we need this assertion. In practice, it prevents compilation
// on EL6 i386. Without the assertion, everything works.
// If you ever run into trouble with this code, please heed the warnings at
// http://man7.org/linux/man-pages/man3/makecontext.3.html#NOTES
// static_assert (sizeof(uintptr_t) == 8,
// "splitPointer() requires an 8 byte 'uintptr_t'");
std::pair<int, int> words;
auto rep = reinterpret_cast<uintptr_t>(ptr);
uint32_t const hw = rep >> 32;
auto const lw = static_cast<uint32_t>(rep);
std::memcpy (&words.first, &hw, 4);
std::memcpy (&words.second, &lw, 4);
return words;
}
template <typename T> static inline
T*
joinPtr (int const first, int const second) noexcept {
static_assert (sizeof(int) == 4, "joinPtr() requires an 4 byte 'int'");
// See above.
// static_assert (sizeof(uintptr_t) == 8,
// "joinPtr() requires an 8 byte 'uintptr_t'");
uint32_t hw;
uint32_t lw;
std::memcpy (&hw, &first, 4);
std::memcpy (&lw, &second, 4);
return reinterpret_cast<T*>((static_cast<uintptr_t>(hw) << 32) | lw);
}
extern "C" {
static
void
threadWrapper (int const ctx0, int const ctx1, int const fun0, int const fun1) {
notifyStackSwitchDone();
auto ctx = joinPtr<pdns_ucontext_t>(ctx0, ctx1);
try {
auto start = std::move(*joinPtr<std::function<void()>>(fun0, fun1));
start();
} catch (...) {
ctx->exception = std::current_exception();
}
notifyStackSwitchToKernel();
}
} // extern "C"
pdns_ucontext_t::pdns_ucontext_t() {
uc_mcontext = new ::ucontext_t();
uc_link = nullptr;
#ifdef PDNS_USE_VALGRIND
valgrind_id = 0;
#endif /* PDNS_USE_VALGRIND */
}
pdns_ucontext_t::~pdns_ucontext_t() {
delete static_cast<ucontext_t*>(uc_mcontext);
#ifdef PDNS_USE_VALGRIND
if (valgrind_id != 0) {
VALGRIND_STACK_DEREGISTER(valgrind_id);
}
#endif /* PDNS_USE_VALGRIND */
}
void
pdns_swapcontext
(pdns_ucontext_t& __restrict octx, pdns_ucontext_t const& __restrict ctx) {
if (::swapcontext (static_cast<ucontext_t*>(octx.uc_mcontext),
static_cast<ucontext_t*>(ctx.uc_mcontext))) {
throw_errno ("swapcontext() failed");
}
if (ctx.exception) {
std::rethrow_exception (ctx.exception);
}
}
void
pdns_makecontext
(pdns_ucontext_t& ctx, std::function<void(void)>& start) {
assert (ctx.uc_link);
assert (ctx.uc_stack.size());
auto const mcp = static_cast<ucontext_t*>(ctx.uc_mcontext);
auto const next = static_cast<ucontext_t*>(ctx.uc_link->uc_mcontext);
if (::getcontext (mcp)) {
throw_errno ("getcontext() failed");
}
mcp->uc_link = next;
mcp->uc_stack.ss_sp = ctx.uc_stack.data();
mcp->uc_stack.ss_size = ctx.uc_stack.size()-1;
mcp->uc_stack.ss_flags = 0;
auto ctxarg = splitPointer (&ctx);
auto funarg = splitPointer (&start);
return ::makecontext (mcp, reinterpret_cast<void(*)(void)>(&threadWrapper),
4, ctxarg.first, ctxarg.second,
funarg.first, funarg.second);
}
|