-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MinGW: mitigate potential abort on rwlocks using PTHREAD_RWLOCK_INITI…
…ALIZER (#1530) ### Description of changes: * On some platforms/pthreads-implementations, there are [race conditions](https://github.com/ldx/winpthreads/blob/fecf6566606c9111628441ed2426e7efdc5faf0e/src/rwlock.c#L161) on the first use of rwlocks initialized with `PTHREAD_RWLOCK_INITIALIZER`. * This change allows for a re-attempt on lock acquisition when it initially fails on MinGW, The current logic aborts the process when this occurs. * A test is added to attempt to reproduce the race-condition, although success of it can be a false-negative for such a bug. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license. --------- Co-authored-by: Justin Smith <[email protected]>
- Loading branch information
1 parent
3a0b28d
commit b8aff82
Showing
5 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,43 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR ISC | ||
|
||
// This test attempts to setup a potential race condition that can cause the | ||
// first use of an rwlock to fail when initialized using | ||
// PTHREAD_RWLOCK_INITIALIZER. | ||
// See: https://sourceforge.net/p/mingw-w64/bugs/883/ | ||
|
||
#include <openssl/rand.h> | ||
|
||
#include <cstdint> | ||
#include <iostream> | ||
#include <thread> | ||
#include <cassert> | ||
#include <cstdlib> | ||
|
||
static void thread_task_rand(bool *myFlag) { | ||
uint8_t buf[16]; | ||
if(1 == RAND_bytes(buf, sizeof(buf))) { | ||
*myFlag = true; | ||
} | ||
} | ||
|
||
int main(int _argc, char** _argv) { | ||
constexpr size_t kNumThreads = 16; | ||
bool myFlags[kNumThreads] = {}; | ||
std::thread myThreads[kNumThreads]; | ||
|
||
for (size_t i = 0; i < kNumThreads; i++) { | ||
bool* myFlag = &myFlags[i]; | ||
myThreads[i] = std::thread(thread_task_rand, myFlag); | ||
} | ||
for (size_t i = 0; i < kNumThreads; i++) { | ||
myThreads[i].join(); | ||
if(!myFlags[i]) { | ||
std::cerr << "Thread " << i << " failed." << std::endl; | ||
exit(1); | ||
return 1; | ||
} | ||
} | ||
std::cout << "PASS" << std::endl; | ||
return 0; | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -114,5 +114,10 @@ | |
"cmd": [ | ||
"crypto/dynamic_loading_test" | ||
] | ||
}, | ||
{ | ||
"cmd": [ | ||
"crypto/rwlock_static_init" | ||
] | ||
} | ||
] |