Skip to content

Commit 0392a51

Browse files
committed
Enabling atexit for tests only
1 parent c6cace9 commit 0392a51

File tree

5 files changed

+26
-30
lines changed

5 files changed

+26
-30
lines changed

api/s2n.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ S2N_API extern unsigned long s2n_get_openssl_version(void);
229229
S2N_API extern int s2n_init(void);
230230

231231
/**
232-
* Cleans up any internal resources used by s2n-tls. This function should be called from each thread or process
233-
* that is created subsequent to calling `s2n_init` when that thread or process is done calling other s2n-tls functions.
232+
* Cleans up thread-local resources used by s2n-tls. Does not perform a full library cleanup. To fully
233+
* clean up the library use s2n_cleanup_final().
234234
*
235235
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
236236
*/
@@ -239,12 +239,6 @@ S2N_API extern int s2n_cleanup(void);
239239
/*
240240
* Performs a complete deinitialization and cleanup of the s2n-tls library.
241241
*
242-
* s2n_cleanup_final will always perform a complete cleanup. In contrast,
243-
* s2n_cleanup will only perform a complete cleanup if the atexit handler
244-
* is disabled and s2n_cleanup is called by the thread that called s2n_init.
245-
* Therefore s2n_cleanup_final should be used instead of s2n_cleanup in cases
246-
* where the user needs full control over when the complete cleanup executes.
247-
*
248242
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
249243
*/
250244
S2N_API extern int s2n_cleanup_final(void);
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
# Initialization and Teardown
2-
The s2n-tls library must be initialized with `s2n_init()` before calling most library functions. `s2n_init()` MUST NOT be called more than once, even when an application uses multiple threads or processes. s2n attempts to clean up its thread-local memory at thread-exit and all other memory at process-exit. However, this may not work if you are using a thread library other than pthreads or other threads using s2n outlive the thread that called `s2n_init()`. In that case you should call `s2n_cleanup_thread()` from every thread or process created after `s2n_init()`.
32

4-
> Note: `s2n_cleanup_thread()` is currently considered unstable, meaning the API is subject to change in a future release. To access this API, include `api/unstable/cleanup.h`.
3+
## Initialization
4+
The s2n-tls library must be initialized with `s2n_init()` before using the library functions. `s2n_init()` will error if it is called more than once per process.
55

66
Initialization can be modified by calling `s2n_crypto_disable_init()` or `s2n_disable_atexit()` before `s2n_init()`.
77

8-
An application can override s2n-tls’s internal memory management by calling `s2n_mem_set_callbacks` before calling s2n_init.
8+
An application can override s2n-tls’s internal memory management by calling `s2n_mem_set_callbacks` before calling `s2n_init()`.
99

1010
If you are trying to use FIPS mode, you must enable FIPS in your libcrypto library (probably by calling `FIPS_mode_set(1)`) before calling `s2n_init()`.
11+
12+
## Teardown
13+
### Thread-local Memory
14+
s2n has thread-local memory that it attempts to clean up automatically at thread-exit. This is done using pthread destructors and may not work if you are using a threads library other than pthreads. You can call `s2n_cleanup()` from every thread or process created after `s2n_init()` if you notice thread-local memory leaks.
15+
16+
### Library Cleanup
17+
A full cleanup and de-initialization of the library can be done by calling `s2n_cleanup_final()`.

tests/s2n_test.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515

1616
#pragma once
1717
#include <errno.h>
18+
#include <openssl/crypto.h>
1819
#include <stdio.h>
1920
#include <stdlib.h>
2021
#include <string.h>
2122
#include <unistd.h>
2223

23-
#include <openssl/crypto.h>
24-
2524
#include "error/s2n_errno.h"
26-
#include "utils/s2n_safety.h"
27-
#include "utils/s2n_result.h"
2825
#include "tls/s2n_alerts.h"
2926
#include "tls/s2n_tls13.h"
27+
#include "utils/s2n_init.h"
28+
#include "utils/s2n_result.h"
29+
#include "utils/s2n_safety.h"
3030

3131
int test_count;
3232

@@ -64,14 +64,15 @@ bool s2n_use_color_in_output = true;
6464
* number of independent childs at the start of a unit test and where you want
6565
* each child to have its own independently initialised s2n.
6666
*/
67-
#define BEGIN_TEST_NO_INIT() \
68-
do { \
69-
test_count = 0; \
70-
fprintf(stdout, "Running %-50s ... ", __FILE__); \
71-
fflush(stdout); \
72-
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_in_unit_test_set(true)); \
73-
S2N_TEST_OPTIONALLY_ENABLE_FIPS_MODE(); \
74-
} while(0)
67+
#define BEGIN_TEST_NO_INIT() \
68+
do { \
69+
test_count = 0; \
70+
fprintf(stdout, "Running %-50s ... ", __FILE__); \
71+
fflush(stdout); \
72+
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_in_unit_test_set(true)); \
73+
S2N_TEST_OPTIONALLY_ENABLE_FIPS_MODE(); \
74+
EXPECT_SUCCESS(s2n_enable_atexit()); \
75+
} while (0)
7576

7677
#define END_TEST_NO_INIT() \
7778
do { \

tests/unit/s2n_init_test.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ int main(int argc, char **argv)
105105
EXPECT_SUCCESS(s2n_cleanup_final());
106106
EXPECT_FALSE(s2n_is_initialized());
107107

108-
/* s2n_cleanup fully cleans up the library when the atexit handler is disabled.
109-
* Therefore, calling s2n_cleanup_final after s2n_cleanup will error */
110-
EXPECT_SUCCESS(s2n_init());
111-
EXPECT_SUCCESS(s2n_cleanup());
112-
EXPECT_FAILURE_WITH_ERRNO(s2n_cleanup_final(), S2N_ERR_NOT_INITIALIZED);
113-
114108
/* s2n_cleanup_thread only cleans up thread-local storage.
115109
* Therefore calling s2n_cleanup_final after s2n_cleanup_thread will succeed */
116110
EXPECT_SUCCESS(s2n_init());
@@ -127,7 +121,6 @@ int main(int argc, char **argv)
127121
pthread_t init_success_thread = { 0 };
128122
EXPECT_EQUAL(pthread_create(&init_success_thread, NULL, s2n_init_success_cb, NULL), 0);
129123
EXPECT_EQUAL(pthread_join(init_success_thread, NULL), 0);
130-
EXPECT_SUCCESS(s2n_cleanup_final());
131124

132125
END_TEST_NO_INIT();
133126
}

utils/s2n_init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
int s2n_init(void);
2121
int s2n_cleanup(void);
2222
bool s2n_is_initialized(void);
23+
int s2n_enable_atexit(void);

0 commit comments

Comments
 (0)