Skip to content

Commit

Permalink
Cleanup aws-lc thread locals and thread local key on library shutdown (
Browse files Browse the repository at this point in the history
…#152)

* Cleanup aws-lc thread locals and thread local key on library shutdown

---------

Co-authored-by: Bret Ambrose <[email protected]>
  • Loading branch information
bretambrose and Bret Ambrose authored Jun 30, 2023
1 parent f204595 commit de43d69
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/aws/cal/cal.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ AWS_EXTERN_C_BEGIN
AWS_CAL_API void aws_cal_library_init(struct aws_allocator *allocator);
AWS_CAL_API void aws_cal_library_clean_up(void);

/*
* Every CRT thread that might invoke aws-lc functionality should call this as part of the thread at_exit process
*/
AWS_CAL_API void aws_cal_thread_clean_up(void);

AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

Expand Down
7 changes: 7 additions & 0 deletions source/cal.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static struct aws_log_subject_info_list s_cal_log_subject_list = {
#ifndef BYO_CRYPTO
extern void aws_cal_platform_init(struct aws_allocator *allocator);
extern void aws_cal_platform_clean_up(void);
extern void aws_cal_platform_thread_clean_up(void);
#endif /* BYO_CRYPTO */

static bool s_cal_library_initialized = false;
Expand All @@ -96,3 +97,9 @@ void aws_cal_library_clean_up(void) {
aws_common_library_clean_up();
}
}

void aws_cal_thread_clean_up(void) {
#ifndef BYO_CRYPTO
aws_cal_platform_thread_clean_up();
#endif /* BYO_CRYPTO */
}
2 changes: 2 additions & 0 deletions source/darwin/commoncrypto_platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ void aws_cal_platform_init(struct aws_allocator *allocator) {
}

void aws_cal_platform_clean_up(void) {}

void aws_cal_platform_thread_clean_up(void) {}
14 changes: 14 additions & 0 deletions source/unix/openssl_platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include <aws/cal/private/opensslcrypto_common.h>

#include <openssl/crypto.h>

static struct openssl_hmac_ctx_table hmac_ctx_table;
static struct openssl_evp_md_ctx_table evp_md_ctx_table;

Expand Down Expand Up @@ -651,12 +653,24 @@ void aws_cal_platform_clean_up(void) {
}
#endif

#if defined(OPENSSL_IS_AWSLC)
AWSLC_thread_local_clear();
AWSLC_thread_local_shutdown();
#endif

if (s_libcrypto_module) {
dlclose(s_libcrypto_module);
}

s_libcrypto_allocator = NULL;
}

void aws_cal_platform_thread_clean_up(void) {
#if defined(OPENSSL_IS_AWSLC)
AWSLC_thread_local_clear();
#endif
}

#if !defined(__GNUC__) || (__GNUC__ >= 4 && __GNUC_MINOR__ > 1)
# pragma GCC diagnostic pop
#endif
2 changes: 2 additions & 0 deletions source/windows/bcrypt_platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ void aws_cal_platform_init(struct aws_allocator *allocator) {
}

void aws_cal_platform_clean_up(void) {}

void aws_cal_platform_thread_clean_up(void) {}

0 comments on commit de43d69

Please sign in to comment.