在 gcc 中使用 fsanitize 开关的 Boost 出现问题

Issues with Boost using fsanitize switch in gcc

本文关键字:Boost 问题 开关 fsanitize gcc      更新时间:2023-10-16

我一直在尝试利用一些 GCC 检测选项进行运行时检查,以尝试调试/诊断我在C++代码的特定区域中遇到的问题。

为了尝试缩小问题范围,我开始启用其中一些功能,但在我意想不到的代码区域中遇到了错误 - 即在 Boost 库中。

当我使用 gcc 开关 '-fsanitize=undefined' 时,我注意到 gdb 报告了许多运行时错误;我似乎能够毫不费力地重现这些错误。 我在下面提供了一些示例代码。

环境配置:

  • Ubuntu v16.10 (Yakkety) x86_x64
  • Boost v1.61 (使用 apt-get)
  • GCC 6.3.0(最近从源代码构建,但来自 apt-get 的版本出现问题)
  • GDB 7.11.90
  • CMake v3.7 (来自cmake网站)

示例代码如下:

// $TEST_BEGIN_HEADER$                                                    //
// $TEST_END_HEADER$                                                      //

// Libraries
#include <boost/log/trivial.hpp>
// Test Headers
// Forward Declarations

typedef boost::log::sources::wseverity_logger_mt< boost::log::trivial::severity_level>   test_mt_wlogger;
#define TEST_LOG_TRACE(lg)        BOOST_LOG_SEV(lg, boost::log::trivial::trace)
#define TEST_LOG_DEBUG(lg)        BOOST_LOG_SEV(lg, boost::log::trivial::debug)
#define TEST_LOG_INFO(lg)         BOOST_LOG_SEV(lg, boost::log::trivial::info)
#define TEST_LOG_WARNING(lg)      BOOST_LOG_SEV(lg, boost::log::trivial::warning)
#define TEST_LOG_ERROR(lg)        BOOST_LOG_SEV(lg, boost::log::trivial::error)
#define TEST_LOG_FATAL(lg)        BOOST_LOG_SEV(lg, boost::log::trivial::fatal)

///////////////////////////////////////////////////////////////////////////
int main(int ac, char* av[])
{
test_mt_wlogger lg;
TEST_LOG_INFO(lg) << L"Example!";
return 0;
}

// $TEST_BEGIN_FOOTER$                                                    //
// $TEST_END_FOOTER$                                                      //

CMake文件如下所示:

cmake_minimum_required (VERSION 3.4)
project(Test)
message(STATUS "Project:    ${PROJECT_NAME}")
message(STATUS "Platform:   ${CMAKE_SYSTEM_NAME} (${CMAKE_SYSTEM})")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

add_definitions(/DUNICODE)

# Options added:
# 1. Using C++ 14.
# 2. Increase warning level
# 3. Make all warnings into errors.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=undefined")
################################################################
# Find Thread - used implicitly by GTest
find_package(Threads REQUIRED)
################################################################
# Find Boost.
# This flag may need to change depending on the version of
# Boost installed.
set(Boost_USE_STATIC_LIBS        OFF) # only find static libs
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME     OFF)
find_package(Boost REQUIRED COMPONENTS thread system chrono log)

# Set the location where our binaries will be stored.
# WARN/TODO: Not quite right, because .lib or .a files should
#            not go in the bin folder!
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Adds 'd' onto artifacts - does NOT apply to executables.
# For executables, this needs to be done an exec-by-exec
# basis.
set(CMAKE_DEBUG_POSTFIX "d")

# Additional Include Directories
include_directories(    #Third Party
${Boost_INCLUDE_DIR}
#Local Directories
)
# Define an executable
add_executable(vt_test
main.cpp
)
# Add postfix onto executable debug filename
set_target_properties(vt_test PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
add_definitions(-DBOOST_LOG_DYN_LINK)
# Define the libraries this project depends upon
target_link_libraries(vt_test
# Local Libraries
# Third Party
Boost::log )

GDB 示例输出:

(gdb) step
/usr/include/boost/log/utility/formatting_ostream.hpp:669:17: runtime error: member call on address 0x55555577c028 which does not point to an object of type 'basic_ostringstreambuf'
0x55555577c028: note: object is of type 'boost::log::v2_mt_posix::aux::basic_ostringstreambuf<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >'
00 00 00 00  70 3a dd f7 ff 7f 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
^~~~~~~~~~~~~~~~~~~~~~~
vptr for 'boost::log::v2_mt_posix::aux::basic_ostringstreambuf<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >'
boost::log::v2_mt_posix::aux::basic_ostringstreambuf<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::storage[abi:cxx11]() const (this=0x55555577c028) at /usr/include/boost/log/detail/attachable_sstream_buf.hpp:109
109     string_type* storage() const { return m_Storage; }
(gdb) bt
#0  boost::log::v2_mt_posix::aux::basic_ostringstreambuf<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::storage[abi:cxx11]() const (this=0x55555577c028) at /usr/include/boost/log/detail/attachable_sstream_buf.hpp:109
#1  0x000055555555e172 in boost::log::v2_mt_posix::basic_formatting_ostream<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::formatted_write (this=0x55555577c028, p=0x55555555fb48 L"Example!", size=8) at /usr/include/boost/log/utility/formatting_ostream.hpp:669
#2  0x000055555555d317 in boost::log::v2_mt_posix::basic_formatting_ostream<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::operator<< (this=0x55555577c028, p=0x55555555fb48 L"Example!") at /usr/include/boost/log/utility/formatting_ostream.hpp:416
#3  0x000055555555c4e5 in boost::log::v2_mt_posix::operator<< <boost::log::v2_mt_posix::basic_record_ostream<wchar_t>, wchar_t [9]> (
strm=..., value=...) at /usr/include/boost/log/sources/record_ostream.hpp:203
#4  0x000055555555a3a2 in main (ac=1, av=0x7fffffffdf68) at /home/user/code/test/main.cpp:31

在对我的发现有一些想法之后,我收集了一些问题:

  1. 这是编码错误吗?
  2. 这让你们感到惊讶吗?
  3. 这些错误是误报吗?
  4. 你们中有人使用"-fsanitize"开关吗?
  5. 我应该尝试较新版本的 Boost(即从源代码构建的 v1.63)吗?
  6. 我可以在我的系统上同时安装 v1.61 和 v1.63 吗(但只能链接到其中一个)?
  1. 是的
  2. 否(或是,但不太可能直接相关)
  3. 是的,为什么?(另请参阅)

我认为您的答案(使用水晶球并且尚未花时间查看实际代码)可能在这里: Boost 关机时 Boost 登录中的 Boost 线程访问冲突