blob: 1eac64f54d6faa178d253dd9075297e62abec530 [file] [log] [blame]
Minos Galanakis77711192024-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
6# This file contains test components that are executed by all.sh
7
8################################################################
9#### Compiler Testing
10################################################################
Minos Galanakis3bd6c302024-08-01 16:56:49 +010011
12support_build_tfm_armcc () {
13 support_build_armcc
14}
15
16component_build_tfm_armcc () {
17 # test the TF-M configuration can build cleanly with various warning flags enabled
18 cp configs/config-tfm.h "$CONFIG_H"
19
20 msg "build: TF-M config, armclang armv7-m thumb2"
David Horstmanndcf42a02024-11-08 14:40:12 +000021 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 Galanakis3bd6c302024-08-01 16:56:49 +010022}
23
24test_build_opt () {
25 info=$1 cc=$2; shift 2
26 $cc --version
27 for opt in "$@"; do
28 msg "build/test: $cc $opt, $info" # ~ 30s
29 make CC="$cc" CFLAGS="$opt -std=c99 -pedantic -Wall -Wextra -Werror"
30 # We're confident enough in compilers to not run _all_ the tests,
31 # but at least run the unit tests. In particular, runs with
32 # optimizations use inline assembly whereas runs with -O0
33 # skip inline assembly.
34 make test # ~30s
35 make clean
36 done
37}
38
39# For FreeBSD we invoke the function by name so this condition is added
40# to disable the existing test_clang_opt function for linux.
41if [[ $(uname) != "Linux" ]]; then
42 component_test_clang_opt () {
43 scripts/config.py full
44 test_build_opt 'full config' clang -O0 -Os -O2
45 }
46fi
47
48component_test_clang_latest_opt () {
49 scripts/config.py full
50 test_build_opt 'full config' "$CLANG_LATEST" -O0 -Os -O2
51}
52
53support_test_clang_latest_opt () {
54 type "$CLANG_LATEST" >/dev/null 2>/dev/null
55}
56
57component_test_clang_earliest_opt () {
58 scripts/config.py full
Gilles Peskine20e17662024-06-19 16:30:36 +020059 test_build_opt 'full config' "$CLANG_EARLIEST" -O2
Minos Galanakis3bd6c302024-08-01 16:56:49 +010060}
61
62support_test_clang_earliest_opt () {
63 type "$CLANG_EARLIEST" >/dev/null 2>/dev/null
64}
65
66component_test_gcc_latest_opt () {
67 scripts/config.py full
68 test_build_opt 'full config' "$GCC_LATEST" -O0 -Os -O2
69}
70
71support_test_gcc_latest_opt () {
72 type "$GCC_LATEST" >/dev/null 2>/dev/null
73}
74
Gilles Peskine94c1b9a2024-12-07 15:08:35 +010075# Prepare for a non-regression for https://github.com/Mbed-TLS/mbedtls/issues/9814 :
Gilles Peskine15bb8e02025-04-30 16:57:07 +020076# test with GCC 15.
Gilles Peskine94c1b9a2024-12-07 15:08:35 +010077# Eventually, $GCC_LATEST will be GCC 15 or above, and we can remove this
78# separate component.
79# For the time being, we don't make $GCC_LATEST be GCC 15 on the CI
Gilles Peskine15bb8e02025-04-30 16:57:07 +020080# platform, because that would break branches where #9814 isn't fixed yet.
Gilles Peskine2eab9fd2025-02-05 20:01:52 +010081support_test_gcc15_drivers_opt () {
Gilles Peskinedf065722025-02-05 20:26:21 +010082 if type gcc-15 >/dev/null 2>/dev/null; then
83 GCC_15=gcc-15
84 elif [ -x /usr/local/gcc-15/bin/gcc-15 ]; then
85 GCC_15=/usr/local/gcc-15/bin/gcc-15
86 else
87 return 1
88 fi
Gilles Peskine94c1b9a2024-12-07 15:08:35 +010089}
Gilles Peskine2eab9fd2025-02-05 20:01:52 +010090component_test_gcc15_drivers_opt () {
91 msg "build: GCC 15: full + test drivers dispatching to builtins"
Gilles Peskine94c1b9a2024-12-07 15:08:35 +010092 scripts/config.py full
Gilles Peskine2eab9fd2025-02-05 20:01:52 +010093 scripts/config.py unset MBEDTLS_PSA_CRYPTO_CONFIG
94 loc_cflags="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST_ALL"
95 loc_cflags="${loc_cflags} '-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-for-test.h\"'"
96 loc_cflags="${loc_cflags} -I../framework/tests/include -O2"
Gilles Peskine2eab9fd2025-02-05 20:01:52 +010097
Gilles Peskinedf065722025-02-05 20:26:21 +010098 make CC=$GCC_15 CFLAGS="${loc_cflags}" LDFLAGS="$ASAN_CFLAGS"
Gilles Peskine2eab9fd2025-02-05 20:01:52 +010099
100 msg "test: GCC 15: full + test drivers dispatching to builtins"
Gilles Peskinebf90dc92024-12-07 23:32:22 +0100101 make test
Gilles Peskine94c1b9a2024-12-07 15:08:35 +0100102}
103
Minos Galanakis3bd6c302024-08-01 16:56:49 +0100104component_test_gcc_earliest_opt () {
105 scripts/config.py full
Gilles Peskine20e17662024-06-19 16:30:36 +0200106 test_build_opt 'full config' "$GCC_EARLIEST" -O2
Minos Galanakis3bd6c302024-08-01 16:56:49 +0100107}
108
109support_test_gcc_earliest_opt () {
110 type "$GCC_EARLIEST" >/dev/null 2>/dev/null
111}
112
113component_build_mingw () {
114 msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
Gilles Peskine574b2922024-06-19 16:34:29 +0200115 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 lib programs
Minos Galanakis3bd6c302024-08-01 16:56:49 +0100116
117 # note Make tests only builds the tests, but doesn't run them
Gilles Peskine574b2922024-06-19 16:34:29 +0200118 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -maes -msse2 -mpclmul' WINDOWS_BUILD=1 tests
Minos Galanakis3bd6c302024-08-01 16:56:49 +0100119 make WINDOWS_BUILD=1 clean
120
121 msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
Gilles Peskine574b2922024-06-19 16:34:29 +0200122 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
123 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 Galanakis3bd6c302024-08-01 16:56:49 +0100124 make WINDOWS_BUILD=1 clean
125
126 msg "build: Windows cross build - mingw64, make (Library only, default config without MBEDTLS_AESNI_C)" # ~ 30s
127 ./scripts/config.py unset MBEDTLS_AESNI_C #
Gilles Peskine574b2922024-06-19 16:34:29 +0200128 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib
Minos Galanakis3bd6c302024-08-01 16:56:49 +0100129 make WINDOWS_BUILD=1 clean
130}
131
132support_build_mingw () {
133 case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
134 [0-5]*|"") false;;
135 *) true;;
136 esac
137}
138
139component_build_zeroize_checks () {
140 msg "build: check for obviously wrong calls to mbedtls_platform_zeroize()"
141
142 scripts/config.py full
143
144 # Only compile - we're looking for sizeof-pointer-memaccess warnings
145 make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-zeroize-memset.h\"' -DMBEDTLS_TEST_DEFINES_ZEROIZE -Werror -Wsizeof-pointer-memaccess"
146}
147
148component_test_zeroize () {
149 # Test that the function mbedtls_platform_zeroize() is not optimized away by
150 # different combinations of compilers and optimization flags by using an
151 # auxiliary GDB script. Unfortunately, GDB does not return error values to the
152 # system in all cases that the script fails, so we must manually search the
153 # output to check whether the pass string is present and no failure strings
154 # were printed.
155
156 # Don't try to disable ASLR. We don't care about ASLR here. We do care
157 # about a spurious message if Gdb tries and fails, so suppress that.
158 gdb_disable_aslr=
159 if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
160 gdb_disable_aslr='set disable-randomization off'
161 fi
162
163 for optimization_flag in -O2 -O3 -Ofast -Os; do
164 for compiler in clang gcc; do
165 msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
166 make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
Harry Ramseyd621d342025-02-17 12:05:26 +0000167 gdb -ex "$gdb_disable_aslr" -x $FRAMEWORK/tests/programs/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log
Minos Galanakis3bd6c302024-08-01 16:56:49 +0100168 grep "The buffer was correctly zeroized" test_zeroize.log
169 not grep -i "error" test_zeroize.log
170 rm -f test_zeroize.log
171 make clean
172 done
173 done
174}