Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-33974

Enable GNU libstdc++ debugging

    XMLWordPrintable

Details

    Description

      The regression MDEV-33972 was only caught in a rather obscure way: a bit of an unrelated pointer was being cleared, because some code was dereferencing an invalid iterator.

      It turns out that there is a simple way to enable debugging for the iterators defined in the GNU libstdc++:

      diff --git a/storage/innobase/CMakeLists.txt b/storage/innobase/CMakeLists.txt
      index 5f01f8c9704..bccf81a6b5b 100644
      --- a/storage/innobase/CMakeLists.txt
      +++ b/storage/innobase/CMakeLists.txt
      @@ -78,7 +78,7 @@ ADD_FEATURE_INFO(INNODB_ROOT_GUESS WITH_INNODB_ROOT_GUESS
       
       OPTION(WITH_INNODB_EXTRA_DEBUG "Enable extra InnoDB debug checks" OFF)
       IF(WITH_INNODB_EXTRA_DEBUG)
      -  ADD_DEFINITIONS(-DUNIV_ZIP_DEBUG -DLOG_LATCH_DEBUG)
      +  ADD_DEFINITIONS(-DUNIV_ZIP_DEBUG -DLOG_LATCH_DEBUG -D_GLIBCXX_DEBUG)
       ENDIF()
       ADD_FEATURE_INFO(INNODB_EXTRA_DEBUG WITH_INNODB_EXTRA_DEBUG "Extra InnoDB debug checks")
       
      

      However, this has no effect on the clang libc++, which is a prerequisite for MemorySanitizer builds (MDEV-20377). There used to be -D_LIBCPP_DEBUG, but it has been split into multiple flags:

      diff --git a/libcxx/include/__assert b/libcxx/include/__assert
      index d94a657aac50..e20ee50b127c 100644
      --- a/libcxx/include/__assert
      +++ b/libcxx/include/__assert
      @@ -17,11 +17,6 @@
       #  pragma GCC system_header
       #endif
       
      -// TODO: Remove in LLVM 17.
      -#if defined(_LIBCPP_DEBUG)
      -#  error "Defining _LIBCPP_DEBUG is not supported anymore. Please use _LIBCPP_ENABLE_DEBUG_MODE instead."
      -#endif
      -
       // Automatically enable assertions when the debug mode is enabled.
       #if defined(_LIBCPP_ENABLE_DEBUG_MODE)
       #  ifndef _LIBCPP_ENABLE_ASSERTIONS
      

      I did not have any luck with that flag either. The libstdc++ flag works:

      10.11 f0d0ddc992703f2d31d8ad0d963f127a8b4d05e9

      /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/debug/safe_iterator.h:372:
      In function:
          pointer 
          gnu_debug::_Safe_iterator<std::_Rb_tree_iterator<std::pair<dict_table_t 
          *const, trx_mod_table_time_t>>, std::map<dict_table_t *, 
          trx_mod_table_time_t, std::less<dict_table_t *>, 
          ut_allocator<std::pair<dict_table_t *const, trx_mod_table_time_t>>>, 
          std::forward_iterator_tag>::operator->() const [_Iterator = 
          std::_Rb_tree_iterator<std::pair<dict_table_t *const, 
          trx_mod_table_time_t>>, _Sequence = std::map<dict_table_t *, 
          trx_mod_table_time_t, std::less<dict_table_t *>, 
          ut_allocator<std::pair<dict_table_t *const, trx_mod_table_time_t>>>, 
          _Category = std::forward_iterator_tag]
       
      Error: attempt to dereference a past-the-end iterator.
       
      Objects involved in the operation:
          iterator "this" @ 0x7fb892ffd7a8 {
            type = std::_Rb_tree_iterator<std::pair<dict_table_t* const, trx_mod_table_time_t> > (mutable iterator);
            state = past-the-end;
            references sequence with type 'std::debug::map<dict_table_t*, trx_mod_table_time_t, std::less<dict_table_t*>, ut_allocator<std::pair<dict_table_t* const, trx_mod_table_time_t>, true> >' @ 0x7fb8918016b0
          }
      240423 14:07:36 [ERROR] mysqld got signal 6 ;
      

      I would prefer something like the following to cover all use of iterators:

      diff --git a/CMakeLists.txt b/CMakeLists.txt
      index 6477c4042d6..e0d73899632 100644
      --- a/CMakeLists.txt
      +++ b/CMakeLists.txt
      @@ -277,7 +277,14 @@ ENDIF()
       # Always enable debug sync for debug builds.
       SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DENABLED_DEBUG_SYNC")
       SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DENABLED_DEBUG_SYNC")
      - 
      +
      +IF(MSVC)
      +  SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_ITERATOR_DEBUG_LEVEL=1")
      +ELSE()
      +  # Enable extra checks on GNU libstdc++ iterators
      +  SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG")
      +ENDIF()
      +
       OPTION(ENABLE_GCOV "Enable gcov (debug, Linux builds only)" OFF)
       IF (ENABLE_GCOV)
         MY_CHECK_AND_SET_COMPILER_FLAG("-DHAVE_gcov -fprofile-arcs -ftest-coverage -lgcov" DEBUG)
      

      Unfortunately it would cause link-time failures like this:

      10.11 f0d0ddc992703f2d31d8ad0d963f127a8b4d05e9

      ld.lld: error: undefined symbol: wsrep::client_state::enter_toi_local(std::__debug::vector<wsrep::key, std::allocator<wsrep::key>> const&, wsrep::const_buffer const&, std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>>)
      >>> referenced by wsrep_mysqld.cc:2794 (/mariadb/10.11/sql/wsrep_mysqld.cc:2794)
      >>>               wsrep_mysqld.cc.o:(wsrep_TOI_begin(THD*, char const*, char const*, TABLE_LIST const*, Alter_info const*, std::__debug::vector<wsrep::key, std::allocator<wsrep::key>> const*, HA_CREATE_INFO const*)) in archive sql/libwsrep.a
      

      I see that wsrep-lib/CMakeLists.txt is doing something to the project-wide CMAKE_CXX_FLAGS; this might be the reason.

      Attachments

        Issue Links

          Activity

            People

              marko Marko Mäkelä
              marko Marko Mäkelä
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.