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