Strengthen against possible compiler optimizations
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/programs/test/metatest.c b/programs/test/metatest.c
index 7e173ee..805de2d 100644
--- a/programs/test/metatest.c
+++ b/programs/test/metatest.c
@@ -25,10 +25,15 @@
/* This is an external variable, so the compiler doesn't know that we're never
* changing its value.
- *
- * TODO: LTO (link-time-optimization) would defeat this.
*/
-int false_but_the_compiler_does_not_know = 0;
+volatile int false_but_the_compiler_does_not_know = 0;
+
+/* Set n bytes at the address p to all-bits-zero, in such a way that
+ * the compiler should not know that p is all-bits-zero. */
+static void set_to_zero_but_the_compiler_does_not_know(void *p, size_t n)
+{
+ memset(p, false_but_the_compiler_does_not_know, n);
+}
/****************************************************************/
@@ -50,7 +55,7 @@
{
(void) name;
volatile char *p;
- mbedtls_platform_zeroize((void *) &p, sizeof(p));
+ set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
}
@@ -58,7 +63,7 @@
{
(void) name;
unsigned (*p)(void);
- mbedtls_platform_zeroize(&p, sizeof(p));
+ set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
/* The pointer representation may be truncated, but we don't care:
* the only point of printing it is to have some use of the pointer
* to dissuade the compiler from optimizing it away. */
@@ -104,8 +109,7 @@
{
(void) name;
volatile char *p = mbedtls_calloc(1, 1);
- /* Hint to the compiler that calloc must not be optimized away. */
- (void) *p;
+ mbedtls_printf("%u\n", (unsigned) *p);
}