From 257274e7543b5ac0ed517314c1549c7d3353e92d Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Mon, 11 Feb 2013 11:34:26 +0000 Subject: [PATCH] [msan] Lit tests for MemorySanitizer. Build system setup for MSan lit tests (build with freshly-built clang, run, check output) - a nearly exact copy from ASan. First 2 lit tests for MSan. llvm-svn: 174876 --- compiler-rt/lib/msan/CMakeLists.txt | 2 + compiler-rt/lib/msan/lit_tests/CMakeLists.txt | 32 +++++++++ .../lib/msan/{tests => lit_tests/Unit}/lit.cfg | 10 ++- .../msan/{tests => lit_tests/Unit}/lit.site.cfg.in | 5 +- compiler-rt/lib/msan/lit_tests/heap-origin.cc | 33 +++++++++ compiler-rt/lib/msan/lit_tests/lit.cfg | 83 ++++++++++++++++++++++ compiler-rt/lib/msan/lit_tests/lit.site.cfg.in | 17 +++++ compiler-rt/lib/msan/lit_tests/stack-origin.cc | 32 +++++++++ compiler-rt/lib/msan/msan_report.cc | 4 +- compiler-rt/lib/msan/tests/CMakeLists.txt | 12 ---- 10 files changed, 208 insertions(+), 22 deletions(-) create mode 100644 compiler-rt/lib/msan/lit_tests/CMakeLists.txt rename compiler-rt/lib/msan/{tests => lit_tests/Unit}/lit.cfg (72%) rename compiler-rt/lib/msan/{tests => lit_tests/Unit}/lit.site.cfg.in (70%) create mode 100644 compiler-rt/lib/msan/lit_tests/heap-origin.cc create mode 100644 compiler-rt/lib/msan/lit_tests/lit.cfg create mode 100644 compiler-rt/lib/msan/lit_tests/lit.site.cfg.in create mode 100644 compiler-rt/lib/msan/lit_tests/stack-origin.cc diff --git a/compiler-rt/lib/msan/CMakeLists.txt b/compiler-rt/lib/msan/CMakeLists.txt index 42c48ca..5677e57 100644 --- a/compiler-rt/lib/msan/CMakeLists.txt +++ b/compiler-rt/lib/msan/CMakeLists.txt @@ -31,3 +31,5 @@ endif() if(LLVM_INCLUDE_TESTS) add_subdirectory(tests) endif() + +add_subdirectory(lit_tests) diff --git a/compiler-rt/lib/msan/lit_tests/CMakeLists.txt b/compiler-rt/lib/msan/lit_tests/CMakeLists.txt new file mode 100644 index 0000000..62b2101 --- /dev/null +++ b/compiler-rt/lib/msan/lit_tests/CMakeLists.txt @@ -0,0 +1,32 @@ +set(MSAN_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..) +set(MSAN_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/..) + +configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in + ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg + ) + +configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.in + ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg + ) + +if(COMPILER_RT_CAN_EXECUTE_TESTS) + # Run MSan tests only if we're sure we may produce working binaries. + set(MSAN_TEST_DEPS + clang clang-headers FileCheck count not llvm-nm llvm-symbolizer + ${MSAN_RUNTIME_LIBRARIES} + ) + set(MSAN_TEST_PARAMS + msan_site_config=${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg + ) + if(LLVM_INCLUDE_TESTS) + list(APPEND MSAN_TEST_DEPS MsanUnitTests) + endif() + add_lit_testsuite(check-msan "Running the MemorySanitizer tests" + ${CMAKE_CURRENT_BINARY_DIR} + PARAMS ${MSAN_TEST_PARAMS} + DEPENDS ${MSAN_TEST_DEPS} + ) + set_target_properties(check-msan PROPERTIES FOLDER "MSan tests") +endif() diff --git a/compiler-rt/lib/msan/tests/lit.cfg b/compiler-rt/lib/msan/lit_tests/Unit/lit.cfg similarity index 72% rename from compiler-rt/lib/msan/tests/lit.cfg rename to compiler-rt/lib/msan/lit_tests/Unit/lit.cfg index 38aa138..afb30e0 100644 --- a/compiler-rt/lib/msan/tests/lit.cfg +++ b/compiler-rt/lib/msan/lit_tests/Unit/lit.cfg @@ -18,12 +18,10 @@ compiler_rt_lit_unit_cfg = os.path.join(llvm_src_root, "projects", lit.load_config(config, compiler_rt_lit_unit_cfg) # Setup config name. -config.name = 'MemorySanitizer' +config.name = 'MemorySanitizer-Unit' # Setup test source and exec root. For unit tests, we define -# it as build directory with sanitizer_common unit tests. -llvm_obj_root = get_required_attr(config, "llvm_obj_root") -config.test_exec_root = os.path.join(llvm_obj_root, "projects", - "compiler-rt", "lib", - "msan", "tests") +# it as build directory with MSan unit tests. +msan_binary_dir = get_required_attr(config, "msan_binary_dir") +config.test_exec_root = os.path.join(msan_binary_dir, "tests") config.test_source_root = config.test_exec_root diff --git a/compiler-rt/lib/msan/tests/lit.site.cfg.in b/compiler-rt/lib/msan/lit_tests/Unit/lit.site.cfg.in similarity index 70% rename from compiler-rt/lib/msan/tests/lit.site.cfg.in rename to compiler-rt/lib/msan/lit_tests/Unit/lit.site.cfg.in index ad0ff3c..4ae84c4 100644 --- a/compiler-rt/lib/msan/tests/lit.site.cfg.in +++ b/compiler-rt/lib/msan/lit_tests/Unit/lit.site.cfg.in @@ -1,9 +1,10 @@ ## Autogenerated by LLVM/Clang configuration. # Do not edit! -config.llvm_obj_root = "@LLVM_BINARY_DIR@" +config.target_triple = "@TARGET_TRIPLE@" config.llvm_src_root = "@LLVM_SOURCE_DIR@" config.llvm_build_mode = "@LLVM_BUILD_MODE@" +config.msan_binary_dir = "@MSAN_BINARY_DIR@" try: config.llvm_build_mode = config.llvm_build_mode % lit.params @@ -12,4 +13,4 @@ except KeyError,e: lit.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key, key)) # Let the main config do the real work. -lit.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg") +lit.load_config(config, "@MSAN_SOURCE_DIR@/lit_tests/Unit/lit.cfg") diff --git a/compiler-rt/lib/msan/lit_tests/heap-origin.cc b/compiler-rt/lib/msan/lit_tests/heap-origin.cc new file mode 100644 index 0000000..54e2c31 --- /dev/null +++ b/compiler-rt/lib/msan/lit_tests/heap-origin.cc @@ -0,0 +1,33 @@ +// RUN: %clangxx_msan -m64 -O0 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out +// RUN: %clangxx_msan -m64 -O1 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out +// RUN: %clangxx_msan -m64 -O2 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out +// RUN: %clangxx_msan -m64 -O3 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out + +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O0 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O1 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O2 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O3 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out + +#include +int main(int argc, char **argv) { + char *volatile x = (char*)malloc(5 * sizeof(char)); + if (*x) + exit(0); + // CHECK: WARNING: Use of uninitialized value + // CHECK: {{#0 0x.* in main .*heap-origin.cc:}}[[@LINE-3]] + + // CHECK-ORIGINS: Uninitialized value was created by a heap allocation + // CHECK-ORIGINS: {{#0 0x.* in .*malloc}} + // CHECK-ORIGINS: {{#1 0x.* in main .*heap-origin.cc:}}[[@LINE-8]] + + // CHECK: SUMMARY: MemorySanitizer: use-of-uninitialized-value {{.*heap-origin.cc:.* main}} + return 0; +} diff --git a/compiler-rt/lib/msan/lit_tests/lit.cfg b/compiler-rt/lib/msan/lit_tests/lit.cfg new file mode 100644 index 0000000..9429050 --- /dev/null +++ b/compiler-rt/lib/msan/lit_tests/lit.cfg @@ -0,0 +1,83 @@ +# -*- Python -*- + +import os + +# Setup config name. +config.name = 'MemorySanitizer' + +# Setup source root. +config.test_source_root = os.path.dirname(__file__) + +def DisplayNoConfigMessage(): + lit.fatal("No site specific configuration available! " + + "Try running your test from the build tree or running " + + "make check-msan") + +# Figure out LLVM source root. +llvm_src_root = getattr(config, 'llvm_src_root', None) +if llvm_src_root is None: + # We probably haven't loaded the site-specific configuration: the user + # is likely trying to run a test file directly, and the site configuration + # wasn't created by the build system. + msan_site_cfg = lit.params.get('msan_site_config', None) + if (msan_site_cfg) and (os.path.exists(msan_site_cfg)): + lit.load_config(config, msan_site_cfg) + raise SystemExit + + # Try to guess the location of site-specific configuration using llvm-config + # util that can point where the build tree is. + llvm_config = lit.util.which("llvm-config", config.environment["PATH"]) + if not llvm_config: + DisplayNoConfigMessage() + + # Validate that llvm-config points to the same source tree. + llvm_src_root = lit.util.capture(["llvm-config", "--src-root"]).strip() + msan_test_src_root = os.path.join(llvm_src_root, "projects", "compiler-rt", + "lib", "msan", "lit_tests") + if (os.path.realpath(msan_test_src_root) != + os.path.realpath(config.test_source_root)): + DisplayNoConfigMessage() + + # Find out the presumed location of generated site config. + llvm_obj_root = lit.util.capture(["llvm-config", "--obj-root"]).strip() + msan_site_cfg = os.path.join(llvm_obj_root, "projects", "compiler-rt", + "lib", "msan", "lit_tests", "lit.site.cfg") + if (not msan_site_cfg) or (not os.path.exists(msan_site_cfg)): + DisplayNoConfigMessage() + + lit.load_config(config, msan_site_cfg) + raise SystemExit + +# Setup attributes common for all compiler-rt projects. +compiler_rt_lit_cfg = os.path.join(llvm_src_root, "projects", "compiler-rt", + "lib", "lit.common.cfg") +if (not compiler_rt_lit_cfg) or (not os.path.exists(compiler_rt_lit_cfg)): + lit.fatal("Can't find common compiler-rt lit config at: %r" + % compiler_rt_lit_cfg) +lit.load_config(config, compiler_rt_lit_cfg) + +# Setup default compiler flags used with -fsanitize=memory option. +clang_msan_cxxflags = ["-ccc-cxx ", + "-fsanitize=memory", + "-mno-omit-leaf-frame-pointer", + "-fno-omit-frame-pointer", + "-fno-optimize-sibling-calls", + "-g", + "-fPIE", + "-pie"] +config.substitutions.append( ("%clangxx_msan ", + " ".join([config.clang] + clang_msan_cxxflags) + + " ") ) + +# Setup path to external LLVM symbolizer to run MemorySanitizer output tests. +llvm_tools_dir = getattr(config, 'llvm_tools_dir', None) +if llvm_tools_dir: + llvm_symbolizer_path = os.path.join(llvm_tools_dir, "llvm-symbolizer") + config.environment['MSAN_SYMBOLIZER_PATH'] = llvm_symbolizer_path + +# Default test suffixes. +config.suffixes = ['.c', '.cc', '.cpp'] + +# MemorySanitizer tests are currently supported on Linux only. +if config.host_os not in ['Linux']: + config.unsupported = True diff --git a/compiler-rt/lib/msan/lit_tests/lit.site.cfg.in b/compiler-rt/lib/msan/lit_tests/lit.site.cfg.in new file mode 100644 index 0000000..cc7c7a0 --- /dev/null +++ b/compiler-rt/lib/msan/lit_tests/lit.site.cfg.in @@ -0,0 +1,17 @@ +config.target_triple = "@TARGET_TRIPLE@" +config.host_os = "@HOST_OS@" +config.llvm_src_root = "@LLVM_SOURCE_DIR@" +config.llvm_obj_root = "@LLVM_BINARY_DIR@" +config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" +config.clang = "@LLVM_BINARY_DIR@/bin/clang" + +# LLVM tools dir can be passed in lit parameters, so try to +# apply substitution. +try: + config.llvm_tools_dir = config.llvm_tools_dir % lit.params +except KeyError,e: + key, = e.args + lit.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key, key)) + +# Let the main config do the real work. +lit.load_config(config, "@MSAN_SOURCE_DIR@/lit_tests/lit.cfg") diff --git a/compiler-rt/lib/msan/lit_tests/stack-origin.cc b/compiler-rt/lib/msan/lit_tests/stack-origin.cc new file mode 100644 index 0000000..90f5273 --- /dev/null +++ b/compiler-rt/lib/msan/lit_tests/stack-origin.cc @@ -0,0 +1,32 @@ +// RUN: %clangxx_msan -m64 -O0 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out +// RUN: %clangxx_msan -m64 -O1 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out +// RUN: %clangxx_msan -m64 -O2 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out +// RUN: %clangxx_msan -m64 -O3 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out + +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O0 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O1 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O2 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out +// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O3 %s -o %t && not %t >%t.out 2>&1 +// RUN: FileCheck %s < %t.out && FileCheck %s --check-prefix=CHECK-ORIGINS < %t.out + +#include +int main(int argc, char **argv) { + int x; + int *volatile p = &x; + if (*p) + exit(0); + // CHECK: WARNING: Use of uninitialized value + // CHECK: {{#0 0x.* in main .*stack-origin.cc:}}[[@LINE-3]] + + // CHECK-ORIGINS: Uninitialized value was created by an allocation of 'x' in the stack frame of function 'main' + + // CHECK: SUMMARY: MemorySanitizer: use-of-uninitialized-value {{.*stack-origin.cc:.* main}} + return 0; +} diff --git a/compiler-rt/lib/msan/msan_report.cc b/compiler-rt/lib/msan/msan_report.cc index 804c8ae..26f34d7 100644 --- a/compiler-rt/lib/msan/msan_report.cc +++ b/compiler-rt/lib/msan/msan_report.cc @@ -54,7 +54,7 @@ static void DescribeOrigin(u32 origin) { CHECK(sep); *sep = '\0'; Printf("%s", d.Origin()); - Printf(" %sUninitialised value was created by an allocation of '%s%s%s'" + Printf(" %sUninitialized value was created by an allocation of '%s%s%s'" " in the stack frame of function '%s%s%s'%s\n", d.Origin(), d.Name(), s, d.Origin(), d.Name(), sep + 1, d.Origin(), d.End()); @@ -62,7 +62,7 @@ static void DescribeOrigin(u32 origin) { } else { uptr size = 0; const uptr *trace = StackDepotGet(origin, &size); - Printf(" %sUninitialised value was created by a heap allocation%s\n", + Printf(" %sUninitialized value was created by a heap allocation%s\n", d.Origin(), d.End()); StackTrace::PrintStack(trace, size, true, flags()->strip_path_prefix, 0); } diff --git a/compiler-rt/lib/msan/tests/CMakeLists.txt b/compiler-rt/lib/msan/tests/CMakeLists.txt index f2be647..7067c45 100644 --- a/compiler-rt/lib/msan/tests/CMakeLists.txt +++ b/compiler-rt/lib/msan/tests/CMakeLists.txt @@ -151,16 +151,4 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS AND EXISTS ${MSAN_LIBCXX_PATH}/) if(CAN_TARGET_x86_64) add_msan_tests_for_arch(x86_64) endif() - - # Run unittests as a part of lit testsuite. - configure_lit_site_cfg( - ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in - ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg - ) - - add_lit_testsuite(check-msan "Running MemorySanitizer unittests" - ${CMAKE_CURRENT_BINARY_DIR} - DEPENDS MsanUnitTests - ) - set_target_properties(check-msan PROPERTIES FOLDER "MemorySanitizer unittests") endif() -- 2.7.4