diff --git a/CMakeLists.txt b/CMakeLists.txt index a7d422d3646..886c6366043 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ option(S2N_LTO, "Enables link time optimizations when building s2n-tls." OFF) option(S2N_STACKTRACE "Enables stacktrace functionality in s2n-tls. Note that this functionality is only available on platforms that support execinfo." ON) option(COVERAGE "Enable profiling collection for code coverage calculation" OFF) +option(BUILD_TESTING "Build tests for s2n-tls. By default only unit tests are built." ON) option(S2N_INTEG_TESTS "Enable the integrationv2 tests" OFF) option(S2N_FAST_INTEG_TESTS "Enable the integrationv2 with more parallelism, only has effect if S2N_INTEG_TESTS=ON" ON) option(S2N_INSTALL_S2NC_S2ND "Install the binaries s2nc and s2nd" OFF) @@ -41,9 +42,6 @@ option(TSAN "Enable ThreadSanitizer to test thread safety" OFF) option(ASAN "Enable AddressSanitizer to test memory safety" OFF) option(SECCOMP "Link with seccomp and run seccomp tests" OFF) -# Turn BUILD_TESTING=ON by default -include(CTest) - file(GLOB API_HEADERS "api/*.h") file(GLOB API_UNSTABLE_HEADERS "api/unstable/*.h") @@ -497,6 +495,29 @@ if (BUILD_TESTING) ########################## configure unit tests ############################ ############################################################################ + # CTest configuration variables need to be set before include(CTest) is called + set(VALGRIND_DEFAULT " \ + --leak-check=full \ + --leak-resolution=high \ + --trace-children=yes \ + -q --error-exitcode=123 \ + --error-limit=no \ + --num-callers=40 \ + --undef-value-errors=no \ + --log-fd=2 \ + --suppressions=valgrind.suppressions") + + # "pedantic valgrind" will error on memory that is "Still Reachable". + # We only run this on OpenSSL 1.1.1 because there are hundreds of false positives in other libcryptos. + # Tracking issue: https://github.com/aws/s2n-tls/issues/4777 + if ($ENV{S2N_LIBCRYPTO} MATCHES "openssl-1.1.1") + set(MEMORYCHECK_COMMAND_OPTIONS "${VALGRIND_DEFAULT} --run-libc-freeres=yes --errors-for-leak-kinds=all --show-leak-kinds=all") + else() + set(MEMORYCHECK_COMMAND_OPTIONS "${VALGRIND_DEFAULT} --run-libc-freeres=no") + endif() + + set(MEMORYCHECK_TYPE "Valgrind") + set(UNIT_TEST_ENVS S2N_DONT_MLOCK=1) if (TSAN OR ASAN) set(UNIT_TEST_ENVS ${UNIT_TEST_ENVS} S2N_ADDRESS_SANITIZER=1) @@ -525,6 +546,8 @@ if (BUILD_TESTING) endif() message(STATUS "Running tests with environment: ${UNIT_TEST_ENVS}") + include(CTest) + ############################################################################ ############################ build unit tests ############################## ############################################################################ diff --git a/codebuild/spec/buildspec_valgrind.yml b/codebuild/spec/buildspec_valgrind.yml new file mode 100644 index 00000000000..01c9b21bb9b --- /dev/null +++ b/codebuild/spec/buildspec_valgrind.yml @@ -0,0 +1,72 @@ +--- +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use +# this file except in compliance with the License. A copy of the License is +# located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "license" file accompanying this file. This file is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. See the License for the specific language governing permissions and +# limitations under the License. +version: 0.2 + +batch: + build-list: + - identifier: gcc_awslc + env: + compute-type: BUILD_GENERAL1_LARGE + image: 024603541914.dkr.ecr.us-west-2.amazonaws.com/docker:ubuntu22codebuild + variables: + S2N_LIBCRYPTO: awslc + COMPILER: gcc + - identifier: gcc_openssl_3_0 + env: + compute-type: BUILD_GENERAL1_LARGE + image: 024603541914.dkr.ecr.us-west-2.amazonaws.com/docker:ubuntu22codebuild + variables: + S2N_LIBCRYPTO: openssl-3.0 + COMPILER: gcc + - identifier: gcc_openssl_1_1_1 + env: + compute-type: BUILD_GENERAL1_LARGE + image: 024603541914.dkr.ecr.us-west-2.amazonaws.com/docker:ubuntu18codebuild + variables: + S2N_LIBCRYPTO: openssl-1.1.1 + COMPILER: gcc + - identifier: gcc_openssl_1_0_2 + env: + compute-type: BUILD_GENERAL1_LARGE + image: 024603541914.dkr.ecr.us-west-2.amazonaws.com/docker:ubuntu22codebuild + variables: + S2N_LIBCRYPTO: openssl-1.0.2 + COMPILER: gcc + +phases: + pre_build: + commands: + - | + if [ -d "third-party-src" ]; then + cd third-party-src; + fi + - /usr/bin/$COMPILER --version + build: + on-failure: ABORT + commands: + - | + cmake . -Bbuild \ + -DCMAKE_C_COMPILER=/usr/bin/$COMPILER \ + -DCMAKE_PREFIX_PATH=/usr/local/$S2N_LIBCRYPTO \ + -DCMAKE_BUILD_TYPE=RelWithDebInfo + - cmake --build ./build -- -j $(nproc) + post_build: + on-failure: ABORT + commands: + - | + S2N_VALGRIND=1 \ + CTEST_PARALLEL_LEVEL=$(nproc) \ + CTEST_OUTPUT_ON_FAILURE=1 \ + cmake --build build/ --target test \ + -- ARGS="--test-action memcheck" diff --git a/tests/unit/valgrind.suppressions b/tests/unit/valgrind.suppressions index 3662410a9bf..e0f90fd05cb 100644 --- a/tests/unit/valgrind.suppressions +++ b/tests/unit/valgrind.suppressions @@ -1,13 +1,48 @@ -# It looks like valgrind may generate false positives on pthreads: https://stackoverflow.com/a/13132968 +# Valgrind may generate false positives on pthreads: https://stackoverflow.com/a/13132968 +# Without these suppressions, the following tests will fail: +# s2n_examples_test, s2n_fork_generation_number_test, s2n_init_test, s2n_key_update_threads_test, and s2n_random_test. { - pthred_false_positive + pthread_false_positive Memcheck:Leak match-leak-kinds: possible fun:calloc + ... fun:allocate_dtv fun:_dl_allocate_tls fun:allocate_stack - fun:pthread_create@@GLIBC_2.2.5 + fun:pthread_create@@* + ... + fun:main +} + +# This memory leak is believed to be caused by backtrace() loading libgcc dynamically. +# See https://man7.org/linux/man-pages/man3/backtrace_symbols_fd.3.html +# We were unable to find any relevant bug reports. However, testing showed that the memory +# leak didn't scale with the number of calls to backtrace(), both supporting this theory and +# limiting the potential impact of the leak. +{ + stacktrace_suppression + Memcheck:Leak + match-leak-kinds: possible + fun:malloc + fun:malloc + fun:_dlfo_mappings_segment_allocate + fun:_dl_find_object_update_1 + fun:_dl_find_object_update + fun:dl_open_worker_begin + fun:_dl_catch_exception + fun:dl_open_worker + fun:_dl_catch_exception + fun:_dl_open + fun:do_dlopen + fun:_dl_catch_exception + fun:_dl_catch_error + fun:dlerror_run + fun:__libc_dlopen_mode + fun:__libc_unwind_link_get + ... + fun:backtrace + ... fun:main }