2023-06-12 20:49:49

by Paul E. McKenney

[permalink] [raw]
Subject: [PATCH v2 nolibc 01/53] tools/nolibc: tests: use volatile to force stack smashing

From: Thomas Weißschuh <[email protected]>

Use a volatile pointer to write outside the buffer so the compiler can't
optimize it away.

Reported-by: Alexey Dobriyan <[email protected]>
Link: https://lore.kernel.org/lkml/c0584807-511c-4496-b062-1263ea38f349@p183/
Signed-off-by: Thomas Weißschuh <[email protected]>
Signed-off-by: Willy Tarreau <[email protected]>
Signed-off-by: Paul E. McKenney <[email protected]>
---
tools/testing/selftests/nolibc/nolibc-test.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 21bacc928bf7..47013b78972e 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -667,17 +667,13 @@ int run_stdlib(int min, int max)
return ret;
}

-#if defined(__clang__)
-__attribute__((optnone))
-#elif defined(__GNUC__)
-__attribute__((optimize("O0")))
-#endif
static int smash_stack(void)
{
char buf[100];
+ volatile char *ptr = buf;

for (size_t i = 0; i < 200; i++)
- buf[i] = 'P';
+ ptr[i] = 'P';

return 1;
}
--
2.40.1