From 5845f1a41d663dcfc59b2cb5caafee1266c22478 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Mon, 3 Nov 2025 22:18:45 -0400 Subject: [PATCH] Bug:Boost: Use shlibs instead of header-only for call_stack::top_ This symbol is defined as a static member object inside of a boost detail header. When boost headers are used in a project that uses Boost in both the main binary as well as dlopen()'d shlibs, the top_ symbol gets duplicated and the metadata gets partitioned. We use the Boost shlib to unify both the main binary and the shlibs to use the same memory address for top_. This involves marking the templated object call_stack::top_ as "extern" and then declaring to Boost that we intend to use the shlibs. --- CMakeLists.txt | 27 ++++++-- cmake/VerifyBoostDynamic.cmake | 63 +++++++++++++++++++ commonLibs/attachmentSupport/CMakeLists.txt | 5 ++ .../attachmentSupport/attachmentSupport.cpp | 6 +- commonLibs/livoxProto1/CMakeLists.txt | 9 ++- commonLibs/livoxProto1/broadcastListener.h | 1 + commonLibs/livoxProto1/device.cpp | 1 + commonLibs/livoxProto1/device.h | 1 + commonLibs/livoxProto1/livoxProto1.cpp | 1 + commonLibs/livoxProto1/livoxProto1.h | 1 + commonLibs/livoxProto1/protocol.h | 1 + commonLibs/livoxProto1/udpCommandDemuxer.h | 1 + smocore/CMakeLists.txt | 7 ++- smocore/componentThread.cpp | 1 + smocore/include/boostAsioLinkageFix.h | 25 ++++++++ smocore/include/componentThread.h | 1 + .../include/deviceManager/deviceReattacher.h | 1 + smocore/marionette/main.cpp | 1 + stimBuffApis/livoxGen1/CMakeLists.txt | 12 +++- .../livoxGen1/ioUringAssemblyEngine.cpp | 1 + .../livoxGen1/ioUringAssemblyEngine.h | 1 + stimBuffApis/livoxGen1/livoxGen1.cpp | 4 +- 22 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 cmake/VerifyBoostDynamic.cmake create mode 100644 smocore/include/boostAsioLinkageFix.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c0d6ecc..5821f57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(salmanoff VERSION 0.01.000 LANGUAGES CXX) include(CMakeDependentOption) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DAPSS.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DebugOpts.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake) # Set C++ standard set(CMAKE_CXX_STANDARD 20) @@ -87,10 +88,19 @@ include_directories( ) # Find core dependencies -# Boost 1.72.0 is required to ensure that a certain bug where boost::asio -# objects depend on specific copies of symbols, and boost will cause a segfault -# if boost::asio objects are used inside of a dlopen()'d library, is fixed. -find_package(Boost 1.73.0 REQUIRED COMPONENTS system) +# We cannot use header-only Boost.Asio because we need both our dlopen()'d +# libraries and the main binary to refer to the same instances of boost::asio's +# metadata. If we use header-only Boost.Asio, each dlopen()'d library will have +# its own copy of boost::asio's metadata, which will cause a segfault if +# boost::asio objects are used inside of a dlopen()'d library. +# +# Honestly, I never liked this whole "header-only" idea so I'm happy to be rid +# of it. +# +# Tell CMake we're linking against the shared library (not header-only) +set(Boost_USE_STATIC_LIBS OFF) +set(Boost_USE_HEADER_ONLY OFF) +find_package(Boost REQUIRED COMPONENTS system log) find_package(PkgConfig REQUIRED) find_package(FLEX REQUIRED) find_package(BISON REQUIRED) @@ -116,11 +126,18 @@ add_subdirectory(devices) # Main executable add_executable(salmanoff main.cpp) target_link_libraries(salmanoff + Boost::system Boost::log smocore - ${Boost_LIBRARIES} ${DL_LIBRARY} ) +# Verify Boost dynamic dependencies after build +add_custom_command(TARGET salmanoff POST_BUILD + COMMAND ${CMAKE_COMMAND} -DVERIFY_FILE="$" + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake + COMMENT "Verifying Boost dynamic dependencies for salmanoff" +) + # Add all registered DAPSS targets as dependencies add_all_daps_dependencies() diff --git a/cmake/VerifyBoostDynamic.cmake b/cmake/VerifyBoostDynamic.cmake new file mode 100644 index 0000000..fd71bf7 --- /dev/null +++ b/cmake/VerifyBoostDynamic.cmake @@ -0,0 +1,63 @@ +# SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY +# Verifies that a target file (executable or shared library) has Boost libraries +# in its dynamic dependency list via ldd. +# +# Usage as function: +# SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY() +# +# Usage as script (with -P): +# cmake -DVERIFY_FILE= -P VerifyBoostDynamic.cmake +# +# This function/script: +# 1. Runs ldd on the target file +# 2. Checks for boost libraries in the dependency list +# 3. Reports success or failure with appropriate messages +# +function(SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY target_file) + _verify_boost_dynamic_dependency("${target_file}") +endfunction() + +# Internal implementation that can be called from script mode or function mode +function(_verify_boost_dynamic_dependency target_file) + if(NOT EXISTS "${target_file}") + message(WARNING "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: Target file '${target_file}' does not exist") + return() + endif() + + # Run ldd on the target file + execute_process( + COMMAND ldd "${target_file}" + OUTPUT_VARIABLE ldd_output + ERROR_VARIABLE ldd_error + RESULT_VARIABLE ldd_result + ) + + if(ldd_result) + message(WARNING "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: Failed to run ldd on '${target_file}': ${ldd_error}") + return() + endif() + + # Check if output contains boost libraries + string(TOLOWER "${ldd_output}" ldd_output_lower) + string(FIND "${ldd_output_lower}" "libboost" boost_found) + + if(boost_found EQUAL -1) + message(STATUS "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: WARNING - No Boost libraries found in dependencies of '${target_file}'") + message(STATUS "ldd output:") + message(STATUS "${ldd_output}") + else() + # Extract boost library lines + string(REGEX MATCHALL "libboost[^\n]*" boost_libs "${ldd_output}") + message(STATUS "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: SUCCESS - Boost libraries found in '${target_file}':") + foreach(boost_lib ${boost_libs}) + string(STRIP "${boost_lib}" boost_lib_stripped) + message(STATUS " ${boost_lib_stripped}") + endforeach() + endif() +endfunction() + +# Script mode: if VERIFY_FILE is defined, run the verification +if(VERIFY_FILE) + _verify_boost_dynamic_dependency("${VERIFY_FILE}") +endif() + diff --git a/commonLibs/attachmentSupport/CMakeLists.txt b/commonLibs/attachmentSupport/CMakeLists.txt index 551aa2e..9995bd5 100644 --- a/commonLibs/attachmentSupport/CMakeLists.txt +++ b/commonLibs/attachmentSupport/CMakeLists.txt @@ -11,3 +11,8 @@ target_include_directories(attachmentSupport PUBLIC ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}/include ) + +target_link_libraries(attachmentSupport PUBLIC + Boost::system + Boost::log +) diff --git a/commonLibs/attachmentSupport/attachmentSupport.cpp b/commonLibs/attachmentSupport/attachmentSupport.cpp index 935384c..f2cc361 100644 --- a/commonLibs/attachmentSupport/attachmentSupport.cpp +++ b/commonLibs/attachmentSupport/attachmentSupport.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -43,9 +44,6 @@ void StimulusBuffer::stop() void StimulusBuffer::scheduleNextTimeout(int delayMs) { -std::cout << __func__ << ": being executed on thread " - << smoHooksPtr->ComponentThread_getSelf()->name << std::endl; - if (!shouldContinue.load()) { return; } @@ -60,8 +58,6 @@ std::cout << __func__ << ": being executed on thread " void StimulusBuffer::onTimeout(const boost::system::error_code& error) { -std::cout << __func__ << ": being executed on thread " - << smoHooksPtr->ComponentThread_getSelf()->name << std::endl; // Timer was cancelled, which is expected when stopping if (error == boost::asio::error::operation_aborted) { return; diff --git a/commonLibs/livoxProto1/CMakeLists.txt b/commonLibs/livoxProto1/CMakeLists.txt index 12e82f6..7b29d5f 100644 --- a/commonLibs/livoxProto1/CMakeLists.txt +++ b/commonLibs/livoxProto1/CMakeLists.txt @@ -13,7 +13,14 @@ if(ENABLE_LIB_livoxProto1) # Set config define for header generation add_compile_definitions(CONFIG_LIB_LIVOXPROTO1_ENABLED) target_include_directories(livoxProto1 PUBLIC ${Boost_INCLUDE_DIRS}) - target_link_libraries(livoxProto1 ${Boost_LIBRARIES}) + target_link_libraries(livoxProto1 PUBLIC Boost::system Boost::log) + + # Verify Boost dynamic dependencies after build + add_custom_command(TARGET livoxProto1 POST_BUILD + COMMAND ${CMAKE_COMMAND} -DVERIFY_FILE="$" + -P ${CMAKE_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake + COMMENT "Verifying Boost dynamic dependencies for livoxProto1" + ) # Install rules install(TARGETS livoxProto1 DESTINATION lib) diff --git a/commonLibs/livoxProto1/broadcastListener.h b/commonLibs/livoxProto1/broadcastListener.h index cea9e81..f5aeab4 100644 --- a/commonLibs/livoxProto1/broadcastListener.h +++ b/commonLibs/livoxProto1/broadcastListener.h @@ -1,6 +1,7 @@ #ifndef BROADCAST_LISTENER_H #define BROADCAST_LISTENER_H +#include #include #include #include diff --git a/commonLibs/livoxProto1/device.cpp b/commonLibs/livoxProto1/device.cpp index b30b3a8..a3ada96 100644 --- a/commonLibs/livoxProto1/device.cpp +++ b/commonLibs/livoxProto1/device.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/commonLibs/livoxProto1/device.h b/commonLibs/livoxProto1/device.h index f44617b..563e79c 100644 --- a/commonLibs/livoxProto1/device.h +++ b/commonLibs/livoxProto1/device.h @@ -1,6 +1,7 @@ #ifndef LIVOX_PROTO1_DEVICE_H #define LIVOX_PROTO1_DEVICE_H +#include #include #include #include diff --git a/commonLibs/livoxProto1/livoxProto1.cpp b/commonLibs/livoxProto1/livoxProto1.cpp index 7948747..cfd3718 100644 --- a/commonLibs/livoxProto1/livoxProto1.cpp +++ b/commonLibs/livoxProto1/livoxProto1.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/commonLibs/livoxProto1/livoxProto1.h b/commonLibs/livoxProto1/livoxProto1.h index 4c5dabc..51d68ae 100644 --- a/commonLibs/livoxProto1/livoxProto1.h +++ b/commonLibs/livoxProto1/livoxProto1.h @@ -1,6 +1,7 @@ #ifndef LIVOXPROTO1_H #define LIVOXPROTO1_H +#include #include #include #include diff --git a/commonLibs/livoxProto1/protocol.h b/commonLibs/livoxProto1/protocol.h index 4ffa4ca..ae51437 100644 --- a/commonLibs/livoxProto1/protocol.h +++ b/commonLibs/livoxProto1/protocol.h @@ -1,6 +1,7 @@ #ifndef LIVOXPROTO1_PROTOCOL_H #define LIVOXPROTO1_PROTOCOL_H +#include #include #include #include diff --git a/commonLibs/livoxProto1/udpCommandDemuxer.h b/commonLibs/livoxProto1/udpCommandDemuxer.h index 1bdc4cf..ab53119 100644 --- a/commonLibs/livoxProto1/udpCommandDemuxer.h +++ b/commonLibs/livoxProto1/udpCommandDemuxer.h @@ -1,6 +1,7 @@ #ifndef UDP_COMMAND_DEMUXER_H #define UDP_COMMAND_DEMUXER_H +#include #include #include #include diff --git a/smocore/CMakeLists.txt b/smocore/CMakeLists.txt index 6da8833..92aeb18 100644 --- a/smocore/CMakeLists.txt +++ b/smocore/CMakeLists.txt @@ -46,8 +46,13 @@ endif() target_include_directories(smocore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR} + ${Boost_INCLUDE_DIRS} ) # Link against pthread for CPU affinity functions find_package(Threads REQUIRED) -target_link_libraries(smocore PRIVATE Threads::Threads) +target_link_libraries(smocore PRIVATE + Threads::Threads + Boost::system + Boost::log +) diff --git a/smocore/componentThread.cpp b/smocore/componentThread.cpp index e19c975..c1094ec 100644 --- a/smocore/componentThread.cpp +++ b/smocore/componentThread.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/smocore/include/boostAsioLinkageFix.h b/smocore/include/boostAsioLinkageFix.h new file mode 100644 index 0000000..8756b5c --- /dev/null +++ b/smocore/include/boostAsioLinkageFix.h @@ -0,0 +1,25 @@ +#ifndef BOOST_ASIO_LINKAGE_FIX_H +#define BOOST_ASIO_LINKAGE_FIX_H + +#include +#include +#include + +namespace boost { +namespace asio { +namespace detail { + +/** EXPLANATION: + * Extern declaration of the template instantiation + * This ensures that the .o translation units don't have their + * own copies of `call_stack<>::top_` defined in them. + */ +extern template +tss_ptr::context> +call_stack::top_; + +} // namespace detail +} // namespace asio +} // namespace boost + +#endif // BOOST_ASIO_LINKAGE_FIX_H diff --git a/smocore/include/componentThread.h b/smocore/include/componentThread.h index 88b596d..181f759 100644 --- a/smocore/include/componentThread.h +++ b/smocore/include/componentThread.h @@ -1,6 +1,7 @@ #ifndef COMPONENT_THREAD_H #define COMPONENT_THREAD_H +#include #include #include #include diff --git a/smocore/include/deviceManager/deviceReattacher.h b/smocore/include/deviceManager/deviceReattacher.h index 649c4da..f652318 100644 --- a/smocore/include/deviceManager/deviceReattacher.h +++ b/smocore/include/deviceManager/deviceReattacher.h @@ -1,6 +1,7 @@ #ifndef DEVICEREATTACHER_H #define DEVICEREATTACHER_H +#include #include #include #include diff --git a/smocore/marionette/main.cpp b/smocore/marionette/main.cpp index 6d382b3..83740e6 100644 --- a/smocore/marionette/main.cpp +++ b/smocore/marionette/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/stimBuffApis/livoxGen1/CMakeLists.txt b/stimBuffApis/livoxGen1/CMakeLists.txt index b8ea75c..b94beec 100644 --- a/stimBuffApis/livoxGen1/CMakeLists.txt +++ b/stimBuffApis/livoxGen1/CMakeLists.txt @@ -21,8 +21,9 @@ if(ENABLE_STIMBUFFAPI_livoxGen1) ${CMAKE_SOURCE_DIR}/commonLibs ${URING_INCLUDE_DIRS} ) - target_link_libraries(livoxGen1 - ${Boost_LIBRARIES} + target_link_libraries(livoxGen1 PUBLIC + Boost::system + Boost::log ${URING_LIBRARIES} attachmentSupport ) @@ -30,6 +31,13 @@ if(ENABLE_STIMBUFFAPI_livoxGen1) ${URING_LIBRARY_DIRS} ) + # Verify Boost dynamic dependencies after build + add_custom_command(TARGET livoxGen1 POST_BUILD + COMMAND ${CMAKE_COMMAND} -DVERIFY_FILE="$" + -P ${CMAKE_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake + COMMENT "Verifying Boost dynamic dependencies for livoxGen1" + ) + # Install rules install(TARGETS livoxGen1 DESTINATION lib) endif() diff --git a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp index 7b78f38..57d10ea 100644 --- a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp +++ b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h index 258afdb..9a63eae 100644 --- a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h +++ b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h @@ -1,6 +1,7 @@ #ifndef _LIVOX_GEN1_IOURING_ASSEMBLY_ENGINE_H #define _LIVOX_GEN1_IOURING_ASSEMBLY_ENGINE_H +#include #include #include #include diff --git a/stimBuffApis/livoxGen1/livoxGen1.cpp b/stimBuffApis/livoxGen1/livoxGen1.cpp index 4a2906d..08e7284 100644 --- a/stimBuffApis/livoxGen1/livoxGen1.cpp +++ b/stimBuffApis/livoxGen1/livoxGen1.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -16,13 +17,14 @@ #include #include #include "pcloudStimulusBuffer.h" +#include "livoxGen1.h" namespace smo { namespace stim_buff { // Salmanoff hooks, obtained from SMO_GET_STIM_BUFF_API_DESC_FN_NAME(). -static const SmoCallbacks* smoHooksPtr = nullptr; +const SmoCallbacks* smoHooksPtr = nullptr; static SmoThreadingModelDesc smoThreadingModelDesc; // Local collection of stimulus buffers