Debian Codegraph @

crt/s2n/tests/unit/s2n_mem_usage_test.c

raw symbols SCIP
1 /*
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 #ifdef __FreeBSD__
17 /* FreeBSD requires POSIX compatibility off for its syscalls (enables __BSD_VISIBLE)
18 * Without the below line, <sys/user.h> cannot be imported (it requires __BSD_VISIBLE) */
19 #undef _POSIX_C_SOURCE
20 /* clang-format off */
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23 /* clang-format on */
24 #include <sys/user.h>
25 #elif defined(__OpenBSD__)
26 #undef _POSIX_C_SOURCE
27 #include <kvm.h>
28 /* clang-format off */
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 /* clang-format on */
32 #include <unistd.h>
33 #endif
34
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <sys/resource.h>
40 #include <sys/time.h>
41 #include <sys/wait.h>
42 #include <unistd.h>
43
44 #include "api/s2n.h"
45 #include "s2n_test.h"
46 #include "testlib/s2n_testlib.h"
47
48 /* The number of connection pairs to allocate before measuring memory
49 * usage. The greater the value, the more accurate the end result. */
50 #define MAX_CONNECTIONS 1000
51
52 ssize_t get_vm_data_size()
53 {
54 long page_size = 0;
55 ssize_t size = 0, resident = 0, share = 0, text = 0, lib = 0, data = 0, dt = 0;
56
57 page_size = sysconf(_SC_PAGESIZE);
58 if (page_size < 0) {
59 return -1;
60 }
61
62 FILE *status_file = fopen("/proc/self/statm", "r");
63 if (fscanf(status_file, "%zd %zd %zd %zd %zd %zd %zd", &size, &resident, &share, &text, &lib, &data, &dt) < 7) {
64 fclose(status_file);
65 return -1;
66 }
67 fclose(status_file);
68
69 return data * page_size;
70 }
71
72 int main(int argc, char **argv)
73 {
74 size_t connectionsToUse = MAX_CONNECTIONS;
75
76 char *cert_chain = NULL;
77 char *private_key = NULL;
78
79 BEGIN_TEST();
80 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
81
82 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
83 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
84
85 /* Skip the test unless specifically enabled.
86 * This test is too unreliable to run in all customer environments.
87 * We should choose specific, known builds to run this test in.
88 */
89 const char *env_var = getenv("S2N_EXPECTED_CONNECTION_MEMORY_KB");
90 if (env_var == NULL) {
91 END_TEST();
92 }
93 const int expected_kbs_per_conn = atoi(env_var);
94 EXPECT_TRUE(expected_kbs_per_conn > 1);
95
96 struct rlimit file_limit;
97 EXPECT_SUCCESS(getrlimit(RLIMIT_NOFILE, &file_limit));
98 /* 4 fds per connection: {client,server} {write,read} fd
99 * and reserve 16 fds for libraries, stdin/stdout/stderr and so on */
100 if (4 * connectionsToUse + 16 > file_limit.rlim_cur) {
101 connectionsToUse = S2N_MAX(1, (file_limit.rlim_cur - 16) / 4);
102 }
103
104 struct s2n_connection **clients = calloc(connectionsToUse, sizeof(struct s2n_connection *));
105 struct s2n_connection **servers = calloc(connectionsToUse, sizeof(struct s2n_connection *));
106
107 EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
108 EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
109
110 struct s2n_config *client_config = NULL;
111 EXPECT_NOT_NULL(client_config = s2n_config_new());
112 EXPECT_SUCCESS(s2n_config_set_check_stapled_ocsp_response(client_config, 0));
113 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
114
115 struct s2n_cert_chain_and_key *chain_and_key = NULL;
116 struct s2n_config *server_config = NULL;
117 EXPECT_NOT_NULL(server_config = s2n_config_new());
118 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
119 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
120 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
121 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain, private_key));
122 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
123
124 ssize_t vm_data_initial = get_vm_data_size();
125 EXPECT_NOT_EQUAL(vm_data_initial, -1);
126
127 /* Allocate all connections */
128 for (size_t i = 0; i < connectionsToUse; i++) {
129 struct s2n_connection *client_conn = NULL;
130 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
131 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
132 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
133 clients[i] = client_conn;
134
135 struct s2n_connection *server_conn = NULL;
136 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
137 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
138 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
139 servers[i] = server_conn;
140 }
141
142 ssize_t vm_data_after_allocation = get_vm_data_size();
143 EXPECT_NOT_EQUAL(vm_data_after_allocation, -1);
144
145 for (size_t i = 0; i < connectionsToUse; i++) {
146 EXPECT_SUCCESS(s2n_connections_set_io_pair(clients[i], servers[i], &io_pair));
147
148 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(servers[i], clients[i]));
149 }
150
151 ssize_t vm_data_after_handshakes = get_vm_data_size();
152 EXPECT_NOT_EQUAL(vm_data_after_handshakes, -1);
153
154 for (int i = 0; i < connectionsToUse; i++) {
155 EXPECT_SUCCESS(s2n_connection_free_handshake(servers[i]));
156 EXPECT_SUCCESS(s2n_connection_free_handshake(clients[i]));
157 }
158 ssize_t vm_data_after_free_handshake = get_vm_data_size();
159 EXPECT_NOT_EQUAL(vm_data_after_free_handshake, -1);
160
161 for (int i = 0; i < connectionsToUse; i++) {
162 EXPECT_SUCCESS(s2n_connection_release_buffers(servers[i]));
163 EXPECT_SUCCESS(s2n_connection_release_buffers(clients[i]));
164 }
165 ssize_t vm_data_after_release_buffers = get_vm_data_size();
166 EXPECT_NOT_EQUAL(vm_data_after_release_buffers, -1);
167
168 for (int i = 0; i < connectionsToUse; i++) {
169 EXPECT_SUCCESS(s2n_connection_free(clients[i]));
170 EXPECT_SUCCESS(s2n_connection_free(servers[i]));
171 }
172
173 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
174 EXPECT_SUCCESS(s2n_config_free(server_config));
175 EXPECT_SUCCESS(s2n_config_free(client_config));
176
177 free(cert_chain);
178 free(private_key);
179 free(clients);
180 free(servers);
181
182 EXPECT_TRUE(vm_data_after_free_handshake <= vm_data_after_handshakes);
183 EXPECT_TRUE(vm_data_after_release_buffers <= vm_data_after_free_handshake);
184
185 ssize_t handshake_diff = (vm_data_after_handshakes - vm_data_initial);
186 ssize_t allocation_diff = (vm_data_after_allocation - vm_data_initial);
187 EXPECT_TRUE(allocation_diff <= handshake_diff);
188
189 ssize_t mem_per_conn = handshake_diff / (connectionsToUse * 2);
190 ssize_t kbs_per_conn = mem_per_conn / 1024;
191
192 if (kbs_per_conn != expected_kbs_per_conn) {
193 printf("\nExpected KB per connection: %i\n", expected_kbs_per_conn);
194 printf("\nActual KB per connection: %zi\n", kbs_per_conn);
195 printf("This is a %.2f%% change\n",
196 (kbs_per_conn - expected_kbs_per_conn) * 100.0 / expected_kbs_per_conn);
197
198 printf("\n");
199 printf("VmData initial: %10zd\n", vm_data_initial);
200 printf("VmData after allocations: %10zd\n", vm_data_after_allocation);
201 printf("VmData after handshakes: %10zd\n", vm_data_after_handshakes);
202 printf("VmData after free handshake: %10zd\n", vm_data_after_free_handshake);
203 printf("VmData after release: %10zd\n", vm_data_after_release_buffers);
204 printf("Number of connections used: %10zu\n", connectionsToUse);
205 FAIL_MSG("Unexpected memory usage. If expected, update MEM_PER_CONNECTION.");
206 }
207
208 END_TEST();
209 }