Skip to content

pthread_cond_wait may return spuriously #4271

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

Merged
merged 2 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions regression/cbmc-library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
if(NOT WIN32)
add_test_pl_tests(
"$<TARGET_FILE:cbmc>"
)
else()
add_test_pl_tests(
"$<TARGET_FILE:cbmc>"
"cbmc-library"
"$<TARGET_FILE:cbmc>"
"-C;-X;pthread"
"CORE"
)
endif()
11 changes: 9 additions & 2 deletions regression/cbmc-library/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
default: tests.log

include ../../src/config.inc
include ../../src/common

ifeq ($(BUILD_ENV_),MSVC)
no_pthread = -X pthread
endif

test:
@../test.pl -e -p -c ../../../src/cbmc/cbmc
@../test.pl -e -p -c ../../../src/cbmc/cbmc $(no_pthread)

tests.log: ../test.pl
@../test.pl -e -p -c ../../../src/cbmc/cbmc
@../test.pl -e -p -c ../../../src/cbmc/cbmc $(no_pthread)

show:
@for dir in *; do \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_barrier_init-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_barrier_wait-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_cancel-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_cond_init-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_cond_signal-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
31 changes: 27 additions & 4 deletions regression/cbmc-library/pthread_cond_wait-01/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
#include <assert.h>
#include <pthread_lib.h>
#include <pthread.h>

pthread_mutex_t lock;
pthread_cond_t cond;

// only accessed in critical section guarded by mutex, so there is no need to
// make this variable atomic or volatile
int x;

void *thread(void *arg)
{
(void)arg;
pthread_mutex_lock(&lock);
pthread_cond_wait(&cond, &lock);
// may fail: pthread_cond_wait can be waken up spuriously (see man
// pthread_cond_wait)
assert(x == 1);
pthread_mutex_unlock(&lock);
return NULL;
}

int main()
{
pthread_cond_wait();
assert(0);
return 0;
pthread_t t;
pthread_mutex_init(&lock, 0);
pthread_cond_init(&cond, 0);
pthread_create(&t, NULL, thread, NULL);
x = 1;
pthread_cond_broadcast(&cond);
pthread_join(t, NULL);
}
9 changes: 5 additions & 4 deletions regression/cbmc-library/pthread_cond_wait-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
KNOWNBUG
CORE pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
--bounds-check
^EXIT=10$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
^\*\* 1 of 2 failed
^VERIFICATION FAILED$
--
^warning: ignoring
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_create-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_exit-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_join-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_mutex_destroy-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_mutex_init-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_mutex_lock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_mutex_trylock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_mutex_unlock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_rwlock_init-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_rwlock_rdlock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_rwlock_unlock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_rwlock_wrlock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_spin_lock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_spin_trylock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-library/pthread_spin_unlock-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
KNOWNBUG pthread
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
4 changes: 2 additions & 2 deletions src/ansi-c/library/pthread_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ inline int pthread_cond_wait(
#endif

__CPROVER_atomic_begin();
__CPROVER_assume(*((unsigned *)cond));
(*((unsigned *)cond))--;
if(*((unsigned *)cond))
(*((unsigned *)cond))--;
__CPROVER_atomic_end();

return 0; // we never fail
Expand Down