Skip to content

Analysis of pthread_barriers #1652

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

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

Analysis of pthread_barriers #1652

wants to merge 32 commits into from

Conversation

michael-schwarz
Copy link
Member

@michael-schwarz michael-schwarz commented Dec 24, 2024

Makes use of the $$||_{\mathcal{A}}: \mathcal{A} \to \mathcal{A} \to \{ \textsf{false},\top \}$$ predicate we want to define for (abstract) digests to provide a sound basis for all of our MHP stuff for races and give a principled account of what I added in #518 .

Interestingly, it even does so outside of the context of data races --- which may be a cute insight on top of putting the race analysis on solid foundations with this predicate (and hopefully finding a way to get pthread_once to also fit (potentially in a reduced fashion)).

Technically, it accumulates the capacity and MHP information for all calls to wait for each barrier at a global unknown, and checks upon a call to wait that there are at least min(capacity)-1 other accesses where $$||_{\mathcal{A}}$$ is true pairwise among these, as well as with the caller to wait.

TODO:

  • Use TIDs and Joins
  • Use further abstract digests (such as must-lockset digest)
  • Use information also for excluding races (beyond unreachability)
  • Come up with a less expensive algorithm for checking there are min(capacity)-1 elements where $$||_{\mathcal{A}}$$ holds pairwise ?
  • Handle barriers configured for inter-process communication soundly by not doing anything for them

Closes #1651.

@michael-schwarz
Copy link
Member Author

michael-schwarz commented Dec 24, 2024

OS X CI does OS X things, I'll just ignore that for now.

Turns out OS X does not support barriers.

@michael-schwarz michael-schwarz marked this pull request as ready for review January 17, 2025 15:51
@michael-schwarz
Copy link
Member Author

Come up with a less expensive algorithm for checking there are min(capacity)-1 elements where $$||_{\mathcal{A}}$$ holds pairwise ?

Does anyone have an idea how this could be done? I'm not sure if one can do better complexity-wise, but it should be possible at least w.r.t. the constants.

@michael-schwarz
Copy link
Member Author

I talked it through with @DrMichaelPetter and we came up with some dynamic programming-like algorithm that hopefully performs better practically, but neither of us see how one can do better complexity wise.

@michael-schwarz
Copy link
Member Author

Apparently mathematicians call this problem the Admissible Subset Selection Problem. But looking around a bit, I have not found an algorithm that would help solve the problem.

@michael-schwarz
Copy link
Member Author

This now uses Seq to hopefully avoid constructing the full product every single time.

@sim642
Copy link
Member

sim642 commented Jun 4, 2025

By the way, @jprotopopov-ut was doing some Linux kernel stubbing using barriers and I mentioned this PR, but I don't know if this ended up being used/useful there.

@jprotopopov-ut
Copy link

By the way, @jprotopopov-ut was doing some Linux kernel stubbing using barriers and I mentioned this PR, but I don't know if this ended up being used/useful there.

It did not work properly, but I did not spend any time to investigate the reasons and instead ended up implementing ad-hoc barriers using a set of atomic flags

@michael-schwarz
Copy link
Member Author

What do you mean it wasn't working properly? Was it unsound (would be worth investigating) or just not precise enough for your case (that is expected from time to time)?

@jprotopopov-ut
Copy link

What do you mean it wasn't working properly? Was it unsound (would be worth investigating) or just not precise enough for your case (that is expected from time to time)?

As far as I remember, it was too imprecise, but I didn't notice any soundness issues. However, it was only a quick test, and I could have probably missed some things

Copy link
Member

@sim642 sim642 left a comment

Choose a reason for hiding this comment

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

This is only a brief look, I didn't yet look at the analysis or the content of the tests.

@michael-schwarz michael-schwarz requested a review from sim642 July 3, 2025 09:34
@michael-schwarz michael-schwarz added this to the v2.6.0 milestone Jul 3, 2025
Copy link
Member

@sim642 sim642 left a comment

Choose a reason for hiding this comment

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

The barrierception is really nice now!

pthread_mutex_t mutex;

void* f1(void* ptr) {
g = 2; //NORACE
Copy link
Member

Choose a reason for hiding this comment

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

This seems irrelevant here, there's no other access of g anyway. The test is about the checks.

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.

Analysis of pthread_barrier for race detection
3 participants