From d7787191e3a40616d78637341c29f75b86ea4025 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Wed, 9 Sep 2020 23:23:56 +0100 Subject: [PATCH] exercises/pointer-revocation: make arg volatile Otherwise the compiler can see that we're only using obj1->arg and so won't necessarily reload it (and might not even store it) as we wish for demonstration. --- src/exercises/pointer-revocation/temporal-control.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/exercises/pointer-revocation/temporal-control.c b/src/exercises/pointer-revocation/temporal-control.c index 820b8a2..fd9b430 100644 --- a/src/exercises/pointer-revocation/temporal-control.c +++ b/src/exercises/pointer-revocation/temporal-control.c @@ -32,9 +32,14 @@ fn2(uintptr_t arg) struct obj { char buf[32]; - /* Volatile so that the compiler will always reload it */ + /* + * The following are marked volatile to ensure the compiler doesn't + * constant propagate fn (making aliasing not work) and to ensure + * neither stores to them are optimised away entirely as dead due + * to calling free. + */ void (* volatile fn)(uintptr_t); - uintptr_t arg; + volatile uintptr_t arg; }; int