blob: ed097df79e37c39f6e295c18f369c860afa4fc35 [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
Gilles Peskine36647802020-03-28 18:50:49 +0100111# Abort on errors (even on the left-hand side of a pipe).
112# Treat uninitialised variables as errors.
113set -e -o pipefail -u
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100114
Gilles Peskine076f7252022-08-30 21:02:44 +0200115# Enable ksh/bash extended file matching patterns
116shopt -s extglob
117
David Horstmann76a77382023-08-17 17:32:26 +0100118in_mbedtls_repo () {
David Horstmannd02b5f82023-08-29 09:53:52 +0100119 test -d include -a -d library -a -d programs -a -d tests
120}
121
Ronald Cron070e8652023-10-09 10:25:45 +0200122in_tf_psa_crypto_repo () {
David Horstmannd02b5f82023-08-29 09:53:52 +0100123 test -d include -a -d core -a -d drivers -a -d programs -a -d tests
David Horstmann9a6c45b2023-07-14 12:30:00 +0100124}
125
Gilles Peskine8f073122018-11-27 15:58:47 +0100126pre_check_environment () {
Ronald Cron070e8652023-10-09 10:25:45 +0200127 if in_mbedtls_repo || in_tf_psa_crypto_repo; then :; else
128 echo "Must be run from Mbed TLS / TF-PSA-Crypto root" >&2
Gilles Peskine8f073122018-11-27 15:58:47 +0100129 exit 1
130 fi
131}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100132
Gilles Peskine8f073122018-11-27 15:58:47 +0100133pre_initialize_variables () {
David Horstmann76a77382023-08-17 17:32:26 +0100134 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100135 CONFIG_H='include/mbedtls/mbedtls_config.h'
David Horstmann76a77382023-08-17 17:32:26 +0100136 else
137 CONFIG_H='drivers/builtin/include/mbedtls/mbedtls_config.h'
David Horstmann9a6c45b2023-07-14 12:30:00 +0100138 fi
John Durkopbd069d32020-10-31 22:14:03 -0700139 CRYPTO_CONFIG_H='include/psa/crypto_config.h'
Manuel Pégourié-Gonnard462e3a92022-12-27 12:35:11 +0100140 CONFIG_TEST_DRIVER_H='tests/include/test/drivers/config_test_driver.h'
Gilles Peskinef83eb822020-03-30 20:11:39 +0200141
142 # Files that are clobbered by some jobs will be backed up. Use a different
143 # suffix from auxiliary scripts so that all.sh and auxiliary scripts can
144 # independently decide when to remove the backup file.
145 backup_suffix='.all.bak'
146 # Files clobbered by config.py
Janos Follath2f045822023-07-31 10:57:16 +0100147 files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H $CONFIG_TEST_DRIVER_H"
David Horstmann76a77382023-08-17 17:32:26 +0100148 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100149 # Files clobbered by in-tree cmake
150 files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile"
151 fi
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200152
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200153 append_outcome=0
Gilles Peskine8f073122018-11-27 15:58:47 +0100154 MEMORY=0
155 FORCE=0
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200156 QUIET=0
Gilles Peskine8f073122018-11-27 15:58:47 +0100157 KEEP_GOING=0
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100158
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200159 # Seed value used with the --release-test option.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +0200160 #
161 # See also RELEASE_SEED in basic-build-test.sh. Debugging is easier if
162 # both values are kept in sync. If you change the value here because it
163 # breaks some tests, you'll definitely want to change it in
164 # basic-build-test.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200165 RELEASE_SEED=1
166
Yanray Wang18040ed2023-11-23 21:29:56 +0800167 # Specify character collation for regular expressions and sorting with C locale
168 export LC_COLLATE=C
169
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200170 : ${MBEDTLS_TEST_OUTCOME_FILE=}
Gilles Peskine9004a172019-09-16 15:20:36 +0200171 : ${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 +0200172 export MBEDTLS_TEST_OUTCOME_FILE
Gilles Peskine9004a172019-09-16 15:20:36 +0200173 export MBEDTLS_TEST_PLATFORM
174
Jaeden Ameroc4cc2512019-01-30 15:35:44 +0000175 # Default commands, can be overridden by the environment
Gilles Peskine8f073122018-11-27 15:58:47 +0100176 : ${OPENSSL:="openssl"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100177 : ${OPENSSL_NEXT:="$OPENSSL"}
178 : ${GNUTLS_CLI:="gnutls-cli"}
179 : ${GNUTLS_SERV:="gnutls-serv"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100180 : ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
181 : ${ARMC5_BIN_DIR:=/usr/bin}
182 : ${ARMC6_BIN_DIR:=/usr/bin}
Gilles Peskine6d061342020-04-30 18:19:32 +0200183 : ${ARM_NONE_EABI_GCC_PREFIX:=arm-none-eabi-}
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200184 : ${ARM_LINUX_GNUEABI_GCC_PREFIX:=arm-linux-gnueabi-}
Gowtham Suresh Kumar9da40b82023-07-31 16:38:10 +0100185 : ${CLANG_LATEST:="clang-latest"}
186 : ${CLANG_EARLIEST:="clang-earliest"}
187 : ${GCC_LATEST:="gcc-latest"}
188 : ${GCC_EARLIEST:="gcc-earliest"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100189 # if MAKEFLAGS is not set add the -j option to speed up invocations of make
Gilles Peskinea1fc4b52019-01-06 20:15:26 +0000190 if [ -z "${MAKEFLAGS+set}" ]; then
Gilles Peskine050d2fc2021-09-30 18:24:21 +0200191 export MAKEFLAGS="-j$(all_sh_nproc)"
Gilles Peskine8f073122018-11-27 15:58:47 +0100192 fi
Dave Rodgman0c5bfe82023-12-18 19:53:25 +0000193 # if CC is not set, use clang by default (if present) to improve build times
194 if [ -z "${CC+set}" ] && (type clang > /dev/null 2>&1); then
Dave Rodgmanfc5b9552023-12-19 16:08:19 +0000195 export CC="clang"
Dave Rodgman66cbc832023-12-18 18:34:50 +0000196 fi
Gilles Peskine878cf602019-01-06 20:50:38 +0000197
Ronald Cron561181c2024-04-02 14:39:53 +0200198 if [ -n "${OPENSSL_3+set}" ]; then
199 export OPENSSL_NEXT="$OPENSSL_3"
200 fi
201
Gilles Peskine396853a2021-09-20 18:57:55 +0200202 # Include more verbose output for failing tests run by CMake or make
Jaeden Amerod48e9c72019-02-07 17:43:39 +0000203 export CTEST_OUTPUT_ON_FAILURE=1
204
Gilles Peskine8fd59422019-10-21 17:11:33 +0200205 # CFLAGS and LDFLAGS for Asan builds that don't use CMake
Manuel Pégourié-Gonnardfe549a72022-11-30 10:42:03 +0100206 # default to -O2, use -Ox _after_ this if you want another level
Przemek Stekiel6efa6082023-05-31 09:38:21 +0200207 ASAN_CFLAGS='-O2 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
Dave Rodgman17127e92023-12-14 16:42:48 +0000208 # Normally, tests should use this compiler for ASAN testing
209 ASAN_CC=clang
Gilles Peskine8fd59422019-10-21 17:11:33 +0200210
Andrzej Kurek04bfe572023-06-27 10:02:09 -0400211 # Platform tests have an allocation that returns null
212 export ASAN_OPTIONS="allocator_may_return_null=1"
Andrzej Kurek026235c2023-07-05 08:32:43 -0400213 export MSAN_OPTIONS="allocator_may_return_null=1"
Andrzej Kurek04bfe572023-06-27 10:02:09 -0400214
Gilles Peskine878cf602019-01-06 20:50:38 +0000215 # Gather the list of available components. These are the functions
216 # defined in this script whose name starts with "component_".
Gowtham Suresh Kumar1e829a42023-07-28 16:41:21 +0100217 ALL_COMPONENTS=$(compgen -A function component_ | sed 's/component_//')
Gilles Peskine878cf602019-01-06 20:50:38 +0000218
Dave Rodgman5f8e2a22024-01-16 17:33:34 +0000219 # Delay determining SUPPORTED_COMPONENTS until the command line options have a chance to override
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100220 # the commands set by the environment
Gilles Peskine8f073122018-11-27 15:58:47 +0100221}
Andres AG38495a32016-07-12 16:54:33 +0100222
Dave Rodgman21900632024-02-26 11:41:19 +0000223setup_quiet_wrappers()
224{
225 # Pick up "quiet" wrappers for make and cmake, which don't output very much
226 # unless there is an error. This reduces logging overhead in the CI.
227 #
228 # Note that the cmake wrapper breaks unless we use an absolute path here.
Dave Rodgmana9e8dbe2024-02-26 17:27:18 +0000229 if [[ -e ${PWD}/tests/scripts/quiet ]]; then
230 export PATH=${PWD}/tests/scripts/quiet:$PATH
Dave Rodgman21900632024-02-26 11:41:19 +0000231 fi
232}
233
Gilles Peskinea28db922019-01-10 00:05:18 +0100234# Test whether the component $1 is included in the command line patterns.
235is_component_included()
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100236{
Gilles Peskine03af6782021-08-06 11:35:17 +0200237 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
238 # only does word splitting.
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100239 set -f
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000240 for pattern in $COMMAND_LINE_COMPONENTS; do
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100241 set +f
242 case ${1#component_} in $pattern) return 0;; esac
243 done
244 set +f
245 return 1
246}
Andres AG7770ea82016-10-10 15:46:20 +0100247
Simon Butcher41eeccf2016-09-07 00:07:09 +0100248usage()
Andres AGd9eba4b2016-08-26 14:42:14 +0100249{
Gilles Peskine709346a2017-12-10 23:43:39 +0100250 cat <<EOF
Gilles Peskine92525112018-11-27 18:15:35 +0100251Usage: $0 [OPTION]... [COMPONENT]...
Gilles Peskine348fb9a2018-11-27 17:04:29 +0100252Run mbedtls release validation tests.
Gilles Peskine92525112018-11-27 18:15:35 +0100253By default, run all tests. With one or more COMPONENT, run only those.
Gilles Peskinea28db922019-01-10 00:05:18 +0100254COMPONENT can be the name of a component or a shell wildcard pattern.
255
256Examples:
257 $0 "check_*"
258 Run all sanity checks.
259 $0 --no-armcc --except test_memsan
260 Run everything except builds that require armcc and MemSan.
Gilles Peskine348fb9a2018-11-27 17:04:29 +0100261
262Special options:
263 -h|--help Print this help and exit.
Gilles Peskine878cf602019-01-06 20:50:38 +0000264 --list-all-components List all available test components and exit.
265 --list-components List components supported on this platform and exit.
Gilles Peskine709346a2017-12-10 23:43:39 +0100266
267General options:
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200268 -q|--quiet Only output component names, and errors if any.
Gilles Peskine709346a2017-12-10 23:43:39 +0100269 -f|--force Force the tests to overwrite any modified files.
Gilles Peskine7c652162017-12-11 00:01:40 +0100270 -k|--keep-going Run all tests and report errors at the end.
Gilles Peskine709346a2017-12-10 23:43:39 +0100271 -m|--memory Additional optional memory tests.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200272 --append-outcome Append to the outcome file (if used).
Gilles Peskine6d061342020-04-30 18:19:32 +0200273 --arm-none-eabi-gcc-prefix=<string>
274 Prefix for a cross-compiler for arm-none-eabi
275 (default: "${ARM_NONE_EABI_GCC_PREFIX}")
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200276 --arm-linux-gnueabi-gcc-prefix=<string>
277 Prefix for a cross-compiler for arm-linux-gnueabi
278 (default: "${ARM_LINUX_GNUEABI_GCC_PREFIX}")
Gilles Peskinebca6ab92017-12-19 18:24:31 +0100279 --armcc Run ARM Compiler builds (on by default).
Gilles Peskine80ddb992021-08-06 11:51:59 +0200280 --restore First clean up the build tree, restoring backed up
281 files. Do not run any components unless they are
282 explicitly specified.
Gilles Peskine5d996822020-03-28 21:09:21 +0100283 --error-test Error test mode: run a failing function in addition
Gilles Peskine86f61292021-08-05 15:11:33 +0200284 to any specified component. May be repeated.
Gilles Peskinea28db922019-01-10 00:05:18 +0100285 --except Exclude the COMPONENTs listed on the command line,
286 instead of running only those.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200287 --no-append-outcome Write a new outcome file and analyze it (default).
Gilles Peskinebca6ab92017-12-19 18:24:31 +0100288 --no-armcc Skip ARM Compiler builds.
Gilles Peskine38d81652018-03-21 08:40:26 +0100289 --no-force Refuse to overwrite modified files (default).
290 --no-keep-going Stop at the first error (default).
291 --no-memory No additional memory tests (default).
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800292 --no-quiet Print full output from components.
Gilles Peskine709346a2017-12-10 23:43:39 +0100293 --out-of-source-dir=<path> Directory used for CMake out-of-source build tests.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200294 --outcome-file=<path> File where test outcomes are written (not done if
295 empty; default: \$MBEDTLS_TEST_OUTCOME_FILE).
Gilles Peskine38d81652018-03-21 08:40:26 +0100296 --random-seed Use a random seed value for randomized tests (default).
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200297 -r|--release-test Run this script in release mode. This fixes the seed value to ${RELEASE_SEED}.
Gilles Peskine709346a2017-12-10 23:43:39 +0100298 -s|--seed Integer seed value to use for this test run.
299
300Tool path options:
301 --armc5-bin-dir=<ARMC5_bin_dir_path> ARM Compiler 5 bin directory.
302 --armc6-bin-dir=<ARMC6_bin_dir_path> ARM Compiler 6 bin directory.
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100303 --clang-earliest=<Clang_earliest_path> Earliest version of clang available
304 --clang-latest=<Clang_latest_path> Latest version of clang available
305 --gcc-earliest=<GCC_earliest_path> Earliest version of GCC available
306 --gcc-latest=<GCC_latest_path> Latest version of GCC available
Gilles Peskine709346a2017-12-10 23:43:39 +0100307 --gnutls-cli=<GnuTLS_cli_path> GnuTLS client executable to use for most tests.
308 --gnutls-serv=<GnuTLS_serv_path> GnuTLS server executable to use for most tests.
Gilles Peskine709346a2017-12-10 23:43:39 +0100309 --openssl=<OpenSSL_path> OpenSSL executable to use for most tests.
Manuel Pégourié-Gonnard6b368922018-02-20 12:02:07 +0100310 --openssl-next=<OpenSSL_path> OpenSSL executable to use for recent things like ARIA
Gilles Peskine709346a2017-12-10 23:43:39 +0100311EOF
SimonB2e23c822016-04-16 21:54:39 +0100312}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100313
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200314# Cleanup before/after running a component.
315# Remove built files as well as the cmake cache/config.
316# Does not remove generated source files.
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100317cleanup()
318{
David Horstmann76a77382023-08-17 17:32:26 +0100319 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100320 command make clean
321 fi
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200322
Gilles Peskine31b07e22018-03-21 12:15:06 +0100323 # Remove CMake artefacts
Jaeden Amero2d0e00f2018-11-07 18:46:41 +0000324 find . -name .git -prune -o \
Gilles Peskine31b07e22018-03-21 12:15:06 +0100325 -iname CMakeFiles -exec rm -rf {} \+ -o \
326 \( -iname cmake_install.cmake -o \
327 -iname CTestTestfile.cmake -o \
Manuel Pégourié-Gonnardbfe54d72021-09-09 11:11:44 +0200328 -iname CMakeCache.txt -o \
329 -path './cmake/*.cmake' \) -exec rm -f {} \+
Ronald Cronda224022024-06-12 10:04:22 +0200330 # Remove Makefiles generated by in-tree CMake builds
331 rm -f 3rdparty/Makefile 3rdparty/*/Makefile pkgconfig/Makefile framework/Makefile
332 rm -f include/Makefile programs/!(fuzz)/Makefile
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200333
Jaeden Ameroab83fdf2019-06-20 17:38:22 +0100334 # Remove any artifacts from the component_test_cmake_as_subdirectory test.
335 rm -rf programs/test/cmake_subproject/build
336 rm -f programs/test/cmake_subproject/Makefile
337 rm -f programs/test/cmake_subproject/cmake_subproject
338
Chris Kayd259e342021-03-25 16:03:25 +0000339 # Remove any artifacts from the component_test_cmake_as_package test.
340 rm -rf programs/test/cmake_package/build
341 rm -f programs/test/cmake_package/Makefile
342 rm -f programs/test/cmake_package/cmake_package
343
344 # Remove any artifacts from the component_test_cmake_as_installed_package test.
345 rm -rf programs/test/cmake_package_install/build
346 rm -f programs/test/cmake_package_install/Makefile
347 rm -f programs/test/cmake_package_install/cmake_package_install
348
Gilles Peskinef83eb822020-03-30 20:11:39 +0200349 # Restore files that may have been clobbered by the job
350 for x in $files_to_back_up; do
Gilles Peskinedd06efb2022-08-30 21:02:00 +0200351 if [[ -e "$x$backup_suffix" ]]; then
352 cp -p "$x$backup_suffix" "$x"
353 fi
Gilles Peskinef83eb822020-03-30 20:11:39 +0200354 done
355}
John Durkopbd069d32020-10-31 22:14:03 -0700356
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200357# Final cleanup when this script exits (except when exiting on a failure
358# in non-keep-going mode).
Gilles Peskinef83eb822020-03-30 20:11:39 +0200359final_cleanup () {
360 cleanup
361
362 for x in $files_to_back_up; do
363 rm -f "$x$backup_suffix"
364 done
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100365}
366
Gilles Peskine7c652162017-12-11 00:01:40 +0100367# Executed on exit. May be redefined depending on command line options.
368final_report () {
369 :
370}
371
372fatal_signal () {
Gilles Peskinef83eb822020-03-30 20:11:39 +0200373 final_cleanup
Gilles Peskine7c652162017-12-11 00:01:40 +0100374 final_report $1
375 trap - $1
376 kill -$1 $$
377}
378
379trap 'fatal_signal HUP' HUP
380trap 'fatal_signal INT' INT
381trap 'fatal_signal TERM' TERM
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200382
Gilles Peskine050d2fc2021-09-30 18:24:21 +0200383# Number of processors on this machine. Used as the default setting
384# for parallel make.
385all_sh_nproc ()
386{
387 {
388 nproc || # Linux
389 sysctl -n hw.ncpuonline || # NetBSD, OpenBSD
390 sysctl -n hw.ncpu || # FreeBSD
391 echo 1
392 } 2>/dev/null
393}
394
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100395msg()
396{
Gilles Peskineffcdeff2018-12-04 12:49:28 +0100397 if [ -n "${current_component:-}" ]; then
398 current_section="${current_component#component_}: $1"
399 else
400 current_section="$1"
401 fi
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200402
403 if [ $QUIET -eq 1 ]; then
404 return
405 fi
406
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100407 echo ""
408 echo "******************************************************************"
Gilles Peskineffcdeff2018-12-04 12:49:28 +0100409 echo "* $current_section "
Manuel Pégourié-Gonnard392d3dd2015-01-26 14:03:56 +0000410 printf "* "; date
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100411 echo "******************************************************************"
412}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100413
Gilles Peskine8f073122018-11-27 15:58:47 +0100414armc6_build_test()
415{
416 FLAGS="$1"
Andres AGa5cd9732016-10-17 15:23:10 +0100417
Gilles Peskine18487f62020-04-30 23:11:54 +0200418 msg "build: ARM Compiler 6 ($FLAGS)"
Yanray Wangbc7716c2023-09-08 11:20:59 +0800419 make clean
Gilles Peskine8f073122018-11-27 15:58:47 +0100420 ARM_TOOL_VARIANT="ult" CC="$ARMC6_CC" AR="$ARMC6_AR" CFLAGS="$FLAGS" \
Dave Rodgman1c232a82023-03-02 13:39:04 +0000421 WARNING_CFLAGS='-Werror -xc -std=c99' make lib
Gilles Peskine18487f62020-04-30 23:11:54 +0200422
423 msg "size: ARM Compiler 6 ($FLAGS)"
424 "$ARMC6_FROMELF" -z library/*.o
Gilles Peskine8f073122018-11-27 15:58:47 +0100425}
Andres AGa5cd9732016-10-17 15:23:10 +0100426
Andres AGd9eba4b2016-08-26 14:42:14 +0100427err_msg()
428{
429 echo "$1" >&2
430}
431
432check_tools()
433{
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200434 for tool in "$@"; do
435 if ! `type "$tool" >/dev/null 2>&1`; then
436 err_msg "$tool not found!"
Andres AGd9eba4b2016-08-26 14:42:14 +0100437 exit 1
438 fi
439 done
440}
441
Gilles Peskine8f073122018-11-27 15:58:47 +0100442pre_parse_command_line () {
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000443 COMMAND_LINE_COMPONENTS=
Gilles Peskinea28db922019-01-10 00:05:18 +0100444 all_except=0
Gilles Peskine5d996822020-03-28 21:09:21 +0100445 error_test=0
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100446 list_components=0
Gilles Peskine80ddb992021-08-06 11:51:59 +0200447 restore_first=0
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000448 no_armcc=
SimonB2e23c822016-04-16 21:54:39 +0100449
Jaeden Amero9b90f2e2018-11-02 18:34:17 +0000450 # Note that legacy options are ignored instead of being omitted from this
451 # list of options, so invocations that worked with previous version of
452 # all.sh will still run and work properly.
Gilles Peskine8f073122018-11-27 15:58:47 +0100453 while [ $# -gt 0 ]; do
Gilles Peskine55f7c942019-01-09 22:28:21 +0100454 case "$1" in
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200455 --append-outcome) append_outcome=1;;
Gilles Peskine6d061342020-04-30 18:19:32 +0200456 --arm-none-eabi-gcc-prefix) shift; ARM_NONE_EABI_GCC_PREFIX="$1";;
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200457 --arm-linux-gnueabi-gcc-prefix) shift; ARM_LINUX_GNUEABI_GCC_PREFIX="$1";;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000458 --armcc) no_armcc=;;
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100459 --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";;
460 --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100461 --clang-earliest) shift; CLANG_EARLIEST="$1";;
462 --clang-latest) shift; CLANG_LATEST="$1";;
Gilles Peskine5d996822020-03-28 21:09:21 +0100463 --error-test) error_test=$((error_test + 1));;
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000464 --except) all_except=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100465 --force|-f) FORCE=1;;
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100466 --gcc-earliest) shift; GCC_EARLIEST="$1";;
467 --gcc-latest) shift; GCC_LATEST="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100468 --gnutls-cli) shift; GNUTLS_CLI="$1";;
Gilles Peskine7be571a2023-08-27 21:39:21 +0200469 --gnutls-legacy-cli) shift;; # ignored for backward compatibility
470 --gnutls-legacy-serv) shift;; # ignored for backward compatibility
Gilles Peskine55f7c942019-01-09 22:28:21 +0100471 --gnutls-serv) shift; GNUTLS_SERV="$1";;
472 --help|-h) usage; exit;;
473 --keep-going|-k) KEEP_GOING=1;;
Gilles Peskine878cf602019-01-06 20:50:38 +0000474 --list-all-components) printf '%s\n' $ALL_COMPONENTS; exit;;
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100475 --list-components) list_components=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100476 --memory|-m) MEMORY=1;;
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200477 --no-append-outcome) append_outcome=0;;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000478 --no-armcc) no_armcc=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100479 --no-force) FORCE=0;;
480 --no-keep-going) KEEP_GOING=0;;
481 --no-memory) MEMORY=0;;
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200482 --no-quiet) QUIET=0;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100483 --openssl) shift; OPENSSL="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100484 --openssl-next) shift; OPENSSL_NEXT="$1";;
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200485 --outcome-file) shift; MBEDTLS_TEST_OUTCOME_FILE="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100486 --out-of-source-dir) shift; OUT_OF_SOURCE_DIR="$1";;
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200487 --quiet|-q) QUIET=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100488 --random-seed) unset SEED;;
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200489 --release-test|-r) SEED=$RELEASE_SEED;;
Gilles Peskine80ddb992021-08-06 11:51:59 +0200490 --restore) restore_first=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100491 --seed|-s) shift; SEED="$1";;
492 -*)
493 echo >&2 "Unknown option: $1"
494 echo >&2 "Run $0 --help for usage."
495 exit 120
496 ;;
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000497 *) COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS $1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100498 esac
499 shift
Gilles Peskine8f073122018-11-27 15:58:47 +0100500 done
SimonB2e23c822016-04-16 21:54:39 +0100501
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100502 # Exclude components that are not supported on this platform.
503 SUPPORTED_COMPONENTS=
504 for component in $ALL_COMPONENTS; do
505 case $(type "support_$component" 2>&1) in
506 *' function'*)
507 if ! support_$component; then continue; fi;;
508 esac
509 SUPPORTED_COMPONENTS="$SUPPORTED_COMPONENTS $component"
510 done
511
512 if [ $list_components -eq 1 ]; then
513 printf '%s\n' $SUPPORTED_COMPONENTS
514 exit
515 fi
516
Gilles Peskinea28db922019-01-10 00:05:18 +0100517 # With no list of components, run everything.
Gilles Peskine80ddb992021-08-06 11:51:59 +0200518 if [ -z "$COMMAND_LINE_COMPONENTS" ] && [ $restore_first -eq 0 ]; then
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000519 all_except=1
Andres AGdc192212016-08-31 17:33:13 +0100520 fi
521
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000522 # --no-armcc is a legacy option. The modern way is --except '*_armcc*'.
523 # Ignore it if components are listed explicitly on the command line.
Gilles Peskinea28db922019-01-10 00:05:18 +0100524 if [ -n "$no_armcc" ] && [ $all_except -eq 1 ]; then
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000525 COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*"
SimonB2e23c822016-04-16 21:54:39 +0100526 fi
SimonB2e23c822016-04-16 21:54:39 +0100527
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200528 # Error out if an explicitly requested component doesn't exist.
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100529 if [ $all_except -eq 0 ]; then
530 unsupported=0
Gilles Peskine03af6782021-08-06 11:35:17 +0200531 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
532 # only does word splitting.
Gilles Peskine1d475b62021-08-03 13:43:36 +0200533 set -f
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100534 for component in $COMMAND_LINE_COMPONENTS; do
Gilles Peskine1d475b62021-08-03 13:43:36 +0200535 set +f
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200536 # If the requested name includes a wildcard character, don't
537 # check it. Accept wildcard patterns that don't match anything.
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100538 case $component in
539 *[*?\[]*) continue;;
540 esac
541 case " $SUPPORTED_COMPONENTS " in
542 *" $component "*) :;;
543 *)
544 echo >&2 "Component $component was explicitly requested, but is not known or not supported."
545 unsupported=$((unsupported + 1));;
546 esac
547 done
Gilles Peskine1d475b62021-08-03 13:43:36 +0200548 set +f
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100549 if [ $unsupported -ne 0 ]; then
550 exit 2
551 fi
552 fi
553
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000554 # Build the list of components to run.
Gilles Peskinea28db922019-01-10 00:05:18 +0100555 RUN_COMPONENTS=
556 for component in $SUPPORTED_COMPONENTS; do
557 if is_component_included "$component"; [ $? -eq $all_except ]; then
558 RUN_COMPONENTS="$RUN_COMPONENTS $component"
559 fi
560 done
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000561
562 unset all_except
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000563 unset no_armcc
Gilles Peskine8f073122018-11-27 15:58:47 +0100564}
SimonB2e23c822016-04-16 21:54:39 +0100565
Gilles Peskine8f073122018-11-27 15:58:47 +0100566pre_check_git () {
567 if [ $FORCE -eq 1 ]; then
Gilles Peskine53190e62019-01-09 23:17:35 +0100568 rm -rf "$OUT_OF_SOURCE_DIR"
Gilles Peskine8f073122018-11-27 15:58:47 +0100569 git checkout-index -f -q $CONFIG_H
570 cleanup
571 else
SimonB2e23c822016-04-16 21:54:39 +0100572
Gilles Peskine8f073122018-11-27 15:58:47 +0100573 if [ -d "$OUT_OF_SOURCE_DIR" ]; then
574 echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
575 echo "You can either delete this directory manually, or force the test by rerunning"
576 echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
577 exit 1
578 fi
579
Ronald Cron7a93ac52023-07-20 09:49:12 +0200580 if ! git diff --quiet "$CONFIG_H"; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100581 err_msg "Warning - the configuration file '$CONFIG_H' has been edited. "
Gilles Peskine8f073122018-11-27 15:58:47 +0100582 echo "You can either delete or preserve your work, or force the test by rerunning the"
583 echo "script as: $0 --force"
584 exit 1
585 fi
SimonB2e23c822016-04-16 21:54:39 +0100586 fi
Andrzej Kurekeb508712019-02-14 07:18:59 -0500587}
588
Gilles Peskine568f53a2021-07-12 18:16:01 +0200589pre_restore_files () {
590 # If the makefiles have been generated by a framework such as cmake,
591 # restore them from git. If the makefiles look like modifications from
592 # the ones checked into git, take care not to modify them. Whatever
593 # this function leaves behind is what the script will restore before
594 # each component.
595 case "$(head -n1 Makefile)" in
596 *[Gg]enerated*)
597 git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
598 git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
599 ;;
600 esac
601}
602
Gilles Peskinef83eb822020-03-30 20:11:39 +0200603pre_back_up () {
604 for x in $files_to_back_up; do
605 cp -p "$x" "$x$backup_suffix"
606 done
607}
608
Gilles Peskine8f073122018-11-27 15:58:47 +0100609pre_setup_keep_going () {
Gilles Peskinece266c42020-03-28 18:50:43 +0100610 failure_count=0 # Number of failed components
611 last_failure_status=0 # Last failure status in this component
612
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100613 # See err_trap
614 previous_failure_status=0
615 previous_failed_command=
616 previous_failure_funcall_depth=0
Gilles Peskinea681c592020-03-28 21:27:40 +0100617 unset report_failed_command
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100618
Gilles Peskine7c652162017-12-11 00:01:40 +0100619 start_red=
620 end_color=
621 if [ -t 1 ]; then
Gilles Peskine9736b9d2018-01-02 21:54:17 +0100622 case "${TERM:-}" in
Gilles Peskine7c652162017-12-11 00:01:40 +0100623 *color*|cygwin|linux|rxvt*|screen|[Eex]term*)
624 start_red=$(printf '\033[31m')
625 end_color=$(printf '\033[0m')
626 ;;
627 esac
628 fi
Gilles Peskinece266c42020-03-28 18:50:43 +0100629
630 # Keep a summary of failures in a file. We'll print it out at the end.
631 failure_summary_file=$PWD/all-sh-failures-$$.log
632 : >"$failure_summary_file"
633
634 # Whether it makes sense to keep a component going after the specified
635 # command fails (test command) or not (configure or build).
Gilles Peskine03af6782021-08-06 11:35:17 +0200636 # This function normally receives the failing simple command
637 # ($BASH_COMMAND) as an argument, but if $report_failed_command is set,
638 # this is passed instead.
Gilles Peskinece266c42020-03-28 18:50:43 +0100639 # This doesn't have to be 100% accurate: all failures are recorded anyway.
Gilles Peskinec111e242021-08-02 23:29:53 +0200640 # False positives result in running things that can't be expected to
641 # work. False negatives result in things not running after something else
642 # failed even though they might have given useful feedback.
Gilles Peskinece266c42020-03-28 18:50:43 +0100643 can_keep_going_after_failure () {
644 case "$1" in
645 "msg "*) false;;
Gilles Peskinec111e242021-08-02 23:29:53 +0200646 "cd "*) false;;
Manuel Pégourié-Gonnarda7423372023-11-10 09:58:31 +0100647 "diff "*) true;;
Gilles Peskinec111e242021-08-02 23:29:53 +0200648 *make*[\ /]tests*) false;; # make tests, make CFLAGS=-I../tests, ...
649 *test*) true;; # make test, tests/stuff, env V=v tests/stuff, ...
650 *make*check*) true;;
651 "grep "*) true;;
652 "[ "*) true;;
653 "! "*) true;;
Gilles Peskinece266c42020-03-28 18:50:43 +0100654 *) false;;
Gilles Peskine7c652162017-12-11 00:01:40 +0100655 esac
656 }
Gilles Peskinece266c42020-03-28 18:50:43 +0100657
658 # This function runs if there is any error in a component.
659 # It must either exit with a nonzero status, or set
660 # last_failure_status to a nonzero value.
661 err_trap () {
662 # Save $? (status of the failing command). This must be the very
663 # first thing, before $? is overridden.
664 last_failure_status=$?
Gilles Peskinea681c592020-03-28 21:27:40 +0100665 failed_command=${report_failed_command-$BASH_COMMAND}
Gilles Peskinece266c42020-03-28 18:50:43 +0100666
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100667 if [[ $last_failure_status -eq $previous_failure_status &&
668 "$failed_command" == "$previous_failed_command" &&
669 ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]]
670 then
671 # The same command failed twice in a row, but this time one level
672 # less deep in the function call stack. This happens when the last
673 # command of a function returns a nonzero status, and the function
674 # returns that same status. Ignore the second failure.
675 previous_failure_funcall_depth=${#FUNCNAME[@]}
676 return
677 fi
678 previous_failure_status=$last_failure_status
679 previous_failed_command=$failed_command
680 previous_failure_funcall_depth=${#FUNCNAME[@]}
681
Gilles Peskinece266c42020-03-28 18:50:43 +0100682 text="$current_section: $failed_command -> $last_failure_status"
683 echo "${start_red}^^^^$text^^^^${end_color}" >&2
684 echo "$text" >>"$failure_summary_file"
685
686 # If the command is fatal (configure or build command), stop this
687 # component. Otherwise (test command) keep the component running
688 # (run more tests from the same build).
689 if ! can_keep_going_after_failure "$failed_command"; then
690 exit $last_failure_status
691 fi
692 }
693
Gilles Peskine7c652162017-12-11 00:01:40 +0100694 final_report () {
695 if [ $failure_count -gt 0 ]; then
696 echo
697 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
Gilles Peskinece266c42020-03-28 18:50:43 +0100698 echo "${start_red}FAILED: $failure_count components${end_color}"
699 cat "$failure_summary_file"
Gilles Peskine7c652162017-12-11 00:01:40 +0100700 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
701 elif [ -z "${1-}" ]; then
702 echo "SUCCESS :)"
703 fi
704 if [ -n "${1-}" ]; then
705 echo "Killed by SIG$1."
706 fi
Gilles Peskinece266c42020-03-28 18:50:43 +0100707 rm -f "$failure_summary_file"
708 if [ $failure_count -gt 0 ]; then
709 exit 1
710 fi
Gilles Peskine7c652162017-12-11 00:01:40 +0100711 }
Gilles Peskine8f073122018-11-27 15:58:47 +0100712}
713
Gilles Peskine1f0cdaf2021-07-08 18:41:16 +0200714# record_status() and if_build_succeeded() are kept temporarily for backward
715# compatibility. Don't use them in new components.
Gilles Peskinece266c42020-03-28 18:50:43 +0100716record_status () {
717 "$@"
Gilles Peskine7c652162017-12-11 00:01:40 +0100718}
Gilles Peskine7c652162017-12-11 00:01:40 +0100719if_build_succeeded () {
Gilles Peskinece266c42020-03-28 18:50:43 +0100720 "$@"
Gilles Peskine7c652162017-12-11 00:01:40 +0100721}
722
Gilles Peskinea681c592020-03-28 21:27:40 +0100723# '! true' does not trigger the ERR trap. Arrange to trigger it, with
724# a reasonably informative error message (not just "$@").
725not () {
726 if "$@"; then
727 report_failed_command="! $*"
728 false
729 unset report_failed_command
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200730 fi
731}
732
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200733pre_prepare_outcome_file () {
734 case "$MBEDTLS_TEST_OUTCOME_FILE" in
735 [!/]*) MBEDTLS_TEST_OUTCOME_FILE="$PWD/$MBEDTLS_TEST_OUTCOME_FILE";;
736 esac
737 if [ -n "$MBEDTLS_TEST_OUTCOME_FILE" ] && [ "$append_outcome" -eq 0 ]; then
738 rm -f "$MBEDTLS_TEST_OUTCOME_FILE"
739 fi
740}
741
Gilles Peskine8f073122018-11-27 15:58:47 +0100742pre_print_configuration () {
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200743 if [ $QUIET -eq 1 ]; then
744 return
745 fi
746
Gilles Peskine8f073122018-11-27 15:58:47 +0100747 msg "info: $0 configuration"
748 echo "MEMORY: $MEMORY"
749 echo "FORCE: $FORCE"
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200750 echo "MBEDTLS_TEST_OUTCOME_FILE: ${MBEDTLS_TEST_OUTCOME_FILE:-(none)}"
Gilles Peskine8f073122018-11-27 15:58:47 +0100751 echo "SEED: ${SEED-"UNSET"}"
Gilles Peskine636c26a2020-03-04 20:46:15 +0100752 echo
Gilles Peskine8f073122018-11-27 15:58:47 +0100753 echo "OPENSSL: $OPENSSL"
Gilles Peskine8f073122018-11-27 15:58:47 +0100754 echo "OPENSSL_NEXT: $OPENSSL_NEXT"
755 echo "GNUTLS_CLI: $GNUTLS_CLI"
756 echo "GNUTLS_SERV: $GNUTLS_SERV"
Gilles Peskine8f073122018-11-27 15:58:47 +0100757 echo "ARMC5_BIN_DIR: $ARMC5_BIN_DIR"
758 echo "ARMC6_BIN_DIR: $ARMC6_BIN_DIR"
759}
Andres AG7770ea82016-10-10 15:46:20 +0100760
Andres AGd9eba4b2016-08-26 14:42:14 +0100761# Make sure the tools we need are available.
Gilles Peskine8f073122018-11-27 15:58:47 +0100762pre_check_tools () {
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000763 # Build the list of variables to pass to output_env.sh.
764 set env
SimonB2e23c822016-04-16 21:54:39 +0100765
Gilles Peskine87964262019-01-06 22:40:00 +0000766 case " $RUN_COMPONENTS " in
767 # Require OpenSSL and GnuTLS if running any tests (as opposed to
768 # only doing builds). Not all tests run OpenSSL and GnuTLS, but this
769 # is a good enough approximation in practice.
Bence Szépkútibbb5af92023-12-15 20:58:15 +0100770 *" test_"* | *" release_test_"*)
Gilles Peskine87964262019-01-06 22:40:00 +0000771 # To avoid setting OpenSSL and GnuTLS for each call to compat.sh
772 # and ssl-opt.sh, we just export the variables they require.
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +0100773 export OPENSSL="$OPENSSL"
Gilles Peskine87964262019-01-06 22:40:00 +0000774 export GNUTLS_CLI="$GNUTLS_CLI"
775 export GNUTLS_SERV="$GNUTLS_SERV"
776 # Avoid passing --seed flag in every call to ssl-opt.sh
777 if [ -n "${SEED-}" ]; then
778 export SEED
779 fi
Gilles Peskine7be571a2023-08-27 21:39:21 +0200780 set "$@" OPENSSL="$OPENSSL"
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000781 set "$@" GNUTLS_CLI="$GNUTLS_CLI" GNUTLS_SERV="$GNUTLS_SERV"
Gilles Peskine7be571a2023-08-27 21:39:21 +0200782 check_tools "$OPENSSL" "$OPENSSL_NEXT" \
783 "$GNUTLS_CLI" "$GNUTLS_SERV"
Gilles Peskine87964262019-01-06 22:40:00 +0000784 ;;
785 esac
Manuel Pégourié-Gonnard57255b12014-06-09 11:21:49 +0200786
Gilles Peskine87964262019-01-06 22:40:00 +0000787 case " $RUN_COMPONENTS " in
788 *_doxygen[_\ ]*) check_tools "doxygen" "dot";;
789 esac
Andres AGd9eba4b2016-08-26 14:42:14 +0100790
Gilles Peskine87964262019-01-06 22:40:00 +0000791 case " $RUN_COMPONENTS " in
Gilles Peskine6d061342020-04-30 18:19:32 +0200792 *_arm_none_eabi_gcc[_\ ]*) check_tools "${ARM_NONE_EABI_GCC_PREFIX}gcc";;
Gilles Peskine87964262019-01-06 22:40:00 +0000793 esac
Andres AGd9eba4b2016-08-26 14:42:14 +0100794
Gilles Peskine87964262019-01-06 22:40:00 +0000795 case " $RUN_COMPONENTS " in
796 *_mingw[_\ ]*) check_tools "i686-w64-mingw32-gcc";;
797 esac
798
799 case " $RUN_COMPONENTS " in
800 *" test_zeroize "*) check_tools "gdb";;
801 esac
802
803 case " $RUN_COMPONENTS " in
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000804 *_armcc*)
Gilles Peskine87964262019-01-06 22:40:00 +0000805 ARMC5_CC="$ARMC5_BIN_DIR/armcc"
806 ARMC5_AR="$ARMC5_BIN_DIR/armar"
Gilles Peskine18487f62020-04-30 23:11:54 +0200807 ARMC5_FROMELF="$ARMC5_BIN_DIR/fromelf"
Gilles Peskine87964262019-01-06 22:40:00 +0000808 ARMC6_CC="$ARMC6_BIN_DIR/armclang"
809 ARMC6_AR="$ARMC6_BIN_DIR/armar"
Gilles Peskine18487f62020-04-30 23:11:54 +0200810 ARMC6_FROMELF="$ARMC6_BIN_DIR/fromelf"
811 check_tools "$ARMC5_CC" "$ARMC5_AR" "$ARMC5_FROMELF" \
812 "$ARMC6_CC" "$ARMC6_AR" "$ARMC6_FROMELF";;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000813 esac
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000814
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200815 # past this point, no call to check_tool, only printing output
816 if [ $QUIET -eq 1 ]; then
817 return
818 fi
819
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000820 msg "info: output_env.sh"
821 case $RUN_COMPONENTS in
822 *_armcc*)
823 set "$@" ARMC5_CC="$ARMC5_CC" ARMC6_CC="$ARMC6_CC" RUN_ARMCC=1;;
824 *) set "$@" RUN_ARMCC=0;;
825 esac
826 "$@" scripts/output_env.sh
Gilles Peskine8f073122018-11-27 15:58:47 +0100827}
Manuel Pégourié-Gonnard9bda9b32014-11-20 13:10:22 +0100828
Minos Galanakisf1f086e2024-07-30 11:58:24 +0100829pre_generate_files () {
Manuel Pégourié-Gonnard87db8a22021-06-18 13:30:14 +0200830 # since make doesn't have proper dependencies, remove any possibly outdate
831 # file that might be around before generating fresh ones
832 make neat
Gilles Peskine72385032021-07-08 19:07:07 +0200833 if [ $QUIET -eq 1 ]; then
Gilles Peskine75301632021-08-05 15:10:47 +0200834 make generated_files >/dev/null
Gilles Peskine72385032021-07-08 19:07:07 +0200835 else
836 make generated_files
837 fi
Gilles Peskine1570b592021-04-22 01:10:12 +0200838}
839
Minos Galanakisf1f086e2024-07-30 11:58:24 +0100840clang_version () {
Tom Cosgroved4c373a2024-01-30 13:56:38 +0000841 if command -v clang > /dev/null ; then
842 clang --version|grep version|sed -E 's#.*version ([0-9]+).*#\1#'
843 else
844 echo 0 # report version 0 for "no clang"
845 fi
846}
847
Manuel Pégourié-Gonnard27e17672023-05-25 09:39:08 +0200848################################################################
849#### Helpers for components using libtestdriver1
850################################################################
Gilles Peskine192c72f2017-12-21 15:59:21 +0100851
Manuel Pégourié-Gonnardf5c5d972023-06-06 11:14:57 +0200852# How to use libtestdriver1
853# -------------------------
854#
855# 1. Define the list algorithms and key types to accelerate,
856# designated the same way as PSA_WANT_ macros but without PSA_WANT_.
857# Examples:
858# - loc_accel_list="ALG_JPAKE"
859# - loc_accel_list="ALG_FFDH KEY_TYPE_DH_KEY_PAIR KEY_TYPE_DH_PUBLIC_KEY"
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200860# 2. Make configurations changes for the driver and/or main libraries.
861# 2a. Call helper_libtestdriver1_adjust_config <base>, where the argument
862# can be either "default" to start with the default config, or a name
863# supported by scripts/config.py (for example, "full"). This selects
Manuel Pégourié-Gonnard2336c8e2023-06-12 17:17:54 +0200864# the base to use, and makes common adjustments.
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200865# 2b. If desired, adjust the PSA_WANT symbols in psa/crypto_config.h.
866# These changes affect both the driver and the main libraries.
867# (Note: they need to have the same set of PSA_WANT symbols, as that
868# determines the ABI between them.)
869# 2c. Adjust MBEDTLS_ symbols in mbedtls_config.h. This only affects the
870# main libraries. Typically, you want to disable the module(s) that are
871# being accelerated. You may need to also disable modules that depend
872# on them or options that are not supported with drivers.
873# 2d. On top of psa/crypto_config.h, the driver library uses its own config
874# file: tests/include/test/drivers/config_test_driver.h. You usually
875# don't need to edit it: using loc_extra_list (see below) is preferred.
876# However, when there's no PSA symbol for what you want to enable,
877# calling scripts/config.py on this file remains the only option.
878# 3. Build the driver library, then the main libraries, test, and programs.
879# 3a. Call helper_libtestdriver1_make_drivers "$loc_accel_list". You may
880# need to enable more algorithms here, typically hash algorithms when
881# accelerating some signature algorithms (ECDSA, RSAv2). This is done
882# by passing a 2nd argument listing the extra algorithms.
883# Example:
884# loc_extra_list="ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512"
885# helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
Yanray Wang0df6d962023-10-11 13:27:25 +0800886# 3b. Call helper_libtestdriver1_make_main "$loc_accel_list". Any
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200887# additional arguments will be passed to make: this can be useful if
888# you don't want to build everything when iterating during development.
889# Example:
890# helper_libtestdriver1_make_main "$loc_accel_list" -C tests test_suite_foo
891# 4. Run the tests you want.
Manuel Pégourié-Gonnardf5c5d972023-06-06 11:14:57 +0200892
Manuel Pégourié-Gonnard27e17672023-05-25 09:39:08 +0200893# Adjust the configuration - for both libtestdriver1 and main library,
894# as they should have the same PSA_WANT macros.
Minos Galanakisf1f086e2024-07-30 11:58:24 +0100895helper_libtestdriver1_adjust_config () {
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200896 base_config=$1
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200897 # Select the base configuration
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200898 if [ "$base_config" != "default" ]; then
899 scripts/config.py "$base_config"
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200900 fi
901
902 # Enable PSA-based config (necessary to use drivers)
903 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
904
Manuel Pégourié-Gonnard2336c8e2023-06-12 17:17:54 +0200905 # Dynamic secure element support is a deprecated feature and needs to be disabled here.
906 # This is done to have the same form of psa_key_attributes_s for libdriver and library.
907 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
Paul Elliott053b7882024-03-05 11:46:32 +0000908
909 # If threading is enabled on the normal build, then we need to enable it in the drivers as well,
910 # otherwise we will end up running multithreaded tests without mutexes to protect them.
911 if scripts/config.py get MBEDTLS_THREADING_C; then
912 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_C
913 fi
914
915 if scripts/config.py get MBEDTLS_THREADING_PTHREAD; then
916 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_PTHREAD
917 fi
Manuel Pégourié-Gonnard27e17672023-05-25 09:39:08 +0200918}
Gilles Peskine192c72f2017-12-21 15:59:21 +0100919
Valerio Settib2fd6732023-08-15 17:10:47 +0200920# When called with no parameter this function disables all builtin curves.
Valerio Setti4ca250b2023-09-04 14:01:41 +0200921# The function optionally accepts 1 parameter: a space-separated list of the
922# curves that should be kept enabled.
Minos Galanakisf1f086e2024-07-30 11:58:24 +0100923helper_disable_builtin_curves () {
Valerio Settib2fd6732023-08-15 17:10:47 +0200924 allowed_list="${1:-}"
925 scripts/config.py unset-all "MBEDTLS_ECP_DP_[0-9A-Z_a-z]*_ENABLED"
926
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200927 for curve in $allowed_list; do
928 scripts/config.py set $curve
Valerio Settib2fd6732023-08-15 17:10:47 +0200929 done
930}
931
932# Helper returning the list of supported elliptic curves from CRYPTO_CONFIG_H,
933# without the "PSA_WANT_" prefix. This becomes handy for accelerating curves
934# in the following helpers.
935helper_get_psa_curve_list () {
936 loc_list=""
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200937 for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
938 loc_list="$loc_list $item"
Valerio Settib2fd6732023-08-15 17:10:47 +0200939 done
940
941 echo "$loc_list"
942}
943
Valerio Setti78aa0bc2024-01-17 15:53:46 +0100944# Helper returning the list of supported DH groups from CRYPTO_CONFIG_H,
945# without the "PSA_WANT_" prefix. This becomes handy for accelerating DH groups
946# in the following helpers.
947helper_get_psa_dh_group_list () {
948 loc_list=""
949 for item in $(sed -n 's/^#define PSA_WANT_\(DH_RFC7919_[0-9]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
950 loc_list="$loc_list $item"
951 done
952
953 echo "$loc_list"
954}
955
Valerio Settib2fd6732023-08-15 17:10:47 +0200956# Get the list of uncommented PSA_WANT_KEY_TYPE_xxx_ from CRYPTO_CONFIG_H. This
Valerio Setti0a342c92023-09-01 09:12:31 +0200957# is useful to easily get a list of key type symbols to accelerate.
Valerio Settib2fd6732023-08-15 17:10:47 +0200958# The function accepts a single argument which is the key type: ECC, DH, RSA.
Minos Galanakisf1f086e2024-07-30 11:58:24 +0100959helper_get_psa_key_type_list () {
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200960 key_type="$1"
Valerio Settib2fd6732023-08-15 17:10:47 +0200961 loc_list=""
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200962 for item in $(sed -n "s/^#define PSA_WANT_\(KEY_TYPE_${key_type}_[0-9A-Z_a-z]*\).*/\1/p" <"$CRYPTO_CONFIG_H"); do
Valerio Settib2fd6732023-08-15 17:10:47 +0200963 # Skip DERIVE for elliptic keys since there is no driver dispatch for
964 # it so it cannot be accelerated.
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200965 if [ "$item" != "KEY_TYPE_ECC_KEY_PAIR_DERIVE" ]; then
966 loc_list="$loc_list $item"
Valerio Settib2fd6732023-08-15 17:10:47 +0200967 fi
968 done
969
970 echo "$loc_list"
971}
972
Manuel Pégourié-Gonnard27dd73f2023-05-25 10:39:23 +0200973# Build the drivers library libtestdriver1.a (with ASan).
Manuel Pégourié-Gonnard31639e42023-05-25 10:07:31 +0200974#
975# Parameters:
976# 1. a space-separated list of things to accelerate;
977# 2. optional: a space-separate list of things to also support.
978# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
Minos Galanakisf1f086e2024-07-30 11:58:24 +0100979helper_libtestdriver1_make_drivers () {
Manuel Pégourié-Gonnard31639e42023-05-25 10:07:31 +0200980 loc_accel_flags=$( echo "$1 ${2-}" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
Gilles Peskine21570cf2023-12-22 11:49:50 +0100981 make CC=$ASAN_CC -C tests libtestdriver1.a CFLAGS=" $ASAN_CFLAGS $loc_accel_flags" LDFLAGS="$ASAN_CFLAGS"
Manuel Pégourié-Gonnard31639e42023-05-25 10:07:31 +0200982}
983
Manuel Pégourié-Gonnard27dd73f2023-05-25 10:39:23 +0200984# Build the main libraries, programs and tests,
985# linking to the drivers library (with ASan).
986#
987# Parameters:
988# 1. a space-separated list of things to accelerate;
989# *. remaining arguments if any are passed directly to make
990# (examples: lib, -C tests test_suite_xxx, etc.)
991# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
Minos Galanakisf1f086e2024-07-30 11:58:24 +0100992helper_libtestdriver1_make_main () {
Manuel Pégourié-Gonnard27dd73f2023-05-25 10:39:23 +0200993 loc_accel_list=$1
994 shift
995
996 # we need flags both with and without the LIBTESTDRIVER1_ prefix
997 loc_accel_flags=$( echo "$loc_accel_list" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
998 loc_accel_flags="$loc_accel_flags $( echo "$loc_accel_list" | sed 's/[^ ]* */-DMBEDTLS_PSA_ACCEL_&/g' )"
Gilles Peskine21570cf2023-12-22 11:49:50 +0100999 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -I../tests/include -I../tests -I../../tests -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_TEST_LIBTESTDRIVER1 $loc_accel_flags" LDFLAGS="-ltestdriver1 $ASAN_CFLAGS" "$@"
Manuel Pégourié-Gonnard27dd73f2023-05-25 10:39:23 +02001000}
Gilles Peskine192c72f2017-12-21 15:59:21 +01001001
Minos Galanakisf7d1cb02024-07-30 17:25:31 +01001002# Include the components from components.sh
1003test_script_dir="${0%/*}"
1004for file in "$test_script_dir"/components*.sh; do
1005 source $file
1006done
Jerry Yud767cc42023-03-31 15:03:55 +08001007
Gilles Peskine192c72f2017-12-21 15:59:21 +01001008################################################################
1009#### Termination
1010################################################################
1011
Gilles Peskine8f073122018-11-27 15:58:47 +01001012post_report () {
1013 msg "Done, cleaning up"
Gilles Peskinef83eb822020-03-30 20:11:39 +02001014 final_cleanup
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +01001015
Gilles Peskine8f073122018-11-27 15:58:47 +01001016 final_report
1017}
1018
Gilles Peskine8f073122018-11-27 15:58:47 +01001019################################################################
1020#### Run all the things
1021################################################################
1022
Gilles Peskine5d996822020-03-28 21:09:21 +01001023# Function invoked by --error-test to test error reporting.
1024pseudo_component_error_test () {
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001025 msg "Testing error reporting $error_test_i"
Gilles Peskine5d996822020-03-28 21:09:21 +01001026 if [ $KEEP_GOING -ne 0 ]; then
1027 echo "Expect three failing commands."
1028 fi
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001029 # If the component doesn't run in a subshell, changing error_test_i to an
1030 # invalid integer will cause an error in the loop that runs this function.
1031 error_test_i=this_should_not_be_used_since_the_component_runs_in_a_subshell
Gilles Peskineec135542021-08-02 23:14:03 +02001032 # Expected error: 'grep non_existent /dev/null -> 1'
Gilles Peskine5d996822020-03-28 21:09:21 +01001033 grep non_existent /dev/null
Gilles Peskineec135542021-08-02 23:14:03 +02001034 # Expected error: '! grep -q . tests/scripts/all.sh -> 1'
Gilles Peskine5d996822020-03-28 21:09:21 +01001035 not grep -q . "$0"
Gilles Peskineec135542021-08-02 23:14:03 +02001036 # Expected error: 'make unknown_target -> 2'
Gilles Peskine5d996822020-03-28 21:09:21 +01001037 make unknown_target
1038 false "this should not be executed"
1039}
1040
Gilles Peskinee48351a2018-11-27 16:06:30 +01001041# Run one component and clean up afterwards.
Gilles Peskine8f073122018-11-27 15:58:47 +01001042run_component () {
Gilles Peskineffcdeff2018-12-04 12:49:28 +01001043 current_component="$1"
Gilles Peskine9004a172019-09-16 15:20:36 +02001044 export MBEDTLS_TEST_CONFIGURATION="$current_component"
Gilles Peskine2ef377d2019-10-07 18:44:21 +02001045
1046 # Unconditionally create a seedfile that's sufficiently long.
1047 # Do this before each component, because a previous component may
1048 # have messed it up or shortened it.
Gilles Peskine88a07452021-07-08 19:03:50 +02001049 local dd_cmd
1050 dd_cmd=(dd if=/dev/urandom of=./tests/seedfile bs=64 count=1)
1051 case $OSTYPE in
Tom Cosgrove226aca12022-03-16 14:11:07 +00001052 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
Gilles Peskine88a07452021-07-08 19:03:50 +02001053 esac
1054 "${dd_cmd[@]}"
Gilles Peskine2ef377d2019-10-07 18:44:21 +02001055
Gilles Peskineec135542021-08-02 23:14:03 +02001056 # Run the component in a subshell, with error trapping and output
1057 # redirection set up based on the relevant options.
Gilles Peskinece266c42020-03-28 18:50:43 +01001058 if [ $KEEP_GOING -eq 1 ]; then
Gilles Peskineec135542021-08-02 23:14:03 +02001059 # We want to keep running if the subshell fails, so 'set -e' must
1060 # be off when the subshell runs.
Gilles Peskinece266c42020-03-28 18:50:43 +01001061 set +e
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +02001062 fi
Gilles Peskinece266c42020-03-28 18:50:43 +01001063 (
1064 if [ $QUIET -eq 1 ]; then
1065 # msg() will be silenced, so just print the component name here.
1066 echo "${current_component#component_}"
1067 exec >/dev/null
1068 fi
1069 if [ $KEEP_GOING -eq 1 ]; then
1070 # Keep "set -e" off, and run an ERR trap instead to record failures.
1071 set -E
1072 trap err_trap ERR
1073 fi
1074 # The next line is what runs the component
1075 "$@"
1076 if [ $KEEP_GOING -eq 1 ]; then
1077 trap - ERR
1078 exit $last_failure_status
1079 fi
1080 )
1081 component_status=$?
1082 if [ $KEEP_GOING -eq 1 ]; then
1083 set -e
1084 if [ $component_status -ne 0 ]; then
1085 failure_count=$((failure_count + 1))
1086 fi
1087 fi
Gilles Peskine2ef377d2019-10-07 18:44:21 +02001088
1089 # Restore the build tree to a clean state.
Gilles Peskinee48351a2018-11-27 16:06:30 +01001090 cleanup
Manuel Pégourié-Gonnard304b0992020-06-08 10:59:41 +02001091 unset current_component
Gilles Peskine8f073122018-11-27 15:58:47 +01001092}
1093
1094# Preliminary setup
1095pre_check_environment
1096pre_initialize_variables
1097pre_parse_command_line "$@"
Gilles Peskine348fb9a2018-11-27 17:04:29 +01001098
Dave Rodgman21900632024-02-26 11:41:19 +00001099setup_quiet_wrappers
Gilles Peskine878cf602019-01-06 20:50:38 +00001100pre_check_git
Gilles Peskine568f53a2021-07-12 18:16:01 +02001101pre_restore_files
Gilles Peskinef83eb822020-03-30 20:11:39 +02001102pre_back_up
Andrzej Kurekeb508712019-02-14 07:18:59 -05001103
Gilles Peskine878cf602019-01-06 20:50:38 +00001104build_status=0
1105if [ $KEEP_GOING -eq 1 ]; then
1106 pre_setup_keep_going
Gilles Peskine8f073122018-11-27 15:58:47 +01001107fi
Gilles Peskine67ffdaf2019-09-16 15:55:46 +02001108pre_prepare_outcome_file
Gilles Peskine878cf602019-01-06 20:50:38 +00001109pre_print_configuration
1110pre_check_tools
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +01001111cleanup
David Horstmann76a77382023-08-17 17:32:26 +01001112if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +01001113 pre_generate_files
1114fi
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +01001115
Gilles Peskinebeb3a812019-01-06 22:11:25 +00001116# Run the requested tests.
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001117for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do
Gilles Peskine5d996822020-03-28 21:09:21 +01001118 run_component pseudo_component_error_test
Gilles Peskine5d996822020-03-28 21:09:21 +01001119done
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001120unset error_test_i
Gilles Peskinebeb3a812019-01-06 22:11:25 +00001121for component in $RUN_COMPONENTS; do
1122 run_component "component_$component"
1123done
Gilles Peskine8f073122018-11-27 15:58:47 +01001124
1125# We're done.
Gilles Peskine878cf602019-01-06 20:50:38 +00001126post_report