blob: 5ccd7cc7dda71af2d3d652b4feb8984e7a736af8 [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"
19
20 msg "build: TF-M config, armclang armv7-m thumb2"
21 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../tests/include/spe"
22}
23
Minos Galanakis9ebb8822024-07-26 15:21:14 +010024test_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
59 test_build_opt 'full config' "$CLANG_EARLIEST" -O0
60}
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
75component_test_gcc_earliest_opt () {
76 scripts/config.py full
77 test_build_opt 'full config' "$GCC_EARLIEST" -O0
78}
79
80support_test_gcc_earliest_opt () {
81 type "$GCC_EARLIEST" >/dev/null 2>/dev/null
82}
83
Minos Galanakis9ebb8822024-07-26 15:21:14 +010084component_build_mingw () {
85 msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
Gilles Peskined860f282024-06-19 16:34:29 +020086 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 +010087
88 # note Make tests only builds the tests, but doesn't run them
Gilles Peskined860f282024-06-19 16:34:29 +020089 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 +010090 make WINDOWS_BUILD=1 clean
91
92 msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
Gilles Peskined860f282024-06-19 16:34:29 +020093 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
94 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 +010095 make WINDOWS_BUILD=1 clean
96
97 msg "build: Windows cross build - mingw64, make (Library only, default config without MBEDTLS_AESNI_C)" # ~ 30s
98 ./scripts/config.py unset MBEDTLS_AESNI_C #
Gilles Peskined860f282024-06-19 16:34:29 +020099 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 +0100100 make WINDOWS_BUILD=1 clean
101}
102
Minos Galanakisf78447f2024-07-26 20:49:51 +0100103support_build_mingw () {
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100104 case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
105 [0-5]*|"") false;;
106 *) true;;
107 esac
108}
109
Minos Galanakisbf47cf72024-08-01 12:35:25 +0100110component_build_zeroize_checks () {
111 msg "build: check for obviously wrong calls to mbedtls_platform_zeroize()"
112
113 scripts/config.py full
114
115 # Only compile - we're looking for sizeof-pointer-memaccess warnings
116 make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-zeroize-memset.h\"' -DMBEDTLS_TEST_DEFINES_ZEROIZE -Werror -Wsizeof-pointer-memaccess"
117}
118
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100119component_test_zeroize () {
120 # Test that the function mbedtls_platform_zeroize() is not optimized away by
121 # different combinations of compilers and optimization flags by using an
122 # auxiliary GDB script. Unfortunately, GDB does not return error values to the
123 # system in all cases that the script fails, so we must manually search the
124 # output to check whether the pass string is present and no failure strings
125 # were printed.
126
127 # Don't try to disable ASLR. We don't care about ASLR here. We do care
128 # about a spurious message if Gdb tries and fails, so suppress that.
129 gdb_disable_aslr=
130 if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
131 gdb_disable_aslr='set disable-randomization off'
132 fi
133
134 for optimization_flag in -O2 -O3 -Ofast -Os; do
135 for compiler in clang gcc; do
136 msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
137 make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
138 gdb -ex "$gdb_disable_aslr" -x tests/scripts/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log
139 grep "The buffer was correctly zeroized" test_zeroize.log
140 not grep -i "error" test_zeroize.log
141 rm -f test_zeroize.log
142 make clean
143 done
144 done
145}