Skip to content

Commit

Permalink
Tweaking build system to enable segfault detection on Linux systems only
Browse files Browse the repository at this point in the history
- type fixes in generated error messages
- std::exit() --> std::terminate()
  • Loading branch information
hkaiser committed Jul 15, 2017
1 parent 0c10ac3 commit a7a947d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
21 changes: 14 additions & 7 deletions CMakeLists.txt
Expand Up @@ -139,14 +139,21 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
# set(DEFAULT_MALLOC "jemalloc")
endif()

hpx_option(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION
BOOL
"Enable stackoverflow detection for HPX threads/coroutines. (default: ON)"
ON ADVANCED)
if(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION)
hpx_add_config_define(HPX_HAVE_THREAD_STACKOVERFLOW_DETECTION)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION_DEFAULT OFF)
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UC)
if("${CMAKE_BUILD_TYPE_UC}" STREQUAL "DEBUG")
set(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION_DEFAULT ON)
endif()
hpx_option(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION
BOOL
"Enable stackoverflow detection for HPX threads/coroutines. (default: OFF, debug: ON)"
${HPX_WITH_THREAD_STACKOVERFLOW_DETECTION_DEFAULT} ADVANCED)
if(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION)
hpx_add_config_define(HPX_HAVE_THREAD_STACKOVERFLOW_DETECTION)
endif()
endif()

hpx_option(HPX_WITH_MALLOC
STRING
"Define which allocator should be linked in. Options are: system, tcmalloc, jemalloc, tbbmalloc, and custom (default is: tcmalloc)"
Expand Down
28 changes: 12 additions & 16 deletions hpx/runtime/threads/coroutines/detail/context_linux_x86.hpp
Expand Up @@ -236,7 +236,7 @@ namespace hpx { namespace threads { namespace coroutines

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

sigaltstack(&segv_stack, nullptr);
sigfillset(&action.sa_mask);
Expand All @@ -254,21 +254,17 @@ namespace hpx { namespace threads { namespace coroutines
<< std::internal << std::hex
<< std::setw(sizeof(addr)*2+2)
<< std::setfill('0') << static_cast<int*>(addr)
<< "." << std::endl;

std::cerr << std::endl << "Configure the hpx runtime to "
<< "allocate a larger "
<< "coroutine stack size." << std::endl
<< "Use the hpx.stacks.small_size, "
<< "hpx.stacks.medium_size, "
<< std::endl
<< "hpx.stacks.large_size, "
<< "or hpx.stacks.huge_size runtime "
<< std::endl
<< "flags to configure coroutine heap sizes."
<< std::endl << std::endl;

std::exit(EXIT_FAILURE);
<< ".\n\n";

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.large_size, "
"or hpx.stacks.huge_size configuration\nflags to configure "
"coroutine stack sizes.\n"
<< std::endl;

std::terminate();
}
#endif
~x86_linux_context_impl()
Expand Down
30 changes: 13 additions & 17 deletions hpx/runtime/threads/coroutines/detail/context_posix.hpp
Expand Up @@ -252,7 +252,7 @@ namespace hpx { namespace threads { namespace coroutines

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

sigaltstack(&segv_stack, nullptr);
sigfillset(&action.sa_mask);
Expand All @@ -270,21 +270,17 @@ namespace hpx { namespace threads { namespace coroutines
<< std::internal << std::hex
<< std::setw(sizeof(addr)*2+2)
<< std::setfill('0') << static_cast<int*>(addr)
<< "." << std::endl;

std::cerr << std::endl << "Configure the hpx runtime to "
<< "allocate a larger "
<< "coroutine stack size." << std::endl
<< "Use the hpx.stacks.small_size, "
<< "hpx.stacks.medium_size, "
<< std::endl
<< "hpx.stacks.large_size, "
<< "or hpx.stacks.huge_size runtime "
<< std::endl
<< "flags to configure coroutine heap sizes."
<< std::endl << std::endl;

std::exit(EXIT_FAILURE);
<< ".\n\n";

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.large_size, "
"or hpx.stacks.huge_size configuration\nflags to configure "
"coroutine stack sizes.\n"
<< std::endl;

std::terminate();
}
#endif

Expand Down Expand Up @@ -394,7 +390,7 @@ namespace hpx { namespace threads { namespace coroutines
* This #else clause is essentially unchanged from the original Google Summer
* of Code version of Boost.Coroutine, which comments:
* "Context swapping can be implemented on most posix systems lacking *context
* using the sigaltstack+longjmp trick."
* using the signaltstack+longjmp trick."
* This is in fact what the (highly portable) Pth library does, so if you
* encounter such a system, perhaps the best approach would be to twiddle the
* #if logic in this header to use the pth.h implementation above.
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/threads/CMakeLists.txt
Expand Up @@ -14,11 +14,14 @@ set(tests
thread_launching
thread_mf
thread_stacksize
thread_stacksize_overflow
thread_suspension_executor
thread_yield
)

if(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION)
set(tests ${tests} thread_stacksize_overflow)
endif()

if(HPX_WITH_THREAD_LOCAL_STORAGE)
set(tests ${tests} tss)
endif()
Expand Down Expand Up @@ -77,6 +80,9 @@ foreach(test ${tests})
endforeach()

set_property(TARGET lockfree_fifo_test_exe APPEND
PROPERTY COMPILE_DEFINITIONS
"HPX_NO_VERSION_CHECK")
PROPERTY COMPILE_DEFINITIONS "HPX_NO_VERSION_CHECK")

if(HPX_WITH_THREAD_STACKOVERFLOW_DETECTION)
set_tests_properties(tests.unit.threads.thread_stacksize_overflow PROPERTIES
PASS_REGULAR_EXPRESSION "Stack overflow in coroutine at address 0x[0-9a-fA-F]*")
endif()

0 comments on commit a7a947d

Please sign in to comment.