1*62c56f98SSadaf Ebrahimi# test_zeroize.gdb 2*62c56f98SSadaf Ebrahimi# 3*62c56f98SSadaf Ebrahimi# Copyright The Mbed TLS Contributors 4*62c56f98SSadaf Ebrahimi# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 5*62c56f98SSadaf Ebrahimi# 6*62c56f98SSadaf Ebrahimi# Purpose 7*62c56f98SSadaf Ebrahimi# 8*62c56f98SSadaf Ebrahimi# Run a test using the debugger to check that the mbedtls_platform_zeroize() 9*62c56f98SSadaf Ebrahimi# function in platform_util.h is not being optimized out by the compiler. To do 10*62c56f98SSadaf Ebrahimi# so, the script loads the test program at programs/test/zeroize.c and sets a 11*62c56f98SSadaf Ebrahimi# breakpoint at the last return statement in main(). When the breakpoint is 12*62c56f98SSadaf Ebrahimi# hit, the debugger manually checks the contents to be zeroized and checks that 13*62c56f98SSadaf Ebrahimi# it is actually cleared. 14*62c56f98SSadaf Ebrahimi# 15*62c56f98SSadaf Ebrahimi# The mbedtls_platform_zeroize() test is debugger driven because there does not 16*62c56f98SSadaf Ebrahimi# seem to be a mechanism to reliably check whether the zeroize calls are being 17*62c56f98SSadaf Ebrahimi# eliminated by compiler optimizations from within the compiled program. The 18*62c56f98SSadaf Ebrahimi# problem is that a compiler would typically remove what it considers to be 19*62c56f98SSadaf Ebrahimi# "unnecessary" assignments as part of redundant code elimination. To identify 20*62c56f98SSadaf Ebrahimi# such code, the compilar will create some form dependency graph between 21*62c56f98SSadaf Ebrahimi# reads and writes to variables (among other situations). It will then use this 22*62c56f98SSadaf Ebrahimi# data structure to remove redundant code that does not have an impact on the 23*62c56f98SSadaf Ebrahimi# program's observable behavior. In the case of mbedtls_platform_zeroize(), an 24*62c56f98SSadaf Ebrahimi# intelligent compiler could determine that this function clears a block of 25*62c56f98SSadaf Ebrahimi# memory that is not accessed later in the program, so removing the call to 26*62c56f98SSadaf Ebrahimi# mbedtls_platform_zeroize() does not have an observable behavior. However, 27*62c56f98SSadaf Ebrahimi# inserting a test after a call to mbedtls_platform_zeroize() to check whether 28*62c56f98SSadaf Ebrahimi# the block of memory was correctly zeroed would force the compiler to not 29*62c56f98SSadaf Ebrahimi# eliminate the mbedtls_platform_zeroize() call. If this does not occur, then 30*62c56f98SSadaf Ebrahimi# the compiler potentially has a bug. 31*62c56f98SSadaf Ebrahimi# 32*62c56f98SSadaf Ebrahimi# Note: This test requires that the test program is compiled with -g3. 33*62c56f98SSadaf Ebrahimi 34*62c56f98SSadaf Ebrahimiset confirm off 35*62c56f98SSadaf Ebrahimi 36*62c56f98SSadaf Ebrahimifile ./programs/test/zeroize 37*62c56f98SSadaf Ebrahimi 38*62c56f98SSadaf Ebrahimisearch GDB_BREAK_HERE 39*62c56f98SSadaf Ebrahimibreak $_ 40*62c56f98SSadaf Ebrahimi 41*62c56f98SSadaf Ebrahimiset args ./programs/test/zeroize.c 42*62c56f98SSadaf Ebrahimirun 43*62c56f98SSadaf Ebrahimi 44*62c56f98SSadaf Ebrahimiset $i = 0 45*62c56f98SSadaf Ebrahimiset $len = sizeof(buf) 46*62c56f98SSadaf Ebrahimiset $buf = buf 47*62c56f98SSadaf Ebrahimi 48*62c56f98SSadaf Ebrahimiwhile $i < $len 49*62c56f98SSadaf Ebrahimi if $buf[$i++] != 0 50*62c56f98SSadaf Ebrahimi echo The buffer at was not zeroized\n 51*62c56f98SSadaf Ebrahimi quit 1 52*62c56f98SSadaf Ebrahimi end 53*62c56f98SSadaf Ebrahimiend 54*62c56f98SSadaf Ebrahimi 55*62c56f98SSadaf Ebrahimiecho The buffer was correctly zeroized\n 56*62c56f98SSadaf Ebrahimi 57*62c56f98SSadaf Ebrahimicontinue 58*62c56f98SSadaf Ebrahimi 59*62c56f98SSadaf Ebrahimiif $_exitcode != 0 60*62c56f98SSadaf Ebrahimi echo The program did not terminate correctly\n 61*62c56f98SSadaf Ebrahimi quit 1 62*62c56f98SSadaf Ebrahimiend 63*62c56f98SSadaf Ebrahimi 64*62c56f98SSadaf Ebrahimiquit 0 65