blob: 57f771f56ad681c88c6c1cb7e3bfa68548da3593 [file] [log] [blame]
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +01001# test_zeroize.gdb
2#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02005#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +01006# Purpose
7#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -05008# Run a test using the debugger to check that the mbedtls_platform_zeroize()
9# function in platform_util.h is not being optimized out by the compiler. To do
10# so, the script loads the test program at programs/test/zeroize.c and sets a
11# breakpoint at the last return statement in main(). When the breakpoint is
12# hit, the debugger manually checks the contents to be zeroized and checks that
13# it is actually cleared.
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010014#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050015# The mbedtls_platform_zeroize() test is debugger driven because there does not
16# seem to be a mechanism to reliably check whether the zeroize calls are being
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000017# eliminated by compiler optimizations from within the compiled program. The
18# problem is that a compiler would typically remove what it considers to be
Antonin Décimo36e89b52019-01-23 15:24:37 +010019# "unnecessary" assignments as part of redundant code elimination. To identify
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000020# such code, the compilar will create some form dependency graph between
21# reads and writes to variables (among other situations). It will then use this
22# data structure to remove redundant code that does not have an impact on the
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050023# program's observable behavior. In the case of mbedtls_platform_zeroize(), an
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000024# intelligent compiler could determine that this function clears a block of
25# memory that is not accessed later in the program, so removing the call to
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050026# mbedtls_platform_zeroize() does not have an observable behavior. However,
Andres Amaya Garcia708c5cb2018-04-24 08:33:31 -050027# inserting a test after a call to mbedtls_platform_zeroize() to check whether
28# the block of memory was correctly zeroed would force the compiler to not
29# eliminate the mbedtls_platform_zeroize() call. If this does not occur, then
30# the compiler potentially has a bug.
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000031#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010032# Note: This test requires that the test program is compiled with -g3.
33
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010034set confirm off
Gilles Peskine427df372018-09-27 11:50:24 +020035
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010036file ./programs/test/zeroize
Bence Szépkúti5620d712020-06-09 12:52:04 +020037
38search GDB_BREAK_HERE
39break $_
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010040
41set args ./programs/test/zeroize.c
42run
43
44set $i = 0
45set $len = sizeof(buf)
46set $buf = buf
47
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010048while $i < $len
49 if $buf[$i++] != 0
50 echo The buffer at was not zeroized\n
51 quit 1
52 end
53end
54
55echo The buffer was correctly zeroized\n
Andres Amaya Garcia806f4032017-11-01 10:03:36 +000056
57continue
58
59if $_exitcode != 0
60 echo The program did not terminate correctly\n
61 quit 1
62end
63
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010064quit 0