From 4e8b5940020b6097f5941a243be6d9eeb41ba4d2 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 20 Sep 2019 19:12:31 +0200 Subject: [PATCH 1/3] Fix uninitialized variable in an edge case If `context_buf = mbedtls_calloc( 1, buf_len )` failed, `context_buf_len` was not initialized. Noticed by `gcc -Os -Werror=maybe-uninitialized`. This was only a problem in ssl_server2 (a test program), only with MBEDTLS_SSL_CONTEXT_SERIALIZATION enabled. --- programs/ssl/ssl_server2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 27f231230..d23a700f8 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -1820,7 +1820,7 @@ int main( int argc, char *argv[] ) #endif #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) unsigned char *context_buf = NULL; - size_t context_buf_len; + size_t context_buf_len = 0; #endif int i; From 6ec0f0f6d0b5d573bf5754cab25d10517460e302 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 20 Sep 2019 19:23:10 +0200 Subject: [PATCH 2/3] Replace -O0 by -O1 or -Os in most components Gcc skips some analyses when compiling with -O0, so we may miss warnings about things like uninitialized variables. --- tests/scripts/all.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index d21f1cee5..0b3b4471e 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -1011,8 +1011,8 @@ component_test_no_platform () { scripts/config.py unset MBEDTLS_PSA_ITS_FILE_C # Note, _DEFAULT_SOURCE needs to be defined for platforms using glibc version >2.19, # to re-enable platform integration features otherwise disabled in C99 builds - make CC=gcc CFLAGS='-Werror -Wall -Wextra -std=c99 -pedantic -O0 -D_DEFAULT_SOURCE' lib programs - make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0' test + make CC=gcc CFLAGS='-Werror -Wall -Wextra -std=c99 -pedantic -Os -D_DEFAULT_SOURCE' lib programs + make CC=gcc CFLAGS='-Werror -Wall -Wextra -Os' test } component_build_no_std_function () { @@ -1021,21 +1021,21 @@ component_build_no_std_function () { scripts/config.py full scripts/config.py set MBEDTLS_PLATFORM_NO_STD_FUNCTIONS scripts/config.py unset MBEDTLS_ENTROPY_NV_SEED - make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0' + make CC=gcc CFLAGS='-Werror -Wall -Wextra -Os' } component_build_no_ssl_srv () { msg "build: full config except ssl_srv.c, make, gcc" # ~ 30s scripts/config.py full scripts/config.py unset MBEDTLS_SSL_SRV_C - make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0' + make CC=gcc CFLAGS='-Werror -Wall -Wextra -O1' } component_build_no_ssl_cli () { msg "build: full config except ssl_cli.c, make, gcc" # ~ 30s scripts/config.py full scripts/config.py unset MBEDTLS_SSL_CLI_C - make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0' + make CC=gcc CFLAGS='-Werror -Wall -Wextra -O1' } component_build_no_sockets () { @@ -1045,7 +1045,7 @@ component_build_no_sockets () { scripts/config.py full scripts/config.py unset MBEDTLS_NET_C # getaddrinfo() undeclared, etc. scripts/config.py set MBEDTLS_NO_PLATFORM_ENTROPY # uses syscall() on GNU/Linux - make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0 -std=c99 -pedantic' lib + make CC=gcc CFLAGS='-Werror -Wall -Wextra -O1 -std=c99 -pedantic' lib } component_test_memory_buffer_allocator_backtrace () { From ec10bf138565e68f8d2e8268be7073bdae652d5e Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 20 Sep 2019 19:56:06 +0200 Subject: [PATCH 3/3] Test GCC and Clang with common build options Goals: * Build with common compilers with common options, so that we don't miss a (potentially useful) warning only triggered with certain build options. * A previous commit removed -O0 test jobs, leaving only the one with -m32. We have inline assembly that is disabled with -O0, falling back to generic C code. This commit restores a test that runs the generic C code on a 64-bit platform. --- tests/scripts/all.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 0b3b4471e..bce414fb1 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -1170,6 +1170,30 @@ component_test_cmake_shared () { make test } +test_build_opt () { + info=$1 cc=$2; shift 2 + for opt in "$@"; do + msg "build/test: $cc $opt, $info" # ~ 30s + make CC="$cc" CFLAGS="$opt -Wall -Wextra -Werror" + # We're confident enough in compilers to not run _all_ the tests, + # but at least run the unit tests. In particular, runs with + # optimizations use inline assembly whereas runs with -O0 + # skip inline assembly. + make test # ~30s + make clean + done +} + +component_test_clang_opt () { + scripts/config.pl full + test_build_opt 'full config' clang -O0 -Os -O2 +} + +component_test_gcc_opt () { + scripts/config.pl full + test_build_opt 'full config' gcc -O0 -Os -O2 +} + component_build_mbedtls_config_file () { msg "build: make with MBEDTLS_CONFIG_FILE" # ~40s # Use the full config so as to catch a maximum of places where