-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add a testcase for pthreads race conditions #12258
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
+100
−10
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <emscripten.h> | ||
#include <stdio.h> | ||
|
||
class random_device | ||
{ | ||
int __f_; | ||
public: | ||
// constructors | ||
explicit random_device(); | ||
~random_device(); | ||
|
||
// generating functions | ||
unsigned operator()(); | ||
}; | ||
|
||
random_device::random_device() | ||
{ | ||
__f_ = open("/dev/urandom", O_RDONLY); | ||
if (__f_ < 0) abort(); | ||
} | ||
|
||
random_device::~random_device() | ||
{ | ||
close(__f_); | ||
} | ||
|
||
unsigned | ||
random_device::operator()() | ||
{ | ||
unsigned r; | ||
size_t n = sizeof(r); | ||
char* p = reinterpret_cast<char*>(&r); | ||
while (n > 0) | ||
{ | ||
ssize_t s = read(__f_, p, 1); | ||
if (s == 0) abort(); | ||
if (s == -1) | ||
{ | ||
if (errno != EINTR) abort(); | ||
continue; | ||
} | ||
n -= static_cast<size_t>(s); | ||
p += static_cast<size_t>(s); | ||
} | ||
return r; | ||
} | ||
|
||
int main() { | ||
int total = 0; | ||
for (int i = 0; i < 1024; i++) { | ||
// printf causes proxying | ||
printf("%d %d\n", i, total); | ||
for (int j = 0; j < 1024; j++) { | ||
// allocation uses a mutex | ||
auto* rd = new random_device(); | ||
// reading data causes proxying | ||
for (int k = 0; k < 4; k++) { | ||
total += (*rd)(); | ||
} | ||
// make sure the optimizer doesn't remove the allocation | ||
EM_ASM({ out("iter") }, rd); | ||
delete rd; | ||
} | ||
} | ||
printf("done: %d", total); | ||
#ifdef REPORT_RESULT | ||
REPORT_RESULT(0); | ||
#endif | ||
return 0; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be doing directly with the pthread_mutex APIs rather than indirectly depending on the implementation of "/dev/urandom"?
Of you could write the test directly against pthread.h we could also see if it occurs in musl's native configuration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the use of
/dev/random
is not just for a mutex - it's also for proxying (all file I/O is proxied to the main thread). That involves more than just a mutex.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, I was hoping for something a little more precise .... there is so much going on here its hard to know what this is testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, definitely... yeah, this is not a great testcase. But it's the smallest I've managed so far that shows the issue, which is really hard to reproduce (as shown by it existing since forever, apparently).
If I have time I can try to reduce this more. But it may be better to focus on figuring out the actual cause of the problem, as that may suggest a testcase. We don't need to merge this urgently and may never merge it I guess.