diff options
author | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2017-12-09 01:31:51 +0000 |
---|---|---|
committer | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2017-12-09 01:31:51 +0000 |
commit | 32e20e4417a0dd8870d8ec5c9be21f5c2d1ebc63 (patch) | |
tree | 1a5c7d5b32c2072600e208cb10157e60e22e59c9 /test/hwasan | |
parent | ba5a005732212e5f111094e9aec667da37417624 (diff) |
Hardware-assisted AddressSanitizer (compiler-rt)
Summary:
Runtime library for HWASan, initial commit.
Does not randomize tags yet, does not handle stack or globals.
Reviewers: kcc, pcc, alekseyshl
Subscribers: srhines, kubamracek, dberris, mgorny, llvm-commits, krytarowski
Differential Revision: https://reviews.llvm.org/D40935
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@320231 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/hwasan')
-rw-r--r-- | test/hwasan/CMakeLists.txt | 29 | ||||
-rw-r--r-- | test/hwasan/TestCases/halt-on-error.cc | 29 | ||||
-rw-r--r-- | test/hwasan/TestCases/use-after-free.cc | 29 | ||||
-rw-r--r-- | test/hwasan/lit.cfg | 32 | ||||
-rw-r--r-- | test/hwasan/lit.site.cfg.in | 12 |
5 files changed, 131 insertions, 0 deletions
diff --git a/test/hwasan/CMakeLists.txt b/test/hwasan/CMakeLists.txt new file mode 100644 index 000000000..972c73250 --- /dev/null +++ b/test/hwasan/CMakeLists.txt @@ -0,0 +1,29 @@ +set(HWASAN_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + +set(HWASAN_TESTSUITES) + +set(HWASAN_TEST_ARCH ${HWASAN_SUPPORTED_ARCH}) + +foreach(arch ${HWASAN_TEST_ARCH}) + set(HWASAN_TEST_TARGET_ARCH ${arch}) + string(TOLOWER "-${arch}" HWASAN_TEST_CONFIG_SUFFIX) + get_test_cc_for_arch(${arch} HWASAN_TEST_TARGET_CC HWASAN_TEST_TARGET_CFLAGS) + string(TOUPPER ${arch} ARCH_UPPER_CASE) + set(CONFIG_NAME ${ARCH_UPPER_CASE}) + + configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in + ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg) + list(APPEND HWASAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}) +endforeach() + +set(HWASAN_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS}) +if(NOT COMPILER_RT_STANDALONE_BUILD) + list(APPEND HWASAN_TEST_DEPS hwasan) +endif() + +add_lit_testsuite(check-hwasan "Running the HWAddressSanitizer tests" + ${HWASAN_TESTSUITES} + DEPENDS ${HWASAN_TEST_DEPS} + ) +set_target_properties(check-hwasan PROPERTIES FOLDER "Compiler-RT Misc") diff --git a/test/hwasan/TestCases/halt-on-error.cc b/test/hwasan/TestCases/halt-on-error.cc new file mode 100644 index 000000000..135c2b828 --- /dev/null +++ b/test/hwasan/TestCases/halt-on-error.cc @@ -0,0 +1,29 @@ +// RUN: %clangxx_hwasan -O0 %s -o %t && not %env_hwasan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK +// REQUIRES: stable-runtime + +#include <stdlib.h> +#include <sanitizer/hwasan_interface.h> + +int main() { + __hwasan_enable_allocator_tagging(); + int* volatile x = (int*)malloc(16); + free(x); + __hwasan_disable_allocator_tagging(); + return x[2] + ((char *)x)[6] + ((char *)x)[9]; + // CHECK: READ of size 4 at + // CHECK: #0 {{.*}} in __hwasan_load4 {{.*}}hwasan.cc + // CHECK: #1 {{.*}} in main {{.*}}halt-on-error.cc:12 + // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in __hwasan_load4 + + // CHECK: READ of size 1 at + // CHECK: #0 {{.*}} in __hwasan_load1 {{.*}}hwasan.cc + // CHECK: #1 {{.*}} in main {{.*}}halt-on-error.cc:12 + // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in __hwasan_load1 + + // CHECK: READ of size 1 at + // CHECK: #0 {{.*}} in __hwasan_load1 {{.*}}hwasan.cc + // CHECK: #1 {{.*}} in main {{.*}}halt-on-error.cc:12 + // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in __hwasan_load1 + + // CHECK-NOT: tag-mismatch +} diff --git a/test/hwasan/TestCases/use-after-free.cc b/test/hwasan/TestCases/use-after-free.cc new file mode 100644 index 000000000..a99cd3c0e --- /dev/null +++ b/test/hwasan/TestCases/use-after-free.cc @@ -0,0 +1,29 @@ +// RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK +// RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK +// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK +// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK +// REQUIRES: stable-runtime + +#include <stdlib.h> +#include <sanitizer/hwasan_interface.h> + +int main() { + __hwasan_enable_allocator_tagging(); + char *x = (char*)malloc(10); + free(x); + __hwasan_disable_allocator_tagging(); + return x[5]; + // CHECK: READ of size 1 at + // CHECK: #0 {{.*}} in __hwasan_load1 {{.*}}hwasan.cc + // CHECK: #1 {{.*}} in main {{.*}}use-after-free.cc:15 + + // CHECK: freed here: + // CHECK: #0 {{.*}} in free {{.*}}hwasan_interceptors.cc + // CHECK: #1 {{.*}} in main {{.*}}use-after-free.cc:13 + + // CHECK: previously allocated here: + // CHECK: #0 {{.*}} in __interceptor_malloc {{.*}}hwasan_interceptors.cc + // CHECK: #1 {{.*}} in main {{.*}}use-after-free.cc:12 + + // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in __hwasan_load1 +} diff --git a/test/hwasan/lit.cfg b/test/hwasan/lit.cfg new file mode 100644 index 000000000..4f099af5b --- /dev/null +++ b/test/hwasan/lit.cfg @@ -0,0 +1,32 @@ +# -*- Python -*- + +import os + +# Setup config name. +config.name = 'HWAddressSanitizer' + getattr(config, 'name_suffix', 'default') + +# Setup source root. +config.test_source_root = os.path.dirname(__file__) + +# Setup default compiler flags used with -fsanitize=memory option. +clang_hwasan_cflags = ["-fsanitize=hwaddress", config.target_cflags] + config.debug_info_flags +clang_hwasan_cxxflags = config.cxx_mode_flags + clang_hwasan_cflags + +def build_invocation(compile_flags): + return " " + " ".join([config.clang] + compile_flags) + " " + +config.substitutions.append( ("%clang_hwasan ", build_invocation(clang_hwasan_cflags)) ) +config.substitutions.append( ("%clangxx_hwasan ", build_invocation(clang_hwasan_cxxflags)) ) + +default_hwasan_opts_str = ':'.join(['disable_allocator_tagging=1'] + config.default_sanitizer_opts) +if default_hwasan_opts_str: + config.environment['HWASAN_OPTIONS'] = default_hwasan_opts_str + default_hwasan_opts_str += ':' +config.substitutions.append(('%env_hwasan_opts=', + 'env HWASAN_OPTIONS=' + default_hwasan_opts_str)) + +# Default test suffixes. +config.suffixes = ['.c', '.cc', '.cpp'] + +if config.host_os not in ['Linux', 'Android']: + config.unsupported = True diff --git a/test/hwasan/lit.site.cfg.in b/test/hwasan/lit.site.cfg.in new file mode 100644 index 000000000..7453d1b74 --- /dev/null +++ b/test/hwasan/lit.site.cfg.in @@ -0,0 +1,12 @@ +@LIT_SITE_CFG_IN_HEADER@ + +# Tool-specific config options. +config.name_suffix = "@HWASAN_TEST_CONFIG_SUFFIX@" +config.target_cflags = "@HWASAN_TEST_TARGET_CFLAGS@" +config.target_arch = "@HWASAN_TEST_TARGET_ARCH@" + +# Load common config for all compiler-rt lit tests. +lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured") + +# Load tool-specific config that would do the real work. +lit_config.load_config(config, "@HWASAN_LIT_SOURCE_DIR@/lit.cfg") |