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

Call Keccak_(X4_)Dispatch with pthread_once #1549

Merged
merged 2 commits into from
Nov 1, 2023

Conversation

zxjtan
Copy link
Contributor

@zxjtan zxjtan commented Sep 11, 2023

Fixes #1548.

Call Keccak_(X4_)Dispatch using pthread_once to ensure setting of global function pointers is atomic and done only once.

  • Does this PR change the input/output behaviour of a cryptographic algorithm (i.e., does it change known answer test values)? NO
  • Does this PR change the list of algorithms available -- either adding, removing, or renaming? Does this PR otherwise change an API? NO

@dstebila
Copy link
Member

@zxjtan will you be continuing to work on this?

@zxjtan
Copy link
Contributor Author

zxjtan commented Oct 1, 2023

@dstebila I have no experience writing Win32 code, but I can try my hand at making it cross-platform if there are reviewers who can check that I am writing it idiomatically.

@baentsch
Copy link
Member

baentsch commented Oct 4, 2023

@zxjtan OQS already has code parts that are not optimized for Win32. In other words, it'd be beneficial to see liboqs improve "just" for pthread environments (as is the goal with this PR) rather than wait for a good Win32 solution to the problem. If a Windows user runs into #1548 we can revisit. What should happen first/now, though, is that this PR passes CI. Do you have time to get this resolved?

@zxjtan
Copy link
Contributor Author

zxjtan commented Oct 4, 2023

@baentsch Sure, I can gate the pthread functionality and pass the CI quite soon.

@zxjtan zxjtan force-pushed the main branch 3 times, most recently from 45b6cad to c3da910 Compare October 4, 2023 20:33
@zxjtan
Copy link
Contributor Author

zxjtan commented Oct 4, 2023

CI still runs into undefined reference to symbol 'call_once@@GLIBC_2.28', any tips on how to proceed?

@baentsch
Copy link
Member

baentsch commented Oct 5, 2023

CI still runs into undefined reference to symbol 'call_once@@GLIBC_2.28', any tips on how to proceed?

Well, that depends on which lib is supposed to hold this symbol: It seems, glibc2.28 doesn't/isn't C11 compliant. What about using pthreads (i.e., pthread_once instead? Then linking in pthreads lib should get this to work.

@zxjtan
Copy link
Contributor Author

zxjtan commented Oct 5, 2023

CI still runs into undefined reference to symbol 'call_once@@GLIBC_2.28', any tips on how to proceed?

Well, that depends on which lib is supposed to hold this symbol: It seems, glibc2.28 doesn't/isn't C11 compliant. What about using pthreads (i.e., pthread_once instead? Then linking in pthreads lib should get this to work.

No dice, build still fails with undefined reference to symbol 'pthread_once@@GLIBC_2.2.5'. However, in both the pthread_once and call_once cases, this is caused by error adding symbols: DSO missing from command line. Do I have to change something in the CMake files to make it link?

@baentsch
Copy link
Member

baentsch commented Oct 6, 2023

Do I have to change something in the CMake files to make it link?

I'd say so: That's what I meant with

Then linking in pthreads lib should get this to work.

Sorry for being imprecise.

So, I'd think one option may be to lift the "pthreads-availability" test logic you correctly built in to the source code to cmakevariable level (such as to then add the lib there -- or not, if unavailable).

.CMake/compiler_opts.cmake Outdated Show resolved Hide resolved
.CMake/compiler_opts.cmake Outdated Show resolved Hide resolved
@zxjtan zxjtan force-pushed the main branch 5 times, most recently from 9fdb552 to b32a437 Compare October 8, 2023 23:33
@zxjtan zxjtan mentioned this pull request Oct 11, 2023
2 tasks
@zxjtan zxjtan force-pushed the main branch 2 times, most recently from 4dc6f09 to d7f7988 Compare October 15, 2023 19:10
@zxjtan zxjtan marked this pull request as ready for review October 18, 2023 19:27
@zxjtan zxjtan requested a review from dstebila as a code owner October 18, 2023 19:27
@zxjtan zxjtan requested a review from baentsch October 26, 2023 15:01
Copy link
Member

@baentsch baentsch left a comment

Choose a reason for hiding this comment

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

Code LGTM. The only slight concern I have is that this seems to add pthreads as a requirement to liboqs (and hence, its users). Have you tested that this new code works OK if used by a downstream project, @zxjtan ?

Thinking that through, what about adding CI to liboqs automatically validating this property with downstream projects we consider essential, @dstebila ? Or would this create a hen-and-egg situation where downstream projects' status (of not being ready for an liboqs update) can break liboqs CI and hence stop the whole procedure? Let me create a separate discussion for this...

@zxjtan
Copy link
Contributor Author

zxjtan commented Oct 27, 2023

The only slight concern I have is that this seems to add pthreads as a requirement to liboqs

I don't think it does. I think of it not as a requirement but an environment fix: if you are running on a platform with threading, and if that threading model is pthreads, this PR provides a fix. Otherwise the code will not #include <pthread.h> because you're on something else like Win32 and you can submit a fix yourself, or you have no threading at all and there is nothing to fix.

Have you tested that this new code works OK if used by a downstream project, @zxjtan ?

Yes, I have used this code in a project and it fixes the race condition.

Copy link
Member

@baentsch baentsch left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the contribution! @dstebila This is one more item to be added to the "Windows ToDo" list (as and when we start it per https://github.com/orgs/open-quantum-safe/discussions/1583.

Copy link
Member

@Martyrshot Martyrshot left a comment

Choose a reason for hiding this comment

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

This looks good to me too! Taking a brief look at the Win32 API the fix will likely look very similar, but it seems the biggest obstacle will be finding someone with a Win32 machine to implement the fix :-)

@baentsch baentsch merged commit 1bb9887 into open-quantum-safe:main Nov 1, 2023
31 checks passed
@baentsch
Copy link
Member

baentsch commented Nov 1, 2023

@zxjtan Thanks again for the contribution; it turned out it causes a failure in one downstream integration: https://app.circleci.com/pipelines/github/open-quantum-safe/oqs-demos/1046/workflows/e474db0c-43e2-4628-b634-23b9f3b5d95a/jobs/5979. Does this ring a bell? At first glance this should work as -lpthread is clearly configured in.... Edit/correct: -lpthread is requested to be configured in but the final openssl link command doesn't contain that instruction... Hmm.....

@zxjtan
Copy link
Contributor Author

zxjtan commented Nov 1, 2023

@zxjtan Thanks again for the contribution; it turned out it causes a failure in one downstream integration: https://app.circleci.com/pipelines/github/open-quantum-safe/oqs-demos/1046/workflows/e474db0c-43e2-4628-b634-23b9f3b5d95a/jobs/5979. Does this ring a bell? At first glance this should work as -lpthread is clearly configured in.... Edit/correct: -lpthread is requested to be configured in but the final openssl link command doesn't contain that instruction... Hmm.....

The sed in this line might have something to do with it?

@baentsch
Copy link
Member

baentsch commented Nov 2, 2023

The sed in this line might have something to do with it?

I don't think so: This doesn't change (presence of) -lpthread but just adds liboqs. HOWEVER, looking further into the build log, this line is key, I'd gather:

./config --prefix=/opt/oqs-openssl-quic/.openssl no-shared no-threads

This configures the OpenSSL installation within quic-nginx to be built without pthreads. Then, in turn, the open symbols in liboqs cannot resolve as liboqs is (needed to be) linked into openssl111.

I see two ways out of this:

  1. Upgrade the nginx-quic demo to use OpenSSL3 (as per replace oqs-openssl111 oqs-demos#182)
  2. Disable pthreads usage in the liboqs build for this demo

For 1) I am out of my depth (having to ask the original contributor @igorbarshteyn for help/advice); 2) would require a cmake variable in liboqs to disable the use of pthreads.

Any other options? We could of course also completely discontinue support of the QUIC demo as it is based on unsupported software (OpenSSL111). The more I think about this, the more I like it. Several people have voiced willingness to help on open-quantum-safe/oqs-demos#182 but nothing has been forthcoming. And OpenSSL111 by now is EOL. Thus I think we should sunset this PQ-OpenSSL1-QUIC integration.

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.

Keccak_(X4_)Dispatch Initialize_ptr assignment is thread unsafe
4 participants