Debian Codegraph @

crt/s2n/codebuild/bin/s2n_open_fds_test.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3
4 # This script parses the LastDynamicAnalysis file generated by Valgrind running through CTest memcheck.
5 # It identifies any leaking file descriptors and triggers an error when detected.
6 # This enhances the capabilities of existing Valgrind checks.
7 # Output snippet for open file descriptors:
8 # ==6652== FILE DESCRIPTORS: 6 open (3 std) at exit.
9 # ==6652== Open AF_INET socket 6: 127.0.0.1:36915 <-> unbound
10 # ==6652== at 0x498B2EB: socket (syscall-template.S:120)
11 # ==6652== by 0x16CD16: s2n_new_inet_socket_pair (s2n_self_talk_ktls_test.c:69)
12 # ==6652== by 0x15DBB2: main (s2n_self_talk_ktls_test.c:168)
13 # ==6652==
14 import os
15 import sys
16
17 EXIT_SUCCESS = 0
18 # Exit with error code 1 if leaking fds are detected.
19 ERROR_EXIT_CODE = 1
20 # This test is designed to be informational only, so we only print fifteen lines of error messages when a leak is detected.
21 NUM_OF_LINES_TO_PRINT = 15
22
23
24 def find_log_file(path):
25 for f in os.listdir(path):
26 if "LastDynamicAnalysis" in f:
27 return os.path.join(path, f)
28
29 raise FileNotFoundError("LastDynamicAnalysis log file is not found!")
30
31
32 def detect_leak(file):
33 fd_leak_detected = False
34 lines = file.readlines()
35 for i in range(len(lines)):
36 if "FILE DESCRIPTORS:" in lines[i]:
37 # Example line: `==6096== FILE DESCRIPTORS: 4 open (3 std) at exit.`
38 line_elements = lines[i].split()
39 open_fd_count = line_elements[line_elements.index("DESCRIPTORS:") + 1]
40 std_fd_count = line_elements[line_elements.index("std)") - 1][1:]
41 # CTest memcheck writes to a LastDynamicAnslysis log file.
42 # We allow that fd to remain opened.
43 if int(open_fd_count) > int(std_fd_count) + 1:
44 for j in range(NUM_OF_LINES_TO_PRINT):
45 print(lines[i + j], end="")
46 print()
47 fd_leak_detected = True
48 return fd_leak_detected
49
50
51 def main():
52 # Print banner of the test
53 print("############################################################################")
54 print("################# Test for Leaking File Descriptors ########################")
55 print("############################################################################")
56
57 with open(find_log_file(sys.argv[1]), 'r') as file:
58 if detect_leak(file):
59 sys.exit(ERROR_EXIT_CODE)
60
61 return EXIT_SUCCESS
62
63
64 if __name__ == '__main__':
65 main()