blob: 1e9f4b8c549a2c95e7e425dc162d0d47f7f178cc [file] [log] [blame]
Minos Galanakis6aab5b72024-07-25 14:24:37 +01001# components-sanitizers.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#### Sanitizer Testing
10################################################################
11
Minos Galanakise280ff82024-07-26 17:42:50 +010012skip_suites_without_constant_flow () {
13 # Skip the test suites that don't have any constant-flow annotations.
14 # This will need to be adjusted if we ever start declaring things as
15 # secret from macros or functions inside tests/include or tests/src.
16 SKIP_TEST_SUITES=$(
17 git -C tests/suites grep -L TEST_CF_ 'test_suite_*.function' |
18 sed 's/test_suite_//; s/\.function$//' |
19 tr '\n' ,),$(
20 git -C tf-psa-crypto/tests/suites grep -L TEST_CF_ 'test_suite_*.function' |
21 sed 's/test_suite_//; s/\.function$//' |
22 tr '\n' ,)
23 export SKIP_TEST_SUITES
24}
25
26skip_all_except_given_suite () {
27 # Skip all but the given test suite
28 SKIP_TEST_SUITES=$(
29 ls -1 tests/suites/test_suite_*.function |
30 grep -v $1.function |
31 sed 's/tests.suites.test_suite_//; s/\.function$//' |
32 tr '\n' ,),$(
33 ls -1 tf-psa-crypto/tests/suites/test_suite_*.function |
34 grep -v $1.function |
35 sed 's/tf-psa-crypto.tests.suites.test_suite_//; s/\.function$//' |
36 tr '\n' ,)
37 export SKIP_TEST_SUITES
38}
39
40component_test_memsan_constant_flow () {
41 # This tests both (1) accesses to undefined memory, and (2) branches or
42 # memory access depending on secret values. To distinguish between those:
43 # - unset MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN - does the failure persist?
44 # - or alternatively, change the build type to MemSanDbg, which enables
45 # origin tracking and nicer stack traces (which are useful for debugging
46 # anyway), and check if the origin was TEST_CF_SECRET() or something else.
47 msg "build: cmake MSan (clang), full config minus MBEDTLS_USE_PSA_CRYPTO with constant flow testing"
48 scripts/config.py full
49 scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN
50 scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
51 scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm
Elena Uziunaite969e9e12024-07-01 16:55:19 +010052 scripts/config.py unset MBEDTLS_HAVE_ASM
Minos Galanakise280ff82024-07-26 17:42:50 +010053 CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
54 make
55
56 msg "test: main suites (full minus MBEDTLS_USE_PSA_CRYPTO, Msan + constant flow)"
57 make test
58}
59
60component_test_memsan_constant_flow_psa () {
61 # This tests both (1) accesses to undefined memory, and (2) branches or
62 # memory access depending on secret values. To distinguish between those:
63 # - unset MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN - does the failure persist?
64 # - or alternatively, change the build type to MemSanDbg, which enables
65 # origin tracking and nicer stack traces (which are useful for debugging
66 # anyway), and check if the origin was TEST_CF_SECRET() or something else.
67 msg "build: cmake MSan (clang), full config with constant flow testing"
68 scripts/config.py full
69 scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN
70 scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm
Elena Uziunaite969e9e12024-07-01 16:55:19 +010071 scripts/config.py unset MBEDTLS_HAVE_ASM
Minos Galanakise280ff82024-07-26 17:42:50 +010072 CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
73 make
74
75 msg "test: main suites (Msan + constant flow)"
76 make test
77}
78
79component_release_test_valgrind_constant_flow () {
80 # This tests both (1) everything that valgrind's memcheck usually checks
81 # (heap buffer overflows, use of uninitialized memory, use-after-free,
82 # etc.) and (2) branches or memory access depending on secret values,
83 # which will be reported as uninitialized memory. To distinguish between
84 # secret and actually uninitialized:
85 # - unset MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND - does the failure persist?
86 # - or alternatively, build with debug info and manually run the offending
87 # test suite with valgrind --track-origins=yes, then check if the origin
88 # was TEST_CF_SECRET() or something else.
89 msg "build: cmake release GCC, full config minus MBEDTLS_USE_PSA_CRYPTO with constant flow testing"
90 scripts/config.py full
91 scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND
92 scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
93 skip_suites_without_constant_flow
94 cmake -D CMAKE_BUILD_TYPE:String=Release .
95 make
96
97 # this only shows a summary of the results (how many of each type)
98 # details are left in Testing/<date>/DynamicAnalysis.xml
99 msg "test: some suites (full minus MBEDTLS_USE_PSA_CRYPTO, valgrind + constant flow)"
100 make memcheck
101
102 # Test asm path in constant time module - by default, it will test the plain C
103 # path under Valgrind or Memsan. Running only the constant_time tests is fast (<1s)
104 msg "test: valgrind asm constant_time"
105 scripts/config.py --force set MBEDTLS_TEST_CONSTANT_FLOW_ASM
106 skip_all_except_given_suite test_suite_constant_time
107 cmake -D CMAKE_BUILD_TYPE:String=Release .
108 make clean
109 make
110 make memcheck
111}
112
Elena Uziunaiteaff61f32024-07-02 15:52:03 +0100113component_release_test_valgrind_constant_flow_no_asm () {
114 # This tests both (1) everything that valgrind's memcheck usually checks
115 # (heap buffer overflows, use of uninitialized memory, use-after-free,
116 # etc.) and (2) branches or memory access depending on secret values,
117 # which will be reported as uninitialized memory. To distinguish between
118 # secret and actually uninitialized:
119 # - unset MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND - does the failure persist?
120 # - or alternatively, build with debug info and manually run the offending
121 # test suite with valgrind --track-origins=yes, then check if the origin
122 # was TEST_CF_SECRET() or something else.
123 msg "build: cmake release GCC, full config minus MBEDTLS_USE_PSA_CRYPTO, minus MBEDTLS_HAVE_ASM with constant flow testing"
124 scripts/config.py full
125 scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND
126 scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
127 scripts/config.py unset MBEDTLS_AESNI_C
128 scripts/config.py unset MBEDTLS_HAVE_ASM
129 skip_suites_without_constant_flow
130 cmake -D CMAKE_BUILD_TYPE:String=Release .
131 make
132
133 # this only shows a summary of the results (how many of each type)
134 # details are left in Testing/<date>/DynamicAnalysis.xml
135 msg "test: some suites (full minus MBEDTLS_USE_PSA_CRYPTO, minus MBEDTLS_HAVE_ASM, valgrind + constant flow)"
136 make memcheck
137
138 # Test asm path in constant time module - by default, it will test the plain C
139 # path under Valgrind or Memsan. Running only the constant_time tests is fast (<1s)
140 msg "test: valgrind asm constant_time"
141 scripts/config.py --force set MBEDTLS_TEST_CONSTANT_FLOW_ASM
142 skip_all_except_given_suite test_suite_constant_time
143 cmake -D CMAKE_BUILD_TYPE:String=Release .
144 make clean
145 make
146 make memcheck
147}
148
Minos Galanakise280ff82024-07-26 17:42:50 +0100149component_release_test_valgrind_constant_flow_psa () {
150 # This tests both (1) everything that valgrind's memcheck usually checks
151 # (heap buffer overflows, use of uninitialized memory, use-after-free,
152 # etc.) and (2) branches or memory access depending on secret values,
153 # which will be reported as uninitialized memory. To distinguish between
154 # secret and actually uninitialized:
155 # - unset MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND - does the failure persist?
156 # - or alternatively, build with debug info and manually run the offending
157 # test suite with valgrind --track-origins=yes, then check if the origin
158 # was TEST_CF_SECRET() or something else.
159 msg "build: cmake release GCC, full config with constant flow testing"
160 scripts/config.py full
161 scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND
162 skip_suites_without_constant_flow
163 cmake -D CMAKE_BUILD_TYPE:String=Release .
164 make
165
166 # this only shows a summary of the results (how many of each type)
167 # details are left in Testing/<date>/DynamicAnalysis.xml
168 msg "test: some suites (valgrind + constant flow)"
169 make memcheck
170}
171
Elena Uziunaiteaff61f32024-07-02 15:52:03 +0100172component_release_test_valgrind_constant_flow_psa_no_asm () {
173 # This tests both (1) everything that valgrind's memcheck usually checks
174 # (heap buffer overflows, use of uninitialized memory, use-after-free,
175 # etc.) and (2) branches or memory access depending on secret values,
176 # which will be reported as uninitialized memory. To distinguish between
177 # secret and actually uninitialized:
178 # - unset MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND - does the failure persist?
179 # - or alternatively, build with debug info and manually run the offending
180 # test suite with valgrind --track-origins=yes, then check if the origin
181 # was TEST_CF_SECRET() or something else.
182 msg "build: cmake release GCC, full config minus MBEDTLS_HAVE_ASM with constant flow testing"
183 scripts/config.py full
184 scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND
185 scripts/config.py unset MBEDTLS_AESNI_C
186 scripts/config.py unset MBEDTLS_HAVE_ASM
187 skip_suites_without_constant_flow
188 cmake -D CMAKE_BUILD_TYPE:String=Release .
189 make
190
191 # this only shows a summary of the results (how many of each type)
192 # details are left in Testing/<date>/DynamicAnalysis.xml
193 msg "test: some suites (full minus MBEDTLS_HAVE_ASM, valgrind + constant flow)"
194 make memcheck
195}
196
Minos Galanakise280ff82024-07-26 17:42:50 +0100197component_test_tsan () {
198 msg "build: TSan (clang)"
199 scripts/config.py full
200 scripts/config.py set MBEDTLS_THREADING_C
201 scripts/config.py set MBEDTLS_THREADING_PTHREAD
202 # Self-tests do not currently use multiple threads.
203 scripts/config.py unset MBEDTLS_SELF_TEST
204
205 # The deprecated MBEDTLS_PSA_CRYPTO_SE_C interface is not thread safe.
206 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
207
208 CC=clang cmake -D CMAKE_BUILD_TYPE:String=TSan .
209 make
210
211 msg "test: main suites (TSan)"
212 make test
213}
214
215component_test_memsan () {
216 msg "build: MSan (clang)" # ~ 1 min 20s
217 scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm
Elena Uziunaitedcb5d832024-07-02 10:52:13 +0100218 scripts/config.py unset MBEDTLS_HAVE_ASM
Minos Galanakise280ff82024-07-26 17:42:50 +0100219 CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
220 make
221
222 msg "test: main suites (MSan)" # ~ 10s
223 make test
224
225 msg "test: metatests (MSan)"
226 tests/scripts/run-metatests.sh any msan
227
228 msg "program demos (MSan)" # ~20s
229 tests/scripts/run_demos.py
230
231 msg "test: ssl-opt.sh (MSan)" # ~ 1 min
232 tests/ssl-opt.sh
233
234 # Optional part(s)
235
236 if [ "$MEMORY" -gt 0 ]; then
237 msg "test: compat.sh (MSan)" # ~ 6 min 20s
238 tests/compat.sh
239 fi
240}
241
242component_release_test_valgrind () {
243 msg "build: Release (clang)"
244 # default config, in particular without MBEDTLS_USE_PSA_CRYPTO
245 CC=clang cmake -D CMAKE_BUILD_TYPE:String=Release .
246 make
247
248 msg "test: main suites, Valgrind (default config)"
249 make memcheck
250
251 # Optional parts (slow; currently broken on OS X because programs don't
252 # seem to receive signals under valgrind on OS X).
253 # These optional parts don't run on the CI.
254 if [ "$MEMORY" -gt 0 ]; then
255 msg "test: ssl-opt.sh --memcheck (default config)"
256 tests/ssl-opt.sh --memcheck
257 fi
258
259 if [ "$MEMORY" -gt 1 ]; then
260 msg "test: compat.sh --memcheck (default config)"
261 tests/compat.sh --memcheck
262 fi
263
264 if [ "$MEMORY" -gt 0 ]; then
265 msg "test: context-info.sh --memcheck (default config)"
266 tests/context-info.sh --memcheck
267 fi
268}
269
270component_release_test_valgrind_psa () {
271 msg "build: Release, full (clang)"
272 # full config, in particular with MBEDTLS_USE_PSA_CRYPTO
273 scripts/config.py full
274 CC=clang cmake -D CMAKE_BUILD_TYPE:String=Release .
275 make
276
277 msg "test: main suites, Valgrind (full config)"
278 make memcheck
279}
280