1# 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################################################################
11
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    cp tf-psa-crypto/configs/ext/crypto_config_profile_medium.h "$CRYPTO_CONFIG_H"
20
21    msg "build: TF-M config, armclang armv7-m thumb2"
22    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"
23}
24
25test_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
60    test_build_opt 'full config' "$CLANG_EARLIEST" -O2
61}
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
76# Prepare for a non-regression for https://github.com/Mbed-TLS/mbedtls/issues/9814 :
77# test with GCC 15.
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't fixed yet.
82support_test_gcc15_drivers_opt () {
83    if type gcc-15 >/dev/null 2>/dev/null; then
84        GCC_15=gcc-15
85    elif [ -x /usr/local/gcc-15/bin/gcc-15 ]; then
86        GCC_15=/usr/local/gcc-15/bin/gcc-15
87    else
88        return 1
89    fi
90}
91component_test_gcc15_drivers_opt () {
92    msg "build: GCC 15: full + test drivers dispatching to builtins"
93    scripts/config.py full
94    loc_cflags="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_CONFIG_ADJUST_TEST_ACCELERATORS"
95    loc_cflags="${loc_cflags} -I../framework/tests/include -O2"
96
97    make CC=$GCC_15 CFLAGS="${loc_cflags}" LDFLAGS="$ASAN_CFLAGS"
98
99    msg "test: GCC 15: full + test drivers dispatching to builtins"
100    make test
101}
102
103component_test_gcc_earliest_opt () {
104    scripts/config.py full
105    test_build_opt 'full config' "$GCC_EARLIEST" -O2
106}
107
108support_test_gcc_earliest_opt () {
109    type "$GCC_EARLIEST" >/dev/null 2>/dev/null
110}
111
112component_build_mingw () {
113    msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
114    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 lib programs
115
116    # note Make tests only builds the tests, but doesn't run them
117    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -maes -msse2 -mpclmul' WINDOWS_BUILD=1 tests
118    make WINDOWS_BUILD=1 clean
119
120    msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
121    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
122    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 SHARED=1 tests
123    make WINDOWS_BUILD=1 clean
124
125    msg "build: Windows cross build - mingw64, make (Library only, default config without MBEDTLS_AESNI_C)" # ~ 30s
126    ./scripts/config.py unset MBEDTLS_AESNI_C #
127    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib
128    make WINDOWS_BUILD=1 clean
129}
130
131support_build_mingw () {
132    case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
133        [0-5]*|"") false;;
134        *) true;;
135    esac
136}
137
138component_build_zeroize_checks () {
139    msg "build: check for obviously wrong calls to mbedtls_platform_zeroize()"
140
141    scripts/config.py full
142
143    # Only compile - we're looking for sizeof-pointer-memaccess warnings
144    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"
145}
146
147component_test_zeroize () {
148    # Test that the function mbedtls_platform_zeroize() is not optimized away by
149    # different combinations of compilers and optimization flags by using an
150    # auxiliary GDB script. Unfortunately, GDB does not return error values to the
151    # system in all cases that the script fails, so we must manually search the
152    # output to check whether the pass string is present and no failure strings
153    # were printed.
154
155    # Don't try to disable ASLR. We don't care about ASLR here. We do care
156    # about a spurious message if Gdb tries and fails, so suppress that.
157    gdb_disable_aslr=
158    if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
159        gdb_disable_aslr='set disable-randomization off'
160    fi
161
162    for optimization_flag in -O2 -O3 -Ofast -Os; do
163        for compiler in clang gcc; do
164            msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
165            make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
166            gdb -ex "$gdb_disable_aslr" -x $FRAMEWORK/tests/programs/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log
167            grep "The buffer was correctly zeroized" test_zeroize.log
168            not grep -i "error" test_zeroize.log
169            rm -f test_zeroize.log
170            make clean
171        done
172    done
173}
174