From 8055ea064619a003618f2698c45b62bf4d5714b2 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 9 May 2024 14:01:10 +0200 Subject: [PATCH] explicit_bzero, memset_explicit tests: Avoid test failures with ASAN. * tests/test-explicit_bzero.c (test_heap, test_stack): Define to empty if ASAN is enabled. * tests/test-memset_explicit.c (test_heap, test_stack): Likewise. --- ChangeLog | 7 ++++++ tests/test-explicit_bzero.c | 48 ++++++++++++++++++++++++++++++------ tests/test-memset_explicit.c | 48 ++++++++++++++++++++++++++++++------ 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5d9c92533c..90c43861d6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2024-05-09 Bruno Haible + + explicit_bzero, memset_explicit tests: Avoid test failures with ASAN. + * tests/test-explicit_bzero.c (test_heap, test_stack): Define to empty + if ASAN is enabled. + * tests/test-memset_explicit.c (test_heap, test_stack): Likewise. + 2024-05-09 Bruno Haible dprintf-posix, fprintf-posix tests: Avoid test failures with ASAN. diff --git a/tests/test-explicit_bzero.c b/tests/test-explicit_bzero.c index c4e4ddde77..b4c98b6652 100644 --- a/tests/test-explicit_bzero.c +++ b/tests/test-explicit_bzero.c @@ -1,5 +1,5 @@ /* Test of explicit_bzero() function. - Copyright (C) 2020-2023 Free Software Foundation, Inc. + Copyright (C) 2020-2024 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -60,8 +60,22 @@ test_static (void) /* =============== Verify operation on heap-allocated memory =============== */ +/* Skip this part when an address sanitizer is in use, because it would report + a "heap use after free". */ +#ifndef __has_feature +# define __has_feature(a) 0 +#endif +#if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer) + +static void +test_heap (void) +{ +} + +#else + /* Test whether an address range is mapped in memory. */ -#if VMA_ITERATE_SUPPORTED +# if VMA_ITERATE_SUPPORTED struct locals { @@ -96,7 +110,7 @@ is_range_mapped (uintptr_t range_start, uintptr_t range_end) return l.range_start == l.range_end; } -#else +# else static bool is_range_mapped (uintptr_t range_start, uintptr_t range_end) @@ -104,7 +118,7 @@ is_range_mapped (uintptr_t range_start, uintptr_t range_end) return true; } -#endif +# endif static void test_heap (void) @@ -127,8 +141,24 @@ test_heap (void) printf ("test_heap: address range is unmapped after free().\n"); } +#endif /* ! address sanitizer enabled */ + /* =============== Verify operation on stack-allocated memory =============== */ +/* Skip this part when an address sanitizer is in use, because it would report + a "stack use after return". */ +#ifndef __has_feature +# define __has_feature(a) 0 +#endif +#if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer) + +static void +test_stack (void) +{ +} + +#else + /* There are two passes: 1. Put a secret in memory and invoke explicit_bzero on it. 2. Verify that the memory has been erased. @@ -138,12 +168,12 @@ test_heap (void) does not eliminate a call to explicit_bzero, even if data flow analysis reveals that the stack area is dead at the end of the function. */ static bool _GL_ATTRIBUTE_NOINLINE -#if __GNUC__ + (__GNUC_MINOR__ >= 5) > 4 +# if __GNUC__ + (__GNUC_MINOR__ >= 5) > 4 __attribute__ ((__noclone__)) -#endif -#if __GNUC__ >= 8 +# endif +# if __GNUC__ >= 8 __attribute__ ((__noipa__)) -#endif +# endif do_secret_stuff (int volatile pass, char *volatile *volatile last_stackbuf) { char stackbuf[SECRET_SIZE]; @@ -192,6 +222,8 @@ test_stack (void) ASSERT (count < 50); } +#endif /* ! address sanitizer enabled */ + /* ========================================================================== */ int diff --git a/tests/test-memset_explicit.c b/tests/test-memset_explicit.c index 5852caec02..f3da4a775b 100644 --- a/tests/test-memset_explicit.c +++ b/tests/test-memset_explicit.c @@ -1,5 +1,5 @@ /* Test memset_explicit. - Copyright 2020-2023 Free Software Foundation, Inc. + Copyright 2020-2024 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -70,8 +70,22 @@ test_static (void) /* =============== Verify operation on heap-allocated memory =============== */ +/* Skip this part when an address sanitizer is in use, because it would report + a "heap use after free". */ +#ifndef __has_feature +# define __has_feature(a) 0 +#endif +#if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer) + +static void +test_heap (void) +{ +} + +#else + /* Test whether an address range is mapped in memory. */ -#if VMA_ITERATE_SUPPORTED +# if VMA_ITERATE_SUPPORTED struct locals { @@ -106,7 +120,7 @@ is_range_mapped (uintptr_t range_start, uintptr_t range_end) return l.range_start == l.range_end; } -#else +# else static bool is_range_mapped (uintptr_t range_start, uintptr_t range_end) @@ -114,7 +128,7 @@ is_range_mapped (uintptr_t range_start, uintptr_t range_end) return true; } -#endif +# endif static void test_heap (void) @@ -137,8 +151,24 @@ test_heap (void) printf ("test_heap: address range is unmapped after free().\n"); } +#endif /* ! address sanitizer enabled */ + /* =============== Verify operation on stack-allocated memory =============== */ +/* Skip this part when an address sanitizer is in use, because it would report + a "stack use after return". */ +#ifndef __has_feature +# define __has_feature(a) 0 +#endif +#if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer) + +static void +test_stack (void) +{ +} + +#else + /* There are two passes: 1. Put a secret in memory and invoke memset_explicit on it. 2. Verify that the memory has been erased. @@ -148,12 +178,12 @@ test_heap (void) does not eliminate a call to memset_explicit, even if data flow analysis reveals that the stack area is dead at the end of the function. */ static bool _GL_ATTRIBUTE_NOINLINE -#if __GNUC__ + (__GNUC_MINOR__ >= 5) > 4 +# if __GNUC__ + (__GNUC_MINOR__ >= 5) > 4 __attribute__ ((__noclone__)) -#endif -#if __GNUC__ >= 8 +# endif +# if __GNUC__ >= 8 __attribute__ ((__noipa__)) -#endif +# endif do_secret_stuff (int volatile pass, char *volatile *volatile last_stackbuf) { char stackbuf[SECRET_SIZE]; @@ -202,6 +232,8 @@ test_stack (void) ASSERT (count < 50); } +#endif /* ! address sanitizer enabled */ + /* ========================================================================== */ int -- 2.39.5