blob: c94a51e632837f296ae1f837a5c53b7bb15ef5fd [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 Cronc5e81d22024-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'
Ronald Cron90279552024-07-04 09:25:59 +0200136 if [ -d tf-psa-crypto ]; then
137 CRYPTO_CONFIG_H='tf-psa-crypto/include/psa/crypto_config.h'
138 PSA_CORE_PATH='tf-psa-crypto/core'
139 BUILTIN_SRC_PATH='tf-psa-crypto/drivers/builtin/src'
140 else
141 CRYPTO_CONFIG_H='include/psa/crypto_config.h'
142 fi
David Horstmann76a77382023-08-17 17:32:26 +0100143 else
144 CONFIG_H='drivers/builtin/include/mbedtls/mbedtls_config.h'
Ronald Cron7e5d61c2024-06-10 14:25:46 +0200145 CRYPTO_CONFIG_H='include/psa/crypto_config.h'
Ronald Cron90279552024-07-04 09:25:59 +0200146 PSA_CORE_PATH='core'
147 BUILTIN_SRC_PATH='drivers/builtin/src'
David Horstmann9a6c45b2023-07-14 12:30:00 +0100148 fi
Manuel Pégourié-Gonnard462e3a92022-12-27 12:35:11 +0100149 CONFIG_TEST_DRIVER_H='tests/include/test/drivers/config_test_driver.h'
Gilles Peskinef83eb822020-03-30 20:11:39 +0200150
151 # Files that are clobbered by some jobs will be backed up. Use a different
152 # suffix from auxiliary scripts so that all.sh and auxiliary scripts can
153 # independently decide when to remove the backup file.
154 backup_suffix='.all.bak'
155 # Files clobbered by config.py
Janos Follath2f045822023-07-31 10:57:16 +0100156 files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H $CONFIG_TEST_DRIVER_H"
David Horstmann76a77382023-08-17 17:32:26 +0100157 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100158 # Files clobbered by in-tree cmake
159 files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile"
160 fi
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200161
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200162 append_outcome=0
Gilles Peskine8f073122018-11-27 15:58:47 +0100163 MEMORY=0
164 FORCE=0
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200165 QUIET=0
Gilles Peskine8f073122018-11-27 15:58:47 +0100166 KEEP_GOING=0
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100167
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200168 # Seed value used with the --release-test option.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +0200169 #
170 # See also RELEASE_SEED in basic-build-test.sh. Debugging is easier if
171 # both values are kept in sync. If you change the value here because it
172 # breaks some tests, you'll definitely want to change it in
173 # basic-build-test.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200174 RELEASE_SEED=1
175
Yanray Wang18040ed2023-11-23 21:29:56 +0800176 # Specify character collation for regular expressions and sorting with C locale
177 export LC_COLLATE=C
178
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200179 : ${MBEDTLS_TEST_OUTCOME_FILE=}
Gilles Peskine9004a172019-09-16 15:20:36 +0200180 : ${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 +0200181 export MBEDTLS_TEST_OUTCOME_FILE
Gilles Peskine9004a172019-09-16 15:20:36 +0200182 export MBEDTLS_TEST_PLATFORM
183
Jaeden Ameroc4cc2512019-01-30 15:35:44 +0000184 # Default commands, can be overridden by the environment
Gilles Peskine8f073122018-11-27 15:58:47 +0100185 : ${OPENSSL:="openssl"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100186 : ${OPENSSL_NEXT:="$OPENSSL"}
187 : ${GNUTLS_CLI:="gnutls-cli"}
188 : ${GNUTLS_SERV:="gnutls-serv"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100189 : ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
190 : ${ARMC5_BIN_DIR:=/usr/bin}
191 : ${ARMC6_BIN_DIR:=/usr/bin}
Gilles Peskine6d061342020-04-30 18:19:32 +0200192 : ${ARM_NONE_EABI_GCC_PREFIX:=arm-none-eabi-}
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200193 : ${ARM_LINUX_GNUEABI_GCC_PREFIX:=arm-linux-gnueabi-}
Gowtham Suresh Kumar9da40b82023-07-31 16:38:10 +0100194 : ${CLANG_LATEST:="clang-latest"}
195 : ${CLANG_EARLIEST:="clang-earliest"}
196 : ${GCC_LATEST:="gcc-latest"}
197 : ${GCC_EARLIEST:="gcc-earliest"}
Gilles Peskine8f073122018-11-27 15:58:47 +0100198 # if MAKEFLAGS is not set add the -j option to speed up invocations of make
Gilles Peskinea1fc4b52019-01-06 20:15:26 +0000199 if [ -z "${MAKEFLAGS+set}" ]; then
Gilles Peskine050d2fc2021-09-30 18:24:21 +0200200 export MAKEFLAGS="-j$(all_sh_nproc)"
Gilles Peskine8f073122018-11-27 15:58:47 +0100201 fi
Dave Rodgman0c5bfe82023-12-18 19:53:25 +0000202 # if CC is not set, use clang by default (if present) to improve build times
203 if [ -z "${CC+set}" ] && (type clang > /dev/null 2>&1); then
Dave Rodgmanfc5b9552023-12-19 16:08:19 +0000204 export CC="clang"
Dave Rodgman66cbc832023-12-18 18:34:50 +0000205 fi
Gilles Peskine878cf602019-01-06 20:50:38 +0000206
Ronald Cronc5e81d22024-04-02 14:39:53 +0200207 if [ -n "${OPENSSL_3+set}" ]; then
208 export OPENSSL_NEXT="$OPENSSL_3"
209 fi
210
Gilles Peskine396853a2021-09-20 18:57:55 +0200211 # Include more verbose output for failing tests run by CMake or make
Jaeden Amerod48e9c72019-02-07 17:43:39 +0000212 export CTEST_OUTPUT_ON_FAILURE=1
213
Gilles Peskine8fd59422019-10-21 17:11:33 +0200214 # CFLAGS and LDFLAGS for Asan builds that don't use CMake
Manuel Pégourié-Gonnardfe549a72022-11-30 10:42:03 +0100215 # default to -O2, use -Ox _after_ this if you want another level
Przemek Stekiel6efa6082023-05-31 09:38:21 +0200216 ASAN_CFLAGS='-O2 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
Dave Rodgman17127e92023-12-14 16:42:48 +0000217 # Normally, tests should use this compiler for ASAN testing
218 ASAN_CC=clang
Gilles Peskine8fd59422019-10-21 17:11:33 +0200219
Andrzej Kurek04bfe572023-06-27 10:02:09 -0400220 # Platform tests have an allocation that returns null
221 export ASAN_OPTIONS="allocator_may_return_null=1"
Andrzej Kurek026235c2023-07-05 08:32:43 -0400222 export MSAN_OPTIONS="allocator_may_return_null=1"
Andrzej Kurek04bfe572023-06-27 10:02:09 -0400223
Gilles Peskine878cf602019-01-06 20:50:38 +0000224 # Gather the list of available components. These are the functions
225 # defined in this script whose name starts with "component_".
Gowtham Suresh Kumar1e829a42023-07-28 16:41:21 +0100226 ALL_COMPONENTS=$(compgen -A function component_ | sed 's/component_//')
Gilles Peskine878cf602019-01-06 20:50:38 +0000227
Valerio Setti9022f712024-06-27 07:59:39 +0200228 PSASIM_PATH='tests/psa-client-server/psasim/'
229
Dave Rodgman5f8e2a22024-01-16 17:33:34 +0000230 # Delay determining SUPPORTED_COMPONENTS until the command line options have a chance to override
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100231 # the commands set by the environment
Gilles Peskine8f073122018-11-27 15:58:47 +0100232}
Andres AG38495a32016-07-12 16:54:33 +0100233
Dave Rodgman21900632024-02-26 11:41:19 +0000234setup_quiet_wrappers()
235{
236 # Pick up "quiet" wrappers for make and cmake, which don't output very much
237 # unless there is an error. This reduces logging overhead in the CI.
238 #
239 # Note that the cmake wrapper breaks unless we use an absolute path here.
Dave Rodgmana9e8dbe2024-02-26 17:27:18 +0000240 if [[ -e ${PWD}/tests/scripts/quiet ]]; then
241 export PATH=${PWD}/tests/scripts/quiet:$PATH
Dave Rodgman21900632024-02-26 11:41:19 +0000242 fi
243}
244
Gilles Peskinea28db922019-01-10 00:05:18 +0100245# Test whether the component $1 is included in the command line patterns.
246is_component_included()
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100247{
Gilles Peskine03af6782021-08-06 11:35:17 +0200248 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
249 # only does word splitting.
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100250 set -f
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000251 for pattern in $COMMAND_LINE_COMPONENTS; do
Gilles Peskine81b96ed2018-11-27 21:37:53 +0100252 set +f
253 case ${1#component_} in $pattern) return 0;; esac
254 done
255 set +f
256 return 1
257}
Andres AG7770ea82016-10-10 15:46:20 +0100258
Simon Butcher41eeccf2016-09-07 00:07:09 +0100259usage()
Andres AGd9eba4b2016-08-26 14:42:14 +0100260{
Gilles Peskine709346a2017-12-10 23:43:39 +0100261 cat <<EOF
Gilles Peskine92525112018-11-27 18:15:35 +0100262Usage: $0 [OPTION]... [COMPONENT]...
Gilles Peskine348fb9a2018-11-27 17:04:29 +0100263Run mbedtls release validation tests.
Gilles Peskine92525112018-11-27 18:15:35 +0100264By default, run all tests. With one or more COMPONENT, run only those.
Gilles Peskinea28db922019-01-10 00:05:18 +0100265COMPONENT can be the name of a component or a shell wildcard pattern.
266
267Examples:
268 $0 "check_*"
269 Run all sanity checks.
270 $0 --no-armcc --except test_memsan
271 Run everything except builds that require armcc and MemSan.
Gilles Peskine348fb9a2018-11-27 17:04:29 +0100272
273Special options:
274 -h|--help Print this help and exit.
Gilles Peskine878cf602019-01-06 20:50:38 +0000275 --list-all-components List all available test components and exit.
276 --list-components List components supported on this platform and exit.
Gilles Peskine709346a2017-12-10 23:43:39 +0100277
278General options:
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200279 -q|--quiet Only output component names, and errors if any.
Gilles Peskine709346a2017-12-10 23:43:39 +0100280 -f|--force Force the tests to overwrite any modified files.
Gilles Peskine7c652162017-12-11 00:01:40 +0100281 -k|--keep-going Run all tests and report errors at the end.
Gilles Peskine709346a2017-12-10 23:43:39 +0100282 -m|--memory Additional optional memory tests.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200283 --append-outcome Append to the outcome file (if used).
Gilles Peskine6d061342020-04-30 18:19:32 +0200284 --arm-none-eabi-gcc-prefix=<string>
285 Prefix for a cross-compiler for arm-none-eabi
286 (default: "${ARM_NONE_EABI_GCC_PREFIX}")
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200287 --arm-linux-gnueabi-gcc-prefix=<string>
288 Prefix for a cross-compiler for arm-linux-gnueabi
289 (default: "${ARM_LINUX_GNUEABI_GCC_PREFIX}")
Gilles Peskinebca6ab92017-12-19 18:24:31 +0100290 --armcc Run ARM Compiler builds (on by default).
Gilles Peskine80ddb992021-08-06 11:51:59 +0200291 --restore First clean up the build tree, restoring backed up
292 files. Do not run any components unless they are
293 explicitly specified.
Gilles Peskine5d996822020-03-28 21:09:21 +0100294 --error-test Error test mode: run a failing function in addition
Gilles Peskine86f61292021-08-05 15:11:33 +0200295 to any specified component. May be repeated.
Gilles Peskinea28db922019-01-10 00:05:18 +0100296 --except Exclude the COMPONENTs listed on the command line,
297 instead of running only those.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200298 --no-append-outcome Write a new outcome file and analyze it (default).
Gilles Peskinebca6ab92017-12-19 18:24:31 +0100299 --no-armcc Skip ARM Compiler builds.
Gilles Peskine38d81652018-03-21 08:40:26 +0100300 --no-force Refuse to overwrite modified files (default).
301 --no-keep-going Stop at the first error (default).
302 --no-memory No additional memory tests (default).
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800303 --no-quiet Print full output from components.
Gilles Peskine709346a2017-12-10 23:43:39 +0100304 --out-of-source-dir=<path> Directory used for CMake out-of-source build tests.
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200305 --outcome-file=<path> File where test outcomes are written (not done if
306 empty; default: \$MBEDTLS_TEST_OUTCOME_FILE).
Gilles Peskine38d81652018-03-21 08:40:26 +0100307 --random-seed Use a random seed value for randomized tests (default).
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200308 -r|--release-test Run this script in release mode. This fixes the seed value to ${RELEASE_SEED}.
Gilles Peskine709346a2017-12-10 23:43:39 +0100309 -s|--seed Integer seed value to use for this test run.
310
311Tool path options:
312 --armc5-bin-dir=<ARMC5_bin_dir_path> ARM Compiler 5 bin directory.
313 --armc6-bin-dir=<ARMC6_bin_dir_path> ARM Compiler 6 bin directory.
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100314 --clang-earliest=<Clang_earliest_path> Earliest version of clang available
315 --clang-latest=<Clang_latest_path> Latest version of clang available
316 --gcc-earliest=<GCC_earliest_path> Earliest version of GCC available
317 --gcc-latest=<GCC_latest_path> Latest version of GCC available
Gilles Peskine709346a2017-12-10 23:43:39 +0100318 --gnutls-cli=<GnuTLS_cli_path> GnuTLS client executable to use for most tests.
319 --gnutls-serv=<GnuTLS_serv_path> GnuTLS server executable to use for most tests.
Gilles Peskine709346a2017-12-10 23:43:39 +0100320 --openssl=<OpenSSL_path> OpenSSL executable to use for most tests.
Manuel Pégourié-Gonnard6b368922018-02-20 12:02:07 +0100321 --openssl-next=<OpenSSL_path> OpenSSL executable to use for recent things like ARIA
Gilles Peskine709346a2017-12-10 23:43:39 +0100322EOF
SimonB2e23c822016-04-16 21:54:39 +0100323}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100324
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200325# Cleanup before/after running a component.
326# Remove built files as well as the cmake cache/config.
327# Does not remove generated source files.
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100328cleanup()
329{
David Horstmann76a77382023-08-17 17:32:26 +0100330 if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100331 command make clean
332 fi
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200333
Gilles Peskine31b07e22018-03-21 12:15:06 +0100334 # Remove CMake artefacts
Jaeden Amero2d0e00f2018-11-07 18:46:41 +0000335 find . -name .git -prune -o \
Gilles Peskine31b07e22018-03-21 12:15:06 +0100336 -iname CMakeFiles -exec rm -rf {} \+ -o \
337 \( -iname cmake_install.cmake -o \
338 -iname CTestTestfile.cmake -o \
Manuel Pégourié-Gonnardbfe54d72021-09-09 11:11:44 +0200339 -iname CMakeCache.txt -o \
340 -path './cmake/*.cmake' \) -exec rm -f {} \+
Ronald Cron5cdc22b2024-06-12 10:04:22 +0200341 # Remove Makefiles generated by in-tree CMake builds
Ronald Cronf37ff7f2024-07-02 07:36:39 +0200342 rm -f pkgconfig/Makefile framework/Makefile
Ronald Cron5cdc22b2024-06-12 10:04:22 +0200343 rm -f include/Makefile programs/!(fuzz)/Makefile
Ronald Cron30ea2b92024-06-11 14:07:09 +0200344 rm -f tf-psa-crypto/Makefile tf-psa-crypto/include/Makefile
Ronald Cron82803982024-07-02 08:40:24 +0200345 rm -f tf-psa-crypto/core/Makefile tf-psa-crypto/drivers/Makefile
Ronald Cron635a2be2024-07-12 14:30:16 +0200346 rm -f tf-psa-crypto/tests/Makefile
Ronald Cronf37ff7f2024-07-02 07:36:39 +0200347 rm -f tf-psa-crypto/drivers/everest/Makefile
348 rm -f tf-psa-crypto/drivers/p256-m/Makefile
Ronald Cron82803982024-07-02 08:40:24 +0200349 rm -f tf-psa-crypto/drivers/builtin/Makefile
350 rm -f tf-psa-crypto/drivers/builtin/src/Makefile
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200351
Jaeden Ameroab83fdf2019-06-20 17:38:22 +0100352 # Remove any artifacts from the component_test_cmake_as_subdirectory test.
353 rm -rf programs/test/cmake_subproject/build
354 rm -f programs/test/cmake_subproject/Makefile
355 rm -f programs/test/cmake_subproject/cmake_subproject
356
Chris Kayd259e342021-03-25 16:03:25 +0000357 # Remove any artifacts from the component_test_cmake_as_package test.
358 rm -rf programs/test/cmake_package/build
359 rm -f programs/test/cmake_package/Makefile
360 rm -f programs/test/cmake_package/cmake_package
361
362 # Remove any artifacts from the component_test_cmake_as_installed_package test.
363 rm -rf programs/test/cmake_package_install/build
364 rm -f programs/test/cmake_package_install/Makefile
365 rm -f programs/test/cmake_package_install/cmake_package_install
366
Gilles Peskinef83eb822020-03-30 20:11:39 +0200367 # Restore files that may have been clobbered by the job
368 for x in $files_to_back_up; do
Gilles Peskinedd06efb2022-08-30 21:02:00 +0200369 if [[ -e "$x$backup_suffix" ]]; then
370 cp -p "$x$backup_suffix" "$x"
371 fi
Gilles Peskinef83eb822020-03-30 20:11:39 +0200372 done
373}
John Durkopbd069d32020-10-31 22:14:03 -0700374
Valerio Setti9022f712024-06-27 07:59:39 +0200375# This is a helper function to be used in psasim builds. It is meant to clean
376# up the library's workspace after the server build and before the client
377# build. Built libraries (mbedcrypto, mbedx509 and mbedtls) are supposed to be
378# already copied to psasim folder at this point.
Valerio Settid8b59372024-07-02 12:02:25 +0200379helper_psasim_cleanup_before_client() {
Valerio Setti9022f712024-06-27 07:59:39 +0200380 # Clean up library files
381 make -C library clean
382 # Clean up intermediate files that were used to build the server
383 make -C $PSASIM_PATH clean_server_intermediate_files
384 # Restore files that were backup before building library files. This
385 # includes $CONFIG_H and $CRYPTO_CONFIG_H.
386 for x in $files_to_back_up; do
387 if [[ -e "$x$backup_suffix" ]]; then
388 cp -p "$x$backup_suffix" "$x"
389 fi
390 done
391}
392
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200393# Final cleanup when this script exits (except when exiting on a failure
394# in non-keep-going mode).
Gilles Peskinef83eb822020-03-30 20:11:39 +0200395final_cleanup () {
396 cleanup
397
398 for x in $files_to_back_up; do
399 rm -f "$x$backup_suffix"
400 done
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100401}
402
Gilles Peskine7c652162017-12-11 00:01:40 +0100403# Executed on exit. May be redefined depending on command line options.
404final_report () {
405 :
406}
407
408fatal_signal () {
Gilles Peskinef83eb822020-03-30 20:11:39 +0200409 final_cleanup
Gilles Peskine7c652162017-12-11 00:01:40 +0100410 final_report $1
411 trap - $1
412 kill -$1 $$
413}
414
415trap 'fatal_signal HUP' HUP
416trap 'fatal_signal INT' INT
417trap 'fatal_signal TERM' TERM
Manuel Pégourié-Gonnarde73b2632014-07-12 04:00:00 +0200418
Gilles Peskine050d2fc2021-09-30 18:24:21 +0200419# Number of processors on this machine. Used as the default setting
420# for parallel make.
421all_sh_nproc ()
422{
423 {
424 nproc || # Linux
425 sysctl -n hw.ncpuonline || # NetBSD, OpenBSD
426 sysctl -n hw.ncpu || # FreeBSD
427 echo 1
428 } 2>/dev/null
429}
430
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100431msg()
432{
Gilles Peskineffcdeff2018-12-04 12:49:28 +0100433 if [ -n "${current_component:-}" ]; then
434 current_section="${current_component#component_}: $1"
435 else
436 current_section="$1"
437 fi
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200438
439 if [ $QUIET -eq 1 ]; then
440 return
441 fi
442
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100443 echo ""
444 echo "******************************************************************"
Gilles Peskineffcdeff2018-12-04 12:49:28 +0100445 echo "* $current_section "
Manuel Pégourié-Gonnard392d3dd2015-01-26 14:03:56 +0000446 printf "* "; date
Manuel Pégourié-Gonnard3895f5a2014-03-27 14:44:04 +0100447 echo "******************************************************************"
448}
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +0100449
Gilles Peskine8f073122018-11-27 15:58:47 +0100450armc6_build_test()
451{
452 FLAGS="$1"
Andres AGa5cd9732016-10-17 15:23:10 +0100453
Gilles Peskine18487f62020-04-30 23:11:54 +0200454 msg "build: ARM Compiler 6 ($FLAGS)"
Yanray Wangbc7716c2023-09-08 11:20:59 +0800455 make clean
Gilles Peskine8f073122018-11-27 15:58:47 +0100456 ARM_TOOL_VARIANT="ult" CC="$ARMC6_CC" AR="$ARMC6_AR" CFLAGS="$FLAGS" \
Dave Rodgman1c232a82023-03-02 13:39:04 +0000457 WARNING_CFLAGS='-Werror -xc -std=c99' make lib
Gilles Peskine18487f62020-04-30 23:11:54 +0200458
459 msg "size: ARM Compiler 6 ($FLAGS)"
460 "$ARMC6_FROMELF" -z library/*.o
Ronald Cron90279552024-07-04 09:25:59 +0200461 if [ -n ${PSA_CORE_PATH} ]; then
462 "$ARMC6_FROMELF" -z ${PSA_CORE_PATH}/*.o
463 fi
464 if [ -n ${BUILTIN_SRC_PATH} ]; then
465 "$ARMC6_FROMELF" -z ${BUILTIN_SRC_PATH}/*.o
466 fi
Gilles Peskine8f073122018-11-27 15:58:47 +0100467}
Andres AGa5cd9732016-10-17 15:23:10 +0100468
Andres AGd9eba4b2016-08-26 14:42:14 +0100469err_msg()
470{
471 echo "$1" >&2
472}
473
474check_tools()
475{
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200476 for tool in "$@"; do
477 if ! `type "$tool" >/dev/null 2>&1`; then
478 err_msg "$tool not found!"
Andres AGd9eba4b2016-08-26 14:42:14 +0100479 exit 1
480 fi
481 done
482}
483
Gilles Peskine8f073122018-11-27 15:58:47 +0100484pre_parse_command_line () {
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000485 COMMAND_LINE_COMPONENTS=
Gilles Peskinea28db922019-01-10 00:05:18 +0100486 all_except=0
Gilles Peskine5d996822020-03-28 21:09:21 +0100487 error_test=0
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100488 list_components=0
Gilles Peskine80ddb992021-08-06 11:51:59 +0200489 restore_first=0
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000490 no_armcc=
SimonB2e23c822016-04-16 21:54:39 +0100491
Jaeden Amero9b90f2e2018-11-02 18:34:17 +0000492 # Note that legacy options are ignored instead of being omitted from this
493 # list of options, so invocations that worked with previous version of
494 # all.sh will still run and work properly.
Gilles Peskine8f073122018-11-27 15:58:47 +0100495 while [ $# -gt 0 ]; do
Gilles Peskine55f7c942019-01-09 22:28:21 +0100496 case "$1" in
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200497 --append-outcome) append_outcome=1;;
Gilles Peskine6d061342020-04-30 18:19:32 +0200498 --arm-none-eabi-gcc-prefix) shift; ARM_NONE_EABI_GCC_PREFIX="$1";;
Manuel Pégourié-Gonnard3a6c7692020-08-18 10:28:51 +0200499 --arm-linux-gnueabi-gcc-prefix) shift; ARM_LINUX_GNUEABI_GCC_PREFIX="$1";;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000500 --armcc) no_armcc=;;
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100501 --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";;
502 --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100503 --clang-earliest) shift; CLANG_EARLIEST="$1";;
504 --clang-latest) shift; CLANG_LATEST="$1";;
Gilles Peskine5d996822020-03-28 21:09:21 +0100505 --error-test) error_test=$((error_test + 1));;
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000506 --except) all_except=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100507 --force|-f) FORCE=1;;
Gowtham Suresh Kumar6f1977b2023-07-28 17:04:47 +0100508 --gcc-earliest) shift; GCC_EARLIEST="$1";;
509 --gcc-latest) shift; GCC_LATEST="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100510 --gnutls-cli) shift; GNUTLS_CLI="$1";;
Gilles Peskine7be571a2023-08-27 21:39:21 +0200511 --gnutls-legacy-cli) shift;; # ignored for backward compatibility
512 --gnutls-legacy-serv) shift;; # ignored for backward compatibility
Gilles Peskine55f7c942019-01-09 22:28:21 +0100513 --gnutls-serv) shift; GNUTLS_SERV="$1";;
514 --help|-h) usage; exit;;
515 --keep-going|-k) KEEP_GOING=1;;
Gilles Peskine878cf602019-01-06 20:50:38 +0000516 --list-all-components) printf '%s\n' $ALL_COMPONENTS; exit;;
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100517 --list-components) list_components=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100518 --memory|-m) MEMORY=1;;
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200519 --no-append-outcome) append_outcome=0;;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000520 --no-armcc) no_armcc=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100521 --no-force) FORCE=0;;
522 --no-keep-going) KEEP_GOING=0;;
523 --no-memory) MEMORY=0;;
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200524 --no-quiet) QUIET=0;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100525 --openssl) shift; OPENSSL="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100526 --openssl-next) shift; OPENSSL_NEXT="$1";;
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200527 --outcome-file) shift; MBEDTLS_TEST_OUTCOME_FILE="$1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100528 --out-of-source-dir) shift; OUT_OF_SOURCE_DIR="$1";;
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200529 --quiet|-q) QUIET=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100530 --random-seed) unset SEED;;
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +0200531 --release-test|-r) SEED=$RELEASE_SEED;;
Gilles Peskine80ddb992021-08-06 11:51:59 +0200532 --restore) restore_first=1;;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100533 --seed|-s) shift; SEED="$1";;
534 -*)
535 echo >&2 "Unknown option: $1"
536 echo >&2 "Run $0 --help for usage."
537 exit 120
538 ;;
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000539 *) COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS $1";;
Gilles Peskine55f7c942019-01-09 22:28:21 +0100540 esac
541 shift
Gilles Peskine8f073122018-11-27 15:58:47 +0100542 done
SimonB2e23c822016-04-16 21:54:39 +0100543
Bence Szépkúti71c71eb2023-12-15 19:20:31 +0100544 # Exclude components that are not supported on this platform.
545 SUPPORTED_COMPONENTS=
546 for component in $ALL_COMPONENTS; do
547 case $(type "support_$component" 2>&1) in
548 *' function'*)
549 if ! support_$component; then continue; fi;;
550 esac
551 SUPPORTED_COMPONENTS="$SUPPORTED_COMPONENTS $component"
552 done
553
554 if [ $list_components -eq 1 ]; then
555 printf '%s\n' $SUPPORTED_COMPONENTS
556 exit
557 fi
558
Gilles Peskinea28db922019-01-10 00:05:18 +0100559 # With no list of components, run everything.
Gilles Peskine80ddb992021-08-06 11:51:59 +0200560 if [ -z "$COMMAND_LINE_COMPONENTS" ] && [ $restore_first -eq 0 ]; then
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000561 all_except=1
Andres AGdc192212016-08-31 17:33:13 +0100562 fi
563
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000564 # --no-armcc is a legacy option. The modern way is --except '*_armcc*'.
565 # Ignore it if components are listed explicitly on the command line.
Gilles Peskinea28db922019-01-10 00:05:18 +0100566 if [ -n "$no_armcc" ] && [ $all_except -eq 1 ]; then
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000567 COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*"
SimonB2e23c822016-04-16 21:54:39 +0100568 fi
SimonB2e23c822016-04-16 21:54:39 +0100569
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200570 # Error out if an explicitly requested component doesn't exist.
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100571 if [ $all_except -eq 0 ]; then
572 unsupported=0
Gilles Peskine03af6782021-08-06 11:35:17 +0200573 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
574 # only does word splitting.
Gilles Peskine1d475b62021-08-03 13:43:36 +0200575 set -f
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100576 for component in $COMMAND_LINE_COMPONENTS; do
Gilles Peskine1d475b62021-08-03 13:43:36 +0200577 set +f
Gilles Peskinebf66e2c2021-08-03 13:44:28 +0200578 # If the requested name includes a wildcard character, don't
579 # check it. Accept wildcard patterns that don't match anything.
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100580 case $component in
581 *[*?\[]*) continue;;
582 esac
583 case " $SUPPORTED_COMPONENTS " in
584 *" $component "*) :;;
585 *)
586 echo >&2 "Component $component was explicitly requested, but is not known or not supported."
587 unsupported=$((unsupported + 1));;
588 esac
589 done
Gilles Peskine1d475b62021-08-03 13:43:36 +0200590 set +f
Gilles Peskineb80f0d22020-03-28 21:37:59 +0100591 if [ $unsupported -ne 0 ]; then
592 exit 2
593 fi
594 fi
595
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000596 # Build the list of components to run.
Gilles Peskinea28db922019-01-10 00:05:18 +0100597 RUN_COMPONENTS=
598 for component in $SUPPORTED_COMPONENTS; do
599 if is_component_included "$component"; [ $? -eq $all_except ]; then
600 RUN_COMPONENTS="$RUN_COMPONENTS $component"
601 fi
602 done
Gilles Peskinebeb3a812019-01-06 22:11:25 +0000603
604 unset all_except
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000605 unset no_armcc
Gilles Peskine8f073122018-11-27 15:58:47 +0100606}
SimonB2e23c822016-04-16 21:54:39 +0100607
Gilles Peskine8f073122018-11-27 15:58:47 +0100608pre_check_git () {
609 if [ $FORCE -eq 1 ]; then
Gilles Peskine53190e62019-01-09 23:17:35 +0100610 rm -rf "$OUT_OF_SOURCE_DIR"
Gilles Peskine8f073122018-11-27 15:58:47 +0100611 git checkout-index -f -q $CONFIG_H
612 cleanup
613 else
SimonB2e23c822016-04-16 21:54:39 +0100614
Gilles Peskine8f073122018-11-27 15:58:47 +0100615 if [ -d "$OUT_OF_SOURCE_DIR" ]; then
616 echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
617 echo "You can either delete this directory manually, or force the test by rerunning"
618 echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
619 exit 1
620 fi
621
Ronald Cron7a93ac52023-07-20 09:49:12 +0200622 if ! git diff --quiet "$CONFIG_H"; then
David Horstmann9a6c45b2023-07-14 12:30:00 +0100623 err_msg "Warning - the configuration file '$CONFIG_H' has been edited. "
Gilles Peskine8f073122018-11-27 15:58:47 +0100624 echo "You can either delete or preserve your work, or force the test by rerunning the"
625 echo "script as: $0 --force"
626 exit 1
627 fi
SimonB2e23c822016-04-16 21:54:39 +0100628 fi
Andrzej Kurekeb508712019-02-14 07:18:59 -0500629}
630
Gilles Peskine568f53a2021-07-12 18:16:01 +0200631pre_restore_files () {
632 # If the makefiles have been generated by a framework such as cmake,
633 # restore them from git. If the makefiles look like modifications from
634 # the ones checked into git, take care not to modify them. Whatever
635 # this function leaves behind is what the script will restore before
636 # each component.
637 case "$(head -n1 Makefile)" in
638 *[Gg]enerated*)
639 git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
640 git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
641 ;;
642 esac
643}
644
Gilles Peskinef83eb822020-03-30 20:11:39 +0200645pre_back_up () {
646 for x in $files_to_back_up; do
647 cp -p "$x" "$x$backup_suffix"
648 done
649}
650
Gilles Peskine8f073122018-11-27 15:58:47 +0100651pre_setup_keep_going () {
Gilles Peskinece266c42020-03-28 18:50:43 +0100652 failure_count=0 # Number of failed components
653 last_failure_status=0 # Last failure status in this component
654
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100655 # See err_trap
656 previous_failure_status=0
657 previous_failed_command=
658 previous_failure_funcall_depth=0
Gilles Peskinea681c592020-03-28 21:27:40 +0100659 unset report_failed_command
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100660
Gilles Peskine7c652162017-12-11 00:01:40 +0100661 start_red=
662 end_color=
663 if [ -t 1 ]; then
Gilles Peskine9736b9d2018-01-02 21:54:17 +0100664 case "${TERM:-}" in
Gilles Peskine7c652162017-12-11 00:01:40 +0100665 *color*|cygwin|linux|rxvt*|screen|[Eex]term*)
666 start_red=$(printf '\033[31m')
667 end_color=$(printf '\033[0m')
668 ;;
669 esac
670 fi
Gilles Peskinece266c42020-03-28 18:50:43 +0100671
672 # Keep a summary of failures in a file. We'll print it out at the end.
673 failure_summary_file=$PWD/all-sh-failures-$$.log
674 : >"$failure_summary_file"
675
676 # Whether it makes sense to keep a component going after the specified
677 # command fails (test command) or not (configure or build).
Gilles Peskine03af6782021-08-06 11:35:17 +0200678 # This function normally receives the failing simple command
679 # ($BASH_COMMAND) as an argument, but if $report_failed_command is set,
680 # this is passed instead.
Gilles Peskinece266c42020-03-28 18:50:43 +0100681 # This doesn't have to be 100% accurate: all failures are recorded anyway.
Gilles Peskinec111e242021-08-02 23:29:53 +0200682 # False positives result in running things that can't be expected to
683 # work. False negatives result in things not running after something else
684 # failed even though they might have given useful feedback.
Gilles Peskinece266c42020-03-28 18:50:43 +0100685 can_keep_going_after_failure () {
686 case "$1" in
687 "msg "*) false;;
Gilles Peskinec111e242021-08-02 23:29:53 +0200688 "cd "*) false;;
Manuel Pégourié-Gonnarda7423372023-11-10 09:58:31 +0100689 "diff "*) true;;
Gilles Peskinec111e242021-08-02 23:29:53 +0200690 *make*[\ /]tests*) false;; # make tests, make CFLAGS=-I../tests, ...
691 *test*) true;; # make test, tests/stuff, env V=v tests/stuff, ...
692 *make*check*) true;;
693 "grep "*) true;;
694 "[ "*) true;;
695 "! "*) true;;
Gilles Peskinece266c42020-03-28 18:50:43 +0100696 *) false;;
Gilles Peskine7c652162017-12-11 00:01:40 +0100697 esac
698 }
Gilles Peskinece266c42020-03-28 18:50:43 +0100699
700 # This function runs if there is any error in a component.
701 # It must either exit with a nonzero status, or set
702 # last_failure_status to a nonzero value.
703 err_trap () {
704 # Save $? (status of the failing command). This must be the very
705 # first thing, before $? is overridden.
706 last_failure_status=$?
Gilles Peskinea681c592020-03-28 21:27:40 +0100707 failed_command=${report_failed_command-$BASH_COMMAND}
Gilles Peskinece266c42020-03-28 18:50:43 +0100708
Gilles Peskinefec30cb2020-03-28 19:34:23 +0100709 if [[ $last_failure_status -eq $previous_failure_status &&
710 "$failed_command" == "$previous_failed_command" &&
711 ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]]
712 then
713 # The same command failed twice in a row, but this time one level
714 # less deep in the function call stack. This happens when the last
715 # command of a function returns a nonzero status, and the function
716 # returns that same status. Ignore the second failure.
717 previous_failure_funcall_depth=${#FUNCNAME[@]}
718 return
719 fi
720 previous_failure_status=$last_failure_status
721 previous_failed_command=$failed_command
722 previous_failure_funcall_depth=${#FUNCNAME[@]}
723
Gilles Peskinece266c42020-03-28 18:50:43 +0100724 text="$current_section: $failed_command -> $last_failure_status"
725 echo "${start_red}^^^^$text^^^^${end_color}" >&2
726 echo "$text" >>"$failure_summary_file"
727
728 # If the command is fatal (configure or build command), stop this
729 # component. Otherwise (test command) keep the component running
730 # (run more tests from the same build).
731 if ! can_keep_going_after_failure "$failed_command"; then
732 exit $last_failure_status
733 fi
734 }
735
Gilles Peskine7c652162017-12-11 00:01:40 +0100736 final_report () {
737 if [ $failure_count -gt 0 ]; then
738 echo
739 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
Gilles Peskinece266c42020-03-28 18:50:43 +0100740 echo "${start_red}FAILED: $failure_count components${end_color}"
741 cat "$failure_summary_file"
Gilles Peskine7c652162017-12-11 00:01:40 +0100742 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
743 elif [ -z "${1-}" ]; then
744 echo "SUCCESS :)"
745 fi
746 if [ -n "${1-}" ]; then
747 echo "Killed by SIG$1."
748 fi
Gilles Peskinece266c42020-03-28 18:50:43 +0100749 rm -f "$failure_summary_file"
750 if [ $failure_count -gt 0 ]; then
751 exit 1
752 fi
Gilles Peskine7c652162017-12-11 00:01:40 +0100753 }
Gilles Peskine8f073122018-11-27 15:58:47 +0100754}
755
Gilles Peskine1f0cdaf2021-07-08 18:41:16 +0200756# record_status() and if_build_succeeded() are kept temporarily for backward
757# compatibility. Don't use them in new components.
Gilles Peskinece266c42020-03-28 18:50:43 +0100758record_status () {
759 "$@"
Gilles Peskine7c652162017-12-11 00:01:40 +0100760}
Gilles Peskine7c652162017-12-11 00:01:40 +0100761if_build_succeeded () {
Gilles Peskinece266c42020-03-28 18:50:43 +0100762 "$@"
Gilles Peskine7c652162017-12-11 00:01:40 +0100763}
764
Gilles Peskinea681c592020-03-28 21:27:40 +0100765# '! true' does not trigger the ERR trap. Arrange to trigger it, with
766# a reasonably informative error message (not just "$@").
767not () {
768 if "$@"; then
769 report_failed_command="! $*"
770 false
771 unset report_failed_command
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200772 fi
773}
774
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200775pre_prepare_outcome_file () {
776 case "$MBEDTLS_TEST_OUTCOME_FILE" in
777 [!/]*) MBEDTLS_TEST_OUTCOME_FILE="$PWD/$MBEDTLS_TEST_OUTCOME_FILE";;
778 esac
779 if [ -n "$MBEDTLS_TEST_OUTCOME_FILE" ] && [ "$append_outcome" -eq 0 ]; then
780 rm -f "$MBEDTLS_TEST_OUTCOME_FILE"
781 fi
782}
783
Gilles Peskine8f073122018-11-27 15:58:47 +0100784pre_print_configuration () {
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200785 if [ $QUIET -eq 1 ]; then
786 return
787 fi
788
Gilles Peskine8f073122018-11-27 15:58:47 +0100789 msg "info: $0 configuration"
790 echo "MEMORY: $MEMORY"
791 echo "FORCE: $FORCE"
Gilles Peskine67ffdaf2019-09-16 15:55:46 +0200792 echo "MBEDTLS_TEST_OUTCOME_FILE: ${MBEDTLS_TEST_OUTCOME_FILE:-(none)}"
Gilles Peskine8f073122018-11-27 15:58:47 +0100793 echo "SEED: ${SEED-"UNSET"}"
Gilles Peskine636c26a2020-03-04 20:46:15 +0100794 echo
Gilles Peskine8f073122018-11-27 15:58:47 +0100795 echo "OPENSSL: $OPENSSL"
Gilles Peskine8f073122018-11-27 15:58:47 +0100796 echo "OPENSSL_NEXT: $OPENSSL_NEXT"
797 echo "GNUTLS_CLI: $GNUTLS_CLI"
798 echo "GNUTLS_SERV: $GNUTLS_SERV"
Gilles Peskine8f073122018-11-27 15:58:47 +0100799 echo "ARMC5_BIN_DIR: $ARMC5_BIN_DIR"
800 echo "ARMC6_BIN_DIR: $ARMC6_BIN_DIR"
801}
Andres AG7770ea82016-10-10 15:46:20 +0100802
Andres AGd9eba4b2016-08-26 14:42:14 +0100803# Make sure the tools we need are available.
Gilles Peskine8f073122018-11-27 15:58:47 +0100804pre_check_tools () {
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000805 # Build the list of variables to pass to output_env.sh.
806 set env
SimonB2e23c822016-04-16 21:54:39 +0100807
Gilles Peskine87964262019-01-06 22:40:00 +0000808 case " $RUN_COMPONENTS " in
809 # Require OpenSSL and GnuTLS if running any tests (as opposed to
810 # only doing builds). Not all tests run OpenSSL and GnuTLS, but this
811 # is a good enough approximation in practice.
Bence Szépkútibbb5af92023-12-15 20:58:15 +0100812 *" test_"* | *" release_test_"*)
Gilles Peskine87964262019-01-06 22:40:00 +0000813 # To avoid setting OpenSSL and GnuTLS for each call to compat.sh
814 # and ssl-opt.sh, we just export the variables they require.
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +0100815 export OPENSSL="$OPENSSL"
Gilles Peskine87964262019-01-06 22:40:00 +0000816 export GNUTLS_CLI="$GNUTLS_CLI"
817 export GNUTLS_SERV="$GNUTLS_SERV"
818 # Avoid passing --seed flag in every call to ssl-opt.sh
819 if [ -n "${SEED-}" ]; then
820 export SEED
821 fi
Gilles Peskine7be571a2023-08-27 21:39:21 +0200822 set "$@" OPENSSL="$OPENSSL"
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000823 set "$@" GNUTLS_CLI="$GNUTLS_CLI" GNUTLS_SERV="$GNUTLS_SERV"
Gilles Peskine7be571a2023-08-27 21:39:21 +0200824 check_tools "$OPENSSL" "$OPENSSL_NEXT" \
825 "$GNUTLS_CLI" "$GNUTLS_SERV"
Gilles Peskine87964262019-01-06 22:40:00 +0000826 ;;
827 esac
Manuel Pégourié-Gonnard57255b12014-06-09 11:21:49 +0200828
Gilles Peskine87964262019-01-06 22:40:00 +0000829 case " $RUN_COMPONENTS " in
830 *_doxygen[_\ ]*) check_tools "doxygen" "dot";;
831 esac
Andres AGd9eba4b2016-08-26 14:42:14 +0100832
Gilles Peskine87964262019-01-06 22:40:00 +0000833 case " $RUN_COMPONENTS " in
Gilles Peskine6d061342020-04-30 18:19:32 +0200834 *_arm_none_eabi_gcc[_\ ]*) check_tools "${ARM_NONE_EABI_GCC_PREFIX}gcc";;
Gilles Peskine87964262019-01-06 22:40:00 +0000835 esac
Andres AGd9eba4b2016-08-26 14:42:14 +0100836
Gilles Peskine87964262019-01-06 22:40:00 +0000837 case " $RUN_COMPONENTS " in
838 *_mingw[_\ ]*) check_tools "i686-w64-mingw32-gcc";;
839 esac
840
841 case " $RUN_COMPONENTS " in
842 *" test_zeroize "*) check_tools "gdb";;
843 esac
844
845 case " $RUN_COMPONENTS " in
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000846 *_armcc*)
Gilles Peskine87964262019-01-06 22:40:00 +0000847 ARMC5_CC="$ARMC5_BIN_DIR/armcc"
848 ARMC5_AR="$ARMC5_BIN_DIR/armar"
Gilles Peskine18487f62020-04-30 23:11:54 +0200849 ARMC5_FROMELF="$ARMC5_BIN_DIR/fromelf"
Gilles Peskine87964262019-01-06 22:40:00 +0000850 ARMC6_CC="$ARMC6_BIN_DIR/armclang"
851 ARMC6_AR="$ARMC6_BIN_DIR/armar"
Gilles Peskine18487f62020-04-30 23:11:54 +0200852 ARMC6_FROMELF="$ARMC6_BIN_DIR/fromelf"
853 check_tools "$ARMC5_CC" "$ARMC5_AR" "$ARMC5_FROMELF" \
854 "$ARMC6_CC" "$ARMC6_AR" "$ARMC6_FROMELF";;
Gilles Peskine5331c6e2019-01-06 22:23:42 +0000855 esac
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000856
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +0200857 # past this point, no call to check_tool, only printing output
858 if [ $QUIET -eq 1 ]; then
859 return
860 fi
861
Gilles Peskinecc9f0b92019-01-06 22:46:21 +0000862 msg "info: output_env.sh"
863 case $RUN_COMPONENTS in
864 *_armcc*)
865 set "$@" ARMC5_CC="$ARMC5_CC" ARMC6_CC="$ARMC6_CC" RUN_ARMCC=1;;
866 *) set "$@" RUN_ARMCC=0;;
867 esac
868 "$@" scripts/output_env.sh
Gilles Peskine8f073122018-11-27 15:58:47 +0100869}
Manuel Pégourié-Gonnard9bda9b32014-11-20 13:10:22 +0100870
Gilles Peskine1570b592021-04-22 01:10:12 +0200871pre_generate_files() {
Manuel Pégourié-Gonnard87db8a22021-06-18 13:30:14 +0200872 # since make doesn't have proper dependencies, remove any possibly outdate
873 # file that might be around before generating fresh ones
874 make neat
Gilles Peskine72385032021-07-08 19:07:07 +0200875 if [ $QUIET -eq 1 ]; then
Gilles Peskine75301632021-08-05 15:10:47 +0200876 make generated_files >/dev/null
Gilles Peskine72385032021-07-08 19:07:07 +0200877 else
878 make generated_files
879 fi
Gilles Peskine1570b592021-04-22 01:10:12 +0200880}
881
Tom Cosgroved4c373a2024-01-30 13:56:38 +0000882clang_version() {
883 if command -v clang > /dev/null ; then
884 clang --version|grep version|sed -E 's#.*version ([0-9]+).*#\1#'
885 else
886 echo 0 # report version 0 for "no clang"
887 fi
888}
889
Manuel Pégourié-Gonnard27e17672023-05-25 09:39:08 +0200890################################################################
891#### Helpers for components using libtestdriver1
892################################################################
Gilles Peskine192c72f2017-12-21 15:59:21 +0100893
Manuel Pégourié-Gonnardf5c5d972023-06-06 11:14:57 +0200894# How to use libtestdriver1
895# -------------------------
896#
897# 1. Define the list algorithms and key types to accelerate,
898# designated the same way as PSA_WANT_ macros but without PSA_WANT_.
899# Examples:
900# - loc_accel_list="ALG_JPAKE"
901# - loc_accel_list="ALG_FFDH KEY_TYPE_DH_KEY_PAIR KEY_TYPE_DH_PUBLIC_KEY"
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200902# 2. Make configurations changes for the driver and/or main libraries.
903# 2a. Call helper_libtestdriver1_adjust_config <base>, where the argument
904# can be either "default" to start with the default config, or a name
905# supported by scripts/config.py (for example, "full"). This selects
Manuel Pégourié-Gonnard2336c8e2023-06-12 17:17:54 +0200906# the base to use, and makes common adjustments.
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200907# 2b. If desired, adjust the PSA_WANT symbols in psa/crypto_config.h.
908# These changes affect both the driver and the main libraries.
909# (Note: they need to have the same set of PSA_WANT symbols, as that
910# determines the ABI between them.)
911# 2c. Adjust MBEDTLS_ symbols in mbedtls_config.h. This only affects the
912# main libraries. Typically, you want to disable the module(s) that are
913# being accelerated. You may need to also disable modules that depend
914# on them or options that are not supported with drivers.
915# 2d. On top of psa/crypto_config.h, the driver library uses its own config
916# file: tests/include/test/drivers/config_test_driver.h. You usually
917# don't need to edit it: using loc_extra_list (see below) is preferred.
918# However, when there's no PSA symbol for what you want to enable,
919# calling scripts/config.py on this file remains the only option.
920# 3. Build the driver library, then the main libraries, test, and programs.
921# 3a. Call helper_libtestdriver1_make_drivers "$loc_accel_list". You may
922# need to enable more algorithms here, typically hash algorithms when
923# accelerating some signature algorithms (ECDSA, RSAv2). This is done
924# by passing a 2nd argument listing the extra algorithms.
925# Example:
926# loc_extra_list="ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512"
927# helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
Yanray Wang0df6d962023-10-11 13:27:25 +0800928# 3b. Call helper_libtestdriver1_make_main "$loc_accel_list". Any
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200929# additional arguments will be passed to make: this can be useful if
930# you don't want to build everything when iterating during development.
931# Example:
932# helper_libtestdriver1_make_main "$loc_accel_list" -C tests test_suite_foo
933# 4. Run the tests you want.
Manuel Pégourié-Gonnardf5c5d972023-06-06 11:14:57 +0200934
Manuel Pégourié-Gonnard27e17672023-05-25 09:39:08 +0200935# Adjust the configuration - for both libtestdriver1 and main library,
936# as they should have the same PSA_WANT macros.
937helper_libtestdriver1_adjust_config() {
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200938 base_config=$1
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200939 # Select the base configuration
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +0200940 if [ "$base_config" != "default" ]; then
941 scripts/config.py "$base_config"
Manuel Pégourié-Gonnard8df87bf2023-06-12 17:09:38 +0200942 fi
943
944 # Enable PSA-based config (necessary to use drivers)
945 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
946
Manuel Pégourié-Gonnard2336c8e2023-06-12 17:17:54 +0200947 # Dynamic secure element support is a deprecated feature and needs to be disabled here.
948 # This is done to have the same form of psa_key_attributes_s for libdriver and library.
949 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
Paul Elliott053b7882024-03-05 11:46:32 +0000950
951 # If threading is enabled on the normal build, then we need to enable it in the drivers as well,
952 # otherwise we will end up running multithreaded tests without mutexes to protect them.
953 if scripts/config.py get MBEDTLS_THREADING_C; then
954 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_C
955 fi
956
957 if scripts/config.py get MBEDTLS_THREADING_PTHREAD; then
958 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_PTHREAD
959 fi
Manuel Pégourié-Gonnard27e17672023-05-25 09:39:08 +0200960}
Gilles Peskine192c72f2017-12-21 15:59:21 +0100961
Manuel Pégourié-Gonnard733a67b2023-11-15 12:32:17 +0100962# Build the drivers library libtestdriver1.a (with ASan).
963#
964# Parameters:
965# 1. a space-separated list of things to accelerate;
966# 2. optional: a space-separate list of things to also support.
967# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
968helper_libtestdriver1_make_drivers() {
969 loc_accel_flags=$( echo "$1 ${2-}" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
970 make CC=$ASAN_CC -C tests libtestdriver1.a CFLAGS=" $ASAN_CFLAGS $loc_accel_flags" LDFLAGS="$ASAN_CFLAGS"
971}
972
973# Build the main libraries, programs and tests,
974# linking to the drivers library (with ASan).
975#
976# Parameters:
977# 1. a space-separated list of things to accelerate;
978# *. remaining arguments if any are passed directly to make
979# (examples: lib, -C tests test_suite_xxx, etc.)
980# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
981helper_libtestdriver1_make_main() {
982 loc_accel_list=$1
983 shift
984
985 # we need flags both with and without the LIBTESTDRIVER1_ prefix
986 loc_accel_flags=$( echo "$loc_accel_list" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
987 loc_accel_flags="$loc_accel_flags $( echo "$loc_accel_list" | sed 's/[^ ]* */-DMBEDTLS_PSA_ACCEL_&/g' )"
988 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" "$@"
989}
990
Valerio Setti9022f712024-06-27 07:59:39 +0200991# Set some default values $CONFIG_H in order to build server or client sides
992# in PSASIM. There is only 1 mandatory parameter:
993# - $1: target which can be "client" or "server"
Valerio Settid8b59372024-07-02 12:02:25 +0200994helper_psasim_config() {
Valerio Settid1b6ef12024-05-07 16:00:21 +0200995 TARGET=$1
Valerio Settid1b6ef12024-05-07 16:00:21 +0200996
Valerio Settid1b6ef12024-05-07 16:00:21 +0200997 if [ "$TARGET" == "client" ]; then
998 scripts/config.py full
999 scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
1000 scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1001 # Dynamic secure element support is a deprecated feature and it is not
1002 # available when CRYPTO_C and PSA_CRYPTO_STORAGE_C are disabled.
1003 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
1004 # Disable potentially problematic features
1005 scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
1006 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
1007 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
1008 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
1009 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
Tom Cosgrove0fe5b8d2024-06-11 17:26:17 +01001010 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Valerio Settid1b6ef12024-05-07 16:00:21 +02001011 else
1012 scripts/config.py crypto_full
1013 scripts/config.py unset MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS
Tom Cosgrove0fe5b8d2024-06-11 17:26:17 +01001014 # We need to match the client with MBEDTLS_PSA_CRYPTO_SE_C
1015 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
1016 # Also ensure MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER not set (to match client)
1017 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Valerio Settid1b6ef12024-05-07 16:00:21 +02001018 fi
Valerio Setti9022f712024-06-27 07:59:39 +02001019}
Valerio Settid1b6ef12024-05-07 16:00:21 +02001020
Valerio Setti9022f712024-06-27 07:59:39 +02001021# Helper to build the libraries for client/server in PSASIM. If the server is
1022# being built, then it builds also the final executable.
1023# There is only 1 mandatory parameter:
1024# - $1: target which can be "client" or "server"
1025helper_psasim_build() {
1026 TARGET=$1
1027 shift
1028 TARGET_LIB=${TARGET}_libs
1029
1030 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" $TARGET_LIB "$@"
1031
1032 # Build also the server application after its libraries have been built.
1033 if [ "$TARGET" == "server" ]; then
1034 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_server
1035 fi
Valerio Settid1b6ef12024-05-07 16:00:21 +02001036}
1037
Manuel Pégourié-Gonnard733a67b2023-11-15 12:32:17 +01001038################################################################
1039#### Configuration helpers
1040################################################################
1041
Valerio Settib2fd6732023-08-15 17:10:47 +02001042# When called with no parameter this function disables all builtin curves.
Valerio Setti4ca250b2023-09-04 14:01:41 +02001043# The function optionally accepts 1 parameter: a space-separated list of the
1044# curves that should be kept enabled.
Valerio Settib2fd6732023-08-15 17:10:47 +02001045helper_disable_builtin_curves() {
1046 allowed_list="${1:-}"
1047 scripts/config.py unset-all "MBEDTLS_ECP_DP_[0-9A-Z_a-z]*_ENABLED"
1048
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +02001049 for curve in $allowed_list; do
1050 scripts/config.py set $curve
Valerio Settib2fd6732023-08-15 17:10:47 +02001051 done
1052}
1053
1054# Helper returning the list of supported elliptic curves from CRYPTO_CONFIG_H,
1055# without the "PSA_WANT_" prefix. This becomes handy for accelerating curves
1056# in the following helpers.
1057helper_get_psa_curve_list () {
1058 loc_list=""
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +02001059 for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
1060 loc_list="$loc_list $item"
Valerio Settib2fd6732023-08-15 17:10:47 +02001061 done
1062
1063 echo "$loc_list"
1064}
1065
Valerio Setti78aa0bc2024-01-17 15:53:46 +01001066# Helper returning the list of supported DH groups from CRYPTO_CONFIG_H,
1067# without the "PSA_WANT_" prefix. This becomes handy for accelerating DH groups
1068# in the following helpers.
1069helper_get_psa_dh_group_list () {
1070 loc_list=""
1071 for item in $(sed -n 's/^#define PSA_WANT_\(DH_RFC7919_[0-9]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
1072 loc_list="$loc_list $item"
1073 done
1074
1075 echo "$loc_list"
1076}
1077
Valerio Settib2fd6732023-08-15 17:10:47 +02001078# Get the list of uncommented PSA_WANT_KEY_TYPE_xxx_ from CRYPTO_CONFIG_H. This
Valerio Setti0a342c92023-09-01 09:12:31 +02001079# is useful to easily get a list of key type symbols to accelerate.
Valerio Settib2fd6732023-08-15 17:10:47 +02001080# The function accepts a single argument which is the key type: ECC, DH, RSA.
Valerio Setti0a342c92023-09-01 09:12:31 +02001081helper_get_psa_key_type_list() {
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +02001082 key_type="$1"
Valerio Settib2fd6732023-08-15 17:10:47 +02001083 loc_list=""
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +02001084 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 +02001085 # Skip DERIVE for elliptic keys since there is no driver dispatch for
1086 # it so it cannot be accelerated.
Manuel Pégourié-Gonnard6be64f72023-09-28 09:08:04 +02001087 if [ "$item" != "KEY_TYPE_ECC_KEY_PAIR_DERIVE" ]; then
1088 loc_list="$loc_list $item"
Valerio Settib2fd6732023-08-15 17:10:47 +02001089 fi
1090 done
1091
1092 echo "$loc_list"
1093}
1094
Minos Galanakis6aab5b72024-07-25 14:24:37 +01001095# Include the components from components.sh
1096test_script_dir="${0%/*}"
Minos Galanakisada21b02024-07-26 12:34:19 +01001097source "$test_script_dir/components-compiler.sh"
1098source "$test_script_dir/components-platform.sh"
1099source "$test_script_dir/components-build-system.sh"
1100source "$test_script_dir/components-configuration.sh"
1101source "$test_script_dir/components-configuration-platform.sh"
1102source "$test_script_dir/components-configuration-tls.sh"
1103source "$test_script_dir/components-configuration-x509.sh"
1104source "$test_script_dir/components-configuration-crypto.sh"
1105source "$test_script_dir/components-basic-checks.sh"
1106source "$test_script_dir/components-sanitizers.sh"
1107source "$test_script_dir/components-compliance.sh"
Minos Galanakis96bf1822024-07-26 20:32:04 +01001108source "$test_script_dir/components-psasim.sh"
Valerio Setti9022f712024-06-27 07:59:39 +02001109
Gilles Peskine192c72f2017-12-21 15:59:21 +01001110################################################################
1111#### Termination
1112################################################################
1113
Gilles Peskine8f073122018-11-27 15:58:47 +01001114post_report () {
1115 msg "Done, cleaning up"
Gilles Peskinef83eb822020-03-30 20:11:39 +02001116 final_cleanup
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +01001117
Gilles Peskine8f073122018-11-27 15:58:47 +01001118 final_report
1119}
1120
Gilles Peskine8f073122018-11-27 15:58:47 +01001121################################################################
1122#### Run all the things
1123################################################################
1124
Gilles Peskine5d996822020-03-28 21:09:21 +01001125# Function invoked by --error-test to test error reporting.
1126pseudo_component_error_test () {
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001127 msg "Testing error reporting $error_test_i"
Gilles Peskine5d996822020-03-28 21:09:21 +01001128 if [ $KEEP_GOING -ne 0 ]; then
1129 echo "Expect three failing commands."
1130 fi
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001131 # If the component doesn't run in a subshell, changing error_test_i to an
1132 # invalid integer will cause an error in the loop that runs this function.
1133 error_test_i=this_should_not_be_used_since_the_component_runs_in_a_subshell
Gilles Peskineec135542021-08-02 23:14:03 +02001134 # Expected error: 'grep non_existent /dev/null -> 1'
Gilles Peskine5d996822020-03-28 21:09:21 +01001135 grep non_existent /dev/null
Gilles Peskineec135542021-08-02 23:14:03 +02001136 # Expected error: '! grep -q . tests/scripts/all.sh -> 1'
Gilles Peskine5d996822020-03-28 21:09:21 +01001137 not grep -q . "$0"
Gilles Peskineec135542021-08-02 23:14:03 +02001138 # Expected error: 'make unknown_target -> 2'
Gilles Peskine5d996822020-03-28 21:09:21 +01001139 make unknown_target
1140 false "this should not be executed"
1141}
1142
Gilles Peskinee48351a2018-11-27 16:06:30 +01001143# Run one component and clean up afterwards.
Gilles Peskine8f073122018-11-27 15:58:47 +01001144run_component () {
Gilles Peskineffcdeff2018-12-04 12:49:28 +01001145 current_component="$1"
Gilles Peskine9004a172019-09-16 15:20:36 +02001146 export MBEDTLS_TEST_CONFIGURATION="$current_component"
Gilles Peskine2ef377d2019-10-07 18:44:21 +02001147
1148 # Unconditionally create a seedfile that's sufficiently long.
1149 # Do this before each component, because a previous component may
1150 # have messed it up or shortened it.
Gilles Peskine88a07452021-07-08 19:03:50 +02001151 local dd_cmd
1152 dd_cmd=(dd if=/dev/urandom of=./tests/seedfile bs=64 count=1)
1153 case $OSTYPE in
Tom Cosgrove226aca12022-03-16 14:11:07 +00001154 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
Gilles Peskine88a07452021-07-08 19:03:50 +02001155 esac
1156 "${dd_cmd[@]}"
Gilles Peskine2ef377d2019-10-07 18:44:21 +02001157
Ronald Cron635a2be2024-07-12 14:30:16 +02001158 if [ -d tf-psa-crypto ]; then
1159 dd_cmd=(dd if=/dev/urandom of=./tf-psa-crypto/tests/seedfile bs=64 count=1)
1160 case $OSTYPE in
1161 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
1162 esac
1163 "${dd_cmd[@]}"
1164 fi
1165
Gilles Peskineec135542021-08-02 23:14:03 +02001166 # Run the component in a subshell, with error trapping and output
1167 # redirection set up based on the relevant options.
Gilles Peskinece266c42020-03-28 18:50:43 +01001168 if [ $KEEP_GOING -eq 1 ]; then
Gilles Peskineec135542021-08-02 23:14:03 +02001169 # We want to keep running if the subshell fails, so 'set -e' must
1170 # be off when the subshell runs.
Gilles Peskinece266c42020-03-28 18:50:43 +01001171 set +e
Manuel Pégourié-Gonnard2b2bdaa2020-06-02 11:28:07 +02001172 fi
Gilles Peskinece266c42020-03-28 18:50:43 +01001173 (
1174 if [ $QUIET -eq 1 ]; then
1175 # msg() will be silenced, so just print the component name here.
1176 echo "${current_component#component_}"
1177 exec >/dev/null
1178 fi
1179 if [ $KEEP_GOING -eq 1 ]; then
1180 # Keep "set -e" off, and run an ERR trap instead to record failures.
1181 set -E
1182 trap err_trap ERR
1183 fi
1184 # The next line is what runs the component
1185 "$@"
1186 if [ $KEEP_GOING -eq 1 ]; then
1187 trap - ERR
1188 exit $last_failure_status
1189 fi
1190 )
1191 component_status=$?
1192 if [ $KEEP_GOING -eq 1 ]; then
1193 set -e
1194 if [ $component_status -ne 0 ]; then
1195 failure_count=$((failure_count + 1))
1196 fi
1197 fi
Gilles Peskine2ef377d2019-10-07 18:44:21 +02001198
1199 # Restore the build tree to a clean state.
Gilles Peskinee48351a2018-11-27 16:06:30 +01001200 cleanup
Manuel Pégourié-Gonnard304b0992020-06-08 10:59:41 +02001201 unset current_component
Gilles Peskine8f073122018-11-27 15:58:47 +01001202}
1203
1204# Preliminary setup
1205pre_check_environment
1206pre_initialize_variables
1207pre_parse_command_line "$@"
Gilles Peskine348fb9a2018-11-27 17:04:29 +01001208
Dave Rodgman21900632024-02-26 11:41:19 +00001209setup_quiet_wrappers
Gilles Peskine878cf602019-01-06 20:50:38 +00001210pre_check_git
Gilles Peskine568f53a2021-07-12 18:16:01 +02001211pre_restore_files
Gilles Peskinef83eb822020-03-30 20:11:39 +02001212pre_back_up
Andrzej Kurekeb508712019-02-14 07:18:59 -05001213
Gilles Peskine878cf602019-01-06 20:50:38 +00001214build_status=0
1215if [ $KEEP_GOING -eq 1 ]; then
1216 pre_setup_keep_going
Gilles Peskine8f073122018-11-27 15:58:47 +01001217fi
Gilles Peskine67ffdaf2019-09-16 15:55:46 +02001218pre_prepare_outcome_file
Gilles Peskine878cf602019-01-06 20:50:38 +00001219pre_print_configuration
1220pre_check_tools
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +01001221cleanup
David Horstmann76a77382023-08-17 17:32:26 +01001222if in_mbedtls_repo; then
David Horstmann9a6c45b2023-07-14 12:30:00 +01001223 pre_generate_files
1224fi
Manuel Pégourié-Gonnard80955ee2014-03-19 18:29:01 +01001225
Gilles Peskinebeb3a812019-01-06 22:11:25 +00001226# Run the requested tests.
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001227for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do
Gilles Peskine5d996822020-03-28 21:09:21 +01001228 run_component pseudo_component_error_test
Gilles Peskine5d996822020-03-28 21:09:21 +01001229done
Gilles Peskine88a7c2b2021-08-02 23:28:00 +02001230unset error_test_i
Gilles Peskinebeb3a812019-01-06 22:11:25 +00001231for component in $RUN_COMPONENTS; do
1232 run_component "component_$component"
1233done
Gilles Peskine8f073122018-11-27 15:58:47 +01001234
1235# We're done.
Gilles Peskine878cf602019-01-06 20:50:38 +00001236post_report