C++ コードの特定の領域で発生している問題をデバッグ/診断するために、実行時チェック用のGCC インストルメンテーション オプションの一部を利用しようとしています。
問題を絞り込むために、これらの機能のいくつかを有効にし始めましたが、予期していなかったコードの領域、つまり Boost ライブラリでエラーが発生しました。
gcc スイッチ '-fsanitize=undefined' を使用すると、gdb によって報告される多数の実行時エラーがあることに気付きました。これらのエラーは、ほとんど手間をかけずに再現できるようです。以下にサンプルコードをいくつか用意しました。
環境構成:
- Ubuntu v16.10 (Yakkety) x86_x64
- ブースト v1.61 (apt-get を使用)
- GCC 6.3.0 (最近ソースからビルドされましたが、apt-get のバージョンで問題が発生しました)
- GDB 7.11.90
- CMake v3.7 (cmake Web サイトから)
サンプルコードは次のとおりです。
// $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$ //
CMakefile は次のようになります。
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
私の調査結果についていくつか考えた後、いくつかの質問をまとめました。
- これはコーディングエラーですか?
- これはあなたを驚かせますか?
- これらのエラーは誤検知ですか?
- 「-fsanitize」スイッチを使用している人はいますか?
- Boost の新しいバージョン (つまり、ソースからビルドされた v1.63) を試す必要がありますか?
- 自分のシステムに v1.61 と v1.63 の両方をインストールできますか (ただし、リンク先はいずれか 1 つに限られます)?