From e743bf5a93ff66059f0f06c75670130027a0117f Mon Sep 17 00:00:00 2001 From: "Madsen, Jonathan" Date: Thu, 6 Feb 2025 08:55:57 -0600 Subject: [PATCH] Undefined behavior warnings caught by ROCPROFILER_DEFAULT_FAIL_REGEX (#23) * Add regex for undefined behavior to ROCPROFILER_DEFAULT_FAIL_REGEX - add UBSAN_OPTIONS to setup-sanitizer-env.sh * Improve ROCPROFILER_DEFAULT_FAIL_REGEX * Use -fno-sanitize-recover=undefined flag - this compiler flag causes all undefined behavior errors to exit * Revert ROCPROFILER_DEFAULT_FAIL_REGEX * fix for shift overflow --------- Co-authored-by: Jonathan R. Madsen Co-authored-by: Manjunath-Jakaraddi --- cmake/rocprofiler_memcheck.cmake | 14 ++++++++------ samples/common/CMakeLists.txt | 4 ++-- .../tests/CMakeLists.txt | 6 ++++-- .../rocprofiler-sdk/aql/tests/CMakeLists.txt | 4 +++- .../lib/rocprofiler-sdk/counters/id_decode.hpp | 6 +++--- .../counters/parser/tests/CMakeLists.txt | 5 ++++- .../counters/tests/CMakeLists.txt | 7 ++++++- source/lib/rocprofiler-sdk/hsa/aql_packet.hpp | 18 +++++++++--------- .../pc_sampling/parser/tests/CMakeLists.txt | 14 +++++++++++--- .../pc_sampling/tests/CMakeLists.txt | 6 ++++-- .../lib/rocprofiler-sdk/tests/CMakeLists.txt | 10 +++++++--- .../thread_trace/tests/CMakeLists.txt | 6 ++++-- source/lib/tests/buffering/CMakeLists.txt | 5 ++++- source/lib/tests/common/CMakeLists.txt | 5 ++++- source/scripts/setup-sanitizer-env.sh | 16 +++++++++++++--- tests/common/CMakeLists.txt | 4 ++-- 16 files changed, 88 insertions(+), 42 deletions(-) diff --git a/cmake/rocprofiler_memcheck.cmake b/cmake/rocprofiler_memcheck.cmake index d81b8f23de..a433d936f2 100644 --- a/cmake/rocprofiler_memcheck.cmake +++ b/cmake/rocprofiler_memcheck.cmake @@ -13,11 +13,12 @@ endif() set_property(CACHE ROCPROFILER_MEMCHECK PROPERTY STRINGS "${ROCPROFILER_MEMCHECK_TYPES}") -function(rocprofiler_add_memcheck_flags _TYPE _FLAG _LIB_BASE) +function(rocprofiler_add_memcheck_flags _TYPE _LIB_BASE _FLAG) target_compile_options( rocprofiler-sdk-memcheck INTERFACE $) + -fno-optimize-sibling-calls -fno-inline-functions -fsanitize=${_FLAG} + ${ARGN}>) target_link_options(rocprofiler-sdk-memcheck INTERFACE $) @@ -87,18 +88,19 @@ else() endif() if(ROCPROFILER_MEMCHECK STREQUAL "AddressSanitizer") - rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "address" "asan") + rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "asan" "address") rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "asan" ${AddressSanitizer_SOVERSION}) elseif(ROCPROFILER_MEMCHECK STREQUAL "LeakSanitizer") - rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "leak" "lsan") + rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "lsan" "leak") rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "lsan") elseif(ROCPROFILER_MEMCHECK STREQUAL "ThreadSanitizer") - rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "thread" "tsan") + rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "tsan" "thread") rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "tsan" ${ThreadSanitizer_SOVERSION}) elseif(ROCPROFILER_MEMCHECK STREQUAL "UndefinedBehaviorSanitizer") - rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "undefined" "ubsan") + rocprofiler_add_memcheck_flags("${ROCPROFILER_MEMCHECK}" "ubsan" "undefined" + "-fno-sanitize-recover=undefined") rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "ubsan") elseif(NOT ROCPROFILER_MEMCHECK STREQUAL "") message(FATAL_ERROR "Unsupported ROCPROFILER_MEMCHECK type: ${ROCPROFILER_MEMCHECK}") diff --git a/samples/common/CMakeLists.txt b/samples/common/CMakeLists.txt index f36a430049..363fbe3108 100644 --- a/samples/common/CMakeLists.txt +++ b/samples/common/CMakeLists.txt @@ -61,8 +61,8 @@ mark_as_advanced(libdw_INCLUDE_DIR libdw_LIBRARY) # default FAIL_REGULAR_EXPRESSION for tests set(ROCPROFILER_DEFAULT_FAIL_REGEX - "threw an exception|Permission denied|Could not create logging file" - CACHE STRING "Default FAIL_REGULAR_EXPRESSION for tests") + "threw an exception|Permission denied|Could not create logging file|failed with error code" + CACHE INTERNAL "Default FAIL_REGULAR_EXPRESSION for tests") # build flags add_library(rocprofiler-sdk-samples-build-flags INTERFACE) diff --git a/source/lib/rocprofiler-sdk-codeobj/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk-codeobj/tests/CMakeLists.txt index 2cd13dc07d..6c063c2a0b 100644 --- a/source/lib/rocprofiler-sdk-codeobj/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk-codeobj/tests/CMakeLists.txt @@ -23,8 +23,10 @@ gtest_add_tests( TEST_LIST codeobj-library-test_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${codeobj-library-test_TESTS} PROPERTIES TIMEOUT 10 LABELS - "unittests") +set_tests_properties( + ${codeobj-library-test_TESTS} + PROPERTIES TIMEOUT 10 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") target_compile_definitions(codeobj-library-test PRIVATE -DCODEOBJ_BINARY_DIR=\"${CMAKE_CURRENT_BINARY_DIR}/\") diff --git a/source/lib/rocprofiler-sdk/aql/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/aql/tests/CMakeLists.txt index bc9831bd64..7fb65a599c 100644 --- a/source/lib/rocprofiler-sdk/aql/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/aql/tests/CMakeLists.txt @@ -25,4 +25,6 @@ gtest_add_tests( TEST_LIST aql-test_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${aql-test_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") +set_tests_properties( + ${aql-test_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/rocprofiler-sdk/counters/id_decode.hpp b/source/lib/rocprofiler-sdk/counters/id_decode.hpp index 1ce4269aa0..041b4f0860 100644 --- a/source/lib/rocprofiler-sdk/counters/id_decode.hpp +++ b/source/lib/rocprofiler-sdk/counters/id_decode.hpp @@ -86,8 +86,7 @@ rocprofiler::counters::set_dim_in_rec(rocprofiler_counter_instance_id_t& rocprofiler_profile_counter_instance_types dim, size_t value) { - size_t bit_length = DIM_BIT_LENGTH / ROCPROFILER_DIMENSION_LAST; - int64_t mask = (MAX_64 >> (64 - bit_length)) << ((dim - 1) * bit_length); + size_t bit_length = DIM_BIT_LENGTH / ROCPROFILER_DIMENSION_LAST; if(dim == ROCPROFILER_DIMENSION_NONE) { @@ -100,7 +99,8 @@ rocprofiler::counters::set_dim_in_rec(rocprofiler_counter_instance_id_t& // Reset bits to 0 for dimension. Does so by getting the bit length as F's then // shifiting that into the position of dim. Not's that value and then and's it // with id. - id = (id & ~(mask)); + uint64_t mask = (MAX_64 >> (64 - bit_length)) << ((dim - 1) * bit_length); + id = (id & ~(mask)); // Set the value for the dimenstion id = id | (value << ((dim - 1) * bit_length)); } diff --git a/source/lib/rocprofiler-sdk/counters/parser/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/counters/parser/tests/CMakeLists.txt index 87821fd617..fb0115fb5f 100644 --- a/source/lib/rocprofiler-sdk/counters/parser/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/counters/parser/tests/CMakeLists.txt @@ -20,4 +20,7 @@ gtest_add_tests( TEST_LIST parser-tests_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${parser-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") +set_tests_properties( + ${parser-tests_TESTS} + PROPERTIES TIMEOUT 45 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/rocprofiler-sdk/counters/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/counters/tests/CMakeLists.txt index 7583c753c1..1bc693b771 100644 --- a/source/lib/rocprofiler-sdk/counters/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/counters/tests/CMakeLists.txt @@ -90,6 +90,8 @@ set_tests_properties( 45 LABELS "unittests" + FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}" SKIP_REGULAR_EXPRESSION "Running non-intercept test(.*)could not be locked for profiling due to lack of permissions.*" ) @@ -110,4 +112,7 @@ gtest_add_tests( TEST_LIST consumer-tests_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${consumer-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") +set_tests_properties( + ${consumer-tests_TESTS} + PROPERTIES TIMEOUT 45 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/rocprofiler-sdk/hsa/aql_packet.hpp b/source/lib/rocprofiler-sdk/hsa/aql_packet.hpp index b8dd21d1cf..534da907d6 100644 --- a/source/lib/rocprofiler-sdk/hsa/aql_packet.hpp +++ b/source/lib/rocprofiler-sdk/hsa/aql_packet.hpp @@ -100,15 +100,15 @@ class CounterAQLPacket : public AQLPacket { using desc_t = aqlprofile_buffer_desc_flags_t; - hsa_agent_t gpu_agent; - hsa_amd_memory_pool_t cpu_pool_; - hsa_amd_memory_pool_t kernarg_pool_; - decltype(hsa_amd_memory_pool_allocate)* allocate_fn; - decltype(hsa_amd_agents_allow_access)* allow_access_fn; - decltype(hsa_amd_memory_pool_free)* free_fn; - decltype(hsa_amd_memory_fill)* fill_fn; - decltype(hsa_memory_copy)* api_copy_fn; - bool bIgnoreKernArg; + hsa_agent_t gpu_agent = {.handle = 0}; + hsa_amd_memory_pool_t cpu_pool_ = {.handle = 0}; + hsa_amd_memory_pool_t kernarg_pool_ = {.handle = 0}; + decltype(hsa_amd_memory_pool_allocate)* allocate_fn = nullptr; + decltype(hsa_amd_agents_allow_access)* allow_access_fn = nullptr; + decltype(hsa_amd_memory_pool_free)* free_fn = nullptr; + decltype(hsa_amd_memory_fill)* fill_fn = nullptr; + decltype(hsa_memory_copy)* api_copy_fn = nullptr; + bool bIgnoreKernArg = false; static void Free(void* ptr, void* data); static hsa_status_t Alloc(void** ptr, size_t size, desc_t flags, void* data); diff --git a/source/lib/rocprofiler-sdk/pc_sampling/parser/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/pc_sampling/parser/tests/CMakeLists.txt index 4557a11dd2..b5283556ca 100644 --- a/source/lib/rocprofiler-sdk/pc_sampling/parser/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/pc_sampling/parser/tests/CMakeLists.txt @@ -28,7 +28,10 @@ gtest_add_tests( TEST_LIST pcs_gfx9_test_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${pcs_gfx9_test_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") +set_tests_properties( + ${pcs_gfx9_test_TESTS} + PROPERTIES TIMEOUT 45 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") add_executable(pcs_id_test) @@ -47,7 +50,9 @@ gtest_add_tests( TEST_LIST pcs_id_test_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${pcs_id_test_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") +set_tests_properties( + ${pcs_id_test_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") add_executable(pcs_bench_test) @@ -80,4 +85,7 @@ gtest_add_tests( TEST_LIST pcs_thread_test_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${pcs_thread_test_TESTS} PROPERTIES TIMEOUT 75 LABELS "unittests") +set_tests_properties( + ${pcs_thread_test_TESTS} + PROPERTIES TIMEOUT 75 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/rocprofiler-sdk/pc_sampling/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/pc_sampling/tests/CMakeLists.txt index 030a81a1b9..99aea0c478 100644 --- a/source/lib/rocprofiler-sdk/pc_sampling/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/pc_sampling/tests/CMakeLists.txt @@ -26,5 +26,7 @@ gtest_add_tests( WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_tests_properties( - ${pcs-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests;pc-sampling" - SKIP_REGULAR_EXPRESSION "PC sampling unavailable") + ${pcs-tests_TESTS} + PROPERTIES TIMEOUT 45 LABELS "unittests;pc-sampling" SKIP_REGULAR_EXPRESSION + "PC sampling unavailable" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/rocprofiler-sdk/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/tests/CMakeLists.txt index e08b02193b..64788932b4 100644 --- a/source/lib/rocprofiler-sdk/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/tests/CMakeLists.txt @@ -41,7 +41,9 @@ gtest_add_tests( TEST_LIST lib_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${lib_TESTS} PROPERTIES TIMEOUT 30 LABELS "unittests") +set_tests_properties( + ${lib_TESTS} PROPERTIES TIMEOUT 30 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") # -------------------------------------------------------------------------------------- # # @@ -76,5 +78,7 @@ set(rocprofiler-sdk-lib-tests-env "LD_LIBRARY_PATH=${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}:$ENV{LD_LIBRARY_PATH}") set_tests_properties( - ${shared_lib_TESTS} PROPERTIES TIMEOUT 120 LABELS "unittests" ENVIRONMENT - "${rocprofiler-sdk-lib-tests-env}") + ${shared_lib_TESTS} + PROPERTIES TIMEOUT 120 LABELS "unittests" ENVIRONMENT + "${rocprofiler-sdk-lib-tests-env}" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/rocprofiler-sdk/thread_trace/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/thread_trace/tests/CMakeLists.txt index 70d2df5894..62acae39fb 100644 --- a/source/lib/rocprofiler-sdk/thread_trace/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/thread_trace/tests/CMakeLists.txt @@ -25,5 +25,7 @@ gtest_add_tests( TEST_LIST thread-trace-packet-test_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${thread-trace-packet-test_TESTS} PROPERTIES TIMEOUT 10 LABELS - "unittests") +set_tests_properties( + ${thread-trace-packet-test_TESTS} + PROPERTIES TIMEOUT 10 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/tests/buffering/CMakeLists.txt b/source/lib/tests/buffering/CMakeLists.txt index d6c5576e95..0ffc46fc1a 100644 --- a/source/lib/tests/buffering/CMakeLists.txt +++ b/source/lib/tests/buffering/CMakeLists.txt @@ -21,4 +21,7 @@ gtest_add_tests( TEST_LIST buffering-tests_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${buffering-tests_TESTS} PROPERTIES TIMEOUT 360 LABELS "unittests") +set_tests_properties( + ${buffering-tests_TESTS} + PROPERTIES TIMEOUT 360 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/lib/tests/common/CMakeLists.txt b/source/lib/tests/common/CMakeLists.txt index 1b26131e66..a675d2d5c4 100644 --- a/source/lib/tests/common/CMakeLists.txt +++ b/source/lib/tests/common/CMakeLists.txt @@ -21,4 +21,7 @@ gtest_add_tests( TEST_LIST common-tests_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${common-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") +set_tests_properties( + ${common-tests_TESTS} + PROPERTIES TIMEOUT 45 LABELS "unittests" FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}") diff --git a/source/scripts/setup-sanitizer-env.sh b/source/scripts/setup-sanitizer-env.sh index 9e576735aa..479a33bf70 100755 --- a/source/scripts/setup-sanitizer-env.sh +++ b/source/scripts/setup-sanitizer-env.sh @@ -11,16 +11,23 @@ SUPPR_DIR=$(cd $(dirname ${BASH_SOURCE[0]}) &> /dev/null && pwd) -for i in $(seq 20 -1 8) -do +find-symbolizer() +{ set +e - SYMBOLIZER=$(which llvm-symbolizer-${i}) + SYMBOLIZER=$(which ${1}) set -e if [ -n "${SYMBOLIZER}" ]; then : ${EXTERNAL_SYMBOLIZER_PATH:="${SYMBOLIZER}"} fi +} + +for i in $(seq 20 -1 8) +do + find-symbolizer llvm-symbolizer-${i} done +find-symbolizer llvm-symbolizer + if [ -n "${EXTERNAL_SYMBOLIZER_PATH}" ]; then EXTERNAL_SYMBOLIZER=" external_symbolizer_path=${EXTERNAL_SYMBOLIZER_PATH}" fi @@ -28,11 +35,14 @@ fi : ${ASAN_OPTIONS="detect_leaks=0 use_sigaltstack=0 suppressions=${SUPPR_DIR}/address-sanitizer-suppr.txt"} : ${LSAN_OPTIONS="suppressions=${SUPPR_DIR}/leak-sanitizer-suppr.txt"} : ${TSAN_OPTIONS="history_size=5 detect_deadlocks=0 suppressions=${SUPPR_DIR}/thread-sanitizer-suppr.txt${EXTERNAL_SYMBOLIZER}"} +: ${UBSAN_OPTIONS="print_stacktrace=1 suppressions=${SUPPR_DIR}/undef-behavior-sanitizer-suppr.txt${EXTERNAL_SYMBOLIZER}"} export ASAN_OPTIONS export LSAN_OPTIONS export TSAN_OPTIONS +export UBSAN_OPTIONS echo "ASAN_OPTIONS=\"${ASAN_OPTIONS}\"" echo "LSAN_OPTIONS=\"${LSAN_OPTIONS}\"" echo "TSAN_OPTIONS=\"${TSAN_OPTIONS}\"" +echo "UBSAN_OPTIONS=\"${UBSAN_OPTIONS}\"" diff --git a/tests/common/CMakeLists.txt b/tests/common/CMakeLists.txt index ee8c28f1ed..3a4942cf0b 100644 --- a/tests/common/CMakeLists.txt +++ b/tests/common/CMakeLists.txt @@ -9,8 +9,8 @@ set(FETCHCONTENT_BASE_DIR ${PROJECT_BINARY_DIR}/external) # default FAIL_REGULAR_EXPRESSION for tests set(ROCPROFILER_DEFAULT_FAIL_REGEX - "threw an exception|Permission denied|Could not create logging file" - CACHE STRING "Default FAIL_REGULAR_EXPRESSION for tests") + "threw an exception|Permission denied|Could not create logging file|failed with error code" + CACHE INTERNAL "Default FAIL_REGULAR_EXPRESSION for tests") set(DEFAULT_GPU_TARGETS "gfx900"