diff --git a/test/unit/guard.cc b/test/unit/guard.cc
index 231b44c7df24e30d8c4948d82015564a219254cf..3d42fee31b0810ec190cce9885f31ae018072b39 100644
--- a/test/unit/guard.cc
+++ b/test/unit/guard.cc
@@ -13,7 +13,11 @@ static int __attribute__((noinline))
 stack_break_f(char *ptr)
 {
 	char block[2048];
-	char sum = 0;
+	/*
+	 * Make sum volatile to prevent a compiler from
+	 * optimizing away call to this function.
+	 */
+	volatile char sum = 0;
 	memset(block, 0xff, 2048);
 	sum += block[block[4]];
 	ptrdiff_t stack_diff = ptr > block ? ptr - block : block - ptr;