blob: 66c63040869fe0d9938ed702f0febf86b49b21af [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
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010018# Purpose
19#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050020# Run a test using the debugger to check that the mbedtls_platform_zeroize()
21# function in platform_util.h is not being optimized out by the compiler. To do
22# so, the script loads the test program at programs/test/zeroize.c and sets a
23# breakpoint at the last return statement in main(). When the breakpoint is
24# hit, the debugger manually checks the contents to be zeroized and checks that
25# it is actually cleared.
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010026#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050027# The mbedtls_platform_zeroize() test is debugger driven because there does not
28# seem to be a mechanism to reliably check whether the zeroize calls are being
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000029# eliminated by compiler optimizations from within the compiled program. The
30# problem is that a compiler would typically remove what it considers to be
Antonin Décimo36e89b52019-01-23 15:24:37 +010031# "unnecessary" assignments as part of redundant code elimination. To identify
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000032# such code, the compilar will create some form dependency graph between
33# reads and writes to variables (among other situations). It will then use this
34# data structure to remove redundant code that does not have an impact on the
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050035# program's observable behavior. In the case of mbedtls_platform_zeroize(), an
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000036# intelligent compiler could determine that this function clears a block of
37# memory that is not accessed later in the program, so removing the call to
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050038# mbedtls_platform_zeroize() does not have an observable behavior. However,
Andres Amaya Garcia708c5cb2018-04-24 08:33:31 -050039# inserting a test after a call to mbedtls_platform_zeroize() to check whether
40# the block of memory was correctly zeroed would force the compiler to not
41# eliminate the mbedtls_platform_zeroize() call. If this does not occur, then
42# the compiler potentially has a bug.
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000043#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010044# Note: This test requires that the test program is compiled with -g3.
45
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010046set confirm off
Gilles Peskine427df372018-09-27 11:50:24 +020047
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010048file ./programs/test/zeroize
Bence Szépkúti5620d712020-06-09 12:52:04 +020049
50search GDB_BREAK_HERE
51break $_
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010052
53set args ./programs/test/zeroize.c
54run
55
56set $i = 0
57set $len = sizeof(buf)
58set $buf = buf
59
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010060while $i < $len
61 if $buf[$i++] != 0
62 echo The buffer at was not zeroized\n
63 quit 1
64 end
65end
66
67echo The buffer was correctly zeroized\n
Andres Amaya Garcia806f4032017-11-01 10:03:36 +000068
69continue
70
71if $_exitcode != 0
72 echo The program did not terminate correctly\n
73 quit 1
74end
75
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010076quit 0