Skip to content

Commit 7b1be6e

Browse files
StanFromIrelandfmayerAnnaAr321
committed
Add support for building with HWASAN
Co-authored-by: Florian Mayer <fmayer@google.com> Co-authored-by: Anna <araslanova.anna.a@gmail.com>
1 parent d87ee27 commit 7b1be6e

7 files changed

Lines changed: 132 additions & 1 deletion

File tree

Doc/using/configure.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,16 @@ Debug options
10151015

10161016
.. versionadded:: 3.6
10171017

1018+
.. option:: --with-hwaddress-sanitizer
1019+
1020+
Enable HWAddressSanitizer memory error detector, ``hwasan`` (default is no).
1021+
Note that on x86-64 this uses `page aliasing
1022+
<https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html#supported-architectures>`_,
1023+
which only tags heap allocations and is unsafe for programs that ``fork()``,
1024+
including much of the test suite.
1025+
1026+
.. versionadded:: 3.16
1027+
10181028
.. option:: --with-memory-sanitizer
10191029

10201030
Enable MemorySanitizer allocation error detector, ``msan`` (default is no).

Doc/whatsnew/3.16.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,11 @@ Build changes
934934

935935
(Contributed by Stan Ulbrych in :gh:`139314`.)
936936

937+
* Add the :option:`--with-hwaddress-sanitizer` :program:`configure` option to
938+
build with `HWAddressSanitizer <https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html>`_.
939+
940+
(Contributed by Stan Ulbrych, Florian Mayer and AnnaAr321 in :gh:`156049`.)
941+
937942

938943
C API changes
939944
=============

Include/pyport.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,12 @@ extern "C" {
562562
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
563563
# endif
564564
# endif
565+
# if __has_feature(hwaddress_sanitizer)
566+
# if !defined(_Py_ADDRESS_SANITIZER)
567+
# define _Py_ADDRESS_SANITIZER
568+
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("hwaddress")))
569+
# endif
570+
# endif
565571
# if __has_feature(thread_sanitizer)
566572
# if !defined(_Py_THREAD_SANITIZER)
567573
# define _Py_THREAD_SANITIZER
@@ -572,6 +578,9 @@ extern "C" {
572578
# if defined(__SANITIZE_ADDRESS__)
573579
# define _Py_ADDRESS_SANITIZER
574580
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
581+
# elif defined(__SANITIZE_HWADDRESS__)
582+
# define _Py_ADDRESS_SANITIZER
583+
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("hwaddress")))
575584
# endif
576585
# if defined(__SANITIZE_THREAD__)
577586
# define _Py_THREAD_SANITIZER

Lib/test/support/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ def check_sanitizer(*, address=False, memory=False, ub=False, thread=False,
463463
)
464464
address_sanitizer = (
465465
'-fsanitize=address' in cflags or
466-
'--with-address-sanitizer' in config_args
466+
'-fsanitize=hwaddress' in cflags or
467+
'--with-address-sanitizer' in config_args or
468+
'--with-hwaddress-sanitizer' in config_args
467469
)
468470
ub_sanitizer = (
469471
'-fsanitize=undefined' in cflags or
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :option:`--with-hwaddress-sanitizer` to build with `HWAddressSanitizer
2+
<https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html>`_.

configure

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,30 @@ with_pymalloc="no"
35333533
],
35343534
[AC_MSG_RESULT([no])])
35353535

3536+
AC_MSG_CHECKING([for --with-hwaddress-sanitizer])
3537+
AC_ARG_WITH(
3538+
[hwaddress_sanitizer],
3539+
[AS_HELP_STRING(
3540+
[--with-hwaddress-sanitizer],
3541+
[enable HWAddressSanitizer memory error detector, 'hwasan' (default is no)]
3542+
)],
3543+
[
3544+
AC_MSG_RESULT([$withval])
3545+
hwasan_flags="-fsanitize=hwaddress"
3546+
# x86-64 lacks address tagging, so HWASan needs the page aliasing mode there.
3547+
# See https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html#supported-architectures
3548+
AS_CASE([$host_cpu],
3549+
[x86_64|amd64], [hwasan_flags="$hwasan_flags -fsanitize-hwaddress-experimental-aliasing"]
3550+
)
3551+
AX_CHECK_COMPILE_FLAG([$hwasan_flags],[
3552+
BASECFLAGS="$hwasan_flags -fno-omit-frame-pointer $BASECFLAGS"
3553+
LDFLAGS="$hwasan_flags $LDFLAGS"
3554+
],[AC_MSG_ERROR([The selected compiler doesn't support hardware address sanitizer])])
3555+
# HWASan works by controlling memory allocation, our own malloc interferes.
3556+
with_pymalloc="no"
3557+
],
3558+
[AC_MSG_RESULT([no])])
3559+
35363560
AC_MSG_CHECKING([for --with-memory-sanitizer])
35373561
AC_ARG_WITH(
35383562
[memory_sanitizer],

0 commit comments

Comments
 (0)