Skip to content

Commit

Permalink
[compiler-rt][test] Heed COMPILER_RT_DEBUG when compiling unittests
Browse files Browse the repository at this point in the history
When trying to debug some `compiler-rt` unittests, I initially had a hard
time because

- even in a `Debug` build one needs to set `COMPILER_RT_DEBUG` to get
  debugging info for some of the code and
- even so the unittests used a hardcoded `-O2` which often makes debugging
  impossible.

This patch addresses this by instead using `-O0` if `COMPILER_RT_DEBUG`.
Two tests in `sanitizer_type_traits_test.cpp` need to be disabled since
they have undefined references to `__sanitizer::integral_constant<bool,
true>::value`.

Tested on `sparcv9-sun-solaris2.11`, `amd64-pc-solaris2.11`, and
`x86_64-pc-linux-gnu`.

Differential Revision: https://reviews.llvm.org/D91620
  • Loading branch information
rorth committed Oct 5, 2022
1 parent 27c634a commit 93b1256
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 36 deletions.
10 changes: 9 additions & 1 deletion compiler-rt/CMakeLists.txt
Expand Up @@ -406,9 +406,11 @@ if (NOT MSVC)

# Build with optimization, unless we're in debug mode.
if(COMPILER_RT_DEBUG)
list(APPEND SANITIZER_COMMON_CFLAGS -O1)
list(APPEND SANITIZER_COMMON_CFLAGS -O0)
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -O0)
else()
list(APPEND SANITIZER_COMMON_CFLAGS -O3)
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -O3)
endif()
endif()

Expand Down Expand Up @@ -443,6 +445,7 @@ if(MSVC)
string(REGEX REPLACE "(^| )/Z[i7I]($| )" " /Z7 "
"${var_to_update}" "${${var_to_update}}")
endforeach()
list(APPEND COMPILER_RT_UNITTEST_CFLAGS -gcodeview)
elseif(APPLE)
# On Apple platforms use full debug info (i.e. not `-gline-tables-only`)
# for all build types so that the runtime can be debugged.
Expand All @@ -452,8 +455,13 @@ elseif(APPLE)
list(APPEND SANITIZER_COMMON_CFLAGS -g)
elseif(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
# -gline-tables-only must be enough for these tests, so use it if possible.
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -gline-tables-only)
list(APPEND COMPILER_RT_UNITTEST_CFLAGS -gline-tables-only)
elseif(COMPILER_RT_HAS_G_FLAG)
list(APPEND SANITIZER_COMMON_CFLAGS -g)
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -g)
list(APPEND COMPILER_RT_UNITTEST_CFLAGS -g)
endif()

if(LLVM_ENABLE_MODULES)
Expand Down
11 changes: 0 additions & 11 deletions compiler-rt/lib/asan/tests/CMakeLists.txt
Expand Up @@ -29,7 +29,6 @@ set(ASAN_UNITTEST_COMMON_CFLAGS
-I${COMPILER_RT_SOURCE_DIR}/lib/asan
-I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/tests
-fno-rtti
-O2
-Wno-format
-Werror=sign-compare)
append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros ASAN_UNITTEST_COMMON_CFLAGS)
Expand All @@ -40,16 +39,6 @@ set(ASAN_UNITTEST_COMMON_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})

# -gline-tables-only must be enough for ASan, so use it if possible.
if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -gline-tables-only)
else()
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -g)
endif()
if(MSVC)
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -gcodeview)
endif()
list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS -g)

# Use -D instead of definitions to please custom compile command.
Expand Down
3 changes: 1 addition & 2 deletions compiler-rt/lib/fuzzer/tests/CMakeLists.txt
Expand Up @@ -4,8 +4,7 @@ set(LIBFUZZER_UNITTEST_CFLAGS
${COMPILER_RT_UNITTEST_CFLAGS}
${COMPILER_RT_GTEST_CFLAGS}
-I${COMPILER_RT_SOURCE_DIR}/lib/fuzzer
-fno-rtti
-O2)
-fno-rtti)

if (APPLE)
set(FUZZER_SUPPORTED_OS osx)
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/lib/gwp_asan/tests/CMakeLists.txt
Expand Up @@ -6,7 +6,6 @@ set(GWP_ASAN_UNITTEST_CFLAGS
${SANITIZER_TEST_CXX_CFLAGS}
-std=c++17
-I${COMPILER_RT_SOURCE_DIR}/lib/
-O2
-g
-fno-omit-frame-pointer)

Expand Down
8 changes: 0 additions & 8 deletions compiler-rt/lib/interception/tests/CMakeLists.txt
Expand Up @@ -18,22 +18,14 @@ set(INTERCEPTION_TEST_CFLAGS_COMMON
-I${COMPILER_RT_SOURCE_DIR}/lib
-I${COMPILER_RT_SOURCE_DIR}/lib/interception
-fno-rtti
-O2
-Werror=sign-compare)

set(INTERCEPTION_TEST_LINK_FLAGS_COMMON
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})

# -gline-tables-only must be enough for these tests, so use it if possible.
if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gline-tables-only)
else()
list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -g)
endif()
if(MSVC)
list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gcodeview)
list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON
-Wl,-largeaddressaware
-Wl,-nodefaultlib:libcmt,-defaultlib:msvcrt,-defaultlib:oldnames
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/lib/msan/tests/CMakeLists.txt
Expand Up @@ -33,7 +33,6 @@ set(MSAN_UNITTEST_COMMON_CFLAGS
-I${COMPILER_RT_SOURCE_DIR}/lib
-I${COMPILER_RT_SOURCE_DIR}/lib/msan
-g
-O2
-fno-omit-frame-pointer
-mno-omit-leaf-frame-pointer
-Wno-deprecated-declarations
Expand Down
11 changes: 0 additions & 11 deletions compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
Expand Up @@ -67,7 +67,6 @@ set(SANITIZER_TEST_CFLAGS_COMMON
-I${COMPILER_RT_SOURCE_DIR}/lib
-I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common
-fno-rtti
-O2
-Werror=sign-compare
-Wno-gnu-zero-variadic-macro-arguments
)
Expand All @@ -76,16 +75,6 @@ set(SANITIZER_TEST_LINK_FLAGS_COMMON
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})

# -gline-tables-only must be enough for these tests, so use it if possible.
if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gline-tables-only)
else()
list(APPEND SANITIZER_TEST_CFLAGS_COMMON -g)
endif()
if(MSVC)
list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gcodeview)
endif()
list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -g)

if(NOT MSVC)
Expand Down
Expand Up @@ -69,15 +69,21 @@ struct TestStruct {
};

TEST(SanitizerCommon, IsTriviallyDestructible) {
// Causes undefined references to __sanitizer::integral_constant<bool,
// true>::value.
#if !SANITIZER_DEBUG
ASSERT_TRUE((is_trivially_destructible<int>::value));
ASSERT_TRUE((is_trivially_destructible<TestStruct>::value));
#endif
ASSERT_FALSE((is_trivially_destructible<std::vector<int>>::value));
}

TEST(SanitizerCommon, IsTriviallyCopyable) {
#if !SANITIZER_DEBUG
ASSERT_TRUE((is_trivially_copyable<int>::value));
ASSERT_TRUE((is_trivially_copyable<TestStruct>::value));
#endif
ASSERT_FALSE((is_trivially_copyable<std::vector<int>>::value));
}

} // namespace __sanitizer
} // namespace __sanitizer

0 comments on commit 93b1256

Please sign in to comment.