Debian Codegraph @

crt/s2n/tests/unit/s2n_mem_allocator_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 #include <stdint.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22
23 #define SUPPORTED_CERTIFICATE_FORMATS (2)
24
25 static const char *certificate_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_RSA_2048_PKCS8_CERT_CHAIN };
26 static const char *private_key_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_KEY, S2N_RSA_2048_PKCS8_KEY };
27
28 #define HISTOGRAM_SIZE 100
29 static uint32_t histogram_values[HISTOGRAM_SIZE] = { 0 };
30 static uint32_t histogram_counts[HISTOGRAM_SIZE] = { 0 };
31
32 static int custom_mem_init(void)
33 {
34 return 0;
35 }
36
37 static int custom_mem_cleanup(void)
38 {
39 return 0;
40 }
41
42 static int custom_mem_malloc(void **ptr, uint32_t requested, uint32_t *allocated)
43 {
44 int i = 0;
45 for (i = 0; i < HISTOGRAM_SIZE; i++) {
46 if (histogram_values[i] == 0) {
47 histogram_values[i] = requested;
48 }
49
50 if (histogram_values[i] == requested) {
51 break;
52 }
53 }
54
55 if (i < HISTOGRAM_SIZE) {
56 histogram_counts[i] += 1;
57 }
58
59 *ptr = malloc(requested);
60 *allocated = requested;
61
62 /* Fill the memory with non-zeroes to check that s2n handles that fine */
63 memset(*ptr, 'a', requested);
64
65 return 0;
66 }
67
68 static int custom_mem_free(void *ptr, uint32_t size)
69 {
70 free(ptr);
71 return 0;
72 }
73
74 static uint64_t s2n_test_mock_time = 0;
75
76 static int s2n_test_mock_clock(void *in, uint64_t *out)
77 {
78 *out = s2n_test_mock_time;
79 return 0;
80 }
81
82 int main(int argc, char **argv)
83 {
84 s2n_blocked_status blocked;
85 char *cert_chain_pem = NULL;
86 char *private_key_pem = NULL;
87 char *dhparams_pem = NULL;
88
89 /* We have to set the callback before BEGIN_TEST, because s2n_init() is called
90 * there.
91 */
92 int rc = s2n_mem_set_callbacks(custom_mem_init, custom_mem_cleanup, custom_mem_malloc, custom_mem_free);
93
94 BEGIN_TEST();
95 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
96
97 /* Can't add callbacks if s2n is initialized */
98 EXPECT_FAILURE(s2n_mem_set_callbacks(custom_mem_init, custom_mem_cleanup, custom_mem_malloc, custom_mem_free));
99
100 EXPECT_SUCCESS(rc);
101
102 for (size_t is_dh_key_exchange = 0; is_dh_key_exchange <= 1; is_dh_key_exchange++) {
103 struct s2n_cert_chain_and_key *chain_and_keys[SUPPORTED_CERTIFICATE_FORMATS];
104
105 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
106 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
107 EXPECT_NOT_NULL(dhparams_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
108
109 /* Set up server config */
110 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
111 EXPECT_NOT_NULL(server_config);
112 EXPECT_OK(s2n_config_set_tls12_security_policy(server_config));
113 EXPECT_SUCCESS(s2n_config_set_monotonic_clock(server_config, s2n_test_mock_clock, NULL));
114
115 for (size_t cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) {
116 EXPECT_SUCCESS(s2n_read_test_pem(certificate_paths[cert], cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
117 EXPECT_SUCCESS(s2n_read_test_pem(private_key_paths[cert], private_key_pem, S2N_MAX_TEST_PEM_SIZE));
118 EXPECT_NOT_NULL(chain_and_keys[cert] = s2n_cert_chain_and_key_new());
119 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_keys[cert], cert_chain_pem, private_key_pem));
120 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_keys[cert]));
121 }
122
123 if (is_dh_key_exchange) {
124 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
125 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
126 }
127
128 /* Set up client config */
129 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
130 EXPECT_NOT_NULL(client_config);
131 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
132 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
133 EXPECT_SUCCESS(s2n_config_set_monotonic_clock(client_config, s2n_test_mock_clock, NULL));
134
135 /* Set up server connection */
136 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
137 EXPECT_NOT_NULL(server_conn);
138 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
139
140 /* Set up client connection */
141 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
142 EXPECT_NOT_NULL(client_conn);
143 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
144
145 /* Use in-memory IO stuffer pair */
146 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
147 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
148 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client_conn, server_conn, &io_pair));
149
150 /* Negotiate the handshake */
151 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
152 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
153 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
154
155 /* Release handshake buffers on client */
156 EXPECT_SUCCESS(s2n_connection_free_handshake(client_conn));
157
158 /* Client sends data in increasing chunks, server reads and verifies */
159 char send_buffer[0xffff];
160 char recv_buffer[0xffff];
161
162 uint16_t timeout = 1;
163 EXPECT_SUCCESS(s2n_connection_set_dynamic_record_threshold(client_conn, 0x7fff, timeout));
164
165 int i = 0;
166 for (i = 1; i < 0xffff - 100; i += 100) {
167 for (int j = 0; j < i; j++) {
168 send_buffer[j] = 33;
169 }
170 EXPECT_EQUAL(s2n_send(client_conn, send_buffer, i, &blocked), i);
171
172 /* Server reads the data */
173 char *ptr = recv_buffer;
174 int size = i;
175 do {
176 int bytes_read = 0;
177 EXPECT_SUCCESS(bytes_read = s2n_recv(server_conn, ptr, size, &blocked));
178 size -= bytes_read;
179 ptr += bytes_read;
180 } while (size);
181
182 for (int j = 0; j < i; j++) {
183 EXPECT_EQUAL(recv_buffer[j], 33);
184 }
185
186 /* release the buffers here to validate we can continue IO after */
187 EXPECT_SUCCESS(s2n_connection_release_buffers(server_conn));
188
189 /* Reset the IO stuffer cursors after all data is consumed */
190 EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), 0);
191 EXPECT_SUCCESS(s2n_stuffer_rewrite(&io_pair.server_in));
192 }
193
194 /* Fill the buffer for the final send */
195 for (int j = 0; j < i; j++) {
196 send_buffer[j] = 33;
197 }
198
199 /* Release buffers on client to validate we can continue IO after */
200 EXPECT_SUCCESS(s2n_connection_release_buffers(client_conn));
201
202 /* Simulate timeout for dynamic record threshold by advancing mock clock.
203 * After the timeout period, active_application_bytes_consumed is reset
204 * to 0 before writing data, so its value should equal bytes written
205 * after the send.
206 */
207 s2n_test_mock_time += (uint64_t) timeout * 1000000000 + 1;
208
209 ssize_t bytes_written = s2n_send(client_conn, send_buffer, i, &blocked);
210 EXPECT_TRUE(bytes_written > 0);
211 EXPECT_EQUAL((uint64_t) bytes_written, client_conn->active_application_bytes_consumed);
212
213 /* Server reads the final chunk */
214 {
215 char *ptr = recv_buffer;
216 int size = i;
217 do {
218 int bytes_read = 0;
219 EXPECT_SUCCESS(bytes_read = s2n_recv(server_conn, ptr, size, &blocked));
220 size -= bytes_read;
221 ptr += bytes_read;
222 } while (size);
223 }
224
225 /* Graceful shutdown */
226 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
227
228 /* Clean up cert chains */
229 for (size_t cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) {
230 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_keys[cert]));
231 }
232
233 free(cert_chain_pem);
234 free(private_key_pem);
235 free(dhparams_pem);
236 }
237
238 #if defined(S2N_TEST_DEBUG)
239 /* Sort our histogram */
240 uint32_t spare_value, spare_count;
241 for (int i = 0; i < HISTOGRAM_SIZE; i++) {
242 if (histogram_counts[i] == 0) {
243 break;
244 }
245
246 for (int j = i + 1; j < HISTOGRAM_SIZE; j++) {
247 if (histogram_counts[j] == 0) {
248 break;
249 }
250
251 if (histogram_values[j] < histogram_values[i]) {
252 spare_value = histogram_values[i];
253 spare_count = histogram_counts[i];
254
255 histogram_values[i] = histogram_values[j];
256 histogram_counts[i] = histogram_counts[j];
257
258 histogram_values[j] = spare_value;
259 histogram_counts[j] = spare_count;
260 }
261 }
262 }
263
264 /* Print the histogram values */
265 TEST_DEBUG_PRINT("\n\n");
266 for (int i = 0; i < HISTOGRAM_SIZE; i++) {
267 if (histogram_values[i] == 0) {
268 break;
269 }
270 TEST_DEBUG_PRINT("Allocated %d bytes %d times\n", histogram_values[i], histogram_counts[i]);
271 }
272 TEST_DEBUG_PRINT("\n");
273 #endif
274
275 END_TEST();
276
277 return 0;
278 }