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
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
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
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
84component_build_mingw () {
85    msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
86    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 lib programs
87
88    # note Make tests only builds the tests, but doesn't run them
89    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -maes -msse2 -mpclmul' WINDOWS_BUILD=1 tests
90    make WINDOWS_BUILD=1 clean
91
92    msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
93    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld 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 LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 SHARED=1 tests
95    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 #
99    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib
100    make WINDOWS_BUILD=1 clean
101}
102
103support_build_mingw () {
104    case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
105        [0-5]*|"") false;;
106        *) true;;
107    esac
108}
109
110component_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
119component_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}
146