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