blob: 8164acb9b1fdfd7e5018fd25d02af7dda155ae79 [file] [log] [blame]
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +01001# test_zeroize.gdb
2#
Andres Amaya Garcia757cd722018-03-08 21:25:25 +00003# This file is part of Mbed TLS (https://tls.mbed.org)
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +01004#
Andres Amaya Garcia757cd722018-03-08 21:25:25 +00005# Copyright (c) 2018, Arm Limited, All Rights Reserved
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +01006#
7# Purpose
8#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -05009# Run a test using the debugger to check that the mbedtls_platform_zeroize()
10# function in platform_util.h is not being optimized out by the compiler. To do
11# so, the script loads the test program at programs/test/zeroize.c and sets a
12# breakpoint at the last return statement in main(). When the breakpoint is
13# hit, the debugger manually checks the contents to be zeroized and checks that
14# it is actually cleared.
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010015#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050016# The mbedtls_platform_zeroize() test is debugger driven because there does not
17# seem to be a mechanism to reliably check whether the zeroize calls are being
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000018# eliminated by compiler optimizations from within the compiled program. The
19# problem is that a compiler would typically remove what it considers to be
Antonin Décimo36e89b52019-01-23 15:24:37 +010020# "unnecessary" assignments as part of redundant code elimination. To identify
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000021# such code, the compilar will create some form dependency graph between
22# reads and writes to variables (among other situations). It will then use this
23# data structure to remove redundant code that does not have an impact on the
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050024# program's observable behavior. In the case of mbedtls_platform_zeroize(), an
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000025# intelligent compiler could determine that this function clears a block of
26# memory that is not accessed later in the program, so removing the call to
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050027# mbedtls_platform_zeroize() does not have an observable behavior. However,
Andres Amaya Garcia708c5cb2018-04-24 08:33:31 -050028# inserting a test after a call to mbedtls_platform_zeroize() to check whether
29# the block of memory was correctly zeroed would force the compiler to not
30# eliminate the mbedtls_platform_zeroize() call. If this does not occur, then
31# the compiler potentially has a bug.
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000032#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010033# Note: This test requires that the test program is compiled with -g3.
34
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010035set confirm off
Gilles Peskine427df372018-09-27 11:50:24 +020036
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010037file ./programs/test/zeroize
Bence Szépkúti5620d712020-06-09 12:52:04 +020038
39search GDB_BREAK_HERE
40break $_
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010041
42set args ./programs/test/zeroize.c
43run
44
45set $i = 0
46set $len = sizeof(buf)
47set $buf = buf
48
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010049while $i < $len
50 if $buf[$i++] != 0
51 echo The buffer at was not zeroized\n
52 quit 1
53 end
54end
55
56echo The buffer was correctly zeroized\n
Andres Amaya Garcia806f4032017-11-01 10:03:36 +000057
58continue
59
60if $_exitcode != 0
61 echo The program did not terminate correctly\n
62 quit 1
63end
64
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010065quit 0