From: Martin Quinson Date: Sun, 16 Oct 2022 10:57:20 +0000 (+0200) Subject: Add a sthread+MC example that deadlocks X-Git-Tag: v3.34~773 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/fe2e372be5c524c254886aca779e030285085125 Add a sthread+MC example that deadlocks + fix make distcheck --- diff --git a/MANIFEST.in b/MANIFEST.in index ebaf62ecd4..af4314f0bb 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -714,8 +714,11 @@ include examples/smpi/trace_call_location/trace_call_location.c include examples/smpi/trace_call_location/trace_call_location.tesh include examples/smpi/trace_simple/trace_simple.c include examples/smpi/trace_simple/trace_simple.tesh +include examples/sthread/pthread-mc-mutex-simple.tesh +include examples/sthread/pthread-mc-mutex-simpledeadlock.tesh include examples/sthread/pthread-mutex-simple.c include examples/sthread/pthread-mutex-simple.tesh +include examples/sthread/pthread-mutex-simpledeadlock.c include examples/sthread/sthread-mutex-simple.c include examples/sthread/sthread-mutex-simple.tesh include src/include/catch_simgrid.hpp @@ -2358,6 +2361,8 @@ include src/mc/sosp/Snapshot.hpp include src/mc/sosp/Snapshot_test.cpp include src/mc/transition/Transition.cpp include src/mc/transition/Transition.hpp +include src/mc/transition/TransitionActorJoin.cpp +include src/mc/transition/TransitionActorJoin.hpp include src/mc/transition/TransitionAny.cpp include src/mc/transition/TransitionAny.hpp include src/mc/transition/TransitionComm.cpp diff --git a/examples/sthread/CMakeLists.txt b/examples/sthread/CMakeLists.txt index fd1c3c5792..a59d61f650 100644 --- a/examples/sthread/CMakeLists.txt +++ b/examples/sthread/CMakeLists.txt @@ -28,6 +28,28 @@ foreach(x endforeach() +# Regular pthread examples that may deadlock: test sthread + MC in that case +############################################################################ + +foreach(x + mutex-simpledeadlock) + + if(SIMGRID_HAVE_MC AND ("${CMAKE_SYSTEM}" MATCHES "Linux")) # sthread is linux-only + + add_executable (pthread-${x} EXCLUDE_FROM_ALL pthread-${x}.c) + set_target_properties(pthread-${x} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries(pthread-${x} PRIVATE Threads::Threads) + target_link_libraries(pthread-${x} PUBLIC "-Wl,-znorelro -Wl,-znoseparate-code") # TODO: convert to target_link_option once cmake is >3.13 + + add_dependencies(tests-mc pthread-${x}) + ADD_TESH_FACTORIES(pthread-mc-${x} "^thread" --setenv libdir=${CMAKE_BINARY_DIR}/lib --cd ${CMAKE_BINARY_DIR}/examples/sthread ${CMAKE_CURRENT_SOURCE_DIR}/pthread-mc-${x}.tesh) + endif() + + set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/pthread-mc-${x}.tesh) + set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/pthread-${x}.c) + +endforeach() + # Regular sthread examples: test the internal interface for debugging purpose ############################################################################# diff --git a/examples/sthread/pthread-mc-mutex-simpledeadlock.tesh b/examples/sthread/pthread-mc-mutex-simpledeadlock.tesh new file mode 100644 index 0000000000..63ff7761ca --- /dev/null +++ b/examples/sthread/pthread-mc-mutex-simpledeadlock.tesh @@ -0,0 +1,26 @@ + +# This test raises a deadlock, thus the return code of 3 +! expect return 3 + +# We ignore the LD_PRELOAD lines from the expected output because they contain the build path +! ignore .*LD_PRELOAD.* + +$ ${bindir:=.}/../../bin/simgrid-mc --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsgmalloc.so:${libdir:=.}/libsthread.so ${bindir:=.}/pthread-mutex-simpledeadlock +> [0.000000] [sthread/INFO] Starting the simulation. +> All threads are started. +> [0.000000] [mc_dfs/INFO] Start a DFS exploration. Reduction is: dpor. +> The thread 0 is terminating. +> The thread 1 is terminating. +> User's main is terminating. +> [0.000000] [mc_global/INFO] ************************** +> [0.000000] [mc_global/INFO] *** DEADLOCK DETECTED *** +> [0.000000] [mc_global/INFO] ************************** +> [0.000000] [mc_global/INFO] Counter-example execution trace: +> [0.000000] [mc_global/INFO] 2: MUTEX_ASYNC_LOCK(mutex: 0, owner:2) +> [0.000000] [mc_global/INFO] 2: MUTEX_WAIT(mutex: 0, owner:2) +> [0.000000] [mc_global/INFO] 3: MUTEX_ASYNC_LOCK(mutex: 1, owner:3) +> [0.000000] [mc_global/INFO] 2: MUTEX_ASYNC_LOCK(mutex: 1, owner:3) +> [0.000000] [mc_global/INFO] 3: MUTEX_WAIT(mutex: 1, owner:3) +> [0.000000] [mc_global/INFO] 3: MUTEX_ASYNC_LOCK(mutex: 0, owner:2) +> [0.000000] [mc_global/INFO] Path = 2;2;3;2;3;3 +> [0.000000] [mc_dfs/INFO] DFS exploration ended. 19 unique states visited; 2 backtracks (22 transition replays, 2 states visited overall) diff --git a/examples/sthread/pthread-mutex-simpledeadlock.c b/examples/sthread/pthread-mutex-simpledeadlock.c new file mode 100644 index 0000000000..9025cd3a81 --- /dev/null +++ b/examples/sthread/pthread-mutex-simpledeadlock.c @@ -0,0 +1,53 @@ +/* Simple test code that may deadlock: + + Thread 1 locks mutex1 then mutex2 while thread 2 locks in reverse order. + Deadlock occures when each thread get one mutex and then ask for the other one. + */ + +#include +#include + +pthread_mutex_t mutex1; +pthread_mutex_t mutex2; + +static void* thread_fun1(void* val) +{ + pthread_mutex_lock(&mutex1); + pthread_mutex_lock(&mutex2); + pthread_mutex_unlock(&mutex1); + pthread_mutex_unlock(&mutex2); + + fprintf(stderr, "The thread %d is terminating.\n", *(int*)val); + return NULL; +} +static void* thread_fun2(void* val) +{ + pthread_mutex_lock(&mutex2); + pthread_mutex_lock(&mutex1); + pthread_mutex_unlock(&mutex1); + pthread_mutex_unlock(&mutex2); + + fprintf(stderr, "The thread %d is terminating.\n", *(int*)val); + return NULL; +} + +int main(int argc, char* argv[]) +{ + pthread_mutex_init(&mutex1, NULL); + pthread_mutex_init(&mutex2, NULL); + + int id[2] = {0, 1}; + pthread_t thread1; + pthread_t thread2; + pthread_create(&thread1, NULL, thread_fun1, (void*)&id[0]); + pthread_create(&thread2, NULL, thread_fun2, (void*)&id[1]); + fprintf(stderr, "All threads are started.\n"); + pthread_join(thread1, NULL); + pthread_join(thread2, NULL); + + pthread_mutex_destroy(&mutex1); + pthread_mutex_destroy(&mutex2); + + fprintf(stderr, "User's main is terminating.\n"); + return 0; +}