From 0f9729c819b39bf02c93407ce82daeae300fef9a Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Tue, 23 Dec 2025 20:19:29 +0300 Subject: [PATCH 1/2] Check for potential memory leak at runtime rather than on compile time --- CMakeLists.txt | 2 +- build/Jamfile.v2 | 3 +-- src/from_exception.cpp | 48 +++++++++++++++++++++++++----------------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4bde74..3dc5118 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,7 @@ stacktrace_check(BOOST_STACKTRACE_HAS_WINDBG has_windbg.cpp "" "dbgeng;ole32" "" stacktrace_check(BOOST_STACKTRACE_HAS_WINDBG_CACHED has_windbg_cached.cpp "${CMAKE_CURRENT_SOURCE_DIR}/../config/include" "dbgeng;ole32" "") set(_default_from_exception ON) -if (NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64|i386|i686|x86" OR CMAKE_CXX_PLATFORM_ID MATCHES "Cygwin") +if (CMAKE_CXX_PLATFORM_ID MATCHES "Cygwin") set(_default_from_exception OFF) endif() diff --git a/build/Jamfile.v2 b/build/Jamfile.v2 index a7e4615..0af76b9 100644 --- a/build/Jamfile.v2 +++ b/build/Jamfile.v2 @@ -249,8 +249,7 @@ rule build-stacktrace-from-exception ( props * ) case "off" : return no ; } - local arch = [ property.select : $(props) ] ; - if ( $(arch) && ( $(arch:G=) != x86 ) ) || ( cygwin in $(props) ) + if ( cygwin in $(props) ) { configure.log-library-search-result "boost.stacktrace.from_exception" : "no" ; return no ; diff --git a/src/from_exception.cpp b/src/from_exception.cpp index 366ba28..4ad7de7 100644 --- a/src/from_exception.cpp +++ b/src/from_exception.cpp @@ -160,29 +160,13 @@ BOOST_SYMBOL_EXPORT void assert_no_pending_traces() noexcept { #include #include + #if !BOOST_STACKTRACE_ALWAYS_STORE_IN_PADDING +#include #include #include -#ifndef BOOST_STACKTRACE_LIBCXX_RUNTIME_MAY_CAUSE_MEMORY_LEAK - -#ifdef BOOST_HAS_THREADS - -#error On this platform memory leaks are possible if capturing stacktrace from \ - exceptions is enabled and exceptions are thrown concurrently \ - and libc++ runtime is used. \ - \ - Define `BOOST_STACKTRACE_LIBCXX_RUNTIME_MAY_CAUSE_MEMORY_LEAK` to \ - suppress this error if the library would not be used with libc++ \ - runtime (for example, it would be only used with GCC runtime). \ - \ - Otherwise, disable the boost_stacktrace_from_exception library build \ - (for example by `./b2 boost.stacktrace.from_exception=off` option). - -#endif - -#endif - +#include #endif namespace { @@ -297,6 +281,32 @@ void* __cxa_allocate_exception(size_t thrown_size) throw() { extern "C" BOOST_SYMBOL_EXPORT void __cxa_decrement_exception_refcount(void *thrown_object) throw() { BOOST_ASSERT(is_libcpp_runtime()); + +#if !defined(BOOST_STACKTRACE_LIBCXX_RUNTIME_MAY_CAUSE_MEMORY_LEAK) && defined(BOOST_HAS_THREADS) + static const char* leaks_are_fine = std::getenv("BOOST_STACKTRACE_LIBCXX_RUNTIME_MAY_CAUSE_MEMORY_LEAK"); + if (!leaks_are_fine || leaks_are_fine[0] != '1') { + const char* const warning = + "\n\n" + "=======================================================================================\n" + "\n" + "On this platform, memory leaks may occur if capturing stacktrace from exceptions is\n" + "enabled and exceptions are thrown concurrently by libc++ runtime (libc++abi).\n" + "\n" + "A proper workaround is to use libstdc++ runtime (libgcc_s) instead.\n" + "\n" + "Alternatively, if you are willing to accept potential MEMORY LEAKS, set the environment\n" + "variable `BOOST_STACKTRACE_LIBCXX_RUNTIME_MAY_CAUSE_MEMORY_LEAK=1` to proceed. You can\n" + "also define the `BOOST_STACKTRACE_LIBCXX_RUNTIME_MAY_CAUSE_MEMORY_LEAK` macro when\n" + "building the `boost_stacktrace_from_exception` library to disable this warning and to\n" + "get the MEMORY LEAKS silently on libc++ runtime.\n" + "\n" + "=======================================================================================\n" + ; + write(STDERR_FILENO, warning, strlen(warning)); + std::abort(); + } +#endif + if (!thrown_object) { return; } From 544c34063dfb5ae05fc376641428dcab29726a8d Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 24 Dec 2025 13:33:07 +0300 Subject: [PATCH 2/2] minor fixes --- src/from_exception.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/from_exception.cpp b/src/from_exception.cpp index 4ad7de7..18f3001 100644 --- a/src/from_exception.cpp +++ b/src/from_exception.cpp @@ -163,6 +163,7 @@ BOOST_SYMBOL_EXPORT void assert_no_pending_traces() noexcept { #if !BOOST_STACKTRACE_ALWAYS_STORE_IN_PADDING #include +#include #include #include @@ -302,7 +303,7 @@ void __cxa_decrement_exception_refcount(void *thrown_object) throw() { "\n" "=======================================================================================\n" ; - write(STDERR_FILENO, warning, strlen(warning)); + write(STDERR_FILENO, warning, std::strlen(warning)); std::abort(); } #endif