blob: e0dfe49e0d9815fc4f0a3d254f9fc51dece6283e [file] [log] [blame]
Minos Galanakis6aab5b72024-07-25 14:24:37 +01001# components-compiler.sh
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
Minos Galanakis609f7492024-07-31 16:39:28 +01006# This file contains test components that are executed by all.sh
Minos Galanakis6aab5b72024-07-25 14:24:37 +01007
8################################################################
9#### Compiler Testing
10################################################################
11
Minos Galanakis9ebb8822024-07-26 15:21:14 +010012support_build_tfm_armcc () {
13 support_build_armcc
14}
15
Minos Galanakisf78447f2024-07-26 20:49:51 +010016component_build_tfm_armcc () {
Minos Galanakis9ebb8822024-07-26 15:21:14 +010017 # test the TF-M configuration can build cleanly with various warning flags enabled
18 cp configs/config-tfm.h "$CONFIG_H"
Harry Ramsey94c386a2025-01-16 16:08:34 +000019 cp tf-psa-crypto/configs/ext/crypto_config_profile_medium.h "$CRYPTO_CONFIG_H"
Minos Galanakis9ebb8822024-07-26 15:21:14 +010020
21 msg "build: TF-M config, armclang armv7-m thumb2"
David Horstmann5b93d972024-10-31 15:36:05 +000022 helper_armc6_build_test "--target=arm-arm-none-eabi -march=armv7-m -mthumb -Os -std=c99 -Werror -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral -Wshadow -Wasm-operand-widths -Wunused -I../framework/tests/include/spe"
Minos Galanakis9ebb8822024-07-26 15:21:14 +010023}
24
Minos Galanakis9ebb8822024-07-26 15:21:14 +010025test_build_opt () {
26 info=$1 cc=$2; shift 2
27 $cc --version
28 for opt in "$@"; do
29 msg "build/test: $cc $opt, $info" # ~ 30s
30 make CC="$cc" CFLAGS="$opt -std=c99 -pedantic -Wall -Wextra -Werror"
31 # We're confident enough in compilers to not run _all_ the tests,
32 # but at least run the unit tests. In particular, runs with
33 # optimizations use inline assembly whereas runs with -O0
34 # skip inline assembly.
35 make test # ~30s
36 make clean
37 done
38}
39
40# For FreeBSD we invoke the function by name so this condition is added
41# to disable the existing test_clang_opt function for linux.
42if [[ $(uname) != "Linux" ]]; then
43 component_test_clang_opt () {
44 scripts/config.py full
45 test_build_opt 'full config' clang -O0 -Os -O2
46 }
47fi
48
49component_test_clang_latest_opt () {
50 scripts/config.py full
51 test_build_opt 'full config' "$CLANG_LATEST" -O0 -Os -O2
52}
53
54support_test_clang_latest_opt () {
55 type "$CLANG_LATEST" >/dev/null 2>/dev/null
56}
57
58component_test_clang_earliest_opt () {
59 scripts/config.py full
Gilles Peskine20de8062024-06-19 16:30:36 +020060 test_build_opt 'full config' "$CLANG_EARLIEST" -O2
Minos Galanakis9ebb8822024-07-26 15:21:14 +010061}
62
63support_test_clang_earliest_opt () {
64 type "$CLANG_EARLIEST" >/dev/null 2>/dev/null
65}
66
67component_test_gcc_latest_opt () {
68 scripts/config.py full
69 test_build_opt 'full config' "$GCC_LATEST" -O0 -Os -O2
70}
71
72support_test_gcc_latest_opt () {
73 type "$GCC_LATEST" >/dev/null 2>/dev/null
74}
75
Gilles Peskine579475d2024-12-07 15:08:35 +010076# Prepare for a non-regression for https://github.com/Mbed-TLS/mbedtls/issues/9814 :
77# test with GCC 15 (initially, a snapshot, since GCC 15 isn't released yet
78# at the time of writing).
79# Eventually, $GCC_LATEST will be GCC 15 or above, and we can remove this
80# separate component.
81# For the time being, we don't make $GCC_LATEST be GCC 15 on the CI
82# platform, because that would break branches where #9814 isn'f fixed yet.
Gilles Peskine27f07132025-02-05 20:01:52 +010083support_test_gcc15_drivers_opt () {
Gilles Peskined69bfb90442025-02-05 20:26:21 +010084 if type gcc-15 >/dev/null 2>/dev/null; then
85 GCC_15=gcc-15
86 elif [ -x /usr/local/gcc-15/bin/gcc-15 ]; then
87 GCC_15=/usr/local/gcc-15/bin/gcc-15
88 else
89 return 1
90 fi
Gilles Peskine579475d2024-12-07 15:08:35 +010091}
Gilles Peskine27f07132025-02-05 20:01:52 +010092component_test_gcc15_drivers_opt () {
93 msg "build: GCC 15: full + test drivers dispatching to builtins"
Gilles Peskine579475d2024-12-07 15:08:35 +010094 scripts/config.py full
Gilles Peskine27f07132025-02-05 20:01:52 +010095 loc_cflags="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_CONFIG_ADJUST_TEST_ACCELERATORS"
96 loc_cflags="${loc_cflags} -I../framework/tests/include -O2"
Gilles Peskine579475d2024-12-07 15:08:35 +010097 # Until https://github.com/Mbed-TLS/mbedtls/issues/9814 is fixed,
98 # disable the new problematic optimization.
Gilles Peskine27f07132025-02-05 20:01:52 +010099 loc_cflags="${loc_cflags} -fzero-init-padding-bits=unions"
Gilles Peskine6e245042024-12-07 23:32:22 +0100100 # Also disable a warning that we don't yet comply to.
Gilles Peskine27f07132025-02-05 20:01:52 +0100101 loc_cflags="${loc_cflags} -Wno-error=unterminated-string-initialization"
102
Gilles Peskined69bfb90442025-02-05 20:26:21 +0100103 make CC=$GCC_15 CFLAGS="${loc_cflags}" LDFLAGS="$ASAN_CFLAGS"
Gilles Peskine27f07132025-02-05 20:01:52 +0100104
105 msg "test: GCC 15: full + test drivers dispatching to builtins"
Gilles Peskine6e245042024-12-07 23:32:22 +0100106 make test
Gilles Peskine579475d2024-12-07 15:08:35 +0100107}
108
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100109component_test_gcc_earliest_opt () {
110 scripts/config.py full
Gilles Peskine20de8062024-06-19 16:30:36 +0200111 test_build_opt 'full config' "$GCC_EARLIEST" -O2
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100112}
113
114support_test_gcc_earliest_opt () {
115 type "$GCC_EARLIEST" >/dev/null 2>/dev/null
116}
117
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100118component_build_mingw () {
119 msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
Gilles Peskined860f282024-06-19 16:34:29 +0200120 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 lib programs
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100121
122 # note Make tests only builds the tests, but doesn't run them
Gilles Peskined860f282024-06-19 16:34:29 +0200123 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -maes -msse2 -mpclmul' WINDOWS_BUILD=1 tests
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100124 make WINDOWS_BUILD=1 clean
125
126 msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
Gilles Peskined860f282024-06-19 16:34:29 +0200127 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 SHARED=1 lib programs
128 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 SHARED=1 tests
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100129 make WINDOWS_BUILD=1 clean
130
131 msg "build: Windows cross build - mingw64, make (Library only, default config without MBEDTLS_AESNI_C)" # ~ 30s
132 ./scripts/config.py unset MBEDTLS_AESNI_C #
Gilles Peskined860f282024-06-19 16:34:29 +0200133 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100134 make WINDOWS_BUILD=1 clean
135}
136
Minos Galanakisf78447f2024-07-26 20:49:51 +0100137support_build_mingw () {
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100138 case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
139 [0-5]*|"") false;;
140 *) true;;
141 esac
142}
143
Minos Galanakisbf47cf72024-08-01 12:35:25 +0100144component_build_zeroize_checks () {
145 msg "build: check for obviously wrong calls to mbedtls_platform_zeroize()"
146
147 scripts/config.py full
148
149 # Only compile - we're looking for sizeof-pointer-memaccess warnings
Harry Ramsey2a75a602025-02-17 15:52:58 +0000150 make CFLAGS="'-DTF_PSA_CRYPTO_USER_CONFIG_FILE=\"$TF_PSA_CRYPTO_ROOT_DIR/tests/configs/user-config-zeroize-memset.h\"' -DMBEDTLS_TEST_DEFINES_ZEROIZE -Werror -Wsizeof-pointer-memaccess"
Minos Galanakisbf47cf72024-08-01 12:35:25 +0100151}
152
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100153component_test_zeroize () {
154 # Test that the function mbedtls_platform_zeroize() is not optimized away by
155 # different combinations of compilers and optimization flags by using an
156 # auxiliary GDB script. Unfortunately, GDB does not return error values to the
157 # system in all cases that the script fails, so we must manually search the
158 # output to check whether the pass string is present and no failure strings
159 # were printed.
160
161 # Don't try to disable ASLR. We don't care about ASLR here. We do care
162 # about a spurious message if Gdb tries and fails, so suppress that.
163 gdb_disable_aslr=
164 if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
165 gdb_disable_aslr='set disable-randomization off'
166 fi
167
168 for optimization_flag in -O2 -O3 -Ofast -Os; do
169 for compiler in clang gcc; do
170 msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
171 make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
Harry Ramsey9b4035c2025-02-17 10:00:11 +0000172 gdb -ex "$gdb_disable_aslr" -x $FRAMEWORK/tests/programs/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100173 grep "The buffer was correctly zeroized" test_zeroize.log
174 not grep -i "error" test_zeroize.log
175 rm -f test_zeroize.log
176 make clean
177 done
178 done
179}