blob: c8b276c0210aa8251246b1f8c604b76047c47d10 [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 :
76# test with GCC 15 (initially, a snapshot, since GCC 15 isn't released yet
77# at the time of writing).
78# Eventually, $GCC_LATEST will be GCC 15 or above, and we can remove this
79# separate component.
80# For the time being, we don't make $GCC_LATEST be GCC 15 on the CI
81# platform, because that would break branches where #9814 isn'f fixed yet.
82support_test_gcc15_opt () {
83 test -x /usr/local/gcc-15/bin/gcc-15
84}
85component_test_gcc15_opt () {
86 scripts/config.py full
87 # Until https://github.com/Mbed-TLS/mbedtls/issues/9814 is fixed,
88 # disable the new problematic optimization.
Gilles Peskinebf90dc92024-12-07 23:32:22 +010089 # Also disable a warning that we don't yet comply to.
90 make CC="/usr/local/gcc-15/bin/gcc-15" CFLAGS="-O2 -Wall -Wextra -Werror -fzero-init-padding-bits=unions -Wno-error=unterminated-string-initialization"
91 make test
Gilles Peskine94c1b9a2024-12-07 15:08:35 +010092}
93
Minos Galanakis3bd6c302024-08-01 16:56:49 +010094component_test_gcc_earliest_opt () {
95 scripts/config.py full
Gilles Peskine20e17662024-06-19 16:30:36 +020096 test_build_opt 'full config' "$GCC_EARLIEST" -O2
Minos Galanakis3bd6c302024-08-01 16:56:49 +010097}
98
99support_test_gcc_earliest_opt () {
100 type "$GCC_EARLIEST" >/dev/null 2>/dev/null
101}
102
103component_build_mingw () {
104 msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
Gilles Peskine574b2922024-06-19 16:34:29 +0200105 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 +0100106
107 # note Make tests only builds the tests, but doesn't run them
Gilles Peskine574b2922024-06-19 16:34:29 +0200108 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 +0100109 make WINDOWS_BUILD=1 clean
110
111 msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
Gilles Peskine574b2922024-06-19 16:34:29 +0200112 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
113 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 +0100114 make WINDOWS_BUILD=1 clean
115
116 msg "build: Windows cross build - mingw64, make (Library only, default config without MBEDTLS_AESNI_C)" # ~ 30s
117 ./scripts/config.py unset MBEDTLS_AESNI_C #
Gilles Peskine574b2922024-06-19 16:34:29 +0200118 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 +0100119 make WINDOWS_BUILD=1 clean
120}
121
122support_build_mingw () {
123 case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
124 [0-5]*|"") false;;
125 *) true;;
126 esac
127}
128
129component_build_zeroize_checks () {
130 msg "build: check for obviously wrong calls to mbedtls_platform_zeroize()"
131
132 scripts/config.py full
133
134 # Only compile - we're looking for sizeof-pointer-memaccess warnings
135 make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-zeroize-memset.h\"' -DMBEDTLS_TEST_DEFINES_ZEROIZE -Werror -Wsizeof-pointer-memaccess"
136}
137
138component_test_zeroize () {
139 # Test that the function mbedtls_platform_zeroize() is not optimized away by
140 # different combinations of compilers and optimization flags by using an
141 # auxiliary GDB script. Unfortunately, GDB does not return error values to the
142 # system in all cases that the script fails, so we must manually search the
143 # output to check whether the pass string is present and no failure strings
144 # were printed.
145
146 # Don't try to disable ASLR. We don't care about ASLR here. We do care
147 # about a spurious message if Gdb tries and fails, so suppress that.
148 gdb_disable_aslr=
149 if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
150 gdb_disable_aslr='set disable-randomization off'
151 fi
152
153 for optimization_flag in -O2 -O3 -Ofast -Os; do
154 for compiler in clang gcc; do
155 msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
156 make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
Harry Ramseyd621d342025-02-17 12:05:26 +0000157 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 +0100158 grep "The buffer was correctly zeroized" test_zeroize.log
159 not grep -i "error" test_zeroize.log
160 rm -f test_zeroize.log
161 make clean
162 done
163 done
164}