Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow s2n_init() to be called multiple times #1999

Closed
wants to merge 2 commits into from

Conversation

raycoll
Copy link
Contributor

@raycoll raycoll commented Jun 9, 2020

Subsequent calls to s2n_init() without a call to s2n_cleanup() will now
return success and reuse the previously initialized library state.

This change is to accommodate use cases where multiple libraries/threads
within a process have a dependency on s2n and attempt to call s2n_init()
indepdently. For example, consider a case where a webserver library
tries to call s2n_init() every time a thread is spawned. For these use
cases I think it is simplest to allow all subsequent calls to s2n_init()
to succeed.

Alternatively, we could try to keep some global state in the application
to guard all code paths that touch s2n_init(), but this can be a large
effort that requires coordination between all libraries in a process.

Testing:

Called s2n_init() twice in a unit test.

@raycoll
Copy link
Contributor Author

raycoll commented Jun 9, 2020

I think this is fairly noncontroversial behavior, but let me know if there is something I'm missing!

@@ -27,6 +27,8 @@

#include "openssl/opensslv.h"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be volatile?
Alternatively (and safer) maybe pthread_once() may be even better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My actual use case is not concerned with concurrent calls to s2n_init(). I have one library's code path calling s2n_init() at process start and another lazily calling s2n_init() some time after start.

But I will look into pthread_once() to see if I can cover easily cover the concurrent use case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for pthread_once

@JonathanHenson
Copy link
Contributor

Also maybe a test where you create a race between multiple threads calling init and cleanup?

@codecov-commenter
Copy link

Codecov Report

Merging #1999 into master will decrease coverage by 8.60%.
The diff coverage is 83.33%.

@@            Coverage Diff             @@
##           master    #1999      +/-   ##
==========================================
- Coverage   89.26%   80.65%   -8.61%     
==========================================
  Files         150      232      +82     
  Lines       11751    17404    +5653     
==========================================
+ Hits        10490    14038    +3548     
- Misses       1261     3366    +2105     

Impacted file tree graph

@raycoll
Copy link
Contributor Author

raycoll commented Jun 15, 2020

This is falling down my list of priorities but I think it is important and will get back to it.

@lrstewart lrstewart changed the base branch from master to main August 7, 2020 17:34
@lrstewart lrstewart changed the base branch from main to master August 7, 2020 17:36
@lrstewart lrstewart changed the base branch from master to main August 7, 2020 17:44
@codecov-io
Copy link

codecov-io commented Dec 9, 2020

Codecov Report

Merging #1999 (58fb2b5) into main (3f47080) will decrease coverage by 1.74%.
The diff coverage is 88.88%.

@@            Coverage Diff             @@
##             main    #1999      +/-   ##
==========================================
- Coverage   81.98%   80.24%   -1.75%     
==========================================
  Files         271      271              
  Lines       18730    18679      -51     
==========================================
- Hits        15356    14989     -367     
- Misses       3374     3690     +316     

Impacted file tree graph

@raycoll
Copy link
Contributor Author

raycoll commented Dec 9, 2020

took a shot at using pthread_once for this. I rearranged functions that are per process and functions that are per thread. My test is failing because after the latest commit s2n_init is no longer able to be called multiple times from the same thread. I think instead we should just have a test that creates multiple threads that call s2n_init() once, like a real application would.

@@ -61,6 +85,9 @@ int s2n_cleanup(void)
/* s2n_cleanup is supposed to be called from each thread before exiting,
* so ensure that whatever clean ups we have here are thread safe */
GUARD_AS_POSIX(s2n_rand_cleanup_thread());
/* This isn't strictly necessary as it isn't currently possible to call s2n_init() again after a call to
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to purge this comment.

raycoll added 2 commits April 21, 2021 07:37
Subsequent calls to s2n_init() without a call to s2n_cleanup() will
return success and reuse the previously initialized library state.

This change is to accommodate use cases where multiple libraries/threads
within a process have a dependency on s2n and attempt to call s2n_init()
indepdently. For example, consider a case where a webserver library
tries to call s2n_init() every time a thread is spawned. For these use
cases I think it is simplest to allow all subsequent calls to s2n_init()
to succeed.

Alternatively, we could try to keep some global state in the application
to guard all code paths that touch s2n_init(), but this can be a large
effort that requires coordination between all libraries in a process.
The error handling feedback from pthread_once is wonky. We're relying
on s2n_errno being set properly after the function is executed.
@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants