From 3e593d9ab5bbf96d1261ed23ba0bc502434d8f75 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Wed, 8 Dec 2021 08:31:08 -0500 Subject: [PATCH] Hide AddressSanitizer flags behind an option We don't want this to accidentally activate if -lasan is no longer a requirement someday - BRL-CAD's code isn't ready for it yet. In particular, the LeakSanitizer trips up on the rt_free_rti/rt_clean tangle as well as _db_walk_subtree. The former may need elimination of the rt_uniresource global to properly clean up, and the latter is deep in the raytracing core and will have to be approached very carefully. For convenience, here are the backtraces: 889: Test command: build/bin/gqa "-P" "1" "-Ao" "-p" "ovlp_" "brlcad/src/gtools/tests/../../libged/tests/ged_tests.g" "ovlp" 889: Test timeout computed to be: 1500 889: Trying initial grid spacing: 50 mm 889: Using grid spacing lower limit: 0.005 mm 889: Plotting overlaps to ovlp_overlaps.plot3 889: Processing with grid spacing 50 mm 44 x 48 x 39 889: Summary (50mm grid spacing): 889: list Overlaps: 889: /ovlp/r1 /ovlp/r2 count:2277 dist:1780.3mm @ (-248 350 -903.325) 889: 889: ================================================================= 889: ==672620==ERROR: LeakSanitizer: detected memory leaks 889: 889: Direct leak of 524288 byte(s) in 1024 object(s) allocated from: 889: #0 0x498087 in posix_memalign (build/bin/gqa+0x498087) 889: #1 0x7f9d3ca476c3 in alloc brlcad/src/libbu/malloc.c:137:10 889: #2 0x7f9d3ca478f6 in bu_calloc brlcad/src/libbu/malloc.c:174:12 889: #3 0x7f9d3ca9b25e in bu_ptbl_init brlcad/src/libbu/ptbl.c:45:26 889: #4 0x7f9d40d38be4 in rt_init_resource brlcad/src/librt/prep.c:855:2 889: #5 0x7f9d40d3ba65 in rt_clean_resource brlcad/src/librt/prep.c:1063:5 889: #6 0x7f9d40d3239a in rt_clean brlcad/src/librt/prep.c:1210:6 889: #7 0x7f9d40d30f80 in rt_free_rti brlcad/src/librt/prep.c:170:5 889: #8 0x7f9d380c4573 () 889: #9 0x7f9d424f2526 in ged_exec brlcad/src/libged/exec.cpp:83:16 889: #10 0x7f9d424fd51b in ged_gqa brlcad/src/libged/exec_mapping.cpp:177:1 889: #11 0x4c8b17 in main brlcad/src/gtools/gqa.c:105:11 889: #12 0x7f9d3c6be0b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16 889: 889: Direct leak of 524288 byte(s) in 1024 object(s) allocated from: 889: #0 0x498087 in posix_memalign (build/bin/gqa+0x498087) 889: #1 0x7f9d3ca476c3 in alloc brlcad/src/libbu/malloc.c:137:10 889: #2 0x7f9d3ca478f6 in bu_calloc brlcad/src/libbu/malloc.c:174:12 889: #3 0x7f9d3ca9b25e in bu_ptbl_init brlcad/src/libbu/ptbl.c:45:26 889: #4 0x7f9d40d38c65 in rt_init_resource brlcad/src/librt/prep.c:858:2 889: #5 0x7f9d380c0db2 () 889: #6 0x7f9d424f2526 in ged_exec brlcad/src/libged/exec.cpp:83:16 889: #7 0x7f9d424fd51b in ged_gqa brlcad/src/libged/exec_mapping.cpp:177:1 889: #8 0x4c8b17 in main brlcad/src/gtools/gqa.c:105:11 889: #9 0x7f9d3c6be0b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16 889: 889: Direct leak of 64 byte(s) in 2 object(s) allocated from: 889: #0 0x498087 in posix_memalign (build/bin/gqa+0x498087) 889: #1 0x7f9d3ca476c3 in alloc brlcad/src/libbu/malloc.c:137:10 889: #2 0x7f9d3ca478f6 in bu_calloc brlcad/src/libbu/malloc.c:174:12 889: #3 0x7f9d40ce3610 in _db_walk_subtree brlcad/src/librt/db_tree.c:1914:6 889: #4 0x7f9d40cdd664 in _db_walk_dispatcher brlcad/src/librt/db_tree.c:1992:2 889: #5 0x7f9d3ca795da in bu_parallel brlcad/src/libbu/parallel.c:574:2 889: #6 0x7f9d40cdc2a2 in db_walk_tree brlcad/src/librt/db_tree.c:2199:5 889: #7 0x7f9d41893a86 in rt_gettrees_muves brlcad/src/librt/tree.c:779:6 889: #8 0x7f9d4189bb3f in rt_gettrees_and_attrs brlcad/src/librt/tree.c:883:12 889: #9 0x7f9d4189bcba in rt_gettree brlcad/src/librt/tree.c:896:11 889: #10 0x7f9d380c0e88 () 889: #11 0x7f9d424f2526 in ged_exec brlcad/src/libged/exec.cpp:83:16 889: #12 0x7f9d424fd51b in ged_gqa brlcad/src/libged/exec_mapping.cpp:177:1 889: #13 0x4c8b17 in main brlcad/src/gtools/gqa.c:105:11 889: #14 0x7f9d3c6be0b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16 889: 889: Direct leak of 64 byte(s) in 2 object(s) allocated from: 889: #0 0x498087 in posix_memalign (build/bin/gqa+0x498087) 889: #1 0x7f9d3ca476c3 in alloc brlcad/src/libbu/malloc.c:137:10 889: #2 0x7f9d3ca478f6 in bu_calloc brlcad/src/libbu/malloc.c:174:12 889: #3 0x7f9d40c2a4bc in rt_comb_import5 brlcad/src/librt/comb/comb.c:518:6 889: #4 0x7f9d40c79613 in rt_db_external5_to_internal5 brlcad/src/librt/db5_io.c:958:8 889: #5 0x7f9d40c79e9f in rt_db_get_internal5 brlcad/src/librt/db5_io.c:998:11 889: #6 0x7f9d40ce6b76 in rt_db_get_internal brlcad/src/librt/dir.c:131:9 889: #7 0x7f9d40cd31b4 in db_recurse brlcad/src/librt/db_tree.c:1016:6 889: #8 0x7f9d40ce34ee in _db_walk_subtree brlcad/src/librt/db_tree.c:1898:16 889: #9 0x7f9d40cdd664 in _db_walk_dispatcher brlcad/src/librt/db_tree.c:1992:2 889: #10 0x7f9d3ca795da in bu_parallel brlcad/src/libbu/parallel.c:574:2 889: #11 0x7f9d40cdc2a2 in db_walk_tree brlcad/src/librt/db_tree.c:2199:5 889: #12 0x7f9d41893a86 in rt_gettrees_muves brlcad/src/librt/tree.c:779:6 889: #13 0x7f9d4189bb3f in rt_gettrees_and_attrs brlcad/src/librt/tree.c:883:12 889: #14 0x7f9d4189bcba in rt_gettree brlcad/src/librt/tree.c:896:11 889: #15 0x7f9d380c0e88 () 889: #16 0x7f9d424f2526 in ged_exec brlcad/src/libged/exec.cpp:83:16 889: #17 0x7f9d424fd51b in ged_gqa brlcad/src/libged/exec_mapping.cpp:177:1 889: #18 0x4c8b17 in main brlcad/src/gtools/gqa.c:105:11 889: #19 0x7f9d3c6be0b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16 889: 889: SUMMARY: AddressSanitizer: 1048704 byte(s) leaked in 2052 allocation(s). --- CMakeLists.txt | 6 +++--- misc/tools/env2c/CMakeLists.txt | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 56c6427e0a2..561c8b11185 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1054,8 +1054,8 @@ if(NOT MSVC) # Similarly, enable the AddressSanitizer memory address sanitizer. # See https://code.google.com/p/address-sanitizer/ for more info. # This typically is reported to cause a 2x slowdown. - if(${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) - #set(CMAKE_REQUIRED_LINK_OPTIONS "${CMAKE_REQUIRED_LINK_OPTIONS};-lasan") + if(BRLCAD_ENABLE_ADDRESS_SANITIZER && ${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) + set(CMAKE_REQUIRED_LINK_OPTIONS "${CMAKE_REQUIRED_LINK_OPTIONS};-lasan") CHECK_C_FLAG(fsanitize=address) CHECK_C_FLAG(fsanitize-address-use-after-return=always) CHECK_C_FLAG(fsanitize-address-use-after-scope) @@ -1064,7 +1064,7 @@ if(NOT MSVC) CHECK_CXX_FLAG(fsanitize-address-use-after-return=always) CHECK_CXX_FLAG(fsanitize-address-use-after-scope) CHECK_CXX_FLAG(fno-omit-frame-pointer) - endif(${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) + endif(BRLCAD_ENABLE_ADDRESS_SANITIZER && ${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) # Differently, enable the ThreadSanitizer race condition detector # only as requested since it can incur a 5x-15x slowdown. See diff --git a/misc/tools/env2c/CMakeLists.txt b/misc/tools/env2c/CMakeLists.txt index edc0c4b1c0c..6fab6c8d2c6 100644 --- a/misc/tools/env2c/CMakeLists.txt +++ b/misc/tools/env2c/CMakeLists.txt @@ -48,10 +48,10 @@ find_package(Threads) include(CheckCXXCompilerFlag) check_cxx_compiler_flag(-O3 O3_COMPILER_FLAG) -#if(${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) -# set(CMAKE_REQUIRED_LINK_OPTIONS "${CMAKE_REQUIRED_LINK_OPTIONS};-lasan") -# check_cxx_compiler_flag(-fsanitize=address SANITIZE_ADDRESS_FLAG) -#endif(${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) +if(BRLCAD_ENABLE_ADDRESS_SANITIZER && ${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) + set(CMAKE_REQUIRED_LINK_OPTIONS "${CMAKE_REQUIRED_LINK_OPTIONS};-lasan") + check_cxx_compiler_flag(-fsanitize=address SANITIZE_ADDRESS_FLAG) +endif(BRLCAD_ENABLE_ADDRESS_SANITIZER && ${BRLCAD_OPTIMIZED_BUILD} MATCHES "OFF" AND BRLCAD_FLAGS_DEBUG) add_executable(env2c env2c.cxx) target_link_libraries(env2c Threads::Threads) @@ -60,14 +60,14 @@ if (O3_COMPILER_FLAG) # If we have the O3 flag, use it target_compile_options(env2c PRIVATE "-O3") endif (O3_COMPILER_FLAG) -#if (SANITIZE_ADDRESS_FLAG) -# # With clang, if we are using the address sanitizer it changes -# # the results of the -lpthread check - using the parent build's -# # find package results, we need to also match its use of this -# # compiler flag or the threading linkage will fail -# target_compile_options(env2c PRIVATE "-fsanitize=address") -# target_link_libraries(env2c -lasan) -#endif (SANITIZE_ADDRESS_FLAG) +if (BRLCAD_ENABLE_ADDRESS_SANITIZER && SANITIZE_ADDRESS_FLAG) + # With clang, if we are using the address sanitizer it changes + # the results of the -lpthread check - using the parent build's + # find package results, we need to also match its use of this + # compiler flag or the threading linkage will fail + target_compile_options(env2c PRIVATE "-fsanitize=address") + target_link_libraries(env2c -lasan) +endif (BRLCAD_ENABLE_ADDRESS_SANITIZER && SANITIZE_ADDRESS_FLAG) # Local Variables: