test_zeroize.gdb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # test_zeroize.gdb
  2. #
  3. # Copyright The Mbed TLS Contributors
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. #
  6. # This file is provided under the Apache License 2.0, or the
  7. # GNU General Public License v2.0 or later.
  8. #
  9. # **********
  10. # Apache License 2.0:
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. # not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. #
  24. # **********
  25. #
  26. # **********
  27. # GNU General Public License v2.0 or later:
  28. #
  29. # This program is free software; you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation; either version 2 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. #
  39. # You should have received a copy of the GNU General Public License along
  40. # with this program; if not, write to the Free Software Foundation, Inc.,
  41. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  42. #
  43. # **********
  44. #
  45. # Purpose
  46. #
  47. # Run a test using the debugger to check that the mbedtls_platform_zeroize()
  48. # function in platform_util.h is not being optimized out by the compiler. To do
  49. # so, the script loads the test program at programs/test/zeroize.c and sets a
  50. # breakpoint at the last return statement in main(). When the breakpoint is
  51. # hit, the debugger manually checks the contents to be zeroized and checks that
  52. # it is actually cleared.
  53. #
  54. # The mbedtls_platform_zeroize() test is debugger driven because there does not
  55. # seem to be a mechanism to reliably check whether the zeroize calls are being
  56. # eliminated by compiler optimizations from within the compiled program. The
  57. # problem is that a compiler would typically remove what it considers to be
  58. # "unnecessary" assignments as part of redundant code elimination. To identify
  59. # such code, the compilar will create some form dependency graph between
  60. # reads and writes to variables (among other situations). It will then use this
  61. # data structure to remove redundant code that does not have an impact on the
  62. # program's observable behavior. In the case of mbedtls_platform_zeroize(), an
  63. # intelligent compiler could determine that this function clears a block of
  64. # memory that is not accessed later in the program, so removing the call to
  65. # mbedtls_platform_zeroize() does not have an observable behavior. However,
  66. # inserting a test after a call to mbedtls_platform_zeroize() to check whether
  67. # the block of memory was correctly zeroed would force the compiler to not
  68. # eliminate the mbedtls_platform_zeroize() call. If this does not occur, then
  69. # the compiler potentially has a bug.
  70. #
  71. # Note: This test requires that the test program is compiled with -g3.
  72. set confirm off
  73. file ./programs/test/zeroize
  74. search GDB_BREAK_HERE
  75. break $_
  76. set args ./programs/test/zeroize.c
  77. run
  78. set $i = 0
  79. set $len = sizeof(buf)
  80. set $buf = buf
  81. while $i < $len
  82. if $buf[$i++] != 0
  83. echo The buffer at was not zeroized\n
  84. quit 1
  85. end
  86. end
  87. echo The buffer was correctly zeroized\n
  88. continue
  89. if $_exitcode != 0
  90. echo The program did not terminate correctly\n
  91. quit 1
  92. end
  93. quit 0