blob: 0a50bebc519bd979cc743c8363f5d0bc1b31eda1 [file] [log] [blame]
Gilles Peskine62cf2e82020-03-27 16:35:23 +01001#! /usr/bin/env bash
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +01002
Simon Butcher3ea7f522016-03-07 23:22:10 +00003# all.sh
4#
Bence Szépkúti1e148272020-08-07 13:07:28 +02005# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine192c72f2017-12-21 15:59:21 +01007
8
9
10################################################################
11#### Documentation
12################################################################
13
Simon Butcher3ea7f522016-03-07 23:22:10 +000014# Purpose
Gilles Peskine192c72f2017-12-21 15:59:21 +010015# -------
Simon Butcher3ea7f522016-03-07 23:22:10 +000016#
SimonB2e23c822016-04-16 21:54:39 +010017# To run all tests possible or available on the platform.
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +010018#
Gilles Peskine192c72f2017-12-21 15:59:21 +010019# Notes for users
20# ---------------
21#
SimonB2e23c822016-04-16 21:54:39 +010022# Warning: the test is destructive. It includes various build modes and
23# configurations, and can and will arbitrarily change the current CMake
Gilles Peskine192c72f2017-12-21 15:59:21 +010024# configuration. The following files must be committed into git:
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020025# * include/mbedtls/mbedtls_config.h
Gilles Peskine636c26a2020-03-04 20:46:15 +010026# * Makefile, library/Makefile, programs/Makefile, tests/Makefile,
27# programs/fuzz/Makefile
Gilles Peskine192c72f2017-12-21 15:59:21 +010028# After running this script, the CMake cache will be lost and CMake
29# will no longer be initialised.
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +010030#
Gilles Peskine192c72f2017-12-21 15:59:21 +010031# The script assumes the presence of a number of tools:
32# * Basic Unix tools (Windows users note: a Unix-style find must be before
33# the Windows find in the PATH)
34# * Perl
35# * GNU Make
36# * CMake
37# * GCC and Clang (recent enough for using ASan with gcc and MemSan with clang, or valgrind)
Andrzej Kurek05be06c2018-06-28 04:41:50 -040038# * G++
Gilles Peskine192c72f2017-12-21 15:59:21 +010039# * arm-gcc and mingw-gcc
40# * ArmCC 5 and ArmCC 6, unless invoked with --no-armcc
Gilles Peskine7be571a2023-08-27 21:39:21 +020041# * OpenSSL and GnuTLS command line tools, in suitable versions for the
42# interoperability tests. The following are the official versions at the
43# time of writing:
44# * GNUTLS_{CLI,SERV} = 3.4.10
45# * GNUTLS_NEXT_{CLI,SERV} = 3.7.2
46# * OPENSSL = 1.0.2g (without Debian/Ubuntu patches)
Ronald Cron561181c2024-04-02 14:39:53 +020047# * OPENSSL_NEXT = 3.1.2
Gilles Peskine192c72f2017-12-21 15:59:21 +010048# See the invocation of check_tools below for details.
49#
50# This script must be invoked from the toplevel directory of a git
51# working copy of Mbed TLS.
52#
Gilles Peskinece266c42020-03-28 18:50:43 +010053# The behavior on an error depends on whether --keep-going (alias -k)
54# is in effect.
55# * Without --keep-going: the script stops on the first error without
56# cleaning up. This lets you work in the configuration of the failing
57# component.
58# * With --keep-going: the script runs all requested components and
59# reports failures at the end. In particular the script always cleans
60# up on exit.
61#
Gilles Peskine192c72f2017-12-21 15:59:21 +010062# Note that the output is not saved. You may want to run
63# script -c tests/scripts/all.sh
64# or
65# tests/scripts/all.sh >all.log 2>&1
66#
67# Notes for maintainers
68# ---------------------
69#
Gilles Peskine8f073122018-11-27 15:58:47 +010070# The bulk of the code is organized into functions that follow one of the
71# following naming conventions:
72# * pre_XXX: things to do before running the tests, in order.
73# * component_XXX: independent components. They can be run in any order.
Gilles Peskinec70637a2019-01-09 22:29:17 +010074# * component_check_XXX: quick tests that aren't worth parallelizing.
75# * component_build_XXX: build things but don't run them.
76# * component_test_XXX: build and test.
Bence Szépkúti89dd5c02023-12-15 17:05:15 +010077# * component_release_XXX: tests that the CI should skip during PR testing.
Gilles Peskine878cf602019-01-06 20:50:38 +000078# * support_XXX: if support_XXX exists and returns false then
79# component_XXX is not run by default.
Gilles Peskine8f073122018-11-27 15:58:47 +010080# * post_XXX: things to do after running the tests.
81# * other: miscellaneous support functions.
82#
Gilles Peskinec70637a2019-01-09 22:29:17 +010083# Each component must start by invoking `msg` with a short informative message.
84#
Gilles Peskinea681c592020-03-28 21:27:40 +010085# Warning: due to the way bash detects errors, the failure of a command
86# inside 'if' or '!' is not detected. Use the 'not' function instead of '!'.
87#
Gilles Peskinece266c42020-03-28 18:50:43 +010088# Each component is executed in a separate shell process. The component
89# fails if any command in it returns a non-zero status.
90#
Gilles Peskinec70637a2019-01-09 22:29:17 +010091# The framework performs some cleanup tasks after each component. This
92# means that components can assume that the working directory is in a
93# cleaned-up state, and don't need to perform the cleanup themselves.
94# * Run `make clean`.
Bence Szépkúti414d6bd2021-06-28 14:11:11 +010095# * Restore `include/mbedtls/mbedtls_config.h` from a backup made before running
Gilles Peskinec70637a2019-01-09 22:29:17 +010096# the component.
Gilles Peskine636c26a2020-03-04 20:46:15 +010097# * Check out `Makefile`, `library/Makefile`, `programs/Makefile`,
98# `tests/Makefile` and `programs/fuzz/Makefile` from git.
99# This cleans up after an in-tree use of CMake.
Gilles Peskinec70637a2019-01-09 22:29:17 +0100100#
Gilles Peskine192c72f2017-12-21 15:59:21 +0100101# The tests are roughly in order from fastest to slowest. This doesn't
102# have to be exact, but in general you should add slower tests towards
103# the end and fast checks near the beginning.
Gilles Peskine192c72f2017-12-21 15:59:21 +0100104
105
106
107################################################################
108#### Initialization and command line parsing
109################################################################
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100110
Manuel Pégourié-Gonnard2c3a6242024-10-01 11:54:34 +0200111# Enable ksh/bash extended file matching patterns.
112# Must come before function definitions or some of them wouldn't parse.
Gilles Peskine076f7252022-08-30 21:02:44 +0200113shopt -s extglob
114
Manuel Pégourié-Gonnard2c3a6242024-10-01 11:54:34 +0200115pre_set_shell_options () {
116 # Abort on errors (even on the left-hand side of a pipe).
117 # Treat uninitialised variables as errors.
118 set -e -o pipefail -u
119}
120
Thomas Daubney2f556402024-07-30 15:52:58 +0100121# For project detection
David Horstmann76a77382023-08-17 17:32:26 +0100122in_mbedtls_repo () {
Thomas Daubney2f556402024-07-30 15:52:58 +0100123 test "$PROJECT_NAME" = "Mbed TLS"
David Horstmannd02b5f82023-08-29 09:53:52 +0100124}
125
Ronald Cron070e8652023-10-09 10:25:45 +0200126in_tf_psa_crypto_repo () {
Thomas Daubney2f556402024-07-30 15:52:58 +0100127 test "$PROJECT_NAME" = "TF-PSA-Crypto"
David Horstmann9a6c45b2023-07-14 12:30:00 +0100128}
129
Gilles Peskine8f073122018-11-27 15:58:47 +0100130pre_check_environment () {
Thomas Daubney5f0b64a2024-08-06 17:38:19 +0100131 # For project detection
132 PROJECT_NAME_FILE='./scripts/project_name.txt'
133 if read -r PROJECT_NAME < "$PROJECT_NAME_FILE"; then :; else
134 echo "$PROJECT_NAME_FILE does not exist... Exiting..." >&2
135 exit 1
136 fi
137
Ronald Cron070e8652023-10-09 10:25:45 +0200138 if in_mbedtls_repo || in_tf_psa_crypto_repo; then :; else
139 echo "Must be run from Mbed TLS / TF-PSA-Crypto root" >&2
Gilles Peskine8f073122018-11-27 15:58:47 +0100140 exit 1
141 fi
142}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100143
Manuel Pégourié-Gonnard03ad80c2024-10-01 13:06:51 +0200144# Must be called before pre_initialize_variables which sets ALL_COMPONENTS.
145pre_load_components () {
146 # Include the components from components.sh
147 test_script_dir="${0%/*}"
148 for file in "$test_script_dir"/components*.sh; do
149 source $file
150 done
151}
152
Gilles Peskine8f073122018-11-27 15:58:47 +0100153pre_initialize_variables () {
David Horstmann76a77382023-08-17 17:32:26 +0100154 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100155 CONFIG_H='include/mbedtls/mbedtls_config.h'
Manuel Pégourié-Gonnard6c0c0f22024-10-16 10:12:00 +0200156 if [ -d tf-psa-crypto ]; then
157 CRYPTO_CONFIG_H='tf-psa-crypto/include/psa/crypto_config.h'
158 PSA_CORE_PATH='tf-psa-crypto/core'
159 BUILTIN_SRC_PATH='tf-psa-crypto/drivers/builtin/src'
160 else
161 CRYPTO_CONFIG_H='include/psa/crypto_config.h'
162 fi
David Horstmann76a77382023-08-17 17:32:26 +0100163 else
164 CONFIG_H='drivers/builtin/include/mbedtls/mbedtls_config.h'
Manuel Pégourié-Gonnard6c0c0f22024-10-16 10:12:00 +0200165 CRYPTO_CONFIG_H='include/psa/crypto_config.h'
166 PSA_CORE_PATH='core'
167 BUILTIN_SRC_PATH='drivers/builtin/src'
David Horstmann9a6c45b2023-07-14 12:30:00 +0100168 fi
Manuel Pégourié-Gonnard462e3a92022-12-27 12:35:11 +0100169 CONFIG_TEST_DRIVER_H='tests/include/test/drivers/config_test_driver.h'
Gilles Peskinef83eb822020-03-30 20:11:39 +0200170
171 # Files that are clobbered by some jobs will be backed up. Use a different
172 # suffix from auxiliary scripts so that all.sh and auxiliary scripts can
173 # independently decide when to remove the backup file.
174 backup_suffix='.all.bak'
175 # Files clobbered by config.py
Janos Follath2f045822023-07-31 10:57:16 +0100176 files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H $CONFIG_TEST_DRIVER_H"
David Horstmann76a77382023-08-17 17:32:26 +0100177 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100178 # Files clobbered by in-tree cmake
179 files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile"
180 fi
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200181
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200182 append_outcome=0
Gilles Peskine8f073122018-11-27 15:58:47 +0100183 MEMORY=0
184 FORCE=0
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200185 QUIET=0
Gilles Peskine8f073122018-11-27 15:58:47 +0100186 KEEP_GOING=0
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100187
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200188 # Seed value used with the --release-test option.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +0200189 #
190 # See also RELEASE_SEED in basic-build-test.sh. Debugging is easier if
191 # both values are kept in sync. If you change the value here because it
192 # breaks some tests, you'll definitely want to change it in
193 # basic-build-test.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200194 RELEASE_SEED=1
195
Yanray Wang18040ed2023-11-23 21:29:56 +0800196 # Specify character collation for regular expressions and sorting with C locale
197 export LC_COLLATE=C
198
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200199 : ${MBEDTLS_TEST_OUTCOME_FILE=}
Gilles Peskine9004a172019-09-16 15:20:36 +0200200 : ${MBEDTLS_TEST_PLATFORM="$(uname -s | tr -c \\n0-9A-Za-z _)-$(uname -m | tr -c \\n0-9A-Za-z _)"}
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200201 export MBEDTLS_TEST_OUTCOME_FILE
Gilles Peskine9004a172019-09-16 15:20:36 +0200202 export MBEDTLS_TEST_PLATFORM
203
Jaeden Ameroc4cc2512019-01-30 15:35:44 +0000204 # Default commands, can be overridden by the environment
Gilles Peskine8f073122018-11-27 15:58:47 +0100205 : ${OPENSSL:="openssl"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100206 : ${OPENSSL_NEXT:="$OPENSSL"}
207 : ${GNUTLS_CLI:="gnutls-cli"}
208 : ${GNUTLS_SERV:="gnutls-serv"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100209 : ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
210 : ${ARMC5_BIN_DIR:=/usr/bin}
211 : ${ARMC6_BIN_DIR:=/usr/bin}
Gilles Peskine6d061342020-04-30 18:19:32 +0200212 : ${ARM_NONE_EABI_GCC_PREFIX:=arm-none-eabi-}
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200213 : ${ARM_LINUX_GNUEABI_GCC_PREFIX:=arm-linux-gnueabi-}
Gowtham Suresh Kumar9da40b82023-07-31 16:38:10 +0100214 : ${CLANG_LATEST:="clang-latest"}
215 : ${CLANG_EARLIEST:="clang-earliest"}
216 : ${GCC_LATEST:="gcc-latest"}
217 : ${GCC_EARLIEST:="gcc-earliest"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100218 # if MAKEFLAGS is not set add the -j option to speed up invocations of make
Gilles Peskinea1fc4b52019-01-06 20:15:26 +0000219 if [ -z "${MAKEFLAGS+set}" ]; then
Gilles Peskine050d2fc2021-09-30 18:24:21 +0200220 export MAKEFLAGS="-j$(all_sh_nproc)"
Gilles Peskine8f073122018-11-27 15:58:47 +0100221 fi
Dave Rodgman0c5bfe82023-12-18 19:53:25 +0000222 # if CC is not set, use clang by default (if present) to improve build times
223 if [ -z "${CC+set}" ] && (type clang > /dev/null 2>&1); then
Dave Rodgmanfc5b9552023-12-19 16:08:19 +0000224 export CC="clang"
Dave Rodgman66cbc832023-12-18 18:34:50 +0000225 fi
Gilles Peskine878cf602019-01-06 20:50:38 +0000226
Ronald Cron561181c2024-04-02 14:39:53 +0200227 if [ -n "${OPENSSL_3+set}" ]; then
228 export OPENSSL_NEXT="$OPENSSL_3"
229 fi
230
Gilles Peskine396853a2021-09-20 18:57:55 +0200231 # Include more verbose output for failing tests run by CMake or make
Jaeden Amerod48e9c72019-02-07 17:43:39 +0000232 export CTEST_OUTPUT_ON_FAILURE=1
233
Gilles Peskine8fd59422019-10-21 17:11:33 +0200234 # CFLAGS and LDFLAGS for Asan builds that don't use CMake
Manuel Pégourié-Gonnardfe549a72022-11-30 10:42:03 +0100235 # default to -O2, use -Ox _after_ this if you want another level
Przemek Stekiel6efa6082023-05-31 09:38:21 +0200236 ASAN_CFLAGS='-O2 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
Dave Rodgman17127e92023-12-14 16:42:48 +0000237 # Normally, tests should use this compiler for ASAN testing
238 ASAN_CC=clang
Gilles Peskine8fd59422019-10-21 17:11:33 +0200239
Andrzej Kurek04bfe572023-06-27 10:02:09 -0400240 # Platform tests have an allocation that returns null
241 export ASAN_OPTIONS="allocator_may_return_null=1"
Andrzej Kurek026235c2023-07-05 08:32:43 -0400242 export MSAN_OPTIONS="allocator_may_return_null=1"
Andrzej Kurek04bfe572023-06-27 10:02:09 -0400243
Gilles Peskine878cf602019-01-06 20:50:38 +0000244 # Gather the list of available components. These are the functions
245 # defined in this script whose name starts with "component_".
Gowtham Suresh Kumar1e829a42023-07-28 16:41:21 +0100246 ALL_COMPONENTS=$(compgen -A function component_ | sed 's/component_//')
Gilles Peskine878cf602019-01-06 20:50:38 +0000247
Manuel Pégourié-Gonnard8535f4c2024-10-16 10:14:49 +0200248 PSASIM_PATH='tests/psa-client-server/psasim/'
249
Dave Rodgman5f8e2a22024-01-16 17:33:34 +0000250 # Delay determining SUPPORTED_COMPONENTS until the command line options have a chance to override
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100251 # the commands set by the environment
Gilles Peskine8f073122018-11-27 15:58:47 +0100252}
Andres AG38495a32016-07-12 16:54:33 +0100253
Dave Rodgman21900632024-02-26 11:41:19 +0000254setup_quiet_wrappers()
255{
256 # Pick up "quiet" wrappers for make and cmake, which don't output very much
257 # unless there is an error. This reduces logging overhead in the CI.
258 #
259 # Note that the cmake wrapper breaks unless we use an absolute path here.
Dave Rodgmana9e8dbe2024-02-26 17:27:18 +0000260 if [[ -e ${PWD}/tests/scripts/quiet ]]; then
261 export PATH=${PWD}/tests/scripts/quiet:$PATH
Dave Rodgman21900632024-02-26 11:41:19 +0000262 fi
263}
264
Gilles Peskinea28db922019-01-10 00:05:18 +0100265# Test whether the component $1 is included in the command line patterns.
266is_component_included()
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100267{
Gilles Peskine03af6782021-08-06 11:35:17 +0200268 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
269 # only does word splitting.
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100270 set -f
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000271 for pattern in $COMMAND_LINE_COMPONENTS; do
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100272 set +f
273 case ${1#component_} in $pattern) return 0;; esac
274 done
275 set +f
276 return 1
277}
Andres AG7770ea82016-10-10 15:46:20 +0100278
Simon Butcher41eeccf2016-09-07 00:07:09 +0100279usage()
Andres AGd9eba4b2016-08-26 14:42:14 +0100280{
Gilles Peskine709346a2017-12-10 23:43:39 +0100281 cat <<EOF
Gilles Peskine92525112018-11-27 18:15:35 +0100282Usage: $0 [OPTION]... [COMPONENT]...
Gilles Peskine348fb9a2018-11-27 17:04:29 +0100283Run mbedtls release validation tests.
Gilles Peskine92525112018-11-27 18:15:35 +0100284By default, run all tests. With one or more COMPONENT, run only those.
Gilles Peskinea28db922019-01-10 00:05:18 +0100285COMPONENT can be the name of a component or a shell wildcard pattern.
286
287Examples:
288 $0 "check_*"
289 Run all sanity checks.
290 $0 --no-armcc --except test_memsan
291 Run everything except builds that require armcc and MemSan.
Gilles Peskine348fb9a2018-11-27 17:04:29 +0100292
293Special options:
294 -h|--help Print this help and exit.
Gilles Peskine878cf602019-01-06 20:50:38 +0000295 --list-all-components List all available test components and exit.
296 --list-components List components supported on this platform and exit.
Gilles Peskine709346a2017-12-10 23:43:39 +0100297
298General options:
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200299 -q|--quiet Only output component names, and errors if any.
Gilles Peskine709346a2017-12-10 23:43:39 +0100300 -f|--force Force the tests to overwrite any modified files.
Gilles Peskine7c652162017-12-11 00:01:40 +0100301 -k|--keep-going Run all tests and report errors at the end.
Gilles Peskine709346a2017-12-10 23:43:39 +0100302 -m|--memory Additional optional memory tests.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200303 --append-outcome Append to the outcome file (if used).
Gilles Peskine6d061342020-04-30 18:19:32 +0200304 --arm-none-eabi-gcc-prefix=<string>
305 Prefix for a cross-compiler for arm-none-eabi
306 (default: "${ARM_NONE_EABI_GCC_PREFIX}")
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200307 --arm-linux-gnueabi-gcc-prefix=<string>
308 Prefix for a cross-compiler for arm-linux-gnueabi
309 (default: "${ARM_LINUX_GNUEABI_GCC_PREFIX}")
Gilles Peskinebca6ab92017-12-19 18:24:31 +0100310 --armcc Run ARM Compiler builds (on by default).
Gilles Peskine80ddb992021-08-06 11:51:59 +0200311 --restore First clean up the build tree, restoring backed up
312 files. Do not run any components unless they are
313 explicitly specified.
Gilles Peskine5d996822020-03-28 21:09:21 +0100314 --error-test Error test mode: run a failing function in addition
Gilles Peskine86f61292021-08-05 15:11:33 +0200315 to any specified component. May be repeated.
Gilles Peskinea28db922019-01-10 00:05:18 +0100316 --except Exclude the COMPONENTs listed on the command line,
317 instead of running only those.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200318 --no-append-outcome Write a new outcome file and analyze it (default).
Gilles Peskinebca6ab92017-12-19 18:24:31 +0100319 --no-armcc Skip ARM Compiler builds.
Gilles Peskine38d81652018-03-21 08:40:26 +0100320 --no-force Refuse to overwrite modified files (default).
321 --no-keep-going Stop at the first error (default).
322 --no-memory No additional memory tests (default).
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800323 --no-quiet Print full output from components.
Gilles Peskine709346a2017-12-10 23:43:39 +0100324 --out-of-source-dir=<path> Directory used for CMake out-of-source build tests.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200325 --outcome-file=<path> File where test outcomes are written (not done if
326 empty; default: \$MBEDTLS_TEST_OUTCOME_FILE).
Gilles Peskine38d81652018-03-21 08:40:26 +0100327 --random-seed Use a random seed value for randomized tests (default).
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200328 -r|--release-test Run this script in release mode. This fixes the seed value to ${RELEASE_SEED}.
Gilles Peskine709346a2017-12-10 23:43:39 +0100329 -s|--seed Integer seed value to use for this test run.
330
331Tool path options:
332 --armc5-bin-dir=<ARMC5_bin_dir_path> ARM Compiler 5 bin directory.
333 --armc6-bin-dir=<ARMC6_bin_dir_path> ARM Compiler 6 bin directory.
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100334 --clang-earliest=<Clang_earliest_path> Earliest version of clang available
335 --clang-latest=<Clang_latest_path> Latest version of clang available
336 --gcc-earliest=<GCC_earliest_path> Earliest version of GCC available
337 --gcc-latest=<GCC_latest_path> Latest version of GCC available
Gilles Peskine709346a2017-12-10 23:43:39 +0100338 --gnutls-cli=<GnuTLS_cli_path> GnuTLS client executable to use for most tests.
339 --gnutls-serv=<GnuTLS_serv_path> GnuTLS server executable to use for most tests.
Gilles Peskine709346a2017-12-10 23:43:39 +0100340 --openssl=<OpenSSL_path> OpenSSL executable to use for most tests.
Manuel Pégourié-Gonnard6b368922018-02-20 12:02:07 +0100341 --openssl-next=<OpenSSL_path> OpenSSL executable to use for recent things like ARIA
Gilles Peskine709346a2017-12-10 23:43:39 +0100342EOF
SimonB2e23c822016-04-16 21:54:39 +0100343}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100344
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200345# Cleanup before/after running a component.
346# Remove built files as well as the cmake cache/config.
347# Does not remove generated source files.
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100348cleanup()
349{
David Horstmann76a77382023-08-17 17:32:26 +0100350 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100351 command make clean
352 fi
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200353
Gilles Peskine31b07e22018-03-21 12:15:06 +0100354 # Remove CMake artefacts
Jaeden Amero2d0e00f2018-11-07 18:46:41 +0000355 find . -name .git -prune -o \
Gilles Peskine31b07e22018-03-21 12:15:06 +0100356 -iname CMakeFiles -exec rm -rf {} \+ -o \
357 \( -iname cmake_install.cmake -o \
358 -iname CTestTestfile.cmake -o \
Manuel Pégourié-Gonnardbfe54d72021-09-09 11:11:44 +0200359 -iname CMakeCache.txt -o \
360 -path './cmake/*.cmake' \) -exec rm -f {} \+
Ronald Cronda224022024-06-12 10:04:22 +0200361 # Remove Makefiles generated by in-tree CMake builds
Manuel Pégourié-Gonnard86c48382024-10-16 10:23:41 +0200362 rm -f pkgconfig/Makefile framework/Makefile
Ronald Cronda224022024-06-12 10:04:22 +0200363 rm -f include/Makefile programs/!(fuzz)/Makefile
Manuel Pégourié-Gonnard6c0c0f22024-10-16 10:12:00 +0200364 rm -f tf-psa-crypto/Makefile tf-psa-crypto/include/Makefile
365 rm -f tf-psa-crypto/core/Makefile tf-psa-crypto/drivers/Makefile
366 rm -f tf-psa-crypto/tests/Makefile
367 rm -f tf-psa-crypto/drivers/everest/Makefile
368 rm -f tf-psa-crypto/drivers/p256-m/Makefile
369 rm -f tf-psa-crypto/drivers/builtin/Makefile
370 rm -f tf-psa-crypto/drivers/builtin/src/Makefile
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200371
Jaeden Ameroab83fdf2019-06-20 17:38:22 +0100372 # Remove any artifacts from the component_test_cmake_as_subdirectory test.
373 rm -rf programs/test/cmake_subproject/build
374 rm -f programs/test/cmake_subproject/Makefile
375 rm -f programs/test/cmake_subproject/cmake_subproject
376
Chris Kayd259e342021-03-25 16:03:25 +0000377 # Remove any artifacts from the component_test_cmake_as_package test.
378 rm -rf programs/test/cmake_package/build
379 rm -f programs/test/cmake_package/Makefile
380 rm -f programs/test/cmake_package/cmake_package
381
382 # Remove any artifacts from the component_test_cmake_as_installed_package test.
383 rm -rf programs/test/cmake_package_install/build
384 rm -f programs/test/cmake_package_install/Makefile
385 rm -f programs/test/cmake_package_install/cmake_package_install
386
Gilles Peskinef83eb822020-03-30 20:11:39 +0200387 # Restore files that may have been clobbered by the job
Manuel Pégourié-Gonnard6fb63b02024-10-01 12:54:37 +0200388 restore_backed_up_files
389}
390
391# Restore files that may have been clobbered
392restore_backed_up_files () {
Gilles Peskinef83eb822020-03-30 20:11:39 +0200393 for x in $files_to_back_up; do
Gilles Peskinedd06efb2022-08-30 21:02:00 +0200394 if [[ -e "$x$backup_suffix" ]]; then
395 cp -p "$x$backup_suffix" "$x"
396 fi
Gilles Peskinef83eb822020-03-30 20:11:39 +0200397 done
398}
John Durkopbd069d32020-10-31 22:14:03 -0700399
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200400# Final cleanup when this script exits (except when exiting on a failure
401# in non-keep-going mode).
Gilles Peskinef83eb822020-03-30 20:11:39 +0200402final_cleanup () {
403 cleanup
404
405 for x in $files_to_back_up; do
406 rm -f "$x$backup_suffix"
407 done
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100408}
409
Gilles Peskine7c652162017-12-11 00:01:40 +0100410# Executed on exit. May be redefined depending on command line options.
411final_report () {
412 :
413}
414
415fatal_signal () {
Gilles Peskinef83eb822020-03-30 20:11:39 +0200416 final_cleanup
Gilles Peskine7c652162017-12-11 00:01:40 +0100417 final_report $1
418 trap - $1
419 kill -$1 $$
420}
421
422trap 'fatal_signal HUP' HUP
423trap 'fatal_signal INT' INT
424trap 'fatal_signal TERM' TERM
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200425
Gilles Peskine050d2fc2021-09-30 18:24:21 +0200426# Number of processors on this machine. Used as the default setting
427# for parallel make.
428all_sh_nproc ()
429{
430 {
431 nproc || # Linux
432 sysctl -n hw.ncpuonline || # NetBSD, OpenBSD
433 sysctl -n hw.ncpu || # FreeBSD
434 echo 1
435 } 2>/dev/null
436}
437
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100438msg()
439{
Gilles Peskineffcdeff2018-12-04 12:49:28 +0100440 if [ -n "${current_component:-}" ]; then
441 current_section="${current_component#component_}: $1"
442 else
443 current_section="$1"
444 fi
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200445
446 if [ $QUIET -eq 1 ]; then
447 return
448 fi
449
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100450 echo ""
451 echo "******************************************************************"
Gilles Peskineffcdeff2018-12-04 12:49:28 +0100452 echo "* $current_section "
Manuel Pégourié-Gonnard392d3dd2015-01-26 14:03:56 +0000453 printf "* "; date
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100454 echo "******************************************************************"
455}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100456
Andres AGd9eba4b2016-08-26 14:42:14 +0100457err_msg()
458{
459 echo "$1" >&2
460}
461
462check_tools()
463{
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200464 for tool in "$@"; do
465 if ! `type "$tool" >/dev/null 2>&1`; then
466 err_msg "$tool not found!"
Andres AGd9eba4b2016-08-26 14:42:14 +0100467 exit 1
468 fi
469 done
470}
471
Gilles Peskine8f073122018-11-27 15:58:47 +0100472pre_parse_command_line () {
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000473 COMMAND_LINE_COMPONENTS=
Gilles Peskinea28db922019-01-10 00:05:18 +0100474 all_except=0
Gilles Peskine5d996822020-03-28 21:09:21 +0100475 error_test=0
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100476 list_components=0
Gilles Peskine80ddb992021-08-06 11:51:59 +0200477 restore_first=0
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000478 no_armcc=
SimonB2e23c822016-04-16 21:54:39 +0100479
Jaeden Amero9b90f2e2018-11-02 18:34:17 +0000480 # Note that legacy options are ignored instead of being omitted from this
481 # list of options, so invocations that worked with previous version of
482 # all.sh will still run and work properly.
Gilles Peskine8f073122018-11-27 15:58:47 +0100483 while [ $# -gt 0 ]; do
Gilles Peskine55f7c942019-01-09 22:28:21 +0100484 case "$1" in
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200485 --append-outcome) append_outcome=1;;
Gilles Peskine6d061342020-04-30 18:19:32 +0200486 --arm-none-eabi-gcc-prefix) shift; ARM_NONE_EABI_GCC_PREFIX="$1";;
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200487 --arm-linux-gnueabi-gcc-prefix) shift; ARM_LINUX_GNUEABI_GCC_PREFIX="$1";;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000488 --armcc) no_armcc=;;
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100489 --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";;
490 --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100491 --clang-earliest) shift; CLANG_EARLIEST="$1";;
492 --clang-latest) shift; CLANG_LATEST="$1";;
Gilles Peskine5d996822020-03-28 21:09:21 +0100493 --error-test) error_test=$((error_test + 1));;
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000494 --except) all_except=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100495 --force|-f) FORCE=1;;
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100496 --gcc-earliest) shift; GCC_EARLIEST="$1";;
497 --gcc-latest) shift; GCC_LATEST="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100498 --gnutls-cli) shift; GNUTLS_CLI="$1";;
Gilles Peskine7be571a2023-08-27 21:39:21 +0200499 --gnutls-legacy-cli) shift;; # ignored for backward compatibility
500 --gnutls-legacy-serv) shift;; # ignored for backward compatibility
Gilles Peskine55f7c942019-01-09 22:28:21 +0100501 --gnutls-serv) shift; GNUTLS_SERV="$1";;
502 --help|-h) usage; exit;;
503 --keep-going|-k) KEEP_GOING=1;;
Gilles Peskine878cf602019-01-06 20:50:38 +0000504 --list-all-components) printf '%s\n' $ALL_COMPONENTS; exit;;
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100505 --list-components) list_components=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100506 --memory|-m) MEMORY=1;;
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200507 --no-append-outcome) append_outcome=0;;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000508 --no-armcc) no_armcc=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100509 --no-force) FORCE=0;;
510 --no-keep-going) KEEP_GOING=0;;
511 --no-memory) MEMORY=0;;
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200512 --no-quiet) QUIET=0;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100513 --openssl) shift; OPENSSL="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100514 --openssl-next) shift; OPENSSL_NEXT="$1";;
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200515 --outcome-file) shift; MBEDTLS_TEST_OUTCOME_FILE="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100516 --out-of-source-dir) shift; OUT_OF_SOURCE_DIR="$1";;
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200517 --quiet|-q) QUIET=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100518 --random-seed) unset SEED;;
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200519 --release-test|-r) SEED=$RELEASE_SEED;;
Gilles Peskine80ddb992021-08-06 11:51:59 +0200520 --restore) restore_first=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100521 --seed|-s) shift; SEED="$1";;
522 -*)
523 echo >&2 "Unknown option: $1"
524 echo >&2 "Run $0 --help for usage."
525 exit 120
526 ;;
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000527 *) COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS $1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100528 esac
529 shift
Gilles Peskine8f073122018-11-27 15:58:47 +0100530 done
SimonB2e23c822016-04-16 21:54:39 +0100531
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100532 # Exclude components that are not supported on this platform.
533 SUPPORTED_COMPONENTS=
534 for component in $ALL_COMPONENTS; do
535 case $(type "support_$component" 2>&1) in
536 *' function'*)
537 if ! support_$component; then continue; fi;;
538 esac
539 SUPPORTED_COMPONENTS="$SUPPORTED_COMPONENTS $component"
540 done
541
542 if [ $list_components -eq 1 ]; then
543 printf '%s\n' $SUPPORTED_COMPONENTS
544 exit
545 fi
546
Gilles Peskinea28db922019-01-10 00:05:18 +0100547 # With no list of components, run everything.
Gilles Peskine80ddb992021-08-06 11:51:59 +0200548 if [ -z "$COMMAND_LINE_COMPONENTS" ] && [ $restore_first -eq 0 ]; then
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000549 all_except=1
Andres AGdc192212016-08-31 17:33:13 +0100550 fi
551
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000552 # --no-armcc is a legacy option. The modern way is --except '*_armcc*'.
553 # Ignore it if components are listed explicitly on the command line.
Gilles Peskinea28db922019-01-10 00:05:18 +0100554 if [ -n "$no_armcc" ] && [ $all_except -eq 1 ]; then
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000555 COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*"
SimonB2e23c822016-04-16 21:54:39 +0100556 fi
SimonB2e23c822016-04-16 21:54:39 +0100557
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200558 # Error out if an explicitly requested component doesn't exist.
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100559 if [ $all_except -eq 0 ]; then
560 unsupported=0
Gilles Peskine03af6782021-08-06 11:35:17 +0200561 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
562 # only does word splitting.
Gilles Peskine1d475b62021-08-03 13:43:36 +0200563 set -f
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100564 for component in $COMMAND_LINE_COMPONENTS; do
Gilles Peskine1d475b62021-08-03 13:43:36 +0200565 set +f
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200566 # If the requested name includes a wildcard character, don't
567 # check it. Accept wildcard patterns that don't match anything.
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100568 case $component in
569 *[*?\[]*) continue;;
570 esac
571 case " $SUPPORTED_COMPONENTS " in
572 *" $component "*) :;;
573 *)
574 echo >&2 "Component $component was explicitly requested, but is not known or not supported."
575 unsupported=$((unsupported + 1));;
576 esac
577 done
Gilles Peskine1d475b62021-08-03 13:43:36 +0200578 set +f
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100579 if [ $unsupported -ne 0 ]; then
580 exit 2
581 fi
582 fi
583
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000584 # Build the list of components to run.
Gilles Peskinea28db922019-01-10 00:05:18 +0100585 RUN_COMPONENTS=
586 for component in $SUPPORTED_COMPONENTS; do
587 if is_component_included "$component"; [ $? -eq $all_except ]; then
588 RUN_COMPONENTS="$RUN_COMPONENTS $component"
589 fi
590 done
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000591
592 unset all_except
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000593 unset no_armcc
Gilles Peskine8f073122018-11-27 15:58:47 +0100594}
SimonB2e23c822016-04-16 21:54:39 +0100595
Gilles Peskine8f073122018-11-27 15:58:47 +0100596pre_check_git () {
597 if [ $FORCE -eq 1 ]; then
Gilles Peskine53190e62019-01-09 23:17:35 +0100598 rm -rf "$OUT_OF_SOURCE_DIR"
Gilles Peskine8f073122018-11-27 15:58:47 +0100599 git checkout-index -f -q $CONFIG_H
600 cleanup
601 else
SimonB2e23c822016-04-16 21:54:39 +0100602
Gilles Peskine8f073122018-11-27 15:58:47 +0100603 if [ -d "$OUT_OF_SOURCE_DIR" ]; then
604 echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
605 echo "You can either delete this directory manually, or force the test by rerunning"
606 echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
607 exit 1
608 fi
609
Ronald Cron7a93ac52023-07-20 09:49:12 +0200610 if ! git diff --quiet "$CONFIG_H"; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100611 err_msg "Warning - the configuration file '$CONFIG_H' has been edited. "
Gilles Peskine8f073122018-11-27 15:58:47 +0100612 echo "You can either delete or preserve your work, or force the test by rerunning the"
613 echo "script as: $0 --force"
614 exit 1
615 fi
SimonB2e23c822016-04-16 21:54:39 +0100616 fi
Andrzej Kurekeb508712019-02-14 07:18:59 -0500617}
618
Gilles Peskine568f53a2021-07-12 18:16:01 +0200619pre_restore_files () {
620 # If the makefiles have been generated by a framework such as cmake,
621 # restore them from git. If the makefiles look like modifications from
622 # the ones checked into git, take care not to modify them. Whatever
623 # this function leaves behind is what the script will restore before
624 # each component.
625 case "$(head -n1 Makefile)" in
626 *[Gg]enerated*)
627 git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
628 git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
629 ;;
630 esac
631}
632
Gilles Peskinef83eb822020-03-30 20:11:39 +0200633pre_back_up () {
634 for x in $files_to_back_up; do
635 cp -p "$x" "$x$backup_suffix"
636 done
637}
638
Gilles Peskine8f073122018-11-27 15:58:47 +0100639pre_setup_keep_going () {
Gilles Peskinece266c42020-03-28 18:50:43 +0100640 failure_count=0 # Number of failed components
641 last_failure_status=0 # Last failure status in this component
642
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100643 # See err_trap
644 previous_failure_status=0
645 previous_failed_command=
646 previous_failure_funcall_depth=0
Gilles Peskinea681c592020-03-28 21:27:40 +0100647 unset report_failed_command
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100648
Gilles Peskine7c652162017-12-11 00:01:40 +0100649 start_red=
650 end_color=
651 if [ -t 1 ]; then
Gilles Peskine9736b9d2018-01-02 21:54:17 +0100652 case "${TERM:-}" in
Gilles Peskine7c652162017-12-11 00:01:40 +0100653 *color*|cygwin|linux|rxvt*|screen|[Eex]term*)
654 start_red=$(printf '\033[31m')
655 end_color=$(printf '\033[0m')
656 ;;
657 esac
658 fi
Gilles Peskinece266c42020-03-28 18:50:43 +0100659
660 # Keep a summary of failures in a file. We'll print it out at the end.
661 failure_summary_file=$PWD/all-sh-failures-$$.log
662 : >"$failure_summary_file"
663
664 # Whether it makes sense to keep a component going after the specified
665 # command fails (test command) or not (configure or build).
Gilles Peskine03af6782021-08-06 11:35:17 +0200666 # This function normally receives the failing simple command
667 # ($BASH_COMMAND) as an argument, but if $report_failed_command is set,
668 # this is passed instead.
Gilles Peskinece266c42020-03-28 18:50:43 +0100669 # This doesn't have to be 100% accurate: all failures are recorded anyway.
Gilles Peskinec111e242021-08-02 23:29:53 +0200670 # False positives result in running things that can't be expected to
671 # work. False negatives result in things not running after something else
672 # failed even though they might have given useful feedback.
Gilles Peskinece266c42020-03-28 18:50:43 +0100673 can_keep_going_after_failure () {
674 case "$1" in
675 "msg "*) false;;
Gilles Peskinec111e242021-08-02 23:29:53 +0200676 "cd "*) false;;
Manuel Pégourié-Gonnarda7423372023-11-10 09:58:31 +0100677 "diff "*) true;;
Gilles Peskinec111e242021-08-02 23:29:53 +0200678 *make*[\ /]tests*) false;; # make tests, make CFLAGS=-I../tests, ...
679 *test*) true;; # make test, tests/stuff, env V=v tests/stuff, ...
680 *make*check*) true;;
681 "grep "*) true;;
682 "[ "*) true;;
683 "! "*) true;;
Gilles Peskinece266c42020-03-28 18:50:43 +0100684 *) false;;
Gilles Peskine7c652162017-12-11 00:01:40 +0100685 esac
686 }
Gilles Peskinece266c42020-03-28 18:50:43 +0100687
688 # This function runs if there is any error in a component.
689 # It must either exit with a nonzero status, or set
690 # last_failure_status to a nonzero value.
691 err_trap () {
692 # Save $? (status of the failing command). This must be the very
693 # first thing, before $? is overridden.
694 last_failure_status=$?
Gilles Peskinea681c592020-03-28 21:27:40 +0100695 failed_command=${report_failed_command-$BASH_COMMAND}
Gilles Peskinece266c42020-03-28 18:50:43 +0100696
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100697 if [[ $last_failure_status -eq $previous_failure_status &&
698 "$failed_command" == "$previous_failed_command" &&
699 ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]]
700 then
701 # The same command failed twice in a row, but this time one level
702 # less deep in the function call stack. This happens when the last
703 # command of a function returns a nonzero status, and the function
704 # returns that same status. Ignore the second failure.
705 previous_failure_funcall_depth=${#FUNCNAME[@]}
706 return
707 fi
708 previous_failure_status=$last_failure_status
709 previous_failed_command=$failed_command
710 previous_failure_funcall_depth=${#FUNCNAME[@]}
711
Gilles Peskinece266c42020-03-28 18:50:43 +0100712 text="$current_section: $failed_command -> $last_failure_status"
713 echo "${start_red}^^^^$text^^^^${end_color}" >&2
714 echo "$text" >>"$failure_summary_file"
715
716 # If the command is fatal (configure or build command), stop this
717 # component. Otherwise (test command) keep the component running
718 # (run more tests from the same build).
719 if ! can_keep_going_after_failure "$failed_command"; then
720 exit $last_failure_status
721 fi
722 }
723
Gilles Peskine7c652162017-12-11 00:01:40 +0100724 final_report () {
725 if [ $failure_count -gt 0 ]; then
726 echo
727 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
Gilles Peskinece266c42020-03-28 18:50:43 +0100728 echo "${start_red}FAILED: $failure_count components${end_color}"
729 cat "$failure_summary_file"
Gilles Peskine7c652162017-12-11 00:01:40 +0100730 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
731 elif [ -z "${1-}" ]; then
732 echo "SUCCESS :)"
733 fi
734 if [ -n "${1-}" ]; then
735 echo "Killed by SIG$1."
736 fi
Gilles Peskinece266c42020-03-28 18:50:43 +0100737 rm -f "$failure_summary_file"
738 if [ $failure_count -gt 0 ]; then
739 exit 1
740 fi
Gilles Peskine7c652162017-12-11 00:01:40 +0100741 }
Gilles Peskine8f073122018-11-27 15:58:47 +0100742}
743
Gilles Peskinea681c592020-03-28 21:27:40 +0100744# '! true' does not trigger the ERR trap. Arrange to trigger it, with
745# a reasonably informative error message (not just "$@").
746not () {
747 if "$@"; then
748 report_failed_command="! $*"
749 false
750 unset report_failed_command
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200751 fi
752}
753
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200754pre_prepare_outcome_file () {
755 case "$MBEDTLS_TEST_OUTCOME_FILE" in
756 [!/]*) MBEDTLS_TEST_OUTCOME_FILE="$PWD/$MBEDTLS_TEST_OUTCOME_FILE";;
757 esac
758 if [ -n "$MBEDTLS_TEST_OUTCOME_FILE" ] && [ "$append_outcome" -eq 0 ]; then
759 rm -f "$MBEDTLS_TEST_OUTCOME_FILE"
760 fi
761}
762
Gilles Peskine8f073122018-11-27 15:58:47 +0100763pre_print_configuration () {
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200764 if [ $QUIET -eq 1 ]; then
765 return
766 fi
767
Gilles Peskine8f073122018-11-27 15:58:47 +0100768 msg "info: $0 configuration"
769 echo "MEMORY: $MEMORY"
770 echo "FORCE: $FORCE"
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200771 echo "MBEDTLS_TEST_OUTCOME_FILE: ${MBEDTLS_TEST_OUTCOME_FILE:-(none)}"
Gilles Peskine8f073122018-11-27 15:58:47 +0100772 echo "SEED: ${SEED-"UNSET"}"
Gilles Peskine636c26a2020-03-04 20:46:15 +0100773 echo
Gilles Peskine8f073122018-11-27 15:58:47 +0100774 echo "OPENSSL: $OPENSSL"
Gilles Peskine8f073122018-11-27 15:58:47 +0100775 echo "OPENSSL_NEXT: $OPENSSL_NEXT"
776 echo "GNUTLS_CLI: $GNUTLS_CLI"
777 echo "GNUTLS_SERV: $GNUTLS_SERV"
Gilles Peskine8f073122018-11-27 15:58:47 +0100778 echo "ARMC5_BIN_DIR: $ARMC5_BIN_DIR"
779 echo "ARMC6_BIN_DIR: $ARMC6_BIN_DIR"
780}
Andres AG7770ea82016-10-10 15:46:20 +0100781
Andres AGd9eba4b2016-08-26 14:42:14 +0100782# Make sure the tools we need are available.
Gilles Peskine8f073122018-11-27 15:58:47 +0100783pre_check_tools () {
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000784 # Build the list of variables to pass to output_env.sh.
785 set env
SimonB2e23c822016-04-16 21:54:39 +0100786
Gilles Peskine87964262019-01-06 22:40:00 +0000787 case " $RUN_COMPONENTS " in
788 # Require OpenSSL and GnuTLS if running any tests (as opposed to
789 # only doing builds). Not all tests run OpenSSL and GnuTLS, but this
790 # is a good enough approximation in practice.
Bence Szépkútibbb5af92023-12-15 20:58:15 +0100791 *" test_"* | *" release_test_"*)
Gilles Peskine87964262019-01-06 22:40:00 +0000792 # To avoid setting OpenSSL and GnuTLS for each call to compat.sh
793 # and ssl-opt.sh, we just export the variables they require.
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +0100794 export OPENSSL="$OPENSSL"
Gilles Peskine87964262019-01-06 22:40:00 +0000795 export GNUTLS_CLI="$GNUTLS_CLI"
796 export GNUTLS_SERV="$GNUTLS_SERV"
797 # Avoid passing --seed flag in every call to ssl-opt.sh
798 if [ -n "${SEED-}" ]; then
799 export SEED
800 fi
Gilles Peskine7be571a2023-08-27 21:39:21 +0200801 set "$@" OPENSSL="$OPENSSL"
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000802 set "$@" GNUTLS_CLI="$GNUTLS_CLI" GNUTLS_SERV="$GNUTLS_SERV"
Gilles Peskine7be571a2023-08-27 21:39:21 +0200803 check_tools "$OPENSSL" "$OPENSSL_NEXT" \
804 "$GNUTLS_CLI" "$GNUTLS_SERV"
Gilles Peskine87964262019-01-06 22:40:00 +0000805 ;;
806 esac
Manuel Pégourié-Gonnard57255b12014-06-09 11:21:49 +0200807
Gilles Peskine87964262019-01-06 22:40:00 +0000808 case " $RUN_COMPONENTS " in
809 *_doxygen[_\ ]*) check_tools "doxygen" "dot";;
810 esac
Andres AGd9eba4b2016-08-26 14:42:14 +0100811
Gilles Peskine87964262019-01-06 22:40:00 +0000812 case " $RUN_COMPONENTS " in
Gilles Peskine6d061342020-04-30 18:19:32 +0200813 *_arm_none_eabi_gcc[_\ ]*) check_tools "${ARM_NONE_EABI_GCC_PREFIX}gcc";;
Gilles Peskine87964262019-01-06 22:40:00 +0000814 esac
Andres AGd9eba4b2016-08-26 14:42:14 +0100815
Gilles Peskine87964262019-01-06 22:40:00 +0000816 case " $RUN_COMPONENTS " in
817 *_mingw[_\ ]*) check_tools "i686-w64-mingw32-gcc";;
818 esac
819
820 case " $RUN_COMPONENTS " in
821 *" test_zeroize "*) check_tools "gdb";;
822 esac
823
824 case " $RUN_COMPONENTS " in
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000825 *_armcc*)
Gilles Peskine87964262019-01-06 22:40:00 +0000826 ARMC5_CC="$ARMC5_BIN_DIR/armcc"
827 ARMC5_AR="$ARMC5_BIN_DIR/armar"
Gilles Peskine18487f62020-04-30 23:11:54 +0200828 ARMC5_FROMELF="$ARMC5_BIN_DIR/fromelf"
Gilles Peskine87964262019-01-06 22:40:00 +0000829 ARMC6_CC="$ARMC6_BIN_DIR/armclang"
830 ARMC6_AR="$ARMC6_BIN_DIR/armar"
Gilles Peskine18487f62020-04-30 23:11:54 +0200831 ARMC6_FROMELF="$ARMC6_BIN_DIR/fromelf"
832 check_tools "$ARMC5_CC" "$ARMC5_AR" "$ARMC5_FROMELF" \
833 "$ARMC6_CC" "$ARMC6_AR" "$ARMC6_FROMELF";;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000834 esac
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000835
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200836 # past this point, no call to check_tool, only printing output
837 if [ $QUIET -eq 1 ]; then
838 return
839 fi
840
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000841 msg "info: output_env.sh"
842 case $RUN_COMPONENTS in
843 *_armcc*)
844 set "$@" ARMC5_CC="$ARMC5_CC" ARMC6_CC="$ARMC6_CC" RUN_ARMCC=1;;
845 *) set "$@" RUN_ARMCC=0;;
846 esac
847 "$@" scripts/output_env.sh
Gilles Peskine8f073122018-11-27 15:58:47 +0100848}
Manuel Pégourié-Gonnard9bda9b32014-11-20 13:10:22 +0100849
Manuel Pégourié-Gonnardc9741342024-10-16 10:06:15 +0200850pre_generate_files() {
Manuel Pégourié-Gonnard87db8a22021-06-18 13:30:14 +0200851 # since make doesn't have proper dependencies, remove any possibly outdate
852 # file that might be around before generating fresh ones
853 make neat
Gilles Peskine72385032021-07-08 19:07:07 +0200854 if [ $QUIET -eq 1 ]; then
Gilles Peskine75301632021-08-05 15:10:47 +0200855 make generated_files >/dev/null
Gilles Peskine72385032021-07-08 19:07:07 +0200856 else
857 make generated_files
858 fi
Gilles Peskine1570b592021-04-22 01:10:12 +0200859}
860
Manuel Pégourié-Gonnard36ae06b2024-10-01 13:19:04 +0200861pre_load_helpers () {
862 # The path is going to change when this is moved to the framework
863 test_script_dir="${0%/*}"
864 source "$test_script_dir"/all-helpers.sh
Manuel Pégourié-Gonnard27e17672023-05-25 09:39:08 +0200865}
Gilles Peskine192c72f2017-12-21 15:59:21 +0100866
Gilles Peskine192c72f2017-12-21 15:59:21 +0100867################################################################
868#### Termination
869################################################################
870
Gilles Peskine8f073122018-11-27 15:58:47 +0100871post_report () {
872 msg "Done, cleaning up"
Gilles Peskinef83eb822020-03-30 20:11:39 +0200873 final_cleanup
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100874
Gilles Peskine8f073122018-11-27 15:58:47 +0100875 final_report
876}
877
Gilles Peskine8f073122018-11-27 15:58:47 +0100878################################################################
879#### Run all the things
880################################################################
881
Gilles Peskine5d996822020-03-28 21:09:21 +0100882# Function invoked by --error-test to test error reporting.
883pseudo_component_error_test () {
Gilles Peskine88a7c2b2021-08-02 23:28:00 +0200884 msg "Testing error reporting $error_test_i"
Gilles Peskine5d996822020-03-28 21:09:21 +0100885 if [ $KEEP_GOING -ne 0 ]; then
886 echo "Expect three failing commands."
887 fi
Gilles Peskine88a7c2b2021-08-02 23:28:00 +0200888 # If the component doesn't run in a subshell, changing error_test_i to an
889 # invalid integer will cause an error in the loop that runs this function.
890 error_test_i=this_should_not_be_used_since_the_component_runs_in_a_subshell
Gilles Peskineec135542021-08-02 23:14:03 +0200891 # Expected error: 'grep non_existent /dev/null -> 1'
Gilles Peskine5d996822020-03-28 21:09:21 +0100892 grep non_existent /dev/null
Gilles Peskineec135542021-08-02 23:14:03 +0200893 # Expected error: '! grep -q . tests/scripts/all.sh -> 1'
Gilles Peskine5d996822020-03-28 21:09:21 +0100894 not grep -q . "$0"
Gilles Peskineec135542021-08-02 23:14:03 +0200895 # Expected error: 'make unknown_target -> 2'
Gilles Peskine5d996822020-03-28 21:09:21 +0100896 make unknown_target
897 false "this should not be executed"
898}
899
Gilles Peskinee48351a2018-11-27 16:06:30 +0100900# Run one component and clean up afterwards.
Gilles Peskine8f073122018-11-27 15:58:47 +0100901run_component () {
Gilles Peskineffcdeff2018-12-04 12:49:28 +0100902 current_component="$1"
Gilles Peskine9004a172019-09-16 15:20:36 +0200903 export MBEDTLS_TEST_CONFIGURATION="$current_component"
Gilles Peskine2ef377d2019-10-07 18:44:21 +0200904
905 # Unconditionally create a seedfile that's sufficiently long.
906 # Do this before each component, because a previous component may
907 # have messed it up or shortened it.
Gilles Peskine88a07452021-07-08 19:03:50 +0200908 local dd_cmd
909 dd_cmd=(dd if=/dev/urandom of=./tests/seedfile bs=64 count=1)
910 case $OSTYPE in
Tom Cosgrove226aca12022-03-16 14:11:07 +0000911 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
Gilles Peskine88a07452021-07-08 19:03:50 +0200912 esac
913 "${dd_cmd[@]}"
Gilles Peskine2ef377d2019-10-07 18:44:21 +0200914
Manuel Pégourié-Gonnard6c0c0f22024-10-16 10:12:00 +0200915 if [ -d tf-psa-crypto ]; then
916 dd_cmd=(dd if=/dev/urandom of=./tf-psa-crypto/tests/seedfile bs=64 count=1)
917 case $OSTYPE in
918 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
919 esac
920 "${dd_cmd[@]}"
921 fi
922
Gilles Peskineec135542021-08-02 23:14:03 +0200923 # Run the component in a subshell, with error trapping and output
924 # redirection set up based on the relevant options.
Gilles Peskinece266c42020-03-28 18:50:43 +0100925 if [ $KEEP_GOING -eq 1 ]; then
Gilles Peskineec135542021-08-02 23:14:03 +0200926 # We want to keep running if the subshell fails, so 'set -e' must
927 # be off when the subshell runs.
Gilles Peskinece266c42020-03-28 18:50:43 +0100928 set +e
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200929 fi
Gilles Peskinece266c42020-03-28 18:50:43 +0100930 (
931 if [ $QUIET -eq 1 ]; then
932 # msg() will be silenced, so just print the component name here.
933 echo "${current_component#component_}"
934 exec >/dev/null
935 fi
936 if [ $KEEP_GOING -eq 1 ]; then
937 # Keep "set -e" off, and run an ERR trap instead to record failures.
938 set -E
939 trap err_trap ERR
940 fi
941 # The next line is what runs the component
942 "$@"
943 if [ $KEEP_GOING -eq 1 ]; then
944 trap - ERR
945 exit $last_failure_status
946 fi
947 )
948 component_status=$?
949 if [ $KEEP_GOING -eq 1 ]; then
950 set -e
951 if [ $component_status -ne 0 ]; then
952 failure_count=$((failure_count + 1))
953 fi
954 fi
Gilles Peskine2ef377d2019-10-07 18:44:21 +0200955
956 # Restore the build tree to a clean state.
Gilles Peskinee48351a2018-11-27 16:06:30 +0100957 cleanup
Manuel Pégourié-Gonnard304b0992020-06-08 10:59:41 +0200958 unset current_component
Gilles Peskine8f073122018-11-27 15:58:47 +0100959}
960
Manuel Pégourié-Gonnard2c3a6242024-10-01 11:54:34 +0200961################################################################
Manuel Pégourié-Gonnard29692802024-10-03 12:52:05 +0200962#### Main
Manuel Pégourié-Gonnard2c3a6242024-10-01 11:54:34 +0200963################################################################
964
Manuel Pégourié-Gonnard29692802024-10-03 12:52:05 +0200965main () {
966 # Preliminary setup
967 pre_set_shell_options
968 pre_check_environment
969 pre_load_helpers
970 pre_load_components
971 pre_initialize_variables
972 pre_parse_command_line "$@"
Manuel Pégourié-Gonnard2c3a6242024-10-01 11:54:34 +0200973
Manuel Pégourié-Gonnard29692802024-10-03 12:52:05 +0200974 setup_quiet_wrappers
975 pre_check_git
976 pre_restore_files
977 pre_back_up
Gilles Peskine348fb9a2018-11-27 17:04:29 +0100978
Manuel Pégourié-Gonnard29692802024-10-03 12:52:05 +0200979 build_status=0
980 if [ $KEEP_GOING -eq 1 ]; then
981 pre_setup_keep_going
982 fi
983 pre_prepare_outcome_file
984 pre_print_configuration
985 pre_check_tools
986 cleanup
987 if in_mbedtls_repo; then
988 pre_generate_files
989 fi
Andrzej Kurekeb508712019-02-14 07:18:59 -0500990
Manuel Pégourié-Gonnard29692802024-10-03 12:52:05 +0200991 # Run the requested tests.
992 for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do
993 run_component pseudo_component_error_test
994 done
995 unset error_test_i
996 for component in $RUN_COMPONENTS; do
997 run_component "component_$component"
998 done
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100999
Manuel Pégourié-Gonnard29692802024-10-03 12:52:05 +02001000 # We're done.
1001 post_report
1002}
Gilles Peskine8f073122018-11-27 15:58:47 +01001003
Manuel Pégourié-Gonnard29692802024-10-03 12:52:05 +02001004main "$@"