Skip to content

Commit

Permalink
Autodetect hwasan support
Browse files Browse the repository at this point in the history
Added a C source that checks if the kernel has Aarch64 tagged address
ABI. CMake will then enable or disable hwasan tests depending on the
result of that test in configure time.
  • Loading branch information
Jesus Checa Hidalgo committed Jul 19, 2023
1 parent bf16a99 commit e7f4031
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ option(ENABLE_LIBCXX "assume libc++ is available" ON)
option(ENABLE_STATIC_LIBCXX "assume libc++.a is available" ON)
option(ENABLE_LIBUNWIND "assume libunwind is available" ON)

message(STATUS "Checking kernel support for tagged address ABI")
try_run(
_TEST_RUN_RESULT
_BUILD_RESULT_UNUSED
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/test_tagged_addr_abi_support.c
)
if(DEFINED _TEST_RUN_RESULT AND _TEST_RUN_RESULT EQUAL 0)
set(SUPPORT_HWASAN ON)
else()
message(STATUS "Tagged address ABI disabled, disabling hwasan test(s)")
set(SUPPORT_HWASAN OFF)
endif()
option(ENABLE_HWASAN
"The host supports Aarch64 tagged address ABI"
${SUPPORT_HWASAN}
)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/tests/lit.site.cfg.in
${CMAKE_BINARY_DIR}/tests/lit.site.cfg
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Sensible configuration variables
- ``ENABLE_COMPILER_RT``: ON (the default) if we assume compiler-rt is available
- ``ENABLE_LIBCXX``: ON (the default) if we assume libc++ is available
- ``ENABLE_UNWIND``: ON (the default) if we assume libunwind is available
- ``ENABLE_HWASAN``: Run hwasan tests. Autodetected based on the host system.

Writing new tests
-----------------
Expand Down
14 changes: 14 additions & 0 deletions test_tagged_addr_abi_support.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This simple program checks if the kernel running the testsuite has support
* for tagged address ABI, needed to test hardware assisted address sanitizer.
* https://www.kernel.org/doc/html/next/arm64/tagged-address-abi.html
*
* The program returns 0 if the kernel supports tagged address ABI.
*/

#include <sys/prctl.h>
int main(void)
{
return prctl(PR_GET_TAGGED_ADDR_CTRL, 0,0,0,0);
}

1 change: 1 addition & 0 deletions tests/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ enable_feature("compiler-rt", "@ENABLE_COMPILER_RT@")
enable_feature("libc++", "@ENABLE_LIBCXX@")
enable_feature("static-libc++", "@ENABLE_STATIC_LIBCXX@")
enable_feature("libunwind", "@ENABLE_LIBUNWIND@")
enable_feature("support_hwasan", "@ENABLE_HWASAN@")
2 changes: 1 addition & 1 deletion tests/test_hwasan.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Test hwasan use after free -- aarch64 only.
//
// REQUIRES: clang, compiler-rt, aarch64
// REQUIRES: clang, compiler-rt, support_hwasan
// RUN: %clang -o %t -fsanitize=hwaddress -g %s
// RUN: env HWASAN_OPTIONS="log_path=stdout:exitcode=0" %t 2>&1 > %t.out
// RUN: grep -q "HWAddressSanitizer: tag-mismatch" %t.out
Expand Down

0 comments on commit e7f4031

Please sign in to comment.