From 7d68a1954c99581059432291b00d1aa332c715ab Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 23 Nov 2023 12:33:39 +0100 Subject: [PATCH] Protect against compiler optimizations GCC 5.4 optimized the write after poisoning (the surprising thing is that 11.4 doesn't). Signed-off-by: Gilles Peskine --- programs/test/metatest.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/programs/test/metatest.c b/programs/test/metatest.c index 0910798e1..1724aed0b 100644 --- a/programs/test/metatest.c +++ b/programs/test/metatest.c @@ -66,6 +66,15 @@ static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t memset((void *) p, false_but_the_compiler_does_not_know, n); } +/* Simulate an access to the given object, to avoid compiler optimizations + * in code that prepares or consumes the object. */ +static void do_nothing_with_object(void *p) +{ + (void) p; +} +void(*volatile do_nothing_with_object_but_the_compiler_does_not_know)(void *) = + do_nothing_with_object; + /****************************************************************/ /* Test framework features */ @@ -202,7 +211,9 @@ void test_memory_poison(const char *name) if (direction == 'w') { aligned.buf[start + offset] = 'b'; + do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf); } else { + do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf); mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]); } }