diff options
Diffstat (limited to 'lib/tsan/tests')
-rw-r--r-- | lib/tsan/tests/CMakeLists.txt | 56 | ||||
-rw-r--r-- | lib/tsan/tests/rtl/CMakeLists.txt | 14 | ||||
-rw-r--r-- | lib/tsan/tests/unit/CMakeLists.txt | 10 | ||||
-rw-r--r-- | lib/tsan/tests/unit/tsan_mman_test.cc | 4 | ||||
-rw-r--r-- | lib/tsan/tests/unit/tsan_stack_test.cc | 9 | ||||
-rw-r--r-- | lib/tsan/tests/unit/tsan_suppressions_test.cc | 128 | ||||
-rw-r--r-- | lib/tsan/tests/unit/tsan_unit_test_main.cc | 19 |
7 files changed, 84 insertions, 156 deletions
diff --git a/lib/tsan/tests/CMakeLists.txt b/lib/tsan/tests/CMakeLists.txt index 7cc079f3d27a..f73a89242859 100644 --- a/lib/tsan/tests/CMakeLists.txt +++ b/lib/tsan/tests/CMakeLists.txt @@ -3,22 +3,44 @@ include_directories(../rtl) add_custom_target(TsanUnitTests) set_target_properties(TsanUnitTests PROPERTIES FOLDER "TSan unittests") -function(add_tsan_unittest testname) - # Build unit tests only on 64-bit Linux. - if(UNIX AND NOT APPLE - AND CAN_TARGET_x86_64 - AND CMAKE_SIZEOF_VOID_P EQUAL 8 - AND NOT LLVM_BUILD_32_BITS) - add_unittest(TsanUnitTests ${testname} ${ARGN}) - # Link with TSan runtime. - target_link_libraries(${testname} clang_rt.tsan-x86_64) - # Compile tests with the same flags as TSan runtime. - set_target_compile_flags(${testname} ${TSAN_CFLAGS}) - # Link tests with -pie. - set_property(TARGET ${testname} APPEND_STRING - PROPERTY LINK_FLAGS " -pie") + +set(TSAN_UNITTEST_CFLAGS + ${TSAN_CFLAGS} + ${COMPILER_RT_GTEST_INCLUDE_CFLAGS} + -I${COMPILER_RT_SOURCE_DIR}/lib + -I${COMPILER_RT_SOURCE_DIR}/lib/tsan/rtl + -DGTEST_HAS_RTTI=0) + +# tsan_compile(obj_list, source, arch, {headers}) +macro(tsan_compile obj_list source arch) + get_filename_component(basename ${source} NAME) + set(output_obj "${basename}.${arch}.o") + get_target_flags_for_arch(${arch} TARGET_CFLAGS) + clang_compile(${output_obj} ${source} + CFLAGS ${TSAN_UNITTEST_CFLAGS} ${TARGET_CFLAGS} + DEPS gtest ${TSAN_RUNTIME_LIBRARIES} ${ARGN}) + list(APPEND ${obj_list} ${output_obj}) +endmacro() + +macro(add_tsan_unittest testname) + # Build unit tests only for 64-bit Linux. + if(UNIX AND NOT APPLE AND CAN_TARGET_x86_64) + parse_arguments(TEST "SOURCES;HEADERS" "" ${ARGN}) + set(TEST_OBJECTS) + foreach(SOURCE ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}) + tsan_compile(TEST_OBJECTS ${SOURCE} x86_64 ${TEST_HEADERS}) + endforeach() + get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS) + add_compiler_rt_test(TsanUnitTests ${testname} + OBJECTS ${TEST_OBJECTS} + DEPS ${TSAN_RUNTIME_LIBRARIES} ${TEST_OBJECTS} + LINK_FLAGS ${TARGET_LINK_FLAGS} + -fsanitize=thread + -lstdc++ -lm) endif() -endfunction() +endmacro() -add_subdirectory(rtl) -add_subdirectory(unit) +if(COMPILER_RT_CAN_EXECUTE_TESTS) + add_subdirectory(rtl) + add_subdirectory(unit) +endif() diff --git a/lib/tsan/tests/rtl/CMakeLists.txt b/lib/tsan/tests/rtl/CMakeLists.txt index b585660e8b4a..989566d9e041 100644 --- a/lib/tsan/tests/rtl/CMakeLists.txt +++ b/lib/tsan/tests/rtl/CMakeLists.txt @@ -1,15 +1,19 @@ -set(TSAN_RTL_TESTS +set(TSAN_RTL_TEST_SOURCES tsan_bench.cc tsan_mop.cc tsan_mutex.cc tsan_posix.cc tsan_string.cc tsan_test.cc - tsan_thread.cc - ) + tsan_thread.cc) if(UNIX AND NOT APPLE) - list(APPEND TSAN_RTL_TESTS tsan_test_util_linux.cc) + list(APPEND TSAN_RTL_TEST_SOURCES tsan_test_util_linux.cc) endif() -add_tsan_unittest(TsanRtlTest ${TSAN_RTL_TESTS}) +set(TSAN_RTL_TEST_HEADERS + tsan_test_util.h) + +add_tsan_unittest(TsanRtlTest + SOURCES ${TSAN_RTL_TEST_SOURCES} + HEADERS ${TSAN_RTL_TEST_HEADERS}) diff --git a/lib/tsan/tests/unit/CMakeLists.txt b/lib/tsan/tests/unit/CMakeLists.txt index b25a56d8d55c..6898f641d6a0 100644 --- a/lib/tsan/tests/unit/CMakeLists.txt +++ b/lib/tsan/tests/unit/CMakeLists.txt @@ -1,13 +1,13 @@ -set(TSAN_UNIT_TESTS +set(TSAN_UNIT_TEST_SOURCES tsan_clock_test.cc tsan_flags_test.cc tsan_mman_test.cc tsan_mutex_test.cc tsan_shadow_test.cc tsan_stack_test.cc - tsan_suppressions_test.cc tsan_sync_test.cc - tsan_vector_test.cc - ) + tsan_unit_test_main.cc + tsan_vector_test.cc) -add_tsan_unittest(TsanUnitTest ${TSAN_UNIT_TESTS}) +add_tsan_unittest(TsanUnitTest + SOURCES ${TSAN_UNIT_TEST_SOURCES}) diff --git a/lib/tsan/tests/unit/tsan_mman_test.cc b/lib/tsan/tests/unit/tsan_mman_test.cc index 0961d2b75d11..e1ad7ac51ad6 100644 --- a/lib/tsan/tests/unit/tsan_mman_test.cc +++ b/lib/tsan/tests/unit/tsan_mman_test.cc @@ -164,7 +164,9 @@ TEST(Mman, CallocOverflow) { size_t kArraySize = 4096; volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max(); volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10; - volatile void *p = calloc(kArraySize, kArraySize2); // Should return 0. + volatile void *p = NULL; + EXPECT_DEATH(p = calloc(kArraySize, kArraySize2), + "allocator is terminating the process instead of returning 0"); EXPECT_EQ(0L, p); } diff --git a/lib/tsan/tests/unit/tsan_stack_test.cc b/lib/tsan/tests/unit/tsan_stack_test.cc index d5392959c48c..9aa2967628cf 100644 --- a/lib/tsan/tests/unit/tsan_stack_test.cc +++ b/lib/tsan/tests/unit/tsan_stack_test.cc @@ -19,6 +19,10 @@ namespace __tsan { static void TestStackTrace(StackTrace *trace) { ThreadState thr(0, 0, 0, 0, 0, 0, 0, 0); + uptr stack[128]; + thr.shadow_stack = &stack[0]; + thr.shadow_stack_pos = &stack[0]; + thr.shadow_stack_end = &stack[128]; trace->ObtainCurrent(&thr, 0); EXPECT_EQ(trace->Size(), (uptr)0); @@ -60,7 +64,12 @@ TEST(StackTrace, StaticTrim) { ScopedInRtl in_rtl; uptr buf[2]; StackTrace trace(buf, 2); + ThreadState thr(0, 0, 0, 0, 0, 0, 0, 0); + uptr stack[128]; + thr.shadow_stack = &stack[0]; + thr.shadow_stack_pos = &stack[0]; + thr.shadow_stack_end = &stack[128]; *thr.shadow_stack_pos++ = 100; *thr.shadow_stack_pos++ = 101; diff --git a/lib/tsan/tests/unit/tsan_suppressions_test.cc b/lib/tsan/tests/unit/tsan_suppressions_test.cc deleted file mode 100644 index decfa3214d23..000000000000 --- a/lib/tsan/tests/unit/tsan_suppressions_test.cc +++ /dev/null @@ -1,128 +0,0 @@ -//===-- tsan_suppressions_test.cc -----------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file is a part of ThreadSanitizer (TSan), a race detector. -// -//===----------------------------------------------------------------------===// -#include "tsan_suppressions.h" -#include "tsan_rtl.h" -#include "gtest/gtest.h" - -#include <string.h> - -namespace __tsan { - -TEST(Suppressions, Parse) { - ScopedInRtl in_rtl; - Suppression *supp0 = SuppressionParse(0, - "race:foo\n" - " race:bar\n" // NOLINT - "race:baz \n" // NOLINT - "# a comment\n" - "race:quz\n" - ); // NOLINT - Suppression *supp = supp0; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "quz")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "baz")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "bar")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "foo")); - supp = supp->next; - EXPECT_EQ((Suppression*)0, supp); -} - -TEST(Suppressions, Parse2) { - ScopedInRtl in_rtl; - Suppression *supp0 = SuppressionParse(0, - " # first line comment\n" // NOLINT - " race:bar \n" // NOLINT - "race:baz* *baz\n" - "# a comment\n" - "# last line comment\n" - ); // NOLINT - Suppression *supp = supp0; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "baz* *baz")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "bar")); - supp = supp->next; - EXPECT_EQ((Suppression*)0, supp); -} - -TEST(Suppressions, Parse3) { - ScopedInRtl in_rtl; - Suppression *supp0 = SuppressionParse(0, - "# last suppression w/o line-feed\n" - "race:foo\n" - "race:bar" - ); // NOLINT - Suppression *supp = supp0; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "bar")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "foo")); - supp = supp->next; - EXPECT_EQ((Suppression*)0, supp); -} - -TEST(Suppressions, ParseType) { - ScopedInRtl in_rtl; - Suppression *supp0 = SuppressionParse(0, - "race:foo\n" - "thread:bar\n" - "mutex:baz\n" - "signal:quz\n" - ); // NOLINT - Suppression *supp = supp0; - EXPECT_EQ(supp->type, SuppressionSignal); - EXPECT_EQ(0, strcmp(supp->templ, "quz")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionMutex); - EXPECT_EQ(0, strcmp(supp->templ, "baz")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionThread); - EXPECT_EQ(0, strcmp(supp->templ, "bar")); - supp = supp->next; - EXPECT_EQ(supp->type, SuppressionRace); - EXPECT_EQ(0, strcmp(supp->templ, "foo")); - supp = supp->next; - EXPECT_EQ((Suppression*)0, supp); -} - -static bool MyMatch(const char *templ, const char *func) { - char tmp[1024]; - strcpy(tmp, templ); // NOLINT - return SuppressionMatch(tmp, func); -} - -TEST(Suppressions, Match) { - EXPECT_TRUE(MyMatch("foobar", "foobar")); - EXPECT_TRUE(MyMatch("foobar", "prefix_foobar_postfix")); - EXPECT_TRUE(MyMatch("*foobar*", "prefix_foobar_postfix")); - EXPECT_TRUE(MyMatch("foo*bar", "foo_middle_bar")); - EXPECT_TRUE(MyMatch("foo*bar", "foobar")); - EXPECT_TRUE(MyMatch("foo*bar*baz", "foo_middle_bar_another_baz")); - EXPECT_TRUE(MyMatch("foo*bar*baz", "foo_middle_barbaz")); - - EXPECT_FALSE(MyMatch("foo", "baz")); - EXPECT_FALSE(MyMatch("foobarbaz", "foobar")); - EXPECT_FALSE(MyMatch("foobarbaz", "barbaz")); - EXPECT_FALSE(MyMatch("foo*bar", "foobaz")); - EXPECT_FALSE(MyMatch("foo*bar", "foo_baz")); -} - -} // namespace __tsan diff --git a/lib/tsan/tests/unit/tsan_unit_test_main.cc b/lib/tsan/tests/unit/tsan_unit_test_main.cc new file mode 100644 index 000000000000..84d94dd03748 --- /dev/null +++ b/lib/tsan/tests/unit/tsan_unit_test_main.cc @@ -0,0 +1,19 @@ +//===-- tsan_unit_test_main.cc --------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file is a part of ThreadSanitizer (TSan), a race detector. +// +//===----------------------------------------------------------------------===// +#include "gtest/gtest.h" + +int main(int argc, char **argv) { + testing::GTEST_FLAG(death_test_style) = "threadsafe"; + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} |