Skip to content

Commit

Permalink
Allowing to disable signal handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Nov 7, 2023
1 parent 89f342b commit fef81e7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#endif

#include <hpx/assert.hpp>
#include <hpx/coroutines/signal_handler_debugging.hpp>
#include <hpx/type_support/unused.hpp>
#include <hpx/util/get_and_reset_value.hpp>

Expand Down Expand Up @@ -267,23 +268,26 @@ namespace hpx::threads::coroutines {
// https://rethinkdb.com/blog/handling-stack-overflow-on-custom-stacks/
// http://www.evanjones.ca/software/threading.html
//
segv_stack.ss_sp = valloc(SEGV_STACK_SIZE);
segv_stack.ss_flags = 0;
segv_stack.ss_size = SEGV_STACK_SIZE;

std::memset(&action, '\0', sizeof(action));
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
action.sa_sigaction = &ucontext_context_impl::sigsegv_handler;

sigaltstack(&segv_stack, nullptr);
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, SIGSEGV);
sigaction(SIGSEGV, &action, nullptr);
if (register_signal_handler)
{
segv_stack.ss_sp = valloc(SEGV_STACK_SIZE);
segv_stack.ss_flags = 0;
segv_stack.ss_size = SEGV_STACK_SIZE;

std::memset(&action, '\0', sizeof(action));
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
action.sa_sigaction =
&ucontext_context_impl::sigsegv_handler;

sigaltstack(&segv_stack, nullptr);
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, SIGSEGV);
sigaction(SIGSEGV, &action, nullptr);
}
#endif
}

#if defined(HPX_HAVE_STACKOVERFLOW_DETECTION)

static void sigsegv_handler(
int signum, siginfo_t* infoptr, void* ctxptr)
{
Expand Down Expand Up @@ -313,9 +317,11 @@ namespace hpx::threads::coroutines {
std::cerr
<< "Configure the hpx runtime to allocate a larger "
"coroutine stack size.\n Use the "
"hpx.stacks.small_size, hpx.stacks.medium_size,\n "
"hpx.stacks.small_size, "
"hpx.stacks.medium_size,\n "
"hpx.stacks.large_size, or hpx.stacks.huge_size "
"configuration\nflags to configure coroutine stack "
"configuration\nflags to configure coroutine "
"stack "
"sizes.\n"
<< std::endl;

Expand Down
2 changes: 1 addition & 1 deletion libs/core/coroutines/src/signal_handler_debugging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ namespace hpx::threads::coroutines {
std::size_t trace_depth = 0;
#endif
#if !defined(HPX_WINDOWS)
bool register_signal_handler = 1;
bool register_signal_handler = true;
#endif
} // namespace hpx::threads::coroutines
9 changes: 5 additions & 4 deletions libs/core/init_runtime_local/src/init_runtime_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ namespace hpx {
}

////////////////////////////////////////////////////////////////////////
void init_environment()
void init_environment(
[[maybe_unused]] hpx::util::runtime_configuration const& cfg)
{
HPX_UNUSED(hpx::filesystem::initial_path());

Expand Down Expand Up @@ -397,7 +398,7 @@ namespace hpx {
&hpx::detail::register_locks_predicate);
#endif
#if !defined(HPX_HAVE_DISABLED_SIGNAL_EXCEPTION_HANDLERS)
set_error_handlers();
set_error_handlers(cfg);
#endif
hpx::threads::detail::set_get_default_pool(
&hpx::detail::get_default_pool);
Expand Down Expand Up @@ -446,8 +447,6 @@ namespace hpx {
hpx::program_options::variables_map& vm)> const& f,
int argc, char** argv, init_params const& params, bool blocking)
{
init_environment();

int result = 0;
try
{
Expand All @@ -468,6 +467,8 @@ namespace hpx {
{
result = cmdline.call(params.desc_cmdline, argc, argv);

init_environment(cmdline.rtcfg_);

hpx::threads::policies::detail::affinity_data
affinity_data{};
affinity_data.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ namespace hpx {
hpx::program_options::options_description app_options_;
};

HPX_CORE_EXPORT void set_error_handlers();
HPX_CORE_EXPORT void set_error_handlers(
hpx::util::runtime_configuration const& cfg);

namespace util {
///////////////////////////////////////////////////////////////////////////
Expand Down
28 changes: 16 additions & 12 deletions libs/core/runtime_local/src/runtime_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <hpx/timing/high_resolution_clock.hpp>
#include <hpx/topology/topology.hpp>
#include <hpx/util/from_string.hpp>
#include <hpx/util/get_entry_as.hpp>
#include <hpx/version.hpp>

#include <atomic>
Expand All @@ -58,7 +59,8 @@
#include <utility>

#if defined(HPX_HAVE_LOGGING)
namespace hpx { namespace detail {
namespace hpx::detail {

void try_log_runtime_threads()
{
// This may be used in non-valid runtime states, let it fail silently
Expand All @@ -84,7 +86,7 @@ namespace hpx { namespace detail {
{
}
}
}}; // namespace hpx::detail
} // namespace hpx::detail
#endif

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -242,27 +244,29 @@ namespace hpx {
} // namespace detail

///////////////////////////////////////////////////////////////////////////
void set_error_handlers()
void set_error_handlers(hpx::util::runtime_configuration const& cfg)
{
// initialize global variables
hpx::threads::coroutines::attach_debugger_on_sigv =
get_config_entry("hpx.attach_debugger", "") == "exception";
hpx::util::get_entry_as<std::string>(
cfg, "hpx.attach_debugger", "") == "exception";
hpx::threads::coroutines::diagnostics_on_terminate =
get_config_entry("hpx.diagnostics_on_terminate", "1") == "1";
hpx::threads::coroutines::exception_verbosity = util::from_string<int>(
get_config_entry("hpx.exception_verbosity", "2"));
hpx::util::get_entry_as<std::string>(
cfg, "hpx.diagnostics_on_terminate", "1") == "1";
hpx::threads::coroutines::exception_verbosity =
hpx::util::get_entry_as<int>(cfg, "hpx.exception_verbosity", 2);
hpx::threads::coroutines::exception_verbosity = 0;
#if defined(HPX_HAVE_STACKTRACES) && defined(HPX_HAVE_THREAD_BACKTRACE_DEPTH)
hpx::threads::coroutines::exception_verbosity =
util::from_string<int>(get_config_entry(
"hpx.trace_depth", HPX_HAVE_THREAD_BACKTRACE_DEPTH));
hpx::util::get_entry_as<int>(
cfg, "hpx.trace_depth", HPX_HAVE_THREAD_BACKTRACE_DEPTH);
#endif

#if defined(HPX_WINDOWS)
// Set console control handler to allow server to be stopped.
SetConsoleCtrlHandler(hpx::termination_handler, TRUE);
#else
if (util::from_string<int>(get_config_entry("hpx.handle_signals", 1)))
if (hpx::util::get_entry_as<int>(cfg, "hpx.handle_signals", 1))
{
struct sigaction new_action;
new_action.sa_handler = hpx::termination_handler;
Expand All @@ -278,11 +282,11 @@ namespace hpx {
sigaction(SIGSEGV, &new_action, nullptr); // Segmentation fault
sigaction(SIGSYS, &new_action, nullptr); // Bad syscall

hpx::threads::coroutines::register_signal_handler = false;
hpx::threads::coroutines::register_signal_handler = true;
}
else
{
hpx::threads::coroutines::register_signal_handler = true;
hpx::threads::coroutines::register_signal_handler = false;
}
#endif

Expand Down
9 changes: 5 additions & 4 deletions libs/full/init_runtime/src/hpx_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,8 @@ namespace hpx {
}

////////////////////////////////////////////////////////////////////////
void init_environment()
void init_environment(
[[maybe_unused]] hpx::util::runtime_configuration const& cfg)
{
HPX_UNUSED(hpx::filesystem::initial_path());

Expand Down Expand Up @@ -792,7 +793,7 @@ namespace hpx {
&detail::register_locks_predicate);
#endif
#if !defined(HPX_HAVE_DISABLED_SIGNAL_EXCEPTION_HANDLERS)
set_error_handlers();
set_error_handlers(cfg);
#endif
hpx::threads::detail::set_get_default_pool(
&detail::get_default_pool);
Expand Down Expand Up @@ -869,8 +870,6 @@ namespace hpx {
f,
int argc, char** argv, init_params const& params, bool blocking)
{
init_environment();

int result;
try
{
Expand Down Expand Up @@ -903,6 +902,8 @@ namespace hpx {
result = cmdline.call(
params.desc_cmdline, argc, argv, component_registries);

init_environment(cmdline.rtcfg_);

hpx::threads::policies::detail::affinity_data
affinity_data{};
affinity_data.init(hpx::util::get_entry_as<std::size_t>(
Expand Down

0 comments on commit fef81e7

Please sign in to comment.