Skip to content
This repository was archived by the owner on Oct 22, 2026. It is now read-only.
This repository was archived by the owner on Oct 22, 2026. It is now read-only.

Mild race condition in secp256k1_context_preallocated_create #351

Open

Description

@Kixunil

Possible scenario:

  1. Assuming the context wasn't initialized yet
  2. Threads A and B loaded HAVE_PREALLOCATED_CONTEXT
  3. Thread A is suspended by the OS
  4. Thread B initializes the context and stores DONE
  5. Thread A is scheduled and swaps DONE for WORKING
  6. Thread C loads WORKING
  7. Thread A sees the mistake and stores DONE again
  8. Thread C can now continue

In this scenario C needlessly waited for A even though the context was initialized. I suggest to use compare exchange to avoid erroneous swapping in the first place.

There's also break missing so B always makes one more iteration of the loop and may even ironically become thread C.

Activity

  1. elichai commented on Jan 4, 2022

    @elichai
    Member

    Just want to note that this is only for the fuzzing configuration

  2. TheBlueMatt commented on Jan 4, 2022

    @TheBlueMatt
    Member

    Right, this just seems like "it could be more performant", not a specific bug, if I'm reading your description correctly, which isn't super relevant given its only used in fuzzing and presumably after initial startup things are "warm" and just stay DONE.

  3. Kixunil commented on Jan 4, 2022

    @Kixunil
    CollaboratorAuthor

    Ah, missed that it's only in fuzzing. Not looking immediately suspicious could have some benefit for reviewers too but if nobody wants to bother feel free to close.

  4. TheBlueMatt commented on Jan 4, 2022

    @TheBlueMatt
    Member

    We could add a comment, I guess? But, really, std should expose Once even if we don't have the ability to park a thread - the code here is basically the same as the std Once, with the only difference being that we don't park threads and instead busy-loop to avoid using std.

  5. Kixunil commented on Jan 4, 2022

    @Kixunil
    CollaboratorAuthor

    Once in std is actually quite involved. It uses an intrusive linked list to track waiting threads. I think it'd be pretty complicated to make usable without std.

  6. TheBlueMatt commented on Jan 5, 2022

    @TheBlueMatt
    Member

    Yes, that's my point, it does the same thing plus parks threads (with relevant tracking to wake them up again).

  7. Kixunil commented on Jan 5, 2022

    @Kixunil
    CollaboratorAuthor

    The code difference is so large that I'm not sure it'd be reasonable to abstract. And I generally love abstractions.

    Anyway I think just changing swap to compare_exchange is better than a comment. :)

  8. apoelstra commented on Jan 5, 2022

    @apoelstra
    Member

    I agree with changing swap to compare_exchange.

    Philosophically I agree with Matt that "it doesn't matter" and also that it's somehow std's fault ;) but there's a straightforward solution so let's just take it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions