blob: 5a16fd930f12ab9447946ebb04aa894a3f5bea18 [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 Galanakisf78447f2024-07-26 20:49:51 +010024support_test_aesni_m32_clang () {
Minos Galanakis9ebb8822024-07-26 15:21:14 +010025 # clang >= 4 is required to build with target attributes
26 support_test_aesni_m32 && [[ $(clang_version) -ge 4 ]]
27}
28
Minos Galanakisf78447f2024-07-26 20:49:51 +010029component_test_aesni_m32_clang () {
Minos Galanakis9ebb8822024-07-26 15:21:14 +010030
31 scripts/config.py set MBEDTLS_AESNI_C
32 scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
33 scripts/config.py set MBEDTLS_HAVE_ASM
34
35 # test the intrinsics implementation with clang
36 msg "AES tests, test intrinsics (clang)"
37 make clean
38 make CC=clang CFLAGS='-m32 -Werror -Wall -Wextra' LDFLAGS='-m32'
39 # check that we built intrinsics - this should be used by default when supported by the compiler
40 ./programs/test/selftest aes | grep "AESNI code" | grep -q "intrinsics"
41 grep -q "AES note: using AESNI" ./programs/test/selftest
42 grep -q "AES note: built-in implementation." ./programs/test/selftest
43 grep -q mbedtls_aesni_has_support ./programs/test/selftest
44}
45
Minos Galanakis9ebb8822024-07-26 15:21:14 +010046test_build_opt () {
47 info=$1 cc=$2; shift 2
48 $cc --version
49 for opt in "$@"; do
50 msg "build/test: $cc $opt, $info" # ~ 30s
51 make CC="$cc" CFLAGS="$opt -std=c99 -pedantic -Wall -Wextra -Werror"
52 # We're confident enough in compilers to not run _all_ the tests,
53 # but at least run the unit tests. In particular, runs with
54 # optimizations use inline assembly whereas runs with -O0
55 # skip inline assembly.
56 make test # ~30s
57 make clean
58 done
59}
60
61# For FreeBSD we invoke the function by name so this condition is added
62# to disable the existing test_clang_opt function for linux.
63if [[ $(uname) != "Linux" ]]; then
64 component_test_clang_opt () {
65 scripts/config.py full
66 test_build_opt 'full config' clang -O0 -Os -O2
67 }
68fi
69
70component_test_clang_latest_opt () {
71 scripts/config.py full
72 test_build_opt 'full config' "$CLANG_LATEST" -O0 -Os -O2
73}
74
75support_test_clang_latest_opt () {
76 type "$CLANG_LATEST" >/dev/null 2>/dev/null
77}
78
79component_test_clang_earliest_opt () {
80 scripts/config.py full
81 test_build_opt 'full config' "$CLANG_EARLIEST" -O0
82}
83
84support_test_clang_earliest_opt () {
85 type "$CLANG_EARLIEST" >/dev/null 2>/dev/null
86}
87
88component_test_gcc_latest_opt () {
89 scripts/config.py full
90 test_build_opt 'full config' "$GCC_LATEST" -O0 -Os -O2
91}
92
93support_test_gcc_latest_opt () {
94 type "$GCC_LATEST" >/dev/null 2>/dev/null
95}
96
97component_test_gcc_earliest_opt () {
98 scripts/config.py full
99 test_build_opt 'full config' "$GCC_EARLIEST" -O0
100}
101
102support_test_gcc_earliest_opt () {
103 type "$GCC_EARLIEST" >/dev/null 2>/dev/null
104}
105
106component_build_arm_none_eabi_gcc () {
107 msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -O1, baremetal+debug" # ~ 10s
108 scripts/config.py baremetal
109 make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -Wall -Wextra -O1' lib
110
111 msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -O1, baremetal+debug"
112 ${ARM_NONE_EABI_GCC_PREFIX}size -t library/*.o
113 ${ARM_NONE_EABI_GCC_PREFIX}size -t ${PSA_CORE_PATH}/*.o
114 ${ARM_NONE_EABI_GCC_PREFIX}size -t ${BUILTIN_SRC_PATH}/*.o
115}
116
117component_build_arm_linux_gnueabi_gcc_arm5vte () {
118 msg "build: ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc -march=arm5vte, baremetal+debug" # ~ 10s
119 scripts/config.py baremetal
120 # Build for a target platform that's close to what Debian uses
121 # for its "armel" distribution (https://wiki.debian.org/ArmEabiPort).
122 # See https://github.com/Mbed-TLS/mbedtls/pull/2169 and comments.
123 # Build everything including programs, see for example
124 # https://github.com/Mbed-TLS/mbedtls/pull/3449#issuecomment-675313720
125 make CC="${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc" AR="${ARM_LINUX_GNUEABI_GCC_PREFIX}ar" CFLAGS='-Werror -Wall -Wextra -march=armv5te -O1' LDFLAGS='-march=armv5te'
126
127 msg "size: ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc -march=armv5te -O1, baremetal+debug"
128 ${ARM_LINUX_GNUEABI_GCC_PREFIX}size -t library/*.o
129 ${ARM_LINUX_GNUEABI_GCC_PREFIX}size -t ${PSA_CORE_PATH}/*.o
130 ${ARM_LINUX_GNUEABI_GCC_PREFIX}size -t ${BUILTIN_SRC_PATH}/*.o
131}
132
133support_build_arm_linux_gnueabi_gcc_arm5vte () {
134 type ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc >/dev/null 2>&1
135}
136
137component_build_arm_none_eabi_gcc_arm5vte () {
138 msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=arm5vte, baremetal+debug" # ~ 10s
139 scripts/config.py baremetal
140 # This is an imperfect substitute for
141 # component_build_arm_linux_gnueabi_gcc_arm5vte
142 # in case the gcc-arm-linux-gnueabi toolchain is not available
143 make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" CFLAGS='-std=c99 -Werror -Wall -Wextra -march=armv5te -O1' LDFLAGS='-march=armv5te' SHELL='sh -x' lib
144
145 msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=armv5te -O1, baremetal+debug"
146 ${ARM_NONE_EABI_GCC_PREFIX}size -t library/*.o
147 ${ARM_NONE_EABI_GCC_PREFIX}size -t ${PSA_CORE_PATH}/*.o
148 ${ARM_NONE_EABI_GCC_PREFIX}size -t ${BUILTIN_SRC_PATH}/*.o
149}
150
151component_build_arm_none_eabi_gcc_m0plus () {
152 msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -mthumb -mcpu=cortex-m0plus, baremetal_size" # ~ 10s
153 scripts/config.py baremetal_size
154 make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -Wall -Wextra -mthumb -mcpu=cortex-m0plus -Os' lib
155
156 msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -mthumb -mcpu=cortex-m0plus -Os, baremetal_size"
157 ${ARM_NONE_EABI_GCC_PREFIX}size -t library/*.o
158 ${ARM_NONE_EABI_GCC_PREFIX}size -t ${PSA_CORE_PATH}/*.o
159 ${ARM_NONE_EABI_GCC_PREFIX}size -t ${BUILTIN_SRC_PATH}/*.o
160 for lib in library/*.a; do
161 echo "$lib:"
162 ${ARM_NONE_EABI_GCC_PREFIX}size -t $lib | grep TOTALS
163 done
164}
165
166component_build_arm_none_eabi_gcc_no_udbl_division () {
167 msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -DMBEDTLS_NO_UDBL_DIVISION, make" # ~ 10s
168 scripts/config.py baremetal
169 scripts/config.py set MBEDTLS_NO_UDBL_DIVISION
170 make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -Wall -Wextra' lib
171 echo "Checking that software 64-bit division is not required"
172 not grep __aeabi_uldiv library/*.o
173 not grep __aeabi_uldiv ${PSA_CORE_PATH}/*.o
174 not grep __aeabi_uldiv ${BUILTIN_SRC_PATH}/*.o
175}
176
177component_build_arm_none_eabi_gcc_no_64bit_multiplication () {
178 msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc MBEDTLS_NO_64BIT_MULTIPLICATION, make" # ~ 10s
179 scripts/config.py baremetal
180 scripts/config.py set MBEDTLS_NO_64BIT_MULTIPLICATION
181 make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -O1 -march=armv6-m -mthumb' lib
182 echo "Checking that software 64-bit multiplication is not required"
183 not grep __aeabi_lmul library/*.o
184 not grep __aeabi_lmul ${PSA_CORE_PATH}/*.o
185 not grep __aeabi_lmul ${BUILTIN_SRC_PATH}/*.o
186}
187
188component_build_arm_clang_thumb () {
189 # ~ 30s
190
191 scripts/config.py baremetal
192
193 msg "build: clang thumb 2, make"
194 make clean
195 make CC="clang" CFLAGS='-std=c99 -Werror -Os --target=arm-linux-gnueabihf -march=armv7-m -mthumb' lib
196
197 # Some Thumb 1 asm is sensitive to optimisation level, so test both -O0 and -Os
198 msg "build: clang thumb 1 -O0, make"
199 make clean
200 make CC="clang" CFLAGS='-std=c99 -Werror -O0 --target=arm-linux-gnueabihf -mcpu=arm1136j-s -mthumb' lib
201
202 msg "build: clang thumb 1 -Os, make"
203 make clean
204 make CC="clang" CFLAGS='-std=c99 -Werror -Os --target=arm-linux-gnueabihf -mcpu=arm1136j-s -mthumb' lib
205}
206
207component_build_armcc () {
208 msg "build: ARM Compiler 5"
209 scripts/config.py baremetal
210 # armc[56] don't support SHA-512 intrinsics
211 scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
212
213 # older versions of armcc/armclang don't support AESCE_C on 32-bit Arm
214 scripts/config.py unset MBEDTLS_AESCE_C
215
216 # Stop armclang warning about feature detection for A64_CRYPTO.
217 # With this enabled, the library does build correctly under armclang,
218 # but in baremetal builds (as tested here), feature detection is
219 # unavailable, and the user is notified via a #warning. So enabling
220 # this feature would prevent us from building with -Werror on
221 # armclang. Tracked in #7198.
222 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
223
224 scripts/config.py set MBEDTLS_HAVE_ASM
225
226 make CC="$ARMC5_CC" AR="$ARMC5_AR" WARNING_CFLAGS='--strict --c99' lib
227
228 msg "size: ARM Compiler 5"
229 "$ARMC5_FROMELF" -z library/*.o
230 "$ARMC5_FROMELF" -z ${PSA_CORE_PATH}/*.o
231 "$ARMC5_FROMELF" -z ${BUILTIN_SRC_PATH}/*.o
232
233 # Compile mostly with -O1 since some Arm inline assembly is disabled for -O0.
234
235 # ARM Compiler 6 - Target ARMv7-A
236 armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv7-a"
237
238 # ARM Compiler 6 - Target ARMv7-M
239 armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv7-m"
240
241 # ARM Compiler 6 - Target ARMv7-M+DSP
242 armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv7-m+dsp"
243
244 # ARM Compiler 6 - Target ARMv8-A - AArch32
245 armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv8.2-a"
246
247 # ARM Compiler 6 - Target ARMv8-M
248 armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv8-m.main"
249
250 # ARM Compiler 6 - Target Cortex-M0 - no optimisation
251 armc6_build_test "-O0 --target=arm-arm-none-eabi -mcpu=cortex-m0"
252
253 # ARM Compiler 6 - Target Cortex-M0
254 armc6_build_test "-Os --target=arm-arm-none-eabi -mcpu=cortex-m0"
255
256 # ARM Compiler 6 - Target ARMv8.2-A - AArch64
257 #
258 # Re-enable MBEDTLS_AESCE_C as this should be supported by the version of armclang
259 # that we have in our CI
260 scripts/config.py set MBEDTLS_AESCE_C
261 armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8.2-a+crypto"
262}
263
264support_build_armcc () {
265 armc5_cc="$ARMC5_BIN_DIR/armcc"
266 armc6_cc="$ARMC6_BIN_DIR/armclang"
267 (check_tools "$armc5_cc" "$armc6_cc" > /dev/null 2>&1)
268}
269
270component_build_mingw () {
271 msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
272 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
273
274 # note Make tests only builds the tests, but doesn't run them
275 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
276 make WINDOWS_BUILD=1 clean
277
278 msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
279 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
280 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
281 make WINDOWS_BUILD=1 clean
282
283 msg "build: Windows cross build - mingw64, make (Library only, default config without MBEDTLS_AESNI_C)" # ~ 30s
284 ./scripts/config.py unset MBEDTLS_AESNI_C #
285 make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib
286 make WINDOWS_BUILD=1 clean
287}
288
Minos Galanakisf78447f2024-07-26 20:49:51 +0100289support_build_mingw () {
Minos Galanakis9ebb8822024-07-26 15:21:14 +0100290 case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
291 [0-5]*|"") false;;
292 *) true;;
293 esac
294}
295
296component_test_zeroize () {
297 # Test that the function mbedtls_platform_zeroize() is not optimized away by
298 # different combinations of compilers and optimization flags by using an
299 # auxiliary GDB script. Unfortunately, GDB does not return error values to the
300 # system in all cases that the script fails, so we must manually search the
301 # output to check whether the pass string is present and no failure strings
302 # were printed.
303
304 # Don't try to disable ASLR. We don't care about ASLR here. We do care
305 # about a spurious message if Gdb tries and fails, so suppress that.
306 gdb_disable_aslr=
307 if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
308 gdb_disable_aslr='set disable-randomization off'
309 fi
310
311 for optimization_flag in -O2 -O3 -Ofast -Os; do
312 for compiler in clang gcc; do
313 msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
314 make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
315 gdb -ex "$gdb_disable_aslr" -x tests/scripts/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log
316 grep "The buffer was correctly zeroized" test_zeroize.log
317 not grep -i "error" test_zeroize.log
318 rm -f test_zeroize.log
319 make clean
320 done
321 done
322}