hwasan: Check if Intel LAM_U57 is enabled

Message ID 20240109164659.1822407-1-hjl.tools@gmail.com
State Accepted
Headers
Series hwasan: Check if Intel LAM_U57 is enabled |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

H.J. Lu Jan. 9, 2024, 4:46 p.m. UTC
  When -fsanitize=hwaddress is used, libhwasan will try to enable LAM_U57
in the startup code.  Update the target check to enable hwaddress tests
if LAM_U57 is enabled.  Also compile hwaddress tests with -mlam=u57 on
x86-64 since hwasan requires LAM_U57 on x86-64.

	* lib/hwasan-dg.exp (check_effective_target_hwaddress_exec):
	Return 1 if Intel LAM_U57 is enabled.
	(hwasan_init): Add -mlam=u57 on x86-64.
---
 gcc/testsuite/lib/hwasan-dg.exp | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
  

Comments

Hongtao Liu Jan. 18, 2024, 4:51 a.m. UTC | #1
On Wed, Jan 10, 2024 at 12:47 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> When -fsanitize=hwaddress is used, libhwasan will try to enable LAM_U57
> in the startup code.  Update the target check to enable hwaddress tests
> if LAM_U57 is enabled.  Also compile hwaddress tests with -mlam=u57 on
> x86-64 since hwasan requires LAM_U57 on x86-64.
I've tested it on lam enabled SRF, and it passed all hwasan testcases
except below

FAIL: c-c++-common/hwasan/alloca-outside-caught.c   -O0  output pattern test
FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O1
scan-assembler-times bl
s*__hwasan_tag_mismatch4 1
FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O2
scan-assembler-times bl
s*__hwasan_tag_mismatch4 1
FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O3 -g
scan-assembler-times bl
s*__hwasan_tag_mismatch4 1
FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -Os
scan-assembler-times bl
s*__hwasan_tag_mismatch4 1
FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O2 -flto
-fno-use-linker-plugin -flto-partition=none   scan-assembler-times bl
s*__hwasan_tag_mismatch4 1
FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O2 -flto
-fuse-linker-plugin -fno-fat-lto-objects   scan-assembler-times bl
s*__hwasan_tag_mismatch4 1
FAIL: c-c++-common/hwasan/vararray-outside-caught.c   -O0  output pattern test

Basically they're testcase issues, the testcases needs to be adjusted
for x86/ I'll commit a separate patch for those after this commit is
upstream.
Also I've also tested the patch on lam unsupported platforms, all
hwasan testcases shows unsupported.
So the patch LGTM.

>
>         * lib/hwasan-dg.exp (check_effective_target_hwaddress_exec):
>         Return 1 if Intel LAM_U57 is enabled.
>         (hwasan_init): Add -mlam=u57 on x86-64.
> ---
>  gcc/testsuite/lib/hwasan-dg.exp | 25 ++++++++++++++++++++++---
>  1 file changed, 22 insertions(+), 3 deletions(-)
>
> diff --git a/gcc/testsuite/lib/hwasan-dg.exp b/gcc/testsuite/lib/hwasan-dg.exp
> index e9c5ef6524d..76057502ee6 100644
> --- a/gcc/testsuite/lib/hwasan-dg.exp
> +++ b/gcc/testsuite/lib/hwasan-dg.exp
> @@ -44,11 +44,25 @@ proc check_effective_target_hwaddress_exec {} {
>         #ifdef __cplusplus
>         extern "C" {
>         #endif
> +       extern int arch_prctl (int, unsigned long int *);
>         extern int prctl(int, unsigned long, unsigned long, unsigned long, unsigned long);
>         #ifdef __cplusplus
>         }
>         #endif
>         int main (void) {
> +       #ifdef __x86_64__
> +       # ifdef __LP64__
> +       #  define ARCH_GET_UNTAG_MASK 0x4001
> +       #  define LAM_U57_MASK (0x3fULL << 57)
> +         unsigned long mask = 0;
> +         if (arch_prctl(ARCH_GET_UNTAG_MASK, &mask) != 0)
> +           return 1;
> +         if (mask != ~LAM_U57_MASK)
> +           return 1;
> +         return 0;
> +       # endif
> +         return 1;
> +       #else
>         #define PR_SET_TAGGED_ADDR_CTRL 55
>         #define PR_GET_TAGGED_ADDR_CTRL 56
>         #define PR_TAGGED_ADDR_ENABLE (1UL << 0)
> @@ -58,6 +72,7 @@ proc check_effective_target_hwaddress_exec {} {
>               || !prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0))
>             return 1;
>           return 0;
> +       #endif
>         }
>      }] {
>         return 0;
> @@ -102,6 +117,10 @@ proc hwasan_init { args } {
>
>      setenv HWASAN_OPTIONS "random_tags=0"
>
> +    if [istarget x86_64-*-*] {
> +      set target_hwasan_flags "-mlam=u57"
> +    }
> +
>      set link_flags ""
>      if ![is_remote host] {
>         if [info exists TOOL_OPTIONS] {
> @@ -119,12 +138,12 @@ proc hwasan_init { args } {
>      if [info exists ALWAYS_CXXFLAGS] {
>         set hwasan_saved_ALWAYS_CXXFLAGS $ALWAYS_CXXFLAGS
>         set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS]
> -       set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=hwaddress --param hwasan-random-frame-tag=0 -g $include_flags}" $ALWAYS_CXXFLAGS]
> +       set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags}" $ALWAYS_CXXFLAGS]
>      } else {
>         if [info exists TEST_ALWAYS_FLAGS] {
> -           set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress --param hwasan-random-frame-tag=0 -g $include_flags $TEST_ALWAYS_FLAGS"
> +           set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags $TEST_ALWAYS_FLAGS"
>         } else {
> -           set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress --param hwasan-random-frame-tag=0 -g $include_flags"
> +           set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags"
>         }
>      }
>  }
> --
> 2.43.0
>
  
H.J. Lu Jan. 18, 2024, 1:22 p.m. UTC | #2
On Wed, Jan 17, 2024 at 8:51 PM Hongtao Liu <crazylht@gmail.com> wrote:
>
> On Wed, Jan 10, 2024 at 12:47 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > When -fsanitize=hwaddress is used, libhwasan will try to enable LAM_U57
> > in the startup code.  Update the target check to enable hwaddress tests
> > if LAM_U57 is enabled.  Also compile hwaddress tests with -mlam=u57 on
> > x86-64 since hwasan requires LAM_U57 on x86-64.
> I've tested it on lam enabled SRF, and it passed all hwasan testcases
> except below
>
> FAIL: c-c++-common/hwasan/alloca-outside-caught.c   -O0  output pattern test
> FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O1
> scan-assembler-times bl
> s*__hwasan_tag_mismatch4 1
> FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O2
> scan-assembler-times bl
> s*__hwasan_tag_mismatch4 1
> FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O3 -g
> scan-assembler-times bl
> s*__hwasan_tag_mismatch4 1
> FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -Os
> scan-assembler-times bl
> s*__hwasan_tag_mismatch4 1
> FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O2 -flto
> -fno-use-linker-plugin -flto-partition=none   scan-assembler-times bl
> s*__hwasan_tag_mismatch4 1
> FAIL: c-c++-common/hwasan/hwasan-poison-optimisation.c   -O2 -flto
> -fuse-linker-plugin -fno-fat-lto-objects   scan-assembler-times bl
> s*__hwasan_tag_mismatch4 1
> FAIL: c-c++-common/hwasan/vararray-outside-caught.c   -O0  output pattern test
>
> Basically they're testcase issues, the testcases needs to be adjusted
> for x86/ I'll commit a separate patch for those after this commit is
> upstream.
> Also I've also tested the patch on lam unsupported platforms, all
> hwasan testcases shows unsupported.
> So the patch LGTM.
>
> >
> >         * lib/hwasan-dg.exp (check_effective_target_hwaddress_exec):
> >         Return 1 if Intel LAM_U57 is enabled.
> >         (hwasan_init): Add -mlam=u57 on x86-64.

Pushed.  LAM has been enabled in GCC 13:

[hjl@gnu-cfl-3 tmp]$ gcc -fsanitize=hwaddress -mlam=u57 alloca-outside-caught.c
[hjl@gnu-cfl-3 tmp]$ ./a.out
FATAL: HWAddressSanitizer requires a kernel with tagged address ABI.
[hjl@gnu-cfl-3 tmp]$ strace ./a.out
...
arch_prctl(ARCH_GET_MAX_TAG_BITS, 0x7ffc56267708) = 0
write(2, "FATAL: HWAddressSanitizer requir"..., 69FATAL:
HWAddressSanitizer requires a kernel with tagged address ABI.

I'd like to backport it to GCC 13.

We should mention LAM in changes for GCC 13.
  

Patch

diff --git a/gcc/testsuite/lib/hwasan-dg.exp b/gcc/testsuite/lib/hwasan-dg.exp
index e9c5ef6524d..76057502ee6 100644
--- a/gcc/testsuite/lib/hwasan-dg.exp
+++ b/gcc/testsuite/lib/hwasan-dg.exp
@@ -44,11 +44,25 @@  proc check_effective_target_hwaddress_exec {} {
 	#ifdef __cplusplus
 	extern "C" {
 	#endif
+	extern int arch_prctl (int, unsigned long int *);
 	extern int prctl(int, unsigned long, unsigned long, unsigned long, unsigned long);
 	#ifdef __cplusplus
 	}
 	#endif
 	int main (void) {
+	#ifdef __x86_64__
+	# ifdef __LP64__
+	#  define ARCH_GET_UNTAG_MASK 0x4001
+	#  define LAM_U57_MASK (0x3fULL << 57)
+	  unsigned long mask = 0;
+	  if (arch_prctl(ARCH_GET_UNTAG_MASK, &mask) != 0)
+	    return 1;
+	  if (mask != ~LAM_U57_MASK)
+	    return 1;
+	  return 0;
+	# endif
+	  return 1;
+	#else
 	#define PR_SET_TAGGED_ADDR_CTRL 55
 	#define PR_GET_TAGGED_ADDR_CTRL 56
 	#define PR_TAGGED_ADDR_ENABLE (1UL << 0)
@@ -58,6 +72,7 @@  proc check_effective_target_hwaddress_exec {} {
 	      || !prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0))
 	    return 1;
 	  return 0;
+	#endif
 	}
     }] {
 	return 0;
@@ -102,6 +117,10 @@  proc hwasan_init { args } {
 
     setenv HWASAN_OPTIONS "random_tags=0"
 
+    if [istarget x86_64-*-*] {
+      set target_hwasan_flags "-mlam=u57"
+    }
+
     set link_flags ""
     if ![is_remote host] {
 	if [info exists TOOL_OPTIONS] {
@@ -119,12 +138,12 @@  proc hwasan_init { args } {
     if [info exists ALWAYS_CXXFLAGS] {
 	set hwasan_saved_ALWAYS_CXXFLAGS $ALWAYS_CXXFLAGS
 	set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS]
-	set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=hwaddress --param hwasan-random-frame-tag=0 -g $include_flags}" $ALWAYS_CXXFLAGS]
+	set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags}" $ALWAYS_CXXFLAGS]
     } else {
 	if [info exists TEST_ALWAYS_FLAGS] {
-	    set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress --param hwasan-random-frame-tag=0 -g $include_flags $TEST_ALWAYS_FLAGS"
+	    set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags $TEST_ALWAYS_FLAGS"
 	} else {
-	    set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress --param hwasan-random-frame-tag=0 -g $include_flags"
+	    set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags"
 	}
     }
 }