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

20241108-WOLFSSL_CLEANUP_THREADSAFE #8169

Merged
merged 3 commits into from
Nov 13, 2024

Conversation

douzzer
Copy link
Contributor

@douzzer douzzer commented Nov 8, 2024

src/ssl.c: implement WOLFSSL_CLEANUP_THREADSAFE in wolfSSL_Init() / wolfSSL_Cleanup().

ZD#18836

tested with ./configure --enable-all CFLAGS='-DWOLFSSL_TEST_NO_MUTEX_INITIALIZER -DWOLFSSL_CLEANUP_THREADSAFE' and make check, plus pro forma wolfssl-multi-test.sh ... check-source-text

src/ssl.c Outdated
/* WOLFSSL_CLEANUP_THREADSAFE depends on a wolfSSL_Atomic_Int that can
* be statically initialized.
*/
static wolfSSL_Atomic_Int inits_count_mutex_valid2 = 0;
Copy link
Member

Choose a reason for hiding this comment

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

wolfSSL_Atomic_Int_Init is not being called anywhere (and that would also defeat the purpose of this PR). While this works for *nix I would consider how we handle this carefully. Maybe a new WOLFSSL_ATOMIC_INITIALIZER is needed.

Copy link
Member

Choose a reason for hiding this comment

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

Also please use a more descriptive name. I know the context and I had a hard time to figure out what is going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

WOLFSSL_ATOMIC_INITIALIZER is a great idea -- then this whole solution can be gated on defined(WOLFSSL_ATOMIC_INITIALIZER).

will do.

Copy link
Member

Choose a reason for hiding this comment

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

Still not descriptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed -- inits_count_mutex_atomic_initing_flag.

src/ssl.c Outdated
Comment on lines 5681 to 5685
while ((current_inits_count_mutex_valid = inits_count_mutex_valid) == 0);
if (current_inits_count_mutex_valid < 0) {
(void)wolfSSL_Atomic_Int_FetchSub(&inits_count_mutex_valid2, 1);
return BAD_MUTEX_E;
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced a spin lock is a good idea to hide in the init function. Since this should be a very rare occurrence, I think this should be treated the same as failing to lock a mutex and returning BAD_MUTEX_E.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that was indeed the other solution I considered. I agree that might be better, but we can't use BAD_MUTEX_E for the error, because the caller needs to be told that the call is retryable. (we already return BAD_MUTEX_E if wc_InitMutex() fails, and that's not retryable.)

with your nudge, I'll switch it to the other solution, and pick or add an appropriate error code that makes it clear by its name that the call should be retried by the application. effectively the spin locking will move to be the application's responsibility, which is better.

@julek-wolfssl julek-wolfssl removed their assignment Nov 12, 2024
* add WOLFSSL_ATOMIC_INITIALIZER() to wc_port.h;
* rename feature macro to WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS for clarity;
* remove spin lock logic in wolfSSL_Init() and instead return DEADLOCK_AVERTED_E on contended initialization;
* unless WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS is user-defined to 0, automatically enable it when appropriate.
@douzzer douzzer force-pushed the 20241108-WOLFSSL_CLEANUP_THREADSAFE branch 6 times, most recently from a0706d5 to 28430c5 Compare November 13, 2024 08:34
@dgarske dgarske self-requested a review November 13, 2024 17:58
dgarske
dgarske previously approved these changes Nov 13, 2024
Comment on lines +5648 to +5660
#ifndef WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
#if !defined(WOLFSSL_MUTEX_INITIALIZER) && !defined(SINGLE_THREADED) && \
defined(WOLFSSL_ATOMIC_OPS) && defined(WOLFSSL_ATOMIC_INITIALIZER)
#define WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS 1
#else
#define WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS 0
#endif
#elif defined(WOLFSSL_MUTEX_INITIALIZER) || defined(SINGLE_THREADED)
#undef WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
#define WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS 0
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Why undef & define and just define in the other?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to force the feature off we define WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS to 0. the user can also force it off by defining to 0.

the top half is followed if WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS has no definition either way, in which case the default setup is used, following the config settings.

the bottom half is followed if the user config activated WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS, but it needs to be forced off because it's not compatible with other config settings.

@dgarske dgarske merged commit c8f56f0 into wolfSSL:master Nov 13, 2024
143 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants