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

Data Race fix when using Seed callback with multiple threads #8300

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

night1rider
Copy link
Contributor

@night1rider night1rider commented Dec 18, 2024

Description

While working on an example for threading using wolfssl, I found that when using seedCb a data race occurs.

Since setting the seedCb is just setting a function pointer to a global this means a simple mutex is not enough.
For example:

You have Thread A and Thread B. Both Threads have different ways they want to have a seed(for whatever reason) SeedA and SeedB

Right now if Thread A sets seedA callback first and Thread B sets seedB callback after. Thread A has the potential to be using seedB for its RNG if ThreadA has not completed all of its RNG calls before ThreadB sets SeedB. This then leads to a sort of data race where that race is getting to the thread's desired function before it is overwritten.

This PR attempts to alleviate this issue by allowing an override macro WC_MULTI_THREADED_CALLBACKS, currently as of posting this fix is only setup for posix threading.

Sources on functions for Posix Threads
https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_setspecific.html

There is potential to make this more portable but we would need to implement a ThreadId key pair and then all the cleanup that is associated with that sort of mechanism....

@night1rider night1rider self-assigned this Dec 18, 2024
@night1rider night1rider changed the title Data Race fix when using callbacks with multiple threads Data Race fix when using Seed callback with multiple threads Dec 18, 2024
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.

1 participant