| 1 | #!/usr/bin/env bash |
| 2 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | # You may not use this file except in compliance with the License. |
| 6 | # A copy of the License is located at |
| 7 | # |
| 8 | # http://aws.amazon.com/apache2.0 |
| 9 | # |
| 10 | # or in the "license" file accompanying this file. This file is distributed |
| 11 | # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | # express or implied. See the License for the specific language governing |
| 13 | # permissions and limitations under the License. |
| 14 | # |
| 15 | |
| 16 | set -e |
| 17 | |
| 18 | # Test that all file descriptors are properly cleaned up when `exec`ing from |
| 19 | # from an initialized s2n-tls process. |
| 20 | |
| 21 | source codebuild/bin/s2n_setup_env.sh |
| 22 | source codebuild/bin/jobs.sh |
| 23 | |
| 24 | function build() { |
| 25 | echo "=== BUILDING $1 ===" |
| 26 | cmake . -B$1 -DCMAKE_PREFIX_PATH=$TARGET_LIBCRYPTO_PATH ${@:2} |
| 27 | cmake --build $1 -- -j $JOBS |
| 28 | } |
| 29 | |
| 30 | function fail() { |
| 31 | echo "test failure: $1" |
| 32 | exit 1 |
| 33 | } |
| 34 | |
| 35 | function write_exec_app() { |
| 36 | cat <<EOF > build/detect_exec_leak.c |
| 37 | #include <s2n.h> |
| 38 | #include "unistd.h" |
| 39 | |
| 40 | int main() { |
| 41 | s2n_init(); |
| 42 | execl("build/bin/detect_exec_leak_finish", "", NULL); |
| 43 | return 0; |
| 44 | } |
| 45 | EOF |
| 46 | } |
| 47 | |
| 48 | function write_exec_finish_app() { |
| 49 | cat <<EOF > build/detect_exec_leak_finish.c |
| 50 | #include <s2n.h> |
| 51 | |
| 52 | int main() { |
| 53 | s2n_init(); |
| 54 | s2n_cleanup_final(); |
| 55 | |
| 56 | /* close std* file descriptors so valgrind output is less noisy */ |
| 57 | fclose(stdin); |
| 58 | fclose(stdout); |
| 59 | fclose(stderr); |
| 60 | return 0; |
| 61 | } |
| 62 | EOF |
| 63 | } |
| 64 | |
| 65 | # download libcrypto if its not available |
| 66 | TARGET_LIBCRYPTO="${S2N_LIBCRYPTO//[-.]/_}" |
| 67 | TARGET_LIBCRYPTO_PATH="${TEST_DEPS_DIR}/${S2N_LIBCRYPTO}" |
| 68 | if [ ! -f $TARGET_LIBCRYPTO_PATH/lib/libcrypto.a ]; then |
| 69 | ./codebuild/bin/install_${TARGET_LIBCRYPTO}.sh $TARGET_LIBCRYPTO_PATH/src $TARGET_LIBCRYPTO_PATH linux |
| 70 | fi |
| 71 | |
| 72 | # build s2n-tls |
| 73 | build build -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=on |
| 74 | |
| 75 | # compile the test app for exec leak test |
| 76 | mkdir -p build/valgrind_log_dir |
| 77 | write_exec_app |
| 78 | write_exec_finish_app |
| 79 | cc -Iapi build/detect_exec_leak.c build/lib/libs2n.so -o build/bin/detect_exec_leak |
| 80 | cc -Iapi build/detect_exec_leak_finish.c build/lib/libs2n.so -o build/bin/detect_exec_leak_finish |
| 81 | |
| 82 | # run valgrind with track-fds enabled |
| 83 | valgrind_log_dir=valgrind_log_dir |
| 84 | for test_file in detect_exec_leak detect_exec_leak_finish; do |
| 85 | LD_LIBRARY_PATH="build/lib:$TARGET_LIBCRYPTO_PATH/lib:$LD_LIBRARY_PATH" S2N_VALGRIND=1 \ |
| 86 | valgrind --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all \ |
| 87 | --run-libc-freeres=yes -q --gen-suppressions=all --track-fds=yes \ |
| 88 | --leak-resolution=high --undef-value-errors=no --trace-children=yes \ |
| 89 | --suppressions=tests/unit/valgrind.suppressions --log-file="build/$valgrind_log_dir/$test_file" \ |
| 90 | build/bin/$test_file |
| 91 | |
| 92 | # search for all leaked file descriptors, excluding the valgrind_log_dir file |
| 93 | cat build/$valgrind_log_dir/$test_file | \ |
| 94 | grep "Open file descriptor" | \ |
| 95 | grep --invert-match $valgrind_log_dir \ |
| 96 | && fail "file leak detected while running $test_file" |
| 97 | done |
| 98 | |
| 99 | echo pass |