Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add open fds valgrind check #4851

Merged
merged 21 commits into from
Nov 12, 2024

Conversation

boquan-fang
Copy link
Contributor

@boquan-fang boquan-fang commented Oct 17, 2024

Resolved issues:

Partially solve #4005

Description of changes:

  • Add --track-fds=yes options to VALGRIND_DEFAULT option.
  • Add s2n_open_fds_test.py script to test for any unexpected open file descriptors at exit.
  • Integrate this script to buildspec_valgrind.yml.

Call-outs:

  • Valgrind doesn't treat leaking file descriptors as error by default.
    • Here are some discussions I found to support such argument (discussion 1, disucssion 2).
    • Our current way of doing similar check is in test_exec_leak.sh. We are also storing open fd error information in a log file and then query from there.
      valgrind_log_dir=valgrind_log_dir
      for test_file in detect_exec_leak detect_exec_leak_finish; do
      LD_LIBRARY_PATH="build/lib:$TARGET_LIBCRYPTO_PATH/lib:$LD_LIBRARY_PATH" S2N_VALGRIND=1 \
      valgrind --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all \
      --run-libc-freeres=yes -q --gen-suppressions=all --track-fds=yes \
      --leak-resolution=high --undef-value-errors=no --trace-children=yes \
      --suppressions=tests/unit/valgrind.suppressions --log-file="build/$valgrind_log_dir/$test_file" \
      build/bin/$test_file
      # search for all leaked file descriptors, excluding the valgrind_log_dir file
      cat build/$valgrind_log_dir/$test_file | \
      grep "Open file descriptor" | \
      grep --invert-match $valgrind_log_dir \
      && fail "file leak detected while running $test_file"
      done
    • That is why I choose this approach to write a Python script to parse LastDynamicAnalysis log file generated directly by CTest memcheck.
  • The new Valgrind test will first test for memory loss. Once that test passed, then it will run s2n_open_fds_test.py script to test for open fds.
    • If a test has both memory loss and leaking open fds, then CTest memcheck will display both information.
    • If a test doesn't have any memory loss, but it leaks fds, then the Python script will capture those fd leaks and display them after CTest memcheck.
  • If there is no fd leaks, then the number of open fds is going to be one more than the number of open std fds.
    • Valgrind version greater than 3.17 provides a good summary of how many fds are opened and how many std fds are opened.
    • Hence, we need to update the existing pedantic check to use Ubuntu 24, in order for this test to be merged. The pedantic check is running in Ubuntu 18 currently, and the Valgrind version in Ubuntu 18 is less than 3.17.
  • We can't simply check if there are more than 4 open file descriptors. There are tests who intentionally close one or more std.
    fclose(stdout);
    • If a test close a std, and leak another fd, that is still problematic, but if we are only checking the number of open fds is four, then that will treat that error as correct.
  • One of the PR to close all /dev/urandom is not merged into the main branch. This branch is also not updated with main, so the valgrind test is supposed to fail right now.
    • Furthermore, those fixes are discovered in Ubuntu 18. If this test detects more problems on other platforms, then I will fix them in future PRs.
    • Here are some previous PRs that fixes all open fds issues on Ubuntu 18 (clean io pair, clean io pair with fork).

Testing:

  • Test it locally with Ubuntu 18, 22, and 24 docker images.
  • Test it on AWS codebuild. A failed result looks like this. A success test result looks like this.
    By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Boquan Fang added 2 commits October 17, 2024 00:12
* handle if the first FILE DESCRIPTORS string come on the same line as
  the Running test string
@github-actions github-actions bot added the s2n-core team label Oct 17, 2024
@boquan-fang boquan-fang marked this pull request as ready for review October 17, 2024 21:05
Copy link
Contributor

@dougch dougch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For regex use, I'd really like to see a couple tests showing the parsing working/failing.

codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/spec/buildspec_valgrind.yml Outdated Show resolved Hide resolved
* fix two regexes to make them capture the correct portion of the tests
* add banner to valgrind buildspec file
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
Boquan Fang added 2 commits October 22, 2024 20:32
* Add tests for regex usages
    * Add __init__.py to make directories python module
    * Import regexes directly from the python script
* Modify valgrind yml file
* add license, and explanations for the file
* add banner to the python script
tests/regex/s2n_regex_test.py Fixed Show fixed Hide fixed
tests/regex/s2n_regex_test.py Fixed Show fixed Hide fixed
* We can't simply check if there are more than 4 open file descriptors
    * There are tests who intentionally close a std, and that will lead
      the total counts to 2.
    * One way is to check if open fds are one more than n std, but that
      information is not in older valgrind.
    * If there is no fd leaks, then the FILE DESCRIPTORS: will always be
      followed by the fd pointing to LastDynamicAnalysis log file
        * The test will check for this to determine if there are any
	  leaking fds
* Change testing for regexes
    * We only have one regex now, so delete those tests for previous
      regexes
Copy link
Contributor

@dougch dougch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With James' document comments addressed...

codebuild/bin/s2n_open_fds_test.py Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
tests/regex/s2n_regex_test.py Outdated Show resolved Hide resolved
Boquan Fang added 2 commits October 23, 2024 21:23
* remove all regexes usages and use in to do string matching
* fix pedantic check and make every valgrind test runnable in Ubuntu 22+
* simplify the testing logic by checking if there is one more open fds
  than open std fds
* only print the next 5 lines once a problem is detected for
  informational purpose only
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
Boquan Fang added 2 commits November 4, 2024 18:12
* make comments more concise
* add error handling for file not found or other file related
  problems
Copy link
Contributor

@dougch dougch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the regex, lgtm

@dougch dougch self-requested a review November 6, 2024 23:59
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
codebuild/bin/s2n_open_fds_test.py Outdated Show resolved Hide resolved
* reduce the abstraction of main function
@boquan-fang
Copy link
Contributor Author

boquan-fang commented Nov 8, 2024

This PR needs to update the Ubuntu version from 18 to 24 for pedantic Valgrind test. Such update can only be done after PR#4852 is merged.

Such update is done. This test will success in Codebuild now. The result is attached.

@boquan-fang boquan-fang enabled auto-merge (squash) November 12, 2024 18:10
@boquan-fang boquan-fang merged commit 0807885 into aws:main Nov 12, 2024
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants